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,
180 unsigned Align, Instruction *InsertBefore = nullptr);
181 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
182 unsigned Align, BasicBlock *InsertAtEnd);
183 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
184 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
185 Instruction *InsertBefore = nullptr)
186 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
187 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
188 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
189 unsigned Align, AtomicOrdering Order,
190 SynchronizationScope SynchScope = CrossThread,
191 Instruction *InsertBefore = nullptr);
192 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
193 unsigned Align, AtomicOrdering Order,
194 SynchronizationScope SynchScope,
195 BasicBlock *InsertAtEnd);
197 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
198 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
199 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
200 bool isVolatile = false,
201 Instruction *InsertBefore = nullptr);
202 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
203 BasicBlock *InsertAtEnd);
205 /// isVolatile - Return true if this is a load from a volatile memory
208 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
210 /// setVolatile - Specify whether this is a volatile load or not.
212 void setVolatile(bool V) {
213 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
217 /// getAlignment - Return the alignment of the access that is being performed
219 unsigned getAlignment() const {
220 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
223 void setAlignment(unsigned Align);
225 /// Returns the ordering effect of this fence.
226 AtomicOrdering getOrdering() const {
227 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
230 /// Set the ordering constraint on this load. May not be Release or
232 void setOrdering(AtomicOrdering Ordering) {
233 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
237 SynchronizationScope getSynchScope() const {
238 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
241 /// Specify whether this load is ordered with respect to all
242 /// concurrently executing threads, or only with respect to signal handlers
243 /// executing in the same thread.
244 void setSynchScope(SynchronizationScope xthread) {
245 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
249 void setAtomic(AtomicOrdering Ordering,
250 SynchronizationScope SynchScope = CrossThread) {
251 setOrdering(Ordering);
252 setSynchScope(SynchScope);
255 bool isSimple() const { return !isAtomic() && !isVolatile(); }
256 bool isUnordered() const {
257 return getOrdering() <= Unordered && !isVolatile();
260 Value *getPointerOperand() { return getOperand(0); }
261 const Value *getPointerOperand() const { return getOperand(0); }
262 static unsigned getPointerOperandIndex() { return 0U; }
264 /// \brief Returns the address space of the pointer operand.
265 unsigned getPointerAddressSpace() const {
266 return getPointerOperand()->getType()->getPointerAddressSpace();
270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool classof(const Instruction *I) {
272 return I->getOpcode() == Instruction::Load;
274 static inline bool classof(const Value *V) {
275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
278 // Shadow Instruction::setInstructionSubclassData with a private forwarding
279 // method so that subclasses cannot accidentally use it.
280 void setInstructionSubclassData(unsigned short D) {
281 Instruction::setInstructionSubclassData(D);
286 //===----------------------------------------------------------------------===//
288 //===----------------------------------------------------------------------===//
290 /// StoreInst - an instruction for storing to memory
292 class StoreInst : public Instruction {
293 void *operator new(size_t, unsigned) = delete;
296 StoreInst *clone_impl() const override;
298 // allocate space for exactly two operands
299 void *operator new(size_t s) {
300 return User::operator new(s, 2);
302 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
303 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
304 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
305 Instruction *InsertBefore = nullptr);
306 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
307 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
308 unsigned Align, Instruction *InsertBefore = nullptr);
309 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
310 unsigned Align, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
312 unsigned Align, AtomicOrdering Order,
313 SynchronizationScope SynchScope = CrossThread,
314 Instruction *InsertBefore = nullptr);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, AtomicOrdering Order,
317 SynchronizationScope SynchScope,
318 BasicBlock *InsertAtEnd);
321 /// isVolatile - Return true if this is a store to a volatile memory
324 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
326 /// setVolatile - Specify whether this is a volatile store or not.
328 void setVolatile(bool V) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
333 /// Transparently provide more efficient getOperand methods.
334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
336 /// getAlignment - Return the alignment of the access that is being performed
338 unsigned getAlignment() const {
339 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
342 void setAlignment(unsigned Align);
344 /// Returns the ordering effect of this store.
345 AtomicOrdering getOrdering() const {
346 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
349 /// Set the ordering constraint on this store. May not be Acquire or
351 void setOrdering(AtomicOrdering Ordering) {
352 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
356 SynchronizationScope getSynchScope() const {
357 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
360 /// Specify whether this store instruction is ordered with respect to all
361 /// concurrently executing threads, or only with respect to signal handlers
362 /// executing in the same thread.
363 void setSynchScope(SynchronizationScope xthread) {
364 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
368 void setAtomic(AtomicOrdering Ordering,
369 SynchronizationScope SynchScope = CrossThread) {
370 setOrdering(Ordering);
371 setSynchScope(SynchScope);
374 bool isSimple() const { return !isAtomic() && !isVolatile(); }
375 bool isUnordered() const {
376 return getOrdering() <= Unordered && !isVolatile();
379 Value *getValueOperand() { return getOperand(0); }
380 const Value *getValueOperand() const { return getOperand(0); }
382 Value *getPointerOperand() { return getOperand(1); }
383 const Value *getPointerOperand() const { return getOperand(1); }
384 static unsigned getPointerOperandIndex() { return 1U; }
386 /// \brief Returns the address space of the pointer operand.
387 unsigned getPointerAddressSpace() const {
388 return getPointerOperand()->getType()->getPointerAddressSpace();
391 // Methods for support type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const Instruction *I) {
393 return I->getOpcode() == Instruction::Store;
395 static inline bool classof(const Value *V) {
396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
399 // Shadow Instruction::setInstructionSubclassData with a private forwarding
400 // method so that subclasses cannot accidentally use it.
401 void setInstructionSubclassData(unsigned short D) {
402 Instruction::setInstructionSubclassData(D);
407 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
410 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
412 //===----------------------------------------------------------------------===//
414 //===----------------------------------------------------------------------===//
416 /// FenceInst - an instruction for ordering other memory operations
418 class FenceInst : public Instruction {
419 void *operator new(size_t, unsigned) = delete;
420 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
422 FenceInst *clone_impl() const override;
424 // allocate space for exactly zero operands
425 void *operator new(size_t s) {
426 return User::operator new(s, 0);
429 // Ordering may only be Acquire, Release, AcquireRelease, or
430 // SequentiallyConsistent.
431 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
432 SynchronizationScope SynchScope = CrossThread,
433 Instruction *InsertBefore = nullptr);
434 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
435 SynchronizationScope SynchScope,
436 BasicBlock *InsertAtEnd);
438 /// Returns the ordering effect of this fence.
439 AtomicOrdering getOrdering() const {
440 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
443 /// Set the ordering constraint on this fence. May only be Acquire, Release,
444 /// AcquireRelease, or SequentiallyConsistent.
445 void setOrdering(AtomicOrdering Ordering) {
446 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
450 SynchronizationScope getSynchScope() const {
451 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
454 /// Specify whether this fence orders other operations with respect to all
455 /// concurrently executing threads, or only with respect to signal handlers
456 /// executing in the same thread.
457 void setSynchScope(SynchronizationScope xthread) {
458 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
462 // Methods for support type inquiry through isa, cast, and dyn_cast:
463 static inline bool classof(const Instruction *I) {
464 return I->getOpcode() == Instruction::Fence;
466 static inline bool classof(const Value *V) {
467 return isa<Instruction>(V) && classof(cast<Instruction>(V));
470 // Shadow Instruction::setInstructionSubclassData with a private forwarding
471 // method so that subclasses cannot accidentally use it.
472 void setInstructionSubclassData(unsigned short D) {
473 Instruction::setInstructionSubclassData(D);
477 //===----------------------------------------------------------------------===//
478 // AtomicCmpXchgInst Class
479 //===----------------------------------------------------------------------===//
481 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
482 /// specified value is in a memory location, and, if it is, stores a new value
483 /// there. Returns the value that was loaded.
485 class AtomicCmpXchgInst : public Instruction {
486 void *operator new(size_t, unsigned) = delete;
487 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
488 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
489 SynchronizationScope SynchScope);
491 AtomicCmpXchgInst *clone_impl() const override;
493 // allocate space for exactly three operands
494 void *operator new(size_t s) {
495 return User::operator new(s, 3);
497 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
498 AtomicOrdering SuccessOrdering,
499 AtomicOrdering FailureOrdering,
500 SynchronizationScope SynchScope,
501 Instruction *InsertBefore = nullptr);
502 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
503 AtomicOrdering SuccessOrdering,
504 AtomicOrdering FailureOrdering,
505 SynchronizationScope SynchScope,
506 BasicBlock *InsertAtEnd);
508 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
511 bool isVolatile() const {
512 return getSubclassDataFromInstruction() & 1;
515 /// setVolatile - Specify whether this is a volatile cmpxchg.
517 void setVolatile(bool V) {
518 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
522 /// Return true if this cmpxchg may spuriously fail.
523 bool isWeak() const {
524 return getSubclassDataFromInstruction() & 0x100;
527 void setWeak(bool IsWeak) {
528 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
532 /// Transparently provide more efficient getOperand methods.
533 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
535 /// Set the ordering constraint on this cmpxchg.
536 void setSuccessOrdering(AtomicOrdering Ordering) {
537 assert(Ordering != NotAtomic &&
538 "CmpXchg instructions can only be atomic.");
539 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
543 void setFailureOrdering(AtomicOrdering Ordering) {
544 assert(Ordering != NotAtomic &&
545 "CmpXchg instructions can only be atomic.");
546 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
550 /// Specify whether this cmpxchg is atomic and orders other operations with
551 /// respect to all concurrently executing threads, or only with respect to
552 /// signal handlers executing in the same thread.
553 void setSynchScope(SynchronizationScope SynchScope) {
554 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
558 /// Returns the ordering constraint on this cmpxchg.
559 AtomicOrdering getSuccessOrdering() const {
560 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
563 /// Returns the ordering constraint on this cmpxchg.
564 AtomicOrdering getFailureOrdering() const {
565 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
568 /// Returns whether this cmpxchg is atomic between threads or only within a
570 SynchronizationScope getSynchScope() const {
571 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
574 Value *getPointerOperand() { return getOperand(0); }
575 const Value *getPointerOperand() const { return getOperand(0); }
576 static unsigned getPointerOperandIndex() { return 0U; }
578 Value *getCompareOperand() { return getOperand(1); }
579 const Value *getCompareOperand() const { return getOperand(1); }
581 Value *getNewValOperand() { return getOperand(2); }
582 const Value *getNewValOperand() const { return getOperand(2); }
584 /// \brief Returns the address space of the pointer operand.
585 unsigned getPointerAddressSpace() const {
586 return getPointerOperand()->getType()->getPointerAddressSpace();
589 /// \brief Returns the strongest permitted ordering on failure, given the
590 /// desired ordering on success.
592 /// If the comparison in a cmpxchg operation fails, there is no atomic store
593 /// so release semantics cannot be provided. So this function drops explicit
594 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
595 /// operation would remain SequentiallyConsistent.
596 static AtomicOrdering
597 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
598 switch (SuccessOrdering) {
599 default: llvm_unreachable("invalid cmpxchg success ordering");
606 case SequentiallyConsistent:
607 return SequentiallyConsistent;
611 // Methods for support type inquiry through isa, cast, and dyn_cast:
612 static inline bool classof(const Instruction *I) {
613 return I->getOpcode() == Instruction::AtomicCmpXchg;
615 static inline bool classof(const Value *V) {
616 return isa<Instruction>(V) && classof(cast<Instruction>(V));
619 // Shadow Instruction::setInstructionSubclassData with a private forwarding
620 // method so that subclasses cannot accidentally use it.
621 void setInstructionSubclassData(unsigned short D) {
622 Instruction::setInstructionSubclassData(D);
627 struct OperandTraits<AtomicCmpXchgInst> :
628 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
631 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
633 //===----------------------------------------------------------------------===//
634 // AtomicRMWInst Class
635 //===----------------------------------------------------------------------===//
637 /// AtomicRMWInst - an instruction that atomically reads a memory location,
638 /// combines it with another value, and then stores the result back. Returns
641 class AtomicRMWInst : public Instruction {
642 void *operator new(size_t, unsigned) = delete;
644 AtomicRMWInst *clone_impl() const override;
646 /// This enumeration lists the possible modifications atomicrmw can make. In
647 /// the descriptions, 'p' is the pointer to the instruction's memory location,
648 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
649 /// instruction. These instructions always return 'old'.
665 /// *p = old >signed v ? old : v
667 /// *p = old <signed v ? old : v
669 /// *p = old >unsigned v ? old : v
671 /// *p = old <unsigned v ? old : v
679 // allocate space for exactly two operands
680 void *operator new(size_t s) {
681 return User::operator new(s, 2);
683 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
684 AtomicOrdering Ordering, SynchronizationScope SynchScope,
685 Instruction *InsertBefore = nullptr);
686 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
687 AtomicOrdering Ordering, SynchronizationScope SynchScope,
688 BasicBlock *InsertAtEnd);
690 BinOp getOperation() const {
691 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
694 void setOperation(BinOp Operation) {
695 unsigned short SubclassData = getSubclassDataFromInstruction();
696 setInstructionSubclassData((SubclassData & 31) |
700 /// isVolatile - Return true if this is a RMW on a volatile memory location.
702 bool isVolatile() const {
703 return getSubclassDataFromInstruction() & 1;
706 /// setVolatile - Specify whether this is a volatile RMW or not.
708 void setVolatile(bool V) {
709 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
713 /// Transparently provide more efficient getOperand methods.
714 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
716 /// Set the ordering constraint on this RMW.
717 void setOrdering(AtomicOrdering Ordering) {
718 assert(Ordering != NotAtomic &&
719 "atomicrmw instructions can only be atomic.");
720 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
724 /// Specify whether this RMW orders other operations with respect to all
725 /// concurrently executing threads, or only with respect to signal handlers
726 /// executing in the same thread.
727 void setSynchScope(SynchronizationScope SynchScope) {
728 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
732 /// Returns the ordering constraint on this RMW.
733 AtomicOrdering getOrdering() const {
734 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
737 /// Returns whether this RMW is atomic between threads or only within a
739 SynchronizationScope getSynchScope() const {
740 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
743 Value *getPointerOperand() { return getOperand(0); }
744 const Value *getPointerOperand() const { return getOperand(0); }
745 static unsigned getPointerOperandIndex() { return 0U; }
747 Value *getValOperand() { return getOperand(1); }
748 const Value *getValOperand() const { return getOperand(1); }
750 /// \brief Returns the address space of the pointer operand.
751 unsigned getPointerAddressSpace() const {
752 return getPointerOperand()->getType()->getPointerAddressSpace();
755 // Methods for support type inquiry through isa, cast, and dyn_cast:
756 static inline bool classof(const Instruction *I) {
757 return I->getOpcode() == Instruction::AtomicRMW;
759 static inline bool classof(const Value *V) {
760 return isa<Instruction>(V) && classof(cast<Instruction>(V));
763 void Init(BinOp Operation, Value *Ptr, Value *Val,
764 AtomicOrdering Ordering, SynchronizationScope SynchScope);
765 // Shadow Instruction::setInstructionSubclassData with a private forwarding
766 // method so that subclasses cannot accidentally use it.
767 void setInstructionSubclassData(unsigned short D) {
768 Instruction::setInstructionSubclassData(D);
773 struct OperandTraits<AtomicRMWInst>
774 : public FixedNumOperandTraits<AtomicRMWInst,2> {
777 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
779 //===----------------------------------------------------------------------===//
780 // GetElementPtrInst Class
781 //===----------------------------------------------------------------------===//
783 // checkGEPType - Simple wrapper function to give a better assertion failure
784 // message on bad indexes for a gep instruction.
786 inline Type *checkGEPType(Type *Ty) {
787 assert(Ty && "Invalid GetElementPtrInst indices for type!");
791 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
792 /// access elements of arrays and structs
794 class GetElementPtrInst : public Instruction {
795 GetElementPtrInst(const GetElementPtrInst &GEPI);
796 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
798 /// Constructors - Create a getelementptr instruction with a base pointer an
799 /// list of indices. The first ctor can optionally insert before an existing
800 /// instruction, the second appends the new instruction to the specified
802 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
803 ArrayRef<Value *> IdxList, unsigned Values,
804 const Twine &NameStr, Instruction *InsertBefore);
805 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
806 ArrayRef<Value *> IdxList, unsigned Values,
807 const Twine &NameStr, BasicBlock *InsertAtEnd);
810 GetElementPtrInst *clone_impl() const override;
812 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
813 ArrayRef<Value *> IdxList,
814 const Twine &NameStr = "",
815 Instruction *InsertBefore = nullptr) {
816 unsigned Values = 1 + unsigned(IdxList.size());
817 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
818 NameStr, InsertBefore);
820 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
821 ArrayRef<Value *> IdxList,
822 const Twine &NameStr,
823 BasicBlock *InsertAtEnd) {
824 unsigned Values = 1 + unsigned(IdxList.size());
825 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
826 NameStr, InsertAtEnd);
829 /// Create an "inbounds" getelementptr. See the documentation for the
830 /// "inbounds" flag in LangRef.html for details.
831 static GetElementPtrInst *CreateInBounds(Value *Ptr,
832 ArrayRef<Value *> IdxList,
833 const Twine &NameStr = "",
834 Instruction *InsertBefore = nullptr){
835 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
837 static GetElementPtrInst *
838 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
839 const Twine &NameStr = "",
840 Instruction *InsertBefore = nullptr) {
841 GetElementPtrInst *GEP =
842 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
843 GEP->setIsInBounds(true);
846 static GetElementPtrInst *CreateInBounds(Value *Ptr,
847 ArrayRef<Value *> IdxList,
848 const Twine &NameStr,
849 BasicBlock *InsertAtEnd) {
850 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
852 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
853 ArrayRef<Value *> IdxList,
854 const Twine &NameStr,
855 BasicBlock *InsertAtEnd) {
856 GetElementPtrInst *GEP =
857 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
858 GEP->setIsInBounds(true);
862 /// Transparently provide more efficient getOperand methods.
863 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
865 // getType - Overload to return most specific sequential type.
866 SequentialType *getType() const {
867 return cast<SequentialType>(Instruction::getType());
870 Type *getSourceElementType() const {
871 return cast<SequentialType>(getPointerOperandType()->getScalarType())
875 Type *getResultElementType() const { return getType()->getElementType(); }
877 /// \brief Returns the address space of this instruction's pointer type.
878 unsigned getAddressSpace() const {
879 // Note that this is always the same as the pointer operand's address space
880 // and that is cheaper to compute, so cheat here.
881 return getPointerAddressSpace();
884 /// getIndexedType - Returns the type of the element that would be loaded with
885 /// a load instruction with the specified parameters.
887 /// Null is returned if the indices are invalid for the specified
890 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
891 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
892 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
894 inline op_iterator idx_begin() { return op_begin()+1; }
895 inline const_op_iterator idx_begin() const { return op_begin()+1; }
896 inline op_iterator idx_end() { return op_end(); }
897 inline const_op_iterator idx_end() const { return op_end(); }
899 Value *getPointerOperand() {
900 return getOperand(0);
902 const Value *getPointerOperand() const {
903 return getOperand(0);
905 static unsigned getPointerOperandIndex() {
906 return 0U; // get index for modifying correct operand.
909 /// getPointerOperandType - Method to return the pointer operand as a
911 Type *getPointerOperandType() const {
912 return getPointerOperand()->getType();
915 /// \brief Returns the address space of the pointer operand.
916 unsigned getPointerAddressSpace() const {
917 return getPointerOperandType()->getPointerAddressSpace();
920 /// GetGEPReturnType - Returns the pointer type returned by the GEP
921 /// instruction, which may be a vector of pointers.
922 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
924 PointerType::get(checkGEPType(getIndexedType(
925 cast<PointerType>(Ptr->getType()->getScalarType())
928 Ptr->getType()->getPointerAddressSpace());
930 if (Ptr->getType()->isVectorTy()) {
931 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
932 return VectorType::get(PtrTy, NumElem);
939 unsigned getNumIndices() const { // Note: always non-negative
940 return getNumOperands() - 1;
943 bool hasIndices() const {
944 return getNumOperands() > 1;
947 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
948 /// zeros. If so, the result pointer and the first operand have the same
949 /// value, just potentially different types.
950 bool hasAllZeroIndices() const;
952 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
953 /// constant integers. If so, the result pointer and the first operand have
954 /// a constant offset between them.
955 bool hasAllConstantIndices() const;
957 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
958 /// See LangRef.html for the meaning of inbounds on a getelementptr.
959 void setIsInBounds(bool b = true);
961 /// isInBounds - Determine whether the GEP has the inbounds flag.
962 bool isInBounds() const;
964 /// \brief Accumulate the constant address offset of this GEP if possible.
966 /// This routine accepts an APInt into which it will accumulate the constant
967 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
968 /// all-constant, it returns false and the value of the offset APInt is
969 /// undefined (it is *not* preserved!). The APInt passed into this routine
970 /// must be at least as wide as the IntPtr type for the address space of
971 /// the base GEP pointer.
972 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
974 // Methods for support type inquiry through isa, cast, and dyn_cast:
975 static inline bool classof(const Instruction *I) {
976 return (I->getOpcode() == Instruction::GetElementPtr);
978 static inline bool classof(const Value *V) {
979 return isa<Instruction>(V) && classof(cast<Instruction>(V));
984 struct OperandTraits<GetElementPtrInst> :
985 public VariadicOperandTraits<GetElementPtrInst, 1> {
988 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
989 ArrayRef<Value *> IdxList, unsigned Values,
990 const Twine &NameStr,
991 Instruction *InsertBefore)
992 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
993 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
994 Values, InsertBefore) {
995 init(Ptr, IdxList, NameStr);
996 assert(!PointeeType || PointeeType == getSourceElementType());
998 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
999 ArrayRef<Value *> IdxList, unsigned Values,
1000 const Twine &NameStr,
1001 BasicBlock *InsertAtEnd)
1002 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
1003 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1004 Values, InsertAtEnd) {
1005 init(Ptr, IdxList, NameStr);
1006 assert(!PointeeType || PointeeType == getSourceElementType());
1010 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1013 //===----------------------------------------------------------------------===//
1015 //===----------------------------------------------------------------------===//
1017 /// This instruction compares its operands according to the predicate given
1018 /// to the constructor. It only operates on integers or pointers. The operands
1019 /// must be identical types.
1020 /// \brief Represent an integer comparison operator.
1021 class ICmpInst: public CmpInst {
1023 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1024 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1025 "Invalid ICmp predicate value");
1026 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1027 "Both operands to ICmp instruction are not of the same type!");
1028 // Check that the operands are the right type
1029 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1030 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1031 "Invalid operand types for ICmp instruction");
1035 /// \brief Clone an identical ICmpInst
1036 ICmpInst *clone_impl() const override;
1038 /// \brief Constructor with insert-before-instruction semantics.
1040 Instruction *InsertBefore, ///< Where to insert
1041 Predicate pred, ///< The predicate to use for the comparison
1042 Value *LHS, ///< The left-hand-side of the expression
1043 Value *RHS, ///< The right-hand-side of the expression
1044 const Twine &NameStr = "" ///< Name of the instruction
1045 ) : CmpInst(makeCmpResultType(LHS->getType()),
1046 Instruction::ICmp, pred, LHS, RHS, NameStr,
1053 /// \brief Constructor with insert-at-end semantics.
1055 BasicBlock &InsertAtEnd, ///< Block to insert into.
1056 Predicate pred, ///< The predicate to use for the comparison
1057 Value *LHS, ///< The left-hand-side of the expression
1058 Value *RHS, ///< The right-hand-side of the expression
1059 const Twine &NameStr = "" ///< Name of the instruction
1060 ) : CmpInst(makeCmpResultType(LHS->getType()),
1061 Instruction::ICmp, pred, LHS, RHS, NameStr,
1068 /// \brief Constructor with no-insertion semantics
1070 Predicate pred, ///< The predicate to use for the comparison
1071 Value *LHS, ///< The left-hand-side of the expression
1072 Value *RHS, ///< The right-hand-side of the expression
1073 const Twine &NameStr = "" ///< Name of the instruction
1074 ) : CmpInst(makeCmpResultType(LHS->getType()),
1075 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1081 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1082 /// @returns the predicate that would be the result if the operand were
1083 /// regarded as signed.
1084 /// \brief Return the signed version of the predicate
1085 Predicate getSignedPredicate() const {
1086 return getSignedPredicate(getPredicate());
1089 /// This is a static version that you can use without an instruction.
1090 /// \brief Return the signed version of the predicate.
1091 static Predicate getSignedPredicate(Predicate pred);
1093 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1094 /// @returns the predicate that would be the result if the operand were
1095 /// regarded as unsigned.
1096 /// \brief Return the unsigned version of the predicate
1097 Predicate getUnsignedPredicate() const {
1098 return getUnsignedPredicate(getPredicate());
1101 /// This is a static version that you can use without an instruction.
1102 /// \brief Return the unsigned version of the predicate.
1103 static Predicate getUnsignedPredicate(Predicate pred);
1105 /// isEquality - Return true if this predicate is either EQ or NE. This also
1106 /// tests for commutativity.
1107 static bool isEquality(Predicate P) {
1108 return P == ICMP_EQ || P == ICMP_NE;
1111 /// isEquality - Return true if this predicate is either EQ or NE. This also
1112 /// tests for commutativity.
1113 bool isEquality() const {
1114 return isEquality(getPredicate());
1117 /// @returns true if the predicate of this ICmpInst is commutative
1118 /// \brief Determine if this relation is commutative.
1119 bool isCommutative() const { return isEquality(); }
1121 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1123 bool isRelational() const {
1124 return !isEquality();
1127 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1129 static bool isRelational(Predicate P) {
1130 return !isEquality(P);
1133 /// Initialize a set of values that all satisfy the predicate with C.
1134 /// \brief Make a ConstantRange for a relation with a constant value.
1135 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1137 /// Exchange the two operands to this instruction in such a way that it does
1138 /// not modify the semantics of the instruction. The predicate value may be
1139 /// changed to retain the same result if the predicate is order dependent
1141 /// \brief Swap operands and adjust predicate.
1142 void swapOperands() {
1143 setPredicate(getSwappedPredicate());
1144 Op<0>().swap(Op<1>());
1147 // Methods for support type inquiry through isa, cast, and dyn_cast:
1148 static inline bool classof(const Instruction *I) {
1149 return I->getOpcode() == Instruction::ICmp;
1151 static inline bool classof(const Value *V) {
1152 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1157 //===----------------------------------------------------------------------===//
1159 //===----------------------------------------------------------------------===//
1161 /// This instruction compares its operands according to the predicate given
1162 /// to the constructor. It only operates on floating point values or packed
1163 /// vectors of floating point values. The operands must be identical types.
1164 /// \brief Represents a floating point comparison operator.
1165 class FCmpInst: public CmpInst {
1167 /// \brief Clone an identical FCmpInst
1168 FCmpInst *clone_impl() const override;
1170 /// \brief Constructor with insert-before-instruction semantics.
1172 Instruction *InsertBefore, ///< Where to insert
1173 Predicate pred, ///< The predicate to use for the comparison
1174 Value *LHS, ///< The left-hand-side of the expression
1175 Value *RHS, ///< The right-hand-side of the expression
1176 const Twine &NameStr = "" ///< Name of the instruction
1177 ) : CmpInst(makeCmpResultType(LHS->getType()),
1178 Instruction::FCmp, pred, LHS, RHS, NameStr,
1180 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1181 "Invalid FCmp predicate value");
1182 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1183 "Both operands to FCmp instruction are not of the same type!");
1184 // Check that the operands are the right type
1185 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1186 "Invalid operand types for FCmp instruction");
1189 /// \brief Constructor with insert-at-end semantics.
1191 BasicBlock &InsertAtEnd, ///< Block to insert into.
1192 Predicate pred, ///< The predicate to use for the comparison
1193 Value *LHS, ///< The left-hand-side of the expression
1194 Value *RHS, ///< The right-hand-side of the expression
1195 const Twine &NameStr = "" ///< Name of the instruction
1196 ) : CmpInst(makeCmpResultType(LHS->getType()),
1197 Instruction::FCmp, pred, LHS, RHS, NameStr,
1199 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1200 "Invalid FCmp predicate value");
1201 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1202 "Both operands to FCmp instruction are not of the same type!");
1203 // Check that the operands are the right type
1204 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1205 "Invalid operand types for FCmp instruction");
1208 /// \brief Constructor with no-insertion semantics
1210 Predicate pred, ///< The predicate to use for the comparison
1211 Value *LHS, ///< The left-hand-side of the expression
1212 Value *RHS, ///< The right-hand-side of the expression
1213 const Twine &NameStr = "" ///< Name of the instruction
1214 ) : CmpInst(makeCmpResultType(LHS->getType()),
1215 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1216 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1217 "Invalid FCmp predicate value");
1218 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1219 "Both operands to FCmp instruction are not of the same type!");
1220 // Check that the operands are the right type
1221 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1222 "Invalid operand types for FCmp instruction");
1225 /// @returns true if the predicate of this instruction is EQ or NE.
1226 /// \brief Determine if this is an equality predicate.
1227 static bool isEquality(Predicate Pred) {
1228 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1232 /// @returns true if the predicate of this instruction is EQ or NE.
1233 /// \brief Determine if this is an equality predicate.
1234 bool isEquality() const { return isEquality(getPredicate()); }
1236 /// @returns true if the predicate of this instruction is commutative.
1237 /// \brief Determine if this is a commutative predicate.
1238 bool isCommutative() const {
1239 return isEquality() ||
1240 getPredicate() == FCMP_FALSE ||
1241 getPredicate() == FCMP_TRUE ||
1242 getPredicate() == FCMP_ORD ||
1243 getPredicate() == FCMP_UNO;
1246 /// @returns true if the predicate is relational (not EQ or NE).
1247 /// \brief Determine if this a relational predicate.
1248 bool isRelational() const { return !isEquality(); }
1250 /// Exchange the two operands to this instruction in such a way that it does
1251 /// not modify the semantics of the instruction. The predicate value may be
1252 /// changed to retain the same result if the predicate is order dependent
1254 /// \brief Swap operands and adjust predicate.
1255 void swapOperands() {
1256 setPredicate(getSwappedPredicate());
1257 Op<0>().swap(Op<1>());
1260 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1261 static inline bool classof(const Instruction *I) {
1262 return I->getOpcode() == Instruction::FCmp;
1264 static inline bool classof(const Value *V) {
1265 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1269 //===----------------------------------------------------------------------===//
1270 /// CallInst - This class represents a function call, abstracting a target
1271 /// machine's calling convention. This class uses low bit of the SubClassData
1272 /// field to indicate whether or not this is a tail call. The rest of the bits
1273 /// hold the calling convention of the call.
1275 class CallInst : public Instruction {
1276 AttributeSet AttributeList; ///< parameter attributes for call
1277 CallInst(const CallInst &CI);
1278 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1279 void init(Value *Func, const Twine &NameStr);
1281 /// Construct a CallInst given a range of arguments.
1282 /// \brief Construct a CallInst from a range of arguments
1283 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1284 const Twine &NameStr, Instruction *InsertBefore);
1286 /// Construct a CallInst given a range of arguments.
1287 /// \brief Construct a CallInst from a range of arguments
1288 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1289 const Twine &NameStr, BasicBlock *InsertAtEnd);
1291 explicit CallInst(Value *F, const Twine &NameStr,
1292 Instruction *InsertBefore);
1293 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1295 CallInst *clone_impl() const override;
1297 static CallInst *Create(Value *Func,
1298 ArrayRef<Value *> Args,
1299 const Twine &NameStr = "",
1300 Instruction *InsertBefore = nullptr) {
1301 return new(unsigned(Args.size() + 1))
1302 CallInst(Func, Args, NameStr, InsertBefore);
1304 static CallInst *Create(Value *Func,
1305 ArrayRef<Value *> Args,
1306 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1307 return new(unsigned(Args.size() + 1))
1308 CallInst(Func, Args, NameStr, InsertAtEnd);
1310 static CallInst *Create(Value *F, const Twine &NameStr = "",
1311 Instruction *InsertBefore = nullptr) {
1312 return new(1) CallInst(F, NameStr, InsertBefore);
1314 static CallInst *Create(Value *F, const Twine &NameStr,
1315 BasicBlock *InsertAtEnd) {
1316 return new(1) CallInst(F, NameStr, InsertAtEnd);
1318 /// CreateMalloc - Generate the IR for a call to malloc:
1319 /// 1. Compute the malloc call's argument as the specified type's size,
1320 /// possibly multiplied by the array size if the array size is not
1322 /// 2. Call malloc with that argument.
1323 /// 3. Bitcast the result of the malloc call to the specified type.
1324 static Instruction *CreateMalloc(Instruction *InsertBefore,
1325 Type *IntPtrTy, Type *AllocTy,
1326 Value *AllocSize, Value *ArraySize = nullptr,
1327 Function* MallocF = nullptr,
1328 const Twine &Name = "");
1329 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1330 Type *IntPtrTy, Type *AllocTy,
1331 Value *AllocSize, Value *ArraySize = nullptr,
1332 Function* MallocF = nullptr,
1333 const Twine &Name = "");
1334 /// CreateFree - Generate the IR for a call to the builtin free function.
1335 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1336 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1338 ~CallInst() override;
1340 // Note that 'musttail' implies 'tail'.
1341 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1342 TailCallKind getTailCallKind() const {
1343 return TailCallKind(getSubclassDataFromInstruction() & 3);
1345 bool isTailCall() const {
1346 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1348 bool isMustTailCall() const {
1349 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1351 void setTailCall(bool isTC = true) {
1352 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1353 unsigned(isTC ? TCK_Tail : TCK_None));
1355 void setTailCallKind(TailCallKind TCK) {
1356 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1360 /// Provide fast operand accessors
1361 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1363 /// getNumArgOperands - Return the number of call arguments.
1365 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1367 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1369 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1370 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1372 /// arg_operands - iteration adapter for range-for loops.
1373 iterator_range<op_iterator> arg_operands() {
1374 // The last operand in the op list is the callee - it's not one of the args
1375 // so we don't want to iterate over it.
1376 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1379 /// arg_operands - iteration adapter for range-for loops.
1380 iterator_range<const_op_iterator> arg_operands() const {
1381 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1384 /// \brief Wrappers for getting the \c Use of a call argument.
1385 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1386 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1388 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1390 CallingConv::ID getCallingConv() const {
1391 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1393 void setCallingConv(CallingConv::ID CC) {
1394 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1395 (static_cast<unsigned>(CC) << 2));
1398 /// getAttributes - Return the parameter attributes for this call.
1400 const AttributeSet &getAttributes() const { return AttributeList; }
1402 /// setAttributes - Set the parameter attributes for this call.
1404 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1406 /// addAttribute - adds the attribute to the list of attributes.
1407 void addAttribute(unsigned i, Attribute::AttrKind attr);
1409 /// removeAttribute - removes the attribute from the list of attributes.
1410 void removeAttribute(unsigned i, Attribute attr);
1412 /// \brief adds the dereferenceable attribute to the list of attributes.
1413 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1415 /// \brief Determine whether this call has the given attribute.
1416 bool hasFnAttr(Attribute::AttrKind A) const {
1417 assert(A != Attribute::NoBuiltin &&
1418 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1419 return hasFnAttrImpl(A);
1422 /// \brief Determine whether the call or the callee has the given attributes.
1423 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1425 /// \brief Extract the alignment for a call or parameter (0=unknown).
1426 unsigned getParamAlignment(unsigned i) const {
1427 return AttributeList.getParamAlignment(i);
1430 /// \brief Extract the number of dereferenceable bytes for a call or
1431 /// parameter (0=unknown).
1432 uint64_t getDereferenceableBytes(unsigned i) const {
1433 return AttributeList.getDereferenceableBytes(i);
1436 /// \brief Return true if the call should not be treated as a call to a
1438 bool isNoBuiltin() const {
1439 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1440 !hasFnAttrImpl(Attribute::Builtin);
1443 /// \brief Return true if the call should not be inlined.
1444 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1445 void setIsNoInline() {
1446 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1449 /// \brief Return true if the call can return twice
1450 bool canReturnTwice() const {
1451 return hasFnAttr(Attribute::ReturnsTwice);
1453 void setCanReturnTwice() {
1454 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1457 /// \brief Determine if the call does not access memory.
1458 bool doesNotAccessMemory() const {
1459 return hasFnAttr(Attribute::ReadNone);
1461 void setDoesNotAccessMemory() {
1462 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1465 /// \brief Determine if the call does not access or only reads memory.
1466 bool onlyReadsMemory() const {
1467 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1469 void setOnlyReadsMemory() {
1470 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1473 /// \brief Determine if the call cannot return.
1474 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1475 void setDoesNotReturn() {
1476 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1479 /// \brief Determine if the call cannot unwind.
1480 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1481 void setDoesNotThrow() {
1482 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1485 /// \brief Determine if the call cannot be duplicated.
1486 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1487 void setCannotDuplicate() {
1488 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1491 /// \brief Determine if the call returns a structure through first
1492 /// pointer argument.
1493 bool hasStructRetAttr() const {
1494 // Be friendly and also check the callee.
1495 return paramHasAttr(1, Attribute::StructRet);
1498 /// \brief Determine if any call argument is an aggregate passed by value.
1499 bool hasByValArgument() const {
1500 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1503 /// getCalledFunction - Return the function called, or null if this is an
1504 /// indirect function invocation.
1506 Function *getCalledFunction() const {
1507 return dyn_cast<Function>(Op<-1>());
1510 /// getCalledValue - Get a pointer to the function that is invoked by this
1512 const Value *getCalledValue() const { return Op<-1>(); }
1513 Value *getCalledValue() { return Op<-1>(); }
1515 /// setCalledFunction - Set the function called.
1516 void setCalledFunction(Value* Fn) {
1520 /// isInlineAsm - Check if this call is an inline asm statement.
1521 bool isInlineAsm() const {
1522 return isa<InlineAsm>(Op<-1>());
1525 // Methods for support type inquiry through isa, cast, and dyn_cast:
1526 static inline bool classof(const Instruction *I) {
1527 return I->getOpcode() == Instruction::Call;
1529 static inline bool classof(const Value *V) {
1530 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1534 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1536 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1537 // method so that subclasses cannot accidentally use it.
1538 void setInstructionSubclassData(unsigned short D) {
1539 Instruction::setInstructionSubclassData(D);
1544 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1547 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1548 const Twine &NameStr, BasicBlock *InsertAtEnd)
1549 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1550 ->getElementType())->getReturnType(),
1552 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1553 unsigned(Args.size() + 1), InsertAtEnd) {
1554 init(Func, Args, NameStr);
1557 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1558 const Twine &NameStr, Instruction *InsertBefore)
1559 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1560 ->getElementType())->getReturnType(),
1562 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1563 unsigned(Args.size() + 1), InsertBefore) {
1564 init(Func, Args, NameStr);
1568 // Note: if you get compile errors about private methods then
1569 // please update your code to use the high-level operand
1570 // interfaces. See line 943 above.
1571 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1573 //===----------------------------------------------------------------------===//
1575 //===----------------------------------------------------------------------===//
1577 /// SelectInst - This class represents the LLVM 'select' instruction.
1579 class SelectInst : public Instruction {
1580 void init(Value *C, Value *S1, Value *S2) {
1581 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1587 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1588 Instruction *InsertBefore)
1589 : Instruction(S1->getType(), Instruction::Select,
1590 &Op<0>(), 3, InsertBefore) {
1594 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1595 BasicBlock *InsertAtEnd)
1596 : Instruction(S1->getType(), Instruction::Select,
1597 &Op<0>(), 3, InsertAtEnd) {
1602 SelectInst *clone_impl() const override;
1604 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1605 const Twine &NameStr = "",
1606 Instruction *InsertBefore = nullptr) {
1607 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1609 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1610 const Twine &NameStr,
1611 BasicBlock *InsertAtEnd) {
1612 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1615 const Value *getCondition() const { return Op<0>(); }
1616 const Value *getTrueValue() const { return Op<1>(); }
1617 const Value *getFalseValue() const { return Op<2>(); }
1618 Value *getCondition() { return Op<0>(); }
1619 Value *getTrueValue() { return Op<1>(); }
1620 Value *getFalseValue() { return Op<2>(); }
1622 /// areInvalidOperands - Return a string if the specified operands are invalid
1623 /// for a select operation, otherwise return null.
1624 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1626 /// Transparently provide more efficient getOperand methods.
1627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1629 OtherOps getOpcode() const {
1630 return static_cast<OtherOps>(Instruction::getOpcode());
1633 // Methods for support type inquiry through isa, cast, and dyn_cast:
1634 static inline bool classof(const Instruction *I) {
1635 return I->getOpcode() == Instruction::Select;
1637 static inline bool classof(const Value *V) {
1638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1643 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1646 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1648 //===----------------------------------------------------------------------===//
1650 //===----------------------------------------------------------------------===//
1652 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1653 /// an argument of the specified type given a va_list and increments that list
1655 class VAArgInst : public UnaryInstruction {
1657 VAArgInst *clone_impl() const override;
1660 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1661 Instruction *InsertBefore = nullptr)
1662 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1665 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1666 BasicBlock *InsertAtEnd)
1667 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1671 Value *getPointerOperand() { return getOperand(0); }
1672 const Value *getPointerOperand() const { return getOperand(0); }
1673 static unsigned getPointerOperandIndex() { return 0U; }
1675 // Methods for support type inquiry through isa, cast, and dyn_cast:
1676 static inline bool classof(const Instruction *I) {
1677 return I->getOpcode() == VAArg;
1679 static inline bool classof(const Value *V) {
1680 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1684 //===----------------------------------------------------------------------===//
1685 // ExtractElementInst Class
1686 //===----------------------------------------------------------------------===//
1688 /// ExtractElementInst - This instruction extracts a single (scalar)
1689 /// element from a VectorType value
1691 class ExtractElementInst : public Instruction {
1692 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1693 Instruction *InsertBefore = nullptr);
1694 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1695 BasicBlock *InsertAtEnd);
1697 ExtractElementInst *clone_impl() const override;
1700 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1701 const Twine &NameStr = "",
1702 Instruction *InsertBefore = nullptr) {
1703 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1705 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1706 const Twine &NameStr,
1707 BasicBlock *InsertAtEnd) {
1708 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1711 /// isValidOperands - Return true if an extractelement instruction can be
1712 /// formed with the specified operands.
1713 static bool isValidOperands(const Value *Vec, const Value *Idx);
1715 Value *getVectorOperand() { return Op<0>(); }
1716 Value *getIndexOperand() { return Op<1>(); }
1717 const Value *getVectorOperand() const { return Op<0>(); }
1718 const Value *getIndexOperand() const { return Op<1>(); }
1720 VectorType *getVectorOperandType() const {
1721 return cast<VectorType>(getVectorOperand()->getType());
1725 /// Transparently provide more efficient getOperand methods.
1726 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1728 // Methods for support type inquiry through isa, cast, and dyn_cast:
1729 static inline bool classof(const Instruction *I) {
1730 return I->getOpcode() == Instruction::ExtractElement;
1732 static inline bool classof(const Value *V) {
1733 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1738 struct OperandTraits<ExtractElementInst> :
1739 public FixedNumOperandTraits<ExtractElementInst, 2> {
1742 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1744 //===----------------------------------------------------------------------===//
1745 // InsertElementInst Class
1746 //===----------------------------------------------------------------------===//
1748 /// InsertElementInst - This instruction inserts a single (scalar)
1749 /// element into a VectorType value
1751 class InsertElementInst : public Instruction {
1752 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1753 const Twine &NameStr = "",
1754 Instruction *InsertBefore = nullptr);
1755 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1756 const Twine &NameStr, BasicBlock *InsertAtEnd);
1758 InsertElementInst *clone_impl() const override;
1761 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1762 const Twine &NameStr = "",
1763 Instruction *InsertBefore = nullptr) {
1764 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1766 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1767 const Twine &NameStr,
1768 BasicBlock *InsertAtEnd) {
1769 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1772 /// isValidOperands - Return true if an insertelement instruction can be
1773 /// formed with the specified operands.
1774 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1777 /// getType - Overload to return most specific vector type.
1779 VectorType *getType() const {
1780 return cast<VectorType>(Instruction::getType());
1783 /// Transparently provide more efficient getOperand methods.
1784 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1786 // Methods for support type inquiry through isa, cast, and dyn_cast:
1787 static inline bool classof(const Instruction *I) {
1788 return I->getOpcode() == Instruction::InsertElement;
1790 static inline bool classof(const Value *V) {
1791 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1796 struct OperandTraits<InsertElementInst> :
1797 public FixedNumOperandTraits<InsertElementInst, 3> {
1800 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1802 //===----------------------------------------------------------------------===//
1803 // ShuffleVectorInst Class
1804 //===----------------------------------------------------------------------===//
1806 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1809 class ShuffleVectorInst : public Instruction {
1811 ShuffleVectorInst *clone_impl() const override;
1814 // allocate space for exactly three operands
1815 void *operator new(size_t s) {
1816 return User::operator new(s, 3);
1818 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1819 const Twine &NameStr = "",
1820 Instruction *InsertBefor = nullptr);
1821 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1822 const Twine &NameStr, BasicBlock *InsertAtEnd);
1824 /// isValidOperands - Return true if a shufflevector instruction can be
1825 /// formed with the specified operands.
1826 static bool isValidOperands(const Value *V1, const Value *V2,
1829 /// getType - Overload to return most specific vector type.
1831 VectorType *getType() const {
1832 return cast<VectorType>(Instruction::getType());
1835 /// Transparently provide more efficient getOperand methods.
1836 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1838 Constant *getMask() const {
1839 return cast<Constant>(getOperand(2));
1842 /// getMaskValue - Return the index from the shuffle mask for the specified
1843 /// output result. This is either -1 if the element is undef or a number less
1844 /// than 2*numelements.
1845 static int getMaskValue(Constant *Mask, unsigned i);
1847 int getMaskValue(unsigned i) const {
1848 return getMaskValue(getMask(), i);
1851 /// getShuffleMask - Return the full mask for this instruction, where each
1852 /// element is the element number and undef's are returned as -1.
1853 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1855 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1856 return getShuffleMask(getMask(), Result);
1859 SmallVector<int, 16> getShuffleMask() const {
1860 SmallVector<int, 16> Mask;
1861 getShuffleMask(Mask);
1866 // Methods for support type inquiry through isa, cast, and dyn_cast:
1867 static inline bool classof(const Instruction *I) {
1868 return I->getOpcode() == Instruction::ShuffleVector;
1870 static inline bool classof(const Value *V) {
1871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1876 struct OperandTraits<ShuffleVectorInst> :
1877 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1880 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1882 //===----------------------------------------------------------------------===//
1883 // ExtractValueInst Class
1884 //===----------------------------------------------------------------------===//
1886 /// ExtractValueInst - This instruction extracts a struct member or array
1887 /// element value from an aggregate value.
1889 class ExtractValueInst : public UnaryInstruction {
1890 SmallVector<unsigned, 4> Indices;
1892 ExtractValueInst(const ExtractValueInst &EVI);
1893 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1895 /// Constructors - Create a extractvalue instruction with a base aggregate
1896 /// value and a list of indices. The first ctor can optionally insert before
1897 /// an existing instruction, the second appends the new instruction to the
1898 /// specified BasicBlock.
1899 inline ExtractValueInst(Value *Agg,
1900 ArrayRef<unsigned> Idxs,
1901 const Twine &NameStr,
1902 Instruction *InsertBefore);
1903 inline ExtractValueInst(Value *Agg,
1904 ArrayRef<unsigned> Idxs,
1905 const Twine &NameStr, BasicBlock *InsertAtEnd);
1907 // allocate space for exactly one operand
1908 void *operator new(size_t s) {
1909 return User::operator new(s, 1);
1912 ExtractValueInst *clone_impl() const override;
1915 static ExtractValueInst *Create(Value *Agg,
1916 ArrayRef<unsigned> Idxs,
1917 const Twine &NameStr = "",
1918 Instruction *InsertBefore = nullptr) {
1920 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1922 static ExtractValueInst *Create(Value *Agg,
1923 ArrayRef<unsigned> Idxs,
1924 const Twine &NameStr,
1925 BasicBlock *InsertAtEnd) {
1926 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1929 /// getIndexedType - Returns the type of the element that would be extracted
1930 /// with an extractvalue instruction with the specified parameters.
1932 /// Null is returned if the indices are invalid for the specified type.
1933 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1935 typedef const unsigned* idx_iterator;
1936 inline idx_iterator idx_begin() const { return Indices.begin(); }
1937 inline idx_iterator idx_end() const { return Indices.end(); }
1938 inline iterator_range<idx_iterator> indices() const {
1939 return iterator_range<idx_iterator>(idx_begin(), idx_end());
1942 Value *getAggregateOperand() {
1943 return getOperand(0);
1945 const Value *getAggregateOperand() const {
1946 return getOperand(0);
1948 static unsigned getAggregateOperandIndex() {
1949 return 0U; // get index for modifying correct operand
1952 ArrayRef<unsigned> getIndices() const {
1956 unsigned getNumIndices() const {
1957 return (unsigned)Indices.size();
1960 bool hasIndices() const {
1964 // Methods for support type inquiry through isa, cast, and dyn_cast:
1965 static inline bool classof(const Instruction *I) {
1966 return I->getOpcode() == Instruction::ExtractValue;
1968 static inline bool classof(const Value *V) {
1969 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1973 ExtractValueInst::ExtractValueInst(Value *Agg,
1974 ArrayRef<unsigned> Idxs,
1975 const Twine &NameStr,
1976 Instruction *InsertBefore)
1977 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1978 ExtractValue, Agg, InsertBefore) {
1979 init(Idxs, NameStr);
1981 ExtractValueInst::ExtractValueInst(Value *Agg,
1982 ArrayRef<unsigned> Idxs,
1983 const Twine &NameStr,
1984 BasicBlock *InsertAtEnd)
1985 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1986 ExtractValue, Agg, InsertAtEnd) {
1987 init(Idxs, NameStr);
1991 //===----------------------------------------------------------------------===//
1992 // InsertValueInst Class
1993 //===----------------------------------------------------------------------===//
1995 /// InsertValueInst - This instruction inserts a struct field of array element
1996 /// value into an aggregate value.
1998 class InsertValueInst : public Instruction {
1999 SmallVector<unsigned, 4> Indices;
2001 void *operator new(size_t, unsigned) = delete;
2002 InsertValueInst(const InsertValueInst &IVI);
2003 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2004 const Twine &NameStr);
2006 /// Constructors - Create a insertvalue instruction with a base aggregate
2007 /// value, a value to insert, and a list of indices. The first ctor can
2008 /// optionally insert before an existing instruction, the second appends
2009 /// the new instruction to the specified BasicBlock.
2010 inline InsertValueInst(Value *Agg, Value *Val,
2011 ArrayRef<unsigned> Idxs,
2012 const Twine &NameStr,
2013 Instruction *InsertBefore);
2014 inline InsertValueInst(Value *Agg, Value *Val,
2015 ArrayRef<unsigned> Idxs,
2016 const Twine &NameStr, BasicBlock *InsertAtEnd);
2018 /// Constructors - These two constructors are convenience methods because one
2019 /// and two index insertvalue instructions are so common.
2020 InsertValueInst(Value *Agg, Value *Val,
2021 unsigned Idx, const Twine &NameStr = "",
2022 Instruction *InsertBefore = nullptr);
2023 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2024 const Twine &NameStr, BasicBlock *InsertAtEnd);
2026 InsertValueInst *clone_impl() const override;
2028 // allocate space for exactly two operands
2029 void *operator new(size_t s) {
2030 return User::operator new(s, 2);
2033 static InsertValueInst *Create(Value *Agg, Value *Val,
2034 ArrayRef<unsigned> Idxs,
2035 const Twine &NameStr = "",
2036 Instruction *InsertBefore = nullptr) {
2037 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2039 static InsertValueInst *Create(Value *Agg, Value *Val,
2040 ArrayRef<unsigned> Idxs,
2041 const Twine &NameStr,
2042 BasicBlock *InsertAtEnd) {
2043 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2046 /// Transparently provide more efficient getOperand methods.
2047 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2049 typedef const unsigned* idx_iterator;
2050 inline idx_iterator idx_begin() const { return Indices.begin(); }
2051 inline idx_iterator idx_end() const { return Indices.end(); }
2052 inline iterator_range<idx_iterator> indices() const {
2053 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2056 Value *getAggregateOperand() {
2057 return getOperand(0);
2059 const Value *getAggregateOperand() const {
2060 return getOperand(0);
2062 static unsigned getAggregateOperandIndex() {
2063 return 0U; // get index for modifying correct operand
2066 Value *getInsertedValueOperand() {
2067 return getOperand(1);
2069 const Value *getInsertedValueOperand() const {
2070 return getOperand(1);
2072 static unsigned getInsertedValueOperandIndex() {
2073 return 1U; // get index for modifying correct operand
2076 ArrayRef<unsigned> getIndices() const {
2080 unsigned getNumIndices() const {
2081 return (unsigned)Indices.size();
2084 bool hasIndices() const {
2088 // Methods for support type inquiry through isa, cast, and dyn_cast:
2089 static inline bool classof(const Instruction *I) {
2090 return I->getOpcode() == Instruction::InsertValue;
2092 static inline bool classof(const Value *V) {
2093 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2098 struct OperandTraits<InsertValueInst> :
2099 public FixedNumOperandTraits<InsertValueInst, 2> {
2102 InsertValueInst::InsertValueInst(Value *Agg,
2104 ArrayRef<unsigned> Idxs,
2105 const Twine &NameStr,
2106 Instruction *InsertBefore)
2107 : Instruction(Agg->getType(), InsertValue,
2108 OperandTraits<InsertValueInst>::op_begin(this),
2110 init(Agg, Val, Idxs, NameStr);
2112 InsertValueInst::InsertValueInst(Value *Agg,
2114 ArrayRef<unsigned> Idxs,
2115 const Twine &NameStr,
2116 BasicBlock *InsertAtEnd)
2117 : Instruction(Agg->getType(), InsertValue,
2118 OperandTraits<InsertValueInst>::op_begin(this),
2120 init(Agg, Val, Idxs, NameStr);
2123 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2125 //===----------------------------------------------------------------------===//
2127 //===----------------------------------------------------------------------===//
2129 // PHINode - The PHINode class is used to represent the magical mystical PHI
2130 // node, that can not exist in nature, but can be synthesized in a computer
2131 // scientist's overactive imagination.
2133 class PHINode : public Instruction {
2134 void *operator new(size_t, unsigned) = delete;
2135 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2136 /// the number actually in use.
2137 unsigned ReservedSpace;
2138 PHINode(const PHINode &PN);
2139 // allocate space for exactly zero operands
2140 void *operator new(size_t s) {
2141 return User::operator new(s, 0);
2143 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2144 const Twine &NameStr = "",
2145 Instruction *InsertBefore = nullptr)
2146 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2147 ReservedSpace(NumReservedValues) {
2149 OperandList = allocHungoffUses(ReservedSpace);
2152 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2153 BasicBlock *InsertAtEnd)
2154 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2155 ReservedSpace(NumReservedValues) {
2157 OperandList = allocHungoffUses(ReservedSpace);
2160 // allocHungoffUses - this is more complicated than the generic
2161 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2162 // values and pointers to the incoming blocks, all in one allocation.
2163 Use *allocHungoffUses(unsigned) const;
2165 PHINode *clone_impl() const override;
2167 /// Constructors - NumReservedValues is a hint for the number of incoming
2168 /// edges that this phi node will have (use 0 if you really have no idea).
2169 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2170 const Twine &NameStr = "",
2171 Instruction *InsertBefore = nullptr) {
2172 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2174 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2175 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2176 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2178 ~PHINode() override;
2180 /// Provide fast operand accessors
2181 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2183 // Block iterator interface. This provides access to the list of incoming
2184 // basic blocks, which parallels the list of incoming values.
2186 typedef BasicBlock **block_iterator;
2187 typedef BasicBlock * const *const_block_iterator;
2189 block_iterator block_begin() {
2191 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2192 return reinterpret_cast<block_iterator>(ref + 1);
2195 const_block_iterator block_begin() const {
2196 const Use::UserRef *ref =
2197 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2198 return reinterpret_cast<const_block_iterator>(ref + 1);
2201 block_iterator block_end() {
2202 return block_begin() + getNumOperands();
2205 const_block_iterator block_end() const {
2206 return block_begin() + getNumOperands();
2209 op_range incoming_values() { return operands(); }
2211 /// getNumIncomingValues - Return the number of incoming edges
2213 unsigned getNumIncomingValues() const { return getNumOperands(); }
2215 /// getIncomingValue - Return incoming value number x
2217 Value *getIncomingValue(unsigned i) const {
2218 return getOperand(i);
2220 void setIncomingValue(unsigned i, Value *V) {
2223 static unsigned getOperandNumForIncomingValue(unsigned i) {
2226 static unsigned getIncomingValueNumForOperand(unsigned i) {
2230 /// getIncomingBlock - Return incoming basic block number @p i.
2232 BasicBlock *getIncomingBlock(unsigned i) const {
2233 return block_begin()[i];
2236 /// getIncomingBlock - Return incoming basic block corresponding
2237 /// to an operand of the PHI.
2239 BasicBlock *getIncomingBlock(const Use &U) const {
2240 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2241 return getIncomingBlock(unsigned(&U - op_begin()));
2244 /// getIncomingBlock - Return incoming basic block corresponding
2245 /// to value use iterator.
2247 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2248 return getIncomingBlock(I.getUse());
2251 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2252 block_begin()[i] = BB;
2255 /// addIncoming - Add an incoming value to the end of the PHI list
2257 void addIncoming(Value *V, BasicBlock *BB) {
2258 assert(V && "PHI node got a null value!");
2259 assert(BB && "PHI node got a null basic block!");
2260 assert(getType() == V->getType() &&
2261 "All operands to PHI node must be the same type as the PHI node!");
2262 if (NumOperands == ReservedSpace)
2263 growOperands(); // Get more space!
2264 // Initialize some new operands.
2266 setIncomingValue(NumOperands - 1, V);
2267 setIncomingBlock(NumOperands - 1, BB);
2270 /// removeIncomingValue - Remove an incoming value. This is useful if a
2271 /// predecessor basic block is deleted. The value removed is returned.
2273 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2274 /// is true), the PHI node is destroyed and any uses of it are replaced with
2275 /// dummy values. The only time there should be zero incoming values to a PHI
2276 /// node is when the block is dead, so this strategy is sound.
2278 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2280 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2281 int Idx = getBasicBlockIndex(BB);
2282 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2283 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2286 /// getBasicBlockIndex - Return the first index of the specified basic
2287 /// block in the value list for this PHI. Returns -1 if no instance.
2289 int getBasicBlockIndex(const BasicBlock *BB) const {
2290 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2291 if (block_begin()[i] == BB)
2296 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2297 int Idx = getBasicBlockIndex(BB);
2298 assert(Idx >= 0 && "Invalid basic block argument!");
2299 return getIncomingValue(Idx);
2302 /// hasConstantValue - If the specified PHI node always merges together the
2303 /// same value, return the value, otherwise return null.
2304 Value *hasConstantValue() const;
2306 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2307 static inline bool classof(const Instruction *I) {
2308 return I->getOpcode() == Instruction::PHI;
2310 static inline bool classof(const Value *V) {
2311 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2314 void growOperands();
2318 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2321 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2323 //===----------------------------------------------------------------------===//
2324 // LandingPadInst Class
2325 //===----------------------------------------------------------------------===//
2327 //===---------------------------------------------------------------------------
2328 /// LandingPadInst - The landingpad instruction holds all of the information
2329 /// necessary to generate correct exception handling. The landingpad instruction
2330 /// cannot be moved from the top of a landing pad block, which itself is
2331 /// accessible only from the 'unwind' edge of an invoke. This uses the
2332 /// SubclassData field in Value to store whether or not the landingpad is a
2335 class LandingPadInst : public Instruction {
2336 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2337 /// the number actually in use.
2338 unsigned ReservedSpace;
2339 LandingPadInst(const LandingPadInst &LP);
2341 enum ClauseType { Catch, Filter };
2343 void *operator new(size_t, unsigned) = delete;
2344 // Allocate space for exactly zero operands.
2345 void *operator new(size_t s) {
2346 return User::operator new(s, 0);
2348 void growOperands(unsigned Size);
2349 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2351 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2352 unsigned NumReservedValues, const Twine &NameStr,
2353 Instruction *InsertBefore);
2354 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2355 unsigned NumReservedValues, const Twine &NameStr,
2356 BasicBlock *InsertAtEnd);
2358 LandingPadInst *clone_impl() const override;
2360 /// Constructors - NumReservedClauses is a hint for the number of incoming
2361 /// clauses that this landingpad will have (use 0 if you really have no idea).
2362 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2363 unsigned NumReservedClauses,
2364 const Twine &NameStr = "",
2365 Instruction *InsertBefore = nullptr);
2366 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2367 unsigned NumReservedClauses,
2368 const Twine &NameStr, BasicBlock *InsertAtEnd);
2369 ~LandingPadInst() override;
2371 /// Provide fast operand accessors
2372 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2374 /// getPersonalityFn - Get the personality function associated with this
2376 Value *getPersonalityFn() const { return getOperand(0); }
2378 /// isCleanup - Return 'true' if this landingpad instruction is a
2379 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2380 /// doesn't catch the exception.
2381 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2383 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2384 void setCleanup(bool V) {
2385 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2389 /// Add a catch or filter clause to the landing pad.
2390 void addClause(Constant *ClauseVal);
2392 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2393 /// determine what type of clause this is.
2394 Constant *getClause(unsigned Idx) const {
2395 return cast<Constant>(OperandList[Idx + 1]);
2398 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2399 bool isCatch(unsigned Idx) const {
2400 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2403 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2404 bool isFilter(unsigned Idx) const {
2405 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2408 /// getNumClauses - Get the number of clauses for this landing pad.
2409 unsigned getNumClauses() const { return getNumOperands() - 1; }
2411 /// reserveClauses - Grow the size of the operand list to accommodate the new
2412 /// number of clauses.
2413 void reserveClauses(unsigned Size) { growOperands(Size); }
2415 // Methods for support type inquiry through isa, cast, and dyn_cast:
2416 static inline bool classof(const Instruction *I) {
2417 return I->getOpcode() == Instruction::LandingPad;
2419 static inline bool classof(const Value *V) {
2420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2425 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2428 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2430 //===----------------------------------------------------------------------===//
2432 //===----------------------------------------------------------------------===//
2434 //===---------------------------------------------------------------------------
2435 /// ReturnInst - Return a value (possibly void), from a function. Execution
2436 /// does not continue in this function any longer.
2438 class ReturnInst : public TerminatorInst {
2439 ReturnInst(const ReturnInst &RI);
2442 // ReturnInst constructors:
2443 // ReturnInst() - 'ret void' instruction
2444 // ReturnInst( null) - 'ret void' instruction
2445 // ReturnInst(Value* X) - 'ret X' instruction
2446 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2447 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2448 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2449 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2451 // NOTE: If the Value* passed is of type void then the constructor behaves as
2452 // if it was passed NULL.
2453 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2454 Instruction *InsertBefore = nullptr);
2455 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2456 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2458 ReturnInst *clone_impl() const override;
2460 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2461 Instruction *InsertBefore = nullptr) {
2462 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2464 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2465 BasicBlock *InsertAtEnd) {
2466 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2468 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2469 return new(0) ReturnInst(C, InsertAtEnd);
2471 ~ReturnInst() override;
2473 /// Provide fast operand accessors
2474 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2476 /// Convenience accessor. Returns null if there is no return value.
2477 Value *getReturnValue() const {
2478 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2481 unsigned getNumSuccessors() const { return 0; }
2483 // Methods for support type inquiry through isa, cast, and dyn_cast:
2484 static inline bool classof(const Instruction *I) {
2485 return (I->getOpcode() == Instruction::Ret);
2487 static inline bool classof(const Value *V) {
2488 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2491 BasicBlock *getSuccessorV(unsigned idx) const override;
2492 unsigned getNumSuccessorsV() const override;
2493 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2497 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2500 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2502 //===----------------------------------------------------------------------===//
2504 //===----------------------------------------------------------------------===//
2506 //===---------------------------------------------------------------------------
2507 /// BranchInst - Conditional or Unconditional Branch instruction.
2509 class BranchInst : public TerminatorInst {
2510 /// Ops list - Branches are strange. The operands are ordered:
2511 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2512 /// they don't have to check for cond/uncond branchness. These are mostly
2513 /// accessed relative from op_end().
2514 BranchInst(const BranchInst &BI);
2516 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2517 // BranchInst(BB *B) - 'br B'
2518 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2519 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2520 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2521 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2522 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2523 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2524 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2525 Instruction *InsertBefore = nullptr);
2526 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2527 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2528 BasicBlock *InsertAtEnd);
2530 BranchInst *clone_impl() const override;
2532 static BranchInst *Create(BasicBlock *IfTrue,
2533 Instruction *InsertBefore = nullptr) {
2534 return new(1) BranchInst(IfTrue, InsertBefore);
2536 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2537 Value *Cond, Instruction *InsertBefore = nullptr) {
2538 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2540 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2541 return new(1) BranchInst(IfTrue, InsertAtEnd);
2543 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2544 Value *Cond, BasicBlock *InsertAtEnd) {
2545 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2548 /// Transparently provide more efficient getOperand methods.
2549 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2551 bool isUnconditional() const { return getNumOperands() == 1; }
2552 bool isConditional() const { return getNumOperands() == 3; }
2554 Value *getCondition() const {
2555 assert(isConditional() && "Cannot get condition of an uncond branch!");
2559 void setCondition(Value *V) {
2560 assert(isConditional() && "Cannot set condition of unconditional branch!");
2564 unsigned getNumSuccessors() const { return 1+isConditional(); }
2566 BasicBlock *getSuccessor(unsigned i) const {
2567 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2568 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2571 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2572 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2573 *(&Op<-1>() - idx) = (Value*)NewSucc;
2576 /// \brief Swap the successors of this branch instruction.
2578 /// Swaps the successors of the branch instruction. This also swaps any
2579 /// branch weight metadata associated with the instruction so that it
2580 /// continues to map correctly to each operand.
2581 void swapSuccessors();
2583 // Methods for support type inquiry through isa, cast, and dyn_cast:
2584 static inline bool classof(const Instruction *I) {
2585 return (I->getOpcode() == Instruction::Br);
2587 static inline bool classof(const Value *V) {
2588 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2591 BasicBlock *getSuccessorV(unsigned idx) const override;
2592 unsigned getNumSuccessorsV() const override;
2593 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2597 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2600 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2602 //===----------------------------------------------------------------------===//
2604 //===----------------------------------------------------------------------===//
2606 //===---------------------------------------------------------------------------
2607 /// SwitchInst - Multiway switch
2609 class SwitchInst : public TerminatorInst {
2610 void *operator new(size_t, unsigned) = delete;
2611 unsigned ReservedSpace;
2612 // Operand[0] = Value to switch on
2613 // Operand[1] = Default basic block destination
2614 // Operand[2n ] = Value to match
2615 // Operand[2n+1] = BasicBlock to go to on match
2616 SwitchInst(const SwitchInst &SI);
2617 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2618 void growOperands();
2619 // allocate space for exactly zero operands
2620 void *operator new(size_t s) {
2621 return User::operator new(s, 0);
2623 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2624 /// switch on and a default destination. The number of additional cases can
2625 /// be specified here to make memory allocation more efficient. This
2626 /// constructor can also autoinsert before another instruction.
2627 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2628 Instruction *InsertBefore);
2630 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2631 /// switch on and a default destination. The number of additional cases can
2632 /// be specified here to make memory allocation more efficient. This
2633 /// constructor also autoinserts at the end of the specified BasicBlock.
2634 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2635 BasicBlock *InsertAtEnd);
2637 SwitchInst *clone_impl() const override;
2641 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2643 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2644 class CaseIteratorT {
2652 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2654 /// Initializes case iterator for given SwitchInst and for given
2656 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2661 /// Initializes case iterator for given SwitchInst and for given
2662 /// TerminatorInst's successor index.
2663 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2664 assert(SuccessorIndex < SI->getNumSuccessors() &&
2665 "Successor index # out of range!");
2666 return SuccessorIndex != 0 ?
2667 Self(SI, SuccessorIndex - 1) :
2668 Self(SI, DefaultPseudoIndex);
2671 /// Resolves case value for current case.
2672 ConstantIntTy *getCaseValue() {
2673 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2674 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2677 /// Resolves successor for current case.
2678 BasicBlockTy *getCaseSuccessor() {
2679 assert((Index < SI->getNumCases() ||
2680 Index == DefaultPseudoIndex) &&
2681 "Index out the number of cases.");
2682 return SI->getSuccessor(getSuccessorIndex());
2685 /// Returns number of current case.
2686 unsigned getCaseIndex() const { return Index; }
2688 /// Returns TerminatorInst's successor index for current case successor.
2689 unsigned getSuccessorIndex() const {
2690 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2691 "Index out the number of cases.");
2692 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2696 // Check index correctness after increment.
2697 // Note: Index == getNumCases() means end().
2698 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2702 Self operator++(int) {
2708 // Check index correctness after decrement.
2709 // Note: Index == getNumCases() means end().
2710 // Also allow "-1" iterator here. That will became valid after ++.
2711 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2712 "Index out the number of cases.");
2716 Self operator--(int) {
2721 bool operator==(const Self& RHS) const {
2722 assert(RHS.SI == SI && "Incompatible operators.");
2723 return RHS.Index == Index;
2725 bool operator!=(const Self& RHS) const {
2726 assert(RHS.SI == SI && "Incompatible operators.");
2727 return RHS.Index != Index;
2734 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2737 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2739 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2743 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2744 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2746 /// Sets the new value for current case.
2747 void setValue(ConstantInt *V) {
2748 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2749 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2752 /// Sets the new successor for current case.
2753 void setSuccessor(BasicBlock *S) {
2754 SI->setSuccessor(getSuccessorIndex(), S);
2758 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2760 Instruction *InsertBefore = nullptr) {
2761 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2763 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2764 unsigned NumCases, BasicBlock *InsertAtEnd) {
2765 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2768 ~SwitchInst() override;
2770 /// Provide fast operand accessors
2771 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2773 // Accessor Methods for Switch stmt
2774 Value *getCondition() const { return getOperand(0); }
2775 void setCondition(Value *V) { setOperand(0, V); }
2777 BasicBlock *getDefaultDest() const {
2778 return cast<BasicBlock>(getOperand(1));
2781 void setDefaultDest(BasicBlock *DefaultCase) {
2782 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2785 /// getNumCases - return the number of 'cases' in this switch instruction,
2786 /// except the default case
2787 unsigned getNumCases() const {
2788 return getNumOperands()/2 - 1;
2791 /// Returns a read/write iterator that points to the first
2792 /// case in SwitchInst.
2793 CaseIt case_begin() {
2794 return CaseIt(this, 0);
2796 /// Returns a read-only iterator that points to the first
2797 /// case in the SwitchInst.
2798 ConstCaseIt case_begin() const {
2799 return ConstCaseIt(this, 0);
2802 /// Returns a read/write iterator that points one past the last
2803 /// in the SwitchInst.
2805 return CaseIt(this, getNumCases());
2807 /// Returns a read-only iterator that points one past the last
2808 /// in the SwitchInst.
2809 ConstCaseIt case_end() const {
2810 return ConstCaseIt(this, getNumCases());
2813 /// cases - iteration adapter for range-for loops.
2814 iterator_range<CaseIt> cases() {
2815 return iterator_range<CaseIt>(case_begin(), case_end());
2818 /// cases - iteration adapter for range-for loops.
2819 iterator_range<ConstCaseIt> cases() const {
2820 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2823 /// Returns an iterator that points to the default case.
2824 /// Note: this iterator allows to resolve successor only. Attempt
2825 /// to resolve case value causes an assertion.
2826 /// Also note, that increment and decrement also causes an assertion and
2827 /// makes iterator invalid.
2828 CaseIt case_default() {
2829 return CaseIt(this, DefaultPseudoIndex);
2831 ConstCaseIt case_default() const {
2832 return ConstCaseIt(this, DefaultPseudoIndex);
2835 /// findCaseValue - Search all of the case values for the specified constant.
2836 /// If it is explicitly handled, return the case iterator of it, otherwise
2837 /// return default case iterator to indicate
2838 /// that it is handled by the default handler.
2839 CaseIt findCaseValue(const ConstantInt *C) {
2840 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2841 if (i.getCaseValue() == C)
2843 return case_default();
2845 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2846 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2847 if (i.getCaseValue() == C)
2849 return case_default();
2852 /// findCaseDest - Finds the unique case value for a given successor. Returns
2853 /// null if the successor is not found, not unique, or is the default case.
2854 ConstantInt *findCaseDest(BasicBlock *BB) {
2855 if (BB == getDefaultDest()) return nullptr;
2857 ConstantInt *CI = nullptr;
2858 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2859 if (i.getCaseSuccessor() == BB) {
2860 if (CI) return nullptr; // Multiple cases lead to BB.
2861 else CI = i.getCaseValue();
2867 /// addCase - Add an entry to the switch instruction...
2869 /// This action invalidates case_end(). Old case_end() iterator will
2870 /// point to the added case.
2871 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2873 /// removeCase - This method removes the specified case and its successor
2874 /// from the switch instruction. Note that this operation may reorder the
2875 /// remaining cases at index idx and above.
2877 /// This action invalidates iterators for all cases following the one removed,
2878 /// including the case_end() iterator.
2879 void removeCase(CaseIt i);
2881 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2882 BasicBlock *getSuccessor(unsigned idx) const {
2883 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2884 return cast<BasicBlock>(getOperand(idx*2+1));
2886 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2887 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2888 setOperand(idx*2+1, (Value*)NewSucc);
2891 // Methods for support type inquiry through isa, cast, and dyn_cast:
2892 static inline bool classof(const Instruction *I) {
2893 return I->getOpcode() == Instruction::Switch;
2895 static inline bool classof(const Value *V) {
2896 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2899 BasicBlock *getSuccessorV(unsigned idx) const override;
2900 unsigned getNumSuccessorsV() const override;
2901 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2905 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2908 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2911 //===----------------------------------------------------------------------===//
2912 // IndirectBrInst Class
2913 //===----------------------------------------------------------------------===//
2915 //===---------------------------------------------------------------------------
2916 /// IndirectBrInst - Indirect Branch Instruction.
2918 class IndirectBrInst : public TerminatorInst {
2919 void *operator new(size_t, unsigned) = delete;
2920 unsigned ReservedSpace;
2921 // Operand[0] = Value to switch on
2922 // Operand[1] = Default basic block destination
2923 // Operand[2n ] = Value to match
2924 // Operand[2n+1] = BasicBlock to go to on match
2925 IndirectBrInst(const IndirectBrInst &IBI);
2926 void init(Value *Address, unsigned NumDests);
2927 void growOperands();
2928 // allocate space for exactly zero operands
2929 void *operator new(size_t s) {
2930 return User::operator new(s, 0);
2932 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2933 /// Address to jump to. The number of expected destinations can be specified
2934 /// here to make memory allocation more efficient. This constructor can also
2935 /// autoinsert before another instruction.
2936 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2938 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2939 /// Address to jump to. The number of expected destinations can be specified
2940 /// here to make memory allocation more efficient. This constructor also
2941 /// autoinserts at the end of the specified BasicBlock.
2942 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2944 IndirectBrInst *clone_impl() const override;
2946 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2947 Instruction *InsertBefore = nullptr) {
2948 return new IndirectBrInst(Address, NumDests, InsertBefore);
2950 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2951 BasicBlock *InsertAtEnd) {
2952 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2954 ~IndirectBrInst() override;
2956 /// Provide fast operand accessors.
2957 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2959 // Accessor Methods for IndirectBrInst instruction.
2960 Value *getAddress() { return getOperand(0); }
2961 const Value *getAddress() const { return getOperand(0); }
2962 void setAddress(Value *V) { setOperand(0, V); }
2965 /// getNumDestinations - return the number of possible destinations in this
2966 /// indirectbr instruction.
2967 unsigned getNumDestinations() const { return getNumOperands()-1; }
2969 /// getDestination - Return the specified destination.
2970 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2971 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2973 /// addDestination - Add a destination.
2975 void addDestination(BasicBlock *Dest);
2977 /// removeDestination - This method removes the specified successor from the
2978 /// indirectbr instruction.
2979 void removeDestination(unsigned i);
2981 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2982 BasicBlock *getSuccessor(unsigned i) const {
2983 return cast<BasicBlock>(getOperand(i+1));
2985 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2986 setOperand(i+1, (Value*)NewSucc);
2989 // Methods for support type inquiry through isa, cast, and dyn_cast:
2990 static inline bool classof(const Instruction *I) {
2991 return I->getOpcode() == Instruction::IndirectBr;
2993 static inline bool classof(const Value *V) {
2994 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2997 BasicBlock *getSuccessorV(unsigned idx) const override;
2998 unsigned getNumSuccessorsV() const override;
2999 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3003 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3006 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3009 //===----------------------------------------------------------------------===//
3011 //===----------------------------------------------------------------------===//
3013 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3014 /// calling convention of the call.
3016 class InvokeInst : public TerminatorInst {
3017 AttributeSet AttributeList;
3018 InvokeInst(const InvokeInst &BI);
3019 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3020 ArrayRef<Value *> Args, const Twine &NameStr);
3022 /// Construct an InvokeInst given a range of arguments.
3024 /// \brief Construct an InvokeInst from a range of arguments
3025 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3026 ArrayRef<Value *> Args, unsigned Values,
3027 const Twine &NameStr, Instruction *InsertBefore);
3029 /// Construct an InvokeInst given a range of arguments.
3031 /// \brief Construct an InvokeInst from a range of arguments
3032 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3033 ArrayRef<Value *> Args, unsigned Values,
3034 const Twine &NameStr, BasicBlock *InsertAtEnd);
3036 InvokeInst *clone_impl() const override;
3038 static InvokeInst *Create(Value *Func,
3039 BasicBlock *IfNormal, BasicBlock *IfException,
3040 ArrayRef<Value *> Args, const Twine &NameStr = "",
3041 Instruction *InsertBefore = nullptr) {
3042 unsigned Values = unsigned(Args.size()) + 3;
3043 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3044 Values, NameStr, InsertBefore);
3046 static InvokeInst *Create(Value *Func,
3047 BasicBlock *IfNormal, BasicBlock *IfException,
3048 ArrayRef<Value *> Args, const Twine &NameStr,
3049 BasicBlock *InsertAtEnd) {
3050 unsigned Values = unsigned(Args.size()) + 3;
3051 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3052 Values, NameStr, InsertAtEnd);
3055 /// Provide fast operand accessors
3056 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3058 /// getNumArgOperands - Return the number of invoke arguments.
3060 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3062 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3064 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3065 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3067 /// arg_operands - iteration adapter for range-for loops.
3068 iterator_range<op_iterator> arg_operands() {
3069 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3072 /// arg_operands - iteration adapter for range-for loops.
3073 iterator_range<const_op_iterator> arg_operands() const {
3074 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3077 /// \brief Wrappers for getting the \c Use of a invoke argument.
3078 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3079 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3081 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3083 CallingConv::ID getCallingConv() const {
3084 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3086 void setCallingConv(CallingConv::ID CC) {
3087 setInstructionSubclassData(static_cast<unsigned>(CC));
3090 /// getAttributes - Return the parameter attributes for this invoke.
3092 const AttributeSet &getAttributes() const { return AttributeList; }
3094 /// setAttributes - Set the parameter attributes for this invoke.
3096 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3098 /// addAttribute - adds the attribute to the list of attributes.
3099 void addAttribute(unsigned i, Attribute::AttrKind attr);
3101 /// removeAttribute - removes the attribute from the list of attributes.
3102 void removeAttribute(unsigned i, Attribute attr);
3104 /// \brief adds the dereferenceable attribute to the list of attributes.
3105 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3107 /// \brief Determine whether this call has the given attribute.
3108 bool hasFnAttr(Attribute::AttrKind A) const {
3109 assert(A != Attribute::NoBuiltin &&
3110 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3111 return hasFnAttrImpl(A);
3114 /// \brief Determine whether the call or the callee has the given attributes.
3115 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3117 /// \brief Extract the alignment for a call or parameter (0=unknown).
3118 unsigned getParamAlignment(unsigned i) const {
3119 return AttributeList.getParamAlignment(i);
3122 /// \brief Extract the number of dereferenceable bytes for a call or
3123 /// parameter (0=unknown).
3124 uint64_t getDereferenceableBytes(unsigned i) const {
3125 return AttributeList.getDereferenceableBytes(i);
3128 /// \brief Return true if the call should not be treated as a call to a
3130 bool isNoBuiltin() const {
3131 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3132 // to check it by hand.
3133 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3134 !hasFnAttrImpl(Attribute::Builtin);
3137 /// \brief Return true if the call should not be inlined.
3138 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3139 void setIsNoInline() {
3140 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3143 /// \brief Determine if the call does not access memory.
3144 bool doesNotAccessMemory() const {
3145 return hasFnAttr(Attribute::ReadNone);
3147 void setDoesNotAccessMemory() {
3148 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3151 /// \brief Determine if the call does not access or only reads memory.
3152 bool onlyReadsMemory() const {
3153 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3155 void setOnlyReadsMemory() {
3156 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3159 /// \brief Determine if the call cannot return.
3160 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3161 void setDoesNotReturn() {
3162 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3165 /// \brief Determine if the call cannot unwind.
3166 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3167 void setDoesNotThrow() {
3168 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3171 /// \brief Determine if the invoke cannot be duplicated.
3172 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3173 void setCannotDuplicate() {
3174 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3177 /// \brief Determine if the call returns a structure through first
3178 /// pointer argument.
3179 bool hasStructRetAttr() const {
3180 // Be friendly and also check the callee.
3181 return paramHasAttr(1, Attribute::StructRet);
3184 /// \brief Determine if any call argument is an aggregate passed by value.
3185 bool hasByValArgument() const {
3186 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3189 /// getCalledFunction - Return the function called, or null if this is an
3190 /// indirect function invocation.
3192 Function *getCalledFunction() const {
3193 return dyn_cast<Function>(Op<-3>());
3196 /// getCalledValue - Get a pointer to the function that is invoked by this
3198 const Value *getCalledValue() const { return Op<-3>(); }
3199 Value *getCalledValue() { return Op<-3>(); }
3201 /// setCalledFunction - Set the function called.
3202 void setCalledFunction(Value* Fn) {
3206 // get*Dest - Return the destination basic blocks...
3207 BasicBlock *getNormalDest() const {
3208 return cast<BasicBlock>(Op<-2>());
3210 BasicBlock *getUnwindDest() const {
3211 return cast<BasicBlock>(Op<-1>());
3213 void setNormalDest(BasicBlock *B) {
3214 Op<-2>() = reinterpret_cast<Value*>(B);
3216 void setUnwindDest(BasicBlock *B) {
3217 Op<-1>() = reinterpret_cast<Value*>(B);
3220 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3221 /// block (the unwind destination).
3222 LandingPadInst *getLandingPadInst() const;
3224 BasicBlock *getSuccessor(unsigned i) const {
3225 assert(i < 2 && "Successor # out of range for invoke!");
3226 return i == 0 ? getNormalDest() : getUnwindDest();
3229 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3230 assert(idx < 2 && "Successor # out of range for invoke!");
3231 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3234 unsigned getNumSuccessors() const { return 2; }
3236 // Methods for support type inquiry through isa, cast, and dyn_cast:
3237 static inline bool classof(const Instruction *I) {
3238 return (I->getOpcode() == Instruction::Invoke);
3240 static inline bool classof(const Value *V) {
3241 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3245 BasicBlock *getSuccessorV(unsigned idx) const override;
3246 unsigned getNumSuccessorsV() const override;
3247 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3249 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3251 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3252 // method so that subclasses cannot accidentally use it.
3253 void setInstructionSubclassData(unsigned short D) {
3254 Instruction::setInstructionSubclassData(D);
3259 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3262 InvokeInst::InvokeInst(Value *Func,
3263 BasicBlock *IfNormal, BasicBlock *IfException,
3264 ArrayRef<Value *> Args, unsigned Values,
3265 const Twine &NameStr, Instruction *InsertBefore)
3266 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3267 ->getElementType())->getReturnType(),
3268 Instruction::Invoke,
3269 OperandTraits<InvokeInst>::op_end(this) - Values,
3270 Values, InsertBefore) {
3271 init(Func, IfNormal, IfException, Args, NameStr);
3273 InvokeInst::InvokeInst(Value *Func,
3274 BasicBlock *IfNormal, BasicBlock *IfException,
3275 ArrayRef<Value *> Args, unsigned Values,
3276 const Twine &NameStr, BasicBlock *InsertAtEnd)
3277 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3278 ->getElementType())->getReturnType(),
3279 Instruction::Invoke,
3280 OperandTraits<InvokeInst>::op_end(this) - Values,
3281 Values, InsertAtEnd) {
3282 init(Func, IfNormal, IfException, Args, NameStr);
3285 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3287 //===----------------------------------------------------------------------===//
3289 //===----------------------------------------------------------------------===//
3291 //===---------------------------------------------------------------------------
3292 /// ResumeInst - Resume the propagation of an exception.
3294 class ResumeInst : public TerminatorInst {
3295 ResumeInst(const ResumeInst &RI);
3297 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3298 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3300 ResumeInst *clone_impl() const override;
3302 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3303 return new(1) ResumeInst(Exn, InsertBefore);
3305 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3306 return new(1) ResumeInst(Exn, InsertAtEnd);
3309 /// Provide fast operand accessors
3310 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3312 /// Convenience accessor.
3313 Value *getValue() const { return Op<0>(); }
3315 unsigned getNumSuccessors() const { return 0; }
3317 // Methods for support type inquiry through isa, cast, and dyn_cast:
3318 static inline bool classof(const Instruction *I) {
3319 return I->getOpcode() == Instruction::Resume;
3321 static inline bool classof(const Value *V) {
3322 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3325 BasicBlock *getSuccessorV(unsigned idx) const override;
3326 unsigned getNumSuccessorsV() const override;
3327 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3331 struct OperandTraits<ResumeInst> :
3332 public FixedNumOperandTraits<ResumeInst, 1> {
3335 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3337 //===----------------------------------------------------------------------===//
3338 // UnreachableInst Class
3339 //===----------------------------------------------------------------------===//
3341 //===---------------------------------------------------------------------------
3342 /// UnreachableInst - This function has undefined behavior. In particular, the
3343 /// presence of this instruction indicates some higher level knowledge that the
3344 /// end of the block cannot be reached.
3346 class UnreachableInst : public TerminatorInst {
3347 void *operator new(size_t, unsigned) = delete;
3349 UnreachableInst *clone_impl() const override;
3352 // allocate space for exactly zero operands
3353 void *operator new(size_t s) {
3354 return User::operator new(s, 0);
3356 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3357 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3359 unsigned getNumSuccessors() const { return 0; }
3361 // Methods for support type inquiry through isa, cast, and dyn_cast:
3362 static inline bool classof(const Instruction *I) {
3363 return I->getOpcode() == Instruction::Unreachable;
3365 static inline bool classof(const Value *V) {
3366 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3369 BasicBlock *getSuccessorV(unsigned idx) const override;
3370 unsigned getNumSuccessorsV() const override;
3371 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3374 //===----------------------------------------------------------------------===//
3376 //===----------------------------------------------------------------------===//
3378 /// \brief This class represents a truncation of integer types.
3379 class TruncInst : public CastInst {
3381 /// \brief Clone an identical TruncInst
3382 TruncInst *clone_impl() const override;
3385 /// \brief Constructor with insert-before-instruction semantics
3387 Value *S, ///< The value to be truncated
3388 Type *Ty, ///< The (smaller) type to truncate to
3389 const Twine &NameStr = "", ///< A name for the new instruction
3390 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3393 /// \brief Constructor with insert-at-end-of-block semantics
3395 Value *S, ///< The value to be truncated
3396 Type *Ty, ///< The (smaller) type to truncate to
3397 const Twine &NameStr, ///< A name for the new instruction
3398 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3401 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3402 static inline bool classof(const Instruction *I) {
3403 return I->getOpcode() == Trunc;
3405 static inline bool classof(const Value *V) {
3406 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3410 //===----------------------------------------------------------------------===//
3412 //===----------------------------------------------------------------------===//
3414 /// \brief This class represents zero extension of integer types.
3415 class ZExtInst : public CastInst {
3417 /// \brief Clone an identical ZExtInst
3418 ZExtInst *clone_impl() const override;
3421 /// \brief Constructor with insert-before-instruction semantics
3423 Value *S, ///< The value to be zero extended
3424 Type *Ty, ///< The type to zero extend to
3425 const Twine &NameStr = "", ///< A name for the new instruction
3426 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3429 /// \brief Constructor with insert-at-end semantics.
3431 Value *S, ///< The value to be zero extended
3432 Type *Ty, ///< The type to zero extend to
3433 const Twine &NameStr, ///< A name for the new instruction
3434 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3437 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3438 static inline bool classof(const Instruction *I) {
3439 return I->getOpcode() == ZExt;
3441 static inline bool classof(const Value *V) {
3442 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3446 //===----------------------------------------------------------------------===//
3448 //===----------------------------------------------------------------------===//
3450 /// \brief This class represents a sign extension of integer types.
3451 class SExtInst : public CastInst {
3453 /// \brief Clone an identical SExtInst
3454 SExtInst *clone_impl() const override;
3457 /// \brief Constructor with insert-before-instruction semantics
3459 Value *S, ///< The value to be sign extended
3460 Type *Ty, ///< The type to sign extend to
3461 const Twine &NameStr = "", ///< A name for the new instruction
3462 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3465 /// \brief Constructor with insert-at-end-of-block semantics
3467 Value *S, ///< The value to be sign extended
3468 Type *Ty, ///< The type to sign extend to
3469 const Twine &NameStr, ///< A name for the new instruction
3470 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3473 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3474 static inline bool classof(const Instruction *I) {
3475 return I->getOpcode() == SExt;
3477 static inline bool classof(const Value *V) {
3478 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3482 //===----------------------------------------------------------------------===//
3483 // FPTruncInst Class
3484 //===----------------------------------------------------------------------===//
3486 /// \brief This class represents a truncation of floating point types.
3487 class FPTruncInst : public CastInst {
3489 /// \brief Clone an identical FPTruncInst
3490 FPTruncInst *clone_impl() const override;
3493 /// \brief Constructor with insert-before-instruction semantics
3495 Value *S, ///< The value to be truncated
3496 Type *Ty, ///< The type to truncate to
3497 const Twine &NameStr = "", ///< A name for the new instruction
3498 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3501 /// \brief Constructor with insert-before-instruction semantics
3503 Value *S, ///< The value to be truncated
3504 Type *Ty, ///< The type to truncate to
3505 const Twine &NameStr, ///< A name for the new instruction
3506 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3509 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3510 static inline bool classof(const Instruction *I) {
3511 return I->getOpcode() == FPTrunc;
3513 static inline bool classof(const Value *V) {
3514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3518 //===----------------------------------------------------------------------===//
3520 //===----------------------------------------------------------------------===//
3522 /// \brief This class represents an extension of floating point types.
3523 class FPExtInst : public CastInst {
3525 /// \brief Clone an identical FPExtInst
3526 FPExtInst *clone_impl() const override;
3529 /// \brief Constructor with insert-before-instruction semantics
3531 Value *S, ///< The value to be extended
3532 Type *Ty, ///< The type to extend to
3533 const Twine &NameStr = "", ///< A name for the new instruction
3534 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3537 /// \brief Constructor with insert-at-end-of-block semantics
3539 Value *S, ///< The value to be extended
3540 Type *Ty, ///< The type to extend to
3541 const Twine &NameStr, ///< A name for the new instruction
3542 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3545 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3546 static inline bool classof(const Instruction *I) {
3547 return I->getOpcode() == FPExt;
3549 static inline bool classof(const Value *V) {
3550 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3554 //===----------------------------------------------------------------------===//
3556 //===----------------------------------------------------------------------===//
3558 /// \brief This class represents a cast unsigned integer to floating point.
3559 class UIToFPInst : public CastInst {
3561 /// \brief Clone an identical UIToFPInst
3562 UIToFPInst *clone_impl() const override;
3565 /// \brief Constructor with insert-before-instruction semantics
3567 Value *S, ///< The value to be converted
3568 Type *Ty, ///< The type to convert to
3569 const Twine &NameStr = "", ///< A name for the new instruction
3570 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3573 /// \brief Constructor with insert-at-end-of-block semantics
3575 Value *S, ///< The value to be converted
3576 Type *Ty, ///< The type to convert to
3577 const Twine &NameStr, ///< A name for the new instruction
3578 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3581 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3582 static inline bool classof(const Instruction *I) {
3583 return I->getOpcode() == UIToFP;
3585 static inline bool classof(const Value *V) {
3586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3590 //===----------------------------------------------------------------------===//
3592 //===----------------------------------------------------------------------===//
3594 /// \brief This class represents a cast from signed integer to floating point.
3595 class SIToFPInst : public CastInst {
3597 /// \brief Clone an identical SIToFPInst
3598 SIToFPInst *clone_impl() const override;
3601 /// \brief Constructor with insert-before-instruction semantics
3603 Value *S, ///< The value to be converted
3604 Type *Ty, ///< The type to convert to
3605 const Twine &NameStr = "", ///< A name for the new instruction
3606 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3609 /// \brief Constructor with insert-at-end-of-block semantics
3611 Value *S, ///< The value to be converted
3612 Type *Ty, ///< The type to convert to
3613 const Twine &NameStr, ///< A name for the new instruction
3614 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3617 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3618 static inline bool classof(const Instruction *I) {
3619 return I->getOpcode() == SIToFP;
3621 static inline bool classof(const Value *V) {
3622 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3626 //===----------------------------------------------------------------------===//
3628 //===----------------------------------------------------------------------===//
3630 /// \brief This class represents a cast from floating point to unsigned integer
3631 class FPToUIInst : public CastInst {
3633 /// \brief Clone an identical FPToUIInst
3634 FPToUIInst *clone_impl() const override;
3637 /// \brief Constructor with insert-before-instruction semantics
3639 Value *S, ///< The value to be converted
3640 Type *Ty, ///< The type to convert to
3641 const Twine &NameStr = "", ///< A name for the new instruction
3642 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3645 /// \brief Constructor with insert-at-end-of-block semantics
3647 Value *S, ///< The value to be converted
3648 Type *Ty, ///< The type to convert to
3649 const Twine &NameStr, ///< A name for the new instruction
3650 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3653 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3654 static inline bool classof(const Instruction *I) {
3655 return I->getOpcode() == FPToUI;
3657 static inline bool classof(const Value *V) {
3658 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3662 //===----------------------------------------------------------------------===//
3664 //===----------------------------------------------------------------------===//
3666 /// \brief This class represents a cast from floating point to signed integer.
3667 class FPToSIInst : public CastInst {
3669 /// \brief Clone an identical FPToSIInst
3670 FPToSIInst *clone_impl() const override;
3673 /// \brief Constructor with insert-before-instruction semantics
3675 Value *S, ///< The value to be converted
3676 Type *Ty, ///< The type to convert to
3677 const Twine &NameStr = "", ///< A name for the new instruction
3678 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3681 /// \brief Constructor with insert-at-end-of-block semantics
3683 Value *S, ///< The value to be converted
3684 Type *Ty, ///< The type to convert to
3685 const Twine &NameStr, ///< A name for the new instruction
3686 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3689 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3690 static inline bool classof(const Instruction *I) {
3691 return I->getOpcode() == FPToSI;
3693 static inline bool classof(const Value *V) {
3694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3698 //===----------------------------------------------------------------------===//
3699 // IntToPtrInst Class
3700 //===----------------------------------------------------------------------===//
3702 /// \brief This class represents a cast from an integer to a pointer.
3703 class IntToPtrInst : public CastInst {
3705 /// \brief Constructor with insert-before-instruction semantics
3707 Value *S, ///< The value to be converted
3708 Type *Ty, ///< The type to convert to
3709 const Twine &NameStr = "", ///< A name for the new instruction
3710 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3713 /// \brief Constructor with insert-at-end-of-block semantics
3715 Value *S, ///< The value to be converted
3716 Type *Ty, ///< The type to convert to
3717 const Twine &NameStr, ///< A name for the new instruction
3718 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3721 /// \brief Clone an identical IntToPtrInst
3722 IntToPtrInst *clone_impl() const override;
3724 /// \brief Returns the address space of this instruction's pointer type.
3725 unsigned getAddressSpace() const {
3726 return getType()->getPointerAddressSpace();
3729 // Methods for support type inquiry through isa, cast, and dyn_cast:
3730 static inline bool classof(const Instruction *I) {
3731 return I->getOpcode() == IntToPtr;
3733 static inline bool classof(const Value *V) {
3734 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3738 //===----------------------------------------------------------------------===//
3739 // PtrToIntInst Class
3740 //===----------------------------------------------------------------------===//
3742 /// \brief This class represents a cast from a pointer to an integer
3743 class PtrToIntInst : public CastInst {
3745 /// \brief Clone an identical PtrToIntInst
3746 PtrToIntInst *clone_impl() const override;
3749 /// \brief Constructor with insert-before-instruction semantics
3751 Value *S, ///< The value to be converted
3752 Type *Ty, ///< The type to convert to
3753 const Twine &NameStr = "", ///< A name for the new instruction
3754 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3757 /// \brief Constructor with insert-at-end-of-block semantics
3759 Value *S, ///< The value to be converted
3760 Type *Ty, ///< The type to convert to
3761 const Twine &NameStr, ///< A name for the new instruction
3762 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3765 /// \brief Gets the pointer operand.
3766 Value *getPointerOperand() { return getOperand(0); }
3767 /// \brief Gets the pointer operand.
3768 const Value *getPointerOperand() const { return getOperand(0); }
3769 /// \brief Gets the operand index of the pointer operand.
3770 static unsigned getPointerOperandIndex() { return 0U; }
3772 /// \brief Returns the address space of the pointer operand.
3773 unsigned getPointerAddressSpace() const {
3774 return getPointerOperand()->getType()->getPointerAddressSpace();
3777 // Methods for support type inquiry through isa, cast, and dyn_cast:
3778 static inline bool classof(const Instruction *I) {
3779 return I->getOpcode() == PtrToInt;
3781 static inline bool classof(const Value *V) {
3782 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3786 //===----------------------------------------------------------------------===//
3787 // BitCastInst Class
3788 //===----------------------------------------------------------------------===//
3790 /// \brief This class represents a no-op cast from one type to another.
3791 class BitCastInst : public CastInst {
3793 /// \brief Clone an identical BitCastInst
3794 BitCastInst *clone_impl() const override;
3797 /// \brief Constructor with insert-before-instruction semantics
3799 Value *S, ///< The value to be casted
3800 Type *Ty, ///< The type to casted to
3801 const Twine &NameStr = "", ///< A name for the new instruction
3802 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3805 /// \brief Constructor with insert-at-end-of-block semantics
3807 Value *S, ///< The value to be casted
3808 Type *Ty, ///< The type to casted to
3809 const Twine &NameStr, ///< A name for the new instruction
3810 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3813 // Methods for support type inquiry through isa, cast, and dyn_cast:
3814 static inline bool classof(const Instruction *I) {
3815 return I->getOpcode() == BitCast;
3817 static inline bool classof(const Value *V) {
3818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3822 //===----------------------------------------------------------------------===//
3823 // AddrSpaceCastInst Class
3824 //===----------------------------------------------------------------------===//
3826 /// \brief This class represents a conversion between pointers from
3827 /// one address space to another.
3828 class AddrSpaceCastInst : public CastInst {
3830 /// \brief Clone an identical AddrSpaceCastInst
3831 AddrSpaceCastInst *clone_impl() const override;
3834 /// \brief Constructor with insert-before-instruction semantics
3836 Value *S, ///< The value to be casted
3837 Type *Ty, ///< The type to casted to
3838 const Twine &NameStr = "", ///< A name for the new instruction
3839 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3842 /// \brief Constructor with insert-at-end-of-block semantics
3844 Value *S, ///< The value to be casted
3845 Type *Ty, ///< The type to casted to
3846 const Twine &NameStr, ///< A name for the new instruction
3847 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3850 // Methods for support type inquiry through isa, cast, and dyn_cast:
3851 static inline bool classof(const Instruction *I) {
3852 return I->getOpcode() == AddrSpaceCast;
3854 static inline bool classof(const Value *V) {
3855 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3859 } // End llvm namespace