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/DerivedTypes.h"
27 //===----------------------------------------------------------------------===//
28 // TerminatorInst Class
29 //===----------------------------------------------------------------------===//
31 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
32 /// block. Thus, these are all the flow control type of operations.
34 class TerminatorInst : public Instruction {
36 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
37 Use *Ops, unsigned NumOps,
38 Instruction *InsertBefore = 0)
39 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
41 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
42 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
43 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
45 // Out of line virtual method, so the vtable, etc has a home.
48 /// Virtual methods - Terminators should overload these and provide inline
49 /// overrides of non-V methods.
50 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
51 virtual unsigned getNumSuccessorsV() const = 0;
52 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
55 virtual Instruction *clone(LLVMContext &Context) const = 0;
57 /// getNumSuccessors - Return the number of successors that this terminator
59 unsigned getNumSuccessors() const {
60 return getNumSuccessorsV();
63 /// getSuccessor - Return the specified successor.
65 BasicBlock *getSuccessor(unsigned idx) const {
66 return getSuccessorV(idx);
69 /// setSuccessor - Update the specified successor to point at the provided
71 void setSuccessor(unsigned idx, BasicBlock *B) {
72 setSuccessorV(idx, B);
75 // Methods for support type inquiry through isa, cast, and dyn_cast:
76 static inline bool classof(const TerminatorInst *) { return true; }
77 static inline bool classof(const Instruction *I) {
78 return I->isTerminator();
80 static inline bool classof(const Value *V) {
81 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 //===----------------------------------------------------------------------===//
87 // UnaryInstruction Class
88 //===----------------------------------------------------------------------===//
90 class UnaryInstruction : public Instruction {
91 void *operator new(size_t, unsigned); // Do not implement
92 UnaryInstruction(const UnaryInstruction&); // 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::Malloc ||
120 I->getOpcode() == Instruction::Alloca ||
121 I->getOpcode() == Instruction::Free ||
122 I->getOpcode() == Instruction::Load ||
123 I->getOpcode() == Instruction::VAArg ||
124 I->getOpcode() == Instruction::ExtractValue ||
125 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
127 static inline bool classof(const Value *V) {
128 return isa<Instruction>(V) && classof(cast<Instruction>(V));
133 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
136 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
138 //===----------------------------------------------------------------------===//
139 // BinaryOperator Class
140 //===----------------------------------------------------------------------===//
142 class BinaryOperator : public Instruction {
143 void *operator new(size_t, unsigned); // Do not implement
145 void init(BinaryOps iType);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147 const std::string &Name, Instruction *InsertBefore);
148 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
149 const std::string &Name, BasicBlock *InsertAtEnd);
151 // allocate space for exactly two operands
152 void *operator new(size_t s) {
153 return User::operator new(s, 2);
156 /// Transparently provide more efficient getOperand methods.
157 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
159 /// Create() - Construct a binary instruction, given the opcode and the two
160 /// operands. Optionally (if InstBefore is specified) insert the instruction
161 /// into a BasicBlock right before the specified instruction. The specified
162 /// Instruction is allowed to be a dereferenced end iterator.
164 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
165 const std::string &Name = "",
166 Instruction *InsertBefore = 0);
168 /// Create() - Construct a binary instruction, given the opcode and the two
169 /// operands. Also automatically insert this instruction to the end of the
170 /// BasicBlock specified.
172 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
173 const std::string &Name,
174 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 std::string &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 std::string &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 std::string &Name, Instruction *I) {\
194 return Create(Instruction::OPC, V1, V2, Name, I);\
196 #include "llvm/Instruction.def"
199 /// Helper functions to construct and inspect unary operations (NEG and NOT)
200 /// via binary operators SUB and XOR:
202 /// CreateNeg, CreateNot - Create the NEG and NOT
203 /// instructions out of SUB and XOR instructions.
205 static BinaryOperator *CreateNeg(LLVMContext &Context,
206 Value *Op, const std::string &Name = "",
207 Instruction *InsertBefore = 0);
208 static BinaryOperator *CreateNeg(LLVMContext &Context,
209 Value *Op, const std::string &Name,
210 BasicBlock *InsertAtEnd);
211 static BinaryOperator *CreateFNeg(LLVMContext &Context,
212 Value *Op, const std::string &Name = "",
213 Instruction *InsertBefore = 0);
214 static BinaryOperator *CreateFNeg(LLVMContext &Context,
215 Value *Op, const std::string &Name,
216 BasicBlock *InsertAtEnd);
217 static BinaryOperator *CreateNot(LLVMContext &Context,
218 Value *Op, const std::string &Name = "",
219 Instruction *InsertBefore = 0);
220 static BinaryOperator *CreateNot(LLVMContext &Context,
221 Value *Op, const std::string &Name,
222 BasicBlock *InsertAtEnd);
224 /// isNeg, isFNeg, isNot - Check if the given Value is a
225 /// NEG, FNeg, or NOT instruction.
227 static bool isNeg(const Value *V);
228 static bool isFNeg(const Value *V);
229 static bool isNot(const Value *V);
231 /// getNegArgument, getNotArgument - Helper functions to extract the
232 /// unary argument of a NEG, FNEG or NOT operation implemented via
233 /// Sub, FSub, or Xor.
235 static const Value *getNegArgument(const Value *BinOp);
236 static Value *getNegArgument( Value *BinOp);
237 static const Value *getFNegArgument(const Value *BinOp);
238 static Value *getFNegArgument( Value *BinOp);
239 static const Value *getNotArgument(const Value *BinOp);
240 static Value *getNotArgument( Value *BinOp);
242 BinaryOps getOpcode() const {
243 return static_cast<BinaryOps>(Instruction::getOpcode());
246 virtual BinaryOperator *clone(LLVMContext &Context) const;
248 /// swapOperands - Exchange the two operands to this instruction.
249 /// This instruction is safe to use on any binary instruction and
250 /// does not modify the semantics of the instruction. If the instruction
251 /// cannot be reversed (ie, it's a Div), then return true.
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const BinaryOperator *) { return true; }
257 static inline bool classof(const Instruction *I) {
258 return I->isBinaryOp();
260 static inline bool classof(const Value *V) {
261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
266 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
271 //===----------------------------------------------------------------------===//
273 //===----------------------------------------------------------------------===//
275 /// CastInst - This is the base class for all instructions that perform data
276 /// casts. It is simply provided so that instruction category testing
277 /// can be performed with code like:
279 /// if (isa<CastInst>(Instr)) { ... }
280 /// @brief Base class of casting instructions.
281 class CastInst : public UnaryInstruction {
282 /// @brief Copy constructor
283 CastInst(const CastInst &CI)
284 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
286 /// @brief Do not allow default construction
289 /// @brief Constructor with insert-before-instruction semantics for subclasses
290 CastInst(const Type *Ty, unsigned iType, Value *S,
291 const std::string &NameStr = "", Instruction *InsertBefore = 0)
292 : UnaryInstruction(Ty, iType, S, InsertBefore) {
295 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
296 CastInst(const Type *Ty, unsigned iType, Value *S,
297 const std::string &NameStr, BasicBlock *InsertAtEnd)
298 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
302 /// Provides a way to construct any of the CastInst subclasses using an
303 /// opcode instead of the subclass's constructor. The opcode must be in the
304 /// CastOps category (Instruction::isCast(opcode) returns true). This
305 /// constructor has insert-before-instruction semantics to automatically
306 /// insert the new CastInst before InsertBefore (if it is non-null).
307 /// @brief Construct any of the CastInst subclasses
308 static CastInst *Create(
309 Instruction::CastOps, ///< The opcode of the cast instruction
310 Value *S, ///< The value to be casted (operand 0)
311 const Type *Ty, ///< The type to which cast should be made
312 const std::string &Name = "", ///< Name for the instruction
313 Instruction *InsertBefore = 0 ///< Place to insert the instruction
315 /// Provides a way to construct any of the CastInst subclasses using an
316 /// opcode instead of the subclass's constructor. The opcode must be in the
317 /// CastOps category. This constructor has insert-at-end-of-block semantics
318 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
320 /// @brief Construct any of the CastInst subclasses
321 static CastInst *Create(
322 Instruction::CastOps, ///< The opcode for the cast instruction
323 Value *S, ///< The value to be casted (operand 0)
324 const Type *Ty, ///< The type to which operand is casted
325 const std::string &Name, ///< The name for the instruction
326 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
329 /// @brief Create a ZExt or BitCast cast instruction
330 static CastInst *CreateZExtOrBitCast(
331 Value *S, ///< The value to be casted (operand 0)
332 const Type *Ty, ///< The type to which cast should be made
333 const std::string &Name = "", ///< Name for the instruction
334 Instruction *InsertBefore = 0 ///< Place to insert the instruction
337 /// @brief Create a ZExt or BitCast cast instruction
338 static CastInst *CreateZExtOrBitCast(
339 Value *S, ///< The value to be casted (operand 0)
340 const Type *Ty, ///< The type to which operand is casted
341 const std::string &Name, ///< The name for the instruction
342 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
345 /// @brief Create a SExt or BitCast cast instruction
346 static CastInst *CreateSExtOrBitCast(
347 Value *S, ///< The value to be casted (operand 0)
348 const Type *Ty, ///< The type to which cast should be made
349 const std::string &Name = "", ///< Name for the instruction
350 Instruction *InsertBefore = 0 ///< Place to insert the instruction
353 /// @brief Create a SExt or BitCast cast instruction
354 static CastInst *CreateSExtOrBitCast(
355 Value *S, ///< The value to be casted (operand 0)
356 const Type *Ty, ///< The type to which operand is casted
357 const std::string &Name, ///< The name for the instruction
358 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
361 /// @brief Create a BitCast or a PtrToInt cast instruction
362 static CastInst *CreatePointerCast(
363 Value *S, ///< The pointer value to be casted (operand 0)
364 const Type *Ty, ///< The type to which operand is casted
365 const std::string &Name, ///< The name for the instruction
366 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
369 /// @brief Create a BitCast or a PtrToInt cast instruction
370 static CastInst *CreatePointerCast(
371 Value *S, ///< The pointer value to be casted (operand 0)
372 const Type *Ty, ///< The type to which cast should be made
373 const std::string &Name = "", ///< Name for the instruction
374 Instruction *InsertBefore = 0 ///< Place to insert the instruction
377 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
378 static CastInst *CreateIntegerCast(
379 Value *S, ///< The pointer value to be casted (operand 0)
380 const Type *Ty, ///< The type to which cast should be made
381 bool isSigned, ///< Whether to regard S as signed or not
382 const std::string &Name = "", ///< Name for the instruction
383 Instruction *InsertBefore = 0 ///< Place to insert the instruction
386 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
387 static CastInst *CreateIntegerCast(
388 Value *S, ///< The integer value to be casted (operand 0)
389 const Type *Ty, ///< The integer type to which operand is casted
390 bool isSigned, ///< Whether to regard S as signed or not
391 const std::string &Name, ///< The name for the instruction
392 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
395 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
396 static CastInst *CreateFPCast(
397 Value *S, ///< The floating point value to be casted
398 const Type *Ty, ///< The floating point type to cast to
399 const std::string &Name = "", ///< Name for the instruction
400 Instruction *InsertBefore = 0 ///< Place to insert the instruction
403 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
404 static CastInst *CreateFPCast(
405 Value *S, ///< The floating point value to be casted
406 const Type *Ty, ///< The floating point type to cast to
407 const std::string &Name, ///< The name for the instruction
408 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
411 /// @brief Create a Trunc or BitCast cast instruction
412 static CastInst *CreateTruncOrBitCast(
413 Value *S, ///< The value to be casted (operand 0)
414 const Type *Ty, ///< The type to which cast should be made
415 const std::string &Name = "", ///< Name for the instruction
416 Instruction *InsertBefore = 0 ///< Place to insert the instruction
419 /// @brief Create a Trunc or BitCast cast instruction
420 static CastInst *CreateTruncOrBitCast(
421 Value *S, ///< The value to be casted (operand 0)
422 const Type *Ty, ///< The type to which operand is casted
423 const std::string &Name, ///< The name for the instruction
424 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
427 /// @brief Check whether it is valid to call getCastOpcode for these types.
428 static bool isCastable(
429 const Type *SrcTy, ///< The Type from which the value should be cast.
430 const Type *DestTy ///< The Type to which the value should be cast.
433 /// Returns the opcode necessary to cast Val into Ty using usual casting
435 /// @brief Infer the opcode for cast operand and type
436 static Instruction::CastOps getCastOpcode(
437 const Value *Val, ///< The value to cast
438 bool SrcIsSigned, ///< Whether to treat the source as signed
439 const Type *Ty, ///< The Type to which the value should be casted
440 bool DstIsSigned ///< Whether to treate the dest. as signed
443 /// There are several places where we need to know if a cast instruction
444 /// only deals with integer source and destination types. To simplify that
445 /// logic, this method is provided.
446 /// @returns true iff the cast has only integral typed operand and dest type.
447 /// @brief Determine if this is an integer-only cast.
448 bool isIntegerCast() const;
450 /// A lossless cast is one that does not alter the basic value. It implies
451 /// a no-op cast but is more stringent, preventing things like int->float,
452 /// long->double, int->ptr, or vector->anything.
453 /// @returns true iff the cast is lossless.
454 /// @brief Determine if this is a lossless cast.
455 bool isLosslessCast() const;
457 /// A no-op cast is one that can be effected without changing any bits.
458 /// It implies that the source and destination types are the same size. The
459 /// IntPtrTy argument is used to make accurate determinations for casts
460 /// involving Integer and Pointer types. They are no-op casts if the integer
461 /// is the same size as the pointer. However, pointer size varies with
462 /// platform. Generally, the result of TargetData::getIntPtrType() should be
463 /// passed in. If that's not available, use Type::Int64Ty, which will make
464 /// the isNoopCast call conservative.
465 /// @brief Determine if this cast is a no-op cast.
467 const Type *IntPtrTy ///< Integer type corresponding to pointer
470 /// Determine how a pair of casts can be eliminated, if they can be at all.
471 /// This is a helper function for both CastInst and ConstantExpr.
472 /// @returns 0 if the CastInst pair can't be eliminated
473 /// @returns Instruction::CastOps value for a cast that can replace
474 /// the pair, casting SrcTy to DstTy.
475 /// @brief Determine if a cast pair is eliminable
476 static unsigned isEliminableCastPair(
477 Instruction::CastOps firstOpcode, ///< Opcode of first cast
478 Instruction::CastOps secondOpcode, ///< Opcode of second cast
479 const Type *SrcTy, ///< SrcTy of 1st cast
480 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
481 const Type *DstTy, ///< DstTy of 2nd cast
482 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
485 /// @brief Return the opcode of this CastInst
486 Instruction::CastOps getOpcode() const {
487 return Instruction::CastOps(Instruction::getOpcode());
490 /// @brief Return the source type, as a convenience
491 const Type* getSrcTy() const { return getOperand(0)->getType(); }
492 /// @brief Return the destination type, as a convenience
493 const Type* getDestTy() const { return getType(); }
495 /// This method can be used to determine if a cast from S to DstTy using
496 /// Opcode op is valid or not.
497 /// @returns true iff the proposed cast is valid.
498 /// @brief Determine if a cast is valid without creating one.
499 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
501 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
502 static inline bool classof(const CastInst *) { return true; }
503 static inline bool classof(const Instruction *I) {
506 static inline bool classof(const Value *V) {
507 return isa<Instruction>(V) && classof(cast<Instruction>(V));
511 //===----------------------------------------------------------------------===//
513 //===----------------------------------------------------------------------===//
515 /// This class is the base class for the comparison instructions.
516 /// @brief Abstract base class of comparison instructions.
517 // FIXME: why not derive from BinaryOperator?
518 class CmpInst: public Instruction {
519 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
520 CmpInst(); // do not implement
522 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
523 Value *LHS, Value *RHS, const std::string &Name = "",
524 Instruction *InsertBefore = 0);
526 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
527 Value *LHS, Value *RHS, const std::string &Name,
528 BasicBlock *InsertAtEnd);
531 /// This enumeration lists the possible predicates for CmpInst subclasses.
532 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
533 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
534 /// predicate values are not overlapping between the classes.
536 // Opcode U L G E Intuitive operation
537 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
538 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
539 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
540 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
541 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
542 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
543 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
544 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
545 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
546 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
547 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
548 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
549 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
550 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
551 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
552 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
553 FIRST_FCMP_PREDICATE = FCMP_FALSE,
554 LAST_FCMP_PREDICATE = FCMP_TRUE,
555 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
556 ICMP_EQ = 32, /// equal
557 ICMP_NE = 33, /// not equal
558 ICMP_UGT = 34, /// unsigned greater than
559 ICMP_UGE = 35, /// unsigned greater or equal
560 ICMP_ULT = 36, /// unsigned less than
561 ICMP_ULE = 37, /// unsigned less or equal
562 ICMP_SGT = 38, /// signed greater than
563 ICMP_SGE = 39, /// signed greater or equal
564 ICMP_SLT = 40, /// signed less than
565 ICMP_SLE = 41, /// signed less or equal
566 FIRST_ICMP_PREDICATE = ICMP_EQ,
567 LAST_ICMP_PREDICATE = ICMP_SLE,
568 BAD_ICMP_PREDICATE = ICMP_SLE + 1
571 // allocate space for exactly two operands
572 void *operator new(size_t s) {
573 return User::operator new(s, 2);
575 /// Construct a compare instruction, given the opcode, the predicate and
576 /// the two operands. Optionally (if InstBefore is specified) insert the
577 /// instruction into a BasicBlock right before the specified instruction.
578 /// The specified Instruction is allowed to be a dereferenced end iterator.
579 /// @brief Create a CmpInst
580 static CmpInst *Create(LLVMContext &Context, OtherOps Op,
581 unsigned short predicate, Value *S1,
582 Value *S2, const std::string &Name = "",
583 Instruction *InsertBefore = 0);
585 /// Construct a compare instruction, given the opcode, the predicate and the
586 /// two operands. Also automatically insert this instruction to the end of
587 /// the BasicBlock specified.
588 /// @brief Create a CmpInst
589 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
590 Value *S2, const std::string &Name,
591 BasicBlock *InsertAtEnd);
593 /// @brief Get the opcode casted to the right type
594 OtherOps getOpcode() const {
595 return static_cast<OtherOps>(Instruction::getOpcode());
598 /// @brief Return the predicate for this instruction.
599 Predicate getPredicate() const { return Predicate(SubclassData); }
601 /// @brief Set the predicate for this instruction to the specified value.
602 void setPredicate(Predicate P) { SubclassData = P; }
604 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
605 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
606 /// @returns the inverse predicate for the instruction's current predicate.
607 /// @brief Return the inverse of the instruction's predicate.
608 Predicate getInversePredicate() const {
609 return getInversePredicate(getPredicate());
612 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
613 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
614 /// @returns the inverse predicate for predicate provided in \p pred.
615 /// @brief Return the inverse of a given predicate
616 static Predicate getInversePredicate(Predicate pred);
618 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
619 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
620 /// @returns the predicate that would be the result of exchanging the two
621 /// operands of the CmpInst instruction without changing the result
623 /// @brief Return the predicate as if the operands were swapped
624 Predicate getSwappedPredicate() const {
625 return getSwappedPredicate(getPredicate());
628 /// This is a static version that you can use without an instruction
630 /// @brief Return the predicate as if the operands were swapped.
631 static Predicate getSwappedPredicate(Predicate pred);
633 /// @brief Provide more efficient getOperand methods.
634 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
636 /// This is just a convenience that dispatches to the subclasses.
637 /// @brief Swap the operands and adjust predicate accordingly to retain
638 /// the same comparison.
641 /// This is just a convenience that dispatches to the subclasses.
642 /// @brief Determine if this CmpInst is commutative.
643 bool isCommutative();
645 /// This is just a convenience that dispatches to the subclasses.
646 /// @brief Determine if this is an equals/not equals predicate.
649 /// @returns true if the predicate is unsigned, false otherwise.
650 /// @brief Determine if the predicate is an unsigned operation.
651 static bool isUnsigned(unsigned short predicate);
653 /// @returns true if the predicate is signed, false otherwise.
654 /// @brief Determine if the predicate is an signed operation.
655 static bool isSigned(unsigned short predicate);
657 /// @brief Determine if the predicate is an ordered operation.
658 static bool isOrdered(unsigned short predicate);
660 /// @brief Determine if the predicate is an unordered operation.
661 static bool isUnordered(unsigned short predicate);
663 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
664 static inline bool classof(const CmpInst *) { return true; }
665 static inline bool classof(const Instruction *I) {
666 return I->getOpcode() == Instruction::ICmp ||
667 I->getOpcode() == Instruction::FCmp;
669 static inline bool classof(const Value *V) {
670 return isa<Instruction>(V) && classof(cast<Instruction>(V));
675 // FIXME: these are redundant if CmpInst < BinaryOperator
677 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
680 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
682 } // End llvm namespace