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 AllocaInst *) { return true; }
116 static inline bool classof(const Instruction *I) {
117 return (I->getOpcode() == Instruction::Alloca);
119 static inline bool classof(const Value *V) {
120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
123 // Shadow Instruction::setInstructionSubclassData with a private forwarding
124 // method so that subclasses cannot accidentally use it.
125 void setInstructionSubclassData(unsigned short D) {
126 Instruction::setInstructionSubclassData(D);
131 //===----------------------------------------------------------------------===//
133 //===----------------------------------------------------------------------===//
135 /// LoadInst - an instruction for reading from memory. This uses the
136 /// SubclassData field in Value to store whether or not the load is volatile.
138 class LoadInst : public UnaryInstruction {
141 virtual LoadInst *clone_impl() const;
143 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
144 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
145 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
146 Instruction *InsertBefore = 0);
147 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
148 BasicBlock *InsertAtEnd);
149 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
150 unsigned Align, Instruction *InsertBefore = 0);
151 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
152 unsigned Align, BasicBlock *InsertAtEnd);
153 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
154 unsigned Align, AtomicOrdering Order,
155 SynchronizationScope SynchScope = CrossThread,
156 Instruction *InsertBefore = 0);
157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
158 unsigned Align, AtomicOrdering Order,
159 SynchronizationScope SynchScope,
160 BasicBlock *InsertAtEnd);
162 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
163 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
164 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
165 bool isVolatile = false, Instruction *InsertBefore = 0);
166 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
167 BasicBlock *InsertAtEnd);
169 /// isVolatile - Return true if this is a load from a volatile memory
172 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
174 /// setVolatile - Specify whether this is a volatile load or not.
176 void setVolatile(bool V) {
177 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
181 /// getAlignment - Return the alignment of the access that is being performed
183 unsigned getAlignment() const {
184 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
187 void setAlignment(unsigned Align);
189 /// Returns the ordering effect of this fence.
190 AtomicOrdering getOrdering() const {
191 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
194 /// Set the ordering constraint on this load. May not be Release or
196 void setOrdering(AtomicOrdering Ordering) {
197 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
201 SynchronizationScope getSynchScope() const {
202 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
205 /// Specify whether this load is ordered with respect to all
206 /// concurrently executing threads, or only with respect to signal handlers
207 /// executing in the same thread.
208 void setSynchScope(SynchronizationScope xthread) {
209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
213 bool isAtomic() const { return getOrdering() != NotAtomic; }
214 void setAtomic(AtomicOrdering Ordering,
215 SynchronizationScope SynchScope = CrossThread) {
216 setOrdering(Ordering);
217 setSynchScope(SynchScope);
220 bool isSimple() const { return !isAtomic() && !isVolatile(); }
221 bool isUnordered() const {
222 return getOrdering() <= Unordered && !isVolatile();
225 Value *getPointerOperand() { return getOperand(0); }
226 const Value *getPointerOperand() const { return getOperand(0); }
227 static unsigned getPointerOperandIndex() { return 0U; }
229 unsigned getPointerAddressSpace() const {
230 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const LoadInst *) { return true; }
236 static inline bool classof(const Instruction *I) {
237 return I->getOpcode() == Instruction::Load;
239 static inline bool classof(const Value *V) {
240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
243 // Shadow Instruction::setInstructionSubclassData with a private forwarding
244 // method so that subclasses cannot accidentally use it.
245 void setInstructionSubclassData(unsigned short D) {
246 Instruction::setInstructionSubclassData(D);
251 //===----------------------------------------------------------------------===//
253 //===----------------------------------------------------------------------===//
255 /// StoreInst - an instruction for storing to memory
257 class StoreInst : public Instruction {
258 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
261 virtual StoreInst *clone_impl() const;
263 // allocate space for exactly two operands
264 void *operator new(size_t s) {
265 return User::operator new(s, 2);
267 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
268 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
270 Instruction *InsertBefore = 0);
271 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
272 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
273 unsigned Align, Instruction *InsertBefore = 0);
274 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
275 unsigned Align, BasicBlock *InsertAtEnd);
276 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
277 unsigned Align, AtomicOrdering Order,
278 SynchronizationScope SynchScope = CrossThread,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
281 unsigned Align, AtomicOrdering Order,
282 SynchronizationScope SynchScope,
283 BasicBlock *InsertAtEnd);
286 /// isVolatile - Return true if this is a store to a volatile memory
289 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
291 /// setVolatile - Specify whether this is a volatile store or not.
293 void setVolatile(bool V) {
294 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
298 /// Transparently provide more efficient getOperand methods.
299 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
301 /// getAlignment - Return the alignment of the access that is being performed
303 unsigned getAlignment() const {
304 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
307 void setAlignment(unsigned Align);
309 /// Returns the ordering effect of this store.
310 AtomicOrdering getOrdering() const {
311 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
314 /// Set the ordering constraint on this store. May not be Acquire or
316 void setOrdering(AtomicOrdering Ordering) {
317 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
321 SynchronizationScope getSynchScope() const {
322 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
325 /// Specify whether this store instruction is ordered with respect to all
326 /// concurrently executing threads, or only with respect to signal handlers
327 /// executing in the same thread.
328 void setSynchScope(SynchronizationScope xthread) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
333 bool isAtomic() const { return getOrdering() != NotAtomic; }
334 void setAtomic(AtomicOrdering Ordering,
335 SynchronizationScope SynchScope = CrossThread) {
336 setOrdering(Ordering);
337 setSynchScope(SynchScope);
340 bool isSimple() const { return !isAtomic() && !isVolatile(); }
341 bool isUnordered() const {
342 return getOrdering() <= Unordered && !isVolatile();
345 Value *getValueOperand() { return getOperand(0); }
346 const Value *getValueOperand() const { return getOperand(0); }
348 Value *getPointerOperand() { return getOperand(1); }
349 const Value *getPointerOperand() const { return getOperand(1); }
350 static unsigned getPointerOperandIndex() { return 1U; }
352 unsigned getPointerAddressSpace() const {
353 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const StoreInst *) { return true; }
358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
365 // Shadow Instruction::setInstructionSubclassData with a private forwarding
366 // method so that subclasses cannot accidentally use it.
367 void setInstructionSubclassData(unsigned short D) {
368 Instruction::setInstructionSubclassData(D);
373 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
376 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
378 //===----------------------------------------------------------------------===//
380 //===----------------------------------------------------------------------===//
382 /// FenceInst - an instruction for ordering other memory operations
384 class FenceInst : public Instruction {
385 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
386 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
388 virtual FenceInst *clone_impl() const;
390 // allocate space for exactly zero operands
391 void *operator new(size_t s) {
392 return User::operator new(s, 0);
395 // Ordering may only be Acquire, Release, AcquireRelease, or
396 // SequentiallyConsistent.
397 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398 SynchronizationScope SynchScope = CrossThread,
399 Instruction *InsertBefore = 0);
400 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
401 SynchronizationScope SynchScope,
402 BasicBlock *InsertAtEnd);
404 /// Returns the ordering effect of this fence.
405 AtomicOrdering getOrdering() const {
406 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
409 /// Set the ordering constraint on this fence. May only be Acquire, Release,
410 /// AcquireRelease, or SequentiallyConsistent.
411 void setOrdering(AtomicOrdering Ordering) {
412 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
416 SynchronizationScope getSynchScope() const {
417 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
420 /// Specify whether this fence orders other operations with respect to all
421 /// concurrently executing threads, or only with respect to signal handlers
422 /// executing in the same thread.
423 void setSynchScope(SynchronizationScope xthread) {
424 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
428 // Methods for support type inquiry through isa, cast, and dyn_cast:
429 static inline bool classof(const FenceInst *) { return true; }
430 static inline bool classof(const Instruction *I) {
431 return I->getOpcode() == Instruction::Fence;
433 static inline bool classof(const Value *V) {
434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
437 // Shadow Instruction::setInstructionSubclassData with a private forwarding
438 // method so that subclasses cannot accidentally use it.
439 void setInstructionSubclassData(unsigned short D) {
440 Instruction::setInstructionSubclassData(D);
444 //===----------------------------------------------------------------------===//
445 // AtomicCmpXchgInst Class
446 //===----------------------------------------------------------------------===//
448 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
449 /// specified value is in a memory location, and, if it is, stores a new value
450 /// there. Returns the value that was loaded.
452 class AtomicCmpXchgInst : public Instruction {
453 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
454 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
455 AtomicOrdering Ordering, SynchronizationScope SynchScope);
457 virtual AtomicCmpXchgInst *clone_impl() const;
459 // allocate space for exactly three operands
460 void *operator new(size_t s) {
461 return User::operator new(s, 3);
463 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
464 AtomicOrdering Ordering, SynchronizationScope SynchScope,
465 Instruction *InsertBefore = 0);
466 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
467 AtomicOrdering Ordering, SynchronizationScope SynchScope,
468 BasicBlock *InsertAtEnd);
470 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
473 bool isVolatile() const {
474 return getSubclassDataFromInstruction() & 1;
477 /// setVolatile - Specify whether this is a volatile cmpxchg.
479 void setVolatile(bool V) {
480 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
484 /// Transparently provide more efficient getOperand methods.
485 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
487 /// Set the ordering constraint on this cmpxchg.
488 void setOrdering(AtomicOrdering Ordering) {
489 assert(Ordering != NotAtomic &&
490 "CmpXchg instructions can only be atomic.");
491 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
495 /// Specify whether this cmpxchg is atomic and orders other operations with
496 /// respect to all concurrently executing threads, or only with respect to
497 /// signal handlers executing in the same thread.
498 void setSynchScope(SynchronizationScope SynchScope) {
499 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
503 /// Returns the ordering constraint on this cmpxchg.
504 AtomicOrdering getOrdering() const {
505 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
508 /// Returns whether this cmpxchg is atomic between threads or only within a
510 SynchronizationScope getSynchScope() const {
511 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
514 Value *getPointerOperand() { return getOperand(0); }
515 const Value *getPointerOperand() const { return getOperand(0); }
516 static unsigned getPointerOperandIndex() { return 0U; }
518 Value *getCompareOperand() { return getOperand(1); }
519 const Value *getCompareOperand() const { return getOperand(1); }
521 Value *getNewValOperand() { return getOperand(2); }
522 const Value *getNewValOperand() const { return getOperand(2); }
524 unsigned getPointerAddressSpace() const {
525 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
528 // Methods for support type inquiry through isa, cast, and dyn_cast:
529 static inline bool classof(const AtomicCmpXchgInst *) { return true; }
530 static inline bool classof(const Instruction *I) {
531 return I->getOpcode() == Instruction::AtomicCmpXchg;
533 static inline bool classof(const Value *V) {
534 return isa<Instruction>(V) && classof(cast<Instruction>(V));
537 // Shadow Instruction::setInstructionSubclassData with a private forwarding
538 // method so that subclasses cannot accidentally use it.
539 void setInstructionSubclassData(unsigned short D) {
540 Instruction::setInstructionSubclassData(D);
545 struct OperandTraits<AtomicCmpXchgInst> :
546 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
549 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
551 //===----------------------------------------------------------------------===//
552 // AtomicRMWInst Class
553 //===----------------------------------------------------------------------===//
555 /// AtomicRMWInst - an instruction that atomically reads a memory location,
556 /// combines it with another value, and then stores the result back. Returns
559 class AtomicRMWInst : public Instruction {
560 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
562 virtual AtomicRMWInst *clone_impl() const;
564 /// This enumeration lists the possible modifications atomicrmw can make. In
565 /// the descriptions, 'p' is the pointer to the instruction's memory location,
566 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
567 /// instruction. These instructions always return 'old'.
583 /// *p = old >signed v ? old : v
585 /// *p = old <signed v ? old : v
587 /// *p = old >unsigned v ? old : v
589 /// *p = old <unsigned v ? old : v
597 // allocate space for exactly two operands
598 void *operator new(size_t s) {
599 return User::operator new(s, 2);
601 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
602 AtomicOrdering Ordering, SynchronizationScope SynchScope,
603 Instruction *InsertBefore = 0);
604 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
605 AtomicOrdering Ordering, SynchronizationScope SynchScope,
606 BasicBlock *InsertAtEnd);
608 BinOp getOperation() const {
609 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
612 void setOperation(BinOp Operation) {
613 unsigned short SubclassData = getSubclassDataFromInstruction();
614 setInstructionSubclassData((SubclassData & 31) |
618 /// isVolatile - Return true if this is a RMW on a volatile memory location.
620 bool isVolatile() const {
621 return getSubclassDataFromInstruction() & 1;
624 /// setVolatile - Specify whether this is a volatile RMW or not.
626 void setVolatile(bool V) {
627 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
631 /// Transparently provide more efficient getOperand methods.
632 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
634 /// Set the ordering constraint on this RMW.
635 void setOrdering(AtomicOrdering Ordering) {
636 assert(Ordering != NotAtomic &&
637 "atomicrmw instructions can only be atomic.");
638 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
642 /// Specify whether this RMW orders other operations with respect to all
643 /// concurrently executing threads, or only with respect to signal handlers
644 /// executing in the same thread.
645 void setSynchScope(SynchronizationScope SynchScope) {
646 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
650 /// Returns the ordering constraint on this RMW.
651 AtomicOrdering getOrdering() const {
652 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
655 /// Returns whether this RMW is atomic between threads or only within a
657 SynchronizationScope getSynchScope() const {
658 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
661 Value *getPointerOperand() { return getOperand(0); }
662 const Value *getPointerOperand() const { return getOperand(0); }
663 static unsigned getPointerOperandIndex() { return 0U; }
665 Value *getValOperand() { return getOperand(1); }
666 const Value *getValOperand() const { return getOperand(1); }
668 unsigned getPointerAddressSpace() const {
669 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
672 // Methods for support type inquiry through isa, cast, and dyn_cast:
673 static inline bool classof(const AtomicRMWInst *) { return true; }
674 static inline bool classof(const Instruction *I) {
675 return I->getOpcode() == Instruction::AtomicRMW;
677 static inline bool classof(const Value *V) {
678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
681 void Init(BinOp Operation, Value *Ptr, Value *Val,
682 AtomicOrdering Ordering, SynchronizationScope SynchScope);
683 // Shadow Instruction::setInstructionSubclassData with a private forwarding
684 // method so that subclasses cannot accidentally use it.
685 void setInstructionSubclassData(unsigned short D) {
686 Instruction::setInstructionSubclassData(D);
691 struct OperandTraits<AtomicRMWInst>
692 : public FixedNumOperandTraits<AtomicRMWInst,2> {
695 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
697 //===----------------------------------------------------------------------===//
698 // GetElementPtrInst Class
699 //===----------------------------------------------------------------------===//
701 // checkGEPType - Simple wrapper function to give a better assertion failure
702 // message on bad indexes for a gep instruction.
704 inline Type *checkGEPType(Type *Ty) {
705 assert(Ty && "Invalid GetElementPtrInst indices for type!");
709 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
710 /// access elements of arrays and structs
712 class GetElementPtrInst : public Instruction {
713 GetElementPtrInst(const GetElementPtrInst &GEPI);
714 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
716 /// Constructors - Create a getelementptr instruction with a base pointer an
717 /// list of indices. The first ctor can optionally insert before an existing
718 /// instruction, the second appends the new instruction to the specified
720 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
721 unsigned Values, const Twine &NameStr,
722 Instruction *InsertBefore);
723 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
724 unsigned Values, const Twine &NameStr,
725 BasicBlock *InsertAtEnd);
727 virtual GetElementPtrInst *clone_impl() const;
729 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
730 const Twine &NameStr = "",
731 Instruction *InsertBefore = 0) {
732 unsigned Values = 1 + unsigned(IdxList.size());
734 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
736 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
737 const Twine &NameStr,
738 BasicBlock *InsertAtEnd) {
739 unsigned Values = 1 + unsigned(IdxList.size());
741 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
744 /// Create an "inbounds" getelementptr. See the documentation for the
745 /// "inbounds" flag in LangRef.html for details.
746 static GetElementPtrInst *CreateInBounds(Value *Ptr,
747 ArrayRef<Value *> IdxList,
748 const Twine &NameStr = "",
749 Instruction *InsertBefore = 0) {
750 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
751 GEP->setIsInBounds(true);
754 static GetElementPtrInst *CreateInBounds(Value *Ptr,
755 ArrayRef<Value *> IdxList,
756 const Twine &NameStr,
757 BasicBlock *InsertAtEnd) {
758 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
759 GEP->setIsInBounds(true);
763 /// Transparently provide more efficient getOperand methods.
764 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
766 // getType - Overload to return most specific pointer type...
767 PointerType *getType() const {
768 return reinterpret_cast<PointerType*>(Instruction::getType());
771 /// getIndexedType - Returns the type of the element that would be loaded with
772 /// a load instruction with the specified parameters.
774 /// Null is returned if the indices are invalid for the specified
777 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
778 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
779 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
781 /// getIndexedType - Returns the address space used by the GEP pointer.
783 static unsigned getAddressSpace(Value *Ptr);
785 inline op_iterator idx_begin() { return op_begin()+1; }
786 inline const_op_iterator idx_begin() const { return op_begin()+1; }
787 inline op_iterator idx_end() { return op_end(); }
788 inline const_op_iterator idx_end() const { return op_end(); }
790 Value *getPointerOperand() {
791 return getOperand(0);
793 const Value *getPointerOperand() const {
794 return getOperand(0);
796 static unsigned getPointerOperandIndex() {
797 return 0U; // get index for modifying correct operand.
800 unsigned getPointerAddressSpace() const {
801 return cast<PointerType>(getType())->getAddressSpace();
804 /// getPointerOperandType - Method to return the pointer operand as a
806 Type *getPointerOperandType() const {
807 return getPointerOperand()->getType();
810 /// GetGEPReturnType - Returns the pointer type returned by the GEP
811 /// instruction, which may be a vector of pointers.
812 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
813 Type *PtrTy = PointerType::get(checkGEPType(
814 getIndexedType(Ptr->getType(), IdxList)),
815 getAddressSpace(Ptr));
817 if (Ptr->getType()->isVectorTy()) {
818 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
819 return VectorType::get(PtrTy, NumElem);
826 unsigned getNumIndices() const { // Note: always non-negative
827 return getNumOperands() - 1;
830 bool hasIndices() const {
831 return getNumOperands() > 1;
834 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
835 /// zeros. If so, the result pointer and the first operand have the same
836 /// value, just potentially different types.
837 bool hasAllZeroIndices() const;
839 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
840 /// constant integers. If so, the result pointer and the first operand have
841 /// a constant offset between them.
842 bool hasAllConstantIndices() const;
844 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
845 /// See LangRef.html for the meaning of inbounds on a getelementptr.
846 void setIsInBounds(bool b = true);
848 /// isInBounds - Determine whether the GEP has the inbounds flag.
849 bool isInBounds() const;
851 // Methods for support type inquiry through isa, cast, and dyn_cast:
852 static inline bool classof(const GetElementPtrInst *) { return true; }
853 static inline bool classof(const Instruction *I) {
854 return (I->getOpcode() == Instruction::GetElementPtr);
856 static inline bool classof(const Value *V) {
857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
862 struct OperandTraits<GetElementPtrInst> :
863 public VariadicOperandTraits<GetElementPtrInst, 1> {
866 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
867 ArrayRef<Value *> IdxList,
869 const Twine &NameStr,
870 Instruction *InsertBefore)
871 : Instruction(getGEPReturnType(Ptr, IdxList),
873 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
874 Values, InsertBefore) {
875 init(Ptr, IdxList, NameStr);
877 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
878 ArrayRef<Value *> IdxList,
880 const Twine &NameStr,
881 BasicBlock *InsertAtEnd)
882 : Instruction(getGEPReturnType(Ptr, IdxList),
884 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
885 Values, InsertAtEnd) {
886 init(Ptr, IdxList, NameStr);
890 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
893 //===----------------------------------------------------------------------===//
895 //===----------------------------------------------------------------------===//
897 /// This instruction compares its operands according to the predicate given
898 /// to the constructor. It only operates on integers or pointers. The operands
899 /// must be identical types.
900 /// @brief Represent an integer comparison operator.
901 class ICmpInst: public CmpInst {
903 /// @brief Clone an identical ICmpInst
904 virtual ICmpInst *clone_impl() const;
906 /// @brief Constructor with insert-before-instruction semantics.
908 Instruction *InsertBefore, ///< Where to insert
909 Predicate pred, ///< The predicate to use for the comparison
910 Value *LHS, ///< The left-hand-side of the expression
911 Value *RHS, ///< The right-hand-side of the expression
912 const Twine &NameStr = "" ///< Name of the instruction
913 ) : CmpInst(makeCmpResultType(LHS->getType()),
914 Instruction::ICmp, pred, LHS, RHS, NameStr,
916 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
917 pred <= CmpInst::LAST_ICMP_PREDICATE &&
918 "Invalid ICmp predicate value");
919 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
920 "Both operands to ICmp instruction are not of the same type!");
921 // Check that the operands are the right type
922 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
923 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
924 "Invalid operand types for ICmp instruction");
927 /// @brief Constructor with insert-at-end semantics.
929 BasicBlock &InsertAtEnd, ///< Block to insert into.
930 Predicate pred, ///< The predicate to use for the comparison
931 Value *LHS, ///< The left-hand-side of the expression
932 Value *RHS, ///< The right-hand-side of the expression
933 const Twine &NameStr = "" ///< Name of the instruction
934 ) : CmpInst(makeCmpResultType(LHS->getType()),
935 Instruction::ICmp, pred, LHS, RHS, NameStr,
937 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
938 pred <= CmpInst::LAST_ICMP_PREDICATE &&
939 "Invalid ICmp predicate value");
940 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
941 "Both operands to ICmp instruction are not of the same type!");
942 // Check that the operands are the right type
943 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
944 getOperand(0)->getType()->isPointerTy()) &&
945 "Invalid operand types for ICmp instruction");
948 /// @brief Constructor with no-insertion semantics
950 Predicate pred, ///< The predicate to use for the comparison
951 Value *LHS, ///< The left-hand-side of the expression
952 Value *RHS, ///< The right-hand-side of the expression
953 const Twine &NameStr = "" ///< Name of the instruction
954 ) : CmpInst(makeCmpResultType(LHS->getType()),
955 Instruction::ICmp, pred, LHS, RHS, NameStr) {
956 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
957 pred <= CmpInst::LAST_ICMP_PREDICATE &&
958 "Invalid ICmp predicate value");
959 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
960 "Both operands to ICmp instruction are not of the same type!");
961 // Check that the operands are the right type
962 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
963 getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
964 "Invalid operand types for ICmp instruction");
967 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
968 /// @returns the predicate that would be the result if the operand were
969 /// regarded as signed.
970 /// @brief Return the signed version of the predicate
971 Predicate getSignedPredicate() const {
972 return getSignedPredicate(getPredicate());
975 /// This is a static version that you can use without an instruction.
976 /// @brief Return the signed version of the predicate.
977 static Predicate getSignedPredicate(Predicate pred);
979 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
980 /// @returns the predicate that would be the result if the operand were
981 /// regarded as unsigned.
982 /// @brief Return the unsigned version of the predicate
983 Predicate getUnsignedPredicate() const {
984 return getUnsignedPredicate(getPredicate());
987 /// This is a static version that you can use without an instruction.
988 /// @brief Return the unsigned version of the predicate.
989 static Predicate getUnsignedPredicate(Predicate pred);
991 /// isEquality - Return true if this predicate is either EQ or NE. This also
992 /// tests for commutativity.
993 static bool isEquality(Predicate P) {
994 return P == ICMP_EQ || P == ICMP_NE;
997 /// isEquality - Return true if this predicate is either EQ or NE. This also
998 /// tests for commutativity.
999 bool isEquality() const {
1000 return isEquality(getPredicate());
1003 /// @returns true if the predicate of this ICmpInst is commutative
1004 /// @brief Determine if this relation is commutative.
1005 bool isCommutative() const { return isEquality(); }
1007 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1009 bool isRelational() const {
1010 return !isEquality();
1013 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1015 static bool isRelational(Predicate P) {
1016 return !isEquality(P);
1019 /// Initialize a set of values that all satisfy the predicate with C.
1020 /// @brief Make a ConstantRange for a relation with a constant value.
1021 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1023 /// Exchange the two operands to this instruction in such a way that it does
1024 /// not modify the semantics of the instruction. The predicate value may be
1025 /// changed to retain the same result if the predicate is order dependent
1027 /// @brief Swap operands and adjust predicate.
1028 void swapOperands() {
1029 setPredicate(getSwappedPredicate());
1030 Op<0>().swap(Op<1>());
1033 // Methods for support type inquiry through isa, cast, and dyn_cast:
1034 static inline bool classof(const ICmpInst *) { return true; }
1035 static inline bool classof(const Instruction *I) {
1036 return I->getOpcode() == Instruction::ICmp;
1038 static inline bool classof(const Value *V) {
1039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1044 //===----------------------------------------------------------------------===//
1046 //===----------------------------------------------------------------------===//
1048 /// This instruction compares its operands according to the predicate given
1049 /// to the constructor. It only operates on floating point values or packed
1050 /// vectors of floating point values. The operands must be identical types.
1051 /// @brief Represents a floating point comparison operator.
1052 class FCmpInst: public CmpInst {
1054 /// @brief Clone an identical FCmpInst
1055 virtual FCmpInst *clone_impl() const;
1057 /// @brief Constructor with insert-before-instruction semantics.
1059 Instruction *InsertBefore, ///< Where to insert
1060 Predicate pred, ///< The predicate to use for the comparison
1061 Value *LHS, ///< The left-hand-side of the expression
1062 Value *RHS, ///< The right-hand-side of the expression
1063 const Twine &NameStr = "" ///< Name of the instruction
1064 ) : CmpInst(makeCmpResultType(LHS->getType()),
1065 Instruction::FCmp, pred, LHS, RHS, NameStr,
1067 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1068 "Invalid FCmp predicate value");
1069 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1070 "Both operands to FCmp instruction are not of the same type!");
1071 // Check that the operands are the right type
1072 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1073 "Invalid operand types for FCmp instruction");
1076 /// @brief Constructor with insert-at-end semantics.
1078 BasicBlock &InsertAtEnd, ///< Block to insert into.
1079 Predicate pred, ///< The predicate to use for the comparison
1080 Value *LHS, ///< The left-hand-side of the expression
1081 Value *RHS, ///< The right-hand-side of the expression
1082 const Twine &NameStr = "" ///< Name of the instruction
1083 ) : CmpInst(makeCmpResultType(LHS->getType()),
1084 Instruction::FCmp, pred, LHS, RHS, NameStr,
1086 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1087 "Invalid FCmp predicate value");
1088 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1089 "Both operands to FCmp instruction are not of the same type!");
1090 // Check that the operands are the right type
1091 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1092 "Invalid operand types for FCmp instruction");
1095 /// @brief Constructor with no-insertion semantics
1097 Predicate pred, ///< The predicate to use for the comparison
1098 Value *LHS, ///< The left-hand-side of the expression
1099 Value *RHS, ///< The right-hand-side of the expression
1100 const Twine &NameStr = "" ///< Name of the instruction
1101 ) : CmpInst(makeCmpResultType(LHS->getType()),
1102 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1103 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1104 "Invalid FCmp predicate value");
1105 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1106 "Both operands to FCmp instruction are not of the same type!");
1107 // Check that the operands are the right type
1108 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1109 "Invalid operand types for FCmp instruction");
1112 /// @returns true if the predicate of this instruction is EQ or NE.
1113 /// @brief Determine if this is an equality predicate.
1114 bool isEquality() const {
1115 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1116 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1119 /// @returns true if the predicate of this instruction is commutative.
1120 /// @brief Determine if this is a commutative predicate.
1121 bool isCommutative() const {
1122 return isEquality() ||
1123 getPredicate() == FCMP_FALSE ||
1124 getPredicate() == FCMP_TRUE ||
1125 getPredicate() == FCMP_ORD ||
1126 getPredicate() == FCMP_UNO;
1129 /// @returns true if the predicate is relational (not EQ or NE).
1130 /// @brief Determine if this a relational predicate.
1131 bool isRelational() const { return !isEquality(); }
1133 /// Exchange the two operands to this instruction in such a way that it does
1134 /// not modify the semantics of the instruction. The predicate value may be
1135 /// changed to retain the same result if the predicate is order dependent
1137 /// @brief Swap operands and adjust predicate.
1138 void swapOperands() {
1139 setPredicate(getSwappedPredicate());
1140 Op<0>().swap(Op<1>());
1143 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1144 static inline bool classof(const FCmpInst *) { return true; }
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 Return true if this call has the given attribute.
1271 bool hasFnAttr(Attributes N) const {
1272 return paramHasAttr(~0, N);
1275 /// @brief Determine whether the call or the callee has the given attributes.
1276 bool paramHasSExtAttr(unsigned i) const;
1277 bool paramHasZExtAttr(unsigned i) const;
1278 bool paramHasInRegAttr(unsigned i) const;
1279 bool paramHasStructRetAttr(unsigned i) const;
1280 bool paramHasNestAttr(unsigned i) const;
1281 bool paramHasByValAttr(unsigned i) const;
1283 /// @brief Determine whether the call or the callee has the given attribute.
1284 bool paramHasAttr(unsigned i, Attributes attr) const;
1286 /// @brief Extract the alignment for a call or parameter (0=unknown).
1287 unsigned getParamAlignment(unsigned i) const {
1288 return AttributeList.getParamAlignment(i);
1291 /// @brief Return true if the call should not be inlined.
1292 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1293 void setIsNoInline(bool Value = true) {
1294 if (Value) addAttribute(~0, Attribute::NoInline);
1295 else removeAttribute(~0, Attribute::NoInline);
1298 /// @brief Return true if the call can return twice
1299 bool canReturnTwice() const {
1300 return hasFnAttr(Attribute::ReturnsTwice);
1302 void setCanReturnTwice(bool Value = true) {
1303 if (Value) addAttribute(~0, Attribute::ReturnsTwice);
1304 else removeAttribute(~0, Attribute::ReturnsTwice);
1307 /// @brief Determine if the call does not access memory.
1308 bool doesNotAccessMemory() const {
1309 return hasFnAttr(Attribute::ReadNone);
1311 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1312 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1313 else removeAttribute(~0, Attribute::ReadNone);
1316 /// @brief Determine if the call does not access or only reads memory.
1317 bool onlyReadsMemory() const {
1318 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1320 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1321 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1322 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1325 /// @brief Determine if the call cannot return.
1326 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1327 void setDoesNotReturn(bool DoesNotReturn = true) {
1328 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1329 else removeAttribute(~0, Attribute::NoReturn);
1332 /// @brief Determine if the call cannot unwind.
1333 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1334 void setDoesNotThrow(bool DoesNotThrow = true) {
1335 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1336 else removeAttribute(~0, Attribute::NoUnwind);
1339 /// @brief Determine if the call returns a structure through first
1340 /// pointer argument.
1341 bool hasStructRetAttr() const {
1342 // Be friendly and also check the callee.
1343 return paramHasAttr(1, Attribute::StructRet);
1346 /// @brief Determine if any call argument is an aggregate passed by value.
1347 bool hasByValArgument() const {
1348 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1351 /// getCalledFunction - Return the function called, or null if this is an
1352 /// indirect function invocation.
1354 Function *getCalledFunction() const {
1355 return dyn_cast<Function>(Op<-1>());
1358 /// getCalledValue - Get a pointer to the function that is invoked by this
1360 const Value *getCalledValue() const { return Op<-1>(); }
1361 Value *getCalledValue() { return Op<-1>(); }
1363 /// setCalledFunction - Set the function called.
1364 void setCalledFunction(Value* Fn) {
1368 /// isInlineAsm - Check if this call is an inline asm statement.
1369 bool isInlineAsm() const {
1370 return isa<InlineAsm>(Op<-1>());
1373 // Methods for support type inquiry through isa, cast, and dyn_cast:
1374 static inline bool classof(const CallInst *) { return true; }
1375 static inline bool classof(const Instruction *I) {
1376 return I->getOpcode() == Instruction::Call;
1378 static inline bool classof(const Value *V) {
1379 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1382 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1383 // method so that subclasses cannot accidentally use it.
1384 void setInstructionSubclassData(unsigned short D) {
1385 Instruction::setInstructionSubclassData(D);
1390 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1393 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1394 const Twine &NameStr, BasicBlock *InsertAtEnd)
1395 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1396 ->getElementType())->getReturnType(),
1398 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1399 unsigned(Args.size() + 1), InsertAtEnd) {
1400 init(Func, Args, NameStr);
1403 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1404 const Twine &NameStr, Instruction *InsertBefore)
1405 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1406 ->getElementType())->getReturnType(),
1408 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1409 unsigned(Args.size() + 1), InsertBefore) {
1410 init(Func, Args, NameStr);
1414 // Note: if you get compile errors about private methods then
1415 // please update your code to use the high-level operand
1416 // interfaces. See line 943 above.
1417 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1419 //===----------------------------------------------------------------------===//
1421 //===----------------------------------------------------------------------===//
1423 /// SelectInst - This class represents the LLVM 'select' instruction.
1425 class SelectInst : public Instruction {
1426 void init(Value *C, Value *S1, Value *S2) {
1427 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1433 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1434 Instruction *InsertBefore)
1435 : Instruction(S1->getType(), Instruction::Select,
1436 &Op<0>(), 3, InsertBefore) {
1440 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1441 BasicBlock *InsertAtEnd)
1442 : Instruction(S1->getType(), Instruction::Select,
1443 &Op<0>(), 3, InsertAtEnd) {
1448 virtual SelectInst *clone_impl() const;
1450 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1451 const Twine &NameStr = "",
1452 Instruction *InsertBefore = 0) {
1453 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1455 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1456 const Twine &NameStr,
1457 BasicBlock *InsertAtEnd) {
1458 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1461 const Value *getCondition() const { return Op<0>(); }
1462 const Value *getTrueValue() const { return Op<1>(); }
1463 const Value *getFalseValue() const { return Op<2>(); }
1464 Value *getCondition() { return Op<0>(); }
1465 Value *getTrueValue() { return Op<1>(); }
1466 Value *getFalseValue() { return Op<2>(); }
1468 /// areInvalidOperands - Return a string if the specified operands are invalid
1469 /// for a select operation, otherwise return null.
1470 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1472 /// Transparently provide more efficient getOperand methods.
1473 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1475 OtherOps getOpcode() const {
1476 return static_cast<OtherOps>(Instruction::getOpcode());
1479 // Methods for support type inquiry through isa, cast, and dyn_cast:
1480 static inline bool classof(const SelectInst *) { return true; }
1481 static inline bool classof(const Instruction *I) {
1482 return I->getOpcode() == Instruction::Select;
1484 static inline bool classof(const Value *V) {
1485 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1490 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1493 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1495 //===----------------------------------------------------------------------===//
1497 //===----------------------------------------------------------------------===//
1499 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1500 /// an argument of the specified type given a va_list and increments that list
1502 class VAArgInst : public UnaryInstruction {
1504 virtual VAArgInst *clone_impl() const;
1507 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1508 Instruction *InsertBefore = 0)
1509 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1512 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1513 BasicBlock *InsertAtEnd)
1514 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1518 Value *getPointerOperand() { return getOperand(0); }
1519 const Value *getPointerOperand() const { return getOperand(0); }
1520 static unsigned getPointerOperandIndex() { return 0U; }
1522 // Methods for support type inquiry through isa, cast, and dyn_cast:
1523 static inline bool classof(const VAArgInst *) { return true; }
1524 static inline bool classof(const Instruction *I) {
1525 return I->getOpcode() == VAArg;
1527 static inline bool classof(const Value *V) {
1528 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1532 //===----------------------------------------------------------------------===//
1533 // ExtractElementInst Class
1534 //===----------------------------------------------------------------------===//
1536 /// ExtractElementInst - This instruction extracts a single (scalar)
1537 /// element from a VectorType value
1539 class ExtractElementInst : public Instruction {
1540 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1541 Instruction *InsertBefore = 0);
1542 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1543 BasicBlock *InsertAtEnd);
1545 virtual ExtractElementInst *clone_impl() const;
1548 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1549 const Twine &NameStr = "",
1550 Instruction *InsertBefore = 0) {
1551 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1553 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1554 const Twine &NameStr,
1555 BasicBlock *InsertAtEnd) {
1556 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1559 /// isValidOperands - Return true if an extractelement instruction can be
1560 /// formed with the specified operands.
1561 static bool isValidOperands(const Value *Vec, const Value *Idx);
1563 Value *getVectorOperand() { return Op<0>(); }
1564 Value *getIndexOperand() { return Op<1>(); }
1565 const Value *getVectorOperand() const { return Op<0>(); }
1566 const Value *getIndexOperand() const { return Op<1>(); }
1568 VectorType *getVectorOperandType() const {
1569 return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1573 /// Transparently provide more efficient getOperand methods.
1574 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1576 // Methods for support type inquiry through isa, cast, and dyn_cast:
1577 static inline bool classof(const ExtractElementInst *) { return true; }
1578 static inline bool classof(const Instruction *I) {
1579 return I->getOpcode() == Instruction::ExtractElement;
1581 static inline bool classof(const Value *V) {
1582 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1587 struct OperandTraits<ExtractElementInst> :
1588 public FixedNumOperandTraits<ExtractElementInst, 2> {
1591 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1593 //===----------------------------------------------------------------------===//
1594 // InsertElementInst Class
1595 //===----------------------------------------------------------------------===//
1597 /// InsertElementInst - This instruction inserts a single (scalar)
1598 /// element into a VectorType value
1600 class InsertElementInst : public Instruction {
1601 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1602 const Twine &NameStr = "",
1603 Instruction *InsertBefore = 0);
1604 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1605 const Twine &NameStr, BasicBlock *InsertAtEnd);
1607 virtual InsertElementInst *clone_impl() const;
1610 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1611 const Twine &NameStr = "",
1612 Instruction *InsertBefore = 0) {
1613 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1615 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1616 const Twine &NameStr,
1617 BasicBlock *InsertAtEnd) {
1618 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1621 /// isValidOperands - Return true if an insertelement instruction can be
1622 /// formed with the specified operands.
1623 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1626 /// getType - Overload to return most specific vector type.
1628 VectorType *getType() const {
1629 return reinterpret_cast<VectorType*>(Instruction::getType());
1632 /// Transparently provide more efficient getOperand methods.
1633 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1635 // Methods for support type inquiry through isa, cast, and dyn_cast:
1636 static inline bool classof(const InsertElementInst *) { return true; }
1637 static inline bool classof(const Instruction *I) {
1638 return I->getOpcode() == Instruction::InsertElement;
1640 static inline bool classof(const Value *V) {
1641 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1646 struct OperandTraits<InsertElementInst> :
1647 public FixedNumOperandTraits<InsertElementInst, 3> {
1650 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1652 //===----------------------------------------------------------------------===//
1653 // ShuffleVectorInst Class
1654 //===----------------------------------------------------------------------===//
1656 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1659 class ShuffleVectorInst : public Instruction {
1661 virtual ShuffleVectorInst *clone_impl() const;
1664 // allocate space for exactly three operands
1665 void *operator new(size_t s) {
1666 return User::operator new(s, 3);
1668 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1669 const Twine &NameStr = "",
1670 Instruction *InsertBefor = 0);
1671 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1672 const Twine &NameStr, BasicBlock *InsertAtEnd);
1674 /// isValidOperands - Return true if a shufflevector instruction can be
1675 /// formed with the specified operands.
1676 static bool isValidOperands(const Value *V1, const Value *V2,
1679 /// getType - Overload to return most specific vector type.
1681 VectorType *getType() const {
1682 return reinterpret_cast<VectorType*>(Instruction::getType());
1685 /// Transparently provide more efficient getOperand methods.
1686 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1688 Constant *getMask() const {
1689 return reinterpret_cast<Constant*>(getOperand(2));
1692 /// getMaskValue - Return the index from the shuffle mask for the specified
1693 /// output result. This is either -1 if the element is undef or a number less
1694 /// than 2*numelements.
1695 static int getMaskValue(Constant *Mask, unsigned i);
1697 int getMaskValue(unsigned i) const {
1698 return getMaskValue(getMask(), i);
1701 /// getShuffleMask - Return the full mask for this instruction, where each
1702 /// element is the element number and undef's are returned as -1.
1703 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1705 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1706 return getShuffleMask(getMask(), Result);
1709 SmallVector<int, 16> getShuffleMask() const {
1710 SmallVector<int, 16> Mask;
1711 getShuffleMask(Mask);
1716 // Methods for support type inquiry through isa, cast, and dyn_cast:
1717 static inline bool classof(const ShuffleVectorInst *) { return true; }
1718 static inline bool classof(const Instruction *I) {
1719 return I->getOpcode() == Instruction::ShuffleVector;
1721 static inline bool classof(const Value *V) {
1722 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1727 struct OperandTraits<ShuffleVectorInst> :
1728 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1731 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1733 //===----------------------------------------------------------------------===//
1734 // ExtractValueInst Class
1735 //===----------------------------------------------------------------------===//
1737 /// ExtractValueInst - This instruction extracts a struct member or array
1738 /// element value from an aggregate value.
1740 class ExtractValueInst : public UnaryInstruction {
1741 SmallVector<unsigned, 4> Indices;
1743 ExtractValueInst(const ExtractValueInst &EVI);
1744 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1746 /// Constructors - Create a extractvalue instruction with a base aggregate
1747 /// value and a list of indices. The first ctor can optionally insert before
1748 /// an existing instruction, the second appends the new instruction to the
1749 /// specified BasicBlock.
1750 inline ExtractValueInst(Value *Agg,
1751 ArrayRef<unsigned> Idxs,
1752 const Twine &NameStr,
1753 Instruction *InsertBefore);
1754 inline ExtractValueInst(Value *Agg,
1755 ArrayRef<unsigned> Idxs,
1756 const Twine &NameStr, BasicBlock *InsertAtEnd);
1758 // allocate space for exactly one operand
1759 void *operator new(size_t s) {
1760 return User::operator new(s, 1);
1763 virtual ExtractValueInst *clone_impl() const;
1766 static ExtractValueInst *Create(Value *Agg,
1767 ArrayRef<unsigned> Idxs,
1768 const Twine &NameStr = "",
1769 Instruction *InsertBefore = 0) {
1771 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1773 static ExtractValueInst *Create(Value *Agg,
1774 ArrayRef<unsigned> Idxs,
1775 const Twine &NameStr,
1776 BasicBlock *InsertAtEnd) {
1777 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1780 /// getIndexedType - Returns the type of the element that would be extracted
1781 /// with an extractvalue instruction with the specified parameters.
1783 /// Null is returned if the indices are invalid for the specified type.
1784 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1786 typedef const unsigned* idx_iterator;
1787 inline idx_iterator idx_begin() const { return Indices.begin(); }
1788 inline idx_iterator idx_end() const { return Indices.end(); }
1790 Value *getAggregateOperand() {
1791 return getOperand(0);
1793 const Value *getAggregateOperand() const {
1794 return getOperand(0);
1796 static unsigned getAggregateOperandIndex() {
1797 return 0U; // get index for modifying correct operand
1800 ArrayRef<unsigned> getIndices() const {
1804 unsigned getNumIndices() const {
1805 return (unsigned)Indices.size();
1808 bool hasIndices() const {
1812 // Methods for support type inquiry through isa, cast, and dyn_cast:
1813 static inline bool classof(const ExtractValueInst *) { return true; }
1814 static inline bool classof(const Instruction *I) {
1815 return I->getOpcode() == Instruction::ExtractValue;
1817 static inline bool classof(const Value *V) {
1818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1822 ExtractValueInst::ExtractValueInst(Value *Agg,
1823 ArrayRef<unsigned> Idxs,
1824 const Twine &NameStr,
1825 Instruction *InsertBefore)
1826 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1827 ExtractValue, Agg, InsertBefore) {
1828 init(Idxs, NameStr);
1830 ExtractValueInst::ExtractValueInst(Value *Agg,
1831 ArrayRef<unsigned> Idxs,
1832 const Twine &NameStr,
1833 BasicBlock *InsertAtEnd)
1834 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1835 ExtractValue, Agg, InsertAtEnd) {
1836 init(Idxs, NameStr);
1840 //===----------------------------------------------------------------------===//
1841 // InsertValueInst Class
1842 //===----------------------------------------------------------------------===//
1844 /// InsertValueInst - This instruction inserts a struct field of array element
1845 /// value into an aggregate value.
1847 class InsertValueInst : public Instruction {
1848 SmallVector<unsigned, 4> Indices;
1850 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1851 InsertValueInst(const InsertValueInst &IVI);
1852 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1853 const Twine &NameStr);
1855 /// Constructors - Create a insertvalue instruction with a base aggregate
1856 /// value, a value to insert, and a list of indices. The first ctor can
1857 /// optionally insert before an existing instruction, the second appends
1858 /// the new instruction to the specified BasicBlock.
1859 inline InsertValueInst(Value *Agg, Value *Val,
1860 ArrayRef<unsigned> Idxs,
1861 const Twine &NameStr,
1862 Instruction *InsertBefore);
1863 inline InsertValueInst(Value *Agg, Value *Val,
1864 ArrayRef<unsigned> Idxs,
1865 const Twine &NameStr, BasicBlock *InsertAtEnd);
1867 /// Constructors - These two constructors are convenience methods because one
1868 /// and two index insertvalue instructions are so common.
1869 InsertValueInst(Value *Agg, Value *Val,
1870 unsigned Idx, const Twine &NameStr = "",
1871 Instruction *InsertBefore = 0);
1872 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1873 const Twine &NameStr, BasicBlock *InsertAtEnd);
1875 virtual InsertValueInst *clone_impl() const;
1877 // allocate space for exactly two operands
1878 void *operator new(size_t s) {
1879 return User::operator new(s, 2);
1882 static InsertValueInst *Create(Value *Agg, Value *Val,
1883 ArrayRef<unsigned> Idxs,
1884 const Twine &NameStr = "",
1885 Instruction *InsertBefore = 0) {
1886 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1888 static InsertValueInst *Create(Value *Agg, Value *Val,
1889 ArrayRef<unsigned> Idxs,
1890 const Twine &NameStr,
1891 BasicBlock *InsertAtEnd) {
1892 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1895 /// Transparently provide more efficient getOperand methods.
1896 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1898 typedef const unsigned* idx_iterator;
1899 inline idx_iterator idx_begin() const { return Indices.begin(); }
1900 inline idx_iterator idx_end() const { return Indices.end(); }
1902 Value *getAggregateOperand() {
1903 return getOperand(0);
1905 const Value *getAggregateOperand() const {
1906 return getOperand(0);
1908 static unsigned getAggregateOperandIndex() {
1909 return 0U; // get index for modifying correct operand
1912 Value *getInsertedValueOperand() {
1913 return getOperand(1);
1915 const Value *getInsertedValueOperand() const {
1916 return getOperand(1);
1918 static unsigned getInsertedValueOperandIndex() {
1919 return 1U; // get index for modifying correct operand
1922 ArrayRef<unsigned> getIndices() const {
1926 unsigned getNumIndices() const {
1927 return (unsigned)Indices.size();
1930 bool hasIndices() const {
1934 // Methods for support type inquiry through isa, cast, and dyn_cast:
1935 static inline bool classof(const InsertValueInst *) { return true; }
1936 static inline bool classof(const Instruction *I) {
1937 return I->getOpcode() == Instruction::InsertValue;
1939 static inline bool classof(const Value *V) {
1940 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1945 struct OperandTraits<InsertValueInst> :
1946 public FixedNumOperandTraits<InsertValueInst, 2> {
1949 InsertValueInst::InsertValueInst(Value *Agg,
1951 ArrayRef<unsigned> Idxs,
1952 const Twine &NameStr,
1953 Instruction *InsertBefore)
1954 : Instruction(Agg->getType(), InsertValue,
1955 OperandTraits<InsertValueInst>::op_begin(this),
1957 init(Agg, Val, Idxs, NameStr);
1959 InsertValueInst::InsertValueInst(Value *Agg,
1961 ArrayRef<unsigned> Idxs,
1962 const Twine &NameStr,
1963 BasicBlock *InsertAtEnd)
1964 : Instruction(Agg->getType(), InsertValue,
1965 OperandTraits<InsertValueInst>::op_begin(this),
1967 init(Agg, Val, Idxs, NameStr);
1970 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1972 //===----------------------------------------------------------------------===//
1974 //===----------------------------------------------------------------------===//
1976 // PHINode - The PHINode class is used to represent the magical mystical PHI
1977 // node, that can not exist in nature, but can be synthesized in a computer
1978 // scientist's overactive imagination.
1980 class PHINode : public Instruction {
1981 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1982 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1983 /// the number actually in use.
1984 unsigned ReservedSpace;
1985 PHINode(const PHINode &PN);
1986 // allocate space for exactly zero operands
1987 void *operator new(size_t s) {
1988 return User::operator new(s, 0);
1990 explicit PHINode(Type *Ty, unsigned NumReservedValues,
1991 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1992 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1993 ReservedSpace(NumReservedValues) {
1995 OperandList = allocHungoffUses(ReservedSpace);
1998 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1999 BasicBlock *InsertAtEnd)
2000 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
2001 ReservedSpace(NumReservedValues) {
2003 OperandList = allocHungoffUses(ReservedSpace);
2006 // allocHungoffUses - this is more complicated than the generic
2007 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2008 // values and pointers to the incoming blocks, all in one allocation.
2009 Use *allocHungoffUses(unsigned) const;
2011 virtual PHINode *clone_impl() const;
2013 /// Constructors - NumReservedValues is a hint for the number of incoming
2014 /// edges that this phi node will have (use 0 if you really have no idea).
2015 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2016 const Twine &NameStr = "",
2017 Instruction *InsertBefore = 0) {
2018 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2020 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2021 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2022 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2026 /// Provide fast operand accessors
2027 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2029 // Block iterator interface. This provides access to the list of incoming
2030 // basic blocks, which parallels the list of incoming values.
2032 typedef BasicBlock **block_iterator;
2033 typedef BasicBlock * const *const_block_iterator;
2035 block_iterator block_begin() {
2037 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2038 return reinterpret_cast<block_iterator>(ref + 1);
2041 const_block_iterator block_begin() const {
2042 const Use::UserRef *ref =
2043 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2044 return reinterpret_cast<const_block_iterator>(ref + 1);
2047 block_iterator block_end() {
2048 return block_begin() + getNumOperands();
2051 const_block_iterator block_end() const {
2052 return block_begin() + getNumOperands();
2055 /// getNumIncomingValues - Return the number of incoming edges
2057 unsigned getNumIncomingValues() const { return getNumOperands(); }
2059 /// getIncomingValue - Return incoming value number x
2061 Value *getIncomingValue(unsigned i) const {
2062 return getOperand(i);
2064 void setIncomingValue(unsigned i, Value *V) {
2067 static unsigned getOperandNumForIncomingValue(unsigned i) {
2070 static unsigned getIncomingValueNumForOperand(unsigned i) {
2074 /// getIncomingBlock - Return incoming basic block number @p i.
2076 BasicBlock *getIncomingBlock(unsigned i) const {
2077 return block_begin()[i];
2080 /// getIncomingBlock - Return incoming basic block corresponding
2081 /// to an operand of the PHI.
2083 BasicBlock *getIncomingBlock(const Use &U) const {
2084 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2085 return getIncomingBlock(unsigned(&U - op_begin()));
2088 /// getIncomingBlock - Return incoming basic block corresponding
2089 /// to value use iterator.
2091 template <typename U>
2092 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2093 return getIncomingBlock(I.getUse());
2096 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2097 block_begin()[i] = BB;
2100 /// addIncoming - Add an incoming value to the end of the PHI list
2102 void addIncoming(Value *V, BasicBlock *BB) {
2103 assert(V && "PHI node got a null value!");
2104 assert(BB && "PHI node got a null basic block!");
2105 assert(getType() == V->getType() &&
2106 "All operands to PHI node must be the same type as the PHI node!");
2107 if (NumOperands == ReservedSpace)
2108 growOperands(); // Get more space!
2109 // Initialize some new operands.
2111 setIncomingValue(NumOperands - 1, V);
2112 setIncomingBlock(NumOperands - 1, BB);
2115 /// removeIncomingValue - Remove an incoming value. This is useful if a
2116 /// predecessor basic block is deleted. The value removed is returned.
2118 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2119 /// is true), the PHI node is destroyed and any uses of it are replaced with
2120 /// dummy values. The only time there should be zero incoming values to a PHI
2121 /// node is when the block is dead, so this strategy is sound.
2123 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2125 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2126 int Idx = getBasicBlockIndex(BB);
2127 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2128 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2131 /// getBasicBlockIndex - Return the first index of the specified basic
2132 /// block in the value list for this PHI. Returns -1 if no instance.
2134 int getBasicBlockIndex(const BasicBlock *BB) const {
2135 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2136 if (block_begin()[i] == BB)
2141 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2142 int Idx = getBasicBlockIndex(BB);
2143 assert(Idx >= 0 && "Invalid basic block argument!");
2144 return getIncomingValue(Idx);
2147 /// hasConstantValue - If the specified PHI node always merges together the
2148 /// same value, return the value, otherwise return null.
2149 Value *hasConstantValue() const;
2151 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2152 static inline bool classof(const PHINode *) { return true; }
2153 static inline bool classof(const Instruction *I) {
2154 return I->getOpcode() == Instruction::PHI;
2156 static inline bool classof(const Value *V) {
2157 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2160 void growOperands();
2164 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2167 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2169 //===----------------------------------------------------------------------===//
2170 // LandingPadInst Class
2171 //===----------------------------------------------------------------------===//
2173 //===---------------------------------------------------------------------------
2174 /// LandingPadInst - The landingpad instruction holds all of the information
2175 /// necessary to generate correct exception handling. The landingpad instruction
2176 /// cannot be moved from the top of a landing pad block, which itself is
2177 /// accessible only from the 'unwind' edge of an invoke. This uses the
2178 /// SubclassData field in Value to store whether or not the landingpad is a
2181 class LandingPadInst : public Instruction {
2182 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2183 /// the number actually in use.
2184 unsigned ReservedSpace;
2185 LandingPadInst(const LandingPadInst &LP);
2187 enum ClauseType { Catch, Filter };
2189 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2190 // Allocate space for exactly zero operands.
2191 void *operator new(size_t s) {
2192 return User::operator new(s, 0);
2194 void growOperands(unsigned Size);
2195 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2197 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2198 unsigned NumReservedValues, const Twine &NameStr,
2199 Instruction *InsertBefore);
2200 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2201 unsigned NumReservedValues, const Twine &NameStr,
2202 BasicBlock *InsertAtEnd);
2204 virtual LandingPadInst *clone_impl() const;
2206 /// Constructors - NumReservedClauses is a hint for the number of incoming
2207 /// clauses that this landingpad will have (use 0 if you really have no idea).
2208 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2209 unsigned NumReservedClauses,
2210 const Twine &NameStr = "",
2211 Instruction *InsertBefore = 0);
2212 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2213 unsigned NumReservedClauses,
2214 const Twine &NameStr, BasicBlock *InsertAtEnd);
2217 /// Provide fast operand accessors
2218 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2220 /// getPersonalityFn - Get the personality function associated with this
2222 Value *getPersonalityFn() const { return getOperand(0); }
2224 /// isCleanup - Return 'true' if this landingpad instruction is a
2225 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2226 /// doesn't catch the exception.
2227 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2229 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2230 void setCleanup(bool V) {
2231 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2235 /// addClause - Add a catch or filter clause to the landing pad.
2236 void addClause(Value *ClauseVal);
2238 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2239 /// to determine what type of clause this is.
2240 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2242 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2243 bool isCatch(unsigned Idx) const {
2244 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2247 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2248 bool isFilter(unsigned Idx) const {
2249 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2252 /// getNumClauses - Get the number of clauses for this landing pad.
2253 unsigned getNumClauses() const { return getNumOperands() - 1; }
2255 /// reserveClauses - Grow the size of the operand list to accommodate the new
2256 /// number of clauses.
2257 void reserveClauses(unsigned Size) { growOperands(Size); }
2259 // Methods for support type inquiry through isa, cast, and dyn_cast:
2260 static inline bool classof(const LandingPadInst *) { return true; }
2261 static inline bool classof(const Instruction *I) {
2262 return I->getOpcode() == Instruction::LandingPad;
2264 static inline bool classof(const Value *V) {
2265 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2270 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2275 //===----------------------------------------------------------------------===//
2277 //===----------------------------------------------------------------------===//
2279 //===---------------------------------------------------------------------------
2280 /// ReturnInst - Return a value (possibly void), from a function. Execution
2281 /// does not continue in this function any longer.
2283 class ReturnInst : public TerminatorInst {
2284 ReturnInst(const ReturnInst &RI);
2287 // ReturnInst constructors:
2288 // ReturnInst() - 'ret void' instruction
2289 // ReturnInst( null) - 'ret void' instruction
2290 // ReturnInst(Value* X) - 'ret X' instruction
2291 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2292 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2293 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2294 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2296 // NOTE: If the Value* passed is of type void then the constructor behaves as
2297 // if it was passed NULL.
2298 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2299 Instruction *InsertBefore = 0);
2300 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2301 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2303 virtual ReturnInst *clone_impl() const;
2305 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2306 Instruction *InsertBefore = 0) {
2307 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2309 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2310 BasicBlock *InsertAtEnd) {
2311 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2313 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2314 return new(0) ReturnInst(C, InsertAtEnd);
2316 virtual ~ReturnInst();
2318 /// Provide fast operand accessors
2319 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2321 /// Convenience accessor. Returns null if there is no return value.
2322 Value *getReturnValue() const {
2323 return getNumOperands() != 0 ? getOperand(0) : 0;
2326 unsigned getNumSuccessors() const { return 0; }
2328 // Methods for support type inquiry through isa, cast, and dyn_cast:
2329 static inline bool classof(const ReturnInst *) { return true; }
2330 static inline bool classof(const Instruction *I) {
2331 return (I->getOpcode() == Instruction::Ret);
2333 static inline bool classof(const Value *V) {
2334 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2337 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2338 virtual unsigned getNumSuccessorsV() const;
2339 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2343 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2346 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2348 //===----------------------------------------------------------------------===//
2350 //===----------------------------------------------------------------------===//
2352 //===---------------------------------------------------------------------------
2353 /// BranchInst - Conditional or Unconditional Branch instruction.
2355 class BranchInst : public TerminatorInst {
2356 /// Ops list - Branches are strange. The operands are ordered:
2357 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2358 /// they don't have to check for cond/uncond branchness. These are mostly
2359 /// accessed relative from op_end().
2360 BranchInst(const BranchInst &BI);
2362 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2363 // BranchInst(BB *B) - 'br B'
2364 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2365 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2366 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2367 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2368 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2369 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2370 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2371 Instruction *InsertBefore = 0);
2372 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2373 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2374 BasicBlock *InsertAtEnd);
2376 virtual BranchInst *clone_impl() const;
2378 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2379 return new(1) BranchInst(IfTrue, InsertBefore);
2381 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2382 Value *Cond, Instruction *InsertBefore = 0) {
2383 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2385 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2386 return new(1) BranchInst(IfTrue, InsertAtEnd);
2388 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2389 Value *Cond, BasicBlock *InsertAtEnd) {
2390 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2393 /// Transparently provide more efficient getOperand methods.
2394 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2396 bool isUnconditional() const { return getNumOperands() == 1; }
2397 bool isConditional() const { return getNumOperands() == 3; }
2399 Value *getCondition() const {
2400 assert(isConditional() && "Cannot get condition of an uncond branch!");
2404 void setCondition(Value *V) {
2405 assert(isConditional() && "Cannot set condition of unconditional branch!");
2409 unsigned getNumSuccessors() const { return 1+isConditional(); }
2411 BasicBlock *getSuccessor(unsigned i) const {
2412 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2413 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2416 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2417 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2418 *(&Op<-1>() - idx) = (Value*)NewSucc;
2421 /// \brief Swap the successors of this branch instruction.
2423 /// Swaps the successors of the branch instruction. This also swaps any
2424 /// branch weight metadata associated with the instruction so that it
2425 /// continues to map correctly to each operand.
2426 void swapSuccessors();
2428 // Methods for support type inquiry through isa, cast, and dyn_cast:
2429 static inline bool classof(const BranchInst *) { return true; }
2430 static inline bool classof(const Instruction *I) {
2431 return (I->getOpcode() == Instruction::Br);
2433 static inline bool classof(const Value *V) {
2434 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2437 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2438 virtual unsigned getNumSuccessorsV() const;
2439 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2443 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2446 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2448 //===----------------------------------------------------------------------===//
2450 //===----------------------------------------------------------------------===//
2452 //===---------------------------------------------------------------------------
2453 /// SwitchInst - Multiway switch
2455 class SwitchInst : public TerminatorInst {
2456 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2457 unsigned ReservedSpace;
2459 // Operand[0] = Value to switch on
2460 // Operand[1] = Default basic block destination
2461 // Operand[2n ] = Value to match
2462 // Operand[2n+1] = BasicBlock to go to on match
2464 // Store case values separately from operands list. We needn't User-Use
2465 // concept here, since it is just a case value, it will always constant,
2466 // and case value couldn't reused with another instructions/values.
2468 // It allows us to use custom type for case values that is not inherited
2469 // from Value. Since case value is a complex type that implements
2470 // the subset of integers, we needn't extract sub-constants within
2471 // slow getAggregateElement method.
2472 // For case values we will use std::list to by two reasons:
2473 // 1. It allows to add/remove cases without whole collection reallocation.
2474 // 2. In most of cases we needn't random access.
2475 // Currently case values are also stored in Operands List, but it will moved
2476 // out in future commits.
2477 typedef std::list<IntegersSubset> Subsets;
2478 typedef Subsets::iterator SubsetsIt;
2479 typedef Subsets::const_iterator SubsetsConstIt;
2483 SwitchInst(const SwitchInst &SI);
2484 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2485 void growOperands();
2486 // allocate space for exactly zero operands
2487 void *operator new(size_t s) {
2488 return User::operator new(s, 0);
2490 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2491 /// switch on and a default destination. The number of additional cases can
2492 /// be specified here to make memory allocation more efficient. This
2493 /// constructor can also autoinsert before another instruction.
2494 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2495 Instruction *InsertBefore);
2497 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2498 /// switch on and a default destination. The number of additional cases can
2499 /// be specified here to make memory allocation more efficient. This
2500 /// constructor also autoinserts at the end of the specified BasicBlock.
2501 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2502 BasicBlock *InsertAtEnd);
2504 virtual SwitchInst *clone_impl() const;
2507 // FIXME: Currently there are a lot of unclean template parameters,
2508 // we need to make refactoring in future.
2509 // All these parameters are used to implement both iterator and const_iterator
2510 // without code duplication.
2511 // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2512 // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2513 // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2514 // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2515 template <class SwitchInstTy, class ConstantIntTy,
2516 class SubsetsItTy, class BasicBlockTy>
2517 class CaseIteratorT;
2519 typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2520 SubsetsConstIt, const BasicBlock> ConstCaseIt;
2524 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2526 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2527 unsigned NumCases, Instruction *InsertBefore = 0) {
2528 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2530 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2531 unsigned NumCases, BasicBlock *InsertAtEnd) {
2532 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2537 /// Provide fast operand accessors
2538 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2540 // Accessor Methods for Switch stmt
2541 Value *getCondition() const { return getOperand(0); }
2542 void setCondition(Value *V) { setOperand(0, V); }
2544 BasicBlock *getDefaultDest() const {
2545 return cast<BasicBlock>(getOperand(1));
2548 void setDefaultDest(BasicBlock *DefaultCase) {
2549 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2552 /// getNumCases - return the number of 'cases' in this switch instruction,
2553 /// except the default case
2554 unsigned getNumCases() const {
2555 return getNumOperands()/2 - 1;
2558 /// Returns a read/write iterator that points to the first
2559 /// case in SwitchInst.
2560 CaseIt case_begin() {
2561 return CaseIt(this, 0, TheSubsets.begin());
2563 /// Returns a read-only iterator that points to the first
2564 /// case in the SwitchInst.
2565 ConstCaseIt case_begin() const {
2566 return ConstCaseIt(this, 0, TheSubsets.begin());
2569 /// Returns a read/write iterator that points one past the last
2570 /// in the SwitchInst.
2572 return CaseIt(this, getNumCases(), TheSubsets.end());
2574 /// Returns a read-only iterator that points one past the last
2575 /// in the SwitchInst.
2576 ConstCaseIt case_end() const {
2577 return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2579 /// Returns an iterator that points to the default case.
2580 /// Note: this iterator allows to resolve successor only. Attempt
2581 /// to resolve case value causes an assertion.
2582 /// Also note, that increment and decrement also causes an assertion and
2583 /// makes iterator invalid.
2584 CaseIt case_default() {
2585 return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2587 ConstCaseIt case_default() const {
2588 return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2591 /// findCaseValue - Search all of the case values for the specified constant.
2592 /// If it is explicitly handled, return the case iterator of it, otherwise
2593 /// return default case iterator to indicate
2594 /// that it is handled by the default handler.
2595 CaseIt findCaseValue(const ConstantInt *C) {
2596 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2597 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2599 return case_default();
2601 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2602 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2603 if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2605 return case_default();
2608 /// findCaseDest - Finds the unique case value for a given successor. Returns
2609 /// null if the successor is not found, not unique, or is the default case.
2610 ConstantInt *findCaseDest(BasicBlock *BB) {
2611 if (BB == getDefaultDest()) return NULL;
2613 ConstantInt *CI = NULL;
2614 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2615 if (i.getCaseSuccessor() == BB) {
2616 if (CI) return NULL; // Multiple cases lead to BB.
2617 else CI = i.getCaseValue();
2623 /// addCase - Add an entry to the switch instruction...
2626 /// This action invalidates case_end(). Old case_end() iterator will
2627 /// point to the added case.
2628 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2630 /// addCase - Add an entry to the switch instruction.
2632 /// This action invalidates case_end(). Old case_end() iterator will
2633 /// point to the added case.
2634 void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2636 /// removeCase - This method removes the specified case and its successor
2637 /// from the switch instruction. Note that this operation may reorder the
2638 /// remaining cases at index idx and above.
2640 /// This action invalidates iterators for all cases following the one removed,
2641 /// including the case_end() iterator.
2642 void removeCase(CaseIt& i);
2644 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2645 BasicBlock *getSuccessor(unsigned idx) const {
2646 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2647 return cast<BasicBlock>(getOperand(idx*2+1));
2649 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2650 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2651 setOperand(idx*2+1, (Value*)NewSucc);
2654 uint16_t hash() const {
2655 uint32_t NumberOfCases = (uint32_t)getNumCases();
2656 uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2657 for (ConstCaseIt i = case_begin(), e = case_end();
2659 uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2660 Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2665 // Case iterators definition.
2667 template <class SwitchInstTy, class ConstantIntTy,
2668 class SubsetsItTy, class BasicBlockTy>
2669 class CaseIteratorT {
2673 unsigned long Index;
2674 SubsetsItTy SubsetIt;
2676 /// Initializes case iterator for given SwitchInst and for given
2678 friend class SwitchInst;
2679 CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2680 SubsetsItTy CaseValueIt) {
2682 Index = SuccessorIndex;
2683 this->SubsetIt = CaseValueIt;
2687 typedef typename SubsetsItTy::reference IntegersSubsetRef;
2688 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2689 SubsetsItTy, BasicBlockTy> Self;
2691 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2694 SubsetIt = SI->TheSubsets.begin();
2695 std::advance(SubsetIt, CaseNum);
2699 /// Initializes case iterator for given SwitchInst and for given
2700 /// TerminatorInst's successor index.
2701 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2702 assert(SuccessorIndex < SI->getNumSuccessors() &&
2703 "Successor index # out of range!");
2704 return SuccessorIndex != 0 ?
2705 Self(SI, SuccessorIndex - 1) :
2706 Self(SI, DefaultPseudoIndex);
2709 /// Resolves case value for current case.
2711 ConstantIntTy *getCaseValue() {
2712 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2713 IntegersSubsetRef CaseRanges = *SubsetIt;
2715 // FIXME: Currently we work with ConstantInt based cases.
2716 // So return CaseValue as ConstantInt.
2717 return CaseRanges.getSingleNumber(0).toConstantInt();
2720 /// Resolves case value for current case.
2721 IntegersSubsetRef getCaseValueEx() {
2722 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2726 /// Resolves successor for current case.
2727 BasicBlockTy *getCaseSuccessor() {
2728 assert((Index < SI->getNumCases() ||
2729 Index == DefaultPseudoIndex) &&
2730 "Index out the number of cases.");
2731 return SI->getSuccessor(getSuccessorIndex());
2734 /// Returns number of current case.
2735 unsigned getCaseIndex() const { return Index; }
2737 /// Returns TerminatorInst's successor index for current case successor.
2738 unsigned getSuccessorIndex() const {
2739 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2740 "Index out the number of cases.");
2741 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2745 // Check index correctness after increment.
2746 // Note: Index == getNumCases() means end().
2747 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2750 SubsetIt = SI->TheSubsets.begin();
2755 Self operator++(int) {
2761 // Check index correctness after decrement.
2762 // Note: Index == getNumCases() means end().
2763 // Also allow "-1" iterator here. That will became valid after ++.
2764 unsigned NumCases = SI->getNumCases();
2765 assert((Index == 0 || Index-1 <= NumCases) &&
2766 "Index out the number of cases.");
2768 if (Index == NumCases) {
2769 SubsetIt = SI->TheSubsets.end();
2778 Self operator--(int) {
2783 bool operator==(const Self& RHS) const {
2784 assert(RHS.SI == SI && "Incompatible operators.");
2785 return RHS.Index == Index;
2787 bool operator!=(const Self& RHS) const {
2788 assert(RHS.SI == SI && "Incompatible operators.");
2789 return RHS.Index != Index;
2793 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2794 SubsetsIt, BasicBlock> {
2795 typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2799 friend class SwitchInst;
2800 CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2801 ParentTy(SI, CaseNum, SubsetIt) {}
2803 void updateCaseValueOperand(IntegersSubset& V) {
2804 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2809 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2811 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2813 /// Sets the new value for current case.
2815 void setValue(ConstantInt *V) {
2816 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2817 IntegersSubsetToBB Mapping;
2818 // FIXME: Currently we work with ConstantInt based cases.
2819 // So inititalize IntItem container directly from ConstantInt.
2820 Mapping.add(IntItem::fromConstantInt(V));
2821 *SubsetIt = Mapping.getCase();
2822 updateCaseValueOperand(*SubsetIt);
2825 /// Sets the new value for current case.
2826 void setValueEx(IntegersSubset& V) {
2827 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2829 updateCaseValueOperand(*SubsetIt);
2832 /// Sets the new successor for current case.
2833 void setSuccessor(BasicBlock *S) {
2834 SI->setSuccessor(getSuccessorIndex(), S);
2838 // Methods for support type inquiry through isa, cast, and dyn_cast:
2840 static inline bool classof(const SwitchInst *) { return true; }
2841 static inline bool classof(const Instruction *I) {
2842 return I->getOpcode() == Instruction::Switch;
2844 static inline bool classof(const Value *V) {
2845 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2849 virtual unsigned getNumSuccessorsV() const;
2850 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2854 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2857 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2860 //===----------------------------------------------------------------------===//
2861 // IndirectBrInst Class
2862 //===----------------------------------------------------------------------===//
2864 //===---------------------------------------------------------------------------
2865 /// IndirectBrInst - Indirect Branch Instruction.
2867 class IndirectBrInst : public TerminatorInst {
2868 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2869 unsigned ReservedSpace;
2870 // Operand[0] = Value to switch on
2871 // Operand[1] = Default basic block destination
2872 // Operand[2n ] = Value to match
2873 // Operand[2n+1] = BasicBlock to go to on match
2874 IndirectBrInst(const IndirectBrInst &IBI);
2875 void init(Value *Address, unsigned NumDests);
2876 void growOperands();
2877 // allocate space for exactly zero operands
2878 void *operator new(size_t s) {
2879 return User::operator new(s, 0);
2881 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2882 /// Address to jump to. The number of expected destinations can be specified
2883 /// here to make memory allocation more efficient. This constructor can also
2884 /// autoinsert before another instruction.
2885 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2887 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2888 /// Address to jump to. The number of expected destinations can be specified
2889 /// here to make memory allocation more efficient. This constructor also
2890 /// autoinserts at the end of the specified BasicBlock.
2891 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2893 virtual IndirectBrInst *clone_impl() const;
2895 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2896 Instruction *InsertBefore = 0) {
2897 return new IndirectBrInst(Address, NumDests, InsertBefore);
2899 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2900 BasicBlock *InsertAtEnd) {
2901 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2905 /// Provide fast operand accessors.
2906 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2908 // Accessor Methods for IndirectBrInst instruction.
2909 Value *getAddress() { return getOperand(0); }
2910 const Value *getAddress() const { return getOperand(0); }
2911 void setAddress(Value *V) { setOperand(0, V); }
2914 /// getNumDestinations - return the number of possible destinations in this
2915 /// indirectbr instruction.
2916 unsigned getNumDestinations() const { return getNumOperands()-1; }
2918 /// getDestination - Return the specified destination.
2919 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2920 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2922 /// addDestination - Add a destination.
2924 void addDestination(BasicBlock *Dest);
2926 /// removeDestination - This method removes the specified successor from the
2927 /// indirectbr instruction.
2928 void removeDestination(unsigned i);
2930 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2931 BasicBlock *getSuccessor(unsigned i) const {
2932 return cast<BasicBlock>(getOperand(i+1));
2934 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2935 setOperand(i+1, (Value*)NewSucc);
2938 // Methods for support type inquiry through isa, cast, and dyn_cast:
2939 static inline bool classof(const IndirectBrInst *) { return true; }
2940 static inline bool classof(const Instruction *I) {
2941 return I->getOpcode() == Instruction::IndirectBr;
2943 static inline bool classof(const Value *V) {
2944 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2947 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2948 virtual unsigned getNumSuccessorsV() const;
2949 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2953 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2956 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2959 //===----------------------------------------------------------------------===//
2961 //===----------------------------------------------------------------------===//
2963 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2964 /// calling convention of the call.
2966 class InvokeInst : public TerminatorInst {
2967 AttrListPtr AttributeList;
2968 InvokeInst(const InvokeInst &BI);
2969 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2970 ArrayRef<Value *> Args, const Twine &NameStr);
2972 /// Construct an InvokeInst given a range of arguments.
2974 /// @brief Construct an InvokeInst from a range of arguments
2975 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2976 ArrayRef<Value *> Args, unsigned Values,
2977 const Twine &NameStr, Instruction *InsertBefore);
2979 /// Construct an InvokeInst given a range of arguments.
2981 /// @brief Construct an InvokeInst from a range of arguments
2982 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2983 ArrayRef<Value *> Args, unsigned Values,
2984 const Twine &NameStr, BasicBlock *InsertAtEnd);
2986 virtual InvokeInst *clone_impl() const;
2988 static InvokeInst *Create(Value *Func,
2989 BasicBlock *IfNormal, BasicBlock *IfException,
2990 ArrayRef<Value *> Args, const Twine &NameStr = "",
2991 Instruction *InsertBefore = 0) {
2992 unsigned Values = unsigned(Args.size()) + 3;
2993 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2994 Values, NameStr, InsertBefore);
2996 static InvokeInst *Create(Value *Func,
2997 BasicBlock *IfNormal, BasicBlock *IfException,
2998 ArrayRef<Value *> Args, const Twine &NameStr,
2999 BasicBlock *InsertAtEnd) {
3000 unsigned Values = unsigned(Args.size()) + 3;
3001 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3002 Values, NameStr, InsertAtEnd);
3005 /// Provide fast operand accessors
3006 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3008 /// getNumArgOperands - Return the number of invoke arguments.
3010 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3012 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3014 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3015 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3017 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3019 CallingConv::ID getCallingConv() const {
3020 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3022 void setCallingConv(CallingConv::ID CC) {
3023 setInstructionSubclassData(static_cast<unsigned>(CC));
3026 /// getAttributes - Return the parameter attributes for this invoke.
3028 const AttrListPtr &getAttributes() const { return AttributeList; }
3030 /// setAttributes - Set the parameter attributes for this invoke.
3032 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3034 /// addAttribute - adds the attribute to the list of attributes.
3035 void addAttribute(unsigned i, Attributes attr);
3037 /// removeAttribute - removes the attribute from the list of attributes.
3038 void removeAttribute(unsigned i, Attributes attr);
3040 /// \brief Return true if this call has the given attribute.
3041 bool hasFnAttr(Attributes N) const {
3042 return paramHasAttr(~0, N);
3045 /// @brief Determine whether the call or the callee has the given attributes.
3046 bool paramHasSExtAttr(unsigned i) const;
3047 bool paramHasZExtAttr(unsigned i) const;
3048 bool paramHasInRegAttr(unsigned i) const;
3049 bool paramHasStructRetAttr(unsigned i) const;
3050 bool paramHasNestAttr(unsigned i) const;
3051 bool paramHasByValAttr(unsigned i) const;
3053 /// @brief Determine whether the call or the callee has the given attribute.
3054 bool paramHasAttr(unsigned i, Attributes attr) const;
3056 /// @brief Extract the alignment for a call or parameter (0=unknown).
3057 unsigned getParamAlignment(unsigned i) const {
3058 return AttributeList.getParamAlignment(i);
3061 /// @brief Return true if the call should not be inlined.
3062 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3063 void setIsNoInline(bool Value = true) {
3064 if (Value) addAttribute(~0, Attribute::NoInline);
3065 else removeAttribute(~0, Attribute::NoInline);
3068 /// @brief Determine if the call does not access memory.
3069 bool doesNotAccessMemory() const {
3070 return hasFnAttr(Attribute::ReadNone);
3072 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
3073 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
3074 else removeAttribute(~0, Attribute::ReadNone);
3077 /// @brief Determine if the call does not access or only reads memory.
3078 bool onlyReadsMemory() const {
3079 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3081 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
3082 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
3083 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
3086 /// @brief Determine if the call cannot return.
3087 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3088 void setDoesNotReturn(bool DoesNotReturn = true) {
3089 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
3090 else removeAttribute(~0, Attribute::NoReturn);
3093 /// @brief Determine if the call cannot unwind.
3094 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3095 void setDoesNotThrow(bool DoesNotThrow = true) {
3096 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
3097 else removeAttribute(~0, Attribute::NoUnwind);
3100 /// @brief Determine if the call returns a structure through first
3101 /// pointer argument.
3102 bool hasStructRetAttr() const {
3103 // Be friendly and also check the callee.
3104 return paramHasAttr(1, Attribute::StructRet);
3107 /// @brief Determine if any call argument is an aggregate passed by value.
3108 bool hasByValArgument() const {
3109 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3112 /// getCalledFunction - Return the function called, or null if this is an
3113 /// indirect function invocation.
3115 Function *getCalledFunction() const {
3116 return dyn_cast<Function>(Op<-3>());
3119 /// getCalledValue - Get a pointer to the function that is invoked by this
3121 const Value *getCalledValue() const { return Op<-3>(); }
3122 Value *getCalledValue() { return Op<-3>(); }
3124 /// setCalledFunction - Set the function called.
3125 void setCalledFunction(Value* Fn) {
3129 // get*Dest - Return the destination basic blocks...
3130 BasicBlock *getNormalDest() const {
3131 return cast<BasicBlock>(Op<-2>());
3133 BasicBlock *getUnwindDest() const {
3134 return cast<BasicBlock>(Op<-1>());
3136 void setNormalDest(BasicBlock *B) {
3137 Op<-2>() = reinterpret_cast<Value*>(B);
3139 void setUnwindDest(BasicBlock *B) {
3140 Op<-1>() = reinterpret_cast<Value*>(B);
3143 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3144 /// block (the unwind destination).
3145 LandingPadInst *getLandingPadInst() const;
3147 BasicBlock *getSuccessor(unsigned i) const {
3148 assert(i < 2 && "Successor # out of range for invoke!");
3149 return i == 0 ? getNormalDest() : getUnwindDest();
3152 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3153 assert(idx < 2 && "Successor # out of range for invoke!");
3154 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3157 unsigned getNumSuccessors() const { return 2; }
3159 // Methods for support type inquiry through isa, cast, and dyn_cast:
3160 static inline bool classof(const InvokeInst *) { return true; }
3161 static inline bool classof(const Instruction *I) {
3162 return (I->getOpcode() == Instruction::Invoke);
3164 static inline bool classof(const Value *V) {
3165 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3169 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3170 virtual unsigned getNumSuccessorsV() const;
3171 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3173 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3174 // method so that subclasses cannot accidentally use it.
3175 void setInstructionSubclassData(unsigned short D) {
3176 Instruction::setInstructionSubclassData(D);
3181 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3184 InvokeInst::InvokeInst(Value *Func,
3185 BasicBlock *IfNormal, BasicBlock *IfException,
3186 ArrayRef<Value *> Args, unsigned Values,
3187 const Twine &NameStr, Instruction *InsertBefore)
3188 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3189 ->getElementType())->getReturnType(),
3190 Instruction::Invoke,
3191 OperandTraits<InvokeInst>::op_end(this) - Values,
3192 Values, InsertBefore) {
3193 init(Func, IfNormal, IfException, Args, NameStr);
3195 InvokeInst::InvokeInst(Value *Func,
3196 BasicBlock *IfNormal, BasicBlock *IfException,
3197 ArrayRef<Value *> Args, unsigned Values,
3198 const Twine &NameStr, BasicBlock *InsertAtEnd)
3199 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3200 ->getElementType())->getReturnType(),
3201 Instruction::Invoke,
3202 OperandTraits<InvokeInst>::op_end(this) - Values,
3203 Values, InsertAtEnd) {
3204 init(Func, IfNormal, IfException, Args, NameStr);
3207 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3209 //===----------------------------------------------------------------------===//
3211 //===----------------------------------------------------------------------===//
3213 //===---------------------------------------------------------------------------
3214 /// ResumeInst - Resume the propagation of an exception.
3216 class ResumeInst : public TerminatorInst {
3217 ResumeInst(const ResumeInst &RI);
3219 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3220 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3222 virtual ResumeInst *clone_impl() const;
3224 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3225 return new(1) ResumeInst(Exn, InsertBefore);
3227 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3228 return new(1) ResumeInst(Exn, InsertAtEnd);
3231 /// Provide fast operand accessors
3232 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3234 /// Convenience accessor.
3235 Value *getValue() const { return Op<0>(); }
3237 unsigned getNumSuccessors() const { return 0; }
3239 // Methods for support type inquiry through isa, cast, and dyn_cast:
3240 static inline bool classof(const ResumeInst *) { return true; }
3241 static inline bool classof(const Instruction *I) {
3242 return I->getOpcode() == Instruction::Resume;
3244 static inline bool classof(const Value *V) {
3245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3248 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3249 virtual unsigned getNumSuccessorsV() const;
3250 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3254 struct OperandTraits<ResumeInst> :
3255 public FixedNumOperandTraits<ResumeInst, 1> {
3258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3260 //===----------------------------------------------------------------------===//
3261 // UnreachableInst Class
3262 //===----------------------------------------------------------------------===//
3264 //===---------------------------------------------------------------------------
3265 /// UnreachableInst - This function has undefined behavior. In particular, the
3266 /// presence of this instruction indicates some higher level knowledge that the
3267 /// end of the block cannot be reached.
3269 class UnreachableInst : public TerminatorInst {
3270 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3272 virtual UnreachableInst *clone_impl() const;
3275 // allocate space for exactly zero operands
3276 void *operator new(size_t s) {
3277 return User::operator new(s, 0);
3279 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3280 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3282 unsigned getNumSuccessors() const { return 0; }
3284 // Methods for support type inquiry through isa, cast, and dyn_cast:
3285 static inline bool classof(const UnreachableInst *) { return true; }
3286 static inline bool classof(const Instruction *I) {
3287 return I->getOpcode() == Instruction::Unreachable;
3289 static inline bool classof(const Value *V) {
3290 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3293 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3294 virtual unsigned getNumSuccessorsV() const;
3295 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3298 //===----------------------------------------------------------------------===//
3300 //===----------------------------------------------------------------------===//
3302 /// @brief This class represents a truncation of integer types.
3303 class TruncInst : public CastInst {
3305 /// @brief Clone an identical TruncInst
3306 virtual TruncInst *clone_impl() const;
3309 /// @brief Constructor with insert-before-instruction semantics
3311 Value *S, ///< The value to be truncated
3312 Type *Ty, ///< The (smaller) type to truncate to
3313 const Twine &NameStr = "", ///< A name for the new instruction
3314 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3317 /// @brief Constructor with insert-at-end-of-block semantics
3319 Value *S, ///< The value to be truncated
3320 Type *Ty, ///< The (smaller) type to truncate to
3321 const Twine &NameStr, ///< A name for the new instruction
3322 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3325 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3326 static inline bool classof(const TruncInst *) { return true; }
3327 static inline bool classof(const Instruction *I) {
3328 return I->getOpcode() == Trunc;
3330 static inline bool classof(const Value *V) {
3331 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3335 //===----------------------------------------------------------------------===//
3337 //===----------------------------------------------------------------------===//
3339 /// @brief This class represents zero extension of integer types.
3340 class ZExtInst : public CastInst {
3342 /// @brief Clone an identical ZExtInst
3343 virtual ZExtInst *clone_impl() const;
3346 /// @brief Constructor with insert-before-instruction semantics
3348 Value *S, ///< The value to be zero extended
3349 Type *Ty, ///< The type to zero extend to
3350 const Twine &NameStr = "", ///< A name for the new instruction
3351 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3354 /// @brief Constructor with insert-at-end semantics.
3356 Value *S, ///< The value to be zero extended
3357 Type *Ty, ///< The type to zero extend to
3358 const Twine &NameStr, ///< A name for the new instruction
3359 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3362 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3363 static inline bool classof(const ZExtInst *) { return true; }
3364 static inline bool classof(const Instruction *I) {
3365 return I->getOpcode() == ZExt;
3367 static inline bool classof(const Value *V) {
3368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3372 //===----------------------------------------------------------------------===//
3374 //===----------------------------------------------------------------------===//
3376 /// @brief This class represents a sign extension of integer types.
3377 class SExtInst : public CastInst {
3379 /// @brief Clone an identical SExtInst
3380 virtual SExtInst *clone_impl() const;
3383 /// @brief Constructor with insert-before-instruction semantics
3385 Value *S, ///< The value to be sign extended
3386 Type *Ty, ///< The type to sign extend to
3387 const Twine &NameStr = "", ///< A name for the new instruction
3388 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3391 /// @brief Constructor with insert-at-end-of-block semantics
3393 Value *S, ///< The value to be sign extended
3394 Type *Ty, ///< The type to sign extend to
3395 const Twine &NameStr, ///< A name for the new instruction
3396 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3399 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3400 static inline bool classof(const SExtInst *) { return true; }
3401 static inline bool classof(const Instruction *I) {
3402 return I->getOpcode() == SExt;
3404 static inline bool classof(const Value *V) {
3405 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3409 //===----------------------------------------------------------------------===//
3410 // FPTruncInst Class
3411 //===----------------------------------------------------------------------===//
3413 /// @brief This class represents a truncation of floating point types.
3414 class FPTruncInst : public CastInst {
3416 /// @brief Clone an identical FPTruncInst
3417 virtual FPTruncInst *clone_impl() const;
3420 /// @brief Constructor with insert-before-instruction semantics
3422 Value *S, ///< The value to be truncated
3423 Type *Ty, ///< The type to truncate to
3424 const Twine &NameStr = "", ///< A name for the new instruction
3425 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3428 /// @brief Constructor with insert-before-instruction semantics
3430 Value *S, ///< The value to be truncated
3431 Type *Ty, ///< The type to truncate to
3432 const Twine &NameStr, ///< A name for the new instruction
3433 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3436 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3437 static inline bool classof(const FPTruncInst *) { return true; }
3438 static inline bool classof(const Instruction *I) {
3439 return I->getOpcode() == FPTrunc;
3441 static inline bool classof(const Value *V) {
3442 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3446 //===----------------------------------------------------------------------===//
3448 //===----------------------------------------------------------------------===//
3450 /// @brief This class represents an extension of floating point types.
3451 class FPExtInst : public CastInst {
3453 /// @brief Clone an identical FPExtInst
3454 virtual FPExtInst *clone_impl() const;
3457 /// @brief Constructor with insert-before-instruction semantics
3459 Value *S, ///< The value to be extended
3460 Type *Ty, ///< The type to extend to
3461 const Twine &NameStr = "", ///< A name for the new instruction
3462 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3465 /// @brief Constructor with insert-at-end-of-block semantics
3467 Value *S, ///< The value to be extended
3468 Type *Ty, ///< The type to extend to
3469 const Twine &NameStr, ///< A name for the new instruction
3470 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3473 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3474 static inline bool classof(const FPExtInst *) { return true; }
3475 static inline bool classof(const Instruction *I) {
3476 return I->getOpcode() == FPExt;
3478 static inline bool classof(const Value *V) {
3479 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3483 //===----------------------------------------------------------------------===//
3485 //===----------------------------------------------------------------------===//
3487 /// @brief This class represents a cast unsigned integer to floating point.
3488 class UIToFPInst : public CastInst {
3490 /// @brief Clone an identical UIToFPInst
3491 virtual UIToFPInst *clone_impl() const;
3494 /// @brief Constructor with insert-before-instruction semantics
3496 Value *S, ///< The value to be converted
3497 Type *Ty, ///< The type to convert to
3498 const Twine &NameStr = "", ///< A name for the new instruction
3499 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3502 /// @brief Constructor with insert-at-end-of-block semantics
3504 Value *S, ///< The value to be converted
3505 Type *Ty, ///< The type to convert to
3506 const Twine &NameStr, ///< A name for the new instruction
3507 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3510 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3511 static inline bool classof(const UIToFPInst *) { return true; }
3512 static inline bool classof(const Instruction *I) {
3513 return I->getOpcode() == UIToFP;
3515 static inline bool classof(const Value *V) {
3516 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3520 //===----------------------------------------------------------------------===//
3522 //===----------------------------------------------------------------------===//
3524 /// @brief This class represents a cast from signed integer to floating point.
3525 class SIToFPInst : public CastInst {
3527 /// @brief Clone an identical SIToFPInst
3528 virtual SIToFPInst *clone_impl() const;
3531 /// @brief Constructor with insert-before-instruction semantics
3533 Value *S, ///< The value to be converted
3534 Type *Ty, ///< The type to convert to
3535 const Twine &NameStr = "", ///< A name for the new instruction
3536 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3539 /// @brief Constructor with insert-at-end-of-block semantics
3541 Value *S, ///< The value to be converted
3542 Type *Ty, ///< The type to convert to
3543 const Twine &NameStr, ///< A name for the new instruction
3544 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3547 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3548 static inline bool classof(const SIToFPInst *) { return true; }
3549 static inline bool classof(const Instruction *I) {
3550 return I->getOpcode() == SIToFP;
3552 static inline bool classof(const Value *V) {
3553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3557 //===----------------------------------------------------------------------===//
3559 //===----------------------------------------------------------------------===//
3561 /// @brief This class represents a cast from floating point to unsigned integer
3562 class FPToUIInst : public CastInst {
3564 /// @brief Clone an identical FPToUIInst
3565 virtual FPToUIInst *clone_impl() const;
3568 /// @brief Constructor with insert-before-instruction semantics
3570 Value *S, ///< The value to be converted
3571 Type *Ty, ///< The type to convert to
3572 const Twine &NameStr = "", ///< A name for the new instruction
3573 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3576 /// @brief Constructor with insert-at-end-of-block semantics
3578 Value *S, ///< The value to be converted
3579 Type *Ty, ///< The type to convert to
3580 const Twine &NameStr, ///< A name for the new instruction
3581 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3584 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3585 static inline bool classof(const FPToUIInst *) { return true; }
3586 static inline bool classof(const Instruction *I) {
3587 return I->getOpcode() == FPToUI;
3589 static inline bool classof(const Value *V) {
3590 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3594 //===----------------------------------------------------------------------===//
3596 //===----------------------------------------------------------------------===//
3598 /// @brief This class represents a cast from floating point to signed integer.
3599 class FPToSIInst : public CastInst {
3601 /// @brief Clone an identical FPToSIInst
3602 virtual FPToSIInst *clone_impl() const;
3605 /// @brief Constructor with insert-before-instruction semantics
3607 Value *S, ///< The value to be converted
3608 Type *Ty, ///< The type to convert to
3609 const Twine &NameStr = "", ///< A name for the new instruction
3610 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3613 /// @brief Constructor with insert-at-end-of-block semantics
3615 Value *S, ///< The value to be converted
3616 Type *Ty, ///< The type to convert to
3617 const Twine &NameStr, ///< A name for the new instruction
3618 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3621 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3622 static inline bool classof(const FPToSIInst *) { return true; }
3623 static inline bool classof(const Instruction *I) {
3624 return I->getOpcode() == FPToSI;
3626 static inline bool classof(const Value *V) {
3627 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3631 //===----------------------------------------------------------------------===//
3632 // IntToPtrInst Class
3633 //===----------------------------------------------------------------------===//
3635 /// @brief This class represents a cast from an integer to a pointer.
3636 class IntToPtrInst : public CastInst {
3638 /// @brief Constructor with insert-before-instruction semantics
3640 Value *S, ///< The value to be converted
3641 Type *Ty, ///< The type to convert to
3642 const Twine &NameStr = "", ///< A name for the new instruction
3643 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3646 /// @brief Constructor with insert-at-end-of-block semantics
3648 Value *S, ///< The value to be converted
3649 Type *Ty, ///< The type to convert to
3650 const Twine &NameStr, ///< A name for the new instruction
3651 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3654 /// @brief Clone an identical IntToPtrInst
3655 virtual IntToPtrInst *clone_impl() const;
3657 // Methods for support type inquiry through isa, cast, and dyn_cast:
3658 static inline bool classof(const IntToPtrInst *) { return true; }
3659 static inline bool classof(const Instruction *I) {
3660 return I->getOpcode() == IntToPtr;
3662 static inline bool classof(const Value *V) {
3663 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3667 //===----------------------------------------------------------------------===//
3668 // PtrToIntInst Class
3669 //===----------------------------------------------------------------------===//
3671 /// @brief This class represents a cast from a pointer to an integer
3672 class PtrToIntInst : public CastInst {
3674 /// @brief Clone an identical PtrToIntInst
3675 virtual PtrToIntInst *clone_impl() const;
3678 /// @brief Constructor with insert-before-instruction semantics
3680 Value *S, ///< The value to be converted
3681 Type *Ty, ///< The type to convert to
3682 const Twine &NameStr = "", ///< A name for the new instruction
3683 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3686 /// @brief Constructor with insert-at-end-of-block semantics
3688 Value *S, ///< The value to be converted
3689 Type *Ty, ///< The type to convert to
3690 const Twine &NameStr, ///< A name for the new instruction
3691 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3694 // Methods for support type inquiry through isa, cast, and dyn_cast:
3695 static inline bool classof(const PtrToIntInst *) { return true; }
3696 static inline bool classof(const Instruction *I) {
3697 return I->getOpcode() == PtrToInt;
3699 static inline bool classof(const Value *V) {
3700 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3704 //===----------------------------------------------------------------------===//
3705 // BitCastInst Class
3706 //===----------------------------------------------------------------------===//
3708 /// @brief This class represents a no-op cast from one type to another.
3709 class BitCastInst : public CastInst {
3711 /// @brief Clone an identical BitCastInst
3712 virtual BitCastInst *clone_impl() const;
3715 /// @brief Constructor with insert-before-instruction semantics
3717 Value *S, ///< The value to be casted
3718 Type *Ty, ///< The type to casted to
3719 const Twine &NameStr = "", ///< A name for the new instruction
3720 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3723 /// @brief Constructor with insert-at-end-of-block semantics
3725 Value *S, ///< The value to be casted
3726 Type *Ty, ///< The type to casted to
3727 const Twine &NameStr, ///< A name for the new instruction
3728 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3731 // Methods for support type inquiry through isa, cast, and dyn_cast:
3732 static inline bool classof(const BitCastInst *) { return true; }
3733 static inline bool classof(const Instruction *I) {
3734 return I->getOpcode() == BitCast;
3736 static inline bool classof(const Value *V) {
3737 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3741 } // End llvm namespace