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 SequentialType *Ty = cast<SequentialType>(getPointerOperandType());
850 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
851 Ty = cast<SequentialType>(VTy->getElementType());
852 return Ty->getElementType();
855 /// \brief Returns the address space of this instruction's pointer type.
856 unsigned getAddressSpace() const {
857 // Note that this is always the same as the pointer operand's address space
858 // and that is cheaper to compute, so cheat here.
859 return getPointerAddressSpace();
862 /// getIndexedType - Returns the type of the element that would be loaded with
863 /// a load instruction with the specified parameters.
865 /// Null is returned if the indices are invalid for the specified
868 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
869 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
870 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
872 inline op_iterator idx_begin() { return op_begin()+1; }
873 inline const_op_iterator idx_begin() const { return op_begin()+1; }
874 inline op_iterator idx_end() { return op_end(); }
875 inline const_op_iterator idx_end() const { return op_end(); }
877 Value *getPointerOperand() {
878 return getOperand(0);
880 const Value *getPointerOperand() const {
881 return getOperand(0);
883 static unsigned getPointerOperandIndex() {
884 return 0U; // get index for modifying correct operand.
887 /// getPointerOperandType - Method to return the pointer operand as a
889 Type *getPointerOperandType() const {
890 return getPointerOperand()->getType();
893 /// \brief Returns the address space of the pointer operand.
894 unsigned getPointerAddressSpace() const {
895 return getPointerOperandType()->getPointerAddressSpace();
898 /// GetGEPReturnType - Returns the pointer type returned by the GEP
899 /// instruction, which may be a vector of pointers.
900 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
901 Type *PtrTy = PointerType::get(checkGEPType(
902 getIndexedType(Ptr->getType(), IdxList)),
903 Ptr->getType()->getPointerAddressSpace());
905 if (Ptr->getType()->isVectorTy()) {
906 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
907 return VectorType::get(PtrTy, NumElem);
914 unsigned getNumIndices() const { // Note: always non-negative
915 return getNumOperands() - 1;
918 bool hasIndices() const {
919 return getNumOperands() > 1;
922 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
923 /// zeros. If so, the result pointer and the first operand have the same
924 /// value, just potentially different types.
925 bool hasAllZeroIndices() const;
927 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
928 /// constant integers. If so, the result pointer and the first operand have
929 /// a constant offset between them.
930 bool hasAllConstantIndices() const;
932 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
933 /// See LangRef.html for the meaning of inbounds on a getelementptr.
934 void setIsInBounds(bool b = true);
936 /// isInBounds - Determine whether the GEP has the inbounds flag.
937 bool isInBounds() const;
939 /// \brief Accumulate the constant address offset of this GEP if possible.
941 /// This routine accepts an APInt into which it will accumulate the constant
942 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
943 /// all-constant, it returns false and the value of the offset APInt is
944 /// undefined (it is *not* preserved!). The APInt passed into this routine
945 /// must be at least as wide as the IntPtr type for the address space of
946 /// the base GEP pointer.
947 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
949 // Methods for support type inquiry through isa, cast, and dyn_cast:
950 static inline bool classof(const Instruction *I) {
951 return (I->getOpcode() == Instruction::GetElementPtr);
953 static inline bool classof(const Value *V) {
954 return isa<Instruction>(V) && classof(cast<Instruction>(V));
959 struct OperandTraits<GetElementPtrInst> :
960 public VariadicOperandTraits<GetElementPtrInst, 1> {
963 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
964 ArrayRef<Value *> IdxList,
966 const Twine &NameStr,
967 Instruction *InsertBefore)
968 : Instruction(getGEPReturnType(Ptr, IdxList),
970 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
971 Values, InsertBefore) {
972 init(Ptr, IdxList, NameStr);
974 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
975 ArrayRef<Value *> IdxList,
977 const Twine &NameStr,
978 BasicBlock *InsertAtEnd)
979 : Instruction(getGEPReturnType(Ptr, IdxList),
981 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
982 Values, InsertAtEnd) {
983 init(Ptr, IdxList, NameStr);
987 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
990 //===----------------------------------------------------------------------===//
992 //===----------------------------------------------------------------------===//
994 /// This instruction compares its operands according to the predicate given
995 /// to the constructor. It only operates on integers or pointers. The operands
996 /// must be identical types.
997 /// \brief Represent an integer comparison operator.
998 class ICmpInst: public CmpInst {
1000 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1001 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1002 "Invalid ICmp predicate value");
1003 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1004 "Both operands to ICmp instruction are not of the same type!");
1005 // Check that the operands are the right type
1006 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1007 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1008 "Invalid operand types for ICmp instruction");
1012 /// \brief Clone an identical ICmpInst
1013 ICmpInst *clone_impl() const override;
1015 /// \brief Constructor with insert-before-instruction semantics.
1017 Instruction *InsertBefore, ///< Where to insert
1018 Predicate pred, ///< The predicate to use for the comparison
1019 Value *LHS, ///< The left-hand-side of the expression
1020 Value *RHS, ///< The right-hand-side of the expression
1021 const Twine &NameStr = "" ///< Name of the instruction
1022 ) : CmpInst(makeCmpResultType(LHS->getType()),
1023 Instruction::ICmp, pred, LHS, RHS, NameStr,
1030 /// \brief Constructor with insert-at-end semantics.
1032 BasicBlock &InsertAtEnd, ///< Block to insert into.
1033 Predicate pred, ///< The predicate to use for the comparison
1034 Value *LHS, ///< The left-hand-side of the expression
1035 Value *RHS, ///< The right-hand-side of the expression
1036 const Twine &NameStr = "" ///< Name of the instruction
1037 ) : CmpInst(makeCmpResultType(LHS->getType()),
1038 Instruction::ICmp, pred, LHS, RHS, NameStr,
1045 /// \brief Constructor with no-insertion semantics
1047 Predicate pred, ///< The predicate to use for the comparison
1048 Value *LHS, ///< The left-hand-side of the expression
1049 Value *RHS, ///< The right-hand-side of the expression
1050 const Twine &NameStr = "" ///< Name of the instruction
1051 ) : CmpInst(makeCmpResultType(LHS->getType()),
1052 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1058 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1059 /// @returns the predicate that would be the result if the operand were
1060 /// regarded as signed.
1061 /// \brief Return the signed version of the predicate
1062 Predicate getSignedPredicate() const {
1063 return getSignedPredicate(getPredicate());
1066 /// This is a static version that you can use without an instruction.
1067 /// \brief Return the signed version of the predicate.
1068 static Predicate getSignedPredicate(Predicate pred);
1070 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1071 /// @returns the predicate that would be the result if the operand were
1072 /// regarded as unsigned.
1073 /// \brief Return the unsigned version of the predicate
1074 Predicate getUnsignedPredicate() const {
1075 return getUnsignedPredicate(getPredicate());
1078 /// This is a static version that you can use without an instruction.
1079 /// \brief Return the unsigned version of the predicate.
1080 static Predicate getUnsignedPredicate(Predicate pred);
1082 /// isEquality - Return true if this predicate is either EQ or NE. This also
1083 /// tests for commutativity.
1084 static bool isEquality(Predicate P) {
1085 return P == ICMP_EQ || P == ICMP_NE;
1088 /// isEquality - Return true if this predicate is either EQ or NE. This also
1089 /// tests for commutativity.
1090 bool isEquality() const {
1091 return isEquality(getPredicate());
1094 /// @returns true if the predicate of this ICmpInst is commutative
1095 /// \brief Determine if this relation is commutative.
1096 bool isCommutative() const { return isEquality(); }
1098 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1100 bool isRelational() const {
1101 return !isEquality();
1104 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1106 static bool isRelational(Predicate P) {
1107 return !isEquality(P);
1110 /// Initialize a set of values that all satisfy the predicate with C.
1111 /// \brief Make a ConstantRange for a relation with a constant value.
1112 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1114 /// Exchange the two operands to this instruction in such a way that it does
1115 /// not modify the semantics of the instruction. The predicate value may be
1116 /// changed to retain the same result if the predicate is order dependent
1118 /// \brief Swap operands and adjust predicate.
1119 void swapOperands() {
1120 setPredicate(getSwappedPredicate());
1121 Op<0>().swap(Op<1>());
1124 // Methods for support type inquiry through isa, cast, and dyn_cast:
1125 static inline bool classof(const Instruction *I) {
1126 return I->getOpcode() == Instruction::ICmp;
1128 static inline bool classof(const Value *V) {
1129 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1134 //===----------------------------------------------------------------------===//
1136 //===----------------------------------------------------------------------===//
1138 /// This instruction compares its operands according to the predicate given
1139 /// to the constructor. It only operates on floating point values or packed
1140 /// vectors of floating point values. The operands must be identical types.
1141 /// \brief Represents a floating point comparison operator.
1142 class FCmpInst: public CmpInst {
1144 /// \brief Clone an identical FCmpInst
1145 FCmpInst *clone_impl() const override;
1147 /// \brief Constructor with insert-before-instruction semantics.
1149 Instruction *InsertBefore, ///< Where to insert
1150 Predicate pred, ///< The predicate to use for the comparison
1151 Value *LHS, ///< The left-hand-side of the expression
1152 Value *RHS, ///< The right-hand-side of the expression
1153 const Twine &NameStr = "" ///< Name of the instruction
1154 ) : CmpInst(makeCmpResultType(LHS->getType()),
1155 Instruction::FCmp, pred, LHS, RHS, NameStr,
1157 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1158 "Invalid FCmp predicate value");
1159 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1160 "Both operands to FCmp instruction are not of the same type!");
1161 // Check that the operands are the right type
1162 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1163 "Invalid operand types for FCmp instruction");
1166 /// \brief Constructor with insert-at-end semantics.
1168 BasicBlock &InsertAtEnd, ///< Block to insert into.
1169 Predicate pred, ///< The predicate to use for the comparison
1170 Value *LHS, ///< The left-hand-side of the expression
1171 Value *RHS, ///< The right-hand-side of the expression
1172 const Twine &NameStr = "" ///< Name of the instruction
1173 ) : CmpInst(makeCmpResultType(LHS->getType()),
1174 Instruction::FCmp, pred, LHS, RHS, NameStr,
1176 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1177 "Invalid FCmp predicate value");
1178 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1179 "Both operands to FCmp instruction are not of the same type!");
1180 // Check that the operands are the right type
1181 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1182 "Invalid operand types for FCmp instruction");
1185 /// \brief Constructor with no-insertion semantics
1187 Predicate pred, ///< The predicate to use for the comparison
1188 Value *LHS, ///< The left-hand-side of the expression
1189 Value *RHS, ///< The right-hand-side of the expression
1190 const Twine &NameStr = "" ///< Name of the instruction
1191 ) : CmpInst(makeCmpResultType(LHS->getType()),
1192 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1193 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1194 "Invalid FCmp predicate value");
1195 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1196 "Both operands to FCmp instruction are not of the same type!");
1197 // Check that the operands are the right type
1198 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1199 "Invalid operand types for FCmp instruction");
1202 /// @returns true if the predicate of this instruction is EQ or NE.
1203 /// \brief Determine if this is an equality predicate.
1204 bool isEquality() const {
1205 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1206 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1209 /// @returns true if the predicate of this instruction is commutative.
1210 /// \brief Determine if this is a commutative predicate.
1211 bool isCommutative() const {
1212 return isEquality() ||
1213 getPredicate() == FCMP_FALSE ||
1214 getPredicate() == FCMP_TRUE ||
1215 getPredicate() == FCMP_ORD ||
1216 getPredicate() == FCMP_UNO;
1219 /// @returns true if the predicate is relational (not EQ or NE).
1220 /// \brief Determine if this a relational predicate.
1221 bool isRelational() const { return !isEquality(); }
1223 /// Exchange the two operands to this instruction in such a way that it does
1224 /// not modify the semantics of the instruction. The predicate value may be
1225 /// changed to retain the same result if the predicate is order dependent
1227 /// \brief Swap operands and adjust predicate.
1228 void swapOperands() {
1229 setPredicate(getSwappedPredicate());
1230 Op<0>().swap(Op<1>());
1233 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1234 static inline bool classof(const Instruction *I) {
1235 return I->getOpcode() == Instruction::FCmp;
1237 static inline bool classof(const Value *V) {
1238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1242 //===----------------------------------------------------------------------===//
1243 /// CallInst - This class represents a function call, abstracting a target
1244 /// machine's calling convention. This class uses low bit of the SubClassData
1245 /// field to indicate whether or not this is a tail call. The rest of the bits
1246 /// hold the calling convention of the call.
1248 class CallInst : public Instruction {
1249 AttributeSet AttributeList; ///< parameter attributes for call
1250 CallInst(const CallInst &CI);
1251 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1252 void init(Value *Func, const Twine &NameStr);
1254 /// Construct a CallInst given a range of arguments.
1255 /// \brief Construct a CallInst from a range of arguments
1256 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1257 const Twine &NameStr, Instruction *InsertBefore);
1259 /// Construct a CallInst given a range of arguments.
1260 /// \brief Construct a CallInst from a range of arguments
1261 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1262 const Twine &NameStr, BasicBlock *InsertAtEnd);
1264 explicit CallInst(Value *F, const Twine &NameStr,
1265 Instruction *InsertBefore);
1266 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1268 CallInst *clone_impl() const override;
1270 static CallInst *Create(Value *Func,
1271 ArrayRef<Value *> Args,
1272 const Twine &NameStr = "",
1273 Instruction *InsertBefore = nullptr) {
1274 return new(unsigned(Args.size() + 1))
1275 CallInst(Func, Args, NameStr, InsertBefore);
1277 static CallInst *Create(Value *Func,
1278 ArrayRef<Value *> Args,
1279 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1280 return new(unsigned(Args.size() + 1))
1281 CallInst(Func, Args, NameStr, InsertAtEnd);
1283 static CallInst *Create(Value *F, const Twine &NameStr = "",
1284 Instruction *InsertBefore = nullptr) {
1285 return new(1) CallInst(F, NameStr, InsertBefore);
1287 static CallInst *Create(Value *F, const Twine &NameStr,
1288 BasicBlock *InsertAtEnd) {
1289 return new(1) CallInst(F, NameStr, InsertAtEnd);
1291 /// CreateMalloc - Generate the IR for a call to malloc:
1292 /// 1. Compute the malloc call's argument as the specified type's size,
1293 /// possibly multiplied by the array size if the array size is not
1295 /// 2. Call malloc with that argument.
1296 /// 3. Bitcast the result of the malloc call to the specified type.
1297 static Instruction *CreateMalloc(Instruction *InsertBefore,
1298 Type *IntPtrTy, Type *AllocTy,
1299 Value *AllocSize, Value *ArraySize = nullptr,
1300 Function* MallocF = nullptr,
1301 const Twine &Name = "");
1302 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1303 Type *IntPtrTy, Type *AllocTy,
1304 Value *AllocSize, Value *ArraySize = nullptr,
1305 Function* MallocF = nullptr,
1306 const Twine &Name = "");
1307 /// CreateFree - Generate the IR for a call to the builtin free function.
1308 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1309 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1313 // Note that 'musttail' implies 'tail'.
1314 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1315 TailCallKind getTailCallKind() const {
1316 return TailCallKind(getSubclassDataFromInstruction() & 3);
1318 bool isTailCall() const {
1319 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1321 bool isMustTailCall() const {
1322 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1324 void setTailCall(bool isTC = true) {
1325 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1326 unsigned(isTC ? TCK_Tail : TCK_None));
1328 void setTailCallKind(TailCallKind TCK) {
1329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1333 /// Provide fast operand accessors
1334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1336 /// getNumArgOperands - Return the number of call arguments.
1338 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1340 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1342 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1343 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1345 /// arg_operands - iteration adapter for range-for loops.
1346 iterator_range<op_iterator> arg_operands() {
1347 // The last operand in the op list is the callee - it's not one of the args
1348 // so we don't want to iterate over it.
1349 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1352 /// arg_operands - iteration adapter for range-for loops.
1353 iterator_range<const_op_iterator> arg_operands() const {
1354 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1357 /// \brief Wrappers for getting the \c Use of a call argument.
1358 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1359 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1361 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1363 CallingConv::ID getCallingConv() const {
1364 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1366 void setCallingConv(CallingConv::ID CC) {
1367 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1368 (static_cast<unsigned>(CC) << 2));
1371 /// getAttributes - Return the parameter attributes for this call.
1373 const AttributeSet &getAttributes() const { return AttributeList; }
1375 /// setAttributes - Set the parameter attributes for this call.
1377 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1379 /// addAttribute - adds the attribute to the list of attributes.
1380 void addAttribute(unsigned i, Attribute::AttrKind attr);
1382 /// removeAttribute - removes the attribute from the list of attributes.
1383 void removeAttribute(unsigned i, Attribute attr);
1385 /// \brief adds the dereferenceable attribute to the list of attributes.
1386 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1388 /// \brief Determine whether this call has the given attribute.
1389 bool hasFnAttr(Attribute::AttrKind A) const {
1390 assert(A != Attribute::NoBuiltin &&
1391 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1392 return hasFnAttrImpl(A);
1395 /// \brief Determine whether the call or the callee has the given attributes.
1396 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1398 /// \brief Extract the alignment for a call or parameter (0=unknown).
1399 unsigned getParamAlignment(unsigned i) const {
1400 return AttributeList.getParamAlignment(i);
1403 /// \brief Extract the number of dereferenceable bytes for a call or
1404 /// parameter (0=unknown).
1405 uint64_t getDereferenceableBytes(unsigned i) const {
1406 return AttributeList.getDereferenceableBytes(i);
1409 /// \brief Return true if the call should not be treated as a call to a
1411 bool isNoBuiltin() const {
1412 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1413 !hasFnAttrImpl(Attribute::Builtin);
1416 /// \brief Return true if the call should not be inlined.
1417 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1418 void setIsNoInline() {
1419 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1422 /// \brief Return true if the call can return twice
1423 bool canReturnTwice() const {
1424 return hasFnAttr(Attribute::ReturnsTwice);
1426 void setCanReturnTwice() {
1427 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1430 /// \brief Determine if the call does not access memory.
1431 bool doesNotAccessMemory() const {
1432 return hasFnAttr(Attribute::ReadNone);
1434 void setDoesNotAccessMemory() {
1435 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1438 /// \brief Determine if the call does not access or only reads memory.
1439 bool onlyReadsMemory() const {
1440 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1442 void setOnlyReadsMemory() {
1443 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1446 /// \brief Determine if the call cannot return.
1447 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1448 void setDoesNotReturn() {
1449 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1452 /// \brief Determine if the call cannot unwind.
1453 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1454 void setDoesNotThrow() {
1455 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1458 /// \brief Determine if the call cannot be duplicated.
1459 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1460 void setCannotDuplicate() {
1461 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1464 /// \brief Determine if the call returns a structure through first
1465 /// pointer argument.
1466 bool hasStructRetAttr() const {
1467 // Be friendly and also check the callee.
1468 return paramHasAttr(1, Attribute::StructRet);
1471 /// \brief Determine if any call argument is an aggregate passed by value.
1472 bool hasByValArgument() const {
1473 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1476 /// getCalledFunction - Return the function called, or null if this is an
1477 /// indirect function invocation.
1479 Function *getCalledFunction() const {
1480 return dyn_cast<Function>(Op<-1>());
1483 /// getCalledValue - Get a pointer to the function that is invoked by this
1485 const Value *getCalledValue() const { return Op<-1>(); }
1486 Value *getCalledValue() { return Op<-1>(); }
1488 /// setCalledFunction - Set the function called.
1489 void setCalledFunction(Value* Fn) {
1493 /// isInlineAsm - Check if this call is an inline asm statement.
1494 bool isInlineAsm() const {
1495 return isa<InlineAsm>(Op<-1>());
1498 // Methods for support type inquiry through isa, cast, and dyn_cast:
1499 static inline bool classof(const Instruction *I) {
1500 return I->getOpcode() == Instruction::Call;
1502 static inline bool classof(const Value *V) {
1503 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1507 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1509 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1510 // method so that subclasses cannot accidentally use it.
1511 void setInstructionSubclassData(unsigned short D) {
1512 Instruction::setInstructionSubclassData(D);
1517 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1520 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1521 const Twine &NameStr, BasicBlock *InsertAtEnd)
1522 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1523 ->getElementType())->getReturnType(),
1525 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1526 unsigned(Args.size() + 1), InsertAtEnd) {
1527 init(Func, Args, NameStr);
1530 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1531 const Twine &NameStr, Instruction *InsertBefore)
1532 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1533 ->getElementType())->getReturnType(),
1535 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1536 unsigned(Args.size() + 1), InsertBefore) {
1537 init(Func, Args, NameStr);
1541 // Note: if you get compile errors about private methods then
1542 // please update your code to use the high-level operand
1543 // interfaces. See line 943 above.
1544 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1546 //===----------------------------------------------------------------------===//
1548 //===----------------------------------------------------------------------===//
1550 /// SelectInst - This class represents the LLVM 'select' instruction.
1552 class SelectInst : public Instruction {
1553 void init(Value *C, Value *S1, Value *S2) {
1554 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1560 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1561 Instruction *InsertBefore)
1562 : Instruction(S1->getType(), Instruction::Select,
1563 &Op<0>(), 3, InsertBefore) {
1567 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1568 BasicBlock *InsertAtEnd)
1569 : Instruction(S1->getType(), Instruction::Select,
1570 &Op<0>(), 3, InsertAtEnd) {
1575 SelectInst *clone_impl() const override;
1577 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1578 const Twine &NameStr = "",
1579 Instruction *InsertBefore = nullptr) {
1580 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1582 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1583 const Twine &NameStr,
1584 BasicBlock *InsertAtEnd) {
1585 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1588 const Value *getCondition() const { return Op<0>(); }
1589 const Value *getTrueValue() const { return Op<1>(); }
1590 const Value *getFalseValue() const { return Op<2>(); }
1591 Value *getCondition() { return Op<0>(); }
1592 Value *getTrueValue() { return Op<1>(); }
1593 Value *getFalseValue() { return Op<2>(); }
1595 /// areInvalidOperands - Return a string if the specified operands are invalid
1596 /// for a select operation, otherwise return null.
1597 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1599 /// Transparently provide more efficient getOperand methods.
1600 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1602 OtherOps getOpcode() const {
1603 return static_cast<OtherOps>(Instruction::getOpcode());
1606 // Methods for support type inquiry through isa, cast, and dyn_cast:
1607 static inline bool classof(const Instruction *I) {
1608 return I->getOpcode() == Instruction::Select;
1610 static inline bool classof(const Value *V) {
1611 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1616 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1619 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1621 //===----------------------------------------------------------------------===//
1623 //===----------------------------------------------------------------------===//
1625 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1626 /// an argument of the specified type given a va_list and increments that list
1628 class VAArgInst : public UnaryInstruction {
1630 VAArgInst *clone_impl() const override;
1633 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1634 Instruction *InsertBefore = nullptr)
1635 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1638 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1639 BasicBlock *InsertAtEnd)
1640 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1644 Value *getPointerOperand() { return getOperand(0); }
1645 const Value *getPointerOperand() const { return getOperand(0); }
1646 static unsigned getPointerOperandIndex() { return 0U; }
1648 // Methods for support type inquiry through isa, cast, and dyn_cast:
1649 static inline bool classof(const Instruction *I) {
1650 return I->getOpcode() == VAArg;
1652 static inline bool classof(const Value *V) {
1653 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1657 //===----------------------------------------------------------------------===//
1658 // ExtractElementInst Class
1659 //===----------------------------------------------------------------------===//
1661 /// ExtractElementInst - This instruction extracts a single (scalar)
1662 /// element from a VectorType value
1664 class ExtractElementInst : public Instruction {
1665 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1666 Instruction *InsertBefore = nullptr);
1667 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1668 BasicBlock *InsertAtEnd);
1670 ExtractElementInst *clone_impl() const override;
1673 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1674 const Twine &NameStr = "",
1675 Instruction *InsertBefore = nullptr) {
1676 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1678 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1679 const Twine &NameStr,
1680 BasicBlock *InsertAtEnd) {
1681 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1684 /// isValidOperands - Return true if an extractelement instruction can be
1685 /// formed with the specified operands.
1686 static bool isValidOperands(const Value *Vec, const Value *Idx);
1688 Value *getVectorOperand() { return Op<0>(); }
1689 Value *getIndexOperand() { return Op<1>(); }
1690 const Value *getVectorOperand() const { return Op<0>(); }
1691 const Value *getIndexOperand() const { return Op<1>(); }
1693 VectorType *getVectorOperandType() const {
1694 return cast<VectorType>(getVectorOperand()->getType());
1698 /// Transparently provide more efficient getOperand methods.
1699 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1701 // Methods for support type inquiry through isa, cast, and dyn_cast:
1702 static inline bool classof(const Instruction *I) {
1703 return I->getOpcode() == Instruction::ExtractElement;
1705 static inline bool classof(const Value *V) {
1706 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1711 struct OperandTraits<ExtractElementInst> :
1712 public FixedNumOperandTraits<ExtractElementInst, 2> {
1715 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1717 //===----------------------------------------------------------------------===//
1718 // InsertElementInst Class
1719 //===----------------------------------------------------------------------===//
1721 /// InsertElementInst - This instruction inserts a single (scalar)
1722 /// element into a VectorType value
1724 class InsertElementInst : public Instruction {
1725 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1726 const Twine &NameStr = "",
1727 Instruction *InsertBefore = nullptr);
1728 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1729 const Twine &NameStr, BasicBlock *InsertAtEnd);
1731 InsertElementInst *clone_impl() const override;
1734 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1735 const Twine &NameStr = "",
1736 Instruction *InsertBefore = nullptr) {
1737 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1739 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1740 const Twine &NameStr,
1741 BasicBlock *InsertAtEnd) {
1742 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1745 /// isValidOperands - Return true if an insertelement instruction can be
1746 /// formed with the specified operands.
1747 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1750 /// getType - Overload to return most specific vector type.
1752 VectorType *getType() const {
1753 return cast<VectorType>(Instruction::getType());
1756 /// Transparently provide more efficient getOperand methods.
1757 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1759 // Methods for support type inquiry through isa, cast, and dyn_cast:
1760 static inline bool classof(const Instruction *I) {
1761 return I->getOpcode() == Instruction::InsertElement;
1763 static inline bool classof(const Value *V) {
1764 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1769 struct OperandTraits<InsertElementInst> :
1770 public FixedNumOperandTraits<InsertElementInst, 3> {
1773 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1775 //===----------------------------------------------------------------------===//
1776 // ShuffleVectorInst Class
1777 //===----------------------------------------------------------------------===//
1779 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1782 class ShuffleVectorInst : public Instruction {
1784 ShuffleVectorInst *clone_impl() const override;
1787 // allocate space for exactly three operands
1788 void *operator new(size_t s) {
1789 return User::operator new(s, 3);
1791 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1792 const Twine &NameStr = "",
1793 Instruction *InsertBefor = nullptr);
1794 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1795 const Twine &NameStr, BasicBlock *InsertAtEnd);
1797 /// isValidOperands - Return true if a shufflevector instruction can be
1798 /// formed with the specified operands.
1799 static bool isValidOperands(const Value *V1, const Value *V2,
1802 /// getType - Overload to return most specific vector type.
1804 VectorType *getType() const {
1805 return cast<VectorType>(Instruction::getType());
1808 /// Transparently provide more efficient getOperand methods.
1809 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1811 Constant *getMask() const {
1812 return cast<Constant>(getOperand(2));
1815 /// getMaskValue - Return the index from the shuffle mask for the specified
1816 /// output result. This is either -1 if the element is undef or a number less
1817 /// than 2*numelements.
1818 static int getMaskValue(Constant *Mask, unsigned i);
1820 int getMaskValue(unsigned i) const {
1821 return getMaskValue(getMask(), i);
1824 /// getShuffleMask - Return the full mask for this instruction, where each
1825 /// element is the element number and undef's are returned as -1.
1826 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1828 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1829 return getShuffleMask(getMask(), Result);
1832 SmallVector<int, 16> getShuffleMask() const {
1833 SmallVector<int, 16> Mask;
1834 getShuffleMask(Mask);
1839 // Methods for support type inquiry through isa, cast, and dyn_cast:
1840 static inline bool classof(const Instruction *I) {
1841 return I->getOpcode() == Instruction::ShuffleVector;
1843 static inline bool classof(const Value *V) {
1844 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1849 struct OperandTraits<ShuffleVectorInst> :
1850 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1853 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1855 //===----------------------------------------------------------------------===//
1856 // ExtractValueInst Class
1857 //===----------------------------------------------------------------------===//
1859 /// ExtractValueInst - This instruction extracts a struct member or array
1860 /// element value from an aggregate value.
1862 class ExtractValueInst : public UnaryInstruction {
1863 SmallVector<unsigned, 4> Indices;
1865 ExtractValueInst(const ExtractValueInst &EVI);
1866 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1868 /// Constructors - Create a extractvalue instruction with a base aggregate
1869 /// value and a list of indices. The first ctor can optionally insert before
1870 /// an existing instruction, the second appends the new instruction to the
1871 /// specified BasicBlock.
1872 inline ExtractValueInst(Value *Agg,
1873 ArrayRef<unsigned> Idxs,
1874 const Twine &NameStr,
1875 Instruction *InsertBefore);
1876 inline ExtractValueInst(Value *Agg,
1877 ArrayRef<unsigned> Idxs,
1878 const Twine &NameStr, BasicBlock *InsertAtEnd);
1880 // allocate space for exactly one operand
1881 void *operator new(size_t s) {
1882 return User::operator new(s, 1);
1885 ExtractValueInst *clone_impl() const override;
1888 static ExtractValueInst *Create(Value *Agg,
1889 ArrayRef<unsigned> Idxs,
1890 const Twine &NameStr = "",
1891 Instruction *InsertBefore = nullptr) {
1893 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1895 static ExtractValueInst *Create(Value *Agg,
1896 ArrayRef<unsigned> Idxs,
1897 const Twine &NameStr,
1898 BasicBlock *InsertAtEnd) {
1899 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1902 /// getIndexedType - Returns the type of the element that would be extracted
1903 /// with an extractvalue instruction with the specified parameters.
1905 /// Null is returned if the indices are invalid for the specified type.
1906 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1908 typedef const unsigned* idx_iterator;
1909 inline idx_iterator idx_begin() const { return Indices.begin(); }
1910 inline idx_iterator idx_end() const { return Indices.end(); }
1911 inline iterator_range<idx_iterator> indices() const {
1912 return iterator_range<idx_iterator>(idx_begin(), idx_end());
1915 Value *getAggregateOperand() {
1916 return getOperand(0);
1918 const Value *getAggregateOperand() const {
1919 return getOperand(0);
1921 static unsigned getAggregateOperandIndex() {
1922 return 0U; // get index for modifying correct operand
1925 ArrayRef<unsigned> getIndices() const {
1929 unsigned getNumIndices() const {
1930 return (unsigned)Indices.size();
1933 bool hasIndices() const {
1937 // Methods for support type inquiry through isa, cast, and dyn_cast:
1938 static inline bool classof(const Instruction *I) {
1939 return I->getOpcode() == Instruction::ExtractValue;
1941 static inline bool classof(const Value *V) {
1942 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1946 ExtractValueInst::ExtractValueInst(Value *Agg,
1947 ArrayRef<unsigned> Idxs,
1948 const Twine &NameStr,
1949 Instruction *InsertBefore)
1950 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1951 ExtractValue, Agg, InsertBefore) {
1952 init(Idxs, NameStr);
1954 ExtractValueInst::ExtractValueInst(Value *Agg,
1955 ArrayRef<unsigned> Idxs,
1956 const Twine &NameStr,
1957 BasicBlock *InsertAtEnd)
1958 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1959 ExtractValue, Agg, InsertAtEnd) {
1960 init(Idxs, NameStr);
1964 //===----------------------------------------------------------------------===//
1965 // InsertValueInst Class
1966 //===----------------------------------------------------------------------===//
1968 /// InsertValueInst - This instruction inserts a struct field of array element
1969 /// value into an aggregate value.
1971 class InsertValueInst : public Instruction {
1972 SmallVector<unsigned, 4> Indices;
1974 void *operator new(size_t, unsigned) = delete;
1975 InsertValueInst(const InsertValueInst &IVI);
1976 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1977 const Twine &NameStr);
1979 /// Constructors - Create a insertvalue instruction with a base aggregate
1980 /// value, a value to insert, and a list of indices. The first ctor can
1981 /// optionally insert before an existing instruction, the second appends
1982 /// the new instruction to the specified BasicBlock.
1983 inline InsertValueInst(Value *Agg, Value *Val,
1984 ArrayRef<unsigned> Idxs,
1985 const Twine &NameStr,
1986 Instruction *InsertBefore);
1987 inline InsertValueInst(Value *Agg, Value *Val,
1988 ArrayRef<unsigned> Idxs,
1989 const Twine &NameStr, BasicBlock *InsertAtEnd);
1991 /// Constructors - These two constructors are convenience methods because one
1992 /// and two index insertvalue instructions are so common.
1993 InsertValueInst(Value *Agg, Value *Val,
1994 unsigned Idx, const Twine &NameStr = "",
1995 Instruction *InsertBefore = nullptr);
1996 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1997 const Twine &NameStr, BasicBlock *InsertAtEnd);
1999 InsertValueInst *clone_impl() const override;
2001 // allocate space for exactly two operands
2002 void *operator new(size_t s) {
2003 return User::operator new(s, 2);
2006 static InsertValueInst *Create(Value *Agg, Value *Val,
2007 ArrayRef<unsigned> Idxs,
2008 const Twine &NameStr = "",
2009 Instruction *InsertBefore = nullptr) {
2010 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2012 static InsertValueInst *Create(Value *Agg, Value *Val,
2013 ArrayRef<unsigned> Idxs,
2014 const Twine &NameStr,
2015 BasicBlock *InsertAtEnd) {
2016 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2019 /// Transparently provide more efficient getOperand methods.
2020 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2022 typedef const unsigned* idx_iterator;
2023 inline idx_iterator idx_begin() const { return Indices.begin(); }
2024 inline idx_iterator idx_end() const { return Indices.end(); }
2025 inline iterator_range<idx_iterator> indices() const {
2026 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2029 Value *getAggregateOperand() {
2030 return getOperand(0);
2032 const Value *getAggregateOperand() const {
2033 return getOperand(0);
2035 static unsigned getAggregateOperandIndex() {
2036 return 0U; // get index for modifying correct operand
2039 Value *getInsertedValueOperand() {
2040 return getOperand(1);
2042 const Value *getInsertedValueOperand() const {
2043 return getOperand(1);
2045 static unsigned getInsertedValueOperandIndex() {
2046 return 1U; // get index for modifying correct operand
2049 ArrayRef<unsigned> getIndices() const {
2053 unsigned getNumIndices() const {
2054 return (unsigned)Indices.size();
2057 bool hasIndices() const {
2061 // Methods for support type inquiry through isa, cast, and dyn_cast:
2062 static inline bool classof(const Instruction *I) {
2063 return I->getOpcode() == Instruction::InsertValue;
2065 static inline bool classof(const Value *V) {
2066 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2071 struct OperandTraits<InsertValueInst> :
2072 public FixedNumOperandTraits<InsertValueInst, 2> {
2075 InsertValueInst::InsertValueInst(Value *Agg,
2077 ArrayRef<unsigned> Idxs,
2078 const Twine &NameStr,
2079 Instruction *InsertBefore)
2080 : Instruction(Agg->getType(), InsertValue,
2081 OperandTraits<InsertValueInst>::op_begin(this),
2083 init(Agg, Val, Idxs, NameStr);
2085 InsertValueInst::InsertValueInst(Value *Agg,
2087 ArrayRef<unsigned> Idxs,
2088 const Twine &NameStr,
2089 BasicBlock *InsertAtEnd)
2090 : Instruction(Agg->getType(), InsertValue,
2091 OperandTraits<InsertValueInst>::op_begin(this),
2093 init(Agg, Val, Idxs, NameStr);
2096 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2098 //===----------------------------------------------------------------------===//
2100 //===----------------------------------------------------------------------===//
2102 // PHINode - The PHINode class is used to represent the magical mystical PHI
2103 // node, that can not exist in nature, but can be synthesized in a computer
2104 // scientist's overactive imagination.
2106 class PHINode : public Instruction {
2107 void *operator new(size_t, unsigned) = delete;
2108 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2109 /// the number actually in use.
2110 unsigned ReservedSpace;
2111 PHINode(const PHINode &PN);
2112 // allocate space for exactly zero operands
2113 void *operator new(size_t s) {
2114 return User::operator new(s, 0);
2116 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2117 const Twine &NameStr = "",
2118 Instruction *InsertBefore = nullptr)
2119 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2120 ReservedSpace(NumReservedValues) {
2122 OperandList = allocHungoffUses(ReservedSpace);
2125 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2126 BasicBlock *InsertAtEnd)
2127 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2128 ReservedSpace(NumReservedValues) {
2130 OperandList = allocHungoffUses(ReservedSpace);
2133 // allocHungoffUses - this is more complicated than the generic
2134 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2135 // values and pointers to the incoming blocks, all in one allocation.
2136 Use *allocHungoffUses(unsigned) const;
2138 PHINode *clone_impl() const override;
2140 /// Constructors - NumReservedValues is a hint for the number of incoming
2141 /// edges that this phi node will have (use 0 if you really have no idea).
2142 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2143 const Twine &NameStr = "",
2144 Instruction *InsertBefore = nullptr) {
2145 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2147 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2148 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2149 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2153 /// Provide fast operand accessors
2154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2156 // Block iterator interface. This provides access to the list of incoming
2157 // basic blocks, which parallels the list of incoming values.
2159 typedef BasicBlock **block_iterator;
2160 typedef BasicBlock * const *const_block_iterator;
2162 block_iterator block_begin() {
2164 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2165 return reinterpret_cast<block_iterator>(ref + 1);
2168 const_block_iterator block_begin() const {
2169 const Use::UserRef *ref =
2170 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2171 return reinterpret_cast<const_block_iterator>(ref + 1);
2174 block_iterator block_end() {
2175 return block_begin() + getNumOperands();
2178 const_block_iterator block_end() const {
2179 return block_begin() + getNumOperands();
2182 op_range incoming_values() { return operands(); }
2184 /// getNumIncomingValues - Return the number of incoming edges
2186 unsigned getNumIncomingValues() const { return getNumOperands(); }
2188 /// getIncomingValue - Return incoming value number x
2190 Value *getIncomingValue(unsigned i) const {
2191 return getOperand(i);
2193 void setIncomingValue(unsigned i, Value *V) {
2196 static unsigned getOperandNumForIncomingValue(unsigned i) {
2199 static unsigned getIncomingValueNumForOperand(unsigned i) {
2203 /// getIncomingBlock - Return incoming basic block number @p i.
2205 BasicBlock *getIncomingBlock(unsigned i) const {
2206 return block_begin()[i];
2209 /// getIncomingBlock - Return incoming basic block corresponding
2210 /// to an operand of the PHI.
2212 BasicBlock *getIncomingBlock(const Use &U) const {
2213 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2214 return getIncomingBlock(unsigned(&U - op_begin()));
2217 /// getIncomingBlock - Return incoming basic block corresponding
2218 /// to value use iterator.
2220 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2221 return getIncomingBlock(I.getUse());
2224 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2225 block_begin()[i] = BB;
2228 /// addIncoming - Add an incoming value to the end of the PHI list
2230 void addIncoming(Value *V, BasicBlock *BB) {
2231 assert(V && "PHI node got a null value!");
2232 assert(BB && "PHI node got a null basic block!");
2233 assert(getType() == V->getType() &&
2234 "All operands to PHI node must be the same type as the PHI node!");
2235 if (NumOperands == ReservedSpace)
2236 growOperands(); // Get more space!
2237 // Initialize some new operands.
2239 setIncomingValue(NumOperands - 1, V);
2240 setIncomingBlock(NumOperands - 1, BB);
2243 /// removeIncomingValue - Remove an incoming value. This is useful if a
2244 /// predecessor basic block is deleted. The value removed is returned.
2246 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2247 /// is true), the PHI node is destroyed and any uses of it are replaced with
2248 /// dummy values. The only time there should be zero incoming values to a PHI
2249 /// node is when the block is dead, so this strategy is sound.
2251 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2253 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2254 int Idx = getBasicBlockIndex(BB);
2255 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2256 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2259 /// getBasicBlockIndex - Return the first index of the specified basic
2260 /// block in the value list for this PHI. Returns -1 if no instance.
2262 int getBasicBlockIndex(const BasicBlock *BB) const {
2263 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2264 if (block_begin()[i] == BB)
2269 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2270 int Idx = getBasicBlockIndex(BB);
2271 assert(Idx >= 0 && "Invalid basic block argument!");
2272 return getIncomingValue(Idx);
2275 /// hasConstantValue - If the specified PHI node always merges together the
2276 /// same value, return the value, otherwise return null.
2277 Value *hasConstantValue() const;
2279 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2280 static inline bool classof(const Instruction *I) {
2281 return I->getOpcode() == Instruction::PHI;
2283 static inline bool classof(const Value *V) {
2284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2287 void growOperands();
2291 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2294 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2296 //===----------------------------------------------------------------------===//
2297 // LandingPadInst Class
2298 //===----------------------------------------------------------------------===//
2300 //===---------------------------------------------------------------------------
2301 /// LandingPadInst - The landingpad instruction holds all of the information
2302 /// necessary to generate correct exception handling. The landingpad instruction
2303 /// cannot be moved from the top of a landing pad block, which itself is
2304 /// accessible only from the 'unwind' edge of an invoke. This uses the
2305 /// SubclassData field in Value to store whether or not the landingpad is a
2308 class LandingPadInst : public Instruction {
2309 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2310 /// the number actually in use.
2311 unsigned ReservedSpace;
2312 LandingPadInst(const LandingPadInst &LP);
2314 enum ClauseType { Catch, Filter };
2316 void *operator new(size_t, unsigned) = delete;
2317 // Allocate space for exactly zero operands.
2318 void *operator new(size_t s) {
2319 return User::operator new(s, 0);
2321 void growOperands(unsigned Size);
2322 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2324 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2325 unsigned NumReservedValues, const Twine &NameStr,
2326 Instruction *InsertBefore);
2327 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2328 unsigned NumReservedValues, const Twine &NameStr,
2329 BasicBlock *InsertAtEnd);
2331 LandingPadInst *clone_impl() const override;
2333 /// Constructors - NumReservedClauses is a hint for the number of incoming
2334 /// clauses that this landingpad will have (use 0 if you really have no idea).
2335 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2336 unsigned NumReservedClauses,
2337 const Twine &NameStr = "",
2338 Instruction *InsertBefore = nullptr);
2339 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2340 unsigned NumReservedClauses,
2341 const Twine &NameStr, BasicBlock *InsertAtEnd);
2344 /// Provide fast operand accessors
2345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2347 /// getPersonalityFn - Get the personality function associated with this
2349 Value *getPersonalityFn() const { return getOperand(0); }
2351 /// isCleanup - Return 'true' if this landingpad instruction is a
2352 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2353 /// doesn't catch the exception.
2354 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2356 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2357 void setCleanup(bool V) {
2358 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2362 /// Add a catch or filter clause to the landing pad.
2363 void addClause(Constant *ClauseVal);
2365 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2366 /// determine what type of clause this is.
2367 Constant *getClause(unsigned Idx) const {
2368 return cast<Constant>(OperandList[Idx + 1]);
2371 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2372 bool isCatch(unsigned Idx) const {
2373 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2376 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2377 bool isFilter(unsigned Idx) const {
2378 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2381 /// getNumClauses - Get the number of clauses for this landing pad.
2382 unsigned getNumClauses() const { return getNumOperands() - 1; }
2384 /// reserveClauses - Grow the size of the operand list to accommodate the new
2385 /// number of clauses.
2386 void reserveClauses(unsigned Size) { growOperands(Size); }
2388 // Methods for support type inquiry through isa, cast, and dyn_cast:
2389 static inline bool classof(const Instruction *I) {
2390 return I->getOpcode() == Instruction::LandingPad;
2392 static inline bool classof(const Value *V) {
2393 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2398 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2401 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2403 //===----------------------------------------------------------------------===//
2405 //===----------------------------------------------------------------------===//
2407 //===---------------------------------------------------------------------------
2408 /// ReturnInst - Return a value (possibly void), from a function. Execution
2409 /// does not continue in this function any longer.
2411 class ReturnInst : public TerminatorInst {
2412 ReturnInst(const ReturnInst &RI);
2415 // ReturnInst constructors:
2416 // ReturnInst() - 'ret void' instruction
2417 // ReturnInst( null) - 'ret void' instruction
2418 // ReturnInst(Value* X) - 'ret X' instruction
2419 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2420 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2421 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2422 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2424 // NOTE: If the Value* passed is of type void then the constructor behaves as
2425 // if it was passed NULL.
2426 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2427 Instruction *InsertBefore = nullptr);
2428 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2429 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2431 ReturnInst *clone_impl() const override;
2433 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2434 Instruction *InsertBefore = nullptr) {
2435 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2437 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2438 BasicBlock *InsertAtEnd) {
2439 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2441 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2442 return new(0) ReturnInst(C, InsertAtEnd);
2444 virtual ~ReturnInst();
2446 /// Provide fast operand accessors
2447 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2449 /// Convenience accessor. Returns null if there is no return value.
2450 Value *getReturnValue() const {
2451 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2454 unsigned getNumSuccessors() const { return 0; }
2456 // Methods for support type inquiry through isa, cast, and dyn_cast:
2457 static inline bool classof(const Instruction *I) {
2458 return (I->getOpcode() == Instruction::Ret);
2460 static inline bool classof(const Value *V) {
2461 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2464 BasicBlock *getSuccessorV(unsigned idx) const override;
2465 unsigned getNumSuccessorsV() const override;
2466 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2470 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2473 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2475 //===----------------------------------------------------------------------===//
2477 //===----------------------------------------------------------------------===//
2479 //===---------------------------------------------------------------------------
2480 /// BranchInst - Conditional or Unconditional Branch instruction.
2482 class BranchInst : public TerminatorInst {
2483 /// Ops list - Branches are strange. The operands are ordered:
2484 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2485 /// they don't have to check for cond/uncond branchness. These are mostly
2486 /// accessed relative from op_end().
2487 BranchInst(const BranchInst &BI);
2489 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2490 // BranchInst(BB *B) - 'br B'
2491 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2492 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2493 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2494 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2495 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2496 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2497 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2498 Instruction *InsertBefore = nullptr);
2499 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2500 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2501 BasicBlock *InsertAtEnd);
2503 BranchInst *clone_impl() const override;
2505 static BranchInst *Create(BasicBlock *IfTrue,
2506 Instruction *InsertBefore = nullptr) {
2507 return new(1) BranchInst(IfTrue, InsertBefore);
2509 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2510 Value *Cond, Instruction *InsertBefore = nullptr) {
2511 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2513 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2514 return new(1) BranchInst(IfTrue, InsertAtEnd);
2516 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2517 Value *Cond, BasicBlock *InsertAtEnd) {
2518 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2521 /// Transparently provide more efficient getOperand methods.
2522 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2524 bool isUnconditional() const { return getNumOperands() == 1; }
2525 bool isConditional() const { return getNumOperands() == 3; }
2527 Value *getCondition() const {
2528 assert(isConditional() && "Cannot get condition of an uncond branch!");
2532 void setCondition(Value *V) {
2533 assert(isConditional() && "Cannot set condition of unconditional branch!");
2537 unsigned getNumSuccessors() const { return 1+isConditional(); }
2539 BasicBlock *getSuccessor(unsigned i) const {
2540 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2541 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2544 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2545 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2546 *(&Op<-1>() - idx) = (Value*)NewSucc;
2549 /// \brief Swap the successors of this branch instruction.
2551 /// Swaps the successors of the branch instruction. This also swaps any
2552 /// branch weight metadata associated with the instruction so that it
2553 /// continues to map correctly to each operand.
2554 void swapSuccessors();
2556 // Methods for support type inquiry through isa, cast, and dyn_cast:
2557 static inline bool classof(const Instruction *I) {
2558 return (I->getOpcode() == Instruction::Br);
2560 static inline bool classof(const Value *V) {
2561 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2564 BasicBlock *getSuccessorV(unsigned idx) const override;
2565 unsigned getNumSuccessorsV() const override;
2566 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2570 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2573 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2575 //===----------------------------------------------------------------------===//
2577 //===----------------------------------------------------------------------===//
2579 //===---------------------------------------------------------------------------
2580 /// SwitchInst - Multiway switch
2582 class SwitchInst : public TerminatorInst {
2583 void *operator new(size_t, unsigned) = delete;
2584 unsigned ReservedSpace;
2585 // Operand[0] = Value to switch on
2586 // Operand[1] = Default basic block destination
2587 // Operand[2n ] = Value to match
2588 // Operand[2n+1] = BasicBlock to go to on match
2589 SwitchInst(const SwitchInst &SI);
2590 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2591 void growOperands();
2592 // allocate space for exactly zero operands
2593 void *operator new(size_t s) {
2594 return User::operator new(s, 0);
2596 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2597 /// switch on and a default destination. The number of additional cases can
2598 /// be specified here to make memory allocation more efficient. This
2599 /// constructor can also autoinsert before another instruction.
2600 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2601 Instruction *InsertBefore);
2603 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2604 /// switch on and a default destination. The number of additional cases can
2605 /// be specified here to make memory allocation more efficient. This
2606 /// constructor also autoinserts at the end of the specified BasicBlock.
2607 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2608 BasicBlock *InsertAtEnd);
2610 SwitchInst *clone_impl() const override;
2614 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2616 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2617 class CaseIteratorT {
2625 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2627 /// Initializes case iterator for given SwitchInst and for given
2629 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2634 /// Initializes case iterator for given SwitchInst and for given
2635 /// TerminatorInst's successor index.
2636 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2637 assert(SuccessorIndex < SI->getNumSuccessors() &&
2638 "Successor index # out of range!");
2639 return SuccessorIndex != 0 ?
2640 Self(SI, SuccessorIndex - 1) :
2641 Self(SI, DefaultPseudoIndex);
2644 /// Resolves case value for current case.
2645 ConstantIntTy *getCaseValue() {
2646 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2647 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2650 /// Resolves successor for current case.
2651 BasicBlockTy *getCaseSuccessor() {
2652 assert((Index < SI->getNumCases() ||
2653 Index == DefaultPseudoIndex) &&
2654 "Index out the number of cases.");
2655 return SI->getSuccessor(getSuccessorIndex());
2658 /// Returns number of current case.
2659 unsigned getCaseIndex() const { return Index; }
2661 /// Returns TerminatorInst's successor index for current case successor.
2662 unsigned getSuccessorIndex() const {
2663 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2664 "Index out the number of cases.");
2665 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2669 // Check index correctness after increment.
2670 // Note: Index == getNumCases() means end().
2671 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2675 Self operator++(int) {
2681 // Check index correctness after decrement.
2682 // Note: Index == getNumCases() means end().
2683 // Also allow "-1" iterator here. That will became valid after ++.
2684 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2685 "Index out the number of cases.");
2689 Self operator--(int) {
2694 bool operator==(const Self& RHS) const {
2695 assert(RHS.SI == SI && "Incompatible operators.");
2696 return RHS.Index == Index;
2698 bool operator!=(const Self& RHS) const {
2699 assert(RHS.SI == SI && "Incompatible operators.");
2700 return RHS.Index != Index;
2707 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2710 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2712 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2716 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2717 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2719 /// Sets the new value for current case.
2720 void setValue(ConstantInt *V) {
2721 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2722 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2725 /// Sets the new successor for current case.
2726 void setSuccessor(BasicBlock *S) {
2727 SI->setSuccessor(getSuccessorIndex(), S);
2731 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2733 Instruction *InsertBefore = nullptr) {
2734 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2736 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2737 unsigned NumCases, BasicBlock *InsertAtEnd) {
2738 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2743 /// Provide fast operand accessors
2744 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2746 // Accessor Methods for Switch stmt
2747 Value *getCondition() const { return getOperand(0); }
2748 void setCondition(Value *V) { setOperand(0, V); }
2750 BasicBlock *getDefaultDest() const {
2751 return cast<BasicBlock>(getOperand(1));
2754 void setDefaultDest(BasicBlock *DefaultCase) {
2755 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2758 /// getNumCases - return the number of 'cases' in this switch instruction,
2759 /// except the default case
2760 unsigned getNumCases() const {
2761 return getNumOperands()/2 - 1;
2764 /// Returns a read/write iterator that points to the first
2765 /// case in SwitchInst.
2766 CaseIt case_begin() {
2767 return CaseIt(this, 0);
2769 /// Returns a read-only iterator that points to the first
2770 /// case in the SwitchInst.
2771 ConstCaseIt case_begin() const {
2772 return ConstCaseIt(this, 0);
2775 /// Returns a read/write iterator that points one past the last
2776 /// in the SwitchInst.
2778 return CaseIt(this, getNumCases());
2780 /// Returns a read-only iterator that points one past the last
2781 /// in the SwitchInst.
2782 ConstCaseIt case_end() const {
2783 return ConstCaseIt(this, getNumCases());
2786 /// cases - iteration adapter for range-for loops.
2787 iterator_range<CaseIt> cases() {
2788 return iterator_range<CaseIt>(case_begin(), case_end());
2791 /// cases - iteration adapter for range-for loops.
2792 iterator_range<ConstCaseIt> cases() const {
2793 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2796 /// Returns an iterator that points to the default case.
2797 /// Note: this iterator allows to resolve successor only. Attempt
2798 /// to resolve case value causes an assertion.
2799 /// Also note, that increment and decrement also causes an assertion and
2800 /// makes iterator invalid.
2801 CaseIt case_default() {
2802 return CaseIt(this, DefaultPseudoIndex);
2804 ConstCaseIt case_default() const {
2805 return ConstCaseIt(this, DefaultPseudoIndex);
2808 /// findCaseValue - Search all of the case values for the specified constant.
2809 /// If it is explicitly handled, return the case iterator of it, otherwise
2810 /// return default case iterator to indicate
2811 /// that it is handled by the default handler.
2812 CaseIt findCaseValue(const ConstantInt *C) {
2813 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2814 if (i.getCaseValue() == C)
2816 return case_default();
2818 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2819 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2820 if (i.getCaseValue() == C)
2822 return case_default();
2825 /// findCaseDest - Finds the unique case value for a given successor. Returns
2826 /// null if the successor is not found, not unique, or is the default case.
2827 ConstantInt *findCaseDest(BasicBlock *BB) {
2828 if (BB == getDefaultDest()) return nullptr;
2830 ConstantInt *CI = nullptr;
2831 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2832 if (i.getCaseSuccessor() == BB) {
2833 if (CI) return nullptr; // Multiple cases lead to BB.
2834 else CI = i.getCaseValue();
2840 /// addCase - Add an entry to the switch instruction...
2842 /// This action invalidates case_end(). Old case_end() iterator will
2843 /// point to the added case.
2844 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2846 /// removeCase - This method removes the specified case and its successor
2847 /// from the switch instruction. Note that this operation may reorder the
2848 /// remaining cases at index idx and above.
2850 /// This action invalidates iterators for all cases following the one removed,
2851 /// including the case_end() iterator.
2852 void removeCase(CaseIt i);
2854 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2855 BasicBlock *getSuccessor(unsigned idx) const {
2856 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2857 return cast<BasicBlock>(getOperand(idx*2+1));
2859 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2860 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2861 setOperand(idx*2+1, (Value*)NewSucc);
2864 // Methods for support type inquiry through isa, cast, and dyn_cast:
2865 static inline bool classof(const Instruction *I) {
2866 return I->getOpcode() == Instruction::Switch;
2868 static inline bool classof(const Value *V) {
2869 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2872 BasicBlock *getSuccessorV(unsigned idx) const override;
2873 unsigned getNumSuccessorsV() const override;
2874 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2878 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2881 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2884 //===----------------------------------------------------------------------===//
2885 // IndirectBrInst Class
2886 //===----------------------------------------------------------------------===//
2888 //===---------------------------------------------------------------------------
2889 /// IndirectBrInst - Indirect Branch Instruction.
2891 class IndirectBrInst : public TerminatorInst {
2892 void *operator new(size_t, unsigned) = delete;
2893 unsigned ReservedSpace;
2894 // Operand[0] = Value to switch on
2895 // Operand[1] = Default basic block destination
2896 // Operand[2n ] = Value to match
2897 // Operand[2n+1] = BasicBlock to go to on match
2898 IndirectBrInst(const IndirectBrInst &IBI);
2899 void init(Value *Address, unsigned NumDests);
2900 void growOperands();
2901 // allocate space for exactly zero operands
2902 void *operator new(size_t s) {
2903 return User::operator new(s, 0);
2905 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2906 /// Address to jump to. The number of expected destinations can be specified
2907 /// here to make memory allocation more efficient. This constructor can also
2908 /// autoinsert before another instruction.
2909 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2911 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2912 /// Address to jump to. The number of expected destinations can be specified
2913 /// here to make memory allocation more efficient. This constructor also
2914 /// autoinserts at the end of the specified BasicBlock.
2915 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2917 IndirectBrInst *clone_impl() const override;
2919 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2920 Instruction *InsertBefore = nullptr) {
2921 return new IndirectBrInst(Address, NumDests, InsertBefore);
2923 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2924 BasicBlock *InsertAtEnd) {
2925 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2929 /// Provide fast operand accessors.
2930 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2932 // Accessor Methods for IndirectBrInst instruction.
2933 Value *getAddress() { return getOperand(0); }
2934 const Value *getAddress() const { return getOperand(0); }
2935 void setAddress(Value *V) { setOperand(0, V); }
2938 /// getNumDestinations - return the number of possible destinations in this
2939 /// indirectbr instruction.
2940 unsigned getNumDestinations() const { return getNumOperands()-1; }
2942 /// getDestination - Return the specified destination.
2943 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2944 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2946 /// addDestination - Add a destination.
2948 void addDestination(BasicBlock *Dest);
2950 /// removeDestination - This method removes the specified successor from the
2951 /// indirectbr instruction.
2952 void removeDestination(unsigned i);
2954 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2955 BasicBlock *getSuccessor(unsigned i) const {
2956 return cast<BasicBlock>(getOperand(i+1));
2958 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2959 setOperand(i+1, (Value*)NewSucc);
2962 // Methods for support type inquiry through isa, cast, and dyn_cast:
2963 static inline bool classof(const Instruction *I) {
2964 return I->getOpcode() == Instruction::IndirectBr;
2966 static inline bool classof(const Value *V) {
2967 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2970 BasicBlock *getSuccessorV(unsigned idx) const override;
2971 unsigned getNumSuccessorsV() const override;
2972 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2976 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2979 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2982 //===----------------------------------------------------------------------===//
2984 //===----------------------------------------------------------------------===//
2986 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2987 /// calling convention of the call.
2989 class InvokeInst : public TerminatorInst {
2990 AttributeSet AttributeList;
2991 InvokeInst(const InvokeInst &BI);
2992 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2993 ArrayRef<Value *> Args, const Twine &NameStr);
2995 /// Construct an InvokeInst given a range of arguments.
2997 /// \brief Construct an InvokeInst from a range of arguments
2998 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2999 ArrayRef<Value *> Args, unsigned Values,
3000 const Twine &NameStr, Instruction *InsertBefore);
3002 /// Construct an InvokeInst given a range of arguments.
3004 /// \brief Construct an InvokeInst from a range of arguments
3005 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3006 ArrayRef<Value *> Args, unsigned Values,
3007 const Twine &NameStr, BasicBlock *InsertAtEnd);
3009 InvokeInst *clone_impl() const override;
3011 static InvokeInst *Create(Value *Func,
3012 BasicBlock *IfNormal, BasicBlock *IfException,
3013 ArrayRef<Value *> Args, const Twine &NameStr = "",
3014 Instruction *InsertBefore = nullptr) {
3015 unsigned Values = unsigned(Args.size()) + 3;
3016 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3017 Values, NameStr, InsertBefore);
3019 static InvokeInst *Create(Value *Func,
3020 BasicBlock *IfNormal, BasicBlock *IfException,
3021 ArrayRef<Value *> Args, const Twine &NameStr,
3022 BasicBlock *InsertAtEnd) {
3023 unsigned Values = unsigned(Args.size()) + 3;
3024 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3025 Values, NameStr, InsertAtEnd);
3028 /// Provide fast operand accessors
3029 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3031 /// getNumArgOperands - Return the number of invoke arguments.
3033 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3035 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3037 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3038 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3040 /// arg_operands - iteration adapter for range-for loops.
3041 iterator_range<op_iterator> arg_operands() {
3042 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3045 /// arg_operands - iteration adapter for range-for loops.
3046 iterator_range<const_op_iterator> arg_operands() const {
3047 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3050 /// \brief Wrappers for getting the \c Use of a invoke argument.
3051 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3052 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3054 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3056 CallingConv::ID getCallingConv() const {
3057 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3059 void setCallingConv(CallingConv::ID CC) {
3060 setInstructionSubclassData(static_cast<unsigned>(CC));
3063 /// getAttributes - Return the parameter attributes for this invoke.
3065 const AttributeSet &getAttributes() const { return AttributeList; }
3067 /// setAttributes - Set the parameter attributes for this invoke.
3069 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3071 /// addAttribute - adds the attribute to the list of attributes.
3072 void addAttribute(unsigned i, Attribute::AttrKind attr);
3074 /// removeAttribute - removes the attribute from the list of attributes.
3075 void removeAttribute(unsigned i, Attribute attr);
3077 /// \brief removes the dereferenceable attribute to the list of attributes.
3078 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3080 /// \brief Determine whether this call has the given attribute.
3081 bool hasFnAttr(Attribute::AttrKind A) const {
3082 assert(A != Attribute::NoBuiltin &&
3083 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3084 return hasFnAttrImpl(A);
3087 /// \brief Determine whether the call or the callee has the given attributes.
3088 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3090 /// \brief Extract the alignment for a call or parameter (0=unknown).
3091 unsigned getParamAlignment(unsigned i) const {
3092 return AttributeList.getParamAlignment(i);
3095 /// \brief Extract the number of dereferenceable bytes for a call or
3096 /// parameter (0=unknown).
3097 uint64_t getDereferenceableBytes(unsigned i) const {
3098 return AttributeList.getDereferenceableBytes(i);
3101 /// \brief Return true if the call should not be treated as a call to a
3103 bool isNoBuiltin() const {
3104 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3105 // to check it by hand.
3106 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3107 !hasFnAttrImpl(Attribute::Builtin);
3110 /// \brief Return true if the call should not be inlined.
3111 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3112 void setIsNoInline() {
3113 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3116 /// \brief Determine if the call does not access memory.
3117 bool doesNotAccessMemory() const {
3118 return hasFnAttr(Attribute::ReadNone);
3120 void setDoesNotAccessMemory() {
3121 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3124 /// \brief Determine if the call does not access or only reads memory.
3125 bool onlyReadsMemory() const {
3126 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3128 void setOnlyReadsMemory() {
3129 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3132 /// \brief Determine if the call cannot return.
3133 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3134 void setDoesNotReturn() {
3135 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3138 /// \brief Determine if the call cannot unwind.
3139 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3140 void setDoesNotThrow() {
3141 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3144 /// \brief Determine if the invoke cannot be duplicated.
3145 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3146 void setCannotDuplicate() {
3147 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3150 /// \brief Determine if the call returns a structure through first
3151 /// pointer argument.
3152 bool hasStructRetAttr() const {
3153 // Be friendly and also check the callee.
3154 return paramHasAttr(1, Attribute::StructRet);
3157 /// \brief Determine if any call argument is an aggregate passed by value.
3158 bool hasByValArgument() const {
3159 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3162 /// getCalledFunction - Return the function called, or null if this is an
3163 /// indirect function invocation.
3165 Function *getCalledFunction() const {
3166 return dyn_cast<Function>(Op<-3>());
3169 /// getCalledValue - Get a pointer to the function that is invoked by this
3171 const Value *getCalledValue() const { return Op<-3>(); }
3172 Value *getCalledValue() { return Op<-3>(); }
3174 /// setCalledFunction - Set the function called.
3175 void setCalledFunction(Value* Fn) {
3179 // get*Dest - Return the destination basic blocks...
3180 BasicBlock *getNormalDest() const {
3181 return cast<BasicBlock>(Op<-2>());
3183 BasicBlock *getUnwindDest() const {
3184 return cast<BasicBlock>(Op<-1>());
3186 void setNormalDest(BasicBlock *B) {
3187 Op<-2>() = reinterpret_cast<Value*>(B);
3189 void setUnwindDest(BasicBlock *B) {
3190 Op<-1>() = reinterpret_cast<Value*>(B);
3193 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3194 /// block (the unwind destination).
3195 LandingPadInst *getLandingPadInst() const;
3197 BasicBlock *getSuccessor(unsigned i) const {
3198 assert(i < 2 && "Successor # out of range for invoke!");
3199 return i == 0 ? getNormalDest() : getUnwindDest();
3202 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3203 assert(idx < 2 && "Successor # out of range for invoke!");
3204 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3207 unsigned getNumSuccessors() const { return 2; }
3209 // Methods for support type inquiry through isa, cast, and dyn_cast:
3210 static inline bool classof(const Instruction *I) {
3211 return (I->getOpcode() == Instruction::Invoke);
3213 static inline bool classof(const Value *V) {
3214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3218 BasicBlock *getSuccessorV(unsigned idx) const override;
3219 unsigned getNumSuccessorsV() const override;
3220 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3222 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3224 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3225 // method so that subclasses cannot accidentally use it.
3226 void setInstructionSubclassData(unsigned short D) {
3227 Instruction::setInstructionSubclassData(D);
3232 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3235 InvokeInst::InvokeInst(Value *Func,
3236 BasicBlock *IfNormal, BasicBlock *IfException,
3237 ArrayRef<Value *> Args, unsigned Values,
3238 const Twine &NameStr, Instruction *InsertBefore)
3239 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3240 ->getElementType())->getReturnType(),
3241 Instruction::Invoke,
3242 OperandTraits<InvokeInst>::op_end(this) - Values,
3243 Values, InsertBefore) {
3244 init(Func, IfNormal, IfException, Args, NameStr);
3246 InvokeInst::InvokeInst(Value *Func,
3247 BasicBlock *IfNormal, BasicBlock *IfException,
3248 ArrayRef<Value *> Args, unsigned Values,
3249 const Twine &NameStr, BasicBlock *InsertAtEnd)
3250 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3251 ->getElementType())->getReturnType(),
3252 Instruction::Invoke,
3253 OperandTraits<InvokeInst>::op_end(this) - Values,
3254 Values, InsertAtEnd) {
3255 init(Func, IfNormal, IfException, Args, NameStr);
3258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3260 //===----------------------------------------------------------------------===//
3262 //===----------------------------------------------------------------------===//
3264 //===---------------------------------------------------------------------------
3265 /// ResumeInst - Resume the propagation of an exception.
3267 class ResumeInst : public TerminatorInst {
3268 ResumeInst(const ResumeInst &RI);
3270 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3271 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3273 ResumeInst *clone_impl() const override;
3275 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3276 return new(1) ResumeInst(Exn, InsertBefore);
3278 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3279 return new(1) ResumeInst(Exn, InsertAtEnd);
3282 /// Provide fast operand accessors
3283 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3285 /// Convenience accessor.
3286 Value *getValue() const { return Op<0>(); }
3288 unsigned getNumSuccessors() const { return 0; }
3290 // Methods for support type inquiry through isa, cast, and dyn_cast:
3291 static inline bool classof(const Instruction *I) {
3292 return I->getOpcode() == Instruction::Resume;
3294 static inline bool classof(const Value *V) {
3295 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3298 BasicBlock *getSuccessorV(unsigned idx) const override;
3299 unsigned getNumSuccessorsV() const override;
3300 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3304 struct OperandTraits<ResumeInst> :
3305 public FixedNumOperandTraits<ResumeInst, 1> {
3308 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3310 //===----------------------------------------------------------------------===//
3311 // UnreachableInst Class
3312 //===----------------------------------------------------------------------===//
3314 //===---------------------------------------------------------------------------
3315 /// UnreachableInst - This function has undefined behavior. In particular, the
3316 /// presence of this instruction indicates some higher level knowledge that the
3317 /// end of the block cannot be reached.
3319 class UnreachableInst : public TerminatorInst {
3320 void *operator new(size_t, unsigned) = delete;
3322 UnreachableInst *clone_impl() const override;
3325 // allocate space for exactly zero operands
3326 void *operator new(size_t s) {
3327 return User::operator new(s, 0);
3329 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3330 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3332 unsigned getNumSuccessors() const { return 0; }
3334 // Methods for support type inquiry through isa, cast, and dyn_cast:
3335 static inline bool classof(const Instruction *I) {
3336 return I->getOpcode() == Instruction::Unreachable;
3338 static inline bool classof(const Value *V) {
3339 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3342 BasicBlock *getSuccessorV(unsigned idx) const override;
3343 unsigned getNumSuccessorsV() const override;
3344 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3347 //===----------------------------------------------------------------------===//
3349 //===----------------------------------------------------------------------===//
3351 /// \brief This class represents a truncation of integer types.
3352 class TruncInst : public CastInst {
3354 /// \brief Clone an identical TruncInst
3355 TruncInst *clone_impl() const override;
3358 /// \brief Constructor with insert-before-instruction semantics
3360 Value *S, ///< The value to be truncated
3361 Type *Ty, ///< The (smaller) type to truncate to
3362 const Twine &NameStr = "", ///< A name for the new instruction
3363 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3366 /// \brief Constructor with insert-at-end-of-block semantics
3368 Value *S, ///< The value to be truncated
3369 Type *Ty, ///< The (smaller) type to truncate to
3370 const Twine &NameStr, ///< A name for the new instruction
3371 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3374 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3375 static inline bool classof(const Instruction *I) {
3376 return I->getOpcode() == Trunc;
3378 static inline bool classof(const Value *V) {
3379 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3383 //===----------------------------------------------------------------------===//
3385 //===----------------------------------------------------------------------===//
3387 /// \brief This class represents zero extension of integer types.
3388 class ZExtInst : public CastInst {
3390 /// \brief Clone an identical ZExtInst
3391 ZExtInst *clone_impl() const override;
3394 /// \brief Constructor with insert-before-instruction semantics
3396 Value *S, ///< The value to be zero extended
3397 Type *Ty, ///< The type to zero extend to
3398 const Twine &NameStr = "", ///< A name for the new instruction
3399 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3402 /// \brief Constructor with insert-at-end semantics.
3404 Value *S, ///< The value to be zero extended
3405 Type *Ty, ///< The type to zero extend to
3406 const Twine &NameStr, ///< A name for the new instruction
3407 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3410 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3411 static inline bool classof(const Instruction *I) {
3412 return I->getOpcode() == ZExt;
3414 static inline bool classof(const Value *V) {
3415 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3419 //===----------------------------------------------------------------------===//
3421 //===----------------------------------------------------------------------===//
3423 /// \brief This class represents a sign extension of integer types.
3424 class SExtInst : public CastInst {
3426 /// \brief Clone an identical SExtInst
3427 SExtInst *clone_impl() const override;
3430 /// \brief Constructor with insert-before-instruction semantics
3432 Value *S, ///< The value to be sign extended
3433 Type *Ty, ///< The type to sign extend to
3434 const Twine &NameStr = "", ///< A name for the new instruction
3435 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3438 /// \brief Constructor with insert-at-end-of-block semantics
3440 Value *S, ///< The value to be sign extended
3441 Type *Ty, ///< The type to sign extend to
3442 const Twine &NameStr, ///< A name for the new instruction
3443 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3446 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3447 static inline bool classof(const Instruction *I) {
3448 return I->getOpcode() == SExt;
3450 static inline bool classof(const Value *V) {
3451 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3455 //===----------------------------------------------------------------------===//
3456 // FPTruncInst Class
3457 //===----------------------------------------------------------------------===//
3459 /// \brief This class represents a truncation of floating point types.
3460 class FPTruncInst : public CastInst {
3462 /// \brief Clone an identical FPTruncInst
3463 FPTruncInst *clone_impl() const override;
3466 /// \brief Constructor with insert-before-instruction semantics
3468 Value *S, ///< The value to be truncated
3469 Type *Ty, ///< The type to truncate to
3470 const Twine &NameStr = "", ///< A name for the new instruction
3471 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3474 /// \brief Constructor with insert-before-instruction semantics
3476 Value *S, ///< The value to be truncated
3477 Type *Ty, ///< The type to truncate to
3478 const Twine &NameStr, ///< A name for the new instruction
3479 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3482 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3483 static inline bool classof(const Instruction *I) {
3484 return I->getOpcode() == FPTrunc;
3486 static inline bool classof(const Value *V) {
3487 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3491 //===----------------------------------------------------------------------===//
3493 //===----------------------------------------------------------------------===//
3495 /// \brief This class represents an extension of floating point types.
3496 class FPExtInst : public CastInst {
3498 /// \brief Clone an identical FPExtInst
3499 FPExtInst *clone_impl() const override;
3502 /// \brief Constructor with insert-before-instruction semantics
3504 Value *S, ///< The value to be extended
3505 Type *Ty, ///< The type to extend to
3506 const Twine &NameStr = "", ///< A name for the new instruction
3507 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3510 /// \brief Constructor with insert-at-end-of-block semantics
3512 Value *S, ///< The value to be extended
3513 Type *Ty, ///< The type to extend to
3514 const Twine &NameStr, ///< A name for the new instruction
3515 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3518 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3519 static inline bool classof(const Instruction *I) {
3520 return I->getOpcode() == FPExt;
3522 static inline bool classof(const Value *V) {
3523 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3527 //===----------------------------------------------------------------------===//
3529 //===----------------------------------------------------------------------===//
3531 /// \brief This class represents a cast unsigned integer to floating point.
3532 class UIToFPInst : public CastInst {
3534 /// \brief Clone an identical UIToFPInst
3535 UIToFPInst *clone_impl() const override;
3538 /// \brief Constructor with insert-before-instruction semantics
3540 Value *S, ///< The value to be converted
3541 Type *Ty, ///< The type to convert to
3542 const Twine &NameStr = "", ///< A name for the new instruction
3543 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3546 /// \brief Constructor with insert-at-end-of-block semantics
3548 Value *S, ///< The value to be converted
3549 Type *Ty, ///< The type to convert to
3550 const Twine &NameStr, ///< A name for the new instruction
3551 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3554 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3555 static inline bool classof(const Instruction *I) {
3556 return I->getOpcode() == UIToFP;
3558 static inline bool classof(const Value *V) {
3559 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3563 //===----------------------------------------------------------------------===//
3565 //===----------------------------------------------------------------------===//
3567 /// \brief This class represents a cast from signed integer to floating point.
3568 class SIToFPInst : public CastInst {
3570 /// \brief Clone an identical SIToFPInst
3571 SIToFPInst *clone_impl() const override;
3574 /// \brief Constructor with insert-before-instruction semantics
3576 Value *S, ///< The value to be converted
3577 Type *Ty, ///< The type to convert to
3578 const Twine &NameStr = "", ///< A name for the new instruction
3579 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3582 /// \brief Constructor with insert-at-end-of-block semantics
3584 Value *S, ///< The value to be converted
3585 Type *Ty, ///< The type to convert to
3586 const Twine &NameStr, ///< A name for the new instruction
3587 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3590 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3591 static inline bool classof(const Instruction *I) {
3592 return I->getOpcode() == SIToFP;
3594 static inline bool classof(const Value *V) {
3595 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3599 //===----------------------------------------------------------------------===//
3601 //===----------------------------------------------------------------------===//
3603 /// \brief This class represents a cast from floating point to unsigned integer
3604 class FPToUIInst : public CastInst {
3606 /// \brief Clone an identical FPToUIInst
3607 FPToUIInst *clone_impl() const override;
3610 /// \brief Constructor with insert-before-instruction semantics
3612 Value *S, ///< The value to be converted
3613 Type *Ty, ///< The type to convert to
3614 const Twine &NameStr = "", ///< A name for the new instruction
3615 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3618 /// \brief Constructor with insert-at-end-of-block semantics
3620 Value *S, ///< The value to be converted
3621 Type *Ty, ///< The type to convert to
3622 const Twine &NameStr, ///< A name for the new instruction
3623 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3626 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3627 static inline bool classof(const Instruction *I) {
3628 return I->getOpcode() == FPToUI;
3630 static inline bool classof(const Value *V) {
3631 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3635 //===----------------------------------------------------------------------===//
3637 //===----------------------------------------------------------------------===//
3639 /// \brief This class represents a cast from floating point to signed integer.
3640 class FPToSIInst : public CastInst {
3642 /// \brief Clone an identical FPToSIInst
3643 FPToSIInst *clone_impl() const override;
3646 /// \brief Constructor with insert-before-instruction semantics
3648 Value *S, ///< The value to be converted
3649 Type *Ty, ///< The type to convert to
3650 const Twine &NameStr = "", ///< A name for the new instruction
3651 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3654 /// \brief Constructor with insert-at-end-of-block semantics
3656 Value *S, ///< The value to be converted
3657 Type *Ty, ///< The type to convert to
3658 const Twine &NameStr, ///< A name for the new instruction
3659 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3662 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3663 static inline bool classof(const Instruction *I) {
3664 return I->getOpcode() == FPToSI;
3666 static inline bool classof(const Value *V) {
3667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3671 //===----------------------------------------------------------------------===//
3672 // IntToPtrInst Class
3673 //===----------------------------------------------------------------------===//
3675 /// \brief This class represents a cast from an integer to a pointer.
3676 class IntToPtrInst : public CastInst {
3678 /// \brief Constructor with insert-before-instruction semantics
3680 Value *S, ///< The value to be converted
3681 Type *Ty, ///< The type to convert to
3682 const Twine &NameStr = "", ///< A name for the new instruction
3683 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3686 /// \brief Constructor with insert-at-end-of-block semantics
3688 Value *S, ///< The value to be converted
3689 Type *Ty, ///< The type to convert to
3690 const Twine &NameStr, ///< A name for the new instruction
3691 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3694 /// \brief Clone an identical IntToPtrInst
3695 IntToPtrInst *clone_impl() const override;
3697 /// \brief Returns the address space of this instruction's pointer type.
3698 unsigned getAddressSpace() const {
3699 return getType()->getPointerAddressSpace();
3702 // Methods for support type inquiry through isa, cast, and dyn_cast:
3703 static inline bool classof(const Instruction *I) {
3704 return I->getOpcode() == IntToPtr;
3706 static inline bool classof(const Value *V) {
3707 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3711 //===----------------------------------------------------------------------===//
3712 // PtrToIntInst Class
3713 //===----------------------------------------------------------------------===//
3715 /// \brief This class represents a cast from a pointer to an integer
3716 class PtrToIntInst : public CastInst {
3718 /// \brief Clone an identical PtrToIntInst
3719 PtrToIntInst *clone_impl() const override;
3722 /// \brief Constructor with insert-before-instruction semantics
3724 Value *S, ///< The value to be converted
3725 Type *Ty, ///< The type to convert to
3726 const Twine &NameStr = "", ///< A name for the new instruction
3727 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3730 /// \brief Constructor with insert-at-end-of-block semantics
3732 Value *S, ///< The value to be converted
3733 Type *Ty, ///< The type to convert to
3734 const Twine &NameStr, ///< A name for the new instruction
3735 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3738 /// \brief Gets the pointer operand.
3739 Value *getPointerOperand() { return getOperand(0); }
3740 /// \brief Gets the pointer operand.
3741 const Value *getPointerOperand() const { return getOperand(0); }
3742 /// \brief Gets the operand index of the pointer operand.
3743 static unsigned getPointerOperandIndex() { return 0U; }
3745 /// \brief Returns the address space of the pointer operand.
3746 unsigned getPointerAddressSpace() const {
3747 return getPointerOperand()->getType()->getPointerAddressSpace();
3750 // Methods for support type inquiry through isa, cast, and dyn_cast:
3751 static inline bool classof(const Instruction *I) {
3752 return I->getOpcode() == PtrToInt;
3754 static inline bool classof(const Value *V) {
3755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3759 //===----------------------------------------------------------------------===//
3760 // BitCastInst Class
3761 //===----------------------------------------------------------------------===//
3763 /// \brief This class represents a no-op cast from one type to another.
3764 class BitCastInst : public CastInst {
3766 /// \brief Clone an identical BitCastInst
3767 BitCastInst *clone_impl() const override;
3770 /// \brief Constructor with insert-before-instruction semantics
3772 Value *S, ///< The value to be casted
3773 Type *Ty, ///< The type to casted to
3774 const Twine &NameStr = "", ///< A name for the new instruction
3775 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3778 /// \brief Constructor with insert-at-end-of-block semantics
3780 Value *S, ///< The value to be casted
3781 Type *Ty, ///< The type to casted to
3782 const Twine &NameStr, ///< A name for the new instruction
3783 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3786 // Methods for support type inquiry through isa, cast, and dyn_cast:
3787 static inline bool classof(const Instruction *I) {
3788 return I->getOpcode() == BitCast;
3790 static inline bool classof(const Value *V) {
3791 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3795 //===----------------------------------------------------------------------===//
3796 // AddrSpaceCastInst Class
3797 //===----------------------------------------------------------------------===//
3799 /// \brief This class represents a conversion between pointers from
3800 /// one address space to another.
3801 class AddrSpaceCastInst : public CastInst {
3803 /// \brief Clone an identical AddrSpaceCastInst
3804 AddrSpaceCastInst *clone_impl() const override;
3807 /// \brief Constructor with insert-before-instruction semantics
3809 Value *S, ///< The value to be casted
3810 Type *Ty, ///< The type to casted to
3811 const Twine &NameStr = "", ///< A name for the new instruction
3812 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3815 /// \brief Constructor with insert-at-end-of-block semantics
3817 Value *S, ///< The value to be casted
3818 Type *Ty, ///< The type to casted to
3819 const Twine &NameStr, ///< A name for the new instruction
3820 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3823 // Methods for support type inquiry through isa, cast, and dyn_cast:
3824 static inline bool classof(const Instruction *I) {
3825 return I->getOpcode() == AddrSpaceCast;
3827 static inline bool classof(const Value *V) {
3828 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3832 } // End llvm namespace