1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
43 AllocationInst(const Type *Ty, Value *ArraySize,
44 unsigned iTy, unsigned Align, const Twine &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const Twine &Name,
48 BasicBlock *InsertAtEnd);
50 // Out of line virtual method, so the vtable, etc. has a home.
51 virtual ~AllocationInst();
53 /// isArrayAllocation - Return true if there is an allocation size parameter
54 /// to the allocation instruction that is not 1.
56 bool isArrayAllocation() const;
58 /// getArraySize - Get the number of elements allocated. For a simple
59 /// allocation of a single element, this will return a constant 1 value.
61 const Value *getArraySize() const { return getOperand(0); }
62 Value *getArraySize() { return getOperand(0); }
64 /// getType - Overload to return most specific pointer type
66 const PointerType *getType() const {
67 return reinterpret_cast<const PointerType*>(Instruction::getType());
70 /// getAllocatedType - Return the type that is being allocated by the
73 const Type *getAllocatedType() const;
75 /// getAlignment - Return the alignment of the memory that is being allocated
76 /// by the instruction.
78 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
79 void setAlignment(unsigned Align);
81 virtual AllocationInst *clone(LLVMContext &Context) const = 0;
83 // Methods for support type inquiry through isa, cast, and dyn_cast:
84 static inline bool classof(const AllocationInst *) { return true; }
85 static inline bool classof(const Instruction *I) {
86 return I->getOpcode() == Instruction::Alloca ||
87 I->getOpcode() == Instruction::Malloc;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// MallocInst - an instruction to allocated memory on the heap
101 class MallocInst : public AllocationInst {
103 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
104 const Twine &NameStr = "",
105 Instruction *InsertBefore = 0)
106 : AllocationInst(Ty, ArraySize, Malloc,
107 0, NameStr, InsertBefore) {}
108 MallocInst(const Type *Ty, Value *ArraySize,
109 const Twine &NameStr, BasicBlock *InsertAtEnd)
110 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
112 MallocInst(const Type *Ty, const Twine &NameStr,
113 Instruction *InsertBefore = 0)
114 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
115 MallocInst(const Type *Ty, const Twine &NameStr,
116 BasicBlock *InsertAtEnd)
117 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
119 MallocInst(const Type *Ty, Value *ArraySize,
120 unsigned Align, const Twine &NameStr,
121 BasicBlock *InsertAtEnd)
122 : AllocationInst(Ty, ArraySize, Malloc,
123 Align, NameStr, InsertAtEnd) {}
124 MallocInst(const Type *Ty, Value *ArraySize,
125 unsigned Align, const Twine &NameStr = "",
126 Instruction *InsertBefore = 0)
127 : AllocationInst(Ty, ArraySize,
128 Malloc, Align, NameStr, InsertBefore) {}
130 virtual MallocInst *clone(LLVMContext &Context) const;
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const MallocInst *) { return true; }
134 static inline bool classof(const Instruction *I) {
135 return (I->getOpcode() == Instruction::Malloc);
137 static inline bool classof(const Value *V) {
138 return isa<Instruction>(V) && classof(cast<Instruction>(V));
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 /// AllocaInst - an instruction to allocate memory on the stack
149 class AllocaInst : public AllocationInst {
151 explicit AllocaInst(const Type *Ty,
152 Value *ArraySize = 0,
153 const Twine &NameStr = "",
154 Instruction *InsertBefore = 0)
155 : AllocationInst(Ty, ArraySize, Alloca,
156 0, NameStr, InsertBefore) {}
157 AllocaInst(const Type *Ty,
158 Value *ArraySize, const Twine &NameStr,
159 BasicBlock *InsertAtEnd)
160 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
162 AllocaInst(const Type *Ty, const Twine &NameStr,
163 Instruction *InsertBefore = 0)
164 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
165 AllocaInst(const Type *Ty, const Twine &NameStr,
166 BasicBlock *InsertAtEnd)
167 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
169 AllocaInst(const Type *Ty, Value *ArraySize,
170 unsigned Align, const Twine &NameStr = "",
171 Instruction *InsertBefore = 0)
172 : AllocationInst(Ty, ArraySize, Alloca,
173 Align, NameStr, InsertBefore) {}
174 AllocaInst(const Type *Ty, Value *ArraySize,
175 unsigned Align, const Twine &NameStr,
176 BasicBlock *InsertAtEnd)
177 : AllocationInst(Ty, ArraySize, Alloca,
178 Align, NameStr, InsertAtEnd) {}
180 virtual AllocaInst *clone(LLVMContext &Context) const;
182 /// isStaticAlloca - Return true if this alloca is in the entry block of the
183 /// function and is a constant size. If so, the code generator will fold it
184 /// into the prolog/epilog code, so it is basically free.
185 bool isStaticAlloca() const;
187 // Methods for support type inquiry through isa, cast, and dyn_cast:
188 static inline bool classof(const AllocaInst *) { return true; }
189 static inline bool classof(const Instruction *I) {
190 return (I->getOpcode() == Instruction::Alloca);
192 static inline bool classof(const Value *V) {
193 return isa<Instruction>(V) && classof(cast<Instruction>(V));
198 //===----------------------------------------------------------------------===//
200 //===----------------------------------------------------------------------===//
202 /// FreeInst - an instruction to deallocate memory
204 class FreeInst : public UnaryInstruction {
207 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
208 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
210 virtual FreeInst *clone(LLVMContext &Context) const;
212 // Accessor methods for consistency with other memory operations
213 Value *getPointerOperand() { return getOperand(0); }
214 const Value *getPointerOperand() const { return getOperand(0); }
216 // Methods for support type inquiry through isa, cast, and dyn_cast:
217 static inline bool classof(const FreeInst *) { return true; }
218 static inline bool classof(const Instruction *I) {
219 return (I->getOpcode() == Instruction::Free);
221 static inline bool classof(const Value *V) {
222 return isa<Instruction>(V) && classof(cast<Instruction>(V));
227 //===----------------------------------------------------------------------===//
229 //===----------------------------------------------------------------------===//
231 /// LoadInst - an instruction for reading from memory. This uses the
232 /// SubclassData field in Value to store whether or not the load is volatile.
234 class LoadInst : public UnaryInstruction {
237 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
238 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
239 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
240 Instruction *InsertBefore = 0);
241 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
242 unsigned Align, Instruction *InsertBefore = 0);
243 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
244 BasicBlock *InsertAtEnd);
245 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
246 unsigned Align, BasicBlock *InsertAtEnd);
248 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
249 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
250 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
251 bool isVolatile = false, Instruction *InsertBefore = 0);
252 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
253 BasicBlock *InsertAtEnd);
255 /// isVolatile - Return true if this is a load from a volatile memory
258 bool isVolatile() const { return SubclassData & 1; }
260 /// setVolatile - Specify whether this is a volatile load or not.
262 void setVolatile(bool V) {
263 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
266 virtual LoadInst *clone(LLVMContext &Context) const;
268 /// getAlignment - Return the alignment of the access that is being performed
270 unsigned getAlignment() const {
271 return (1 << (SubclassData>>1)) >> 1;
274 void setAlignment(unsigned Align);
276 Value *getPointerOperand() { return getOperand(0); }
277 const Value *getPointerOperand() const { return getOperand(0); }
278 static unsigned getPointerOperandIndex() { return 0U; }
280 unsigned getPointerAddressSpace() const {
281 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
285 // Methods for support type inquiry through isa, cast, and dyn_cast:
286 static inline bool classof(const LoadInst *) { return true; }
287 static inline bool classof(const Instruction *I) {
288 return I->getOpcode() == Instruction::Load;
290 static inline bool classof(const Value *V) {
291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
296 //===----------------------------------------------------------------------===//
298 //===----------------------------------------------------------------------===//
300 /// StoreInst - an instruction for storing to memory
302 class StoreInst : public Instruction {
303 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
306 // allocate space for exactly two operands
307 void *operator new(size_t s) {
308 return User::operator new(s, 2);
310 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
311 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
312 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
313 Instruction *InsertBefore = 0);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
315 unsigned Align, Instruction *InsertBefore = 0);
316 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
318 unsigned Align, BasicBlock *InsertAtEnd);
321 /// isVolatile - Return true if this is a load from a volatile memory
324 bool isVolatile() const { return SubclassData & 1; }
326 /// setVolatile - Specify whether this is a volatile load or not.
328 void setVolatile(bool V) {
329 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
332 /// Transparently provide more efficient getOperand methods.
333 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
335 /// getAlignment - Return the alignment of the access that is being performed
337 unsigned getAlignment() const {
338 return (1 << (SubclassData>>1)) >> 1;
341 void setAlignment(unsigned Align);
343 virtual StoreInst *clone(LLVMContext &Context) const;
345 Value *getPointerOperand() { return getOperand(1); }
346 const Value *getPointerOperand() const { return getOperand(1); }
347 static unsigned getPointerOperandIndex() { return 1U; }
349 unsigned getPointerAddressSpace() const {
350 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
353 // Methods for support type inquiry through isa, cast, and dyn_cast:
354 static inline bool classof(const StoreInst *) { return true; }
355 static inline bool classof(const Instruction *I) {
356 return I->getOpcode() == Instruction::Store;
358 static inline bool classof(const Value *V) {
359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
364 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
367 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
369 //===----------------------------------------------------------------------===//
370 // GetElementPtrInst Class
371 //===----------------------------------------------------------------------===//
373 // checkType - Simple wrapper function to give a better assertion failure
374 // message on bad indexes for a gep instruction.
376 static inline const Type *checkType(const Type *Ty) {
377 assert(Ty && "Invalid GetElementPtrInst indices for type!");
381 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
382 /// access elements of arrays and structs
384 class GetElementPtrInst : public Instruction {
385 GetElementPtrInst(const GetElementPtrInst &GEPI);
386 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
387 const Twine &NameStr);
388 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
390 template<typename InputIterator>
391 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
392 const Twine &NameStr,
393 // This argument ensures that we have an iterator we can
394 // do arithmetic on in constant time
395 std::random_access_iterator_tag) {
396 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
399 // This requires that the iterator points to contiguous memory.
400 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
401 // we have to build an array here
404 init(Ptr, 0, NumIdx, NameStr);
408 /// getIndexedType - Returns the type of the element that would be loaded with
409 /// a load instruction with the specified parameters.
411 /// Null is returned if the indices are invalid for the specified
414 template<typename InputIterator>
415 static const Type *getIndexedType(const Type *Ptr,
416 InputIterator IdxBegin,
417 InputIterator IdxEnd,
418 // This argument ensures that we
419 // have an iterator we can do
420 // arithmetic on in constant time
421 std::random_access_iterator_tag) {
422 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
425 // This requires that the iterator points to contiguous memory.
426 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
428 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
431 /// Constructors - Create a getelementptr instruction with a base pointer an
432 /// list of indices. The first ctor can optionally insert before an existing
433 /// instruction, the second appends the new instruction to the specified
435 template<typename InputIterator>
436 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
437 InputIterator IdxEnd,
439 const Twine &NameStr,
440 Instruction *InsertBefore);
441 template<typename InputIterator>
442 inline GetElementPtrInst(Value *Ptr,
443 InputIterator IdxBegin, InputIterator IdxEnd,
445 const Twine &NameStr, BasicBlock *InsertAtEnd);
447 /// Constructors - These two constructors are convenience methods because one
448 /// and two index getelementptr instructions are so common.
449 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
450 Instruction *InsertBefore = 0);
451 GetElementPtrInst(Value *Ptr, Value *Idx,
452 const Twine &NameStr, BasicBlock *InsertAtEnd);
454 template<typename InputIterator>
455 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
456 InputIterator IdxEnd,
457 const Twine &NameStr = "",
458 Instruction *InsertBefore = 0) {
459 typename std::iterator_traits<InputIterator>::difference_type Values =
460 1 + std::distance(IdxBegin, IdxEnd);
462 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
464 template<typename InputIterator>
465 static GetElementPtrInst *Create(Value *Ptr,
466 InputIterator IdxBegin, InputIterator IdxEnd,
467 const Twine &NameStr,
468 BasicBlock *InsertAtEnd) {
469 typename std::iterator_traits<InputIterator>::difference_type Values =
470 1 + std::distance(IdxBegin, IdxEnd);
472 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
475 /// Constructors - These two creators are convenience methods because one
476 /// index getelementptr instructions are so common.
477 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
478 const Twine &NameStr = "",
479 Instruction *InsertBefore = 0) {
480 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
482 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
483 const Twine &NameStr,
484 BasicBlock *InsertAtEnd) {
485 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
488 /// Create an "inbounds" getelementptr. See the documentation for the
489 /// "inbounds" flag in LangRef.html for details.
490 template<typename InputIterator>
491 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
492 InputIterator IdxEnd,
493 const Twine &NameStr = "",
494 Instruction *InsertBefore = 0) {
495 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
496 NameStr, InsertBefore);
497 cast<GEPOperator>(GEP)->setIsInBounds(true);
500 template<typename InputIterator>
501 static GetElementPtrInst *CreateInBounds(Value *Ptr,
502 InputIterator IdxBegin,
503 InputIterator IdxEnd,
504 const Twine &NameStr,
505 BasicBlock *InsertAtEnd) {
506 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
507 NameStr, InsertAtEnd);
508 cast<GEPOperator>(GEP)->setIsInBounds(true);
511 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
512 const Twine &NameStr = "",
513 Instruction *InsertBefore = 0) {
514 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
515 cast<GEPOperator>(GEP)->setIsInBounds(true);
518 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
519 const Twine &NameStr,
520 BasicBlock *InsertAtEnd) {
521 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
522 cast<GEPOperator>(GEP)->setIsInBounds(true);
526 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
528 /// Transparently provide more efficient getOperand methods.
529 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
531 // getType - Overload to return most specific pointer type...
532 const PointerType *getType() const {
533 return reinterpret_cast<const PointerType*>(Instruction::getType());
536 /// getIndexedType - Returns the type of the element that would be loaded with
537 /// a load instruction with the specified parameters.
539 /// Null is returned if the indices are invalid for the specified
542 template<typename InputIterator>
543 static const Type *getIndexedType(const Type *Ptr,
544 InputIterator IdxBegin,
545 InputIterator IdxEnd) {
546 return getIndexedType(Ptr, IdxBegin, IdxEnd,
547 typename std::iterator_traits<InputIterator>::
548 iterator_category());
551 static const Type *getIndexedType(const Type *Ptr,
552 Value* const *Idx, unsigned NumIdx);
554 static const Type *getIndexedType(const Type *Ptr,
555 uint64_t const *Idx, unsigned NumIdx);
557 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
559 inline op_iterator idx_begin() { return op_begin()+1; }
560 inline const_op_iterator idx_begin() const { return op_begin()+1; }
561 inline op_iterator idx_end() { return op_end(); }
562 inline const_op_iterator idx_end() const { return op_end(); }
564 Value *getPointerOperand() {
565 return getOperand(0);
567 const Value *getPointerOperand() const {
568 return getOperand(0);
570 static unsigned getPointerOperandIndex() {
571 return 0U; // get index for modifying correct operand
574 unsigned getPointerAddressSpace() const {
575 return cast<PointerType>(getType())->getAddressSpace();
578 /// getPointerOperandType - Method to return the pointer operand as a
580 const PointerType *getPointerOperandType() const {
581 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
585 unsigned getNumIndices() const { // Note: always non-negative
586 return getNumOperands() - 1;
589 bool hasIndices() const {
590 return getNumOperands() > 1;
593 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
594 /// zeros. If so, the result pointer and the first operand have the same
595 /// value, just potentially different types.
596 bool hasAllZeroIndices() const;
598 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
599 /// constant integers. If so, the result pointer and the first operand have
600 /// a constant offset between them.
601 bool hasAllConstantIndices() const;
603 // Methods for support type inquiry through isa, cast, and dyn_cast:
604 static inline bool classof(const GetElementPtrInst *) { return true; }
605 static inline bool classof(const Instruction *I) {
606 return (I->getOpcode() == Instruction::GetElementPtr);
608 static inline bool classof(const Value *V) {
609 return isa<Instruction>(V) && classof(cast<Instruction>(V));
614 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
617 template<typename InputIterator>
618 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
619 InputIterator IdxBegin,
620 InputIterator IdxEnd,
622 const Twine &NameStr,
623 Instruction *InsertBefore)
624 : Instruction(PointerType::get(checkType(
625 getIndexedType(Ptr->getType(),
627 cast<PointerType>(Ptr->getType())
628 ->getAddressSpace()),
630 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
631 Values, InsertBefore) {
632 init(Ptr, IdxBegin, IdxEnd, NameStr,
633 typename std::iterator_traits<InputIterator>::iterator_category());
635 template<typename InputIterator>
636 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
637 InputIterator IdxBegin,
638 InputIterator IdxEnd,
640 const Twine &NameStr,
641 BasicBlock *InsertAtEnd)
642 : Instruction(PointerType::get(checkType(
643 getIndexedType(Ptr->getType(),
645 cast<PointerType>(Ptr->getType())
646 ->getAddressSpace()),
648 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
649 Values, InsertAtEnd) {
650 init(Ptr, IdxBegin, IdxEnd, NameStr,
651 typename std::iterator_traits<InputIterator>::iterator_category());
655 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
658 //===----------------------------------------------------------------------===//
660 //===----------------------------------------------------------------------===//
662 /// This instruction compares its operands according to the predicate given
663 /// to the constructor. It only operates on integers or pointers. The operands
664 /// must be identical types.
665 /// @brief Represent an integer comparison operator.
666 class ICmpInst: public CmpInst {
668 /// @brief Constructor with insert-before-instruction semantics.
670 Instruction *InsertBefore, ///< Where to insert
671 Predicate pred, ///< The predicate to use for the comparison
672 Value *LHS, ///< The left-hand-side of the expression
673 Value *RHS, ///< The right-hand-side of the expression
674 const Twine &NameStr = "" ///< Name of the instruction
675 ) : CmpInst(makeCmpResultType(LHS->getType()),
676 Instruction::ICmp, pred, LHS, RHS, NameStr,
678 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
679 pred <= CmpInst::LAST_ICMP_PREDICATE &&
680 "Invalid ICmp predicate value");
681 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
682 "Both operands to ICmp instruction are not of the same type!");
683 // Check that the operands are the right type
684 assert((getOperand(0)->getType()->isIntOrIntVector() ||
685 isa<PointerType>(getOperand(0)->getType())) &&
686 "Invalid operand types for ICmp instruction");
689 /// @brief Constructor with insert-at-end semantics.
691 BasicBlock &InsertAtEnd, ///< Block to insert into.
692 Predicate pred, ///< The predicate to use for the comparison
693 Value *LHS, ///< The left-hand-side of the expression
694 Value *RHS, ///< The right-hand-side of the expression
695 const Twine &NameStr = "" ///< Name of the instruction
696 ) : CmpInst(makeCmpResultType(LHS->getType()),
697 Instruction::ICmp, pred, LHS, RHS, NameStr,
699 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
700 pred <= CmpInst::LAST_ICMP_PREDICATE &&
701 "Invalid ICmp predicate value");
702 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
703 "Both operands to ICmp instruction are not of the same type!");
704 // Check that the operands are the right type
705 assert((getOperand(0)->getType()->isIntOrIntVector() ||
706 isa<PointerType>(getOperand(0)->getType())) &&
707 "Invalid operand types for ICmp instruction");
710 /// @brief Constructor with no-insertion semantics
712 Predicate pred, ///< The predicate to use for the comparison
713 Value *LHS, ///< The left-hand-side of the expression
714 Value *RHS, ///< The right-hand-side of the expression
715 const Twine &NameStr = "" ///< Name of the instruction
716 ) : CmpInst(makeCmpResultType(LHS->getType()),
717 Instruction::ICmp, pred, LHS, RHS, NameStr) {
718 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
719 pred <= CmpInst::LAST_ICMP_PREDICATE &&
720 "Invalid ICmp predicate value");
721 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
722 "Both operands to ICmp instruction are not of the same type!");
723 // Check that the operands are the right type
724 assert((getOperand(0)->getType()->isIntOrIntVector() ||
725 isa<PointerType>(getOperand(0)->getType())) &&
726 "Invalid operand types for ICmp instruction");
729 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
730 /// @returns the predicate that would be the result if the operand were
731 /// regarded as signed.
732 /// @brief Return the signed version of the predicate
733 Predicate getSignedPredicate() const {
734 return getSignedPredicate(getPredicate());
737 /// This is a static version that you can use without an instruction.
738 /// @brief Return the signed version of the predicate.
739 static Predicate getSignedPredicate(Predicate pred);
741 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
742 /// @returns the predicate that would be the result if the operand were
743 /// regarded as unsigned.
744 /// @brief Return the unsigned version of the predicate
745 Predicate getUnsignedPredicate() const {
746 return getUnsignedPredicate(getPredicate());
749 /// This is a static version that you can use without an instruction.
750 /// @brief Return the unsigned version of the predicate.
751 static Predicate getUnsignedPredicate(Predicate pred);
753 /// isEquality - Return true if this predicate is either EQ or NE. This also
754 /// tests for commutativity.
755 static bool isEquality(Predicate P) {
756 return P == ICMP_EQ || P == ICMP_NE;
759 /// isEquality - Return true if this predicate is either EQ or NE. This also
760 /// tests for commutativity.
761 bool isEquality() const {
762 return isEquality(getPredicate());
765 /// @returns true if the predicate of this ICmpInst is commutative
766 /// @brief Determine if this relation is commutative.
767 bool isCommutative() const { return isEquality(); }
769 /// isRelational - Return true if the predicate is relational (not EQ or NE).
771 bool isRelational() const {
772 return !isEquality();
775 /// isRelational - Return true if the predicate is relational (not EQ or NE).
777 static bool isRelational(Predicate P) {
778 return !isEquality(P);
781 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
782 /// @brief Determine if this instruction's predicate is signed.
783 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
785 /// @returns true if the predicate provided is signed, false otherwise
786 /// @brief Determine if the predicate is signed.
787 static bool isSignedPredicate(Predicate pred);
789 /// @returns true if the specified compare predicate is
790 /// true when both operands are equal...
791 /// @brief Determine if the icmp is true when both operands are equal
792 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
793 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
794 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
795 pred == ICmpInst::ICMP_SLE;
798 /// @returns true if the specified compare instruction is
799 /// true when both operands are equal...
800 /// @brief Determine if the ICmpInst returns true when both operands are equal
801 bool isTrueWhenEqual() {
802 return isTrueWhenEqual(getPredicate());
805 /// Initialize a set of values that all satisfy the predicate with C.
806 /// @brief Make a ConstantRange for a relation with a constant value.
807 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
809 /// Exchange the two operands to this instruction in such a way that it does
810 /// not modify the semantics of the instruction. The predicate value may be
811 /// changed to retain the same result if the predicate is order dependent
813 /// @brief Swap operands and adjust predicate.
814 void swapOperands() {
815 SubclassData = getSwappedPredicate();
816 Op<0>().swap(Op<1>());
819 virtual ICmpInst *clone(LLVMContext &Context) const;
821 // Methods for support type inquiry through isa, cast, and dyn_cast:
822 static inline bool classof(const ICmpInst *) { return true; }
823 static inline bool classof(const Instruction *I) {
824 return I->getOpcode() == Instruction::ICmp;
826 static inline bool classof(const Value *V) {
827 return isa<Instruction>(V) && classof(cast<Instruction>(V));
832 //===----------------------------------------------------------------------===//
834 //===----------------------------------------------------------------------===//
836 /// This instruction compares its operands according to the predicate given
837 /// to the constructor. It only operates on floating point values or packed
838 /// vectors of floating point values. The operands must be identical types.
839 /// @brief Represents a floating point comparison operator.
840 class FCmpInst: public CmpInst {
842 /// @brief Constructor with insert-before-instruction semantics.
844 Instruction *InsertBefore, ///< Where to insert
845 Predicate pred, ///< The predicate to use for the comparison
846 Value *LHS, ///< The left-hand-side of the expression
847 Value *RHS, ///< The right-hand-side of the expression
848 const Twine &NameStr = "" ///< Name of the instruction
849 ) : CmpInst(makeCmpResultType(LHS->getType()),
850 Instruction::FCmp, pred, LHS, RHS, NameStr,
852 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
853 "Invalid FCmp predicate value");
854 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
855 "Both operands to FCmp instruction are not of the same type!");
856 // Check that the operands are the right type
857 assert(getOperand(0)->getType()->isFPOrFPVector() &&
858 "Invalid operand types for FCmp instruction");
861 /// @brief Constructor with insert-at-end semantics.
863 BasicBlock &InsertAtEnd, ///< Block to insert into.
864 Predicate pred, ///< The predicate to use for the comparison
865 Value *LHS, ///< The left-hand-side of the expression
866 Value *RHS, ///< The right-hand-side of the expression
867 const Twine &NameStr = "" ///< Name of the instruction
868 ) : CmpInst(makeCmpResultType(LHS->getType()),
869 Instruction::FCmp, pred, LHS, RHS, NameStr,
871 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
872 "Invalid FCmp predicate value");
873 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
874 "Both operands to FCmp instruction are not of the same type!");
875 // Check that the operands are the right type
876 assert(getOperand(0)->getType()->isFPOrFPVector() &&
877 "Invalid operand types for FCmp instruction");
880 /// @brief Constructor with no-insertion semantics
882 Predicate pred, ///< The predicate to use for the comparison
883 Value *LHS, ///< The left-hand-side of the expression
884 Value *RHS, ///< The right-hand-side of the expression
885 const Twine &NameStr = "" ///< Name of the instruction
886 ) : CmpInst(makeCmpResultType(LHS->getType()),
887 Instruction::FCmp, pred, LHS, RHS, NameStr) {
888 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
889 "Invalid FCmp predicate value");
890 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
891 "Both operands to FCmp instruction are not of the same type!");
892 // Check that the operands are the right type
893 assert(getOperand(0)->getType()->isFPOrFPVector() &&
894 "Invalid operand types for FCmp instruction");
897 /// @returns true if the predicate of this instruction is EQ or NE.
898 /// @brief Determine if this is an equality predicate.
899 bool isEquality() const {
900 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
901 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
904 /// @returns true if the predicate of this instruction is commutative.
905 /// @brief Determine if this is a commutative predicate.
906 bool isCommutative() const {
907 return isEquality() ||
908 SubclassData == FCMP_FALSE ||
909 SubclassData == FCMP_TRUE ||
910 SubclassData == FCMP_ORD ||
911 SubclassData == FCMP_UNO;
914 /// @returns true if the predicate is relational (not EQ or NE).
915 /// @brief Determine if this a relational predicate.
916 bool isRelational() const { return !isEquality(); }
918 /// Exchange the two operands to this instruction in such a way that it does
919 /// not modify the semantics of the instruction. The predicate value may be
920 /// changed to retain the same result if the predicate is order dependent
922 /// @brief Swap operands and adjust predicate.
923 void swapOperands() {
924 SubclassData = getSwappedPredicate();
925 Op<0>().swap(Op<1>());
928 virtual FCmpInst *clone(LLVMContext &Context) const;
930 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
931 static inline bool classof(const FCmpInst *) { return true; }
932 static inline bool classof(const Instruction *I) {
933 return I->getOpcode() == Instruction::FCmp;
935 static inline bool classof(const Value *V) {
936 return isa<Instruction>(V) && classof(cast<Instruction>(V));
940 //===----------------------------------------------------------------------===//
942 //===----------------------------------------------------------------------===//
943 /// CallInst - This class represents a function call, abstracting a target
944 /// machine's calling convention. This class uses low bit of the SubClassData
945 /// field to indicate whether or not this is a tail call. The rest of the bits
946 /// hold the calling convention of the call.
949 class CallInst : public Instruction {
950 AttrListPtr AttributeList; ///< parameter attributes for call
951 CallInst(const CallInst &CI);
952 void init(Value *Func, Value* const *Params, unsigned NumParams);
953 void init(Value *Func, Value *Actual1, Value *Actual2);
954 void init(Value *Func, Value *Actual);
955 void init(Value *Func);
957 template<typename InputIterator>
958 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
959 const Twine &NameStr,
960 // This argument ensures that we have an iterator we can
961 // do arithmetic on in constant time
962 std::random_access_iterator_tag) {
963 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
965 // This requires that the iterator points to contiguous memory.
966 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
970 /// Construct a CallInst given a range of arguments. InputIterator
971 /// must be a random-access iterator pointing to contiguous storage
972 /// (e.g. a std::vector<>::iterator). Checks are made for
973 /// random-accessness but not for contiguous storage as that would
974 /// incur runtime overhead.
975 /// @brief Construct a CallInst from a range of arguments
976 template<typename InputIterator>
977 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
978 const Twine &NameStr, Instruction *InsertBefore);
980 /// Construct a CallInst given a range of arguments. InputIterator
981 /// must be a random-access iterator pointing to contiguous storage
982 /// (e.g. a std::vector<>::iterator). Checks are made for
983 /// random-accessness but not for contiguous storage as that would
984 /// incur runtime overhead.
985 /// @brief Construct a CallInst from a range of arguments
986 template<typename InputIterator>
987 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
988 const Twine &NameStr, BasicBlock *InsertAtEnd);
990 CallInst(Value *F, Value *Actual, const Twine &NameStr,
991 Instruction *InsertBefore);
992 CallInst(Value *F, Value *Actual, const Twine &NameStr,
993 BasicBlock *InsertAtEnd);
994 explicit CallInst(Value *F, const Twine &NameStr,
995 Instruction *InsertBefore);
996 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
998 template<typename InputIterator>
999 static CallInst *Create(Value *Func,
1000 InputIterator ArgBegin, InputIterator ArgEnd,
1001 const Twine &NameStr = "",
1002 Instruction *InsertBefore = 0) {
1003 return new((unsigned)(ArgEnd - ArgBegin + 1))
1004 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1006 template<typename InputIterator>
1007 static CallInst *Create(Value *Func,
1008 InputIterator ArgBegin, InputIterator ArgEnd,
1009 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1010 return new((unsigned)(ArgEnd - ArgBegin + 1))
1011 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1013 static CallInst *Create(Value *F, Value *Actual,
1014 const Twine &NameStr = "",
1015 Instruction *InsertBefore = 0) {
1016 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1018 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1019 BasicBlock *InsertAtEnd) {
1020 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1022 static CallInst *Create(Value *F, const Twine &NameStr = "",
1023 Instruction *InsertBefore = 0) {
1024 return new(1) CallInst(F, NameStr, InsertBefore);
1026 static CallInst *Create(Value *F, const Twine &NameStr,
1027 BasicBlock *InsertAtEnd) {
1028 return new(1) CallInst(F, NameStr, InsertAtEnd);
1033 bool isTailCall() const { return SubclassData & 1; }
1034 void setTailCall(bool isTC = true) {
1035 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1038 virtual CallInst *clone(LLVMContext &Context) const;
1040 /// Provide fast operand accessors
1041 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1043 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1045 unsigned getCallingConv() const { return SubclassData >> 1; }
1046 void setCallingConv(unsigned CC) {
1047 SubclassData = (SubclassData & 1) | (CC << 1);
1050 /// getAttributes - Return the parameter attributes for this call.
1052 const AttrListPtr &getAttributes() const { return AttributeList; }
1054 /// setAttributes - Set the parameter attributes for this call.
1056 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1058 /// addAttribute - adds the attribute to the list of attributes.
1059 void addAttribute(unsigned i, Attributes attr);
1061 /// removeAttribute - removes the attribute from the list of attributes.
1062 void removeAttribute(unsigned i, Attributes attr);
1064 /// @brief Determine whether the call or the callee has the given attribute.
1065 bool paramHasAttr(unsigned i, Attributes attr) const;
1067 /// @brief Extract the alignment for a call or parameter (0=unknown).
1068 unsigned getParamAlignment(unsigned i) const {
1069 return AttributeList.getParamAlignment(i);
1072 /// @brief Determine if the call does not access memory.
1073 bool doesNotAccessMemory() const {
1074 return paramHasAttr(~0, Attribute::ReadNone);
1076 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1077 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1078 else removeAttribute(~0, Attribute::ReadNone);
1081 /// @brief Determine if the call does not access or only reads memory.
1082 bool onlyReadsMemory() const {
1083 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1085 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1086 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1087 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1090 /// @brief Determine if the call cannot return.
1091 bool doesNotReturn() const {
1092 return paramHasAttr(~0, Attribute::NoReturn);
1094 void setDoesNotReturn(bool DoesNotReturn = true) {
1095 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1096 else removeAttribute(~0, Attribute::NoReturn);
1099 /// @brief Determine if the call cannot unwind.
1100 bool doesNotThrow() const {
1101 return paramHasAttr(~0, Attribute::NoUnwind);
1103 void setDoesNotThrow(bool DoesNotThrow = true) {
1104 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1105 else removeAttribute(~0, Attribute::NoUnwind);
1108 /// @brief Determine if the call returns a structure through first
1109 /// pointer argument.
1110 bool hasStructRetAttr() const {
1111 // Be friendly and also check the callee.
1112 return paramHasAttr(1, Attribute::StructRet);
1115 /// @brief Determine if any call argument is an aggregate passed by value.
1116 bool hasByValArgument() const {
1117 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1120 /// getCalledFunction - Return the function called, or null if this is an
1121 /// indirect function invocation.
1123 Function *getCalledFunction() const {
1124 return dyn_cast<Function>(Op<0>());
1127 /// getCalledValue - Get a pointer to the function that is invoked by this
1129 const Value *getCalledValue() const { return Op<0>(); }
1130 Value *getCalledValue() { return Op<0>(); }
1132 // Methods for support type inquiry through isa, cast, and dyn_cast:
1133 static inline bool classof(const CallInst *) { return true; }
1134 static inline bool classof(const Instruction *I) {
1135 return I->getOpcode() == Instruction::Call;
1137 static inline bool classof(const Value *V) {
1138 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1143 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1146 template<typename InputIterator>
1147 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1148 const Twine &NameStr, BasicBlock *InsertAtEnd)
1149 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1150 ->getElementType())->getReturnType(),
1152 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1153 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1154 init(Func, ArgBegin, ArgEnd, NameStr,
1155 typename std::iterator_traits<InputIterator>::iterator_category());
1158 template<typename InputIterator>
1159 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1160 const Twine &NameStr, Instruction *InsertBefore)
1161 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1162 ->getElementType())->getReturnType(),
1164 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1165 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1166 init(Func, ArgBegin, ArgEnd, NameStr,
1167 typename std::iterator_traits<InputIterator>::iterator_category());
1170 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1172 //===----------------------------------------------------------------------===//
1174 //===----------------------------------------------------------------------===//
1176 /// SelectInst - This class represents the LLVM 'select' instruction.
1178 class SelectInst : public Instruction {
1179 void init(Value *C, Value *S1, Value *S2) {
1180 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1186 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1187 Instruction *InsertBefore)
1188 : Instruction(S1->getType(), Instruction::Select,
1189 &Op<0>(), 3, InsertBefore) {
1193 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1194 BasicBlock *InsertAtEnd)
1195 : Instruction(S1->getType(), Instruction::Select,
1196 &Op<0>(), 3, InsertAtEnd) {
1201 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1202 const Twine &NameStr = "",
1203 Instruction *InsertBefore = 0) {
1204 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1206 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1207 const Twine &NameStr,
1208 BasicBlock *InsertAtEnd) {
1209 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1212 Value *getCondition() const { return Op<0>(); }
1213 Value *getTrueValue() const { return Op<1>(); }
1214 Value *getFalseValue() const { return Op<2>(); }
1216 /// areInvalidOperands - Return a string if the specified operands are invalid
1217 /// for a select operation, otherwise return null.
1218 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1220 /// Transparently provide more efficient getOperand methods.
1221 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1223 OtherOps getOpcode() const {
1224 return static_cast<OtherOps>(Instruction::getOpcode());
1227 virtual SelectInst *clone(LLVMContext &Context) const;
1229 // Methods for support type inquiry through isa, cast, and dyn_cast:
1230 static inline bool classof(const SelectInst *) { return true; }
1231 static inline bool classof(const Instruction *I) {
1232 return I->getOpcode() == Instruction::Select;
1234 static inline bool classof(const Value *V) {
1235 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1240 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1243 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1245 //===----------------------------------------------------------------------===//
1247 //===----------------------------------------------------------------------===//
1249 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1250 /// an argument of the specified type given a va_list and increments that list
1252 class VAArgInst : public UnaryInstruction {
1254 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1255 Instruction *InsertBefore = 0)
1256 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1259 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1260 BasicBlock *InsertAtEnd)
1261 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1265 virtual VAArgInst *clone(LLVMContext &Context) const;
1267 // Methods for support type inquiry through isa, cast, and dyn_cast:
1268 static inline bool classof(const VAArgInst *) { return true; }
1269 static inline bool classof(const Instruction *I) {
1270 return I->getOpcode() == VAArg;
1272 static inline bool classof(const Value *V) {
1273 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1277 //===----------------------------------------------------------------------===//
1278 // ExtractElementInst Class
1279 //===----------------------------------------------------------------------===//
1281 /// ExtractElementInst - This instruction extracts a single (scalar)
1282 /// element from a VectorType value
1284 class ExtractElementInst : public Instruction {
1285 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1286 Instruction *InsertBefore = 0);
1287 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1288 BasicBlock *InsertAtEnd);
1290 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1291 const Twine &NameStr = "",
1292 Instruction *InsertBefore = 0) {
1293 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1295 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1296 const Twine &NameStr,
1297 BasicBlock *InsertAtEnd) {
1298 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1301 /// isValidOperands - Return true if an extractelement instruction can be
1302 /// formed with the specified operands.
1303 static bool isValidOperands(const Value *Vec, const Value *Idx);
1305 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1307 /// Transparently provide more efficient getOperand methods.
1308 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1310 // Methods for support type inquiry through isa, cast, and dyn_cast:
1311 static inline bool classof(const ExtractElementInst *) { return true; }
1312 static inline bool classof(const Instruction *I) {
1313 return I->getOpcode() == Instruction::ExtractElement;
1315 static inline bool classof(const Value *V) {
1316 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1321 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1324 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1326 //===----------------------------------------------------------------------===//
1327 // InsertElementInst Class
1328 //===----------------------------------------------------------------------===//
1330 /// InsertElementInst - This instruction inserts a single (scalar)
1331 /// element into a VectorType value
1333 class InsertElementInst : public Instruction {
1334 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1335 const Twine &NameStr = "",
1336 Instruction *InsertBefore = 0);
1337 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1338 const Twine &NameStr, BasicBlock *InsertAtEnd);
1340 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1341 const Twine &NameStr = "",
1342 Instruction *InsertBefore = 0) {
1343 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1345 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1346 const Twine &NameStr,
1347 BasicBlock *InsertAtEnd) {
1348 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1351 /// isValidOperands - Return true if an insertelement instruction can be
1352 /// formed with the specified operands.
1353 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1356 virtual InsertElementInst *clone(LLVMContext &Context) const;
1358 /// getType - Overload to return most specific vector type.
1360 const VectorType *getType() const {
1361 return reinterpret_cast<const VectorType*>(Instruction::getType());
1364 /// Transparently provide more efficient getOperand methods.
1365 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1367 // Methods for support type inquiry through isa, cast, and dyn_cast:
1368 static inline bool classof(const InsertElementInst *) { return true; }
1369 static inline bool classof(const Instruction *I) {
1370 return I->getOpcode() == Instruction::InsertElement;
1372 static inline bool classof(const Value *V) {
1373 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1378 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1381 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1383 //===----------------------------------------------------------------------===//
1384 // ShuffleVectorInst Class
1385 //===----------------------------------------------------------------------===//
1387 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1390 class ShuffleVectorInst : public Instruction {
1392 // allocate space for exactly three operands
1393 void *operator new(size_t s) {
1394 return User::operator new(s, 3);
1396 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1397 const Twine &NameStr = "",
1398 Instruction *InsertBefor = 0);
1399 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1400 const Twine &NameStr, BasicBlock *InsertAtEnd);
1402 /// isValidOperands - Return true if a shufflevector instruction can be
1403 /// formed with the specified operands.
1404 static bool isValidOperands(const Value *V1, const Value *V2,
1407 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1409 /// getType - Overload to return most specific vector type.
1411 const VectorType *getType() const {
1412 return reinterpret_cast<const VectorType*>(Instruction::getType());
1415 /// Transparently provide more efficient getOperand methods.
1416 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1418 /// getMaskValue - Return the index from the shuffle mask for the specified
1419 /// output result. This is either -1 if the element is undef or a number less
1420 /// than 2*numelements.
1421 int getMaskValue(unsigned i) const;
1423 // Methods for support type inquiry through isa, cast, and dyn_cast:
1424 static inline bool classof(const ShuffleVectorInst *) { return true; }
1425 static inline bool classof(const Instruction *I) {
1426 return I->getOpcode() == Instruction::ShuffleVector;
1428 static inline bool classof(const Value *V) {
1429 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1434 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1437 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1439 //===----------------------------------------------------------------------===//
1440 // ExtractValueInst Class
1441 //===----------------------------------------------------------------------===//
1443 /// ExtractValueInst - This instruction extracts a struct member or array
1444 /// element value from an aggregate value.
1446 class ExtractValueInst : public UnaryInstruction {
1447 SmallVector<unsigned, 4> Indices;
1449 ExtractValueInst(const ExtractValueInst &EVI);
1450 void init(const unsigned *Idx, unsigned NumIdx,
1451 const Twine &NameStr);
1452 void init(unsigned Idx, const Twine &NameStr);
1454 template<typename InputIterator>
1455 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1456 const Twine &NameStr,
1457 // This argument ensures that we have an iterator we can
1458 // do arithmetic on in constant time
1459 std::random_access_iterator_tag) {
1460 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1462 // There's no fundamental reason why we require at least one index
1463 // (other than weirdness with &*IdxBegin being invalid; see
1464 // getelementptr's init routine for example). But there's no
1465 // present need to support it.
1466 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1468 // This requires that the iterator points to contiguous memory.
1469 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1470 // we have to build an array here
1473 /// getIndexedType - Returns the type of the element that would be extracted
1474 /// with an extractvalue instruction with the specified parameters.
1476 /// Null is returned if the indices are invalid for the specified
1479 static const Type *getIndexedType(const Type *Agg,
1480 const unsigned *Idx, unsigned NumIdx);
1482 template<typename InputIterator>
1483 static const Type *getIndexedType(const Type *Ptr,
1484 InputIterator IdxBegin,
1485 InputIterator IdxEnd,
1486 // This argument ensures that we
1487 // have an iterator we can do
1488 // arithmetic on in constant time
1489 std::random_access_iterator_tag) {
1490 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1493 // This requires that the iterator points to contiguous memory.
1494 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1496 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1499 /// Constructors - Create a extractvalue instruction with a base aggregate
1500 /// value and a list of indices. The first ctor can optionally insert before
1501 /// an existing instruction, the second appends the new instruction to the
1502 /// specified BasicBlock.
1503 template<typename InputIterator>
1504 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1505 InputIterator IdxEnd,
1506 const Twine &NameStr,
1507 Instruction *InsertBefore);
1508 template<typename InputIterator>
1509 inline ExtractValueInst(Value *Agg,
1510 InputIterator IdxBegin, InputIterator IdxEnd,
1511 const Twine &NameStr, BasicBlock *InsertAtEnd);
1513 // allocate space for exactly one operand
1514 void *operator new(size_t s) {
1515 return User::operator new(s, 1);
1519 template<typename InputIterator>
1520 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1521 InputIterator IdxEnd,
1522 const Twine &NameStr = "",
1523 Instruction *InsertBefore = 0) {
1525 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1527 template<typename InputIterator>
1528 static ExtractValueInst *Create(Value *Agg,
1529 InputIterator IdxBegin, InputIterator IdxEnd,
1530 const Twine &NameStr,
1531 BasicBlock *InsertAtEnd) {
1532 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1535 /// Constructors - These two creators are convenience methods because one
1536 /// index extractvalue instructions are much more common than those with
1538 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1539 const Twine &NameStr = "",
1540 Instruction *InsertBefore = 0) {
1541 unsigned Idxs[1] = { Idx };
1542 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1544 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1545 const Twine &NameStr,
1546 BasicBlock *InsertAtEnd) {
1547 unsigned Idxs[1] = { Idx };
1548 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1551 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1553 /// getIndexedType - Returns the type of the element that would be extracted
1554 /// with an extractvalue instruction with the specified parameters.
1556 /// Null is returned if the indices are invalid for the specified
1559 template<typename InputIterator>
1560 static const Type *getIndexedType(const Type *Ptr,
1561 InputIterator IdxBegin,
1562 InputIterator IdxEnd) {
1563 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1564 typename std::iterator_traits<InputIterator>::
1565 iterator_category());
1567 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1569 typedef const unsigned* idx_iterator;
1570 inline idx_iterator idx_begin() const { return Indices.begin(); }
1571 inline idx_iterator idx_end() const { return Indices.end(); }
1573 Value *getAggregateOperand() {
1574 return getOperand(0);
1576 const Value *getAggregateOperand() const {
1577 return getOperand(0);
1579 static unsigned getAggregateOperandIndex() {
1580 return 0U; // get index for modifying correct operand
1583 unsigned getNumIndices() const { // Note: always non-negative
1584 return (unsigned)Indices.size();
1587 bool hasIndices() const {
1591 // Methods for support type inquiry through isa, cast, and dyn_cast:
1592 static inline bool classof(const ExtractValueInst *) { return true; }
1593 static inline bool classof(const Instruction *I) {
1594 return I->getOpcode() == Instruction::ExtractValue;
1596 static inline bool classof(const Value *V) {
1597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1601 template<typename InputIterator>
1602 ExtractValueInst::ExtractValueInst(Value *Agg,
1603 InputIterator IdxBegin,
1604 InputIterator IdxEnd,
1605 const Twine &NameStr,
1606 Instruction *InsertBefore)
1607 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1609 ExtractValue, Agg, InsertBefore) {
1610 init(IdxBegin, IdxEnd, NameStr,
1611 typename std::iterator_traits<InputIterator>::iterator_category());
1613 template<typename InputIterator>
1614 ExtractValueInst::ExtractValueInst(Value *Agg,
1615 InputIterator IdxBegin,
1616 InputIterator IdxEnd,
1617 const Twine &NameStr,
1618 BasicBlock *InsertAtEnd)
1619 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1621 ExtractValue, Agg, InsertAtEnd) {
1622 init(IdxBegin, IdxEnd, NameStr,
1623 typename std::iterator_traits<InputIterator>::iterator_category());
1627 //===----------------------------------------------------------------------===//
1628 // InsertValueInst Class
1629 //===----------------------------------------------------------------------===//
1631 /// InsertValueInst - This instruction inserts a struct field of array element
1632 /// value into an aggregate value.
1634 class InsertValueInst : public Instruction {
1635 SmallVector<unsigned, 4> Indices;
1637 void *operator new(size_t, unsigned); // Do not implement
1638 InsertValueInst(const InsertValueInst &IVI);
1639 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1640 const Twine &NameStr);
1641 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1643 template<typename InputIterator>
1644 void init(Value *Agg, Value *Val,
1645 InputIterator IdxBegin, InputIterator IdxEnd,
1646 const Twine &NameStr,
1647 // This argument ensures that we have an iterator we can
1648 // do arithmetic on in constant time
1649 std::random_access_iterator_tag) {
1650 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1652 // There's no fundamental reason why we require at least one index
1653 // (other than weirdness with &*IdxBegin being invalid; see
1654 // getelementptr's init routine for example). But there's no
1655 // present need to support it.
1656 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1658 // This requires that the iterator points to contiguous memory.
1659 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1660 // we have to build an array here
1663 /// Constructors - Create a insertvalue instruction with a base aggregate
1664 /// value, a value to insert, and a list of indices. The first ctor can
1665 /// optionally insert before an existing instruction, the second appends
1666 /// the new instruction to the specified BasicBlock.
1667 template<typename InputIterator>
1668 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1669 InputIterator IdxEnd,
1670 const Twine &NameStr,
1671 Instruction *InsertBefore);
1672 template<typename InputIterator>
1673 inline InsertValueInst(Value *Agg, Value *Val,
1674 InputIterator IdxBegin, InputIterator IdxEnd,
1675 const Twine &NameStr, BasicBlock *InsertAtEnd);
1677 /// Constructors - These two constructors are convenience methods because one
1678 /// and two index insertvalue instructions are so common.
1679 InsertValueInst(Value *Agg, Value *Val,
1680 unsigned Idx, const Twine &NameStr = "",
1681 Instruction *InsertBefore = 0);
1682 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1683 const Twine &NameStr, BasicBlock *InsertAtEnd);
1685 // allocate space for exactly two operands
1686 void *operator new(size_t s) {
1687 return User::operator new(s, 2);
1690 template<typename InputIterator>
1691 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1692 InputIterator IdxEnd,
1693 const Twine &NameStr = "",
1694 Instruction *InsertBefore = 0) {
1695 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1696 NameStr, InsertBefore);
1698 template<typename InputIterator>
1699 static InsertValueInst *Create(Value *Agg, Value *Val,
1700 InputIterator IdxBegin, InputIterator IdxEnd,
1701 const Twine &NameStr,
1702 BasicBlock *InsertAtEnd) {
1703 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1704 NameStr, InsertAtEnd);
1707 /// Constructors - These two creators are convenience methods because one
1708 /// index insertvalue instructions are much more common than those with
1710 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1711 const Twine &NameStr = "",
1712 Instruction *InsertBefore = 0) {
1713 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1715 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1716 const Twine &NameStr,
1717 BasicBlock *InsertAtEnd) {
1718 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1721 virtual InsertValueInst *clone(LLVMContext &Context) const;
1723 /// Transparently provide more efficient getOperand methods.
1724 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1726 typedef const unsigned* idx_iterator;
1727 inline idx_iterator idx_begin() const { return Indices.begin(); }
1728 inline idx_iterator idx_end() const { return Indices.end(); }
1730 Value *getAggregateOperand() {
1731 return getOperand(0);
1733 const Value *getAggregateOperand() const {
1734 return getOperand(0);
1736 static unsigned getAggregateOperandIndex() {
1737 return 0U; // get index for modifying correct operand
1740 Value *getInsertedValueOperand() {
1741 return getOperand(1);
1743 const Value *getInsertedValueOperand() const {
1744 return getOperand(1);
1746 static unsigned getInsertedValueOperandIndex() {
1747 return 1U; // get index for modifying correct operand
1750 unsigned getNumIndices() const { // Note: always non-negative
1751 return (unsigned)Indices.size();
1754 bool hasIndices() const {
1758 // Methods for support type inquiry through isa, cast, and dyn_cast:
1759 static inline bool classof(const InsertValueInst *) { return true; }
1760 static inline bool classof(const Instruction *I) {
1761 return I->getOpcode() == Instruction::InsertValue;
1763 static inline bool classof(const Value *V) {
1764 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1769 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1772 template<typename InputIterator>
1773 InsertValueInst::InsertValueInst(Value *Agg,
1775 InputIterator IdxBegin,
1776 InputIterator IdxEnd,
1777 const Twine &NameStr,
1778 Instruction *InsertBefore)
1779 : Instruction(Agg->getType(), InsertValue,
1780 OperandTraits<InsertValueInst>::op_begin(this),
1782 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1783 typename std::iterator_traits<InputIterator>::iterator_category());
1785 template<typename InputIterator>
1786 InsertValueInst::InsertValueInst(Value *Agg,
1788 InputIterator IdxBegin,
1789 InputIterator IdxEnd,
1790 const Twine &NameStr,
1791 BasicBlock *InsertAtEnd)
1792 : Instruction(Agg->getType(), InsertValue,
1793 OperandTraits<InsertValueInst>::op_begin(this),
1795 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1796 typename std::iterator_traits<InputIterator>::iterator_category());
1799 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1801 //===----------------------------------------------------------------------===//
1803 //===----------------------------------------------------------------------===//
1805 // PHINode - The PHINode class is used to represent the magical mystical PHI
1806 // node, that can not exist in nature, but can be synthesized in a computer
1807 // scientist's overactive imagination.
1809 class PHINode : public Instruction {
1810 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1811 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1812 /// the number actually in use.
1813 unsigned ReservedSpace;
1814 PHINode(const PHINode &PN);
1815 // allocate space for exactly zero operands
1816 void *operator new(size_t s) {
1817 return User::operator new(s, 0);
1819 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1820 Instruction *InsertBefore = 0)
1821 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1826 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1827 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1832 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1833 Instruction *InsertBefore = 0) {
1834 return new PHINode(Ty, NameStr, InsertBefore);
1836 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1837 BasicBlock *InsertAtEnd) {
1838 return new PHINode(Ty, NameStr, InsertAtEnd);
1842 /// reserveOperandSpace - This method can be used to avoid repeated
1843 /// reallocation of PHI operand lists by reserving space for the correct
1844 /// number of operands before adding them. Unlike normal vector reserves,
1845 /// this method can also be used to trim the operand space.
1846 void reserveOperandSpace(unsigned NumValues) {
1847 resizeOperands(NumValues*2);
1850 virtual PHINode *clone(LLVMContext &Context) const;
1852 /// Provide fast operand accessors
1853 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1855 /// getNumIncomingValues - Return the number of incoming edges
1857 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1859 /// getIncomingValue - Return incoming value number x
1861 Value *getIncomingValue(unsigned i) const {
1862 assert(i*2 < getNumOperands() && "Invalid value number!");
1863 return getOperand(i*2);
1865 void setIncomingValue(unsigned i, Value *V) {
1866 assert(i*2 < getNumOperands() && "Invalid value number!");
1869 static unsigned getOperandNumForIncomingValue(unsigned i) {
1872 static unsigned getIncomingValueNumForOperand(unsigned i) {
1873 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1877 /// getIncomingBlock - Return incoming basic block corresponding
1878 /// to value use iterator
1880 template <typename U>
1881 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1882 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1883 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1885 /// getIncomingBlock - Return incoming basic block number x
1887 BasicBlock *getIncomingBlock(unsigned i) const {
1888 return static_cast<BasicBlock*>(getOperand(i*2+1));
1890 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1891 setOperand(i*2+1, BB);
1893 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1896 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1897 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1901 /// addIncoming - Add an incoming value to the end of the PHI list
1903 void addIncoming(Value *V, BasicBlock *BB) {
1904 assert(V && "PHI node got a null value!");
1905 assert(BB && "PHI node got a null basic block!");
1906 assert(getType() == V->getType() &&
1907 "All operands to PHI node must be the same type as the PHI node!");
1908 unsigned OpNo = NumOperands;
1909 if (OpNo+2 > ReservedSpace)
1910 resizeOperands(0); // Get more space!
1911 // Initialize some new operands.
1912 NumOperands = OpNo+2;
1913 OperandList[OpNo] = V;
1914 OperandList[OpNo+1] = BB;
1917 /// removeIncomingValue - Remove an incoming value. This is useful if a
1918 /// predecessor basic block is deleted. The value removed is returned.
1920 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1921 /// is true), the PHI node is destroyed and any uses of it are replaced with
1922 /// dummy values. The only time there should be zero incoming values to a PHI
1923 /// node is when the block is dead, so this strategy is sound.
1925 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1927 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1928 int Idx = getBasicBlockIndex(BB);
1929 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1930 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1933 /// getBasicBlockIndex - Return the first index of the specified basic
1934 /// block in the value list for this PHI. Returns -1 if no instance.
1936 int getBasicBlockIndex(const BasicBlock *BB) const {
1937 Use *OL = OperandList;
1938 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1939 if (OL[i+1].get() == BB) return i/2;
1943 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1944 return getIncomingValue(getBasicBlockIndex(BB));
1947 /// hasConstantValue - If the specified PHI node always merges together the
1948 /// same value, return the value, otherwise return null.
1950 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1952 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1953 static inline bool classof(const PHINode *) { return true; }
1954 static inline bool classof(const Instruction *I) {
1955 return I->getOpcode() == Instruction::PHI;
1957 static inline bool classof(const Value *V) {
1958 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1961 void resizeOperands(unsigned NumOperands);
1965 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1968 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1971 //===----------------------------------------------------------------------===//
1973 //===----------------------------------------------------------------------===//
1975 //===---------------------------------------------------------------------------
1976 /// ReturnInst - Return a value (possibly void), from a function. Execution
1977 /// does not continue in this function any longer.
1979 class ReturnInst : public TerminatorInst {
1980 ReturnInst(const ReturnInst &RI);
1983 // ReturnInst constructors:
1984 // ReturnInst() - 'ret void' instruction
1985 // ReturnInst( null) - 'ret void' instruction
1986 // ReturnInst(Value* X) - 'ret X' instruction
1987 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1988 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1989 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1990 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1992 // NOTE: If the Value* passed is of type void then the constructor behaves as
1993 // if it was passed NULL.
1994 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1995 Instruction *InsertBefore = 0);
1996 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1997 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1999 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2000 Instruction *InsertBefore = 0) {
2001 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2003 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2004 BasicBlock *InsertAtEnd) {
2005 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2007 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2008 return new(0) ReturnInst(C, InsertAtEnd);
2010 virtual ~ReturnInst();
2012 virtual ReturnInst *clone(LLVMContext &Context) const;
2014 /// Provide fast operand accessors
2015 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2017 /// Convenience accessor
2018 Value *getReturnValue(unsigned n = 0) const {
2019 return n < getNumOperands()
2024 unsigned getNumSuccessors() const { return 0; }
2026 // Methods for support type inquiry through isa, cast, and dyn_cast:
2027 static inline bool classof(const ReturnInst *) { return true; }
2028 static inline bool classof(const Instruction *I) {
2029 return (I->getOpcode() == Instruction::Ret);
2031 static inline bool classof(const Value *V) {
2032 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2035 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2036 virtual unsigned getNumSuccessorsV() const;
2037 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2041 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2044 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2046 //===----------------------------------------------------------------------===//
2048 //===----------------------------------------------------------------------===//
2050 //===---------------------------------------------------------------------------
2051 /// BranchInst - Conditional or Unconditional Branch instruction.
2053 class BranchInst : public TerminatorInst {
2054 /// Ops list - Branches are strange. The operands are ordered:
2055 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2056 /// they don't have to check for cond/uncond branchness. These are mostly
2057 /// accessed relative from op_end().
2058 BranchInst(const BranchInst &BI);
2060 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2061 // BranchInst(BB *B) - 'br B'
2062 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2063 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2064 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2065 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2066 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2067 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2068 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2069 Instruction *InsertBefore = 0);
2070 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2071 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2072 BasicBlock *InsertAtEnd);
2074 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2075 return new(1, true) BranchInst(IfTrue, InsertBefore);
2077 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2078 Value *Cond, Instruction *InsertBefore = 0) {
2079 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2081 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2082 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2084 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2085 Value *Cond, BasicBlock *InsertAtEnd) {
2086 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2091 /// Transparently provide more efficient getOperand methods.
2092 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2094 virtual BranchInst *clone(LLVMContext &Context) const;
2096 bool isUnconditional() const { return getNumOperands() == 1; }
2097 bool isConditional() const { return getNumOperands() == 3; }
2099 Value *getCondition() const {
2100 assert(isConditional() && "Cannot get condition of an uncond branch!");
2104 void setCondition(Value *V) {
2105 assert(isConditional() && "Cannot set condition of unconditional branch!");
2109 // setUnconditionalDest - Change the current branch to an unconditional branch
2110 // targeting the specified block.
2111 // FIXME: Eliminate this ugly method.
2112 void setUnconditionalDest(BasicBlock *Dest) {
2114 if (isConditional()) { // Convert this to an uncond branch.
2118 OperandList = op_begin();
2122 unsigned getNumSuccessors() const { return 1+isConditional(); }
2124 BasicBlock *getSuccessor(unsigned i) const {
2125 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2126 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2129 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2130 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2131 *(&Op<-1>() - idx) = NewSucc;
2134 // Methods for support type inquiry through isa, cast, and dyn_cast:
2135 static inline bool classof(const BranchInst *) { return true; }
2136 static inline bool classof(const Instruction *I) {
2137 return (I->getOpcode() == Instruction::Br);
2139 static inline bool classof(const Value *V) {
2140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2143 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2144 virtual unsigned getNumSuccessorsV() const;
2145 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2149 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2151 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2153 //===----------------------------------------------------------------------===//
2155 //===----------------------------------------------------------------------===//
2157 //===---------------------------------------------------------------------------
2158 /// SwitchInst - Multiway switch
2160 class SwitchInst : public TerminatorInst {
2161 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2162 unsigned ReservedSpace;
2163 // Operand[0] = Value to switch on
2164 // Operand[1] = Default basic block destination
2165 // Operand[2n ] = Value to match
2166 // Operand[2n+1] = BasicBlock to go to on match
2167 SwitchInst(const SwitchInst &RI);
2168 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2169 void resizeOperands(unsigned No);
2170 // allocate space for exactly zero operands
2171 void *operator new(size_t s) {
2172 return User::operator new(s, 0);
2174 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2175 /// switch on and a default destination. The number of additional cases can
2176 /// be specified here to make memory allocation more efficient. This
2177 /// constructor can also autoinsert before another instruction.
2178 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2179 Instruction *InsertBefore = 0);
2181 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2182 /// switch on and a default destination. The number of additional cases can
2183 /// be specified here to make memory allocation more efficient. This
2184 /// constructor also autoinserts at the end of the specified BasicBlock.
2185 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2186 BasicBlock *InsertAtEnd);
2188 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2189 unsigned NumCases, Instruction *InsertBefore = 0) {
2190 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2192 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2193 unsigned NumCases, BasicBlock *InsertAtEnd) {
2194 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2198 /// Provide fast operand accessors
2199 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2201 // Accessor Methods for Switch stmt
2202 Value *getCondition() const { return getOperand(0); }
2203 void setCondition(Value *V) { setOperand(0, V); }
2205 BasicBlock *getDefaultDest() const {
2206 return cast<BasicBlock>(getOperand(1));
2209 /// getNumCases - return the number of 'cases' in this switch instruction.
2210 /// Note that case #0 is always the default case.
2211 unsigned getNumCases() const {
2212 return getNumOperands()/2;
2215 /// getCaseValue - Return the specified case value. Note that case #0, the
2216 /// default destination, does not have a case value.
2217 ConstantInt *getCaseValue(unsigned i) {
2218 assert(i && i < getNumCases() && "Illegal case value to get!");
2219 return getSuccessorValue(i);
2222 /// getCaseValue - Return the specified case value. Note that case #0, the
2223 /// default destination, does not have a case value.
2224 const ConstantInt *getCaseValue(unsigned i) const {
2225 assert(i && i < getNumCases() && "Illegal case value to get!");
2226 return getSuccessorValue(i);
2229 /// findCaseValue - Search all of the case values for the specified constant.
2230 /// If it is explicitly handled, return the case number of it, otherwise
2231 /// return 0 to indicate that it is handled by the default handler.
2232 unsigned findCaseValue(const ConstantInt *C) const {
2233 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2234 if (getCaseValue(i) == C)
2239 /// findCaseDest - Finds the unique case value for a given successor. Returns
2240 /// null if the successor is not found, not unique, or is the default case.
2241 ConstantInt *findCaseDest(BasicBlock *BB) {
2242 if (BB == getDefaultDest()) return NULL;
2244 ConstantInt *CI = NULL;
2245 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2246 if (getSuccessor(i) == BB) {
2247 if (CI) return NULL; // Multiple cases lead to BB.
2248 else CI = getCaseValue(i);
2254 /// addCase - Add an entry to the switch instruction...
2256 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2258 /// removeCase - This method removes the specified successor from the switch
2259 /// instruction. Note that this cannot be used to remove the default
2260 /// destination (successor #0).
2262 void removeCase(unsigned idx);
2264 virtual SwitchInst *clone(LLVMContext &Context) const;
2266 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2267 BasicBlock *getSuccessor(unsigned idx) const {
2268 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2269 return cast<BasicBlock>(getOperand(idx*2+1));
2271 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2272 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2273 setOperand(idx*2+1, NewSucc);
2276 // getSuccessorValue - Return the value associated with the specified
2278 ConstantInt *getSuccessorValue(unsigned idx) const {
2279 assert(idx < getNumSuccessors() && "Successor # out of range!");
2280 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2283 // Methods for support type inquiry through isa, cast, and dyn_cast:
2284 static inline bool classof(const SwitchInst *) { return true; }
2285 static inline bool classof(const Instruction *I) {
2286 return I->getOpcode() == Instruction::Switch;
2288 static inline bool classof(const Value *V) {
2289 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2292 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2293 virtual unsigned getNumSuccessorsV() const;
2294 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2298 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2301 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2304 //===----------------------------------------------------------------------===//
2306 //===----------------------------------------------------------------------===//
2308 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2309 /// calling convention of the call.
2311 class InvokeInst : public TerminatorInst {
2312 AttrListPtr AttributeList;
2313 InvokeInst(const InvokeInst &BI);
2314 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2315 Value* const *Args, unsigned NumArgs);
2317 template<typename InputIterator>
2318 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2319 InputIterator ArgBegin, InputIterator ArgEnd,
2320 const Twine &NameStr,
2321 // This argument ensures that we have an iterator we can
2322 // do arithmetic on in constant time
2323 std::random_access_iterator_tag) {
2324 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2326 // This requires that the iterator points to contiguous memory.
2327 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2331 /// Construct an InvokeInst given a range of arguments.
2332 /// InputIterator must be a random-access iterator pointing to
2333 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2334 /// made for random-accessness but not for contiguous storage as
2335 /// that would incur runtime overhead.
2337 /// @brief Construct an InvokeInst from a range of arguments
2338 template<typename InputIterator>
2339 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2340 InputIterator ArgBegin, InputIterator ArgEnd,
2342 const Twine &NameStr, Instruction *InsertBefore);
2344 /// Construct an InvokeInst given a range of arguments.
2345 /// InputIterator must be a random-access iterator pointing to
2346 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2347 /// made for random-accessness but not for contiguous storage as
2348 /// that would incur runtime overhead.
2350 /// @brief Construct an InvokeInst from a range of arguments
2351 template<typename InputIterator>
2352 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2353 InputIterator ArgBegin, InputIterator ArgEnd,
2355 const Twine &NameStr, BasicBlock *InsertAtEnd);
2357 template<typename InputIterator>
2358 static InvokeInst *Create(Value *Func,
2359 BasicBlock *IfNormal, BasicBlock *IfException,
2360 InputIterator ArgBegin, InputIterator ArgEnd,
2361 const Twine &NameStr = "",
2362 Instruction *InsertBefore = 0) {
2363 unsigned Values(ArgEnd - ArgBegin + 3);
2364 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2365 Values, NameStr, InsertBefore);
2367 template<typename InputIterator>
2368 static InvokeInst *Create(Value *Func,
2369 BasicBlock *IfNormal, BasicBlock *IfException,
2370 InputIterator ArgBegin, InputIterator ArgEnd,
2371 const Twine &NameStr,
2372 BasicBlock *InsertAtEnd) {
2373 unsigned Values(ArgEnd - ArgBegin + 3);
2374 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2375 Values, NameStr, InsertAtEnd);
2378 virtual InvokeInst *clone(LLVMContext &Context) const;
2380 /// Provide fast operand accessors
2381 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2383 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2385 unsigned getCallingConv() const { return SubclassData; }
2386 void setCallingConv(unsigned CC) {
2390 /// getAttributes - Return the parameter attributes for this invoke.
2392 const AttrListPtr &getAttributes() const { return AttributeList; }
2394 /// setAttributes - Set the parameter attributes for this invoke.
2396 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2398 /// addAttribute - adds the attribute to the list of attributes.
2399 void addAttribute(unsigned i, Attributes attr);
2401 /// removeAttribute - removes the attribute from the list of attributes.
2402 void removeAttribute(unsigned i, Attributes attr);
2404 /// @brief Determine whether the call or the callee has the given attribute.
2405 bool paramHasAttr(unsigned i, Attributes attr) const;
2407 /// @brief Extract the alignment for a call or parameter (0=unknown).
2408 unsigned getParamAlignment(unsigned i) const {
2409 return AttributeList.getParamAlignment(i);
2412 /// @brief Determine if the call does not access memory.
2413 bool doesNotAccessMemory() const {
2414 return paramHasAttr(~0, Attribute::ReadNone);
2416 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2417 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2418 else removeAttribute(~0, Attribute::ReadNone);
2421 /// @brief Determine if the call does not access or only reads memory.
2422 bool onlyReadsMemory() const {
2423 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2425 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2426 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2427 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2430 /// @brief Determine if the call cannot return.
2431 bool doesNotReturn() const {
2432 return paramHasAttr(~0, Attribute::NoReturn);
2434 void setDoesNotReturn(bool DoesNotReturn = true) {
2435 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2436 else removeAttribute(~0, Attribute::NoReturn);
2439 /// @brief Determine if the call cannot unwind.
2440 bool doesNotThrow() const {
2441 return paramHasAttr(~0, Attribute::NoUnwind);
2443 void setDoesNotThrow(bool DoesNotThrow = true) {
2444 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2445 else removeAttribute(~0, Attribute::NoUnwind);
2448 /// @brief Determine if the call returns a structure through first
2449 /// pointer argument.
2450 bool hasStructRetAttr() const {
2451 // Be friendly and also check the callee.
2452 return paramHasAttr(1, Attribute::StructRet);
2455 /// @brief Determine if any call argument is an aggregate passed by value.
2456 bool hasByValArgument() const {
2457 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2460 /// getCalledFunction - Return the function called, or null if this is an
2461 /// indirect function invocation.
2463 Function *getCalledFunction() const {
2464 return dyn_cast<Function>(getOperand(0));
2467 /// getCalledValue - Get a pointer to the function that is invoked by this
2469 const Value *getCalledValue() const { return getOperand(0); }
2470 Value *getCalledValue() { return getOperand(0); }
2472 // get*Dest - Return the destination basic blocks...
2473 BasicBlock *getNormalDest() const {
2474 return cast<BasicBlock>(getOperand(1));
2476 BasicBlock *getUnwindDest() const {
2477 return cast<BasicBlock>(getOperand(2));
2479 void setNormalDest(BasicBlock *B) {
2483 void setUnwindDest(BasicBlock *B) {
2487 BasicBlock *getSuccessor(unsigned i) const {
2488 assert(i < 2 && "Successor # out of range for invoke!");
2489 return i == 0 ? getNormalDest() : getUnwindDest();
2492 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2493 assert(idx < 2 && "Successor # out of range for invoke!");
2494 setOperand(idx+1, NewSucc);
2497 unsigned getNumSuccessors() const { return 2; }
2499 // Methods for support type inquiry through isa, cast, and dyn_cast:
2500 static inline bool classof(const InvokeInst *) { return true; }
2501 static inline bool classof(const Instruction *I) {
2502 return (I->getOpcode() == Instruction::Invoke);
2504 static inline bool classof(const Value *V) {
2505 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2508 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2509 virtual unsigned getNumSuccessorsV() const;
2510 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2514 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2517 template<typename InputIterator>
2518 InvokeInst::InvokeInst(Value *Func,
2519 BasicBlock *IfNormal, BasicBlock *IfException,
2520 InputIterator ArgBegin, InputIterator ArgEnd,
2522 const Twine &NameStr, Instruction *InsertBefore)
2523 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2524 ->getElementType())->getReturnType(),
2525 Instruction::Invoke,
2526 OperandTraits<InvokeInst>::op_end(this) - Values,
2527 Values, InsertBefore) {
2528 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2529 typename std::iterator_traits<InputIterator>::iterator_category());
2531 template<typename InputIterator>
2532 InvokeInst::InvokeInst(Value *Func,
2533 BasicBlock *IfNormal, BasicBlock *IfException,
2534 InputIterator ArgBegin, InputIterator ArgEnd,
2536 const Twine &NameStr, BasicBlock *InsertAtEnd)
2537 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2538 ->getElementType())->getReturnType(),
2539 Instruction::Invoke,
2540 OperandTraits<InvokeInst>::op_end(this) - Values,
2541 Values, InsertAtEnd) {
2542 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2543 typename std::iterator_traits<InputIterator>::iterator_category());
2546 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2548 //===----------------------------------------------------------------------===//
2550 //===----------------------------------------------------------------------===//
2552 //===---------------------------------------------------------------------------
2553 /// UnwindInst - Immediately exit the current function, unwinding the stack
2554 /// until an invoke instruction is found.
2556 class UnwindInst : public TerminatorInst {
2557 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2559 // allocate space for exactly zero operands
2560 void *operator new(size_t s) {
2561 return User::operator new(s, 0);
2563 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2564 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2566 virtual UnwindInst *clone(LLVMContext &Context) const;
2568 unsigned getNumSuccessors() const { return 0; }
2570 // Methods for support type inquiry through isa, cast, and dyn_cast:
2571 static inline bool classof(const UnwindInst *) { return true; }
2572 static inline bool classof(const Instruction *I) {
2573 return I->getOpcode() == Instruction::Unwind;
2575 static inline bool classof(const Value *V) {
2576 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2579 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2580 virtual unsigned getNumSuccessorsV() const;
2581 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2584 //===----------------------------------------------------------------------===//
2585 // UnreachableInst Class
2586 //===----------------------------------------------------------------------===//
2588 //===---------------------------------------------------------------------------
2589 /// UnreachableInst - This function has undefined behavior. In particular, the
2590 /// presence of this instruction indicates some higher level knowledge that the
2591 /// end of the block cannot be reached.
2593 class UnreachableInst : public TerminatorInst {
2594 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2596 // allocate space for exactly zero operands
2597 void *operator new(size_t s) {
2598 return User::operator new(s, 0);
2600 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2601 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2603 virtual UnreachableInst *clone(LLVMContext &Context) const;
2605 unsigned getNumSuccessors() const { return 0; }
2607 // Methods for support type inquiry through isa, cast, and dyn_cast:
2608 static inline bool classof(const UnreachableInst *) { return true; }
2609 static inline bool classof(const Instruction *I) {
2610 return I->getOpcode() == Instruction::Unreachable;
2612 static inline bool classof(const Value *V) {
2613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2616 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2617 virtual unsigned getNumSuccessorsV() const;
2618 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2621 //===----------------------------------------------------------------------===//
2623 //===----------------------------------------------------------------------===//
2625 /// @brief This class represents a truncation of integer types.
2626 class TruncInst : public CastInst {
2628 /// @brief Constructor with insert-before-instruction semantics
2630 Value *S, ///< The value to be truncated
2631 const Type *Ty, ///< The (smaller) type to truncate to
2632 const Twine &NameStr = "", ///< A name for the new instruction
2633 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2636 /// @brief Constructor with insert-at-end-of-block semantics
2638 Value *S, ///< The value to be truncated
2639 const Type *Ty, ///< The (smaller) type to truncate to
2640 const Twine &NameStr, ///< A name for the new instruction
2641 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2644 /// @brief Clone an identical TruncInst
2645 virtual TruncInst *clone(LLVMContext &Context) const;
2647 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2648 static inline bool classof(const TruncInst *) { return true; }
2649 static inline bool classof(const Instruction *I) {
2650 return I->getOpcode() == Trunc;
2652 static inline bool classof(const Value *V) {
2653 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2657 //===----------------------------------------------------------------------===//
2659 //===----------------------------------------------------------------------===//
2661 /// @brief This class represents zero extension of integer types.
2662 class ZExtInst : public CastInst {
2664 /// @brief Constructor with insert-before-instruction semantics
2666 Value *S, ///< The value to be zero extended
2667 const Type *Ty, ///< The type to zero extend to
2668 const Twine &NameStr = "", ///< A name for the new instruction
2669 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2672 /// @brief Constructor with insert-at-end semantics.
2674 Value *S, ///< The value to be zero extended
2675 const Type *Ty, ///< The type to zero extend to
2676 const Twine &NameStr, ///< A name for the new instruction
2677 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2680 /// @brief Clone an identical ZExtInst
2681 virtual ZExtInst *clone(LLVMContext &Context) const;
2683 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2684 static inline bool classof(const ZExtInst *) { return true; }
2685 static inline bool classof(const Instruction *I) {
2686 return I->getOpcode() == ZExt;
2688 static inline bool classof(const Value *V) {
2689 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2693 //===----------------------------------------------------------------------===//
2695 //===----------------------------------------------------------------------===//
2697 /// @brief This class represents a sign extension of integer types.
2698 class SExtInst : public CastInst {
2700 /// @brief Constructor with insert-before-instruction semantics
2702 Value *S, ///< The value to be sign extended
2703 const Type *Ty, ///< The type to sign extend to
2704 const Twine &NameStr = "", ///< A name for the new instruction
2705 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2708 /// @brief Constructor with insert-at-end-of-block semantics
2710 Value *S, ///< The value to be sign extended
2711 const Type *Ty, ///< The type to sign extend to
2712 const Twine &NameStr, ///< A name for the new instruction
2713 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2716 /// @brief Clone an identical SExtInst
2717 virtual SExtInst *clone(LLVMContext &Context) const;
2719 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2720 static inline bool classof(const SExtInst *) { return true; }
2721 static inline bool classof(const Instruction *I) {
2722 return I->getOpcode() == SExt;
2724 static inline bool classof(const Value *V) {
2725 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2729 //===----------------------------------------------------------------------===//
2730 // FPTruncInst Class
2731 //===----------------------------------------------------------------------===//
2733 /// @brief This class represents a truncation of floating point types.
2734 class FPTruncInst : public CastInst {
2736 /// @brief Constructor with insert-before-instruction semantics
2738 Value *S, ///< The value to be truncated
2739 const Type *Ty, ///< The type to truncate to
2740 const Twine &NameStr = "", ///< A name for the new instruction
2741 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2744 /// @brief Constructor with insert-before-instruction semantics
2746 Value *S, ///< The value to be truncated
2747 const Type *Ty, ///< The type to truncate to
2748 const Twine &NameStr, ///< A name for the new instruction
2749 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2752 /// @brief Clone an identical FPTruncInst
2753 virtual FPTruncInst *clone(LLVMContext &Context) const;
2755 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2756 static inline bool classof(const FPTruncInst *) { return true; }
2757 static inline bool classof(const Instruction *I) {
2758 return I->getOpcode() == FPTrunc;
2760 static inline bool classof(const Value *V) {
2761 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2765 //===----------------------------------------------------------------------===//
2767 //===----------------------------------------------------------------------===//
2769 /// @brief This class represents an extension of floating point types.
2770 class FPExtInst : public CastInst {
2772 /// @brief Constructor with insert-before-instruction semantics
2774 Value *S, ///< The value to be extended
2775 const Type *Ty, ///< The type to extend to
2776 const Twine &NameStr = "", ///< A name for the new instruction
2777 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2780 /// @brief Constructor with insert-at-end-of-block semantics
2782 Value *S, ///< The value to be extended
2783 const Type *Ty, ///< The type to extend to
2784 const Twine &NameStr, ///< A name for the new instruction
2785 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2788 /// @brief Clone an identical FPExtInst
2789 virtual FPExtInst *clone(LLVMContext &Context) const;
2791 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2792 static inline bool classof(const FPExtInst *) { return true; }
2793 static inline bool classof(const Instruction *I) {
2794 return I->getOpcode() == FPExt;
2796 static inline bool classof(const Value *V) {
2797 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2801 //===----------------------------------------------------------------------===//
2803 //===----------------------------------------------------------------------===//
2805 /// @brief This class represents a cast unsigned integer to floating point.
2806 class UIToFPInst : public CastInst {
2808 /// @brief Constructor with insert-before-instruction semantics
2810 Value *S, ///< The value to be converted
2811 const Type *Ty, ///< The type to convert to
2812 const Twine &NameStr = "", ///< A name for the new instruction
2813 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2816 /// @brief Constructor with insert-at-end-of-block semantics
2818 Value *S, ///< The value to be converted
2819 const Type *Ty, ///< The type to convert to
2820 const Twine &NameStr, ///< A name for the new instruction
2821 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2824 /// @brief Clone an identical UIToFPInst
2825 virtual UIToFPInst *clone(LLVMContext &Context) const;
2827 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2828 static inline bool classof(const UIToFPInst *) { return true; }
2829 static inline bool classof(const Instruction *I) {
2830 return I->getOpcode() == UIToFP;
2832 static inline bool classof(const Value *V) {
2833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2837 //===----------------------------------------------------------------------===//
2839 //===----------------------------------------------------------------------===//
2841 /// @brief This class represents a cast from signed integer to floating point.
2842 class SIToFPInst : public CastInst {
2844 /// @brief Constructor with insert-before-instruction semantics
2846 Value *S, ///< The value to be converted
2847 const Type *Ty, ///< The type to convert to
2848 const Twine &NameStr = "", ///< A name for the new instruction
2849 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2852 /// @brief Constructor with insert-at-end-of-block semantics
2854 Value *S, ///< The value to be converted
2855 const Type *Ty, ///< The type to convert to
2856 const Twine &NameStr, ///< A name for the new instruction
2857 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2860 /// @brief Clone an identical SIToFPInst
2861 virtual SIToFPInst *clone(LLVMContext &Context) const;
2863 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2864 static inline bool classof(const SIToFPInst *) { return true; }
2865 static inline bool classof(const Instruction *I) {
2866 return I->getOpcode() == SIToFP;
2868 static inline bool classof(const Value *V) {
2869 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2873 //===----------------------------------------------------------------------===//
2875 //===----------------------------------------------------------------------===//
2877 /// @brief This class represents a cast from floating point to unsigned integer
2878 class FPToUIInst : public CastInst {
2880 /// @brief Constructor with insert-before-instruction semantics
2882 Value *S, ///< The value to be converted
2883 const Type *Ty, ///< The type to convert to
2884 const Twine &NameStr = "", ///< A name for the new instruction
2885 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2888 /// @brief Constructor with insert-at-end-of-block semantics
2890 Value *S, ///< The value to be converted
2891 const Type *Ty, ///< The type to convert to
2892 const Twine &NameStr, ///< A name for the new instruction
2893 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2896 /// @brief Clone an identical FPToUIInst
2897 virtual FPToUIInst *clone(LLVMContext &Context) const;
2899 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2900 static inline bool classof(const FPToUIInst *) { return true; }
2901 static inline bool classof(const Instruction *I) {
2902 return I->getOpcode() == FPToUI;
2904 static inline bool classof(const Value *V) {
2905 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2909 //===----------------------------------------------------------------------===//
2911 //===----------------------------------------------------------------------===//
2913 /// @brief This class represents a cast from floating point to signed integer.
2914 class FPToSIInst : public CastInst {
2916 /// @brief Constructor with insert-before-instruction semantics
2918 Value *S, ///< The value to be converted
2919 const Type *Ty, ///< The type to convert to
2920 const Twine &NameStr = "", ///< A name for the new instruction
2921 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2924 /// @brief Constructor with insert-at-end-of-block semantics
2926 Value *S, ///< The value to be converted
2927 const Type *Ty, ///< The type to convert to
2928 const Twine &NameStr, ///< A name for the new instruction
2929 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2932 /// @brief Clone an identical FPToSIInst
2933 virtual FPToSIInst *clone(LLVMContext &Context) const;
2935 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2936 static inline bool classof(const FPToSIInst *) { return true; }
2937 static inline bool classof(const Instruction *I) {
2938 return I->getOpcode() == FPToSI;
2940 static inline bool classof(const Value *V) {
2941 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2945 //===----------------------------------------------------------------------===//
2946 // IntToPtrInst Class
2947 //===----------------------------------------------------------------------===//
2949 /// @brief This class represents a cast from an integer to a pointer.
2950 class IntToPtrInst : public CastInst {
2952 /// @brief Constructor with insert-before-instruction semantics
2954 Value *S, ///< The value to be converted
2955 const Type *Ty, ///< The type to convert to
2956 const Twine &NameStr = "", ///< A name for the new instruction
2957 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2960 /// @brief Constructor with insert-at-end-of-block semantics
2962 Value *S, ///< The value to be converted
2963 const Type *Ty, ///< The type to convert to
2964 const Twine &NameStr, ///< A name for the new instruction
2965 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2968 /// @brief Clone an identical IntToPtrInst
2969 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2971 // Methods for support type inquiry through isa, cast, and dyn_cast:
2972 static inline bool classof(const IntToPtrInst *) { return true; }
2973 static inline bool classof(const Instruction *I) {
2974 return I->getOpcode() == IntToPtr;
2976 static inline bool classof(const Value *V) {
2977 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2981 //===----------------------------------------------------------------------===//
2982 // PtrToIntInst Class
2983 //===----------------------------------------------------------------------===//
2985 /// @brief This class represents a cast from a pointer to an integer
2986 class PtrToIntInst : public CastInst {
2988 /// @brief Constructor with insert-before-instruction semantics
2990 Value *S, ///< The value to be converted
2991 const Type *Ty, ///< The type to convert to
2992 const Twine &NameStr = "", ///< A name for the new instruction
2993 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2996 /// @brief Constructor with insert-at-end-of-block semantics
2998 Value *S, ///< The value to be converted
2999 const Type *Ty, ///< The type to convert to
3000 const Twine &NameStr, ///< A name for the new instruction
3001 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3004 /// @brief Clone an identical PtrToIntInst
3005 virtual PtrToIntInst *clone(LLVMContext &Context) const;
3007 // Methods for support type inquiry through isa, cast, and dyn_cast:
3008 static inline bool classof(const PtrToIntInst *) { return true; }
3009 static inline bool classof(const Instruction *I) {
3010 return I->getOpcode() == PtrToInt;
3012 static inline bool classof(const Value *V) {
3013 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3017 //===----------------------------------------------------------------------===//
3018 // BitCastInst Class
3019 //===----------------------------------------------------------------------===//
3021 /// @brief This class represents a no-op cast from one type to another.
3022 class BitCastInst : public CastInst {
3024 /// @brief Constructor with insert-before-instruction semantics
3026 Value *S, ///< The value to be casted
3027 const Type *Ty, ///< The type to casted to
3028 const Twine &NameStr = "", ///< A name for the new instruction
3029 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3032 /// @brief Constructor with insert-at-end-of-block semantics
3034 Value *S, ///< The value to be casted
3035 const Type *Ty, ///< The type to casted to
3036 const Twine &NameStr, ///< A name for the new instruction
3037 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3040 /// @brief Clone an identical BitCastInst
3041 virtual BitCastInst *clone(LLVMContext &Context) const;
3043 // Methods for support type inquiry through isa, cast, and dyn_cast:
3044 static inline bool classof(const BitCastInst *) { return true; }
3045 static inline bool classof(const Instruction *I) {
3046 return I->getOpcode() == BitCast;
3048 static inline bool classof(const Value *V) {
3049 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3053 } // End llvm namespace