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 LLVMContext &Context, ///< Context to construct within
700 Predicate pred, ///< The predicate to use for the comparison
701 Value *LHS, ///< The left-hand-side of the expression
702 Value *RHS, ///< The right-hand-side of the expression
703 const Twine &NameStr = "" ///< Name of the instruction
704 ) : CmpInst(makeCmpResultType(LHS->getType()),
705 Instruction::ICmp, pred, LHS, RHS, NameStr) {
706 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
707 pred <= CmpInst::LAST_ICMP_PREDICATE &&
708 "Invalid ICmp predicate value");
709 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
710 "Both operands to ICmp instruction are not of the same type!");
711 // Check that the operands are the right type
712 assert((getOperand(0)->getType()->isIntOrIntVector() ||
713 isa<PointerType>(getOperand(0)->getType())) &&
714 "Invalid operand types for ICmp instruction");
717 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
718 /// @returns the predicate that would be the result if the operand were
719 /// regarded as signed.
720 /// @brief Return the signed version of the predicate
721 Predicate getSignedPredicate() const {
722 return getSignedPredicate(getPredicate());
725 /// This is a static version that you can use without an instruction.
726 /// @brief Return the signed version of the predicate.
727 static Predicate getSignedPredicate(Predicate pred);
729 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
730 /// @returns the predicate that would be the result if the operand were
731 /// regarded as unsigned.
732 /// @brief Return the unsigned version of the predicate
733 Predicate getUnsignedPredicate() const {
734 return getUnsignedPredicate(getPredicate());
737 /// This is a static version that you can use without an instruction.
738 /// @brief Return the unsigned version of the predicate.
739 static Predicate getUnsignedPredicate(Predicate pred);
741 /// isEquality - Return true if this predicate is either EQ or NE. This also
742 /// tests for commutativity.
743 static bool isEquality(Predicate P) {
744 return P == ICMP_EQ || P == ICMP_NE;
747 /// isEquality - Return true if this predicate is either EQ or NE. This also
748 /// tests for commutativity.
749 bool isEquality() const {
750 return isEquality(getPredicate());
753 /// @returns true if the predicate of this ICmpInst is commutative
754 /// @brief Determine if this relation is commutative.
755 bool isCommutative() const { return isEquality(); }
757 /// isRelational - Return true if the predicate is relational (not EQ or NE).
759 bool isRelational() const {
760 return !isEquality();
763 /// isRelational - Return true if the predicate is relational (not EQ or NE).
765 static bool isRelational(Predicate P) {
766 return !isEquality(P);
769 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
770 /// @brief Determine if this instruction's predicate is signed.
771 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
773 /// @returns true if the predicate provided is signed, false otherwise
774 /// @brief Determine if the predicate is signed.
775 static bool isSignedPredicate(Predicate pred);
777 /// @returns true if the specified compare predicate is
778 /// true when both operands are equal...
779 /// @brief Determine if the icmp is true when both operands are equal
780 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
781 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
782 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
783 pred == ICmpInst::ICMP_SLE;
786 /// @returns true if the specified compare instruction is
787 /// true when both operands are equal...
788 /// @brief Determine if the ICmpInst returns true when both operands are equal
789 bool isTrueWhenEqual() {
790 return isTrueWhenEqual(getPredicate());
793 /// Initialize a set of values that all satisfy the predicate with C.
794 /// @brief Make a ConstantRange for a relation with a constant value.
795 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
797 /// Exchange the two operands to this instruction in such a way that it does
798 /// not modify the semantics of the instruction. The predicate value may be
799 /// changed to retain the same result if the predicate is order dependent
801 /// @brief Swap operands and adjust predicate.
802 void swapOperands() {
803 SubclassData = getSwappedPredicate();
804 Op<0>().swap(Op<1>());
807 virtual ICmpInst *clone(LLVMContext &Context) const;
809 // Methods for support type inquiry through isa, cast, and dyn_cast:
810 static inline bool classof(const ICmpInst *) { return true; }
811 static inline bool classof(const Instruction *I) {
812 return I->getOpcode() == Instruction::ICmp;
814 static inline bool classof(const Value *V) {
815 return isa<Instruction>(V) && classof(cast<Instruction>(V));
820 //===----------------------------------------------------------------------===//
822 //===----------------------------------------------------------------------===//
824 /// This instruction compares its operands according to the predicate given
825 /// to the constructor. It only operates on floating point values or packed
826 /// vectors of floating point values. The operands must be identical types.
827 /// @brief Represents a floating point comparison operator.
828 class FCmpInst: public CmpInst {
830 /// @brief Constructor with insert-before-instruction semantics.
832 Instruction *InsertBefore, ///< Where to insert
833 Predicate pred, ///< The predicate to use for the comparison
834 Value *LHS, ///< The left-hand-side of the expression
835 Value *RHS, ///< The right-hand-side of the expression
836 const Twine &NameStr = "" ///< Name of the instruction
837 ) : CmpInst(makeCmpResultType(LHS->getType()),
838 Instruction::FCmp, pred, LHS, RHS, NameStr,
840 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
841 "Invalid FCmp predicate value");
842 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
843 "Both operands to FCmp instruction are not of the same type!");
844 // Check that the operands are the right type
845 assert(getOperand(0)->getType()->isFPOrFPVector() &&
846 "Invalid operand types for FCmp instruction");
849 /// @brief Constructor with insert-at-end semantics.
851 BasicBlock &InsertAtEnd, ///< Block to insert into.
852 Predicate pred, ///< The predicate to use for the comparison
853 Value *LHS, ///< The left-hand-side of the expression
854 Value *RHS, ///< The right-hand-side of the expression
855 const Twine &NameStr = "" ///< Name of the instruction
856 ) : CmpInst(makeCmpResultType(LHS->getType()),
857 Instruction::FCmp, pred, LHS, RHS, NameStr,
859 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
860 "Invalid FCmp predicate value");
861 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
862 "Both operands to FCmp instruction are not of the same type!");
863 // Check that the operands are the right type
864 assert(getOperand(0)->getType()->isFPOrFPVector() &&
865 "Invalid operand types for FCmp instruction");
868 /// @brief Constructor with no-insertion semantics
870 LLVMContext &Context, ///< Context to build in
871 Predicate pred, ///< The predicate to use for the comparison
872 Value *LHS, ///< The left-hand-side of the expression
873 Value *RHS, ///< The right-hand-side of the expression
874 const Twine &NameStr = "" ///< Name of the instruction
875 ) : CmpInst(makeCmpResultType(LHS->getType()),
876 Instruction::FCmp, pred, LHS, RHS, NameStr) {
877 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
878 "Invalid FCmp predicate value");
879 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
880 "Both operands to FCmp instruction are not of the same type!");
881 // Check that the operands are the right type
882 assert(getOperand(0)->getType()->isFPOrFPVector() &&
883 "Invalid operand types for FCmp instruction");
886 /// @returns true if the predicate of this instruction is EQ or NE.
887 /// @brief Determine if this is an equality predicate.
888 bool isEquality() const {
889 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
890 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
893 /// @returns true if the predicate of this instruction is commutative.
894 /// @brief Determine if this is a commutative predicate.
895 bool isCommutative() const {
896 return isEquality() ||
897 SubclassData == FCMP_FALSE ||
898 SubclassData == FCMP_TRUE ||
899 SubclassData == FCMP_ORD ||
900 SubclassData == FCMP_UNO;
903 /// @returns true if the predicate is relational (not EQ or NE).
904 /// @brief Determine if this a relational predicate.
905 bool isRelational() const { return !isEquality(); }
907 /// Exchange the two operands to this instruction in such a way that it does
908 /// not modify the semantics of the instruction. The predicate value may be
909 /// changed to retain the same result if the predicate is order dependent
911 /// @brief Swap operands and adjust predicate.
912 void swapOperands() {
913 SubclassData = getSwappedPredicate();
914 Op<0>().swap(Op<1>());
917 virtual FCmpInst *clone(LLVMContext &Context) const;
919 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
920 static inline bool classof(const FCmpInst *) { return true; }
921 static inline bool classof(const Instruction *I) {
922 return I->getOpcode() == Instruction::FCmp;
924 static inline bool classof(const Value *V) {
925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
929 //===----------------------------------------------------------------------===//
931 //===----------------------------------------------------------------------===//
932 /// CallInst - This class represents a function call, abstracting a target
933 /// machine's calling convention. This class uses low bit of the SubClassData
934 /// field to indicate whether or not this is a tail call. The rest of the bits
935 /// hold the calling convention of the call.
938 class CallInst : public Instruction {
939 AttrListPtr AttributeList; ///< parameter attributes for call
940 CallInst(const CallInst &CI);
941 void init(Value *Func, Value* const *Params, unsigned NumParams);
942 void init(Value *Func, Value *Actual1, Value *Actual2);
943 void init(Value *Func, Value *Actual);
944 void init(Value *Func);
946 template<typename InputIterator>
947 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
948 const Twine &NameStr,
949 // This argument ensures that we have an iterator we can
950 // do arithmetic on in constant time
951 std::random_access_iterator_tag) {
952 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
954 // This requires that the iterator points to contiguous memory.
955 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
959 /// Construct a CallInst given a range of arguments. InputIterator
960 /// must be a random-access iterator pointing to contiguous storage
961 /// (e.g. a std::vector<>::iterator). Checks are made for
962 /// random-accessness but not for contiguous storage as that would
963 /// incur runtime overhead.
964 /// @brief Construct a CallInst from a range of arguments
965 template<typename InputIterator>
966 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
967 const Twine &NameStr, Instruction *InsertBefore);
969 /// Construct a CallInst given a range of arguments. InputIterator
970 /// must be a random-access iterator pointing to contiguous storage
971 /// (e.g. a std::vector<>::iterator). Checks are made for
972 /// random-accessness but not for contiguous storage as that would
973 /// incur runtime overhead.
974 /// @brief Construct a CallInst from a range of arguments
975 template<typename InputIterator>
976 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
977 const Twine &NameStr, BasicBlock *InsertAtEnd);
979 CallInst(Value *F, Value *Actual, const Twine &NameStr,
980 Instruction *InsertBefore);
981 CallInst(Value *F, Value *Actual, const Twine &NameStr,
982 BasicBlock *InsertAtEnd);
983 explicit CallInst(Value *F, const Twine &NameStr,
984 Instruction *InsertBefore);
985 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
987 template<typename InputIterator>
988 static CallInst *Create(Value *Func,
989 InputIterator ArgBegin, InputIterator ArgEnd,
990 const Twine &NameStr = "",
991 Instruction *InsertBefore = 0) {
992 return new((unsigned)(ArgEnd - ArgBegin + 1))
993 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
995 template<typename InputIterator>
996 static CallInst *Create(Value *Func,
997 InputIterator ArgBegin, InputIterator ArgEnd,
998 const Twine &NameStr, BasicBlock *InsertAtEnd) {
999 return new((unsigned)(ArgEnd - ArgBegin + 1))
1000 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1002 static CallInst *Create(Value *F, Value *Actual,
1003 const Twine &NameStr = "",
1004 Instruction *InsertBefore = 0) {
1005 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1007 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1008 BasicBlock *InsertAtEnd) {
1009 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1011 static CallInst *Create(Value *F, const Twine &NameStr = "",
1012 Instruction *InsertBefore = 0) {
1013 return new(1) CallInst(F, NameStr, InsertBefore);
1015 static CallInst *Create(Value *F, const Twine &NameStr,
1016 BasicBlock *InsertAtEnd) {
1017 return new(1) CallInst(F, NameStr, InsertAtEnd);
1022 bool isTailCall() const { return SubclassData & 1; }
1023 void setTailCall(bool isTC = true) {
1024 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1027 virtual CallInst *clone(LLVMContext &Context) const;
1029 /// Provide fast operand accessors
1030 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1032 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1034 unsigned getCallingConv() const { return SubclassData >> 1; }
1035 void setCallingConv(unsigned CC) {
1036 SubclassData = (SubclassData & 1) | (CC << 1);
1039 /// getAttributes - Return the parameter attributes for this call.
1041 const AttrListPtr &getAttributes() const { return AttributeList; }
1043 /// setAttributes - Set the parameter attributes for this call.
1045 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1047 /// addAttribute - adds the attribute to the list of attributes.
1048 void addAttribute(unsigned i, Attributes attr);
1050 /// removeAttribute - removes the attribute from the list of attributes.
1051 void removeAttribute(unsigned i, Attributes attr);
1053 /// @brief Determine whether the call or the callee has the given attribute.
1054 bool paramHasAttr(unsigned i, Attributes attr) const;
1056 /// @brief Extract the alignment for a call or parameter (0=unknown).
1057 unsigned getParamAlignment(unsigned i) const {
1058 return AttributeList.getParamAlignment(i);
1061 /// @brief Determine if the call does not access memory.
1062 bool doesNotAccessMemory() const {
1063 return paramHasAttr(~0, Attribute::ReadNone);
1065 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1066 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1067 else removeAttribute(~0, Attribute::ReadNone);
1070 /// @brief Determine if the call does not access or only reads memory.
1071 bool onlyReadsMemory() const {
1072 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1074 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1075 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1076 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1079 /// @brief Determine if the call cannot return.
1080 bool doesNotReturn() const {
1081 return paramHasAttr(~0, Attribute::NoReturn);
1083 void setDoesNotReturn(bool DoesNotReturn = true) {
1084 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1085 else removeAttribute(~0, Attribute::NoReturn);
1088 /// @brief Determine if the call cannot unwind.
1089 bool doesNotThrow() const {
1090 return paramHasAttr(~0, Attribute::NoUnwind);
1092 void setDoesNotThrow(bool DoesNotThrow = true) {
1093 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1094 else removeAttribute(~0, Attribute::NoUnwind);
1097 /// @brief Determine if the call returns a structure through first
1098 /// pointer argument.
1099 bool hasStructRetAttr() const {
1100 // Be friendly and also check the callee.
1101 return paramHasAttr(1, Attribute::StructRet);
1104 /// @brief Determine if any call argument is an aggregate passed by value.
1105 bool hasByValArgument() const {
1106 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1109 /// getCalledFunction - Return the function called, or null if this is an
1110 /// indirect function invocation.
1112 Function *getCalledFunction() const {
1113 return dyn_cast<Function>(Op<0>());
1116 /// getCalledValue - Get a pointer to the function that is invoked by this
1118 const Value *getCalledValue() const { return Op<0>(); }
1119 Value *getCalledValue() { return Op<0>(); }
1121 // Methods for support type inquiry through isa, cast, and dyn_cast:
1122 static inline bool classof(const CallInst *) { return true; }
1123 static inline bool classof(const Instruction *I) {
1124 return I->getOpcode() == Instruction::Call;
1126 static inline bool classof(const Value *V) {
1127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1132 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1135 template<typename InputIterator>
1136 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1137 const Twine &NameStr, BasicBlock *InsertAtEnd)
1138 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1139 ->getElementType())->getReturnType(),
1141 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1142 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1143 init(Func, ArgBegin, ArgEnd, NameStr,
1144 typename std::iterator_traits<InputIterator>::iterator_category());
1147 template<typename InputIterator>
1148 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1149 const Twine &NameStr, Instruction *InsertBefore)
1150 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1151 ->getElementType())->getReturnType(),
1153 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1154 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1155 init(Func, ArgBegin, ArgEnd, NameStr,
1156 typename std::iterator_traits<InputIterator>::iterator_category());
1159 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1161 //===----------------------------------------------------------------------===//
1163 //===----------------------------------------------------------------------===//
1165 /// SelectInst - This class represents the LLVM 'select' instruction.
1167 class SelectInst : public Instruction {
1168 void init(Value *C, Value *S1, Value *S2) {
1169 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1175 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1176 Instruction *InsertBefore)
1177 : Instruction(S1->getType(), Instruction::Select,
1178 &Op<0>(), 3, InsertBefore) {
1182 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1183 BasicBlock *InsertAtEnd)
1184 : Instruction(S1->getType(), Instruction::Select,
1185 &Op<0>(), 3, InsertAtEnd) {
1190 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1191 const Twine &NameStr = "",
1192 Instruction *InsertBefore = 0) {
1193 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1195 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1196 const Twine &NameStr,
1197 BasicBlock *InsertAtEnd) {
1198 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1201 Value *getCondition() const { return Op<0>(); }
1202 Value *getTrueValue() const { return Op<1>(); }
1203 Value *getFalseValue() const { return Op<2>(); }
1205 /// areInvalidOperands - Return a string if the specified operands are invalid
1206 /// for a select operation, otherwise return null.
1207 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1209 /// Transparently provide more efficient getOperand methods.
1210 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1212 OtherOps getOpcode() const {
1213 return static_cast<OtherOps>(Instruction::getOpcode());
1216 virtual SelectInst *clone(LLVMContext &Context) const;
1218 // Methods for support type inquiry through isa, cast, and dyn_cast:
1219 static inline bool classof(const SelectInst *) { return true; }
1220 static inline bool classof(const Instruction *I) {
1221 return I->getOpcode() == Instruction::Select;
1223 static inline bool classof(const Value *V) {
1224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1229 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1232 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1234 //===----------------------------------------------------------------------===//
1236 //===----------------------------------------------------------------------===//
1238 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1239 /// an argument of the specified type given a va_list and increments that list
1241 class VAArgInst : public UnaryInstruction {
1243 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1244 Instruction *InsertBefore = 0)
1245 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1248 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1249 BasicBlock *InsertAtEnd)
1250 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1254 virtual VAArgInst *clone(LLVMContext &Context) const;
1256 // Methods for support type inquiry through isa, cast, and dyn_cast:
1257 static inline bool classof(const VAArgInst *) { return true; }
1258 static inline bool classof(const Instruction *I) {
1259 return I->getOpcode() == VAArg;
1261 static inline bool classof(const Value *V) {
1262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1266 //===----------------------------------------------------------------------===//
1267 // ExtractElementInst Class
1268 //===----------------------------------------------------------------------===//
1270 /// ExtractElementInst - This instruction extracts a single (scalar)
1271 /// element from a VectorType value
1273 class ExtractElementInst : public Instruction {
1274 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1275 Instruction *InsertBefore = 0);
1276 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1277 BasicBlock *InsertAtEnd);
1279 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1280 const Twine &NameStr = "",
1281 Instruction *InsertBefore = 0) {
1282 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1284 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1285 const Twine &NameStr,
1286 BasicBlock *InsertAtEnd) {
1287 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1290 /// isValidOperands - Return true if an extractelement instruction can be
1291 /// formed with the specified operands.
1292 static bool isValidOperands(const Value *Vec, const Value *Idx);
1294 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1296 /// Transparently provide more efficient getOperand methods.
1297 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1299 // Methods for support type inquiry through isa, cast, and dyn_cast:
1300 static inline bool classof(const ExtractElementInst *) { return true; }
1301 static inline bool classof(const Instruction *I) {
1302 return I->getOpcode() == Instruction::ExtractElement;
1304 static inline bool classof(const Value *V) {
1305 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1310 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1313 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1315 //===----------------------------------------------------------------------===//
1316 // InsertElementInst Class
1317 //===----------------------------------------------------------------------===//
1319 /// InsertElementInst - This instruction inserts a single (scalar)
1320 /// element into a VectorType value
1322 class InsertElementInst : public Instruction {
1323 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1324 const Twine &NameStr = "",
1325 Instruction *InsertBefore = 0);
1326 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1327 const Twine &NameStr, BasicBlock *InsertAtEnd);
1329 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1330 const Twine &NameStr = "",
1331 Instruction *InsertBefore = 0) {
1332 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1334 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1335 const Twine &NameStr,
1336 BasicBlock *InsertAtEnd) {
1337 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1340 /// isValidOperands - Return true if an insertelement instruction can be
1341 /// formed with the specified operands.
1342 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1345 virtual InsertElementInst *clone(LLVMContext &Context) const;
1347 /// getType - Overload to return most specific vector type.
1349 const VectorType *getType() const {
1350 return reinterpret_cast<const VectorType*>(Instruction::getType());
1353 /// Transparently provide more efficient getOperand methods.
1354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1356 // Methods for support type inquiry through isa, cast, and dyn_cast:
1357 static inline bool classof(const InsertElementInst *) { return true; }
1358 static inline bool classof(const Instruction *I) {
1359 return I->getOpcode() == Instruction::InsertElement;
1361 static inline bool classof(const Value *V) {
1362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1367 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1370 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1372 //===----------------------------------------------------------------------===//
1373 // ShuffleVectorInst Class
1374 //===----------------------------------------------------------------------===//
1376 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1379 class ShuffleVectorInst : public Instruction {
1381 // allocate space for exactly three operands
1382 void *operator new(size_t s) {
1383 return User::operator new(s, 3);
1385 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1386 const Twine &NameStr = "",
1387 Instruction *InsertBefor = 0);
1388 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1389 const Twine &NameStr, BasicBlock *InsertAtEnd);
1391 /// isValidOperands - Return true if a shufflevector instruction can be
1392 /// formed with the specified operands.
1393 static bool isValidOperands(const Value *V1, const Value *V2,
1396 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1398 /// getType - Overload to return most specific vector type.
1400 const VectorType *getType() const {
1401 return reinterpret_cast<const VectorType*>(Instruction::getType());
1404 /// Transparently provide more efficient getOperand methods.
1405 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1407 /// getMaskValue - Return the index from the shuffle mask for the specified
1408 /// output result. This is either -1 if the element is undef or a number less
1409 /// than 2*numelements.
1410 int getMaskValue(unsigned i) const;
1412 // Methods for support type inquiry through isa, cast, and dyn_cast:
1413 static inline bool classof(const ShuffleVectorInst *) { return true; }
1414 static inline bool classof(const Instruction *I) {
1415 return I->getOpcode() == Instruction::ShuffleVector;
1417 static inline bool classof(const Value *V) {
1418 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1423 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1426 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1428 //===----------------------------------------------------------------------===//
1429 // ExtractValueInst Class
1430 //===----------------------------------------------------------------------===//
1432 /// ExtractValueInst - This instruction extracts a struct member or array
1433 /// element value from an aggregate value.
1435 class ExtractValueInst : public UnaryInstruction {
1436 SmallVector<unsigned, 4> Indices;
1438 ExtractValueInst(const ExtractValueInst &EVI);
1439 void init(const unsigned *Idx, unsigned NumIdx,
1440 const Twine &NameStr);
1441 void init(unsigned Idx, const Twine &NameStr);
1443 template<typename InputIterator>
1444 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1445 const Twine &NameStr,
1446 // This argument ensures that we have an iterator we can
1447 // do arithmetic on in constant time
1448 std::random_access_iterator_tag) {
1449 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1451 // There's no fundamental reason why we require at least one index
1452 // (other than weirdness with &*IdxBegin being invalid; see
1453 // getelementptr's init routine for example). But there's no
1454 // present need to support it.
1455 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1457 // This requires that the iterator points to contiguous memory.
1458 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1459 // we have to build an array here
1462 /// getIndexedType - Returns the type of the element that would be extracted
1463 /// with an extractvalue instruction with the specified parameters.
1465 /// Null is returned if the indices are invalid for the specified
1468 static const Type *getIndexedType(const Type *Agg,
1469 const unsigned *Idx, unsigned NumIdx);
1471 template<typename InputIterator>
1472 static const Type *getIndexedType(const Type *Ptr,
1473 InputIterator IdxBegin,
1474 InputIterator IdxEnd,
1475 // This argument ensures that we
1476 // have an iterator we can do
1477 // arithmetic on in constant time
1478 std::random_access_iterator_tag) {
1479 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1482 // This requires that the iterator points to contiguous memory.
1483 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1485 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1488 /// Constructors - Create a extractvalue instruction with a base aggregate
1489 /// value and a list of indices. The first ctor can optionally insert before
1490 /// an existing instruction, the second appends the new instruction to the
1491 /// specified BasicBlock.
1492 template<typename InputIterator>
1493 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1494 InputIterator IdxEnd,
1495 const Twine &NameStr,
1496 Instruction *InsertBefore);
1497 template<typename InputIterator>
1498 inline ExtractValueInst(Value *Agg,
1499 InputIterator IdxBegin, InputIterator IdxEnd,
1500 const Twine &NameStr, BasicBlock *InsertAtEnd);
1502 // allocate space for exactly one operand
1503 void *operator new(size_t s) {
1504 return User::operator new(s, 1);
1508 template<typename InputIterator>
1509 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1510 InputIterator IdxEnd,
1511 const Twine &NameStr = "",
1512 Instruction *InsertBefore = 0) {
1514 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1516 template<typename InputIterator>
1517 static ExtractValueInst *Create(Value *Agg,
1518 InputIterator IdxBegin, InputIterator IdxEnd,
1519 const Twine &NameStr,
1520 BasicBlock *InsertAtEnd) {
1521 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1524 /// Constructors - These two creators are convenience methods because one
1525 /// index extractvalue instructions are much more common than those with
1527 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1528 const Twine &NameStr = "",
1529 Instruction *InsertBefore = 0) {
1530 unsigned Idxs[1] = { Idx };
1531 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1533 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1534 const Twine &NameStr,
1535 BasicBlock *InsertAtEnd) {
1536 unsigned Idxs[1] = { Idx };
1537 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1540 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1542 /// getIndexedType - Returns the type of the element that would be extracted
1543 /// with an extractvalue instruction with the specified parameters.
1545 /// Null is returned if the indices are invalid for the specified
1548 template<typename InputIterator>
1549 static const Type *getIndexedType(const Type *Ptr,
1550 InputIterator IdxBegin,
1551 InputIterator IdxEnd) {
1552 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1553 typename std::iterator_traits<InputIterator>::
1554 iterator_category());
1556 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1558 typedef const unsigned* idx_iterator;
1559 inline idx_iterator idx_begin() const { return Indices.begin(); }
1560 inline idx_iterator idx_end() const { return Indices.end(); }
1562 Value *getAggregateOperand() {
1563 return getOperand(0);
1565 const Value *getAggregateOperand() const {
1566 return getOperand(0);
1568 static unsigned getAggregateOperandIndex() {
1569 return 0U; // get index for modifying correct operand
1572 unsigned getNumIndices() const { // Note: always non-negative
1573 return (unsigned)Indices.size();
1576 bool hasIndices() const {
1580 // Methods for support type inquiry through isa, cast, and dyn_cast:
1581 static inline bool classof(const ExtractValueInst *) { return true; }
1582 static inline bool classof(const Instruction *I) {
1583 return I->getOpcode() == Instruction::ExtractValue;
1585 static inline bool classof(const Value *V) {
1586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1590 template<typename InputIterator>
1591 ExtractValueInst::ExtractValueInst(Value *Agg,
1592 InputIterator IdxBegin,
1593 InputIterator IdxEnd,
1594 const Twine &NameStr,
1595 Instruction *InsertBefore)
1596 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1598 ExtractValue, Agg, InsertBefore) {
1599 init(IdxBegin, IdxEnd, NameStr,
1600 typename std::iterator_traits<InputIterator>::iterator_category());
1602 template<typename InputIterator>
1603 ExtractValueInst::ExtractValueInst(Value *Agg,
1604 InputIterator IdxBegin,
1605 InputIterator IdxEnd,
1606 const Twine &NameStr,
1607 BasicBlock *InsertAtEnd)
1608 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1610 ExtractValue, Agg, InsertAtEnd) {
1611 init(IdxBegin, IdxEnd, NameStr,
1612 typename std::iterator_traits<InputIterator>::iterator_category());
1616 //===----------------------------------------------------------------------===//
1617 // InsertValueInst Class
1618 //===----------------------------------------------------------------------===//
1620 /// InsertValueInst - This instruction inserts a struct field of array element
1621 /// value into an aggregate value.
1623 class InsertValueInst : public Instruction {
1624 SmallVector<unsigned, 4> Indices;
1626 void *operator new(size_t, unsigned); // Do not implement
1627 InsertValueInst(const InsertValueInst &IVI);
1628 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1629 const Twine &NameStr);
1630 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1632 template<typename InputIterator>
1633 void init(Value *Agg, Value *Val,
1634 InputIterator IdxBegin, InputIterator IdxEnd,
1635 const Twine &NameStr,
1636 // This argument ensures that we have an iterator we can
1637 // do arithmetic on in constant time
1638 std::random_access_iterator_tag) {
1639 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1641 // There's no fundamental reason why we require at least one index
1642 // (other than weirdness with &*IdxBegin being invalid; see
1643 // getelementptr's init routine for example). But there's no
1644 // present need to support it.
1645 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1647 // This requires that the iterator points to contiguous memory.
1648 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1649 // we have to build an array here
1652 /// Constructors - Create a insertvalue instruction with a base aggregate
1653 /// value, a value to insert, and a list of indices. The first ctor can
1654 /// optionally insert before an existing instruction, the second appends
1655 /// the new instruction to the specified BasicBlock.
1656 template<typename InputIterator>
1657 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1658 InputIterator IdxEnd,
1659 const Twine &NameStr,
1660 Instruction *InsertBefore);
1661 template<typename InputIterator>
1662 inline InsertValueInst(Value *Agg, Value *Val,
1663 InputIterator IdxBegin, InputIterator IdxEnd,
1664 const Twine &NameStr, BasicBlock *InsertAtEnd);
1666 /// Constructors - These two constructors are convenience methods because one
1667 /// and two index insertvalue instructions are so common.
1668 InsertValueInst(Value *Agg, Value *Val,
1669 unsigned Idx, const Twine &NameStr = "",
1670 Instruction *InsertBefore = 0);
1671 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1672 const Twine &NameStr, BasicBlock *InsertAtEnd);
1674 // allocate space for exactly two operands
1675 void *operator new(size_t s) {
1676 return User::operator new(s, 2);
1679 template<typename InputIterator>
1680 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1681 InputIterator IdxEnd,
1682 const Twine &NameStr = "",
1683 Instruction *InsertBefore = 0) {
1684 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1685 NameStr, InsertBefore);
1687 template<typename InputIterator>
1688 static InsertValueInst *Create(Value *Agg, Value *Val,
1689 InputIterator IdxBegin, InputIterator IdxEnd,
1690 const Twine &NameStr,
1691 BasicBlock *InsertAtEnd) {
1692 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1693 NameStr, InsertAtEnd);
1696 /// Constructors - These two creators are convenience methods because one
1697 /// index insertvalue instructions are much more common than those with
1699 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1700 const Twine &NameStr = "",
1701 Instruction *InsertBefore = 0) {
1702 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1704 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1705 const Twine &NameStr,
1706 BasicBlock *InsertAtEnd) {
1707 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1710 virtual InsertValueInst *clone(LLVMContext &Context) const;
1712 /// Transparently provide more efficient getOperand methods.
1713 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1715 typedef const unsigned* idx_iterator;
1716 inline idx_iterator idx_begin() const { return Indices.begin(); }
1717 inline idx_iterator idx_end() const { return Indices.end(); }
1719 Value *getAggregateOperand() {
1720 return getOperand(0);
1722 const Value *getAggregateOperand() const {
1723 return getOperand(0);
1725 static unsigned getAggregateOperandIndex() {
1726 return 0U; // get index for modifying correct operand
1729 Value *getInsertedValueOperand() {
1730 return getOperand(1);
1732 const Value *getInsertedValueOperand() const {
1733 return getOperand(1);
1735 static unsigned getInsertedValueOperandIndex() {
1736 return 1U; // get index for modifying correct operand
1739 unsigned getNumIndices() const { // Note: always non-negative
1740 return (unsigned)Indices.size();
1743 bool hasIndices() const {
1747 // Methods for support type inquiry through isa, cast, and dyn_cast:
1748 static inline bool classof(const InsertValueInst *) { return true; }
1749 static inline bool classof(const Instruction *I) {
1750 return I->getOpcode() == Instruction::InsertValue;
1752 static inline bool classof(const Value *V) {
1753 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1758 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1761 template<typename InputIterator>
1762 InsertValueInst::InsertValueInst(Value *Agg,
1764 InputIterator IdxBegin,
1765 InputIterator IdxEnd,
1766 const Twine &NameStr,
1767 Instruction *InsertBefore)
1768 : Instruction(Agg->getType(), InsertValue,
1769 OperandTraits<InsertValueInst>::op_begin(this),
1771 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1772 typename std::iterator_traits<InputIterator>::iterator_category());
1774 template<typename InputIterator>
1775 InsertValueInst::InsertValueInst(Value *Agg,
1777 InputIterator IdxBegin,
1778 InputIterator IdxEnd,
1779 const Twine &NameStr,
1780 BasicBlock *InsertAtEnd)
1781 : Instruction(Agg->getType(), InsertValue,
1782 OperandTraits<InsertValueInst>::op_begin(this),
1784 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1785 typename std::iterator_traits<InputIterator>::iterator_category());
1788 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1790 //===----------------------------------------------------------------------===//
1792 //===----------------------------------------------------------------------===//
1794 // PHINode - The PHINode class is used to represent the magical mystical PHI
1795 // node, that can not exist in nature, but can be synthesized in a computer
1796 // scientist's overactive imagination.
1798 class PHINode : public Instruction {
1799 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1800 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1801 /// the number actually in use.
1802 unsigned ReservedSpace;
1803 PHINode(const PHINode &PN);
1804 // allocate space for exactly zero operands
1805 void *operator new(size_t s) {
1806 return User::operator new(s, 0);
1808 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1809 Instruction *InsertBefore = 0)
1810 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1815 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1816 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1821 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1822 Instruction *InsertBefore = 0) {
1823 return new PHINode(Ty, NameStr, InsertBefore);
1825 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1826 BasicBlock *InsertAtEnd) {
1827 return new PHINode(Ty, NameStr, InsertAtEnd);
1831 /// reserveOperandSpace - This method can be used to avoid repeated
1832 /// reallocation of PHI operand lists by reserving space for the correct
1833 /// number of operands before adding them. Unlike normal vector reserves,
1834 /// this method can also be used to trim the operand space.
1835 void reserveOperandSpace(unsigned NumValues) {
1836 resizeOperands(NumValues*2);
1839 virtual PHINode *clone(LLVMContext &Context) const;
1841 /// Provide fast operand accessors
1842 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1844 /// getNumIncomingValues - Return the number of incoming edges
1846 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1848 /// getIncomingValue - Return incoming value number x
1850 Value *getIncomingValue(unsigned i) const {
1851 assert(i*2 < getNumOperands() && "Invalid value number!");
1852 return getOperand(i*2);
1854 void setIncomingValue(unsigned i, Value *V) {
1855 assert(i*2 < getNumOperands() && "Invalid value number!");
1858 static unsigned getOperandNumForIncomingValue(unsigned i) {
1861 static unsigned getIncomingValueNumForOperand(unsigned i) {
1862 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1866 /// getIncomingBlock - Return incoming basic block corresponding
1867 /// to value use iterator
1869 template <typename U>
1870 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1871 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1872 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1874 /// getIncomingBlock - Return incoming basic block number x
1876 BasicBlock *getIncomingBlock(unsigned i) const {
1877 return static_cast<BasicBlock*>(getOperand(i*2+1));
1879 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1880 setOperand(i*2+1, BB);
1882 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1885 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1886 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1890 /// addIncoming - Add an incoming value to the end of the PHI list
1892 void addIncoming(Value *V, BasicBlock *BB) {
1893 assert(V && "PHI node got a null value!");
1894 assert(BB && "PHI node got a null basic block!");
1895 assert(getType() == V->getType() &&
1896 "All operands to PHI node must be the same type as the PHI node!");
1897 unsigned OpNo = NumOperands;
1898 if (OpNo+2 > ReservedSpace)
1899 resizeOperands(0); // Get more space!
1900 // Initialize some new operands.
1901 NumOperands = OpNo+2;
1902 OperandList[OpNo] = V;
1903 OperandList[OpNo+1] = BB;
1906 /// removeIncomingValue - Remove an incoming value. This is useful if a
1907 /// predecessor basic block is deleted. The value removed is returned.
1909 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1910 /// is true), the PHI node is destroyed and any uses of it are replaced with
1911 /// dummy values. The only time there should be zero incoming values to a PHI
1912 /// node is when the block is dead, so this strategy is sound.
1914 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1916 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1917 int Idx = getBasicBlockIndex(BB);
1918 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1919 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1922 /// getBasicBlockIndex - Return the first index of the specified basic
1923 /// block in the value list for this PHI. Returns -1 if no instance.
1925 int getBasicBlockIndex(const BasicBlock *BB) const {
1926 Use *OL = OperandList;
1927 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1928 if (OL[i+1].get() == BB) return i/2;
1932 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1933 return getIncomingValue(getBasicBlockIndex(BB));
1936 /// hasConstantValue - If the specified PHI node always merges together the
1937 /// same value, return the value, otherwise return null.
1939 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1941 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1942 static inline bool classof(const PHINode *) { return true; }
1943 static inline bool classof(const Instruction *I) {
1944 return I->getOpcode() == Instruction::PHI;
1946 static inline bool classof(const Value *V) {
1947 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1950 void resizeOperands(unsigned NumOperands);
1954 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1957 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1960 //===----------------------------------------------------------------------===//
1962 //===----------------------------------------------------------------------===//
1964 //===---------------------------------------------------------------------------
1965 /// ReturnInst - Return a value (possibly void), from a function. Execution
1966 /// does not continue in this function any longer.
1968 class ReturnInst : public TerminatorInst {
1969 ReturnInst(const ReturnInst &RI);
1972 // ReturnInst constructors:
1973 // ReturnInst() - 'ret void' instruction
1974 // ReturnInst( null) - 'ret void' instruction
1975 // ReturnInst(Value* X) - 'ret X' instruction
1976 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1977 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1978 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1979 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1981 // NOTE: If the Value* passed is of type void then the constructor behaves as
1982 // if it was passed NULL.
1983 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1984 Instruction *InsertBefore = 0);
1985 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1986 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1988 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1989 Instruction *InsertBefore = 0) {
1990 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1992 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1993 BasicBlock *InsertAtEnd) {
1994 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1996 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1997 return new(0) ReturnInst(C, InsertAtEnd);
1999 virtual ~ReturnInst();
2001 virtual ReturnInst *clone(LLVMContext &Context) const;
2003 /// Provide fast operand accessors
2004 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2006 /// Convenience accessor
2007 Value *getReturnValue(unsigned n = 0) const {
2008 return n < getNumOperands()
2013 unsigned getNumSuccessors() const { return 0; }
2015 // Methods for support type inquiry through isa, cast, and dyn_cast:
2016 static inline bool classof(const ReturnInst *) { return true; }
2017 static inline bool classof(const Instruction *I) {
2018 return (I->getOpcode() == Instruction::Ret);
2020 static inline bool classof(const Value *V) {
2021 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2024 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2025 virtual unsigned getNumSuccessorsV() const;
2026 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2030 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2033 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2035 //===----------------------------------------------------------------------===//
2037 //===----------------------------------------------------------------------===//
2039 //===---------------------------------------------------------------------------
2040 /// BranchInst - Conditional or Unconditional Branch instruction.
2042 class BranchInst : public TerminatorInst {
2043 /// Ops list - Branches are strange. The operands are ordered:
2044 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2045 /// they don't have to check for cond/uncond branchness. These are mostly
2046 /// accessed relative from op_end().
2047 BranchInst(const BranchInst &BI);
2049 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2050 // BranchInst(BB *B) - 'br B'
2051 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2052 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2053 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2054 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2055 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2056 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2057 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2058 Instruction *InsertBefore = 0);
2059 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2060 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2061 BasicBlock *InsertAtEnd);
2063 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2064 return new(1, true) BranchInst(IfTrue, InsertBefore);
2066 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2067 Value *Cond, Instruction *InsertBefore = 0) {
2068 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2070 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2071 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2073 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2074 Value *Cond, BasicBlock *InsertAtEnd) {
2075 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2080 /// Transparently provide more efficient getOperand methods.
2081 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2083 virtual BranchInst *clone(LLVMContext &Context) const;
2085 bool isUnconditional() const { return getNumOperands() == 1; }
2086 bool isConditional() const { return getNumOperands() == 3; }
2088 Value *getCondition() const {
2089 assert(isConditional() && "Cannot get condition of an uncond branch!");
2093 void setCondition(Value *V) {
2094 assert(isConditional() && "Cannot set condition of unconditional branch!");
2098 // setUnconditionalDest - Change the current branch to an unconditional branch
2099 // targeting the specified block.
2100 // FIXME: Eliminate this ugly method.
2101 void setUnconditionalDest(BasicBlock *Dest) {
2103 if (isConditional()) { // Convert this to an uncond branch.
2107 OperandList = op_begin();
2111 unsigned getNumSuccessors() const { return 1+isConditional(); }
2113 BasicBlock *getSuccessor(unsigned i) const {
2114 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2115 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2118 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2119 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2120 *(&Op<-1>() - idx) = NewSucc;
2123 // Methods for support type inquiry through isa, cast, and dyn_cast:
2124 static inline bool classof(const BranchInst *) { return true; }
2125 static inline bool classof(const Instruction *I) {
2126 return (I->getOpcode() == Instruction::Br);
2128 static inline bool classof(const Value *V) {
2129 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2132 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2133 virtual unsigned getNumSuccessorsV() const;
2134 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2138 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2140 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2142 //===----------------------------------------------------------------------===//
2144 //===----------------------------------------------------------------------===//
2146 //===---------------------------------------------------------------------------
2147 /// SwitchInst - Multiway switch
2149 class SwitchInst : public TerminatorInst {
2150 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2151 unsigned ReservedSpace;
2152 // Operand[0] = Value to switch on
2153 // Operand[1] = Default basic block destination
2154 // Operand[2n ] = Value to match
2155 // Operand[2n+1] = BasicBlock to go to on match
2156 SwitchInst(const SwitchInst &RI);
2157 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2158 void resizeOperands(unsigned No);
2159 // allocate space for exactly zero operands
2160 void *operator new(size_t s) {
2161 return User::operator new(s, 0);
2163 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2164 /// switch on and a default destination. The number of additional cases can
2165 /// be specified here to make memory allocation more efficient. This
2166 /// constructor can also autoinsert before another instruction.
2167 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2168 Instruction *InsertBefore = 0);
2170 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2171 /// switch on and a default destination. The number of additional cases can
2172 /// be specified here to make memory allocation more efficient. This
2173 /// constructor also autoinserts at the end of the specified BasicBlock.
2174 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2175 BasicBlock *InsertAtEnd);
2177 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2178 unsigned NumCases, Instruction *InsertBefore = 0) {
2179 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2181 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2182 unsigned NumCases, BasicBlock *InsertAtEnd) {
2183 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2187 /// Provide fast operand accessors
2188 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2190 // Accessor Methods for Switch stmt
2191 Value *getCondition() const { return getOperand(0); }
2192 void setCondition(Value *V) { setOperand(0, V); }
2194 BasicBlock *getDefaultDest() const {
2195 return cast<BasicBlock>(getOperand(1));
2198 /// getNumCases - return the number of 'cases' in this switch instruction.
2199 /// Note that case #0 is always the default case.
2200 unsigned getNumCases() const {
2201 return getNumOperands()/2;
2204 /// getCaseValue - Return the specified case value. Note that case #0, the
2205 /// default destination, does not have a case value.
2206 ConstantInt *getCaseValue(unsigned i) {
2207 assert(i && i < getNumCases() && "Illegal case value to get!");
2208 return getSuccessorValue(i);
2211 /// getCaseValue - Return the specified case value. Note that case #0, the
2212 /// default destination, does not have a case value.
2213 const ConstantInt *getCaseValue(unsigned i) const {
2214 assert(i && i < getNumCases() && "Illegal case value to get!");
2215 return getSuccessorValue(i);
2218 /// findCaseValue - Search all of the case values for the specified constant.
2219 /// If it is explicitly handled, return the case number of it, otherwise
2220 /// return 0 to indicate that it is handled by the default handler.
2221 unsigned findCaseValue(const ConstantInt *C) const {
2222 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2223 if (getCaseValue(i) == C)
2228 /// findCaseDest - Finds the unique case value for a given successor. Returns
2229 /// null if the successor is not found, not unique, or is the default case.
2230 ConstantInt *findCaseDest(BasicBlock *BB) {
2231 if (BB == getDefaultDest()) return NULL;
2233 ConstantInt *CI = NULL;
2234 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2235 if (getSuccessor(i) == BB) {
2236 if (CI) return NULL; // Multiple cases lead to BB.
2237 else CI = getCaseValue(i);
2243 /// addCase - Add an entry to the switch instruction...
2245 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2247 /// removeCase - This method removes the specified successor from the switch
2248 /// instruction. Note that this cannot be used to remove the default
2249 /// destination (successor #0).
2251 void removeCase(unsigned idx);
2253 virtual SwitchInst *clone(LLVMContext &Context) const;
2255 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2256 BasicBlock *getSuccessor(unsigned idx) const {
2257 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2258 return cast<BasicBlock>(getOperand(idx*2+1));
2260 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2261 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2262 setOperand(idx*2+1, NewSucc);
2265 // getSuccessorValue - Return the value associated with the specified
2267 ConstantInt *getSuccessorValue(unsigned idx) const {
2268 assert(idx < getNumSuccessors() && "Successor # out of range!");
2269 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2272 // Methods for support type inquiry through isa, cast, and dyn_cast:
2273 static inline bool classof(const SwitchInst *) { return true; }
2274 static inline bool classof(const Instruction *I) {
2275 return I->getOpcode() == Instruction::Switch;
2277 static inline bool classof(const Value *V) {
2278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2281 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2282 virtual unsigned getNumSuccessorsV() const;
2283 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2287 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2290 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2293 //===----------------------------------------------------------------------===//
2295 //===----------------------------------------------------------------------===//
2297 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2298 /// calling convention of the call.
2300 class InvokeInst : public TerminatorInst {
2301 AttrListPtr AttributeList;
2302 InvokeInst(const InvokeInst &BI);
2303 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2304 Value* const *Args, unsigned NumArgs);
2306 template<typename InputIterator>
2307 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2308 InputIterator ArgBegin, InputIterator ArgEnd,
2309 const Twine &NameStr,
2310 // This argument ensures that we have an iterator we can
2311 // do arithmetic on in constant time
2312 std::random_access_iterator_tag) {
2313 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2315 // This requires that the iterator points to contiguous memory.
2316 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2320 /// Construct an InvokeInst given a range of arguments.
2321 /// InputIterator must be a random-access iterator pointing to
2322 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2323 /// made for random-accessness but not for contiguous storage as
2324 /// that would incur runtime overhead.
2326 /// @brief Construct an InvokeInst from a range of arguments
2327 template<typename InputIterator>
2328 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2329 InputIterator ArgBegin, InputIterator ArgEnd,
2331 const Twine &NameStr, Instruction *InsertBefore);
2333 /// Construct an InvokeInst given a range of arguments.
2334 /// InputIterator must be a random-access iterator pointing to
2335 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2336 /// made for random-accessness but not for contiguous storage as
2337 /// that would incur runtime overhead.
2339 /// @brief Construct an InvokeInst from a range of arguments
2340 template<typename InputIterator>
2341 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2342 InputIterator ArgBegin, InputIterator ArgEnd,
2344 const Twine &NameStr, BasicBlock *InsertAtEnd);
2346 template<typename InputIterator>
2347 static InvokeInst *Create(Value *Func,
2348 BasicBlock *IfNormal, BasicBlock *IfException,
2349 InputIterator ArgBegin, InputIterator ArgEnd,
2350 const Twine &NameStr = "",
2351 Instruction *InsertBefore = 0) {
2352 unsigned Values(ArgEnd - ArgBegin + 3);
2353 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2354 Values, NameStr, InsertBefore);
2356 template<typename InputIterator>
2357 static InvokeInst *Create(Value *Func,
2358 BasicBlock *IfNormal, BasicBlock *IfException,
2359 InputIterator ArgBegin, InputIterator ArgEnd,
2360 const Twine &NameStr,
2361 BasicBlock *InsertAtEnd) {
2362 unsigned Values(ArgEnd - ArgBegin + 3);
2363 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2364 Values, NameStr, InsertAtEnd);
2367 virtual InvokeInst *clone(LLVMContext &Context) const;
2369 /// Provide fast operand accessors
2370 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2372 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2374 unsigned getCallingConv() const { return SubclassData; }
2375 void setCallingConv(unsigned CC) {
2379 /// getAttributes - Return the parameter attributes for this invoke.
2381 const AttrListPtr &getAttributes() const { return AttributeList; }
2383 /// setAttributes - Set the parameter attributes for this invoke.
2385 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2387 /// addAttribute - adds the attribute to the list of attributes.
2388 void addAttribute(unsigned i, Attributes attr);
2390 /// removeAttribute - removes the attribute from the list of attributes.
2391 void removeAttribute(unsigned i, Attributes attr);
2393 /// @brief Determine whether the call or the callee has the given attribute.
2394 bool paramHasAttr(unsigned i, Attributes attr) const;
2396 /// @brief Extract the alignment for a call or parameter (0=unknown).
2397 unsigned getParamAlignment(unsigned i) const {
2398 return AttributeList.getParamAlignment(i);
2401 /// @brief Determine if the call does not access memory.
2402 bool doesNotAccessMemory() const {
2403 return paramHasAttr(~0, Attribute::ReadNone);
2405 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2406 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2407 else removeAttribute(~0, Attribute::ReadNone);
2410 /// @brief Determine if the call does not access or only reads memory.
2411 bool onlyReadsMemory() const {
2412 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2414 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2415 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2416 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2419 /// @brief Determine if the call cannot return.
2420 bool doesNotReturn() const {
2421 return paramHasAttr(~0, Attribute::NoReturn);
2423 void setDoesNotReturn(bool DoesNotReturn = true) {
2424 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2425 else removeAttribute(~0, Attribute::NoReturn);
2428 /// @brief Determine if the call cannot unwind.
2429 bool doesNotThrow() const {
2430 return paramHasAttr(~0, Attribute::NoUnwind);
2432 void setDoesNotThrow(bool DoesNotThrow = true) {
2433 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2434 else removeAttribute(~0, Attribute::NoUnwind);
2437 /// @brief Determine if the call returns a structure through first
2438 /// pointer argument.
2439 bool hasStructRetAttr() const {
2440 // Be friendly and also check the callee.
2441 return paramHasAttr(1, Attribute::StructRet);
2444 /// @brief Determine if any call argument is an aggregate passed by value.
2445 bool hasByValArgument() const {
2446 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2449 /// getCalledFunction - Return the function called, or null if this is an
2450 /// indirect function invocation.
2452 Function *getCalledFunction() const {
2453 return dyn_cast<Function>(getOperand(0));
2456 /// getCalledValue - Get a pointer to the function that is invoked by this
2458 const Value *getCalledValue() const { return getOperand(0); }
2459 Value *getCalledValue() { return getOperand(0); }
2461 // get*Dest - Return the destination basic blocks...
2462 BasicBlock *getNormalDest() const {
2463 return cast<BasicBlock>(getOperand(1));
2465 BasicBlock *getUnwindDest() const {
2466 return cast<BasicBlock>(getOperand(2));
2468 void setNormalDest(BasicBlock *B) {
2472 void setUnwindDest(BasicBlock *B) {
2476 BasicBlock *getSuccessor(unsigned i) const {
2477 assert(i < 2 && "Successor # out of range for invoke!");
2478 return i == 0 ? getNormalDest() : getUnwindDest();
2481 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2482 assert(idx < 2 && "Successor # out of range for invoke!");
2483 setOperand(idx+1, NewSucc);
2486 unsigned getNumSuccessors() const { return 2; }
2488 // Methods for support type inquiry through isa, cast, and dyn_cast:
2489 static inline bool classof(const InvokeInst *) { return true; }
2490 static inline bool classof(const Instruction *I) {
2491 return (I->getOpcode() == Instruction::Invoke);
2493 static inline bool classof(const Value *V) {
2494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2497 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2498 virtual unsigned getNumSuccessorsV() const;
2499 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2503 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2506 template<typename InputIterator>
2507 InvokeInst::InvokeInst(Value *Func,
2508 BasicBlock *IfNormal, BasicBlock *IfException,
2509 InputIterator ArgBegin, InputIterator ArgEnd,
2511 const Twine &NameStr, Instruction *InsertBefore)
2512 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2513 ->getElementType())->getReturnType(),
2514 Instruction::Invoke,
2515 OperandTraits<InvokeInst>::op_end(this) - Values,
2516 Values, InsertBefore) {
2517 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2518 typename std::iterator_traits<InputIterator>::iterator_category());
2520 template<typename InputIterator>
2521 InvokeInst::InvokeInst(Value *Func,
2522 BasicBlock *IfNormal, BasicBlock *IfException,
2523 InputIterator ArgBegin, InputIterator ArgEnd,
2525 const Twine &NameStr, BasicBlock *InsertAtEnd)
2526 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2527 ->getElementType())->getReturnType(),
2528 Instruction::Invoke,
2529 OperandTraits<InvokeInst>::op_end(this) - Values,
2530 Values, InsertAtEnd) {
2531 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2532 typename std::iterator_traits<InputIterator>::iterator_category());
2535 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2537 //===----------------------------------------------------------------------===//
2539 //===----------------------------------------------------------------------===//
2541 //===---------------------------------------------------------------------------
2542 /// UnwindInst - Immediately exit the current function, unwinding the stack
2543 /// until an invoke instruction is found.
2545 class UnwindInst : public TerminatorInst {
2546 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2548 // allocate space for exactly zero operands
2549 void *operator new(size_t s) {
2550 return User::operator new(s, 0);
2552 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2553 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2555 virtual UnwindInst *clone(LLVMContext &Context) const;
2557 unsigned getNumSuccessors() const { return 0; }
2559 // Methods for support type inquiry through isa, cast, and dyn_cast:
2560 static inline bool classof(const UnwindInst *) { return true; }
2561 static inline bool classof(const Instruction *I) {
2562 return I->getOpcode() == Instruction::Unwind;
2564 static inline bool classof(const Value *V) {
2565 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2568 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2569 virtual unsigned getNumSuccessorsV() const;
2570 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2573 //===----------------------------------------------------------------------===//
2574 // UnreachableInst Class
2575 //===----------------------------------------------------------------------===//
2577 //===---------------------------------------------------------------------------
2578 /// UnreachableInst - This function has undefined behavior. In particular, the
2579 /// presence of this instruction indicates some higher level knowledge that the
2580 /// end of the block cannot be reached.
2582 class UnreachableInst : public TerminatorInst {
2583 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2585 // allocate space for exactly zero operands
2586 void *operator new(size_t s) {
2587 return User::operator new(s, 0);
2589 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2590 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2592 virtual UnreachableInst *clone(LLVMContext &Context) const;
2594 unsigned getNumSuccessors() const { return 0; }
2596 // Methods for support type inquiry through isa, cast, and dyn_cast:
2597 static inline bool classof(const UnreachableInst *) { return true; }
2598 static inline bool classof(const Instruction *I) {
2599 return I->getOpcode() == Instruction::Unreachable;
2601 static inline bool classof(const Value *V) {
2602 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2605 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2606 virtual unsigned getNumSuccessorsV() const;
2607 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2610 //===----------------------------------------------------------------------===//
2612 //===----------------------------------------------------------------------===//
2614 /// @brief This class represents a truncation of integer types.
2615 class TruncInst : public CastInst {
2617 /// @brief Constructor with insert-before-instruction semantics
2619 Value *S, ///< The value to be truncated
2620 const Type *Ty, ///< The (smaller) type to truncate to
2621 const Twine &NameStr = "", ///< A name for the new instruction
2622 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2625 /// @brief Constructor with insert-at-end-of-block semantics
2627 Value *S, ///< The value to be truncated
2628 const Type *Ty, ///< The (smaller) type to truncate to
2629 const Twine &NameStr, ///< A name for the new instruction
2630 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2633 /// @brief Clone an identical TruncInst
2634 virtual TruncInst *clone(LLVMContext &Context) const;
2636 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2637 static inline bool classof(const TruncInst *) { return true; }
2638 static inline bool classof(const Instruction *I) {
2639 return I->getOpcode() == Trunc;
2641 static inline bool classof(const Value *V) {
2642 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2646 //===----------------------------------------------------------------------===//
2648 //===----------------------------------------------------------------------===//
2650 /// @brief This class represents zero extension of integer types.
2651 class ZExtInst : public CastInst {
2653 /// @brief Constructor with insert-before-instruction semantics
2655 Value *S, ///< The value to be zero extended
2656 const Type *Ty, ///< The type to zero extend to
2657 const Twine &NameStr = "", ///< A name for the new instruction
2658 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2661 /// @brief Constructor with insert-at-end semantics.
2663 Value *S, ///< The value to be zero extended
2664 const Type *Ty, ///< The type to zero extend to
2665 const Twine &NameStr, ///< A name for the new instruction
2666 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2669 /// @brief Clone an identical ZExtInst
2670 virtual ZExtInst *clone(LLVMContext &Context) const;
2672 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2673 static inline bool classof(const ZExtInst *) { return true; }
2674 static inline bool classof(const Instruction *I) {
2675 return I->getOpcode() == ZExt;
2677 static inline bool classof(const Value *V) {
2678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2682 //===----------------------------------------------------------------------===//
2684 //===----------------------------------------------------------------------===//
2686 /// @brief This class represents a sign extension of integer types.
2687 class SExtInst : public CastInst {
2689 /// @brief Constructor with insert-before-instruction semantics
2691 Value *S, ///< The value to be sign extended
2692 const Type *Ty, ///< The type to sign extend to
2693 const Twine &NameStr = "", ///< A name for the new instruction
2694 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2697 /// @brief Constructor with insert-at-end-of-block semantics
2699 Value *S, ///< The value to be sign extended
2700 const Type *Ty, ///< The type to sign extend to
2701 const Twine &NameStr, ///< A name for the new instruction
2702 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2705 /// @brief Clone an identical SExtInst
2706 virtual SExtInst *clone(LLVMContext &Context) const;
2708 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2709 static inline bool classof(const SExtInst *) { return true; }
2710 static inline bool classof(const Instruction *I) {
2711 return I->getOpcode() == SExt;
2713 static inline bool classof(const Value *V) {
2714 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2718 //===----------------------------------------------------------------------===//
2719 // FPTruncInst Class
2720 //===----------------------------------------------------------------------===//
2722 /// @brief This class represents a truncation of floating point types.
2723 class FPTruncInst : public CastInst {
2725 /// @brief Constructor with insert-before-instruction semantics
2727 Value *S, ///< The value to be truncated
2728 const Type *Ty, ///< The type to truncate to
2729 const Twine &NameStr = "", ///< A name for the new instruction
2730 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2733 /// @brief Constructor with insert-before-instruction semantics
2735 Value *S, ///< The value to be truncated
2736 const Type *Ty, ///< The type to truncate to
2737 const Twine &NameStr, ///< A name for the new instruction
2738 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2741 /// @brief Clone an identical FPTruncInst
2742 virtual FPTruncInst *clone(LLVMContext &Context) const;
2744 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2745 static inline bool classof(const FPTruncInst *) { return true; }
2746 static inline bool classof(const Instruction *I) {
2747 return I->getOpcode() == FPTrunc;
2749 static inline bool classof(const Value *V) {
2750 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2754 //===----------------------------------------------------------------------===//
2756 //===----------------------------------------------------------------------===//
2758 /// @brief This class represents an extension of floating point types.
2759 class FPExtInst : public CastInst {
2761 /// @brief Constructor with insert-before-instruction semantics
2763 Value *S, ///< The value to be extended
2764 const Type *Ty, ///< The type to extend to
2765 const Twine &NameStr = "", ///< A name for the new instruction
2766 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2769 /// @brief Constructor with insert-at-end-of-block semantics
2771 Value *S, ///< The value to be extended
2772 const Type *Ty, ///< The type to extend to
2773 const Twine &NameStr, ///< A name for the new instruction
2774 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2777 /// @brief Clone an identical FPExtInst
2778 virtual FPExtInst *clone(LLVMContext &Context) const;
2780 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2781 static inline bool classof(const FPExtInst *) { return true; }
2782 static inline bool classof(const Instruction *I) {
2783 return I->getOpcode() == FPExt;
2785 static inline bool classof(const Value *V) {
2786 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2790 //===----------------------------------------------------------------------===//
2792 //===----------------------------------------------------------------------===//
2794 /// @brief This class represents a cast unsigned integer to floating point.
2795 class UIToFPInst : public CastInst {
2797 /// @brief Constructor with insert-before-instruction semantics
2799 Value *S, ///< The value to be converted
2800 const Type *Ty, ///< The type to convert to
2801 const Twine &NameStr = "", ///< A name for the new instruction
2802 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2805 /// @brief Constructor with insert-at-end-of-block semantics
2807 Value *S, ///< The value to be converted
2808 const Type *Ty, ///< The type to convert to
2809 const Twine &NameStr, ///< A name for the new instruction
2810 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2813 /// @brief Clone an identical UIToFPInst
2814 virtual UIToFPInst *clone(LLVMContext &Context) const;
2816 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2817 static inline bool classof(const UIToFPInst *) { return true; }
2818 static inline bool classof(const Instruction *I) {
2819 return I->getOpcode() == UIToFP;
2821 static inline bool classof(const Value *V) {
2822 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2826 //===----------------------------------------------------------------------===//
2828 //===----------------------------------------------------------------------===//
2830 /// @brief This class represents a cast from signed integer to floating point.
2831 class SIToFPInst : public CastInst {
2833 /// @brief Constructor with insert-before-instruction semantics
2835 Value *S, ///< The value to be converted
2836 const Type *Ty, ///< The type to convert to
2837 const Twine &NameStr = "", ///< A name for the new instruction
2838 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2841 /// @brief Constructor with insert-at-end-of-block semantics
2843 Value *S, ///< The value to be converted
2844 const Type *Ty, ///< The type to convert to
2845 const Twine &NameStr, ///< A name for the new instruction
2846 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2849 /// @brief Clone an identical SIToFPInst
2850 virtual SIToFPInst *clone(LLVMContext &Context) const;
2852 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2853 static inline bool classof(const SIToFPInst *) { return true; }
2854 static inline bool classof(const Instruction *I) {
2855 return I->getOpcode() == SIToFP;
2857 static inline bool classof(const Value *V) {
2858 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2862 //===----------------------------------------------------------------------===//
2864 //===----------------------------------------------------------------------===//
2866 /// @brief This class represents a cast from floating point to unsigned integer
2867 class FPToUIInst : public CastInst {
2869 /// @brief Constructor with insert-before-instruction semantics
2871 Value *S, ///< The value to be converted
2872 const Type *Ty, ///< The type to convert to
2873 const Twine &NameStr = "", ///< A name for the new instruction
2874 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2877 /// @brief Constructor with insert-at-end-of-block semantics
2879 Value *S, ///< The value to be converted
2880 const Type *Ty, ///< The type to convert to
2881 const Twine &NameStr, ///< A name for the new instruction
2882 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2885 /// @brief Clone an identical FPToUIInst
2886 virtual FPToUIInst *clone(LLVMContext &Context) const;
2888 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2889 static inline bool classof(const FPToUIInst *) { return true; }
2890 static inline bool classof(const Instruction *I) {
2891 return I->getOpcode() == FPToUI;
2893 static inline bool classof(const Value *V) {
2894 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2898 //===----------------------------------------------------------------------===//
2900 //===----------------------------------------------------------------------===//
2902 /// @brief This class represents a cast from floating point to signed integer.
2903 class FPToSIInst : public CastInst {
2905 /// @brief Constructor with insert-before-instruction semantics
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
2909 const Twine &NameStr = "", ///< A name for the new instruction
2910 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2913 /// @brief Constructor with insert-at-end-of-block semantics
2915 Value *S, ///< The value to be converted
2916 const Type *Ty, ///< The type to convert to
2917 const Twine &NameStr, ///< A name for the new instruction
2918 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2921 /// @brief Clone an identical FPToSIInst
2922 virtual FPToSIInst *clone(LLVMContext &Context) const;
2924 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2925 static inline bool classof(const FPToSIInst *) { return true; }
2926 static inline bool classof(const Instruction *I) {
2927 return I->getOpcode() == FPToSI;
2929 static inline bool classof(const Value *V) {
2930 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2934 //===----------------------------------------------------------------------===//
2935 // IntToPtrInst Class
2936 //===----------------------------------------------------------------------===//
2938 /// @brief This class represents a cast from an integer to a pointer.
2939 class IntToPtrInst : public CastInst {
2941 /// @brief Constructor with insert-before-instruction semantics
2943 Value *S, ///< The value to be converted
2944 const Type *Ty, ///< The type to convert to
2945 const Twine &NameStr = "", ///< A name for the new instruction
2946 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2949 /// @brief Constructor with insert-at-end-of-block semantics
2951 Value *S, ///< The value to be converted
2952 const Type *Ty, ///< The type to convert to
2953 const Twine &NameStr, ///< A name for the new instruction
2954 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2957 /// @brief Clone an identical IntToPtrInst
2958 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2960 // Methods for support type inquiry through isa, cast, and dyn_cast:
2961 static inline bool classof(const IntToPtrInst *) { return true; }
2962 static inline bool classof(const Instruction *I) {
2963 return I->getOpcode() == IntToPtr;
2965 static inline bool classof(const Value *V) {
2966 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2970 //===----------------------------------------------------------------------===//
2971 // PtrToIntInst Class
2972 //===----------------------------------------------------------------------===//
2974 /// @brief This class represents a cast from a pointer to an integer
2975 class PtrToIntInst : public CastInst {
2977 /// @brief Constructor with insert-before-instruction semantics
2979 Value *S, ///< The value to be converted
2980 const Type *Ty, ///< The type to convert to
2981 const Twine &NameStr = "", ///< A name for the new instruction
2982 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2985 /// @brief Constructor with insert-at-end-of-block semantics
2987 Value *S, ///< The value to be converted
2988 const Type *Ty, ///< The type to convert to
2989 const Twine &NameStr, ///< A name for the new instruction
2990 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2993 /// @brief Clone an identical PtrToIntInst
2994 virtual PtrToIntInst *clone(LLVMContext &Context) const;
2996 // Methods for support type inquiry through isa, cast, and dyn_cast:
2997 static inline bool classof(const PtrToIntInst *) { return true; }
2998 static inline bool classof(const Instruction *I) {
2999 return I->getOpcode() == PtrToInt;
3001 static inline bool classof(const Value *V) {
3002 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3006 //===----------------------------------------------------------------------===//
3007 // BitCastInst Class
3008 //===----------------------------------------------------------------------===//
3010 /// @brief This class represents a no-op cast from one type to another.
3011 class BitCastInst : public CastInst {
3013 /// @brief Constructor with insert-before-instruction semantics
3015 Value *S, ///< The value to be casted
3016 const Type *Ty, ///< The type to casted to
3017 const Twine &NameStr = "", ///< A name for the new instruction
3018 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3021 /// @brief Constructor with insert-at-end-of-block semantics
3023 Value *S, ///< The value to be casted
3024 const Type *Ty, ///< The type to casted to
3025 const Twine &NameStr, ///< A name for the new instruction
3026 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3029 /// @brief Clone an identical BitCastInst
3030 virtual BitCastInst *clone(LLVMContext &Context) const;
3032 // Methods for support type inquiry through isa, cast, and dyn_cast:
3033 static inline bool classof(const BitCastInst *) { return true; }
3034 static inline bool classof(const Instruction *I) {
3035 return I->getOpcode() == BitCast;
3037 static inline bool classof(const Value *V) {
3038 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3042 } // End llvm namespace