1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file exposes the class definitions of all of the subclasses of the 11 // Instruction class. This is meant to be an easy way to get access to all 12 // instruction subclasses. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_INSTRUCTIONS_H 17 #define LLVM_INSTRUCTIONS_H 18 19 #include "llvm/InstrTypes.h" 20 #include "llvm/DerivedTypes.h" 21 #include "llvm/Attributes.h" 22 #include "llvm/CallingConv.h" 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include <iterator> 26 27 namespace llvm { 28 29 class ConstantInt; 30 class ConstantRange; 31 class APInt; 32 class LLVMContext; 33 34 //===----------------------------------------------------------------------===// 35 // AllocaInst Class 36 //===----------------------------------------------------------------------===// 37 38 /// AllocaInst - an instruction to allocate memory on the stack 39 /// 40 class AllocaInst : public UnaryInstruction { 41 protected: 42 virtual AllocaInst *clone_impl() const; 43 public: 44 explicit AllocaInst(Type *Ty, Value *ArraySize = 0, 45 const Twine &Name = "", Instruction *InsertBefore = 0); 46 AllocaInst(Type *Ty, Value *ArraySize, 47 const Twine &Name, BasicBlock *InsertAtEnd); 48 49 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0); 50 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd); 51 52 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 53 const Twine &Name = "", Instruction *InsertBefore = 0); 54 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 55 const Twine &Name, BasicBlock *InsertAtEnd); 56 57 // Out of line virtual method, so the vtable, etc. has a home. 58 virtual ~AllocaInst(); 59 60 /// isArrayAllocation - Return true if there is an allocation size parameter 61 /// to the allocation instruction that is not 1. 62 /// 63 bool isArrayAllocation() const; 64 65 /// getArraySize - Get the number of elements allocated. For a simple 66 /// allocation of a single element, this will return a constant 1 value. 67 /// 68 const Value *getArraySize() const { return getOperand(0); } 69 Value *getArraySize() { return getOperand(0); } 70 71 /// getType - Overload to return most specific pointer type 72 /// 73 PointerType *getType() const { 74 return reinterpret_cast<PointerType*>(Instruction::getType()); 75 } 76 77 /// getAllocatedType - Return the type that is being allocated by the 78 /// instruction. 79 /// 80 Type *getAllocatedType() const; 81 82 /// getAlignment - Return the alignment of the memory that is being allocated 83 /// by the instruction. 84 /// 85 unsigned getAlignment() const { 86 return (1u << getSubclassDataFromInstruction()) >> 1; 87 } 88 void setAlignment(unsigned Align); 89 90 /// isStaticAlloca - Return true if this alloca is in the entry block of the 91 /// function and is a constant size. If so, the code generator will fold it 92 /// into the prolog/epilog code, so it is basically free. 93 bool isStaticAlloca() const; 94 95 // Methods for support type inquiry through isa, cast, and dyn_cast: 96 static inline bool classof(const AllocaInst *) { return true; } 97 static inline bool classof(const Instruction *I) { 98 return (I->getOpcode() == Instruction::Alloca); 99 } 100 static inline bool classof(const Value *V) { 101 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 102 } 103 private: 104 // Shadow Instruction::setInstructionSubclassData with a private forwarding 105 // method so that subclasses cannot accidentally use it. 106 void setInstructionSubclassData(unsigned short D) { 107 Instruction::setInstructionSubclassData(D); 108 } 109 }; 110 111 112 //===----------------------------------------------------------------------===// 113 // LoadInst Class 114 //===----------------------------------------------------------------------===// 115 116 /// LoadInst - an instruction for reading from memory. This uses the 117 /// SubclassData field in Value to store whether or not the load is volatile. 118 /// 119 class LoadInst : public UnaryInstruction { 120 void AssertOK(); 121 protected: 122 virtual LoadInst *clone_impl() const; 123 public: 124 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore); 125 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); 126 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false, 127 Instruction *InsertBefore = 0); 128 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 129 unsigned Align, Instruction *InsertBefore = 0); 130 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 131 BasicBlock *InsertAtEnd); 132 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 133 unsigned Align, BasicBlock *InsertAtEnd); 134 135 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore); 136 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd); 137 explicit LoadInst(Value *Ptr, const char *NameStr = 0, 138 bool isVolatile = false, Instruction *InsertBefore = 0); 139 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile, 140 BasicBlock *InsertAtEnd); 141 142 /// isVolatile - Return true if this is a load from a volatile memory 143 /// location. 144 /// 145 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 146 147 /// setVolatile - Specify whether this is a volatile load or not. 148 /// 149 void setVolatile(bool V) { 150 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 151 (V ? 1 : 0)); 152 } 153 154 /// getAlignment - Return the alignment of the access that is being performed 155 /// 156 unsigned getAlignment() const { 157 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1; 158 } 159 160 void setAlignment(unsigned Align); 161 162 Value *getPointerOperand() { return getOperand(0); } 163 const Value *getPointerOperand() const { return getOperand(0); } 164 static unsigned getPointerOperandIndex() { return 0U; } 165 166 unsigned getPointerAddressSpace() const { 167 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace(); 168 } 169 170 171 // Methods for support type inquiry through isa, cast, and dyn_cast: 172 static inline bool classof(const LoadInst *) { return true; } 173 static inline bool classof(const Instruction *I) { 174 return I->getOpcode() == Instruction::Load; 175 } 176 static inline bool classof(const Value *V) { 177 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 178 } 179 private: 180 // Shadow Instruction::setInstructionSubclassData with a private forwarding 181 // method so that subclasses cannot accidentally use it. 182 void setInstructionSubclassData(unsigned short D) { 183 Instruction::setInstructionSubclassData(D); 184 } 185 }; 186 187 188 //===----------------------------------------------------------------------===// 189 // StoreInst Class 190 //===----------------------------------------------------------------------===// 191 192 /// StoreInst - an instruction for storing to memory 193 /// 194 class StoreInst : public Instruction { 195 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 196 void AssertOK(); 197 protected: 198 virtual StoreInst *clone_impl() const; 199 public: 200 // allocate space for exactly two operands 201 void *operator new(size_t s) { 202 return User::operator new(s, 2); 203 } 204 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); 205 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); 206 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, 207 Instruction *InsertBefore = 0); 208 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 209 unsigned Align, Instruction *InsertBefore = 0); 210 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); 211 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 212 unsigned Align, BasicBlock *InsertAtEnd); 213 214 215 /// isVolatile - Return true if this is a load from a volatile memory 216 /// location. 217 /// 218 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 219 220 /// setVolatile - Specify whether this is a volatile load or not. 221 /// 222 void setVolatile(bool V) { 223 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 224 (V ? 1 : 0)); 225 } 226 227 /// Transparently provide more efficient getOperand methods. 228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 229 230 /// getAlignment - Return the alignment of the access that is being performed 231 /// 232 unsigned getAlignment() const { 233 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1; 234 } 235 236 void setAlignment(unsigned Align); 237 238 Value *getValueOperand() { return getOperand(0); } 239 const Value *getValueOperand() const { return getOperand(0); } 240 241 Value *getPointerOperand() { return getOperand(1); } 242 const Value *getPointerOperand() const { return getOperand(1); } 243 static unsigned getPointerOperandIndex() { return 1U; } 244 245 unsigned getPointerAddressSpace() const { 246 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace(); 247 } 248 249 // Methods for support type inquiry through isa, cast, and dyn_cast: 250 static inline bool classof(const StoreInst *) { return true; } 251 static inline bool classof(const Instruction *I) { 252 return I->getOpcode() == Instruction::Store; 253 } 254 static inline bool classof(const Value *V) { 255 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 256 } 257 private: 258 // Shadow Instruction::setInstructionSubclassData with a private forwarding 259 // method so that subclasses cannot accidentally use it. 260 void setInstructionSubclassData(unsigned short D) { 261 Instruction::setInstructionSubclassData(D); 262 } 263 }; 264 265 template <> 266 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { 267 }; 268 269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value) 270 271 //===----------------------------------------------------------------------===// 272 // GetElementPtrInst Class 273 //===----------------------------------------------------------------------===// 274 275 // checkGEPType - Simple wrapper function to give a better assertion failure 276 // message on bad indexes for a gep instruction. 277 // 278 static inline Type *checkGEPType(Type *Ty) { 279 assert(Ty && "Invalid GetElementPtrInst indices for type!"); 280 return Ty; 281 } 282 283 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to 284 /// access elements of arrays and structs 285 /// 286 class GetElementPtrInst : public Instruction { 287 GetElementPtrInst(const GetElementPtrInst &GEPI); 288 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx, 289 const Twine &NameStr); 290 void init(Value *Ptr, Value *Idx, const Twine &NameStr); 291 292 template<typename RandomAccessIterator> 293 void init(Value *Ptr, 294 RandomAccessIterator IdxBegin, 295 RandomAccessIterator IdxEnd, 296 const Twine &NameStr, 297 // This argument ensures that we have an iterator we can 298 // do arithmetic on in constant time 299 std::random_access_iterator_tag) { 300 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd)); 301 302 if (NumIdx > 0) { 303 // This requires that the iterator points to contiguous memory. 304 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case 305 // we have to build an array here 306 } 307 else { 308 init(Ptr, 0, NumIdx, NameStr); 309 } 310 } 311 312 /// getIndexedType - Returns the type of the element that would be loaded with 313 /// a load instruction with the specified parameters. 314 /// 315 /// Null is returned if the indices are invalid for the specified 316 /// pointer type. 317 /// 318 template<typename RandomAccessIterator> 319 static Type *getIndexedType(Type *Ptr, 320 RandomAccessIterator IdxBegin, 321 RandomAccessIterator IdxEnd, 322 // This argument ensures that we 323 // have an iterator we can do 324 // arithmetic on in constant time 325 std::random_access_iterator_tag) { 326 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd)); 327 328 if (NumIdx > 0) 329 // This requires that the iterator points to contiguous memory. 330 return getIndexedType(Ptr, &*IdxBegin, NumIdx); 331 else 332 return getIndexedType(Ptr, (Value *const*)0, NumIdx); 333 } 334 335 /// Constructors - Create a getelementptr instruction with a base pointer an 336 /// list of indices. The first ctor can optionally insert before an existing 337 /// instruction, the second appends the new instruction to the specified 338 /// BasicBlock. 339 template<typename RandomAccessIterator> 340 inline GetElementPtrInst(Value *Ptr, RandomAccessIterator IdxBegin, 341 RandomAccessIterator IdxEnd, 342 unsigned Values, 343 const Twine &NameStr, 344 Instruction *InsertBefore); 345 template<typename RandomAccessIterator> 346 inline GetElementPtrInst(Value *Ptr, 347 RandomAccessIterator IdxBegin, 348 RandomAccessIterator IdxEnd, 349 unsigned Values, 350 const Twine &NameStr, BasicBlock *InsertAtEnd); 351 352 /// Constructors - These two constructors are convenience methods because one 353 /// and two index getelementptr instructions are so common. 354 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "", 355 Instruction *InsertBefore = 0); 356 GetElementPtrInst(Value *Ptr, Value *Idx, 357 const Twine &NameStr, BasicBlock *InsertAtEnd); 358 protected: 359 virtual GetElementPtrInst *clone_impl() const; 360 public: 361 template<typename RandomAccessIterator> 362 static GetElementPtrInst *Create(Value *Ptr, RandomAccessIterator IdxBegin, 363 RandomAccessIterator IdxEnd, 364 const Twine &NameStr = "", 365 Instruction *InsertBefore = 0) { 366 typename std::iterator_traits<RandomAccessIterator>::difference_type 367 Values = 1 + std::distance(IdxBegin, IdxEnd); 368 return new(Values) 369 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore); 370 } 371 template<typename RandomAccessIterator> 372 static GetElementPtrInst *Create(Value *Ptr, 373 RandomAccessIterator IdxBegin, 374 RandomAccessIterator IdxEnd, 375 const Twine &NameStr, 376 BasicBlock *InsertAtEnd) { 377 typename std::iterator_traits<RandomAccessIterator>::difference_type 378 Values = 1 + std::distance(IdxBegin, IdxEnd); 379 return new(Values) 380 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd); 381 } 382 383 /// Constructors - These two creators are convenience methods because one 384 /// index getelementptr instructions are so common. 385 static GetElementPtrInst *Create(Value *Ptr, Value *Idx, 386 const Twine &NameStr = "", 387 Instruction *InsertBefore = 0) { 388 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore); 389 } 390 static GetElementPtrInst *Create(Value *Ptr, Value *Idx, 391 const Twine &NameStr, 392 BasicBlock *InsertAtEnd) { 393 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd); 394 } 395 396 /// Create an "inbounds" getelementptr. See the documentation for the 397 /// "inbounds" flag in LangRef.html for details. 398 template<typename RandomAccessIterator> 399 static GetElementPtrInst *CreateInBounds(Value *Ptr, 400 RandomAccessIterator IdxBegin, 401 RandomAccessIterator IdxEnd, 402 const Twine &NameStr = "", 403 Instruction *InsertBefore = 0) { 404 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd, 405 NameStr, InsertBefore); 406 GEP->setIsInBounds(true); 407 return GEP; 408 } 409 template<typename RandomAccessIterator> 410 static GetElementPtrInst *CreateInBounds(Value *Ptr, 411 RandomAccessIterator IdxBegin, 412 RandomAccessIterator IdxEnd, 413 const Twine &NameStr, 414 BasicBlock *InsertAtEnd) { 415 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd, 416 NameStr, InsertAtEnd); 417 GEP->setIsInBounds(true); 418 return GEP; 419 } 420 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx, 421 const Twine &NameStr = "", 422 Instruction *InsertBefore = 0) { 423 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore); 424 GEP->setIsInBounds(true); 425 return GEP; 426 } 427 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx, 428 const Twine &NameStr, 429 BasicBlock *InsertAtEnd) { 430 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd); 431 GEP->setIsInBounds(true); 432 return GEP; 433 } 434 435 /// Transparently provide more efficient getOperand methods. 436 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 437 438 // getType - Overload to return most specific pointer type... 439 PointerType *getType() const { 440 return reinterpret_cast<PointerType*>(Instruction::getType()); 441 } 442 443 /// getIndexedType - Returns the type of the element that would be loaded with 444 /// a load instruction with the specified parameters. 445 /// 446 /// Null is returned if the indices are invalid for the specified 447 /// pointer type. 448 /// 449 template<typename RandomAccessIterator> 450 static Type *getIndexedType(Type *Ptr, RandomAccessIterator IdxBegin, 451 RandomAccessIterator IdxEnd) { 452 return getIndexedType(Ptr, IdxBegin, IdxEnd, 453 typename std::iterator_traits<RandomAccessIterator>:: 454 iterator_category()); 455 } 456 457 // FIXME: Use ArrayRef 458 static Type *getIndexedType(Type *Ptr, 459 Value* const *Idx, unsigned NumIdx); 460 static Type *getIndexedType(Type *Ptr, 461 Constant* const *Idx, unsigned NumIdx); 462 463 static Type *getIndexedType(Type *Ptr, 464 uint64_t const *Idx, unsigned NumIdx); 465 static Type *getIndexedType(Type *Ptr, Value *Idx); 466 467 inline op_iterator idx_begin() { return op_begin()+1; } 468 inline const_op_iterator idx_begin() const { return op_begin()+1; } 469 inline op_iterator idx_end() { return op_end(); } 470 inline const_op_iterator idx_end() const { return op_end(); } 471 472 Value *getPointerOperand() { 473 return getOperand(0); 474 } 475 const Value *getPointerOperand() const { 476 return getOperand(0); 477 } 478 static unsigned getPointerOperandIndex() { 479 return 0U; // get index for modifying correct operand 480 } 481 482 unsigned getPointerAddressSpace() const { 483 return cast<PointerType>(getType())->getAddressSpace(); 484 } 485 486 /// getPointerOperandType - Method to return the pointer operand as a 487 /// PointerType. 488 PointerType *getPointerOperandType() const { 489 return reinterpret_cast<PointerType*>(getPointerOperand()->getType()); 490 } 491 492 493 unsigned getNumIndices() const { // Note: always non-negative 494 return getNumOperands() - 1; 495 } 496 497 bool hasIndices() const { 498 return getNumOperands() > 1; 499 } 500 501 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 502 /// zeros. If so, the result pointer and the first operand have the same 503 /// value, just potentially different types. 504 bool hasAllZeroIndices() const; 505 506 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 507 /// constant integers. If so, the result pointer and the first operand have 508 /// a constant offset between them. 509 bool hasAllConstantIndices() const; 510 511 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction. 512 /// See LangRef.html for the meaning of inbounds on a getelementptr. 513 void setIsInBounds(bool b = true); 514 515 /// isInBounds - Determine whether the GEP has the inbounds flag. 516 bool isInBounds() const; 517 518 // Methods for support type inquiry through isa, cast, and dyn_cast: 519 static inline bool classof(const GetElementPtrInst *) { return true; } 520 static inline bool classof(const Instruction *I) { 521 return (I->getOpcode() == Instruction::GetElementPtr); 522 } 523 static inline bool classof(const Value *V) { 524 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 525 } 526 }; 527 528 template <> 529 struct OperandTraits<GetElementPtrInst> : 530 public VariadicOperandTraits<GetElementPtrInst, 1> { 531 }; 532 533 template<typename RandomAccessIterator> 534 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 535 RandomAccessIterator IdxBegin, 536 RandomAccessIterator IdxEnd, 537 unsigned Values, 538 const Twine &NameStr, 539 Instruction *InsertBefore) 540 : Instruction(PointerType::get(checkGEPType( 541 getIndexedType(Ptr->getType(), 542 IdxBegin, IdxEnd)), 543 cast<PointerType>(Ptr->getType()) 544 ->getAddressSpace()), 545 GetElementPtr, 546 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 547 Values, InsertBefore) { 548 init(Ptr, IdxBegin, IdxEnd, NameStr, 549 typename std::iterator_traits<RandomAccessIterator> 550 ::iterator_category()); 551 } 552 template<typename RandomAccessIterator> 553 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 554 RandomAccessIterator IdxBegin, 555 RandomAccessIterator IdxEnd, 556 unsigned Values, 557 const Twine &NameStr, 558 BasicBlock *InsertAtEnd) 559 : Instruction(PointerType::get(checkGEPType( 560 getIndexedType(Ptr->getType(), 561 IdxBegin, IdxEnd)), 562 cast<PointerType>(Ptr->getType()) 563 ->getAddressSpace()), 564 GetElementPtr, 565 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 566 Values, InsertAtEnd) { 567 init(Ptr, IdxBegin, IdxEnd, NameStr, 568 typename std::iterator_traits<RandomAccessIterator> 569 ::iterator_category()); 570 } 571 572 573 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value) 574 575 576 //===----------------------------------------------------------------------===// 577 // ICmpInst Class 578 //===----------------------------------------------------------------------===// 579 580 /// This instruction compares its operands according to the predicate given 581 /// to the constructor. It only operates on integers or pointers. The operands 582 /// must be identical types. 583 /// @brief Represent an integer comparison operator. 584 class ICmpInst: public CmpInst { 585 protected: 586 /// @brief Clone an identical ICmpInst 587 virtual ICmpInst *clone_impl() const; 588 public: 589 /// @brief Constructor with insert-before-instruction semantics. 590 ICmpInst( 591 Instruction *InsertBefore, ///< Where to insert 592 Predicate pred, ///< The predicate to use for the comparison 593 Value *LHS, ///< The left-hand-side of the expression 594 Value *RHS, ///< The right-hand-side of the expression 595 const Twine &NameStr = "" ///< Name of the instruction 596 ) : CmpInst(makeCmpResultType(LHS->getType()), 597 Instruction::ICmp, pred, LHS, RHS, NameStr, 598 InsertBefore) { 599 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 600 pred <= CmpInst::LAST_ICMP_PREDICATE && 601 "Invalid ICmp predicate value"); 602 assert(getOperand(0)->getType() == getOperand(1)->getType() && 603 "Both operands to ICmp instruction are not of the same type!"); 604 // Check that the operands are the right type 605 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 606 getOperand(0)->getType()->isPointerTy()) && 607 "Invalid operand types for ICmp instruction"); 608 } 609 610 /// @brief Constructor with insert-at-end semantics. 611 ICmpInst( 612 BasicBlock &InsertAtEnd, ///< Block to insert into. 613 Predicate pred, ///< The predicate to use for the comparison 614 Value *LHS, ///< The left-hand-side of the expression 615 Value *RHS, ///< The right-hand-side of the expression 616 const Twine &NameStr = "" ///< Name of the instruction 617 ) : CmpInst(makeCmpResultType(LHS->getType()), 618 Instruction::ICmp, pred, LHS, RHS, NameStr, 619 &InsertAtEnd) { 620 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 621 pred <= CmpInst::LAST_ICMP_PREDICATE && 622 "Invalid ICmp predicate value"); 623 assert(getOperand(0)->getType() == getOperand(1)->getType() && 624 "Both operands to ICmp instruction are not of the same type!"); 625 // Check that the operands are the right type 626 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 627 getOperand(0)->getType()->isPointerTy()) && 628 "Invalid operand types for ICmp instruction"); 629 } 630 631 /// @brief Constructor with no-insertion semantics 632 ICmpInst( 633 Predicate pred, ///< The predicate to use for the comparison 634 Value *LHS, ///< The left-hand-side of the expression 635 Value *RHS, ///< The right-hand-side of the expression 636 const Twine &NameStr = "" ///< Name of the instruction 637 ) : CmpInst(makeCmpResultType(LHS->getType()), 638 Instruction::ICmp, pred, LHS, RHS, NameStr) { 639 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 640 pred <= CmpInst::LAST_ICMP_PREDICATE && 641 "Invalid ICmp predicate value"); 642 assert(getOperand(0)->getType() == getOperand(1)->getType() && 643 "Both operands to ICmp instruction are not of the same type!"); 644 // Check that the operands are the right type 645 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 646 getOperand(0)->getType()->isPointerTy()) && 647 "Invalid operand types for ICmp instruction"); 648 } 649 650 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. 651 /// @returns the predicate that would be the result if the operand were 652 /// regarded as signed. 653 /// @brief Return the signed version of the predicate 654 Predicate getSignedPredicate() const { 655 return getSignedPredicate(getPredicate()); 656 } 657 658 /// This is a static version that you can use without an instruction. 659 /// @brief Return the signed version of the predicate. 660 static Predicate getSignedPredicate(Predicate pred); 661 662 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. 663 /// @returns the predicate that would be the result if the operand were 664 /// regarded as unsigned. 665 /// @brief Return the unsigned version of the predicate 666 Predicate getUnsignedPredicate() const { 667 return getUnsignedPredicate(getPredicate()); 668 } 669 670 /// This is a static version that you can use without an instruction. 671 /// @brief Return the unsigned version of the predicate. 672 static Predicate getUnsignedPredicate(Predicate pred); 673 674 /// isEquality - Return true if this predicate is either EQ or NE. This also 675 /// tests for commutativity. 676 static bool isEquality(Predicate P) { 677 return P == ICMP_EQ || P == ICMP_NE; 678 } 679 680 /// isEquality - Return true if this predicate is either EQ or NE. This also 681 /// tests for commutativity. 682 bool isEquality() const { 683 return isEquality(getPredicate()); 684 } 685 686 /// @returns true if the predicate of this ICmpInst is commutative 687 /// @brief Determine if this relation is commutative. 688 bool isCommutative() const { return isEquality(); } 689 690 /// isRelational - Return true if the predicate is relational (not EQ or NE). 691 /// 692 bool isRelational() const { 693 return !isEquality(); 694 } 695 696 /// isRelational - Return true if the predicate is relational (not EQ or NE). 697 /// 698 static bool isRelational(Predicate P) { 699 return !isEquality(P); 700 } 701 702 /// Initialize a set of values that all satisfy the predicate with C. 703 /// @brief Make a ConstantRange for a relation with a constant value. 704 static ConstantRange makeConstantRange(Predicate pred, const APInt &C); 705 706 /// Exchange the two operands to this instruction in such a way that it does 707 /// not modify the semantics of the instruction. The predicate value may be 708 /// changed to retain the same result if the predicate is order dependent 709 /// (e.g. ult). 710 /// @brief Swap operands and adjust predicate. 711 void swapOperands() { 712 setPredicate(getSwappedPredicate()); 713 Op<0>().swap(Op<1>()); 714 } 715 716 // Methods for support type inquiry through isa, cast, and dyn_cast: 717 static inline bool classof(const ICmpInst *) { return true; } 718 static inline bool classof(const Instruction *I) { 719 return I->getOpcode() == Instruction::ICmp; 720 } 721 static inline bool classof(const Value *V) { 722 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 723 } 724 725 }; 726 727 //===----------------------------------------------------------------------===// 728 // FCmpInst Class 729 //===----------------------------------------------------------------------===// 730 731 /// This instruction compares its operands according to the predicate given 732 /// to the constructor. It only operates on floating point values or packed 733 /// vectors of floating point values. The operands must be identical types. 734 /// @brief Represents a floating point comparison operator. 735 class FCmpInst: public CmpInst { 736 protected: 737 /// @brief Clone an identical FCmpInst 738 virtual FCmpInst *clone_impl() const; 739 public: 740 /// @brief Constructor with insert-before-instruction semantics. 741 FCmpInst( 742 Instruction *InsertBefore, ///< Where to insert 743 Predicate pred, ///< The predicate to use for the comparison 744 Value *LHS, ///< The left-hand-side of the expression 745 Value *RHS, ///< The right-hand-side of the expression 746 const Twine &NameStr = "" ///< Name of the instruction 747 ) : CmpInst(makeCmpResultType(LHS->getType()), 748 Instruction::FCmp, pred, LHS, RHS, NameStr, 749 InsertBefore) { 750 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 751 "Invalid FCmp predicate value"); 752 assert(getOperand(0)->getType() == getOperand(1)->getType() && 753 "Both operands to FCmp instruction are not of the same type!"); 754 // Check that the operands are the right type 755 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 756 "Invalid operand types for FCmp instruction"); 757 } 758 759 /// @brief Constructor with insert-at-end semantics. 760 FCmpInst( 761 BasicBlock &InsertAtEnd, ///< Block to insert into. 762 Predicate pred, ///< The predicate to use for the comparison 763 Value *LHS, ///< The left-hand-side of the expression 764 Value *RHS, ///< The right-hand-side of the expression 765 const Twine &NameStr = "" ///< Name of the instruction 766 ) : CmpInst(makeCmpResultType(LHS->getType()), 767 Instruction::FCmp, pred, LHS, RHS, NameStr, 768 &InsertAtEnd) { 769 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 770 "Invalid FCmp predicate value"); 771 assert(getOperand(0)->getType() == getOperand(1)->getType() && 772 "Both operands to FCmp instruction are not of the same type!"); 773 // Check that the operands are the right type 774 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 775 "Invalid operand types for FCmp instruction"); 776 } 777 778 /// @brief Constructor with no-insertion semantics 779 FCmpInst( 780 Predicate pred, ///< The predicate to use for the comparison 781 Value *LHS, ///< The left-hand-side of the expression 782 Value *RHS, ///< The right-hand-side of the expression 783 const Twine &NameStr = "" ///< Name of the instruction 784 ) : CmpInst(makeCmpResultType(LHS->getType()), 785 Instruction::FCmp, pred, LHS, RHS, NameStr) { 786 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 787 "Invalid FCmp predicate value"); 788 assert(getOperand(0)->getType() == getOperand(1)->getType() && 789 "Both operands to FCmp instruction are not of the same type!"); 790 // Check that the operands are the right type 791 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 792 "Invalid operand types for FCmp instruction"); 793 } 794 795 /// @returns true if the predicate of this instruction is EQ or NE. 796 /// @brief Determine if this is an equality predicate. 797 bool isEquality() const { 798 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE || 799 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE; 800 } 801 802 /// @returns true if the predicate of this instruction is commutative. 803 /// @brief Determine if this is a commutative predicate. 804 bool isCommutative() const { 805 return isEquality() || 806 getPredicate() == FCMP_FALSE || 807 getPredicate() == FCMP_TRUE || 808 getPredicate() == FCMP_ORD || 809 getPredicate() == FCMP_UNO; 810 } 811 812 /// @returns true if the predicate is relational (not EQ or NE). 813 /// @brief Determine if this a relational predicate. 814 bool isRelational() const { return !isEquality(); } 815 816 /// Exchange the two operands to this instruction in such a way that it does 817 /// not modify the semantics of the instruction. The predicate value may be 818 /// changed to retain the same result if the predicate is order dependent 819 /// (e.g. ult). 820 /// @brief Swap operands and adjust predicate. 821 void swapOperands() { 822 setPredicate(getSwappedPredicate()); 823 Op<0>().swap(Op<1>()); 824 } 825 826 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 827 static inline bool classof(const FCmpInst *) { return true; } 828 static inline bool classof(const Instruction *I) { 829 return I->getOpcode() == Instruction::FCmp; 830 } 831 static inline bool classof(const Value *V) { 832 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 833 } 834 }; 835 836 //===----------------------------------------------------------------------===// 837 /// CallInst - This class represents a function call, abstracting a target 838 /// machine's calling convention. This class uses low bit of the SubClassData 839 /// field to indicate whether or not this is a tail call. The rest of the bits 840 /// hold the calling convention of the call. 841 /// 842 class CallInst : public Instruction { 843 AttrListPtr AttributeList; ///< parameter attributes for call 844 CallInst(const CallInst &CI); 845 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr); 846 void init(Value *Func, const Twine &NameStr); 847 848 /// Construct a CallInst given a range of arguments. 849 /// @brief Construct a CallInst from a range of arguments 850 inline CallInst(Value *Func, ArrayRef<Value *> Args, 851 const Twine &NameStr, Instruction *InsertBefore); 852 853 /// Construct a CallInst given a range of arguments. 854 /// @brief Construct a CallInst from a range of arguments 855 inline CallInst(Value *Func, ArrayRef<Value *> Args, 856 const Twine &NameStr, BasicBlock *InsertAtEnd); 857 858 CallInst(Value *F, Value *Actual, const Twine &NameStr, 859 Instruction *InsertBefore); 860 CallInst(Value *F, Value *Actual, const Twine &NameStr, 861 BasicBlock *InsertAtEnd); 862 explicit CallInst(Value *F, const Twine &NameStr, 863 Instruction *InsertBefore); 864 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd); 865 protected: 866 virtual CallInst *clone_impl() const; 867 public: 868 static CallInst *Create(Value *Func, 869 ArrayRef<Value *> Args, 870 const Twine &NameStr = "", 871 Instruction *InsertBefore = 0) { 872 return new(unsigned(Args.size() + 1)) 873 CallInst(Func, Args, NameStr, InsertBefore); 874 } 875 static CallInst *Create(Value *Func, 876 ArrayRef<Value *> Args, 877 const Twine &NameStr, BasicBlock *InsertAtEnd) { 878 return new(unsigned(Args.size() + 1)) 879 CallInst(Func, Args, NameStr, InsertAtEnd); 880 } 881 static CallInst *Create(Value *F, const Twine &NameStr = "", 882 Instruction *InsertBefore = 0) { 883 return new(1) CallInst(F, NameStr, InsertBefore); 884 } 885 static CallInst *Create(Value *F, const Twine &NameStr, 886 BasicBlock *InsertAtEnd) { 887 return new(1) CallInst(F, NameStr, InsertAtEnd); 888 } 889 /// CreateMalloc - Generate the IR for a call to malloc: 890 /// 1. Compute the malloc call's argument as the specified type's size, 891 /// possibly multiplied by the array size if the array size is not 892 /// constant 1. 893 /// 2. Call malloc with that argument. 894 /// 3. Bitcast the result of the malloc call to the specified type. 895 static Instruction *CreateMalloc(Instruction *InsertBefore, 896 Type *IntPtrTy, Type *AllocTy, 897 Value *AllocSize, Value *ArraySize = 0, 898 Function* MallocF = 0, 899 const Twine &Name = ""); 900 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, 901 Type *IntPtrTy, Type *AllocTy, 902 Value *AllocSize, Value *ArraySize = 0, 903 Function* MallocF = 0, 904 const Twine &Name = ""); 905 /// CreateFree - Generate the IR for a call to the builtin free function. 906 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore); 907 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd); 908 909 ~CallInst(); 910 911 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; } 912 void setTailCall(bool isTC = true) { 913 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 914 unsigned(isTC)); 915 } 916 917 /// Provide fast operand accessors 918 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 919 920 /// getNumArgOperands - Return the number of call arguments. 921 /// 922 unsigned getNumArgOperands() const { return getNumOperands() - 1; } 923 924 /// getArgOperand/setArgOperand - Return/set the i-th call argument. 925 /// 926 Value *getArgOperand(unsigned i) const { return getOperand(i); } 927 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } 928 929 /// getCallingConv/setCallingConv - Get or set the calling convention of this 930 /// function call. 931 CallingConv::ID getCallingConv() const { 932 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1); 933 } 934 void setCallingConv(CallingConv::ID CC) { 935 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | 936 (static_cast<unsigned>(CC) << 1)); 937 } 938 939 /// getAttributes - Return the parameter attributes for this call. 940 /// 941 const AttrListPtr &getAttributes() const { return AttributeList; } 942 943 /// setAttributes - Set the parameter attributes for this call. 944 /// 945 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; } 946 947 /// addAttribute - adds the attribute to the list of attributes. 948 void addAttribute(unsigned i, Attributes attr); 949 950 /// removeAttribute - removes the attribute from the list of attributes. 951 void removeAttribute(unsigned i, Attributes attr); 952 953 /// @brief Determine whether the call or the callee has the given attribute. 954 bool paramHasAttr(unsigned i, Attributes attr) const; 955 956 /// @brief Extract the alignment for a call or parameter (0=unknown). 957 unsigned getParamAlignment(unsigned i) const { 958 return AttributeList.getParamAlignment(i); 959 } 960 961 /// @brief Return true if the call should not be inlined. 962 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); } 963 void setIsNoInline(bool Value = true) { 964 if (Value) addAttribute(~0, Attribute::NoInline); 965 else removeAttribute(~0, Attribute::NoInline); 966 } 967 968 /// @brief Determine if the call does not access memory. 969 bool doesNotAccessMemory() const { 970 return paramHasAttr(~0, Attribute::ReadNone); 971 } 972 void setDoesNotAccessMemory(bool NotAccessMemory = true) { 973 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone); 974 else removeAttribute(~0, Attribute::ReadNone); 975 } 976 977 /// @brief Determine if the call does not access or only reads memory. 978 bool onlyReadsMemory() const { 979 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly); 980 } 981 void setOnlyReadsMemory(bool OnlyReadsMemory = true) { 982 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly); 983 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); 984 } 985 986 /// @brief Determine if the call cannot return. 987 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); } 988 void setDoesNotReturn(bool DoesNotReturn = true) { 989 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn); 990 else removeAttribute(~0, Attribute::NoReturn); 991 } 992 993 /// @brief Determine if the call cannot unwind. 994 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); } 995 void setDoesNotThrow(bool DoesNotThrow = true) { 996 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind); 997 else removeAttribute(~0, Attribute::NoUnwind); 998 } 999 1000 /// @brief Determine if the call returns a structure through first 1001 /// pointer argument. 1002 bool hasStructRetAttr() const { 1003 // Be friendly and also check the callee. 1004 return paramHasAttr(1, Attribute::StructRet); 1005 } 1006 1007 /// @brief Determine if any call argument is an aggregate passed by value. 1008 bool hasByValArgument() const { 1009 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 1010 } 1011 1012 /// getCalledFunction - Return the function called, or null if this is an 1013 /// indirect function invocation. 1014 /// 1015 Function *getCalledFunction() const { 1016 return dyn_cast<Function>(Op<-1>()); 1017 } 1018 1019 /// getCalledValue - Get a pointer to the function that is invoked by this 1020 /// instruction. 1021 const Value *getCalledValue() const { return Op<-1>(); } 1022 Value *getCalledValue() { return Op<-1>(); } 1023 1024 /// setCalledFunction - Set the function called. 1025 void setCalledFunction(Value* Fn) { 1026 Op<-1>() = Fn; 1027 } 1028 1029 /// isInlineAsm - Check if this call is an inline asm statement. 1030 bool isInlineAsm() const { 1031 return isa<InlineAsm>(Op<-1>()); 1032 } 1033 1034 // Methods for support type inquiry through isa, cast, and dyn_cast: 1035 static inline bool classof(const CallInst *) { return true; } 1036 static inline bool classof(const Instruction *I) { 1037 return I->getOpcode() == Instruction::Call; 1038 } 1039 static inline bool classof(const Value *V) { 1040 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1041 } 1042 private: 1043 // Shadow Instruction::setInstructionSubclassData with a private forwarding 1044 // method so that subclasses cannot accidentally use it. 1045 void setInstructionSubclassData(unsigned short D) { 1046 Instruction::setInstructionSubclassData(D); 1047 } 1048 }; 1049 1050 template <> 1051 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> { 1052 }; 1053 1054 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args, 1055 const Twine &NameStr, BasicBlock *InsertAtEnd) 1056 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 1057 ->getElementType())->getReturnType(), 1058 Instruction::Call, 1059 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1), 1060 unsigned(Args.size() + 1), InsertAtEnd) { 1061 init(Func, Args, NameStr); 1062 } 1063 1064 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args, 1065 const Twine &NameStr, Instruction *InsertBefore) 1066 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 1067 ->getElementType())->getReturnType(), 1068 Instruction::Call, 1069 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1), 1070 unsigned(Args.size() + 1), InsertBefore) { 1071 init(Func, Args, NameStr); 1072 } 1073 1074 1075 // Note: if you get compile errors about private methods then 1076 // please update your code to use the high-level operand 1077 // interfaces. See line 943 above. 1078 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value) 1079 1080 //===----------------------------------------------------------------------===// 1081 // SelectInst Class 1082 //===----------------------------------------------------------------------===// 1083 1084 /// SelectInst - This class represents the LLVM 'select' instruction. 1085 /// 1086 class SelectInst : public Instruction { 1087 void init(Value *C, Value *S1, Value *S2) { 1088 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select"); 1089 Op<0>() = C; 1090 Op<1>() = S1; 1091 Op<2>() = S2; 1092 } 1093 1094 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1095 Instruction *InsertBefore) 1096 : Instruction(S1->getType(), Instruction::Select, 1097 &Op<0>(), 3, InsertBefore) { 1098 init(C, S1, S2); 1099 setName(NameStr); 1100 } 1101 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1102 BasicBlock *InsertAtEnd) 1103 : Instruction(S1->getType(), Instruction::Select, 1104 &Op<0>(), 3, InsertAtEnd) { 1105 init(C, S1, S2); 1106 setName(NameStr); 1107 } 1108 protected: 1109 virtual SelectInst *clone_impl() const; 1110 public: 1111 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1112 const Twine &NameStr = "", 1113 Instruction *InsertBefore = 0) { 1114 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); 1115 } 1116 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1117 const Twine &NameStr, 1118 BasicBlock *InsertAtEnd) { 1119 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); 1120 } 1121 1122 const Value *getCondition() const { return Op<0>(); } 1123 const Value *getTrueValue() const { return Op<1>(); } 1124 const Value *getFalseValue() const { return Op<2>(); } 1125 Value *getCondition() { return Op<0>(); } 1126 Value *getTrueValue() { return Op<1>(); } 1127 Value *getFalseValue() { return Op<2>(); } 1128 1129 /// areInvalidOperands - Return a string if the specified operands are invalid 1130 /// for a select operation, otherwise return null. 1131 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); 1132 1133 /// Transparently provide more efficient getOperand methods. 1134 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1135 1136 OtherOps getOpcode() const { 1137 return static_cast<OtherOps>(Instruction::getOpcode()); 1138 } 1139 1140 // Methods for support type inquiry through isa, cast, and dyn_cast: 1141 static inline bool classof(const SelectInst *) { return true; } 1142 static inline bool classof(const Instruction *I) { 1143 return I->getOpcode() == Instruction::Select; 1144 } 1145 static inline bool classof(const Value *V) { 1146 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1147 } 1148 }; 1149 1150 template <> 1151 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { 1152 }; 1153 1154 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value) 1155 1156 //===----------------------------------------------------------------------===// 1157 // VAArgInst Class 1158 //===----------------------------------------------------------------------===// 1159 1160 /// VAArgInst - This class represents the va_arg llvm instruction, which returns 1161 /// an argument of the specified type given a va_list and increments that list 1162 /// 1163 class VAArgInst : public UnaryInstruction { 1164 protected: 1165 virtual VAArgInst *clone_impl() const; 1166 1167 public: 1168 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", 1169 Instruction *InsertBefore = 0) 1170 : UnaryInstruction(Ty, VAArg, List, InsertBefore) { 1171 setName(NameStr); 1172 } 1173 VAArgInst(Value *List, Type *Ty, const Twine &NameStr, 1174 BasicBlock *InsertAtEnd) 1175 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { 1176 setName(NameStr); 1177 } 1178 1179 Value *getPointerOperand() { return getOperand(0); } 1180 const Value *getPointerOperand() const { return getOperand(0); } 1181 static unsigned getPointerOperandIndex() { return 0U; } 1182 1183 // Methods for support type inquiry through isa, cast, and dyn_cast: 1184 static inline bool classof(const VAArgInst *) { return true; } 1185 static inline bool classof(const Instruction *I) { 1186 return I->getOpcode() == VAArg; 1187 } 1188 static inline bool classof(const Value *V) { 1189 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1190 } 1191 }; 1192 1193 //===----------------------------------------------------------------------===// 1194 // ExtractElementInst Class 1195 //===----------------------------------------------------------------------===// 1196 1197 /// ExtractElementInst - This instruction extracts a single (scalar) 1198 /// element from a VectorType value 1199 /// 1200 class ExtractElementInst : public Instruction { 1201 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", 1202 Instruction *InsertBefore = 0); 1203 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, 1204 BasicBlock *InsertAtEnd); 1205 protected: 1206 virtual ExtractElementInst *clone_impl() const; 1207 1208 public: 1209 static ExtractElementInst *Create(Value *Vec, Value *Idx, 1210 const Twine &NameStr = "", 1211 Instruction *InsertBefore = 0) { 1212 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); 1213 } 1214 static ExtractElementInst *Create(Value *Vec, Value *Idx, 1215 const Twine &NameStr, 1216 BasicBlock *InsertAtEnd) { 1217 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); 1218 } 1219 1220 /// isValidOperands - Return true if an extractelement instruction can be 1221 /// formed with the specified operands. 1222 static bool isValidOperands(const Value *Vec, const Value *Idx); 1223 1224 Value *getVectorOperand() { return Op<0>(); } 1225 Value *getIndexOperand() { return Op<1>(); } 1226 const Value *getVectorOperand() const { return Op<0>(); } 1227 const Value *getIndexOperand() const { return Op<1>(); } 1228 1229 VectorType *getVectorOperandType() const { 1230 return reinterpret_cast<VectorType*>(getVectorOperand()->getType()); 1231 } 1232 1233 1234 /// Transparently provide more efficient getOperand methods. 1235 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1236 1237 // Methods for support type inquiry through isa, cast, and dyn_cast: 1238 static inline bool classof(const ExtractElementInst *) { return true; } 1239 static inline bool classof(const Instruction *I) { 1240 return I->getOpcode() == Instruction::ExtractElement; 1241 } 1242 static inline bool classof(const Value *V) { 1243 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1244 } 1245 }; 1246 1247 template <> 1248 struct OperandTraits<ExtractElementInst> : 1249 public FixedNumOperandTraits<ExtractElementInst, 2> { 1250 }; 1251 1252 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value) 1253 1254 //===----------------------------------------------------------------------===// 1255 // InsertElementInst Class 1256 //===----------------------------------------------------------------------===// 1257 1258 /// InsertElementInst - This instruction inserts a single (scalar) 1259 /// element into a VectorType value 1260 /// 1261 class InsertElementInst : public Instruction { 1262 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, 1263 const Twine &NameStr = "", 1264 Instruction *InsertBefore = 0); 1265 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, 1266 const Twine &NameStr, BasicBlock *InsertAtEnd); 1267 protected: 1268 virtual InsertElementInst *clone_impl() const; 1269 1270 public: 1271 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 1272 const Twine &NameStr = "", 1273 Instruction *InsertBefore = 0) { 1274 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); 1275 } 1276 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 1277 const Twine &NameStr, 1278 BasicBlock *InsertAtEnd) { 1279 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); 1280 } 1281 1282 /// isValidOperands - Return true if an insertelement instruction can be 1283 /// formed with the specified operands. 1284 static bool isValidOperands(const Value *Vec, const Value *NewElt, 1285 const Value *Idx); 1286 1287 /// getType - Overload to return most specific vector type. 1288 /// 1289 VectorType *getType() const { 1290 return reinterpret_cast<VectorType*>(Instruction::getType()); 1291 } 1292 1293 /// Transparently provide more efficient getOperand methods. 1294 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1295 1296 // Methods for support type inquiry through isa, cast, and dyn_cast: 1297 static inline bool classof(const InsertElementInst *) { return true; } 1298 static inline bool classof(const Instruction *I) { 1299 return I->getOpcode() == Instruction::InsertElement; 1300 } 1301 static inline bool classof(const Value *V) { 1302 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1303 } 1304 }; 1305 1306 template <> 1307 struct OperandTraits<InsertElementInst> : 1308 public FixedNumOperandTraits<InsertElementInst, 3> { 1309 }; 1310 1311 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value) 1312 1313 //===----------------------------------------------------------------------===// 1314 // ShuffleVectorInst Class 1315 //===----------------------------------------------------------------------===// 1316 1317 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two 1318 /// input vectors. 1319 /// 1320 class ShuffleVectorInst : public Instruction { 1321 protected: 1322 virtual ShuffleVectorInst *clone_impl() const; 1323 1324 public: 1325 // allocate space for exactly three operands 1326 void *operator new(size_t s) { 1327 return User::operator new(s, 3); 1328 } 1329 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1330 const Twine &NameStr = "", 1331 Instruction *InsertBefor = 0); 1332 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1333 const Twine &NameStr, BasicBlock *InsertAtEnd); 1334 1335 /// isValidOperands - Return true if a shufflevector instruction can be 1336 /// formed with the specified operands. 1337 static bool isValidOperands(const Value *V1, const Value *V2, 1338 const Value *Mask); 1339 1340 /// getType - Overload to return most specific vector type. 1341 /// 1342 VectorType *getType() const { 1343 return reinterpret_cast<VectorType*>(Instruction::getType()); 1344 } 1345 1346 /// Transparently provide more efficient getOperand methods. 1347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1348 1349 /// getMaskValue - Return the index from the shuffle mask for the specified 1350 /// output result. This is either -1 if the element is undef or a number less 1351 /// than 2*numelements. 1352 int getMaskValue(unsigned i) const; 1353 1354 // Methods for support type inquiry through isa, cast, and dyn_cast: 1355 static inline bool classof(const ShuffleVectorInst *) { return true; } 1356 static inline bool classof(const Instruction *I) { 1357 return I->getOpcode() == Instruction::ShuffleVector; 1358 } 1359 static inline bool classof(const Value *V) { 1360 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1361 } 1362 }; 1363 1364 template <> 1365 struct OperandTraits<ShuffleVectorInst> : 1366 public FixedNumOperandTraits<ShuffleVectorInst, 3> { 1367 }; 1368 1369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value) 1370 1371 //===----------------------------------------------------------------------===// 1372 // ExtractValueInst Class 1373 //===----------------------------------------------------------------------===// 1374 1375 /// ExtractValueInst - This instruction extracts a struct member or array 1376 /// element value from an aggregate value. 1377 /// 1378 class ExtractValueInst : public UnaryInstruction { 1379 SmallVector<unsigned, 4> Indices; 1380 1381 ExtractValueInst(const ExtractValueInst &EVI); 1382 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); 1383 1384 /// Constructors - Create a extractvalue instruction with a base aggregate 1385 /// value and a list of indices. The first ctor can optionally insert before 1386 /// an existing instruction, the second appends the new instruction to the 1387 /// specified BasicBlock. 1388 inline ExtractValueInst(Value *Agg, 1389 ArrayRef<unsigned> Idxs, 1390 const Twine &NameStr, 1391 Instruction *InsertBefore); 1392 inline ExtractValueInst(Value *Agg, 1393 ArrayRef<unsigned> Idxs, 1394 const Twine &NameStr, BasicBlock *InsertAtEnd); 1395 1396 // allocate space for exactly one operand 1397 void *operator new(size_t s) { 1398 return User::operator new(s, 1); 1399 } 1400 protected: 1401 virtual ExtractValueInst *clone_impl() const; 1402 1403 public: 1404 static ExtractValueInst *Create(Value *Agg, 1405 ArrayRef<unsigned> Idxs, 1406 const Twine &NameStr = "", 1407 Instruction *InsertBefore = 0) { 1408 return new 1409 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); 1410 } 1411 static ExtractValueInst *Create(Value *Agg, 1412 ArrayRef<unsigned> Idxs, 1413 const Twine &NameStr, 1414 BasicBlock *InsertAtEnd) { 1415 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); 1416 } 1417 1418 /// getIndexedType - Returns the type of the element that would be extracted 1419 /// with an extractvalue instruction with the specified parameters. 1420 /// 1421 /// Null is returned if the indices are invalid for the specified type. 1422 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); 1423 1424 typedef const unsigned* idx_iterator; 1425 inline idx_iterator idx_begin() const { return Indices.begin(); } 1426 inline idx_iterator idx_end() const { return Indices.end(); } 1427 1428 Value *getAggregateOperand() { 1429 return getOperand(0); 1430 } 1431 const Value *getAggregateOperand() const { 1432 return getOperand(0); 1433 } 1434 static unsigned getAggregateOperandIndex() { 1435 return 0U; // get index for modifying correct operand 1436 } 1437 1438 ArrayRef<unsigned> getIndices() const { 1439 return Indices; 1440 } 1441 1442 unsigned getNumIndices() const { 1443 return (unsigned)Indices.size(); 1444 } 1445 1446 bool hasIndices() const { 1447 return true; 1448 } 1449 1450 // Methods for support type inquiry through isa, cast, and dyn_cast: 1451 static inline bool classof(const ExtractValueInst *) { return true; } 1452 static inline bool classof(const Instruction *I) { 1453 return I->getOpcode() == Instruction::ExtractValue; 1454 } 1455 static inline bool classof(const Value *V) { 1456 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1457 } 1458 }; 1459 1460 ExtractValueInst::ExtractValueInst(Value *Agg, 1461 ArrayRef<unsigned> Idxs, 1462 const Twine &NameStr, 1463 Instruction *InsertBefore) 1464 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 1465 ExtractValue, Agg, InsertBefore) { 1466 init(Idxs, NameStr); 1467 } 1468 ExtractValueInst::ExtractValueInst(Value *Agg, 1469 ArrayRef<unsigned> Idxs, 1470 const Twine &NameStr, 1471 BasicBlock *InsertAtEnd) 1472 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 1473 ExtractValue, Agg, InsertAtEnd) { 1474 init(Idxs, NameStr); 1475 } 1476 1477 1478 //===----------------------------------------------------------------------===// 1479 // InsertValueInst Class 1480 //===----------------------------------------------------------------------===// 1481 1482 /// InsertValueInst - This instruction inserts a struct field of array element 1483 /// value into an aggregate value. 1484 /// 1485 class InsertValueInst : public Instruction { 1486 SmallVector<unsigned, 4> Indices; 1487 1488 void *operator new(size_t, unsigned); // Do not implement 1489 InsertValueInst(const InsertValueInst &IVI); 1490 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 1491 const Twine &NameStr); 1492 1493 /// Constructors - Create a insertvalue instruction with a base aggregate 1494 /// value, a value to insert, and a list of indices. The first ctor can 1495 /// optionally insert before an existing instruction, the second appends 1496 /// the new instruction to the specified BasicBlock. 1497 inline InsertValueInst(Value *Agg, Value *Val, 1498 ArrayRef<unsigned> Idxs, 1499 const Twine &NameStr, 1500 Instruction *InsertBefore); 1501 inline InsertValueInst(Value *Agg, Value *Val, 1502 ArrayRef<unsigned> Idxs, 1503 const Twine &NameStr, BasicBlock *InsertAtEnd); 1504 1505 /// Constructors - These two constructors are convenience methods because one 1506 /// and two index insertvalue instructions are so common. 1507 InsertValueInst(Value *Agg, Value *Val, 1508 unsigned Idx, const Twine &NameStr = "", 1509 Instruction *InsertBefore = 0); 1510 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, 1511 const Twine &NameStr, BasicBlock *InsertAtEnd); 1512 protected: 1513 virtual InsertValueInst *clone_impl() const; 1514 public: 1515 // allocate space for exactly two operands 1516 void *operator new(size_t s) { 1517 return User::operator new(s, 2); 1518 } 1519 1520 static InsertValueInst *Create(Value *Agg, Value *Val, 1521 ArrayRef<unsigned> Idxs, 1522 const Twine &NameStr = "", 1523 Instruction *InsertBefore = 0) { 1524 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); 1525 } 1526 static InsertValueInst *Create(Value *Agg, Value *Val, 1527 ArrayRef<unsigned> Idxs, 1528 const Twine &NameStr, 1529 BasicBlock *InsertAtEnd) { 1530 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); 1531 } 1532 1533 /// Transparently provide more efficient getOperand methods. 1534 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1535 1536 typedef const unsigned* idx_iterator; 1537 inline idx_iterator idx_begin() const { return Indices.begin(); } 1538 inline idx_iterator idx_end() const { return Indices.end(); } 1539 1540 Value *getAggregateOperand() { 1541 return getOperand(0); 1542 } 1543 const Value *getAggregateOperand() const { 1544 return getOperand(0); 1545 } 1546 static unsigned getAggregateOperandIndex() { 1547 return 0U; // get index for modifying correct operand 1548 } 1549 1550 Value *getInsertedValueOperand() { 1551 return getOperand(1); 1552 } 1553 const Value *getInsertedValueOperand() const { 1554 return getOperand(1); 1555 } 1556 static unsigned getInsertedValueOperandIndex() { 1557 return 1U; // get index for modifying correct operand 1558 } 1559 1560 ArrayRef<unsigned> getIndices() const { 1561 return Indices; 1562 } 1563 1564 unsigned getNumIndices() const { 1565 return (unsigned)Indices.size(); 1566 } 1567 1568 bool hasIndices() const { 1569 return true; 1570 } 1571 1572 // Methods for support type inquiry through isa, cast, and dyn_cast: 1573 static inline bool classof(const InsertValueInst *) { return true; } 1574 static inline bool classof(const Instruction *I) { 1575 return I->getOpcode() == Instruction::InsertValue; 1576 } 1577 static inline bool classof(const Value *V) { 1578 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1579 } 1580 }; 1581 1582 template <> 1583 struct OperandTraits<InsertValueInst> : 1584 public FixedNumOperandTraits<InsertValueInst, 2> { 1585 }; 1586 1587 InsertValueInst::InsertValueInst(Value *Agg, 1588 Value *Val, 1589 ArrayRef<unsigned> Idxs, 1590 const Twine &NameStr, 1591 Instruction *InsertBefore) 1592 : Instruction(Agg->getType(), InsertValue, 1593 OperandTraits<InsertValueInst>::op_begin(this), 1594 2, InsertBefore) { 1595 init(Agg, Val, Idxs, NameStr); 1596 } 1597 InsertValueInst::InsertValueInst(Value *Agg, 1598 Value *Val, 1599 ArrayRef<unsigned> Idxs, 1600 const Twine &NameStr, 1601 BasicBlock *InsertAtEnd) 1602 : Instruction(Agg->getType(), InsertValue, 1603 OperandTraits<InsertValueInst>::op_begin(this), 1604 2, InsertAtEnd) { 1605 init(Agg, Val, Idxs, NameStr); 1606 } 1607 1608 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value) 1609 1610 //===----------------------------------------------------------------------===// 1611 // PHINode Class 1612 //===----------------------------------------------------------------------===// 1613 1614 // PHINode - The PHINode class is used to represent the magical mystical PHI 1615 // node, that can not exist in nature, but can be synthesized in a computer 1616 // scientist's overactive imagination. 1617 // 1618 class PHINode : public Instruction { 1619 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 1620 /// ReservedSpace - The number of operands actually allocated. NumOperands is 1621 /// the number actually in use. 1622 unsigned ReservedSpace; 1623 PHINode(const PHINode &PN); 1624 // allocate space for exactly zero operands 1625 void *operator new(size_t s) { 1626 return User::operator new(s, 0); 1627 } 1628 explicit PHINode(Type *Ty, unsigned NumReservedValues, 1629 const Twine &NameStr = "", Instruction *InsertBefore = 0) 1630 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore), 1631 ReservedSpace(NumReservedValues) { 1632 setName(NameStr); 1633 OperandList = allocHungoffUses(ReservedSpace); 1634 } 1635 1636 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, 1637 BasicBlock *InsertAtEnd) 1638 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd), 1639 ReservedSpace(NumReservedValues) { 1640 setName(NameStr); 1641 OperandList = allocHungoffUses(ReservedSpace); 1642 } 1643 protected: 1644 // allocHungoffUses - this is more complicated than the generic 1645 // User::allocHungoffUses, because we have to allocate Uses for the incoming 1646 // values and pointers to the incoming blocks, all in one allocation. 1647 Use *allocHungoffUses(unsigned) const; 1648 1649 virtual PHINode *clone_impl() const; 1650 public: 1651 /// Constructors - NumReservedValues is a hint for the number of incoming 1652 /// edges that this phi node will have (use 0 if you really have no idea). 1653 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 1654 const Twine &NameStr = "", 1655 Instruction *InsertBefore = 0) { 1656 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); 1657 } 1658 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 1659 const Twine &NameStr, BasicBlock *InsertAtEnd) { 1660 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); 1661 } 1662 ~PHINode(); 1663 1664 /// Provide fast operand accessors 1665 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1666 1667 // Block iterator interface. This provides access to the list of incoming 1668 // basic blocks, which parallels the list of incoming values. 1669 1670 typedef BasicBlock **block_iterator; 1671 typedef BasicBlock * const *const_block_iterator; 1672 1673 block_iterator block_begin() { 1674 Use::UserRef *ref = 1675 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace); 1676 return reinterpret_cast<block_iterator>(ref + 1); 1677 } 1678 1679 const_block_iterator block_begin() const { 1680 const Use::UserRef *ref = 1681 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace); 1682 return reinterpret_cast<const_block_iterator>(ref + 1); 1683 } 1684 1685 block_iterator block_end() { 1686 return block_begin() + getNumOperands(); 1687 } 1688 1689 const_block_iterator block_end() const { 1690 return block_begin() + getNumOperands(); 1691 } 1692 1693 /// getNumIncomingValues - Return the number of incoming edges 1694 /// 1695 unsigned getNumIncomingValues() const { return getNumOperands(); } 1696 1697 /// getIncomingValue - Return incoming value number x 1698 /// 1699 Value *getIncomingValue(unsigned i) const { 1700 return getOperand(i); 1701 } 1702 void setIncomingValue(unsigned i, Value *V) { 1703 setOperand(i, V); 1704 } 1705 static unsigned getOperandNumForIncomingValue(unsigned i) { 1706 return i; 1707 } 1708 static unsigned getIncomingValueNumForOperand(unsigned i) { 1709 return i; 1710 } 1711 1712 /// getIncomingBlock - Return incoming basic block number @p i. 1713 /// 1714 BasicBlock *getIncomingBlock(unsigned i) const { 1715 return block_begin()[i]; 1716 } 1717 1718 /// getIncomingBlock - Return incoming basic block corresponding 1719 /// to an operand of the PHI. 1720 /// 1721 BasicBlock *getIncomingBlock(const Use &U) const { 1722 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?"); 1723 return getIncomingBlock(unsigned(&U - op_begin())); 1724 } 1725 1726 /// getIncomingBlock - Return incoming basic block corresponding 1727 /// to value use iterator. 1728 /// 1729 template <typename U> 1730 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const { 1731 return getIncomingBlock(I.getUse()); 1732 } 1733 1734 void setIncomingBlock(unsigned i, BasicBlock *BB) { 1735 block_begin()[i] = BB; 1736 } 1737 1738 /// addIncoming - Add an incoming value to the end of the PHI list 1739 /// 1740 void addIncoming(Value *V, BasicBlock *BB) { 1741 assert(V && "PHI node got a null value!"); 1742 assert(BB && "PHI node got a null basic block!"); 1743 assert(getType() == V->getType() && 1744 "All operands to PHI node must be the same type as the PHI node!"); 1745 if (NumOperands == ReservedSpace) 1746 growOperands(); // Get more space! 1747 // Initialize some new operands. 1748 ++NumOperands; 1749 setIncomingValue(NumOperands - 1, V); 1750 setIncomingBlock(NumOperands - 1, BB); 1751 } 1752 1753 /// removeIncomingValue - Remove an incoming value. This is useful if a 1754 /// predecessor basic block is deleted. The value removed is returned. 1755 /// 1756 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty 1757 /// is true), the PHI node is destroyed and any uses of it are replaced with 1758 /// dummy values. The only time there should be zero incoming values to a PHI 1759 /// node is when the block is dead, so this strategy is sound. 1760 /// 1761 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); 1762 1763 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { 1764 int Idx = getBasicBlockIndex(BB); 1765 assert(Idx >= 0 && "Invalid basic block argument to remove!"); 1766 return removeIncomingValue(Idx, DeletePHIIfEmpty); 1767 } 1768 1769 /// getBasicBlockIndex - Return the first index of the specified basic 1770 /// block in the value list for this PHI. Returns -1 if no instance. 1771 /// 1772 int getBasicBlockIndex(const BasicBlock *BB) const { 1773 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 1774 if (block_begin()[i] == BB) 1775 return i; 1776 return -1; 1777 } 1778 1779 Value *getIncomingValueForBlock(const BasicBlock *BB) const { 1780 int Idx = getBasicBlockIndex(BB); 1781 assert(Idx >= 0 && "Invalid basic block argument!"); 1782 return getIncomingValue(Idx); 1783 } 1784 1785 /// hasConstantValue - If the specified PHI node always merges together the 1786 /// same value, return the value, otherwise return null. 1787 Value *hasConstantValue() const; 1788 1789 /// Methods for support type inquiry through isa, cast, and dyn_cast: 1790 static inline bool classof(const PHINode *) { return true; } 1791 static inline bool classof(const Instruction *I) { 1792 return I->getOpcode() == Instruction::PHI; 1793 } 1794 static inline bool classof(const Value *V) { 1795 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1796 } 1797 private: 1798 void growOperands(); 1799 }; 1800 1801 template <> 1802 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { 1803 }; 1804 1805 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value) 1806 1807 1808 //===----------------------------------------------------------------------===// 1809 // ReturnInst Class 1810 //===----------------------------------------------------------------------===// 1811 1812 //===--------------------------------------------------------------------------- 1813 /// ReturnInst - Return a value (possibly void), from a function. Execution 1814 /// does not continue in this function any longer. 1815 /// 1816 class ReturnInst : public TerminatorInst { 1817 ReturnInst(const ReturnInst &RI); 1818 1819 private: 1820 // ReturnInst constructors: 1821 // ReturnInst() - 'ret void' instruction 1822 // ReturnInst( null) - 'ret void' instruction 1823 // ReturnInst(Value* X) - 'ret X' instruction 1824 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I 1825 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I 1826 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B 1827 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B 1828 // 1829 // NOTE: If the Value* passed is of type void then the constructor behaves as 1830 // if it was passed NULL. 1831 explicit ReturnInst(LLVMContext &C, Value *retVal = 0, 1832 Instruction *InsertBefore = 0); 1833 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); 1834 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); 1835 protected: 1836 virtual ReturnInst *clone_impl() const; 1837 public: 1838 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0, 1839 Instruction *InsertBefore = 0) { 1840 return new(!!retVal) ReturnInst(C, retVal, InsertBefore); 1841 } 1842 static ReturnInst* Create(LLVMContext &C, Value *retVal, 1843 BasicBlock *InsertAtEnd) { 1844 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); 1845 } 1846 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { 1847 return new(0) ReturnInst(C, InsertAtEnd); 1848 } 1849 virtual ~ReturnInst(); 1850 1851 /// Provide fast operand accessors 1852 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1853 1854 /// Convenience accessor. Returns null if there is no return value. 1855 Value *getReturnValue() const { 1856 return getNumOperands() != 0 ? getOperand(0) : 0; 1857 } 1858 1859 unsigned getNumSuccessors() const { return 0; } 1860 1861 // Methods for support type inquiry through isa, cast, and dyn_cast: 1862 static inline bool classof(const ReturnInst *) { return true; } 1863 static inline bool classof(const Instruction *I) { 1864 return (I->getOpcode() == Instruction::Ret); 1865 } 1866 static inline bool classof(const Value *V) { 1867 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1868 } 1869 private: 1870 virtual BasicBlock *getSuccessorV(unsigned idx) const; 1871 virtual unsigned getNumSuccessorsV() const; 1872 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 1873 }; 1874 1875 template <> 1876 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { 1877 }; 1878 1879 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value) 1880 1881 //===----------------------------------------------------------------------===// 1882 // BranchInst Class 1883 //===----------------------------------------------------------------------===// 1884 1885 //===--------------------------------------------------------------------------- 1886 /// BranchInst - Conditional or Unconditional Branch instruction. 1887 /// 1888 class BranchInst : public TerminatorInst { 1889 /// Ops list - Branches are strange. The operands are ordered: 1890 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because 1891 /// they don't have to check for cond/uncond branchness. These are mostly 1892 /// accessed relative from op_end(). 1893 BranchInst(const BranchInst &BI); 1894 void AssertOK(); 1895 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): 1896 // BranchInst(BB *B) - 'br B' 1897 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' 1898 // BranchInst(BB* B, Inst *I) - 'br B' insert before I 1899 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I 1900 // BranchInst(BB* B, BB *I) - 'br B' insert at end 1901 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end 1902 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0); 1903 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1904 Instruction *InsertBefore = 0); 1905 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); 1906 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1907 BasicBlock *InsertAtEnd); 1908 protected: 1909 virtual BranchInst *clone_impl() const; 1910 public: 1911 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) { 1912 return new(1) BranchInst(IfTrue, InsertBefore); 1913 } 1914 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 1915 Value *Cond, Instruction *InsertBefore = 0) { 1916 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); 1917 } 1918 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { 1919 return new(1) BranchInst(IfTrue, InsertAtEnd); 1920 } 1921 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 1922 Value *Cond, BasicBlock *InsertAtEnd) { 1923 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); 1924 } 1925 1926 /// Transparently provide more efficient getOperand methods. 1927 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1928 1929 bool isUnconditional() const { return getNumOperands() == 1; } 1930 bool isConditional() const { return getNumOperands() == 3; } 1931 1932 Value *getCondition() const { 1933 assert(isConditional() && "Cannot get condition of an uncond branch!"); 1934 return Op<-3>(); 1935 } 1936 1937 void setCondition(Value *V) { 1938 assert(isConditional() && "Cannot set condition of unconditional branch!"); 1939 Op<-3>() = V; 1940 } 1941 1942 unsigned getNumSuccessors() const { return 1+isConditional(); } 1943 1944 BasicBlock *getSuccessor(unsigned i) const { 1945 assert(i < getNumSuccessors() && "Successor # out of range for Branch!"); 1946 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); 1947 } 1948 1949 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 1950 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!"); 1951 *(&Op<-1>() - idx) = (Value*)NewSucc; 1952 } 1953 1954 // Methods for support type inquiry through isa, cast, and dyn_cast: 1955 static inline bool classof(const BranchInst *) { return true; } 1956 static inline bool classof(const Instruction *I) { 1957 return (I->getOpcode() == Instruction::Br); 1958 } 1959 static inline bool classof(const Value *V) { 1960 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1961 } 1962 private: 1963 virtual BasicBlock *getSuccessorV(unsigned idx) const; 1964 virtual unsigned getNumSuccessorsV() const; 1965 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 1966 }; 1967 1968 template <> 1969 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { 1970 }; 1971 1972 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value) 1973 1974 //===----------------------------------------------------------------------===// 1975 // SwitchInst Class 1976 //===----------------------------------------------------------------------===// 1977 1978 //===--------------------------------------------------------------------------- 1979 /// SwitchInst - Multiway switch 1980 /// 1981 class SwitchInst : public TerminatorInst { 1982 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 1983 unsigned ReservedSpace; 1984 // Operand[0] = Value to switch on 1985 // Operand[1] = Default basic block destination 1986 // Operand[2n ] = Value to match 1987 // Operand[2n+1] = BasicBlock to go to on match 1988 SwitchInst(const SwitchInst &SI); 1989 void init(Value *Value, BasicBlock *Default, unsigned NumReserved); 1990 void growOperands(); 1991 // allocate space for exactly zero operands 1992 void *operator new(size_t s) { 1993 return User::operator new(s, 0); 1994 } 1995 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 1996 /// switch on and a default destination. The number of additional cases can 1997 /// be specified here to make memory allocation more efficient. This 1998 /// constructor can also autoinsert before another instruction. 1999 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 2000 Instruction *InsertBefore); 2001 2002 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 2003 /// switch on and a default destination. The number of additional cases can 2004 /// be specified here to make memory allocation more efficient. This 2005 /// constructor also autoinserts at the end of the specified BasicBlock. 2006 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 2007 BasicBlock *InsertAtEnd); 2008 protected: 2009 virtual SwitchInst *clone_impl() const; 2010 public: 2011 static SwitchInst *Create(Value *Value, BasicBlock *Default, 2012 unsigned NumCases, Instruction *InsertBefore = 0) { 2013 return new SwitchInst(Value, Default, NumCases, InsertBefore); 2014 } 2015 static SwitchInst *Create(Value *Value, BasicBlock *Default, 2016 unsigned NumCases, BasicBlock *InsertAtEnd) { 2017 return new SwitchInst(Value, Default, NumCases, InsertAtEnd); 2018 } 2019 ~SwitchInst(); 2020 2021 /// Provide fast operand accessors 2022 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2023 2024 // Accessor Methods for Switch stmt 2025 Value *getCondition() const { return getOperand(0); } 2026 void setCondition(Value *V) { setOperand(0, V); } 2027 2028 BasicBlock *getDefaultDest() const { 2029 return cast<BasicBlock>(getOperand(1)); 2030 } 2031 2032 /// getNumCases - return the number of 'cases' in this switch instruction. 2033 /// Note that case #0 is always the default case. 2034 unsigned getNumCases() const { 2035 return getNumOperands()/2; 2036 } 2037 2038 /// getCaseValue - Return the specified case value. Note that case #0, the 2039 /// default destination, does not have a case value. 2040 ConstantInt *getCaseValue(unsigned i) { 2041 assert(i && i < getNumCases() && "Illegal case value to get!"); 2042 return getSuccessorValue(i); 2043 } 2044 2045 /// getCaseValue - Return the specified case value. Note that case #0, the 2046 /// default destination, does not have a case value. 2047 const ConstantInt *getCaseValue(unsigned i) const { 2048 assert(i && i < getNumCases() && "Illegal case value to get!"); 2049 return getSuccessorValue(i); 2050 } 2051 2052 /// findCaseValue - Search all of the case values for the specified constant. 2053 /// If it is explicitly handled, return the case number of it, otherwise 2054 /// return 0 to indicate that it is handled by the default handler. 2055 unsigned findCaseValue(const ConstantInt *C) const { 2056 for (unsigned i = 1, e = getNumCases(); i != e; ++i) 2057 if (getCaseValue(i) == C) 2058 return i; 2059 return 0; 2060 } 2061 2062 /// findCaseDest - Finds the unique case value for a given successor. Returns 2063 /// null if the successor is not found, not unique, or is the default case. 2064 ConstantInt *findCaseDest(BasicBlock *BB) { 2065 if (BB == getDefaultDest()) return NULL; 2066 2067 ConstantInt *CI = NULL; 2068 for (unsigned i = 1, e = getNumCases(); i != e; ++i) { 2069 if (getSuccessor(i) == BB) { 2070 if (CI) return NULL; // Multiple cases lead to BB. 2071 else CI = getCaseValue(i); 2072 } 2073 } 2074 return CI; 2075 } 2076 2077 /// addCase - Add an entry to the switch instruction... 2078 /// 2079 void addCase(ConstantInt *OnVal, BasicBlock *Dest); 2080 2081 /// removeCase - This method removes the specified successor from the switch 2082 /// instruction. Note that this cannot be used to remove the default 2083 /// destination (successor #0). Also note that this operation may reorder the 2084 /// remaining cases at index idx and above. 2085 /// 2086 void removeCase(unsigned idx); 2087 2088 unsigned getNumSuccessors() const { return getNumOperands()/2; } 2089 BasicBlock *getSuccessor(unsigned idx) const { 2090 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!"); 2091 return cast<BasicBlock>(getOperand(idx*2+1)); 2092 } 2093 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 2094 assert(idx < getNumSuccessors() && "Successor # out of range for switch!"); 2095 setOperand(idx*2+1, (Value*)NewSucc); 2096 } 2097 2098 // getSuccessorValue - Return the value associated with the specified 2099 // successor. 2100 ConstantInt *getSuccessorValue(unsigned idx) const { 2101 assert(idx < getNumSuccessors() && "Successor # out of range!"); 2102 return reinterpret_cast<ConstantInt*>(getOperand(idx*2)); 2103 } 2104 2105 // Methods for support type inquiry through isa, cast, and dyn_cast: 2106 static inline bool classof(const SwitchInst *) { return true; } 2107 static inline bool classof(const Instruction *I) { 2108 return I->getOpcode() == Instruction::Switch; 2109 } 2110 static inline bool classof(const Value *V) { 2111 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2112 } 2113 private: 2114 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2115 virtual unsigned getNumSuccessorsV() const; 2116 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2117 }; 2118 2119 template <> 2120 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { 2121 }; 2122 2123 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value) 2124 2125 2126 //===----------------------------------------------------------------------===// 2127 // IndirectBrInst Class 2128 //===----------------------------------------------------------------------===// 2129 2130 //===--------------------------------------------------------------------------- 2131 /// IndirectBrInst - Indirect Branch Instruction. 2132 /// 2133 class IndirectBrInst : public TerminatorInst { 2134 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 2135 unsigned ReservedSpace; 2136 // Operand[0] = Value to switch on 2137 // Operand[1] = Default basic block destination 2138 // Operand[2n ] = Value to match 2139 // Operand[2n+1] = BasicBlock to go to on match 2140 IndirectBrInst(const IndirectBrInst &IBI); 2141 void init(Value *Address, unsigned NumDests); 2142 void growOperands(); 2143 // allocate space for exactly zero operands 2144 void *operator new(size_t s) { 2145 return User::operator new(s, 0); 2146 } 2147 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 2148 /// Address to jump to. The number of expected destinations can be specified 2149 /// here to make memory allocation more efficient. This constructor can also 2150 /// autoinsert before another instruction. 2151 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); 2152 2153 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 2154 /// Address to jump to. The number of expected destinations can be specified 2155 /// here to make memory allocation more efficient. This constructor also 2156 /// autoinserts at the end of the specified BasicBlock. 2157 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); 2158 protected: 2159 virtual IndirectBrInst *clone_impl() const; 2160 public: 2161 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 2162 Instruction *InsertBefore = 0) { 2163 return new IndirectBrInst(Address, NumDests, InsertBefore); 2164 } 2165 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 2166 BasicBlock *InsertAtEnd) { 2167 return new IndirectBrInst(Address, NumDests, InsertAtEnd); 2168 } 2169 ~IndirectBrInst(); 2170 2171 /// Provide fast operand accessors. 2172 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2173 2174 // Accessor Methods for IndirectBrInst instruction. 2175 Value *getAddress() { return getOperand(0); } 2176 const Value *getAddress() const { return getOperand(0); } 2177 void setAddress(Value *V) { setOperand(0, V); } 2178 2179 2180 /// getNumDestinations - return the number of possible destinations in this 2181 /// indirectbr instruction. 2182 unsigned getNumDestinations() const { return getNumOperands()-1; } 2183 2184 /// getDestination - Return the specified destination. 2185 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } 2186 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } 2187 2188 /// addDestination - Add a destination. 2189 /// 2190 void addDestination(BasicBlock *Dest); 2191 2192 /// removeDestination - This method removes the specified successor from the 2193 /// indirectbr instruction. 2194 void removeDestination(unsigned i); 2195 2196 unsigned getNumSuccessors() const { return getNumOperands()-1; } 2197 BasicBlock *getSuccessor(unsigned i) const { 2198 return cast<BasicBlock>(getOperand(i+1)); 2199 } 2200 void setSuccessor(unsigned i, BasicBlock *NewSucc) { 2201 setOperand(i+1, (Value*)NewSucc); 2202 } 2203 2204 // Methods for support type inquiry through isa, cast, and dyn_cast: 2205 static inline bool classof(const IndirectBrInst *) { return true; } 2206 static inline bool classof(const Instruction *I) { 2207 return I->getOpcode() == Instruction::IndirectBr; 2208 } 2209 static inline bool classof(const Value *V) { 2210 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2211 } 2212 private: 2213 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2214 virtual unsigned getNumSuccessorsV() const; 2215 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2216 }; 2217 2218 template <> 2219 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { 2220 }; 2221 2222 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value) 2223 2224 2225 //===----------------------------------------------------------------------===// 2226 // InvokeInst Class 2227 //===----------------------------------------------------------------------===// 2228 2229 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the 2230 /// calling convention of the call. 2231 /// 2232 class InvokeInst : public TerminatorInst { 2233 AttrListPtr AttributeList; 2234 InvokeInst(const InvokeInst &BI); 2235 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2236 ArrayRef<Value *> Args, const Twine &NameStr); 2237 2238 /// Construct an InvokeInst given a range of arguments. 2239 /// 2240 /// @brief Construct an InvokeInst from a range of arguments 2241 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2242 ArrayRef<Value *> Args, unsigned Values, 2243 const Twine &NameStr, Instruction *InsertBefore); 2244 2245 /// Construct an InvokeInst given a range of arguments. 2246 /// 2247 /// @brief Construct an InvokeInst from a range of arguments 2248 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2249 ArrayRef<Value *> Args, unsigned Values, 2250 const Twine &NameStr, BasicBlock *InsertAtEnd); 2251 protected: 2252 virtual InvokeInst *clone_impl() const; 2253 public: 2254 static InvokeInst *Create(Value *Func, 2255 BasicBlock *IfNormal, BasicBlock *IfException, 2256 ArrayRef<Value *> Args, const Twine &NameStr = "", 2257 Instruction *InsertBefore = 0) { 2258 unsigned Values = unsigned(Args.size()) + 3; 2259 return new(Values) InvokeInst(Func, IfNormal, IfException, Args, 2260 Values, NameStr, InsertBefore); 2261 } 2262 static InvokeInst *Create(Value *Func, 2263 BasicBlock *IfNormal, BasicBlock *IfException, 2264 ArrayRef<Value *> Args, const Twine &NameStr, 2265 BasicBlock *InsertAtEnd) { 2266 unsigned Values = unsigned(Args.size()) + 3; 2267 return new(Values) InvokeInst(Func, IfNormal, IfException, Args, 2268 Values, NameStr, InsertAtEnd); 2269 } 2270 2271 /// Provide fast operand accessors 2272 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2273 2274 /// getNumArgOperands - Return the number of invoke arguments. 2275 /// 2276 unsigned getNumArgOperands() const { return getNumOperands() - 3; } 2277 2278 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument. 2279 /// 2280 Value *getArgOperand(unsigned i) const { return getOperand(i); } 2281 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } 2282 2283 /// getCallingConv/setCallingConv - Get or set the calling convention of this 2284 /// function call. 2285 CallingConv::ID getCallingConv() const { 2286 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction()); 2287 } 2288 void setCallingConv(CallingConv::ID CC) { 2289 setInstructionSubclassData(static_cast<unsigned>(CC)); 2290 } 2291 2292 /// getAttributes - Return the parameter attributes for this invoke. 2293 /// 2294 const AttrListPtr &getAttributes() const { return AttributeList; } 2295 2296 /// setAttributes - Set the parameter attributes for this invoke. 2297 /// 2298 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; } 2299 2300 /// addAttribute - adds the attribute to the list of attributes. 2301 void addAttribute(unsigned i, Attributes attr); 2302 2303 /// removeAttribute - removes the attribute from the list of attributes. 2304 void removeAttribute(unsigned i, Attributes attr); 2305 2306 /// @brief Determine whether the call or the callee has the given attribute. 2307 bool paramHasAttr(unsigned i, Attributes attr) const; 2308 2309 /// @brief Extract the alignment for a call or parameter (0=unknown). 2310 unsigned getParamAlignment(unsigned i) const { 2311 return AttributeList.getParamAlignment(i); 2312 } 2313 2314 /// @brief Return true if the call should not be inlined. 2315 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); } 2316 void setIsNoInline(bool Value = true) { 2317 if (Value) addAttribute(~0, Attribute::NoInline); 2318 else removeAttribute(~0, Attribute::NoInline); 2319 } 2320 2321 /// @brief Determine if the call does not access memory. 2322 bool doesNotAccessMemory() const { 2323 return paramHasAttr(~0, Attribute::ReadNone); 2324 } 2325 void setDoesNotAccessMemory(bool NotAccessMemory = true) { 2326 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone); 2327 else removeAttribute(~0, Attribute::ReadNone); 2328 } 2329 2330 /// @brief Determine if the call does not access or only reads memory. 2331 bool onlyReadsMemory() const { 2332 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly); 2333 } 2334 void setOnlyReadsMemory(bool OnlyReadsMemory = true) { 2335 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly); 2336 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); 2337 } 2338 2339 /// @brief Determine if the call cannot return. 2340 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); } 2341 void setDoesNotReturn(bool DoesNotReturn = true) { 2342 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn); 2343 else removeAttribute(~0, Attribute::NoReturn); 2344 } 2345 2346 /// @brief Determine if the call cannot unwind. 2347 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); } 2348 void setDoesNotThrow(bool DoesNotThrow = true) { 2349 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind); 2350 else removeAttribute(~0, Attribute::NoUnwind); 2351 } 2352 2353 /// @brief Determine if the call returns a structure through first 2354 /// pointer argument. 2355 bool hasStructRetAttr() const { 2356 // Be friendly and also check the callee. 2357 return paramHasAttr(1, Attribute::StructRet); 2358 } 2359 2360 /// @brief Determine if any call argument is an aggregate passed by value. 2361 bool hasByValArgument() const { 2362 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 2363 } 2364 2365 /// getCalledFunction - Return the function called, or null if this is an 2366 /// indirect function invocation. 2367 /// 2368 Function *getCalledFunction() const { 2369 return dyn_cast<Function>(Op<-3>()); 2370 } 2371 2372 /// getCalledValue - Get a pointer to the function that is invoked by this 2373 /// instruction 2374 const Value *getCalledValue() const { return Op<-3>(); } 2375 Value *getCalledValue() { return Op<-3>(); } 2376 2377 /// setCalledFunction - Set the function called. 2378 void setCalledFunction(Value* Fn) { 2379 Op<-3>() = Fn; 2380 } 2381 2382 // get*Dest - Return the destination basic blocks... 2383 BasicBlock *getNormalDest() const { 2384 return cast<BasicBlock>(Op<-2>()); 2385 } 2386 BasicBlock *getUnwindDest() const { 2387 return cast<BasicBlock>(Op<-1>()); 2388 } 2389 void setNormalDest(BasicBlock *B) { 2390 Op<-2>() = reinterpret_cast<Value*>(B); 2391 } 2392 void setUnwindDest(BasicBlock *B) { 2393 Op<-1>() = reinterpret_cast<Value*>(B); 2394 } 2395 2396 BasicBlock *getSuccessor(unsigned i) const { 2397 assert(i < 2 && "Successor # out of range for invoke!"); 2398 return i == 0 ? getNormalDest() : getUnwindDest(); 2399 } 2400 2401 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 2402 assert(idx < 2 && "Successor # out of range for invoke!"); 2403 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc); 2404 } 2405 2406 unsigned getNumSuccessors() const { return 2; } 2407 2408 // Methods for support type inquiry through isa, cast, and dyn_cast: 2409 static inline bool classof(const InvokeInst *) { return true; } 2410 static inline bool classof(const Instruction *I) { 2411 return (I->getOpcode() == Instruction::Invoke); 2412 } 2413 static inline bool classof(const Value *V) { 2414 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2415 } 2416 2417 private: 2418 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2419 virtual unsigned getNumSuccessorsV() const; 2420 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2421 2422 // Shadow Instruction::setInstructionSubclassData with a private forwarding 2423 // method so that subclasses cannot accidentally use it. 2424 void setInstructionSubclassData(unsigned short D) { 2425 Instruction::setInstructionSubclassData(D); 2426 } 2427 }; 2428 2429 template <> 2430 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> { 2431 }; 2432 2433 InvokeInst::InvokeInst(Value *Func, 2434 BasicBlock *IfNormal, BasicBlock *IfException, 2435 ArrayRef<Value *> Args, unsigned Values, 2436 const Twine &NameStr, Instruction *InsertBefore) 2437 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType()) 2438 ->getElementType())->getReturnType(), 2439 Instruction::Invoke, 2440 OperandTraits<InvokeInst>::op_end(this) - Values, 2441 Values, InsertBefore) { 2442 init(Func, IfNormal, IfException, Args, NameStr); 2443 } 2444 InvokeInst::InvokeInst(Value *Func, 2445 BasicBlock *IfNormal, BasicBlock *IfException, 2446 ArrayRef<Value *> Args, unsigned Values, 2447 const Twine &NameStr, BasicBlock *InsertAtEnd) 2448 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType()) 2449 ->getElementType())->getReturnType(), 2450 Instruction::Invoke, 2451 OperandTraits<InvokeInst>::op_end(this) - Values, 2452 Values, InsertAtEnd) { 2453 init(Func, IfNormal, IfException, Args, NameStr); 2454 } 2455 2456 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value) 2457 2458 //===----------------------------------------------------------------------===// 2459 // UnwindInst Class 2460 //===----------------------------------------------------------------------===// 2461 2462 //===--------------------------------------------------------------------------- 2463 /// UnwindInst - Immediately exit the current function, unwinding the stack 2464 /// until an invoke instruction is found. 2465 /// 2466 class UnwindInst : public TerminatorInst { 2467 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 2468 protected: 2469 virtual UnwindInst *clone_impl() const; 2470 public: 2471 // allocate space for exactly zero operands 2472 void *operator new(size_t s) { 2473 return User::operator new(s, 0); 2474 } 2475 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0); 2476 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd); 2477 2478 unsigned getNumSuccessors() const { return 0; } 2479 2480 // Methods for support type inquiry through isa, cast, and dyn_cast: 2481 static inline bool classof(const UnwindInst *) { return true; } 2482 static inline bool classof(const Instruction *I) { 2483 return I->getOpcode() == Instruction::Unwind; 2484 } 2485 static inline bool classof(const Value *V) { 2486 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2487 } 2488 private: 2489 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2490 virtual unsigned getNumSuccessorsV() const; 2491 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2492 }; 2493 2494 //===----------------------------------------------------------------------===// 2495 // UnreachableInst Class 2496 //===----------------------------------------------------------------------===// 2497 2498 //===--------------------------------------------------------------------------- 2499 /// UnreachableInst - This function has undefined behavior. In particular, the 2500 /// presence of this instruction indicates some higher level knowledge that the 2501 /// end of the block cannot be reached. 2502 /// 2503 class UnreachableInst : public TerminatorInst { 2504 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT 2505 protected: 2506 virtual UnreachableInst *clone_impl() const; 2507 2508 public: 2509 // allocate space for exactly zero operands 2510 void *operator new(size_t s) { 2511 return User::operator new(s, 0); 2512 } 2513 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0); 2514 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); 2515 2516 unsigned getNumSuccessors() const { return 0; } 2517 2518 // Methods for support type inquiry through isa, cast, and dyn_cast: 2519 static inline bool classof(const UnreachableInst *) { return true; } 2520 static inline bool classof(const Instruction *I) { 2521 return I->getOpcode() == Instruction::Unreachable; 2522 } 2523 static inline bool classof(const Value *V) { 2524 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2525 } 2526 private: 2527 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2528 virtual unsigned getNumSuccessorsV() const; 2529 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2530 }; 2531 2532 //===----------------------------------------------------------------------===// 2533 // TruncInst Class 2534 //===----------------------------------------------------------------------===// 2535 2536 /// @brief This class represents a truncation of integer types. 2537 class TruncInst : public CastInst { 2538 protected: 2539 /// @brief Clone an identical TruncInst 2540 virtual TruncInst *clone_impl() const; 2541 2542 public: 2543 /// @brief Constructor with insert-before-instruction semantics 2544 TruncInst( 2545 Value *S, ///< The value to be truncated 2546 Type *Ty, ///< The (smaller) type to truncate to 2547 const Twine &NameStr = "", ///< A name for the new instruction 2548 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2549 ); 2550 2551 /// @brief Constructor with insert-at-end-of-block semantics 2552 TruncInst( 2553 Value *S, ///< The value to be truncated 2554 Type *Ty, ///< The (smaller) type to truncate to 2555 const Twine &NameStr, ///< A name for the new instruction 2556 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2557 ); 2558 2559 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2560 static inline bool classof(const TruncInst *) { return true; } 2561 static inline bool classof(const Instruction *I) { 2562 return I->getOpcode() == Trunc; 2563 } 2564 static inline bool classof(const Value *V) { 2565 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2566 } 2567 }; 2568 2569 //===----------------------------------------------------------------------===// 2570 // ZExtInst Class 2571 //===----------------------------------------------------------------------===// 2572 2573 /// @brief This class represents zero extension of integer types. 2574 class ZExtInst : public CastInst { 2575 protected: 2576 /// @brief Clone an identical ZExtInst 2577 virtual ZExtInst *clone_impl() const; 2578 2579 public: 2580 /// @brief Constructor with insert-before-instruction semantics 2581 ZExtInst( 2582 Value *S, ///< The value to be zero extended 2583 Type *Ty, ///< The type to zero extend to 2584 const Twine &NameStr = "", ///< A name for the new instruction 2585 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2586 ); 2587 2588 /// @brief Constructor with insert-at-end semantics. 2589 ZExtInst( 2590 Value *S, ///< The value to be zero extended 2591 Type *Ty, ///< The type to zero extend to 2592 const Twine &NameStr, ///< A name for the new instruction 2593 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2594 ); 2595 2596 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2597 static inline bool classof(const ZExtInst *) { return true; } 2598 static inline bool classof(const Instruction *I) { 2599 return I->getOpcode() == ZExt; 2600 } 2601 static inline bool classof(const Value *V) { 2602 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2603 } 2604 }; 2605 2606 //===----------------------------------------------------------------------===// 2607 // SExtInst Class 2608 //===----------------------------------------------------------------------===// 2609 2610 /// @brief This class represents a sign extension of integer types. 2611 class SExtInst : public CastInst { 2612 protected: 2613 /// @brief Clone an identical SExtInst 2614 virtual SExtInst *clone_impl() const; 2615 2616 public: 2617 /// @brief Constructor with insert-before-instruction semantics 2618 SExtInst( 2619 Value *S, ///< The value to be sign extended 2620 Type *Ty, ///< The type to sign extend to 2621 const Twine &NameStr = "", ///< A name for the new instruction 2622 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2623 ); 2624 2625 /// @brief Constructor with insert-at-end-of-block semantics 2626 SExtInst( 2627 Value *S, ///< The value to be sign extended 2628 Type *Ty, ///< The type to sign extend to 2629 const Twine &NameStr, ///< A name for the new instruction 2630 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2631 ); 2632 2633 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2634 static inline bool classof(const SExtInst *) { return true; } 2635 static inline bool classof(const Instruction *I) { 2636 return I->getOpcode() == SExt; 2637 } 2638 static inline bool classof(const Value *V) { 2639 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2640 } 2641 }; 2642 2643 //===----------------------------------------------------------------------===// 2644 // FPTruncInst Class 2645 //===----------------------------------------------------------------------===// 2646 2647 /// @brief This class represents a truncation of floating point types. 2648 class FPTruncInst : public CastInst { 2649 protected: 2650 /// @brief Clone an identical FPTruncInst 2651 virtual FPTruncInst *clone_impl() const; 2652 2653 public: 2654 /// @brief Constructor with insert-before-instruction semantics 2655 FPTruncInst( 2656 Value *S, ///< The value to be truncated 2657 Type *Ty, ///< The type to truncate to 2658 const Twine &NameStr = "", ///< A name for the new instruction 2659 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2660 ); 2661 2662 /// @brief Constructor with insert-before-instruction semantics 2663 FPTruncInst( 2664 Value *S, ///< The value to be truncated 2665 Type *Ty, ///< The type to truncate to 2666 const Twine &NameStr, ///< A name for the new instruction 2667 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2668 ); 2669 2670 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2671 static inline bool classof(const FPTruncInst *) { return true; } 2672 static inline bool classof(const Instruction *I) { 2673 return I->getOpcode() == FPTrunc; 2674 } 2675 static inline bool classof(const Value *V) { 2676 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2677 } 2678 }; 2679 2680 //===----------------------------------------------------------------------===// 2681 // FPExtInst Class 2682 //===----------------------------------------------------------------------===// 2683 2684 /// @brief This class represents an extension of floating point types. 2685 class FPExtInst : public CastInst { 2686 protected: 2687 /// @brief Clone an identical FPExtInst 2688 virtual FPExtInst *clone_impl() const; 2689 2690 public: 2691 /// @brief Constructor with insert-before-instruction semantics 2692 FPExtInst( 2693 Value *S, ///< The value to be extended 2694 Type *Ty, ///< The type to extend to 2695 const Twine &NameStr = "", ///< A name for the new instruction 2696 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2697 ); 2698 2699 /// @brief Constructor with insert-at-end-of-block semantics 2700 FPExtInst( 2701 Value *S, ///< The value to be extended 2702 Type *Ty, ///< The type to extend to 2703 const Twine &NameStr, ///< A name for the new instruction 2704 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2705 ); 2706 2707 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2708 static inline bool classof(const FPExtInst *) { return true; } 2709 static inline bool classof(const Instruction *I) { 2710 return I->getOpcode() == FPExt; 2711 } 2712 static inline bool classof(const Value *V) { 2713 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2714 } 2715 }; 2716 2717 //===----------------------------------------------------------------------===// 2718 // UIToFPInst Class 2719 //===----------------------------------------------------------------------===// 2720 2721 /// @brief This class represents a cast unsigned integer to floating point. 2722 class UIToFPInst : public CastInst { 2723 protected: 2724 /// @brief Clone an identical UIToFPInst 2725 virtual UIToFPInst *clone_impl() const; 2726 2727 public: 2728 /// @brief Constructor with insert-before-instruction semantics 2729 UIToFPInst( 2730 Value *S, ///< The value to be converted 2731 Type *Ty, ///< The type to convert to 2732 const Twine &NameStr = "", ///< A name for the new instruction 2733 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2734 ); 2735 2736 /// @brief Constructor with insert-at-end-of-block semantics 2737 UIToFPInst( 2738 Value *S, ///< The value to be converted 2739 Type *Ty, ///< The type to convert to 2740 const Twine &NameStr, ///< A name for the new instruction 2741 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2742 ); 2743 2744 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2745 static inline bool classof(const UIToFPInst *) { return true; } 2746 static inline bool classof(const Instruction *I) { 2747 return I->getOpcode() == UIToFP; 2748 } 2749 static inline bool classof(const Value *V) { 2750 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2751 } 2752 }; 2753 2754 //===----------------------------------------------------------------------===// 2755 // SIToFPInst Class 2756 //===----------------------------------------------------------------------===// 2757 2758 /// @brief This class represents a cast from signed integer to floating point. 2759 class SIToFPInst : public CastInst { 2760 protected: 2761 /// @brief Clone an identical SIToFPInst 2762 virtual SIToFPInst *clone_impl() const; 2763 2764 public: 2765 /// @brief Constructor with insert-before-instruction semantics 2766 SIToFPInst( 2767 Value *S, ///< The value to be converted 2768 Type *Ty, ///< The type to convert to 2769 const Twine &NameStr = "", ///< A name for the new instruction 2770 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2771 ); 2772 2773 /// @brief Constructor with insert-at-end-of-block semantics 2774 SIToFPInst( 2775 Value *S, ///< The value to be converted 2776 Type *Ty, ///< The type to convert to 2777 const Twine &NameStr, ///< A name for the new instruction 2778 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2779 ); 2780 2781 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2782 static inline bool classof(const SIToFPInst *) { return true; } 2783 static inline bool classof(const Instruction *I) { 2784 return I->getOpcode() == SIToFP; 2785 } 2786 static inline bool classof(const Value *V) { 2787 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2788 } 2789 }; 2790 2791 //===----------------------------------------------------------------------===// 2792 // FPToUIInst Class 2793 //===----------------------------------------------------------------------===// 2794 2795 /// @brief This class represents a cast from floating point to unsigned integer 2796 class FPToUIInst : public CastInst { 2797 protected: 2798 /// @brief Clone an identical FPToUIInst 2799 virtual FPToUIInst *clone_impl() const; 2800 2801 public: 2802 /// @brief Constructor with insert-before-instruction semantics 2803 FPToUIInst( 2804 Value *S, ///< The value to be converted 2805 Type *Ty, ///< The type to convert to 2806 const Twine &NameStr = "", ///< A name for the new instruction 2807 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2808 ); 2809 2810 /// @brief Constructor with insert-at-end-of-block semantics 2811 FPToUIInst( 2812 Value *S, ///< The value to be converted 2813 Type *Ty, ///< The type to convert to 2814 const Twine &NameStr, ///< A name for the new instruction 2815 BasicBlock *InsertAtEnd ///< Where to insert the new instruction 2816 ); 2817 2818 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2819 static inline bool classof(const FPToUIInst *) { return true; } 2820 static inline bool classof(const Instruction *I) { 2821 return I->getOpcode() == FPToUI; 2822 } 2823 static inline bool classof(const Value *V) { 2824 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2825 } 2826 }; 2827 2828 //===----------------------------------------------------------------------===// 2829 // FPToSIInst Class 2830 //===----------------------------------------------------------------------===// 2831 2832 /// @brief This class represents a cast from floating point to signed integer. 2833 class FPToSIInst : public CastInst { 2834 protected: 2835 /// @brief Clone an identical FPToSIInst 2836 virtual FPToSIInst *clone_impl() const; 2837 2838 public: 2839 /// @brief Constructor with insert-before-instruction semantics 2840 FPToSIInst( 2841 Value *S, ///< The value to be converted 2842 Type *Ty, ///< The type to convert to 2843 const Twine &NameStr = "", ///< A name for the new instruction 2844 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2845 ); 2846 2847 /// @brief Constructor with insert-at-end-of-block semantics 2848 FPToSIInst( 2849 Value *S, ///< The value to be converted 2850 Type *Ty, ///< The type to convert to 2851 const Twine &NameStr, ///< A name for the new instruction 2852 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2853 ); 2854 2855 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: 2856 static inline bool classof(const FPToSIInst *) { return true; } 2857 static inline bool classof(const Instruction *I) { 2858 return I->getOpcode() == FPToSI; 2859 } 2860 static inline bool classof(const Value *V) { 2861 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2862 } 2863 }; 2864 2865 //===----------------------------------------------------------------------===// 2866 // IntToPtrInst Class 2867 //===----------------------------------------------------------------------===// 2868 2869 /// @brief This class represents a cast from an integer to a pointer. 2870 class IntToPtrInst : public CastInst { 2871 public: 2872 /// @brief Constructor with insert-before-instruction semantics 2873 IntToPtrInst( 2874 Value *S, ///< The value to be converted 2875 Type *Ty, ///< The type to convert to 2876 const Twine &NameStr = "", ///< A name for the new instruction 2877 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2878 ); 2879 2880 /// @brief Constructor with insert-at-end-of-block semantics 2881 IntToPtrInst( 2882 Value *S, ///< The value to be converted 2883 Type *Ty, ///< The type to convert to 2884 const Twine &NameStr, ///< A name for the new instruction 2885 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2886 ); 2887 2888 /// @brief Clone an identical IntToPtrInst 2889 virtual IntToPtrInst *clone_impl() const; 2890 2891 // Methods for support type inquiry through isa, cast, and dyn_cast: 2892 static inline bool classof(const IntToPtrInst *) { return true; } 2893 static inline bool classof(const Instruction *I) { 2894 return I->getOpcode() == IntToPtr; 2895 } 2896 static inline bool classof(const Value *V) { 2897 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2898 } 2899 }; 2900 2901 //===----------------------------------------------------------------------===// 2902 // PtrToIntInst Class 2903 //===----------------------------------------------------------------------===// 2904 2905 /// @brief This class represents a cast from a pointer to an integer 2906 class PtrToIntInst : public CastInst { 2907 protected: 2908 /// @brief Clone an identical PtrToIntInst 2909 virtual PtrToIntInst *clone_impl() const; 2910 2911 public: 2912 /// @brief Constructor with insert-before-instruction semantics 2913 PtrToIntInst( 2914 Value *S, ///< The value to be converted 2915 Type *Ty, ///< The type to convert to 2916 const Twine &NameStr = "", ///< A name for the new instruction 2917 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2918 ); 2919 2920 /// @brief Constructor with insert-at-end-of-block semantics 2921 PtrToIntInst( 2922 Value *S, ///< The value to be converted 2923 Type *Ty, ///< The type to convert to 2924 const Twine &NameStr, ///< A name for the new instruction 2925 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2926 ); 2927 2928 // Methods for support type inquiry through isa, cast, and dyn_cast: 2929 static inline bool classof(const PtrToIntInst *) { return true; } 2930 static inline bool classof(const Instruction *I) { 2931 return I->getOpcode() == PtrToInt; 2932 } 2933 static inline bool classof(const Value *V) { 2934 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2935 } 2936 }; 2937 2938 //===----------------------------------------------------------------------===// 2939 // BitCastInst Class 2940 //===----------------------------------------------------------------------===// 2941 2942 /// @brief This class represents a no-op cast from one type to another. 2943 class BitCastInst : public CastInst { 2944 protected: 2945 /// @brief Clone an identical BitCastInst 2946 virtual BitCastInst *clone_impl() const; 2947 2948 public: 2949 /// @brief Constructor with insert-before-instruction semantics 2950 BitCastInst( 2951 Value *S, ///< The value to be casted 2952 Type *Ty, ///< The type to casted to 2953 const Twine &NameStr = "", ///< A name for the new instruction 2954 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 2955 ); 2956 2957 /// @brief Constructor with insert-at-end-of-block semantics 2958 BitCastInst( 2959 Value *S, ///< The value to be casted 2960 Type *Ty, ///< The type to casted to 2961 const Twine &NameStr, ///< A name for the new instruction 2962 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 2963 ); 2964 2965 // Methods for support type inquiry through isa, cast, and dyn_cast: 2966 static inline bool classof(const BitCastInst *) { return true; } 2967 static inline bool classof(const Instruction *I) { 2968 return I->getOpcode() == BitCast; 2969 } 2970 static inline bool classof(const Value *V) { 2971 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2972 } 2973 }; 2974 2975 } // End llvm namespace 2976 2977 #endif 2978