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/SmallVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/IR/Attributes.h"
24 #include "llvm/IR/CallingConv.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/Support/ErrorHandling.h"
43 // Consume = 3, // Not specified yet.
47 SequentiallyConsistent = 7
50 enum SynchronizationScope {
55 /// Returns true if the ordering is at least as strong as acquire
56 /// (i.e. acquire, acq_rel or seq_cst)
57 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
58 return (Ord == Acquire ||
59 Ord == AcquireRelease ||
60 Ord == SequentiallyConsistent);
63 /// Returns true if the ordering is at least as strong as release
64 /// (i.e. release, acq_rel or seq_cst)
65 inline bool isAtLeastRelease(AtomicOrdering Ord) {
66 return (Ord == Release ||
67 Ord == AcquireRelease ||
68 Ord == SequentiallyConsistent);
71 //===----------------------------------------------------------------------===//
73 //===----------------------------------------------------------------------===//
75 /// AllocaInst - an instruction to allocate memory on the stack
77 class AllocaInst : public UnaryInstruction {
81 // Note: Instruction needs to be a friend here to call cloneImpl.
82 friend class Instruction;
83 AllocaInst *cloneImpl() const;
86 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
87 const Twine &Name = "",
88 Instruction *InsertBefore = nullptr);
89 AllocaInst(Type *Ty, Value *ArraySize,
90 const Twine &Name, BasicBlock *InsertAtEnd);
92 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
93 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
95 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
96 const Twine &Name = "", Instruction *InsertBefore = nullptr);
97 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
98 const Twine &Name, BasicBlock *InsertAtEnd);
100 // Out of line virtual method, so the vtable, etc. has a home.
101 ~AllocaInst() override;
103 /// isArrayAllocation - Return true if there is an allocation size parameter
104 /// to the allocation instruction that is not 1.
106 bool isArrayAllocation() const;
108 /// getArraySize - Get the number of elements allocated. For a simple
109 /// allocation of a single element, this will return a constant 1 value.
111 const Value *getArraySize() const { return getOperand(0); }
112 Value *getArraySize() { return getOperand(0); }
114 /// getType - Overload to return most specific pointer type
116 PointerType *getType() const {
117 return cast<PointerType>(Instruction::getType());
120 /// getAllocatedType - Return the type that is being allocated by the
123 Type *getAllocatedType() const { return AllocatedType; }
124 /// \brief for use only in special circumstances that need to generically
125 /// transform a whole instruction (eg: IR linking and vectorization).
126 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
128 /// getAlignment - Return the alignment of the memory that is being allocated
129 /// by the instruction.
131 unsigned getAlignment() const {
132 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
134 void setAlignment(unsigned Align);
136 /// isStaticAlloca - Return true if this alloca is in the entry block of the
137 /// function and is a constant size. If so, the code generator will fold it
138 /// into the prolog/epilog code, so it is basically free.
139 bool isStaticAlloca() const;
141 /// \brief Return true if this alloca is used as an inalloca argument to a
142 /// call. Such allocas are never considered static even if they are in the
144 bool isUsedWithInAlloca() const {
145 return getSubclassDataFromInstruction() & 32;
148 /// \brief Specify whether this alloca is used to represent the arguments to
150 void setUsedWithInAlloca(bool V) {
151 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
155 // Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const Instruction *I) {
157 return (I->getOpcode() == Instruction::Alloca);
159 static inline bool classof(const Value *V) {
160 return isa<Instruction>(V) && classof(cast<Instruction>(V));
164 // Shadow Instruction::setInstructionSubclassData with a private forwarding
165 // method so that subclasses cannot accidentally use it.
166 void setInstructionSubclassData(unsigned short D) {
167 Instruction::setInstructionSubclassData(D);
171 //===----------------------------------------------------------------------===//
173 //===----------------------------------------------------------------------===//
175 /// LoadInst - an instruction for reading from memory. This uses the
176 /// SubclassData field in Value to store whether or not the load is volatile.
178 class LoadInst : public UnaryInstruction {
182 // Note: Instruction needs to be a friend here to call cloneImpl.
183 friend class Instruction;
184 LoadInst *cloneImpl() const;
187 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
188 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
189 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
190 Instruction *InsertBefore = nullptr);
191 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
192 Instruction *InsertBefore = nullptr)
193 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
194 NameStr, isVolatile, InsertBefore) {}
195 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
196 BasicBlock *InsertAtEnd);
197 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
198 Instruction *InsertBefore = nullptr)
199 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
200 NameStr, isVolatile, Align, InsertBefore) {}
201 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
202 unsigned Align, Instruction *InsertBefore = nullptr);
203 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
204 unsigned Align, BasicBlock *InsertAtEnd);
205 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
206 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
207 Instruction *InsertBefore = nullptr)
208 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
209 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
210 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
211 unsigned Align, AtomicOrdering Order,
212 SynchronizationScope SynchScope = CrossThread,
213 Instruction *InsertBefore = nullptr);
214 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
215 unsigned Align, AtomicOrdering Order,
216 SynchronizationScope SynchScope,
217 BasicBlock *InsertAtEnd);
219 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
220 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
221 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
222 bool isVolatile = false, Instruction *InsertBefore = nullptr);
223 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
224 bool isVolatile = false,
225 Instruction *InsertBefore = nullptr)
226 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
227 NameStr, isVolatile, InsertBefore) {}
228 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
229 BasicBlock *InsertAtEnd);
231 /// isVolatile - Return true if this is a load from a volatile memory
234 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
236 /// setVolatile - Specify whether this is a volatile load or not.
238 void setVolatile(bool V) {
239 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
243 /// getAlignment - Return the alignment of the access that is being performed
245 unsigned getAlignment() const {
246 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
249 void setAlignment(unsigned Align);
251 /// Returns the ordering effect of this fence.
252 AtomicOrdering getOrdering() const {
253 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
256 /// Set the ordering constraint on this load. May not be Release or
258 void setOrdering(AtomicOrdering Ordering) {
259 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
263 SynchronizationScope getSynchScope() const {
264 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
267 /// Specify whether this load is ordered with respect to all
268 /// concurrently executing threads, or only with respect to signal handlers
269 /// executing in the same thread.
270 void setSynchScope(SynchronizationScope xthread) {
271 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
275 void setAtomic(AtomicOrdering Ordering,
276 SynchronizationScope SynchScope = CrossThread) {
277 setOrdering(Ordering);
278 setSynchScope(SynchScope);
281 bool isSimple() const { return !isAtomic() && !isVolatile(); }
282 bool isUnordered() const {
283 return getOrdering() <= Unordered && !isVolatile();
286 Value *getPointerOperand() { return getOperand(0); }
287 const Value *getPointerOperand() const { return getOperand(0); }
288 static unsigned getPointerOperandIndex() { return 0U; }
290 /// \brief Returns the address space of the pointer operand.
291 unsigned getPointerAddressSpace() const {
292 return getPointerOperand()->getType()->getPointerAddressSpace();
295 // Methods for support type inquiry through isa, cast, and dyn_cast:
296 static inline bool classof(const Instruction *I) {
297 return I->getOpcode() == Instruction::Load;
299 static inline bool classof(const Value *V) {
300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
304 // Shadow Instruction::setInstructionSubclassData with a private forwarding
305 // method so that subclasses cannot accidentally use it.
306 void setInstructionSubclassData(unsigned short D) {
307 Instruction::setInstructionSubclassData(D);
311 //===----------------------------------------------------------------------===//
313 //===----------------------------------------------------------------------===//
315 /// StoreInst - an instruction for storing to memory
317 class StoreInst : public Instruction {
318 void *operator new(size_t, unsigned) = delete;
322 // Note: Instruction needs to be a friend here to call cloneImpl.
323 friend class Instruction;
324 StoreInst *cloneImpl() const;
327 // allocate space for exactly two operands
328 void *operator new(size_t s) {
329 return User::operator new(s, 2);
331 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
332 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
334 Instruction *InsertBefore = nullptr);
335 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, Instruction *InsertBefore = nullptr);
338 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
339 unsigned Align, BasicBlock *InsertAtEnd);
340 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
341 unsigned Align, AtomicOrdering Order,
342 SynchronizationScope SynchScope = CrossThread,
343 Instruction *InsertBefore = nullptr);
344 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
345 unsigned Align, AtomicOrdering Order,
346 SynchronizationScope SynchScope,
347 BasicBlock *InsertAtEnd);
349 /// isVolatile - Return true if this is a store to a volatile memory
352 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
354 /// setVolatile - Specify whether this is a volatile store or not.
356 void setVolatile(bool V) {
357 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
361 /// Transparently provide more efficient getOperand methods.
362 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
364 /// getAlignment - Return the alignment of the access that is being performed
366 unsigned getAlignment() const {
367 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
370 void setAlignment(unsigned Align);
372 /// Returns the ordering effect of this store.
373 AtomicOrdering getOrdering() const {
374 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
377 /// Set the ordering constraint on this store. May not be Acquire or
379 void setOrdering(AtomicOrdering Ordering) {
380 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
384 SynchronizationScope getSynchScope() const {
385 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
388 /// Specify whether this store instruction is ordered with respect to all
389 /// concurrently executing threads, or only with respect to signal handlers
390 /// executing in the same thread.
391 void setSynchScope(SynchronizationScope xthread) {
392 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
396 void setAtomic(AtomicOrdering Ordering,
397 SynchronizationScope SynchScope = CrossThread) {
398 setOrdering(Ordering);
399 setSynchScope(SynchScope);
402 bool isSimple() const { return !isAtomic() && !isVolatile(); }
403 bool isUnordered() const {
404 return getOrdering() <= Unordered && !isVolatile();
407 Value *getValueOperand() { return getOperand(0); }
408 const Value *getValueOperand() const { return getOperand(0); }
410 Value *getPointerOperand() { return getOperand(1); }
411 const Value *getPointerOperand() const { return getOperand(1); }
412 static unsigned getPointerOperandIndex() { return 1U; }
414 /// \brief Returns the address space of the pointer operand.
415 unsigned getPointerAddressSpace() const {
416 return getPointerOperand()->getType()->getPointerAddressSpace();
419 // Methods for support type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const Instruction *I) {
421 return I->getOpcode() == Instruction::Store;
423 static inline bool classof(const Value *V) {
424 return isa<Instruction>(V) && classof(cast<Instruction>(V));
428 // Shadow Instruction::setInstructionSubclassData with a private forwarding
429 // method so that subclasses cannot accidentally use it.
430 void setInstructionSubclassData(unsigned short D) {
431 Instruction::setInstructionSubclassData(D);
436 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
439 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
441 //===----------------------------------------------------------------------===//
443 //===----------------------------------------------------------------------===//
445 /// FenceInst - an instruction for ordering other memory operations
447 class FenceInst : public Instruction {
448 void *operator new(size_t, unsigned) = delete;
449 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
452 // Note: Instruction needs to be a friend here to call cloneImpl.
453 friend class Instruction;
454 FenceInst *cloneImpl() const;
457 // allocate space for exactly zero operands
458 void *operator new(size_t s) {
459 return User::operator new(s, 0);
462 // Ordering may only be Acquire, Release, AcquireRelease, or
463 // SequentiallyConsistent.
464 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
465 SynchronizationScope SynchScope = CrossThread,
466 Instruction *InsertBefore = nullptr);
467 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
468 SynchronizationScope SynchScope,
469 BasicBlock *InsertAtEnd);
471 /// Returns the ordering effect of this fence.
472 AtomicOrdering getOrdering() const {
473 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
476 /// Set the ordering constraint on this fence. May only be Acquire, Release,
477 /// AcquireRelease, or SequentiallyConsistent.
478 void setOrdering(AtomicOrdering Ordering) {
479 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
483 SynchronizationScope getSynchScope() const {
484 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
487 /// Specify whether this fence orders other operations with respect to all
488 /// concurrently executing threads, or only with respect to signal handlers
489 /// executing in the same thread.
490 void setSynchScope(SynchronizationScope xthread) {
491 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
495 // Methods for support type inquiry through isa, cast, and dyn_cast:
496 static inline bool classof(const Instruction *I) {
497 return I->getOpcode() == Instruction::Fence;
499 static inline bool classof(const Value *V) {
500 return isa<Instruction>(V) && classof(cast<Instruction>(V));
504 // Shadow Instruction::setInstructionSubclassData with a private forwarding
505 // method so that subclasses cannot accidentally use it.
506 void setInstructionSubclassData(unsigned short D) {
507 Instruction::setInstructionSubclassData(D);
511 //===----------------------------------------------------------------------===//
512 // AtomicCmpXchgInst Class
513 //===----------------------------------------------------------------------===//
515 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
516 /// specified value is in a memory location, and, if it is, stores a new value
517 /// there. Returns the value that was loaded.
519 class AtomicCmpXchgInst : public Instruction {
520 void *operator new(size_t, unsigned) = delete;
521 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
522 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
523 SynchronizationScope SynchScope);
526 // Note: Instruction needs to be a friend here to call cloneImpl.
527 friend class Instruction;
528 AtomicCmpXchgInst *cloneImpl() const;
531 // allocate space for exactly three operands
532 void *operator new(size_t s) {
533 return User::operator new(s, 3);
535 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
536 AtomicOrdering SuccessOrdering,
537 AtomicOrdering FailureOrdering,
538 SynchronizationScope SynchScope,
539 Instruction *InsertBefore = nullptr);
540 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
541 AtomicOrdering SuccessOrdering,
542 AtomicOrdering FailureOrdering,
543 SynchronizationScope SynchScope,
544 BasicBlock *InsertAtEnd);
546 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
549 bool isVolatile() const {
550 return getSubclassDataFromInstruction() & 1;
553 /// setVolatile - Specify whether this is a volatile cmpxchg.
555 void setVolatile(bool V) {
556 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
560 /// Return true if this cmpxchg may spuriously fail.
561 bool isWeak() const {
562 return getSubclassDataFromInstruction() & 0x100;
565 void setWeak(bool IsWeak) {
566 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
570 /// Transparently provide more efficient getOperand methods.
571 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
573 /// Set the ordering constraint on this cmpxchg.
574 void setSuccessOrdering(AtomicOrdering Ordering) {
575 assert(Ordering != NotAtomic &&
576 "CmpXchg instructions can only be atomic.");
577 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
581 void setFailureOrdering(AtomicOrdering Ordering) {
582 assert(Ordering != NotAtomic &&
583 "CmpXchg instructions can only be atomic.");
584 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
588 /// Specify whether this cmpxchg is atomic and orders other operations with
589 /// respect to all concurrently executing threads, or only with respect to
590 /// signal handlers executing in the same thread.
591 void setSynchScope(SynchronizationScope SynchScope) {
592 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
596 /// Returns the ordering constraint on this cmpxchg.
597 AtomicOrdering getSuccessOrdering() const {
598 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
601 /// Returns the ordering constraint on this cmpxchg.
602 AtomicOrdering getFailureOrdering() const {
603 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
606 /// Returns whether this cmpxchg is atomic between threads or only within a
608 SynchronizationScope getSynchScope() const {
609 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
612 Value *getPointerOperand() { return getOperand(0); }
613 const Value *getPointerOperand() const { return getOperand(0); }
614 static unsigned getPointerOperandIndex() { return 0U; }
616 Value *getCompareOperand() { return getOperand(1); }
617 const Value *getCompareOperand() const { return getOperand(1); }
619 Value *getNewValOperand() { return getOperand(2); }
620 const Value *getNewValOperand() const { return getOperand(2); }
622 /// \brief Returns the address space of the pointer operand.
623 unsigned getPointerAddressSpace() const {
624 return getPointerOperand()->getType()->getPointerAddressSpace();
627 /// \brief Returns the strongest permitted ordering on failure, given the
628 /// desired ordering on success.
630 /// If the comparison in a cmpxchg operation fails, there is no atomic store
631 /// so release semantics cannot be provided. So this function drops explicit
632 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
633 /// operation would remain SequentiallyConsistent.
634 static AtomicOrdering
635 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
636 switch (SuccessOrdering) {
637 default: llvm_unreachable("invalid cmpxchg success ordering");
644 case SequentiallyConsistent:
645 return SequentiallyConsistent;
649 // Methods for support type inquiry through isa, cast, and dyn_cast:
650 static inline bool classof(const Instruction *I) {
651 return I->getOpcode() == Instruction::AtomicCmpXchg;
653 static inline bool classof(const Value *V) {
654 return isa<Instruction>(V) && classof(cast<Instruction>(V));
658 // Shadow Instruction::setInstructionSubclassData with a private forwarding
659 // method so that subclasses cannot accidentally use it.
660 void setInstructionSubclassData(unsigned short D) {
661 Instruction::setInstructionSubclassData(D);
666 struct OperandTraits<AtomicCmpXchgInst> :
667 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
670 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
672 //===----------------------------------------------------------------------===//
673 // AtomicRMWInst Class
674 //===----------------------------------------------------------------------===//
676 /// AtomicRMWInst - an instruction that atomically reads a memory location,
677 /// combines it with another value, and then stores the result back. Returns
680 class AtomicRMWInst : public Instruction {
681 void *operator new(size_t, unsigned) = delete;
684 // Note: Instruction needs to be a friend here to call cloneImpl.
685 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 // allocate space for exactly two operands
723 void *operator new(size_t s) {
724 return User::operator new(s, 2);
726 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
727 AtomicOrdering Ordering, SynchronizationScope SynchScope,
728 Instruction *InsertBefore = nullptr);
729 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
730 AtomicOrdering Ordering, SynchronizationScope SynchScope,
731 BasicBlock *InsertAtEnd);
733 BinOp getOperation() const {
734 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
737 void setOperation(BinOp Operation) {
738 unsigned short SubclassData = getSubclassDataFromInstruction();
739 setInstructionSubclassData((SubclassData & 31) |
743 /// isVolatile - Return true if this is a RMW on a volatile memory location.
745 bool isVolatile() const {
746 return getSubclassDataFromInstruction() & 1;
749 /// setVolatile - Specify whether this is a volatile RMW or not.
751 void setVolatile(bool V) {
752 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
756 /// Transparently provide more efficient getOperand methods.
757 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
759 /// Set the ordering constraint on this RMW.
760 void setOrdering(AtomicOrdering Ordering) {
761 assert(Ordering != NotAtomic &&
762 "atomicrmw instructions can only be atomic.");
763 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
767 /// Specify whether this RMW orders other operations with respect to all
768 /// concurrently executing threads, or only with respect to signal handlers
769 /// executing in the same thread.
770 void setSynchScope(SynchronizationScope SynchScope) {
771 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
775 /// Returns the ordering constraint on this RMW.
776 AtomicOrdering getOrdering() const {
777 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
780 /// Returns whether this RMW is atomic between threads or only within a
782 SynchronizationScope getSynchScope() const {
783 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
786 Value *getPointerOperand() { return getOperand(0); }
787 const Value *getPointerOperand() const { return getOperand(0); }
788 static unsigned getPointerOperandIndex() { return 0U; }
790 Value *getValOperand() { return getOperand(1); }
791 const Value *getValOperand() const { return getOperand(1); }
793 /// \brief Returns the address space of the pointer operand.
794 unsigned getPointerAddressSpace() const {
795 return getPointerOperand()->getType()->getPointerAddressSpace();
798 // Methods for support type inquiry through isa, cast, and dyn_cast:
799 static inline bool classof(const Instruction *I) {
800 return I->getOpcode() == Instruction::AtomicRMW;
802 static inline bool classof(const Value *V) {
803 return isa<Instruction>(V) && classof(cast<Instruction>(V));
807 void Init(BinOp Operation, Value *Ptr, Value *Val,
808 AtomicOrdering Ordering, SynchronizationScope SynchScope);
809 // Shadow Instruction::setInstructionSubclassData with a private forwarding
810 // method so that subclasses cannot accidentally use it.
811 void setInstructionSubclassData(unsigned short D) {
812 Instruction::setInstructionSubclassData(D);
817 struct OperandTraits<AtomicRMWInst>
818 : public FixedNumOperandTraits<AtomicRMWInst,2> {
821 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
823 //===----------------------------------------------------------------------===//
824 // GetElementPtrInst Class
825 //===----------------------------------------------------------------------===//
827 // checkGEPType - Simple wrapper function to give a better assertion failure
828 // message on bad indexes for a gep instruction.
830 inline Type *checkGEPType(Type *Ty) {
831 assert(Ty && "Invalid GetElementPtrInst indices for type!");
835 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
836 /// access elements of arrays and structs
838 class GetElementPtrInst : public Instruction {
839 Type *SourceElementType;
840 Type *ResultElementType;
842 void anchor() override;
844 GetElementPtrInst(const GetElementPtrInst &GEPI);
845 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
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);
859 // Note: Instruction needs to be a friend here to call cloneImpl.
860 friend class Instruction;
861 GetElementPtrInst *cloneImpl() const;
864 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
865 ArrayRef<Value *> IdxList,
866 const Twine &NameStr = "",
867 Instruction *InsertBefore = nullptr) {
868 unsigned Values = 1 + unsigned(IdxList.size());
871 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
875 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
876 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
877 NameStr, InsertBefore);
879 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
880 ArrayRef<Value *> IdxList,
881 const Twine &NameStr,
882 BasicBlock *InsertAtEnd) {
883 unsigned Values = 1 + unsigned(IdxList.size());
886 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
890 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
891 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
892 NameStr, InsertAtEnd);
895 /// Create an "inbounds" getelementptr. See the documentation for the
896 /// "inbounds" flag in LangRef.html for details.
897 static GetElementPtrInst *CreateInBounds(Value *Ptr,
898 ArrayRef<Value *> IdxList,
899 const Twine &NameStr = "",
900 Instruction *InsertBefore = nullptr){
901 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
903 static GetElementPtrInst *
904 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
905 const Twine &NameStr = "",
906 Instruction *InsertBefore = nullptr) {
907 GetElementPtrInst *GEP =
908 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
909 GEP->setIsInBounds(true);
912 static GetElementPtrInst *CreateInBounds(Value *Ptr,
913 ArrayRef<Value *> IdxList,
914 const Twine &NameStr,
915 BasicBlock *InsertAtEnd) {
916 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
918 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
919 ArrayRef<Value *> IdxList,
920 const Twine &NameStr,
921 BasicBlock *InsertAtEnd) {
922 GetElementPtrInst *GEP =
923 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
924 GEP->setIsInBounds(true);
928 /// Transparently provide more efficient getOperand methods.
929 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
931 // getType - Overload to return most specific sequential type.
932 SequentialType *getType() const {
933 return cast<SequentialType>(Instruction::getType());
936 Type *getSourceElementType() const { return SourceElementType; }
938 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
939 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
941 Type *getResultElementType() const {
942 assert(ResultElementType ==
943 cast<PointerType>(getType()->getScalarType())->getElementType());
944 return ResultElementType;
947 /// \brief Returns the address space of this instruction's pointer type.
948 unsigned getAddressSpace() const {
949 // Note that this is always the same as the pointer operand's address space
950 // and that is cheaper to compute, so cheat here.
951 return getPointerAddressSpace();
954 /// getIndexedType - Returns the type of the element that would be loaded with
955 /// a load instruction with the specified parameters.
957 /// Null is returned if the indices are invalid for the specified
960 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
961 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
962 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
964 inline op_iterator idx_begin() { return op_begin()+1; }
965 inline const_op_iterator idx_begin() const { return op_begin()+1; }
966 inline op_iterator idx_end() { return op_end(); }
967 inline const_op_iterator idx_end() const { return op_end(); }
969 Value *getPointerOperand() {
970 return getOperand(0);
972 const Value *getPointerOperand() const {
973 return getOperand(0);
975 static unsigned getPointerOperandIndex() {
976 return 0U; // get index for modifying correct operand.
979 /// getPointerOperandType - Method to return the pointer operand as a
981 Type *getPointerOperandType() const {
982 return getPointerOperand()->getType();
985 /// \brief Returns the address space of the pointer operand.
986 unsigned getPointerAddressSpace() const {
987 return getPointerOperandType()->getPointerAddressSpace();
990 /// GetGEPReturnType - Returns the pointer type returned by the GEP
991 /// instruction, which may be a vector of pointers.
992 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
993 return getGEPReturnType(
994 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
997 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
998 ArrayRef<Value *> IdxList) {
999 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
1000 Ptr->getType()->getPointerAddressSpace());
1002 if (Ptr->getType()->isVectorTy()) {
1003 unsigned NumElem = Ptr->getType()->getVectorNumElements();
1004 return VectorType::get(PtrTy, NumElem);
1006 for (Value *Index : IdxList)
1007 if (Index->getType()->isVectorTy()) {
1008 unsigned NumElem = Index->getType()->getVectorNumElements();
1009 return VectorType::get(PtrTy, NumElem);
1015 unsigned getNumIndices() const { // Note: always non-negative
1016 return getNumOperands() - 1;
1019 bool hasIndices() const {
1020 return getNumOperands() > 1;
1023 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1024 /// zeros. If so, the result pointer and the first operand have the same
1025 /// value, just potentially different types.
1026 bool hasAllZeroIndices() const;
1028 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1029 /// constant integers. If so, the result pointer and the first operand have
1030 /// a constant offset between them.
1031 bool hasAllConstantIndices() const;
1033 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
1034 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1035 void setIsInBounds(bool b = true);
1037 /// isInBounds - Determine whether the GEP has the inbounds flag.
1038 bool isInBounds() const;
1040 /// \brief Accumulate the constant address offset of this GEP if possible.
1042 /// This routine accepts an APInt into which it will accumulate the constant
1043 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1044 /// all-constant, it returns false and the value of the offset APInt is
1045 /// undefined (it is *not* preserved!). The APInt passed into this routine
1046 /// must be at least as wide as the IntPtr type for the address space of
1047 /// the base GEP pointer.
1048 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1050 // Methods for support type inquiry through isa, cast, and dyn_cast:
1051 static inline bool classof(const Instruction *I) {
1052 return (I->getOpcode() == Instruction::GetElementPtr);
1054 static inline bool classof(const Value *V) {
1055 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1060 struct OperandTraits<GetElementPtrInst> :
1061 public VariadicOperandTraits<GetElementPtrInst, 1> {
1064 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1065 ArrayRef<Value *> IdxList, unsigned Values,
1066 const Twine &NameStr,
1067 Instruction *InsertBefore)
1068 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1069 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1070 Values, InsertBefore),
1071 SourceElementType(PointeeType),
1072 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1073 assert(ResultElementType ==
1074 cast<PointerType>(getType()->getScalarType())->getElementType());
1075 init(Ptr, IdxList, NameStr);
1077 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1078 ArrayRef<Value *> IdxList, unsigned Values,
1079 const Twine &NameStr,
1080 BasicBlock *InsertAtEnd)
1081 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1082 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1083 Values, InsertAtEnd),
1084 SourceElementType(PointeeType),
1085 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1086 assert(ResultElementType ==
1087 cast<PointerType>(getType()->getScalarType())->getElementType());
1088 init(Ptr, IdxList, NameStr);
1091 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1093 //===----------------------------------------------------------------------===//
1095 //===----------------------------------------------------------------------===//
1097 /// This instruction compares its operands according to the predicate given
1098 /// to the constructor. It only operates on integers or pointers. The operands
1099 /// must be identical types.
1100 /// \brief Represent an integer comparison operator.
1101 class ICmpInst: public CmpInst {
1102 void anchor() override;
1105 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1106 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1107 "Invalid ICmp predicate value");
1108 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1109 "Both operands to ICmp instruction are not of the same type!");
1110 // Check that the operands are the right type
1111 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1112 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1113 "Invalid operand types for ICmp instruction");
1117 // Note: Instruction needs to be a friend here to call cloneImpl.
1118 friend class Instruction;
1119 /// \brief Clone an identical ICmpInst
1120 ICmpInst *cloneImpl() const;
1123 /// \brief Constructor with insert-before-instruction semantics.
1125 Instruction *InsertBefore, ///< Where to insert
1126 Predicate pred, ///< The predicate to use for the comparison
1127 Value *LHS, ///< The left-hand-side of the expression
1128 Value *RHS, ///< The right-hand-side of the expression
1129 const Twine &NameStr = "" ///< Name of the instruction
1130 ) : CmpInst(makeCmpResultType(LHS->getType()),
1131 Instruction::ICmp, pred, LHS, RHS, NameStr,
1138 /// \brief Constructor with insert-at-end semantics.
1140 BasicBlock &InsertAtEnd, ///< Block to insert into.
1141 Predicate pred, ///< The predicate to use for the comparison
1142 Value *LHS, ///< The left-hand-side of the expression
1143 Value *RHS, ///< The right-hand-side of the expression
1144 const Twine &NameStr = "" ///< Name of the instruction
1145 ) : CmpInst(makeCmpResultType(LHS->getType()),
1146 Instruction::ICmp, pred, LHS, RHS, NameStr,
1153 /// \brief Constructor with no-insertion semantics
1155 Predicate pred, ///< The predicate to use for the comparison
1156 Value *LHS, ///< The left-hand-side of the expression
1157 Value *RHS, ///< The right-hand-side of the expression
1158 const Twine &NameStr = "" ///< Name of the instruction
1159 ) : CmpInst(makeCmpResultType(LHS->getType()),
1160 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1166 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1167 /// @returns the predicate that would be the result if the operand were
1168 /// regarded as signed.
1169 /// \brief Return the signed version of the predicate
1170 Predicate getSignedPredicate() const {
1171 return getSignedPredicate(getPredicate());
1174 /// This is a static version that you can use without an instruction.
1175 /// \brief Return the signed version of the predicate.
1176 static Predicate getSignedPredicate(Predicate pred);
1178 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1179 /// @returns the predicate that would be the result if the operand were
1180 /// regarded as unsigned.
1181 /// \brief Return the unsigned version of the predicate
1182 Predicate getUnsignedPredicate() const {
1183 return getUnsignedPredicate(getPredicate());
1186 /// This is a static version that you can use without an instruction.
1187 /// \brief Return the unsigned version of the predicate.
1188 static Predicate getUnsignedPredicate(Predicate pred);
1190 /// isEquality - Return true if this predicate is either EQ or NE. This also
1191 /// tests for commutativity.
1192 static bool isEquality(Predicate P) {
1193 return P == ICMP_EQ || P == ICMP_NE;
1196 /// isEquality - Return true if this predicate is either EQ or NE. This also
1197 /// tests for commutativity.
1198 bool isEquality() const {
1199 return isEquality(getPredicate());
1202 /// @returns true if the predicate of this ICmpInst is commutative
1203 /// \brief Determine if this relation is commutative.
1204 bool isCommutative() const { return isEquality(); }
1206 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1208 bool isRelational() const {
1209 return !isEquality();
1212 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1214 static bool isRelational(Predicate P) {
1215 return !isEquality(P);
1218 /// Initialize a set of values that all satisfy the predicate with C.
1219 /// \brief Make a ConstantRange for a relation with a constant value.
1220 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1222 /// Exchange the two operands to this instruction in such a way that it does
1223 /// not modify the semantics of the instruction. The predicate value may be
1224 /// changed to retain the same result if the predicate is order dependent
1226 /// \brief Swap operands and adjust predicate.
1227 void swapOperands() {
1228 setPredicate(getSwappedPredicate());
1229 Op<0>().swap(Op<1>());
1232 // Methods for support type inquiry through isa, cast, and dyn_cast:
1233 static inline bool classof(const Instruction *I) {
1234 return I->getOpcode() == Instruction::ICmp;
1236 static inline bool classof(const Value *V) {
1237 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1241 //===----------------------------------------------------------------------===//
1243 //===----------------------------------------------------------------------===//
1245 /// This instruction compares its operands according to the predicate given
1246 /// to the constructor. It only operates on floating point values or packed
1247 /// vectors of floating point values. The operands must be identical types.
1248 /// \brief Represents a floating point comparison operator.
1249 class FCmpInst: public CmpInst {
1251 // Note: Instruction needs to be a friend here to call cloneImpl.
1252 friend class Instruction;
1253 /// \brief Clone an identical FCmpInst
1254 FCmpInst *cloneImpl() const;
1257 /// \brief Constructor with insert-before-instruction semantics.
1259 Instruction *InsertBefore, ///< Where to insert
1260 Predicate pred, ///< The predicate to use for the comparison
1261 Value *LHS, ///< The left-hand-side of the expression
1262 Value *RHS, ///< The right-hand-side of the expression
1263 const Twine &NameStr = "" ///< Name of the instruction
1264 ) : CmpInst(makeCmpResultType(LHS->getType()),
1265 Instruction::FCmp, pred, LHS, RHS, NameStr,
1267 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1268 "Invalid FCmp predicate value");
1269 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1270 "Both operands to FCmp instruction are not of the same type!");
1271 // Check that the operands are the right type
1272 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1273 "Invalid operand types for FCmp instruction");
1276 /// \brief Constructor with insert-at-end semantics.
1278 BasicBlock &InsertAtEnd, ///< Block to insert into.
1279 Predicate pred, ///< The predicate to use for the comparison
1280 Value *LHS, ///< The left-hand-side of the expression
1281 Value *RHS, ///< The right-hand-side of the expression
1282 const Twine &NameStr = "" ///< Name of the instruction
1283 ) : CmpInst(makeCmpResultType(LHS->getType()),
1284 Instruction::FCmp, pred, LHS, RHS, NameStr,
1286 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1287 "Invalid FCmp predicate value");
1288 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1289 "Both operands to FCmp instruction are not of the same type!");
1290 // Check that the operands are the right type
1291 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1292 "Invalid operand types for FCmp instruction");
1295 /// \brief Constructor with no-insertion semantics
1297 Predicate pred, ///< The predicate to use for the comparison
1298 Value *LHS, ///< The left-hand-side of the expression
1299 Value *RHS, ///< The right-hand-side of the expression
1300 const Twine &NameStr = "" ///< Name of the instruction
1301 ) : CmpInst(makeCmpResultType(LHS->getType()),
1302 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1303 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1304 "Invalid FCmp predicate value");
1305 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1306 "Both operands to FCmp instruction are not of the same type!");
1307 // Check that the operands are the right type
1308 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1309 "Invalid operand types for FCmp instruction");
1312 /// @returns true if the predicate of this instruction is EQ or NE.
1313 /// \brief Determine if this is an equality predicate.
1314 static bool isEquality(Predicate Pred) {
1315 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1319 /// @returns true if the predicate of this instruction is EQ or NE.
1320 /// \brief Determine if this is an equality predicate.
1321 bool isEquality() const { return isEquality(getPredicate()); }
1323 /// @returns true if the predicate of this instruction is commutative.
1324 /// \brief Determine if this is a commutative predicate.
1325 bool isCommutative() const {
1326 return isEquality() ||
1327 getPredicate() == FCMP_FALSE ||
1328 getPredicate() == FCMP_TRUE ||
1329 getPredicate() == FCMP_ORD ||
1330 getPredicate() == FCMP_UNO;
1333 /// @returns true if the predicate is relational (not EQ or NE).
1334 /// \brief Determine if this a relational predicate.
1335 bool isRelational() const { return !isEquality(); }
1337 /// Exchange the two operands to this instruction in such a way that it does
1338 /// not modify the semantics of the instruction. The predicate value may be
1339 /// changed to retain the same result if the predicate is order dependent
1341 /// \brief Swap operands and adjust predicate.
1342 void swapOperands() {
1343 setPredicate(getSwappedPredicate());
1344 Op<0>().swap(Op<1>());
1347 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1348 static inline bool classof(const Instruction *I) {
1349 return I->getOpcode() == Instruction::FCmp;
1351 static inline bool classof(const Value *V) {
1352 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1356 //===----------------------------------------------------------------------===//
1357 /// CallInst - This class represents a function call, abstracting a target
1358 /// machine's calling convention. This class uses low bit of the SubClassData
1359 /// field to indicate whether or not this is a tail call. The rest of the bits
1360 /// hold the calling convention of the call.
1362 class CallInst : public Instruction,
1363 public OperandBundleUser<CallInst, User::op_iterator> {
1364 AttributeSet AttributeList; ///< parameter attributes for call
1366 CallInst(const CallInst &CI);
1367 void init(Value *Func, ArrayRef<Value *> Args,
1368 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
1369 init(cast<FunctionType>(
1370 cast<PointerType>(Func->getType())->getElementType()),
1371 Func, Args, Bundles, NameStr);
1373 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1374 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1375 void init(Value *Func, const Twine &NameStr);
1377 /// Construct a CallInst given a range of arguments.
1378 /// \brief Construct a CallInst from a range of arguments
1379 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1380 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1381 Instruction *InsertBefore);
1382 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1383 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1384 Instruction *InsertBefore)
1385 : CallInst(cast<FunctionType>(
1386 cast<PointerType>(Func->getType())->getElementType()),
1387 Func, Args, Bundles, NameStr, InsertBefore) {}
1389 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1390 Instruction *InsertBefore)
1391 : CallInst(Func, Args, None, NameStr, InsertBefore) {}
1393 /// Construct a CallInst given a range of arguments.
1394 /// \brief Construct a CallInst from a range of arguments
1395 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1396 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1397 BasicBlock *InsertAtEnd);
1399 explicit CallInst(Value *F, const Twine &NameStr,
1400 Instruction *InsertBefore);
1401 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1403 friend class OperandBundleUser<CallInst, User::op_iterator>;
1404 bool hasDescriptor() const { return HasDescriptor; }
1407 // Note: Instruction needs to be a friend here to call cloneImpl.
1408 friend class Instruction;
1409 CallInst *cloneImpl() const;
1412 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1413 ArrayRef<OperandBundleDef> Bundles = None,
1414 const Twine &NameStr = "",
1415 Instruction *InsertBefore = nullptr) {
1416 return Create(cast<FunctionType>(
1417 cast<PointerType>(Func->getType())->getElementType()),
1418 Func, Args, Bundles, NameStr, InsertBefore);
1420 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1421 const Twine &NameStr,
1422 Instruction *InsertBefore = nullptr) {
1423 return Create(cast<FunctionType>(
1424 cast<PointerType>(Func->getType())->getElementType()),
1425 Func, Args, None, NameStr, InsertBefore);
1427 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1428 const Twine &NameStr,
1429 Instruction *InsertBefore = nullptr) {
1430 return new (unsigned(Args.size() + 1))
1431 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1433 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1434 ArrayRef<OperandBundleDef> Bundles = None,
1435 const Twine &NameStr = "",
1436 Instruction *InsertBefore = nullptr) {
1437 const unsigned TotalOps =
1438 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1439 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1441 return new (TotalOps, DescriptorBytes)
1442 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1444 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1445 ArrayRef<OperandBundleDef> Bundles,
1446 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1447 const unsigned TotalOps =
1448 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1449 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1451 return new (TotalOps, DescriptorBytes)
1452 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
1454 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1455 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1456 return new (unsigned(Args.size() + 1))
1457 CallInst(Func, Args, None, NameStr, InsertAtEnd);
1459 static CallInst *Create(Value *F, const Twine &NameStr = "",
1460 Instruction *InsertBefore = nullptr) {
1461 return new(1) CallInst(F, NameStr, InsertBefore);
1463 static CallInst *Create(Value *F, const Twine &NameStr,
1464 BasicBlock *InsertAtEnd) {
1465 return new(1) CallInst(F, NameStr, InsertAtEnd);
1468 /// \brief Create a clone of \p CI with a different set of operand bundles and
1469 /// insert it before \p InsertPt.
1471 /// The returned call instruction is identical \p CI in every way except that
1472 /// the operand bundles for the new instruction are set to the operand bundles
1474 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1475 Instruction *InsertPt = nullptr);
1477 /// CreateMalloc - Generate the IR for a call to malloc:
1478 /// 1. Compute the malloc call's argument as the specified type's size,
1479 /// possibly multiplied by the array size if the array size is not
1481 /// 2. Call malloc with that argument.
1482 /// 3. Bitcast the result of the malloc call to the specified type.
1483 static Instruction *CreateMalloc(Instruction *InsertBefore,
1484 Type *IntPtrTy, Type *AllocTy,
1485 Value *AllocSize, Value *ArraySize = nullptr,
1486 Function* MallocF = nullptr,
1487 const Twine &Name = "");
1488 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1489 Type *IntPtrTy, Type *AllocTy,
1490 Value *AllocSize, Value *ArraySize = nullptr,
1491 Function* MallocF = nullptr,
1492 const Twine &Name = "");
1493 /// CreateFree - Generate the IR for a call to the builtin free function.
1494 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1495 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1497 ~CallInst() override;
1499 FunctionType *getFunctionType() const { return FTy; }
1501 void mutateFunctionType(FunctionType *FTy) {
1502 mutateType(FTy->getReturnType());
1506 // Note that 'musttail' implies 'tail'.
1507 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2,
1509 TailCallKind getTailCallKind() const {
1510 return TailCallKind(getSubclassDataFromInstruction() & 3);
1512 bool isTailCall() const {
1513 unsigned Kind = getSubclassDataFromInstruction() & 3;
1514 return Kind == TCK_Tail || Kind == TCK_MustTail;
1516 bool isMustTailCall() const {
1517 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1519 bool isNoTailCall() const {
1520 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
1522 void setTailCall(bool isTC = true) {
1523 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1524 unsigned(isTC ? TCK_Tail : TCK_None));
1526 void setTailCallKind(TailCallKind TCK) {
1527 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1531 /// Provide fast operand accessors
1532 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1534 /// getNumArgOperands - Return the number of call arguments.
1536 unsigned getNumArgOperands() const {
1537 return getNumOperands() - getNumTotalBundleOperands() - 1;
1540 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1542 Value *getArgOperand(unsigned i) const {
1543 assert(i < getNumArgOperands() && "Out of bounds!");
1544 return getOperand(i);
1546 void setArgOperand(unsigned i, Value *v) {
1547 assert(i < getNumArgOperands() && "Out of bounds!");
1551 /// \brief Return the iterator pointing to the beginning of the argument list.
1552 op_iterator arg_begin() { return op_begin(); }
1554 /// \brief Return the iterator pointing to the end of the argument list.
1555 op_iterator arg_end() {
1556 // [ call args ], [ operand bundles ], callee
1557 return op_end() - getNumTotalBundleOperands() - 1;
1560 /// \brief Iteration adapter for range-for loops.
1561 iterator_range<op_iterator> arg_operands() {
1562 return make_range(arg_begin(), arg_end());
1565 /// \brief Return the iterator pointing to the beginning of the argument list.
1566 const_op_iterator arg_begin() const { return op_begin(); }
1568 /// \brief Return the iterator pointing to the end of the argument list.
1569 const_op_iterator arg_end() const {
1570 // [ call args ], [ operand bundles ], callee
1571 return op_end() - getNumTotalBundleOperands() - 1;
1574 /// \brief Iteration adapter for range-for loops.
1575 iterator_range<const_op_iterator> arg_operands() const {
1576 return make_range(arg_begin(), arg_end());
1579 /// \brief Wrappers for getting the \c Use of a call argument.
1580 const Use &getArgOperandUse(unsigned i) const {
1581 assert(i < getNumArgOperands() && "Out of bounds!");
1582 return getOperandUse(i);
1584 Use &getArgOperandUse(unsigned i) {
1585 assert(i < getNumArgOperands() && "Out of bounds!");
1586 return getOperandUse(i);
1589 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1591 CallingConv::ID getCallingConv() const {
1592 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1594 void setCallingConv(CallingConv::ID CC) {
1595 auto ID = static_cast<unsigned>(CC);
1596 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1597 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1601 /// getAttributes - Return the parameter attributes for this call.
1603 const AttributeSet &getAttributes() const { return AttributeList; }
1605 /// setAttributes - Set the parameter attributes for this call.
1607 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1609 /// addAttribute - adds the attribute to the list of attributes.
1610 void addAttribute(unsigned i, Attribute::AttrKind attr);
1612 /// addAttribute - adds the attribute to the list of attributes.
1613 void addAttribute(unsigned i, StringRef Kind, StringRef Value);
1615 /// removeAttribute - removes the attribute from the list of attributes.
1616 void removeAttribute(unsigned i, Attribute attr);
1618 /// \brief adds the dereferenceable attribute to the list of attributes.
1619 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1621 /// \brief adds the dereferenceable_or_null attribute to the list of
1623 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1625 /// \brief Determine whether this call has the given attribute.
1626 bool hasFnAttr(Attribute::AttrKind A) const {
1627 assert(A != Attribute::NoBuiltin &&
1628 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1629 return hasFnAttrImpl(A);
1632 /// \brief Determine whether this call has the given attribute.
1633 bool hasFnAttr(StringRef A) const {
1634 return hasFnAttrImpl(A);
1637 /// \brief Determine whether the call or the callee has the given attributes.
1638 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1640 /// \brief Return true if the data operand at index \p i has the attribute \p
1643 /// Data operands include call arguments and values used in operand bundles,
1644 /// but does not include the callee operand. This routine dispatches to the
1645 /// underlying AttributeList or the OperandBundleUser as appropriate.
1647 /// The index \p i is interpreted as
1649 /// \p i == Attribute::ReturnIndex -> the return value
1650 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1651 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1652 /// (\p i - 1) in the operand list.
1653 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind A) const;
1655 /// \brief Extract the alignment for a call or parameter (0=unknown).
1656 unsigned getParamAlignment(unsigned i) const {
1657 return AttributeList.getParamAlignment(i);
1660 /// \brief Extract the number of dereferenceable bytes for a call or
1661 /// parameter (0=unknown).
1662 uint64_t getDereferenceableBytes(unsigned i) const {
1663 return AttributeList.getDereferenceableBytes(i);
1666 /// \brief Extract the number of dereferenceable_or_null bytes for a call or
1667 /// parameter (0=unknown).
1668 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1669 return AttributeList.getDereferenceableOrNullBytes(i);
1672 /// @brief Determine if the parameter or return value is marked with NoAlias
1674 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
1675 bool doesNotAlias(unsigned n) const {
1676 return AttributeList.hasAttribute(n, Attribute::NoAlias);
1679 /// \brief Return true if the call should not be treated as a call to a
1681 bool isNoBuiltin() const {
1682 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1683 !hasFnAttrImpl(Attribute::Builtin);
1686 /// \brief Return true if the call should not be inlined.
1687 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1688 void setIsNoInline() {
1689 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1692 /// \brief Return true if the call can return twice
1693 bool canReturnTwice() const {
1694 return hasFnAttr(Attribute::ReturnsTwice);
1696 void setCanReturnTwice() {
1697 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1700 /// \brief Determine if the call does not access memory.
1701 bool doesNotAccessMemory() const {
1702 return hasFnAttr(Attribute::ReadNone);
1704 void setDoesNotAccessMemory() {
1705 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1708 /// \brief Determine if the call does not access or only reads memory.
1709 bool onlyReadsMemory() const {
1710 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1712 void setOnlyReadsMemory() {
1713 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1716 /// @brief Determine if the call can access memmory only using pointers based
1717 /// on its arguments.
1718 bool onlyAccessesArgMemory() const {
1719 return hasFnAttr(Attribute::ArgMemOnly);
1721 void setOnlyAccessesArgMemory() {
1722 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
1725 /// \brief Determine if the call cannot return.
1726 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1727 void setDoesNotReturn() {
1728 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1731 /// \brief Determine if the call cannot unwind.
1732 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1733 void setDoesNotThrow() {
1734 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1737 /// \brief Determine if the call cannot be duplicated.
1738 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1739 void setCannotDuplicate() {
1740 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1743 /// \brief Determine if the call is convergent
1744 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1745 void setConvergent() {
1746 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
1749 /// \brief Determine if the call returns a structure through first
1750 /// pointer argument.
1751 bool hasStructRetAttr() const {
1752 if (getNumArgOperands() == 0)
1755 // Be friendly and also check the callee.
1756 return paramHasAttr(1, Attribute::StructRet);
1759 /// \brief Determine if any call argument is an aggregate passed by value.
1760 bool hasByValArgument() const {
1761 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1764 /// getCalledFunction - Return the function called, or null if this is an
1765 /// indirect function invocation.
1767 Function *getCalledFunction() const {
1768 return dyn_cast<Function>(Op<-1>());
1771 /// getCalledValue - Get a pointer to the function that is invoked by this
1773 const Value *getCalledValue() const { return Op<-1>(); }
1774 Value *getCalledValue() { return Op<-1>(); }
1776 /// setCalledFunction - Set the function called.
1777 void setCalledFunction(Value* Fn) {
1779 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1782 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1784 assert(FTy == cast<FunctionType>(
1785 cast<PointerType>(Fn->getType())->getElementType()));
1789 /// isInlineAsm - Check if this call is an inline asm statement.
1790 bool isInlineAsm() const {
1791 return isa<InlineAsm>(Op<-1>());
1794 // Methods for support type inquiry through isa, cast, and dyn_cast:
1795 static inline bool classof(const Instruction *I) {
1796 return I->getOpcode() == Instruction::Call;
1798 static inline bool classof(const Value *V) {
1799 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1803 template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const {
1804 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
1807 // Operand bundles override attributes on the called function, but don't
1808 // override attributes directly present on the call instruction.
1809 if (isFnAttrDisallowedByOpBundle(A))
1812 if (const Function *F = getCalledFunction())
1813 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
1817 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1818 // method so that subclasses cannot accidentally use it.
1819 void setInstructionSubclassData(unsigned short D) {
1820 Instruction::setInstructionSubclassData(D);
1825 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1828 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1829 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1830 BasicBlock *InsertAtEnd)
1832 cast<FunctionType>(cast<PointerType>(Func->getType())
1833 ->getElementType())->getReturnType(),
1834 Instruction::Call, OperandTraits<CallInst>::op_end(this) -
1835 (Args.size() + CountBundleInputs(Bundles) + 1),
1836 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
1837 init(Func, Args, Bundles, NameStr);
1840 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1841 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1842 Instruction *InsertBefore)
1843 : Instruction(Ty->getReturnType(), Instruction::Call,
1844 OperandTraits<CallInst>::op_end(this) -
1845 (Args.size() + CountBundleInputs(Bundles) + 1),
1846 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1848 init(Ty, Func, Args, Bundles, NameStr);
1851 // Note: if you get compile errors about private methods then
1852 // please update your code to use the high-level operand
1853 // interfaces. See line 943 above.
1854 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1856 //===----------------------------------------------------------------------===//
1858 //===----------------------------------------------------------------------===//
1860 /// SelectInst - This class represents the LLVM 'select' instruction.
1862 class SelectInst : public Instruction {
1863 void init(Value *C, Value *S1, Value *S2) {
1864 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1870 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1871 Instruction *InsertBefore)
1872 : Instruction(S1->getType(), Instruction::Select,
1873 &Op<0>(), 3, InsertBefore) {
1877 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1878 BasicBlock *InsertAtEnd)
1879 : Instruction(S1->getType(), Instruction::Select,
1880 &Op<0>(), 3, InsertAtEnd) {
1886 // Note: Instruction needs to be a friend here to call cloneImpl.
1887 friend class Instruction;
1888 SelectInst *cloneImpl() const;
1891 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1892 const Twine &NameStr = "",
1893 Instruction *InsertBefore = nullptr) {
1894 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1896 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1897 const Twine &NameStr,
1898 BasicBlock *InsertAtEnd) {
1899 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1902 const Value *getCondition() const { return Op<0>(); }
1903 const Value *getTrueValue() const { return Op<1>(); }
1904 const Value *getFalseValue() const { return Op<2>(); }
1905 Value *getCondition() { return Op<0>(); }
1906 Value *getTrueValue() { return Op<1>(); }
1907 Value *getFalseValue() { return Op<2>(); }
1909 /// areInvalidOperands - Return a string if the specified operands are invalid
1910 /// for a select operation, otherwise return null.
1911 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1913 /// Transparently provide more efficient getOperand methods.
1914 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1916 OtherOps getOpcode() const {
1917 return static_cast<OtherOps>(Instruction::getOpcode());
1920 // Methods for support type inquiry through isa, cast, and dyn_cast:
1921 static inline bool classof(const Instruction *I) {
1922 return I->getOpcode() == Instruction::Select;
1924 static inline bool classof(const Value *V) {
1925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1930 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1933 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1935 //===----------------------------------------------------------------------===//
1937 //===----------------------------------------------------------------------===//
1939 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1940 /// an argument of the specified type given a va_list and increments that list
1942 class VAArgInst : public UnaryInstruction {
1944 // Note: Instruction needs to be a friend here to call cloneImpl.
1945 friend class Instruction;
1946 VAArgInst *cloneImpl() const;
1949 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1950 Instruction *InsertBefore = nullptr)
1951 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1954 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1955 BasicBlock *InsertAtEnd)
1956 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1960 Value *getPointerOperand() { return getOperand(0); }
1961 const Value *getPointerOperand() const { return getOperand(0); }
1962 static unsigned getPointerOperandIndex() { return 0U; }
1964 // Methods for support type inquiry through isa, cast, and dyn_cast:
1965 static inline bool classof(const Instruction *I) {
1966 return I->getOpcode() == VAArg;
1968 static inline bool classof(const Value *V) {
1969 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1973 //===----------------------------------------------------------------------===//
1974 // ExtractElementInst Class
1975 //===----------------------------------------------------------------------===//
1977 /// ExtractElementInst - This instruction extracts a single (scalar)
1978 /// element from a VectorType value
1980 class ExtractElementInst : public Instruction {
1981 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1982 Instruction *InsertBefore = nullptr);
1983 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1984 BasicBlock *InsertAtEnd);
1987 // Note: Instruction needs to be a friend here to call cloneImpl.
1988 friend class Instruction;
1989 ExtractElementInst *cloneImpl() const;
1992 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1993 const Twine &NameStr = "",
1994 Instruction *InsertBefore = nullptr) {
1995 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1997 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1998 const Twine &NameStr,
1999 BasicBlock *InsertAtEnd) {
2000 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
2003 /// isValidOperands - Return true if an extractelement instruction can be
2004 /// formed with the specified operands.
2005 static bool isValidOperands(const Value *Vec, const Value *Idx);
2007 Value *getVectorOperand() { return Op<0>(); }
2008 Value *getIndexOperand() { return Op<1>(); }
2009 const Value *getVectorOperand() const { return Op<0>(); }
2010 const Value *getIndexOperand() const { return Op<1>(); }
2012 VectorType *getVectorOperandType() const {
2013 return cast<VectorType>(getVectorOperand()->getType());
2016 /// Transparently provide more efficient getOperand methods.
2017 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2019 // Methods for support type inquiry through isa, cast, and dyn_cast:
2020 static inline bool classof(const Instruction *I) {
2021 return I->getOpcode() == Instruction::ExtractElement;
2023 static inline bool classof(const Value *V) {
2024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2029 struct OperandTraits<ExtractElementInst> :
2030 public FixedNumOperandTraits<ExtractElementInst, 2> {
2033 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
2035 //===----------------------------------------------------------------------===//
2036 // InsertElementInst Class
2037 //===----------------------------------------------------------------------===//
2039 /// InsertElementInst - This instruction inserts a single (scalar)
2040 /// element into a VectorType value
2042 class InsertElementInst : public Instruction {
2043 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
2044 const Twine &NameStr = "",
2045 Instruction *InsertBefore = nullptr);
2046 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
2047 BasicBlock *InsertAtEnd);
2050 // Note: Instruction needs to be a friend here to call cloneImpl.
2051 friend class Instruction;
2052 InsertElementInst *cloneImpl() const;
2055 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2056 const Twine &NameStr = "",
2057 Instruction *InsertBefore = nullptr) {
2058 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2060 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2061 const Twine &NameStr,
2062 BasicBlock *InsertAtEnd) {
2063 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2066 /// isValidOperands - Return true if an insertelement instruction can be
2067 /// formed with the specified operands.
2068 static bool isValidOperands(const Value *Vec, const Value *NewElt,
2071 /// getType - Overload to return most specific vector type.
2073 VectorType *getType() const {
2074 return cast<VectorType>(Instruction::getType());
2077 /// Transparently provide more efficient getOperand methods.
2078 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2080 // Methods for support type inquiry through isa, cast, and dyn_cast:
2081 static inline bool classof(const Instruction *I) {
2082 return I->getOpcode() == Instruction::InsertElement;
2084 static inline bool classof(const Value *V) {
2085 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2090 struct OperandTraits<InsertElementInst> :
2091 public FixedNumOperandTraits<InsertElementInst, 3> {
2094 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2096 //===----------------------------------------------------------------------===//
2097 // ShuffleVectorInst Class
2098 //===----------------------------------------------------------------------===//
2100 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
2103 class ShuffleVectorInst : public Instruction {
2105 // Note: Instruction needs to be a friend here to call cloneImpl.
2106 friend class Instruction;
2107 ShuffleVectorInst *cloneImpl() const;
2110 // allocate space for exactly three operands
2111 void *operator new(size_t s) {
2112 return User::operator new(s, 3);
2114 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2115 const Twine &NameStr = "",
2116 Instruction *InsertBefor = nullptr);
2117 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2118 const Twine &NameStr, BasicBlock *InsertAtEnd);
2120 /// isValidOperands - Return true if a shufflevector instruction can be
2121 /// formed with the specified operands.
2122 static bool isValidOperands(const Value *V1, const Value *V2,
2125 /// getType - Overload to return most specific vector type.
2127 VectorType *getType() const {
2128 return cast<VectorType>(Instruction::getType());
2131 /// Transparently provide more efficient getOperand methods.
2132 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2134 Constant *getMask() const {
2135 return cast<Constant>(getOperand(2));
2138 /// getMaskValue - Return the index from the shuffle mask for the specified
2139 /// output result. This is either -1 if the element is undef or a number less
2140 /// than 2*numelements.
2141 static int getMaskValue(Constant *Mask, unsigned i);
2143 int getMaskValue(unsigned i) const {
2144 return getMaskValue(getMask(), i);
2147 /// getShuffleMask - Return the full mask for this instruction, where each
2148 /// element is the element number and undef's are returned as -1.
2149 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
2151 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2152 return getShuffleMask(getMask(), Result);
2155 SmallVector<int, 16> getShuffleMask() const {
2156 SmallVector<int, 16> Mask;
2157 getShuffleMask(Mask);
2161 // Methods for support type inquiry through isa, cast, and dyn_cast:
2162 static inline bool classof(const Instruction *I) {
2163 return I->getOpcode() == Instruction::ShuffleVector;
2165 static inline bool classof(const Value *V) {
2166 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2171 struct OperandTraits<ShuffleVectorInst> :
2172 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2175 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2177 //===----------------------------------------------------------------------===//
2178 // ExtractValueInst Class
2179 //===----------------------------------------------------------------------===//
2181 /// ExtractValueInst - This instruction extracts a struct member or array
2182 /// element value from an aggregate value.
2184 class ExtractValueInst : public UnaryInstruction {
2185 SmallVector<unsigned, 4> Indices;
2187 ExtractValueInst(const ExtractValueInst &EVI);
2188 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2190 /// Constructors - Create a extractvalue instruction with a base aggregate
2191 /// value and a list of indices. The first ctor can optionally insert before
2192 /// an existing instruction, the second appends the new instruction to the
2193 /// specified BasicBlock.
2194 inline ExtractValueInst(Value *Agg,
2195 ArrayRef<unsigned> Idxs,
2196 const Twine &NameStr,
2197 Instruction *InsertBefore);
2198 inline ExtractValueInst(Value *Agg,
2199 ArrayRef<unsigned> Idxs,
2200 const Twine &NameStr, BasicBlock *InsertAtEnd);
2202 // allocate space for exactly one operand
2203 void *operator new(size_t s) { return User::operator new(s, 1); }
2206 // Note: Instruction needs to be a friend here to call cloneImpl.
2207 friend class Instruction;
2208 ExtractValueInst *cloneImpl() const;
2211 static ExtractValueInst *Create(Value *Agg,
2212 ArrayRef<unsigned> Idxs,
2213 const Twine &NameStr = "",
2214 Instruction *InsertBefore = nullptr) {
2216 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2218 static ExtractValueInst *Create(Value *Agg,
2219 ArrayRef<unsigned> Idxs,
2220 const Twine &NameStr,
2221 BasicBlock *InsertAtEnd) {
2222 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2225 /// getIndexedType - Returns the type of the element that would be extracted
2226 /// with an extractvalue instruction with the specified parameters.
2228 /// Null is returned if the indices are invalid for the specified type.
2229 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2231 typedef const unsigned* idx_iterator;
2232 inline idx_iterator idx_begin() const { return Indices.begin(); }
2233 inline idx_iterator idx_end() const { return Indices.end(); }
2234 inline iterator_range<idx_iterator> indices() const {
2235 return make_range(idx_begin(), idx_end());
2238 Value *getAggregateOperand() {
2239 return getOperand(0);
2241 const Value *getAggregateOperand() const {
2242 return getOperand(0);
2244 static unsigned getAggregateOperandIndex() {
2245 return 0U; // get index for modifying correct operand
2248 ArrayRef<unsigned> getIndices() const {
2252 unsigned getNumIndices() const {
2253 return (unsigned)Indices.size();
2256 bool hasIndices() const {
2260 // Methods for support type inquiry through isa, cast, and dyn_cast:
2261 static inline bool classof(const Instruction *I) {
2262 return I->getOpcode() == Instruction::ExtractValue;
2264 static inline bool classof(const Value *V) {
2265 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2269 ExtractValueInst::ExtractValueInst(Value *Agg,
2270 ArrayRef<unsigned> Idxs,
2271 const Twine &NameStr,
2272 Instruction *InsertBefore)
2273 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2274 ExtractValue, Agg, InsertBefore) {
2275 init(Idxs, NameStr);
2277 ExtractValueInst::ExtractValueInst(Value *Agg,
2278 ArrayRef<unsigned> Idxs,
2279 const Twine &NameStr,
2280 BasicBlock *InsertAtEnd)
2281 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2282 ExtractValue, Agg, InsertAtEnd) {
2283 init(Idxs, NameStr);
2286 //===----------------------------------------------------------------------===//
2287 // InsertValueInst Class
2288 //===----------------------------------------------------------------------===//
2290 /// InsertValueInst - This instruction inserts a struct field of array element
2291 /// value into an aggregate value.
2293 class InsertValueInst : public Instruction {
2294 SmallVector<unsigned, 4> Indices;
2296 void *operator new(size_t, unsigned) = delete;
2297 InsertValueInst(const InsertValueInst &IVI);
2298 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2299 const Twine &NameStr);
2301 /// Constructors - Create a insertvalue instruction with a base aggregate
2302 /// value, a value to insert, and a list of indices. The first ctor can
2303 /// optionally insert before an existing instruction, the second appends
2304 /// the new instruction to the specified BasicBlock.
2305 inline InsertValueInst(Value *Agg, Value *Val,
2306 ArrayRef<unsigned> Idxs,
2307 const Twine &NameStr,
2308 Instruction *InsertBefore);
2309 inline InsertValueInst(Value *Agg, Value *Val,
2310 ArrayRef<unsigned> Idxs,
2311 const Twine &NameStr, BasicBlock *InsertAtEnd);
2313 /// Constructors - These two constructors are convenience methods because one
2314 /// and two index insertvalue instructions are so common.
2315 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2316 const Twine &NameStr = "",
2317 Instruction *InsertBefore = nullptr);
2318 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2319 BasicBlock *InsertAtEnd);
2322 // Note: Instruction needs to be a friend here to call cloneImpl.
2323 friend class Instruction;
2324 InsertValueInst *cloneImpl() const;
2327 // allocate space for exactly two operands
2328 void *operator new(size_t s) {
2329 return User::operator new(s, 2);
2332 static InsertValueInst *Create(Value *Agg, Value *Val,
2333 ArrayRef<unsigned> Idxs,
2334 const Twine &NameStr = "",
2335 Instruction *InsertBefore = nullptr) {
2336 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2338 static InsertValueInst *Create(Value *Agg, Value *Val,
2339 ArrayRef<unsigned> Idxs,
2340 const Twine &NameStr,
2341 BasicBlock *InsertAtEnd) {
2342 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2345 /// Transparently provide more efficient getOperand methods.
2346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2348 typedef const unsigned* idx_iterator;
2349 inline idx_iterator idx_begin() const { return Indices.begin(); }
2350 inline idx_iterator idx_end() const { return Indices.end(); }
2351 inline iterator_range<idx_iterator> indices() const {
2352 return make_range(idx_begin(), idx_end());
2355 Value *getAggregateOperand() {
2356 return getOperand(0);
2358 const Value *getAggregateOperand() const {
2359 return getOperand(0);
2361 static unsigned getAggregateOperandIndex() {
2362 return 0U; // get index for modifying correct operand
2365 Value *getInsertedValueOperand() {
2366 return getOperand(1);
2368 const Value *getInsertedValueOperand() const {
2369 return getOperand(1);
2371 static unsigned getInsertedValueOperandIndex() {
2372 return 1U; // get index for modifying correct operand
2375 ArrayRef<unsigned> getIndices() const {
2379 unsigned getNumIndices() const {
2380 return (unsigned)Indices.size();
2383 bool hasIndices() const {
2387 // Methods for support type inquiry through isa, cast, and dyn_cast:
2388 static inline bool classof(const Instruction *I) {
2389 return I->getOpcode() == Instruction::InsertValue;
2391 static inline bool classof(const Value *V) {
2392 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2397 struct OperandTraits<InsertValueInst> :
2398 public FixedNumOperandTraits<InsertValueInst, 2> {
2401 InsertValueInst::InsertValueInst(Value *Agg,
2403 ArrayRef<unsigned> Idxs,
2404 const Twine &NameStr,
2405 Instruction *InsertBefore)
2406 : Instruction(Agg->getType(), InsertValue,
2407 OperandTraits<InsertValueInst>::op_begin(this),
2409 init(Agg, Val, Idxs, NameStr);
2411 InsertValueInst::InsertValueInst(Value *Agg,
2413 ArrayRef<unsigned> Idxs,
2414 const Twine &NameStr,
2415 BasicBlock *InsertAtEnd)
2416 : Instruction(Agg->getType(), InsertValue,
2417 OperandTraits<InsertValueInst>::op_begin(this),
2419 init(Agg, Val, Idxs, NameStr);
2422 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2424 //===----------------------------------------------------------------------===//
2426 //===----------------------------------------------------------------------===//
2428 // PHINode - The PHINode class is used to represent the magical mystical PHI
2429 // node, that can not exist in nature, but can be synthesized in a computer
2430 // scientist's overactive imagination.
2432 class PHINode : public Instruction {
2433 void anchor() override;
2435 void *operator new(size_t, unsigned) = delete;
2436 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2437 /// the number actually in use.
2438 unsigned ReservedSpace;
2439 PHINode(const PHINode &PN);
2440 // allocate space for exactly zero operands
2441 void *operator new(size_t s) {
2442 return User::operator new(s);
2444 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2445 const Twine &NameStr = "",
2446 Instruction *InsertBefore = nullptr)
2447 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2448 ReservedSpace(NumReservedValues) {
2450 allocHungoffUses(ReservedSpace);
2453 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2454 BasicBlock *InsertAtEnd)
2455 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2456 ReservedSpace(NumReservedValues) {
2458 allocHungoffUses(ReservedSpace);
2462 // allocHungoffUses - this is more complicated than the generic
2463 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2464 // values and pointers to the incoming blocks, all in one allocation.
2465 void allocHungoffUses(unsigned N) {
2466 User::allocHungoffUses(N, /* IsPhi */ true);
2469 // Note: Instruction needs to be a friend here to call cloneImpl.
2470 friend class Instruction;
2471 PHINode *cloneImpl() const;
2474 /// Constructors - NumReservedValues is a hint for the number of incoming
2475 /// edges that this phi node will have (use 0 if you really have no idea).
2476 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2477 const Twine &NameStr = "",
2478 Instruction *InsertBefore = nullptr) {
2479 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2481 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2482 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2483 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2486 /// Provide fast operand accessors
2487 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2489 // Block iterator interface. This provides access to the list of incoming
2490 // basic blocks, which parallels the list of incoming values.
2492 typedef BasicBlock **block_iterator;
2493 typedef BasicBlock * const *const_block_iterator;
2495 block_iterator block_begin() {
2497 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2498 return reinterpret_cast<block_iterator>(ref + 1);
2501 const_block_iterator block_begin() const {
2502 const Use::UserRef *ref =
2503 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2504 return reinterpret_cast<const_block_iterator>(ref + 1);
2507 block_iterator block_end() {
2508 return block_begin() + getNumOperands();
2511 const_block_iterator block_end() const {
2512 return block_begin() + getNumOperands();
2515 op_range incoming_values() { return operands(); }
2517 const_op_range incoming_values() const { return operands(); }
2519 /// getNumIncomingValues - Return the number of incoming edges
2521 unsigned getNumIncomingValues() const { return getNumOperands(); }
2523 /// getIncomingValue - Return incoming value number x
2525 Value *getIncomingValue(unsigned i) const {
2526 return getOperand(i);
2528 void setIncomingValue(unsigned i, Value *V) {
2529 assert(V && "PHI node got a null value!");
2530 assert(getType() == V->getType() &&
2531 "All operands to PHI node must be the same type as the PHI node!");
2534 static unsigned getOperandNumForIncomingValue(unsigned i) {
2537 static unsigned getIncomingValueNumForOperand(unsigned i) {
2541 /// getIncomingBlock - Return incoming basic block number @p i.
2543 BasicBlock *getIncomingBlock(unsigned i) const {
2544 return block_begin()[i];
2547 /// getIncomingBlock - Return incoming basic block corresponding
2548 /// to an operand of the PHI.
2550 BasicBlock *getIncomingBlock(const Use &U) const {
2551 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2552 return getIncomingBlock(unsigned(&U - op_begin()));
2555 /// getIncomingBlock - Return incoming basic block corresponding
2556 /// to value use iterator.
2558 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2559 return getIncomingBlock(I.getUse());
2562 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2563 assert(BB && "PHI node got a null basic block!");
2564 block_begin()[i] = BB;
2567 /// addIncoming - Add an incoming value to the end of the PHI list
2569 void addIncoming(Value *V, BasicBlock *BB) {
2570 if (getNumOperands() == ReservedSpace)
2571 growOperands(); // Get more space!
2572 // Initialize some new operands.
2573 setNumHungOffUseOperands(getNumOperands() + 1);
2574 setIncomingValue(getNumOperands() - 1, V);
2575 setIncomingBlock(getNumOperands() - 1, BB);
2578 /// removeIncomingValue - Remove an incoming value. This is useful if a
2579 /// predecessor basic block is deleted. The value removed is returned.
2581 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2582 /// is true), the PHI node is destroyed and any uses of it are replaced with
2583 /// dummy values. The only time there should be zero incoming values to a PHI
2584 /// node is when the block is dead, so this strategy is sound.
2586 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2588 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2589 int Idx = getBasicBlockIndex(BB);
2590 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2591 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2594 /// getBasicBlockIndex - Return the first index of the specified basic
2595 /// block in the value list for this PHI. Returns -1 if no instance.
2597 int getBasicBlockIndex(const BasicBlock *BB) const {
2598 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2599 if (block_begin()[i] == BB)
2604 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2605 int Idx = getBasicBlockIndex(BB);
2606 assert(Idx >= 0 && "Invalid basic block argument!");
2607 return getIncomingValue(Idx);
2610 /// hasConstantValue - If the specified PHI node always merges together the
2611 /// same value, return the value, otherwise return null.
2612 Value *hasConstantValue() const;
2614 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2615 static inline bool classof(const Instruction *I) {
2616 return I->getOpcode() == Instruction::PHI;
2618 static inline bool classof(const Value *V) {
2619 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2623 void growOperands();
2627 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2630 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2632 //===----------------------------------------------------------------------===//
2633 // LandingPadInst Class
2634 //===----------------------------------------------------------------------===//
2636 //===---------------------------------------------------------------------------
2637 /// LandingPadInst - The landingpad instruction holds all of the information
2638 /// necessary to generate correct exception handling. The landingpad instruction
2639 /// cannot be moved from the top of a landing pad block, which itself is
2640 /// accessible only from the 'unwind' edge of an invoke. This uses the
2641 /// SubclassData field in Value to store whether or not the landingpad is a
2644 class LandingPadInst : public Instruction {
2645 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2646 /// the number actually in use.
2647 unsigned ReservedSpace;
2648 LandingPadInst(const LandingPadInst &LP);
2651 enum ClauseType { Catch, Filter };
2654 void *operator new(size_t, unsigned) = delete;
2655 // Allocate space for exactly zero operands.
2656 void *operator new(size_t s) {
2657 return User::operator new(s);
2659 void growOperands(unsigned Size);
2660 void init(unsigned NumReservedValues, const Twine &NameStr);
2662 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2663 const Twine &NameStr, Instruction *InsertBefore);
2664 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2665 const Twine &NameStr, BasicBlock *InsertAtEnd);
2668 // Note: Instruction needs to be a friend here to call cloneImpl.
2669 friend class Instruction;
2670 LandingPadInst *cloneImpl() const;
2673 /// Constructors - NumReservedClauses is a hint for the number of incoming
2674 /// clauses that this landingpad will have (use 0 if you really have no idea).
2675 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2676 const Twine &NameStr = "",
2677 Instruction *InsertBefore = nullptr);
2678 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2679 const Twine &NameStr, BasicBlock *InsertAtEnd);
2681 /// Provide fast operand accessors
2682 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2684 /// isCleanup - Return 'true' if this landingpad instruction is a
2685 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2686 /// doesn't catch the exception.
2687 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2689 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2690 void setCleanup(bool V) {
2691 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2695 /// Add a catch or filter clause to the landing pad.
2696 void addClause(Constant *ClauseVal);
2698 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2699 /// determine what type of clause this is.
2700 Constant *getClause(unsigned Idx) const {
2701 return cast<Constant>(getOperandList()[Idx]);
2704 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2705 bool isCatch(unsigned Idx) const {
2706 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2709 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2710 bool isFilter(unsigned Idx) const {
2711 return isa<ArrayType>(getOperandList()[Idx]->getType());
2714 /// getNumClauses - Get the number of clauses for this landing pad.
2715 unsigned getNumClauses() const { return getNumOperands(); }
2717 /// reserveClauses - Grow the size of the operand list to accommodate the new
2718 /// number of clauses.
2719 void reserveClauses(unsigned Size) { growOperands(Size); }
2721 // Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const Instruction *I) {
2723 return I->getOpcode() == Instruction::LandingPad;
2725 static inline bool classof(const Value *V) {
2726 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2731 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2734 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2736 //===----------------------------------------------------------------------===//
2738 //===----------------------------------------------------------------------===//
2740 //===---------------------------------------------------------------------------
2741 /// ReturnInst - Return a value (possibly void), from a function. Execution
2742 /// does not continue in this function any longer.
2744 class ReturnInst : public TerminatorInst {
2745 ReturnInst(const ReturnInst &RI);
2748 // ReturnInst constructors:
2749 // ReturnInst() - 'ret void' instruction
2750 // ReturnInst( null) - 'ret void' instruction
2751 // ReturnInst(Value* X) - 'ret X' instruction
2752 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2753 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2754 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2755 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2757 // NOTE: If the Value* passed is of type void then the constructor behaves as
2758 // if it was passed NULL.
2759 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2760 Instruction *InsertBefore = nullptr);
2761 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2762 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2765 // Note: Instruction needs to be a friend here to call cloneImpl.
2766 friend class Instruction;
2767 ReturnInst *cloneImpl() const;
2770 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2771 Instruction *InsertBefore = nullptr) {
2772 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2774 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2775 BasicBlock *InsertAtEnd) {
2776 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2778 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2779 return new(0) ReturnInst(C, InsertAtEnd);
2781 ~ReturnInst() override;
2783 /// Provide fast operand accessors
2784 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2786 /// Convenience accessor. Returns null if there is no return value.
2787 Value *getReturnValue() const {
2788 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2791 unsigned getNumSuccessors() const { return 0; }
2793 // Methods for support type inquiry through isa, cast, and dyn_cast:
2794 static inline bool classof(const Instruction *I) {
2795 return (I->getOpcode() == Instruction::Ret);
2797 static inline bool classof(const Value *V) {
2798 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2802 BasicBlock *getSuccessorV(unsigned idx) const override;
2803 unsigned getNumSuccessorsV() const override;
2804 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2808 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2811 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2813 //===----------------------------------------------------------------------===//
2815 //===----------------------------------------------------------------------===//
2817 //===---------------------------------------------------------------------------
2818 /// BranchInst - Conditional or Unconditional Branch instruction.
2820 class BranchInst : public TerminatorInst {
2821 /// Ops list - Branches are strange. The operands are ordered:
2822 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2823 /// they don't have to check for cond/uncond branchness. These are mostly
2824 /// accessed relative from op_end().
2825 BranchInst(const BranchInst &BI);
2827 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2828 // BranchInst(BB *B) - 'br B'
2829 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2830 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2831 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2832 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2833 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2834 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2835 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2836 Instruction *InsertBefore = nullptr);
2837 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2838 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2839 BasicBlock *InsertAtEnd);
2842 // Note: Instruction needs to be a friend here to call cloneImpl.
2843 friend class Instruction;
2844 BranchInst *cloneImpl() const;
2847 static BranchInst *Create(BasicBlock *IfTrue,
2848 Instruction *InsertBefore = nullptr) {
2849 return new(1) BranchInst(IfTrue, InsertBefore);
2851 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2852 Value *Cond, Instruction *InsertBefore = nullptr) {
2853 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2855 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2856 return new(1) BranchInst(IfTrue, InsertAtEnd);
2858 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2859 Value *Cond, BasicBlock *InsertAtEnd) {
2860 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2863 /// Transparently provide more efficient getOperand methods.
2864 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2866 bool isUnconditional() const { return getNumOperands() == 1; }
2867 bool isConditional() const { return getNumOperands() == 3; }
2869 Value *getCondition() const {
2870 assert(isConditional() && "Cannot get condition of an uncond branch!");
2874 void setCondition(Value *V) {
2875 assert(isConditional() && "Cannot set condition of unconditional branch!");
2879 unsigned getNumSuccessors() const { return 1+isConditional(); }
2881 BasicBlock *getSuccessor(unsigned i) const {
2882 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2883 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2886 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2887 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2888 *(&Op<-1>() - idx) = NewSucc;
2891 /// \brief Swap the successors of this branch instruction.
2893 /// Swaps the successors of the branch instruction. This also swaps any
2894 /// branch weight metadata associated with the instruction so that it
2895 /// continues to map correctly to each operand.
2896 void swapSuccessors();
2898 // Methods for support type inquiry through isa, cast, and dyn_cast:
2899 static inline bool classof(const Instruction *I) {
2900 return (I->getOpcode() == Instruction::Br);
2902 static inline bool classof(const Value *V) {
2903 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2907 BasicBlock *getSuccessorV(unsigned idx) const override;
2908 unsigned getNumSuccessorsV() const override;
2909 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2913 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2916 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2918 //===----------------------------------------------------------------------===//
2920 //===----------------------------------------------------------------------===//
2922 //===---------------------------------------------------------------------------
2923 /// SwitchInst - Multiway switch
2925 class SwitchInst : public TerminatorInst {
2926 void *operator new(size_t, unsigned) = delete;
2927 unsigned ReservedSpace;
2928 // Operand[0] = Value to switch on
2929 // Operand[1] = Default basic block destination
2930 // Operand[2n ] = Value to match
2931 // Operand[2n+1] = BasicBlock to go to on match
2932 SwitchInst(const SwitchInst &SI);
2933 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2934 void growOperands();
2935 // allocate space for exactly zero operands
2936 void *operator new(size_t s) {
2937 return User::operator new(s);
2939 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2940 /// switch on and a default destination. The number of additional cases can
2941 /// be specified here to make memory allocation more efficient. This
2942 /// constructor can also autoinsert before another instruction.
2943 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2944 Instruction *InsertBefore);
2946 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2947 /// switch on and a default destination. The number of additional cases can
2948 /// be specified here to make memory allocation more efficient. This
2949 /// constructor also autoinserts at the end of the specified BasicBlock.
2950 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2951 BasicBlock *InsertAtEnd);
2954 // Note: Instruction needs to be a friend here to call cloneImpl.
2955 friend class Instruction;
2956 SwitchInst *cloneImpl() const;
2960 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2962 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2963 class CaseIteratorT {
2969 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2971 /// Initializes case iterator for given SwitchInst and for given
2973 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2978 /// Initializes case iterator for given SwitchInst and for given
2979 /// TerminatorInst's successor index.
2980 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2981 assert(SuccessorIndex < SI->getNumSuccessors() &&
2982 "Successor index # out of range!");
2983 return SuccessorIndex != 0 ?
2984 Self(SI, SuccessorIndex - 1) :
2985 Self(SI, DefaultPseudoIndex);
2988 /// Resolves case value for current case.
2989 ConstantIntTy *getCaseValue() {
2990 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2991 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2994 /// Resolves successor for current case.
2995 BasicBlockTy *getCaseSuccessor() {
2996 assert((Index < SI->getNumCases() ||
2997 Index == DefaultPseudoIndex) &&
2998 "Index out the number of cases.");
2999 return SI->getSuccessor(getSuccessorIndex());
3002 /// Returns number of current case.
3003 unsigned getCaseIndex() const { return Index; }
3005 /// Returns TerminatorInst's successor index for current case successor.
3006 unsigned getSuccessorIndex() const {
3007 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
3008 "Index out the number of cases.");
3009 return Index != DefaultPseudoIndex ? Index + 1 : 0;
3013 // Check index correctness after increment.
3014 // Note: Index == getNumCases() means end().
3015 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
3019 Self operator++(int) {
3025 // Check index correctness after decrement.
3026 // Note: Index == getNumCases() means end().
3027 // Also allow "-1" iterator here. That will became valid after ++.
3028 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
3029 "Index out the number of cases.");
3033 Self operator--(int) {
3038 bool operator==(const Self& RHS) const {
3039 assert(RHS.SI == SI && "Incompatible operators.");
3040 return RHS.Index == Index;
3042 bool operator!=(const Self& RHS) const {
3043 assert(RHS.SI == SI && "Incompatible operators.");
3044 return RHS.Index != Index;
3051 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
3054 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
3056 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
3059 CaseIt(const ParentTy &Src) : ParentTy(Src) {}
3060 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
3062 /// Sets the new value for current case.
3063 void setValue(ConstantInt *V) {
3064 assert(Index < SI->getNumCases() && "Index out the number of cases.");
3065 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3068 /// Sets the new successor for current case.
3069 void setSuccessor(BasicBlock *S) {
3070 SI->setSuccessor(getSuccessorIndex(), S);
3074 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3076 Instruction *InsertBefore = nullptr) {
3077 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3079 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3080 unsigned NumCases, BasicBlock *InsertAtEnd) {
3081 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3084 /// Provide fast operand accessors
3085 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3087 // Accessor Methods for Switch stmt
3088 Value *getCondition() const { return getOperand(0); }
3089 void setCondition(Value *V) { setOperand(0, V); }
3091 BasicBlock *getDefaultDest() const {
3092 return cast<BasicBlock>(getOperand(1));
3095 void setDefaultDest(BasicBlock *DefaultCase) {
3096 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3099 /// getNumCases - return the number of 'cases' in this switch instruction,
3100 /// except the default case
3101 unsigned getNumCases() const {
3102 return getNumOperands()/2 - 1;
3105 /// Returns a read/write iterator that points to the first
3106 /// case in SwitchInst.
3107 CaseIt case_begin() {
3108 return CaseIt(this, 0);
3110 /// Returns a read-only iterator that points to the first
3111 /// case in the SwitchInst.
3112 ConstCaseIt case_begin() const {
3113 return ConstCaseIt(this, 0);
3116 /// Returns a read/write iterator that points one past the last
3117 /// in the SwitchInst.
3119 return CaseIt(this, getNumCases());
3121 /// Returns a read-only iterator that points one past the last
3122 /// in the SwitchInst.
3123 ConstCaseIt case_end() const {
3124 return ConstCaseIt(this, getNumCases());
3127 /// cases - iteration adapter for range-for loops.
3128 iterator_range<CaseIt> cases() {
3129 return make_range(case_begin(), case_end());
3132 /// cases - iteration adapter for range-for loops.
3133 iterator_range<ConstCaseIt> cases() const {
3134 return make_range(case_begin(), case_end());
3137 /// Returns an iterator that points to the default case.
3138 /// Note: this iterator allows to resolve successor only. Attempt
3139 /// to resolve case value causes an assertion.
3140 /// Also note, that increment and decrement also causes an assertion and
3141 /// makes iterator invalid.
3142 CaseIt case_default() {
3143 return CaseIt(this, DefaultPseudoIndex);
3145 ConstCaseIt case_default() const {
3146 return ConstCaseIt(this, DefaultPseudoIndex);
3149 /// findCaseValue - Search all of the case values for the specified constant.
3150 /// If it is explicitly handled, return the case iterator of it, otherwise
3151 /// return default case iterator to indicate
3152 /// that it is handled by the default handler.
3153 CaseIt findCaseValue(const ConstantInt *C) {
3154 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
3155 if (i.getCaseValue() == C)
3157 return case_default();
3159 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3160 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
3161 if (i.getCaseValue() == C)
3163 return case_default();
3166 /// findCaseDest - Finds the unique case value for a given successor. Returns
3167 /// null if the successor is not found, not unique, or is the default case.
3168 ConstantInt *findCaseDest(BasicBlock *BB) {
3169 if (BB == getDefaultDest()) return nullptr;
3171 ConstantInt *CI = nullptr;
3172 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
3173 if (i.getCaseSuccessor() == BB) {
3174 if (CI) return nullptr; // Multiple cases lead to BB.
3175 else CI = i.getCaseValue();
3181 /// addCase - Add an entry to the switch instruction...
3183 /// This action invalidates case_end(). Old case_end() iterator will
3184 /// point to the added case.
3185 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3187 /// removeCase - This method removes the specified case and its successor
3188 /// from the switch instruction. Note that this operation may reorder the
3189 /// remaining cases at index idx and above.
3191 /// This action invalidates iterators for all cases following the one removed,
3192 /// including the case_end() iterator.
3193 void removeCase(CaseIt i);
3195 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3196 BasicBlock *getSuccessor(unsigned idx) const {
3197 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3198 return cast<BasicBlock>(getOperand(idx*2+1));
3200 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3201 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3202 setOperand(idx * 2 + 1, NewSucc);
3205 // Methods for support type inquiry through isa, cast, and dyn_cast:
3206 static inline bool classof(const Instruction *I) {
3207 return I->getOpcode() == Instruction::Switch;
3209 static inline bool classof(const Value *V) {
3210 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3214 BasicBlock *getSuccessorV(unsigned idx) const override;
3215 unsigned getNumSuccessorsV() const override;
3216 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3220 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3223 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3225 //===----------------------------------------------------------------------===//
3226 // IndirectBrInst Class
3227 //===----------------------------------------------------------------------===//
3229 //===---------------------------------------------------------------------------
3230 /// IndirectBrInst - Indirect Branch Instruction.
3232 class IndirectBrInst : public TerminatorInst {
3233 void *operator new(size_t, unsigned) = delete;
3234 unsigned ReservedSpace;
3235 // Operand[0] = Value to switch on
3236 // Operand[1] = Default basic block destination
3237 // Operand[2n ] = Value to match
3238 // Operand[2n+1] = BasicBlock to go to on match
3239 IndirectBrInst(const IndirectBrInst &IBI);
3240 void init(Value *Address, unsigned NumDests);
3241 void growOperands();
3242 // allocate space for exactly zero operands
3243 void *operator new(size_t s) {
3244 return User::operator new(s);
3246 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3247 /// Address to jump to. The number of expected destinations can be specified
3248 /// here to make memory allocation more efficient. This constructor can also
3249 /// autoinsert before another instruction.
3250 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3252 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3253 /// Address to jump to. The number of expected destinations can be specified
3254 /// here to make memory allocation more efficient. This constructor also
3255 /// autoinserts at the end of the specified BasicBlock.
3256 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3259 // Note: Instruction needs to be a friend here to call cloneImpl.
3260 friend class Instruction;
3261 IndirectBrInst *cloneImpl() const;
3264 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3265 Instruction *InsertBefore = nullptr) {
3266 return new IndirectBrInst(Address, NumDests, InsertBefore);
3268 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3269 BasicBlock *InsertAtEnd) {
3270 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3273 /// Provide fast operand accessors.
3274 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3276 // Accessor Methods for IndirectBrInst instruction.
3277 Value *getAddress() { return getOperand(0); }
3278 const Value *getAddress() const { return getOperand(0); }
3279 void setAddress(Value *V) { setOperand(0, V); }
3281 /// getNumDestinations - return the number of possible destinations in this
3282 /// indirectbr instruction.
3283 unsigned getNumDestinations() const { return getNumOperands()-1; }
3285 /// getDestination - Return the specified destination.
3286 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3287 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3289 /// addDestination - Add a destination.
3291 void addDestination(BasicBlock *Dest);
3293 /// removeDestination - This method removes the specified successor from the
3294 /// indirectbr instruction.
3295 void removeDestination(unsigned i);
3297 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3298 BasicBlock *getSuccessor(unsigned i) const {
3299 return cast<BasicBlock>(getOperand(i+1));
3301 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3302 setOperand(i + 1, NewSucc);
3305 // Methods for support type inquiry through isa, cast, and dyn_cast:
3306 static inline bool classof(const Instruction *I) {
3307 return I->getOpcode() == Instruction::IndirectBr;
3309 static inline bool classof(const Value *V) {
3310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3314 BasicBlock *getSuccessorV(unsigned idx) const override;
3315 unsigned getNumSuccessorsV() const override;
3316 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3320 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3323 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3325 //===----------------------------------------------------------------------===//
3327 //===----------------------------------------------------------------------===//
3329 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3330 /// calling convention of the call.
3332 class InvokeInst : public TerminatorInst,
3333 public OperandBundleUser<InvokeInst, User::op_iterator> {
3334 AttributeSet AttributeList;
3336 InvokeInst(const InvokeInst &BI);
3337 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3338 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3339 const Twine &NameStr) {
3340 init(cast<FunctionType>(
3341 cast<PointerType>(Func->getType())->getElementType()),
3342 Func, IfNormal, IfException, Args, Bundles, NameStr);
3344 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3345 BasicBlock *IfException, ArrayRef<Value *> Args,
3346 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3348 /// Construct an InvokeInst given a range of arguments.
3350 /// \brief Construct an InvokeInst from a range of arguments
3351 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3352 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3353 unsigned Values, const Twine &NameStr,
3354 Instruction *InsertBefore)
3355 : InvokeInst(cast<FunctionType>(
3356 cast<PointerType>(Func->getType())->getElementType()),
3357 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3360 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3361 BasicBlock *IfException, ArrayRef<Value *> Args,
3362 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3363 const Twine &NameStr, Instruction *InsertBefore);
3364 /// Construct an InvokeInst given a range of arguments.
3366 /// \brief Construct an InvokeInst from a range of arguments
3367 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3368 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3369 unsigned Values, const Twine &NameStr,
3370 BasicBlock *InsertAtEnd);
3372 friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3373 bool hasDescriptor() const { return HasDescriptor; }
3376 // Note: Instruction needs to be a friend here to call cloneImpl.
3377 friend class Instruction;
3378 InvokeInst *cloneImpl() const;
3381 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3382 BasicBlock *IfException, ArrayRef<Value *> Args,
3383 const Twine &NameStr,
3384 Instruction *InsertBefore = nullptr) {
3385 return Create(cast<FunctionType>(
3386 cast<PointerType>(Func->getType())->getElementType()),
3387 Func, IfNormal, IfException, Args, None, NameStr,
3390 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3391 BasicBlock *IfException, ArrayRef<Value *> Args,
3392 ArrayRef<OperandBundleDef> Bundles = None,
3393 const Twine &NameStr = "",
3394 Instruction *InsertBefore = nullptr) {
3395 return Create(cast<FunctionType>(
3396 cast<PointerType>(Func->getType())->getElementType()),
3397 Func, IfNormal, IfException, Args, Bundles, NameStr,
3400 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3401 BasicBlock *IfException, ArrayRef<Value *> Args,
3402 const Twine &NameStr,
3403 Instruction *InsertBefore = nullptr) {
3404 unsigned Values = unsigned(Args.size()) + 3;
3405 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3406 Values, NameStr, InsertBefore);
3408 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3409 BasicBlock *IfException, ArrayRef<Value *> Args,
3410 ArrayRef<OperandBundleDef> Bundles = None,
3411 const Twine &NameStr = "",
3412 Instruction *InsertBefore = nullptr) {
3413 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3414 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3416 return new (Values, DescriptorBytes)
3417 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3418 NameStr, InsertBefore);
3420 static InvokeInst *Create(Value *Func,
3421 BasicBlock *IfNormal, BasicBlock *IfException,
3422 ArrayRef<Value *> Args, const Twine &NameStr,
3423 BasicBlock *InsertAtEnd) {
3424 unsigned Values = unsigned(Args.size()) + 3;
3425 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3426 Values, NameStr, InsertAtEnd);
3428 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3429 BasicBlock *IfException, ArrayRef<Value *> Args,
3430 ArrayRef<OperandBundleDef> Bundles,
3431 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3432 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3433 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3435 return new (Values, DescriptorBytes)
3436 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3440 /// \brief Create a clone of \p II with a different set of operand bundles and
3441 /// insert it before \p InsertPt.
3443 /// The returned invoke instruction is identical to \p II in every way except
3444 /// that the operand bundles for the new instruction are set to the operand
3445 /// bundles in \p Bundles.
3446 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3447 Instruction *InsertPt = nullptr);
3449 /// Provide fast operand accessors
3450 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3452 FunctionType *getFunctionType() const { return FTy; }
3454 void mutateFunctionType(FunctionType *FTy) {
3455 mutateType(FTy->getReturnType());
3459 /// getNumArgOperands - Return the number of invoke arguments.
3461 unsigned getNumArgOperands() const {
3462 return getNumOperands() - getNumTotalBundleOperands() - 3;
3465 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3467 Value *getArgOperand(unsigned i) const {
3468 assert(i < getNumArgOperands() && "Out of bounds!");
3469 return getOperand(i);