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