LLVM API Documentation

InstrTypes.h
Go to the documentation of this file.
00001 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines various meta classes of instructions that exist in the VM
00011 // representation.  Specific concrete subclasses of these may be found in the
00012 // i*.h files...
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_IR_INSTRTYPES_H
00017 #define LLVM_IR_INSTRTYPES_H
00018 
00019 #include "llvm/ADT/Twine.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/Instruction.h"
00022 #include "llvm/IR/OperandTraits.h"
00023 
00024 namespace llvm {
00025 
00026 class LLVMContext;
00027 
00028 //===----------------------------------------------------------------------===//
00029 //                            TerminatorInst Class
00030 //===----------------------------------------------------------------------===//
00031 
00032 /// Subclasses of this class are all able to terminate a basic
00033 /// block. Thus, these are all the flow control type of operations.
00034 ///
00035 class TerminatorInst : public Instruction {
00036 protected:
00037   TerminatorInst(Type *Ty, Instruction::TermOps iType,
00038                  Use *Ops, unsigned NumOps,
00039                  Instruction *InsertBefore = nullptr)
00040     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
00041 
00042   TerminatorInst(Type *Ty, Instruction::TermOps iType,
00043                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
00044     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
00045 
00046   // Out of line virtual method, so the vtable, etc has a home.
00047   ~TerminatorInst();
00048 
00049   /// Virtual methods - Terminators should overload these and provide inline
00050   /// overrides of non-V methods.
00051   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
00052   virtual unsigned getNumSuccessorsV() const = 0;
00053   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
00054   TerminatorInst *clone_impl() const override = 0;
00055 public:
00056 
00057   /// Return the number of successors that this terminator has.
00058   unsigned getNumSuccessors() const {
00059     return getNumSuccessorsV();
00060   }
00061 
00062   /// Return the specified successor.
00063   BasicBlock *getSuccessor(unsigned idx) const {
00064     return getSuccessorV(idx);
00065   }
00066 
00067   /// Update the specified successor to point at the provided block.
00068   void setSuccessor(unsigned idx, BasicBlock *B) {
00069     setSuccessorV(idx, B);
00070   }
00071 
00072   // Methods for support type inquiry through isa, cast, and dyn_cast:
00073   static inline bool classof(const Instruction *I) {
00074     return I->isTerminator();
00075   }
00076   static inline bool classof(const Value *V) {
00077     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00078   }
00079 };
00080 
00081 
00082 //===----------------------------------------------------------------------===//
00083 //                          UnaryInstruction Class
00084 //===----------------------------------------------------------------------===//
00085 
00086 class UnaryInstruction : public Instruction {
00087   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00088 
00089 protected:
00090   UnaryInstruction(Type *Ty, unsigned iType, Value *V,
00091                    Instruction *IB = nullptr)
00092     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
00093     Op<0>() = V;
00094   }
00095   UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
00096     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
00097     Op<0>() = V;
00098   }
00099 public:
00100   // allocate space for exactly one operand
00101   void *operator new(size_t s) {
00102     return User::operator new(s, 1);
00103   }
00104 
00105   // Out of line virtual method, so the vtable, etc has a home.
00106   ~UnaryInstruction();
00107 
00108   /// Transparently provide more efficient getOperand methods.
00109   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00110 
00111   // Methods for support type inquiry through isa, cast, and dyn_cast:
00112   static inline bool classof(const Instruction *I) {
00113     return I->getOpcode() == Instruction::Alloca ||
00114            I->getOpcode() == Instruction::Load ||
00115            I->getOpcode() == Instruction::VAArg ||
00116            I->getOpcode() == Instruction::ExtractValue ||
00117            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
00118   }
00119   static inline bool classof(const Value *V) {
00120     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00121   }
00122 };
00123 
00124 template <>
00125 struct OperandTraits<UnaryInstruction> :
00126   public FixedNumOperandTraits<UnaryInstruction, 1> {
00127 };
00128 
00129 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
00130 
00131 //===----------------------------------------------------------------------===//
00132 //                           BinaryOperator Class
00133 //===----------------------------------------------------------------------===//
00134 
00135 class BinaryOperator : public Instruction {
00136   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00137 protected:
00138   void init(BinaryOps iType);
00139   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
00140                  const Twine &Name, Instruction *InsertBefore);
00141   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
00142                  const Twine &Name, BasicBlock *InsertAtEnd);
00143   BinaryOperator *clone_impl() const override;
00144 public:
00145   // allocate space for exactly two operands
00146   void *operator new(size_t s) {
00147     return User::operator new(s, 2);
00148   }
00149 
00150   /// Transparently provide more efficient getOperand methods.
00151   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00152 
00153   /// Construct a binary instruction, given the opcode and the two
00154   /// operands.  Optionally (if InstBefore is specified) insert the instruction
00155   /// into a BasicBlock right before the specified instruction.  The specified
00156   /// Instruction is allowed to be a dereferenced end iterator.
00157   ///
00158   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
00159                                 const Twine &Name = Twine(),
00160                                 Instruction *InsertBefore = nullptr);
00161 
00162   /// Construct a binary instruction, given the opcode and the two
00163   /// operands.  Also automatically insert this instruction to the end of the
00164   /// BasicBlock specified.
00165   ///
00166   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
00167                                 const Twine &Name, BasicBlock *InsertAtEnd);
00168 
00169   /// These methods just forward to Create, and are useful when you
00170   /// statically know what type of instruction you're going to create.  These
00171   /// helpers just save some typing.
00172 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00173   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00174                                      const Twine &Name = "") {\
00175     return Create(Instruction::OPC, V1, V2, Name);\
00176   }
00177 #include "llvm/IR/Instruction.def"
00178 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00179   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00180                                      const Twine &Name, BasicBlock *BB) {\
00181     return Create(Instruction::OPC, V1, V2, Name, BB);\
00182   }
00183 #include "llvm/IR/Instruction.def"
00184 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00185   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00186                                      const Twine &Name, Instruction *I) {\
00187     return Create(Instruction::OPC, V1, V2, Name, I);\
00188   }
00189 #include "llvm/IR/Instruction.def"
00190 
00191   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
00192                                    const Twine &Name = "") {
00193     BinaryOperator *BO = Create(Opc, V1, V2, Name);
00194     BO->setHasNoSignedWrap(true);
00195     return BO;
00196   }
00197   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
00198                                    const Twine &Name, BasicBlock *BB) {
00199     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
00200     BO->setHasNoSignedWrap(true);
00201     return BO;
00202   }
00203   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
00204                                    const Twine &Name, Instruction *I) {
00205     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
00206     BO->setHasNoSignedWrap(true);
00207     return BO;
00208   }
00209   
00210   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
00211                                    const Twine &Name = "") {
00212     BinaryOperator *BO = Create(Opc, V1, V2, Name);
00213     BO->setHasNoUnsignedWrap(true);
00214     return BO;
00215   }
00216   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
00217                                    const Twine &Name, BasicBlock *BB) {
00218     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
00219     BO->setHasNoUnsignedWrap(true);
00220     return BO;
00221   }
00222   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
00223                                    const Twine &Name, Instruction *I) {
00224     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
00225     BO->setHasNoUnsignedWrap(true);
00226     return BO;
00227   }
00228   
00229   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
00230                                      const Twine &Name = "") {
00231     BinaryOperator *BO = Create(Opc, V1, V2, Name);
00232     BO->setIsExact(true);
00233     return BO;
00234   }
00235   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
00236                                      const Twine &Name, BasicBlock *BB) {
00237     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
00238     BO->setIsExact(true);
00239     return BO;
00240   }
00241   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
00242                                      const Twine &Name, Instruction *I) {
00243     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
00244     BO->setIsExact(true);
00245     return BO;
00246   }
00247   
00248 #define DEFINE_HELPERS(OPC, NUWNSWEXACT)                                     \
00249   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
00250            (Value *V1, Value *V2, const Twine &Name = "") {                  \
00251     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name);            \
00252   }                                                                          \
00253   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
00254            (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) {       \
00255     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB);        \
00256   }                                                                          \
00257   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
00258            (Value *V1, Value *V2, const Twine &Name, Instruction *I) {       \
00259     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I);         \
00260   }
00261   
00262   DEFINE_HELPERS(Add, NSW)  // CreateNSWAdd
00263   DEFINE_HELPERS(Add, NUW)  // CreateNUWAdd
00264   DEFINE_HELPERS(Sub, NSW)  // CreateNSWSub
00265   DEFINE_HELPERS(Sub, NUW)  // CreateNUWSub
00266   DEFINE_HELPERS(Mul, NSW)  // CreateNSWMul
00267   DEFINE_HELPERS(Mul, NUW)  // CreateNUWMul
00268   DEFINE_HELPERS(Shl, NSW)  // CreateNSWShl
00269   DEFINE_HELPERS(Shl, NUW)  // CreateNUWShl
00270 
00271   DEFINE_HELPERS(SDiv, Exact)  // CreateExactSDiv
00272   DEFINE_HELPERS(UDiv, Exact)  // CreateExactUDiv
00273   DEFINE_HELPERS(AShr, Exact)  // CreateExactAShr
00274   DEFINE_HELPERS(LShr, Exact)  // CreateExactLShr
00275 
00276 #undef DEFINE_HELPERS
00277   
00278   /// Helper functions to construct and inspect unary operations (NEG and NOT)
00279   /// via binary operators SUB and XOR:
00280   ///
00281   /// Create the NEG and NOT instructions out of SUB and XOR instructions.
00282   ///
00283   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
00284                                    Instruction *InsertBefore = nullptr);
00285   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
00286                                    BasicBlock *InsertAtEnd);
00287   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
00288                                       Instruction *InsertBefore = nullptr);
00289   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
00290                                       BasicBlock *InsertAtEnd);
00291   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
00292                                       Instruction *InsertBefore = nullptr);
00293   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
00294                                       BasicBlock *InsertAtEnd);
00295   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
00296                                     Instruction *InsertBefore = nullptr);
00297   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
00298                                     BasicBlock *InsertAtEnd);
00299   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
00300                                    Instruction *InsertBefore = nullptr);
00301   static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
00302                                    BasicBlock *InsertAtEnd);
00303 
00304   /// Check if the given Value is a NEG, FNeg, or NOT instruction.
00305   ///
00306   static bool isNeg(const Value *V);
00307   static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
00308   static bool isNot(const Value *V);
00309 
00310   /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
00311   /// operation implemented via Sub, FSub, or Xor.
00312   ///
00313   static const Value *getNegArgument(const Value *BinOp);
00314   static       Value *getNegArgument(      Value *BinOp);
00315   static const Value *getFNegArgument(const Value *BinOp);
00316   static       Value *getFNegArgument(      Value *BinOp);
00317   static const Value *getNotArgument(const Value *BinOp);
00318   static       Value *getNotArgument(      Value *BinOp);
00319 
00320   BinaryOps getOpcode() const {
00321     return static_cast<BinaryOps>(Instruction::getOpcode());
00322   }
00323 
00324   /// Exchange the two operands to this instruction.
00325   /// This instruction is safe to use on any binary instruction and
00326   /// does not modify the semantics of the instruction.  If the instruction
00327   /// cannot be reversed (ie, it's a Div), then return true.
00328   ///
00329   bool swapOperands();
00330 
00331   /// Set or clear the nsw flag on this instruction, which must be an operator
00332   /// which supports this flag. See LangRef.html for the meaning of this flag.
00333   void setHasNoUnsignedWrap(bool b = true);
00334 
00335   /// Set or clear the nsw flag on this instruction, which must be an operator
00336   /// which supports this flag. See LangRef.html for the meaning of this flag.
00337   void setHasNoSignedWrap(bool b = true);
00338 
00339   /// Set or clear the exact flag on this instruction, which must be an operator
00340   /// which supports this flag. See LangRef.html for the meaning of this flag.
00341   void setIsExact(bool b = true);
00342 
00343   /// Determine whether the no unsigned wrap flag is set.
00344   bool hasNoUnsignedWrap() const;
00345 
00346   /// Determine whether the no signed wrap flag is set.
00347   bool hasNoSignedWrap() const;
00348 
00349   /// Determine whether the exact flag is set.
00350   bool isExact() const;
00351 
00352   /// Convenience method to copy supported wrapping, exact, and fast-math flags
00353   /// from V to this instruction.
00354   void copyIRFlags(const Value *V);
00355   
00356   /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
00357   /// V and this instruction.
00358   void andIRFlags(const Value *V);
00359 
00360   // Methods for support type inquiry through isa, cast, and dyn_cast:
00361   static inline bool classof(const Instruction *I) {
00362     return I->isBinaryOp();
00363   }
00364   static inline bool classof(const Value *V) {
00365     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00366   }
00367 };
00368 
00369 template <>
00370 struct OperandTraits<BinaryOperator> :
00371   public FixedNumOperandTraits<BinaryOperator, 2> {
00372 };
00373 
00374 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
00375 
00376 //===----------------------------------------------------------------------===//
00377 //                               CastInst Class
00378 //===----------------------------------------------------------------------===//
00379 
00380 /// This is the base class for all instructions that perform data
00381 /// casts. It is simply provided so that instruction category testing
00382 /// can be performed with code like:
00383 ///
00384 /// if (isa<CastInst>(Instr)) { ... }
00385 /// @brief Base class of casting instructions.
00386 class CastInst : public UnaryInstruction {
00387   void anchor() override;
00388 protected:
00389   /// @brief Constructor with insert-before-instruction semantics for subclasses
00390   CastInst(Type *Ty, unsigned iType, Value *S,
00391            const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
00392     : UnaryInstruction(Ty, iType, S, InsertBefore) {
00393     setName(NameStr);
00394   }
00395   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
00396   CastInst(Type *Ty, unsigned iType, Value *S,
00397            const Twine &NameStr, BasicBlock *InsertAtEnd)
00398     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
00399     setName(NameStr);
00400   }
00401 public:
00402   /// Provides a way to construct any of the CastInst subclasses using an
00403   /// opcode instead of the subclass's constructor. The opcode must be in the
00404   /// CastOps category (Instruction::isCast(opcode) returns true). This
00405   /// constructor has insert-before-instruction semantics to automatically
00406   /// insert the new CastInst before InsertBefore (if it is non-null).
00407   /// @brief Construct any of the CastInst subclasses
00408   static CastInst *Create(
00409     Instruction::CastOps,    ///< The opcode of the cast instruction
00410     Value *S,                ///< The value to be casted (operand 0)
00411     Type *Ty,          ///< The type to which cast should be made
00412     const Twine &Name = "", ///< Name for the instruction
00413     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00414   );
00415   /// Provides a way to construct any of the CastInst subclasses using an
00416   /// opcode instead of the subclass's constructor. The opcode must be in the
00417   /// CastOps category. This constructor has insert-at-end-of-block semantics
00418   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
00419   /// its non-null).
00420   /// @brief Construct any of the CastInst subclasses
00421   static CastInst *Create(
00422     Instruction::CastOps,    ///< The opcode for the cast instruction
00423     Value *S,                ///< The value to be casted (operand 0)
00424     Type *Ty,          ///< The type to which operand is casted
00425     const Twine &Name, ///< The name for the instruction
00426     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00427   );
00428 
00429   /// @brief Create a ZExt or BitCast cast instruction
00430   static CastInst *CreateZExtOrBitCast(
00431     Value *S,                ///< The value to be casted (operand 0)
00432     Type *Ty,          ///< The type to which cast should be made
00433     const Twine &Name = "", ///< Name for the instruction
00434     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00435   );
00436 
00437   /// @brief Create a ZExt or BitCast cast instruction
00438   static CastInst *CreateZExtOrBitCast(
00439     Value *S,                ///< The value to be casted (operand 0)
00440     Type *Ty,          ///< The type to which operand is casted
00441     const Twine &Name, ///< The name for the instruction
00442     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00443   );
00444 
00445   /// @brief Create a SExt or BitCast cast instruction
00446   static CastInst *CreateSExtOrBitCast(
00447     Value *S,                ///< The value to be casted (operand 0)
00448     Type *Ty,          ///< The type to which cast should be made
00449     const Twine &Name = "", ///< Name for the instruction
00450     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00451   );
00452 
00453   /// @brief Create a SExt or BitCast cast instruction
00454   static CastInst *CreateSExtOrBitCast(
00455     Value *S,                ///< The value to be casted (operand 0)
00456     Type *Ty,          ///< The type to which operand is casted
00457     const Twine &Name, ///< The name for the instruction
00458     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00459   );
00460 
00461   /// @brief Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
00462   static CastInst *CreatePointerCast(
00463     Value *S,                ///< The pointer value to be casted (operand 0)
00464     Type *Ty,          ///< The type to which operand is casted
00465     const Twine &Name, ///< The name for the instruction
00466     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00467   );
00468 
00469   /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
00470   static CastInst *CreatePointerCast(
00471     Value *S,                ///< The pointer value to be casted (operand 0)
00472     Type *Ty,          ///< The type to which cast should be made
00473     const Twine &Name = "", ///< Name for the instruction
00474     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00475   );
00476 
00477   /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
00478   static CastInst *CreatePointerBitCastOrAddrSpaceCast(
00479     Value *S,                ///< The pointer value to be casted (operand 0)
00480     Type *Ty,          ///< The type to which operand is casted
00481     const Twine &Name, ///< The name for the instruction
00482     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00483   );
00484 
00485   /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
00486   static CastInst *CreatePointerBitCastOrAddrSpaceCast(
00487     Value *S,                ///< The pointer value to be casted (operand 0)
00488     Type *Ty,          ///< The type to which cast should be made
00489     const Twine &Name = "", ///< Name for the instruction
00490     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00491   );
00492 
00493   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
00494   static CastInst *CreateIntegerCast(
00495     Value *S,                ///< The pointer value to be casted (operand 0)
00496     Type *Ty,          ///< The type to which cast should be made
00497     bool isSigned,           ///< Whether to regard S as signed or not
00498     const Twine &Name = "", ///< Name for the instruction
00499     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00500   );
00501 
00502   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
00503   static CastInst *CreateIntegerCast(
00504     Value *S,                ///< The integer value to be casted (operand 0)
00505     Type *Ty,          ///< The integer type to which operand is casted
00506     bool isSigned,           ///< Whether to regard S as signed or not
00507     const Twine &Name, ///< The name for the instruction
00508     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00509   );
00510 
00511   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
00512   static CastInst *CreateFPCast(
00513     Value *S,                ///< The floating point value to be casted
00514     Type *Ty,          ///< The floating point type to cast to
00515     const Twine &Name = "", ///< Name for the instruction
00516     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00517   );
00518 
00519   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
00520   static CastInst *CreateFPCast(
00521     Value *S,                ///< The floating point value to be casted
00522     Type *Ty,          ///< The floating point type to cast to
00523     const Twine &Name, ///< The name for the instruction
00524     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00525   );
00526 
00527   /// @brief Create a Trunc or BitCast cast instruction
00528   static CastInst *CreateTruncOrBitCast(
00529     Value *S,                ///< The value to be casted (operand 0)
00530     Type *Ty,          ///< The type to which cast should be made
00531     const Twine &Name = "", ///< Name for the instruction
00532     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
00533   );
00534 
00535   /// @brief Create a Trunc or BitCast cast instruction
00536   static CastInst *CreateTruncOrBitCast(
00537     Value *S,                ///< The value to be casted (operand 0)
00538     Type *Ty,          ///< The type to which operand is casted
00539     const Twine &Name, ///< The name for the instruction
00540     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00541   );
00542 
00543   /// @brief Check whether it is valid to call getCastOpcode for these types.
00544   static bool isCastable(
00545     Type *SrcTy, ///< The Type from which the value should be cast.
00546     Type *DestTy ///< The Type to which the value should be cast.
00547   );
00548 
00549   /// @brief Check whether a bitcast between these types is valid
00550   static bool isBitCastable(
00551     Type *SrcTy, ///< The Type from which the value should be cast.
00552     Type *DestTy ///< The Type to which the value should be cast.
00553   );
00554 
00555   /// Returns the opcode necessary to cast Val into Ty using usual casting
00556   /// rules.
00557   /// @brief Infer the opcode for cast operand and type
00558   static Instruction::CastOps getCastOpcode(
00559     const Value *Val, ///< The value to cast
00560     bool SrcIsSigned, ///< Whether to treat the source as signed
00561     Type *Ty,   ///< The Type to which the value should be casted
00562     bool DstIsSigned  ///< Whether to treate the dest. as signed
00563   );
00564 
00565   /// There are several places where we need to know if a cast instruction
00566   /// only deals with integer source and destination types. To simplify that
00567   /// logic, this method is provided.
00568   /// @returns true iff the cast has only integral typed operand and dest type.
00569   /// @brief Determine if this is an integer-only cast.
00570   bool isIntegerCast() const;
00571 
00572   /// A lossless cast is one that does not alter the basic value. It implies
00573   /// a no-op cast but is more stringent, preventing things like int->float,
00574   /// long->double, or int->ptr.
00575   /// @returns true iff the cast is lossless.
00576   /// @brief Determine if this is a lossless cast.
00577   bool isLosslessCast() const;
00578 
00579   /// A no-op cast is one that can be effected without changing any bits.
00580   /// It implies that the source and destination types are the same size. The
00581   /// IntPtrTy argument is used to make accurate determinations for casts
00582   /// involving Integer and Pointer types. They are no-op casts if the integer
00583   /// is the same size as the pointer. However, pointer size varies with
00584   /// platform. Generally, the result of DataLayout::getIntPtrType() should be
00585   /// passed in. If that's not available, use Type::Int64Ty, which will make
00586   /// the isNoopCast call conservative.
00587   /// @brief Determine if the described cast is a no-op cast.
00588   static bool isNoopCast(
00589     Instruction::CastOps Opcode,  ///< Opcode of cast
00590     Type *SrcTy,   ///< SrcTy of cast
00591     Type *DstTy,   ///< DstTy of cast
00592     Type *IntPtrTy ///< Integer type corresponding to Ptr types
00593   );
00594 
00595   /// @brief Determine if this cast is a no-op cast.
00596   bool isNoopCast(
00597     Type *IntPtrTy ///< Integer type corresponding to pointer
00598   ) const;
00599 
00600   /// @brief Determine if this cast is a no-op cast.
00601   bool isNoopCast(
00602     const DataLayout *DL ///< DataLayout to get the Int Ptr type from.
00603   ) const;
00604 
00605   /// Determine how a pair of casts can be eliminated, if they can be at all.
00606   /// This is a helper function for both CastInst and ConstantExpr.
00607   /// @returns 0 if the CastInst pair can't be eliminated, otherwise
00608   /// returns Instruction::CastOps value for a cast that can replace
00609   /// the pair, casting SrcTy to DstTy.
00610   /// @brief Determine if a cast pair is eliminable
00611   static unsigned isEliminableCastPair(
00612     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
00613     Instruction::CastOps secondOpcode, ///< Opcode of second cast
00614     Type *SrcTy, ///< SrcTy of 1st cast
00615     Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
00616     Type *DstTy, ///< DstTy of 2nd cast
00617     Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
00618     Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
00619     Type *DstIntPtrTy  ///< Integer type corresponding to Ptr DstTy, or null
00620   );
00621 
00622   /// @brief Return the opcode of this CastInst
00623   Instruction::CastOps getOpcode() const {
00624     return Instruction::CastOps(Instruction::getOpcode());
00625   }
00626 
00627   /// @brief Return the source type, as a convenience
00628   Type* getSrcTy() const { return getOperand(0)->getType(); }
00629   /// @brief Return the destination type, as a convenience
00630   Type* getDestTy() const { return getType(); }
00631 
00632   /// This method can be used to determine if a cast from S to DstTy using
00633   /// Opcode op is valid or not.
00634   /// @returns true iff the proposed cast is valid.
00635   /// @brief Determine if a cast is valid without creating one.
00636   static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
00637 
00638   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
00639   static inline bool classof(const Instruction *I) {
00640     return I->isCast();
00641   }
00642   static inline bool classof(const Value *V) {
00643     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00644   }
00645 };
00646 
00647 //===----------------------------------------------------------------------===//
00648 //                               CmpInst Class
00649 //===----------------------------------------------------------------------===//
00650 
00651 /// This class is the base class for the comparison instructions.
00652 /// @brief Abstract base class of comparison instructions.
00653 class CmpInst : public Instruction {
00654   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00655   CmpInst() LLVM_DELETED_FUNCTION;
00656 protected:
00657   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
00658           Value *LHS, Value *RHS, const Twine &Name = "",
00659           Instruction *InsertBefore = nullptr);
00660 
00661   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
00662           Value *LHS, Value *RHS, const Twine &Name,
00663           BasicBlock *InsertAtEnd);
00664 
00665   void anchor() override; // Out of line virtual method.
00666 public:
00667   /// This enumeration lists the possible predicates for CmpInst subclasses.
00668   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
00669   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
00670   /// predicate values are not overlapping between the classes.
00671   enum Predicate {
00672     // Opcode              U L G E    Intuitive operation
00673     FCMP_FALSE =  0,  ///< 0 0 0 0    Always false (always folded)
00674     FCMP_OEQ   =  1,  ///< 0 0 0 1    True if ordered and equal
00675     FCMP_OGT   =  2,  ///< 0 0 1 0    True if ordered and greater than
00676     FCMP_OGE   =  3,  ///< 0 0 1 1    True if ordered and greater than or equal
00677     FCMP_OLT   =  4,  ///< 0 1 0 0    True if ordered and less than
00678     FCMP_OLE   =  5,  ///< 0 1 0 1    True if ordered and less than or equal
00679     FCMP_ONE   =  6,  ///< 0 1 1 0    True if ordered and operands are unequal
00680     FCMP_ORD   =  7,  ///< 0 1 1 1    True if ordered (no nans)
00681     FCMP_UNO   =  8,  ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
00682     FCMP_UEQ   =  9,  ///< 1 0 0 1    True if unordered or equal
00683     FCMP_UGT   = 10,  ///< 1 0 1 0    True if unordered or greater than
00684     FCMP_UGE   = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
00685     FCMP_ULT   = 12,  ///< 1 1 0 0    True if unordered or less than
00686     FCMP_ULE   = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
00687     FCMP_UNE   = 14,  ///< 1 1 1 0    True if unordered or not equal
00688     FCMP_TRUE  = 15,  ///< 1 1 1 1    Always true (always folded)
00689     FIRST_FCMP_PREDICATE = FCMP_FALSE,
00690     LAST_FCMP_PREDICATE = FCMP_TRUE,
00691     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
00692     ICMP_EQ    = 32,  ///< equal
00693     ICMP_NE    = 33,  ///< not equal
00694     ICMP_UGT   = 34,  ///< unsigned greater than
00695     ICMP_UGE   = 35,  ///< unsigned greater or equal
00696     ICMP_ULT   = 36,  ///< unsigned less than
00697     ICMP_ULE   = 37,  ///< unsigned less or equal
00698     ICMP_SGT   = 38,  ///< signed greater than
00699     ICMP_SGE   = 39,  ///< signed greater or equal
00700     ICMP_SLT   = 40,  ///< signed less than
00701     ICMP_SLE   = 41,  ///< signed less or equal
00702     FIRST_ICMP_PREDICATE = ICMP_EQ,
00703     LAST_ICMP_PREDICATE = ICMP_SLE,
00704     BAD_ICMP_PREDICATE = ICMP_SLE + 1
00705   };
00706 
00707   // allocate space for exactly two operands
00708   void *operator new(size_t s) {
00709     return User::operator new(s, 2);
00710   }
00711   /// Construct a compare instruction, given the opcode, the predicate and
00712   /// the two operands.  Optionally (if InstBefore is specified) insert the
00713   /// instruction into a BasicBlock right before the specified instruction.
00714   /// The specified Instruction is allowed to be a dereferenced end iterator.
00715   /// @brief Create a CmpInst
00716   static CmpInst *Create(OtherOps Op,
00717                          unsigned short predicate, Value *S1,
00718                          Value *S2, const Twine &Name = "",
00719                          Instruction *InsertBefore = nullptr);
00720 
00721   /// Construct a compare instruction, given the opcode, the predicate and the
00722   /// two operands.  Also automatically insert this instruction to the end of
00723   /// the BasicBlock specified.
00724   /// @brief Create a CmpInst
00725   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
00726                          Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
00727 
00728   /// @brief Get the opcode casted to the right type
00729   OtherOps getOpcode() const {
00730     return static_cast<OtherOps>(Instruction::getOpcode());
00731   }
00732 
00733   /// @brief Return the predicate for this instruction.
00734   Predicate getPredicate() const {
00735     return Predicate(getSubclassDataFromInstruction());
00736   }
00737 
00738   /// @brief Set the predicate for this instruction to the specified value.
00739   void setPredicate(Predicate P) { setInstructionSubclassData(P); }
00740 
00741   static bool isFPPredicate(Predicate P) {
00742     return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
00743   }
00744 
00745   static bool isIntPredicate(Predicate P) {
00746     return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
00747   }
00748 
00749   bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
00750   bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
00751 
00752 
00753   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
00754   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
00755   /// @returns the inverse predicate for the instruction's current predicate.
00756   /// @brief Return the inverse of the instruction's predicate.
00757   Predicate getInversePredicate() const {
00758     return getInversePredicate(getPredicate());
00759   }
00760 
00761   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
00762   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
00763   /// @returns the inverse predicate for predicate provided in \p pred.
00764   /// @brief Return the inverse of a given predicate
00765   static Predicate getInversePredicate(Predicate pred);
00766 
00767   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
00768   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
00769   /// @returns the predicate that would be the result of exchanging the two
00770   /// operands of the CmpInst instruction without changing the result
00771   /// produced.
00772   /// @brief Return the predicate as if the operands were swapped
00773   Predicate getSwappedPredicate() const {
00774     return getSwappedPredicate(getPredicate());
00775   }
00776 
00777   /// This is a static version that you can use without an instruction
00778   /// available.
00779   /// @brief Return the predicate as if the operands were swapped.
00780   static Predicate getSwappedPredicate(Predicate pred);
00781 
00782   /// @brief Provide more efficient getOperand methods.
00783   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00784 
00785   /// This is just a convenience that dispatches to the subclasses.
00786   /// @brief Swap the operands and adjust predicate accordingly to retain
00787   /// the same comparison.
00788   void swapOperands();
00789 
00790   /// This is just a convenience that dispatches to the subclasses.
00791   /// @brief Determine if this CmpInst is commutative.
00792   bool isCommutative() const;
00793 
00794   /// This is just a convenience that dispatches to the subclasses.
00795   /// @brief Determine if this is an equals/not equals predicate.
00796   bool isEquality() const;
00797 
00798   /// @returns true if the comparison is signed, false otherwise.
00799   /// @brief Determine if this instruction is using a signed comparison.
00800   bool isSigned() const {
00801     return isSigned(getPredicate());
00802   }
00803 
00804   /// @returns true if the comparison is unsigned, false otherwise.
00805   /// @brief Determine if this instruction is using an unsigned comparison.
00806   bool isUnsigned() const {
00807     return isUnsigned(getPredicate());
00808   }
00809 
00810   /// This is just a convenience.
00811   /// @brief Determine if this is true when both operands are the same.
00812   bool isTrueWhenEqual() const {
00813     return isTrueWhenEqual(getPredicate());
00814   }
00815 
00816   /// This is just a convenience.
00817   /// @brief Determine if this is false when both operands are the same.
00818   bool isFalseWhenEqual() const {
00819     return isFalseWhenEqual(getPredicate());
00820   }
00821 
00822   /// @returns true if the predicate is unsigned, false otherwise.
00823   /// @brief Determine if the predicate is an unsigned operation.
00824   static bool isUnsigned(unsigned short predicate);
00825 
00826   /// @returns true if the predicate is signed, false otherwise.
00827   /// @brief Determine if the predicate is an signed operation.
00828   static bool isSigned(unsigned short predicate);
00829 
00830   /// @brief Determine if the predicate is an ordered operation.
00831   static bool isOrdered(unsigned short predicate);
00832 
00833   /// @brief Determine if the predicate is an unordered operation.
00834   static bool isUnordered(unsigned short predicate);
00835 
00836   /// Determine if the predicate is true when comparing a value with itself.
00837   static bool isTrueWhenEqual(unsigned short predicate);
00838 
00839   /// Determine if the predicate is false when comparing a value with itself.
00840   static bool isFalseWhenEqual(unsigned short predicate);
00841 
00842   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
00843   static inline bool classof(const Instruction *I) {
00844     return I->getOpcode() == Instruction::ICmp ||
00845            I->getOpcode() == Instruction::FCmp;
00846   }
00847   static inline bool classof(const Value *V) {
00848     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00849   }
00850 
00851   /// @brief Create a result type for fcmp/icmp
00852   static Type* makeCmpResultType(Type* opnd_type) {
00853     if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
00854       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
00855                              vt->getNumElements());
00856     }
00857     return Type::getInt1Ty(opnd_type->getContext());
00858   }
00859 private:
00860   // Shadow Value::setValueSubclassData with a private forwarding method so that
00861   // subclasses cannot accidentally use it.
00862   void setValueSubclassData(unsigned short D) {
00863     Value::setValueSubclassData(D);
00864   }
00865 };
00866 
00867 
00868 // FIXME: these are redundant if CmpInst < BinaryOperator
00869 template <>
00870 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
00871 };
00872 
00873 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
00874 
00875 } // End llvm namespace
00876 
00877 #endif