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