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 = nullptr)
     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   TerminatorInst *clone_impl() const override = 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 = nullptr)
     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   BinaryOperator *clone_impl() const 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 = nullptr);
    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 = nullptr);
    289   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
    290                                    BasicBlock *InsertAtEnd);
    291   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
    292                                       Instruction *InsertBefore = nullptr);
    293   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
    294                                       BasicBlock *InsertAtEnd);
    295   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
    296                                       Instruction *InsertBefore = nullptr);
    297   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
    298                                       BasicBlock *InsertAtEnd);
    299   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
    300                                     Instruction *InsertBefore = nullptr);
    301   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
    302                                     BasicBlock *InsertAtEnd);
    303   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
    304                                    Instruction *InsertBefore = nullptr);
    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   void anchor() 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 = nullptr)
    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 = nullptr ///< 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 = nullptr ///< 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 = nullptr ///< 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 = nullptr ///< 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 = nullptr ///< 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 = nullptr ///< 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 = nullptr ///< 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   /// @brief Determine if this cast is a no-op cast.
    586   bool isNoopCast(
    587     const DataLayout *DL ///< DataLayout to get the Int Ptr type from.
    588   ) const;
    589 
    590   /// Determine how a pair of casts can be eliminated, if they can be at all.
    591   /// This is a helper function for both CastInst and ConstantExpr.
    592   /// @returns 0 if the CastInst pair can't be eliminated, otherwise
    593   /// returns Instruction::CastOps value for a cast that can replace
    594   /// the pair, casting SrcTy to DstTy.
    595   /// @brief Determine if a cast pair is eliminable
    596   static unsigned isEliminableCastPair(
    597     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
    598     Instruction::CastOps secondOpcode, ///< Opcode of second cast
    599     Type *SrcTy, ///< SrcTy of 1st cast
    600     Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
    601     Type *DstTy, ///< DstTy of 2nd cast
    602     Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
    603     Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
    604     Type *DstIntPtrTy  ///< Integer type corresponding to Ptr DstTy, or null
    605   );
    606 
    607   /// @brief Return the opcode of this CastInst
    608   Instruction::CastOps getOpcode() const {
    609     return Instruction::CastOps(Instruction::getOpcode());
    610   }
    611 
    612   /// @brief Return the source type, as a convenience
    613   Type* getSrcTy() const { return getOperand(0)->getType(); }
    614   /// @brief Return the destination type, as a convenience
    615   Type* getDestTy() const { return getType(); }
    616 
    617   /// This method can be used to determine if a cast from S to DstTy using
    618   /// Opcode op is valid or not.
    619   /// @returns true iff the proposed cast is valid.
    620   /// @brief Determine if a cast is valid without creating one.
    621   static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
    622 
    623   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
    624   static inline bool classof(const Instruction *I) {
    625     return I->isCast();
    626   }
    627   static inline bool classof(const Value *V) {
    628     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    629   }
    630 };
    631 
    632 //===----------------------------------------------------------------------===//
    633 //                               CmpInst Class
    634 //===----------------------------------------------------------------------===//
    635 
    636 /// This class is the base class for the comparison instructions.
    637 /// @brief Abstract base class of comparison instructions.
    638 class CmpInst : public Instruction {
    639   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
    640   CmpInst() LLVM_DELETED_FUNCTION;
    641 protected:
    642   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
    643           Value *LHS, Value *RHS, const Twine &Name = "",
    644           Instruction *InsertBefore = nullptr);
    645 
    646   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
    647           Value *LHS, Value *RHS, const Twine &Name,
    648           BasicBlock *InsertAtEnd);
    649 
    650   void anchor() override; // Out of line virtual method.
    651 public:
    652   /// This enumeration lists the possible predicates for CmpInst subclasses.
    653   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
    654   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
    655   /// predicate values are not overlapping between the classes.
    656   enum Predicate {
    657     // Opcode              U L G E    Intuitive operation
    658     FCMP_FALSE =  0,  ///< 0 0 0 0    Always false (always folded)
    659     FCMP_OEQ   =  1,  ///< 0 0 0 1    True if ordered and equal
    660     FCMP_OGT   =  2,  ///< 0 0 1 0    True if ordered and greater than
    661     FCMP_OGE   =  3,  ///< 0 0 1 1    True if ordered and greater than or equal
    662     FCMP_OLT   =  4,  ///< 0 1 0 0    True if ordered and less than
    663     FCMP_OLE   =  5,  ///< 0 1 0 1    True if ordered and less than or equal
    664     FCMP_ONE   =  6,  ///< 0 1 1 0    True if ordered and operands are unequal
    665     FCMP_ORD   =  7,  ///< 0 1 1 1    True if ordered (no nans)
    666     FCMP_UNO   =  8,  ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
    667     FCMP_UEQ   =  9,  ///< 1 0 0 1    True if unordered or equal
    668     FCMP_UGT   = 10,  ///< 1 0 1 0    True if unordered or greater than
    669     FCMP_UGE   = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
    670     FCMP_ULT   = 12,  ///< 1 1 0 0    True if unordered or less than
    671     FCMP_ULE   = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
    672     FCMP_UNE   = 14,  ///< 1 1 1 0    True if unordered or not equal
    673     FCMP_TRUE  = 15,  ///< 1 1 1 1    Always true (always folded)
    674     FIRST_FCMP_PREDICATE = FCMP_FALSE,
    675     LAST_FCMP_PREDICATE = FCMP_TRUE,
    676     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
    677     ICMP_EQ    = 32,  ///< equal
    678     ICMP_NE    = 33,  ///< not equal
    679     ICMP_UGT   = 34,  ///< unsigned greater than
    680     ICMP_UGE   = 35,  ///< unsigned greater or equal
    681     ICMP_ULT   = 36,  ///< unsigned less than
    682     ICMP_ULE   = 37,  ///< unsigned less or equal
    683     ICMP_SGT   = 38,  ///< signed greater than
    684     ICMP_SGE   = 39,  ///< signed greater or equal
    685     ICMP_SLT   = 40,  ///< signed less than
    686     ICMP_SLE   = 41,  ///< signed less or equal
    687     FIRST_ICMP_PREDICATE = ICMP_EQ,
    688     LAST_ICMP_PREDICATE = ICMP_SLE,
    689     BAD_ICMP_PREDICATE = ICMP_SLE + 1
    690   };
    691 
    692   // allocate space for exactly two operands
    693   void *operator new(size_t s) {
    694     return User::operator new(s, 2);
    695   }
    696   /// Construct a compare instruction, given the opcode, the predicate and
    697   /// the two operands.  Optionally (if InstBefore is specified) insert the
    698   /// instruction into a BasicBlock right before the specified instruction.
    699   /// The specified Instruction is allowed to be a dereferenced end iterator.
    700   /// @brief Create a CmpInst
    701   static CmpInst *Create(OtherOps Op,
    702                          unsigned short predicate, Value *S1,
    703                          Value *S2, const Twine &Name = "",
    704                          Instruction *InsertBefore = nullptr);
    705 
    706   /// Construct a compare instruction, given the opcode, the predicate and the
    707   /// two operands.  Also automatically insert this instruction to the end of
    708   /// the BasicBlock specified.
    709   /// @brief Create a CmpInst
    710   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
    711                          Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
    712 
    713   /// @brief Get the opcode casted to the right type
    714   OtherOps getOpcode() const {
    715     return static_cast<OtherOps>(Instruction::getOpcode());
    716   }
    717 
    718   /// @brief Return the predicate for this instruction.
    719   Predicate getPredicate() const {
    720     return Predicate(getSubclassDataFromInstruction());
    721   }
    722 
    723   /// @brief Set the predicate for this instruction to the specified value.
    724   void setPredicate(Predicate P) { setInstructionSubclassData(P); }
    725 
    726   static bool isFPPredicate(Predicate P) {
    727     return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
    728   }
    729 
    730   static bool isIntPredicate(Predicate P) {
    731     return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
    732   }
    733 
    734   bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
    735   bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
    736 
    737 
    738   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
    739   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
    740   /// @returns the inverse predicate for the instruction's current predicate.
    741   /// @brief Return the inverse of the instruction's predicate.
    742   Predicate getInversePredicate() const {
    743     return getInversePredicate(getPredicate());
    744   }
    745 
    746   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
    747   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
    748   /// @returns the inverse predicate for predicate provided in \p pred.
    749   /// @brief Return the inverse of a given predicate
    750   static Predicate getInversePredicate(Predicate pred);
    751 
    752   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
    753   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
    754   /// @returns the predicate that would be the result of exchanging the two
    755   /// operands of the CmpInst instruction without changing the result
    756   /// produced.
    757   /// @brief Return the predicate as if the operands were swapped
    758   Predicate getSwappedPredicate() const {
    759     return getSwappedPredicate(getPredicate());
    760   }
    761 
    762   /// This is a static version that you can use without an instruction
    763   /// available.
    764   /// @brief Return the predicate as if the operands were swapped.
    765   static Predicate getSwappedPredicate(Predicate pred);
    766 
    767   /// @brief Provide more efficient getOperand methods.
    768   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    769 
    770   /// This is just a convenience that dispatches to the subclasses.
    771   /// @brief Swap the operands and adjust predicate accordingly to retain
    772   /// the same comparison.
    773   void swapOperands();
    774 
    775   /// This is just a convenience that dispatches to the subclasses.
    776   /// @brief Determine if this CmpInst is commutative.
    777   bool isCommutative() const;
    778 
    779   /// This is just a convenience that dispatches to the subclasses.
    780   /// @brief Determine if this is an equals/not equals predicate.
    781   bool isEquality() const;
    782 
    783   /// @returns true if the comparison is signed, false otherwise.
    784   /// @brief Determine if this instruction is using a signed comparison.
    785   bool isSigned() const {
    786     return isSigned(getPredicate());
    787   }
    788 
    789   /// @returns true if the comparison is unsigned, false otherwise.
    790   /// @brief Determine if this instruction is using an unsigned comparison.
    791   bool isUnsigned() const {
    792     return isUnsigned(getPredicate());
    793   }
    794 
    795   /// This is just a convenience.
    796   /// @brief Determine if this is true when both operands are the same.
    797   bool isTrueWhenEqual() const {
    798     return isTrueWhenEqual(getPredicate());
    799   }
    800 
    801   /// This is just a convenience.
    802   /// @brief Determine if this is false when both operands are the same.
    803   bool isFalseWhenEqual() const {
    804     return isFalseWhenEqual(getPredicate());
    805   }
    806 
    807   /// @returns true if the predicate is unsigned, false otherwise.
    808   /// @brief Determine if the predicate is an unsigned operation.
    809   static bool isUnsigned(unsigned short predicate);
    810 
    811   /// @returns true if the predicate is signed, false otherwise.
    812   /// @brief Determine if the predicate is an signed operation.
    813   static bool isSigned(unsigned short predicate);
    814 
    815   /// @brief Determine if the predicate is an ordered operation.
    816   static bool isOrdered(unsigned short predicate);
    817 
    818   /// @brief Determine if the predicate is an unordered operation.
    819   static bool isUnordered(unsigned short predicate);
    820 
    821   /// Determine if the predicate is true when comparing a value with itself.
    822   static bool isTrueWhenEqual(unsigned short predicate);
    823 
    824   /// Determine if the predicate is false when comparing a value with itself.
    825   static bool isFalseWhenEqual(unsigned short predicate);
    826 
    827   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
    828   static inline bool classof(const Instruction *I) {
    829     return I->getOpcode() == Instruction::ICmp ||
    830            I->getOpcode() == Instruction::FCmp;
    831   }
    832   static inline bool classof(const Value *V) {
    833     return isa<Instruction>(V) && classof(cast<Instruction>(V));
    834   }
    835 
    836   /// @brief Create a result type for fcmp/icmp
    837   static Type* makeCmpResultType(Type* opnd_type) {
    838     if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
    839       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
    840                              vt->getNumElements());
    841     }
    842     return Type::getInt1Ty(opnd_type->getContext());
    843   }
    844 private:
    845   // Shadow Value::setValueSubclassData with a private forwarding method so that
    846   // subclasses cannot accidentally use it.
    847   void setValueSubclassData(unsigned short D) {
    848     Value::setValueSubclassData(D);
    849   }
    850 };
    851 
    852 
    853 // FIXME: these are redundant if CmpInst < BinaryOperator
    854 template <>
    855 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
    856 };
    857 
    858 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
    859 
    860 } // End llvm namespace
    861 
    862 #endif
    863