1 //===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
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"
55 //===----------------------------------------------------------------------===//
57 //===----------------------------------------------------------------------===//
59 /// an instruction to allocate memory on the stack
60 class AllocaInst : public UnaryInstruction {
64 // Note: Instruction needs to be a friend here to call cloneImpl.
65 friend class Instruction;
67 AllocaInst *cloneImpl() const;
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);
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);
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);
87 /// Return true if there is an allocation size parameter to the allocation
88 /// instruction that is not 1.
89 bool isArrayAllocation() const;
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); }
96 /// Overload to return most specific pointer type.
97 PointerType *getType() const {
98 return cast<PointerType>(Instruction::getType());
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;
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; }
111 /// Return the alignment of the memory that is being allocated by the
113 unsigned getAlignment() const {
114 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
116 void setAlignment(unsigned Align);
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;
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;
129 /// Specify whether this alloca is used to represent the arguments to a call.
130 void setUsedWithInAlloca(bool V) {
131 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
135 /// Return true if this alloca is used as a swifterror argument to a call.
136 bool isSwiftError() const {
137 return getSubclassDataFromInstruction() & 64;
140 /// Specify whether this alloca is used to represent a swifterror.
141 void setSwiftError(bool V) {
142 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
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);
150 static bool classof(const Value *V) {
151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
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);
162 //===----------------------------------------------------------------------===//
164 //===----------------------------------------------------------------------===//
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 {
172 // Note: Instruction needs to be a friend here to call cloneImpl.
173 friend class Instruction;
175 LoadInst *cloneImpl() const;
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);
220 /// Return true if this is a load from a volatile memory location.
221 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
223 /// Specify whether this is a volatile load or not.
224 void setVolatile(bool V) {
225 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
229 /// Return the alignment of the access that is being performed.
230 unsigned getAlignment() const {
231 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
234 void setAlignment(unsigned Align);
236 /// Returns the ordering constraint of this load instruction.
237 AtomicOrdering getOrdering() const {
238 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
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));
248 /// Returns the synchronization scope ID of this load instruction.
249 SyncScope::ID getSyncScopeID() const {
253 /// Sets the synchronization scope ID of this load instruction.
254 void setSyncScopeID(SyncScope::ID SSID) {
258 /// Sets the ordering constraint and the synchronization scope ID of this load
260 void setAtomic(AtomicOrdering Ordering,
261 SyncScope::ID SSID = SyncScope::System) {
262 setOrdering(Ordering);
263 setSyncScopeID(SSID);
266 bool isSimple() const { return !isAtomic() && !isVolatile(); }
268 bool isUnordered() const {
269 return (getOrdering() == AtomicOrdering::NotAtomic ||
270 getOrdering() == AtomicOrdering::Unordered) &&
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(); }
279 /// Returns the address space of the pointer operand.
280 unsigned getPointerAddressSpace() const {
281 return getPointerOperandType()->getPointerAddressSpace();
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;
288 static bool classof(const Value *V) {
289 return isa<Instruction>(V) && classof(cast<Instruction>(V));
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);
299 /// The synchronization scope ID of this load instruction. Not quite enough
300 /// room in SubClassData for everything, so synchronization scope ID gets its
305 //===----------------------------------------------------------------------===//
307 //===----------------------------------------------------------------------===//
309 /// An instruction for storing to memory.
310 class StoreInst : public Instruction {
314 // Note: Instruction needs to be a friend here to call cloneImpl.
315 friend class Instruction;
317 StoreInst *cloneImpl() const;
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);
337 // allocate space for exactly two operands
338 void *operator new(size_t s) {
339 return User::operator new(s, 2);
342 /// Return true if this is a store to a volatile memory location.
343 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
345 /// Specify whether this is a volatile store or not.
346 void setVolatile(bool V) {
347 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
354 /// Return the alignment of the access that is being performed
355 unsigned getAlignment() const {
356 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
359 void setAlignment(unsigned Align);
361 /// Returns the ordering constraint of this store instruction.
362 AtomicOrdering getOrdering() const {
363 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
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));
373 /// Returns the synchronization scope ID of this store instruction.
374 SyncScope::ID getSyncScopeID() const {
378 /// Sets the synchronization scope ID of this store instruction.
379 void setSyncScopeID(SyncScope::ID SSID) {
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);
391 bool isSimple() const { return !isAtomic() && !isVolatile(); }
393 bool isUnordered() const {
394 return (getOrdering() == AtomicOrdering::NotAtomic ||
395 getOrdering() == AtomicOrdering::Unordered) &&
399 Value *getValueOperand() { return getOperand(0); }
400 const Value *getValueOperand() const { return getOperand(0); }
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(); }
407 /// Returns the address space of the pointer operand.
408 unsigned getPointerAddressSpace() const {
409 return getPointerOperandType()->getPointerAddressSpace();
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;
416 static bool classof(const Value *V) {
417 return isa<Instruction>(V) && classof(cast<Instruction>(V));
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);
427 /// The synchronization scope ID of this store instruction. Not quite enough
428 /// room in SubClassData for everything, so synchronization scope ID gets its
434 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
437 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
439 //===----------------------------------------------------------------------===//
441 //===----------------------------------------------------------------------===//
443 /// An instruction for ordering other memory operations.
444 class FenceInst : public Instruction {
445 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
448 // Note: Instruction needs to be a friend here to call cloneImpl.
449 friend class Instruction;
451 FenceInst *cloneImpl() const;
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);
462 // allocate space for exactly zero operands
463 void *operator new(size_t s) {
464 return User::operator new(s, 0);
467 /// Returns the ordering constraint of this fence instruction.
468 AtomicOrdering getOrdering() const {
469 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
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));
479 /// Returns the synchronization scope ID of this fence instruction.
480 SyncScope::ID getSyncScopeID() const {
484 /// Sets the synchronization scope ID of this fence instruction.
485 void setSyncScopeID(SyncScope::ID SSID) {
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;
493 static bool classof(const Value *V) {
494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
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);
504 /// The synchronization scope ID of this fence instruction. Not quite enough
505 /// room in SubClassData for everything, so synchronization scope ID gets its
510 //===----------------------------------------------------------------------===//
511 // AtomicCmpXchgInst Class
512 //===----------------------------------------------------------------------===//
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.
518 class AtomicCmpXchgInst : public Instruction {
519 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
520 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
524 // Note: Instruction needs to be a friend here to call cloneImpl.
525 friend class Instruction;
527 AtomicCmpXchgInst *cloneImpl() const;
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);
539 // allocate space for exactly three operands
540 void *operator new(size_t s) {
541 return User::operator new(s, 3);
544 /// Return true if this is a cmpxchg from a volatile memory
547 bool isVolatile() const {
548 return getSubclassDataFromInstruction() & 1;
551 /// Specify whether this is a volatile cmpxchg.
553 void setVolatile(bool V) {
554 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
558 /// Return true if this cmpxchg may spuriously fail.
559 bool isWeak() const {
560 return getSubclassDataFromInstruction() & 0x100;
563 void setWeak(bool IsWeak) {
564 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
568 /// Transparently provide more efficient getOperand methods.
569 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
571 /// Returns the success ordering constraint of this cmpxchg instruction.
572 AtomicOrdering getSuccessOrdering() const {
573 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
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));
584 /// Returns the failure ordering constraint of this cmpxchg instruction.
585 AtomicOrdering getFailureOrdering() const {
586 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
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));
597 /// Returns the synchronization scope ID of this cmpxchg instruction.
598 SyncScope::ID getSyncScopeID() const {
602 /// Sets the synchronization scope ID of this cmpxchg instruction.
603 void setSyncScopeID(SyncScope::ID SSID) {
607 Value *getPointerOperand() { return getOperand(0); }
608 const Value *getPointerOperand() const { return getOperand(0); }
609 static unsigned getPointerOperandIndex() { return 0U; }
611 Value *getCompareOperand() { return getOperand(1); }
612 const Value *getCompareOperand() const { return getOperand(1); }
614 Value *getNewValOperand() { return getOperand(2); }
615 const Value *getNewValOperand() const { return getOperand(2); }
617 /// Returns the address space of the pointer operand.
618 unsigned getPointerAddressSpace() const {
619 return getPointerOperand()->getType()->getPointerAddressSpace();
622 /// Returns the strongest permitted ordering on failure, given the
623 /// desired ordering on success.
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) {
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;
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;
649 static bool classof(const Value *V) {
650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
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);
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.
667 struct OperandTraits<AtomicCmpXchgInst> :
668 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
671 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
673 //===----------------------------------------------------------------------===//
674 // AtomicRMWInst Class
675 //===----------------------------------------------------------------------===//
677 /// an instruction that atomically reads a memory location,
678 /// combines it with another value, and then stores the result back. Returns
681 class AtomicRMWInst : public Instruction {
683 // Note: Instruction needs to be a friend here to call cloneImpl.
684 friend class Instruction;
686 AtomicRMWInst *cloneImpl() const;
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'.
708 /// *p = old >signed v ? old : v
710 /// *p = old <signed v ? old : v
712 /// *p = old >unsigned v ? old : v
714 /// *p = old <unsigned v ? old : v
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);
729 // allocate space for exactly two operands
730 void *operator new(size_t s) {
731 return User::operator new(s, 2);
734 BinOp getOperation() const {
735 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
738 void setOperation(BinOp Operation) {
739 unsigned short SubclassData = getSubclassDataFromInstruction();
740 setInstructionSubclassData((SubclassData & 31) |
744 /// Return true if this is a RMW on a volatile memory location.
746 bool isVolatile() const {
747 return getSubclassDataFromInstruction() & 1;
750 /// Specify whether this is a volatile RMW or not.
752 void setVolatile(bool V) {
753 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
757 /// Transparently provide more efficient getOperand methods.
758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
760 /// Returns the ordering constraint of this rmw instruction.
761 AtomicOrdering getOrdering() const {
762 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
765 /// Sets the ordering constraint of this rmw instruction.
766 void setOrdering(AtomicOrdering Ordering) {
767 assert(Ordering != AtomicOrdering::NotAtomic &&
768 "atomicrmw instructions can only be atomic.");
769 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
770 ((unsigned)Ordering << 2));
773 /// Returns the synchronization scope ID of this rmw instruction.
774 SyncScope::ID getSyncScopeID() const {
778 /// Sets the synchronization scope ID of this rmw instruction.
779 void setSyncScopeID(SyncScope::ID SSID) {
783 Value *getPointerOperand() { return getOperand(0); }
784 const Value *getPointerOperand() const { return getOperand(0); }
785 static unsigned getPointerOperandIndex() { return 0U; }
787 Value *getValOperand() { return getOperand(1); }
788 const Value *getValOperand() const { return getOperand(1); }
790 /// Returns the address space of the pointer operand.
791 unsigned getPointerAddressSpace() const {
792 return getPointerOperand()->getType()->getPointerAddressSpace();
795 // Methods for support type inquiry through isa, cast, and dyn_cast:
796 static bool classof(const Instruction *I) {
797 return I->getOpcode() == Instruction::AtomicRMW;
799 static bool classof(const Value *V) {
800 return isa<Instruction>(V) && classof(cast<Instruction>(V));
804 void Init(BinOp Operation, Value *Ptr, Value *Val,
805 AtomicOrdering Ordering, SyncScope::ID SSID);
807 // Shadow Instruction::setInstructionSubclassData with a private forwarding
808 // method so that subclasses cannot accidentally use it.
809 void setInstructionSubclassData(unsigned short D) {
810 Instruction::setInstructionSubclassData(D);
813 /// The synchronization scope ID of this rmw instruction. Not quite enough
814 /// room in SubClassData for everything, so synchronization scope ID gets its
820 struct OperandTraits<AtomicRMWInst>
821 : public FixedNumOperandTraits<AtomicRMWInst,2> {
824 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
826 //===----------------------------------------------------------------------===//
827 // GetElementPtrInst Class
828 //===----------------------------------------------------------------------===//
830 // checkGEPType - Simple wrapper function to give a better assertion failure
831 // message on bad indexes for a gep instruction.
833 inline Type *checkGEPType(Type *Ty) {
834 assert(Ty && "Invalid GetElementPtrInst indices for type!");
838 /// an instruction for type-safe pointer arithmetic to
839 /// access elements of arrays and structs
841 class GetElementPtrInst : public Instruction {
842 Type *SourceElementType;
843 Type *ResultElementType;
845 GetElementPtrInst(const GetElementPtrInst &GEPI);
847 /// Constructors - Create a getelementptr instruction with a base pointer an
848 /// list of indices. The first ctor can optionally insert before an existing
849 /// instruction, the second appends the new instruction to the specified
851 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
852 ArrayRef<Value *> IdxList, unsigned Values,
853 const Twine &NameStr, Instruction *InsertBefore);
854 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
855 ArrayRef<Value *> IdxList, unsigned Values,
856 const Twine &NameStr, BasicBlock *InsertAtEnd);
858 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
861 // Note: Instruction needs to be a friend here to call cloneImpl.
862 friend class Instruction;
864 GetElementPtrInst *cloneImpl() const;
867 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
868 ArrayRef<Value *> IdxList,
869 const Twine &NameStr = "",
870 Instruction *InsertBefore = nullptr) {
871 unsigned Values = 1 + unsigned(IdxList.size());
874 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
878 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
879 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
880 NameStr, InsertBefore);
883 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
884 ArrayRef<Value *> IdxList,
885 const Twine &NameStr,
886 BasicBlock *InsertAtEnd) {
887 unsigned Values = 1 + unsigned(IdxList.size());
890 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
894 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
895 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
896 NameStr, InsertAtEnd);
899 /// Create an "inbounds" getelementptr. See the documentation for the
900 /// "inbounds" flag in LangRef.html for details.
901 static GetElementPtrInst *CreateInBounds(Value *Ptr,
902 ArrayRef<Value *> IdxList,
903 const Twine &NameStr = "",
904 Instruction *InsertBefore = nullptr){
905 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
908 static GetElementPtrInst *
909 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
910 const Twine &NameStr = "",
911 Instruction *InsertBefore = nullptr) {
912 GetElementPtrInst *GEP =
913 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
914 GEP->setIsInBounds(true);
918 static GetElementPtrInst *CreateInBounds(Value *Ptr,
919 ArrayRef<Value *> IdxList,
920 const Twine &NameStr,
921 BasicBlock *InsertAtEnd) {
922 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
925 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
926 ArrayRef<Value *> IdxList,
927 const Twine &NameStr,
928 BasicBlock *InsertAtEnd) {
929 GetElementPtrInst *GEP =
930 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
931 GEP->setIsInBounds(true);
935 /// Transparently provide more efficient getOperand methods.
936 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
938 Type *getSourceElementType() const { return SourceElementType; }
940 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
941 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
943 Type *getResultElementType() const {
944 assert(ResultElementType ==
945 cast<PointerType>(getType()->getScalarType())->getElementType());
946 return ResultElementType;
949 /// Returns the address space of this instruction's pointer type.
950 unsigned getAddressSpace() const {
951 // Note that this is always the same as the pointer operand's address space
952 // and that is cheaper to compute, so cheat here.
953 return getPointerAddressSpace();
956 /// Returns the type of the element that would be loaded with
957 /// a load instruction with the specified parameters.
959 /// Null is returned if the indices are invalid for the specified
962 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
963 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
964 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
966 inline op_iterator idx_begin() { return op_begin()+1; }
967 inline const_op_iterator idx_begin() const { return op_begin()+1; }
968 inline op_iterator idx_end() { return op_end(); }
969 inline const_op_iterator idx_end() const { return op_end(); }
971 inline iterator_range<op_iterator> indices() {
972 return make_range(idx_begin(), idx_end());
975 inline iterator_range<const_op_iterator> indices() const {
976 return make_range(idx_begin(), idx_end());
979 Value *getPointerOperand() {
980 return getOperand(0);
982 const Value *getPointerOperand() const {
983 return getOperand(0);
985 static unsigned getPointerOperandIndex() {
986 return 0U; // get index for modifying correct operand.
989 /// Method to return the pointer operand as a
991 Type *getPointerOperandType() const {
992 return getPointerOperand()->getType();
995 /// Returns the address space of the pointer operand.
996 unsigned getPointerAddressSpace() const {
997 return getPointerOperandType()->getPointerAddressSpace();
1000 /// Returns the pointer type returned by the GEP
1001 /// instruction, which may be a vector of pointers.
1002 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
1003 return getGEPReturnType(
1004 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
1007 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1008 ArrayRef<Value *> IdxList) {
1009 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
1010 Ptr->getType()->getPointerAddressSpace());
1012 if (Ptr->getType()->isVectorTy()) {
1013 unsigned NumElem = Ptr->getType()->getVectorNumElements();
1014 return VectorType::get(PtrTy, NumElem);
1016 for (Value *Index : IdxList)
1017 if (Index->getType()->isVectorTy()) {
1018 unsigned NumElem = Index->getType()->getVectorNumElements();
1019 return VectorType::get(PtrTy, NumElem);
1025 unsigned getNumIndices() const { // Note: always non-negative
1026 return getNumOperands() - 1;
1029 bool hasIndices() const {
1030 return getNumOperands() > 1;
1033 /// Return true if all of the indices of this GEP are
1034 /// zeros. If so, the result pointer and the first operand have the same
1035 /// value, just potentially different types.
1036 bool hasAllZeroIndices() const;
1038 /// Return true if all of the indices of this GEP are
1039 /// constant integers. If so, the result pointer and the first operand have
1040 /// a constant offset between them.
1041 bool hasAllConstantIndices() const;
1043 /// Set or clear the inbounds flag on this GEP instruction.
1044 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1045 void setIsInBounds(bool b = true);
1047 /// Determine whether the GEP has the inbounds flag.
1048 bool isInBounds() const;
1050 /// Accumulate the constant address offset of this GEP if possible.
1052 /// This routine accepts an APInt into which it will accumulate the constant
1053 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1054 /// all-constant, it returns false and the value of the offset APInt is
1055 /// undefined (it is *not* preserved!). The APInt passed into this routine
1056 /// must be at least as wide as the IntPtr type for the address space of
1057 /// the base GEP pointer.
1058 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1060 // Methods for support type inquiry through isa, cast, and dyn_cast:
1061 static bool classof(const Instruction *I) {
1062 return (I->getOpcode() == Instruction::GetElementPtr);
1064 static bool classof(const Value *V) {
1065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1070 struct OperandTraits<GetElementPtrInst> :
1071 public VariadicOperandTraits<GetElementPtrInst, 1> {
1074 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1075 ArrayRef<Value *> IdxList, unsigned Values,
1076 const Twine &NameStr,
1077 Instruction *InsertBefore)
1078 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1079 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1080 Values, InsertBefore),
1081 SourceElementType(PointeeType),
1082 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1083 assert(ResultElementType ==
1084 cast<PointerType>(getType()->getScalarType())->getElementType());
1085 init(Ptr, IdxList, NameStr);
1088 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1089 ArrayRef<Value *> IdxList, unsigned Values,
1090 const Twine &NameStr,
1091 BasicBlock *InsertAtEnd)
1092 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1093 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1094 Values, InsertAtEnd),
1095 SourceElementType(PointeeType),
1096 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1097 assert(ResultElementType ==
1098 cast<PointerType>(getType()->getScalarType())->getElementType());
1099 init(Ptr, IdxList, NameStr);
1102 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1104 //===----------------------------------------------------------------------===//
1106 //===----------------------------------------------------------------------===//
1108 /// This instruction compares its operands according to the predicate given
1109 /// to the constructor. It only operates on integers or pointers. The operands
1110 /// must be identical types.
1111 /// Represent an integer comparison operator.
1112 class ICmpInst: public CmpInst {
1114 assert(isIntPredicate() &&
1115 "Invalid ICmp predicate value");
1116 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1117 "Both operands to ICmp instruction are not of the same type!");
1118 // Check that the operands are the right type
1119 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1120 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1121 "Invalid operand types for ICmp instruction");
1125 // Note: Instruction needs to be a friend here to call cloneImpl.
1126 friend class Instruction;
1128 /// Clone an identical ICmpInst
1129 ICmpInst *cloneImpl() const;
1132 /// Constructor with insert-before-instruction semantics.
1134 Instruction *InsertBefore, ///< Where to insert
1135 Predicate pred, ///< The predicate to use for the comparison
1136 Value *LHS, ///< The left-hand-side of the expression
1137 Value *RHS, ///< The right-hand-side of the expression
1138 const Twine &NameStr = "" ///< Name of the instruction
1139 ) : CmpInst(makeCmpResultType(LHS->getType()),
1140 Instruction::ICmp, pred, LHS, RHS, NameStr,
1147 /// Constructor with insert-at-end semantics.
1149 BasicBlock &InsertAtEnd, ///< Block to insert into.
1150 Predicate pred, ///< The predicate to use for the comparison
1151 Value *LHS, ///< The left-hand-side of the expression
1152 Value *RHS, ///< The right-hand-side of the expression
1153 const Twine &NameStr = "" ///< Name of the instruction
1154 ) : CmpInst(makeCmpResultType(LHS->getType()),
1155 Instruction::ICmp, pred, LHS, RHS, NameStr,
1162 /// Constructor with no-insertion semantics
1164 Predicate pred, ///< The predicate to use for the comparison
1165 Value *LHS, ///< The left-hand-side of the expression
1166 Value *RHS, ///< The right-hand-side of the expression
1167 const Twine &NameStr = "" ///< Name of the instruction
1168 ) : CmpInst(makeCmpResultType(LHS->getType()),
1169 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1175 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1176 /// @returns the predicate that would be the result if the operand were
1177 /// regarded as signed.
1178 /// Return the signed version of the predicate
1179 Predicate getSignedPredicate() const {
1180 return getSignedPredicate(getPredicate());
1183 /// This is a static version that you can use without an instruction.
1184 /// Return the signed version of the predicate.
1185 static Predicate getSignedPredicate(Predicate pred);
1187 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1188 /// @returns the predicate that would be the result if the operand were
1189 /// regarded as unsigned.
1190 /// Return the unsigned version of the predicate
1191 Predicate getUnsignedPredicate() const {
1192 return getUnsignedPredicate(getPredicate());
1195 /// This is a static version that you can use without an instruction.
1196 /// Return the unsigned version of the predicate.
1197 static Predicate getUnsignedPredicate(Predicate pred);
1199 /// Return true if this predicate is either EQ or NE. This also
1200 /// tests for commutativity.
1201 static bool isEquality(Predicate P) {
1202 return P == ICMP_EQ || P == ICMP_NE;
1205 /// Return true if this predicate is either EQ or NE. This also
1206 /// tests for commutativity.
1207 bool isEquality() const {
1208 return isEquality(getPredicate());
1211 /// @returns true if the predicate of this ICmpInst is commutative
1212 /// Determine if this relation is commutative.
1213 bool isCommutative() const { return isEquality(); }
1215 /// Return true if the predicate is relational (not EQ or NE).
1217 bool isRelational() const {
1218 return !isEquality();
1221 /// Return true if the predicate is relational (not EQ or NE).
1223 static bool isRelational(Predicate P) {
1224 return !isEquality(P);
1227 /// Exchange the two operands to this instruction in such a way that it does
1228 /// not modify the semantics of the instruction. The predicate value may be
1229 /// changed to retain the same result if the predicate is order dependent
1231 /// Swap operands and adjust predicate.
1232 void swapOperands() {
1233 setPredicate(getSwappedPredicate());
1234 Op<0>().swap(Op<1>());
1237 // Methods for support type inquiry through isa, cast, and dyn_cast:
1238 static bool classof(const Instruction *I) {
1239 return I->getOpcode() == Instruction::ICmp;
1241 static bool classof(const Value *V) {
1242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1246 //===----------------------------------------------------------------------===//
1248 //===----------------------------------------------------------------------===//
1250 /// This instruction compares its operands according to the predicate given
1251 /// to the constructor. It only operates on floating point values or packed
1252 /// vectors of floating point values. The operands must be identical types.
1253 /// Represents a floating point comparison operator.
1254 class FCmpInst: public CmpInst {
1256 assert(isFPPredicate() && "Invalid FCmp predicate value");
1257 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1258 "Both operands to FCmp instruction are not of the same type!");
1259 // Check that the operands are the right type
1260 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1261 "Invalid operand types for FCmp instruction");
1265 // Note: Instruction needs to be a friend here to call cloneImpl.
1266 friend class Instruction;
1268 /// Clone an identical FCmpInst
1269 FCmpInst *cloneImpl() const;
1272 /// Constructor with insert-before-instruction semantics.
1274 Instruction *InsertBefore, ///< Where to insert
1275 Predicate pred, ///< The predicate to use for the comparison
1276 Value *LHS, ///< The left-hand-side of the expression
1277 Value *RHS, ///< The right-hand-side of the expression
1278 const Twine &NameStr = "" ///< Name of the instruction
1279 ) : CmpInst(makeCmpResultType(LHS->getType()),
1280 Instruction::FCmp, pred, LHS, RHS, NameStr,
1285 /// Constructor with insert-at-end semantics.
1287 BasicBlock &InsertAtEnd, ///< Block to insert into.
1288 Predicate pred, ///< The predicate to use for the comparison
1289 Value *LHS, ///< The left-hand-side of the expression
1290 Value *RHS, ///< The right-hand-side of the expression
1291 const Twine &NameStr = "" ///< Name of the instruction
1292 ) : CmpInst(makeCmpResultType(LHS->getType()),
1293 Instruction::FCmp, pred, LHS, RHS, NameStr,
1298 /// Constructor with no-insertion semantics
1300 Predicate pred, ///< The predicate to use for the comparison
1301 Value *LHS, ///< The left-hand-side of the expression
1302 Value *RHS, ///< The right-hand-side of the expression
1303 const Twine &NameStr = "" ///< Name of the instruction
1304 ) : CmpInst(makeCmpResultType(LHS->getType()),
1305 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1309 /// @returns true if the predicate of this instruction is EQ or NE.
1310 /// Determine if this is an equality predicate.
1311 static bool isEquality(Predicate Pred) {
1312 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1316 /// @returns true if the predicate of this instruction is EQ or NE.
1317 /// Determine if this is an equality predicate.
1318 bool isEquality() const { return isEquality(getPredicate()); }
1320 /// @returns true if the predicate of this instruction is commutative.
1321 /// Determine if this is a commutative predicate.
1322 bool isCommutative() const {
1323 return isEquality() ||
1324 getPredicate() == FCMP_FALSE ||
1325 getPredicate() == FCMP_TRUE ||
1326 getPredicate() == FCMP_ORD ||
1327 getPredicate() == FCMP_UNO;
1330 /// @returns true if the predicate is relational (not EQ or NE).
1331 /// Determine if this a relational predicate.
1332 bool isRelational() const { return !isEquality(); }
1334 /// Exchange the two operands to this instruction in such a way that it does
1335 /// not modify the semantics of the instruction. The predicate value may be
1336 /// changed to retain the same result if the predicate is order dependent
1338 /// Swap operands and adjust predicate.
1339 void swapOperands() {
1340 setPredicate(getSwappedPredicate());
1341 Op<0>().swap(Op<1>());
1344 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1345 static bool classof(const Instruction *I) {
1346 return I->getOpcode() == Instruction::FCmp;
1348 static bool classof(const Value *V) {
1349 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1356 template <class T> struct CallBaseParent { using type = Instruction; };
1358 template <> struct CallBaseParent<InvokeInst> { using type = TerminatorInst; };
1360 //===----------------------------------------------------------------------===//
1361 /// Base class for all callable instructions (InvokeInst and CallInst)
1362 /// Holds everything related to calling a function, abstracting from the base
1363 /// type @p BaseInstTy and the concrete instruction @p InstTy
1365 template <class InstTy>
1366 class CallBase : public CallBaseParent<InstTy>::type,
1367 public OperandBundleUser<InstTy, User::op_iterator> {
1369 AttributeList Attrs; ///< parameter attributes for callable
1371 using BaseInstTy = typename CallBaseParent<InstTy>::type;
1373 template <class... ArgsTy>
1374 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1375 : BaseInstTy(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {}
1376 bool hasDescriptor() const { return Value::HasDescriptor; }
1378 using BaseInstTy::BaseInstTy;
1380 using OperandBundleUser<InstTy,
1381 User::op_iterator>::isFnAttrDisallowedByOpBundle;
1382 using OperandBundleUser<InstTy, User::op_iterator>::getNumTotalBundleOperands;
1383 using OperandBundleUser<InstTy, User::op_iterator>::bundleOperandHasAttr;
1384 using Instruction::getSubclassDataFromInstruction;
1385 using Instruction::setInstructionSubclassData;
1388 using Instruction::getContext;
1389 using OperandBundleUser<InstTy, User::op_iterator>::hasOperandBundles;
1390 using OperandBundleUser<InstTy,
1391 User::op_iterator>::getBundleOperandsStartIndex;
1393 static bool classof(const Instruction *I) {
1395 "CallBase is not meant to be used as part of the classof hierarchy");
1399 /// Return the parameter attributes for this call.
1401 AttributeList getAttributes() const { return Attrs; }
1403 /// Set the parameter attributes for this call.
1405 void setAttributes(AttributeList A) { Attrs = A; }
1407 FunctionType *getFunctionType() const { return FTy; }
1409 void mutateFunctionType(FunctionType *FTy) {
1410 Value::mutateType(FTy->getReturnType());
1414 /// Return the number of call arguments.
1416 unsigned getNumArgOperands() const {
1417 return getNumOperands() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1420 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1422 Value *getArgOperand(unsigned i) const {
1423 assert(i < getNumArgOperands() && "Out of bounds!");
1424 return getOperand(i);
1426 void setArgOperand(unsigned i, Value *v) {
1427 assert(i < getNumArgOperands() && "Out of bounds!");
1431 /// Return the iterator pointing to the beginning of the argument list.
1432 User::op_iterator arg_begin() { return op_begin(); }
1434 /// Return the iterator pointing to the end of the argument list.
1435 User::op_iterator arg_end() {
1436 // [ call args ], [ operand bundles ], callee
1437 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1440 /// Iteration adapter for range-for loops.
1441 iterator_range<User::op_iterator> arg_operands() {
1442 return make_range(arg_begin(), arg_end());
1445 /// Return the iterator pointing to the beginning of the argument list.
1446 User::const_op_iterator arg_begin() const { return op_begin(); }
1448 /// Return the iterator pointing to the end of the argument list.
1449 User::const_op_iterator arg_end() const {
1450 // [ call args ], [ operand bundles ], callee
1451 return op_end() - getNumTotalBundleOperands() - InstTy::ArgOffset;
1454 /// Iteration adapter for range-for loops.
1455 iterator_range<User::const_op_iterator> arg_operands() const {
1456 return make_range(arg_begin(), arg_end());
1459 /// Wrappers for getting the \c Use of a call argument.
1460 const Use &getArgOperandUse(unsigned i) const {
1461 assert(i < getNumArgOperands() && "Out of bounds!");
1462 return User::getOperandUse(i);
1464 Use &getArgOperandUse(unsigned i) {
1465 assert(i < getNumArgOperands() && "Out of bounds!");
1466 return User::getOperandUse(i);
1469 /// If one of the arguments has the 'returned' attribute, return its
1470 /// operand value. Otherwise, return nullptr.
1471 Value *getReturnedArgOperand() const {
1474 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
1475 return getArgOperand(Index - AttributeList::FirstArgIndex);
1476 if (const Function *F = getCalledFunction())
1477 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
1479 return getArgOperand(Index - AttributeList::FirstArgIndex);
1484 User::op_iterator op_begin() {
1485 return OperandTraits<CallBase>::op_begin(this);
1488 User::const_op_iterator op_begin() const {
1489 return OperandTraits<CallBase>::op_begin(const_cast<CallBase *>(this));
1492 User::op_iterator op_end() { return OperandTraits<CallBase>::op_end(this); }
1494 User::const_op_iterator op_end() const {
1495 return OperandTraits<CallBase>::op_end(const_cast<CallBase *>(this));
1498 Value *getOperand(unsigned i_nocapture) const {
1499 assert(i_nocapture < OperandTraits<CallBase>::operands(this) &&
1500 "getOperand() out of range!");
1501 return cast_or_null<Value>(OperandTraits<CallBase>::op_begin(
1502 const_cast<CallBase *>(this))[i_nocapture]
1506 void setOperand(unsigned i_nocapture, Value *Val_nocapture) {
1507 assert(i_nocapture < OperandTraits<CallBase>::operands(this) &&
1508 "setOperand() out of range!");
1509 OperandTraits<CallBase>::op_begin(this)[i_nocapture] = Val_nocapture;
1512 unsigned getNumOperands() const {
1513 return OperandTraits<CallBase>::operands(this);
1515 template <int Idx_nocapture> Use &Op() {
1516 return User::OpFrom<Idx_nocapture>(this);
1518 template <int Idx_nocapture> const Use &Op() const {
1519 return User::OpFrom<Idx_nocapture>(this);
1522 /// Return the function called, or null if this is an
1523 /// indirect function invocation.
1525 Function *getCalledFunction() const {
1526 return dyn_cast<Function>(Op<-InstTy::ArgOffset>());
1529 /// Determine whether this call has the given attribute.
1530 bool hasFnAttr(Attribute::AttrKind Kind) const {
1531 assert(Kind != Attribute::NoBuiltin &&
1532 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
1533 return hasFnAttrImpl(Kind);
1536 /// Determine whether this call has the given attribute.
1537 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1539 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1541 CallingConv::ID getCallingConv() const {
1542 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1544 void setCallingConv(CallingConv::ID CC) {
1545 auto ID = static_cast<unsigned>(CC);
1546 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1547 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1552 /// adds the attribute to the list of attributes.
1553 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
1554 AttributeList PAL = getAttributes();
1555 PAL = PAL.addAttribute(getContext(), i, Kind);
1559 /// adds the attribute to the list of attributes.
1560 void addAttribute(unsigned i, Attribute Attr) {
1561 AttributeList PAL = getAttributes();
1562 PAL = PAL.addAttribute(getContext(), i, Attr);
1566 /// Adds the attribute to the indicated argument
1567 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1568 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1569 AttributeList PAL = getAttributes();
1570 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
1574 /// Adds the attribute to the indicated argument
1575 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1576 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1577 AttributeList PAL = getAttributes();
1578 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
1582 /// removes the attribute from the list of attributes.
1583 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
1584 AttributeList PAL = getAttributes();
1585 PAL = PAL.removeAttribute(getContext(), i, Kind);
1589 /// removes the attribute from the list of attributes.
1590 void removeAttribute(unsigned i, StringRef Kind) {
1591 AttributeList PAL = getAttributes();
1592 PAL = PAL.removeAttribute(getContext(), i, Kind);
1596 /// Removes the attribute from the given argument
1597 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1598 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1599 AttributeList PAL = getAttributes();
1600 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1604 /// Removes the attribute from the given argument
1605 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1606 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1607 AttributeList PAL = getAttributes();
1608 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1612 /// adds the dereferenceable attribute to the list of attributes.
1613 void addDereferenceableAttr(unsigned i, uint64_t Bytes) {
1614 AttributeList PAL = getAttributes();
1615 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
1619 /// adds the dereferenceable_or_null attribute to the list of
1621 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
1622 AttributeList PAL = getAttributes();
1623 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
1627 /// Determine whether the return value has the given attribute.
1628 bool hasRetAttr(Attribute::AttrKind Kind) const {
1629 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
1632 // Look at the callee, if available.
1633 if (const Function *F = getCalledFunction())
1634 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
1638 /// Determine whether the argument or parameter has the given attribute.
1639 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1640 assert(ArgNo < getNumArgOperands() && "Param index out of bounds!");
1642 if (Attrs.hasParamAttribute(ArgNo, Kind))
1644 if (const Function *F = getCalledFunction())
1645 return F->getAttributes().hasParamAttribute(ArgNo, Kind);
1649 /// Get the attribute of a given kind at a position.
1650 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1651 return getAttributes().getAttribute(i, Kind);
1654 /// Get the attribute of a given kind at a position.
1655 Attribute getAttribute(unsigned i, StringRef Kind) const {
1656 return getAttributes().getAttribute(i, Kind);
1659 /// Get the attribute of a given kind from a given arg
1660 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1661 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1662 return getAttributes().getParamAttr(ArgNo, Kind);
1665 /// Get the attribute of a given kind from a given arg
1666 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1667 assert(ArgNo < getNumArgOperands() && "Out of bounds");
1668 return getAttributes().getParamAttr(ArgNo, Kind);
1670 /// Return true if the data operand at index \p i has the attribute \p
1673 /// Data operands include call arguments and values used in operand bundles,
1674 /// but does not include the callee operand. This routine dispatches to the
1675 /// underlying AttributeList or the OperandBundleUser as appropriate.
1677 /// The index \p i is interpreted as
1679 /// \p i == Attribute::ReturnIndex -> the return value
1680 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1681 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1682 /// (\p i - 1) in the operand list.
1683 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1684 // There are getNumOperands() - (InstTy::ArgOffset - 1) data operands.
1685 // The last operand is the callee.
1686 assert(i < (getNumOperands() - InstTy::ArgOffset + 1) &&
1687 "Data operand index out of bounds!");
1689 // The attribute A can either be directly specified, if the operand in
1690 // question is a call argument; or be indirectly implied by the kind of its
1691 // containing operand bundle, if the operand is a bundle operand.
1693 if (i == AttributeList::ReturnIndex)
1694 return hasRetAttr(Kind);
1696 // FIXME: Avoid these i - 1 calculations and update the API to use
1697 // zero-based indices.
1698 if (i < (getNumArgOperands() + 1))
1699 return paramHasAttr(i - 1, Kind);
1701 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
1702 "Must be either a call argument or an operand bundle!");
1703 return bundleOperandHasAttr(i - 1, Kind);
1706 /// Extract the alignment of the return value.
1707 unsigned getRetAlignment() const { return Attrs.getRetAlignment(); }
1709 /// Extract the alignment for a call or parameter (0=unknown).
1710 unsigned getParamAlignment(unsigned ArgNo) const {
1711 return Attrs.getParamAlignment(ArgNo);
1714 /// Extract the number of dereferenceable bytes for a call or
1715 /// parameter (0=unknown).
1716 uint64_t getDereferenceableBytes(unsigned i) const {
1717 return Attrs.getDereferenceableBytes(i);
1720 /// Extract the number of dereferenceable_or_null bytes for a call or
1721 /// parameter (0=unknown).
1722 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1723 return Attrs.getDereferenceableOrNullBytes(i);
1726 /// Determine if the return value is marked with NoAlias attribute.
1727 bool returnDoesNotAlias() const {
1728 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1731 /// Return true if the call should not be treated as a call to a
1733 bool isNoBuiltin() const {
1734 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1735 !hasFnAttrImpl(Attribute::Builtin);
1738 /// Determine if the call requires strict floating point semantics.
1739 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1741 /// Return true if the call should not be inlined.
1742 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1743 void setIsNoInline() {
1744 addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1746 /// Determine if the call does not access memory.
1747 bool doesNotAccessMemory() const {
1748 return hasFnAttr(Attribute::ReadNone);
1750 void setDoesNotAccessMemory() {
1751 addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
1754 /// Determine if the call does not access or only reads memory.
1755 bool onlyReadsMemory() const {
1756 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1758 void setOnlyReadsMemory() {
1759 addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
1762 /// Determine if the call does not access or only writes memory.
1763 bool doesNotReadMemory() const {
1764 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1766 void setDoesNotReadMemory() {
1767 addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
1770 /// Determine if the call can access memmory only using pointers based
1771 /// on its arguments.
1772 bool onlyAccessesArgMemory() const {
1773 return hasFnAttr(Attribute::ArgMemOnly);
1775 void setOnlyAccessesArgMemory() {
1776 addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly);
1779 /// Determine if the function may only access memory that is
1780 /// inaccessible from the IR.
1781 bool onlyAccessesInaccessibleMemory() const {
1782 return hasFnAttr(Attribute::InaccessibleMemOnly);
1784 void setOnlyAccessesInaccessibleMemory() {
1785 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly);
1788 /// Determine if the function may only access memory that is
1789 /// either inaccessible from the IR or pointed to by its arguments.
1790 bool onlyAccessesInaccessibleMemOrArgMem() const {
1791 return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
1793 void setOnlyAccessesInaccessibleMemOrArgMem() {
1794 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOrArgMemOnly);
1796 /// Determine if the call cannot return.
1797 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1798 void setDoesNotReturn() {
1799 addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
1802 /// Determine if the call should not perform indirect branch tracking.
1803 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1805 /// Determine if the call cannot unwind.
1806 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1807 void setDoesNotThrow() {
1808 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1811 /// Determine if the invoke cannot be duplicated.
1812 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1813 void setCannotDuplicate() {
1814 addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate);
1817 /// Determine if the invoke is convergent
1818 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1819 void setConvergent() {
1820 addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1822 void setNotConvergent() {
1823 removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1826 /// Determine if the call returns a structure through first
1827 /// pointer argument.
1828 bool hasStructRetAttr() const {
1829 if (getNumArgOperands() == 0)
1832 // Be friendly and also check the callee.
1833 return paramHasAttr(0, Attribute::StructRet);
1836 /// Determine if any call argument is an aggregate passed by value.
1837 bool hasByValArgument() const {
1838 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1840 /// Get a pointer to the function that is invoked by this
1842 const Value *getCalledValue() const { return Op<-InstTy::ArgOffset>(); }
1843 Value *getCalledValue() { return Op<-InstTy::ArgOffset>(); }
1845 /// Set the function called.
1846 void setCalledFunction(Value* Fn) {
1848 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1851 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1853 assert(FTy == cast<FunctionType>(
1854 cast<PointerType>(Fn->getType())->getElementType()));
1855 Op<-InstTy::ArgOffset>() = Fn;
1859 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
1860 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Kind))
1863 // Operand bundles override attributes on the called function, but don't
1864 // override attributes directly present on the call instruction.
1865 if (isFnAttrDisallowedByOpBundle(Kind))
1868 if (const Function *F = getCalledFunction())
1869 return F->getAttributes().hasAttribute(AttributeList::FunctionIndex,
1875 //===----------------------------------------------------------------------===//
1876 /// This class represents a function call, abstracting a target
1877 /// machine's calling convention. This class uses low bit of the SubClassData
1878 /// field to indicate whether or not this is a tail call. The rest of the bits
1879 /// hold the calling convention of the call.
1881 class CallInst : public CallBase<CallInst> {
1882 friend class OperandBundleUser<CallInst, User::op_iterator>;
1884 CallInst(const CallInst &CI);
1886 /// Construct a CallInst given a range of arguments.
1887 /// Construct a CallInst from a range of arguments
1888 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1889 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1890 Instruction *InsertBefore);
1892 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1893 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1894 Instruction *InsertBefore)
1895 : CallInst(cast<FunctionType>(
1896 cast<PointerType>(Func->getType())->getElementType()),
1897 Func, Args, Bundles, NameStr, InsertBefore) {}
1899 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1900 Instruction *InsertBefore)
1901 : CallInst(Func, Args, None, NameStr, InsertBefore) {}
1903 /// Construct a CallInst given a range of arguments.
1904 /// Construct a CallInst from a range of arguments
1905 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1906 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1907 BasicBlock *InsertAtEnd);
1909 explicit CallInst(Value *F, const Twine &NameStr, Instruction *InsertBefore);
1911 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1913 void init(Value *Func, ArrayRef<Value *> Args,
1914 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
1915 init(cast<FunctionType>(
1916 cast<PointerType>(Func->getType())->getElementType()),
1917 Func, Args, Bundles, NameStr);
1919 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1920 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1921 void init(Value *Func, const Twine &NameStr);
1924 // Note: Instruction needs to be a friend here to call cloneImpl.
1925 friend class Instruction;
1927 CallInst *cloneImpl() const;
1930 static constexpr int ArgOffset = 1;
1932 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1933 ArrayRef<OperandBundleDef> Bundles = None,
1934 const Twine &NameStr = "",
1935 Instruction *InsertBefore = nullptr) {
1936 return Create(cast<FunctionType>(
1937 cast<PointerType>(Func->getType())->getElementType()),
1938 Func, Args, Bundles, NameStr, InsertBefore);
1941 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1942 const Twine &NameStr,
1943 Instruction *InsertBefore = nullptr) {
1944 return Create(cast<FunctionType>(
1945 cast<PointerType>(Func->getType())->getElementType()),
1946 Func, Args, None, NameStr, InsertBefore);
1949 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1950 const Twine &NameStr,
1951 Instruction *InsertBefore = nullptr) {
1952 return new (unsigned(Args.size() + 1))
1953 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1956 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1957 ArrayRef<OperandBundleDef> Bundles = None,
1958 const Twine &NameStr = "",
1959 Instruction *InsertBefore = nullptr) {
1960 const unsigned TotalOps =
1961 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1962 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1964 return new (TotalOps, DescriptorBytes)
1965 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1968 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1969 ArrayRef<OperandBundleDef> Bundles,
1970 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1971 const unsigned TotalOps =
1972 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1973 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1975 return new (TotalOps, DescriptorBytes)
1976 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
1979 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1980 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1981 return new (unsigned(Args.size() + 1))
1982 CallInst(Func, Args, None, NameStr, InsertAtEnd);
1985 static CallInst *Create(Value *F, const Twine &NameStr = "",
1986 Instruction *InsertBefore = nullptr) {
1987 return new (1) CallInst(F, NameStr, InsertBefore);
1990 static CallInst *Create(Value *F, const Twine &NameStr,
1991 BasicBlock *InsertAtEnd) {
1992 return new (1) CallInst(F, NameStr, InsertAtEnd);
1995 /// Create a clone of \p CI with a different set of operand bundles and
1996 /// insert it before \p InsertPt.
1998 /// The returned call instruction is identical \p CI in every way except that
1999 /// the operand bundles for the new instruction are set to the operand bundles
2001 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
2002 Instruction *InsertPt = nullptr);
2004 /// Generate the IR for a call to malloc:
2005 /// 1. Compute the malloc call's argument as the specified type's size,
2006 /// possibly multiplied by the array size if the array size is not
2008 /// 2. Call malloc with that argument.
2009 /// 3. Bitcast the result of the malloc call to the specified type.
2010 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
2011 Type *AllocTy, Value *AllocSize,
2012 Value *ArraySize = nullptr,
2013 Function *MallocF = nullptr,
2014 const Twine &Name = "");
2015 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
2016 Type *AllocTy, Value *AllocSize,
2017 Value *ArraySize = nullptr,
2018 Function *MallocF = nullptr,
2019 const Twine &Name = "");
2020 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
2021 Type *AllocTy, Value *AllocSize,
2022 Value *ArraySize = nullptr,
2023 ArrayRef<OperandBundleDef> Bundles = None,
2024 Function *MallocF = nullptr,
2025 const Twine &Name = "");
2026 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
2027 Type *AllocTy, Value *AllocSize,
2028 Value *ArraySize = nullptr,
2029 ArrayRef<OperandBundleDef> Bundles = None,
2030 Function *MallocF = nullptr,
2031 const Twine &Name = "");
2032 /// Generate the IR for a call to the builtin free function.
2033 static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
2034 static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
2035 static Instruction *CreateFree(Value *Source,
2036 ArrayRef<OperandBundleDef> Bundles,
2037 Instruction *InsertBefore);
2038 static Instruction *CreateFree(Value *Source,
2039 ArrayRef<OperandBundleDef> Bundles,
2040 BasicBlock *InsertAtEnd);
2042 // Note that 'musttail' implies 'tail'.
2049 TailCallKind getTailCallKind() const {
2050 return TailCallKind(getSubclassDataFromInstruction() & 3);
2053 bool isTailCall() const {
2054 unsigned Kind = getSubclassDataFromInstruction() & 3;
2055 return Kind == TCK_Tail || Kind == TCK_MustTail;
2058 bool isMustTailCall() const {
2059 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
2062 bool isNoTailCall() const {
2063 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
2066 void setTailCall(bool isTC = true) {
2067 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
2068 unsigned(isTC ? TCK_Tail : TCK_None));
2071 void setTailCallKind(TailCallKind TCK) {
2072 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
2076 /// Return true if the call can return twice
2077 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
2078 void setCanReturnTwice() {
2079 addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice);
2082 /// Check if this call is an inline asm statement.
2083 bool isInlineAsm() const { return isa<InlineAsm>(Op<-1>()); }
2085 // Methods for support type inquiry through isa, cast, and dyn_cast:
2086 static bool classof(const Instruction *I) {
2087 return I->getOpcode() == Instruction::Call;
2089 static bool classof(const Value *V) {
2090 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2094 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2095 // method so that subclasses cannot accidentally use it.
2096 void setInstructionSubclassData(unsigned short D) {
2097 Instruction::setInstructionSubclassData(D);
2102 struct OperandTraits<CallBase<CallInst>>
2103 : public VariadicOperandTraits<CallBase<CallInst>, 1> {};
2105 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
2106 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
2107 BasicBlock *InsertAtEnd)
2108 : CallBase<CallInst>(
2110 cast<PointerType>(Func->getType())->getElementType())
2113 OperandTraits<CallBase<CallInst>>::op_end(this) -
2114 (Args.size() + CountBundleInputs(Bundles) + 1),
2115 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
2116 init(Func, Args, Bundles, NameStr);
2119 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
2120 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
2121 Instruction *InsertBefore)
2122 : CallBase<CallInst>(Ty->getReturnType(), Instruction::Call,
2123 OperandTraits<CallBase<CallInst>>::op_end(this) -
2124 (Args.size() + CountBundleInputs(Bundles) + 1),
2125 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
2127 init(Ty, Func, Args, Bundles, NameStr);
2130 //===----------------------------------------------------------------------===//
2132 //===----------------------------------------------------------------------===//
2134 /// This class represents the LLVM 'select' instruction.
2136 class SelectInst : public Instruction {
2137 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
2138 Instruction *InsertBefore)
2139 : Instruction(S1->getType(), Instruction::Select,
2140 &Op<0>(), 3, InsertBefore) {
2145 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
2146 BasicBlock *InsertAtEnd)
2147 : Instruction(S1->getType(), Instruction::Select,
2148 &Op<0>(), 3, InsertAtEnd) {
2153 void init(Value *C, Value *S1, Value *S2) {
2154 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
2161 // Note: Instruction needs to be a friend here to call cloneImpl.
2162 friend class Instruction;
2164 SelectInst *cloneImpl() const;
2167 static SelectInst *Create(Value *C, Value *S1, Value *S2,
2168 const Twine &NameStr = "",
2169 Instruction *InsertBefore = nullptr,
2170 Instruction *MDFrom = nullptr) {
2171 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
2173 Sel->copyMetadata(*MDFrom);
2177 static SelectInst *Create(Value *C, Value *S1, Value *S2,
2178 const Twine &NameStr,
2179 BasicBlock *InsertAtEnd) {
2180 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
2183 const Value *getCondition() const { return Op<0>(); }
2184 const Value *getTrueValue() const { return Op<1>(); }
2185 const Value *getFalseValue() const { return Op<2>(); }
2186 Value *getCondition() { return Op<0>(); }
2187 Value *getTrueValue() { return Op<1>(); }
2188 Value *getFalseValue() { return Op<2>(); }
2190 void setCondition(Value *V) { Op<0>() = V; }
2191 void setTrueValue(Value *V) { Op<1>() = V; }
2192 void setFalseValue(Value *V) { Op<2>() = V; }
2194 /// Return a string if the specified operands are invalid
2195 /// for a select operation, otherwise return null.
2196 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
2198 /// Transparently provide more efficient getOperand methods.
2199 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2201 OtherOps getOpcode() const {
2202 return static_cast<OtherOps>(Instruction::getOpcode());
2205 // Methods for support type inquiry through isa, cast, and dyn_cast:
2206 static bool classof(const Instruction *I) {
2207 return I->getOpcode() == Instruction::Select;
2209 static bool classof(const Value *V) {
2210 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2215 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
2218 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
2220 //===----------------------------------------------------------------------===//
2222 //===----------------------------------------------------------------------===//
2224 /// This class represents the va_arg llvm instruction, which returns
2225 /// an argument of the specified type given a va_list and increments that list
2227 class VAArgInst : public UnaryInstruction {
2229 // Note: Instruction needs to be a friend here to call cloneImpl.
2230 friend class Instruction;
2232 VAArgInst *cloneImpl() const;
2235 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
2236 Instruction *InsertBefore = nullptr)
2237 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
2241 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
2242 BasicBlock *InsertAtEnd)
2243 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
2247 Value *getPointerOperand() { return getOperand(0); }
2248 const Value *getPointerOperand() const { return getOperand(0); }
2249 static unsigned getPointerOperandIndex() { return 0U; }
2251 // Methods for support type inquiry through isa, cast, and dyn_cast:
2252 static bool classof(const Instruction *I) {
2253 return I->getOpcode() == VAArg;
2255 static bool classof(const Value *V) {
2256 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2260 //===----------------------------------------------------------------------===//
2261 // ExtractElementInst Class
2262 //===----------------------------------------------------------------------===//
2264 /// This instruction extracts a single (scalar)
2265 /// element from a VectorType value
2267 class ExtractElementInst : public Instruction {
2268 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
2269 Instruction *InsertBefore = nullptr);
2270 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
2271 BasicBlock *InsertAtEnd);
2274 // Note: Instruction needs to be a friend here to call cloneImpl.
2275 friend class Instruction;
2277 ExtractElementInst *cloneImpl() const;
2280 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2281 const Twine &NameStr = "",
2282 Instruction *InsertBefore = nullptr) {
2283 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
2286 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2287 const Twine &NameStr,
2288 BasicBlock *InsertAtEnd) {
2289 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
2292 /// Return true if an extractelement instruction can be
2293 /// formed with the specified operands.
2294 static bool isValidOperands(const Value *Vec, const Value *Idx);
2296 Value *getVectorOperand() { return Op<0>(); }
2297 Value *getIndexOperand() { return Op<1>(); }
2298 const Value *getVectorOperand() const { return Op<0>(); }
2299 const Value *getIndexOperand() const { return Op<1>(); }
2301 VectorType *getVectorOperandType() const {
2302 return cast<VectorType>(getVectorOperand()->getType());
2305 /// Transparently provide more efficient getOperand methods.
2306 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2308 // Methods for support type inquiry through isa, cast, and dyn_cast:
2309 static bool classof(const Instruction *I) {
2310 return I->getOpcode() == Instruction::ExtractElement;
2312 static bool classof(const Value *V) {
2313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2318 struct OperandTraits<ExtractElementInst> :
2319 public FixedNumOperandTraits<ExtractElementInst, 2> {
2322 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
2324 //===----------------------------------------------------------------------===//
2325 // InsertElementInst Class
2326 //===----------------------------------------------------------------------===//
2328 /// This instruction inserts a single (scalar)
2329 /// element into a VectorType value
2331 class InsertElementInst : public Instruction {
2332 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
2333 const Twine &NameStr = "",
2334 Instruction *InsertBefore = nullptr);
2335 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
2336 BasicBlock *InsertAtEnd);
2339 // Note: Instruction needs to be a friend here to call cloneImpl.
2340 friend class Instruction;
2342 InsertElementInst *cloneImpl() const;
2345 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2346 const Twine &NameStr = "",
2347 Instruction *InsertBefore = nullptr) {
2348 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2351 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2352 const Twine &NameStr,
2353 BasicBlock *InsertAtEnd) {
2354 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2357 /// Return true if an insertelement instruction can be
2358 /// formed with the specified operands.
2359 static bool isValidOperands(const Value *Vec, const Value *NewElt,
2362 /// Overload to return most specific vector type.
2364 VectorType *getType() const {
2365 return cast<VectorType>(Instruction::getType());
2368 /// Transparently provide more efficient getOperand methods.
2369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2371 // Methods for support type inquiry through isa, cast, and dyn_cast:
2372 static bool classof(const Instruction *I) {
2373 return I->getOpcode() == Instruction::InsertElement;
2375 static bool classof(const Value *V) {
2376 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2381 struct OperandTraits<InsertElementInst> :
2382 public FixedNumOperandTraits<InsertElementInst, 3> {
2385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2387 //===----------------------------------------------------------------------===//
2388 // ShuffleVectorInst Class
2389 //===----------------------------------------------------------------------===//
2391 /// This instruction constructs a fixed permutation of two
2394 class ShuffleVectorInst : public Instruction {
2396 // Note: Instruction needs to be a friend here to call cloneImpl.
2397 friend class Instruction;
2399 ShuffleVectorInst *cloneImpl() const;
2402 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2403 const Twine &NameStr = "",
2404 Instruction *InsertBefor = nullptr);
2405 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2406 const Twine &NameStr, BasicBlock *InsertAtEnd);
2408 // allocate space for exactly three operands
2409 void *operator new(size_t s) {
2410 return User::operator new(s, 3);
2413 /// Return true if a shufflevector instruction can be
2414 /// formed with the specified operands.
2415 static bool isValidOperands(const Value *V1, const Value *V2,
2418 /// Overload to return most specific vector type.
2420 VectorType *getType() const {
2421 return cast<VectorType>(Instruction::getType());
2424 /// Transparently provide more efficient getOperand methods.
2425 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2427 Constant *getMask() const {
2428 return cast<Constant>(getOperand(2));
2431 /// Return the shuffle mask value for the specified element of the mask.
2432 /// Return -1 if the element is undef.
2433 static int getMaskValue(const Constant *Mask, unsigned Elt);
2435 /// Return the shuffle mask value of this instruction for the given element
2436 /// index. Return -1 if the element is undef.
2437 int getMaskValue(unsigned Elt) const {
2438 return getMaskValue(getMask(), Elt);
2441 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2442 /// elements of the mask are returned as -1.
2443 static void getShuffleMask(const Constant *Mask,
2444 SmallVectorImpl<int> &Result);
2446 /// Return the mask for this instruction as a vector of integers. Undefined
2447 /// elements of the mask are returned as -1.
2448 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2449 return getShuffleMask(getMask(), Result);
2452 SmallVector<int, 16> getShuffleMask() const {
2453 SmallVector<int, 16> Mask;
2454 getShuffleMask(Mask);
2458 /// Return true if this shuffle returns a vector with a different number of
2459 /// elements than its source elements.
2460 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2>
2461 bool changesLength() const {
2462 unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements();
2463 unsigned NumMaskElts = getMask()->getType()->getVectorNumElements();
2464 return NumSourceElts != NumMaskElts;
2467 /// Return true if this shuffle mask chooses elements from exactly one source
2469 /// Example: <7,5,undef,7>
2470 /// This assumes that vector operands are the same length as the mask.
2471 static bool isSingleSourceMask(ArrayRef<int> Mask);
2472 static bool isSingleSourceMask(const Constant *Mask) {
2473 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2474 SmallVector<int, 16> MaskAsInts;
2475 getShuffleMask(Mask, MaskAsInts);
2476 return isSingleSourceMask(MaskAsInts);
2479 /// Return true if this shuffle chooses elements from exactly one source
2480 /// vector without changing the length of that vector.
2481 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2482 /// TODO: Optionally allow length-changing shuffles.
2483 bool isSingleSource() const {
2484 return !changesLength() && isSingleSourceMask(getMask());
2487 /// Return true if this shuffle mask chooses elements from exactly one source
2488 /// vector without lane crossings. A shuffle using this mask is not
2489 /// necessarily a no-op because it may change the number of elements from its
2490 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2491 /// Example: <undef,undef,2,3>
2492 static bool isIdentityMask(ArrayRef<int> Mask);
2493 static bool isIdentityMask(const Constant *Mask) {
2494 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2495 SmallVector<int, 16> MaskAsInts;
2496 getShuffleMask(Mask, MaskAsInts);
2497 return isIdentityMask(MaskAsInts);
2500 /// Return true if this shuffle mask chooses elements from exactly one source
2501 /// vector without lane crossings and does not change the number of elements
2502 /// from its input vectors.
2503 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2504 /// TODO: Optionally allow length-changing shuffles.
2505 bool isIdentity() const {
2506 return !changesLength() && isIdentityMask(getShuffleMask());
2509 /// Return true if this shuffle mask chooses elements from its source vectors
2510 /// without lane crossings. A shuffle using this mask would be
2511 /// equivalent to a vector select with a constant condition operand.
2512 /// Example: <4,1,6,undef>
2513 /// This returns false if the mask does not choose from both input vectors.
2514 /// In that case, the shuffle is better classified as an identity shuffle.
2515 /// This assumes that vector operands are the same length as the mask
2516 /// (a length-changing shuffle can never be equivalent to a vector select).
2517 static bool isSelectMask(ArrayRef<int> Mask);
2518 static bool isSelectMask(const Constant *Mask) {
2519 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2520 SmallVector<int, 16> MaskAsInts;
2521 getShuffleMask(Mask, MaskAsInts);
2522 return isSelectMask(MaskAsInts);
2525 /// Return true if this shuffle chooses elements from its source vectors
2526 /// without lane crossings and all operands have the same number of elements.
2527 /// In other words, this shuffle is equivalent to a vector select with a
2528 /// constant condition operand.
2529 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2530 /// This returns false if the mask does not choose from both input vectors.
2531 /// In that case, the shuffle is better classified as an identity shuffle.
2532 /// TODO: Optionally allow length-changing shuffles.
2533 bool isSelect() const {
2534 return !changesLength() && isSelectMask(getMask());
2537 /// Return true if this shuffle mask swaps the order of elements from exactly
2538 /// one source vector.
2539 /// Example: <7,6,undef,4>
2540 /// This assumes that vector operands are the same length as the mask.
2541 static bool isReverseMask(ArrayRef<int> Mask);
2542 static bool isReverseMask(const Constant *Mask) {
2543 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2544 SmallVector<int, 16> MaskAsInts;
2545 getShuffleMask(Mask, MaskAsInts);
2546 return isReverseMask(MaskAsInts);
2549 /// Return true if this shuffle swaps the order of elements from exactly
2550 /// one source vector.
2551 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2552 /// TODO: Optionally allow length-changing shuffles.
2553 bool isReverse() const {
2554 return !changesLength() && isReverseMask(getMask());
2557 /// Return true if this shuffle mask chooses all elements with the same value
2558 /// as the first element of exactly one source vector.
2559 /// Example: <4,undef,undef,4>
2560 /// This assumes that vector operands are the same length as the mask.
2561 static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2562 static bool isZeroEltSplatMask(const Constant *Mask) {
2563 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2564 SmallVector<int, 16> MaskAsInts;
2565 getShuffleMask(Mask, MaskAsInts);
2566 return isZeroEltSplatMask(MaskAsInts);
2569 /// Return true if all elements of this shuffle are the same value as the
2570 /// first element of exactly one source vector without changing the length
2572 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2573 /// TODO: Optionally allow length-changing shuffles.
2574 /// TODO: Optionally allow splats from other elements.
2575 bool isZeroEltSplat() const {
2576 return !changesLength() && isZeroEltSplatMask(getMask());
2579 /// Return true if this shuffle mask is a transpose mask.
2580 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2581 /// even- or odd-numbered vector elements from two n-dimensional source
2582 /// vectors and write each result into consecutive elements of an
2583 /// n-dimensional destination vector. Two shuffles are necessary to complete
2584 /// the transpose, one for the even elements and another for the odd elements.
2585 /// This description closely follows how the TRN1 and TRN2 AArch64
2586 /// instructions operate.
2588 /// For example, a simple 2x2 matrix can be transposed with:
2590 /// ; Original matrix
2594 /// ; Transposed matrix
2595 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2596 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2598 /// For matrices having greater than n columns, the resulting nx2 transposed
2599 /// matrix is stored in two result vectors such that one vector contains
2600 /// interleaved elements from all the even-numbered rows and the other vector
2601 /// contains interleaved elements from all the odd-numbered rows. For example,
2602 /// a 2x4 matrix can be transposed with:
2604 /// ; Original matrix
2605 /// m0 = < a, b, c, d >
2606 /// m1 = < e, f, g, h >
2608 /// ; Transposed matrix
2609 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2610 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2611 static bool isTransposeMask(ArrayRef<int> Mask);
2612 static bool isTransposeMask(const Constant *Mask) {
2613 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2614 SmallVector<int, 16> MaskAsInts;
2615 getShuffleMask(Mask, MaskAsInts);
2616 return isTransposeMask(MaskAsInts);
2619 /// Return true if this shuffle transposes the elements of its inputs without
2620 /// changing the length of the vectors. This operation may also be known as a
2621 /// merge or interleave. See the description for isTransposeMask() for the
2622 /// exact specification.
2623 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2624 bool isTranspose() const {
2625 return !changesLength() && isTransposeMask(getMask());
2628 /// Change values in a shuffle permute mask assuming the two vector operands
2629 /// of length InVecNumElts have swapped position.
2630 static void commuteShuffleMask(MutableArrayRef<int> Mask,
2631 unsigned InVecNumElts) {
2632 for (int &Idx : Mask) {
2635 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2636 assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&
2637 "shufflevector mask index out of range");
2641 // Methods for support type inquiry through isa, cast, and dyn_cast:
2642 static bool classof(const Instruction *I) {
2643 return I->getOpcode() == Instruction::ShuffleVector;
2645 static bool classof(const Value *V) {
2646 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2651 struct OperandTraits<ShuffleVectorInst> :
2652 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2655 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2657 //===----------------------------------------------------------------------===//
2658 // ExtractValueInst Class
2659 //===----------------------------------------------------------------------===//
2661 /// This instruction extracts a struct member or array
2662 /// element value from an aggregate value.
2664 class ExtractValueInst : public UnaryInstruction {
2665 SmallVector<unsigned, 4> Indices;
2667 ExtractValueInst(const ExtractValueInst &EVI);
2669 /// Constructors - Create a extractvalue instruction with a base aggregate
2670 /// value and a list of indices. The first ctor can optionally insert before
2671 /// an existing instruction, the second appends the new instruction to the
2672 /// specified BasicBlock.
2673 inline ExtractValueInst(Value *Agg,
2674 ArrayRef<unsigned> Idxs,
2675 const Twine &NameStr,
2676 Instruction *InsertBefore);
2677 inline ExtractValueInst(Value *Agg,
2678 ArrayRef<unsigned> Idxs,
2679 const Twine &NameStr, BasicBlock *InsertAtEnd);
2681 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2684 // Note: Instruction needs to be a friend here to call cloneImpl.
2685 friend class Instruction;
2687 ExtractValueInst *cloneImpl() const;
2690 static ExtractValueInst *Create(Value *Agg,
2691 ArrayRef<unsigned> Idxs,
2692 const Twine &NameStr = "",
2693 Instruction *InsertBefore = nullptr) {
2695 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2698 static ExtractValueInst *Create(Value *Agg,
2699 ArrayRef<unsigned> Idxs,
2700 const Twine &NameStr,
2701 BasicBlock *InsertAtEnd) {
2702 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2705 /// Returns the type of the element that would be extracted
2706 /// with an extractvalue instruction with the specified parameters.
2708 /// Null is returned if the indices are invalid for the specified type.
2709 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2711 using idx_iterator = const unsigned*;
2713 inline idx_iterator idx_begin() const { return Indices.begin(); }
2714 inline idx_iterator idx_end() const { return Indices.end(); }
2715 inline iterator_range<idx_iterator> indices() const {
2716 return make_range(idx_begin(), idx_end());
2719 Value *getAggregateOperand() {
2720 return getOperand(0);
2722 const Value *getAggregateOperand() const {
2723 return getOperand(0);
2725 static unsigned getAggregateOperandIndex() {
2726 return 0U; // get index for modifying correct operand
2729 ArrayRef<unsigned> getIndices() const {
2733 unsigned getNumIndices() const {
2734 return (unsigned)Indices.size();
2737 bool hasIndices() const {
2741 // Methods for support type inquiry through isa, cast, and dyn_cast:
2742 static bool classof(const Instruction *I) {
2743 return I->getOpcode() == Instruction::ExtractValue;
2745 static bool classof(const Value *V) {
2746 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2750 ExtractValueInst::ExtractValueInst(Value *Agg,
2751 ArrayRef<unsigned> Idxs,
2752 const Twine &NameStr,
2753 Instruction *InsertBefore)
2754 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2755 ExtractValue, Agg, InsertBefore) {
2756 init(Idxs, NameStr);
2759 ExtractValueInst::ExtractValueInst(Value *Agg,
2760 ArrayRef<unsigned> Idxs,
2761 const Twine &NameStr,
2762 BasicBlock *InsertAtEnd)
2763 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2764 ExtractValue, Agg, InsertAtEnd) {
2765 init(Idxs, NameStr);
2768 //===----------------------------------------------------------------------===//
2769 // InsertValueInst Class
2770 //===----------------------------------------------------------------------===//
2772 /// This instruction inserts a struct field of array element
2773 /// value into an aggregate value.
2775 class InsertValueInst : public Instruction {
2776 SmallVector<unsigned, 4> Indices;
2778 InsertValueInst(const InsertValueInst &IVI);
2780 /// Constructors - Create a insertvalue instruction with a base aggregate
2781 /// value, a value to insert, and a list of indices. The first ctor can
2782 /// optionally insert before an existing instruction, the second appends
2783 /// the new instruction to the specified BasicBlock.
2784 inline InsertValueInst(Value *Agg, Value *Val,
2785 ArrayRef<unsigned> Idxs,
2786 const Twine &NameStr,
2787 Instruction *InsertBefore);
2788 inline InsertValueInst(Value *Agg, Value *Val,
2789 ArrayRef<unsigned> Idxs,
2790 const Twine &NameStr, BasicBlock *InsertAtEnd);
2792 /// Constructors - These two constructors are convenience methods because one
2793 /// and two index insertvalue instructions are so common.
2794 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2795 const Twine &NameStr = "",
2796 Instruction *InsertBefore = nullptr);
2797 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2798 BasicBlock *InsertAtEnd);
2800 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2801 const Twine &NameStr);
2804 // Note: Instruction needs to be a friend here to call cloneImpl.
2805 friend class Instruction;
2807 InsertValueInst *cloneImpl() const;
2810 // allocate space for exactly two operands
2811 void *operator new(size_t s) {
2812 return User::operator new(s, 2);
2815 static InsertValueInst *Create(Value *Agg, Value *Val,
2816 ArrayRef<unsigned> Idxs,
2817 const Twine &NameStr = "",
2818 Instruction *InsertBefore = nullptr) {
2819 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2822 static InsertValueInst *Create(Value *Agg, Value *Val,
2823 ArrayRef<unsigned> Idxs,
2824 const Twine &NameStr,
2825 BasicBlock *InsertAtEnd) {
2826 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2829 /// Transparently provide more efficient getOperand methods.
2830 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2832 using idx_iterator = const unsigned*;
2834 inline idx_iterator idx_begin() const { return Indices.begin(); }
2835 inline idx_iterator idx_end() const { return Indices.end(); }
2836 inline iterator_range<idx_iterator> indices() const {
2837 return make_range(idx_begin(), idx_end());
2840 Value *getAggregateOperand() {
2841 return getOperand(0);
2843 const Value *getAggregateOperand() const {
2844 return getOperand(0);
2846 static unsigned getAggregateOperandIndex() {
2847 return 0U; // get index for modifying correct operand
2850 Value *getInsertedValueOperand() {
2851 return getOperand(1);
2853 const Value *getInsertedValueOperand() const {
2854 return getOperand(1);
2856 static unsigned getInsertedValueOperandIndex() {
2857 return 1U; // get index for modifying correct operand
2860 ArrayRef<unsigned> getIndices() const {
2864 unsigned getNumIndices() const {
2865 return (unsigned)Indices.size();
2868 bool hasIndices() const {
2872 // Methods for support type inquiry through isa, cast, and dyn_cast:
2873 static bool classof(const Instruction *I) {
2874 return I->getOpcode() == Instruction::InsertValue;
2876 static bool classof(const Value *V) {
2877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2882 struct OperandTraits<InsertValueInst> :
2883 public FixedNumOperandTraits<InsertValueInst, 2> {
2886 InsertValueInst::InsertValueInst(Value *Agg,
2888 ArrayRef<unsigned> Idxs,
2889 const Twine &NameStr,
2890 Instruction *InsertBefore)
2891 : Instruction(Agg->getType(), InsertValue,
2892 OperandTraits<InsertValueInst>::op_begin(this),
2894 init(Agg, Val, Idxs, NameStr);
2897 InsertValueInst::InsertValueInst(Value *Agg,
2899 ArrayRef<unsigned> Idxs,
2900 const Twine &NameStr,
2901 BasicBlock *InsertAtEnd)
2902 : Instruction(Agg->getType(), InsertValue,
2903 OperandTraits<InsertValueInst>::op_begin(this),
2905 init(Agg, Val, Idxs, NameStr);
2908 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2910 //===----------------------------------------------------------------------===//
2912 //===----------------------------------------------------------------------===//
2914 // PHINode - The PHINode class is used to represent the magical mystical PHI
2915 // node, that can not exist in nature, but can be synthesized in a computer
2916 // scientist's overactive imagination.
2918 class PHINode : public Instruction {
2919 /// The number of operands actually allocated. NumOperands is
2920 /// the number actually in use.
2921 unsigned ReservedSpace;
2923 PHINode(const PHINode &PN);
2925 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2926 const Twine &NameStr = "",
2927 Instruction *InsertBefore = nullptr)
2928 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2929 ReservedSpace(NumReservedValues) {
2931 allocHungoffUses(ReservedSpace);
2934 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2935 BasicBlock *InsertAtEnd)
2936 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2937 ReservedSpace(NumReservedValues) {
2939 allocHungoffUses(ReservedSpace);
2943 // Note: Instruction needs to be a friend here to call cloneImpl.
2944 friend class Instruction;
2946 PHINode *cloneImpl() const;
2948 // allocHungoffUses - this is more complicated than the generic
2949 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2950 // values and pointers to the incoming blocks, all in one allocation.
2951 void allocHungoffUses(unsigned N) {
2952 User::allocHungoffUses(N, /* IsPhi */ true);
2956 /// Constructors - NumReservedValues is a hint for the number of incoming
2957 /// edges that this phi node will have (use 0 if you really have no idea).
2958 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2959 const Twine &NameStr = "",
2960 Instruction *InsertBefore = nullptr) {
2961 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2964 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2965 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2966 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2969 /// Provide fast operand accessors
2970 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2972 // Block iterator interface. This provides access to the list of incoming
2973 // basic blocks, which parallels the list of incoming values.
2975 using block_iterator = BasicBlock **;
2976 using const_block_iterator = BasicBlock * const *;
2978 block_iterator block_begin() {
2980 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2981 return reinterpret_cast<block_iterator>(ref + 1);
2984 const_block_iterator block_begin() const {
2985 const Use::UserRef *ref =
2986 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2987 return reinterpret_cast<const_block_iterator>(ref + 1);
2990 block_iterator block_end() {
2991 return block_begin() + getNumOperands();
2994 const_block_iterator block_end() const {
2995 return block_begin() + getNumOperands();
2998 iterator_range<block_iterator> blocks() {
2999 return make_range(block_begin(), block_end());
3002 iterator_range<const_block_iterator> blocks() const {
3003 return make_range(block_begin(), block_end());
3006 op_range incoming_values() { return operands(); }
3008 const_op_range incoming_values() const { return operands(); }
3010 /// Return the number of incoming edges
3012 unsigned getNumIncomingValues() const { return getNumOperands(); }
3014 /// Return incoming value number x
3016 Value *getIncomingValue(unsigned i) const {
3017 return getOperand(i);
3019 void setIncomingValue(unsigned i, Value *V) {
3020 assert(V && "PHI node got a null value!");
3021 assert(getType() == V->getType() &&
3022 "All operands to PHI node must be the same type as the PHI node!");
3026 static unsigned getOperandNumForIncomingValue(unsigned i) {
3030 static unsigned getIncomingValueNumForOperand(unsigned i) {
3034 /// Return incoming basic block number @p i.
3036 BasicBlock *getIncomingBlock(unsigned i) const {
3037 return block_begin()[i];
3040 /// Return incoming basic block corresponding
3041 /// to an operand of the PHI.
3043 BasicBlock *getIncomingBlock(const Use &U) const {
3044 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
3045 return getIncomingBlock(unsigned(&U - op_begin()));
3048 /// Return incoming basic block corresponding
3049 /// to value use iterator.
3051 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
3052 return getIncomingBlock(I.getUse());
3055 void setIncomingBlock(unsigned i, BasicBlock *BB) {
3056 assert(BB && "PHI node got a null basic block!");
3057 block_begin()[i] = BB;
3060 /// Add an incoming value to the end of the PHI list
3062 void addIncoming(Value *V, BasicBlock *BB) {
3063 if (getNumOperands() == ReservedSpace)
3064 growOperands(); // Get more space!
3065 // Initialize some new operands.
3066 setNumHungOffUseOperands(getNumOperands() + 1);
3067 setIncomingValue(getNumOperands() - 1, V);
3068 setIncomingBlock(getNumOperands() - 1, BB);
3071 /// Remove an incoming value. This is useful if a
3072 /// predecessor basic block is deleted. The value removed is returned.
3074 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
3075 /// is true), the PHI node is destroyed and any uses of it are replaced with
3076 /// dummy values. The only time there should be zero incoming values to a PHI
3077 /// node is when the block is dead, so this strategy is sound.
3079 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
3081 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
3082 int Idx = getBasicBlockIndex(BB);
3083 assert(Idx >= 0 && "Invalid basic block argument to remove!");
3084 return removeIncomingValue(Idx, DeletePHIIfEmpty);
3087 /// Return the first index of the specified basic
3088 /// block in the value list for this PHI. Returns -1 if no instance.
3090 int getBasicBlockIndex(const BasicBlock *BB) const {
3091 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3092 if (block_begin()[i] == BB)
3097 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
3098 int Idx = getBasicBlockIndex(BB);
3099 assert(Idx >= 0 && "Invalid basic block argument!");
3100 return getIncomingValue(Idx);
3103 /// If the specified PHI node always merges together the
3104 /// same value, return the value, otherwise return null.
3105 Value *hasConstantValue() const;
3107 /// Whether the specified PHI node always merges
3108 /// together the same value, assuming undefs are equal to a unique
3109 /// non-undef value.
3110 bool hasConstantOrUndefValue() const;
3112 /// Methods for support type inquiry through isa, cast, and dyn_cast:
3113 static bool classof(const Instruction *I) {
3114 return I->getOpcode() == Instruction::PHI;
3116 static bool classof(const Value *V) {
3117 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3121 void growOperands();
3125 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
3128 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
3130 //===----------------------------------------------------------------------===//
3131 // LandingPadInst Class
3132 //===----------------------------------------------------------------------===//
3134 //===---------------------------------------------------------------------------
3135 /// The landingpad instruction holds all of the information
3136 /// necessary to generate correct exception handling. The landingpad instruction
3137 /// cannot be moved from the top of a landing pad block, which itself is
3138 /// accessible only from the 'unwind' edge of an invoke. This uses the
3139 /// SubclassData field in Value to store whether or not the landingpad is a
3142 class LandingPadInst : public Instruction {
3143 /// The number of operands actually allocated. NumOperands is
3144 /// the number actually in use.
3145 unsigned ReservedSpace;
3147 LandingPadInst(const LandingPadInst &LP);
3150 enum ClauseType { Catch, Filter };
3153 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
3154 const Twine &NameStr, Instruction *InsertBefore);
3155 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
3156 const Twine &NameStr, BasicBlock *InsertAtEnd);
3158 // Allocate space for exactly zero operands.
3159 void *operator new(size_t s) {
3160 return User::operator new(s);
3163 void growOperands(unsigned Size);
3164 void init(unsigned NumReservedValues, const Twine &NameStr);
3167 // Note: Instruction needs to be a friend here to call cloneImpl.
3168 friend class Instruction;
3170 LandingPadInst *cloneImpl() const;
3173 /// Constructors - NumReservedClauses is a hint for the number of incoming
3174 /// clauses that this landingpad will have (use 0 if you really have no idea).
3175 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
3176 const Twine &NameStr = "",
3177 Instruction *InsertBefore = nullptr);
3178 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
3179 const Twine &NameStr, BasicBlock *InsertAtEnd);
3181 /// Provide fast operand accessors
3182 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3184 /// Return 'true' if this landingpad instruction is a
3185 /// cleanup. I.e., it should be run when unwinding even if its landing pad
3186 /// doesn't catch the exception.
3187 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
3189 /// Indicate that this landingpad instruction is a cleanup.
3190 void setCleanup(bool V) {
3191 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
3195 /// Add a catch or filter clause to the landing pad.
3196 void addClause(Constant *ClauseVal);
3198 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
3199 /// determine what type of clause this is.
3200 Constant *getClause(unsigned Idx) const {
3201 return cast<Constant>(getOperandList()[Idx]);
3204 /// Return 'true' if the clause and index Idx is a catch clause.
3205 bool isCatch(unsigned Idx) const {
3206 return !isa<ArrayType>(getOperandList()[Idx]->getType());
3209 /// Return 'true' if the clause and index Idx is a filter clause.
3210 bool isFilter(unsigned Idx) const {
3211 return isa<ArrayType>(getOperandList()[Idx]->getType());
3214 /// Get the number of clauses for this landing pad.
3215 unsigned getNumClauses() const { return getNumOperands(); }
3217 /// Grow the size of the operand list to accommodate the new
3218 /// number of clauses.
3219 void reserveClauses(unsigned Size) { growOperands(Size); }
3221 // Methods for support type inquiry through isa, cast, and dyn_cast:
3222 static bool classof(const Instruction *I) {
3223 return I->getOpcode() == Instruction::LandingPad;
3225 static bool classof(const Value *V) {
3226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3231 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
3234 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
3236 //===----------------------------------------------------------------------===//
3238 //===----------------------------------------------------------------------===//
3240 //===---------------------------------------------------------------------------
3241 /// Return a value (possibly void), from a function. Execution
3242 /// does not continue in this function any longer.
3244 class ReturnInst : public TerminatorInst {
3245 ReturnInst(const ReturnInst &RI);
3248 // ReturnInst constructors:
3249 // ReturnInst() - 'ret void' instruction
3250 // ReturnInst( null) - 'ret void' instruction
3251 // ReturnInst(Value* X) - 'ret X' instruction
3252 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
3253 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
3254 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
3255 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
3257 // NOTE: If the Value* passed is of type void then the constructor behaves as
3258 // if it was passed NULL.
3259 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
3260 Instruction *InsertBefore = nullptr);
3261 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
3262 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3265 // Note: Instruction needs to be a friend here to call cloneImpl.
3266 friend class Instruction;
3268 ReturnInst *cloneImpl() const;
3271 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
3272 Instruction *InsertBefore = nullptr) {
3273 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
3276 static ReturnInst* Create(LLVMContext &C, Value *retVal,
3277 BasicBlock *InsertAtEnd) {
3278 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
3281 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
3282 return new(0) ReturnInst(C, InsertAtEnd);
3285 /// Provide fast operand accessors
3286 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3288 /// Convenience accessor. Returns null if there is no return value.
3289 Value *getReturnValue() const {
3290 return getNumOperands() != 0 ? getOperand(0) : nullptr;
3293 unsigned getNumSuccessors() const { return 0; }
3295 // Methods for support type inquiry through isa, cast, and dyn_cast:
3296 static bool classof(const Instruction *I) {
3297 return (I->getOpcode() == Instruction::Ret);
3299 static bool classof(const Value *V) {
3300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3304 friend TerminatorInst;
3306 BasicBlock *getSuccessor(unsigned idx) const {
3307 llvm_unreachable("ReturnInst has no successors!");
3310 void setSuccessor(unsigned idx, BasicBlock *B) {
3311 llvm_unreachable("ReturnInst has no successors!");
3316 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
3319 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
3321 //===----------------------------------------------------------------------===//
3323 //===----------------------------------------------------------------------===//
3325 //===---------------------------------------------------------------------------
3326 /// Conditional or Unconditional Branch instruction.
3328 class BranchInst : public TerminatorInst {
3329 /// Ops list - Branches are strange. The operands are ordered:
3330 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
3331 /// they don't have to check for cond/uncond branchness. These are mostly
3332 /// accessed relative from op_end().
3333 BranchInst(const BranchInst &BI);
3334 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
3335 // BranchInst(BB *B) - 'br B'
3336 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
3337 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
3338 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
3339 // BranchInst(BB* B, BB *I) - 'br B' insert at end
3340 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
3341 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
3342 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3343 Instruction *InsertBefore = nullptr);
3344 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
3345 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3346 BasicBlock *InsertAtEnd);
3351 // Note: Instruction needs to be a friend here to call cloneImpl.
3352 friend class Instruction;
3354 BranchInst *cloneImpl() const;
3357 static BranchInst *Create(BasicBlock *IfTrue,
3358 Instruction *InsertBefore = nullptr) {
3359 return new(1) BranchInst(IfTrue, InsertBefore);
3362 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3363 Value *Cond, Instruction *InsertBefore = nullptr) {
3364 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
3367 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
3368 return new(1) BranchInst(IfTrue, InsertAtEnd);
3371 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3372 Value *Cond, BasicBlock *InsertAtEnd) {
3373 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
3376 /// Transparently provide more efficient getOperand methods.
3377 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3379 bool isUnconditional() const { return getNumOperands() == 1; }
3380 bool isConditional() const { return getNumOperands() == 3; }
3382 Value *getCondition() const {
3383 assert(isConditional() && "Cannot get condition of an uncond branch!");
3387 void setCondition(Value *V) {
3388 assert(isConditional() && "Cannot set condition of unconditional branch!");
3392 unsigned getNumSuccessors() const { return 1+isConditional(); }
3394 BasicBlock *getSuccessor(unsigned i) const {
3395 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
3396 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3399 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3400 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
3401 *(&Op<-1>() - idx) = NewSucc;
3404 /// Swap the successors of this branch instruction.
3406 /// Swaps the successors of the branch instruction. This also swaps any
3407 /// branch weight metadata associated with the instruction so that it
3408 /// continues to map correctly to each operand.
3409 void swapSuccessors();
3411 // Methods for support type inquiry through isa, cast, and dyn_cast:
3412 static bool classof(const Instruction *I) {
3413 return (I->getOpcode() == Instruction::Br);
3415 static bool classof(const Value *V) {
3416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3421 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3424 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
3426 //===----------------------------------------------------------------------===//
3428 //===----------------------------------------------------------------------===//
3430 //===---------------------------------------------------------------------------
3433 class SwitchInst : public TerminatorInst {
3434 unsigned ReservedSpace;
3436 // Operand[0] = Value to switch on
3437 // Operand[1] = Default basic block destination
3438 // Operand[2n ] = Value to match
3439 // Operand[2n+1] = BasicBlock to go to on match
3440 SwitchInst(const SwitchInst &SI);
3442 /// Create a new switch instruction, specifying a value to switch on and a
3443 /// default destination. The number of additional cases can be specified here
3444 /// to make memory allocation more efficient. This constructor can also
3445 /// auto-insert before another instruction.
3446 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3447 Instruction *InsertBefore);
3449 /// Create a new switch instruction, specifying a value to switch on and a
3450 /// default destination. The number of additional cases can be specified here
3451 /// to make memory allocation more efficient. This constructor also
3452 /// auto-inserts at the end of the specified BasicBlock.
3453 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3454 BasicBlock *InsertAtEnd);
3456 // allocate space for exactly zero operands
3457 void *operator new(size_t s) {
3458 return User::operator new(s);
3461 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3462 void growOperands();
3465 // Note: Instruction needs to be a friend here to call cloneImpl.
3466 friend class Instruction;
3468 SwitchInst *cloneImpl() const;
3472 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3474 template <typename CaseHandleT> class CaseIteratorImpl;
3476 /// A handle to a particular switch case. It exposes a convenient interface
3477 /// to both the case value and the successor block.
3479 /// We define this as a template and instantiate it to form both a const and
3480 /// non-const handle.
3481 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3482 class CaseHandleImpl {
3483 // Directly befriend both const and non-const iterators.
3484 friend class SwitchInst::CaseIteratorImpl<
3485 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3488 // Expose the switch type we're parameterized with to the iterator.
3489 using SwitchInstType = SwitchInstT;
3494 CaseHandleImpl() = default;
3495 CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {}
3498 /// Resolves case value for current case.
3499 ConstantIntT *getCaseValue() const {
3500 assert((unsigned)Index < SI->getNumCases() &&
3501 "Index out the number of cases.");
3502 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3505 /// Resolves successor for current case.
3506 BasicBlockT *getCaseSuccessor() const {
3507 assert(((unsigned)Index < SI->getNumCases() ||
3508 (unsigned)Index == DefaultPseudoIndex) &&
3509 "Index out the number of cases.");
3510 return SI->getSuccessor(getSuccessorIndex());
3513 /// Returns number of current case.
3514 unsigned getCaseIndex() const { return Index; }
3516 /// Returns TerminatorInst's successor index for current case successor.
3517 unsigned getSuccessorIndex() const {
3518 assert(((unsigned)Index == DefaultPseudoIndex ||
3519 (unsigned)Index < SI->getNumCases()) &&
3520 "Index out the number of cases.");
3521 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3524 bool operator==(const CaseHandleImpl &RHS) const {
3525 assert(SI == RHS.SI && "Incompatible operators.");
3526 return Index == RHS.Index;
3530 using ConstCaseHandle =
3531 CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>;
3534 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3535 friend class SwitchInst::CaseIteratorImpl<CaseHandle>;
3538 CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {}
3540 /// Sets the new value for current case.
3541 void setValue(ConstantInt *V) {
3542 assert((unsigned)Index < SI->getNumCases() &&
3543 "Index out the number of cases.");
3544 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3547 /// Sets the new successor for current case.
3548 void setSuccessor(BasicBlock *S) {
3549 SI->setSuccessor(getSuccessorIndex(), S);
3553 template <typename CaseHandleT>
3554 class CaseIteratorImpl
3555 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3556 std::random_access_iterator_tag,
3558 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3563 /// Default constructed iterator is in an invalid state until assigned to
3564 /// a case for a particular switch.
3565 CaseIteratorImpl() = default;
3567 /// Initializes case iterator for given SwitchInst and for given
3569 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3571 /// Initializes case iterator for given SwitchInst and for given
3572 /// TerminatorInst's successor index.
3573 static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI,
3574 unsigned SuccessorIndex) {
3575 assert(SuccessorIndex < SI->getNumSuccessors() &&
3576 "Successor index # out of range!");
3577 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3578 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3581 /// Support converting to the const variant. This will be a no-op for const
3583 operator CaseIteratorImpl<ConstCaseHandle>() const {
3584 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3587 CaseIteratorImpl &operator+=(ptrdiff_t N) {
3588 // Check index correctness after addition.
3589 // Note: Index == getNumCases() means end().
3590 assert(Case.Index + N >= 0 &&
3591 (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
3592 "Case.Index out the number of cases.");
3596 CaseIteratorImpl &operator-=(ptrdiff_t N) {
3597 // Check index correctness after subtraction.
3598 // Note: Case.Index == getNumCases() means end().
3599 assert(Case.Index - N >= 0 &&
3600 (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
3601 "Case.Index out the number of cases.");
3605 ptrdiff_t operator-(const CaseIteratorImpl &RHS) const {
3606 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3607 return Case.Index - RHS.Case.Index;
3609 bool operator==(const CaseIteratorImpl &RHS) const {
3610 return Case == RHS.Case;
3612 bool operator<(const CaseIteratorImpl &RHS) const {
3613 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3614 return Case.Index < RHS.Case.Index;
3616 CaseHandleT &operator*() { return Case; }
3617 const CaseHandleT &operator*() const { return Case; }
3620 using CaseIt = CaseIteratorImpl<CaseHandle>;
3621 using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>;
3623 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3625 Instruction *InsertBefore = nullptr) {
3626 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3629 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3630 unsigned NumCases, BasicBlock *InsertAtEnd) {
3631 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3634 /// Provide fast operand accessors
3635 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3637 // Accessor Methods for Switch stmt
3638 Value *getCondition() const { return getOperand(0); }
3639 void setCondition(Value *V) { setOperand(0, V); }
3641 BasicBlock *getDefaultDest() const {
3642 return cast<BasicBlock>(getOperand(1));
3645 void setDefaultDest(BasicBlock *DefaultCase) {
3646 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3649 /// Return the number of 'cases' in this switch instruction, excluding the
3651 unsigned getNumCases() const {
3652 return getNumOperands()/2 - 1;
3655 /// Returns a read/write iterator that points to the first case in the
3657 CaseIt case_begin() {
3658 return CaseIt(this, 0);
3661 /// Returns a read-only iterator that points to the first case in the
3663 ConstCaseIt case_begin() const {
3664 return ConstCaseIt(this, 0);
3667 /// Returns a read/write iterator that points one past the last in the
3670 return CaseIt(this, getNumCases());
3673 /// Returns a read-only iterator that points one past the last in the
3675 ConstCaseIt case_end() const {
3676 return ConstCaseIt(this, getNumCases());
3679 /// Iteration adapter for range-for loops.
3680 iterator_range<CaseIt> cases() {
3681 return make_range(case_begin(), case_end());
3684 /// Constant iteration adapter for range-for loops.
3685 iterator_range<ConstCaseIt> cases() const {
3686 return make_range(case_begin(), case_end());
3689 /// Returns an iterator that points to the default case.
3690 /// Note: this iterator allows to resolve successor only. Attempt
3691 /// to resolve case value causes an assertion.
3692 /// Also note, that increment and decrement also causes an assertion and
3693 /// makes iterator invalid.
3694 CaseIt case_default() {
3695 return CaseIt(this, DefaultPseudoIndex);
3697 ConstCaseIt case_default() const {
3698 return ConstCaseIt(this, DefaultPseudoIndex);
3701 /// Search all of the case values for the specified constant. If it is
3702 /// explicitly handled, return the case iterator of it, otherwise return
3703 /// default case iterator to indicate that it is handled by the default
3705 CaseIt findCaseValue(const ConstantInt *C) {
3706 CaseIt I = llvm::find_if(
3707 cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; });
3708 if (I != case_end())
3711 return case_default();
3713 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3714 ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) {
3715 return Case.getCaseValue() == C;
3717 if (I != case_end())
3720 return case_default();
3723 /// Finds the unique case value for a given successor. Returns null if the
3724 /// successor is not found, not unique, or is the default case.
3725 ConstantInt *findCaseDest(BasicBlock *BB) {
3726 if (BB == getDefaultDest())
3729 ConstantInt *CI = nullptr;
3730 for (auto Case : cases()) {
3731 if (Case.getCaseSuccessor() != BB)
3735 return nullptr; // Multiple cases lead to BB.
3737 CI = Case.getCaseValue();
3743 /// Add an entry to the switch instruction.
3745 /// This action invalidates case_end(). Old case_end() iterator will
3746 /// point to the added case.
3747 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3749 /// This method removes the specified case and its successor from the switch
3750 /// instruction. Note that this operation may reorder the remaining cases at
3751 /// index idx and above.
3753 /// This action invalidates iterators for all cases following the one removed,
3754 /// including the case_end() iterator. It returns an iterator for the next
3756 CaseIt removeCase(CaseIt I);
3758 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3759 BasicBlock *getSuccessor(unsigned idx) const {
3760 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3761 return cast<BasicBlock>(getOperand(idx*2+1));
3763 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3764 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3765 setOperand(idx * 2 + 1, NewSucc);
3768 // Methods for support type inquiry through isa, cast, and dyn_cast:
3769 static bool classof(const Instruction *I) {
3770 return I->getOpcode() == Instruction::Switch;
3772 static bool classof(const Value *V) {
3773 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3778 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3781 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3783 //===----------------------------------------------------------------------===//
3784 // IndirectBrInst Class
3785 //===----------------------------------------------------------------------===//
3787 //===---------------------------------------------------------------------------
3788 /// Indirect Branch Instruction.
3790 class IndirectBrInst : public TerminatorInst {
3791 unsigned ReservedSpace;
3793 // Operand[0] = Address to jump to
3794 // Operand[n+1] = n-th destination
3795 IndirectBrInst(const IndirectBrInst &IBI);
3797 /// Create a new indirectbr instruction, specifying an
3798 /// Address to jump to. The number of expected destinations can be specified
3799 /// here to make memory allocation more efficient. This constructor can also
3800 /// autoinsert before another instruction.
3801 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3803 /// Create a new indirectbr instruction, specifying an
3804 /// Address to jump to. The number of expected destinations can be specified
3805 /// here to make memory allocation more efficient. This constructor also
3806 /// autoinserts at the end of the specified BasicBlock.
3807 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3809 // allocate space for exactly zero operands
3810 void *operator new(size_t s) {
3811 return User::operator new(s);
3814 void init(Value *Address, unsigned NumDests);
3815 void growOperands();
3818 // Note: Instruction needs to be a friend here to call cloneImpl.
3819 friend class Instruction;
3821 IndirectBrInst *cloneImpl() const;
3824 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3825 Instruction *InsertBefore = nullptr) {
3826 return new IndirectBrInst(Address, NumDests, InsertBefore);
3829 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3830 BasicBlock *InsertAtEnd) {
3831 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3834 /// Provide fast operand accessors.
3835 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3837 // Accessor Methods for IndirectBrInst instruction.
3838 Value *getAddress() { return getOperand(0); }
3839 const Value *getAddress() const { return getOperand(0); }
3840 void setAddress(Value *V) { setOperand(0, V); }
3842 /// return the number of possible destinations in this
3843 /// indirectbr instruction.
3844 unsigned getNumDestinations() const { return getNumOperands()-1; }
3846 /// Return the specified destination.
3847 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3848 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3850 /// Add a destination.
3852 void addDestination(BasicBlock *Dest);
3854 /// This method removes the specified successor from the
3855 /// indirectbr instruction.
3856 void removeDestination(unsigned i);
3858 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3859 BasicBlock *getSuccessor(unsigned i) const {
3860 return cast<BasicBlock>(getOperand(i+1));
3862 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3863 setOperand(i + 1, NewSucc);
3866 // Methods for support type inquiry through isa, cast, and dyn_cast:
3867 static bool classof(const Instruction *I) {
3868 return I->getOpcode() == Instruction::IndirectBr;
3870 static bool classof(const Value *V) {
3871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3876 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3879 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3881 //===----------------------------------------------------------------------===//
3883 //===----------------------------------------------------------------------===//
3885 /// Invoke instruction. The SubclassData field is used to hold the
3886 /// calling convention of the call.
3888 class InvokeInst : public CallBase<InvokeInst> {
3889 friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3891 InvokeInst(const InvokeInst &BI);
3893 /// Construct an InvokeInst given a range of arguments.
3895 /// Construct an InvokeInst from a range of arguments
3896 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3897 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3898 unsigned Values, const Twine &NameStr,
3899 Instruction *InsertBefore)
3900 : InvokeInst(cast<FunctionType>(
3901 cast<PointerType>(Func->getType())->getElementType()),
3902 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3905 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3906 BasicBlock *IfException, ArrayRef<Value *> Args,
3907 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3908 const Twine &NameStr, Instruction *InsertBefore);
3909 /// Construct an InvokeInst given a range of arguments.
3911 /// Construct an InvokeInst from a range of arguments
3912 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3913 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3914 unsigned Values, const Twine &NameStr,
3915 BasicBlock *InsertAtEnd);
3918 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3919 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3920 const Twine &NameStr) {
3921 init(cast<FunctionType>(
3922 cast<PointerType>(Func->getType())->getElementType()),
3923 Func, IfNormal, IfException, Args, Bundles, NameStr);
3926 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3927 BasicBlock *IfException, ArrayRef<Value *> Args,
3928 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3931 // Note: Instruction needs to be a friend here to call cloneImpl.
3932 friend class Instruction;
3934 InvokeInst *cloneImpl() const;
3937 static constexpr int ArgOffset = 3;
3938 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3939 BasicBlock *IfException, ArrayRef<Value *> Args,
3940 const Twine &NameStr,
3941 Instruction *InsertBefore = nullptr) {
3942 return Create(cast<FunctionType>(
3943 cast<PointerType>(Func->getType())->getElementType()),
3944 Func, IfNormal, IfException, Args, None, NameStr,
3948 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3949 BasicBlock *IfException, ArrayRef<Value *> Args,
3950 ArrayRef<OperandBundleDef> Bundles = None,
3951 const Twine &NameStr = "",
3952 Instruction *InsertBefore = nullptr) {
3953 return Create(cast<FunctionType>(
3954 cast<PointerType>(Func->getType())->getElementType()),
3955 Func, IfNormal, IfException, Args, Bundles, NameStr,
3959 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3960 BasicBlock *IfException, ArrayRef<Value *> Args,
3961 const Twine &NameStr,
3962 Instruction *InsertBefore = nullptr) {
3963 unsigned Values = unsigned(Args.size()) + 3;
3964 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3965 Values, NameStr, InsertBefore);
3968 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3969 BasicBlock *IfException, ArrayRef<Value *> Args,
3970 ArrayRef<OperandBundleDef> Bundles = None,
3971 const Twine &NameStr = "",
3972 Instruction *InsertBefore = nullptr) {
3973 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3974 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3976 return new (Values, DescriptorBytes)
3977 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3978 NameStr, InsertBefore);
3981 static InvokeInst *Create(Value *Func,
3982 BasicBlock *IfNormal, BasicBlock *IfException,
3983 ArrayRef<Value *> Args, const Twine &NameStr,
3984 BasicBlock *InsertAtEnd) {
3985 unsigned Values = unsigned(Args.size()) + 3;
3986 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3987 Values, NameStr, InsertAtEnd);
3990 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3991 BasicBlock *IfException, ArrayRef<Value *> Args,
3992 ArrayRef<OperandBundleDef> Bundles,
3993 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3994 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3995 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3997 return new (Values, DescriptorBytes)
3998 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
4002 /// Create a clone of \p II with a different set of operand bundles and
4003 /// insert it before \p InsertPt.
4005 /// The returned invoke instruction is identical to \p II in every way except
4006 /// that the operand bundles for the new instruction are set to the operand
4007 /// bundles in \p Bundles.
4008 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
4009 Instruction *InsertPt = nullptr);
4011 /// Determine if the call should not perform indirect branch tracking.
4012 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
4014 /// Determine if the call cannot unwind.
4015 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
4016 void setDoesNotThrow() {
4017 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
4020 /// Return the function called, or null if this is an
4021 /// indirect function invocation.
4023 Function *getCalledFunction() const {
4024 return dyn_cast<Function>(Op<-3>());
4027 /// Get a pointer to the function that is invoked by this
4029 const Value *getCalledValue() const { return Op<-3>(); }
4030 Value *getCalledValue() { return Op<-3>(); }
4032 /// Set the function called.
4033 void setCalledFunction(Value* Fn) {
4035 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
4038 void setCalledFunction(FunctionType *FTy, Value *Fn) {
4040 assert(FTy == cast<FunctionType>(
4041 cast<PointerType>(Fn->getType())->getElementType()));
4045 // get*Dest - Return the destination basic blocks...
4046 BasicBlock *getNormalDest() const {
4047 return cast<BasicBlock>(Op<-2>());
4049 BasicBlock *getUnwindDest() const {
4050 return cast<BasicBlock>(Op<-1>());
4052 void setNormalDest(BasicBlock *B) {
4053 Op<-2>() = reinterpret_cast<Value*>(B);
4055 void setUnwindDest(BasicBlock *B) {
4056 Op<-1>() = reinterpret_cast<Value*>(B);
4059 /// Get the landingpad instruction from the landing pad
4060 /// block (the unwind destination).
4061 LandingPadInst *getLandingPadInst() const;
4063 BasicBlock *getSuccessor(unsigned i) const {
4064 assert(i < 2 && "Successor # out of range for invoke!");
4065 return i == 0 ? getNormalDest() : getUnwindDest();
4068 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4069 assert(idx < 2 && "Successor # out of range for invoke!");
4070 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
4073 unsigned getNumSuccessors() const { return 2; }
4075 // Methods for support type inquiry through isa, cast, and dyn_cast:
4076 static bool classof(const Instruction *I) {
4077 return (I->getOpcode() == Instruction::Invoke);
4079 static bool classof(const Value *V) {
4080 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4085 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4086 // method so that subclasses cannot accidentally use it.
4087 void setInstructionSubclassData(unsigned short D) {
4088 Instruction::setInstructionSubclassData(D);
4093 struct OperandTraits<CallBase<InvokeInst>>
4094 : public VariadicOperandTraits<CallBase<InvokeInst>, 3> {};
4096 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
4097 BasicBlock *IfException, ArrayRef<Value *> Args,
4098 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
4099 const Twine &NameStr, Instruction *InsertBefore)
4100 : CallBase<InvokeInst>(Ty->getReturnType(), Instruction::Invoke,
4101 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
4103 Values, InsertBefore) {
4104 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
4107 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
4108 BasicBlock *IfException, ArrayRef<Value *> Args,
4109 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
4110 const Twine &NameStr, BasicBlock *InsertAtEnd)
4111 : CallBase<InvokeInst>(
4113 cast<PointerType>(Func->getType())->getElementType())
4115 Instruction::Invoke,
4116 OperandTraits<CallBase<InvokeInst>>::op_end(this) - Values, Values,
4118 init(Func, IfNormal, IfException, Args, Bundles, NameStr);
4122 //===----------------------------------------------------------------------===//
4124 //===----------------------------------------------------------------------===//
4126 //===---------------------------------------------------------------------------
4127 /// Resume the propagation of an exception.
4129 class ResumeInst : public TerminatorInst {
4130 ResumeInst(const ResumeInst &RI);
4132 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
4133 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
4136 // Note: Instruction needs to be a friend here to call cloneImpl.
4137 friend class Instruction;
4139 ResumeInst *cloneImpl() const;
4142 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
4143 return new(1) ResumeInst(Exn, InsertBefore);
4146 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
4147 return new(1) ResumeInst(Exn, InsertAtEnd);
4150 /// Provide fast operand accessors
4151 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4153 /// Convenience accessor.
4154 Value *getValue() const { return Op<0>(); }
4156 unsigned getNumSuccessors() const { return 0; }
4158 // Methods for support type inquiry through isa, cast, and dyn_cast:
4159 static bool classof(const Instruction *I) {
4160 return I->getOpcode() == Instruction::Resume;
4162 static bool classof(const Value *V) {
4163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4167 friend TerminatorInst;
4169 BasicBlock *getSuccessor(unsigned idx) const {
4170 llvm_unreachable("ResumeInst has no successors!");
4173 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4174 llvm_unreachable("ResumeInst has no successors!");
4179 struct OperandTraits<ResumeInst> :
4180 public FixedNumOperandTraits<ResumeInst, 1> {
4183 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
4185 //===----------------------------------------------------------------------===//
4186 // CatchSwitchInst Class
4187 //===----------------------------------------------------------------------===//
4188 class CatchSwitchInst : public TerminatorInst {
4189 /// The number of operands actually allocated. NumOperands is
4190 /// the number actually in use.
4191 unsigned ReservedSpace;
4193 // Operand[0] = Outer scope
4194 // Operand[1] = Unwind block destination
4195 // Operand[n] = BasicBlock to go to on match
4196 CatchSwitchInst(const CatchSwitchInst &CSI);
4198 /// Create a new switch instruction, specifying a
4199 /// default destination. The number of additional handlers can be specified
4200 /// here to make memory allocation more efficient.
4201 /// This constructor can also autoinsert before another instruction.
4202 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4203 unsigned NumHandlers, const Twine &NameStr,
4204 Instruction *InsertBefore);
4206 /// Create a new switch instruction, specifying a
4207 /// default destination. The number of additional handlers can be specified
4208 /// here to make memory allocation more efficient.
4209 /// This constructor also autoinserts at the end of the specified BasicBlock.
4210 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4211 unsigned NumHandlers, const Twine &NameStr,
4212 BasicBlock *InsertAtEnd);
4214 // allocate space for exactly zero operands
4215 void *operator new(size_t s) { return User::operator new(s); }
4217 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
4218 void growOperands(unsigned Size);
4221 // Note: Instruction needs to be a friend here to call cloneImpl.
4222 friend class Instruction;
4224 CatchSwitchInst *cloneImpl() const;
4227 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4228 unsigned NumHandlers,
4229 const Twine &NameStr = "",
4230 Instruction *InsertBefore = nullptr) {
4231 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4235 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4236 unsigned NumHandlers, const Twine &NameStr,
4237 BasicBlock *InsertAtEnd) {
4238 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4242 /// Provide fast operand accessors
4243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4245 // Accessor Methods for CatchSwitch stmt
4246 Value *getParentPad() const { return getOperand(0); }
4247 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4249 // Accessor Methods for CatchSwitch stmt
4250 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4251 bool unwindsToCaller() const { return !hasUnwindDest(); }
4252 BasicBlock *getUnwindDest() const {
4253 if (hasUnwindDest())
4254 return cast<BasicBlock>(getOperand(1));
4257 void setUnwindDest(BasicBlock *UnwindDest) {
4259 assert(hasUnwindDest());
4260 setOperand(1, UnwindDest);
4263 /// return the number of 'handlers' in this catchswitch
4264 /// instruction, except the default handler
4265 unsigned getNumHandlers() const {
4266 if (hasUnwindDest())
4267 return getNumOperands() - 2;
4268 return getNumOperands() - 1;
4272 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4273 static const BasicBlock *handler_helper(const Value *V) {
4274 return cast<BasicBlock>(V);
4278 using DerefFnTy = BasicBlock *(*)(Value *);
4279 using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>;
4280 using handler_range = iterator_range<handler_iterator>;
4281 using ConstDerefFnTy = const BasicBlock *(*)(const Value *);
4282 using const_handler_iterator =
4283 mapped_iterator<const_op_iterator, ConstDerefFnTy>;
4284 using const_handler_range = iterator_range<const_handler_iterator>;
4286 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4287 handler_iterator handler_begin() {
4288 op_iterator It = op_begin() + 1;
4289 if (hasUnwindDest())
4291 return handler_iterator(It, DerefFnTy(handler_helper));
4294 /// Returns an iterator that points to the first handler in the
4295 /// CatchSwitchInst.
4296 const_handler_iterator handler_begin() const {
4297 const_op_iterator It = op_begin() + 1;
4298 if (hasUnwindDest())
4300 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4303 /// Returns a read-only iterator that points one past the last
4304 /// handler in the CatchSwitchInst.
4305 handler_iterator handler_end() {
4306 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4309 /// Returns an iterator that points one past the last handler in the
4310 /// CatchSwitchInst.
4311 const_handler_iterator handler_end() const {
4312 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4315 /// iteration adapter for range-for loops.
4316 handler_range handlers() {
4317 return make_range(handler_begin(), handler_end());
4320 /// iteration adapter for range-for loops.
4321 const_handler_range handlers() const {
4322 return make_range(handler_begin(), handler_end());
4325 /// Add an entry to the switch instruction...
4327 /// This action invalidates handler_end(). Old handler_end() iterator will
4328 /// point to the added handler.
4329 void addHandler(BasicBlock *Dest);
4331 void removeHandler(handler_iterator HI);
4333 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4334 BasicBlock *getSuccessor(unsigned Idx) const {
4335 assert(Idx < getNumSuccessors() &&
4336 "Successor # out of range for catchswitch!");
4337 return cast<BasicBlock>(getOperand(Idx + 1));
4339 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4340 assert(Idx < getNumSuccessors() &&
4341 "Successor # out of range for catchswitch!");
4342 setOperand(Idx + 1, NewSucc);
4345 // Methods for support type inquiry through isa, cast, and dyn_cast:
4346 static bool classof(const Instruction *I) {
4347 return I->getOpcode() == Instruction::CatchSwitch;
4349 static bool classof(const Value *V) {
4350 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4355 struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4357 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)
4359 //===----------------------------------------------------------------------===//
4360 // CleanupPadInst Class
4361 //===----------------------------------------------------------------------===//
4362 class CleanupPadInst : public FuncletPadInst {
4364 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4365 unsigned Values, const Twine &NameStr,
4366 Instruction *InsertBefore)
4367 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4368 NameStr, InsertBefore) {}
4369 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4370 unsigned Values, const Twine &NameStr,
4371 BasicBlock *InsertAtEnd)
4372 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4373 NameStr, InsertAtEnd) {}
4376 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4377 const Twine &NameStr = "",
4378 Instruction *InsertBefore = nullptr) {
4379 unsigned Values = 1 + Args.size();
4381 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4384 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4385 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4386 unsigned Values = 1 + Args.size();
4388 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4391 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4392 static bool classof(const Instruction *I) {
4393 return I->getOpcode() == Instruction::CleanupPad;
4395 static bool classof(const Value *V) {
4396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4400 //===----------------------------------------------------------------------===//
4401 // CatchPadInst Class
4402 //===----------------------------------------------------------------------===//
4403 class CatchPadInst : public FuncletPadInst {
4405 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4406 unsigned Values, const Twine &NameStr,
4407 Instruction *InsertBefore)
4408 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4409 NameStr, InsertBefore) {}
4410 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4411 unsigned Values, const Twine &NameStr,
4412 BasicBlock *InsertAtEnd)
4413 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4414 NameStr, InsertAtEnd) {}
4417 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4418 const Twine &NameStr = "",
4419 Instruction *InsertBefore = nullptr) {
4420 unsigned Values = 1 + Args.size();
4422 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4425 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4426 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4427 unsigned Values = 1 + Args.size();
4429 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4432 /// Convenience accessors
4433 CatchSwitchInst *getCatchSwitch() const {
4434 return cast<CatchSwitchInst>(Op<-1>());
4436 void setCatchSwitch(Value *CatchSwitch) {
4437 assert(CatchSwitch);
4438 Op<-1>() = CatchSwitch;
4441 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4442 static bool classof(const Instruction *I) {
4443 return I->getOpcode() == Instruction::CatchPad;
4445 static bool classof(const Value *V) {
4446 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4450 //===----------------------------------------------------------------------===//
4451 // CatchReturnInst Class
4452 //===----------------------------------------------------------------------===//
4454 class CatchReturnInst : public TerminatorInst {
4455 CatchReturnInst(const CatchReturnInst &RI);
4456 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4457 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4459 void init(Value *CatchPad, BasicBlock *BB);
4462 // Note: Instruction needs to be a friend here to call cloneImpl.
4463 friend class Instruction;
4465 CatchReturnInst *cloneImpl() const;
4468 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4469 Instruction *InsertBefore = nullptr) {
4472 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4475 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4476 BasicBlock *InsertAtEnd) {
4479 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4482 /// Provide fast operand accessors
4483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4485 /// Convenience accessors.
4486 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4487 void setCatchPad(CatchPadInst *CatchPad) {
4492 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4493 void setSuccessor(BasicBlock *NewSucc) {
4497 unsigned getNumSuccessors() const { return 1; }
4499 /// Get the parentPad of this catchret's catchpad's catchswitch.
4500 /// The successor block is implicitly a member of this funclet.
4501 Value *getCatchSwitchParentPad() const {
4502 return getCatchPad()->getCatchSwitch()->getParentPad();
4505 // Methods for support type inquiry through isa, cast, and dyn_cast:
4506 static bool classof(const Instruction *I) {
4507 return (I->getOpcode() == Instruction::CatchRet);
4509 static bool classof(const Value *V) {
4510 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4514 friend TerminatorInst;
4516 BasicBlock *getSuccessor(unsigned Idx) const {
4517 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4518 return getSuccessor();
4521 void setSuccessor(unsigned Idx, BasicBlock *B) {
4522 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4528 struct OperandTraits<CatchReturnInst>
4529 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4531 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
4533 //===----------------------------------------------------------------------===//
4534 // CleanupReturnInst Class
4535 //===----------------------------------------------------------------------===//
4537 class CleanupReturnInst : public TerminatorInst {
4539 CleanupReturnInst(const CleanupReturnInst &RI);
4540 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4541 Instruction *InsertBefore = nullptr);
4542 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4543 BasicBlock *InsertAtEnd);
4545 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4548 // Note: Instruction needs to be a friend here to call cloneImpl.
4549 friend class Instruction;
4551 CleanupReturnInst *cloneImpl() const;
4554 static CleanupReturnInst *Create(Value *CleanupPad,
4555 BasicBlock *UnwindBB = nullptr,
4556 Instruction *InsertBefore = nullptr) {
4558 unsigned Values = 1;
4562 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4565 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4566 BasicBlock *InsertAtEnd) {
4568 unsigned Values = 1;
4572 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4575 /// Provide fast operand accessors
4576 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4578 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4579 bool unwindsToCaller() const { return !hasUnwindDest(); }
4581 /// Convenience accessor.
4582 CleanupPadInst *getCleanupPad() const {
4583 return cast<CleanupPadInst>(Op<0>());
4585 void setCleanupPad(CleanupPadInst *CleanupPad) {
4587 Op<0>() = CleanupPad;
4590 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4592 BasicBlock *getUnwindDest() const {
4593 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4595 void setUnwindDest(BasicBlock *NewDest) {
4597 assert(hasUnwindDest());
4601 // Methods for support type inquiry through isa, cast, and dyn_cast:
4602 static bool classof(const Instruction *I) {
4603 return (I->getOpcode() == Instruction::CleanupRet);
4605 static bool classof(const Value *V) {
4606 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4610 friend TerminatorInst;
4612 BasicBlock *getSuccessor(unsigned Idx) const {
4614 return getUnwindDest();
4617 void setSuccessor(unsigned Idx, BasicBlock *B) {
4622 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4623 // method so that subclasses cannot accidentally use it.
4624 void setInstructionSubclassData(unsigned short D) {
4625 Instruction::setInstructionSubclassData(D);
4630 struct OperandTraits<CleanupReturnInst>
4631 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4633 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)
4635 //===----------------------------------------------------------------------===//
4636 // UnreachableInst Class
4637 //===----------------------------------------------------------------------===//
4639 //===---------------------------------------------------------------------------
4640 /// This function has undefined behavior. In particular, the
4641 /// presence of this instruction indicates some higher level knowledge that the
4642 /// end of the block cannot be reached.
4644 class UnreachableInst : public TerminatorInst {
4646 // Note: Instruction needs to be a friend here to call cloneImpl.
4647 friend class Instruction;
4649 UnreachableInst *cloneImpl() const;
4652 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4653 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4655 // allocate space for exactly zero operands
4656 void *operator new(size_t s) {
4657 return User::operator new(s, 0);
4660 unsigned getNumSuccessors() const { return 0; }
4662 // Methods for support type inquiry through isa, cast, and dyn_cast:
4663 static bool classof(const Instruction *I) {
4664 return I->getOpcode() == Instruction::Unreachable;
4666 static bool classof(const Value *V) {
4667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4671 friend TerminatorInst;
4673 BasicBlock *getSuccessor(unsigned idx) const {
4674 llvm_unreachable("UnreachableInst has no successors!");
4677 void setSuccessor(unsigned idx, BasicBlock *B) {
4678 llvm_unreachable("UnreachableInst has no successors!");
4682 //===----------------------------------------------------------------------===//
4684 //===----------------------------------------------------------------------===//
4686 /// This class represents a truncation of integer types.
4687 class TruncInst : public CastInst {
4689 // Note: Instruction needs to be a friend here to call cloneImpl.
4690 friend class Instruction;
4692 /// Clone an identical TruncInst
4693 TruncInst *cloneImpl() const;
4696 /// Constructor with insert-before-instruction semantics
4698 Value *S, ///< The value to be truncated
4699 Type *Ty, ///< The (smaller) type to truncate to
4700 const Twine &NameStr = "", ///< A name for the new instruction
4701 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4704 /// Constructor with insert-at-end-of-block semantics
4706 Value *S, ///< The value to be truncated
4707 Type *Ty, ///< The (smaller) type to truncate to
4708 const Twine &NameStr, ///< A name for the new instruction
4709 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4712 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4713 static bool classof(const Instruction *I) {
4714 return I->getOpcode() == Trunc;
4716 static bool classof(const Value *V) {
4717 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4721 //===----------------------------------------------------------------------===//
4723 //===----------------------------------------------------------------------===//
4725 /// This class represents zero extension of integer types.
4726 class ZExtInst : public CastInst {
4728 // Note: Instruction needs to be a friend here to call cloneImpl.
4729 friend class Instruction;
4731 /// Clone an identical ZExtInst
4732 ZExtInst *cloneImpl() const;
4735 /// Constructor with insert-before-instruction semantics
4737 Value *S, ///< The value to be zero extended
4738 Type *Ty, ///< The type to zero extend to
4739 const Twine &NameStr = "", ///< A name for the new instruction
4740 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4743 /// Constructor with insert-at-end semantics.
4745 Value *S, ///< The value to be zero extended
4746 Type *Ty, ///< The type to zero extend to
4747 const Twine &NameStr, ///< A name for the new instruction
4748 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4751 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4752 static bool classof(const Instruction *I) {
4753 return I->getOpcode() == ZExt;
4755 static bool classof(const Value *V) {
4756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4760 //===----------------------------------------------------------------------===//
4762 //===----------------------------------------------------------------------===//
4764 /// This class represents a sign extension of integer types.
4765 class SExtInst : public CastInst {
4767 // Note: Instruction needs to be a friend here to call cloneImpl.
4768 friend class Instruction;
4770 /// Clone an identical SExtInst
4771 SExtInst *cloneImpl() const;
4774 /// Constructor with insert-before-instruction semantics
4776 Value *S, ///< The value to be sign extended
4777 Type *Ty, ///< The type to sign extend to
4778 const Twine &NameStr = "", ///< A name for the new instruction
4779 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4782 /// Constructor with insert-at-end-of-block semantics
4784 Value *S, ///< The value to be sign extended
4785 Type *Ty, ///< The type to sign extend to
4786 const Twine &NameStr, ///< A name for the new instruction
4787 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4790 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4791 static bool classof(const Instruction *I) {
4792 return I->getOpcode() == SExt;
4794 static bool classof(const Value *V) {
4795 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4799 //===----------------------------------------------------------------------===//
4800 // FPTruncInst Class
4801 //===----------------------------------------------------------------------===//
4803 /// This class represents a truncation of floating point types.
4804 class FPTruncInst : public CastInst {
4806 // Note: Instruction needs to be a friend here to call cloneImpl.
4807 friend class Instruction;
4809 /// Clone an identical FPTruncInst
4810 FPTruncInst *cloneImpl() const;
4813 /// Constructor with insert-before-instruction semantics
4815 Value *S, ///< The value to be truncated
4816 Type *Ty, ///< The type to truncate to
4817 const Twine &NameStr = "", ///< A name for the new instruction
4818 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4821 /// Constructor with insert-before-instruction semantics
4823 Value *S, ///< The value to be truncated
4824 Type *Ty, ///< The type to truncate to
4825 const Twine &NameStr, ///< A name for the new instruction
4826 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4829 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4830 static bool classof(const Instruction *I) {
4831 return I->getOpcode() == FPTrunc;
4833 static bool classof(const Value *V) {
4834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4838 //===----------------------------------------------------------------------===//
4840 //===----------------------------------------------------------------------===//
4842 /// This class represents an extension of floating point types.
4843 class FPExtInst : public CastInst {
4845 // Note: Instruction needs to be a friend here to call cloneImpl.
4846 friend class Instruction;
4848 /// Clone an identical FPExtInst
4849 FPExtInst *cloneImpl() const;
4852 /// Constructor with insert-before-instruction semantics
4854 Value *S, ///< The value to be extended
4855 Type *Ty, ///< The type to extend to
4856 const Twine &NameStr = "", ///< A name for the new instruction
4857 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4860 /// Constructor with insert-at-end-of-block semantics
4862 Value *S, ///< The value to be extended
4863 Type *Ty, ///< The type to extend to
4864 const Twine &NameStr, ///< A name for the new instruction
4865 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4868 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4869 static bool classof(const Instruction *I) {
4870 return I->getOpcode() == FPExt;
4872 static bool classof(const Value *V) {
4873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4877 //===----------------------------------------------------------------------===//
4879 //===----------------------------------------------------------------------===//
4881 /// This class represents a cast unsigned integer to floating point.
4882 class UIToFPInst : public CastInst {
4884 // Note: Instruction needs to be a friend here to call cloneImpl.
4885 friend class Instruction;
4887 /// Clone an identical UIToFPInst
4888 UIToFPInst *cloneImpl() const;
4891 /// Constructor with insert-before-instruction semantics
4893 Value *S, ///< The value to be converted
4894 Type *Ty, ///< The type to convert to
4895 const Twine &NameStr = "", ///< A name for the new instruction
4896 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4899 /// Constructor with insert-at-end-of-block semantics
4901 Value *S, ///< The value to be converted
4902 Type *Ty, ///< The type to convert to
4903 const Twine &NameStr, ///< A name for the new instruction
4904 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4907 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4908 static bool classof(const Instruction *I) {
4909 return I->getOpcode() == UIToFP;
4911 static bool classof(const Value *V) {
4912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4916 //===----------------------------------------------------------------------===//
4918 //===----------------------------------------------------------------------===//
4920 /// This class represents a cast from signed integer to floating point.
4921 class SIToFPInst : public CastInst {
4923 // Note: Instruction needs to be a friend here to call cloneImpl.
4924 friend class Instruction;
4926 /// Clone an identical SIToFPInst
4927 SIToFPInst *cloneImpl() const;
4930 /// Constructor with insert-before-instruction semantics
4932 Value *S, ///< The value to be converted
4933 Type *Ty, ///< The type to convert to
4934 const Twine &NameStr = "", ///< A name for the new instruction
4935 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4938 /// Constructor with insert-at-end-of-block semantics
4940 Value *S, ///< The value to be converted
4941 Type *Ty, ///< The type to convert to
4942 const Twine &NameStr, ///< A name for the new instruction
4943 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4946 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4947 static bool classof(const Instruction *I) {
4948 return I->getOpcode() == SIToFP;
4950 static bool classof(const Value *V) {
4951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4955 //===----------------------------------------------------------------------===//
4957 //===----------------------------------------------------------------------===//
4959 /// This class represents a cast from floating point to unsigned integer
4960 class FPToUIInst : public CastInst {
4962 // Note: Instruction needs to be a friend here to call cloneImpl.
4963 friend class Instruction;
4965 /// Clone an identical FPToUIInst
4966 FPToUIInst *cloneImpl() const;
4969 /// Constructor with insert-before-instruction semantics
4971 Value *S, ///< The value to be converted
4972 Type *Ty, ///< The type to convert to
4973 const Twine &NameStr = "", ///< A name for the new instruction
4974 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4977 /// Constructor with insert-at-end-of-block semantics
4979 Value *S, ///< The value to be converted
4980 Type *Ty, ///< The type to convert to
4981 const Twine &NameStr, ///< A name for the new instruction
4982 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
4985 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4986 static bool classof(const Instruction *I) {
4987 return I->getOpcode() == FPToUI;
4989 static bool classof(const Value *V) {
4990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4994 //===----------------------------------------------------------------------===//
4996 //===----------------------------------------------------------------------===//
4998 /// This class represents a cast from floating point to signed integer.
4999 class FPToSIInst : public CastInst {
5001 // Note: Instruction needs to be a friend here to call cloneImpl.
5002 friend class Instruction;
5004 /// Clone an identical FPToSIInst
5005 FPToSIInst *cloneImpl() const;
5008 /// Constructor with insert-before-instruction semantics
5010 Value *S, ///< The value to be converted
5011 Type *Ty, ///< The type to convert to
5012 const Twine &NameStr = "", ///< A name for the new instruction
5013 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5016 /// Constructor with insert-at-end-of-block semantics
5018 Value *S, ///< The value to be converted
5019 Type *Ty, ///< The type to convert to
5020 const Twine &NameStr, ///< A name for the new instruction
5021 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5024 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5025 static bool classof(const Instruction *I) {
5026 return I->getOpcode() == FPToSI;
5028 static bool classof(const Value *V) {
5029 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5033 //===----------------------------------------------------------------------===//
5034 // IntToPtrInst Class
5035 //===----------------------------------------------------------------------===//
5037 /// This class represents a cast from an integer to a pointer.
5038 class IntToPtrInst : public CastInst {
5040 // Note: Instruction needs to be a friend here to call cloneImpl.
5041 friend class Instruction;
5043 /// Constructor with insert-before-instruction semantics
5045 Value *S, ///< The value to be converted
5046 Type *Ty, ///< The type to convert to
5047 const Twine &NameStr = "", ///< A name for the new instruction
5048 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5051 /// Constructor with insert-at-end-of-block semantics
5053 Value *S, ///< The value to be converted
5054 Type *Ty, ///< The type to convert to
5055 const Twine &NameStr, ///< A name for the new instruction
5056 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5059 /// Clone an identical IntToPtrInst.
5060 IntToPtrInst *cloneImpl() const;
5062 /// Returns the address space of this instruction's pointer type.
5063 unsigned getAddressSpace() const {
5064 return getType()->getPointerAddressSpace();
5067 // Methods for support type inquiry through isa, cast, and dyn_cast:
5068 static bool classof(const Instruction *I) {
5069 return I->getOpcode() == IntToPtr;
5071 static bool classof(const Value *V) {
5072 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5076 //===----------------------------------------------------------------------===//
5077 // PtrToIntInst Class
5078 //===----------------------------------------------------------------------===//
5080 /// This class represents a cast from a pointer to an integer.
5081 class PtrToIntInst : public CastInst {
5083 // Note: Instruction needs to be a friend here to call cloneImpl.
5084 friend class Instruction;
5086 /// Clone an identical PtrToIntInst.
5087 PtrToIntInst *cloneImpl() const;
5090 /// Constructor with insert-before-instruction semantics
5092 Value *S, ///< The value to be converted
5093 Type *Ty, ///< The type to convert to
5094 const Twine &NameStr = "", ///< A name for the new instruction
5095 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5098 /// Constructor with insert-at-end-of-block semantics
5100 Value *S, ///< The value to be converted
5101 Type *Ty, ///< The type to convert to
5102 const Twine &NameStr, ///< A name for the new instruction
5103 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5106 /// Gets the pointer operand.
5107 Value *getPointerOperand() { return getOperand(0); }
5108 /// Gets the pointer operand.
5109 const Value *getPointerOperand() const { return getOperand(0); }
5110 /// Gets the operand index of the pointer operand.
5111 static unsigned getPointerOperandIndex() { return 0U; }
5113 /// Returns the address space of the pointer operand.
5114 unsigned getPointerAddressSpace() const {
5115 return getPointerOperand()->getType()->getPointerAddressSpace();
5118 // Methods for support type inquiry through isa, cast, and dyn_cast:
5119 static bool classof(const Instruction *I) {
5120 return I->getOpcode() == PtrToInt;
5122 static bool classof(const Value *V) {
5123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5127 //===----------------------------------------------------------------------===//
5128 // BitCastInst Class
5129 //===----------------------------------------------------------------------===//
5131 /// This class represents a no-op cast from one type to another.
5132 class BitCastInst : public CastInst {
5134 // Note: Instruction needs to be a friend here to call cloneImpl.
5135 friend class Instruction;
5137 /// Clone an identical BitCastInst.
5138 BitCastInst *cloneImpl() const;
5141 /// Constructor with insert-before-instruction semantics
5143 Value *S, ///< The value to be casted
5144 Type *Ty, ///< The type to casted to
5145 const Twine &NameStr = "", ///< A name for the new instruction
5146 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5149 /// Constructor with insert-at-end-of-block semantics
5151 Value *S, ///< The value to be casted
5152 Type *Ty, ///< The type to casted to
5153 const Twine &NameStr, ///< A name for the new instruction
5154 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5157 // Methods for support type inquiry through isa, cast, and dyn_cast:
5158 static bool classof(const Instruction *I) {
5159 return I->getOpcode() == BitCast;
5161 static bool classof(const Value *V) {
5162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5166 //===----------------------------------------------------------------------===//
5167 // AddrSpaceCastInst Class
5168 //===----------------------------------------------------------------------===//
5170 /// This class represents a conversion between pointers from one address space
5172 class AddrSpaceCastInst : public CastInst {
5174 // Note: Instruction needs to be a friend here to call cloneImpl.
5175 friend class Instruction;
5177 /// Clone an identical AddrSpaceCastInst.
5178 AddrSpaceCastInst *cloneImpl() const;
5181 /// Constructor with insert-before-instruction semantics
5183 Value *S, ///< The value to be casted
5184 Type *Ty, ///< The type to casted to
5185 const Twine &NameStr = "", ///< A name for the new instruction
5186 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5189 /// Constructor with insert-at-end-of-block semantics
5191 Value *S, ///< The value to be casted
5192 Type *Ty, ///< The type to casted to
5193 const Twine &NameStr, ///< A name for the new instruction
5194 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5197 // Methods for support type inquiry through isa, cast, and dyn_cast:
5198 static bool classof(const Instruction *I) {
5199 return I->getOpcode() == AddrSpaceCast;
5201 static bool classof(const Value *V) {
5202 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5205 /// Gets the pointer operand.
5206 Value *getPointerOperand() {
5207 return getOperand(0);
5210 /// Gets the pointer operand.
5211 const Value *getPointerOperand() const {
5212 return getOperand(0);
5215 /// Gets the operand index of the pointer operand.
5216 static unsigned getPointerOperandIndex() {
5220 /// Returns the address space of the pointer operand.
5221 unsigned getSrcAddressSpace() const {
5222 return getPointerOperand()->getType()->getPointerAddressSpace();
5225 /// Returns the address space of the result.
5226 unsigned getDestAddressSpace() const {
5227 return getType()->getPointerAddressSpace();
5231 /// A helper function that returns the pointer operand of a load or store
5232 /// instruction. Returns nullptr if not load or store.
5233 inline Value *getLoadStorePointerOperand(Value *V) {
5234 if (auto *Load = dyn_cast<LoadInst>(V))
5235 return Load->getPointerOperand();
5236 if (auto *Store = dyn_cast<StoreInst>(V))
5237 return Store->getPointerOperand();
5241 /// A helper function that returns the pointer operand of a load, store
5242 /// or GEP instruction. Returns nullptr if not load, store, or GEP.
5243 inline Value *getPointerOperand(Value *V) {
5244 if (auto *Ptr = getLoadStorePointerOperand(V))
5246 if (auto *Gep = dyn_cast<GetElementPtrInst>(V))
5247 return Gep->getPointerOperand();
5251 } // end namespace llvm
5253 #endif // LLVM_IR_INSTRUCTIONS_H