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(Value *Op, const std::string &Name = "",
206 Instruction *InsertBefore = 0);
207 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
208 BasicBlock *InsertAtEnd);
209 static BinaryOperator *CreateFNeg(Value *Op, const std::string &Name = "",
210 Instruction *InsertBefore = 0);
211 static BinaryOperator *CreateFNeg(Value *Op, const std::string &Name,
212 BasicBlock *InsertAtEnd);
213 static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
214 Instruction *InsertBefore = 0);
215 static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
216 BasicBlock *InsertAtEnd);
218 /// isNeg, isFNeg, isNot - Check if the given Value is a
219 /// NEG, FNeg, or NOT instruction.
221 static bool isNeg(const Value *V);
222 static bool isFNeg(const Value *V);
223 static bool isNot(const Value *V);
225 /// getNegArgument, getNotArgument - Helper functions to extract the
226 /// unary argument of a NEG, FNEG or NOT operation implemented via
227 /// Sub, FSub, or Xor.
229 static const Value *getNegArgument(const Value *BinOp);
230 static Value *getNegArgument( Value *BinOp);
231 static const Value *getFNegArgument(const Value *BinOp);
232 static Value *getFNegArgument( Value *BinOp);
233 static const Value *getNotArgument(const Value *BinOp);
234 static Value *getNotArgument( Value *BinOp);
236 BinaryOps getOpcode() const {
237 return static_cast<BinaryOps>(Instruction::getOpcode());
240 virtual BinaryOperator *clone(LLVMContext &Context) const;
242 /// swapOperands - Exchange the two operands to this instruction.
243 /// This instruction is safe to use on any binary instruction and
244 /// does not modify the semantics of the instruction. If the instruction
245 /// cannot be reversed (ie, it's a Div), then return true.
249 // Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const BinaryOperator *) { return true; }
251 static inline bool classof(const Instruction *I) {
252 return I->isBinaryOp();
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
260 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
265 //===----------------------------------------------------------------------===//
267 //===----------------------------------------------------------------------===//
269 /// CastInst - This is the base class for all instructions that perform data
270 /// casts. It is simply provided so that instruction category testing
271 /// can be performed with code like:
273 /// if (isa<CastInst>(Instr)) { ... }
274 /// @brief Base class of casting instructions.
275 class CastInst : public UnaryInstruction {
276 /// @brief Copy constructor
277 CastInst(const CastInst &CI)
278 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
280 /// @brief Do not allow default construction
283 /// @brief Constructor with insert-before-instruction semantics for subclasses
284 CastInst(const Type *Ty, unsigned iType, Value *S,
285 const std::string &NameStr = "", Instruction *InsertBefore = 0)
286 : UnaryInstruction(Ty, iType, S, InsertBefore) {
289 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
290 CastInst(const Type *Ty, unsigned iType, Value *S,
291 const std::string &NameStr, BasicBlock *InsertAtEnd)
292 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
296 /// Provides a way to construct any of the CastInst subclasses using an
297 /// opcode instead of the subclass's constructor. The opcode must be in the
298 /// CastOps category (Instruction::isCast(opcode) returns true). This
299 /// constructor has insert-before-instruction semantics to automatically
300 /// insert the new CastInst before InsertBefore (if it is non-null).
301 /// @brief Construct any of the CastInst subclasses
302 static CastInst *Create(
303 Instruction::CastOps, ///< The opcode of the cast instruction
304 Value *S, ///< The value to be casted (operand 0)
305 const Type *Ty, ///< The type to which cast should be made
306 const std::string &Name = "", ///< Name for the instruction
307 Instruction *InsertBefore = 0 ///< Place to insert the instruction
309 /// Provides a way to construct any of the CastInst subclasses using an
310 /// opcode instead of the subclass's constructor. The opcode must be in the
311 /// CastOps category. This constructor has insert-at-end-of-block semantics
312 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
314 /// @brief Construct any of the CastInst subclasses
315 static CastInst *Create(
316 Instruction::CastOps, ///< The opcode for the cast instruction
317 Value *S, ///< The value to be casted (operand 0)
318 const Type *Ty, ///< The type to which operand is casted
319 const std::string &Name, ///< The name for the instruction
320 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
323 /// @brief Create a ZExt or BitCast cast instruction
324 static CastInst *CreateZExtOrBitCast(
325 Value *S, ///< The value to be casted (operand 0)
326 const Type *Ty, ///< The type to which cast should be made
327 const std::string &Name = "", ///< Name for the instruction
328 Instruction *InsertBefore = 0 ///< Place to insert the instruction
331 /// @brief Create a ZExt or BitCast cast instruction
332 static CastInst *CreateZExtOrBitCast(
333 Value *S, ///< The value to be casted (operand 0)
334 const Type *Ty, ///< The type to which operand is casted
335 const std::string &Name, ///< The name for the instruction
336 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
339 /// @brief Create a SExt or BitCast cast instruction
340 static CastInst *CreateSExtOrBitCast(
341 Value *S, ///< The value to be casted (operand 0)
342 const Type *Ty, ///< The type to which cast should be made
343 const std::string &Name = "", ///< Name for the instruction
344 Instruction *InsertBefore = 0 ///< Place to insert the instruction
347 /// @brief Create a SExt or BitCast cast instruction
348 static CastInst *CreateSExtOrBitCast(
349 Value *S, ///< The value to be casted (operand 0)
350 const Type *Ty, ///< The type to which operand is casted
351 const std::string &Name, ///< The name for the instruction
352 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
355 /// @brief Create a BitCast or a PtrToInt cast instruction
356 static CastInst *CreatePointerCast(
357 Value *S, ///< The pointer value to be casted (operand 0)
358 const Type *Ty, ///< The type to which operand is casted
359 const std::string &Name, ///< The name for the instruction
360 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
363 /// @brief Create a BitCast or a PtrToInt cast instruction
364 static CastInst *CreatePointerCast(
365 Value *S, ///< The pointer value to be casted (operand 0)
366 const Type *Ty, ///< The type to which cast should be made
367 const std::string &Name = "", ///< Name for the instruction
368 Instruction *InsertBefore = 0 ///< Place to insert the instruction
371 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
372 static CastInst *CreateIntegerCast(
373 Value *S, ///< The pointer value to be casted (operand 0)
374 const Type *Ty, ///< The type to which cast should be made
375 bool isSigned, ///< Whether to regard S as signed or not
376 const std::string &Name = "", ///< Name for the instruction
377 Instruction *InsertBefore = 0 ///< Place to insert the instruction
380 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
381 static CastInst *CreateIntegerCast(
382 Value *S, ///< The integer value to be casted (operand 0)
383 const Type *Ty, ///< The integer type to which operand is casted
384 bool isSigned, ///< Whether to regard S as signed or not
385 const std::string &Name, ///< The name for the instruction
386 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
389 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
390 static CastInst *CreateFPCast(
391 Value *S, ///< The floating point value to be casted
392 const Type *Ty, ///< The floating point type to cast to
393 const std::string &Name = "", ///< Name for the instruction
394 Instruction *InsertBefore = 0 ///< Place to insert the instruction
397 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
398 static CastInst *CreateFPCast(
399 Value *S, ///< The floating point value to be casted
400 const Type *Ty, ///< The floating point type to cast to
401 const std::string &Name, ///< The name for the instruction
402 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
405 /// @brief Create a Trunc or BitCast cast instruction
406 static CastInst *CreateTruncOrBitCast(
407 Value *S, ///< The value to be casted (operand 0)
408 const Type *Ty, ///< The type to which cast should be made
409 const std::string &Name = "", ///< Name for the instruction
410 Instruction *InsertBefore = 0 ///< Place to insert the instruction
413 /// @brief Create a Trunc or BitCast cast instruction
414 static CastInst *CreateTruncOrBitCast(
415 Value *S, ///< The value to be casted (operand 0)
416 const Type *Ty, ///< The type to which operand is casted
417 const std::string &Name, ///< The name for the instruction
418 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
421 /// @brief Check whether it is valid to call getCastOpcode for these types.
422 static bool isCastable(
423 const Type *SrcTy, ///< The Type from which the value should be cast.
424 const Type *DestTy ///< The Type to which the value should be cast.
427 /// Returns the opcode necessary to cast Val into Ty using usual casting
429 /// @brief Infer the opcode for cast operand and type
430 static Instruction::CastOps getCastOpcode(
431 const Value *Val, ///< The value to cast
432 bool SrcIsSigned, ///< Whether to treat the source as signed
433 const Type *Ty, ///< The Type to which the value should be casted
434 bool DstIsSigned ///< Whether to treate the dest. as signed
437 /// There are several places where we need to know if a cast instruction
438 /// only deals with integer source and destination types. To simplify that
439 /// logic, this method is provided.
440 /// @returns true iff the cast has only integral typed operand and dest type.
441 /// @brief Determine if this is an integer-only cast.
442 bool isIntegerCast() const;
444 /// A lossless cast is one that does not alter the basic value. It implies
445 /// a no-op cast but is more stringent, preventing things like int->float,
446 /// long->double, int->ptr, or vector->anything.
447 /// @returns true iff the cast is lossless.
448 /// @brief Determine if this is a lossless cast.
449 bool isLosslessCast() const;
451 /// A no-op cast is one that can be effected without changing any bits.
452 /// It implies that the source and destination types are the same size. The
453 /// IntPtrTy argument is used to make accurate determinations for casts
454 /// involving Integer and Pointer types. They are no-op casts if the integer
455 /// is the same size as the pointer. However, pointer size varies with
456 /// platform. Generally, the result of TargetData::getIntPtrType() should be
457 /// passed in. If that's not available, use Type::Int64Ty, which will make
458 /// the isNoopCast call conservative.
459 /// @brief Determine if this cast is a no-op cast.
461 const Type *IntPtrTy ///< Integer type corresponding to pointer
464 /// Determine how a pair of casts can be eliminated, if they can be at all.
465 /// This is a helper function for both CastInst and ConstantExpr.
466 /// @returns 0 if the CastInst pair can't be eliminated
467 /// @returns Instruction::CastOps value for a cast that can replace
468 /// the pair, casting SrcTy to DstTy.
469 /// @brief Determine if a cast pair is eliminable
470 static unsigned isEliminableCastPair(
471 Instruction::CastOps firstOpcode, ///< Opcode of first cast
472 Instruction::CastOps secondOpcode, ///< Opcode of second cast
473 const Type *SrcTy, ///< SrcTy of 1st cast
474 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
475 const Type *DstTy, ///< DstTy of 2nd cast
476 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
479 /// @brief Return the opcode of this CastInst
480 Instruction::CastOps getOpcode() const {
481 return Instruction::CastOps(Instruction::getOpcode());
484 /// @brief Return the source type, as a convenience
485 const Type* getSrcTy() const { return getOperand(0)->getType(); }
486 /// @brief Return the destination type, as a convenience
487 const Type* getDestTy() const { return getType(); }
489 /// This method can be used to determine if a cast from S to DstTy using
490 /// Opcode op is valid or not.
491 /// @returns true iff the proposed cast is valid.
492 /// @brief Determine if a cast is valid without creating one.
493 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
495 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
496 static inline bool classof(const CastInst *) { return true; }
497 static inline bool classof(const Instruction *I) {
500 static inline bool classof(const Value *V) {
501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
505 //===----------------------------------------------------------------------===//
507 //===----------------------------------------------------------------------===//
509 /// This class is the base class for the comparison instructions.
510 /// @brief Abstract base class of comparison instructions.
511 // FIXME: why not derive from BinaryOperator?
512 class CmpInst: public Instruction {
513 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
514 CmpInst(); // do not implement
516 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
517 Value *LHS, Value *RHS, const std::string &Name = "",
518 Instruction *InsertBefore = 0);
520 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
521 Value *LHS, Value *RHS, const std::string &Name,
522 BasicBlock *InsertAtEnd);
525 /// This enumeration lists the possible predicates for CmpInst subclasses.
526 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
527 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
528 /// predicate values are not overlapping between the classes.
530 // Opcode U L G E Intuitive operation
531 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
532 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
533 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
534 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
535 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
536 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
537 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
538 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
539 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
540 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
541 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
542 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
543 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
544 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
545 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
546 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
547 FIRST_FCMP_PREDICATE = FCMP_FALSE,
548 LAST_FCMP_PREDICATE = FCMP_TRUE,
549 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
550 ICMP_EQ = 32, /// equal
551 ICMP_NE = 33, /// not equal
552 ICMP_UGT = 34, /// unsigned greater than
553 ICMP_UGE = 35, /// unsigned greater or equal
554 ICMP_ULT = 36, /// unsigned less than
555 ICMP_ULE = 37, /// unsigned less or equal
556 ICMP_SGT = 38, /// signed greater than
557 ICMP_SGE = 39, /// signed greater or equal
558 ICMP_SLT = 40, /// signed less than
559 ICMP_SLE = 41, /// signed less or equal
560 FIRST_ICMP_PREDICATE = ICMP_EQ,
561 LAST_ICMP_PREDICATE = ICMP_SLE,
562 BAD_ICMP_PREDICATE = ICMP_SLE + 1
565 // allocate space for exactly two operands
566 void *operator new(size_t s) {
567 return User::operator new(s, 2);
569 /// Construct a compare instruction, given the opcode, the predicate and
570 /// the two operands. Optionally (if InstBefore is specified) insert the
571 /// instruction into a BasicBlock right before the specified instruction.
572 /// The specified Instruction is allowed to be a dereferenced end iterator.
573 /// @brief Create a CmpInst
574 static CmpInst *Create(LLVMContext &Context, OtherOps Op,
575 unsigned short predicate, Value *S1,
576 Value *S2, const std::string &Name = "",
577 Instruction *InsertBefore = 0);
579 /// Construct a compare instruction, given the opcode, the predicate and the
580 /// two operands. Also automatically insert this instruction to the end of
581 /// the BasicBlock specified.
582 /// @brief Create a CmpInst
583 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
584 Value *S2, const std::string &Name,
585 BasicBlock *InsertAtEnd);
587 /// @brief Get the opcode casted to the right type
588 OtherOps getOpcode() const {
589 return static_cast<OtherOps>(Instruction::getOpcode());
592 /// @brief Return the predicate for this instruction.
593 Predicate getPredicate() const { return Predicate(SubclassData); }
595 /// @brief Set the predicate for this instruction to the specified value.
596 void setPredicate(Predicate P) { SubclassData = P; }
598 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
599 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
600 /// @returns the inverse predicate for the instruction's current predicate.
601 /// @brief Return the inverse of the instruction's predicate.
602 Predicate getInversePredicate() const {
603 return getInversePredicate(getPredicate());
606 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
607 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
608 /// @returns the inverse predicate for predicate provided in \p pred.
609 /// @brief Return the inverse of a given predicate
610 static Predicate getInversePredicate(Predicate pred);
612 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
613 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
614 /// @returns the predicate that would be the result of exchanging the two
615 /// operands of the CmpInst instruction without changing the result
617 /// @brief Return the predicate as if the operands were swapped
618 Predicate getSwappedPredicate() const {
619 return getSwappedPredicate(getPredicate());
622 /// This is a static version that you can use without an instruction
624 /// @brief Return the predicate as if the operands were swapped.
625 static Predicate getSwappedPredicate(Predicate pred);
627 /// @brief Provide more efficient getOperand methods.
628 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
630 /// This is just a convenience that dispatches to the subclasses.
631 /// @brief Swap the operands and adjust predicate accordingly to retain
632 /// the same comparison.
635 /// This is just a convenience that dispatches to the subclasses.
636 /// @brief Determine if this CmpInst is commutative.
637 bool isCommutative();
639 /// This is just a convenience that dispatches to the subclasses.
640 /// @brief Determine if this is an equals/not equals predicate.
643 /// @returns true if the predicate is unsigned, false otherwise.
644 /// @brief Determine if the predicate is an unsigned operation.
645 static bool isUnsigned(unsigned short predicate);
647 /// @returns true if the predicate is signed, false otherwise.
648 /// @brief Determine if the predicate is an signed operation.
649 static bool isSigned(unsigned short predicate);
651 /// @brief Determine if the predicate is an ordered operation.
652 static bool isOrdered(unsigned short predicate);
654 /// @brief Determine if the predicate is an unordered operation.
655 static bool isUnordered(unsigned short predicate);
657 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
658 static inline bool classof(const CmpInst *) { return true; }
659 static inline bool classof(const Instruction *I) {
660 return I->getOpcode() == Instruction::ICmp ||
661 I->getOpcode() == Instruction::FCmp;
663 static inline bool classof(const Value *V) {
664 return isa<Instruction>(V) && classof(cast<Instruction>(V));
669 // FIXME: these are redundant if CmpInst < BinaryOperator
671 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
674 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
676 } // End llvm namespace