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(Type *PointeeType, Value *Ptr,
798 ArrayRef<Value *> IdxList, unsigned Values,
799 const Twine &NameStr, Instruction *InsertBefore);
800 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
801 ArrayRef<Value *> IdxList, unsigned Values,
802 const Twine &NameStr, BasicBlock *InsertAtEnd);
805 GetElementPtrInst *clone_impl() const override;
807 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
808 const Twine &NameStr = "",
809 Instruction *InsertBefore = nullptr) {
810 return Create(nullptr, Ptr, IdxList, NameStr, InsertBefore);
812 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
813 ArrayRef<Value *> IdxList,
814 const Twine &NameStr = "",
815 Instruction *InsertBefore = nullptr) {
816 unsigned Values = 1 + unsigned(IdxList.size());
817 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
818 NameStr, InsertBefore);
820 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
821 const Twine &NameStr,
822 BasicBlock *InsertAtEnd) {
823 return Create(nullptr, Ptr, NameStr, InsertAtEnd);
825 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
826 ArrayRef<Value *> IdxList,
827 const Twine &NameStr,
828 BasicBlock *InsertAtEnd) {
829 unsigned Values = 1 + unsigned(IdxList.size());
830 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
831 NameStr, InsertAtEnd);
834 /// Create an "inbounds" getelementptr. See the documentation for the
835 /// "inbounds" flag in LangRef.html for details.
836 static GetElementPtrInst *CreateInBounds(Value *Ptr,
837 ArrayRef<Value *> IdxList,
838 const Twine &NameStr = "",
839 Instruction *InsertBefore = nullptr){
840 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
842 static GetElementPtrInst *
843 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
844 const Twine &NameStr = "",
845 Instruction *InsertBefore = nullptr) {
846 GetElementPtrInst *GEP =
847 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
848 GEP->setIsInBounds(true);
851 static GetElementPtrInst *CreateInBounds(Value *Ptr,
852 ArrayRef<Value *> IdxList,
853 const Twine &NameStr,
854 BasicBlock *InsertAtEnd) {
855 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
857 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
858 ArrayRef<Value *> IdxList,
859 const Twine &NameStr,
860 BasicBlock *InsertAtEnd) {
861 GetElementPtrInst *GEP =
862 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
863 GEP->setIsInBounds(true);
867 /// Transparently provide more efficient getOperand methods.
868 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
870 // getType - Overload to return most specific sequential type.
871 SequentialType *getType() const {
872 return cast<SequentialType>(Instruction::getType());
875 Type *getSourceElementType() const {
876 return cast<SequentialType>(getPointerOperandType()->getScalarType())
880 Type *getResultElementType() const { return getType()->getElementType(); }
882 /// \brief Returns the address space of this instruction's pointer type.
883 unsigned getAddressSpace() const {
884 // Note that this is always the same as the pointer operand's address space
885 // and that is cheaper to compute, so cheat here.
886 return getPointerAddressSpace();
889 /// getIndexedType - Returns the type of the element that would be loaded with
890 /// a load instruction with the specified parameters.
892 /// Null is returned if the indices are invalid for the specified
895 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
896 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
897 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
899 inline op_iterator idx_begin() { return op_begin()+1; }
900 inline const_op_iterator idx_begin() const { return op_begin()+1; }
901 inline op_iterator idx_end() { return op_end(); }
902 inline const_op_iterator idx_end() const { return op_end(); }
904 Value *getPointerOperand() {
905 return getOperand(0);
907 const Value *getPointerOperand() const {
908 return getOperand(0);
910 static unsigned getPointerOperandIndex() {
911 return 0U; // get index for modifying correct operand.
914 /// getPointerOperandType - Method to return the pointer operand as a
916 Type *getPointerOperandType() const {
917 return getPointerOperand()->getType();
920 /// \brief Returns the address space of the pointer operand.
921 unsigned getPointerAddressSpace() const {
922 return getPointerOperandType()->getPointerAddressSpace();
925 /// GetGEPReturnType - Returns the pointer type returned by the GEP
926 /// instruction, which may be a vector of pointers.
927 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
928 Type *PtrTy = PointerType::get(checkGEPType(
929 getIndexedType(Ptr->getType(), IdxList)),
930 Ptr->getType()->getPointerAddressSpace());
932 if (Ptr->getType()->isVectorTy()) {
933 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
934 return VectorType::get(PtrTy, NumElem);
941 unsigned getNumIndices() const { // Note: always non-negative
942 return getNumOperands() - 1;
945 bool hasIndices() const {
946 return getNumOperands() > 1;
949 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
950 /// zeros. If so, the result pointer and the first operand have the same
951 /// value, just potentially different types.
952 bool hasAllZeroIndices() const;
954 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
955 /// constant integers. If so, the result pointer and the first operand have
956 /// a constant offset between them.
957 bool hasAllConstantIndices() const;
959 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
960 /// See LangRef.html for the meaning of inbounds on a getelementptr.
961 void setIsInBounds(bool b = true);
963 /// isInBounds - Determine whether the GEP has the inbounds flag.
964 bool isInBounds() const;
966 /// \brief Accumulate the constant address offset of this GEP if possible.
968 /// This routine accepts an APInt into which it will accumulate the constant
969 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
970 /// all-constant, it returns false and the value of the offset APInt is
971 /// undefined (it is *not* preserved!). The APInt passed into this routine
972 /// must be at least as wide as the IntPtr type for the address space of
973 /// the base GEP pointer.
974 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
976 // Methods for support type inquiry through isa, cast, and dyn_cast:
977 static inline bool classof(const Instruction *I) {
978 return (I->getOpcode() == Instruction::GetElementPtr);
980 static inline bool classof(const Value *V) {
981 return isa<Instruction>(V) && classof(cast<Instruction>(V));
986 struct OperandTraits<GetElementPtrInst> :
987 public VariadicOperandTraits<GetElementPtrInst, 1> {
990 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
991 ArrayRef<Value *> IdxList, unsigned Values,
992 const Twine &NameStr,
993 Instruction *InsertBefore)
994 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
995 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
996 Values, InsertBefore) {
997 init(Ptr, IdxList, NameStr);
998 assert(!PointeeType || PointeeType == getSourceElementType());
1000 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1001 ArrayRef<Value *> IdxList, unsigned Values,
1002 const Twine &NameStr,
1003 BasicBlock *InsertAtEnd)
1004 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
1005 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1006 Values, InsertAtEnd) {
1007 init(Ptr, IdxList, NameStr);
1008 assert(!PointeeType || PointeeType == getSourceElementType());
1012 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1015 //===----------------------------------------------------------------------===//
1017 //===----------------------------------------------------------------------===//
1019 /// This instruction compares its operands according to the predicate given
1020 /// to the constructor. It only operates on integers or pointers. The operands
1021 /// must be identical types.
1022 /// \brief Represent an integer comparison operator.
1023 class ICmpInst: public CmpInst {
1025 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1026 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1027 "Invalid ICmp predicate value");
1028 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1029 "Both operands to ICmp instruction are not of the same type!");
1030 // Check that the operands are the right type
1031 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1032 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1033 "Invalid operand types for ICmp instruction");
1037 /// \brief Clone an identical ICmpInst
1038 ICmpInst *clone_impl() const override;
1040 /// \brief Constructor with insert-before-instruction semantics.
1042 Instruction *InsertBefore, ///< Where to insert
1043 Predicate pred, ///< The predicate to use for the comparison
1044 Value *LHS, ///< The left-hand-side of the expression
1045 Value *RHS, ///< The right-hand-side of the expression
1046 const Twine &NameStr = "" ///< Name of the instruction
1047 ) : CmpInst(makeCmpResultType(LHS->getType()),
1048 Instruction::ICmp, pred, LHS, RHS, NameStr,
1055 /// \brief Constructor with insert-at-end semantics.
1057 BasicBlock &InsertAtEnd, ///< Block to insert into.
1058 Predicate pred, ///< The predicate to use for the comparison
1059 Value *LHS, ///< The left-hand-side of the expression
1060 Value *RHS, ///< The right-hand-side of the expression
1061 const Twine &NameStr = "" ///< Name of the instruction
1062 ) : CmpInst(makeCmpResultType(LHS->getType()),
1063 Instruction::ICmp, pred, LHS, RHS, NameStr,
1070 /// \brief Constructor with no-insertion semantics
1072 Predicate pred, ///< The predicate to use for the comparison
1073 Value *LHS, ///< The left-hand-side of the expression
1074 Value *RHS, ///< The right-hand-side of the expression
1075 const Twine &NameStr = "" ///< Name of the instruction
1076 ) : CmpInst(makeCmpResultType(LHS->getType()),
1077 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1083 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1084 /// @returns the predicate that would be the result if the operand were
1085 /// regarded as signed.
1086 /// \brief Return the signed version of the predicate
1087 Predicate getSignedPredicate() const {
1088 return getSignedPredicate(getPredicate());
1091 /// This is a static version that you can use without an instruction.
1092 /// \brief Return the signed version of the predicate.
1093 static Predicate getSignedPredicate(Predicate pred);
1095 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1096 /// @returns the predicate that would be the result if the operand were
1097 /// regarded as unsigned.
1098 /// \brief Return the unsigned version of the predicate
1099 Predicate getUnsignedPredicate() const {
1100 return getUnsignedPredicate(getPredicate());
1103 /// This is a static version that you can use without an instruction.
1104 /// \brief Return the unsigned version of the predicate.
1105 static Predicate getUnsignedPredicate(Predicate pred);
1107 /// isEquality - Return true if this predicate is either EQ or NE. This also
1108 /// tests for commutativity.
1109 static bool isEquality(Predicate P) {
1110 return P == ICMP_EQ || P == ICMP_NE;
1113 /// isEquality - Return true if this predicate is either EQ or NE. This also
1114 /// tests for commutativity.
1115 bool isEquality() const {
1116 return isEquality(getPredicate());
1119 /// @returns true if the predicate of this ICmpInst is commutative
1120 /// \brief Determine if this relation is commutative.
1121 bool isCommutative() const { return isEquality(); }
1123 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1125 bool isRelational() const {
1126 return !isEquality();
1129 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1131 static bool isRelational(Predicate P) {
1132 return !isEquality(P);
1135 /// Initialize a set of values that all satisfy the predicate with C.
1136 /// \brief Make a ConstantRange for a relation with a constant value.
1137 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1139 /// Exchange the two operands to this instruction in such a way that it does
1140 /// not modify the semantics of the instruction. The predicate value may be
1141 /// changed to retain the same result if the predicate is order dependent
1143 /// \brief Swap operands and adjust predicate.
1144 void swapOperands() {
1145 setPredicate(getSwappedPredicate());
1146 Op<0>().swap(Op<1>());
1149 // Methods for support type inquiry through isa, cast, and dyn_cast:
1150 static inline bool classof(const Instruction *I) {
1151 return I->getOpcode() == Instruction::ICmp;
1153 static inline bool classof(const Value *V) {
1154 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1159 //===----------------------------------------------------------------------===//
1161 //===----------------------------------------------------------------------===//
1163 /// This instruction compares its operands according to the predicate given
1164 /// to the constructor. It only operates on floating point values or packed
1165 /// vectors of floating point values. The operands must be identical types.
1166 /// \brief Represents a floating point comparison operator.
1167 class FCmpInst: public CmpInst {
1169 /// \brief Clone an identical FCmpInst
1170 FCmpInst *clone_impl() const override;
1172 /// \brief Constructor with insert-before-instruction semantics.
1174 Instruction *InsertBefore, ///< Where to insert
1175 Predicate pred, ///< The predicate to use for the comparison
1176 Value *LHS, ///< The left-hand-side of the expression
1177 Value *RHS, ///< The right-hand-side of the expression
1178 const Twine &NameStr = "" ///< Name of the instruction
1179 ) : CmpInst(makeCmpResultType(LHS->getType()),
1180 Instruction::FCmp, pred, LHS, RHS, NameStr,
1182 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1183 "Invalid FCmp predicate value");
1184 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1185 "Both operands to FCmp instruction are not of the same type!");
1186 // Check that the operands are the right type
1187 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1188 "Invalid operand types for FCmp instruction");
1191 /// \brief Constructor with insert-at-end semantics.
1193 BasicBlock &InsertAtEnd, ///< Block to insert into.
1194 Predicate pred, ///< The predicate to use for the comparison
1195 Value *LHS, ///< The left-hand-side of the expression
1196 Value *RHS, ///< The right-hand-side of the expression
1197 const Twine &NameStr = "" ///< Name of the instruction
1198 ) : CmpInst(makeCmpResultType(LHS->getType()),
1199 Instruction::FCmp, pred, LHS, RHS, NameStr,
1201 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1202 "Invalid FCmp predicate value");
1203 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1204 "Both operands to FCmp instruction are not of the same type!");
1205 // Check that the operands are the right type
1206 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1207 "Invalid operand types for FCmp instruction");
1210 /// \brief Constructor with no-insertion semantics
1212 Predicate pred, ///< The predicate to use for the comparison
1213 Value *LHS, ///< The left-hand-side of the expression
1214 Value *RHS, ///< The right-hand-side of the expression
1215 const Twine &NameStr = "" ///< Name of the instruction
1216 ) : CmpInst(makeCmpResultType(LHS->getType()),
1217 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1218 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1219 "Invalid FCmp predicate value");
1220 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1221 "Both operands to FCmp instruction are not of the same type!");
1222 // Check that the operands are the right type
1223 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1224 "Invalid operand types for FCmp instruction");
1227 /// @returns true if the predicate of this instruction is EQ or NE.
1228 /// \brief Determine if this is an equality predicate.
1229 static bool isEquality(Predicate Pred) {
1230 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1234 /// @returns true if the predicate of this instruction is EQ or NE.
1235 /// \brief Determine if this is an equality predicate.
1236 bool isEquality() const { return isEquality(getPredicate()); }
1238 /// @returns true if the predicate of this instruction is commutative.
1239 /// \brief Determine if this is a commutative predicate.
1240 bool isCommutative() const {
1241 return isEquality() ||
1242 getPredicate() == FCMP_FALSE ||
1243 getPredicate() == FCMP_TRUE ||
1244 getPredicate() == FCMP_ORD ||
1245 getPredicate() == FCMP_UNO;
1248 /// @returns true if the predicate is relational (not EQ or NE).
1249 /// \brief Determine if this a relational predicate.
1250 bool isRelational() const { return !isEquality(); }
1252 /// Exchange the two operands to this instruction in such a way that it does
1253 /// not modify the semantics of the instruction. The predicate value may be
1254 /// changed to retain the same result if the predicate is order dependent
1256 /// \brief Swap operands and adjust predicate.
1257 void swapOperands() {
1258 setPredicate(getSwappedPredicate());
1259 Op<0>().swap(Op<1>());
1262 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1263 static inline bool classof(const Instruction *I) {
1264 return I->getOpcode() == Instruction::FCmp;
1266 static inline bool classof(const Value *V) {
1267 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1271 //===----------------------------------------------------------------------===//
1272 /// CallInst - This class represents a function call, abstracting a target
1273 /// machine's calling convention. This class uses low bit of the SubClassData
1274 /// field to indicate whether or not this is a tail call. The rest of the bits
1275 /// hold the calling convention of the call.
1277 class CallInst : public Instruction {
1278 AttributeSet AttributeList; ///< parameter attributes for call
1279 CallInst(const CallInst &CI);
1280 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1281 void init(Value *Func, const Twine &NameStr);
1283 /// Construct a CallInst given a range of arguments.
1284 /// \brief Construct a CallInst from a range of arguments
1285 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1286 const Twine &NameStr, Instruction *InsertBefore);
1288 /// Construct a CallInst given a range of arguments.
1289 /// \brief Construct a CallInst from a range of arguments
1290 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1291 const Twine &NameStr, BasicBlock *InsertAtEnd);
1293 explicit CallInst(Value *F, const Twine &NameStr,
1294 Instruction *InsertBefore);
1295 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1297 CallInst *clone_impl() const override;
1299 static CallInst *Create(Value *Func,
1300 ArrayRef<Value *> Args,
1301 const Twine &NameStr = "",
1302 Instruction *InsertBefore = nullptr) {
1303 return new(unsigned(Args.size() + 1))
1304 CallInst(Func, Args, NameStr, InsertBefore);
1306 static CallInst *Create(Value *Func,
1307 ArrayRef<Value *> Args,
1308 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1309 return new(unsigned(Args.size() + 1))
1310 CallInst(Func, Args, NameStr, InsertAtEnd);
1312 static CallInst *Create(Value *F, const Twine &NameStr = "",
1313 Instruction *InsertBefore = nullptr) {
1314 return new(1) CallInst(F, NameStr, InsertBefore);
1316 static CallInst *Create(Value *F, const Twine &NameStr,
1317 BasicBlock *InsertAtEnd) {
1318 return new(1) CallInst(F, NameStr, InsertAtEnd);
1320 /// CreateMalloc - Generate the IR for a call to malloc:
1321 /// 1. Compute the malloc call's argument as the specified type's size,
1322 /// possibly multiplied by the array size if the array size is not
1324 /// 2. Call malloc with that argument.
1325 /// 3. Bitcast the result of the malloc call to the specified type.
1326 static Instruction *CreateMalloc(Instruction *InsertBefore,
1327 Type *IntPtrTy, Type *AllocTy,
1328 Value *AllocSize, Value *ArraySize = nullptr,
1329 Function* MallocF = nullptr,
1330 const Twine &Name = "");
1331 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1332 Type *IntPtrTy, Type *AllocTy,
1333 Value *AllocSize, Value *ArraySize = nullptr,
1334 Function* MallocF = nullptr,
1335 const Twine &Name = "");
1336 /// CreateFree - Generate the IR for a call to the builtin free function.
1337 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1338 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1342 // Note that 'musttail' implies 'tail'.
1343 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1344 TailCallKind getTailCallKind() const {
1345 return TailCallKind(getSubclassDataFromInstruction() & 3);
1347 bool isTailCall() const {
1348 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1350 bool isMustTailCall() const {
1351 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1353 void setTailCall(bool isTC = true) {
1354 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1355 unsigned(isTC ? TCK_Tail : TCK_None));
1357 void setTailCallKind(TailCallKind TCK) {
1358 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1362 /// Provide fast operand accessors
1363 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1365 /// getNumArgOperands - Return the number of call arguments.
1367 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1369 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1371 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1372 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1374 /// arg_operands - iteration adapter for range-for loops.
1375 iterator_range<op_iterator> arg_operands() {
1376 // The last operand in the op list is the callee - it's not one of the args
1377 // so we don't want to iterate over it.
1378 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1381 /// arg_operands - iteration adapter for range-for loops.
1382 iterator_range<const_op_iterator> arg_operands() const {
1383 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1386 /// \brief Wrappers for getting the \c Use of a call argument.
1387 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1388 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1390 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1392 CallingConv::ID getCallingConv() const {
1393 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1395 void setCallingConv(CallingConv::ID CC) {
1396 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1397 (static_cast<unsigned>(CC) << 2));
1400 /// getAttributes - Return the parameter attributes for this call.
1402 const AttributeSet &getAttributes() const { return AttributeList; }
1404 /// setAttributes - Set the parameter attributes for this call.
1406 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1408 /// addAttribute - adds the attribute to the list of attributes.
1409 void addAttribute(unsigned i, Attribute::AttrKind attr);
1411 /// removeAttribute - removes the attribute from the list of attributes.
1412 void removeAttribute(unsigned i, Attribute attr);
1414 /// \brief adds the dereferenceable attribute to the list of attributes.
1415 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1417 /// \brief Determine whether this call has the given attribute.
1418 bool hasFnAttr(Attribute::AttrKind A) const {
1419 assert(A != Attribute::NoBuiltin &&
1420 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1421 return hasFnAttrImpl(A);
1424 /// \brief Determine whether the call or the callee has the given attributes.
1425 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1427 /// \brief Extract the alignment for a call or parameter (0=unknown).
1428 unsigned getParamAlignment(unsigned i) const {
1429 return AttributeList.getParamAlignment(i);
1432 /// \brief Extract the number of dereferenceable bytes for a call or
1433 /// parameter (0=unknown).
1434 uint64_t getDereferenceableBytes(unsigned i) const {
1435 return AttributeList.getDereferenceableBytes(i);
1438 /// \brief Return true if the call should not be treated as a call to a
1440 bool isNoBuiltin() const {
1441 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1442 !hasFnAttrImpl(Attribute::Builtin);
1445 /// \brief Return true if the call should not be inlined.
1446 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1447 void setIsNoInline() {
1448 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1451 /// \brief Return true if the call can return twice
1452 bool canReturnTwice() const {
1453 return hasFnAttr(Attribute::ReturnsTwice);
1455 void setCanReturnTwice() {
1456 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1459 /// \brief Determine if the call does not access memory.
1460 bool doesNotAccessMemory() const {
1461 return hasFnAttr(Attribute::ReadNone);
1463 void setDoesNotAccessMemory() {
1464 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1467 /// \brief Determine if the call does not access or only reads memory.
1468 bool onlyReadsMemory() const {
1469 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1471 void setOnlyReadsMemory() {
1472 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1475 /// \brief Determine if the call cannot return.
1476 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1477 void setDoesNotReturn() {
1478 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1481 /// \brief Determine if the call cannot unwind.
1482 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1483 void setDoesNotThrow() {
1484 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1487 /// \brief Determine if the call cannot be duplicated.
1488 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1489 void setCannotDuplicate() {
1490 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1493 /// \brief Determine if the call returns a structure through first
1494 /// pointer argument.
1495 bool hasStructRetAttr() const {
1496 // Be friendly and also check the callee.
1497 return paramHasAttr(1, Attribute::StructRet);
1500 /// \brief Determine if any call argument is an aggregate passed by value.
1501 bool hasByValArgument() const {
1502 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1505 /// getCalledFunction - Return the function called, or null if this is an
1506 /// indirect function invocation.
1508 Function *getCalledFunction() const {
1509 return dyn_cast<Function>(Op<-1>());
1512 /// getCalledValue - Get a pointer to the function that is invoked by this
1514 const Value *getCalledValue() const { return Op<-1>(); }
1515 Value *getCalledValue() { return Op<-1>(); }
1517 /// setCalledFunction - Set the function called.
1518 void setCalledFunction(Value* Fn) {
1522 /// isInlineAsm - Check if this call is an inline asm statement.
1523 bool isInlineAsm() const {
1524 return isa<InlineAsm>(Op<-1>());
1527 // Methods for support type inquiry through isa, cast, and dyn_cast:
1528 static inline bool classof(const Instruction *I) {
1529 return I->getOpcode() == Instruction::Call;
1531 static inline bool classof(const Value *V) {
1532 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1536 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1538 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1539 // method so that subclasses cannot accidentally use it.
1540 void setInstructionSubclassData(unsigned short D) {
1541 Instruction::setInstructionSubclassData(D);
1546 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1549 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1550 const Twine &NameStr, BasicBlock *InsertAtEnd)
1551 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1552 ->getElementType())->getReturnType(),
1554 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1555 unsigned(Args.size() + 1), InsertAtEnd) {
1556 init(Func, Args, NameStr);
1559 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1560 const Twine &NameStr, Instruction *InsertBefore)
1561 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1562 ->getElementType())->getReturnType(),
1564 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1565 unsigned(Args.size() + 1), InsertBefore) {
1566 init(Func, Args, NameStr);
1570 // Note: if you get compile errors about private methods then
1571 // please update your code to use the high-level operand
1572 // interfaces. See line 943 above.
1573 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1575 //===----------------------------------------------------------------------===//
1577 //===----------------------------------------------------------------------===//
1579 /// SelectInst - This class represents the LLVM 'select' instruction.
1581 class SelectInst : public Instruction {
1582 void init(Value *C, Value *S1, Value *S2) {
1583 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1589 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1590 Instruction *InsertBefore)
1591 : Instruction(S1->getType(), Instruction::Select,
1592 &Op<0>(), 3, InsertBefore) {
1596 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1597 BasicBlock *InsertAtEnd)
1598 : Instruction(S1->getType(), Instruction::Select,
1599 &Op<0>(), 3, InsertAtEnd) {
1604 SelectInst *clone_impl() const override;
1606 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1607 const Twine &NameStr = "",
1608 Instruction *InsertBefore = nullptr) {
1609 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1611 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1612 const Twine &NameStr,
1613 BasicBlock *InsertAtEnd) {
1614 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1617 const Value *getCondition() const { return Op<0>(); }
1618 const Value *getTrueValue() const { return Op<1>(); }
1619 const Value *getFalseValue() const { return Op<2>(); }
1620 Value *getCondition() { return Op<0>(); }
1621 Value *getTrueValue() { return Op<1>(); }
1622 Value *getFalseValue() { return Op<2>(); }
1624 /// areInvalidOperands - Return a string if the specified operands are invalid
1625 /// for a select operation, otherwise return null.
1626 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1628 /// Transparently provide more efficient getOperand methods.
1629 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1631 OtherOps getOpcode() const {
1632 return static_cast<OtherOps>(Instruction::getOpcode());
1635 // Methods for support type inquiry through isa, cast, and dyn_cast:
1636 static inline bool classof(const Instruction *I) {
1637 return I->getOpcode() == Instruction::Select;
1639 static inline bool classof(const Value *V) {
1640 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1645 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1648 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1650 //===----------------------------------------------------------------------===//
1652 //===----------------------------------------------------------------------===//
1654 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1655 /// an argument of the specified type given a va_list and increments that list
1657 class VAArgInst : public UnaryInstruction {
1659 VAArgInst *clone_impl() const override;
1662 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1663 Instruction *InsertBefore = nullptr)
1664 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1667 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1668 BasicBlock *InsertAtEnd)
1669 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1673 Value *getPointerOperand() { return getOperand(0); }
1674 const Value *getPointerOperand() const { return getOperand(0); }
1675 static unsigned getPointerOperandIndex() { return 0U; }
1677 // Methods for support type inquiry through isa, cast, and dyn_cast:
1678 static inline bool classof(const Instruction *I) {
1679 return I->getOpcode() == VAArg;
1681 static inline bool classof(const Value *V) {
1682 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1686 //===----------------------------------------------------------------------===//
1687 // ExtractElementInst Class
1688 //===----------------------------------------------------------------------===//
1690 /// ExtractElementInst - This instruction extracts a single (scalar)
1691 /// element from a VectorType value
1693 class ExtractElementInst : public Instruction {
1694 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1695 Instruction *InsertBefore = nullptr);
1696 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1697 BasicBlock *InsertAtEnd);
1699 ExtractElementInst *clone_impl() const override;
1702 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1703 const Twine &NameStr = "",
1704 Instruction *InsertBefore = nullptr) {
1705 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1707 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1708 const Twine &NameStr,
1709 BasicBlock *InsertAtEnd) {
1710 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1713 /// isValidOperands - Return true if an extractelement instruction can be
1714 /// formed with the specified operands.
1715 static bool isValidOperands(const Value *Vec, const Value *Idx);
1717 Value *getVectorOperand() { return Op<0>(); }
1718 Value *getIndexOperand() { return Op<1>(); }
1719 const Value *getVectorOperand() const { return Op<0>(); }
1720 const Value *getIndexOperand() const { return Op<1>(); }
1722 VectorType *getVectorOperandType() const {
1723 return cast<VectorType>(getVectorOperand()->getType());
1727 /// Transparently provide more efficient getOperand methods.
1728 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1730 // Methods for support type inquiry through isa, cast, and dyn_cast:
1731 static inline bool classof(const Instruction *I) {
1732 return I->getOpcode() == Instruction::ExtractElement;
1734 static inline bool classof(const Value *V) {
1735 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1740 struct OperandTraits<ExtractElementInst> :
1741 public FixedNumOperandTraits<ExtractElementInst, 2> {
1744 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1746 //===----------------------------------------------------------------------===//
1747 // InsertElementInst Class
1748 //===----------------------------------------------------------------------===//
1750 /// InsertElementInst - This instruction inserts a single (scalar)
1751 /// element into a VectorType value
1753 class InsertElementInst : public Instruction {
1754 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1755 const Twine &NameStr = "",
1756 Instruction *InsertBefore = nullptr);
1757 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1758 const Twine &NameStr, BasicBlock *InsertAtEnd);
1760 InsertElementInst *clone_impl() const override;
1763 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1764 const Twine &NameStr = "",
1765 Instruction *InsertBefore = nullptr) {
1766 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1768 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1769 const Twine &NameStr,
1770 BasicBlock *InsertAtEnd) {
1771 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1774 /// isValidOperands - Return true if an insertelement instruction can be
1775 /// formed with the specified operands.
1776 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1779 /// getType - Overload to return most specific vector type.
1781 VectorType *getType() const {
1782 return cast<VectorType>(Instruction::getType());
1785 /// Transparently provide more efficient getOperand methods.
1786 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1788 // Methods for support type inquiry through isa, cast, and dyn_cast:
1789 static inline bool classof(const Instruction *I) {
1790 return I->getOpcode() == Instruction::InsertElement;
1792 static inline bool classof(const Value *V) {
1793 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1798 struct OperandTraits<InsertElementInst> :
1799 public FixedNumOperandTraits<InsertElementInst, 3> {
1802 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1804 //===----------------------------------------------------------------------===//
1805 // ShuffleVectorInst Class
1806 //===----------------------------------------------------------------------===//
1808 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1811 class ShuffleVectorInst : public Instruction {
1813 ShuffleVectorInst *clone_impl() const override;
1816 // allocate space for exactly three operands
1817 void *operator new(size_t s) {
1818 return User::operator new(s, 3);
1820 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1821 const Twine &NameStr = "",
1822 Instruction *InsertBefor = nullptr);
1823 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1824 const Twine &NameStr, BasicBlock *InsertAtEnd);
1826 /// isValidOperands - Return true if a shufflevector instruction can be
1827 /// formed with the specified operands.
1828 static bool isValidOperands(const Value *V1, const Value *V2,
1831 /// getType - Overload to return most specific vector type.
1833 VectorType *getType() const {
1834 return cast<VectorType>(Instruction::getType());
1837 /// Transparently provide more efficient getOperand methods.
1838 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1840 Constant *getMask() const {
1841 return cast<Constant>(getOperand(2));
1844 /// getMaskValue - Return the index from the shuffle mask for the specified
1845 /// output result. This is either -1 if the element is undef or a number less
1846 /// than 2*numelements.
1847 static int getMaskValue(Constant *Mask, unsigned i);
1849 int getMaskValue(unsigned i) const {
1850 return getMaskValue(getMask(), i);
1853 /// getShuffleMask - Return the full mask for this instruction, where each
1854 /// element is the element number and undef's are returned as -1.
1855 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1857 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1858 return getShuffleMask(getMask(), Result);
1861 SmallVector<int, 16> getShuffleMask() const {
1862 SmallVector<int, 16> Mask;
1863 getShuffleMask(Mask);
1868 // Methods for support type inquiry through isa, cast, and dyn_cast:
1869 static inline bool classof(const Instruction *I) {
1870 return I->getOpcode() == Instruction::ShuffleVector;
1872 static inline bool classof(const Value *V) {
1873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1878 struct OperandTraits<ShuffleVectorInst> :
1879 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1882 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1884 //===----------------------------------------------------------------------===//
1885 // ExtractValueInst Class
1886 //===----------------------------------------------------------------------===//
1888 /// ExtractValueInst - This instruction extracts a struct member or array
1889 /// element value from an aggregate value.
1891 class ExtractValueInst : public UnaryInstruction {
1892 SmallVector<unsigned, 4> Indices;
1894 ExtractValueInst(const ExtractValueInst &EVI);
1895 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1897 /// Constructors - Create a extractvalue instruction with a base aggregate
1898 /// value and a list of indices. The first ctor can optionally insert before
1899 /// an existing instruction, the second appends the new instruction to the
1900 /// specified BasicBlock.
1901 inline ExtractValueInst(Value *Agg,
1902 ArrayRef<unsigned> Idxs,
1903 const Twine &NameStr,
1904 Instruction *InsertBefore);
1905 inline ExtractValueInst(Value *Agg,
1906 ArrayRef<unsigned> Idxs,
1907 const Twine &NameStr, BasicBlock *InsertAtEnd);
1909 // allocate space for exactly one operand
1910 void *operator new(size_t s) {
1911 return User::operator new(s, 1);
1914 ExtractValueInst *clone_impl() const override;
1917 static ExtractValueInst *Create(Value *Agg,
1918 ArrayRef<unsigned> Idxs,
1919 const Twine &NameStr = "",
1920 Instruction *InsertBefore = nullptr) {
1922 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1924 static ExtractValueInst *Create(Value *Agg,
1925 ArrayRef<unsigned> Idxs,
1926 const Twine &NameStr,
1927 BasicBlock *InsertAtEnd) {
1928 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1931 /// getIndexedType - Returns the type of the element that would be extracted
1932 /// with an extractvalue instruction with the specified parameters.
1934 /// Null is returned if the indices are invalid for the specified type.
1935 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1937 typedef const unsigned* idx_iterator;
1938 inline idx_iterator idx_begin() const { return Indices.begin(); }
1939 inline idx_iterator idx_end() const { return Indices.end(); }
1940 inline iterator_range<idx_iterator> indices() const {
1941 return iterator_range<idx_iterator>(idx_begin(), idx_end());
1944 Value *getAggregateOperand() {
1945 return getOperand(0);
1947 const Value *getAggregateOperand() const {
1948 return getOperand(0);
1950 static unsigned getAggregateOperandIndex() {
1951 return 0U; // get index for modifying correct operand
1954 ArrayRef<unsigned> getIndices() const {
1958 unsigned getNumIndices() const {
1959 return (unsigned)Indices.size();
1962 bool hasIndices() const {
1966 // Methods for support type inquiry through isa, cast, and dyn_cast:
1967 static inline bool classof(const Instruction *I) {
1968 return I->getOpcode() == Instruction::ExtractValue;
1970 static inline bool classof(const Value *V) {
1971 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1975 ExtractValueInst::ExtractValueInst(Value *Agg,
1976 ArrayRef<unsigned> Idxs,
1977 const Twine &NameStr,
1978 Instruction *InsertBefore)
1979 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1980 ExtractValue, Agg, InsertBefore) {
1981 init(Idxs, NameStr);
1983 ExtractValueInst::ExtractValueInst(Value *Agg,
1984 ArrayRef<unsigned> Idxs,
1985 const Twine &NameStr,
1986 BasicBlock *InsertAtEnd)
1987 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1988 ExtractValue, Agg, InsertAtEnd) {
1989 init(Idxs, NameStr);
1993 //===----------------------------------------------------------------------===//
1994 // InsertValueInst Class
1995 //===----------------------------------------------------------------------===//
1997 /// InsertValueInst - This instruction inserts a struct field of array element
1998 /// value into an aggregate value.
2000 class InsertValueInst : public Instruction {
2001 SmallVector<unsigned, 4> Indices;
2003 void *operator new(size_t, unsigned) = delete;
2004 InsertValueInst(const InsertValueInst &IVI);
2005 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2006 const Twine &NameStr);
2008 /// Constructors - Create a insertvalue instruction with a base aggregate
2009 /// value, a value to insert, and a list of indices. The first ctor can
2010 /// optionally insert before an existing instruction, the second appends
2011 /// the new instruction to the specified BasicBlock.
2012 inline InsertValueInst(Value *Agg, Value *Val,
2013 ArrayRef<unsigned> Idxs,
2014 const Twine &NameStr,
2015 Instruction *InsertBefore);
2016 inline InsertValueInst(Value *Agg, Value *Val,
2017 ArrayRef<unsigned> Idxs,
2018 const Twine &NameStr, BasicBlock *InsertAtEnd);
2020 /// Constructors - These two constructors are convenience methods because one
2021 /// and two index insertvalue instructions are so common.
2022 InsertValueInst(Value *Agg, Value *Val,
2023 unsigned Idx, const Twine &NameStr = "",
2024 Instruction *InsertBefore = nullptr);
2025 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2026 const Twine &NameStr, BasicBlock *InsertAtEnd);
2028 InsertValueInst *clone_impl() const override;
2030 // allocate space for exactly two operands
2031 void *operator new(size_t s) {
2032 return User::operator new(s, 2);
2035 static InsertValueInst *Create(Value *Agg, Value *Val,
2036 ArrayRef<unsigned> Idxs,
2037 const Twine &NameStr = "",
2038 Instruction *InsertBefore = nullptr) {
2039 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2041 static InsertValueInst *Create(Value *Agg, Value *Val,
2042 ArrayRef<unsigned> Idxs,
2043 const Twine &NameStr,
2044 BasicBlock *InsertAtEnd) {
2045 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2048 /// Transparently provide more efficient getOperand methods.
2049 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2051 typedef const unsigned* idx_iterator;
2052 inline idx_iterator idx_begin() const { return Indices.begin(); }
2053 inline idx_iterator idx_end() const { return Indices.end(); }
2054 inline iterator_range<idx_iterator> indices() const {
2055 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2058 Value *getAggregateOperand() {
2059 return getOperand(0);
2061 const Value *getAggregateOperand() const {
2062 return getOperand(0);
2064 static unsigned getAggregateOperandIndex() {
2065 return 0U; // get index for modifying correct operand
2068 Value *getInsertedValueOperand() {
2069 return getOperand(1);
2071 const Value *getInsertedValueOperand() const {
2072 return getOperand(1);
2074 static unsigned getInsertedValueOperandIndex() {
2075 return 1U; // get index for modifying correct operand
2078 ArrayRef<unsigned> getIndices() const {
2082 unsigned getNumIndices() const {
2083 return (unsigned)Indices.size();
2086 bool hasIndices() const {
2090 // Methods for support type inquiry through isa, cast, and dyn_cast:
2091 static inline bool classof(const Instruction *I) {
2092 return I->getOpcode() == Instruction::InsertValue;
2094 static inline bool classof(const Value *V) {
2095 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2100 struct OperandTraits<InsertValueInst> :
2101 public FixedNumOperandTraits<InsertValueInst, 2> {
2104 InsertValueInst::InsertValueInst(Value *Agg,
2106 ArrayRef<unsigned> Idxs,
2107 const Twine &NameStr,
2108 Instruction *InsertBefore)
2109 : Instruction(Agg->getType(), InsertValue,
2110 OperandTraits<InsertValueInst>::op_begin(this),
2112 init(Agg, Val, Idxs, NameStr);
2114 InsertValueInst::InsertValueInst(Value *Agg,
2116 ArrayRef<unsigned> Idxs,
2117 const Twine &NameStr,
2118 BasicBlock *InsertAtEnd)
2119 : Instruction(Agg->getType(), InsertValue,
2120 OperandTraits<InsertValueInst>::op_begin(this),
2122 init(Agg, Val, Idxs, NameStr);
2125 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2127 //===----------------------------------------------------------------------===//
2129 //===----------------------------------------------------------------------===//
2131 // PHINode - The PHINode class is used to represent the magical mystical PHI
2132 // node, that can not exist in nature, but can be synthesized in a computer
2133 // scientist's overactive imagination.
2135 class PHINode : public Instruction {
2136 void *operator new(size_t, unsigned) = delete;
2137 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2138 /// the number actually in use.
2139 unsigned ReservedSpace;
2140 PHINode(const PHINode &PN);
2141 // allocate space for exactly zero operands
2142 void *operator new(size_t s) {
2143 return User::operator new(s, 0);
2145 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2146 const Twine &NameStr = "",
2147 Instruction *InsertBefore = nullptr)
2148 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2149 ReservedSpace(NumReservedValues) {
2151 OperandList = allocHungoffUses(ReservedSpace);
2154 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2155 BasicBlock *InsertAtEnd)
2156 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2157 ReservedSpace(NumReservedValues) {
2159 OperandList = allocHungoffUses(ReservedSpace);
2162 // allocHungoffUses - this is more complicated than the generic
2163 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2164 // values and pointers to the incoming blocks, all in one allocation.
2165 Use *allocHungoffUses(unsigned) const;
2167 PHINode *clone_impl() const override;
2169 /// Constructors - NumReservedValues is a hint for the number of incoming
2170 /// edges that this phi node will have (use 0 if you really have no idea).
2171 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2172 const Twine &NameStr = "",
2173 Instruction *InsertBefore = nullptr) {
2174 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2176 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2177 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2178 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2182 /// Provide fast operand accessors
2183 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2185 // Block iterator interface. This provides access to the list of incoming
2186 // basic blocks, which parallels the list of incoming values.
2188 typedef BasicBlock **block_iterator;
2189 typedef BasicBlock * const *const_block_iterator;
2191 block_iterator block_begin() {
2193 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2194 return reinterpret_cast<block_iterator>(ref + 1);
2197 const_block_iterator block_begin() const {
2198 const Use::UserRef *ref =
2199 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2200 return reinterpret_cast<const_block_iterator>(ref + 1);
2203 block_iterator block_end() {
2204 return block_begin() + getNumOperands();
2207 const_block_iterator block_end() const {
2208 return block_begin() + getNumOperands();
2211 op_range incoming_values() { return operands(); }
2213 /// getNumIncomingValues - Return the number of incoming edges
2215 unsigned getNumIncomingValues() const { return getNumOperands(); }
2217 /// getIncomingValue - Return incoming value number x
2219 Value *getIncomingValue(unsigned i) const {
2220 return getOperand(i);
2222 void setIncomingValue(unsigned i, Value *V) {
2225 static unsigned getOperandNumForIncomingValue(unsigned i) {
2228 static unsigned getIncomingValueNumForOperand(unsigned i) {
2232 /// getIncomingBlock - Return incoming basic block number @p i.
2234 BasicBlock *getIncomingBlock(unsigned i) const {
2235 return block_begin()[i];
2238 /// getIncomingBlock - Return incoming basic block corresponding
2239 /// to an operand of the PHI.
2241 BasicBlock *getIncomingBlock(const Use &U) const {
2242 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2243 return getIncomingBlock(unsigned(&U - op_begin()));
2246 /// getIncomingBlock - Return incoming basic block corresponding
2247 /// to value use iterator.
2249 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2250 return getIncomingBlock(I.getUse());
2253 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2254 block_begin()[i] = BB;
2257 /// addIncoming - Add an incoming value to the end of the PHI list
2259 void addIncoming(Value *V, BasicBlock *BB) {
2260 assert(V && "PHI node got a null value!");
2261 assert(BB && "PHI node got a null basic block!");
2262 assert(getType() == V->getType() &&
2263 "All operands to PHI node must be the same type as the PHI node!");
2264 if (NumOperands == ReservedSpace)
2265 growOperands(); // Get more space!
2266 // Initialize some new operands.
2268 setIncomingValue(NumOperands - 1, V);
2269 setIncomingBlock(NumOperands - 1, BB);
2272 /// removeIncomingValue - Remove an incoming value. This is useful if a
2273 /// predecessor basic block is deleted. The value removed is returned.
2275 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2276 /// is true), the PHI node is destroyed and any uses of it are replaced with
2277 /// dummy values. The only time there should be zero incoming values to a PHI
2278 /// node is when the block is dead, so this strategy is sound.
2280 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2282 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2283 int Idx = getBasicBlockIndex(BB);
2284 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2285 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2288 /// getBasicBlockIndex - Return the first index of the specified basic
2289 /// block in the value list for this PHI. Returns -1 if no instance.
2291 int getBasicBlockIndex(const BasicBlock *BB) const {
2292 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2293 if (block_begin()[i] == BB)
2298 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2299 int Idx = getBasicBlockIndex(BB);
2300 assert(Idx >= 0 && "Invalid basic block argument!");
2301 return getIncomingValue(Idx);
2304 /// hasConstantValue - If the specified PHI node always merges together the
2305 /// same value, return the value, otherwise return null.
2306 Value *hasConstantValue() const;
2308 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2309 static inline bool classof(const Instruction *I) {
2310 return I->getOpcode() == Instruction::PHI;
2312 static inline bool classof(const Value *V) {
2313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2316 void growOperands();
2320 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2323 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2325 //===----------------------------------------------------------------------===//
2326 // LandingPadInst Class
2327 //===----------------------------------------------------------------------===//
2329 //===---------------------------------------------------------------------------
2330 /// LandingPadInst - The landingpad instruction holds all of the information
2331 /// necessary to generate correct exception handling. The landingpad instruction
2332 /// cannot be moved from the top of a landing pad block, which itself is
2333 /// accessible only from the 'unwind' edge of an invoke. This uses the
2334 /// SubclassData field in Value to store whether or not the landingpad is a
2337 class LandingPadInst : public Instruction {
2338 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2339 /// the number actually in use.
2340 unsigned ReservedSpace;
2341 LandingPadInst(const LandingPadInst &LP);
2343 enum ClauseType { Catch, Filter };
2345 void *operator new(size_t, unsigned) = delete;
2346 // Allocate space for exactly zero operands.
2347 void *operator new(size_t s) {
2348 return User::operator new(s, 0);
2350 void growOperands(unsigned Size);
2351 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2353 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2354 unsigned NumReservedValues, const Twine &NameStr,
2355 Instruction *InsertBefore);
2356 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2357 unsigned NumReservedValues, const Twine &NameStr,
2358 BasicBlock *InsertAtEnd);
2360 LandingPadInst *clone_impl() const override;
2362 /// Constructors - NumReservedClauses is a hint for the number of incoming
2363 /// clauses that this landingpad will have (use 0 if you really have no idea).
2364 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2365 unsigned NumReservedClauses,
2366 const Twine &NameStr = "",
2367 Instruction *InsertBefore = nullptr);
2368 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2369 unsigned NumReservedClauses,
2370 const Twine &NameStr, BasicBlock *InsertAtEnd);
2373 /// Provide fast operand accessors
2374 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2376 /// getPersonalityFn - Get the personality function associated with this
2378 Value *getPersonalityFn() const { return getOperand(0); }
2380 /// isCleanup - Return 'true' if this landingpad instruction is a
2381 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2382 /// doesn't catch the exception.
2383 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2385 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2386 void setCleanup(bool V) {
2387 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2391 /// Add a catch or filter clause to the landing pad.
2392 void addClause(Constant *ClauseVal);
2394 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2395 /// determine what type of clause this is.
2396 Constant *getClause(unsigned Idx) const {
2397 return cast<Constant>(OperandList[Idx + 1]);
2400 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2401 bool isCatch(unsigned Idx) const {
2402 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2405 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2406 bool isFilter(unsigned Idx) const {
2407 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2410 /// getNumClauses - Get the number of clauses for this landing pad.
2411 unsigned getNumClauses() const { return getNumOperands() - 1; }
2413 /// reserveClauses - Grow the size of the operand list to accommodate the new
2414 /// number of clauses.
2415 void reserveClauses(unsigned Size) { growOperands(Size); }
2417 // Methods for support type inquiry through isa, cast, and dyn_cast:
2418 static inline bool classof(const Instruction *I) {
2419 return I->getOpcode() == Instruction::LandingPad;
2421 static inline bool classof(const Value *V) {
2422 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2427 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2430 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2432 //===----------------------------------------------------------------------===//
2434 //===----------------------------------------------------------------------===//
2436 //===---------------------------------------------------------------------------
2437 /// ReturnInst - Return a value (possibly void), from a function. Execution
2438 /// does not continue in this function any longer.
2440 class ReturnInst : public TerminatorInst {
2441 ReturnInst(const ReturnInst &RI);
2444 // ReturnInst constructors:
2445 // ReturnInst() - 'ret void' instruction
2446 // ReturnInst( null) - 'ret void' instruction
2447 // ReturnInst(Value* X) - 'ret X' instruction
2448 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2449 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2450 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2451 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2453 // NOTE: If the Value* passed is of type void then the constructor behaves as
2454 // if it was passed NULL.
2455 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2456 Instruction *InsertBefore = nullptr);
2457 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2458 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2460 ReturnInst *clone_impl() const override;
2462 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2463 Instruction *InsertBefore = nullptr) {
2464 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2466 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2467 BasicBlock *InsertAtEnd) {
2468 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2470 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2471 return new(0) ReturnInst(C, InsertAtEnd);
2473 virtual ~ReturnInst();
2475 /// Provide fast operand accessors
2476 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2478 /// Convenience accessor. Returns null if there is no return value.
2479 Value *getReturnValue() const {
2480 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2483 unsigned getNumSuccessors() const { return 0; }
2485 // Methods for support type inquiry through isa, cast, and dyn_cast:
2486 static inline bool classof(const Instruction *I) {
2487 return (I->getOpcode() == Instruction::Ret);
2489 static inline bool classof(const Value *V) {
2490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2493 BasicBlock *getSuccessorV(unsigned idx) const override;
2494 unsigned getNumSuccessorsV() const override;
2495 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2499 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2502 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2504 //===----------------------------------------------------------------------===//
2506 //===----------------------------------------------------------------------===//
2508 //===---------------------------------------------------------------------------
2509 /// BranchInst - Conditional or Unconditional Branch instruction.
2511 class BranchInst : public TerminatorInst {
2512 /// Ops list - Branches are strange. The operands are ordered:
2513 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2514 /// they don't have to check for cond/uncond branchness. These are mostly
2515 /// accessed relative from op_end().
2516 BranchInst(const BranchInst &BI);
2518 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2519 // BranchInst(BB *B) - 'br B'
2520 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2521 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2522 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2523 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2524 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2525 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2526 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2527 Instruction *InsertBefore = nullptr);
2528 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2529 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2530 BasicBlock *InsertAtEnd);
2532 BranchInst *clone_impl() const override;
2534 static BranchInst *Create(BasicBlock *IfTrue,
2535 Instruction *InsertBefore = nullptr) {
2536 return new(1) BranchInst(IfTrue, InsertBefore);
2538 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2539 Value *Cond, Instruction *InsertBefore = nullptr) {
2540 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2542 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2543 return new(1) BranchInst(IfTrue, InsertAtEnd);
2545 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2546 Value *Cond, BasicBlock *InsertAtEnd) {
2547 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2550 /// Transparently provide more efficient getOperand methods.
2551 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2553 bool isUnconditional() const { return getNumOperands() == 1; }
2554 bool isConditional() const { return getNumOperands() == 3; }
2556 Value *getCondition() const {
2557 assert(isConditional() && "Cannot get condition of an uncond branch!");
2561 void setCondition(Value *V) {
2562 assert(isConditional() && "Cannot set condition of unconditional branch!");
2566 unsigned getNumSuccessors() const { return 1+isConditional(); }
2568 BasicBlock *getSuccessor(unsigned i) const {
2569 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2570 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2573 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2574 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2575 *(&Op<-1>() - idx) = (Value*)NewSucc;
2578 /// \brief Swap the successors of this branch instruction.
2580 /// Swaps the successors of the branch instruction. This also swaps any
2581 /// branch weight metadata associated with the instruction so that it
2582 /// continues to map correctly to each operand.
2583 void swapSuccessors();
2585 // Methods for support type inquiry through isa, cast, and dyn_cast:
2586 static inline bool classof(const Instruction *I) {
2587 return (I->getOpcode() == Instruction::Br);
2589 static inline bool classof(const Value *V) {
2590 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2593 BasicBlock *getSuccessorV(unsigned idx) const override;
2594 unsigned getNumSuccessorsV() const override;
2595 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2599 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2602 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2604 //===----------------------------------------------------------------------===//
2606 //===----------------------------------------------------------------------===//
2608 //===---------------------------------------------------------------------------
2609 /// SwitchInst - Multiway switch
2611 class SwitchInst : public TerminatorInst {
2612 void *operator new(size_t, unsigned) = delete;
2613 unsigned ReservedSpace;
2614 // Operand[0] = Value to switch on
2615 // Operand[1] = Default basic block destination
2616 // Operand[2n ] = Value to match
2617 // Operand[2n+1] = BasicBlock to go to on match
2618 SwitchInst(const SwitchInst &SI);
2619 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2620 void growOperands();
2621 // allocate space for exactly zero operands
2622 void *operator new(size_t s) {
2623 return User::operator new(s, 0);
2625 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2626 /// switch on and a default destination. The number of additional cases can
2627 /// be specified here to make memory allocation more efficient. This
2628 /// constructor can also autoinsert before another instruction.
2629 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2630 Instruction *InsertBefore);
2632 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2633 /// switch on and a default destination. The number of additional cases can
2634 /// be specified here to make memory allocation more efficient. This
2635 /// constructor also autoinserts at the end of the specified BasicBlock.
2636 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2637 BasicBlock *InsertAtEnd);
2639 SwitchInst *clone_impl() const override;
2643 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2645 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2646 class CaseIteratorT {
2654 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2656 /// Initializes case iterator for given SwitchInst and for given
2658 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2663 /// Initializes case iterator for given SwitchInst and for given
2664 /// TerminatorInst's successor index.
2665 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2666 assert(SuccessorIndex < SI->getNumSuccessors() &&
2667 "Successor index # out of range!");
2668 return SuccessorIndex != 0 ?
2669 Self(SI, SuccessorIndex - 1) :
2670 Self(SI, DefaultPseudoIndex);
2673 /// Resolves case value for current case.
2674 ConstantIntTy *getCaseValue() {
2675 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2676 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2679 /// Resolves successor for current case.
2680 BasicBlockTy *getCaseSuccessor() {
2681 assert((Index < SI->getNumCases() ||
2682 Index == DefaultPseudoIndex) &&
2683 "Index out the number of cases.");
2684 return SI->getSuccessor(getSuccessorIndex());
2687 /// Returns number of current case.
2688 unsigned getCaseIndex() const { return Index; }
2690 /// Returns TerminatorInst's successor index for current case successor.
2691 unsigned getSuccessorIndex() const {
2692 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2693 "Index out the number of cases.");
2694 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2698 // Check index correctness after increment.
2699 // Note: Index == getNumCases() means end().
2700 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2704 Self operator++(int) {
2710 // Check index correctness after decrement.
2711 // Note: Index == getNumCases() means end().
2712 // Also allow "-1" iterator here. That will became valid after ++.
2713 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2714 "Index out the number of cases.");
2718 Self operator--(int) {
2723 bool operator==(const Self& RHS) const {
2724 assert(RHS.SI == SI && "Incompatible operators.");
2725 return RHS.Index == Index;
2727 bool operator!=(const Self& RHS) const {
2728 assert(RHS.SI == SI && "Incompatible operators.");
2729 return RHS.Index != Index;
2736 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2739 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2741 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2745 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2746 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2748 /// Sets the new value for current case.
2749 void setValue(ConstantInt *V) {
2750 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2751 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2754 /// Sets the new successor for current case.
2755 void setSuccessor(BasicBlock *S) {
2756 SI->setSuccessor(getSuccessorIndex(), S);
2760 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2762 Instruction *InsertBefore = nullptr) {
2763 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2765 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2766 unsigned NumCases, BasicBlock *InsertAtEnd) {
2767 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2772 /// Provide fast operand accessors
2773 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2775 // Accessor Methods for Switch stmt
2776 Value *getCondition() const { return getOperand(0); }
2777 void setCondition(Value *V) { setOperand(0, V); }
2779 BasicBlock *getDefaultDest() const {
2780 return cast<BasicBlock>(getOperand(1));
2783 void setDefaultDest(BasicBlock *DefaultCase) {
2784 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2787 /// getNumCases - return the number of 'cases' in this switch instruction,
2788 /// except the default case
2789 unsigned getNumCases() const {
2790 return getNumOperands()/2 - 1;
2793 /// Returns a read/write iterator that points to the first
2794 /// case in SwitchInst.
2795 CaseIt case_begin() {
2796 return CaseIt(this, 0);
2798 /// Returns a read-only iterator that points to the first
2799 /// case in the SwitchInst.
2800 ConstCaseIt case_begin() const {
2801 return ConstCaseIt(this, 0);
2804 /// Returns a read/write iterator that points one past the last
2805 /// in the SwitchInst.
2807 return CaseIt(this, getNumCases());
2809 /// Returns a read-only iterator that points one past the last
2810 /// in the SwitchInst.
2811 ConstCaseIt case_end() const {
2812 return ConstCaseIt(this, getNumCases());
2815 /// cases - iteration adapter for range-for loops.
2816 iterator_range<CaseIt> cases() {
2817 return iterator_range<CaseIt>(case_begin(), case_end());
2820 /// cases - iteration adapter for range-for loops.
2821 iterator_range<ConstCaseIt> cases() const {
2822 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2825 /// Returns an iterator that points to the default case.
2826 /// Note: this iterator allows to resolve successor only. Attempt
2827 /// to resolve case value causes an assertion.
2828 /// Also note, that increment and decrement also causes an assertion and
2829 /// makes iterator invalid.
2830 CaseIt case_default() {
2831 return CaseIt(this, DefaultPseudoIndex);
2833 ConstCaseIt case_default() const {
2834 return ConstCaseIt(this, DefaultPseudoIndex);
2837 /// findCaseValue - Search all of the case values for the specified constant.
2838 /// If it is explicitly handled, return the case iterator of it, otherwise
2839 /// return default case iterator to indicate
2840 /// that it is handled by the default handler.
2841 CaseIt findCaseValue(const ConstantInt *C) {
2842 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2843 if (i.getCaseValue() == C)
2845 return case_default();
2847 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2848 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2849 if (i.getCaseValue() == C)
2851 return case_default();
2854 /// findCaseDest - Finds the unique case value for a given successor. Returns
2855 /// null if the successor is not found, not unique, or is the default case.
2856 ConstantInt *findCaseDest(BasicBlock *BB) {
2857 if (BB == getDefaultDest()) return nullptr;
2859 ConstantInt *CI = nullptr;
2860 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2861 if (i.getCaseSuccessor() == BB) {
2862 if (CI) return nullptr; // Multiple cases lead to BB.
2863 else CI = i.getCaseValue();
2869 /// addCase - Add an entry to the switch instruction...
2871 /// This action invalidates case_end(). Old case_end() iterator will
2872 /// point to the added case.
2873 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2875 /// removeCase - This method removes the specified case and its successor
2876 /// from the switch instruction. Note that this operation may reorder the
2877 /// remaining cases at index idx and above.
2879 /// This action invalidates iterators for all cases following the one removed,
2880 /// including the case_end() iterator.
2881 void removeCase(CaseIt i);
2883 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2884 BasicBlock *getSuccessor(unsigned idx) const {
2885 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2886 return cast<BasicBlock>(getOperand(idx*2+1));
2888 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2889 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2890 setOperand(idx*2+1, (Value*)NewSucc);
2893 // Methods for support type inquiry through isa, cast, and dyn_cast:
2894 static inline bool classof(const Instruction *I) {
2895 return I->getOpcode() == Instruction::Switch;
2897 static inline bool classof(const Value *V) {
2898 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2901 BasicBlock *getSuccessorV(unsigned idx) const override;
2902 unsigned getNumSuccessorsV() const override;
2903 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2907 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2910 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2913 //===----------------------------------------------------------------------===//
2914 // IndirectBrInst Class
2915 //===----------------------------------------------------------------------===//
2917 //===---------------------------------------------------------------------------
2918 /// IndirectBrInst - Indirect Branch Instruction.
2920 class IndirectBrInst : public TerminatorInst {
2921 void *operator new(size_t, unsigned) = delete;
2922 unsigned ReservedSpace;
2923 // Operand[0] = Value to switch on
2924 // Operand[1] = Default basic block destination
2925 // Operand[2n ] = Value to match
2926 // Operand[2n+1] = BasicBlock to go to on match
2927 IndirectBrInst(const IndirectBrInst &IBI);
2928 void init(Value *Address, unsigned NumDests);
2929 void growOperands();
2930 // allocate space for exactly zero operands
2931 void *operator new(size_t s) {
2932 return User::operator new(s, 0);
2934 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2935 /// Address to jump to. The number of expected destinations can be specified
2936 /// here to make memory allocation more efficient. This constructor can also
2937 /// autoinsert before another instruction.
2938 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2940 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2941 /// Address to jump to. The number of expected destinations can be specified
2942 /// here to make memory allocation more efficient. This constructor also
2943 /// autoinserts at the end of the specified BasicBlock.
2944 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2946 IndirectBrInst *clone_impl() const override;
2948 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2949 Instruction *InsertBefore = nullptr) {
2950 return new IndirectBrInst(Address, NumDests, InsertBefore);
2952 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2953 BasicBlock *InsertAtEnd) {
2954 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2958 /// Provide fast operand accessors.
2959 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2961 // Accessor Methods for IndirectBrInst instruction.
2962 Value *getAddress() { return getOperand(0); }
2963 const Value *getAddress() const { return getOperand(0); }
2964 void setAddress(Value *V) { setOperand(0, V); }
2967 /// getNumDestinations - return the number of possible destinations in this
2968 /// indirectbr instruction.
2969 unsigned getNumDestinations() const { return getNumOperands()-1; }
2971 /// getDestination - Return the specified destination.
2972 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2973 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2975 /// addDestination - Add a destination.
2977 void addDestination(BasicBlock *Dest);
2979 /// removeDestination - This method removes the specified successor from the
2980 /// indirectbr instruction.
2981 void removeDestination(unsigned i);
2983 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2984 BasicBlock *getSuccessor(unsigned i) const {
2985 return cast<BasicBlock>(getOperand(i+1));
2987 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2988 setOperand(i+1, (Value*)NewSucc);
2991 // Methods for support type inquiry through isa, cast, and dyn_cast:
2992 static inline bool classof(const Instruction *I) {
2993 return I->getOpcode() == Instruction::IndirectBr;
2995 static inline bool classof(const Value *V) {
2996 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2999 BasicBlock *getSuccessorV(unsigned idx) const override;
3000 unsigned getNumSuccessorsV() const override;
3001 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3005 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3008 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3011 //===----------------------------------------------------------------------===//
3013 //===----------------------------------------------------------------------===//
3015 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3016 /// calling convention of the call.
3018 class InvokeInst : public TerminatorInst {
3019 AttributeSet AttributeList;
3020 InvokeInst(const InvokeInst &BI);
3021 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3022 ArrayRef<Value *> Args, const Twine &NameStr);
3024 /// Construct an InvokeInst given a range of arguments.
3026 /// \brief Construct an InvokeInst from a range of arguments
3027 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3028 ArrayRef<Value *> Args, unsigned Values,
3029 const Twine &NameStr, Instruction *InsertBefore);
3031 /// Construct an InvokeInst given a range of arguments.
3033 /// \brief Construct an InvokeInst from a range of arguments
3034 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3035 ArrayRef<Value *> Args, unsigned Values,
3036 const Twine &NameStr, BasicBlock *InsertAtEnd);
3038 InvokeInst *clone_impl() const override;
3040 static InvokeInst *Create(Value *Func,
3041 BasicBlock *IfNormal, BasicBlock *IfException,
3042 ArrayRef<Value *> Args, const Twine &NameStr = "",
3043 Instruction *InsertBefore = nullptr) {
3044 unsigned Values = unsigned(Args.size()) + 3;
3045 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3046 Values, NameStr, InsertBefore);
3048 static InvokeInst *Create(Value *Func,
3049 BasicBlock *IfNormal, BasicBlock *IfException,
3050 ArrayRef<Value *> Args, const Twine &NameStr,
3051 BasicBlock *InsertAtEnd) {
3052 unsigned Values = unsigned(Args.size()) + 3;
3053 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3054 Values, NameStr, InsertAtEnd);
3057 /// Provide fast operand accessors
3058 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3060 /// getNumArgOperands - Return the number of invoke arguments.
3062 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3064 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3066 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3067 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3069 /// arg_operands - iteration adapter for range-for loops.
3070 iterator_range<op_iterator> arg_operands() {
3071 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3074 /// arg_operands - iteration adapter for range-for loops.
3075 iterator_range<const_op_iterator> arg_operands() const {
3076 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3079 /// \brief Wrappers for getting the \c Use of a invoke argument.
3080 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3081 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3083 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3085 CallingConv::ID getCallingConv() const {
3086 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3088 void setCallingConv(CallingConv::ID CC) {
3089 setInstructionSubclassData(static_cast<unsigned>(CC));
3092 /// getAttributes - Return the parameter attributes for this invoke.
3094 const AttributeSet &getAttributes() const { return AttributeList; }
3096 /// setAttributes - Set the parameter attributes for this invoke.
3098 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3100 /// addAttribute - adds the attribute to the list of attributes.
3101 void addAttribute(unsigned i, Attribute::AttrKind attr);
3103 /// removeAttribute - removes the attribute from the list of attributes.
3104 void removeAttribute(unsigned i, Attribute attr);
3106 /// \brief removes the dereferenceable attribute to the list of attributes.
3107 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3109 /// \brief Determine whether this call has the given attribute.
3110 bool hasFnAttr(Attribute::AttrKind A) const {
3111 assert(A != Attribute::NoBuiltin &&
3112 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3113 return hasFnAttrImpl(A);
3116 /// \brief Determine whether the call or the callee has the given attributes.
3117 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3119 /// \brief Extract the alignment for a call or parameter (0=unknown).
3120 unsigned getParamAlignment(unsigned i) const {
3121 return AttributeList.getParamAlignment(i);
3124 /// \brief Extract the number of dereferenceable bytes for a call or
3125 /// parameter (0=unknown).
3126 uint64_t getDereferenceableBytes(unsigned i) const {
3127 return AttributeList.getDereferenceableBytes(i);
3130 /// \brief Return true if the call should not be treated as a call to a
3132 bool isNoBuiltin() const {
3133 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3134 // to check it by hand.
3135 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3136 !hasFnAttrImpl(Attribute::Builtin);
3139 /// \brief Return true if the call should not be inlined.
3140 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3141 void setIsNoInline() {
3142 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3145 /// \brief Determine if the call does not access memory.
3146 bool doesNotAccessMemory() const {
3147 return hasFnAttr(Attribute::ReadNone);
3149 void setDoesNotAccessMemory() {
3150 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3153 /// \brief Determine if the call does not access or only reads memory.
3154 bool onlyReadsMemory() const {
3155 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3157 void setOnlyReadsMemory() {
3158 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3161 /// \brief Determine if the call cannot return.
3162 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3163 void setDoesNotReturn() {
3164 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3167 /// \brief Determine if the call cannot unwind.
3168 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3169 void setDoesNotThrow() {
3170 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3173 /// \brief Determine if the invoke cannot be duplicated.
3174 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3175 void setCannotDuplicate() {
3176 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3179 /// \brief Determine if the call returns a structure through first
3180 /// pointer argument.
3181 bool hasStructRetAttr() const {
3182 // Be friendly and also check the callee.
3183 return paramHasAttr(1, Attribute::StructRet);
3186 /// \brief Determine if any call argument is an aggregate passed by value.
3187 bool hasByValArgument() const {
3188 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3191 /// getCalledFunction - Return the function called, or null if this is an
3192 /// indirect function invocation.
3194 Function *getCalledFunction() const {
3195 return dyn_cast<Function>(Op<-3>());
3198 /// getCalledValue - Get a pointer to the function that is invoked by this
3200 const Value *getCalledValue() const { return Op<-3>(); }
3201 Value *getCalledValue() { return Op<-3>(); }
3203 /// setCalledFunction - Set the function called.
3204 void setCalledFunction(Value* Fn) {
3208 // get*Dest - Return the destination basic blocks...
3209 BasicBlock *getNormalDest() const {
3210 return cast<BasicBlock>(Op<-2>());
3212 BasicBlock *getUnwindDest() const {
3213 return cast<BasicBlock>(Op<-1>());
3215 void setNormalDest(BasicBlock *B) {
3216 Op<-2>() = reinterpret_cast<Value*>(B);
3218 void setUnwindDest(BasicBlock *B) {
3219 Op<-1>() = reinterpret_cast<Value*>(B);
3222 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3223 /// block (the unwind destination).
3224 LandingPadInst *getLandingPadInst() const;
3226 BasicBlock *getSuccessor(unsigned i) const {
3227 assert(i < 2 && "Successor # out of range for invoke!");
3228 return i == 0 ? getNormalDest() : getUnwindDest();
3231 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3232 assert(idx < 2 && "Successor # out of range for invoke!");
3233 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3236 unsigned getNumSuccessors() const { return 2; }
3238 // Methods for support type inquiry through isa, cast, and dyn_cast:
3239 static inline bool classof(const Instruction *I) {
3240 return (I->getOpcode() == Instruction::Invoke);
3242 static inline bool classof(const Value *V) {
3243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3247 BasicBlock *getSuccessorV(unsigned idx) const override;
3248 unsigned getNumSuccessorsV() const override;
3249 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3251 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3253 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3254 // method so that subclasses cannot accidentally use it.
3255 void setInstructionSubclassData(unsigned short D) {
3256 Instruction::setInstructionSubclassData(D);
3261 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3264 InvokeInst::InvokeInst(Value *Func,
3265 BasicBlock *IfNormal, BasicBlock *IfException,
3266 ArrayRef<Value *> Args, unsigned Values,
3267 const Twine &NameStr, Instruction *InsertBefore)
3268 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3269 ->getElementType())->getReturnType(),
3270 Instruction::Invoke,
3271 OperandTraits<InvokeInst>::op_end(this) - Values,
3272 Values, InsertBefore) {
3273 init(Func, IfNormal, IfException, Args, NameStr);
3275 InvokeInst::InvokeInst(Value *Func,
3276 BasicBlock *IfNormal, BasicBlock *IfException,
3277 ArrayRef<Value *> Args, unsigned Values,
3278 const Twine &NameStr, BasicBlock *InsertAtEnd)
3279 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3280 ->getElementType())->getReturnType(),
3281 Instruction::Invoke,
3282 OperandTraits<InvokeInst>::op_end(this) - Values,
3283 Values, InsertAtEnd) {
3284 init(Func, IfNormal, IfException, Args, NameStr);
3287 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3289 //===----------------------------------------------------------------------===//
3291 //===----------------------------------------------------------------------===//
3293 //===---------------------------------------------------------------------------
3294 /// ResumeInst - Resume the propagation of an exception.
3296 class ResumeInst : public TerminatorInst {
3297 ResumeInst(const ResumeInst &RI);
3299 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3300 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3302 ResumeInst *clone_impl() const override;
3304 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3305 return new(1) ResumeInst(Exn, InsertBefore);
3307 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3308 return new(1) ResumeInst(Exn, InsertAtEnd);
3311 /// Provide fast operand accessors
3312 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3314 /// Convenience accessor.
3315 Value *getValue() const { return Op<0>(); }
3317 unsigned getNumSuccessors() const { return 0; }
3319 // Methods for support type inquiry through isa, cast, and dyn_cast:
3320 static inline bool classof(const Instruction *I) {
3321 return I->getOpcode() == Instruction::Resume;
3323 static inline bool classof(const Value *V) {
3324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3327 BasicBlock *getSuccessorV(unsigned idx) const override;
3328 unsigned getNumSuccessorsV() const override;
3329 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3333 struct OperandTraits<ResumeInst> :
3334 public FixedNumOperandTraits<ResumeInst, 1> {
3337 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3339 //===----------------------------------------------------------------------===//
3340 // UnreachableInst Class
3341 //===----------------------------------------------------------------------===//
3343 //===---------------------------------------------------------------------------
3344 /// UnreachableInst - This function has undefined behavior. In particular, the
3345 /// presence of this instruction indicates some higher level knowledge that the
3346 /// end of the block cannot be reached.
3348 class UnreachableInst : public TerminatorInst {
3349 void *operator new(size_t, unsigned) = delete;
3351 UnreachableInst *clone_impl() const override;
3354 // allocate space for exactly zero operands
3355 void *operator new(size_t s) {
3356 return User::operator new(s, 0);
3358 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3359 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3361 unsigned getNumSuccessors() const { return 0; }
3363 // Methods for support type inquiry through isa, cast, and dyn_cast:
3364 static inline bool classof(const Instruction *I) {
3365 return I->getOpcode() == Instruction::Unreachable;
3367 static inline bool classof(const Value *V) {
3368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3371 BasicBlock *getSuccessorV(unsigned idx) const override;
3372 unsigned getNumSuccessorsV() const override;
3373 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3376 //===----------------------------------------------------------------------===//
3378 //===----------------------------------------------------------------------===//
3380 /// \brief This class represents a truncation of integer types.
3381 class TruncInst : public CastInst {
3383 /// \brief Clone an identical TruncInst
3384 TruncInst *clone_impl() const override;
3387 /// \brief Constructor with insert-before-instruction semantics
3389 Value *S, ///< The value to be truncated
3390 Type *Ty, ///< The (smaller) type to truncate to
3391 const Twine &NameStr = "", ///< A name for the new instruction
3392 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3395 /// \brief Constructor with insert-at-end-of-block semantics
3397 Value *S, ///< The value to be truncated
3398 Type *Ty, ///< The (smaller) type to truncate to
3399 const Twine &NameStr, ///< A name for the new instruction
3400 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3403 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3404 static inline bool classof(const Instruction *I) {
3405 return I->getOpcode() == Trunc;
3407 static inline bool classof(const Value *V) {
3408 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3412 //===----------------------------------------------------------------------===//
3414 //===----------------------------------------------------------------------===//
3416 /// \brief This class represents zero extension of integer types.
3417 class ZExtInst : public CastInst {
3419 /// \brief Clone an identical ZExtInst
3420 ZExtInst *clone_impl() const override;
3423 /// \brief Constructor with insert-before-instruction semantics
3425 Value *S, ///< The value to be zero extended
3426 Type *Ty, ///< The type to zero extend to
3427 const Twine &NameStr = "", ///< A name for the new instruction
3428 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3431 /// \brief Constructor with insert-at-end semantics.
3433 Value *S, ///< The value to be zero extended
3434 Type *Ty, ///< The type to zero extend to
3435 const Twine &NameStr, ///< A name for the new instruction
3436 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3439 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3440 static inline bool classof(const Instruction *I) {
3441 return I->getOpcode() == ZExt;
3443 static inline bool classof(const Value *V) {
3444 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3448 //===----------------------------------------------------------------------===//
3450 //===----------------------------------------------------------------------===//
3452 /// \brief This class represents a sign extension of integer types.
3453 class SExtInst : public CastInst {
3455 /// \brief Clone an identical SExtInst
3456 SExtInst *clone_impl() const override;
3459 /// \brief Constructor with insert-before-instruction semantics
3461 Value *S, ///< The value to be sign extended
3462 Type *Ty, ///< The type to sign extend to
3463 const Twine &NameStr = "", ///< A name for the new instruction
3464 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3467 /// \brief Constructor with insert-at-end-of-block semantics
3469 Value *S, ///< The value to be sign extended
3470 Type *Ty, ///< The type to sign extend to
3471 const Twine &NameStr, ///< A name for the new instruction
3472 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3475 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3476 static inline bool classof(const Instruction *I) {
3477 return I->getOpcode() == SExt;
3479 static inline bool classof(const Value *V) {
3480 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3484 //===----------------------------------------------------------------------===//
3485 // FPTruncInst Class
3486 //===----------------------------------------------------------------------===//
3488 /// \brief This class represents a truncation of floating point types.
3489 class FPTruncInst : public CastInst {
3491 /// \brief Clone an identical FPTruncInst
3492 FPTruncInst *clone_impl() const override;
3495 /// \brief Constructor with insert-before-instruction semantics
3497 Value *S, ///< The value to be truncated
3498 Type *Ty, ///< The type to truncate to
3499 const Twine &NameStr = "", ///< A name for the new instruction
3500 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3503 /// \brief Constructor with insert-before-instruction semantics
3505 Value *S, ///< The value to be truncated
3506 Type *Ty, ///< The type to truncate to
3507 const Twine &NameStr, ///< A name for the new instruction
3508 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3511 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3512 static inline bool classof(const Instruction *I) {
3513 return I->getOpcode() == FPTrunc;
3515 static inline bool classof(const Value *V) {
3516 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3520 //===----------------------------------------------------------------------===//
3522 //===----------------------------------------------------------------------===//
3524 /// \brief This class represents an extension of floating point types.
3525 class FPExtInst : public CastInst {
3527 /// \brief Clone an identical FPExtInst
3528 FPExtInst *clone_impl() const override;
3531 /// \brief Constructor with insert-before-instruction semantics
3533 Value *S, ///< The value to be extended
3534 Type *Ty, ///< The type to extend to
3535 const Twine &NameStr = "", ///< A name for the new instruction
3536 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3539 /// \brief Constructor with insert-at-end-of-block semantics
3541 Value *S, ///< The value to be extended
3542 Type *Ty, ///< The type to extend to
3543 const Twine &NameStr, ///< A name for the new instruction
3544 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3547 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3548 static inline bool classof(const Instruction *I) {
3549 return I->getOpcode() == FPExt;
3551 static inline bool classof(const Value *V) {
3552 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3556 //===----------------------------------------------------------------------===//
3558 //===----------------------------------------------------------------------===//
3560 /// \brief This class represents a cast unsigned integer to floating point.
3561 class UIToFPInst : public CastInst {
3563 /// \brief Clone an identical UIToFPInst
3564 UIToFPInst *clone_impl() const override;
3567 /// \brief Constructor with insert-before-instruction semantics
3569 Value *S, ///< The value to be converted
3570 Type *Ty, ///< The type to convert to
3571 const Twine &NameStr = "", ///< A name for the new instruction
3572 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3575 /// \brief Constructor with insert-at-end-of-block semantics
3577 Value *S, ///< The value to be converted
3578 Type *Ty, ///< The type to convert to
3579 const Twine &NameStr, ///< A name for the new instruction
3580 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3583 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3584 static inline bool classof(const Instruction *I) {
3585 return I->getOpcode() == UIToFP;
3587 static inline bool classof(const Value *V) {
3588 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3592 //===----------------------------------------------------------------------===//
3594 //===----------------------------------------------------------------------===//
3596 /// \brief This class represents a cast from signed integer to floating point.
3597 class SIToFPInst : public CastInst {
3599 /// \brief Clone an identical SIToFPInst
3600 SIToFPInst *clone_impl() const override;
3603 /// \brief Constructor with insert-before-instruction semantics
3605 Value *S, ///< The value to be converted
3606 Type *Ty, ///< The type to convert to
3607 const Twine &NameStr = "", ///< A name for the new instruction
3608 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3611 /// \brief Constructor with insert-at-end-of-block semantics
3613 Value *S, ///< The value to be converted
3614 Type *Ty, ///< The type to convert to
3615 const Twine &NameStr, ///< A name for the new instruction
3616 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3619 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3620 static inline bool classof(const Instruction *I) {
3621 return I->getOpcode() == SIToFP;
3623 static inline bool classof(const Value *V) {
3624 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3628 //===----------------------------------------------------------------------===//
3630 //===----------------------------------------------------------------------===//
3632 /// \brief This class represents a cast from floating point to unsigned integer
3633 class FPToUIInst : public CastInst {
3635 /// \brief Clone an identical FPToUIInst
3636 FPToUIInst *clone_impl() const override;
3639 /// \brief Constructor with insert-before-instruction semantics
3641 Value *S, ///< The value to be converted
3642 Type *Ty, ///< The type to convert to
3643 const Twine &NameStr = "", ///< A name for the new instruction
3644 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3647 /// \brief Constructor with insert-at-end-of-block semantics
3649 Value *S, ///< The value to be converted
3650 Type *Ty, ///< The type to convert to
3651 const Twine &NameStr, ///< A name for the new instruction
3652 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3655 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3656 static inline bool classof(const Instruction *I) {
3657 return I->getOpcode() == FPToUI;
3659 static inline bool classof(const Value *V) {
3660 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3664 //===----------------------------------------------------------------------===//
3666 //===----------------------------------------------------------------------===//
3668 /// \brief This class represents a cast from floating point to signed integer.
3669 class FPToSIInst : public CastInst {
3671 /// \brief Clone an identical FPToSIInst
3672 FPToSIInst *clone_impl() const override;
3675 /// \brief Constructor with insert-before-instruction semantics
3677 Value *S, ///< The value to be converted
3678 Type *Ty, ///< The type to convert to
3679 const Twine &NameStr = "", ///< A name for the new instruction
3680 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3683 /// \brief Constructor with insert-at-end-of-block semantics
3685 Value *S, ///< The value to be converted
3686 Type *Ty, ///< The type to convert to
3687 const Twine &NameStr, ///< A name for the new instruction
3688 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3691 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3692 static inline bool classof(const Instruction *I) {
3693 return I->getOpcode() == FPToSI;
3695 static inline bool classof(const Value *V) {
3696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3700 //===----------------------------------------------------------------------===//
3701 // IntToPtrInst Class
3702 //===----------------------------------------------------------------------===//
3704 /// \brief This class represents a cast from an integer to a pointer.
3705 class IntToPtrInst : public CastInst {
3707 /// \brief Constructor with insert-before-instruction semantics
3709 Value *S, ///< The value to be converted
3710 Type *Ty, ///< The type to convert to
3711 const Twine &NameStr = "", ///< A name for the new instruction
3712 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3715 /// \brief Constructor with insert-at-end-of-block semantics
3717 Value *S, ///< The value to be converted
3718 Type *Ty, ///< The type to convert to
3719 const Twine &NameStr, ///< A name for the new instruction
3720 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3723 /// \brief Clone an identical IntToPtrInst
3724 IntToPtrInst *clone_impl() const override;
3726 /// \brief Returns the address space of this instruction's pointer type.
3727 unsigned getAddressSpace() const {
3728 return getType()->getPointerAddressSpace();
3731 // Methods for support type inquiry through isa, cast, and dyn_cast:
3732 static inline bool classof(const Instruction *I) {
3733 return I->getOpcode() == IntToPtr;
3735 static inline bool classof(const Value *V) {
3736 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3740 //===----------------------------------------------------------------------===//
3741 // PtrToIntInst Class
3742 //===----------------------------------------------------------------------===//
3744 /// \brief This class represents a cast from a pointer to an integer
3745 class PtrToIntInst : public CastInst {
3747 /// \brief Clone an identical PtrToIntInst
3748 PtrToIntInst *clone_impl() const override;
3751 /// \brief Constructor with insert-before-instruction semantics
3753 Value *S, ///< The value to be converted
3754 Type *Ty, ///< The type to convert to
3755 const Twine &NameStr = "", ///< A name for the new instruction
3756 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3759 /// \brief Constructor with insert-at-end-of-block semantics
3761 Value *S, ///< The value to be converted
3762 Type *Ty, ///< The type to convert to
3763 const Twine &NameStr, ///< A name for the new instruction
3764 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3767 /// \brief Gets the pointer operand.
3768 Value *getPointerOperand() { return getOperand(0); }
3769 /// \brief Gets the pointer operand.
3770 const Value *getPointerOperand() const { return getOperand(0); }
3771 /// \brief Gets the operand index of the pointer operand.
3772 static unsigned getPointerOperandIndex() { return 0U; }
3774 /// \brief Returns the address space of the pointer operand.
3775 unsigned getPointerAddressSpace() const {
3776 return getPointerOperand()->getType()->getPointerAddressSpace();
3779 // Methods for support type inquiry through isa, cast, and dyn_cast:
3780 static inline bool classof(const Instruction *I) {
3781 return I->getOpcode() == PtrToInt;
3783 static inline bool classof(const Value *V) {
3784 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3788 //===----------------------------------------------------------------------===//
3789 // BitCastInst Class
3790 //===----------------------------------------------------------------------===//
3792 /// \brief This class represents a no-op cast from one type to another.
3793 class BitCastInst : public CastInst {
3795 /// \brief Clone an identical BitCastInst
3796 BitCastInst *clone_impl() const override;
3799 /// \brief Constructor with insert-before-instruction semantics
3801 Value *S, ///< The value to be casted
3802 Type *Ty, ///< The type to casted to
3803 const Twine &NameStr = "", ///< A name for the new instruction
3804 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3807 /// \brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3815 // Methods for support type inquiry through isa, cast, and dyn_cast:
3816 static inline bool classof(const Instruction *I) {
3817 return I->getOpcode() == BitCast;
3819 static inline bool classof(const Value *V) {
3820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3824 //===----------------------------------------------------------------------===//
3825 // AddrSpaceCastInst Class
3826 //===----------------------------------------------------------------------===//
3828 /// \brief This class represents a conversion between pointers from
3829 /// one address space to another.
3830 class AddrSpaceCastInst : public CastInst {
3832 /// \brief Clone an identical AddrSpaceCastInst
3833 AddrSpaceCastInst *clone_impl() const override;
3836 /// \brief Constructor with insert-before-instruction semantics
3838 Value *S, ///< The value to be casted
3839 Type *Ty, ///< The type to casted to
3840 const Twine &NameStr = "", ///< A name for the new instruction
3841 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3844 /// \brief Constructor with insert-at-end-of-block semantics
3846 Value *S, ///< The value to be casted
3847 Type *Ty, ///< The type to casted to
3848 const Twine &NameStr, ///< A name for the new instruction
3849 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3852 // Methods for support type inquiry through isa, cast, and dyn_cast:
3853 static inline bool classof(const Instruction *I) {
3854 return I->getOpcode() == AddrSpaceCast;
3856 static inline bool classof(const Value *V) {
3857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3861 } // End llvm namespace