1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 /// Returns true if the ordering is at least as strong as acquire
54 /// (i.e. acquire, acq_rel or seq_cst)
55 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
56 return (Ord == Acquire ||
57 Ord == AcquireRelease ||
58 Ord == SequentiallyConsistent);
61 /// Returns true if the ordering is at least as strong as release
62 /// (i.e. release, acq_rel or seq_cst)
63 inline bool isAtLeastRelease(AtomicOrdering Ord) {
64 return (Ord == Release ||
65 Ord == AcquireRelease ||
66 Ord == SequentiallyConsistent);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// AllocaInst - an instruction to allocate memory on the stack
75 class AllocaInst : public UnaryInstruction {
77 AllocaInst *clone_impl() const override;
79 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
80 const Twine &Name = "",
81 Instruction *InsertBefore = nullptr);
82 AllocaInst(Type *Ty, Value *ArraySize,
83 const Twine &Name, BasicBlock *InsertAtEnd);
85 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
86 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
88 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
89 const Twine &Name = "", Instruction *InsertBefore = nullptr);
90 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
91 const Twine &Name, BasicBlock *InsertAtEnd);
93 // Out of line virtual method, so the vtable, etc. has a home.
94 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 void setAtomic(AtomicOrdering Ordering,
245 SynchronizationScope SynchScope = CrossThread) {
246 setOrdering(Ordering);
247 setSynchScope(SynchScope);
250 bool isSimple() const { return !isAtomic() && !isVolatile(); }
251 bool isUnordered() const {
252 return getOrdering() <= Unordered && !isVolatile();
255 Value *getPointerOperand() { return getOperand(0); }
256 const Value *getPointerOperand() const { return getOperand(0); }
257 static unsigned getPointerOperandIndex() { return 0U; }
259 /// \brief Returns the address space of the pointer operand.
260 unsigned getPointerAddressSpace() const {
261 return getPointerOperand()->getType()->getPointerAddressSpace();
265 // Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const Instruction *I) {
267 return I->getOpcode() == Instruction::Load;
269 static inline bool classof(const Value *V) {
270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
273 // Shadow Instruction::setInstructionSubclassData with a private forwarding
274 // method so that subclasses cannot accidentally use it.
275 void setInstructionSubclassData(unsigned short D) {
276 Instruction::setInstructionSubclassData(D);
281 //===----------------------------------------------------------------------===//
283 //===----------------------------------------------------------------------===//
285 /// StoreInst - an instruction for storing to memory
287 class StoreInst : public Instruction {
288 void *operator new(size_t, unsigned) = delete;
291 StoreInst *clone_impl() const override;
293 // allocate space for exactly two operands
294 void *operator new(size_t s) {
295 return User::operator new(s, 2);
297 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
298 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
299 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
300 Instruction *InsertBefore = nullptr);
301 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
302 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
303 unsigned Align, Instruction *InsertBefore = nullptr);
304 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
305 unsigned Align, BasicBlock *InsertAtEnd);
306 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
307 unsigned Align, AtomicOrdering Order,
308 SynchronizationScope SynchScope = CrossThread,
309 Instruction *InsertBefore = nullptr);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
311 unsigned Align, AtomicOrdering Order,
312 SynchronizationScope SynchScope,
313 BasicBlock *InsertAtEnd);
316 /// isVolatile - Return true if this is a store to a volatile memory
319 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
321 /// setVolatile - Specify whether this is a volatile store or not.
323 void setVolatile(bool V) {
324 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
328 /// Transparently provide more efficient getOperand methods.
329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
331 /// getAlignment - Return the alignment of the access that is being performed
333 unsigned getAlignment() const {
334 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
337 void setAlignment(unsigned Align);
339 /// Returns the ordering effect of this store.
340 AtomicOrdering getOrdering() const {
341 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
344 /// Set the ordering constraint on this store. May not be Acquire or
346 void setOrdering(AtomicOrdering Ordering) {
347 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
351 SynchronizationScope getSynchScope() const {
352 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
355 /// Specify whether this store instruction is ordered with respect to all
356 /// concurrently executing threads, or only with respect to signal handlers
357 /// executing in the same thread.
358 void setSynchScope(SynchronizationScope xthread) {
359 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
363 void setAtomic(AtomicOrdering Ordering,
364 SynchronizationScope SynchScope = CrossThread) {
365 setOrdering(Ordering);
366 setSynchScope(SynchScope);
369 bool isSimple() const { return !isAtomic() && !isVolatile(); }
370 bool isUnordered() const {
371 return getOrdering() <= Unordered && !isVolatile();
374 Value *getValueOperand() { return getOperand(0); }
375 const Value *getValueOperand() const { return getOperand(0); }
377 Value *getPointerOperand() { return getOperand(1); }
378 const Value *getPointerOperand() const { return getOperand(1); }
379 static unsigned getPointerOperandIndex() { return 1U; }
381 /// \brief Returns the address space of the pointer operand.
382 unsigned getPointerAddressSpace() const {
383 return getPointerOperand()->getType()->getPointerAddressSpace();
386 // Methods for support type inquiry through isa, cast, and dyn_cast:
387 static inline bool classof(const Instruction *I) {
388 return I->getOpcode() == Instruction::Store;
390 static inline bool classof(const Value *V) {
391 return isa<Instruction>(V) && classof(cast<Instruction>(V));
394 // Shadow Instruction::setInstructionSubclassData with a private forwarding
395 // method so that subclasses cannot accidentally use it.
396 void setInstructionSubclassData(unsigned short D) {
397 Instruction::setInstructionSubclassData(D);
402 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
405 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
407 //===----------------------------------------------------------------------===//
409 //===----------------------------------------------------------------------===//
411 /// FenceInst - an instruction for ordering other memory operations
413 class FenceInst : public Instruction {
414 void *operator new(size_t, unsigned) = delete;
415 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
417 FenceInst *clone_impl() const override;
419 // allocate space for exactly zero operands
420 void *operator new(size_t s) {
421 return User::operator new(s, 0);
424 // Ordering may only be Acquire, Release, AcquireRelease, or
425 // SequentiallyConsistent.
426 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
427 SynchronizationScope SynchScope = CrossThread,
428 Instruction *InsertBefore = nullptr);
429 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
430 SynchronizationScope SynchScope,
431 BasicBlock *InsertAtEnd);
433 /// Returns the ordering effect of this fence.
434 AtomicOrdering getOrdering() const {
435 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
438 /// Set the ordering constraint on this fence. May only be Acquire, Release,
439 /// AcquireRelease, or SequentiallyConsistent.
440 void setOrdering(AtomicOrdering Ordering) {
441 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
445 SynchronizationScope getSynchScope() const {
446 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
449 /// Specify whether this fence orders other operations with respect to all
450 /// concurrently executing threads, or only with respect to signal handlers
451 /// executing in the same thread.
452 void setSynchScope(SynchronizationScope xthread) {
453 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
457 // Methods for support type inquiry through isa, cast, and dyn_cast:
458 static inline bool classof(const Instruction *I) {
459 return I->getOpcode() == Instruction::Fence;
461 static inline bool classof(const Value *V) {
462 return isa<Instruction>(V) && classof(cast<Instruction>(V));
465 // Shadow Instruction::setInstructionSubclassData with a private forwarding
466 // method so that subclasses cannot accidentally use it.
467 void setInstructionSubclassData(unsigned short D) {
468 Instruction::setInstructionSubclassData(D);
472 //===----------------------------------------------------------------------===//
473 // AtomicCmpXchgInst Class
474 //===----------------------------------------------------------------------===//
476 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
477 /// specified value is in a memory location, and, if it is, stores a new value
478 /// there. Returns the value that was loaded.
480 class AtomicCmpXchgInst : public Instruction {
481 void *operator new(size_t, unsigned) = delete;
482 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
483 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
484 SynchronizationScope SynchScope);
486 AtomicCmpXchgInst *clone_impl() const override;
488 // allocate space for exactly three operands
489 void *operator new(size_t s) {
490 return User::operator new(s, 3);
492 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
493 AtomicOrdering SuccessOrdering,
494 AtomicOrdering FailureOrdering,
495 SynchronizationScope SynchScope,
496 Instruction *InsertBefore = nullptr);
497 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
498 AtomicOrdering SuccessOrdering,
499 AtomicOrdering FailureOrdering,
500 SynchronizationScope SynchScope,
501 BasicBlock *InsertAtEnd);
503 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
506 bool isVolatile() const {
507 return getSubclassDataFromInstruction() & 1;
510 /// setVolatile - Specify whether this is a volatile cmpxchg.
512 void setVolatile(bool V) {
513 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
517 /// Return true if this cmpxchg may spuriously fail.
518 bool isWeak() const {
519 return getSubclassDataFromInstruction() & 0x100;
522 void setWeak(bool IsWeak) {
523 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
527 /// Transparently provide more efficient getOperand methods.
528 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
530 /// Set the ordering constraint on this cmpxchg.
531 void setSuccessOrdering(AtomicOrdering Ordering) {
532 assert(Ordering != NotAtomic &&
533 "CmpXchg instructions can only be atomic.");
534 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
538 void setFailureOrdering(AtomicOrdering Ordering) {
539 assert(Ordering != NotAtomic &&
540 "CmpXchg instructions can only be atomic.");
541 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
545 /// Specify whether this cmpxchg is atomic and orders other operations with
546 /// respect to all concurrently executing threads, or only with respect to
547 /// signal handlers executing in the same thread.
548 void setSynchScope(SynchronizationScope SynchScope) {
549 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
553 /// Returns the ordering constraint on this cmpxchg.
554 AtomicOrdering getSuccessOrdering() const {
555 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
558 /// Returns the ordering constraint on this cmpxchg.
559 AtomicOrdering getFailureOrdering() const {
560 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
563 /// Returns whether this cmpxchg is atomic between threads or only within a
565 SynchronizationScope getSynchScope() const {
566 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
569 Value *getPointerOperand() { return getOperand(0); }
570 const Value *getPointerOperand() const { return getOperand(0); }
571 static unsigned getPointerOperandIndex() { return 0U; }
573 Value *getCompareOperand() { return getOperand(1); }
574 const Value *getCompareOperand() const { return getOperand(1); }
576 Value *getNewValOperand() { return getOperand(2); }
577 const Value *getNewValOperand() const { return getOperand(2); }
579 /// \brief Returns the address space of the pointer operand.
580 unsigned getPointerAddressSpace() const {
581 return getPointerOperand()->getType()->getPointerAddressSpace();
584 /// \brief Returns the strongest permitted ordering on failure, given the
585 /// desired ordering on success.
587 /// If the comparison in a cmpxchg operation fails, there is no atomic store
588 /// so release semantics cannot be provided. So this function drops explicit
589 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
590 /// operation would remain SequentiallyConsistent.
591 static AtomicOrdering
592 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
593 switch (SuccessOrdering) {
594 default: llvm_unreachable("invalid cmpxchg success ordering");
601 case SequentiallyConsistent:
602 return SequentiallyConsistent;
606 // Methods for support type inquiry through isa, cast, and dyn_cast:
607 static inline bool classof(const Instruction *I) {
608 return I->getOpcode() == Instruction::AtomicCmpXchg;
610 static inline bool classof(const Value *V) {
611 return isa<Instruction>(V) && classof(cast<Instruction>(V));
614 // Shadow Instruction::setInstructionSubclassData with a private forwarding
615 // method so that subclasses cannot accidentally use it.
616 void setInstructionSubclassData(unsigned short D) {
617 Instruction::setInstructionSubclassData(D);
622 struct OperandTraits<AtomicCmpXchgInst> :
623 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
626 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
628 //===----------------------------------------------------------------------===//
629 // AtomicRMWInst Class
630 //===----------------------------------------------------------------------===//
632 /// AtomicRMWInst - an instruction that atomically reads a memory location,
633 /// combines it with another value, and then stores the result back. Returns
636 class AtomicRMWInst : public Instruction {
637 void *operator new(size_t, unsigned) = delete;
639 AtomicRMWInst *clone_impl() const override;
641 /// This enumeration lists the possible modifications atomicrmw can make. In
642 /// the descriptions, 'p' is the pointer to the instruction's memory location,
643 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
644 /// instruction. These instructions always return 'old'.
660 /// *p = old >signed v ? old : v
662 /// *p = old <signed v ? old : v
664 /// *p = old >unsigned v ? old : v
666 /// *p = old <unsigned v ? old : v
674 // allocate space for exactly two operands
675 void *operator new(size_t s) {
676 return User::operator new(s, 2);
678 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
679 AtomicOrdering Ordering, SynchronizationScope SynchScope,
680 Instruction *InsertBefore = nullptr);
681 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
682 AtomicOrdering Ordering, SynchronizationScope SynchScope,
683 BasicBlock *InsertAtEnd);
685 BinOp getOperation() const {
686 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
689 void setOperation(BinOp Operation) {
690 unsigned short SubclassData = getSubclassDataFromInstruction();
691 setInstructionSubclassData((SubclassData & 31) |
695 /// isVolatile - Return true if this is a RMW on a volatile memory location.
697 bool isVolatile() const {
698 return getSubclassDataFromInstruction() & 1;
701 /// setVolatile - Specify whether this is a volatile RMW or not.
703 void setVolatile(bool V) {
704 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
708 /// Transparently provide more efficient getOperand methods.
709 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
711 /// Set the ordering constraint on this RMW.
712 void setOrdering(AtomicOrdering Ordering) {
713 assert(Ordering != NotAtomic &&
714 "atomicrmw instructions can only be atomic.");
715 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
719 /// Specify whether this RMW orders other operations with respect to all
720 /// concurrently executing threads, or only with respect to signal handlers
721 /// executing in the same thread.
722 void setSynchScope(SynchronizationScope SynchScope) {
723 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
727 /// Returns the ordering constraint on this RMW.
728 AtomicOrdering getOrdering() const {
729 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
732 /// Returns whether this RMW is atomic between threads or only within a
734 SynchronizationScope getSynchScope() const {
735 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
738 Value *getPointerOperand() { return getOperand(0); }
739 const Value *getPointerOperand() const { return getOperand(0); }
740 static unsigned getPointerOperandIndex() { return 0U; }
742 Value *getValOperand() { return getOperand(1); }
743 const Value *getValOperand() const { return getOperand(1); }
745 /// \brief Returns the address space of the pointer operand.
746 unsigned getPointerAddressSpace() const {
747 return getPointerOperand()->getType()->getPointerAddressSpace();
750 // Methods for support type inquiry through isa, cast, and dyn_cast:
751 static inline bool classof(const Instruction *I) {
752 return I->getOpcode() == Instruction::AtomicRMW;
754 static inline bool classof(const Value *V) {
755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
758 void Init(BinOp Operation, Value *Ptr, Value *Val,
759 AtomicOrdering Ordering, SynchronizationScope SynchScope);
760 // Shadow Instruction::setInstructionSubclassData with a private forwarding
761 // method so that subclasses cannot accidentally use it.
762 void setInstructionSubclassData(unsigned short D) {
763 Instruction::setInstructionSubclassData(D);
768 struct OperandTraits<AtomicRMWInst>
769 : public FixedNumOperandTraits<AtomicRMWInst,2> {
772 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
774 //===----------------------------------------------------------------------===//
775 // GetElementPtrInst Class
776 //===----------------------------------------------------------------------===//
778 // checkGEPType - Simple wrapper function to give a better assertion failure
779 // message on bad indexes for a gep instruction.
781 inline Type *checkGEPType(Type *Ty) {
782 assert(Ty && "Invalid GetElementPtrInst indices for type!");
786 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
787 /// access elements of arrays and structs
789 class GetElementPtrInst : public Instruction {
790 GetElementPtrInst(const GetElementPtrInst &GEPI);
791 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
793 /// Constructors - Create a getelementptr instruction with a base pointer an
794 /// list of indices. The first ctor can optionally insert before an existing
795 /// instruction, the second appends the new instruction to the specified
797 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
798 unsigned Values, const Twine &NameStr,
799 Instruction *InsertBefore);
800 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
801 unsigned Values, const Twine &NameStr,
802 BasicBlock *InsertAtEnd);
804 GetElementPtrInst *clone_impl() const override;
806 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
807 const Twine &NameStr = "",
808 Instruction *InsertBefore = nullptr) {
809 unsigned Values = 1 + unsigned(IdxList.size());
811 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
813 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
814 const Twine &NameStr,
815 BasicBlock *InsertAtEnd) {
816 unsigned Values = 1 + unsigned(IdxList.size());
818 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
821 /// Create an "inbounds" getelementptr. See the documentation for the
822 /// "inbounds" flag in LangRef.html for details.
823 static GetElementPtrInst *CreateInBounds(Value *Ptr,
824 ArrayRef<Value *> IdxList,
825 const Twine &NameStr = "",
826 Instruction *InsertBefore = nullptr){
827 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
828 GEP->setIsInBounds(true);
831 static GetElementPtrInst *CreateInBounds(Value *Ptr,
832 ArrayRef<Value *> IdxList,
833 const Twine &NameStr,
834 BasicBlock *InsertAtEnd) {
835 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
836 GEP->setIsInBounds(true);
840 /// Transparently provide more efficient getOperand methods.
841 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
843 // getType - Overload to return most specific sequential type.
844 SequentialType *getType() const {
845 return cast<SequentialType>(Instruction::getType());
848 /// \brief Returns the address space of this instruction's pointer type.
849 unsigned getAddressSpace() const {
850 // Note that this is always the same as the pointer operand's address space
851 // and that is cheaper to compute, so cheat here.
852 return getPointerAddressSpace();
855 /// getIndexedType - Returns the type of the element that would be loaded with
856 /// a load instruction with the specified parameters.
858 /// Null is returned if the indices are invalid for the specified
861 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
862 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
863 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
865 inline op_iterator idx_begin() { return op_begin()+1; }
866 inline const_op_iterator idx_begin() const { return op_begin()+1; }
867 inline op_iterator idx_end() { return op_end(); }
868 inline const_op_iterator idx_end() const { return op_end(); }
870 Value *getPointerOperand() {
871 return getOperand(0);
873 const Value *getPointerOperand() const {
874 return getOperand(0);
876 static unsigned getPointerOperandIndex() {
877 return 0U; // get index for modifying correct operand.
880 /// getPointerOperandType - Method to return the pointer operand as a
882 Type *getPointerOperandType() const {
883 return getPointerOperand()->getType();
886 /// \brief Returns the address space of the pointer operand.
887 unsigned getPointerAddressSpace() const {
888 return getPointerOperandType()->getPointerAddressSpace();
891 /// GetGEPReturnType - Returns the pointer type returned by the GEP
892 /// instruction, which may be a vector of pointers.
893 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
894 Type *PtrTy = PointerType::get(checkGEPType(
895 getIndexedType(Ptr->getType(), IdxList)),
896 Ptr->getType()->getPointerAddressSpace());
898 if (Ptr->getType()->isVectorTy()) {
899 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
900 return VectorType::get(PtrTy, NumElem);
907 unsigned getNumIndices() const { // Note: always non-negative
908 return getNumOperands() - 1;
911 bool hasIndices() const {
912 return getNumOperands() > 1;
915 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
916 /// zeros. If so, the result pointer and the first operand have the same
917 /// value, just potentially different types.
918 bool hasAllZeroIndices() const;
920 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
921 /// constant integers. If so, the result pointer and the first operand have
922 /// a constant offset between them.
923 bool hasAllConstantIndices() const;
925 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
926 /// See LangRef.html for the meaning of inbounds on a getelementptr.
927 void setIsInBounds(bool b = true);
929 /// isInBounds - Determine whether the GEP has the inbounds flag.
930 bool isInBounds() const;
932 /// \brief Accumulate the constant address offset of this GEP if possible.
934 /// This routine accepts an APInt into which it will accumulate the constant
935 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
936 /// all-constant, it returns false and the value of the offset APInt is
937 /// undefined (it is *not* preserved!). The APInt passed into this routine
938 /// must be at least as wide as the IntPtr type for the address space of
939 /// the base GEP pointer.
940 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
942 // Methods for support type inquiry through isa, cast, and dyn_cast:
943 static inline bool classof(const Instruction *I) {
944 return (I->getOpcode() == Instruction::GetElementPtr);
946 static inline bool classof(const Value *V) {
947 return isa<Instruction>(V) && classof(cast<Instruction>(V));
952 struct OperandTraits<GetElementPtrInst> :
953 public VariadicOperandTraits<GetElementPtrInst, 1> {
956 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
957 ArrayRef<Value *> IdxList,
959 const Twine &NameStr,
960 Instruction *InsertBefore)
961 : Instruction(getGEPReturnType(Ptr, IdxList),
963 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
964 Values, InsertBefore) {
965 init(Ptr, IdxList, NameStr);
967 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
968 ArrayRef<Value *> IdxList,
970 const Twine &NameStr,
971 BasicBlock *InsertAtEnd)
972 : Instruction(getGEPReturnType(Ptr, IdxList),
974 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
975 Values, InsertAtEnd) {
976 init(Ptr, IdxList, NameStr);
980 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
983 //===----------------------------------------------------------------------===//
985 //===----------------------------------------------------------------------===//
987 /// This instruction compares its operands according to the predicate given
988 /// to the constructor. It only operates on integers or pointers. The operands
989 /// must be identical types.
990 /// \brief Represent an integer comparison operator.
991 class ICmpInst: public CmpInst {
993 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
994 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
995 "Invalid ICmp predicate value");
996 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
997 "Both operands to ICmp instruction are not of the same type!");
998 // Check that the operands are the right type
999 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1000 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1001 "Invalid operand types for ICmp instruction");
1005 /// \brief Clone an identical ICmpInst
1006 ICmpInst *clone_impl() const override;
1008 /// \brief Constructor with insert-before-instruction semantics.
1010 Instruction *InsertBefore, ///< Where to insert
1011 Predicate pred, ///< The predicate to use for the comparison
1012 Value *LHS, ///< The left-hand-side of the expression
1013 Value *RHS, ///< The right-hand-side of the expression
1014 const Twine &NameStr = "" ///< Name of the instruction
1015 ) : CmpInst(makeCmpResultType(LHS->getType()),
1016 Instruction::ICmp, pred, LHS, RHS, NameStr,
1023 /// \brief Constructor with insert-at-end semantics.
1025 BasicBlock &InsertAtEnd, ///< Block to insert into.
1026 Predicate pred, ///< The predicate to use for the comparison
1027 Value *LHS, ///< The left-hand-side of the expression
1028 Value *RHS, ///< The right-hand-side of the expression
1029 const Twine &NameStr = "" ///< Name of the instruction
1030 ) : CmpInst(makeCmpResultType(LHS->getType()),
1031 Instruction::ICmp, pred, LHS, RHS, NameStr,
1038 /// \brief Constructor with no-insertion semantics
1040 Predicate pred, ///< The predicate to use for the comparison
1041 Value *LHS, ///< The left-hand-side of the expression
1042 Value *RHS, ///< The right-hand-side of the expression
1043 const Twine &NameStr = "" ///< Name of the instruction
1044 ) : CmpInst(makeCmpResultType(LHS->getType()),
1045 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1051 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1052 /// @returns the predicate that would be the result if the operand were
1053 /// regarded as signed.
1054 /// \brief Return the signed version of the predicate
1055 Predicate getSignedPredicate() const {
1056 return getSignedPredicate(getPredicate());
1059 /// This is a static version that you can use without an instruction.
1060 /// \brief Return the signed version of the predicate.
1061 static Predicate getSignedPredicate(Predicate pred);
1063 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1064 /// @returns the predicate that would be the result if the operand were
1065 /// regarded as unsigned.
1066 /// \brief Return the unsigned version of the predicate
1067 Predicate getUnsignedPredicate() const {
1068 return getUnsignedPredicate(getPredicate());
1071 /// This is a static version that you can use without an instruction.
1072 /// \brief Return the unsigned version of the predicate.
1073 static Predicate getUnsignedPredicate(Predicate pred);
1075 /// isEquality - Return true if this predicate is either EQ or NE. This also
1076 /// tests for commutativity.
1077 static bool isEquality(Predicate P) {
1078 return P == ICMP_EQ || P == ICMP_NE;
1081 /// isEquality - Return true if this predicate is either EQ or NE. This also
1082 /// tests for commutativity.
1083 bool isEquality() const {
1084 return isEquality(getPredicate());
1087 /// @returns true if the predicate of this ICmpInst is commutative
1088 /// \brief Determine if this relation is commutative.
1089 bool isCommutative() const { return isEquality(); }
1091 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1093 bool isRelational() const {
1094 return !isEquality();
1097 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1099 static bool isRelational(Predicate P) {
1100 return !isEquality(P);
1103 /// Initialize a set of values that all satisfy the predicate with C.
1104 /// \brief Make a ConstantRange for a relation with a constant value.
1105 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1107 /// Exchange the two operands to this instruction in such a way that it does
1108 /// not modify the semantics of the instruction. The predicate value may be
1109 /// changed to retain the same result if the predicate is order dependent
1111 /// \brief Swap operands and adjust predicate.
1112 void swapOperands() {
1113 setPredicate(getSwappedPredicate());
1114 Op<0>().swap(Op<1>());
1117 // Methods for support type inquiry through isa, cast, and dyn_cast:
1118 static inline bool classof(const Instruction *I) {
1119 return I->getOpcode() == Instruction::ICmp;
1121 static inline bool classof(const Value *V) {
1122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1127 //===----------------------------------------------------------------------===//
1129 //===----------------------------------------------------------------------===//
1131 /// This instruction compares its operands according to the predicate given
1132 /// to the constructor. It only operates on floating point values or packed
1133 /// vectors of floating point values. The operands must be identical types.
1134 /// \brief Represents a floating point comparison operator.
1135 class FCmpInst: public CmpInst {
1137 /// \brief Clone an identical FCmpInst
1138 FCmpInst *clone_impl() const override;
1140 /// \brief Constructor with insert-before-instruction semantics.
1142 Instruction *InsertBefore, ///< Where to insert
1143 Predicate pred, ///< The predicate to use for the comparison
1144 Value *LHS, ///< The left-hand-side of the expression
1145 Value *RHS, ///< The right-hand-side of the expression
1146 const Twine &NameStr = "" ///< Name of the instruction
1147 ) : CmpInst(makeCmpResultType(LHS->getType()),
1148 Instruction::FCmp, pred, LHS, RHS, NameStr,
1150 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1151 "Invalid FCmp predicate value");
1152 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1153 "Both operands to FCmp instruction are not of the same type!");
1154 // Check that the operands are the right type
1155 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1156 "Invalid operand types for FCmp instruction");
1159 /// \brief Constructor with insert-at-end semantics.
1161 BasicBlock &InsertAtEnd, ///< Block to insert into.
1162 Predicate pred, ///< The predicate to use for the comparison
1163 Value *LHS, ///< The left-hand-side of the expression
1164 Value *RHS, ///< The right-hand-side of the expression
1165 const Twine &NameStr = "" ///< Name of the instruction
1166 ) : CmpInst(makeCmpResultType(LHS->getType()),
1167 Instruction::FCmp, pred, LHS, RHS, NameStr,
1169 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1170 "Invalid FCmp predicate value");
1171 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1172 "Both operands to FCmp instruction are not of the same type!");
1173 // Check that the operands are the right type
1174 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1175 "Invalid operand types for FCmp instruction");
1178 /// \brief Constructor with no-insertion semantics
1180 Predicate pred, ///< The predicate to use for the comparison
1181 Value *LHS, ///< The left-hand-side of the expression
1182 Value *RHS, ///< The right-hand-side of the expression
1183 const Twine &NameStr = "" ///< Name of the instruction
1184 ) : CmpInst(makeCmpResultType(LHS->getType()),
1185 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1186 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1187 "Invalid FCmp predicate value");
1188 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1189 "Both operands to FCmp instruction are not of the same type!");
1190 // Check that the operands are the right type
1191 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1192 "Invalid operand types for FCmp instruction");
1195 /// @returns true if the predicate of this instruction is EQ or NE.
1196 /// \brief Determine if this is an equality predicate.
1197 bool isEquality() const {
1198 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1199 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1202 /// @returns true if the predicate of this instruction is commutative.
1203 /// \brief Determine if this is a commutative predicate.
1204 bool isCommutative() const {
1205 return isEquality() ||
1206 getPredicate() == FCMP_FALSE ||
1207 getPredicate() == FCMP_TRUE ||
1208 getPredicate() == FCMP_ORD ||
1209 getPredicate() == FCMP_UNO;
1212 /// @returns true if the predicate is relational (not EQ or NE).
1213 /// \brief Determine if this a relational predicate.
1214 bool isRelational() const { return !isEquality(); }
1216 /// Exchange the two operands to this instruction in such a way that it does
1217 /// not modify the semantics of the instruction. The predicate value may be
1218 /// changed to retain the same result if the predicate is order dependent
1220 /// \brief Swap operands and adjust predicate.
1221 void swapOperands() {
1222 setPredicate(getSwappedPredicate());
1223 Op<0>().swap(Op<1>());
1226 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1227 static inline bool classof(const Instruction *I) {
1228 return I->getOpcode() == Instruction::FCmp;
1230 static inline bool classof(const Value *V) {
1231 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1235 //===----------------------------------------------------------------------===//
1236 /// CallInst - This class represents a function call, abstracting a target
1237 /// machine's calling convention. This class uses low bit of the SubClassData
1238 /// field to indicate whether or not this is a tail call. The rest of the bits
1239 /// hold the calling convention of the call.
1241 class CallInst : public Instruction {
1242 AttributeSet AttributeList; ///< parameter attributes for call
1243 CallInst(const CallInst &CI);
1244 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1245 void init(Value *Func, const Twine &NameStr);
1247 /// Construct a CallInst given a range of arguments.
1248 /// \brief Construct a CallInst from a range of arguments
1249 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1250 const Twine &NameStr, Instruction *InsertBefore);
1252 /// Construct a CallInst given a range of arguments.
1253 /// \brief Construct a CallInst from a range of arguments
1254 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1255 const Twine &NameStr, BasicBlock *InsertAtEnd);
1257 explicit CallInst(Value *F, const Twine &NameStr,
1258 Instruction *InsertBefore);
1259 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1261 CallInst *clone_impl() const override;
1263 static CallInst *Create(Value *Func,
1264 ArrayRef<Value *> Args,
1265 const Twine &NameStr = "",
1266 Instruction *InsertBefore = nullptr) {
1267 return new(unsigned(Args.size() + 1))
1268 CallInst(Func, Args, NameStr, InsertBefore);
1270 static CallInst *Create(Value *Func,
1271 ArrayRef<Value *> Args,
1272 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1273 return new(unsigned(Args.size() + 1))
1274 CallInst(Func, Args, NameStr, InsertAtEnd);
1276 static CallInst *Create(Value *F, const Twine &NameStr = "",
1277 Instruction *InsertBefore = nullptr) {
1278 return new(1) CallInst(F, NameStr, InsertBefore);
1280 static CallInst *Create(Value *F, const Twine &NameStr,
1281 BasicBlock *InsertAtEnd) {
1282 return new(1) CallInst(F, NameStr, InsertAtEnd);
1284 /// CreateMalloc - Generate the IR for a call to malloc:
1285 /// 1. Compute the malloc call's argument as the specified type's size,
1286 /// possibly multiplied by the array size if the array size is not
1288 /// 2. Call malloc with that argument.
1289 /// 3. Bitcast the result of the malloc call to the specified type.
1290 static Instruction *CreateMalloc(Instruction *InsertBefore,
1291 Type *IntPtrTy, Type *AllocTy,
1292 Value *AllocSize, Value *ArraySize = nullptr,
1293 Function* MallocF = nullptr,
1294 const Twine &Name = "");
1295 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1296 Type *IntPtrTy, Type *AllocTy,
1297 Value *AllocSize, Value *ArraySize = nullptr,
1298 Function* MallocF = nullptr,
1299 const Twine &Name = "");
1300 /// CreateFree - Generate the IR for a call to the builtin free function.
1301 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1302 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1306 // Note that 'musttail' implies 'tail'.
1307 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1308 TailCallKind getTailCallKind() const {
1309 return TailCallKind(getSubclassDataFromInstruction() & 3);
1311 bool isTailCall() const {
1312 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1314 bool isMustTailCall() const {
1315 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1317 void setTailCall(bool isTC = true) {
1318 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1319 unsigned(isTC ? TCK_Tail : TCK_None));
1321 void setTailCallKind(TailCallKind TCK) {
1322 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1326 /// Provide fast operand accessors
1327 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1329 /// getNumArgOperands - Return the number of call arguments.
1331 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1333 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1335 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1336 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1338 /// arg_operands - iteration adapter for range-for loops.
1339 iterator_range<op_iterator> arg_operands() {
1340 // The last operand in the op list is the callee - it's not one of the args
1341 // so we don't want to iterate over it.
1342 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1345 /// arg_operands - iteration adapter for range-for loops.
1346 iterator_range<const_op_iterator> arg_operands() const {
1347 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1350 /// \brief Wrappers for getting the \c Use of a call argument.
1351 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1352 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1354 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1356 CallingConv::ID getCallingConv() const {
1357 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1359 void setCallingConv(CallingConv::ID CC) {
1360 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1361 (static_cast<unsigned>(CC) << 2));
1364 /// getAttributes - Return the parameter attributes for this call.
1366 const AttributeSet &getAttributes() const { return AttributeList; }
1368 /// setAttributes - Set the parameter attributes for this call.
1370 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1372 /// addAttribute - adds the attribute to the list of attributes.
1373 void addAttribute(unsigned i, Attribute::AttrKind attr);
1375 /// removeAttribute - removes the attribute from the list of attributes.
1376 void removeAttribute(unsigned i, Attribute attr);
1378 /// \brief adds the dereferenceable attribute to the list of attributes.
1379 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1381 /// \brief Determine whether this call has the given attribute.
1382 bool hasFnAttr(Attribute::AttrKind A) const {
1383 assert(A != Attribute::NoBuiltin &&
1384 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1385 return hasFnAttrImpl(A);
1388 /// \brief Determine whether the call or the callee has the given attributes.
1389 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1391 /// \brief Extract the alignment for a call or parameter (0=unknown).
1392 unsigned getParamAlignment(unsigned i) const {
1393 return AttributeList.getParamAlignment(i);
1396 /// \brief Extract the number of dereferenceable bytes for a call or
1397 /// parameter (0=unknown).
1398 uint64_t getDereferenceableBytes(unsigned i) const {
1399 return AttributeList.getDereferenceableBytes(i);
1402 /// \brief Return true if the call should not be treated as a call to a
1404 bool isNoBuiltin() const {
1405 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1406 !hasFnAttrImpl(Attribute::Builtin);
1409 /// \brief Return true if the call should not be inlined.
1410 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1411 void setIsNoInline() {
1412 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1415 /// \brief Return true if the call can return twice
1416 bool canReturnTwice() const {
1417 return hasFnAttr(Attribute::ReturnsTwice);
1419 void setCanReturnTwice() {
1420 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1423 /// \brief Determine if the call does not access memory.
1424 bool doesNotAccessMemory() const {
1425 return hasFnAttr(Attribute::ReadNone);
1427 void setDoesNotAccessMemory() {
1428 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1431 /// \brief Determine if the call does not access or only reads memory.
1432 bool onlyReadsMemory() const {
1433 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1435 void setOnlyReadsMemory() {
1436 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1439 /// \brief Determine if the call cannot return.
1440 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1441 void setDoesNotReturn() {
1442 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1445 /// \brief Determine if the call cannot unwind.
1446 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1447 void setDoesNotThrow() {
1448 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1451 /// \brief Determine if the call cannot be duplicated.
1452 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1453 void setCannotDuplicate() {
1454 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1457 /// \brief Determine if the call returns a structure through first
1458 /// pointer argument.
1459 bool hasStructRetAttr() const {
1460 // Be friendly and also check the callee.
1461 return paramHasAttr(1, Attribute::StructRet);
1464 /// \brief Determine if any call argument is an aggregate passed by value.
1465 bool hasByValArgument() const {
1466 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1469 /// getCalledFunction - Return the function called, or null if this is an
1470 /// indirect function invocation.
1472 Function *getCalledFunction() const {
1473 return dyn_cast<Function>(Op<-1>());
1476 /// getCalledValue - Get a pointer to the function that is invoked by this
1478 const Value *getCalledValue() const { return Op<-1>(); }
1479 Value *getCalledValue() { return Op<-1>(); }
1481 /// setCalledFunction - Set the function called.
1482 void setCalledFunction(Value* Fn) {
1486 /// isInlineAsm - Check if this call is an inline asm statement.
1487 bool isInlineAsm() const {
1488 return isa<InlineAsm>(Op<-1>());
1491 // Methods for support type inquiry through isa, cast, and dyn_cast:
1492 static inline bool classof(const Instruction *I) {
1493 return I->getOpcode() == Instruction::Call;
1495 static inline bool classof(const Value *V) {
1496 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1500 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1502 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1503 // method so that subclasses cannot accidentally use it.
1504 void setInstructionSubclassData(unsigned short D) {
1505 Instruction::setInstructionSubclassData(D);
1510 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1513 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1514 const Twine &NameStr, BasicBlock *InsertAtEnd)
1515 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1516 ->getElementType())->getReturnType(),
1518 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1519 unsigned(Args.size() + 1), InsertAtEnd) {
1520 init(Func, Args, NameStr);
1523 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1524 const Twine &NameStr, Instruction *InsertBefore)
1525 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1526 ->getElementType())->getReturnType(),
1528 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1529 unsigned(Args.size() + 1), InsertBefore) {
1530 init(Func, Args, NameStr);
1534 // Note: if you get compile errors about private methods then
1535 // please update your code to use the high-level operand
1536 // interfaces. See line 943 above.
1537 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1539 //===----------------------------------------------------------------------===//
1541 //===----------------------------------------------------------------------===//
1543 /// SelectInst - This class represents the LLVM 'select' instruction.
1545 class SelectInst : public Instruction {
1546 void init(Value *C, Value *S1, Value *S2) {
1547 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1553 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1554 Instruction *InsertBefore)
1555 : Instruction(S1->getType(), Instruction::Select,
1556 &Op<0>(), 3, InsertBefore) {
1560 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1561 BasicBlock *InsertAtEnd)
1562 : Instruction(S1->getType(), Instruction::Select,
1563 &Op<0>(), 3, InsertAtEnd) {
1568 SelectInst *clone_impl() const override;
1570 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1571 const Twine &NameStr = "",
1572 Instruction *InsertBefore = nullptr) {
1573 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1575 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1576 const Twine &NameStr,
1577 BasicBlock *InsertAtEnd) {
1578 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1581 const Value *getCondition() const { return Op<0>(); }
1582 const Value *getTrueValue() const { return Op<1>(); }
1583 const Value *getFalseValue() const { return Op<2>(); }
1584 Value *getCondition() { return Op<0>(); }
1585 Value *getTrueValue() { return Op<1>(); }
1586 Value *getFalseValue() { return Op<2>(); }
1588 /// areInvalidOperands - Return a string if the specified operands are invalid
1589 /// for a select operation, otherwise return null.
1590 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1592 /// Transparently provide more efficient getOperand methods.
1593 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1595 OtherOps getOpcode() const {
1596 return static_cast<OtherOps>(Instruction::getOpcode());
1599 // Methods for support type inquiry through isa, cast, and dyn_cast:
1600 static inline bool classof(const Instruction *I) {
1601 return I->getOpcode() == Instruction::Select;
1603 static inline bool classof(const Value *V) {
1604 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1609 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1612 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1614 //===----------------------------------------------------------------------===//
1616 //===----------------------------------------------------------------------===//
1618 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1619 /// an argument of the specified type given a va_list and increments that list
1621 class VAArgInst : public UnaryInstruction {
1623 VAArgInst *clone_impl() const override;
1626 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1627 Instruction *InsertBefore = nullptr)
1628 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1631 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1632 BasicBlock *InsertAtEnd)
1633 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1637 Value *getPointerOperand() { return getOperand(0); }
1638 const Value *getPointerOperand() const { return getOperand(0); }
1639 static unsigned getPointerOperandIndex() { return 0U; }
1641 // Methods for support type inquiry through isa, cast, and dyn_cast:
1642 static inline bool classof(const Instruction *I) {
1643 return I->getOpcode() == VAArg;
1645 static inline bool classof(const Value *V) {
1646 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1650 //===----------------------------------------------------------------------===//
1651 // ExtractElementInst Class
1652 //===----------------------------------------------------------------------===//
1654 /// ExtractElementInst - This instruction extracts a single (scalar)
1655 /// element from a VectorType value
1657 class ExtractElementInst : public Instruction {
1658 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1659 Instruction *InsertBefore = nullptr);
1660 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1661 BasicBlock *InsertAtEnd);
1663 ExtractElementInst *clone_impl() const override;
1666 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1667 const Twine &NameStr = "",
1668 Instruction *InsertBefore = nullptr) {
1669 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1671 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1672 const Twine &NameStr,
1673 BasicBlock *InsertAtEnd) {
1674 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1677 /// isValidOperands - Return true if an extractelement instruction can be
1678 /// formed with the specified operands.
1679 static bool isValidOperands(const Value *Vec, const Value *Idx);
1681 Value *getVectorOperand() { return Op<0>(); }
1682 Value *getIndexOperand() { return Op<1>(); }
1683 const Value *getVectorOperand() const { return Op<0>(); }
1684 const Value *getIndexOperand() const { return Op<1>(); }
1686 VectorType *getVectorOperandType() const {
1687 return cast<VectorType>(getVectorOperand()->getType());
1691 /// Transparently provide more efficient getOperand methods.
1692 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1694 // Methods for support type inquiry through isa, cast, and dyn_cast:
1695 static inline bool classof(const Instruction *I) {
1696 return I->getOpcode() == Instruction::ExtractElement;
1698 static inline bool classof(const Value *V) {
1699 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1704 struct OperandTraits<ExtractElementInst> :
1705 public FixedNumOperandTraits<ExtractElementInst, 2> {
1708 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1710 //===----------------------------------------------------------------------===//
1711 // InsertElementInst Class
1712 //===----------------------------------------------------------------------===//
1714 /// InsertElementInst - This instruction inserts a single (scalar)
1715 /// element into a VectorType value
1717 class InsertElementInst : public Instruction {
1718 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1719 const Twine &NameStr = "",
1720 Instruction *InsertBefore = nullptr);
1721 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1722 const Twine &NameStr, BasicBlock *InsertAtEnd);
1724 InsertElementInst *clone_impl() const override;
1727 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1728 const Twine &NameStr = "",
1729 Instruction *InsertBefore = nullptr) {
1730 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1732 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1733 const Twine &NameStr,
1734 BasicBlock *InsertAtEnd) {
1735 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1738 /// isValidOperands - Return true if an insertelement instruction can be
1739 /// formed with the specified operands.
1740 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1743 /// getType - Overload to return most specific vector type.
1745 VectorType *getType() const {
1746 return cast<VectorType>(Instruction::getType());
1749 /// Transparently provide more efficient getOperand methods.
1750 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1752 // Methods for support type inquiry through isa, cast, and dyn_cast:
1753 static inline bool classof(const Instruction *I) {
1754 return I->getOpcode() == Instruction::InsertElement;
1756 static inline bool classof(const Value *V) {
1757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1762 struct OperandTraits<InsertElementInst> :
1763 public FixedNumOperandTraits<InsertElementInst, 3> {
1766 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1768 //===----------------------------------------------------------------------===//
1769 // ShuffleVectorInst Class
1770 //===----------------------------------------------------------------------===//
1772 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1775 class ShuffleVectorInst : public Instruction {
1777 ShuffleVectorInst *clone_impl() const override;
1780 // allocate space for exactly three operands
1781 void *operator new(size_t s) {
1782 return User::operator new(s, 3);
1784 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1785 const Twine &NameStr = "",
1786 Instruction *InsertBefor = nullptr);
1787 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1788 const Twine &NameStr, BasicBlock *InsertAtEnd);
1790 /// isValidOperands - Return true if a shufflevector instruction can be
1791 /// formed with the specified operands.
1792 static bool isValidOperands(const Value *V1, const Value *V2,
1795 /// getType - Overload to return most specific vector type.
1797 VectorType *getType() const {
1798 return cast<VectorType>(Instruction::getType());
1801 /// Transparently provide more efficient getOperand methods.
1802 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1804 Constant *getMask() const {
1805 return cast<Constant>(getOperand(2));
1808 /// getMaskValue - Return the index from the shuffle mask for the specified
1809 /// output result. This is either -1 if the element is undef or a number less
1810 /// than 2*numelements.
1811 static int getMaskValue(Constant *Mask, unsigned i);
1813 int getMaskValue(unsigned i) const {
1814 return getMaskValue(getMask(), i);
1817 /// getShuffleMask - Return the full mask for this instruction, where each
1818 /// element is the element number and undef's are returned as -1.
1819 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1821 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1822 return getShuffleMask(getMask(), Result);
1825 SmallVector<int, 16> getShuffleMask() const {
1826 SmallVector<int, 16> Mask;
1827 getShuffleMask(Mask);
1832 // Methods for support type inquiry through isa, cast, and dyn_cast:
1833 static inline bool classof(const Instruction *I) {
1834 return I->getOpcode() == Instruction::ShuffleVector;
1836 static inline bool classof(const Value *V) {
1837 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1842 struct OperandTraits<ShuffleVectorInst> :
1843 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1846 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1848 //===----------------------------------------------------------------------===//
1849 // ExtractValueInst Class
1850 //===----------------------------------------------------------------------===//
1852 /// ExtractValueInst - This instruction extracts a struct member or array
1853 /// element value from an aggregate value.
1855 class ExtractValueInst : public UnaryInstruction {
1856 SmallVector<unsigned, 4> Indices;
1858 ExtractValueInst(const ExtractValueInst &EVI);
1859 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1861 /// Constructors - Create a extractvalue instruction with a base aggregate
1862 /// value and a list of indices. The first ctor can optionally insert before
1863 /// an existing instruction, the second appends the new instruction to the
1864 /// specified BasicBlock.
1865 inline ExtractValueInst(Value *Agg,
1866 ArrayRef<unsigned> Idxs,
1867 const Twine &NameStr,
1868 Instruction *InsertBefore);
1869 inline ExtractValueInst(Value *Agg,
1870 ArrayRef<unsigned> Idxs,
1871 const Twine &NameStr, BasicBlock *InsertAtEnd);
1873 // allocate space for exactly one operand
1874 void *operator new(size_t s) {
1875 return User::operator new(s, 1);
1878 ExtractValueInst *clone_impl() const override;
1881 static ExtractValueInst *Create(Value *Agg,
1882 ArrayRef<unsigned> Idxs,
1883 const Twine &NameStr = "",
1884 Instruction *InsertBefore = nullptr) {
1886 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1888 static ExtractValueInst *Create(Value *Agg,
1889 ArrayRef<unsigned> Idxs,
1890 const Twine &NameStr,
1891 BasicBlock *InsertAtEnd) {
1892 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1895 /// getIndexedType - Returns the type of the element that would be extracted
1896 /// with an extractvalue instruction with the specified parameters.
1898 /// Null is returned if the indices are invalid for the specified type.
1899 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1901 typedef const unsigned* idx_iterator;
1902 inline idx_iterator idx_begin() const { return Indices.begin(); }
1903 inline idx_iterator idx_end() const { return Indices.end(); }
1905 Value *getAggregateOperand() {
1906 return getOperand(0);
1908 const Value *getAggregateOperand() const {
1909 return getOperand(0);
1911 static unsigned getAggregateOperandIndex() {
1912 return 0U; // get index for modifying correct operand
1915 ArrayRef<unsigned> getIndices() const {
1919 unsigned getNumIndices() const {
1920 return (unsigned)Indices.size();
1923 bool hasIndices() const {
1927 // Methods for support type inquiry through isa, cast, and dyn_cast:
1928 static inline bool classof(const Instruction *I) {
1929 return I->getOpcode() == Instruction::ExtractValue;
1931 static inline bool classof(const Value *V) {
1932 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1936 ExtractValueInst::ExtractValueInst(Value *Agg,
1937 ArrayRef<unsigned> Idxs,
1938 const Twine &NameStr,
1939 Instruction *InsertBefore)
1940 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1941 ExtractValue, Agg, InsertBefore) {
1942 init(Idxs, NameStr);
1944 ExtractValueInst::ExtractValueInst(Value *Agg,
1945 ArrayRef<unsigned> Idxs,
1946 const Twine &NameStr,
1947 BasicBlock *InsertAtEnd)
1948 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1949 ExtractValue, Agg, InsertAtEnd) {
1950 init(Idxs, NameStr);
1954 //===----------------------------------------------------------------------===//
1955 // InsertValueInst Class
1956 //===----------------------------------------------------------------------===//
1958 /// InsertValueInst - This instruction inserts a struct field of array element
1959 /// value into an aggregate value.
1961 class InsertValueInst : public Instruction {
1962 SmallVector<unsigned, 4> Indices;
1964 void *operator new(size_t, unsigned) = delete;
1965 InsertValueInst(const InsertValueInst &IVI);
1966 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1967 const Twine &NameStr);
1969 /// Constructors - Create a insertvalue instruction with a base aggregate
1970 /// value, a value to insert, and a list of indices. The first ctor can
1971 /// optionally insert before an existing instruction, the second appends
1972 /// the new instruction to the specified BasicBlock.
1973 inline InsertValueInst(Value *Agg, Value *Val,
1974 ArrayRef<unsigned> Idxs,
1975 const Twine &NameStr,
1976 Instruction *InsertBefore);
1977 inline InsertValueInst(Value *Agg, Value *Val,
1978 ArrayRef<unsigned> Idxs,
1979 const Twine &NameStr, BasicBlock *InsertAtEnd);
1981 /// Constructors - These two constructors are convenience methods because one
1982 /// and two index insertvalue instructions are so common.
1983 InsertValueInst(Value *Agg, Value *Val,
1984 unsigned Idx, const Twine &NameStr = "",
1985 Instruction *InsertBefore = nullptr);
1986 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1987 const Twine &NameStr, BasicBlock *InsertAtEnd);
1989 InsertValueInst *clone_impl() const override;
1991 // allocate space for exactly two operands
1992 void *operator new(size_t s) {
1993 return User::operator new(s, 2);
1996 static InsertValueInst *Create(Value *Agg, Value *Val,
1997 ArrayRef<unsigned> Idxs,
1998 const Twine &NameStr = "",
1999 Instruction *InsertBefore = nullptr) {
2000 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2002 static InsertValueInst *Create(Value *Agg, Value *Val,
2003 ArrayRef<unsigned> Idxs,
2004 const Twine &NameStr,
2005 BasicBlock *InsertAtEnd) {
2006 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2009 /// Transparently provide more efficient getOperand methods.
2010 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2012 typedef const unsigned* idx_iterator;
2013 inline idx_iterator idx_begin() const { return Indices.begin(); }
2014 inline idx_iterator idx_end() const { return Indices.end(); }
2016 Value *getAggregateOperand() {
2017 return getOperand(0);
2019 const Value *getAggregateOperand() const {
2020 return getOperand(0);
2022 static unsigned getAggregateOperandIndex() {
2023 return 0U; // get index for modifying correct operand
2026 Value *getInsertedValueOperand() {
2027 return getOperand(1);
2029 const Value *getInsertedValueOperand() const {
2030 return getOperand(1);
2032 static unsigned getInsertedValueOperandIndex() {
2033 return 1U; // get index for modifying correct operand
2036 ArrayRef<unsigned> getIndices() const {
2040 unsigned getNumIndices() const {
2041 return (unsigned)Indices.size();
2044 bool hasIndices() const {
2048 // Methods for support type inquiry through isa, cast, and dyn_cast:
2049 static inline bool classof(const Instruction *I) {
2050 return I->getOpcode() == Instruction::InsertValue;
2052 static inline bool classof(const Value *V) {
2053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2058 struct OperandTraits<InsertValueInst> :
2059 public FixedNumOperandTraits<InsertValueInst, 2> {
2062 InsertValueInst::InsertValueInst(Value *Agg,
2064 ArrayRef<unsigned> Idxs,
2065 const Twine &NameStr,
2066 Instruction *InsertBefore)
2067 : Instruction(Agg->getType(), InsertValue,
2068 OperandTraits<InsertValueInst>::op_begin(this),
2070 init(Agg, Val, Idxs, NameStr);
2072 InsertValueInst::InsertValueInst(Value *Agg,
2074 ArrayRef<unsigned> Idxs,
2075 const Twine &NameStr,
2076 BasicBlock *InsertAtEnd)
2077 : Instruction(Agg->getType(), InsertValue,
2078 OperandTraits<InsertValueInst>::op_begin(this),
2080 init(Agg, Val, Idxs, NameStr);
2083 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2085 //===----------------------------------------------------------------------===//
2087 //===----------------------------------------------------------------------===//
2089 // PHINode - The PHINode class is used to represent the magical mystical PHI
2090 // node, that can not exist in nature, but can be synthesized in a computer
2091 // scientist's overactive imagination.
2093 class PHINode : public Instruction {
2094 void *operator new(size_t, unsigned) = delete;
2095 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2096 /// the number actually in use.
2097 unsigned ReservedSpace;
2098 PHINode(const PHINode &PN);
2099 // allocate space for exactly zero operands
2100 void *operator new(size_t s) {
2101 return User::operator new(s, 0);
2103 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2104 const Twine &NameStr = "",
2105 Instruction *InsertBefore = nullptr)
2106 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2107 ReservedSpace(NumReservedValues) {
2109 OperandList = allocHungoffUses(ReservedSpace);
2112 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2113 BasicBlock *InsertAtEnd)
2114 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2115 ReservedSpace(NumReservedValues) {
2117 OperandList = allocHungoffUses(ReservedSpace);
2120 // allocHungoffUses - this is more complicated than the generic
2121 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2122 // values and pointers to the incoming blocks, all in one allocation.
2123 Use *allocHungoffUses(unsigned) const;
2125 PHINode *clone_impl() const override;
2127 /// Constructors - NumReservedValues is a hint for the number of incoming
2128 /// edges that this phi node will have (use 0 if you really have no idea).
2129 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2130 const Twine &NameStr = "",
2131 Instruction *InsertBefore = nullptr) {
2132 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2134 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2135 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2136 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2140 /// Provide fast operand accessors
2141 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2143 // Block iterator interface. This provides access to the list of incoming
2144 // basic blocks, which parallels the list of incoming values.
2146 typedef BasicBlock **block_iterator;
2147 typedef BasicBlock * const *const_block_iterator;
2149 block_iterator block_begin() {
2151 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2152 return reinterpret_cast<block_iterator>(ref + 1);
2155 const_block_iterator block_begin() const {
2156 const Use::UserRef *ref =
2157 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2158 return reinterpret_cast<const_block_iterator>(ref + 1);
2161 block_iterator block_end() {
2162 return block_begin() + getNumOperands();
2165 const_block_iterator block_end() const {
2166 return block_begin() + getNumOperands();
2169 /// getNumIncomingValues - Return the number of incoming edges
2171 unsigned getNumIncomingValues() const { return getNumOperands(); }
2173 /// getIncomingValue - Return incoming value number x
2175 Value *getIncomingValue(unsigned i) const {
2176 return getOperand(i);
2178 void setIncomingValue(unsigned i, Value *V) {
2181 static unsigned getOperandNumForIncomingValue(unsigned i) {
2184 static unsigned getIncomingValueNumForOperand(unsigned i) {
2188 /// getIncomingBlock - Return incoming basic block number @p i.
2190 BasicBlock *getIncomingBlock(unsigned i) const {
2191 return block_begin()[i];
2194 /// getIncomingBlock - Return incoming basic block corresponding
2195 /// to an operand of the PHI.
2197 BasicBlock *getIncomingBlock(const Use &U) const {
2198 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2199 return getIncomingBlock(unsigned(&U - op_begin()));
2202 /// getIncomingBlock - Return incoming basic block corresponding
2203 /// to value use iterator.
2205 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2206 return getIncomingBlock(I.getUse());
2209 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2210 block_begin()[i] = BB;
2213 /// addIncoming - Add an incoming value to the end of the PHI list
2215 void addIncoming(Value *V, BasicBlock *BB) {
2216 assert(V && "PHI node got a null value!");
2217 assert(BB && "PHI node got a null basic block!");
2218 assert(getType() == V->getType() &&
2219 "All operands to PHI node must be the same type as the PHI node!");
2220 if (NumOperands == ReservedSpace)
2221 growOperands(); // Get more space!
2222 // Initialize some new operands.
2224 setIncomingValue(NumOperands - 1, V);
2225 setIncomingBlock(NumOperands - 1, BB);
2228 /// removeIncomingValue - Remove an incoming value. This is useful if a
2229 /// predecessor basic block is deleted. The value removed is returned.
2231 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2232 /// is true), the PHI node is destroyed and any uses of it are replaced with
2233 /// dummy values. The only time there should be zero incoming values to a PHI
2234 /// node is when the block is dead, so this strategy is sound.
2236 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2238 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2239 int Idx = getBasicBlockIndex(BB);
2240 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2241 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2244 /// getBasicBlockIndex - Return the first index of the specified basic
2245 /// block in the value list for this PHI. Returns -1 if no instance.
2247 int getBasicBlockIndex(const BasicBlock *BB) const {
2248 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2249 if (block_begin()[i] == BB)
2254 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2255 int Idx = getBasicBlockIndex(BB);
2256 assert(Idx >= 0 && "Invalid basic block argument!");
2257 return getIncomingValue(Idx);
2260 /// hasConstantValue - If the specified PHI node always merges together the
2261 /// same value, return the value, otherwise return null.
2262 Value *hasConstantValue() const;
2264 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2265 static inline bool classof(const Instruction *I) {
2266 return I->getOpcode() == Instruction::PHI;
2268 static inline bool classof(const Value *V) {
2269 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2272 void growOperands();
2276 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2279 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2281 //===----------------------------------------------------------------------===//
2282 // LandingPadInst Class
2283 //===----------------------------------------------------------------------===//
2285 //===---------------------------------------------------------------------------
2286 /// LandingPadInst - The landingpad instruction holds all of the information
2287 /// necessary to generate correct exception handling. The landingpad instruction
2288 /// cannot be moved from the top of a landing pad block, which itself is
2289 /// accessible only from the 'unwind' edge of an invoke. This uses the
2290 /// SubclassData field in Value to store whether or not the landingpad is a
2293 class LandingPadInst : public Instruction {
2294 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2295 /// the number actually in use.
2296 unsigned ReservedSpace;
2297 LandingPadInst(const LandingPadInst &LP);
2299 enum ClauseType { Catch, Filter };
2301 void *operator new(size_t, unsigned) = delete;
2302 // Allocate space for exactly zero operands.
2303 void *operator new(size_t s) {
2304 return User::operator new(s, 0);
2306 void growOperands(unsigned Size);
2307 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2309 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2310 unsigned NumReservedValues, const Twine &NameStr,
2311 Instruction *InsertBefore);
2312 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2313 unsigned NumReservedValues, const Twine &NameStr,
2314 BasicBlock *InsertAtEnd);
2316 LandingPadInst *clone_impl() const override;
2318 /// Constructors - NumReservedClauses is a hint for the number of incoming
2319 /// clauses that this landingpad will have (use 0 if you really have no idea).
2320 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2321 unsigned NumReservedClauses,
2322 const Twine &NameStr = "",
2323 Instruction *InsertBefore = nullptr);
2324 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2325 unsigned NumReservedClauses,
2326 const Twine &NameStr, BasicBlock *InsertAtEnd);
2329 /// Provide fast operand accessors
2330 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2332 /// getPersonalityFn - Get the personality function associated with this
2334 Value *getPersonalityFn() const { return getOperand(0); }
2336 /// isCleanup - Return 'true' if this landingpad instruction is a
2337 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2338 /// doesn't catch the exception.
2339 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2341 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2342 void setCleanup(bool V) {
2343 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2347 /// Add a catch or filter clause to the landing pad.
2348 void addClause(Constant *ClauseVal);
2350 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2351 /// determine what type of clause this is.
2352 Constant *getClause(unsigned Idx) const {
2353 return cast<Constant>(OperandList[Idx + 1]);
2356 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2357 bool isCatch(unsigned Idx) const {
2358 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2361 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2362 bool isFilter(unsigned Idx) const {
2363 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2366 /// getNumClauses - Get the number of clauses for this landing pad.
2367 unsigned getNumClauses() const { return getNumOperands() - 1; }
2369 /// reserveClauses - Grow the size of the operand list to accommodate the new
2370 /// number of clauses.
2371 void reserveClauses(unsigned Size) { growOperands(Size); }
2373 // Methods for support type inquiry through isa, cast, and dyn_cast:
2374 static inline bool classof(const Instruction *I) {
2375 return I->getOpcode() == Instruction::LandingPad;
2377 static inline bool classof(const Value *V) {
2378 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2383 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2386 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2388 //===----------------------------------------------------------------------===//
2390 //===----------------------------------------------------------------------===//
2392 //===---------------------------------------------------------------------------
2393 /// ReturnInst - Return a value (possibly void), from a function. Execution
2394 /// does not continue in this function any longer.
2396 class ReturnInst : public TerminatorInst {
2397 ReturnInst(const ReturnInst &RI);
2400 // ReturnInst constructors:
2401 // ReturnInst() - 'ret void' instruction
2402 // ReturnInst( null) - 'ret void' instruction
2403 // ReturnInst(Value* X) - 'ret X' instruction
2404 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2405 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2406 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2407 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2409 // NOTE: If the Value* passed is of type void then the constructor behaves as
2410 // if it was passed NULL.
2411 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2412 Instruction *InsertBefore = nullptr);
2413 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2414 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2416 ReturnInst *clone_impl() const override;
2418 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2419 Instruction *InsertBefore = nullptr) {
2420 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2422 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2423 BasicBlock *InsertAtEnd) {
2424 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2426 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2427 return new(0) ReturnInst(C, InsertAtEnd);
2429 virtual ~ReturnInst();
2431 /// Provide fast operand accessors
2432 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2434 /// Convenience accessor. Returns null if there is no return value.
2435 Value *getReturnValue() const {
2436 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2439 unsigned getNumSuccessors() const { return 0; }
2441 // Methods for support type inquiry through isa, cast, and dyn_cast:
2442 static inline bool classof(const Instruction *I) {
2443 return (I->getOpcode() == Instruction::Ret);
2445 static inline bool classof(const Value *V) {
2446 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2449 BasicBlock *getSuccessorV(unsigned idx) const override;
2450 unsigned getNumSuccessorsV() const override;
2451 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2455 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2458 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2460 //===----------------------------------------------------------------------===//
2462 //===----------------------------------------------------------------------===//
2464 //===---------------------------------------------------------------------------
2465 /// BranchInst - Conditional or Unconditional Branch instruction.
2467 class BranchInst : public TerminatorInst {
2468 /// Ops list - Branches are strange. The operands are ordered:
2469 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2470 /// they don't have to check for cond/uncond branchness. These are mostly
2471 /// accessed relative from op_end().
2472 BranchInst(const BranchInst &BI);
2474 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2475 // BranchInst(BB *B) - 'br B'
2476 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2477 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2478 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2479 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2480 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2481 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2482 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2483 Instruction *InsertBefore = nullptr);
2484 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2485 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2486 BasicBlock *InsertAtEnd);
2488 BranchInst *clone_impl() const override;
2490 static BranchInst *Create(BasicBlock *IfTrue,
2491 Instruction *InsertBefore = nullptr) {
2492 return new(1) BranchInst(IfTrue, InsertBefore);
2494 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2495 Value *Cond, Instruction *InsertBefore = nullptr) {
2496 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2498 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2499 return new(1) BranchInst(IfTrue, InsertAtEnd);
2501 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2502 Value *Cond, BasicBlock *InsertAtEnd) {
2503 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2506 /// Transparently provide more efficient getOperand methods.
2507 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2509 bool isUnconditional() const { return getNumOperands() == 1; }
2510 bool isConditional() const { return getNumOperands() == 3; }
2512 Value *getCondition() const {
2513 assert(isConditional() && "Cannot get condition of an uncond branch!");
2517 void setCondition(Value *V) {
2518 assert(isConditional() && "Cannot set condition of unconditional branch!");
2522 unsigned getNumSuccessors() const { return 1+isConditional(); }
2524 BasicBlock *getSuccessor(unsigned i) const {
2525 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2526 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2529 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2530 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2531 *(&Op<-1>() - idx) = (Value*)NewSucc;
2534 /// \brief Swap the successors of this branch instruction.
2536 /// Swaps the successors of the branch instruction. This also swaps any
2537 /// branch weight metadata associated with the instruction so that it
2538 /// continues to map correctly to each operand.
2539 void swapSuccessors();
2541 // Methods for support type inquiry through isa, cast, and dyn_cast:
2542 static inline bool classof(const Instruction *I) {
2543 return (I->getOpcode() == Instruction::Br);
2545 static inline bool classof(const Value *V) {
2546 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2549 BasicBlock *getSuccessorV(unsigned idx) const override;
2550 unsigned getNumSuccessorsV() const override;
2551 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2555 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2558 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2560 //===----------------------------------------------------------------------===//
2562 //===----------------------------------------------------------------------===//
2564 //===---------------------------------------------------------------------------
2565 /// SwitchInst - Multiway switch
2567 class SwitchInst : public TerminatorInst {
2568 void *operator new(size_t, unsigned) = delete;
2569 unsigned ReservedSpace;
2570 // Operand[0] = Value to switch on
2571 // Operand[1] = Default basic block destination
2572 // Operand[2n ] = Value to match
2573 // Operand[2n+1] = BasicBlock to go to on match
2574 SwitchInst(const SwitchInst &SI);
2575 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2576 void growOperands();
2577 // allocate space for exactly zero operands
2578 void *operator new(size_t s) {
2579 return User::operator new(s, 0);
2581 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2582 /// switch on and a default destination. The number of additional cases can
2583 /// be specified here to make memory allocation more efficient. This
2584 /// constructor can also autoinsert before another instruction.
2585 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2586 Instruction *InsertBefore);
2588 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2589 /// switch on and a default destination. The number of additional cases can
2590 /// be specified here to make memory allocation more efficient. This
2591 /// constructor also autoinserts at the end of the specified BasicBlock.
2592 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2593 BasicBlock *InsertAtEnd);
2595 SwitchInst *clone_impl() const override;
2599 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2601 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2602 class CaseIteratorT {
2610 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2612 /// Initializes case iterator for given SwitchInst and for given
2614 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2619 /// Initializes case iterator for given SwitchInst and for given
2620 /// TerminatorInst's successor index.
2621 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2622 assert(SuccessorIndex < SI->getNumSuccessors() &&
2623 "Successor index # out of range!");
2624 return SuccessorIndex != 0 ?
2625 Self(SI, SuccessorIndex - 1) :
2626 Self(SI, DefaultPseudoIndex);
2629 /// Resolves case value for current case.
2630 ConstantIntTy *getCaseValue() {
2631 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2632 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2635 /// Resolves successor for current case.
2636 BasicBlockTy *getCaseSuccessor() {
2637 assert((Index < SI->getNumCases() ||
2638 Index == DefaultPseudoIndex) &&
2639 "Index out the number of cases.");
2640 return SI->getSuccessor(getSuccessorIndex());
2643 /// Returns number of current case.
2644 unsigned getCaseIndex() const { return Index; }
2646 /// Returns TerminatorInst's successor index for current case successor.
2647 unsigned getSuccessorIndex() const {
2648 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2649 "Index out the number of cases.");
2650 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2654 // Check index correctness after increment.
2655 // Note: Index == getNumCases() means end().
2656 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2660 Self operator++(int) {
2666 // Check index correctness after decrement.
2667 // Note: Index == getNumCases() means end().
2668 // Also allow "-1" iterator here. That will became valid after ++.
2669 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2670 "Index out the number of cases.");
2674 Self operator--(int) {
2679 bool operator==(const Self& RHS) const {
2680 assert(RHS.SI == SI && "Incompatible operators.");
2681 return RHS.Index == Index;
2683 bool operator!=(const Self& RHS) const {
2684 assert(RHS.SI == SI && "Incompatible operators.");
2685 return RHS.Index != Index;
2692 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2695 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2697 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2701 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2702 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2704 /// Sets the new value for current case.
2705 void setValue(ConstantInt *V) {
2706 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2707 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2710 /// Sets the new successor for current case.
2711 void setSuccessor(BasicBlock *S) {
2712 SI->setSuccessor(getSuccessorIndex(), S);
2716 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2718 Instruction *InsertBefore = nullptr) {
2719 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2721 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2722 unsigned NumCases, BasicBlock *InsertAtEnd) {
2723 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2728 /// Provide fast operand accessors
2729 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2731 // Accessor Methods for Switch stmt
2732 Value *getCondition() const { return getOperand(0); }
2733 void setCondition(Value *V) { setOperand(0, V); }
2735 BasicBlock *getDefaultDest() const {
2736 return cast<BasicBlock>(getOperand(1));
2739 void setDefaultDest(BasicBlock *DefaultCase) {
2740 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2743 /// getNumCases - return the number of 'cases' in this switch instruction,
2744 /// except the default case
2745 unsigned getNumCases() const {
2746 return getNumOperands()/2 - 1;
2749 /// Returns a read/write iterator that points to the first
2750 /// case in SwitchInst.
2751 CaseIt case_begin() {
2752 return CaseIt(this, 0);
2754 /// Returns a read-only iterator that points to the first
2755 /// case in the SwitchInst.
2756 ConstCaseIt case_begin() const {
2757 return ConstCaseIt(this, 0);
2760 /// Returns a read/write iterator that points one past the last
2761 /// in the SwitchInst.
2763 return CaseIt(this, getNumCases());
2765 /// Returns a read-only iterator that points one past the last
2766 /// in the SwitchInst.
2767 ConstCaseIt case_end() const {
2768 return ConstCaseIt(this, getNumCases());
2771 /// cases - iteration adapter for range-for loops.
2772 iterator_range<CaseIt> cases() {
2773 return iterator_range<CaseIt>(case_begin(), case_end());
2776 /// cases - iteration adapter for range-for loops.
2777 iterator_range<ConstCaseIt> cases() const {
2778 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2781 /// Returns an iterator that points to the default case.
2782 /// Note: this iterator allows to resolve successor only. Attempt
2783 /// to resolve case value causes an assertion.
2784 /// Also note, that increment and decrement also causes an assertion and
2785 /// makes iterator invalid.
2786 CaseIt case_default() {
2787 return CaseIt(this, DefaultPseudoIndex);
2789 ConstCaseIt case_default() const {
2790 return ConstCaseIt(this, DefaultPseudoIndex);
2793 /// findCaseValue - Search all of the case values for the specified constant.
2794 /// If it is explicitly handled, return the case iterator of it, otherwise
2795 /// return default case iterator to indicate
2796 /// that it is handled by the default handler.
2797 CaseIt findCaseValue(const ConstantInt *C) {
2798 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2799 if (i.getCaseValue() == C)
2801 return case_default();
2803 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2804 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2805 if (i.getCaseValue() == C)
2807 return case_default();
2810 /// findCaseDest - Finds the unique case value for a given successor. Returns
2811 /// null if the successor is not found, not unique, or is the default case.
2812 ConstantInt *findCaseDest(BasicBlock *BB) {
2813 if (BB == getDefaultDest()) return nullptr;
2815 ConstantInt *CI = nullptr;
2816 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2817 if (i.getCaseSuccessor() == BB) {
2818 if (CI) return nullptr; // Multiple cases lead to BB.
2819 else CI = i.getCaseValue();
2825 /// addCase - Add an entry to the switch instruction...
2827 /// This action invalidates case_end(). Old case_end() iterator will
2828 /// point to the added case.
2829 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2831 /// removeCase - This method removes the specified case and its successor
2832 /// from the switch instruction. Note that this operation may reorder the
2833 /// remaining cases at index idx and above.
2835 /// This action invalidates iterators for all cases following the one removed,
2836 /// including the case_end() iterator.
2837 void removeCase(CaseIt i);
2839 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2840 BasicBlock *getSuccessor(unsigned idx) const {
2841 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2842 return cast<BasicBlock>(getOperand(idx*2+1));
2844 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2845 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2846 setOperand(idx*2+1, (Value*)NewSucc);
2849 // Methods for support type inquiry through isa, cast, and dyn_cast:
2850 static inline bool classof(const Instruction *I) {
2851 return I->getOpcode() == Instruction::Switch;
2853 static inline bool classof(const Value *V) {
2854 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2857 BasicBlock *getSuccessorV(unsigned idx) const override;
2858 unsigned getNumSuccessorsV() const override;
2859 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2863 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2866 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2869 //===----------------------------------------------------------------------===//
2870 // IndirectBrInst Class
2871 //===----------------------------------------------------------------------===//
2873 //===---------------------------------------------------------------------------
2874 /// IndirectBrInst - Indirect Branch Instruction.
2876 class IndirectBrInst : public TerminatorInst {
2877 void *operator new(size_t, unsigned) = delete;
2878 unsigned ReservedSpace;
2879 // Operand[0] = Value to switch on
2880 // Operand[1] = Default basic block destination
2881 // Operand[2n ] = Value to match
2882 // Operand[2n+1] = BasicBlock to go to on match
2883 IndirectBrInst(const IndirectBrInst &IBI);
2884 void init(Value *Address, unsigned NumDests);
2885 void growOperands();
2886 // allocate space for exactly zero operands
2887 void *operator new(size_t s) {
2888 return User::operator new(s, 0);
2890 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2891 /// Address to jump to. The number of expected destinations can be specified
2892 /// here to make memory allocation more efficient. This constructor can also
2893 /// autoinsert before another instruction.
2894 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2896 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2897 /// Address to jump to. The number of expected destinations can be specified
2898 /// here to make memory allocation more efficient. This constructor also
2899 /// autoinserts at the end of the specified BasicBlock.
2900 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2902 IndirectBrInst *clone_impl() const override;
2904 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2905 Instruction *InsertBefore = nullptr) {
2906 return new IndirectBrInst(Address, NumDests, InsertBefore);
2908 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2909 BasicBlock *InsertAtEnd) {
2910 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2914 /// Provide fast operand accessors.
2915 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2917 // Accessor Methods for IndirectBrInst instruction.
2918 Value *getAddress() { return getOperand(0); }
2919 const Value *getAddress() const { return getOperand(0); }
2920 void setAddress(Value *V) { setOperand(0, V); }
2923 /// getNumDestinations - return the number of possible destinations in this
2924 /// indirectbr instruction.
2925 unsigned getNumDestinations() const { return getNumOperands()-1; }
2927 /// getDestination - Return the specified destination.
2928 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2929 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2931 /// addDestination - Add a destination.
2933 void addDestination(BasicBlock *Dest);
2935 /// removeDestination - This method removes the specified successor from the
2936 /// indirectbr instruction.
2937 void removeDestination(unsigned i);
2939 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2940 BasicBlock *getSuccessor(unsigned i) const {
2941 return cast<BasicBlock>(getOperand(i+1));
2943 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2944 setOperand(i+1, (Value*)NewSucc);
2947 // Methods for support type inquiry through isa, cast, and dyn_cast:
2948 static inline bool classof(const Instruction *I) {
2949 return I->getOpcode() == Instruction::IndirectBr;
2951 static inline bool classof(const Value *V) {
2952 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2955 BasicBlock *getSuccessorV(unsigned idx) const override;
2956 unsigned getNumSuccessorsV() const override;
2957 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2961 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2964 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2967 //===----------------------------------------------------------------------===//
2969 //===----------------------------------------------------------------------===//
2971 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2972 /// calling convention of the call.
2974 class InvokeInst : public TerminatorInst {
2975 AttributeSet AttributeList;
2976 InvokeInst(const InvokeInst &BI);
2977 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2978 ArrayRef<Value *> Args, const Twine &NameStr);
2980 /// Construct an InvokeInst given a range of arguments.
2982 /// \brief Construct an InvokeInst from a range of arguments
2983 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2984 ArrayRef<Value *> Args, unsigned Values,
2985 const Twine &NameStr, Instruction *InsertBefore);
2987 /// Construct an InvokeInst given a range of arguments.
2989 /// \brief Construct an InvokeInst from a range of arguments
2990 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2991 ArrayRef<Value *> Args, unsigned Values,
2992 const Twine &NameStr, BasicBlock *InsertAtEnd);
2994 InvokeInst *clone_impl() const override;
2996 static InvokeInst *Create(Value *Func,
2997 BasicBlock *IfNormal, BasicBlock *IfException,
2998 ArrayRef<Value *> Args, const Twine &NameStr = "",
2999 Instruction *InsertBefore = nullptr) {
3000 unsigned Values = unsigned(Args.size()) + 3;
3001 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3002 Values, NameStr, InsertBefore);
3004 static InvokeInst *Create(Value *Func,
3005 BasicBlock *IfNormal, BasicBlock *IfException,
3006 ArrayRef<Value *> Args, const Twine &NameStr,
3007 BasicBlock *InsertAtEnd) {
3008 unsigned Values = unsigned(Args.size()) + 3;
3009 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3010 Values, NameStr, InsertAtEnd);
3013 /// Provide fast operand accessors
3014 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3016 /// getNumArgOperands - Return the number of invoke arguments.
3018 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3020 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3022 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3023 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3025 /// arg_operands - iteration adapter for range-for loops.
3026 iterator_range<op_iterator> arg_operands() {
3027 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3030 /// arg_operands - iteration adapter for range-for loops.
3031 iterator_range<const_op_iterator> arg_operands() const {
3032 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3035 /// \brief Wrappers for getting the \c Use of a invoke argument.
3036 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3037 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3039 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3041 CallingConv::ID getCallingConv() const {
3042 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3044 void setCallingConv(CallingConv::ID CC) {
3045 setInstructionSubclassData(static_cast<unsigned>(CC));
3048 /// getAttributes - Return the parameter attributes for this invoke.
3050 const AttributeSet &getAttributes() const { return AttributeList; }
3052 /// setAttributes - Set the parameter attributes for this invoke.
3054 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3056 /// addAttribute - adds the attribute to the list of attributes.
3057 void addAttribute(unsigned i, Attribute::AttrKind attr);
3059 /// removeAttribute - removes the attribute from the list of attributes.
3060 void removeAttribute(unsigned i, Attribute attr);
3062 /// \brief removes the dereferenceable attribute to the list of attributes.
3063 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3065 /// \brief Determine whether this call has the given attribute.
3066 bool hasFnAttr(Attribute::AttrKind A) const {
3067 assert(A != Attribute::NoBuiltin &&
3068 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3069 return hasFnAttrImpl(A);
3072 /// \brief Determine whether the call or the callee has the given attributes.
3073 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3075 /// \brief Extract the alignment for a call or parameter (0=unknown).
3076 unsigned getParamAlignment(unsigned i) const {
3077 return AttributeList.getParamAlignment(i);
3080 /// \brief Extract the number of dereferenceable bytes for a call or
3081 /// parameter (0=unknown).
3082 uint64_t getDereferenceableBytes(unsigned i) const {
3083 return AttributeList.getDereferenceableBytes(i);
3086 /// \brief Return true if the call should not be treated as a call to a
3088 bool isNoBuiltin() const {
3089 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3090 // to check it by hand.
3091 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3092 !hasFnAttrImpl(Attribute::Builtin);
3095 /// \brief Return true if the call should not be inlined.
3096 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3097 void setIsNoInline() {
3098 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3101 /// \brief Determine if the call does not access memory.
3102 bool doesNotAccessMemory() const {
3103 return hasFnAttr(Attribute::ReadNone);
3105 void setDoesNotAccessMemory() {
3106 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3109 /// \brief Determine if the call does not access or only reads memory.
3110 bool onlyReadsMemory() const {
3111 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3113 void setOnlyReadsMemory() {
3114 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3117 /// \brief Determine if the call cannot return.
3118 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3119 void setDoesNotReturn() {
3120 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3123 /// \brief Determine if the call cannot unwind.
3124 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3125 void setDoesNotThrow() {
3126 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3129 /// \brief Determine if the invoke cannot be duplicated.
3130 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3131 void setCannotDuplicate() {
3132 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3135 /// \brief Determine if the call returns a structure through first
3136 /// pointer argument.
3137 bool hasStructRetAttr() const {
3138 // Be friendly and also check the callee.
3139 return paramHasAttr(1, Attribute::StructRet);
3142 /// \brief Determine if any call argument is an aggregate passed by value.
3143 bool hasByValArgument() const {
3144 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3147 /// getCalledFunction - Return the function called, or null if this is an
3148 /// indirect function invocation.
3150 Function *getCalledFunction() const {
3151 return dyn_cast<Function>(Op<-3>());
3154 /// getCalledValue - Get a pointer to the function that is invoked by this
3156 const Value *getCalledValue() const { return Op<-3>(); }
3157 Value *getCalledValue() { return Op<-3>(); }
3159 /// setCalledFunction - Set the function called.
3160 void setCalledFunction(Value* Fn) {
3164 // get*Dest - Return the destination basic blocks...
3165 BasicBlock *getNormalDest() const {
3166 return cast<BasicBlock>(Op<-2>());
3168 BasicBlock *getUnwindDest() const {
3169 return cast<BasicBlock>(Op<-1>());
3171 void setNormalDest(BasicBlock *B) {
3172 Op<-2>() = reinterpret_cast<Value*>(B);
3174 void setUnwindDest(BasicBlock *B) {
3175 Op<-1>() = reinterpret_cast<Value*>(B);
3178 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3179 /// block (the unwind destination).
3180 LandingPadInst *getLandingPadInst() const;
3182 BasicBlock *getSuccessor(unsigned i) const {
3183 assert(i < 2 && "Successor # out of range for invoke!");
3184 return i == 0 ? getNormalDest() : getUnwindDest();
3187 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3188 assert(idx < 2 && "Successor # out of range for invoke!");
3189 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3192 unsigned getNumSuccessors() const { return 2; }
3194 // Methods for support type inquiry through isa, cast, and dyn_cast:
3195 static inline bool classof(const Instruction *I) {
3196 return (I->getOpcode() == Instruction::Invoke);
3198 static inline bool classof(const Value *V) {
3199 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3203 BasicBlock *getSuccessorV(unsigned idx) const override;
3204 unsigned getNumSuccessorsV() const override;
3205 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3207 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3209 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3210 // method so that subclasses cannot accidentally use it.
3211 void setInstructionSubclassData(unsigned short D) {
3212 Instruction::setInstructionSubclassData(D);
3217 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3220 InvokeInst::InvokeInst(Value *Func,
3221 BasicBlock *IfNormal, BasicBlock *IfException,
3222 ArrayRef<Value *> Args, unsigned Values,
3223 const Twine &NameStr, Instruction *InsertBefore)
3224 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3225 ->getElementType())->getReturnType(),
3226 Instruction::Invoke,
3227 OperandTraits<InvokeInst>::op_end(this) - Values,
3228 Values, InsertBefore) {
3229 init(Func, IfNormal, IfException, Args, NameStr);
3231 InvokeInst::InvokeInst(Value *Func,
3232 BasicBlock *IfNormal, BasicBlock *IfException,
3233 ArrayRef<Value *> Args, unsigned Values,
3234 const Twine &NameStr, BasicBlock *InsertAtEnd)
3235 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3236 ->getElementType())->getReturnType(),
3237 Instruction::Invoke,
3238 OperandTraits<InvokeInst>::op_end(this) - Values,
3239 Values, InsertAtEnd) {
3240 init(Func, IfNormal, IfException, Args, NameStr);
3243 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3245 //===----------------------------------------------------------------------===//
3247 //===----------------------------------------------------------------------===//
3249 //===---------------------------------------------------------------------------
3250 /// ResumeInst - Resume the propagation of an exception.
3252 class ResumeInst : public TerminatorInst {
3253 ResumeInst(const ResumeInst &RI);
3255 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3256 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3258 ResumeInst *clone_impl() const override;
3260 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3261 return new(1) ResumeInst(Exn, InsertBefore);
3263 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3264 return new(1) ResumeInst(Exn, InsertAtEnd);
3267 /// Provide fast operand accessors
3268 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3270 /// Convenience accessor.
3271 Value *getValue() const { return Op<0>(); }
3273 unsigned getNumSuccessors() const { return 0; }
3275 // Methods for support type inquiry through isa, cast, and dyn_cast:
3276 static inline bool classof(const Instruction *I) {
3277 return I->getOpcode() == Instruction::Resume;
3279 static inline bool classof(const Value *V) {
3280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3283 BasicBlock *getSuccessorV(unsigned idx) const override;
3284 unsigned getNumSuccessorsV() const override;
3285 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3289 struct OperandTraits<ResumeInst> :
3290 public FixedNumOperandTraits<ResumeInst, 1> {
3293 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3295 //===----------------------------------------------------------------------===//
3296 // UnreachableInst Class
3297 //===----------------------------------------------------------------------===//
3299 //===---------------------------------------------------------------------------
3300 /// UnreachableInst - This function has undefined behavior. In particular, the
3301 /// presence of this instruction indicates some higher level knowledge that the
3302 /// end of the block cannot be reached.
3304 class UnreachableInst : public TerminatorInst {
3305 void *operator new(size_t, unsigned) = delete;
3307 UnreachableInst *clone_impl() const override;
3310 // allocate space for exactly zero operands
3311 void *operator new(size_t s) {
3312 return User::operator new(s, 0);
3314 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3315 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3317 unsigned getNumSuccessors() const { return 0; }
3319 // Methods for support type inquiry through isa, cast, and dyn_cast:
3320 static inline bool classof(const Instruction *I) {
3321 return I->getOpcode() == Instruction::Unreachable;
3323 static inline bool classof(const Value *V) {
3324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3327 BasicBlock *getSuccessorV(unsigned idx) const override;
3328 unsigned getNumSuccessorsV() const override;
3329 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3332 //===----------------------------------------------------------------------===//
3334 //===----------------------------------------------------------------------===//
3336 /// \brief This class represents a truncation of integer types.
3337 class TruncInst : public CastInst {
3339 /// \brief Clone an identical TruncInst
3340 TruncInst *clone_impl() const override;
3343 /// \brief Constructor with insert-before-instruction semantics
3345 Value *S, ///< The value to be truncated
3346 Type *Ty, ///< The (smaller) type to truncate to
3347 const Twine &NameStr = "", ///< A name for the new instruction
3348 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3351 /// \brief Constructor with insert-at-end-of-block semantics
3353 Value *S, ///< The value to be truncated
3354 Type *Ty, ///< The (smaller) type to truncate to
3355 const Twine &NameStr, ///< A name for the new instruction
3356 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3359 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3360 static inline bool classof(const Instruction *I) {
3361 return I->getOpcode() == Trunc;
3363 static inline bool classof(const Value *V) {
3364 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3368 //===----------------------------------------------------------------------===//
3370 //===----------------------------------------------------------------------===//
3372 /// \brief This class represents zero extension of integer types.
3373 class ZExtInst : public CastInst {
3375 /// \brief Clone an identical ZExtInst
3376 ZExtInst *clone_impl() const override;
3379 /// \brief Constructor with insert-before-instruction semantics
3381 Value *S, ///< The value to be zero extended
3382 Type *Ty, ///< The type to zero extend to
3383 const Twine &NameStr = "", ///< A name for the new instruction
3384 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3387 /// \brief Constructor with insert-at-end semantics.
3389 Value *S, ///< The value to be zero extended
3390 Type *Ty, ///< The type to zero extend to
3391 const Twine &NameStr, ///< A name for the new instruction
3392 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3395 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3396 static inline bool classof(const Instruction *I) {
3397 return I->getOpcode() == ZExt;
3399 static inline bool classof(const Value *V) {
3400 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3404 //===----------------------------------------------------------------------===//
3406 //===----------------------------------------------------------------------===//
3408 /// \brief This class represents a sign extension of integer types.
3409 class SExtInst : public CastInst {
3411 /// \brief Clone an identical SExtInst
3412 SExtInst *clone_impl() const override;
3415 /// \brief Constructor with insert-before-instruction semantics
3417 Value *S, ///< The value to be sign extended
3418 Type *Ty, ///< The type to sign extend to
3419 const Twine &NameStr = "", ///< A name for the new instruction
3420 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3423 /// \brief Constructor with insert-at-end-of-block semantics
3425 Value *S, ///< The value to be sign extended
3426 Type *Ty, ///< The type to sign extend to
3427 const Twine &NameStr, ///< A name for the new instruction
3428 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3431 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3432 static inline bool classof(const Instruction *I) {
3433 return I->getOpcode() == SExt;
3435 static inline bool classof(const Value *V) {
3436 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3440 //===----------------------------------------------------------------------===//
3441 // FPTruncInst Class
3442 //===----------------------------------------------------------------------===//
3444 /// \brief This class represents a truncation of floating point types.
3445 class FPTruncInst : public CastInst {
3447 /// \brief Clone an identical FPTruncInst
3448 FPTruncInst *clone_impl() const override;
3451 /// \brief Constructor with insert-before-instruction semantics
3453 Value *S, ///< The value to be truncated
3454 Type *Ty, ///< The type to truncate to
3455 const Twine &NameStr = "", ///< A name for the new instruction
3456 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3459 /// \brief Constructor with insert-before-instruction semantics
3461 Value *S, ///< The value to be truncated
3462 Type *Ty, ///< The type to truncate to
3463 const Twine &NameStr, ///< A name for the new instruction
3464 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3467 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3468 static inline bool classof(const Instruction *I) {
3469 return I->getOpcode() == FPTrunc;
3471 static inline bool classof(const Value *V) {
3472 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3476 //===----------------------------------------------------------------------===//
3478 //===----------------------------------------------------------------------===//
3480 /// \brief This class represents an extension of floating point types.
3481 class FPExtInst : public CastInst {
3483 /// \brief Clone an identical FPExtInst
3484 FPExtInst *clone_impl() const override;
3487 /// \brief Constructor with insert-before-instruction semantics
3489 Value *S, ///< The value to be extended
3490 Type *Ty, ///< The type to extend to
3491 const Twine &NameStr = "", ///< A name for the new instruction
3492 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3495 /// \brief Constructor with insert-at-end-of-block semantics
3497 Value *S, ///< The value to be extended
3498 Type *Ty, ///< The type to extend to
3499 const Twine &NameStr, ///< A name for the new instruction
3500 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3503 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3504 static inline bool classof(const Instruction *I) {
3505 return I->getOpcode() == FPExt;
3507 static inline bool classof(const Value *V) {
3508 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3512 //===----------------------------------------------------------------------===//
3514 //===----------------------------------------------------------------------===//
3516 /// \brief This class represents a cast unsigned integer to floating point.
3517 class UIToFPInst : public CastInst {
3519 /// \brief Clone an identical UIToFPInst
3520 UIToFPInst *clone_impl() const override;
3523 /// \brief Constructor with insert-before-instruction semantics
3525 Value *S, ///< The value to be converted
3526 Type *Ty, ///< The type to convert to
3527 const Twine &NameStr = "", ///< A name for the new instruction
3528 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3531 /// \brief Constructor with insert-at-end-of-block semantics
3533 Value *S, ///< The value to be converted
3534 Type *Ty, ///< The type to convert to
3535 const Twine &NameStr, ///< A name for the new instruction
3536 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3539 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3540 static inline bool classof(const Instruction *I) {
3541 return I->getOpcode() == UIToFP;
3543 static inline bool classof(const Value *V) {
3544 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3548 //===----------------------------------------------------------------------===//
3550 //===----------------------------------------------------------------------===//
3552 /// \brief This class represents a cast from signed integer to floating point.
3553 class SIToFPInst : public CastInst {
3555 /// \brief Clone an identical SIToFPInst
3556 SIToFPInst *clone_impl() const override;
3559 /// \brief Constructor with insert-before-instruction semantics
3561 Value *S, ///< The value to be converted
3562 Type *Ty, ///< The type to convert to
3563 const Twine &NameStr = "", ///< A name for the new instruction
3564 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3567 /// \brief Constructor with insert-at-end-of-block semantics
3569 Value *S, ///< The value to be converted
3570 Type *Ty, ///< The type to convert to
3571 const Twine &NameStr, ///< A name for the new instruction
3572 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3575 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3576 static inline bool classof(const Instruction *I) {
3577 return I->getOpcode() == SIToFP;
3579 static inline bool classof(const Value *V) {
3580 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3584 //===----------------------------------------------------------------------===//
3586 //===----------------------------------------------------------------------===//
3588 /// \brief This class represents a cast from floating point to unsigned integer
3589 class FPToUIInst : public CastInst {
3591 /// \brief Clone an identical FPToUIInst
3592 FPToUIInst *clone_impl() const override;
3595 /// \brief Constructor with insert-before-instruction semantics
3597 Value *S, ///< The value to be converted
3598 Type *Ty, ///< The type to convert to
3599 const Twine &NameStr = "", ///< A name for the new instruction
3600 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3603 /// \brief Constructor with insert-at-end-of-block semantics
3605 Value *S, ///< The value to be converted
3606 Type *Ty, ///< The type to convert to
3607 const Twine &NameStr, ///< A name for the new instruction
3608 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3611 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3612 static inline bool classof(const Instruction *I) {
3613 return I->getOpcode() == FPToUI;
3615 static inline bool classof(const Value *V) {
3616 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3620 //===----------------------------------------------------------------------===//
3622 //===----------------------------------------------------------------------===//
3624 /// \brief This class represents a cast from floating point to signed integer.
3625 class FPToSIInst : public CastInst {
3627 /// \brief Clone an identical FPToSIInst
3628 FPToSIInst *clone_impl() const override;
3631 /// \brief Constructor with insert-before-instruction semantics
3633 Value *S, ///< The value to be converted
3634 Type *Ty, ///< The type to convert to
3635 const Twine &NameStr = "", ///< A name for the new instruction
3636 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3639 /// \brief Constructor with insert-at-end-of-block semantics
3641 Value *S, ///< The value to be converted
3642 Type *Ty, ///< The type to convert to
3643 const Twine &NameStr, ///< A name for the new instruction
3644 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3647 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3648 static inline bool classof(const Instruction *I) {
3649 return I->getOpcode() == FPToSI;
3651 static inline bool classof(const Value *V) {
3652 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3656 //===----------------------------------------------------------------------===//
3657 // IntToPtrInst Class
3658 //===----------------------------------------------------------------------===//
3660 /// \brief This class represents a cast from an integer to a pointer.
3661 class IntToPtrInst : public CastInst {
3663 /// \brief Constructor with insert-before-instruction semantics
3665 Value *S, ///< The value to be converted
3666 Type *Ty, ///< The type to convert to
3667 const Twine &NameStr = "", ///< A name for the new instruction
3668 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3671 /// \brief Constructor with insert-at-end-of-block semantics
3673 Value *S, ///< The value to be converted
3674 Type *Ty, ///< The type to convert to
3675 const Twine &NameStr, ///< A name for the new instruction
3676 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3679 /// \brief Clone an identical IntToPtrInst
3680 IntToPtrInst *clone_impl() const override;
3682 /// \brief Returns the address space of this instruction's pointer type.
3683 unsigned getAddressSpace() const {
3684 return getType()->getPointerAddressSpace();
3687 // Methods for support type inquiry through isa, cast, and dyn_cast:
3688 static inline bool classof(const Instruction *I) {
3689 return I->getOpcode() == IntToPtr;
3691 static inline bool classof(const Value *V) {
3692 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3696 //===----------------------------------------------------------------------===//
3697 // PtrToIntInst Class
3698 //===----------------------------------------------------------------------===//
3700 /// \brief This class represents a cast from a pointer to an integer
3701 class PtrToIntInst : public CastInst {
3703 /// \brief Clone an identical PtrToIntInst
3704 PtrToIntInst *clone_impl() const override;
3707 /// \brief Constructor with insert-before-instruction semantics
3709 Value *S, ///< The value to be converted
3710 Type *Ty, ///< The type to convert to
3711 const Twine &NameStr = "", ///< A name for the new instruction
3712 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3715 /// \brief Constructor with insert-at-end-of-block semantics
3717 Value *S, ///< The value to be converted
3718 Type *Ty, ///< The type to convert to
3719 const Twine &NameStr, ///< A name for the new instruction
3720 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3723 /// \brief Gets the pointer operand.
3724 Value *getPointerOperand() { return getOperand(0); }
3725 /// \brief Gets the pointer operand.
3726 const Value *getPointerOperand() const { return getOperand(0); }
3727 /// \brief Gets the operand index of the pointer operand.
3728 static unsigned getPointerOperandIndex() { return 0U; }
3730 /// \brief Returns the address space of the pointer operand.
3731 unsigned getPointerAddressSpace() const {
3732 return getPointerOperand()->getType()->getPointerAddressSpace();
3735 // Methods for support type inquiry through isa, cast, and dyn_cast:
3736 static inline bool classof(const Instruction *I) {
3737 return I->getOpcode() == PtrToInt;
3739 static inline bool classof(const Value *V) {
3740 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3744 //===----------------------------------------------------------------------===//
3745 // BitCastInst Class
3746 //===----------------------------------------------------------------------===//
3748 /// \brief This class represents a no-op cast from one type to another.
3749 class BitCastInst : public CastInst {
3751 /// \brief Clone an identical BitCastInst
3752 BitCastInst *clone_impl() const override;
3755 /// \brief Constructor with insert-before-instruction semantics
3757 Value *S, ///< The value to be casted
3758 Type *Ty, ///< The type to casted to
3759 const Twine &NameStr = "", ///< A name for the new instruction
3760 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3763 /// \brief Constructor with insert-at-end-of-block semantics
3765 Value *S, ///< The value to be casted
3766 Type *Ty, ///< The type to casted to
3767 const Twine &NameStr, ///< A name for the new instruction
3768 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3771 // Methods for support type inquiry through isa, cast, and dyn_cast:
3772 static inline bool classof(const Instruction *I) {
3773 return I->getOpcode() == BitCast;
3775 static inline bool classof(const Value *V) {
3776 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3780 //===----------------------------------------------------------------------===//
3781 // AddrSpaceCastInst Class
3782 //===----------------------------------------------------------------------===//
3784 /// \brief This class represents a conversion between pointers from
3785 /// one address space to another.
3786 class AddrSpaceCastInst : public CastInst {
3788 /// \brief Clone an identical AddrSpaceCastInst
3789 AddrSpaceCastInst *clone_impl() const override;
3792 /// \brief Constructor with insert-before-instruction semantics
3794 Value *S, ///< The value to be casted
3795 Type *Ty, ///< The type to casted to
3796 const Twine &NameStr = "", ///< A name for the new instruction
3797 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3800 /// \brief Constructor with insert-at-end-of-block semantics
3802 Value *S, ///< The value to be casted
3803 Type *Ty, ///< The type to casted to
3804 const Twine &NameStr, ///< A name for the new instruction
3805 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3808 // Methods for support type inquiry through isa, cast, and dyn_cast:
3809 static inline bool classof(const Instruction *I) {
3810 return I->getOpcode() == AddrSpaceCast;
3812 static inline bool classof(const Value *V) {
3813 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3817 } // End llvm namespace