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_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
40 // Consume = 3, // Not specified yet.
44 SequentiallyConsistent = 7
47 enum SynchronizationScope {
52 //===----------------------------------------------------------------------===//
54 //===----------------------------------------------------------------------===//
56 /// AllocaInst - an instruction to allocate memory on the stack
58 class AllocaInst : public UnaryInstruction {
60 virtual AllocaInst *clone_impl() const;
62 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
63 const Twine &Name = "", Instruction *InsertBefore = 0);
64 AllocaInst(Type *Ty, Value *ArraySize,
65 const Twine &Name, BasicBlock *InsertAtEnd);
67 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
68 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
70 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
71 const Twine &Name = "", Instruction *InsertBefore = 0);
72 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
73 const Twine &Name, BasicBlock *InsertAtEnd);
75 // Out of line virtual method, so the vtable, etc. has a home.
76 virtual ~AllocaInst();
78 /// isArrayAllocation - Return true if there is an allocation size parameter
79 /// to the allocation instruction that is not 1.
81 bool isArrayAllocation() const;
83 /// getArraySize - Get the number of elements allocated. For a simple
84 /// allocation of a single element, this will return a constant 1 value.
86 const Value *getArraySize() const { return getOperand(0); }
87 Value *getArraySize() { return getOperand(0); }
89 /// getType - Overload to return most specific pointer type
91 PointerType *getType() const {
92 return cast<PointerType>(Instruction::getType());
95 /// getAllocatedType - Return the type that is being allocated by the
98 Type *getAllocatedType() const;
100 /// getAlignment - Return the alignment of the memory that is being allocated
101 /// by the instruction.
103 unsigned getAlignment() const {
104 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
106 void setAlignment(unsigned Align);
108 /// isStaticAlloca - Return true if this alloca is in the entry block of the
109 /// function and is a constant size. If so, the code generator will fold it
110 /// into the prolog/epilog code, so it is basically free.
111 bool isStaticAlloca() const;
113 /// \brief Return true if this alloca is used as an inalloca argument to a
114 /// call. Such allocas are never considered static even if they are in the
116 bool isUsedWithInAlloca() const {
117 return getSubclassDataFromInstruction() & 32;
120 /// \brief Specify whether this alloca is used to represent a the arguments to
122 void setUsedWithInAlloca(bool V) {
123 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
127 // Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Alloca);
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 // Shadow Instruction::setInstructionSubclassData with a private forwarding
136 // method so that subclasses cannot accidentally use it.
137 void setInstructionSubclassData(unsigned short D) {
138 Instruction::setInstructionSubclassData(D);
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 /// LoadInst - an instruction for reading from memory. This uses the
148 /// SubclassData field in Value to store whether or not the load is volatile.
150 class LoadInst : public UnaryInstruction {
153 virtual LoadInst *clone_impl() const;
155 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
156 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
157 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
158 Instruction *InsertBefore = 0);
159 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
160 BasicBlock *InsertAtEnd);
161 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
162 unsigned Align, Instruction *InsertBefore = 0);
163 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
164 unsigned Align, BasicBlock *InsertAtEnd);
165 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
166 unsigned Align, AtomicOrdering Order,
167 SynchronizationScope SynchScope = CrossThread,
168 Instruction *InsertBefore = 0);
169 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
170 unsigned Align, AtomicOrdering Order,
171 SynchronizationScope SynchScope,
172 BasicBlock *InsertAtEnd);
174 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
175 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
176 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
177 bool isVolatile = false, Instruction *InsertBefore = 0);
178 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
179 BasicBlock *InsertAtEnd);
181 /// isVolatile - Return true if this is a load from a volatile memory
184 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
186 /// setVolatile - Specify whether this is a volatile load or not.
188 void setVolatile(bool V) {
189 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
193 /// getAlignment - Return the alignment of the access that is being performed
195 unsigned getAlignment() const {
196 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
199 void setAlignment(unsigned Align);
201 /// Returns the ordering effect of this fence.
202 AtomicOrdering getOrdering() const {
203 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
206 /// Set the ordering constraint on this load. May not be Release or
208 void setOrdering(AtomicOrdering Ordering) {
209 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
213 SynchronizationScope getSynchScope() const {
214 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
217 /// Specify whether this load is ordered with respect to all
218 /// concurrently executing threads, or only with respect to signal handlers
219 /// executing in the same thread.
220 void setSynchScope(SynchronizationScope xthread) {
221 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
225 bool isAtomic() const { return getOrdering() != NotAtomic; }
226 void setAtomic(AtomicOrdering Ordering,
227 SynchronizationScope SynchScope = CrossThread) {
228 setOrdering(Ordering);
229 setSynchScope(SynchScope);
232 bool isSimple() const { return !isAtomic() && !isVolatile(); }
233 bool isUnordered() const {
234 return getOrdering() <= Unordered && !isVolatile();
237 Value *getPointerOperand() { return getOperand(0); }
238 const Value *getPointerOperand() const { return getOperand(0); }
239 static unsigned getPointerOperandIndex() { return 0U; }
241 /// \brief Returns the address space of the pointer operand.
242 unsigned getPointerAddressSpace() const {
243 return getPointerOperand()->getType()->getPointerAddressSpace();
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() == Instruction::Load;
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 // Shadow Instruction::setInstructionSubclassData with a private forwarding
256 // method so that subclasses cannot accidentally use it.
257 void setInstructionSubclassData(unsigned short D) {
258 Instruction::setInstructionSubclassData(D);
263 //===----------------------------------------------------------------------===//
265 //===----------------------------------------------------------------------===//
267 /// StoreInst - an instruction for storing to memory
269 class StoreInst : public Instruction {
270 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
273 virtual StoreInst *clone_impl() const;
275 // allocate space for exactly two operands
276 void *operator new(size_t s) {
277 return User::operator new(s, 2);
279 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
280 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
281 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
282 Instruction *InsertBefore = 0);
283 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
284 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
285 unsigned Align, Instruction *InsertBefore = 0);
286 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
287 unsigned Align, BasicBlock *InsertAtEnd);
288 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
289 unsigned Align, AtomicOrdering Order,
290 SynchronizationScope SynchScope = CrossThread,
291 Instruction *InsertBefore = 0);
292 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
293 unsigned Align, AtomicOrdering Order,
294 SynchronizationScope SynchScope,
295 BasicBlock *InsertAtEnd);
298 /// isVolatile - Return true if this is a store to a volatile memory
301 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
303 /// setVolatile - Specify whether this is a volatile store or not.
305 void setVolatile(bool V) {
306 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
310 /// Transparently provide more efficient getOperand methods.
311 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
313 /// getAlignment - Return the alignment of the access that is being performed
315 unsigned getAlignment() const {
316 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
319 void setAlignment(unsigned Align);
321 /// Returns the ordering effect of this store.
322 AtomicOrdering getOrdering() const {
323 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
326 /// Set the ordering constraint on this store. May not be Acquire or
328 void setOrdering(AtomicOrdering Ordering) {
329 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
333 SynchronizationScope getSynchScope() const {
334 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
337 /// Specify whether this store instruction is ordered with respect to all
338 /// concurrently executing threads, or only with respect to signal handlers
339 /// executing in the same thread.
340 void setSynchScope(SynchronizationScope xthread) {
341 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
345 bool isAtomic() const { return getOrdering() != NotAtomic; }
346 void setAtomic(AtomicOrdering Ordering,
347 SynchronizationScope SynchScope = CrossThread) {
348 setOrdering(Ordering);
349 setSynchScope(SynchScope);
352 bool isSimple() const { return !isAtomic() && !isVolatile(); }
353 bool isUnordered() const {
354 return getOrdering() <= Unordered && !isVolatile();
357 Value *getValueOperand() { return getOperand(0); }
358 const Value *getValueOperand() const { return getOperand(0); }
360 Value *getPointerOperand() { return getOperand(1); }
361 const Value *getPointerOperand() const { return getOperand(1); }
362 static unsigned getPointerOperandIndex() { return 1U; }
364 /// \brief Returns the address space of the pointer operand.
365 unsigned getPointerAddressSpace() const {
366 return getPointerOperand()->getType()->getPointerAddressSpace();
369 // Methods for support type inquiry through isa, cast, and dyn_cast:
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
377 // Shadow Instruction::setInstructionSubclassData with a private forwarding
378 // method so that subclasses cannot accidentally use it.
379 void setInstructionSubclassData(unsigned short D) {
380 Instruction::setInstructionSubclassData(D);
385 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
388 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
390 //===----------------------------------------------------------------------===//
392 //===----------------------------------------------------------------------===//
394 /// FenceInst - an instruction for ordering other memory operations
396 class FenceInst : public Instruction {
397 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
398 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
400 virtual FenceInst *clone_impl() const;
402 // allocate space for exactly zero operands
403 void *operator new(size_t s) {
404 return User::operator new(s, 0);
407 // Ordering may only be Acquire, Release, AcquireRelease, or
408 // SequentiallyConsistent.
409 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
410 SynchronizationScope SynchScope = CrossThread,
411 Instruction *InsertBefore = 0);
412 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
413 SynchronizationScope SynchScope,
414 BasicBlock *InsertAtEnd);
416 /// Returns the ordering effect of this fence.
417 AtomicOrdering getOrdering() const {
418 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
421 /// Set the ordering constraint on this fence. May only be Acquire, Release,
422 /// AcquireRelease, or SequentiallyConsistent.
423 void setOrdering(AtomicOrdering Ordering) {
424 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
428 SynchronizationScope getSynchScope() const {
429 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
432 /// Specify whether this fence orders other operations with respect to all
433 /// concurrently executing threads, or only with respect to signal handlers
434 /// executing in the same thread.
435 void setSynchScope(SynchronizationScope xthread) {
436 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
440 // Methods for support type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const Instruction *I) {
442 return I->getOpcode() == Instruction::Fence;
444 static inline bool classof(const Value *V) {
445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
448 // Shadow Instruction::setInstructionSubclassData with a private forwarding
449 // method so that subclasses cannot accidentally use it.
450 void setInstructionSubclassData(unsigned short D) {
451 Instruction::setInstructionSubclassData(D);
455 //===----------------------------------------------------------------------===//
456 // AtomicCmpXchgInst Class
457 //===----------------------------------------------------------------------===//
459 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
460 /// specified value is in a memory location, and, if it is, stores a new value
461 /// there. Returns the value that was loaded.
463 class AtomicCmpXchgInst : public Instruction {
464 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
465 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
466 AtomicOrdering Ordering, SynchronizationScope SynchScope);
468 virtual AtomicCmpXchgInst *clone_impl() const;
470 // allocate space for exactly three operands
471 void *operator new(size_t s) {
472 return User::operator new(s, 3);
474 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
475 AtomicOrdering Ordering, SynchronizationScope SynchScope,
476 Instruction *InsertBefore = 0);
477 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
478 AtomicOrdering Ordering, SynchronizationScope SynchScope,
479 BasicBlock *InsertAtEnd);
481 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
484 bool isVolatile() const {
485 return getSubclassDataFromInstruction() & 1;
488 /// setVolatile - Specify whether this is a volatile cmpxchg.
490 void setVolatile(bool V) {
491 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
495 /// Transparently provide more efficient getOperand methods.
496 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
498 /// Set the ordering constraint on this cmpxchg.
499 void setOrdering(AtomicOrdering Ordering) {
500 assert(Ordering != NotAtomic &&
501 "CmpXchg instructions can only be atomic.");
502 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
506 /// Specify whether this cmpxchg is atomic and orders other operations with
507 /// respect to all concurrently executing threads, or only with respect to
508 /// signal handlers executing in the same thread.
509 void setSynchScope(SynchronizationScope SynchScope) {
510 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
514 /// Returns the ordering constraint on this cmpxchg.
515 AtomicOrdering getOrdering() const {
516 return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
519 /// Returns whether this cmpxchg is atomic between threads or only within a
521 SynchronizationScope getSynchScope() const {
522 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
525 Value *getPointerOperand() { return getOperand(0); }
526 const Value *getPointerOperand() const { return getOperand(0); }
527 static unsigned getPointerOperandIndex() { return 0U; }
529 Value *getCompareOperand() { return getOperand(1); }
530 const Value *getCompareOperand() const { return getOperand(1); }
532 Value *getNewValOperand() { return getOperand(2); }
533 const Value *getNewValOperand() const { return getOperand(2); }
535 /// \brief Returns the address space of the pointer operand.
536 unsigned getPointerAddressSpace() const {
537 return getPointerOperand()->getType()->getPointerAddressSpace();
540 // Methods for support type inquiry through isa, cast, and dyn_cast:
541 static inline bool classof(const Instruction *I) {
542 return I->getOpcode() == Instruction::AtomicCmpXchg;
544 static inline bool classof(const Value *V) {
545 return isa<Instruction>(V) && classof(cast<Instruction>(V));
548 // Shadow Instruction::setInstructionSubclassData with a private forwarding
549 // method so that subclasses cannot accidentally use it.
550 void setInstructionSubclassData(unsigned short D) {
551 Instruction::setInstructionSubclassData(D);
556 struct OperandTraits<AtomicCmpXchgInst> :
557 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
560 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
562 //===----------------------------------------------------------------------===//
563 // AtomicRMWInst Class
564 //===----------------------------------------------------------------------===//
566 /// AtomicRMWInst - an instruction that atomically reads a memory location,
567 /// combines it with another value, and then stores the result back. Returns
570 class AtomicRMWInst : public Instruction {
571 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
573 virtual AtomicRMWInst *clone_impl() const;
575 /// This enumeration lists the possible modifications atomicrmw can make. In
576 /// the descriptions, 'p' is the pointer to the instruction's memory location,
577 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
578 /// instruction. These instructions always return 'old'.
594 /// *p = old >signed v ? old : v
596 /// *p = old <signed v ? old : v
598 /// *p = old >unsigned v ? old : v
600 /// *p = old <unsigned v ? old : v
608 // allocate space for exactly two operands
609 void *operator new(size_t s) {
610 return User::operator new(s, 2);
612 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
613 AtomicOrdering Ordering, SynchronizationScope SynchScope,
614 Instruction *InsertBefore = 0);
615 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
616 AtomicOrdering Ordering, SynchronizationScope SynchScope,
617 BasicBlock *InsertAtEnd);
619 BinOp getOperation() const {
620 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
623 void setOperation(BinOp Operation) {
624 unsigned short SubclassData = getSubclassDataFromInstruction();
625 setInstructionSubclassData((SubclassData & 31) |
629 /// isVolatile - Return true if this is a RMW on a volatile memory location.
631 bool isVolatile() const {
632 return getSubclassDataFromInstruction() & 1;
635 /// setVolatile - Specify whether this is a volatile RMW or not.
637 void setVolatile(bool V) {
638 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
642 /// Transparently provide more efficient getOperand methods.
643 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
645 /// Set the ordering constraint on this RMW.
646 void setOrdering(AtomicOrdering Ordering) {
647 assert(Ordering != NotAtomic &&
648 "atomicrmw instructions can only be atomic.");
649 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
653 /// Specify whether this RMW orders other operations with respect to all
654 /// concurrently executing threads, or only with respect to signal handlers
655 /// executing in the same thread.
656 void setSynchScope(SynchronizationScope SynchScope) {
657 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
661 /// Returns the ordering constraint on this RMW.
662 AtomicOrdering getOrdering() const {
663 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
666 /// Returns whether this RMW is atomic between threads or only within a
668 SynchronizationScope getSynchScope() const {
669 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
672 Value *getPointerOperand() { return getOperand(0); }
673 const Value *getPointerOperand() const { return getOperand(0); }
674 static unsigned getPointerOperandIndex() { return 0U; }
676 Value *getValOperand() { return getOperand(1); }
677 const Value *getValOperand() const { return getOperand(1); }
679 /// \brief Returns the address space of the pointer operand.
680 unsigned getPointerAddressSpace() const {
681 return getPointerOperand()->getType()->getPointerAddressSpace();
684 // Methods for support type inquiry through isa, cast, and dyn_cast:
685 static inline bool classof(const Instruction *I) {
686 return I->getOpcode() == Instruction::AtomicRMW;
688 static inline bool classof(const Value *V) {
689 return isa<Instruction>(V) && classof(cast<Instruction>(V));
692 void Init(BinOp Operation, Value *Ptr, Value *Val,
693 AtomicOrdering Ordering, SynchronizationScope SynchScope);
694 // Shadow Instruction::setInstructionSubclassData with a private forwarding
695 // method so that subclasses cannot accidentally use it.
696 void setInstructionSubclassData(unsigned short D) {
697 Instruction::setInstructionSubclassData(D);
702 struct OperandTraits<AtomicRMWInst>
703 : public FixedNumOperandTraits<AtomicRMWInst,2> {
706 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
708 //===----------------------------------------------------------------------===//
709 // GetElementPtrInst Class
710 //===----------------------------------------------------------------------===//
712 // checkGEPType - Simple wrapper function to give a better assertion failure
713 // message on bad indexes for a gep instruction.
715 inline Type *checkGEPType(Type *Ty) {
716 assert(Ty && "Invalid GetElementPtrInst indices for type!");
720 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
721 /// access elements of arrays and structs
723 class GetElementPtrInst : public Instruction {
724 GetElementPtrInst(const GetElementPtrInst &GEPI);
725 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
727 /// Constructors - Create a getelementptr instruction with a base pointer an
728 /// list of indices. The first ctor can optionally insert before an existing
729 /// instruction, the second appends the new instruction to the specified
731 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
732 unsigned Values, const Twine &NameStr,
733 Instruction *InsertBefore);
734 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
735 unsigned Values, const Twine &NameStr,
736 BasicBlock *InsertAtEnd);
738 virtual GetElementPtrInst *clone_impl() const;
740 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
741 const Twine &NameStr = "",
742 Instruction *InsertBefore = 0) {
743 unsigned Values = 1 + unsigned(IdxList.size());
745 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
747 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
748 const Twine &NameStr,
749 BasicBlock *InsertAtEnd) {
750 unsigned Values = 1 + unsigned(IdxList.size());
752 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
755 /// Create an "inbounds" getelementptr. See the documentation for the
756 /// "inbounds" flag in LangRef.html for details.
757 static GetElementPtrInst *CreateInBounds(Value *Ptr,
758 ArrayRef<Value *> IdxList,
759 const Twine &NameStr = "",
760 Instruction *InsertBefore = 0) {
761 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
762 GEP->setIsInBounds(true);
765 static GetElementPtrInst *CreateInBounds(Value *Ptr,
766 ArrayRef<Value *> IdxList,
767 const Twine &NameStr,
768 BasicBlock *InsertAtEnd) {
769 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
770 GEP->setIsInBounds(true);
774 /// Transparently provide more efficient getOperand methods.
775 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
777 // getType - Overload to return most specific sequential type.
778 SequentialType *getType() const {
779 return cast<SequentialType>(Instruction::getType());
782 /// \brief Returns the address space of this instruction's pointer type.
783 unsigned getAddressSpace() const {
784 // Note that this is always the same as the pointer operand's address space
785 // and that is cheaper to compute, so cheat here.
786 return getPointerAddressSpace();
789 /// getIndexedType - Returns the type of the element that would be loaded with
790 /// a load instruction with the specified parameters.
792 /// Null is returned if the indices are invalid for the specified
795 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
796 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
797 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
799 inline op_iterator idx_begin() { return op_begin()+1; }
800 inline const_op_iterator idx_begin() const { return op_begin()+1; }
801 inline op_iterator idx_end() { return op_end(); }
802 inline const_op_iterator idx_end() const { return op_end(); }
804 Value *getPointerOperand() {
805 return getOperand(0);
807 const Value *getPointerOperand() const {
808 return getOperand(0);
810 static unsigned getPointerOperandIndex() {
811 return 0U; // get index for modifying correct operand.
814 /// getPointerOperandType - Method to return the pointer operand as a
816 Type *getPointerOperandType() const {
817 return getPointerOperand()->getType();
820 /// \brief Returns the address space of the pointer operand.
821 unsigned getPointerAddressSpace() const {
822 return getPointerOperandType()->getPointerAddressSpace();
825 /// GetGEPReturnType - Returns the pointer type returned by the GEP
826 /// instruction, which may be a vector of pointers.
827 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
828 Type *PtrTy = PointerType::get(checkGEPType(
829 getIndexedType(Ptr->getType(), IdxList)),
830 Ptr->getType()->getPointerAddressSpace());
832 if (Ptr->getType()->isVectorTy()) {
833 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
834 return VectorType::get(PtrTy, NumElem);
841 unsigned getNumIndices() const { // Note: always non-negative
842 return getNumOperands() - 1;
845 bool hasIndices() const {
846 return getNumOperands() > 1;
849 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
850 /// zeros. If so, the result pointer and the first operand have the same
851 /// value, just potentially different types.
852 bool hasAllZeroIndices() const;
854 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
855 /// constant integers. If so, the result pointer and the first operand have
856 /// a constant offset between them.
857 bool hasAllConstantIndices() const;
859 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
860 /// See LangRef.html for the meaning of inbounds on a getelementptr.
861 void setIsInBounds(bool b = true);
863 /// isInBounds - Determine whether the GEP has the inbounds flag.
864 bool isInBounds() const;
866 /// \brief Accumulate the constant address offset of this GEP if possible.
868 /// This routine accepts an APInt into which it will accumulate the constant
869 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
870 /// all-constant, it returns false and the value of the offset APInt is
871 /// undefined (it is *not* preserved!). The APInt passed into this routine
872 /// must be at least as wide as the IntPtr type for the address space of
873 /// the base GEP pointer.
874 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
876 // Methods for support type inquiry through isa, cast, and dyn_cast:
877 static inline bool classof(const Instruction *I) {
878 return (I->getOpcode() == Instruction::GetElementPtr);
880 static inline bool classof(const Value *V) {
881 return isa<Instruction>(V) && classof(cast<Instruction>(V));
886 struct OperandTraits<GetElementPtrInst> :
887 public VariadicOperandTraits<GetElementPtrInst, 1> {
890 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
891 ArrayRef<Value *> IdxList,
893 const Twine &NameStr,
894 Instruction *InsertBefore)
895 : Instruction(getGEPReturnType(Ptr, IdxList),
897 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
898 Values, InsertBefore) {
899 init(Ptr, IdxList, NameStr);
901 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
902 ArrayRef<Value *> IdxList,
904 const Twine &NameStr,
905 BasicBlock *InsertAtEnd)
906 : Instruction(getGEPReturnType(Ptr, IdxList),
908 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
909 Values, InsertAtEnd) {
910 init(Ptr, IdxList, NameStr);
914 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
917 //===----------------------------------------------------------------------===//
919 //===----------------------------------------------------------------------===//
921 /// This instruction compares its operands according to the predicate given
922 /// to the constructor. It only operates on integers or pointers. The operands
923 /// must be identical types.
924 /// \brief Represent an integer comparison operator.
925 class ICmpInst: public CmpInst {
927 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
928 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
929 "Invalid ICmp predicate value");
930 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
931 "Both operands to ICmp instruction are not of the same type!");
932 // Check that the operands are the right type
933 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
934 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
935 "Invalid operand types for ICmp instruction");
939 /// \brief Clone an identical ICmpInst
940 virtual ICmpInst *clone_impl() const;
942 /// \brief Constructor with insert-before-instruction semantics.
944 Instruction *InsertBefore, ///< Where to insert
945 Predicate pred, ///< The predicate to use for the comparison
946 Value *LHS, ///< The left-hand-side of the expression
947 Value *RHS, ///< The right-hand-side of the expression
948 const Twine &NameStr = "" ///< Name of the instruction
949 ) : CmpInst(makeCmpResultType(LHS->getType()),
950 Instruction::ICmp, pred, LHS, RHS, NameStr,
957 /// \brief Constructor with insert-at-end semantics.
959 BasicBlock &InsertAtEnd, ///< Block to insert into.
960 Predicate pred, ///< The predicate to use for the comparison
961 Value *LHS, ///< The left-hand-side of the expression
962 Value *RHS, ///< The right-hand-side of the expression
963 const Twine &NameStr = "" ///< Name of the instruction
964 ) : CmpInst(makeCmpResultType(LHS->getType()),
965 Instruction::ICmp, pred, LHS, RHS, NameStr,
972 /// \brief Constructor with no-insertion semantics
974 Predicate pred, ///< The predicate to use for the comparison
975 Value *LHS, ///< The left-hand-side of the expression
976 Value *RHS, ///< The right-hand-side of the expression
977 const Twine &NameStr = "" ///< Name of the instruction
978 ) : CmpInst(makeCmpResultType(LHS->getType()),
979 Instruction::ICmp, pred, LHS, RHS, NameStr) {
985 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
986 /// @returns the predicate that would be the result if the operand were
987 /// regarded as signed.
988 /// \brief Return the signed version of the predicate
989 Predicate getSignedPredicate() const {
990 return getSignedPredicate(getPredicate());
993 /// This is a static version that you can use without an instruction.
994 /// \brief Return the signed version of the predicate.
995 static Predicate getSignedPredicate(Predicate pred);
997 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
998 /// @returns the predicate that would be the result if the operand were
999 /// regarded as unsigned.
1000 /// \brief Return the unsigned version of the predicate
1001 Predicate getUnsignedPredicate() const {
1002 return getUnsignedPredicate(getPredicate());
1005 /// This is a static version that you can use without an instruction.
1006 /// \brief Return the unsigned version of the predicate.
1007 static Predicate getUnsignedPredicate(Predicate pred);
1009 /// isEquality - Return true if this predicate is either EQ or NE. This also
1010 /// tests for commutativity.
1011 static bool isEquality(Predicate P) {
1012 return P == ICMP_EQ || P == ICMP_NE;
1015 /// isEquality - Return true if this predicate is either EQ or NE. This also
1016 /// tests for commutativity.
1017 bool isEquality() const {
1018 return isEquality(getPredicate());
1021 /// @returns true if the predicate of this ICmpInst is commutative
1022 /// \brief Determine if this relation is commutative.
1023 bool isCommutative() const { return isEquality(); }
1025 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1027 bool isRelational() const {
1028 return !isEquality();
1031 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1033 static bool isRelational(Predicate P) {
1034 return !isEquality(P);
1037 /// Initialize a set of values that all satisfy the predicate with C.
1038 /// \brief Make a ConstantRange for a relation with a constant value.
1039 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1041 /// Exchange the two operands to this instruction in such a way that it does
1042 /// not modify the semantics of the instruction. The predicate value may be
1043 /// changed to retain the same result if the predicate is order dependent
1045 /// \brief Swap operands and adjust predicate.
1046 void swapOperands() {
1047 setPredicate(getSwappedPredicate());
1048 Op<0>().swap(Op<1>());
1051 // Methods for support type inquiry through isa, cast, and dyn_cast:
1052 static inline bool classof(const Instruction *I) {
1053 return I->getOpcode() == Instruction::ICmp;
1055 static inline bool classof(const Value *V) {
1056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1061 //===----------------------------------------------------------------------===//
1063 //===----------------------------------------------------------------------===//
1065 /// This instruction compares its operands according to the predicate given
1066 /// to the constructor. It only operates on floating point values or packed
1067 /// vectors of floating point values. The operands must be identical types.
1068 /// \brief Represents a floating point comparison operator.
1069 class FCmpInst: public CmpInst {
1071 /// \brief Clone an identical FCmpInst
1072 virtual FCmpInst *clone_impl() const;
1074 /// \brief Constructor with insert-before-instruction semantics.
1076 Instruction *InsertBefore, ///< Where to insert
1077 Predicate pred, ///< The predicate to use for the comparison
1078 Value *LHS, ///< The left-hand-side of the expression
1079 Value *RHS, ///< The right-hand-side of the expression
1080 const Twine &NameStr = "" ///< Name of the instruction
1081 ) : CmpInst(makeCmpResultType(LHS->getType()),
1082 Instruction::FCmp, pred, LHS, RHS, NameStr,
1084 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1085 "Invalid FCmp predicate value");
1086 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1087 "Both operands to FCmp instruction are not of the same type!");
1088 // Check that the operands are the right type
1089 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1090 "Invalid operand types for FCmp instruction");
1093 /// \brief Constructor with insert-at-end semantics.
1095 BasicBlock &InsertAtEnd, ///< Block to insert into.
1096 Predicate pred, ///< The predicate to use for the comparison
1097 Value *LHS, ///< The left-hand-side of the expression
1098 Value *RHS, ///< The right-hand-side of the expression
1099 const Twine &NameStr = "" ///< Name of the instruction
1100 ) : CmpInst(makeCmpResultType(LHS->getType()),
1101 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 /// \brief Constructor with no-insertion semantics
1114 Predicate pred, ///< The predicate to use for the comparison
1115 Value *LHS, ///< The left-hand-side of the expression
1116 Value *RHS, ///< The right-hand-side of the expression
1117 const Twine &NameStr = "" ///< Name of the instruction
1118 ) : CmpInst(makeCmpResultType(LHS->getType()),
1119 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1120 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1121 "Invalid FCmp predicate value");
1122 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1123 "Both operands to FCmp instruction are not of the same type!");
1124 // Check that the operands are the right type
1125 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1126 "Invalid operand types for FCmp instruction");
1129 /// @returns true if the predicate of this instruction is EQ or NE.
1130 /// \brief Determine if this is an equality predicate.
1131 bool isEquality() const {
1132 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1133 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1136 /// @returns true if the predicate of this instruction is commutative.
1137 /// \brief Determine if this is a commutative predicate.
1138 bool isCommutative() const {
1139 return isEquality() ||
1140 getPredicate() == FCMP_FALSE ||
1141 getPredicate() == FCMP_TRUE ||
1142 getPredicate() == FCMP_ORD ||
1143 getPredicate() == FCMP_UNO;
1146 /// @returns true if the predicate is relational (not EQ or NE).
1147 /// \brief Determine if this a relational predicate.
1148 bool isRelational() const { return !isEquality(); }
1150 /// Exchange the two operands to this instruction in such a way that it does
1151 /// not modify the semantics of the instruction. The predicate value may be
1152 /// changed to retain the same result if the predicate is order dependent
1154 /// \brief Swap operands and adjust predicate.
1155 void swapOperands() {
1156 setPredicate(getSwappedPredicate());
1157 Op<0>().swap(Op<1>());
1160 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1161 static inline bool classof(const Instruction *I) {
1162 return I->getOpcode() == Instruction::FCmp;
1164 static inline bool classof(const Value *V) {
1165 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1169 //===----------------------------------------------------------------------===//
1170 /// CallInst - This class represents a function call, abstracting a target
1171 /// machine's calling convention. This class uses low bit of the SubClassData
1172 /// field to indicate whether or not this is a tail call. The rest of the bits
1173 /// hold the calling convention of the call.
1175 class CallInst : public Instruction {
1176 AttributeSet AttributeList; ///< parameter attributes for call
1177 CallInst(const CallInst &CI);
1178 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1179 void init(Value *Func, const Twine &NameStr);
1181 /// Construct a CallInst given a range of arguments.
1182 /// \brief Construct a CallInst from a range of arguments
1183 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1184 const Twine &NameStr, Instruction *InsertBefore);
1186 /// Construct a CallInst given a range of arguments.
1187 /// \brief Construct a CallInst from a range of arguments
1188 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1189 const Twine &NameStr, BasicBlock *InsertAtEnd);
1191 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1192 Instruction *InsertBefore);
1193 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1194 BasicBlock *InsertAtEnd);
1195 explicit CallInst(Value *F, const Twine &NameStr,
1196 Instruction *InsertBefore);
1197 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1199 virtual CallInst *clone_impl() const;
1201 static CallInst *Create(Value *Func,
1202 ArrayRef<Value *> Args,
1203 const Twine &NameStr = "",
1204 Instruction *InsertBefore = 0) {
1205 return new(unsigned(Args.size() + 1))
1206 CallInst(Func, Args, NameStr, InsertBefore);
1208 static CallInst *Create(Value *Func,
1209 ArrayRef<Value *> Args,
1210 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1211 return new(unsigned(Args.size() + 1))
1212 CallInst(Func, Args, NameStr, InsertAtEnd);
1214 static CallInst *Create(Value *F, const Twine &NameStr = "",
1215 Instruction *InsertBefore = 0) {
1216 return new(1) CallInst(F, NameStr, InsertBefore);
1218 static CallInst *Create(Value *F, const Twine &NameStr,
1219 BasicBlock *InsertAtEnd) {
1220 return new(1) CallInst(F, NameStr, InsertAtEnd);
1222 /// CreateMalloc - Generate the IR for a call to malloc:
1223 /// 1. Compute the malloc call's argument as the specified type's size,
1224 /// possibly multiplied by the array size if the array size is not
1226 /// 2. Call malloc with that argument.
1227 /// 3. Bitcast the result of the malloc call to the specified type.
1228 static Instruction *CreateMalloc(Instruction *InsertBefore,
1229 Type *IntPtrTy, Type *AllocTy,
1230 Value *AllocSize, Value *ArraySize = 0,
1231 Function* MallocF = 0,
1232 const Twine &Name = "");
1233 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1234 Type *IntPtrTy, Type *AllocTy,
1235 Value *AllocSize, Value *ArraySize = 0,
1236 Function* MallocF = 0,
1237 const Twine &Name = "");
1238 /// CreateFree - Generate the IR for a call to the builtin free function.
1239 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1240 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1244 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1245 void setTailCall(bool isTC = true) {
1246 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1250 /// Provide fast operand accessors
1251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1253 /// getNumArgOperands - Return the number of call arguments.
1255 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1257 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1259 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1260 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1262 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1264 CallingConv::ID getCallingConv() const {
1265 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1267 void setCallingConv(CallingConv::ID CC) {
1268 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1269 (static_cast<unsigned>(CC) << 1));
1272 /// getAttributes - Return the parameter attributes for this call.
1274 const AttributeSet &getAttributes() const { return AttributeList; }
1276 /// setAttributes - Set the parameter attributes for this call.
1278 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1280 /// addAttribute - adds the attribute to the list of attributes.
1281 void addAttribute(unsigned i, Attribute::AttrKind attr);
1283 /// removeAttribute - removes the attribute from the list of attributes.
1284 void removeAttribute(unsigned i, Attribute attr);
1286 /// \brief Determine whether this call has the given attribute.
1287 bool hasFnAttr(Attribute::AttrKind A) const {
1288 assert(A != Attribute::NoBuiltin &&
1289 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1290 return hasFnAttrImpl(A);
1293 /// \brief Determine whether the call or the callee has the given attributes.
1294 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1296 /// \brief Extract the alignment for a call or parameter (0=unknown).
1297 unsigned getParamAlignment(unsigned i) const {
1298 return AttributeList.getParamAlignment(i);
1301 /// \brief Return true if the call should not be treated as a call to a
1303 bool isNoBuiltin() const {
1304 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1305 !hasFnAttrImpl(Attribute::Builtin);
1308 /// \brief Return true if the call should not be inlined.
1309 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1310 void setIsNoInline() {
1311 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1314 /// \brief Return true if the call can return twice
1315 bool canReturnTwice() const {
1316 return hasFnAttr(Attribute::ReturnsTwice);
1318 void setCanReturnTwice() {
1319 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1322 /// \brief Determine if the call does not access memory.
1323 bool doesNotAccessMemory() const {
1324 return hasFnAttr(Attribute::ReadNone);
1326 void setDoesNotAccessMemory() {
1327 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1330 /// \brief Determine if the call does not access or only reads memory.
1331 bool onlyReadsMemory() const {
1332 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1334 void setOnlyReadsMemory() {
1335 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1338 /// \brief Determine if the call cannot return.
1339 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1340 void setDoesNotReturn() {
1341 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1344 /// \brief Determine if the call cannot unwind.
1345 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1346 void setDoesNotThrow() {
1347 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1350 /// \brief Determine if the call cannot be duplicated.
1351 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1352 void setCannotDuplicate() {
1353 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1356 /// \brief Determine if the call returns a structure through first
1357 /// pointer argument.
1358 bool hasStructRetAttr() const {
1359 // Be friendly and also check the callee.
1360 return paramHasAttr(1, Attribute::StructRet);
1363 /// \brief Determine if any call argument is an aggregate passed by value.
1364 bool hasByValArgument() const {
1365 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1368 /// getCalledFunction - Return the function called, or null if this is an
1369 /// indirect function invocation.
1371 Function *getCalledFunction() const {
1372 return dyn_cast<Function>(Op<-1>());
1375 /// getCalledValue - Get a pointer to the function that is invoked by this
1377 const Value *getCalledValue() const { return Op<-1>(); }
1378 Value *getCalledValue() { return Op<-1>(); }
1380 /// setCalledFunction - Set the function called.
1381 void setCalledFunction(Value* Fn) {
1385 /// isInlineAsm - Check if this call is an inline asm statement.
1386 bool isInlineAsm() const {
1387 return isa<InlineAsm>(Op<-1>());
1390 // Methods for support type inquiry through isa, cast, and dyn_cast:
1391 static inline bool classof(const Instruction *I) {
1392 return I->getOpcode() == Instruction::Call;
1394 static inline bool classof(const Value *V) {
1395 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1399 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1401 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1402 // method so that subclasses cannot accidentally use it.
1403 void setInstructionSubclassData(unsigned short D) {
1404 Instruction::setInstructionSubclassData(D);
1409 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1412 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1413 const Twine &NameStr, BasicBlock *InsertAtEnd)
1414 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1415 ->getElementType())->getReturnType(),
1417 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1418 unsigned(Args.size() + 1), InsertAtEnd) {
1419 init(Func, Args, NameStr);
1422 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1423 const Twine &NameStr, Instruction *InsertBefore)
1424 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1425 ->getElementType())->getReturnType(),
1427 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1428 unsigned(Args.size() + 1), InsertBefore) {
1429 init(Func, Args, NameStr);
1433 // Note: if you get compile errors about private methods then
1434 // please update your code to use the high-level operand
1435 // interfaces. See line 943 above.
1436 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1438 //===----------------------------------------------------------------------===//
1440 //===----------------------------------------------------------------------===//
1442 /// SelectInst - This class represents the LLVM 'select' instruction.
1444 class SelectInst : public Instruction {
1445 void init(Value *C, Value *S1, Value *S2) {
1446 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1452 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1453 Instruction *InsertBefore)
1454 : Instruction(S1->getType(), Instruction::Select,
1455 &Op<0>(), 3, InsertBefore) {
1459 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1460 BasicBlock *InsertAtEnd)
1461 : Instruction(S1->getType(), Instruction::Select,
1462 &Op<0>(), 3, InsertAtEnd) {
1467 virtual SelectInst *clone_impl() const;
1469 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1470 const Twine &NameStr = "",
1471 Instruction *InsertBefore = 0) {
1472 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1474 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1475 const Twine &NameStr,
1476 BasicBlock *InsertAtEnd) {
1477 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1480 const Value *getCondition() const { return Op<0>(); }
1481 const Value *getTrueValue() const { return Op<1>(); }
1482 const Value *getFalseValue() const { return Op<2>(); }
1483 Value *getCondition() { return Op<0>(); }
1484 Value *getTrueValue() { return Op<1>(); }
1485 Value *getFalseValue() { return Op<2>(); }
1487 /// areInvalidOperands - Return a string if the specified operands are invalid
1488 /// for a select operation, otherwise return null.
1489 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1491 /// Transparently provide more efficient getOperand methods.
1492 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1494 OtherOps getOpcode() const {
1495 return static_cast<OtherOps>(Instruction::getOpcode());
1498 // Methods for support type inquiry through isa, cast, and dyn_cast:
1499 static inline bool classof(const Instruction *I) {
1500 return I->getOpcode() == Instruction::Select;
1502 static inline bool classof(const Value *V) {
1503 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1508 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1511 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1513 //===----------------------------------------------------------------------===//
1515 //===----------------------------------------------------------------------===//
1517 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1518 /// an argument of the specified type given a va_list and increments that list
1520 class VAArgInst : public UnaryInstruction {
1522 virtual VAArgInst *clone_impl() const;
1525 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1526 Instruction *InsertBefore = 0)
1527 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1530 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1531 BasicBlock *InsertAtEnd)
1532 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1536 Value *getPointerOperand() { return getOperand(0); }
1537 const Value *getPointerOperand() const { return getOperand(0); }
1538 static unsigned getPointerOperandIndex() { return 0U; }
1540 // Methods for support type inquiry through isa, cast, and dyn_cast:
1541 static inline bool classof(const Instruction *I) {
1542 return I->getOpcode() == VAArg;
1544 static inline bool classof(const Value *V) {
1545 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1549 //===----------------------------------------------------------------------===//
1550 // ExtractElementInst Class
1551 //===----------------------------------------------------------------------===//
1553 /// ExtractElementInst - This instruction extracts a single (scalar)
1554 /// element from a VectorType value
1556 class ExtractElementInst : public Instruction {
1557 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1558 Instruction *InsertBefore = 0);
1559 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1560 BasicBlock *InsertAtEnd);
1562 virtual ExtractElementInst *clone_impl() const;
1565 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1566 const Twine &NameStr = "",
1567 Instruction *InsertBefore = 0) {
1568 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1570 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1571 const Twine &NameStr,
1572 BasicBlock *InsertAtEnd) {
1573 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1576 /// isValidOperands - Return true if an extractelement instruction can be
1577 /// formed with the specified operands.
1578 static bool isValidOperands(const Value *Vec, const Value *Idx);
1580 Value *getVectorOperand() { return Op<0>(); }
1581 Value *getIndexOperand() { return Op<1>(); }
1582 const Value *getVectorOperand() const { return Op<0>(); }
1583 const Value *getIndexOperand() const { return Op<1>(); }
1585 VectorType *getVectorOperandType() const {
1586 return cast<VectorType>(getVectorOperand()->getType());
1590 /// Transparently provide more efficient getOperand methods.
1591 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1593 // Methods for support type inquiry through isa, cast, and dyn_cast:
1594 static inline bool classof(const Instruction *I) {
1595 return I->getOpcode() == Instruction::ExtractElement;
1597 static inline bool classof(const Value *V) {
1598 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1603 struct OperandTraits<ExtractElementInst> :
1604 public FixedNumOperandTraits<ExtractElementInst, 2> {
1607 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1609 //===----------------------------------------------------------------------===//
1610 // InsertElementInst Class
1611 //===----------------------------------------------------------------------===//
1613 /// InsertElementInst - This instruction inserts a single (scalar)
1614 /// element into a VectorType value
1616 class InsertElementInst : public Instruction {
1617 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1618 const Twine &NameStr = "",
1619 Instruction *InsertBefore = 0);
1620 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1621 const Twine &NameStr, BasicBlock *InsertAtEnd);
1623 virtual InsertElementInst *clone_impl() const;
1626 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1627 const Twine &NameStr = "",
1628 Instruction *InsertBefore = 0) {
1629 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1631 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1632 const Twine &NameStr,
1633 BasicBlock *InsertAtEnd) {
1634 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1637 /// isValidOperands - Return true if an insertelement instruction can be
1638 /// formed with the specified operands.
1639 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1642 /// getType - Overload to return most specific vector type.
1644 VectorType *getType() const {
1645 return cast<VectorType>(Instruction::getType());
1648 /// Transparently provide more efficient getOperand methods.
1649 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1651 // Methods for support type inquiry through isa, cast, and dyn_cast:
1652 static inline bool classof(const Instruction *I) {
1653 return I->getOpcode() == Instruction::InsertElement;
1655 static inline bool classof(const Value *V) {
1656 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1661 struct OperandTraits<InsertElementInst> :
1662 public FixedNumOperandTraits<InsertElementInst, 3> {
1665 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1667 //===----------------------------------------------------------------------===//
1668 // ShuffleVectorInst Class
1669 //===----------------------------------------------------------------------===//
1671 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1674 class ShuffleVectorInst : public Instruction {
1676 virtual ShuffleVectorInst *clone_impl() const;
1679 // allocate space for exactly three operands
1680 void *operator new(size_t s) {
1681 return User::operator new(s, 3);
1683 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1684 const Twine &NameStr = "",
1685 Instruction *InsertBefor = 0);
1686 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1687 const Twine &NameStr, BasicBlock *InsertAtEnd);
1689 /// isValidOperands - Return true if a shufflevector instruction can be
1690 /// formed with the specified operands.
1691 static bool isValidOperands(const Value *V1, const Value *V2,
1694 /// getType - Overload to return most specific vector type.
1696 VectorType *getType() const {
1697 return cast<VectorType>(Instruction::getType());
1700 /// Transparently provide more efficient getOperand methods.
1701 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1703 Constant *getMask() const {
1704 return cast<Constant>(getOperand(2));
1707 /// getMaskValue - Return the index from the shuffle mask for the specified
1708 /// output result. This is either -1 if the element is undef or a number less
1709 /// than 2*numelements.
1710 static int getMaskValue(Constant *Mask, unsigned i);
1712 int getMaskValue(unsigned i) const {
1713 return getMaskValue(getMask(), i);
1716 /// getShuffleMask - Return the full mask for this instruction, where each
1717 /// element is the element number and undef's are returned as -1.
1718 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1720 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1721 return getShuffleMask(getMask(), Result);
1724 SmallVector<int, 16> getShuffleMask() const {
1725 SmallVector<int, 16> Mask;
1726 getShuffleMask(Mask);
1731 // Methods for support type inquiry through isa, cast, and dyn_cast:
1732 static inline bool classof(const Instruction *I) {
1733 return I->getOpcode() == Instruction::ShuffleVector;
1735 static inline bool classof(const Value *V) {
1736 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1741 struct OperandTraits<ShuffleVectorInst> :
1742 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1745 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1747 //===----------------------------------------------------------------------===//
1748 // ExtractValueInst Class
1749 //===----------------------------------------------------------------------===//
1751 /// ExtractValueInst - This instruction extracts a struct member or array
1752 /// element value from an aggregate value.
1754 class ExtractValueInst : public UnaryInstruction {
1755 SmallVector<unsigned, 4> Indices;
1757 ExtractValueInst(const ExtractValueInst &EVI);
1758 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1760 /// Constructors - Create a extractvalue instruction with a base aggregate
1761 /// value and a list of indices. The first ctor can optionally insert before
1762 /// an existing instruction, the second appends the new instruction to the
1763 /// specified BasicBlock.
1764 inline ExtractValueInst(Value *Agg,
1765 ArrayRef<unsigned> Idxs,
1766 const Twine &NameStr,
1767 Instruction *InsertBefore);
1768 inline ExtractValueInst(Value *Agg,
1769 ArrayRef<unsigned> Idxs,
1770 const Twine &NameStr, BasicBlock *InsertAtEnd);
1772 // allocate space for exactly one operand
1773 void *operator new(size_t s) {
1774 return User::operator new(s, 1);
1777 virtual ExtractValueInst *clone_impl() const;
1780 static ExtractValueInst *Create(Value *Agg,
1781 ArrayRef<unsigned> Idxs,
1782 const Twine &NameStr = "",
1783 Instruction *InsertBefore = 0) {
1785 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1787 static ExtractValueInst *Create(Value *Agg,
1788 ArrayRef<unsigned> Idxs,
1789 const Twine &NameStr,
1790 BasicBlock *InsertAtEnd) {
1791 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1794 /// getIndexedType - Returns the type of the element that would be extracted
1795 /// with an extractvalue instruction with the specified parameters.
1797 /// Null is returned if the indices are invalid for the specified type.
1798 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1800 typedef const unsigned* idx_iterator;
1801 inline idx_iterator idx_begin() const { return Indices.begin(); }
1802 inline idx_iterator idx_end() const { return Indices.end(); }
1804 Value *getAggregateOperand() {
1805 return getOperand(0);
1807 const Value *getAggregateOperand() const {
1808 return getOperand(0);
1810 static unsigned getAggregateOperandIndex() {
1811 return 0U; // get index for modifying correct operand
1814 ArrayRef<unsigned> getIndices() const {
1818 unsigned getNumIndices() const {
1819 return (unsigned)Indices.size();
1822 bool hasIndices() const {
1826 // Methods for support type inquiry through isa, cast, and dyn_cast:
1827 static inline bool classof(const Instruction *I) {
1828 return I->getOpcode() == Instruction::ExtractValue;
1830 static inline bool classof(const Value *V) {
1831 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1835 ExtractValueInst::ExtractValueInst(Value *Agg,
1836 ArrayRef<unsigned> Idxs,
1837 const Twine &NameStr,
1838 Instruction *InsertBefore)
1839 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1840 ExtractValue, Agg, InsertBefore) {
1841 init(Idxs, NameStr);
1843 ExtractValueInst::ExtractValueInst(Value *Agg,
1844 ArrayRef<unsigned> Idxs,
1845 const Twine &NameStr,
1846 BasicBlock *InsertAtEnd)
1847 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1848 ExtractValue, Agg, InsertAtEnd) {
1849 init(Idxs, NameStr);
1853 //===----------------------------------------------------------------------===//
1854 // InsertValueInst Class
1855 //===----------------------------------------------------------------------===//
1857 /// InsertValueInst - This instruction inserts a struct field of array element
1858 /// value into an aggregate value.
1860 class InsertValueInst : public Instruction {
1861 SmallVector<unsigned, 4> Indices;
1863 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1864 InsertValueInst(const InsertValueInst &IVI);
1865 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1866 const Twine &NameStr);
1868 /// Constructors - Create a insertvalue instruction with a base aggregate
1869 /// value, a value to insert, and a list of indices. The first ctor can
1870 /// optionally insert before an existing instruction, the second appends
1871 /// the new instruction to the specified BasicBlock.
1872 inline InsertValueInst(Value *Agg, Value *Val,
1873 ArrayRef<unsigned> Idxs,
1874 const Twine &NameStr,
1875 Instruction *InsertBefore);
1876 inline InsertValueInst(Value *Agg, Value *Val,
1877 ArrayRef<unsigned> Idxs,
1878 const Twine &NameStr, BasicBlock *InsertAtEnd);
1880 /// Constructors - These two constructors are convenience methods because one
1881 /// and two index insertvalue instructions are so common.
1882 InsertValueInst(Value *Agg, Value *Val,
1883 unsigned Idx, const Twine &NameStr = "",
1884 Instruction *InsertBefore = 0);
1885 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1886 const Twine &NameStr, BasicBlock *InsertAtEnd);
1888 virtual InsertValueInst *clone_impl() const;
1890 // allocate space for exactly two operands
1891 void *operator new(size_t s) {
1892 return User::operator new(s, 2);
1895 static InsertValueInst *Create(Value *Agg, Value *Val,
1896 ArrayRef<unsigned> Idxs,
1897 const Twine &NameStr = "",
1898 Instruction *InsertBefore = 0) {
1899 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1901 static InsertValueInst *Create(Value *Agg, Value *Val,
1902 ArrayRef<unsigned> Idxs,
1903 const Twine &NameStr,
1904 BasicBlock *InsertAtEnd) {
1905 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1908 /// Transparently provide more efficient getOperand methods.
1909 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1911 typedef const unsigned* idx_iterator;
1912 inline idx_iterator idx_begin() const { return Indices.begin(); }
1913 inline idx_iterator idx_end() const { return Indices.end(); }
1915 Value *getAggregateOperand() {
1916 return getOperand(0);
1918 const Value *getAggregateOperand() const {
1919 return getOperand(0);
1921 static unsigned getAggregateOperandIndex() {
1922 return 0U; // get index for modifying correct operand
1925 Value *getInsertedValueOperand() {
1926 return getOperand(1);
1928 const Value *getInsertedValueOperand() const {
1929 return getOperand(1);
1931 static unsigned getInsertedValueOperandIndex() {
1932 return 1U; // get index for modifying correct operand
1935 ArrayRef<unsigned> getIndices() const {
1939 unsigned getNumIndices() const {
1940 return (unsigned)Indices.size();
1943 bool hasIndices() const {
1947 // Methods for support type inquiry through isa, cast, and dyn_cast:
1948 static inline bool classof(const Instruction *I) {
1949 return I->getOpcode() == Instruction::InsertValue;
1951 static inline bool classof(const Value *V) {
1952 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1957 struct OperandTraits<InsertValueInst> :
1958 public FixedNumOperandTraits<InsertValueInst, 2> {
1961 InsertValueInst::InsertValueInst(Value *Agg,
1963 ArrayRef<unsigned> Idxs,
1964 const Twine &NameStr,
1965 Instruction *InsertBefore)
1966 : Instruction(Agg->getType(), InsertValue,
1967 OperandTraits<InsertValueInst>::op_begin(this),
1969 init(Agg, Val, Idxs, NameStr);
1971 InsertValueInst::InsertValueInst(Value *Agg,
1973 ArrayRef<unsigned> Idxs,
1974 const Twine &NameStr,
1975 BasicBlock *InsertAtEnd)
1976 : Instruction(Agg->getType(), InsertValue,
1977 OperandTraits<InsertValueInst>::op_begin(this),
1979 init(Agg, Val, Idxs, NameStr);
1982 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1984 //===----------------------------------------------------------------------===//
1986 //===----------------------------------------------------------------------===//
1988 // PHINode - The PHINode class is used to represent the magical mystical PHI
1989 // node, that can not exist in nature, but can be synthesized in a computer
1990 // scientist's overactive imagination.
1992 class PHINode : public Instruction {
1993 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1994 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1995 /// the number actually in use.
1996 unsigned ReservedSpace;
1997 PHINode(const PHINode &PN);
1998 // allocate space for exactly zero operands
1999 void *operator new(size_t s) {
2000 return User::operator new(s, 0);
2002 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2003 const Twine &NameStr = "", Instruction *InsertBefore = 0)
2004 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
2005 ReservedSpace(NumReservedValues) {
2007 OperandList = allocHungoffUses(ReservedSpace);
2010 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2011 BasicBlock *InsertAtEnd)
2012 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
2013 ReservedSpace(NumReservedValues) {
2015 OperandList = allocHungoffUses(ReservedSpace);
2018 // allocHungoffUses - this is more complicated than the generic
2019 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2020 // values and pointers to the incoming blocks, all in one allocation.
2021 Use *allocHungoffUses(unsigned) const;
2023 virtual PHINode *clone_impl() const;
2025 /// Constructors - NumReservedValues is a hint for the number of incoming
2026 /// edges that this phi node will have (use 0 if you really have no idea).
2027 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2028 const Twine &NameStr = "",
2029 Instruction *InsertBefore = 0) {
2030 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2032 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2033 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2034 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2038 /// Provide fast operand accessors
2039 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2041 // Block iterator interface. This provides access to the list of incoming
2042 // basic blocks, which parallels the list of incoming values.
2044 typedef BasicBlock **block_iterator;
2045 typedef BasicBlock * const *const_block_iterator;
2047 block_iterator block_begin() {
2049 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2050 return reinterpret_cast<block_iterator>(ref + 1);
2053 const_block_iterator block_begin() const {
2054 const Use::UserRef *ref =
2055 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2056 return reinterpret_cast<const_block_iterator>(ref + 1);
2059 block_iterator block_end() {
2060 return block_begin() + getNumOperands();
2063 const_block_iterator block_end() const {
2064 return block_begin() + getNumOperands();
2067 /// getNumIncomingValues - Return the number of incoming edges
2069 unsigned getNumIncomingValues() const { return getNumOperands(); }
2071 /// getIncomingValue - Return incoming value number x
2073 Value *getIncomingValue(unsigned i) const {
2074 return getOperand(i);
2076 void setIncomingValue(unsigned i, Value *V) {
2079 static unsigned getOperandNumForIncomingValue(unsigned i) {
2082 static unsigned getIncomingValueNumForOperand(unsigned i) {
2086 /// getIncomingBlock - Return incoming basic block number @p i.
2088 BasicBlock *getIncomingBlock(unsigned i) const {
2089 return block_begin()[i];
2092 /// getIncomingBlock - Return incoming basic block corresponding
2093 /// to an operand of the PHI.
2095 BasicBlock *getIncomingBlock(const Use &U) const {
2096 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2097 return getIncomingBlock(unsigned(&U - op_begin()));
2100 /// getIncomingBlock - Return incoming basic block corresponding
2101 /// to value use iterator.
2103 template <typename U>
2104 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2105 return getIncomingBlock(I.getUse());
2108 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2109 block_begin()[i] = BB;
2112 /// addIncoming - Add an incoming value to the end of the PHI list
2114 void addIncoming(Value *V, BasicBlock *BB) {
2115 assert(V && "PHI node got a null value!");
2116 assert(BB && "PHI node got a null basic block!");
2117 assert(getType() == V->getType() &&
2118 "All operands to PHI node must be the same type as the PHI node!");
2119 if (NumOperands == ReservedSpace)
2120 growOperands(); // Get more space!
2121 // Initialize some new operands.
2123 setIncomingValue(NumOperands - 1, V);
2124 setIncomingBlock(NumOperands - 1, BB);
2127 /// removeIncomingValue - Remove an incoming value. This is useful if a
2128 /// predecessor basic block is deleted. The value removed is returned.
2130 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2131 /// is true), the PHI node is destroyed and any uses of it are replaced with
2132 /// dummy values. The only time there should be zero incoming values to a PHI
2133 /// node is when the block is dead, so this strategy is sound.
2135 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2137 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2138 int Idx = getBasicBlockIndex(BB);
2139 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2140 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2143 /// getBasicBlockIndex - Return the first index of the specified basic
2144 /// block in the value list for this PHI. Returns -1 if no instance.
2146 int getBasicBlockIndex(const BasicBlock *BB) const {
2147 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2148 if (block_begin()[i] == BB)
2153 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2154 int Idx = getBasicBlockIndex(BB);
2155 assert(Idx >= 0 && "Invalid basic block argument!");
2156 return getIncomingValue(Idx);
2159 /// hasConstantValue - If the specified PHI node always merges together the
2160 /// same value, return the value, otherwise return null.
2161 Value *hasConstantValue() const;
2163 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2164 static inline bool classof(const Instruction *I) {
2165 return I->getOpcode() == Instruction::PHI;
2167 static inline bool classof(const Value *V) {
2168 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2171 void growOperands();
2175 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2178 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2180 //===----------------------------------------------------------------------===//
2181 // LandingPadInst Class
2182 //===----------------------------------------------------------------------===//
2184 //===---------------------------------------------------------------------------
2185 /// LandingPadInst - The landingpad instruction holds all of the information
2186 /// necessary to generate correct exception handling. The landingpad instruction
2187 /// cannot be moved from the top of a landing pad block, which itself is
2188 /// accessible only from the 'unwind' edge of an invoke. This uses the
2189 /// SubclassData field in Value to store whether or not the landingpad is a
2192 class LandingPadInst : public Instruction {
2193 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2194 /// the number actually in use.
2195 unsigned ReservedSpace;
2196 LandingPadInst(const LandingPadInst &LP);
2198 enum ClauseType { Catch, Filter };
2200 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2201 // Allocate space for exactly zero operands.
2202 void *operator new(size_t s) {
2203 return User::operator new(s, 0);
2205 void growOperands(unsigned Size);
2206 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2208 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2209 unsigned NumReservedValues, const Twine &NameStr,
2210 Instruction *InsertBefore);
2211 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2212 unsigned NumReservedValues, const Twine &NameStr,
2213 BasicBlock *InsertAtEnd);
2215 virtual LandingPadInst *clone_impl() const;
2217 /// Constructors - NumReservedClauses is a hint for the number of incoming
2218 /// clauses that this landingpad will have (use 0 if you really have no idea).
2219 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2220 unsigned NumReservedClauses,
2221 const Twine &NameStr = "",
2222 Instruction *InsertBefore = 0);
2223 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2224 unsigned NumReservedClauses,
2225 const Twine &NameStr, BasicBlock *InsertAtEnd);
2228 /// Provide fast operand accessors
2229 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2231 /// getPersonalityFn - Get the personality function associated with this
2233 Value *getPersonalityFn() const { return getOperand(0); }
2235 /// isCleanup - Return 'true' if this landingpad instruction is a
2236 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2237 /// doesn't catch the exception.
2238 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2240 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2241 void setCleanup(bool V) {
2242 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2246 /// addClause - Add a catch or filter clause to the landing pad.
2247 void addClause(Value *ClauseVal);
2249 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2250 /// to determine what type of clause this is.
2251 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2253 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2254 bool isCatch(unsigned Idx) const {
2255 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2258 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2259 bool isFilter(unsigned Idx) const {
2260 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2263 /// getNumClauses - Get the number of clauses for this landing pad.
2264 unsigned getNumClauses() const { return getNumOperands() - 1; }
2266 /// reserveClauses - Grow the size of the operand list to accommodate the new
2267 /// number of clauses.
2268 void reserveClauses(unsigned Size) { growOperands(Size); }
2270 // Methods for support type inquiry through isa, cast, and dyn_cast:
2271 static inline bool classof(const Instruction *I) {
2272 return I->getOpcode() == Instruction::LandingPad;
2274 static inline bool classof(const Value *V) {
2275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2280 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2283 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2285 //===----------------------------------------------------------------------===//
2287 //===----------------------------------------------------------------------===//
2289 //===---------------------------------------------------------------------------
2290 /// ReturnInst - Return a value (possibly void), from a function. Execution
2291 /// does not continue in this function any longer.
2293 class ReturnInst : public TerminatorInst {
2294 ReturnInst(const ReturnInst &RI);
2297 // ReturnInst constructors:
2298 // ReturnInst() - 'ret void' instruction
2299 // ReturnInst( null) - 'ret void' instruction
2300 // ReturnInst(Value* X) - 'ret X' instruction
2301 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2302 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2303 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2304 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2306 // NOTE: If the Value* passed is of type void then the constructor behaves as
2307 // if it was passed NULL.
2308 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2309 Instruction *InsertBefore = 0);
2310 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2311 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2313 virtual ReturnInst *clone_impl() const;
2315 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2316 Instruction *InsertBefore = 0) {
2317 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2319 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2320 BasicBlock *InsertAtEnd) {
2321 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2323 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2324 return new(0) ReturnInst(C, InsertAtEnd);
2326 virtual ~ReturnInst();
2328 /// Provide fast operand accessors
2329 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2331 /// Convenience accessor. Returns null if there is no return value.
2332 Value *getReturnValue() const {
2333 return getNumOperands() != 0 ? getOperand(0) : 0;
2336 unsigned getNumSuccessors() const { return 0; }
2338 // Methods for support type inquiry through isa, cast, and dyn_cast:
2339 static inline bool classof(const Instruction *I) {
2340 return (I->getOpcode() == Instruction::Ret);
2342 static inline bool classof(const Value *V) {
2343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2346 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2347 virtual unsigned getNumSuccessorsV() const;
2348 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2352 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2355 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2357 //===----------------------------------------------------------------------===//
2359 //===----------------------------------------------------------------------===//
2361 //===---------------------------------------------------------------------------
2362 /// BranchInst - Conditional or Unconditional Branch instruction.
2364 class BranchInst : public TerminatorInst {
2365 /// Ops list - Branches are strange. The operands are ordered:
2366 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2367 /// they don't have to check for cond/uncond branchness. These are mostly
2368 /// accessed relative from op_end().
2369 BranchInst(const BranchInst &BI);
2371 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2372 // BranchInst(BB *B) - 'br B'
2373 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2374 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2375 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2376 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2377 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2378 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2379 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2380 Instruction *InsertBefore = 0);
2381 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2382 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2383 BasicBlock *InsertAtEnd);
2385 virtual BranchInst *clone_impl() const;
2387 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2388 return new(1) BranchInst(IfTrue, InsertBefore);
2390 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2391 Value *Cond, Instruction *InsertBefore = 0) {
2392 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2394 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2395 return new(1) BranchInst(IfTrue, InsertAtEnd);
2397 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2398 Value *Cond, BasicBlock *InsertAtEnd) {
2399 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2402 /// Transparently provide more efficient getOperand methods.
2403 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2405 bool isUnconditional() const { return getNumOperands() == 1; }
2406 bool isConditional() const { return getNumOperands() == 3; }
2408 Value *getCondition() const {
2409 assert(isConditional() && "Cannot get condition of an uncond branch!");
2413 void setCondition(Value *V) {
2414 assert(isConditional() && "Cannot set condition of unconditional branch!");
2418 unsigned getNumSuccessors() const { return 1+isConditional(); }
2420 BasicBlock *getSuccessor(unsigned i) const {
2421 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2422 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2425 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2426 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2427 *(&Op<-1>() - idx) = (Value*)NewSucc;
2430 /// \brief Swap the successors of this branch instruction.
2432 /// Swaps the successors of the branch instruction. This also swaps any
2433 /// branch weight metadata associated with the instruction so that it
2434 /// continues to map correctly to each operand.
2435 void swapSuccessors();
2437 // Methods for support type inquiry through isa, cast, and dyn_cast:
2438 static inline bool classof(const Instruction *I) {
2439 return (I->getOpcode() == Instruction::Br);
2441 static inline bool classof(const Value *V) {
2442 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2445 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2446 virtual unsigned getNumSuccessorsV() const;
2447 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2451 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2454 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2456 //===----------------------------------------------------------------------===//
2458 //===----------------------------------------------------------------------===//
2460 //===---------------------------------------------------------------------------
2461 /// SwitchInst - Multiway switch
2463 class SwitchInst : public TerminatorInst {
2464 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2465 unsigned ReservedSpace;
2466 // Operand[0] = Value to switch on
2467 // Operand[1] = Default basic block destination
2468 // Operand[2n ] = Value to match
2469 // Operand[2n+1] = BasicBlock to go to on match
2470 SwitchInst(const SwitchInst &SI);
2471 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2472 void growOperands();
2473 // allocate space for exactly zero operands
2474 void *operator new(size_t s) {
2475 return User::operator new(s, 0);
2477 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2478 /// switch on and a default destination. The number of additional cases can
2479 /// be specified here to make memory allocation more efficient. This
2480 /// constructor can also autoinsert before another instruction.
2481 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2482 Instruction *InsertBefore);
2484 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2485 /// switch on and a default destination. The number of additional cases can
2486 /// be specified here to make memory allocation more efficient. This
2487 /// constructor also autoinserts at the end of the specified BasicBlock.
2488 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2489 BasicBlock *InsertAtEnd);
2491 virtual SwitchInst *clone_impl() const;
2495 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2497 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2498 class CaseIteratorT {
2506 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2508 /// Initializes case iterator for given SwitchInst and for given
2510 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2515 /// Initializes case iterator for given SwitchInst and for given
2516 /// TerminatorInst's successor index.
2517 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2518 assert(SuccessorIndex < SI->getNumSuccessors() &&
2519 "Successor index # out of range!");
2520 return SuccessorIndex != 0 ?
2521 Self(SI, SuccessorIndex - 1) :
2522 Self(SI, DefaultPseudoIndex);
2525 /// Resolves case value for current case.
2526 ConstantIntTy *getCaseValue() {
2527 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2528 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2531 /// Resolves successor for current case.
2532 BasicBlockTy *getCaseSuccessor() {
2533 assert((Index < SI->getNumCases() ||
2534 Index == DefaultPseudoIndex) &&
2535 "Index out the number of cases.");
2536 return SI->getSuccessor(getSuccessorIndex());
2539 /// Returns number of current case.
2540 unsigned getCaseIndex() const { return Index; }
2542 /// Returns TerminatorInst's successor index for current case successor.
2543 unsigned getSuccessorIndex() const {
2544 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2545 "Index out the number of cases.");
2546 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2550 // Check index correctness after increment.
2551 // Note: Index == getNumCases() means end().
2552 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2556 Self operator++(int) {
2562 // Check index correctness after decrement.
2563 // Note: Index == getNumCases() means end().
2564 // Also allow "-1" iterator here. That will became valid after ++.
2565 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2566 "Index out the number of cases.");
2570 Self operator--(int) {
2575 bool operator==(const Self& RHS) const {
2576 assert(RHS.SI == SI && "Incompatible operators.");
2577 return RHS.Index == Index;
2579 bool operator!=(const Self& RHS) const {
2580 assert(RHS.SI == SI && "Incompatible operators.");
2581 return RHS.Index != Index;
2585 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2588 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2590 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2594 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2595 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2597 /// Sets the new value for current case.
2598 void setValue(ConstantInt *V) {
2599 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2600 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2603 /// Sets the new successor for current case.
2604 void setSuccessor(BasicBlock *S) {
2605 SI->setSuccessor(getSuccessorIndex(), S);
2609 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2610 unsigned NumCases, Instruction *InsertBefore = 0) {
2611 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2613 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2614 unsigned NumCases, BasicBlock *InsertAtEnd) {
2615 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2620 /// Provide fast operand accessors
2621 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2623 // Accessor Methods for Switch stmt
2624 Value *getCondition() const { return getOperand(0); }
2625 void setCondition(Value *V) { setOperand(0, V); }
2627 BasicBlock *getDefaultDest() const {
2628 return cast<BasicBlock>(getOperand(1));
2631 void setDefaultDest(BasicBlock *DefaultCase) {
2632 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2635 /// getNumCases - return the number of 'cases' in this switch instruction,
2636 /// except the default case
2637 unsigned getNumCases() const {
2638 return getNumOperands()/2 - 1;
2641 /// Returns a read/write iterator that points to the first
2642 /// case in SwitchInst.
2643 CaseIt case_begin() {
2644 return CaseIt(this, 0);
2646 /// Returns a read-only iterator that points to the first
2647 /// case in the SwitchInst.
2648 ConstCaseIt case_begin() const {
2649 return ConstCaseIt(this, 0);
2652 /// Returns a read/write iterator that points one past the last
2653 /// in the SwitchInst.
2655 return CaseIt(this, getNumCases());
2657 /// Returns a read-only iterator that points one past the last
2658 /// in the SwitchInst.
2659 ConstCaseIt case_end() const {
2660 return ConstCaseIt(this, getNumCases());
2662 /// Returns an iterator that points to the default case.
2663 /// Note: this iterator allows to resolve successor only. Attempt
2664 /// to resolve case value causes an assertion.
2665 /// Also note, that increment and decrement also causes an assertion and
2666 /// makes iterator invalid.
2667 CaseIt case_default() {
2668 return CaseIt(this, DefaultPseudoIndex);
2670 ConstCaseIt case_default() const {
2671 return ConstCaseIt(this, DefaultPseudoIndex);
2674 /// findCaseValue - Search all of the case values for the specified constant.
2675 /// If it is explicitly handled, return the case iterator of it, otherwise
2676 /// return default case iterator to indicate
2677 /// that it is handled by the default handler.
2678 CaseIt findCaseValue(const ConstantInt *C) {
2679 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2680 if (i.getCaseValue() == C)
2682 return case_default();
2684 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2685 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2686 if (i.getCaseValue() == C)
2688 return case_default();
2691 /// findCaseDest - Finds the unique case value for a given successor. Returns
2692 /// null if the successor is not found, not unique, or is the default case.
2693 ConstantInt *findCaseDest(BasicBlock *BB) {
2694 if (BB == getDefaultDest()) return NULL;
2696 ConstantInt *CI = NULL;
2697 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2698 if (i.getCaseSuccessor() == BB) {
2699 if (CI) return NULL; // Multiple cases lead to BB.
2700 else CI = i.getCaseValue();
2706 /// addCase - Add an entry to the switch instruction...
2708 /// This action invalidates case_end(). Old case_end() iterator will
2709 /// point to the added case.
2710 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2712 /// removeCase - This method removes the specified case and its successor
2713 /// from the switch instruction. Note that this operation may reorder the
2714 /// remaining cases at index idx and above.
2716 /// This action invalidates iterators for all cases following the one removed,
2717 /// including the case_end() iterator.
2718 void removeCase(CaseIt i);
2720 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2721 BasicBlock *getSuccessor(unsigned idx) const {
2722 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2723 return cast<BasicBlock>(getOperand(idx*2+1));
2725 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2726 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2727 setOperand(idx*2+1, (Value*)NewSucc);
2730 // Methods for support type inquiry through isa, cast, and dyn_cast:
2731 static inline bool classof(const Instruction *I) {
2732 return I->getOpcode() == Instruction::Switch;
2734 static inline bool classof(const Value *V) {
2735 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2738 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2739 virtual unsigned getNumSuccessorsV() const;
2740 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2744 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2747 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2750 //===----------------------------------------------------------------------===//
2751 // IndirectBrInst Class
2752 //===----------------------------------------------------------------------===//
2754 //===---------------------------------------------------------------------------
2755 /// IndirectBrInst - Indirect Branch Instruction.
2757 class IndirectBrInst : public TerminatorInst {
2758 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2759 unsigned ReservedSpace;
2760 // Operand[0] = Value to switch on
2761 // Operand[1] = Default basic block destination
2762 // Operand[2n ] = Value to match
2763 // Operand[2n+1] = BasicBlock to go to on match
2764 IndirectBrInst(const IndirectBrInst &IBI);
2765 void init(Value *Address, unsigned NumDests);
2766 void growOperands();
2767 // allocate space for exactly zero operands
2768 void *operator new(size_t s) {
2769 return User::operator new(s, 0);
2771 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2772 /// Address to jump to. The number of expected destinations can be specified
2773 /// here to make memory allocation more efficient. This constructor can also
2774 /// autoinsert before another instruction.
2775 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2777 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2778 /// Address to jump to. The number of expected destinations can be specified
2779 /// here to make memory allocation more efficient. This constructor also
2780 /// autoinserts at the end of the specified BasicBlock.
2781 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2783 virtual IndirectBrInst *clone_impl() const;
2785 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2786 Instruction *InsertBefore = 0) {
2787 return new IndirectBrInst(Address, NumDests, InsertBefore);
2789 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2790 BasicBlock *InsertAtEnd) {
2791 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2795 /// Provide fast operand accessors.
2796 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2798 // Accessor Methods for IndirectBrInst instruction.
2799 Value *getAddress() { return getOperand(0); }
2800 const Value *getAddress() const { return getOperand(0); }
2801 void setAddress(Value *V) { setOperand(0, V); }
2804 /// getNumDestinations - return the number of possible destinations in this
2805 /// indirectbr instruction.
2806 unsigned getNumDestinations() const { return getNumOperands()-1; }
2808 /// getDestination - Return the specified destination.
2809 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2810 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2812 /// addDestination - Add a destination.
2814 void addDestination(BasicBlock *Dest);
2816 /// removeDestination - This method removes the specified successor from the
2817 /// indirectbr instruction.
2818 void removeDestination(unsigned i);
2820 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2821 BasicBlock *getSuccessor(unsigned i) const {
2822 return cast<BasicBlock>(getOperand(i+1));
2824 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2825 setOperand(i+1, (Value*)NewSucc);
2828 // Methods for support type inquiry through isa, cast, and dyn_cast:
2829 static inline bool classof(const Instruction *I) {
2830 return I->getOpcode() == Instruction::IndirectBr;
2832 static inline bool classof(const Value *V) {
2833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2836 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2837 virtual unsigned getNumSuccessorsV() const;
2838 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2842 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2845 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2848 //===----------------------------------------------------------------------===//
2850 //===----------------------------------------------------------------------===//
2852 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2853 /// calling convention of the call.
2855 class InvokeInst : public TerminatorInst {
2856 AttributeSet AttributeList;
2857 InvokeInst(const InvokeInst &BI);
2858 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2859 ArrayRef<Value *> Args, const Twine &NameStr);
2861 /// Construct an InvokeInst given a range of arguments.
2863 /// \brief Construct an InvokeInst from a range of arguments
2864 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2865 ArrayRef<Value *> Args, unsigned Values,
2866 const Twine &NameStr, Instruction *InsertBefore);
2868 /// Construct an InvokeInst given a range of arguments.
2870 /// \brief Construct an InvokeInst from a range of arguments
2871 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2872 ArrayRef<Value *> Args, unsigned Values,
2873 const Twine &NameStr, BasicBlock *InsertAtEnd);
2875 virtual InvokeInst *clone_impl() const;
2877 static InvokeInst *Create(Value *Func,
2878 BasicBlock *IfNormal, BasicBlock *IfException,
2879 ArrayRef<Value *> Args, const Twine &NameStr = "",
2880 Instruction *InsertBefore = 0) {
2881 unsigned Values = unsigned(Args.size()) + 3;
2882 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2883 Values, NameStr, InsertBefore);
2885 static InvokeInst *Create(Value *Func,
2886 BasicBlock *IfNormal, BasicBlock *IfException,
2887 ArrayRef<Value *> Args, const Twine &NameStr,
2888 BasicBlock *InsertAtEnd) {
2889 unsigned Values = unsigned(Args.size()) + 3;
2890 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2891 Values, NameStr, InsertAtEnd);
2894 /// Provide fast operand accessors
2895 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2897 /// getNumArgOperands - Return the number of invoke arguments.
2899 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2901 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2903 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2904 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2906 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2908 CallingConv::ID getCallingConv() const {
2909 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2911 void setCallingConv(CallingConv::ID CC) {
2912 setInstructionSubclassData(static_cast<unsigned>(CC));
2915 /// getAttributes - Return the parameter attributes for this invoke.
2917 const AttributeSet &getAttributes() const { return AttributeList; }
2919 /// setAttributes - Set the parameter attributes for this invoke.
2921 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
2923 /// addAttribute - adds the attribute to the list of attributes.
2924 void addAttribute(unsigned i, Attribute::AttrKind attr);
2926 /// removeAttribute - removes the attribute from the list of attributes.
2927 void removeAttribute(unsigned i, Attribute attr);
2929 /// \brief Determine whether this call has the given attribute.
2930 bool hasFnAttr(Attribute::AttrKind A) const {
2931 assert(A != Attribute::NoBuiltin &&
2932 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
2933 return hasFnAttrImpl(A);
2936 /// \brief Determine whether the call or the callee has the given attributes.
2937 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
2939 /// \brief Extract the alignment for a call or parameter (0=unknown).
2940 unsigned getParamAlignment(unsigned i) const {
2941 return AttributeList.getParamAlignment(i);
2944 /// \brief Return true if the call should not be treated as a call to a
2946 bool isNoBuiltin() const {
2947 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
2948 // to check it by hand.
2949 return hasFnAttrImpl(Attribute::NoBuiltin) &&
2950 !hasFnAttrImpl(Attribute::Builtin);
2953 /// \brief Return true if the call should not be inlined.
2954 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
2955 void setIsNoInline() {
2956 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
2959 /// \brief Determine if the call does not access memory.
2960 bool doesNotAccessMemory() const {
2961 return hasFnAttr(Attribute::ReadNone);
2963 void setDoesNotAccessMemory() {
2964 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
2967 /// \brief Determine if the call does not access or only reads memory.
2968 bool onlyReadsMemory() const {
2969 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
2971 void setOnlyReadsMemory() {
2972 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
2975 /// \brief Determine if the call cannot return.
2976 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
2977 void setDoesNotReturn() {
2978 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
2981 /// \brief Determine if the call cannot unwind.
2982 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
2983 void setDoesNotThrow() {
2984 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
2987 /// \brief Determine if the call returns a structure through first
2988 /// pointer argument.
2989 bool hasStructRetAttr() const {
2990 // Be friendly and also check the callee.
2991 return paramHasAttr(1, Attribute::StructRet);
2994 /// \brief Determine if any call argument is an aggregate passed by value.
2995 bool hasByValArgument() const {
2996 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2999 /// getCalledFunction - Return the function called, or null if this is an
3000 /// indirect function invocation.
3002 Function *getCalledFunction() const {
3003 return dyn_cast<Function>(Op<-3>());
3006 /// getCalledValue - Get a pointer to the function that is invoked by this
3008 const Value *getCalledValue() const { return Op<-3>(); }
3009 Value *getCalledValue() { return Op<-3>(); }
3011 /// setCalledFunction - Set the function called.
3012 void setCalledFunction(Value* Fn) {
3016 // get*Dest - Return the destination basic blocks...
3017 BasicBlock *getNormalDest() const {
3018 return cast<BasicBlock>(Op<-2>());
3020 BasicBlock *getUnwindDest() const {
3021 return cast<BasicBlock>(Op<-1>());
3023 void setNormalDest(BasicBlock *B) {
3024 Op<-2>() = reinterpret_cast<Value*>(B);
3026 void setUnwindDest(BasicBlock *B) {
3027 Op<-1>() = reinterpret_cast<Value*>(B);
3030 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3031 /// block (the unwind destination).
3032 LandingPadInst *getLandingPadInst() const;
3034 BasicBlock *getSuccessor(unsigned i) const {
3035 assert(i < 2 && "Successor # out of range for invoke!");
3036 return i == 0 ? getNormalDest() : getUnwindDest();
3039 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3040 assert(idx < 2 && "Successor # out of range for invoke!");
3041 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3044 unsigned getNumSuccessors() const { return 2; }
3046 // Methods for support type inquiry through isa, cast, and dyn_cast:
3047 static inline bool classof(const Instruction *I) {
3048 return (I->getOpcode() == Instruction::Invoke);
3050 static inline bool classof(const Value *V) {
3051 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3055 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3056 virtual unsigned getNumSuccessorsV() const;
3057 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3059 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3061 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3062 // method so that subclasses cannot accidentally use it.
3063 void setInstructionSubclassData(unsigned short D) {
3064 Instruction::setInstructionSubclassData(D);
3069 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3072 InvokeInst::InvokeInst(Value *Func,
3073 BasicBlock *IfNormal, BasicBlock *IfException,
3074 ArrayRef<Value *> Args, unsigned Values,
3075 const Twine &NameStr, Instruction *InsertBefore)
3076 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3077 ->getElementType())->getReturnType(),
3078 Instruction::Invoke,
3079 OperandTraits<InvokeInst>::op_end(this) - Values,
3080 Values, InsertBefore) {
3081 init(Func, IfNormal, IfException, Args, NameStr);
3083 InvokeInst::InvokeInst(Value *Func,
3084 BasicBlock *IfNormal, BasicBlock *IfException,
3085 ArrayRef<Value *> Args, unsigned Values,
3086 const Twine &NameStr, BasicBlock *InsertAtEnd)
3087 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3088 ->getElementType())->getReturnType(),
3089 Instruction::Invoke,
3090 OperandTraits<InvokeInst>::op_end(this) - Values,
3091 Values, InsertAtEnd) {
3092 init(Func, IfNormal, IfException, Args, NameStr);
3095 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3097 //===----------------------------------------------------------------------===//
3099 //===----------------------------------------------------------------------===//
3101 //===---------------------------------------------------------------------------
3102 /// ResumeInst - Resume the propagation of an exception.
3104 class ResumeInst : public TerminatorInst {
3105 ResumeInst(const ResumeInst &RI);
3107 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3108 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3110 virtual ResumeInst *clone_impl() const;
3112 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3113 return new(1) ResumeInst(Exn, InsertBefore);
3115 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3116 return new(1) ResumeInst(Exn, InsertAtEnd);
3119 /// Provide fast operand accessors
3120 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3122 /// Convenience accessor.
3123 Value *getValue() const { return Op<0>(); }
3125 unsigned getNumSuccessors() const { return 0; }
3127 // Methods for support type inquiry through isa, cast, and dyn_cast:
3128 static inline bool classof(const Instruction *I) {
3129 return I->getOpcode() == Instruction::Resume;
3131 static inline bool classof(const Value *V) {
3132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3135 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3136 virtual unsigned getNumSuccessorsV() const;
3137 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3141 struct OperandTraits<ResumeInst> :
3142 public FixedNumOperandTraits<ResumeInst, 1> {
3145 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3147 //===----------------------------------------------------------------------===//
3148 // UnreachableInst Class
3149 //===----------------------------------------------------------------------===//
3151 //===---------------------------------------------------------------------------
3152 /// UnreachableInst - This function has undefined behavior. In particular, the
3153 /// presence of this instruction indicates some higher level knowledge that the
3154 /// end of the block cannot be reached.
3156 class UnreachableInst : public TerminatorInst {
3157 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3159 virtual UnreachableInst *clone_impl() const;
3162 // allocate space for exactly zero operands
3163 void *operator new(size_t s) {
3164 return User::operator new(s, 0);
3166 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3167 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3169 unsigned getNumSuccessors() const { return 0; }
3171 // Methods for support type inquiry through isa, cast, and dyn_cast:
3172 static inline bool classof(const Instruction *I) {
3173 return I->getOpcode() == Instruction::Unreachable;
3175 static inline bool classof(const Value *V) {
3176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3179 virtual BasicBlock *getSuccessorV(unsigned idx) const;
3180 virtual unsigned getNumSuccessorsV() const;
3181 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3184 //===----------------------------------------------------------------------===//
3186 //===----------------------------------------------------------------------===//
3188 /// \brief This class represents a truncation of integer types.
3189 class TruncInst : public CastInst {
3191 /// \brief Clone an identical TruncInst
3192 virtual TruncInst *clone_impl() const;
3195 /// \brief Constructor with insert-before-instruction semantics
3197 Value *S, ///< The value to be truncated
3198 Type *Ty, ///< The (smaller) type to truncate to
3199 const Twine &NameStr = "", ///< A name for the new instruction
3200 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3203 /// \brief Constructor with insert-at-end-of-block semantics
3205 Value *S, ///< The value to be truncated
3206 Type *Ty, ///< The (smaller) type to truncate to
3207 const Twine &NameStr, ///< A name for the new instruction
3208 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3211 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3212 static inline bool classof(const Instruction *I) {
3213 return I->getOpcode() == Trunc;
3215 static inline bool classof(const Value *V) {
3216 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3220 //===----------------------------------------------------------------------===//
3222 //===----------------------------------------------------------------------===//
3224 /// \brief This class represents zero extension of integer types.
3225 class ZExtInst : public CastInst {
3227 /// \brief Clone an identical ZExtInst
3228 virtual ZExtInst *clone_impl() const;
3231 /// \brief Constructor with insert-before-instruction semantics
3233 Value *S, ///< The value to be zero extended
3234 Type *Ty, ///< The type to zero extend to
3235 const Twine &NameStr = "", ///< A name for the new instruction
3236 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3239 /// \brief Constructor with insert-at-end semantics.
3241 Value *S, ///< The value to be zero extended
3242 Type *Ty, ///< The type to zero extend to
3243 const Twine &NameStr, ///< A name for the new instruction
3244 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3247 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3248 static inline bool classof(const Instruction *I) {
3249 return I->getOpcode() == ZExt;
3251 static inline bool classof(const Value *V) {
3252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3256 //===----------------------------------------------------------------------===//
3258 //===----------------------------------------------------------------------===//
3260 /// \brief This class represents a sign extension of integer types.
3261 class SExtInst : public CastInst {
3263 /// \brief Clone an identical SExtInst
3264 virtual SExtInst *clone_impl() const;
3267 /// \brief Constructor with insert-before-instruction semantics
3269 Value *S, ///< The value to be sign extended
3270 Type *Ty, ///< The type to sign extend to
3271 const Twine &NameStr = "", ///< A name for the new instruction
3272 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3275 /// \brief Constructor with insert-at-end-of-block semantics
3277 Value *S, ///< The value to be sign extended
3278 Type *Ty, ///< The type to sign extend to
3279 const Twine &NameStr, ///< A name for the new instruction
3280 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3283 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3284 static inline bool classof(const Instruction *I) {
3285 return I->getOpcode() == SExt;
3287 static inline bool classof(const Value *V) {
3288 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3292 //===----------------------------------------------------------------------===//
3293 // FPTruncInst Class
3294 //===----------------------------------------------------------------------===//
3296 /// \brief This class represents a truncation of floating point types.
3297 class FPTruncInst : public CastInst {
3299 /// \brief Clone an identical FPTruncInst
3300 virtual FPTruncInst *clone_impl() const;
3303 /// \brief Constructor with insert-before-instruction semantics
3305 Value *S, ///< The value to be truncated
3306 Type *Ty, ///< The type to truncate to
3307 const Twine &NameStr = "", ///< A name for the new instruction
3308 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3311 /// \brief Constructor with insert-before-instruction semantics
3313 Value *S, ///< The value to be truncated
3314 Type *Ty, ///< The type to truncate to
3315 const Twine &NameStr, ///< A name for the new instruction
3316 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3319 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3320 static inline bool classof(const Instruction *I) {
3321 return I->getOpcode() == FPTrunc;
3323 static inline bool classof(const Value *V) {
3324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3328 //===----------------------------------------------------------------------===//
3330 //===----------------------------------------------------------------------===//
3332 /// \brief This class represents an extension of floating point types.
3333 class FPExtInst : public CastInst {
3335 /// \brief Clone an identical FPExtInst
3336 virtual FPExtInst *clone_impl() const;
3339 /// \brief Constructor with insert-before-instruction semantics
3341 Value *S, ///< The value to be extended
3342 Type *Ty, ///< The type to extend to
3343 const Twine &NameStr = "", ///< A name for the new instruction
3344 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3347 /// \brief Constructor with insert-at-end-of-block semantics
3349 Value *S, ///< The value to be extended
3350 Type *Ty, ///< The type to extend to
3351 const Twine &NameStr, ///< A name for the new instruction
3352 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3355 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3356 static inline bool classof(const Instruction *I) {
3357 return I->getOpcode() == FPExt;
3359 static inline bool classof(const Value *V) {
3360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3364 //===----------------------------------------------------------------------===//
3366 //===----------------------------------------------------------------------===//
3368 /// \brief This class represents a cast unsigned integer to floating point.
3369 class UIToFPInst : public CastInst {
3371 /// \brief Clone an identical UIToFPInst
3372 virtual UIToFPInst *clone_impl() const;
3375 /// \brief Constructor with insert-before-instruction semantics
3377 Value *S, ///< The value to be converted
3378 Type *Ty, ///< The type to convert to
3379 const Twine &NameStr = "", ///< A name for the new instruction
3380 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3383 /// \brief Constructor with insert-at-end-of-block semantics
3385 Value *S, ///< The value to be converted
3386 Type *Ty, ///< The type to convert to
3387 const Twine &NameStr, ///< A name for the new instruction
3388 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3391 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3392 static inline bool classof(const Instruction *I) {
3393 return I->getOpcode() == UIToFP;
3395 static inline bool classof(const Value *V) {
3396 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3400 //===----------------------------------------------------------------------===//
3402 //===----------------------------------------------------------------------===//
3404 /// \brief This class represents a cast from signed integer to floating point.
3405 class SIToFPInst : public CastInst {
3407 /// \brief Clone an identical SIToFPInst
3408 virtual SIToFPInst *clone_impl() const;
3411 /// \brief Constructor with insert-before-instruction semantics
3413 Value *S, ///< The value to be converted
3414 Type *Ty, ///< The type to convert to
3415 const Twine &NameStr = "", ///< A name for the new instruction
3416 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3419 /// \brief Constructor with insert-at-end-of-block semantics
3421 Value *S, ///< The value to be converted
3422 Type *Ty, ///< The type to convert to
3423 const Twine &NameStr, ///< A name for the new instruction
3424 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3427 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3428 static inline bool classof(const Instruction *I) {
3429 return I->getOpcode() == SIToFP;
3431 static inline bool classof(const Value *V) {
3432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3436 //===----------------------------------------------------------------------===//
3438 //===----------------------------------------------------------------------===//
3440 /// \brief This class represents a cast from floating point to unsigned integer
3441 class FPToUIInst : public CastInst {
3443 /// \brief Clone an identical FPToUIInst
3444 virtual FPToUIInst *clone_impl() const;
3447 /// \brief Constructor with insert-before-instruction semantics
3449 Value *S, ///< The value to be converted
3450 Type *Ty, ///< The type to convert to
3451 const Twine &NameStr = "", ///< A name for the new instruction
3452 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3455 /// \brief Constructor with insert-at-end-of-block semantics
3457 Value *S, ///< The value to be converted
3458 Type *Ty, ///< The type to convert to
3459 const Twine &NameStr, ///< A name for the new instruction
3460 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3463 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3464 static inline bool classof(const Instruction *I) {
3465 return I->getOpcode() == FPToUI;
3467 static inline bool classof(const Value *V) {
3468 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3472 //===----------------------------------------------------------------------===//
3474 //===----------------------------------------------------------------------===//
3476 /// \brief This class represents a cast from floating point to signed integer.
3477 class FPToSIInst : public CastInst {
3479 /// \brief Clone an identical FPToSIInst
3480 virtual FPToSIInst *clone_impl() const;
3483 /// \brief Constructor with insert-before-instruction semantics
3485 Value *S, ///< The value to be converted
3486 Type *Ty, ///< The type to convert to
3487 const Twine &NameStr = "", ///< A name for the new instruction
3488 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3491 /// \brief Constructor with insert-at-end-of-block semantics
3493 Value *S, ///< The value to be converted
3494 Type *Ty, ///< The type to convert to
3495 const Twine &NameStr, ///< A name for the new instruction
3496 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3499 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3500 static inline bool classof(const Instruction *I) {
3501 return I->getOpcode() == FPToSI;
3503 static inline bool classof(const Value *V) {
3504 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3508 //===----------------------------------------------------------------------===//
3509 // IntToPtrInst Class
3510 //===----------------------------------------------------------------------===//
3512 /// \brief This class represents a cast from an integer to a pointer.
3513 class IntToPtrInst : public CastInst {
3515 /// \brief Constructor with insert-before-instruction semantics
3517 Value *S, ///< The value to be converted
3518 Type *Ty, ///< The type to convert to
3519 const Twine &NameStr = "", ///< A name for the new instruction
3520 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3523 /// \brief Constructor with insert-at-end-of-block semantics
3525 Value *S, ///< The value to be converted
3526 Type *Ty, ///< The type to convert to
3527 const Twine &NameStr, ///< A name for the new instruction
3528 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3531 /// \brief Clone an identical IntToPtrInst
3532 virtual IntToPtrInst *clone_impl() const;
3534 /// \brief Returns the address space of this instruction's pointer type.
3535 unsigned getAddressSpace() const {
3536 return getType()->getPointerAddressSpace();
3539 // Methods for support type inquiry through isa, cast, and dyn_cast:
3540 static inline bool classof(const Instruction *I) {
3541 return I->getOpcode() == IntToPtr;
3543 static inline bool classof(const Value *V) {
3544 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3548 //===----------------------------------------------------------------------===//
3549 // PtrToIntInst Class
3550 //===----------------------------------------------------------------------===//
3552 /// \brief This class represents a cast from a pointer to an integer
3553 class PtrToIntInst : public CastInst {
3555 /// \brief Clone an identical PtrToIntInst
3556 virtual PtrToIntInst *clone_impl() const;
3559 /// \brief Constructor with insert-before-instruction semantics
3561 Value *S, ///< The value to be converted
3562 Type *Ty, ///< The type to convert to
3563 const Twine &NameStr = "", ///< A name for the new instruction
3564 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3567 /// \brief Constructor with insert-at-end-of-block semantics
3569 Value *S, ///< The value to be converted
3570 Type *Ty, ///< The type to convert to
3571 const Twine &NameStr, ///< A name for the new instruction
3572 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3575 /// \brief Gets the pointer operand.
3576 Value *getPointerOperand() { return getOperand(0); }
3577 /// \brief Gets the pointer operand.
3578 const Value *getPointerOperand() const { return getOperand(0); }
3579 /// \brief Gets the operand index of the pointer operand.
3580 static unsigned getPointerOperandIndex() { return 0U; }
3582 /// \brief Returns the address space of the pointer operand.
3583 unsigned getPointerAddressSpace() const {
3584 return getPointerOperand()->getType()->getPointerAddressSpace();
3587 // Methods for support type inquiry through isa, cast, and dyn_cast:
3588 static inline bool classof(const Instruction *I) {
3589 return I->getOpcode() == PtrToInt;
3591 static inline bool classof(const Value *V) {
3592 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3596 //===----------------------------------------------------------------------===//
3597 // BitCastInst Class
3598 //===----------------------------------------------------------------------===//
3600 /// \brief This class represents a no-op cast from one type to another.
3601 class BitCastInst : public CastInst {
3603 /// \brief Clone an identical BitCastInst
3604 virtual BitCastInst *clone_impl() const;
3607 /// \brief Constructor with insert-before-instruction semantics
3609 Value *S, ///< The value to be casted
3610 Type *Ty, ///< The type to casted to
3611 const Twine &NameStr = "", ///< A name for the new instruction
3612 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3615 /// \brief Constructor with insert-at-end-of-block semantics
3617 Value *S, ///< The value to be casted
3618 Type *Ty, ///< The type to casted to
3619 const Twine &NameStr, ///< A name for the new instruction
3620 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3623 // Methods for support type inquiry through isa, cast, and dyn_cast:
3624 static inline bool classof(const Instruction *I) {
3625 return I->getOpcode() == BitCast;
3627 static inline bool classof(const Value *V) {
3628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3632 //===----------------------------------------------------------------------===//
3633 // AddrSpaceCastInst Class
3634 //===----------------------------------------------------------------------===//
3636 /// \brief This class represents a conversion between pointers from
3637 /// one address space to another.
3638 class AddrSpaceCastInst : public CastInst {
3640 /// \brief Clone an identical AddrSpaceCastInst
3641 virtual AddrSpaceCastInst *clone_impl() const;
3644 /// \brief Constructor with insert-before-instruction semantics
3646 Value *S, ///< The value to be casted
3647 Type *Ty, ///< The type to casted to
3648 const Twine &NameStr = "", ///< A name for the new instruction
3649 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3652 /// \brief Constructor with insert-at-end-of-block semantics
3654 Value *S, ///< The value to be casted
3655 Type *Ty, ///< The type to casted to
3656 const Twine &NameStr, ///< A name for the new instruction
3657 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3660 // Methods for support type inquiry through isa, cast, and dyn_cast:
3661 static inline bool classof(const Instruction *I) {
3662 return I->getOpcode() == AddrSpaceCast;
3664 static inline bool classof(const Value *V) {
3665 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3669 } // End llvm namespace