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