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/ADT/STLExtras.h" 22 #include "llvm/ADT/iterator_range.h" 23 #include "llvm/IR/Attributes.h" 24 #include "llvm/IR/CallingConv.h" 25 #include "llvm/IR/DerivedTypes.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/InstrTypes.h" 28 #include "llvm/Support/AtomicOrdering.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include <iterator> 31 32 namespace llvm { 33 34 class APInt; 35 class ConstantInt; 36 class ConstantRange; 37 class DataLayout; 38 class LLVMContext; 39 40 enum SynchronizationScope { 41 SingleThread = 0, 42 CrossThread = 1 43 }; 44 45 //===----------------------------------------------------------------------===// 46 // AllocaInst Class 47 //===----------------------------------------------------------------------===// 48 49 /// AllocaInst - an instruction to allocate memory on the stack 50 /// 51 class AllocaInst : public UnaryInstruction { 52 Type *AllocatedType; 53 54 protected: 55 // Note: Instruction needs to be a friend here to call cloneImpl. 56 friend class Instruction; 57 AllocaInst *cloneImpl() const; 58 59 public: 60 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr, 61 const Twine &Name = "", 62 Instruction *InsertBefore = nullptr); 63 AllocaInst(Type *Ty, Value *ArraySize, 64 const Twine &Name, BasicBlock *InsertAtEnd); 65 66 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr); 67 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd); 68 69 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 70 const Twine &Name = "", Instruction *InsertBefore = nullptr); 71 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 72 const Twine &Name, BasicBlock *InsertAtEnd); 73 74 // Out of line virtual method, so the vtable, etc. has a home. 75 ~AllocaInst() override; 76 77 /// isArrayAllocation - Return true if there is an allocation size parameter 78 /// to the allocation instruction that is not 1. 79 /// 80 bool isArrayAllocation() const; 81 82 /// getArraySize - Get the number of elements allocated. For a simple 83 /// allocation of a single element, this will return a constant 1 value. 84 /// getArraySize()85 const Value *getArraySize() const { return getOperand(0); } getArraySize()86 Value *getArraySize() { return getOperand(0); } 87 88 /// getType - Overload to return most specific pointer type 89 /// getType()90 PointerType *getType() const { 91 return cast<PointerType>(Instruction::getType()); 92 } 93 94 /// getAllocatedType - Return the type that is being allocated by the 95 /// instruction. 96 /// getAllocatedType()97 Type *getAllocatedType() const { return AllocatedType; } 98 /// \brief for use only in special circumstances that need to generically 99 /// transform a whole instruction (eg: IR linking and vectorization). setAllocatedType(Type * Ty)100 void setAllocatedType(Type *Ty) { AllocatedType = Ty; } 101 102 /// getAlignment - Return the alignment of the memory that is being allocated 103 /// by the instruction. 104 /// getAlignment()105 unsigned getAlignment() const { 106 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1; 107 } 108 void setAlignment(unsigned Align); 109 110 /// isStaticAlloca - Return true if this alloca is in the entry block of the 111 /// function and is a constant size. If so, the code generator will fold it 112 /// into the prolog/epilog code, so it is basically free. 113 bool isStaticAlloca() const; 114 115 /// \brief Return true if this alloca is used as an inalloca argument to a 116 /// call. Such allocas are never considered static even if they are in the 117 /// entry block. isUsedWithInAlloca()118 bool isUsedWithInAlloca() const { 119 return getSubclassDataFromInstruction() & 32; 120 } 121 122 /// \brief Specify whether this alloca is used to represent the arguments to 123 /// a call. setUsedWithInAlloca(bool V)124 void setUsedWithInAlloca(bool V) { 125 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) | 126 (V ? 32 : 0)); 127 } 128 129 /// \brief Return true if this alloca is used as a swifterror argument to a 130 /// call. isSwiftError()131 bool isSwiftError() const { 132 return getSubclassDataFromInstruction() & 64; 133 } 134 135 /// \brief Specify whether this alloca is used to represent a swifterror. setSwiftError(bool V)136 void setSwiftError(bool V) { 137 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) | 138 (V ? 64 : 0)); 139 } 140 141 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Instruction * I)142 static inline bool classof(const Instruction *I) { 143 return (I->getOpcode() == Instruction::Alloca); 144 } classof(const Value * V)145 static inline bool classof(const Value *V) { 146 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 147 } 148 149 private: 150 // Shadow Instruction::setInstructionSubclassData with a private forwarding 151 // method so that subclasses cannot accidentally use it. setInstructionSubclassData(unsigned short D)152 void setInstructionSubclassData(unsigned short D) { 153 Instruction::setInstructionSubclassData(D); 154 } 155 }; 156 157 //===----------------------------------------------------------------------===// 158 // LoadInst Class 159 //===----------------------------------------------------------------------===// 160 161 /// LoadInst - an instruction for reading from memory. This uses the 162 /// SubclassData field in Value to store whether or not the load is volatile. 163 /// 164 class LoadInst : public UnaryInstruction { 165 void AssertOK(); 166 167 protected: 168 // Note: Instruction needs to be a friend here to call cloneImpl. 169 friend class Instruction; 170 LoadInst *cloneImpl() const; 171 172 public: 173 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore); 174 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); 175 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false, 176 Instruction *InsertBefore = nullptr); 177 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false, 178 Instruction *InsertBefore = nullptr) 179 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr, 180 NameStr, isVolatile, InsertBefore) {} 181 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 182 BasicBlock *InsertAtEnd); 183 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, 184 Instruction *InsertBefore = nullptr) 185 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr, 186 NameStr, isVolatile, Align, InsertBefore) {} 187 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, 188 unsigned Align, Instruction *InsertBefore = nullptr); 189 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 190 unsigned Align, BasicBlock *InsertAtEnd); 191 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, 192 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread, 193 Instruction *InsertBefore = nullptr) 194 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr, 195 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {} 196 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, 197 unsigned Align, AtomicOrdering Order, 198 SynchronizationScope SynchScope = CrossThread, 199 Instruction *InsertBefore = nullptr); 200 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, 201 unsigned Align, AtomicOrdering Order, 202 SynchronizationScope SynchScope, 203 BasicBlock *InsertAtEnd); 204 205 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore); 206 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd); 207 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr, 208 bool isVolatile = false, Instruction *InsertBefore = nullptr); 209 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr, 210 bool isVolatile = false, 211 Instruction *InsertBefore = nullptr) 212 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr, 213 NameStr, isVolatile, InsertBefore) {} 214 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile, 215 BasicBlock *InsertAtEnd); 216 217 /// isVolatile - Return true if this is a load from a volatile memory 218 /// location. 219 /// isVolatile()220 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 221 222 /// setVolatile - Specify whether this is a volatile load or not. 223 /// setVolatile(bool V)224 void setVolatile(bool V) { 225 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 226 (V ? 1 : 0)); 227 } 228 229 /// getAlignment - Return the alignment of the access that is being performed 230 /// getAlignment()231 unsigned getAlignment() const { 232 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1; 233 } 234 235 void setAlignment(unsigned Align); 236 237 /// Returns the ordering effect of this fence. getOrdering()238 AtomicOrdering getOrdering() const { 239 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); 240 } 241 242 /// Set the ordering constraint on this load. May not be Release or 243 /// AcquireRelease. setOrdering(AtomicOrdering Ordering)244 void setOrdering(AtomicOrdering Ordering) { 245 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | 246 ((unsigned)Ordering << 7)); 247 } 248 getSynchScope()249 SynchronizationScope getSynchScope() const { 250 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1); 251 } 252 253 /// Specify whether this load is ordered with respect to all 254 /// concurrently executing threads, or only with respect to signal handlers 255 /// executing in the same thread. setSynchScope(SynchronizationScope xthread)256 void setSynchScope(SynchronizationScope xthread) { 257 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) | 258 (xthread << 6)); 259 } 260 261 void setAtomic(AtomicOrdering Ordering, 262 SynchronizationScope SynchScope = CrossThread) { 263 setOrdering(Ordering); 264 setSynchScope(SynchScope); 265 } 266 isSimple()267 bool isSimple() const { return !isAtomic() && !isVolatile(); } isUnordered()268 bool isUnordered() const { 269 return (getOrdering() == AtomicOrdering::NotAtomic || 270 getOrdering() == AtomicOrdering::Unordered) && 271 !isVolatile(); 272 } 273 getPointerOperand()274 Value *getPointerOperand() { return getOperand(0); } getPointerOperand()275 const Value *getPointerOperand() const { return getOperand(0); } getPointerOperandIndex()276 static unsigned getPointerOperandIndex() { return 0U; } 277 278 /// \brief Returns the address space of the pointer operand. getPointerAddressSpace()279 unsigned getPointerAddressSpace() const { 280 return getPointerOperand()->getType()->getPointerAddressSpace(); 281 } 282 283 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Instruction * I)284 static inline bool classof(const Instruction *I) { 285 return I->getOpcode() == Instruction::Load; 286 } classof(const Value * V)287 static inline bool classof(const Value *V) { 288 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 289 } 290 291 private: 292 // Shadow Instruction::setInstructionSubclassData with a private forwarding 293 // method so that subclasses cannot accidentally use it. setInstructionSubclassData(unsigned short D)294 void setInstructionSubclassData(unsigned short D) { 295 Instruction::setInstructionSubclassData(D); 296 } 297 }; 298 299 //===----------------------------------------------------------------------===// 300 // StoreInst Class 301 //===----------------------------------------------------------------------===// 302 303 /// StoreInst - an instruction for storing to memory 304 /// 305 class StoreInst : public Instruction { 306 void *operator new(size_t, unsigned) = delete; 307 void AssertOK(); 308 309 protected: 310 // Note: Instruction needs to be a friend here to call cloneImpl. 311 friend class Instruction; 312 StoreInst *cloneImpl() const; 313 314 public: 315 // allocate space for exactly two operands new(size_t s)316 void *operator new(size_t s) { 317 return User::operator new(s, 2); 318 } 319 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); 320 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); 321 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, 322 Instruction *InsertBefore = nullptr); 323 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); 324 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 325 unsigned Align, Instruction *InsertBefore = nullptr); 326 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 327 unsigned Align, BasicBlock *InsertAtEnd); 328 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 329 unsigned Align, AtomicOrdering Order, 330 SynchronizationScope SynchScope = CrossThread, 331 Instruction *InsertBefore = nullptr); 332 StoreInst(Value *Val, Value *Ptr, bool isVolatile, 333 unsigned Align, AtomicOrdering Order, 334 SynchronizationScope SynchScope, 335 BasicBlock *InsertAtEnd); 336 337 /// isVolatile - Return true if this is a store to a volatile memory 338 /// location. 339 /// isVolatile()340 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } 341 342 /// setVolatile - Specify whether this is a volatile store or not. 343 /// setVolatile(bool V)344 void setVolatile(bool V) { 345 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 346 (V ? 1 : 0)); 347 } 348 349 /// Transparently provide more efficient getOperand methods. 350 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 351 352 /// getAlignment - Return the alignment of the access that is being performed 353 /// getAlignment()354 unsigned getAlignment() const { 355 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1; 356 } 357 358 void setAlignment(unsigned Align); 359 360 /// Returns the ordering effect of this store. getOrdering()361 AtomicOrdering getOrdering() const { 362 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); 363 } 364 365 /// Set the ordering constraint on this store. May not be Acquire or 366 /// AcquireRelease. setOrdering(AtomicOrdering Ordering)367 void setOrdering(AtomicOrdering Ordering) { 368 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | 369 ((unsigned)Ordering << 7)); 370 } 371 getSynchScope()372 SynchronizationScope getSynchScope() const { 373 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1); 374 } 375 376 /// Specify whether this store instruction is ordered with respect to all 377 /// concurrently executing threads, or only with respect to signal handlers 378 /// executing in the same thread. setSynchScope(SynchronizationScope xthread)379 void setSynchScope(SynchronizationScope xthread) { 380 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) | 381 (xthread << 6)); 382 } 383 384 void setAtomic(AtomicOrdering Ordering, 385 SynchronizationScope SynchScope = CrossThread) { 386 setOrdering(Ordering); 387 setSynchScope(SynchScope); 388 } 389 isSimple()390 bool isSimple() const { return !isAtomic() && !isVolatile(); } isUnordered()391 bool isUnordered() const { 392 return (getOrdering() == AtomicOrdering::NotAtomic || 393 getOrdering() == AtomicOrdering::Unordered) && 394 !isVolatile(); 395 } 396 getValueOperand()397 Value *getValueOperand() { return getOperand(0); } getValueOperand()398 const Value *getValueOperand() const { return getOperand(0); } 399 getPointerOperand()400 Value *getPointerOperand() { return getOperand(1); } getPointerOperand()401 const Value *getPointerOperand() const { return getOperand(1); } getPointerOperandIndex()402 static unsigned getPointerOperandIndex() { return 1U; } 403 404 /// \brief Returns the address space of the pointer operand. getPointerAddressSpace()405 unsigned getPointerAddressSpace() const { 406 return getPointerOperand()->getType()->getPointerAddressSpace(); 407 } 408 409 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Instruction * I)410 static inline bool classof(const Instruction *I) { 411 return I->getOpcode() == Instruction::Store; 412 } classof(const Value * V)413 static inline bool classof(const Value *V) { 414 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 415 } 416 417 private: 418 // Shadow Instruction::setInstructionSubclassData with a private forwarding 419 // method so that subclasses cannot accidentally use it. setInstructionSubclassData(unsigned short D)420 void setInstructionSubclassData(unsigned short D) { 421 Instruction::setInstructionSubclassData(D); 422 } 423 }; 424 425 template <> 426 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { 427 }; 428 429 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value) 430 431 //===----------------------------------------------------------------------===// 432 // FenceInst Class 433 //===----------------------------------------------------------------------===// 434 435 /// FenceInst - an instruction for ordering other memory operations 436 /// 437 class FenceInst : public Instruction { 438 void *operator new(size_t, unsigned) = delete; 439 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope); 440 441 protected: 442 // Note: Instruction needs to be a friend here to call cloneImpl. 443 friend class Instruction; 444 FenceInst *cloneImpl() const; 445 446 public: 447 // allocate space for exactly zero operands 448 void *operator new(size_t s) { 449 return User::operator new(s, 0); 450 } 451 452 // Ordering may only be Acquire, Release, AcquireRelease, or 453 // SequentiallyConsistent. 454 FenceInst(LLVMContext &C, AtomicOrdering Ordering, 455 SynchronizationScope SynchScope = CrossThread, 456 Instruction *InsertBefore = nullptr); 457 FenceInst(LLVMContext &C, AtomicOrdering Ordering, 458 SynchronizationScope SynchScope, 459 BasicBlock *InsertAtEnd); 460 461 /// Returns the ordering effect of this fence. 462 AtomicOrdering getOrdering() const { 463 return AtomicOrdering(getSubclassDataFromInstruction() >> 1); 464 } 465 466 /// Set the ordering constraint on this fence. May only be Acquire, Release, 467 /// AcquireRelease, or SequentiallyConsistent. 468 void setOrdering(AtomicOrdering Ordering) { 469 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | 470 ((unsigned)Ordering << 1)); 471 } 472 473 SynchronizationScope getSynchScope() const { 474 return SynchronizationScope(getSubclassDataFromInstruction() & 1); 475 } 476 477 /// Specify whether this fence orders other operations with respect to all 478 /// concurrently executing threads, or only with respect to signal handlers 479 /// executing in the same thread. 480 void setSynchScope(SynchronizationScope xthread) { 481 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 482 xthread); 483 } 484 485 // Methods for support type inquiry through isa, cast, and dyn_cast: 486 static inline bool classof(const Instruction *I) { 487 return I->getOpcode() == Instruction::Fence; 488 } 489 static inline bool classof(const Value *V) { 490 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 491 } 492 493 private: 494 // Shadow Instruction::setInstructionSubclassData with a private forwarding 495 // method so that subclasses cannot accidentally use it. 496 void setInstructionSubclassData(unsigned short D) { 497 Instruction::setInstructionSubclassData(D); 498 } 499 }; 500 501 //===----------------------------------------------------------------------===// 502 // AtomicCmpXchgInst Class 503 //===----------------------------------------------------------------------===// 504 505 /// AtomicCmpXchgInst - an instruction that atomically checks whether a 506 /// specified value is in a memory location, and, if it is, stores a new value 507 /// there. Returns the value that was loaded. 508 /// 509 class AtomicCmpXchgInst : public Instruction { 510 void *operator new(size_t, unsigned) = delete; 511 void Init(Value *Ptr, Value *Cmp, Value *NewVal, 512 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, 513 SynchronizationScope SynchScope); 514 515 protected: 516 // Note: Instruction needs to be a friend here to call cloneImpl. 517 friend class Instruction; 518 AtomicCmpXchgInst *cloneImpl() const; 519 520 public: 521 // allocate space for exactly three operands 522 void *operator new(size_t s) { 523 return User::operator new(s, 3); 524 } 525 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 526 AtomicOrdering SuccessOrdering, 527 AtomicOrdering FailureOrdering, 528 SynchronizationScope SynchScope, 529 Instruction *InsertBefore = nullptr); 530 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 531 AtomicOrdering SuccessOrdering, 532 AtomicOrdering FailureOrdering, 533 SynchronizationScope SynchScope, 534 BasicBlock *InsertAtEnd); 535 536 /// isVolatile - Return true if this is a cmpxchg from a volatile memory 537 /// location. 538 /// 539 bool isVolatile() const { 540 return getSubclassDataFromInstruction() & 1; 541 } 542 543 /// setVolatile - Specify whether this is a volatile cmpxchg. 544 /// 545 void setVolatile(bool V) { 546 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 547 (unsigned)V); 548 } 549 550 /// Return true if this cmpxchg may spuriously fail. 551 bool isWeak() const { 552 return getSubclassDataFromInstruction() & 0x100; 553 } 554 555 void setWeak(bool IsWeak) { 556 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) | 557 (IsWeak << 8)); 558 } 559 560 /// Transparently provide more efficient getOperand methods. 561 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 562 563 /// Set the ordering constraint on this cmpxchg. 564 void setSuccessOrdering(AtomicOrdering Ordering) { 565 assert(Ordering != AtomicOrdering::NotAtomic && 566 "CmpXchg instructions can only be atomic."); 567 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) | 568 ((unsigned)Ordering << 2)); 569 } 570 571 void setFailureOrdering(AtomicOrdering Ordering) { 572 assert(Ordering != AtomicOrdering::NotAtomic && 573 "CmpXchg instructions can only be atomic."); 574 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) | 575 ((unsigned)Ordering << 5)); 576 } 577 578 /// Specify whether this cmpxchg is atomic and orders other operations with 579 /// respect to all concurrently executing threads, or only with respect to 580 /// signal handlers executing in the same thread. 581 void setSynchScope(SynchronizationScope SynchScope) { 582 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) | 583 (SynchScope << 1)); 584 } 585 586 /// Returns the ordering constraint on this cmpxchg. 587 AtomicOrdering getSuccessOrdering() const { 588 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); 589 } 590 591 /// Returns the ordering constraint on this cmpxchg. 592 AtomicOrdering getFailureOrdering() const { 593 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7); 594 } 595 596 /// Returns whether this cmpxchg is atomic between threads or only within a 597 /// single thread. 598 SynchronizationScope getSynchScope() const { 599 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1); 600 } 601 602 Value *getPointerOperand() { return getOperand(0); } 603 const Value *getPointerOperand() const { return getOperand(0); } 604 static unsigned getPointerOperandIndex() { return 0U; } 605 606 Value *getCompareOperand() { return getOperand(1); } 607 const Value *getCompareOperand() const { return getOperand(1); } 608 609 Value *getNewValOperand() { return getOperand(2); } 610 const Value *getNewValOperand() const { return getOperand(2); } 611 612 /// \brief Returns the address space of the pointer operand. 613 unsigned getPointerAddressSpace() const { 614 return getPointerOperand()->getType()->getPointerAddressSpace(); 615 } 616 617 /// \brief Returns the strongest permitted ordering on failure, given the 618 /// desired ordering on success. 619 /// 620 /// If the comparison in a cmpxchg operation fails, there is no atomic store 621 /// so release semantics cannot be provided. So this function drops explicit 622 /// Release requests from the AtomicOrdering. A SequentiallyConsistent 623 /// operation would remain SequentiallyConsistent. 624 static AtomicOrdering 625 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { 626 switch (SuccessOrdering) { 627 default: 628 llvm_unreachable("invalid cmpxchg success ordering"); 629 case AtomicOrdering::Release: 630 case AtomicOrdering::Monotonic: 631 return AtomicOrdering::Monotonic; 632 case AtomicOrdering::AcquireRelease: 633 case AtomicOrdering::Acquire: 634 return AtomicOrdering::Acquire; 635 case AtomicOrdering::SequentiallyConsistent: 636 return AtomicOrdering::SequentiallyConsistent; 637 } 638 } 639 640 // Methods for support type inquiry through isa, cast, and dyn_cast: 641 static inline bool classof(const Instruction *I) { 642 return I->getOpcode() == Instruction::AtomicCmpXchg; 643 } 644 static inline bool classof(const Value *V) { 645 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 646 } 647 648 private: 649 // Shadow Instruction::setInstructionSubclassData with a private forwarding 650 // method so that subclasses cannot accidentally use it. 651 void setInstructionSubclassData(unsigned short D) { 652 Instruction::setInstructionSubclassData(D); 653 } 654 }; 655 656 template <> 657 struct OperandTraits<AtomicCmpXchgInst> : 658 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { 659 }; 660 661 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value) 662 663 //===----------------------------------------------------------------------===// 664 // AtomicRMWInst Class 665 //===----------------------------------------------------------------------===// 666 667 /// AtomicRMWInst - an instruction that atomically reads a memory location, 668 /// combines it with another value, and then stores the result back. Returns 669 /// the old value. 670 /// 671 class AtomicRMWInst : public Instruction { 672 void *operator new(size_t, unsigned) = delete; 673 674 protected: 675 // Note: Instruction needs to be a friend here to call cloneImpl. 676 friend class Instruction; 677 AtomicRMWInst *cloneImpl() const; 678 679 public: 680 /// This enumeration lists the possible modifications atomicrmw can make. In 681 /// the descriptions, 'p' is the pointer to the instruction's memory location, 682 /// 'old' is the initial value of *p, and 'v' is the other value passed to the 683 /// instruction. These instructions always return 'old'. 684 enum BinOp { 685 /// *p = v 686 Xchg, 687 /// *p = old + v 688 Add, 689 /// *p = old - v 690 Sub, 691 /// *p = old & v 692 And, 693 /// *p = ~(old & v) 694 Nand, 695 /// *p = old | v 696 Or, 697 /// *p = old ^ v 698 Xor, 699 /// *p = old >signed v ? old : v 700 Max, 701 /// *p = old <signed v ? old : v 702 Min, 703 /// *p = old >unsigned v ? old : v 704 UMax, 705 /// *p = old <unsigned v ? old : v 706 UMin, 707 708 FIRST_BINOP = Xchg, 709 LAST_BINOP = UMin, 710 BAD_BINOP 711 }; 712 713 // allocate space for exactly two operands 714 void *operator new(size_t s) { 715 return User::operator new(s, 2); 716 } 717 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 718 AtomicOrdering Ordering, SynchronizationScope SynchScope, 719 Instruction *InsertBefore = nullptr); 720 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 721 AtomicOrdering Ordering, SynchronizationScope SynchScope, 722 BasicBlock *InsertAtEnd); 723 724 BinOp getOperation() const { 725 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5); 726 } 727 728 void setOperation(BinOp Operation) { 729 unsigned short SubclassData = getSubclassDataFromInstruction(); 730 setInstructionSubclassData((SubclassData & 31) | 731 (Operation << 5)); 732 } 733 734 /// isVolatile - Return true if this is a RMW on a volatile memory location. 735 /// 736 bool isVolatile() const { 737 return getSubclassDataFromInstruction() & 1; 738 } 739 740 /// setVolatile - Specify whether this is a volatile RMW or not. 741 /// 742 void setVolatile(bool V) { 743 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 744 (unsigned)V); 745 } 746 747 /// Transparently provide more efficient getOperand methods. 748 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 749 750 /// Set the ordering constraint on this RMW. 751 void setOrdering(AtomicOrdering Ordering) { 752 assert(Ordering != AtomicOrdering::NotAtomic && 753 "atomicrmw instructions can only be atomic."); 754 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) | 755 ((unsigned)Ordering << 2)); 756 } 757 758 /// Specify whether this RMW orders other operations with respect to all 759 /// concurrently executing threads, or only with respect to signal handlers 760 /// executing in the same thread. 761 void setSynchScope(SynchronizationScope SynchScope) { 762 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) | 763 (SynchScope << 1)); 764 } 765 766 /// Returns the ordering constraint on this RMW. 767 AtomicOrdering getOrdering() const { 768 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); 769 } 770 771 /// Returns whether this RMW is atomic between threads or only within a 772 /// single thread. 773 SynchronizationScope getSynchScope() const { 774 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1); 775 } 776 777 Value *getPointerOperand() { return getOperand(0); } 778 const Value *getPointerOperand() const { return getOperand(0); } 779 static unsigned getPointerOperandIndex() { return 0U; } 780 781 Value *getValOperand() { return getOperand(1); } 782 const Value *getValOperand() const { return getOperand(1); } 783 784 /// \brief Returns the address space of the pointer operand. 785 unsigned getPointerAddressSpace() const { 786 return getPointerOperand()->getType()->getPointerAddressSpace(); 787 } 788 789 // Methods for support type inquiry through isa, cast, and dyn_cast: 790 static inline bool classof(const Instruction *I) { 791 return I->getOpcode() == Instruction::AtomicRMW; 792 } 793 static inline bool classof(const Value *V) { 794 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 795 } 796 797 private: 798 void Init(BinOp Operation, Value *Ptr, Value *Val, 799 AtomicOrdering Ordering, SynchronizationScope SynchScope); 800 // Shadow Instruction::setInstructionSubclassData with a private forwarding 801 // method so that subclasses cannot accidentally use it. 802 void setInstructionSubclassData(unsigned short D) { 803 Instruction::setInstructionSubclassData(D); 804 } 805 }; 806 807 template <> 808 struct OperandTraits<AtomicRMWInst> 809 : public FixedNumOperandTraits<AtomicRMWInst,2> { 810 }; 811 812 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value) 813 814 //===----------------------------------------------------------------------===// 815 // GetElementPtrInst Class 816 //===----------------------------------------------------------------------===// 817 818 // checkGEPType - Simple wrapper function to give a better assertion failure 819 // message on bad indexes for a gep instruction. 820 // 821 inline Type *checkGEPType(Type *Ty) { 822 assert(Ty && "Invalid GetElementPtrInst indices for type!"); 823 return Ty; 824 } 825 826 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to 827 /// access elements of arrays and structs 828 /// 829 class GetElementPtrInst : public Instruction { 830 Type *SourceElementType; 831 Type *ResultElementType; 832 833 void anchor() override; 834 835 GetElementPtrInst(const GetElementPtrInst &GEPI); 836 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); 837 838 /// Constructors - Create a getelementptr instruction with a base pointer an 839 /// list of indices. The first ctor can optionally insert before an existing 840 /// instruction, the second appends the new instruction to the specified 841 /// BasicBlock. 842 inline GetElementPtrInst(Type *PointeeType, Value *Ptr, 843 ArrayRef<Value *> IdxList, unsigned Values, 844 const Twine &NameStr, Instruction *InsertBefore); 845 inline GetElementPtrInst(Type *PointeeType, Value *Ptr, 846 ArrayRef<Value *> IdxList, unsigned Values, 847 const Twine &NameStr, BasicBlock *InsertAtEnd); 848 849 protected: 850 // Note: Instruction needs to be a friend here to call cloneImpl. 851 friend class Instruction; 852 GetElementPtrInst *cloneImpl() const; 853 854 public: 855 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, 856 ArrayRef<Value *> IdxList, 857 const Twine &NameStr = "", 858 Instruction *InsertBefore = nullptr) { 859 unsigned Values = 1 + unsigned(IdxList.size()); 860 if (!PointeeType) 861 PointeeType = 862 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); 863 else 864 assert( 865 PointeeType == 866 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()); 867 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, 868 NameStr, InsertBefore); 869 } 870 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, 871 ArrayRef<Value *> IdxList, 872 const Twine &NameStr, 873 BasicBlock *InsertAtEnd) { 874 unsigned Values = 1 + unsigned(IdxList.size()); 875 if (!PointeeType) 876 PointeeType = 877 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); 878 else 879 assert( 880 PointeeType == 881 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()); 882 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, 883 NameStr, InsertAtEnd); 884 } 885 886 /// Create an "inbounds" getelementptr. See the documentation for the 887 /// "inbounds" flag in LangRef.html for details. 888 static GetElementPtrInst *CreateInBounds(Value *Ptr, 889 ArrayRef<Value *> IdxList, 890 const Twine &NameStr = "", 891 Instruction *InsertBefore = nullptr){ 892 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore); 893 } 894 static GetElementPtrInst * 895 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, 896 const Twine &NameStr = "", 897 Instruction *InsertBefore = nullptr) { 898 GetElementPtrInst *GEP = 899 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); 900 GEP->setIsInBounds(true); 901 return GEP; 902 } 903 static GetElementPtrInst *CreateInBounds(Value *Ptr, 904 ArrayRef<Value *> IdxList, 905 const Twine &NameStr, 906 BasicBlock *InsertAtEnd) { 907 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd); 908 } 909 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, 910 ArrayRef<Value *> IdxList, 911 const Twine &NameStr, 912 BasicBlock *InsertAtEnd) { 913 GetElementPtrInst *GEP = 914 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); 915 GEP->setIsInBounds(true); 916 return GEP; 917 } 918 919 /// Transparently provide more efficient getOperand methods. 920 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 921 922 // getType - Overload to return most specific sequential type. 923 SequentialType *getType() const { 924 return cast<SequentialType>(Instruction::getType()); 925 } 926 927 Type *getSourceElementType() const { return SourceElementType; } 928 929 void setSourceElementType(Type *Ty) { SourceElementType = Ty; } 930 void setResultElementType(Type *Ty) { ResultElementType = Ty; } 931 932 Type *getResultElementType() const { 933 assert(ResultElementType == 934 cast<PointerType>(getType()->getScalarType())->getElementType()); 935 return ResultElementType; 936 } 937 938 /// \brief Returns the address space of this instruction's pointer type. 939 unsigned getAddressSpace() const { 940 // Note that this is always the same as the pointer operand's address space 941 // and that is cheaper to compute, so cheat here. 942 return getPointerAddressSpace(); 943 } 944 945 /// getIndexedType - Returns the type of the element that would be loaded with 946 /// a load instruction with the specified parameters. 947 /// 948 /// Null is returned if the indices are invalid for the specified 949 /// pointer type. 950 /// 951 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); 952 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); 953 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); 954 955 inline op_iterator idx_begin() { return op_begin()+1; } 956 inline const_op_iterator idx_begin() const { return op_begin()+1; } 957 inline op_iterator idx_end() { return op_end(); } 958 inline const_op_iterator idx_end() const { return op_end(); } 959 960 Value *getPointerOperand() { 961 return getOperand(0); 962 } 963 const Value *getPointerOperand() const { 964 return getOperand(0); 965 } 966 static unsigned getPointerOperandIndex() { 967 return 0U; // get index for modifying correct operand. 968 } 969 970 /// getPointerOperandType - Method to return the pointer operand as a 971 /// PointerType. 972 Type *getPointerOperandType() const { 973 return getPointerOperand()->getType(); 974 } 975 976 /// \brief Returns the address space of the pointer operand. 977 unsigned getPointerAddressSpace() const { 978 return getPointerOperandType()->getPointerAddressSpace(); 979 } 980 981 /// GetGEPReturnType - Returns the pointer type returned by the GEP 982 /// instruction, which may be a vector of pointers. 983 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) { 984 return getGEPReturnType( 985 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(), 986 Ptr, IdxList); 987 } 988 static Type *getGEPReturnType(Type *ElTy, Value *Ptr, 989 ArrayRef<Value *> IdxList) { 990 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)), 991 Ptr->getType()->getPointerAddressSpace()); 992 // Vector GEP 993 if (Ptr->getType()->isVectorTy()) { 994 unsigned NumElem = Ptr->getType()->getVectorNumElements(); 995 return VectorType::get(PtrTy, NumElem); 996 } 997 for (Value *Index : IdxList) 998 if (Index->getType()->isVectorTy()) { 999 unsigned NumElem = Index->getType()->getVectorNumElements(); 1000 return VectorType::get(PtrTy, NumElem); 1001 } 1002 // Scalar GEP 1003 return PtrTy; 1004 } 1005 1006 unsigned getNumIndices() const { // Note: always non-negative 1007 return getNumOperands() - 1; 1008 } 1009 1010 bool hasIndices() const { 1011 return getNumOperands() > 1; 1012 } 1013 1014 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 1015 /// zeros. If so, the result pointer and the first operand have the same 1016 /// value, just potentially different types. 1017 bool hasAllZeroIndices() const; 1018 1019 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 1020 /// constant integers. If so, the result pointer and the first operand have 1021 /// a constant offset between them. 1022 bool hasAllConstantIndices() const; 1023 1024 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction. 1025 /// See LangRef.html for the meaning of inbounds on a getelementptr. 1026 void setIsInBounds(bool b = true); 1027 1028 /// isInBounds - Determine whether the GEP has the inbounds flag. 1029 bool isInBounds() const; 1030 1031 /// \brief Accumulate the constant address offset of this GEP if possible. 1032 /// 1033 /// This routine accepts an APInt into which it will accumulate the constant 1034 /// offset of this GEP if the GEP is in fact constant. If the GEP is not 1035 /// all-constant, it returns false and the value of the offset APInt is 1036 /// undefined (it is *not* preserved!). The APInt passed into this routine 1037 /// must be at least as wide as the IntPtr type for the address space of 1038 /// the base GEP pointer. 1039 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; 1040 1041 // Methods for support type inquiry through isa, cast, and dyn_cast: 1042 static inline bool classof(const Instruction *I) { 1043 return (I->getOpcode() == Instruction::GetElementPtr); 1044 } 1045 static inline bool classof(const Value *V) { 1046 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1047 } 1048 }; 1049 1050 template <> 1051 struct OperandTraits<GetElementPtrInst> : 1052 public VariadicOperandTraits<GetElementPtrInst, 1> { 1053 }; 1054 1055 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, 1056 ArrayRef<Value *> IdxList, unsigned Values, 1057 const Twine &NameStr, 1058 Instruction *InsertBefore) 1059 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, 1060 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 1061 Values, InsertBefore), 1062 SourceElementType(PointeeType), 1063 ResultElementType(getIndexedType(PointeeType, IdxList)) { 1064 assert(ResultElementType == 1065 cast<PointerType>(getType()->getScalarType())->getElementType()); 1066 init(Ptr, IdxList, NameStr); 1067 } 1068 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, 1069 ArrayRef<Value *> IdxList, unsigned Values, 1070 const Twine &NameStr, 1071 BasicBlock *InsertAtEnd) 1072 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, 1073 OperandTraits<GetElementPtrInst>::op_end(this) - Values, 1074 Values, InsertAtEnd), 1075 SourceElementType(PointeeType), 1076 ResultElementType(getIndexedType(PointeeType, IdxList)) { 1077 assert(ResultElementType == 1078 cast<PointerType>(getType()->getScalarType())->getElementType()); 1079 init(Ptr, IdxList, NameStr); 1080 } 1081 1082 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value) 1083 1084 //===----------------------------------------------------------------------===// 1085 // ICmpInst Class 1086 //===----------------------------------------------------------------------===// 1087 1088 /// This instruction compares its operands according to the predicate given 1089 /// to the constructor. It only operates on integers or pointers. The operands 1090 /// must be identical types. 1091 /// \brief Represent an integer comparison operator. 1092 class ICmpInst: public CmpInst { 1093 void anchor() override; 1094 1095 void AssertOK() { 1096 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE && 1097 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE && 1098 "Invalid ICmp predicate value"); 1099 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1100 "Both operands to ICmp instruction are not of the same type!"); 1101 // Check that the operands are the right type 1102 assert((getOperand(0)->getType()->isIntOrIntVectorTy() || 1103 getOperand(0)->getType()->isPtrOrPtrVectorTy()) && 1104 "Invalid operand types for ICmp instruction"); 1105 } 1106 1107 protected: 1108 // Note: Instruction needs to be a friend here to call cloneImpl. 1109 friend class Instruction; 1110 /// \brief Clone an identical ICmpInst 1111 ICmpInst *cloneImpl() const; 1112 1113 public: 1114 /// \brief Constructor with insert-before-instruction semantics. 1115 ICmpInst( 1116 Instruction *InsertBefore, ///< Where to insert 1117 Predicate pred, ///< The predicate to use for the comparison 1118 Value *LHS, ///< The left-hand-side of the expression 1119 Value *RHS, ///< The right-hand-side of the expression 1120 const Twine &NameStr = "" ///< Name of the instruction 1121 ) : CmpInst(makeCmpResultType(LHS->getType()), 1122 Instruction::ICmp, pred, LHS, RHS, NameStr, 1123 InsertBefore) { 1124 #ifndef NDEBUG 1125 AssertOK(); 1126 #endif 1127 } 1128 1129 /// \brief Constructor with insert-at-end semantics. 1130 ICmpInst( 1131 BasicBlock &InsertAtEnd, ///< Block to insert into. 1132 Predicate pred, ///< The predicate to use for the comparison 1133 Value *LHS, ///< The left-hand-side of the expression 1134 Value *RHS, ///< The right-hand-side of the expression 1135 const Twine &NameStr = "" ///< Name of the instruction 1136 ) : CmpInst(makeCmpResultType(LHS->getType()), 1137 Instruction::ICmp, pred, LHS, RHS, NameStr, 1138 &InsertAtEnd) { 1139 #ifndef NDEBUG 1140 AssertOK(); 1141 #endif 1142 } 1143 1144 /// \brief Constructor with no-insertion semantics 1145 ICmpInst( 1146 Predicate pred, ///< The predicate to use for the comparison 1147 Value *LHS, ///< The left-hand-side of the expression 1148 Value *RHS, ///< The right-hand-side of the expression 1149 const Twine &NameStr = "" ///< Name of the instruction 1150 ) : CmpInst(makeCmpResultType(LHS->getType()), 1151 Instruction::ICmp, pred, LHS, RHS, NameStr) { 1152 #ifndef NDEBUG 1153 AssertOK(); 1154 #endif 1155 } 1156 1157 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. 1158 /// @returns the predicate that would be the result if the operand were 1159 /// regarded as signed. 1160 /// \brief Return the signed version of the predicate 1161 Predicate getSignedPredicate() const { 1162 return getSignedPredicate(getPredicate()); 1163 } 1164 1165 /// This is a static version that you can use without an instruction. 1166 /// \brief Return the signed version of the predicate. 1167 static Predicate getSignedPredicate(Predicate pred); 1168 1169 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. 1170 /// @returns the predicate that would be the result if the operand were 1171 /// regarded as unsigned. 1172 /// \brief Return the unsigned version of the predicate 1173 Predicate getUnsignedPredicate() const { 1174 return getUnsignedPredicate(getPredicate()); 1175 } 1176 1177 /// This is a static version that you can use without an instruction. 1178 /// \brief Return the unsigned version of the predicate. 1179 static Predicate getUnsignedPredicate(Predicate pred); 1180 1181 /// isEquality - Return true if this predicate is either EQ or NE. This also 1182 /// tests for commutativity. 1183 static bool isEquality(Predicate P) { 1184 return P == ICMP_EQ || P == ICMP_NE; 1185 } 1186 1187 /// isEquality - Return true if this predicate is either EQ or NE. This also 1188 /// tests for commutativity. 1189 bool isEquality() const { 1190 return isEquality(getPredicate()); 1191 } 1192 1193 /// @returns true if the predicate of this ICmpInst is commutative 1194 /// \brief Determine if this relation is commutative. 1195 bool isCommutative() const { return isEquality(); } 1196 1197 /// isRelational - Return true if the predicate is relational (not EQ or NE). 1198 /// 1199 bool isRelational() const { 1200 return !isEquality(); 1201 } 1202 1203 /// isRelational - Return true if the predicate is relational (not EQ or NE). 1204 /// 1205 static bool isRelational(Predicate P) { 1206 return !isEquality(P); 1207 } 1208 1209 /// Initialize a set of values that all satisfy the predicate with C. 1210 /// \brief Make a ConstantRange for a relation with a constant value. 1211 static ConstantRange makeConstantRange(Predicate pred, const APInt &C); 1212 1213 /// Exchange the two operands to this instruction in such a way that it does 1214 /// not modify the semantics of the instruction. The predicate value may be 1215 /// changed to retain the same result if the predicate is order dependent 1216 /// (e.g. ult). 1217 /// \brief Swap operands and adjust predicate. 1218 void swapOperands() { 1219 setPredicate(getSwappedPredicate()); 1220 Op<0>().swap(Op<1>()); 1221 } 1222 1223 // Methods for support type inquiry through isa, cast, and dyn_cast: 1224 static inline bool classof(const Instruction *I) { 1225 return I->getOpcode() == Instruction::ICmp; 1226 } 1227 static inline bool classof(const Value *V) { 1228 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1229 } 1230 }; 1231 1232 //===----------------------------------------------------------------------===// 1233 // FCmpInst Class 1234 //===----------------------------------------------------------------------===// 1235 1236 /// This instruction compares its operands according to the predicate given 1237 /// to the constructor. It only operates on floating point values or packed 1238 /// vectors of floating point values. The operands must be identical types. 1239 /// \brief Represents a floating point comparison operator. 1240 class FCmpInst: public CmpInst { 1241 protected: 1242 // Note: Instruction needs to be a friend here to call cloneImpl. 1243 friend class Instruction; 1244 /// \brief Clone an identical FCmpInst 1245 FCmpInst *cloneImpl() const; 1246 1247 public: 1248 /// \brief Constructor with insert-before-instruction semantics. 1249 FCmpInst( 1250 Instruction *InsertBefore, ///< Where to insert 1251 Predicate pred, ///< The predicate to use for the comparison 1252 Value *LHS, ///< The left-hand-side of the expression 1253 Value *RHS, ///< The right-hand-side of the expression 1254 const Twine &NameStr = "" ///< Name of the instruction 1255 ) : CmpInst(makeCmpResultType(LHS->getType()), 1256 Instruction::FCmp, pred, LHS, RHS, NameStr, 1257 InsertBefore) { 1258 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1259 "Invalid FCmp predicate value"); 1260 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1261 "Both operands to FCmp instruction are not of the same type!"); 1262 // Check that the operands are the right type 1263 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1264 "Invalid operand types for FCmp instruction"); 1265 } 1266 1267 /// \brief Constructor with insert-at-end semantics. 1268 FCmpInst( 1269 BasicBlock &InsertAtEnd, ///< Block to insert into. 1270 Predicate pred, ///< The predicate to use for the comparison 1271 Value *LHS, ///< The left-hand-side of the expression 1272 Value *RHS, ///< The right-hand-side of the expression 1273 const Twine &NameStr = "" ///< Name of the instruction 1274 ) : CmpInst(makeCmpResultType(LHS->getType()), 1275 Instruction::FCmp, pred, LHS, RHS, NameStr, 1276 &InsertAtEnd) { 1277 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1278 "Invalid FCmp predicate value"); 1279 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1280 "Both operands to FCmp instruction are not of the same type!"); 1281 // Check that the operands are the right type 1282 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1283 "Invalid operand types for FCmp instruction"); 1284 } 1285 1286 /// \brief Constructor with no-insertion semantics 1287 FCmpInst( 1288 Predicate pred, ///< The predicate to use for the comparison 1289 Value *LHS, ///< The left-hand-side of the expression 1290 Value *RHS, ///< The right-hand-side of the expression 1291 const Twine &NameStr = "" ///< Name of the instruction 1292 ) : CmpInst(makeCmpResultType(LHS->getType()), 1293 Instruction::FCmp, pred, LHS, RHS, NameStr) { 1294 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && 1295 "Invalid FCmp predicate value"); 1296 assert(getOperand(0)->getType() == getOperand(1)->getType() && 1297 "Both operands to FCmp instruction are not of the same type!"); 1298 // Check that the operands are the right type 1299 assert(getOperand(0)->getType()->isFPOrFPVectorTy() && 1300 "Invalid operand types for FCmp instruction"); 1301 } 1302 1303 /// @returns true if the predicate of this instruction is EQ or NE. 1304 /// \brief Determine if this is an equality predicate. 1305 static bool isEquality(Predicate Pred) { 1306 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || 1307 Pred == FCMP_UNE; 1308 } 1309 1310 /// @returns true if the predicate of this instruction is EQ or NE. 1311 /// \brief Determine if this is an equality predicate. 1312 bool isEquality() const { return isEquality(getPredicate()); } 1313 1314 /// @returns true if the predicate of this instruction is commutative. 1315 /// \brief Determine if this is a commutative predicate. 1316 bool isCommutative() const { 1317 return isEquality() || 1318 getPredicate() == FCMP_FALSE || 1319 getPredicate() == FCMP_TRUE || 1320 getPredicate() == FCMP_ORD || 1321 getPredicate() == FCMP_UNO; 1322 } 1323 1324 /// @returns true if the predicate is relational (not EQ or NE). 1325 /// \brief Determine if this a relational predicate. 1326 bool isRelational() const { return !isEquality(); } 1327 1328 /// Exchange the two operands to this instruction in such a way that it does 1329 /// not modify the semantics of the instruction. The predicate value may be 1330 /// changed to retain the same result if the predicate is order dependent 1331 /// (e.g. ult). 1332 /// \brief Swap operands and adjust predicate. 1333 void swapOperands() { 1334 setPredicate(getSwappedPredicate()); 1335 Op<0>().swap(Op<1>()); 1336 } 1337 1338 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 1339 static inline bool classof(const Instruction *I) { 1340 return I->getOpcode() == Instruction::FCmp; 1341 } 1342 static inline bool classof(const Value *V) { 1343 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1344 } 1345 }; 1346 1347 //===----------------------------------------------------------------------===// 1348 /// CallInst - This class represents a function call, abstracting a target 1349 /// machine's calling convention. This class uses low bit of the SubClassData 1350 /// field to indicate whether or not this is a tail call. The rest of the bits 1351 /// hold the calling convention of the call. 1352 /// 1353 class CallInst : public Instruction, 1354 public OperandBundleUser<CallInst, User::op_iterator> { 1355 AttributeSet AttributeList; ///< parameter attributes for call 1356 FunctionType *FTy; 1357 CallInst(const CallInst &CI); 1358 void init(Value *Func, ArrayRef<Value *> Args, 1359 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { 1360 init(cast<FunctionType>( 1361 cast<PointerType>(Func->getType())->getElementType()), 1362 Func, Args, Bundles, NameStr); 1363 } 1364 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, 1365 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); 1366 void init(Value *Func, const Twine &NameStr); 1367 1368 /// Construct a CallInst given a range of arguments. 1369 /// \brief Construct a CallInst from a range of arguments 1370 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, 1371 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, 1372 Instruction *InsertBefore); 1373 inline CallInst(Value *Func, ArrayRef<Value *> Args, 1374 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, 1375 Instruction *InsertBefore) 1376 : CallInst(cast<FunctionType>( 1377 cast<PointerType>(Func->getType())->getElementType()), 1378 Func, Args, Bundles, NameStr, InsertBefore) {} 1379 1380 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr, 1381 Instruction *InsertBefore) 1382 : CallInst(Func, Args, None, NameStr, InsertBefore) {} 1383 1384 /// Construct a CallInst given a range of arguments. 1385 /// \brief Construct a CallInst from a range of arguments 1386 inline CallInst(Value *Func, ArrayRef<Value *> Args, 1387 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, 1388 BasicBlock *InsertAtEnd); 1389 1390 explicit CallInst(Value *F, const Twine &NameStr, 1391 Instruction *InsertBefore); 1392 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd); 1393 1394 friend class OperandBundleUser<CallInst, User::op_iterator>; 1395 bool hasDescriptor() const { return HasDescriptor; } 1396 1397 protected: 1398 // Note: Instruction needs to be a friend here to call cloneImpl. 1399 friend class Instruction; 1400 CallInst *cloneImpl() const; 1401 1402 public: 1403 static CallInst *Create(Value *Func, ArrayRef<Value *> Args, 1404 ArrayRef<OperandBundleDef> Bundles = None, 1405 const Twine &NameStr = "", 1406 Instruction *InsertBefore = nullptr) { 1407 return Create(cast<FunctionType>( 1408 cast<PointerType>(Func->getType())->getElementType()), 1409 Func, Args, Bundles, NameStr, InsertBefore); 1410 } 1411 static CallInst *Create(Value *Func, ArrayRef<Value *> Args, 1412 const Twine &NameStr, 1413 Instruction *InsertBefore = nullptr) { 1414 return Create(cast<FunctionType>( 1415 cast<PointerType>(Func->getType())->getElementType()), 1416 Func, Args, None, NameStr, InsertBefore); 1417 } 1418 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, 1419 const Twine &NameStr, 1420 Instruction *InsertBefore = nullptr) { 1421 return new (unsigned(Args.size() + 1)) 1422 CallInst(Ty, Func, Args, None, NameStr, InsertBefore); 1423 } 1424 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, 1425 ArrayRef<OperandBundleDef> Bundles = None, 1426 const Twine &NameStr = "", 1427 Instruction *InsertBefore = nullptr) { 1428 const unsigned TotalOps = 1429 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1; 1430 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); 1431 1432 return new (TotalOps, DescriptorBytes) 1433 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); 1434 } 1435 static CallInst *Create(Value *Func, ArrayRef<Value *> Args, 1436 ArrayRef<OperandBundleDef> Bundles, 1437 const Twine &NameStr, BasicBlock *InsertAtEnd) { 1438 const unsigned TotalOps = 1439 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1; 1440 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); 1441 1442 return new (TotalOps, DescriptorBytes) 1443 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd); 1444 } 1445 static CallInst *Create(Value *Func, ArrayRef<Value *> Args, 1446 const Twine &NameStr, BasicBlock *InsertAtEnd) { 1447 return new (unsigned(Args.size() + 1)) 1448 CallInst(Func, Args, None, NameStr, InsertAtEnd); 1449 } 1450 static CallInst *Create(Value *F, const Twine &NameStr = "", 1451 Instruction *InsertBefore = nullptr) { 1452 return new(1) CallInst(F, NameStr, InsertBefore); 1453 } 1454 static CallInst *Create(Value *F, const Twine &NameStr, 1455 BasicBlock *InsertAtEnd) { 1456 return new(1) CallInst(F, NameStr, InsertAtEnd); 1457 } 1458 1459 /// \brief Create a clone of \p CI with a different set of operand bundles and 1460 /// insert it before \p InsertPt. 1461 /// 1462 /// The returned call instruction is identical \p CI in every way except that 1463 /// the operand bundles for the new instruction are set to the operand bundles 1464 /// in \p Bundles. 1465 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, 1466 Instruction *InsertPt = nullptr); 1467 1468 /// CreateMalloc - Generate the IR for a call to malloc: 1469 /// 1. Compute the malloc call's argument as the specified type's size, 1470 /// possibly multiplied by the array size if the array size is not 1471 /// constant 1. 1472 /// 2. Call malloc with that argument. 1473 /// 3. Bitcast the result of the malloc call to the specified type. 1474 static Instruction *CreateMalloc(Instruction *InsertBefore, 1475 Type *IntPtrTy, Type *AllocTy, 1476 Value *AllocSize, Value *ArraySize = nullptr, 1477 Function* MallocF = nullptr, 1478 const Twine &Name = ""); 1479 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, 1480 Type *IntPtrTy, Type *AllocTy, 1481 Value *AllocSize, Value *ArraySize = nullptr, 1482 Function* MallocF = nullptr, 1483 const Twine &Name = ""); 1484 static Instruction *CreateMalloc(Instruction *InsertBefore, 1485 Type *IntPtrTy, Type *AllocTy, 1486 Value *AllocSize, Value *ArraySize = nullptr, 1487 ArrayRef<OperandBundleDef> Bundles = None, 1488 Function* MallocF = nullptr, 1489 const Twine &Name = ""); 1490 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, 1491 Type *IntPtrTy, Type *AllocTy, 1492 Value *AllocSize, Value *ArraySize = nullptr, 1493 ArrayRef<OperandBundleDef> Bundles = None, 1494 Function* MallocF = nullptr, 1495 const Twine &Name = ""); 1496 /// CreateFree - Generate the IR for a call to the builtin free function. 1497 static Instruction *CreateFree(Value *Source, 1498 Instruction *InsertBefore); 1499 static Instruction *CreateFree(Value *Source, 1500 BasicBlock *InsertAtEnd); 1501 static Instruction *CreateFree(Value *Source, 1502 ArrayRef<OperandBundleDef> Bundles, 1503 Instruction *InsertBefore); 1504 static Instruction *CreateFree(Value *Source, 1505 ArrayRef<OperandBundleDef> Bundles, 1506 BasicBlock *InsertAtEnd); 1507 1508 ~CallInst() override; 1509 1510 FunctionType *getFunctionType() const { return FTy; } 1511 1512 void mutateFunctionType(FunctionType *FTy) { 1513 mutateType(FTy->getReturnType()); 1514 this->FTy = FTy; 1515 } 1516 1517 // Note that 'musttail' implies 'tail'. 1518 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2, 1519 TCK_NoTail = 3 }; 1520 TailCallKind getTailCallKind() const { 1521 return TailCallKind(getSubclassDataFromInstruction() & 3); 1522 } 1523 bool isTailCall() const { 1524 unsigned Kind = getSubclassDataFromInstruction() & 3; 1525 return Kind == TCK_Tail || Kind == TCK_MustTail; 1526 } 1527 bool isMustTailCall() const { 1528 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail; 1529 } 1530 bool isNoTailCall() const { 1531 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail; 1532 } 1533 void setTailCall(bool isTC = true) { 1534 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | 1535 unsigned(isTC ? TCK_Tail : TCK_None)); 1536 } 1537 void setTailCallKind(TailCallKind TCK) { 1538 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | 1539 unsigned(TCK)); 1540 } 1541 1542 /// Provide fast operand accessors 1543 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1544 1545 /// getNumArgOperands - Return the number of call arguments. 1546 /// 1547 unsigned getNumArgOperands() const { 1548 return getNumOperands() - getNumTotalBundleOperands() - 1; 1549 } 1550 1551 /// getArgOperand/setArgOperand - Return/set the i-th call argument. 1552 /// 1553 Value *getArgOperand(unsigned i) const { 1554 assert(i < getNumArgOperands() && "Out of bounds!"); 1555 return getOperand(i); 1556 } 1557 void setArgOperand(unsigned i, Value *v) { 1558 assert(i < getNumArgOperands() && "Out of bounds!"); 1559 setOperand(i, v); 1560 } 1561 1562 /// \brief Return the iterator pointing to the beginning of the argument list. 1563 op_iterator arg_begin() { return op_begin(); } 1564 1565 /// \brief Return the iterator pointing to the end of the argument list. 1566 op_iterator arg_end() { 1567 // [ call args ], [ operand bundles ], callee 1568 return op_end() - getNumTotalBundleOperands() - 1; 1569 }; 1570 1571 /// \brief Iteration adapter for range-for loops. 1572 iterator_range<op_iterator> arg_operands() { 1573 return make_range(arg_begin(), arg_end()); 1574 } 1575 1576 /// \brief Return the iterator pointing to the beginning of the argument list. 1577 const_op_iterator arg_begin() const { return op_begin(); } 1578 1579 /// \brief Return the iterator pointing to the end of the argument list. 1580 const_op_iterator arg_end() const { 1581 // [ call args ], [ operand bundles ], callee 1582 return op_end() - getNumTotalBundleOperands() - 1; 1583 }; 1584 1585 /// \brief Iteration adapter for range-for loops. 1586 iterator_range<const_op_iterator> arg_operands() const { 1587 return make_range(arg_begin(), arg_end()); 1588 } 1589 1590 /// \brief Wrappers for getting the \c Use of a call argument. 1591 const Use &getArgOperandUse(unsigned i) const { 1592 assert(i < getNumArgOperands() && "Out of bounds!"); 1593 return getOperandUse(i); 1594 } 1595 Use &getArgOperandUse(unsigned i) { 1596 assert(i < getNumArgOperands() && "Out of bounds!"); 1597 return getOperandUse(i); 1598 } 1599 1600 /// If one of the arguments has the 'returned' attribute, return its 1601 /// operand value. Otherwise, return nullptr. 1602 Value *getReturnedArgOperand() const; 1603 1604 /// getCallingConv/setCallingConv - Get or set the calling convention of this 1605 /// function call. 1606 CallingConv::ID getCallingConv() const { 1607 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2); 1608 } 1609 void setCallingConv(CallingConv::ID CC) { 1610 auto ID = static_cast<unsigned>(CC); 1611 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention"); 1612 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) | 1613 (ID << 2)); 1614 } 1615 1616 /// getAttributes - Return the parameter attributes for this call. 1617 /// 1618 const AttributeSet &getAttributes() const { return AttributeList; } 1619 1620 /// setAttributes - Set the parameter attributes for this call. 1621 /// 1622 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; } 1623 1624 /// addAttribute - adds the attribute to the list of attributes. 1625 void addAttribute(unsigned i, Attribute::AttrKind Kind); 1626 1627 /// addAttribute - adds the attribute to the list of attributes. 1628 void addAttribute(unsigned i, StringRef Kind, StringRef Value); 1629 1630 /// addAttribute - adds the attribute to the list of attributes. 1631 void addAttribute(unsigned i, Attribute Attr); 1632 1633 /// removeAttribute - removes the attribute from the list of attributes. 1634 void removeAttribute(unsigned i, Attribute::AttrKind Kind); 1635 1636 /// removeAttribute - removes the attribute from the list of attributes. 1637 void removeAttribute(unsigned i, StringRef Kind); 1638 1639 /// removeAttribute - removes the attribute from the list of attributes. 1640 void removeAttribute(unsigned i, Attribute Attr); 1641 1642 /// \brief adds the dereferenceable attribute to the list of attributes. 1643 void addDereferenceableAttr(unsigned i, uint64_t Bytes); 1644 1645 /// \brief adds the dereferenceable_or_null attribute to the list of 1646 /// attributes. 1647 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes); 1648 1649 /// \brief Determine whether this call has the given attribute. 1650 bool hasFnAttr(Attribute::AttrKind Kind) const { 1651 assert(Kind != Attribute::NoBuiltin && 1652 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin"); 1653 return hasFnAttrImpl(Kind); 1654 } 1655 1656 /// \brief Determine whether this call has the given attribute. 1657 bool hasFnAttr(StringRef Kind) const { 1658 return hasFnAttrImpl(Kind); 1659 } 1660 1661 /// \brief Determine whether the call or the callee has the given attributes. 1662 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const; 1663 1664 /// \brief Get the attribute of a given kind at a position. 1665 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const; 1666 1667 /// \brief Get the attribute of a given kind at a position. 1668 Attribute getAttribute(unsigned i, StringRef Kind) const; 1669 1670 /// \brief Return true if the data operand at index \p i has the attribute \p 1671 /// A. 1672 /// 1673 /// Data operands include call arguments and values used in operand bundles, 1674 /// but does not include the callee operand. This routine dispatches to the 1675 /// underlying AttributeList or the OperandBundleUser as appropriate. 1676 /// 1677 /// The index \p i is interpreted as 1678 /// 1679 /// \p i == Attribute::ReturnIndex -> the return value 1680 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) 1681 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index 1682 /// (\p i - 1) in the operand list. 1683 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const; 1684 1685 /// \brief Extract the alignment for a call or parameter (0=unknown). 1686 unsigned getParamAlignment(unsigned i) const { 1687 return AttributeList.getParamAlignment(i); 1688 } 1689 1690 /// \brief Extract the number of dereferenceable bytes for a call or 1691 /// parameter (0=unknown). 1692 uint64_t getDereferenceableBytes(unsigned i) const { 1693 return AttributeList.getDereferenceableBytes(i); 1694 } 1695 1696 /// \brief Extract the number of dereferenceable_or_null bytes for a call or 1697 /// parameter (0=unknown). 1698 uint64_t getDereferenceableOrNullBytes(unsigned i) const { 1699 return AttributeList.getDereferenceableOrNullBytes(i); 1700 } 1701 1702 /// @brief Determine if the parameter or return value is marked with NoAlias 1703 /// attribute. 1704 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 1705 bool doesNotAlias(unsigned n) const { 1706 return AttributeList.hasAttribute(n, Attribute::NoAlias); 1707 } 1708 1709 /// \brief Return true if the call should not be treated as a call to a 1710 /// builtin. 1711 bool isNoBuiltin() const { 1712 return hasFnAttrImpl(Attribute::NoBuiltin) && 1713 !hasFnAttrImpl(Attribute::Builtin); 1714 } 1715 1716 /// \brief Return true if the call should not be inlined. 1717 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } 1718 void setIsNoInline() { 1719 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline); 1720 } 1721 1722 /// \brief Return true if the call can return twice 1723 bool canReturnTwice() const { 1724 return hasFnAttr(Attribute::ReturnsTwice); 1725 } 1726 void setCanReturnTwice() { 1727 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice); 1728 } 1729 1730 /// \brief Determine if the call does not access memory. 1731 bool doesNotAccessMemory() const { 1732 return hasFnAttr(Attribute::ReadNone); 1733 } 1734 void setDoesNotAccessMemory() { 1735 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone); 1736 } 1737 1738 /// \brief Determine if the call does not access or only reads memory. 1739 bool onlyReadsMemory() const { 1740 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 1741 } 1742 void setOnlyReadsMemory() { 1743 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly); 1744 } 1745 1746 /// \brief Determine if the call does not access or only writes memory. 1747 bool doesNotReadMemory() const { 1748 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); 1749 } 1750 void setDoesNotReadMemory() { 1751 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly); 1752 } 1753 1754 /// @brief Determine if the call can access memmory only using pointers based 1755 /// on its arguments. 1756 bool onlyAccessesArgMemory() const { 1757 return hasFnAttr(Attribute::ArgMemOnly); 1758 } 1759 void setOnlyAccessesArgMemory() { 1760 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly); 1761 } 1762 1763 /// \brief Determine if the call cannot return. 1764 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } 1765 void setDoesNotReturn() { 1766 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn); 1767 } 1768 1769 /// \brief Determine if the call cannot unwind. 1770 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } 1771 void setDoesNotThrow() { 1772 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind); 1773 } 1774 1775 /// \brief Determine if the call cannot be duplicated. 1776 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); } 1777 void setCannotDuplicate() { 1778 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate); 1779 } 1780 1781 /// \brief Determine if the call is convergent 1782 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } 1783 void setConvergent() { 1784 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent); 1785 } 1786 void setNotConvergent() { 1787 removeAttribute(AttributeSet::FunctionIndex, 1788 Attribute::get(getContext(), Attribute::Convergent)); 1789 } 1790 1791 /// \brief Determine if the call returns a structure through first 1792 /// pointer argument. 1793 bool hasStructRetAttr() const { 1794 if (getNumArgOperands() == 0) 1795 return false; 1796 1797 // Be friendly and also check the callee. 1798 return paramHasAttr(1, Attribute::StructRet); 1799 } 1800 1801 /// \brief Determine if any call argument is an aggregate passed by value. 1802 bool hasByValArgument() const { 1803 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 1804 } 1805 1806 /// getCalledFunction - Return the function called, or null if this is an 1807 /// indirect function invocation. 1808 /// 1809 Function *getCalledFunction() const { 1810 return dyn_cast<Function>(Op<-1>()); 1811 } 1812 1813 /// getCalledValue - Get a pointer to the function that is invoked by this 1814 /// instruction. 1815 const Value *getCalledValue() const { return Op<-1>(); } 1816 Value *getCalledValue() { return Op<-1>(); } 1817 1818 /// setCalledFunction - Set the function called. 1819 void setCalledFunction(Value* Fn) { 1820 setCalledFunction( 1821 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()), 1822 Fn); 1823 } 1824 void setCalledFunction(FunctionType *FTy, Value *Fn) { 1825 this->FTy = FTy; 1826 assert(FTy == cast<FunctionType>( 1827 cast<PointerType>(Fn->getType())->getElementType())); 1828 Op<-1>() = Fn; 1829 } 1830 1831 /// isInlineAsm - Check if this call is an inline asm statement. 1832 bool isInlineAsm() const { 1833 return isa<InlineAsm>(Op<-1>()); 1834 } 1835 1836 // Methods for support type inquiry through isa, cast, and dyn_cast: 1837 static inline bool classof(const Instruction *I) { 1838 return I->getOpcode() == Instruction::Call; 1839 } 1840 static inline bool classof(const Value *V) { 1841 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1842 } 1843 1844 private: 1845 template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const { 1846 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) 1847 return true; 1848 1849 // Operand bundles override attributes on the called function, but don't 1850 // override attributes directly present on the call instruction. 1851 if (isFnAttrDisallowedByOpBundle(A)) 1852 return false; 1853 1854 if (const Function *F = getCalledFunction()) 1855 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); 1856 return false; 1857 } 1858 1859 // Shadow Instruction::setInstructionSubclassData with a private forwarding 1860 // method so that subclasses cannot accidentally use it. 1861 void setInstructionSubclassData(unsigned short D) { 1862 Instruction::setInstructionSubclassData(D); 1863 } 1864 }; 1865 1866 template <> 1867 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> { 1868 }; 1869 1870 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args, 1871 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, 1872 BasicBlock *InsertAtEnd) 1873 : Instruction( 1874 cast<FunctionType>(cast<PointerType>(Func->getType()) 1875 ->getElementType())->getReturnType(), 1876 Instruction::Call, OperandTraits<CallInst>::op_end(this) - 1877 (Args.size() + CountBundleInputs(Bundles) + 1), 1878 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) { 1879 init(Func, Args, Bundles, NameStr); 1880 } 1881 1882 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, 1883 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, 1884 Instruction *InsertBefore) 1885 : Instruction(Ty->getReturnType(), Instruction::Call, 1886 OperandTraits<CallInst>::op_end(this) - 1887 (Args.size() + CountBundleInputs(Bundles) + 1), 1888 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), 1889 InsertBefore) { 1890 init(Ty, Func, Args, Bundles, NameStr); 1891 } 1892 1893 // Note: if you get compile errors about private methods then 1894 // please update your code to use the high-level operand 1895 // interfaces. See line 943 above. 1896 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value) 1897 1898 //===----------------------------------------------------------------------===// 1899 // SelectInst Class 1900 //===----------------------------------------------------------------------===// 1901 1902 /// SelectInst - This class represents the LLVM 'select' instruction. 1903 /// 1904 class SelectInst : public Instruction { 1905 void init(Value *C, Value *S1, Value *S2) { 1906 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select"); 1907 Op<0>() = C; 1908 Op<1>() = S1; 1909 Op<2>() = S2; 1910 } 1911 1912 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1913 Instruction *InsertBefore) 1914 : Instruction(S1->getType(), Instruction::Select, 1915 &Op<0>(), 3, InsertBefore) { 1916 init(C, S1, S2); 1917 setName(NameStr); 1918 } 1919 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, 1920 BasicBlock *InsertAtEnd) 1921 : Instruction(S1->getType(), Instruction::Select, 1922 &Op<0>(), 3, InsertAtEnd) { 1923 init(C, S1, S2); 1924 setName(NameStr); 1925 } 1926 1927 protected: 1928 // Note: Instruction needs to be a friend here to call cloneImpl. 1929 friend class Instruction; 1930 SelectInst *cloneImpl() const; 1931 1932 public: 1933 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1934 const Twine &NameStr = "", 1935 Instruction *InsertBefore = nullptr) { 1936 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); 1937 } 1938 static SelectInst *Create(Value *C, Value *S1, Value *S2, 1939 const Twine &NameStr, 1940 BasicBlock *InsertAtEnd) { 1941 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); 1942 } 1943 1944 const Value *getCondition() const { return Op<0>(); } 1945 const Value *getTrueValue() const { return Op<1>(); } 1946 const Value *getFalseValue() const { return Op<2>(); } 1947 Value *getCondition() { return Op<0>(); } 1948 Value *getTrueValue() { return Op<1>(); } 1949 Value *getFalseValue() { return Op<2>(); } 1950 1951 void setCondition(Value *V) { Op<0>() = V; } 1952 void setTrueValue(Value *V) { Op<1>() = V; } 1953 void setFalseValue(Value *V) { Op<2>() = V; } 1954 1955 /// areInvalidOperands - Return a string if the specified operands are invalid 1956 /// for a select operation, otherwise return null. 1957 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); 1958 1959 /// Transparently provide more efficient getOperand methods. 1960 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 1961 1962 OtherOps getOpcode() const { 1963 return static_cast<OtherOps>(Instruction::getOpcode()); 1964 } 1965 1966 // Methods for support type inquiry through isa, cast, and dyn_cast: 1967 static inline bool classof(const Instruction *I) { 1968 return I->getOpcode() == Instruction::Select; 1969 } 1970 static inline bool classof(const Value *V) { 1971 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 1972 } 1973 }; 1974 1975 template <> 1976 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { 1977 }; 1978 1979 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value) 1980 1981 //===----------------------------------------------------------------------===// 1982 // VAArgInst Class 1983 //===----------------------------------------------------------------------===// 1984 1985 /// VAArgInst - This class represents the va_arg llvm instruction, which returns 1986 /// an argument of the specified type given a va_list and increments that list 1987 /// 1988 class VAArgInst : public UnaryInstruction { 1989 protected: 1990 // Note: Instruction needs to be a friend here to call cloneImpl. 1991 friend class Instruction; 1992 VAArgInst *cloneImpl() const; 1993 1994 public: 1995 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", 1996 Instruction *InsertBefore = nullptr) 1997 : UnaryInstruction(Ty, VAArg, List, InsertBefore) { 1998 setName(NameStr); 1999 } 2000 VAArgInst(Value *List, Type *Ty, const Twine &NameStr, 2001 BasicBlock *InsertAtEnd) 2002 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { 2003 setName(NameStr); 2004 } 2005 2006 Value *getPointerOperand() { return getOperand(0); } 2007 const Value *getPointerOperand() const { return getOperand(0); } 2008 static unsigned getPointerOperandIndex() { return 0U; } 2009 2010 // Methods for support type inquiry through isa, cast, and dyn_cast: 2011 static inline bool classof(const Instruction *I) { 2012 return I->getOpcode() == VAArg; 2013 } 2014 static inline bool classof(const Value *V) { 2015 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2016 } 2017 }; 2018 2019 //===----------------------------------------------------------------------===// 2020 // ExtractElementInst Class 2021 //===----------------------------------------------------------------------===// 2022 2023 /// ExtractElementInst - This instruction extracts a single (scalar) 2024 /// element from a VectorType value 2025 /// 2026 class ExtractElementInst : public Instruction { 2027 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", 2028 Instruction *InsertBefore = nullptr); 2029 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, 2030 BasicBlock *InsertAtEnd); 2031 2032 protected: 2033 // Note: Instruction needs to be a friend here to call cloneImpl. 2034 friend class Instruction; 2035 ExtractElementInst *cloneImpl() const; 2036 2037 public: 2038 static ExtractElementInst *Create(Value *Vec, Value *Idx, 2039 const Twine &NameStr = "", 2040 Instruction *InsertBefore = nullptr) { 2041 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); 2042 } 2043 static ExtractElementInst *Create(Value *Vec, Value *Idx, 2044 const Twine &NameStr, 2045 BasicBlock *InsertAtEnd) { 2046 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); 2047 } 2048 2049 /// isValidOperands - Return true if an extractelement instruction can be 2050 /// formed with the specified operands. 2051 static bool isValidOperands(const Value *Vec, const Value *Idx); 2052 2053 Value *getVectorOperand() { return Op<0>(); } 2054 Value *getIndexOperand() { return Op<1>(); } 2055 const Value *getVectorOperand() const { return Op<0>(); } 2056 const Value *getIndexOperand() const { return Op<1>(); } 2057 2058 VectorType *getVectorOperandType() const { 2059 return cast<VectorType>(getVectorOperand()->getType()); 2060 } 2061 2062 /// Transparently provide more efficient getOperand methods. 2063 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2064 2065 // Methods for support type inquiry through isa, cast, and dyn_cast: 2066 static inline bool classof(const Instruction *I) { 2067 return I->getOpcode() == Instruction::ExtractElement; 2068 } 2069 static inline bool classof(const Value *V) { 2070 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2071 } 2072 }; 2073 2074 template <> 2075 struct OperandTraits<ExtractElementInst> : 2076 public FixedNumOperandTraits<ExtractElementInst, 2> { 2077 }; 2078 2079 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value) 2080 2081 //===----------------------------------------------------------------------===// 2082 // InsertElementInst Class 2083 //===----------------------------------------------------------------------===// 2084 2085 /// InsertElementInst - This instruction inserts a single (scalar) 2086 /// element into a VectorType value 2087 /// 2088 class InsertElementInst : public Instruction { 2089 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, 2090 const Twine &NameStr = "", 2091 Instruction *InsertBefore = nullptr); 2092 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, 2093 BasicBlock *InsertAtEnd); 2094 2095 protected: 2096 // Note: Instruction needs to be a friend here to call cloneImpl. 2097 friend class Instruction; 2098 InsertElementInst *cloneImpl() const; 2099 2100 public: 2101 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 2102 const Twine &NameStr = "", 2103 Instruction *InsertBefore = nullptr) { 2104 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); 2105 } 2106 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, 2107 const Twine &NameStr, 2108 BasicBlock *InsertAtEnd) { 2109 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); 2110 } 2111 2112 /// isValidOperands - Return true if an insertelement instruction can be 2113 /// formed with the specified operands. 2114 static bool isValidOperands(const Value *Vec, const Value *NewElt, 2115 const Value *Idx); 2116 2117 /// getType - Overload to return most specific vector type. 2118 /// 2119 VectorType *getType() const { 2120 return cast<VectorType>(Instruction::getType()); 2121 } 2122 2123 /// Transparently provide more efficient getOperand methods. 2124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2125 2126 // Methods for support type inquiry through isa, cast, and dyn_cast: 2127 static inline bool classof(const Instruction *I) { 2128 return I->getOpcode() == Instruction::InsertElement; 2129 } 2130 static inline bool classof(const Value *V) { 2131 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2132 } 2133 }; 2134 2135 template <> 2136 struct OperandTraits<InsertElementInst> : 2137 public FixedNumOperandTraits<InsertElementInst, 3> { 2138 }; 2139 2140 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value) 2141 2142 //===----------------------------------------------------------------------===// 2143 // ShuffleVectorInst Class 2144 //===----------------------------------------------------------------------===// 2145 2146 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two 2147 /// input vectors. 2148 /// 2149 class ShuffleVectorInst : public Instruction { 2150 protected: 2151 // Note: Instruction needs to be a friend here to call cloneImpl. 2152 friend class Instruction; 2153 ShuffleVectorInst *cloneImpl() const; 2154 2155 public: 2156 // allocate space for exactly three operands 2157 void *operator new(size_t s) { 2158 return User::operator new(s, 3); 2159 } 2160 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 2161 const Twine &NameStr = "", 2162 Instruction *InsertBefor = nullptr); 2163 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 2164 const Twine &NameStr, BasicBlock *InsertAtEnd); 2165 2166 /// isValidOperands - Return true if a shufflevector instruction can be 2167 /// formed with the specified operands. 2168 static bool isValidOperands(const Value *V1, const Value *V2, 2169 const Value *Mask); 2170 2171 /// getType - Overload to return most specific vector type. 2172 /// 2173 VectorType *getType() const { 2174 return cast<VectorType>(Instruction::getType()); 2175 } 2176 2177 /// Transparently provide more efficient getOperand methods. 2178 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2179 2180 Constant *getMask() const { 2181 return cast<Constant>(getOperand(2)); 2182 } 2183 2184 /// getMaskValue - Return the index from the shuffle mask for the specified 2185 /// output result. This is either -1 if the element is undef or a number less 2186 /// than 2*numelements. 2187 static int getMaskValue(Constant *Mask, unsigned i); 2188 2189 int getMaskValue(unsigned i) const { 2190 return getMaskValue(getMask(), i); 2191 } 2192 2193 /// getShuffleMask - Return the full mask for this instruction, where each 2194 /// element is the element number and undef's are returned as -1. 2195 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result); 2196 2197 void getShuffleMask(SmallVectorImpl<int> &Result) const { 2198 return getShuffleMask(getMask(), Result); 2199 } 2200 2201 SmallVector<int, 16> getShuffleMask() const { 2202 SmallVector<int, 16> Mask; 2203 getShuffleMask(Mask); 2204 return Mask; 2205 } 2206 2207 // Methods for support type inquiry through isa, cast, and dyn_cast: 2208 static inline bool classof(const Instruction *I) { 2209 return I->getOpcode() == Instruction::ShuffleVector; 2210 } 2211 static inline bool classof(const Value *V) { 2212 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2213 } 2214 }; 2215 2216 template <> 2217 struct OperandTraits<ShuffleVectorInst> : 2218 public FixedNumOperandTraits<ShuffleVectorInst, 3> { 2219 }; 2220 2221 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value) 2222 2223 //===----------------------------------------------------------------------===// 2224 // ExtractValueInst Class 2225 //===----------------------------------------------------------------------===// 2226 2227 /// ExtractValueInst - This instruction extracts a struct member or array 2228 /// element value from an aggregate value. 2229 /// 2230 class ExtractValueInst : public UnaryInstruction { 2231 SmallVector<unsigned, 4> Indices; 2232 2233 ExtractValueInst(const ExtractValueInst &EVI); 2234 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); 2235 2236 /// Constructors - Create a extractvalue instruction with a base aggregate 2237 /// value and a list of indices. The first ctor can optionally insert before 2238 /// an existing instruction, the second appends the new instruction to the 2239 /// specified BasicBlock. 2240 inline ExtractValueInst(Value *Agg, 2241 ArrayRef<unsigned> Idxs, 2242 const Twine &NameStr, 2243 Instruction *InsertBefore); 2244 inline ExtractValueInst(Value *Agg, 2245 ArrayRef<unsigned> Idxs, 2246 const Twine &NameStr, BasicBlock *InsertAtEnd); 2247 2248 // allocate space for exactly one operand 2249 void *operator new(size_t s) { return User::operator new(s, 1); } 2250 2251 protected: 2252 // Note: Instruction needs to be a friend here to call cloneImpl. 2253 friend class Instruction; 2254 ExtractValueInst *cloneImpl() const; 2255 2256 public: 2257 static ExtractValueInst *Create(Value *Agg, 2258 ArrayRef<unsigned> Idxs, 2259 const Twine &NameStr = "", 2260 Instruction *InsertBefore = nullptr) { 2261 return new 2262 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); 2263 } 2264 static ExtractValueInst *Create(Value *Agg, 2265 ArrayRef<unsigned> Idxs, 2266 const Twine &NameStr, 2267 BasicBlock *InsertAtEnd) { 2268 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); 2269 } 2270 2271 /// getIndexedType - Returns the type of the element that would be extracted 2272 /// with an extractvalue instruction with the specified parameters. 2273 /// 2274 /// Null is returned if the indices are invalid for the specified type. 2275 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); 2276 2277 typedef const unsigned* idx_iterator; 2278 inline idx_iterator idx_begin() const { return Indices.begin(); } 2279 inline idx_iterator idx_end() const { return Indices.end(); } 2280 inline iterator_range<idx_iterator> indices() const { 2281 return make_range(idx_begin(), idx_end()); 2282 } 2283 2284 Value *getAggregateOperand() { 2285 return getOperand(0); 2286 } 2287 const Value *getAggregateOperand() const { 2288 return getOperand(0); 2289 } 2290 static unsigned getAggregateOperandIndex() { 2291 return 0U; // get index for modifying correct operand 2292 } 2293 2294 ArrayRef<unsigned> getIndices() const { 2295 return Indices; 2296 } 2297 2298 unsigned getNumIndices() const { 2299 return (unsigned)Indices.size(); 2300 } 2301 2302 bool hasIndices() const { 2303 return true; 2304 } 2305 2306 // Methods for support type inquiry through isa, cast, and dyn_cast: 2307 static inline bool classof(const Instruction *I) { 2308 return I->getOpcode() == Instruction::ExtractValue; 2309 } 2310 static inline bool classof(const Value *V) { 2311 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2312 } 2313 }; 2314 2315 ExtractValueInst::ExtractValueInst(Value *Agg, 2316 ArrayRef<unsigned> Idxs, 2317 const Twine &NameStr, 2318 Instruction *InsertBefore) 2319 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 2320 ExtractValue, Agg, InsertBefore) { 2321 init(Idxs, NameStr); 2322 } 2323 ExtractValueInst::ExtractValueInst(Value *Agg, 2324 ArrayRef<unsigned> Idxs, 2325 const Twine &NameStr, 2326 BasicBlock *InsertAtEnd) 2327 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), 2328 ExtractValue, Agg, InsertAtEnd) { 2329 init(Idxs, NameStr); 2330 } 2331 2332 //===----------------------------------------------------------------------===// 2333 // InsertValueInst Class 2334 //===----------------------------------------------------------------------===// 2335 2336 /// InsertValueInst - This instruction inserts a struct field of array element 2337 /// value into an aggregate value. 2338 /// 2339 class InsertValueInst : public Instruction { 2340 SmallVector<unsigned, 4> Indices; 2341 2342 void *operator new(size_t, unsigned) = delete; 2343 InsertValueInst(const InsertValueInst &IVI); 2344 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 2345 const Twine &NameStr); 2346 2347 /// Constructors - Create a insertvalue instruction with a base aggregate 2348 /// value, a value to insert, and a list of indices. The first ctor can 2349 /// optionally insert before an existing instruction, the second appends 2350 /// the new instruction to the specified BasicBlock. 2351 inline InsertValueInst(Value *Agg, Value *Val, 2352 ArrayRef<unsigned> Idxs, 2353 const Twine &NameStr, 2354 Instruction *InsertBefore); 2355 inline InsertValueInst(Value *Agg, Value *Val, 2356 ArrayRef<unsigned> Idxs, 2357 const Twine &NameStr, BasicBlock *InsertAtEnd); 2358 2359 /// Constructors - These two constructors are convenience methods because one 2360 /// and two index insertvalue instructions are so common. 2361 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, 2362 const Twine &NameStr = "", 2363 Instruction *InsertBefore = nullptr); 2364 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, 2365 BasicBlock *InsertAtEnd); 2366 2367 protected: 2368 // Note: Instruction needs to be a friend here to call cloneImpl. 2369 friend class Instruction; 2370 InsertValueInst *cloneImpl() const; 2371 2372 public: 2373 // allocate space for exactly two operands 2374 void *operator new(size_t s) { 2375 return User::operator new(s, 2); 2376 } 2377 2378 static InsertValueInst *Create(Value *Agg, Value *Val, 2379 ArrayRef<unsigned> Idxs, 2380 const Twine &NameStr = "", 2381 Instruction *InsertBefore = nullptr) { 2382 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); 2383 } 2384 static InsertValueInst *Create(Value *Agg, Value *Val, 2385 ArrayRef<unsigned> Idxs, 2386 const Twine &NameStr, 2387 BasicBlock *InsertAtEnd) { 2388 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); 2389 } 2390 2391 /// Transparently provide more efficient getOperand methods. 2392 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2393 2394 typedef const unsigned* idx_iterator; 2395 inline idx_iterator idx_begin() const { return Indices.begin(); } 2396 inline idx_iterator idx_end() const { return Indices.end(); } 2397 inline iterator_range<idx_iterator> indices() const { 2398 return make_range(idx_begin(), idx_end()); 2399 } 2400 2401 Value *getAggregateOperand() { 2402 return getOperand(0); 2403 } 2404 const Value *getAggregateOperand() const { 2405 return getOperand(0); 2406 } 2407 static unsigned getAggregateOperandIndex() { 2408 return 0U; // get index for modifying correct operand 2409 } 2410 2411 Value *getInsertedValueOperand() { 2412 return getOperand(1); 2413 } 2414 const Value *getInsertedValueOperand() const { 2415 return getOperand(1); 2416 } 2417 static unsigned getInsertedValueOperandIndex() { 2418 return 1U; // get index for modifying correct operand 2419 } 2420 2421 ArrayRef<unsigned> getIndices() const { 2422 return Indices; 2423 } 2424 2425 unsigned getNumIndices() const { 2426 return (unsigned)Indices.size(); 2427 } 2428 2429 bool hasIndices() const { 2430 return true; 2431 } 2432 2433 // Methods for support type inquiry through isa, cast, and dyn_cast: 2434 static inline bool classof(const Instruction *I) { 2435 return I->getOpcode() == Instruction::InsertValue; 2436 } 2437 static inline bool classof(const Value *V) { 2438 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2439 } 2440 }; 2441 2442 template <> 2443 struct OperandTraits<InsertValueInst> : 2444 public FixedNumOperandTraits<InsertValueInst, 2> { 2445 }; 2446 2447 InsertValueInst::InsertValueInst(Value *Agg, 2448 Value *Val, 2449 ArrayRef<unsigned> Idxs, 2450 const Twine &NameStr, 2451 Instruction *InsertBefore) 2452 : Instruction(Agg->getType(), InsertValue, 2453 OperandTraits<InsertValueInst>::op_begin(this), 2454 2, InsertBefore) { 2455 init(Agg, Val, Idxs, NameStr); 2456 } 2457 InsertValueInst::InsertValueInst(Value *Agg, 2458 Value *Val, 2459 ArrayRef<unsigned> Idxs, 2460 const Twine &NameStr, 2461 BasicBlock *InsertAtEnd) 2462 : Instruction(Agg->getType(), InsertValue, 2463 OperandTraits<InsertValueInst>::op_begin(this), 2464 2, InsertAtEnd) { 2465 init(Agg, Val, Idxs, NameStr); 2466 } 2467 2468 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value) 2469 2470 //===----------------------------------------------------------------------===// 2471 // PHINode Class 2472 //===----------------------------------------------------------------------===// 2473 2474 // PHINode - The PHINode class is used to represent the magical mystical PHI 2475 // node, that can not exist in nature, but can be synthesized in a computer 2476 // scientist's overactive imagination. 2477 // 2478 class PHINode : public Instruction { 2479 void anchor() override; 2480 2481 void *operator new(size_t, unsigned) = delete; 2482 /// ReservedSpace - The number of operands actually allocated. NumOperands is 2483 /// the number actually in use. 2484 unsigned ReservedSpace; 2485 PHINode(const PHINode &PN); 2486 // allocate space for exactly zero operands 2487 void *operator new(size_t s) { 2488 return User::operator new(s); 2489 } 2490 explicit PHINode(Type *Ty, unsigned NumReservedValues, 2491 const Twine &NameStr = "", 2492 Instruction *InsertBefore = nullptr) 2493 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), 2494 ReservedSpace(NumReservedValues) { 2495 setName(NameStr); 2496 allocHungoffUses(ReservedSpace); 2497 } 2498 2499 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, 2500 BasicBlock *InsertAtEnd) 2501 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), 2502 ReservedSpace(NumReservedValues) { 2503 setName(NameStr); 2504 allocHungoffUses(ReservedSpace); 2505 } 2506 2507 protected: 2508 // allocHungoffUses - this is more complicated than the generic 2509 // User::allocHungoffUses, because we have to allocate Uses for the incoming 2510 // values and pointers to the incoming blocks, all in one allocation. 2511 void allocHungoffUses(unsigned N) { 2512 User::allocHungoffUses(N, /* IsPhi */ true); 2513 } 2514 2515 // Note: Instruction needs to be a friend here to call cloneImpl. 2516 friend class Instruction; 2517 PHINode *cloneImpl() const; 2518 2519 public: 2520 /// Constructors - NumReservedValues is a hint for the number of incoming 2521 /// edges that this phi node will have (use 0 if you really have no idea). 2522 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 2523 const Twine &NameStr = "", 2524 Instruction *InsertBefore = nullptr) { 2525 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); 2526 } 2527 static PHINode *Create(Type *Ty, unsigned NumReservedValues, 2528 const Twine &NameStr, BasicBlock *InsertAtEnd) { 2529 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); 2530 } 2531 2532 /// Provide fast operand accessors 2533 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2534 2535 // Block iterator interface. This provides access to the list of incoming 2536 // basic blocks, which parallels the list of incoming values. 2537 2538 typedef BasicBlock **block_iterator; 2539 typedef BasicBlock * const *const_block_iterator; 2540 2541 block_iterator block_begin() { 2542 Use::UserRef *ref = 2543 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace); 2544 return reinterpret_cast<block_iterator>(ref + 1); 2545 } 2546 2547 const_block_iterator block_begin() const { 2548 const Use::UserRef *ref = 2549 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace); 2550 return reinterpret_cast<const_block_iterator>(ref + 1); 2551 } 2552 2553 block_iterator block_end() { 2554 return block_begin() + getNumOperands(); 2555 } 2556 2557 const_block_iterator block_end() const { 2558 return block_begin() + getNumOperands(); 2559 } 2560 2561 iterator_range<block_iterator> blocks() { 2562 return make_range(block_begin(), block_end()); 2563 } 2564 2565 iterator_range<const_block_iterator> blocks() const { 2566 return make_range(block_begin(), block_end()); 2567 } 2568 2569 op_range incoming_values() { return operands(); } 2570 2571 const_op_range incoming_values() const { return operands(); } 2572 2573 /// getNumIncomingValues - Return the number of incoming edges 2574 /// 2575 unsigned getNumIncomingValues() const { return getNumOperands(); } 2576 2577 /// getIncomingValue - Return incoming value number x 2578 /// 2579 Value *getIncomingValue(unsigned i) const { 2580 return getOperand(i); 2581 } 2582 void setIncomingValue(unsigned i, Value *V) { 2583 assert(V && "PHI node got a null value!"); 2584 assert(getType() == V->getType() && 2585 "All operands to PHI node must be the same type as the PHI node!"); 2586 setOperand(i, V); 2587 } 2588 static unsigned getOperandNumForIncomingValue(unsigned i) { 2589 return i; 2590 } 2591 static unsigned getIncomingValueNumForOperand(unsigned i) { 2592 return i; 2593 } 2594 2595 /// getIncomingBlock - Return incoming basic block number @p i. 2596 /// 2597 BasicBlock *getIncomingBlock(unsigned i) const { 2598 return block_begin()[i]; 2599 } 2600 2601 /// getIncomingBlock - Return incoming basic block corresponding 2602 /// to an operand of the PHI. 2603 /// 2604 BasicBlock *getIncomingBlock(const Use &U) const { 2605 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?"); 2606 return getIncomingBlock(unsigned(&U - op_begin())); 2607 } 2608 2609 /// getIncomingBlock - Return incoming basic block corresponding 2610 /// to value use iterator. 2611 /// 2612 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { 2613 return getIncomingBlock(I.getUse()); 2614 } 2615 2616 void setIncomingBlock(unsigned i, BasicBlock *BB) { 2617 assert(BB && "PHI node got a null basic block!"); 2618 block_begin()[i] = BB; 2619 } 2620 2621 /// addIncoming - Add an incoming value to the end of the PHI list 2622 /// 2623 void addIncoming(Value *V, BasicBlock *BB) { 2624 if (getNumOperands() == ReservedSpace) 2625 growOperands(); // Get more space! 2626 // Initialize some new operands. 2627 setNumHungOffUseOperands(getNumOperands() + 1); 2628 setIncomingValue(getNumOperands() - 1, V); 2629 setIncomingBlock(getNumOperands() - 1, BB); 2630 } 2631 2632 /// removeIncomingValue - Remove an incoming value. This is useful if a 2633 /// predecessor basic block is deleted. The value removed is returned. 2634 /// 2635 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty 2636 /// is true), the PHI node is destroyed and any uses of it are replaced with 2637 /// dummy values. The only time there should be zero incoming values to a PHI 2638 /// node is when the block is dead, so this strategy is sound. 2639 /// 2640 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); 2641 2642 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { 2643 int Idx = getBasicBlockIndex(BB); 2644 assert(Idx >= 0 && "Invalid basic block argument to remove!"); 2645 return removeIncomingValue(Idx, DeletePHIIfEmpty); 2646 } 2647 2648 /// getBasicBlockIndex - Return the first index of the specified basic 2649 /// block in the value list for this PHI. Returns -1 if no instance. 2650 /// 2651 int getBasicBlockIndex(const BasicBlock *BB) const { 2652 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 2653 if (block_begin()[i] == BB) 2654 return i; 2655 return -1; 2656 } 2657 2658 Value *getIncomingValueForBlock(const BasicBlock *BB) const { 2659 int Idx = getBasicBlockIndex(BB); 2660 assert(Idx >= 0 && "Invalid basic block argument!"); 2661 return getIncomingValue(Idx); 2662 } 2663 2664 /// hasConstantValue - If the specified PHI node always merges together the 2665 /// same value, return the value, otherwise return null. 2666 Value *hasConstantValue() const; 2667 2668 /// hasConstantOrUndefValue - Whether the specified PHI node always merges 2669 /// together the same value, assuming undefs are equal to a unique 2670 /// non-undef value. 2671 bool hasConstantOrUndefValue() const; 2672 2673 /// Methods for support type inquiry through isa, cast, and dyn_cast: 2674 static inline bool classof(const Instruction *I) { 2675 return I->getOpcode() == Instruction::PHI; 2676 } 2677 static inline bool classof(const Value *V) { 2678 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2679 } 2680 2681 private: 2682 void growOperands(); 2683 }; 2684 2685 template <> 2686 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { 2687 }; 2688 2689 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value) 2690 2691 //===----------------------------------------------------------------------===// 2692 // LandingPadInst Class 2693 //===----------------------------------------------------------------------===// 2694 2695 //===--------------------------------------------------------------------------- 2696 /// LandingPadInst - The landingpad instruction holds all of the information 2697 /// necessary to generate correct exception handling. The landingpad instruction 2698 /// cannot be moved from the top of a landing pad block, which itself is 2699 /// accessible only from the 'unwind' edge of an invoke. This uses the 2700 /// SubclassData field in Value to store whether or not the landingpad is a 2701 /// cleanup. 2702 /// 2703 class LandingPadInst : public Instruction { 2704 /// ReservedSpace - The number of operands actually allocated. NumOperands is 2705 /// the number actually in use. 2706 unsigned ReservedSpace; 2707 LandingPadInst(const LandingPadInst &LP); 2708 2709 public: 2710 enum ClauseType { Catch, Filter }; 2711 2712 private: 2713 void *operator new(size_t, unsigned) = delete; 2714 // Allocate space for exactly zero operands. 2715 void *operator new(size_t s) { 2716 return User::operator new(s); 2717 } 2718 void growOperands(unsigned Size); 2719 void init(unsigned NumReservedValues, const Twine &NameStr); 2720 2721 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, 2722 const Twine &NameStr, Instruction *InsertBefore); 2723 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, 2724 const Twine &NameStr, BasicBlock *InsertAtEnd); 2725 2726 protected: 2727 // Note: Instruction needs to be a friend here to call cloneImpl. 2728 friend class Instruction; 2729 LandingPadInst *cloneImpl() const; 2730 2731 public: 2732 /// Constructors - NumReservedClauses is a hint for the number of incoming 2733 /// clauses that this landingpad will have (use 0 if you really have no idea). 2734 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, 2735 const Twine &NameStr = "", 2736 Instruction *InsertBefore = nullptr); 2737 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, 2738 const Twine &NameStr, BasicBlock *InsertAtEnd); 2739 2740 /// Provide fast operand accessors 2741 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2742 2743 /// isCleanup - Return 'true' if this landingpad instruction is a 2744 /// cleanup. I.e., it should be run when unwinding even if its landing pad 2745 /// doesn't catch the exception. 2746 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; } 2747 2748 /// setCleanup - Indicate that this landingpad instruction is a cleanup. 2749 void setCleanup(bool V) { 2750 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | 2751 (V ? 1 : 0)); 2752 } 2753 2754 /// Add a catch or filter clause to the landing pad. 2755 void addClause(Constant *ClauseVal); 2756 2757 /// Get the value of the clause at index Idx. Use isCatch/isFilter to 2758 /// determine what type of clause this is. 2759 Constant *getClause(unsigned Idx) const { 2760 return cast<Constant>(getOperandList()[Idx]); 2761 } 2762 2763 /// isCatch - Return 'true' if the clause and index Idx is a catch clause. 2764 bool isCatch(unsigned Idx) const { 2765 return !isa<ArrayType>(getOperandList()[Idx]->getType()); 2766 } 2767 2768 /// isFilter - Return 'true' if the clause and index Idx is a filter clause. 2769 bool isFilter(unsigned Idx) const { 2770 return isa<ArrayType>(getOperandList()[Idx]->getType()); 2771 } 2772 2773 /// getNumClauses - Get the number of clauses for this landing pad. 2774 unsigned getNumClauses() const { return getNumOperands(); } 2775 2776 /// reserveClauses - Grow the size of the operand list to accommodate the new 2777 /// number of clauses. 2778 void reserveClauses(unsigned Size) { growOperands(Size); } 2779 2780 // Methods for support type inquiry through isa, cast, and dyn_cast: 2781 static inline bool classof(const Instruction *I) { 2782 return I->getOpcode() == Instruction::LandingPad; 2783 } 2784 static inline bool classof(const Value *V) { 2785 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2786 } 2787 }; 2788 2789 template <> 2790 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { 2791 }; 2792 2793 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value) 2794 2795 //===----------------------------------------------------------------------===// 2796 // ReturnInst Class 2797 //===----------------------------------------------------------------------===// 2798 2799 //===--------------------------------------------------------------------------- 2800 /// ReturnInst - Return a value (possibly void), from a function. Execution 2801 /// does not continue in this function any longer. 2802 /// 2803 class ReturnInst : public TerminatorInst { 2804 ReturnInst(const ReturnInst &RI); 2805 2806 private: 2807 // ReturnInst constructors: 2808 // ReturnInst() - 'ret void' instruction 2809 // ReturnInst( null) - 'ret void' instruction 2810 // ReturnInst(Value* X) - 'ret X' instruction 2811 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I 2812 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I 2813 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B 2814 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B 2815 // 2816 // NOTE: If the Value* passed is of type void then the constructor behaves as 2817 // if it was passed NULL. 2818 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, 2819 Instruction *InsertBefore = nullptr); 2820 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); 2821 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); 2822 2823 protected: 2824 // Note: Instruction needs to be a friend here to call cloneImpl. 2825 friend class Instruction; 2826 ReturnInst *cloneImpl() const; 2827 2828 public: 2829 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, 2830 Instruction *InsertBefore = nullptr) { 2831 return new(!!retVal) ReturnInst(C, retVal, InsertBefore); 2832 } 2833 static ReturnInst* Create(LLVMContext &C, Value *retVal, 2834 BasicBlock *InsertAtEnd) { 2835 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); 2836 } 2837 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { 2838 return new(0) ReturnInst(C, InsertAtEnd); 2839 } 2840 ~ReturnInst() override; 2841 2842 /// Provide fast operand accessors 2843 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2844 2845 /// Convenience accessor. Returns null if there is no return value. 2846 Value *getReturnValue() const { 2847 return getNumOperands() != 0 ? getOperand(0) : nullptr; 2848 } 2849 2850 unsigned getNumSuccessors() const { return 0; } 2851 2852 // Methods for support type inquiry through isa, cast, and dyn_cast: 2853 static inline bool classof(const Instruction *I) { 2854 return (I->getOpcode() == Instruction::Ret); 2855 } 2856 static inline bool classof(const Value *V) { 2857 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2858 } 2859 2860 private: 2861 BasicBlock *getSuccessorV(unsigned idx) const override; 2862 unsigned getNumSuccessorsV() const override; 2863 void setSuccessorV(unsigned idx, BasicBlock *B) override; 2864 }; 2865 2866 template <> 2867 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { 2868 }; 2869 2870 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value) 2871 2872 //===----------------------------------------------------------------------===// 2873 // BranchInst Class 2874 //===----------------------------------------------------------------------===// 2875 2876 //===--------------------------------------------------------------------------- 2877 /// BranchInst - Conditional or Unconditional Branch instruction. 2878 /// 2879 class BranchInst : public TerminatorInst { 2880 /// Ops list - Branches are strange. The operands are ordered: 2881 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because 2882 /// they don't have to check for cond/uncond branchness. These are mostly 2883 /// accessed relative from op_end(). 2884 BranchInst(const BranchInst &BI); 2885 void AssertOK(); 2886 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): 2887 // BranchInst(BB *B) - 'br B' 2888 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' 2889 // BranchInst(BB* B, Inst *I) - 'br B' insert before I 2890 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I 2891 // BranchInst(BB* B, BB *I) - 'br B' insert at end 2892 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end 2893 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); 2894 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 2895 Instruction *InsertBefore = nullptr); 2896 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); 2897 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 2898 BasicBlock *InsertAtEnd); 2899 2900 protected: 2901 // Note: Instruction needs to be a friend here to call cloneImpl. 2902 friend class Instruction; 2903 BranchInst *cloneImpl() const; 2904 2905 public: 2906 static BranchInst *Create(BasicBlock *IfTrue, 2907 Instruction *InsertBefore = nullptr) { 2908 return new(1) BranchInst(IfTrue, InsertBefore); 2909 } 2910 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 2911 Value *Cond, Instruction *InsertBefore = nullptr) { 2912 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); 2913 } 2914 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { 2915 return new(1) BranchInst(IfTrue, InsertAtEnd); 2916 } 2917 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, 2918 Value *Cond, BasicBlock *InsertAtEnd) { 2919 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); 2920 } 2921 2922 /// Transparently provide more efficient getOperand methods. 2923 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 2924 2925 bool isUnconditional() const { return getNumOperands() == 1; } 2926 bool isConditional() const { return getNumOperands() == 3; } 2927 2928 Value *getCondition() const { 2929 assert(isConditional() && "Cannot get condition of an uncond branch!"); 2930 return Op<-3>(); 2931 } 2932 2933 void setCondition(Value *V) { 2934 assert(isConditional() && "Cannot set condition of unconditional branch!"); 2935 Op<-3>() = V; 2936 } 2937 2938 unsigned getNumSuccessors() const { return 1+isConditional(); } 2939 2940 BasicBlock *getSuccessor(unsigned i) const { 2941 assert(i < getNumSuccessors() && "Successor # out of range for Branch!"); 2942 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); 2943 } 2944 2945 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 2946 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!"); 2947 *(&Op<-1>() - idx) = NewSucc; 2948 } 2949 2950 /// \brief Swap the successors of this branch instruction. 2951 /// 2952 /// Swaps the successors of the branch instruction. This also swaps any 2953 /// branch weight metadata associated with the instruction so that it 2954 /// continues to map correctly to each operand. 2955 void swapSuccessors(); 2956 2957 // Methods for support type inquiry through isa, cast, and dyn_cast: 2958 static inline bool classof(const Instruction *I) { 2959 return (I->getOpcode() == Instruction::Br); 2960 } 2961 static inline bool classof(const Value *V) { 2962 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 2963 } 2964 2965 private: 2966 BasicBlock *getSuccessorV(unsigned idx) const override; 2967 unsigned getNumSuccessorsV() const override; 2968 void setSuccessorV(unsigned idx, BasicBlock *B) override; 2969 }; 2970 2971 template <> 2972 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { 2973 }; 2974 2975 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value) 2976 2977 //===----------------------------------------------------------------------===// 2978 // SwitchInst Class 2979 //===----------------------------------------------------------------------===// 2980 2981 //===--------------------------------------------------------------------------- 2982 /// Multiway switch 2983 /// 2984 class SwitchInst : public TerminatorInst { 2985 void *operator new(size_t, unsigned) = delete; 2986 unsigned ReservedSpace; 2987 // Operand[0] = Value to switch on 2988 // Operand[1] = Default basic block destination 2989 // Operand[2n ] = Value to match 2990 // Operand[2n+1] = BasicBlock to go to on match 2991 SwitchInst(const SwitchInst &SI); 2992 void init(Value *Value, BasicBlock *Default, unsigned NumReserved); 2993 void growOperands(); 2994 // allocate space for exactly zero operands 2995 void *operator new(size_t s) { 2996 return User::operator new(s); 2997 } 2998 /// Create a new switch instruction, specifying a value to switch on and a 2999 /// default destination. The number of additional cases can be specified here 3000 /// to make memory allocation more efficient. This constructor can also 3001 /// auto-insert before another instruction. 3002 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 3003 Instruction *InsertBefore); 3004 3005 /// Create a new switch instruction, specifying a value to switch on and a 3006 /// default destination. The number of additional cases can be specified here 3007 /// to make memory allocation more efficient. This constructor also 3008 /// auto-inserts at the end of the specified BasicBlock. 3009 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 3010 BasicBlock *InsertAtEnd); 3011 3012 protected: 3013 // Note: Instruction needs to be a friend here to call cloneImpl. 3014 friend class Instruction; 3015 SwitchInst *cloneImpl() const; 3016 3017 public: 3018 // -2 3019 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); 3020 3021 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy> 3022 class CaseIteratorT { 3023 protected: 3024 SwitchInstTy *SI; 3025 unsigned Index; 3026 3027 public: 3028 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self; 3029 3030 /// Initializes case iterator for given SwitchInst and for given 3031 /// case number. 3032 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) { 3033 this->SI = SI; 3034 Index = CaseNum; 3035 } 3036 3037 /// Initializes case iterator for given SwitchInst and for given 3038 /// TerminatorInst's successor index. 3039 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) { 3040 assert(SuccessorIndex < SI->getNumSuccessors() && 3041 "Successor index # out of range!"); 3042 return SuccessorIndex != 0 ? 3043 Self(SI, SuccessorIndex - 1) : 3044 Self(SI, DefaultPseudoIndex); 3045 } 3046 3047 /// Resolves case value for current case. 3048 ConstantIntTy *getCaseValue() { 3049 assert(Index < SI->getNumCases() && "Index out the number of cases."); 3050 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2)); 3051 } 3052 3053 /// Resolves successor for current case. 3054 BasicBlockTy *getCaseSuccessor() { 3055 assert((Index < SI->getNumCases() || 3056 Index == DefaultPseudoIndex) && 3057 "Index out the number of cases."); 3058 return SI->getSuccessor(getSuccessorIndex()); 3059 } 3060 3061 /// Returns number of current case. 3062 unsigned getCaseIndex() const { return Index; } 3063 3064 /// Returns TerminatorInst's successor index for current case successor. 3065 unsigned getSuccessorIndex() const { 3066 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) && 3067 "Index out the number of cases."); 3068 return Index != DefaultPseudoIndex ? Index + 1 : 0; 3069 } 3070 3071 Self operator++() { 3072 // Check index correctness after increment. 3073 // Note: Index == getNumCases() means end(). 3074 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases."); 3075 ++Index; 3076 return *this; 3077 } 3078 Self operator++(int) { 3079 Self tmp = *this; 3080 ++(*this); 3081 return tmp; 3082 } 3083 Self operator--() { 3084 // Check index correctness after decrement. 3085 // Note: Index == getNumCases() means end(). 3086 // Also allow "-1" iterator here. That will became valid after ++. 3087 assert((Index == 0 || Index-1 <= SI->getNumCases()) && 3088 "Index out the number of cases."); 3089 --Index; 3090 return *this; 3091 } 3092 Self operator--(int) { 3093 Self tmp = *this; 3094 --(*this); 3095 return tmp; 3096 } 3097 bool operator==(const Self& RHS) const { 3098 assert(RHS.SI == SI && "Incompatible operators."); 3099 return RHS.Index == Index; 3100 } 3101 bool operator!=(const Self& RHS) const { 3102 assert(RHS.SI == SI && "Incompatible operators."); 3103 return RHS.Index != Index; 3104 } 3105 Self &operator*() { 3106 return *this; 3107 } 3108 }; 3109 3110 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock> 3111 ConstCaseIt; 3112 3113 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> { 3114 3115 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy; 3116 3117 public: 3118 CaseIt(const ParentTy &Src) : ParentTy(Src) {} 3119 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {} 3120 3121 /// Sets the new value for current case. 3122 void setValue(ConstantInt *V) { 3123 assert(Index < SI->getNumCases() && "Index out the number of cases."); 3124 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); 3125 } 3126 3127 /// Sets the new successor for current case. 3128 void setSuccessor(BasicBlock *S) { 3129 SI->setSuccessor(getSuccessorIndex(), S); 3130 } 3131 }; 3132 3133 static SwitchInst *Create(Value *Value, BasicBlock *Default, 3134 unsigned NumCases, 3135 Instruction *InsertBefore = nullptr) { 3136 return new SwitchInst(Value, Default, NumCases, InsertBefore); 3137 } 3138 static SwitchInst *Create(Value *Value, BasicBlock *Default, 3139 unsigned NumCases, BasicBlock *InsertAtEnd) { 3140 return new SwitchInst(Value, Default, NumCases, InsertAtEnd); 3141 } 3142 3143 /// Provide fast operand accessors 3144 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3145 3146 // Accessor Methods for Switch stmt 3147 Value *getCondition() const { return getOperand(0); } 3148 void setCondition(Value *V) { setOperand(0, V); } 3149 3150 BasicBlock *getDefaultDest() const { 3151 return cast<BasicBlock>(getOperand(1)); 3152 } 3153 3154 void setDefaultDest(BasicBlock *DefaultCase) { 3155 setOperand(1, reinterpret_cast<Value*>(DefaultCase)); 3156 } 3157 3158 /// Return the number of 'cases' in this switch instruction, excluding the 3159 /// default case. 3160 unsigned getNumCases() const { 3161 return getNumOperands()/2 - 1; 3162 } 3163 3164 /// Returns a read/write iterator that points to the first case in the 3165 /// SwitchInst. 3166 CaseIt case_begin() { 3167 return CaseIt(this, 0); 3168 } 3169 /// Returns a read-only iterator that points to the first case in the 3170 /// SwitchInst. 3171 ConstCaseIt case_begin() const { 3172 return ConstCaseIt(this, 0); 3173 } 3174 3175 /// Returns a read/write iterator that points one past the last in the 3176 /// SwitchInst. 3177 CaseIt case_end() { 3178 return CaseIt(this, getNumCases()); 3179 } 3180 /// Returns a read-only iterator that points one past the last in the 3181 /// SwitchInst. 3182 ConstCaseIt case_end() const { 3183 return ConstCaseIt(this, getNumCases()); 3184 } 3185 3186 /// Iteration adapter for range-for loops. 3187 iterator_range<CaseIt> cases() { 3188 return make_range(case_begin(), case_end()); 3189 } 3190 3191 /// Constant iteration adapter for range-for loops. 3192 iterator_range<ConstCaseIt> cases() const { 3193 return make_range(case_begin(), case_end()); 3194 } 3195 3196 /// Returns an iterator that points to the default case. 3197 /// Note: this iterator allows to resolve successor only. Attempt 3198 /// to resolve case value causes an assertion. 3199 /// Also note, that increment and decrement also causes an assertion and 3200 /// makes iterator invalid. 3201 CaseIt case_default() { 3202 return CaseIt(this, DefaultPseudoIndex); 3203 } 3204 ConstCaseIt case_default() const { 3205 return ConstCaseIt(this, DefaultPseudoIndex); 3206 } 3207 3208 /// Search all of the case values for the specified constant. If it is 3209 /// explicitly handled, return the case iterator of it, otherwise return 3210 /// default case iterator to indicate that it is handled by the default 3211 /// handler. 3212 CaseIt findCaseValue(const ConstantInt *C) { 3213 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) 3214 if (i.getCaseValue() == C) 3215 return i; 3216 return case_default(); 3217 } 3218 ConstCaseIt findCaseValue(const ConstantInt *C) const { 3219 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i) 3220 if (i.getCaseValue() == C) 3221 return i; 3222 return case_default(); 3223 } 3224 3225 /// Finds the unique case value for a given successor. Returns null if the 3226 /// successor is not found, not unique, or is the default case. 3227 ConstantInt *findCaseDest(BasicBlock *BB) { 3228 if (BB == getDefaultDest()) return nullptr; 3229 3230 ConstantInt *CI = nullptr; 3231 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) { 3232 if (i.getCaseSuccessor() == BB) { 3233 if (CI) return nullptr; // Multiple cases lead to BB. 3234 else CI = i.getCaseValue(); 3235 } 3236 } 3237 return CI; 3238 } 3239 3240 /// Add an entry to the switch instruction. 3241 /// Note: 3242 /// This action invalidates case_end(). Old case_end() iterator will 3243 /// point to the added case. 3244 void addCase(ConstantInt *OnVal, BasicBlock *Dest); 3245 3246 /// This method removes the specified case and its successor from the switch 3247 /// instruction. Note that this operation may reorder the remaining cases at 3248 /// index idx and above. 3249 /// Note: 3250 /// This action invalidates iterators for all cases following the one removed, 3251 /// including the case_end() iterator. 3252 void removeCase(CaseIt i); 3253 3254 unsigned getNumSuccessors() const { return getNumOperands()/2; } 3255 BasicBlock *getSuccessor(unsigned idx) const { 3256 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!"); 3257 return cast<BasicBlock>(getOperand(idx*2+1)); 3258 } 3259 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 3260 assert(idx < getNumSuccessors() && "Successor # out of range for switch!"); 3261 setOperand(idx * 2 + 1, NewSucc); 3262 } 3263 3264 // Methods for support type inquiry through isa, cast, and dyn_cast: 3265 static inline bool classof(const Instruction *I) { 3266 return I->getOpcode() == Instruction::Switch; 3267 } 3268 static inline bool classof(const Value *V) { 3269 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3270 } 3271 3272 private: 3273 BasicBlock *getSuccessorV(unsigned idx) const override; 3274 unsigned getNumSuccessorsV() const override; 3275 void setSuccessorV(unsigned idx, BasicBlock *B) override; 3276 }; 3277 3278 template <> 3279 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { 3280 }; 3281 3282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value) 3283 3284 //===----------------------------------------------------------------------===// 3285 // IndirectBrInst Class 3286 //===----------------------------------------------------------------------===// 3287 3288 //===--------------------------------------------------------------------------- 3289 /// IndirectBrInst - Indirect Branch Instruction. 3290 /// 3291 class IndirectBrInst : public TerminatorInst { 3292 void *operator new(size_t, unsigned) = delete; 3293 unsigned ReservedSpace; 3294 // Operand[0] = Value to switch on 3295 // Operand[1] = Default basic block destination 3296 // Operand[2n ] = Value to match 3297 // Operand[2n+1] = BasicBlock to go to on match 3298 IndirectBrInst(const IndirectBrInst &IBI); 3299 void init(Value *Address, unsigned NumDests); 3300 void growOperands(); 3301 // allocate space for exactly zero operands 3302 void *operator new(size_t s) { 3303 return User::operator new(s); 3304 } 3305 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 3306 /// Address to jump to. The number of expected destinations can be specified 3307 /// here to make memory allocation more efficient. This constructor can also 3308 /// autoinsert before another instruction. 3309 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); 3310 3311 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an 3312 /// Address to jump to. The number of expected destinations can be specified 3313 /// here to make memory allocation more efficient. This constructor also 3314 /// autoinserts at the end of the specified BasicBlock. 3315 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); 3316 3317 protected: 3318 // Note: Instruction needs to be a friend here to call cloneImpl. 3319 friend class Instruction; 3320 IndirectBrInst *cloneImpl() const; 3321 3322 public: 3323 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 3324 Instruction *InsertBefore = nullptr) { 3325 return new IndirectBrInst(Address, NumDests, InsertBefore); 3326 } 3327 static IndirectBrInst *Create(Value *Address, unsigned NumDests, 3328 BasicBlock *InsertAtEnd) { 3329 return new IndirectBrInst(Address, NumDests, InsertAtEnd); 3330 } 3331 3332 /// Provide fast operand accessors. 3333 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3334 3335 // Accessor Methods for IndirectBrInst instruction. 3336 Value *getAddress() { return getOperand(0); } 3337 const Value *getAddress() const { return getOperand(0); } 3338 void setAddress(Value *V) { setOperand(0, V); } 3339 3340 /// getNumDestinations - return the number of possible destinations in this 3341 /// indirectbr instruction. 3342 unsigned getNumDestinations() const { return getNumOperands()-1; } 3343 3344 /// getDestination - Return the specified destination. 3345 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } 3346 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } 3347 3348 /// addDestination - Add a destination. 3349 /// 3350 void addDestination(BasicBlock *Dest); 3351 3352 /// removeDestination - This method removes the specified successor from the 3353 /// indirectbr instruction. 3354 void removeDestination(unsigned i); 3355 3356 unsigned getNumSuccessors() const { return getNumOperands()-1; } 3357 BasicBlock *getSuccessor(unsigned i) const { 3358 return cast<BasicBlock>(getOperand(i+1)); 3359 } 3360 void setSuccessor(unsigned i, BasicBlock *NewSucc) { 3361 setOperand(i + 1, NewSucc); 3362 } 3363 3364 // Methods for support type inquiry through isa, cast, and dyn_cast: 3365 static inline bool classof(const Instruction *I) { 3366 return I->getOpcode() == Instruction::IndirectBr; 3367 } 3368 static inline bool classof(const Value *V) { 3369 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3370 } 3371 3372 private: 3373 BasicBlock *getSuccessorV(unsigned idx) const override; 3374 unsigned getNumSuccessorsV() const override; 3375 void setSuccessorV(unsigned idx, BasicBlock *B) override; 3376 }; 3377 3378 template <> 3379 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { 3380 }; 3381 3382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value) 3383 3384 //===----------------------------------------------------------------------===// 3385 // InvokeInst Class 3386 //===----------------------------------------------------------------------===// 3387 3388 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the 3389 /// calling convention of the call. 3390 /// 3391 class InvokeInst : public TerminatorInst, 3392 public OperandBundleUser<InvokeInst, User::op_iterator> { 3393 AttributeSet AttributeList; 3394 FunctionType *FTy; 3395 InvokeInst(const InvokeInst &BI); 3396 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 3397 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles, 3398 const Twine &NameStr) { 3399 init(cast<FunctionType>( 3400 cast<PointerType>(Func->getType())->getElementType()), 3401 Func, IfNormal, IfException, Args, Bundles, NameStr); 3402 } 3403 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, 3404 BasicBlock *IfException, ArrayRef<Value *> Args, 3405 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); 3406 3407 /// Construct an InvokeInst given a range of arguments. 3408 /// 3409 /// \brief Construct an InvokeInst from a range of arguments 3410 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 3411 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles, 3412 unsigned Values, const Twine &NameStr, 3413 Instruction *InsertBefore) 3414 : InvokeInst(cast<FunctionType>( 3415 cast<PointerType>(Func->getType())->getElementType()), 3416 Func, IfNormal, IfException, Args, Bundles, Values, NameStr, 3417 InsertBefore) {} 3418 3419 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, 3420 BasicBlock *IfException, ArrayRef<Value *> Args, 3421 ArrayRef<OperandBundleDef> Bundles, unsigned Values, 3422 const Twine &NameStr, Instruction *InsertBefore); 3423 /// Construct an InvokeInst given a range of arguments. 3424 /// 3425 /// \brief Construct an InvokeInst from a range of arguments 3426 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, 3427 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles, 3428 unsigned Values, const Twine &NameStr, 3429 BasicBlock *InsertAtEnd); 3430 3431 friend class OperandBundleUser<InvokeInst, User::op_iterator>; 3432 bool hasDescriptor() const { return HasDescriptor; } 3433 3434 protected: 3435 // Note: Instruction needs to be a friend here to call cloneImpl. 3436 friend class Instruction; 3437 InvokeInst *cloneImpl() const; 3438 3439 public: 3440 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, 3441 BasicBlock *IfException, ArrayRef<Value *> Args, 3442 const Twine &NameStr, 3443 Instruction *InsertBefore = nullptr) { 3444 return Create(cast<FunctionType>( 3445 cast<PointerType>(Func->getType())->getElementType()), 3446 Func, IfNormal, IfException, Args, None, NameStr, 3447 InsertBefore); 3448 } 3449 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, 3450 BasicBlock *IfException, ArrayRef<Value *> Args, 3451 ArrayRef<OperandBundleDef> Bundles = None, 3452 const Twine &NameStr = "", 3453 Instruction *InsertBefore = nullptr) { 3454 return Create(cast<FunctionType>( 3455 cast<PointerType>(Func->getType())->getElementType()), 3456 Func, IfNormal, IfException, Args, Bundles, NameStr, 3457 InsertBefore); 3458 } 3459 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, 3460 BasicBlock *IfException, ArrayRef<Value *> Args, 3461 const Twine &NameStr, 3462 Instruction *InsertBefore = nullptr) { 3463 unsigned Values = unsigned(Args.size()) + 3; 3464 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None, 3465 Values, NameStr, InsertBefore); 3466 } 3467 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, 3468 BasicBlock *IfException, ArrayRef<Value *> Args, 3469 ArrayRef<OperandBundleDef> Bundles = None, 3470 const Twine &NameStr = "", 3471 Instruction *InsertBefore = nullptr) { 3472 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3; 3473 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); 3474 3475 return new (Values, DescriptorBytes) 3476 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values, 3477 NameStr, InsertBefore); 3478 } 3479 static InvokeInst *Create(Value *Func, 3480 BasicBlock *IfNormal, BasicBlock *IfException, 3481 ArrayRef<Value *> Args, const Twine &NameStr, 3482 BasicBlock *InsertAtEnd) { 3483 unsigned Values = unsigned(Args.size()) + 3; 3484 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None, 3485 Values, NameStr, InsertAtEnd); 3486 } 3487 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, 3488 BasicBlock *IfException, ArrayRef<Value *> Args, 3489 ArrayRef<OperandBundleDef> Bundles, 3490 const Twine &NameStr, BasicBlock *InsertAtEnd) { 3491 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3; 3492 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); 3493 3494 return new (Values, DescriptorBytes) 3495 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr, 3496 InsertAtEnd); 3497 } 3498 3499 /// \brief Create a clone of \p II with a different set of operand bundles and 3500 /// insert it before \p InsertPt. 3501 /// 3502 /// The returned invoke instruction is identical to \p II in every way except 3503 /// that the operand bundles for the new instruction are set to the operand 3504 /// bundles in \p Bundles. 3505 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, 3506 Instruction *InsertPt = nullptr); 3507 3508 /// Provide fast operand accessors 3509 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3510 3511 FunctionType *getFunctionType() const { return FTy; } 3512 3513 void mutateFunctionType(FunctionType *FTy) { 3514 mutateType(FTy->getReturnType()); 3515 this->FTy = FTy; 3516 } 3517 3518 /// getNumArgOperands - Return the number of invoke arguments. 3519 /// 3520 unsigned getNumArgOperands() const { 3521 return getNumOperands() - getNumTotalBundleOperands() - 3; 3522 } 3523 3524 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument. 3525 /// 3526 Value *getArgOperand(unsigned i) const { 3527 assert(i < getNumArgOperands() && "Out of bounds!"); 3528 return getOperand(i); 3529 } 3530 void setArgOperand(unsigned i, Value *v) { 3531 assert(i < getNumArgOperands() && "Out of bounds!"); 3532 setOperand(i, v); 3533 } 3534 3535 /// \brief Return the iterator pointing to the beginning of the argument list. 3536 op_iterator arg_begin() { return op_begin(); } 3537 3538 /// \brief Return the iterator pointing to the end of the argument list. 3539 op_iterator arg_end() { 3540 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee 3541 return op_end() - getNumTotalBundleOperands() - 3; 3542 }; 3543 3544 /// \brief Iteration adapter for range-for loops. 3545 iterator_range<op_iterator> arg_operands() { 3546 return make_range(arg_begin(), arg_end()); 3547 } 3548 3549 /// \brief Return the iterator pointing to the beginning of the argument list. 3550 const_op_iterator arg_begin() const { return op_begin(); } 3551 3552 /// \brief Return the iterator pointing to the end of the argument list. 3553 const_op_iterator arg_end() const { 3554 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee 3555 return op_end() - getNumTotalBundleOperands() - 3; 3556 }; 3557 3558 /// \brief Iteration adapter for range-for loops. 3559 iterator_range<const_op_iterator> arg_operands() const { 3560 return make_range(arg_begin(), arg_end()); 3561 } 3562 3563 /// \brief Wrappers for getting the \c Use of a invoke argument. 3564 const Use &getArgOperandUse(unsigned i) const { 3565 assert(i < getNumArgOperands() && "Out of bounds!"); 3566 return getOperandUse(i); 3567 } 3568 Use &getArgOperandUse(unsigned i) { 3569 assert(i < getNumArgOperands() && "Out of bounds!"); 3570 return getOperandUse(i); 3571 } 3572 3573 /// If one of the arguments has the 'returned' attribute, return its 3574 /// operand value. Otherwise, return nullptr. 3575 Value *getReturnedArgOperand() const; 3576 3577 /// getCallingConv/setCallingConv - Get or set the calling convention of this 3578 /// function call. 3579 CallingConv::ID getCallingConv() const { 3580 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction()); 3581 } 3582 void setCallingConv(CallingConv::ID CC) { 3583 auto ID = static_cast<unsigned>(CC); 3584 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention"); 3585 setInstructionSubclassData(ID); 3586 } 3587 3588 /// getAttributes - Return the parameter attributes for this invoke. 3589 /// 3590 const AttributeSet &getAttributes() const { return AttributeList; } 3591 3592 /// setAttributes - Set the parameter attributes for this invoke. 3593 /// 3594 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; } 3595 3596 /// addAttribute - adds the attribute to the list of attributes. 3597 void addAttribute(unsigned i, Attribute::AttrKind Kind); 3598 3599 /// addAttribute - adds the attribute to the list of attributes. 3600 void addAttribute(unsigned i, Attribute Attr); 3601 3602 /// removeAttribute - removes the attribute from the list of attributes. 3603 void removeAttribute(unsigned i, Attribute::AttrKind Kind); 3604 3605 /// removeAttribute - removes the attribute from the list of attributes. 3606 void removeAttribute(unsigned i, StringRef Kind); 3607 3608 /// removeAttribute - removes the attribute from the list of attributes. 3609 void removeAttribute(unsigned i, Attribute Attr); 3610 3611 /// \brief adds the dereferenceable attribute to the list of attributes. 3612 void addDereferenceableAttr(unsigned i, uint64_t Bytes); 3613 3614 /// \brief adds the dereferenceable_or_null attribute to the list of 3615 /// attributes. 3616 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes); 3617 3618 /// \brief Determine whether this call has the given attribute. 3619 bool hasFnAttr(Attribute::AttrKind Kind) const { 3620 assert(Kind != Attribute::NoBuiltin && 3621 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin"); 3622 return hasFnAttrImpl(Kind); 3623 } 3624 3625 /// \brief Determine whether this call has the given attribute. 3626 bool hasFnAttr(StringRef Kind) const { 3627 return hasFnAttrImpl(Kind); 3628 } 3629 3630 /// \brief Determine whether the call or the callee has the given attributes. 3631 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const; 3632 3633 /// \brief Get the attribute of a given kind at a position. 3634 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const; 3635 3636 /// \brief Get the attribute of a given kind at a position. 3637 Attribute getAttribute(unsigned i, StringRef Kind) const; 3638 3639 /// \brief Return true if the data operand at index \p i has the attribute \p 3640 /// A. 3641 /// 3642 /// Data operands include invoke arguments and values used in operand bundles, 3643 /// but does not include the invokee operand, or the two successor blocks. 3644 /// This routine dispatches to the underlying AttributeList or the 3645 /// OperandBundleUser as appropriate. 3646 /// 3647 /// The index \p i is interpreted as 3648 /// 3649 /// \p i == Attribute::ReturnIndex -> the return value 3650 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) 3651 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index 3652 /// (\p i - 1) in the operand list. 3653 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const; 3654 3655 /// \brief Extract the alignment for a call or parameter (0=unknown). 3656 unsigned getParamAlignment(unsigned i) const { 3657 return AttributeList.getParamAlignment(i); 3658 } 3659 3660 /// \brief Extract the number of dereferenceable bytes for a call or 3661 /// parameter (0=unknown). 3662 uint64_t getDereferenceableBytes(unsigned i) const { 3663 return AttributeList.getDereferenceableBytes(i); 3664 } 3665 3666 /// \brief Extract the number of dereferenceable_or_null bytes for a call or 3667 /// parameter (0=unknown). 3668 uint64_t getDereferenceableOrNullBytes(unsigned i) const { 3669 return AttributeList.getDereferenceableOrNullBytes(i); 3670 } 3671 3672 /// @brief Determine if the parameter or return value is marked with NoAlias 3673 /// attribute. 3674 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 3675 bool doesNotAlias(unsigned n) const { 3676 return AttributeList.hasAttribute(n, Attribute::NoAlias); 3677 } 3678 3679 /// \brief Return true if the call should not be treated as a call to a 3680 /// builtin. 3681 bool isNoBuiltin() const { 3682 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have 3683 // to check it by hand. 3684 return hasFnAttrImpl(Attribute::NoBuiltin) && 3685 !hasFnAttrImpl(Attribute::Builtin); 3686 } 3687 3688 /// \brief Return true if the call should not be inlined. 3689 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } 3690 void setIsNoInline() { 3691 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline); 3692 } 3693 3694 /// \brief Determine if the call does not access memory. 3695 bool doesNotAccessMemory() const { 3696 return hasFnAttr(Attribute::ReadNone); 3697 } 3698 void setDoesNotAccessMemory() { 3699 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone); 3700 } 3701 3702 /// \brief Determine if the call does not access or only reads memory. 3703 bool onlyReadsMemory() const { 3704 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 3705 } 3706 void setOnlyReadsMemory() { 3707 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly); 3708 } 3709 3710 /// \brief Determine if the call does not access or only writes memory. 3711 bool doesNotReadMemory() const { 3712 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); 3713 } 3714 void setDoesNotReadMemory() { 3715 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly); 3716 } 3717 3718 /// @brief Determine if the call access memmory only using it's pointer 3719 /// arguments. 3720 bool onlyAccessesArgMemory() const { 3721 return hasFnAttr(Attribute::ArgMemOnly); 3722 } 3723 void setOnlyAccessesArgMemory() { 3724 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly); 3725 } 3726 3727 /// \brief Determine if the call cannot return. 3728 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } 3729 void setDoesNotReturn() { 3730 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn); 3731 } 3732 3733 /// \brief Determine if the call cannot unwind. 3734 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } 3735 void setDoesNotThrow() { 3736 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind); 3737 } 3738 3739 /// \brief Determine if the invoke cannot be duplicated. 3740 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); } 3741 void setCannotDuplicate() { 3742 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate); 3743 } 3744 3745 /// \brief Determine if the invoke is convergent 3746 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } 3747 void setConvergent() { 3748 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent); 3749 } 3750 void setNotConvergent() { 3751 removeAttribute(AttributeSet::FunctionIndex, 3752 Attribute::get(getContext(), Attribute::Convergent)); 3753 } 3754 3755 /// \brief Determine if the call returns a structure through first 3756 /// pointer argument. 3757 bool hasStructRetAttr() const { 3758 if (getNumArgOperands() == 0) 3759 return false; 3760 3761 // Be friendly and also check the callee. 3762 return paramHasAttr(1, Attribute::StructRet); 3763 } 3764 3765 /// \brief Determine if any call argument is an aggregate passed by value. 3766 bool hasByValArgument() const { 3767 return AttributeList.hasAttrSomewhere(Attribute::ByVal); 3768 } 3769 3770 /// getCalledFunction - Return the function called, or null if this is an 3771 /// indirect function invocation. 3772 /// 3773 Function *getCalledFunction() const { 3774 return dyn_cast<Function>(Op<-3>()); 3775 } 3776 3777 /// getCalledValue - Get a pointer to the function that is invoked by this 3778 /// instruction 3779 const Value *getCalledValue() const { return Op<-3>(); } 3780 Value *getCalledValue() { return Op<-3>(); } 3781 3782 /// setCalledFunction - Set the function called. 3783 void setCalledFunction(Value* Fn) { 3784 setCalledFunction( 3785 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()), 3786 Fn); 3787 } 3788 void setCalledFunction(FunctionType *FTy, Value *Fn) { 3789 this->FTy = FTy; 3790 assert(FTy == cast<FunctionType>( 3791 cast<PointerType>(Fn->getType())->getElementType())); 3792 Op<-3>() = Fn; 3793 } 3794 3795 // get*Dest - Return the destination basic blocks... 3796 BasicBlock *getNormalDest() const { 3797 return cast<BasicBlock>(Op<-2>()); 3798 } 3799 BasicBlock *getUnwindDest() const { 3800 return cast<BasicBlock>(Op<-1>()); 3801 } 3802 void setNormalDest(BasicBlock *B) { 3803 Op<-2>() = reinterpret_cast<Value*>(B); 3804 } 3805 void setUnwindDest(BasicBlock *B) { 3806 Op<-1>() = reinterpret_cast<Value*>(B); 3807 } 3808 3809 /// getLandingPadInst - Get the landingpad instruction from the landing pad 3810 /// block (the unwind destination). 3811 LandingPadInst *getLandingPadInst() const; 3812 3813 BasicBlock *getSuccessor(unsigned i) const { 3814 assert(i < 2 && "Successor # out of range for invoke!"); 3815 return i == 0 ? getNormalDest() : getUnwindDest(); 3816 } 3817 3818 void setSuccessor(unsigned idx, BasicBlock *NewSucc) { 3819 assert(idx < 2 && "Successor # out of range for invoke!"); 3820 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc); 3821 } 3822 3823 unsigned getNumSuccessors() const { return 2; } 3824 3825 // Methods for support type inquiry through isa, cast, and dyn_cast: 3826 static inline bool classof(const Instruction *I) { 3827 return (I->getOpcode() == Instruction::Invoke); 3828 } 3829 static inline bool classof(const Value *V) { 3830 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3831 } 3832 3833 private: 3834 BasicBlock *getSuccessorV(unsigned idx) const override; 3835 unsigned getNumSuccessorsV() const override; 3836 void setSuccessorV(unsigned idx, BasicBlock *B) override; 3837 3838 template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const { 3839 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) 3840 return true; 3841 3842 // Operand bundles override attributes on the called function, but don't 3843 // override attributes directly present on the invoke instruction. 3844 if (isFnAttrDisallowedByOpBundle(A)) 3845 return false; 3846 3847 if (const Function *F = getCalledFunction()) 3848 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); 3849 return false; 3850 } 3851 3852 // Shadow Instruction::setInstructionSubclassData with a private forwarding 3853 // method so that subclasses cannot accidentally use it. 3854 void setInstructionSubclassData(unsigned short D) { 3855 Instruction::setInstructionSubclassData(D); 3856 } 3857 }; 3858 3859 template <> 3860 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> { 3861 }; 3862 3863 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, 3864 BasicBlock *IfException, ArrayRef<Value *> Args, 3865 ArrayRef<OperandBundleDef> Bundles, unsigned Values, 3866 const Twine &NameStr, Instruction *InsertBefore) 3867 : TerminatorInst(Ty->getReturnType(), Instruction::Invoke, 3868 OperandTraits<InvokeInst>::op_end(this) - Values, Values, 3869 InsertBefore) { 3870 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); 3871 } 3872 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal, 3873 BasicBlock *IfException, ArrayRef<Value *> Args, 3874 ArrayRef<OperandBundleDef> Bundles, unsigned Values, 3875 const Twine &NameStr, BasicBlock *InsertAtEnd) 3876 : TerminatorInst( 3877 cast<FunctionType>(cast<PointerType>(Func->getType()) 3878 ->getElementType())->getReturnType(), 3879 Instruction::Invoke, OperandTraits<InvokeInst>::op_end(this) - Values, 3880 Values, InsertAtEnd) { 3881 init(Func, IfNormal, IfException, Args, Bundles, NameStr); 3882 } 3883 3884 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value) 3885 3886 //===----------------------------------------------------------------------===// 3887 // ResumeInst Class 3888 //===----------------------------------------------------------------------===// 3889 3890 //===--------------------------------------------------------------------------- 3891 /// ResumeInst - Resume the propagation of an exception. 3892 /// 3893 class ResumeInst : public TerminatorInst { 3894 ResumeInst(const ResumeInst &RI); 3895 3896 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); 3897 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); 3898 3899 protected: 3900 // Note: Instruction needs to be a friend here to call cloneImpl. 3901 friend class Instruction; 3902 ResumeInst *cloneImpl() const; 3903 3904 public: 3905 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { 3906 return new(1) ResumeInst(Exn, InsertBefore); 3907 } 3908 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { 3909 return new(1) ResumeInst(Exn, InsertAtEnd); 3910 } 3911 3912 /// Provide fast operand accessors 3913 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3914 3915 /// Convenience accessor. 3916 Value *getValue() const { return Op<0>(); } 3917 3918 unsigned getNumSuccessors() const { return 0; } 3919 3920 // Methods for support type inquiry through isa, cast, and dyn_cast: 3921 static inline bool classof(const Instruction *I) { 3922 return I->getOpcode() == Instruction::Resume; 3923 } 3924 static inline bool classof(const Value *V) { 3925 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 3926 } 3927 3928 private: 3929 BasicBlock *getSuccessorV(unsigned idx) const override; 3930 unsigned getNumSuccessorsV() const override; 3931 void setSuccessorV(unsigned idx, BasicBlock *B) override; 3932 }; 3933 3934 template <> 3935 struct OperandTraits<ResumeInst> : 3936 public FixedNumOperandTraits<ResumeInst, 1> { 3937 }; 3938 3939 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value) 3940 3941 //===----------------------------------------------------------------------===// 3942 // CatchSwitchInst Class 3943 //===----------------------------------------------------------------------===// 3944 class CatchSwitchInst : public TerminatorInst { 3945 void *operator new(size_t, unsigned) = delete; 3946 /// ReservedSpace - The number of operands actually allocated. NumOperands is 3947 /// the number actually in use. 3948 unsigned ReservedSpace; 3949 // Operand[0] = Outer scope 3950 // Operand[1] = Unwind block destination 3951 // Operand[n] = BasicBlock to go to on match 3952 CatchSwitchInst(const CatchSwitchInst &CSI); 3953 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); 3954 void growOperands(unsigned Size); 3955 // allocate space for exactly zero operands 3956 void *operator new(size_t s) { return User::operator new(s); } 3957 /// CatchSwitchInst ctor - Create a new switch instruction, specifying a 3958 /// default destination. The number of additional handlers can be specified 3959 /// here to make memory allocation more efficient. 3960 /// This constructor can also autoinsert before another instruction. 3961 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 3962 unsigned NumHandlers, const Twine &NameStr, 3963 Instruction *InsertBefore); 3964 3965 /// CatchSwitchInst ctor - Create a new switch instruction, specifying a 3966 /// default destination. The number of additional handlers can be specified 3967 /// here to make memory allocation more efficient. 3968 /// This constructor also autoinserts at the end of the specified BasicBlock. 3969 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 3970 unsigned NumHandlers, const Twine &NameStr, 3971 BasicBlock *InsertAtEnd); 3972 3973 protected: 3974 // Note: Instruction needs to be a friend here to call cloneImpl. 3975 friend class Instruction; 3976 CatchSwitchInst *cloneImpl() const; 3977 3978 public: 3979 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, 3980 unsigned NumHandlers, 3981 const Twine &NameStr = "", 3982 Instruction *InsertBefore = nullptr) { 3983 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, 3984 InsertBefore); 3985 } 3986 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, 3987 unsigned NumHandlers, const Twine &NameStr, 3988 BasicBlock *InsertAtEnd) { 3989 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, 3990 InsertAtEnd); 3991 } 3992 3993 /// Provide fast operand accessors 3994 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 3995 3996 // Accessor Methods for CatchSwitch stmt 3997 Value *getParentPad() const { return getOperand(0); } 3998 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } 3999 4000 // Accessor Methods for CatchSwitch stmt 4001 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } 4002 bool unwindsToCaller() const { return !hasUnwindDest(); } 4003 BasicBlock *getUnwindDest() const { 4004 if (hasUnwindDest()) 4005 return cast<BasicBlock>(getOperand(1)); 4006 return nullptr; 4007 } 4008 void setUnwindDest(BasicBlock *UnwindDest) { 4009 assert(UnwindDest); 4010 assert(hasUnwindDest()); 4011 setOperand(1, UnwindDest); 4012 } 4013 4014 /// getNumHandlers - return the number of 'handlers' in this catchswitch 4015 /// instruction, except the default handler 4016 unsigned getNumHandlers() const { 4017 if (hasUnwindDest()) 4018 return getNumOperands() - 2; 4019 return getNumOperands() - 1; 4020 } 4021 4022 private: 4023 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } 4024 static const BasicBlock *handler_helper(const Value *V) { 4025 return cast<BasicBlock>(V); 4026 } 4027 4028 public: 4029 typedef std::pointer_to_unary_function<Value *, BasicBlock *> DerefFnTy; 4030 typedef mapped_iterator<op_iterator, DerefFnTy> handler_iterator; 4031 typedef iterator_range<handler_iterator> handler_range; 4032 4033 4034 typedef std::pointer_to_unary_function<const Value *, const BasicBlock *> 4035 ConstDerefFnTy; 4036 typedef mapped_iterator<const_op_iterator, ConstDerefFnTy> const_handler_iterator; 4037 typedef iterator_range<const_handler_iterator> const_handler_range; 4038 4039 /// Returns an iterator that points to the first handler in CatchSwitchInst. 4040 handler_iterator handler_begin() { 4041 op_iterator It = op_begin() + 1; 4042 if (hasUnwindDest()) 4043 ++It; 4044 return handler_iterator(It, DerefFnTy(handler_helper)); 4045 } 4046 /// Returns an iterator that points to the first handler in the 4047 /// CatchSwitchInst. 4048 const_handler_iterator handler_begin() const { 4049 const_op_iterator It = op_begin() + 1; 4050 if (hasUnwindDest()) 4051 ++It; 4052 return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); 4053 } 4054 4055 /// Returns a read-only iterator that points one past the last 4056 /// handler in the CatchSwitchInst. 4057 handler_iterator handler_end() { 4058 return handler_iterator(op_end(), DerefFnTy(handler_helper)); 4059 } 4060 /// Returns an iterator that points one past the last handler in the 4061 /// CatchSwitchInst. 4062 const_handler_iterator handler_end() const { 4063 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); 4064 } 4065 4066 /// handlers - iteration adapter for range-for loops. 4067 handler_range handlers() { 4068 return make_range(handler_begin(), handler_end()); 4069 } 4070 4071 /// handlers - iteration adapter for range-for loops. 4072 const_handler_range handlers() const { 4073 return make_range(handler_begin(), handler_end()); 4074 } 4075 4076 /// addHandler - Add an entry to the switch instruction... 4077 /// Note: 4078 /// This action invalidates handler_end(). Old handler_end() iterator will 4079 /// point to the added handler. 4080 void addHandler(BasicBlock *Dest); 4081 4082 void removeHandler(handler_iterator HI); 4083 4084 unsigned getNumSuccessors() const { return getNumOperands() - 1; } 4085 BasicBlock *getSuccessor(unsigned Idx) const { 4086 assert(Idx < getNumSuccessors() && 4087 "Successor # out of range for catchswitch!"); 4088 return cast<BasicBlock>(getOperand(Idx + 1)); 4089 } 4090 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { 4091 assert(Idx < getNumSuccessors() && 4092 "Successor # out of range for catchswitch!"); 4093 setOperand(Idx + 1, NewSucc); 4094 } 4095 4096 // Methods for support type inquiry through isa, cast, and dyn_cast: 4097 static inline bool classof(const Instruction *I) { 4098 return I->getOpcode() == Instruction::CatchSwitch; 4099 } 4100 static inline bool classof(const Value *V) { 4101 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4102 } 4103 4104 private: 4105 BasicBlock *getSuccessorV(unsigned Idx) const override; 4106 unsigned getNumSuccessorsV() const override; 4107 void setSuccessorV(unsigned Idx, BasicBlock *B) override; 4108 }; 4109 4110 template <> 4111 struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; 4112 4113 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value) 4114 4115 //===----------------------------------------------------------------------===// 4116 // CleanupPadInst Class 4117 //===----------------------------------------------------------------------===// 4118 class CleanupPadInst : public FuncletPadInst { 4119 private: 4120 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, 4121 unsigned Values, const Twine &NameStr, 4122 Instruction *InsertBefore) 4123 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, 4124 NameStr, InsertBefore) {} 4125 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, 4126 unsigned Values, const Twine &NameStr, 4127 BasicBlock *InsertAtEnd) 4128 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, 4129 NameStr, InsertAtEnd) {} 4130 4131 public: 4132 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None, 4133 const Twine &NameStr = "", 4134 Instruction *InsertBefore = nullptr) { 4135 unsigned Values = 1 + Args.size(); 4136 return new (Values) 4137 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); 4138 } 4139 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, 4140 const Twine &NameStr, BasicBlock *InsertAtEnd) { 4141 unsigned Values = 1 + Args.size(); 4142 return new (Values) 4143 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); 4144 } 4145 4146 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4147 static inline bool classof(const Instruction *I) { 4148 return I->getOpcode() == Instruction::CleanupPad; 4149 } 4150 static inline bool classof(const Value *V) { 4151 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4152 } 4153 }; 4154 4155 //===----------------------------------------------------------------------===// 4156 // CatchPadInst Class 4157 //===----------------------------------------------------------------------===// 4158 class CatchPadInst : public FuncletPadInst { 4159 private: 4160 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, 4161 unsigned Values, const Twine &NameStr, 4162 Instruction *InsertBefore) 4163 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, 4164 NameStr, InsertBefore) {} 4165 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, 4166 unsigned Values, const Twine &NameStr, 4167 BasicBlock *InsertAtEnd) 4168 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, 4169 NameStr, InsertAtEnd) {} 4170 4171 public: 4172 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, 4173 const Twine &NameStr = "", 4174 Instruction *InsertBefore = nullptr) { 4175 unsigned Values = 1 + Args.size(); 4176 return new (Values) 4177 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); 4178 } 4179 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, 4180 const Twine &NameStr, BasicBlock *InsertAtEnd) { 4181 unsigned Values = 1 + Args.size(); 4182 return new (Values) 4183 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); 4184 } 4185 4186 /// Convenience accessors 4187 CatchSwitchInst *getCatchSwitch() const { 4188 return cast<CatchSwitchInst>(Op<-1>()); 4189 } 4190 void setCatchSwitch(Value *CatchSwitch) { 4191 assert(CatchSwitch); 4192 Op<-1>() = CatchSwitch; 4193 } 4194 4195 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4196 static inline bool classof(const Instruction *I) { 4197 return I->getOpcode() == Instruction::CatchPad; 4198 } 4199 static inline bool classof(const Value *V) { 4200 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4201 } 4202 }; 4203 4204 //===----------------------------------------------------------------------===// 4205 // CatchReturnInst Class 4206 //===----------------------------------------------------------------------===// 4207 4208 class CatchReturnInst : public TerminatorInst { 4209 CatchReturnInst(const CatchReturnInst &RI); 4210 4211 void init(Value *CatchPad, BasicBlock *BB); 4212 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); 4213 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); 4214 4215 protected: 4216 // Note: Instruction needs to be a friend here to call cloneImpl. 4217 friend class Instruction; 4218 CatchReturnInst *cloneImpl() const; 4219 4220 public: 4221 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, 4222 Instruction *InsertBefore = nullptr) { 4223 assert(CatchPad); 4224 assert(BB); 4225 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); 4226 } 4227 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, 4228 BasicBlock *InsertAtEnd) { 4229 assert(CatchPad); 4230 assert(BB); 4231 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); 4232 } 4233 4234 /// Provide fast operand accessors 4235 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 4236 4237 /// Convenience accessors. 4238 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } 4239 void setCatchPad(CatchPadInst *CatchPad) { 4240 assert(CatchPad); 4241 Op<0>() = CatchPad; 4242 } 4243 4244 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } 4245 void setSuccessor(BasicBlock *NewSucc) { 4246 assert(NewSucc); 4247 Op<1>() = NewSucc; 4248 } 4249 unsigned getNumSuccessors() const { return 1; } 4250 4251 /// Get the parentPad of this catchret's catchpad's catchswitch. 4252 /// The successor block is implicitly a member of this funclet. 4253 Value *getCatchSwitchParentPad() const { 4254 return getCatchPad()->getCatchSwitch()->getParentPad(); 4255 } 4256 4257 // Methods for support type inquiry through isa, cast, and dyn_cast: 4258 static inline bool classof(const Instruction *I) { 4259 return (I->getOpcode() == Instruction::CatchRet); 4260 } 4261 static inline bool classof(const Value *V) { 4262 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4263 } 4264 4265 private: 4266 BasicBlock *getSuccessorV(unsigned Idx) const override; 4267 unsigned getNumSuccessorsV() const override; 4268 void setSuccessorV(unsigned Idx, BasicBlock *B) override; 4269 }; 4270 4271 template <> 4272 struct OperandTraits<CatchReturnInst> 4273 : public FixedNumOperandTraits<CatchReturnInst, 2> {}; 4274 4275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value) 4276 4277 //===----------------------------------------------------------------------===// 4278 // CleanupReturnInst Class 4279 //===----------------------------------------------------------------------===// 4280 4281 class CleanupReturnInst : public TerminatorInst { 4282 private: 4283 CleanupReturnInst(const CleanupReturnInst &RI); 4284 4285 void init(Value *CleanupPad, BasicBlock *UnwindBB); 4286 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, 4287 Instruction *InsertBefore = nullptr); 4288 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, 4289 BasicBlock *InsertAtEnd); 4290 4291 protected: 4292 // Note: Instruction needs to be a friend here to call cloneImpl. 4293 friend class Instruction; 4294 CleanupReturnInst *cloneImpl() const; 4295 4296 public: 4297 static CleanupReturnInst *Create(Value *CleanupPad, 4298 BasicBlock *UnwindBB = nullptr, 4299 Instruction *InsertBefore = nullptr) { 4300 assert(CleanupPad); 4301 unsigned Values = 1; 4302 if (UnwindBB) 4303 ++Values; 4304 return new (Values) 4305 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); 4306 } 4307 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, 4308 BasicBlock *InsertAtEnd) { 4309 assert(CleanupPad); 4310 unsigned Values = 1; 4311 if (UnwindBB) 4312 ++Values; 4313 return new (Values) 4314 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); 4315 } 4316 4317 /// Provide fast operand accessors 4318 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 4319 4320 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } 4321 bool unwindsToCaller() const { return !hasUnwindDest(); } 4322 4323 /// Convenience accessor. 4324 CleanupPadInst *getCleanupPad() const { 4325 return cast<CleanupPadInst>(Op<0>()); 4326 } 4327 void setCleanupPad(CleanupPadInst *CleanupPad) { 4328 assert(CleanupPad); 4329 Op<0>() = CleanupPad; 4330 } 4331 4332 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } 4333 4334 BasicBlock *getUnwindDest() const { 4335 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; 4336 } 4337 void setUnwindDest(BasicBlock *NewDest) { 4338 assert(NewDest); 4339 assert(hasUnwindDest()); 4340 Op<1>() = NewDest; 4341 } 4342 4343 // Methods for support type inquiry through isa, cast, and dyn_cast: 4344 static inline bool classof(const Instruction *I) { 4345 return (I->getOpcode() == Instruction::CleanupRet); 4346 } 4347 static inline bool classof(const Value *V) { 4348 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4349 } 4350 4351 private: 4352 BasicBlock *getSuccessorV(unsigned Idx) const override; 4353 unsigned getNumSuccessorsV() const override; 4354 void setSuccessorV(unsigned Idx, BasicBlock *B) override; 4355 4356 // Shadow Instruction::setInstructionSubclassData with a private forwarding 4357 // method so that subclasses cannot accidentally use it. 4358 void setInstructionSubclassData(unsigned short D) { 4359 Instruction::setInstructionSubclassData(D); 4360 } 4361 }; 4362 4363 template <> 4364 struct OperandTraits<CleanupReturnInst> 4365 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; 4366 4367 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value) 4368 4369 //===----------------------------------------------------------------------===// 4370 // UnreachableInst Class 4371 //===----------------------------------------------------------------------===// 4372 4373 //===--------------------------------------------------------------------------- 4374 /// UnreachableInst - This function has undefined behavior. In particular, the 4375 /// presence of this instruction indicates some higher level knowledge that the 4376 /// end of the block cannot be reached. 4377 /// 4378 class UnreachableInst : public TerminatorInst { 4379 void *operator new(size_t, unsigned) = delete; 4380 4381 protected: 4382 // Note: Instruction needs to be a friend here to call cloneImpl. 4383 friend class Instruction; 4384 UnreachableInst *cloneImpl() const; 4385 4386 public: 4387 // allocate space for exactly zero operands 4388 void *operator new(size_t s) { 4389 return User::operator new(s, 0); 4390 } 4391 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); 4392 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); 4393 4394 unsigned getNumSuccessors() const { return 0; } 4395 4396 // Methods for support type inquiry through isa, cast, and dyn_cast: 4397 static inline bool classof(const Instruction *I) { 4398 return I->getOpcode() == Instruction::Unreachable; 4399 } 4400 static inline bool classof(const Value *V) { 4401 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4402 } 4403 4404 private: 4405 BasicBlock *getSuccessorV(unsigned idx) const override; 4406 unsigned getNumSuccessorsV() const override; 4407 void setSuccessorV(unsigned idx, BasicBlock *B) override; 4408 }; 4409 4410 //===----------------------------------------------------------------------===// 4411 // TruncInst Class 4412 //===----------------------------------------------------------------------===// 4413 4414 /// \brief This class represents a truncation of integer types. 4415 class TruncInst : public CastInst { 4416 protected: 4417 // Note: Instruction needs to be a friend here to call cloneImpl. 4418 friend class Instruction; 4419 /// \brief Clone an identical TruncInst 4420 TruncInst *cloneImpl() const; 4421 4422 public: 4423 /// \brief Constructor with insert-before-instruction semantics 4424 TruncInst( 4425 Value *S, ///< The value to be truncated 4426 Type *Ty, ///< The (smaller) type to truncate to 4427 const Twine &NameStr = "", ///< A name for the new instruction 4428 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4429 ); 4430 4431 /// \brief Constructor with insert-at-end-of-block semantics 4432 TruncInst( 4433 Value *S, ///< The value to be truncated 4434 Type *Ty, ///< The (smaller) type to truncate to 4435 const Twine &NameStr, ///< A name for the new instruction 4436 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4437 ); 4438 4439 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4440 static inline bool classof(const Instruction *I) { 4441 return I->getOpcode() == Trunc; 4442 } 4443 static inline bool classof(const Value *V) { 4444 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4445 } 4446 }; 4447 4448 //===----------------------------------------------------------------------===// 4449 // ZExtInst Class 4450 //===----------------------------------------------------------------------===// 4451 4452 /// \brief This class represents zero extension of integer types. 4453 class ZExtInst : public CastInst { 4454 protected: 4455 // Note: Instruction needs to be a friend here to call cloneImpl. 4456 friend class Instruction; 4457 /// \brief Clone an identical ZExtInst 4458 ZExtInst *cloneImpl() const; 4459 4460 public: 4461 /// \brief Constructor with insert-before-instruction semantics 4462 ZExtInst( 4463 Value *S, ///< The value to be zero extended 4464 Type *Ty, ///< The type to zero extend to 4465 const Twine &NameStr = "", ///< A name for the new instruction 4466 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4467 ); 4468 4469 /// \brief Constructor with insert-at-end semantics. 4470 ZExtInst( 4471 Value *S, ///< The value to be zero extended 4472 Type *Ty, ///< The type to zero extend to 4473 const Twine &NameStr, ///< A name for the new instruction 4474 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4475 ); 4476 4477 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4478 static inline bool classof(const Instruction *I) { 4479 return I->getOpcode() == ZExt; 4480 } 4481 static inline bool classof(const Value *V) { 4482 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4483 } 4484 }; 4485 4486 //===----------------------------------------------------------------------===// 4487 // SExtInst Class 4488 //===----------------------------------------------------------------------===// 4489 4490 /// \brief This class represents a sign extension of integer types. 4491 class SExtInst : public CastInst { 4492 protected: 4493 // Note: Instruction needs to be a friend here to call cloneImpl. 4494 friend class Instruction; 4495 /// \brief Clone an identical SExtInst 4496 SExtInst *cloneImpl() const; 4497 4498 public: 4499 /// \brief Constructor with insert-before-instruction semantics 4500 SExtInst( 4501 Value *S, ///< The value to be sign extended 4502 Type *Ty, ///< The type to sign extend to 4503 const Twine &NameStr = "", ///< A name for the new instruction 4504 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4505 ); 4506 4507 /// \brief Constructor with insert-at-end-of-block semantics 4508 SExtInst( 4509 Value *S, ///< The value to be sign extended 4510 Type *Ty, ///< The type to sign extend to 4511 const Twine &NameStr, ///< A name for the new instruction 4512 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4513 ); 4514 4515 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4516 static inline bool classof(const Instruction *I) { 4517 return I->getOpcode() == SExt; 4518 } 4519 static inline bool classof(const Value *V) { 4520 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4521 } 4522 }; 4523 4524 //===----------------------------------------------------------------------===// 4525 // FPTruncInst Class 4526 //===----------------------------------------------------------------------===// 4527 4528 /// \brief This class represents a truncation of floating point types. 4529 class FPTruncInst : public CastInst { 4530 protected: 4531 // Note: Instruction needs to be a friend here to call cloneImpl. 4532 friend class Instruction; 4533 /// \brief Clone an identical FPTruncInst 4534 FPTruncInst *cloneImpl() const; 4535 4536 public: 4537 /// \brief Constructor with insert-before-instruction semantics 4538 FPTruncInst( 4539 Value *S, ///< The value to be truncated 4540 Type *Ty, ///< The type to truncate to 4541 const Twine &NameStr = "", ///< A name for the new instruction 4542 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4543 ); 4544 4545 /// \brief Constructor with insert-before-instruction semantics 4546 FPTruncInst( 4547 Value *S, ///< The value to be truncated 4548 Type *Ty, ///< The type to truncate to 4549 const Twine &NameStr, ///< A name for the new instruction 4550 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4551 ); 4552 4553 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4554 static inline bool classof(const Instruction *I) { 4555 return I->getOpcode() == FPTrunc; 4556 } 4557 static inline bool classof(const Value *V) { 4558 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4559 } 4560 }; 4561 4562 //===----------------------------------------------------------------------===// 4563 // FPExtInst Class 4564 //===----------------------------------------------------------------------===// 4565 4566 /// \brief This class represents an extension of floating point types. 4567 class FPExtInst : public CastInst { 4568 protected: 4569 // Note: Instruction needs to be a friend here to call cloneImpl. 4570 friend class Instruction; 4571 /// \brief Clone an identical FPExtInst 4572 FPExtInst *cloneImpl() const; 4573 4574 public: 4575 /// \brief Constructor with insert-before-instruction semantics 4576 FPExtInst( 4577 Value *S, ///< The value to be extended 4578 Type *Ty, ///< The type to extend to 4579 const Twine &NameStr = "", ///< A name for the new instruction 4580 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4581 ); 4582 4583 /// \brief Constructor with insert-at-end-of-block semantics 4584 FPExtInst( 4585 Value *S, ///< The value to be extended 4586 Type *Ty, ///< The type to extend to 4587 const Twine &NameStr, ///< A name for the new instruction 4588 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4589 ); 4590 4591 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4592 static inline bool classof(const Instruction *I) { 4593 return I->getOpcode() == FPExt; 4594 } 4595 static inline bool classof(const Value *V) { 4596 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4597 } 4598 }; 4599 4600 //===----------------------------------------------------------------------===// 4601 // UIToFPInst Class 4602 //===----------------------------------------------------------------------===// 4603 4604 /// \brief This class represents a cast unsigned integer to floating point. 4605 class UIToFPInst : public CastInst { 4606 protected: 4607 // Note: Instruction needs to be a friend here to call cloneImpl. 4608 friend class Instruction; 4609 /// \brief Clone an identical UIToFPInst 4610 UIToFPInst *cloneImpl() const; 4611 4612 public: 4613 /// \brief Constructor with insert-before-instruction semantics 4614 UIToFPInst( 4615 Value *S, ///< The value to be converted 4616 Type *Ty, ///< The type to convert to 4617 const Twine &NameStr = "", ///< A name for the new instruction 4618 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4619 ); 4620 4621 /// \brief Constructor with insert-at-end-of-block semantics 4622 UIToFPInst( 4623 Value *S, ///< The value to be converted 4624 Type *Ty, ///< The type to convert to 4625 const Twine &NameStr, ///< A name for the new instruction 4626 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4627 ); 4628 4629 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4630 static inline bool classof(const Instruction *I) { 4631 return I->getOpcode() == UIToFP; 4632 } 4633 static inline bool classof(const Value *V) { 4634 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4635 } 4636 }; 4637 4638 //===----------------------------------------------------------------------===// 4639 // SIToFPInst Class 4640 //===----------------------------------------------------------------------===// 4641 4642 /// \brief This class represents a cast from signed integer to floating point. 4643 class SIToFPInst : public CastInst { 4644 protected: 4645 // Note: Instruction needs to be a friend here to call cloneImpl. 4646 friend class Instruction; 4647 /// \brief Clone an identical SIToFPInst 4648 SIToFPInst *cloneImpl() const; 4649 4650 public: 4651 /// \brief Constructor with insert-before-instruction semantics 4652 SIToFPInst( 4653 Value *S, ///< The value to be converted 4654 Type *Ty, ///< The type to convert to 4655 const Twine &NameStr = "", ///< A name for the new instruction 4656 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4657 ); 4658 4659 /// \brief Constructor with insert-at-end-of-block semantics 4660 SIToFPInst( 4661 Value *S, ///< The value to be converted 4662 Type *Ty, ///< The type to convert to 4663 const Twine &NameStr, ///< A name for the new instruction 4664 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4665 ); 4666 4667 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4668 static inline bool classof(const Instruction *I) { 4669 return I->getOpcode() == SIToFP; 4670 } 4671 static inline bool classof(const Value *V) { 4672 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4673 } 4674 }; 4675 4676 //===----------------------------------------------------------------------===// 4677 // FPToUIInst Class 4678 //===----------------------------------------------------------------------===// 4679 4680 /// \brief This class represents a cast from floating point to unsigned integer 4681 class FPToUIInst : public CastInst { 4682 protected: 4683 // Note: Instruction needs to be a friend here to call cloneImpl. 4684 friend class Instruction; 4685 /// \brief Clone an identical FPToUIInst 4686 FPToUIInst *cloneImpl() const; 4687 4688 public: 4689 /// \brief Constructor with insert-before-instruction semantics 4690 FPToUIInst( 4691 Value *S, ///< The value to be converted 4692 Type *Ty, ///< The type to convert to 4693 const Twine &NameStr = "", ///< A name for the new instruction 4694 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4695 ); 4696 4697 /// \brief Constructor with insert-at-end-of-block semantics 4698 FPToUIInst( 4699 Value *S, ///< The value to be converted 4700 Type *Ty, ///< The type to convert to 4701 const Twine &NameStr, ///< A name for the new instruction 4702 BasicBlock *InsertAtEnd ///< Where to insert the new instruction 4703 ); 4704 4705 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4706 static inline bool classof(const Instruction *I) { 4707 return I->getOpcode() == FPToUI; 4708 } 4709 static inline bool classof(const Value *V) { 4710 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4711 } 4712 }; 4713 4714 //===----------------------------------------------------------------------===// 4715 // FPToSIInst Class 4716 //===----------------------------------------------------------------------===// 4717 4718 /// \brief This class represents a cast from floating point to signed integer. 4719 class FPToSIInst : public CastInst { 4720 protected: 4721 // Note: Instruction needs to be a friend here to call cloneImpl. 4722 friend class Instruction; 4723 /// \brief Clone an identical FPToSIInst 4724 FPToSIInst *cloneImpl() const; 4725 4726 public: 4727 /// \brief Constructor with insert-before-instruction semantics 4728 FPToSIInst( 4729 Value *S, ///< The value to be converted 4730 Type *Ty, ///< The type to convert to 4731 const Twine &NameStr = "", ///< A name for the new instruction 4732 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4733 ); 4734 4735 /// \brief Constructor with insert-at-end-of-block semantics 4736 FPToSIInst( 4737 Value *S, ///< The value to be converted 4738 Type *Ty, ///< The type to convert to 4739 const Twine &NameStr, ///< A name for the new instruction 4740 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4741 ); 4742 4743 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast: 4744 static inline bool classof(const Instruction *I) { 4745 return I->getOpcode() == FPToSI; 4746 } 4747 static inline bool classof(const Value *V) { 4748 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4749 } 4750 }; 4751 4752 //===----------------------------------------------------------------------===// 4753 // IntToPtrInst Class 4754 //===----------------------------------------------------------------------===// 4755 4756 /// \brief This class represents a cast from an integer to a pointer. 4757 class IntToPtrInst : public CastInst { 4758 public: 4759 /// \brief Constructor with insert-before-instruction semantics 4760 IntToPtrInst( 4761 Value *S, ///< The value to be converted 4762 Type *Ty, ///< The type to convert to 4763 const Twine &NameStr = "", ///< A name for the new instruction 4764 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4765 ); 4766 4767 /// \brief Constructor with insert-at-end-of-block semantics 4768 IntToPtrInst( 4769 Value *S, ///< The value to be converted 4770 Type *Ty, ///< The type to convert to 4771 const Twine &NameStr, ///< A name for the new instruction 4772 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4773 ); 4774 4775 // Note: Instruction needs to be a friend here to call cloneImpl. 4776 friend class Instruction; 4777 /// \brief Clone an identical IntToPtrInst 4778 IntToPtrInst *cloneImpl() const; 4779 4780 /// \brief Returns the address space of this instruction's pointer type. 4781 unsigned getAddressSpace() const { 4782 return getType()->getPointerAddressSpace(); 4783 } 4784 4785 // Methods for support type inquiry through isa, cast, and dyn_cast: 4786 static inline bool classof(const Instruction *I) { 4787 return I->getOpcode() == IntToPtr; 4788 } 4789 static inline bool classof(const Value *V) { 4790 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4791 } 4792 }; 4793 4794 //===----------------------------------------------------------------------===// 4795 // PtrToIntInst Class 4796 //===----------------------------------------------------------------------===// 4797 4798 /// \brief This class represents a cast from a pointer to an integer 4799 class PtrToIntInst : public CastInst { 4800 protected: 4801 // Note: Instruction needs to be a friend here to call cloneImpl. 4802 friend class Instruction; 4803 /// \brief Clone an identical PtrToIntInst 4804 PtrToIntInst *cloneImpl() const; 4805 4806 public: 4807 /// \brief Constructor with insert-before-instruction semantics 4808 PtrToIntInst( 4809 Value *S, ///< The value to be converted 4810 Type *Ty, ///< The type to convert to 4811 const Twine &NameStr = "", ///< A name for the new instruction 4812 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4813 ); 4814 4815 /// \brief Constructor with insert-at-end-of-block semantics 4816 PtrToIntInst( 4817 Value *S, ///< The value to be converted 4818 Type *Ty, ///< The type to convert to 4819 const Twine &NameStr, ///< A name for the new instruction 4820 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4821 ); 4822 4823 /// \brief Gets the pointer operand. 4824 Value *getPointerOperand() { return getOperand(0); } 4825 /// \brief Gets the pointer operand. 4826 const Value *getPointerOperand() const { return getOperand(0); } 4827 /// \brief Gets the operand index of the pointer operand. 4828 static unsigned getPointerOperandIndex() { return 0U; } 4829 4830 /// \brief Returns the address space of the pointer operand. 4831 unsigned getPointerAddressSpace() const { 4832 return getPointerOperand()->getType()->getPointerAddressSpace(); 4833 } 4834 4835 // Methods for support type inquiry through isa, cast, and dyn_cast: 4836 static inline bool classof(const Instruction *I) { 4837 return I->getOpcode() == PtrToInt; 4838 } 4839 static inline bool classof(const Value *V) { 4840 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4841 } 4842 }; 4843 4844 //===----------------------------------------------------------------------===// 4845 // BitCastInst Class 4846 //===----------------------------------------------------------------------===// 4847 4848 /// \brief This class represents a no-op cast from one type to another. 4849 class BitCastInst : public CastInst { 4850 protected: 4851 // Note: Instruction needs to be a friend here to call cloneImpl. 4852 friend class Instruction; 4853 /// \brief Clone an identical BitCastInst 4854 BitCastInst *cloneImpl() const; 4855 4856 public: 4857 /// \brief Constructor with insert-before-instruction semantics 4858 BitCastInst( 4859 Value *S, ///< The value to be casted 4860 Type *Ty, ///< The type to casted to 4861 const Twine &NameStr = "", ///< A name for the new instruction 4862 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4863 ); 4864 4865 /// \brief Constructor with insert-at-end-of-block semantics 4866 BitCastInst( 4867 Value *S, ///< The value to be casted 4868 Type *Ty, ///< The type to casted to 4869 const Twine &NameStr, ///< A name for the new instruction 4870 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4871 ); 4872 4873 // Methods for support type inquiry through isa, cast, and dyn_cast: 4874 static inline bool classof(const Instruction *I) { 4875 return I->getOpcode() == BitCast; 4876 } 4877 static inline bool classof(const Value *V) { 4878 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4879 } 4880 }; 4881 4882 //===----------------------------------------------------------------------===// 4883 // AddrSpaceCastInst Class 4884 //===----------------------------------------------------------------------===// 4885 4886 /// \brief This class represents a conversion between pointers from 4887 /// one address space to another. 4888 class AddrSpaceCastInst : public CastInst { 4889 protected: 4890 // Note: Instruction needs to be a friend here to call cloneImpl. 4891 friend class Instruction; 4892 /// \brief Clone an identical AddrSpaceCastInst 4893 AddrSpaceCastInst *cloneImpl() const; 4894 4895 public: 4896 /// \brief Constructor with insert-before-instruction semantics 4897 AddrSpaceCastInst( 4898 Value *S, ///< The value to be casted 4899 Type *Ty, ///< The type to casted to 4900 const Twine &NameStr = "", ///< A name for the new instruction 4901 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction 4902 ); 4903 4904 /// \brief Constructor with insert-at-end-of-block semantics 4905 AddrSpaceCastInst( 4906 Value *S, ///< The value to be casted 4907 Type *Ty, ///< The type to casted to 4908 const Twine &NameStr, ///< A name for the new instruction 4909 BasicBlock *InsertAtEnd ///< The block to insert the instruction into 4910 ); 4911 4912 // Methods for support type inquiry through isa, cast, and dyn_cast: 4913 static inline bool classof(const Instruction *I) { 4914 return I->getOpcode() == AddrSpaceCast; 4915 } 4916 static inline bool classof(const Value *V) { 4917 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 4918 } 4919 4920 /// \brief Gets the pointer operand. 4921 Value *getPointerOperand() { 4922 return getOperand(0); 4923 } 4924 4925 /// \brief Gets the pointer operand. 4926 const Value *getPointerOperand() const { 4927 return getOperand(0); 4928 } 4929 4930 /// \brief Gets the operand index of the pointer operand. 4931 static unsigned getPointerOperandIndex() { 4932 return 0U; 4933 } 4934 4935 /// \brief Returns the address space of the pointer operand. 4936 unsigned getSrcAddressSpace() const { 4937 return getPointerOperand()->getType()->getPointerAddressSpace(); 4938 } 4939 4940 /// \brief Returns the address space of the result. 4941 unsigned getDestAddressSpace() const { 4942 return getType()->getPointerAddressSpace(); 4943 } 4944 }; 4945 4946 } // End llvm namespace 4947 4948 #endif 4949