Home | History | Annotate | Download | only in IR
      1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     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
     12 // i*.h files...
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_IR_INSTRTYPES_H
     17 #define LLVM_IR_INSTRTYPES_H
     18 
     19 #include "llvm/ADT/Twine.h"
     20 #include "llvm/IR/DerivedTypes.h"
     21 #include "llvm/IR/Instruction.h"
     22 #include "llvm/IR/OperandTraits.h"
     23 
     24 namespace llvm {
     25 
     26 class LLVMContext;
     27 
     28 //===----------------------------------------------------------------------===//
     29 //                            TerminatorInst Class
     30 //===----------------------------------------------------------------------===//
     31 
     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.
     34 ///
     35 class TerminatorInst : public Instruction {
     36 protected:
     37   TerminatorInst(Type *Ty, Instruction::TermOps iType,
     38                  Use *Ops, unsigned NumOps,
     39                  Instruction *InsertBefore = 0)
     40     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
     41 
     42   TerminatorInst(Type *Ty, Instruction::TermOps iType,
     43                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
     44     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
     45 
     46   // Out of line virtual method, so the vtable, etc has a home.
     47   ~TerminatorInst();
     48 
     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;
     54   virtual TerminatorInst *clone_impl() const = 0;
     55 public:
     56 
     57   /// getNumSuccessors - Return the number of successors that this terminator
     58   /// has.
     59   unsigned getNumSuccessors() const {
     60     return getNumSuccessorsV();
     61   }
     62 
     63   /// getSuccessor - Return the specified successor.
     64   ///
     65   BasicBlock *getSuccessor(unsigned idx) const {
     66     return getSuccessorV(idx);
     67   }
     68 
     69   /// setSuccessor - Update the specified successor to point at the provided
     70   /// block.
     71   void setSuccessor(unsigned idx, BasicBlock *B) {
     72     setSuccessorV(idx, B);
     73   }
     74 
     75   // Methods for support type inquiry through isa, cast, and dyn_cast:
     76   static inline bool classof(const Instruction *I) {
     77     return I->isTerminator();
     78   }
     79   static inline bool classof(const Value *V) {
     80     return isa<Instruction>(V) && classof(cast<Instruction>(V));
     81   }
     82 };
     83 
     84 
     85 //===----------------------------------------------------------------------===//
     86 //                          UnaryInstruction Class
     87 //===----------------------------------------------------------------------===//
     88 
     89 class UnaryInstruction : public Instruction {
     90   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
     91 
     92 protected:
     93   UnaryInstruction(Type *Ty, unsigned iType, Value *V,
     94                    Instruction *IB = 0)
     95     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
     96     Op<0>() = V;
     97   }
     98   UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
     99     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
    100     Op<0>() = V;
    101   }
    102 public:
    103   // allocate space for exactly one operand
    104   void *operator new(size_t s) {
    105     return User::operator new(s, 1);
    106   }
    107 
    108   // Out of line virtual method, so the vtable, etc has a home.
    109   ~UnaryInstruction();
    110 
    111   /// Transparently provide more efficient getOperand methods.
    112   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    113 
    114   // Methods for support type inquiry through isa, cast, and dyn_cast:
    115   static inline bool classof(const Instruction *I) {
    116     return I->getOpcode() == Instruction::Alloca ||
    117            I->getOpcode() == Instruction::Load ||
    118            I->getOpcode() == Instruction::VAArg ||
    119            I->getOpcode() == Instruction::ExtractValue ||
    120            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
    121   }
    122   static inline bool classof(const Value *V) {
    123     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    124   }
    125 };
    126 
    127 template <>
    128 struct OperandTraits<UnaryInstruction> :
    129   public FixedNumOperandTraits<UnaryInstruction, 1> {
    130 };
    131 
    132 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
    133 
    134 //===----------------------------------------------------------------------===//
    135 //                           BinaryOperator Class
    136 //===----------------------------------------------------------------------===//
    137 
    138 class BinaryOperator : public Instruction {
    139   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
    140 protected:
    141   void init(BinaryOps iType);
    142   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
    143                  const Twine &Name, Instruction *InsertBefore);
    144   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
    145                  const Twine &Name, BasicBlock *InsertAtEnd);
    146   virtual BinaryOperator *clone_impl() const LLVM_OVERRIDE;
    147 public:
    148   // allocate space for exactly two operands
    149   void *operator new(size_t s) {
    150     return User::operator new(s, 2);
    151   }
    152 
    153   /// Transparently provide more efficient getOperand methods.
    154   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    155 
    156   /// Create() - Construct a binary instruction, given the opcode and the two
    157   /// operands.  Optionally (if InstBefore is specified) insert the instruction
    158   /// into a BasicBlock right before the specified instruction.  The specified
    159   /// Instruction is allowed to be a dereferenced end iterator.
    160   ///
    161   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
    162                                 const Twine &Name = Twine(),
    163                                 Instruction *InsertBefore = 0);
    164 
    165   /// Create() - Construct a binary instruction, given the opcode and the two
    166   /// operands.  Also automatically insert this instruction to the end of the
    167   /// BasicBlock specified.
    168   ///
    169   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
    170                                 const Twine &Name, BasicBlock *InsertAtEnd);
    171 
    172   /// Create* - These methods just forward to Create, and are useful when you
    173   /// statically know what type of instruction you're going to create.  These
    174   /// helpers just save some typing.
    175 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
    176   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
    177                                      const Twine &Name = "") {\
    178     return Create(Instruction::OPC, V1, V2, Name);\
    179   }
    180 #include "llvm/IR/Instruction.def"
    181 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
    182   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
    183                                      const Twine &Name, BasicBlock *BB) {\
    184     return Create(Instruction::OPC, V1, V2, Name, BB);\
    185   }
    186 #include "llvm/IR/Instruction.def"
    187 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
    188   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
    189                                      const Twine &Name, Instruction *I) {\
    190     return Create(Instruction::OPC, V1, V2, Name, I);\
    191   }
    192 #include "llvm/IR/Instruction.def"
    193 
    194   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
    195                                    const Twine &Name = "") {
    196     BinaryOperator *BO = Create(Opc, V1, V2, Name);
    197     BO->setHasNoSignedWrap(true);
    198     return BO;
    199   }
    200   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
    201                                    const Twine &Name, BasicBlock *BB) {
    202     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
    203     BO->setHasNoSignedWrap(true);
    204     return BO;
    205   }
    206   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
    207                                    const Twine &Name, Instruction *I) {
    208     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
    209     BO->setHasNoSignedWrap(true);
    210     return BO;
    211   }
    212 
    213   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
    214                                    const Twine &Name = "") {
    215     BinaryOperator *BO = Create(Opc, V1, V2, Name);
    216     BO->setHasNoUnsignedWrap(true);
    217     return BO;
    218   }
    219   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
    220                                    const Twine &Name, BasicBlock *BB) {
    221     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
    222     BO->setHasNoUnsignedWrap(true);
    223     return BO;
    224   }
    225   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
    226                                    const Twine &Name, Instruction *I) {
    227     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
    228     BO->setHasNoUnsignedWrap(true);
    229     return BO;
    230   }
    231 
    232   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
    233                                      const Twine &Name = "") {
    234     BinaryOperator *BO = Create(Opc, V1, V2, Name);
    235     BO->setIsExact(true);
    236     return BO;
    237   }
    238   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
    239                                      const Twine &Name, BasicBlock *BB) {
    240     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
    241     BO->setIsExact(true);
    242     return BO;
    243   }
    244   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
    245                                      const Twine &Name, Instruction *I) {
    246     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
    247     BO->setIsExact(true);
    248     return BO;
    249   }
    250 
    251 #define DEFINE_HELPERS(OPC, NUWNSWEXACT)                                     \
    252   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
    253            (Value *V1, Value *V2, const Twine &Name = "") {                  \
    254     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name);            \
    255   }                                                                          \
    256   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
    257            (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) {       \
    258     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB);        \
    259   }                                                                          \
    260   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
    261            (Value *V1, Value *V2, const Twine &Name, Instruction *I) {       \
    262     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I);         \
    263   }
    264 
    265   DEFINE_HELPERS(Add, NSW)  // CreateNSWAdd
    266   DEFINE_HELPERS(Add, NUW)  // CreateNUWAdd
    267   DEFINE_HELPERS(Sub, NSW)  // CreateNSWSub
    268   DEFINE_HELPERS(Sub, NUW)  // CreateNUWSub
    269   DEFINE_HELPERS(Mul, NSW)  // CreateNSWMul
    270   DEFINE_HELPERS(Mul, NUW)  // CreateNUWMul
    271   DEFINE_HELPERS(Shl, NSW)  // CreateNSWShl
    272   DEFINE_HELPERS(Shl, NUW)  // CreateNUWShl
    273 
    274   DEFINE_HELPERS(SDiv, Exact)  // CreateExactSDiv
    275   DEFINE_HELPERS(UDiv, Exact)  // CreateExactUDiv
    276   DEFINE_HELPERS(AShr, Exact)  // CreateExactAShr
    277   DEFINE_HELPERS(LShr, Exact)  // CreateExactLShr
    278 
    279 #undef DEFINE_HELPERS
    280 
    281   /// Helper functions to construct and inspect unary operations (NEG and NOT)
    282   /// via binary operators SUB and XOR:
    283   ///
    284   /// CreateNeg, CreateNot - Create the NEG and NOT
    285   ///     instructions out of SUB and XOR instructions.
    286   ///
    287   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
    288                                    Instruction *InsertBefore = 0);
    289   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
    290                                    BasicBlock *InsertAtEnd);
    291   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
    292                                       Instruction *InsertBefore = 0);
    293   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
    294                                       BasicBlock *InsertAtEnd);
    295   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
    296                                       Instruction *InsertBefore = 0);
    297   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
    298                                       BasicBlock *InsertAtEnd);
    299   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
    300                                     Instruction *InsertBefore = 0);
    301   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
    302                                     BasicBlock *InsertAtEnd);
    303   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
    304                                    Instruction *InsertBefore = 0);
    305   static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
    306                                    BasicBlock *InsertAtEnd);
    307 
    308   /// isNeg, isFNeg, isNot - Check if the given Value is a
    309   /// NEG, FNeg, or NOT instruction.
    310   ///
    311   static bool isNeg(const Value *V);
    312   static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
    313   static bool isNot(const Value *V);
    314 
    315   /// getNegArgument, getNotArgument - Helper functions to extract the
    316   ///     unary argument of a NEG, FNEG or NOT operation implemented via
    317   ///     Sub, FSub, or Xor.
    318   ///
    319   static const Value *getNegArgument(const Value *BinOp);
    320   static       Value *getNegArgument(      Value *BinOp);
    321   static const Value *getFNegArgument(const Value *BinOp);
    322   static       Value *getFNegArgument(      Value *BinOp);
    323   static const Value *getNotArgument(const Value *BinOp);
    324   static       Value *getNotArgument(      Value *BinOp);
    325 
    326   BinaryOps getOpcode() const {
    327     return static_cast<BinaryOps>(Instruction::getOpcode());
    328   }
    329 
    330   /// swapOperands - Exchange the two operands to this instruction.
    331   /// This instruction is safe to use on any binary instruction and
    332   /// does not modify the semantics of the instruction.  If the instruction
    333   /// cannot be reversed (ie, it's a Div), then return true.
    334   ///
    335   bool swapOperands();
    336 
    337   /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
    338   /// which must be an operator which supports this flag. See LangRef.html
    339   /// for the meaning of this flag.
    340   void setHasNoUnsignedWrap(bool b = true);
    341 
    342   /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
    343   /// which must be an operator which supports this flag. See LangRef.html
    344   /// for the meaning of this flag.
    345   void setHasNoSignedWrap(bool b = true);
    346 
    347   /// setIsExact - Set or clear the exact flag on this instruction,
    348   /// which must be an operator which supports this flag. See LangRef.html
    349   /// for the meaning of this flag.
    350   void setIsExact(bool b = true);
    351 
    352   /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
    353   bool hasNoUnsignedWrap() const;
    354 
    355   /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
    356   bool hasNoSignedWrap() const;
    357 
    358   /// isExact - Determine whether the exact flag is set.
    359   bool isExact() const;
    360 
    361   // Methods for support type inquiry through isa, cast, and dyn_cast:
    362   static inline bool classof(const Instruction *I) {
    363     return I->isBinaryOp();
    364   }
    365   static inline bool classof(const Value *V) {
    366     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    367   }
    368 };
    369 
    370 template <>
    371 struct OperandTraits<BinaryOperator> :
    372   public FixedNumOperandTraits<BinaryOperator, 2> {
    373 };
    374 
    375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
    376 
    377 //===----------------------------------------------------------------------===//
    378 //                               CastInst Class
    379 //===----------------------------------------------------------------------===//
    380 
    381 /// CastInst - This is the base class for all instructions that perform data
    382 /// casts. It is simply provided so that instruction category testing
    383 /// can be performed with code like:
    384 ///
    385 /// if (isa<CastInst>(Instr)) { ... }
    386 /// @brief Base class of casting instructions.
    387 class CastInst : public UnaryInstruction {
    388   virtual void anchor() LLVM_OVERRIDE;
    389 protected:
    390   /// @brief Constructor with insert-before-instruction semantics for subclasses
    391   CastInst(Type *Ty, unsigned iType, Value *S,
    392            const Twine &NameStr = "", Instruction *InsertBefore = 0)
    393     : UnaryInstruction(Ty, iType, S, InsertBefore) {
    394     setName(NameStr);
    395   }
    396   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
    397   CastInst(Type *Ty, unsigned iType, Value *S,
    398            const Twine &NameStr, BasicBlock *InsertAtEnd)
    399     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
    400     setName(NameStr);
    401   }
    402 public:
    403   /// Provides a way to construct any of the CastInst subclasses using an
    404   /// opcode instead of the subclass's constructor. The opcode must be in the
    405   /// CastOps category (Instruction::isCast(opcode) returns true). This
    406   /// constructor has insert-before-instruction semantics to automatically
    407   /// insert the new CastInst before InsertBefore (if it is non-null).
    408   /// @brief Construct any of the CastInst subclasses
    409   static CastInst *Create(
    410     Instruction::CastOps,    ///< The opcode of the cast instruction
    411     Value *S,                ///< The value to be casted (operand 0)
    412     Type *Ty,          ///< The type to which cast should be made
    413     const Twine &Name = "", ///< Name for the instruction
    414     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    415   );
    416   /// Provides a way to construct any of the CastInst subclasses using an
    417   /// opcode instead of the subclass's constructor. The opcode must be in the
    418   /// CastOps category. This constructor has insert-at-end-of-block semantics
    419   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
    420   /// its non-null).
    421   /// @brief Construct any of the CastInst subclasses
    422   static CastInst *Create(
    423     Instruction::CastOps,    ///< The opcode for the cast instruction
    424     Value *S,                ///< The value to be casted (operand 0)
    425     Type *Ty,          ///< The type to which operand is casted
    426     const Twine &Name, ///< The name for the instruction
    427     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    428   );
    429 
    430   /// @brief Create a ZExt or BitCast cast instruction
    431   static CastInst *CreateZExtOrBitCast(
    432     Value *S,                ///< The value to be casted (operand 0)
    433     Type *Ty,          ///< The type to which cast should be made
    434     const Twine &Name = "", ///< Name for the instruction
    435     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    436   );
    437 
    438   /// @brief Create a ZExt or BitCast cast instruction
    439   static CastInst *CreateZExtOrBitCast(
    440     Value *S,                ///< The value to be casted (operand 0)
    441     Type *Ty,          ///< The type to which operand is casted
    442     const Twine &Name, ///< The name for the instruction
    443     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    444   );
    445 
    446   /// @brief Create a SExt or BitCast cast instruction
    447   static CastInst *CreateSExtOrBitCast(
    448     Value *S,                ///< The value to be casted (operand 0)
    449     Type *Ty,          ///< The type to which cast should be made
    450     const Twine &Name = "", ///< Name for the instruction
    451     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    452   );
    453 
    454   /// @brief Create a SExt or BitCast cast instruction
    455   static CastInst *CreateSExtOrBitCast(
    456     Value *S,                ///< The value to be casted (operand 0)
    457     Type *Ty,          ///< The type to which operand is casted
    458     const Twine &Name, ///< The name for the instruction
    459     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    460   );
    461 
    462   /// @brief Create a BitCast or a PtrToInt cast instruction
    463   static CastInst *CreatePointerCast(
    464     Value *S,                ///< The pointer value to be casted (operand 0)
    465     Type *Ty,          ///< The type to which operand is casted
    466     const Twine &Name, ///< The name for the instruction
    467     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    468   );
    469 
    470   /// @brief Create a BitCast or a PtrToInt cast instruction
    471   static CastInst *CreatePointerCast(
    472     Value *S,                ///< The pointer value to be casted (operand 0)
    473     Type *Ty,          ///< The type to which cast should be made
    474     const Twine &Name = "", ///< Name for the instruction
    475     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    476   );
    477 
    478   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
    479   static CastInst *CreateIntegerCast(
    480     Value *S,                ///< The pointer value to be casted (operand 0)
    481     Type *Ty,          ///< The type to which cast should be made
    482     bool isSigned,           ///< Whether to regard S as signed or not
    483     const Twine &Name = "", ///< Name for the instruction
    484     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    485   );
    486 
    487   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
    488   static CastInst *CreateIntegerCast(
    489     Value *S,                ///< The integer value to be casted (operand 0)
    490     Type *Ty,          ///< The integer type to which operand is casted
    491     bool isSigned,           ///< Whether to regard S as signed or not
    492     const Twine &Name, ///< The name for the instruction
    493     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    494   );
    495 
    496   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
    497   static CastInst *CreateFPCast(
    498     Value *S,                ///< The floating point value to be casted
    499     Type *Ty,          ///< The floating point type to cast to
    500     const Twine &Name = "", ///< Name for the instruction
    501     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    502   );
    503 
    504   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
    505   static CastInst *CreateFPCast(
    506     Value *S,                ///< The floating point value to be casted
    507     Type *Ty,          ///< The floating point type to cast to
    508     const Twine &Name, ///< The name for the instruction
    509     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    510   );
    511 
    512   /// @brief Create a Trunc or BitCast cast instruction
    513   static CastInst *CreateTruncOrBitCast(
    514     Value *S,                ///< The value to be casted (operand 0)
    515     Type *Ty,          ///< The type to which cast should be made
    516     const Twine &Name = "", ///< Name for the instruction
    517     Instruction *InsertBefore = 0 ///< Place to insert the instruction
    518   );
    519 
    520   /// @brief Create a Trunc or BitCast cast instruction
    521   static CastInst *CreateTruncOrBitCast(
    522     Value *S,                ///< The value to be casted (operand 0)
    523     Type *Ty,          ///< The type to which operand is casted
    524     const Twine &Name, ///< The name for the instruction
    525     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
    526   );
    527 
    528   /// @brief Check whether it is valid to call getCastOpcode for these types.
    529   static bool isCastable(
    530     Type *SrcTy, ///< The Type from which the value should be cast.
    531     Type *DestTy ///< The Type to which the value should be cast.
    532   );
    533 
    534   /// @brief Check whether a bitcast between these types is valid
    535   static bool isBitCastable(
    536     Type *SrcTy, ///< The Type from which the value should be cast.
    537     Type *DestTy ///< The Type to which the value should be cast.
    538   );
    539 
    540   /// Returns the opcode necessary to cast Val into Ty using usual casting
    541   /// rules.
    542   /// @brief Infer the opcode for cast operand and type
    543   static Instruction::CastOps getCastOpcode(
    544     const Value *Val, ///< The value to cast
    545     bool SrcIsSigned, ///< Whether to treat the source as signed
    546     Type *Ty,   ///< The Type to which the value should be casted
    547     bool DstIsSigned  ///< Whether to treate the dest. as signed
    548   );
    549 
    550   /// There are several places where we need to know if a cast instruction
    551   /// only deals with integer source and destination types. To simplify that
    552   /// logic, this method is provided.
    553   /// @returns true iff the cast has only integral typed operand and dest type.
    554   /// @brief Determine if this is an integer-only cast.
    555   bool isIntegerCast() const;
    556 
    557   /// A lossless cast is one that does not alter the basic value. It implies
    558   /// a no-op cast but is more stringent, preventing things like int->float,
    559   /// long->double, or int->ptr.
    560   /// @returns true iff the cast is lossless.
    561   /// @brief Determine if this is a lossless cast.
    562   bool isLosslessCast() const;
    563 
    564   /// A no-op cast is one that can be effected without changing any bits.
    565   /// It implies that the source and destination types are the same size. The
    566   /// IntPtrTy argument is used to make accurate determinations for casts
    567   /// involving Integer and Pointer types. They are no-op casts if the integer
    568   /// is the same size as the pointer. However, pointer size varies with
    569   /// platform. Generally, the result of DataLayout::getIntPtrType() should be
    570   /// passed in. If that's not available, use Type::Int64Ty, which will make
    571   /// the isNoopCast call conservative.
    572   /// @brief Determine if the described cast is a no-op cast.
    573   static bool isNoopCast(
    574     Instruction::CastOps Opcode,  ///< Opcode of cast
    575     Type *SrcTy,   ///< SrcTy of cast
    576     Type *DstTy,   ///< DstTy of cast
    577     Type *IntPtrTy ///< Integer type corresponding to Ptr types
    578   );
    579 
    580   /// @brief Determine if this cast is a no-op cast.
    581   bool isNoopCast(
    582     Type *IntPtrTy ///< Integer type corresponding to pointer
    583   ) const;
    584 
    585   /// Determine how a pair of casts can be eliminated, if they can be at all.
    586   /// This is a helper function for both CastInst and ConstantExpr.
    587   /// @returns 0 if the CastInst pair can't be eliminated, otherwise
    588   /// returns Instruction::CastOps value for a cast that can replace
    589   /// the pair, casting SrcTy to DstTy.
    590   /// @brief Determine if a cast pair is eliminable
    591   static unsigned isEliminableCastPair(
    592     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
    593     Instruction::CastOps secondOpcode, ///< Opcode of second cast
    594     Type *SrcTy, ///< SrcTy of 1st cast
    595     Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
    596     Type *DstTy, ///< DstTy of 2nd cast
    597     Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
    598     Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
    599     Type *DstIntPtrTy  ///< Integer type corresponding to Ptr DstTy, or null
    600   );
    601 
    602   /// @brief Return the opcode of this CastInst
    603   Instruction::CastOps getOpcode() const {
    604     return Instruction::CastOps(Instruction::getOpcode());
    605   }
    606 
    607   /// @brief Return the source type, as a convenience
    608   Type* getSrcTy() const { return getOperand(0)->getType(); }
    609   /// @brief Return the destination type, as a convenience
    610   Type* getDestTy() const { return getType(); }
    611 
    612   /// This method can be used to determine if a cast from S to DstTy using
    613   /// Opcode op is valid or not.
    614   /// @returns true iff the proposed cast is valid.
    615   /// @brief Determine if a cast is valid without creating one.
    616   static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
    617 
    618   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
    619   static inline bool classof(const Instruction *I) {
    620     return I->isCast();
    621   }
    622   static inline bool classof(const Value *V) {
    623     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    624   }
    625 };
    626 
    627 //===----------------------------------------------------------------------===//
    628 //                               CmpInst Class
    629 //===----------------------------------------------------------------------===//
    630 
    631 /// This class is the base class for the comparison instructions.
    632 /// @brief Abstract base class of comparison instructions.
    633 class CmpInst : public Instruction {
    634   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
    635   CmpInst() LLVM_DELETED_FUNCTION;
    636 protected:
    637   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
    638           Value *LHS, Value *RHS, const Twine &Name = "",
    639           Instruction *InsertBefore = 0);
    640 
    641   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
    642           Value *LHS, Value *RHS, const Twine &Name,
    643           BasicBlock *InsertAtEnd);
    644 
    645   virtual void anchor() LLVM_OVERRIDE; // Out of line virtual method.
    646 public:
    647   /// This enumeration lists the possible predicates for CmpInst subclasses.
    648   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
    649   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
    650   /// predicate values are not overlapping between the classes.
    651   enum Predicate {
    652     // Opcode              U L G E    Intuitive operation
    653     FCMP_FALSE =  0,  ///< 0 0 0 0    Always false (always folded)
    654     FCMP_OEQ   =  1,  ///< 0 0 0 1    True if ordered and equal
    655     FCMP_OGT   =  2,  ///< 0 0 1 0    True if ordered and greater than
    656     FCMP_OGE   =  3,  ///< 0 0 1 1    True if ordered and greater than or equal
    657     FCMP_OLT   =  4,  ///< 0 1 0 0    True if ordered and less than
    658     FCMP_OLE   =  5,  ///< 0 1 0 1    True if ordered and less than or equal
    659     FCMP_ONE   =  6,  ///< 0 1 1 0    True if ordered and operands are unequal
    660     FCMP_ORD   =  7,  ///< 0 1 1 1    True if ordered (no nans)
    661     FCMP_UNO   =  8,  ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
    662     FCMP_UEQ   =  9,  ///< 1 0 0 1    True if unordered or equal
    663     FCMP_UGT   = 10,  ///< 1 0 1 0    True if unordered or greater than
    664     FCMP_UGE   = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
    665     FCMP_ULT   = 12,  ///< 1 1 0 0    True if unordered or less than
    666     FCMP_ULE   = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
    667     FCMP_UNE   = 14,  ///< 1 1 1 0    True if unordered or not equal
    668     FCMP_TRUE  = 15,  ///< 1 1 1 1    Always true (always folded)
    669     FIRST_FCMP_PREDICATE = FCMP_FALSE,
    670     LAST_FCMP_PREDICATE = FCMP_TRUE,
    671     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
    672     ICMP_EQ    = 32,  ///< equal
    673     ICMP_NE    = 33,  ///< not equal
    674     ICMP_UGT   = 34,  ///< unsigned greater than
    675     ICMP_UGE   = 35,  ///< unsigned greater or equal
    676     ICMP_ULT   = 36,  ///< unsigned less than
    677     ICMP_ULE   = 37,  ///< unsigned less or equal
    678     ICMP_SGT   = 38,  ///< signed greater than
    679     ICMP_SGE   = 39,  ///< signed greater or equal
    680     ICMP_SLT   = 40,  ///< signed less than
    681     ICMP_SLE   = 41,  ///< signed less or equal
    682     FIRST_ICMP_PREDICATE = ICMP_EQ,
    683     LAST_ICMP_PREDICATE = ICMP_SLE,
    684     BAD_ICMP_PREDICATE = ICMP_SLE + 1
    685   };
    686 
    687   // allocate space for exactly two operands
    688   void *operator new(size_t s) {
    689     return User::operator new(s, 2);
    690   }
    691   /// Construct a compare instruction, given the opcode, the predicate and
    692   /// the two operands.  Optionally (if InstBefore is specified) insert the
    693   /// instruction into a BasicBlock right before the specified instruction.
    694   /// The specified Instruction is allowed to be a dereferenced end iterator.
    695   /// @brief Create a CmpInst
    696   static CmpInst *Create(OtherOps Op,
    697                          unsigned short predicate, Value *S1,
    698                          Value *S2, const Twine &Name = "",
    699                          Instruction *InsertBefore = 0);
    700 
    701   /// Construct a compare instruction, given the opcode, the predicate and the
    702   /// two operands.  Also automatically insert this instruction to the end of
    703   /// the BasicBlock specified.
    704   /// @brief Create a CmpInst
    705   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
    706                          Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
    707 
    708   /// @brief Get the opcode casted to the right type
    709   OtherOps getOpcode() const {
    710     return static_cast<OtherOps>(Instruction::getOpcode());
    711   }
    712 
    713   /// @brief Return the predicate for this instruction.
    714   Predicate getPredicate() const {
    715     return Predicate(getSubclassDataFromInstruction());
    716   }
    717 
    718   /// @brief Set the predicate for this instruction to the specified value.
    719   void setPredicate(Predicate P) { setInstructionSubclassData(P); }
    720 
    721   static bool isFPPredicate(Predicate P) {
    722     return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
    723   }
    724 
    725   static bool isIntPredicate(Predicate P) {
    726     return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
    727   }
    728 
    729   bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
    730   bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
    731 
    732 
    733   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
    734   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
    735   /// @returns the inverse predicate for the instruction's current predicate.
    736   /// @brief Return the inverse of the instruction's predicate.
    737   Predicate getInversePredicate() const {
    738     return getInversePredicate(getPredicate());
    739   }
    740 
    741   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
    742   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
    743   /// @returns the inverse predicate for predicate provided in \p pred.
    744   /// @brief Return the inverse of a given predicate
    745   static Predicate getInversePredicate(Predicate pred);
    746 
    747   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
    748   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
    749   /// @returns the predicate that would be the result of exchanging the two
    750   /// operands of the CmpInst instruction without changing the result
    751   /// produced.
    752   /// @brief Return the predicate as if the operands were swapped
    753   Predicate getSwappedPredicate() const {
    754     return getSwappedPredicate(getPredicate());
    755   }
    756 
    757   /// This is a static version that you can use without an instruction
    758   /// available.
    759   /// @brief Return the predicate as if the operands were swapped.
    760   static Predicate getSwappedPredicate(Predicate pred);
    761 
    762   /// @brief Provide more efficient getOperand methods.
    763   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    764 
    765   /// This is just a convenience that dispatches to the subclasses.
    766   /// @brief Swap the operands and adjust predicate accordingly to retain
    767   /// the same comparison.
    768   void swapOperands();
    769 
    770   /// This is just a convenience that dispatches to the subclasses.
    771   /// @brief Determine if this CmpInst is commutative.
    772   bool isCommutative() const;
    773 
    774   /// This is just a convenience that dispatches to the subclasses.
    775   /// @brief Determine if this is an equals/not equals predicate.
    776   bool isEquality() const;
    777 
    778   /// @returns true if the comparison is signed, false otherwise.
    779   /// @brief Determine if this instruction is using a signed comparison.
    780   bool isSigned() const {
    781     return isSigned(getPredicate());
    782   }
    783 
    784   /// @returns true if the comparison is unsigned, false otherwise.
    785   /// @brief Determine if this instruction is using an unsigned comparison.
    786   bool isUnsigned() const {
    787     return isUnsigned(getPredicate());
    788   }
    789 
    790   /// This is just a convenience.
    791   /// @brief Determine if this is true when both operands are the same.
    792   bool isTrueWhenEqual() const {
    793     return isTrueWhenEqual(getPredicate());
    794   }
    795 
    796   /// This is just a convenience.
    797   /// @brief Determine if this is false when both operands are the same.
    798   bool isFalseWhenEqual() const {
    799     return isFalseWhenEqual(getPredicate());
    800   }
    801 
    802   /// @returns true if the predicate is unsigned, false otherwise.
    803   /// @brief Determine if the predicate is an unsigned operation.
    804   static bool isUnsigned(unsigned short predicate);
    805 
    806   /// @returns true if the predicate is signed, false otherwise.
    807   /// @brief Determine if the predicate is an signed operation.
    808   static bool isSigned(unsigned short predicate);
    809 
    810   /// @brief Determine if the predicate is an ordered operation.
    811   static bool isOrdered(unsigned short predicate);
    812 
    813   /// @brief Determine if the predicate is an unordered operation.
    814   static bool isUnordered(unsigned short predicate);
    815 
    816   /// Determine if the predicate is true when comparing a value with itself.
    817   static bool isTrueWhenEqual(unsigned short predicate);
    818 
    819   /// Determine if the predicate is false when comparing a value with itself.
    820   static bool isFalseWhenEqual(unsigned short predicate);
    821 
    822   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
    823   static inline bool classof(const Instruction *I) {
    824     return I->getOpcode() == Instruction::ICmp ||
    825            I->getOpcode() == Instruction::FCmp;
    826   }
    827   static inline bool classof(const Value *V) {
    828     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    829   }
    830 
    831   /// @brief Create a result type for fcmp/icmp
    832   static Type* makeCmpResultType(Type* opnd_type) {
    833     if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
    834       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
    835                              vt->getNumElements());
    836     }
    837     return Type::getInt1Ty(opnd_type->getContext());
    838   }
    839 private:
    840   // Shadow Value::setValueSubclassData with a private forwarding method so that
    841   // subclasses cannot accidentally use it.
    842   void setValueSubclassData(unsigned short D) {
    843     Value::setValueSubclassData(D);
    844   }
    845 };
    846 
    847 
    848 // FIXME: these are redundant if CmpInst < BinaryOperator
    849 template <>
    850 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
    851 };
    852 
    853 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
    854 
    855 } // End llvm namespace
    856 
    857 #endif
    858