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