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() {
1285 B.addAttribute(Attributes::NoInline);
1286 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1289 /// @brief Return true if the call can return twice
1290 bool canReturnTwice() const {
1291 return hasFnAttr(Attributes::ReturnsTwice);
1293 void setCanReturnTwice() {
1295 B.addAttribute(Attributes::ReturnsTwice);
1296 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1299 /// @brief Determine if the call does not access memory.
1300 bool doesNotAccessMemory() const {
1301 return hasFnAttr(Attributes::ReadNone);
1303 void setDoesNotAccessMemory() {
1305 B.addAttribute(Attributes::ReadNone);
1306 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1309 /// @brief Determine if the call does not access or only reads memory.
1310 bool onlyReadsMemory() const {
1311 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
1313 void setOnlyReadsMemory() {
1315 B.addAttribute(Attributes::ReadOnly);
1316 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1319 /// @brief Determine if the call cannot return.
1320 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
1321 void setDoesNotReturn() {
1323 B.addAttribute(Attributes::NoReturn);
1324 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1327 /// @brief Determine if the call cannot unwind.
1328 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
1329 void setDoesNotThrow() {
1331 B.addAttribute(Attributes::NoUnwind);
1332 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
1335 /// @brief Determine if the call returns a structure through first
1336 /// pointer argument.
1337 bool hasStructRetAttr() const {
1338 // Be friendly and also check the callee.
1339 return paramHasAttr(1, Attributes::StructRet);
1342 /// @brief Determine if any call argument is an aggregate passed by value.
1343 bool hasByValArgument() const {
1344 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1345 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
1350 /// getCalledFunction - Return the function called, or null if this is an
1351 /// indirect function invocation.
1353 Function *getCalledFunction() const {
1354 return dyn_cast<Function>(Op<-1>());
1357 /// getCalledValue - Get a pointer to the function that is invoked by this
1359 const Value *getCalledValue() const { return Op<-1>(); }
1360 Value *getCalledValue() { return Op<-1>(); }
1362 /// setCalledFunction - Set the function called.
1363 void setCalledFunction(Value* Fn) {
1367 /// isInlineAsm - Check if this call is an inline asm statement.
1368 bool isInlineAsm() const {
1369 return isa<InlineAsm>(Op<-1>());
1372 // Methods for support type inquiry through isa, cast, and dyn_cast:
1373 static inline bool classof(const Instruction *I) {
1374 return I->getOpcode() == Instruction::Call;
1376 static inline bool classof(const Value *V) {
1377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1380 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1381 // method so that subclasses cannot accidentally use it.
1382 void setInstructionSubclassData(unsigned short D) {
1383 Instruction::setInstructionSubclassData(D);
1388 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1391 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1392 const Twine &NameStr, BasicBlock *InsertAtEnd)
1393 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1394 ->getElementType())->getReturnType(),
1396 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1397 unsigned(Args.size() + 1), InsertAtEnd) {
1398 init(Func, Args, NameStr);
1401 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1402 const Twine &NameStr, Instruction *InsertBefore)
1403 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1404 ->getElementType())->getReturnType(),
1406 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1407 unsigned(Args.size() + 1), InsertBefore) {
1408 init(Func, Args, NameStr);
1412 // Note: if you get compile errors about private methods then
1413 // please update your code to use the high-level operand
1414 // interfaces. See line 943 above.
1415 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1417 //===----------------------------------------------------------------------===//
1419 //===----------------------------------------------------------------------===//
1421 /// SelectInst - This class represents the LLVM 'select' instruction.
1423 class SelectInst : public Instruction {
1424 void init(Value *C, Value *S1, Value *S2) {
1425 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1431 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1432 Instruction *InsertBefore)
1433 : Instruction(S1->getType(), Instruction::Select,
1434 &Op<0>(), 3, InsertBefore) {
1438 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1439 BasicBlock *InsertAtEnd)
1440 : Instruction(S1->getType(), Instruction::Select,
1441 &Op<0>(), 3, InsertAtEnd) {
1446 virtual SelectInst *clone_impl() const;
1448 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1449 const Twine &NameStr = "",
1450 Instruction *InsertBefore = 0) {
1451 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1453 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1454 const Twine &NameStr,
1455 BasicBlock *InsertAtEnd) {
1456 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1459 const Value *getCondition() const { return Op<0>(); }
1460 const Value *getTrueValue() const { return Op<1>(); }
1461 const Value *getFalseValue() const { return Op<2>(); }
1462 Value *getCondition() { return Op<0>(); }
1463 Value *getTrueValue() { return Op<1>(); }
1464 Value *getFalseValue() { return Op<2>(); }
1466 /// areInvalidOperands - Return a string if the specified operands are invalid
1467 /// for a select operation, otherwise return null.
1468 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1470 /// Transparently provide more efficient getOperand methods.
1471 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1473 OtherOps getOpcode() const {
1474 return static_cast<OtherOps>(Instruction::getOpcode());
1477 // Methods for support type inquiry through isa, cast, and dyn_cast:
1478 static inline bool classof(const Instruction *I) {
1479 return I->getOpcode() == Instruction::Select;
1481 static inline bool classof(const Value *V) {
1482 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1487 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1490 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1492 //===----------------------------------------------------------------------===//
1494 //===----------------------------------------------------------------------===//
1496 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1497 /// an argument of the specified type given a va_list and increments that list
1499 class VAArgInst : public UnaryInstruction {
1501 virtual VAArgInst *clone_impl() const;
1504 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1505 Instruction *InsertBefore = 0)
1506 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1509 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1510 BasicBlock *InsertAtEnd)
1511 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1515 Value *getPointerOperand() { return getOperand(0); }
1516 const Value *getPointerOperand() const { return getOperand(0); }
1517 static unsigned getPointerOperandIndex() { return 0U; }
1519 // Methods for support type inquiry through isa, cast, and dyn_cast:
1520 static inline bool classof(const Instruction *I) {
1521 return I->getOpcode() == VAArg;
1523 static inline bool classof(const Value *V) {
1524 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1528 //===----------------------------------------------------------------------===//
1529 // ExtractElementInst Class
1530 //===----------------------------------------------------------------------===//
1532 /// ExtractElementInst - This instruction extracts a single (scalar)
1533 /// element from a VectorType value
1535 class ExtractElementInst : public Instruction {
1536 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1537 Instruction *InsertBefore = 0);
1538 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1539 BasicBlock *InsertAtEnd);
1541 virtual ExtractElementInst *clone_impl() const;
1544 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1545 const Twine &NameStr = "",
1546 Instruction *InsertBefore = 0) {
1547 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1549 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1550 const Twine &NameStr,
1551 BasicBlock *InsertAtEnd) {
1552 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1555 /// isValidOperands - Return true if an extractelement instruction can be
1556 /// formed with the specified operands.
1557 static bool isValidOperands(const Value *Vec, const Value *Idx);
1559 Value *getVectorOperand() { return Op<0>(); }
1560 Value *getIndexOperand() { return Op<1>(); }
1561 const Value *getVectorOperand() const { return Op<0>(); }
1562 const Value *getIndexOperand() const { return Op<1>(); }
1564 VectorType *getVectorOperandType() const {
1565 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1569 /// Transparently provide more efficient getOperand methods.
1570 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1572 // Methods for support type inquiry through isa, cast, and dyn_cast:
1573 static inline bool classof(const Instruction *I) {
1574 return I->getOpcode() == Instruction::ExtractElement;
1576 static inline bool classof(const Value *V) {
1577 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1582 struct OperandTraits<ExtractElementInst> :
1583 public FixedNumOperandTraits<ExtractElementInst, 2> {
1586 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1588 //===----------------------------------------------------------------------===//
1589 // InsertElementInst Class
1590 //===----------------------------------------------------------------------===//
1592 /// InsertElementInst - This instruction inserts a single (scalar)
1593 /// element into a VectorType value
1595 class InsertElementInst : public Instruction {
1596 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1597 const Twine &NameStr = "",
1598 Instruction *InsertBefore = 0);
1599 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1600 const Twine &NameStr, BasicBlock *InsertAtEnd);
1602 virtual InsertElementInst *clone_impl() const;
1605 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1606 const Twine &NameStr = "",
1607 Instruction *InsertBefore = 0) {
1608 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1610 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1611 const Twine &NameStr,
1612 BasicBlock *InsertAtEnd) {
1613 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1616 /// isValidOperands - Return true if an insertelement instruction can be
1617 /// formed with the specified operands.
1618 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1621 /// getType - Overload to return most specific vector type.
1623 VectorType *getType() const {
1624 return reinterpret_cast<VectorType*>(Instruction::getType());
1627 /// Transparently provide more efficient getOperand methods.
1628 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1630 // Methods for support type inquiry through isa, cast, and dyn_cast:
1631 static inline bool classof(const Instruction *I) {
1632 return I->getOpcode() == Instruction::InsertElement;
1634 static inline bool classof(const Value *V) {
1635 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1640 struct OperandTraits<InsertElementInst> :
1641 public FixedNumOperandTraits<InsertElementInst, 3> {
1644 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1646 //===----------------------------------------------------------------------===//
1647 // ShuffleVectorInst Class
1648 //===----------------------------------------------------------------------===//
1650 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1653 class ShuffleVectorInst : public Instruction {
1655 virtual ShuffleVectorInst *clone_impl() const;
1658 // allocate space for exactly three operands
1659 void *operator new(size_t s) {
1660 return User::operator new(s, 3);
1662 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1663 const Twine &NameStr = "",
1664 Instruction *InsertBefor = 0);
1665 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1666 const Twine &NameStr, BasicBlock *InsertAtEnd);
1668 /// isValidOperands - Return true if a shufflevector instruction can be
1669 /// formed with the specified operands.
1670 static bool isValidOperands(const Value *V1, const Value *V2,
1673 /// getType - Overload to return most specific vector type.
1675 VectorType *getType() const {
1676 return reinterpret_cast<VectorType*>(Instruction::getType());
1679 /// Transparently provide more efficient getOperand methods.
1680 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1682 Constant *getMask() const {
1683 return reinterpret_cast<Constant*>(getOperand(2));
1686 /// getMaskValue - Return the index from the shuffle mask for the specified
1687 /// output result. This is either -1 if the element is undef or a number less
1688 /// than 2*numelements.
1689 static int getMaskValue(Constant *Mask, unsigned i);
1691 int getMaskValue(unsigned i) const {
1692 return getMaskValue(getMask(), i);
1695 /// getShuffleMask - Return the full mask for this instruction, where each
1696 /// element is the element number and undef's are returned as -1.
1697 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1699 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1700 return getShuffleMask(getMask(), Result);
1703 SmallVector<int, 16> getShuffleMask() const {
1704 SmallVector<int, 16> Mask;
1705 getShuffleMask(Mask);
1710 // Methods for support type inquiry through isa, cast, and dyn_cast:
1711 static inline bool classof(const Instruction *I) {
1712 return I->getOpcode() == Instruction::ShuffleVector;
1714 static inline bool classof(const Value *V) {
1715 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1720 struct OperandTraits<ShuffleVectorInst> :
1721 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1724 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1726 //===----------------------------------------------------------------------===//
1727 // ExtractValueInst Class
1728 //===----------------------------------------------------------------------===//
1730 /// ExtractValueInst - This instruction extracts a struct member or array
1731 /// element value from an aggregate value.
1733 class ExtractValueInst : public UnaryInstruction {
1734 SmallVector<unsigned, 4> Indices;
1736 ExtractValueInst(const ExtractValueInst &EVI);
1737 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1739 /// Constructors - Create a extractvalue instruction with a base aggregate
1740 /// value and a list of indices. The first ctor can optionally insert before
1741 /// an existing instruction, the second appends the new instruction to the
1742 /// specified BasicBlock.
1743 inline ExtractValueInst(Value *Agg,
1744 ArrayRef<unsigned> Idxs,
1745 const Twine &NameStr,
1746 Instruction *InsertBefore);
1747 inline ExtractValueInst(Value *Agg,
1748 ArrayRef<unsigned> Idxs,
1749 const Twine &NameStr, BasicBlock *InsertAtEnd);
1751 // allocate space for exactly one operand
1752 void *operator new(size_t s) {
1753 return User::operator new(s, 1);
1756 virtual ExtractValueInst *clone_impl() const;
1759 static ExtractValueInst *Create(Value *Agg,
1760 ArrayRef<unsigned> Idxs,
1761 const Twine &NameStr = "",
1762 Instruction *InsertBefore = 0) {
1764 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1766 static ExtractValueInst *Create(Value *Agg,
1767 ArrayRef<unsigned> Idxs,
1768 const Twine &NameStr,
1769 BasicBlock *InsertAtEnd) {
1770 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1773 /// getIndexedType - Returns the type of the element that would be extracted
1774 /// with an extractvalue instruction with the specified parameters.
1776 /// Null is returned if the indices are invalid for the specified type.
1777 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1779 typedef const unsigned* idx_iterator;
1780 inline idx_iterator idx_begin() const { return Indices.begin(); }
1781 inline idx_iterator idx_end() const { return Indices.end(); }
1783 Value *getAggregateOperand() {
1784 return getOperand(0);
1786 const Value *getAggregateOperand() const {
1787 return getOperand(0);
1789 static unsigned getAggregateOperandIndex() {
1790 return 0U; // get index for modifying correct operand
1793 ArrayRef<unsigned> getIndices() const {
1797 unsigned getNumIndices() const {
1798 return (unsigned)Indices.size();
1801 bool hasIndices() const {
1805 // Methods for support type inquiry through isa, cast, and dyn_cast:
1806 static inline bool classof(const Instruction *I) {
1807 return I->getOpcode() == Instruction::ExtractValue;
1809 static inline bool classof(const Value *V) {
1810 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1814 ExtractValueInst::ExtractValueInst(Value *Agg,
1815 ArrayRef<unsigned> Idxs,
1816 const Twine &NameStr,
1817 Instruction *InsertBefore)
1818 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1819 ExtractValue, Agg, InsertBefore) {
1820 init(Idxs, NameStr);
1822 ExtractValueInst::ExtractValueInst(Value *Agg,
1823 ArrayRef<unsigned> Idxs,
1824 const Twine &NameStr,
1825 BasicBlock *InsertAtEnd)
1826 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1827 ExtractValue, Agg, InsertAtEnd) {
1828 init(Idxs, NameStr);
1832 //===----------------------------------------------------------------------===//
1833 // InsertValueInst Class
1834 //===----------------------------------------------------------------------===//
1836 /// InsertValueInst - This instruction inserts a struct field of array element
1837 /// value into an aggregate value.
1839 class InsertValueInst : public Instruction {
1840 SmallVector<unsigned, 4> Indices;
1842 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1843 InsertValueInst(const InsertValueInst &IVI);
1844 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1845 const Twine &NameStr);
1847 /// Constructors - Create a insertvalue instruction with a base aggregate
1848 /// value, a value to insert, and a list of indices. The first ctor can
1849 /// optionally insert before an existing instruction, the second appends
1850 /// the new instruction to the specified BasicBlock.
1851 inline InsertValueInst(Value *Agg, Value *Val,
1852 ArrayRef<unsigned> Idxs,
1853 const Twine &NameStr,
1854 Instruction *InsertBefore);
1855 inline InsertValueInst(Value *Agg, Value *Val,
1856 ArrayRef<unsigned> Idxs,
1857 const Twine &NameStr, BasicBlock *InsertAtEnd);
1859 /// Constructors - These two constructors are convenience methods because one
1860 /// and two index insertvalue instructions are so common.
1861 InsertValueInst(Value *Agg, Value *Val,
1862 unsigned Idx, const Twine &NameStr = "",
1863 Instruction *InsertBefore = 0);
1864 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1865 const Twine &NameStr, BasicBlock *InsertAtEnd);
1867 virtual InsertValueInst *clone_impl() const;
1869 // allocate space for exactly two operands
1870 void *operator new(size_t s) {
1871 return User::operator new(s, 2);
1874 static InsertValueInst *Create(Value *Agg, Value *Val,
1875 ArrayRef<unsigned> Idxs,
1876 const Twine &NameStr = "",
1877 Instruction *InsertBefore = 0) {
1878 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1880 static InsertValueInst *Create(Value *Agg, Value *Val,
1881 ArrayRef<unsigned> Idxs,
1882 const Twine &NameStr,
1883 BasicBlock *InsertAtEnd) {
1884 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1887 /// Transparently provide more efficient getOperand methods.
1888 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1890 typedef const unsigned* idx_iterator;
1891 inline idx_iterator idx_begin() const { return Indices.begin(); }
1892 inline idx_iterator idx_end() const { return Indices.end(); }
1894 Value *getAggregateOperand() {
1895 return getOperand(0);
1897 const Value *getAggregateOperand() const {
1898 return getOperand(0);
1900 static unsigned getAggregateOperandIndex() {
1901 return 0U; // get index for modifying correct operand
1904 Value *getInsertedValueOperand() {
1905 return getOperand(1);
1907 const Value *getInsertedValueOperand() const {
1908 return getOperand(1);
1910 static unsigned getInsertedValueOperandIndex() {
1911 return 1U; // get index for modifying correct operand
1914 ArrayRef<unsigned> getIndices() const {
1918 unsigned getNumIndices() const {
1919 return (unsigned)Indices.size();
1922 bool hasIndices() const {
1926 // Methods for support type inquiry through isa, cast, and dyn_cast:
1927 static inline bool classof(const Instruction *I) {
1928 return I->getOpcode() == Instruction::InsertValue;
1930 static inline bool classof(const Value *V) {
1931 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1936 struct OperandTraits<InsertValueInst> :
1937 public FixedNumOperandTraits<InsertValueInst, 2> {
1940 InsertValueInst::InsertValueInst(Value *Agg,
1942 ArrayRef<unsigned> Idxs,
1943 const Twine &NameStr,
1944 Instruction *InsertBefore)
1945 : Instruction(Agg->getType(), InsertValue,
1946 OperandTraits<InsertValueInst>::op_begin(this),
1948 init(Agg, Val, Idxs, NameStr);
1950 InsertValueInst::InsertValueInst(Value *Agg,
1952 ArrayRef<unsigned> Idxs,
1953 const Twine &NameStr,
1954 BasicBlock *InsertAtEnd)
1955 : Instruction(Agg->getType(), InsertValue,
1956 OperandTraits<InsertValueInst>::op_begin(this),
1958 init(Agg, Val, Idxs, NameStr);
1961 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1963 //===----------------------------------------------------------------------===//
1965 //===----------------------------------------------------------------------===//
1967 // PHINode - The PHINode class is used to represent the magical mystical PHI
1968 // node, that can not exist in nature, but can be synthesized in a computer
1969 // scientist's overactive imagination.
1971 class PHINode : public Instruction {
1972 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1973 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1974 /// the number actually in use.
1975 unsigned ReservedSpace;
1976 PHINode(const PHINode &PN);
1977 // allocate space for exactly zero operands
1978 void *operator new(size_t s) {
1979 return User::operator new(s, 0);
1981 explicit PHINode(Type *Ty, unsigned NumReservedValues,
1982 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1983 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1984 ReservedSpace(NumReservedValues) {
1986 OperandList = allocHungoffUses(ReservedSpace);
1989 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1990 BasicBlock *InsertAtEnd)
1991 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1992 ReservedSpace(NumReservedValues) {
1994 OperandList = allocHungoffUses(ReservedSpace);
1997 // allocHungoffUses - this is more complicated than the generic
1998 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1999 // values and pointers to the incoming blocks, all in one allocation.
2000 Use *allocHungoffUses(unsigned) const;
2002 virtual PHINode *clone_impl() const;
2004 /// Constructors - NumReservedValues is a hint for the number of incoming
2005 /// edges that this phi node will have (use 0 if you really have no idea).
2006 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2007 const Twine &NameStr = "",
2008 Instruction *InsertBefore = 0) {
2009 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2011 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2012 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2013 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2017 /// Provide fast operand accessors
2018 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2020 // Block iterator interface. This provides access to the list of incoming
2021 // basic blocks, which parallels the list of incoming values.
2023 typedef BasicBlock **block_iterator;
2024 typedef BasicBlock * const *const_block_iterator;
2026 block_iterator block_begin() {
2028 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2029 return reinterpret_cast<block_iterator>(ref + 1);
2032 const_block_iterator block_begin() const {
2033 const Use::UserRef *ref =
2034 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2035 return reinterpret_cast<const_block_iterator>(ref + 1);
2038 block_iterator block_end() {
2039 return block_begin() + getNumOperands();
2042 const_block_iterator block_end() const {
2043 return block_begin() + getNumOperands();
2046 /// getNumIncomingValues - Return the number of incoming edges
2048 unsigned getNumIncomingValues() const { return getNumOperands(); }
2050 /// getIncomingValue - Return incoming value number x
2052 Value *getIncomingValue(unsigned i) const {
2053 return getOperand(i);
2055 void setIncomingValue(unsigned i, Value *V) {
2058 static unsigned getOperandNumForIncomingValue(unsigned i) {
2061 static unsigned getIncomingValueNumForOperand(unsigned i) {
2065 /// getIncomingBlock - Return incoming basic block number @p i.
2067 BasicBlock *getIncomingBlock(unsigned i) const {
2068 return block_begin()[i];
2071 /// getIncomingBlock - Return incoming basic block corresponding
2072 /// to an operand of the PHI.
2074 BasicBlock *getIncomingBlock(const Use &U) const {
2075 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2076 return getIncomingBlock(unsigned(&U - op_begin()));
2079 /// getIncomingBlock - Return incoming basic block corresponding
2080 /// to value use iterator.
2082 template <typename U>
2083 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2084 return getIncomingBlock(I.getUse());
2087 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2088 block_begin()[i] = BB;
2091 /// addIncoming - Add an incoming value to the end of the PHI list
2093 void addIncoming(Value *V, BasicBlock *BB) {
2094 assert(V && "PHI node got a null value!");
2095 assert(BB && "PHI node got a null basic block!");
2096 assert(getType() == V->getType() &&
2097 "All operands to PHI node must be the same type as the PHI node!");
2098 if (NumOperands == ReservedSpace)
2099 growOperands(); // Get more space!
2100 // Initialize some new operands.
2102 setIncomingValue(NumOperands - 1, V);
2103 setIncomingBlock(NumOperands - 1, BB);
2106 /// removeIncomingValue - Remove an incoming value. This is useful if a
2107 /// predecessor basic block is deleted. The value removed is returned.
2109 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2110 /// is true), the PHI node is destroyed and any uses of it are replaced with
2111 /// dummy values. The only time there should be zero incoming values to a PHI
2112 /// node is when the block is dead, so this strategy is sound.
2114 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2116 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2117 int Idx = getBasicBlockIndex(BB);
2118 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2119 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2122 /// getBasicBlockIndex - Return the first index of the specified basic
2123 /// block in the value list for this PHI. Returns -1 if no instance.
2125 int getBasicBlockIndex(const BasicBlock *BB) const {
2126 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2127 if (block_begin()[i] == BB)
2132 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2133 int Idx = getBasicBlockIndex(BB);
2134 assert(Idx >= 0 && "Invalid basic block argument!");
2135 return getIncomingValue(Idx);
2138 /// hasConstantValue - If the specified PHI node always merges together the
2139 /// same value, return the value, otherwise return null.
2140 Value *hasConstantValue() const;
2142 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2143 static inline bool classof(const Instruction *I) {
2144 return I->getOpcode() == Instruction::PHI;
2146 static inline bool classof(const Value *V) {
2147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2150 void growOperands();
2154 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2157 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2159 //===----------------------------------------------------------------------===//
2160 // LandingPadInst Class
2161 //===----------------------------------------------------------------------===//
2163 //===---------------------------------------------------------------------------
2164 /// LandingPadInst - The landingpad instruction holds all of the information
2165 /// necessary to generate correct exception handling. The landingpad instruction
2166 /// cannot be moved from the top of a landing pad block, which itself is
2167 /// accessible only from the 'unwind' edge of an invoke. This uses the
2168 /// SubclassData field in Value to store whether or not the landingpad is a
2171 class LandingPadInst : public Instruction {
2172 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2173 /// the number actually in use.
2174 unsigned ReservedSpace;
2175 LandingPadInst(const LandingPadInst &LP);
2177 enum ClauseType { Catch, Filter };
2179 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2180 // Allocate space for exactly zero operands.
2181 void *operator new(size_t s) {
2182 return User::operator new(s, 0);
2184 void growOperands(unsigned Size);
2185 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2187 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2188 unsigned NumReservedValues, const Twine &NameStr,
2189 Instruction *InsertBefore);
2190 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2191 unsigned NumReservedValues, const Twine &NameStr,
2192 BasicBlock *InsertAtEnd);
2194 virtual LandingPadInst *clone_impl() const;
2196 /// Constructors - NumReservedClauses is a hint for the number of incoming
2197 /// clauses that this landingpad will have (use 0 if you really have no idea).
2198 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2199 unsigned NumReservedClauses,
2200 const Twine &NameStr = "",
2201 Instruction *InsertBefore = 0);
2202 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2203 unsigned NumReservedClauses,
2204 const Twine &NameStr, BasicBlock *InsertAtEnd);
2207 /// Provide fast operand accessors
2208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2210 /// getPersonalityFn - Get the personality function associated with this
2212 Value *getPersonalityFn() const { return getOperand(0); }
2214 /// isCleanup - Return 'true' if this landingpad instruction is a
2215 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2216 /// doesn't catch the exception.
2217 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2219 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2220 void setCleanup(bool V) {
2221 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2225 /// addClause - Add a catch or filter clause to the landing pad.
2226 void addClause(Value *ClauseVal);
2228 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2229 /// to determine what type of clause this is.
2230 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2232 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2233 bool isCatch(unsigned Idx) const {
2234 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2237 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2238 bool isFilter(unsigned Idx) const {
2239 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2242 /// getNumClauses - Get the number of clauses for this landing pad.
2243 unsigned getNumClauses() const { return getNumOperands() - 1; }
2245 /// reserveClauses - Grow the size of the operand list to accommodate the new
2246 /// number of clauses.
2247 void reserveClauses(unsigned Size) { growOperands(Size); }
2249 // Methods for support type inquiry through isa, cast, and dyn_cast:
2250 static inline bool classof(const Instruction *I) {
2251 return I->getOpcode() == Instruction::LandingPad;
2253 static inline bool classof(const Value *V) {
2254 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2259 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2262 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2264 //===----------------------------------------------------------------------===//
2266 //===----------------------------------------------------------------------===//
2268 //===---------------------------------------------------------------------------
2269 /// ReturnInst - Return a value (possibly void), from a function. Execution
2270 /// does not continue in this function any longer.
2272 class ReturnInst : public TerminatorInst {
2273 ReturnInst(const ReturnInst &RI);
2276 // ReturnInst constructors:
2277 // ReturnInst() - 'ret void' instruction
2278 // ReturnInst( null) - 'ret void' instruction
2279 // ReturnInst(Value* X) - 'ret X' instruction
2280 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2281 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2282 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2283 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2285 // NOTE: If the Value* passed is of type void then the constructor behaves as
2286 // if it was passed NULL.
2287 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2288 Instruction *InsertBefore = 0);
2289 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2290 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2292 virtual ReturnInst *clone_impl() const;
2294 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2295 Instruction *InsertBefore = 0) {
2296 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2298 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2299 BasicBlock *InsertAtEnd) {
2300 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2302 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2303 return new(0) ReturnInst(C, InsertAtEnd);
2305 virtual ~ReturnInst();
2307 /// Provide fast operand accessors
2308 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2310 /// Convenience accessor. Returns null if there is no return value.
2311 Value *getReturnValue() const {
2312 return getNumOperands() != 0 ? getOperand(0) : 0;
2315 unsigned getNumSuccessors() const { return 0; }
2317 // Methods for support type inquiry through isa, cast, and dyn_cast:
2318 static inline bool classof(const Instruction *I) {
2319 return (I->getOpcode() == Instruction::Ret);
2321 static inline bool classof(const Value *V) {
2322 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2325 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2326 virtual unsigned getNumSuccessorsV() const;
2327 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2331 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2334 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2336 //===----------------------------------------------------------------------===//
2338 //===----------------------------------------------------------------------===//
2340 //===---------------------------------------------------------------------------
2341 /// BranchInst - Conditional or Unconditional Branch instruction.
2343 class BranchInst : public TerminatorInst {
2344 /// Ops list - Branches are strange. The operands are ordered:
2345 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2346 /// they don't have to check for cond/uncond branchness. These are mostly
2347 /// accessed relative from op_end().
2348 BranchInst(const BranchInst &BI);
2350 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2351 // BranchInst(BB *B) - 'br B'
2352 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2353 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2354 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2355 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2356 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2357 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2358 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2359 Instruction *InsertBefore = 0);
2360 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2361 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2362 BasicBlock *InsertAtEnd);
2364 virtual BranchInst *clone_impl() const;
2366 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2367 return new(1) BranchInst(IfTrue, InsertBefore);
2369 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2370 Value *Cond, Instruction *InsertBefore = 0) {
2371 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2373 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2374 return new(1) BranchInst(IfTrue, InsertAtEnd);
2376 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2377 Value *Cond, BasicBlock *InsertAtEnd) {
2378 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2381 /// Transparently provide more efficient getOperand methods.
2382 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2384 bool isUnconditional() const { return getNumOperands() == 1; }
2385 bool isConditional() const { return getNumOperands() == 3; }
2387 Value *getCondition() const {
2388 assert(isConditional() && "Cannot get condition of an uncond branch!");
2392 void setCondition(Value *V) {
2393 assert(isConditional() && "Cannot set condition of unconditional branch!");
2397 unsigned getNumSuccessors() const { return 1+isConditional(); }
2399 BasicBlock *getSuccessor(unsigned i) const {
2400 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2401 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2404 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2405 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2406 *(&Op<-1>() - idx) = (Value*)NewSucc;
2409 /// \brief Swap the successors of this branch instruction.
2411 /// Swaps the successors of the branch instruction. This also swaps any
2412 /// branch weight metadata associated with the instruction so that it
2413 /// continues to map correctly to each operand.
2414 void swapSuccessors();
2416 // Methods for support type inquiry through isa, cast, and dyn_cast:
2417 static inline bool classof(const Instruction *I) {
2418 return (I->getOpcode() == Instruction::Br);
2420 static inline bool classof(const Value *V) {
2421 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2424 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2425 virtual unsigned getNumSuccessorsV() const;
2426 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2430 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2433 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2435 //===----------------------------------------------------------------------===//
2437 //===----------------------------------------------------------------------===//
2439 //===---------------------------------------------------------------------------
2440 /// SwitchInst - Multiway switch
2442 class SwitchInst : public TerminatorInst {
2443 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2444 unsigned ReservedSpace;
2446 // Operand[0] = Value to switch on
2447 // Operand[1] = Default basic block destination
2448 // Operand[2n ] = Value to match
2449 // Operand[2n+1] = BasicBlock to go to on match
2451 // Store case values separately from operands list. We needn't User-Use
2452 // concept here, since it is just a case value, it will always constant,
2453 // and case value couldn't reused with another instructions/values.
2455 // It allows us to use custom type for case values that is not inherited
2456 // from Value. Since case value is a complex type that implements
2457 // the subset of integers, we needn't extract sub-constants within
2458 // slow getAggregateElement method.
2459 // For case values we will use std::list to by two reasons:
2460 // 1. It allows to add/remove cases without whole collection reallocation.
2461 // 2. In most of cases we needn't random access.
2462 // Currently case values are also stored in Operands List, but it will moved
2463 // out in future commits.
2464 typedef std::list<IntegersSubset> Subsets;
2465 typedef Subsets::iterator SubsetsIt;
2466 typedef Subsets::const_iterator SubsetsConstIt;
2470 SwitchInst(const SwitchInst &SI);
2471 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2472 void growOperands();
2473 // allocate space for exactly zero operands
2474 void *operator new(size_t s) {
2475 return User::operator new(s, 0);
2477 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2478 /// switch on and a default destination. The number of additional cases can
2479 /// be specified here to make memory allocation more efficient. This
2480 /// constructor can also autoinsert before another instruction.
2481 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2482 Instruction *InsertBefore);
2484 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2485 /// switch on and a default destination. The number of additional cases can
2486 /// be specified here to make memory allocation more efficient. This
2487 /// constructor also autoinserts at the end of the specified BasicBlock.
2488 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2489 BasicBlock *InsertAtEnd);
2491 virtual SwitchInst *clone_impl() const;
2494 // FIXME: Currently there are a lot of unclean template parameters,
2495 // we need to make refactoring in future.
2496 // All these parameters are used to implement both iterator and const_iterator
2497 // without code duplication.
2498 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2499 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2500 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2501 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2502 template <class SwitchInstTy, class ConstantIntTy,
2503 class SubsetsItTy, class BasicBlockTy>
2504 class CaseIteratorT;
2506 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2507 SubsetsConstIt, const BasicBlock> ConstCaseIt;
2511 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2513 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2514 unsigned NumCases, Instruction *InsertBefore = 0) {
2515 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2517 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2518 unsigned NumCases, BasicBlock *InsertAtEnd) {
2519 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2524 /// Provide fast operand accessors
2525 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2527 // Accessor Methods for Switch stmt
2528 Value *getCondition() const { return getOperand(0); }
2529 void setCondition(Value *V) { setOperand(0, V); }
2531 BasicBlock *getDefaultDest() const {
2532 return cast<BasicBlock>(getOperand(1));
2535 void setDefaultDest(BasicBlock *DefaultCase) {
2536 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2539 /// getNumCases - return the number of 'cases' in this switch instruction,
2540 /// except the default case
2541 unsigned getNumCases() const {
2542 return getNumOperands()/2 - 1;
2545 /// Returns a read/write iterator that points to the first
2546 /// case in SwitchInst.
2547 CaseIt case_begin() {
2548 return CaseIt(this, 0, TheSubsets.begin());
2550 /// Returns a read-only iterator that points to the first
2551 /// case in the SwitchInst.
2552 ConstCaseIt case_begin() const {
2553 return ConstCaseIt(this, 0, TheSubsets.begin());
2556 /// Returns a read/write iterator that points one past the last
2557 /// in the SwitchInst.
2559 return CaseIt(this, getNumCases(), TheSubsets.end());
2561 /// Returns a read-only iterator that points one past the last
2562 /// in the SwitchInst.
2563 ConstCaseIt case_end() const {
2564 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2566 /// Returns an iterator that points to the default case.
2567 /// Note: this iterator allows to resolve successor only. Attempt
2568 /// to resolve case value causes an assertion.
2569 /// Also note, that increment and decrement also causes an assertion and
2570 /// makes iterator invalid.
2571 CaseIt case_default() {
2572 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2574 ConstCaseIt case_default() const {
2575 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2578 /// findCaseValue - Search all of the case values for the specified constant.
2579 /// If it is explicitly handled, return the case iterator of it, otherwise
2580 /// return default case iterator to indicate
2581 /// that it is handled by the default handler.
2582 CaseIt findCaseValue(const ConstantInt *C) {
2583 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2584 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2586 return case_default();
2588 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2589 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2590 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2592 return case_default();
2595 /// findCaseDest - Finds the unique case value for a given successor. Returns
2596 /// null if the successor is not found, not unique, or is the default case.
2597 ConstantInt *findCaseDest(BasicBlock *BB) {
2598 if (BB == getDefaultDest()) return NULL;
2600 ConstantInt *CI = NULL;
2601 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2602 if (i.getCaseSuccessor() == BB) {
2603 if (CI) return NULL; // Multiple cases lead to BB.
2604 else CI = i.getCaseValue();
2610 /// 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(ConstantInt *OnVal, BasicBlock *Dest);
2617 /// addCase - Add an entry to the switch instruction.
2619 /// This action invalidates case_end(). Old case_end() iterator will
2620 /// point to the added case.
2621 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2623 /// removeCase - This method removes the specified case and its successor
2624 /// from the switch instruction. Note that this operation may reorder the
2625 /// remaining cases at index idx and above.
2627 /// This action invalidates iterators for all cases following the one removed,
2628 /// including the case_end() iterator.
2629 void removeCase(CaseIt& i);
2631 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2632 BasicBlock *getSuccessor(unsigned idx) const {
2633 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2634 return cast<BasicBlock>(getOperand(idx*2+1));
2636 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2637 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2638 setOperand(idx*2+1, (Value*)NewSucc);
2641 uint16_t hash() const {
2642 uint32_t NumberOfCases = (uint32_t)getNumCases();
2643 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2644 for (ConstCaseIt i = case_begin(), e = case_end();
2646 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2647 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2652 // Case iterators definition.
2654 template <class SwitchInstTy, class ConstantIntTy,
2655 class SubsetsItTy, class BasicBlockTy>
2656 class CaseIteratorT {
2660 unsigned long Index;
2661 SubsetsItTy SubsetIt;
2663 /// Initializes case iterator for given SwitchInst and for given
2665 friend class SwitchInst;
2666 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2667 SubsetsItTy CaseValueIt) {
2669 Index = SuccessorIndex;
2670 this->SubsetIt = CaseValueIt;
2674 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2675 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2676 SubsetsItTy, BasicBlockTy> Self;
2678 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2681 SubsetIt = SI->TheSubsets.begin();
2682 std::advance(SubsetIt, CaseNum);
2686 /// Initializes case iterator for given SwitchInst and for given
2687 /// TerminatorInst's successor index.
2688 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2689 assert(SuccessorIndex < SI->getNumSuccessors() &&
2690 "Successor index # out of range!");
2691 return SuccessorIndex != 0 ?
2692 Self(SI, SuccessorIndex - 1) :
2693 Self(SI, DefaultPseudoIndex);
2696 /// Resolves case value for current case.
2698 ConstantIntTy *getCaseValue() {
2699 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2700 IntegersSubsetRef CaseRanges = *SubsetIt;
2702 // FIXME: Currently we work with ConstantInt based cases.
2703 // So return CaseValue as ConstantInt.
2704 return CaseRanges.getSingleNumber(0).toConstantInt();
2707 /// Resolves case value for current case.
2708 IntegersSubsetRef getCaseValueEx() {
2709 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2713 /// Resolves successor for current case.
2714 BasicBlockTy *getCaseSuccessor() {
2715 assert((Index < SI->getNumCases() ||
2716 Index == DefaultPseudoIndex) &&
2717 "Index out the number of cases.");
2718 return SI->getSuccessor(getSuccessorIndex());
2721 /// Returns number of current case.
2722 unsigned getCaseIndex() const { return Index; }
2724 /// Returns TerminatorInst's successor index for current case successor.
2725 unsigned getSuccessorIndex() const {
2726 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2727 "Index out the number of cases.");
2728 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2732 // Check index correctness after increment.
2733 // Note: Index == getNumCases() means end().
2734 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2737 SubsetIt = SI->TheSubsets.begin();
2742 Self operator++(int) {
2748 // Check index correctness after decrement.
2749 // Note: Index == getNumCases() means end().
2750 // Also allow "-1" iterator here. That will became valid after ++.
2751 unsigned NumCases = SI->getNumCases();
2752 assert((Index == 0 || Index-1 <= NumCases) &&
2753 "Index out the number of cases.");
2755 if (Index == NumCases) {
2756 SubsetIt = SI->TheSubsets.end();
2765 Self operator--(int) {
2770 bool operator==(const Self& RHS) const {
2771 assert(RHS.SI == SI && "Incompatible operators.");
2772 return RHS.Index == Index;
2774 bool operator!=(const Self& RHS) const {
2775 assert(RHS.SI == SI && "Incompatible operators.");
2776 return RHS.Index != Index;
2780 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2781 SubsetsIt, BasicBlock> {
2782 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2786 friend class SwitchInst;
2787 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2788 ParentTy(SI, CaseNum, SubsetIt) {}
2790 void updateCaseValueOperand(IntegersSubset& V) {
2791 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2796 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2798 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2800 /// Sets the new value for current case.
2802 void setValue(ConstantInt *V) {
2803 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2804 IntegersSubsetToBB Mapping;
2805 // FIXME: Currently we work with ConstantInt based cases.
2806 // So inititalize IntItem container directly from ConstantInt.
2807 Mapping.add(IntItem::fromConstantInt(V));
2808 *SubsetIt = Mapping.getCase();
2809 updateCaseValueOperand(*SubsetIt);
2812 /// Sets the new value for current case.
2813 void setValueEx(IntegersSubset& V) {
2814 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2816 updateCaseValueOperand(*SubsetIt);
2819 /// Sets the new successor for current case.
2820 void setSuccessor(BasicBlock *S) {
2821 SI->setSuccessor(getSuccessorIndex(), S);
2825 // Methods for support type inquiry through isa, cast, and dyn_cast:
2827 static inline bool classof(const Instruction *I) {
2828 return I->getOpcode() == Instruction::Switch;
2830 static inline bool classof(const Value *V) {
2831 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2834 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2835 virtual unsigned getNumSuccessorsV() const;
2836 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2840 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2843 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2846 //===----------------------------------------------------------------------===//
2847 // IndirectBrInst Class
2848 //===----------------------------------------------------------------------===//
2850 //===---------------------------------------------------------------------------
2851 /// IndirectBrInst - Indirect Branch Instruction.
2853 class IndirectBrInst : public TerminatorInst {
2854 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2855 unsigned ReservedSpace;
2856 // Operand[0] = Value to switch on
2857 // Operand[1] = Default basic block destination
2858 // Operand[2n ] = Value to match
2859 // Operand[2n+1] = BasicBlock to go to on match
2860 IndirectBrInst(const IndirectBrInst &IBI);
2861 void init(Value *Address, unsigned NumDests);
2862 void growOperands();
2863 // allocate space for exactly zero operands
2864 void *operator new(size_t s) {
2865 return User::operator new(s, 0);
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 can also
2870 /// autoinsert before another instruction.
2871 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2873 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2874 /// Address to jump to. The number of expected destinations can be specified
2875 /// here to make memory allocation more efficient. This constructor also
2876 /// autoinserts at the end of the specified BasicBlock.
2877 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2879 virtual IndirectBrInst *clone_impl() const;
2881 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2882 Instruction *InsertBefore = 0) {
2883 return new IndirectBrInst(Address, NumDests, InsertBefore);
2885 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2886 BasicBlock *InsertAtEnd) {
2887 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2891 /// Provide fast operand accessors.
2892 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2894 // Accessor Methods for IndirectBrInst instruction.
2895 Value *getAddress() { return getOperand(0); }
2896 const Value *getAddress() const { return getOperand(0); }
2897 void setAddress(Value *V) { setOperand(0, V); }
2900 /// getNumDestinations - return the number of possible destinations in this
2901 /// indirectbr instruction.
2902 unsigned getNumDestinations() const { return getNumOperands()-1; }
2904 /// getDestination - Return the specified destination.
2905 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2906 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2908 /// addDestination - Add a destination.
2910 void addDestination(BasicBlock *Dest);
2912 /// removeDestination - This method removes the specified successor from the
2913 /// indirectbr instruction.
2914 void removeDestination(unsigned i);
2916 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2917 BasicBlock *getSuccessor(unsigned i) const {
2918 return cast<BasicBlock>(getOperand(i+1));
2920 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2921 setOperand(i+1, (Value*)NewSucc);
2924 // Methods for support type inquiry through isa, cast, and dyn_cast:
2925 static inline bool classof(const Instruction *I) {
2926 return I->getOpcode() == Instruction::IndirectBr;
2928 static inline bool classof(const Value *V) {
2929 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2932 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2933 virtual unsigned getNumSuccessorsV() const;
2934 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2938 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2941 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2944 //===----------------------------------------------------------------------===//
2946 //===----------------------------------------------------------------------===//
2948 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2949 /// calling convention of the call.
2951 class InvokeInst : public TerminatorInst {
2952 AttrListPtr AttributeList;
2953 InvokeInst(const InvokeInst &BI);
2954 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2955 ArrayRef<Value *> Args, const Twine &NameStr);
2957 /// Construct an InvokeInst given a range of arguments.
2959 /// @brief Construct an InvokeInst from a range of arguments
2960 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2961 ArrayRef<Value *> Args, unsigned Values,
2962 const Twine &NameStr, Instruction *InsertBefore);
2964 /// Construct an InvokeInst given a range of arguments.
2966 /// @brief Construct an InvokeInst from a range of arguments
2967 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2968 ArrayRef<Value *> Args, unsigned Values,
2969 const Twine &NameStr, BasicBlock *InsertAtEnd);
2971 virtual InvokeInst *clone_impl() const;
2973 static InvokeInst *Create(Value *Func,
2974 BasicBlock *IfNormal, BasicBlock *IfException,
2975 ArrayRef<Value *> Args, const Twine &NameStr = "",
2976 Instruction *InsertBefore = 0) {
2977 unsigned Values = unsigned(Args.size()) + 3;
2978 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2979 Values, NameStr, InsertBefore);
2981 static InvokeInst *Create(Value *Func,
2982 BasicBlock *IfNormal, BasicBlock *IfException,
2983 ArrayRef<Value *> Args, const Twine &NameStr,
2984 BasicBlock *InsertAtEnd) {
2985 unsigned Values = unsigned(Args.size()) + 3;
2986 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2987 Values, NameStr, InsertAtEnd);
2990 /// Provide fast operand accessors
2991 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2993 /// getNumArgOperands - Return the number of invoke arguments.
2995 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2997 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2999 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3000 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3002 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3004 CallingConv::ID getCallingConv() const {
3005 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3007 void setCallingConv(CallingConv::ID CC) {
3008 setInstructionSubclassData(static_cast<unsigned>(CC));
3011 /// getAttributes - Return the parameter attributes for this invoke.
3013 const AttrListPtr &getAttributes() const { return AttributeList; }
3015 /// setAttributes - Set the parameter attributes for this invoke.
3017 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3019 /// addAttribute - adds the attribute to the list of attributes.
3020 void addAttribute(unsigned i, Attributes attr);
3022 /// removeAttribute - removes the attribute from the list of attributes.
3023 void removeAttribute(unsigned i, Attributes attr);
3025 /// @brief Determine whether this call has the NoAlias attribute.
3026 bool hasFnAttr(Attributes::AttrVal A) const;
3028 /// @brief Determine whether the call or the callee has the given attributes.
3029 bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
3031 /// @brief Extract the alignment for a call or parameter (0=unknown).
3032 unsigned getParamAlignment(unsigned i) const {
3033 return AttributeList.getParamAlignment(i);
3036 /// @brief Return true if the call should not be inlined.
3037 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
3038 void setIsNoInline() {
3040 B.addAttribute(Attributes::NoInline);
3041 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
3044 /// @brief Determine if the call does not access memory.
3045 bool doesNotAccessMemory() const {
3046 return hasFnAttr(Attributes::ReadNone);
3048 void setDoesNotAccessMemory() {
3050 B.addAttribute(Attributes::ReadNone);
3051 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
3054 /// @brief Determine if the call does not access or only reads memory.
3055 bool onlyReadsMemory() const {
3056 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
3058 void setOnlyReadsMemory() {
3060 B.addAttribute(Attributes::ReadOnly);
3061 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
3064 /// @brief Determine if the call cannot return.
3065 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
3066 void setDoesNotReturn() {
3068 B.addAttribute(Attributes::NoReturn);
3069 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
3072 /// @brief Determine if the call cannot unwind.
3073 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
3074 void setDoesNotThrow() {
3076 B.addAttribute(Attributes::NoUnwind);
3077 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
3080 /// @brief Determine if the call returns a structure through first
3081 /// pointer argument.
3082 bool hasStructRetAttr() const {
3083 // Be friendly and also check the callee.
3084 return paramHasAttr(1, Attributes::StructRet);
3087 /// @brief Determine if any call argument is an aggregate passed by value.
3088 bool hasByValArgument() const {
3089 for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3090 if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
3095 /// getCalledFunction - Return the function called, or null if this is an
3096 /// indirect function invocation.
3098 Function *getCalledFunction() const {
3099 return dyn_cast<Function>(Op<-3>());
3102 /// getCalledValue - Get a pointer to the function that is invoked by this
3104 const Value *getCalledValue() const { return Op<-3>(); }
3105 Value *getCalledValue() { return Op<-3>(); }
3107 /// setCalledFunction - Set the function called.
3108 void setCalledFunction(Value* Fn) {
3112 // get*Dest - Return the destination basic blocks...
3113 BasicBlock *getNormalDest() const {
3114 return cast<BasicBlock>(Op<-2>());
3116 BasicBlock *getUnwindDest() const {
3117 return cast<BasicBlock>(Op<-1>());
3119 void setNormalDest(BasicBlock *B) {
3120 Op<-2>() = reinterpret_cast<Value*>(B);
3122 void setUnwindDest(BasicBlock *B) {
3123 Op<-1>() = reinterpret_cast<Value*>(B);
3126 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3127 /// block (the unwind destination).
3128 LandingPadInst *getLandingPadInst() const;
3130 BasicBlock *getSuccessor(unsigned i) const {
3131 assert(i < 2 && "Successor # out of range for invoke!");
3132 return i == 0 ? getNormalDest() : getUnwindDest();
3135 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3136 assert(idx < 2 && "Successor # out of range for invoke!");
3137 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3140 unsigned getNumSuccessors() const { return 2; }
3142 // Methods for support type inquiry through isa, cast, and dyn_cast:
3143 static inline bool classof(const Instruction *I) {
3144 return (I->getOpcode() == Instruction::Invoke);
3146 static inline bool classof(const Value *V) {
3147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3151 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3152 virtual unsigned getNumSuccessorsV() const;
3153 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3155 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3156 // method so that subclasses cannot accidentally use it.
3157 void setInstructionSubclassData(unsigned short D) {
3158 Instruction::setInstructionSubclassData(D);
3163 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3166 InvokeInst::InvokeInst(Value *Func,
3167 BasicBlock *IfNormal, BasicBlock *IfException,
3168 ArrayRef<Value *> Args, unsigned Values,
3169 const Twine &NameStr, Instruction *InsertBefore)
3170 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3171 ->getElementType())->getReturnType(),
3172 Instruction::Invoke,
3173 OperandTraits<InvokeInst>::op_end(this) - Values,
3174 Values, InsertBefore) {
3175 init(Func, IfNormal, IfException, Args, NameStr);
3177 InvokeInst::InvokeInst(Value *Func,
3178 BasicBlock *IfNormal, BasicBlock *IfException,
3179 ArrayRef<Value *> Args, unsigned Values,
3180 const Twine &NameStr, BasicBlock *InsertAtEnd)
3181 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3182 ->getElementType())->getReturnType(),
3183 Instruction::Invoke,
3184 OperandTraits<InvokeInst>::op_end(this) - Values,
3185 Values, InsertAtEnd) {
3186 init(Func, IfNormal, IfException, Args, NameStr);
3189 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3191 //===----------------------------------------------------------------------===//
3193 //===----------------------------------------------------------------------===//
3195 //===---------------------------------------------------------------------------
3196 /// ResumeInst - Resume the propagation of an exception.
3198 class ResumeInst : public TerminatorInst {
3199 ResumeInst(const ResumeInst &RI);
3201 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3202 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3204 virtual ResumeInst *clone_impl() const;
3206 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3207 return new(1) ResumeInst(Exn, InsertBefore);
3209 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3210 return new(1) ResumeInst(Exn, InsertAtEnd);
3213 /// Provide fast operand accessors
3214 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3216 /// Convenience accessor.
3217 Value *getValue() const { return Op<0>(); }
3219 unsigned getNumSuccessors() const { return 0; }
3221 // Methods for support type inquiry through isa, cast, and dyn_cast:
3222 static inline bool classof(const Instruction *I) {
3223 return I->getOpcode() == Instruction::Resume;
3225 static inline bool classof(const Value *V) {
3226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3229 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3230 virtual unsigned getNumSuccessorsV() const;
3231 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3235 struct OperandTraits<ResumeInst> :
3236 public FixedNumOperandTraits<ResumeInst, 1> {
3239 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3241 //===----------------------------------------------------------------------===//
3242 // UnreachableInst Class
3243 //===----------------------------------------------------------------------===//
3245 //===---------------------------------------------------------------------------
3246 /// UnreachableInst - This function has undefined behavior. In particular, the
3247 /// presence of this instruction indicates some higher level knowledge that the
3248 /// end of the block cannot be reached.
3250 class UnreachableInst : public TerminatorInst {
3251 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3253 virtual UnreachableInst *clone_impl() const;
3256 // allocate space for exactly zero operands
3257 void *operator new(size_t s) {
3258 return User::operator new(s, 0);
3260 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3261 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3263 unsigned getNumSuccessors() const { return 0; }
3265 // Methods for support type inquiry through isa, cast, and dyn_cast:
3266 static inline bool classof(const Instruction *I) {
3267 return I->getOpcode() == Instruction::Unreachable;
3269 static inline bool classof(const Value *V) {
3270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3273 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3274 virtual unsigned getNumSuccessorsV() const;
3275 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3278 //===----------------------------------------------------------------------===//
3280 //===----------------------------------------------------------------------===//
3282 /// @brief This class represents a truncation of integer types.
3283 class TruncInst : public CastInst {
3285 /// @brief Clone an identical TruncInst
3286 virtual TruncInst *clone_impl() const;
3289 /// @brief Constructor with insert-before-instruction semantics
3291 Value *S, ///< The value to be truncated
3292 Type *Ty, ///< The (smaller) type to truncate to
3293 const Twine &NameStr = "", ///< A name for the new instruction
3294 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3297 /// @brief Constructor with insert-at-end-of-block semantics
3299 Value *S, ///< The value to be truncated
3300 Type *Ty, ///< The (smaller) type to truncate to
3301 const Twine &NameStr, ///< A name for the new instruction
3302 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3305 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3306 static inline bool classof(const Instruction *I) {
3307 return I->getOpcode() == Trunc;
3309 static inline bool classof(const Value *V) {
3310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3314 //===----------------------------------------------------------------------===//
3316 //===----------------------------------------------------------------------===//
3318 /// @brief This class represents zero extension of integer types.
3319 class ZExtInst : public CastInst {
3321 /// @brief Clone an identical ZExtInst
3322 virtual ZExtInst *clone_impl() const;
3325 /// @brief Constructor with insert-before-instruction semantics
3327 Value *S, ///< The value to be zero extended
3328 Type *Ty, ///< The type to zero extend to
3329 const Twine &NameStr = "", ///< A name for the new instruction
3330 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3333 /// @brief Constructor with insert-at-end semantics.
3335 Value *S, ///< The value to be zero extended
3336 Type *Ty, ///< The type to zero extend to
3337 const Twine &NameStr, ///< A name for the new instruction
3338 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3341 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3342 static inline bool classof(const Instruction *I) {
3343 return I->getOpcode() == ZExt;
3345 static inline bool classof(const Value *V) {
3346 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3350 //===----------------------------------------------------------------------===//
3352 //===----------------------------------------------------------------------===//
3354 /// @brief This class represents a sign extension of integer types.
3355 class SExtInst : public CastInst {
3357 /// @brief Clone an identical SExtInst
3358 virtual SExtInst *clone_impl() const;
3361 /// @brief Constructor with insert-before-instruction semantics
3363 Value *S, ///< The value to be sign extended
3364 Type *Ty, ///< The type to sign extend to
3365 const Twine &NameStr = "", ///< A name for the new instruction
3366 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3369 /// @brief Constructor with insert-at-end-of-block semantics
3371 Value *S, ///< The value to be sign extended
3372 Type *Ty, ///< The type to sign extend to
3373 const Twine &NameStr, ///< A name for the new instruction
3374 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3377 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3378 static inline bool classof(const Instruction *I) {
3379 return I->getOpcode() == SExt;
3381 static inline bool classof(const Value *V) {
3382 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3386 //===----------------------------------------------------------------------===//
3387 // FPTruncInst Class
3388 //===----------------------------------------------------------------------===//
3390 /// @brief This class represents a truncation of floating point types.
3391 class FPTruncInst : public CastInst {
3393 /// @brief Clone an identical FPTruncInst
3394 virtual FPTruncInst *clone_impl() const;
3397 /// @brief Constructor with insert-before-instruction semantics
3399 Value *S, ///< The value to be truncated
3400 Type *Ty, ///< The type to truncate to
3401 const Twine &NameStr = "", ///< A name for the new instruction
3402 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3405 /// @brief Constructor with insert-before-instruction semantics
3407 Value *S, ///< The value to be truncated
3408 Type *Ty, ///< The type to truncate to
3409 const Twine &NameStr, ///< A name for the new instruction
3410 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3413 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3414 static inline bool classof(const Instruction *I) {
3415 return I->getOpcode() == FPTrunc;
3417 static inline bool classof(const Value *V) {
3418 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3422 //===----------------------------------------------------------------------===//
3424 //===----------------------------------------------------------------------===//
3426 /// @brief This class represents an extension of floating point types.
3427 class FPExtInst : public CastInst {
3429 /// @brief Clone an identical FPExtInst
3430 virtual FPExtInst *clone_impl() const;
3433 /// @brief Constructor with insert-before-instruction semantics
3435 Value *S, ///< The value to be extended
3436 Type *Ty, ///< The type to extend to
3437 const Twine &NameStr = "", ///< A name for the new instruction
3438 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3441 /// @brief Constructor with insert-at-end-of-block semantics
3443 Value *S, ///< The value to be extended
3444 Type *Ty, ///< The type to extend to
3445 const Twine &NameStr, ///< A name for the new instruction
3446 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3449 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3450 static inline bool classof(const Instruction *I) {
3451 return I->getOpcode() == FPExt;
3453 static inline bool classof(const Value *V) {
3454 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3458 //===----------------------------------------------------------------------===//
3460 //===----------------------------------------------------------------------===//
3462 /// @brief This class represents a cast unsigned integer to floating point.
3463 class UIToFPInst : public CastInst {
3465 /// @brief Clone an identical UIToFPInst
3466 virtual UIToFPInst *clone_impl() const;
3469 /// @brief Constructor with insert-before-instruction semantics
3471 Value *S, ///< The value to be converted
3472 Type *Ty, ///< The type to convert to
3473 const Twine &NameStr = "", ///< A name for the new instruction
3474 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3477 /// @brief Constructor with insert-at-end-of-block semantics
3479 Value *S, ///< The value to be converted
3480 Type *Ty, ///< The type to convert to
3481 const Twine &NameStr, ///< A name for the new instruction
3482 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3485 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3486 static inline bool classof(const Instruction *I) {
3487 return I->getOpcode() == UIToFP;
3489 static inline bool classof(const Value *V) {
3490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3494 //===----------------------------------------------------------------------===//
3496 //===----------------------------------------------------------------------===//
3498 /// @brief This class represents a cast from signed integer to floating point.
3499 class SIToFPInst : public CastInst {
3501 /// @brief Clone an identical SIToFPInst
3502 virtual SIToFPInst *clone_impl() const;
3505 /// @brief Constructor with insert-before-instruction semantics
3507 Value *S, ///< The value to be converted
3508 Type *Ty, ///< The type to convert to
3509 const Twine &NameStr = "", ///< A name for the new instruction
3510 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3513 /// @brief Constructor with insert-at-end-of-block semantics
3515 Value *S, ///< The value to be converted
3516 Type *Ty, ///< The type to convert to
3517 const Twine &NameStr, ///< A name for the new instruction
3518 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3521 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3522 static inline bool classof(const Instruction *I) {
3523 return I->getOpcode() == SIToFP;
3525 static inline bool classof(const Value *V) {
3526 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3530 //===----------------------------------------------------------------------===//
3532 //===----------------------------------------------------------------------===//
3534 /// @brief This class represents a cast from floating point to unsigned integer
3535 class FPToUIInst : public CastInst {
3537 /// @brief Clone an identical FPToUIInst
3538 virtual FPToUIInst *clone_impl() const;
3541 /// @brief Constructor with insert-before-instruction semantics
3543 Value *S, ///< The value to be converted
3544 Type *Ty, ///< The type to convert to
3545 const Twine &NameStr = "", ///< A name for the new instruction
3546 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3549 /// @brief Constructor with insert-at-end-of-block semantics
3551 Value *S, ///< The value to be converted
3552 Type *Ty, ///< The type to convert to
3553 const Twine &NameStr, ///< A name for the new instruction
3554 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3557 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3558 static inline bool classof(const Instruction *I) {
3559 return I->getOpcode() == FPToUI;
3561 static inline bool classof(const Value *V) {
3562 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3566 //===----------------------------------------------------------------------===//
3568 //===----------------------------------------------------------------------===//
3570 /// @brief This class represents a cast from floating point to signed integer.
3571 class FPToSIInst : public CastInst {
3573 /// @brief Clone an identical FPToSIInst
3574 virtual FPToSIInst *clone_impl() const;
3577 /// @brief Constructor with insert-before-instruction semantics
3579 Value *S, ///< The value to be converted
3580 Type *Ty, ///< The type to convert to
3581 const Twine &NameStr = "", ///< A name for the new instruction
3582 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3585 /// @brief Constructor with insert-at-end-of-block semantics
3587 Value *S, ///< The value to be converted
3588 Type *Ty, ///< The type to convert to
3589 const Twine &NameStr, ///< A name for the new instruction
3590 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3593 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3594 static inline bool classof(const Instruction *I) {
3595 return I->getOpcode() == FPToSI;
3597 static inline bool classof(const Value *V) {
3598 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3602 //===----------------------------------------------------------------------===//
3603 // IntToPtrInst Class
3604 //===----------------------------------------------------------------------===//
3606 /// @brief This class represents a cast from an integer to a pointer.
3607 class IntToPtrInst : public CastInst {
3609 /// @brief Constructor with insert-before-instruction semantics
3611 Value *S, ///< The value to be converted
3612 Type *Ty, ///< The type to convert to
3613 const Twine &NameStr = "", ///< A name for the new instruction
3614 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3617 /// @brief Constructor with insert-at-end-of-block semantics
3619 Value *S, ///< The value to be converted
3620 Type *Ty, ///< The type to convert to
3621 const Twine &NameStr, ///< A name for the new instruction
3622 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3625 /// @brief Clone an identical IntToPtrInst
3626 virtual IntToPtrInst *clone_impl() const;
3628 /// @brief return the address space of the pointer.
3629 unsigned getAddressSpace() const {
3630 if (getType()->isPointerTy())
3631 return cast<PointerType>(getType())->getAddressSpace();
3632 if (getType()->isVectorTy() &&
3633 cast<VectorType>(getType())->getElementType()->isPointerTy())
3634 return cast<PointerType>(
3635 cast<VectorType>(getType())->getElementType())
3636 ->getAddressSpace();
3637 llvm_unreachable("Must be a pointer or a vector of pointers.");
3641 // Methods for support type inquiry through isa, cast, and dyn_cast:
3642 static inline bool classof(const Instruction *I) {
3643 return I->getOpcode() == IntToPtr;
3645 static inline bool classof(const Value *V) {
3646 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3650 //===----------------------------------------------------------------------===//
3651 // PtrToIntInst Class
3652 //===----------------------------------------------------------------------===//
3654 /// @brief This class represents a cast from a pointer to an integer
3655 class PtrToIntInst : public CastInst {
3657 /// @brief Clone an identical PtrToIntInst
3658 virtual PtrToIntInst *clone_impl() const;
3661 /// @brief Constructor with insert-before-instruction semantics
3663 Value *S, ///< The value to be converted
3664 Type *Ty, ///< The type to convert to
3665 const Twine &NameStr = "", ///< A name for the new instruction
3666 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3669 /// @brief Constructor with insert-at-end-of-block semantics
3671 Value *S, ///< The value to be converted
3672 Type *Ty, ///< The type to convert to
3673 const Twine &NameStr, ///< A name for the new instruction
3674 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3677 /// @brief return the address space of the pointer.
3678 unsigned getPointerAddressSpace() const {
3679 Type *Ty = getOperand(0)->getType();
3680 if (Ty->isPointerTy())
3681 return cast<PointerType>(Ty)->getAddressSpace();
3682 if (Ty->isVectorTy()
3683 && cast<VectorType>(Ty)->getElementType()->isPointerTy())
3684 return cast<PointerType>(
3685 cast<VectorType>(Ty)->getElementType())
3686 ->getAddressSpace();
3687 llvm_unreachable("Must be a pointer or a vector of pointers.");
3691 // Methods for support type inquiry through isa, cast, and dyn_cast:
3692 static inline bool classof(const Instruction *I) {
3693 return I->getOpcode() == PtrToInt;
3695 static inline bool classof(const Value *V) {
3696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3700 //===----------------------------------------------------------------------===//
3701 // BitCastInst Class
3702 //===----------------------------------------------------------------------===//
3704 /// @brief This class represents a no-op cast from one type to another.
3705 class BitCastInst : public CastInst {
3707 /// @brief Clone an identical BitCastInst
3708 virtual BitCastInst *clone_impl() const;
3711 /// @brief Constructor with insert-before-instruction semantics
3713 Value *S, ///< The value to be casted
3714 Type *Ty, ///< The type to casted to
3715 const Twine &NameStr = "", ///< A name for the new instruction
3716 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3719 /// @brief Constructor with insert-at-end-of-block semantics
3721 Value *S, ///< The value to be casted
3722 Type *Ty, ///< The type to casted to
3723 const Twine &NameStr, ///< A name for the new instruction
3724 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3727 // Methods for support type inquiry through isa, cast, and dyn_cast:
3728 static inline bool classof(const Instruction *I) {
3729 return I->getOpcode() == BitCast;
3731 static inline bool classof(const Value *V) {
3732 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3736 } // End llvm namespace