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_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Support/IntegersSubset.h"
24 #include "llvm/Support/IntegersSubsetMapping.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 /// AllocaInst - an instruction to allocate memory on the stack
59 class AllocaInst : public UnaryInstruction {
61 virtual AllocaInst *clone_impl() const;
63 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
64 const Twine &Name = "", Instruction *InsertBefore = 0);
65 AllocaInst(Type *Ty, Value *ArraySize,
66 const Twine &Name, BasicBlock *InsertAtEnd);
68 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
69 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
71 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
72 const Twine &Name = "", Instruction *InsertBefore = 0);
73 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
74 const Twine &Name, BasicBlock *InsertAtEnd);
76 // Out of line virtual method, so the vtable, etc. has a home.
77 virtual ~AllocaInst();
79 /// isArrayAllocation - Return true if there is an allocation size parameter
80 /// to the allocation instruction that is not 1.
82 bool isArrayAllocation() const;
84 /// getArraySize - Get the number of elements allocated. For a simple
85 /// allocation of a single element, this will return a constant 1 value.
87 const Value *getArraySize() const { return getOperand(0); }
88 Value *getArraySize() { return getOperand(0); }
90 /// getType - Overload to return most specific pointer type
92 PointerType *getType() const {
93 return reinterpret_cast<PointerType*>(Instruction::getType());
96 /// getAllocatedType - Return the type that is being allocated by the
99 Type *getAllocatedType() const;
101 /// getAlignment - Return the alignment of the memory that is being allocated
102 /// by the instruction.
104 unsigned getAlignment() const {
105 return (1u << getSubclassDataFromInstruction()) >> 1;
107 void setAlignment(unsigned Align);
109 /// isStaticAlloca - Return true if this alloca is in the entry block of the
110 /// function and is a constant size. If so, the code generator will fold it
111 /// into the prolog/epilog code, so it is basically free.
112 bool isStaticAlloca() const;
114 // Methods for support type inquiry through isa, cast, and dyn_cast:
115 static inline bool classof(const Instruction *I) {
116 return (I->getOpcode() == Instruction::Alloca);
118 static inline bool classof(const Value *V) {
119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
122 // Shadow Instruction::setInstructionSubclassData with a private forwarding
123 // method so that subclasses cannot accidentally use it.
124 void setInstructionSubclassData(unsigned short D) {
125 Instruction::setInstructionSubclassData(D);
130 //===----------------------------------------------------------------------===//
132 //===----------------------------------------------------------------------===//
134 /// LoadInst - an instruction for reading from memory. This uses the
135 /// SubclassData field in Value to store whether or not the load is volatile.
137 class LoadInst : public UnaryInstruction {
140 virtual LoadInst *clone_impl() const;
142 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
143 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
144 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
145 Instruction *InsertBefore = 0);
146 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
147 BasicBlock *InsertAtEnd);
148 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
149 unsigned Align, Instruction *InsertBefore = 0);
150 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
151 unsigned Align, BasicBlock *InsertAtEnd);
152 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
153 unsigned Align, AtomicOrdering Order,
154 SynchronizationScope SynchScope = CrossThread,
155 Instruction *InsertBefore = 0);
156 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
157 unsigned Align, AtomicOrdering Order,
158 SynchronizationScope SynchScope,
159 BasicBlock *InsertAtEnd);
161 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
162 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
163 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
164 bool isVolatile = false, Instruction *InsertBefore = 0);
165 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
166 BasicBlock *InsertAtEnd);
168 /// isVolatile - Return true if this is a load from a volatile memory
171 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
173 /// setVolatile - Specify whether this is a volatile load or not.
175 void setVolatile(bool V) {
176 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
180 /// getAlignment - Return the alignment of the access that is being performed
182 unsigned getAlignment() const {
183 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
186 void setAlignment(unsigned Align);
188 /// Returns the ordering effect of this fence.
189 AtomicOrdering getOrdering() const {
190 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
193 /// Set the ordering constraint on this load. May not be Release or
195 void setOrdering(AtomicOrdering Ordering) {
196 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
200 SynchronizationScope getSynchScope() const {
201 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
204 /// Specify whether this load is ordered with respect to all
205 /// concurrently executing threads, or only with respect to signal handlers
206 /// executing in the same thread.
207 void setSynchScope(SynchronizationScope xthread) {
208 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
212 bool isAtomic() const { return getOrdering() != NotAtomic; }
213 void setAtomic(AtomicOrdering Ordering,
214 SynchronizationScope SynchScope = CrossThread) {
215 setOrdering(Ordering);
216 setSynchScope(SynchScope);
219 bool isSimple() const { return !isAtomic() && !isVolatile(); }
220 bool isUnordered() const {
221 return getOrdering() <= Unordered && !isVolatile();
224 Value *getPointerOperand() { return getOperand(0); }
225 const Value *getPointerOperand() const { return getOperand(0); }
226 static unsigned getPointerOperandIndex() { return 0U; }
228 unsigned getPointerAddressSpace() const {
229 return getPointerOperand()->getType()->getPointerAddressSpace();
233 // Methods for support type inquiry through isa, cast, and dyn_cast:
234 static inline bool classof(const Instruction *I) {
235 return I->getOpcode() == Instruction::Load;
237 static inline bool classof(const Value *V) {
238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 // Shadow Instruction::setInstructionSubclassData with a private forwarding
242 // method so that subclasses cannot accidentally use it.
243 void setInstructionSubclassData(unsigned short D) {
244 Instruction::setInstructionSubclassData(D);
249 //===----------------------------------------------------------------------===//
251 //===----------------------------------------------------------------------===//
253 /// StoreInst - an instruction for storing to memory
255 class StoreInst : public Instruction {
256 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
259 virtual StoreInst *clone_impl() const;
261 // allocate space for exactly two operands
262 void *operator new(size_t s) {
263 return User::operator new(s, 2);
265 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
266 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
267 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
268 Instruction *InsertBefore = 0);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
270 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
271 unsigned Align, Instruction *InsertBefore = 0);
272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
273 unsigned Align, BasicBlock *InsertAtEnd);
274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
275 unsigned Align, AtomicOrdering Order,
276 SynchronizationScope SynchScope = CrossThread,
277 Instruction *InsertBefore = 0);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
279 unsigned Align, AtomicOrdering Order,
280 SynchronizationScope SynchScope,
281 BasicBlock *InsertAtEnd);
284 /// isVolatile - Return true if this is a store to a volatile memory
287 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
289 /// setVolatile - Specify whether this is a volatile store or not.
291 void setVolatile(bool V) {
292 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
296 /// Transparently provide more efficient getOperand methods.
297 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
299 /// getAlignment - Return the alignment of the access that is being performed
301 unsigned getAlignment() const {
302 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
305 void setAlignment(unsigned Align);
307 /// Returns the ordering effect of this store.
308 AtomicOrdering getOrdering() const {
309 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
312 /// Set the ordering constraint on this store. May not be Acquire or
314 void setOrdering(AtomicOrdering Ordering) {
315 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
319 SynchronizationScope getSynchScope() const {
320 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
323 /// Specify whether this store instruction is ordered with respect to all
324 /// concurrently executing threads, or only with respect to signal handlers
325 /// executing in the same thread.
326 void setSynchScope(SynchronizationScope xthread) {
327 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
331 bool isAtomic() const { return getOrdering() != NotAtomic; }
332 void setAtomic(AtomicOrdering Ordering,
333 SynchronizationScope SynchScope = CrossThread) {
334 setOrdering(Ordering);
335 setSynchScope(SynchScope);
338 bool isSimple() const { return !isAtomic() && !isVolatile(); }
339 bool isUnordered() const {
340 return getOrdering() <= Unordered && !isVolatile();
343 Value *getValueOperand() { return getOperand(0); }
344 const Value *getValueOperand() const { return getOperand(0); }
346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
350 unsigned getPointerAddressSpace() const {
351 return getPointerOperand()->getType()->getPointerAddressSpace();
354 // Methods for support type inquiry through isa, cast, and dyn_cast:
355 static inline bool classof(const Instruction *I) {
356 return I->getOpcode() == Instruction::Store;
358 static inline bool classof(const Value *V) {
359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
362 // Shadow Instruction::setInstructionSubclassData with a private forwarding
363 // method so that subclasses cannot accidentally use it.
364 void setInstructionSubclassData(unsigned short D) {
365 Instruction::setInstructionSubclassData(D);
370 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
373 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
375 //===----------------------------------------------------------------------===//
377 //===----------------------------------------------------------------------===//
379 /// FenceInst - an instruction for ordering other memory operations
381 class FenceInst : public Instruction {
382 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
383 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
385 virtual FenceInst *clone_impl() const;
387 // allocate space for exactly zero operands
388 void *operator new(size_t s) {
389 return User::operator new(s, 0);
392 // Ordering may only be Acquire, Release, AcquireRelease, or
393 // SequentiallyConsistent.
394 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
395 SynchronizationScope SynchScope = CrossThread,
396 Instruction *InsertBefore = 0);
397 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398 SynchronizationScope SynchScope,
399 BasicBlock *InsertAtEnd);
401 /// Returns the ordering effect of this fence.
402 AtomicOrdering getOrdering() const {
403 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
406 /// Set the ordering constraint on this fence. May only be Acquire, Release,
407 /// AcquireRelease, or SequentiallyConsistent.
408 void setOrdering(AtomicOrdering Ordering) {
409 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
413 SynchronizationScope getSynchScope() const {
414 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
417 /// Specify whether this fence orders other operations with respect to all
418 /// concurrently executing threads, or only with respect to signal handlers
419 /// executing in the same thread.
420 void setSynchScope(SynchronizationScope xthread) {
421 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
425 // Methods for support type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const Instruction *I) {
427 return I->getOpcode() == Instruction::Fence;
429 static inline bool classof(const Value *V) {
430 return isa<Instruction>(V) && classof(cast<Instruction>(V));
433 // Shadow Instruction::setInstructionSubclassData with a private forwarding
434 // method so that subclasses cannot accidentally use it.
435 void setInstructionSubclassData(unsigned short D) {
436 Instruction::setInstructionSubclassData(D);
440 //===----------------------------------------------------------------------===//
441 // AtomicCmpXchgInst Class
442 //===----------------------------------------------------------------------===//
444 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
445 /// specified value is in a memory location, and, if it is, stores a new value
446 /// there. Returns the value that was loaded.
448 class AtomicCmpXchgInst : public Instruction {
449 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
450 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
451 AtomicOrdering Ordering, SynchronizationScope SynchScope);
453 virtual AtomicCmpXchgInst *clone_impl() const;
455 // allocate space for exactly three operands
456 void *operator new(size_t s) {
457 return User::operator new(s, 3);
459 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
460 AtomicOrdering Ordering, SynchronizationScope SynchScope,
461 Instruction *InsertBefore = 0);
462 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
463 AtomicOrdering Ordering, SynchronizationScope SynchScope,
464 BasicBlock *InsertAtEnd);
466 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
469 bool isVolatile() const {
470 return getSubclassDataFromInstruction() & 1;
473 /// setVolatile - Specify whether this is a volatile cmpxchg.
475 void setVolatile(bool V) {
476 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
483 /// Set the ordering constraint on this cmpxchg.
484 void setOrdering(AtomicOrdering Ordering) {
485 assert(Ordering != NotAtomic &&
486 "CmpXchg instructions can only be atomic.");
487 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
491 /// Specify whether this cmpxchg is atomic and orders other operations with
492 /// respect to all concurrently executing threads, or only with respect to
493 /// signal handlers executing in the same thread.
494 void setSynchScope(SynchronizationScope SynchScope) {
495 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
499 /// Returns the ordering constraint on this cmpxchg.
500 AtomicOrdering getOrdering() const {
501 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
504 /// Returns whether this cmpxchg is atomic between threads or only within a
506 SynchronizationScope getSynchScope() const {
507 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
510 Value *getPointerOperand() { return getOperand(0); }
511 const Value *getPointerOperand() const { return getOperand(0); }
512 static unsigned getPointerOperandIndex() { return 0U; }
514 Value *getCompareOperand() { return getOperand(1); }
515 const Value *getCompareOperand() const { return getOperand(1); }
517 Value *getNewValOperand() { return getOperand(2); }
518 const Value *getNewValOperand() const { return getOperand(2); }
520 unsigned getPointerAddressSpace() const {
521 return getPointerOperand()->getType()->getPointerAddressSpace();
524 // Methods for support type inquiry through isa, cast, and dyn_cast:
525 static inline bool classof(const Instruction *I) {
526 return I->getOpcode() == Instruction::AtomicCmpXchg;
528 static inline bool classof(const Value *V) {
529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
532 // Shadow Instruction::setInstructionSubclassData with a private forwarding
533 // method so that subclasses cannot accidentally use it.
534 void setInstructionSubclassData(unsigned short D) {
535 Instruction::setInstructionSubclassData(D);
540 struct OperandTraits<AtomicCmpXchgInst> :
541 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
544 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
546 //===----------------------------------------------------------------------===//
547 // AtomicRMWInst Class
548 //===----------------------------------------------------------------------===//
550 /// AtomicRMWInst - an instruction that atomically reads a memory location,
551 /// combines it with another value, and then stores the result back. Returns
554 class AtomicRMWInst : public Instruction {
555 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
557 virtual AtomicRMWInst *clone_impl() const;
559 /// This enumeration lists the possible modifications atomicrmw can make. In
560 /// the descriptions, 'p' is the pointer to the instruction's memory location,
561 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
562 /// instruction. These instructions always return 'old'.
578 /// *p = old >signed v ? old : v
580 /// *p = old <signed v ? old : v
582 /// *p = old >unsigned v ? old : v
584 /// *p = old <unsigned v ? old : v
592 // allocate space for exactly two operands
593 void *operator new(size_t s) {
594 return User::operator new(s, 2);
596 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
597 AtomicOrdering Ordering, SynchronizationScope SynchScope,
598 Instruction *InsertBefore = 0);
599 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
600 AtomicOrdering Ordering, SynchronizationScope SynchScope,
601 BasicBlock *InsertAtEnd);
603 BinOp getOperation() const {
604 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
607 void setOperation(BinOp Operation) {
608 unsigned short SubclassData = getSubclassDataFromInstruction();
609 setInstructionSubclassData((SubclassData & 31) |
613 /// isVolatile - Return true if this is a RMW on a volatile memory location.
615 bool isVolatile() const {
616 return getSubclassDataFromInstruction() & 1;
619 /// setVolatile - Specify whether this is a volatile RMW or not.
621 void setVolatile(bool V) {
622 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
626 /// Transparently provide more efficient getOperand methods.
627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
629 /// Set the ordering constraint on this RMW.
630 void setOrdering(AtomicOrdering Ordering) {
631 assert(Ordering != NotAtomic &&
632 "atomicrmw instructions can only be atomic.");
633 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
637 /// Specify whether this RMW orders other operations with respect to all
638 /// concurrently executing threads, or only with respect to signal handlers
639 /// executing in the same thread.
640 void setSynchScope(SynchronizationScope SynchScope) {
641 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
645 /// Returns the ordering constraint on this RMW.
646 AtomicOrdering getOrdering() const {
647 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
650 /// Returns whether this RMW is atomic between threads or only within a
652 SynchronizationScope getSynchScope() const {
653 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
656 Value *getPointerOperand() { return getOperand(0); }
657 const Value *getPointerOperand() const { return getOperand(0); }
658 static unsigned getPointerOperandIndex() { return 0U; }
660 Value *getValOperand() { return getOperand(1); }
661 const Value *getValOperand() const { return getOperand(1); }
663 unsigned getPointerAddressSpace() const {
664 return getPointerOperand()->getType()->getPointerAddressSpace();
667 // Methods for support type inquiry through isa, cast, and dyn_cast:
668 static inline bool classof(const Instruction *I) {
669 return I->getOpcode() == Instruction::AtomicRMW;
671 static inline bool classof(const Value *V) {
672 return isa<Instruction>(V) && classof(cast<Instruction>(V));
675 void Init(BinOp Operation, Value *Ptr, Value *Val,
676 AtomicOrdering Ordering, SynchronizationScope SynchScope);
677 // Shadow Instruction::setInstructionSubclassData with a private forwarding
678 // method so that subclasses cannot accidentally use it.
679 void setInstructionSubclassData(unsigned short D) {
680 Instruction::setInstructionSubclassData(D);
685 struct OperandTraits<AtomicRMWInst>
686 : public FixedNumOperandTraits<AtomicRMWInst,2> {
689 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
691 //===----------------------------------------------------------------------===//
692 // GetElementPtrInst Class
693 //===----------------------------------------------------------------------===//
695 // checkGEPType - Simple wrapper function to give a better assertion failure
696 // message on bad indexes for a gep instruction.
698 inline Type *checkGEPType(Type *Ty) {
699 assert(Ty && "Invalid GetElementPtrInst indices for type!");
703 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
704 /// access elements of arrays and structs
706 class GetElementPtrInst : public Instruction {
707 GetElementPtrInst(const GetElementPtrInst &GEPI);
708 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
710 /// Constructors - Create a getelementptr instruction with a base pointer an
711 /// list of indices. The first ctor can optionally insert before an existing
712 /// instruction, the second appends the new instruction to the specified
714 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
715 unsigned Values, const Twine &NameStr,
716 Instruction *InsertBefore);
717 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
718 unsigned Values, const Twine &NameStr,
719 BasicBlock *InsertAtEnd);
721 virtual GetElementPtrInst *clone_impl() const;
723 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
724 const Twine &NameStr = "",
725 Instruction *InsertBefore = 0) {
726 unsigned Values = 1 + unsigned(IdxList.size());
728 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
730 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
731 const Twine &NameStr,
732 BasicBlock *InsertAtEnd) {
733 unsigned Values = 1 + unsigned(IdxList.size());
735 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
738 /// Create an "inbounds" getelementptr. See the documentation for the
739 /// "inbounds" flag in LangRef.html for details.
740 static GetElementPtrInst *CreateInBounds(Value *Ptr,
741 ArrayRef<Value *> IdxList,
742 const Twine &NameStr = "",
743 Instruction *InsertBefore = 0) {
744 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
745 GEP->setIsInBounds(true);
748 static GetElementPtrInst *CreateInBounds(Value *Ptr,
749 ArrayRef<Value *> IdxList,
750 const Twine &NameStr,
751 BasicBlock *InsertAtEnd) {
752 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
753 GEP->setIsInBounds(true);
757 /// Transparently provide more efficient getOperand methods.
758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
760 // getType - Overload to return most specific pointer type...
761 PointerType *getType() const {
762 return reinterpret_cast<PointerType*>(Instruction::getType());
765 /// getIndexedType - Returns the type of the element that would be loaded with
766 /// a load instruction with the specified parameters.
768 /// Null is returned if the indices are invalid for the specified
771 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
772 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
773 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
775 inline op_iterator idx_begin() { return op_begin()+1; }
776 inline const_op_iterator idx_begin() const { return op_begin()+1; }
777 inline op_iterator idx_end() { return op_end(); }
778 inline const_op_iterator idx_end() const { return op_end(); }
780 Value *getPointerOperand() {
781 return getOperand(0);
783 const Value *getPointerOperand() const {
784 return getOperand(0);
786 static unsigned getPointerOperandIndex() {
787 return 0U; // get index for modifying correct operand.
790 /// getPointerOperandType - Method to return the pointer operand as a
792 Type *getPointerOperandType() const {
793 return getPointerOperand()->getType();
796 unsigned getPointerAddressSpace() const {
797 return getPointerOperandType()->getPointerAddressSpace();
800 /// GetGEPReturnType - Returns the pointer type returned by the GEP
801 /// instruction, which may be a vector of pointers.
802 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
803 Type *PtrTy = PointerType::get(checkGEPType(
804 getIndexedType(Ptr->getType(), IdxList)),
805 Ptr->getType()->getPointerAddressSpace());
807 if (Ptr->getType()->isVectorTy()) {
808 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
809 return VectorType::get(PtrTy, NumElem);
816 unsigned getNumIndices() const { // Note: always non-negative
817 return getNumOperands() - 1;
820 bool hasIndices() const {
821 return getNumOperands() > 1;
824 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
825 /// zeros. If so, the result pointer and the first operand have the same
826 /// value, just potentially different types.
827 bool hasAllZeroIndices() const;
829 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
830 /// constant integers. If so, the result pointer and the first operand have
831 /// a constant offset between them.
832 bool hasAllConstantIndices() const;
834 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
835 /// See LangRef.html for the meaning of inbounds on a getelementptr.
836 void setIsInBounds(bool b = true);
838 /// isInBounds - Determine whether the GEP has the inbounds flag.
839 bool isInBounds() const;
841 // Methods for support type inquiry through isa, cast, and dyn_cast:
842 static inline bool classof(const Instruction *I) {
843 return (I->getOpcode() == Instruction::GetElementPtr);
845 static inline bool classof(const Value *V) {
846 return isa<Instruction>(V) && classof(cast<Instruction>(V));
851 struct OperandTraits<GetElementPtrInst> :
852 public VariadicOperandTraits<GetElementPtrInst, 1> {
855 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
856 ArrayRef<Value *> IdxList,
858 const Twine &NameStr,
859 Instruction *InsertBefore)
860 : Instruction(getGEPReturnType(Ptr, IdxList),
862 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
863 Values, InsertBefore) {
864 init(Ptr, IdxList, NameStr);
866 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
867 ArrayRef<Value *> IdxList,
869 const Twine &NameStr,
870 BasicBlock *InsertAtEnd)
871 : Instruction(getGEPReturnType(Ptr, IdxList),
873 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
874 Values, InsertAtEnd) {
875 init(Ptr, IdxList, NameStr);
879 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
882 //===----------------------------------------------------------------------===//
884 //===----------------------------------------------------------------------===//
886 /// This instruction compares its operands according to the predicate given
887 /// to the constructor. It only operates on integers or pointers. The operands
888 /// must be identical types.
889 /// \brief Represent an integer comparison operator.
890 class ICmpInst: public CmpInst {
892 /// \brief Clone an identical ICmpInst
893 virtual ICmpInst *clone_impl() const;
895 /// \brief Constructor with insert-before-instruction semantics.
897 Instruction *InsertBefore, ///< Where to insert
898 Predicate pred, ///< The predicate to use for the comparison
899 Value *LHS, ///< The left-hand-side of the expression
900 Value *RHS, ///< The right-hand-side of the expression
901 const Twine &NameStr = "" ///< Name of the instruction
902 ) : CmpInst(makeCmpResultType(LHS->getType()),
903 Instruction::ICmp, pred, LHS, RHS, NameStr,
905 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
906 pred <= CmpInst::LAST_ICMP_PREDICATE &&
907 "Invalid ICmp predicate value");
908 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
909 "Both operands to ICmp instruction are not of the same type!");
910 // Check that the operands are the right type
911 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
912 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
913 "Invalid operand types for ICmp instruction");
916 /// \brief Constructor with insert-at-end semantics.
918 BasicBlock &InsertAtEnd, ///< Block to insert into.
919 Predicate pred, ///< The predicate to use for the comparison
920 Value *LHS, ///< The left-hand-side of the expression
921 Value *RHS, ///< The right-hand-side of the expression
922 const Twine &NameStr = "" ///< Name of the instruction
923 ) : CmpInst(makeCmpResultType(LHS->getType()),
924 Instruction::ICmp, pred, LHS, RHS, NameStr,
926 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
927 pred <= CmpInst::LAST_ICMP_PREDICATE &&
928 "Invalid ICmp predicate value");
929 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
930 "Both operands to ICmp instruction are not of the same type!");
931 // Check that the operands are the right type
932 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
933 getOperand(0)->getType()->isPointerTy()) &&
934 "Invalid operand types for ICmp instruction");
937 /// \brief Constructor with no-insertion semantics
939 Predicate pred, ///< The predicate to use for the comparison
940 Value *LHS, ///< The left-hand-side of the expression
941 Value *RHS, ///< The right-hand-side of the expression
942 const Twine &NameStr = "" ///< Name of the instruction
943 ) : CmpInst(makeCmpResultType(LHS->getType()),
944 Instruction::ICmp, pred, LHS, RHS, NameStr) {
945 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
946 pred <= CmpInst::LAST_ICMP_PREDICATE &&
947 "Invalid ICmp predicate value");
948 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
949 "Both operands to ICmp instruction are not of the same type!");
950 // Check that the operands are the right type
951 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
952 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
953 "Invalid operand types for ICmp instruction");
956 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
957 /// @returns the predicate that would be the result if the operand were
958 /// regarded as signed.
959 /// \brief Return the signed version of the predicate
960 Predicate getSignedPredicate() const {
961 return getSignedPredicate(getPredicate());
964 /// This is a static version that you can use without an instruction.
965 /// \brief Return the signed version of the predicate.
966 static Predicate getSignedPredicate(Predicate pred);
968 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
969 /// @returns the predicate that would be the result if the operand were
970 /// regarded as unsigned.
971 /// \brief Return the unsigned version of the predicate
972 Predicate getUnsignedPredicate() const {
973 return getUnsignedPredicate(getPredicate());
976 /// This is a static version that you can use without an instruction.
977 /// \brief Return the unsigned version of the predicate.
978 static Predicate getUnsignedPredicate(Predicate pred);
980 /// isEquality - Return true if this predicate is either EQ or NE. This also
981 /// tests for commutativity.
982 static bool isEquality(Predicate P) {
983 return P == ICMP_EQ || P == ICMP_NE;
986 /// isEquality - Return true if this predicate is either EQ or NE. This also
987 /// tests for commutativity.
988 bool isEquality() const {
989 return isEquality(getPredicate());
992 /// @returns true if the predicate of this ICmpInst is commutative
993 /// \brief Determine if this relation is commutative.
994 bool isCommutative() const { return isEquality(); }
996 /// isRelational - Return true if the predicate is relational (not EQ or NE).
998 bool isRelational() const {
999 return !isEquality();
1002 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1004 static bool isRelational(Predicate P) {
1005 return !isEquality(P);
1008 /// Initialize a set of values that all satisfy the predicate with C.
1009 /// \brief Make a ConstantRange for a relation with a constant value.
1010 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1012 /// Exchange the two operands to this instruction in such a way that it does
1013 /// not modify the semantics of the instruction. The predicate value may be
1014 /// changed to retain the same result if the predicate is order dependent
1016 /// \brief Swap operands and adjust predicate.
1017 void swapOperands() {
1018 setPredicate(getSwappedPredicate());
1019 Op<0>().swap(Op<1>());
1022 // Methods for support type inquiry through isa, cast, and dyn_cast:
1023 static inline bool classof(const Instruction *I) {
1024 return I->getOpcode() == Instruction::ICmp;
1026 static inline bool classof(const Value *V) {
1027 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1032 //===----------------------------------------------------------------------===//
1034 //===----------------------------------------------------------------------===//
1036 /// This instruction compares its operands according to the predicate given
1037 /// to the constructor. It only operates on floating point values or packed
1038 /// vectors of floating point values. The operands must be identical types.
1039 /// \brief Represents a floating point comparison operator.
1040 class FCmpInst: public CmpInst {
1042 /// \brief Clone an identical FCmpInst
1043 virtual FCmpInst *clone_impl() const;
1045 /// \brief Constructor with insert-before-instruction semantics.
1047 Instruction *InsertBefore, ///< Where to insert
1048 Predicate pred, ///< The predicate to use for the comparison
1049 Value *LHS, ///< The left-hand-side of the expression
1050 Value *RHS, ///< The right-hand-side of the expression
1051 const Twine &NameStr = "" ///< Name of the instruction
1052 ) : CmpInst(makeCmpResultType(LHS->getType()),
1053 Instruction::FCmp, pred, LHS, RHS, NameStr,
1055 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1056 "Invalid FCmp predicate value");
1057 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1058 "Both operands to FCmp instruction are not of the same type!");
1059 // Check that the operands are the right type
1060 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1061 "Invalid operand types for FCmp instruction");
1064 /// \brief Constructor with insert-at-end semantics.
1066 BasicBlock &InsertAtEnd, ///< Block to insert into.
1067 Predicate pred, ///< The predicate to use for the comparison
1068 Value *LHS, ///< The left-hand-side of the expression
1069 Value *RHS, ///< The right-hand-side of the expression
1070 const Twine &NameStr = "" ///< Name of the instruction
1071 ) : CmpInst(makeCmpResultType(LHS->getType()),
1072 Instruction::FCmp, pred, LHS, RHS, NameStr,
1074 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1075 "Invalid FCmp predicate value");
1076 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1077 "Both operands to FCmp instruction are not of the same type!");
1078 // Check that the operands are the right type
1079 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1080 "Invalid operand types for FCmp instruction");
1083 /// \brief Constructor with no-insertion semantics
1085 Predicate pred, ///< The predicate to use for the comparison
1086 Value *LHS, ///< The left-hand-side of the expression
1087 Value *RHS, ///< The right-hand-side of the expression
1088 const Twine &NameStr = "" ///< Name of the instruction
1089 ) : CmpInst(makeCmpResultType(LHS->getType()),
1090 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1091 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1092 "Invalid FCmp predicate value");
1093 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1094 "Both operands to FCmp instruction are not of the same type!");
1095 // Check that the operands are the right type
1096 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1097 "Invalid operand types for FCmp instruction");
1100 /// @returns true if the predicate of this instruction is EQ or NE.
1101 /// \brief Determine if this is an equality predicate.
1102 bool isEquality() const {
1103 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1104 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1107 /// @returns true if the predicate of this instruction is commutative.
1108 /// \brief Determine if this is a commutative predicate.
1109 bool isCommutative() const {
1110 return isEquality() ||
1111 getPredicate() == FCMP_FALSE ||
1112 getPredicate() == FCMP_TRUE ||
1113 getPredicate() == FCMP_ORD ||
1114 getPredicate() == FCMP_UNO;
1117 /// @returns true if the predicate is relational (not EQ or NE).
1118 /// \brief Determine if this a relational predicate.
1119 bool isRelational() const { return !isEquality(); }
1121 /// Exchange the two operands to this instruction in such a way that it does
1122 /// not modify the semantics of the instruction. The predicate value may be
1123 /// changed to retain the same result if the predicate is order dependent
1125 /// \brief Swap operands and adjust predicate.
1126 void swapOperands() {
1127 setPredicate(getSwappedPredicate());
1128 Op<0>().swap(Op<1>());
1131 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1132 static inline bool classof(const Instruction *I) {
1133 return I->getOpcode() == Instruction::FCmp;
1135 static inline bool classof(const Value *V) {
1136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1140 //===----------------------------------------------------------------------===//
1141 /// CallInst - This class represents a function call, abstracting a target
1142 /// machine's calling convention. This class uses low bit of the SubClassData
1143 /// field to indicate whether or not this is a tail call. The rest of the bits
1144 /// hold the calling convention of the call.
1146 class CallInst : public Instruction {
1147 AttrListPtr AttributeList; ///< parameter attributes for call
1148 CallInst(const CallInst &CI);
1149 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1150 void init(Value *Func, const Twine &NameStr);
1152 /// Construct a CallInst given a range of arguments.
1153 /// \brief Construct a CallInst from a range of arguments
1154 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1155 const Twine &NameStr, Instruction *InsertBefore);
1157 /// Construct a CallInst given a range of arguments.
1158 /// \brief Construct a CallInst from a range of arguments
1159 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1160 const Twine &NameStr, BasicBlock *InsertAtEnd);
1162 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1163 Instruction *InsertBefore);
1164 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1165 BasicBlock *InsertAtEnd);
1166 explicit CallInst(Value *F, const Twine &NameStr,
1167 Instruction *InsertBefore);
1168 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1170 virtual CallInst *clone_impl() const;
1172 static CallInst *Create(Value *Func,
1173 ArrayRef<Value *> Args,
1174 const Twine &NameStr = "",
1175 Instruction *InsertBefore = 0) {
1176 return new(unsigned(Args.size() + 1))
1177 CallInst(Func, Args, NameStr, InsertBefore);
1179 static CallInst *Create(Value *Func,
1180 ArrayRef<Value *> Args,
1181 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1182 return new(unsigned(Args.size() + 1))
1183 CallInst(Func, Args, NameStr, InsertAtEnd);
1185 static CallInst *Create(Value *F, const Twine &NameStr = "",
1186 Instruction *InsertBefore = 0) {
1187 return new(1) CallInst(F, NameStr, InsertBefore);
1189 static CallInst *Create(Value *F, const Twine &NameStr,
1190 BasicBlock *InsertAtEnd) {
1191 return new(1) CallInst(F, NameStr, InsertAtEnd);
1193 /// CreateMalloc - Generate the IR for a call to malloc:
1194 /// 1. Compute the malloc call's argument as the specified type's size,
1195 /// possibly multiplied by the array size if the array size is not
1197 /// 2. Call malloc with that argument.
1198 /// 3. Bitcast the result of the malloc call to the specified type.
1199 static Instruction *CreateMalloc(Instruction *InsertBefore,
1200 Type *IntPtrTy, Type *AllocTy,
1201 Value *AllocSize, Value *ArraySize = 0,
1202 Function* MallocF = 0,
1203 const Twine &Name = "");
1204 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1205 Type *IntPtrTy, Type *AllocTy,
1206 Value *AllocSize, Value *ArraySize = 0,
1207 Function* MallocF = 0,
1208 const Twine &Name = "");
1209 /// CreateFree - Generate the IR for a call to the builtin free function.
1210 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1211 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1215 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1216 void setTailCall(bool isTC = true) {
1217 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1221 /// Provide fast operand accessors
1222 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1224 /// getNumArgOperands - Return the number of call arguments.
1226 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1228 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1230 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1231 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1233 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1235 CallingConv::ID getCallingConv() const {
1236 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1238 void setCallingConv(CallingConv::ID CC) {
1239 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1240 (static_cast<unsigned>(CC) << 1));
1243 /// getAttributes - Return the parameter attributes for this call.
1245 const AttrListPtr &getAttributes() const { return AttributeList; }
1247 /// setAttributes - Set the parameter attributes for this call.
1249 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1251 /// addAttribute - adds the attribute to the list of attributes.
1252 void addAttribute(unsigned i, Attributes attr);
1254 /// removeAttribute - removes the attribute from the list of attributes.
1255 void removeAttribute(unsigned i, Attributes attr);
1257 /// \brief Determine whether this call has the given attribute.
1258 bool hasFnAttr(Attributes::AttrVal A) const;
1260 /// \brief Determine whether the call or the callee has the given attributes.
1261 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
1263 /// \brief Extract the alignment for a call or parameter (0=unknown).
1264 unsigned getParamAlignment(unsigned i) const {
1265 return AttributeList.getParamAlignment(i);
1268 /// \brief Return true if the call should not be inlined.
1269 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
1270 void setIsNoInline() {
1271 addAttribute(AttrListPtr::FunctionIndex,
1272 Attributes::get(getContext(), Attributes::NoInline));
1275 /// \brief Return true if the call can return twice
1276 bool canReturnTwice() const {
1277 return hasFnAttr(Attributes::ReturnsTwice);
1279 void setCanReturnTwice() {
1280 addAttribute(AttrListPtr::FunctionIndex,
1281 Attributes::get(getContext(), Attributes::ReturnsTwice));
1284 /// \brief Determine if the call does not access memory.
1285 bool doesNotAccessMemory() const {
1286 return hasFnAttr(Attributes::ReadNone);
1288 void setDoesNotAccessMemory() {
1289 addAttribute(AttrListPtr::FunctionIndex,
1290 Attributes::get(getContext(), Attributes::ReadNone));
1293 /// \brief Determine if the call does not access or only reads memory.
1294 bool onlyReadsMemory() const {
1295 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
1297 void setOnlyReadsMemory() {
1298 addAttribute(AttrListPtr::FunctionIndex,
1299 Attributes::get(getContext(), Attributes::ReadOnly));
1302 /// \brief Determine if the call cannot return.
1303 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
1304 void setDoesNotReturn() {
1305 addAttribute(AttrListPtr::FunctionIndex,
1306 Attributes::get(getContext(), Attributes::NoReturn));
1309 /// \brief Determine if the call cannot unwind.
1310 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
1311 void setDoesNotThrow() {
1312 addAttribute(AttrListPtr::FunctionIndex,
1313 Attributes::get(getContext(), Attributes::NoUnwind));
1316 /// \brief Determine if the call returns a structure through first
1317 /// pointer argument.
1318 bool hasStructRetAttr() const {
1319 // Be friendly and also check the callee.
1320 return paramHasAttr(1, Attributes::StructRet);
1323 /// \brief Determine if any call argument is an aggregate passed by value.
1324 bool hasByValArgument() const {
1325 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1326 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
1331 /// getCalledFunction - Return the function called, or null if this is an
1332 /// indirect function invocation.
1334 Function *getCalledFunction() const {
1335 return dyn_cast<Function>(Op<-1>());
1338 /// getCalledValue - Get a pointer to the function that is invoked by this
1340 const Value *getCalledValue() const { return Op<-1>(); }
1341 Value *getCalledValue() { return Op<-1>(); }
1343 /// setCalledFunction - Set the function called.
1344 void setCalledFunction(Value* Fn) {
1348 /// isInlineAsm - Check if this call is an inline asm statement.
1349 bool isInlineAsm() const {
1350 return isa<InlineAsm>(Op<-1>());
1353 // Methods for support type inquiry through isa, cast, and dyn_cast:
1354 static inline bool classof(const Instruction *I) {
1355 return I->getOpcode() == Instruction::Call;
1357 static inline bool classof(const Value *V) {
1358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1361 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1362 // method so that subclasses cannot accidentally use it.
1363 void setInstructionSubclassData(unsigned short D) {
1364 Instruction::setInstructionSubclassData(D);
1369 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1372 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1373 const Twine &NameStr, BasicBlock *InsertAtEnd)
1374 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1375 ->getElementType())->getReturnType(),
1377 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1378 unsigned(Args.size() + 1), InsertAtEnd) {
1379 init(Func, Args, NameStr);
1382 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1383 const Twine &NameStr, Instruction *InsertBefore)
1384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1385 ->getElementType())->getReturnType(),
1387 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1388 unsigned(Args.size() + 1), InsertBefore) {
1389 init(Func, Args, NameStr);
1393 // Note: if you get compile errors about private methods then
1394 // please update your code to use the high-level operand
1395 // interfaces. See line 943 above.
1396 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1398 //===----------------------------------------------------------------------===//
1400 //===----------------------------------------------------------------------===//
1402 /// SelectInst - This class represents the LLVM 'select' instruction.
1404 class SelectInst : public Instruction {
1405 void init(Value *C, Value *S1, Value *S2) {
1406 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1412 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1413 Instruction *InsertBefore)
1414 : Instruction(S1->getType(), Instruction::Select,
1415 &Op<0>(), 3, InsertBefore) {
1419 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1420 BasicBlock *InsertAtEnd)
1421 : Instruction(S1->getType(), Instruction::Select,
1422 &Op<0>(), 3, InsertAtEnd) {
1427 virtual SelectInst *clone_impl() const;
1429 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1430 const Twine &NameStr = "",
1431 Instruction *InsertBefore = 0) {
1432 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1434 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1435 const Twine &NameStr,
1436 BasicBlock *InsertAtEnd) {
1437 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1440 const Value *getCondition() const { return Op<0>(); }
1441 const Value *getTrueValue() const { return Op<1>(); }
1442 const Value *getFalseValue() const { return Op<2>(); }
1443 Value *getCondition() { return Op<0>(); }
1444 Value *getTrueValue() { return Op<1>(); }
1445 Value *getFalseValue() { return Op<2>(); }
1447 /// areInvalidOperands - Return a string if the specified operands are invalid
1448 /// for a select operation, otherwise return null.
1449 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1451 /// Transparently provide more efficient getOperand methods.
1452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1454 OtherOps getOpcode() const {
1455 return static_cast<OtherOps>(Instruction::getOpcode());
1458 // Methods for support type inquiry through isa, cast, and dyn_cast:
1459 static inline bool classof(const Instruction *I) {
1460 return I->getOpcode() == Instruction::Select;
1462 static inline bool classof(const Value *V) {
1463 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1468 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1471 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1473 //===----------------------------------------------------------------------===//
1475 //===----------------------------------------------------------------------===//
1477 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1478 /// an argument of the specified type given a va_list and increments that list
1480 class VAArgInst : public UnaryInstruction {
1482 virtual VAArgInst *clone_impl() const;
1485 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1486 Instruction *InsertBefore = 0)
1487 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1490 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1491 BasicBlock *InsertAtEnd)
1492 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1496 Value *getPointerOperand() { return getOperand(0); }
1497 const Value *getPointerOperand() const { return getOperand(0); }
1498 static unsigned getPointerOperandIndex() { return 0U; }
1500 // Methods for support type inquiry through isa, cast, and dyn_cast:
1501 static inline bool classof(const Instruction *I) {
1502 return I->getOpcode() == VAArg;
1504 static inline bool classof(const Value *V) {
1505 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1509 //===----------------------------------------------------------------------===//
1510 // ExtractElementInst Class
1511 //===----------------------------------------------------------------------===//
1513 /// ExtractElementInst - This instruction extracts a single (scalar)
1514 /// element from a VectorType value
1516 class ExtractElementInst : public Instruction {
1517 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1518 Instruction *InsertBefore = 0);
1519 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1520 BasicBlock *InsertAtEnd);
1522 virtual ExtractElementInst *clone_impl() const;
1525 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1526 const Twine &NameStr = "",
1527 Instruction *InsertBefore = 0) {
1528 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1530 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1531 const Twine &NameStr,
1532 BasicBlock *InsertAtEnd) {
1533 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1536 /// isValidOperands - Return true if an extractelement instruction can be
1537 /// formed with the specified operands.
1538 static bool isValidOperands(const Value *Vec, const Value *Idx);
1540 Value *getVectorOperand() { return Op<0>(); }
1541 Value *getIndexOperand() { return Op<1>(); }
1542 const Value *getVectorOperand() const { return Op<0>(); }
1543 const Value *getIndexOperand() const { return Op<1>(); }
1545 VectorType *getVectorOperandType() const {
1546 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1550 /// Transparently provide more efficient getOperand methods.
1551 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1553 // Methods for support type inquiry through isa, cast, and dyn_cast:
1554 static inline bool classof(const Instruction *I) {
1555 return I->getOpcode() == Instruction::ExtractElement;
1557 static inline bool classof(const Value *V) {
1558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1563 struct OperandTraits<ExtractElementInst> :
1564 public FixedNumOperandTraits<ExtractElementInst, 2> {
1567 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1569 //===----------------------------------------------------------------------===//
1570 // InsertElementInst Class
1571 //===----------------------------------------------------------------------===//
1573 /// InsertElementInst - This instruction inserts a single (scalar)
1574 /// element into a VectorType value
1576 class InsertElementInst : public Instruction {
1577 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1578 const Twine &NameStr = "",
1579 Instruction *InsertBefore = 0);
1580 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1581 const Twine &NameStr, BasicBlock *InsertAtEnd);
1583 virtual InsertElementInst *clone_impl() const;
1586 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1587 const Twine &NameStr = "",
1588 Instruction *InsertBefore = 0) {
1589 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1591 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1592 const Twine &NameStr,
1593 BasicBlock *InsertAtEnd) {
1594 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1597 /// isValidOperands - Return true if an insertelement instruction can be
1598 /// formed with the specified operands.
1599 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1602 /// getType - Overload to return most specific vector type.
1604 VectorType *getType() const {
1605 return reinterpret_cast<VectorType*>(Instruction::getType());
1608 /// Transparently provide more efficient getOperand methods.
1609 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1611 // Methods for support type inquiry through isa, cast, and dyn_cast:
1612 static inline bool classof(const Instruction *I) {
1613 return I->getOpcode() == Instruction::InsertElement;
1615 static inline bool classof(const Value *V) {
1616 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1621 struct OperandTraits<InsertElementInst> :
1622 public FixedNumOperandTraits<InsertElementInst, 3> {
1625 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1627 //===----------------------------------------------------------------------===//
1628 // ShuffleVectorInst Class
1629 //===----------------------------------------------------------------------===//
1631 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1634 class ShuffleVectorInst : public Instruction {
1636 virtual ShuffleVectorInst *clone_impl() const;
1639 // allocate space for exactly three operands
1640 void *operator new(size_t s) {
1641 return User::operator new(s, 3);
1643 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1644 const Twine &NameStr = "",
1645 Instruction *InsertBefor = 0);
1646 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1647 const Twine &NameStr, BasicBlock *InsertAtEnd);
1649 /// isValidOperands - Return true if a shufflevector instruction can be
1650 /// formed with the specified operands.
1651 static bool isValidOperands(const Value *V1, const Value *V2,
1654 /// getType - Overload to return most specific vector type.
1656 VectorType *getType() const {
1657 return reinterpret_cast<VectorType*>(Instruction::getType());
1660 /// Transparently provide more efficient getOperand methods.
1661 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1663 Constant *getMask() const {
1664 return reinterpret_cast<Constant*>(getOperand(2));
1667 /// getMaskValue - Return the index from the shuffle mask for the specified
1668 /// output result. This is either -1 if the element is undef or a number less
1669 /// than 2*numelements.
1670 static int getMaskValue(Constant *Mask, unsigned i);
1672 int getMaskValue(unsigned i) const {
1673 return getMaskValue(getMask(), i);
1676 /// getShuffleMask - Return the full mask for this instruction, where each
1677 /// element is the element number and undef's are returned as -1.
1678 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1680 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1681 return getShuffleMask(getMask(), Result);
1684 SmallVector<int, 16> getShuffleMask() const {
1685 SmallVector<int, 16> Mask;
1686 getShuffleMask(Mask);
1691 // Methods for support type inquiry through isa, cast, and dyn_cast:
1692 static inline bool classof(const Instruction *I) {
1693 return I->getOpcode() == Instruction::ShuffleVector;
1695 static inline bool classof(const Value *V) {
1696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1701 struct OperandTraits<ShuffleVectorInst> :
1702 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1705 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1707 //===----------------------------------------------------------------------===//
1708 // ExtractValueInst Class
1709 //===----------------------------------------------------------------------===//
1711 /// ExtractValueInst - This instruction extracts a struct member or array
1712 /// element value from an aggregate value.
1714 class ExtractValueInst : public UnaryInstruction {
1715 SmallVector<unsigned, 4> Indices;
1717 ExtractValueInst(const ExtractValueInst &EVI);
1718 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1720 /// Constructors - Create a extractvalue instruction with a base aggregate
1721 /// value and a list of indices. The first ctor can optionally insert before
1722 /// an existing instruction, the second appends the new instruction to the
1723 /// specified BasicBlock.
1724 inline ExtractValueInst(Value *Agg,
1725 ArrayRef<unsigned> Idxs,
1726 const Twine &NameStr,
1727 Instruction *InsertBefore);
1728 inline ExtractValueInst(Value *Agg,
1729 ArrayRef<unsigned> Idxs,
1730 const Twine &NameStr, BasicBlock *InsertAtEnd);
1732 // allocate space for exactly one operand
1733 void *operator new(size_t s) {
1734 return User::operator new(s, 1);
1737 virtual ExtractValueInst *clone_impl() const;
1740 static ExtractValueInst *Create(Value *Agg,
1741 ArrayRef<unsigned> Idxs,
1742 const Twine &NameStr = "",
1743 Instruction *InsertBefore = 0) {
1745 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1747 static ExtractValueInst *Create(Value *Agg,
1748 ArrayRef<unsigned> Idxs,
1749 const Twine &NameStr,
1750 BasicBlock *InsertAtEnd) {
1751 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1754 /// getIndexedType - Returns the type of the element that would be extracted
1755 /// with an extractvalue instruction with the specified parameters.
1757 /// Null is returned if the indices are invalid for the specified type.
1758 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1760 typedef const unsigned* idx_iterator;
1761 inline idx_iterator idx_begin() const { return Indices.begin(); }
1762 inline idx_iterator idx_end() const { return Indices.end(); }
1764 Value *getAggregateOperand() {
1765 return getOperand(0);
1767 const Value *getAggregateOperand() const {
1768 return getOperand(0);
1770 static unsigned getAggregateOperandIndex() {
1771 return 0U; // get index for modifying correct operand
1774 ArrayRef<unsigned> getIndices() const {
1778 unsigned getNumIndices() const {
1779 return (unsigned)Indices.size();
1782 bool hasIndices() const {
1786 // Methods for support type inquiry through isa, cast, and dyn_cast:
1787 static inline bool classof(const Instruction *I) {
1788 return I->getOpcode() == Instruction::ExtractValue;
1790 static inline bool classof(const Value *V) {
1791 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1795 ExtractValueInst::ExtractValueInst(Value *Agg,
1796 ArrayRef<unsigned> Idxs,
1797 const Twine &NameStr,
1798 Instruction *InsertBefore)
1799 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1800 ExtractValue, Agg, InsertBefore) {
1801 init(Idxs, NameStr);
1803 ExtractValueInst::ExtractValueInst(Value *Agg,
1804 ArrayRef<unsigned> Idxs,
1805 const Twine &NameStr,
1806 BasicBlock *InsertAtEnd)
1807 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1808 ExtractValue, Agg, InsertAtEnd) {
1809 init(Idxs, NameStr);
1813 //===----------------------------------------------------------------------===//
1814 // InsertValueInst Class
1815 //===----------------------------------------------------------------------===//
1817 /// InsertValueInst - This instruction inserts a struct field of array element
1818 /// value into an aggregate value.
1820 class InsertValueInst : public Instruction {
1821 SmallVector<unsigned, 4> Indices;
1823 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1824 InsertValueInst(const InsertValueInst &IVI);
1825 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1826 const Twine &NameStr);
1828 /// Constructors - Create a insertvalue instruction with a base aggregate
1829 /// value, a value to insert, and a list of indices. The first ctor can
1830 /// optionally insert before an existing instruction, the second appends
1831 /// the new instruction to the specified BasicBlock.
1832 inline InsertValueInst(Value *Agg, Value *Val,
1833 ArrayRef<unsigned> Idxs,
1834 const Twine &NameStr,
1835 Instruction *InsertBefore);
1836 inline InsertValueInst(Value *Agg, Value *Val,
1837 ArrayRef<unsigned> Idxs,
1838 const Twine &NameStr, BasicBlock *InsertAtEnd);
1840 /// Constructors - These two constructors are convenience methods because one
1841 /// and two index insertvalue instructions are so common.
1842 InsertValueInst(Value *Agg, Value *Val,
1843 unsigned Idx, const Twine &NameStr = "",
1844 Instruction *InsertBefore = 0);
1845 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1846 const Twine &NameStr, BasicBlock *InsertAtEnd);
1848 virtual InsertValueInst *clone_impl() const;
1850 // allocate space for exactly two operands
1851 void *operator new(size_t s) {
1852 return User::operator new(s, 2);
1855 static InsertValueInst *Create(Value *Agg, Value *Val,
1856 ArrayRef<unsigned> Idxs,
1857 const Twine &NameStr = "",
1858 Instruction *InsertBefore = 0) {
1859 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1861 static InsertValueInst *Create(Value *Agg, Value *Val,
1862 ArrayRef<unsigned> Idxs,
1863 const Twine &NameStr,
1864 BasicBlock *InsertAtEnd) {
1865 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1868 /// Transparently provide more efficient getOperand methods.
1869 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1871 typedef const unsigned* idx_iterator;
1872 inline idx_iterator idx_begin() const { return Indices.begin(); }
1873 inline idx_iterator idx_end() const { return Indices.end(); }
1875 Value *getAggregateOperand() {
1876 return getOperand(0);
1878 const Value *getAggregateOperand() const {
1879 return getOperand(0);
1881 static unsigned getAggregateOperandIndex() {
1882 return 0U; // get index for modifying correct operand
1885 Value *getInsertedValueOperand() {
1886 return getOperand(1);
1888 const Value *getInsertedValueOperand() const {
1889 return getOperand(1);
1891 static unsigned getInsertedValueOperandIndex() {
1892 return 1U; // get index for modifying correct operand
1895 ArrayRef<unsigned> getIndices() const {
1899 unsigned getNumIndices() const {
1900 return (unsigned)Indices.size();
1903 bool hasIndices() const {
1907 // Methods for support type inquiry through isa, cast, and dyn_cast:
1908 static inline bool classof(const Instruction *I) {
1909 return I->getOpcode() == Instruction::InsertValue;
1911 static inline bool classof(const Value *V) {
1912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1917 struct OperandTraits<InsertValueInst> :
1918 public FixedNumOperandTraits<InsertValueInst, 2> {
1921 InsertValueInst::InsertValueInst(Value *Agg,
1923 ArrayRef<unsigned> Idxs,
1924 const Twine &NameStr,
1925 Instruction *InsertBefore)
1926 : Instruction(Agg->getType(), InsertValue,
1927 OperandTraits<InsertValueInst>::op_begin(this),
1929 init(Agg, Val, Idxs, NameStr);
1931 InsertValueInst::InsertValueInst(Value *Agg,
1933 ArrayRef<unsigned> Idxs,
1934 const Twine &NameStr,
1935 BasicBlock *InsertAtEnd)
1936 : Instruction(Agg->getType(), InsertValue,
1937 OperandTraits<InsertValueInst>::op_begin(this),
1939 init(Agg, Val, Idxs, NameStr);
1942 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1944 //===----------------------------------------------------------------------===//
1946 //===----------------------------------------------------------------------===//
1948 // PHINode - The PHINode class is used to represent the magical mystical PHI
1949 // node, that can not exist in nature, but can be synthesized in a computer
1950 // scientist's overactive imagination.
1952 class PHINode : public Instruction {
1953 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1954 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1955 /// the number actually in use.
1956 unsigned ReservedSpace;
1957 PHINode(const PHINode &PN);
1958 // allocate space for exactly zero operands
1959 void *operator new(size_t s) {
1960 return User::operator new(s, 0);
1962 explicit PHINode(Type *Ty, unsigned NumReservedValues,
1963 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1964 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1965 ReservedSpace(NumReservedValues) {
1967 OperandList = allocHungoffUses(ReservedSpace);
1970 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1971 BasicBlock *InsertAtEnd)
1972 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1973 ReservedSpace(NumReservedValues) {
1975 OperandList = allocHungoffUses(ReservedSpace);
1978 // allocHungoffUses - this is more complicated than the generic
1979 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1980 // values and pointers to the incoming blocks, all in one allocation.
1981 Use *allocHungoffUses(unsigned) const;
1983 virtual PHINode *clone_impl() const;
1985 /// Constructors - NumReservedValues is a hint for the number of incoming
1986 /// edges that this phi node will have (use 0 if you really have no idea).
1987 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
1988 const Twine &NameStr = "",
1989 Instruction *InsertBefore = 0) {
1990 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
1992 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
1993 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1994 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
1998 /// Provide fast operand accessors
1999 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2001 // Block iterator interface. This provides access to the list of incoming
2002 // basic blocks, which parallels the list of incoming values.
2004 typedef BasicBlock **block_iterator;
2005 typedef BasicBlock * const *const_block_iterator;
2007 block_iterator block_begin() {
2009 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2010 return reinterpret_cast<block_iterator>(ref + 1);
2013 const_block_iterator block_begin() const {
2014 const Use::UserRef *ref =
2015 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2016 return reinterpret_cast<const_block_iterator>(ref + 1);
2019 block_iterator block_end() {
2020 return block_begin() + getNumOperands();
2023 const_block_iterator block_end() const {
2024 return block_begin() + getNumOperands();
2027 /// getNumIncomingValues - Return the number of incoming edges
2029 unsigned getNumIncomingValues() const { return getNumOperands(); }
2031 /// getIncomingValue - Return incoming value number x
2033 Value *getIncomingValue(unsigned i) const {
2034 return getOperand(i);
2036 void setIncomingValue(unsigned i, Value *V) {
2039 static unsigned getOperandNumForIncomingValue(unsigned i) {
2042 static unsigned getIncomingValueNumForOperand(unsigned i) {
2046 /// getIncomingBlock - Return incoming basic block number @p i.
2048 BasicBlock *getIncomingBlock(unsigned i) const {
2049 return block_begin()[i];
2052 /// getIncomingBlock - Return incoming basic block corresponding
2053 /// to an operand of the PHI.
2055 BasicBlock *getIncomingBlock(const Use &U) const {
2056 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2057 return getIncomingBlock(unsigned(&U - op_begin()));
2060 /// getIncomingBlock - Return incoming basic block corresponding
2061 /// to value use iterator.
2063 template <typename U>
2064 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2065 return getIncomingBlock(I.getUse());
2068 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2069 block_begin()[i] = BB;
2072 /// addIncoming - Add an incoming value to the end of the PHI list
2074 void addIncoming(Value *V, BasicBlock *BB) {
2075 assert(V && "PHI node got a null value!");
2076 assert(BB && "PHI node got a null basic block!");
2077 assert(getType() == V->getType() &&
2078 "All operands to PHI node must be the same type as the PHI node!");
2079 if (NumOperands == ReservedSpace)
2080 growOperands(); // Get more space!
2081 // Initialize some new operands.
2083 setIncomingValue(NumOperands - 1, V);
2084 setIncomingBlock(NumOperands - 1, BB);
2087 /// removeIncomingValue - Remove an incoming value. This is useful if a
2088 /// predecessor basic block is deleted. The value removed is returned.
2090 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2091 /// is true), the PHI node is destroyed and any uses of it are replaced with
2092 /// dummy values. The only time there should be zero incoming values to a PHI
2093 /// node is when the block is dead, so this strategy is sound.
2095 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2097 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2098 int Idx = getBasicBlockIndex(BB);
2099 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2100 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2103 /// getBasicBlockIndex - Return the first index of the specified basic
2104 /// block in the value list for this PHI. Returns -1 if no instance.
2106 int getBasicBlockIndex(const BasicBlock *BB) const {
2107 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2108 if (block_begin()[i] == BB)
2113 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2114 int Idx = getBasicBlockIndex(BB);
2115 assert(Idx >= 0 && "Invalid basic block argument!");
2116 return getIncomingValue(Idx);
2119 /// hasConstantValue - If the specified PHI node always merges together the
2120 /// same value, return the value, otherwise return null.
2121 Value *hasConstantValue() const;
2123 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2124 static inline bool classof(const Instruction *I) {
2125 return I->getOpcode() == Instruction::PHI;
2127 static inline bool classof(const Value *V) {
2128 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2131 void growOperands();
2135 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2138 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2140 //===----------------------------------------------------------------------===//
2141 // LandingPadInst Class
2142 //===----------------------------------------------------------------------===//
2144 //===---------------------------------------------------------------------------
2145 /// LandingPadInst - The landingpad instruction holds all of the information
2146 /// necessary to generate correct exception handling. The landingpad instruction
2147 /// cannot be moved from the top of a landing pad block, which itself is
2148 /// accessible only from the 'unwind' edge of an invoke. This uses the
2149 /// SubclassData field in Value to store whether or not the landingpad is a
2152 class LandingPadInst : public Instruction {
2153 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2154 /// the number actually in use.
2155 unsigned ReservedSpace;
2156 LandingPadInst(const LandingPadInst &LP);
2158 enum ClauseType { Catch, Filter };
2160 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2161 // Allocate space for exactly zero operands.
2162 void *operator new(size_t s) {
2163 return User::operator new(s, 0);
2165 void growOperands(unsigned Size);
2166 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2168 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2169 unsigned NumReservedValues, const Twine &NameStr,
2170 Instruction *InsertBefore);
2171 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2172 unsigned NumReservedValues, const Twine &NameStr,
2173 BasicBlock *InsertAtEnd);
2175 virtual LandingPadInst *clone_impl() const;
2177 /// Constructors - NumReservedClauses is a hint for the number of incoming
2178 /// clauses that this landingpad will have (use 0 if you really have no idea).
2179 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2180 unsigned NumReservedClauses,
2181 const Twine &NameStr = "",
2182 Instruction *InsertBefore = 0);
2183 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2184 unsigned NumReservedClauses,
2185 const Twine &NameStr, BasicBlock *InsertAtEnd);
2188 /// Provide fast operand accessors
2189 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2191 /// getPersonalityFn - Get the personality function associated with this
2193 Value *getPersonalityFn() const { return getOperand(0); }
2195 /// isCleanup - Return 'true' if this landingpad instruction is a
2196 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2197 /// doesn't catch the exception.
2198 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2200 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2201 void setCleanup(bool V) {
2202 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2206 /// addClause - Add a catch or filter clause to the landing pad.
2207 void addClause(Value *ClauseVal);
2209 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2210 /// to determine what type of clause this is.
2211 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2213 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2214 bool isCatch(unsigned Idx) const {
2215 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2218 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2219 bool isFilter(unsigned Idx) const {
2220 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2223 /// getNumClauses - Get the number of clauses for this landing pad.
2224 unsigned getNumClauses() const { return getNumOperands() - 1; }
2226 /// reserveClauses - Grow the size of the operand list to accommodate the new
2227 /// number of clauses.
2228 void reserveClauses(unsigned Size) { growOperands(Size); }
2230 // Methods for support type inquiry through isa, cast, and dyn_cast:
2231 static inline bool classof(const Instruction *I) {
2232 return I->getOpcode() == Instruction::LandingPad;
2234 static inline bool classof(const Value *V) {
2235 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2240 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2243 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2245 //===----------------------------------------------------------------------===//
2247 //===----------------------------------------------------------------------===//
2249 //===---------------------------------------------------------------------------
2250 /// ReturnInst - Return a value (possibly void), from a function. Execution
2251 /// does not continue in this function any longer.
2253 class ReturnInst : public TerminatorInst {
2254 ReturnInst(const ReturnInst &RI);
2257 // ReturnInst constructors:
2258 // ReturnInst() - 'ret void' instruction
2259 // ReturnInst( null) - 'ret void' instruction
2260 // ReturnInst(Value* X) - 'ret X' instruction
2261 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2262 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2263 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2264 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2266 // NOTE: If the Value* passed is of type void then the constructor behaves as
2267 // if it was passed NULL.
2268 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2269 Instruction *InsertBefore = 0);
2270 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2271 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2273 virtual ReturnInst *clone_impl() const;
2275 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2276 Instruction *InsertBefore = 0) {
2277 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2279 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2280 BasicBlock *InsertAtEnd) {
2281 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2283 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2284 return new(0) ReturnInst(C, InsertAtEnd);
2286 virtual ~ReturnInst();
2288 /// Provide fast operand accessors
2289 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2291 /// Convenience accessor. Returns null if there is no return value.
2292 Value *getReturnValue() const {
2293 return getNumOperands() != 0 ? getOperand(0) : 0;
2296 unsigned getNumSuccessors() const { return 0; }
2298 // Methods for support type inquiry through isa, cast, and dyn_cast:
2299 static inline bool classof(const Instruction *I) {
2300 return (I->getOpcode() == Instruction::Ret);
2302 static inline bool classof(const Value *V) {
2303 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2306 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2307 virtual unsigned getNumSuccessorsV() const;
2308 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2312 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2315 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2317 //===----------------------------------------------------------------------===//
2319 //===----------------------------------------------------------------------===//
2321 //===---------------------------------------------------------------------------
2322 /// BranchInst - Conditional or Unconditional Branch instruction.
2324 class BranchInst : public TerminatorInst {
2325 /// Ops list - Branches are strange. The operands are ordered:
2326 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2327 /// they don't have to check for cond/uncond branchness. These are mostly
2328 /// accessed relative from op_end().
2329 BranchInst(const BranchInst &BI);
2331 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2332 // BranchInst(BB *B) - 'br B'
2333 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2334 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2335 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2336 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2337 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2338 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2339 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2340 Instruction *InsertBefore = 0);
2341 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2342 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2343 BasicBlock *InsertAtEnd);
2345 virtual BranchInst *clone_impl() const;
2347 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2348 return new(1) BranchInst(IfTrue, InsertBefore);
2350 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2351 Value *Cond, Instruction *InsertBefore = 0) {
2352 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2354 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2355 return new(1) BranchInst(IfTrue, InsertAtEnd);
2357 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2358 Value *Cond, BasicBlock *InsertAtEnd) {
2359 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2362 /// Transparently provide more efficient getOperand methods.
2363 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2365 bool isUnconditional() const { return getNumOperands() == 1; }
2366 bool isConditional() const { return getNumOperands() == 3; }
2368 Value *getCondition() const {
2369 assert(isConditional() && "Cannot get condition of an uncond branch!");
2373 void setCondition(Value *V) {
2374 assert(isConditional() && "Cannot set condition of unconditional branch!");
2378 unsigned getNumSuccessors() const { return 1+isConditional(); }
2380 BasicBlock *getSuccessor(unsigned i) const {
2381 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2382 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2385 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2386 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2387 *(&Op<-1>() - idx) = (Value*)NewSucc;
2390 /// \brief Swap the successors of this branch instruction.
2392 /// Swaps the successors of the branch instruction. This also swaps any
2393 /// branch weight metadata associated with the instruction so that it
2394 /// continues to map correctly to each operand.
2395 void swapSuccessors();
2397 // Methods for support type inquiry through isa, cast, and dyn_cast:
2398 static inline bool classof(const Instruction *I) {
2399 return (I->getOpcode() == Instruction::Br);
2401 static inline bool classof(const Value *V) {
2402 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2405 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2406 virtual unsigned getNumSuccessorsV() const;
2407 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2411 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2414 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2416 //===----------------------------------------------------------------------===//
2418 //===----------------------------------------------------------------------===//
2420 //===---------------------------------------------------------------------------
2421 /// SwitchInst - Multiway switch
2423 class SwitchInst : public TerminatorInst {
2424 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2425 unsigned ReservedSpace;
2427 // Operand[0] = Value to switch on
2428 // Operand[1] = Default basic block destination
2429 // Operand[2n ] = Value to match
2430 // Operand[2n+1] = BasicBlock to go to on match
2432 // Store case values separately from operands list. We needn't User-Use
2433 // concept here, since it is just a case value, it will always constant,
2434 // and case value couldn't reused with another instructions/values.
2436 // It allows us to use custom type for case values that is not inherited
2437 // from Value. Since case value is a complex type that implements
2438 // the subset of integers, we needn't extract sub-constants within
2439 // slow getAggregateElement method.
2440 // For case values we will use std::list to by two reasons:
2441 // 1. It allows to add/remove cases without whole collection reallocation.
2442 // 2. In most of cases we needn't random access.
2443 // Currently case values are also stored in Operands List, but it will moved
2444 // out in future commits.
2445 typedef std::list<IntegersSubset> Subsets;
2446 typedef Subsets::iterator SubsetsIt;
2447 typedef Subsets::const_iterator SubsetsConstIt;
2451 SwitchInst(const SwitchInst &SI);
2452 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2453 void growOperands();
2454 // allocate space for exactly zero operands
2455 void *operator new(size_t s) {
2456 return User::operator new(s, 0);
2458 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2459 /// switch on and a default destination. The number of additional cases can
2460 /// be specified here to make memory allocation more efficient. This
2461 /// constructor can also autoinsert before another instruction.
2462 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2463 Instruction *InsertBefore);
2465 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2466 /// switch on and a default destination. The number of additional cases can
2467 /// be specified here to make memory allocation more efficient. This
2468 /// constructor also autoinserts at the end of the specified BasicBlock.
2469 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2470 BasicBlock *InsertAtEnd);
2472 virtual SwitchInst *clone_impl() const;
2475 // FIXME: Currently there are a lot of unclean template parameters,
2476 // we need to make refactoring in future.
2477 // All these parameters are used to implement both iterator and const_iterator
2478 // without code duplication.
2479 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2480 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2481 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2482 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2483 template <class SwitchInstTy, class ConstantIntTy,
2484 class SubsetsItTy, class BasicBlockTy>
2485 class CaseIteratorT;
2487 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2488 SubsetsConstIt, const BasicBlock> ConstCaseIt;
2492 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2494 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2495 unsigned NumCases, Instruction *InsertBefore = 0) {
2496 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2498 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2499 unsigned NumCases, BasicBlock *InsertAtEnd) {
2500 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2505 /// Provide fast operand accessors
2506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2508 // Accessor Methods for Switch stmt
2509 Value *getCondition() const { return getOperand(0); }
2510 void setCondition(Value *V) { setOperand(0, V); }
2512 BasicBlock *getDefaultDest() const {
2513 return cast<BasicBlock>(getOperand(1));
2516 void setDefaultDest(BasicBlock *DefaultCase) {
2517 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2520 /// getNumCases - return the number of 'cases' in this switch instruction,
2521 /// except the default case
2522 unsigned getNumCases() const {
2523 return getNumOperands()/2 - 1;
2526 /// Returns a read/write iterator that points to the first
2527 /// case in SwitchInst.
2528 CaseIt case_begin() {
2529 return CaseIt(this, 0, TheSubsets.begin());
2531 /// Returns a read-only iterator that points to the first
2532 /// case in the SwitchInst.
2533 ConstCaseIt case_begin() const {
2534 return ConstCaseIt(this, 0, TheSubsets.begin());
2537 /// Returns a read/write iterator that points one past the last
2538 /// in the SwitchInst.
2540 return CaseIt(this, getNumCases(), TheSubsets.end());
2542 /// Returns a read-only iterator that points one past the last
2543 /// in the SwitchInst.
2544 ConstCaseIt case_end() const {
2545 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2547 /// Returns an iterator that points to the default case.
2548 /// Note: this iterator allows to resolve successor only. Attempt
2549 /// to resolve case value causes an assertion.
2550 /// Also note, that increment and decrement also causes an assertion and
2551 /// makes iterator invalid.
2552 CaseIt case_default() {
2553 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2555 ConstCaseIt case_default() const {
2556 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2559 /// findCaseValue - Search all of the case values for the specified constant.
2560 /// If it is explicitly handled, return the case iterator of it, otherwise
2561 /// return default case iterator to indicate
2562 /// that it is handled by the default handler.
2563 CaseIt findCaseValue(const ConstantInt *C) {
2564 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2565 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2567 return case_default();
2569 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2570 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2571 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2573 return case_default();
2576 /// findCaseDest - Finds the unique case value for a given successor. Returns
2577 /// null if the successor is not found, not unique, or is the default case.
2578 ConstantInt *findCaseDest(BasicBlock *BB) {
2579 if (BB == getDefaultDest()) return NULL;
2581 ConstantInt *CI = NULL;
2582 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2583 if (i.getCaseSuccessor() == BB) {
2584 if (CI) return NULL; // Multiple cases lead to BB.
2585 else CI = i.getCaseValue();
2591 /// addCase - Add an entry to the switch instruction...
2594 /// This action invalidates case_end(). Old case_end() iterator will
2595 /// point to the added case.
2596 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2598 /// addCase - Add an entry to the switch instruction.
2600 /// This action invalidates case_end(). Old case_end() iterator will
2601 /// point to the added case.
2602 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2604 /// removeCase - This method removes the specified case and its successor
2605 /// from the switch instruction. Note that this operation may reorder the
2606 /// remaining cases at index idx and above.
2608 /// This action invalidates iterators for all cases following the one removed,
2609 /// including the case_end() iterator.
2610 void removeCase(CaseIt& i);
2612 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2613 BasicBlock *getSuccessor(unsigned idx) const {
2614 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2615 return cast<BasicBlock>(getOperand(idx*2+1));
2617 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2618 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2619 setOperand(idx*2+1, (Value*)NewSucc);
2622 uint16_t hash() const {
2623 uint32_t NumberOfCases = (uint32_t)getNumCases();
2624 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2625 for (ConstCaseIt i = case_begin(), e = case_end();
2627 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2628 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2633 // Case iterators definition.
2635 template <class SwitchInstTy, class ConstantIntTy,
2636 class SubsetsItTy, class BasicBlockTy>
2637 class CaseIteratorT {
2641 unsigned long Index;
2642 SubsetsItTy SubsetIt;
2644 /// Initializes case iterator for given SwitchInst and for given
2646 friend class SwitchInst;
2647 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2648 SubsetsItTy CaseValueIt) {
2650 Index = SuccessorIndex;
2651 this->SubsetIt = CaseValueIt;
2655 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2656 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2657 SubsetsItTy, BasicBlockTy> Self;
2659 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2662 SubsetIt = SI->TheSubsets.begin();
2663 std::advance(SubsetIt, CaseNum);
2667 /// Initializes case iterator for given SwitchInst and for given
2668 /// TerminatorInst's successor index.
2669 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2670 assert(SuccessorIndex < SI->getNumSuccessors() &&
2671 "Successor index # out of range!");
2672 return SuccessorIndex != 0 ?
2673 Self(SI, SuccessorIndex - 1) :
2674 Self(SI, DefaultPseudoIndex);
2677 /// Resolves case value for current case.
2679 ConstantIntTy *getCaseValue() {
2680 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2681 IntegersSubsetRef CaseRanges = *SubsetIt;
2683 // FIXME: Currently we work with ConstantInt based cases.
2684 // So return CaseValue as ConstantInt.
2685 return CaseRanges.getSingleNumber(0).toConstantInt();
2688 /// Resolves case value for current case.
2689 IntegersSubsetRef getCaseValueEx() {
2690 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2694 /// Resolves successor for current case.
2695 BasicBlockTy *getCaseSuccessor() {
2696 assert((Index < SI->getNumCases() ||
2697 Index == DefaultPseudoIndex) &&
2698 "Index out the number of cases.");
2699 return SI->getSuccessor(getSuccessorIndex());
2702 /// Returns number of current case.
2703 unsigned getCaseIndex() const { return Index; }
2705 /// Returns TerminatorInst's successor index for current case successor.
2706 unsigned getSuccessorIndex() const {
2707 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2708 "Index out the number of cases.");
2709 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2713 // Check index correctness after increment.
2714 // Note: Index == getNumCases() means end().
2715 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2718 SubsetIt = SI->TheSubsets.begin();
2723 Self operator++(int) {
2729 // Check index correctness after decrement.
2730 // Note: Index == getNumCases() means end().
2731 // Also allow "-1" iterator here. That will became valid after ++.
2732 unsigned NumCases = SI->getNumCases();
2733 assert((Index == 0 || Index-1 <= NumCases) &&
2734 "Index out the number of cases.");
2736 if (Index == NumCases) {
2737 SubsetIt = SI->TheSubsets.end();
2746 Self operator--(int) {
2751 bool operator==(const Self& RHS) const {
2752 assert(RHS.SI == SI && "Incompatible operators.");
2753 return RHS.Index == Index;
2755 bool operator!=(const Self& RHS) const {
2756 assert(RHS.SI == SI && "Incompatible operators.");
2757 return RHS.Index != Index;
2761 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2762 SubsetsIt, BasicBlock> {
2763 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2767 friend class SwitchInst;
2768 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2769 ParentTy(SI, CaseNum, SubsetIt) {}
2771 void updateCaseValueOperand(IntegersSubset& V) {
2772 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2777 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2779 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2781 /// Sets the new value for current case.
2783 void setValue(ConstantInt *V) {
2784 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2785 IntegersSubsetToBB Mapping;
2786 // FIXME: Currently we work with ConstantInt based cases.
2787 // So inititalize IntItem container directly from ConstantInt.
2788 Mapping.add(IntItem::fromConstantInt(V));
2789 *SubsetIt = Mapping.getCase();
2790 updateCaseValueOperand(*SubsetIt);
2793 /// Sets the new value for current case.
2794 void setValueEx(IntegersSubset& V) {
2795 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2797 updateCaseValueOperand(*SubsetIt);
2800 /// Sets the new successor for current case.
2801 void setSuccessor(BasicBlock *S) {
2802 SI->setSuccessor(getSuccessorIndex(), S);
2806 // Methods for support type inquiry through isa, cast, and dyn_cast:
2808 static inline bool classof(const Instruction *I) {
2809 return I->getOpcode() == Instruction::Switch;
2811 static inline bool classof(const Value *V) {
2812 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2815 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2816 virtual unsigned getNumSuccessorsV() const;
2817 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2821 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2824 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2827 //===----------------------------------------------------------------------===//
2828 // IndirectBrInst Class
2829 //===----------------------------------------------------------------------===//
2831 //===---------------------------------------------------------------------------
2832 /// IndirectBrInst - Indirect Branch Instruction.
2834 class IndirectBrInst : public TerminatorInst {
2835 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2836 unsigned ReservedSpace;
2837 // Operand[0] = Value to switch on
2838 // Operand[1] = Default basic block destination
2839 // Operand[2n ] = Value to match
2840 // Operand[2n+1] = BasicBlock to go to on match
2841 IndirectBrInst(const IndirectBrInst &IBI);
2842 void init(Value *Address, unsigned NumDests);
2843 void growOperands();
2844 // allocate space for exactly zero operands
2845 void *operator new(size_t s) {
2846 return User::operator new(s, 0);
2848 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2849 /// Address to jump to. The number of expected destinations can be specified
2850 /// here to make memory allocation more efficient. This constructor can also
2851 /// autoinsert before another instruction.
2852 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2854 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2855 /// Address to jump to. The number of expected destinations can be specified
2856 /// here to make memory allocation more efficient. This constructor also
2857 /// autoinserts at the end of the specified BasicBlock.
2858 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2860 virtual IndirectBrInst *clone_impl() const;
2862 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2863 Instruction *InsertBefore = 0) {
2864 return new IndirectBrInst(Address, NumDests, InsertBefore);
2866 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2867 BasicBlock *InsertAtEnd) {
2868 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2872 /// Provide fast operand accessors.
2873 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2875 // Accessor Methods for IndirectBrInst instruction.
2876 Value *getAddress() { return getOperand(0); }
2877 const Value *getAddress() const { return getOperand(0); }
2878 void setAddress(Value *V) { setOperand(0, V); }
2881 /// getNumDestinations - return the number of possible destinations in this
2882 /// indirectbr instruction.
2883 unsigned getNumDestinations() const { return getNumOperands()-1; }
2885 /// getDestination - Return the specified destination.
2886 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2887 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2889 /// addDestination - Add a destination.
2891 void addDestination(BasicBlock *Dest);
2893 /// removeDestination - This method removes the specified successor from the
2894 /// indirectbr instruction.
2895 void removeDestination(unsigned i);
2897 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2898 BasicBlock *getSuccessor(unsigned i) const {
2899 return cast<BasicBlock>(getOperand(i+1));
2901 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2902 setOperand(i+1, (Value*)NewSucc);
2905 // Methods for support type inquiry through isa, cast, and dyn_cast:
2906 static inline bool classof(const Instruction *I) {
2907 return I->getOpcode() == Instruction::IndirectBr;
2909 static inline bool classof(const Value *V) {
2910 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2913 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2914 virtual unsigned getNumSuccessorsV() const;
2915 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2919 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2922 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2925 //===----------------------------------------------------------------------===//
2927 //===----------------------------------------------------------------------===//
2929 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2930 /// calling convention of the call.
2932 class InvokeInst : public TerminatorInst {
2933 AttrListPtr AttributeList;
2934 InvokeInst(const InvokeInst &BI);
2935 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2936 ArrayRef<Value *> Args, const Twine &NameStr);
2938 /// Construct an InvokeInst given a range of arguments.
2940 /// \brief Construct an InvokeInst from a range of arguments
2941 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2942 ArrayRef<Value *> Args, unsigned Values,
2943 const Twine &NameStr, Instruction *InsertBefore);
2945 /// Construct an InvokeInst given a range of arguments.
2947 /// \brief Construct an InvokeInst from a range of arguments
2948 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2949 ArrayRef<Value *> Args, unsigned Values,
2950 const Twine &NameStr, BasicBlock *InsertAtEnd);
2952 virtual InvokeInst *clone_impl() const;
2954 static InvokeInst *Create(Value *Func,
2955 BasicBlock *IfNormal, BasicBlock *IfException,
2956 ArrayRef<Value *> Args, const Twine &NameStr = "",
2957 Instruction *InsertBefore = 0) {
2958 unsigned Values = unsigned(Args.size()) + 3;
2959 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2960 Values, NameStr, InsertBefore);
2962 static InvokeInst *Create(Value *Func,
2963 BasicBlock *IfNormal, BasicBlock *IfException,
2964 ArrayRef<Value *> Args, const Twine &NameStr,
2965 BasicBlock *InsertAtEnd) {
2966 unsigned Values = unsigned(Args.size()) + 3;
2967 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2968 Values, NameStr, InsertAtEnd);
2971 /// Provide fast operand accessors
2972 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2974 /// getNumArgOperands - Return the number of invoke arguments.
2976 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2978 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2980 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2981 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2983 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2985 CallingConv::ID getCallingConv() const {
2986 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2988 void setCallingConv(CallingConv::ID CC) {
2989 setInstructionSubclassData(static_cast<unsigned>(CC));
2992 /// getAttributes - Return the parameter attributes for this invoke.
2994 const AttrListPtr &getAttributes() const { return AttributeList; }
2996 /// setAttributes - Set the parameter attributes for this invoke.
2998 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3000 /// addAttribute - adds the attribute to the list of attributes.
3001 void addAttribute(unsigned i, Attributes attr);
3003 /// removeAttribute - removes the attribute from the list of attributes.
3004 void removeAttribute(unsigned i, Attributes attr);
3006 /// \brief Determine whether this call has the NoAlias attribute.
3007 bool hasFnAttr(Attributes::AttrVal A) const;
3009 /// \brief Determine whether the call or the callee has the given attributes.
3010 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
3012 /// \brief Extract the alignment for a call or parameter (0=unknown).
3013 unsigned getParamAlignment(unsigned i) const {
3014 return AttributeList.getParamAlignment(i);
3017 /// \brief Return true if the call should not be inlined.
3018 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
3019 void setIsNoInline() {
3020 addAttribute(AttrListPtr::FunctionIndex,
3021 Attributes::get(getContext(), Attributes::NoInline));
3024 /// \brief Determine if the call does not access memory.
3025 bool doesNotAccessMemory() const {
3026 return hasFnAttr(Attributes::ReadNone);
3028 void setDoesNotAccessMemory() {
3029 addAttribute(AttrListPtr::FunctionIndex,
3030 Attributes::get(getContext(), Attributes::ReadNone));
3033 /// \brief Determine if the call does not access or only reads memory.
3034 bool onlyReadsMemory() const {
3035 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
3037 void setOnlyReadsMemory() {
3038 addAttribute(AttrListPtr::FunctionIndex,
3039 Attributes::get(getContext(), Attributes::ReadOnly));
3042 /// \brief Determine if the call cannot return.
3043 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
3044 void setDoesNotReturn() {
3045 addAttribute(AttrListPtr::FunctionIndex,
3046 Attributes::get(getContext(), Attributes::NoReturn));
3049 /// \brief Determine if the call cannot unwind.
3050 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
3051 void setDoesNotThrow() {
3052 addAttribute(AttrListPtr::FunctionIndex,
3053 Attributes::get(getContext(), Attributes::NoUnwind));
3056 /// \brief Determine if the call returns a structure through first
3057 /// pointer argument.
3058 bool hasStructRetAttr() const {
3059 // Be friendly and also check the callee.
3060 return paramHasAttr(1, Attributes::StructRet);
3063 /// \brief Determine if any call argument is an aggregate passed by value.
3064 bool hasByValArgument() const {
3065 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3066 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
3071 /// getCalledFunction - Return the function called, or null if this is an
3072 /// indirect function invocation.
3074 Function *getCalledFunction() const {
3075 return dyn_cast<Function>(Op<-3>());
3078 /// getCalledValue - Get a pointer to the function that is invoked by this
3080 const Value *getCalledValue() const { return Op<-3>(); }
3081 Value *getCalledValue() { return Op<-3>(); }
3083 /// setCalledFunction - Set the function called.
3084 void setCalledFunction(Value* Fn) {
3088 // get*Dest - Return the destination basic blocks...
3089 BasicBlock *getNormalDest() const {
3090 return cast<BasicBlock>(Op<-2>());
3092 BasicBlock *getUnwindDest() const {
3093 return cast<BasicBlock>(Op<-1>());
3095 void setNormalDest(BasicBlock *B) {
3096 Op<-2>() = reinterpret_cast<Value*>(B);
3098 void setUnwindDest(BasicBlock *B) {
3099 Op<-1>() = reinterpret_cast<Value*>(B);
3102 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3103 /// block (the unwind destination).
3104 LandingPadInst *getLandingPadInst() const;
3106 BasicBlock *getSuccessor(unsigned i) const {
3107 assert(i < 2 && "Successor # out of range for invoke!");
3108 return i == 0 ? getNormalDest() : getUnwindDest();
3111 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3112 assert(idx < 2 && "Successor # out of range for invoke!");
3113 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3116 unsigned getNumSuccessors() const { return 2; }
3118 // Methods for support type inquiry through isa, cast, and dyn_cast:
3119 static inline bool classof(const Instruction *I) {
3120 return (I->getOpcode() == Instruction::Invoke);
3122 static inline bool classof(const Value *V) {
3123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3127 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3128 virtual unsigned getNumSuccessorsV() const;
3129 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3131 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3132 // method so that subclasses cannot accidentally use it.
3133 void setInstructionSubclassData(unsigned short D) {
3134 Instruction::setInstructionSubclassData(D);
3139 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3142 InvokeInst::InvokeInst(Value *Func,
3143 BasicBlock *IfNormal, BasicBlock *IfException,
3144 ArrayRef<Value *> Args, unsigned Values,
3145 const Twine &NameStr, Instruction *InsertBefore)
3146 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3147 ->getElementType())->getReturnType(),
3148 Instruction::Invoke,
3149 OperandTraits<InvokeInst>::op_end(this) - Values,
3150 Values, InsertBefore) {
3151 init(Func, IfNormal, IfException, Args, NameStr);
3153 InvokeInst::InvokeInst(Value *Func,
3154 BasicBlock *IfNormal, BasicBlock *IfException,
3155 ArrayRef<Value *> Args, unsigned Values,
3156 const Twine &NameStr, BasicBlock *InsertAtEnd)
3157 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3158 ->getElementType())->getReturnType(),
3159 Instruction::Invoke,
3160 OperandTraits<InvokeInst>::op_end(this) - Values,
3161 Values, InsertAtEnd) {
3162 init(Func, IfNormal, IfException, Args, NameStr);
3165 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3167 //===----------------------------------------------------------------------===//
3169 //===----------------------------------------------------------------------===//
3171 //===---------------------------------------------------------------------------
3172 /// ResumeInst - Resume the propagation of an exception.
3174 class ResumeInst : public TerminatorInst {
3175 ResumeInst(const ResumeInst &RI);
3177 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3178 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3180 virtual ResumeInst *clone_impl() const;
3182 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3183 return new(1) ResumeInst(Exn, InsertBefore);
3185 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3186 return new(1) ResumeInst(Exn, InsertAtEnd);
3189 /// Provide fast operand accessors
3190 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3192 /// Convenience accessor.
3193 Value *getValue() const { return Op<0>(); }
3195 unsigned getNumSuccessors() const { return 0; }
3197 // Methods for support type inquiry through isa, cast, and dyn_cast:
3198 static inline bool classof(const Instruction *I) {
3199 return I->getOpcode() == Instruction::Resume;
3201 static inline bool classof(const Value *V) {
3202 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3205 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3206 virtual unsigned getNumSuccessorsV() const;
3207 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3211 struct OperandTraits<ResumeInst> :
3212 public FixedNumOperandTraits<ResumeInst, 1> {
3215 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3217 //===----------------------------------------------------------------------===//
3218 // UnreachableInst Class
3219 //===----------------------------------------------------------------------===//
3221 //===---------------------------------------------------------------------------
3222 /// UnreachableInst - This function has undefined behavior. In particular, the
3223 /// presence of this instruction indicates some higher level knowledge that the
3224 /// end of the block cannot be reached.
3226 class UnreachableInst : public TerminatorInst {
3227 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3229 virtual UnreachableInst *clone_impl() const;
3232 // allocate space for exactly zero operands
3233 void *operator new(size_t s) {
3234 return User::operator new(s, 0);
3236 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3237 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3239 unsigned getNumSuccessors() const { return 0; }
3241 // Methods for support type inquiry through isa, cast, and dyn_cast:
3242 static inline bool classof(const Instruction *I) {
3243 return I->getOpcode() == Instruction::Unreachable;
3245 static inline bool classof(const Value *V) {
3246 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3249 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3250 virtual unsigned getNumSuccessorsV() const;
3251 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3254 //===----------------------------------------------------------------------===//
3256 //===----------------------------------------------------------------------===//
3258 /// \brief This class represents a truncation of integer types.
3259 class TruncInst : public CastInst {
3261 /// \brief Clone an identical TruncInst
3262 virtual TruncInst *clone_impl() const;
3265 /// \brief Constructor with insert-before-instruction semantics
3267 Value *S, ///< The value to be truncated
3268 Type *Ty, ///< The (smaller) type to truncate to
3269 const Twine &NameStr = "", ///< A name for the new instruction
3270 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3273 /// \brief Constructor with insert-at-end-of-block semantics
3275 Value *S, ///< The value to be truncated
3276 Type *Ty, ///< The (smaller) type to truncate to
3277 const Twine &NameStr, ///< A name for the new instruction
3278 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3281 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3282 static inline bool classof(const Instruction *I) {
3283 return I->getOpcode() == Trunc;
3285 static inline bool classof(const Value *V) {
3286 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3290 //===----------------------------------------------------------------------===//
3292 //===----------------------------------------------------------------------===//
3294 /// \brief This class represents zero extension of integer types.
3295 class ZExtInst : public CastInst {
3297 /// \brief Clone an identical ZExtInst
3298 virtual ZExtInst *clone_impl() const;
3301 /// \brief Constructor with insert-before-instruction semantics
3303 Value *S, ///< The value to be zero extended
3304 Type *Ty, ///< The type to zero extend to
3305 const Twine &NameStr = "", ///< A name for the new instruction
3306 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3309 /// \brief Constructor with insert-at-end semantics.
3311 Value *S, ///< The value to be zero extended
3312 Type *Ty, ///< The type to zero extend to
3313 const Twine &NameStr, ///< A name for the new instruction
3314 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3317 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3318 static inline bool classof(const Instruction *I) {
3319 return I->getOpcode() == ZExt;
3321 static inline bool classof(const Value *V) {
3322 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3326 //===----------------------------------------------------------------------===//
3328 //===----------------------------------------------------------------------===//
3330 /// \brief This class represents a sign extension of integer types.
3331 class SExtInst : public CastInst {
3333 /// \brief Clone an identical SExtInst
3334 virtual SExtInst *clone_impl() const;
3337 /// \brief Constructor with insert-before-instruction semantics
3339 Value *S, ///< The value to be sign extended
3340 Type *Ty, ///< The type to sign extend to
3341 const Twine &NameStr = "", ///< A name for the new instruction
3342 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3345 /// \brief Constructor with insert-at-end-of-block semantics
3347 Value *S, ///< The value to be sign extended
3348 Type *Ty, ///< The type to sign extend to
3349 const Twine &NameStr, ///< A name for the new instruction
3350 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3353 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3354 static inline bool classof(const Instruction *I) {
3355 return I->getOpcode() == SExt;
3357 static inline bool classof(const Value *V) {
3358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3362 //===----------------------------------------------------------------------===//
3363 // FPTruncInst Class
3364 //===----------------------------------------------------------------------===//
3366 /// \brief This class represents a truncation of floating point types.
3367 class FPTruncInst : public CastInst {
3369 /// \brief Clone an identical FPTruncInst
3370 virtual FPTruncInst *clone_impl() const;
3373 /// \brief Constructor with insert-before-instruction semantics
3375 Value *S, ///< The value to be truncated
3376 Type *Ty, ///< The type to truncate to
3377 const Twine &NameStr = "", ///< A name for the new instruction
3378 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3381 /// \brief Constructor with insert-before-instruction semantics
3383 Value *S, ///< The value to be truncated
3384 Type *Ty, ///< The type to truncate to
3385 const Twine &NameStr, ///< A name for the new instruction
3386 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3389 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3390 static inline bool classof(const Instruction *I) {
3391 return I->getOpcode() == FPTrunc;
3393 static inline bool classof(const Value *V) {
3394 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3398 //===----------------------------------------------------------------------===//
3400 //===----------------------------------------------------------------------===//
3402 /// \brief This class represents an extension of floating point types.
3403 class FPExtInst : public CastInst {
3405 /// \brief Clone an identical FPExtInst
3406 virtual FPExtInst *clone_impl() const;
3409 /// \brief Constructor with insert-before-instruction semantics
3411 Value *S, ///< The value to be extended
3412 Type *Ty, ///< The type to extend to
3413 const Twine &NameStr = "", ///< A name for the new instruction
3414 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3417 /// \brief Constructor with insert-at-end-of-block semantics
3419 Value *S, ///< The value to be extended
3420 Type *Ty, ///< The type to extend to
3421 const Twine &NameStr, ///< A name for the new instruction
3422 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3425 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3426 static inline bool classof(const Instruction *I) {
3427 return I->getOpcode() == FPExt;
3429 static inline bool classof(const Value *V) {
3430 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3434 //===----------------------------------------------------------------------===//
3436 //===----------------------------------------------------------------------===//
3438 /// \brief This class represents a cast unsigned integer to floating point.
3439 class UIToFPInst : public CastInst {
3441 /// \brief Clone an identical UIToFPInst
3442 virtual UIToFPInst *clone_impl() const;
3445 /// \brief Constructor with insert-before-instruction semantics
3447 Value *S, ///< The value to be converted
3448 Type *Ty, ///< The type to convert to
3449 const Twine &NameStr = "", ///< A name for the new instruction
3450 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3453 /// \brief Constructor with insert-at-end-of-block semantics
3455 Value *S, ///< The value to be converted
3456 Type *Ty, ///< The type to convert to
3457 const Twine &NameStr, ///< A name for the new instruction
3458 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3461 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3462 static inline bool classof(const Instruction *I) {
3463 return I->getOpcode() == UIToFP;
3465 static inline bool classof(const Value *V) {
3466 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3470 //===----------------------------------------------------------------------===//
3472 //===----------------------------------------------------------------------===//
3474 /// \brief This class represents a cast from signed integer to floating point.
3475 class SIToFPInst : public CastInst {
3477 /// \brief Clone an identical SIToFPInst
3478 virtual SIToFPInst *clone_impl() const;
3481 /// \brief Constructor with insert-before-instruction semantics
3483 Value *S, ///< The value to be converted
3484 Type *Ty, ///< The type to convert to
3485 const Twine &NameStr = "", ///< A name for the new instruction
3486 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3489 /// \brief Constructor with insert-at-end-of-block semantics
3491 Value *S, ///< The value to be converted
3492 Type *Ty, ///< The type to convert to
3493 const Twine &NameStr, ///< A name for the new instruction
3494 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3497 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3498 static inline bool classof(const Instruction *I) {
3499 return I->getOpcode() == SIToFP;
3501 static inline bool classof(const Value *V) {
3502 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3506 //===----------------------------------------------------------------------===//
3508 //===----------------------------------------------------------------------===//
3510 /// \brief This class represents a cast from floating point to unsigned integer
3511 class FPToUIInst : public CastInst {
3513 /// \brief Clone an identical FPToUIInst
3514 virtual FPToUIInst *clone_impl() const;
3517 /// \brief Constructor with insert-before-instruction semantics
3519 Value *S, ///< The value to be converted
3520 Type *Ty, ///< The type to convert to
3521 const Twine &NameStr = "", ///< A name for the new instruction
3522 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3525 /// \brief Constructor with insert-at-end-of-block semantics
3527 Value *S, ///< The value to be converted
3528 Type *Ty, ///< The type to convert to
3529 const Twine &NameStr, ///< A name for the new instruction
3530 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3533 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3534 static inline bool classof(const Instruction *I) {
3535 return I->getOpcode() == FPToUI;
3537 static inline bool classof(const Value *V) {
3538 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3542 //===----------------------------------------------------------------------===//
3544 //===----------------------------------------------------------------------===//
3546 /// \brief This class represents a cast from floating point to signed integer.
3547 class FPToSIInst : public CastInst {
3549 /// \brief Clone an identical FPToSIInst
3550 virtual FPToSIInst *clone_impl() const;
3553 /// \brief Constructor with insert-before-instruction semantics
3555 Value *S, ///< The value to be converted
3556 Type *Ty, ///< The type to convert to
3557 const Twine &NameStr = "", ///< A name for the new instruction
3558 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3561 /// \brief Constructor with insert-at-end-of-block semantics
3563 Value *S, ///< The value to be converted
3564 Type *Ty, ///< The type to convert to
3565 const Twine &NameStr, ///< A name for the new instruction
3566 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3569 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3570 static inline bool classof(const Instruction *I) {
3571 return I->getOpcode() == FPToSI;
3573 static inline bool classof(const Value *V) {
3574 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3578 //===----------------------------------------------------------------------===//
3579 // IntToPtrInst Class
3580 //===----------------------------------------------------------------------===//
3582 /// \brief This class represents a cast from an integer to a pointer.
3583 class IntToPtrInst : public CastInst {
3585 /// \brief Constructor with insert-before-instruction semantics
3587 Value *S, ///< The value to be converted
3588 Type *Ty, ///< The type to convert to
3589 const Twine &NameStr = "", ///< A name for the new instruction
3590 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3593 /// \brief Constructor with insert-at-end-of-block semantics
3595 Value *S, ///< The value to be converted
3596 Type *Ty, ///< The type to convert to
3597 const Twine &NameStr, ///< A name for the new instruction
3598 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3601 /// \brief Clone an identical IntToPtrInst
3602 virtual IntToPtrInst *clone_impl() const;
3604 /// \brief return the address space of the pointer.
3605 unsigned getAddressSpace() const {
3606 return getType()->getPointerAddressSpace();
3609 // Methods for support type inquiry through isa, cast, and dyn_cast:
3610 static inline bool classof(const Instruction *I) {
3611 return I->getOpcode() == IntToPtr;
3613 static inline bool classof(const Value *V) {
3614 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3618 //===----------------------------------------------------------------------===//
3619 // PtrToIntInst Class
3620 //===----------------------------------------------------------------------===//
3622 /// \brief This class represents a cast from a pointer to an integer
3623 class PtrToIntInst : public CastInst {
3625 /// \brief Clone an identical PtrToIntInst
3626 virtual PtrToIntInst *clone_impl() const;
3629 /// \brief Constructor with insert-before-instruction semantics
3631 Value *S, ///< The value to be converted
3632 Type *Ty, ///< The type to convert to
3633 const Twine &NameStr = "", ///< A name for the new instruction
3634 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3637 /// \brief Constructor with insert-at-end-of-block semantics
3639 Value *S, ///< The value to be converted
3640 Type *Ty, ///< The type to convert to
3641 const Twine &NameStr, ///< A name for the new instruction
3642 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3645 /// \brief Gets the pointer operand.
3646 Value *getPointerOperand() { return getOperand(0); }
3647 /// \brief Gets the pointer operand.
3648 const Value *getPointerOperand() const { return getOperand(0); }
3649 /// \brief Gets the operand index of the pointer operand.
3650 static unsigned getPointerOperandIndex() { return 0U; }
3652 /// \brief Returns the address space of the pointer operand.
3653 unsigned getPointerAddressSpace() const {
3654 return getPointerOperand()->getType()->getPointerAddressSpace();
3657 // Methods for support type inquiry through isa, cast, and dyn_cast:
3658 static inline bool classof(const Instruction *I) {
3659 return I->getOpcode() == PtrToInt;
3661 static inline bool classof(const Value *V) {
3662 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3666 //===----------------------------------------------------------------------===//
3667 // BitCastInst Class
3668 //===----------------------------------------------------------------------===//
3670 /// \brief This class represents a no-op cast from one type to another.
3671 class BitCastInst : public CastInst {
3673 /// \brief Clone an identical BitCastInst
3674 virtual BitCastInst *clone_impl() const;
3677 /// \brief Constructor with insert-before-instruction semantics
3679 Value *S, ///< The value to be casted
3680 Type *Ty, ///< The type to casted to
3681 const Twine &NameStr = "", ///< A name for the new instruction
3682 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3685 /// \brief Constructor with insert-at-end-of-block semantics
3687 Value *S, ///< The value to be casted
3688 Type *Ty, ///< The type to casted to
3689 const Twine &NameStr, ///< A name for the new instruction
3690 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3693 // Methods for support type inquiry through isa, cast, and dyn_cast:
3694 static inline bool classof(const Instruction *I) {
3695 return I->getOpcode() == BitCast;
3697 static inline bool classof(const Value *V) {
3698 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3702 } // End llvm namespace