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