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_INSTRUCTION_TYPES_H
00017 #define LLVM_INSTRUCTION_TYPES_H
00018 
00019 #include "llvm/Instruction.h"
00020 #include "llvm/OperandTraits.h"
00021 #include "llvm/DerivedTypes.h"
00022 
00023 namespace llvm {
00024 
00025 //===----------------------------------------------------------------------===//
00026 //                            TerminatorInst Class
00027 //===----------------------------------------------------------------------===//
00028 
00029 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
00030 /// block.  Thus, these are all the flow control type of operations.
00031 ///
00032 class TerminatorInst : public Instruction {
00033 protected:
00034   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
00035                  Use *Ops, unsigned NumOps,
00036                  Instruction *InsertBefore = 0)
00037     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
00038 
00039   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
00040                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
00041     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
00042 
00043   // Out of line virtual method, so the vtable, etc has a home.
00044   ~TerminatorInst();
00045 
00046   /// Virtual methods - Terminators should overload these and provide inline
00047   /// overrides of non-V methods.
00048   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
00049   virtual unsigned getNumSuccessorsV() const = 0;
00050   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
00051 public:
00052 
00053   virtual Instruction *clone() const = 0;
00054 
00055   /// getNumSuccessors - Return the number of successors that this terminator
00056   /// has.
00057   unsigned getNumSuccessors() const {
00058     return getNumSuccessorsV();
00059   }
00060 
00061   /// getSuccessor - Return the specified successor.
00062   ///
00063   BasicBlock *getSuccessor(unsigned idx) const {
00064     return getSuccessorV(idx);
00065   }
00066 
00067   /// setSuccessor - Update the specified successor to point at the provided
00068   /// block.
00069   void setSuccessor(unsigned idx, BasicBlock *B) {
00070     setSuccessorV(idx, B);
00071   }
00072 
00073   // Methods for support type inquiry through isa, cast, and dyn_cast:
00074   static inline bool classof(const TerminatorInst *) { return true; }
00075   static inline bool classof(const Instruction *I) {
00076     return I->isTerminator();
00077   }
00078   static inline bool classof(const Value *V) {
00079     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00080   }
00081 };
00082 
00083 
00084 //===----------------------------------------------------------------------===//
00085 //                          UnaryInstruction Class
00086 //===----------------------------------------------------------------------===//
00087 
00088 class UnaryInstruction : public Instruction {
00089   void *operator new(size_t, unsigned);      // Do not implement
00090   UnaryInstruction(const UnaryInstruction&); // Do not implement
00091 
00092 protected:
00093   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
00094     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
00095     Op<0>() = V;
00096   }
00097   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
00098     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
00099     Op<0>() = V;
00100   }
00101 public:
00102   // allocate space for exactly one operand
00103   void *operator new(size_t s) {
00104     return User::operator new(s, 1);
00105   }
00106 
00107   // Out of line virtual method, so the vtable, etc has a home.
00108   ~UnaryInstruction();
00109 
00110   /// Transparently provide more efficient getOperand methods.
00111   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00112   
00113   // Methods for support type inquiry through isa, cast, and dyn_cast:
00114   static inline bool classof(const UnaryInstruction *) { return true; }
00115   static inline bool classof(const Instruction *I) {
00116     return I->getOpcode() == Instruction::Malloc ||
00117            I->getOpcode() == Instruction::Alloca ||
00118            I->getOpcode() == Instruction::Free ||
00119            I->getOpcode() == Instruction::Load ||
00120            I->getOpcode() == Instruction::VAArg ||
00121            I->getOpcode() == Instruction::ExtractValue ||
00122            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
00123   }
00124   static inline bool classof(const Value *V) {
00125     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00126   }
00127 };
00128 
00129 template <>
00130 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
00131 };
00132 
00133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
00134 
00135 //===----------------------------------------------------------------------===//
00136 //                           BinaryOperator Class
00137 //===----------------------------------------------------------------------===//
00138 
00139 class BinaryOperator : public Instruction {
00140   void *operator new(size_t, unsigned); // Do not implement
00141 protected:
00142   void init(BinaryOps iType);
00143   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
00144                  const std::string &Name, Instruction *InsertBefore);
00145   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
00146                  const std::string &Name, BasicBlock *InsertAtEnd);
00147 public:
00148   // allocate space for exactly two operands
00149   void *operator new(size_t s) {
00150     return User::operator new(s, 2);
00151   }
00152 
00153   /// Transparently provide more efficient getOperand methods.
00154   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00155 
00156   /// Create() - Construct a binary instruction, given the opcode and the two
00157   /// operands.  Optionally (if InstBefore is specified) insert the instruction
00158   /// into a BasicBlock right before the specified instruction.  The specified
00159   /// Instruction is allowed to be a dereferenced end iterator.
00160   ///
00161   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
00162                                 const std::string &Name = "",
00163                                 Instruction *InsertBefore = 0);
00164 
00165   /// Create() - Construct a binary instruction, given the opcode and the two
00166   /// operands.  Also automatically insert this instruction to the end of the
00167   /// BasicBlock specified.
00168   ///
00169   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
00170                                 const std::string &Name,
00171                                 BasicBlock *InsertAtEnd);
00172 
00173   /// Create* - These methods just forward to Create, and are useful when you
00174   /// statically know what type of instruction you're going to create.  These
00175   /// helpers just save some typing.
00176 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00177   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00178                                      const std::string &Name = "") {\
00179     return Create(Instruction::OPC, V1, V2, Name);\
00180   }
00181 #include "llvm/Instruction.def"
00182 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00183   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00184                                      const std::string &Name, BasicBlock *BB) {\
00185     return Create(Instruction::OPC, V1, V2, Name, BB);\
00186   }
00187 #include "llvm/Instruction.def"
00188 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
00189   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
00190                                      const std::string &Name, Instruction *I) {\
00191     return Create(Instruction::OPC, V1, V2, Name, I);\
00192   }
00193 #include "llvm/Instruction.def"
00194 
00195 
00196   /// Helper functions to construct and inspect unary operations (NEG and NOT)
00197   /// via binary operators SUB and XOR:
00198   ///
00199   /// CreateNeg, CreateNot - Create the NEG and NOT
00200   ///     instructions out of SUB and XOR instructions.
00201   ///
00202   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
00203                                    Instruction *InsertBefore = 0);
00204   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
00205                                    BasicBlock *InsertAtEnd);
00206   static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
00207                                    Instruction *InsertBefore = 0);
00208   static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
00209                                    BasicBlock *InsertAtEnd);
00210 
00211   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
00212   ///
00213   static bool isNeg(const Value *V);
00214   static bool isNot(const Value *V);
00215 
00216   /// getNegArgument, getNotArgument - Helper functions to extract the
00217   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
00218   ///
00219   static const Value *getNegArgument(const Value *BinOp);
00220   static       Value *getNegArgument(      Value *BinOp);
00221   static const Value *getNotArgument(const Value *BinOp);
00222   static       Value *getNotArgument(      Value *BinOp);
00223 
00224   BinaryOps getOpcode() const {
00225     return static_cast<BinaryOps>(Instruction::getOpcode());
00226   }
00227 
00228   virtual BinaryOperator *clone() const;
00229 
00230   /// swapOperands - Exchange the two operands to this instruction.
00231   /// This instruction is safe to use on any binary instruction and
00232   /// does not modify the semantics of the instruction.  If the instruction
00233   /// cannot be reversed (ie, it's a Div), then return true.
00234   ///
00235   bool swapOperands();
00236 
00237   // Methods for support type inquiry through isa, cast, and dyn_cast:
00238   static inline bool classof(const BinaryOperator *) { return true; }
00239   static inline bool classof(const Instruction *I) {
00240     return I->isBinaryOp();
00241   }
00242   static inline bool classof(const Value *V) {
00243     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00244   }
00245 };
00246 
00247 template <>
00248 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
00249 };
00250 
00251 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
00252 
00253 //===----------------------------------------------------------------------===//
00254 //                               CastInst Class
00255 //===----------------------------------------------------------------------===//
00256 
00257 /// CastInst - This is the base class for all instructions that perform data
00258 /// casts. It is simply provided so that instruction category testing
00259 /// can be performed with code like:
00260 ///
00261 /// if (isa<CastInst>(Instr)) { ... }
00262 /// @brief Base class of casting instructions.
00263 class CastInst : public UnaryInstruction {
00264   /// @brief Copy constructor
00265   CastInst(const CastInst &CI)
00266     : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
00267   }
00268   /// @brief Do not allow default construction
00269   CastInst(); 
00270 protected:
00271   /// @brief Constructor with insert-before-instruction semantics for subclasses
00272   CastInst(const Type *Ty, unsigned iType, Value *S, 
00273            const std::string &NameStr = "", Instruction *InsertBefore = 0)
00274     : UnaryInstruction(Ty, iType, S, InsertBefore) {
00275     setName(NameStr);
00276   }
00277   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
00278   CastInst(const Type *Ty, unsigned iType, Value *S, 
00279            const std::string &NameStr, BasicBlock *InsertAtEnd)
00280     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
00281     setName(NameStr);
00282   }
00283 public:
00284   /// Provides a way to construct any of the CastInst subclasses using an 
00285   /// opcode instead of the subclass's constructor. The opcode must be in the
00286   /// CastOps category (Instruction::isCast(opcode) returns true). This
00287   /// constructor has insert-before-instruction semantics to automatically
00288   /// insert the new CastInst before InsertBefore (if it is non-null).
00289   /// @brief Construct any of the CastInst subclasses
00290   static CastInst *Create(
00291     Instruction::CastOps,    ///< The opcode of the cast instruction
00292     Value *S,                ///< The value to be casted (operand 0)
00293     const Type *Ty,          ///< The type to which cast should be made
00294     const std::string &Name = "", ///< Name for the instruction
00295     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00296   );
00297   /// Provides a way to construct any of the CastInst subclasses using an
00298   /// opcode instead of the subclass's constructor. The opcode must be in the
00299   /// CastOps category. This constructor has insert-at-end-of-block semantics
00300   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
00301   /// its non-null).
00302   /// @brief Construct any of the CastInst subclasses
00303   static CastInst *Create(
00304     Instruction::CastOps,    ///< The opcode for the cast instruction
00305     Value *S,                ///< The value to be casted (operand 0)
00306     const Type *Ty,          ///< The type to which operand is casted
00307     const std::string &Name, ///< The name for the instruction
00308     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00309   );
00310 
00311   /// @brief Create a ZExt or BitCast cast instruction
00312   static CastInst *CreateZExtOrBitCast(
00313     Value *S,                ///< The value to be casted (operand 0)
00314     const Type *Ty,          ///< The type to which cast should be made
00315     const std::string &Name = "", ///< Name for the instruction
00316     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00317   );
00318 
00319   /// @brief Create a ZExt or BitCast cast instruction
00320   static CastInst *CreateZExtOrBitCast(
00321     Value *S,                ///< The value to be casted (operand 0)
00322     const Type *Ty,          ///< The type to which operand is casted
00323     const std::string &Name, ///< The name for the instruction
00324     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00325   );
00326 
00327   /// @brief Create a SExt or BitCast cast instruction
00328   static CastInst *CreateSExtOrBitCast(
00329     Value *S,                ///< The value to be casted (operand 0)
00330     const Type *Ty,          ///< The type to which cast should be made
00331     const std::string &Name = "", ///< Name for the instruction
00332     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00333   );
00334 
00335   /// @brief Create a SExt or BitCast cast instruction
00336   static CastInst *CreateSExtOrBitCast(
00337     Value *S,                ///< The value to be casted (operand 0)
00338     const Type *Ty,          ///< The type to which operand is casted
00339     const std::string &Name, ///< The name for the instruction
00340     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00341   );
00342 
00343   /// @brief Create a BitCast or a PtrToInt cast instruction
00344   static CastInst *CreatePointerCast(
00345     Value *S,                ///< The pointer value to be casted (operand 0)
00346     const Type *Ty,          ///< The type to which operand is casted
00347     const std::string &Name, ///< The name for the instruction
00348     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00349   );
00350 
00351   /// @brief Create a BitCast or a PtrToInt cast instruction
00352   static CastInst *CreatePointerCast(
00353     Value *S,                ///< The pointer value to be casted (operand 0)
00354     const Type *Ty,          ///< The type to which cast should be made
00355     const std::string &Name = "", ///< Name for the instruction
00356     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00357   );
00358 
00359   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
00360   static CastInst *CreateIntegerCast(
00361     Value *S,                ///< The pointer value to be casted (operand 0)
00362     const Type *Ty,          ///< The type to which cast should be made
00363     bool isSigned,           ///< Whether to regard S as signed or not
00364     const std::string &Name = "", ///< Name for the instruction
00365     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00366   );
00367 
00368   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
00369   static CastInst *CreateIntegerCast(
00370     Value *S,                ///< The integer value to be casted (operand 0)
00371     const Type *Ty,          ///< The integer type to which operand is casted
00372     bool isSigned,           ///< Whether to regard S as signed or not
00373     const std::string &Name, ///< The name for the instruction
00374     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00375   );
00376 
00377   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
00378   static CastInst *CreateFPCast(
00379     Value *S,                ///< The floating point value to be casted 
00380     const Type *Ty,          ///< The floating point type to cast to
00381     const std::string &Name = "", ///< Name for the instruction
00382     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00383   );
00384 
00385   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
00386   static CastInst *CreateFPCast(
00387     Value *S,                ///< The floating point value to be casted 
00388     const Type *Ty,          ///< The floating point type to cast to
00389     const std::string &Name, ///< The name for the instruction
00390     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00391   );
00392 
00393   /// @brief Create a Trunc or BitCast cast instruction
00394   static CastInst *CreateTruncOrBitCast(
00395     Value *S,                ///< The value to be casted (operand 0)
00396     const Type *Ty,          ///< The type to which cast should be made
00397     const std::string &Name = "", ///< Name for the instruction
00398     Instruction *InsertBefore = 0 ///< Place to insert the instruction
00399   );
00400 
00401   /// @brief Create a Trunc or BitCast cast instruction
00402   static CastInst *CreateTruncOrBitCast(
00403     Value *S,                ///< The value to be casted (operand 0)
00404     const Type *Ty,          ///< The type to which operand is casted
00405     const std::string &Name, ///< The name for the instruction
00406     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
00407   );
00408 
00409   /// @brief Check whether it is valid to call getCastOpcode for these types.
00410   static bool isCastable(
00411     const Type *SrcTy, ///< The Type from which the value should be cast.
00412     const Type *DestTy ///< The Type to which the value should be cast.
00413   );
00414 
00415   /// Returns the opcode necessary to cast Val into Ty using usual casting
00416   /// rules.
00417   /// @brief Infer the opcode for cast operand and type
00418   static Instruction::CastOps getCastOpcode(
00419     const Value *Val, ///< The value to cast
00420     bool SrcIsSigned, ///< Whether to treat the source as signed
00421     const Type *Ty,   ///< The Type to which the value should be casted
00422     bool DstIsSigned  ///< Whether to treate the dest. as signed
00423   );
00424 
00425   /// There are several places where we need to know if a cast instruction 
00426   /// only deals with integer source and destination types. To simplify that
00427   /// logic, this method is provided.
00428   /// @returns true iff the cast has only integral typed operand and dest type.
00429   /// @brief Determine if this is an integer-only cast.
00430   bool isIntegerCast() const;
00431 
00432   /// A lossless cast is one that does not alter the basic value. It implies
00433   /// a no-op cast but is more stringent, preventing things like int->float,
00434   /// long->double, int->ptr, or vector->anything. 
00435   /// @returns true iff the cast is lossless.
00436   /// @brief Determine if this is a lossless cast.
00437   bool isLosslessCast() const;
00438 
00439   /// A no-op cast is one that can be effected without changing any bits. 
00440   /// It implies that the source and destination types are the same size. The
00441   /// IntPtrTy argument is used to make accurate determinations for casts 
00442   /// involving Integer and Pointer types. They are no-op casts if the integer
00443   /// is the same size as the pointer. However, pointer size varies with 
00444   /// platform. Generally, the result of TargetData::getIntPtrType() should be
00445   /// passed in. If that's not available, use Type::Int64Ty, which will make
00446   /// the isNoopCast call conservative.
00447   /// @brief Determine if this cast is a no-op cast. 
00448   bool isNoopCast(
00449     const Type *IntPtrTy ///< Integer type corresponding to pointer
00450   ) const;
00451 
00452   /// Determine how a pair of casts can be eliminated, if they can be at all.
00453   /// This is a helper function for both CastInst and ConstantExpr.
00454   /// @returns 0 if the CastInst pair can't be eliminated
00455   /// @returns Instruction::CastOps value for a cast that can replace 
00456   /// the pair, casting SrcTy to DstTy.
00457   /// @brief Determine if a cast pair is eliminable
00458   static unsigned isEliminableCastPair(
00459     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
00460     Instruction::CastOps secondOpcode, ///< Opcode of second cast
00461     const Type *SrcTy, ///< SrcTy of 1st cast
00462     const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
00463     const Type *DstTy, ///< DstTy of 2nd cast
00464     const Type *IntPtrTy ///< Integer type corresponding to Ptr types
00465   );
00466 
00467   /// @brief Return the opcode of this CastInst
00468   Instruction::CastOps getOpcode() const { 
00469     return Instruction::CastOps(Instruction::getOpcode()); 
00470   }
00471 
00472   /// @brief Return the source type, as a convenience
00473   const Type* getSrcTy() const { return getOperand(0)->getType(); }
00474   /// @brief Return the destination type, as a convenience
00475   const Type* getDestTy() const { return getType(); }
00476 
00477   /// This method can be used to determine if a cast from S to DstTy using
00478   /// Opcode op is valid or not. 
00479   /// @returns true iff the proposed cast is valid.
00480   /// @brief Determine if a cast is valid without creating one.
00481   static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
00482 
00483   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
00484   static inline bool classof(const CastInst *) { return true; }
00485   static inline bool classof(const Instruction *I) {
00486     return I->isCast();
00487   }
00488   static inline bool classof(const Value *V) {
00489     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00490   }
00491 };
00492 
00493 //===----------------------------------------------------------------------===//
00494 //                               CmpInst Class
00495 //===----------------------------------------------------------------------===//
00496 
00497 /// This class is the base class for the comparison instructions. 
00498 /// @brief Abstract base class of comparison instructions.
00499 // FIXME: why not derive from BinaryOperator?
00500 class CmpInst: public Instruction {
00501   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
00502   CmpInst(); // do not implement
00503 protected:
00504   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
00505           Value *LHS, Value *RHS, const std::string &Name = "",
00506           Instruction *InsertBefore = 0);
00507   
00508   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
00509           Value *LHS, Value *RHS, const std::string &Name,
00510           BasicBlock *InsertAtEnd);
00511 
00512 public:
00513   /// This enumeration lists the possible predicates for CmpInst subclasses.
00514   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
00515   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
00516   /// predicate values are not overlapping between the classes.
00517   enum Predicate {
00518     // Opcode             U L G E    Intuitive operation
00519     FCMP_FALSE =  0,  /// 0 0 0 0    Always false (always folded)
00520     FCMP_OEQ   =  1,  /// 0 0 0 1    True if ordered and equal
00521     FCMP_OGT   =  2,  /// 0 0 1 0    True if ordered and greater than
00522     FCMP_OGE   =  3,  /// 0 0 1 1    True if ordered and greater than or equal
00523     FCMP_OLT   =  4,  /// 0 1 0 0    True if ordered and less than
00524     FCMP_OLE   =  5,  /// 0 1 0 1    True if ordered and less than or equal
00525     FCMP_ONE   =  6,  /// 0 1 1 0    True if ordered and operands are unequal
00526     FCMP_ORD   =  7,  /// 0 1 1 1    True if ordered (no nans)
00527     FCMP_UNO   =  8,  /// 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
00528     FCMP_UEQ   =  9,  /// 1 0 0 1    True if unordered or equal
00529     FCMP_UGT   = 10,  /// 1 0 1 0    True if unordered or greater than
00530     FCMP_UGE   = 11,  /// 1 0 1 1    True if unordered, greater than, or equal
00531     FCMP_ULT   = 12,  /// 1 1 0 0    True if unordered or less than
00532     FCMP_ULE   = 13,  /// 1 1 0 1    True if unordered, less than, or equal
00533     FCMP_UNE   = 14,  /// 1 1 1 0    True if unordered or not equal
00534     FCMP_TRUE  = 15,  /// 1 1 1 1    Always true (always folded)
00535     FIRST_FCMP_PREDICATE = FCMP_FALSE,
00536     LAST_FCMP_PREDICATE = FCMP_TRUE,
00537     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
00538     ICMP_EQ    = 32,  /// equal
00539     ICMP_NE    = 33,  /// not equal
00540     ICMP_UGT   = 34,  /// unsigned greater than
00541     ICMP_UGE   = 35,  /// unsigned greater or equal
00542     ICMP_ULT   = 36,  /// unsigned less than
00543     ICMP_ULE   = 37,  /// unsigned less or equal
00544     ICMP_SGT   = 38,  /// signed greater than
00545     ICMP_SGE   = 39,  /// signed greater or equal
00546     ICMP_SLT   = 40,  /// signed less than
00547     ICMP_SLE   = 41,  /// signed less or equal
00548     FIRST_ICMP_PREDICATE = ICMP_EQ,
00549     LAST_ICMP_PREDICATE = ICMP_SLE,
00550     BAD_ICMP_PREDICATE = ICMP_SLE + 1
00551   };
00552 
00553   // allocate space for exactly two operands
00554   void *operator new(size_t s) {
00555     return User::operator new(s, 2);
00556   }
00557   /// Construct a compare instruction, given the opcode, the predicate and 
00558   /// the two operands.  Optionally (if InstBefore is specified) insert the 
00559   /// instruction into a BasicBlock right before the specified instruction.  
00560   /// The specified Instruction is allowed to be a dereferenced end iterator.
00561   /// @brief Create a CmpInst
00562   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
00563                          Value *S2, const std::string &Name = "",
00564                          Instruction *InsertBefore = 0);
00565 
00566   /// Construct a compare instruction, given the opcode, the predicate and the 
00567   /// two operands.  Also automatically insert this instruction to the end of 
00568   /// the BasicBlock specified.
00569   /// @brief Create a CmpInst
00570   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
00571                          Value *S2, const std::string &Name, 
00572                          BasicBlock *InsertAtEnd);
00573 
00574   /// @brief Get the opcode casted to the right type
00575   OtherOps getOpcode() const {
00576     return static_cast<OtherOps>(Instruction::getOpcode());
00577   }
00578 
00579   /// @brief Return the predicate for this instruction.
00580   Predicate getPredicate() const { return Predicate(SubclassData); }
00581 
00582   /// @brief Set the predicate for this instruction to the specified value.
00583   void setPredicate(Predicate P) { SubclassData = P; }
00584   
00585   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
00586   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
00587   /// @returns the inverse predicate for the instruction's current predicate. 
00588   /// @brief Return the inverse of the instruction's predicate.
00589   Predicate getInversePredicate() const {
00590     return getInversePredicate(getPredicate());
00591   }
00592 
00593   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
00594   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
00595   /// @returns the inverse predicate for predicate provided in \p pred. 
00596   /// @brief Return the inverse of a given predicate
00597   static Predicate getInversePredicate(Predicate pred);
00598 
00599   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
00600   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
00601   /// @returns the predicate that would be the result of exchanging the two 
00602   /// operands of the CmpInst instruction without changing the result 
00603   /// produced.  
00604   /// @brief Return the predicate as if the operands were swapped
00605   Predicate getSwappedPredicate() const {
00606     return getSwappedPredicate(getPredicate());
00607   }
00608 
00609   /// This is a static version that you can use without an instruction 
00610   /// available.
00611   /// @brief Return the predicate as if the operands were swapped.
00612   static Predicate getSwappedPredicate(Predicate pred);
00613 
00614   /// @brief Provide more efficient getOperand methods.
00615   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00616 
00617   /// This is just a convenience that dispatches to the subclasses.
00618   /// @brief Swap the operands and adjust predicate accordingly to retain
00619   /// the same comparison.
00620   void swapOperands();
00621 
00622   /// This is just a convenience that dispatches to the subclasses.
00623   /// @brief Determine if this CmpInst is commutative.
00624   bool isCommutative();
00625 
00626   /// This is just a convenience that dispatches to the subclasses.
00627   /// @brief Determine if this is an equals/not equals predicate.
00628   bool isEquality();
00629 
00630   /// @returns true if the predicate is unsigned, false otherwise.
00631   /// @brief Determine if the predicate is an unsigned operation.
00632   static bool isUnsigned(unsigned short predicate);
00633 
00634   /// @returns true if the predicate is signed, false otherwise.
00635   /// @brief Determine if the predicate is an signed operation.
00636   static bool isSigned(unsigned short predicate);
00637 
00638   /// @brief Determine if the predicate is an ordered operation.
00639   static bool isOrdered(unsigned short predicate);
00640 
00641   /// @brief Determine if the predicate is an unordered operation.
00642   static bool isUnordered(unsigned short predicate);
00643 
00644   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
00645   static inline bool classof(const CmpInst *) { return true; }
00646   static inline bool classof(const Instruction *I) {
00647     return I->getOpcode() == Instruction::ICmp || 
00648            I->getOpcode() == Instruction::FCmp ||
00649            I->getOpcode() == Instruction::VICmp ||
00650            I->getOpcode() == Instruction::VFCmp;
00651   }
00652   static inline bool classof(const Value *V) {
00653     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00654   }
00655   /// @brief Create a result type for fcmp/icmp (but not vicmp/vfcmp)
00656   static const Type* makeCmpResultType(const Type* opnd_type) {
00657     if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
00658       return VectorType::get(Type::Int1Ty, vt->getNumElements());
00659     }
00660     return Type::Int1Ty;
00661   }
00662 };
00663 
00664 
00665 // FIXME: these are redundant if CmpInst < BinaryOperator
00666 template <>
00667 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
00668 };
00669 
00670 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
00671 
00672 } // End llvm namespace
00673 
00674 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.