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 Type *getSourceElementType() const {
849 return cast<SequentialType>(getPointerOperandType()->getScalarType())
853 /// \brief Returns the address space of this instruction's pointer type.
854 unsigned getAddressSpace() const {
855 // Note that this is always the same as the pointer operand's address space
856 // and that is cheaper to compute, so cheat here.
857 return getPointerAddressSpace();
860 /// getIndexedType - Returns the type of the element that would be loaded with
861 /// a load instruction with the specified parameters.
863 /// Null is returned if the indices are invalid for the specified
866 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
867 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
868 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
870 inline op_iterator idx_begin() { return op_begin()+1; }
871 inline const_op_iterator idx_begin() const { return op_begin()+1; }
872 inline op_iterator idx_end() { return op_end(); }
873 inline const_op_iterator idx_end() const { return op_end(); }
875 Value *getPointerOperand() {
876 return getOperand(0);
878 const Value *getPointerOperand() const {
879 return getOperand(0);
881 static unsigned getPointerOperandIndex() {
882 return 0U; // get index for modifying correct operand.
885 /// getPointerOperandType - Method to return the pointer operand as a
887 Type *getPointerOperandType() const {
888 return getPointerOperand()->getType();
891 /// \brief Returns the address space of the pointer operand.
892 unsigned getPointerAddressSpace() const {
893 return getPointerOperandType()->getPointerAddressSpace();
896 /// GetGEPReturnType - Returns the pointer type returned by the GEP
897 /// instruction, which may be a vector of pointers.
898 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
899 Type *PtrTy = PointerType::get(checkGEPType(
900 getIndexedType(Ptr->getType(), IdxList)),
901 Ptr->getType()->getPointerAddressSpace());
903 if (Ptr->getType()->isVectorTy()) {
904 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
905 return VectorType::get(PtrTy, NumElem);
912 unsigned getNumIndices() const { // Note: always non-negative
913 return getNumOperands() - 1;
916 bool hasIndices() const {
917 return getNumOperands() > 1;
920 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
921 /// zeros. If so, the result pointer and the first operand have the same
922 /// value, just potentially different types.
923 bool hasAllZeroIndices() const;
925 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
926 /// constant integers. If so, the result pointer and the first operand have
927 /// a constant offset between them.
928 bool hasAllConstantIndices() const;
930 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
931 /// See LangRef.html for the meaning of inbounds on a getelementptr.
932 void setIsInBounds(bool b = true);
934 /// isInBounds - Determine whether the GEP has the inbounds flag.
935 bool isInBounds() const;
937 /// \brief Accumulate the constant address offset of this GEP if possible.
939 /// This routine accepts an APInt into which it will accumulate the constant
940 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
941 /// all-constant, it returns false and the value of the offset APInt is
942 /// undefined (it is *not* preserved!). The APInt passed into this routine
943 /// must be at least as wide as the IntPtr type for the address space of
944 /// the base GEP pointer.
945 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
947 // Methods for support type inquiry through isa, cast, and dyn_cast:
948 static inline bool classof(const Instruction *I) {
949 return (I->getOpcode() == Instruction::GetElementPtr);
951 static inline bool classof(const Value *V) {
952 return isa<Instruction>(V) && classof(cast<Instruction>(V));
957 struct OperandTraits<GetElementPtrInst> :
958 public VariadicOperandTraits<GetElementPtrInst, 1> {
961 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
962 ArrayRef<Value *> IdxList,
964 const Twine &NameStr,
965 Instruction *InsertBefore)
966 : Instruction(getGEPReturnType(Ptr, IdxList),
968 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
969 Values, InsertBefore) {
970 init(Ptr, IdxList, NameStr);
972 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
973 ArrayRef<Value *> IdxList,
975 const Twine &NameStr,
976 BasicBlock *InsertAtEnd)
977 : Instruction(getGEPReturnType(Ptr, IdxList),
979 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
980 Values, InsertAtEnd) {
981 init(Ptr, IdxList, NameStr);
985 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
988 //===----------------------------------------------------------------------===//
990 //===----------------------------------------------------------------------===//
992 /// This instruction compares its operands according to the predicate given
993 /// to the constructor. It only operates on integers or pointers. The operands
994 /// must be identical types.
995 /// \brief Represent an integer comparison operator.
996 class ICmpInst: public CmpInst {
998 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
999 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1000 "Invalid ICmp predicate value");
1001 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1002 "Both operands to ICmp instruction are not of the same type!");
1003 // Check that the operands are the right type
1004 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1005 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1006 "Invalid operand types for ICmp instruction");
1010 /// \brief Clone an identical ICmpInst
1011 ICmpInst *clone_impl() const override;
1013 /// \brief Constructor with insert-before-instruction semantics.
1015 Instruction *InsertBefore, ///< Where to insert
1016 Predicate pred, ///< The predicate to use for the comparison
1017 Value *LHS, ///< The left-hand-side of the expression
1018 Value *RHS, ///< The right-hand-side of the expression
1019 const Twine &NameStr = "" ///< Name of the instruction
1020 ) : CmpInst(makeCmpResultType(LHS->getType()),
1021 Instruction::ICmp, pred, LHS, RHS, NameStr,
1028 /// \brief Constructor with insert-at-end semantics.
1030 BasicBlock &InsertAtEnd, ///< Block to insert into.
1031 Predicate pred, ///< The predicate to use for the comparison
1032 Value *LHS, ///< The left-hand-side of the expression
1033 Value *RHS, ///< The right-hand-side of the expression
1034 const Twine &NameStr = "" ///< Name of the instruction
1035 ) : CmpInst(makeCmpResultType(LHS->getType()),
1036 Instruction::ICmp, pred, LHS, RHS, NameStr,
1043 /// \brief Constructor with no-insertion semantics
1045 Predicate pred, ///< The predicate to use for the comparison
1046 Value *LHS, ///< The left-hand-side of the expression
1047 Value *RHS, ///< The right-hand-side of the expression
1048 const Twine &NameStr = "" ///< Name of the instruction
1049 ) : CmpInst(makeCmpResultType(LHS->getType()),
1050 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1056 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1057 /// @returns the predicate that would be the result if the operand were
1058 /// regarded as signed.
1059 /// \brief Return the signed version of the predicate
1060 Predicate getSignedPredicate() const {
1061 return getSignedPredicate(getPredicate());
1064 /// This is a static version that you can use without an instruction.
1065 /// \brief Return the signed version of the predicate.
1066 static Predicate getSignedPredicate(Predicate pred);
1068 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1069 /// @returns the predicate that would be the result if the operand were
1070 /// regarded as unsigned.
1071 /// \brief Return the unsigned version of the predicate
1072 Predicate getUnsignedPredicate() const {
1073 return getUnsignedPredicate(getPredicate());
1076 /// This is a static version that you can use without an instruction.
1077 /// \brief Return the unsigned version of the predicate.
1078 static Predicate getUnsignedPredicate(Predicate pred);
1080 /// isEquality - Return true if this predicate is either EQ or NE. This also
1081 /// tests for commutativity.
1082 static bool isEquality(Predicate P) {
1083 return P == ICMP_EQ || P == ICMP_NE;
1086 /// isEquality - Return true if this predicate is either EQ or NE. This also
1087 /// tests for commutativity.
1088 bool isEquality() const {
1089 return isEquality(getPredicate());
1092 /// @returns true if the predicate of this ICmpInst is commutative
1093 /// \brief Determine if this relation is commutative.
1094 bool isCommutative() const { return isEquality(); }
1096 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1098 bool isRelational() const {
1099 return !isEquality();
1102 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1104 static bool isRelational(Predicate P) {
1105 return !isEquality(P);
1108 /// Initialize a set of values that all satisfy the predicate with C.
1109 /// \brief Make a ConstantRange for a relation with a constant value.
1110 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1112 /// Exchange the two operands to this instruction in such a way that it does
1113 /// not modify the semantics of the instruction. The predicate value may be
1114 /// changed to retain the same result if the predicate is order dependent
1116 /// \brief Swap operands and adjust predicate.
1117 void swapOperands() {
1118 setPredicate(getSwappedPredicate());
1119 Op<0>().swap(Op<1>());
1122 // Methods for support type inquiry through isa, cast, and dyn_cast:
1123 static inline bool classof(const Instruction *I) {
1124 return I->getOpcode() == Instruction::ICmp;
1126 static inline bool classof(const Value *V) {
1127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1132 //===----------------------------------------------------------------------===//
1134 //===----------------------------------------------------------------------===//
1136 /// This instruction compares its operands according to the predicate given
1137 /// to the constructor. It only operates on floating point values or packed
1138 /// vectors of floating point values. The operands must be identical types.
1139 /// \brief Represents a floating point comparison operator.
1140 class FCmpInst: public CmpInst {
1142 /// \brief Clone an identical FCmpInst
1143 FCmpInst *clone_impl() const override;
1145 /// \brief Constructor with insert-before-instruction semantics.
1147 Instruction *InsertBefore, ///< Where to insert
1148 Predicate pred, ///< The predicate to use for the comparison
1149 Value *LHS, ///< The left-hand-side of the expression
1150 Value *RHS, ///< The right-hand-side of the expression
1151 const Twine &NameStr = "" ///< Name of the instruction
1152 ) : CmpInst(makeCmpResultType(LHS->getType()),
1153 Instruction::FCmp, pred, LHS, RHS, NameStr,
1155 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1156 "Invalid FCmp predicate value");
1157 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1158 "Both operands to FCmp instruction are not of the same type!");
1159 // Check that the operands are the right type
1160 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1161 "Invalid operand types for FCmp instruction");
1164 /// \brief Constructor with insert-at-end semantics.
1166 BasicBlock &InsertAtEnd, ///< Block to insert into.
1167 Predicate pred, ///< The predicate to use for the comparison
1168 Value *LHS, ///< The left-hand-side of the expression
1169 Value *RHS, ///< The right-hand-side of the expression
1170 const Twine &NameStr = "" ///< Name of the instruction
1171 ) : CmpInst(makeCmpResultType(LHS->getType()),
1172 Instruction::FCmp, pred, LHS, RHS, NameStr,
1174 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1175 "Invalid FCmp predicate value");
1176 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1177 "Both operands to FCmp instruction are not of the same type!");
1178 // Check that the operands are the right type
1179 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1180 "Invalid operand types for FCmp instruction");
1183 /// \brief Constructor with no-insertion semantics
1185 Predicate pred, ///< The predicate to use for the comparison
1186 Value *LHS, ///< The left-hand-side of the expression
1187 Value *RHS, ///< The right-hand-side of the expression
1188 const Twine &NameStr = "" ///< Name of the instruction
1189 ) : CmpInst(makeCmpResultType(LHS->getType()),
1190 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1191 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1192 "Invalid FCmp predicate value");
1193 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1194 "Both operands to FCmp instruction are not of the same type!");
1195 // Check that the operands are the right type
1196 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1197 "Invalid operand types for FCmp instruction");
1200 /// @returns true if the predicate of this instruction is EQ or NE.
1201 /// \brief Determine if this is an equality predicate.
1202 static bool isEquality(Predicate Pred) {
1203 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1207 /// @returns true if the predicate of this instruction is EQ or NE.
1208 /// \brief Determine if this is an equality predicate.
1209 bool isEquality() const { return isEquality(getPredicate()); }
1211 /// @returns true if the predicate of this instruction is commutative.
1212 /// \brief Determine if this is a commutative predicate.
1213 bool isCommutative() const {
1214 return isEquality() ||
1215 getPredicate() == FCMP_FALSE ||
1216 getPredicate() == FCMP_TRUE ||
1217 getPredicate() == FCMP_ORD ||
1218 getPredicate() == FCMP_UNO;
1221 /// @returns true if the predicate is relational (not EQ or NE).
1222 /// \brief Determine if this a relational predicate.
1223 bool isRelational() const { return !isEquality(); }
1225 /// Exchange the two operands to this instruction in such a way that it does
1226 /// not modify the semantics of the instruction. The predicate value may be
1227 /// changed to retain the same result if the predicate is order dependent
1229 /// \brief Swap operands and adjust predicate.
1230 void swapOperands() {
1231 setPredicate(getSwappedPredicate());
1232 Op<0>().swap(Op<1>());
1235 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1236 static inline bool classof(const Instruction *I) {
1237 return I->getOpcode() == Instruction::FCmp;
1239 static inline bool classof(const Value *V) {
1240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1244 //===----------------------------------------------------------------------===//
1245 /// CallInst - This class represents a function call, abstracting a target
1246 /// machine's calling convention. This class uses low bit of the SubClassData
1247 /// field to indicate whether or not this is a tail call. The rest of the bits
1248 /// hold the calling convention of the call.
1250 class CallInst : public Instruction {
1251 AttributeSet AttributeList; ///< parameter attributes for call
1252 CallInst(const CallInst &CI);
1253 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1254 void init(Value *Func, const Twine &NameStr);
1256 /// Construct a CallInst given a range of arguments.
1257 /// \brief Construct a CallInst from a range of arguments
1258 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1259 const Twine &NameStr, Instruction *InsertBefore);
1261 /// Construct a CallInst given a range of arguments.
1262 /// \brief Construct a CallInst from a range of arguments
1263 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1264 const Twine &NameStr, BasicBlock *InsertAtEnd);
1266 explicit CallInst(Value *F, const Twine &NameStr,
1267 Instruction *InsertBefore);
1268 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1270 CallInst *clone_impl() const override;
1272 static CallInst *Create(Value *Func,
1273 ArrayRef<Value *> Args,
1274 const Twine &NameStr = "",
1275 Instruction *InsertBefore = nullptr) {
1276 return new(unsigned(Args.size() + 1))
1277 CallInst(Func, Args, NameStr, InsertBefore);
1279 static CallInst *Create(Value *Func,
1280 ArrayRef<Value *> Args,
1281 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1282 return new(unsigned(Args.size() + 1))
1283 CallInst(Func, Args, NameStr, InsertAtEnd);
1285 static CallInst *Create(Value *F, const Twine &NameStr = "",
1286 Instruction *InsertBefore = nullptr) {
1287 return new(1) CallInst(F, NameStr, InsertBefore);
1289 static CallInst *Create(Value *F, const Twine &NameStr,
1290 BasicBlock *InsertAtEnd) {
1291 return new(1) CallInst(F, NameStr, InsertAtEnd);
1293 /// CreateMalloc - Generate the IR for a call to malloc:
1294 /// 1. Compute the malloc call's argument as the specified type's size,
1295 /// possibly multiplied by the array size if the array size is not
1297 /// 2. Call malloc with that argument.
1298 /// 3. Bitcast the result of the malloc call to the specified type.
1299 static Instruction *CreateMalloc(Instruction *InsertBefore,
1300 Type *IntPtrTy, Type *AllocTy,
1301 Value *AllocSize, Value *ArraySize = nullptr,
1302 Function* MallocF = nullptr,
1303 const Twine &Name = "");
1304 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1305 Type *IntPtrTy, Type *AllocTy,
1306 Value *AllocSize, Value *ArraySize = nullptr,
1307 Function* MallocF = nullptr,
1308 const Twine &Name = "");
1309 /// CreateFree - Generate the IR for a call to the builtin free function.
1310 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1311 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1315 // Note that 'musttail' implies 'tail'.
1316 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1317 TailCallKind getTailCallKind() const {
1318 return TailCallKind(getSubclassDataFromInstruction() & 3);
1320 bool isTailCall() const {
1321 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1323 bool isMustTailCall() const {
1324 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1326 void setTailCall(bool isTC = true) {
1327 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1328 unsigned(isTC ? TCK_Tail : TCK_None));
1330 void setTailCallKind(TailCallKind TCK) {
1331 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1335 /// Provide fast operand accessors
1336 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1338 /// getNumArgOperands - Return the number of call arguments.
1340 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1342 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1344 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1345 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1347 /// arg_operands - iteration adapter for range-for loops.
1348 iterator_range<op_iterator> arg_operands() {
1349 // The last operand in the op list is the callee - it's not one of the args
1350 // so we don't want to iterate over it.
1351 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1354 /// arg_operands - iteration adapter for range-for loops.
1355 iterator_range<const_op_iterator> arg_operands() const {
1356 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1359 /// \brief Wrappers for getting the \c Use of a call argument.
1360 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1361 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1363 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1365 CallingConv::ID getCallingConv() const {
1366 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1368 void setCallingConv(CallingConv::ID CC) {
1369 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1370 (static_cast<unsigned>(CC) << 2));
1373 /// getAttributes - Return the parameter attributes for this call.
1375 const AttributeSet &getAttributes() const { return AttributeList; }
1377 /// setAttributes - Set the parameter attributes for this call.
1379 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1381 /// addAttribute - adds the attribute to the list of attributes.
1382 void addAttribute(unsigned i, Attribute::AttrKind attr);
1384 /// removeAttribute - removes the attribute from the list of attributes.
1385 void removeAttribute(unsigned i, Attribute attr);
1387 /// \brief adds the dereferenceable attribute to the list of attributes.
1388 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1390 /// \brief Determine whether this call has the given attribute.
1391 bool hasFnAttr(Attribute::AttrKind A) const {
1392 assert(A != Attribute::NoBuiltin &&
1393 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1394 return hasFnAttrImpl(A);
1397 /// \brief Determine whether the call or the callee has the given attributes.
1398 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1400 /// \brief Extract the alignment for a call or parameter (0=unknown).
1401 unsigned getParamAlignment(unsigned i) const {
1402 return AttributeList.getParamAlignment(i);
1405 /// \brief Extract the number of dereferenceable bytes for a call or
1406 /// parameter (0=unknown).
1407 uint64_t getDereferenceableBytes(unsigned i) const {
1408 return AttributeList.getDereferenceableBytes(i);
1411 /// \brief Return true if the call should not be treated as a call to a
1413 bool isNoBuiltin() const {
1414 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1415 !hasFnAttrImpl(Attribute::Builtin);
1418 /// \brief Return true if the call should not be inlined.
1419 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1420 void setIsNoInline() {
1421 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1424 /// \brief Return true if the call can return twice
1425 bool canReturnTwice() const {
1426 return hasFnAttr(Attribute::ReturnsTwice);
1428 void setCanReturnTwice() {
1429 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1432 /// \brief Determine if the call does not access memory.
1433 bool doesNotAccessMemory() const {
1434 return hasFnAttr(Attribute::ReadNone);
1436 void setDoesNotAccessMemory() {
1437 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1440 /// \brief Determine if the call does not access or only reads memory.
1441 bool onlyReadsMemory() const {
1442 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1444 void setOnlyReadsMemory() {
1445 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1448 /// \brief Determine if the call cannot return.
1449 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1450 void setDoesNotReturn() {
1451 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1454 /// \brief Determine if the call cannot unwind.
1455 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1456 void setDoesNotThrow() {
1457 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1460 /// \brief Determine if the call cannot be duplicated.
1461 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1462 void setCannotDuplicate() {
1463 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1466 /// \brief Determine if the call returns a structure through first
1467 /// pointer argument.
1468 bool hasStructRetAttr() const {
1469 // Be friendly and also check the callee.
1470 return paramHasAttr(1, Attribute::StructRet);
1473 /// \brief Determine if any call argument is an aggregate passed by value.
1474 bool hasByValArgument() const {
1475 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1478 /// getCalledFunction - Return the function called, or null if this is an
1479 /// indirect function invocation.
1481 Function *getCalledFunction() const {
1482 return dyn_cast<Function>(Op<-1>());
1485 /// getCalledValue - Get a pointer to the function that is invoked by this
1487 const Value *getCalledValue() const { return Op<-1>(); }
1488 Value *getCalledValue() { return Op<-1>(); }
1490 /// setCalledFunction - Set the function called.
1491 void setCalledFunction(Value* Fn) {
1495 /// isInlineAsm - Check if this call is an inline asm statement.
1496 bool isInlineAsm() const {
1497 return isa<InlineAsm>(Op<-1>());
1500 // Methods for support type inquiry through isa, cast, and dyn_cast:
1501 static inline bool classof(const Instruction *I) {
1502 return I->getOpcode() == Instruction::Call;
1504 static inline bool classof(const Value *V) {
1505 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1509 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1511 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1512 // method so that subclasses cannot accidentally use it.
1513 void setInstructionSubclassData(unsigned short D) {
1514 Instruction::setInstructionSubclassData(D);
1519 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1522 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1523 const Twine &NameStr, BasicBlock *InsertAtEnd)
1524 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1525 ->getElementType())->getReturnType(),
1527 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1528 unsigned(Args.size() + 1), InsertAtEnd) {
1529 init(Func, Args, NameStr);
1532 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1533 const Twine &NameStr, Instruction *InsertBefore)
1534 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1535 ->getElementType())->getReturnType(),
1537 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1538 unsigned(Args.size() + 1), InsertBefore) {
1539 init(Func, Args, NameStr);
1543 // Note: if you get compile errors about private methods then
1544 // please update your code to use the high-level operand
1545 // interfaces. See line 943 above.
1546 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1548 //===----------------------------------------------------------------------===//
1550 //===----------------------------------------------------------------------===//
1552 /// SelectInst - This class represents the LLVM 'select' instruction.
1554 class SelectInst : public Instruction {
1555 void init(Value *C, Value *S1, Value *S2) {
1556 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1562 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1563 Instruction *InsertBefore)
1564 : Instruction(S1->getType(), Instruction::Select,
1565 &Op<0>(), 3, InsertBefore) {
1569 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1570 BasicBlock *InsertAtEnd)
1571 : Instruction(S1->getType(), Instruction::Select,
1572 &Op<0>(), 3, InsertAtEnd) {
1577 SelectInst *clone_impl() const override;
1579 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1580 const Twine &NameStr = "",
1581 Instruction *InsertBefore = nullptr) {
1582 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1584 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1585 const Twine &NameStr,
1586 BasicBlock *InsertAtEnd) {
1587 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1590 const Value *getCondition() const { return Op<0>(); }
1591 const Value *getTrueValue() const { return Op<1>(); }
1592 const Value *getFalseValue() const { return Op<2>(); }
1593 Value *getCondition() { return Op<0>(); }
1594 Value *getTrueValue() { return Op<1>(); }
1595 Value *getFalseValue() { return Op<2>(); }
1597 /// areInvalidOperands - Return a string if the specified operands are invalid
1598 /// for a select operation, otherwise return null.
1599 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1601 /// Transparently provide more efficient getOperand methods.
1602 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1604 OtherOps getOpcode() const {
1605 return static_cast<OtherOps>(Instruction::getOpcode());
1608 // Methods for support type inquiry through isa, cast, and dyn_cast:
1609 static inline bool classof(const Instruction *I) {
1610 return I->getOpcode() == Instruction::Select;
1612 static inline bool classof(const Value *V) {
1613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1618 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1621 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1623 //===----------------------------------------------------------------------===//
1625 //===----------------------------------------------------------------------===//
1627 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1628 /// an argument of the specified type given a va_list and increments that list
1630 class VAArgInst : public UnaryInstruction {
1632 VAArgInst *clone_impl() const override;
1635 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1636 Instruction *InsertBefore = nullptr)
1637 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1640 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1641 BasicBlock *InsertAtEnd)
1642 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1646 Value *getPointerOperand() { return getOperand(0); }
1647 const Value *getPointerOperand() const { return getOperand(0); }
1648 static unsigned getPointerOperandIndex() { return 0U; }
1650 // Methods for support type inquiry through isa, cast, and dyn_cast:
1651 static inline bool classof(const Instruction *I) {
1652 return I->getOpcode() == VAArg;
1654 static inline bool classof(const Value *V) {
1655 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1659 //===----------------------------------------------------------------------===//
1660 // ExtractElementInst Class
1661 //===----------------------------------------------------------------------===//
1663 /// ExtractElementInst - This instruction extracts a single (scalar)
1664 /// element from a VectorType value
1666 class ExtractElementInst : public Instruction {
1667 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1668 Instruction *InsertBefore = nullptr);
1669 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1670 BasicBlock *InsertAtEnd);
1672 ExtractElementInst *clone_impl() const override;
1675 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1676 const Twine &NameStr = "",
1677 Instruction *InsertBefore = nullptr) {
1678 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1680 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1681 const Twine &NameStr,
1682 BasicBlock *InsertAtEnd) {
1683 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1686 /// isValidOperands - Return true if an extractelement instruction can be
1687 /// formed with the specified operands.
1688 static bool isValidOperands(const Value *Vec, const Value *Idx);
1690 Value *getVectorOperand() { return Op<0>(); }
1691 Value *getIndexOperand() { return Op<1>(); }
1692 const Value *getVectorOperand() const { return Op<0>(); }
1693 const Value *getIndexOperand() const { return Op<1>(); }
1695 VectorType *getVectorOperandType() const {
1696 return cast<VectorType>(getVectorOperand()->getType());
1700 /// Transparently provide more efficient getOperand methods.
1701 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1703 // Methods for support type inquiry through isa, cast, and dyn_cast:
1704 static inline bool classof(const Instruction *I) {
1705 return I->getOpcode() == Instruction::ExtractElement;
1707 static inline bool classof(const Value *V) {
1708 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1713 struct OperandTraits<ExtractElementInst> :
1714 public FixedNumOperandTraits<ExtractElementInst, 2> {
1717 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1719 //===----------------------------------------------------------------------===//
1720 // InsertElementInst Class
1721 //===----------------------------------------------------------------------===//
1723 /// InsertElementInst - This instruction inserts a single (scalar)
1724 /// element into a VectorType value
1726 class InsertElementInst : public Instruction {
1727 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1728 const Twine &NameStr = "",
1729 Instruction *InsertBefore = nullptr);
1730 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1731 const Twine &NameStr, BasicBlock *InsertAtEnd);
1733 InsertElementInst *clone_impl() const override;
1736 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1737 const Twine &NameStr = "",
1738 Instruction *InsertBefore = nullptr) {
1739 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1741 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1742 const Twine &NameStr,
1743 BasicBlock *InsertAtEnd) {
1744 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1747 /// isValidOperands - Return true if an insertelement instruction can be
1748 /// formed with the specified operands.
1749 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1752 /// getType - Overload to return most specific vector type.
1754 VectorType *getType() const {
1755 return cast<VectorType>(Instruction::getType());
1758 /// Transparently provide more efficient getOperand methods.
1759 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1761 // Methods for support type inquiry through isa, cast, and dyn_cast:
1762 static inline bool classof(const Instruction *I) {
1763 return I->getOpcode() == Instruction::InsertElement;
1765 static inline bool classof(const Value *V) {
1766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1771 struct OperandTraits<InsertElementInst> :
1772 public FixedNumOperandTraits<InsertElementInst, 3> {
1775 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1777 //===----------------------------------------------------------------------===//
1778 // ShuffleVectorInst Class
1779 //===----------------------------------------------------------------------===//
1781 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1784 class ShuffleVectorInst : public Instruction {
1786 ShuffleVectorInst *clone_impl() const override;
1789 // allocate space for exactly three operands
1790 void *operator new(size_t s) {
1791 return User::operator new(s, 3);
1793 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1794 const Twine &NameStr = "",
1795 Instruction *InsertBefor = nullptr);
1796 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1797 const Twine &NameStr, BasicBlock *InsertAtEnd);
1799 /// isValidOperands - Return true if a shufflevector instruction can be
1800 /// formed with the specified operands.
1801 static bool isValidOperands(const Value *V1, const Value *V2,
1804 /// getType - Overload to return most specific vector type.
1806 VectorType *getType() const {
1807 return cast<VectorType>(Instruction::getType());
1810 /// Transparently provide more efficient getOperand methods.
1811 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1813 Constant *getMask() const {
1814 return cast<Constant>(getOperand(2));
1817 /// getMaskValue - Return the index from the shuffle mask for the specified
1818 /// output result. This is either -1 if the element is undef or a number less
1819 /// than 2*numelements.
1820 static int getMaskValue(Constant *Mask, unsigned i);
1822 int getMaskValue(unsigned i) const {
1823 return getMaskValue(getMask(), i);
1826 /// getShuffleMask - Return the full mask for this instruction, where each
1827 /// element is the element number and undef's are returned as -1.
1828 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1830 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1831 return getShuffleMask(getMask(), Result);
1834 SmallVector<int, 16> getShuffleMask() const {
1835 SmallVector<int, 16> Mask;
1836 getShuffleMask(Mask);
1841 // Methods for support type inquiry through isa, cast, and dyn_cast:
1842 static inline bool classof(const Instruction *I) {
1843 return I->getOpcode() == Instruction::ShuffleVector;
1845 static inline bool classof(const Value *V) {
1846 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1851 struct OperandTraits<ShuffleVectorInst> :
1852 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1855 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1857 //===----------------------------------------------------------------------===//
1858 // ExtractValueInst Class
1859 //===----------------------------------------------------------------------===//
1861 /// ExtractValueInst - This instruction extracts a struct member or array
1862 /// element value from an aggregate value.
1864 class ExtractValueInst : public UnaryInstruction {
1865 SmallVector<unsigned, 4> Indices;
1867 ExtractValueInst(const ExtractValueInst &EVI);
1868 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1870 /// Constructors - Create a extractvalue instruction with a base aggregate
1871 /// value and a list of indices. The first ctor can optionally insert before
1872 /// an existing instruction, the second appends the new instruction to the
1873 /// specified BasicBlock.
1874 inline ExtractValueInst(Value *Agg,
1875 ArrayRef<unsigned> Idxs,
1876 const Twine &NameStr,
1877 Instruction *InsertBefore);
1878 inline ExtractValueInst(Value *Agg,
1879 ArrayRef<unsigned> Idxs,
1880 const Twine &NameStr, BasicBlock *InsertAtEnd);
1882 // allocate space for exactly one operand
1883 void *operator new(size_t s) {
1884 return User::operator new(s, 1);
1887 ExtractValueInst *clone_impl() const override;
1890 static ExtractValueInst *Create(Value *Agg,
1891 ArrayRef<unsigned> Idxs,
1892 const Twine &NameStr = "",
1893 Instruction *InsertBefore = nullptr) {
1895 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1897 static ExtractValueInst *Create(Value *Agg,
1898 ArrayRef<unsigned> Idxs,
1899 const Twine &NameStr,
1900 BasicBlock *InsertAtEnd) {
1901 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1904 /// getIndexedType - Returns the type of the element that would be extracted
1905 /// with an extractvalue instruction with the specified parameters.
1907 /// Null is returned if the indices are invalid for the specified type.
1908 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1910 typedef const unsigned* idx_iterator;
1911 inline idx_iterator idx_begin() const { return Indices.begin(); }
1912 inline idx_iterator idx_end() const { return Indices.end(); }
1913 inline iterator_range<idx_iterator> indices() const {
1914 return iterator_range<idx_iterator>(idx_begin(), idx_end());
1917 Value *getAggregateOperand() {
1918 return getOperand(0);
1920 const Value *getAggregateOperand() const {
1921 return getOperand(0);
1923 static unsigned getAggregateOperandIndex() {
1924 return 0U; // get index for modifying correct operand
1927 ArrayRef<unsigned> getIndices() const {
1931 unsigned getNumIndices() const {
1932 return (unsigned)Indices.size();
1935 bool hasIndices() const {
1939 // Methods for support type inquiry through isa, cast, and dyn_cast:
1940 static inline bool classof(const Instruction *I) {
1941 return I->getOpcode() == Instruction::ExtractValue;
1943 static inline bool classof(const Value *V) {
1944 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1948 ExtractValueInst::ExtractValueInst(Value *Agg,
1949 ArrayRef<unsigned> Idxs,
1950 const Twine &NameStr,
1951 Instruction *InsertBefore)
1952 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1953 ExtractValue, Agg, InsertBefore) {
1954 init(Idxs, NameStr);
1956 ExtractValueInst::ExtractValueInst(Value *Agg,
1957 ArrayRef<unsigned> Idxs,
1958 const Twine &NameStr,
1959 BasicBlock *InsertAtEnd)
1960 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1961 ExtractValue, Agg, InsertAtEnd) {
1962 init(Idxs, NameStr);
1966 //===----------------------------------------------------------------------===//
1967 // InsertValueInst Class
1968 //===----------------------------------------------------------------------===//
1970 /// InsertValueInst - This instruction inserts a struct field of array element
1971 /// value into an aggregate value.
1973 class InsertValueInst : public Instruction {
1974 SmallVector<unsigned, 4> Indices;
1976 void *operator new(size_t, unsigned) = delete;
1977 InsertValueInst(const InsertValueInst &IVI);
1978 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1979 const Twine &NameStr);
1981 /// Constructors - Create a insertvalue instruction with a base aggregate
1982 /// value, a value to insert, and a list of indices. The first ctor can
1983 /// optionally insert before an existing instruction, the second appends
1984 /// the new instruction to the specified BasicBlock.
1985 inline InsertValueInst(Value *Agg, Value *Val,
1986 ArrayRef<unsigned> Idxs,
1987 const Twine &NameStr,
1988 Instruction *InsertBefore);
1989 inline InsertValueInst(Value *Agg, Value *Val,
1990 ArrayRef<unsigned> Idxs,
1991 const Twine &NameStr, BasicBlock *InsertAtEnd);
1993 /// Constructors - These two constructors are convenience methods because one
1994 /// and two index insertvalue instructions are so common.
1995 InsertValueInst(Value *Agg, Value *Val,
1996 unsigned Idx, const Twine &NameStr = "",
1997 Instruction *InsertBefore = nullptr);
1998 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1999 const Twine &NameStr, BasicBlock *InsertAtEnd);
2001 InsertValueInst *clone_impl() const override;
2003 // allocate space for exactly two operands
2004 void *operator new(size_t s) {
2005 return User::operator new(s, 2);
2008 static InsertValueInst *Create(Value *Agg, Value *Val,
2009 ArrayRef<unsigned> Idxs,
2010 const Twine &NameStr = "",
2011 Instruction *InsertBefore = nullptr) {
2012 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2014 static InsertValueInst *Create(Value *Agg, Value *Val,
2015 ArrayRef<unsigned> Idxs,
2016 const Twine &NameStr,
2017 BasicBlock *InsertAtEnd) {
2018 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2021 /// Transparently provide more efficient getOperand methods.
2022 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2024 typedef const unsigned* idx_iterator;
2025 inline idx_iterator idx_begin() const { return Indices.begin(); }
2026 inline idx_iterator idx_end() const { return Indices.end(); }
2027 inline iterator_range<idx_iterator> indices() const {
2028 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2031 Value *getAggregateOperand() {
2032 return getOperand(0);
2034 const Value *getAggregateOperand() const {
2035 return getOperand(0);
2037 static unsigned getAggregateOperandIndex() {
2038 return 0U; // get index for modifying correct operand
2041 Value *getInsertedValueOperand() {
2042 return getOperand(1);
2044 const Value *getInsertedValueOperand() const {
2045 return getOperand(1);
2047 static unsigned getInsertedValueOperandIndex() {
2048 return 1U; // get index for modifying correct operand
2051 ArrayRef<unsigned> getIndices() const {
2055 unsigned getNumIndices() const {
2056 return (unsigned)Indices.size();
2059 bool hasIndices() const {
2063 // Methods for support type inquiry through isa, cast, and dyn_cast:
2064 static inline bool classof(const Instruction *I) {
2065 return I->getOpcode() == Instruction::InsertValue;
2067 static inline bool classof(const Value *V) {
2068 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2073 struct OperandTraits<InsertValueInst> :
2074 public FixedNumOperandTraits<InsertValueInst, 2> {
2077 InsertValueInst::InsertValueInst(Value *Agg,
2079 ArrayRef<unsigned> Idxs,
2080 const Twine &NameStr,
2081 Instruction *InsertBefore)
2082 : Instruction(Agg->getType(), InsertValue,
2083 OperandTraits<InsertValueInst>::op_begin(this),
2085 init(Agg, Val, Idxs, NameStr);
2087 InsertValueInst::InsertValueInst(Value *Agg,
2089 ArrayRef<unsigned> Idxs,
2090 const Twine &NameStr,
2091 BasicBlock *InsertAtEnd)
2092 : Instruction(Agg->getType(), InsertValue,
2093 OperandTraits<InsertValueInst>::op_begin(this),
2095 init(Agg, Val, Idxs, NameStr);
2098 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2100 //===----------------------------------------------------------------------===//
2102 //===----------------------------------------------------------------------===//
2104 // PHINode - The PHINode class is used to represent the magical mystical PHI
2105 // node, that can not exist in nature, but can be synthesized in a computer
2106 // scientist's overactive imagination.
2108 class PHINode : public Instruction {
2109 void *operator new(size_t, unsigned) = delete;
2110 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2111 /// the number actually in use.
2112 unsigned ReservedSpace;
2113 PHINode(const PHINode &PN);
2114 // allocate space for exactly zero operands
2115 void *operator new(size_t s) {
2116 return User::operator new(s, 0);
2118 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2119 const Twine &NameStr = "",
2120 Instruction *InsertBefore = nullptr)
2121 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2122 ReservedSpace(NumReservedValues) {
2124 OperandList = allocHungoffUses(ReservedSpace);
2127 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2128 BasicBlock *InsertAtEnd)
2129 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2130 ReservedSpace(NumReservedValues) {
2132 OperandList = allocHungoffUses(ReservedSpace);
2135 // allocHungoffUses - this is more complicated than the generic
2136 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2137 // values and pointers to the incoming blocks, all in one allocation.
2138 Use *allocHungoffUses(unsigned) const;
2140 PHINode *clone_impl() const override;
2142 /// Constructors - NumReservedValues is a hint for the number of incoming
2143 /// edges that this phi node will have (use 0 if you really have no idea).
2144 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2145 const Twine &NameStr = "",
2146 Instruction *InsertBefore = nullptr) {
2147 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2149 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2150 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2151 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2155 /// Provide fast operand accessors
2156 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2158 // Block iterator interface. This provides access to the list of incoming
2159 // basic blocks, which parallels the list of incoming values.
2161 typedef BasicBlock **block_iterator;
2162 typedef BasicBlock * const *const_block_iterator;
2164 block_iterator block_begin() {
2166 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2167 return reinterpret_cast<block_iterator>(ref + 1);
2170 const_block_iterator block_begin() const {
2171 const Use::UserRef *ref =
2172 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2173 return reinterpret_cast<const_block_iterator>(ref + 1);
2176 block_iterator block_end() {
2177 return block_begin() + getNumOperands();
2180 const_block_iterator block_end() const {
2181 return block_begin() + getNumOperands();
2184 op_range incoming_values() { return operands(); }
2186 /// getNumIncomingValues - Return the number of incoming edges
2188 unsigned getNumIncomingValues() const { return getNumOperands(); }
2190 /// getIncomingValue - Return incoming value number x
2192 Value *getIncomingValue(unsigned i) const {
2193 return getOperand(i);
2195 void setIncomingValue(unsigned i, Value *V) {
2198 static unsigned getOperandNumForIncomingValue(unsigned i) {
2201 static unsigned getIncomingValueNumForOperand(unsigned i) {
2205 /// getIncomingBlock - Return incoming basic block number @p i.
2207 BasicBlock *getIncomingBlock(unsigned i) const {
2208 return block_begin()[i];
2211 /// getIncomingBlock - Return incoming basic block corresponding
2212 /// to an operand of the PHI.
2214 BasicBlock *getIncomingBlock(const Use &U) const {
2215 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2216 return getIncomingBlock(unsigned(&U - op_begin()));
2219 /// getIncomingBlock - Return incoming basic block corresponding
2220 /// to value use iterator.
2222 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2223 return getIncomingBlock(I.getUse());
2226 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2227 block_begin()[i] = BB;
2230 /// addIncoming - Add an incoming value to the end of the PHI list
2232 void addIncoming(Value *V, BasicBlock *BB) {
2233 assert(V && "PHI node got a null value!");
2234 assert(BB && "PHI node got a null basic block!");
2235 assert(getType() == V->getType() &&
2236 "All operands to PHI node must be the same type as the PHI node!");
2237 if (NumOperands == ReservedSpace)
2238 growOperands(); // Get more space!
2239 // Initialize some new operands.
2241 setIncomingValue(NumOperands - 1, V);
2242 setIncomingBlock(NumOperands - 1, BB);
2245 /// removeIncomingValue - Remove an incoming value. This is useful if a
2246 /// predecessor basic block is deleted. The value removed is returned.
2248 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2249 /// is true), the PHI node is destroyed and any uses of it are replaced with
2250 /// dummy values. The only time there should be zero incoming values to a PHI
2251 /// node is when the block is dead, so this strategy is sound.
2253 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2255 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2256 int Idx = getBasicBlockIndex(BB);
2257 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2258 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2261 /// getBasicBlockIndex - Return the first index of the specified basic
2262 /// block in the value list for this PHI. Returns -1 if no instance.
2264 int getBasicBlockIndex(const BasicBlock *BB) const {
2265 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2266 if (block_begin()[i] == BB)
2271 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2272 int Idx = getBasicBlockIndex(BB);
2273 assert(Idx >= 0 && "Invalid basic block argument!");
2274 return getIncomingValue(Idx);
2277 /// hasConstantValue - If the specified PHI node always merges together the
2278 /// same value, return the value, otherwise return null.
2279 Value *hasConstantValue() const;
2281 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2282 static inline bool classof(const Instruction *I) {
2283 return I->getOpcode() == Instruction::PHI;
2285 static inline bool classof(const Value *V) {
2286 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2289 void growOperands();
2293 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2296 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2298 //===----------------------------------------------------------------------===//
2299 // LandingPadInst Class
2300 //===----------------------------------------------------------------------===//
2302 //===---------------------------------------------------------------------------
2303 /// LandingPadInst - The landingpad instruction holds all of the information
2304 /// necessary to generate correct exception handling. The landingpad instruction
2305 /// cannot be moved from the top of a landing pad block, which itself is
2306 /// accessible only from the 'unwind' edge of an invoke. This uses the
2307 /// SubclassData field in Value to store whether or not the landingpad is a
2310 class LandingPadInst : public Instruction {
2311 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2312 /// the number actually in use.
2313 unsigned ReservedSpace;
2314 LandingPadInst(const LandingPadInst &LP);
2316 enum ClauseType { Catch, Filter };
2318 void *operator new(size_t, unsigned) = delete;
2319 // Allocate space for exactly zero operands.
2320 void *operator new(size_t s) {
2321 return User::operator new(s, 0);
2323 void growOperands(unsigned Size);
2324 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2326 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2327 unsigned NumReservedValues, const Twine &NameStr,
2328 Instruction *InsertBefore);
2329 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2330 unsigned NumReservedValues, const Twine &NameStr,
2331 BasicBlock *InsertAtEnd);
2333 LandingPadInst *clone_impl() const override;
2335 /// Constructors - NumReservedClauses is a hint for the number of incoming
2336 /// clauses that this landingpad will have (use 0 if you really have no idea).
2337 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2338 unsigned NumReservedClauses,
2339 const Twine &NameStr = "",
2340 Instruction *InsertBefore = nullptr);
2341 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2342 unsigned NumReservedClauses,
2343 const Twine &NameStr, BasicBlock *InsertAtEnd);
2346 /// Provide fast operand accessors
2347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2349 /// getPersonalityFn - Get the personality function associated with this
2351 Value *getPersonalityFn() const { return getOperand(0); }
2353 /// isCleanup - Return 'true' if this landingpad instruction is a
2354 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2355 /// doesn't catch the exception.
2356 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2358 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2359 void setCleanup(bool V) {
2360 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2364 /// Add a catch or filter clause to the landing pad.
2365 void addClause(Constant *ClauseVal);
2367 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2368 /// determine what type of clause this is.
2369 Constant *getClause(unsigned Idx) const {
2370 return cast<Constant>(OperandList[Idx + 1]);
2373 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2374 bool isCatch(unsigned Idx) const {
2375 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2378 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2379 bool isFilter(unsigned Idx) const {
2380 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2383 /// getNumClauses - Get the number of clauses for this landing pad.
2384 unsigned getNumClauses() const { return getNumOperands() - 1; }
2386 /// reserveClauses - Grow the size of the operand list to accommodate the new
2387 /// number of clauses.
2388 void reserveClauses(unsigned Size) { growOperands(Size); }
2390 // Methods for support type inquiry through isa, cast, and dyn_cast:
2391 static inline bool classof(const Instruction *I) {
2392 return I->getOpcode() == Instruction::LandingPad;
2394 static inline bool classof(const Value *V) {
2395 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2400 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2403 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2405 //===----------------------------------------------------------------------===//
2407 //===----------------------------------------------------------------------===//
2409 //===---------------------------------------------------------------------------
2410 /// ReturnInst - Return a value (possibly void), from a function. Execution
2411 /// does not continue in this function any longer.
2413 class ReturnInst : public TerminatorInst {
2414 ReturnInst(const ReturnInst &RI);
2417 // ReturnInst constructors:
2418 // ReturnInst() - 'ret void' instruction
2419 // ReturnInst( null) - 'ret void' instruction
2420 // ReturnInst(Value* X) - 'ret X' instruction
2421 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2422 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2423 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2424 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2426 // NOTE: If the Value* passed is of type void then the constructor behaves as
2427 // if it was passed NULL.
2428 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2429 Instruction *InsertBefore = nullptr);
2430 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2431 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2433 ReturnInst *clone_impl() const override;
2435 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2436 Instruction *InsertBefore = nullptr) {
2437 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2439 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2440 BasicBlock *InsertAtEnd) {
2441 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2443 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2444 return new(0) ReturnInst(C, InsertAtEnd);
2446 virtual ~ReturnInst();
2448 /// Provide fast operand accessors
2449 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2451 /// Convenience accessor. Returns null if there is no return value.
2452 Value *getReturnValue() const {
2453 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2456 unsigned getNumSuccessors() const { return 0; }
2458 // Methods for support type inquiry through isa, cast, and dyn_cast:
2459 static inline bool classof(const Instruction *I) {
2460 return (I->getOpcode() == Instruction::Ret);
2462 static inline bool classof(const Value *V) {
2463 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2466 BasicBlock *getSuccessorV(unsigned idx) const override;
2467 unsigned getNumSuccessorsV() const override;
2468 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2472 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2475 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2477 //===----------------------------------------------------------------------===//
2479 //===----------------------------------------------------------------------===//
2481 //===---------------------------------------------------------------------------
2482 /// BranchInst - Conditional or Unconditional Branch instruction.
2484 class BranchInst : public TerminatorInst {
2485 /// Ops list - Branches are strange. The operands are ordered:
2486 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2487 /// they don't have to check for cond/uncond branchness. These are mostly
2488 /// accessed relative from op_end().
2489 BranchInst(const BranchInst &BI);
2491 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2492 // BranchInst(BB *B) - 'br B'
2493 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2494 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2495 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2496 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2497 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2498 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2499 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2500 Instruction *InsertBefore = nullptr);
2501 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2502 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2503 BasicBlock *InsertAtEnd);
2505 BranchInst *clone_impl() const override;
2507 static BranchInst *Create(BasicBlock *IfTrue,
2508 Instruction *InsertBefore = nullptr) {
2509 return new(1) BranchInst(IfTrue, InsertBefore);
2511 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2512 Value *Cond, Instruction *InsertBefore = nullptr) {
2513 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2515 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2516 return new(1) BranchInst(IfTrue, InsertAtEnd);
2518 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2519 Value *Cond, BasicBlock *InsertAtEnd) {
2520 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2523 /// Transparently provide more efficient getOperand methods.
2524 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2526 bool isUnconditional() const { return getNumOperands() == 1; }
2527 bool isConditional() const { return getNumOperands() == 3; }
2529 Value *getCondition() const {
2530 assert(isConditional() && "Cannot get condition of an uncond branch!");
2534 void setCondition(Value *V) {
2535 assert(isConditional() && "Cannot set condition of unconditional branch!");
2539 unsigned getNumSuccessors() const { return 1+isConditional(); }
2541 BasicBlock *getSuccessor(unsigned i) const {
2542 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2543 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2546 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2547 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2548 *(&Op<-1>() - idx) = (Value*)NewSucc;
2551 /// \brief Swap the successors of this branch instruction.
2553 /// Swaps the successors of the branch instruction. This also swaps any
2554 /// branch weight metadata associated with the instruction so that it
2555 /// continues to map correctly to each operand.
2556 void swapSuccessors();
2558 // Methods for support type inquiry through isa, cast, and dyn_cast:
2559 static inline bool classof(const Instruction *I) {
2560 return (I->getOpcode() == Instruction::Br);
2562 static inline bool classof(const Value *V) {
2563 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2566 BasicBlock *getSuccessorV(unsigned idx) const override;
2567 unsigned getNumSuccessorsV() const override;
2568 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2572 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2575 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2577 //===----------------------------------------------------------------------===//
2579 //===----------------------------------------------------------------------===//
2581 //===---------------------------------------------------------------------------
2582 /// SwitchInst - Multiway switch
2584 class SwitchInst : public TerminatorInst {
2585 void *operator new(size_t, unsigned) = delete;
2586 unsigned ReservedSpace;
2587 // Operand[0] = Value to switch on
2588 // Operand[1] = Default basic block destination
2589 // Operand[2n ] = Value to match
2590 // Operand[2n+1] = BasicBlock to go to on match
2591 SwitchInst(const SwitchInst &SI);
2592 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2593 void growOperands();
2594 // allocate space for exactly zero operands
2595 void *operator new(size_t s) {
2596 return User::operator new(s, 0);
2598 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2599 /// switch on and a default destination. The number of additional cases can
2600 /// be specified here to make memory allocation more efficient. This
2601 /// constructor can also autoinsert before another instruction.
2602 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2603 Instruction *InsertBefore);
2605 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2606 /// switch on and a default destination. The number of additional cases can
2607 /// be specified here to make memory allocation more efficient. This
2608 /// constructor also autoinserts at the end of the specified BasicBlock.
2609 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2610 BasicBlock *InsertAtEnd);
2612 SwitchInst *clone_impl() const override;
2616 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2618 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2619 class CaseIteratorT {
2627 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2629 /// Initializes case iterator for given SwitchInst and for given
2631 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2636 /// Initializes case iterator for given SwitchInst and for given
2637 /// TerminatorInst's successor index.
2638 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2639 assert(SuccessorIndex < SI->getNumSuccessors() &&
2640 "Successor index # out of range!");
2641 return SuccessorIndex != 0 ?
2642 Self(SI, SuccessorIndex - 1) :
2643 Self(SI, DefaultPseudoIndex);
2646 /// Resolves case value for current case.
2647 ConstantIntTy *getCaseValue() {
2648 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2649 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2652 /// Resolves successor for current case.
2653 BasicBlockTy *getCaseSuccessor() {
2654 assert((Index < SI->getNumCases() ||
2655 Index == DefaultPseudoIndex) &&
2656 "Index out the number of cases.");
2657 return SI->getSuccessor(getSuccessorIndex());
2660 /// Returns number of current case.
2661 unsigned getCaseIndex() const { return Index; }
2663 /// Returns TerminatorInst's successor index for current case successor.
2664 unsigned getSuccessorIndex() const {
2665 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2666 "Index out the number of cases.");
2667 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2671 // Check index correctness after increment.
2672 // Note: Index == getNumCases() means end().
2673 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2677 Self operator++(int) {
2683 // Check index correctness after decrement.
2684 // Note: Index == getNumCases() means end().
2685 // Also allow "-1" iterator here. That will became valid after ++.
2686 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2687 "Index out the number of cases.");
2691 Self operator--(int) {
2696 bool operator==(const Self& RHS) const {
2697 assert(RHS.SI == SI && "Incompatible operators.");
2698 return RHS.Index == Index;
2700 bool operator!=(const Self& RHS) const {
2701 assert(RHS.SI == SI && "Incompatible operators.");
2702 return RHS.Index != Index;
2709 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2712 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2714 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2718 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2719 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2721 /// Sets the new value for current case.
2722 void setValue(ConstantInt *V) {
2723 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2724 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2727 /// Sets the new successor for current case.
2728 void setSuccessor(BasicBlock *S) {
2729 SI->setSuccessor(getSuccessorIndex(), S);
2733 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2735 Instruction *InsertBefore = nullptr) {
2736 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2738 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2739 unsigned NumCases, BasicBlock *InsertAtEnd) {
2740 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2745 /// Provide fast operand accessors
2746 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2748 // Accessor Methods for Switch stmt
2749 Value *getCondition() const { return getOperand(0); }
2750 void setCondition(Value *V) { setOperand(0, V); }
2752 BasicBlock *getDefaultDest() const {
2753 return cast<BasicBlock>(getOperand(1));
2756 void setDefaultDest(BasicBlock *DefaultCase) {
2757 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2760 /// getNumCases - return the number of 'cases' in this switch instruction,
2761 /// except the default case
2762 unsigned getNumCases() const {
2763 return getNumOperands()/2 - 1;
2766 /// Returns a read/write iterator that points to the first
2767 /// case in SwitchInst.
2768 CaseIt case_begin() {
2769 return CaseIt(this, 0);
2771 /// Returns a read-only iterator that points to the first
2772 /// case in the SwitchInst.
2773 ConstCaseIt case_begin() const {
2774 return ConstCaseIt(this, 0);
2777 /// Returns a read/write iterator that points one past the last
2778 /// in the SwitchInst.
2780 return CaseIt(this, getNumCases());
2782 /// Returns a read-only iterator that points one past the last
2783 /// in the SwitchInst.
2784 ConstCaseIt case_end() const {
2785 return ConstCaseIt(this, getNumCases());
2788 /// cases - iteration adapter for range-for loops.
2789 iterator_range<CaseIt> cases() {
2790 return iterator_range<CaseIt>(case_begin(), case_end());
2793 /// cases - iteration adapter for range-for loops.
2794 iterator_range<ConstCaseIt> cases() const {
2795 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2798 /// Returns an iterator that points to the default case.
2799 /// Note: this iterator allows to resolve successor only. Attempt
2800 /// to resolve case value causes an assertion.
2801 /// Also note, that increment and decrement also causes an assertion and
2802 /// makes iterator invalid.
2803 CaseIt case_default() {
2804 return CaseIt(this, DefaultPseudoIndex);
2806 ConstCaseIt case_default() const {
2807 return ConstCaseIt(this, DefaultPseudoIndex);
2810 /// findCaseValue - Search all of the case values for the specified constant.
2811 /// If it is explicitly handled, return the case iterator of it, otherwise
2812 /// return default case iterator to indicate
2813 /// that it is handled by the default handler.
2814 CaseIt findCaseValue(const ConstantInt *C) {
2815 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2816 if (i.getCaseValue() == C)
2818 return case_default();
2820 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2821 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2822 if (i.getCaseValue() == C)
2824 return case_default();
2827 /// findCaseDest - Finds the unique case value for a given successor. Returns
2828 /// null if the successor is not found, not unique, or is the default case.
2829 ConstantInt *findCaseDest(BasicBlock *BB) {
2830 if (BB == getDefaultDest()) return nullptr;
2832 ConstantInt *CI = nullptr;
2833 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2834 if (i.getCaseSuccessor() == BB) {
2835 if (CI) return nullptr; // Multiple cases lead to BB.
2836 else CI = i.getCaseValue();
2842 /// addCase - Add an entry to the switch instruction...
2844 /// This action invalidates case_end(). Old case_end() iterator will
2845 /// point to the added case.
2846 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2848 /// removeCase - This method removes the specified case and its successor
2849 /// from the switch instruction. Note that this operation may reorder the
2850 /// remaining cases at index idx and above.
2852 /// This action invalidates iterators for all cases following the one removed,
2853 /// including the case_end() iterator.
2854 void removeCase(CaseIt i);
2856 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2857 BasicBlock *getSuccessor(unsigned idx) const {
2858 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2859 return cast<BasicBlock>(getOperand(idx*2+1));
2861 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2862 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2863 setOperand(idx*2+1, (Value*)NewSucc);
2866 // Methods for support type inquiry through isa, cast, and dyn_cast:
2867 static inline bool classof(const Instruction *I) {
2868 return I->getOpcode() == Instruction::Switch;
2870 static inline bool classof(const Value *V) {
2871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2874 BasicBlock *getSuccessorV(unsigned idx) const override;
2875 unsigned getNumSuccessorsV() const override;
2876 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2880 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2883 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2886 //===----------------------------------------------------------------------===//
2887 // IndirectBrInst Class
2888 //===----------------------------------------------------------------------===//
2890 //===---------------------------------------------------------------------------
2891 /// IndirectBrInst - Indirect Branch Instruction.
2893 class IndirectBrInst : public TerminatorInst {
2894 void *operator new(size_t, unsigned) = delete;
2895 unsigned ReservedSpace;
2896 // Operand[0] = Value to switch on
2897 // Operand[1] = Default basic block destination
2898 // Operand[2n ] = Value to match
2899 // Operand[2n+1] = BasicBlock to go to on match
2900 IndirectBrInst(const IndirectBrInst &IBI);
2901 void init(Value *Address, unsigned NumDests);
2902 void growOperands();
2903 // allocate space for exactly zero operands
2904 void *operator new(size_t s) {
2905 return User::operator new(s, 0);
2907 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2908 /// Address to jump to. The number of expected destinations can be specified
2909 /// here to make memory allocation more efficient. This constructor can also
2910 /// autoinsert before another instruction.
2911 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2913 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2914 /// Address to jump to. The number of expected destinations can be specified
2915 /// here to make memory allocation more efficient. This constructor also
2916 /// autoinserts at the end of the specified BasicBlock.
2917 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2919 IndirectBrInst *clone_impl() const override;
2921 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2922 Instruction *InsertBefore = nullptr) {
2923 return new IndirectBrInst(Address, NumDests, InsertBefore);
2925 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2926 BasicBlock *InsertAtEnd) {
2927 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2931 /// Provide fast operand accessors.
2932 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2934 // Accessor Methods for IndirectBrInst instruction.
2935 Value *getAddress() { return getOperand(0); }
2936 const Value *getAddress() const { return getOperand(0); }
2937 void setAddress(Value *V) { setOperand(0, V); }
2940 /// getNumDestinations - return the number of possible destinations in this
2941 /// indirectbr instruction.
2942 unsigned getNumDestinations() const { return getNumOperands()-1; }
2944 /// getDestination - Return the specified destination.
2945 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2946 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2948 /// addDestination - Add a destination.
2950 void addDestination(BasicBlock *Dest);
2952 /// removeDestination - This method removes the specified successor from the
2953 /// indirectbr instruction.
2954 void removeDestination(unsigned i);
2956 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2957 BasicBlock *getSuccessor(unsigned i) const {
2958 return cast<BasicBlock>(getOperand(i+1));
2960 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2961 setOperand(i+1, (Value*)NewSucc);
2964 // Methods for support type inquiry through isa, cast, and dyn_cast:
2965 static inline bool classof(const Instruction *I) {
2966 return I->getOpcode() == Instruction::IndirectBr;
2968 static inline bool classof(const Value *V) {
2969 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2972 BasicBlock *getSuccessorV(unsigned idx) const override;
2973 unsigned getNumSuccessorsV() const override;
2974 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2978 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2981 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2984 //===----------------------------------------------------------------------===//
2986 //===----------------------------------------------------------------------===//
2988 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2989 /// calling convention of the call.
2991 class InvokeInst : public TerminatorInst {
2992 AttributeSet AttributeList;
2993 InvokeInst(const InvokeInst &BI);
2994 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2995 ArrayRef<Value *> Args, const Twine &NameStr);
2997 /// Construct an InvokeInst given a range of arguments.
2999 /// \brief Construct an InvokeInst from a range of arguments
3000 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3001 ArrayRef<Value *> Args, unsigned Values,
3002 const Twine &NameStr, Instruction *InsertBefore);
3004 /// Construct an InvokeInst given a range of arguments.
3006 /// \brief Construct an InvokeInst from a range of arguments
3007 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3008 ArrayRef<Value *> Args, unsigned Values,
3009 const Twine &NameStr, BasicBlock *InsertAtEnd);
3011 InvokeInst *clone_impl() const override;
3013 static InvokeInst *Create(Value *Func,
3014 BasicBlock *IfNormal, BasicBlock *IfException,
3015 ArrayRef<Value *> Args, const Twine &NameStr = "",
3016 Instruction *InsertBefore = nullptr) {
3017 unsigned Values = unsigned(Args.size()) + 3;
3018 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3019 Values, NameStr, InsertBefore);
3021 static InvokeInst *Create(Value *Func,
3022 BasicBlock *IfNormal, BasicBlock *IfException,
3023 ArrayRef<Value *> Args, const Twine &NameStr,
3024 BasicBlock *InsertAtEnd) {
3025 unsigned Values = unsigned(Args.size()) + 3;
3026 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3027 Values, NameStr, InsertAtEnd);
3030 /// Provide fast operand accessors
3031 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3033 /// getNumArgOperands - Return the number of invoke arguments.
3035 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3037 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3039 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3040 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3042 /// arg_operands - iteration adapter for range-for loops.
3043 iterator_range<op_iterator> arg_operands() {
3044 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3047 /// arg_operands - iteration adapter for range-for loops.
3048 iterator_range<const_op_iterator> arg_operands() const {
3049 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3052 /// \brief Wrappers for getting the \c Use of a invoke argument.
3053 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3054 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3056 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3058 CallingConv::ID getCallingConv() const {
3059 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3061 void setCallingConv(CallingConv::ID CC) {
3062 setInstructionSubclassData(static_cast<unsigned>(CC));
3065 /// getAttributes - Return the parameter attributes for this invoke.
3067 const AttributeSet &getAttributes() const { return AttributeList; }
3069 /// setAttributes - Set the parameter attributes for this invoke.
3071 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3073 /// addAttribute - adds the attribute to the list of attributes.
3074 void addAttribute(unsigned i, Attribute::AttrKind attr);
3076 /// removeAttribute - removes the attribute from the list of attributes.
3077 void removeAttribute(unsigned i, Attribute attr);
3079 /// \brief removes the dereferenceable attribute to the list of attributes.
3080 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3082 /// \brief Determine whether this call has the given attribute.
3083 bool hasFnAttr(Attribute::AttrKind A) const {
3084 assert(A != Attribute::NoBuiltin &&
3085 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3086 return hasFnAttrImpl(A);
3089 /// \brief Determine whether the call or the callee has the given attributes.
3090 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3092 /// \brief Extract the alignment for a call or parameter (0=unknown).
3093 unsigned getParamAlignment(unsigned i) const {
3094 return AttributeList.getParamAlignment(i);
3097 /// \brief Extract the number of dereferenceable bytes for a call or
3098 /// parameter (0=unknown).
3099 uint64_t getDereferenceableBytes(unsigned i) const {
3100 return AttributeList.getDereferenceableBytes(i);
3103 /// \brief Return true if the call should not be treated as a call to a
3105 bool isNoBuiltin() const {
3106 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3107 // to check it by hand.
3108 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3109 !hasFnAttrImpl(Attribute::Builtin);
3112 /// \brief Return true if the call should not be inlined.
3113 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3114 void setIsNoInline() {
3115 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3118 /// \brief Determine if the call does not access memory.
3119 bool doesNotAccessMemory() const {
3120 return hasFnAttr(Attribute::ReadNone);
3122 void setDoesNotAccessMemory() {
3123 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3126 /// \brief Determine if the call does not access or only reads memory.
3127 bool onlyReadsMemory() const {
3128 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3130 void setOnlyReadsMemory() {
3131 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3134 /// \brief Determine if the call cannot return.
3135 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3136 void setDoesNotReturn() {
3137 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3140 /// \brief Determine if the call cannot unwind.
3141 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3142 void setDoesNotThrow() {
3143 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3146 /// \brief Determine if the invoke cannot be duplicated.
3147 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3148 void setCannotDuplicate() {
3149 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3152 /// \brief Determine if the call returns a structure through first
3153 /// pointer argument.
3154 bool hasStructRetAttr() const {
3155 // Be friendly and also check the callee.
3156 return paramHasAttr(1, Attribute::StructRet);
3159 /// \brief Determine if any call argument is an aggregate passed by value.
3160 bool hasByValArgument() const {
3161 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3164 /// getCalledFunction - Return the function called, or null if this is an
3165 /// indirect function invocation.
3167 Function *getCalledFunction() const {
3168 return dyn_cast<Function>(Op<-3>());
3171 /// getCalledValue - Get a pointer to the function that is invoked by this
3173 const Value *getCalledValue() const { return Op<-3>(); }
3174 Value *getCalledValue() { return Op<-3>(); }
3176 /// setCalledFunction - Set the function called.
3177 void setCalledFunction(Value* Fn) {
3181 // get*Dest - Return the destination basic blocks...
3182 BasicBlock *getNormalDest() const {
3183 return cast<BasicBlock>(Op<-2>());
3185 BasicBlock *getUnwindDest() const {
3186 return cast<BasicBlock>(Op<-1>());
3188 void setNormalDest(BasicBlock *B) {
3189 Op<-2>() = reinterpret_cast<Value*>(B);
3191 void setUnwindDest(BasicBlock *B) {
3192 Op<-1>() = reinterpret_cast<Value*>(B);
3195 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3196 /// block (the unwind destination).
3197 LandingPadInst *getLandingPadInst() const;
3199 BasicBlock *getSuccessor(unsigned i) const {
3200 assert(i < 2 && "Successor # out of range for invoke!");
3201 return i == 0 ? getNormalDest() : getUnwindDest();
3204 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3205 assert(idx < 2 && "Successor # out of range for invoke!");
3206 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3209 unsigned getNumSuccessors() const { return 2; }
3211 // Methods for support type inquiry through isa, cast, and dyn_cast:
3212 static inline bool classof(const Instruction *I) {
3213 return (I->getOpcode() == Instruction::Invoke);
3215 static inline bool classof(const Value *V) {
3216 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3220 BasicBlock *getSuccessorV(unsigned idx) const override;
3221 unsigned getNumSuccessorsV() const override;
3222 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3224 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3226 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3227 // method so that subclasses cannot accidentally use it.
3228 void setInstructionSubclassData(unsigned short D) {
3229 Instruction::setInstructionSubclassData(D);
3234 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3237 InvokeInst::InvokeInst(Value *Func,
3238 BasicBlock *IfNormal, BasicBlock *IfException,
3239 ArrayRef<Value *> Args, unsigned Values,
3240 const Twine &NameStr, Instruction *InsertBefore)
3241 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3242 ->getElementType())->getReturnType(),
3243 Instruction::Invoke,
3244 OperandTraits<InvokeInst>::op_end(this) - Values,
3245 Values, InsertBefore) {
3246 init(Func, IfNormal, IfException, Args, NameStr);
3248 InvokeInst::InvokeInst(Value *Func,
3249 BasicBlock *IfNormal, BasicBlock *IfException,
3250 ArrayRef<Value *> Args, unsigned Values,
3251 const Twine &NameStr, BasicBlock *InsertAtEnd)
3252 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3253 ->getElementType())->getReturnType(),
3254 Instruction::Invoke,
3255 OperandTraits<InvokeInst>::op_end(this) - Values,
3256 Values, InsertAtEnd) {
3257 init(Func, IfNormal, IfException, Args, NameStr);
3260 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3262 //===----------------------------------------------------------------------===//
3264 //===----------------------------------------------------------------------===//
3266 //===---------------------------------------------------------------------------
3267 /// ResumeInst - Resume the propagation of an exception.
3269 class ResumeInst : public TerminatorInst {
3270 ResumeInst(const ResumeInst &RI);
3272 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3273 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3275 ResumeInst *clone_impl() const override;
3277 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3278 return new(1) ResumeInst(Exn, InsertBefore);
3280 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3281 return new(1) ResumeInst(Exn, InsertAtEnd);
3284 /// Provide fast operand accessors
3285 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3287 /// Convenience accessor.
3288 Value *getValue() const { return Op<0>(); }
3290 unsigned getNumSuccessors() const { return 0; }
3292 // Methods for support type inquiry through isa, cast, and dyn_cast:
3293 static inline bool classof(const Instruction *I) {
3294 return I->getOpcode() == Instruction::Resume;
3296 static inline bool classof(const Value *V) {
3297 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3300 BasicBlock *getSuccessorV(unsigned idx) const override;
3301 unsigned getNumSuccessorsV() const override;
3302 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3306 struct OperandTraits<ResumeInst> :
3307 public FixedNumOperandTraits<ResumeInst, 1> {
3310 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3312 //===----------------------------------------------------------------------===//
3313 // UnreachableInst Class
3314 //===----------------------------------------------------------------------===//
3316 //===---------------------------------------------------------------------------
3317 /// UnreachableInst - This function has undefined behavior. In particular, the
3318 /// presence of this instruction indicates some higher level knowledge that the
3319 /// end of the block cannot be reached.
3321 class UnreachableInst : public TerminatorInst {
3322 void *operator new(size_t, unsigned) = delete;
3324 UnreachableInst *clone_impl() const override;
3327 // allocate space for exactly zero operands
3328 void *operator new(size_t s) {
3329 return User::operator new(s, 0);
3331 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3332 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3334 unsigned getNumSuccessors() const { return 0; }
3336 // Methods for support type inquiry through isa, cast, and dyn_cast:
3337 static inline bool classof(const Instruction *I) {
3338 return I->getOpcode() == Instruction::Unreachable;
3340 static inline bool classof(const Value *V) {
3341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3344 BasicBlock *getSuccessorV(unsigned idx) const override;
3345 unsigned getNumSuccessorsV() const override;
3346 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3349 //===----------------------------------------------------------------------===//
3351 //===----------------------------------------------------------------------===//
3353 /// \brief This class represents a truncation of integer types.
3354 class TruncInst : public CastInst {
3356 /// \brief Clone an identical TruncInst
3357 TruncInst *clone_impl() const override;
3360 /// \brief Constructor with insert-before-instruction semantics
3362 Value *S, ///< The value to be truncated
3363 Type *Ty, ///< The (smaller) type to truncate to
3364 const Twine &NameStr = "", ///< A name for the new instruction
3365 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3368 /// \brief Constructor with insert-at-end-of-block semantics
3370 Value *S, ///< The value to be truncated
3371 Type *Ty, ///< The (smaller) type to truncate to
3372 const Twine &NameStr, ///< A name for the new instruction
3373 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3376 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3377 static inline bool classof(const Instruction *I) {
3378 return I->getOpcode() == Trunc;
3380 static inline bool classof(const Value *V) {
3381 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3385 //===----------------------------------------------------------------------===//
3387 //===----------------------------------------------------------------------===//
3389 /// \brief This class represents zero extension of integer types.
3390 class ZExtInst : public CastInst {
3392 /// \brief Clone an identical ZExtInst
3393 ZExtInst *clone_impl() const override;
3396 /// \brief Constructor with insert-before-instruction semantics
3398 Value *S, ///< The value to be zero extended
3399 Type *Ty, ///< The type to zero extend to
3400 const Twine &NameStr = "", ///< A name for the new instruction
3401 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3404 /// \brief Constructor with insert-at-end semantics.
3406 Value *S, ///< The value to be zero extended
3407 Type *Ty, ///< The type to zero extend to
3408 const Twine &NameStr, ///< A name for the new instruction
3409 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3412 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3413 static inline bool classof(const Instruction *I) {
3414 return I->getOpcode() == ZExt;
3416 static inline bool classof(const Value *V) {
3417 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3421 //===----------------------------------------------------------------------===//
3423 //===----------------------------------------------------------------------===//
3425 /// \brief This class represents a sign extension of integer types.
3426 class SExtInst : public CastInst {
3428 /// \brief Clone an identical SExtInst
3429 SExtInst *clone_impl() const override;
3432 /// \brief Constructor with insert-before-instruction semantics
3434 Value *S, ///< The value to be sign extended
3435 Type *Ty, ///< The type to sign extend to
3436 const Twine &NameStr = "", ///< A name for the new instruction
3437 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3440 /// \brief Constructor with insert-at-end-of-block semantics
3442 Value *S, ///< The value to be sign extended
3443 Type *Ty, ///< The type to sign extend to
3444 const Twine &NameStr, ///< A name for the new instruction
3445 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3448 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3449 static inline bool classof(const Instruction *I) {
3450 return I->getOpcode() == SExt;
3452 static inline bool classof(const Value *V) {
3453 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3457 //===----------------------------------------------------------------------===//
3458 // FPTruncInst Class
3459 //===----------------------------------------------------------------------===//
3461 /// \brief This class represents a truncation of floating point types.
3462 class FPTruncInst : public CastInst {
3464 /// \brief Clone an identical FPTruncInst
3465 FPTruncInst *clone_impl() const override;
3468 /// \brief Constructor with insert-before-instruction semantics
3470 Value *S, ///< The value to be truncated
3471 Type *Ty, ///< The type to truncate to
3472 const Twine &NameStr = "", ///< A name for the new instruction
3473 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3476 /// \brief Constructor with insert-before-instruction semantics
3478 Value *S, ///< The value to be truncated
3479 Type *Ty, ///< The type to truncate to
3480 const Twine &NameStr, ///< A name for the new instruction
3481 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3484 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3485 static inline bool classof(const Instruction *I) {
3486 return I->getOpcode() == FPTrunc;
3488 static inline bool classof(const Value *V) {
3489 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3493 //===----------------------------------------------------------------------===//
3495 //===----------------------------------------------------------------------===//
3497 /// \brief This class represents an extension of floating point types.
3498 class FPExtInst : public CastInst {
3500 /// \brief Clone an identical FPExtInst
3501 FPExtInst *clone_impl() const override;
3504 /// \brief Constructor with insert-before-instruction semantics
3506 Value *S, ///< The value to be extended
3507 Type *Ty, ///< The type to extend to
3508 const Twine &NameStr = "", ///< A name for the new instruction
3509 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3512 /// \brief Constructor with insert-at-end-of-block semantics
3514 Value *S, ///< The value to be extended
3515 Type *Ty, ///< The type to extend to
3516 const Twine &NameStr, ///< A name for the new instruction
3517 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3520 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3521 static inline bool classof(const Instruction *I) {
3522 return I->getOpcode() == FPExt;
3524 static inline bool classof(const Value *V) {
3525 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3529 //===----------------------------------------------------------------------===//
3531 //===----------------------------------------------------------------------===//
3533 /// \brief This class represents a cast unsigned integer to floating point.
3534 class UIToFPInst : public CastInst {
3536 /// \brief Clone an identical UIToFPInst
3537 UIToFPInst *clone_impl() const override;
3540 /// \brief Constructor with insert-before-instruction semantics
3542 Value *S, ///< The value to be converted
3543 Type *Ty, ///< The type to convert to
3544 const Twine &NameStr = "", ///< A name for the new instruction
3545 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3548 /// \brief Constructor with insert-at-end-of-block semantics
3550 Value *S, ///< The value to be converted
3551 Type *Ty, ///< The type to convert to
3552 const Twine &NameStr, ///< A name for the new instruction
3553 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3556 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3557 static inline bool classof(const Instruction *I) {
3558 return I->getOpcode() == UIToFP;
3560 static inline bool classof(const Value *V) {
3561 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3565 //===----------------------------------------------------------------------===//
3567 //===----------------------------------------------------------------------===//
3569 /// \brief This class represents a cast from signed integer to floating point.
3570 class SIToFPInst : public CastInst {
3572 /// \brief Clone an identical SIToFPInst
3573 SIToFPInst *clone_impl() const override;
3576 /// \brief Constructor with insert-before-instruction semantics
3578 Value *S, ///< The value to be converted
3579 Type *Ty, ///< The type to convert to
3580 const Twine &NameStr = "", ///< A name for the new instruction
3581 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3584 /// \brief Constructor with insert-at-end-of-block semantics
3586 Value *S, ///< The value to be converted
3587 Type *Ty, ///< The type to convert to
3588 const Twine &NameStr, ///< A name for the new instruction
3589 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3592 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3593 static inline bool classof(const Instruction *I) {
3594 return I->getOpcode() == SIToFP;
3596 static inline bool classof(const Value *V) {
3597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3601 //===----------------------------------------------------------------------===//
3603 //===----------------------------------------------------------------------===//
3605 /// \brief This class represents a cast from floating point to unsigned integer
3606 class FPToUIInst : public CastInst {
3608 /// \brief Clone an identical FPToUIInst
3609 FPToUIInst *clone_impl() const override;
3612 /// \brief Constructor with insert-before-instruction semantics
3614 Value *S, ///< The value to be converted
3615 Type *Ty, ///< The type to convert to
3616 const Twine &NameStr = "", ///< A name for the new instruction
3617 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3620 /// \brief Constructor with insert-at-end-of-block semantics
3622 Value *S, ///< The value to be converted
3623 Type *Ty, ///< The type to convert to
3624 const Twine &NameStr, ///< A name for the new instruction
3625 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3628 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3629 static inline bool classof(const Instruction *I) {
3630 return I->getOpcode() == FPToUI;
3632 static inline bool classof(const Value *V) {
3633 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3637 //===----------------------------------------------------------------------===//
3639 //===----------------------------------------------------------------------===//
3641 /// \brief This class represents a cast from floating point to signed integer.
3642 class FPToSIInst : public CastInst {
3644 /// \brief Clone an identical FPToSIInst
3645 FPToSIInst *clone_impl() const override;
3648 /// \brief Constructor with insert-before-instruction semantics
3650 Value *S, ///< The value to be converted
3651 Type *Ty, ///< The type to convert to
3652 const Twine &NameStr = "", ///< A name for the new instruction
3653 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3656 /// \brief Constructor with insert-at-end-of-block semantics
3658 Value *S, ///< The value to be converted
3659 Type *Ty, ///< The type to convert to
3660 const Twine &NameStr, ///< A name for the new instruction
3661 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3664 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3665 static inline bool classof(const Instruction *I) {
3666 return I->getOpcode() == FPToSI;
3668 static inline bool classof(const Value *V) {
3669 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3673 //===----------------------------------------------------------------------===//
3674 // IntToPtrInst Class
3675 //===----------------------------------------------------------------------===//
3677 /// \brief This class represents a cast from an integer to a pointer.
3678 class IntToPtrInst : public CastInst {
3680 /// \brief Constructor with insert-before-instruction semantics
3682 Value *S, ///< The value to be converted
3683 Type *Ty, ///< The type to convert to
3684 const Twine &NameStr = "", ///< A name for the new instruction
3685 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3688 /// \brief Constructor with insert-at-end-of-block semantics
3690 Value *S, ///< The value to be converted
3691 Type *Ty, ///< The type to convert to
3692 const Twine &NameStr, ///< A name for the new instruction
3693 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3696 /// \brief Clone an identical IntToPtrInst
3697 IntToPtrInst *clone_impl() const override;
3699 /// \brief Returns the address space of this instruction's pointer type.
3700 unsigned getAddressSpace() const {
3701 return getType()->getPointerAddressSpace();
3704 // Methods for support type inquiry through isa, cast, and dyn_cast:
3705 static inline bool classof(const Instruction *I) {
3706 return I->getOpcode() == IntToPtr;
3708 static inline bool classof(const Value *V) {
3709 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3713 //===----------------------------------------------------------------------===//
3714 // PtrToIntInst Class
3715 //===----------------------------------------------------------------------===//
3717 /// \brief This class represents a cast from a pointer to an integer
3718 class PtrToIntInst : public CastInst {
3720 /// \brief Clone an identical PtrToIntInst
3721 PtrToIntInst *clone_impl() const override;
3724 /// \brief Constructor with insert-before-instruction semantics
3726 Value *S, ///< The value to be converted
3727 Type *Ty, ///< The type to convert to
3728 const Twine &NameStr = "", ///< A name for the new instruction
3729 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3732 /// \brief Constructor with insert-at-end-of-block semantics
3734 Value *S, ///< The value to be converted
3735 Type *Ty, ///< The type to convert to
3736 const Twine &NameStr, ///< A name for the new instruction
3737 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3740 /// \brief Gets the pointer operand.
3741 Value *getPointerOperand() { return getOperand(0); }
3742 /// \brief Gets the pointer operand.
3743 const Value *getPointerOperand() const { return getOperand(0); }
3744 /// \brief Gets the operand index of the pointer operand.
3745 static unsigned getPointerOperandIndex() { return 0U; }
3747 /// \brief Returns the address space of the pointer operand.
3748 unsigned getPointerAddressSpace() const {
3749 return getPointerOperand()->getType()->getPointerAddressSpace();
3752 // Methods for support type inquiry through isa, cast, and dyn_cast:
3753 static inline bool classof(const Instruction *I) {
3754 return I->getOpcode() == PtrToInt;
3756 static inline bool classof(const Value *V) {
3757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3761 //===----------------------------------------------------------------------===//
3762 // BitCastInst Class
3763 //===----------------------------------------------------------------------===//
3765 /// \brief This class represents a no-op cast from one type to another.
3766 class BitCastInst : public CastInst {
3768 /// \brief Clone an identical BitCastInst
3769 BitCastInst *clone_impl() const override;
3772 /// \brief Constructor with insert-before-instruction semantics
3774 Value *S, ///< The value to be casted
3775 Type *Ty, ///< The type to casted to
3776 const Twine &NameStr = "", ///< A name for the new instruction
3777 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3780 /// \brief Constructor with insert-at-end-of-block semantics
3782 Value *S, ///< The value to be casted
3783 Type *Ty, ///< The type to casted to
3784 const Twine &NameStr, ///< A name for the new instruction
3785 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3788 // Methods for support type inquiry through isa, cast, and dyn_cast:
3789 static inline bool classof(const Instruction *I) {
3790 return I->getOpcode() == BitCast;
3792 static inline bool classof(const Value *V) {
3793 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3797 //===----------------------------------------------------------------------===//
3798 // AddrSpaceCastInst Class
3799 //===----------------------------------------------------------------------===//
3801 /// \brief This class represents a conversion between pointers from
3802 /// one address space to another.
3803 class AddrSpaceCastInst : public CastInst {
3805 /// \brief Clone an identical AddrSpaceCastInst
3806 AddrSpaceCastInst *clone_impl() const override;
3809 /// \brief Constructor with insert-before-instruction semantics
3811 Value *S, ///< The value to be casted
3812 Type *Ty, ///< The type to casted to
3813 const Twine &NameStr = "", ///< A name for the new instruction
3814 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3817 /// \brief Constructor with insert-at-end-of-block semantics
3819 Value *S, ///< The value to be casted
3820 Type *Ty, ///< The type to casted to
3821 const Twine &NameStr, ///< A name for the new instruction
3822 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3825 // Methods for support type inquiry through isa, cast, and dyn_cast:
3826 static inline bool classof(const Instruction *I) {
3827 return I->getOpcode() == AddrSpaceCast;
3829 static inline bool classof(const Value *V) {
3830 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3834 } // End llvm namespace