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/iterator_range.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/Constant.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/OperandTraits.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/Use.h"
35 #include "llvm/IR/User.h"
36 #include "llvm/Support/AtomicOrdering.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/ErrorHandling.h"
50 enum SynchronizationScope {
55 //===----------------------------------------------------------------------===//
57 //===----------------------------------------------------------------------===//
59 /// an instruction to allocate memory on the stack
60 class AllocaInst : public UnaryInstruction {
64 // Note: Instruction needs to be a friend here to call cloneImpl.
65 friend class Instruction;
67 AllocaInst *cloneImpl() const;
70 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
71 const Twine &Name = "",
72 Instruction *InsertBefore = nullptr);
73 AllocaInst(Type *Ty, Value *ArraySize,
74 const Twine &Name, BasicBlock *InsertAtEnd);
76 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
77 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
79 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
80 const Twine &Name = "", Instruction *InsertBefore = nullptr);
81 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
82 const Twine &Name, BasicBlock *InsertAtEnd);
84 // Out of line virtual method, so the vtable, etc. has a home.
85 ~AllocaInst() override;
87 /// Return true if there is an allocation size parameter to the allocation
88 /// instruction that is not 1.
89 bool isArrayAllocation() const;
91 /// Get the number of elements allocated. For a simple allocation of a single
92 /// element, this will return a constant 1 value.
93 const Value *getArraySize() const { return getOperand(0); }
94 Value *getArraySize() { return getOperand(0); }
96 /// Overload to return most specific pointer type.
97 PointerType *getType() const {
98 return cast<PointerType>(Instruction::getType());
101 /// Return the type that is being allocated by the instruction.
102 Type *getAllocatedType() const { return AllocatedType; }
103 /// for use only in special circumstances that need to generically
104 /// transform a whole instruction (eg: IR linking and vectorization).
105 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
107 /// Return the alignment of the memory that is being allocated by the
109 unsigned getAlignment() const {
110 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
112 void setAlignment(unsigned Align);
114 /// Return true if this alloca is in the entry block of the function and is a
115 /// constant size. If so, the code generator will fold it into the
116 /// prolog/epilog code, so it is basically free.
117 bool isStaticAlloca() const;
119 /// Return true if this alloca is used as an inalloca argument to a call. Such
120 /// allocas are never considered static even if they are in the entry block.
121 bool isUsedWithInAlloca() const {
122 return getSubclassDataFromInstruction() & 32;
125 /// Specify whether this alloca is used to represent the arguments to a call.
126 void setUsedWithInAlloca(bool V) {
127 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
131 /// Return true if this alloca is used as a swifterror argument to a call.
132 bool isSwiftError() const {
133 return getSubclassDataFromInstruction() & 64;
136 /// Specify whether this alloca is used to represent a swifterror.
137 void setSwiftError(bool V) {
138 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
142 // Methods for support type inquiry through isa, cast, and dyn_cast:
143 static inline bool classof(const Instruction *I) {
144 return (I->getOpcode() == Instruction::Alloca);
146 static inline bool classof(const Value *V) {
147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
151 // Shadow Instruction::setInstructionSubclassData with a private forwarding
152 // method so that subclasses cannot accidentally use it.
153 void setInstructionSubclassData(unsigned short D) {
154 Instruction::setInstructionSubclassData(D);
158 //===----------------------------------------------------------------------===//
160 //===----------------------------------------------------------------------===//
162 /// An instruction for reading from memory. This uses the SubclassData field in
163 /// Value to store whether or not the load is volatile.
164 class LoadInst : public UnaryInstruction {
168 // Note: Instruction needs to be a friend here to call cloneImpl.
169 friend class Instruction;
171 LoadInst *cloneImpl() const;
174 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
175 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
176 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
177 Instruction *InsertBefore = nullptr);
178 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
179 Instruction *InsertBefore = nullptr)
180 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
181 NameStr, isVolatile, InsertBefore) {}
182 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
183 BasicBlock *InsertAtEnd);
184 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
185 Instruction *InsertBefore = nullptr)
186 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
187 NameStr, isVolatile, Align, InsertBefore) {}
188 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
189 unsigned Align, Instruction *InsertBefore = nullptr);
190 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
191 unsigned Align, BasicBlock *InsertAtEnd);
192 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
193 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
194 Instruction *InsertBefore = nullptr)
195 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
196 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
197 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
198 unsigned Align, AtomicOrdering Order,
199 SynchronizationScope SynchScope = CrossThread,
200 Instruction *InsertBefore = nullptr);
201 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
202 unsigned Align, AtomicOrdering Order,
203 SynchronizationScope SynchScope,
204 BasicBlock *InsertAtEnd);
205 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
206 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
207 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
208 bool isVolatile = false, Instruction *InsertBefore = nullptr);
209 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
210 bool isVolatile = false,
211 Instruction *InsertBefore = nullptr)
212 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
213 NameStr, isVolatile, InsertBefore) {}
214 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
215 BasicBlock *InsertAtEnd);
217 /// Return true if this is a load from a volatile memory location.
218 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
220 /// Specify whether this is a volatile load or not.
221 void setVolatile(bool V) {
222 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
226 /// Return the alignment of the access that is being performed.
227 unsigned getAlignment() const {
228 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
231 void setAlignment(unsigned Align);
233 /// Returns the ordering effect of this fence.
234 AtomicOrdering getOrdering() const {
235 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
238 /// Set the ordering constraint on this load. May not be Release or
240 void setOrdering(AtomicOrdering Ordering) {
241 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
242 ((unsigned)Ordering << 7));
245 SynchronizationScope getSynchScope() const {
246 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
249 /// Specify whether this load is ordered with respect to all
250 /// concurrently executing threads, or only with respect to signal handlers
251 /// executing in the same thread.
252 void setSynchScope(SynchronizationScope xthread) {
253 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
257 void setAtomic(AtomicOrdering Ordering,
258 SynchronizationScope SynchScope = CrossThread) {
259 setOrdering(Ordering);
260 setSynchScope(SynchScope);
263 bool isSimple() const { return !isAtomic() && !isVolatile(); }
264 bool isUnordered() const {
265 return (getOrdering() == AtomicOrdering::NotAtomic ||
266 getOrdering() == AtomicOrdering::Unordered) &&
270 Value *getPointerOperand() { return getOperand(0); }
271 const Value *getPointerOperand() const { return getOperand(0); }
272 static unsigned getPointerOperandIndex() { return 0U; }
274 /// Returns the address space of the pointer operand.
275 unsigned getPointerAddressSpace() const {
276 return getPointerOperand()->getType()->getPointerAddressSpace();
279 // Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const Instruction *I) {
281 return I->getOpcode() == Instruction::Load;
283 static inline bool classof(const Value *V) {
284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
288 // Shadow Instruction::setInstructionSubclassData with a private forwarding
289 // method so that subclasses cannot accidentally use it.
290 void setInstructionSubclassData(unsigned short D) {
291 Instruction::setInstructionSubclassData(D);
295 //===----------------------------------------------------------------------===//
297 //===----------------------------------------------------------------------===//
299 /// An instruction for storing to memory.
300 class StoreInst : public Instruction {
304 // Note: Instruction needs to be a friend here to call cloneImpl.
305 friend class Instruction;
307 StoreInst *cloneImpl() const;
310 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
311 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
312 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
313 Instruction *InsertBefore = nullptr);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, Instruction *InsertBefore = nullptr);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
318 unsigned Align, BasicBlock *InsertAtEnd);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320 unsigned Align, AtomicOrdering Order,
321 SynchronizationScope SynchScope = CrossThread,
322 Instruction *InsertBefore = nullptr);
323 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
324 unsigned Align, AtomicOrdering Order,
325 SynchronizationScope SynchScope,
326 BasicBlock *InsertAtEnd);
328 // allocate space for exactly two operands
329 void *operator new(size_t s) {
330 return User::operator new(s, 2);
333 void *operator new(size_t, unsigned) = delete;
335 /// Return true if this is a store to a volatile memory location.
336 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
338 /// Specify whether this is a volatile store or not.
339 void setVolatile(bool V) {
340 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
344 /// Transparently provide more efficient getOperand methods.
345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
347 /// Return the alignment of the access that is being performed
348 unsigned getAlignment() const {
349 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
352 void setAlignment(unsigned Align);
354 /// Returns the ordering effect of this store.
355 AtomicOrdering getOrdering() const {
356 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
359 /// Set the ordering constraint on this store. May not be Acquire or
361 void setOrdering(AtomicOrdering Ordering) {
362 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
363 ((unsigned)Ordering << 7));
366 SynchronizationScope getSynchScope() const {
367 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
370 /// Specify whether this store instruction is ordered with respect to all
371 /// concurrently executing threads, or only with respect to signal handlers
372 /// executing in the same thread.
373 void setSynchScope(SynchronizationScope xthread) {
374 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
378 void setAtomic(AtomicOrdering Ordering,
379 SynchronizationScope SynchScope = CrossThread) {
380 setOrdering(Ordering);
381 setSynchScope(SynchScope);
384 bool isSimple() const { return !isAtomic() && !isVolatile(); }
385 bool isUnordered() const {
386 return (getOrdering() == AtomicOrdering::NotAtomic ||
387 getOrdering() == AtomicOrdering::Unordered) &&
391 Value *getValueOperand() { return getOperand(0); }
392 const Value *getValueOperand() const { return getOperand(0); }
394 Value *getPointerOperand() { return getOperand(1); }
395 const Value *getPointerOperand() const { return getOperand(1); }
396 static unsigned getPointerOperandIndex() { return 1U; }
398 /// Returns the address space of the pointer operand.
399 unsigned getPointerAddressSpace() const {
400 return getPointerOperand()->getType()->getPointerAddressSpace();
403 // Methods for support type inquiry through isa, cast, and dyn_cast:
404 static inline bool classof(const Instruction *I) {
405 return I->getOpcode() == Instruction::Store;
407 static inline bool classof(const Value *V) {
408 return isa<Instruction>(V) && classof(cast<Instruction>(V));
412 // Shadow Instruction::setInstructionSubclassData with a private forwarding
413 // method so that subclasses cannot accidentally use it.
414 void setInstructionSubclassData(unsigned short D) {
415 Instruction::setInstructionSubclassData(D);
420 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
423 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
425 //===----------------------------------------------------------------------===//
427 //===----------------------------------------------------------------------===//
429 /// An instruction for ordering other memory operations.
430 class FenceInst : public Instruction {
431 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
434 // Note: Instruction needs to be a friend here to call cloneImpl.
435 friend class Instruction;
437 FenceInst *cloneImpl() const;
440 // Ordering may only be Acquire, Release, AcquireRelease, or
441 // SequentiallyConsistent.
442 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
443 SynchronizationScope SynchScope = CrossThread,
444 Instruction *InsertBefore = nullptr);
445 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
446 SynchronizationScope SynchScope,
447 BasicBlock *InsertAtEnd);
449 // allocate space for exactly zero operands
450 void *operator new(size_t s) {
451 return User::operator new(s, 0);
454 void *operator new(size_t, unsigned) = delete;
456 /// Returns the ordering effect of this fence.
457 AtomicOrdering getOrdering() const {
458 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
461 /// Set the ordering constraint on this fence. May only be Acquire, Release,
462 /// AcquireRelease, or SequentiallyConsistent.
463 void setOrdering(AtomicOrdering Ordering) {
464 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
465 ((unsigned)Ordering << 1));
468 SynchronizationScope getSynchScope() const {
469 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
472 /// Specify whether this fence orders other operations with respect to all
473 /// concurrently executing threads, or only with respect to signal handlers
474 /// executing in the same thread.
475 void setSynchScope(SynchronizationScope xthread) {
476 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
480 // Methods for support type inquiry through isa, cast, and dyn_cast:
481 static inline bool classof(const Instruction *I) {
482 return I->getOpcode() == Instruction::Fence;
484 static inline bool classof(const Value *V) {
485 return isa<Instruction>(V) && classof(cast<Instruction>(V));
489 // Shadow Instruction::setInstructionSubclassData with a private forwarding
490 // method so that subclasses cannot accidentally use it.
491 void setInstructionSubclassData(unsigned short D) {
492 Instruction::setInstructionSubclassData(D);
496 //===----------------------------------------------------------------------===//
497 // AtomicCmpXchgInst Class
498 //===----------------------------------------------------------------------===//
500 /// an instruction that atomically checks whether a
501 /// specified value is in a memory location, and, if it is, stores a new value
502 /// there. Returns the value that was loaded.
504 class AtomicCmpXchgInst : public Instruction {
505 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
506 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
507 SynchronizationScope SynchScope);
510 // Note: Instruction needs to be a friend here to call cloneImpl.
511 friend class Instruction;
513 AtomicCmpXchgInst *cloneImpl() const;
516 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
517 AtomicOrdering SuccessOrdering,
518 AtomicOrdering FailureOrdering,
519 SynchronizationScope SynchScope,
520 Instruction *InsertBefore = nullptr);
521 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
522 AtomicOrdering SuccessOrdering,
523 AtomicOrdering FailureOrdering,
524 SynchronizationScope SynchScope,
525 BasicBlock *InsertAtEnd);
527 // allocate space for exactly three operands
528 void *operator new(size_t s) {
529 return User::operator new(s, 3);
532 void *operator new(size_t, unsigned) = delete;
534 /// Return true if this is a cmpxchg from a volatile memory
537 bool isVolatile() const {
538 return getSubclassDataFromInstruction() & 1;
541 /// Specify whether this is a volatile cmpxchg.
543 void setVolatile(bool V) {
544 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
548 /// Return true if this cmpxchg may spuriously fail.
549 bool isWeak() const {
550 return getSubclassDataFromInstruction() & 0x100;
553 void setWeak(bool IsWeak) {
554 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
558 /// Transparently provide more efficient getOperand methods.
559 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
561 /// Set the ordering constraint on this cmpxchg.
562 void setSuccessOrdering(AtomicOrdering Ordering) {
563 assert(Ordering != AtomicOrdering::NotAtomic &&
564 "CmpXchg instructions can only be atomic.");
565 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
566 ((unsigned)Ordering << 2));
569 void setFailureOrdering(AtomicOrdering Ordering) {
570 assert(Ordering != AtomicOrdering::NotAtomic &&
571 "CmpXchg instructions can only be atomic.");
572 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
573 ((unsigned)Ordering << 5));
576 /// Specify whether this cmpxchg is atomic and orders other operations with
577 /// respect to all concurrently executing threads, or only with respect to
578 /// signal handlers executing in the same thread.
579 void setSynchScope(SynchronizationScope SynchScope) {
580 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
584 /// Returns the ordering constraint on this cmpxchg.
585 AtomicOrdering getSuccessOrdering() const {
586 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
589 /// Returns the ordering constraint on this cmpxchg.
590 AtomicOrdering getFailureOrdering() const {
591 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
594 /// Returns whether this cmpxchg is atomic between threads or only within a
596 SynchronizationScope getSynchScope() const {
597 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
600 Value *getPointerOperand() { return getOperand(0); }
601 const Value *getPointerOperand() const { return getOperand(0); }
602 static unsigned getPointerOperandIndex() { return 0U; }
604 Value *getCompareOperand() { return getOperand(1); }
605 const Value *getCompareOperand() const { return getOperand(1); }
607 Value *getNewValOperand() { return getOperand(2); }
608 const Value *getNewValOperand() const { return getOperand(2); }
610 /// Returns the address space of the pointer operand.
611 unsigned getPointerAddressSpace() const {
612 return getPointerOperand()->getType()->getPointerAddressSpace();
615 /// Returns the strongest permitted ordering on failure, given the
616 /// desired ordering on success.
618 /// If the comparison in a cmpxchg operation fails, there is no atomic store
619 /// so release semantics cannot be provided. So this function drops explicit
620 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
621 /// operation would remain SequentiallyConsistent.
622 static AtomicOrdering
623 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
624 switch (SuccessOrdering) {
626 llvm_unreachable("invalid cmpxchg success ordering");
627 case AtomicOrdering::Release:
628 case AtomicOrdering::Monotonic:
629 return AtomicOrdering::Monotonic;
630 case AtomicOrdering::AcquireRelease:
631 case AtomicOrdering::Acquire:
632 return AtomicOrdering::Acquire;
633 case AtomicOrdering::SequentiallyConsistent:
634 return AtomicOrdering::SequentiallyConsistent;
638 // Methods for support type inquiry through isa, cast, and dyn_cast:
639 static inline bool classof(const Instruction *I) {
640 return I->getOpcode() == Instruction::AtomicCmpXchg;
642 static inline bool classof(const Value *V) {
643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
647 // Shadow Instruction::setInstructionSubclassData with a private forwarding
648 // method so that subclasses cannot accidentally use it.
649 void setInstructionSubclassData(unsigned short D) {
650 Instruction::setInstructionSubclassData(D);
655 struct OperandTraits<AtomicCmpXchgInst> :
656 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
659 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
661 //===----------------------------------------------------------------------===//
662 // AtomicRMWInst Class
663 //===----------------------------------------------------------------------===//
665 /// an instruction that atomically reads a memory location,
666 /// combines it with another value, and then stores the result back. Returns
669 class AtomicRMWInst : public Instruction {
671 // Note: Instruction needs to be a friend here to call cloneImpl.
672 friend class Instruction;
674 AtomicRMWInst *cloneImpl() const;
677 /// This enumeration lists the possible modifications atomicrmw can make. In
678 /// the descriptions, 'p' is the pointer to the instruction's memory location,
679 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
680 /// instruction. These instructions always return 'old'.
696 /// *p = old >signed v ? old : v
698 /// *p = old <signed v ? old : v
700 /// *p = old >unsigned v ? old : v
702 /// *p = old <unsigned v ? old : v
710 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
711 AtomicOrdering Ordering, SynchronizationScope SynchScope,
712 Instruction *InsertBefore = nullptr);
713 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
714 AtomicOrdering Ordering, SynchronizationScope SynchScope,
715 BasicBlock *InsertAtEnd);
717 // allocate space for exactly two operands
718 void *operator new(size_t s) {
719 return User::operator new(s, 2);
722 void *operator new(size_t, unsigned) = delete;
724 BinOp getOperation() const {
725 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
728 void setOperation(BinOp Operation) {
729 unsigned short SubclassData = getSubclassDataFromInstruction();
730 setInstructionSubclassData((SubclassData & 31) |
734 /// Return true if this is a RMW on a volatile memory location.
736 bool isVolatile() const {
737 return getSubclassDataFromInstruction() & 1;
740 /// Specify whether this is a volatile RMW or not.
742 void setVolatile(bool V) {
743 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
747 /// Transparently provide more efficient getOperand methods.
748 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
750 /// Set the ordering constraint on this RMW.
751 void setOrdering(AtomicOrdering Ordering) {
752 assert(Ordering != AtomicOrdering::NotAtomic &&
753 "atomicrmw instructions can only be atomic.");
754 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
755 ((unsigned)Ordering << 2));
758 /// Specify whether this RMW orders other operations with respect to all
759 /// concurrently executing threads, or only with respect to signal handlers
760 /// executing in the same thread.
761 void setSynchScope(SynchronizationScope SynchScope) {
762 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
766 /// Returns the ordering constraint on this RMW.
767 AtomicOrdering getOrdering() const {
768 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
771 /// Returns whether this RMW is atomic between threads or only within a
773 SynchronizationScope getSynchScope() const {
774 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
777 Value *getPointerOperand() { return getOperand(0); }
778 const Value *getPointerOperand() const { return getOperand(0); }
779 static unsigned getPointerOperandIndex() { return 0U; }
781 Value *getValOperand() { return getOperand(1); }
782 const Value *getValOperand() const { return getOperand(1); }
784 /// Returns the address space of the pointer operand.
785 unsigned getPointerAddressSpace() const {
786 return getPointerOperand()->getType()->getPointerAddressSpace();
789 // Methods for support type inquiry through isa, cast, and dyn_cast:
790 static inline bool classof(const Instruction *I) {
791 return I->getOpcode() == Instruction::AtomicRMW;
793 static inline bool classof(const Value *V) {
794 return isa<Instruction>(V) && classof(cast<Instruction>(V));
798 void Init(BinOp Operation, Value *Ptr, Value *Val,
799 AtomicOrdering Ordering, SynchronizationScope SynchScope);
801 // Shadow Instruction::setInstructionSubclassData with a private forwarding
802 // method so that subclasses cannot accidentally use it.
803 void setInstructionSubclassData(unsigned short D) {
804 Instruction::setInstructionSubclassData(D);
809 struct OperandTraits<AtomicRMWInst>
810 : public FixedNumOperandTraits<AtomicRMWInst,2> {
813 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
815 //===----------------------------------------------------------------------===//
816 // GetElementPtrInst Class
817 //===----------------------------------------------------------------------===//
819 // checkGEPType - Simple wrapper function to give a better assertion failure
820 // message on bad indexes for a gep instruction.
822 inline Type *checkGEPType(Type *Ty) {
823 assert(Ty && "Invalid GetElementPtrInst indices for type!");
827 /// an instruction for type-safe pointer arithmetic to
828 /// access elements of arrays and structs
830 class GetElementPtrInst : public Instruction {
831 Type *SourceElementType;
832 Type *ResultElementType;
834 void anchor() override;
836 GetElementPtrInst(const GetElementPtrInst &GEPI);
837 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
839 /// Constructors - Create a getelementptr instruction with a base pointer an
840 /// list of indices. The first ctor can optionally insert before an existing
841 /// instruction, the second appends the new instruction to the specified
843 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
844 ArrayRef<Value *> IdxList, unsigned Values,
845 const Twine &NameStr, Instruction *InsertBefore);
846 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
847 ArrayRef<Value *> IdxList, unsigned Values,
848 const Twine &NameStr, BasicBlock *InsertAtEnd);
851 // Note: Instruction needs to be a friend here to call cloneImpl.
852 friend class Instruction;
854 GetElementPtrInst *cloneImpl() const;
857 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
858 ArrayRef<Value *> IdxList,
859 const Twine &NameStr = "",
860 Instruction *InsertBefore = nullptr) {
861 unsigned Values = 1 + unsigned(IdxList.size());
864 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
868 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
869 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
870 NameStr, InsertBefore);
873 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
874 ArrayRef<Value *> IdxList,
875 const Twine &NameStr,
876 BasicBlock *InsertAtEnd) {
877 unsigned Values = 1 + unsigned(IdxList.size());
880 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
884 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
885 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
886 NameStr, InsertAtEnd);
889 /// Create an "inbounds" getelementptr. See the documentation for the
890 /// "inbounds" flag in LangRef.html for details.
891 static GetElementPtrInst *CreateInBounds(Value *Ptr,
892 ArrayRef<Value *> IdxList,
893 const Twine &NameStr = "",
894 Instruction *InsertBefore = nullptr){
895 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
898 static GetElementPtrInst *
899 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
900 const Twine &NameStr = "",
901 Instruction *InsertBefore = nullptr) {
902 GetElementPtrInst *GEP =
903 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
904 GEP->setIsInBounds(true);
908 static GetElementPtrInst *CreateInBounds(Value *Ptr,
909 ArrayRef<Value *> IdxList,
910 const Twine &NameStr,
911 BasicBlock *InsertAtEnd) {
912 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
915 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
916 ArrayRef<Value *> IdxList,
917 const Twine &NameStr,
918 BasicBlock *InsertAtEnd) {
919 GetElementPtrInst *GEP =
920 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
921 GEP->setIsInBounds(true);
925 /// Transparently provide more efficient getOperand methods.
926 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
928 Type *getSourceElementType() const { return SourceElementType; }
930 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
931 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
933 Type *getResultElementType() const {
934 assert(ResultElementType ==
935 cast<PointerType>(getType()->getScalarType())->getElementType());
936 return ResultElementType;
939 /// Returns the address space of this instruction's pointer type.
940 unsigned getAddressSpace() const {
941 // Note that this is always the same as the pointer operand's address space
942 // and that is cheaper to compute, so cheat here.
943 return getPointerAddressSpace();
946 /// Returns the type of the element that would be loaded with
947 /// a load instruction with the specified parameters.
949 /// Null is returned if the indices are invalid for the specified
952 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
953 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
954 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
956 inline op_iterator idx_begin() { return op_begin()+1; }
957 inline const_op_iterator idx_begin() const { return op_begin()+1; }
958 inline op_iterator idx_end() { return op_end(); }
959 inline const_op_iterator idx_end() const { return op_end(); }
961 Value *getPointerOperand() {
962 return getOperand(0);
964 const Value *getPointerOperand() const {
965 return getOperand(0);
967 static unsigned getPointerOperandIndex() {
968 return 0U; // get index for modifying correct operand.
971 /// Method to return the pointer operand as a
973 Type *getPointerOperandType() const {
974 return getPointerOperand()->getType();
977 /// Returns the address space of the pointer operand.
978 unsigned getPointerAddressSpace() const {
979 return getPointerOperandType()->getPointerAddressSpace();
982 /// Returns the pointer type returned by the GEP
983 /// instruction, which may be a vector of pointers.
984 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
985 return getGEPReturnType(
986 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
989 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
990 ArrayRef<Value *> IdxList) {
991 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
992 Ptr->getType()->getPointerAddressSpace());
994 if (Ptr->getType()->isVectorTy()) {
995 unsigned NumElem = Ptr->getType()->getVectorNumElements();
996 return VectorType::get(PtrTy, NumElem);
998 for (Value *Index : IdxList)
999 if (Index->getType()->isVectorTy()) {
1000 unsigned NumElem = Index->getType()->getVectorNumElements();
1001 return VectorType::get(PtrTy, NumElem);
1007 unsigned getNumIndices() const { // Note: always non-negative
1008 return getNumOperands() - 1;
1011 bool hasIndices() const {
1012 return getNumOperands() > 1;
1015 /// Return true if all of the indices of this GEP are
1016 /// zeros. If so, the result pointer and the first operand have the same
1017 /// value, just potentially different types.
1018 bool hasAllZeroIndices() const;
1020 /// Return true if all of the indices of this GEP are
1021 /// constant integers. If so, the result pointer and the first operand have
1022 /// a constant offset between them.
1023 bool hasAllConstantIndices() const;
1025 /// Set or clear the inbounds flag on this GEP instruction.
1026 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1027 void setIsInBounds(bool b = true);
1029 /// Determine whether the GEP has the inbounds flag.
1030 bool isInBounds() const;
1032 /// Accumulate the constant address offset of this GEP if possible.
1034 /// This routine accepts an APInt into which it will accumulate the constant
1035 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1036 /// all-constant, it returns false and the value of the offset APInt is
1037 /// undefined (it is *not* preserved!). The APInt passed into this routine
1038 /// must be at least as wide as the IntPtr type for the address space of
1039 /// the base GEP pointer.
1040 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1042 // Methods for support type inquiry through isa, cast, and dyn_cast:
1043 static inline bool classof(const Instruction *I) {
1044 return (I->getOpcode() == Instruction::GetElementPtr);
1046 static inline bool classof(const Value *V) {
1047 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1052 struct OperandTraits<GetElementPtrInst> :
1053 public VariadicOperandTraits<GetElementPtrInst, 1> {
1056 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1057 ArrayRef<Value *> IdxList, unsigned Values,
1058 const Twine &NameStr,
1059 Instruction *InsertBefore)
1060 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1061 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1062 Values, InsertBefore),
1063 SourceElementType(PointeeType),
1064 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1065 assert(ResultElementType ==
1066 cast<PointerType>(getType()->getScalarType())->getElementType());
1067 init(Ptr, IdxList, NameStr);
1070 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1071 ArrayRef<Value *> IdxList, unsigned Values,
1072 const Twine &NameStr,
1073 BasicBlock *InsertAtEnd)
1074 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1075 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1076 Values, InsertAtEnd),
1077 SourceElementType(PointeeType),
1078 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1079 assert(ResultElementType ==
1080 cast<PointerType>(getType()->getScalarType())->getElementType());
1081 init(Ptr, IdxList, NameStr);
1084 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1086 //===----------------------------------------------------------------------===//
1088 //===----------------------------------------------------------------------===//
1090 /// This instruction compares its operands according to the predicate given
1091 /// to the constructor. It only operates on integers or pointers. The operands
1092 /// must be identical types.
1093 /// Represent an integer comparison operator.
1094 class ICmpInst: public CmpInst {
1095 void anchor() override;
1098 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1099 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1100 "Invalid ICmp predicate value");
1101 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1102 "Both operands to ICmp instruction are not of the same type!");
1103 // Check that the operands are the right type
1104 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1105 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1106 "Invalid operand types for ICmp instruction");
1110 // Note: Instruction needs to be a friend here to call cloneImpl.
1111 friend class Instruction;
1113 /// Clone an identical ICmpInst
1114 ICmpInst *cloneImpl() const;
1117 /// Constructor with insert-before-instruction semantics.
1119 Instruction *InsertBefore, ///< Where to insert
1120 Predicate pred, ///< The predicate to use for the comparison
1121 Value *LHS, ///< The left-hand-side of the expression
1122 Value *RHS, ///< The right-hand-side of the expression
1123 const Twine &NameStr = "" ///< Name of the instruction
1124 ) : CmpInst(makeCmpResultType(LHS->getType()),
1125 Instruction::ICmp, pred, LHS, RHS, NameStr,
1132 /// Constructor with insert-at-end semantics.
1134 BasicBlock &InsertAtEnd, ///< Block to insert into.
1135 Predicate pred, ///< The predicate to use for the comparison
1136 Value *LHS, ///< The left-hand-side of the expression
1137 Value *RHS, ///< The right-hand-side of the expression
1138 const Twine &NameStr = "" ///< Name of the instruction
1139 ) : CmpInst(makeCmpResultType(LHS->getType()),
1140 Instruction::ICmp, pred, LHS, RHS, NameStr,
1147 /// Constructor with no-insertion semantics
1149 Predicate pred, ///< The predicate to use for the comparison
1150 Value *LHS, ///< The left-hand-side of the expression
1151 Value *RHS, ///< The right-hand-side of the expression
1152 const Twine &NameStr = "" ///< Name of the instruction
1153 ) : CmpInst(makeCmpResultType(LHS->getType()),
1154 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1160 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1161 /// @returns the predicate that would be the result if the operand were
1162 /// regarded as signed.
1163 /// Return the signed version of the predicate
1164 Predicate getSignedPredicate() const {
1165 return getSignedPredicate(getPredicate());
1168 /// This is a static version that you can use without an instruction.
1169 /// Return the signed version of the predicate.
1170 static Predicate getSignedPredicate(Predicate pred);
1172 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1173 /// @returns the predicate that would be the result if the operand were
1174 /// regarded as unsigned.
1175 /// Return the unsigned version of the predicate
1176 Predicate getUnsignedPredicate() const {
1177 return getUnsignedPredicate(getPredicate());
1180 /// This is a static version that you can use without an instruction.
1181 /// Return the unsigned version of the predicate.
1182 static Predicate getUnsignedPredicate(Predicate pred);
1184 /// Return true if this predicate is either EQ or NE. This also
1185 /// tests for commutativity.
1186 static bool isEquality(Predicate P) {
1187 return P == ICMP_EQ || P == ICMP_NE;
1190 /// Return true if this predicate is either EQ or NE. This also
1191 /// tests for commutativity.
1192 bool isEquality() const {
1193 return isEquality(getPredicate());
1196 /// @returns true if the predicate of this ICmpInst is commutative
1197 /// Determine if this relation is commutative.
1198 bool isCommutative() const { return isEquality(); }
1200 /// Return true if the predicate is relational (not EQ or NE).
1202 bool isRelational() const {
1203 return !isEquality();
1206 /// Return true if the predicate is relational (not EQ or NE).
1208 static bool isRelational(Predicate P) {
1209 return !isEquality(P);
1212 /// Exchange the two operands to this instruction in such a way that it does
1213 /// not modify the semantics of the instruction. The predicate value may be
1214 /// changed to retain the same result if the predicate is order dependent
1216 /// Swap operands and adjust predicate.
1217 void swapOperands() {
1218 setPredicate(getSwappedPredicate());
1219 Op<0>().swap(Op<1>());
1222 // Methods for support type inquiry through isa, cast, and dyn_cast:
1223 static inline bool classof(const Instruction *I) {
1224 return I->getOpcode() == Instruction::ICmp;
1226 static inline bool classof(const Value *V) {
1227 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1231 //===----------------------------------------------------------------------===//
1233 //===----------------------------------------------------------------------===//
1235 /// This instruction compares its operands according to the predicate given
1236 /// to the constructor. It only operates on floating point values or packed
1237 /// vectors of floating point values. The operands must be identical types.
1238 /// Represents a floating point comparison operator.
1239 class FCmpInst: public CmpInst {
1241 // Note: Instruction needs to be a friend here to call cloneImpl.
1242 friend class Instruction;
1244 /// Clone an identical FCmpInst
1245 FCmpInst *cloneImpl() const;
1248 /// Constructor with insert-before-instruction semantics.
1250 Instruction *InsertBefore, ///< Where to insert
1251 Predicate pred, ///< The predicate to use for the comparison
1252 Value *LHS, ///< The left-hand-side of the expression
1253 Value *RHS, ///< The right-hand-side of the expression
1254 const Twine &NameStr = "" ///< Name of the instruction
1255 ) : CmpInst(makeCmpResultType(LHS->getType()),
1256 Instruction::FCmp, pred, LHS, RHS, NameStr,
1258 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1259 "Invalid FCmp predicate value");
1260 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1261 "Both operands to FCmp instruction are not of the same type!");
1262 // Check that the operands are the right type
1263 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1264 "Invalid operand types for FCmp instruction");
1267 /// Constructor with insert-at-end semantics.
1269 BasicBlock &InsertAtEnd, ///< Block to insert into.
1270 Predicate pred, ///< The predicate to use for the comparison
1271 Value *LHS, ///< The left-hand-side of the expression
1272 Value *RHS, ///< The right-hand-side of the expression
1273 const Twine &NameStr = "" ///< Name of the instruction
1274 ) : CmpInst(makeCmpResultType(LHS->getType()),
1275 Instruction::FCmp, pred, LHS, RHS, NameStr,
1277 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1278 "Invalid FCmp predicate value");
1279 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1280 "Both operands to FCmp instruction are not of the same type!");
1281 // Check that the operands are the right type
1282 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1283 "Invalid operand types for FCmp instruction");
1286 /// Constructor with no-insertion semantics
1288 Predicate pred, ///< The predicate to use for the comparison
1289 Value *LHS, ///< The left-hand-side of the expression
1290 Value *RHS, ///< The right-hand-side of the expression
1291 const Twine &NameStr = "" ///< Name of the instruction
1292 ) : CmpInst(makeCmpResultType(LHS->getType()),
1293 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1294 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1295 "Invalid FCmp predicate value");
1296 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1297 "Both operands to FCmp instruction are not of the same type!");
1298 // Check that the operands are the right type
1299 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1300 "Invalid operand types for FCmp instruction");
1303 /// @returns true if the predicate of this instruction is EQ or NE.
1304 /// Determine if this is an equality predicate.
1305 static bool isEquality(Predicate Pred) {
1306 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1310 /// @returns true if the predicate of this instruction is EQ or NE.
1311 /// Determine if this is an equality predicate.
1312 bool isEquality() const { return isEquality(getPredicate()); }
1314 /// @returns true if the predicate of this instruction is commutative.
1315 /// Determine if this is a commutative predicate.
1316 bool isCommutative() const {
1317 return isEquality() ||
1318 getPredicate() == FCMP_FALSE ||
1319 getPredicate() == FCMP_TRUE ||
1320 getPredicate() == FCMP_ORD ||
1321 getPredicate() == FCMP_UNO;
1324 /// @returns true if the predicate is relational (not EQ or NE).
1325 /// Determine if this a relational predicate.
1326 bool isRelational() const { return !isEquality(); }
1328 /// Exchange the two operands to this instruction in such a way that it does
1329 /// not modify the semantics of the instruction. The predicate value may be
1330 /// changed to retain the same result if the predicate is order dependent
1332 /// Swap operands and adjust predicate.
1333 void swapOperands() {
1334 setPredicate(getSwappedPredicate());
1335 Op<0>().swap(Op<1>());
1338 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1339 static inline bool classof(const Instruction *I) {
1340 return I->getOpcode() == Instruction::FCmp;
1342 static inline bool classof(const Value *V) {
1343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1347 //===----------------------------------------------------------------------===//
1348 /// This class represents a function call, abstracting a target
1349 /// machine's calling convention. This class uses low bit of the SubClassData
1350 /// field to indicate whether or not this is a tail call. The rest of the bits
1351 /// hold the calling convention of the call.
1353 class CallInst : public Instruction,
1354 public OperandBundleUser<CallInst, User::op_iterator> {
1355 friend class OperandBundleUser<CallInst, User::op_iterator>;
1357 AttributeSet AttributeList; ///< parameter attributes for call
1360 CallInst(const CallInst &CI);
1362 /// Construct a CallInst given a range of arguments.
1363 /// Construct a CallInst from a range of arguments
1364 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1365 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1366 Instruction *InsertBefore);
1368 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1369 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1370 Instruction *InsertBefore)
1371 : CallInst(cast<FunctionType>(
1372 cast<PointerType>(Func->getType())->getElementType()),
1373 Func, Args, Bundles, NameStr, InsertBefore) {}
1375 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1376 Instruction *InsertBefore)
1377 : CallInst(Func, Args, None, NameStr, InsertBefore) {}
1379 /// Construct a CallInst given a range of arguments.
1380 /// Construct a CallInst from a range of arguments
1381 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1382 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1383 BasicBlock *InsertAtEnd);
1385 explicit CallInst(Value *F, const Twine &NameStr,
1386 Instruction *InsertBefore);
1388 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1390 void init(Value *Func, ArrayRef<Value *> Args,
1391 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
1392 init(cast<FunctionType>(
1393 cast<PointerType>(Func->getType())->getElementType()),
1394 Func, Args, Bundles, NameStr);
1396 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1397 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1398 void init(Value *Func, const Twine &NameStr);
1400 bool hasDescriptor() const { return HasDescriptor; }
1403 // Note: Instruction needs to be a friend here to call cloneImpl.
1404 friend class Instruction;
1406 CallInst *cloneImpl() const;
1409 ~CallInst() override;
1411 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1412 ArrayRef<OperandBundleDef> Bundles = None,
1413 const Twine &NameStr = "",
1414 Instruction *InsertBefore = nullptr) {
1415 return Create(cast<FunctionType>(
1416 cast<PointerType>(Func->getType())->getElementType()),
1417 Func, Args, Bundles, NameStr, InsertBefore);
1420 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1421 const Twine &NameStr,
1422 Instruction *InsertBefore = nullptr) {
1423 return Create(cast<FunctionType>(
1424 cast<PointerType>(Func->getType())->getElementType()),
1425 Func, Args, None, NameStr, InsertBefore);
1428 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1429 const Twine &NameStr,
1430 Instruction *InsertBefore = nullptr) {
1431 return new (unsigned(Args.size() + 1))
1432 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1435 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1436 ArrayRef<OperandBundleDef> Bundles = None,
1437 const Twine &NameStr = "",
1438 Instruction *InsertBefore = nullptr) {
1439 const unsigned TotalOps =
1440 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1441 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1443 return new (TotalOps, DescriptorBytes)
1444 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1447 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1448 ArrayRef<OperandBundleDef> Bundles,
1449 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1450 const unsigned TotalOps =
1451 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1452 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1454 return new (TotalOps, DescriptorBytes)
1455 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
1458 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1459 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1460 return new (unsigned(Args.size() + 1))
1461 CallInst(Func, Args, None, NameStr, InsertAtEnd);
1464 static CallInst *Create(Value *F, const Twine &NameStr = "",
1465 Instruction *InsertBefore = nullptr) {
1466 return new(1) CallInst(F, NameStr, InsertBefore);
1469 static CallInst *Create(Value *F, const Twine &NameStr,
1470 BasicBlock *InsertAtEnd) {
1471 return new(1) CallInst(F, NameStr, InsertAtEnd);
1474 /// Create a clone of \p CI with a different set of operand bundles and
1475 /// insert it before \p InsertPt.
1477 /// The returned call instruction is identical \p CI in every way except that
1478 /// the operand bundles for the new instruction are set to the operand bundles
1480 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1481 Instruction *InsertPt = nullptr);
1483 /// Generate the IR for a call to malloc:
1484 /// 1. Compute the malloc call's argument as the specified type's size,
1485 /// possibly multiplied by the array size if the array size is not
1487 /// 2. Call malloc with that argument.
1488 /// 3. Bitcast the result of the malloc call to the specified type.
1489 static Instruction *CreateMalloc(Instruction *InsertBefore,
1490 Type *IntPtrTy, Type *AllocTy,
1491 Value *AllocSize, Value *ArraySize = nullptr,
1492 Function* MallocF = nullptr,
1493 const Twine &Name = "");
1494 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1495 Type *IntPtrTy, Type *AllocTy,
1496 Value *AllocSize, Value *ArraySize = nullptr,
1497 Function* MallocF = nullptr,
1498 const Twine &Name = "");
1499 static Instruction *CreateMalloc(Instruction *InsertBefore,
1500 Type *IntPtrTy, Type *AllocTy,
1501 Value *AllocSize, Value *ArraySize = nullptr,
1502 ArrayRef<OperandBundleDef> Bundles = None,
1503 Function* MallocF = nullptr,
1504 const Twine &Name = "");
1505 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1506 Type *IntPtrTy, Type *AllocTy,
1507 Value *AllocSize, Value *ArraySize = nullptr,
1508 ArrayRef<OperandBundleDef> Bundles = None,
1509 Function* MallocF = nullptr,
1510 const Twine &Name = "");
1511 /// Generate the IR for a call to the builtin free function.
1512 static Instruction *CreateFree(Value *Source,
1513 Instruction *InsertBefore);
1514 static Instruction *CreateFree(Value *Source,
1515 BasicBlock *InsertAtEnd);
1516 static Instruction *CreateFree(Value *Source,
1517 ArrayRef<OperandBundleDef> Bundles,
1518 Instruction *InsertBefore);
1519 static Instruction *CreateFree(Value *Source,
1520 ArrayRef<OperandBundleDef> Bundles,
1521 BasicBlock *InsertAtEnd);
1523 FunctionType *getFunctionType() const { return FTy; }
1525 void mutateFunctionType(FunctionType *FTy) {
1526 mutateType(FTy->getReturnType());
1530 // Note that 'musttail' implies 'tail'.
1531 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2,
1533 TailCallKind getTailCallKind() const {
1534 return TailCallKind(getSubclassDataFromInstruction() & 3);
1537 bool isTailCall() const {
1538 unsigned Kind = getSubclassDataFromInstruction() & 3;
1539 return Kind == TCK_Tail || Kind == TCK_MustTail;
1542 bool isMustTailCall() const {
1543 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1546 bool isNoTailCall() const {
1547 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
1550 void setTailCall(bool isTC = true) {
1551 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1552 unsigned(isTC ? TCK_Tail : TCK_None));
1555 void setTailCallKind(TailCallKind TCK) {
1556 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1560 /// Provide fast operand accessors
1561 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1563 /// Return the number of call arguments.
1565 unsigned getNumArgOperands() const {
1566 return getNumOperands() - getNumTotalBundleOperands() - 1;
1569 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1571 Value *getArgOperand(unsigned i) const {
1572 assert(i < getNumArgOperands() && "Out of bounds!");
1573 return getOperand(i);
1575 void setArgOperand(unsigned i, Value *v) {
1576 assert(i < getNumArgOperands() && "Out of bounds!");
1580 /// Return the iterator pointing to the beginning of the argument list.
1581 op_iterator arg_begin() { return op_begin(); }
1583 /// Return the iterator pointing to the end of the argument list.
1584 op_iterator arg_end() {
1585 // [ call args ], [ operand bundles ], callee
1586 return op_end() - getNumTotalBundleOperands() - 1;
1589 /// Iteration adapter for range-for loops.
1590 iterator_range<op_iterator> arg_operands() {
1591 return make_range(arg_begin(), arg_end());
1594 /// Return the iterator pointing to the beginning of the argument list.
1595 const_op_iterator arg_begin() const { return op_begin(); }
1597 /// Return the iterator pointing to the end of the argument list.
1598 const_op_iterator arg_end() const {
1599 // [ call args ], [ operand bundles ], callee
1600 return op_end() - getNumTotalBundleOperands() - 1;
1603 /// Iteration adapter for range-for loops.
1604 iterator_range<const_op_iterator> arg_operands() const {
1605 return make_range(arg_begin(), arg_end());
1608 /// Wrappers for getting the \c Use of a call argument.
1609 const Use &getArgOperandUse(unsigned i) const {
1610 assert(i < getNumArgOperands() && "Out of bounds!");
1611 return getOperandUse(i);
1613 Use &getArgOperandUse(unsigned i) {
1614 assert(i < getNumArgOperands() && "Out of bounds!");
1615 return getOperandUse(i);
1618 /// If one of the arguments has the 'returned' attribute, return its
1619 /// operand value. Otherwise, return nullptr.
1620 Value *getReturnedArgOperand() const;
1622 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1624 CallingConv::ID getCallingConv() const {
1625 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1627 void setCallingConv(CallingConv::ID CC) {
1628 auto ID = static_cast<unsigned>(CC);
1629 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1630 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1634 /// Return the parameter attributes for this call.
1636 AttributeSet getAttributes() const { return AttributeList; }
1638 /// Set the parameter attributes for this call.
1640 void setAttributes(AttributeSet Attrs) { AttributeList = Attrs; }
1642 /// adds the attribute to the list of attributes.
1643 void addAttribute(unsigned i, Attribute::AttrKind Kind);
1645 /// adds the attribute to the list of attributes.
1646 void addAttribute(unsigned i, Attribute Attr);
1648 /// removes the attribute from the list of attributes.
1649 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
1651 /// removes the attribute from the list of attributes.
1652 void removeAttribute(unsigned i, StringRef Kind);
1654 /// adds the dereferenceable attribute to the list of attributes.
1655 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1657 /// adds the dereferenceable_or_null attribute to the list of
1659 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1661 /// Determine whether this call has the given attribute.
1662 bool hasFnAttr(Attribute::AttrKind Kind) const {
1663 assert(Kind != Attribute::NoBuiltin &&
1664 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1665 return hasFnAttrImpl(Kind);
1668 /// Determine whether this call has the given attribute.
1669 bool hasFnAttr(StringRef Kind) const {
1670 return hasFnAttrImpl(Kind);
1673 /// Determine whether the call or the callee has the given attributes.
1674 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const;
1676 /// Get the attribute of a given kind at a position.
1677 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1678 return getAttributes().getAttribute(i, Kind);
1681 /// Get the attribute of a given kind at a position.
1682 Attribute getAttribute(unsigned i, StringRef Kind) const {
1683 return getAttributes().getAttribute(i, Kind);
1686 /// Return true if the data operand at index \p i has the attribute \p
1689 /// Data operands include call arguments and values used in operand bundles,
1690 /// but does not include the callee operand. This routine dispatches to the
1691 /// underlying AttributeList or the OperandBundleUser as appropriate.
1693 /// The index \p i is interpreted as
1695 /// \p i == Attribute::ReturnIndex -> the return value
1696 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1697 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1698 /// (\p i - 1) in the operand list.
1699 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const;
1701 /// Extract the alignment for a call or parameter (0=unknown).
1702 unsigned getParamAlignment(unsigned i) const {
1703 return AttributeList.getParamAlignment(i);
1706 /// Extract the number of dereferenceable bytes for a call or
1707 /// parameter (0=unknown).
1708 uint64_t getDereferenceableBytes(unsigned i) const {
1709 return AttributeList.getDereferenceableBytes(i);
1712 /// Extract the number of dereferenceable_or_null bytes for a call or
1713 /// parameter (0=unknown).
1714 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1715 return AttributeList.getDereferenceableOrNullBytes(i);
1718 /// @brief Determine if the parameter or return value is marked with NoAlias
1720 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
1721 bool doesNotAlias(unsigned n) const {
1722 return AttributeList.hasAttribute(n, Attribute::NoAlias);
1725 /// Return true if the call should not be treated as a call to a
1727 bool isNoBuiltin() const {
1728 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1729 !hasFnAttrImpl(Attribute::Builtin);
1732 /// Return true if the call should not be inlined.
1733 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1734 void setIsNoInline() {
1735 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1738 /// Return true if the call can return twice
1739 bool canReturnTwice() const {
1740 return hasFnAttr(Attribute::ReturnsTwice);
1742 void setCanReturnTwice() {
1743 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1746 /// Determine if the call does not access memory.
1747 bool doesNotAccessMemory() const {
1748 return hasFnAttr(Attribute::ReadNone);
1750 void setDoesNotAccessMemory() {
1751 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1754 /// Determine if the call does not access or only reads memory.
1755 bool onlyReadsMemory() const {
1756 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1758 void setOnlyReadsMemory() {
1759 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1762 /// Determine if the call does not access or only writes memory.
1763 bool doesNotReadMemory() const {
1764 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1766 void setDoesNotReadMemory() {
1767 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly);
1770 /// @brief Determine if the call can access memmory only using pointers based
1771 /// on its arguments.
1772 bool onlyAccessesArgMemory() const {
1773 return hasFnAttr(Attribute::ArgMemOnly);
1775 void setOnlyAccessesArgMemory() {
1776 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
1779 /// Determine if the call cannot return.
1780 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1781 void setDoesNotReturn() {
1782 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1785 /// Determine if the call cannot unwind.
1786 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1787 void setDoesNotThrow() {
1788 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1791 /// Determine if the call cannot be duplicated.
1792 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1793 void setCannotDuplicate() {
1794 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1797 /// Determine if the call is convergent
1798 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1799 void setConvergent() {
1800 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
1802 void setNotConvergent() {
1803 removeAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
1806 /// Determine if the call returns a structure through first
1807 /// pointer argument.
1808 bool hasStructRetAttr() const {
1809 if (getNumArgOperands() == 0)
1812 // Be friendly and also check the callee.
1813 return paramHasAttr(1, Attribute::StructRet);
1816 /// Determine if any call argument is an aggregate passed by value.
1817 bool hasByValArgument() const {
1818 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1821 /// Return the function called, or null if this is an
1822 /// indirect function invocation.
1824 Function *getCalledFunction() const {
1825 return dyn_cast<Function>(Op<-1>());
1828 /// Get a pointer to the function that is invoked by this
1830 const Value *getCalledValue() const { return Op<-1>(); }
1831 Value *getCalledValue() { return Op<-1>(); }
1833 /// Set the function called.
1834 void setCalledFunction(Value* Fn) {
1836 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1839 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1841 assert(FTy == cast<FunctionType>(
1842 cast<PointerType>(Fn->getType())->getElementType()));
1846 /// Check if this call is an inline asm statement.
1847 bool isInlineAsm() const {
1848 return isa<InlineAsm>(Op<-1>());
1851 // Methods for support type inquiry through isa, cast, and dyn_cast:
1852 static inline bool classof(const Instruction *I) {
1853 return I->getOpcode() == Instruction::Call;
1855 static inline bool classof(const Value *V) {
1856 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1860 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
1861 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, Kind))
1864 // Operand bundles override attributes on the called function, but don't
1865 // override attributes directly present on the call instruction.
1866 if (isFnAttrDisallowedByOpBundle(Kind))
1869 if (const Function *F = getCalledFunction())
1870 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, Kind);
1874 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1875 // method so that subclasses cannot accidentally use it.
1876 void setInstructionSubclassData(unsigned short D) {
1877 Instruction::setInstructionSubclassData(D);
1882 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1885 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1886 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1887 BasicBlock *InsertAtEnd)
1889 cast<FunctionType>(cast<PointerType>(Func->getType())
1890 ->getElementType())->getReturnType(),
1891 Instruction::Call, OperandTraits<CallInst>::op_end(this) -
1892 (Args.size() + CountBundleInputs(Bundles) + 1),
1893 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
1894 init(Func, Args, Bundles, NameStr);
1897 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1898 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1899 Instruction *InsertBefore)
1900 : Instruction(Ty->getReturnType(), Instruction::Call,
1901 OperandTraits<CallInst>::op_end(this) -
1902 (Args.size() + CountBundleInputs(Bundles) + 1),
1903 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1905 init(Ty, Func, Args, Bundles, NameStr);
1908 // Note: if you get compile errors about private methods then
1909 // please update your code to use the high-level operand
1910 // interfaces. See line 943 above.
1911 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1913 //===----------------------------------------------------------------------===//
1915 //===----------------------------------------------------------------------===//
1917 /// This class represents the LLVM 'select' instruction.
1919 class SelectInst : public Instruction {
1920 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1921 Instruction *InsertBefore)
1922 : Instruction(S1->getType(), Instruction::Select,
1923 &Op<0>(), 3, InsertBefore) {
1928 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1929 BasicBlock *InsertAtEnd)
1930 : Instruction(S1->getType(), Instruction::Select,
1931 &Op<0>(), 3, InsertAtEnd) {
1936 void init(Value *C, Value *S1, Value *S2) {
1937 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1944 // Note: Instruction needs to be a friend here to call cloneImpl.
1945 friend class Instruction;
1947 SelectInst *cloneImpl() const;
1950 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1951 const Twine &NameStr = "",
1952 Instruction *InsertBefore = nullptr,
1953 Instruction *MDFrom = nullptr) {
1954 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1956 Sel->copyMetadata(*MDFrom);
1960 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1961 const Twine &NameStr,
1962 BasicBlock *InsertAtEnd) {
1963 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1966 const Value *getCondition() const { return Op<0>(); }
1967 const Value *getTrueValue() const { return Op<1>(); }
1968 const Value *getFalseValue() const { return Op<2>(); }
1969 Value *getCondition() { return Op<0>(); }
1970 Value *getTrueValue() { return Op<1>(); }
1971 Value *getFalseValue() { return Op<2>(); }
1973 void setCondition(Value *V) { Op<0>() = V; }
1974 void setTrueValue(Value *V) { Op<1>() = V; }
1975 void setFalseValue(Value *V) { Op<2>() = V; }
1977 /// Return a string if the specified operands are invalid
1978 /// for a select operation, otherwise return null.
1979 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1981 /// Transparently provide more efficient getOperand methods.
1982 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1984 OtherOps getOpcode() const {
1985 return static_cast<OtherOps>(Instruction::getOpcode());
1988 // Methods for support type inquiry through isa, cast, and dyn_cast:
1989 static inline bool classof(const Instruction *I) {
1990 return I->getOpcode() == Instruction::Select;
1992 static inline bool classof(const Value *V) {
1993 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1998 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
2001 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
2003 //===----------------------------------------------------------------------===//
2005 //===----------------------------------------------------------------------===//
2007 /// This class represents the va_arg llvm instruction, which returns
2008 /// an argument of the specified type given a va_list and increments that list
2010 class VAArgInst : public UnaryInstruction {
2012 // Note: Instruction needs to be a friend here to call cloneImpl.
2013 friend class Instruction;
2015 VAArgInst *cloneImpl() const;
2018 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
2019 Instruction *InsertBefore = nullptr)
2020 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
2024 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
2025 BasicBlock *InsertAtEnd)
2026 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
2030 Value *getPointerOperand() { return getOperand(0); }
2031 const Value *getPointerOperand() const { return getOperand(0); }
2032 static unsigned getPointerOperandIndex() { return 0U; }
2034 // Methods for support type inquiry through isa, cast, and dyn_cast:
2035 static inline bool classof(const Instruction *I) {
2036 return I->getOpcode() == VAArg;
2038 static inline bool classof(const Value *V) {
2039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2043 //===----------------------------------------------------------------------===//
2044 // ExtractElementInst Class
2045 //===----------------------------------------------------------------------===//
2047 /// This instruction extracts a single (scalar)
2048 /// element from a VectorType value
2050 class ExtractElementInst : public Instruction {
2051 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
2052 Instruction *InsertBefore = nullptr);
2053 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
2054 BasicBlock *InsertAtEnd);
2057 // Note: Instruction needs to be a friend here to call cloneImpl.
2058 friend class Instruction;
2060 ExtractElementInst *cloneImpl() const;
2063 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2064 const Twine &NameStr = "",
2065 Instruction *InsertBefore = nullptr) {
2066 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
2069 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2070 const Twine &NameStr,
2071 BasicBlock *InsertAtEnd) {
2072 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
2075 /// Return true if an extractelement instruction can be
2076 /// formed with the specified operands.
2077 static bool isValidOperands(const Value *Vec, const Value *Idx);
2079 Value *getVectorOperand() { return Op<0>(); }
2080 Value *getIndexOperand() { return Op<1>(); }
2081 const Value *getVectorOperand() const { return Op<0>(); }
2082 const Value *getIndexOperand() const { return Op<1>(); }
2084 VectorType *getVectorOperandType() const {
2085 return cast<VectorType>(getVectorOperand()->getType());
2088 /// Transparently provide more efficient getOperand methods.
2089 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2091 // Methods for support type inquiry through isa, cast, and dyn_cast:
2092 static inline bool classof(const Instruction *I) {
2093 return I->getOpcode() == Instruction::ExtractElement;
2095 static inline bool classof(const Value *V) {
2096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2101 struct OperandTraits<ExtractElementInst> :
2102 public FixedNumOperandTraits<ExtractElementInst, 2> {
2105 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
2107 //===----------------------------------------------------------------------===//
2108 // InsertElementInst Class
2109 //===----------------------------------------------------------------------===//
2111 /// This instruction inserts a single (scalar)
2112 /// element into a VectorType value
2114 class InsertElementInst : public Instruction {
2115 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
2116 const Twine &NameStr = "",
2117 Instruction *InsertBefore = nullptr);
2118 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
2119 BasicBlock *InsertAtEnd);
2122 // Note: Instruction needs to be a friend here to call cloneImpl.
2123 friend class Instruction;
2125 InsertElementInst *cloneImpl() const;
2128 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2129 const Twine &NameStr = "",
2130 Instruction *InsertBefore = nullptr) {
2131 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2134 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2135 const Twine &NameStr,
2136 BasicBlock *InsertAtEnd) {
2137 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2140 /// Return true if an insertelement instruction can be
2141 /// formed with the specified operands.
2142 static bool isValidOperands(const Value *Vec, const Value *NewElt,
2145 /// Overload to return most specific vector type.
2147 VectorType *getType() const {
2148 return cast<VectorType>(Instruction::getType());
2151 /// Transparently provide more efficient getOperand methods.
2152 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2154 // Methods for support type inquiry through isa, cast, and dyn_cast:
2155 static inline bool classof(const Instruction *I) {
2156 return I->getOpcode() == Instruction::InsertElement;
2158 static inline bool classof(const Value *V) {
2159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2164 struct OperandTraits<InsertElementInst> :
2165 public FixedNumOperandTraits<InsertElementInst, 3> {
2168 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2170 //===----------------------------------------------------------------------===//
2171 // ShuffleVectorInst Class
2172 //===----------------------------------------------------------------------===//
2174 /// This instruction constructs a fixed permutation of two
2177 class ShuffleVectorInst : public Instruction {
2179 // Note: Instruction needs to be a friend here to call cloneImpl.
2180 friend class Instruction;
2182 ShuffleVectorInst *cloneImpl() const;
2185 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2186 const Twine &NameStr = "",
2187 Instruction *InsertBefor = nullptr);
2188 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2189 const Twine &NameStr, BasicBlock *InsertAtEnd);
2191 // allocate space for exactly three operands
2192 void *operator new(size_t s) {
2193 return User::operator new(s, 3);
2196 /// Return true if a shufflevector instruction can be
2197 /// formed with the specified operands.
2198 static bool isValidOperands(const Value *V1, const Value *V2,
2201 /// Overload to return most specific vector type.
2203 VectorType *getType() const {
2204 return cast<VectorType>(Instruction::getType());
2207 /// Transparently provide more efficient getOperand methods.
2208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2210 Constant *getMask() const {
2211 return cast<Constant>(getOperand(2));
2214 /// Return the shuffle mask value for the specified element of the mask.
2215 /// Return -1 if the element is undef.
2216 static int getMaskValue(Constant *Mask, unsigned Elt);
2218 /// Return the shuffle mask value of this instruction for the given element
2219 /// index. Return -1 if the element is undef.
2220 int getMaskValue(unsigned Elt) const {
2221 return getMaskValue(getMask(), Elt);
2224 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2225 /// elements of the mask are returned as -1.
2226 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
2228 /// Return the mask for this instruction as a vector of integers. Undefined
2229 /// elements of the mask are returned as -1.
2230 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2231 return getShuffleMask(getMask(), Result);
2234 SmallVector<int, 16> getShuffleMask() const {
2235 SmallVector<int, 16> Mask;
2236 getShuffleMask(Mask);
2240 // Methods for support type inquiry through isa, cast, and dyn_cast:
2241 static inline bool classof(const Instruction *I) {
2242 return I->getOpcode() == Instruction::ShuffleVector;
2244 static inline bool classof(const Value *V) {
2245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2250 struct OperandTraits<ShuffleVectorInst> :
2251 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2254 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2256 //===----------------------------------------------------------------------===//
2257 // ExtractValueInst Class
2258 //===----------------------------------------------------------------------===//
2260 /// This instruction extracts a struct member or array
2261 /// element value from an aggregate value.
2263 class ExtractValueInst : public UnaryInstruction {
2264 SmallVector<unsigned, 4> Indices;
2266 ExtractValueInst(const ExtractValueInst &EVI);
2267 /// Constructors - Create a extractvalue instruction with a base aggregate
2268 /// value and a list of indices. The first ctor can optionally insert before
2269 /// an existing instruction, the second appends the new instruction to the
2270 /// specified BasicBlock.
2271 inline ExtractValueInst(Value *Agg,
2272 ArrayRef<unsigned> Idxs,
2273 const Twine &NameStr,
2274 Instruction *InsertBefore);
2275 inline ExtractValueInst(Value *Agg,
2276 ArrayRef<unsigned> Idxs,
2277 const Twine &NameStr, BasicBlock *InsertAtEnd);
2279 // allocate space for exactly one operand
2280 void *operator new(size_t s) { return User::operator new(s, 1); }
2282 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2285 // Note: Instruction needs to be a friend here to call cloneImpl.
2286 friend class Instruction;
2288 ExtractValueInst *cloneImpl() const;
2291 static ExtractValueInst *Create(Value *Agg,
2292 ArrayRef<unsigned> Idxs,
2293 const Twine &NameStr = "",
2294 Instruction *InsertBefore = nullptr) {
2296 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2299 static ExtractValueInst *Create(Value *Agg,
2300 ArrayRef<unsigned> Idxs,
2301 const Twine &NameStr,
2302 BasicBlock *InsertAtEnd) {
2303 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2306 /// Returns the type of the element that would be extracted
2307 /// with an extractvalue instruction with the specified parameters.
2309 /// Null is returned if the indices are invalid for the specified type.
2310 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2312 typedef const unsigned* idx_iterator;
2313 inline idx_iterator idx_begin() const { return Indices.begin(); }
2314 inline idx_iterator idx_end() const { return Indices.end(); }
2315 inline iterator_range<idx_iterator> indices() const {
2316 return make_range(idx_begin(), idx_end());
2319 Value *getAggregateOperand() {
2320 return getOperand(0);
2322 const Value *getAggregateOperand() const {
2323 return getOperand(0);
2325 static unsigned getAggregateOperandIndex() {
2326 return 0U; // get index for modifying correct operand
2329 ArrayRef<unsigned> getIndices() const {
2333 unsigned getNumIndices() const {
2334 return (unsigned)Indices.size();
2337 bool hasIndices() const {
2341 // Methods for support type inquiry through isa, cast, and dyn_cast:
2342 static inline bool classof(const Instruction *I) {
2343 return I->getOpcode() == Instruction::ExtractValue;
2345 static inline bool classof(const Value *V) {
2346 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2350 ExtractValueInst::ExtractValueInst(Value *Agg,
2351 ArrayRef<unsigned> Idxs,
2352 const Twine &NameStr,
2353 Instruction *InsertBefore)
2354 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2355 ExtractValue, Agg, InsertBefore) {
2356 init(Idxs, NameStr);
2359 ExtractValueInst::ExtractValueInst(Value *Agg,
2360 ArrayRef<unsigned> Idxs,
2361 const Twine &NameStr,
2362 BasicBlock *InsertAtEnd)
2363 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2364 ExtractValue, Agg, InsertAtEnd) {
2365 init(Idxs, NameStr);
2368 //===----------------------------------------------------------------------===//
2369 // InsertValueInst Class
2370 //===----------------------------------------------------------------------===//
2372 /// This instruction inserts a struct field of array element
2373 /// value into an aggregate value.
2375 class InsertValueInst : public Instruction {
2376 SmallVector<unsigned, 4> Indices;
2378 InsertValueInst(const InsertValueInst &IVI);
2380 /// Constructors - Create a insertvalue instruction with a base aggregate
2381 /// value, a value to insert, and a list of indices. The first ctor can
2382 /// optionally insert before an existing instruction, the second appends
2383 /// the new instruction to the specified BasicBlock.
2384 inline InsertValueInst(Value *Agg, Value *Val,
2385 ArrayRef<unsigned> Idxs,
2386 const Twine &NameStr,
2387 Instruction *InsertBefore);
2388 inline InsertValueInst(Value *Agg, Value *Val,
2389 ArrayRef<unsigned> Idxs,
2390 const Twine &NameStr, BasicBlock *InsertAtEnd);
2392 /// Constructors - These two constructors are convenience methods because one
2393 /// and two index insertvalue instructions are so common.
2394 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2395 const Twine &NameStr = "",
2396 Instruction *InsertBefore = nullptr);
2397 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2398 BasicBlock *InsertAtEnd);
2400 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2401 const Twine &NameStr);
2404 // Note: Instruction needs to be a friend here to call cloneImpl.
2405 friend class Instruction;
2407 InsertValueInst *cloneImpl() const;
2410 // allocate space for exactly two operands
2411 void *operator new(size_t s) {
2412 return User::operator new(s, 2);
2415 void *operator new(size_t, unsigned) = delete;
2417 static InsertValueInst *Create(Value *Agg, Value *Val,
2418 ArrayRef<unsigned> Idxs,
2419 const Twine &NameStr = "",
2420 Instruction *InsertBefore = nullptr) {
2421 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2424 static InsertValueInst *Create(Value *Agg, Value *Val,
2425 ArrayRef<unsigned> Idxs,
2426 const Twine &NameStr,
2427 BasicBlock *InsertAtEnd) {
2428 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2431 /// Transparently provide more efficient getOperand methods.
2432 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2434 typedef const unsigned* idx_iterator;
2435 inline idx_iterator idx_begin() const { return Indices.begin(); }
2436 inline idx_iterator idx_end() const { return Indices.end(); }
2437 inline iterator_range<idx_iterator> indices() const {
2438 return make_range(idx_begin(), idx_end());
2441 Value *getAggregateOperand() {
2442 return getOperand(0);
2444 const Value *getAggregateOperand() const {
2445 return getOperand(0);
2447 static unsigned getAggregateOperandIndex() {
2448 return 0U; // get index for modifying correct operand
2451 Value *getInsertedValueOperand() {
2452 return getOperand(1);
2454 const Value *getInsertedValueOperand() const {
2455 return getOperand(1);
2457 static unsigned getInsertedValueOperandIndex() {
2458 return 1U; // get index for modifying correct operand
2461 ArrayRef<unsigned> getIndices() const {
2465 unsigned getNumIndices() const {
2466 return (unsigned)Indices.size();
2469 bool hasIndices() const {
2473 // Methods for support type inquiry through isa, cast, and dyn_cast:
2474 static inline bool classof(const Instruction *I) {
2475 return I->getOpcode() == Instruction::InsertValue;
2477 static inline bool classof(const Value *V) {
2478 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2483 struct OperandTraits<InsertValueInst> :
2484 public FixedNumOperandTraits<InsertValueInst, 2> {
2487 InsertValueInst::InsertValueInst(Value *Agg,
2489 ArrayRef<unsigned> Idxs,
2490 const Twine &NameStr,
2491 Instruction *InsertBefore)
2492 : Instruction(Agg->getType(), InsertValue,
2493 OperandTraits<InsertValueInst>::op_begin(this),
2495 init(Agg, Val, Idxs, NameStr);
2498 InsertValueInst::InsertValueInst(Value *Agg,
2500 ArrayRef<unsigned> Idxs,
2501 const Twine &NameStr,
2502 BasicBlock *InsertAtEnd)
2503 : Instruction(Agg->getType(), InsertValue,
2504 OperandTraits<InsertValueInst>::op_begin(this),
2506 init(Agg, Val, Idxs, NameStr);
2509 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2511 //===----------------------------------------------------------------------===//
2513 //===----------------------------------------------------------------------===//
2515 // PHINode - The PHINode class is used to represent the magical mystical PHI
2516 // node, that can not exist in nature, but can be synthesized in a computer
2517 // scientist's overactive imagination.
2519 class PHINode : public Instruction {
2520 /// The number of operands actually allocated. NumOperands is
2521 /// the number actually in use.
2522 unsigned ReservedSpace;
2524 PHINode(const PHINode &PN);
2525 // allocate space for exactly zero operands
2527 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2528 const Twine &NameStr = "",
2529 Instruction *InsertBefore = nullptr)
2530 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2531 ReservedSpace(NumReservedValues) {
2533 allocHungoffUses(ReservedSpace);
2536 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2537 BasicBlock *InsertAtEnd)
2538 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2539 ReservedSpace(NumReservedValues) {
2541 allocHungoffUses(ReservedSpace);
2544 void *operator new(size_t s) {
2545 return User::operator new(s);
2548 void anchor() override;
2551 // Note: Instruction needs to be a friend here to call cloneImpl.
2552 friend class Instruction;
2554 PHINode *cloneImpl() const;
2556 // allocHungoffUses - this is more complicated than the generic
2557 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2558 // values and pointers to the incoming blocks, all in one allocation.
2559 void allocHungoffUses(unsigned N) {
2560 User::allocHungoffUses(N, /* IsPhi */ true);
2564 void *operator new(size_t, unsigned) = delete;
2566 /// Constructors - NumReservedValues is a hint for the number of incoming
2567 /// edges that this phi node will have (use 0 if you really have no idea).
2568 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2569 const Twine &NameStr = "",
2570 Instruction *InsertBefore = nullptr) {
2571 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2574 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2575 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2576 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2579 /// Provide fast operand accessors
2580 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2582 // Block iterator interface. This provides access to the list of incoming
2583 // basic blocks, which parallels the list of incoming values.
2585 typedef BasicBlock **block_iterator;
2586 typedef BasicBlock * const *const_block_iterator;
2588 block_iterator block_begin() {
2590 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2591 return reinterpret_cast<block_iterator>(ref + 1);
2594 const_block_iterator block_begin() const {
2595 const Use::UserRef *ref =
2596 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2597 return reinterpret_cast<const_block_iterator>(ref + 1);
2600 block_iterator block_end() {
2601 return block_begin() + getNumOperands();
2604 const_block_iterator block_end() const {
2605 return block_begin() + getNumOperands();
2608 iterator_range<block_iterator> blocks() {
2609 return make_range(block_begin(), block_end());
2612 iterator_range<const_block_iterator> blocks() const {
2613 return make_range(block_begin(), block_end());
2616 op_range incoming_values() { return operands(); }
2618 const_op_range incoming_values() const { return operands(); }
2620 /// Return the number of incoming edges
2622 unsigned getNumIncomingValues() const { return getNumOperands(); }
2624 /// Return incoming value number x
2626 Value *getIncomingValue(unsigned i) const {
2627 return getOperand(i);
2629 void setIncomingValue(unsigned i, Value *V) {
2630 assert(V && "PHI node got a null value!");
2631 assert(getType() == V->getType() &&
2632 "All operands to PHI node must be the same type as the PHI node!");
2635 static unsigned getOperandNumForIncomingValue(unsigned i) {
2638 static unsigned getIncomingValueNumForOperand(unsigned i) {
2642 /// Return incoming basic block number @p i.
2644 BasicBlock *getIncomingBlock(unsigned i) const {
2645 return block_begin()[i];
2648 /// Return incoming basic block corresponding
2649 /// to an operand of the PHI.
2651 BasicBlock *getIncomingBlock(const Use &U) const {
2652 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2653 return getIncomingBlock(unsigned(&U - op_begin()));
2656 /// Return incoming basic block corresponding
2657 /// to value use iterator.
2659 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2660 return getIncomingBlock(I.getUse());
2663 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2664 assert(BB && "PHI node got a null basic block!");
2665 block_begin()[i] = BB;
2668 /// Add an incoming value to the end of the PHI list
2670 void addIncoming(Value *V, BasicBlock *BB) {
2671 if (getNumOperands() == ReservedSpace)
2672 growOperands(); // Get more space!
2673 // Initialize some new operands.
2674 setNumHungOffUseOperands(getNumOperands() + 1);
2675 setIncomingValue(getNumOperands() - 1, V);
2676 setIncomingBlock(getNumOperands() - 1, BB);
2679 /// Remove an incoming value. This is useful if a
2680 /// predecessor basic block is deleted. The value removed is returned.
2682 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2683 /// is true), the PHI node is destroyed and any uses of it are replaced with
2684 /// dummy values. The only time there should be zero incoming values to a PHI
2685 /// node is when the block is dead, so this strategy is sound.
2687 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2689 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2690 int Idx = getBasicBlockIndex(BB);
2691 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2692 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2695 /// Return the first index of the specified basic
2696 /// block in the value list for this PHI. Returns -1 if no instance.
2698 int getBasicBlockIndex(const BasicBlock *BB) const {
2699 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2700 if (block_begin()[i] == BB)
2705 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2706 int Idx = getBasicBlockIndex(BB);
2707 assert(Idx >= 0 && "Invalid basic block argument!");
2708 return getIncomingValue(Idx);
2711 /// If the specified PHI node always merges together the
2712 /// same value, return the value, otherwise return null.
2713 Value *hasConstantValue() const;
2715 /// Whether the specified PHI node always merges
2716 /// together the same value, assuming undefs are equal to a unique
2717 /// non-undef value.
2718 bool hasConstantOrUndefValue() const;
2720 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2721 static inline bool classof(const Instruction *I) {
2722 return I->getOpcode() == Instruction::PHI;
2724 static inline bool classof(const Value *V) {
2725 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2729 void growOperands();
2733 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2736 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2738 //===----------------------------------------------------------------------===//
2739 // LandingPadInst Class
2740 //===----------------------------------------------------------------------===//
2742 //===---------------------------------------------------------------------------
2743 /// The landingpad instruction holds all of the information
2744 /// necessary to generate correct exception handling. The landingpad instruction
2745 /// cannot be moved from the top of a landing pad block, which itself is
2746 /// accessible only from the 'unwind' edge of an invoke. This uses the
2747 /// SubclassData field in Value to store whether or not the landingpad is a
2750 class LandingPadInst : public Instruction {
2751 /// The number of operands actually allocated. NumOperands is
2752 /// the number actually in use.
2753 unsigned ReservedSpace;
2755 LandingPadInst(const LandingPadInst &LP);
2758 enum ClauseType { Catch, Filter };
2761 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2762 const Twine &NameStr, Instruction *InsertBefore);
2763 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2764 const Twine &NameStr, BasicBlock *InsertAtEnd);
2766 // Allocate space for exactly zero operands.
2767 void *operator new(size_t s) {
2768 return User::operator new(s);
2771 void growOperands(unsigned Size);
2772 void init(unsigned NumReservedValues, const Twine &NameStr);
2775 // Note: Instruction needs to be a friend here to call cloneImpl.
2776 friend class Instruction;
2778 LandingPadInst *cloneImpl() const;
2781 void *operator new(size_t, unsigned) = delete;
2783 /// Constructors - NumReservedClauses is a hint for the number of incoming
2784 /// clauses that this landingpad will have (use 0 if you really have no idea).
2785 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2786 const Twine &NameStr = "",
2787 Instruction *InsertBefore = nullptr);
2788 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2789 const Twine &NameStr, BasicBlock *InsertAtEnd);
2791 /// Provide fast operand accessors
2792 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2794 /// Return 'true' if this landingpad instruction is a
2795 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2796 /// doesn't catch the exception.
2797 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2799 /// Indicate that this landingpad instruction is a cleanup.
2800 void setCleanup(bool V) {
2801 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2805 /// Add a catch or filter clause to the landing pad.
2806 void addClause(Constant *ClauseVal);
2808 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2809 /// determine what type of clause this is.
2810 Constant *getClause(unsigned Idx) const {
2811 return cast<Constant>(getOperandList()[Idx]);
2814 /// Return 'true' if the clause and index Idx is a catch clause.
2815 bool isCatch(unsigned Idx) const {
2816 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2819 /// Return 'true' if the clause and index Idx is a filter clause.
2820 bool isFilter(unsigned Idx) const {
2821 return isa<ArrayType>(getOperandList()[Idx]->getType());
2824 /// Get the number of clauses for this landing pad.
2825 unsigned getNumClauses() const { return getNumOperands(); }
2827 /// Grow the size of the operand list to accommodate the new
2828 /// number of clauses.
2829 void reserveClauses(unsigned Size) { growOperands(Size); }
2831 // Methods for support type inquiry through isa, cast, and dyn_cast:
2832 static inline bool classof(const Instruction *I) {
2833 return I->getOpcode() == Instruction::LandingPad;
2835 static inline bool classof(const Value *V) {
2836 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2841 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2844 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2846 //===----------------------------------------------------------------------===//
2848 //===----------------------------------------------------------------------===//
2850 //===---------------------------------------------------------------------------
2851 /// Return a value (possibly void), from a function. Execution
2852 /// does not continue in this function any longer.
2854 class ReturnInst : public TerminatorInst {
2855 ReturnInst(const ReturnInst &RI);
2858 // ReturnInst constructors:
2859 // ReturnInst() - 'ret void' instruction
2860 // ReturnInst( null) - 'ret void' instruction
2861 // ReturnInst(Value* X) - 'ret X' instruction
2862 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2863 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2864 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2865 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2867 // NOTE: If the Value* passed is of type void then the constructor behaves as
2868 // if it was passed NULL.
2869 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2870 Instruction *InsertBefore = nullptr);
2871 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2872 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2875 // Note: Instruction needs to be a friend here to call cloneImpl.
2876 friend class Instruction;
2878 ReturnInst *cloneImpl() const;
2881 ~ReturnInst() override;
2883 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2884 Instruction *InsertBefore = nullptr) {
2885 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2888 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2889 BasicBlock *InsertAtEnd) {
2890 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2893 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2894 return new(0) ReturnInst(C, InsertAtEnd);
2897 /// Provide fast operand accessors
2898 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2900 /// Convenience accessor. Returns null if there is no return value.
2901 Value *getReturnValue() const {
2902 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2905 unsigned getNumSuccessors() const { return 0; }
2907 // Methods for support type inquiry through isa, cast, and dyn_cast:
2908 static inline bool classof(const Instruction *I) {
2909 return (I->getOpcode() == Instruction::Ret);
2911 static inline bool classof(const Value *V) {
2912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2916 BasicBlock *getSuccessorV(unsigned idx) const override;
2917 unsigned getNumSuccessorsV() const override;
2918 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2922 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2925 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2927 //===----------------------------------------------------------------------===//
2929 //===----------------------------------------------------------------------===//
2931 //===---------------------------------------------------------------------------
2932 /// Conditional or Unconditional Branch instruction.
2934 class BranchInst : public TerminatorInst {
2935 /// Ops list - Branches are strange. The operands are ordered:
2936 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2937 /// they don't have to check for cond/uncond branchness. These are mostly
2938 /// accessed relative from op_end().
2939 BranchInst(const BranchInst &BI);
2940 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2941 // BranchInst(BB *B) - 'br B'
2942 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2943 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2944 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2945 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2946 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2947 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2948 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2949 Instruction *InsertBefore = nullptr);
2950 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2951 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2952 BasicBlock *InsertAtEnd);
2957 // Note: Instruction needs to be a friend here to call cloneImpl.
2958 friend class Instruction;
2960 BranchInst *cloneImpl() const;
2963 static BranchInst *Create(BasicBlock *IfTrue,
2964 Instruction *InsertBefore = nullptr) {
2965 return new(1) BranchInst(IfTrue, InsertBefore);
2968 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2969 Value *Cond, Instruction *InsertBefore = nullptr) {
2970 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2973 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2974 return new(1) BranchInst(IfTrue, InsertAtEnd);
2977 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2978 Value *Cond, BasicBlock *InsertAtEnd) {
2979 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2982 /// Transparently provide more efficient getOperand methods.
2983 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2985 bool isUnconditional() const { return getNumOperands() == 1; }
2986 bool isConditional() const { return getNumOperands() == 3; }
2988 Value *getCondition() const {
2989 assert(isConditional() && "Cannot get condition of an uncond branch!");
2993 void setCondition(Value *V) {
2994 assert(isConditional() && "Cannot set condition of unconditional branch!");
2998 unsigned getNumSuccessors() const { return 1+isConditional(); }
3000 BasicBlock *getSuccessor(unsigned i) const {
3001 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
3002 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3005 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3006 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
3007 *(&Op<-1>() - idx) = NewSucc;
3010 /// Swap the successors of this branch instruction.
3012 /// Swaps the successors of the branch instruction. This also swaps any
3013 /// branch weight metadata associated with the instruction so that it
3014 /// continues to map correctly to each operand.
3015 void swapSuccessors();
3017 // Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const Instruction *I) {
3019 return (I->getOpcode() == Instruction::Br);
3021 static inline bool classof(const Value *V) {
3022 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3026 BasicBlock *getSuccessorV(unsigned idx) const override;
3027 unsigned getNumSuccessorsV() const override;
3028 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3032 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3035 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
3037 //===----------------------------------------------------------------------===//
3039 //===----------------------------------------------------------------------===//
3041 //===---------------------------------------------------------------------------
3044 class SwitchInst : public TerminatorInst {
3045 unsigned ReservedSpace;
3047 // Operand[0] = Value to switch on
3048 // Operand[1] = Default basic block destination
3049 // Operand[2n ] = Value to match
3050 // Operand[2n+1] = BasicBlock to go to on match
3051 SwitchInst(const SwitchInst &SI);
3053 /// Create a new switch instruction, specifying a value to switch on and a
3054 /// default destination. The number of additional cases can be specified here
3055 /// to make memory allocation more efficient. This constructor can also
3056 /// auto-insert before another instruction.
3057 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3058 Instruction *InsertBefore);
3060 /// Create a new switch instruction, specifying a value to switch on and a
3061 /// default destination. The number of additional cases can be specified here
3062 /// to make memory allocation more efficient. This constructor also
3063 /// auto-inserts at the end of the specified BasicBlock.
3064 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3065 BasicBlock *InsertAtEnd);
3067 // allocate space for exactly zero operands
3068 void *operator new(size_t s) {
3069 return User::operator new(s);
3072 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3073 void growOperands();
3076 // Note: Instruction needs to be a friend here to call cloneImpl.
3077 friend class Instruction;
3079 SwitchInst *cloneImpl() const;
3082 void *operator new(size_t, unsigned) = delete;
3085 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3087 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
3088 class CaseIteratorT {
3094 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
3096 /// Initializes case iterator for given SwitchInst and for given
3098 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
3103 /// Initializes case iterator for given SwitchInst and for given
3104 /// TerminatorInst's successor index.
3105 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
3106 assert(SuccessorIndex < SI->getNumSuccessors() &&
3107 "Successor index # out of range!");
3108 return SuccessorIndex != 0 ?
3109 Self(SI, SuccessorIndex - 1) :
3110 Self(SI, DefaultPseudoIndex);
3113 /// Resolves case value for current case.
3114 ConstantIntTy *getCaseValue() {
3115 assert(Index < SI->getNumCases() && "Index out the number of cases.");
3116 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
3119 /// Resolves successor for current case.
3120 BasicBlockTy *getCaseSuccessor() {
3121 assert((Index < SI->getNumCases() ||
3122 Index == DefaultPseudoIndex) &&
3123 "Index out the number of cases.");
3124 return SI->getSuccessor(getSuccessorIndex());
3127 /// Returns number of current case.
3128 unsigned getCaseIndex() const { return Index; }
3130 /// Returns TerminatorInst's successor index for current case successor.
3131 unsigned getSuccessorIndex() const {
3132 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
3133 "Index out the number of cases.");
3134 return Index != DefaultPseudoIndex ? Index + 1 : 0;
3138 // Check index correctness after increment.
3139 // Note: Index == getNumCases() means end().
3140 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
3144 Self operator++(int) {
3150 // Check index correctness after decrement.
3151 // Note: Index == getNumCases() means end().
3152 // Also allow "-1" iterator here. That will became valid after ++.
3153 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
3154 "Index out the number of cases.");
3158 Self operator--(int) {
3163 bool operator==(const Self& RHS) const {
3164 assert(RHS.SI == SI && "Incompatible operators.");
3165 return RHS.Index == Index;
3167 bool operator!=(const Self& RHS) const {
3168 assert(RHS.SI == SI && "Incompatible operators.");
3169 return RHS.Index != Index;
3176 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
3179 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
3180 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
3183 CaseIt(const ParentTy &Src) : ParentTy(Src) {}
3184 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
3186 /// Sets the new value for current case.
3187 void setValue(ConstantInt *V) {
3188 assert(Index < SI->getNumCases() && "Index out the number of cases.");
3189 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3192 /// Sets the new successor for current case.
3193 void setSuccessor(BasicBlock *S) {
3194 SI->setSuccessor(getSuccessorIndex(), S);
3198 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3200 Instruction *InsertBefore = nullptr) {
3201 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3204 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3205 unsigned NumCases, BasicBlock *InsertAtEnd) {
3206 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3209 /// Provide fast operand accessors
3210 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3212 // Accessor Methods for Switch stmt
3213 Value *getCondition() const { return getOperand(0); }
3214 void setCondition(Value *V) { setOperand(0, V); }
3216 BasicBlock *getDefaultDest() const {
3217 return cast<BasicBlock>(getOperand(1));
3220 void setDefaultDest(BasicBlock *DefaultCase) {
3221 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3224 /// Return the number of 'cases' in this switch instruction, excluding the
3226 unsigned getNumCases() const {
3227 return getNumOperands()/2 - 1;
3230 /// Returns a read/write iterator that points to the first case in the
3232 CaseIt case_begin() {
3233 return CaseIt(this, 0);
3236 /// Returns a read-only iterator that points to the first case in the
3238 ConstCaseIt case_begin() const {
3239 return ConstCaseIt(this, 0);
3242 /// Returns a read/write iterator that points one past the last in the
3245 return CaseIt(this, getNumCases());
3248 /// Returns a read-only iterator that points one past the last in the
3250 ConstCaseIt case_end() const {
3251 return ConstCaseIt(this, getNumCases());
3254 /// Iteration adapter for range-for loops.
3255 iterator_range<CaseIt> cases() {
3256 return make_range(case_begin(), case_end());
3259 /// Constant iteration adapter for range-for loops.
3260 iterator_range<ConstCaseIt> cases() const {
3261 return make_range(case_begin(), case_end());
3264 /// Returns an iterator that points to the default case.
3265 /// Note: this iterator allows to resolve successor only. Attempt
3266 /// to resolve case value causes an assertion.
3267 /// Also note, that increment and decrement also causes an assertion and
3268 /// makes iterator invalid.
3269 CaseIt case_default() {
3270 return CaseIt(this, DefaultPseudoIndex);
3272 ConstCaseIt case_default() const {
3273 return ConstCaseIt(this, DefaultPseudoIndex);
3276 /// Search all of the case values for the specified constant. If it is
3277 /// explicitly handled, return the case iterator of it, otherwise return
3278 /// default case iterator to indicate that it is handled by the default
3280 CaseIt findCaseValue(const ConstantInt *C) {
3281 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
3282 if (i.getCaseValue() == C)
3284 return case_default();
3286 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3287 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
3288 if (i.getCaseValue() == C)
3290 return case_default();
3293 /// Finds the unique case value for a given successor. Returns null if the
3294 /// successor is not found, not unique, or is the default case.
3295 ConstantInt *findCaseDest(BasicBlock *BB) {
3296 if (BB == getDefaultDest()) return nullptr;
3298 ConstantInt *CI = nullptr;
3299 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
3300 if (i.getCaseSuccessor() == BB) {
3301 if (CI) return nullptr; // Multiple cases lead to BB.
3302 else CI = i.getCaseValue();
3308 /// Add an entry to the switch instruction.
3310 /// This action invalidates case_end(). Old case_end() iterator will
3311 /// point to the added case.
3312 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3314 /// This method removes the specified case and its successor from the switch
3315 /// instruction. Note that this operation may reorder the remaining cases at
3316 /// index idx and above.
3318 /// This action invalidates iterators for all cases following the one removed,
3319 /// including the case_end() iterator.
3320 void removeCase(CaseIt i);
3322 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3323 BasicBlock *getSuccessor(unsigned idx) const {
3324 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3325 return cast<BasicBlock>(getOperand(idx*2+1));
3327 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3328 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3329 setOperand(idx * 2 + 1, NewSucc);
3332 // Methods for support type inquiry through isa, cast, and dyn_cast:
3333 static inline bool classof(const Instruction *I) {
3334 return I->getOpcode() == Instruction::Switch;
3336 static inline bool classof(const Value *V) {
3337 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3341 BasicBlock *getSuccessorV(unsigned idx) const override;
3342 unsigned getNumSuccessorsV() const override;
3343 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3347 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3350 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3352 //===----------------------------------------------------------------------===//
3353 // IndirectBrInst Class
3354 //===----------------------------------------------------------------------===//
3356 //===---------------------------------------------------------------------------
3357 /// Indirect Branch Instruction.
3359 class IndirectBrInst : public TerminatorInst {
3360 unsigned ReservedSpace;
3362 // Operand[0] = Address to jump to
3363 // Operand[n+1] = n-th destination
3364 IndirectBrInst(const IndirectBrInst &IBI);
3366 /// Create a new indirectbr instruction, specifying an
3367 /// Address to jump to. The number of expected destinations can be specified
3368 /// here to make memory allocation more efficient. This constructor can also
3369 /// autoinsert before another instruction.
3370 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3372 /// Create a new indirectbr instruction, specifying an
3373 /// Address to jump to. The number of expected destinations can be specified
3374 /// here to make memory allocation more efficient. This constructor also
3375 /// autoinserts at the end of the specified BasicBlock.
3376 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3378 // allocate space for exactly zero operands
3379 void *operator new(size_t s) {
3380 return User::operator new(s);
3383 void init(Value *Address, unsigned NumDests);
3384 void growOperands();
3387 // Note: Instruction needs to be a friend here to call cloneImpl.
3388 friend class Instruction;
3390 IndirectBrInst *cloneImpl() const;
3393 void *operator new(size_t, unsigned) = delete;
3395 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3396 Instruction *InsertBefore = nullptr) {
3397 return new IndirectBrInst(Address, NumDests, InsertBefore);
3400 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3401 BasicBlock *InsertAtEnd) {
3402 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3405 /// Provide fast operand accessors.
3406 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3408 // Accessor Methods for IndirectBrInst instruction.
3409 Value *getAddress() { return getOperand(0); }
3410 const Value *getAddress() const { return getOperand(0); }
3411 void setAddress(Value *V) { setOperand(0, V); }
3413 /// return the number of possible destinations in this
3414 /// indirectbr instruction.
3415 unsigned getNumDestinations() const { return getNumOperands()-1; }
3417 /// Return the specified destination.
3418 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3419 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3421 /// Add a destination.
3423 void addDestination(BasicBlock *Dest);
3425 /// This method removes the specified successor from the
3426 /// indirectbr instruction.
3427 void removeDestination(unsigned i);
3429 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3430 BasicBlock *getSuccessor(unsigned i) const {
3431 return cast<BasicBlock>(getOperand(i+1));
3433 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3434 setOperand(i + 1, NewSucc);
3437 // Methods for support type inquiry through isa, cast, and dyn_cast:
3438 static inline bool classof(const Instruction *I) {
3439 return I->getOpcode() == Instruction::IndirectBr;
3441 static inline bool classof(const Value *V) {
3442 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3446 BasicBlock *getSuccessorV(unsigned idx) const override;
3447 unsigned getNumSuccessorsV() const override;
3448 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3452 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3455 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3457 //===----------------------------------------------------------------------===//
3459 //===----------------------------------------------------------------------===//
3461 /// Invoke instruction. The SubclassData field is used to hold the
3462 /// calling convention of the call.
3464 class InvokeInst : public TerminatorInst,
3465 public OperandBundleUser<InvokeInst, User::op_iterator> {
3466 friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3468 AttributeSet AttributeList;
3471 InvokeInst(const InvokeInst &BI);
3473 /// Construct an InvokeInst given a range of arguments.
3475 /// Construct an InvokeInst from a range of arguments
3476 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3477 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3478 unsigned Values, const Twine &NameStr,
3479 Instruction *InsertBefore)
3480 : InvokeInst(cast<FunctionType>(
3481 cast<PointerType>(Func->getType())->getElementType()),
3482 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3485 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3486 BasicBlock *IfException, ArrayRef<Value *> Args,
3487 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3488 const Twine &NameStr, Instruction *InsertBefore);
3489 /// Construct an InvokeInst given a range of arguments.
3491 /// Construct an InvokeInst from a range of arguments
3492 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3493 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3494 unsigned Values, const Twine &NameStr,
3495 BasicBlock *InsertAtEnd);
3497 bool hasDescriptor() const { return HasDescriptor; }
3499 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3500 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3501 const Twine &NameStr) {
3502 init(cast<FunctionType>(
3503 cast<PointerType>(Func->getType())->getElementType()),
3504 Func, IfNormal, IfException, Args, Bundles, NameStr);
3507 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3508 BasicBlock *IfException, ArrayRef<Value *> Args,
3509 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3512 // Note: Instruction needs to be a friend here to call cloneImpl.
3513 friend class Instruction;
3515 InvokeInst *cloneImpl() const;
3518 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3519 BasicBlock *IfException, ArrayRef<Value *> Args,
3520 const Twine &NameStr,
3521 Instruction *InsertBefore = nullptr) {
3522 return Create(cast<FunctionType>(
3523 cast<PointerType>(Func->getType())->getElementType()),
3524 Func, IfNormal, IfException, Args, None, NameStr,
3528 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3529 BasicBlock *IfException, ArrayRef<Value *> Args,
3530 ArrayRef<OperandBundleDef> Bundles = None,
3531 const Twine &NameStr = "",
3532 Instruction *InsertBefore = nullptr) {
3533 return Create(cast<FunctionType>(
3534 cast<PointerType>(Func->getType())->getElementType()),
3535 Func, IfNormal, IfException, Args, Bundles, NameStr,
3539 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3540 BasicBlock *IfException, ArrayRef<Value *> Args,
3541 const Twine &NameStr,
3542 Instruction *InsertBefore = nullptr) {
3543 unsigned Values = unsigned(Args.size()) + 3;
3544 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3545 Values, NameStr, InsertBefore);
3548 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3549 BasicBlock *IfException, ArrayRef<Value *> Args,
3550 ArrayRef<OperandBundleDef> Bundles = None,
3551 const Twine &NameStr = "",
3552 Instruction *InsertBefore = nullptr) {
3553 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3554 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3556 return new (Values, DescriptorBytes)
3557 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3558 NameStr, InsertBefore);
3561 static InvokeInst *Create(Value *Func,
3562 BasicBlock *IfNormal, BasicBlock *IfException,
3563 ArrayRef<Value *> Args, const Twine &NameStr,
3564 BasicBlock *InsertAtEnd) {
3565 unsigned Values = unsigned(Args.size()) + 3;
3566 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3567 Values, NameStr, InsertAtEnd);
3569 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3570 BasicBlock *IfException, ArrayRef<Value *> Args,
3571 ArrayRef<OperandBundleDef> Bundles,
3572 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3573 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3574 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3576 return new (Values, DescriptorBytes)
3577 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3581 /// Create a clone of \p II with a different set of operand bundles and
3582 /// insert it before \p InsertPt.
3584 /// The returned invoke instruction is identical to \p II in every way except
3585 /// that the operand bundles for the new instruction are set to the operand
3586 /// bundles in \p Bundles.
3587 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3588 Instruction *InsertPt = nullptr);
3590 /// Provide fast operand accessors
3591 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3593 FunctionType *getFunctionType() const { return FTy; }
3595 void mutateFunctionType(FunctionType *FTy) {
3596 mutateType(FTy->getReturnType());
3600 /// Return the number of invoke arguments.
3602 unsigned getNumArgOperands() const {
3603 return getNumOperands() - getNumTotalBundleOperands() - 3;
3606 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3608 Value *getArgOperand(unsigned i) const {
3609 assert(i < getNumArgOperands() && "Out of bounds!");
3610 return getOperand(i);
3612 void setArgOperand(unsigned i, Value *v) {
3613 assert(i < getNumArgOperands() && "Out of bounds!");
3617 /// Return the iterator pointing to the beginning of the argument list.
3618 op_iterator arg_begin() { return op_begin(); }
3620 /// Return the iterator pointing to the end of the argument list.
3621 op_iterator arg_end() {
3622 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee
3623 return op_end() - getNumTotalBundleOperands() - 3;
3626 /// Iteration adapter for range-for loops.
3627 iterator_range<op_iterator> arg_operands() {
3628 return make_range(arg_begin(), arg_end());
3631 /// Return the iterator pointing to the beginning of the argument list.
3632 const_op_iterator arg_begin() const { return op_begin(); }
3634 /// Return the iterator pointing to the end of the argument list.
3635 const_op_iterator arg_end() const {
3636 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee
3637 return op_end() - getNumTotalBundleOperands() - 3;
3640 /// Iteration adapter for range-for loops.
3641 iterator_range<const_op_iterator> arg_operands() const {
3642 return make_range(arg_begin(), arg_end());
3645 /// Wrappers for getting the \c Use of a invoke argument.
3646 const Use &getArgOperandUse(unsigned i) const {
3647 assert(i < getNumArgOperands() && "Out of bounds!");
3648 return getOperandUse(i);
3650 Use &getArgOperandUse(unsigned i) {
3651 assert(i < getNumArgOperands() && "Out of bounds!");
3652 return getOperandUse(i);
3655 /// If one of the arguments has the 'returned' attribute, return its
3656 /// operand value. Otherwise, return nullptr.
3657 Value *getReturnedArgOperand() const;
3659 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3661 CallingConv::ID getCallingConv() const {
3662 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3664 void setCallingConv(CallingConv::ID CC) {
3665 auto ID = static_cast<unsigned>(CC);
3666 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
3667 setInstructionSubclassData(ID);
3670 /// Return the parameter attributes for this invoke.
3672 AttributeSet getAttributes() const { return AttributeList; }
3674 /// Set the parameter attributes for this invoke.
3676 void setAttributes(AttributeSet Attrs) { AttributeList = Attrs; }
3678 /// adds the attribute to the list of attributes.
3679 void addAttribute(unsigned i, Attribute::AttrKind Kind);
3681 /// adds the attribute to the list of attributes.
3682 void addAttribute(unsigned i, Attribute Attr);
3684 /// removes the attribute from the list of attributes.
3685 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
3687 /// removes the attribute from the list of attributes.
3688 void removeAttribute(unsigned i, StringRef Kind);
3690 /// adds the dereferenceable attribute to the list of attributes.
3691 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3693 /// adds the dereferenceable_or_null attribute to the list of
3695 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
3697 /// Determine whether this call has the given attribute.
3698 bool hasFnAttr(Attribute::AttrKind Kind) const {
3699 assert(Kind != Attribute::NoBuiltin &&
3700 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3701 return hasFnAttrImpl(Kind);
3704 /// Determine whether this call has the given attribute.
3705 bool hasFnAttr(StringRef Kind) const {
3706 return hasFnAttrImpl(Kind);
3709 /// Determine whether the call or the callee has the given attributes.
3710 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const;
3712 /// Get the attribute of a given kind at a position.
3713 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
3714 return getAttributes().getAttribute(i, Kind);
3717 /// Get the attribute of a given kind at a position.
3718 Attribute getAttribute(unsigned i, StringRef Kind) const {
3719 return getAttributes().getAttribute(i, Kind);
3722 /// Return true if the data operand at index \p i has the attribute \p
3725 /// Data operands include invoke arguments and values used in operand bundles,
3726 /// but does not include the invokee operand, or the two successor blocks.
3727 /// This routine dispatches to the underlying AttributeList or the
3728 /// OperandBundleUser as appropriate.
3730 /// The index \p i is interpreted as
3732 /// \p i == Attribute::ReturnIndex -> the return value
3733 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
3734 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
3735 /// (\p i - 1) in the operand list.
3736 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const;
3738 /// Extract the alignment for a call or parameter (0=unknown).
3739 unsigned getParamAlignment(unsigned i) const {
3740 return AttributeList.getParamAlignment(i);
3743 /// Extract the number of dereferenceable bytes for a call or
3744 /// parameter (0=unknown).
3745 uint64_t getDereferenceableBytes(unsigned i) const {
3746 return AttributeList.getDereferenceableBytes(i);
3749 /// Extract the number of dereferenceable_or_null bytes for a call or
3750 /// parameter (0=unknown).
3751 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
3752 return AttributeList.getDereferenceableOrNullBytes(i);
3755 /// @brief Determine if the parameter or return value is marked with NoAlias
3757 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
3758 bool doesNotAlias(unsigned n) const {
3759 return AttributeList.hasAttribute(n, Attribute::NoAlias);
3762 /// Return true if the call should not be treated as a call to a
3764 bool isNoBuiltin() const {
3765 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3766 // to check it by hand.
3767 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3768 !hasFnAttrImpl(Attribute::Builtin);
3771 /// Return true if the call should not be inlined.
3772 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3773 void setIsNoInline() {
3774 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3777 /// Determine if the call does not access memory.
3778 bool doesNotAccessMemory() const {
3779 return hasFnAttr(Attribute::ReadNone);
3781 void setDoesNotAccessMemory() {
3782 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3785 /// Determine if the call does not access or only reads memory.
3786 bool onlyReadsMemory() const {
3787 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3789 void setOnlyReadsMemory() {
3790 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3793 /// Determine if the call does not access or only writes memory.
3794 bool doesNotReadMemory() const {
3795 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
3797 void setDoesNotReadMemory() {
3798 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly);
3801 /// @brief Determine if the call access memmory only using it's pointer
3803 bool onlyAccessesArgMemory() const {
3804 return hasFnAttr(Attribute::ArgMemOnly);
3806 void setOnlyAccessesArgMemory() {
3807 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
3810 /// Determine if the call cannot return.
3811 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3812 void setDoesNotReturn() {
3813 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3816 /// Determine if the call cannot unwind.
3817 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3818 void setDoesNotThrow() {
3819 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3822 /// Determine if the invoke cannot be duplicated.
3823 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3824 void setCannotDuplicate() {
3825 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3828 /// Determine if the invoke is convergent
3829 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
3830 void setConvergent() {
3831 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
3833 void setNotConvergent() {
3834 removeAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
3837 /// Determine if the call returns a structure through first
3838 /// pointer argument.
3839 bool hasStructRetAttr() const {
3840 if (getNumArgOperands() == 0)
3843 // Be friendly and also check the callee.
3844 return paramHasAttr(1, Attribute::StructRet);
3847 /// Determine if any call argument is an aggregate passed by value.
3848 bool hasByValArgument() const {
3849 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3852 /// Return the function called, or null if this is an
3853 /// indirect function invocation.
3855 Function *getCalledFunction() const {
3856 return dyn_cast<Function>(Op<-3>());
3859 /// Get a pointer to the function that is invoked by this
3861 const Value *getCalledValue() const { return Op<-3>(); }
3862 Value *getCalledValue() { return Op<-3>(); }
3864 /// Set the function called.
3865 void setCalledFunction(Value* Fn) {
3867 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
3870 void setCalledFunction(FunctionType *FTy, Value *Fn) {
3872 assert(FTy == cast<FunctionType>(
3873 cast<PointerType>(Fn->getType())->getElementType()));
3877 // get*Dest - Return the destination basic blocks...
3878 BasicBlock *getNormalDest() const {
3879 return cast<BasicBlock>(Op<-2>());
3881 BasicBlock *getUnwindDest() const {
3882 return cast<BasicBlock>(Op<-1>());
3884 void setNormalDest(BasicBlock *B) {
3885 Op<-2>() = reinterpret_cast<Value*>(B);
3887 void setUnwindDest(BasicBlock *B) {
3888 Op<-1>() = reinterpret_cast<Value*>(B);
3891 /// Get the landingpad instruction from the landing pad
3892 /// block (the unwind destination).
3893 LandingPadInst *getLandingPadInst() const;
3895 BasicBlock *getSuccessor(unsigned i) const {
3896 assert(i < 2 && "Successor # out of range for invoke!");
3897 return i == 0 ? getNormalDest() : getUnwindDest();
3900 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3901 assert(idx < 2 && "Successor # out of range for invoke!");
3902 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3905 unsigned getNumSuccessors() const { return 2; }
3907 // Methods for support type inquiry through isa, cast, and dyn_cast:
3908 static inline bool classof(const Instruction *I) {
3909 return (I->getOpcode() == Instruction::Invoke);
3911 static inline bool classof(const Value *V) {
3912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3916 BasicBlock *getSuccessorV(unsigned idx) const override;
3917 unsigned getNumSuccessorsV() const override;
3918 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3920 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
3921 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, Kind))
3924 // Operand bundles override attributes on the called function, but don't
3925 // override attributes directly present on the invoke instruction.
3926 if (isFnAttrDisallowedByOpBundle(Kind))
3929 if (const Function *F = getCalledFunction())
3930 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, Kind);
3934 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3935 // method so that subclasses cannot accidentally use it.
3936 void setInstructionSubclassData(unsigned short D) {
3937 Instruction::setInstructionSubclassData(D);
3942 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3945 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3946 BasicBlock *IfException, ArrayRef<Value *> Args,
3947 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3948 const Twine &NameStr, Instruction *InsertBefore)
3949 : TerminatorInst(Ty->getReturnType(), Instruction::Invoke,
3950 OperandTraits<InvokeInst>::op_end(this) - Values, Values,
3952 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3955 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
3956 BasicBlock *IfException, ArrayRef<Value *> Args,
3957 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3958 const Twine &NameStr, BasicBlock *InsertAtEnd)
3960 cast<FunctionType>(cast<PointerType>(Func->getType())
3961 ->getElementType())->getReturnType(),
3962 Instruction::Invoke, OperandTraits<InvokeInst>::op_end(this) - Values,
3963 Values, InsertAtEnd) {
3964 init(Func, IfNormal, IfException, Args, Bundles, NameStr);
3967 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3969 //===----------------------------------------------------------------------===//
3971 //===----------------------------------------------------------------------===//
3973 //===---------------------------------------------------------------------------
3974 /// Resume the propagation of an exception.
3976 class ResumeInst : public TerminatorInst {
3977 ResumeInst(const ResumeInst &RI);
3979 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3980 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3983 // Note: Instruction needs to be a friend here to call cloneImpl.
3984 friend class Instruction;
3986 ResumeInst *cloneImpl() const;
3989 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3990 return new(1) ResumeInst(Exn, InsertBefore);
3993 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3994 return new(1) ResumeInst(Exn, InsertAtEnd);
3997 /// Provide fast operand accessors
3998 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4000 /// Convenience accessor.
4001 Value *getValue() const { return Op<0>(); }
4003 unsigned getNumSuccessors() const { return 0; }
4005 // Methods for support type inquiry through isa, cast, and dyn_cast:
4006 static inline bool classof(const Instruction *I) {
4007 return I->getOpcode() == Instruction::Resume;
4009 static inline bool classof(const Value *V) {
4010 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4014 BasicBlock *getSuccessorV(unsigned idx) const override;
4015 unsigned getNumSuccessorsV() const override;
4016 void setSuccessorV(unsigned idx, BasicBlock *B) override;
4020 struct OperandTraits<ResumeInst> :
4021 public FixedNumOperandTraits<ResumeInst, 1> {
4024 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
4026 //===----------------------------------------------------------------------===//
4027 // CatchSwitchInst Class
4028 //===----------------------------------------------------------------------===//
4029 class CatchSwitchInst : public TerminatorInst {
4030 /// The number of operands actually allocated. NumOperands is
4031 /// the number actually in use.
4032 unsigned ReservedSpace;
4034 // Operand[0] = Outer scope
4035 // Operand[1] = Unwind block destination
4036 // Operand[n] = BasicBlock to go to on match
4037 CatchSwitchInst(const CatchSwitchInst &CSI);
4039 /// Create a new switch instruction, specifying a
4040 /// default destination. The number of additional handlers can be specified
4041 /// here to make memory allocation more efficient.
4042 /// This constructor can also autoinsert before another instruction.
4043 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4044 unsigned NumHandlers, const Twine &NameStr,
4045 Instruction *InsertBefore);
4047 /// Create a new switch instruction, specifying a
4048 /// default destination. The number of additional handlers can be specified
4049 /// here to make memory allocation more efficient.
4050 /// This constructor also autoinserts at the end of the specified BasicBlock.
4051 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4052 unsigned NumHandlers, const Twine &NameStr,
4053 BasicBlock *InsertAtEnd);
4055 // allocate space for exactly zero operands
4056 void *operator new(size_t s) { return User::operator new(s); }
4058 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
4059 void growOperands(unsigned Size);
4062 // Note: Instruction needs to be a friend here to call cloneImpl.
4063 friend class Instruction;
4065 CatchSwitchInst *cloneImpl() const;
4068 void *operator new(size_t, unsigned) = delete;
4070 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4071 unsigned NumHandlers,
4072 const Twine &NameStr = "",
4073 Instruction *InsertBefore = nullptr) {
4074 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4078 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4079 unsigned NumHandlers, const Twine &NameStr,
4080 BasicBlock *InsertAtEnd) {
4081 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4085 /// Provide fast operand accessors
4086 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4088 // Accessor Methods for CatchSwitch stmt
4089 Value *getParentPad() const { return getOperand(0); }
4090 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4092 // Accessor Methods for CatchSwitch stmt
4093 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4094 bool unwindsToCaller() const { return !hasUnwindDest(); }
4095 BasicBlock *getUnwindDest() const {
4096 if (hasUnwindDest())
4097 return cast<BasicBlock>(getOperand(1));
4100 void setUnwindDest(BasicBlock *UnwindDest) {
4102 assert(hasUnwindDest());
4103 setOperand(1, UnwindDest);
4106 /// return the number of 'handlers' in this catchswitch
4107 /// instruction, except the default handler
4108 unsigned getNumHandlers() const {
4109 if (hasUnwindDest())
4110 return getNumOperands() - 2;
4111 return getNumOperands() - 1;
4115 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4116 static const BasicBlock *handler_helper(const Value *V) {
4117 return cast<BasicBlock>(V);
4121 typedef std::pointer_to_unary_function<Value *, BasicBlock *> DerefFnTy;
4122 typedef mapped_iterator<op_iterator, DerefFnTy> handler_iterator;
4123 typedef iterator_range<handler_iterator> handler_range;
4124 typedef std::pointer_to_unary_function<const Value *, const BasicBlock *>
4126 typedef mapped_iterator<const_op_iterator, ConstDerefFnTy> const_handler_iterator;
4127 typedef iterator_range<const_handler_iterator> const_handler_range;
4129 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4130 handler_iterator handler_begin() {
4131 op_iterator It = op_begin() + 1;
4132 if (hasUnwindDest())
4134 return handler_iterator(It, DerefFnTy(handler_helper));
4137 /// Returns an iterator that points to the first handler in the
4138 /// CatchSwitchInst.
4139 const_handler_iterator handler_begin() const {
4140 const_op_iterator It = op_begin() + 1;
4141 if (hasUnwindDest())
4143 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4146 /// Returns a read-only iterator that points one past the last
4147 /// handler in the CatchSwitchInst.
4148 handler_iterator handler_end() {
4149 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4152 /// Returns an iterator that points one past the last handler in the
4153 /// CatchSwitchInst.
4154 const_handler_iterator handler_end() const {
4155 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4158 /// iteration adapter for range-for loops.
4159 handler_range handlers() {
4160 return make_range(handler_begin(), handler_end());
4163 /// iteration adapter for range-for loops.
4164 const_handler_range handlers() const {
4165 return make_range(handler_begin(), handler_end());
4168 /// Add an entry to the switch instruction...
4170 /// This action invalidates handler_end(). Old handler_end() iterator will
4171 /// point to the added handler.
4172 void addHandler(BasicBlock *Dest);
4174 void removeHandler(handler_iterator HI);
4176 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4177 BasicBlock *getSuccessor(unsigned Idx) const {
4178 assert(Idx < getNumSuccessors() &&
4179 "Successor # out of range for catchswitch!");
4180 return cast<BasicBlock>(getOperand(Idx + 1));
4182 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4183 assert(Idx < getNumSuccessors() &&
4184 "Successor # out of range for catchswitch!");
4185 setOperand(Idx + 1, NewSucc);
4188 // Methods for support type inquiry through isa, cast, and dyn_cast:
4189 static inline bool classof(const Instruction *I) {
4190 return I->getOpcode() == Instruction::CatchSwitch;
4192 static inline bool classof(const Value *V) {
4193 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4197 BasicBlock *getSuccessorV(unsigned Idx) const override;
4198 unsigned getNumSuccessorsV() const override;
4199 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4203 struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4205 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)
4207 //===----------------------------------------------------------------------===//
4208 // CleanupPadInst Class
4209 //===----------------------------------------------------------------------===//
4210 class CleanupPadInst : public FuncletPadInst {
4212 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4213 unsigned Values, const Twine &NameStr,
4214 Instruction *InsertBefore)
4215 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4216 NameStr, InsertBefore) {}
4217 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4218 unsigned Values, const Twine &NameStr,
4219 BasicBlock *InsertAtEnd)
4220 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4221 NameStr, InsertAtEnd) {}
4224 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4225 const Twine &NameStr = "",
4226 Instruction *InsertBefore = nullptr) {
4227 unsigned Values = 1 + Args.size();
4229 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4232 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4233 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4234 unsigned Values = 1 + Args.size();
4236 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4239 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4240 static inline bool classof(const Instruction *I) {
4241 return I->getOpcode() == Instruction::CleanupPad;
4243 static inline bool classof(const Value *V) {
4244 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4248 //===----------------------------------------------------------------------===//
4249 // CatchPadInst Class
4250 //===----------------------------------------------------------------------===//
4251 class CatchPadInst : public FuncletPadInst {
4253 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4254 unsigned Values, const Twine &NameStr,
4255 Instruction *InsertBefore)
4256 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4257 NameStr, InsertBefore) {}
4258 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4259 unsigned Values, const Twine &NameStr,
4260 BasicBlock *InsertAtEnd)
4261 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4262 NameStr, InsertAtEnd) {}
4265 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4266 const Twine &NameStr = "",
4267 Instruction *InsertBefore = nullptr) {
4268 unsigned Values = 1 + Args.size();
4270 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4273 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4274 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4275 unsigned Values = 1 + Args.size();
4277 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4280 /// Convenience accessors
4281 CatchSwitchInst *getCatchSwitch() const {
4282 return cast<CatchSwitchInst>(Op<-1>());
4284 void setCatchSwitch(Value *CatchSwitch) {
4285 assert(CatchSwitch);
4286 Op<-1>() = CatchSwitch;
4289 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4290 static inline bool classof(const Instruction *I) {
4291 return I->getOpcode() == Instruction::CatchPad;
4293 static inline bool classof(const Value *V) {
4294 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4298 //===----------------------------------------------------------------------===//
4299 // CatchReturnInst Class
4300 //===----------------------------------------------------------------------===//
4302 class CatchReturnInst : public TerminatorInst {
4303 CatchReturnInst(const CatchReturnInst &RI);
4304 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4305 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4307 void init(Value *CatchPad, BasicBlock *BB);
4310 // Note: Instruction needs to be a friend here to call cloneImpl.
4311 friend class Instruction;
4313 CatchReturnInst *cloneImpl() const;
4316 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4317 Instruction *InsertBefore = nullptr) {
4320 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4323 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4324 BasicBlock *InsertAtEnd) {
4327 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4330 /// Provide fast operand accessors
4331 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4333 /// Convenience accessors.
4334 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4335 void setCatchPad(CatchPadInst *CatchPad) {
4340 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4341 void setSuccessor(BasicBlock *NewSucc) {
4345 unsigned getNumSuccessors() const { return 1; }
4347 /// Get the parentPad of this catchret's catchpad's catchswitch.
4348 /// The successor block is implicitly a member of this funclet.
4349 Value *getCatchSwitchParentPad() const {
4350 return getCatchPad()->getCatchSwitch()->getParentPad();
4353 // Methods for support type inquiry through isa, cast, and dyn_cast:
4354 static inline bool classof(const Instruction *I) {
4355 return (I->getOpcode() == Instruction::CatchRet);
4357 static inline bool classof(const Value *V) {
4358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4362 BasicBlock *getSuccessorV(unsigned Idx) const override;
4363 unsigned getNumSuccessorsV() const override;
4364 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4368 struct OperandTraits<CatchReturnInst>
4369 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4371 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
4373 //===----------------------------------------------------------------------===//
4374 // CleanupReturnInst Class
4375 //===----------------------------------------------------------------------===//
4377 class CleanupReturnInst : public TerminatorInst {
4379 CleanupReturnInst(const CleanupReturnInst &RI);
4380 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4381 Instruction *InsertBefore = nullptr);
4382 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4383 BasicBlock *InsertAtEnd);
4385 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4388 // Note: Instruction needs to be a friend here to call cloneImpl.
4389 friend class Instruction;
4391 CleanupReturnInst *cloneImpl() const;
4394 static CleanupReturnInst *Create(Value *CleanupPad,
4395 BasicBlock *UnwindBB = nullptr,
4396 Instruction *InsertBefore = nullptr) {
4398 unsigned Values = 1;
4402 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4405 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4406 BasicBlock *InsertAtEnd) {
4408 unsigned Values = 1;
4412 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4415 /// Provide fast operand accessors
4416 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4418 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4419 bool unwindsToCaller() const { return !hasUnwindDest(); }
4421 /// Convenience accessor.
4422 CleanupPadInst *getCleanupPad() const {
4423 return cast<CleanupPadInst>(Op<0>());
4425 void setCleanupPad(CleanupPadInst *CleanupPad) {
4427 Op<0>() = CleanupPad;
4430 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4432 BasicBlock *getUnwindDest() const {
4433 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4435 void setUnwindDest(BasicBlock *NewDest) {
4437 assert(hasUnwindDest());
4441 // Methods for support type inquiry through isa, cast, and dyn_cast:
4442 static inline bool classof(const Instruction *I) {
4443 return (I->getOpcode() == Instruction::CleanupRet);
4445 static inline bool classof(const Value *V) {
4446 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4450 BasicBlock *getSuccessorV(unsigned Idx) const override;
4451 unsigned getNumSuccessorsV() const override;
4452 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4454 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4455 // method so that subclasses cannot accidentally use it.
4456 void setInstructionSubclassData(unsigned short D) {
4457 Instruction::setInstructionSubclassData(D);
4462 struct OperandTraits<CleanupReturnInst>
4463 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4465 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)
4467 //===----------------------------------------------------------------------===//
4468 // UnreachableInst Class
4469 //===----------------------------------------------------------------------===//
4471 //===---------------------------------------------------------------------------
4472 /// This function has undefined behavior. In particular, the
4473 /// presence of this instruction indicates some higher level knowledge that the
4474 /// end of the block cannot be reached.
4476 class UnreachableInst : public TerminatorInst {
4478 // Note: Instruction needs to be a friend here to call cloneImpl.
4479 friend class Instruction;
4481 UnreachableInst *cloneImpl() const;
4484 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4485 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4487 // allocate space for exactly zero operands
4488 void *operator new(size_t s) {
4489 return User::operator new(s, 0);
4492 void *operator new(size_t, unsigned) = delete;
4494 unsigned getNumSuccessors() const { return 0; }
4496 // Methods for support type inquiry through isa, cast, and dyn_cast:
4497 static inline bool classof(const Instruction *I) {
4498 return I->getOpcode() == Instruction::Unreachable;
4500 static inline bool classof(const Value *V) {
4501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4505 BasicBlock *getSuccessorV(unsigned idx) const override;
4506 unsigned getNumSuccessorsV() const override;
4507 void setSuccessorV(unsigned idx, BasicBlock *B) override;
4510 //===----------------------------------------------------------------------===//
4512 //===----------------------------------------------------------------------===//
4514 /// This class represents a truncation of integer types.
4515 class TruncInst : public CastInst {
4517 // Note: Instruction needs to be a friend here to call cloneImpl.
4518 friend class Instruction;
4520 /// Clone an identical TruncInst
4521 TruncInst *cloneImpl() const;
4524 /// Constructor with insert-before-instruction semantics
4526 Value *S, ///< The value to be truncated
4527 Type *Ty, ///< The (smaller) type to truncate to
4528 const Twine &NameStr = "", ///< A name for the new instruction
4529 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4532 /// Constructor with insert-at-end-of-block semantics
4534 Value *S, ///< The value to be truncated
4535 Type *Ty, ///< The (smaller) type to truncate to
4536 const Twine &NameStr, ///< A name for the new instruction
4537 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4540 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4541 static inline bool classof(const Instruction *I) {
4542 return I->getOpcode() == Trunc;
4544 static inline bool classof(const Value *V) {
4545 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4549 //===----------------------------------------------------------------------===//
4551 //===----------------------------------------------------------------------===//
4553 /// This class represents zero extension of integer types.
4554 class ZExtInst : public CastInst {
4556 // Note: Instruction needs to be a friend here to call cloneImpl.
4557 friend class Instruction;
4559 /// Clone an identical ZExtInst
4560 ZExtInst *cloneImpl() const;
4563 /// Constructor with insert-before-instruction semantics
4565 Value *S, ///< The value to be zero extended
4566 Type *Ty, ///< The type to zero extend to
4567 const Twine &NameStr = "", ///< A name for the new instruction
4568 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4571 /// Constructor with insert-at-end semantics.
4573 Value *S, ///< The value to be zero extended
4574 Type *Ty, ///< The type to zero extend to
4575 const Twine &NameStr, ///< A name for the new instruction
4576 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4579 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4580 static inline bool classof(const Instruction *I) {
4581 return I->getOpcode() == ZExt;
4583 static inline bool classof(const Value *V) {
4584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4588 //===----------------------------------------------------------------------===//
4590 //===----------------------------------------------------------------------===//
4592 /// This class represents a sign extension of integer types.
4593 class SExtInst : public CastInst {
4595 // Note: Instruction needs to be a friend here to call cloneImpl.
4596 friend class Instruction;
4598 /// Clone an identical SExtInst
4599 SExtInst *cloneImpl() const;
4602 /// Constructor with insert-before-instruction semantics
4604 Value *S, ///< The value to be sign extended
4605 Type *Ty, ///< The type to sign extend to
4606 const Twine &NameStr = "", ///< A name for the new instruction
4607 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4610 /// Constructor with insert-at-end-of-block semantics
4612 Value *S, ///< The value to be sign extended
4613 Type *Ty, ///< The type to sign extend to
4614 const Twine &NameStr, ///< A name for the new instruction
4615 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4618 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4619 static inline bool classof(const Instruction *I) {
4620 return I->getOpcode() == SExt;
4622 static inline bool classof(const Value *V) {
4623 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4627 //===----------------------------------------------------------------------===//
4628 // FPTruncInst Class
4629 //===----------------------------------------------------------------------===//
4631 /// This class represents a truncation of floating point types.
4632 class FPTruncInst : public CastInst {
4634 // Note: Instruction needs to be a friend here to call cloneImpl.
4635 friend class Instruction;
4637 /// Clone an identical FPTruncInst
4638 FPTruncInst *cloneImpl() const;
4641 /// Constructor with insert-before-instruction semantics
4643 Value *S, ///< The value to be truncated
4644 Type *Ty, ///< The type to truncate to
4645 const Twine &NameStr = "", ///< A name for the new instruction
4646 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4649 /// Constructor with insert-before-instruction semantics
4651 Value *S, ///< The value to be truncated
4652 Type *Ty, ///< The type to truncate to
4653 const Twine &NameStr, ///< A name for the new instruction
4654 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4657 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4658 static inline bool classof(const Instruction *I) {
4659 return I->getOpcode() == FPTrunc;
4661 static inline bool classof(const Value *V) {
4662 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4666 //===----------------------------------------------------------------------===//
4668 //===----------------------------------------------------------------------===//
4670 /// This class represents an extension of floating point types.
4671 class FPExtInst : public CastInst {
4673 // Note: Instruction needs to be a friend here to call cloneImpl.
4674 friend class Instruction;
4676 /// Clone an identical FPExtInst
4677 FPExtInst *cloneImpl() const;
4680 /// Constructor with insert-before-instruction semantics
4682 Value *S, ///< The value to be extended
4683 Type *Ty, ///< The type to extend to
4684 const Twine &NameStr = "", ///< A name for the new instruction
4685 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4688 /// Constructor with insert-at-end-of-block semantics
4690 Value *S, ///< The value to be extended
4691 Type *Ty, ///< The type to extend to
4692 const Twine &NameStr, ///< A name for the new instruction
4693 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4696 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4697 static inline bool classof(const Instruction *I) {
4698 return I->getOpcode() == FPExt;
4700 static inline bool classof(const Value *V) {
4701 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4705 //===----------------------------------------------------------------------===//
4707 //===----------------------------------------------------------------------===//
4709 /// This class represents a cast unsigned integer to floating point.
4710 class UIToFPInst : public CastInst {
4712 // Note: Instruction needs to be a friend here to call cloneImpl.
4713 friend class Instruction;
4715 /// Clone an identical UIToFPInst
4716 UIToFPInst *cloneImpl() const;
4719 /// Constructor with insert-before-instruction semantics
4721 Value *S, ///< The value to be converted
4722 Type *Ty, ///< The type to convert to
4723 const Twine &NameStr = "", ///< A name for the new instruction
4724 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4727 /// Constructor with insert-at-end-of-block semantics
4729 Value *S, ///< The value to be converted
4730 Type *Ty, ///< The type to convert to
4731 const Twine &NameStr, ///< A name for the new instruction
4732 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4735 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4736 static inline bool classof(const Instruction *I) {
4737 return I->getOpcode() == UIToFP;
4739 static inline bool classof(const Value *V) {
4740 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4744 //===----------------------------------------------------------------------===//
4746 //===----------------------------------------------------------------------===//
4748 /// This class represents a cast from signed integer to floating point.
4749 class SIToFPInst : public CastInst {
4751 // Note: Instruction needs to be a friend here to call cloneImpl.
4752 friend class Instruction;
4754 /// Clone an identical SIToFPInst
4755 SIToFPInst *cloneImpl() const;
4758 /// Constructor with insert-before-instruction semantics
4760 Value *S, ///< The value to be converted
4761 Type *Ty, ///< The type to convert to
4762 const Twine &NameStr = "", ///< A name for the new instruction
4763 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4766 /// Constructor with insert-at-end-of-block semantics
4768 Value *S, ///< The value to be converted
4769 Type *Ty, ///< The type to convert to
4770 const Twine &NameStr, ///< A name for the new instruction
4771 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4774 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4775 static inline bool classof(const Instruction *I) {
4776 return I->getOpcode() == SIToFP;
4778 static inline bool classof(const Value *V) {
4779 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4783 //===----------------------------------------------------------------------===//
4785 //===----------------------------------------------------------------------===//
4787 /// This class represents a cast from floating point to unsigned integer
4788 class FPToUIInst : public CastInst {
4790 // Note: Instruction needs to be a friend here to call cloneImpl.
4791 friend class Instruction;
4793 /// Clone an identical FPToUIInst
4794 FPToUIInst *cloneImpl() const;
4797 /// Constructor with insert-before-instruction semantics
4799 Value *S, ///< The value to be converted
4800 Type *Ty, ///< The type to convert to
4801 const Twine &NameStr = "", ///< A name for the new instruction
4802 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4805 /// Constructor with insert-at-end-of-block semantics
4807 Value *S, ///< The value to be converted
4808 Type *Ty, ///< The type to convert to
4809 const Twine &NameStr, ///< A name for the new instruction
4810 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
4813 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4814 static inline bool classof(const Instruction *I) {
4815 return I->getOpcode() == FPToUI;
4817 static inline bool classof(const Value *V) {
4818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4822 //===----------------------------------------------------------------------===//
4824 //===----------------------------------------------------------------------===//
4826 /// This class represents a cast from floating point to signed integer.
4827 class FPToSIInst : public CastInst {
4829 // Note: Instruction needs to be a friend here to call cloneImpl.
4830 friend class Instruction;
4832 /// Clone an identical FPToSIInst
4833 FPToSIInst *cloneImpl() const;
4836 /// Constructor with insert-before-instruction semantics
4838 Value *S, ///< The value to be converted
4839 Type *Ty, ///< The type to convert to
4840 const Twine &NameStr = "", ///< A name for the new instruction
4841 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4844 /// Constructor with insert-at-end-of-block semantics
4846 Value *S, ///< The value to be converted
4847 Type *Ty, ///< The type to convert to
4848 const Twine &NameStr, ///< A name for the new instruction
4849 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4852 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4853 static inline bool classof(const Instruction *I) {
4854 return I->getOpcode() == FPToSI;
4856 static inline bool classof(const Value *V) {
4857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4861 //===----------------------------------------------------------------------===//
4862 // IntToPtrInst Class
4863 //===----------------------------------------------------------------------===//
4865 /// This class represents a cast from an integer to a pointer.
4866 class IntToPtrInst : public CastInst {
4868 // Note: Instruction needs to be a friend here to call cloneImpl.
4869 friend class Instruction;
4871 /// Constructor with insert-before-instruction semantics
4873 Value *S, ///< The value to be converted
4874 Type *Ty, ///< The type to convert to
4875 const Twine &NameStr = "", ///< A name for the new instruction
4876 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4879 /// Constructor with insert-at-end-of-block semantics
4881 Value *S, ///< The value to be converted
4882 Type *Ty, ///< The type to convert to
4883 const Twine &NameStr, ///< A name for the new instruction
4884 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4887 /// Clone an identical IntToPtrInst.
4888 IntToPtrInst *cloneImpl() const;
4890 /// Returns the address space of this instruction's pointer type.
4891 unsigned getAddressSpace() const {
4892 return getType()->getPointerAddressSpace();
4895 // Methods for support type inquiry through isa, cast, and dyn_cast:
4896 static inline bool classof(const Instruction *I) {
4897 return I->getOpcode() == IntToPtr;
4899 static inline bool classof(const Value *V) {
4900 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4904 //===----------------------------------------------------------------------===//
4905 // PtrToIntInst Class
4906 //===----------------------------------------------------------------------===//
4908 /// This class represents a cast from a pointer to an integer.
4909 class PtrToIntInst : public CastInst {
4911 // Note: Instruction needs to be a friend here to call cloneImpl.
4912 friend class Instruction;
4914 /// Clone an identical PtrToIntInst.
4915 PtrToIntInst *cloneImpl() const;
4918 /// Constructor with insert-before-instruction semantics
4920 Value *S, ///< The value to be converted
4921 Type *Ty, ///< The type to convert to
4922 const Twine &NameStr = "", ///< A name for the new instruction
4923 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4926 /// Constructor with insert-at-end-of-block semantics
4928 Value *S, ///< The value to be converted
4929 Type *Ty, ///< The type to convert to
4930 const Twine &NameStr, ///< A name for the new instruction
4931 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4934 /// Gets the pointer operand.
4935 Value *getPointerOperand() { return getOperand(0); }
4936 /// Gets the pointer operand.
4937 const Value *getPointerOperand() const { return getOperand(0); }
4938 /// Gets the operand index of the pointer operand.
4939 static unsigned getPointerOperandIndex() { return 0U; }
4941 /// Returns the address space of the pointer operand.
4942 unsigned getPointerAddressSpace() const {
4943 return getPointerOperand()->getType()->getPointerAddressSpace();
4946 // Methods for support type inquiry through isa, cast, and dyn_cast:
4947 static inline bool classof(const Instruction *I) {
4948 return I->getOpcode() == PtrToInt;
4950 static inline bool classof(const Value *V) {
4951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4955 //===----------------------------------------------------------------------===//
4956 // BitCastInst Class
4957 //===----------------------------------------------------------------------===//
4959 /// This class represents a no-op cast from one type to another.
4960 class BitCastInst : public CastInst {
4962 // Note: Instruction needs to be a friend here to call cloneImpl.
4963 friend class Instruction;
4965 /// Clone an identical BitCastInst.
4966 BitCastInst *cloneImpl() const;
4969 /// Constructor with insert-before-instruction semantics
4971 Value *S, ///< The value to be casted
4972 Type *Ty, ///< The type to casted to
4973 const Twine &NameStr = "", ///< A name for the new instruction
4974 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4977 /// Constructor with insert-at-end-of-block semantics
4979 Value *S, ///< The value to be casted
4980 Type *Ty, ///< The type to casted to
4981 const Twine &NameStr, ///< A name for the new instruction
4982 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4985 // Methods for support type inquiry through isa, cast, and dyn_cast:
4986 static inline bool classof(const Instruction *I) {
4987 return I->getOpcode() == BitCast;
4989 static inline bool classof(const Value *V) {
4990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4994 //===----------------------------------------------------------------------===//
4995 // AddrSpaceCastInst Class
4996 //===----------------------------------------------------------------------===//
4998 /// This class represents a conversion between pointers from one address space
5000 class AddrSpaceCastInst : public CastInst {
5002 // Note: Instruction needs to be a friend here to call cloneImpl.
5003 friend class Instruction;
5005 /// Clone an identical AddrSpaceCastInst.
5006 AddrSpaceCastInst *cloneImpl() const;
5009 /// Constructor with insert-before-instruction semantics
5011 Value *S, ///< The value to be casted
5012 Type *Ty, ///< The type to casted to
5013 const Twine &NameStr = "", ///< A name for the new instruction
5014 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5017 /// Constructor with insert-at-end-of-block semantics
5019 Value *S, ///< The value to be casted
5020 Type *Ty, ///< The type to casted to
5021 const Twine &NameStr, ///< A name for the new instruction
5022 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5025 // Methods for support type inquiry through isa, cast, and dyn_cast:
5026 static inline bool classof(const Instruction *I) {
5027 return I->getOpcode() == AddrSpaceCast;
5029 static inline bool classof(const Value *V) {
5030 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5033 /// Gets the pointer operand.
5034 Value *getPointerOperand() {
5035 return getOperand(0);
5038 /// Gets the pointer operand.
5039 const Value *getPointerOperand() const {
5040 return getOperand(0);
5043 /// Gets the operand index of the pointer operand.
5044 static unsigned getPointerOperandIndex() {
5048 /// Returns the address space of the pointer operand.
5049 unsigned getSrcAddressSpace() const {
5050 return getPointerOperand()->getType()->getPointerAddressSpace();
5053 /// Returns the address space of the result.
5054 unsigned getDestAddressSpace() const {
5055 return getType()->getPointerAddressSpace();
5059 } // end namespace llvm
5061 #endif // LLVM_IR_INSTRUCTIONS_H