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 Instruction *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(const ExtractElementInst &EE) {
1280 return Create(EE.getOperand(0), EE.getOperand(1));
1283 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1284 const Twine &NameStr = "",
1285 Instruction *InsertBefore = 0) {
1286 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1288 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1289 const Twine &NameStr,
1290 BasicBlock *InsertAtEnd) {
1291 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1294 /// isValidOperands - Return true if an extractelement instruction can be
1295 /// formed with the specified operands.
1296 static bool isValidOperands(const Value *Vec, const Value *Idx);
1298 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1300 /// Transparently provide more efficient getOperand methods.
1301 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1303 // Methods for support type inquiry through isa, cast, and dyn_cast:
1304 static inline bool classof(const ExtractElementInst *) { return true; }
1305 static inline bool classof(const Instruction *I) {
1306 return I->getOpcode() == Instruction::ExtractElement;
1308 static inline bool classof(const Value *V) {
1309 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1314 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1317 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1319 //===----------------------------------------------------------------------===//
1320 // InsertElementInst Class
1321 //===----------------------------------------------------------------------===//
1323 /// InsertElementInst - This instruction inserts a single (scalar)
1324 /// element into a VectorType value
1326 class InsertElementInst : public Instruction {
1327 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1328 const Twine &NameStr = "",
1329 Instruction *InsertBefore = 0);
1330 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1331 const Twine &NameStr, BasicBlock *InsertAtEnd);
1333 static InsertElementInst *Create(const InsertElementInst &IE) {
1334 return Create(IE.getOperand(0), IE.getOperand(1), IE.getOperand(2));
1336 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1337 const Twine &NameStr = "",
1338 Instruction *InsertBefore = 0) {
1339 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1341 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1342 const Twine &NameStr,
1343 BasicBlock *InsertAtEnd) {
1344 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1347 /// isValidOperands - Return true if an insertelement instruction can be
1348 /// formed with the specified operands.
1349 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1352 virtual InsertElementInst *clone(LLVMContext &Context) const;
1354 /// getType - Overload to return most specific vector type.
1356 const VectorType *getType() const {
1357 return reinterpret_cast<const VectorType*>(Instruction::getType());
1360 /// Transparently provide more efficient getOperand methods.
1361 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1363 // Methods for support type inquiry through isa, cast, and dyn_cast:
1364 static inline bool classof(const InsertElementInst *) { return true; }
1365 static inline bool classof(const Instruction *I) {
1366 return I->getOpcode() == Instruction::InsertElement;
1368 static inline bool classof(const Value *V) {
1369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1374 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1377 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1379 //===----------------------------------------------------------------------===//
1380 // ShuffleVectorInst Class
1381 //===----------------------------------------------------------------------===//
1383 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1386 class ShuffleVectorInst : public Instruction {
1388 // allocate space for exactly three operands
1389 void *operator new(size_t s) {
1390 return User::operator new(s, 3);
1392 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1393 const Twine &NameStr = "",
1394 Instruction *InsertBefor = 0);
1395 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1396 const Twine &NameStr, BasicBlock *InsertAtEnd);
1398 /// isValidOperands - Return true if a shufflevector instruction can be
1399 /// formed with the specified operands.
1400 static bool isValidOperands(const Value *V1, const Value *V2,
1403 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1405 /// getType - Overload to return most specific vector type.
1407 const VectorType *getType() const {
1408 return reinterpret_cast<const VectorType*>(Instruction::getType());
1411 /// Transparently provide more efficient getOperand methods.
1412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1414 /// getMaskValue - Return the index from the shuffle mask for the specified
1415 /// output result. This is either -1 if the element is undef or a number less
1416 /// than 2*numelements.
1417 int getMaskValue(unsigned i) const;
1419 // Methods for support type inquiry through isa, cast, and dyn_cast:
1420 static inline bool classof(const ShuffleVectorInst *) { return true; }
1421 static inline bool classof(const Instruction *I) {
1422 return I->getOpcode() == Instruction::ShuffleVector;
1424 static inline bool classof(const Value *V) {
1425 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1430 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1433 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1435 //===----------------------------------------------------------------------===//
1436 // ExtractValueInst Class
1437 //===----------------------------------------------------------------------===//
1439 /// ExtractValueInst - This instruction extracts a struct member or array
1440 /// element value from an aggregate value.
1442 class ExtractValueInst : public UnaryInstruction {
1443 SmallVector<unsigned, 4> Indices;
1445 ExtractValueInst(const ExtractValueInst &EVI);
1446 void init(const unsigned *Idx, unsigned NumIdx,
1447 const Twine &NameStr);
1448 void init(unsigned Idx, const Twine &NameStr);
1450 template<typename InputIterator>
1451 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1452 const Twine &NameStr,
1453 // This argument ensures that we have an iterator we can
1454 // do arithmetic on in constant time
1455 std::random_access_iterator_tag) {
1456 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1458 // There's no fundamental reason why we require at least one index
1459 // (other than weirdness with &*IdxBegin being invalid; see
1460 // getelementptr's init routine for example). But there's no
1461 // present need to support it.
1462 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1464 // This requires that the iterator points to contiguous memory.
1465 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1466 // we have to build an array here
1469 /// getIndexedType - Returns the type of the element that would be extracted
1470 /// with an extractvalue instruction with the specified parameters.
1472 /// Null is returned if the indices are invalid for the specified
1475 static const Type *getIndexedType(const Type *Agg,
1476 const unsigned *Idx, unsigned NumIdx);
1478 template<typename InputIterator>
1479 static const Type *getIndexedType(const Type *Ptr,
1480 InputIterator IdxBegin,
1481 InputIterator IdxEnd,
1482 // This argument ensures that we
1483 // have an iterator we can do
1484 // arithmetic on in constant time
1485 std::random_access_iterator_tag) {
1486 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1489 // This requires that the iterator points to contiguous memory.
1490 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1492 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1495 /// Constructors - Create a extractvalue instruction with a base aggregate
1496 /// value and a list of indices. The first ctor can optionally insert before
1497 /// an existing instruction, the second appends the new instruction to the
1498 /// specified BasicBlock.
1499 template<typename InputIterator>
1500 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1501 InputIterator IdxEnd,
1502 const Twine &NameStr,
1503 Instruction *InsertBefore);
1504 template<typename InputIterator>
1505 inline ExtractValueInst(Value *Agg,
1506 InputIterator IdxBegin, InputIterator IdxEnd,
1507 const Twine &NameStr, BasicBlock *InsertAtEnd);
1509 // allocate space for exactly one operand
1510 void *operator new(size_t s) {
1511 return User::operator new(s, 1);
1515 template<typename InputIterator>
1516 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1517 InputIterator IdxEnd,
1518 const Twine &NameStr = "",
1519 Instruction *InsertBefore = 0) {
1521 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1523 template<typename InputIterator>
1524 static ExtractValueInst *Create(Value *Agg,
1525 InputIterator IdxBegin, InputIterator IdxEnd,
1526 const Twine &NameStr,
1527 BasicBlock *InsertAtEnd) {
1528 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1531 /// Constructors - These two creators are convenience methods because one
1532 /// index extractvalue instructions are much more common than those with
1534 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1535 const Twine &NameStr = "",
1536 Instruction *InsertBefore = 0) {
1537 unsigned Idxs[1] = { Idx };
1538 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1540 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1541 const Twine &NameStr,
1542 BasicBlock *InsertAtEnd) {
1543 unsigned Idxs[1] = { Idx };
1544 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1547 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1549 /// getIndexedType - Returns the type of the element that would be extracted
1550 /// with an extractvalue instruction with the specified parameters.
1552 /// Null is returned if the indices are invalid for the specified
1555 template<typename InputIterator>
1556 static const Type *getIndexedType(const Type *Ptr,
1557 InputIterator IdxBegin,
1558 InputIterator IdxEnd) {
1559 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1560 typename std::iterator_traits<InputIterator>::
1561 iterator_category());
1563 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1565 typedef const unsigned* idx_iterator;
1566 inline idx_iterator idx_begin() const { return Indices.begin(); }
1567 inline idx_iterator idx_end() const { return Indices.end(); }
1569 Value *getAggregateOperand() {
1570 return getOperand(0);
1572 const Value *getAggregateOperand() const {
1573 return getOperand(0);
1575 static unsigned getAggregateOperandIndex() {
1576 return 0U; // get index for modifying correct operand
1579 unsigned getNumIndices() const { // Note: always non-negative
1580 return (unsigned)Indices.size();
1583 bool hasIndices() const {
1587 // Methods for support type inquiry through isa, cast, and dyn_cast:
1588 static inline bool classof(const ExtractValueInst *) { return true; }
1589 static inline bool classof(const Instruction *I) {
1590 return I->getOpcode() == Instruction::ExtractValue;
1592 static inline bool classof(const Value *V) {
1593 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1597 template<typename InputIterator>
1598 ExtractValueInst::ExtractValueInst(Value *Agg,
1599 InputIterator IdxBegin,
1600 InputIterator IdxEnd,
1601 const Twine &NameStr,
1602 Instruction *InsertBefore)
1603 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1605 ExtractValue, Agg, InsertBefore) {
1606 init(IdxBegin, IdxEnd, NameStr,
1607 typename std::iterator_traits<InputIterator>::iterator_category());
1609 template<typename InputIterator>
1610 ExtractValueInst::ExtractValueInst(Value *Agg,
1611 InputIterator IdxBegin,
1612 InputIterator IdxEnd,
1613 const Twine &NameStr,
1614 BasicBlock *InsertAtEnd)
1615 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1617 ExtractValue, Agg, InsertAtEnd) {
1618 init(IdxBegin, IdxEnd, NameStr,
1619 typename std::iterator_traits<InputIterator>::iterator_category());
1623 //===----------------------------------------------------------------------===//
1624 // InsertValueInst Class
1625 //===----------------------------------------------------------------------===//
1627 /// InsertValueInst - This instruction inserts a struct field of array element
1628 /// value into an aggregate value.
1630 class InsertValueInst : public Instruction {
1631 SmallVector<unsigned, 4> Indices;
1633 void *operator new(size_t, unsigned); // Do not implement
1634 InsertValueInst(const InsertValueInst &IVI);
1635 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1636 const Twine &NameStr);
1637 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1639 template<typename InputIterator>
1640 void init(Value *Agg, Value *Val,
1641 InputIterator IdxBegin, InputIterator IdxEnd,
1642 const Twine &NameStr,
1643 // This argument ensures that we have an iterator we can
1644 // do arithmetic on in constant time
1645 std::random_access_iterator_tag) {
1646 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1648 // There's no fundamental reason why we require at least one index
1649 // (other than weirdness with &*IdxBegin being invalid; see
1650 // getelementptr's init routine for example). But there's no
1651 // present need to support it.
1652 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1654 // This requires that the iterator points to contiguous memory.
1655 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1656 // we have to build an array here
1659 /// Constructors - Create a insertvalue instruction with a base aggregate
1660 /// value, a value to insert, and a list of indices. The first ctor can
1661 /// optionally insert before an existing instruction, the second appends
1662 /// the new instruction to the specified BasicBlock.
1663 template<typename InputIterator>
1664 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1665 InputIterator IdxEnd,
1666 const Twine &NameStr,
1667 Instruction *InsertBefore);
1668 template<typename InputIterator>
1669 inline InsertValueInst(Value *Agg, Value *Val,
1670 InputIterator IdxBegin, InputIterator IdxEnd,
1671 const Twine &NameStr, BasicBlock *InsertAtEnd);
1673 /// Constructors - These two constructors are convenience methods because one
1674 /// and two index insertvalue instructions are so common.
1675 InsertValueInst(Value *Agg, Value *Val,
1676 unsigned Idx, const Twine &NameStr = "",
1677 Instruction *InsertBefore = 0);
1678 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1679 const Twine &NameStr, BasicBlock *InsertAtEnd);
1681 // allocate space for exactly two operands
1682 void *operator new(size_t s) {
1683 return User::operator new(s, 2);
1686 template<typename InputIterator>
1687 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1688 InputIterator IdxEnd,
1689 const Twine &NameStr = "",
1690 Instruction *InsertBefore = 0) {
1691 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1692 NameStr, InsertBefore);
1694 template<typename InputIterator>
1695 static InsertValueInst *Create(Value *Agg, Value *Val,
1696 InputIterator IdxBegin, InputIterator IdxEnd,
1697 const Twine &NameStr,
1698 BasicBlock *InsertAtEnd) {
1699 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1700 NameStr, InsertAtEnd);
1703 /// Constructors - These two creators are convenience methods because one
1704 /// index insertvalue instructions are much more common than those with
1706 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1707 const Twine &NameStr = "",
1708 Instruction *InsertBefore = 0) {
1709 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1711 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1712 const Twine &NameStr,
1713 BasicBlock *InsertAtEnd) {
1714 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1717 virtual InsertValueInst *clone(LLVMContext &Context) const;
1719 /// Transparently provide more efficient getOperand methods.
1720 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1722 typedef const unsigned* idx_iterator;
1723 inline idx_iterator idx_begin() const { return Indices.begin(); }
1724 inline idx_iterator idx_end() const { return Indices.end(); }
1726 Value *getAggregateOperand() {
1727 return getOperand(0);
1729 const Value *getAggregateOperand() const {
1730 return getOperand(0);
1732 static unsigned getAggregateOperandIndex() {
1733 return 0U; // get index for modifying correct operand
1736 Value *getInsertedValueOperand() {
1737 return getOperand(1);
1739 const Value *getInsertedValueOperand() const {
1740 return getOperand(1);
1742 static unsigned getInsertedValueOperandIndex() {
1743 return 1U; // get index for modifying correct operand
1746 unsigned getNumIndices() const { // Note: always non-negative
1747 return (unsigned)Indices.size();
1750 bool hasIndices() const {
1754 // Methods for support type inquiry through isa, cast, and dyn_cast:
1755 static inline bool classof(const InsertValueInst *) { return true; }
1756 static inline bool classof(const Instruction *I) {
1757 return I->getOpcode() == Instruction::InsertValue;
1759 static inline bool classof(const Value *V) {
1760 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1765 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1768 template<typename InputIterator>
1769 InsertValueInst::InsertValueInst(Value *Agg,
1771 InputIterator IdxBegin,
1772 InputIterator IdxEnd,
1773 const Twine &NameStr,
1774 Instruction *InsertBefore)
1775 : Instruction(Agg->getType(), InsertValue,
1776 OperandTraits<InsertValueInst>::op_begin(this),
1778 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1779 typename std::iterator_traits<InputIterator>::iterator_category());
1781 template<typename InputIterator>
1782 InsertValueInst::InsertValueInst(Value *Agg,
1784 InputIterator IdxBegin,
1785 InputIterator IdxEnd,
1786 const Twine &NameStr,
1787 BasicBlock *InsertAtEnd)
1788 : Instruction(Agg->getType(), InsertValue,
1789 OperandTraits<InsertValueInst>::op_begin(this),
1791 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1792 typename std::iterator_traits<InputIterator>::iterator_category());
1795 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1797 //===----------------------------------------------------------------------===//
1799 //===----------------------------------------------------------------------===//
1801 // PHINode - The PHINode class is used to represent the magical mystical PHI
1802 // node, that can not exist in nature, but can be synthesized in a computer
1803 // scientist's overactive imagination.
1805 class PHINode : public Instruction {
1806 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1807 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1808 /// the number actually in use.
1809 unsigned ReservedSpace;
1810 PHINode(const PHINode &PN);
1811 // allocate space for exactly zero operands
1812 void *operator new(size_t s) {
1813 return User::operator new(s, 0);
1815 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1816 Instruction *InsertBefore = 0)
1817 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1822 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1823 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1828 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1829 Instruction *InsertBefore = 0) {
1830 return new PHINode(Ty, NameStr, InsertBefore);
1832 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1833 BasicBlock *InsertAtEnd) {
1834 return new PHINode(Ty, NameStr, InsertAtEnd);
1838 /// reserveOperandSpace - This method can be used to avoid repeated
1839 /// reallocation of PHI operand lists by reserving space for the correct
1840 /// number of operands before adding them. Unlike normal vector reserves,
1841 /// this method can also be used to trim the operand space.
1842 void reserveOperandSpace(unsigned NumValues) {
1843 resizeOperands(NumValues*2);
1846 virtual PHINode *clone(LLVMContext &Context) const;
1848 /// Provide fast operand accessors
1849 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1851 /// getNumIncomingValues - Return the number of incoming edges
1853 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1855 /// getIncomingValue - Return incoming value number x
1857 Value *getIncomingValue(unsigned i) const {
1858 assert(i*2 < getNumOperands() && "Invalid value number!");
1859 return getOperand(i*2);
1861 void setIncomingValue(unsigned i, Value *V) {
1862 assert(i*2 < getNumOperands() && "Invalid value number!");
1865 static unsigned getOperandNumForIncomingValue(unsigned i) {
1868 static unsigned getIncomingValueNumForOperand(unsigned i) {
1869 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1873 /// getIncomingBlock - Return incoming basic block corresponding
1874 /// to value use iterator
1876 template <typename U>
1877 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1878 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1879 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1881 /// getIncomingBlock - Return incoming basic block number x
1883 BasicBlock *getIncomingBlock(unsigned i) const {
1884 return static_cast<BasicBlock*>(getOperand(i*2+1));
1886 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1887 setOperand(i*2+1, BB);
1889 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1892 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1893 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1897 /// addIncoming - Add an incoming value to the end of the PHI list
1899 void addIncoming(Value *V, BasicBlock *BB) {
1900 assert(V && "PHI node got a null value!");
1901 assert(BB && "PHI node got a null basic block!");
1902 assert(getType() == V->getType() &&
1903 "All operands to PHI node must be the same type as the PHI node!");
1904 unsigned OpNo = NumOperands;
1905 if (OpNo+2 > ReservedSpace)
1906 resizeOperands(0); // Get more space!
1907 // Initialize some new operands.
1908 NumOperands = OpNo+2;
1909 OperandList[OpNo] = V;
1910 OperandList[OpNo+1] = BB;
1913 /// removeIncomingValue - Remove an incoming value. This is useful if a
1914 /// predecessor basic block is deleted. The value removed is returned.
1916 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1917 /// is true), the PHI node is destroyed and any uses of it are replaced with
1918 /// dummy values. The only time there should be zero incoming values to a PHI
1919 /// node is when the block is dead, so this strategy is sound.
1921 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1923 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1924 int Idx = getBasicBlockIndex(BB);
1925 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1926 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1929 /// getBasicBlockIndex - Return the first index of the specified basic
1930 /// block in the value list for this PHI. Returns -1 if no instance.
1932 int getBasicBlockIndex(const BasicBlock *BB) const {
1933 Use *OL = OperandList;
1934 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1935 if (OL[i+1].get() == BB) return i/2;
1939 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1940 return getIncomingValue(getBasicBlockIndex(BB));
1943 /// hasConstantValue - If the specified PHI node always merges together the
1944 /// same value, return the value, otherwise return null.
1946 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1948 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1949 static inline bool classof(const PHINode *) { return true; }
1950 static inline bool classof(const Instruction *I) {
1951 return I->getOpcode() == Instruction::PHI;
1953 static inline bool classof(const Value *V) {
1954 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1957 void resizeOperands(unsigned NumOperands);
1961 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1964 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1967 //===----------------------------------------------------------------------===//
1969 //===----------------------------------------------------------------------===//
1971 //===---------------------------------------------------------------------------
1972 /// ReturnInst - Return a value (possibly void), from a function. Execution
1973 /// does not continue in this function any longer.
1975 class ReturnInst : public TerminatorInst {
1976 ReturnInst(const ReturnInst &RI);
1979 // ReturnInst constructors:
1980 // ReturnInst() - 'ret void' instruction
1981 // ReturnInst( null) - 'ret void' instruction
1982 // ReturnInst(Value* X) - 'ret X' instruction
1983 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1984 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1985 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1986 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1988 // NOTE: If the Value* passed is of type void then the constructor behaves as
1989 // if it was passed NULL.
1990 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1991 Instruction *InsertBefore = 0);
1992 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1993 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1995 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1996 Instruction *InsertBefore = 0) {
1997 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1999 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2000 BasicBlock *InsertAtEnd) {
2001 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2003 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2004 return new(0) ReturnInst(C, InsertAtEnd);
2006 virtual ~ReturnInst();
2008 virtual ReturnInst *clone(LLVMContext &Context) const;
2010 /// Provide fast operand accessors
2011 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2013 /// Convenience accessor
2014 Value *getReturnValue(unsigned n = 0) const {
2015 return n < getNumOperands()
2020 unsigned getNumSuccessors() const { return 0; }
2022 // Methods for support type inquiry through isa, cast, and dyn_cast:
2023 static inline bool classof(const ReturnInst *) { return true; }
2024 static inline bool classof(const Instruction *I) {
2025 return (I->getOpcode() == Instruction::Ret);
2027 static inline bool classof(const Value *V) {
2028 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2031 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2032 virtual unsigned getNumSuccessorsV() const;
2033 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2037 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2040 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2042 //===----------------------------------------------------------------------===//
2044 //===----------------------------------------------------------------------===//
2046 //===---------------------------------------------------------------------------
2047 /// BranchInst - Conditional or Unconditional Branch instruction.
2049 class BranchInst : public TerminatorInst {
2050 /// Ops list - Branches are strange. The operands are ordered:
2051 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2052 /// they don't have to check for cond/uncond branchness. These are mostly
2053 /// accessed relative from op_end().
2054 BranchInst(const BranchInst &BI);
2056 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2057 // BranchInst(BB *B) - 'br B'
2058 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2059 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2060 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2061 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2062 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2063 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2064 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2065 Instruction *InsertBefore = 0);
2066 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2067 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2068 BasicBlock *InsertAtEnd);
2070 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2071 return new(1, true) BranchInst(IfTrue, InsertBefore);
2073 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2074 Value *Cond, Instruction *InsertBefore = 0) {
2075 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2077 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2078 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2080 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2081 Value *Cond, BasicBlock *InsertAtEnd) {
2082 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2087 /// Transparently provide more efficient getOperand methods.
2088 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2090 virtual BranchInst *clone(LLVMContext &Context) const;
2092 bool isUnconditional() const { return getNumOperands() == 1; }
2093 bool isConditional() const { return getNumOperands() == 3; }
2095 Value *getCondition() const {
2096 assert(isConditional() && "Cannot get condition of an uncond branch!");
2100 void setCondition(Value *V) {
2101 assert(isConditional() && "Cannot set condition of unconditional branch!");
2105 // setUnconditionalDest - Change the current branch to an unconditional branch
2106 // targeting the specified block.
2107 // FIXME: Eliminate this ugly method.
2108 void setUnconditionalDest(BasicBlock *Dest) {
2110 if (isConditional()) { // Convert this to an uncond branch.
2114 OperandList = op_begin();
2118 unsigned getNumSuccessors() const { return 1+isConditional(); }
2120 BasicBlock *getSuccessor(unsigned i) const {
2121 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2122 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2125 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2126 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2127 *(&Op<-1>() - idx) = NewSucc;
2130 // Methods for support type inquiry through isa, cast, and dyn_cast:
2131 static inline bool classof(const BranchInst *) { return true; }
2132 static inline bool classof(const Instruction *I) {
2133 return (I->getOpcode() == Instruction::Br);
2135 static inline bool classof(const Value *V) {
2136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2139 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2140 virtual unsigned getNumSuccessorsV() const;
2141 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2145 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2147 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2149 //===----------------------------------------------------------------------===//
2151 //===----------------------------------------------------------------------===//
2153 //===---------------------------------------------------------------------------
2154 /// SwitchInst - Multiway switch
2156 class SwitchInst : public TerminatorInst {
2157 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2158 unsigned ReservedSpace;
2159 // Operand[0] = Value to switch on
2160 // Operand[1] = Default basic block destination
2161 // Operand[2n ] = Value to match
2162 // Operand[2n+1] = BasicBlock to go to on match
2163 SwitchInst(const SwitchInst &RI);
2164 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2165 void resizeOperands(unsigned No);
2166 // allocate space for exactly zero operands
2167 void *operator new(size_t s) {
2168 return User::operator new(s, 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 can also autoinsert before another instruction.
2174 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2175 Instruction *InsertBefore = 0);
2177 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2178 /// switch on and a default destination. The number of additional cases can
2179 /// be specified here to make memory allocation more efficient. This
2180 /// constructor also autoinserts at the end of the specified BasicBlock.
2181 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2182 BasicBlock *InsertAtEnd);
2184 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2185 unsigned NumCases, Instruction *InsertBefore = 0) {
2186 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2188 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2189 unsigned NumCases, BasicBlock *InsertAtEnd) {
2190 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2194 /// Provide fast operand accessors
2195 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2197 // Accessor Methods for Switch stmt
2198 Value *getCondition() const { return getOperand(0); }
2199 void setCondition(Value *V) { setOperand(0, V); }
2201 BasicBlock *getDefaultDest() const {
2202 return cast<BasicBlock>(getOperand(1));
2205 /// getNumCases - return the number of 'cases' in this switch instruction.
2206 /// Note that case #0 is always the default case.
2207 unsigned getNumCases() const {
2208 return getNumOperands()/2;
2211 /// getCaseValue - Return the specified case value. Note that case #0, the
2212 /// default destination, does not have a case value.
2213 ConstantInt *getCaseValue(unsigned i) {
2214 assert(i && i < getNumCases() && "Illegal case value to get!");
2215 return getSuccessorValue(i);
2218 /// getCaseValue - Return the specified case value. Note that case #0, the
2219 /// default destination, does not have a case value.
2220 const ConstantInt *getCaseValue(unsigned i) const {
2221 assert(i && i < getNumCases() && "Illegal case value to get!");
2222 return getSuccessorValue(i);
2225 /// findCaseValue - Search all of the case values for the specified constant.
2226 /// If it is explicitly handled, return the case number of it, otherwise
2227 /// return 0 to indicate that it is handled by the default handler.
2228 unsigned findCaseValue(const ConstantInt *C) const {
2229 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2230 if (getCaseValue(i) == C)
2235 /// findCaseDest - Finds the unique case value for a given successor. Returns
2236 /// null if the successor is not found, not unique, or is the default case.
2237 ConstantInt *findCaseDest(BasicBlock *BB) {
2238 if (BB == getDefaultDest()) return NULL;
2240 ConstantInt *CI = NULL;
2241 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2242 if (getSuccessor(i) == BB) {
2243 if (CI) return NULL; // Multiple cases lead to BB.
2244 else CI = getCaseValue(i);
2250 /// addCase - Add an entry to the switch instruction...
2252 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2254 /// removeCase - This method removes the specified successor from the switch
2255 /// instruction. Note that this cannot be used to remove the default
2256 /// destination (successor #0).
2258 void removeCase(unsigned idx);
2260 virtual SwitchInst *clone(LLVMContext &Context) const;
2262 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2263 BasicBlock *getSuccessor(unsigned idx) const {
2264 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2265 return cast<BasicBlock>(getOperand(idx*2+1));
2267 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2268 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2269 setOperand(idx*2+1, NewSucc);
2272 // getSuccessorValue - Return the value associated with the specified
2274 ConstantInt *getSuccessorValue(unsigned idx) const {
2275 assert(idx < getNumSuccessors() && "Successor # out of range!");
2276 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2279 // Methods for support type inquiry through isa, cast, and dyn_cast:
2280 static inline bool classof(const SwitchInst *) { return true; }
2281 static inline bool classof(const Instruction *I) {
2282 return I->getOpcode() == Instruction::Switch;
2284 static inline bool classof(const Value *V) {
2285 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2288 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2289 virtual unsigned getNumSuccessorsV() const;
2290 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2294 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2297 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2300 //===----------------------------------------------------------------------===//
2302 //===----------------------------------------------------------------------===//
2304 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2305 /// calling convention of the call.
2307 class InvokeInst : public TerminatorInst {
2308 AttrListPtr AttributeList;
2309 InvokeInst(const InvokeInst &BI);
2310 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2311 Value* const *Args, unsigned NumArgs);
2313 template<typename InputIterator>
2314 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2315 InputIterator ArgBegin, InputIterator ArgEnd,
2316 const Twine &NameStr,
2317 // This argument ensures that we have an iterator we can
2318 // do arithmetic on in constant time
2319 std::random_access_iterator_tag) {
2320 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2322 // This requires that the iterator points to contiguous memory.
2323 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2327 /// Construct an InvokeInst given a range of arguments.
2328 /// InputIterator must be a random-access iterator pointing to
2329 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2330 /// made for random-accessness but not for contiguous storage as
2331 /// that would incur runtime overhead.
2333 /// @brief Construct an InvokeInst from a range of arguments
2334 template<typename InputIterator>
2335 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2336 InputIterator ArgBegin, InputIterator ArgEnd,
2338 const Twine &NameStr, Instruction *InsertBefore);
2340 /// Construct an InvokeInst given a range of arguments.
2341 /// InputIterator must be a random-access iterator pointing to
2342 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2343 /// made for random-accessness but not for contiguous storage as
2344 /// that would incur runtime overhead.
2346 /// @brief Construct an InvokeInst from a range of arguments
2347 template<typename InputIterator>
2348 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2349 InputIterator ArgBegin, InputIterator ArgEnd,
2351 const Twine &NameStr, BasicBlock *InsertAtEnd);
2353 template<typename InputIterator>
2354 static InvokeInst *Create(Value *Func,
2355 BasicBlock *IfNormal, BasicBlock *IfException,
2356 InputIterator ArgBegin, InputIterator ArgEnd,
2357 const Twine &NameStr = "",
2358 Instruction *InsertBefore = 0) {
2359 unsigned Values(ArgEnd - ArgBegin + 3);
2360 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2361 Values, NameStr, InsertBefore);
2363 template<typename InputIterator>
2364 static InvokeInst *Create(Value *Func,
2365 BasicBlock *IfNormal, BasicBlock *IfException,
2366 InputIterator ArgBegin, InputIterator ArgEnd,
2367 const Twine &NameStr,
2368 BasicBlock *InsertAtEnd) {
2369 unsigned Values(ArgEnd - ArgBegin + 3);
2370 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2371 Values, NameStr, InsertAtEnd);
2374 virtual InvokeInst *clone(LLVMContext &Context) const;
2376 /// Provide fast operand accessors
2377 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2379 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2381 unsigned getCallingConv() const { return SubclassData; }
2382 void setCallingConv(unsigned CC) {
2386 /// getAttributes - Return the parameter attributes for this invoke.
2388 const AttrListPtr &getAttributes() const { return AttributeList; }
2390 /// setAttributes - Set the parameter attributes for this invoke.
2392 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2394 /// addAttribute - adds the attribute to the list of attributes.
2395 void addAttribute(unsigned i, Attributes attr);
2397 /// removeAttribute - removes the attribute from the list of attributes.
2398 void removeAttribute(unsigned i, Attributes attr);
2400 /// @brief Determine whether the call or the callee has the given attribute.
2401 bool paramHasAttr(unsigned i, Attributes attr) const;
2403 /// @brief Extract the alignment for a call or parameter (0=unknown).
2404 unsigned getParamAlignment(unsigned i) const {
2405 return AttributeList.getParamAlignment(i);
2408 /// @brief Determine if the call does not access memory.
2409 bool doesNotAccessMemory() const {
2410 return paramHasAttr(~0, Attribute::ReadNone);
2412 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2413 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2414 else removeAttribute(~0, Attribute::ReadNone);
2417 /// @brief Determine if the call does not access or only reads memory.
2418 bool onlyReadsMemory() const {
2419 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2421 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2422 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2423 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2426 /// @brief Determine if the call cannot return.
2427 bool doesNotReturn() const {
2428 return paramHasAttr(~0, Attribute::NoReturn);
2430 void setDoesNotReturn(bool DoesNotReturn = true) {
2431 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2432 else removeAttribute(~0, Attribute::NoReturn);
2435 /// @brief Determine if the call cannot unwind.
2436 bool doesNotThrow() const {
2437 return paramHasAttr(~0, Attribute::NoUnwind);
2439 void setDoesNotThrow(bool DoesNotThrow = true) {
2440 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2441 else removeAttribute(~0, Attribute::NoUnwind);
2444 /// @brief Determine if the call returns a structure through first
2445 /// pointer argument.
2446 bool hasStructRetAttr() const {
2447 // Be friendly and also check the callee.
2448 return paramHasAttr(1, Attribute::StructRet);
2451 /// @brief Determine if any call argument is an aggregate passed by value.
2452 bool hasByValArgument() const {
2453 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2456 /// getCalledFunction - Return the function called, or null if this is an
2457 /// indirect function invocation.
2459 Function *getCalledFunction() const {
2460 return dyn_cast<Function>(getOperand(0));
2463 /// getCalledValue - Get a pointer to the function that is invoked by this
2465 const Value *getCalledValue() const { return getOperand(0); }
2466 Value *getCalledValue() { return getOperand(0); }
2468 // get*Dest - Return the destination basic blocks...
2469 BasicBlock *getNormalDest() const {
2470 return cast<BasicBlock>(getOperand(1));
2472 BasicBlock *getUnwindDest() const {
2473 return cast<BasicBlock>(getOperand(2));
2475 void setNormalDest(BasicBlock *B) {
2479 void setUnwindDest(BasicBlock *B) {
2483 BasicBlock *getSuccessor(unsigned i) const {
2484 assert(i < 2 && "Successor # out of range for invoke!");
2485 return i == 0 ? getNormalDest() : getUnwindDest();
2488 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2489 assert(idx < 2 && "Successor # out of range for invoke!");
2490 setOperand(idx+1, NewSucc);
2493 unsigned getNumSuccessors() const { return 2; }
2495 // Methods for support type inquiry through isa, cast, and dyn_cast:
2496 static inline bool classof(const InvokeInst *) { return true; }
2497 static inline bool classof(const Instruction *I) {
2498 return (I->getOpcode() == Instruction::Invoke);
2500 static inline bool classof(const Value *V) {
2501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2504 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2505 virtual unsigned getNumSuccessorsV() const;
2506 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2510 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2513 template<typename InputIterator>
2514 InvokeInst::InvokeInst(Value *Func,
2515 BasicBlock *IfNormal, BasicBlock *IfException,
2516 InputIterator ArgBegin, InputIterator ArgEnd,
2518 const Twine &NameStr, Instruction *InsertBefore)
2519 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2520 ->getElementType())->getReturnType(),
2521 Instruction::Invoke,
2522 OperandTraits<InvokeInst>::op_end(this) - Values,
2523 Values, InsertBefore) {
2524 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2525 typename std::iterator_traits<InputIterator>::iterator_category());
2527 template<typename InputIterator>
2528 InvokeInst::InvokeInst(Value *Func,
2529 BasicBlock *IfNormal, BasicBlock *IfException,
2530 InputIterator ArgBegin, InputIterator ArgEnd,
2532 const Twine &NameStr, BasicBlock *InsertAtEnd)
2533 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2534 ->getElementType())->getReturnType(),
2535 Instruction::Invoke,
2536 OperandTraits<InvokeInst>::op_end(this) - Values,
2537 Values, InsertAtEnd) {
2538 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2539 typename std::iterator_traits<InputIterator>::iterator_category());
2542 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2544 //===----------------------------------------------------------------------===//
2546 //===----------------------------------------------------------------------===//
2548 //===---------------------------------------------------------------------------
2549 /// UnwindInst - Immediately exit the current function, unwinding the stack
2550 /// until an invoke instruction is found.
2552 class UnwindInst : public TerminatorInst {
2553 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2555 // allocate space for exactly zero operands
2556 void *operator new(size_t s) {
2557 return User::operator new(s, 0);
2559 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2560 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2562 virtual UnwindInst *clone(LLVMContext &Context) const;
2564 unsigned getNumSuccessors() const { return 0; }
2566 // Methods for support type inquiry through isa, cast, and dyn_cast:
2567 static inline bool classof(const UnwindInst *) { return true; }
2568 static inline bool classof(const Instruction *I) {
2569 return I->getOpcode() == Instruction::Unwind;
2571 static inline bool classof(const Value *V) {
2572 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2575 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2576 virtual unsigned getNumSuccessorsV() const;
2577 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2580 //===----------------------------------------------------------------------===//
2581 // UnreachableInst Class
2582 //===----------------------------------------------------------------------===//
2584 //===---------------------------------------------------------------------------
2585 /// UnreachableInst - This function has undefined behavior. In particular, the
2586 /// presence of this instruction indicates some higher level knowledge that the
2587 /// end of the block cannot be reached.
2589 class UnreachableInst : public TerminatorInst {
2590 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2592 // allocate space for exactly zero operands
2593 void *operator new(size_t s) {
2594 return User::operator new(s, 0);
2596 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2597 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2599 virtual UnreachableInst *clone(LLVMContext &Context) const;
2601 unsigned getNumSuccessors() const { return 0; }
2603 // Methods for support type inquiry through isa, cast, and dyn_cast:
2604 static inline bool classof(const UnreachableInst *) { return true; }
2605 static inline bool classof(const Instruction *I) {
2606 return I->getOpcode() == Instruction::Unreachable;
2608 static inline bool classof(const Value *V) {
2609 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2612 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2613 virtual unsigned getNumSuccessorsV() const;
2614 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2617 //===----------------------------------------------------------------------===//
2619 //===----------------------------------------------------------------------===//
2621 /// @brief This class represents a truncation of integer types.
2622 class TruncInst : public CastInst {
2624 /// @brief Constructor with insert-before-instruction semantics
2626 Value *S, ///< The value to be truncated
2627 const Type *Ty, ///< The (smaller) type to truncate to
2628 const Twine &NameStr = "", ///< A name for the new instruction
2629 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2632 /// @brief Constructor with insert-at-end-of-block semantics
2634 Value *S, ///< The value to be truncated
2635 const Type *Ty, ///< The (smaller) type to truncate to
2636 const Twine &NameStr, ///< A name for the new instruction
2637 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2640 /// @brief Clone an identical TruncInst
2641 virtual CastInst *clone(LLVMContext &Context) const;
2643 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2644 static inline bool classof(const TruncInst *) { return true; }
2645 static inline bool classof(const Instruction *I) {
2646 return I->getOpcode() == Trunc;
2648 static inline bool classof(const Value *V) {
2649 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2653 //===----------------------------------------------------------------------===//
2655 //===----------------------------------------------------------------------===//
2657 /// @brief This class represents zero extension of integer types.
2658 class ZExtInst : public CastInst {
2660 /// @brief Constructor with insert-before-instruction semantics
2662 Value *S, ///< The value to be zero extended
2663 const Type *Ty, ///< The type to zero extend to
2664 const Twine &NameStr = "", ///< A name for the new instruction
2665 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2668 /// @brief Constructor with insert-at-end semantics.
2670 Value *S, ///< The value to be zero extended
2671 const Type *Ty, ///< The type to zero extend to
2672 const Twine &NameStr, ///< A name for the new instruction
2673 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2676 /// @brief Clone an identical ZExtInst
2677 virtual CastInst *clone(LLVMContext &Context) const;
2679 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2680 static inline bool classof(const ZExtInst *) { return true; }
2681 static inline bool classof(const Instruction *I) {
2682 return I->getOpcode() == ZExt;
2684 static inline bool classof(const Value *V) {
2685 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2689 //===----------------------------------------------------------------------===//
2691 //===----------------------------------------------------------------------===//
2693 /// @brief This class represents a sign extension of integer types.
2694 class SExtInst : public CastInst {
2696 /// @brief Constructor with insert-before-instruction semantics
2698 Value *S, ///< The value to be sign extended
2699 const Type *Ty, ///< The type to sign extend to
2700 const Twine &NameStr = "", ///< A name for the new instruction
2701 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2704 /// @brief Constructor with insert-at-end-of-block semantics
2706 Value *S, ///< The value to be sign extended
2707 const Type *Ty, ///< The type to sign extend to
2708 const Twine &NameStr, ///< A name for the new instruction
2709 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2712 /// @brief Clone an identical SExtInst
2713 virtual CastInst *clone(LLVMContext &Context) const;
2715 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2716 static inline bool classof(const SExtInst *) { return true; }
2717 static inline bool classof(const Instruction *I) {
2718 return I->getOpcode() == SExt;
2720 static inline bool classof(const Value *V) {
2721 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2725 //===----------------------------------------------------------------------===//
2726 // FPTruncInst Class
2727 //===----------------------------------------------------------------------===//
2729 /// @brief This class represents a truncation of floating point types.
2730 class FPTruncInst : public CastInst {
2732 /// @brief Constructor with insert-before-instruction semantics
2734 Value *S, ///< The value to be truncated
2735 const Type *Ty, ///< The type to truncate to
2736 const Twine &NameStr = "", ///< A name for the new instruction
2737 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2740 /// @brief Constructor with insert-before-instruction semantics
2742 Value *S, ///< The value to be truncated
2743 const Type *Ty, ///< The type to truncate to
2744 const Twine &NameStr, ///< A name for the new instruction
2745 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2748 /// @brief Clone an identical FPTruncInst
2749 virtual CastInst *clone(LLVMContext &Context) const;
2751 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2752 static inline bool classof(const FPTruncInst *) { return true; }
2753 static inline bool classof(const Instruction *I) {
2754 return I->getOpcode() == FPTrunc;
2756 static inline bool classof(const Value *V) {
2757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2761 //===----------------------------------------------------------------------===//
2763 //===----------------------------------------------------------------------===//
2765 /// @brief This class represents an extension of floating point types.
2766 class FPExtInst : public CastInst {
2768 /// @brief Constructor with insert-before-instruction semantics
2770 Value *S, ///< The value to be extended
2771 const Type *Ty, ///< The type to extend to
2772 const Twine &NameStr = "", ///< A name for the new instruction
2773 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2776 /// @brief Constructor with insert-at-end-of-block semantics
2778 Value *S, ///< The value to be extended
2779 const Type *Ty, ///< The type to extend to
2780 const Twine &NameStr, ///< A name for the new instruction
2781 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2784 /// @brief Clone an identical FPExtInst
2785 virtual CastInst *clone(LLVMContext &Context) const;
2787 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2788 static inline bool classof(const FPExtInst *) { return true; }
2789 static inline bool classof(const Instruction *I) {
2790 return I->getOpcode() == FPExt;
2792 static inline bool classof(const Value *V) {
2793 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2797 //===----------------------------------------------------------------------===//
2799 //===----------------------------------------------------------------------===//
2801 /// @brief This class represents a cast unsigned integer to floating point.
2802 class UIToFPInst : public CastInst {
2804 /// @brief Constructor with insert-before-instruction semantics
2806 Value *S, ///< The value to be converted
2807 const Type *Ty, ///< The type to convert to
2808 const Twine &NameStr = "", ///< A name for the new instruction
2809 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2812 /// @brief Constructor with insert-at-end-of-block semantics
2814 Value *S, ///< The value to be converted
2815 const Type *Ty, ///< The type to convert to
2816 const Twine &NameStr, ///< A name for the new instruction
2817 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2820 /// @brief Clone an identical UIToFPInst
2821 virtual CastInst *clone(LLVMContext &Context) const;
2823 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2824 static inline bool classof(const UIToFPInst *) { return true; }
2825 static inline bool classof(const Instruction *I) {
2826 return I->getOpcode() == UIToFP;
2828 static inline bool classof(const Value *V) {
2829 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2833 //===----------------------------------------------------------------------===//
2835 //===----------------------------------------------------------------------===//
2837 /// @brief This class represents a cast from signed integer to floating point.
2838 class SIToFPInst : public CastInst {
2840 /// @brief Constructor with insert-before-instruction semantics
2842 Value *S, ///< The value to be converted
2843 const Type *Ty, ///< The type to convert to
2844 const Twine &NameStr = "", ///< A name for the new instruction
2845 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2848 /// @brief Constructor with insert-at-end-of-block semantics
2850 Value *S, ///< The value to be converted
2851 const Type *Ty, ///< The type to convert to
2852 const Twine &NameStr, ///< A name for the new instruction
2853 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2856 /// @brief Clone an identical SIToFPInst
2857 virtual CastInst *clone(LLVMContext &Context) const;
2859 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2860 static inline bool classof(const SIToFPInst *) { return true; }
2861 static inline bool classof(const Instruction *I) {
2862 return I->getOpcode() == SIToFP;
2864 static inline bool classof(const Value *V) {
2865 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2869 //===----------------------------------------------------------------------===//
2871 //===----------------------------------------------------------------------===//
2873 /// @brief This class represents a cast from floating point to unsigned integer
2874 class FPToUIInst : public CastInst {
2876 /// @brief Constructor with insert-before-instruction semantics
2878 Value *S, ///< The value to be converted
2879 const Type *Ty, ///< The type to convert to
2880 const Twine &NameStr = "", ///< A name for the new instruction
2881 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2884 /// @brief Constructor with insert-at-end-of-block semantics
2886 Value *S, ///< The value to be converted
2887 const Type *Ty, ///< The type to convert to
2888 const Twine &NameStr, ///< A name for the new instruction
2889 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2892 /// @brief Clone an identical FPToUIInst
2893 virtual CastInst *clone(LLVMContext &Context) const;
2895 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2896 static inline bool classof(const FPToUIInst *) { return true; }
2897 static inline bool classof(const Instruction *I) {
2898 return I->getOpcode() == FPToUI;
2900 static inline bool classof(const Value *V) {
2901 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2905 //===----------------------------------------------------------------------===//
2907 //===----------------------------------------------------------------------===//
2909 /// @brief This class represents a cast from floating point to signed integer.
2910 class FPToSIInst : public CastInst {
2912 /// @brief Constructor with insert-before-instruction semantics
2914 Value *S, ///< The value to be converted
2915 const Type *Ty, ///< The type to convert to
2916 const Twine &NameStr = "", ///< A name for the new instruction
2917 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2920 /// @brief Constructor with insert-at-end-of-block semantics
2922 Value *S, ///< The value to be converted
2923 const Type *Ty, ///< The type to convert to
2924 const Twine &NameStr, ///< A name for the new instruction
2925 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2928 /// @brief Clone an identical FPToSIInst
2929 virtual CastInst *clone(LLVMContext &Context) const;
2931 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2932 static inline bool classof(const FPToSIInst *) { return true; }
2933 static inline bool classof(const Instruction *I) {
2934 return I->getOpcode() == FPToSI;
2936 static inline bool classof(const Value *V) {
2937 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2941 //===----------------------------------------------------------------------===//
2942 // IntToPtrInst Class
2943 //===----------------------------------------------------------------------===//
2945 /// @brief This class represents a cast from an integer to a pointer.
2946 class IntToPtrInst : public CastInst {
2948 /// @brief Constructor with insert-before-instruction semantics
2950 Value *S, ///< The value to be converted
2951 const Type *Ty, ///< The type to convert to
2952 const Twine &NameStr = "", ///< A name for the new instruction
2953 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2956 /// @brief Constructor with insert-at-end-of-block semantics
2958 Value *S, ///< The value to be converted
2959 const Type *Ty, ///< The type to convert to
2960 const Twine &NameStr, ///< A name for the new instruction
2961 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2964 /// @brief Clone an identical IntToPtrInst
2965 virtual CastInst *clone(LLVMContext &Context) const;
2967 // Methods for support type inquiry through isa, cast, and dyn_cast:
2968 static inline bool classof(const IntToPtrInst *) { return true; }
2969 static inline bool classof(const Instruction *I) {
2970 return I->getOpcode() == IntToPtr;
2972 static inline bool classof(const Value *V) {
2973 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2977 //===----------------------------------------------------------------------===//
2978 // PtrToIntInst Class
2979 //===----------------------------------------------------------------------===//
2981 /// @brief This class represents a cast from a pointer to an integer
2982 class PtrToIntInst : public CastInst {
2984 /// @brief Constructor with insert-before-instruction semantics
2986 Value *S, ///< The value to be converted
2987 const Type *Ty, ///< The type to convert to
2988 const Twine &NameStr = "", ///< A name for the new instruction
2989 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2992 /// @brief Constructor with insert-at-end-of-block semantics
2994 Value *S, ///< The value to be converted
2995 const Type *Ty, ///< The type to convert to
2996 const Twine &NameStr, ///< A name for the new instruction
2997 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3000 /// @brief Clone an identical PtrToIntInst
3001 virtual CastInst *clone(LLVMContext &Context) const;
3003 // Methods for support type inquiry through isa, cast, and dyn_cast:
3004 static inline bool classof(const PtrToIntInst *) { return true; }
3005 static inline bool classof(const Instruction *I) {
3006 return I->getOpcode() == PtrToInt;
3008 static inline bool classof(const Value *V) {
3009 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3013 //===----------------------------------------------------------------------===//
3014 // BitCastInst Class
3015 //===----------------------------------------------------------------------===//
3017 /// @brief This class represents a no-op cast from one type to another.
3018 class BitCastInst : public CastInst {
3020 /// @brief Constructor with insert-before-instruction semantics
3022 Value *S, ///< The value to be casted
3023 const Type *Ty, ///< The type to casted to
3024 const Twine &NameStr = "", ///< A name for the new instruction
3025 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3028 /// @brief Constructor with insert-at-end-of-block semantics
3030 Value *S, ///< The value to be casted
3031 const Type *Ty, ///< The type to casted to
3032 const Twine &NameStr, ///< A name for the new instruction
3033 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3036 /// @brief Clone an identical BitCastInst
3037 virtual CastInst *clone(LLVMContext &Context) const;
3039 // Methods for support type inquiry through isa, cast, and dyn_cast:
3040 static inline bool classof(const BitCastInst *) { return true; }
3041 static inline bool classof(const Instruction *I) {
3042 return I->getOpcode() == BitCast;
3044 static inline bool classof(const Value *V) {
3045 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3049 } // End llvm namespace