1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Support/IntegersSubset.h"
24 #include "llvm/Support/IntegersSubsetMapping.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 /// AllocaInst - an instruction to allocate memory on the stack
59 class AllocaInst : public UnaryInstruction {
61 virtual AllocaInst *clone_impl() const;
63 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
64 const Twine &Name = "", Instruction *InsertBefore = 0);
65 AllocaInst(Type *Ty, Value *ArraySize,
66 const Twine &Name, BasicBlock *InsertAtEnd);
68 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
69 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
71 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
72 const Twine &Name = "", Instruction *InsertBefore = 0);
73 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
74 const Twine &Name, BasicBlock *InsertAtEnd);
76 // Out of line virtual method, so the vtable, etc. has a home.
77 virtual ~AllocaInst();
79 /// isArrayAllocation - Return true if there is an allocation size parameter
80 /// to the allocation instruction that is not 1.
82 bool isArrayAllocation() const;
84 /// getArraySize - Get the number of elements allocated. For a simple
85 /// allocation of a single element, this will return a constant 1 value.
87 const Value *getArraySize() const { return getOperand(0); }
88 Value *getArraySize() { return getOperand(0); }
90 /// getType - Overload to return most specific pointer type
92 PointerType *getType() const {
93 return reinterpret_cast<PointerType*>(Instruction::getType());
96 /// getAllocatedType - Return the type that is being allocated by the
99 Type *getAllocatedType() const;
101 /// getAlignment - Return the alignment of the memory that is being allocated
102 /// by the instruction.
104 unsigned getAlignment() const {
105 return (1u << getSubclassDataFromInstruction()) >> 1;
107 void setAlignment(unsigned Align);
109 /// isStaticAlloca - Return true if this alloca is in the entry block of the
110 /// function and is a constant size. If so, the code generator will fold it
111 /// into the prolog/epilog code, so it is basically free.
112 bool isStaticAlloca() const;
114 // Methods for support type inquiry through isa, cast, and dyn_cast:
115 static inline bool classof(const Instruction *I) {
116 return (I->getOpcode() == Instruction::Alloca);
118 static inline bool classof(const Value *V) {
119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
122 // Shadow Instruction::setInstructionSubclassData with a private forwarding
123 // method so that subclasses cannot accidentally use it.
124 void setInstructionSubclassData(unsigned short D) {
125 Instruction::setInstructionSubclassData(D);
130 //===----------------------------------------------------------------------===//
132 //===----------------------------------------------------------------------===//
134 /// LoadInst - an instruction for reading from memory. This uses the
135 /// SubclassData field in Value to store whether or not the load is volatile.
137 class LoadInst : public UnaryInstruction {
140 virtual LoadInst *clone_impl() const;
142 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
143 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
144 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
145 Instruction *InsertBefore = 0);
146 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
147 BasicBlock *InsertAtEnd);
148 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
149 unsigned Align, Instruction *InsertBefore = 0);
150 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
151 unsigned Align, BasicBlock *InsertAtEnd);
152 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
153 unsigned Align, AtomicOrdering Order,
154 SynchronizationScope SynchScope = CrossThread,
155 Instruction *InsertBefore = 0);
156 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
157 unsigned Align, AtomicOrdering Order,
158 SynchronizationScope SynchScope,
159 BasicBlock *InsertAtEnd);
161 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
162 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
163 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
164 bool isVolatile = false, Instruction *InsertBefore = 0);
165 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
166 BasicBlock *InsertAtEnd);
168 /// isVolatile - Return true if this is a load from a volatile memory
171 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
173 /// setVolatile - Specify whether this is a volatile load or not.
175 void setVolatile(bool V) {
176 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
180 /// getAlignment - Return the alignment of the access that is being performed
182 unsigned getAlignment() const {
183 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
186 void setAlignment(unsigned Align);
188 /// Returns the ordering effect of this fence.
189 AtomicOrdering getOrdering() const {
190 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
193 /// Set the ordering constraint on this load. May not be Release or
195 void setOrdering(AtomicOrdering Ordering) {
196 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
200 SynchronizationScope getSynchScope() const {
201 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
204 /// Specify whether this load is ordered with respect to all
205 /// concurrently executing threads, or only with respect to signal handlers
206 /// executing in the same thread.
207 void setSynchScope(SynchronizationScope xthread) {
208 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
212 bool isAtomic() const { return getOrdering() != NotAtomic; }
213 void setAtomic(AtomicOrdering Ordering,
214 SynchronizationScope SynchScope = CrossThread) {
215 setOrdering(Ordering);
216 setSynchScope(SynchScope);
219 bool isSimple() const { return !isAtomic() && !isVolatile(); }
220 bool isUnordered() const {
221 return getOrdering() <= Unordered && !isVolatile();
224 Value *getPointerOperand() { return getOperand(0); }
225 const Value *getPointerOperand() const { return getOperand(0); }
226 static unsigned getPointerOperandIndex() { return 0U; }
228 unsigned getPointerAddressSpace() const {
229 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
233 // Methods for support type inquiry through isa, cast, and dyn_cast:
234 static inline bool classof(const Instruction *I) {
235 return I->getOpcode() == Instruction::Load;
237 static inline bool classof(const Value *V) {
238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 // Shadow Instruction::setInstructionSubclassData with a private forwarding
242 // method so that subclasses cannot accidentally use it.
243 void setInstructionSubclassData(unsigned short D) {
244 Instruction::setInstructionSubclassData(D);
249 //===----------------------------------------------------------------------===//
251 //===----------------------------------------------------------------------===//
253 /// StoreInst - an instruction for storing to memory
255 class StoreInst : public Instruction {
256 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
259 virtual StoreInst *clone_impl() const;
261 // allocate space for exactly two operands
262 void *operator new(size_t s) {
263 return User::operator new(s, 2);
265 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
266 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
267 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
268 Instruction *InsertBefore = 0);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
270 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
271 unsigned Align, Instruction *InsertBefore = 0);
272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
273 unsigned Align, BasicBlock *InsertAtEnd);
274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
275 unsigned Align, AtomicOrdering Order,
276 SynchronizationScope SynchScope = CrossThread,
277 Instruction *InsertBefore = 0);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
279 unsigned Align, AtomicOrdering Order,
280 SynchronizationScope SynchScope,
281 BasicBlock *InsertAtEnd);
284 /// isVolatile - Return true if this is a store to a volatile memory
287 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
289 /// setVolatile - Specify whether this is a volatile store or not.
291 void setVolatile(bool V) {
292 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
296 /// Transparently provide more efficient getOperand methods.
297 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
299 /// getAlignment - Return the alignment of the access that is being performed
301 unsigned getAlignment() const {
302 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
305 void setAlignment(unsigned Align);
307 /// Returns the ordering effect of this store.
308 AtomicOrdering getOrdering() const {
309 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
312 /// Set the ordering constraint on this store. May not be Acquire or
314 void setOrdering(AtomicOrdering Ordering) {
315 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
319 SynchronizationScope getSynchScope() const {
320 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
323 /// Specify whether this store instruction is ordered with respect to all
324 /// concurrently executing threads, or only with respect to signal handlers
325 /// executing in the same thread.
326 void setSynchScope(SynchronizationScope xthread) {
327 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
331 bool isAtomic() const { return getOrdering() != NotAtomic; }
332 void setAtomic(AtomicOrdering Ordering,
333 SynchronizationScope SynchScope = CrossThread) {
334 setOrdering(Ordering);
335 setSynchScope(SynchScope);
338 bool isSimple() const { return !isAtomic() && !isVolatile(); }
339 bool isUnordered() const {
340 return getOrdering() <= Unordered && !isVolatile();
343 Value *getValueOperand() { return getOperand(0); }
344 const Value *getValueOperand() const { return getOperand(0); }
346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
350 unsigned getPointerAddressSpace() const {
351 if (getPointerOperand()->getType()->isPointerTy())
352 return cast<PointerType>(getPointerOperand()->getType())
354 if (getPointerOperand()->getType()->isVectorTy()
355 && cast<VectorType>(getPointerOperand()->getType())->isPointerTy())
356 return cast<PointerType>(cast<VectorType>(
357 getPointerOperand()->getType())->getElementType())
359 llvm_unreachable("Only a vector of pointers or pointers can be used!");
363 // Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const Instruction *I) {
365 return I->getOpcode() == Instruction::Store;
367 static inline bool classof(const Value *V) {
368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
371 // Shadow Instruction::setInstructionSubclassData with a private forwarding
372 // method so that subclasses cannot accidentally use it.
373 void setInstructionSubclassData(unsigned short D) {
374 Instruction::setInstructionSubclassData(D);
379 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
384 //===----------------------------------------------------------------------===//
386 //===----------------------------------------------------------------------===//
388 /// FenceInst - an instruction for ordering other memory operations
390 class FenceInst : public Instruction {
391 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
392 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
394 virtual FenceInst *clone_impl() const;
396 // allocate space for exactly zero operands
397 void *operator new(size_t s) {
398 return User::operator new(s, 0);
401 // Ordering may only be Acquire, Release, AcquireRelease, or
402 // SequentiallyConsistent.
403 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
404 SynchronizationScope SynchScope = CrossThread,
405 Instruction *InsertBefore = 0);
406 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
407 SynchronizationScope SynchScope,
408 BasicBlock *InsertAtEnd);
410 /// Returns the ordering effect of this fence.
411 AtomicOrdering getOrdering() const {
412 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
415 /// Set the ordering constraint on this fence. May only be Acquire, Release,
416 /// AcquireRelease, or SequentiallyConsistent.
417 void setOrdering(AtomicOrdering Ordering) {
418 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
422 SynchronizationScope getSynchScope() const {
423 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
426 /// Specify whether this fence orders other operations with respect to all
427 /// concurrently executing threads, or only with respect to signal handlers
428 /// executing in the same thread.
429 void setSynchScope(SynchronizationScope xthread) {
430 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
434 // Methods for support type inquiry through isa, cast, and dyn_cast:
435 static inline bool classof(const Instruction *I) {
436 return I->getOpcode() == Instruction::Fence;
438 static inline bool classof(const Value *V) {
439 return isa<Instruction>(V) && classof(cast<Instruction>(V));
442 // Shadow Instruction::setInstructionSubclassData with a private forwarding
443 // method so that subclasses cannot accidentally use it.
444 void setInstructionSubclassData(unsigned short D) {
445 Instruction::setInstructionSubclassData(D);
449 //===----------------------------------------------------------------------===//
450 // AtomicCmpXchgInst Class
451 //===----------------------------------------------------------------------===//
453 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
454 /// specified value is in a memory location, and, if it is, stores a new value
455 /// there. Returns the value that was loaded.
457 class AtomicCmpXchgInst : public Instruction {
458 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
459 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
460 AtomicOrdering Ordering, SynchronizationScope SynchScope);
462 virtual AtomicCmpXchgInst *clone_impl() const;
464 // allocate space for exactly three operands
465 void *operator new(size_t s) {
466 return User::operator new(s, 3);
468 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
469 AtomicOrdering Ordering, SynchronizationScope SynchScope,
470 Instruction *InsertBefore = 0);
471 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
472 AtomicOrdering Ordering, SynchronizationScope SynchScope,
473 BasicBlock *InsertAtEnd);
475 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
478 bool isVolatile() const {
479 return getSubclassDataFromInstruction() & 1;
482 /// setVolatile - Specify whether this is a volatile cmpxchg.
484 void setVolatile(bool V) {
485 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
492 /// Set the ordering constraint on this cmpxchg.
493 void setOrdering(AtomicOrdering Ordering) {
494 assert(Ordering != NotAtomic &&
495 "CmpXchg instructions can only be atomic.");
496 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
500 /// Specify whether this cmpxchg is atomic and orders other operations with
501 /// respect to all concurrently executing threads, or only with respect to
502 /// signal handlers executing in the same thread.
503 void setSynchScope(SynchronizationScope SynchScope) {
504 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
508 /// Returns the ordering constraint on this cmpxchg.
509 AtomicOrdering getOrdering() const {
510 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
513 /// Returns whether this cmpxchg is atomic between threads or only within a
515 SynchronizationScope getSynchScope() const {
516 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
519 Value *getPointerOperand() { return getOperand(0); }
520 const Value *getPointerOperand() const { return getOperand(0); }
521 static unsigned getPointerOperandIndex() { return 0U; }
523 Value *getCompareOperand() { return getOperand(1); }
524 const Value *getCompareOperand() const { return getOperand(1); }
526 Value *getNewValOperand() { return getOperand(2); }
527 const Value *getNewValOperand() const { return getOperand(2); }
529 unsigned getPointerAddressSpace() const {
530 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
533 // Methods for support type inquiry through isa, cast, and dyn_cast:
534 static inline bool classof(const Instruction *I) {
535 return I->getOpcode() == Instruction::AtomicCmpXchg;
537 static inline bool classof(const Value *V) {
538 return isa<Instruction>(V) && classof(cast<Instruction>(V));
541 // Shadow Instruction::setInstructionSubclassData with a private forwarding
542 // method so that subclasses cannot accidentally use it.
543 void setInstructionSubclassData(unsigned short D) {
544 Instruction::setInstructionSubclassData(D);
549 struct OperandTraits<AtomicCmpXchgInst> :
550 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
553 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
555 //===----------------------------------------------------------------------===//
556 // AtomicRMWInst Class
557 //===----------------------------------------------------------------------===//
559 /// AtomicRMWInst - an instruction that atomically reads a memory location,
560 /// combines it with another value, and then stores the result back. Returns
563 class AtomicRMWInst : public Instruction {
564 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
566 virtual AtomicRMWInst *clone_impl() const;
568 /// This enumeration lists the possible modifications atomicrmw can make. In
569 /// the descriptions, 'p' is the pointer to the instruction's memory location,
570 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
571 /// instruction. These instructions always return 'old'.
587 /// *p = old >signed v ? old : v
589 /// *p = old <signed v ? old : v
591 /// *p = old >unsigned v ? old : v
593 /// *p = old <unsigned v ? old : v
601 // allocate space for exactly two operands
602 void *operator new(size_t s) {
603 return User::operator new(s, 2);
605 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
606 AtomicOrdering Ordering, SynchronizationScope SynchScope,
607 Instruction *InsertBefore = 0);
608 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
609 AtomicOrdering Ordering, SynchronizationScope SynchScope,
610 BasicBlock *InsertAtEnd);
612 BinOp getOperation() const {
613 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
616 void setOperation(BinOp Operation) {
617 unsigned short SubclassData = getSubclassDataFromInstruction();
618 setInstructionSubclassData((SubclassData & 31) |
622 /// isVolatile - Return true if this is a RMW on a volatile memory location.
624 bool isVolatile() const {
625 return getSubclassDataFromInstruction() & 1;
628 /// setVolatile - Specify whether this is a volatile RMW or not.
630 void setVolatile(bool V) {
631 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
635 /// Transparently provide more efficient getOperand methods.
636 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
638 /// Set the ordering constraint on this RMW.
639 void setOrdering(AtomicOrdering Ordering) {
640 assert(Ordering != NotAtomic &&
641 "atomicrmw instructions can only be atomic.");
642 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
646 /// Specify whether this RMW orders other operations with respect to all
647 /// concurrently executing threads, or only with respect to signal handlers
648 /// executing in the same thread.
649 void setSynchScope(SynchronizationScope SynchScope) {
650 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
654 /// Returns the ordering constraint on this RMW.
655 AtomicOrdering getOrdering() const {
656 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
659 /// Returns whether this RMW is atomic between threads or only within a
661 SynchronizationScope getSynchScope() const {
662 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
665 Value *getPointerOperand() { return getOperand(0); }
666 const Value *getPointerOperand() const { return getOperand(0); }
667 static unsigned getPointerOperandIndex() { return 0U; }
669 Value *getValOperand() { return getOperand(1); }
670 const Value *getValOperand() const { return getOperand(1); }
672 unsigned getPointerAddressSpace() const {
673 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
676 // Methods for support type inquiry through isa, cast, and dyn_cast:
677 static inline bool classof(const Instruction *I) {
678 return I->getOpcode() == Instruction::AtomicRMW;
680 static inline bool classof(const Value *V) {
681 return isa<Instruction>(V) && classof(cast<Instruction>(V));
684 void Init(BinOp Operation, Value *Ptr, Value *Val,
685 AtomicOrdering Ordering, SynchronizationScope SynchScope);
686 // Shadow Instruction::setInstructionSubclassData with a private forwarding
687 // method so that subclasses cannot accidentally use it.
688 void setInstructionSubclassData(unsigned short D) {
689 Instruction::setInstructionSubclassData(D);
694 struct OperandTraits<AtomicRMWInst>
695 : public FixedNumOperandTraits<AtomicRMWInst,2> {
698 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
700 //===----------------------------------------------------------------------===//
701 // GetElementPtrInst Class
702 //===----------------------------------------------------------------------===//
704 // checkGEPType - Simple wrapper function to give a better assertion failure
705 // message on bad indexes for a gep instruction.
707 inline Type *checkGEPType(Type *Ty) {
708 assert(Ty && "Invalid GetElementPtrInst indices for type!");
712 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
713 /// access elements of arrays and structs
715 class GetElementPtrInst : public Instruction {
716 GetElementPtrInst(const GetElementPtrInst &GEPI);
717 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
719 /// Constructors - Create a getelementptr instruction with a base pointer an
720 /// list of indices. The first ctor can optionally insert before an existing
721 /// instruction, the second appends the new instruction to the specified
723 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
724 unsigned Values, const Twine &NameStr,
725 Instruction *InsertBefore);
726 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
727 unsigned Values, const Twine &NameStr,
728 BasicBlock *InsertAtEnd);
730 virtual GetElementPtrInst *clone_impl() const;
732 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
733 const Twine &NameStr = "",
734 Instruction *InsertBefore = 0) {
735 unsigned Values = 1 + unsigned(IdxList.size());
737 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
739 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
740 const Twine &NameStr,
741 BasicBlock *InsertAtEnd) {
742 unsigned Values = 1 + unsigned(IdxList.size());
744 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
747 /// Create an "inbounds" getelementptr. See the documentation for the
748 /// "inbounds" flag in LangRef.html for details.
749 static GetElementPtrInst *CreateInBounds(Value *Ptr,
750 ArrayRef<Value *> IdxList,
751 const Twine &NameStr = "",
752 Instruction *InsertBefore = 0) {
753 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
754 GEP->setIsInBounds(true);
757 static GetElementPtrInst *CreateInBounds(Value *Ptr,
758 ArrayRef<Value *> IdxList,
759 const Twine &NameStr,
760 BasicBlock *InsertAtEnd) {
761 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
762 GEP->setIsInBounds(true);
766 /// Transparently provide more efficient getOperand methods.
767 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
769 // getType - Overload to return most specific pointer type...
770 PointerType *getType() const {
771 return reinterpret_cast<PointerType*>(Instruction::getType());
774 /// getIndexedType - Returns the type of the element that would be loaded with
775 /// a load instruction with the specified parameters.
777 /// Null is returned if the indices are invalid for the specified
780 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
781 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
782 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
784 /// getAddressSpace - Returns the address space used by the GEP pointer.
786 static unsigned getAddressSpace(Value *Ptr);
788 inline op_iterator idx_begin() { return op_begin()+1; }
789 inline const_op_iterator idx_begin() const { return op_begin()+1; }
790 inline op_iterator idx_end() { return op_end(); }
791 inline const_op_iterator idx_end() const { return op_end(); }
793 Value *getPointerOperand() {
794 return getOperand(0);
796 const Value *getPointerOperand() const {
797 return getOperand(0);
799 static unsigned getPointerOperandIndex() {
800 return 0U; // get index for modifying correct operand.
803 unsigned getPointerAddressSpace() const {
804 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
807 /// getPointerOperandType - Method to return the pointer operand as a
809 Type *getPointerOperandType() const {
810 return getPointerOperand()->getType();
813 /// GetGEPReturnType - Returns the pointer type returned by the GEP
814 /// instruction, which may be a vector of pointers.
815 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
816 Type *PtrTy = PointerType::get(checkGEPType(
817 getIndexedType(Ptr->getType(), IdxList)),
818 getAddressSpace(Ptr));
820 if (Ptr->getType()->isVectorTy()) {
821 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
822 return VectorType::get(PtrTy, NumElem);
829 unsigned getNumIndices() const { // Note: always non-negative
830 return getNumOperands() - 1;
833 bool hasIndices() const {
834 return getNumOperands() > 1;
837 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
838 /// zeros. If so, the result pointer and the first operand have the same
839 /// value, just potentially different types.
840 bool hasAllZeroIndices() const;
842 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
843 /// constant integers. If so, the result pointer and the first operand have
844 /// a constant offset between them.
845 bool hasAllConstantIndices() const;
847 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
848 /// See LangRef.html for the meaning of inbounds on a getelementptr.
849 void setIsInBounds(bool b = true);
851 /// isInBounds - Determine whether the GEP has the inbounds flag.
852 bool isInBounds() const;
854 // Methods for support type inquiry through isa, cast, and dyn_cast:
855 static inline bool classof(const Instruction *I) {
856 return (I->getOpcode() == Instruction::GetElementPtr);
858 static inline bool classof(const Value *V) {
859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
864 struct OperandTraits<GetElementPtrInst> :
865 public VariadicOperandTraits<GetElementPtrInst, 1> {
868 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
869 ArrayRef<Value *> IdxList,
871 const Twine &NameStr,
872 Instruction *InsertBefore)
873 : Instruction(getGEPReturnType(Ptr, IdxList),
875 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
876 Values, InsertBefore) {
877 init(Ptr, IdxList, NameStr);
879 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
880 ArrayRef<Value *> IdxList,
882 const Twine &NameStr,
883 BasicBlock *InsertAtEnd)
884 : Instruction(getGEPReturnType(Ptr, IdxList),
886 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
887 Values, InsertAtEnd) {
888 init(Ptr, IdxList, NameStr);
892 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
895 //===----------------------------------------------------------------------===//
897 //===----------------------------------------------------------------------===//
899 /// This instruction compares its operands according to the predicate given
900 /// to the constructor. It only operates on integers or pointers. The operands
901 /// must be identical types.
902 /// @brief Represent an integer comparison operator.
903 class ICmpInst: public CmpInst {
905 /// @brief Clone an identical ICmpInst
906 virtual ICmpInst *clone_impl() const;
908 /// @brief Constructor with insert-before-instruction semantics.
910 Instruction *InsertBefore, ///< Where to insert
911 Predicate pred, ///< The predicate to use for the comparison
912 Value *LHS, ///< The left-hand-side of the expression
913 Value *RHS, ///< The right-hand-side of the expression
914 const Twine &NameStr = "" ///< Name of the instruction
915 ) : CmpInst(makeCmpResultType(LHS->getType()),
916 Instruction::ICmp, pred, LHS, RHS, NameStr,
918 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
919 pred <= CmpInst::LAST_ICMP_PREDICATE &&
920 "Invalid ICmp predicate value");
921 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
922 "Both operands to ICmp instruction are not of the same type!");
923 // Check that the operands are the right type
924 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
925 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
926 "Invalid operand types for ICmp instruction");
929 /// @brief Constructor with insert-at-end semantics.
931 BasicBlock &InsertAtEnd, ///< Block to insert into.
932 Predicate pred, ///< The predicate to use for the comparison
933 Value *LHS, ///< The left-hand-side of the expression
934 Value *RHS, ///< The right-hand-side of the expression
935 const Twine &NameStr = "" ///< Name of the instruction
936 ) : CmpInst(makeCmpResultType(LHS->getType()),
937 Instruction::ICmp, pred, LHS, RHS, NameStr,
939 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
940 pred <= CmpInst::LAST_ICMP_PREDICATE &&
941 "Invalid ICmp predicate value");
942 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
943 "Both operands to ICmp instruction are not of the same type!");
944 // Check that the operands are the right type
945 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
946 getOperand(0)->getType()->isPointerTy()) &&
947 "Invalid operand types for ICmp instruction");
950 /// @brief Constructor with no-insertion semantics
952 Predicate pred, ///< The predicate to use for the comparison
953 Value *LHS, ///< The left-hand-side of the expression
954 Value *RHS, ///< The right-hand-side of the expression
955 const Twine &NameStr = "" ///< Name of the instruction
956 ) : CmpInst(makeCmpResultType(LHS->getType()),
957 Instruction::ICmp, pred, LHS, RHS, NameStr) {
958 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
959 pred <= CmpInst::LAST_ICMP_PREDICATE &&
960 "Invalid ICmp predicate value");
961 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
962 "Both operands to ICmp instruction are not of the same type!");
963 // Check that the operands are the right type
964 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
965 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
966 "Invalid operand types for ICmp instruction");
969 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
970 /// @returns the predicate that would be the result if the operand were
971 /// regarded as signed.
972 /// @brief Return the signed version of the predicate
973 Predicate getSignedPredicate() const {
974 return getSignedPredicate(getPredicate());
977 /// This is a static version that you can use without an instruction.
978 /// @brief Return the signed version of the predicate.
979 static Predicate getSignedPredicate(Predicate pred);
981 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
982 /// @returns the predicate that would be the result if the operand were
983 /// regarded as unsigned.
984 /// @brief Return the unsigned version of the predicate
985 Predicate getUnsignedPredicate() const {
986 return getUnsignedPredicate(getPredicate());
989 /// This is a static version that you can use without an instruction.
990 /// @brief Return the unsigned version of the predicate.
991 static Predicate getUnsignedPredicate(Predicate pred);
993 /// isEquality - Return true if this predicate is either EQ or NE. This also
994 /// tests for commutativity.
995 static bool isEquality(Predicate P) {
996 return P == ICMP_EQ || P == ICMP_NE;
999 /// isEquality - Return true if this predicate is either EQ or NE. This also
1000 /// tests for commutativity.
1001 bool isEquality() const {
1002 return isEquality(getPredicate());
1005 /// @returns true if the predicate of this ICmpInst is commutative
1006 /// @brief Determine if this relation is commutative.
1007 bool isCommutative() const { return isEquality(); }
1009 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1011 bool isRelational() const {
1012 return !isEquality();
1015 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1017 static bool isRelational(Predicate P) {
1018 return !isEquality(P);
1021 /// Initialize a set of values that all satisfy the predicate with C.
1022 /// @brief Make a ConstantRange for a relation with a constant value.
1023 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1025 /// Exchange the two operands to this instruction in such a way that it does
1026 /// not modify the semantics of the instruction. The predicate value may be
1027 /// changed to retain the same result if the predicate is order dependent
1029 /// @brief Swap operands and adjust predicate.
1030 void swapOperands() {
1031 setPredicate(getSwappedPredicate());
1032 Op<0>().swap(Op<1>());
1035 // Methods for support type inquiry through isa, cast, and dyn_cast:
1036 static inline bool classof(const Instruction *I) {
1037 return I->getOpcode() == Instruction::ICmp;
1039 static inline bool classof(const Value *V) {
1040 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1045 //===----------------------------------------------------------------------===//
1047 //===----------------------------------------------------------------------===//
1049 /// This instruction compares its operands according to the predicate given
1050 /// to the constructor. It only operates on floating point values or packed
1051 /// vectors of floating point values. The operands must be identical types.
1052 /// @brief Represents a floating point comparison operator.
1053 class FCmpInst: public CmpInst {
1055 /// @brief Clone an identical FCmpInst
1056 virtual FCmpInst *clone_impl() const;
1058 /// @brief Constructor with insert-before-instruction semantics.
1060 Instruction *InsertBefore, ///< Where to insert
1061 Predicate pred, ///< The predicate to use for the comparison
1062 Value *LHS, ///< The left-hand-side of the expression
1063 Value *RHS, ///< The right-hand-side of the expression
1064 const Twine &NameStr = "" ///< Name of the instruction
1065 ) : CmpInst(makeCmpResultType(LHS->getType()),
1066 Instruction::FCmp, pred, LHS, RHS, NameStr,
1068 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1069 "Invalid FCmp predicate value");
1070 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1071 "Both operands to FCmp instruction are not of the same type!");
1072 // Check that the operands are the right type
1073 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1074 "Invalid operand types for FCmp instruction");
1077 /// @brief Constructor with insert-at-end semantics.
1079 BasicBlock &InsertAtEnd, ///< Block to insert into.
1080 Predicate pred, ///< The predicate to use for the comparison
1081 Value *LHS, ///< The left-hand-side of the expression
1082 Value *RHS, ///< The right-hand-side of the expression
1083 const Twine &NameStr = "" ///< Name of the instruction
1084 ) : CmpInst(makeCmpResultType(LHS->getType()),
1085 Instruction::FCmp, pred, LHS, RHS, NameStr,
1087 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1088 "Invalid FCmp predicate value");
1089 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1090 "Both operands to FCmp instruction are not of the same type!");
1091 // Check that the operands are the right type
1092 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1093 "Invalid operand types for FCmp instruction");
1096 /// @brief Constructor with no-insertion semantics
1098 Predicate pred, ///< The predicate to use for the comparison
1099 Value *LHS, ///< The left-hand-side of the expression
1100 Value *RHS, ///< The right-hand-side of the expression
1101 const Twine &NameStr = "" ///< Name of the instruction
1102 ) : CmpInst(makeCmpResultType(LHS->getType()),
1103 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1104 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1105 "Invalid FCmp predicate value");
1106 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1107 "Both operands to FCmp instruction are not of the same type!");
1108 // Check that the operands are the right type
1109 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1110 "Invalid operand types for FCmp instruction");
1113 /// @returns true if the predicate of this instruction is EQ or NE.
1114 /// @brief Determine if this is an equality predicate.
1115 bool isEquality() const {
1116 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1117 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1120 /// @returns true if the predicate of this instruction is commutative.
1121 /// @brief Determine if this is a commutative predicate.
1122 bool isCommutative() const {
1123 return isEquality() ||
1124 getPredicate() == FCMP_FALSE ||
1125 getPredicate() == FCMP_TRUE ||
1126 getPredicate() == FCMP_ORD ||
1127 getPredicate() == FCMP_UNO;
1130 /// @returns true if the predicate is relational (not EQ or NE).
1131 /// @brief Determine if this a relational predicate.
1132 bool isRelational() const { return !isEquality(); }
1134 /// Exchange the two operands to this instruction in such a way that it does
1135 /// not modify the semantics of the instruction. The predicate value may be
1136 /// changed to retain the same result if the predicate is order dependent
1138 /// @brief Swap operands and adjust predicate.
1139 void swapOperands() {
1140 setPredicate(getSwappedPredicate());
1141 Op<0>().swap(Op<1>());
1144 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1145 static inline bool classof(const Instruction *I) {
1146 return I->getOpcode() == Instruction::FCmp;
1148 static inline bool classof(const Value *V) {
1149 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1153 //===----------------------------------------------------------------------===//
1154 /// CallInst - This class represents a function call, abstracting a target
1155 /// machine's calling convention. This class uses low bit of the SubClassData
1156 /// field to indicate whether or not this is a tail call. The rest of the bits
1157 /// hold the calling convention of the call.
1159 class CallInst : public Instruction {
1160 AttrListPtr AttributeList; ///< parameter attributes for call
1161 CallInst(const CallInst &CI);
1162 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1163 void init(Value *Func, const Twine &NameStr);
1165 /// Construct a CallInst given a range of arguments.
1166 /// @brief Construct a CallInst from a range of arguments
1167 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1168 const Twine &NameStr, Instruction *InsertBefore);
1170 /// Construct a CallInst given a range of arguments.
1171 /// @brief Construct a CallInst from a range of arguments
1172 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1173 const Twine &NameStr, BasicBlock *InsertAtEnd);
1175 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1176 Instruction *InsertBefore);
1177 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1178 BasicBlock *InsertAtEnd);
1179 explicit CallInst(Value *F, const Twine &NameStr,
1180 Instruction *InsertBefore);
1181 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1183 virtual CallInst *clone_impl() const;
1185 static CallInst *Create(Value *Func,
1186 ArrayRef<Value *> Args,
1187 const Twine &NameStr = "",
1188 Instruction *InsertBefore = 0) {
1189 return new(unsigned(Args.size() + 1))
1190 CallInst(Func, Args, NameStr, InsertBefore);
1192 static CallInst *Create(Value *Func,
1193 ArrayRef<Value *> Args,
1194 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1195 return new(unsigned(Args.size() + 1))
1196 CallInst(Func, Args, NameStr, InsertAtEnd);
1198 static CallInst *Create(Value *F, const Twine &NameStr = "",
1199 Instruction *InsertBefore = 0) {
1200 return new(1) CallInst(F, NameStr, InsertBefore);
1202 static CallInst *Create(Value *F, const Twine &NameStr,
1203 BasicBlock *InsertAtEnd) {
1204 return new(1) CallInst(F, NameStr, InsertAtEnd);
1206 /// CreateMalloc - Generate the IR for a call to malloc:
1207 /// 1. Compute the malloc call's argument as the specified type's size,
1208 /// possibly multiplied by the array size if the array size is not
1210 /// 2. Call malloc with that argument.
1211 /// 3. Bitcast the result of the malloc call to the specified type.
1212 static Instruction *CreateMalloc(Instruction *InsertBefore,
1213 Type *IntPtrTy, Type *AllocTy,
1214 Value *AllocSize, Value *ArraySize = 0,
1215 Function* MallocF = 0,
1216 const Twine &Name = "");
1217 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1218 Type *IntPtrTy, Type *AllocTy,
1219 Value *AllocSize, Value *ArraySize = 0,
1220 Function* MallocF = 0,
1221 const Twine &Name = "");
1222 /// CreateFree - Generate the IR for a call to the builtin free function.
1223 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1224 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1228 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1229 void setTailCall(bool isTC = true) {
1230 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1234 /// Provide fast operand accessors
1235 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1237 /// getNumArgOperands - Return the number of call arguments.
1239 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1241 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1243 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1244 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1246 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1248 CallingConv::ID getCallingConv() const {
1249 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1251 void setCallingConv(CallingConv::ID CC) {
1252 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1253 (static_cast<unsigned>(CC) << 1));
1256 /// getAttributes - Return the parameter attributes for this call.
1258 const AttrListPtr &getAttributes() const { return AttributeList; }
1260 /// setAttributes - Set the parameter attributes for this call.
1262 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1264 /// addAttribute - adds the attribute to the list of attributes.
1265 void addAttribute(unsigned i, Attributes attr);
1267 /// removeAttribute - removes the attribute from the list of attributes.
1268 void removeAttribute(unsigned i, Attributes attr);
1270 /// @brief Determine whether this call has the given attribute.
1271 bool hasFnAttr(Attributes::AttrVal A) const;
1273 /// @brief Determine whether the call or the callee has the given attributes.
1274 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
1276 /// @brief Extract the alignment for a call or parameter (0=unknown).
1277 unsigned getParamAlignment(unsigned i) const {
1278 return AttributeList.getParamAlignment(i);
1281 /// @brief Return true if the call should not be inlined.
1282 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
1283 void setIsNoInline() {
1284 addAttribute(AttrListPtr::FunctionIndex,
1285 Attributes::get(getContext(), Attributes::NoInline));
1288 /// @brief Return true if the call can return twice
1289 bool canReturnTwice() const {
1290 return hasFnAttr(Attributes::ReturnsTwice);
1292 void setCanReturnTwice() {
1293 addAttribute(AttrListPtr::FunctionIndex,
1294 Attributes::get(getContext(), Attributes::ReturnsTwice));
1297 /// @brief Determine if the call does not access memory.
1298 bool doesNotAccessMemory() const {
1299 return hasFnAttr(Attributes::ReadNone);
1301 void setDoesNotAccessMemory() {
1302 addAttribute(AttrListPtr::FunctionIndex,
1303 Attributes::get(getContext(), Attributes::ReadNone));
1306 /// @brief Determine if the call does not access or only reads memory.
1307 bool onlyReadsMemory() const {
1308 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
1310 void setOnlyReadsMemory() {
1311 addAttribute(AttrListPtr::FunctionIndex,
1312 Attributes::get(getContext(), Attributes::ReadOnly));
1315 /// @brief Determine if the call cannot return.
1316 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
1317 void setDoesNotReturn() {
1318 addAttribute(AttrListPtr::FunctionIndex,
1319 Attributes::get(getContext(), Attributes::NoReturn));
1322 /// @brief Determine if the call cannot unwind.
1323 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
1324 void setDoesNotThrow() {
1325 addAttribute(AttrListPtr::FunctionIndex,
1326 Attributes::get(getContext(), Attributes::NoUnwind));
1329 /// @brief Determine if the call returns a structure through first
1330 /// pointer argument.
1331 bool hasStructRetAttr() const {
1332 // Be friendly and also check the callee.
1333 return paramHasAttr(1, Attributes::StructRet);
1336 /// @brief Determine if any call argument is an aggregate passed by value.
1337 bool hasByValArgument() const {
1338 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1339 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
1344 /// getCalledFunction - Return the function called, or null if this is an
1345 /// indirect function invocation.
1347 Function *getCalledFunction() const {
1348 return dyn_cast<Function>(Op<-1>());
1351 /// getCalledValue - Get a pointer to the function that is invoked by this
1353 const Value *getCalledValue() const { return Op<-1>(); }
1354 Value *getCalledValue() { return Op<-1>(); }
1356 /// setCalledFunction - Set the function called.
1357 void setCalledFunction(Value* Fn) {
1361 /// isInlineAsm - Check if this call is an inline asm statement.
1362 bool isInlineAsm() const {
1363 return isa<InlineAsm>(Op<-1>());
1366 // Methods for support type inquiry through isa, cast, and dyn_cast:
1367 static inline bool classof(const Instruction *I) {
1368 return I->getOpcode() == Instruction::Call;
1370 static inline bool classof(const Value *V) {
1371 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1374 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1375 // method so that subclasses cannot accidentally use it.
1376 void setInstructionSubclassData(unsigned short D) {
1377 Instruction::setInstructionSubclassData(D);
1382 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1385 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1386 const Twine &NameStr, BasicBlock *InsertAtEnd)
1387 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1388 ->getElementType())->getReturnType(),
1390 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1391 unsigned(Args.size() + 1), InsertAtEnd) {
1392 init(Func, Args, NameStr);
1395 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1396 const Twine &NameStr, Instruction *InsertBefore)
1397 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1398 ->getElementType())->getReturnType(),
1400 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1401 unsigned(Args.size() + 1), InsertBefore) {
1402 init(Func, Args, NameStr);
1406 // Note: if you get compile errors about private methods then
1407 // please update your code to use the high-level operand
1408 // interfaces. See line 943 above.
1409 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1411 //===----------------------------------------------------------------------===//
1413 //===----------------------------------------------------------------------===//
1415 /// SelectInst - This class represents the LLVM 'select' instruction.
1417 class SelectInst : public Instruction {
1418 void init(Value *C, Value *S1, Value *S2) {
1419 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1425 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1426 Instruction *InsertBefore)
1427 : Instruction(S1->getType(), Instruction::Select,
1428 &Op<0>(), 3, InsertBefore) {
1432 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1433 BasicBlock *InsertAtEnd)
1434 : Instruction(S1->getType(), Instruction::Select,
1435 &Op<0>(), 3, InsertAtEnd) {
1440 virtual SelectInst *clone_impl() const;
1442 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1443 const Twine &NameStr = "",
1444 Instruction *InsertBefore = 0) {
1445 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1447 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1448 const Twine &NameStr,
1449 BasicBlock *InsertAtEnd) {
1450 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1453 const Value *getCondition() const { return Op<0>(); }
1454 const Value *getTrueValue() const { return Op<1>(); }
1455 const Value *getFalseValue() const { return Op<2>(); }
1456 Value *getCondition() { return Op<0>(); }
1457 Value *getTrueValue() { return Op<1>(); }
1458 Value *getFalseValue() { return Op<2>(); }
1460 /// areInvalidOperands - Return a string if the specified operands are invalid
1461 /// for a select operation, otherwise return null.
1462 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1464 /// Transparently provide more efficient getOperand methods.
1465 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1467 OtherOps getOpcode() const {
1468 return static_cast<OtherOps>(Instruction::getOpcode());
1471 // Methods for support type inquiry through isa, cast, and dyn_cast:
1472 static inline bool classof(const Instruction *I) {
1473 return I->getOpcode() == Instruction::Select;
1475 static inline bool classof(const Value *V) {
1476 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1481 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1484 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1486 //===----------------------------------------------------------------------===//
1488 //===----------------------------------------------------------------------===//
1490 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1491 /// an argument of the specified type given a va_list and increments that list
1493 class VAArgInst : public UnaryInstruction {
1495 virtual VAArgInst *clone_impl() const;
1498 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1499 Instruction *InsertBefore = 0)
1500 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1503 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1504 BasicBlock *InsertAtEnd)
1505 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1509 Value *getPointerOperand() { return getOperand(0); }
1510 const Value *getPointerOperand() const { return getOperand(0); }
1511 static unsigned getPointerOperandIndex() { return 0U; }
1513 // Methods for support type inquiry through isa, cast, and dyn_cast:
1514 static inline bool classof(const Instruction *I) {
1515 return I->getOpcode() == VAArg;
1517 static inline bool classof(const Value *V) {
1518 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1522 //===----------------------------------------------------------------------===//
1523 // ExtractElementInst Class
1524 //===----------------------------------------------------------------------===//
1526 /// ExtractElementInst - This instruction extracts a single (scalar)
1527 /// element from a VectorType value
1529 class ExtractElementInst : public Instruction {
1530 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1531 Instruction *InsertBefore = 0);
1532 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1533 BasicBlock *InsertAtEnd);
1535 virtual ExtractElementInst *clone_impl() const;
1538 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1539 const Twine &NameStr = "",
1540 Instruction *InsertBefore = 0) {
1541 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1543 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1544 const Twine &NameStr,
1545 BasicBlock *InsertAtEnd) {
1546 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1549 /// isValidOperands - Return true if an extractelement instruction can be
1550 /// formed with the specified operands.
1551 static bool isValidOperands(const Value *Vec, const Value *Idx);
1553 Value *getVectorOperand() { return Op<0>(); }
1554 Value *getIndexOperand() { return Op<1>(); }
1555 const Value *getVectorOperand() const { return Op<0>(); }
1556 const Value *getIndexOperand() const { return Op<1>(); }
1558 VectorType *getVectorOperandType() const {
1559 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1563 /// Transparently provide more efficient getOperand methods.
1564 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1566 // Methods for support type inquiry through isa, cast, and dyn_cast:
1567 static inline bool classof(const Instruction *I) {
1568 return I->getOpcode() == Instruction::ExtractElement;
1570 static inline bool classof(const Value *V) {
1571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1576 struct OperandTraits<ExtractElementInst> :
1577 public FixedNumOperandTraits<ExtractElementInst, 2> {
1580 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1582 //===----------------------------------------------------------------------===//
1583 // InsertElementInst Class
1584 //===----------------------------------------------------------------------===//
1586 /// InsertElementInst - This instruction inserts a single (scalar)
1587 /// element into a VectorType value
1589 class InsertElementInst : public Instruction {
1590 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1591 const Twine &NameStr = "",
1592 Instruction *InsertBefore = 0);
1593 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1594 const Twine &NameStr, BasicBlock *InsertAtEnd);
1596 virtual InsertElementInst *clone_impl() const;
1599 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1600 const Twine &NameStr = "",
1601 Instruction *InsertBefore = 0) {
1602 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1604 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1605 const Twine &NameStr,
1606 BasicBlock *InsertAtEnd) {
1607 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1610 /// isValidOperands - Return true if an insertelement instruction can be
1611 /// formed with the specified operands.
1612 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1615 /// getType - Overload to return most specific vector type.
1617 VectorType *getType() const {
1618 return reinterpret_cast<VectorType*>(Instruction::getType());
1621 /// Transparently provide more efficient getOperand methods.
1622 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1624 // Methods for support type inquiry through isa, cast, and dyn_cast:
1625 static inline bool classof(const Instruction *I) {
1626 return I->getOpcode() == Instruction::InsertElement;
1628 static inline bool classof(const Value *V) {
1629 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1634 struct OperandTraits<InsertElementInst> :
1635 public FixedNumOperandTraits<InsertElementInst, 3> {
1638 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1640 //===----------------------------------------------------------------------===//
1641 // ShuffleVectorInst Class
1642 //===----------------------------------------------------------------------===//
1644 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1647 class ShuffleVectorInst : public Instruction {
1649 virtual ShuffleVectorInst *clone_impl() const;
1652 // allocate space for exactly three operands
1653 void *operator new(size_t s) {
1654 return User::operator new(s, 3);
1656 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1657 const Twine &NameStr = "",
1658 Instruction *InsertBefor = 0);
1659 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1660 const Twine &NameStr, BasicBlock *InsertAtEnd);
1662 /// isValidOperands - Return true if a shufflevector instruction can be
1663 /// formed with the specified operands.
1664 static bool isValidOperands(const Value *V1, const Value *V2,
1667 /// getType - Overload to return most specific vector type.
1669 VectorType *getType() const {
1670 return reinterpret_cast<VectorType*>(Instruction::getType());
1673 /// Transparently provide more efficient getOperand methods.
1674 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1676 Constant *getMask() const {
1677 return reinterpret_cast<Constant*>(getOperand(2));
1680 /// getMaskValue - Return the index from the shuffle mask for the specified
1681 /// output result. This is either -1 if the element is undef or a number less
1682 /// than 2*numelements.
1683 static int getMaskValue(Constant *Mask, unsigned i);
1685 int getMaskValue(unsigned i) const {
1686 return getMaskValue(getMask(), i);
1689 /// getShuffleMask - Return the full mask for this instruction, where each
1690 /// element is the element number and undef's are returned as -1.
1691 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1693 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1694 return getShuffleMask(getMask(), Result);
1697 SmallVector<int, 16> getShuffleMask() const {
1698 SmallVector<int, 16> Mask;
1699 getShuffleMask(Mask);
1704 // Methods for support type inquiry through isa, cast, and dyn_cast:
1705 static inline bool classof(const Instruction *I) {
1706 return I->getOpcode() == Instruction::ShuffleVector;
1708 static inline bool classof(const Value *V) {
1709 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1714 struct OperandTraits<ShuffleVectorInst> :
1715 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1718 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1720 //===----------------------------------------------------------------------===//
1721 // ExtractValueInst Class
1722 //===----------------------------------------------------------------------===//
1724 /// ExtractValueInst - This instruction extracts a struct member or array
1725 /// element value from an aggregate value.
1727 class ExtractValueInst : public UnaryInstruction {
1728 SmallVector<unsigned, 4> Indices;
1730 ExtractValueInst(const ExtractValueInst &EVI);
1731 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1733 /// Constructors - Create a extractvalue instruction with a base aggregate
1734 /// value and a list of indices. The first ctor can optionally insert before
1735 /// an existing instruction, the second appends the new instruction to the
1736 /// specified BasicBlock.
1737 inline ExtractValueInst(Value *Agg,
1738 ArrayRef<unsigned> Idxs,
1739 const Twine &NameStr,
1740 Instruction *InsertBefore);
1741 inline ExtractValueInst(Value *Agg,
1742 ArrayRef<unsigned> Idxs,
1743 const Twine &NameStr, BasicBlock *InsertAtEnd);
1745 // allocate space for exactly one operand
1746 void *operator new(size_t s) {
1747 return User::operator new(s, 1);
1750 virtual ExtractValueInst *clone_impl() const;
1753 static ExtractValueInst *Create(Value *Agg,
1754 ArrayRef<unsigned> Idxs,
1755 const Twine &NameStr = "",
1756 Instruction *InsertBefore = 0) {
1758 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1760 static ExtractValueInst *Create(Value *Agg,
1761 ArrayRef<unsigned> Idxs,
1762 const Twine &NameStr,
1763 BasicBlock *InsertAtEnd) {
1764 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1767 /// getIndexedType - Returns the type of the element that would be extracted
1768 /// with an extractvalue instruction with the specified parameters.
1770 /// Null is returned if the indices are invalid for the specified type.
1771 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1773 typedef const unsigned* idx_iterator;
1774 inline idx_iterator idx_begin() const { return Indices.begin(); }
1775 inline idx_iterator idx_end() const { return Indices.end(); }
1777 Value *getAggregateOperand() {
1778 return getOperand(0);
1780 const Value *getAggregateOperand() const {
1781 return getOperand(0);
1783 static unsigned getAggregateOperandIndex() {
1784 return 0U; // get index for modifying correct operand
1787 ArrayRef<unsigned> getIndices() const {
1791 unsigned getNumIndices() const {
1792 return (unsigned)Indices.size();
1795 bool hasIndices() const {
1799 // Methods for support type inquiry through isa, cast, and dyn_cast:
1800 static inline bool classof(const Instruction *I) {
1801 return I->getOpcode() == Instruction::ExtractValue;
1803 static inline bool classof(const Value *V) {
1804 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1808 ExtractValueInst::ExtractValueInst(Value *Agg,
1809 ArrayRef<unsigned> Idxs,
1810 const Twine &NameStr,
1811 Instruction *InsertBefore)
1812 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1813 ExtractValue, Agg, InsertBefore) {
1814 init(Idxs, NameStr);
1816 ExtractValueInst::ExtractValueInst(Value *Agg,
1817 ArrayRef<unsigned> Idxs,
1818 const Twine &NameStr,
1819 BasicBlock *InsertAtEnd)
1820 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1821 ExtractValue, Agg, InsertAtEnd) {
1822 init(Idxs, NameStr);
1826 //===----------------------------------------------------------------------===//
1827 // InsertValueInst Class
1828 //===----------------------------------------------------------------------===//
1830 /// InsertValueInst - This instruction inserts a struct field of array element
1831 /// value into an aggregate value.
1833 class InsertValueInst : public Instruction {
1834 SmallVector<unsigned, 4> Indices;
1836 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1837 InsertValueInst(const InsertValueInst &IVI);
1838 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1839 const Twine &NameStr);
1841 /// Constructors - Create a insertvalue instruction with a base aggregate
1842 /// value, a value to insert, and a list of indices. The first ctor can
1843 /// optionally insert before an existing instruction, the second appends
1844 /// the new instruction to the specified BasicBlock.
1845 inline InsertValueInst(Value *Agg, Value *Val,
1846 ArrayRef<unsigned> Idxs,
1847 const Twine &NameStr,
1848 Instruction *InsertBefore);
1849 inline InsertValueInst(Value *Agg, Value *Val,
1850 ArrayRef<unsigned> Idxs,
1851 const Twine &NameStr, BasicBlock *InsertAtEnd);
1853 /// Constructors - These two constructors are convenience methods because one
1854 /// and two index insertvalue instructions are so common.
1855 InsertValueInst(Value *Agg, Value *Val,
1856 unsigned Idx, const Twine &NameStr = "",
1857 Instruction *InsertBefore = 0);
1858 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1859 const Twine &NameStr, BasicBlock *InsertAtEnd);
1861 virtual InsertValueInst *clone_impl() const;
1863 // allocate space for exactly two operands
1864 void *operator new(size_t s) {
1865 return User::operator new(s, 2);
1868 static InsertValueInst *Create(Value *Agg, Value *Val,
1869 ArrayRef<unsigned> Idxs,
1870 const Twine &NameStr = "",
1871 Instruction *InsertBefore = 0) {
1872 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1874 static InsertValueInst *Create(Value *Agg, Value *Val,
1875 ArrayRef<unsigned> Idxs,
1876 const Twine &NameStr,
1877 BasicBlock *InsertAtEnd) {
1878 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1881 /// Transparently provide more efficient getOperand methods.
1882 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1884 typedef const unsigned* idx_iterator;
1885 inline idx_iterator idx_begin() const { return Indices.begin(); }
1886 inline idx_iterator idx_end() const { return Indices.end(); }
1888 Value *getAggregateOperand() {
1889 return getOperand(0);
1891 const Value *getAggregateOperand() const {
1892 return getOperand(0);
1894 static unsigned getAggregateOperandIndex() {
1895 return 0U; // get index for modifying correct operand
1898 Value *getInsertedValueOperand() {
1899 return getOperand(1);
1901 const Value *getInsertedValueOperand() const {
1902 return getOperand(1);
1904 static unsigned getInsertedValueOperandIndex() {
1905 return 1U; // get index for modifying correct operand
1908 ArrayRef<unsigned> getIndices() const {
1912 unsigned getNumIndices() const {
1913 return (unsigned)Indices.size();
1916 bool hasIndices() const {
1920 // Methods for support type inquiry through isa, cast, and dyn_cast:
1921 static inline bool classof(const Instruction *I) {
1922 return I->getOpcode() == Instruction::InsertValue;
1924 static inline bool classof(const Value *V) {
1925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1930 struct OperandTraits<InsertValueInst> :
1931 public FixedNumOperandTraits<InsertValueInst, 2> {
1934 InsertValueInst::InsertValueInst(Value *Agg,
1936 ArrayRef<unsigned> Idxs,
1937 const Twine &NameStr,
1938 Instruction *InsertBefore)
1939 : Instruction(Agg->getType(), InsertValue,
1940 OperandTraits<InsertValueInst>::op_begin(this),
1942 init(Agg, Val, Idxs, NameStr);
1944 InsertValueInst::InsertValueInst(Value *Agg,
1946 ArrayRef<unsigned> Idxs,
1947 const Twine &NameStr,
1948 BasicBlock *InsertAtEnd)
1949 : Instruction(Agg->getType(), InsertValue,
1950 OperandTraits<InsertValueInst>::op_begin(this),
1952 init(Agg, Val, Idxs, NameStr);
1955 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1957 //===----------------------------------------------------------------------===//
1959 //===----------------------------------------------------------------------===//
1961 // PHINode - The PHINode class is used to represent the magical mystical PHI
1962 // node, that can not exist in nature, but can be synthesized in a computer
1963 // scientist's overactive imagination.
1965 class PHINode : public Instruction {
1966 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1967 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1968 /// the number actually in use.
1969 unsigned ReservedSpace;
1970 PHINode(const PHINode &PN);
1971 // allocate space for exactly zero operands
1972 void *operator new(size_t s) {
1973 return User::operator new(s, 0);
1975 explicit PHINode(Type *Ty, unsigned NumReservedValues,
1976 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1977 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1978 ReservedSpace(NumReservedValues) {
1980 OperandList = allocHungoffUses(ReservedSpace);
1983 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1984 BasicBlock *InsertAtEnd)
1985 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1986 ReservedSpace(NumReservedValues) {
1988 OperandList = allocHungoffUses(ReservedSpace);
1991 // allocHungoffUses - this is more complicated than the generic
1992 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1993 // values and pointers to the incoming blocks, all in one allocation.
1994 Use *allocHungoffUses(unsigned) const;
1996 virtual PHINode *clone_impl() const;
1998 /// Constructors - NumReservedValues is a hint for the number of incoming
1999 /// edges that this phi node will have (use 0 if you really have no idea).
2000 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2001 const Twine &NameStr = "",
2002 Instruction *InsertBefore = 0) {
2003 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2005 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2006 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2007 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2011 /// Provide fast operand accessors
2012 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2014 // Block iterator interface. This provides access to the list of incoming
2015 // basic blocks, which parallels the list of incoming values.
2017 typedef BasicBlock **block_iterator;
2018 typedef BasicBlock * const *const_block_iterator;
2020 block_iterator block_begin() {
2022 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2023 return reinterpret_cast<block_iterator>(ref + 1);
2026 const_block_iterator block_begin() const {
2027 const Use::UserRef *ref =
2028 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2029 return reinterpret_cast<const_block_iterator>(ref + 1);
2032 block_iterator block_end() {
2033 return block_begin() + getNumOperands();
2036 const_block_iterator block_end() const {
2037 return block_begin() + getNumOperands();
2040 /// getNumIncomingValues - Return the number of incoming edges
2042 unsigned getNumIncomingValues() const { return getNumOperands(); }
2044 /// getIncomingValue - Return incoming value number x
2046 Value *getIncomingValue(unsigned i) const {
2047 return getOperand(i);
2049 void setIncomingValue(unsigned i, Value *V) {
2052 static unsigned getOperandNumForIncomingValue(unsigned i) {
2055 static unsigned getIncomingValueNumForOperand(unsigned i) {
2059 /// getIncomingBlock - Return incoming basic block number @p i.
2061 BasicBlock *getIncomingBlock(unsigned i) const {
2062 return block_begin()[i];
2065 /// getIncomingBlock - Return incoming basic block corresponding
2066 /// to an operand of the PHI.
2068 BasicBlock *getIncomingBlock(const Use &U) const {
2069 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2070 return getIncomingBlock(unsigned(&U - op_begin()));
2073 /// getIncomingBlock - Return incoming basic block corresponding
2074 /// to value use iterator.
2076 template <typename U>
2077 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2078 return getIncomingBlock(I.getUse());
2081 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2082 block_begin()[i] = BB;
2085 /// addIncoming - Add an incoming value to the end of the PHI list
2087 void addIncoming(Value *V, BasicBlock *BB) {
2088 assert(V && "PHI node got a null value!");
2089 assert(BB && "PHI node got a null basic block!");
2090 assert(getType() == V->getType() &&
2091 "All operands to PHI node must be the same type as the PHI node!");
2092 if (NumOperands == ReservedSpace)
2093 growOperands(); // Get more space!
2094 // Initialize some new operands.
2096 setIncomingValue(NumOperands - 1, V);
2097 setIncomingBlock(NumOperands - 1, BB);
2100 /// removeIncomingValue - Remove an incoming value. This is useful if a
2101 /// predecessor basic block is deleted. The value removed is returned.
2103 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2104 /// is true), the PHI node is destroyed and any uses of it are replaced with
2105 /// dummy values. The only time there should be zero incoming values to a PHI
2106 /// node is when the block is dead, so this strategy is sound.
2108 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2110 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2111 int Idx = getBasicBlockIndex(BB);
2112 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2113 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2116 /// getBasicBlockIndex - Return the first index of the specified basic
2117 /// block in the value list for this PHI. Returns -1 if no instance.
2119 int getBasicBlockIndex(const BasicBlock *BB) const {
2120 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2121 if (block_begin()[i] == BB)
2126 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2127 int Idx = getBasicBlockIndex(BB);
2128 assert(Idx >= 0 && "Invalid basic block argument!");
2129 return getIncomingValue(Idx);
2132 /// hasConstantValue - If the specified PHI node always merges together the
2133 /// same value, return the value, otherwise return null.
2134 Value *hasConstantValue() const;
2136 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2137 static inline bool classof(const Instruction *I) {
2138 return I->getOpcode() == Instruction::PHI;
2140 static inline bool classof(const Value *V) {
2141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2144 void growOperands();
2148 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2151 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2153 //===----------------------------------------------------------------------===//
2154 // LandingPadInst Class
2155 //===----------------------------------------------------------------------===//
2157 //===---------------------------------------------------------------------------
2158 /// LandingPadInst - The landingpad instruction holds all of the information
2159 /// necessary to generate correct exception handling. The landingpad instruction
2160 /// cannot be moved from the top of a landing pad block, which itself is
2161 /// accessible only from the 'unwind' edge of an invoke. This uses the
2162 /// SubclassData field in Value to store whether or not the landingpad is a
2165 class LandingPadInst : public Instruction {
2166 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2167 /// the number actually in use.
2168 unsigned ReservedSpace;
2169 LandingPadInst(const LandingPadInst &LP);
2171 enum ClauseType { Catch, Filter };
2173 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2174 // Allocate space for exactly zero operands.
2175 void *operator new(size_t s) {
2176 return User::operator new(s, 0);
2178 void growOperands(unsigned Size);
2179 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2181 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2182 unsigned NumReservedValues, const Twine &NameStr,
2183 Instruction *InsertBefore);
2184 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2185 unsigned NumReservedValues, const Twine &NameStr,
2186 BasicBlock *InsertAtEnd);
2188 virtual LandingPadInst *clone_impl() const;
2190 /// Constructors - NumReservedClauses is a hint for the number of incoming
2191 /// clauses that this landingpad will have (use 0 if you really have no idea).
2192 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2193 unsigned NumReservedClauses,
2194 const Twine &NameStr = "",
2195 Instruction *InsertBefore = 0);
2196 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2197 unsigned NumReservedClauses,
2198 const Twine &NameStr, BasicBlock *InsertAtEnd);
2201 /// Provide fast operand accessors
2202 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2204 /// getPersonalityFn - Get the personality function associated with this
2206 Value *getPersonalityFn() const { return getOperand(0); }
2208 /// isCleanup - Return 'true' if this landingpad instruction is a
2209 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2210 /// doesn't catch the exception.
2211 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2213 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2214 void setCleanup(bool V) {
2215 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2219 /// addClause - Add a catch or filter clause to the landing pad.
2220 void addClause(Value *ClauseVal);
2222 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2223 /// to determine what type of clause this is.
2224 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2226 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2227 bool isCatch(unsigned Idx) const {
2228 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2231 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2232 bool isFilter(unsigned Idx) const {
2233 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2236 /// getNumClauses - Get the number of clauses for this landing pad.
2237 unsigned getNumClauses() const { return getNumOperands() - 1; }
2239 /// reserveClauses - Grow the size of the operand list to accommodate the new
2240 /// number of clauses.
2241 void reserveClauses(unsigned Size) { growOperands(Size); }
2243 // Methods for support type inquiry through isa, cast, and dyn_cast:
2244 static inline bool classof(const Instruction *I) {
2245 return I->getOpcode() == Instruction::LandingPad;
2247 static inline bool classof(const Value *V) {
2248 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2253 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2256 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2258 //===----------------------------------------------------------------------===//
2260 //===----------------------------------------------------------------------===//
2262 //===---------------------------------------------------------------------------
2263 /// ReturnInst - Return a value (possibly void), from a function. Execution
2264 /// does not continue in this function any longer.
2266 class ReturnInst : public TerminatorInst {
2267 ReturnInst(const ReturnInst &RI);
2270 // ReturnInst constructors:
2271 // ReturnInst() - 'ret void' instruction
2272 // ReturnInst( null) - 'ret void' instruction
2273 // ReturnInst(Value* X) - 'ret X' instruction
2274 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2275 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2276 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2277 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2279 // NOTE: If the Value* passed is of type void then the constructor behaves as
2280 // if it was passed NULL.
2281 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2282 Instruction *InsertBefore = 0);
2283 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2284 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2286 virtual ReturnInst *clone_impl() const;
2288 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2289 Instruction *InsertBefore = 0) {
2290 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2292 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2293 BasicBlock *InsertAtEnd) {
2294 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2296 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2297 return new(0) ReturnInst(C, InsertAtEnd);
2299 virtual ~ReturnInst();
2301 /// Provide fast operand accessors
2302 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2304 /// Convenience accessor. Returns null if there is no return value.
2305 Value *getReturnValue() const {
2306 return getNumOperands() != 0 ? getOperand(0) : 0;
2309 unsigned getNumSuccessors() const { return 0; }
2311 // Methods for support type inquiry through isa, cast, and dyn_cast:
2312 static inline bool classof(const Instruction *I) {
2313 return (I->getOpcode() == Instruction::Ret);
2315 static inline bool classof(const Value *V) {
2316 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2319 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2320 virtual unsigned getNumSuccessorsV() const;
2321 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2325 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2328 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2330 //===----------------------------------------------------------------------===//
2332 //===----------------------------------------------------------------------===//
2334 //===---------------------------------------------------------------------------
2335 /// BranchInst - Conditional or Unconditional Branch instruction.
2337 class BranchInst : public TerminatorInst {
2338 /// Ops list - Branches are strange. The operands are ordered:
2339 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2340 /// they don't have to check for cond/uncond branchness. These are mostly
2341 /// accessed relative from op_end().
2342 BranchInst(const BranchInst &BI);
2344 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2345 // BranchInst(BB *B) - 'br B'
2346 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2347 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2348 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2349 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2350 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2351 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2352 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2353 Instruction *InsertBefore = 0);
2354 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2355 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2356 BasicBlock *InsertAtEnd);
2358 virtual BranchInst *clone_impl() const;
2360 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2361 return new(1) BranchInst(IfTrue, InsertBefore);
2363 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2364 Value *Cond, Instruction *InsertBefore = 0) {
2365 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2367 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2368 return new(1) BranchInst(IfTrue, InsertAtEnd);
2370 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2371 Value *Cond, BasicBlock *InsertAtEnd) {
2372 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2375 /// Transparently provide more efficient getOperand methods.
2376 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2378 bool isUnconditional() const { return getNumOperands() == 1; }
2379 bool isConditional() const { return getNumOperands() == 3; }
2381 Value *getCondition() const {
2382 assert(isConditional() && "Cannot get condition of an uncond branch!");
2386 void setCondition(Value *V) {
2387 assert(isConditional() && "Cannot set condition of unconditional branch!");
2391 unsigned getNumSuccessors() const { return 1+isConditional(); }
2393 BasicBlock *getSuccessor(unsigned i) const {
2394 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2395 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2398 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2399 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2400 *(&Op<-1>() - idx) = (Value*)NewSucc;
2403 /// \brief Swap the successors of this branch instruction.
2405 /// Swaps the successors of the branch instruction. This also swaps any
2406 /// branch weight metadata associated with the instruction so that it
2407 /// continues to map correctly to each operand.
2408 void swapSuccessors();
2410 // Methods for support type inquiry through isa, cast, and dyn_cast:
2411 static inline bool classof(const Instruction *I) {
2412 return (I->getOpcode() == Instruction::Br);
2414 static inline bool classof(const Value *V) {
2415 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2418 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2419 virtual unsigned getNumSuccessorsV() const;
2420 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2424 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2427 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2429 //===----------------------------------------------------------------------===//
2431 //===----------------------------------------------------------------------===//
2433 //===---------------------------------------------------------------------------
2434 /// SwitchInst - Multiway switch
2436 class SwitchInst : public TerminatorInst {
2437 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2438 unsigned ReservedSpace;
2440 // Operand[0] = Value to switch on
2441 // Operand[1] = Default basic block destination
2442 // Operand[2n ] = Value to match
2443 // Operand[2n+1] = BasicBlock to go to on match
2445 // Store case values separately from operands list. We needn't User-Use
2446 // concept here, since it is just a case value, it will always constant,
2447 // and case value couldn't reused with another instructions/values.
2449 // It allows us to use custom type for case values that is not inherited
2450 // from Value. Since case value is a complex type that implements
2451 // the subset of integers, we needn't extract sub-constants within
2452 // slow getAggregateElement method.
2453 // For case values we will use std::list to by two reasons:
2454 // 1. It allows to add/remove cases without whole collection reallocation.
2455 // 2. In most of cases we needn't random access.
2456 // Currently case values are also stored in Operands List, but it will moved
2457 // out in future commits.
2458 typedef std::list<IntegersSubset> Subsets;
2459 typedef Subsets::iterator SubsetsIt;
2460 typedef Subsets::const_iterator SubsetsConstIt;
2464 SwitchInst(const SwitchInst &SI);
2465 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2466 void growOperands();
2467 // allocate space for exactly zero operands
2468 void *operator new(size_t s) {
2469 return User::operator new(s, 0);
2471 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2472 /// switch on and a default destination. The number of additional cases can
2473 /// be specified here to make memory allocation more efficient. This
2474 /// constructor can also autoinsert before another instruction.
2475 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2476 Instruction *InsertBefore);
2478 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2479 /// switch on and a default destination. The number of additional cases can
2480 /// be specified here to make memory allocation more efficient. This
2481 /// constructor also autoinserts at the end of the specified BasicBlock.
2482 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2483 BasicBlock *InsertAtEnd);
2485 virtual SwitchInst *clone_impl() const;
2488 // FIXME: Currently there are a lot of unclean template parameters,
2489 // we need to make refactoring in future.
2490 // All these parameters are used to implement both iterator and const_iterator
2491 // without code duplication.
2492 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2493 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2494 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2495 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2496 template <class SwitchInstTy, class ConstantIntTy,
2497 class SubsetsItTy, class BasicBlockTy>
2498 class CaseIteratorT;
2500 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2501 SubsetsConstIt, const BasicBlock> ConstCaseIt;
2505 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2507 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2508 unsigned NumCases, Instruction *InsertBefore = 0) {
2509 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2511 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2512 unsigned NumCases, BasicBlock *InsertAtEnd) {
2513 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2518 /// Provide fast operand accessors
2519 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2521 // Accessor Methods for Switch stmt
2522 Value *getCondition() const { return getOperand(0); }
2523 void setCondition(Value *V) { setOperand(0, V); }
2525 BasicBlock *getDefaultDest() const {
2526 return cast<BasicBlock>(getOperand(1));
2529 void setDefaultDest(BasicBlock *DefaultCase) {
2530 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2533 /// getNumCases - return the number of 'cases' in this switch instruction,
2534 /// except the default case
2535 unsigned getNumCases() const {
2536 return getNumOperands()/2 - 1;
2539 /// Returns a read/write iterator that points to the first
2540 /// case in SwitchInst.
2541 CaseIt case_begin() {
2542 return CaseIt(this, 0, TheSubsets.begin());
2544 /// Returns a read-only iterator that points to the first
2545 /// case in the SwitchInst.
2546 ConstCaseIt case_begin() const {
2547 return ConstCaseIt(this, 0, TheSubsets.begin());
2550 /// Returns a read/write iterator that points one past the last
2551 /// in the SwitchInst.
2553 return CaseIt(this, getNumCases(), TheSubsets.end());
2555 /// Returns a read-only iterator that points one past the last
2556 /// in the SwitchInst.
2557 ConstCaseIt case_end() const {
2558 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2560 /// Returns an iterator that points to the default case.
2561 /// Note: this iterator allows to resolve successor only. Attempt
2562 /// to resolve case value causes an assertion.
2563 /// Also note, that increment and decrement also causes an assertion and
2564 /// makes iterator invalid.
2565 CaseIt case_default() {
2566 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2568 ConstCaseIt case_default() const {
2569 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2572 /// findCaseValue - Search all of the case values for the specified constant.
2573 /// If it is explicitly handled, return the case iterator of it, otherwise
2574 /// return default case iterator to indicate
2575 /// that it is handled by the default handler.
2576 CaseIt findCaseValue(const ConstantInt *C) {
2577 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2578 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2580 return case_default();
2582 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2583 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2584 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2586 return case_default();
2589 /// findCaseDest - Finds the unique case value for a given successor. Returns
2590 /// null if the successor is not found, not unique, or is the default case.
2591 ConstantInt *findCaseDest(BasicBlock *BB) {
2592 if (BB == getDefaultDest()) return NULL;
2594 ConstantInt *CI = NULL;
2595 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2596 if (i.getCaseSuccessor() == BB) {
2597 if (CI) return NULL; // Multiple cases lead to BB.
2598 else CI = i.getCaseValue();
2604 /// addCase - Add an entry to the switch instruction...
2607 /// This action invalidates case_end(). Old case_end() iterator will
2608 /// point to the added case.
2609 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2611 /// addCase - Add an entry to the switch instruction.
2613 /// This action invalidates case_end(). Old case_end() iterator will
2614 /// point to the added case.
2615 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2617 /// removeCase - This method removes the specified case and its successor
2618 /// from the switch instruction. Note that this operation may reorder the
2619 /// remaining cases at index idx and above.
2621 /// This action invalidates iterators for all cases following the one removed,
2622 /// including the case_end() iterator.
2623 void removeCase(CaseIt& i);
2625 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2626 BasicBlock *getSuccessor(unsigned idx) const {
2627 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2628 return cast<BasicBlock>(getOperand(idx*2+1));
2630 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2631 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2632 setOperand(idx*2+1, (Value*)NewSucc);
2635 uint16_t hash() const {
2636 uint32_t NumberOfCases = (uint32_t)getNumCases();
2637 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2638 for (ConstCaseIt i = case_begin(), e = case_end();
2640 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2641 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2646 // Case iterators definition.
2648 template <class SwitchInstTy, class ConstantIntTy,
2649 class SubsetsItTy, class BasicBlockTy>
2650 class CaseIteratorT {
2654 unsigned long Index;
2655 SubsetsItTy SubsetIt;
2657 /// Initializes case iterator for given SwitchInst and for given
2659 friend class SwitchInst;
2660 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2661 SubsetsItTy CaseValueIt) {
2663 Index = SuccessorIndex;
2664 this->SubsetIt = CaseValueIt;
2668 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2669 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2670 SubsetsItTy, BasicBlockTy> Self;
2672 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2675 SubsetIt = SI->TheSubsets.begin();
2676 std::advance(SubsetIt, CaseNum);
2680 /// Initializes case iterator for given SwitchInst and for given
2681 /// TerminatorInst's successor index.
2682 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2683 assert(SuccessorIndex < SI->getNumSuccessors() &&
2684 "Successor index # out of range!");
2685 return SuccessorIndex != 0 ?
2686 Self(SI, SuccessorIndex - 1) :
2687 Self(SI, DefaultPseudoIndex);
2690 /// Resolves case value for current case.
2692 ConstantIntTy *getCaseValue() {
2693 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2694 IntegersSubsetRef CaseRanges = *SubsetIt;
2696 // FIXME: Currently we work with ConstantInt based cases.
2697 // So return CaseValue as ConstantInt.
2698 return CaseRanges.getSingleNumber(0).toConstantInt();
2701 /// Resolves case value for current case.
2702 IntegersSubsetRef getCaseValueEx() {
2703 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2707 /// Resolves successor for current case.
2708 BasicBlockTy *getCaseSuccessor() {
2709 assert((Index < SI->getNumCases() ||
2710 Index == DefaultPseudoIndex) &&
2711 "Index out the number of cases.");
2712 return SI->getSuccessor(getSuccessorIndex());
2715 /// Returns number of current case.
2716 unsigned getCaseIndex() const { return Index; }
2718 /// Returns TerminatorInst's successor index for current case successor.
2719 unsigned getSuccessorIndex() const {
2720 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2721 "Index out the number of cases.");
2722 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2726 // Check index correctness after increment.
2727 // Note: Index == getNumCases() means end().
2728 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2731 SubsetIt = SI->TheSubsets.begin();
2736 Self operator++(int) {
2742 // Check index correctness after decrement.
2743 // Note: Index == getNumCases() means end().
2744 // Also allow "-1" iterator here. That will became valid after ++.
2745 unsigned NumCases = SI->getNumCases();
2746 assert((Index == 0 || Index-1 <= NumCases) &&
2747 "Index out the number of cases.");
2749 if (Index == NumCases) {
2750 SubsetIt = SI->TheSubsets.end();
2759 Self operator--(int) {
2764 bool operator==(const Self& RHS) const {
2765 assert(RHS.SI == SI && "Incompatible operators.");
2766 return RHS.Index == Index;
2768 bool operator!=(const Self& RHS) const {
2769 assert(RHS.SI == SI && "Incompatible operators.");
2770 return RHS.Index != Index;
2774 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2775 SubsetsIt, BasicBlock> {
2776 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2780 friend class SwitchInst;
2781 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2782 ParentTy(SI, CaseNum, SubsetIt) {}
2784 void updateCaseValueOperand(IntegersSubset& V) {
2785 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2790 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2792 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2794 /// Sets the new value for current case.
2796 void setValue(ConstantInt *V) {
2797 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2798 IntegersSubsetToBB Mapping;
2799 // FIXME: Currently we work with ConstantInt based cases.
2800 // So inititalize IntItem container directly from ConstantInt.
2801 Mapping.add(IntItem::fromConstantInt(V));
2802 *SubsetIt = Mapping.getCase();
2803 updateCaseValueOperand(*SubsetIt);
2806 /// Sets the new value for current case.
2807 void setValueEx(IntegersSubset& V) {
2808 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2810 updateCaseValueOperand(*SubsetIt);
2813 /// Sets the new successor for current case.
2814 void setSuccessor(BasicBlock *S) {
2815 SI->setSuccessor(getSuccessorIndex(), S);
2819 // Methods for support type inquiry through isa, cast, and dyn_cast:
2821 static inline bool classof(const Instruction *I) {
2822 return I->getOpcode() == Instruction::Switch;
2824 static inline bool classof(const Value *V) {
2825 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2828 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2829 virtual unsigned getNumSuccessorsV() const;
2830 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2834 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2837 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2840 //===----------------------------------------------------------------------===//
2841 // IndirectBrInst Class
2842 //===----------------------------------------------------------------------===//
2844 //===---------------------------------------------------------------------------
2845 /// IndirectBrInst - Indirect Branch Instruction.
2847 class IndirectBrInst : public TerminatorInst {
2848 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2849 unsigned ReservedSpace;
2850 // Operand[0] = Value to switch on
2851 // Operand[1] = Default basic block destination
2852 // Operand[2n ] = Value to match
2853 // Operand[2n+1] = BasicBlock to go to on match
2854 IndirectBrInst(const IndirectBrInst &IBI);
2855 void init(Value *Address, unsigned NumDests);
2856 void growOperands();
2857 // allocate space for exactly zero operands
2858 void *operator new(size_t s) {
2859 return User::operator new(s, 0);
2861 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2862 /// Address to jump to. The number of expected destinations can be specified
2863 /// here to make memory allocation more efficient. This constructor can also
2864 /// autoinsert before another instruction.
2865 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2867 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2868 /// Address to jump to. The number of expected destinations can be specified
2869 /// here to make memory allocation more efficient. This constructor also
2870 /// autoinserts at the end of the specified BasicBlock.
2871 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2873 virtual IndirectBrInst *clone_impl() const;
2875 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2876 Instruction *InsertBefore = 0) {
2877 return new IndirectBrInst(Address, NumDests, InsertBefore);
2879 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2880 BasicBlock *InsertAtEnd) {
2881 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2885 /// Provide fast operand accessors.
2886 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2888 // Accessor Methods for IndirectBrInst instruction.
2889 Value *getAddress() { return getOperand(0); }
2890 const Value *getAddress() const { return getOperand(0); }
2891 void setAddress(Value *V) { setOperand(0, V); }
2894 /// getNumDestinations - return the number of possible destinations in this
2895 /// indirectbr instruction.
2896 unsigned getNumDestinations() const { return getNumOperands()-1; }
2898 /// getDestination - Return the specified destination.
2899 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2900 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2902 /// addDestination - Add a destination.
2904 void addDestination(BasicBlock *Dest);
2906 /// removeDestination - This method removes the specified successor from the
2907 /// indirectbr instruction.
2908 void removeDestination(unsigned i);
2910 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2911 BasicBlock *getSuccessor(unsigned i) const {
2912 return cast<BasicBlock>(getOperand(i+1));
2914 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2915 setOperand(i+1, (Value*)NewSucc);
2918 // Methods for support type inquiry through isa, cast, and dyn_cast:
2919 static inline bool classof(const Instruction *I) {
2920 return I->getOpcode() == Instruction::IndirectBr;
2922 static inline bool classof(const Value *V) {
2923 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2926 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2927 virtual unsigned getNumSuccessorsV() const;
2928 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2932 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2935 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2938 //===----------------------------------------------------------------------===//
2940 //===----------------------------------------------------------------------===//
2942 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2943 /// calling convention of the call.
2945 class InvokeInst : public TerminatorInst {
2946 AttrListPtr AttributeList;
2947 InvokeInst(const InvokeInst &BI);
2948 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2949 ArrayRef<Value *> Args, const Twine &NameStr);
2951 /// Construct an InvokeInst given a range of arguments.
2953 /// @brief Construct an InvokeInst from a range of arguments
2954 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2955 ArrayRef<Value *> Args, unsigned Values,
2956 const Twine &NameStr, Instruction *InsertBefore);
2958 /// Construct an InvokeInst given a range of arguments.
2960 /// @brief Construct an InvokeInst from a range of arguments
2961 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2962 ArrayRef<Value *> Args, unsigned Values,
2963 const Twine &NameStr, BasicBlock *InsertAtEnd);
2965 virtual InvokeInst *clone_impl() const;
2967 static InvokeInst *Create(Value *Func,
2968 BasicBlock *IfNormal, BasicBlock *IfException,
2969 ArrayRef<Value *> Args, const Twine &NameStr = "",
2970 Instruction *InsertBefore = 0) {
2971 unsigned Values = unsigned(Args.size()) + 3;
2972 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2973 Values, NameStr, InsertBefore);
2975 static InvokeInst *Create(Value *Func,
2976 BasicBlock *IfNormal, BasicBlock *IfException,
2977 ArrayRef<Value *> Args, const Twine &NameStr,
2978 BasicBlock *InsertAtEnd) {
2979 unsigned Values = unsigned(Args.size()) + 3;
2980 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2981 Values, NameStr, InsertAtEnd);
2984 /// Provide fast operand accessors
2985 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2987 /// getNumArgOperands - Return the number of invoke arguments.
2989 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2991 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2993 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2994 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2996 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2998 CallingConv::ID getCallingConv() const {
2999 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3001 void setCallingConv(CallingConv::ID CC) {
3002 setInstructionSubclassData(static_cast<unsigned>(CC));
3005 /// getAttributes - Return the parameter attributes for this invoke.
3007 const AttrListPtr &getAttributes() const { return AttributeList; }
3009 /// setAttributes - Set the parameter attributes for this invoke.
3011 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3013 /// addAttribute - adds the attribute to the list of attributes.
3014 void addAttribute(unsigned i, Attributes attr);
3016 /// removeAttribute - removes the attribute from the list of attributes.
3017 void removeAttribute(unsigned i, Attributes attr);
3019 /// @brief Determine whether this call has the NoAlias attribute.
3020 bool hasFnAttr(Attributes::AttrVal A) const;
3022 /// @brief Determine whether the call or the callee has the given attributes.
3023 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
3025 /// @brief Extract the alignment for a call or parameter (0=unknown).
3026 unsigned getParamAlignment(unsigned i) const {
3027 return AttributeList.getParamAlignment(i);
3030 /// @brief Return true if the call should not be inlined.
3031 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
3032 void setIsNoInline() {
3033 addAttribute(AttrListPtr::FunctionIndex,
3034 Attributes::get(getContext(), Attributes::NoInline));
3037 /// @brief Determine if the call does not access memory.
3038 bool doesNotAccessMemory() const {
3039 return hasFnAttr(Attributes::ReadNone);
3041 void setDoesNotAccessMemory() {
3042 addAttribute(AttrListPtr::FunctionIndex,
3043 Attributes::get(getContext(), Attributes::ReadNone));
3046 /// @brief Determine if the call does not access or only reads memory.
3047 bool onlyReadsMemory() const {
3048 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
3050 void setOnlyReadsMemory() {
3051 addAttribute(AttrListPtr::FunctionIndex,
3052 Attributes::get(getContext(), Attributes::ReadOnly));
3055 /// @brief Determine if the call cannot return.
3056 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
3057 void setDoesNotReturn() {
3058 addAttribute(AttrListPtr::FunctionIndex,
3059 Attributes::get(getContext(), Attributes::NoReturn));
3062 /// @brief Determine if the call cannot unwind.
3063 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
3064 void setDoesNotThrow() {
3065 addAttribute(AttrListPtr::FunctionIndex,
3066 Attributes::get(getContext(), Attributes::NoUnwind));
3069 /// @brief Determine if the call returns a structure through first
3070 /// pointer argument.
3071 bool hasStructRetAttr() const {
3072 // Be friendly and also check the callee.
3073 return paramHasAttr(1, Attributes::StructRet);
3076 /// @brief Determine if any call argument is an aggregate passed by value.
3077 bool hasByValArgument() const {
3078 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3079 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
3084 /// getCalledFunction - Return the function called, or null if this is an
3085 /// indirect function invocation.
3087 Function *getCalledFunction() const {
3088 return dyn_cast<Function>(Op<-3>());
3091 /// getCalledValue - Get a pointer to the function that is invoked by this
3093 const Value *getCalledValue() const { return Op<-3>(); }
3094 Value *getCalledValue() { return Op<-3>(); }
3096 /// setCalledFunction - Set the function called.
3097 void setCalledFunction(Value* Fn) {
3101 // get*Dest - Return the destination basic blocks...
3102 BasicBlock *getNormalDest() const {
3103 return cast<BasicBlock>(Op<-2>());
3105 BasicBlock *getUnwindDest() const {
3106 return cast<BasicBlock>(Op<-1>());
3108 void setNormalDest(BasicBlock *B) {
3109 Op<-2>() = reinterpret_cast<Value*>(B);
3111 void setUnwindDest(BasicBlock *B) {
3112 Op<-1>() = reinterpret_cast<Value*>(B);
3115 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3116 /// block (the unwind destination).
3117 LandingPadInst *getLandingPadInst() const;
3119 BasicBlock *getSuccessor(unsigned i) const {
3120 assert(i < 2 && "Successor # out of range for invoke!");
3121 return i == 0 ? getNormalDest() : getUnwindDest();
3124 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3125 assert(idx < 2 && "Successor # out of range for invoke!");
3126 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3129 unsigned getNumSuccessors() const { return 2; }
3131 // Methods for support type inquiry through isa, cast, and dyn_cast:
3132 static inline bool classof(const Instruction *I) {
3133 return (I->getOpcode() == Instruction::Invoke);
3135 static inline bool classof(const Value *V) {
3136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3140 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3141 virtual unsigned getNumSuccessorsV() const;
3142 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3144 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3145 // method so that subclasses cannot accidentally use it.
3146 void setInstructionSubclassData(unsigned short D) {
3147 Instruction::setInstructionSubclassData(D);
3152 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3155 InvokeInst::InvokeInst(Value *Func,
3156 BasicBlock *IfNormal, BasicBlock *IfException,
3157 ArrayRef<Value *> Args, unsigned Values,
3158 const Twine &NameStr, Instruction *InsertBefore)
3159 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3160 ->getElementType())->getReturnType(),
3161 Instruction::Invoke,
3162 OperandTraits<InvokeInst>::op_end(this) - Values,
3163 Values, InsertBefore) {
3164 init(Func, IfNormal, IfException, Args, NameStr);
3166 InvokeInst::InvokeInst(Value *Func,
3167 BasicBlock *IfNormal, BasicBlock *IfException,
3168 ArrayRef<Value *> Args, unsigned Values,
3169 const Twine &NameStr, BasicBlock *InsertAtEnd)
3170 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3171 ->getElementType())->getReturnType(),
3172 Instruction::Invoke,
3173 OperandTraits<InvokeInst>::op_end(this) - Values,
3174 Values, InsertAtEnd) {
3175 init(Func, IfNormal, IfException, Args, NameStr);
3178 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3180 //===----------------------------------------------------------------------===//
3182 //===----------------------------------------------------------------------===//
3184 //===---------------------------------------------------------------------------
3185 /// ResumeInst - Resume the propagation of an exception.
3187 class ResumeInst : public TerminatorInst {
3188 ResumeInst(const ResumeInst &RI);
3190 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3191 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3193 virtual ResumeInst *clone_impl() const;
3195 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3196 return new(1) ResumeInst(Exn, InsertBefore);
3198 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3199 return new(1) ResumeInst(Exn, InsertAtEnd);
3202 /// Provide fast operand accessors
3203 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3205 /// Convenience accessor.
3206 Value *getValue() const { return Op<0>(); }
3208 unsigned getNumSuccessors() const { return 0; }
3210 // Methods for support type inquiry through isa, cast, and dyn_cast:
3211 static inline bool classof(const Instruction *I) {
3212 return I->getOpcode() == Instruction::Resume;
3214 static inline bool classof(const Value *V) {
3215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3218 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3219 virtual unsigned getNumSuccessorsV() const;
3220 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3224 struct OperandTraits<ResumeInst> :
3225 public FixedNumOperandTraits<ResumeInst, 1> {
3228 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3230 //===----------------------------------------------------------------------===//
3231 // UnreachableInst Class
3232 //===----------------------------------------------------------------------===//
3234 //===---------------------------------------------------------------------------
3235 /// UnreachableInst - This function has undefined behavior. In particular, the
3236 /// presence of this instruction indicates some higher level knowledge that the
3237 /// end of the block cannot be reached.
3239 class UnreachableInst : public TerminatorInst {
3240 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3242 virtual UnreachableInst *clone_impl() const;
3245 // allocate space for exactly zero operands
3246 void *operator new(size_t s) {
3247 return User::operator new(s, 0);
3249 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3250 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3252 unsigned getNumSuccessors() const { return 0; }
3254 // Methods for support type inquiry through isa, cast, and dyn_cast:
3255 static inline bool classof(const Instruction *I) {
3256 return I->getOpcode() == Instruction::Unreachable;
3258 static inline bool classof(const Value *V) {
3259 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3262 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3263 virtual unsigned getNumSuccessorsV() const;
3264 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3267 //===----------------------------------------------------------------------===//
3269 //===----------------------------------------------------------------------===//
3271 /// @brief This class represents a truncation of integer types.
3272 class TruncInst : public CastInst {
3274 /// @brief Clone an identical TruncInst
3275 virtual TruncInst *clone_impl() const;
3278 /// @brief Constructor with insert-before-instruction semantics
3280 Value *S, ///< The value to be truncated
3281 Type *Ty, ///< The (smaller) type to truncate to
3282 const Twine &NameStr = "", ///< A name for the new instruction
3283 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3286 /// @brief Constructor with insert-at-end-of-block semantics
3288 Value *S, ///< The value to be truncated
3289 Type *Ty, ///< The (smaller) type to truncate to
3290 const Twine &NameStr, ///< A name for the new instruction
3291 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3294 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3295 static inline bool classof(const Instruction *I) {
3296 return I->getOpcode() == Trunc;
3298 static inline bool classof(const Value *V) {
3299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3303 //===----------------------------------------------------------------------===//
3305 //===----------------------------------------------------------------------===//
3307 /// @brief This class represents zero extension of integer types.
3308 class ZExtInst : public CastInst {
3310 /// @brief Clone an identical ZExtInst
3311 virtual ZExtInst *clone_impl() const;
3314 /// @brief Constructor with insert-before-instruction semantics
3316 Value *S, ///< The value to be zero extended
3317 Type *Ty, ///< The type to zero extend to
3318 const Twine &NameStr = "", ///< A name for the new instruction
3319 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3322 /// @brief Constructor with insert-at-end semantics.
3324 Value *S, ///< The value to be zero extended
3325 Type *Ty, ///< The type to zero extend to
3326 const Twine &NameStr, ///< A name for the new instruction
3327 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3330 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3331 static inline bool classof(const Instruction *I) {
3332 return I->getOpcode() == ZExt;
3334 static inline bool classof(const Value *V) {
3335 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3339 //===----------------------------------------------------------------------===//
3341 //===----------------------------------------------------------------------===//
3343 /// @brief This class represents a sign extension of integer types.
3344 class SExtInst : public CastInst {
3346 /// @brief Clone an identical SExtInst
3347 virtual SExtInst *clone_impl() const;
3350 /// @brief Constructor with insert-before-instruction semantics
3352 Value *S, ///< The value to be sign extended
3353 Type *Ty, ///< The type to sign extend to
3354 const Twine &NameStr = "", ///< A name for the new instruction
3355 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3358 /// @brief Constructor with insert-at-end-of-block semantics
3360 Value *S, ///< The value to be sign extended
3361 Type *Ty, ///< The type to sign extend to
3362 const Twine &NameStr, ///< A name for the new instruction
3363 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3366 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3367 static inline bool classof(const Instruction *I) {
3368 return I->getOpcode() == SExt;
3370 static inline bool classof(const Value *V) {
3371 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3375 //===----------------------------------------------------------------------===//
3376 // FPTruncInst Class
3377 //===----------------------------------------------------------------------===//
3379 /// @brief This class represents a truncation of floating point types.
3380 class FPTruncInst : public CastInst {
3382 /// @brief Clone an identical FPTruncInst
3383 virtual FPTruncInst *clone_impl() const;
3386 /// @brief Constructor with insert-before-instruction semantics
3388 Value *S, ///< The value to be truncated
3389 Type *Ty, ///< The type to truncate to
3390 const Twine &NameStr = "", ///< A name for the new instruction
3391 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3394 /// @brief Constructor with insert-before-instruction semantics
3396 Value *S, ///< The value to be truncated
3397 Type *Ty, ///< The type to truncate to
3398 const Twine &NameStr, ///< A name for the new instruction
3399 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3402 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3403 static inline bool classof(const Instruction *I) {
3404 return I->getOpcode() == FPTrunc;
3406 static inline bool classof(const Value *V) {
3407 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3411 //===----------------------------------------------------------------------===//
3413 //===----------------------------------------------------------------------===//
3415 /// @brief This class represents an extension of floating point types.
3416 class FPExtInst : public CastInst {
3418 /// @brief Clone an identical FPExtInst
3419 virtual FPExtInst *clone_impl() const;
3422 /// @brief Constructor with insert-before-instruction semantics
3424 Value *S, ///< The value to be extended
3425 Type *Ty, ///< The type to extend to
3426 const Twine &NameStr = "", ///< A name for the new instruction
3427 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3430 /// @brief Constructor with insert-at-end-of-block semantics
3432 Value *S, ///< The value to be extended
3433 Type *Ty, ///< The type to extend to
3434 const Twine &NameStr, ///< A name for the new instruction
3435 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3438 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3439 static inline bool classof(const Instruction *I) {
3440 return I->getOpcode() == FPExt;
3442 static inline bool classof(const Value *V) {
3443 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3447 //===----------------------------------------------------------------------===//
3449 //===----------------------------------------------------------------------===//
3451 /// @brief This class represents a cast unsigned integer to floating point.
3452 class UIToFPInst : public CastInst {
3454 /// @brief Clone an identical UIToFPInst
3455 virtual UIToFPInst *clone_impl() const;
3458 /// @brief Constructor with insert-before-instruction semantics
3460 Value *S, ///< The value to be converted
3461 Type *Ty, ///< The type to convert to
3462 const Twine &NameStr = "", ///< A name for the new instruction
3463 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3466 /// @brief Constructor with insert-at-end-of-block semantics
3468 Value *S, ///< The value to be converted
3469 Type *Ty, ///< The type to convert to
3470 const Twine &NameStr, ///< A name for the new instruction
3471 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3474 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3475 static inline bool classof(const Instruction *I) {
3476 return I->getOpcode() == UIToFP;
3478 static inline bool classof(const Value *V) {
3479 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3483 //===----------------------------------------------------------------------===//
3485 //===----------------------------------------------------------------------===//
3487 /// @brief This class represents a cast from signed integer to floating point.
3488 class SIToFPInst : public CastInst {
3490 /// @brief Clone an identical SIToFPInst
3491 virtual SIToFPInst *clone_impl() const;
3494 /// @brief Constructor with insert-before-instruction semantics
3496 Value *S, ///< The value to be converted
3497 Type *Ty, ///< The type to convert to
3498 const Twine &NameStr = "", ///< A name for the new instruction
3499 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3502 /// @brief Constructor with insert-at-end-of-block semantics
3504 Value *S, ///< The value to be converted
3505 Type *Ty, ///< The type to convert to
3506 const Twine &NameStr, ///< A name for the new instruction
3507 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3510 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3511 static inline bool classof(const Instruction *I) {
3512 return I->getOpcode() == SIToFP;
3514 static inline bool classof(const Value *V) {
3515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3519 //===----------------------------------------------------------------------===//
3521 //===----------------------------------------------------------------------===//
3523 /// @brief This class represents a cast from floating point to unsigned integer
3524 class FPToUIInst : public CastInst {
3526 /// @brief Clone an identical FPToUIInst
3527 virtual FPToUIInst *clone_impl() const;
3530 /// @brief Constructor with insert-before-instruction semantics
3532 Value *S, ///< The value to be converted
3533 Type *Ty, ///< The type to convert to
3534 const Twine &NameStr = "", ///< A name for the new instruction
3535 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3538 /// @brief Constructor with insert-at-end-of-block semantics
3540 Value *S, ///< The value to be converted
3541 Type *Ty, ///< The type to convert to
3542 const Twine &NameStr, ///< A name for the new instruction
3543 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3546 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3547 static inline bool classof(const Instruction *I) {
3548 return I->getOpcode() == FPToUI;
3550 static inline bool classof(const Value *V) {
3551 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3555 //===----------------------------------------------------------------------===//
3557 //===----------------------------------------------------------------------===//
3559 /// @brief This class represents a cast from floating point to signed integer.
3560 class FPToSIInst : public CastInst {
3562 /// @brief Clone an identical FPToSIInst
3563 virtual FPToSIInst *clone_impl() const;
3566 /// @brief Constructor with insert-before-instruction semantics
3568 Value *S, ///< The value to be converted
3569 Type *Ty, ///< The type to convert to
3570 const Twine &NameStr = "", ///< A name for the new instruction
3571 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3574 /// @brief Constructor with insert-at-end-of-block semantics
3576 Value *S, ///< The value to be converted
3577 Type *Ty, ///< The type to convert to
3578 const Twine &NameStr, ///< A name for the new instruction
3579 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3582 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3583 static inline bool classof(const Instruction *I) {
3584 return I->getOpcode() == FPToSI;
3586 static inline bool classof(const Value *V) {
3587 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3591 //===----------------------------------------------------------------------===//
3592 // IntToPtrInst Class
3593 //===----------------------------------------------------------------------===//
3595 /// @brief This class represents a cast from an integer to a pointer.
3596 class IntToPtrInst : public CastInst {
3598 /// @brief Constructor with insert-before-instruction semantics
3600 Value *S, ///< The value to be converted
3601 Type *Ty, ///< The type to convert to
3602 const Twine &NameStr = "", ///< A name for the new instruction
3603 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3606 /// @brief Constructor with insert-at-end-of-block semantics
3608 Value *S, ///< The value to be converted
3609 Type *Ty, ///< The type to convert to
3610 const Twine &NameStr, ///< A name for the new instruction
3611 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3614 /// @brief Clone an identical IntToPtrInst
3615 virtual IntToPtrInst *clone_impl() const;
3617 /// @brief return the address space of the pointer.
3618 unsigned getAddressSpace() const {
3619 if (getType()->isPointerTy())
3620 return cast<PointerType>(getType())->getAddressSpace();
3621 if (getType()->isVectorTy() &&
3622 cast<VectorType>(getType())->getElementType()->isPointerTy())
3623 return cast<PointerType>(
3624 cast<VectorType>(getType())->getElementType())
3625 ->getAddressSpace();
3626 llvm_unreachable("Must be a pointer or a vector of pointers.");
3630 // Methods for support type inquiry through isa, cast, and dyn_cast:
3631 static inline bool classof(const Instruction *I) {
3632 return I->getOpcode() == IntToPtr;
3634 static inline bool classof(const Value *V) {
3635 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3639 //===----------------------------------------------------------------------===//
3640 // PtrToIntInst Class
3641 //===----------------------------------------------------------------------===//
3643 /// @brief This class represents a cast from a pointer to an integer
3644 class PtrToIntInst : public CastInst {
3646 /// @brief Clone an identical PtrToIntInst
3647 virtual PtrToIntInst *clone_impl() const;
3650 /// @brief Constructor with insert-before-instruction semantics
3652 Value *S, ///< The value to be converted
3653 Type *Ty, ///< The type to convert to
3654 const Twine &NameStr = "", ///< A name for the new instruction
3655 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3658 /// @brief Constructor with insert-at-end-of-block semantics
3660 Value *S, ///< The value to be converted
3661 Type *Ty, ///< The type to convert to
3662 const Twine &NameStr, ///< A name for the new instruction
3663 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3666 /// @brief return the address space of the pointer.
3667 unsigned getPointerAddressSpace() const {
3668 Type *Ty = getOperand(0)->getType();
3669 if (Ty->isPointerTy())
3670 return cast<PointerType>(Ty)->getAddressSpace();
3671 if (Ty->isVectorTy()
3672 && cast<VectorType>(Ty)->getElementType()->isPointerTy())
3673 return cast<PointerType>(
3674 cast<VectorType>(Ty)->getElementType())
3675 ->getAddressSpace();
3676 llvm_unreachable("Must be a pointer or a vector of pointers.");
3680 // Methods for support type inquiry through isa, cast, and dyn_cast:
3681 static inline bool classof(const Instruction *I) {
3682 return I->getOpcode() == PtrToInt;
3684 static inline bool classof(const Value *V) {
3685 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3689 //===----------------------------------------------------------------------===//
3690 // BitCastInst Class
3691 //===----------------------------------------------------------------------===//
3693 /// @brief This class represents a no-op cast from one type to another.
3694 class BitCastInst : public CastInst {
3696 /// @brief Clone an identical BitCastInst
3697 virtual BitCastInst *clone_impl() const;
3700 /// @brief Constructor with insert-before-instruction semantics
3702 Value *S, ///< The value to be casted
3703 Type *Ty, ///< The type to casted to
3704 const Twine &NameStr = "", ///< A name for the new instruction
3705 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3708 /// @brief Constructor with insert-at-end-of-block semantics
3710 Value *S, ///< The value to be casted
3711 Type *Ty, ///< The type to casted to
3712 const Twine &NameStr, ///< A name for the new instruction
3713 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3716 // Methods for support type inquiry through isa, cast, and dyn_cast:
3717 static inline bool classof(const Instruction *I) {
3718 return I->getOpcode() == BitCast;
3720 static inline bool classof(const Value *V) {
3721 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3725 } // End llvm namespace