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 /// \brief Returns the address space of the pointer operand.
229 unsigned getPointerAddressSpace() const {
230 return getPointerOperand()->getType()->getPointerAddressSpace();
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const Instruction *I) {
236 return I->getOpcode() == Instruction::Load;
238 static inline bool classof(const Value *V) {
239 return isa<Instruction>(V) && classof(cast<Instruction>(V));
242 // Shadow Instruction::setInstructionSubclassData with a private forwarding
243 // method so that subclasses cannot accidentally use it.
244 void setInstructionSubclassData(unsigned short D) {
245 Instruction::setInstructionSubclassData(D);
250 //===----------------------------------------------------------------------===//
252 //===----------------------------------------------------------------------===//
254 /// StoreInst - an instruction for storing to memory
256 class StoreInst : public Instruction {
257 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
260 virtual StoreInst *clone_impl() const;
262 // allocate space for exactly two operands
263 void *operator new(size_t s) {
264 return User::operator new(s, 2);
266 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
267 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
268 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
269 Instruction *InsertBefore = 0);
270 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
271 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
272 unsigned Align, Instruction *InsertBefore = 0);
273 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
274 unsigned Align, BasicBlock *InsertAtEnd);
275 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
276 unsigned Align, AtomicOrdering Order,
277 SynchronizationScope SynchScope = CrossThread,
278 Instruction *InsertBefore = 0);
279 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
280 unsigned Align, AtomicOrdering Order,
281 SynchronizationScope SynchScope,
282 BasicBlock *InsertAtEnd);
285 /// isVolatile - Return true if this is a store to a volatile memory
288 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
290 /// setVolatile - Specify whether this is a volatile store or not.
292 void setVolatile(bool V) {
293 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
297 /// Transparently provide more efficient getOperand methods.
298 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
300 /// getAlignment - Return the alignment of the access that is being performed
302 unsigned getAlignment() const {
303 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
306 void setAlignment(unsigned Align);
308 /// Returns the ordering effect of this store.
309 AtomicOrdering getOrdering() const {
310 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
313 /// Set the ordering constraint on this store. May not be Acquire or
315 void setOrdering(AtomicOrdering Ordering) {
316 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
320 SynchronizationScope getSynchScope() const {
321 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
324 /// Specify whether this store instruction is ordered with respect to all
325 /// concurrently executing threads, or only with respect to signal handlers
326 /// executing in the same thread.
327 void setSynchScope(SynchronizationScope xthread) {
328 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
332 bool isAtomic() const { return getOrdering() != NotAtomic; }
333 void setAtomic(AtomicOrdering Ordering,
334 SynchronizationScope SynchScope = CrossThread) {
335 setOrdering(Ordering);
336 setSynchScope(SynchScope);
339 bool isSimple() const { return !isAtomic() && !isVolatile(); }
340 bool isUnordered() const {
341 return getOrdering() <= Unordered && !isVolatile();
344 Value *getValueOperand() { return getOperand(0); }
345 const Value *getValueOperand() const { return getOperand(0); }
347 Value *getPointerOperand() { return getOperand(1); }
348 const Value *getPointerOperand() const { return getOperand(1); }
349 static unsigned getPointerOperandIndex() { return 1U; }
351 /// \brief Returns the address space of the pointer operand.
352 unsigned getPointerAddressSpace() const {
353 return getPointerOperand()->getType()->getPointerAddressSpace();
356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const Instruction *I) {
358 return I->getOpcode() == Instruction::Store;
360 static inline bool classof(const Value *V) {
361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
364 // Shadow Instruction::setInstructionSubclassData with a private forwarding
365 // method so that subclasses cannot accidentally use it.
366 void setInstructionSubclassData(unsigned short D) {
367 Instruction::setInstructionSubclassData(D);
372 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
377 //===----------------------------------------------------------------------===//
379 //===----------------------------------------------------------------------===//
381 /// FenceInst - an instruction for ordering other memory operations
383 class FenceInst : public Instruction {
384 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
385 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
387 virtual FenceInst *clone_impl() const;
389 // allocate space for exactly zero operands
390 void *operator new(size_t s) {
391 return User::operator new(s, 0);
394 // Ordering may only be Acquire, Release, AcquireRelease, or
395 // SequentiallyConsistent.
396 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
397 SynchronizationScope SynchScope = CrossThread,
398 Instruction *InsertBefore = 0);
399 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
400 SynchronizationScope SynchScope,
401 BasicBlock *InsertAtEnd);
403 /// Returns the ordering effect of this fence.
404 AtomicOrdering getOrdering() const {
405 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
408 /// Set the ordering constraint on this fence. May only be Acquire, Release,
409 /// AcquireRelease, or SequentiallyConsistent.
410 void setOrdering(AtomicOrdering Ordering) {
411 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
415 SynchronizationScope getSynchScope() const {
416 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
419 /// Specify whether this fence orders other operations with respect to all
420 /// concurrently executing threads, or only with respect to signal handlers
421 /// executing in the same thread.
422 void setSynchScope(SynchronizationScope xthread) {
423 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
427 // Methods for support type inquiry through isa, cast, and dyn_cast:
428 static inline bool classof(const Instruction *I) {
429 return I->getOpcode() == Instruction::Fence;
431 static inline bool classof(const Value *V) {
432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
435 // Shadow Instruction::setInstructionSubclassData with a private forwarding
436 // method so that subclasses cannot accidentally use it.
437 void setInstructionSubclassData(unsigned short D) {
438 Instruction::setInstructionSubclassData(D);
442 //===----------------------------------------------------------------------===//
443 // AtomicCmpXchgInst Class
444 //===----------------------------------------------------------------------===//
446 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
447 /// specified value is in a memory location, and, if it is, stores a new value
448 /// there. Returns the value that was loaded.
450 class AtomicCmpXchgInst : public Instruction {
451 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
452 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
453 AtomicOrdering Ordering, SynchronizationScope SynchScope);
455 virtual AtomicCmpXchgInst *clone_impl() const;
457 // allocate space for exactly three operands
458 void *operator new(size_t s) {
459 return User::operator new(s, 3);
461 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
462 AtomicOrdering Ordering, SynchronizationScope SynchScope,
463 Instruction *InsertBefore = 0);
464 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
465 AtomicOrdering Ordering, SynchronizationScope SynchScope,
466 BasicBlock *InsertAtEnd);
468 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
471 bool isVolatile() const {
472 return getSubclassDataFromInstruction() & 1;
475 /// setVolatile - Specify whether this is a volatile cmpxchg.
477 void setVolatile(bool V) {
478 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
482 /// Transparently provide more efficient getOperand methods.
483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
485 /// Set the ordering constraint on this cmpxchg.
486 void setOrdering(AtomicOrdering Ordering) {
487 assert(Ordering != NotAtomic &&
488 "CmpXchg instructions can only be atomic.");
489 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
493 /// Specify whether this cmpxchg is atomic and orders other operations with
494 /// respect to all concurrently executing threads, or only with respect to
495 /// signal handlers executing in the same thread.
496 void setSynchScope(SynchronizationScope SynchScope) {
497 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
501 /// Returns the ordering constraint on this cmpxchg.
502 AtomicOrdering getOrdering() const {
503 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
506 /// Returns whether this cmpxchg is atomic between threads or only within a
508 SynchronizationScope getSynchScope() const {
509 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
512 Value *getPointerOperand() { return getOperand(0); }
513 const Value *getPointerOperand() const { return getOperand(0); }
514 static unsigned getPointerOperandIndex() { return 0U; }
516 Value *getCompareOperand() { return getOperand(1); }
517 const Value *getCompareOperand() const { return getOperand(1); }
519 Value *getNewValOperand() { return getOperand(2); }
520 const Value *getNewValOperand() const { return getOperand(2); }
522 /// \brief Returns the address space of the pointer operand.
523 unsigned getPointerAddressSpace() const {
524 return getPointerOperand()->getType()->getPointerAddressSpace();
527 // Methods for support type inquiry through isa, cast, and dyn_cast:
528 static inline bool classof(const Instruction *I) {
529 return I->getOpcode() == Instruction::AtomicCmpXchg;
531 static inline bool classof(const Value *V) {
532 return isa<Instruction>(V) && classof(cast<Instruction>(V));
535 // Shadow Instruction::setInstructionSubclassData with a private forwarding
536 // method so that subclasses cannot accidentally use it.
537 void setInstructionSubclassData(unsigned short D) {
538 Instruction::setInstructionSubclassData(D);
543 struct OperandTraits<AtomicCmpXchgInst> :
544 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
547 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
549 //===----------------------------------------------------------------------===//
550 // AtomicRMWInst Class
551 //===----------------------------------------------------------------------===//
553 /// AtomicRMWInst - an instruction that atomically reads a memory location,
554 /// combines it with another value, and then stores the result back. Returns
557 class AtomicRMWInst : public Instruction {
558 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
560 virtual AtomicRMWInst *clone_impl() const;
562 /// This enumeration lists the possible modifications atomicrmw can make. In
563 /// the descriptions, 'p' is the pointer to the instruction's memory location,
564 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
565 /// instruction. These instructions always return 'old'.
581 /// *p = old >signed v ? old : v
583 /// *p = old <signed v ? old : v
585 /// *p = old >unsigned v ? old : v
587 /// *p = old <unsigned v ? old : v
595 // allocate space for exactly two operands
596 void *operator new(size_t s) {
597 return User::operator new(s, 2);
599 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
600 AtomicOrdering Ordering, SynchronizationScope SynchScope,
601 Instruction *InsertBefore = 0);
602 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
603 AtomicOrdering Ordering, SynchronizationScope SynchScope,
604 BasicBlock *InsertAtEnd);
606 BinOp getOperation() const {
607 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
610 void setOperation(BinOp Operation) {
611 unsigned short SubclassData = getSubclassDataFromInstruction();
612 setInstructionSubclassData((SubclassData & 31) |
616 /// isVolatile - Return true if this is a RMW on a volatile memory location.
618 bool isVolatile() const {
619 return getSubclassDataFromInstruction() & 1;
622 /// setVolatile - Specify whether this is a volatile RMW or not.
624 void setVolatile(bool V) {
625 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
629 /// Transparently provide more efficient getOperand methods.
630 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
632 /// Set the ordering constraint on this RMW.
633 void setOrdering(AtomicOrdering Ordering) {
634 assert(Ordering != NotAtomic &&
635 "atomicrmw instructions can only be atomic.");
636 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
640 /// Specify whether this RMW orders other operations with respect to all
641 /// concurrently executing threads, or only with respect to signal handlers
642 /// executing in the same thread.
643 void setSynchScope(SynchronizationScope SynchScope) {
644 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
648 /// Returns the ordering constraint on this RMW.
649 AtomicOrdering getOrdering() const {
650 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
653 /// Returns whether this RMW is atomic between threads or only within a
655 SynchronizationScope getSynchScope() const {
656 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
659 Value *getPointerOperand() { return getOperand(0); }
660 const Value *getPointerOperand() const { return getOperand(0); }
661 static unsigned getPointerOperandIndex() { return 0U; }
663 Value *getValOperand() { return getOperand(1); }
664 const Value *getValOperand() const { return getOperand(1); }
666 /// \brief Returns the address space of the pointer operand.
667 unsigned getPointerAddressSpace() const {
668 return getPointerOperand()->getType()->getPointerAddressSpace();
671 // Methods for support type inquiry through isa, cast, and dyn_cast:
672 static inline bool classof(const Instruction *I) {
673 return I->getOpcode() == Instruction::AtomicRMW;
675 static inline bool classof(const Value *V) {
676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
679 void Init(BinOp Operation, Value *Ptr, Value *Val,
680 AtomicOrdering Ordering, SynchronizationScope SynchScope);
681 // Shadow Instruction::setInstructionSubclassData with a private forwarding
682 // method so that subclasses cannot accidentally use it.
683 void setInstructionSubclassData(unsigned short D) {
684 Instruction::setInstructionSubclassData(D);
689 struct OperandTraits<AtomicRMWInst>
690 : public FixedNumOperandTraits<AtomicRMWInst,2> {
693 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
695 //===----------------------------------------------------------------------===//
696 // GetElementPtrInst Class
697 //===----------------------------------------------------------------------===//
699 // checkGEPType - Simple wrapper function to give a better assertion failure
700 // message on bad indexes for a gep instruction.
702 inline Type *checkGEPType(Type *Ty) {
703 assert(Ty && "Invalid GetElementPtrInst indices for type!");
707 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
708 /// access elements of arrays and structs
710 class GetElementPtrInst : public Instruction {
711 GetElementPtrInst(const GetElementPtrInst &GEPI);
712 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
714 /// Constructors - Create a getelementptr instruction with a base pointer an
715 /// list of indices. The first ctor can optionally insert before an existing
716 /// instruction, the second appends the new instruction to the specified
718 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
719 unsigned Values, const Twine &NameStr,
720 Instruction *InsertBefore);
721 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
722 unsigned Values, const Twine &NameStr,
723 BasicBlock *InsertAtEnd);
725 virtual GetElementPtrInst *clone_impl() const;
727 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
728 const Twine &NameStr = "",
729 Instruction *InsertBefore = 0) {
730 unsigned Values = 1 + unsigned(IdxList.size());
732 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
734 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
735 const Twine &NameStr,
736 BasicBlock *InsertAtEnd) {
737 unsigned Values = 1 + unsigned(IdxList.size());
739 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
742 /// Create an "inbounds" getelementptr. See the documentation for the
743 /// "inbounds" flag in LangRef.html for details.
744 static GetElementPtrInst *CreateInBounds(Value *Ptr,
745 ArrayRef<Value *> IdxList,
746 const Twine &NameStr = "",
747 Instruction *InsertBefore = 0) {
748 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
749 GEP->setIsInBounds(true);
752 static GetElementPtrInst *CreateInBounds(Value *Ptr,
753 ArrayRef<Value *> IdxList,
754 const Twine &NameStr,
755 BasicBlock *InsertAtEnd) {
756 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
757 GEP->setIsInBounds(true);
761 /// Transparently provide more efficient getOperand methods.
762 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
764 // getType - Overload to return most specific pointer type...
765 PointerType *getType() const {
766 return reinterpret_cast<PointerType*>(Instruction::getType());
769 /// \brief Returns the address space of this instruction's pointer type.
770 unsigned getAddressSpace() const {
771 // Note that this is always the same as the pointer operand's address space
772 // and that is cheaper to compute, so cheat here.
773 return getPointerAddressSpace();
776 /// getIndexedType - Returns the type of the element that would be loaded with
777 /// a load instruction with the specified parameters.
779 /// Null is returned if the indices are invalid for the specified
782 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
783 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
784 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
786 inline op_iterator idx_begin() { return op_begin()+1; }
787 inline const_op_iterator idx_begin() const { return op_begin()+1; }
788 inline op_iterator idx_end() { return op_end(); }
789 inline const_op_iterator idx_end() const { return op_end(); }
791 Value *getPointerOperand() {
792 return getOperand(0);
794 const Value *getPointerOperand() const {
795 return getOperand(0);
797 static unsigned getPointerOperandIndex() {
798 return 0U; // get index for modifying correct operand.
801 /// getPointerOperandType - Method to return the pointer operand as a
803 Type *getPointerOperandType() const {
804 return getPointerOperand()->getType();
807 /// \brief Returns the address space of the pointer operand.
808 unsigned getPointerAddressSpace() const {
809 return getPointerOperandType()->getPointerAddressSpace();
812 /// GetGEPReturnType - Returns the pointer type returned by the GEP
813 /// instruction, which may be a vector of pointers.
814 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
815 Type *PtrTy = PointerType::get(checkGEPType(
816 getIndexedType(Ptr->getType(), IdxList)),
817 Ptr->getType()->getPointerAddressSpace());
819 if (Ptr->getType()->isVectorTy()) {
820 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
821 return VectorType::get(PtrTy, NumElem);
828 unsigned getNumIndices() const { // Note: always non-negative
829 return getNumOperands() - 1;
832 bool hasIndices() const {
833 return getNumOperands() > 1;
836 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
837 /// zeros. If so, the result pointer and the first operand have the same
838 /// value, just potentially different types.
839 bool hasAllZeroIndices() const;
841 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
842 /// constant integers. If so, the result pointer and the first operand have
843 /// a constant offset between them.
844 bool hasAllConstantIndices() const;
846 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
847 /// See LangRef.html for the meaning of inbounds on a getelementptr.
848 void setIsInBounds(bool b = true);
850 /// isInBounds - Determine whether the GEP has the inbounds flag.
851 bool isInBounds() const;
853 // Methods for support type inquiry through isa, cast, and dyn_cast:
854 static inline bool classof(const Instruction *I) {
855 return (I->getOpcode() == Instruction::GetElementPtr);
857 static inline bool classof(const Value *V) {
858 return isa<Instruction>(V) && classof(cast<Instruction>(V));
863 struct OperandTraits<GetElementPtrInst> :
864 public VariadicOperandTraits<GetElementPtrInst, 1> {
867 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
868 ArrayRef<Value *> IdxList,
870 const Twine &NameStr,
871 Instruction *InsertBefore)
872 : Instruction(getGEPReturnType(Ptr, IdxList),
874 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
875 Values, InsertBefore) {
876 init(Ptr, IdxList, NameStr);
878 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
879 ArrayRef<Value *> IdxList,
881 const Twine &NameStr,
882 BasicBlock *InsertAtEnd)
883 : Instruction(getGEPReturnType(Ptr, IdxList),
885 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
886 Values, InsertAtEnd) {
887 init(Ptr, IdxList, NameStr);
891 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
894 //===----------------------------------------------------------------------===//
896 //===----------------------------------------------------------------------===//
898 /// This instruction compares its operands according to the predicate given
899 /// to the constructor. It only operates on integers or pointers. The operands
900 /// must be identical types.
901 /// \brief Represent an integer comparison operator.
902 class ICmpInst: public CmpInst {
904 /// \brief Clone an identical ICmpInst
905 virtual ICmpInst *clone_impl() const;
907 /// \brief Constructor with insert-before-instruction semantics.
909 Instruction *InsertBefore, ///< Where to insert
910 Predicate pred, ///< The predicate to use for the comparison
911 Value *LHS, ///< The left-hand-side of the expression
912 Value *RHS, ///< The right-hand-side of the expression
913 const Twine &NameStr = "" ///< Name of the instruction
914 ) : CmpInst(makeCmpResultType(LHS->getType()),
915 Instruction::ICmp, pred, LHS, RHS, NameStr,
917 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
918 pred <= CmpInst::LAST_ICMP_PREDICATE &&
919 "Invalid ICmp predicate value");
920 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
921 "Both operands to ICmp instruction are not of the same type!");
922 // Check that the operands are the right type
923 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
924 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
925 "Invalid operand types for ICmp instruction");
928 /// \brief Constructor with insert-at-end semantics.
930 BasicBlock &InsertAtEnd, ///< Block to insert into.
931 Predicate pred, ///< The predicate to use for the comparison
932 Value *LHS, ///< The left-hand-side of the expression
933 Value *RHS, ///< The right-hand-side of the expression
934 const Twine &NameStr = "" ///< Name of the instruction
935 ) : CmpInst(makeCmpResultType(LHS->getType()),
936 Instruction::ICmp, pred, LHS, RHS, NameStr,
938 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
939 pred <= CmpInst::LAST_ICMP_PREDICATE &&
940 "Invalid ICmp predicate value");
941 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
942 "Both operands to ICmp instruction are not of the same type!");
943 // Check that the operands are the right type
944 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
945 getOperand(0)->getType()->isPointerTy()) &&
946 "Invalid operand types for ICmp instruction");
949 /// \brief Constructor with no-insertion semantics
951 Predicate pred, ///< The predicate to use for the comparison
952 Value *LHS, ///< The left-hand-side of the expression
953 Value *RHS, ///< The right-hand-side of the expression
954 const Twine &NameStr = "" ///< Name of the instruction
955 ) : CmpInst(makeCmpResultType(LHS->getType()),
956 Instruction::ICmp, pred, LHS, RHS, NameStr) {
957 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
958 pred <= CmpInst::LAST_ICMP_PREDICATE &&
959 "Invalid ICmp predicate value");
960 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
961 "Both operands to ICmp instruction are not of the same type!");
962 // Check that the operands are the right type
963 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
964 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
965 "Invalid operand types for ICmp instruction");
968 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
969 /// @returns the predicate that would be the result if the operand were
970 /// regarded as signed.
971 /// \brief Return the signed version of the predicate
972 Predicate getSignedPredicate() const {
973 return getSignedPredicate(getPredicate());
976 /// This is a static version that you can use without an instruction.
977 /// \brief Return the signed version of the predicate.
978 static Predicate getSignedPredicate(Predicate pred);
980 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
981 /// @returns the predicate that would be the result if the operand were
982 /// regarded as unsigned.
983 /// \brief Return the unsigned version of the predicate
984 Predicate getUnsignedPredicate() const {
985 return getUnsignedPredicate(getPredicate());
988 /// This is a static version that you can use without an instruction.
989 /// \brief Return the unsigned version of the predicate.
990 static Predicate getUnsignedPredicate(Predicate pred);
992 /// isEquality - Return true if this predicate is either EQ or NE. This also
993 /// tests for commutativity.
994 static bool isEquality(Predicate P) {
995 return P == ICMP_EQ || P == ICMP_NE;
998 /// isEquality - Return true if this predicate is either EQ or NE. This also
999 /// tests for commutativity.
1000 bool isEquality() const {
1001 return isEquality(getPredicate());
1004 /// @returns true if the predicate of this ICmpInst is commutative
1005 /// \brief Determine if this relation is commutative.
1006 bool isCommutative() const { return isEquality(); }
1008 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1010 bool isRelational() const {
1011 return !isEquality();
1014 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1016 static bool isRelational(Predicate P) {
1017 return !isEquality(P);
1020 /// Initialize a set of values that all satisfy the predicate with C.
1021 /// \brief Make a ConstantRange for a relation with a constant value.
1022 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1024 /// Exchange the two operands to this instruction in such a way that it does
1025 /// not modify the semantics of the instruction. The predicate value may be
1026 /// changed to retain the same result if the predicate is order dependent
1028 /// \brief Swap operands and adjust predicate.
1029 void swapOperands() {
1030 setPredicate(getSwappedPredicate());
1031 Op<0>().swap(Op<1>());
1034 // Methods for support type inquiry through isa, cast, and dyn_cast:
1035 static inline bool classof(const Instruction *I) {
1036 return I->getOpcode() == Instruction::ICmp;
1038 static inline bool classof(const Value *V) {
1039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1044 //===----------------------------------------------------------------------===//
1046 //===----------------------------------------------------------------------===//
1048 /// This instruction compares its operands according to the predicate given
1049 /// to the constructor. It only operates on floating point values or packed
1050 /// vectors of floating point values. The operands must be identical types.
1051 /// \brief Represents a floating point comparison operator.
1052 class FCmpInst: public CmpInst {
1054 /// \brief Clone an identical FCmpInst
1055 virtual FCmpInst *clone_impl() const;
1057 /// \brief Constructor with insert-before-instruction semantics.
1059 Instruction *InsertBefore, ///< Where to insert
1060 Predicate pred, ///< The predicate to use for the comparison
1061 Value *LHS, ///< The left-hand-side of the expression
1062 Value *RHS, ///< The right-hand-side of the expression
1063 const Twine &NameStr = "" ///< Name of the instruction
1064 ) : CmpInst(makeCmpResultType(LHS->getType()),
1065 Instruction::FCmp, pred, LHS, RHS, NameStr,
1067 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1068 "Invalid FCmp predicate value");
1069 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1070 "Both operands to FCmp instruction are not of the same type!");
1071 // Check that the operands are the right type
1072 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1073 "Invalid operand types for FCmp instruction");
1076 /// \brief Constructor with insert-at-end semantics.
1078 BasicBlock &InsertAtEnd, ///< Block to insert into.
1079 Predicate pred, ///< The predicate to use for the comparison
1080 Value *LHS, ///< The left-hand-side of the expression
1081 Value *RHS, ///< The right-hand-side of the expression
1082 const Twine &NameStr = "" ///< Name of the instruction
1083 ) : CmpInst(makeCmpResultType(LHS->getType()),
1084 Instruction::FCmp, pred, LHS, RHS, NameStr,
1086 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1087 "Invalid FCmp predicate value");
1088 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1089 "Both operands to FCmp instruction are not of the same type!");
1090 // Check that the operands are the right type
1091 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1092 "Invalid operand types for FCmp instruction");
1095 /// \brief Constructor with no-insertion semantics
1097 Predicate pred, ///< The predicate to use for the comparison
1098 Value *LHS, ///< The left-hand-side of the expression
1099 Value *RHS, ///< The right-hand-side of the expression
1100 const Twine &NameStr = "" ///< Name of the instruction
1101 ) : CmpInst(makeCmpResultType(LHS->getType()),
1102 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1103 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1104 "Invalid FCmp predicate value");
1105 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1106 "Both operands to FCmp instruction are not of the same type!");
1107 // Check that the operands are the right type
1108 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1109 "Invalid operand types for FCmp instruction");
1112 /// @returns true if the predicate of this instruction is EQ or NE.
1113 /// \brief Determine if this is an equality predicate.
1114 bool isEquality() const {
1115 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1116 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1119 /// @returns true if the predicate of this instruction is commutative.
1120 /// \brief Determine if this is a commutative predicate.
1121 bool isCommutative() const {
1122 return isEquality() ||
1123 getPredicate() == FCMP_FALSE ||
1124 getPredicate() == FCMP_TRUE ||
1125 getPredicate() == FCMP_ORD ||
1126 getPredicate() == FCMP_UNO;
1129 /// @returns true if the predicate is relational (not EQ or NE).
1130 /// \brief Determine if this a relational predicate.
1131 bool isRelational() const { return !isEquality(); }
1133 /// Exchange the two operands to this instruction in such a way that it does
1134 /// not modify the semantics of the instruction. The predicate value may be
1135 /// changed to retain the same result if the predicate is order dependent
1137 /// \brief Swap operands and adjust predicate.
1138 void swapOperands() {
1139 setPredicate(getSwappedPredicate());
1140 Op<0>().swap(Op<1>());
1143 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1144 static inline bool classof(const Instruction *I) {
1145 return I->getOpcode() == Instruction::FCmp;
1147 static inline bool classof(const Value *V) {
1148 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1152 //===----------------------------------------------------------------------===//
1153 /// CallInst - This class represents a function call, abstracting a target
1154 /// machine's calling convention. This class uses low bit of the SubClassData
1155 /// field to indicate whether or not this is a tail call. The rest of the bits
1156 /// hold the calling convention of the call.
1158 class CallInst : public Instruction {
1159 AttrListPtr AttributeList; ///< parameter attributes for call
1160 CallInst(const CallInst &CI);
1161 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1162 void init(Value *Func, const Twine &NameStr);
1164 /// Construct a CallInst given a range of arguments.
1165 /// \brief Construct a CallInst from a range of arguments
1166 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1167 const Twine &NameStr, Instruction *InsertBefore);
1169 /// Construct a CallInst given a range of arguments.
1170 /// \brief Construct a CallInst from a range of arguments
1171 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1172 const Twine &NameStr, BasicBlock *InsertAtEnd);
1174 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1175 Instruction *InsertBefore);
1176 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1177 BasicBlock *InsertAtEnd);
1178 explicit CallInst(Value *F, const Twine &NameStr,
1179 Instruction *InsertBefore);
1180 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1182 virtual CallInst *clone_impl() const;
1184 static CallInst *Create(Value *Func,
1185 ArrayRef<Value *> Args,
1186 const Twine &NameStr = "",
1187 Instruction *InsertBefore = 0) {
1188 return new(unsigned(Args.size() + 1))
1189 CallInst(Func, Args, NameStr, InsertBefore);
1191 static CallInst *Create(Value *Func,
1192 ArrayRef<Value *> Args,
1193 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1194 return new(unsigned(Args.size() + 1))
1195 CallInst(Func, Args, NameStr, InsertAtEnd);
1197 static CallInst *Create(Value *F, const Twine &NameStr = "",
1198 Instruction *InsertBefore = 0) {
1199 return new(1) CallInst(F, NameStr, InsertBefore);
1201 static CallInst *Create(Value *F, const Twine &NameStr,
1202 BasicBlock *InsertAtEnd) {
1203 return new(1) CallInst(F, NameStr, InsertAtEnd);
1205 /// CreateMalloc - Generate the IR for a call to malloc:
1206 /// 1. Compute the malloc call's argument as the specified type's size,
1207 /// possibly multiplied by the array size if the array size is not
1209 /// 2. Call malloc with that argument.
1210 /// 3. Bitcast the result of the malloc call to the specified type.
1211 static Instruction *CreateMalloc(Instruction *InsertBefore,
1212 Type *IntPtrTy, Type *AllocTy,
1213 Value *AllocSize, Value *ArraySize = 0,
1214 Function* MallocF = 0,
1215 const Twine &Name = "");
1216 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1217 Type *IntPtrTy, Type *AllocTy,
1218 Value *AllocSize, Value *ArraySize = 0,
1219 Function* MallocF = 0,
1220 const Twine &Name = "");
1221 /// CreateFree - Generate the IR for a call to the builtin free function.
1222 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1223 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1227 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1228 void setTailCall(bool isTC = true) {
1229 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1233 /// Provide fast operand accessors
1234 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1236 /// getNumArgOperands - Return the number of call arguments.
1238 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1240 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1242 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1243 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1245 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1247 CallingConv::ID getCallingConv() const {
1248 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1250 void setCallingConv(CallingConv::ID CC) {
1251 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1252 (static_cast<unsigned>(CC) << 1));
1255 /// getAttributes - Return the parameter attributes for this call.
1257 const AttrListPtr &getAttributes() const { return AttributeList; }
1259 /// setAttributes - Set the parameter attributes for this call.
1261 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1263 /// addAttribute - adds the attribute to the list of attributes.
1264 void addAttribute(unsigned i, Attributes attr);
1266 /// removeAttribute - removes the attribute from the list of attributes.
1267 void removeAttribute(unsigned i, Attributes attr);
1269 /// \brief Determine whether this call has the given attribute.
1270 bool hasFnAttr(Attributes::AttrVal A) const;
1272 /// \brief Determine whether the call or the callee has the given attributes.
1273 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
1275 /// \brief Extract the alignment for a call or parameter (0=unknown).
1276 unsigned getParamAlignment(unsigned i) const {
1277 return AttributeList.getParamAlignment(i);
1280 /// \brief Return true if the call should not be inlined.
1281 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
1282 void setIsNoInline() {
1283 addAttribute(AttrListPtr::FunctionIndex,
1284 Attributes::get(getContext(), Attributes::NoInline));
1287 /// \brief Return true if the call can return twice
1288 bool canReturnTwice() const {
1289 return hasFnAttr(Attributes::ReturnsTwice);
1291 void setCanReturnTwice() {
1292 addAttribute(AttrListPtr::FunctionIndex,
1293 Attributes::get(getContext(), Attributes::ReturnsTwice));
1296 /// \brief Determine if the call does not access memory.
1297 bool doesNotAccessMemory() const {
1298 return hasFnAttr(Attributes::ReadNone);
1300 void setDoesNotAccessMemory() {
1301 addAttribute(AttrListPtr::FunctionIndex,
1302 Attributes::get(getContext(), Attributes::ReadNone));
1305 /// \brief Determine if the call does not access or only reads memory.
1306 bool onlyReadsMemory() const {
1307 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
1309 void setOnlyReadsMemory() {
1310 addAttribute(AttrListPtr::FunctionIndex,
1311 Attributes::get(getContext(), Attributes::ReadOnly));
1314 /// \brief Determine if the call cannot return.
1315 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
1316 void setDoesNotReturn() {
1317 addAttribute(AttrListPtr::FunctionIndex,
1318 Attributes::get(getContext(), Attributes::NoReturn));
1321 /// \brief Determine if the call cannot unwind.
1322 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
1323 void setDoesNotThrow() {
1324 addAttribute(AttrListPtr::FunctionIndex,
1325 Attributes::get(getContext(), Attributes::NoUnwind));
1328 /// \brief Determine if the call returns a structure through first
1329 /// pointer argument.
1330 bool hasStructRetAttr() const {
1331 // Be friendly and also check the callee.
1332 return paramHasAttr(1, Attributes::StructRet);
1335 /// \brief Determine if any call argument is an aggregate passed by value.
1336 bool hasByValArgument() const {
1337 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1338 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
1343 /// getCalledFunction - Return the function called, or null if this is an
1344 /// indirect function invocation.
1346 Function *getCalledFunction() const {
1347 return dyn_cast<Function>(Op<-1>());
1350 /// getCalledValue - Get a pointer to the function that is invoked by this
1352 const Value *getCalledValue() const { return Op<-1>(); }
1353 Value *getCalledValue() { return Op<-1>(); }
1355 /// setCalledFunction - Set the function called.
1356 void setCalledFunction(Value* Fn) {
1360 /// isInlineAsm - Check if this call is an inline asm statement.
1361 bool isInlineAsm() const {
1362 return isa<InlineAsm>(Op<-1>());
1365 // Methods for support type inquiry through isa, cast, and dyn_cast:
1366 static inline bool classof(const Instruction *I) {
1367 return I->getOpcode() == Instruction::Call;
1369 static inline bool classof(const Value *V) {
1370 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1373 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1374 // method so that subclasses cannot accidentally use it.
1375 void setInstructionSubclassData(unsigned short D) {
1376 Instruction::setInstructionSubclassData(D);
1381 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1384 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1385 const Twine &NameStr, BasicBlock *InsertAtEnd)
1386 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1387 ->getElementType())->getReturnType(),
1389 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1390 unsigned(Args.size() + 1), InsertAtEnd) {
1391 init(Func, Args, NameStr);
1394 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1395 const Twine &NameStr, Instruction *InsertBefore)
1396 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1397 ->getElementType())->getReturnType(),
1399 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1400 unsigned(Args.size() + 1), InsertBefore) {
1401 init(Func, Args, NameStr);
1405 // Note: if you get compile errors about private methods then
1406 // please update your code to use the high-level operand
1407 // interfaces. See line 943 above.
1408 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1410 //===----------------------------------------------------------------------===//
1412 //===----------------------------------------------------------------------===//
1414 /// SelectInst - This class represents the LLVM 'select' instruction.
1416 class SelectInst : public Instruction {
1417 void init(Value *C, Value *S1, Value *S2) {
1418 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1424 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1425 Instruction *InsertBefore)
1426 : Instruction(S1->getType(), Instruction::Select,
1427 &Op<0>(), 3, InsertBefore) {
1431 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1432 BasicBlock *InsertAtEnd)
1433 : Instruction(S1->getType(), Instruction::Select,
1434 &Op<0>(), 3, InsertAtEnd) {
1439 virtual SelectInst *clone_impl() const;
1441 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1442 const Twine &NameStr = "",
1443 Instruction *InsertBefore = 0) {
1444 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1446 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1447 const Twine &NameStr,
1448 BasicBlock *InsertAtEnd) {
1449 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1452 const Value *getCondition() const { return Op<0>(); }
1453 const Value *getTrueValue() const { return Op<1>(); }
1454 const Value *getFalseValue() const { return Op<2>(); }
1455 Value *getCondition() { return Op<0>(); }
1456 Value *getTrueValue() { return Op<1>(); }
1457 Value *getFalseValue() { return Op<2>(); }
1459 /// areInvalidOperands - Return a string if the specified operands are invalid
1460 /// for a select operation, otherwise return null.
1461 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1463 /// Transparently provide more efficient getOperand methods.
1464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1466 OtherOps getOpcode() const {
1467 return static_cast<OtherOps>(Instruction::getOpcode());
1470 // Methods for support type inquiry through isa, cast, and dyn_cast:
1471 static inline bool classof(const Instruction *I) {
1472 return I->getOpcode() == Instruction::Select;
1474 static inline bool classof(const Value *V) {
1475 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1480 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1483 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1485 //===----------------------------------------------------------------------===//
1487 //===----------------------------------------------------------------------===//
1489 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1490 /// an argument of the specified type given a va_list and increments that list
1492 class VAArgInst : public UnaryInstruction {
1494 virtual VAArgInst *clone_impl() const;
1497 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1498 Instruction *InsertBefore = 0)
1499 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1502 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1503 BasicBlock *InsertAtEnd)
1504 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1508 Value *getPointerOperand() { return getOperand(0); }
1509 const Value *getPointerOperand() const { return getOperand(0); }
1510 static unsigned getPointerOperandIndex() { return 0U; }
1512 // Methods for support type inquiry through isa, cast, and dyn_cast:
1513 static inline bool classof(const Instruction *I) {
1514 return I->getOpcode() == VAArg;
1516 static inline bool classof(const Value *V) {
1517 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1521 //===----------------------------------------------------------------------===//
1522 // ExtractElementInst Class
1523 //===----------------------------------------------------------------------===//
1525 /// ExtractElementInst - This instruction extracts a single (scalar)
1526 /// element from a VectorType value
1528 class ExtractElementInst : public Instruction {
1529 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1530 Instruction *InsertBefore = 0);
1531 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1532 BasicBlock *InsertAtEnd);
1534 virtual ExtractElementInst *clone_impl() const;
1537 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1538 const Twine &NameStr = "",
1539 Instruction *InsertBefore = 0) {
1540 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1542 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1543 const Twine &NameStr,
1544 BasicBlock *InsertAtEnd) {
1545 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1548 /// isValidOperands - Return true if an extractelement instruction can be
1549 /// formed with the specified operands.
1550 static bool isValidOperands(const Value *Vec, const Value *Idx);
1552 Value *getVectorOperand() { return Op<0>(); }
1553 Value *getIndexOperand() { return Op<1>(); }
1554 const Value *getVectorOperand() const { return Op<0>(); }
1555 const Value *getIndexOperand() const { return Op<1>(); }
1557 VectorType *getVectorOperandType() const {
1558 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1562 /// Transparently provide more efficient getOperand methods.
1563 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1565 // Methods for support type inquiry through isa, cast, and dyn_cast:
1566 static inline bool classof(const Instruction *I) {
1567 return I->getOpcode() == Instruction::ExtractElement;
1569 static inline bool classof(const Value *V) {
1570 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1575 struct OperandTraits<ExtractElementInst> :
1576 public FixedNumOperandTraits<ExtractElementInst, 2> {
1579 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1581 //===----------------------------------------------------------------------===//
1582 // InsertElementInst Class
1583 //===----------------------------------------------------------------------===//
1585 /// InsertElementInst - This instruction inserts a single (scalar)
1586 /// element into a VectorType value
1588 class InsertElementInst : public Instruction {
1589 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1590 const Twine &NameStr = "",
1591 Instruction *InsertBefore = 0);
1592 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1593 const Twine &NameStr, BasicBlock *InsertAtEnd);
1595 virtual InsertElementInst *clone_impl() const;
1598 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1599 const Twine &NameStr = "",
1600 Instruction *InsertBefore = 0) {
1601 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1603 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1604 const Twine &NameStr,
1605 BasicBlock *InsertAtEnd) {
1606 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1609 /// isValidOperands - Return true if an insertelement instruction can be
1610 /// formed with the specified operands.
1611 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1614 /// getType - Overload to return most specific vector type.
1616 VectorType *getType() const {
1617 return reinterpret_cast<VectorType*>(Instruction::getType());
1620 /// Transparently provide more efficient getOperand methods.
1621 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1623 // Methods for support type inquiry through isa, cast, and dyn_cast:
1624 static inline bool classof(const Instruction *I) {
1625 return I->getOpcode() == Instruction::InsertElement;
1627 static inline bool classof(const Value *V) {
1628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1633 struct OperandTraits<InsertElementInst> :
1634 public FixedNumOperandTraits<InsertElementInst, 3> {
1637 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1639 //===----------------------------------------------------------------------===//
1640 // ShuffleVectorInst Class
1641 //===----------------------------------------------------------------------===//
1643 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1646 class ShuffleVectorInst : public Instruction {
1648 virtual ShuffleVectorInst *clone_impl() const;
1651 // allocate space for exactly three operands
1652 void *operator new(size_t s) {
1653 return User::operator new(s, 3);
1655 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1656 const Twine &NameStr = "",
1657 Instruction *InsertBefor = 0);
1658 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1659 const Twine &NameStr, BasicBlock *InsertAtEnd);
1661 /// isValidOperands - Return true if a shufflevector instruction can be
1662 /// formed with the specified operands.
1663 static bool isValidOperands(const Value *V1, const Value *V2,
1666 /// getType - Overload to return most specific vector type.
1668 VectorType *getType() const {
1669 return reinterpret_cast<VectorType*>(Instruction::getType());
1672 /// Transparently provide more efficient getOperand methods.
1673 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1675 Constant *getMask() const {
1676 return reinterpret_cast<Constant*>(getOperand(2));
1679 /// getMaskValue - Return the index from the shuffle mask for the specified
1680 /// output result. This is either -1 if the element is undef or a number less
1681 /// than 2*numelements.
1682 static int getMaskValue(Constant *Mask, unsigned i);
1684 int getMaskValue(unsigned i) const {
1685 return getMaskValue(getMask(), i);
1688 /// getShuffleMask - Return the full mask for this instruction, where each
1689 /// element is the element number and undef's are returned as -1.
1690 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1692 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1693 return getShuffleMask(getMask(), Result);
1696 SmallVector<int, 16> getShuffleMask() const {
1697 SmallVector<int, 16> Mask;
1698 getShuffleMask(Mask);
1703 // Methods for support type inquiry through isa, cast, and dyn_cast:
1704 static inline bool classof(const Instruction *I) {
1705 return I->getOpcode() == Instruction::ShuffleVector;
1707 static inline bool classof(const Value *V) {
1708 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1713 struct OperandTraits<ShuffleVectorInst> :
1714 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1717 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1719 //===----------------------------------------------------------------------===//
1720 // ExtractValueInst Class
1721 //===----------------------------------------------------------------------===//
1723 /// ExtractValueInst - This instruction extracts a struct member or array
1724 /// element value from an aggregate value.
1726 class ExtractValueInst : public UnaryInstruction {
1727 SmallVector<unsigned, 4> Indices;
1729 ExtractValueInst(const ExtractValueInst &EVI);
1730 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1732 /// Constructors - Create a extractvalue instruction with a base aggregate
1733 /// value and a list of indices. The first ctor can optionally insert before
1734 /// an existing instruction, the second appends the new instruction to the
1735 /// specified BasicBlock.
1736 inline ExtractValueInst(Value *Agg,
1737 ArrayRef<unsigned> Idxs,
1738 const Twine &NameStr,
1739 Instruction *InsertBefore);
1740 inline ExtractValueInst(Value *Agg,
1741 ArrayRef<unsigned> Idxs,
1742 const Twine &NameStr, BasicBlock *InsertAtEnd);
1744 // allocate space for exactly one operand
1745 void *operator new(size_t s) {
1746 return User::operator new(s, 1);
1749 virtual ExtractValueInst *clone_impl() const;
1752 static ExtractValueInst *Create(Value *Agg,
1753 ArrayRef<unsigned> Idxs,
1754 const Twine &NameStr = "",
1755 Instruction *InsertBefore = 0) {
1757 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1759 static ExtractValueInst *Create(Value *Agg,
1760 ArrayRef<unsigned> Idxs,
1761 const Twine &NameStr,
1762 BasicBlock *InsertAtEnd) {
1763 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1766 /// getIndexedType - Returns the type of the element that would be extracted
1767 /// with an extractvalue instruction with the specified parameters.
1769 /// Null is returned if the indices are invalid for the specified type.
1770 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1772 typedef const unsigned* idx_iterator;
1773 inline idx_iterator idx_begin() const { return Indices.begin(); }
1774 inline idx_iterator idx_end() const { return Indices.end(); }
1776 Value *getAggregateOperand() {
1777 return getOperand(0);
1779 const Value *getAggregateOperand() const {
1780 return getOperand(0);
1782 static unsigned getAggregateOperandIndex() {
1783 return 0U; // get index for modifying correct operand
1786 ArrayRef<unsigned> getIndices() const {
1790 unsigned getNumIndices() const {
1791 return (unsigned)Indices.size();
1794 bool hasIndices() const {
1798 // Methods for support type inquiry through isa, cast, and dyn_cast:
1799 static inline bool classof(const Instruction *I) {
1800 return I->getOpcode() == Instruction::ExtractValue;
1802 static inline bool classof(const Value *V) {
1803 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1807 ExtractValueInst::ExtractValueInst(Value *Agg,
1808 ArrayRef<unsigned> Idxs,
1809 const Twine &NameStr,
1810 Instruction *InsertBefore)
1811 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1812 ExtractValue, Agg, InsertBefore) {
1813 init(Idxs, NameStr);
1815 ExtractValueInst::ExtractValueInst(Value *Agg,
1816 ArrayRef<unsigned> Idxs,
1817 const Twine &NameStr,
1818 BasicBlock *InsertAtEnd)
1819 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1820 ExtractValue, Agg, InsertAtEnd) {
1821 init(Idxs, NameStr);
1825 //===----------------------------------------------------------------------===//
1826 // InsertValueInst Class
1827 //===----------------------------------------------------------------------===//
1829 /// InsertValueInst - This instruction inserts a struct field of array element
1830 /// value into an aggregate value.
1832 class InsertValueInst : public Instruction {
1833 SmallVector<unsigned, 4> Indices;
1835 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1836 InsertValueInst(const InsertValueInst &IVI);
1837 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1838 const Twine &NameStr);
1840 /// Constructors - Create a insertvalue instruction with a base aggregate
1841 /// value, a value to insert, and a list of indices. The first ctor can
1842 /// optionally insert before an existing instruction, the second appends
1843 /// the new instruction to the specified BasicBlock.
1844 inline InsertValueInst(Value *Agg, Value *Val,
1845 ArrayRef<unsigned> Idxs,
1846 const Twine &NameStr,
1847 Instruction *InsertBefore);
1848 inline InsertValueInst(Value *Agg, Value *Val,
1849 ArrayRef<unsigned> Idxs,
1850 const Twine &NameStr, BasicBlock *InsertAtEnd);
1852 /// Constructors - These two constructors are convenience methods because one
1853 /// and two index insertvalue instructions are so common.
1854 InsertValueInst(Value *Agg, Value *Val,
1855 unsigned Idx, const Twine &NameStr = "",
1856 Instruction *InsertBefore = 0);
1857 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1858 const Twine &NameStr, BasicBlock *InsertAtEnd);
1860 virtual InsertValueInst *clone_impl() const;
1862 // allocate space for exactly two operands
1863 void *operator new(size_t s) {
1864 return User::operator new(s, 2);
1867 static InsertValueInst *Create(Value *Agg, Value *Val,
1868 ArrayRef<unsigned> Idxs,
1869 const Twine &NameStr = "",
1870 Instruction *InsertBefore = 0) {
1871 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1873 static InsertValueInst *Create(Value *Agg, Value *Val,
1874 ArrayRef<unsigned> Idxs,
1875 const Twine &NameStr,
1876 BasicBlock *InsertAtEnd) {
1877 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1880 /// Transparently provide more efficient getOperand methods.
1881 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1883 typedef const unsigned* idx_iterator;
1884 inline idx_iterator idx_begin() const { return Indices.begin(); }
1885 inline idx_iterator idx_end() const { return Indices.end(); }
1887 Value *getAggregateOperand() {
1888 return getOperand(0);
1890 const Value *getAggregateOperand() const {
1891 return getOperand(0);
1893 static unsigned getAggregateOperandIndex() {
1894 return 0U; // get index for modifying correct operand
1897 Value *getInsertedValueOperand() {
1898 return getOperand(1);
1900 const Value *getInsertedValueOperand() const {
1901 return getOperand(1);
1903 static unsigned getInsertedValueOperandIndex() {
1904 return 1U; // get index for modifying correct operand
1907 ArrayRef<unsigned> getIndices() const {
1911 unsigned getNumIndices() const {
1912 return (unsigned)Indices.size();
1915 bool hasIndices() const {
1919 // Methods for support type inquiry through isa, cast, and dyn_cast:
1920 static inline bool classof(const Instruction *I) {
1921 return I->getOpcode() == Instruction::InsertValue;
1923 static inline bool classof(const Value *V) {
1924 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1929 struct OperandTraits<InsertValueInst> :
1930 public FixedNumOperandTraits<InsertValueInst, 2> {
1933 InsertValueInst::InsertValueInst(Value *Agg,
1935 ArrayRef<unsigned> Idxs,
1936 const Twine &NameStr,
1937 Instruction *InsertBefore)
1938 : Instruction(Agg->getType(), InsertValue,
1939 OperandTraits<InsertValueInst>::op_begin(this),
1941 init(Agg, Val, Idxs, NameStr);
1943 InsertValueInst::InsertValueInst(Value *Agg,
1945 ArrayRef<unsigned> Idxs,
1946 const Twine &NameStr,
1947 BasicBlock *InsertAtEnd)
1948 : Instruction(Agg->getType(), InsertValue,
1949 OperandTraits<InsertValueInst>::op_begin(this),
1951 init(Agg, Val, Idxs, NameStr);
1954 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1956 //===----------------------------------------------------------------------===//
1958 //===----------------------------------------------------------------------===//
1960 // PHINode - The PHINode class is used to represent the magical mystical PHI
1961 // node, that can not exist in nature, but can be synthesized in a computer
1962 // scientist's overactive imagination.
1964 class PHINode : public Instruction {
1965 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1966 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1967 /// the number actually in use.
1968 unsigned ReservedSpace;
1969 PHINode(const PHINode &PN);
1970 // allocate space for exactly zero operands
1971 void *operator new(size_t s) {
1972 return User::operator new(s, 0);
1974 explicit PHINode(Type *Ty, unsigned NumReservedValues,
1975 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1976 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1977 ReservedSpace(NumReservedValues) {
1979 OperandList = allocHungoffUses(ReservedSpace);
1982 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1983 BasicBlock *InsertAtEnd)
1984 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1985 ReservedSpace(NumReservedValues) {
1987 OperandList = allocHungoffUses(ReservedSpace);
1990 // allocHungoffUses - this is more complicated than the generic
1991 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1992 // values and pointers to the incoming blocks, all in one allocation.
1993 Use *allocHungoffUses(unsigned) const;
1995 virtual PHINode *clone_impl() const;
1997 /// Constructors - NumReservedValues is a hint for the number of incoming
1998 /// edges that this phi node will have (use 0 if you really have no idea).
1999 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2000 const Twine &NameStr = "",
2001 Instruction *InsertBefore = 0) {
2002 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2004 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2005 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2006 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2010 /// Provide fast operand accessors
2011 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2013 // Block iterator interface. This provides access to the list of incoming
2014 // basic blocks, which parallels the list of incoming values.
2016 typedef BasicBlock **block_iterator;
2017 typedef BasicBlock * const *const_block_iterator;
2019 block_iterator block_begin() {
2021 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2022 return reinterpret_cast<block_iterator>(ref + 1);
2025 const_block_iterator block_begin() const {
2026 const Use::UserRef *ref =
2027 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2028 return reinterpret_cast<const_block_iterator>(ref + 1);
2031 block_iterator block_end() {
2032 return block_begin() + getNumOperands();
2035 const_block_iterator block_end() const {
2036 return block_begin() + getNumOperands();
2039 /// getNumIncomingValues - Return the number of incoming edges
2041 unsigned getNumIncomingValues() const { return getNumOperands(); }
2043 /// getIncomingValue - Return incoming value number x
2045 Value *getIncomingValue(unsigned i) const {
2046 return getOperand(i);
2048 void setIncomingValue(unsigned i, Value *V) {
2051 static unsigned getOperandNumForIncomingValue(unsigned i) {
2054 static unsigned getIncomingValueNumForOperand(unsigned i) {
2058 /// getIncomingBlock - Return incoming basic block number @p i.
2060 BasicBlock *getIncomingBlock(unsigned i) const {
2061 return block_begin()[i];
2064 /// getIncomingBlock - Return incoming basic block corresponding
2065 /// to an operand of the PHI.
2067 BasicBlock *getIncomingBlock(const Use &U) const {
2068 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2069 return getIncomingBlock(unsigned(&U - op_begin()));
2072 /// getIncomingBlock - Return incoming basic block corresponding
2073 /// to value use iterator.
2075 template <typename U>
2076 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2077 return getIncomingBlock(I.getUse());
2080 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2081 block_begin()[i] = BB;
2084 /// addIncoming - Add an incoming value to the end of the PHI list
2086 void addIncoming(Value *V, BasicBlock *BB) {
2087 assert(V && "PHI node got a null value!");
2088 assert(BB && "PHI node got a null basic block!");
2089 assert(getType() == V->getType() &&
2090 "All operands to PHI node must be the same type as the PHI node!");
2091 if (NumOperands == ReservedSpace)
2092 growOperands(); // Get more space!
2093 // Initialize some new operands.
2095 setIncomingValue(NumOperands - 1, V);
2096 setIncomingBlock(NumOperands - 1, BB);
2099 /// removeIncomingValue - Remove an incoming value. This is useful if a
2100 /// predecessor basic block is deleted. The value removed is returned.
2102 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2103 /// is true), the PHI node is destroyed and any uses of it are replaced with
2104 /// dummy values. The only time there should be zero incoming values to a PHI
2105 /// node is when the block is dead, so this strategy is sound.
2107 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2109 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2110 int Idx = getBasicBlockIndex(BB);
2111 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2112 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2115 /// getBasicBlockIndex - Return the first index of the specified basic
2116 /// block in the value list for this PHI. Returns -1 if no instance.
2118 int getBasicBlockIndex(const BasicBlock *BB) const {
2119 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2120 if (block_begin()[i] == BB)
2125 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2126 int Idx = getBasicBlockIndex(BB);
2127 assert(Idx >= 0 && "Invalid basic block argument!");
2128 return getIncomingValue(Idx);
2131 /// hasConstantValue - If the specified PHI node always merges together the
2132 /// same value, return the value, otherwise return null.
2133 Value *hasConstantValue() const;
2135 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2136 static inline bool classof(const Instruction *I) {
2137 return I->getOpcode() == Instruction::PHI;
2139 static inline bool classof(const Value *V) {
2140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2143 void growOperands();
2147 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2150 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2152 //===----------------------------------------------------------------------===//
2153 // LandingPadInst Class
2154 //===----------------------------------------------------------------------===//
2156 //===---------------------------------------------------------------------------
2157 /// LandingPadInst - The landingpad instruction holds all of the information
2158 /// necessary to generate correct exception handling. The landingpad instruction
2159 /// cannot be moved from the top of a landing pad block, which itself is
2160 /// accessible only from the 'unwind' edge of an invoke. This uses the
2161 /// SubclassData field in Value to store whether or not the landingpad is a
2164 class LandingPadInst : public Instruction {
2165 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2166 /// the number actually in use.
2167 unsigned ReservedSpace;
2168 LandingPadInst(const LandingPadInst &LP);
2170 enum ClauseType { Catch, Filter };
2172 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2173 // Allocate space for exactly zero operands.
2174 void *operator new(size_t s) {
2175 return User::operator new(s, 0);
2177 void growOperands(unsigned Size);
2178 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2180 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2181 unsigned NumReservedValues, const Twine &NameStr,
2182 Instruction *InsertBefore);
2183 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2184 unsigned NumReservedValues, const Twine &NameStr,
2185 BasicBlock *InsertAtEnd);
2187 virtual LandingPadInst *clone_impl() const;
2189 /// Constructors - NumReservedClauses is a hint for the number of incoming
2190 /// clauses that this landingpad will have (use 0 if you really have no idea).
2191 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2192 unsigned NumReservedClauses,
2193 const Twine &NameStr = "",
2194 Instruction *InsertBefore = 0);
2195 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2196 unsigned NumReservedClauses,
2197 const Twine &NameStr, BasicBlock *InsertAtEnd);
2200 /// Provide fast operand accessors
2201 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2203 /// getPersonalityFn - Get the personality function associated with this
2205 Value *getPersonalityFn() const { return getOperand(0); }
2207 /// isCleanup - Return 'true' if this landingpad instruction is a
2208 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2209 /// doesn't catch the exception.
2210 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2212 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2213 void setCleanup(bool V) {
2214 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2218 /// addClause - Add a catch or filter clause to the landing pad.
2219 void addClause(Value *ClauseVal);
2221 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2222 /// to determine what type of clause this is.
2223 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2225 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2226 bool isCatch(unsigned Idx) const {
2227 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2230 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2231 bool isFilter(unsigned Idx) const {
2232 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2235 /// getNumClauses - Get the number of clauses for this landing pad.
2236 unsigned getNumClauses() const { return getNumOperands() - 1; }
2238 /// reserveClauses - Grow the size of the operand list to accommodate the new
2239 /// number of clauses.
2240 void reserveClauses(unsigned Size) { growOperands(Size); }
2242 // Methods for support type inquiry through isa, cast, and dyn_cast:
2243 static inline bool classof(const Instruction *I) {
2244 return I->getOpcode() == Instruction::LandingPad;
2246 static inline bool classof(const Value *V) {
2247 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2252 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2255 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2257 //===----------------------------------------------------------------------===//
2259 //===----------------------------------------------------------------------===//
2261 //===---------------------------------------------------------------------------
2262 /// ReturnInst - Return a value (possibly void), from a function. Execution
2263 /// does not continue in this function any longer.
2265 class ReturnInst : public TerminatorInst {
2266 ReturnInst(const ReturnInst &RI);
2269 // ReturnInst constructors:
2270 // ReturnInst() - 'ret void' instruction
2271 // ReturnInst( null) - 'ret void' instruction
2272 // ReturnInst(Value* X) - 'ret X' instruction
2273 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2274 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2275 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2276 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2278 // NOTE: If the Value* passed is of type void then the constructor behaves as
2279 // if it was passed NULL.
2280 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2281 Instruction *InsertBefore = 0);
2282 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2283 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2285 virtual ReturnInst *clone_impl() const;
2287 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2288 Instruction *InsertBefore = 0) {
2289 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2291 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2292 BasicBlock *InsertAtEnd) {
2293 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2295 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2296 return new(0) ReturnInst(C, InsertAtEnd);
2298 virtual ~ReturnInst();
2300 /// Provide fast operand accessors
2301 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2303 /// Convenience accessor. Returns null if there is no return value.
2304 Value *getReturnValue() const {
2305 return getNumOperands() != 0 ? getOperand(0) : 0;
2308 unsigned getNumSuccessors() const { return 0; }
2310 // Methods for support type inquiry through isa, cast, and dyn_cast:
2311 static inline bool classof(const Instruction *I) {
2312 return (I->getOpcode() == Instruction::Ret);
2314 static inline bool classof(const Value *V) {
2315 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2318 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2319 virtual unsigned getNumSuccessorsV() const;
2320 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2324 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2327 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2329 //===----------------------------------------------------------------------===//
2331 //===----------------------------------------------------------------------===//
2333 //===---------------------------------------------------------------------------
2334 /// BranchInst - Conditional or Unconditional Branch instruction.
2336 class BranchInst : public TerminatorInst {
2337 /// Ops list - Branches are strange. The operands are ordered:
2338 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2339 /// they don't have to check for cond/uncond branchness. These are mostly
2340 /// accessed relative from op_end().
2341 BranchInst(const BranchInst &BI);
2343 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2344 // BranchInst(BB *B) - 'br B'
2345 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2346 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2347 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2348 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2349 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2350 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2351 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2352 Instruction *InsertBefore = 0);
2353 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2354 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2355 BasicBlock *InsertAtEnd);
2357 virtual BranchInst *clone_impl() const;
2359 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2360 return new(1) BranchInst(IfTrue, InsertBefore);
2362 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2363 Value *Cond, Instruction *InsertBefore = 0) {
2364 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2366 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2367 return new(1) BranchInst(IfTrue, InsertAtEnd);
2369 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2370 Value *Cond, BasicBlock *InsertAtEnd) {
2371 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2374 /// Transparently provide more efficient getOperand methods.
2375 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2377 bool isUnconditional() const { return getNumOperands() == 1; }
2378 bool isConditional() const { return getNumOperands() == 3; }
2380 Value *getCondition() const {
2381 assert(isConditional() && "Cannot get condition of an uncond branch!");
2385 void setCondition(Value *V) {
2386 assert(isConditional() && "Cannot set condition of unconditional branch!");
2390 unsigned getNumSuccessors() const { return 1+isConditional(); }
2392 BasicBlock *getSuccessor(unsigned i) const {
2393 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2394 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2397 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2398 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2399 *(&Op<-1>() - idx) = (Value*)NewSucc;
2402 /// \brief Swap the successors of this branch instruction.
2404 /// Swaps the successors of the branch instruction. This also swaps any
2405 /// branch weight metadata associated with the instruction so that it
2406 /// continues to map correctly to each operand.
2407 void swapSuccessors();
2409 // Methods for support type inquiry through isa, cast, and dyn_cast:
2410 static inline bool classof(const Instruction *I) {
2411 return (I->getOpcode() == Instruction::Br);
2413 static inline bool classof(const Value *V) {
2414 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2417 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2418 virtual unsigned getNumSuccessorsV() const;
2419 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2423 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2426 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2428 //===----------------------------------------------------------------------===//
2430 //===----------------------------------------------------------------------===//
2432 //===---------------------------------------------------------------------------
2433 /// SwitchInst - Multiway switch
2435 class SwitchInst : public TerminatorInst {
2436 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2437 unsigned ReservedSpace;
2439 // Operand[0] = Value to switch on
2440 // Operand[1] = Default basic block destination
2441 // Operand[2n ] = Value to match
2442 // Operand[2n+1] = BasicBlock to go to on match
2444 // Store case values separately from operands list. We needn't User-Use
2445 // concept here, since it is just a case value, it will always constant,
2446 // and case value couldn't reused with another instructions/values.
2448 // It allows us to use custom type for case values that is not inherited
2449 // from Value. Since case value is a complex type that implements
2450 // the subset of integers, we needn't extract sub-constants within
2451 // slow getAggregateElement method.
2452 // For case values we will use std::list to by two reasons:
2453 // 1. It allows to add/remove cases without whole collection reallocation.
2454 // 2. In most of cases we needn't random access.
2455 // Currently case values are also stored in Operands List, but it will moved
2456 // out in future commits.
2457 typedef std::list<IntegersSubset> Subsets;
2458 typedef Subsets::iterator SubsetsIt;
2459 typedef Subsets::const_iterator SubsetsConstIt;
2463 SwitchInst(const SwitchInst &SI);
2464 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2465 void growOperands();
2466 // allocate space for exactly zero operands
2467 void *operator new(size_t s) {
2468 return User::operator new(s, 0);
2470 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2471 /// switch on and a default destination. The number of additional cases can
2472 /// be specified here to make memory allocation more efficient. This
2473 /// constructor can also autoinsert before another instruction.
2474 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2475 Instruction *InsertBefore);
2477 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2478 /// switch on and a default destination. The number of additional cases can
2479 /// be specified here to make memory allocation more efficient. This
2480 /// constructor also autoinserts at the end of the specified BasicBlock.
2481 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2482 BasicBlock *InsertAtEnd);
2484 virtual SwitchInst *clone_impl() const;
2487 // FIXME: Currently there are a lot of unclean template parameters,
2488 // we need to make refactoring in future.
2489 // All these parameters are used to implement both iterator and const_iterator
2490 // without code duplication.
2491 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2492 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2493 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2494 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2495 template <class SwitchInstTy, class ConstantIntTy,
2496 class SubsetsItTy, class BasicBlockTy>
2497 class CaseIteratorT;
2499 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2500 SubsetsConstIt, const BasicBlock> ConstCaseIt;
2504 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2506 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2507 unsigned NumCases, Instruction *InsertBefore = 0) {
2508 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2510 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2511 unsigned NumCases, BasicBlock *InsertAtEnd) {
2512 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2517 /// Provide fast operand accessors
2518 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2520 // Accessor Methods for Switch stmt
2521 Value *getCondition() const { return getOperand(0); }
2522 void setCondition(Value *V) { setOperand(0, V); }
2524 BasicBlock *getDefaultDest() const {
2525 return cast<BasicBlock>(getOperand(1));
2528 void setDefaultDest(BasicBlock *DefaultCase) {
2529 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2532 /// getNumCases - return the number of 'cases' in this switch instruction,
2533 /// except the default case
2534 unsigned getNumCases() const {
2535 return getNumOperands()/2 - 1;
2538 /// Returns a read/write iterator that points to the first
2539 /// case in SwitchInst.
2540 CaseIt case_begin() {
2541 return CaseIt(this, 0, TheSubsets.begin());
2543 /// Returns a read-only iterator that points to the first
2544 /// case in the SwitchInst.
2545 ConstCaseIt case_begin() const {
2546 return ConstCaseIt(this, 0, TheSubsets.begin());
2549 /// Returns a read/write iterator that points one past the last
2550 /// in the SwitchInst.
2552 return CaseIt(this, getNumCases(), TheSubsets.end());
2554 /// Returns a read-only iterator that points one past the last
2555 /// in the SwitchInst.
2556 ConstCaseIt case_end() const {
2557 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2559 /// Returns an iterator that points to the default case.
2560 /// Note: this iterator allows to resolve successor only. Attempt
2561 /// to resolve case value causes an assertion.
2562 /// Also note, that increment and decrement also causes an assertion and
2563 /// makes iterator invalid.
2564 CaseIt case_default() {
2565 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2567 ConstCaseIt case_default() const {
2568 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2571 /// findCaseValue - Search all of the case values for the specified constant.
2572 /// If it is explicitly handled, return the case iterator of it, otherwise
2573 /// return default case iterator to indicate
2574 /// that it is handled by the default handler.
2575 CaseIt findCaseValue(const ConstantInt *C) {
2576 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2577 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2579 return case_default();
2581 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2582 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2583 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2585 return case_default();
2588 /// findCaseDest - Finds the unique case value for a given successor. Returns
2589 /// null if the successor is not found, not unique, or is the default case.
2590 ConstantInt *findCaseDest(BasicBlock *BB) {
2591 if (BB == getDefaultDest()) return NULL;
2593 ConstantInt *CI = NULL;
2594 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2595 if (i.getCaseSuccessor() == BB) {
2596 if (CI) return NULL; // Multiple cases lead to BB.
2597 else CI = i.getCaseValue();
2603 /// addCase - Add an entry to the switch instruction...
2606 /// This action invalidates case_end(). Old case_end() iterator will
2607 /// point to the added case.
2608 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2610 /// addCase - Add an entry to the switch instruction.
2612 /// This action invalidates case_end(). Old case_end() iterator will
2613 /// point to the added case.
2614 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2616 /// removeCase - This method removes the specified case and its successor
2617 /// from the switch instruction. Note that this operation may reorder the
2618 /// remaining cases at index idx and above.
2620 /// This action invalidates iterators for all cases following the one removed,
2621 /// including the case_end() iterator.
2622 void removeCase(CaseIt& i);
2624 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2625 BasicBlock *getSuccessor(unsigned idx) const {
2626 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2627 return cast<BasicBlock>(getOperand(idx*2+1));
2629 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2630 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2631 setOperand(idx*2+1, (Value*)NewSucc);
2634 uint16_t hash() const {
2635 uint32_t NumberOfCases = (uint32_t)getNumCases();
2636 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2637 for (ConstCaseIt i = case_begin(), e = case_end();
2639 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2640 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2645 // Case iterators definition.
2647 template <class SwitchInstTy, class ConstantIntTy,
2648 class SubsetsItTy, class BasicBlockTy>
2649 class CaseIteratorT {
2653 unsigned long Index;
2654 SubsetsItTy SubsetIt;
2656 /// Initializes case iterator for given SwitchInst and for given
2658 friend class SwitchInst;
2659 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2660 SubsetsItTy CaseValueIt) {
2662 Index = SuccessorIndex;
2663 this->SubsetIt = CaseValueIt;
2667 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2668 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2669 SubsetsItTy, BasicBlockTy> Self;
2671 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2674 SubsetIt = SI->TheSubsets.begin();
2675 std::advance(SubsetIt, CaseNum);
2679 /// Initializes case iterator for given SwitchInst and for given
2680 /// TerminatorInst's successor index.
2681 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2682 assert(SuccessorIndex < SI->getNumSuccessors() &&
2683 "Successor index # out of range!");
2684 return SuccessorIndex != 0 ?
2685 Self(SI, SuccessorIndex - 1) :
2686 Self(SI, DefaultPseudoIndex);
2689 /// Resolves case value for current case.
2691 ConstantIntTy *getCaseValue() {
2692 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2693 IntegersSubsetRef CaseRanges = *SubsetIt;
2695 // FIXME: Currently we work with ConstantInt based cases.
2696 // So return CaseValue as ConstantInt.
2697 return CaseRanges.getSingleNumber(0).toConstantInt();
2700 /// Resolves case value for current case.
2701 IntegersSubsetRef getCaseValueEx() {
2702 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2706 /// Resolves successor for current case.
2707 BasicBlockTy *getCaseSuccessor() {
2708 assert((Index < SI->getNumCases() ||
2709 Index == DefaultPseudoIndex) &&
2710 "Index out the number of cases.");
2711 return SI->getSuccessor(getSuccessorIndex());
2714 /// Returns number of current case.
2715 unsigned getCaseIndex() const { return Index; }
2717 /// Returns TerminatorInst's successor index for current case successor.
2718 unsigned getSuccessorIndex() const {
2719 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2720 "Index out the number of cases.");
2721 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2725 // Check index correctness after increment.
2726 // Note: Index == getNumCases() means end().
2727 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2730 SubsetIt = SI->TheSubsets.begin();
2735 Self operator++(int) {
2741 // Check index correctness after decrement.
2742 // Note: Index == getNumCases() means end().
2743 // Also allow "-1" iterator here. That will became valid after ++.
2744 unsigned NumCases = SI->getNumCases();
2745 assert((Index == 0 || Index-1 <= NumCases) &&
2746 "Index out the number of cases.");
2748 if (Index == NumCases) {
2749 SubsetIt = SI->TheSubsets.end();
2758 Self operator--(int) {
2763 bool operator==(const Self& RHS) const {
2764 assert(RHS.SI == SI && "Incompatible operators.");
2765 return RHS.Index == Index;
2767 bool operator!=(const Self& RHS) const {
2768 assert(RHS.SI == SI && "Incompatible operators.");
2769 return RHS.Index != Index;
2773 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2774 SubsetsIt, BasicBlock> {
2775 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2779 friend class SwitchInst;
2780 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2781 ParentTy(SI, CaseNum, SubsetIt) {}
2783 void updateCaseValueOperand(IntegersSubset& V) {
2784 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2789 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2791 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2793 /// Sets the new value for current case.
2795 void setValue(ConstantInt *V) {
2796 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2797 IntegersSubsetToBB Mapping;
2798 // FIXME: Currently we work with ConstantInt based cases.
2799 // So inititalize IntItem container directly from ConstantInt.
2800 Mapping.add(IntItem::fromConstantInt(V));
2801 *SubsetIt = Mapping.getCase();
2802 updateCaseValueOperand(*SubsetIt);
2805 /// Sets the new value for current case.
2806 void setValueEx(IntegersSubset& V) {
2807 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2809 updateCaseValueOperand(*SubsetIt);
2812 /// Sets the new successor for current case.
2813 void setSuccessor(BasicBlock *S) {
2814 SI->setSuccessor(getSuccessorIndex(), S);
2818 // Methods for support type inquiry through isa, cast, and dyn_cast:
2820 static inline bool classof(const Instruction *I) {
2821 return I->getOpcode() == Instruction::Switch;
2823 static inline bool classof(const Value *V) {
2824 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2827 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2828 virtual unsigned getNumSuccessorsV() const;
2829 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2833 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2836 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2839 //===----------------------------------------------------------------------===//
2840 // IndirectBrInst Class
2841 //===----------------------------------------------------------------------===//
2843 //===---------------------------------------------------------------------------
2844 /// IndirectBrInst - Indirect Branch Instruction.
2846 class IndirectBrInst : public TerminatorInst {
2847 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2848 unsigned ReservedSpace;
2849 // Operand[0] = Value to switch on
2850 // Operand[1] = Default basic block destination
2851 // Operand[2n ] = Value to match
2852 // Operand[2n+1] = BasicBlock to go to on match
2853 IndirectBrInst(const IndirectBrInst &IBI);
2854 void init(Value *Address, unsigned NumDests);
2855 void growOperands();
2856 // allocate space for exactly zero operands
2857 void *operator new(size_t s) {
2858 return User::operator new(s, 0);
2860 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2861 /// Address to jump to. The number of expected destinations can be specified
2862 /// here to make memory allocation more efficient. This constructor can also
2863 /// autoinsert before another instruction.
2864 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2866 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2867 /// Address to jump to. The number of expected destinations can be specified
2868 /// here to make memory allocation more efficient. This constructor also
2869 /// autoinserts at the end of the specified BasicBlock.
2870 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2872 virtual IndirectBrInst *clone_impl() const;
2874 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2875 Instruction *InsertBefore = 0) {
2876 return new IndirectBrInst(Address, NumDests, InsertBefore);
2878 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2879 BasicBlock *InsertAtEnd) {
2880 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2884 /// Provide fast operand accessors.
2885 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2887 // Accessor Methods for IndirectBrInst instruction.
2888 Value *getAddress() { return getOperand(0); }
2889 const Value *getAddress() const { return getOperand(0); }
2890 void setAddress(Value *V) { setOperand(0, V); }
2893 /// getNumDestinations - return the number of possible destinations in this
2894 /// indirectbr instruction.
2895 unsigned getNumDestinations() const { return getNumOperands()-1; }
2897 /// getDestination - Return the specified destination.
2898 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2899 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2901 /// addDestination - Add a destination.
2903 void addDestination(BasicBlock *Dest);
2905 /// removeDestination - This method removes the specified successor from the
2906 /// indirectbr instruction.
2907 void removeDestination(unsigned i);
2909 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2910 BasicBlock *getSuccessor(unsigned i) const {
2911 return cast<BasicBlock>(getOperand(i+1));
2913 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2914 setOperand(i+1, (Value*)NewSucc);
2917 // Methods for support type inquiry through isa, cast, and dyn_cast:
2918 static inline bool classof(const Instruction *I) {
2919 return I->getOpcode() == Instruction::IndirectBr;
2921 static inline bool classof(const Value *V) {
2922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2925 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2926 virtual unsigned getNumSuccessorsV() const;
2927 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2931 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2934 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2937 //===----------------------------------------------------------------------===//
2939 //===----------------------------------------------------------------------===//
2941 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2942 /// calling convention of the call.
2944 class InvokeInst : public TerminatorInst {
2945 AttrListPtr AttributeList;
2946 InvokeInst(const InvokeInst &BI);
2947 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2948 ArrayRef<Value *> Args, const Twine &NameStr);
2950 /// Construct an InvokeInst given a range of arguments.
2952 /// \brief Construct an InvokeInst from a range of arguments
2953 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2954 ArrayRef<Value *> Args, unsigned Values,
2955 const Twine &NameStr, Instruction *InsertBefore);
2957 /// Construct an InvokeInst given a range of arguments.
2959 /// \brief Construct an InvokeInst from a range of arguments
2960 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2961 ArrayRef<Value *> Args, unsigned Values,
2962 const Twine &NameStr, BasicBlock *InsertAtEnd);
2964 virtual InvokeInst *clone_impl() const;
2966 static InvokeInst *Create(Value *Func,
2967 BasicBlock *IfNormal, BasicBlock *IfException,
2968 ArrayRef<Value *> Args, const Twine &NameStr = "",
2969 Instruction *InsertBefore = 0) {
2970 unsigned Values = unsigned(Args.size()) + 3;
2971 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2972 Values, NameStr, InsertBefore);
2974 static InvokeInst *Create(Value *Func,
2975 BasicBlock *IfNormal, BasicBlock *IfException,
2976 ArrayRef<Value *> Args, const Twine &NameStr,
2977 BasicBlock *InsertAtEnd) {
2978 unsigned Values = unsigned(Args.size()) + 3;
2979 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2980 Values, NameStr, InsertAtEnd);
2983 /// Provide fast operand accessors
2984 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2986 /// getNumArgOperands - Return the number of invoke arguments.
2988 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2990 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2992 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2993 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2995 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2997 CallingConv::ID getCallingConv() const {
2998 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3000 void setCallingConv(CallingConv::ID CC) {
3001 setInstructionSubclassData(static_cast<unsigned>(CC));
3004 /// getAttributes - Return the parameter attributes for this invoke.
3006 const AttrListPtr &getAttributes() const { return AttributeList; }
3008 /// setAttributes - Set the parameter attributes for this invoke.
3010 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3012 /// addAttribute - adds the attribute to the list of attributes.
3013 void addAttribute(unsigned i, Attributes attr);
3015 /// removeAttribute - removes the attribute from the list of attributes.
3016 void removeAttribute(unsigned i, Attributes attr);
3018 /// \brief Determine whether this call has the NoAlias attribute.
3019 bool hasFnAttr(Attributes::AttrVal A) const;
3021 /// \brief Determine whether the call or the callee has the given attributes.
3022 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
3024 /// \brief Extract the alignment for a call or parameter (0=unknown).
3025 unsigned getParamAlignment(unsigned i) const {
3026 return AttributeList.getParamAlignment(i);
3029 /// \brief Return true if the call should not be inlined.
3030 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
3031 void setIsNoInline() {
3032 addAttribute(AttrListPtr::FunctionIndex,
3033 Attributes::get(getContext(), Attributes::NoInline));
3036 /// \brief Determine if the call does not access memory.
3037 bool doesNotAccessMemory() const {
3038 return hasFnAttr(Attributes::ReadNone);
3040 void setDoesNotAccessMemory() {
3041 addAttribute(AttrListPtr::FunctionIndex,
3042 Attributes::get(getContext(), Attributes::ReadNone));
3045 /// \brief Determine if the call does not access or only reads memory.
3046 bool onlyReadsMemory() const {
3047 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
3049 void setOnlyReadsMemory() {
3050 addAttribute(AttrListPtr::FunctionIndex,
3051 Attributes::get(getContext(), Attributes::ReadOnly));
3054 /// \brief Determine if the call cannot return.
3055 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
3056 void setDoesNotReturn() {
3057 addAttribute(AttrListPtr::FunctionIndex,
3058 Attributes::get(getContext(), Attributes::NoReturn));
3061 /// \brief Determine if the call cannot unwind.
3062 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
3063 void setDoesNotThrow() {
3064 addAttribute(AttrListPtr::FunctionIndex,
3065 Attributes::get(getContext(), Attributes::NoUnwind));
3068 /// \brief Determine if the call returns a structure through first
3069 /// pointer argument.
3070 bool hasStructRetAttr() const {
3071 // Be friendly and also check the callee.
3072 return paramHasAttr(1, Attributes::StructRet);
3075 /// \brief Determine if any call argument is an aggregate passed by value.
3076 bool hasByValArgument() const {
3077 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3078 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
3083 /// getCalledFunction - Return the function called, or null if this is an
3084 /// indirect function invocation.
3086 Function *getCalledFunction() const {
3087 return dyn_cast<Function>(Op<-3>());
3090 /// getCalledValue - Get a pointer to the function that is invoked by this
3092 const Value *getCalledValue() const { return Op<-3>(); }
3093 Value *getCalledValue() { return Op<-3>(); }
3095 /// setCalledFunction - Set the function called.
3096 void setCalledFunction(Value* Fn) {
3100 // get*Dest - Return the destination basic blocks...
3101 BasicBlock *getNormalDest() const {
3102 return cast<BasicBlock>(Op<-2>());
3104 BasicBlock *getUnwindDest() const {
3105 return cast<BasicBlock>(Op<-1>());
3107 void setNormalDest(BasicBlock *B) {
3108 Op<-2>() = reinterpret_cast<Value*>(B);
3110 void setUnwindDest(BasicBlock *B) {
3111 Op<-1>() = reinterpret_cast<Value*>(B);
3114 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3115 /// block (the unwind destination).
3116 LandingPadInst *getLandingPadInst() const;
3118 BasicBlock *getSuccessor(unsigned i) const {
3119 assert(i < 2 && "Successor # out of range for invoke!");
3120 return i == 0 ? getNormalDest() : getUnwindDest();
3123 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3124 assert(idx < 2 && "Successor # out of range for invoke!");
3125 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3128 unsigned getNumSuccessors() const { return 2; }
3130 // Methods for support type inquiry through isa, cast, and dyn_cast:
3131 static inline bool classof(const Instruction *I) {
3132 return (I->getOpcode() == Instruction::Invoke);
3134 static inline bool classof(const Value *V) {
3135 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3139 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3140 virtual unsigned getNumSuccessorsV() const;
3141 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3143 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3144 // method so that subclasses cannot accidentally use it.
3145 void setInstructionSubclassData(unsigned short D) {
3146 Instruction::setInstructionSubclassData(D);
3151 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3154 InvokeInst::InvokeInst(Value *Func,
3155 BasicBlock *IfNormal, BasicBlock *IfException,
3156 ArrayRef<Value *> Args, unsigned Values,
3157 const Twine &NameStr, Instruction *InsertBefore)
3158 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3159 ->getElementType())->getReturnType(),
3160 Instruction::Invoke,
3161 OperandTraits<InvokeInst>::op_end(this) - Values,
3162 Values, InsertBefore) {
3163 init(Func, IfNormal, IfException, Args, NameStr);
3165 InvokeInst::InvokeInst(Value *Func,
3166 BasicBlock *IfNormal, BasicBlock *IfException,
3167 ArrayRef<Value *> Args, unsigned Values,
3168 const Twine &NameStr, BasicBlock *InsertAtEnd)
3169 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3170 ->getElementType())->getReturnType(),
3171 Instruction::Invoke,
3172 OperandTraits<InvokeInst>::op_end(this) - Values,
3173 Values, InsertAtEnd) {
3174 init(Func, IfNormal, IfException, Args, NameStr);
3177 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3179 //===----------------------------------------------------------------------===//
3181 //===----------------------------------------------------------------------===//
3183 //===---------------------------------------------------------------------------
3184 /// ResumeInst - Resume the propagation of an exception.
3186 class ResumeInst : public TerminatorInst {
3187 ResumeInst(const ResumeInst &RI);
3189 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3190 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3192 virtual ResumeInst *clone_impl() const;
3194 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3195 return new(1) ResumeInst(Exn, InsertBefore);
3197 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3198 return new(1) ResumeInst(Exn, InsertAtEnd);
3201 /// Provide fast operand accessors
3202 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3204 /// Convenience accessor.
3205 Value *getValue() const { return Op<0>(); }
3207 unsigned getNumSuccessors() const { return 0; }
3209 // Methods for support type inquiry through isa, cast, and dyn_cast:
3210 static inline bool classof(const Instruction *I) {
3211 return I->getOpcode() == Instruction::Resume;
3213 static inline bool classof(const Value *V) {
3214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3217 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3218 virtual unsigned getNumSuccessorsV() const;
3219 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3223 struct OperandTraits<ResumeInst> :
3224 public FixedNumOperandTraits<ResumeInst, 1> {
3227 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3229 //===----------------------------------------------------------------------===//
3230 // UnreachableInst Class
3231 //===----------------------------------------------------------------------===//
3233 //===---------------------------------------------------------------------------
3234 /// UnreachableInst - This function has undefined behavior. In particular, the
3235 /// presence of this instruction indicates some higher level knowledge that the
3236 /// end of the block cannot be reached.
3238 class UnreachableInst : public TerminatorInst {
3239 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3241 virtual UnreachableInst *clone_impl() const;
3244 // allocate space for exactly zero operands
3245 void *operator new(size_t s) {
3246 return User::operator new(s, 0);
3248 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3249 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3251 unsigned getNumSuccessors() const { return 0; }
3253 // Methods for support type inquiry through isa, cast, and dyn_cast:
3254 static inline bool classof(const Instruction *I) {
3255 return I->getOpcode() == Instruction::Unreachable;
3257 static inline bool classof(const Value *V) {
3258 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3261 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3262 virtual unsigned getNumSuccessorsV() const;
3263 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3266 //===----------------------------------------------------------------------===//
3268 //===----------------------------------------------------------------------===//
3270 /// \brief This class represents a truncation of integer types.
3271 class TruncInst : public CastInst {
3273 /// \brief Clone an identical TruncInst
3274 virtual TruncInst *clone_impl() const;
3277 /// \brief Constructor with insert-before-instruction semantics
3279 Value *S, ///< The value to be truncated
3280 Type *Ty, ///< The (smaller) type to truncate to
3281 const Twine &NameStr = "", ///< A name for the new instruction
3282 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3285 /// \brief Constructor with insert-at-end-of-block semantics
3287 Value *S, ///< The value to be truncated
3288 Type *Ty, ///< The (smaller) type to truncate to
3289 const Twine &NameStr, ///< A name for the new instruction
3290 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3293 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3294 static inline bool classof(const Instruction *I) {
3295 return I->getOpcode() == Trunc;
3297 static inline bool classof(const Value *V) {
3298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3302 //===----------------------------------------------------------------------===//
3304 //===----------------------------------------------------------------------===//
3306 /// \brief This class represents zero extension of integer types.
3307 class ZExtInst : public CastInst {
3309 /// \brief Clone an identical ZExtInst
3310 virtual ZExtInst *clone_impl() const;
3313 /// \brief Constructor with insert-before-instruction semantics
3315 Value *S, ///< The value to be zero extended
3316 Type *Ty, ///< The type to zero extend to
3317 const Twine &NameStr = "", ///< A name for the new instruction
3318 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3321 /// \brief Constructor with insert-at-end semantics.
3323 Value *S, ///< The value to be zero extended
3324 Type *Ty, ///< The type to zero extend to
3325 const Twine &NameStr, ///< A name for the new instruction
3326 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3329 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3330 static inline bool classof(const Instruction *I) {
3331 return I->getOpcode() == ZExt;
3333 static inline bool classof(const Value *V) {
3334 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3338 //===----------------------------------------------------------------------===//
3340 //===----------------------------------------------------------------------===//
3342 /// \brief This class represents a sign extension of integer types.
3343 class SExtInst : public CastInst {
3345 /// \brief Clone an identical SExtInst
3346 virtual SExtInst *clone_impl() const;
3349 /// \brief Constructor with insert-before-instruction semantics
3351 Value *S, ///< The value to be sign extended
3352 Type *Ty, ///< The type to sign extend to
3353 const Twine &NameStr = "", ///< A name for the new instruction
3354 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3357 /// \brief Constructor with insert-at-end-of-block semantics
3359 Value *S, ///< The value to be sign extended
3360 Type *Ty, ///< The type to sign extend to
3361 const Twine &NameStr, ///< A name for the new instruction
3362 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3365 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3366 static inline bool classof(const Instruction *I) {
3367 return I->getOpcode() == SExt;
3369 static inline bool classof(const Value *V) {
3370 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3374 //===----------------------------------------------------------------------===//
3375 // FPTruncInst Class
3376 //===----------------------------------------------------------------------===//
3378 /// \brief This class represents a truncation of floating point types.
3379 class FPTruncInst : public CastInst {
3381 /// \brief Clone an identical FPTruncInst
3382 virtual FPTruncInst *clone_impl() const;
3385 /// \brief Constructor with insert-before-instruction semantics
3387 Value *S, ///< The value to be truncated
3388 Type *Ty, ///< The type to truncate to
3389 const Twine &NameStr = "", ///< A name for the new instruction
3390 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3393 /// \brief Constructor with insert-before-instruction semantics
3395 Value *S, ///< The value to be truncated
3396 Type *Ty, ///< The type to truncate to
3397 const Twine &NameStr, ///< A name for the new instruction
3398 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3401 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3402 static inline bool classof(const Instruction *I) {
3403 return I->getOpcode() == FPTrunc;
3405 static inline bool classof(const Value *V) {
3406 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3410 //===----------------------------------------------------------------------===//
3412 //===----------------------------------------------------------------------===//
3414 /// \brief This class represents an extension of floating point types.
3415 class FPExtInst : public CastInst {
3417 /// \brief Clone an identical FPExtInst
3418 virtual FPExtInst *clone_impl() const;
3421 /// \brief Constructor with insert-before-instruction semantics
3423 Value *S, ///< The value to be extended
3424 Type *Ty, ///< The type to extend to
3425 const Twine &NameStr = "", ///< A name for the new instruction
3426 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3429 /// \brief Constructor with insert-at-end-of-block semantics
3431 Value *S, ///< The value to be extended
3432 Type *Ty, ///< The type to extend to
3433 const Twine &NameStr, ///< A name for the new instruction
3434 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3437 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3438 static inline bool classof(const Instruction *I) {
3439 return I->getOpcode() == FPExt;
3441 static inline bool classof(const Value *V) {
3442 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3446 //===----------------------------------------------------------------------===//
3448 //===----------------------------------------------------------------------===//
3450 /// \brief This class represents a cast unsigned integer to floating point.
3451 class UIToFPInst : public CastInst {
3453 /// \brief Clone an identical UIToFPInst
3454 virtual UIToFPInst *clone_impl() const;
3457 /// \brief Constructor with insert-before-instruction semantics
3459 Value *S, ///< The value to be converted
3460 Type *Ty, ///< The type to convert to
3461 const Twine &NameStr = "", ///< A name for the new instruction
3462 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3465 /// \brief Constructor with insert-at-end-of-block semantics
3467 Value *S, ///< The value to be converted
3468 Type *Ty, ///< The type to convert to
3469 const Twine &NameStr, ///< A name for the new instruction
3470 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3473 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3474 static inline bool classof(const Instruction *I) {
3475 return I->getOpcode() == UIToFP;
3477 static inline bool classof(const Value *V) {
3478 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3482 //===----------------------------------------------------------------------===//
3484 //===----------------------------------------------------------------------===//
3486 /// \brief This class represents a cast from signed integer to floating point.
3487 class SIToFPInst : public CastInst {
3489 /// \brief Clone an identical SIToFPInst
3490 virtual SIToFPInst *clone_impl() const;
3493 /// \brief Constructor with insert-before-instruction semantics
3495 Value *S, ///< The value to be converted
3496 Type *Ty, ///< The type to convert to
3497 const Twine &NameStr = "", ///< A name for the new instruction
3498 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3501 /// \brief Constructor with insert-at-end-of-block semantics
3503 Value *S, ///< The value to be converted
3504 Type *Ty, ///< The type to convert to
3505 const Twine &NameStr, ///< A name for the new instruction
3506 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3509 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3510 static inline bool classof(const Instruction *I) {
3511 return I->getOpcode() == SIToFP;
3513 static inline bool classof(const Value *V) {
3514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3518 //===----------------------------------------------------------------------===//
3520 //===----------------------------------------------------------------------===//
3522 /// \brief This class represents a cast from floating point to unsigned integer
3523 class FPToUIInst : public CastInst {
3525 /// \brief Clone an identical FPToUIInst
3526 virtual FPToUIInst *clone_impl() const;
3529 /// \brief Constructor with insert-before-instruction semantics
3531 Value *S, ///< The value to be converted
3532 Type *Ty, ///< The type to convert to
3533 const Twine &NameStr = "", ///< A name for the new instruction
3534 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3537 /// \brief Constructor with insert-at-end-of-block semantics
3539 Value *S, ///< The value to be converted
3540 Type *Ty, ///< The type to convert to
3541 const Twine &NameStr, ///< A name for the new instruction
3542 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3545 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3546 static inline bool classof(const Instruction *I) {
3547 return I->getOpcode() == FPToUI;
3549 static inline bool classof(const Value *V) {
3550 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3554 //===----------------------------------------------------------------------===//
3556 //===----------------------------------------------------------------------===//
3558 /// \brief This class represents a cast from floating point to signed integer.
3559 class FPToSIInst : public CastInst {
3561 /// \brief Clone an identical FPToSIInst
3562 virtual FPToSIInst *clone_impl() const;
3565 /// \brief Constructor with insert-before-instruction semantics
3567 Value *S, ///< The value to be converted
3568 Type *Ty, ///< The type to convert to
3569 const Twine &NameStr = "", ///< A name for the new instruction
3570 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3573 /// \brief Constructor with insert-at-end-of-block semantics
3575 Value *S, ///< The value to be converted
3576 Type *Ty, ///< The type to convert to
3577 const Twine &NameStr, ///< A name for the new instruction
3578 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3581 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3582 static inline bool classof(const Instruction *I) {
3583 return I->getOpcode() == FPToSI;
3585 static inline bool classof(const Value *V) {
3586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3590 //===----------------------------------------------------------------------===//
3591 // IntToPtrInst Class
3592 //===----------------------------------------------------------------------===//
3594 /// \brief This class represents a cast from an integer to a pointer.
3595 class IntToPtrInst : public CastInst {
3597 /// \brief Constructor with insert-before-instruction semantics
3599 Value *S, ///< The value to be converted
3600 Type *Ty, ///< The type to convert to
3601 const Twine &NameStr = "", ///< A name for the new instruction
3602 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3605 /// \brief Constructor with insert-at-end-of-block semantics
3607 Value *S, ///< The value to be converted
3608 Type *Ty, ///< The type to convert to
3609 const Twine &NameStr, ///< A name for the new instruction
3610 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3613 /// \brief Clone an identical IntToPtrInst
3614 virtual IntToPtrInst *clone_impl() const;
3616 /// \brief Returns the address space of this instruction's pointer type.
3617 unsigned getAddressSpace() const {
3618 return getType()->getPointerAddressSpace();
3621 // Methods for support type inquiry through isa, cast, and dyn_cast:
3622 static inline bool classof(const Instruction *I) {
3623 return I->getOpcode() == IntToPtr;
3625 static inline bool classof(const Value *V) {
3626 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3630 //===----------------------------------------------------------------------===//
3631 // PtrToIntInst Class
3632 //===----------------------------------------------------------------------===//
3634 /// \brief This class represents a cast from a pointer to an integer
3635 class PtrToIntInst : public CastInst {
3637 /// \brief Clone an identical PtrToIntInst
3638 virtual PtrToIntInst *clone_impl() const;
3641 /// \brief Constructor with insert-before-instruction semantics
3643 Value *S, ///< The value to be converted
3644 Type *Ty, ///< The type to convert to
3645 const Twine &NameStr = "", ///< A name for the new instruction
3646 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3649 /// \brief Constructor with insert-at-end-of-block semantics
3651 Value *S, ///< The value to be converted
3652 Type *Ty, ///< The type to convert to
3653 const Twine &NameStr, ///< A name for the new instruction
3654 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3657 /// \brief Gets the pointer operand.
3658 Value *getPointerOperand() { return getOperand(0); }
3659 /// \brief Gets the pointer operand.
3660 const Value *getPointerOperand() const { return getOperand(0); }
3661 /// \brief Gets the operand index of the pointer operand.
3662 static unsigned getPointerOperandIndex() { return 0U; }
3664 /// \brief Returns the address space of the pointer operand.
3665 unsigned getPointerAddressSpace() const {
3666 return getPointerOperand()->getType()->getPointerAddressSpace();
3669 // Methods for support type inquiry through isa, cast, and dyn_cast:
3670 static inline bool classof(const Instruction *I) {
3671 return I->getOpcode() == PtrToInt;
3673 static inline bool classof(const Value *V) {
3674 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3678 //===----------------------------------------------------------------------===//
3679 // BitCastInst Class
3680 //===----------------------------------------------------------------------===//
3682 /// \brief This class represents a no-op cast from one type to another.
3683 class BitCastInst : public CastInst {
3685 /// \brief Clone an identical BitCastInst
3686 virtual BitCastInst *clone_impl() const;
3689 /// \brief Constructor with insert-before-instruction semantics
3691 Value *S, ///< The value to be casted
3692 Type *Ty, ///< The type to casted to
3693 const Twine &NameStr = "", ///< A name for the new instruction
3694 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3697 /// \brief Constructor with insert-at-end-of-block semantics
3699 Value *S, ///< The value to be casted
3700 Type *Ty, ///< The type to casted to
3701 const Twine &NameStr, ///< A name for the new instruction
3702 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3705 // Methods for support type inquiry through isa, cast, and dyn_cast:
3706 static inline bool classof(const Instruction *I) {
3707 return I->getOpcode() == BitCast;
3709 static inline bool classof(const Value *V) {
3710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3714 } // End llvm namespace