LLVM API Documentation
00001 //===-- llvm/Instructions.h - Instruction subclass definitions --*- 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 exposes the class definitions of all of the subclasses of the 00011 // Instruction class. This is meant to be an easy way to get access to all 00012 // instruction subclasses. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_INSTRUCTIONS_H 00017 #define LLVM_INSTRUCTIONS_H 00018 00019 #include <iterator> 00020 00021 #include "llvm/InstrTypes.h" 00022 #include "llvm/DerivedTypes.h" 00023 #include "llvm/ParameterAttributes.h" 00024 #include "llvm/BasicBlock.h" 00025 #include "llvm/ADT/SmallVector.h" 00026 00027 namespace llvm { 00028 00029 class ConstantInt; 00030 class PointerType; 00031 class VectorType; 00032 class ConstantRange; 00033 class APInt; 00034 00035 //===----------------------------------------------------------------------===// 00036 // AllocationInst Class 00037 //===----------------------------------------------------------------------===// 00038 00039 /// AllocationInst - This class is the common base class of MallocInst and 00040 /// AllocaInst. 00041 /// 00042 class AllocationInst : public UnaryInstruction { 00043 protected: 00044 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align, 00045 const std::string &Name = "", Instruction *InsertBefore = 0); 00046 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align, 00047 const std::string &Name, BasicBlock *InsertAtEnd); 00048 public: 00049 // Out of line virtual method, so the vtable, etc. has a home. 00050 virtual ~AllocationInst(); 00051 00052 /// isArrayAllocation - Return true if there is an allocation size parameter 00053 /// to the allocation instruction that is not 1. 00054 /// 00055 bool isArrayAllocation() const; 00056 00057 /// getArraySize - Get the number of element allocated, for a simple 00058 /// allocation of a single element, this will return a constant 1 value. 00059 /// 00060 const Value *getArraySize() const { return getOperand(0); } 00061 Value *getArraySize() { return getOperand(0); } 00062 00063 /// getType - Overload to return most specific pointer type 00064 /// 00065 const PointerType *getType() const { 00066 return reinterpret_cast<const PointerType*>(Instruction::getType()); 00067 } 00068 00069 /// getAllocatedType - Return the type that is being allocated by the 00070 /// instruction. 00071 /// 00072 const Type *getAllocatedType() const; 00073 00074 /// getAlignment - Return the alignment of the memory that is being allocated 00075 /// by the instruction. 00076 /// 00077 unsigned getAlignment() const { return (1u << SubclassData) >> 1; } 00078 void setAlignment(unsigned Align); 00079 00080 virtual Instruction *clone() const = 0; 00081 00082 // Methods for support type inquiry through isa, cast, and dyn_cast: 00083 static inline bool classof(const AllocationInst *) { return true; } 00084 static inline bool classof(const Instruction *I) { 00085 return I->getOpcode() == Instruction::Alloca || 00086 I->getOpcode() == Instruction::Malloc; 00087 } 00088 static inline bool classof(const Value *V) { 00089 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00090 } 00091 }; 00092 00093 00094 //===----------------------------------------------------------------------===// 00095 // MallocInst Class 00096 //===----------------------------------------------------------------------===// 00097 00098 /// MallocInst - an instruction to allocated memory on the heap 00099 /// 00100 class MallocInst : public AllocationInst { 00101 MallocInst(const MallocInst &MI); 00102 public: 00103 explicit MallocInst(const Type *Ty, Value *ArraySize = 0, 00104 const std::string &NameStr = "", 00105 Instruction *InsertBefore = 0) 00106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {} 00107 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr, 00108 BasicBlock *InsertAtEnd) 00109 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {} 00110 00111 MallocInst(const Type *Ty, const std::string &NameStr, 00112 Instruction *InsertBefore = 0) 00113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {} 00114 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd) 00115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {} 00116 00117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align, 00118 const std::string &NameStr, BasicBlock *InsertAtEnd) 00119 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {} 00120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align, 00121 const std::string &NameStr = "", 00122 Instruction *InsertBefore = 0) 00123 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {} 00124 00125 virtual MallocInst *clone() const; 00126 00127 // Methods for support type inquiry through isa, cast, and dyn_cast: 00128 static inline bool classof(const MallocInst *) { return true; } 00129 static inline bool classof(const Instruction *I) { 00130 return (I->getOpcode() == Instruction::Malloc); 00131 } 00132 static inline bool classof(const Value *V) { 00133 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00134 } 00135 }; 00136 00137 00138 //===----------------------------------------------------------------------===// 00139 // AllocaInst Class 00140 //===----------------------------------------------------------------------===// 00141 00142 /// AllocaInst - an instruction to allocate memory on the stack 00143 /// 00144 class AllocaInst : public AllocationInst { 00145 AllocaInst(const AllocaInst &); 00146 public: 00147 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0, 00148 const std::string &NameStr = "", 00149 Instruction *InsertBefore = 0) 00150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {} 00151 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr, 00152 BasicBlock *InsertAtEnd) 00153 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {} 00154 00155 AllocaInst(const Type *Ty, const std::string &NameStr, 00156 Instruction *InsertBefore = 0) 00157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {} 00158 AllocaInst(const Type *Ty, const std::string &NameStr, 00159 BasicBlock *InsertAtEnd) 00160 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {} 00161 00162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align, 00163 const std::string &NameStr = "", Instruction *InsertBefore = 0) 00164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {} 00165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align, 00166 const std::string &NameStr, BasicBlock *InsertAtEnd) 00167 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {} 00168 00169 virtual AllocaInst *clone() const; 00170 00171 // Methods for support type inquiry through isa, cast, and dyn_cast: 00172 static inline bool classof(const AllocaInst *) { return true; } 00173 static inline bool classof(const Instruction *I) { 00174 return (I->getOpcode() == Instruction::Alloca); 00175 } 00176 static inline bool classof(const Value *V) { 00177 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00178 } 00179 }; 00180 00181 00182 //===----------------------------------------------------------------------===// 00183 // FreeInst Class 00184 //===----------------------------------------------------------------------===// 00185 00186 /// FreeInst - an instruction to deallocate memory 00187 /// 00188 class FreeInst : public UnaryInstruction { 00189 void AssertOK(); 00190 public: 00191 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0); 00192 FreeInst(Value *Ptr, BasicBlock *InsertAfter); 00193 00194 virtual FreeInst *clone() const; 00195 00196 // Accessor methods for consistency with other memory operations 00197 Value *getPointerOperand() { return getOperand(0); } 00198 const Value *getPointerOperand() const { return getOperand(0); } 00199 00200 // Methods for support type inquiry through isa, cast, and dyn_cast: 00201 static inline bool classof(const FreeInst *) { return true; } 00202 static inline bool classof(const Instruction *I) { 00203 return (I->getOpcode() == Instruction::Free); 00204 } 00205 static inline bool classof(const Value *V) { 00206 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00207 } 00208 }; 00209 00210 00211 //===----------------------------------------------------------------------===// 00212 // LoadInst Class 00213 //===----------------------------------------------------------------------===// 00214 00215 /// LoadInst - an instruction for reading from memory. This uses the 00216 /// SubclassData field in Value to store whether or not the load is volatile. 00217 /// 00218 class LoadInst : public UnaryInstruction { 00219 00220 LoadInst(const LoadInst &LI) 00221 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) { 00222 setVolatile(LI.isVolatile()); 00223 setAlignment(LI.getAlignment()); 00224 00225 #ifndef NDEBUG 00226 AssertOK(); 00227 #endif 00228 } 00229 void AssertOK(); 00230 public: 00231 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore); 00232 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd); 00233 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false, 00234 Instruction *InsertBefore = 0); 00235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile, 00236 unsigned Align, Instruction *InsertBefore = 0); 00237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile, 00238 BasicBlock *InsertAtEnd); 00239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile, 00240 unsigned Align, BasicBlock *InsertAtEnd); 00241 00242 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore); 00243 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd); 00244 explicit LoadInst(Value *Ptr, const char *NameStr = 0, 00245 bool isVolatile = false, Instruction *InsertBefore = 0); 00246 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile, 00247 BasicBlock *InsertAtEnd); 00248 00249 /// isVolatile - Return true if this is a load from a volatile memory 00250 /// location. 00251 /// 00252 bool isVolatile() const { return SubclassData & 1; } 00253 00254 /// setVolatile - Specify whether this is a volatile load or not. 00255 /// 00256 void setVolatile(bool V) { 00257 SubclassData = (SubclassData & ~1) | (V ? 1 : 0); 00258 } 00259 00260 virtual LoadInst *clone() const; 00261 00262 /// getAlignment - Return the alignment of the access that is being performed 00263 /// 00264 unsigned getAlignment() const { 00265 return (1 << (SubclassData>>1)) >> 1; 00266 } 00267 00268 void setAlignment(unsigned Align); 00269 00270 Value *getPointerOperand() { return getOperand(0); } 00271 const Value *getPointerOperand() const { return getOperand(0); } 00272 static unsigned getPointerOperandIndex() { return 0U; } 00273 00274 // Methods for support type inquiry through isa, cast, and dyn_cast: 00275 static inline bool classof(const LoadInst *) { return true; } 00276 static inline bool classof(const Instruction *I) { 00277 return I->getOpcode() == Instruction::Load; 00278 } 00279 static inline bool classof(const Value *V) { 00280 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00281 } 00282 }; 00283 00284 00285 //===----------------------------------------------------------------------===// 00286 // StoreInst Class 00287 //===----------------------------------------------------------------------===// 00288 00289 /// StoreInst - an instruction for storing to memory 00290 /// 00291 class StoreInst : public Instruction { 00292 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 00293 00294 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, 00295 &Op<0>(), 2) { 00296 Op<0>() = SI.Op<0>(); 00297 Op<1>() = SI.Op<1>(); 00298 setVolatile(SI.isVolatile()); 00299 setAlignment(SI.getAlignment()); 00300 00301 #ifndef NDEBUG 00302 AssertOK(); 00303 #endif 00304 } 00305 void AssertOK(); 00306 public: 00307 // allocate space for exactly two operands 00308 void *operator new(size_t s) { 00309 return User::operator new(s, 2); 00310 } 00311 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); 00312 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); 00313 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, 00314 Instruction *InsertBefore = 0); 00315 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 00316 unsigned Align, Instruction *InsertBefore = 0); 00317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); 00318 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 00319 unsigned Align, BasicBlock *InsertAtEnd); 00320 00321 00322 /// isVolatile - Return true if this is a load from a volatile memory 00323 /// location. 00324 /// 00325 bool isVolatile() const { return SubclassData & 1; } 00326 00327 /// setVolatile - Specify whether this is a volatile load or not. 00328 /// 00329 void setVolatile(bool V) { 00330 SubclassData = (SubclassData & ~1) | (V ? 1 : 0); 00331 } 00332 00333 /// Transparently provide more efficient getOperand methods. 00334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00335 00336 /// getAlignment - Return the alignment of the access that is being performed 00337 /// 00338 unsigned getAlignment() const { 00339 return (1 << (SubclassData>>1)) >> 1; 00340 } 00341 00342 void setAlignment(unsigned Align); 00343 00344 virtual StoreInst *clone() const; 00345 00346 Value *getPointerOperand() { return getOperand(1); } 00347 const Value *getPointerOperand() const { return getOperand(1); } 00348 static unsigned getPointerOperandIndex() { return 1U; } 00349 00350 // Methods for support type inquiry through isa, cast, and dyn_cast: 00351 static inline bool classof(const StoreInst *) { return true; } 00352 static inline bool classof(const Instruction *I) { 00353 return I->getOpcode() == Instruction::Store; 00354 } 00355 static inline bool classof(const Value *V) { 00356 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00357 } 00358 }; 00359 00360 template <> 00361 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> { 00362 }; 00363 00364 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value) 00365 00366 //===----------------------------------------------------------------------===// 00367 // GetElementPtrInst Class 00368 //===----------------------------------------------------------------------===// 00369 00370 // checkType - Simple wrapper function to give a better assertion failure 00371 // message on bad indexes for a gep instruction. 00372 // 00373 static inline const Type *checkType(const Type *Ty) { 00374 assert(Ty && "Invalid GetElementPtrInst indices for type!"); 00375 return Ty; 00376 } 00377 00378 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to 00379 /// access elements of arrays and structs 00380 /// 00381 class GetElementPtrInst : public Instruction { 00382 GetElementPtrInst(const GetElementPtrInst &GEPI); 00383 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx, 00384 const std::string &NameStr); 00385 void init(Value *Ptr, Value *Idx, const std::string &NameStr); 00386 00387 template<typename InputIterator> 00388 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd, 00389 const std::string &NameStr, 00390 // This argument ensures that we have an iterator we can 00391 // do arithmetic on in constant time 00392 std::random_access_iterator_tag) { 00393 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd)); 00394 00395 if (NumIdx > 0) { 00396 // This requires that the iterator points to contiguous memory. 00397 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case 00398 // we have to build an array here 00399 } 00400 else { 00401 init(Ptr, 0, NumIdx, NameStr); 00402 } 00403 } 00404 00405 /// getIndexedType - Returns the type of the element that would be loaded with 00406 /// a load instruction with the specified parameters. 00407 /// 00408 /// Null is returned if the indices are invalid for the specified 00409 /// pointer type. 00410 /// 00411 template<typename InputIterator> 00412 static const Type *getIndexedType(const Type *Ptr, 00413 InputIterator IdxBegin, 00414 InputIterator IdxEnd, 00415 // This argument ensures that we 00416 // have an iterator we can do 00417 // arithmetic on in constant time 00418 std::random_access_iterator_tag) { 00419 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd)); 00420 00421 if (NumIdx > 0) 00422 // This requires that the iterator points to contiguous memory. 00423 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx); 00424 else 00425 return getIndexedType(Ptr, (Value *const*)0, NumIdx); 00426 } 00427 00428 /// Constructors - Create a getelementptr instruction with a base pointer an 00429 /// list of indices. The first ctor can optionally insert before an existing 00430 /// instruction, the second appends the new instruction to the specified 00431 /// BasicBlock. 00432 template<typename InputIterator> 00433 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, 00434 InputIterator IdxEnd, 00435 unsigned Values, 00436 const std::string &NameStr, 00437 Instruction *InsertBefore); 00438 template<typename InputIterator> 00439 inline GetElementPtrInst(Value *Ptr, 00440 InputIterator IdxBegin, InputIterator IdxEnd, 00441 unsigned Values, 00442 const std::string &NameStr, BasicBlock *InsertAtEnd); 00443 00444 /// Constructors - These two constructors are convenience methods because one 00445 /// and two index getelementptr instructions are so common. 00446 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "", 00447 Instruction *InsertBefore = 0); 00448 GetElementPtrInst(Value *Ptr, Value *Idx, 00449 const std::string &NameStr, BasicBlock *InsertAtEnd); 00450 public: 00451 template<typename InputIterator> 00452 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin, 00453 InputIterator IdxEnd, 00454 const std::string &NameStr = "", 00455 Instruction *InsertBefore = 0) { 00456 typename std::iterator_traits<InputIterator>::difference_type Values = 00457 1 + std::distance(IdxBegin, IdxEnd); 00458 return new(Values) 00459 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore); 00460 } 00461 template<typename InputIterator> 00462 static GetElementPtrInst *Create(Value *Ptr, 00463 InputIterator IdxBegin, InputIterator IdxEnd, 00464 const std::string &NameStr, 00465 BasicBlock *InsertAtEnd) { 00466 typename std::iterator_traits<InputIterator>::difference_type Values = 00467 1 + std::distance(IdxBegin, IdxEnd); 00468 return new(Values) 00469 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd); 00470 } 00471 00472 /// Constructors - These two creators are convenience methods because one 00473 /// index getelementptr instructions are so common. 00474 static GetElementPtrInst *Create(Value *Ptr, Value *Idx, 00475 const std::string &NameStr = "", 00476 Instruction *InsertBefore = 0) { 00477 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore); 00478 } 00479 static GetElementPtrInst *Create(Value *Ptr, Value *Idx, 00480 const std::string &NameStr, 00481 BasicBlock *InsertAtEnd) { 00482 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd); 00483 } 00484 00485 virtual GetElementPtrInst *clone() const; 00486 00487 /// Transparently provide more efficient getOperand methods. 00488 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00489 00490 // getType - Overload to return most specific pointer type... 00491 const PointerType *getType() const { 00492 return reinterpret_cast<const PointerType*>(Instruction::getType()); 00493 } 00494 00495 /// getIndexedType - Returns the type of the element that would be loaded with 00496 /// a load instruction with the specified parameters. 00497 /// 00498 /// Null is returned if the indices are invalid for the specified 00499 /// pointer type. 00500 /// 00501 template<typename InputIterator> 00502 static const Type *getIndexedType(const Type *Ptr, 00503 InputIterator IdxBegin, 00504 InputIterator IdxEnd) { 00505 return getIndexedType(Ptr, IdxBegin, IdxEnd, 00506 typename std::iterator_traits<InputIterator>:: 00507 iterator_category()); 00508 } 00509 00510 static const Type *getIndexedType(const Type *Ptr, 00511 Value* const *Idx, unsigned NumIdx); 00512 00513 static const Type *getIndexedType(const Type *Ptr, 00514 uint64_t const *Idx, unsigned NumIdx); 00515 00516 static const Type *getIndexedType(const Type *Ptr, Value *Idx); 00517 00518 inline op_iterator idx_begin() { return op_begin()+1; } 00519 inline const_op_iterator idx_begin() const { return op_begin()+1; } 00520 inline op_iterator idx_end() { return op_end(); } 00521 inline const_op_iterator idx_end() const { return op_end(); } 00522 00523 Value *getPointerOperand() { 00524 return getOperand(0); 00525 } 00526 const Value *getPointerOperand() const { 00527 return getOperand(0); 00528 } 00529 static unsigned getPointerOperandIndex() { 00530 return 0U; // get index for modifying correct operand 00531 } 00532 00533 unsigned getNumIndices() const { // Note: always non-negative 00534 return getNumOperands() - 1; 00535 } 00536 00537 bool hasIndices() const { 00538 return getNumOperands() > 1; 00539 } 00540 00541 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 00542 /// zeros. If so, the result pointer and the first operand have the same 00543 /// value, just potentially different types. 00544 bool hasAllZeroIndices() const; 00545 00546 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 00547 /// constant integers. If so, the result pointer and the first operand have 00548 /// a constant offset between them. 00549 bool hasAllConstantIndices() const; 00550 00551 00552 // Methods for support type inquiry through isa, cast, and dyn_cast: 00553 static inline bool classof(const GetElementPtrInst *) { return true; } 00554 static inline bool classof(const Instruction *I) { 00555 return (I->getOpcode() == Instruction::GetElementPtr); 00556 } 00557 static inline bool classof(const Value *V) { 00558 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00559 } 00560 }; 00561 00562 template <> 00563 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> { 00564 }; 00565 00566 template<typename InputIterator> 00567 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 00568 InputIterator IdxBegin, 00569 InputIterator IdxEnd, 00570 unsigned Values, 00571 const std::string &NameStr, 00572 Instruction *InsertBefore) 00573 : Instruction(PointerType::get(checkType( 00574 getIndexedType(Ptr->getType(), 00575 IdxBegin, IdxEnd)), 00576 cast<PointerType>(Ptr->getType()) 00577 ->getAddressSpace()), 00578 GetElementPtr, 00579 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 00580 Values, InsertBefore) { 00581 init(Ptr, IdxBegin, IdxEnd, NameStr, 00582 typename std::iterator_traits<InputIterator>::iterator_category()); 00583 } 00584 template<typename InputIterator> 00585 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 00586 InputIterator IdxBegin, 00587 InputIterator IdxEnd, 00588 unsigned Values, 00589 const std::string &NameStr, 00590 BasicBlock *InsertAtEnd) 00591 : Instruction(PointerType::get(checkType( 00592 getIndexedType(Ptr->getType(), 00593 IdxBegin, IdxEnd)), 00594 cast<PointerType>(Ptr->getType()) 00595 ->getAddressSpace()), 00596 GetElementPtr, 00597 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 00598 Values, InsertAtEnd) { 00599 init(Ptr, IdxBegin, IdxEnd, NameStr, 00600 typename std::iterator_traits<InputIterator>::iterator_category()); 00601 } 00602 00603 00604 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value) 00605 00606 00607 //===----------------------------------------------------------------------===// 00608 // ICmpInst Class 00609 //===----------------------------------------------------------------------===// 00610 00611 /// This instruction compares its operands according to the predicate given 00612 /// to the constructor. It only operates on integers or pointers. The operands 00613 /// must be identical types. 00614 /// @brief Represent an integer comparison operator. 00615 class ICmpInst: public CmpInst { 00616 public: 00617 /// @brief Constructor with insert-before-instruction semantics. 00618 ICmpInst( 00619 Predicate pred, ///< The predicate to use for the comparison 00620 Value *LHS, ///< The left-hand-side of the expression 00621 Value *RHS, ///< The right-hand-side of the expression 00622 const std::string &NameStr = "", ///< Name of the instruction 00623 Instruction *InsertBefore = 0 ///< Where to insert 00624 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr, 00625 InsertBefore) { 00626 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 00627 pred <= CmpInst::LAST_ICMP_PREDICATE && 00628 "Invalid ICmp predicate value"); 00629 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00630 "Both operands to ICmp instruction are not of the same type!"); 00631 // Check that the operands are the right type 00632 assert((getOperand(0)->getType()->isInteger() || 00633 isa<PointerType>(getOperand(0)->getType())) && 00634 "Invalid operand types for ICmp instruction"); 00635 } 00636 00637 /// @brief Constructor with insert-at-block-end semantics. 00638 ICmpInst( 00639 Predicate pred, ///< The predicate to use for the comparison 00640 Value *LHS, ///< The left-hand-side of the expression 00641 Value *RHS, ///< The right-hand-side of the expression 00642 const std::string &NameStr, ///< Name of the instruction 00643 BasicBlock *InsertAtEnd ///< Block to insert into. 00644 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr, 00645 InsertAtEnd) { 00646 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 00647 pred <= CmpInst::LAST_ICMP_PREDICATE && 00648 "Invalid ICmp predicate value"); 00649 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00650 "Both operands to ICmp instruction are not of the same type!"); 00651 // Check that the operands are the right type 00652 assert((getOperand(0)->getType()->isInteger() || 00653 isa<PointerType>(getOperand(0)->getType())) && 00654 "Invalid operand types for ICmp instruction"); 00655 } 00656 00657 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. 00658 /// @returns the predicate that would be the result if the operand were 00659 /// regarded as signed. 00660 /// @brief Return the signed version of the predicate 00661 Predicate getSignedPredicate() const { 00662 return getSignedPredicate(getPredicate()); 00663 } 00664 00665 /// This is a static version that you can use without an instruction. 00666 /// @brief Return the signed version of the predicate. 00667 static Predicate getSignedPredicate(Predicate pred); 00668 00669 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. 00670 /// @returns the predicate that would be the result if the operand were 00671 /// regarded as unsigned. 00672 /// @brief Return the unsigned version of the predicate 00673 Predicate getUnsignedPredicate() const { 00674 return getUnsignedPredicate(getPredicate()); 00675 } 00676 00677 /// This is a static version that you can use without an instruction. 00678 /// @brief Return the unsigned version of the predicate. 00679 static Predicate getUnsignedPredicate(Predicate pred); 00680 00681 /// isEquality - Return true if this predicate is either EQ or NE. This also 00682 /// tests for commutativity. 00683 static bool isEquality(Predicate P) { 00684 return P == ICMP_EQ || P == ICMP_NE; 00685 } 00686 00687 /// isEquality - Return true if this predicate is either EQ or NE. This also 00688 /// tests for commutativity. 00689 bool isEquality() const { 00690 return isEquality(getPredicate()); 00691 } 00692 00693 /// @returns true if the predicate of this ICmpInst is commutative 00694 /// @brief Determine if this relation is commutative. 00695 bool isCommutative() const { return isEquality(); } 00696 00697 /// isRelational - Return true if the predicate is relational (not EQ or NE). 00698 /// 00699 bool isRelational() const { 00700 return !isEquality(); 00701 } 00702 00703 /// isRelational - Return true if the predicate is relational (not EQ or NE). 00704 /// 00705 static bool isRelational(Predicate P) { 00706 return !isEquality(P); 00707 } 00708 00709 /// @returns true if the predicate of this ICmpInst is signed, false otherwise 00710 /// @brief Determine if this instruction's predicate is signed. 00711 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); } 00712 00713 /// @returns true if the predicate provided is signed, false otherwise 00714 /// @brief Determine if the predicate is signed. 00715 static bool isSignedPredicate(Predicate pred); 00716 00717 /// @returns true if the specified compare predicate is 00718 /// true when both operands are equal... 00719 /// @brief Determine if the icmp is true when both operands are equal 00720 static bool isTrueWhenEqual(ICmpInst::Predicate pred) { 00721 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || 00722 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || 00723 pred == ICmpInst::ICMP_SLE; 00724 } 00725 00726 /// @returns true if the specified compare instruction is 00727 /// true when both operands are equal... 00728 /// @brief Determine if the ICmpInst returns true when both operands are equal 00729 bool isTrueWhenEqual() { 00730 return isTrueWhenEqual(getPredicate()); 00731 } 00732 00733 /// Initialize a set of values that all satisfy the predicate with C. 00734 /// @brief Make a ConstantRange for a relation with a constant value. 00735 static ConstantRange makeConstantRange(Predicate pred, const APInt &C); 00736 00737 /// Exchange the two operands to this instruction in such a way that it does 00738 /// not modify the semantics of the instruction. The predicate value may be 00739 /// changed to retain the same result if the predicate is order dependent 00740 /// (e.g. ult). 00741 /// @brief Swap operands and adjust predicate. 00742 void swapOperands() { 00743 SubclassData = getSwappedPredicate(); 00744 Op<0>().swap(Op<1>()); 00745 } 00746 00747 virtual ICmpInst *clone() const; 00748 00749 // Methods for support type inquiry through isa, cast, and dyn_cast: 00750 static inline bool classof(const ICmpInst *) { return true; } 00751 static inline bool classof(const Instruction *I) { 00752 return I->getOpcode() == Instruction::ICmp; 00753 } 00754 static inline bool classof(const Value *V) { 00755 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00756 } 00757 }; 00758 00759 //===----------------------------------------------------------------------===// 00760 // FCmpInst Class 00761 //===----------------------------------------------------------------------===// 00762 00763 /// This instruction compares its operands according to the predicate given 00764 /// to the constructor. It only operates on floating point values or packed 00765 /// vectors of floating point values. The operands must be identical types. 00766 /// @brief Represents a floating point comparison operator. 00767 class FCmpInst: public CmpInst { 00768 public: 00769 /// @brief Constructor with insert-before-instruction semantics. 00770 FCmpInst( 00771 Predicate pred, ///< The predicate to use for the comparison 00772 Value *LHS, ///< The left-hand-side of the expression 00773 Value *RHS, ///< The right-hand-side of the expression 00774 const std::string &NameStr = "", ///< Name of the instruction 00775 Instruction *InsertBefore = 0 ///< Where to insert 00776 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr, 00777 InsertBefore) { 00778 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 00779 "Invalid FCmp predicate value"); 00780 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00781 "Both operands to FCmp instruction are not of the same type!"); 00782 // Check that the operands are the right type 00783 assert(getOperand(0)->getType()->isFloatingPoint() && 00784 "Invalid operand types for FCmp instruction"); 00785 } 00786 00787 /// @brief Constructor with insert-at-block-end semantics. 00788 FCmpInst( 00789 Predicate pred, ///< The predicate to use for the comparison 00790 Value *LHS, ///< The left-hand-side of the expression 00791 Value *RHS, ///< The right-hand-side of the expression 00792 const std::string &NameStr, ///< Name of the instruction 00793 BasicBlock *InsertAtEnd ///< Block to insert into. 00794 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr, 00795 InsertAtEnd) { 00796 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 00797 "Invalid FCmp predicate value"); 00798 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00799 "Both operands to FCmp instruction are not of the same type!"); 00800 // Check that the operands are the right type 00801 assert(getOperand(0)->getType()->isFloatingPoint() && 00802 "Invalid operand types for FCmp instruction"); 00803 } 00804 00805 /// This also tests for commutativity. If isEquality() returns true then 00806 /// the predicate is also commutative. Only the equality predicates are 00807 /// commutative. 00808 /// @returns true if the predicate of this instruction is EQ or NE. 00809 /// @brief Determine if this is an equality predicate. 00810 bool isEquality() const { 00811 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE || 00812 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE; 00813 } 00814 bool isCommutative() const { return isEquality(); } 00815 00816 /// @returns true if the predicate is relational (not EQ or NE). 00817 /// @brief Determine if this a relational predicate. 00818 bool isRelational() const { return !isEquality(); } 00819 00820 /// Exchange the two operands to this instruction in such a way that it does 00821 /// not modify the semantics of the instruction. The predicate value may be 00822 /// changed to retain the same result if the predicate is order dependent 00823 /// (e.g. ult). 00824 /// @brief Swap operands and adjust predicate. 00825 void swapOperands() { 00826 SubclassData = getSwappedPredicate(); 00827 Op<0>().swap(Op<1>()); 00828 } 00829 00830 virtual FCmpInst *clone() const; 00831 00832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 00833 static inline bool classof(const FCmpInst *) { return true; } 00834 static inline bool classof(const Instruction *I) { 00835 return I->getOpcode() == Instruction::FCmp; 00836 } 00837 static inline bool classof(const Value *V) { 00838 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00839 } 00840 }; 00841 00842 //===----------------------------------------------------------------------===// 00843 // VICmpInst Class 00844 //===----------------------------------------------------------------------===// 00845 00846 /// This instruction compares its operands according to the predicate given 00847 /// to the constructor. It only operates on vectors of integers. 00848 /// The operands must be identical types. 00849 /// @brief Represents a vector integer comparison operator. 00850 class VICmpInst: public CmpInst { 00851 public: 00852 /// @brief Constructor with insert-before-instruction semantics. 00853 VICmpInst( 00854 Predicate pred, ///< The predicate to use for the comparison 00855 Value *LHS, ///< The left-hand-side of the expression 00856 Value *RHS, ///< The right-hand-side of the expression 00857 const std::string &NameStr = "", ///< Name of the instruction 00858 Instruction *InsertBefore = 0 ///< Where to insert 00859 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr, 00860 InsertBefore) { 00861 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 00862 pred <= CmpInst::LAST_ICMP_PREDICATE && 00863 "Invalid VICmp predicate value"); 00864 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00865 "Both operands to VICmp instruction are not of the same type!"); 00866 } 00867 00868 /// @brief Constructor with insert-at-block-end semantics. 00869 VICmpInst( 00870 Predicate pred, ///< The predicate to use for the comparison 00871 Value *LHS, ///< The left-hand-side of the expression 00872 Value *RHS, ///< The right-hand-side of the expression 00873 const std::string &NameStr, ///< Name of the instruction 00874 BasicBlock *InsertAtEnd ///< Block to insert into. 00875 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr, 00876 InsertAtEnd) { 00877 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 00878 pred <= CmpInst::LAST_ICMP_PREDICATE && 00879 "Invalid VICmp predicate value"); 00880 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00881 "Both operands to VICmp instruction are not of the same type!"); 00882 } 00883 00884 /// @brief Return the predicate for this instruction. 00885 Predicate getPredicate() const { return Predicate(SubclassData); } 00886 00887 virtual VICmpInst *clone() const; 00888 00889 // Methods for support type inquiry through isa, cast, and dyn_cast: 00890 static inline bool classof(const VICmpInst *) { return true; } 00891 static inline bool classof(const Instruction *I) { 00892 return I->getOpcode() == Instruction::VICmp; 00893 } 00894 static inline bool classof(const Value *V) { 00895 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00896 } 00897 }; 00898 00899 //===----------------------------------------------------------------------===// 00900 // VFCmpInst Class 00901 //===----------------------------------------------------------------------===// 00902 00903 /// This instruction compares its operands according to the predicate given 00904 /// to the constructor. It only operates on vectors of floating point values. 00905 /// The operands must be identical types. 00906 /// @brief Represents a vector floating point comparison operator. 00907 class VFCmpInst: public CmpInst { 00908 public: 00909 /// @brief Constructor with insert-before-instruction semantics. 00910 VFCmpInst( 00911 Predicate pred, ///< The predicate to use for the comparison 00912 Value *LHS, ///< The left-hand-side of the expression 00913 Value *RHS, ///< The right-hand-side of the expression 00914 const std::string &NameStr = "", ///< Name of the instruction 00915 Instruction *InsertBefore = 0 ///< Where to insert 00916 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())), 00917 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) { 00918 assert(pred <= CmpInst::LAST_FCMP_PREDICATE && 00919 "Invalid VFCmp predicate value"); 00920 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00921 "Both operands to VFCmp instruction are not of the same type!"); 00922 } 00923 00924 /// @brief Constructor with insert-at-block-end semantics. 00925 VFCmpInst( 00926 Predicate pred, ///< The predicate to use for the comparison 00927 Value *LHS, ///< The left-hand-side of the expression 00928 Value *RHS, ///< The right-hand-side of the expression 00929 const std::string &NameStr, ///< Name of the instruction 00930 BasicBlock *InsertAtEnd ///< Block to insert into. 00931 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())), 00932 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) { 00933 assert(pred <= CmpInst::LAST_FCMP_PREDICATE && 00934 "Invalid VFCmp predicate value"); 00935 assert(getOperand(0)->getType() == getOperand(1)->getType() && 00936 "Both operands to VFCmp instruction are not of the same type!"); 00937 } 00938 00939 /// @brief Return the predicate for this instruction. 00940 Predicate getPredicate() const { return Predicate(SubclassData); } 00941 00942 virtual VFCmpInst *clone() const; 00943 00944 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 00945 static inline bool classof(const VFCmpInst *) { return true; } 00946 static inline bool classof(const Instruction *I) { 00947 return I->getOpcode() == Instruction::VFCmp; 00948 } 00949 static inline bool classof(const Value *V) { 00950 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00951 } 00952 }; 00953 00954 //===----------------------------------------------------------------------===// 00955 // CallInst Class 00956 //===----------------------------------------------------------------------===// 00957 /// CallInst - This class represents a function call, abstracting a target 00958 /// machine's calling convention. This class uses low bit of the SubClassData 00959 /// field to indicate whether or not this is a tail call. The rest of the bits 00960 /// hold the calling convention of the call. 00961 /// 00962 00963 class CallInst : public Instruction { 00964 PAListPtr ParamAttrs; ///< parameter attributes for call 00965 CallInst(const CallInst &CI); 00966 void init(Value *Func, Value* const *Params, unsigned NumParams); 00967 void init(Value *Func, Value *Actual1, Value *Actual2); 00968 void init(Value *Func, Value *Actual); 00969 void init(Value *Func); 00970 00971 template<typename InputIterator> 00972 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd, 00973 const std::string &NameStr, 00974 // This argument ensures that we have an iterator we can 00975 // do arithmetic on in constant time 00976 std::random_access_iterator_tag) { 00977 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd); 00978 00979 // This requires that the iterator points to contiguous memory. 00980 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs); 00981 setName(NameStr); 00982 } 00983 00984 /// Construct a CallInst given a range of arguments. InputIterator 00985 /// must be a random-access iterator pointing to contiguous storage 00986 /// (e.g. a std::vector<>::iterator). Checks are made for 00987 /// random-accessness but not for contiguous storage as that would 00988 /// incur runtime overhead. 00989 /// @brief Construct a CallInst from a range of arguments 00990 template<typename InputIterator> 00991 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd, 00992 const std::string &NameStr, Instruction *InsertBefore); 00993 00994 /// Construct a CallInst given a range of arguments. InputIterator 00995 /// must be a random-access iterator pointing to contiguous storage 00996 /// (e.g. a std::vector<>::iterator). Checks are made for 00997 /// random-accessness but not for contiguous storage as that would 00998 /// incur runtime overhead. 00999 /// @brief Construct a CallInst from a range of arguments 01000 template<typename InputIterator> 01001 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd, 01002 const std::string &NameStr, BasicBlock *InsertAtEnd); 01003 01004 CallInst(Value *F, Value *Actual, const std::string& NameStr, 01005 Instruction *InsertBefore); 01006 CallInst(Value *F, Value *Actual, const std::string& NameStr, 01007 BasicBlock *InsertAtEnd); 01008 explicit CallInst(Value *F, const std::string &NameStr, 01009 Instruction *InsertBefore); 01010 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd); 01011 public: 01012 template<typename InputIterator> 01013 static CallInst *Create(Value *Func, 01014 InputIterator ArgBegin, InputIterator ArgEnd, 01015 const std::string &NameStr = "", 01016 Instruction *InsertBefore = 0) { 01017 return new((unsigned)(ArgEnd - ArgBegin + 1)) 01018 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore); 01019 } 01020 template<typename InputIterator> 01021 static CallInst *Create(Value *Func, 01022 InputIterator ArgBegin, InputIterator ArgEnd, 01023 const std::string &NameStr, BasicBlock *InsertAtEnd) { 01024 return new((unsigned)(ArgEnd - ArgBegin + 1)) 01025 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd); 01026 } 01027 static CallInst *Create(Value *F, Value *Actual, 01028 const std::string& NameStr = "", 01029 Instruction *InsertBefore = 0) { 01030 return new(2) CallInst(F, Actual, NameStr, InsertBefore); 01031 } 01032 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr, 01033 BasicBlock *InsertAtEnd) { 01034 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd); 01035 } 01036 static CallInst *Create(Value *F, const std::string &NameStr = "", 01037 Instruction *InsertBefore = 0) { 01038 return new(1) CallInst(F, NameStr, InsertBefore); 01039 } 01040 static CallInst *Create(Value *F, const std::string &NameStr, 01041 BasicBlock *InsertAtEnd) { 01042 return new(1) CallInst(F, NameStr, InsertAtEnd); 01043 } 01044 01045 ~CallInst(); 01046 01047 virtual CallInst *clone() const; 01048 01049 /// Provide fast operand accessors 01050 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 01051 01052 bool isTailCall() const { return SubclassData & 1; } 01053 void setTailCall(bool isTC = true) { 01054 SubclassData = (SubclassData & ~1) | unsigned(isTC); 01055 } 01056 01057 /// getCallingConv/setCallingConv - Get or set the calling convention of this 01058 /// function call. 01059 unsigned getCallingConv() const { return SubclassData >> 1; } 01060 void setCallingConv(unsigned CC) { 01061 SubclassData = (SubclassData & 1) | (CC << 1); 01062 } 01063 01064 /// getParamAttrs - Return the parameter attributes for this call. 01065 /// 01066 const PAListPtr &getParamAttrs() const { return ParamAttrs; } 01067 01068 /// setParamAttrs - Sets the parameter attributes for this call. 01069 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; } 01070 01071 /// addParamAttr - adds the attribute to the list of attributes. 01072 void addParamAttr(unsigned i, ParameterAttributes attr); 01073 01074 /// removeParamAttr - removes the attribute from the list of attributes. 01075 void removeParamAttr(unsigned i, ParameterAttributes attr); 01076 01077 /// @brief Determine whether the call or the callee has the given attribute. 01078 bool paramHasAttr(unsigned i, unsigned attr) const; 01079 01080 /// @brief Extract the alignment for a call or parameter (0=unknown). 01081 unsigned getParamAlignment(unsigned i) const { 01082 return ParamAttrs.getParamAlignment(i); 01083 } 01084 01085 /// @brief Deter