1
//===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines various meta classes of instructions that exist in the VM
11
// representation. Specific concrete subclasses of these may be found in the
14
//===----------------------------------------------------------------------===//
16
#ifndef LLVM_INSTRUCTION_TYPES_H
17
#define LLVM_INSTRUCTION_TYPES_H
19
#include "llvm/Instruction.h"
20
#include "llvm/OperandTraits.h"
21
#include "llvm/Operator.h"
22
#include "llvm/DerivedTypes.h"
23
#include "llvm/ADT/Twine.h"
29
//===----------------------------------------------------------------------===//
30
// TerminatorInst Class
31
//===----------------------------------------------------------------------===//
33
/// TerminatorInst - Subclasses of this class are all able to terminate a basic
34
/// block. Thus, these are all the flow control type of operations.
36
class TerminatorInst : public Instruction {
38
TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39
Use *Ops, unsigned NumOps,
40
Instruction *InsertBefore = 0)
41
: Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
43
TerminatorInst(const Type *Ty, Instruction::TermOps iType,
44
Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
45
: Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
47
// Out of line virtual method, so the vtable, etc has a home.
50
/// Virtual methods - Terminators should overload these and provide inline
51
/// overrides of non-V methods.
52
virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
53
virtual unsigned getNumSuccessorsV() const = 0;
54
virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
55
virtual TerminatorInst *clone_impl() const = 0;
58
/// getNumSuccessors - Return the number of successors that this terminator
60
unsigned getNumSuccessors() const {
61
return getNumSuccessorsV();
64
/// getSuccessor - Return the specified successor.
66
BasicBlock *getSuccessor(unsigned idx) const {
67
return getSuccessorV(idx);
70
/// setSuccessor - Update the specified successor to point at the provided
72
void setSuccessor(unsigned idx, BasicBlock *B) {
73
setSuccessorV(idx, B);
76
// Methods for support type inquiry through isa, cast, and dyn_cast:
77
static inline bool classof(const TerminatorInst *) { return true; }
78
static inline bool classof(const Instruction *I) {
79
return I->isTerminator();
81
static inline bool classof(const Value *V) {
82
return isa<Instruction>(V) && classof(cast<Instruction>(V));
87
//===----------------------------------------------------------------------===//
88
// UnaryInstruction Class
89
//===----------------------------------------------------------------------===//
91
class UnaryInstruction : public Instruction {
92
void *operator new(size_t, unsigned); // Do not implement
95
UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
97
: Instruction(Ty, iType, &Op<0>(), 1, IB) {
100
UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
101
: Instruction(Ty, iType, &Op<0>(), 1, IAE) {
105
// allocate space for exactly one operand
106
void *operator new(size_t s) {
107
return User::operator new(s, 1);
110
// Out of line virtual method, so the vtable, etc has a home.
113
/// Transparently provide more efficient getOperand methods.
114
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
116
// Methods for support type inquiry through isa, cast, and dyn_cast:
117
static inline bool classof(const UnaryInstruction *) { return true; }
118
static inline bool classof(const Instruction *I) {
119
return I->getOpcode() == Instruction::Alloca ||
120
I->getOpcode() == Instruction::Load ||
121
I->getOpcode() == Instruction::VAArg ||
122
I->getOpcode() == Instruction::ExtractValue ||
123
(I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
125
static inline bool classof(const Value *V) {
126
return isa<Instruction>(V) && classof(cast<Instruction>(V));
131
struct OperandTraits<UnaryInstruction> : public FixedNumOperandTraits<1> {
134
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
136
//===----------------------------------------------------------------------===//
137
// BinaryOperator Class
138
//===----------------------------------------------------------------------===//
140
class BinaryOperator : public Instruction {
141
void *operator new(size_t, unsigned); // Do not implement
143
void init(BinaryOps iType);
144
BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
145
const Twine &Name, Instruction *InsertBefore);
146
BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147
const Twine &Name, BasicBlock *InsertAtEnd);
148
virtual BinaryOperator *clone_impl() const;
150
// allocate space for exactly two operands
151
void *operator new(size_t s) {
152
return User::operator new(s, 2);
155
/// Transparently provide more efficient getOperand methods.
156
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
158
/// Create() - Construct a binary instruction, given the opcode and the two
159
/// operands. Optionally (if InstBefore is specified) insert the instruction
160
/// into a BasicBlock right before the specified instruction. The specified
161
/// Instruction is allowed to be a dereferenced end iterator.
163
static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
164
const Twine &Name = Twine(),
165
Instruction *InsertBefore = 0);
167
/// Create() - Construct a binary instruction, given the opcode and the two
168
/// operands. Also automatically insert this instruction to the end of the
169
/// BasicBlock specified.
171
static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
172
const Twine &Name, BasicBlock *InsertAtEnd);
174
/// Create* - These methods just forward to Create, and are useful when you
175
/// statically know what type of instruction you're going to create. These
176
/// helpers just save some typing.
177
#define HANDLE_BINARY_INST(N, OPC, CLASS) \
178
static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
179
const Twine &Name = "") {\
180
return Create(Instruction::OPC, V1, V2, Name);\
182
#include "llvm/Instruction.def"
183
#define HANDLE_BINARY_INST(N, OPC, CLASS) \
184
static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
185
const Twine &Name, BasicBlock *BB) {\
186
return Create(Instruction::OPC, V1, V2, Name, BB);\
188
#include "llvm/Instruction.def"
189
#define HANDLE_BINARY_INST(N, OPC, CLASS) \
190
static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
191
const Twine &Name, Instruction *I) {\
192
return Create(Instruction::OPC, V1, V2, Name, I);\
194
#include "llvm/Instruction.def"
197
/// CreateNSWAdd - Create an Add operator with the NSW flag set.
199
static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
200
const Twine &Name = "") {
201
BinaryOperator *BO = CreateAdd(V1, V2, Name);
202
BO->setHasNoSignedWrap(true);
205
static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
206
const Twine &Name, BasicBlock *BB) {
207
BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
208
BO->setHasNoSignedWrap(true);
211
static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
212
const Twine &Name, Instruction *I) {
213
BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
214
BO->setHasNoSignedWrap(true);
218
/// CreateNUWAdd - Create an Add operator with the NUW flag set.
220
static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
221
const Twine &Name = "") {
222
BinaryOperator *BO = CreateAdd(V1, V2, Name);
223
BO->setHasNoUnsignedWrap(true);
226
static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
227
const Twine &Name, BasicBlock *BB) {
228
BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
229
BO->setHasNoUnsignedWrap(true);
232
static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
233
const Twine &Name, Instruction *I) {
234
BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
235
BO->setHasNoUnsignedWrap(true);
239
/// CreateNSWSub - Create an Sub operator with the NSW flag set.
241
static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
242
const Twine &Name = "") {
243
BinaryOperator *BO = CreateSub(V1, V2, Name);
244
BO->setHasNoSignedWrap(true);
247
static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
248
const Twine &Name, BasicBlock *BB) {
249
BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
250
BO->setHasNoSignedWrap(true);
253
static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
254
const Twine &Name, Instruction *I) {
255
BinaryOperator *BO = CreateSub(V1, V2, Name, I);
256
BO->setHasNoSignedWrap(true);
260
/// CreateNUWSub - Create an Sub operator with the NUW flag set.
262
static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
263
const Twine &Name = "") {
264
BinaryOperator *BO = CreateSub(V1, V2, Name);
265
BO->setHasNoUnsignedWrap(true);
268
static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
269
const Twine &Name, BasicBlock *BB) {
270
BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
271
BO->setHasNoUnsignedWrap(true);
274
static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
275
const Twine &Name, Instruction *I) {
276
BinaryOperator *BO = CreateSub(V1, V2, Name, I);
277
BO->setHasNoUnsignedWrap(true);
281
/// CreateNSWMul - Create a Mul operator with the NSW flag set.
283
static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
284
const Twine &Name = "") {
285
BinaryOperator *BO = CreateMul(V1, V2, Name);
286
BO->setHasNoSignedWrap(true);
289
static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
290
const Twine &Name, BasicBlock *BB) {
291
BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
292
BO->setHasNoSignedWrap(true);
295
static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
296
const Twine &Name, Instruction *I) {
297
BinaryOperator *BO = CreateMul(V1, V2, Name, I);
298
BO->setHasNoSignedWrap(true);
302
/// CreateNUWMul - Create a Mul operator with the NUW flag set.
304
static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
305
const Twine &Name = "") {
306
BinaryOperator *BO = CreateMul(V1, V2, Name);
307
BO->setHasNoUnsignedWrap(true);
310
static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
311
const Twine &Name, BasicBlock *BB) {
312
BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
313
BO->setHasNoUnsignedWrap(true);
316
static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
317
const Twine &Name, Instruction *I) {
318
BinaryOperator *BO = CreateMul(V1, V2, Name, I);
319
BO->setHasNoUnsignedWrap(true);
323
/// CreateExactSDiv - Create an SDiv operator with the exact flag set.
325
static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
326
const Twine &Name = "") {
327
BinaryOperator *BO = CreateSDiv(V1, V2, Name);
328
BO->setIsExact(true);
331
static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
332
const Twine &Name, BasicBlock *BB) {
333
BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
334
BO->setIsExact(true);
337
static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
338
const Twine &Name, Instruction *I) {
339
BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
340
BO->setIsExact(true);
344
/// Helper functions to construct and inspect unary operations (NEG and NOT)
345
/// via binary operators SUB and XOR:
347
/// CreateNeg, CreateNot - Create the NEG and NOT
348
/// instructions out of SUB and XOR instructions.
350
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
351
Instruction *InsertBefore = 0);
352
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
353
BasicBlock *InsertAtEnd);
354
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
355
Instruction *InsertBefore = 0);
356
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
357
BasicBlock *InsertAtEnd);
358
static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
359
Instruction *InsertBefore = 0);
360
static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
361
BasicBlock *InsertAtEnd);
362
static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
363
Instruction *InsertBefore = 0);
364
static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
365
BasicBlock *InsertAtEnd);
366
static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
367
Instruction *InsertBefore = 0);
368
static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
369
BasicBlock *InsertAtEnd);
371
/// isNeg, isFNeg, isNot - Check if the given Value is a
372
/// NEG, FNeg, or NOT instruction.
374
static bool isNeg(const Value *V);
375
static bool isFNeg(const Value *V);
376
static bool isNot(const Value *V);
378
/// getNegArgument, getNotArgument - Helper functions to extract the
379
/// unary argument of a NEG, FNEG or NOT operation implemented via
380
/// Sub, FSub, or Xor.
382
static const Value *getNegArgument(const Value *BinOp);
383
static Value *getNegArgument( Value *BinOp);
384
static const Value *getFNegArgument(const Value *BinOp);
385
static Value *getFNegArgument( Value *BinOp);
386
static const Value *getNotArgument(const Value *BinOp);
387
static Value *getNotArgument( Value *BinOp);
389
BinaryOps getOpcode() const {
390
return static_cast<BinaryOps>(Instruction::getOpcode());
393
/// swapOperands - Exchange the two operands to this instruction.
394
/// This instruction is safe to use on any binary instruction and
395
/// does not modify the semantics of the instruction. If the instruction
396
/// cannot be reversed (ie, it's a Div), then return true.
400
/// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
401
/// which must be an operator which supports this flag. See LangRef.html
402
/// for the meaning of this flag.
403
void setHasNoUnsignedWrap(bool b = true);
405
/// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
406
/// which must be an operator which supports this flag. See LangRef.html
407
/// for the meaning of this flag.
408
void setHasNoSignedWrap(bool b = true);
410
/// setIsExact - Set or clear the exact flag on this instruction,
411
/// which must be an operator which supports this flag. See LangRef.html
412
/// for the meaning of this flag.
413
void setIsExact(bool b = true);
415
/// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
416
bool hasNoUnsignedWrap() const;
418
/// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
419
bool hasNoSignedWrap() const;
421
/// isExact - Determine whether the exact flag is set.
422
bool isExact() const;
424
// Methods for support type inquiry through isa, cast, and dyn_cast:
425
static inline bool classof(const BinaryOperator *) { return true; }
426
static inline bool classof(const Instruction *I) {
427
return I->isBinaryOp();
429
static inline bool classof(const Value *V) {
430
return isa<Instruction>(V) && classof(cast<Instruction>(V));
435
struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
438
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
440
//===----------------------------------------------------------------------===//
442
//===----------------------------------------------------------------------===//
444
/// CastInst - This is the base class for all instructions that perform data
445
/// casts. It is simply provided so that instruction category testing
446
/// can be performed with code like:
448
/// if (isa<CastInst>(Instr)) { ... }
449
/// @brief Base class of casting instructions.
450
class CastInst : public UnaryInstruction {
452
/// @brief Constructor with insert-before-instruction semantics for subclasses
453
CastInst(const Type *Ty, unsigned iType, Value *S,
454
const Twine &NameStr = "", Instruction *InsertBefore = 0)
455
: UnaryInstruction(Ty, iType, S, InsertBefore) {
458
/// @brief Constructor with insert-at-end-of-block semantics for subclasses
459
CastInst(const Type *Ty, unsigned iType, Value *S,
460
const Twine &NameStr, BasicBlock *InsertAtEnd)
461
: UnaryInstruction(Ty, iType, S, InsertAtEnd) {
465
/// Provides a way to construct any of the CastInst subclasses using an
466
/// opcode instead of the subclass's constructor. The opcode must be in the
467
/// CastOps category (Instruction::isCast(opcode) returns true). This
468
/// constructor has insert-before-instruction semantics to automatically
469
/// insert the new CastInst before InsertBefore (if it is non-null).
470
/// @brief Construct any of the CastInst subclasses
471
static CastInst *Create(
472
Instruction::CastOps, ///< The opcode of the cast instruction
473
Value *S, ///< The value to be casted (operand 0)
474
const Type *Ty, ///< The type to which cast should be made
475
const Twine &Name = "", ///< Name for the instruction
476
Instruction *InsertBefore = 0 ///< Place to insert the instruction
478
/// Provides a way to construct any of the CastInst subclasses using an
479
/// opcode instead of the subclass's constructor. The opcode must be in the
480
/// CastOps category. This constructor has insert-at-end-of-block semantics
481
/// to automatically insert the new CastInst at the end of InsertAtEnd (if
483
/// @brief Construct any of the CastInst subclasses
484
static CastInst *Create(
485
Instruction::CastOps, ///< The opcode for the cast instruction
486
Value *S, ///< The value to be casted (operand 0)
487
const Type *Ty, ///< The type to which operand is casted
488
const Twine &Name, ///< The name for the instruction
489
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
492
/// @brief Create a ZExt or BitCast cast instruction
493
static CastInst *CreateZExtOrBitCast(
494
Value *S, ///< The value to be casted (operand 0)
495
const Type *Ty, ///< The type to which cast should be made
496
const Twine &Name = "", ///< Name for the instruction
497
Instruction *InsertBefore = 0 ///< Place to insert the instruction
500
/// @brief Create a ZExt or BitCast cast instruction
501
static CastInst *CreateZExtOrBitCast(
502
Value *S, ///< The value to be casted (operand 0)
503
const Type *Ty, ///< The type to which operand is casted
504
const Twine &Name, ///< The name for the instruction
505
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
508
/// @brief Create a SExt or BitCast cast instruction
509
static CastInst *CreateSExtOrBitCast(
510
Value *S, ///< The value to be casted (operand 0)
511
const Type *Ty, ///< The type to which cast should be made
512
const Twine &Name = "", ///< Name for the instruction
513
Instruction *InsertBefore = 0 ///< Place to insert the instruction
516
/// @brief Create a SExt or BitCast cast instruction
517
static CastInst *CreateSExtOrBitCast(
518
Value *S, ///< The value to be casted (operand 0)
519
const Type *Ty, ///< The type to which operand is casted
520
const Twine &Name, ///< The name for the instruction
521
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
524
/// @brief Create a BitCast or a PtrToInt cast instruction
525
static CastInst *CreatePointerCast(
526
Value *S, ///< The pointer value to be casted (operand 0)
527
const Type *Ty, ///< The type to which operand is casted
528
const Twine &Name, ///< The name for the instruction
529
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
532
/// @brief Create a BitCast or a PtrToInt cast instruction
533
static CastInst *CreatePointerCast(
534
Value *S, ///< The pointer value to be casted (operand 0)
535
const Type *Ty, ///< The type to which cast should be made
536
const Twine &Name = "", ///< Name for the instruction
537
Instruction *InsertBefore = 0 ///< Place to insert the instruction
540
/// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
541
static CastInst *CreateIntegerCast(
542
Value *S, ///< The pointer value to be casted (operand 0)
543
const Type *Ty, ///< The type to which cast should be made
544
bool isSigned, ///< Whether to regard S as signed or not
545
const Twine &Name = "", ///< Name for the instruction
546
Instruction *InsertBefore = 0 ///< Place to insert the instruction
549
/// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
550
static CastInst *CreateIntegerCast(
551
Value *S, ///< The integer value to be casted (operand 0)
552
const Type *Ty, ///< The integer type to which operand is casted
553
bool isSigned, ///< Whether to regard S as signed or not
554
const Twine &Name, ///< The name for the instruction
555
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
558
/// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
559
static CastInst *CreateFPCast(
560
Value *S, ///< The floating point value to be casted
561
const Type *Ty, ///< The floating point type to cast to
562
const Twine &Name = "", ///< Name for the instruction
563
Instruction *InsertBefore = 0 ///< Place to insert the instruction
566
/// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
567
static CastInst *CreateFPCast(
568
Value *S, ///< The floating point value to be casted
569
const Type *Ty, ///< The floating point type to cast to
570
const Twine &Name, ///< The name for the instruction
571
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
574
/// @brief Create a Trunc or BitCast cast instruction
575
static CastInst *CreateTruncOrBitCast(
576
Value *S, ///< The value to be casted (operand 0)
577
const Type *Ty, ///< The type to which cast should be made
578
const Twine &Name = "", ///< Name for the instruction
579
Instruction *InsertBefore = 0 ///< Place to insert the instruction
582
/// @brief Create a Trunc or BitCast cast instruction
583
static CastInst *CreateTruncOrBitCast(
584
Value *S, ///< The value to be casted (operand 0)
585
const Type *Ty, ///< The type to which operand is casted
586
const Twine &Name, ///< The name for the instruction
587
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
590
/// @brief Check whether it is valid to call getCastOpcode for these types.
591
static bool isCastable(
592
const Type *SrcTy, ///< The Type from which the value should be cast.
593
const Type *DestTy ///< The Type to which the value should be cast.
596
/// Returns the opcode necessary to cast Val into Ty using usual casting
598
/// @brief Infer the opcode for cast operand and type
599
static Instruction::CastOps getCastOpcode(
600
const Value *Val, ///< The value to cast
601
bool SrcIsSigned, ///< Whether to treat the source as signed
602
const Type *Ty, ///< The Type to which the value should be casted
603
bool DstIsSigned ///< Whether to treate the dest. as signed
606
/// There are several places where we need to know if a cast instruction
607
/// only deals with integer source and destination types. To simplify that
608
/// logic, this method is provided.
609
/// @returns true iff the cast has only integral typed operand and dest type.
610
/// @brief Determine if this is an integer-only cast.
611
bool isIntegerCast() const;
613
/// A lossless cast is one that does not alter the basic value. It implies
614
/// a no-op cast but is more stringent, preventing things like int->float,
615
/// long->double, int->ptr, or vector->anything.
616
/// @returns true iff the cast is lossless.
617
/// @brief Determine if this is a lossless cast.
618
bool isLosslessCast() const;
620
/// A no-op cast is one that can be effected without changing any bits.
621
/// It implies that the source and destination types are the same size. The
622
/// IntPtrTy argument is used to make accurate determinations for casts
623
/// involving Integer and Pointer types. They are no-op casts if the integer
624
/// is the same size as the pointer. However, pointer size varies with
625
/// platform. Generally, the result of TargetData::getIntPtrType() should be
626
/// passed in. If that's not available, use Type::Int64Ty, which will make
627
/// the isNoopCast call conservative.
628
/// @brief Determine if this cast is a no-op cast.
630
const Type *IntPtrTy ///< Integer type corresponding to pointer
633
/// Determine how a pair of casts can be eliminated, if they can be at all.
634
/// This is a helper function for both CastInst and ConstantExpr.
635
/// @returns 0 if the CastInst pair can't be eliminated
636
/// @returns Instruction::CastOps value for a cast that can replace
637
/// the pair, casting SrcTy to DstTy.
638
/// @brief Determine if a cast pair is eliminable
639
static unsigned isEliminableCastPair(
640
Instruction::CastOps firstOpcode, ///< Opcode of first cast
641
Instruction::CastOps secondOpcode, ///< Opcode of second cast
642
const Type *SrcTy, ///< SrcTy of 1st cast
643
const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
644
const Type *DstTy, ///< DstTy of 2nd cast
645
const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
648
/// @brief Return the opcode of this CastInst
649
Instruction::CastOps getOpcode() const {
650
return Instruction::CastOps(Instruction::getOpcode());
653
/// @brief Return the source type, as a convenience
654
const Type* getSrcTy() const { return getOperand(0)->getType(); }
655
/// @brief Return the destination type, as a convenience
656
const Type* getDestTy() const { return getType(); }
658
/// This method can be used to determine if a cast from S to DstTy using
659
/// Opcode op is valid or not.
660
/// @returns true iff the proposed cast is valid.
661
/// @brief Determine if a cast is valid without creating one.
662
static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
664
/// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
665
static inline bool classof(const CastInst *) { return true; }
666
static inline bool classof(const Instruction *I) {
669
static inline bool classof(const Value *V) {
670
return isa<Instruction>(V) && classof(cast<Instruction>(V));
674
//===----------------------------------------------------------------------===//
676
//===----------------------------------------------------------------------===//
678
/// This class is the base class for the comparison instructions.
679
/// @brief Abstract base class of comparison instructions.
680
class CmpInst : public Instruction {
681
void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
682
CmpInst(); // do not implement
684
CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
685
Value *LHS, Value *RHS, const Twine &Name = "",
686
Instruction *InsertBefore = 0);
688
CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
689
Value *LHS, Value *RHS, const Twine &Name,
690
BasicBlock *InsertAtEnd);
692
virtual void Anchor() const; // Out of line virtual method.
694
/// This enumeration lists the possible predicates for CmpInst subclasses.
695
/// Values in the range 0-31 are reserved for FCmpInst, while values in the
696
/// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
697
/// predicate values are not overlapping between the classes.
699
// Opcode U L G E Intuitive operation
700
FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
701
FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
702
FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
703
FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
704
FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
705
FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
706
FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
707
FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
708
FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
709
FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
710
FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
711
FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
712
FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
713
FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
714
FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
715
FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
716
FIRST_FCMP_PREDICATE = FCMP_FALSE,
717
LAST_FCMP_PREDICATE = FCMP_TRUE,
718
BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
719
ICMP_EQ = 32, ///< equal
720
ICMP_NE = 33, ///< not equal
721
ICMP_UGT = 34, ///< unsigned greater than
722
ICMP_UGE = 35, ///< unsigned greater or equal
723
ICMP_ULT = 36, ///< unsigned less than
724
ICMP_ULE = 37, ///< unsigned less or equal
725
ICMP_SGT = 38, ///< signed greater than
726
ICMP_SGE = 39, ///< signed greater or equal
727
ICMP_SLT = 40, ///< signed less than
728
ICMP_SLE = 41, ///< signed less or equal
729
FIRST_ICMP_PREDICATE = ICMP_EQ,
730
LAST_ICMP_PREDICATE = ICMP_SLE,
731
BAD_ICMP_PREDICATE = ICMP_SLE + 1
734
// allocate space for exactly two operands
735
void *operator new(size_t s) {
736
return User::operator new(s, 2);
738
/// Construct a compare instruction, given the opcode, the predicate and
739
/// the two operands. Optionally (if InstBefore is specified) insert the
740
/// instruction into a BasicBlock right before the specified instruction.
741
/// The specified Instruction is allowed to be a dereferenced end iterator.
742
/// @brief Create a CmpInst
743
static CmpInst *Create(OtherOps Op,
744
unsigned short predicate, Value *S1,
745
Value *S2, const Twine &Name = "",
746
Instruction *InsertBefore = 0);
748
/// Construct a compare instruction, given the opcode, the predicate and the
749
/// two operands. Also automatically insert this instruction to the end of
750
/// the BasicBlock specified.
751
/// @brief Create a CmpInst
752
static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
753
Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
755
/// @brief Get the opcode casted to the right type
756
OtherOps getOpcode() const {
757
return static_cast<OtherOps>(Instruction::getOpcode());
760
/// @brief Return the predicate for this instruction.
761
Predicate getPredicate() const {
762
return Predicate(getSubclassDataFromInstruction());
765
/// @brief Set the predicate for this instruction to the specified value.
766
void setPredicate(Predicate P) { setInstructionSubclassData(P); }
768
static bool isFPPredicate(Predicate P) {
769
return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
772
static bool isIntPredicate(Predicate P) {
773
return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
776
bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
777
bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
780
/// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
781
/// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
782
/// @returns the inverse predicate for the instruction's current predicate.
783
/// @brief Return the inverse of the instruction's predicate.
784
Predicate getInversePredicate() const {
785
return getInversePredicate(getPredicate());
788
/// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
789
/// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
790
/// @returns the inverse predicate for predicate provided in \p pred.
791
/// @brief Return the inverse of a given predicate
792
static Predicate getInversePredicate(Predicate pred);
794
/// For example, EQ->EQ, SLE->SGE, ULT->UGT,
795
/// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
796
/// @returns the predicate that would be the result of exchanging the two
797
/// operands of the CmpInst instruction without changing the result
799
/// @brief Return the predicate as if the operands were swapped
800
Predicate getSwappedPredicate() const {
801
return getSwappedPredicate(getPredicate());
804
/// This is a static version that you can use without an instruction
806
/// @brief Return the predicate as if the operands were swapped.
807
static Predicate getSwappedPredicate(Predicate pred);
809
/// @brief Provide more efficient getOperand methods.
810
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
812
/// This is just a convenience that dispatches to the subclasses.
813
/// @brief Swap the operands and adjust predicate accordingly to retain
814
/// the same comparison.
817
/// This is just a convenience that dispatches to the subclasses.
818
/// @brief Determine if this CmpInst is commutative.
819
bool isCommutative();
821
/// This is just a convenience that dispatches to the subclasses.
822
/// @brief Determine if this is an equals/not equals predicate.
825
/// @returns true if the comparison is signed, false otherwise.
826
/// @brief Determine if this instruction is using a signed comparison.
827
bool isSigned() const {
828
return isSigned(getPredicate());
831
/// @returns true if the comparison is unsigned, false otherwise.
832
/// @brief Determine if this instruction is using an unsigned comparison.
833
bool isUnsigned() const {
834
return isUnsigned(getPredicate());
837
/// This is just a convenience.
838
/// @brief Determine if this is true when both operands are the same.
839
bool isTrueWhenEqual() const {
840
return isTrueWhenEqual(getPredicate());
843
/// This is just a convenience.
844
/// @brief Determine if this is false when both operands are the same.
845
bool isFalseWhenEqual() const {
846
return isFalseWhenEqual(getPredicate());
849
/// @returns true if the predicate is unsigned, false otherwise.
850
/// @brief Determine if the predicate is an unsigned operation.
851
static bool isUnsigned(unsigned short predicate);
853
/// @returns true if the predicate is signed, false otherwise.
854
/// @brief Determine if the predicate is an signed operation.
855
static bool isSigned(unsigned short predicate);
857
/// @brief Determine if the predicate is an ordered operation.
858
static bool isOrdered(unsigned short predicate);
860
/// @brief Determine if the predicate is an unordered operation.
861
static bool isUnordered(unsigned short predicate);
863
/// Determine if the predicate is true when comparing a value with itself.
864
static bool isTrueWhenEqual(unsigned short predicate);
866
/// Determine if the predicate is false when comparing a value with itself.
867
static bool isFalseWhenEqual(unsigned short predicate);
869
/// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
870
static inline bool classof(const CmpInst *) { return true; }
871
static inline bool classof(const Instruction *I) {
872
return I->getOpcode() == Instruction::ICmp ||
873
I->getOpcode() == Instruction::FCmp;
875
static inline bool classof(const Value *V) {
876
return isa<Instruction>(V) && classof(cast<Instruction>(V));
879
/// @brief Create a result type for fcmp/icmp
880
static const Type* makeCmpResultType(const Type* opnd_type) {
881
if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
882
return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
883
vt->getNumElements());
885
return Type::getInt1Ty(opnd_type->getContext());
888
// Shadow Value::setValueSubclassData with a private forwarding method so that
889
// subclasses cannot accidentally use it.
890
void setValueSubclassData(unsigned short D) {
891
Value::setValueSubclassData(D);
896
// FIXME: these are redundant if CmpInst < BinaryOperator
898
struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
901
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
903
} // End llvm namespace