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/SmallVector.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 /// Returns true if the ordering is at least as strong as acquire
54 /// (i.e. acquire, acq_rel or seq_cst)
55 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
56 return (Ord == Acquire ||
57 Ord == AcquireRelease ||
58 Ord == SequentiallyConsistent);
61 /// Returns true if the ordering is at least as strong as release
62 /// (i.e. release, acq_rel or seq_cst)
63 inline bool isAtLeastRelease(AtomicOrdering Ord) {
64 return (Ord == Release ||
65 Ord == AcquireRelease ||
66 Ord == SequentiallyConsistent);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// AllocaInst - an instruction to allocate memory on the stack
75 class AllocaInst : public UnaryInstruction {
77 AllocaInst *clone_impl() const override;
79 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
80 const Twine &Name = "",
81 Instruction *InsertBefore = nullptr);
82 AllocaInst(Type *Ty, Value *ArraySize,
83 const Twine &Name, BasicBlock *InsertAtEnd);
85 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
86 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
88 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
89 const Twine &Name = "", Instruction *InsertBefore = nullptr);
90 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
91 const Twine &Name, BasicBlock *InsertAtEnd);
93 // Out of line virtual method, so the vtable, etc. has a home.
94 virtual ~AllocaInst();
96 /// isArrayAllocation - Return true if there is an allocation size parameter
97 /// to the allocation instruction that is not 1.
99 bool isArrayAllocation() const;
101 /// getArraySize - Get the number of elements allocated. For a simple
102 /// allocation of a single element, this will return a constant 1 value.
104 const Value *getArraySize() const { return getOperand(0); }
105 Value *getArraySize() { return getOperand(0); }
107 /// getType - Overload to return most specific pointer type
109 PointerType *getType() const {
110 return cast<PointerType>(Instruction::getType());
113 /// getAllocatedType - Return the type that is being allocated by the
116 Type *getAllocatedType() const;
118 /// getAlignment - Return the alignment of the memory that is being allocated
119 /// by the instruction.
121 unsigned getAlignment() const {
122 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
124 void setAlignment(unsigned Align);
126 /// isStaticAlloca - Return true if this alloca is in the entry block of the
127 /// function and is a constant size. If so, the code generator will fold it
128 /// into the prolog/epilog code, so it is basically free.
129 bool isStaticAlloca() const;
131 /// \brief Return true if this alloca is used as an inalloca argument to a
132 /// call. Such allocas are never considered static even if they are in the
134 bool isUsedWithInAlloca() const {
135 return getSubclassDataFromInstruction() & 32;
138 /// \brief Specify whether this alloca is used to represent the arguments to
140 void setUsedWithInAlloca(bool V) {
141 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
145 // Methods for support type inquiry through isa, cast, and dyn_cast:
146 static inline bool classof(const Instruction *I) {
147 return (I->getOpcode() == Instruction::Alloca);
149 static inline bool classof(const Value *V) {
150 return isa<Instruction>(V) && classof(cast<Instruction>(V));
153 // Shadow Instruction::setInstructionSubclassData with a private forwarding
154 // method so that subclasses cannot accidentally use it.
155 void setInstructionSubclassData(unsigned short D) {
156 Instruction::setInstructionSubclassData(D);
161 //===----------------------------------------------------------------------===//
163 //===----------------------------------------------------------------------===//
165 /// LoadInst - an instruction for reading from memory. This uses the
166 /// SubclassData field in Value to store whether or not the load is volatile.
168 class LoadInst : public UnaryInstruction {
171 LoadInst *clone_impl() const override;
173 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
174 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
175 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
176 Instruction *InsertBefore = nullptr);
177 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
178 BasicBlock *InsertAtEnd);
179 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
180 unsigned Align, Instruction *InsertBefore = nullptr);
181 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
182 unsigned Align, BasicBlock *InsertAtEnd);
183 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
184 unsigned Align, AtomicOrdering Order,
185 SynchronizationScope SynchScope = CrossThread,
186 Instruction *InsertBefore = nullptr);
187 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
188 unsigned Align, AtomicOrdering Order,
189 SynchronizationScope SynchScope,
190 BasicBlock *InsertAtEnd);
192 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
193 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
194 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
195 bool isVolatile = false,
196 Instruction *InsertBefore = nullptr);
197 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
198 BasicBlock *InsertAtEnd);
200 /// isVolatile - Return true if this is a load from a volatile memory
203 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
205 /// setVolatile - Specify whether this is a volatile load or not.
207 void setVolatile(bool V) {
208 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
212 /// getAlignment - Return the alignment of the access that is being performed
214 unsigned getAlignment() const {
215 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
218 void setAlignment(unsigned Align);
220 /// Returns the ordering effect of this fence.
221 AtomicOrdering getOrdering() const {
222 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
225 /// Set the ordering constraint on this load. May not be Release or
227 void setOrdering(AtomicOrdering Ordering) {
228 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
232 SynchronizationScope getSynchScope() const {
233 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
236 /// Specify whether this load is ordered with respect to all
237 /// concurrently executing threads, or only with respect to signal handlers
238 /// executing in the same thread.
239 void setSynchScope(SynchronizationScope xthread) {
240 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
244 bool isAtomic() const { return getOrdering() != NotAtomic; }
245 void setAtomic(AtomicOrdering Ordering,
246 SynchronizationScope SynchScope = CrossThread) {
247 setOrdering(Ordering);
248 setSynchScope(SynchScope);
251 bool isSimple() const { return !isAtomic() && !isVolatile(); }
252 bool isUnordered() const {
253 return getOrdering() <= Unordered && !isVolatile();
256 Value *getPointerOperand() { return getOperand(0); }
257 const Value *getPointerOperand() const { return getOperand(0); }
258 static unsigned getPointerOperandIndex() { return 0U; }
260 /// \brief Returns the address space of the pointer operand.
261 unsigned getPointerAddressSpace() const {
262 return getPointerOperand()->getType()->getPointerAddressSpace();
266 // Methods for support type inquiry through isa, cast, and dyn_cast:
267 static inline bool classof(const Instruction *I) {
268 return I->getOpcode() == Instruction::Load;
270 static inline bool classof(const Value *V) {
271 return isa<Instruction>(V) && classof(cast<Instruction>(V));
274 // Shadow Instruction::setInstructionSubclassData with a private forwarding
275 // method so that subclasses cannot accidentally use it.
276 void setInstructionSubclassData(unsigned short D) {
277 Instruction::setInstructionSubclassData(D);
282 //===----------------------------------------------------------------------===//
284 //===----------------------------------------------------------------------===//
286 /// StoreInst - an instruction for storing to memory
288 class StoreInst : public Instruction {
289 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
292 StoreInst *clone_impl() const override;
294 // allocate space for exactly two operands
295 void *operator new(size_t s) {
296 return User::operator new(s, 2);
298 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
299 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
300 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
301 Instruction *InsertBefore = nullptr);
302 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
303 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
304 unsigned Align, Instruction *InsertBefore = nullptr);
305 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
306 unsigned Align, BasicBlock *InsertAtEnd);
307 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
308 unsigned Align, AtomicOrdering Order,
309 SynchronizationScope SynchScope = CrossThread,
310 Instruction *InsertBefore = nullptr);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
312 unsigned Align, AtomicOrdering Order,
313 SynchronizationScope SynchScope,
314 BasicBlock *InsertAtEnd);
317 /// isVolatile - Return true if this is a store to a volatile memory
320 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
322 /// setVolatile - Specify whether this is a volatile store or not.
324 void setVolatile(bool V) {
325 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
329 /// Transparently provide more efficient getOperand methods.
330 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
332 /// getAlignment - Return the alignment of the access that is being performed
334 unsigned getAlignment() const {
335 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
338 void setAlignment(unsigned Align);
340 /// Returns the ordering effect of this store.
341 AtomicOrdering getOrdering() const {
342 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
345 /// Set the ordering constraint on this store. May not be Acquire or
347 void setOrdering(AtomicOrdering Ordering) {
348 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
352 SynchronizationScope getSynchScope() const {
353 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
356 /// Specify whether this store instruction is ordered with respect to all
357 /// concurrently executing threads, or only with respect to signal handlers
358 /// executing in the same thread.
359 void setSynchScope(SynchronizationScope xthread) {
360 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
364 bool isAtomic() const { return getOrdering() != NotAtomic; }
365 void setAtomic(AtomicOrdering Ordering,
366 SynchronizationScope SynchScope = CrossThread) {
367 setOrdering(Ordering);
368 setSynchScope(SynchScope);
371 bool isSimple() const { return !isAtomic() && !isVolatile(); }
372 bool isUnordered() const {
373 return getOrdering() <= Unordered && !isVolatile();
376 Value *getValueOperand() { return getOperand(0); }
377 const Value *getValueOperand() const { return getOperand(0); }
379 Value *getPointerOperand() { return getOperand(1); }
380 const Value *getPointerOperand() const { return getOperand(1); }
381 static unsigned getPointerOperandIndex() { return 1U; }
383 /// \brief Returns the address space of the pointer operand.
384 unsigned getPointerAddressSpace() const {
385 return getPointerOperand()->getType()->getPointerAddressSpace();
388 // Methods for support type inquiry through isa, cast, and dyn_cast:
389 static inline bool classof(const Instruction *I) {
390 return I->getOpcode() == Instruction::Store;
392 static inline bool classof(const Value *V) {
393 return isa<Instruction>(V) && classof(cast<Instruction>(V));
396 // Shadow Instruction::setInstructionSubclassData with a private forwarding
397 // method so that subclasses cannot accidentally use it.
398 void setInstructionSubclassData(unsigned short D) {
399 Instruction::setInstructionSubclassData(D);
404 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
407 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
409 //===----------------------------------------------------------------------===//
411 //===----------------------------------------------------------------------===//
413 /// FenceInst - an instruction for ordering other memory operations
415 class FenceInst : public Instruction {
416 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
417 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
419 FenceInst *clone_impl() const override;
421 // allocate space for exactly zero operands
422 void *operator new(size_t s) {
423 return User::operator new(s, 0);
426 // Ordering may only be Acquire, Release, AcquireRelease, or
427 // SequentiallyConsistent.
428 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
429 SynchronizationScope SynchScope = CrossThread,
430 Instruction *InsertBefore = nullptr);
431 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
432 SynchronizationScope SynchScope,
433 BasicBlock *InsertAtEnd);
435 /// Returns the ordering effect of this fence.
436 AtomicOrdering getOrdering() const {
437 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
440 /// Set the ordering constraint on this fence. May only be Acquire, Release,
441 /// AcquireRelease, or SequentiallyConsistent.
442 void setOrdering(AtomicOrdering Ordering) {
443 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
447 SynchronizationScope getSynchScope() const {
448 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
451 /// Specify whether this fence orders other operations with respect to all
452 /// concurrently executing threads, or only with respect to signal handlers
453 /// executing in the same thread.
454 void setSynchScope(SynchronizationScope xthread) {
455 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
459 // Methods for support type inquiry through isa, cast, and dyn_cast:
460 static inline bool classof(const Instruction *I) {
461 return I->getOpcode() == Instruction::Fence;
463 static inline bool classof(const Value *V) {
464 return isa<Instruction>(V) && classof(cast<Instruction>(V));
467 // Shadow Instruction::setInstructionSubclassData with a private forwarding
468 // method so that subclasses cannot accidentally use it.
469 void setInstructionSubclassData(unsigned short D) {
470 Instruction::setInstructionSubclassData(D);
474 //===----------------------------------------------------------------------===//
475 // AtomicCmpXchgInst Class
476 //===----------------------------------------------------------------------===//
478 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
479 /// specified value is in a memory location, and, if it is, stores a new value
480 /// there. Returns the value that was loaded.
482 class AtomicCmpXchgInst : public Instruction {
483 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
484 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
485 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
486 SynchronizationScope SynchScope);
488 AtomicCmpXchgInst *clone_impl() const override;
490 // allocate space for exactly three operands
491 void *operator new(size_t s) {
492 return User::operator new(s, 3);
494 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
495 AtomicOrdering SuccessOrdering,
496 AtomicOrdering FailureOrdering,
497 SynchronizationScope SynchScope,
498 Instruction *InsertBefore = nullptr);
499 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
500 AtomicOrdering SuccessOrdering,
501 AtomicOrdering FailureOrdering,
502 SynchronizationScope SynchScope,
503 BasicBlock *InsertAtEnd);
505 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
508 bool isVolatile() const {
509 return getSubclassDataFromInstruction() & 1;
512 /// setVolatile - Specify whether this is a volatile cmpxchg.
514 void setVolatile(bool V) {
515 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
519 /// Return true if this cmpxchg may spuriously fail.
520 bool isWeak() const {
521 return getSubclassDataFromInstruction() & 0x100;
524 void setWeak(bool IsWeak) {
525 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
529 /// Transparently provide more efficient getOperand methods.
530 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
532 /// Set the ordering constraint on this cmpxchg.
533 void setSuccessOrdering(AtomicOrdering Ordering) {
534 assert(Ordering != NotAtomic &&
535 "CmpXchg instructions can only be atomic.");
536 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
540 void setFailureOrdering(AtomicOrdering Ordering) {
541 assert(Ordering != NotAtomic &&
542 "CmpXchg instructions can only be atomic.");
543 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
547 /// Specify whether this cmpxchg is atomic and orders other operations with
548 /// respect to all concurrently executing threads, or only with respect to
549 /// signal handlers executing in the same thread.
550 void setSynchScope(SynchronizationScope SynchScope) {
551 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
555 /// Returns the ordering constraint on this cmpxchg.
556 AtomicOrdering getSuccessOrdering() const {
557 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
560 /// Returns the ordering constraint on this cmpxchg.
561 AtomicOrdering getFailureOrdering() const {
562 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
565 /// Returns whether this cmpxchg is atomic between threads or only within a
567 SynchronizationScope getSynchScope() const {
568 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
571 Value *getPointerOperand() { return getOperand(0); }
572 const Value *getPointerOperand() const { return getOperand(0); }
573 static unsigned getPointerOperandIndex() { return 0U; }
575 Value *getCompareOperand() { return getOperand(1); }
576 const Value *getCompareOperand() const { return getOperand(1); }
578 Value *getNewValOperand() { return getOperand(2); }
579 const Value *getNewValOperand() const { return getOperand(2); }
581 /// \brief Returns the address space of the pointer operand.
582 unsigned getPointerAddressSpace() const {
583 return getPointerOperand()->getType()->getPointerAddressSpace();
586 /// \brief Returns the strongest permitted ordering on failure, given the
587 /// desired ordering on success.
589 /// If the comparison in a cmpxchg operation fails, there is no atomic store
590 /// so release semantics cannot be provided. So this function drops explicit
591 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
592 /// operation would remain SequentiallyConsistent.
593 static AtomicOrdering
594 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
595 switch (SuccessOrdering) {
596 default: llvm_unreachable("invalid cmpxchg success ordering");
603 case SequentiallyConsistent:
604 return SequentiallyConsistent;
608 // Methods for support type inquiry through isa, cast, and dyn_cast:
609 static inline bool classof(const Instruction *I) {
610 return I->getOpcode() == Instruction::AtomicCmpXchg;
612 static inline bool classof(const Value *V) {
613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
616 // Shadow Instruction::setInstructionSubclassData with a private forwarding
617 // method so that subclasses cannot accidentally use it.
618 void setInstructionSubclassData(unsigned short D) {
619 Instruction::setInstructionSubclassData(D);
624 struct OperandTraits<AtomicCmpXchgInst> :
625 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
628 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
630 //===----------------------------------------------------------------------===//
631 // AtomicRMWInst Class
632 //===----------------------------------------------------------------------===//
634 /// AtomicRMWInst - an instruction that atomically reads a memory location,
635 /// combines it with another value, and then stores the result back. Returns
638 class AtomicRMWInst : public Instruction {
639 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
641 AtomicRMWInst *clone_impl() const override;
643 /// This enumeration lists the possible modifications atomicrmw can make. In
644 /// the descriptions, 'p' is the pointer to the instruction's memory location,
645 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
646 /// instruction. These instructions always return 'old'.
662 /// *p = old >signed v ? old : v
664 /// *p = old <signed v ? old : v
666 /// *p = old >unsigned v ? old : v
668 /// *p = old <unsigned v ? old : v
676 // allocate space for exactly two operands
677 void *operator new(size_t s) {
678 return User::operator new(s, 2);
680 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
681 AtomicOrdering Ordering, SynchronizationScope SynchScope,
682 Instruction *InsertBefore = nullptr);
683 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
684 AtomicOrdering Ordering, SynchronizationScope SynchScope,
685 BasicBlock *InsertAtEnd);
687 BinOp getOperation() const {
688 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
691 void setOperation(BinOp Operation) {
692 unsigned short SubclassData = getSubclassDataFromInstruction();
693 setInstructionSubclassData((SubclassData & 31) |
697 /// isVolatile - Return true if this is a RMW on a volatile memory location.
699 bool isVolatile() const {
700 return getSubclassDataFromInstruction() & 1;
703 /// setVolatile - Specify whether this is a volatile RMW or not.
705 void setVolatile(bool V) {
706 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
710 /// Transparently provide more efficient getOperand methods.
711 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
713 /// Set the ordering constraint on this RMW.
714 void setOrdering(AtomicOrdering Ordering) {
715 assert(Ordering != NotAtomic &&
716 "atomicrmw instructions can only be atomic.");
717 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
721 /// Specify whether this RMW orders other operations with respect to all
722 /// concurrently executing threads, or only with respect to signal handlers
723 /// executing in the same thread.
724 void setSynchScope(SynchronizationScope SynchScope) {
725 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
729 /// Returns the ordering constraint on this RMW.
730 AtomicOrdering getOrdering() const {
731 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
734 /// Returns whether this RMW is atomic between threads or only within a
736 SynchronizationScope getSynchScope() const {
737 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
740 Value *getPointerOperand() { return getOperand(0); }
741 const Value *getPointerOperand() const { return getOperand(0); }
742 static unsigned getPointerOperandIndex() { return 0U; }
744 Value *getValOperand() { return getOperand(1); }
745 const Value *getValOperand() const { return getOperand(1); }
747 /// \brief Returns the address space of the pointer operand.
748 unsigned getPointerAddressSpace() const {
749 return getPointerOperand()->getType()->getPointerAddressSpace();
752 // Methods for support type inquiry through isa, cast, and dyn_cast:
753 static inline bool classof(const Instruction *I) {
754 return I->getOpcode() == Instruction::AtomicRMW;
756 static inline bool classof(const Value *V) {
757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
760 void Init(BinOp Operation, Value *Ptr, Value *Val,
761 AtomicOrdering Ordering, SynchronizationScope SynchScope);
762 // Shadow Instruction::setInstructionSubclassData with a private forwarding
763 // method so that subclasses cannot accidentally use it.
764 void setInstructionSubclassData(unsigned short D) {
765 Instruction::setInstructionSubclassData(D);
770 struct OperandTraits<AtomicRMWInst>
771 : public FixedNumOperandTraits<AtomicRMWInst,2> {
774 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
776 //===----------------------------------------------------------------------===//
777 // GetElementPtrInst Class
778 //===----------------------------------------------------------------------===//
780 // checkGEPType - Simple wrapper function to give a better assertion failure
781 // message on bad indexes for a gep instruction.
783 inline Type *checkGEPType(Type *Ty) {
784 assert(Ty && "Invalid GetElementPtrInst indices for type!");
788 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
789 /// access elements of arrays and structs
791 class GetElementPtrInst : public Instruction {
792 GetElementPtrInst(const GetElementPtrInst &GEPI);
793 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
795 /// Constructors - Create a getelementptr instruction with a base pointer an
796 /// list of indices. The first ctor can optionally insert before an existing
797 /// instruction, the second appends the new instruction to the specified
799 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
800 unsigned Values, const Twine &NameStr,
801 Instruction *InsertBefore);
802 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
803 unsigned Values, const Twine &NameStr,
804 BasicBlock *InsertAtEnd);
806 GetElementPtrInst *clone_impl() const override;
808 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
809 const Twine &NameStr = "",
810 Instruction *InsertBefore = nullptr) {
811 unsigned Values = 1 + unsigned(IdxList.size());
813 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
815 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
816 const Twine &NameStr,
817 BasicBlock *InsertAtEnd) {
818 unsigned Values = 1 + unsigned(IdxList.size());
820 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
823 /// Create an "inbounds" getelementptr. See the documentation for the
824 /// "inbounds" flag in LangRef.html for details.
825 static GetElementPtrInst *CreateInBounds(Value *Ptr,
826 ArrayRef<Value *> IdxList,
827 const Twine &NameStr = "",
828 Instruction *InsertBefore = nullptr){
829 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
830 GEP->setIsInBounds(true);
833 static GetElementPtrInst *CreateInBounds(Value *Ptr,
834 ArrayRef<Value *> IdxList,
835 const Twine &NameStr,
836 BasicBlock *InsertAtEnd) {
837 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
838 GEP->setIsInBounds(true);
842 /// Transparently provide more efficient getOperand methods.
843 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
845 // getType - Overload to return most specific sequential type.
846 SequentialType *getType() const {
847 return cast<SequentialType>(Instruction::getType());
850 /// \brief Returns the address space of this instruction's pointer type.
851 unsigned getAddressSpace() const {
852 // Note that this is always the same as the pointer operand's address space
853 // and that is cheaper to compute, so cheat here.
854 return getPointerAddressSpace();
857 /// getIndexedType - Returns the type of the element that would be loaded with
858 /// a load instruction with the specified parameters.
860 /// Null is returned if the indices are invalid for the specified
863 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
864 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
865 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
867 inline op_iterator idx_begin() { return op_begin()+1; }
868 inline const_op_iterator idx_begin() const { return op_begin()+1; }
869 inline op_iterator idx_end() { return op_end(); }
870 inline const_op_iterator idx_end() const { return op_end(); }
872 Value *getPointerOperand() {
873 return getOperand(0);
875 const Value *getPointerOperand() const {
876 return getOperand(0);
878 static unsigned getPointerOperandIndex() {
879 return 0U; // get index for modifying correct operand.
882 /// getPointerOperandType - Method to return the pointer operand as a
884 Type *getPointerOperandType() const {
885 return getPointerOperand()->getType();
888 /// \brief Returns the address space of the pointer operand.
889 unsigned getPointerAddressSpace() const {
890 return getPointerOperandType()->getPointerAddressSpace();
893 /// GetGEPReturnType - Returns the pointer type returned by the GEP
894 /// instruction, which may be a vector of pointers.
895 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
896 Type *PtrTy = PointerType::get(checkGEPType(
897 getIndexedType(Ptr->getType(), IdxList)),
898 Ptr->getType()->getPointerAddressSpace());
900 if (Ptr->getType()->isVectorTy()) {
901 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
902 return VectorType::get(PtrTy, NumElem);
909 unsigned getNumIndices() const { // Note: always non-negative
910 return getNumOperands() - 1;
913 bool hasIndices() const {
914 return getNumOperands() > 1;
917 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
918 /// zeros. If so, the result pointer and the first operand have the same
919 /// value, just potentially different types.
920 bool hasAllZeroIndices() const;
922 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
923 /// constant integers. If so, the result pointer and the first operand have
924 /// a constant offset between them.
925 bool hasAllConstantIndices() const;
927 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
928 /// See LangRef.html for the meaning of inbounds on a getelementptr.
929 void setIsInBounds(bool b = true);
931 /// isInBounds - Determine whether the GEP has the inbounds flag.
932 bool isInBounds() const;
934 /// \brief Accumulate the constant address offset of this GEP if possible.
936 /// This routine accepts an APInt into which it will accumulate the constant
937 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
938 /// all-constant, it returns false and the value of the offset APInt is
939 /// undefined (it is *not* preserved!). The APInt passed into this routine
940 /// must be at least as wide as the IntPtr type for the address space of
941 /// the base GEP pointer.
942 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
944 // Methods for support type inquiry through isa, cast, and dyn_cast:
945 static inline bool classof(const Instruction *I) {
946 return (I->getOpcode() == Instruction::GetElementPtr);
948 static inline bool classof(const Value *V) {
949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
954 struct OperandTraits<GetElementPtrInst> :
955 public VariadicOperandTraits<GetElementPtrInst, 1> {
958 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
959 ArrayRef<Value *> IdxList,
961 const Twine &NameStr,
962 Instruction *InsertBefore)
963 : Instruction(getGEPReturnType(Ptr, IdxList),
965 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
966 Values, InsertBefore) {
967 init(Ptr, IdxList, NameStr);
969 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
970 ArrayRef<Value *> IdxList,
972 const Twine &NameStr,
973 BasicBlock *InsertAtEnd)
974 : Instruction(getGEPReturnType(Ptr, IdxList),
976 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
977 Values, InsertAtEnd) {
978 init(Ptr, IdxList, NameStr);
982 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
985 //===----------------------------------------------------------------------===//
987 //===----------------------------------------------------------------------===//
989 /// This instruction compares its operands according to the predicate given
990 /// to the constructor. It only operates on integers or pointers. The operands
991 /// must be identical types.
992 /// \brief Represent an integer comparison operator.
993 class ICmpInst: public CmpInst {
995 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
996 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
997 "Invalid ICmp predicate value");
998 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
999 "Both operands to ICmp instruction are not of the same type!");
1000 // Check that the operands are the right type
1001 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1002 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1003 "Invalid operand types for ICmp instruction");
1007 /// \brief Clone an identical ICmpInst
1008 ICmpInst *clone_impl() const override;
1010 /// \brief Constructor with insert-before-instruction semantics.
1012 Instruction *InsertBefore, ///< Where to insert
1013 Predicate pred, ///< The predicate to use for the comparison
1014 Value *LHS, ///< The left-hand-side of the expression
1015 Value *RHS, ///< The right-hand-side of the expression
1016 const Twine &NameStr = "" ///< Name of the instruction
1017 ) : CmpInst(makeCmpResultType(LHS->getType()),
1018 Instruction::ICmp, pred, LHS, RHS, NameStr,
1025 /// \brief Constructor with insert-at-end semantics.
1027 BasicBlock &InsertAtEnd, ///< Block to insert into.
1028 Predicate pred, ///< The predicate to use for the comparison
1029 Value *LHS, ///< The left-hand-side of the expression
1030 Value *RHS, ///< The right-hand-side of the expression
1031 const Twine &NameStr = "" ///< Name of the instruction
1032 ) : CmpInst(makeCmpResultType(LHS->getType()),
1033 Instruction::ICmp, pred, LHS, RHS, NameStr,
1040 /// \brief Constructor with no-insertion semantics
1042 Predicate pred, ///< The predicate to use for the comparison
1043 Value *LHS, ///< The left-hand-side of the expression
1044 Value *RHS, ///< The right-hand-side of the expression
1045 const Twine &NameStr = "" ///< Name of the instruction
1046 ) : CmpInst(makeCmpResultType(LHS->getType()),
1047 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1053 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1054 /// @returns the predicate that would be the result if the operand were
1055 /// regarded as signed.
1056 /// \brief Return the signed version of the predicate
1057 Predicate getSignedPredicate() const {
1058 return getSignedPredicate(getPredicate());
1061 /// This is a static version that you can use without an instruction.
1062 /// \brief Return the signed version of the predicate.
1063 static Predicate getSignedPredicate(Predicate pred);
1065 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1066 /// @returns the predicate that would be the result if the operand were
1067 /// regarded as unsigned.
1068 /// \brief Return the unsigned version of the predicate
1069 Predicate getUnsignedPredicate() const {
1070 return getUnsignedPredicate(getPredicate());
1073 /// This is a static version that you can use without an instruction.
1074 /// \brief Return the unsigned version of the predicate.
1075 static Predicate getUnsignedPredicate(Predicate pred);
1077 /// isEquality - Return true if this predicate is either EQ or NE. This also
1078 /// tests for commutativity.
1079 static bool isEquality(Predicate P) {
1080 return P == ICMP_EQ || P == ICMP_NE;
1083 /// isEquality - Return true if this predicate is either EQ or NE. This also
1084 /// tests for commutativity.
1085 bool isEquality() const {
1086 return isEquality(getPredicate());
1089 /// @returns true if the predicate of this ICmpInst is commutative
1090 /// \brief Determine if this relation is commutative.
1091 bool isCommutative() const { return isEquality(); }
1093 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1095 bool isRelational() const {
1096 return !isEquality();
1099 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1101 static bool isRelational(Predicate P) {
1102 return !isEquality(P);
1105 /// Initialize a set of values that all satisfy the predicate with C.
1106 /// \brief Make a ConstantRange for a relation with a constant value.
1107 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1109 /// Exchange the two operands to this instruction in such a way that it does
1110 /// not modify the semantics of the instruction. The predicate value may be
1111 /// changed to retain the same result if the predicate is order dependent
1113 /// \brief Swap operands and adjust predicate.
1114 void swapOperands() {
1115 setPredicate(getSwappedPredicate());
1116 Op<0>().swap(Op<1>());
1119 // Methods for support type inquiry through isa, cast, and dyn_cast:
1120 static inline bool classof(const Instruction *I) {
1121 return I->getOpcode() == Instruction::ICmp;
1123 static inline bool classof(const Value *V) {
1124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1129 //===----------------------------------------------------------------------===//
1131 //===----------------------------------------------------------------------===//
1133 /// This instruction compares its operands according to the predicate given
1134 /// to the constructor. It only operates on floating point values or packed
1135 /// vectors of floating point values. The operands must be identical types.
1136 /// \brief Represents a floating point comparison operator.
1137 class FCmpInst: public CmpInst {
1139 /// \brief Clone an identical FCmpInst
1140 FCmpInst *clone_impl() const override;
1142 /// \brief Constructor with insert-before-instruction semantics.
1144 Instruction *InsertBefore, ///< Where to insert
1145 Predicate pred, ///< The predicate to use for the comparison
1146 Value *LHS, ///< The left-hand-side of the expression
1147 Value *RHS, ///< The right-hand-side of the expression
1148 const Twine &NameStr = "" ///< Name of the instruction
1149 ) : CmpInst(makeCmpResultType(LHS->getType()),
1150 Instruction::FCmp, pred, LHS, RHS, NameStr,
1152 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1153 "Invalid FCmp predicate value");
1154 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1155 "Both operands to FCmp instruction are not of the same type!");
1156 // Check that the operands are the right type
1157 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1158 "Invalid operand types for FCmp instruction");
1161 /// \brief Constructor with insert-at-end semantics.
1163 BasicBlock &InsertAtEnd, ///< Block to insert into.
1164 Predicate pred, ///< The predicate to use for the comparison
1165 Value *LHS, ///< The left-hand-side of the expression
1166 Value *RHS, ///< The right-hand-side of the expression
1167 const Twine &NameStr = "" ///< Name of the instruction
1168 ) : CmpInst(makeCmpResultType(LHS->getType()),
1169 Instruction::FCmp, pred, LHS, RHS, NameStr,
1171 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1172 "Invalid FCmp predicate value");
1173 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1174 "Both operands to FCmp instruction are not of the same type!");
1175 // Check that the operands are the right type
1176 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1177 "Invalid operand types for FCmp instruction");
1180 /// \brief Constructor with no-insertion semantics
1182 Predicate pred, ///< The predicate to use for the comparison
1183 Value *LHS, ///< The left-hand-side of the expression
1184 Value *RHS, ///< The right-hand-side of the expression
1185 const Twine &NameStr = "" ///< Name of the instruction
1186 ) : CmpInst(makeCmpResultType(LHS->getType()),
1187 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1188 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1189 "Invalid FCmp predicate value");
1190 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1191 "Both operands to FCmp instruction are not of the same type!");
1192 // Check that the operands are the right type
1193 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1194 "Invalid operand types for FCmp instruction");
1197 /// @returns true if the predicate of this instruction is EQ or NE.
1198 /// \brief Determine if this is an equality predicate.
1199 bool isEquality() const {
1200 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1201 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1204 /// @returns true if the predicate of this instruction is commutative.
1205 /// \brief Determine if this is a commutative predicate.
1206 bool isCommutative() const {
1207 return isEquality() ||
1208 getPredicate() == FCMP_FALSE ||
1209 getPredicate() == FCMP_TRUE ||
1210 getPredicate() == FCMP_ORD ||
1211 getPredicate() == FCMP_UNO;
1214 /// @returns true if the predicate is relational (not EQ or NE).
1215 /// \brief Determine if this a relational predicate.
1216 bool isRelational() const { return !isEquality(); }
1218 /// Exchange the two operands to this instruction in such a way that it does
1219 /// not modify the semantics of the instruction. The predicate value may be
1220 /// changed to retain the same result if the predicate is order dependent
1222 /// \brief Swap operands and adjust predicate.
1223 void swapOperands() {
1224 setPredicate(getSwappedPredicate());
1225 Op<0>().swap(Op<1>());
1228 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1229 static inline bool classof(const Instruction *I) {
1230 return I->getOpcode() == Instruction::FCmp;
1232 static inline bool classof(const Value *V) {
1233 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1237 //===----------------------------------------------------------------------===//
1238 /// CallInst - This class represents a function call, abstracting a target
1239 /// machine's calling convention. This class uses low bit of the SubClassData
1240 /// field to indicate whether or not this is a tail call. The rest of the bits
1241 /// hold the calling convention of the call.
1243 class CallInst : public Instruction {
1244 AttributeSet AttributeList; ///< parameter attributes for call
1245 CallInst(const CallInst &CI);
1246 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1247 void init(Value *Func, const Twine &NameStr);
1249 /// Construct a CallInst given a range of arguments.
1250 /// \brief Construct a CallInst from a range of arguments
1251 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1252 const Twine &NameStr, Instruction *InsertBefore);
1254 /// Construct a CallInst given a range of arguments.
1255 /// \brief Construct a CallInst from a range of arguments
1256 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1257 const Twine &NameStr, BasicBlock *InsertAtEnd);
1259 explicit CallInst(Value *F, const Twine &NameStr,
1260 Instruction *InsertBefore);
1261 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1263 CallInst *clone_impl() const override;
1265 static CallInst *Create(Value *Func,
1266 ArrayRef<Value *> Args,
1267 const Twine &NameStr = "",
1268 Instruction *InsertBefore = nullptr) {
1269 return new(unsigned(Args.size() + 1))
1270 CallInst(Func, Args, NameStr, InsertBefore);
1272 static CallInst *Create(Value *Func,
1273 ArrayRef<Value *> Args,
1274 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1275 return new(unsigned(Args.size() + 1))
1276 CallInst(Func, Args, NameStr, InsertAtEnd);
1278 static CallInst *Create(Value *F, const Twine &NameStr = "",
1279 Instruction *InsertBefore = nullptr) {
1280 return new(1) CallInst(F, NameStr, InsertBefore);
1282 static CallInst *Create(Value *F, const Twine &NameStr,
1283 BasicBlock *InsertAtEnd) {
1284 return new(1) CallInst(F, NameStr, InsertAtEnd);
1286 /// CreateMalloc - Generate the IR for a call to malloc:
1287 /// 1. Compute the malloc call's argument as the specified type's size,
1288 /// possibly multiplied by the array size if the array size is not
1290 /// 2. Call malloc with that argument.
1291 /// 3. Bitcast the result of the malloc call to the specified type.
1292 static Instruction *CreateMalloc(Instruction *InsertBefore,
1293 Type *IntPtrTy, Type *AllocTy,
1294 Value *AllocSize, Value *ArraySize = nullptr,
1295 Function* MallocF = nullptr,
1296 const Twine &Name = "");
1297 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1298 Type *IntPtrTy, Type *AllocTy,
1299 Value *AllocSize, Value *ArraySize = nullptr,
1300 Function* MallocF = nullptr,
1301 const Twine &Name = "");
1302 /// CreateFree - Generate the IR for a call to the builtin free function.
1303 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1304 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1308 // Note that 'musttail' implies 'tail'.
1309 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1310 TailCallKind getTailCallKind() const {
1311 return TailCallKind(getSubclassDataFromInstruction() & 3);
1313 bool isTailCall() const {
1314 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1316 bool isMustTailCall() const {
1317 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1319 void setTailCall(bool isTC = true) {
1320 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1321 unsigned(isTC ? TCK_Tail : TCK_None));
1323 void setTailCallKind(TailCallKind TCK) {
1324 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1328 /// Provide fast operand accessors
1329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1331 /// getNumArgOperands - Return the number of call arguments.
1333 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1335 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1337 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1338 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1340 /// arg_operands - iteration adapter for range-for loops.
1341 iterator_range<op_iterator> arg_operands() {
1342 // The last operand in the op list is the callee - it's not one of the args
1343 // so we don't want to iterate over it.
1344 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1347 /// arg_operands - iteration adapter for range-for loops.
1348 iterator_range<const_op_iterator> arg_operands() const {
1349 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1352 /// \brief Wrappers for getting the \c Use of a call argument.
1353 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1354 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1356 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1358 CallingConv::ID getCallingConv() const {
1359 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1361 void setCallingConv(CallingConv::ID CC) {
1362 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1363 (static_cast<unsigned>(CC) << 2));
1366 /// getAttributes - Return the parameter attributes for this call.
1368 const AttributeSet &getAttributes() const { return AttributeList; }
1370 /// setAttributes - Set the parameter attributes for this call.
1372 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1374 /// addAttribute - adds the attribute to the list of attributes.
1375 void addAttribute(unsigned i, Attribute::AttrKind attr);
1377 /// removeAttribute - removes the attribute from the list of attributes.
1378 void removeAttribute(unsigned i, Attribute attr);
1380 /// \brief Determine whether this call has the given attribute.
1381 bool hasFnAttr(Attribute::AttrKind A) const {
1382 assert(A != Attribute::NoBuiltin &&
1383 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1384 return hasFnAttrImpl(A);
1387 /// \brief Determine whether the call or the callee has the given attributes.
1388 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1390 /// \brief Extract the alignment for a call or parameter (0=unknown).
1391 unsigned getParamAlignment(unsigned i) const {
1392 return AttributeList.getParamAlignment(i);
1395 /// \brief Extract the number of dereferenceable bytes for a call or
1396 /// parameter (0=unknown).
1397 uint64_t getDereferenceableBytes(unsigned i) const {
1398 return AttributeList.getDereferenceableBytes(i);
1401 /// \brief Return true if the call should not be treated as a call to a
1403 bool isNoBuiltin() const {
1404 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1405 !hasFnAttrImpl(Attribute::Builtin);
1408 /// \brief Return true if the call should not be inlined.
1409 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1410 void setIsNoInline() {
1411 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1414 /// \brief Return true if the call can return twice
1415 bool canReturnTwice() const {
1416 return hasFnAttr(Attribute::ReturnsTwice);
1418 void setCanReturnTwice() {
1419 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1422 /// \brief Determine if the call does not access memory.
1423 bool doesNotAccessMemory() const {
1424 return hasFnAttr(Attribute::ReadNone);
1426 void setDoesNotAccessMemory() {
1427 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1430 /// \brief Determine if the call does not access or only reads memory.
1431 bool onlyReadsMemory() const {
1432 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1434 void setOnlyReadsMemory() {
1435 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1438 /// \brief Determine if the call cannot return.
1439 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1440 void setDoesNotReturn() {
1441 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1444 /// \brief Determine if the call cannot unwind.
1445 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1446 void setDoesNotThrow() {
1447 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1450 /// \brief Determine if the call cannot be duplicated.
1451 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1452 void setCannotDuplicate() {
1453 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1456 /// \brief Determine if the call returns a structure through first
1457 /// pointer argument.
1458 bool hasStructRetAttr() const {
1459 // Be friendly and also check the callee.
1460 return paramHasAttr(1, Attribute::StructRet);
1463 /// \brief Determine if any call argument is an aggregate passed by value.
1464 bool hasByValArgument() const {
1465 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1468 /// getCalledFunction - Return the function called, or null if this is an
1469 /// indirect function invocation.
1471 Function *getCalledFunction() const {
1472 return dyn_cast<Function>(Op<-1>());
1475 /// getCalledValue - Get a pointer to the function that is invoked by this
1477 const Value *getCalledValue() const { return Op<-1>(); }
1478 Value *getCalledValue() { return Op<-1>(); }
1480 /// setCalledFunction - Set the function called.
1481 void setCalledFunction(Value* Fn) {
1485 /// isInlineAsm - Check if this call is an inline asm statement.
1486 bool isInlineAsm() const {
1487 return isa<InlineAsm>(Op<-1>());
1490 // Methods for support type inquiry through isa, cast, and dyn_cast:
1491 static inline bool classof(const Instruction *I) {
1492 return I->getOpcode() == Instruction::Call;
1494 static inline bool classof(const Value *V) {
1495 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1499 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1501 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1502 // method so that subclasses cannot accidentally use it.
1503 void setInstructionSubclassData(unsigned short D) {
1504 Instruction::setInstructionSubclassData(D);
1509 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1512 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1513 const Twine &NameStr, BasicBlock *InsertAtEnd)
1514 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1515 ->getElementType())->getReturnType(),
1517 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1518 unsigned(Args.size() + 1), InsertAtEnd) {
1519 init(Func, Args, NameStr);
1522 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1523 const Twine &NameStr, Instruction *InsertBefore)
1524 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1525 ->getElementType())->getReturnType(),
1527 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1528 unsigned(Args.size() + 1), InsertBefore) {
1529 init(Func, Args, NameStr);
1533 // Note: if you get compile errors about private methods then
1534 // please update your code to use the high-level operand
1535 // interfaces. See line 943 above.
1536 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1538 //===----------------------------------------------------------------------===//
1540 //===----------------------------------------------------------------------===//
1542 /// SelectInst - This class represents the LLVM 'select' instruction.
1544 class SelectInst : public Instruction {
1545 void init(Value *C, Value *S1, Value *S2) {
1546 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1552 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1553 Instruction *InsertBefore)
1554 : Instruction(S1->getType(), Instruction::Select,
1555 &Op<0>(), 3, InsertBefore) {
1559 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1560 BasicBlock *InsertAtEnd)
1561 : Instruction(S1->getType(), Instruction::Select,
1562 &Op<0>(), 3, InsertAtEnd) {
1567 SelectInst *clone_impl() const override;
1569 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1570 const Twine &NameStr = "",
1571 Instruction *InsertBefore = nullptr) {
1572 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1574 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1575 const Twine &NameStr,
1576 BasicBlock *InsertAtEnd) {
1577 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1580 const Value *getCondition() const { return Op<0>(); }
1581 const Value *getTrueValue() const { return Op<1>(); }
1582 const Value *getFalseValue() const { return Op<2>(); }
1583 Value *getCondition() { return Op<0>(); }
1584 Value *getTrueValue() { return Op<1>(); }
1585 Value *getFalseValue() { return Op<2>(); }
1587 /// areInvalidOperands - Return a string if the specified operands are invalid
1588 /// for a select operation, otherwise return null.
1589 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1591 /// Transparently provide more efficient getOperand methods.
1592 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1594 OtherOps getOpcode() const {
1595 return static_cast<OtherOps>(Instruction::getOpcode());
1598 // Methods for support type inquiry through isa, cast, and dyn_cast:
1599 static inline bool classof(const Instruction *I) {
1600 return I->getOpcode() == Instruction::Select;
1602 static inline bool classof(const Value *V) {
1603 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1608 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1611 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1613 //===----------------------------------------------------------------------===//
1615 //===----------------------------------------------------------------------===//
1617 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1618 /// an argument of the specified type given a va_list and increments that list
1620 class VAArgInst : public UnaryInstruction {
1622 VAArgInst *clone_impl() const override;
1625 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1626 Instruction *InsertBefore = nullptr)
1627 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1630 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1631 BasicBlock *InsertAtEnd)
1632 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1636 Value *getPointerOperand() { return getOperand(0); }
1637 const Value *getPointerOperand() const { return getOperand(0); }
1638 static unsigned getPointerOperandIndex() { return 0U; }
1640 // Methods for support type inquiry through isa, cast, and dyn_cast:
1641 static inline bool classof(const Instruction *I) {
1642 return I->getOpcode() == VAArg;
1644 static inline bool classof(const Value *V) {
1645 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1649 //===----------------------------------------------------------------------===//
1650 // ExtractElementInst Class
1651 //===----------------------------------------------------------------------===//
1653 /// ExtractElementInst - This instruction extracts a single (scalar)
1654 /// element from a VectorType value
1656 class ExtractElementInst : public Instruction {
1657 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1658 Instruction *InsertBefore = nullptr);
1659 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1660 BasicBlock *InsertAtEnd);
1662 ExtractElementInst *clone_impl() const override;
1665 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1666 const Twine &NameStr = "",
1667 Instruction *InsertBefore = nullptr) {
1668 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1670 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1671 const Twine &NameStr,
1672 BasicBlock *InsertAtEnd) {
1673 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1676 /// isValidOperands - Return true if an extractelement instruction can be
1677 /// formed with the specified operands.
1678 static bool isValidOperands(const Value *Vec, const Value *Idx);
1680 Value *getVectorOperand() { return Op<0>(); }
1681 Value *getIndexOperand() { return Op<1>(); }
1682 const Value *getVectorOperand() const { return Op<0>(); }
1683 const Value *getIndexOperand() const { return Op<1>(); }
1685 VectorType *getVectorOperandType() const {
1686 return cast<VectorType>(getVectorOperand()->getType());
1690 /// Transparently provide more efficient getOperand methods.
1691 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1693 // Methods for support type inquiry through isa, cast, and dyn_cast:
1694 static inline bool classof(const Instruction *I) {
1695 return I->getOpcode() == Instruction::ExtractElement;
1697 static inline bool classof(const Value *V) {
1698 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1703 struct OperandTraits<ExtractElementInst> :
1704 public FixedNumOperandTraits<ExtractElementInst, 2> {
1707 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1709 //===----------------------------------------------------------------------===//
1710 // InsertElementInst Class
1711 //===----------------------------------------------------------------------===//
1713 /// InsertElementInst - This instruction inserts a single (scalar)
1714 /// element into a VectorType value
1716 class InsertElementInst : public Instruction {
1717 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1718 const Twine &NameStr = "",
1719 Instruction *InsertBefore = nullptr);
1720 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1721 const Twine &NameStr, BasicBlock *InsertAtEnd);
1723 InsertElementInst *clone_impl() const override;
1726 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1727 const Twine &NameStr = "",
1728 Instruction *InsertBefore = nullptr) {
1729 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1731 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1732 const Twine &NameStr,
1733 BasicBlock *InsertAtEnd) {
1734 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1737 /// isValidOperands - Return true if an insertelement instruction can be
1738 /// formed with the specified operands.
1739 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1742 /// getType - Overload to return most specific vector type.
1744 VectorType *getType() const {
1745 return cast<VectorType>(Instruction::getType());
1748 /// Transparently provide more efficient getOperand methods.
1749 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1751 // Methods for support type inquiry through isa, cast, and dyn_cast:
1752 static inline bool classof(const Instruction *I) {
1753 return I->getOpcode() == Instruction::InsertElement;
1755 static inline bool classof(const Value *V) {
1756 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1761 struct OperandTraits<InsertElementInst> :
1762 public FixedNumOperandTraits<InsertElementInst, 3> {
1765 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1767 //===----------------------------------------------------------------------===//
1768 // ShuffleVectorInst Class
1769 //===----------------------------------------------------------------------===//
1771 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1774 class ShuffleVectorInst : public Instruction {
1776 ShuffleVectorInst *clone_impl() const override;
1779 // allocate space for exactly three operands
1780 void *operator new(size_t s) {
1781 return User::operator new(s, 3);
1783 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1784 const Twine &NameStr = "",
1785 Instruction *InsertBefor = nullptr);
1786 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1787 const Twine &NameStr, BasicBlock *InsertAtEnd);
1789 /// isValidOperands - Return true if a shufflevector instruction can be
1790 /// formed with the specified operands.
1791 static bool isValidOperands(const Value *V1, const Value *V2,
1794 /// getType - Overload to return most specific vector type.
1796 VectorType *getType() const {
1797 return cast<VectorType>(Instruction::getType());
1800 /// Transparently provide more efficient getOperand methods.
1801 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1803 Constant *getMask() const {
1804 return cast<Constant>(getOperand(2));
1807 /// getMaskValue - Return the index from the shuffle mask for the specified
1808 /// output result. This is either -1 if the element is undef or a number less
1809 /// than 2*numelements.
1810 static int getMaskValue(Constant *Mask, unsigned i);
1812 int getMaskValue(unsigned i) const {
1813 return getMaskValue(getMask(), i);
1816 /// getShuffleMask - Return the full mask for this instruction, where each
1817 /// element is the element number and undef's are returned as -1.
1818 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1820 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1821 return getShuffleMask(getMask(), Result);
1824 SmallVector<int, 16> getShuffleMask() const {
1825 SmallVector<int, 16> Mask;
1826 getShuffleMask(Mask);
1831 // Methods for support type inquiry through isa, cast, and dyn_cast:
1832 static inline bool classof(const Instruction *I) {
1833 return I->getOpcode() == Instruction::ShuffleVector;
1835 static inline bool classof(const Value *V) {
1836 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1841 struct OperandTraits<ShuffleVectorInst> :
1842 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1845 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1847 //===----------------------------------------------------------------------===//
1848 // ExtractValueInst Class
1849 //===----------------------------------------------------------------------===//
1851 /// ExtractValueInst - This instruction extracts a struct member or array
1852 /// element value from an aggregate value.
1854 class ExtractValueInst : public UnaryInstruction {
1855 SmallVector<unsigned, 4> Indices;
1857 ExtractValueInst(const ExtractValueInst &EVI);
1858 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1860 /// Constructors - Create a extractvalue instruction with a base aggregate
1861 /// value and a list of indices. The first ctor can optionally insert before
1862 /// an existing instruction, the second appends the new instruction to the
1863 /// specified BasicBlock.
1864 inline ExtractValueInst(Value *Agg,
1865 ArrayRef<unsigned> Idxs,
1866 const Twine &NameStr,
1867 Instruction *InsertBefore);
1868 inline ExtractValueInst(Value *Agg,
1869 ArrayRef<unsigned> Idxs,
1870 const Twine &NameStr, BasicBlock *InsertAtEnd);
1872 // allocate space for exactly one operand
1873 void *operator new(size_t s) {
1874 return User::operator new(s, 1);
1877 ExtractValueInst *clone_impl() const override;
1880 static ExtractValueInst *Create(Value *Agg,
1881 ArrayRef<unsigned> Idxs,
1882 const Twine &NameStr = "",
1883 Instruction *InsertBefore = nullptr) {
1885 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1887 static ExtractValueInst *Create(Value *Agg,
1888 ArrayRef<unsigned> Idxs,
1889 const Twine &NameStr,
1890 BasicBlock *InsertAtEnd) {
1891 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1894 /// getIndexedType - Returns the type of the element that would be extracted
1895 /// with an extractvalue instruction with the specified parameters.
1897 /// Null is returned if the indices are invalid for the specified type.
1898 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1900 typedef const unsigned* idx_iterator;
1901 inline idx_iterator idx_begin() const { return Indices.begin(); }
1902 inline idx_iterator idx_end() const { return Indices.end(); }
1904 Value *getAggregateOperand() {
1905 return getOperand(0);
1907 const Value *getAggregateOperand() const {
1908 return getOperand(0);
1910 static unsigned getAggregateOperandIndex() {
1911 return 0U; // get index for modifying correct operand
1914 ArrayRef<unsigned> getIndices() const {
1918 unsigned getNumIndices() const {
1919 return (unsigned)Indices.size();
1922 bool hasIndices() const {
1926 // Methods for support type inquiry through isa, cast, and dyn_cast:
1927 static inline bool classof(const Instruction *I) {
1928 return I->getOpcode() == Instruction::ExtractValue;
1930 static inline bool classof(const Value *V) {
1931 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1935 ExtractValueInst::ExtractValueInst(Value *Agg,
1936 ArrayRef<unsigned> Idxs,
1937 const Twine &NameStr,
1938 Instruction *InsertBefore)
1939 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1940 ExtractValue, Agg, InsertBefore) {
1941 init(Idxs, NameStr);
1943 ExtractValueInst::ExtractValueInst(Value *Agg,
1944 ArrayRef<unsigned> Idxs,
1945 const Twine &NameStr,
1946 BasicBlock *InsertAtEnd)
1947 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1948 ExtractValue, Agg, InsertAtEnd) {
1949 init(Idxs, NameStr);
1953 //===----------------------------------------------------------------------===//
1954 // InsertValueInst Class
1955 //===----------------------------------------------------------------------===//
1957 /// InsertValueInst - This instruction inserts a struct field of array element
1958 /// value into an aggregate value.
1960 class InsertValueInst : public Instruction {
1961 SmallVector<unsigned, 4> Indices;
1963 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1964 InsertValueInst(const InsertValueInst &IVI);
1965 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1966 const Twine &NameStr);
1968 /// Constructors - Create a insertvalue instruction with a base aggregate
1969 /// value, a value to insert, and a list of indices. The first ctor can
1970 /// optionally insert before an existing instruction, the second appends
1971 /// the new instruction to the specified BasicBlock.
1972 inline InsertValueInst(Value *Agg, Value *Val,
1973 ArrayRef<unsigned> Idxs,
1974 const Twine &NameStr,
1975 Instruction *InsertBefore);
1976 inline InsertValueInst(Value *Agg, Value *Val,
1977 ArrayRef<unsigned> Idxs,
1978 const Twine &NameStr, BasicBlock *InsertAtEnd);
1980 /// Constructors - These two constructors are convenience methods because one
1981 /// and two index insertvalue instructions are so common.
1982 InsertValueInst(Value *Agg, Value *Val,
1983 unsigned Idx, const Twine &NameStr = "",
1984 Instruction *InsertBefore = nullptr);
1985 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1986 const Twine &NameStr, BasicBlock *InsertAtEnd);
1988 InsertValueInst *clone_impl() const override;
1990 // allocate space for exactly two operands
1991 void *operator new(size_t s) {
1992 return User::operator new(s, 2);
1995 static InsertValueInst *Create(Value *Agg, Value *Val,
1996 ArrayRef<unsigned> Idxs,
1997 const Twine &NameStr = "",
1998 Instruction *InsertBefore = nullptr) {
1999 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2001 static InsertValueInst *Create(Value *Agg, Value *Val,
2002 ArrayRef<unsigned> Idxs,
2003 const Twine &NameStr,
2004 BasicBlock *InsertAtEnd) {
2005 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2008 /// Transparently provide more efficient getOperand methods.
2009 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2011 typedef const unsigned* idx_iterator;
2012 inline idx_iterator idx_begin() const { return Indices.begin(); }
2013 inline idx_iterator idx_end() const { return Indices.end(); }
2015 Value *getAggregateOperand() {
2016 return getOperand(0);
2018 const Value *getAggregateOperand() const {
2019 return getOperand(0);
2021 static unsigned getAggregateOperandIndex() {
2022 return 0U; // get index for modifying correct operand
2025 Value *getInsertedValueOperand() {
2026 return getOperand(1);
2028 const Value *getInsertedValueOperand() const {
2029 return getOperand(1);
2031 static unsigned getInsertedValueOperandIndex() {
2032 return 1U; // get index for modifying correct operand
2035 ArrayRef<unsigned> getIndices() const {
2039 unsigned getNumIndices() const {
2040 return (unsigned)Indices.size();
2043 bool hasIndices() const {
2047 // Methods for support type inquiry through isa, cast, and dyn_cast:
2048 static inline bool classof(const Instruction *I) {
2049 return I->getOpcode() == Instruction::InsertValue;
2051 static inline bool classof(const Value *V) {
2052 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2057 struct OperandTraits<InsertValueInst> :
2058 public FixedNumOperandTraits<InsertValueInst, 2> {
2061 InsertValueInst::InsertValueInst(Value *Agg,
2063 ArrayRef<unsigned> Idxs,
2064 const Twine &NameStr,
2065 Instruction *InsertBefore)
2066 : Instruction(Agg->getType(), InsertValue,
2067 OperandTraits<InsertValueInst>::op_begin(this),
2069 init(Agg, Val, Idxs, NameStr);
2071 InsertValueInst::InsertValueInst(Value *Agg,
2073 ArrayRef<unsigned> Idxs,
2074 const Twine &NameStr,
2075 BasicBlock *InsertAtEnd)
2076 : Instruction(Agg->getType(), InsertValue,
2077 OperandTraits<InsertValueInst>::op_begin(this),
2079 init(Agg, Val, Idxs, NameStr);
2082 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2084 //===----------------------------------------------------------------------===//
2086 //===----------------------------------------------------------------------===//
2088 // PHINode - The PHINode class is used to represent the magical mystical PHI
2089 // node, that can not exist in nature, but can be synthesized in a computer
2090 // scientist's overactive imagination.
2092 class PHINode : public Instruction {
2093 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2094 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2095 /// the number actually in use.
2096 unsigned ReservedSpace;
2097 PHINode(const PHINode &PN);
2098 // allocate space for exactly zero operands
2099 void *operator new(size_t s) {
2100 return User::operator new(s, 0);
2102 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2103 const Twine &NameStr = "",
2104 Instruction *InsertBefore = nullptr)
2105 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2106 ReservedSpace(NumReservedValues) {
2108 OperandList = allocHungoffUses(ReservedSpace);
2111 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2112 BasicBlock *InsertAtEnd)
2113 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2114 ReservedSpace(NumReservedValues) {
2116 OperandList = allocHungoffUses(ReservedSpace);
2119 // allocHungoffUses - this is more complicated than the generic
2120 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2121 // values and pointers to the incoming blocks, all in one allocation.
2122 Use *allocHungoffUses(unsigned) const;
2124 PHINode *clone_impl() const override;
2126 /// Constructors - NumReservedValues is a hint for the number of incoming
2127 /// edges that this phi node will have (use 0 if you really have no idea).
2128 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2129 const Twine &NameStr = "",
2130 Instruction *InsertBefore = nullptr) {
2131 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2133 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2134 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2135 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2139 /// Provide fast operand accessors
2140 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2142 // Block iterator interface. This provides access to the list of incoming
2143 // basic blocks, which parallels the list of incoming values.
2145 typedef BasicBlock **block_iterator;
2146 typedef BasicBlock * const *const_block_iterator;
2148 block_iterator block_begin() {
2150 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2151 return reinterpret_cast<block_iterator>(ref + 1);
2154 const_block_iterator block_begin() const {
2155 const Use::UserRef *ref =
2156 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2157 return reinterpret_cast<const_block_iterator>(ref + 1);
2160 block_iterator block_end() {
2161 return block_begin() + getNumOperands();
2164 const_block_iterator block_end() const {
2165 return block_begin() + getNumOperands();
2168 /// getNumIncomingValues - Return the number of incoming edges
2170 unsigned getNumIncomingValues() const { return getNumOperands(); }
2172 /// getIncomingValue - Return incoming value number x
2174 Value *getIncomingValue(unsigned i) const {
2175 return getOperand(i);
2177 void setIncomingValue(unsigned i, Value *V) {
2180 static unsigned getOperandNumForIncomingValue(unsigned i) {
2183 static unsigned getIncomingValueNumForOperand(unsigned i) {
2187 /// getIncomingBlock - Return incoming basic block number @p i.
2189 BasicBlock *getIncomingBlock(unsigned i) const {
2190 return block_begin()[i];
2193 /// getIncomingBlock - Return incoming basic block corresponding
2194 /// to an operand of the PHI.
2196 BasicBlock *getIncomingBlock(const Use &U) const {
2197 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2198 return getIncomingBlock(unsigned(&U - op_begin()));
2201 /// getIncomingBlock - Return incoming basic block corresponding
2202 /// to value use iterator.
2204 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2205 return getIncomingBlock(I.getUse());
2208 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2209 block_begin()[i] = BB;
2212 /// addIncoming - Add an incoming value to the end of the PHI list
2214 void addIncoming(Value *V, BasicBlock *BB) {
2215 assert(V && "PHI node got a null value!");
2216 assert(BB && "PHI node got a null basic block!");
2217 assert(getType() == V->getType() &&
2218 "All operands to PHI node must be the same type as the PHI node!");
2219 if (NumOperands == ReservedSpace)
2220 growOperands(); // Get more space!
2221 // Initialize some new operands.
2223 setIncomingValue(NumOperands - 1, V);
2224 setIncomingBlock(NumOperands - 1, BB);
2227 /// removeIncomingValue - Remove an incoming value. This is useful if a
2228 /// predecessor basic block is deleted. The value removed is returned.
2230 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2231 /// is true), the PHI node is destroyed and any uses of it are replaced with
2232 /// dummy values. The only time there should be zero incoming values to a PHI
2233 /// node is when the block is dead, so this strategy is sound.
2235 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2237 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2238 int Idx = getBasicBlockIndex(BB);
2239 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2240 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2243 /// getBasicBlockIndex - Return the first index of the specified basic
2244 /// block in the value list for this PHI. Returns -1 if no instance.
2246 int getBasicBlockIndex(const BasicBlock *BB) const {
2247 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2248 if (block_begin()[i] == BB)
2253 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2254 int Idx = getBasicBlockIndex(BB);
2255 assert(Idx >= 0 && "Invalid basic block argument!");
2256 return getIncomingValue(Idx);
2259 /// hasConstantValue - If the specified PHI node always merges together the
2260 /// same value, return the value, otherwise return null.
2261 Value *hasConstantValue() const;
2263 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2264 static inline bool classof(const Instruction *I) {
2265 return I->getOpcode() == Instruction::PHI;
2267 static inline bool classof(const Value *V) {
2268 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2271 void growOperands();
2275 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2278 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2280 //===----------------------------------------------------------------------===//
2281 // LandingPadInst Class
2282 //===----------------------------------------------------------------------===//
2284 //===---------------------------------------------------------------------------
2285 /// LandingPadInst - The landingpad instruction holds all of the information
2286 /// necessary to generate correct exception handling. The landingpad instruction
2287 /// cannot be moved from the top of a landing pad block, which itself is
2288 /// accessible only from the 'unwind' edge of an invoke. This uses the
2289 /// SubclassData field in Value to store whether or not the landingpad is a
2292 class LandingPadInst : public Instruction {
2293 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2294 /// the number actually in use.
2295 unsigned ReservedSpace;
2296 LandingPadInst(const LandingPadInst &LP);
2298 enum ClauseType { Catch, Filter };
2300 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2301 // Allocate space for exactly zero operands.
2302 void *operator new(size_t s) {
2303 return User::operator new(s, 0);
2305 void growOperands(unsigned Size);
2306 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2308 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2309 unsigned NumReservedValues, const Twine &NameStr,
2310 Instruction *InsertBefore);
2311 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2312 unsigned NumReservedValues, const Twine &NameStr,
2313 BasicBlock *InsertAtEnd);
2315 LandingPadInst *clone_impl() const override;
2317 /// Constructors - NumReservedClauses is a hint for the number of incoming
2318 /// clauses that this landingpad will have (use 0 if you really have no idea).
2319 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2320 unsigned NumReservedClauses,
2321 const Twine &NameStr = "",
2322 Instruction *InsertBefore = nullptr);
2323 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2324 unsigned NumReservedClauses,
2325 const Twine &NameStr, BasicBlock *InsertAtEnd);
2328 /// Provide fast operand accessors
2329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2331 /// getPersonalityFn - Get the personality function associated with this
2333 Value *getPersonalityFn() const { return getOperand(0); }
2335 /// isCleanup - Return 'true' if this landingpad instruction is a
2336 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2337 /// doesn't catch the exception.
2338 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2340 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2341 void setCleanup(bool V) {
2342 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2346 /// Add a catch or filter clause to the landing pad.
2347 void addClause(Constant *ClauseVal);
2349 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2350 /// determine what type of clause this is.
2351 Constant *getClause(unsigned Idx) const {
2352 return cast<Constant>(OperandList[Idx + 1]);
2355 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2356 bool isCatch(unsigned Idx) const {
2357 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2360 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2361 bool isFilter(unsigned Idx) const {
2362 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2365 /// getNumClauses - Get the number of clauses for this landing pad.
2366 unsigned getNumClauses() const { return getNumOperands() - 1; }
2368 /// reserveClauses - Grow the size of the operand list to accommodate the new
2369 /// number of clauses.
2370 void reserveClauses(unsigned Size) { growOperands(Size); }
2372 // Methods for support type inquiry through isa, cast, and dyn_cast:
2373 static inline bool classof(const Instruction *I) {
2374 return I->getOpcode() == Instruction::LandingPad;
2376 static inline bool classof(const Value *V) {
2377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2382 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2387 //===----------------------------------------------------------------------===//
2389 //===----------------------------------------------------------------------===//
2391 //===---------------------------------------------------------------------------
2392 /// ReturnInst - Return a value (possibly void), from a function. Execution
2393 /// does not continue in this function any longer.
2395 class ReturnInst : public TerminatorInst {
2396 ReturnInst(const ReturnInst &RI);
2399 // ReturnInst constructors:
2400 // ReturnInst() - 'ret void' instruction
2401 // ReturnInst( null) - 'ret void' instruction
2402 // ReturnInst(Value* X) - 'ret X' instruction
2403 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2404 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2405 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2406 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2408 // NOTE: If the Value* passed is of type void then the constructor behaves as
2409 // if it was passed NULL.
2410 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2411 Instruction *InsertBefore = nullptr);
2412 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2413 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2415 ReturnInst *clone_impl() const override;
2417 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2418 Instruction *InsertBefore = nullptr) {
2419 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2421 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2422 BasicBlock *InsertAtEnd) {
2423 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2425 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2426 return new(0) ReturnInst(C, InsertAtEnd);
2428 virtual ~ReturnInst();
2430 /// Provide fast operand accessors
2431 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2433 /// Convenience accessor. Returns null if there is no return value.
2434 Value *getReturnValue() const {
2435 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2438 unsigned getNumSuccessors() const { return 0; }
2440 // Methods for support type inquiry through isa, cast, and dyn_cast:
2441 static inline bool classof(const Instruction *I) {
2442 return (I->getOpcode() == Instruction::Ret);
2444 static inline bool classof(const Value *V) {
2445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2448 BasicBlock *getSuccessorV(unsigned idx) const override;
2449 unsigned getNumSuccessorsV() const override;
2450 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2454 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2457 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2459 //===----------------------------------------------------------------------===//
2461 //===----------------------------------------------------------------------===//
2463 //===---------------------------------------------------------------------------
2464 /// BranchInst - Conditional or Unconditional Branch instruction.
2466 class BranchInst : public TerminatorInst {
2467 /// Ops list - Branches are strange. The operands are ordered:
2468 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2469 /// they don't have to check for cond/uncond branchness. These are mostly
2470 /// accessed relative from op_end().
2471 BranchInst(const BranchInst &BI);
2473 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2474 // BranchInst(BB *B) - 'br B'
2475 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2476 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2477 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2478 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2479 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2480 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2481 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2482 Instruction *InsertBefore = nullptr);
2483 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2484 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2485 BasicBlock *InsertAtEnd);
2487 BranchInst *clone_impl() const override;
2489 static BranchInst *Create(BasicBlock *IfTrue,
2490 Instruction *InsertBefore = nullptr) {
2491 return new(1) BranchInst(IfTrue, InsertBefore);
2493 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2494 Value *Cond, Instruction *InsertBefore = nullptr) {
2495 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2497 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2498 return new(1) BranchInst(IfTrue, InsertAtEnd);
2500 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2501 Value *Cond, BasicBlock *InsertAtEnd) {
2502 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2505 /// Transparently provide more efficient getOperand methods.
2506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2508 bool isUnconditional() const { return getNumOperands() == 1; }
2509 bool isConditional() const { return getNumOperands() == 3; }
2511 Value *getCondition() const {
2512 assert(isConditional() && "Cannot get condition of an uncond branch!");
2516 void setCondition(Value *V) {
2517 assert(isConditional() && "Cannot set condition of unconditional branch!");
2521 unsigned getNumSuccessors() const { return 1+isConditional(); }
2523 BasicBlock *getSuccessor(unsigned i) const {
2524 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2525 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2528 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2529 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2530 *(&Op<-1>() - idx) = (Value*)NewSucc;
2533 /// \brief Swap the successors of this branch instruction.
2535 /// Swaps the successors of the branch instruction. This also swaps any
2536 /// branch weight metadata associated with the instruction so that it
2537 /// continues to map correctly to each operand.
2538 void swapSuccessors();
2540 // Methods for support type inquiry through isa, cast, and dyn_cast:
2541 static inline bool classof(const Instruction *I) {
2542 return (I->getOpcode() == Instruction::Br);
2544 static inline bool classof(const Value *V) {
2545 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2548 BasicBlock *getSuccessorV(unsigned idx) const override;
2549 unsigned getNumSuccessorsV() const override;
2550 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2554 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2557 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2559 //===----------------------------------------------------------------------===//
2561 //===----------------------------------------------------------------------===//
2563 //===---------------------------------------------------------------------------
2564 /// SwitchInst - Multiway switch
2566 class SwitchInst : public TerminatorInst {
2567 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2568 unsigned ReservedSpace;
2569 // Operand[0] = Value to switch on
2570 // Operand[1] = Default basic block destination
2571 // Operand[2n ] = Value to match
2572 // Operand[2n+1] = BasicBlock to go to on match
2573 SwitchInst(const SwitchInst &SI);
2574 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2575 void growOperands();
2576 // allocate space for exactly zero operands
2577 void *operator new(size_t s) {
2578 return User::operator new(s, 0);
2580 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2581 /// switch on and a default destination. The number of additional cases can
2582 /// be specified here to make memory allocation more efficient. This
2583 /// constructor can also autoinsert before another instruction.
2584 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2585 Instruction *InsertBefore);
2587 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2588 /// switch on and a default destination. The number of additional cases can
2589 /// be specified here to make memory allocation more efficient. This
2590 /// constructor also autoinserts at the end of the specified BasicBlock.
2591 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2592 BasicBlock *InsertAtEnd);
2594 SwitchInst *clone_impl() const override;
2598 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2600 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2601 class CaseIteratorT {
2609 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2611 /// Initializes case iterator for given SwitchInst and for given
2613 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2618 /// Initializes case iterator for given SwitchInst and for given
2619 /// TerminatorInst's successor index.
2620 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2621 assert(SuccessorIndex < SI->getNumSuccessors() &&
2622 "Successor index # out of range!");
2623 return SuccessorIndex != 0 ?
2624 Self(SI, SuccessorIndex - 1) :
2625 Self(SI, DefaultPseudoIndex);
2628 /// Resolves case value for current case.
2629 ConstantIntTy *getCaseValue() {
2630 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2631 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2634 /// Resolves successor for current case.
2635 BasicBlockTy *getCaseSuccessor() {
2636 assert((Index < SI->getNumCases() ||
2637 Index == DefaultPseudoIndex) &&
2638 "Index out the number of cases.");
2639 return SI->getSuccessor(getSuccessorIndex());
2642 /// Returns number of current case.
2643 unsigned getCaseIndex() const { return Index; }
2645 /// Returns TerminatorInst's successor index for current case successor.
2646 unsigned getSuccessorIndex() const {
2647 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2648 "Index out the number of cases.");
2649 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2653 // Check index correctness after increment.
2654 // Note: Index == getNumCases() means end().
2655 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2659 Self operator++(int) {
2665 // Check index correctness after decrement.
2666 // Note: Index == getNumCases() means end().
2667 // Also allow "-1" iterator here. That will became valid after ++.
2668 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2669 "Index out the number of cases.");
2673 Self operator--(int) {
2678 bool operator==(const Self& RHS) const {
2679 assert(RHS.SI == SI && "Incompatible operators.");
2680 return RHS.Index == Index;
2682 bool operator!=(const Self& RHS) const {
2683 assert(RHS.SI == SI && "Incompatible operators.");
2684 return RHS.Index != Index;
2691 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2694 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2696 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2700 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2701 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2703 /// Sets the new value for current case.
2704 void setValue(ConstantInt *V) {
2705 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2706 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2709 /// Sets the new successor for current case.
2710 void setSuccessor(BasicBlock *S) {
2711 SI->setSuccessor(getSuccessorIndex(), S);
2715 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2717 Instruction *InsertBefore = nullptr) {
2718 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2720 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2721 unsigned NumCases, BasicBlock *InsertAtEnd) {
2722 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2727 /// Provide fast operand accessors
2728 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2730 // Accessor Methods for Switch stmt
2731 Value *getCondition() const { return getOperand(0); }
2732 void setCondition(Value *V) { setOperand(0, V); }
2734 BasicBlock *getDefaultDest() const {
2735 return cast<BasicBlock>(getOperand(1));
2738 void setDefaultDest(BasicBlock *DefaultCase) {
2739 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2742 /// getNumCases - return the number of 'cases' in this switch instruction,
2743 /// except the default case
2744 unsigned getNumCases() const {
2745 return getNumOperands()/2 - 1;
2748 /// Returns a read/write iterator that points to the first
2749 /// case in SwitchInst.
2750 CaseIt case_begin() {
2751 return CaseIt(this, 0);
2753 /// Returns a read-only iterator that points to the first
2754 /// case in the SwitchInst.
2755 ConstCaseIt case_begin() const {
2756 return ConstCaseIt(this, 0);
2759 /// Returns a read/write iterator that points one past the last
2760 /// in the SwitchInst.
2762 return CaseIt(this, getNumCases());
2764 /// Returns a read-only iterator that points one past the last
2765 /// in the SwitchInst.
2766 ConstCaseIt case_end() const {
2767 return ConstCaseIt(this, getNumCases());
2770 /// cases - iteration adapter for range-for loops.
2771 iterator_range<CaseIt> cases() {
2772 return iterator_range<CaseIt>(case_begin(), case_end());
2775 /// cases - iteration adapter for range-for loops.
2776 iterator_range<ConstCaseIt> cases() const {
2777 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2780 /// Returns an iterator that points to the default case.
2781 /// Note: this iterator allows to resolve successor only. Attempt
2782 /// to resolve case value causes an assertion.
2783 /// Also note, that increment and decrement also causes an assertion and
2784 /// makes iterator invalid.
2785 CaseIt case_default() {
2786 return CaseIt(this, DefaultPseudoIndex);
2788 ConstCaseIt case_default() const {
2789 return ConstCaseIt(this, DefaultPseudoIndex);
2792 /// findCaseValue - Search all of the case values for the specified constant.
2793 /// If it is explicitly handled, return the case iterator of it, otherwise
2794 /// return default case iterator to indicate
2795 /// that it is handled by the default handler.
2796 CaseIt findCaseValue(const ConstantInt *C) {
2797 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2798 if (i.getCaseValue() == C)
2800 return case_default();
2802 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2803 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2804 if (i.getCaseValue() == C)
2806 return case_default();
2809 /// findCaseDest - Finds the unique case value for a given successor. Returns
2810 /// null if the successor is not found, not unique, or is the default case.
2811 ConstantInt *findCaseDest(BasicBlock *BB) {
2812 if (BB == getDefaultDest()) return nullptr;
2814 ConstantInt *CI = nullptr;
2815 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2816 if (i.getCaseSuccessor() == BB) {
2817 if (CI) return nullptr; // Multiple cases lead to BB.
2818 else CI = i.getCaseValue();
2824 /// addCase - Add an entry to the switch instruction...
2826 /// This action invalidates case_end(). Old case_end() iterator will
2827 /// point to the added case.
2828 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2830 /// removeCase - This method removes the specified case and its successor
2831 /// from the switch instruction. Note that this operation may reorder the
2832 /// remaining cases at index idx and above.
2834 /// This action invalidates iterators for all cases following the one removed,
2835 /// including the case_end() iterator.
2836 void removeCase(CaseIt i);
2838 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2839 BasicBlock *getSuccessor(unsigned idx) const {
2840 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2841 return cast<BasicBlock>(getOperand(idx*2+1));
2843 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2844 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2845 setOperand(idx*2+1, (Value*)NewSucc);
2848 // Methods for support type inquiry through isa, cast, and dyn_cast:
2849 static inline bool classof(const Instruction *I) {
2850 return I->getOpcode() == Instruction::Switch;
2852 static inline bool classof(const Value *V) {
2853 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2856 BasicBlock *getSuccessorV(unsigned idx) const override;
2857 unsigned getNumSuccessorsV() const override;
2858 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2862 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2865 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2868 //===----------------------------------------------------------------------===//
2869 // IndirectBrInst Class
2870 //===----------------------------------------------------------------------===//
2872 //===---------------------------------------------------------------------------
2873 /// IndirectBrInst - Indirect Branch Instruction.
2875 class IndirectBrInst : public TerminatorInst {
2876 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2877 unsigned ReservedSpace;
2878 // Operand[0] = Value to switch on
2879 // Operand[1] = Default basic block destination
2880 // Operand[2n ] = Value to match
2881 // Operand[2n+1] = BasicBlock to go to on match
2882 IndirectBrInst(const IndirectBrInst &IBI);
2883 void init(Value *Address, unsigned NumDests);
2884 void growOperands();
2885 // allocate space for exactly zero operands
2886 void *operator new(size_t s) {
2887 return User::operator new(s, 0);
2889 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2890 /// Address to jump to. The number of expected destinations can be specified
2891 /// here to make memory allocation more efficient. This constructor can also
2892 /// autoinsert before another instruction.
2893 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2895 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2896 /// Address to jump to. The number of expected destinations can be specified
2897 /// here to make memory allocation more efficient. This constructor also
2898 /// autoinserts at the end of the specified BasicBlock.
2899 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2901 IndirectBrInst *clone_impl() const override;
2903 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2904 Instruction *InsertBefore = nullptr) {
2905 return new IndirectBrInst(Address, NumDests, InsertBefore);
2907 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2908 BasicBlock *InsertAtEnd) {
2909 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2913 /// Provide fast operand accessors.
2914 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2916 // Accessor Methods for IndirectBrInst instruction.
2917 Value *getAddress() { return getOperand(0); }
2918 const Value *getAddress() const { return getOperand(0); }
2919 void setAddress(Value *V) { setOperand(0, V); }
2922 /// getNumDestinations - return the number of possible destinations in this
2923 /// indirectbr instruction.
2924 unsigned getNumDestinations() const { return getNumOperands()-1; }
2926 /// getDestination - Return the specified destination.
2927 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2928 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2930 /// addDestination - Add a destination.
2932 void addDestination(BasicBlock *Dest);
2934 /// removeDestination - This method removes the specified successor from the
2935 /// indirectbr instruction.
2936 void removeDestination(unsigned i);
2938 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2939 BasicBlock *getSuccessor(unsigned i) const {
2940 return cast<BasicBlock>(getOperand(i+1));
2942 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2943 setOperand(i+1, (Value*)NewSucc);
2946 // Methods for support type inquiry through isa, cast, and dyn_cast:
2947 static inline bool classof(const Instruction *I) {
2948 return I->getOpcode() == Instruction::IndirectBr;
2950 static inline bool classof(const Value *V) {
2951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2954 BasicBlock *getSuccessorV(unsigned idx) const override;
2955 unsigned getNumSuccessorsV() const override;
2956 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2960 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2963 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2966 //===----------------------------------------------------------------------===//
2968 //===----------------------------------------------------------------------===//
2970 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2971 /// calling convention of the call.
2973 class InvokeInst : public TerminatorInst {
2974 AttributeSet AttributeList;
2975 InvokeInst(const InvokeInst &BI);
2976 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2977 ArrayRef<Value *> Args, const Twine &NameStr);
2979 /// Construct an InvokeInst given a range of arguments.
2981 /// \brief Construct an InvokeInst from a range of arguments
2982 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2983 ArrayRef<Value *> Args, unsigned Values,
2984 const Twine &NameStr, Instruction *InsertBefore);
2986 /// Construct an InvokeInst given a range of arguments.
2988 /// \brief Construct an InvokeInst from a range of arguments
2989 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2990 ArrayRef<Value *> Args, unsigned Values,
2991 const Twine &NameStr, BasicBlock *InsertAtEnd);
2993 InvokeInst *clone_impl() const override;
2995 static InvokeInst *Create(Value *Func,
2996 BasicBlock *IfNormal, BasicBlock *IfException,
2997 ArrayRef<Value *> Args, const Twine &NameStr = "",
2998 Instruction *InsertBefore = nullptr) {
2999 unsigned Values = unsigned(Args.size()) + 3;
3000 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3001 Values, NameStr, InsertBefore);
3003 static InvokeInst *Create(Value *Func,
3004 BasicBlock *IfNormal, BasicBlock *IfException,
3005 ArrayRef<Value *> Args, const Twine &NameStr,
3006 BasicBlock *InsertAtEnd) {
3007 unsigned Values = unsigned(Args.size()) + 3;
3008 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3009 Values, NameStr, InsertAtEnd);
3012 /// Provide fast operand accessors
3013 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3015 /// getNumArgOperands - Return the number of invoke arguments.
3017 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3019 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3021 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3022 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3024 /// arg_operands - iteration adapter for range-for loops.
3025 iterator_range<op_iterator> arg_operands() {
3026 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3029 /// arg_operands - iteration adapter for range-for loops.
3030 iterator_range<const_op_iterator> arg_operands() const {
3031 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3034 /// \brief Wrappers for getting the \c Use of a invoke argument.
3035 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3036 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3038 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3040 CallingConv::ID getCallingConv() const {
3041 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3043 void setCallingConv(CallingConv::ID CC) {
3044 setInstructionSubclassData(static_cast<unsigned>(CC));
3047 /// getAttributes - Return the parameter attributes for this invoke.
3049 const AttributeSet &getAttributes() const { return AttributeList; }
3051 /// setAttributes - Set the parameter attributes for this invoke.
3053 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3055 /// addAttribute - adds the attribute to the list of attributes.
3056 void addAttribute(unsigned i, Attribute::AttrKind attr);
3058 /// removeAttribute - removes the attribute from the list of attributes.
3059 void removeAttribute(unsigned i, Attribute attr);
3061 /// \brief Determine whether this call has the given attribute.
3062 bool hasFnAttr(Attribute::AttrKind A) const {
3063 assert(A != Attribute::NoBuiltin &&
3064 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3065 return hasFnAttrImpl(A);
3068 /// \brief Determine whether the call or the callee has the given attributes.
3069 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3071 /// \brief Extract the alignment for a call or parameter (0=unknown).
3072 unsigned getParamAlignment(unsigned i) const {
3073 return AttributeList.getParamAlignment(i);
3076 /// \brief Extract the number of dereferenceable bytes for a call or
3077 /// parameter (0=unknown).
3078 uint64_t getDereferenceableBytes(unsigned i) const {
3079 return AttributeList.getDereferenceableBytes(i);
3082 /// \brief Return true if the call should not be treated as a call to a
3084 bool isNoBuiltin() const {
3085 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3086 // to check it by hand.
3087 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3088 !hasFnAttrImpl(Attribute::Builtin);
3091 /// \brief Return true if the call should not be inlined.
3092 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3093 void setIsNoInline() {
3094 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3097 /// \brief Determine if the call does not access memory.
3098 bool doesNotAccessMemory() const {
3099 return hasFnAttr(Attribute::ReadNone);
3101 void setDoesNotAccessMemory() {
3102 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3105 /// \brief Determine if the call does not access or only reads memory.
3106 bool onlyReadsMemory() const {
3107 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3109 void setOnlyReadsMemory() {
3110 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3113 /// \brief Determine if the call cannot return.
3114 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3115 void setDoesNotReturn() {
3116 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3119 /// \brief Determine if the call cannot unwind.
3120 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3121 void setDoesNotThrow() {
3122 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3125 /// \brief Determine if the invoke cannot be duplicated.
3126 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3127 void setCannotDuplicate() {
3128 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3131 /// \brief Determine if the call returns a structure through first
3132 /// pointer argument.
3133 bool hasStructRetAttr() const {
3134 // Be friendly and also check the callee.
3135 return paramHasAttr(1, Attribute::StructRet);
3138 /// \brief Determine if any call argument is an aggregate passed by value.
3139 bool hasByValArgument() const {
3140 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3143 /// getCalledFunction - Return the function called, or null if this is an
3144 /// indirect function invocation.
3146 Function *getCalledFunction() const {
3147 return dyn_cast<Function>(Op<-3>());
3150 /// getCalledValue - Get a pointer to the function that is invoked by this
3152 const Value *getCalledValue() const { return Op<-3>(); }
3153 Value *getCalledValue() { return Op<-3>(); }
3155 /// setCalledFunction - Set the function called.
3156 void setCalledFunction(Value* Fn) {
3160 // get*Dest - Return the destination basic blocks...
3161 BasicBlock *getNormalDest() const {
3162 return cast<BasicBlock>(Op<-2>());
3164 BasicBlock *getUnwindDest() const {
3165 return cast<BasicBlock>(Op<-1>());
3167 void setNormalDest(BasicBlock *B) {
3168 Op<-2>() = reinterpret_cast<Value*>(B);
3170 void setUnwindDest(BasicBlock *B) {
3171 Op<-1>() = reinterpret_cast<Value*>(B);
3174 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3175 /// block (the unwind destination).
3176 LandingPadInst *getLandingPadInst() const;
3178 BasicBlock *getSuccessor(unsigned i) const {
3179 assert(i < 2 && "Successor # out of range for invoke!");
3180 return i == 0 ? getNormalDest() : getUnwindDest();
3183 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3184 assert(idx < 2 && "Successor # out of range for invoke!");
3185 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3188 unsigned getNumSuccessors() const { return 2; }
3190 // Methods for support type inquiry through isa, cast, and dyn_cast:
3191 static inline bool classof(const Instruction *I) {
3192 return (I->getOpcode() == Instruction::Invoke);
3194 static inline bool classof(const Value *V) {
3195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3199 BasicBlock *getSuccessorV(unsigned idx) const override;
3200 unsigned getNumSuccessorsV() const override;
3201 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3203 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3205 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3206 // method so that subclasses cannot accidentally use it.
3207 void setInstructionSubclassData(unsigned short D) {
3208 Instruction::setInstructionSubclassData(D);
3213 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3216 InvokeInst::InvokeInst(Value *Func,
3217 BasicBlock *IfNormal, BasicBlock *IfException,
3218 ArrayRef<Value *> Args, unsigned Values,
3219 const Twine &NameStr, Instruction *InsertBefore)
3220 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3221 ->getElementType())->getReturnType(),
3222 Instruction::Invoke,
3223 OperandTraits<InvokeInst>::op_end(this) - Values,
3224 Values, InsertBefore) {
3225 init(Func, IfNormal, IfException, Args, NameStr);
3227 InvokeInst::InvokeInst(Value *Func,
3228 BasicBlock *IfNormal, BasicBlock *IfException,
3229 ArrayRef<Value *> Args, unsigned Values,
3230 const Twine &NameStr, BasicBlock *InsertAtEnd)
3231 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3232 ->getElementType())->getReturnType(),
3233 Instruction::Invoke,
3234 OperandTraits<InvokeInst>::op_end(this) - Values,
3235 Values, InsertAtEnd) {
3236 init(Func, IfNormal, IfException, Args, NameStr);
3239 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3241 //===----------------------------------------------------------------------===//
3243 //===----------------------------------------------------------------------===//
3245 //===---------------------------------------------------------------------------
3246 /// ResumeInst - Resume the propagation of an exception.
3248 class ResumeInst : public TerminatorInst {
3249 ResumeInst(const ResumeInst &RI);
3251 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3252 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3254 ResumeInst *clone_impl() const override;
3256 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3257 return new(1) ResumeInst(Exn, InsertBefore);
3259 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3260 return new(1) ResumeInst(Exn, InsertAtEnd);
3263 /// Provide fast operand accessors
3264 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3266 /// Convenience accessor.
3267 Value *getValue() const { return Op<0>(); }
3269 unsigned getNumSuccessors() const { return 0; }
3271 // Methods for support type inquiry through isa, cast, and dyn_cast:
3272 static inline bool classof(const Instruction *I) {
3273 return I->getOpcode() == Instruction::Resume;
3275 static inline bool classof(const Value *V) {
3276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3279 BasicBlock *getSuccessorV(unsigned idx) const override;
3280 unsigned getNumSuccessorsV() const override;
3281 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3285 struct OperandTraits<ResumeInst> :
3286 public FixedNumOperandTraits<ResumeInst, 1> {
3289 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3291 //===----------------------------------------------------------------------===//
3292 // UnreachableInst Class
3293 //===----------------------------------------------------------------------===//
3295 //===---------------------------------------------------------------------------
3296 /// UnreachableInst - This function has undefined behavior. In particular, the
3297 /// presence of this instruction indicates some higher level knowledge that the
3298 /// end of the block cannot be reached.
3300 class UnreachableInst : public TerminatorInst {
3301 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3303 UnreachableInst *clone_impl() const override;
3306 // allocate space for exactly zero operands
3307 void *operator new(size_t s) {
3308 return User::operator new(s, 0);
3310 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3311 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3313 unsigned getNumSuccessors() const { return 0; }
3315 // Methods for support type inquiry through isa, cast, and dyn_cast:
3316 static inline bool classof(const Instruction *I) {
3317 return I->getOpcode() == Instruction::Unreachable;
3319 static inline bool classof(const Value *V) {
3320 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3323 BasicBlock *getSuccessorV(unsigned idx) const override;
3324 unsigned getNumSuccessorsV() const override;
3325 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3328 //===----------------------------------------------------------------------===//
3330 //===----------------------------------------------------------------------===//
3332 /// \brief This class represents a truncation of integer types.
3333 class TruncInst : public CastInst {
3335 /// \brief Clone an identical TruncInst
3336 TruncInst *clone_impl() const override;
3339 /// \brief Constructor with insert-before-instruction semantics
3341 Value *S, ///< The value to be truncated
3342 Type *Ty, ///< The (smaller) type to truncate to
3343 const Twine &NameStr = "", ///< A name for the new instruction
3344 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3347 /// \brief Constructor with insert-at-end-of-block semantics
3349 Value *S, ///< The value to be truncated
3350 Type *Ty, ///< The (smaller) type to truncate to
3351 const Twine &NameStr, ///< A name for the new instruction
3352 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3355 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3356 static inline bool classof(const Instruction *I) {
3357 return I->getOpcode() == Trunc;
3359 static inline bool classof(const Value *V) {
3360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3364 //===----------------------------------------------------------------------===//
3366 //===----------------------------------------------------------------------===//
3368 /// \brief This class represents zero extension of integer types.
3369 class ZExtInst : public CastInst {
3371 /// \brief Clone an identical ZExtInst
3372 ZExtInst *clone_impl() const override;
3375 /// \brief Constructor with insert-before-instruction semantics
3377 Value *S, ///< The value to be zero extended
3378 Type *Ty, ///< The type to zero extend to
3379 const Twine &NameStr = "", ///< A name for the new instruction
3380 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3383 /// \brief Constructor with insert-at-end semantics.
3385 Value *S, ///< The value to be zero extended
3386 Type *Ty, ///< The type to zero extend to
3387 const Twine &NameStr, ///< A name for the new instruction
3388 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3391 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3392 static inline bool classof(const Instruction *I) {
3393 return I->getOpcode() == ZExt;
3395 static inline bool classof(const Value *V) {
3396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3400 //===----------------------------------------------------------------------===//
3402 //===----------------------------------------------------------------------===//
3404 /// \brief This class represents a sign extension of integer types.
3405 class SExtInst : public CastInst {
3407 /// \brief Clone an identical SExtInst
3408 SExtInst *clone_impl() const override;
3411 /// \brief Constructor with insert-before-instruction semantics
3413 Value *S, ///< The value to be sign extended
3414 Type *Ty, ///< The type to sign extend to
3415 const Twine &NameStr = "", ///< A name for the new instruction
3416 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3419 /// \brief Constructor with insert-at-end-of-block semantics
3421 Value *S, ///< The value to be sign extended
3422 Type *Ty, ///< The type to sign extend to
3423 const Twine &NameStr, ///< A name for the new instruction
3424 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3427 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3428 static inline bool classof(const Instruction *I) {
3429 return I->getOpcode() == SExt;
3431 static inline bool classof(const Value *V) {
3432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3436 //===----------------------------------------------------------------------===//
3437 // FPTruncInst Class
3438 //===----------------------------------------------------------------------===//
3440 /// \brief This class represents a truncation of floating point types.
3441 class FPTruncInst : public CastInst {
3443 /// \brief Clone an identical FPTruncInst
3444 FPTruncInst *clone_impl() const override;
3447 /// \brief Constructor with insert-before-instruction semantics
3449 Value *S, ///< The value to be truncated
3450 Type *Ty, ///< The type to truncate to
3451 const Twine &NameStr = "", ///< A name for the new instruction
3452 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3455 /// \brief Constructor with insert-before-instruction semantics
3457 Value *S, ///< The value to be truncated
3458 Type *Ty, ///< The type to truncate to
3459 const Twine &NameStr, ///< A name for the new instruction
3460 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3463 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3464 static inline bool classof(const Instruction *I) {
3465 return I->getOpcode() == FPTrunc;
3467 static inline bool classof(const Value *V) {
3468 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3472 //===----------------------------------------------------------------------===//
3474 //===----------------------------------------------------------------------===//
3476 /// \brief This class represents an extension of floating point types.
3477 class FPExtInst : public CastInst {
3479 /// \brief Clone an identical FPExtInst
3480 FPExtInst *clone_impl() const override;
3483 /// \brief Constructor with insert-before-instruction semantics
3485 Value *S, ///< The value to be extended
3486 Type *Ty, ///< The type to extend to
3487 const Twine &NameStr = "", ///< A name for the new instruction
3488 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3491 /// \brief Constructor with insert-at-end-of-block semantics
3493 Value *S, ///< The value to be extended
3494 Type *Ty, ///< The type to extend to
3495 const Twine &NameStr, ///< A name for the new instruction
3496 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3499 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3500 static inline bool classof(const Instruction *I) {
3501 return I->getOpcode() == FPExt;
3503 static inline bool classof(const Value *V) {
3504 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3508 //===----------------------------------------------------------------------===//
3510 //===----------------------------------------------------------------------===//
3512 /// \brief This class represents a cast unsigned integer to floating point.
3513 class UIToFPInst : public CastInst {
3515 /// \brief Clone an identical UIToFPInst
3516 UIToFPInst *clone_impl() const override;
3519 /// \brief Constructor with insert-before-instruction semantics
3521 Value *S, ///< The value to be converted
3522 Type *Ty, ///< The type to convert to
3523 const Twine &NameStr = "", ///< A name for the new instruction
3524 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3527 /// \brief Constructor with insert-at-end-of-block semantics
3529 Value *S, ///< The value to be converted
3530 Type *Ty, ///< The type to convert to
3531 const Twine &NameStr, ///< A name for the new instruction
3532 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3535 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3536 static inline bool classof(const Instruction *I) {
3537 return I->getOpcode() == UIToFP;
3539 static inline bool classof(const Value *V) {
3540 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3544 //===----------------------------------------------------------------------===//
3546 //===----------------------------------------------------------------------===//
3548 /// \brief This class represents a cast from signed integer to floating point.
3549 class SIToFPInst : public CastInst {
3551 /// \brief Clone an identical SIToFPInst
3552 SIToFPInst *clone_impl() const override;
3555 /// \brief Constructor with insert-before-instruction semantics
3557 Value *S, ///< The value to be converted
3558 Type *Ty, ///< The type to convert to
3559 const Twine &NameStr = "", ///< A name for the new instruction
3560 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3563 /// \brief Constructor with insert-at-end-of-block semantics
3565 Value *S, ///< The value to be converted
3566 Type *Ty, ///< The type to convert to
3567 const Twine &NameStr, ///< A name for the new instruction
3568 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3571 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3572 static inline bool classof(const Instruction *I) {
3573 return I->getOpcode() == SIToFP;
3575 static inline bool classof(const Value *V) {
3576 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3580 //===----------------------------------------------------------------------===//
3582 //===----------------------------------------------------------------------===//
3584 /// \brief This class represents a cast from floating point to unsigned integer
3585 class FPToUIInst : public CastInst {
3587 /// \brief Clone an identical FPToUIInst
3588 FPToUIInst *clone_impl() const override;
3591 /// \brief Constructor with insert-before-instruction semantics
3593 Value *S, ///< The value to be converted
3594 Type *Ty, ///< The type to convert to
3595 const Twine &NameStr = "", ///< A name for the new instruction
3596 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3599 /// \brief Constructor with insert-at-end-of-block semantics
3601 Value *S, ///< The value to be converted
3602 Type *Ty, ///< The type to convert to
3603 const Twine &NameStr, ///< A name for the new instruction
3604 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3607 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3608 static inline bool classof(const Instruction *I) {
3609 return I->getOpcode() == FPToUI;
3611 static inline bool classof(const Value *V) {
3612 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3616 //===----------------------------------------------------------------------===//
3618 //===----------------------------------------------------------------------===//
3620 /// \brief This class represents a cast from floating point to signed integer.
3621 class FPToSIInst : public CastInst {
3623 /// \brief Clone an identical FPToSIInst
3624 FPToSIInst *clone_impl() const override;
3627 /// \brief Constructor with insert-before-instruction semantics
3629 Value *S, ///< The value to be converted
3630 Type *Ty, ///< The type to convert to
3631 const Twine &NameStr = "", ///< A name for the new instruction
3632 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3635 /// \brief Constructor with insert-at-end-of-block semantics
3637 Value *S, ///< The value to be converted
3638 Type *Ty, ///< The type to convert to
3639 const Twine &NameStr, ///< A name for the new instruction
3640 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3643 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3644 static inline bool classof(const Instruction *I) {
3645 return I->getOpcode() == FPToSI;
3647 static inline bool classof(const Value *V) {
3648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3652 //===----------------------------------------------------------------------===//
3653 // IntToPtrInst Class
3654 //===----------------------------------------------------------------------===//
3656 /// \brief This class represents a cast from an integer to a pointer.
3657 class IntToPtrInst : public CastInst {
3659 /// \brief Constructor with insert-before-instruction semantics
3661 Value *S, ///< The value to be converted
3662 Type *Ty, ///< The type to convert to
3663 const Twine &NameStr = "", ///< A name for the new instruction
3664 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3667 /// \brief Constructor with insert-at-end-of-block semantics
3669 Value *S, ///< The value to be converted
3670 Type *Ty, ///< The type to convert to
3671 const Twine &NameStr, ///< A name for the new instruction
3672 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3675 /// \brief Clone an identical IntToPtrInst
3676 IntToPtrInst *clone_impl() const override;
3678 /// \brief Returns the address space of this instruction's pointer type.
3679 unsigned getAddressSpace() const {
3680 return getType()->getPointerAddressSpace();
3683 // Methods for support type inquiry through isa, cast, and dyn_cast:
3684 static inline bool classof(const Instruction *I) {
3685 return I->getOpcode() == IntToPtr;
3687 static inline bool classof(const Value *V) {
3688 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3692 //===----------------------------------------------------------------------===//
3693 // PtrToIntInst Class
3694 //===----------------------------------------------------------------------===//
3696 /// \brief This class represents a cast from a pointer to an integer
3697 class PtrToIntInst : public CastInst {
3699 /// \brief Clone an identical PtrToIntInst
3700 PtrToIntInst *clone_impl() const override;
3703 /// \brief Constructor with insert-before-instruction semantics
3705 Value *S, ///< The value to be converted
3706 Type *Ty, ///< The type to convert to
3707 const Twine &NameStr = "", ///< A name for the new instruction
3708 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3711 /// \brief Constructor with insert-at-end-of-block semantics
3713 Value *S, ///< The value to be converted
3714 Type *Ty, ///< The type to convert to
3715 const Twine &NameStr, ///< A name for the new instruction
3716 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3719 /// \brief Gets the pointer operand.
3720 Value *getPointerOperand() { return getOperand(0); }
3721 /// \brief Gets the pointer operand.
3722 const Value *getPointerOperand() const { return getOperand(0); }
3723 /// \brief Gets the operand index of the pointer operand.
3724 static unsigned getPointerOperandIndex() { return 0U; }
3726 /// \brief Returns the address space of the pointer operand.
3727 unsigned getPointerAddressSpace() const {
3728 return getPointerOperand()->getType()->getPointerAddressSpace();
3731 // Methods for support type inquiry through isa, cast, and dyn_cast:
3732 static inline bool classof(const Instruction *I) {
3733 return I->getOpcode() == PtrToInt;
3735 static inline bool classof(const Value *V) {
3736 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3740 //===----------------------------------------------------------------------===//
3741 // BitCastInst Class
3742 //===----------------------------------------------------------------------===//
3744 /// \brief This class represents a no-op cast from one type to another.
3745 class BitCastInst : public CastInst {
3747 /// \brief Clone an identical BitCastInst
3748 BitCastInst *clone_impl() const override;
3751 /// \brief Constructor with insert-before-instruction semantics
3753 Value *S, ///< The value to be casted
3754 Type *Ty, ///< The type to casted to
3755 const Twine &NameStr = "", ///< A name for the new instruction
3756 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3759 /// \brief Constructor with insert-at-end-of-block semantics
3761 Value *S, ///< The value to be casted
3762 Type *Ty, ///< The type to casted to
3763 const Twine &NameStr, ///< A name for the new instruction
3764 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3767 // Methods for support type inquiry through isa, cast, and dyn_cast:
3768 static inline bool classof(const Instruction *I) {
3769 return I->getOpcode() == BitCast;
3771 static inline bool classof(const Value *V) {
3772 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3776 //===----------------------------------------------------------------------===//
3777 // AddrSpaceCastInst Class
3778 //===----------------------------------------------------------------------===//
3780 /// \brief This class represents a conversion between pointers from
3781 /// one address space to another.
3782 class AddrSpaceCastInst : public CastInst {
3784 /// \brief Clone an identical AddrSpaceCastInst
3785 AddrSpaceCastInst *clone_impl() const override;
3788 /// \brief Constructor with insert-before-instruction semantics
3790 Value *S, ///< The value to be casted
3791 Type *Ty, ///< The type to casted to
3792 const Twine &NameStr = "", ///< A name for the new instruction
3793 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3796 /// \brief Constructor with insert-at-end-of-block semantics
3798 Value *S, ///< The value to be casted
3799 Type *Ty, ///< The type to casted to
3800 const Twine &NameStr, ///< A name for the new instruction
3801 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3804 // Methods for support type inquiry through isa, cast, and dyn_cast:
3805 static inline bool classof(const Instruction *I) {
3806 return I->getOpcode() == AddrSpaceCast;
3808 static inline bool classof(const Value *V) {
3809 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3813 } // End llvm namespace