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_IR_INSTRUCTIONS_H 17 #define LLVM_IR_INSTRUCTIONS_H 18 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/IR/Attributes.h" 22 #include "llvm/IR/CallingConv.h" 23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/InstrTypes.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/IntegersSubset.h" 27 #include "llvm/Support/IntegersSubsetMapping.h" 28 #include <iterator> 29 30 namespace llvm { 31 32 class APInt; 33 class ConstantInt; 34 class ConstantRange; 35 class DataLayout; 36 class LLVMContext; 37 38 enum AtomicOrdering { 39 NotAtomic = 0, 40 Unordered = 1, 41 Monotonic = 2, 42 // Consume = 3, // Not specified yet. 43 Acquire = 4, 44 Release = 5, 45 AcquireRelease = 6, 46 SequentiallyConsistent = 7 47 }; 48 49 enum SynchronizationScope { 50 SingleThread = 0, 51 CrossThread = 1 52 }; 53 54 //===----------------------------------------------------------------------===// 55 // AllocaInst Class 56 //===----------------------------------------------------------------------===// 57 58 /// AllocaInst - an instruction to allocate memory on the stack 59 /// 60 class AllocaInst : public UnaryInstruction { 61 protected: 62 virtual AllocaInst *clone_impl() const; 63 public: 64 explicit AllocaInst(Type *Ty, Value *ArraySize = 0, 65 const Twine &Name = "", Instruction *InsertBefore = 0); 66 AllocaInst(Type *Ty, Value *ArraySize, 67 const Twine &Name, BasicBlock *InsertAtEnd); 68 69 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0); 70 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd); 71 72 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 73 const Twine &Name = "", Instruction *InsertBefore = 0); 74 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 75 const Twine &Name, BasicBlock *InsertAtEnd); 76 77 // Out of line virtual method, so the vtable, etc. has a home. 78 virtual ~AllocaInst(); 79 80 /// isArrayAllocation - Return true if there is an allocation size parameter 81 /// to the allocation instruction that is not 1. 82 /// 83 bool isArrayAllocation() const; 84 85 /// getArraySize - Get the number of elements allocated. For a simple 86 /// allocation of a single element, this will return a constant 1 value. 87 /// 88 const Value *getArraySize() const { return getOperand(0); } 89 Value *getArraySize() { return getOperand(0); } 90 91 /// getType - Overload to return most specific pointer type 92 /// 93 PointerType *getType() const { 94 return cast<PointerType>(Instruction::getType()); 95 } 96 97 /// getAllocatedType - Return the type that is being allocated by the 98 /// instruction. 99 /// 100 Type *getAllocatedType() const; 101 102 /// getAlignment - Return the alignment of the memory that is being allocated 103 /// by the instruction. 104 /// 105 unsigned getAlignment() const { 106 return (1u << getSubclassDataFromInstruction()) >> 1; 107 } 108 void setAlignment(unsigned Align); 109 110 /// isStaticAlloca - Return true if this alloca is in the entry block of the 111 /// function and is a constant size. If so, the code generator will fold it 112 /// into the prolog/epilog code, so it is basically free. 113 bool isStaticAlloca() const; 114 115 // Methods for support type inquiry through isa, cast, and dyn_cast: 116 static inline bool classof(const Instruction *I) { 117 return (I->getOpcode() == Instruction::Alloca); 118 } 119 static inline bool classof(const Value *V) { 120 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 121 } 122 private: 123 // Shadow Instruction::setInstructionSubclassData with a private forwarding 124 // method so that subclasses cannot accidentally use it. 125 void setInstructionSubclassData(unsigned short D) { 126 Instruction::setInstructionSubclassData(D); 127 } 128 }; 129 130 131 //===----------------------------------------------------------------------===// 132 // LoadInst Class 133 //===----------------------------------------------------------------------===// 134 135 /// LoadInst - an instruction for reading from memory. This uses the 136 /// SubclassData field in Value to store whether or not the load is volatile. 137 /// 138 class LoadInst : public UnaryInstruction { 139 void AssertOK(); 140 protected: 141 virtual LoadInst *clone_impl() const; 142 public: 143 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore); 144 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); 145 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false, 146 Instruction *InsertBefore = 0); 147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 148 BasicBlock *InsertAtEnd); 149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 150 unsigned Align, Instruction *InsertBefore = 0); 151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 152 unsigned Align, BasicBlock *InsertAtEnd); 153 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 154 unsigned Align, AtomicOrdering Order, 155 SynchronizationScope SynchScope = CrossThread, 156 Instruction *InsertBefore = 0); 157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 158 unsigned Align, AtomicOrdering Order, 159 SynchronizationScope SynchScope, 160 BasicBlock *InsertAtEnd); 161 162 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore); 163 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd); 164 explicit LoadInst(Value *Ptr, const char *NameStr = 0, 165 bool isVolatile = false, Instruction *InsertBefore = 0); 166 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile, 167 BasicBlock *InsertAtEnd); 168 169 /// isVolatile - Return true if this is a load from a volatile memory 170 /// location. 171 /// 172 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 173 174 /// setVolatile - Specify whether this is a volatile load or not. 175 /// 176 void setVolatile(bool V) { 177 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 178 (V ? 1 : 0)); 179 } 180 181 /// getAlignment - Return the alignment of the access that is being performed 182 /// 183 unsigned getAlignment() const { 184 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1; 185 } 186 187 void setAlignment(unsigned Align); 188 189 /// Returns the ordering effect of this fence. 190 AtomicOrdering getOrdering() const { 191 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); 192 } 193 194 /// Set the ordering constraint on this load. May not be Release or 195 /// AcquireRelease. 196 void setOrdering(AtomicOrdering Ordering) { 197 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | 198 (Ordering << 7)); 199 } 200 201 SynchronizationScope getSynchScope() const { 202 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1); 203 } 204 205 /// Specify whether this load is ordered with respect to all 206 /// concurrently executing threads, or only with respect to signal handlers 207 /// executing in the same thread. 208 void setSynchScope(SynchronizationScope xthread) { 209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) | 210 (xthread << 6)); 211 } 212 213 bool isAtomic() const { return getOrdering() != NotAtomic; } 214 void setAtomic(AtomicOrdering Ordering, 215 SynchronizationScope SynchScope = CrossThread) { 216 setOrdering(Ordering); 217 setSynchScope(SynchScope); 218 } 219 220 bool isSimple() const { return !isAtomic() && !isVolatile(); } 221 bool isUnordered() const { 222 return getOrdering() <= Unordered && !isVolatile(); 223 } 224 225 Value *getPointerOperand() { return getOperand(0); } 226 const Value *getPointerOperand() const { return getOperand(0); } 227 static unsigned getPointerOperandIndex() { return 0U; } 228 229 /// \brief Returns the address space of the pointer operand. 230 unsigned getPointerAddressSpace() const { 231 return getPointerOperand()->getType()->getPointerAddressSpace(); 232 } 233 234 235 // Methods for support type inquiry through isa, cast, and dyn_cast: 236 static inline bool classof(const Instruction *I) { 237 return I->getOpcode() == Instruction::Load; 238 } 239 static inline bool classof(const Value *V) { 240 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 241 } 242 private: 243 // Shadow Instruction::setInstructionSubclassData with a private forwarding 244 // method so that subclasses cannot accidentally use it. 245 void setInstructionSubclassData(unsigned short D) { 246 Instruction::setInstructionSubclassData(D); 247 } 248 }; 249 250 251 //===----------------------------------------------------------------------===// 252 // StoreInst Class 253 //===----------------------------------------------------------------------===// 254 255 /// StoreInst - an instruction for storing to memory 256 /// 257 class StoreInst : public Instruction { 258 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 259 void AssertOK(); 260 protected: 261 virtual StoreInst *clone_impl() const; 262 public: 263 // allocate space for exactly two operands 264 void *operator new(size_t s) { 265 return User::operator new(s, 2); 266 } 267 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); 268 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); 269 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, 270 Instruction *InsertBefore = 0); 271 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); 272 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 273 unsigned Align, Instruction *InsertBefore = 0); 274 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 275 unsigned Align, BasicBlock *InsertAtEnd); 276 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 277 unsigned Align, AtomicOrdering Order, 278 SynchronizationScope SynchScope = CrossThread, 279 Instruction *InsertBefore = 0); 280 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 281 unsigned Align, AtomicOrdering Order, 282 SynchronizationScope SynchScope, 283 BasicBlock *InsertAtEnd); 284 285 286 /// isVolatile - Return true if this is a store to a volatile memory 287 /// location. 288 /// 289 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 290 291 /// setVolatile - Specify whether this is a volatile store or not. 292 /// 293 void setVolatile(bool V) { 294 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 295 (V ? 1 : 0)); 296 } 297 298 /// Transparently provide more efficient getOperand methods. 299 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 300 301 /// getAlignment - Return the alignment of the access that is being performed 302 /// 303 unsigned getAlignment() const { 304 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1; 305 } 306 307 void setAlignment(unsigned Align); 308 309 /// Returns the ordering effect of this store. 310 AtomicOrdering getOrdering() const { 311 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); 312 } 313 314 /// Set the ordering constraint on this store. May not be Acquire or 315 /// AcquireRelease. 316 void setOrdering(AtomicOrdering Ordering) { 317 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | 318 (Ordering << 7)); 319 } 320 321 SynchronizationScope getSynchScope() const { 322 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1); 323 } 324 325 /// Specify whether this store instruction is ordered with respect to all 326 /// concurrently executing threads, or only with respect to signal handlers 327 /// executing in the same thread. 328 void setSynchScope(SynchronizationScope xthread) { 329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) | 330 (xthread << 6)); 331 } 332 333 bool isAtomic() const { return getOrdering() != NotAtomic; } 334 void setAtomic(AtomicOrdering Ordering, 335 SynchronizationScope SynchScope = CrossThread) { 336 setOrdering(Ordering); 337 setSynchScope(SynchScope); 338 } 339 340 bool isSimple() const { return !isAtomic() && !isVolatile(); } 341 bool isUnordered() const { 342 return getOrdering() <= Unordered && !isVolatile(); 343 } 344 345 Value *getValueOperand() { return getOperand(0); } 346 const Value *getValueOperand() const { return getOperand(0); } 347 348 Value *getPointerOperand() { return getOperand(1); } 349 const Value *getPointerOperand() const { return getOperand(1); } 350 static unsigned getPointerOperandIndex() { return 1U; } 351 352 /// \brief Returns the address space of the pointer operand. 353 unsigned getPointerAddressSpace() const { 354 return getPointerOperand()->getType()->getPointerAddressSpace(); 355 } 356 357 // Methods for support type inquiry through isa, cast, and dyn_cast: 358 static inline bool classof(const Instruction *I) { 359 return I->getOpcode() == Instruction::Store; 360 } 361 static inline bool classof(const Value *V) { 362 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 363 } 364 private: 365 // Shadow Instruction::setInstructionSubclassData with a private forwarding 366 // method so that subclasses cannot accidentally use it. 367 void setInstructionSubclassData(unsigned short D) { 368 Instruction::setInstructionSubclassData(D); 369 } 370 }; 371 372 template <> 373 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { 374 }; 375 376 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value) 377 378 //===----------------------------------------------------------------------===// 379 // FenceInst Class 380 //===----------------------------------------------------------------------===// 381 382 /// FenceInst - an instruction for ordering other memory operations 383 /// 384 class FenceInst : public Instruction { 385 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 386 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope); 387 protected: 388 virtual FenceInst *clone_impl() const; 389 public: 390 // allocate space for exactly zero operands 391 void *operator new(size_t s) { 392 return User::operator new(s, 0); 393 } 394 395 // Ordering may only be Acquire, Release, AcquireRelease, or 396 // SequentiallyConsistent. 397 FenceInst(LLVMContext &C, AtomicOrdering Ordering, 398 SynchronizationScope SynchScope = CrossThread, 399 Instruction *InsertBefore = 0); 400 FenceInst(LLVMContext &C, AtomicOrdering Ordering, 401 SynchronizationScope SynchScope, 402 BasicBlock *InsertAtEnd); 403 404 /// Returns the ordering effect of this fence. 405 AtomicOrdering getOrdering() const { 406 return AtomicOrdering(getSubclassDataFromInstruction() >> 1); 407 } 408 409 /// Set the ordering constraint on this fence. May only be Acquire, Release, 410 /// AcquireRelease, or SequentiallyConsistent. 411 void setOrdering(AtomicOrdering Ordering) { 412 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | 413 (Ordering << 1)); 414 } 415 416 SynchronizationScope getSynchScope() const { 417 return SynchronizationScope(getSubclassDataFromInstruction() & 1); 418 } 419 420 /// Specify whether this fence orders other operations with respect to all 421 /// concurrently executing threads, or only with respect to signal handlers 422 /// executing in the same thread. 423 void setSynchScope(SynchronizationScope xthread) { 424 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 425 xthread); 426 } 427 428 // Methods for support type inquiry through isa, cast, and dyn_cast: 429 static inline bool classof(const Instruction *I) { 430 return I->getOpcode() == Instruction::Fence; 431 } 432 static inline bool classof(const Value *V) { 433 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 434 } 435 private: 436 // Shadow Instruction::setInstructionSubclassData with a private forwarding 437 // method so that subclasses cannot accidentally use it. 438 void setInstructionSubclassData(unsigned short D) { 439 Instruction::setInstructionSubclassData(D); 440 } 441 }; 442 443 //===----------------------------------------------------------------------===// 444 // AtomicCmpXchgInst Class 445 //===----------------------------------------------------------------------===// 446 447 /// AtomicCmpXchgInst - an instruction that atomically checks whether a 448 /// specified value is in a memory location, and, if it is, stores a new value 449 /// there. Returns the value that was loaded. 450 /// 451 class AtomicCmpXchgInst : public Instruction { 452 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 453 void Init(Value *Ptr, Value *Cmp, Value *NewVal, 454 AtomicOrdering Ordering, SynchronizationScope SynchScope); 455 protected: 456 virtual AtomicCmpXchgInst *clone_impl() const; 457 public: 458 // allocate space for exactly three operands 459 void *operator new(size_t s) { 460 return User::operator new(s, 3); 461 } 462 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 463 AtomicOrdering Ordering, SynchronizationScope SynchScope, 464 Instruction *InsertBefore = 0); 465 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 466 AtomicOrdering Ordering, SynchronizationScope SynchScope, 467 BasicBlock *InsertAtEnd); 468 469 /// isVolatile - Return true if this is a cmpxchg from a volatile memory 470 /// location. 471 /// 472 bool isVolatile() const { 473 return getSubclassDataFromInstruction() & 1; 474 } 475 476 /// setVolatile - Specify whether this is a volatile cmpxchg. 477 /// 478 void setVolatile(bool V) { 479 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 480 (unsigned)V); 481 } 482 483 /// Transparently provide more efficient getOperand methods. 484 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 485 486 /// Set the ordering constraint on this cmpxchg. 487 void setOrdering(AtomicOrdering Ordering) { 488 assert(Ordering != NotAtomic && 489 "CmpXchg instructions can only be atomic."); 490 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) | 491 (Ordering << 2)); 492 } 493 494 /// Specify whether this cmpxchg is atomic and orders other operations with 495 /// respect to all concurrently executing threads, or only with respect to 496 /// signal handlers executing in the same thread. 497 void setSynchScope(SynchronizationScope SynchScope) { 498 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) | 499 (SynchScope << 1)); 500 } 501 502 /// Returns the ordering constraint on this cmpxchg. 503 AtomicOrdering getOrdering() const { 504 return AtomicOrdering(getSubclassDataFromInstruction() >> 2); 505 } 506 507 /// Returns whether this cmpxchg is atomic between threads or only within a 508 /// single thread. 509 SynchronizationScope getSynchScope() const { 510 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1); 511 } 512 513 Value *getPointerOperand() { return getOperand(0); } 514 const Value *getPointerOperand() const { return getOperand(0); } 515 static unsigned getPointerOperandIndex() { return 0U; } 516 517 Value *getCompareOperand() { return getOperand(1); } 518 const Value *getCompareOperand() const { return getOperand(1); } 519 520 Value *getNewValOperand() { return getOperand(2); } 521 const Value *getNewValOperand() const { return getOperand(2); } 522 523 /// \brief Returns the address space of the pointer operand. 524 unsigned getPointerAddressSpace() const { 525 return getPointerOperand()->getType()->getPointerAddressSpace(); 526 } 527 528 // Methods for support type inquiry through isa, cast, and dyn_cast: 529 static inline bool classof(const Instruction *I) { 530 return I->getOpcode() == Instruction::AtomicCmpXchg; 531 } 532 static inline bool classof(const Value *V) { 533 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 534 } 535 private: 536 // Shadow Instruction::setInstructionSubclassData with a private forwarding 537 // method so that subclasses cannot accidentally use it. 538 void setInstructionSubclassData(unsigned short D) { 539 Instruction::setInstructionSubclassData(D); 540 } 541 }; 542 543 template <> 544 struct OperandTraits<AtomicCmpXchgInst> : 545 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { 546 }; 547 548 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value) 549 550 //===----------------------------------------------------------------------===// 551 // AtomicRMWInst Class 552 //===----------------------------------------------------------------------===// 553 554 /// AtomicRMWInst - an instruction that atomically reads a memory location, 555 /// combines it with another value, and then stores the result back. Returns 556 /// the old value. 557 /// 558 class AtomicRMWInst : public Instruction { 559 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 560 protected: 561 virtual AtomicRMWInst *clone_impl() const; 562 public: 563 /// This enumeration lists the possible modifications atomicrmw can make. In 564 /// the descriptions, 'p' is the pointer to the instruction's memory location, 565 /// 'old' is the initial value of *p, and 'v' is the other value passed to the 566 /// instruction. These instructions always return 'old'. 567 enum BinOp { 568 /// *p = v 569 Xchg, 570 /// *p = old + v 571 Add, 572 /// *p = old - v 573 Sub, 574 /// *p = old & v 575 And, 576 /// *p = ~old & v 577 Nand, 578 /// *p = old | v 579 Or, 580 /// *p = old ^ v 581 Xor, 582 /// *p = old >signed v ? old : v 583 Max, 584 /// *p = old <signed v ? old : v 585 Min, 586 /// *p = old >unsigned v ? old : v 587 UMax, 588 /// *p = old <unsigned v ? old : v 589 UMin, 590 591 FIRST_BINOP = Xchg, 592 LAST_BINOP = UMin, 593 BAD_BINOP 594 }; 595 596 // allocate space for exactly two operands 597 void *operator new(size_t s) { 598 return User::operator new(s, 2); 599 } 600 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 601 AtomicOrdering Ordering, SynchronizationScope SynchScope, 602 Instruction *InsertBefore = 0); 603 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 604 AtomicOrdering Ordering, SynchronizationScope SynchScope, 605 BasicBlock *InsertAtEnd); 606 607 BinOp getOperation() const { 608 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5); 609 } 610 611 void setOperation(BinOp Operation) { 612 unsigned short SubclassData = getSubclassDataFromInstruction(); 613 setInstructionSubclassData((SubclassData & 31) | 614 (Operation << 5)); 615 } 616 617 /// isVolatile - Return true if this is a RMW on a volatile memory location. 618 /// 619 bool isVolatile() const { 620 return getSubclassDataFromInstruction() & 1; 621 } 622 623 /// setVolatile - Specify whether this is a volatile RMW or not. 624 /// 625 void setVolatile(bool V) { 626 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 627 (unsigned)V); 628 } 629 630 /// Transparently provide more efficient getOperand methods. 631 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 632 633 /// Set the ordering constraint on this RMW. 634 void setOrdering(AtomicOrdering Ordering) { 635 assert(Ordering != NotAtomic && 636 "atomicrmw instructions can only be atomic."); 637 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) | 638 (Ordering << 2)); 639 } 640 641 /// Specify whether this RMW orders other operations with respect to all 642 /// concurrently executing threads, or only with respect to signal handlers 643 /// executing in the same thread. 644 void setSynchScope(SynchronizationScope SynchScope) { 645 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) | 646 (SynchScope << 1)); 647 } 648 649 /// Returns the ordering constraint on this RMW. 650 AtomicOrdering getOrdering() const { 651 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); 652 } 653 654 /// Returns whether this RMW is atomic between threads or only within a 655 /// single thread. 656 SynchronizationScope getSynchScope() const { 657 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1); 658 } 659 660 Value *getPointerOperand() { return getOperand(0); } 661 const Value *getPointerOperand() const { return getOperand(0); } 662 static unsigned getPointerOperandIndex() { return 0U; } 663 664 Value *getValOperand() { return getOperand(1); } 665 const Value *getValOperand() const { return getOperand(1); } 666 667 /// \brief Returns the address space of the pointer operand. 668 unsigned getPointerAddressSpace() const { 669 return getPointerOperand()->getType()->getPointerAddressSpace(); 670 } 671 672 // Methods for support type inquiry through isa, cast, and dyn_cast: 673 static inline bool classof(const Instruction *I) { 674 return I->getOpcode() == Instruction::AtomicRMW; 675 } 676 static inline bool classof(const Value *V) { 677 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 678 } 679 private: 680 void Init(BinOp Operation, Value *Ptr, Value *Val, 681 AtomicOrdering Ordering, SynchronizationScope SynchScope); 682 // Shadow Instruction::setInstructionSubclassData with a private forwarding 683 // method so that subclasses cannot accidentally use it. 684 void setInstructionSubclassData(unsigned short D) { 685 Instruction::setInstructionSubclassData(D); 686 } 687 }; 688 689 template <> 690 struct OperandTraits<AtomicRMWInst> 691 : public FixedNumOperandTraits<AtomicRMWInst,2> { 692 }; 693 694 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value) 695 696 //===----------------------------------------------------------------------===// 697 // GetElementPtrInst Class 698 //===----------------------------------------------------------------------===// 699 700 // checkGEPType - Simple wrapper function to give a better assertion failure 701 // message on bad indexes for a gep instruction. 702 // 703 inline Type *checkGEPType(Type *Ty) { 704 assert(Ty && "Invalid GetElementPtrInst indices for type!"); 705 return Ty; 706 } 707 708 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to 709 /// access elements of arrays and structs 710 /// 711 class GetElementPtrInst : public Instruction { 712 GetElementPtrInst(const GetElementPtrInst &GEPI); 713 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); 714 715 /// Constructors - Create a getelementptr instruction with a base pointer an 716 /// list of indices. The first ctor can optionally insert before an existing 717 /// instruction, the second appends the new instruction to the specified 718 /// BasicBlock. 719 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList, 720 unsigned Values, const Twine &NameStr, 721 Instruction *InsertBefore); 722 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList, 723 unsigned Values, const Twine &NameStr, 724 BasicBlock *InsertAtEnd); 725 protected: 726 virtual GetElementPtrInst *clone_impl() const; 727 public: 728 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList, 729 const Twine &NameStr = "", 730 Instruction *InsertBefore = 0) { 731 unsigned Values = 1 + unsigned(IdxList.size()); 732 return new(Values) 733 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore); 734 } 735 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList, 736 const Twine &NameStr, 737 BasicBlock *InsertAtEnd) { 738 unsigned Values = 1 + unsigned(IdxList.size()); 739 return new(Values) 740 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd); 741 } 742 743 /// Create an "inbounds" getelementptr. See the documentation for the 744 /// "inbounds" flag in LangRef.html for details. 745 static GetElementPtrInst *CreateInBounds(Value *Ptr, 746 ArrayRef<Value *> IdxList, 747 const Twine &NameStr = "", 748 Instruction *InsertBefore = 0) { 749 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore); 750 GEP->setIsInBounds(true); 751 return GEP; 752 } 753 static GetElementPtrInst *CreateInBounds(Value *Ptr, 754 ArrayRef<Value *> IdxList, 755 const Twine &NameStr, 756 BasicBlock *InsertAtEnd) { 757 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd); 758 GEP->setIsInBounds(true); 759 return GEP; 760 } 761 762 /// Transparently provide more efficient getOperand methods. 763 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 764 765 // getType - Overload to return most specific sequential type. 766 SequentialType *getType() const { 767 return cast<SequentialType>(Instruction::getType()); 768 } 769 770 /// \brief Returns the address space of this instruction's pointer type. 771 unsigned getAddressSpace() const { 772 // Note that this is always the same as the pointer operand's address space 773 // and that is cheaper to compute, so cheat here. 774 return getPointerAddressSpace(); 775 } 776 777 /// getIndexedType - Returns the type of the element that would be loaded with 778 /// a load instruction with the specified parameters. 779 /// 780 /// Null is returned if the indices are invalid for the specified 781 /// pointer type. 782 /// 783 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList); 784 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList); 785 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList); 786 787 inline op_iterator idx_begin() { return op_begin()+1; } 788 inline const_op_iterator idx_begin() const { return op_begin()+1; } 789 inline op_iterator idx_end() { return op_end(); } 790 inline const_op_iterator idx_end() const { return op_end(); } 791 792 Value *getPointerOperand() { 793 return getOperand(0); 794 } 795 const Value *getPointerOperand() const { 796 return getOperand(0); 797 } 798 static unsigned getPointerOperandIndex() { 799 return 0U; // get index for modifying correct operand. 800 } 801 802 /// getPointerOperandType - Method to return the pointer operand as a 803 /// PointerType. 804 Type *getPointerOperandType() const { 805 return getPointerOperand()->getType(); 806 } 807 808 /// \brief Returns the address space of the pointer operand. 809 unsigned getPointerAddressSpace() const { 810 return getPointerOperandType()->getPointerAddressSpace(); 811 } 812 813 /// GetGEPReturnType - Returns the pointer type returned by the GEP 814 /// instruction, which may be a vector of pointers. 815 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) { 816 Type *PtrTy = PointerType::get(checkGEPType( 817 getIndexedType(Ptr->getType(), IdxList)), 818 Ptr->getType()->getPointerAddressSpace()); 819 // Vector GEP 820 if (Ptr->getType()->isVectorTy()) { 821 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements(); 822 return VectorType::get(PtrTy, NumElem); 823 } 824 825 // Scalar GEP 826 return PtrTy; 827 } 828 829 unsigned getNumIndices() const { // Note: always non-negative 830 return getNumOperands() - 1; 831 } 832 833 bool hasIndices() const { 834 return getNumOperands() > 1; 835 } 836 837 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 838 /// zeros. If so, the result pointer and the first operand have the same 839 /// value, just potentially different types. 840 bool hasAllZeroIndices() const; 841 842 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 843 /// constant integers. If so, the result pointer and the first operand have 844 /// a constant offset between them. 845 bool hasAllConstantIndices() const; 846 847 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction. 848 /// See LangRef.html for the meaning of inbounds on a getelementptr. 849 void setIsInBounds(bool b = true); 850 851 /// isInBounds - Determine whether the GEP has the inbounds flag. 852 bool isInBounds() const; 853 854 /// \brief Accumulate the constant address offset of this GEP if possible. 855 /// 856 /// This routine accepts an APInt into which it will accumulate the constant 857 /// offset of this GEP if the GEP is in fact constant. If the GEP is not 858 /// all-constant, it returns false and the value of the offset APInt is 859 /// undefined (it is *not* preserved!). The APInt passed into this routine 860 /// must be at least as wide as the IntPtr type for the address space of 861 /// the base GEP pointer. 862 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; 863 864 // Methods for support type inquiry through isa, cast, and dyn_cast: 865 static inline bool classof(const Instruction *I) { 866 return (I->getOpcode() == Instruction::GetElementPtr); 867 } 868 static inline bool classof(const Value *V) { 869 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 870 } 871 }; 872 873 template <> 874 struct OperandTraits<GetElementPtrInst> : 875 public VariadicOperandTraits<GetElementPtrInst, 1> { 876 }; 877 878 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 879 ArrayRef<Value *> IdxList, 880 unsigned Values, 881 const Twine &NameStr, 882 Instruction *InsertBefore) 883 : Instruction(getGEPReturnType(Ptr, IdxList), 884 GetElementPtr, 885 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 886 Values, InsertBefore) { 887 init(Ptr, IdxList, NameStr); 888 } 889 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 890 ArrayRef<Value *> IdxList, 891 unsigned Values, 892 const Twine &NameStr, 893 BasicBlock *InsertAtEnd) 894 : Instruction(getGEPReturnType(Ptr, IdxList), 895 GetElementPtr, 896 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 897 Values, InsertAtEnd) { 898 init(Ptr, IdxList, NameStr); 899 } 900 901 902 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value) 903 904 905 //===----------------------------------------------------------------------===// 906 // ICmpInst Class 907 //===----------------------------------------------------------------------===// 908 909 /// This instruction compares its operands according to the predicate given 910 /// to the constructor. It only operates on integers or pointers. The operands 911 /// must be identical types. 912 /// \brief Represent an integer comparison operator. 913 class ICmpInst: public CmpInst { 914 protected: 915 /// \brief Clone an identical ICmpInst 916 virtual ICmpInst *clone_impl() const; 917 public: 918 /// \brief Constructor with insert-before-instruction semantics. 919 ICmpInst( 920 Instruction *InsertBefore, ///< Where to insert 921 Predicate pred, ///< The predicate to use for the comparison 922 Value *LHS, ///< The left-hand-side of the expression 923 Value *RHS, ///< The right-hand-side of the expression 924 const Twine &NameStr = "" ///< Name of the instruction 925 ) : CmpInst(makeCmpResultType(LHS->getType()), 926 Instruction::ICmp, pred, LHS, RHS, NameStr, 927 InsertBefore) { 928 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 929 pred <= CmpInst::LAST_ICMP_PREDICATE && 930 "Invalid ICmp predicate value"); 931 assert(getOperand(0)->getType() == getOperand(1)->getType() && 932 "Both operands to ICmp instruction are not of the same type!"); 933 // Check that the operands are the right type 934 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 935 getOperand(0)->getType()->getScalarType()->isPointerTy()) && 936 "Invalid operand types for ICmp instruction"); 937 } 938 939 /// \brief Constructor with insert-at-end semantics. 940 ICmpInst( 941 BasicBlock &InsertAtEnd, ///< Block to insert into. 942 Predicate pred, ///< The predicate to use for the comparison 943 Value *LHS, ///< The left-hand-side of the expression 944 Value *RHS, ///< The right-hand-side of the expression 945 const Twine &NameStr = "" ///< Name of the instruction 946 ) : CmpInst(makeCmpResultType(LHS->getType()), 947 Instruction::ICmp, pred, LHS, RHS, NameStr, 948 &InsertAtEnd) { 949 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 950 pred <= CmpInst::LAST_ICMP_PREDICATE && 951 "Invalid ICmp predicate value"); 952 assert(getOperand(0)->getType() == getOperand(1)->getType() && 953 "Both operands to ICmp instruction are not of the same type!"); 954 // Check that the operands are the right type 955 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 956 getOperand(0)->getType()->getScalarType()->isPointerTy()) && 957 "Invalid operand types for ICmp instruction"); 958 } 959 960 /// \brief Constructor with no-insertion semantics 961 ICmpInst( 962 Predicate pred, ///< The predicate to use for the comparison 963 Value *LHS, ///< The left-hand-side of the expression 964 Value *RHS, ///< The right-hand-side of the expression 965 const Twine &NameStr = "" ///< Name of the instruction 966 ) : CmpInst(makeCmpResultType(LHS->getType()), 967 Instruction::ICmp, pred, LHS, RHS, NameStr) { 968 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE && 969 pred <= CmpInst::LAST_ICMP_PREDICATE && 970 "Invalid ICmp predicate value"); 971 assert(getOperand(0)->getType() == getOperand(1)->getType() && 972 "Both operands to ICmp instruction are not of the same type!"); 973 // Check that the operands are the right type 974 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 975 getOperand(0)->getType()->getScalarType()->isPointerTy()) && 976 "Invalid operand types for ICmp instruction"); 977 } 978 979 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. 980 /// @returns the predicate that would be the result if the operand were 981 /// regarded as signed. 982 /// \brief Return the signed version of the predicate 983 Predicate getSignedPredicate() const { 984 return getSignedPredicate(getPredicate()); 985 } 986 987 /// This is a static version that you can use without an instruction. 988 /// \brief Return the signed version of the predicate. 989 static Predicate getSignedPredicate(Predicate pred); 990 991 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. 992 /// @returns the predicate that would be the result if the operand were 993 /// regarded as unsigned. 994 /// \brief Return the unsigned version of the predicate 995 Predicate getUnsignedPredicate() const { 996 return getUnsignedPredicate(getPredicate()); 997 } 998 999 /// This is a static version that you can use without an instruction. 1000 /// \brief Return the unsigned version of the predicate. 1001 static Predicate getUnsignedPredicate(Predicate pred); 1002 1003 /// isEquality - Return true if this predicate is either EQ or NE. This also 1004 /// tests for commutativity. 1005 static bool isEquality(Predicate P) { 1006 return P == ICMP_EQ || P == ICMP_NE; 1007 } 1008 1009 /// isEquality - Return true if this predicate is either EQ or NE. This also 1010 /// tests for commutativity. 1011 bool isEquality() const { 1012 return isEquality(getPredicate()); 1013 } 1014 1015 /// @returns true if the predicate of this ICmpInst is commutative 1016 /// \brief Determine if this relation is commutative. 1017 bool isCommutative() const { return isEquality(); } 1018 1019 /// isRelational - Return true if the predicate is relational (not EQ or NE). 1020 /// 1021 bool isRelational() const { 1022 return !isEquality(); 1023 } 1024 1025 /// isRelational - Return true if the predicate is relational (not EQ or NE). 1026 /// 1027 static bool isRelational(Predicate P) { 1028 return !isEquality(P); 1029 } 1030 1031 /// Initialize a set of values that all satisfy the predicate with C. 1032 /// \brief Make a ConstantRange for a relation with a constant value. 1033 static ConstantRange makeConstantRange(Predicate pred, const APInt &C); 1034 1035 /// Exchange the two operands to this instruction in such a way that it does 1036 /// not modify the semantics of the instruction. The predicate value may be 1037 /// changed to retain the same result if the predicate is order dependent 1038 /// (e.g. ult). 1039 /// \brief Swap operands and adjust predicate. 1040 void swapOperands() { 1041 setPredicate(getSwappedPredicate()); 1042 Op<0>().swap(Op<1>()); 1043 } 1044 1045 // Methods for support type inquiry through isa, cast, and dyn_cast: 1046 static inline bool classof(const Instruction *I) { 1047 return I->getOpcode() == Instruction::ICmp; 1048 } 1049 static inline bool classof(const Value *V) { 1050 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1051 } 1052 1053 }; 1054 1055 //===----------------------------------------------------------------------===// 1056 // FCmpInst Class 1057 //===----------------------------------------------------------------------===// 1058 1059 /// This instruction compares its operands according to the predicate given 1060 /// to the constructor. It only operates on floating point values or packed 1061 /// vectors of floating point values. The operands must be identical types. 1062 /// \brief Represents a floating point comparison operator. 1063 class FCmpInst: public CmpInst { 1064 protected: 1065 /// \brief Clone an identical FCmpInst 1066 virtual FCmpInst *clone_impl() const; 1067 public: 1068 /// \brief Constructor with insert-before-instruction semantics. 1069 FCmpInst( 1070 Instruction *InsertBefore, ///< Where to insert 1071 Predicate pred, ///< The predicate to use for the comparison 1072 Value *LHS, ///< The left-hand-side of the expression 1073 Value *RHS, ///< The right-hand-side of the expression 1074 const Twine &NameStr = "" ///< Name of the instruction 1075 ) : CmpInst(makeCmpResultType(LHS->getType()), 1076 Instruction::FCmp, pred, LHS, RHS, NameStr, 1077 InsertBefore) { 1078 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1079 "Invalid FCmp predicate value"); 1080 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1081 "Both operands to FCmp instruction are not of the same type!"); 1082 // Check that the operands are the right type 1083 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1084 "Invalid operand types for FCmp instruction"); 1085 } 1086 1087 /// \brief Constructor with insert-at-end semantics. 1088 FCmpInst( 1089 BasicBlock &InsertAtEnd, ///< Block to insert into. 1090 Predicate pred, ///< The predicate to use for the comparison 1091 Value *LHS, ///< The left-hand-side of the expression 1092 Value *RHS, ///< The right-hand-side of the expression 1093 const Twine &NameStr = "" ///< Name of the instruction 1094 ) : CmpInst(makeCmpResultType(LHS->getType()), 1095 Instruction::FCmp, pred, LHS, RHS, NameStr, 1096 &InsertAtEnd) { 1097 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1098 "Invalid FCmp predicate value"); 1099 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1100 "Both operands to FCmp instruction are not of the same type!"); 1101 // Check that the operands are the right type 1102 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1103 "Invalid operand types for FCmp instruction"); 1104 } 1105 1106 /// \brief Constructor with no-insertion semantics 1107 FCmpInst( 1108 Predicate pred, ///< The predicate to use for the comparison 1109 Value *LHS, ///< The left-hand-side of the expression 1110 Value *RHS, ///< The right-hand-side of the expression 1111 const Twine &NameStr = "" ///< Name of the instruction 1112 ) : CmpInst(makeCmpResultType(LHS->getType()), 1113 Instruction::FCmp, pred, LHS, RHS, NameStr) { 1114 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1115 "Invalid FCmp predicate value"); 1116 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1117 "Both operands to FCmp instruction are not of the same type!"); 1118 // Check that the operands are the right type 1119 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1120 "Invalid operand types for FCmp instruction"); 1121 } 1122 1123 /// @returns true if the predicate of this instruction is EQ or NE. 1124 /// \brief Determine if this is an equality predicate. 1125 bool isEquality() const { 1126 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE || 1127 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE; 1128 } 1129 1130 /// @returns true if the predicate of this instruction is commutative. 1131 /// \brief Determine if this is a commutative predicate. 1132 bool isCommutative() const { 1133 return isEquality() || 1134 getPredicate() == FCMP_FALSE || 1135 getPredicate() == FCMP_TRUE || 1136 getPredicate() == FCMP_ORD || 1137 getPredicate() == FCMP_UNO; 1138 } 1139 1140 /// @returns true if the predicate is relational (not EQ or NE). 1141 /// \brief Determine if this a relational predicate. 1142 bool isRelational() const { return !isEquality(); } 1143 1144 /// Exchange the two operands to this instruction in such a way that it does 1145 /// not modify the semantics of the instruction. The predicate value may be 1146 /// changed to retain the same result if the predicate is order dependent 1147 /// (e.g. ult). 1148 /// \brief Swap operands and adjust predicate. 1149 void swapOperands() { 1150 setPredicate(getSwappedPredicate()); 1151 Op<0>().swap(Op<1>()); 1152 } 1153 1154 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 1155 static inline bool classof(const Instruction *I) { 1156 return I->getOpcode() == Instruction::FCmp; 1157 } 1158 static inline bool classof(const Value *V) { 1159 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1160 } 1161 }; 1162 1163 //===----------------------------------------------------------------------===// 1164 /// CallInst - This class represents a function call, abstracting a target 1165 /// machine's calling convention. This class uses low bit of the SubClassData 1166 /// field to indicate whether or not this is a tail call. The rest of the bits 1167 /// hold the calling convention of the call. 1168 /// 1169 class CallInst : public Instruction { 1170 AttributeSet AttributeList; ///< parameter attributes for call 1171 CallInst(const CallInst &CI); 1172 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr); 1173 void init(Value *Func, const Twine &NameStr); 1174 1175 /// Construct a CallInst given a range of arguments. 1176 /// \brief Construct a CallInst from a range of arguments 1177 inline CallInst(Value *Func, ArrayRef<Value *> Args, 1178 const Twine &NameStr, Instruction *InsertBefore); 1179 1180 /// Construct a CallInst given a range of arguments. 1181 /// \brief Construct a CallInst from a range of arguments 1182 inline CallInst(Value *Func, ArrayRef<Value *> Args, 1183 const Twine &NameStr, BasicBlock *InsertAtEnd); 1184 1185 CallInst(Value *F, Value *Actual, const Twine &NameStr, 1186 Instruction *InsertBefore); 1187 CallInst(Value *F, Value *Actual, const Twine &NameStr, 1188 BasicBlock *InsertAtEnd); 1189 explicit CallInst(Value *F, const Twine &NameStr, 1190 Instruction *InsertBefore); 1191 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd); 1192 protected: 1193 virtual CallInst *clone_impl() const; 1194 public: 1195 static CallInst *Create(Value *Func, 1196 ArrayRef<Value *> Args, 1197 const Twine &NameStr = "", 1198 Instruction *InsertBefore = 0) { 1199 return new(unsigned(Args.size() + 1)) 1200 CallInst(Func, Args, NameStr, InsertBefore); 1201 } 1202 static CallInst *Create(Value *Func, 1203 ArrayRef<Value *> Args, 1204 const Twine &NameStr, BasicBlock *InsertAtEnd) { 1205 return new(unsigned(Args.size() + 1)) 1206 CallInst(Func, Args, NameStr, InsertAtEnd); 1207 } 1208 static CallInst *Create(Value *F, const Twine &NameStr = "", 1209 Instruction *InsertBefore = 0) { 1210 return new(1) CallInst(F, NameStr, InsertBefore); 1211 } 1212 static CallInst *Create(Value *F, const Twine &NameStr, 1213 BasicBlock *InsertAtEnd) { 1214 return new(1) CallInst(F, NameStr, InsertAtEnd); 1215 } 1216 /// CreateMalloc - Generate the IR for a call to malloc: 1217 /// 1. Compute the malloc call's argument as the specified type's size, 1218 /// possibly multiplied by the array size if the array size is not 1219 /// constant 1. 1220 /// 2. Call malloc with that argument. 1221 /// 3. Bitcast the result of the malloc call to the specified type. 1222 static Instruction *CreateMalloc(Instruction *InsertBefore, 1223 Type *IntPtrTy, Type *AllocTy, 1224 Value *AllocSize, Value *ArraySize = 0, 1225 Function* MallocF = 0, 1226 const Twine &Name = ""); 1227 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, 1228 Type *IntPtrTy, Type *AllocTy, 1229 Value *AllocSize, Value *ArraySize = 0, 1230 Function* MallocF = 0, 1231 const Twine &Name = ""); 1232 /// CreateFree - Generate the IR for a call to the builtin free function. 1233 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore); 1234 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd); 1235 1236 ~CallInst(); 1237 1238 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; } 1239 void setTailCall(bool isTC = true) { 1240 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 1241 unsigned(isTC)); 1242 } 1243 1244 /// Provide fast operand accessors 1245 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1246 1247 /// getNumArgOperands - Return the number of call arguments. 1248 /// 1249 unsigned getNumArgOperands() const { return getNumOperands() - 1; } 1250 1251 /// getArgOperand/setArgOperand - Return/set the i-th call argument. 1252 /// 1253 Value *getArgOperand(unsigned i) const { return getOperand(i); } 1254 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } 1255 1256 /// getCallingConv/setCallingConv - Get or set the calling convention of this 1257 /// function call. 1258 CallingConv::ID getCallingConv() const { 1259 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1); 1260 } 1261 void setCallingConv(CallingConv::ID CC) { 1262 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | 1263 (static_cast<unsigned>(CC) << 1)); 1264 } 1265 1266 /// getAttributes - Return the parameter attributes for this call. 1267 /// 1268 const AttributeSet &getAttributes() const { return AttributeList; } 1269 1270 /// setAttributes - Set the parameter attributes for this call. 1271 /// 1272 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; } 1273 1274 /// addAttribute - adds the attribute to the list of attributes. 1275 void addAttribute(unsigned i, Attribute::AttrKind attr); 1276 1277 /// removeAttribute - removes the attribute from the list of attributes. 1278 void removeAttribute(unsigned i, Attribute attr); 1279 1280 /// \brief Determine whether this call has the given attribute. 1281 bool hasFnAttr(Attribute::AttrKind A) const { 1282 assert(A != Attribute::NoBuiltin && 1283 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin"); 1284 return hasFnAttrImpl(A); 1285 } 1286 1287 /// \brief Determine whether the call or the callee has the given attributes. 1288 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const; 1289 1290 /// \brief Extract the alignment for a call or parameter (0=unknown). 1291 unsigned getParamAlignment(unsigned i) const { 1292 return AttributeList.getParamAlignment(i); 1293 } 1294 1295 /// \brief Return true if the call should not be treated as a call to a 1296 /// builtin. 1297 bool isNoBuiltin() const { 1298 return hasFnAttrImpl(Attribute::NoBuiltin) && 1299 !hasFnAttrImpl(Attribute::Builtin); 1300 } 1301 1302 /// \brief Return true if the call should not be inlined. 1303 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } 1304 void setIsNoInline() { 1305 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline); 1306 } 1307 1308 /// \brief Return true if the call can return twice 1309 bool canReturnTwice() const { 1310 return hasFnAttr(Attribute::ReturnsTwice); 1311 } 1312 void setCanReturnTwice() { 1313 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice); 1314 } 1315 1316 /// \brief Determine if the call does not access memory. 1317 bool doesNotAccessMemory() const { 1318 return hasFnAttr(Attribute::ReadNone); 1319 } 1320 void setDoesNotAccessMemory() { 1321 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone); 1322 } 1323 1324 /// \brief Determine if the call does not access or only reads memory. 1325 bool onlyReadsMemory() const { 1326 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 1327 } 1328 void setOnlyReadsMemory() { 1329 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly); 1330 } 1331 1332 /// \brief Determine if the call cannot return. 1333 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } 1334 void setDoesNotReturn() { 1335 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn); 1336 } 1337 1338 /// \brief Determine if the call cannot unwind. 1339 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } 1340 void setDoesNotThrow() { 1341 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind); 1342 } 1343 1344 /// \brief Determine if the call cannot be duplicated. 1345 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); } 1346 void setCannotDuplicate() { 1347 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate); 1348 } 1349 1350 /// \brief Determine if the call returns a structure through first 1351 /// pointer argument. 1352 bool hasStructRetAttr() const { 1353 // Be friendly and also check the callee. 1354 return paramHasAttr(1, Attribute::StructRet); 1355 } 1356 1357 /// \brief Determine if any call argument is an aggregate passed by value. 1358 bool hasByValArgument() const { 1359 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 1360 } 1361 1362 /// getCalledFunction - Return the function called, or null if this is an 1363 /// indirect function invocation. 1364 /// 1365 Function *getCalledFunction() const { 1366 return dyn_cast<Function>(Op<-1>()); 1367 } 1368 1369 /// getCalledValue - Get a pointer to the function that is invoked by this 1370 /// instruction. 1371 const Value *getCalledValue() const { return Op<-1>(); } 1372 Value *getCalledValue() { return Op<-1>(); } 1373 1374 /// setCalledFunction - Set the function called. 1375 void setCalledFunction(Value* Fn) { 1376 Op<-1>() = Fn; 1377 } 1378 1379 /// isInlineAsm - Check if this call is an inline asm statement. 1380 bool isInlineAsm() const { 1381 return isa<InlineAsm>(Op<-1>()); 1382 } 1383 1384 // Methods for support type inquiry through isa, cast, and dyn_cast: 1385 static inline bool classof(const Instruction *I) { 1386 return I->getOpcode() == Instruction::Call; 1387 } 1388 static inline bool classof(const Value *V) { 1389 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1390 } 1391 private: 1392 1393 bool hasFnAttrImpl(Attribute::AttrKind A) const; 1394 1395 // Shadow Instruction::setInstructionSubclassData with a private forwarding 1396 // method so that subclasses cannot accidentally use it. 1397 void setInstructionSubclassData(unsigned short D) { 1398 Instruction::setInstructionSubclassData(D); 1399 } 1400 }; 1401 1402 template <> 1403 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> { 1404 }; 1405 1406 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args, 1407 const Twine &NameStr, BasicBlock *InsertAtEnd) 1408 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 1409 ->getElementType())->getReturnType(), 1410 Instruction::Call, 1411 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1), 1412 unsigned(Args.size() + 1), InsertAtEnd) { 1413 init(Func, Args, NameStr); 1414 } 1415 1416 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args, 1417 const Twine &NameStr, Instruction *InsertBefore) 1418 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 1419 ->getElementType())->getReturnType(), 1420 Instruction::Call, 1421 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1), 1422 unsigned(Args.size() + 1), InsertBefore) { 1423 init(Func, Args, NameStr); 1424 } 1425 1426 1427 // Note: if you get compile errors about private methods then 1428 // please update your code to use the high-level operand 1429 // interfaces. See line 943 above. 1430 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value) 1431 1432 //===----------------------------------------------------------------------===// 1433 // SelectInst Class 1434 //===----------------------------------------------------------------------===// 1435 1436 /// SelectInst - This class represents the LLVM 'select' instruction. 1437 /// 1438 class SelectInst : public Instruction { 1439 void init(Value *C, Value *S1, Value *S2) { 1440 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select"); 1441 Op<0>() = C; 1442 Op<1>() = S1; 1443 Op<2>() = S2; 1444 } 1445 1446 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1447 Instruction *InsertBefore) 1448 : Instruction(S1->getType(), Instruction::Select, 1449 &Op<0>(), 3, InsertBefore) { 1450 init(C, S1, S2); 1451 setName(NameStr); 1452 } 1453 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1454 BasicBlock *InsertAtEnd) 1455 : Instruction(S1->getType(), Instruction::Select, 1456 &Op<0>(), 3, InsertAtEnd) { 1457 init(C, S1, S2); 1458 setName(NameStr); 1459 } 1460 protected: 1461 virtual SelectInst *clone_impl() const; 1462 public: 1463 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1464 const Twine &NameStr = "", 1465 Instruction *InsertBefore = 0) { 1466 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); 1467 } 1468 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1469 const Twine &NameStr, 1470 BasicBlock *InsertAtEnd) { 1471 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); 1472 } 1473 1474 const Value *getCondition() const { return Op<0>(); } 1475 const Value *getTrueValue() const { return Op<1>(); } 1476 const Value *getFalseValue() const { return Op<2>(); } 1477 Value *getCondition() { return Op<0>(); } 1478 Value *getTrueValue() { return Op<1>(); } 1479 Value *getFalseValue() { return Op<2>(); } 1480 1481 /// areInvalidOperands - Return a string if the specified operands are invalid 1482 /// for a select operation, otherwise return null. 1483 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); 1484 1485 /// Transparently provide more efficient getOperand methods. 1486 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1487 1488 OtherOps getOpcode() const { 1489 return static_cast<OtherOps>(Instruction::getOpcode()); 1490 } 1491 1492 // Methods for support type inquiry through isa, cast, and dyn_cast: 1493 static inline bool classof(const Instruction *I) { 1494 return I->getOpcode() == Instruction::Select; 1495 } 1496 static inline bool classof(const Value *V) { 1497 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1498 } 1499 }; 1500 1501 template <> 1502 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { 1503 }; 1504 1505 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value) 1506 1507 //===----------------------------------------------------------------------===// 1508 // VAArgInst Class 1509 //===----------------------------------------------------------------------===// 1510 1511 /// VAArgInst - This class represents the va_arg llvm instruction, which returns 1512 /// an argument of the specified type given a va_list and increments that list 1513 /// 1514 class VAArgInst : public UnaryInstruction { 1515 protected: 1516 virtual VAArgInst *clone_impl() const; 1517 1518 public: 1519 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", 1520 Instruction *InsertBefore = 0) 1521 : UnaryInstruction(Ty, VAArg, List, InsertBefore) { 1522 setName(NameStr); 1523 } 1524 VAArgInst(Value *List, Type *Ty, const Twine &NameStr, 1525 BasicBlock *InsertAtEnd) 1526 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { 1527 setName(NameStr); 1528 } 1529 1530 Value *getPointerOperand() { return getOperand(0); } 1531 const Value *getPointerOperand() const { return getOperand(0); } 1532 static unsigned getPointerOperandIndex() { return 0U; } 1533 1534 // Methods for support type inquiry through isa, cast, and dyn_cast: 1535 static inline bool classof(const Instruction *I) { 1536 return I->getOpcode() == VAArg; 1537 } 1538 static inline bool classof(const Value *V) { 1539 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1540 } 1541 }; 1542 1543 //===----------------------------------------------------------------------===// 1544 // ExtractElementInst Class 1545 //===----------------------------------------------------------------------===// 1546 1547 /// ExtractElementInst - This instruction extracts a single (scalar) 1548 /// element from a VectorType value 1549 /// 1550 class ExtractElementInst : public Instruction { 1551 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", 1552 Instruction *InsertBefore = 0); 1553 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, 1554 BasicBlock *InsertAtEnd); 1555 protected: 1556 virtual ExtractElementInst *clone_impl() const; 1557 1558 public: 1559 static ExtractElementInst *Create(Value *Vec, Value *Idx, 1560 const Twine &NameStr = "", 1561 Instruction *InsertBefore = 0) { 1562 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); 1563 } 1564 static ExtractElementInst *Create(Value *Vec, Value *Idx, 1565 const Twine &NameStr, 1566 BasicBlock *InsertAtEnd) { 1567 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); 1568 } 1569 1570 /// isValidOperands - Return true if an extractelement instruction can be 1571 /// formed with the specified operands. 1572 static bool isValidOperands(const Value *Vec, const Value *Idx); 1573 1574 Value *getVectorOperand() { return Op<0>(); } 1575 Value *getIndexOperand() { return Op<1>(); } 1576 const Value *getVectorOperand() const { return Op<0>(); } 1577 const Value *getIndexOperand() const { return Op<1>(); } 1578 1579 VectorType *getVectorOperandType() const { 1580 return cast<VectorType>(getVectorOperand()->getType()); 1581 } 1582 1583 1584 /// Transparently provide more efficient getOperand methods. 1585 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1586 1587 // Methods for support type inquiry through isa, cast, and dyn_cast: 1588 static inline bool classof(const Instruction *I) { 1589 return I->getOpcode() == Instruction::ExtractElement; 1590 } 1591 static inline bool classof(const Value *V) { 1592 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1593 } 1594 }; 1595 1596 template <> 1597 struct OperandTraits<ExtractElementInst> : 1598 public FixedNumOperandTraits<ExtractElementInst, 2> { 1599 }; 1600 1601 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value) 1602 1603 //===----------------------------------------------------------------------===// 1604 // InsertElementInst Class 1605 //===----------------------------------------------------------------------===// 1606 1607 /// InsertElementInst - This instruction inserts a single (scalar) 1608 /// element into a VectorType value 1609 /// 1610 class InsertElementInst : public Instruction { 1611 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, 1612 const Twine &NameStr = "", 1613 Instruction *InsertBefore = 0); 1614 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, 1615 const Twine &NameStr, BasicBlock *InsertAtEnd); 1616 protected: 1617 virtual InsertElementInst *clone_impl() const; 1618 1619 public: 1620 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 1621 const Twine &NameStr = "", 1622 Instruction *InsertBefore = 0) { 1623 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); 1624 } 1625 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 1626 const Twine &NameStr, 1627 BasicBlock *InsertAtEnd) { 1628 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); 1629 } 1630 1631 /// isValidOperands - Return true if an insertelement instruction can be 1632 /// formed with the specified operands. 1633 static bool isValidOperands(const Value *Vec, const Value *NewElt, 1634 const Value *Idx); 1635 1636 /// getType - Overload to return most specific vector type. 1637 /// 1638 VectorType *getType() const { 1639 return cast<VectorType>(Instruction::getType()); 1640 } 1641 1642 /// Transparently provide more efficient getOperand methods. 1643 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1644 1645 // Methods for support type inquiry through isa, cast, and dyn_cast: 1646 static inline bool classof(const Instruction *I) { 1647 return I->getOpcode() == Instruction::InsertElement; 1648 } 1649 static inline bool classof(const Value *V) { 1650 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1651 } 1652 }; 1653 1654 template <> 1655 struct OperandTraits<InsertElementInst> : 1656 public FixedNumOperandTraits<InsertElementInst, 3> { 1657 }; 1658 1659 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value) 1660 1661 //===----------------------------------------------------------------------===// 1662 // ShuffleVectorInst Class 1663 //===----------------------------------------------------------------------===// 1664 1665 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two 1666 /// input vectors. 1667 /// 1668 class ShuffleVectorInst : public Instruction { 1669 protected: 1670 virtual ShuffleVectorInst *clone_impl() const; 1671 1672 public: 1673 // allocate space for exactly three operands 1674 void *operator new(size_t s) { 1675 return User::operator new(s, 3); 1676 } 1677 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1678 const Twine &NameStr = "", 1679 Instruction *InsertBefor = 0); 1680 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1681 const Twine &NameStr, BasicBlock *InsertAtEnd); 1682 1683 /// isValidOperands - Return true if a shufflevector instruction can be 1684 /// formed with the specified operands. 1685 static bool isValidOperands(const Value *V1, const Value *V2, 1686 const Value *Mask); 1687 1688 /// getType - Overload to return most specific vector type. 1689 /// 1690 VectorType *getType() const { 1691 return cast<VectorType>(Instruction::getType()); 1692 } 1693 1694 /// Transparently provide more efficient getOperand methods. 1695 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1696 1697 Constant *getMask() const { 1698 return cast<Constant>(getOperand(2)); 1699 } 1700 1701 /// getMaskValue - Return the index from the shuffle mask for the specified 1702 /// output result. This is either -1 if the element is undef or a number less 1703 /// than 2*numelements. 1704 static int getMaskValue(Constant *Mask, unsigned i); 1705 1706 int getMaskValue(unsigned i) const { 1707 return getMaskValue(getMask(), i); 1708 } 1709 1710 /// getShuffleMask - Return the full mask for this instruction, where each 1711 /// element is the element number and undef's are returned as -1. 1712 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result); 1713 1714 void getShuffleMask(SmallVectorImpl<int> &Result) const { 1715 return getShuffleMask(getMask(), Result); 1716 } 1717 1718 SmallVector<int, 16> getShuffleMask() const { 1719 SmallVector<int, 16> Mask; 1720 getShuffleMask(Mask); 1721 return Mask; 1722 } 1723 1724 1725 // Methods for support type inquiry through isa, cast, and dyn_cast: 1726 static inline bool classof(const Instruction *I) { 1727 return I->getOpcode() == Instruction::ShuffleVector; 1728 } 1729 static inline bool classof(const Value *V) { 1730 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1731 } 1732 }; 1733 1734 template <> 1735 struct OperandTraits<ShuffleVectorInst> : 1736 public FixedNumOperandTraits<ShuffleVectorInst, 3> { 1737 }; 1738 1739 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value) 1740 1741 //===----------------------------------------------------------------------===// 1742 // ExtractValueInst Class 1743 //===----------------------------------------------------------------------===// 1744 1745 /// ExtractValueInst - This instruction extracts a struct member or array 1746 /// element value from an aggregate value. 1747 /// 1748 class ExtractValueInst : public UnaryInstruction { 1749 SmallVector<unsigned, 4> Indices; 1750 1751 ExtractValueInst(const ExtractValueInst &EVI); 1752 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); 1753 1754 /// Constructors - Create a extractvalue instruction with a base aggregate 1755 /// value and a list of indices. The first ctor can optionally insert before 1756 /// an existing instruction, the second appends the new instruction to the 1757 /// specified BasicBlock. 1758 inline ExtractValueInst(Value *Agg, 1759 ArrayRef<unsigned> Idxs, 1760 const Twine &NameStr, 1761 Instruction *InsertBefore); 1762 inline ExtractValueInst(Value *Agg, 1763 ArrayRef<unsigned> Idxs, 1764 const Twine &NameStr, BasicBlock *InsertAtEnd); 1765 1766 // allocate space for exactly one operand 1767 void *operator new(size_t s) { 1768 return User::operator new(s, 1); 1769 } 1770 protected: 1771 virtual ExtractValueInst *clone_impl() const; 1772 1773 public: 1774 static ExtractValueInst *Create(Value *Agg, 1775 ArrayRef<unsigned> Idxs, 1776 const Twine &NameStr = "", 1777 Instruction *InsertBefore = 0) { 1778 return new 1779 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); 1780 } 1781 static ExtractValueInst *Create(Value *Agg, 1782 ArrayRef<unsigned> Idxs, 1783 const Twine &NameStr, 1784 BasicBlock *InsertAtEnd) { 1785 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); 1786 } 1787 1788 /// getIndexedType - Returns the type of the element that would be extracted 1789 /// with an extractvalue instruction with the specified parameters. 1790 /// 1791 /// Null is returned if the indices are invalid for the specified type. 1792 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); 1793 1794 typedef const unsigned* idx_iterator; 1795 inline idx_iterator idx_begin() const { return Indices.begin(); } 1796 inline idx_iterator idx_end() const { return Indices.end(); } 1797 1798 Value *getAggregateOperand() { 1799 return getOperand(0); 1800 } 1801 const Value *getAggregateOperand() const { 1802 return getOperand(0); 1803 } 1804 static unsigned getAggregateOperandIndex() { 1805 return 0U; // get index for modifying correct operand 1806 } 1807 1808 ArrayRef<unsigned> getIndices() const { 1809 return Indices; 1810 } 1811 1812 unsigned getNumIndices() const { 1813 return (unsigned)Indices.size(); 1814 } 1815 1816 bool hasIndices() const { 1817 return true; 1818 } 1819 1820 // Methods for support type inquiry through isa, cast, and dyn_cast: 1821 static inline bool classof(const Instruction *I) { 1822 return I->getOpcode() == Instruction::ExtractValue; 1823 } 1824 static inline bool classof(const Value *V) { 1825 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1826 } 1827 }; 1828 1829 ExtractValueInst::ExtractValueInst(Value *Agg, 1830 ArrayRef<unsigned> Idxs, 1831 const Twine &NameStr, 1832 Instruction *InsertBefore) 1833 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 1834 ExtractValue, Agg, InsertBefore) { 1835 init(Idxs, NameStr); 1836 } 1837 ExtractValueInst::ExtractValueInst(Value *Agg, 1838 ArrayRef<unsigned> Idxs, 1839 const Twine &NameStr, 1840 BasicBlock *InsertAtEnd) 1841 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 1842 ExtractValue, Agg, InsertAtEnd) { 1843 init(Idxs, NameStr); 1844 } 1845 1846 1847 //===----------------------------------------------------------------------===// 1848 // InsertValueInst Class 1849 //===----------------------------------------------------------------------===// 1850 1851 /// InsertValueInst - This instruction inserts a struct field of array element 1852 /// value into an aggregate value. 1853 /// 1854 class InsertValueInst : public Instruction { 1855 SmallVector<unsigned, 4> Indices; 1856 1857 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 1858 InsertValueInst(const InsertValueInst &IVI); 1859 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 1860 const Twine &NameStr); 1861 1862 /// Constructors - Create a insertvalue instruction with a base aggregate 1863 /// value, a value to insert, and a list of indices. The first ctor can 1864 /// optionally insert before an existing instruction, the second appends 1865 /// the new instruction to the specified BasicBlock. 1866 inline InsertValueInst(Value *Agg, Value *Val, 1867 ArrayRef<unsigned> Idxs, 1868 const Twine &NameStr, 1869 Instruction *InsertBefore); 1870 inline InsertValueInst(Value *Agg, Value *Val, 1871 ArrayRef<unsigned> Idxs, 1872 const Twine &NameStr, BasicBlock *InsertAtEnd); 1873 1874 /// Constructors - These two constructors are convenience methods because one 1875 /// and two index insertvalue instructions are so common. 1876 InsertValueInst(Value *Agg, Value *Val, 1877 unsigned Idx, const Twine &NameStr = "", 1878 Instruction *InsertBefore = 0); 1879 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, 1880 const Twine &NameStr, BasicBlock *InsertAtEnd); 1881 protected: 1882 virtual InsertValueInst *clone_impl() const; 1883 public: 1884 // allocate space for exactly two operands 1885 void *operator new(size_t s) { 1886 return User::operator new(s, 2); 1887 } 1888 1889 static InsertValueInst *Create(Value *Agg, Value *Val, 1890 ArrayRef<unsigned> Idxs, 1891 const Twine &NameStr = "", 1892 Instruction *InsertBefore = 0) { 1893 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); 1894 } 1895 static InsertValueInst *Create(Value *Agg, Value *Val, 1896 ArrayRef<unsigned> Idxs, 1897 const Twine &NameStr, 1898 BasicBlock *InsertAtEnd) { 1899 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); 1900 } 1901 1902 /// Transparently provide more efficient getOperand methods. 1903 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1904 1905 typedef const unsigned* idx_iterator; 1906 inline idx_iterator idx_begin() const { return Indices.begin(); } 1907 inline idx_iterator idx_end() const { return Indices.end(); } 1908 1909 Value *getAggregateOperand() { 1910 return getOperand(0); 1911 } 1912 const Value *getAggregateOperand() const { 1913 return getOperand(0); 1914 } 1915 static unsigned getAggregateOperandIndex() { 1916 return 0U; // get index for modifying correct operand 1917 } 1918 1919 Value *getInsertedValueOperand() { 1920 return getOperand(1); 1921 } 1922 const Value *getInsertedValueOperand() const { 1923 return getOperand(1); 1924 } 1925 static unsigned getInsertedValueOperandIndex() { 1926 return 1U; // get index for modifying correct operand 1927 } 1928 1929 ArrayRef<unsigned> getIndices() const { 1930 return Indices; 1931 } 1932 1933 unsigned getNumIndices() const { 1934 return (unsigned)Indices.size(); 1935 } 1936 1937 bool hasIndices() const { 1938 return true; 1939 } 1940 1941 // Methods for support type inquiry through isa, cast, and dyn_cast: 1942 static inline bool classof(const Instruction *I) { 1943 return I->getOpcode() == Instruction::InsertValue; 1944 } 1945 static inline bool classof(const Value *V) { 1946 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1947 } 1948 }; 1949 1950 template <> 1951 struct OperandTraits<InsertValueInst> : 1952 public FixedNumOperandTraits<InsertValueInst, 2> { 1953 }; 1954 1955 InsertValueInst::InsertValueInst(Value *Agg, 1956 Value *Val, 1957 ArrayRef<unsigned> Idxs, 1958 const Twine &NameStr, 1959 Instruction *InsertBefore) 1960 : Instruction(Agg->getType(), InsertValue, 1961 OperandTraits<InsertValueInst>::op_begin(this), 1962 2, InsertBefore) { 1963 init(Agg, Val, Idxs, NameStr); 1964 } 1965 InsertValueInst::InsertValueInst(Value *Agg, 1966 Value *Val, 1967 ArrayRef<unsigned> Idxs, 1968 const Twine &NameStr, 1969 BasicBlock *InsertAtEnd) 1970 : Instruction(Agg->getType(), InsertValue, 1971 OperandTraits<InsertValueInst>::op_begin(this), 1972 2, InsertAtEnd) { 1973 init(Agg, Val, Idxs, NameStr); 1974 } 1975 1976 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value) 1977 1978 //===----------------------------------------------------------------------===// 1979 // PHINode Class 1980 //===----------------------------------------------------------------------===// 1981 1982 // PHINode - The PHINode class is used to represent the magical mystical PHI 1983 // node, that can not exist in nature, but can be synthesized in a computer 1984 // scientist's overactive imagination. 1985 // 1986 class PHINode : public Instruction { 1987 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 1988 /// ReservedSpace - The number of operands actually allocated. NumOperands is 1989 /// the number actually in use. 1990 unsigned ReservedSpace; 1991 PHINode(const PHINode &PN); 1992 // allocate space for exactly zero operands 1993 void *operator new(size_t s) { 1994 return User::operator new(s, 0); 1995 } 1996 explicit PHINode(Type *Ty, unsigned NumReservedValues, 1997 const Twine &NameStr = "", Instruction *InsertBefore = 0) 1998 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore), 1999 ReservedSpace(NumReservedValues) { 2000 setName(NameStr); 2001 OperandList = allocHungoffUses(ReservedSpace); 2002 } 2003 2004 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, 2005 BasicBlock *InsertAtEnd) 2006 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd), 2007 ReservedSpace(NumReservedValues) { 2008 setName(NameStr); 2009 OperandList = allocHungoffUses(ReservedSpace); 2010 } 2011 protected: 2012 // allocHungoffUses - this is more complicated than the generic 2013 // User::allocHungoffUses, because we have to allocate Uses for the incoming 2014 // values and pointers to the incoming blocks, all in one allocation. 2015 Use *allocHungoffUses(unsigned) const; 2016 2017 virtual PHINode *clone_impl() const; 2018 public: 2019 /// Constructors - NumReservedValues is a hint for the number of incoming 2020 /// edges that this phi node will have (use 0 if you really have no idea). 2021 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 2022 const Twine &NameStr = "", 2023 Instruction *InsertBefore = 0) { 2024 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); 2025 } 2026 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 2027 const Twine &NameStr, BasicBlock *InsertAtEnd) { 2028 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); 2029 } 2030 ~PHINode(); 2031 2032 /// Provide fast operand accessors 2033 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2034 2035 // Block iterator interface. This provides access to the list of incoming 2036 // basic blocks, which parallels the list of incoming values. 2037 2038 typedef BasicBlock **block_iterator; 2039 typedef BasicBlock * const *const_block_iterator; 2040 2041 block_iterator block_begin() { 2042 Use::UserRef *ref = 2043 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace); 2044 return reinterpret_cast<block_iterator>(ref + 1); 2045 } 2046 2047 const_block_iterator block_begin() const { 2048 const Use::UserRef *ref = 2049 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace); 2050 return reinterpret_cast<const_block_iterator>(ref + 1); 2051 } 2052 2053 block_iterator block_end() { 2054 return block_begin() + getNumOperands(); 2055 } 2056 2057 const_block_iterator block_end() const { 2058 return block_begin() + getNumOperands(); 2059 } 2060 2061 /// getNumIncomingValues - Return the number of incoming edges 2062 /// 2063 unsigned getNumIncomingValues() const { return getNumOperands(); } 2064 2065 /// getIncomingValue - Return incoming value number x 2066 /// 2067 Value *getIncomingValue(unsigned i) const { 2068 return getOperand(i); 2069 } 2070 void setIncomingValue(unsigned i, Value *V) { 2071 setOperand(i, V); 2072 } 2073 static unsigned getOperandNumForIncomingValue(unsigned i) { 2074 return i; 2075 } 2076 static unsigned getIncomingValueNumForOperand(unsigned i) { 2077 return i; 2078 } 2079 2080 /// getIncomingBlock - Return incoming basic block number @p i. 2081 /// 2082 BasicBlock *getIncomingBlock(unsigned i) const { 2083 return block_begin()[i]; 2084 } 2085 2086 /// getIncomingBlock - Return incoming basic block corresponding 2087 /// to an operand of the PHI. 2088 /// 2089 BasicBlock *getIncomingBlock(const Use &U) const { 2090 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?"); 2091 return getIncomingBlock(unsigned(&U - op_begin())); 2092 } 2093 2094 /// getIncomingBlock - Return incoming basic block corresponding 2095 /// to value use iterator. 2096 /// 2097 template <typename U> 2098 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const { 2099 return getIncomingBlock(I.getUse()); 2100 } 2101 2102 void setIncomingBlock(unsigned i, BasicBlock *BB) { 2103 block_begin()[i] = BB; 2104 } 2105 2106 /// addIncoming - Add an incoming value to the end of the PHI list 2107 /// 2108 void addIncoming(Value *V, BasicBlock *BB) { 2109 assert(V && "PHI node got a null value!"); 2110 assert(BB && "PHI node got a null basic block!"); 2111 assert(getType() == V->getType() && 2112 "All operands to PHI node must be the same type as the PHI node!"); 2113 if (NumOperands == ReservedSpace) 2114 growOperands(); // Get more space! 2115 // Initialize some new operands. 2116 ++NumOperands; 2117 setIncomingValue(NumOperands - 1, V); 2118 setIncomingBlock(NumOperands - 1, BB); 2119 } 2120 2121 /// removeIncomingValue - Remove an incoming value. This is useful if a 2122 /// predecessor basic block is deleted. The value removed is returned. 2123 /// 2124 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty 2125 /// is true), the PHI node is destroyed and any uses of it are replaced with 2126 /// dummy values. The only time there should be zero incoming values to a PHI 2127 /// node is when the block is dead, so this strategy is sound. 2128 /// 2129 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); 2130 2131 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { 2132 int Idx = getBasicBlockIndex(BB); 2133 assert(Idx >= 0 && "Invalid basic block argument to remove!"); 2134 return removeIncomingValue(Idx, DeletePHIIfEmpty); 2135 } 2136 2137 /// getBasicBlockIndex - Return the first index of the specified basic 2138 /// block in the value list for this PHI. Returns -1 if no instance. 2139 /// 2140 int getBasicBlockIndex(const BasicBlock *BB) const { 2141 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 2142 if (block_begin()[i] == BB) 2143 return i; 2144 return -1; 2145 } 2146 2147 Value *getIncomingValueForBlock(const BasicBlock *BB) const { 2148 int Idx = getBasicBlockIndex(BB); 2149 assert(Idx >= 0 && "Invalid basic block argument!"); 2150 return getIncomingValue(Idx); 2151 } 2152 2153 /// hasConstantValue - If the specified PHI node always merges together the 2154 /// same value, return the value, otherwise return null. 2155 Value *hasConstantValue() const; 2156 2157 /// Methods for support type inquiry through isa, cast, and dyn_cast: 2158 static inline bool classof(const Instruction *I) { 2159 return I->getOpcode() == Instruction::PHI; 2160 } 2161 static inline bool classof(const Value *V) { 2162 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2163 } 2164 private: 2165 void growOperands(); 2166 }; 2167 2168 template <> 2169 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { 2170 }; 2171 2172 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value) 2173 2174 //===----------------------------------------------------------------------===// 2175 // LandingPadInst Class 2176 //===----------------------------------------------------------------------===// 2177 2178 //===--------------------------------------------------------------------------- 2179 /// LandingPadInst - The landingpad instruction holds all of the information 2180 /// necessary to generate correct exception handling. The landingpad instruction 2181 /// cannot be moved from the top of a landing pad block, which itself is 2182 /// accessible only from the 'unwind' edge of an invoke. This uses the 2183 /// SubclassData field in Value to store whether or not the landingpad is a 2184 /// cleanup. 2185 /// 2186 class LandingPadInst : public Instruction { 2187 /// ReservedSpace - The number of operands actually allocated. NumOperands is 2188 /// the number actually in use. 2189 unsigned ReservedSpace; 2190 LandingPadInst(const LandingPadInst &LP); 2191 public: 2192 enum ClauseType { Catch, Filter }; 2193 private: 2194 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 2195 // Allocate space for exactly zero operands. 2196 void *operator new(size_t s) { 2197 return User::operator new(s, 0); 2198 } 2199 void growOperands(unsigned Size); 2200 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr); 2201 2202 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn, 2203 unsigned NumReservedValues, const Twine &NameStr, 2204 Instruction *InsertBefore); 2205 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn, 2206 unsigned NumReservedValues, const Twine &NameStr, 2207 BasicBlock *InsertAtEnd); 2208 protected: 2209 virtual LandingPadInst *clone_impl() const; 2210 public: 2211 /// Constructors - NumReservedClauses is a hint for the number of incoming 2212 /// clauses that this landingpad will have (use 0 if you really have no idea). 2213 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn, 2214 unsigned NumReservedClauses, 2215 const Twine &NameStr = "", 2216 Instruction *InsertBefore = 0); 2217 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn, 2218 unsigned NumReservedClauses, 2219 const Twine &NameStr, BasicBlock *InsertAtEnd); 2220 ~LandingPadInst(); 2221 2222 /// Provide fast operand accessors 2223 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2224 2225 /// getPersonalityFn - Get the personality function associated with this 2226 /// landing pad. 2227 Value *getPersonalityFn() const { return getOperand(0); } 2228 2229 /// isCleanup - Return 'true' if this landingpad instruction is a 2230 /// cleanup. I.e., it should be run when unwinding even if its landing pad 2231 /// doesn't catch the exception. 2232 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; } 2233 2234 /// setCleanup - Indicate that this landingpad instruction is a cleanup. 2235 void setCleanup(bool V) { 2236 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 2237 (V ? 1 : 0)); 2238 } 2239 2240 /// addClause - Add a catch or filter clause to the landing pad. 2241 void addClause(Value *ClauseVal); 2242 2243 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter 2244 /// to determine what type of clause this is. 2245 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; } 2246 2247 /// isCatch - Return 'true' if the clause and index Idx is a catch clause. 2248 bool isCatch(unsigned Idx) const { 2249 return !isa<ArrayType>(OperandList[Idx + 1]->getType()); 2250 } 2251 2252 /// isFilter - Return 'true' if the clause and index Idx is a filter clause. 2253 bool isFilter(unsigned Idx) const { 2254 return isa<ArrayType>(OperandList[Idx + 1]->getType()); 2255 } 2256 2257 /// getNumClauses - Get the number of clauses for this landing pad. 2258 unsigned getNumClauses() const { return getNumOperands() - 1; } 2259 2260 /// reserveClauses - Grow the size of the operand list to accommodate the new 2261 /// number of clauses. 2262 void reserveClauses(unsigned Size) { growOperands(Size); } 2263 2264 // Methods for support type inquiry through isa, cast, and dyn_cast: 2265 static inline bool classof(const Instruction *I) { 2266 return I->getOpcode() == Instruction::LandingPad; 2267 } 2268 static inline bool classof(const Value *V) { 2269 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2270 } 2271 }; 2272 2273 template <> 2274 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> { 2275 }; 2276 2277 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value) 2278 2279 //===----------------------------------------------------------------------===// 2280 // ReturnInst Class 2281 //===----------------------------------------------------------------------===// 2282 2283 //===--------------------------------------------------------------------------- 2284 /// ReturnInst - Return a value (possibly void), from a function. Execution 2285 /// does not continue in this function any longer. 2286 /// 2287 class ReturnInst : public TerminatorInst { 2288 ReturnInst(const ReturnInst &RI); 2289 2290 private: 2291 // ReturnInst constructors: 2292 // ReturnInst() - 'ret void' instruction 2293 // ReturnInst( null) - 'ret void' instruction 2294 // ReturnInst(Value* X) - 'ret X' instruction 2295 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I 2296 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I 2297 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B 2298 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B 2299 // 2300 // NOTE: If the Value* passed is of type void then the constructor behaves as 2301 // if it was passed NULL. 2302 explicit ReturnInst(LLVMContext &C, Value *retVal = 0, 2303 Instruction *InsertBefore = 0); 2304 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); 2305 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); 2306 protected: 2307 virtual ReturnInst *clone_impl() const; 2308 public: 2309 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0, 2310 Instruction *InsertBefore = 0) { 2311 return new(!!retVal) ReturnInst(C, retVal, InsertBefore); 2312 } 2313 static ReturnInst* Create(LLVMContext &C, Value *retVal, 2314 BasicBlock *InsertAtEnd) { 2315 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); 2316 } 2317 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { 2318 return new(0) ReturnInst(C, InsertAtEnd); 2319 } 2320 virtual ~ReturnInst(); 2321 2322 /// Provide fast operand accessors 2323 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2324 2325 /// Convenience accessor. Returns null if there is no return value. 2326 Value *getReturnValue() const { 2327 return getNumOperands() != 0 ? getOperand(0) : 0; 2328 } 2329 2330 unsigned getNumSuccessors() const { return 0; } 2331 2332 // Methods for support type inquiry through isa, cast, and dyn_cast: 2333 static inline bool classof(const Instruction *I) { 2334 return (I->getOpcode() == Instruction::Ret); 2335 } 2336 static inline bool classof(const Value *V) { 2337 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2338 } 2339 private: 2340 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2341 virtual unsigned getNumSuccessorsV() const; 2342 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2343 }; 2344 2345 template <> 2346 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { 2347 }; 2348 2349 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value) 2350 2351 //===----------------------------------------------------------------------===// 2352 // BranchInst Class 2353 //===----------------------------------------------------------------------===// 2354 2355 //===--------------------------------------------------------------------------- 2356 /// BranchInst - Conditional or Unconditional Branch instruction. 2357 /// 2358 class BranchInst : public TerminatorInst { 2359 /// Ops list - Branches are strange. The operands are ordered: 2360 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because 2361 /// they don't have to check for cond/uncond branchness. These are mostly 2362 /// accessed relative from op_end(). 2363 BranchInst(const BranchInst &BI); 2364 void AssertOK(); 2365 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): 2366 // BranchInst(BB *B) - 'br B' 2367 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' 2368 // BranchInst(BB* B, Inst *I) - 'br B' insert before I 2369 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I 2370 // BranchInst(BB* B, BB *I) - 'br B' insert at end 2371 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end 2372 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0); 2373 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 2374 Instruction *InsertBefore = 0); 2375 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); 2376 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 2377 BasicBlock *InsertAtEnd); 2378 protected: 2379 virtual BranchInst *clone_impl() const; 2380 public: 2381 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) { 2382 return new(1) BranchInst(IfTrue, InsertBefore); 2383 } 2384 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 2385 Value *Cond, Instruction *InsertBefore = 0) { 2386 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); 2387 } 2388 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { 2389 return new(1) BranchInst(IfTrue, InsertAtEnd); 2390 } 2391 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 2392 Value *Cond, BasicBlock *InsertAtEnd) { 2393 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); 2394 } 2395 2396 /// Transparently provide more efficient getOperand methods. 2397 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2398 2399 bool isUnconditional() const { return getNumOperands() == 1; } 2400 bool isConditional() const { return getNumOperands() == 3; } 2401 2402 Value *getCondition() const { 2403 assert(isConditional() && "Cannot get condition of an uncond branch!"); 2404 return Op<-3>(); 2405 } 2406 2407 void setCondition(Value *V) { 2408 assert(isConditional() && "Cannot set condition of unconditional branch!"); 2409 Op<-3>() = V; 2410 } 2411 2412 unsigned getNumSuccessors() const { return 1+isConditional(); } 2413 2414 BasicBlock *getSuccessor(unsigned i) const { 2415 assert(i < getNumSuccessors() && "Successor # out of range for Branch!"); 2416 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); 2417 } 2418 2419 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 2420 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!"); 2421 *(&Op<-1>() - idx) = (Value*)NewSucc; 2422 } 2423 2424 /// \brief Swap the successors of this branch instruction. 2425 /// 2426 /// Swaps the successors of the branch instruction. This also swaps any 2427 /// branch weight metadata associated with the instruction so that it 2428 /// continues to map correctly to each operand. 2429 void swapSuccessors(); 2430 2431 // Methods for support type inquiry through isa, cast, and dyn_cast: 2432 static inline bool classof(const Instruction *I) { 2433 return (I->getOpcode() == Instruction::Br); 2434 } 2435 static inline bool classof(const Value *V) { 2436 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2437 } 2438 private: 2439 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2440 virtual unsigned getNumSuccessorsV() const; 2441 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2442 }; 2443 2444 template <> 2445 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { 2446 }; 2447 2448 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value) 2449 2450 //===----------------------------------------------------------------------===// 2451 // SwitchInst Class 2452 //===----------------------------------------------------------------------===// 2453 2454 //===--------------------------------------------------------------------------- 2455 /// SwitchInst - Multiway switch 2456 /// 2457 class SwitchInst : public TerminatorInst { 2458 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 2459 unsigned ReservedSpace; 2460 // Operands format: 2461 // Operand[0] = Value to switch on 2462 // Operand[1] = Default basic block destination 2463 // Operand[2n ] = Value to match 2464 // Operand[2n+1] = BasicBlock to go to on match 2465 2466 // Store case values separately from operands list. We needn't User-Use 2467 // concept here, since it is just a case value, it will always constant, 2468 // and case value couldn't reused with another instructions/values. 2469 // Additionally: 2470 // It allows us to use custom type for case values that is not inherited 2471 // from Value. Since case value is a complex type that implements 2472 // the subset of integers, we needn't extract sub-constants within 2473 // slow getAggregateElement method. 2474 // For case values we will use std::list to by two reasons: 2475 // 1. It allows to add/remove cases without whole collection reallocation. 2476 // 2. In most of cases we needn't random access. 2477 // Currently case values are also stored in Operands List, but it will moved 2478 // out in future commits. 2479 typedef std::list<IntegersSubset> Subsets; 2480 typedef Subsets::iterator SubsetsIt; 2481 typedef Subsets::const_iterator SubsetsConstIt; 2482 2483 Subsets TheSubsets; 2484 2485 SwitchInst(const SwitchInst &SI); 2486 void init(Value *Value, BasicBlock *Default, unsigned NumReserved); 2487 void growOperands(); 2488 // allocate space for exactly zero operands 2489 void *operator new(size_t s) { 2490 return User::operator new(s, 0); 2491 } 2492 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 2493 /// switch on and a default destination. The number of additional cases can 2494 /// be specified here to make memory allocation more efficient. This 2495 /// constructor can also autoinsert before another instruction. 2496 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 2497 Instruction *InsertBefore); 2498 2499 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 2500 /// switch on and a default destination. The number of additional cases can 2501 /// be specified here to make memory allocation more efficient. This 2502 /// constructor also autoinserts at the end of the specified BasicBlock. 2503 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 2504 BasicBlock *InsertAtEnd); 2505 protected: 2506 virtual SwitchInst *clone_impl() const; 2507 public: 2508 2509 // FIXME: Currently there are a lot of unclean template parameters, 2510 // we need to make refactoring in future. 2511 // All these parameters are used to implement both iterator and const_iterator 2512 // without code duplication. 2513 // SwitchInstTy may be "const SwitchInst" or "SwitchInst" 2514 // ConstantIntTy may be "const ConstantInt" or "ConstantInt" 2515 // SubsetsItTy may be SubsetsConstIt or SubsetsIt 2516 // BasicBlockTy may be "const BasicBlock" or "BasicBlock" 2517 template <class SwitchInstTy, class ConstantIntTy, 2518 class SubsetsItTy, class BasicBlockTy> 2519 class CaseIteratorT; 2520 2521 typedef CaseIteratorT<const SwitchInst, const ConstantInt, 2522 SubsetsConstIt, const BasicBlock> ConstCaseIt; 2523 class CaseIt; 2524 2525 // -2 2526 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); 2527 2528 static SwitchInst *Create(Value *Value, BasicBlock *Default, 2529 unsigned NumCases, Instruction *InsertBefore = 0) { 2530 return new SwitchInst(Value, Default, NumCases, InsertBefore); 2531 } 2532 static SwitchInst *Create(Value *Value, BasicBlock *Default, 2533 unsigned NumCases, BasicBlock *InsertAtEnd) { 2534 return new SwitchInst(Value, Default, NumCases, InsertAtEnd); 2535 } 2536 2537 ~SwitchInst(); 2538 2539 /// Provide fast operand accessors 2540 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2541 2542 // Accessor Methods for Switch stmt 2543 Value *getCondition() const { return getOperand(0); } 2544 void setCondition(Value *V) { setOperand(0, V); } 2545 2546 BasicBlock *getDefaultDest() const { 2547 return cast<BasicBlock>(getOperand(1)); 2548 } 2549 2550 void setDefaultDest(BasicBlock *DefaultCase) { 2551 setOperand(1, reinterpret_cast<Value*>(DefaultCase)); 2552 } 2553 2554 /// getNumCases - return the number of 'cases' in this switch instruction, 2555 /// except the default case 2556 unsigned getNumCases() const { 2557 return getNumOperands()/2 - 1; 2558 } 2559 2560 /// Returns a read/write iterator that points to the first 2561 /// case in SwitchInst. 2562 CaseIt case_begin() { 2563 return CaseIt(this, 0, TheSubsets.begin()); 2564 } 2565 /// Returns a read-only iterator that points to the first 2566 /// case in the SwitchInst. 2567 ConstCaseIt case_begin() const { 2568 return ConstCaseIt(this, 0, TheSubsets.begin()); 2569 } 2570 2571 /// Returns a read/write iterator that points one past the last 2572 /// in the SwitchInst. 2573 CaseIt case_end() { 2574 return CaseIt(this, getNumCases(), TheSubsets.end()); 2575 } 2576 /// Returns a read-only iterator that points one past the last 2577 /// in the SwitchInst. 2578 ConstCaseIt case_end() const { 2579 return ConstCaseIt(this, getNumCases(), TheSubsets.end()); 2580 } 2581 /// Returns an iterator that points to the default case. 2582 /// Note: this iterator allows to resolve successor only. Attempt 2583 /// to resolve case value causes an assertion. 2584 /// Also note, that increment and decrement also causes an assertion and 2585 /// makes iterator invalid. 2586 CaseIt case_default() { 2587 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end()); 2588 } 2589 ConstCaseIt case_default() const { 2590 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end()); 2591 } 2592 2593 /// findCaseValue - Search all of the case values for the specified constant. 2594 /// If it is explicitly handled, return the case iterator of it, otherwise 2595 /// return default case iterator to indicate 2596 /// that it is handled by the default handler. 2597 CaseIt findCaseValue(const ConstantInt *C) { 2598 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) 2599 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C))) 2600 return i; 2601 return case_default(); 2602 } 2603 ConstCaseIt findCaseValue(const ConstantInt *C) const { 2604 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i) 2605 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C))) 2606 return i; 2607 return case_default(); 2608 } 2609 2610 /// findCaseDest - Finds the unique case value for a given successor. Returns 2611 /// null if the successor is not found, not unique, or is the default case. 2612 ConstantInt *findCaseDest(BasicBlock *BB) { 2613 if (BB == getDefaultDest()) return NULL; 2614 2615 ConstantInt *CI = NULL; 2616 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) { 2617 if (i.getCaseSuccessor() == BB) { 2618 if (CI) return NULL; // Multiple cases lead to BB. 2619 else CI = i.getCaseValue(); 2620 } 2621 } 2622 return CI; 2623 } 2624 2625 /// addCase - Add an entry to the switch instruction... 2626 /// Note: 2627 /// This action invalidates case_end(). Old case_end() iterator will 2628 /// point to the added case. 2629 void addCase(ConstantInt *OnVal, BasicBlock *Dest); 2630 2631 /// addCase - Add an entry to the switch instruction. 2632 /// Note: 2633 /// This action invalidates case_end(). Old case_end() iterator will 2634 /// point to the added case. 2635 void addCase(IntegersSubset& OnVal, BasicBlock *Dest); 2636 2637 /// removeCase - This method removes the specified case and its successor 2638 /// from the switch instruction. Note that this operation may reorder the 2639 /// remaining cases at index idx and above. 2640 /// Note: 2641 /// This action invalidates iterators for all cases following the one removed, 2642 /// including the case_end() iterator. 2643 void removeCase(CaseIt& i); 2644 2645 unsigned getNumSuccessors() const { return getNumOperands()/2; } 2646 BasicBlock *getSuccessor(unsigned idx) const { 2647 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!"); 2648 return cast<BasicBlock>(getOperand(idx*2+1)); 2649 } 2650 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 2651 assert(idx < getNumSuccessors() && "Successor # out of range for switch!"); 2652 setOperand(idx*2+1, (Value*)NewSucc); 2653 } 2654 2655 uint16_t hash() const { 2656 uint32_t NumberOfCases = (uint32_t)getNumCases(); 2657 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16); 2658 for (ConstCaseIt i = case_begin(), e = case_end(); 2659 i != e; ++i) { 2660 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems(); 2661 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16); 2662 } 2663 return Hash; 2664 } 2665 2666 // Case iterators definition. 2667 2668 template <class SwitchInstTy, class ConstantIntTy, 2669 class SubsetsItTy, class BasicBlockTy> 2670 class CaseIteratorT { 2671 protected: 2672 2673 SwitchInstTy *SI; 2674 unsigned Index; 2675 SubsetsItTy SubsetIt; 2676 2677 /// Initializes case iterator for given SwitchInst and for given 2678 /// case number. 2679 friend class SwitchInst; 2680 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex, 2681 SubsetsItTy CaseValueIt) { 2682 this->SI = SI; 2683 Index = SuccessorIndex; 2684 this->SubsetIt = CaseValueIt; 2685 } 2686 2687 public: 2688 typedef typename SubsetsItTy::reference IntegersSubsetRef; 2689 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, 2690 SubsetsItTy, BasicBlockTy> Self; 2691 2692 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) { 2693 this->SI = SI; 2694 Index = CaseNum; 2695 SubsetIt = SI->TheSubsets.begin(); 2696 std::advance(SubsetIt, CaseNum); 2697 } 2698 2699 2700 /// Initializes case iterator for given SwitchInst and for given 2701 /// TerminatorInst's successor index. 2702 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) { 2703 assert(SuccessorIndex < SI->getNumSuccessors() && 2704 "Successor index # out of range!"); 2705 return SuccessorIndex != 0 ? 2706 Self(SI, SuccessorIndex - 1) : 2707 Self(SI, DefaultPseudoIndex); 2708 } 2709 2710 /// Resolves case value for current case. 2711 ConstantIntTy *getCaseValue() { 2712 assert(Index < SI->getNumCases() && "Index out the number of cases."); 2713 IntegersSubsetRef CaseRanges = *SubsetIt; 2714 2715 // FIXME: Currently we work with ConstantInt based cases. 2716 // So return CaseValue as ConstantInt. 2717 return CaseRanges.getSingleNumber(0).toConstantInt(); 2718 } 2719 2720 /// Resolves case value for current case. 2721 IntegersSubsetRef getCaseValueEx() { 2722 assert(Index < SI->getNumCases() && "Index out the number of cases."); 2723 return *SubsetIt; 2724 } 2725 2726 /// Resolves successor for current case. 2727 BasicBlockTy *getCaseSuccessor() { 2728 assert((Index < SI->getNumCases() || 2729 Index == DefaultPseudoIndex) && 2730 "Index out the number of cases."); 2731 return SI->getSuccessor(getSuccessorIndex()); 2732 } 2733 2734 /// Returns number of current case. 2735 unsigned getCaseIndex() const { return Index; } 2736 2737 /// Returns TerminatorInst's successor index for current case successor. 2738 unsigned getSuccessorIndex() const { 2739 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) && 2740 "Index out the number of cases."); 2741 return Index != DefaultPseudoIndex ? Index + 1 : 0; 2742 } 2743 2744 Self operator++() { 2745 // Check index correctness after increment. 2746 // Note: Index == getNumCases() means end(). 2747 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases."); 2748 ++Index; 2749 if (Index == 0) 2750 SubsetIt = SI->TheSubsets.begin(); 2751 else 2752 ++SubsetIt; 2753 return *this; 2754 } 2755 Self operator++(int) { 2756 Self tmp = *this; 2757 ++(*this); 2758 return tmp; 2759 } 2760 Self operator--() { 2761 // Check index correctness after decrement. 2762 // Note: Index == getNumCases() means end(). 2763 // Also allow "-1" iterator here. That will became valid after ++. 2764 unsigned NumCases = SI->getNumCases(); 2765 assert((Index == 0 || Index-1 <= NumCases) && 2766 "Index out the number of cases."); 2767 --Index; 2768 if (Index == NumCases) { 2769 SubsetIt = SI->TheSubsets.end(); 2770 return *this; 2771 } 2772 2773 if (Index != -1U) 2774 --SubsetIt; 2775 2776 return *this; 2777 } 2778 Self operator--(int) { 2779 Self tmp = *this; 2780 --(*this); 2781 return tmp; 2782 } 2783 bool operator==(const Self& RHS) const { 2784 assert(RHS.SI == SI && "Incompatible operators."); 2785 return RHS.Index == Index; 2786 } 2787 bool operator!=(const Self& RHS) const { 2788 assert(RHS.SI == SI && "Incompatible operators."); 2789 return RHS.Index != Index; 2790 } 2791 }; 2792 2793 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, 2794 SubsetsIt, BasicBlock> { 2795 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock> 2796 ParentTy; 2797 2798 protected: 2799 friend class SwitchInst; 2800 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) : 2801 ParentTy(SI, CaseNum, SubsetIt) {} 2802 2803 void updateCaseValueOperand(IntegersSubset& V) { 2804 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V)); 2805 } 2806 2807 public: 2808 2809 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {} 2810 2811 CaseIt(const ParentTy& Src) : ParentTy(Src) {} 2812 2813 /// Sets the new value for current case. 2814 void setValue(ConstantInt *V) { 2815 assert(Index < SI->getNumCases() && "Index out the number of cases."); 2816 IntegersSubsetToBB Mapping; 2817 // FIXME: Currently we work with ConstantInt based cases. 2818 // So inititalize IntItem container directly from ConstantInt. 2819 Mapping.add(IntItem::fromConstantInt(V)); 2820 *SubsetIt = Mapping.getCase(); 2821 updateCaseValueOperand(*SubsetIt); 2822 } 2823 2824 /// Sets the new value for current case. 2825 void setValueEx(IntegersSubset& V) { 2826 assert(Index < SI->getNumCases() && "Index out the number of cases."); 2827 *SubsetIt = V; 2828 updateCaseValueOperand(*SubsetIt); 2829 } 2830 2831 /// Sets the new successor for current case. 2832 void setSuccessor(BasicBlock *S) { 2833 SI->setSuccessor(getSuccessorIndex(), S); 2834 } 2835 }; 2836 2837 // Methods for support type inquiry through isa, cast, and dyn_cast: 2838 2839 static inline bool classof(const Instruction *I) { 2840 return I->getOpcode() == Instruction::Switch; 2841 } 2842 static inline bool classof(const Value *V) { 2843 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2844 } 2845 private: 2846 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2847 virtual unsigned getNumSuccessorsV() const; 2848 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2849 }; 2850 2851 template <> 2852 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { 2853 }; 2854 2855 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value) 2856 2857 2858 //===----------------------------------------------------------------------===// 2859 // IndirectBrInst Class 2860 //===----------------------------------------------------------------------===// 2861 2862 //===--------------------------------------------------------------------------- 2863 /// IndirectBrInst - Indirect Branch Instruction. 2864 /// 2865 class IndirectBrInst : public TerminatorInst { 2866 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 2867 unsigned ReservedSpace; 2868 // Operand[0] = Value to switch on 2869 // Operand[1] = Default basic block destination 2870 // Operand[2n ] = Value to match 2871 // Operand[2n+1] = BasicBlock to go to on match 2872 IndirectBrInst(const IndirectBrInst &IBI); 2873 void init(Value *Address, unsigned NumDests); 2874 void growOperands(); 2875 // allocate space for exactly zero operands 2876 void *operator new(size_t s) { 2877 return User::operator new(s, 0); 2878 } 2879 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 2880 /// Address to jump to. The number of expected destinations can be specified 2881 /// here to make memory allocation more efficient. This constructor can also 2882 /// autoinsert before another instruction. 2883 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); 2884 2885 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 2886 /// Address to jump to. The number of expected destinations can be specified 2887 /// here to make memory allocation more efficient. This constructor also 2888 /// autoinserts at the end of the specified BasicBlock. 2889 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); 2890 protected: 2891 virtual IndirectBrInst *clone_impl() const; 2892 public: 2893 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 2894 Instruction *InsertBefore = 0) { 2895 return new IndirectBrInst(Address, NumDests, InsertBefore); 2896 } 2897 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 2898 BasicBlock *InsertAtEnd) { 2899 return new IndirectBrInst(Address, NumDests, InsertAtEnd); 2900 } 2901 ~IndirectBrInst(); 2902 2903 /// Provide fast operand accessors. 2904 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2905 2906 // Accessor Methods for IndirectBrInst instruction. 2907 Value *getAddress() { return getOperand(0); } 2908 const Value *getAddress() const { return getOperand(0); } 2909 void setAddress(Value *V) { setOperand(0, V); } 2910 2911 2912 /// getNumDestinations - return the number of possible destinations in this 2913 /// indirectbr instruction. 2914 unsigned getNumDestinations() const { return getNumOperands()-1; } 2915 2916 /// getDestination - Return the specified destination. 2917 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } 2918 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } 2919 2920 /// addDestination - Add a destination. 2921 /// 2922 void addDestination(BasicBlock *Dest); 2923 2924 /// removeDestination - This method removes the specified successor from the 2925 /// indirectbr instruction. 2926 void removeDestination(unsigned i); 2927 2928 unsigned getNumSuccessors() const { return getNumOperands()-1; } 2929 BasicBlock *getSuccessor(unsigned i) const { 2930 return cast<BasicBlock>(getOperand(i+1)); 2931 } 2932 void setSuccessor(unsigned i, BasicBlock *NewSucc) { 2933 setOperand(i+1, (Value*)NewSucc); 2934 } 2935 2936 // Methods for support type inquiry through isa, cast, and dyn_cast: 2937 static inline bool classof(const Instruction *I) { 2938 return I->getOpcode() == Instruction::IndirectBr; 2939 } 2940 static inline bool classof(const Value *V) { 2941 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2942 } 2943 private: 2944 virtual BasicBlock *getSuccessorV(unsigned idx) const; 2945 virtual unsigned getNumSuccessorsV() const; 2946 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 2947 }; 2948 2949 template <> 2950 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { 2951 }; 2952 2953 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value) 2954 2955 2956 //===----------------------------------------------------------------------===// 2957 // InvokeInst Class 2958 //===----------------------------------------------------------------------===// 2959 2960 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the 2961 /// calling convention of the call. 2962 /// 2963 class InvokeInst : public TerminatorInst { 2964 AttributeSet AttributeList; 2965 InvokeInst(const InvokeInst &BI); 2966 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2967 ArrayRef<Value *> Args, const Twine &NameStr); 2968 2969 /// Construct an InvokeInst given a range of arguments. 2970 /// 2971 /// \brief Construct an InvokeInst from a range of arguments 2972 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2973 ArrayRef<Value *> Args, unsigned Values, 2974 const Twine &NameStr, Instruction *InsertBefore); 2975 2976 /// Construct an InvokeInst given a range of arguments. 2977 /// 2978 /// \brief Construct an InvokeInst from a range of arguments 2979 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 2980 ArrayRef<Value *> Args, unsigned Values, 2981 const Twine &NameStr, BasicBlock *InsertAtEnd); 2982 protected: 2983 virtual InvokeInst *clone_impl() const; 2984 public: 2985 static InvokeInst *Create(Value *Func, 2986 BasicBlock *IfNormal, BasicBlock *IfException, 2987 ArrayRef<Value *> Args, const Twine &NameStr = "", 2988 Instruction *InsertBefore = 0) { 2989 unsigned Values = unsigned(Args.size()) + 3; 2990 return new(Values) InvokeInst(Func, IfNormal, IfException, Args, 2991 Values, NameStr, InsertBefore); 2992 } 2993 static InvokeInst *Create(Value *Func, 2994 BasicBlock *IfNormal, BasicBlock *IfException, 2995 ArrayRef<Value *> Args, const Twine &NameStr, 2996 BasicBlock *InsertAtEnd) { 2997 unsigned Values = unsigned(Args.size()) + 3; 2998 return new(Values) InvokeInst(Func, IfNormal, IfException, Args, 2999 Values, NameStr, InsertAtEnd); 3000 } 3001 3002 /// Provide fast operand accessors 3003 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3004 3005 /// getNumArgOperands - Return the number of invoke arguments. 3006 /// 3007 unsigned getNumArgOperands() const { return getNumOperands() - 3; } 3008 3009 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument. 3010 /// 3011 Value *getArgOperand(unsigned i) const { return getOperand(i); } 3012 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } 3013 3014 /// getCallingConv/setCallingConv - Get or set the calling convention of this 3015 /// function call. 3016 CallingConv::ID getCallingConv() const { 3017 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction()); 3018 } 3019 void setCallingConv(CallingConv::ID CC) { 3020 setInstructionSubclassData(static_cast<unsigned>(CC)); 3021 } 3022 3023 /// getAttributes - Return the parameter attributes for this invoke. 3024 /// 3025 const AttributeSet &getAttributes() const { return AttributeList; } 3026 3027 /// setAttributes - Set the parameter attributes for this invoke. 3028 /// 3029 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; } 3030 3031 /// addAttribute - adds the attribute to the list of attributes. 3032 void addAttribute(unsigned i, Attribute::AttrKind attr); 3033 3034 /// removeAttribute - removes the attribute from the list of attributes. 3035 void removeAttribute(unsigned i, Attribute attr); 3036 3037 /// \brief Determine whether this call has the NoAlias attribute. 3038 bool hasFnAttr(Attribute::AttrKind A) const { 3039 assert(A != Attribute::NoBuiltin && 3040 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin"); 3041 return hasFnAttrImpl(A); 3042 } 3043 3044 /// \brief Determine whether the call or the callee has the given attributes. 3045 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const; 3046 3047 /// \brief Extract the alignment for a call or parameter (0=unknown). 3048 unsigned getParamAlignment(unsigned i) const { 3049 return AttributeList.getParamAlignment(i); 3050 } 3051 3052 /// \brief Return true if the call should not be treated as a call to a 3053 /// builtin. 3054 bool isNoBuiltin() const { 3055 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have 3056 // to check it by hand. 3057 return hasFnAttrImpl(Attribute::NoBuiltin) && 3058 !hasFnAttrImpl(Attribute::Builtin); 3059 } 3060 3061 /// \brief Return true if the call should not be inlined. 3062 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } 3063 void setIsNoInline() { 3064 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline); 3065 } 3066 3067 /// \brief Determine if the call does not access memory. 3068 bool doesNotAccessMemory() const { 3069 return hasFnAttr(Attribute::ReadNone); 3070 } 3071 void setDoesNotAccessMemory() { 3072 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone); 3073 } 3074 3075 /// \brief Determine if the call does not access or only reads memory. 3076 bool onlyReadsMemory() const { 3077 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 3078 } 3079 void setOnlyReadsMemory() { 3080 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly); 3081 } 3082 3083 /// \brief Determine if the call cannot return. 3084 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } 3085 void setDoesNotReturn() { 3086 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn); 3087 } 3088 3089 /// \brief Determine if the call cannot unwind. 3090 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } 3091 void setDoesNotThrow() { 3092 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind); 3093 } 3094 3095 /// \brief Determine if the call returns a structure through first 3096 /// pointer argument. 3097 bool hasStructRetAttr() const { 3098 // Be friendly and also check the callee. 3099 return paramHasAttr(1, Attribute::StructRet); 3100 } 3101 3102 /// \brief Determine if any call argument is an aggregate passed by value. 3103 bool hasByValArgument() const { 3104 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 3105 } 3106 3107 /// getCalledFunction - Return the function called, or null if this is an 3108 /// indirect function invocation. 3109 /// 3110 Function *getCalledFunction() const { 3111 return dyn_cast<Function>(Op<-3>()); 3112 } 3113 3114 /// getCalledValue - Get a pointer to the function that is invoked by this 3115 /// instruction 3116 const Value *getCalledValue() const { return Op<-3>(); } 3117 Value *getCalledValue() { return Op<-3>(); } 3118 3119 /// setCalledFunction - Set the function called. 3120 void setCalledFunction(Value* Fn) { 3121 Op<-3>() = Fn; 3122 } 3123 3124 // get*Dest - Return the destination basic blocks... 3125 BasicBlock *getNormalDest() const { 3126 return cast<BasicBlock>(Op<-2>()); 3127 } 3128 BasicBlock *getUnwindDest() const { 3129 return cast<BasicBlock>(Op<-1>()); 3130 } 3131 void setNormalDest(BasicBlock *B) { 3132 Op<-2>() = reinterpret_cast<Value*>(B); 3133 } 3134 void setUnwindDest(BasicBlock *B) { 3135 Op<-1>() = reinterpret_cast<Value*>(B); 3136 } 3137 3138 /// getLandingPadInst - Get the landingpad instruction from the landing pad 3139 /// block (the unwind destination). 3140 LandingPadInst *getLandingPadInst() const; 3141 3142 BasicBlock *getSuccessor(unsigned i) const { 3143 assert(i < 2 && "Successor # out of range for invoke!"); 3144 return i == 0 ? getNormalDest() : getUnwindDest(); 3145 } 3146 3147 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 3148 assert(idx < 2 && "Successor # out of range for invoke!"); 3149 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc); 3150 } 3151 3152 unsigned getNumSuccessors() const { return 2; } 3153 3154 // Methods for support type inquiry through isa, cast, and dyn_cast: 3155 static inline bool classof(const Instruction *I) { 3156 return (I->getOpcode() == Instruction::Invoke); 3157 } 3158 static inline bool classof(const Value *V) { 3159 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3160 } 3161 3162 private: 3163 virtual BasicBlock *getSuccessorV(unsigned idx) const; 3164 virtual unsigned getNumSuccessorsV() const; 3165 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 3166 3167 bool hasFnAttrImpl(Attribute::AttrKind A) const; 3168 3169 // Shadow Instruction::setInstructionSubclassData with a private forwarding 3170 // method so that subclasses cannot accidentally use it. 3171 void setInstructionSubclassData(unsigned short D) { 3172 Instruction::setInstructionSubclassData(D); 3173 } 3174 }; 3175 3176 template <> 3177 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> { 3178 }; 3179 3180 InvokeInst::InvokeInst(Value *Func, 3181 BasicBlock *IfNormal, BasicBlock *IfException, 3182 ArrayRef<Value *> Args, unsigned Values, 3183 const Twine &NameStr, Instruction *InsertBefore) 3184 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType()) 3185 ->getElementType())->getReturnType(), 3186 Instruction::Invoke, 3187 OperandTraits<InvokeInst>::op_end(this) - Values, 3188 Values, InsertBefore) { 3189 init(Func, IfNormal, IfException, Args, NameStr); 3190 } 3191 InvokeInst::InvokeInst(Value *Func, 3192 BasicBlock *IfNormal, BasicBlock *IfException, 3193 ArrayRef<Value *> Args, unsigned Values, 3194 const Twine &NameStr, BasicBlock *InsertAtEnd) 3195 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType()) 3196 ->getElementType())->getReturnType(), 3197 Instruction::Invoke, 3198 OperandTraits<InvokeInst>::op_end(this) - Values, 3199 Values, InsertAtEnd) { 3200 init(Func, IfNormal, IfException, Args, NameStr); 3201 } 3202 3203 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value) 3204 3205 //===----------------------------------------------------------------------===// 3206 // ResumeInst Class 3207 //===----------------------------------------------------------------------===// 3208 3209 //===--------------------------------------------------------------------------- 3210 /// ResumeInst - Resume the propagation of an exception. 3211 /// 3212 class ResumeInst : public TerminatorInst { 3213 ResumeInst(const ResumeInst &RI); 3214 3215 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0); 3216 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); 3217 protected: 3218 virtual ResumeInst *clone_impl() const; 3219 public: 3220 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) { 3221 return new(1) ResumeInst(Exn, InsertBefore); 3222 } 3223 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { 3224 return new(1) ResumeInst(Exn, InsertAtEnd); 3225 } 3226 3227 /// Provide fast operand accessors 3228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3229 3230 /// Convenience accessor. 3231 Value *getValue() const { return Op<0>(); } 3232 3233 unsigned getNumSuccessors() const { return 0; } 3234 3235 // Methods for support type inquiry through isa, cast, and dyn_cast: 3236 static inline bool classof(const Instruction *I) { 3237 return I->getOpcode() == Instruction::Resume; 3238 } 3239 static inline bool classof(const Value *V) { 3240 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3241 } 3242 private: 3243 virtual BasicBlock *getSuccessorV(unsigned idx) const; 3244 virtual unsigned getNumSuccessorsV() const; 3245 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 3246 }; 3247 3248 template <> 3249 struct OperandTraits<ResumeInst> : 3250 public FixedNumOperandTraits<ResumeInst, 1> { 3251 }; 3252 3253 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value) 3254 3255 //===----------------------------------------------------------------------===// 3256 // UnreachableInst Class 3257 //===----------------------------------------------------------------------===// 3258 3259 //===--------------------------------------------------------------------------- 3260 /// UnreachableInst - This function has undefined behavior. In particular, the 3261 /// presence of this instruction indicates some higher level knowledge that the 3262 /// end of the block cannot be reached. 3263 /// 3264 class UnreachableInst : public TerminatorInst { 3265 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; 3266 protected: 3267 virtual UnreachableInst *clone_impl() const; 3268 3269 public: 3270 // allocate space for exactly zero operands 3271 void *operator new(size_t s) { 3272 return User::operator new(s, 0); 3273 } 3274 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0); 3275 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); 3276 3277 unsigned getNumSuccessors() const { return 0; } 3278 3279 // Methods for support type inquiry through isa, cast, and dyn_cast: 3280 static inline bool classof(const Instruction *I) { 3281 return I->getOpcode() == Instruction::Unreachable; 3282 } 3283 static inline bool classof(const Value *V) { 3284 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3285 } 3286 private: 3287 virtual BasicBlock *getSuccessorV(unsigned idx) const; 3288 virtual unsigned getNumSuccessorsV() const; 3289 virtual void setSuccessorV(unsigned idx, BasicBlock *B); 3290 }; 3291 3292 //===----------------------------------------------------------------------===// 3293 // TruncInst Class 3294 //===----------------------------------------------------------------------===// 3295 3296 /// \brief This class represents a truncation of integer types. 3297 class TruncInst : public CastInst { 3298 protected: 3299 /// \brief Clone an identical TruncInst 3300 virtual TruncInst *clone_impl() const; 3301 3302 public: 3303 /// \brief Constructor with insert-before-instruction semantics 3304 TruncInst( 3305 Value *S, ///< The value to be truncated 3306 Type *Ty, ///< The (smaller) type to truncate to 3307 const Twine &NameStr = "", ///< A name for the new instruction 3308 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3309 ); 3310 3311 /// \brief Constructor with insert-at-end-of-block semantics 3312 TruncInst( 3313 Value *S, ///< The value to be truncated 3314 Type *Ty, ///< The (smaller) type to truncate to 3315 const Twine &NameStr, ///< A name for the new instruction 3316 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3317 ); 3318 3319 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3320 static inline bool classof(const Instruction *I) { 3321 return I->getOpcode() == Trunc; 3322 } 3323 static inline bool classof(const Value *V) { 3324 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3325 } 3326 }; 3327 3328 //===----------------------------------------------------------------------===// 3329 // ZExtInst Class 3330 //===----------------------------------------------------------------------===// 3331 3332 /// \brief This class represents zero extension of integer types. 3333 class ZExtInst : public CastInst { 3334 protected: 3335 /// \brief Clone an identical ZExtInst 3336 virtual ZExtInst *clone_impl() const; 3337 3338 public: 3339 /// \brief Constructor with insert-before-instruction semantics 3340 ZExtInst( 3341 Value *S, ///< The value to be zero extended 3342 Type *Ty, ///< The type to zero extend to 3343 const Twine &NameStr = "", ///< A name for the new instruction 3344 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3345 ); 3346 3347 /// \brief Constructor with insert-at-end semantics. 3348 ZExtInst( 3349 Value *S, ///< The value to be zero extended 3350 Type *Ty, ///< The type to zero extend to 3351 const Twine &NameStr, ///< A name for the new instruction 3352 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3353 ); 3354 3355 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3356 static inline bool classof(const Instruction *I) { 3357 return I->getOpcode() == ZExt; 3358 } 3359 static inline bool classof(const Value *V) { 3360 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3361 } 3362 }; 3363 3364 //===----------------------------------------------------------------------===// 3365 // SExtInst Class 3366 //===----------------------------------------------------------------------===// 3367 3368 /// \brief This class represents a sign extension of integer types. 3369 class SExtInst : public CastInst { 3370 protected: 3371 /// \brief Clone an identical SExtInst 3372 virtual SExtInst *clone_impl() const; 3373 3374 public: 3375 /// \brief Constructor with insert-before-instruction semantics 3376 SExtInst( 3377 Value *S, ///< The value to be sign extended 3378 Type *Ty, ///< The type to sign extend to 3379 const Twine &NameStr = "", ///< A name for the new instruction 3380 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3381 ); 3382 3383 /// \brief Constructor with insert-at-end-of-block semantics 3384 SExtInst( 3385 Value *S, ///< The value to be sign extended 3386 Type *Ty, ///< The type to sign extend to 3387 const Twine &NameStr, ///< A name for the new instruction 3388 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3389 ); 3390 3391 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3392 static inline bool classof(const Instruction *I) { 3393 return I->getOpcode() == SExt; 3394 } 3395 static inline bool classof(const Value *V) { 3396 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3397 } 3398 }; 3399 3400 //===----------------------------------------------------------------------===// 3401 // FPTruncInst Class 3402 //===----------------------------------------------------------------------===// 3403 3404 /// \brief This class represents a truncation of floating point types. 3405 class FPTruncInst : public CastInst { 3406 protected: 3407 /// \brief Clone an identical FPTruncInst 3408 virtual FPTruncInst *clone_impl() const; 3409 3410 public: 3411 /// \brief Constructor with insert-before-instruction semantics 3412 FPTruncInst( 3413 Value *S, ///< The value to be truncated 3414 Type *Ty, ///< The type to truncate to 3415 const Twine &NameStr = "", ///< A name for the new instruction 3416 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3417 ); 3418 3419 /// \brief Constructor with insert-before-instruction semantics 3420 FPTruncInst( 3421 Value *S, ///< The value to be truncated 3422 Type *Ty, ///< The type to truncate to 3423 const Twine &NameStr, ///< A name for the new instruction 3424 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3425 ); 3426 3427 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3428 static inline bool classof(const Instruction *I) { 3429 return I->getOpcode() == FPTrunc; 3430 } 3431 static inline bool classof(const Value *V) { 3432 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3433 } 3434 }; 3435 3436 //===----------------------------------------------------------------------===// 3437 // FPExtInst Class 3438 //===----------------------------------------------------------------------===// 3439 3440 /// \brief This class represents an extension of floating point types. 3441 class FPExtInst : public CastInst { 3442 protected: 3443 /// \brief Clone an identical FPExtInst 3444 virtual FPExtInst *clone_impl() const; 3445 3446 public: 3447 /// \brief Constructor with insert-before-instruction semantics 3448 FPExtInst( 3449 Value *S, ///< The value to be extended 3450 Type *Ty, ///< The type to extend to 3451 const Twine &NameStr = "", ///< A name for the new instruction 3452 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3453 ); 3454 3455 /// \brief Constructor with insert-at-end-of-block semantics 3456 FPExtInst( 3457 Value *S, ///< The value to be extended 3458 Type *Ty, ///< The type to extend to 3459 const Twine &NameStr, ///< A name for the new instruction 3460 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3461 ); 3462 3463 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3464 static inline bool classof(const Instruction *I) { 3465 return I->getOpcode() == FPExt; 3466 } 3467 static inline bool classof(const Value *V) { 3468 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3469 } 3470 }; 3471 3472 //===----------------------------------------------------------------------===// 3473 // UIToFPInst Class 3474 //===----------------------------------------------------------------------===// 3475 3476 /// \brief This class represents a cast unsigned integer to floating point. 3477 class UIToFPInst : public CastInst { 3478 protected: 3479 /// \brief Clone an identical UIToFPInst 3480 virtual UIToFPInst *clone_impl() const; 3481 3482 public: 3483 /// \brief Constructor with insert-before-instruction semantics 3484 UIToFPInst( 3485 Value *S, ///< The value to be converted 3486 Type *Ty, ///< The type to convert to 3487 const Twine &NameStr = "", ///< A name for the new instruction 3488 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3489 ); 3490 3491 /// \brief Constructor with insert-at-end-of-block semantics 3492 UIToFPInst( 3493 Value *S, ///< The value to be converted 3494 Type *Ty, ///< The type to convert to 3495 const Twine &NameStr, ///< A name for the new instruction 3496 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3497 ); 3498 3499 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3500 static inline bool classof(const Instruction *I) { 3501 return I->getOpcode() == UIToFP; 3502 } 3503 static inline bool classof(const Value *V) { 3504 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3505 } 3506 }; 3507 3508 //===----------------------------------------------------------------------===// 3509 // SIToFPInst Class 3510 //===----------------------------------------------------------------------===// 3511 3512 /// \brief This class represents a cast from signed integer to floating point. 3513 class SIToFPInst : public CastInst { 3514 protected: 3515 /// \brief Clone an identical SIToFPInst 3516 virtual SIToFPInst *clone_impl() const; 3517 3518 public: 3519 /// \brief Constructor with insert-before-instruction semantics 3520 SIToFPInst( 3521 Value *S, ///< The value to be converted 3522 Type *Ty, ///< The type to convert to 3523 const Twine &NameStr = "", ///< A name for the new instruction 3524 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3525 ); 3526 3527 /// \brief Constructor with insert-at-end-of-block semantics 3528 SIToFPInst( 3529 Value *S, ///< The value to be converted 3530 Type *Ty, ///< The type to convert to 3531 const Twine &NameStr, ///< A name for the new instruction 3532 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3533 ); 3534 3535 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3536 static inline bool classof(const Instruction *I) { 3537 return I->getOpcode() == SIToFP; 3538 } 3539 static inline bool classof(const Value *V) { 3540 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3541 } 3542 }; 3543 3544 //===----------------------------------------------------------------------===// 3545 // FPToUIInst Class 3546 //===----------------------------------------------------------------------===// 3547 3548 /// \brief This class represents a cast from floating point to unsigned integer 3549 class FPToUIInst : public CastInst { 3550 protected: 3551 /// \brief Clone an identical FPToUIInst 3552 virtual FPToUIInst *clone_impl() const; 3553 3554 public: 3555 /// \brief Constructor with insert-before-instruction semantics 3556 FPToUIInst( 3557 Value *S, ///< The value to be converted 3558 Type *Ty, ///< The type to convert to 3559 const Twine &NameStr = "", ///< A name for the new instruction 3560 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3561 ); 3562 3563 /// \brief Constructor with insert-at-end-of-block semantics 3564 FPToUIInst( 3565 Value *S, ///< The value to be converted 3566 Type *Ty, ///< The type to convert to 3567 const Twine &NameStr, ///< A name for the new instruction 3568 BasicBlock *InsertAtEnd ///< Where to insert the new instruction 3569 ); 3570 3571 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3572 static inline bool classof(const Instruction *I) { 3573 return I->getOpcode() == FPToUI; 3574 } 3575 static inline bool classof(const Value *V) { 3576 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3577 } 3578 }; 3579 3580 //===----------------------------------------------------------------------===// 3581 // FPToSIInst Class 3582 //===----------------------------------------------------------------------===// 3583 3584 /// \brief This class represents a cast from floating point to signed integer. 3585 class FPToSIInst : public CastInst { 3586 protected: 3587 /// \brief Clone an identical FPToSIInst 3588 virtual FPToSIInst *clone_impl() const; 3589 3590 public: 3591 /// \brief Constructor with insert-before-instruction semantics 3592 FPToSIInst( 3593 Value *S, ///< The value to be converted 3594 Type *Ty, ///< The type to convert to 3595 const Twine &NameStr = "", ///< A name for the new instruction 3596 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3597 ); 3598 3599 /// \brief Constructor with insert-at-end-of-block semantics 3600 FPToSIInst( 3601 Value *S, ///< The value to be converted 3602 Type *Ty, ///< The type to convert to 3603 const Twine &NameStr, ///< A name for the new instruction 3604 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3605 ); 3606 3607 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 3608 static inline bool classof(const Instruction *I) { 3609 return I->getOpcode() == FPToSI; 3610 } 3611 static inline bool classof(const Value *V) { 3612 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3613 } 3614 }; 3615 3616 //===----------------------------------------------------------------------===// 3617 // IntToPtrInst Class 3618 //===----------------------------------------------------------------------===// 3619 3620 /// \brief This class represents a cast from an integer to a pointer. 3621 class IntToPtrInst : public CastInst { 3622 public: 3623 /// \brief Constructor with insert-before-instruction semantics 3624 IntToPtrInst( 3625 Value *S, ///< The value to be converted 3626 Type *Ty, ///< The type to convert to 3627 const Twine &NameStr = "", ///< A name for the new instruction 3628 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3629 ); 3630 3631 /// \brief Constructor with insert-at-end-of-block semantics 3632 IntToPtrInst( 3633 Value *S, ///< The value to be converted 3634 Type *Ty, ///< The type to convert to 3635 const Twine &NameStr, ///< A name for the new instruction 3636 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3637 ); 3638 3639 /// \brief Clone an identical IntToPtrInst 3640 virtual IntToPtrInst *clone_impl() const; 3641 3642 /// \brief Returns the address space of this instruction's pointer type. 3643 unsigned getAddressSpace() const { 3644 return getType()->getPointerAddressSpace(); 3645 } 3646 3647 // Methods for support type inquiry through isa, cast, and dyn_cast: 3648 static inline bool classof(const Instruction *I) { 3649 return I->getOpcode() == IntToPtr; 3650 } 3651 static inline bool classof(const Value *V) { 3652 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3653 } 3654 }; 3655 3656 //===----------------------------------------------------------------------===// 3657 // PtrToIntInst Class 3658 //===----------------------------------------------------------------------===// 3659 3660 /// \brief This class represents a cast from a pointer to an integer 3661 class PtrToIntInst : public CastInst { 3662 protected: 3663 /// \brief Clone an identical PtrToIntInst 3664 virtual PtrToIntInst *clone_impl() const; 3665 3666 public: 3667 /// \brief Constructor with insert-before-instruction semantics 3668 PtrToIntInst( 3669 Value *S, ///< The value to be converted 3670 Type *Ty, ///< The type to convert to 3671 const Twine &NameStr = "", ///< A name for the new instruction 3672 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3673 ); 3674 3675 /// \brief Constructor with insert-at-end-of-block semantics 3676 PtrToIntInst( 3677 Value *S, ///< The value to be converted 3678 Type *Ty, ///< The type to convert to 3679 const Twine &NameStr, ///< A name for the new instruction 3680 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3681 ); 3682 3683 /// \brief Gets the pointer operand. 3684 Value *getPointerOperand() { return getOperand(0); } 3685 /// \brief Gets the pointer operand. 3686 const Value *getPointerOperand() const { return getOperand(0); } 3687 /// \brief Gets the operand index of the pointer operand. 3688 static unsigned getPointerOperandIndex() { return 0U; } 3689 3690 /// \brief Returns the address space of the pointer operand. 3691 unsigned getPointerAddressSpace() const { 3692 return getPointerOperand()->getType()->getPointerAddressSpace(); 3693 } 3694 3695 // Methods for support type inquiry through isa, cast, and dyn_cast: 3696 static inline bool classof(const Instruction *I) { 3697 return I->getOpcode() == PtrToInt; 3698 } 3699 static inline bool classof(const Value *V) { 3700 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3701 } 3702 }; 3703 3704 //===----------------------------------------------------------------------===// 3705 // BitCastInst Class 3706 //===----------------------------------------------------------------------===// 3707 3708 /// \brief This class represents a no-op cast from one type to another. 3709 class BitCastInst : public CastInst { 3710 protected: 3711 /// \brief Clone an identical BitCastInst 3712 virtual BitCastInst *clone_impl() const; 3713 3714 public: 3715 /// \brief Constructor with insert-before-instruction semantics 3716 BitCastInst( 3717 Value *S, ///< The value to be casted 3718 Type *Ty, ///< The type to casted to 3719 const Twine &NameStr = "", ///< A name for the new instruction 3720 Instruction *InsertBefore = 0 ///< Where to insert the new instruction 3721 ); 3722 3723 /// \brief Constructor with insert-at-end-of-block semantics 3724 BitCastInst( 3725 Value *S, ///< The value to be casted 3726 Type *Ty, ///< The type to casted to 3727 const Twine &NameStr, ///< A name for the new instruction 3728 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 3729 ); 3730 3731 // Methods for support type inquiry through isa, cast, and dyn_cast: 3732 static inline bool classof(const Instruction *I) { 3733 return I->getOpcode() == BitCast; 3734 } 3735 static inline bool classof(const Value *V) { 3736 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3737 } 3738 }; 3739 3740 } // End llvm namespace 3741 3742 #endif 3743