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/iterator_range.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 /// Returns true if the ordering is at least as strong as acquire
54 /// (i.e. acquire, acq_rel or seq_cst)
55 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
56 return (Ord == Acquire ||
57 Ord == AcquireRelease ||
58 Ord == SequentiallyConsistent);
61 /// Returns true if the ordering is at least as strong as release
62 /// (i.e. release, acq_rel or seq_cst)
63 inline bool isAtLeastRelease(AtomicOrdering Ord) {
64 return (Ord == Release ||
65 Ord == AcquireRelease ||
66 Ord == SequentiallyConsistent);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// AllocaInst - an instruction to allocate memory on the stack
75 class AllocaInst : public UnaryInstruction {
77 AllocaInst *clone_impl() const override;
79 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
80 const Twine &Name = "",
81 Instruction *InsertBefore = nullptr);
82 AllocaInst(Type *Ty, Value *ArraySize,
83 const Twine &Name, BasicBlock *InsertAtEnd);
85 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
86 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
88 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
89 const Twine &Name = "", Instruction *InsertBefore = nullptr);
90 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
91 const Twine &Name, BasicBlock *InsertAtEnd);
93 // Out of line virtual method, so the vtable, etc. has a home.
94 ~AllocaInst() override;
96 /// isArrayAllocation - Return true if there is an allocation size parameter
97 /// to the allocation instruction that is not 1.
99 bool isArrayAllocation() const;
101 /// getArraySize - Get the number of elements allocated. For a simple
102 /// allocation of a single element, this will return a constant 1 value.
104 const Value *getArraySize() const { return getOperand(0); }
105 Value *getArraySize() { return getOperand(0); }
107 /// getType - Overload to return most specific pointer type
109 PointerType *getType() const {
110 return cast<PointerType>(Instruction::getType());
113 /// getAllocatedType - Return the type that is being allocated by the
116 Type *getAllocatedType() const;
118 /// getAlignment - Return the alignment of the memory that is being allocated
119 /// by the instruction.
121 unsigned getAlignment() const {
122 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
124 void setAlignment(unsigned Align);
126 /// isStaticAlloca - Return true if this alloca is in the entry block of the
127 /// function and is a constant size. If so, the code generator will fold it
128 /// into the prolog/epilog code, so it is basically free.
129 bool isStaticAlloca() const;
131 /// \brief Return true if this alloca is used as an inalloca argument to a
132 /// call. Such allocas are never considered static even if they are in the
134 bool isUsedWithInAlloca() const {
135 return getSubclassDataFromInstruction() & 32;
138 /// \brief Specify whether this alloca is used to represent the arguments to
140 void setUsedWithInAlloca(bool V) {
141 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
145 // Methods for support type inquiry through isa, cast, and dyn_cast:
146 static inline bool classof(const Instruction *I) {
147 return (I->getOpcode() == Instruction::Alloca);
149 static inline bool classof(const Value *V) {
150 return isa<Instruction>(V) && classof(cast<Instruction>(V));
153 // Shadow Instruction::setInstructionSubclassData with a private forwarding
154 // method so that subclasses cannot accidentally use it.
155 void setInstructionSubclassData(unsigned short D) {
156 Instruction::setInstructionSubclassData(D);
161 //===----------------------------------------------------------------------===//
163 //===----------------------------------------------------------------------===//
165 /// LoadInst - an instruction for reading from memory. This uses the
166 /// SubclassData field in Value to store whether or not the load is volatile.
168 class LoadInst : public UnaryInstruction {
171 LoadInst *clone_impl() const override;
173 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
174 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
175 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
176 Instruction *InsertBefore = nullptr);
177 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
178 BasicBlock *InsertAtEnd);
179 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
180 Instruction *InsertBefore = nullptr)
181 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
182 NameStr, isVolatile, Align, InsertBefore) {}
183 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
184 unsigned Align, Instruction *InsertBefore = nullptr);
185 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
186 unsigned Align, BasicBlock *InsertAtEnd);
187 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
188 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
189 Instruction *InsertBefore = nullptr)
190 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
191 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
192 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
193 unsigned Align, AtomicOrdering Order,
194 SynchronizationScope SynchScope = CrossThread,
195 Instruction *InsertBefore = nullptr);
196 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
197 unsigned Align, AtomicOrdering Order,
198 SynchronizationScope SynchScope,
199 BasicBlock *InsertAtEnd);
201 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
202 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
203 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
204 bool isVolatile = false,
205 Instruction *InsertBefore = nullptr);
206 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
207 BasicBlock *InsertAtEnd);
209 /// isVolatile - Return true if this is a load from a volatile memory
212 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
214 /// setVolatile - Specify whether this is a volatile load or not.
216 void setVolatile(bool V) {
217 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
221 /// getAlignment - Return the alignment of the access that is being performed
223 unsigned getAlignment() const {
224 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
227 void setAlignment(unsigned Align);
229 /// Returns the ordering effect of this fence.
230 AtomicOrdering getOrdering() const {
231 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
234 /// Set the ordering constraint on this load. May not be Release or
236 void setOrdering(AtomicOrdering Ordering) {
237 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
241 SynchronizationScope getSynchScope() const {
242 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
245 /// Specify whether this load is ordered with respect to all
246 /// concurrently executing threads, or only with respect to signal handlers
247 /// executing in the same thread.
248 void setSynchScope(SynchronizationScope xthread) {
249 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
253 void setAtomic(AtomicOrdering Ordering,
254 SynchronizationScope SynchScope = CrossThread) {
255 setOrdering(Ordering);
256 setSynchScope(SynchScope);
259 bool isSimple() const { return !isAtomic() && !isVolatile(); }
260 bool isUnordered() const {
261 return getOrdering() <= Unordered && !isVolatile();
264 Value *getPointerOperand() { return getOperand(0); }
265 const Value *getPointerOperand() const { return getOperand(0); }
266 static unsigned getPointerOperandIndex() { return 0U; }
268 /// \brief Returns the address space of the pointer operand.
269 unsigned getPointerAddressSpace() const {
270 return getPointerOperand()->getType()->getPointerAddressSpace();
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const Instruction *I) {
276 return I->getOpcode() == Instruction::Load;
278 static inline bool classof(const Value *V) {
279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
282 // Shadow Instruction::setInstructionSubclassData with a private forwarding
283 // method so that subclasses cannot accidentally use it.
284 void setInstructionSubclassData(unsigned short D) {
285 Instruction::setInstructionSubclassData(D);
290 //===----------------------------------------------------------------------===//
292 //===----------------------------------------------------------------------===//
294 /// StoreInst - an instruction for storing to memory
296 class StoreInst : public Instruction {
297 void *operator new(size_t, unsigned) = delete;
300 StoreInst *clone_impl() const override;
302 // allocate space for exactly two operands
303 void *operator new(size_t s) {
304 return User::operator new(s, 2);
306 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
307 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
308 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
309 Instruction *InsertBefore = nullptr);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
312 unsigned Align, Instruction *InsertBefore = nullptr);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
314 unsigned Align, BasicBlock *InsertAtEnd);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, AtomicOrdering Order,
317 SynchronizationScope SynchScope = CrossThread,
318 Instruction *InsertBefore = nullptr);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320 unsigned Align, AtomicOrdering Order,
321 SynchronizationScope SynchScope,
322 BasicBlock *InsertAtEnd);
325 /// isVolatile - Return true if this is a store to a volatile memory
328 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
330 /// setVolatile - Specify whether this is a volatile store or not.
332 void setVolatile(bool V) {
333 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
337 /// Transparently provide more efficient getOperand methods.
338 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
340 /// getAlignment - Return the alignment of the access that is being performed
342 unsigned getAlignment() const {
343 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
346 void setAlignment(unsigned Align);
348 /// Returns the ordering effect of this store.
349 AtomicOrdering getOrdering() const {
350 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
353 /// Set the ordering constraint on this store. May not be Acquire or
355 void setOrdering(AtomicOrdering Ordering) {
356 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
360 SynchronizationScope getSynchScope() const {
361 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
364 /// Specify whether this store instruction is ordered with respect to all
365 /// concurrently executing threads, or only with respect to signal handlers
366 /// executing in the same thread.
367 void setSynchScope(SynchronizationScope xthread) {
368 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
372 void setAtomic(AtomicOrdering Ordering,
373 SynchronizationScope SynchScope = CrossThread) {
374 setOrdering(Ordering);
375 setSynchScope(SynchScope);
378 bool isSimple() const { return !isAtomic() && !isVolatile(); }
379 bool isUnordered() const {
380 return getOrdering() <= Unordered && !isVolatile();
383 Value *getValueOperand() { return getOperand(0); }
384 const Value *getValueOperand() const { return getOperand(0); }
386 Value *getPointerOperand() { return getOperand(1); }
387 const Value *getPointerOperand() const { return getOperand(1); }
388 static unsigned getPointerOperandIndex() { return 1U; }
390 /// \brief Returns the address space of the pointer operand.
391 unsigned getPointerAddressSpace() const {
392 return getPointerOperand()->getType()->getPointerAddressSpace();
395 // Methods for support type inquiry through isa, cast, and dyn_cast:
396 static inline bool classof(const Instruction *I) {
397 return I->getOpcode() == Instruction::Store;
399 static inline bool classof(const Value *V) {
400 return isa<Instruction>(V) && classof(cast<Instruction>(V));
403 // Shadow Instruction::setInstructionSubclassData with a private forwarding
404 // method so that subclasses cannot accidentally use it.
405 void setInstructionSubclassData(unsigned short D) {
406 Instruction::setInstructionSubclassData(D);
411 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
414 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
416 //===----------------------------------------------------------------------===//
418 //===----------------------------------------------------------------------===//
420 /// FenceInst - an instruction for ordering other memory operations
422 class FenceInst : public Instruction {
423 void *operator new(size_t, unsigned) = delete;
424 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
426 FenceInst *clone_impl() const override;
428 // allocate space for exactly zero operands
429 void *operator new(size_t s) {
430 return User::operator new(s, 0);
433 // Ordering may only be Acquire, Release, AcquireRelease, or
434 // SequentiallyConsistent.
435 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
436 SynchronizationScope SynchScope = CrossThread,
437 Instruction *InsertBefore = nullptr);
438 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
439 SynchronizationScope SynchScope,
440 BasicBlock *InsertAtEnd);
442 /// Returns the ordering effect of this fence.
443 AtomicOrdering getOrdering() const {
444 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
447 /// Set the ordering constraint on this fence. May only be Acquire, Release,
448 /// AcquireRelease, or SequentiallyConsistent.
449 void setOrdering(AtomicOrdering Ordering) {
450 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
454 SynchronizationScope getSynchScope() const {
455 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
458 /// Specify whether this fence orders other operations with respect to all
459 /// concurrently executing threads, or only with respect to signal handlers
460 /// executing in the same thread.
461 void setSynchScope(SynchronizationScope xthread) {
462 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
466 // Methods for support type inquiry through isa, cast, and dyn_cast:
467 static inline bool classof(const Instruction *I) {
468 return I->getOpcode() == Instruction::Fence;
470 static inline bool classof(const Value *V) {
471 return isa<Instruction>(V) && classof(cast<Instruction>(V));
474 // Shadow Instruction::setInstructionSubclassData with a private forwarding
475 // method so that subclasses cannot accidentally use it.
476 void setInstructionSubclassData(unsigned short D) {
477 Instruction::setInstructionSubclassData(D);
481 //===----------------------------------------------------------------------===//
482 // AtomicCmpXchgInst Class
483 //===----------------------------------------------------------------------===//
485 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
486 /// specified value is in a memory location, and, if it is, stores a new value
487 /// there. Returns the value that was loaded.
489 class AtomicCmpXchgInst : public Instruction {
490 void *operator new(size_t, unsigned) = delete;
491 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
492 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
493 SynchronizationScope SynchScope);
495 AtomicCmpXchgInst *clone_impl() const override;
497 // allocate space for exactly three operands
498 void *operator new(size_t s) {
499 return User::operator new(s, 3);
501 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
502 AtomicOrdering SuccessOrdering,
503 AtomicOrdering FailureOrdering,
504 SynchronizationScope SynchScope,
505 Instruction *InsertBefore = nullptr);
506 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
507 AtomicOrdering SuccessOrdering,
508 AtomicOrdering FailureOrdering,
509 SynchronizationScope SynchScope,
510 BasicBlock *InsertAtEnd);
512 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
515 bool isVolatile() const {
516 return getSubclassDataFromInstruction() & 1;
519 /// setVolatile - Specify whether this is a volatile cmpxchg.
521 void setVolatile(bool V) {
522 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
526 /// Return true if this cmpxchg may spuriously fail.
527 bool isWeak() const {
528 return getSubclassDataFromInstruction() & 0x100;
531 void setWeak(bool IsWeak) {
532 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
536 /// Transparently provide more efficient getOperand methods.
537 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
539 /// Set the ordering constraint on this cmpxchg.
540 void setSuccessOrdering(AtomicOrdering Ordering) {
541 assert(Ordering != NotAtomic &&
542 "CmpXchg instructions can only be atomic.");
543 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
547 void setFailureOrdering(AtomicOrdering Ordering) {
548 assert(Ordering != NotAtomic &&
549 "CmpXchg instructions can only be atomic.");
550 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
554 /// Specify whether this cmpxchg is atomic and orders other operations with
555 /// respect to all concurrently executing threads, or only with respect to
556 /// signal handlers executing in the same thread.
557 void setSynchScope(SynchronizationScope SynchScope) {
558 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
562 /// Returns the ordering constraint on this cmpxchg.
563 AtomicOrdering getSuccessOrdering() const {
564 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
567 /// Returns the ordering constraint on this cmpxchg.
568 AtomicOrdering getFailureOrdering() const {
569 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
572 /// Returns whether this cmpxchg is atomic between threads or only within a
574 SynchronizationScope getSynchScope() const {
575 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
578 Value *getPointerOperand() { return getOperand(0); }
579 const Value *getPointerOperand() const { return getOperand(0); }
580 static unsigned getPointerOperandIndex() { return 0U; }
582 Value *getCompareOperand() { return getOperand(1); }
583 const Value *getCompareOperand() const { return getOperand(1); }
585 Value *getNewValOperand() { return getOperand(2); }
586 const Value *getNewValOperand() const { return getOperand(2); }
588 /// \brief Returns the address space of the pointer operand.
589 unsigned getPointerAddressSpace() const {
590 return getPointerOperand()->getType()->getPointerAddressSpace();
593 /// \brief Returns the strongest permitted ordering on failure, given the
594 /// desired ordering on success.
596 /// If the comparison in a cmpxchg operation fails, there is no atomic store
597 /// so release semantics cannot be provided. So this function drops explicit
598 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
599 /// operation would remain SequentiallyConsistent.
600 static AtomicOrdering
601 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
602 switch (SuccessOrdering) {
603 default: llvm_unreachable("invalid cmpxchg success ordering");
610 case SequentiallyConsistent:
611 return SequentiallyConsistent;
615 // Methods for support type inquiry through isa, cast, and dyn_cast:
616 static inline bool classof(const Instruction *I) {
617 return I->getOpcode() == Instruction::AtomicCmpXchg;
619 static inline bool classof(const Value *V) {
620 return isa<Instruction>(V) && classof(cast<Instruction>(V));
623 // Shadow Instruction::setInstructionSubclassData with a private forwarding
624 // method so that subclasses cannot accidentally use it.
625 void setInstructionSubclassData(unsigned short D) {
626 Instruction::setInstructionSubclassData(D);
631 struct OperandTraits<AtomicCmpXchgInst> :
632 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
635 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
637 //===----------------------------------------------------------------------===//
638 // AtomicRMWInst Class
639 //===----------------------------------------------------------------------===//
641 /// AtomicRMWInst - an instruction that atomically reads a memory location,
642 /// combines it with another value, and then stores the result back. Returns
645 class AtomicRMWInst : public Instruction {
646 void *operator new(size_t, unsigned) = delete;
648 AtomicRMWInst *clone_impl() const override;
650 /// This enumeration lists the possible modifications atomicrmw can make. In
651 /// the descriptions, 'p' is the pointer to the instruction's memory location,
652 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
653 /// instruction. These instructions always return 'old'.
669 /// *p = old >signed v ? old : v
671 /// *p = old <signed v ? old : v
673 /// *p = old >unsigned v ? old : v
675 /// *p = old <unsigned v ? old : v
683 // allocate space for exactly two operands
684 void *operator new(size_t s) {
685 return User::operator new(s, 2);
687 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
688 AtomicOrdering Ordering, SynchronizationScope SynchScope,
689 Instruction *InsertBefore = nullptr);
690 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
691 AtomicOrdering Ordering, SynchronizationScope SynchScope,
692 BasicBlock *InsertAtEnd);
694 BinOp getOperation() const {
695 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
698 void setOperation(BinOp Operation) {
699 unsigned short SubclassData = getSubclassDataFromInstruction();
700 setInstructionSubclassData((SubclassData & 31) |
704 /// isVolatile - Return true if this is a RMW on a volatile memory location.
706 bool isVolatile() const {
707 return getSubclassDataFromInstruction() & 1;
710 /// setVolatile - Specify whether this is a volatile RMW or not.
712 void setVolatile(bool V) {
713 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
717 /// Transparently provide more efficient getOperand methods.
718 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
720 /// Set the ordering constraint on this RMW.
721 void setOrdering(AtomicOrdering Ordering) {
722 assert(Ordering != NotAtomic &&
723 "atomicrmw instructions can only be atomic.");
724 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
728 /// Specify whether this RMW orders other operations with respect to all
729 /// concurrently executing threads, or only with respect to signal handlers
730 /// executing in the same thread.
731 void setSynchScope(SynchronizationScope SynchScope) {
732 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
736 /// Returns the ordering constraint on this RMW.
737 AtomicOrdering getOrdering() const {
738 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
741 /// Returns whether this RMW is atomic between threads or only within a
743 SynchronizationScope getSynchScope() const {
744 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
747 Value *getPointerOperand() { return getOperand(0); }
748 const Value *getPointerOperand() const { return getOperand(0); }
749 static unsigned getPointerOperandIndex() { return 0U; }
751 Value *getValOperand() { return getOperand(1); }
752 const Value *getValOperand() const { return getOperand(1); }
754 /// \brief Returns the address space of the pointer operand.
755 unsigned getPointerAddressSpace() const {
756 return getPointerOperand()->getType()->getPointerAddressSpace();
759 // Methods for support type inquiry through isa, cast, and dyn_cast:
760 static inline bool classof(const Instruction *I) {
761 return I->getOpcode() == Instruction::AtomicRMW;
763 static inline bool classof(const Value *V) {
764 return isa<Instruction>(V) && classof(cast<Instruction>(V));
767 void Init(BinOp Operation, Value *Ptr, Value *Val,
768 AtomicOrdering Ordering, SynchronizationScope SynchScope);
769 // Shadow Instruction::setInstructionSubclassData with a private forwarding
770 // method so that subclasses cannot accidentally use it.
771 void setInstructionSubclassData(unsigned short D) {
772 Instruction::setInstructionSubclassData(D);
777 struct OperandTraits<AtomicRMWInst>
778 : public FixedNumOperandTraits<AtomicRMWInst,2> {
781 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
783 //===----------------------------------------------------------------------===//
784 // GetElementPtrInst Class
785 //===----------------------------------------------------------------------===//
787 // checkGEPType - Simple wrapper function to give a better assertion failure
788 // message on bad indexes for a gep instruction.
790 inline Type *checkGEPType(Type *Ty) {
791 assert(Ty && "Invalid GetElementPtrInst indices for type!");
795 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
796 /// access elements of arrays and structs
798 class GetElementPtrInst : public Instruction {
799 GetElementPtrInst(const GetElementPtrInst &GEPI);
800 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
802 /// Constructors - Create a getelementptr instruction with a base pointer an
803 /// list of indices. The first ctor can optionally insert before an existing
804 /// instruction, the second appends the new instruction to the specified
806 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
807 ArrayRef<Value *> IdxList, unsigned Values,
808 const Twine &NameStr, Instruction *InsertBefore);
809 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
810 ArrayRef<Value *> IdxList, unsigned Values,
811 const Twine &NameStr, BasicBlock *InsertAtEnd);
814 GetElementPtrInst *clone_impl() const override;
816 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
817 ArrayRef<Value *> IdxList,
818 const Twine &NameStr = "",
819 Instruction *InsertBefore = nullptr) {
820 unsigned Values = 1 + unsigned(IdxList.size());
821 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
822 NameStr, InsertBefore);
824 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
825 ArrayRef<Value *> IdxList,
826 const Twine &NameStr,
827 BasicBlock *InsertAtEnd) {
828 unsigned Values = 1 + unsigned(IdxList.size());
829 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
830 NameStr, InsertAtEnd);
833 /// Create an "inbounds" getelementptr. See the documentation for the
834 /// "inbounds" flag in LangRef.html for details.
835 static GetElementPtrInst *CreateInBounds(Value *Ptr,
836 ArrayRef<Value *> IdxList,
837 const Twine &NameStr = "",
838 Instruction *InsertBefore = nullptr){
839 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
841 static GetElementPtrInst *
842 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
843 const Twine &NameStr = "",
844 Instruction *InsertBefore = nullptr) {
845 GetElementPtrInst *GEP =
846 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
847 GEP->setIsInBounds(true);
850 static GetElementPtrInst *CreateInBounds(Value *Ptr,
851 ArrayRef<Value *> IdxList,
852 const Twine &NameStr,
853 BasicBlock *InsertAtEnd) {
854 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
856 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
857 ArrayRef<Value *> IdxList,
858 const Twine &NameStr,
859 BasicBlock *InsertAtEnd) {
860 GetElementPtrInst *GEP =
861 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
862 GEP->setIsInBounds(true);
866 /// Transparently provide more efficient getOperand methods.
867 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
869 // getType - Overload to return most specific sequential type.
870 SequentialType *getType() const {
871 return cast<SequentialType>(Instruction::getType());
874 Type *getSourceElementType() const {
875 return cast<SequentialType>(getPointerOperandType()->getScalarType())
879 Type *getResultElementType() const { return getType()->getElementType(); }
881 /// \brief Returns the address space of this instruction's pointer type.
882 unsigned getAddressSpace() const {
883 // Note that this is always the same as the pointer operand's address space
884 // and that is cheaper to compute, so cheat here.
885 return getPointerAddressSpace();
888 /// getIndexedType - Returns the type of the element that would be loaded with
889 /// a load instruction with the specified parameters.
891 /// Null is returned if the indices are invalid for the specified
894 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
895 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
896 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
898 inline op_iterator idx_begin() { return op_begin()+1; }
899 inline const_op_iterator idx_begin() const { return op_begin()+1; }
900 inline op_iterator idx_end() { return op_end(); }
901 inline const_op_iterator idx_end() const { return op_end(); }
903 Value *getPointerOperand() {
904 return getOperand(0);
906 const Value *getPointerOperand() const {
907 return getOperand(0);
909 static unsigned getPointerOperandIndex() {
910 return 0U; // get index for modifying correct operand.
913 /// getPointerOperandType - Method to return the pointer operand as a
915 Type *getPointerOperandType() const {
916 return getPointerOperand()->getType();
919 /// \brief Returns the address space of the pointer operand.
920 unsigned getPointerAddressSpace() const {
921 return getPointerOperandType()->getPointerAddressSpace();
924 /// GetGEPReturnType - Returns the pointer type returned by the GEP
925 /// instruction, which may be a vector of pointers.
926 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
927 return getGEPReturnType(
928 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
931 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
932 ArrayRef<Value *> IdxList) {
933 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
934 Ptr->getType()->getPointerAddressSpace());
936 if (Ptr->getType()->isVectorTy()) {
937 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
938 return VectorType::get(PtrTy, NumElem);
945 unsigned getNumIndices() const { // Note: always non-negative
946 return getNumOperands() - 1;
949 bool hasIndices() const {
950 return getNumOperands() > 1;
953 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
954 /// zeros. If so, the result pointer and the first operand have the same
955 /// value, just potentially different types.
956 bool hasAllZeroIndices() const;
958 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
959 /// constant integers. If so, the result pointer and the first operand have
960 /// a constant offset between them.
961 bool hasAllConstantIndices() const;
963 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
964 /// See LangRef.html for the meaning of inbounds on a getelementptr.
965 void setIsInBounds(bool b = true);
967 /// isInBounds - Determine whether the GEP has the inbounds flag.
968 bool isInBounds() const;
970 /// \brief Accumulate the constant address offset of this GEP if possible.
972 /// This routine accepts an APInt into which it will accumulate the constant
973 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
974 /// all-constant, it returns false and the value of the offset APInt is
975 /// undefined (it is *not* preserved!). The APInt passed into this routine
976 /// must be at least as wide as the IntPtr type for the address space of
977 /// the base GEP pointer.
978 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
980 // Methods for support type inquiry through isa, cast, and dyn_cast:
981 static inline bool classof(const Instruction *I) {
982 return (I->getOpcode() == Instruction::GetElementPtr);
984 static inline bool classof(const Value *V) {
985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
990 struct OperandTraits<GetElementPtrInst> :
991 public VariadicOperandTraits<GetElementPtrInst, 1> {
994 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
995 ArrayRef<Value *> IdxList, unsigned Values,
996 const Twine &NameStr,
997 Instruction *InsertBefore)
998 : Instruction(PointeeType ? getGEPReturnType(PointeeType, Ptr, IdxList)
999 : getGEPReturnType(Ptr, IdxList),
1001 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1002 Values, InsertBefore) {
1003 init(Ptr, IdxList, NameStr);
1004 assert(!PointeeType || PointeeType == getSourceElementType());
1006 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1007 ArrayRef<Value *> IdxList, unsigned Values,
1008 const Twine &NameStr,
1009 BasicBlock *InsertAtEnd)
1010 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
1011 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1012 Values, InsertAtEnd) {
1013 init(Ptr, IdxList, NameStr);
1014 assert(!PointeeType || PointeeType == getSourceElementType());
1018 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1021 //===----------------------------------------------------------------------===//
1023 //===----------------------------------------------------------------------===//
1025 /// This instruction compares its operands according to the predicate given
1026 /// to the constructor. It only operates on integers or pointers. The operands
1027 /// must be identical types.
1028 /// \brief Represent an integer comparison operator.
1029 class ICmpInst: public CmpInst {
1031 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1032 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1033 "Invalid ICmp predicate value");
1034 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1035 "Both operands to ICmp instruction are not of the same type!");
1036 // Check that the operands are the right type
1037 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1038 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1039 "Invalid operand types for ICmp instruction");
1043 /// \brief Clone an identical ICmpInst
1044 ICmpInst *clone_impl() const override;
1046 /// \brief Constructor with insert-before-instruction semantics.
1048 Instruction *InsertBefore, ///< Where to insert
1049 Predicate pred, ///< The predicate to use for the comparison
1050 Value *LHS, ///< The left-hand-side of the expression
1051 Value *RHS, ///< The right-hand-side of the expression
1052 const Twine &NameStr = "" ///< Name of the instruction
1053 ) : CmpInst(makeCmpResultType(LHS->getType()),
1054 Instruction::ICmp, pred, LHS, RHS, NameStr,
1061 /// \brief Constructor with insert-at-end semantics.
1063 BasicBlock &InsertAtEnd, ///< Block to insert into.
1064 Predicate pred, ///< The predicate to use for the comparison
1065 Value *LHS, ///< The left-hand-side of the expression
1066 Value *RHS, ///< The right-hand-side of the expression
1067 const Twine &NameStr = "" ///< Name of the instruction
1068 ) : CmpInst(makeCmpResultType(LHS->getType()),
1069 Instruction::ICmp, pred, LHS, RHS, NameStr,
1076 /// \brief Constructor with no-insertion semantics
1078 Predicate pred, ///< The predicate to use for the comparison
1079 Value *LHS, ///< The left-hand-side of the expression
1080 Value *RHS, ///< The right-hand-side of the expression
1081 const Twine &NameStr = "" ///< Name of the instruction
1082 ) : CmpInst(makeCmpResultType(LHS->getType()),
1083 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1089 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1090 /// @returns the predicate that would be the result if the operand were
1091 /// regarded as signed.
1092 /// \brief Return the signed version of the predicate
1093 Predicate getSignedPredicate() const {
1094 return getSignedPredicate(getPredicate());
1097 /// This is a static version that you can use without an instruction.
1098 /// \brief Return the signed version of the predicate.
1099 static Predicate getSignedPredicate(Predicate pred);
1101 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1102 /// @returns the predicate that would be the result if the operand were
1103 /// regarded as unsigned.
1104 /// \brief Return the unsigned version of the predicate
1105 Predicate getUnsignedPredicate() const {
1106 return getUnsignedPredicate(getPredicate());
1109 /// This is a static version that you can use without an instruction.
1110 /// \brief Return the unsigned version of the predicate.
1111 static Predicate getUnsignedPredicate(Predicate pred);
1113 /// isEquality - Return true if this predicate is either EQ or NE. This also
1114 /// tests for commutativity.
1115 static bool isEquality(Predicate P) {
1116 return P == ICMP_EQ || P == ICMP_NE;
1119 /// isEquality - Return true if this predicate is either EQ or NE. This also
1120 /// tests for commutativity.
1121 bool isEquality() const {
1122 return isEquality(getPredicate());
1125 /// @returns true if the predicate of this ICmpInst is commutative
1126 /// \brief Determine if this relation is commutative.
1127 bool isCommutative() const { return isEquality(); }
1129 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1131 bool isRelational() const {
1132 return !isEquality();
1135 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1137 static bool isRelational(Predicate P) {
1138 return !isEquality(P);
1141 /// Initialize a set of values that all satisfy the predicate with C.
1142 /// \brief Make a ConstantRange for a relation with a constant value.
1143 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1145 /// Exchange the two operands to this instruction in such a way that it does
1146 /// not modify the semantics of the instruction. The predicate value may be
1147 /// changed to retain the same result if the predicate is order dependent
1149 /// \brief Swap operands and adjust predicate.
1150 void swapOperands() {
1151 setPredicate(getSwappedPredicate());
1152 Op<0>().swap(Op<1>());
1155 // Methods for support type inquiry through isa, cast, and dyn_cast:
1156 static inline bool classof(const Instruction *I) {
1157 return I->getOpcode() == Instruction::ICmp;
1159 static inline bool classof(const Value *V) {
1160 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1165 //===----------------------------------------------------------------------===//
1167 //===----------------------------------------------------------------------===//
1169 /// This instruction compares its operands according to the predicate given
1170 /// to the constructor. It only operates on floating point values or packed
1171 /// vectors of floating point values. The operands must be identical types.
1172 /// \brief Represents a floating point comparison operator.
1173 class FCmpInst: public CmpInst {
1175 /// \brief Clone an identical FCmpInst
1176 FCmpInst *clone_impl() const override;
1178 /// \brief Constructor with insert-before-instruction semantics.
1180 Instruction *InsertBefore, ///< Where to insert
1181 Predicate pred, ///< The predicate to use for the comparison
1182 Value *LHS, ///< The left-hand-side of the expression
1183 Value *RHS, ///< The right-hand-side of the expression
1184 const Twine &NameStr = "" ///< Name of the instruction
1185 ) : CmpInst(makeCmpResultType(LHS->getType()),
1186 Instruction::FCmp, pred, LHS, RHS, NameStr,
1188 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1189 "Invalid FCmp predicate value");
1190 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1191 "Both operands to FCmp instruction are not of the same type!");
1192 // Check that the operands are the right type
1193 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1194 "Invalid operand types for FCmp instruction");
1197 /// \brief Constructor with insert-at-end semantics.
1199 BasicBlock &InsertAtEnd, ///< Block to insert into.
1200 Predicate pred, ///< The predicate to use for the comparison
1201 Value *LHS, ///< The left-hand-side of the expression
1202 Value *RHS, ///< The right-hand-side of the expression
1203 const Twine &NameStr = "" ///< Name of the instruction
1204 ) : CmpInst(makeCmpResultType(LHS->getType()),
1205 Instruction::FCmp, pred, LHS, RHS, NameStr,
1207 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1208 "Invalid FCmp predicate value");
1209 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1210 "Both operands to FCmp instruction are not of the same type!");
1211 // Check that the operands are the right type
1212 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1213 "Invalid operand types for FCmp instruction");
1216 /// \brief Constructor with no-insertion semantics
1218 Predicate pred, ///< The predicate to use for the comparison
1219 Value *LHS, ///< The left-hand-side of the expression
1220 Value *RHS, ///< The right-hand-side of the expression
1221 const Twine &NameStr = "" ///< Name of the instruction
1222 ) : CmpInst(makeCmpResultType(LHS->getType()),
1223 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1224 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1225 "Invalid FCmp predicate value");
1226 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1227 "Both operands to FCmp instruction are not of the same type!");
1228 // Check that the operands are the right type
1229 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1230 "Invalid operand types for FCmp instruction");
1233 /// @returns true if the predicate of this instruction is EQ or NE.
1234 /// \brief Determine if this is an equality predicate.
1235 static bool isEquality(Predicate Pred) {
1236 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1240 /// @returns true if the predicate of this instruction is EQ or NE.
1241 /// \brief Determine if this is an equality predicate.
1242 bool isEquality() const { return isEquality(getPredicate()); }
1244 /// @returns true if the predicate of this instruction is commutative.
1245 /// \brief Determine if this is a commutative predicate.
1246 bool isCommutative() const {
1247 return isEquality() ||
1248 getPredicate() == FCMP_FALSE ||
1249 getPredicate() == FCMP_TRUE ||
1250 getPredicate() == FCMP_ORD ||
1251 getPredicate() == FCMP_UNO;
1254 /// @returns true if the predicate is relational (not EQ or NE).
1255 /// \brief Determine if this a relational predicate.
1256 bool isRelational() const { return !isEquality(); }
1258 /// Exchange the two operands to this instruction in such a way that it does
1259 /// not modify the semantics of the instruction. The predicate value may be
1260 /// changed to retain the same result if the predicate is order dependent
1262 /// \brief Swap operands and adjust predicate.
1263 void swapOperands() {
1264 setPredicate(getSwappedPredicate());
1265 Op<0>().swap(Op<1>());
1268 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1269 static inline bool classof(const Instruction *I) {
1270 return I->getOpcode() == Instruction::FCmp;
1272 static inline bool classof(const Value *V) {
1273 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1277 //===----------------------------------------------------------------------===//
1278 /// CallInst - This class represents a function call, abstracting a target
1279 /// machine's calling convention. This class uses low bit of the SubClassData
1280 /// field to indicate whether or not this is a tail call. The rest of the bits
1281 /// hold the calling convention of the call.
1283 class CallInst : public Instruction {
1284 AttributeSet AttributeList; ///< parameter attributes for call
1285 CallInst(const CallInst &CI);
1286 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1287 void init(Value *Func, const Twine &NameStr);
1289 /// Construct a CallInst given a range of arguments.
1290 /// \brief Construct a CallInst from a range of arguments
1291 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1292 const Twine &NameStr, Instruction *InsertBefore);
1294 /// Construct a CallInst given a range of arguments.
1295 /// \brief Construct a CallInst from a range of arguments
1296 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1297 const Twine &NameStr, BasicBlock *InsertAtEnd);
1299 explicit CallInst(Value *F, const Twine &NameStr,
1300 Instruction *InsertBefore);
1301 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1303 CallInst *clone_impl() const override;
1305 static CallInst *Create(Value *Func,
1306 ArrayRef<Value *> Args,
1307 const Twine &NameStr = "",
1308 Instruction *InsertBefore = nullptr) {
1309 return new(unsigned(Args.size() + 1))
1310 CallInst(Func, Args, NameStr, InsertBefore);
1312 static CallInst *Create(Value *Func,
1313 ArrayRef<Value *> Args,
1314 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1315 return new(unsigned(Args.size() + 1))
1316 CallInst(Func, Args, NameStr, InsertAtEnd);
1318 static CallInst *Create(Value *F, const Twine &NameStr = "",
1319 Instruction *InsertBefore = nullptr) {
1320 return new(1) CallInst(F, NameStr, InsertBefore);
1322 static CallInst *Create(Value *F, const Twine &NameStr,
1323 BasicBlock *InsertAtEnd) {
1324 return new(1) CallInst(F, NameStr, InsertAtEnd);
1326 /// CreateMalloc - Generate the IR for a call to malloc:
1327 /// 1. Compute the malloc call's argument as the specified type's size,
1328 /// possibly multiplied by the array size if the array size is not
1330 /// 2. Call malloc with that argument.
1331 /// 3. Bitcast the result of the malloc call to the specified type.
1332 static Instruction *CreateMalloc(Instruction *InsertBefore,
1333 Type *IntPtrTy, Type *AllocTy,
1334 Value *AllocSize, Value *ArraySize = nullptr,
1335 Function* MallocF = nullptr,
1336 const Twine &Name = "");
1337 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1338 Type *IntPtrTy, Type *AllocTy,
1339 Value *AllocSize, Value *ArraySize = nullptr,
1340 Function* MallocF = nullptr,
1341 const Twine &Name = "");
1342 /// CreateFree - Generate the IR for a call to the builtin free function.
1343 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1344 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1346 ~CallInst() override;
1348 FunctionType *getFunctionType() const {
1349 return cast<FunctionType>(
1350 cast<PointerType>(getCalledValue()->getType())->getElementType());
1353 // Note that 'musttail' implies 'tail'.
1354 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1355 TailCallKind getTailCallKind() const {
1356 return TailCallKind(getSubclassDataFromInstruction() & 3);
1358 bool isTailCall() const {
1359 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1361 bool isMustTailCall() const {
1362 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1364 void setTailCall(bool isTC = true) {
1365 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1366 unsigned(isTC ? TCK_Tail : TCK_None));
1368 void setTailCallKind(TailCallKind TCK) {
1369 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1373 /// Provide fast operand accessors
1374 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1376 /// getNumArgOperands - Return the number of call arguments.
1378 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1380 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1382 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1383 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1385 /// arg_operands - iteration adapter for range-for loops.
1386 iterator_range<op_iterator> arg_operands() {
1387 // The last operand in the op list is the callee - it's not one of the args
1388 // so we don't want to iterate over it.
1389 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1392 /// arg_operands - iteration adapter for range-for loops.
1393 iterator_range<const_op_iterator> arg_operands() const {
1394 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1397 /// \brief Wrappers for getting the \c Use of a call argument.
1398 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1399 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1401 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1403 CallingConv::ID getCallingConv() const {
1404 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1406 void setCallingConv(CallingConv::ID CC) {
1407 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1408 (static_cast<unsigned>(CC) << 2));
1411 /// getAttributes - Return the parameter attributes for this call.
1413 const AttributeSet &getAttributes() const { return AttributeList; }
1415 /// setAttributes - Set the parameter attributes for this call.
1417 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1419 /// addAttribute - adds the attribute to the list of attributes.
1420 void addAttribute(unsigned i, Attribute::AttrKind attr);
1422 /// removeAttribute - removes the attribute from the list of attributes.
1423 void removeAttribute(unsigned i, Attribute attr);
1425 /// \brief adds the dereferenceable attribute to the list of attributes.
1426 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1428 /// \brief adds the dereferenceable_or_null attribute to the list of
1430 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1432 /// \brief Determine whether this call has the given attribute.
1433 bool hasFnAttr(Attribute::AttrKind A) const {
1434 assert(A != Attribute::NoBuiltin &&
1435 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1436 return hasFnAttrImpl(A);
1439 /// \brief Determine whether the call or the callee has the given attributes.
1440 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1442 /// \brief Extract the alignment for a call or parameter (0=unknown).
1443 unsigned getParamAlignment(unsigned i) const {
1444 return AttributeList.getParamAlignment(i);
1447 /// \brief Extract the number of dereferenceable bytes for a call or
1448 /// parameter (0=unknown).
1449 uint64_t getDereferenceableBytes(unsigned i) const {
1450 return AttributeList.getDereferenceableBytes(i);
1453 /// \brief Return true if the call should not be treated as a call to a
1455 bool isNoBuiltin() const {
1456 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1457 !hasFnAttrImpl(Attribute::Builtin);
1460 /// \brief Return true if the call should not be inlined.
1461 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1462 void setIsNoInline() {
1463 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1466 /// \brief Return true if the call can return twice
1467 bool canReturnTwice() const {
1468 return hasFnAttr(Attribute::ReturnsTwice);
1470 void setCanReturnTwice() {
1471 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1474 /// \brief Determine if the call does not access memory.
1475 bool doesNotAccessMemory() const {
1476 return hasFnAttr(Attribute::ReadNone);
1478 void setDoesNotAccessMemory() {
1479 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1482 /// \brief Determine if the call does not access or only reads memory.
1483 bool onlyReadsMemory() const {
1484 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1486 void setOnlyReadsMemory() {
1487 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1490 /// \brief Determine if the call cannot return.
1491 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1492 void setDoesNotReturn() {
1493 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1496 /// \brief Determine if the call cannot unwind.
1497 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1498 void setDoesNotThrow() {
1499 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1502 /// \brief Determine if the call cannot be duplicated.
1503 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1504 void setCannotDuplicate() {
1505 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1508 /// \brief Determine if the call returns a structure through first
1509 /// pointer argument.
1510 bool hasStructRetAttr() const {
1511 // Be friendly and also check the callee.
1512 return paramHasAttr(1, Attribute::StructRet);
1515 /// \brief Determine if any call argument is an aggregate passed by value.
1516 bool hasByValArgument() const {
1517 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1520 /// getCalledFunction - Return the function called, or null if this is an
1521 /// indirect function invocation.
1523 Function *getCalledFunction() const {
1524 return dyn_cast<Function>(Op<-1>());
1527 /// getCalledValue - Get a pointer to the function that is invoked by this
1529 const Value *getCalledValue() const { return Op<-1>(); }
1530 Value *getCalledValue() { return Op<-1>(); }
1532 /// setCalledFunction - Set the function called.
1533 void setCalledFunction(Value* Fn) {
1537 /// isInlineAsm - Check if this call is an inline asm statement.
1538 bool isInlineAsm() const {
1539 return isa<InlineAsm>(Op<-1>());
1542 // Methods for support type inquiry through isa, cast, and dyn_cast:
1543 static inline bool classof(const Instruction *I) {
1544 return I->getOpcode() == Instruction::Call;
1546 static inline bool classof(const Value *V) {
1547 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1551 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1553 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1554 // method so that subclasses cannot accidentally use it.
1555 void setInstructionSubclassData(unsigned short D) {
1556 Instruction::setInstructionSubclassData(D);
1561 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1564 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1565 const Twine &NameStr, BasicBlock *InsertAtEnd)
1566 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1567 ->getElementType())->getReturnType(),
1569 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1570 unsigned(Args.size() + 1), InsertAtEnd) {
1571 init(Func, Args, NameStr);
1574 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1575 const Twine &NameStr, Instruction *InsertBefore)
1576 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1577 ->getElementType())->getReturnType(),
1579 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1580 unsigned(Args.size() + 1), InsertBefore) {
1581 init(Func, Args, NameStr);
1585 // Note: if you get compile errors about private methods then
1586 // please update your code to use the high-level operand
1587 // interfaces. See line 943 above.
1588 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1590 //===----------------------------------------------------------------------===//
1592 //===----------------------------------------------------------------------===//
1594 /// SelectInst - This class represents the LLVM 'select' instruction.
1596 class SelectInst : public Instruction {
1597 void init(Value *C, Value *S1, Value *S2) {
1598 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1604 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1605 Instruction *InsertBefore)
1606 : Instruction(S1->getType(), Instruction::Select,
1607 &Op<0>(), 3, InsertBefore) {
1611 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1612 BasicBlock *InsertAtEnd)
1613 : Instruction(S1->getType(), Instruction::Select,
1614 &Op<0>(), 3, InsertAtEnd) {
1619 SelectInst *clone_impl() const override;
1621 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1622 const Twine &NameStr = "",
1623 Instruction *InsertBefore = nullptr) {
1624 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1626 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1627 const Twine &NameStr,
1628 BasicBlock *InsertAtEnd) {
1629 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1632 const Value *getCondition() const { return Op<0>(); }
1633 const Value *getTrueValue() const { return Op<1>(); }
1634 const Value *getFalseValue() const { return Op<2>(); }
1635 Value *getCondition() { return Op<0>(); }
1636 Value *getTrueValue() { return Op<1>(); }
1637 Value *getFalseValue() { return Op<2>(); }
1639 /// areInvalidOperands - Return a string if the specified operands are invalid
1640 /// for a select operation, otherwise return null.
1641 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1643 /// Transparently provide more efficient getOperand methods.
1644 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1646 OtherOps getOpcode() const {
1647 return static_cast<OtherOps>(Instruction::getOpcode());
1650 // Methods for support type inquiry through isa, cast, and dyn_cast:
1651 static inline bool classof(const Instruction *I) {
1652 return I->getOpcode() == Instruction::Select;
1654 static inline bool classof(const Value *V) {
1655 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1660 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1663 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1665 //===----------------------------------------------------------------------===//
1667 //===----------------------------------------------------------------------===//
1669 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1670 /// an argument of the specified type given a va_list and increments that list
1672 class VAArgInst : public UnaryInstruction {
1674 VAArgInst *clone_impl() const override;
1677 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1678 Instruction *InsertBefore = nullptr)
1679 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1682 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1683 BasicBlock *InsertAtEnd)
1684 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1688 Value *getPointerOperand() { return getOperand(0); }
1689 const Value *getPointerOperand() const { return getOperand(0); }
1690 static unsigned getPointerOperandIndex() { return 0U; }
1692 // Methods for support type inquiry through isa, cast, and dyn_cast:
1693 static inline bool classof(const Instruction *I) {
1694 return I->getOpcode() == VAArg;
1696 static inline bool classof(const Value *V) {
1697 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1701 //===----------------------------------------------------------------------===//
1702 // ExtractElementInst Class
1703 //===----------------------------------------------------------------------===//
1705 /// ExtractElementInst - This instruction extracts a single (scalar)
1706 /// element from a VectorType value
1708 class ExtractElementInst : public Instruction {
1709 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1710 Instruction *InsertBefore = nullptr);
1711 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1712 BasicBlock *InsertAtEnd);
1714 ExtractElementInst *clone_impl() const override;
1717 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1718 const Twine &NameStr = "",
1719 Instruction *InsertBefore = nullptr) {
1720 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1722 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1723 const Twine &NameStr,
1724 BasicBlock *InsertAtEnd) {
1725 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1728 /// isValidOperands - Return true if an extractelement instruction can be
1729 /// formed with the specified operands.
1730 static bool isValidOperands(const Value *Vec, const Value *Idx);
1732 Value *getVectorOperand() { return Op<0>(); }
1733 Value *getIndexOperand() { return Op<1>(); }
1734 const Value *getVectorOperand() const { return Op<0>(); }
1735 const Value *getIndexOperand() const { return Op<1>(); }
1737 VectorType *getVectorOperandType() const {
1738 return cast<VectorType>(getVectorOperand()->getType());
1742 /// Transparently provide more efficient getOperand methods.
1743 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1745 // Methods for support type inquiry through isa, cast, and dyn_cast:
1746 static inline bool classof(const Instruction *I) {
1747 return I->getOpcode() == Instruction::ExtractElement;
1749 static inline bool classof(const Value *V) {
1750 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1755 struct OperandTraits<ExtractElementInst> :
1756 public FixedNumOperandTraits<ExtractElementInst, 2> {
1759 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1761 //===----------------------------------------------------------------------===//
1762 // InsertElementInst Class
1763 //===----------------------------------------------------------------------===//
1765 /// InsertElementInst - This instruction inserts a single (scalar)
1766 /// element into a VectorType value
1768 class InsertElementInst : public Instruction {
1769 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1770 const Twine &NameStr = "",
1771 Instruction *InsertBefore = nullptr);
1772 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1773 const Twine &NameStr, BasicBlock *InsertAtEnd);
1775 InsertElementInst *clone_impl() const override;
1778 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1779 const Twine &NameStr = "",
1780 Instruction *InsertBefore = nullptr) {
1781 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1783 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1784 const Twine &NameStr,
1785 BasicBlock *InsertAtEnd) {
1786 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1789 /// isValidOperands - Return true if an insertelement instruction can be
1790 /// formed with the specified operands.
1791 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1794 /// getType - Overload to return most specific vector type.
1796 VectorType *getType() const {
1797 return cast<VectorType>(Instruction::getType());
1800 /// Transparently provide more efficient getOperand methods.
1801 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1803 // Methods for support type inquiry through isa, cast, and dyn_cast:
1804 static inline bool classof(const Instruction *I) {
1805 return I->getOpcode() == Instruction::InsertElement;
1807 static inline bool classof(const Value *V) {
1808 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1813 struct OperandTraits<InsertElementInst> :
1814 public FixedNumOperandTraits<InsertElementInst, 3> {
1817 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1819 //===----------------------------------------------------------------------===//
1820 // ShuffleVectorInst Class
1821 //===----------------------------------------------------------------------===//
1823 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1826 class ShuffleVectorInst : public Instruction {
1828 ShuffleVectorInst *clone_impl() const override;
1831 // allocate space for exactly three operands
1832 void *operator new(size_t s) {
1833 return User::operator new(s, 3);
1835 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1836 const Twine &NameStr = "",
1837 Instruction *InsertBefor = nullptr);
1838 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1839 const Twine &NameStr, BasicBlock *InsertAtEnd);
1841 /// isValidOperands - Return true if a shufflevector instruction can be
1842 /// formed with the specified operands.
1843 static bool isValidOperands(const Value *V1, const Value *V2,
1846 /// getType - Overload to return most specific vector type.
1848 VectorType *getType() const {
1849 return cast<VectorType>(Instruction::getType());
1852 /// Transparently provide more efficient getOperand methods.
1853 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1855 Constant *getMask() const {
1856 return cast<Constant>(getOperand(2));
1859 /// getMaskValue - Return the index from the shuffle mask for the specified
1860 /// output result. This is either -1 if the element is undef or a number less
1861 /// than 2*numelements.
1862 static int getMaskValue(Constant *Mask, unsigned i);
1864 int getMaskValue(unsigned i) const {
1865 return getMaskValue(getMask(), i);
1868 /// getShuffleMask - Return the full mask for this instruction, where each
1869 /// element is the element number and undef's are returned as -1.
1870 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1872 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1873 return getShuffleMask(getMask(), Result);
1876 SmallVector<int, 16> getShuffleMask() const {
1877 SmallVector<int, 16> Mask;
1878 getShuffleMask(Mask);
1883 // Methods for support type inquiry through isa, cast, and dyn_cast:
1884 static inline bool classof(const Instruction *I) {
1885 return I->getOpcode() == Instruction::ShuffleVector;
1887 static inline bool classof(const Value *V) {
1888 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1893 struct OperandTraits<ShuffleVectorInst> :
1894 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1897 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1899 //===----------------------------------------------------------------------===//
1900 // ExtractValueInst Class
1901 //===----------------------------------------------------------------------===//
1903 /// ExtractValueInst - This instruction extracts a struct member or array
1904 /// element value from an aggregate value.
1906 class ExtractValueInst : public UnaryInstruction {
1907 SmallVector<unsigned, 4> Indices;
1909 ExtractValueInst(const ExtractValueInst &EVI);
1910 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1912 /// Constructors - Create a extractvalue instruction with a base aggregate
1913 /// value and a list of indices. The first ctor can optionally insert before
1914 /// an existing instruction, the second appends the new instruction to the
1915 /// specified BasicBlock.
1916 inline ExtractValueInst(Value *Agg,
1917 ArrayRef<unsigned> Idxs,
1918 const Twine &NameStr,
1919 Instruction *InsertBefore);
1920 inline ExtractValueInst(Value *Agg,
1921 ArrayRef<unsigned> Idxs,
1922 const Twine &NameStr, BasicBlock *InsertAtEnd);
1924 // allocate space for exactly one operand
1925 void *operator new(size_t s) {
1926 return User::operator new(s, 1);
1929 ExtractValueInst *clone_impl() const override;
1932 static ExtractValueInst *Create(Value *Agg,
1933 ArrayRef<unsigned> Idxs,
1934 const Twine &NameStr = "",
1935 Instruction *InsertBefore = nullptr) {
1937 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1939 static ExtractValueInst *Create(Value *Agg,
1940 ArrayRef<unsigned> Idxs,
1941 const Twine &NameStr,
1942 BasicBlock *InsertAtEnd) {
1943 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1946 /// getIndexedType - Returns the type of the element that would be extracted
1947 /// with an extractvalue instruction with the specified parameters.
1949 /// Null is returned if the indices are invalid for the specified type.
1950 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1952 typedef const unsigned* idx_iterator;
1953 inline idx_iterator idx_begin() const { return Indices.begin(); }
1954 inline idx_iterator idx_end() const { return Indices.end(); }
1955 inline iterator_range<idx_iterator> indices() const {
1956 return iterator_range<idx_iterator>(idx_begin(), idx_end());
1959 Value *getAggregateOperand() {
1960 return getOperand(0);
1962 const Value *getAggregateOperand() const {
1963 return getOperand(0);
1965 static unsigned getAggregateOperandIndex() {
1966 return 0U; // get index for modifying correct operand
1969 ArrayRef<unsigned> getIndices() const {
1973 unsigned getNumIndices() const {
1974 return (unsigned)Indices.size();
1977 bool hasIndices() const {
1981 // Methods for support type inquiry through isa, cast, and dyn_cast:
1982 static inline bool classof(const Instruction *I) {
1983 return I->getOpcode() == Instruction::ExtractValue;
1985 static inline bool classof(const Value *V) {
1986 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1990 ExtractValueInst::ExtractValueInst(Value *Agg,
1991 ArrayRef<unsigned> Idxs,
1992 const Twine &NameStr,
1993 Instruction *InsertBefore)
1994 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1995 ExtractValue, Agg, InsertBefore) {
1996 init(Idxs, NameStr);
1998 ExtractValueInst::ExtractValueInst(Value *Agg,
1999 ArrayRef<unsigned> Idxs,
2000 const Twine &NameStr,
2001 BasicBlock *InsertAtEnd)
2002 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2003 ExtractValue, Agg, InsertAtEnd) {
2004 init(Idxs, NameStr);
2008 //===----------------------------------------------------------------------===//
2009 // InsertValueInst Class
2010 //===----------------------------------------------------------------------===//
2012 /// InsertValueInst - This instruction inserts a struct field of array element
2013 /// value into an aggregate value.
2015 class InsertValueInst : public Instruction {
2016 SmallVector<unsigned, 4> Indices;
2018 void *operator new(size_t, unsigned) = delete;
2019 InsertValueInst(const InsertValueInst &IVI);
2020 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2021 const Twine &NameStr);
2023 /// Constructors - Create a insertvalue instruction with a base aggregate
2024 /// value, a value to insert, and a list of indices. The first ctor can
2025 /// optionally insert before an existing instruction, the second appends
2026 /// the new instruction to the specified BasicBlock.
2027 inline InsertValueInst(Value *Agg, Value *Val,
2028 ArrayRef<unsigned> Idxs,
2029 const Twine &NameStr,
2030 Instruction *InsertBefore);
2031 inline InsertValueInst(Value *Agg, Value *Val,
2032 ArrayRef<unsigned> Idxs,
2033 const Twine &NameStr, BasicBlock *InsertAtEnd);
2035 /// Constructors - These two constructors are convenience methods because one
2036 /// and two index insertvalue instructions are so common.
2037 InsertValueInst(Value *Agg, Value *Val,
2038 unsigned Idx, const Twine &NameStr = "",
2039 Instruction *InsertBefore = nullptr);
2040 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2041 const Twine &NameStr, BasicBlock *InsertAtEnd);
2043 InsertValueInst *clone_impl() const override;
2045 // allocate space for exactly two operands
2046 void *operator new(size_t s) {
2047 return User::operator new(s, 2);
2050 static InsertValueInst *Create(Value *Agg, Value *Val,
2051 ArrayRef<unsigned> Idxs,
2052 const Twine &NameStr = "",
2053 Instruction *InsertBefore = nullptr) {
2054 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2056 static InsertValueInst *Create(Value *Agg, Value *Val,
2057 ArrayRef<unsigned> Idxs,
2058 const Twine &NameStr,
2059 BasicBlock *InsertAtEnd) {
2060 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2063 /// Transparently provide more efficient getOperand methods.
2064 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2066 typedef const unsigned* idx_iterator;
2067 inline idx_iterator idx_begin() const { return Indices.begin(); }
2068 inline idx_iterator idx_end() const { return Indices.end(); }
2069 inline iterator_range<idx_iterator> indices() const {
2070 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2073 Value *getAggregateOperand() {
2074 return getOperand(0);
2076 const Value *getAggregateOperand() const {
2077 return getOperand(0);
2079 static unsigned getAggregateOperandIndex() {
2080 return 0U; // get index for modifying correct operand
2083 Value *getInsertedValueOperand() {
2084 return getOperand(1);
2086 const Value *getInsertedValueOperand() const {
2087 return getOperand(1);
2089 static unsigned getInsertedValueOperandIndex() {
2090 return 1U; // get index for modifying correct operand
2093 ArrayRef<unsigned> getIndices() const {
2097 unsigned getNumIndices() const {
2098 return (unsigned)Indices.size();
2101 bool hasIndices() const {
2105 // Methods for support type inquiry through isa, cast, and dyn_cast:
2106 static inline bool classof(const Instruction *I) {
2107 return I->getOpcode() == Instruction::InsertValue;
2109 static inline bool classof(const Value *V) {
2110 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2115 struct OperandTraits<InsertValueInst> :
2116 public FixedNumOperandTraits<InsertValueInst, 2> {
2119 InsertValueInst::InsertValueInst(Value *Agg,
2121 ArrayRef<unsigned> Idxs,
2122 const Twine &NameStr,
2123 Instruction *InsertBefore)
2124 : Instruction(Agg->getType(), InsertValue,
2125 OperandTraits<InsertValueInst>::op_begin(this),
2127 init(Agg, Val, Idxs, NameStr);
2129 InsertValueInst::InsertValueInst(Value *Agg,
2131 ArrayRef<unsigned> Idxs,
2132 const Twine &NameStr,
2133 BasicBlock *InsertAtEnd)
2134 : Instruction(Agg->getType(), InsertValue,
2135 OperandTraits<InsertValueInst>::op_begin(this),
2137 init(Agg, Val, Idxs, NameStr);
2140 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2142 //===----------------------------------------------------------------------===//
2144 //===----------------------------------------------------------------------===//
2146 // PHINode - The PHINode class is used to represent the magical mystical PHI
2147 // node, that can not exist in nature, but can be synthesized in a computer
2148 // scientist's overactive imagination.
2150 class PHINode : public Instruction {
2151 void *operator new(size_t, unsigned) = delete;
2152 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2153 /// the number actually in use.
2154 unsigned ReservedSpace;
2155 PHINode(const PHINode &PN);
2156 // allocate space for exactly zero operands
2157 void *operator new(size_t s) {
2158 return User::operator new(s, 0);
2160 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2161 const Twine &NameStr = "",
2162 Instruction *InsertBefore = nullptr)
2163 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2164 ReservedSpace(NumReservedValues) {
2166 OperandList = allocHungoffUses(ReservedSpace);
2169 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2170 BasicBlock *InsertAtEnd)
2171 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2172 ReservedSpace(NumReservedValues) {
2174 OperandList = allocHungoffUses(ReservedSpace);
2177 // allocHungoffUses - this is more complicated than the generic
2178 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2179 // values and pointers to the incoming blocks, all in one allocation.
2180 Use *allocHungoffUses(unsigned) const;
2182 PHINode *clone_impl() const override;
2184 /// Constructors - NumReservedValues is a hint for the number of incoming
2185 /// edges that this phi node will have (use 0 if you really have no idea).
2186 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2187 const Twine &NameStr = "",
2188 Instruction *InsertBefore = nullptr) {
2189 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2191 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2192 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2193 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2195 ~PHINode() override;
2197 /// Provide fast operand accessors
2198 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2200 // Block iterator interface. This provides access to the list of incoming
2201 // basic blocks, which parallels the list of incoming values.
2203 typedef BasicBlock **block_iterator;
2204 typedef BasicBlock * const *const_block_iterator;
2206 block_iterator block_begin() {
2208 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2209 return reinterpret_cast<block_iterator>(ref + 1);
2212 const_block_iterator block_begin() const {
2213 const Use::UserRef *ref =
2214 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2215 return reinterpret_cast<const_block_iterator>(ref + 1);
2218 block_iterator block_end() {
2219 return block_begin() + getNumOperands();
2222 const_block_iterator block_end() const {
2223 return block_begin() + getNumOperands();
2226 op_range incoming_values() { return operands(); }
2228 /// getNumIncomingValues - Return the number of incoming edges
2230 unsigned getNumIncomingValues() const { return getNumOperands(); }
2232 /// getIncomingValue - Return incoming value number x
2234 Value *getIncomingValue(unsigned i) const {
2235 return getOperand(i);
2237 void setIncomingValue(unsigned i, Value *V) {
2240 static unsigned getOperandNumForIncomingValue(unsigned i) {
2243 static unsigned getIncomingValueNumForOperand(unsigned i) {
2247 /// getIncomingBlock - Return incoming basic block number @p i.
2249 BasicBlock *getIncomingBlock(unsigned i) const {
2250 return block_begin()[i];
2253 /// getIncomingBlock - Return incoming basic block corresponding
2254 /// to an operand of the PHI.
2256 BasicBlock *getIncomingBlock(const Use &U) const {
2257 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2258 return getIncomingBlock(unsigned(&U - op_begin()));
2261 /// getIncomingBlock - Return incoming basic block corresponding
2262 /// to value use iterator.
2264 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2265 return getIncomingBlock(I.getUse());
2268 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2269 block_begin()[i] = BB;
2272 /// addIncoming - Add an incoming value to the end of the PHI list
2274 void addIncoming(Value *V, BasicBlock *BB) {
2275 assert(V && "PHI node got a null value!");
2276 assert(BB && "PHI node got a null basic block!");
2277 assert(getType() == V->getType() &&
2278 "All operands to PHI node must be the same type as the PHI node!");
2279 if (NumOperands == ReservedSpace)
2280 growOperands(); // Get more space!
2281 // Initialize some new operands.
2283 setIncomingValue(NumOperands - 1, V);
2284 setIncomingBlock(NumOperands - 1, BB);
2287 /// removeIncomingValue - Remove an incoming value. This is useful if a
2288 /// predecessor basic block is deleted. The value removed is returned.
2290 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2291 /// is true), the PHI node is destroyed and any uses of it are replaced with
2292 /// dummy values. The only time there should be zero incoming values to a PHI
2293 /// node is when the block is dead, so this strategy is sound.
2295 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2297 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2298 int Idx = getBasicBlockIndex(BB);
2299 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2300 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2303 /// getBasicBlockIndex - Return the first index of the specified basic
2304 /// block in the value list for this PHI. Returns -1 if no instance.
2306 int getBasicBlockIndex(const BasicBlock *BB) const {
2307 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2308 if (block_begin()[i] == BB)
2313 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2314 int Idx = getBasicBlockIndex(BB);
2315 assert(Idx >= 0 && "Invalid basic block argument!");
2316 return getIncomingValue(Idx);
2319 /// hasConstantValue - If the specified PHI node always merges together the
2320 /// same value, return the value, otherwise return null.
2321 Value *hasConstantValue() const;
2323 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2324 static inline bool classof(const Instruction *I) {
2325 return I->getOpcode() == Instruction::PHI;
2327 static inline bool classof(const Value *V) {
2328 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2331 void growOperands();
2335 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2338 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2340 //===----------------------------------------------------------------------===//
2341 // LandingPadInst Class
2342 //===----------------------------------------------------------------------===//
2344 //===---------------------------------------------------------------------------
2345 /// LandingPadInst - The landingpad instruction holds all of the information
2346 /// necessary to generate correct exception handling. The landingpad instruction
2347 /// cannot be moved from the top of a landing pad block, which itself is
2348 /// accessible only from the 'unwind' edge of an invoke. This uses the
2349 /// SubclassData field in Value to store whether or not the landingpad is a
2352 class LandingPadInst : public Instruction {
2353 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2354 /// the number actually in use.
2355 unsigned ReservedSpace;
2356 LandingPadInst(const LandingPadInst &LP);
2358 enum ClauseType { Catch, Filter };
2360 void *operator new(size_t, unsigned) = delete;
2361 // Allocate space for exactly zero operands.
2362 void *operator new(size_t s) {
2363 return User::operator new(s, 0);
2365 void growOperands(unsigned Size);
2366 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2368 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2369 unsigned NumReservedValues, const Twine &NameStr,
2370 Instruction *InsertBefore);
2371 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2372 unsigned NumReservedValues, const Twine &NameStr,
2373 BasicBlock *InsertAtEnd);
2375 LandingPadInst *clone_impl() const override;
2377 /// Constructors - NumReservedClauses is a hint for the number of incoming
2378 /// clauses that this landingpad will have (use 0 if you really have no idea).
2379 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2380 unsigned NumReservedClauses,
2381 const Twine &NameStr = "",
2382 Instruction *InsertBefore = nullptr);
2383 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2384 unsigned NumReservedClauses,
2385 const Twine &NameStr, BasicBlock *InsertAtEnd);
2386 ~LandingPadInst() override;
2388 /// Provide fast operand accessors
2389 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2391 /// getPersonalityFn - Get the personality function associated with this
2393 Value *getPersonalityFn() const { return getOperand(0); }
2395 /// isCleanup - Return 'true' if this landingpad instruction is a
2396 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2397 /// doesn't catch the exception.
2398 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2400 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2401 void setCleanup(bool V) {
2402 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2406 /// Add a catch or filter clause to the landing pad.
2407 void addClause(Constant *ClauseVal);
2409 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2410 /// determine what type of clause this is.
2411 Constant *getClause(unsigned Idx) const {
2412 return cast<Constant>(OperandList[Idx + 1]);
2415 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2416 bool isCatch(unsigned Idx) const {
2417 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2420 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2421 bool isFilter(unsigned Idx) const {
2422 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2425 /// getNumClauses - Get the number of clauses for this landing pad.
2426 unsigned getNumClauses() const { return getNumOperands() - 1; }
2428 /// reserveClauses - Grow the size of the operand list to accommodate the new
2429 /// number of clauses.
2430 void reserveClauses(unsigned Size) { growOperands(Size); }
2432 // Methods for support type inquiry through isa, cast, and dyn_cast:
2433 static inline bool classof(const Instruction *I) {
2434 return I->getOpcode() == Instruction::LandingPad;
2436 static inline bool classof(const Value *V) {
2437 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2442 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2445 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2447 //===----------------------------------------------------------------------===//
2449 //===----------------------------------------------------------------------===//
2451 //===---------------------------------------------------------------------------
2452 /// ReturnInst - Return a value (possibly void), from a function. Execution
2453 /// does not continue in this function any longer.
2455 class ReturnInst : public TerminatorInst {
2456 ReturnInst(const ReturnInst &RI);
2459 // ReturnInst constructors:
2460 // ReturnInst() - 'ret void' instruction
2461 // ReturnInst( null) - 'ret void' instruction
2462 // ReturnInst(Value* X) - 'ret X' instruction
2463 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2464 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2465 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2466 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2468 // NOTE: If the Value* passed is of type void then the constructor behaves as
2469 // if it was passed NULL.
2470 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2471 Instruction *InsertBefore = nullptr);
2472 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2473 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2475 ReturnInst *clone_impl() const override;
2477 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2478 Instruction *InsertBefore = nullptr) {
2479 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2481 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2482 BasicBlock *InsertAtEnd) {
2483 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2485 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2486 return new(0) ReturnInst(C, InsertAtEnd);
2488 ~ReturnInst() override;
2490 /// Provide fast operand accessors
2491 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2493 /// Convenience accessor. Returns null if there is no return value.
2494 Value *getReturnValue() const {
2495 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2498 unsigned getNumSuccessors() const { return 0; }
2500 // Methods for support type inquiry through isa, cast, and dyn_cast:
2501 static inline bool classof(const Instruction *I) {
2502 return (I->getOpcode() == Instruction::Ret);
2504 static inline bool classof(const Value *V) {
2505 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2508 BasicBlock *getSuccessorV(unsigned idx) const override;
2509 unsigned getNumSuccessorsV() const override;
2510 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2514 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2517 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2519 //===----------------------------------------------------------------------===//
2521 //===----------------------------------------------------------------------===//
2523 //===---------------------------------------------------------------------------
2524 /// BranchInst - Conditional or Unconditional Branch instruction.
2526 class BranchInst : public TerminatorInst {
2527 /// Ops list - Branches are strange. The operands are ordered:
2528 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2529 /// they don't have to check for cond/uncond branchness. These are mostly
2530 /// accessed relative from op_end().
2531 BranchInst(const BranchInst &BI);
2533 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2534 // BranchInst(BB *B) - 'br B'
2535 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2536 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2537 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2538 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2539 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2540 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2541 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2542 Instruction *InsertBefore = nullptr);
2543 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2544 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2545 BasicBlock *InsertAtEnd);
2547 BranchInst *clone_impl() const override;
2549 static BranchInst *Create(BasicBlock *IfTrue,
2550 Instruction *InsertBefore = nullptr) {
2551 return new(1) BranchInst(IfTrue, InsertBefore);
2553 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2554 Value *Cond, Instruction *InsertBefore = nullptr) {
2555 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2557 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2558 return new(1) BranchInst(IfTrue, InsertAtEnd);
2560 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2561 Value *Cond, BasicBlock *InsertAtEnd) {
2562 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2565 /// Transparently provide more efficient getOperand methods.
2566 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2568 bool isUnconditional() const { return getNumOperands() == 1; }
2569 bool isConditional() const { return getNumOperands() == 3; }
2571 Value *getCondition() const {
2572 assert(isConditional() && "Cannot get condition of an uncond branch!");
2576 void setCondition(Value *V) {
2577 assert(isConditional() && "Cannot set condition of unconditional branch!");
2581 unsigned getNumSuccessors() const { return 1+isConditional(); }
2583 BasicBlock *getSuccessor(unsigned i) const {
2584 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2585 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2588 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2589 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2590 *(&Op<-1>() - idx) = (Value*)NewSucc;
2593 /// \brief Swap the successors of this branch instruction.
2595 /// Swaps the successors of the branch instruction. This also swaps any
2596 /// branch weight metadata associated with the instruction so that it
2597 /// continues to map correctly to each operand.
2598 void swapSuccessors();
2600 // Methods for support type inquiry through isa, cast, and dyn_cast:
2601 static inline bool classof(const Instruction *I) {
2602 return (I->getOpcode() == Instruction::Br);
2604 static inline bool classof(const Value *V) {
2605 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2608 BasicBlock *getSuccessorV(unsigned idx) const override;
2609 unsigned getNumSuccessorsV() const override;
2610 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2614 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2617 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2619 //===----------------------------------------------------------------------===//
2621 //===----------------------------------------------------------------------===//
2623 //===---------------------------------------------------------------------------
2624 /// SwitchInst - Multiway switch
2626 class SwitchInst : public TerminatorInst {
2627 void *operator new(size_t, unsigned) = delete;
2628 unsigned ReservedSpace;
2629 // Operand[0] = Value to switch on
2630 // Operand[1] = Default basic block destination
2631 // Operand[2n ] = Value to match
2632 // Operand[2n+1] = BasicBlock to go to on match
2633 SwitchInst(const SwitchInst &SI);
2634 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2635 void growOperands();
2636 // allocate space for exactly zero operands
2637 void *operator new(size_t s) {
2638 return User::operator new(s, 0);
2640 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2641 /// switch on and a default destination. The number of additional cases can
2642 /// be specified here to make memory allocation more efficient. This
2643 /// constructor can also autoinsert before another instruction.
2644 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2645 Instruction *InsertBefore);
2647 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2648 /// switch on and a default destination. The number of additional cases can
2649 /// be specified here to make memory allocation more efficient. This
2650 /// constructor also autoinserts at the end of the specified BasicBlock.
2651 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2652 BasicBlock *InsertAtEnd);
2654 SwitchInst *clone_impl() const override;
2658 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2660 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2661 class CaseIteratorT {
2669 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2671 /// Initializes case iterator for given SwitchInst and for given
2673 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2678 /// Initializes case iterator for given SwitchInst and for given
2679 /// TerminatorInst's successor index.
2680 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2681 assert(SuccessorIndex < SI->getNumSuccessors() &&
2682 "Successor index # out of range!");
2683 return SuccessorIndex != 0 ?
2684 Self(SI, SuccessorIndex - 1) :
2685 Self(SI, DefaultPseudoIndex);
2688 /// Resolves case value for current case.
2689 ConstantIntTy *getCaseValue() {
2690 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2691 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2694 /// Resolves successor for current case.
2695 BasicBlockTy *getCaseSuccessor() {
2696 assert((Index < SI->getNumCases() ||
2697 Index == DefaultPseudoIndex) &&
2698 "Index out the number of cases.");
2699 return SI->getSuccessor(getSuccessorIndex());
2702 /// Returns number of current case.
2703 unsigned getCaseIndex() const { return Index; }
2705 /// Returns TerminatorInst's successor index for current case successor.
2706 unsigned getSuccessorIndex() const {
2707 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2708 "Index out the number of cases.");
2709 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2713 // Check index correctness after increment.
2714 // Note: Index == getNumCases() means end().
2715 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2719 Self operator++(int) {
2725 // Check index correctness after decrement.
2726 // Note: Index == getNumCases() means end().
2727 // Also allow "-1" iterator here. That will became valid after ++.
2728 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2729 "Index out the number of cases.");
2733 Self operator--(int) {
2738 bool operator==(const Self& RHS) const {
2739 assert(RHS.SI == SI && "Incompatible operators.");
2740 return RHS.Index == Index;
2742 bool operator!=(const Self& RHS) const {
2743 assert(RHS.SI == SI && "Incompatible operators.");
2744 return RHS.Index != Index;
2751 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2754 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2756 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2760 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2761 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2763 /// Sets the new value for current case.
2764 void setValue(ConstantInt *V) {
2765 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2766 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2769 /// Sets the new successor for current case.
2770 void setSuccessor(BasicBlock *S) {
2771 SI->setSuccessor(getSuccessorIndex(), S);
2775 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2777 Instruction *InsertBefore = nullptr) {
2778 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2780 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2781 unsigned NumCases, BasicBlock *InsertAtEnd) {
2782 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2785 ~SwitchInst() override;
2787 /// Provide fast operand accessors
2788 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2790 // Accessor Methods for Switch stmt
2791 Value *getCondition() const { return getOperand(0); }
2792 void setCondition(Value *V) { setOperand(0, V); }
2794 BasicBlock *getDefaultDest() const {
2795 return cast<BasicBlock>(getOperand(1));
2798 void setDefaultDest(BasicBlock *DefaultCase) {
2799 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2802 /// getNumCases - return the number of 'cases' in this switch instruction,
2803 /// except the default case
2804 unsigned getNumCases() const {
2805 return getNumOperands()/2 - 1;
2808 /// Returns a read/write iterator that points to the first
2809 /// case in SwitchInst.
2810 CaseIt case_begin() {
2811 return CaseIt(this, 0);
2813 /// Returns a read-only iterator that points to the first
2814 /// case in the SwitchInst.
2815 ConstCaseIt case_begin() const {
2816 return ConstCaseIt(this, 0);
2819 /// Returns a read/write iterator that points one past the last
2820 /// in the SwitchInst.
2822 return CaseIt(this, getNumCases());
2824 /// Returns a read-only iterator that points one past the last
2825 /// in the SwitchInst.
2826 ConstCaseIt case_end() const {
2827 return ConstCaseIt(this, getNumCases());
2830 /// cases - iteration adapter for range-for loops.
2831 iterator_range<CaseIt> cases() {
2832 return iterator_range<CaseIt>(case_begin(), case_end());
2835 /// cases - iteration adapter for range-for loops.
2836 iterator_range<ConstCaseIt> cases() const {
2837 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2840 /// Returns an iterator that points to the default case.
2841 /// Note: this iterator allows to resolve successor only. Attempt
2842 /// to resolve case value causes an assertion.
2843 /// Also note, that increment and decrement also causes an assertion and
2844 /// makes iterator invalid.
2845 CaseIt case_default() {
2846 return CaseIt(this, DefaultPseudoIndex);
2848 ConstCaseIt case_default() const {
2849 return ConstCaseIt(this, DefaultPseudoIndex);
2852 /// findCaseValue - Search all of the case values for the specified constant.
2853 /// If it is explicitly handled, return the case iterator of it, otherwise
2854 /// return default case iterator to indicate
2855 /// that it is handled by the default handler.
2856 CaseIt findCaseValue(const ConstantInt *C) {
2857 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2858 if (i.getCaseValue() == C)
2860 return case_default();
2862 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2863 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2864 if (i.getCaseValue() == C)
2866 return case_default();
2869 /// findCaseDest - Finds the unique case value for a given successor. Returns
2870 /// null if the successor is not found, not unique, or is the default case.
2871 ConstantInt *findCaseDest(BasicBlock *BB) {
2872 if (BB == getDefaultDest()) return nullptr;
2874 ConstantInt *CI = nullptr;
2875 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2876 if (i.getCaseSuccessor() == BB) {
2877 if (CI) return nullptr; // Multiple cases lead to BB.
2878 else CI = i.getCaseValue();
2884 /// addCase - Add an entry to the switch instruction...
2886 /// This action invalidates case_end(). Old case_end() iterator will
2887 /// point to the added case.
2888 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2890 /// removeCase - This method removes the specified case and its successor
2891 /// from the switch instruction. Note that this operation may reorder the
2892 /// remaining cases at index idx and above.
2894 /// This action invalidates iterators for all cases following the one removed,
2895 /// including the case_end() iterator.
2896 void removeCase(CaseIt i);
2898 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2899 BasicBlock *getSuccessor(unsigned idx) const {
2900 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2901 return cast<BasicBlock>(getOperand(idx*2+1));
2903 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2904 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2905 setOperand(idx*2+1, (Value*)NewSucc);
2908 // Methods for support type inquiry through isa, cast, and dyn_cast:
2909 static inline bool classof(const Instruction *I) {
2910 return I->getOpcode() == Instruction::Switch;
2912 static inline bool classof(const Value *V) {
2913 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2916 BasicBlock *getSuccessorV(unsigned idx) const override;
2917 unsigned getNumSuccessorsV() const override;
2918 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2922 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2925 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2928 //===----------------------------------------------------------------------===//
2929 // IndirectBrInst Class
2930 //===----------------------------------------------------------------------===//
2932 //===---------------------------------------------------------------------------
2933 /// IndirectBrInst - Indirect Branch Instruction.
2935 class IndirectBrInst : public TerminatorInst {
2936 void *operator new(size_t, unsigned) = delete;
2937 unsigned ReservedSpace;
2938 // Operand[0] = Value to switch on
2939 // Operand[1] = Default basic block destination
2940 // Operand[2n ] = Value to match
2941 // Operand[2n+1] = BasicBlock to go to on match
2942 IndirectBrInst(const IndirectBrInst &IBI);
2943 void init(Value *Address, unsigned NumDests);
2944 void growOperands();
2945 // allocate space for exactly zero operands
2946 void *operator new(size_t s) {
2947 return User::operator new(s, 0);
2949 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2950 /// Address to jump to. The number of expected destinations can be specified
2951 /// here to make memory allocation more efficient. This constructor can also
2952 /// autoinsert before another instruction.
2953 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2955 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2956 /// Address to jump to. The number of expected destinations can be specified
2957 /// here to make memory allocation more efficient. This constructor also
2958 /// autoinserts at the end of the specified BasicBlock.
2959 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2961 IndirectBrInst *clone_impl() const override;
2963 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2964 Instruction *InsertBefore = nullptr) {
2965 return new IndirectBrInst(Address, NumDests, InsertBefore);
2967 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2968 BasicBlock *InsertAtEnd) {
2969 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2971 ~IndirectBrInst() override;
2973 /// Provide fast operand accessors.
2974 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2976 // Accessor Methods for IndirectBrInst instruction.
2977 Value *getAddress() { return getOperand(0); }
2978 const Value *getAddress() const { return getOperand(0); }
2979 void setAddress(Value *V) { setOperand(0, V); }
2982 /// getNumDestinations - return the number of possible destinations in this
2983 /// indirectbr instruction.
2984 unsigned getNumDestinations() const { return getNumOperands()-1; }
2986 /// getDestination - Return the specified destination.
2987 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2988 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2990 /// addDestination - Add a destination.
2992 void addDestination(BasicBlock *Dest);
2994 /// removeDestination - This method removes the specified successor from the
2995 /// indirectbr instruction.
2996 void removeDestination(unsigned i);
2998 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2999 BasicBlock *getSuccessor(unsigned i) const {
3000 return cast<BasicBlock>(getOperand(i+1));
3002 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3003 setOperand(i+1, (Value*)NewSucc);
3006 // Methods for support type inquiry through isa, cast, and dyn_cast:
3007 static inline bool classof(const Instruction *I) {
3008 return I->getOpcode() == Instruction::IndirectBr;
3010 static inline bool classof(const Value *V) {
3011 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3014 BasicBlock *getSuccessorV(unsigned idx) const override;
3015 unsigned getNumSuccessorsV() const override;
3016 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3020 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3023 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3026 //===----------------------------------------------------------------------===//
3028 //===----------------------------------------------------------------------===//
3030 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3031 /// calling convention of the call.
3033 class InvokeInst : public TerminatorInst {
3034 AttributeSet AttributeList;
3035 InvokeInst(const InvokeInst &BI);
3036 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3037 ArrayRef<Value *> Args, const Twine &NameStr);
3039 /// Construct an InvokeInst given a range of arguments.
3041 /// \brief Construct an InvokeInst from a range of arguments
3042 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3043 ArrayRef<Value *> Args, unsigned Values,
3044 const Twine &NameStr, Instruction *InsertBefore);
3046 /// Construct an InvokeInst given a range of arguments.
3048 /// \brief Construct an InvokeInst from a range of arguments
3049 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3050 ArrayRef<Value *> Args, unsigned Values,
3051 const Twine &NameStr, BasicBlock *InsertAtEnd);
3053 InvokeInst *clone_impl() const override;
3055 static InvokeInst *Create(Value *Func,
3056 BasicBlock *IfNormal, BasicBlock *IfException,
3057 ArrayRef<Value *> Args, const Twine &NameStr = "",
3058 Instruction *InsertBefore = nullptr) {
3059 unsigned Values = unsigned(Args.size()) + 3;
3060 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3061 Values, NameStr, InsertBefore);
3063 static InvokeInst *Create(Value *Func,
3064 BasicBlock *IfNormal, BasicBlock *IfException,
3065 ArrayRef<Value *> Args, const Twine &NameStr,
3066 BasicBlock *InsertAtEnd) {
3067 unsigned Values = unsigned(Args.size()) + 3;
3068 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3069 Values, NameStr, InsertAtEnd);
3072 /// Provide fast operand accessors
3073 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3075 /// getNumArgOperands - Return the number of invoke arguments.
3077 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3079 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3081 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3082 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3084 /// arg_operands - iteration adapter for range-for loops.
3085 iterator_range<op_iterator> arg_operands() {
3086 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3089 /// arg_operands - iteration adapter for range-for loops.
3090 iterator_range<const_op_iterator> arg_operands() const {
3091 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3094 /// \brief Wrappers for getting the \c Use of a invoke argument.
3095 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3096 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3098 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3100 CallingConv::ID getCallingConv() const {
3101 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3103 void setCallingConv(CallingConv::ID CC) {
3104 setInstructionSubclassData(static_cast<unsigned>(CC));
3107 /// getAttributes - Return the parameter attributes for this invoke.
3109 const AttributeSet &getAttributes() const { return AttributeList; }
3111 /// setAttributes - Set the parameter attributes for this invoke.
3113 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3115 /// addAttribute - adds the attribute to the list of attributes.
3116 void addAttribute(unsigned i, Attribute::AttrKind attr);
3118 /// removeAttribute - removes the attribute from the list of attributes.
3119 void removeAttribute(unsigned i, Attribute attr);
3121 /// \brief adds the dereferenceable attribute to the list of attributes.
3122 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3124 /// \brief adds the dereferenceable_or_null attribute to the list of
3126 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
3128 /// \brief Determine whether this call has the given attribute.
3129 bool hasFnAttr(Attribute::AttrKind A) const {
3130 assert(A != Attribute::NoBuiltin &&
3131 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3132 return hasFnAttrImpl(A);
3135 /// \brief Determine whether the call or the callee has the given attributes.
3136 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3138 /// \brief Extract the alignment for a call or parameter (0=unknown).
3139 unsigned getParamAlignment(unsigned i) const {
3140 return AttributeList.getParamAlignment(i);
3143 /// \brief Extract the number of dereferenceable bytes for a call or
3144 /// parameter (0=unknown).
3145 uint64_t getDereferenceableBytes(unsigned i) const {
3146 return AttributeList.getDereferenceableBytes(i);
3149 /// \brief Return true if the call should not be treated as a call to a
3151 bool isNoBuiltin() const {
3152 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3153 // to check it by hand.
3154 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3155 !hasFnAttrImpl(Attribute::Builtin);
3158 /// \brief Return true if the call should not be inlined.
3159 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3160 void setIsNoInline() {
3161 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3164 /// \brief Determine if the call does not access memory.
3165 bool doesNotAccessMemory() const {
3166 return hasFnAttr(Attribute::ReadNone);
3168 void setDoesNotAccessMemory() {
3169 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3172 /// \brief Determine if the call does not access or only reads memory.
3173 bool onlyReadsMemory() const {
3174 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3176 void setOnlyReadsMemory() {
3177 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3180 /// \brief Determine if the call cannot return.
3181 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3182 void setDoesNotReturn() {
3183 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3186 /// \brief Determine if the call cannot unwind.
3187 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3188 void setDoesNotThrow() {
3189 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3192 /// \brief Determine if the invoke cannot be duplicated.
3193 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3194 void setCannotDuplicate() {
3195 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3198 /// \brief Determine if the call returns a structure through first
3199 /// pointer argument.
3200 bool hasStructRetAttr() const {
3201 // Be friendly and also check the callee.
3202 return paramHasAttr(1, Attribute::StructRet);
3205 /// \brief Determine if any call argument is an aggregate passed by value.
3206 bool hasByValArgument() const {
3207 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3210 /// getCalledFunction - Return the function called, or null if this is an
3211 /// indirect function invocation.
3213 Function *getCalledFunction() const {
3214 return dyn_cast<Function>(Op<-3>());
3217 /// getCalledValue - Get a pointer to the function that is invoked by this
3219 const Value *getCalledValue() const { return Op<-3>(); }
3220 Value *getCalledValue() { return Op<-3>(); }
3222 /// setCalledFunction - Set the function called.
3223 void setCalledFunction(Value* Fn) {
3227 // get*Dest - Return the destination basic blocks...
3228 BasicBlock *getNormalDest() const {
3229 return cast<BasicBlock>(Op<-2>());
3231 BasicBlock *getUnwindDest() const {
3232 return cast<BasicBlock>(Op<-1>());
3234 void setNormalDest(BasicBlock *B) {
3235 Op<-2>() = reinterpret_cast<Value*>(B);
3237 void setUnwindDest(BasicBlock *B) {
3238 Op<-1>() = reinterpret_cast<Value*>(B);
3241 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3242 /// block (the unwind destination).
3243 LandingPadInst *getLandingPadInst() const;
3245 BasicBlock *getSuccessor(unsigned i) const {
3246 assert(i < 2 && "Successor # out of range for invoke!");
3247 return i == 0 ? getNormalDest() : getUnwindDest();
3250 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3251 assert(idx < 2 && "Successor # out of range for invoke!");
3252 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3255 unsigned getNumSuccessors() const { return 2; }
3257 // Methods for support type inquiry through isa, cast, and dyn_cast:
3258 static inline bool classof(const Instruction *I) {
3259 return (I->getOpcode() == Instruction::Invoke);
3261 static inline bool classof(const Value *V) {
3262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3266 BasicBlock *getSuccessorV(unsigned idx) const override;
3267 unsigned getNumSuccessorsV() const override;
3268 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3270 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3272 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3273 // method so that subclasses cannot accidentally use it.
3274 void setInstructionSubclassData(unsigned short D) {
3275 Instruction::setInstructionSubclassData(D);
3280 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3283 InvokeInst::InvokeInst(Value *Func,
3284 BasicBlock *IfNormal, BasicBlock *IfException,
3285 ArrayRef<Value *> Args, unsigned Values,
3286 const Twine &NameStr, Instruction *InsertBefore)
3287 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3288 ->getElementType())->getReturnType(),
3289 Instruction::Invoke,
3290 OperandTraits<InvokeInst>::op_end(this) - Values,
3291 Values, InsertBefore) {
3292 init(Func, IfNormal, IfException, Args, NameStr);
3294 InvokeInst::InvokeInst(Value *Func,
3295 BasicBlock *IfNormal, BasicBlock *IfException,
3296 ArrayRef<Value *> Args, unsigned Values,
3297 const Twine &NameStr, BasicBlock *InsertAtEnd)
3298 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3299 ->getElementType())->getReturnType(),
3300 Instruction::Invoke,
3301 OperandTraits<InvokeInst>::op_end(this) - Values,
3302 Values, InsertAtEnd) {
3303 init(Func, IfNormal, IfException, Args, NameStr);
3306 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3308 //===----------------------------------------------------------------------===//
3310 //===----------------------------------------------------------------------===//
3312 //===---------------------------------------------------------------------------
3313 /// ResumeInst - Resume the propagation of an exception.
3315 class ResumeInst : public TerminatorInst {
3316 ResumeInst(const ResumeInst &RI);
3318 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3319 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3321 ResumeInst *clone_impl() const override;
3323 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3324 return new(1) ResumeInst(Exn, InsertBefore);
3326 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3327 return new(1) ResumeInst(Exn, InsertAtEnd);
3330 /// Provide fast operand accessors
3331 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3333 /// Convenience accessor.
3334 Value *getValue() const { return Op<0>(); }
3336 unsigned getNumSuccessors() const { return 0; }
3338 // Methods for support type inquiry through isa, cast, and dyn_cast:
3339 static inline bool classof(const Instruction *I) {
3340 return I->getOpcode() == Instruction::Resume;
3342 static inline bool classof(const Value *V) {
3343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3346 BasicBlock *getSuccessorV(unsigned idx) const override;
3347 unsigned getNumSuccessorsV() const override;
3348 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3352 struct OperandTraits<ResumeInst> :
3353 public FixedNumOperandTraits<ResumeInst, 1> {
3356 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3358 //===----------------------------------------------------------------------===//
3359 // UnreachableInst Class
3360 //===----------------------------------------------------------------------===//
3362 //===---------------------------------------------------------------------------
3363 /// UnreachableInst - This function has undefined behavior. In particular, the
3364 /// presence of this instruction indicates some higher level knowledge that the
3365 /// end of the block cannot be reached.
3367 class UnreachableInst : public TerminatorInst {
3368 void *operator new(size_t, unsigned) = delete;
3370 UnreachableInst *clone_impl() const override;
3373 // allocate space for exactly zero operands
3374 void *operator new(size_t s) {
3375 return User::operator new(s, 0);
3377 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3378 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3380 unsigned getNumSuccessors() const { return 0; }
3382 // Methods for support type inquiry through isa, cast, and dyn_cast:
3383 static inline bool classof(const Instruction *I) {
3384 return I->getOpcode() == Instruction::Unreachable;
3386 static inline bool classof(const Value *V) {
3387 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3390 BasicBlock *getSuccessorV(unsigned idx) const override;
3391 unsigned getNumSuccessorsV() const override;
3392 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3395 //===----------------------------------------------------------------------===//
3397 //===----------------------------------------------------------------------===//
3399 /// \brief This class represents a truncation of integer types.
3400 class TruncInst : public CastInst {
3402 /// \brief Clone an identical TruncInst
3403 TruncInst *clone_impl() const override;
3406 /// \brief Constructor with insert-before-instruction semantics
3408 Value *S, ///< The value to be truncated
3409 Type *Ty, ///< The (smaller) type to truncate to
3410 const Twine &NameStr = "", ///< A name for the new instruction
3411 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3414 /// \brief Constructor with insert-at-end-of-block semantics
3416 Value *S, ///< The value to be truncated
3417 Type *Ty, ///< The (smaller) type to truncate to
3418 const Twine &NameStr, ///< A name for the new instruction
3419 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3422 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3423 static inline bool classof(const Instruction *I) {
3424 return I->getOpcode() == Trunc;
3426 static inline bool classof(const Value *V) {
3427 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3431 //===----------------------------------------------------------------------===//
3433 //===----------------------------------------------------------------------===//
3435 /// \brief This class represents zero extension of integer types.
3436 class ZExtInst : public CastInst {
3438 /// \brief Clone an identical ZExtInst
3439 ZExtInst *clone_impl() const override;
3442 /// \brief Constructor with insert-before-instruction semantics
3444 Value *S, ///< The value to be zero extended
3445 Type *Ty, ///< The type to zero extend to
3446 const Twine &NameStr = "", ///< A name for the new instruction
3447 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3450 /// \brief Constructor with insert-at-end semantics.
3452 Value *S, ///< The value to be zero extended
3453 Type *Ty, ///< The type to zero extend to
3454 const Twine &NameStr, ///< A name for the new instruction
3455 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3458 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3459 static inline bool classof(const Instruction *I) {
3460 return I->getOpcode() == ZExt;
3462 static inline bool classof(const Value *V) {
3463 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3467 //===----------------------------------------------------------------------===//
3469 //===----------------------------------------------------------------------===//
3471 /// \brief This class represents a sign extension of integer types.
3472 class SExtInst : public CastInst {
3474 /// \brief Clone an identical SExtInst
3475 SExtInst *clone_impl() const override;
3478 /// \brief Constructor with insert-before-instruction semantics
3480 Value *S, ///< The value to be sign extended
3481 Type *Ty, ///< The type to sign extend to
3482 const Twine &NameStr = "", ///< A name for the new instruction
3483 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3486 /// \brief Constructor with insert-at-end-of-block semantics
3488 Value *S, ///< The value to be sign extended
3489 Type *Ty, ///< The type to sign extend to
3490 const Twine &NameStr, ///< A name for the new instruction
3491 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3494 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3495 static inline bool classof(const Instruction *I) {
3496 return I->getOpcode() == SExt;
3498 static inline bool classof(const Value *V) {
3499 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3503 //===----------------------------------------------------------------------===//
3504 // FPTruncInst Class
3505 //===----------------------------------------------------------------------===//
3507 /// \brief This class represents a truncation of floating point types.
3508 class FPTruncInst : public CastInst {
3510 /// \brief Clone an identical FPTruncInst
3511 FPTruncInst *clone_impl() const override;
3514 /// \brief Constructor with insert-before-instruction semantics
3516 Value *S, ///< The value to be truncated
3517 Type *Ty, ///< The type to truncate to
3518 const Twine &NameStr = "", ///< A name for the new instruction
3519 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3522 /// \brief Constructor with insert-before-instruction semantics
3524 Value *S, ///< The value to be truncated
3525 Type *Ty, ///< The type to truncate to
3526 const Twine &NameStr, ///< A name for the new instruction
3527 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3530 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3531 static inline bool classof(const Instruction *I) {
3532 return I->getOpcode() == FPTrunc;
3534 static inline bool classof(const Value *V) {
3535 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3539 //===----------------------------------------------------------------------===//
3541 //===----------------------------------------------------------------------===//
3543 /// \brief This class represents an extension of floating point types.
3544 class FPExtInst : public CastInst {
3546 /// \brief Clone an identical FPExtInst
3547 FPExtInst *clone_impl() const override;
3550 /// \brief Constructor with insert-before-instruction semantics
3552 Value *S, ///< The value to be extended
3553 Type *Ty, ///< The type to extend to
3554 const Twine &NameStr = "", ///< A name for the new instruction
3555 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3558 /// \brief Constructor with insert-at-end-of-block semantics
3560 Value *S, ///< The value to be extended
3561 Type *Ty, ///< The type to extend to
3562 const Twine &NameStr, ///< A name for the new instruction
3563 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3566 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3567 static inline bool classof(const Instruction *I) {
3568 return I->getOpcode() == FPExt;
3570 static inline bool classof(const Value *V) {
3571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3575 //===----------------------------------------------------------------------===//
3577 //===----------------------------------------------------------------------===//
3579 /// \brief This class represents a cast unsigned integer to floating point.
3580 class UIToFPInst : public CastInst {
3582 /// \brief Clone an identical UIToFPInst
3583 UIToFPInst *clone_impl() const override;
3586 /// \brief Constructor with insert-before-instruction semantics
3588 Value *S, ///< The value to be converted
3589 Type *Ty, ///< The type to convert to
3590 const Twine &NameStr = "", ///< A name for the new instruction
3591 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3594 /// \brief Constructor with insert-at-end-of-block semantics
3596 Value *S, ///< The value to be converted
3597 Type *Ty, ///< The type to convert to
3598 const Twine &NameStr, ///< A name for the new instruction
3599 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3602 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3603 static inline bool classof(const Instruction *I) {
3604 return I->getOpcode() == UIToFP;
3606 static inline bool classof(const Value *V) {
3607 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3611 //===----------------------------------------------------------------------===//
3613 //===----------------------------------------------------------------------===//
3615 /// \brief This class represents a cast from signed integer to floating point.
3616 class SIToFPInst : public CastInst {
3618 /// \brief Clone an identical SIToFPInst
3619 SIToFPInst *clone_impl() const override;
3622 /// \brief Constructor with insert-before-instruction semantics
3624 Value *S, ///< The value to be converted
3625 Type *Ty, ///< The type to convert to
3626 const Twine &NameStr = "", ///< A name for the new instruction
3627 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3630 /// \brief Constructor with insert-at-end-of-block semantics
3632 Value *S, ///< The value to be converted
3633 Type *Ty, ///< The type to convert to
3634 const Twine &NameStr, ///< A name for the new instruction
3635 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3638 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3639 static inline bool classof(const Instruction *I) {
3640 return I->getOpcode() == SIToFP;
3642 static inline bool classof(const Value *V) {
3643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3647 //===----------------------------------------------------------------------===//
3649 //===----------------------------------------------------------------------===//
3651 /// \brief This class represents a cast from floating point to unsigned integer
3652 class FPToUIInst : public CastInst {
3654 /// \brief Clone an identical FPToUIInst
3655 FPToUIInst *clone_impl() const override;
3658 /// \brief Constructor with insert-before-instruction semantics
3660 Value *S, ///< The value to be converted
3661 Type *Ty, ///< The type to convert to
3662 const Twine &NameStr = "", ///< A name for the new instruction
3663 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3666 /// \brief Constructor with insert-at-end-of-block semantics
3668 Value *S, ///< The value to be converted
3669 Type *Ty, ///< The type to convert to
3670 const Twine &NameStr, ///< A name for the new instruction
3671 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3674 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3675 static inline bool classof(const Instruction *I) {
3676 return I->getOpcode() == FPToUI;
3678 static inline bool classof(const Value *V) {
3679 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3683 //===----------------------------------------------------------------------===//
3685 //===----------------------------------------------------------------------===//
3687 /// \brief This class represents a cast from floating point to signed integer.
3688 class FPToSIInst : public CastInst {
3690 /// \brief Clone an identical FPToSIInst
3691 FPToSIInst *clone_impl() const override;
3694 /// \brief Constructor with insert-before-instruction semantics
3696 Value *S, ///< The value to be converted
3697 Type *Ty, ///< The type to convert to
3698 const Twine &NameStr = "", ///< A name for the new instruction
3699 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3702 /// \brief Constructor with insert-at-end-of-block semantics
3704 Value *S, ///< The value to be converted
3705 Type *Ty, ///< The type to convert to
3706 const Twine &NameStr, ///< A name for the new instruction
3707 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3710 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3711 static inline bool classof(const Instruction *I) {
3712 return I->getOpcode() == FPToSI;
3714 static inline bool classof(const Value *V) {
3715 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3719 //===----------------------------------------------------------------------===//
3720 // IntToPtrInst Class
3721 //===----------------------------------------------------------------------===//
3723 /// \brief This class represents a cast from an integer to a pointer.
3724 class IntToPtrInst : public CastInst {
3726 /// \brief Constructor with insert-before-instruction semantics
3728 Value *S, ///< The value to be converted
3729 Type *Ty, ///< The type to convert to
3730 const Twine &NameStr = "", ///< A name for the new instruction
3731 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3734 /// \brief Constructor with insert-at-end-of-block semantics
3736 Value *S, ///< The value to be converted
3737 Type *Ty, ///< The type to convert to
3738 const Twine &NameStr, ///< A name for the new instruction
3739 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3742 /// \brief Clone an identical IntToPtrInst
3743 IntToPtrInst *clone_impl() const override;
3745 /// \brief Returns the address space of this instruction's pointer type.
3746 unsigned getAddressSpace() const {
3747 return getType()->getPointerAddressSpace();
3750 // Methods for support type inquiry through isa, cast, and dyn_cast:
3751 static inline bool classof(const Instruction *I) {
3752 return I->getOpcode() == IntToPtr;
3754 static inline bool classof(const Value *V) {
3755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3759 //===----------------------------------------------------------------------===//
3760 // PtrToIntInst Class
3761 //===----------------------------------------------------------------------===//
3763 /// \brief This class represents a cast from a pointer to an integer
3764 class PtrToIntInst : public CastInst {
3766 /// \brief Clone an identical PtrToIntInst
3767 PtrToIntInst *clone_impl() const override;
3770 /// \brief Constructor with insert-before-instruction semantics
3772 Value *S, ///< The value to be converted
3773 Type *Ty, ///< The type to convert to
3774 const Twine &NameStr = "", ///< A name for the new instruction
3775 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3778 /// \brief Constructor with insert-at-end-of-block semantics
3780 Value *S, ///< The value to be converted
3781 Type *Ty, ///< The type to convert to
3782 const Twine &NameStr, ///< A name for the new instruction
3783 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3786 /// \brief Gets the pointer operand.
3787 Value *getPointerOperand() { return getOperand(0); }
3788 /// \brief Gets the pointer operand.
3789 const Value *getPointerOperand() const { return getOperand(0); }
3790 /// \brief Gets the operand index of the pointer operand.
3791 static unsigned getPointerOperandIndex() { return 0U; }
3793 /// \brief Returns the address space of the pointer operand.
3794 unsigned getPointerAddressSpace() const {
3795 return getPointerOperand()->getType()->getPointerAddressSpace();
3798 // Methods for support type inquiry through isa, cast, and dyn_cast:
3799 static inline bool classof(const Instruction *I) {
3800 return I->getOpcode() == PtrToInt;
3802 static inline bool classof(const Value *V) {
3803 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3807 //===----------------------------------------------------------------------===//
3808 // BitCastInst Class
3809 //===----------------------------------------------------------------------===//
3811 /// \brief This class represents a no-op cast from one type to another.
3812 class BitCastInst : public CastInst {
3814 /// \brief Clone an identical BitCastInst
3815 BitCastInst *clone_impl() const override;
3818 /// \brief Constructor with insert-before-instruction semantics
3820 Value *S, ///< The value to be casted
3821 Type *Ty, ///< The type to casted to
3822 const Twine &NameStr = "", ///< A name for the new instruction
3823 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3826 /// \brief Constructor with insert-at-end-of-block semantics
3828 Value *S, ///< The value to be casted
3829 Type *Ty, ///< The type to casted to
3830 const Twine &NameStr, ///< A name for the new instruction
3831 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3834 // Methods for support type inquiry through isa, cast, and dyn_cast:
3835 static inline bool classof(const Instruction *I) {
3836 return I->getOpcode() == BitCast;
3838 static inline bool classof(const Value *V) {
3839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3843 //===----------------------------------------------------------------------===//
3844 // AddrSpaceCastInst Class
3845 //===----------------------------------------------------------------------===//
3847 /// \brief This class represents a conversion between pointers from
3848 /// one address space to another.
3849 class AddrSpaceCastInst : public CastInst {
3851 /// \brief Clone an identical AddrSpaceCastInst
3852 AddrSpaceCastInst *clone_impl() const override;
3855 /// \brief Constructor with insert-before-instruction semantics
3857 Value *S, ///< The value to be casted
3858 Type *Ty, ///< The type to casted to
3859 const Twine &NameStr = "", ///< A name for the new instruction
3860 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3863 /// \brief Constructor with insert-at-end-of-block semantics
3865 Value *S, ///< The value to be casted
3866 Type *Ty, ///< The type to casted to
3867 const Twine &NameStr, ///< A name for the new instruction
3868 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3871 // Methods for support type inquiry through isa, cast, and dyn_cast:
3872 static inline bool classof(const Instruction *I) {
3873 return I->getOpcode() == AddrSpaceCast;
3875 static inline bool classof(const Value *V) {
3876 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3880 } // End llvm namespace