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 // Methods for support type inquiry through isa, cast, and dyn_cast:
281 static inline bool classof(const LoadInst *) { return true; }
282 static inline bool classof(const Instruction *I) {
283 return I->getOpcode() == Instruction::Load;
285 static inline bool classof(const Value *V) {
286 return isa<Instruction>(V) && classof(cast<Instruction>(V));
291 //===----------------------------------------------------------------------===//
293 //===----------------------------------------------------------------------===//
295 /// StoreInst - an instruction for storing to memory
297 class StoreInst : public Instruction {
298 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
301 // allocate space for exactly two operands
302 void *operator new(size_t s) {
303 return User::operator new(s, 2);
305 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
306 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
307 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
308 Instruction *InsertBefore = 0);
309 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
310 unsigned Align, Instruction *InsertBefore = 0);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
312 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
313 unsigned Align, BasicBlock *InsertAtEnd);
316 /// isVolatile - Return true if this is a load from a volatile memory
319 bool isVolatile() const { return SubclassData & 1; }
321 /// setVolatile - Specify whether this is a volatile load or not.
323 void setVolatile(bool V) {
324 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
327 /// Transparently provide more efficient getOperand methods.
328 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
330 /// getAlignment - Return the alignment of the access that is being performed
332 unsigned getAlignment() const {
333 return (1 << (SubclassData>>1)) >> 1;
336 void setAlignment(unsigned Align);
338 virtual StoreInst *clone(LLVMContext &Context) const;
340 Value *getPointerOperand() { return getOperand(1); }
341 const Value *getPointerOperand() const { return getOperand(1); }
342 static unsigned getPointerOperandIndex() { return 1U; }
344 // Methods for support type inquiry through isa, cast, and dyn_cast:
345 static inline bool classof(const StoreInst *) { return true; }
346 static inline bool classof(const Instruction *I) {
347 return I->getOpcode() == Instruction::Store;
349 static inline bool classof(const Value *V) {
350 return isa<Instruction>(V) && classof(cast<Instruction>(V));
355 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
358 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
360 //===----------------------------------------------------------------------===//
361 // GetElementPtrInst Class
362 //===----------------------------------------------------------------------===//
364 // checkType - Simple wrapper function to give a better assertion failure
365 // message on bad indexes for a gep instruction.
367 static inline const Type *checkType(const Type *Ty) {
368 assert(Ty && "Invalid GetElementPtrInst indices for type!");
372 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
373 /// access elements of arrays and structs
375 class GetElementPtrInst : public Instruction {
376 GetElementPtrInst(const GetElementPtrInst &GEPI);
377 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
378 const Twine &NameStr);
379 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
381 template<typename InputIterator>
382 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
383 const Twine &NameStr,
384 // This argument ensures that we have an iterator we can
385 // do arithmetic on in constant time
386 std::random_access_iterator_tag) {
387 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
390 // This requires that the iterator points to contiguous memory.
391 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
392 // we have to build an array here
395 init(Ptr, 0, NumIdx, NameStr);
399 /// getIndexedType - Returns the type of the element that would be loaded with
400 /// a load instruction with the specified parameters.
402 /// Null is returned if the indices are invalid for the specified
405 template<typename InputIterator>
406 static const Type *getIndexedType(const Type *Ptr,
407 InputIterator IdxBegin,
408 InputIterator IdxEnd,
409 // This argument ensures that we
410 // have an iterator we can do
411 // arithmetic on in constant time
412 std::random_access_iterator_tag) {
413 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
416 // This requires that the iterator points to contiguous memory.
417 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
419 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
422 /// Constructors - Create a getelementptr instruction with a base pointer an
423 /// list of indices. The first ctor can optionally insert before an existing
424 /// instruction, the second appends the new instruction to the specified
426 template<typename InputIterator>
427 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
428 InputIterator IdxEnd,
430 const Twine &NameStr,
431 Instruction *InsertBefore);
432 template<typename InputIterator>
433 inline GetElementPtrInst(Value *Ptr,
434 InputIterator IdxBegin, InputIterator IdxEnd,
436 const Twine &NameStr, BasicBlock *InsertAtEnd);
438 /// Constructors - These two constructors are convenience methods because one
439 /// and two index getelementptr instructions are so common.
440 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
441 Instruction *InsertBefore = 0);
442 GetElementPtrInst(Value *Ptr, Value *Idx,
443 const Twine &NameStr, BasicBlock *InsertAtEnd);
445 template<typename InputIterator>
446 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
447 InputIterator IdxEnd,
448 const Twine &NameStr = "",
449 Instruction *InsertBefore = 0) {
450 typename std::iterator_traits<InputIterator>::difference_type Values =
451 1 + std::distance(IdxBegin, IdxEnd);
453 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
455 template<typename InputIterator>
456 static GetElementPtrInst *Create(Value *Ptr,
457 InputIterator IdxBegin, InputIterator IdxEnd,
458 const Twine &NameStr,
459 BasicBlock *InsertAtEnd) {
460 typename std::iterator_traits<InputIterator>::difference_type Values =
461 1 + std::distance(IdxBegin, IdxEnd);
463 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
466 /// Constructors - These two creators are convenience methods because one
467 /// index getelementptr instructions are so common.
468 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
469 const Twine &NameStr = "",
470 Instruction *InsertBefore = 0) {
471 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
473 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
474 const Twine &NameStr,
475 BasicBlock *InsertAtEnd) {
476 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
479 /// Create an "inbounds" getelementptr. See the documentation for the
480 /// "inbounds" flag in LangRef.html for details.
481 template<typename InputIterator>
482 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
483 InputIterator IdxEnd,
484 const Twine &NameStr = "",
485 Instruction *InsertBefore = 0) {
486 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
487 NameStr, InsertBefore);
488 cast<GEPOperator>(GEP)->setIsInBounds(true);
491 template<typename InputIterator>
492 static GetElementPtrInst *CreateInBounds(Value *Ptr,
493 InputIterator IdxBegin,
494 InputIterator IdxEnd,
495 const Twine &NameStr,
496 BasicBlock *InsertAtEnd) {
497 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
498 NameStr, InsertAtEnd);
499 cast<GEPOperator>(GEP)->setIsInBounds(true);
502 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
503 const Twine &NameStr = "",
504 Instruction *InsertBefore = 0) {
505 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
506 cast<GEPOperator>(GEP)->setIsInBounds(true);
509 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
510 const Twine &NameStr,
511 BasicBlock *InsertAtEnd) {
512 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
513 cast<GEPOperator>(GEP)->setIsInBounds(true);
517 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
519 /// Transparently provide more efficient getOperand methods.
520 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
522 // getType - Overload to return most specific pointer type...
523 const PointerType *getType() const {
524 return reinterpret_cast<const PointerType*>(Instruction::getType());
527 /// getIndexedType - Returns the type of the element that would be loaded with
528 /// a load instruction with the specified parameters.
530 /// Null is returned if the indices are invalid for the specified
533 template<typename InputIterator>
534 static const Type *getIndexedType(const Type *Ptr,
535 InputIterator IdxBegin,
536 InputIterator IdxEnd) {
537 return getIndexedType(Ptr, IdxBegin, IdxEnd,
538 typename std::iterator_traits<InputIterator>::
539 iterator_category());
542 static const Type *getIndexedType(const Type *Ptr,
543 Value* const *Idx, unsigned NumIdx);
545 static const Type *getIndexedType(const Type *Ptr,
546 uint64_t const *Idx, unsigned NumIdx);
548 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
550 inline op_iterator idx_begin() { return op_begin()+1; }
551 inline const_op_iterator idx_begin() const { return op_begin()+1; }
552 inline op_iterator idx_end() { return op_end(); }
553 inline const_op_iterator idx_end() const { return op_end(); }
555 Value *getPointerOperand() {
556 return getOperand(0);
558 const Value *getPointerOperand() const {
559 return getOperand(0);
561 static unsigned getPointerOperandIndex() {
562 return 0U; // get index for modifying correct operand
565 /// getPointerOperandType - Method to return the pointer operand as a
567 const PointerType *getPointerOperandType() const {
568 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
572 unsigned getNumIndices() const { // Note: always non-negative
573 return getNumOperands() - 1;
576 bool hasIndices() const {
577 return getNumOperands() > 1;
580 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
581 /// zeros. If so, the result pointer and the first operand have the same
582 /// value, just potentially different types.
583 bool hasAllZeroIndices() const;
585 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
586 /// constant integers. If so, the result pointer and the first operand have
587 /// a constant offset between them.
588 bool hasAllConstantIndices() const;
590 // Methods for support type inquiry through isa, cast, and dyn_cast:
591 static inline bool classof(const GetElementPtrInst *) { return true; }
592 static inline bool classof(const Instruction *I) {
593 return (I->getOpcode() == Instruction::GetElementPtr);
595 static inline bool classof(const Value *V) {
596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
601 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
604 template<typename InputIterator>
605 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
606 InputIterator IdxBegin,
607 InputIterator IdxEnd,
609 const Twine &NameStr,
610 Instruction *InsertBefore)
611 : Instruction(PointerType::get(checkType(
612 getIndexedType(Ptr->getType(),
614 cast<PointerType>(Ptr->getType())
615 ->getAddressSpace()),
617 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
618 Values, InsertBefore) {
619 init(Ptr, IdxBegin, IdxEnd, NameStr,
620 typename std::iterator_traits<InputIterator>::iterator_category());
622 template<typename InputIterator>
623 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
624 InputIterator IdxBegin,
625 InputIterator IdxEnd,
627 const Twine &NameStr,
628 BasicBlock *InsertAtEnd)
629 : Instruction(PointerType::get(checkType(
630 getIndexedType(Ptr->getType(),
632 cast<PointerType>(Ptr->getType())
633 ->getAddressSpace()),
635 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
636 Values, InsertAtEnd) {
637 init(Ptr, IdxBegin, IdxEnd, NameStr,
638 typename std::iterator_traits<InputIterator>::iterator_category());
642 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
645 //===----------------------------------------------------------------------===//
647 //===----------------------------------------------------------------------===//
649 /// This instruction compares its operands according to the predicate given
650 /// to the constructor. It only operates on integers or pointers. The operands
651 /// must be identical types.
652 /// @brief Represent an integer comparison operator.
653 class ICmpInst: public CmpInst {
655 /// @brief Constructor with insert-before-instruction semantics.
657 Instruction *InsertBefore, ///< Where to insert
658 Predicate pred, ///< The predicate to use for the comparison
659 Value *LHS, ///< The left-hand-side of the expression
660 Value *RHS, ///< The right-hand-side of the expression
661 const Twine &NameStr = "" ///< Name of the instruction
662 ) : CmpInst(makeCmpResultType(LHS->getType()),
663 Instruction::ICmp, pred, LHS, RHS, NameStr,
665 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
666 pred <= CmpInst::LAST_ICMP_PREDICATE &&
667 "Invalid ICmp predicate value");
668 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
669 "Both operands to ICmp instruction are not of the same type!");
670 // Check that the operands are the right type
671 assert((getOperand(0)->getType()->isIntOrIntVector() ||
672 isa<PointerType>(getOperand(0)->getType())) &&
673 "Invalid operand types for ICmp instruction");
676 /// @brief Constructor with insert-at-end semantics.
678 BasicBlock &InsertAtEnd, ///< Block to insert into.
679 Predicate pred, ///< The predicate to use for the comparison
680 Value *LHS, ///< The left-hand-side of the expression
681 Value *RHS, ///< The right-hand-side of the expression
682 const Twine &NameStr = "" ///< Name of the instruction
683 ) : CmpInst(makeCmpResultType(LHS->getType()),
684 Instruction::ICmp, pred, LHS, RHS, NameStr,
686 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
687 pred <= CmpInst::LAST_ICMP_PREDICATE &&
688 "Invalid ICmp predicate value");
689 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
690 "Both operands to ICmp instruction are not of the same type!");
691 // Check that the operands are the right type
692 assert((getOperand(0)->getType()->isIntOrIntVector() ||
693 isa<PointerType>(getOperand(0)->getType())) &&
694 "Invalid operand types for ICmp instruction");
697 /// @brief Constructor with no-insertion semantics
699 Predicate pred, ///< The predicate to use for the comparison
700 Value *LHS, ///< The left-hand-side of the expression
701 Value *RHS, ///< The right-hand-side of the expression
702 const Twine &NameStr = "" ///< Name of the instruction
703 ) : CmpInst(makeCmpResultType(LHS->getType()),
704 Instruction::ICmp, pred, LHS, RHS, NameStr) {
705 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
706 pred <= CmpInst::LAST_ICMP_PREDICATE &&
707 "Invalid ICmp predicate value");
708 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
709 "Both operands to ICmp instruction are not of the same type!");
710 // Check that the operands are the right type
711 assert((getOperand(0)->getType()->isIntOrIntVector() ||
712 isa<PointerType>(getOperand(0)->getType())) &&
713 "Invalid operand types for ICmp instruction");
716 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
717 /// @returns the predicate that would be the result if the operand were
718 /// regarded as signed.
719 /// @brief Return the signed version of the predicate
720 Predicate getSignedPredicate() const {
721 return getSignedPredicate(getPredicate());
724 /// This is a static version that you can use without an instruction.
725 /// @brief Return the signed version of the predicate.
726 static Predicate getSignedPredicate(Predicate pred);
728 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
729 /// @returns the predicate that would be the result if the operand were
730 /// regarded as unsigned.
731 /// @brief Return the unsigned version of the predicate
732 Predicate getUnsignedPredicate() const {
733 return getUnsignedPredicate(getPredicate());
736 /// This is a static version that you can use without an instruction.
737 /// @brief Return the unsigned version of the predicate.
738 static Predicate getUnsignedPredicate(Predicate pred);
740 /// isEquality - Return true if this predicate is either EQ or NE. This also
741 /// tests for commutativity.
742 static bool isEquality(Predicate P) {
743 return P == ICMP_EQ || P == ICMP_NE;
746 /// isEquality - Return true if this predicate is either EQ or NE. This also
747 /// tests for commutativity.
748 bool isEquality() const {
749 return isEquality(getPredicate());
752 /// @returns true if the predicate of this ICmpInst is commutative
753 /// @brief Determine if this relation is commutative.
754 bool isCommutative() const { return isEquality(); }
756 /// isRelational - Return true if the predicate is relational (not EQ or NE).
758 bool isRelational() const {
759 return !isEquality();
762 /// isRelational - Return true if the predicate is relational (not EQ or NE).
764 static bool isRelational(Predicate P) {
765 return !isEquality(P);
768 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
769 /// @brief Determine if this instruction's predicate is signed.
770 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
772 /// @returns true if the predicate provided is signed, false otherwise
773 /// @brief Determine if the predicate is signed.
774 static bool isSignedPredicate(Predicate pred);
776 /// @returns true if the specified compare predicate is
777 /// true when both operands are equal...
778 /// @brief Determine if the icmp is true when both operands are equal
779 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
780 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
781 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
782 pred == ICmpInst::ICMP_SLE;
785 /// @returns true if the specified compare instruction is
786 /// true when both operands are equal...
787 /// @brief Determine if the ICmpInst returns true when both operands are equal
788 bool isTrueWhenEqual() {
789 return isTrueWhenEqual(getPredicate());
792 /// Initialize a set of values that all satisfy the predicate with C.
793 /// @brief Make a ConstantRange for a relation with a constant value.
794 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
796 /// Exchange the two operands to this instruction in such a way that it does
797 /// not modify the semantics of the instruction. The predicate value may be
798 /// changed to retain the same result if the predicate is order dependent
800 /// @brief Swap operands and adjust predicate.
801 void swapOperands() {
802 SubclassData = getSwappedPredicate();
803 Op<0>().swap(Op<1>());
806 virtual ICmpInst *clone(LLVMContext &Context) const;
808 // Methods for support type inquiry through isa, cast, and dyn_cast:
809 static inline bool classof(const ICmpInst *) { return true; }
810 static inline bool classof(const Instruction *I) {
811 return I->getOpcode() == Instruction::ICmp;
813 static inline bool classof(const Value *V) {
814 return isa<Instruction>(V) && classof(cast<Instruction>(V));
819 //===----------------------------------------------------------------------===//
821 //===----------------------------------------------------------------------===//
823 /// This instruction compares its operands according to the predicate given
824 /// to the constructor. It only operates on floating point values or packed
825 /// vectors of floating point values. The operands must be identical types.
826 /// @brief Represents a floating point comparison operator.
827 class FCmpInst: public CmpInst {
829 /// @brief Constructor with insert-before-instruction semantics.
831 Instruction *InsertBefore, ///< Where to insert
832 Predicate pred, ///< The predicate to use for the comparison
833 Value *LHS, ///< The left-hand-side of the expression
834 Value *RHS, ///< The right-hand-side of the expression
835 const Twine &NameStr = "" ///< Name of the instruction
836 ) : CmpInst(makeCmpResultType(LHS->getType()),
837 Instruction::FCmp, pred, LHS, RHS, NameStr,
839 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
840 "Invalid FCmp predicate value");
841 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
842 "Both operands to FCmp instruction are not of the same type!");
843 // Check that the operands are the right type
844 assert(getOperand(0)->getType()->isFPOrFPVector() &&
845 "Invalid operand types for FCmp instruction");
848 /// @brief Constructor with insert-at-end semantics.
850 BasicBlock &InsertAtEnd, ///< Block to insert into.
851 Predicate pred, ///< The predicate to use for the comparison
852 Value *LHS, ///< The left-hand-side of the expression
853 Value *RHS, ///< The right-hand-side of the expression
854 const Twine &NameStr = "" ///< Name of the instruction
855 ) : CmpInst(makeCmpResultType(LHS->getType()),
856 Instruction::FCmp, pred, LHS, RHS, NameStr,
858 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
859 "Invalid FCmp predicate value");
860 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
861 "Both operands to FCmp instruction are not of the same type!");
862 // Check that the operands are the right type
863 assert(getOperand(0)->getType()->isFPOrFPVector() &&
864 "Invalid operand types for FCmp instruction");
867 /// @brief Constructor with no-insertion semantics
869 Predicate pred, ///< The predicate to use for the comparison
870 Value *LHS, ///< The left-hand-side of the expression
871 Value *RHS, ///< The right-hand-side of the expression
872 const Twine &NameStr = "" ///< Name of the instruction
873 ) : CmpInst(makeCmpResultType(LHS->getType()),
874 Instruction::FCmp, pred, LHS, RHS, NameStr) {
875 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
876 "Invalid FCmp predicate value");
877 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
878 "Both operands to FCmp instruction are not of the same type!");
879 // Check that the operands are the right type
880 assert(getOperand(0)->getType()->isFPOrFPVector() &&
881 "Invalid operand types for FCmp instruction");
884 /// @returns true if the predicate of this instruction is EQ or NE.
885 /// @brief Determine if this is an equality predicate.
886 bool isEquality() const {
887 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
888 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
891 /// @returns true if the predicate of this instruction is commutative.
892 /// @brief Determine if this is a commutative predicate.
893 bool isCommutative() const {
894 return isEquality() ||
895 SubclassData == FCMP_FALSE ||
896 SubclassData == FCMP_TRUE ||
897 SubclassData == FCMP_ORD ||
898 SubclassData == FCMP_UNO;
901 /// @returns true if the predicate is relational (not EQ or NE).
902 /// @brief Determine if this a relational predicate.
903 bool isRelational() const { return !isEquality(); }
905 /// Exchange the two operands to this instruction in such a way that it does
906 /// not modify the semantics of the instruction. The predicate value may be
907 /// changed to retain the same result if the predicate is order dependent
909 /// @brief Swap operands and adjust predicate.
910 void swapOperands() {
911 SubclassData = getSwappedPredicate();
912 Op<0>().swap(Op<1>());
915 virtual FCmpInst *clone(LLVMContext &Context) const;
917 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
918 static inline bool classof(const FCmpInst *) { return true; }
919 static inline bool classof(const Instruction *I) {
920 return I->getOpcode() == Instruction::FCmp;
922 static inline bool classof(const Value *V) {
923 return isa<Instruction>(V) && classof(cast<Instruction>(V));
927 //===----------------------------------------------------------------------===//
929 //===----------------------------------------------------------------------===//
930 /// CallInst - This class represents a function call, abstracting a target
931 /// machine's calling convention. This class uses low bit of the SubClassData
932 /// field to indicate whether or not this is a tail call. The rest of the bits
933 /// hold the calling convention of the call.
936 class CallInst : public Instruction {
937 AttrListPtr AttributeList; ///< parameter attributes for call
938 CallInst(const CallInst &CI);
939 void init(Value *Func, Value* const *Params, unsigned NumParams);
940 void init(Value *Func, Value *Actual1, Value *Actual2);
941 void init(Value *Func, Value *Actual);
942 void init(Value *Func);
944 template<typename InputIterator>
945 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
946 const Twine &NameStr,
947 // This argument ensures that we have an iterator we can
948 // do arithmetic on in constant time
949 std::random_access_iterator_tag) {
950 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
952 // This requires that the iterator points to contiguous memory.
953 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
957 /// Construct a CallInst given a range of arguments. InputIterator
958 /// must be a random-access iterator pointing to contiguous storage
959 /// (e.g. a std::vector<>::iterator). Checks are made for
960 /// random-accessness but not for contiguous storage as that would
961 /// incur runtime overhead.
962 /// @brief Construct a CallInst from a range of arguments
963 template<typename InputIterator>
964 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
965 const Twine &NameStr, Instruction *InsertBefore);
967 /// Construct a CallInst given a range of arguments. InputIterator
968 /// must be a random-access iterator pointing to contiguous storage
969 /// (e.g. a std::vector<>::iterator). Checks are made for
970 /// random-accessness but not for contiguous storage as that would
971 /// incur runtime overhead.
972 /// @brief Construct a CallInst from a range of arguments
973 template<typename InputIterator>
974 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
975 const Twine &NameStr, BasicBlock *InsertAtEnd);
977 CallInst(Value *F, Value *Actual, const Twine &NameStr,
978 Instruction *InsertBefore);
979 CallInst(Value *F, Value *Actual, const Twine &NameStr,
980 BasicBlock *InsertAtEnd);
981 explicit CallInst(Value *F, const Twine &NameStr,
982 Instruction *InsertBefore);
983 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
985 template<typename InputIterator>
986 static CallInst *Create(Value *Func,
987 InputIterator ArgBegin, InputIterator ArgEnd,
988 const Twine &NameStr = "",
989 Instruction *InsertBefore = 0) {
990 return new((unsigned)(ArgEnd - ArgBegin + 1))
991 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
993 template<typename InputIterator>
994 static CallInst *Create(Value *Func,
995 InputIterator ArgBegin, InputIterator ArgEnd,
996 const Twine &NameStr, BasicBlock *InsertAtEnd) {
997 return new((unsigned)(ArgEnd - ArgBegin + 1))
998 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1000 static CallInst *Create(Value *F, Value *Actual,
1001 const Twine &NameStr = "",
1002 Instruction *InsertBefore = 0) {
1003 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1005 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1006 BasicBlock *InsertAtEnd) {
1007 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1009 static CallInst *Create(Value *F, const Twine &NameStr = "",
1010 Instruction *InsertBefore = 0) {
1011 return new(1) CallInst(F, NameStr, InsertBefore);
1013 static CallInst *Create(Value *F, const Twine &NameStr,
1014 BasicBlock *InsertAtEnd) {
1015 return new(1) CallInst(F, NameStr, InsertAtEnd);
1020 bool isTailCall() const { return SubclassData & 1; }
1021 void setTailCall(bool isTC = true) {
1022 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1025 virtual CallInst *clone(LLVMContext &Context) const;
1027 /// Provide fast operand accessors
1028 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1030 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1032 unsigned getCallingConv() const { return SubclassData >> 1; }
1033 void setCallingConv(unsigned CC) {
1034 SubclassData = (SubclassData & 1) | (CC << 1);
1037 /// getAttributes - Return the parameter attributes for this call.
1039 const AttrListPtr &getAttributes() const { return AttributeList; }
1041 /// setAttributes - Set the parameter attributes for this call.
1043 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1045 /// addAttribute - adds the attribute to the list of attributes.
1046 void addAttribute(unsigned i, Attributes attr);
1048 /// removeAttribute - removes the attribute from the list of attributes.
1049 void removeAttribute(unsigned i, Attributes attr);
1051 /// @brief Determine whether the call or the callee has the given attribute.
1052 bool paramHasAttr(unsigned i, Attributes attr) const;
1054 /// @brief Extract the alignment for a call or parameter (0=unknown).
1055 unsigned getParamAlignment(unsigned i) const {
1056 return AttributeList.getParamAlignment(i);
1059 /// @brief Determine if the call does not access memory.
1060 bool doesNotAccessMemory() const {
1061 return paramHasAttr(~0, Attribute::ReadNone);
1063 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1064 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1065 else removeAttribute(~0, Attribute::ReadNone);
1068 /// @brief Determine if the call does not access or only reads memory.
1069 bool onlyReadsMemory() const {
1070 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1072 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1073 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1074 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1077 /// @brief Determine if the call cannot return.
1078 bool doesNotReturn() const {
1079 return paramHasAttr(~0, Attribute::NoReturn);
1081 void setDoesNotReturn(bool DoesNotReturn = true) {
1082 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1083 else removeAttribute(~0, Attribute::NoReturn);
1086 /// @brief Determine if the call cannot unwind.
1087 bool doesNotThrow() const {
1088 return paramHasAttr(~0, Attribute::NoUnwind);
1090 void setDoesNotThrow(bool DoesNotThrow = true) {
1091 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1092 else removeAttribute(~0, Attribute::NoUnwind);
1095 /// @brief Determine if the call returns a structure through first
1096 /// pointer argument.
1097 bool hasStructRetAttr() const {
1098 // Be friendly and also check the callee.
1099 return paramHasAttr(1, Attribute::StructRet);
1102 /// @brief Determine if any call argument is an aggregate passed by value.
1103 bool hasByValArgument() const {
1104 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1107 /// getCalledFunction - Return the function called, or null if this is an
1108 /// indirect function invocation.
1110 Function *getCalledFunction() const {
1111 return dyn_cast<Function>(Op<0>());
1114 /// getCalledValue - Get a pointer to the function that is invoked by this
1116 const Value *getCalledValue() const { return Op<0>(); }
1117 Value *getCalledValue() { return Op<0>(); }
1119 // Methods for support type inquiry through isa, cast, and dyn_cast:
1120 static inline bool classof(const CallInst *) { return true; }
1121 static inline bool classof(const Instruction *I) {
1122 return I->getOpcode() == Instruction::Call;
1124 static inline bool classof(const Value *V) {
1125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1130 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1133 template<typename InputIterator>
1134 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1135 const Twine &NameStr, BasicBlock *InsertAtEnd)
1136 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1137 ->getElementType())->getReturnType(),
1139 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1140 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1141 init(Func, ArgBegin, ArgEnd, NameStr,
1142 typename std::iterator_traits<InputIterator>::iterator_category());
1145 template<typename InputIterator>
1146 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1147 const Twine &NameStr, Instruction *InsertBefore)
1148 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1149 ->getElementType())->getReturnType(),
1151 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1152 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1153 init(Func, ArgBegin, ArgEnd, NameStr,
1154 typename std::iterator_traits<InputIterator>::iterator_category());
1157 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1159 //===----------------------------------------------------------------------===//
1161 //===----------------------------------------------------------------------===//
1163 /// SelectInst - This class represents the LLVM 'select' instruction.
1165 class SelectInst : public Instruction {
1166 void init(Value *C, Value *S1, Value *S2) {
1167 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1173 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1174 Instruction *InsertBefore)
1175 : Instruction(S1->getType(), Instruction::Select,
1176 &Op<0>(), 3, InsertBefore) {
1180 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1181 BasicBlock *InsertAtEnd)
1182 : Instruction(S1->getType(), Instruction::Select,
1183 &Op<0>(), 3, InsertAtEnd) {
1188 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1189 const Twine &NameStr = "",
1190 Instruction *InsertBefore = 0) {
1191 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1193 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1194 const Twine &NameStr,
1195 BasicBlock *InsertAtEnd) {
1196 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1199 Value *getCondition() const { return Op<0>(); }
1200 Value *getTrueValue() const { return Op<1>(); }
1201 Value *getFalseValue() const { return Op<2>(); }
1203 /// areInvalidOperands - Return a string if the specified operands are invalid
1204 /// for a select operation, otherwise return null.
1205 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1207 /// Transparently provide more efficient getOperand methods.
1208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1210 OtherOps getOpcode() const {
1211 return static_cast<OtherOps>(Instruction::getOpcode());
1214 virtual SelectInst *clone(LLVMContext &Context) const;
1216 // Methods for support type inquiry through isa, cast, and dyn_cast:
1217 static inline bool classof(const SelectInst *) { return true; }
1218 static inline bool classof(const Instruction *I) {
1219 return I->getOpcode() == Instruction::Select;
1221 static inline bool classof(const Value *V) {
1222 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1227 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1230 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1232 //===----------------------------------------------------------------------===//
1234 //===----------------------------------------------------------------------===//
1236 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1237 /// an argument of the specified type given a va_list and increments that list
1239 class VAArgInst : public UnaryInstruction {
1241 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1242 Instruction *InsertBefore = 0)
1243 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1246 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1247 BasicBlock *InsertAtEnd)
1248 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1252 virtual VAArgInst *clone(LLVMContext &Context) const;
1254 // Methods for support type inquiry through isa, cast, and dyn_cast:
1255 static inline bool classof(const VAArgInst *) { return true; }
1256 static inline bool classof(const Instruction *I) {
1257 return I->getOpcode() == VAArg;
1259 static inline bool classof(const Value *V) {
1260 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1264 //===----------------------------------------------------------------------===//
1265 // ExtractElementInst Class
1266 //===----------------------------------------------------------------------===//
1268 /// ExtractElementInst - This instruction extracts a single (scalar)
1269 /// element from a VectorType value
1271 class ExtractElementInst : public Instruction {
1272 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1273 Instruction *InsertBefore = 0);
1274 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1275 BasicBlock *InsertAtEnd);
1277 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1278 const Twine &NameStr = "",
1279 Instruction *InsertBefore = 0) {
1280 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1282 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1283 const Twine &NameStr,
1284 BasicBlock *InsertAtEnd) {
1285 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1288 /// isValidOperands - Return true if an extractelement instruction can be
1289 /// formed with the specified operands.
1290 static bool isValidOperands(const Value *Vec, const Value *Idx);
1292 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1294 /// Transparently provide more efficient getOperand methods.
1295 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1297 // Methods for support type inquiry through isa, cast, and dyn_cast:
1298 static inline bool classof(const ExtractElementInst *) { return true; }
1299 static inline bool classof(const Instruction *I) {
1300 return I->getOpcode() == Instruction::ExtractElement;
1302 static inline bool classof(const Value *V) {
1303 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1308 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1311 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1313 //===----------------------------------------------------------------------===//
1314 // InsertElementInst Class
1315 //===----------------------------------------------------------------------===//
1317 /// InsertElementInst - This instruction inserts a single (scalar)
1318 /// element into a VectorType value
1320 class InsertElementInst : public Instruction {
1321 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1322 const Twine &NameStr = "",
1323 Instruction *InsertBefore = 0);
1324 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1325 const Twine &NameStr, BasicBlock *InsertAtEnd);
1327 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1328 const Twine &NameStr = "",
1329 Instruction *InsertBefore = 0) {
1330 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1332 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1333 const Twine &NameStr,
1334 BasicBlock *InsertAtEnd) {
1335 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1338 /// isValidOperands - Return true if an insertelement instruction can be
1339 /// formed with the specified operands.
1340 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1343 virtual InsertElementInst *clone(LLVMContext &Context) const;
1345 /// getType - Overload to return most specific vector type.
1347 const VectorType *getType() const {
1348 return reinterpret_cast<const VectorType*>(Instruction::getType());
1351 /// Transparently provide more efficient getOperand methods.
1352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1354 // Methods for support type inquiry through isa, cast, and dyn_cast:
1355 static inline bool classof(const InsertElementInst *) { return true; }
1356 static inline bool classof(const Instruction *I) {
1357 return I->getOpcode() == Instruction::InsertElement;
1359 static inline bool classof(const Value *V) {
1360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1365 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1368 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1370 //===----------------------------------------------------------------------===//
1371 // ShuffleVectorInst Class
1372 //===----------------------------------------------------------------------===//
1374 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1377 class ShuffleVectorInst : public Instruction {
1379 // allocate space for exactly three operands
1380 void *operator new(size_t s) {
1381 return User::operator new(s, 3);
1383 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1384 const Twine &NameStr = "",
1385 Instruction *InsertBefor = 0);
1386 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1387 const Twine &NameStr, BasicBlock *InsertAtEnd);
1389 /// isValidOperands - Return true if a shufflevector instruction can be
1390 /// formed with the specified operands.
1391 static bool isValidOperands(const Value *V1, const Value *V2,
1394 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1396 /// getType - Overload to return most specific vector type.
1398 const VectorType *getType() const {
1399 return reinterpret_cast<const VectorType*>(Instruction::getType());
1402 /// Transparently provide more efficient getOperand methods.
1403 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1405 /// getMaskValue - Return the index from the shuffle mask for the specified
1406 /// output result. This is either -1 if the element is undef or a number less
1407 /// than 2*numelements.
1408 int getMaskValue(unsigned i) const;
1410 // Methods for support type inquiry through isa, cast, and dyn_cast:
1411 static inline bool classof(const ShuffleVectorInst *) { return true; }
1412 static inline bool classof(const Instruction *I) {
1413 return I->getOpcode() == Instruction::ShuffleVector;
1415 static inline bool classof(const Value *V) {
1416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1421 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1424 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1426 //===----------------------------------------------------------------------===//
1427 // ExtractValueInst Class
1428 //===----------------------------------------------------------------------===//
1430 /// ExtractValueInst - This instruction extracts a struct member or array
1431 /// element value from an aggregate value.
1433 class ExtractValueInst : public UnaryInstruction {
1434 SmallVector<unsigned, 4> Indices;
1436 ExtractValueInst(const ExtractValueInst &EVI);
1437 void init(const unsigned *Idx, unsigned NumIdx,
1438 const Twine &NameStr);
1439 void init(unsigned Idx, const Twine &NameStr);
1441 template<typename InputIterator>
1442 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1443 const Twine &NameStr,
1444 // This argument ensures that we have an iterator we can
1445 // do arithmetic on in constant time
1446 std::random_access_iterator_tag) {
1447 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1449 // There's no fundamental reason why we require at least one index
1450 // (other than weirdness with &*IdxBegin being invalid; see
1451 // getelementptr's init routine for example). But there's no
1452 // present need to support it.
1453 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1455 // This requires that the iterator points to contiguous memory.
1456 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1457 // we have to build an array here
1460 /// getIndexedType - Returns the type of the element that would be extracted
1461 /// with an extractvalue instruction with the specified parameters.
1463 /// Null is returned if the indices are invalid for the specified
1466 static const Type *getIndexedType(const Type *Agg,
1467 const unsigned *Idx, unsigned NumIdx);
1469 template<typename InputIterator>
1470 static const Type *getIndexedType(const Type *Ptr,
1471 InputIterator IdxBegin,
1472 InputIterator IdxEnd,
1473 // This argument ensures that we
1474 // have an iterator we can do
1475 // arithmetic on in constant time
1476 std::random_access_iterator_tag) {
1477 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1480 // This requires that the iterator points to contiguous memory.
1481 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1483 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1486 /// Constructors - Create a extractvalue instruction with a base aggregate
1487 /// value and a list of indices. The first ctor can optionally insert before
1488 /// an existing instruction, the second appends the new instruction to the
1489 /// specified BasicBlock.
1490 template<typename InputIterator>
1491 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1492 InputIterator IdxEnd,
1493 const Twine &NameStr,
1494 Instruction *InsertBefore);
1495 template<typename InputIterator>
1496 inline ExtractValueInst(Value *Agg,
1497 InputIterator IdxBegin, InputIterator IdxEnd,
1498 const Twine &NameStr, BasicBlock *InsertAtEnd);
1500 // allocate space for exactly one operand
1501 void *operator new(size_t s) {
1502 return User::operator new(s, 1);
1506 template<typename InputIterator>
1507 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1508 InputIterator IdxEnd,
1509 const Twine &NameStr = "",
1510 Instruction *InsertBefore = 0) {
1512 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1514 template<typename InputIterator>
1515 static ExtractValueInst *Create(Value *Agg,
1516 InputIterator IdxBegin, InputIterator IdxEnd,
1517 const Twine &NameStr,
1518 BasicBlock *InsertAtEnd) {
1519 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1522 /// Constructors - These two creators are convenience methods because one
1523 /// index extractvalue instructions are much more common than those with
1525 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1526 const Twine &NameStr = "",
1527 Instruction *InsertBefore = 0) {
1528 unsigned Idxs[1] = { Idx };
1529 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1531 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532 const Twine &NameStr,
1533 BasicBlock *InsertAtEnd) {
1534 unsigned Idxs[1] = { Idx };
1535 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1538 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1540 /// getIndexedType - Returns the type of the element that would be extracted
1541 /// with an extractvalue instruction with the specified parameters.
1543 /// Null is returned if the indices are invalid for the specified
1546 template<typename InputIterator>
1547 static const Type *getIndexedType(const Type *Ptr,
1548 InputIterator IdxBegin,
1549 InputIterator IdxEnd) {
1550 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1551 typename std::iterator_traits<InputIterator>::
1552 iterator_category());
1554 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1556 typedef const unsigned* idx_iterator;
1557 inline idx_iterator idx_begin() const { return Indices.begin(); }
1558 inline idx_iterator idx_end() const { return Indices.end(); }
1560 Value *getAggregateOperand() {
1561 return getOperand(0);
1563 const Value *getAggregateOperand() const {
1564 return getOperand(0);
1566 static unsigned getAggregateOperandIndex() {
1567 return 0U; // get index for modifying correct operand
1570 unsigned getNumIndices() const { // Note: always non-negative
1571 return (unsigned)Indices.size();
1574 bool hasIndices() const {
1578 // Methods for support type inquiry through isa, cast, and dyn_cast:
1579 static inline bool classof(const ExtractValueInst *) { return true; }
1580 static inline bool classof(const Instruction *I) {
1581 return I->getOpcode() == Instruction::ExtractValue;
1583 static inline bool classof(const Value *V) {
1584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1588 template<typename InputIterator>
1589 ExtractValueInst::ExtractValueInst(Value *Agg,
1590 InputIterator IdxBegin,
1591 InputIterator IdxEnd,
1592 const Twine &NameStr,
1593 Instruction *InsertBefore)
1594 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1596 ExtractValue, Agg, InsertBefore) {
1597 init(IdxBegin, IdxEnd, NameStr,
1598 typename std::iterator_traits<InputIterator>::iterator_category());
1600 template<typename InputIterator>
1601 ExtractValueInst::ExtractValueInst(Value *Agg,
1602 InputIterator IdxBegin,
1603 InputIterator IdxEnd,
1604 const Twine &NameStr,
1605 BasicBlock *InsertAtEnd)
1606 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1608 ExtractValue, Agg, InsertAtEnd) {
1609 init(IdxBegin, IdxEnd, NameStr,
1610 typename std::iterator_traits<InputIterator>::iterator_category());
1614 //===----------------------------------------------------------------------===//
1615 // InsertValueInst Class
1616 //===----------------------------------------------------------------------===//
1618 /// InsertValueInst - This instruction inserts a struct field of array element
1619 /// value into an aggregate value.
1621 class InsertValueInst : public Instruction {
1622 SmallVector<unsigned, 4> Indices;
1624 void *operator new(size_t, unsigned); // Do not implement
1625 InsertValueInst(const InsertValueInst &IVI);
1626 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1627 const Twine &NameStr);
1628 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1630 template<typename InputIterator>
1631 void init(Value *Agg, Value *Val,
1632 InputIterator IdxBegin, InputIterator IdxEnd,
1633 const Twine &NameStr,
1634 // This argument ensures that we have an iterator we can
1635 // do arithmetic on in constant time
1636 std::random_access_iterator_tag) {
1637 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1639 // There's no fundamental reason why we require at least one index
1640 // (other than weirdness with &*IdxBegin being invalid; see
1641 // getelementptr's init routine for example). But there's no
1642 // present need to support it.
1643 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1645 // This requires that the iterator points to contiguous memory.
1646 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1647 // we have to build an array here
1650 /// Constructors - Create a insertvalue instruction with a base aggregate
1651 /// value, a value to insert, and a list of indices. The first ctor can
1652 /// optionally insert before an existing instruction, the second appends
1653 /// the new instruction to the specified BasicBlock.
1654 template<typename InputIterator>
1655 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1656 InputIterator IdxEnd,
1657 const Twine &NameStr,
1658 Instruction *InsertBefore);
1659 template<typename InputIterator>
1660 inline InsertValueInst(Value *Agg, Value *Val,
1661 InputIterator IdxBegin, InputIterator IdxEnd,
1662 const Twine &NameStr, BasicBlock *InsertAtEnd);
1664 /// Constructors - These two constructors are convenience methods because one
1665 /// and two index insertvalue instructions are so common.
1666 InsertValueInst(Value *Agg, Value *Val,
1667 unsigned Idx, const Twine &NameStr = "",
1668 Instruction *InsertBefore = 0);
1669 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1670 const Twine &NameStr, BasicBlock *InsertAtEnd);
1672 // allocate space for exactly two operands
1673 void *operator new(size_t s) {
1674 return User::operator new(s, 2);
1677 template<typename InputIterator>
1678 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1679 InputIterator IdxEnd,
1680 const Twine &NameStr = "",
1681 Instruction *InsertBefore = 0) {
1682 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1683 NameStr, InsertBefore);
1685 template<typename InputIterator>
1686 static InsertValueInst *Create(Value *Agg, Value *Val,
1687 InputIterator IdxBegin, InputIterator IdxEnd,
1688 const Twine &NameStr,
1689 BasicBlock *InsertAtEnd) {
1690 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1691 NameStr, InsertAtEnd);
1694 /// Constructors - These two creators are convenience methods because one
1695 /// index insertvalue instructions are much more common than those with
1697 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1698 const Twine &NameStr = "",
1699 Instruction *InsertBefore = 0) {
1700 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1702 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1703 const Twine &NameStr,
1704 BasicBlock *InsertAtEnd) {
1705 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1708 virtual InsertValueInst *clone(LLVMContext &Context) const;
1710 /// Transparently provide more efficient getOperand methods.
1711 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1713 typedef const unsigned* idx_iterator;
1714 inline idx_iterator idx_begin() const { return Indices.begin(); }
1715 inline idx_iterator idx_end() const { return Indices.end(); }
1717 Value *getAggregateOperand() {
1718 return getOperand(0);
1720 const Value *getAggregateOperand() const {
1721 return getOperand(0);
1723 static unsigned getAggregateOperandIndex() {
1724 return 0U; // get index for modifying correct operand
1727 Value *getInsertedValueOperand() {
1728 return getOperand(1);
1730 const Value *getInsertedValueOperand() const {
1731 return getOperand(1);
1733 static unsigned getInsertedValueOperandIndex() {
1734 return 1U; // get index for modifying correct operand
1737 unsigned getNumIndices() const { // Note: always non-negative
1738 return (unsigned)Indices.size();
1741 bool hasIndices() const {
1745 // Methods for support type inquiry through isa, cast, and dyn_cast:
1746 static inline bool classof(const InsertValueInst *) { return true; }
1747 static inline bool classof(const Instruction *I) {
1748 return I->getOpcode() == Instruction::InsertValue;
1750 static inline bool classof(const Value *V) {
1751 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1756 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1759 template<typename InputIterator>
1760 InsertValueInst::InsertValueInst(Value *Agg,
1762 InputIterator IdxBegin,
1763 InputIterator IdxEnd,
1764 const Twine &NameStr,
1765 Instruction *InsertBefore)
1766 : Instruction(Agg->getType(), InsertValue,
1767 OperandTraits<InsertValueInst>::op_begin(this),
1769 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1770 typename std::iterator_traits<InputIterator>::iterator_category());
1772 template<typename InputIterator>
1773 InsertValueInst::InsertValueInst(Value *Agg,
1775 InputIterator IdxBegin,
1776 InputIterator IdxEnd,
1777 const Twine &NameStr,
1778 BasicBlock *InsertAtEnd)
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());
1786 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1788 //===----------------------------------------------------------------------===//
1790 //===----------------------------------------------------------------------===//
1792 // PHINode - The PHINode class is used to represent the magical mystical PHI
1793 // node, that can not exist in nature, but can be synthesized in a computer
1794 // scientist's overactive imagination.
1796 class PHINode : public Instruction {
1797 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1798 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1799 /// the number actually in use.
1800 unsigned ReservedSpace;
1801 PHINode(const PHINode &PN);
1802 // allocate space for exactly zero operands
1803 void *operator new(size_t s) {
1804 return User::operator new(s, 0);
1806 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1807 Instruction *InsertBefore = 0)
1808 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1813 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1814 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1819 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1820 Instruction *InsertBefore = 0) {
1821 return new PHINode(Ty, NameStr, InsertBefore);
1823 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1824 BasicBlock *InsertAtEnd) {
1825 return new PHINode(Ty, NameStr, InsertAtEnd);
1829 /// reserveOperandSpace - This method can be used to avoid repeated
1830 /// reallocation of PHI operand lists by reserving space for the correct
1831 /// number of operands before adding them. Unlike normal vector reserves,
1832 /// this method can also be used to trim the operand space.
1833 void reserveOperandSpace(unsigned NumValues) {
1834 resizeOperands(NumValues*2);
1837 virtual PHINode *clone(LLVMContext &Context) const;
1839 /// Provide fast operand accessors
1840 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1842 /// getNumIncomingValues - Return the number of incoming edges
1844 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1846 /// getIncomingValue - Return incoming value number x
1848 Value *getIncomingValue(unsigned i) const {
1849 assert(i*2 < getNumOperands() && "Invalid value number!");
1850 return getOperand(i*2);
1852 void setIncomingValue(unsigned i, Value *V) {
1853 assert(i*2 < getNumOperands() && "Invalid value number!");
1856 static unsigned getOperandNumForIncomingValue(unsigned i) {
1859 static unsigned getIncomingValueNumForOperand(unsigned i) {
1860 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1864 /// getIncomingBlock - Return incoming basic block corresponding
1865 /// to value use iterator
1867 template <typename U>
1868 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1869 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1870 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1872 /// getIncomingBlock - Return incoming basic block number x
1874 BasicBlock *getIncomingBlock(unsigned i) const {
1875 return static_cast<BasicBlock*>(getOperand(i*2+1));
1877 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1878 setOperand(i*2+1, BB);
1880 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1883 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1884 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1888 /// addIncoming - Add an incoming value to the end of the PHI list
1890 void addIncoming(Value *V, BasicBlock *BB) {
1891 assert(V && "PHI node got a null value!");
1892 assert(BB && "PHI node got a null basic block!");
1893 assert(getType() == V->getType() &&
1894 "All operands to PHI node must be the same type as the PHI node!");
1895 unsigned OpNo = NumOperands;
1896 if (OpNo+2 > ReservedSpace)
1897 resizeOperands(0); // Get more space!
1898 // Initialize some new operands.
1899 NumOperands = OpNo+2;
1900 OperandList[OpNo] = V;
1901 OperandList[OpNo+1] = BB;
1904 /// removeIncomingValue - Remove an incoming value. This is useful if a
1905 /// predecessor basic block is deleted. The value removed is returned.
1907 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1908 /// is true), the PHI node is destroyed and any uses of it are replaced with
1909 /// dummy values. The only time there should be zero incoming values to a PHI
1910 /// node is when the block is dead, so this strategy is sound.
1912 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1914 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1915 int Idx = getBasicBlockIndex(BB);
1916 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1917 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1920 /// getBasicBlockIndex - Return the first index of the specified basic
1921 /// block in the value list for this PHI. Returns -1 if no instance.
1923 int getBasicBlockIndex(const BasicBlock *BB) const {
1924 Use *OL = OperandList;
1925 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1926 if (OL[i+1].get() == BB) return i/2;
1930 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1931 return getIncomingValue(getBasicBlockIndex(BB));
1934 /// hasConstantValue - If the specified PHI node always merges together the
1935 /// same value, return the value, otherwise return null.
1937 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1939 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1940 static inline bool classof(const PHINode *) { return true; }
1941 static inline bool classof(const Instruction *I) {
1942 return I->getOpcode() == Instruction::PHI;
1944 static inline bool classof(const Value *V) {
1945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1948 void resizeOperands(unsigned NumOperands);
1952 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1955 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1958 //===----------------------------------------------------------------------===//
1960 //===----------------------------------------------------------------------===//
1962 //===---------------------------------------------------------------------------
1963 /// ReturnInst - Return a value (possibly void), from a function. Execution
1964 /// does not continue in this function any longer.
1966 class ReturnInst : public TerminatorInst {
1967 ReturnInst(const ReturnInst &RI);
1970 // ReturnInst constructors:
1971 // ReturnInst() - 'ret void' instruction
1972 // ReturnInst( null) - 'ret void' instruction
1973 // ReturnInst(Value* X) - 'ret X' instruction
1974 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1975 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1976 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1977 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1979 // NOTE: If the Value* passed is of type void then the constructor behaves as
1980 // if it was passed NULL.
1981 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1982 Instruction *InsertBefore = 0);
1983 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1984 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1986 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1987 Instruction *InsertBefore = 0) {
1988 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1990 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1991 BasicBlock *InsertAtEnd) {
1992 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1994 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1995 return new(0) ReturnInst(C, InsertAtEnd);
1997 virtual ~ReturnInst();
1999 virtual ReturnInst *clone(LLVMContext &Context) const;
2001 /// Provide fast operand accessors
2002 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2004 /// Convenience accessor
2005 Value *getReturnValue(unsigned n = 0) const {
2006 return n < getNumOperands()
2011 unsigned getNumSuccessors() const { return 0; }
2013 // Methods for support type inquiry through isa, cast, and dyn_cast:
2014 static inline bool classof(const ReturnInst *) { return true; }
2015 static inline bool classof(const Instruction *I) {
2016 return (I->getOpcode() == Instruction::Ret);
2018 static inline bool classof(const Value *V) {
2019 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2022 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2023 virtual unsigned getNumSuccessorsV() const;
2024 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2028 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2031 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2033 //===----------------------------------------------------------------------===//
2035 //===----------------------------------------------------------------------===//
2037 //===---------------------------------------------------------------------------
2038 /// BranchInst - Conditional or Unconditional Branch instruction.
2040 class BranchInst : public TerminatorInst {
2041 /// Ops list - Branches are strange. The operands are ordered:
2042 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2043 /// they don't have to check for cond/uncond branchness. These are mostly
2044 /// accessed relative from op_end().
2045 BranchInst(const BranchInst &BI);
2047 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2048 // BranchInst(BB *B) - 'br B'
2049 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2050 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2051 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2052 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2053 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2054 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2055 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2056 Instruction *InsertBefore = 0);
2057 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2058 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2059 BasicBlock *InsertAtEnd);
2061 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2062 return new(1, true) BranchInst(IfTrue, InsertBefore);
2064 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2065 Value *Cond, Instruction *InsertBefore = 0) {
2066 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2068 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2069 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2071 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2072 Value *Cond, BasicBlock *InsertAtEnd) {
2073 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2078 /// Transparently provide more efficient getOperand methods.
2079 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2081 virtual BranchInst *clone(LLVMContext &Context) const;
2083 bool isUnconditional() const { return getNumOperands() == 1; }
2084 bool isConditional() const { return getNumOperands() == 3; }
2086 Value *getCondition() const {
2087 assert(isConditional() && "Cannot get condition of an uncond branch!");
2091 void setCondition(Value *V) {
2092 assert(isConditional() && "Cannot set condition of unconditional branch!");
2096 // setUnconditionalDest - Change the current branch to an unconditional branch
2097 // targeting the specified block.
2098 // FIXME: Eliminate this ugly method.
2099 void setUnconditionalDest(BasicBlock *Dest) {
2101 if (isConditional()) { // Convert this to an uncond branch.
2105 OperandList = op_begin();
2109 unsigned getNumSuccessors() const { return 1+isConditional(); }
2111 BasicBlock *getSuccessor(unsigned i) const {
2112 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2113 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2116 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2117 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2118 *(&Op<-1>() - idx) = NewSucc;
2121 // Methods for support type inquiry through isa, cast, and dyn_cast:
2122 static inline bool classof(const BranchInst *) { return true; }
2123 static inline bool classof(const Instruction *I) {
2124 return (I->getOpcode() == Instruction::Br);
2126 static inline bool classof(const Value *V) {
2127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2130 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2131 virtual unsigned getNumSuccessorsV() const;
2132 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2136 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2138 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2140 //===----------------------------------------------------------------------===//
2142 //===----------------------------------------------------------------------===//
2144 //===---------------------------------------------------------------------------
2145 /// SwitchInst - Multiway switch
2147 class SwitchInst : public TerminatorInst {
2148 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2149 unsigned ReservedSpace;
2150 // Operand[0] = Value to switch on
2151 // Operand[1] = Default basic block destination
2152 // Operand[2n ] = Value to match
2153 // Operand[2n+1] = BasicBlock to go to on match
2154 SwitchInst(const SwitchInst &RI);
2155 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2156 void resizeOperands(unsigned No);
2157 // allocate space for exactly zero operands
2158 void *operator new(size_t s) {
2159 return User::operator new(s, 0);
2161 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2162 /// switch on and a default destination. The number of additional cases can
2163 /// be specified here to make memory allocation more efficient. This
2164 /// constructor can also autoinsert before another instruction.
2165 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2166 Instruction *InsertBefore = 0);
2168 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2169 /// switch on and a default destination. The number of additional cases can
2170 /// be specified here to make memory allocation more efficient. This
2171 /// constructor also autoinserts at the end of the specified BasicBlock.
2172 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2173 BasicBlock *InsertAtEnd);
2175 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2176 unsigned NumCases, Instruction *InsertBefore = 0) {
2177 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2179 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2180 unsigned NumCases, BasicBlock *InsertAtEnd) {
2181 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2185 /// Provide fast operand accessors
2186 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2188 // Accessor Methods for Switch stmt
2189 Value *getCondition() const { return getOperand(0); }
2190 void setCondition(Value *V) { setOperand(0, V); }
2192 BasicBlock *getDefaultDest() const {
2193 return cast<BasicBlock>(getOperand(1));
2196 /// getNumCases - return the number of 'cases' in this switch instruction.
2197 /// Note that case #0 is always the default case.
2198 unsigned getNumCases() const {
2199 return getNumOperands()/2;
2202 /// getCaseValue - Return the specified case value. Note that case #0, the
2203 /// default destination, does not have a case value.
2204 ConstantInt *getCaseValue(unsigned i) {
2205 assert(i && i < getNumCases() && "Illegal case value to get!");
2206 return getSuccessorValue(i);
2209 /// getCaseValue - Return the specified case value. Note that case #0, the
2210 /// default destination, does not have a case value.
2211 const ConstantInt *getCaseValue(unsigned i) const {
2212 assert(i && i < getNumCases() && "Illegal case value to get!");
2213 return getSuccessorValue(i);
2216 /// findCaseValue - Search all of the case values for the specified constant.
2217 /// If it is explicitly handled, return the case number of it, otherwise
2218 /// return 0 to indicate that it is handled by the default handler.
2219 unsigned findCaseValue(const ConstantInt *C) const {
2220 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2221 if (getCaseValue(i) == C)
2226 /// findCaseDest - Finds the unique case value for a given successor. Returns
2227 /// null if the successor is not found, not unique, or is the default case.
2228 ConstantInt *findCaseDest(BasicBlock *BB) {
2229 if (BB == getDefaultDest()) return NULL;
2231 ConstantInt *CI = NULL;
2232 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2233 if (getSuccessor(i) == BB) {
2234 if (CI) return NULL; // Multiple cases lead to BB.
2235 else CI = getCaseValue(i);
2241 /// addCase - Add an entry to the switch instruction...
2243 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2245 /// removeCase - This method removes the specified successor from the switch
2246 /// instruction. Note that this cannot be used to remove the default
2247 /// destination (successor #0).
2249 void removeCase(unsigned idx);
2251 virtual SwitchInst *clone(LLVMContext &Context) const;
2253 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2254 BasicBlock *getSuccessor(unsigned idx) const {
2255 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2256 return cast<BasicBlock>(getOperand(idx*2+1));
2258 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2259 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2260 setOperand(idx*2+1, NewSucc);
2263 // getSuccessorValue - Return the value associated with the specified
2265 ConstantInt *getSuccessorValue(unsigned idx) const {
2266 assert(idx < getNumSuccessors() && "Successor # out of range!");
2267 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2270 // Methods for support type inquiry through isa, cast, and dyn_cast:
2271 static inline bool classof(const SwitchInst *) { return true; }
2272 static inline bool classof(const Instruction *I) {
2273 return I->getOpcode() == Instruction::Switch;
2275 static inline bool classof(const Value *V) {
2276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2279 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2280 virtual unsigned getNumSuccessorsV() const;
2281 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2285 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2291 //===----------------------------------------------------------------------===//
2293 //===----------------------------------------------------------------------===//
2295 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2296 /// calling convention of the call.
2298 class InvokeInst : public TerminatorInst {
2299 AttrListPtr AttributeList;
2300 InvokeInst(const InvokeInst &BI);
2301 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2302 Value* const *Args, unsigned NumArgs);
2304 template<typename InputIterator>
2305 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2306 InputIterator ArgBegin, InputIterator ArgEnd,
2307 const Twine &NameStr,
2308 // This argument ensures that we have an iterator we can
2309 // do arithmetic on in constant time
2310 std::random_access_iterator_tag) {
2311 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2313 // This requires that the iterator points to contiguous memory.
2314 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2318 /// Construct an InvokeInst given a range of arguments.
2319 /// InputIterator must be a random-access iterator pointing to
2320 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2321 /// made for random-accessness but not for contiguous storage as
2322 /// that would incur runtime overhead.
2324 /// @brief Construct an InvokeInst from a range of arguments
2325 template<typename InputIterator>
2326 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2327 InputIterator ArgBegin, InputIterator ArgEnd,
2329 const Twine &NameStr, Instruction *InsertBefore);
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, BasicBlock *InsertAtEnd);
2344 template<typename InputIterator>
2345 static InvokeInst *Create(Value *Func,
2346 BasicBlock *IfNormal, BasicBlock *IfException,
2347 InputIterator ArgBegin, InputIterator ArgEnd,
2348 const Twine &NameStr = "",
2349 Instruction *InsertBefore = 0) {
2350 unsigned Values(ArgEnd - ArgBegin + 3);
2351 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2352 Values, NameStr, InsertBefore);
2354 template<typename InputIterator>
2355 static InvokeInst *Create(Value *Func,
2356 BasicBlock *IfNormal, BasicBlock *IfException,
2357 InputIterator ArgBegin, InputIterator ArgEnd,
2358 const Twine &NameStr,
2359 BasicBlock *InsertAtEnd) {
2360 unsigned Values(ArgEnd - ArgBegin + 3);
2361 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2362 Values, NameStr, InsertAtEnd);
2365 virtual InvokeInst *clone(LLVMContext &Context) const;
2367 /// Provide fast operand accessors
2368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2370 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2372 unsigned getCallingConv() const { return SubclassData; }
2373 void setCallingConv(unsigned CC) {
2377 /// getAttributes - Return the parameter attributes for this invoke.
2379 const AttrListPtr &getAttributes() const { return AttributeList; }
2381 /// setAttributes - Set the parameter attributes for this invoke.
2383 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2385 /// addAttribute - adds the attribute to the list of attributes.
2386 void addAttribute(unsigned i, Attributes attr);
2388 /// removeAttribute - removes the attribute from the list of attributes.
2389 void removeAttribute(unsigned i, Attributes attr);
2391 /// @brief Determine whether the call or the callee has the given attribute.
2392 bool paramHasAttr(unsigned i, Attributes attr) const;
2394 /// @brief Extract the alignment for a call or parameter (0=unknown).
2395 unsigned getParamAlignment(unsigned i) const {
2396 return AttributeList.getParamAlignment(i);
2399 /// @brief Determine if the call does not access memory.
2400 bool doesNotAccessMemory() const {
2401 return paramHasAttr(~0, Attribute::ReadNone);
2403 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2404 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2405 else removeAttribute(~0, Attribute::ReadNone);
2408 /// @brief Determine if the call does not access or only reads memory.
2409 bool onlyReadsMemory() const {
2410 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2412 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2413 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2414 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2417 /// @brief Determine if the call cannot return.
2418 bool doesNotReturn() const {
2419 return paramHasAttr(~0, Attribute::NoReturn);
2421 void setDoesNotReturn(bool DoesNotReturn = true) {
2422 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2423 else removeAttribute(~0, Attribute::NoReturn);
2426 /// @brief Determine if the call cannot unwind.
2427 bool doesNotThrow() const {
2428 return paramHasAttr(~0, Attribute::NoUnwind);
2430 void setDoesNotThrow(bool DoesNotThrow = true) {
2431 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2432 else removeAttribute(~0, Attribute::NoUnwind);
2435 /// @brief Determine if the call returns a structure through first
2436 /// pointer argument.
2437 bool hasStructRetAttr() const {
2438 // Be friendly and also check the callee.
2439 return paramHasAttr(1, Attribute::StructRet);
2442 /// @brief Determine if any call argument is an aggregate passed by value.
2443 bool hasByValArgument() const {
2444 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2447 /// getCalledFunction - Return the function called, or null if this is an
2448 /// indirect function invocation.
2450 Function *getCalledFunction() const {
2451 return dyn_cast<Function>(getOperand(0));
2454 /// getCalledValue - Get a pointer to the function that is invoked by this
2456 const Value *getCalledValue() const { return getOperand(0); }
2457 Value *getCalledValue() { return getOperand(0); }
2459 // get*Dest - Return the destination basic blocks...
2460 BasicBlock *getNormalDest() const {
2461 return cast<BasicBlock>(getOperand(1));
2463 BasicBlock *getUnwindDest() const {
2464 return cast<BasicBlock>(getOperand(2));
2466 void setNormalDest(BasicBlock *B) {
2470 void setUnwindDest(BasicBlock *B) {
2474 BasicBlock *getSuccessor(unsigned i) const {
2475 assert(i < 2 && "Successor # out of range for invoke!");
2476 return i == 0 ? getNormalDest() : getUnwindDest();
2479 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2480 assert(idx < 2 && "Successor # out of range for invoke!");
2481 setOperand(idx+1, NewSucc);
2484 unsigned getNumSuccessors() const { return 2; }
2486 // Methods for support type inquiry through isa, cast, and dyn_cast:
2487 static inline bool classof(const InvokeInst *) { return true; }
2488 static inline bool classof(const Instruction *I) {
2489 return (I->getOpcode() == Instruction::Invoke);
2491 static inline bool classof(const Value *V) {
2492 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2495 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2496 virtual unsigned getNumSuccessorsV() const;
2497 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2501 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2504 template<typename InputIterator>
2505 InvokeInst::InvokeInst(Value *Func,
2506 BasicBlock *IfNormal, BasicBlock *IfException,
2507 InputIterator ArgBegin, InputIterator ArgEnd,
2509 const Twine &NameStr, Instruction *InsertBefore)
2510 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2511 ->getElementType())->getReturnType(),
2512 Instruction::Invoke,
2513 OperandTraits<InvokeInst>::op_end(this) - Values,
2514 Values, InsertBefore) {
2515 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2516 typename std::iterator_traits<InputIterator>::iterator_category());
2518 template<typename InputIterator>
2519 InvokeInst::InvokeInst(Value *Func,
2520 BasicBlock *IfNormal, BasicBlock *IfException,
2521 InputIterator ArgBegin, InputIterator ArgEnd,
2523 const Twine &NameStr, BasicBlock *InsertAtEnd)
2524 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2525 ->getElementType())->getReturnType(),
2526 Instruction::Invoke,
2527 OperandTraits<InvokeInst>::op_end(this) - Values,
2528 Values, InsertAtEnd) {
2529 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2530 typename std::iterator_traits<InputIterator>::iterator_category());
2533 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2535 //===----------------------------------------------------------------------===//
2537 //===----------------------------------------------------------------------===//
2539 //===---------------------------------------------------------------------------
2540 /// UnwindInst - Immediately exit the current function, unwinding the stack
2541 /// until an invoke instruction is found.
2543 class UnwindInst : public TerminatorInst {
2544 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2546 // allocate space for exactly zero operands
2547 void *operator new(size_t s) {
2548 return User::operator new(s, 0);
2550 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2551 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2553 virtual UnwindInst *clone(LLVMContext &Context) const;
2555 unsigned getNumSuccessors() const { return 0; }
2557 // Methods for support type inquiry through isa, cast, and dyn_cast:
2558 static inline bool classof(const UnwindInst *) { return true; }
2559 static inline bool classof(const Instruction *I) {
2560 return I->getOpcode() == Instruction::Unwind;
2562 static inline bool classof(const Value *V) {
2563 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2566 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2567 virtual unsigned getNumSuccessorsV() const;
2568 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2571 //===----------------------------------------------------------------------===//
2572 // UnreachableInst Class
2573 //===----------------------------------------------------------------------===//
2575 //===---------------------------------------------------------------------------
2576 /// UnreachableInst - This function has undefined behavior. In particular, the
2577 /// presence of this instruction indicates some higher level knowledge that the
2578 /// end of the block cannot be reached.
2580 class UnreachableInst : public TerminatorInst {
2581 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2583 // allocate space for exactly zero operands
2584 void *operator new(size_t s) {
2585 return User::operator new(s, 0);
2587 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2588 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2590 virtual UnreachableInst *clone(LLVMContext &Context) const;
2592 unsigned getNumSuccessors() const { return 0; }
2594 // Methods for support type inquiry through isa, cast, and dyn_cast:
2595 static inline bool classof(const UnreachableInst *) { return true; }
2596 static inline bool classof(const Instruction *I) {
2597 return I->getOpcode() == Instruction::Unreachable;
2599 static inline bool classof(const Value *V) {
2600 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2603 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2604 virtual unsigned getNumSuccessorsV() const;
2605 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2608 //===----------------------------------------------------------------------===//
2610 //===----------------------------------------------------------------------===//
2612 /// @brief This class represents a truncation of integer types.
2613 class TruncInst : public CastInst {
2615 /// @brief Constructor with insert-before-instruction semantics
2617 Value *S, ///< The value to be truncated
2618 const Type *Ty, ///< The (smaller) type to truncate to
2619 const Twine &NameStr = "", ///< A name for the new instruction
2620 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2623 /// @brief Constructor with insert-at-end-of-block semantics
2625 Value *S, ///< The value to be truncated
2626 const Type *Ty, ///< The (smaller) type to truncate to
2627 const Twine &NameStr, ///< A name for the new instruction
2628 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2631 /// @brief Clone an identical TruncInst
2632 virtual TruncInst *clone(LLVMContext &Context) const;
2634 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2635 static inline bool classof(const TruncInst *) { return true; }
2636 static inline bool classof(const Instruction *I) {
2637 return I->getOpcode() == Trunc;
2639 static inline bool classof(const Value *V) {
2640 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2644 //===----------------------------------------------------------------------===//
2646 //===----------------------------------------------------------------------===//
2648 /// @brief This class represents zero extension of integer types.
2649 class ZExtInst : public CastInst {
2651 /// @brief Constructor with insert-before-instruction semantics
2653 Value *S, ///< The value to be zero extended
2654 const Type *Ty, ///< The type to zero extend to
2655 const Twine &NameStr = "", ///< A name for the new instruction
2656 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2659 /// @brief Constructor with insert-at-end semantics.
2661 Value *S, ///< The value to be zero extended
2662 const Type *Ty, ///< The type to zero extend to
2663 const Twine &NameStr, ///< A name for the new instruction
2664 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2667 /// @brief Clone an identical ZExtInst
2668 virtual ZExtInst *clone(LLVMContext &Context) const;
2670 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2671 static inline bool classof(const ZExtInst *) { return true; }
2672 static inline bool classof(const Instruction *I) {
2673 return I->getOpcode() == ZExt;
2675 static inline bool classof(const Value *V) {
2676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2680 //===----------------------------------------------------------------------===//
2682 //===----------------------------------------------------------------------===//
2684 /// @brief This class represents a sign extension of integer types.
2685 class SExtInst : public CastInst {
2687 /// @brief Constructor with insert-before-instruction semantics
2689 Value *S, ///< The value to be sign extended
2690 const Type *Ty, ///< The type to sign extend to
2691 const Twine &NameStr = "", ///< A name for the new instruction
2692 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2695 /// @brief Constructor with insert-at-end-of-block semantics
2697 Value *S, ///< The value to be sign extended
2698 const Type *Ty, ///< The type to sign extend to
2699 const Twine &NameStr, ///< A name for the new instruction
2700 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2703 /// @brief Clone an identical SExtInst
2704 virtual SExtInst *clone(LLVMContext &Context) const;
2706 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2707 static inline bool classof(const SExtInst *) { return true; }
2708 static inline bool classof(const Instruction *I) {
2709 return I->getOpcode() == SExt;
2711 static inline bool classof(const Value *V) {
2712 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2716 //===----------------------------------------------------------------------===//
2717 // FPTruncInst Class
2718 //===----------------------------------------------------------------------===//
2720 /// @brief This class represents a truncation of floating point types.
2721 class FPTruncInst : public CastInst {
2723 /// @brief Constructor with insert-before-instruction semantics
2725 Value *S, ///< The value to be truncated
2726 const Type *Ty, ///< The type to truncate to
2727 const Twine &NameStr = "", ///< A name for the new instruction
2728 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2731 /// @brief Constructor with insert-before-instruction semantics
2733 Value *S, ///< The value to be truncated
2734 const Type *Ty, ///< The type to truncate to
2735 const Twine &NameStr, ///< A name for the new instruction
2736 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2739 /// @brief Clone an identical FPTruncInst
2740 virtual FPTruncInst *clone(LLVMContext &Context) const;
2742 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2743 static inline bool classof(const FPTruncInst *) { return true; }
2744 static inline bool classof(const Instruction *I) {
2745 return I->getOpcode() == FPTrunc;
2747 static inline bool classof(const Value *V) {
2748 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2752 //===----------------------------------------------------------------------===//
2754 //===----------------------------------------------------------------------===//
2756 /// @brief This class represents an extension of floating point types.
2757 class FPExtInst : public CastInst {
2759 /// @brief Constructor with insert-before-instruction semantics
2761 Value *S, ///< The value to be extended
2762 const Type *Ty, ///< The type to extend to
2763 const Twine &NameStr = "", ///< A name for the new instruction
2764 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2767 /// @brief Constructor with insert-at-end-of-block semantics
2769 Value *S, ///< The value to be extended
2770 const Type *Ty, ///< The type to extend to
2771 const Twine &NameStr, ///< A name for the new instruction
2772 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2775 /// @brief Clone an identical FPExtInst
2776 virtual FPExtInst *clone(LLVMContext &Context) const;
2778 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2779 static inline bool classof(const FPExtInst *) { return true; }
2780 static inline bool classof(const Instruction *I) {
2781 return I->getOpcode() == FPExt;
2783 static inline bool classof(const Value *V) {
2784 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2788 //===----------------------------------------------------------------------===//
2790 //===----------------------------------------------------------------------===//
2792 /// @brief This class represents a cast unsigned integer to floating point.
2793 class UIToFPInst : public CastInst {
2795 /// @brief Constructor with insert-before-instruction semantics
2797 Value *S, ///< The value to be converted
2798 const Type *Ty, ///< The type to convert to
2799 const Twine &NameStr = "", ///< A name for the new instruction
2800 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2803 /// @brief Constructor with insert-at-end-of-block semantics
2805 Value *S, ///< The value to be converted
2806 const Type *Ty, ///< The type to convert to
2807 const Twine &NameStr, ///< A name for the new instruction
2808 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2811 /// @brief Clone an identical UIToFPInst
2812 virtual UIToFPInst *clone(LLVMContext &Context) const;
2814 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2815 static inline bool classof(const UIToFPInst *) { return true; }
2816 static inline bool classof(const Instruction *I) {
2817 return I->getOpcode() == UIToFP;
2819 static inline bool classof(const Value *V) {
2820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2824 //===----------------------------------------------------------------------===//
2826 //===----------------------------------------------------------------------===//
2828 /// @brief This class represents a cast from signed integer to floating point.
2829 class SIToFPInst : public CastInst {
2831 /// @brief Constructor with insert-before-instruction semantics
2833 Value *S, ///< The value to be converted
2834 const Type *Ty, ///< The type to convert to
2835 const Twine &NameStr = "", ///< A name for the new instruction
2836 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2839 /// @brief Constructor with insert-at-end-of-block semantics
2841 Value *S, ///< The value to be converted
2842 const Type *Ty, ///< The type to convert to
2843 const Twine &NameStr, ///< A name for the new instruction
2844 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2847 /// @brief Clone an identical SIToFPInst
2848 virtual SIToFPInst *clone(LLVMContext &Context) const;
2850 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2851 static inline bool classof(const SIToFPInst *) { return true; }
2852 static inline bool classof(const Instruction *I) {
2853 return I->getOpcode() == SIToFP;
2855 static inline bool classof(const Value *V) {
2856 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2860 //===----------------------------------------------------------------------===//
2862 //===----------------------------------------------------------------------===//
2864 /// @brief This class represents a cast from floating point to unsigned integer
2865 class FPToUIInst : public CastInst {
2867 /// @brief Constructor with insert-before-instruction semantics
2869 Value *S, ///< The value to be converted
2870 const Type *Ty, ///< The type to convert to
2871 const Twine &NameStr = "", ///< A name for the new instruction
2872 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2875 /// @brief Constructor with insert-at-end-of-block semantics
2877 Value *S, ///< The value to be converted
2878 const Type *Ty, ///< The type to convert to
2879 const Twine &NameStr, ///< A name for the new instruction
2880 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2883 /// @brief Clone an identical FPToUIInst
2884 virtual FPToUIInst *clone(LLVMContext &Context) const;
2886 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2887 static inline bool classof(const FPToUIInst *) { return true; }
2888 static inline bool classof(const Instruction *I) {
2889 return I->getOpcode() == FPToUI;
2891 static inline bool classof(const Value *V) {
2892 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2896 //===----------------------------------------------------------------------===//
2898 //===----------------------------------------------------------------------===//
2900 /// @brief This class represents a cast from floating point to signed integer.
2901 class FPToSIInst : public CastInst {
2903 /// @brief Constructor with insert-before-instruction semantics
2905 Value *S, ///< The value to be converted
2906 const Type *Ty, ///< The type to convert to
2907 const Twine &NameStr = "", ///< A name for the new instruction
2908 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2911 /// @brief Constructor with insert-at-end-of-block semantics
2913 Value *S, ///< The value to be converted
2914 const Type *Ty, ///< The type to convert to
2915 const Twine &NameStr, ///< A name for the new instruction
2916 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2919 /// @brief Clone an identical FPToSIInst
2920 virtual FPToSIInst *clone(LLVMContext &Context) const;
2922 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2923 static inline bool classof(const FPToSIInst *) { return true; }
2924 static inline bool classof(const Instruction *I) {
2925 return I->getOpcode() == FPToSI;
2927 static inline bool classof(const Value *V) {
2928 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2932 //===----------------------------------------------------------------------===//
2933 // IntToPtrInst Class
2934 //===----------------------------------------------------------------------===//
2936 /// @brief This class represents a cast from an integer to a pointer.
2937 class IntToPtrInst : public CastInst {
2939 /// @brief Constructor with insert-before-instruction semantics
2941 Value *S, ///< The value to be converted
2942 const Type *Ty, ///< The type to convert to
2943 const Twine &NameStr = "", ///< A name for the new instruction
2944 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2947 /// @brief Constructor with insert-at-end-of-block semantics
2949 Value *S, ///< The value to be converted
2950 const Type *Ty, ///< The type to convert to
2951 const Twine &NameStr, ///< A name for the new instruction
2952 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2955 /// @brief Clone an identical IntToPtrInst
2956 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2958 // Methods for support type inquiry through isa, cast, and dyn_cast:
2959 static inline bool classof(const IntToPtrInst *) { return true; }
2960 static inline bool classof(const Instruction *I) {
2961 return I->getOpcode() == IntToPtr;
2963 static inline bool classof(const Value *V) {
2964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2968 //===----------------------------------------------------------------------===//
2969 // PtrToIntInst Class
2970 //===----------------------------------------------------------------------===//
2972 /// @brief This class represents a cast from a pointer to an integer
2973 class PtrToIntInst : public CastInst {
2975 /// @brief Constructor with insert-before-instruction semantics
2977 Value *S, ///< The value to be converted
2978 const Type *Ty, ///< The type to convert to
2979 const Twine &NameStr = "", ///< A name for the new instruction
2980 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2983 /// @brief Constructor with insert-at-end-of-block semantics
2985 Value *S, ///< The value to be converted
2986 const Type *Ty, ///< The type to convert to
2987 const Twine &NameStr, ///< A name for the new instruction
2988 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2991 /// @brief Clone an identical PtrToIntInst
2992 virtual PtrToIntInst *clone(LLVMContext &Context) const;
2994 // Methods for support type inquiry through isa, cast, and dyn_cast:
2995 static inline bool classof(const PtrToIntInst *) { return true; }
2996 static inline bool classof(const Instruction *I) {
2997 return I->getOpcode() == PtrToInt;
2999 static inline bool classof(const Value *V) {
3000 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3004 //===----------------------------------------------------------------------===//
3005 // BitCastInst Class
3006 //===----------------------------------------------------------------------===//
3008 /// @brief This class represents a no-op cast from one type to another.
3009 class BitCastInst : public CastInst {
3011 /// @brief Constructor with insert-before-instruction semantics
3013 Value *S, ///< The value to be casted
3014 const Type *Ty, ///< The type to casted to
3015 const Twine &NameStr = "", ///< A name for the new instruction
3016 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3019 /// @brief Constructor with insert-at-end-of-block semantics
3021 Value *S, ///< The value to be casted
3022 const Type *Ty, ///< The type to casted to
3023 const Twine &NameStr, ///< A name for the new instruction
3024 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3027 /// @brief Clone an identical BitCastInst
3028 virtual BitCastInst *clone(LLVMContext &Context) const;
3030 // Methods for support type inquiry through isa, cast, and dyn_cast:
3031 static inline bool classof(const BitCastInst *) { return true; }
3032 static inline bool classof(const Instruction *I) {
3033 return I->getOpcode() == BitCast;
3035 static inline bool classof(const Value *V) {
3036 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3040 } // End llvm namespace