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"
28 //===----------------------------------------------------------------------===//
29 // TerminatorInst Class
30 //===----------------------------------------------------------------------===//
32 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
33 /// block. Thus, these are all the flow control type of operations.
35 class TerminatorInst : public Instruction {
37 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps,
39 Instruction *InsertBefore = 0)
40 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
42 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
43 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
44 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
46 // Out of line virtual method, so the vtable, etc has a home.
49 /// Virtual methods - Terminators should overload these and provide inline
50 /// overrides of non-V methods.
51 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
52 virtual unsigned getNumSuccessorsV() const = 0;
53 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
56 virtual Instruction *clone(LLVMContext &Context) 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
93 UnaryInstruction(const UnaryInstruction&); // Do not implement
96 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
98 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
101 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
102 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
106 // allocate space for exactly one operand
107 void *operator new(size_t s) {
108 return User::operator new(s, 1);
111 // Out of line virtual method, so the vtable, etc has a home.
114 /// Transparently provide more efficient getOperand methods.
115 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
117 // Methods for support type inquiry through isa, cast, and dyn_cast:
118 static inline bool classof(const UnaryInstruction *) { return true; }
119 static inline bool classof(const Instruction *I) {
120 return I->getOpcode() == Instruction::Malloc ||
121 I->getOpcode() == Instruction::Alloca ||
122 I->getOpcode() == Instruction::Free ||
123 I->getOpcode() == Instruction::Load ||
124 I->getOpcode() == Instruction::VAArg ||
125 I->getOpcode() == Instruction::ExtractValue ||
126 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
128 static inline bool classof(const Value *V) {
129 return isa<Instruction>(V) && classof(cast<Instruction>(V));
134 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
137 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
139 //===----------------------------------------------------------------------===//
140 // BinaryOperator Class
141 //===----------------------------------------------------------------------===//
143 class BinaryOperator : public Instruction {
144 void *operator new(size_t, unsigned); // Do not implement
146 void init(BinaryOps iType);
147 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
148 const Twine &Name, Instruction *InsertBefore);
149 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
150 const Twine &Name, BasicBlock *InsertAtEnd);
152 // allocate space for exactly two operands
153 void *operator new(size_t s) {
154 return User::operator new(s, 2);
157 /// Transparently provide more efficient getOperand methods.
158 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
160 /// Create() - Construct a binary instruction, given the opcode and the two
161 /// operands. Optionally (if InstBefore is specified) insert the instruction
162 /// into a BasicBlock right before the specified instruction. The specified
163 /// Instruction is allowed to be a dereferenced end iterator.
165 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
166 const Twine &Name = "",
167 Instruction *InsertBefore = 0);
169 /// Create() - Construct a binary instruction, given the opcode and the two
170 /// operands. Also automatically insert this instruction to the end of the
171 /// BasicBlock specified.
173 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
174 const Twine &Name, BasicBlock *InsertAtEnd);
176 /// Create* - These methods just forward to Create, and are useful when you
177 /// statically know what type of instruction you're going to create. These
178 /// helpers just save some typing.
179 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
180 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
181 const Twine &Name = "") {\
182 return Create(Instruction::OPC, V1, V2, Name);\
184 #include "llvm/Instruction.def"
185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
186 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
187 const Twine &Name, BasicBlock *BB) {\
188 return Create(Instruction::OPC, V1, V2, Name, BB);\
190 #include "llvm/Instruction.def"
191 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
192 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
193 const Twine &Name, Instruction *I) {\
194 return Create(Instruction::OPC, V1, V2, Name, I);\
196 #include "llvm/Instruction.def"
199 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
201 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
202 const Twine &Name = "") {
203 BinaryOperator *BO = CreateAdd(V1, V2, Name);
204 cast<AddOperator>(BO)->setHasNoSignedWrap(true);
207 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
208 const Twine &Name, BasicBlock *BB) {
209 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
210 cast<AddOperator>(BO)->setHasNoSignedWrap(true);
213 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
214 const Twine &Name, Instruction *I) {
215 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
216 cast<AddOperator>(BO)->setHasNoSignedWrap(true);
220 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
222 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
223 const Twine &Name = "") {
224 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
225 cast<SDivOperator>(BO)->setIsExact(true);
228 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
229 const Twine &Name, BasicBlock *BB) {
230 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
231 cast<SDivOperator>(BO)->setIsExact(true);
234 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
235 const Twine &Name, Instruction *I) {
236 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
237 cast<SDivOperator>(BO)->setIsExact(true);
241 /// Helper functions to construct and inspect unary operations (NEG and NOT)
242 /// via binary operators SUB and XOR:
244 /// CreateNeg, CreateNot - Create the NEG and NOT
245 /// instructions out of SUB and XOR instructions.
247 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
248 Instruction *InsertBefore = 0);
249 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
250 BasicBlock *InsertAtEnd);
251 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
252 Instruction *InsertBefore = 0);
253 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
254 BasicBlock *InsertAtEnd);
255 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
256 Instruction *InsertBefore = 0);
257 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
258 BasicBlock *InsertAtEnd);
260 /// isNeg, isFNeg, isNot - Check if the given Value is a
261 /// NEG, FNeg, or NOT instruction.
263 static bool isNeg(const Value *V);
264 static bool isFNeg(const Value *V);
265 static bool isNot(const Value *V);
267 /// getNegArgument, getNotArgument - Helper functions to extract the
268 /// unary argument of a NEG, FNEG or NOT operation implemented via
269 /// Sub, FSub, or Xor.
271 static const Value *getNegArgument(const Value *BinOp);
272 static Value *getNegArgument( Value *BinOp);
273 static const Value *getFNegArgument(const Value *BinOp);
274 static Value *getFNegArgument( Value *BinOp);
275 static const Value *getNotArgument(const Value *BinOp);
276 static Value *getNotArgument( Value *BinOp);
278 BinaryOps getOpcode() const {
279 return static_cast<BinaryOps>(Instruction::getOpcode());
282 virtual BinaryOperator *clone(LLVMContext &Context) const;
284 /// swapOperands - Exchange the two operands to this instruction.
285 /// This instruction is safe to use on any binary instruction and
286 /// does not modify the semantics of the instruction. If the instruction
287 /// cannot be reversed (ie, it's a Div), then return true.
291 // Methods for support type inquiry through isa, cast, and dyn_cast:
292 static inline bool classof(const BinaryOperator *) { return true; }
293 static inline bool classof(const Instruction *I) {
294 return I->isBinaryOp();
296 static inline bool classof(const Value *V) {
297 return isa<Instruction>(V) && classof(cast<Instruction>(V));
302 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
305 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
307 //===----------------------------------------------------------------------===//
309 //===----------------------------------------------------------------------===//
311 /// CastInst - This is the base class for all instructions that perform data
312 /// casts. It is simply provided so that instruction category testing
313 /// can be performed with code like:
315 /// if (isa<CastInst>(Instr)) { ... }
316 /// @brief Base class of casting instructions.
317 class CastInst : public UnaryInstruction {
318 /// @brief Copy constructor
319 CastInst(const CastInst &CI)
320 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
322 /// @brief Do not allow default construction
325 /// @brief Constructor with insert-before-instruction semantics for subclasses
326 CastInst(const Type *Ty, unsigned iType, Value *S,
327 const Twine &NameStr = "", Instruction *InsertBefore = 0)
328 : UnaryInstruction(Ty, iType, S, InsertBefore) {
331 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
332 CastInst(const Type *Ty, unsigned iType, Value *S,
333 const Twine &NameStr, BasicBlock *InsertAtEnd)
334 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
338 /// Provides a way to construct any of the CastInst subclasses using an
339 /// opcode instead of the subclass's constructor. The opcode must be in the
340 /// CastOps category (Instruction::isCast(opcode) returns true). This
341 /// constructor has insert-before-instruction semantics to automatically
342 /// insert the new CastInst before InsertBefore (if it is non-null).
343 /// @brief Construct any of the CastInst subclasses
344 static CastInst *Create(
345 Instruction::CastOps, ///< The opcode of the cast instruction
346 Value *S, ///< The value to be casted (operand 0)
347 const Type *Ty, ///< The type to which cast should be made
348 const Twine &Name = "", ///< Name for the instruction
349 Instruction *InsertBefore = 0 ///< Place to insert the instruction
351 /// Provides a way to construct any of the CastInst subclasses using an
352 /// opcode instead of the subclass's constructor. The opcode must be in the
353 /// CastOps category. This constructor has insert-at-end-of-block semantics
354 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
356 /// @brief Construct any of the CastInst subclasses
357 static CastInst *Create(
358 Instruction::CastOps, ///< The opcode for the cast instruction
359 Value *S, ///< The value to be casted (operand 0)
360 const Type *Ty, ///< The type to which operand is casted
361 const Twine &Name, ///< The name for the instruction
362 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
365 /// @brief Create a ZExt or BitCast cast instruction
366 static CastInst *CreateZExtOrBitCast(
367 Value *S, ///< The value to be casted (operand 0)
368 const Type *Ty, ///< The type to which cast should be made
369 const Twine &Name = "", ///< Name for the instruction
370 Instruction *InsertBefore = 0 ///< Place to insert the instruction
373 /// @brief Create a ZExt or BitCast cast instruction
374 static CastInst *CreateZExtOrBitCast(
375 Value *S, ///< The value to be casted (operand 0)
376 const Type *Ty, ///< The type to which operand is casted
377 const Twine &Name, ///< The name for the instruction
378 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
381 /// @brief Create a SExt or BitCast cast instruction
382 static CastInst *CreateSExtOrBitCast(
383 Value *S, ///< The value to be casted (operand 0)
384 const Type *Ty, ///< The type to which cast should be made
385 const Twine &Name = "", ///< Name for the instruction
386 Instruction *InsertBefore = 0 ///< Place to insert the instruction
389 /// @brief Create a SExt or BitCast cast instruction
390 static CastInst *CreateSExtOrBitCast(
391 Value *S, ///< The value to be casted (operand 0)
392 const Type *Ty, ///< The type to which operand is casted
393 const Twine &Name, ///< The name for the instruction
394 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
397 /// @brief Create a BitCast or a PtrToInt cast instruction
398 static CastInst *CreatePointerCast(
399 Value *S, ///< The pointer value to be casted (operand 0)
400 const Type *Ty, ///< The type to which operand is casted
401 const Twine &Name, ///< The name for the instruction
402 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
405 /// @brief Create a BitCast or a PtrToInt cast instruction
406 static CastInst *CreatePointerCast(
407 Value *S, ///< The pointer value to be casted (operand 0)
408 const Type *Ty, ///< The type to which cast should be made
409 const Twine &Name = "", ///< Name for the instruction
410 Instruction *InsertBefore = 0 ///< Place to insert the instruction
413 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
414 static CastInst *CreateIntegerCast(
415 Value *S, ///< The pointer value to be casted (operand 0)
416 const Type *Ty, ///< The type to which cast should be made
417 bool isSigned, ///< Whether to regard S as signed or not
418 const Twine &Name = "", ///< Name for the instruction
419 Instruction *InsertBefore = 0 ///< Place to insert the instruction
422 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
423 static CastInst *CreateIntegerCast(
424 Value *S, ///< The integer value to be casted (operand 0)
425 const Type *Ty, ///< The integer type to which operand is casted
426 bool isSigned, ///< Whether to regard S as signed or not
427 const Twine &Name, ///< The name for the instruction
428 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
431 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
432 static CastInst *CreateFPCast(
433 Value *S, ///< The floating point value to be casted
434 const Type *Ty, ///< The floating point type to cast to
435 const Twine &Name = "", ///< Name for the instruction
436 Instruction *InsertBefore = 0 ///< Place to insert the instruction
439 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
440 static CastInst *CreateFPCast(
441 Value *S, ///< The floating point value to be casted
442 const Type *Ty, ///< The floating point type to cast to
443 const Twine &Name, ///< The name for the instruction
444 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
447 /// @brief Create a Trunc or BitCast cast instruction
448 static CastInst *CreateTruncOrBitCast(
449 Value *S, ///< The value to be casted (operand 0)
450 const Type *Ty, ///< The type to which cast should be made
451 const Twine &Name = "", ///< Name for the instruction
452 Instruction *InsertBefore = 0 ///< Place to insert the instruction
455 /// @brief Create a Trunc or BitCast cast instruction
456 static CastInst *CreateTruncOrBitCast(
457 Value *S, ///< The value to be casted (operand 0)
458 const Type *Ty, ///< The type to which operand is casted
459 const Twine &Name, ///< The name for the instruction
460 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
463 /// @brief Check whether it is valid to call getCastOpcode for these types.
464 static bool isCastable(
465 const Type *SrcTy, ///< The Type from which the value should be cast.
466 const Type *DestTy ///< The Type to which the value should be cast.
469 /// Returns the opcode necessary to cast Val into Ty using usual casting
471 /// @brief Infer the opcode for cast operand and type
472 static Instruction::CastOps getCastOpcode(
473 const Value *Val, ///< The value to cast
474 bool SrcIsSigned, ///< Whether to treat the source as signed
475 const Type *Ty, ///< The Type to which the value should be casted
476 bool DstIsSigned ///< Whether to treate the dest. as signed
479 /// There are several places where we need to know if a cast instruction
480 /// only deals with integer source and destination types. To simplify that
481 /// logic, this method is provided.
482 /// @returns true iff the cast has only integral typed operand and dest type.
483 /// @brief Determine if this is an integer-only cast.
484 bool isIntegerCast() const;
486 /// A lossless cast is one that does not alter the basic value. It implies
487 /// a no-op cast but is more stringent, preventing things like int->float,
488 /// long->double, int->ptr, or vector->anything.
489 /// @returns true iff the cast is lossless.
490 /// @brief Determine if this is a lossless cast.
491 bool isLosslessCast() const;
493 /// A no-op cast is one that can be effected without changing any bits.
494 /// It implies that the source and destination types are the same size. The
495 /// IntPtrTy argument is used to make accurate determinations for casts
496 /// involving Integer and Pointer types. They are no-op casts if the integer
497 /// is the same size as the pointer. However, pointer size varies with
498 /// platform. Generally, the result of TargetData::getIntPtrType() should be
499 /// passed in. If that's not available, use Type::Int64Ty, which will make
500 /// the isNoopCast call conservative.
501 /// @brief Determine if this cast is a no-op cast.
503 const Type *IntPtrTy ///< Integer type corresponding to pointer
506 /// Determine how a pair of casts can be eliminated, if they can be at all.
507 /// This is a helper function for both CastInst and ConstantExpr.
508 /// @returns 0 if the CastInst pair can't be eliminated
509 /// @returns Instruction::CastOps value for a cast that can replace
510 /// the pair, casting SrcTy to DstTy.
511 /// @brief Determine if a cast pair is eliminable
512 static unsigned isEliminableCastPair(
513 Instruction::CastOps firstOpcode, ///< Opcode of first cast
514 Instruction::CastOps secondOpcode, ///< Opcode of second cast
515 const Type *SrcTy, ///< SrcTy of 1st cast
516 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
517 const Type *DstTy, ///< DstTy of 2nd cast
518 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
521 /// @brief Return the opcode of this CastInst
522 Instruction::CastOps getOpcode() const {
523 return Instruction::CastOps(Instruction::getOpcode());
526 /// @brief Return the source type, as a convenience
527 const Type* getSrcTy() const { return getOperand(0)->getType(); }
528 /// @brief Return the destination type, as a convenience
529 const Type* getDestTy() const { return getType(); }
531 /// This method can be used to determine if a cast from S to DstTy using
532 /// Opcode op is valid or not.
533 /// @returns true iff the proposed cast is valid.
534 /// @brief Determine if a cast is valid without creating one.
535 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
537 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
538 static inline bool classof(const CastInst *) { return true; }
539 static inline bool classof(const Instruction *I) {
542 static inline bool classof(const Value *V) {
543 return isa<Instruction>(V) && classof(cast<Instruction>(V));
547 //===----------------------------------------------------------------------===//
549 //===----------------------------------------------------------------------===//
551 /// This class is the base class for the comparison instructions.
552 /// @brief Abstract base class of comparison instructions.
553 // FIXME: why not derive from BinaryOperator?
554 class CmpInst: public Instruction {
555 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
556 CmpInst(); // do not implement
558 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
559 Value *LHS, Value *RHS, const Twine &Name = "",
560 Instruction *InsertBefore = 0);
562 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
563 Value *LHS, Value *RHS, const Twine &Name,
564 BasicBlock *InsertAtEnd);
567 /// This enumeration lists the possible predicates for CmpInst subclasses.
568 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
569 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
570 /// predicate values are not overlapping between the classes.
572 // Opcode U L G E Intuitive operation
573 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
574 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
575 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
576 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
577 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
578 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
579 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
580 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
581 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
582 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
583 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
584 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
585 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
586 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
587 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
588 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
589 FIRST_FCMP_PREDICATE = FCMP_FALSE,
590 LAST_FCMP_PREDICATE = FCMP_TRUE,
591 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
592 ICMP_EQ = 32, /// equal
593 ICMP_NE = 33, /// not equal
594 ICMP_UGT = 34, /// unsigned greater than
595 ICMP_UGE = 35, /// unsigned greater or equal
596 ICMP_ULT = 36, /// unsigned less than
597 ICMP_ULE = 37, /// unsigned less or equal
598 ICMP_SGT = 38, /// signed greater than
599 ICMP_SGE = 39, /// signed greater or equal
600 ICMP_SLT = 40, /// signed less than
601 ICMP_SLE = 41, /// signed less or equal
602 FIRST_ICMP_PREDICATE = ICMP_EQ,
603 LAST_ICMP_PREDICATE = ICMP_SLE,
604 BAD_ICMP_PREDICATE = ICMP_SLE + 1
607 // allocate space for exactly two operands
608 void *operator new(size_t s) {
609 return User::operator new(s, 2);
611 /// Construct a compare instruction, given the opcode, the predicate and
612 /// the two operands. Optionally (if InstBefore is specified) insert the
613 /// instruction into a BasicBlock right before the specified instruction.
614 /// The specified Instruction is allowed to be a dereferenced end iterator.
615 /// @brief Create a CmpInst
616 static CmpInst *Create(LLVMContext &Context, OtherOps Op,
617 unsigned short predicate, Value *S1,
618 Value *S2, const Twine &Name = "",
619 Instruction *InsertBefore = 0);
621 /// Construct a compare instruction, given the opcode, the predicate and the
622 /// two operands. Also automatically insert this instruction to the end of
623 /// the BasicBlock specified.
624 /// @brief Create a CmpInst
625 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
626 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
628 /// @brief Get the opcode casted to the right type
629 OtherOps getOpcode() const {
630 return static_cast<OtherOps>(Instruction::getOpcode());
633 /// @brief Return the predicate for this instruction.
634 Predicate getPredicate() const { return Predicate(SubclassData); }
636 /// @brief Set the predicate for this instruction to the specified value.
637 void setPredicate(Predicate P) { SubclassData = P; }
639 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
640 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
641 /// @returns the inverse predicate for the instruction's current predicate.
642 /// @brief Return the inverse of the instruction's predicate.
643 Predicate getInversePredicate() const {
644 return getInversePredicate(getPredicate());
647 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
648 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
649 /// @returns the inverse predicate for predicate provided in \p pred.
650 /// @brief Return the inverse of a given predicate
651 static Predicate getInversePredicate(Predicate pred);
653 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
654 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
655 /// @returns the predicate that would be the result of exchanging the two
656 /// operands of the CmpInst instruction without changing the result
658 /// @brief Return the predicate as if the operands were swapped
659 Predicate getSwappedPredicate() const {
660 return getSwappedPredicate(getPredicate());
663 /// This is a static version that you can use without an instruction
665 /// @brief Return the predicate as if the operands were swapped.
666 static Predicate getSwappedPredicate(Predicate pred);
668 /// @brief Provide more efficient getOperand methods.
669 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
671 /// This is just a convenience that dispatches to the subclasses.
672 /// @brief Swap the operands and adjust predicate accordingly to retain
673 /// the same comparison.
676 /// This is just a convenience that dispatches to the subclasses.
677 /// @brief Determine if this CmpInst is commutative.
678 bool isCommutative();
680 /// This is just a convenience that dispatches to the subclasses.
681 /// @brief Determine if this is an equals/not equals predicate.
684 /// @returns true if the predicate is unsigned, false otherwise.
685 /// @brief Determine if the predicate is an unsigned operation.
686 static bool isUnsigned(unsigned short predicate);
688 /// @returns true if the predicate is signed, false otherwise.
689 /// @brief Determine if the predicate is an signed operation.
690 static bool isSigned(unsigned short predicate);
692 /// @brief Determine if the predicate is an ordered operation.
693 static bool isOrdered(unsigned short predicate);
695 /// @brief Determine if the predicate is an unordered operation.
696 static bool isUnordered(unsigned short predicate);
698 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
699 static inline bool classof(const CmpInst *) { return true; }
700 static inline bool classof(const Instruction *I) {
701 return I->getOpcode() == Instruction::ICmp ||
702 I->getOpcode() == Instruction::FCmp;
704 static inline bool classof(const Value *V) {
705 return isa<Instruction>(V) && classof(cast<Instruction>(V));
708 /// @brief Create a result type for fcmp/icmp
709 static const Type* makeCmpResultType(const Type* opnd_type) {
710 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
711 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
712 vt->getNumElements());
714 return Type::getInt1Ty(opnd_type->getContext());
719 // FIXME: these are redundant if CmpInst < BinaryOperator
721 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
724 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
726 } // End llvm namespace