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 {
102 MallocInst(const MallocInst &MI);
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const Twine &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const Twine &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const Twine &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const Twine &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const Twine &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const Twine &NameStr = "",
127 Instruction *InsertBefore = 0)
128 : AllocationInst(Ty, ArraySize,
129 Malloc, Align, NameStr, InsertBefore) {}
131 virtual MallocInst *clone(LLVMContext &Context) const;
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const MallocInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Malloc);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// AllocaInst - an instruction to allocate memory on the stack
150 class AllocaInst : public AllocationInst {
151 AllocaInst(const AllocaInst &);
153 explicit AllocaInst(const Type *Ty,
154 Value *ArraySize = 0,
155 const Twine &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const Twine &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const Twine &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const Twine &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const Twine &NameStr = "",
173 Instruction *InsertBefore = 0)
174 : AllocationInst(Ty, ArraySize, Alloca,
175 Align, NameStr, InsertBefore) {}
176 AllocaInst(const Type *Ty, Value *ArraySize,
177 unsigned Align, const Twine &NameStr,
178 BasicBlock *InsertAtEnd)
179 : AllocationInst(Ty, ArraySize, Alloca,
180 Align, NameStr, InsertAtEnd) {}
182 virtual AllocaInst *clone(LLVMContext &Context) const;
184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 //===----------------------------------------------------------------------===//
202 //===----------------------------------------------------------------------===//
204 /// FreeInst - an instruction to deallocate memory
206 class FreeInst : public UnaryInstruction {
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
212 virtual FreeInst *clone(LLVMContext &Context) const;
214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// LoadInst - an instruction for reading from memory. This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
236 class LoadInst : public UnaryInstruction {
238 LoadInst(const LoadInst &LI)
239 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
240 setVolatile(LI.isVolatile());
241 setAlignment(LI.getAlignment());
249 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const Twine &NameStr = "", bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 /// isVolatile - Return true if this is a load from a volatile memory
263 bool isVolatile() const { return SubclassData & 1; }
265 /// setVolatile - Specify whether this is a volatile load or not.
267 void setVolatile(bool V) {
268 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
271 virtual LoadInst *clone(LLVMContext &Context) const;
273 /// getAlignment - Return the alignment of the access that is being performed
275 unsigned getAlignment() const {
276 return (1 << (SubclassData>>1)) >> 1;
279 void setAlignment(unsigned Align);
281 Value *getPointerOperand() { return getOperand(0); }
282 const Value *getPointerOperand() const { return getOperand(0); }
283 static unsigned getPointerOperandIndex() { return 0U; }
285 // Methods for support type inquiry through isa, cast, and dyn_cast:
286 static inline bool classof(const LoadInst *) { return true; }
287 static inline bool classof(const Instruction *I) {
288 return I->getOpcode() == Instruction::Load;
290 static inline bool classof(const Value *V) {
291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
296 //===----------------------------------------------------------------------===//
298 //===----------------------------------------------------------------------===//
300 /// StoreInst - an instruction for storing to memory
302 class StoreInst : public Instruction {
303 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
305 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
307 Op<0>() = SI.Op<0>();
308 Op<1>() = SI.Op<1>();
309 setVolatile(SI.isVolatile());
310 setAlignment(SI.getAlignment());
318 // allocate space for exactly two operands
319 void *operator new(size_t s) {
320 return User::operator new(s, 2);
322 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
323 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
324 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
325 Instruction *InsertBefore = 0);
326 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
327 unsigned Align, Instruction *InsertBefore = 0);
328 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
329 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
330 unsigned Align, BasicBlock *InsertAtEnd);
333 /// isVolatile - Return true if this is a load from a volatile memory
336 bool isVolatile() const { return SubclassData & 1; }
338 /// setVolatile - Specify whether this is a volatile load or not.
340 void setVolatile(bool V) {
341 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
344 /// Transparently provide more efficient getOperand methods.
345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
347 /// getAlignment - Return the alignment of the access that is being performed
349 unsigned getAlignment() const {
350 return (1 << (SubclassData>>1)) >> 1;
353 void setAlignment(unsigned Align);
355 virtual StoreInst *clone(LLVMContext &Context) const;
357 Value *getPointerOperand() { return getOperand(1); }
358 const Value *getPointerOperand() const { return getOperand(1); }
359 static unsigned getPointerOperandIndex() { return 1U; }
361 // Methods for support type inquiry through isa, cast, and dyn_cast:
362 static inline bool classof(const StoreInst *) { return true; }
363 static inline bool classof(const Instruction *I) {
364 return I->getOpcode() == Instruction::Store;
366 static inline bool classof(const Value *V) {
367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
372 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
377 //===----------------------------------------------------------------------===//
378 // GetElementPtrInst Class
379 //===----------------------------------------------------------------------===//
381 // checkType - Simple wrapper function to give a better assertion failure
382 // message on bad indexes for a gep instruction.
384 static inline const Type *checkType(const Type *Ty) {
385 assert(Ty && "Invalid GetElementPtrInst indices for type!");
389 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
390 /// access elements of arrays and structs
392 class GetElementPtrInst : public Instruction {
393 GetElementPtrInst(const GetElementPtrInst &GEPI);
394 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
395 const Twine &NameStr);
396 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
398 template<typename InputIterator>
399 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
400 const Twine &NameStr,
401 // This argument ensures that we have an iterator we can
402 // do arithmetic on in constant time
403 std::random_access_iterator_tag) {
404 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
407 // This requires that the iterator points to contiguous memory.
408 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
409 // we have to build an array here
412 init(Ptr, 0, NumIdx, NameStr);
416 /// getIndexedType - Returns the type of the element that would be loaded with
417 /// a load instruction with the specified parameters.
419 /// Null is returned if the indices are invalid for the specified
422 template<typename InputIterator>
423 static const Type *getIndexedType(const Type *Ptr,
424 InputIterator IdxBegin,
425 InputIterator IdxEnd,
426 // This argument ensures that we
427 // have an iterator we can do
428 // arithmetic on in constant time
429 std::random_access_iterator_tag) {
430 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
433 // This requires that the iterator points to contiguous memory.
434 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
436 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
439 /// Constructors - Create a getelementptr instruction with a base pointer an
440 /// list of indices. The first ctor can optionally insert before an existing
441 /// instruction, the second appends the new instruction to the specified
443 template<typename InputIterator>
444 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
445 InputIterator IdxEnd,
447 const Twine &NameStr,
448 Instruction *InsertBefore);
449 template<typename InputIterator>
450 inline GetElementPtrInst(Value *Ptr,
451 InputIterator IdxBegin, InputIterator IdxEnd,
453 const Twine &NameStr, BasicBlock *InsertAtEnd);
455 /// Constructors - These two constructors are convenience methods because one
456 /// and two index getelementptr instructions are so common.
457 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
458 Instruction *InsertBefore = 0);
459 GetElementPtrInst(Value *Ptr, Value *Idx,
460 const Twine &NameStr, BasicBlock *InsertAtEnd);
462 template<typename InputIterator>
463 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
464 InputIterator IdxEnd,
465 const Twine &NameStr = "",
466 Instruction *InsertBefore = 0) {
467 typename std::iterator_traits<InputIterator>::difference_type Values =
468 1 + std::distance(IdxBegin, IdxEnd);
470 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
472 template<typename InputIterator>
473 static GetElementPtrInst *Create(Value *Ptr,
474 InputIterator IdxBegin, InputIterator IdxEnd,
475 const Twine &NameStr,
476 BasicBlock *InsertAtEnd) {
477 typename std::iterator_traits<InputIterator>::difference_type Values =
478 1 + std::distance(IdxBegin, IdxEnd);
480 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
483 /// Constructors - These two creators are convenience methods because one
484 /// index getelementptr instructions are so common.
485 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
486 const Twine &NameStr = "",
487 Instruction *InsertBefore = 0) {
488 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
490 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
491 const Twine &NameStr,
492 BasicBlock *InsertAtEnd) {
493 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
496 /// Create an "inbounds" getelementptr. See the documentation for the
497 /// "inbounds" flag in LangRef.html for details.
498 template<typename InputIterator>
499 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
500 InputIterator IdxEnd,
501 const Twine &NameStr = "",
502 Instruction *InsertBefore = 0) {
503 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
504 NameStr, InsertBefore);
505 cast<GEPOperator>(GEP)->setIsInBounds(true);
508 template<typename InputIterator>
509 static GetElementPtrInst *CreateInBounds(Value *Ptr,
510 InputIterator IdxBegin,
511 InputIterator IdxEnd,
512 const Twine &NameStr,
513 BasicBlock *InsertAtEnd) {
514 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
515 NameStr, InsertAtEnd);
516 cast<GEPOperator>(GEP)->setIsInBounds(true);
519 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
520 const Twine &NameStr = "",
521 Instruction *InsertBefore = 0) {
522 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
523 cast<GEPOperator>(GEP)->setIsInBounds(true);
526 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
527 const Twine &NameStr,
528 BasicBlock *InsertAtEnd) {
529 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
530 cast<GEPOperator>(GEP)->setIsInBounds(true);
534 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
536 /// Transparently provide more efficient getOperand methods.
537 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
539 // getType - Overload to return most specific pointer type...
540 const PointerType *getType() const {
541 return reinterpret_cast<const PointerType*>(Instruction::getType());
544 /// getIndexedType - Returns the type of the element that would be loaded with
545 /// a load instruction with the specified parameters.
547 /// Null is returned if the indices are invalid for the specified
550 template<typename InputIterator>
551 static const Type *getIndexedType(const Type *Ptr,
552 InputIterator IdxBegin,
553 InputIterator IdxEnd) {
554 return getIndexedType(Ptr, IdxBegin, IdxEnd,
555 typename std::iterator_traits<InputIterator>::
556 iterator_category());
559 static const Type *getIndexedType(const Type *Ptr,
560 Value* const *Idx, unsigned NumIdx);
562 static const Type *getIndexedType(const Type *Ptr,
563 uint64_t const *Idx, unsigned NumIdx);
565 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
567 inline op_iterator idx_begin() { return op_begin()+1; }
568 inline const_op_iterator idx_begin() const { return op_begin()+1; }
569 inline op_iterator idx_end() { return op_end(); }
570 inline const_op_iterator idx_end() const { return op_end(); }
572 Value *getPointerOperand() {
573 return getOperand(0);
575 const Value *getPointerOperand() const {
576 return getOperand(0);
578 static unsigned getPointerOperandIndex() {
579 return 0U; // get index for modifying correct operand
582 /// getPointerOperandType - Method to return the pointer operand as a
584 const PointerType *getPointerOperandType() const {
585 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
589 unsigned getNumIndices() const { // Note: always non-negative
590 return getNumOperands() - 1;
593 bool hasIndices() const {
594 return getNumOperands() > 1;
597 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
598 /// zeros. If so, the result pointer and the first operand have the same
599 /// value, just potentially different types.
600 bool hasAllZeroIndices() const;
602 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
603 /// constant integers. If so, the result pointer and the first operand have
604 /// a constant offset between them.
605 bool hasAllConstantIndices() const;
607 // Methods for support type inquiry through isa, cast, and dyn_cast:
608 static inline bool classof(const GetElementPtrInst *) { return true; }
609 static inline bool classof(const Instruction *I) {
610 return (I->getOpcode() == Instruction::GetElementPtr);
612 static inline bool classof(const Value *V) {
613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
618 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
621 template<typename InputIterator>
622 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
623 InputIterator IdxBegin,
624 InputIterator IdxEnd,
626 const Twine &NameStr,
627 Instruction *InsertBefore)
628 : Instruction(PointerType::get(checkType(
629 getIndexedType(Ptr->getType(),
631 cast<PointerType>(Ptr->getType())
632 ->getAddressSpace()),
634 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
635 Values, InsertBefore) {
636 init(Ptr, IdxBegin, IdxEnd, NameStr,
637 typename std::iterator_traits<InputIterator>::iterator_category());
639 template<typename InputIterator>
640 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
641 InputIterator IdxBegin,
642 InputIterator IdxEnd,
644 const Twine &NameStr,
645 BasicBlock *InsertAtEnd)
646 : Instruction(PointerType::get(checkType(
647 getIndexedType(Ptr->getType(),
649 cast<PointerType>(Ptr->getType())
650 ->getAddressSpace()),
652 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
653 Values, InsertAtEnd) {
654 init(Ptr, IdxBegin, IdxEnd, NameStr,
655 typename std::iterator_traits<InputIterator>::iterator_category());
659 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
662 //===----------------------------------------------------------------------===//
664 //===----------------------------------------------------------------------===//
666 /// This instruction compares its operands according to the predicate given
667 /// to the constructor. It only operates on integers or pointers. The operands
668 /// must be identical types.
669 /// @brief Represent an integer comparison operator.
670 class ICmpInst: public CmpInst {
672 /// @brief Constructor with insert-before-instruction semantics.
674 Instruction *InsertBefore, ///< Where to insert
675 Predicate pred, ///< The predicate to use for the comparison
676 Value *LHS, ///< The left-hand-side of the expression
677 Value *RHS, ///< The right-hand-side of the expression
678 const Twine &NameStr = "" ///< Name of the instruction
679 ) : CmpInst(makeCmpResultType(LHS->getType()),
680 Instruction::ICmp, pred, LHS, RHS, NameStr,
682 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
683 pred <= CmpInst::LAST_ICMP_PREDICATE &&
684 "Invalid ICmp predicate value");
685 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
686 "Both operands to ICmp instruction are not of the same type!");
687 // Check that the operands are the right type
688 assert((getOperand(0)->getType()->isIntOrIntVector() ||
689 isa<PointerType>(getOperand(0)->getType())) &&
690 "Invalid operand types for ICmp instruction");
693 /// @brief Constructor with insert-at-end semantics.
695 BasicBlock &InsertAtEnd, ///< Block to insert into.
696 Predicate pred, ///< The predicate to use for the comparison
697 Value *LHS, ///< The left-hand-side of the expression
698 Value *RHS, ///< The right-hand-side of the expression
699 const Twine &NameStr = "" ///< Name of the instruction
700 ) : CmpInst(makeCmpResultType(LHS->getType()),
701 Instruction::ICmp, pred, LHS, RHS, NameStr,
703 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
704 pred <= CmpInst::LAST_ICMP_PREDICATE &&
705 "Invalid ICmp predicate value");
706 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
707 "Both operands to ICmp instruction are not of the same type!");
708 // Check that the operands are the right type
709 assert((getOperand(0)->getType()->isIntOrIntVector() ||
710 isa<PointerType>(getOperand(0)->getType())) &&
711 "Invalid operand types for ICmp instruction");
714 /// @brief Constructor with no-insertion semantics
716 LLVMContext &Context, ///< Context to construct within
717 Predicate pred, ///< The predicate to use for the comparison
718 Value *LHS, ///< The left-hand-side of the expression
719 Value *RHS, ///< The right-hand-side of the expression
720 const Twine &NameStr = "" ///< Name of the instruction
721 ) : CmpInst(makeCmpResultType(LHS->getType()),
722 Instruction::ICmp, pred, LHS, RHS, NameStr) {
723 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
724 pred <= CmpInst::LAST_ICMP_PREDICATE &&
725 "Invalid ICmp predicate value");
726 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
727 "Both operands to ICmp instruction are not of the same type!");
728 // Check that the operands are the right type
729 assert((getOperand(0)->getType()->isIntOrIntVector() ||
730 isa<PointerType>(getOperand(0)->getType())) &&
731 "Invalid operand types for ICmp instruction");
734 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
735 /// @returns the predicate that would be the result if the operand were
736 /// regarded as signed.
737 /// @brief Return the signed version of the predicate
738 Predicate getSignedPredicate() const {
739 return getSignedPredicate(getPredicate());
742 /// This is a static version that you can use without an instruction.
743 /// @brief Return the signed version of the predicate.
744 static Predicate getSignedPredicate(Predicate pred);
746 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
747 /// @returns the predicate that would be the result if the operand were
748 /// regarded as unsigned.
749 /// @brief Return the unsigned version of the predicate
750 Predicate getUnsignedPredicate() const {
751 return getUnsignedPredicate(getPredicate());
754 /// This is a static version that you can use without an instruction.
755 /// @brief Return the unsigned version of the predicate.
756 static Predicate getUnsignedPredicate(Predicate pred);
758 /// isEquality - Return true if this predicate is either EQ or NE. This also
759 /// tests for commutativity.
760 static bool isEquality(Predicate P) {
761 return P == ICMP_EQ || P == ICMP_NE;
764 /// isEquality - Return true if this predicate is either EQ or NE. This also
765 /// tests for commutativity.
766 bool isEquality() const {
767 return isEquality(getPredicate());
770 /// @returns true if the predicate of this ICmpInst is commutative
771 /// @brief Determine if this relation is commutative.
772 bool isCommutative() const { return isEquality(); }
774 /// isRelational - Return true if the predicate is relational (not EQ or NE).
776 bool isRelational() const {
777 return !isEquality();
780 /// isRelational - Return true if the predicate is relational (not EQ or NE).
782 static bool isRelational(Predicate P) {
783 return !isEquality(P);
786 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
787 /// @brief Determine if this instruction's predicate is signed.
788 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
790 /// @returns true if the predicate provided is signed, false otherwise
791 /// @brief Determine if the predicate is signed.
792 static bool isSignedPredicate(Predicate pred);
794 /// @returns true if the specified compare predicate is
795 /// true when both operands are equal...
796 /// @brief Determine if the icmp is true when both operands are equal
797 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
798 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
799 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
800 pred == ICmpInst::ICMP_SLE;
803 /// @returns true if the specified compare instruction is
804 /// true when both operands are equal...
805 /// @brief Determine if the ICmpInst returns true when both operands are equal
806 bool isTrueWhenEqual() {
807 return isTrueWhenEqual(getPredicate());
810 /// Initialize a set of values that all satisfy the predicate with C.
811 /// @brief Make a ConstantRange for a relation with a constant value.
812 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
814 /// Exchange the two operands to this instruction in such a way that it does
815 /// not modify the semantics of the instruction. The predicate value may be
816 /// changed to retain the same result if the predicate is order dependent
818 /// @brief Swap operands and adjust predicate.
819 void swapOperands() {
820 SubclassData = getSwappedPredicate();
821 Op<0>().swap(Op<1>());
824 virtual ICmpInst *clone(LLVMContext &Context) const;
826 // Methods for support type inquiry through isa, cast, and dyn_cast:
827 static inline bool classof(const ICmpInst *) { return true; }
828 static inline bool classof(const Instruction *I) {
829 return I->getOpcode() == Instruction::ICmp;
831 static inline bool classof(const Value *V) {
832 return isa<Instruction>(V) && classof(cast<Instruction>(V));
837 //===----------------------------------------------------------------------===//
839 //===----------------------------------------------------------------------===//
841 /// This instruction compares its operands according to the predicate given
842 /// to the constructor. It only operates on floating point values or packed
843 /// vectors of floating point values. The operands must be identical types.
844 /// @brief Represents a floating point comparison operator.
845 class FCmpInst: public CmpInst {
847 /// @brief Constructor with insert-before-instruction semantics.
849 Instruction *InsertBefore, ///< Where to insert
850 Predicate pred, ///< The predicate to use for the comparison
851 Value *LHS, ///< The left-hand-side of the expression
852 Value *RHS, ///< The right-hand-side of the expression
853 const Twine &NameStr = "" ///< Name of the instruction
854 ) : CmpInst(makeCmpResultType(LHS->getType()),
855 Instruction::FCmp, pred, LHS, RHS, NameStr,
857 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
858 "Invalid FCmp predicate value");
859 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
860 "Both operands to FCmp instruction are not of the same type!");
861 // Check that the operands are the right type
862 assert(getOperand(0)->getType()->isFPOrFPVector() &&
863 "Invalid operand types for FCmp instruction");
866 /// @brief Constructor with insert-at-end semantics.
868 BasicBlock &InsertAtEnd, ///< Block to insert into.
869 Predicate pred, ///< The predicate to use for the comparison
870 Value *LHS, ///< The left-hand-side of the expression
871 Value *RHS, ///< The right-hand-side of the expression
872 const Twine &NameStr = "" ///< Name of the instruction
873 ) : CmpInst(makeCmpResultType(LHS->getType()),
874 Instruction::FCmp, pred, LHS, RHS, NameStr,
876 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
877 "Invalid FCmp predicate value");
878 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
879 "Both operands to FCmp instruction are not of the same type!");
880 // Check that the operands are the right type
881 assert(getOperand(0)->getType()->isFPOrFPVector() &&
882 "Invalid operand types for FCmp instruction");
885 /// @brief Constructor with no-insertion semantics
887 LLVMContext &Context, ///< Context to build in
888 Predicate pred, ///< The predicate to use for the comparison
889 Value *LHS, ///< The left-hand-side of the expression
890 Value *RHS, ///< The right-hand-side of the expression
891 const Twine &NameStr = "" ///< Name of the instruction
892 ) : CmpInst(makeCmpResultType(LHS->getType()),
893 Instruction::FCmp, pred, LHS, RHS, NameStr) {
894 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
895 "Invalid FCmp predicate value");
896 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
897 "Both operands to FCmp instruction are not of the same type!");
898 // Check that the operands are the right type
899 assert(getOperand(0)->getType()->isFPOrFPVector() &&
900 "Invalid operand types for FCmp instruction");
903 /// @returns true if the predicate of this instruction is EQ or NE.
904 /// @brief Determine if this is an equality predicate.
905 bool isEquality() const {
906 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
907 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
910 /// @returns true if the predicate of this instruction is commutative.
911 /// @brief Determine if this is a commutative predicate.
912 bool isCommutative() const {
913 return isEquality() ||
914 SubclassData == FCMP_FALSE ||
915 SubclassData == FCMP_TRUE ||
916 SubclassData == FCMP_ORD ||
917 SubclassData == FCMP_UNO;
920 /// @returns true if the predicate is relational (not EQ or NE).
921 /// @brief Determine if this a relational predicate.
922 bool isRelational() const { return !isEquality(); }
924 /// Exchange the two operands to this instruction in such a way that it does
925 /// not modify the semantics of the instruction. The predicate value may be
926 /// changed to retain the same result if the predicate is order dependent
928 /// @brief Swap operands and adjust predicate.
929 void swapOperands() {
930 SubclassData = getSwappedPredicate();
931 Op<0>().swap(Op<1>());
934 virtual FCmpInst *clone(LLVMContext &Context) const;
936 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
937 static inline bool classof(const FCmpInst *) { return true; }
938 static inline bool classof(const Instruction *I) {
939 return I->getOpcode() == Instruction::FCmp;
941 static inline bool classof(const Value *V) {
942 return isa<Instruction>(V) && classof(cast<Instruction>(V));
946 //===----------------------------------------------------------------------===//
948 //===----------------------------------------------------------------------===//
949 /// CallInst - This class represents a function call, abstracting a target
950 /// machine's calling convention. This class uses low bit of the SubClassData
951 /// field to indicate whether or not this is a tail call. The rest of the bits
952 /// hold the calling convention of the call.
955 class CallInst : public Instruction {
956 AttrListPtr AttributeList; ///< parameter attributes for call
957 CallInst(const CallInst &CI);
958 void init(Value *Func, Value* const *Params, unsigned NumParams);
959 void init(Value *Func, Value *Actual1, Value *Actual2);
960 void init(Value *Func, Value *Actual);
961 void init(Value *Func);
963 template<typename InputIterator>
964 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
965 const Twine &NameStr,
966 // This argument ensures that we have an iterator we can
967 // do arithmetic on in constant time
968 std::random_access_iterator_tag) {
969 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
971 // This requires that the iterator points to contiguous memory.
972 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
976 /// Construct a CallInst given a range of arguments. InputIterator
977 /// must be a random-access iterator pointing to contiguous storage
978 /// (e.g. a std::vector<>::iterator). Checks are made for
979 /// random-accessness but not for contiguous storage as that would
980 /// incur runtime overhead.
981 /// @brief Construct a CallInst from a range of arguments
982 template<typename InputIterator>
983 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
984 const Twine &NameStr, Instruction *InsertBefore);
986 /// Construct a CallInst given a range of arguments. InputIterator
987 /// must be a random-access iterator pointing to contiguous storage
988 /// (e.g. a std::vector<>::iterator). Checks are made for
989 /// random-accessness but not for contiguous storage as that would
990 /// incur runtime overhead.
991 /// @brief Construct a CallInst from a range of arguments
992 template<typename InputIterator>
993 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
994 const Twine &NameStr, BasicBlock *InsertAtEnd);
996 CallInst(Value *F, Value *Actual, const Twine &NameStr,
997 Instruction *InsertBefore);
998 CallInst(Value *F, Value *Actual, const Twine &NameStr,
999 BasicBlock *InsertAtEnd);
1000 explicit CallInst(Value *F, const Twine &NameStr,
1001 Instruction *InsertBefore);
1002 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1004 template<typename InputIterator>
1005 static CallInst *Create(Value *Func,
1006 InputIterator ArgBegin, InputIterator ArgEnd,
1007 const Twine &NameStr = "",
1008 Instruction *InsertBefore = 0) {
1009 return new((unsigned)(ArgEnd - ArgBegin + 1))
1010 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1012 template<typename InputIterator>
1013 static CallInst *Create(Value *Func,
1014 InputIterator ArgBegin, InputIterator ArgEnd,
1015 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1016 return new((unsigned)(ArgEnd - ArgBegin + 1))
1017 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1019 static CallInst *Create(Value *F, Value *Actual,
1020 const Twine &NameStr = "",
1021 Instruction *InsertBefore = 0) {
1022 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1024 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1025 BasicBlock *InsertAtEnd) {
1026 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1028 static CallInst *Create(Value *F, const Twine &NameStr = "",
1029 Instruction *InsertBefore = 0) {
1030 return new(1) CallInst(F, NameStr, InsertBefore);
1032 static CallInst *Create(Value *F, const Twine &NameStr,
1033 BasicBlock *InsertAtEnd) {
1034 return new(1) CallInst(F, NameStr, InsertAtEnd);
1039 bool isTailCall() const { return SubclassData & 1; }
1040 void setTailCall(bool isTC = true) {
1041 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1044 virtual CallInst *clone(LLVMContext &Context) const;
1046 /// Provide fast operand accessors
1047 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1049 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1051 unsigned getCallingConv() const { return SubclassData >> 1; }
1052 void setCallingConv(unsigned CC) {
1053 SubclassData = (SubclassData & 1) | (CC << 1);
1056 /// getAttributes - Return the parameter attributes for this call.
1058 const AttrListPtr &getAttributes() const { return AttributeList; }
1060 /// setAttributes - Set the parameter attributes for this call.
1062 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1064 /// addAttribute - adds the attribute to the list of attributes.
1065 void addAttribute(unsigned i, Attributes attr);
1067 /// removeAttribute - removes the attribute from the list of attributes.
1068 void removeAttribute(unsigned i, Attributes attr);
1070 /// @brief Determine whether the call or the callee has the given attribute.
1071 bool paramHasAttr(unsigned i, Attributes attr) const;
1073 /// @brief Extract the alignment for a call or parameter (0=unknown).
1074 unsigned getParamAlignment(unsigned i) const {
1075 return AttributeList.getParamAlignment(i);
1078 /// @brief Determine if the call does not access memory.
1079 bool doesNotAccessMemory() const {
1080 return paramHasAttr(~0, Attribute::ReadNone);
1082 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1083 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1084 else removeAttribute(~0, Attribute::ReadNone);
1087 /// @brief Determine if the call does not access or only reads memory.
1088 bool onlyReadsMemory() const {
1089 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1091 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1092 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1093 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1096 /// @brief Determine if the call cannot return.
1097 bool doesNotReturn() const {
1098 return paramHasAttr(~0, Attribute::NoReturn);
1100 void setDoesNotReturn(bool DoesNotReturn = true) {
1101 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1102 else removeAttribute(~0, Attribute::NoReturn);
1105 /// @brief Determine if the call cannot unwind.
1106 bool doesNotThrow() const {
1107 return paramHasAttr(~0, Attribute::NoUnwind);
1109 void setDoesNotThrow(bool DoesNotThrow = true) {
1110 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1111 else removeAttribute(~0, Attribute::NoUnwind);
1114 /// @brief Determine if the call returns a structure through first
1115 /// pointer argument.
1116 bool hasStructRetAttr() const {
1117 // Be friendly and also check the callee.
1118 return paramHasAttr(1, Attribute::StructRet);
1121 /// @brief Determine if any call argument is an aggregate passed by value.
1122 bool hasByValArgument() const {
1123 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1126 /// getCalledFunction - Return the function called, or null if this is an
1127 /// indirect function invocation.
1129 Function *getCalledFunction() const {
1130 return dyn_cast<Function>(Op<0>());
1133 /// getCalledValue - Get a pointer to the function that is invoked by this
1135 const Value *getCalledValue() const { return Op<0>(); }
1136 Value *getCalledValue() { return Op<0>(); }
1138 // Methods for support type inquiry through isa, cast, and dyn_cast:
1139 static inline bool classof(const CallInst *) { return true; }
1140 static inline bool classof(const Instruction *I) {
1141 return I->getOpcode() == Instruction::Call;
1143 static inline bool classof(const Value *V) {
1144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1149 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1152 template<typename InputIterator>
1153 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1154 const Twine &NameStr, BasicBlock *InsertAtEnd)
1155 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1156 ->getElementType())->getReturnType(),
1158 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1159 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1160 init(Func, ArgBegin, ArgEnd, NameStr,
1161 typename std::iterator_traits<InputIterator>::iterator_category());
1164 template<typename InputIterator>
1165 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1166 const Twine &NameStr, Instruction *InsertBefore)
1167 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1168 ->getElementType())->getReturnType(),
1170 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1171 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1172 init(Func, ArgBegin, ArgEnd, NameStr,
1173 typename std::iterator_traits<InputIterator>::iterator_category());
1176 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1178 //===----------------------------------------------------------------------===//
1180 //===----------------------------------------------------------------------===//
1182 /// SelectInst - This class represents the LLVM 'select' instruction.
1184 class SelectInst : public Instruction {
1185 void init(Value *C, Value *S1, Value *S2) {
1186 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1192 SelectInst(const SelectInst &SI)
1193 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1194 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1196 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1197 Instruction *InsertBefore)
1198 : Instruction(S1->getType(), Instruction::Select,
1199 &Op<0>(), 3, InsertBefore) {
1203 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1204 BasicBlock *InsertAtEnd)
1205 : Instruction(S1->getType(), Instruction::Select,
1206 &Op<0>(), 3, InsertAtEnd) {
1211 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1212 const Twine &NameStr = "",
1213 Instruction *InsertBefore = 0) {
1214 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1216 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1217 const Twine &NameStr,
1218 BasicBlock *InsertAtEnd) {
1219 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1222 Value *getCondition() const { return Op<0>(); }
1223 Value *getTrueValue() const { return Op<1>(); }
1224 Value *getFalseValue() const { return Op<2>(); }
1226 /// areInvalidOperands - Return a string if the specified operands are invalid
1227 /// for a select operation, otherwise return null.
1228 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1230 /// Transparently provide more efficient getOperand methods.
1231 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1233 OtherOps getOpcode() const {
1234 return static_cast<OtherOps>(Instruction::getOpcode());
1237 virtual SelectInst *clone(LLVMContext &Context) const;
1239 // Methods for support type inquiry through isa, cast, and dyn_cast:
1240 static inline bool classof(const SelectInst *) { return true; }
1241 static inline bool classof(const Instruction *I) {
1242 return I->getOpcode() == Instruction::Select;
1244 static inline bool classof(const Value *V) {
1245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1250 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1253 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1255 //===----------------------------------------------------------------------===//
1257 //===----------------------------------------------------------------------===//
1259 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1260 /// an argument of the specified type given a va_list and increments that list
1262 class VAArgInst : public UnaryInstruction {
1263 VAArgInst(const VAArgInst &VAA)
1264 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1266 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1267 Instruction *InsertBefore = 0)
1268 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1271 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1272 BasicBlock *InsertAtEnd)
1273 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1277 virtual VAArgInst *clone(LLVMContext &Context) const;
1279 // Methods for support type inquiry through isa, cast, and dyn_cast:
1280 static inline bool classof(const VAArgInst *) { return true; }
1281 static inline bool classof(const Instruction *I) {
1282 return I->getOpcode() == VAArg;
1284 static inline bool classof(const Value *V) {
1285 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1289 //===----------------------------------------------------------------------===//
1290 // ExtractElementInst Class
1291 //===----------------------------------------------------------------------===//
1293 /// ExtractElementInst - This instruction extracts a single (scalar)
1294 /// element from a VectorType value
1296 class ExtractElementInst : public Instruction {
1297 ExtractElementInst(const ExtractElementInst &EE) :
1298 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1299 Op<0>() = EE.Op<0>();
1300 Op<1>() = EE.Op<1>();
1303 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1304 Instruction *InsertBefore = 0);
1305 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1306 BasicBlock *InsertAtEnd);
1308 static ExtractElementInst *Create(const ExtractElementInst &EE) {
1309 return new(EE.getNumOperands()) ExtractElementInst(EE);
1312 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1313 const Twine &NameStr = "",
1314 Instruction *InsertBefore = 0) {
1315 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1317 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1318 const Twine &NameStr,
1319 BasicBlock *InsertAtEnd) {
1320 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1323 /// isValidOperands - Return true if an extractelement instruction can be
1324 /// formed with the specified operands.
1325 static bool isValidOperands(const Value *Vec, const Value *Idx);
1327 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1329 /// Transparently provide more efficient getOperand methods.
1330 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1332 // Methods for support type inquiry through isa, cast, and dyn_cast:
1333 static inline bool classof(const ExtractElementInst *) { return true; }
1334 static inline bool classof(const Instruction *I) {
1335 return I->getOpcode() == Instruction::ExtractElement;
1337 static inline bool classof(const Value *V) {
1338 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1343 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1346 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1348 //===----------------------------------------------------------------------===//
1349 // InsertElementInst Class
1350 //===----------------------------------------------------------------------===//
1352 /// InsertElementInst - This instruction inserts a single (scalar)
1353 /// element into a VectorType value
1355 class InsertElementInst : public Instruction {
1356 InsertElementInst(const InsertElementInst &IE);
1357 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1358 const Twine &NameStr = "",
1359 Instruction *InsertBefore = 0);
1360 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1361 const Twine &NameStr, BasicBlock *InsertAtEnd);
1363 static InsertElementInst *Create(const InsertElementInst &IE) {
1364 return new(IE.getNumOperands()) InsertElementInst(IE);
1366 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1367 const Twine &NameStr = "",
1368 Instruction *InsertBefore = 0) {
1369 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1371 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1372 const Twine &NameStr,
1373 BasicBlock *InsertAtEnd) {
1374 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1377 /// isValidOperands - Return true if an insertelement instruction can be
1378 /// formed with the specified operands.
1379 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1382 virtual InsertElementInst *clone(LLVMContext &Context) const;
1384 /// getType - Overload to return most specific vector type.
1386 const VectorType *getType() const {
1387 return reinterpret_cast<const VectorType*>(Instruction::getType());
1390 /// Transparently provide more efficient getOperand methods.
1391 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1393 // Methods for support type inquiry through isa, cast, and dyn_cast:
1394 static inline bool classof(const InsertElementInst *) { return true; }
1395 static inline bool classof(const Instruction *I) {
1396 return I->getOpcode() == Instruction::InsertElement;
1398 static inline bool classof(const Value *V) {
1399 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1404 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1407 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1409 //===----------------------------------------------------------------------===//
1410 // ShuffleVectorInst Class
1411 //===----------------------------------------------------------------------===//
1413 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1416 class ShuffleVectorInst : public Instruction {
1417 ShuffleVectorInst(const ShuffleVectorInst &IE);
1419 // allocate space for exactly three operands
1420 void *operator new(size_t s) {
1421 return User::operator new(s, 3);
1423 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1424 const Twine &NameStr = "",
1425 Instruction *InsertBefor = 0);
1426 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1427 const Twine &NameStr, BasicBlock *InsertAtEnd);
1429 /// isValidOperands - Return true if a shufflevector instruction can be
1430 /// formed with the specified operands.
1431 static bool isValidOperands(const Value *V1, const Value *V2,
1434 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1436 /// getType - Overload to return most specific vector type.
1438 const VectorType *getType() const {
1439 return reinterpret_cast<const VectorType*>(Instruction::getType());
1442 /// Transparently provide more efficient getOperand methods.
1443 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1445 /// getMaskValue - Return the index from the shuffle mask for the specified
1446 /// output result. This is either -1 if the element is undef or a number less
1447 /// than 2*numelements.
1448 int getMaskValue(unsigned i) const;
1450 // Methods for support type inquiry through isa, cast, and dyn_cast:
1451 static inline bool classof(const ShuffleVectorInst *) { return true; }
1452 static inline bool classof(const Instruction *I) {
1453 return I->getOpcode() == Instruction::ShuffleVector;
1455 static inline bool classof(const Value *V) {
1456 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1461 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1464 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1466 //===----------------------------------------------------------------------===//
1467 // ExtractValueInst Class
1468 //===----------------------------------------------------------------------===//
1470 /// ExtractValueInst - This instruction extracts a struct member or array
1471 /// element value from an aggregate value.
1473 class ExtractValueInst : public UnaryInstruction {
1474 SmallVector<unsigned, 4> Indices;
1476 ExtractValueInst(const ExtractValueInst &EVI);
1477 void init(const unsigned *Idx, unsigned NumIdx,
1478 const Twine &NameStr);
1479 void init(unsigned Idx, const Twine &NameStr);
1481 template<typename InputIterator>
1482 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1483 const Twine &NameStr,
1484 // This argument ensures that we have an iterator we can
1485 // do arithmetic on in constant time
1486 std::random_access_iterator_tag) {
1487 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1489 // There's no fundamental reason why we require at least one index
1490 // (other than weirdness with &*IdxBegin being invalid; see
1491 // getelementptr's init routine for example). But there's no
1492 // present need to support it.
1493 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1495 // This requires that the iterator points to contiguous memory.
1496 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1497 // we have to build an array here
1500 /// getIndexedType - Returns the type of the element that would be extracted
1501 /// with an extractvalue instruction with the specified parameters.
1503 /// Null is returned if the indices are invalid for the specified
1506 static const Type *getIndexedType(const Type *Agg,
1507 const unsigned *Idx, unsigned NumIdx);
1509 template<typename InputIterator>
1510 static const Type *getIndexedType(const Type *Ptr,
1511 InputIterator IdxBegin,
1512 InputIterator IdxEnd,
1513 // This argument ensures that we
1514 // have an iterator we can do
1515 // arithmetic on in constant time
1516 std::random_access_iterator_tag) {
1517 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1520 // This requires that the iterator points to contiguous memory.
1521 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1523 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1526 /// Constructors - Create a extractvalue instruction with a base aggregate
1527 /// value and a list of indices. The first ctor can optionally insert before
1528 /// an existing instruction, the second appends the new instruction to the
1529 /// specified BasicBlock.
1530 template<typename InputIterator>
1531 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1532 InputIterator IdxEnd,
1533 const Twine &NameStr,
1534 Instruction *InsertBefore);
1535 template<typename InputIterator>
1536 inline ExtractValueInst(Value *Agg,
1537 InputIterator IdxBegin, InputIterator IdxEnd,
1538 const Twine &NameStr, BasicBlock *InsertAtEnd);
1540 // allocate space for exactly one operand
1541 void *operator new(size_t s) {
1542 return User::operator new(s, 1);
1546 template<typename InputIterator>
1547 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1548 InputIterator IdxEnd,
1549 const Twine &NameStr = "",
1550 Instruction *InsertBefore = 0) {
1552 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1554 template<typename InputIterator>
1555 static ExtractValueInst *Create(Value *Agg,
1556 InputIterator IdxBegin, InputIterator IdxEnd,
1557 const Twine &NameStr,
1558 BasicBlock *InsertAtEnd) {
1559 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1562 /// Constructors - These two creators are convenience methods because one
1563 /// index extractvalue instructions are much more common than those with
1565 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1566 const Twine &NameStr = "",
1567 Instruction *InsertBefore = 0) {
1568 unsigned Idxs[1] = { Idx };
1569 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1571 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1572 const Twine &NameStr,
1573 BasicBlock *InsertAtEnd) {
1574 unsigned Idxs[1] = { Idx };
1575 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1578 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1580 /// getIndexedType - Returns the type of the element that would be extracted
1581 /// with an extractvalue instruction with the specified parameters.
1583 /// Null is returned if the indices are invalid for the specified
1586 template<typename InputIterator>
1587 static const Type *getIndexedType(const Type *Ptr,
1588 InputIterator IdxBegin,
1589 InputIterator IdxEnd) {
1590 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1591 typename std::iterator_traits<InputIterator>::
1592 iterator_category());
1594 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1596 typedef const unsigned* idx_iterator;
1597 inline idx_iterator idx_begin() const { return Indices.begin(); }
1598 inline idx_iterator idx_end() const { return Indices.end(); }
1600 Value *getAggregateOperand() {
1601 return getOperand(0);
1603 const Value *getAggregateOperand() const {
1604 return getOperand(0);
1606 static unsigned getAggregateOperandIndex() {
1607 return 0U; // get index for modifying correct operand
1610 unsigned getNumIndices() const { // Note: always non-negative
1611 return (unsigned)Indices.size();
1614 bool hasIndices() const {
1618 // Methods for support type inquiry through isa, cast, and dyn_cast:
1619 static inline bool classof(const ExtractValueInst *) { return true; }
1620 static inline bool classof(const Instruction *I) {
1621 return I->getOpcode() == Instruction::ExtractValue;
1623 static inline bool classof(const Value *V) {
1624 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1628 template<typename InputIterator>
1629 ExtractValueInst::ExtractValueInst(Value *Agg,
1630 InputIterator IdxBegin,
1631 InputIterator IdxEnd,
1632 const Twine &NameStr,
1633 Instruction *InsertBefore)
1634 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1636 ExtractValue, Agg, InsertBefore) {
1637 init(IdxBegin, IdxEnd, NameStr,
1638 typename std::iterator_traits<InputIterator>::iterator_category());
1640 template<typename InputIterator>
1641 ExtractValueInst::ExtractValueInst(Value *Agg,
1642 InputIterator IdxBegin,
1643 InputIterator IdxEnd,
1644 const Twine &NameStr,
1645 BasicBlock *InsertAtEnd)
1646 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1648 ExtractValue, Agg, InsertAtEnd) {
1649 init(IdxBegin, IdxEnd, NameStr,
1650 typename std::iterator_traits<InputIterator>::iterator_category());
1654 //===----------------------------------------------------------------------===//
1655 // InsertValueInst Class
1656 //===----------------------------------------------------------------------===//
1658 /// InsertValueInst - This instruction inserts a struct field of array element
1659 /// value into an aggregate value.
1661 class InsertValueInst : public Instruction {
1662 SmallVector<unsigned, 4> Indices;
1664 void *operator new(size_t, unsigned); // Do not implement
1665 InsertValueInst(const InsertValueInst &IVI);
1666 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1667 const Twine &NameStr);
1668 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1670 template<typename InputIterator>
1671 void init(Value *Agg, Value *Val,
1672 InputIterator IdxBegin, InputIterator IdxEnd,
1673 const Twine &NameStr,
1674 // This argument ensures that we have an iterator we can
1675 // do arithmetic on in constant time
1676 std::random_access_iterator_tag) {
1677 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1679 // There's no fundamental reason why we require at least one index
1680 // (other than weirdness with &*IdxBegin being invalid; see
1681 // getelementptr's init routine for example). But there's no
1682 // present need to support it.
1683 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1685 // This requires that the iterator points to contiguous memory.
1686 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1687 // we have to build an array here
1690 /// Constructors - Create a insertvalue instruction with a base aggregate
1691 /// value, a value to insert, and a list of indices. The first ctor can
1692 /// optionally insert before an existing instruction, the second appends
1693 /// the new instruction to the specified BasicBlock.
1694 template<typename InputIterator>
1695 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1696 InputIterator IdxEnd,
1697 const Twine &NameStr,
1698 Instruction *InsertBefore);
1699 template<typename InputIterator>
1700 inline InsertValueInst(Value *Agg, Value *Val,
1701 InputIterator IdxBegin, InputIterator IdxEnd,
1702 const Twine &NameStr, BasicBlock *InsertAtEnd);
1704 /// Constructors - These two constructors are convenience methods because one
1705 /// and two index insertvalue instructions are so common.
1706 InsertValueInst(Value *Agg, Value *Val,
1707 unsigned Idx, const Twine &NameStr = "",
1708 Instruction *InsertBefore = 0);
1709 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1710 const Twine &NameStr, BasicBlock *InsertAtEnd);
1712 // allocate space for exactly two operands
1713 void *operator new(size_t s) {
1714 return User::operator new(s, 2);
1717 template<typename InputIterator>
1718 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1719 InputIterator IdxEnd,
1720 const Twine &NameStr = "",
1721 Instruction *InsertBefore = 0) {
1722 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1723 NameStr, InsertBefore);
1725 template<typename InputIterator>
1726 static InsertValueInst *Create(Value *Agg, Value *Val,
1727 InputIterator IdxBegin, InputIterator IdxEnd,
1728 const Twine &NameStr,
1729 BasicBlock *InsertAtEnd) {
1730 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1731 NameStr, InsertAtEnd);
1734 /// Constructors - These two creators are convenience methods because one
1735 /// index insertvalue instructions are much more common than those with
1737 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1738 const Twine &NameStr = "",
1739 Instruction *InsertBefore = 0) {
1740 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1742 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1743 const Twine &NameStr,
1744 BasicBlock *InsertAtEnd) {
1745 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1748 virtual InsertValueInst *clone(LLVMContext &Context) const;
1750 /// Transparently provide more efficient getOperand methods.
1751 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1753 typedef const unsigned* idx_iterator;
1754 inline idx_iterator idx_begin() const { return Indices.begin(); }
1755 inline idx_iterator idx_end() const { return Indices.end(); }
1757 Value *getAggregateOperand() {
1758 return getOperand(0);
1760 const Value *getAggregateOperand() const {
1761 return getOperand(0);
1763 static unsigned getAggregateOperandIndex() {
1764 return 0U; // get index for modifying correct operand
1767 Value *getInsertedValueOperand() {
1768 return getOperand(1);
1770 const Value *getInsertedValueOperand() const {
1771 return getOperand(1);
1773 static unsigned getInsertedValueOperandIndex() {
1774 return 1U; // get index for modifying correct operand
1777 unsigned getNumIndices() const { // Note: always non-negative
1778 return (unsigned)Indices.size();
1781 bool hasIndices() const {
1785 // Methods for support type inquiry through isa, cast, and dyn_cast:
1786 static inline bool classof(const InsertValueInst *) { return true; }
1787 static inline bool classof(const Instruction *I) {
1788 return I->getOpcode() == Instruction::InsertValue;
1790 static inline bool classof(const Value *V) {
1791 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1796 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1799 template<typename InputIterator>
1800 InsertValueInst::InsertValueInst(Value *Agg,
1802 InputIterator IdxBegin,
1803 InputIterator IdxEnd,
1804 const Twine &NameStr,
1805 Instruction *InsertBefore)
1806 : Instruction(Agg->getType(), InsertValue,
1807 OperandTraits<InsertValueInst>::op_begin(this),
1809 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1810 typename std::iterator_traits<InputIterator>::iterator_category());
1812 template<typename InputIterator>
1813 InsertValueInst::InsertValueInst(Value *Agg,
1815 InputIterator IdxBegin,
1816 InputIterator IdxEnd,
1817 const Twine &NameStr,
1818 BasicBlock *InsertAtEnd)
1819 : Instruction(Agg->getType(), InsertValue,
1820 OperandTraits<InsertValueInst>::op_begin(this),
1822 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1823 typename std::iterator_traits<InputIterator>::iterator_category());
1826 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1828 //===----------------------------------------------------------------------===//
1830 //===----------------------------------------------------------------------===//
1832 // PHINode - The PHINode class is used to represent the magical mystical PHI
1833 // node, that can not exist in nature, but can be synthesized in a computer
1834 // scientist's overactive imagination.
1836 class PHINode : public Instruction {
1837 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1838 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1839 /// the number actually in use.
1840 unsigned ReservedSpace;
1841 PHINode(const PHINode &PN);
1842 // allocate space for exactly zero operands
1843 void *operator new(size_t s) {
1844 return User::operator new(s, 0);
1846 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1847 Instruction *InsertBefore = 0)
1848 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1853 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1854 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1859 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1860 Instruction *InsertBefore = 0) {
1861 return new PHINode(Ty, NameStr, InsertBefore);
1863 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1864 BasicBlock *InsertAtEnd) {
1865 return new PHINode(Ty, NameStr, InsertAtEnd);
1869 /// reserveOperandSpace - This method can be used to avoid repeated
1870 /// reallocation of PHI operand lists by reserving space for the correct
1871 /// number of operands before adding them. Unlike normal vector reserves,
1872 /// this method can also be used to trim the operand space.
1873 void reserveOperandSpace(unsigned NumValues) {
1874 resizeOperands(NumValues*2);
1877 virtual PHINode *clone(LLVMContext &Context) const;
1879 /// Provide fast operand accessors
1880 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1882 /// getNumIncomingValues - Return the number of incoming edges
1884 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1886 /// getIncomingValue - Return incoming value number x
1888 Value *getIncomingValue(unsigned i) const {
1889 assert(i*2 < getNumOperands() && "Invalid value number!");
1890 return getOperand(i*2);
1892 void setIncomingValue(unsigned i, Value *V) {
1893 assert(i*2 < getNumOperands() && "Invalid value number!");
1896 static unsigned getOperandNumForIncomingValue(unsigned i) {
1899 static unsigned getIncomingValueNumForOperand(unsigned i) {
1900 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1904 /// getIncomingBlock - Return incoming basic block corresponding
1905 /// to value use iterator
1907 template <typename U>
1908 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1909 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1910 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1912 /// getIncomingBlock - Return incoming basic block number x
1914 BasicBlock *getIncomingBlock(unsigned i) const {
1915 return static_cast<BasicBlock*>(getOperand(i*2+1));
1917 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1918 setOperand(i*2+1, BB);
1920 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1923 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1924 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1928 /// addIncoming - Add an incoming value to the end of the PHI list
1930 void addIncoming(Value *V, BasicBlock *BB) {
1931 assert(V && "PHI node got a null value!");
1932 assert(BB && "PHI node got a null basic block!");
1933 assert(getType() == V->getType() &&
1934 "All operands to PHI node must be the same type as the PHI node!");
1935 unsigned OpNo = NumOperands;
1936 if (OpNo+2 > ReservedSpace)
1937 resizeOperands(0); // Get more space!
1938 // Initialize some new operands.
1939 NumOperands = OpNo+2;
1940 OperandList[OpNo] = V;
1941 OperandList[OpNo+1] = BB;
1944 /// removeIncomingValue - Remove an incoming value. This is useful if a
1945 /// predecessor basic block is deleted. The value removed is returned.
1947 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1948 /// is true), the PHI node is destroyed and any uses of it are replaced with
1949 /// dummy values. The only time there should be zero incoming values to a PHI
1950 /// node is when the block is dead, so this strategy is sound.
1952 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1954 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1955 int Idx = getBasicBlockIndex(BB);
1956 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1957 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1960 /// getBasicBlockIndex - Return the first index of the specified basic
1961 /// block in the value list for this PHI. Returns -1 if no instance.
1963 int getBasicBlockIndex(const BasicBlock *BB) const {
1964 Use *OL = OperandList;
1965 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1966 if (OL[i+1].get() == BB) return i/2;
1970 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1971 return getIncomingValue(getBasicBlockIndex(BB));
1974 /// hasConstantValue - If the specified PHI node always merges together the
1975 /// same value, return the value, otherwise return null.
1977 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1979 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1980 static inline bool classof(const PHINode *) { return true; }
1981 static inline bool classof(const Instruction *I) {
1982 return I->getOpcode() == Instruction::PHI;
1984 static inline bool classof(const Value *V) {
1985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1988 void resizeOperands(unsigned NumOperands);
1992 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1995 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1998 //===----------------------------------------------------------------------===//
2000 //===----------------------------------------------------------------------===//
2002 //===---------------------------------------------------------------------------
2003 /// ReturnInst - Return a value (possibly void), from a function. Execution
2004 /// does not continue in this function any longer.
2006 class ReturnInst : public TerminatorInst {
2007 ReturnInst(const ReturnInst &RI);
2010 // ReturnInst constructors:
2011 // ReturnInst() - 'ret void' instruction
2012 // ReturnInst( null) - 'ret void' instruction
2013 // ReturnInst(Value* X) - 'ret X' instruction
2014 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2015 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2016 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2017 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2019 // NOTE: If the Value* passed is of type void then the constructor behaves as
2020 // if it was passed NULL.
2021 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2022 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2023 explicit ReturnInst(BasicBlock *InsertAtEnd);
2025 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2026 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2028 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2029 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2031 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2032 return new(0) ReturnInst(InsertAtEnd);
2034 virtual ~ReturnInst();
2036 virtual ReturnInst *clone(LLVMContext &Context) const;
2038 /// Provide fast operand accessors
2039 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2041 /// Convenience accessor
2042 Value *getReturnValue(unsigned n = 0) const {
2043 return n < getNumOperands()
2048 unsigned getNumSuccessors() const { return 0; }
2050 // Methods for support type inquiry through isa, cast, and dyn_cast:
2051 static inline bool classof(const ReturnInst *) { return true; }
2052 static inline bool classof(const Instruction *I) {
2053 return (I->getOpcode() == Instruction::Ret);
2055 static inline bool classof(const Value *V) {
2056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2059 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2060 virtual unsigned getNumSuccessorsV() const;
2061 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2065 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2068 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2070 //===----------------------------------------------------------------------===//
2072 //===----------------------------------------------------------------------===//
2074 //===---------------------------------------------------------------------------
2075 /// BranchInst - Conditional or Unconditional Branch instruction.
2077 class BranchInst : public TerminatorInst {
2078 /// Ops list - Branches are strange. The operands are ordered:
2079 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2080 /// they don't have to check for cond/uncond branchness. These are mostly
2081 /// accessed relative from op_end().
2082 BranchInst(const BranchInst &BI);
2084 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2085 // BranchInst(BB *B) - 'br B'
2086 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2087 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2088 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2089 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2090 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2091 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2092 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2093 Instruction *InsertBefore = 0);
2094 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2095 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2096 BasicBlock *InsertAtEnd);
2098 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2099 return new(1, true) BranchInst(IfTrue, InsertBefore);
2101 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2102 Value *Cond, Instruction *InsertBefore = 0) {
2103 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2105 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2106 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2108 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2109 Value *Cond, BasicBlock *InsertAtEnd) {
2110 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2115 /// Transparently provide more efficient getOperand methods.
2116 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2118 virtual BranchInst *clone(LLVMContext &Context) const;
2120 bool isUnconditional() const { return getNumOperands() == 1; }
2121 bool isConditional() const { return getNumOperands() == 3; }
2123 Value *getCondition() const {
2124 assert(isConditional() && "Cannot get condition of an uncond branch!");
2128 void setCondition(Value *V) {
2129 assert(isConditional() && "Cannot set condition of unconditional branch!");
2133 // setUnconditionalDest - Change the current branch to an unconditional branch
2134 // targeting the specified block.
2135 // FIXME: Eliminate this ugly method.
2136 void setUnconditionalDest(BasicBlock *Dest) {
2138 if (isConditional()) { // Convert this to an uncond branch.
2142 OperandList = op_begin();
2146 unsigned getNumSuccessors() const { return 1+isConditional(); }
2148 BasicBlock *getSuccessor(unsigned i) const {
2149 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2150 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2153 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2154 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2155 *(&Op<-1>() - idx) = NewSucc;
2158 // Methods for support type inquiry through isa, cast, and dyn_cast:
2159 static inline bool classof(const BranchInst *) { return true; }
2160 static inline bool classof(const Instruction *I) {
2161 return (I->getOpcode() == Instruction::Br);
2163 static inline bool classof(const Value *V) {
2164 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2167 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2168 virtual unsigned getNumSuccessorsV() const;
2169 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2173 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2175 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2177 //===----------------------------------------------------------------------===//
2179 //===----------------------------------------------------------------------===//
2181 //===---------------------------------------------------------------------------
2182 /// SwitchInst - Multiway switch
2184 class SwitchInst : public TerminatorInst {
2185 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2186 unsigned ReservedSpace;
2187 // Operand[0] = Value to switch on
2188 // Operand[1] = Default basic block destination
2189 // Operand[2n ] = Value to match
2190 // Operand[2n+1] = BasicBlock to go to on match
2191 SwitchInst(const SwitchInst &RI);
2192 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2193 void resizeOperands(unsigned No);
2194 // allocate space for exactly zero operands
2195 void *operator new(size_t s) {
2196 return User::operator new(s, 0);
2198 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2199 /// switch on and a default destination. The number of additional cases can
2200 /// be specified here to make memory allocation more efficient. This
2201 /// constructor can also autoinsert before another instruction.
2202 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2203 Instruction *InsertBefore = 0);
2205 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2206 /// switch on and a default destination. The number of additional cases can
2207 /// be specified here to make memory allocation more efficient. This
2208 /// constructor also autoinserts at the end of the specified BasicBlock.
2209 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2210 BasicBlock *InsertAtEnd);
2212 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2213 unsigned NumCases, Instruction *InsertBefore = 0) {
2214 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2216 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2217 unsigned NumCases, BasicBlock *InsertAtEnd) {
2218 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2222 /// Provide fast operand accessors
2223 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2225 // Accessor Methods for Switch stmt
2226 Value *getCondition() const { return getOperand(0); }
2227 void setCondition(Value *V) { setOperand(0, V); }
2229 BasicBlock *getDefaultDest() const {
2230 return cast<BasicBlock>(getOperand(1));
2233 /// getNumCases - return the number of 'cases' in this switch instruction.
2234 /// Note that case #0 is always the default case.
2235 unsigned getNumCases() const {
2236 return getNumOperands()/2;
2239 /// getCaseValue - Return the specified case value. Note that case #0, the
2240 /// default destination, does not have a case value.
2241 ConstantInt *getCaseValue(unsigned i) {
2242 assert(i && i < getNumCases() && "Illegal case value to get!");
2243 return getSuccessorValue(i);
2246 /// getCaseValue - Return the specified case value. Note that case #0, the
2247 /// default destination, does not have a case value.
2248 const ConstantInt *getCaseValue(unsigned i) const {
2249 assert(i && i < getNumCases() && "Illegal case value to get!");
2250 return getSuccessorValue(i);
2253 /// findCaseValue - Search all of the case values for the specified constant.
2254 /// If it is explicitly handled, return the case number of it, otherwise
2255 /// return 0 to indicate that it is handled by the default handler.
2256 unsigned findCaseValue(const ConstantInt *C) const {
2257 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2258 if (getCaseValue(i) == C)
2263 /// findCaseDest - Finds the unique case value for a given successor. Returns
2264 /// null if the successor is not found, not unique, or is the default case.
2265 ConstantInt *findCaseDest(BasicBlock *BB) {
2266 if (BB == getDefaultDest()) return NULL;
2268 ConstantInt *CI = NULL;
2269 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2270 if (getSuccessor(i) == BB) {
2271 if (CI) return NULL; // Multiple cases lead to BB.
2272 else CI = getCaseValue(i);
2278 /// addCase - Add an entry to the switch instruction...
2280 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2282 /// removeCase - This method removes the specified successor from the switch
2283 /// instruction. Note that this cannot be used to remove the default
2284 /// destination (successor #0).
2286 void removeCase(unsigned idx);
2288 virtual SwitchInst *clone(LLVMContext &Context) const;
2290 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2291 BasicBlock *getSuccessor(unsigned idx) const {
2292 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2293 return cast<BasicBlock>(getOperand(idx*2+1));
2295 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2296 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2297 setOperand(idx*2+1, NewSucc);
2300 // getSuccessorValue - Return the value associated with the specified
2302 ConstantInt *getSuccessorValue(unsigned idx) const {
2303 assert(idx < getNumSuccessors() && "Successor # out of range!");
2304 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2307 // Methods for support type inquiry through isa, cast, and dyn_cast:
2308 static inline bool classof(const SwitchInst *) { return true; }
2309 static inline bool classof(const Instruction *I) {
2310 return I->getOpcode() == Instruction::Switch;
2312 static inline bool classof(const Value *V) {
2313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2316 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2317 virtual unsigned getNumSuccessorsV() const;
2318 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2322 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2325 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2328 //===----------------------------------------------------------------------===//
2330 //===----------------------------------------------------------------------===//
2332 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2333 /// calling convention of the call.
2335 class InvokeInst : public TerminatorInst {
2336 AttrListPtr AttributeList;
2337 InvokeInst(const InvokeInst &BI);
2338 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2339 Value* const *Args, unsigned NumArgs);
2341 template<typename InputIterator>
2342 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2343 InputIterator ArgBegin, InputIterator ArgEnd,
2344 const Twine &NameStr,
2345 // This argument ensures that we have an iterator we can
2346 // do arithmetic on in constant time
2347 std::random_access_iterator_tag) {
2348 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2350 // This requires that the iterator points to contiguous memory.
2351 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2355 /// Construct an InvokeInst given a range of arguments.
2356 /// InputIterator must be a random-access iterator pointing to
2357 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2358 /// made for random-accessness but not for contiguous storage as
2359 /// that would incur runtime overhead.
2361 /// @brief Construct an InvokeInst from a range of arguments
2362 template<typename InputIterator>
2363 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2364 InputIterator ArgBegin, InputIterator ArgEnd,
2366 const Twine &NameStr, Instruction *InsertBefore);
2368 /// Construct an InvokeInst given a range of arguments.
2369 /// InputIterator must be a random-access iterator pointing to
2370 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2371 /// made for random-accessness but not for contiguous storage as
2372 /// that would incur runtime overhead.
2374 /// @brief Construct an InvokeInst from a range of arguments
2375 template<typename InputIterator>
2376 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2377 InputIterator ArgBegin, InputIterator ArgEnd,
2379 const Twine &NameStr, BasicBlock *InsertAtEnd);
2381 template<typename InputIterator>
2382 static InvokeInst *Create(Value *Func,
2383 BasicBlock *IfNormal, BasicBlock *IfException,
2384 InputIterator ArgBegin, InputIterator ArgEnd,
2385 const Twine &NameStr = "",
2386 Instruction *InsertBefore = 0) {
2387 unsigned Values(ArgEnd - ArgBegin + 3);
2388 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2389 Values, NameStr, InsertBefore);
2391 template<typename InputIterator>
2392 static InvokeInst *Create(Value *Func,
2393 BasicBlock *IfNormal, BasicBlock *IfException,
2394 InputIterator ArgBegin, InputIterator ArgEnd,
2395 const Twine &NameStr,
2396 BasicBlock *InsertAtEnd) {
2397 unsigned Values(ArgEnd - ArgBegin + 3);
2398 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2399 Values, NameStr, InsertAtEnd);
2402 virtual InvokeInst *clone(LLVMContext &Context) const;
2404 /// Provide fast operand accessors
2405 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2407 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2409 unsigned getCallingConv() const { return SubclassData; }
2410 void setCallingConv(unsigned CC) {
2414 /// getAttributes - Return the parameter attributes for this invoke.
2416 const AttrListPtr &getAttributes() const { return AttributeList; }
2418 /// setAttributes - Set the parameter attributes for this invoke.
2420 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2422 /// addAttribute - adds the attribute to the list of attributes.
2423 void addAttribute(unsigned i, Attributes attr);
2425 /// removeAttribute - removes the attribute from the list of attributes.
2426 void removeAttribute(unsigned i, Attributes attr);
2428 /// @brief Determine whether the call or the callee has the given attribute.
2429 bool paramHasAttr(unsigned i, Attributes attr) const;
2431 /// @brief Extract the alignment for a call or parameter (0=unknown).
2432 unsigned getParamAlignment(unsigned i) const {
2433 return AttributeList.getParamAlignment(i);
2436 /// @brief Determine if the call does not access memory.
2437 bool doesNotAccessMemory() const {
2438 return paramHasAttr(~0, Attribute::ReadNone);
2440 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2441 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2442 else removeAttribute(~0, Attribute::ReadNone);
2445 /// @brief Determine if the call does not access or only reads memory.
2446 bool onlyReadsMemory() const {
2447 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2449 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2450 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2451 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2454 /// @brief Determine if the call cannot return.
2455 bool doesNotReturn() const {
2456 return paramHasAttr(~0, Attribute::NoReturn);
2458 void setDoesNotReturn(bool DoesNotReturn = true) {
2459 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2460 else removeAttribute(~0, Attribute::NoReturn);
2463 /// @brief Determine if the call cannot unwind.
2464 bool doesNotThrow() const {
2465 return paramHasAttr(~0, Attribute::NoUnwind);
2467 void setDoesNotThrow(bool DoesNotThrow = true) {
2468 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2469 else removeAttribute(~0, Attribute::NoUnwind);
2472 /// @brief Determine if the call returns a structure through first
2473 /// pointer argument.
2474 bool hasStructRetAttr() const {
2475 // Be friendly and also check the callee.
2476 return paramHasAttr(1, Attribute::StructRet);
2479 /// @brief Determine if any call argument is an aggregate passed by value.
2480 bool hasByValArgument() const {
2481 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2484 /// getCalledFunction - Return the function called, or null if this is an
2485 /// indirect function invocation.
2487 Function *getCalledFunction() const {
2488 return dyn_cast<Function>(getOperand(0));
2491 /// getCalledValue - Get a pointer to the function that is invoked by this
2493 const Value *getCalledValue() const { return getOperand(0); }
2494 Value *getCalledValue() { return getOperand(0); }
2496 // get*Dest - Return the destination basic blocks...
2497 BasicBlock *getNormalDest() const {
2498 return cast<BasicBlock>(getOperand(1));
2500 BasicBlock *getUnwindDest() const {
2501 return cast<BasicBlock>(getOperand(2));
2503 void setNormalDest(BasicBlock *B) {
2507 void setUnwindDest(BasicBlock *B) {
2511 BasicBlock *getSuccessor(unsigned i) const {
2512 assert(i < 2 && "Successor # out of range for invoke!");
2513 return i == 0 ? getNormalDest() : getUnwindDest();
2516 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2517 assert(idx < 2 && "Successor # out of range for invoke!");
2518 setOperand(idx+1, NewSucc);
2521 unsigned getNumSuccessors() const { return 2; }
2523 // Methods for support type inquiry through isa, cast, and dyn_cast:
2524 static inline bool classof(const InvokeInst *) { return true; }
2525 static inline bool classof(const Instruction *I) {
2526 return (I->getOpcode() == Instruction::Invoke);
2528 static inline bool classof(const Value *V) {
2529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2532 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2533 virtual unsigned getNumSuccessorsV() const;
2534 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2538 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2541 template<typename InputIterator>
2542 InvokeInst::InvokeInst(Value *Func,
2543 BasicBlock *IfNormal, BasicBlock *IfException,
2544 InputIterator ArgBegin, InputIterator ArgEnd,
2546 const Twine &NameStr, Instruction *InsertBefore)
2547 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2548 ->getElementType())->getReturnType(),
2549 Instruction::Invoke,
2550 OperandTraits<InvokeInst>::op_end(this) - Values,
2551 Values, InsertBefore) {
2552 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2553 typename std::iterator_traits<InputIterator>::iterator_category());
2555 template<typename InputIterator>
2556 InvokeInst::InvokeInst(Value *Func,
2557 BasicBlock *IfNormal, BasicBlock *IfException,
2558 InputIterator ArgBegin, InputIterator ArgEnd,
2560 const Twine &NameStr, BasicBlock *InsertAtEnd)
2561 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2562 ->getElementType())->getReturnType(),
2563 Instruction::Invoke,
2564 OperandTraits<InvokeInst>::op_end(this) - Values,
2565 Values, InsertAtEnd) {
2566 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2567 typename std::iterator_traits<InputIterator>::iterator_category());
2570 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2572 //===----------------------------------------------------------------------===//
2574 //===----------------------------------------------------------------------===//
2576 //===---------------------------------------------------------------------------
2577 /// UnwindInst - Immediately exit the current function, unwinding the stack
2578 /// until an invoke instruction is found.
2580 class UnwindInst : public TerminatorInst {
2581 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2583 // allocate space for exactly zero operands
2584 void *operator new(size_t s) {
2585 return User::operator new(s, 0);
2587 explicit UnwindInst(Instruction *InsertBefore = 0);
2588 explicit UnwindInst(BasicBlock *InsertAtEnd);
2590 virtual UnwindInst *clone(LLVMContext &Context) const;
2592 unsigned getNumSuccessors() const { return 0; }
2594 // Methods for support type inquiry through isa, cast, and dyn_cast:
2595 static inline bool classof(const UnwindInst *) { return true; }
2596 static inline bool classof(const Instruction *I) {
2597 return I->getOpcode() == Instruction::Unwind;
2599 static inline bool classof(const Value *V) {
2600 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2603 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2604 virtual unsigned getNumSuccessorsV() const;
2605 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2608 //===----------------------------------------------------------------------===//
2609 // UnreachableInst Class
2610 //===----------------------------------------------------------------------===//
2612 //===---------------------------------------------------------------------------
2613 /// UnreachableInst - This function has undefined behavior. In particular, the
2614 /// presence of this instruction indicates some higher level knowledge that the
2615 /// end of the block cannot be reached.
2617 class UnreachableInst : public TerminatorInst {
2618 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2620 // allocate space for exactly zero operands
2621 void *operator new(size_t s) {
2622 return User::operator new(s, 0);
2624 explicit UnreachableInst(Instruction *InsertBefore = 0);
2625 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2627 virtual UnreachableInst *clone(LLVMContext &Context) const;
2629 unsigned getNumSuccessors() const { return 0; }
2631 // Methods for support type inquiry through isa, cast, and dyn_cast:
2632 static inline bool classof(const UnreachableInst *) { return true; }
2633 static inline bool classof(const Instruction *I) {
2634 return I->getOpcode() == Instruction::Unreachable;
2636 static inline bool classof(const Value *V) {
2637 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2640 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2641 virtual unsigned getNumSuccessorsV() const;
2642 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2645 //===----------------------------------------------------------------------===//
2647 //===----------------------------------------------------------------------===//
2649 /// @brief This class represents a truncation of integer types.
2650 class TruncInst : public CastInst {
2651 /// Private copy constructor
2652 TruncInst(const TruncInst &CI)
2653 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2656 /// @brief Constructor with insert-before-instruction semantics
2658 Value *S, ///< The value to be truncated
2659 const Type *Ty, ///< The (smaller) type to truncate to
2660 const Twine &NameStr = "", ///< A name for the new instruction
2661 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2664 /// @brief Constructor with insert-at-end-of-block semantics
2666 Value *S, ///< The value to be truncated
2667 const Type *Ty, ///< The (smaller) type to truncate to
2668 const Twine &NameStr, ///< A name for the new instruction
2669 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2672 /// @brief Clone an identical TruncInst
2673 virtual CastInst *clone(LLVMContext &Context) const;
2675 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2676 static inline bool classof(const TruncInst *) { return true; }
2677 static inline bool classof(const Instruction *I) {
2678 return I->getOpcode() == Trunc;
2680 static inline bool classof(const Value *V) {
2681 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2685 //===----------------------------------------------------------------------===//
2687 //===----------------------------------------------------------------------===//
2689 /// @brief This class represents zero extension of integer types.
2690 class ZExtInst : public CastInst {
2691 /// @brief Private copy constructor
2692 ZExtInst(const ZExtInst &CI)
2693 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2696 /// @brief Constructor with insert-before-instruction semantics
2698 Value *S, ///< The value to be zero extended
2699 const Type *Ty, ///< The type to zero 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 semantics.
2706 Value *S, ///< The value to be zero extended
2707 const Type *Ty, ///< The type to zero 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 ZExtInst
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 ZExtInst *) { return true; }
2717 static inline bool classof(const Instruction *I) {
2718 return I->getOpcode() == ZExt;
2720 static inline bool classof(const Value *V) {
2721 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2725 //===----------------------------------------------------------------------===//
2727 //===----------------------------------------------------------------------===//
2729 /// @brief This class represents a sign extension of integer types.
2730 class SExtInst : public CastInst {
2731 /// @brief Private copy constructor
2732 SExtInst(const SExtInst &CI)
2733 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2736 /// @brief Constructor with insert-before-instruction semantics
2738 Value *S, ///< The value to be sign extended
2739 const Type *Ty, ///< The type to sign extend to
2740 const Twine &NameStr = "", ///< A name for the new instruction
2741 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2744 /// @brief Constructor with insert-at-end-of-block semantics
2746 Value *S, ///< The value to be sign extended
2747 const Type *Ty, ///< The type to sign extend to
2748 const Twine &NameStr, ///< A name for the new instruction
2749 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2752 /// @brief Clone an identical SExtInst
2753 virtual CastInst *clone(LLVMContext &Context) const;
2755 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2756 static inline bool classof(const SExtInst *) { return true; }
2757 static inline bool classof(const Instruction *I) {
2758 return I->getOpcode() == SExt;
2760 static inline bool classof(const Value *V) {
2761 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2765 //===----------------------------------------------------------------------===//
2766 // FPTruncInst Class
2767 //===----------------------------------------------------------------------===//
2769 /// @brief This class represents a truncation of floating point types.
2770 class FPTruncInst : public CastInst {
2771 FPTruncInst(const FPTruncInst &CI)
2772 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2775 /// @brief Constructor with insert-before-instruction semantics
2777 Value *S, ///< The value to be truncated
2778 const Type *Ty, ///< The type to truncate to
2779 const Twine &NameStr = "", ///< A name for the new instruction
2780 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2783 /// @brief Constructor with insert-before-instruction semantics
2785 Value *S, ///< The value to be truncated
2786 const Type *Ty, ///< The type to truncate to
2787 const Twine &NameStr, ///< A name for the new instruction
2788 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2791 /// @brief Clone an identical FPTruncInst
2792 virtual CastInst *clone(LLVMContext &Context) const;
2794 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2795 static inline bool classof(const FPTruncInst *) { return true; }
2796 static inline bool classof(const Instruction *I) {
2797 return I->getOpcode() == FPTrunc;
2799 static inline bool classof(const Value *V) {
2800 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2804 //===----------------------------------------------------------------------===//
2806 //===----------------------------------------------------------------------===//
2808 /// @brief This class represents an extension of floating point types.
2809 class FPExtInst : public CastInst {
2810 FPExtInst(const FPExtInst &CI)
2811 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2814 /// @brief Constructor with insert-before-instruction semantics
2816 Value *S, ///< The value to be extended
2817 const Type *Ty, ///< The type to extend to
2818 const Twine &NameStr = "", ///< A name for the new instruction
2819 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2822 /// @brief Constructor with insert-at-end-of-block semantics
2824 Value *S, ///< The value to be extended
2825 const Type *Ty, ///< The type to extend to
2826 const Twine &NameStr, ///< A name for the new instruction
2827 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2830 /// @brief Clone an identical FPExtInst
2831 virtual CastInst *clone(LLVMContext &Context) const;
2833 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2834 static inline bool classof(const FPExtInst *) { return true; }
2835 static inline bool classof(const Instruction *I) {
2836 return I->getOpcode() == FPExt;
2838 static inline bool classof(const Value *V) {
2839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2843 //===----------------------------------------------------------------------===//
2845 //===----------------------------------------------------------------------===//
2847 /// @brief This class represents a cast unsigned integer to floating point.
2848 class UIToFPInst : public CastInst {
2849 UIToFPInst(const UIToFPInst &CI)
2850 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2853 /// @brief Constructor with insert-before-instruction semantics
2855 Value *S, ///< The value to be converted
2856 const Type *Ty, ///< The type to convert to
2857 const Twine &NameStr = "", ///< A name for the new instruction
2858 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2861 /// @brief Constructor with insert-at-end-of-block semantics
2863 Value *S, ///< The value to be converted
2864 const Type *Ty, ///< The type to convert to
2865 const Twine &NameStr, ///< A name for the new instruction
2866 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2869 /// @brief Clone an identical UIToFPInst
2870 virtual CastInst *clone(LLVMContext &Context) const;
2872 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2873 static inline bool classof(const UIToFPInst *) { return true; }
2874 static inline bool classof(const Instruction *I) {
2875 return I->getOpcode() == UIToFP;
2877 static inline bool classof(const Value *V) {
2878 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2882 //===----------------------------------------------------------------------===//
2884 //===----------------------------------------------------------------------===//
2886 /// @brief This class represents a cast from signed integer to floating point.
2887 class SIToFPInst : public CastInst {
2888 SIToFPInst(const SIToFPInst &CI)
2889 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2892 /// @brief Constructor with insert-before-instruction semantics
2894 Value *S, ///< The value to be converted
2895 const Type *Ty, ///< The type to convert to
2896 const Twine &NameStr = "", ///< A name for the new instruction
2897 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2900 /// @brief Constructor with insert-at-end-of-block semantics
2902 Value *S, ///< The value to be converted
2903 const Type *Ty, ///< The type to convert to
2904 const Twine &NameStr, ///< A name for the new instruction
2905 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2908 /// @brief Clone an identical SIToFPInst
2909 virtual CastInst *clone(LLVMContext &Context) const;
2911 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2912 static inline bool classof(const SIToFPInst *) { return true; }
2913 static inline bool classof(const Instruction *I) {
2914 return I->getOpcode() == SIToFP;
2916 static inline bool classof(const Value *V) {
2917 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2921 //===----------------------------------------------------------------------===//
2923 //===----------------------------------------------------------------------===//
2925 /// @brief This class represents a cast from floating point to unsigned integer
2926 class FPToUIInst : public CastInst {
2927 FPToUIInst(const FPToUIInst &CI)
2928 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2931 /// @brief Constructor with insert-before-instruction semantics
2933 Value *S, ///< The value to be converted
2934 const Type *Ty, ///< The type to convert to
2935 const Twine &NameStr = "", ///< A name for the new instruction
2936 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2939 /// @brief Constructor with insert-at-end-of-block semantics
2941 Value *S, ///< The value to be converted
2942 const Type *Ty, ///< The type to convert to
2943 const Twine &NameStr, ///< A name for the new instruction
2944 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2947 /// @brief Clone an identical FPToUIInst
2948 virtual CastInst *clone(LLVMContext &Context) const;
2950 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2951 static inline bool classof(const FPToUIInst *) { return true; }
2952 static inline bool classof(const Instruction *I) {
2953 return I->getOpcode() == FPToUI;
2955 static inline bool classof(const Value *V) {
2956 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2960 //===----------------------------------------------------------------------===//
2962 //===----------------------------------------------------------------------===//
2964 /// @brief This class represents a cast from floating point to signed integer.
2965 class FPToSIInst : public CastInst {
2966 FPToSIInst(const FPToSIInst &CI)
2967 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2970 /// @brief Constructor with insert-before-instruction semantics
2972 Value *S, ///< The value to be converted
2973 const Type *Ty, ///< The type to convert to
2974 const Twine &NameStr = "", ///< A name for the new instruction
2975 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2978 /// @brief Constructor with insert-at-end-of-block semantics
2980 Value *S, ///< The value to be converted
2981 const Type *Ty, ///< The type to convert to
2982 const Twine &NameStr, ///< A name for the new instruction
2983 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2986 /// @brief Clone an identical FPToSIInst
2987 virtual CastInst *clone(LLVMContext &Context) const;
2989 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2990 static inline bool classof(const FPToSIInst *) { return true; }
2991 static inline bool classof(const Instruction *I) {
2992 return I->getOpcode() == FPToSI;
2994 static inline bool classof(const Value *V) {
2995 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2999 //===----------------------------------------------------------------------===//
3000 // IntToPtrInst Class
3001 //===----------------------------------------------------------------------===//
3003 /// @brief This class represents a cast from an integer to a pointer.
3004 class IntToPtrInst : public CastInst {
3005 IntToPtrInst(const IntToPtrInst &CI)
3006 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3009 /// @brief Constructor with insert-before-instruction semantics
3011 Value *S, ///< The value to be converted
3012 const Type *Ty, ///< The type to convert to
3013 const Twine &NameStr = "", ///< A name for the new instruction
3014 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3017 /// @brief Constructor with insert-at-end-of-block semantics
3019 Value *S, ///< The value to be converted
3020 const Type *Ty, ///< The type to convert to
3021 const Twine &NameStr, ///< A name for the new instruction
3022 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3025 /// @brief Clone an identical IntToPtrInst
3026 virtual CastInst *clone(LLVMContext &Context) const;
3028 // Methods for support type inquiry through isa, cast, and dyn_cast:
3029 static inline bool classof(const IntToPtrInst *) { return true; }
3030 static inline bool classof(const Instruction *I) {
3031 return I->getOpcode() == IntToPtr;
3033 static inline bool classof(const Value *V) {
3034 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3038 //===----------------------------------------------------------------------===//
3039 // PtrToIntInst Class
3040 //===----------------------------------------------------------------------===//
3042 /// @brief This class represents a cast from a pointer to an integer
3043 class PtrToIntInst : public CastInst {
3044 PtrToIntInst(const PtrToIntInst &CI)
3045 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3048 /// @brief Constructor with insert-before-instruction semantics
3050 Value *S, ///< The value to be converted
3051 const Type *Ty, ///< The type to convert to
3052 const Twine &NameStr = "", ///< A name for the new instruction
3053 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3056 /// @brief Constructor with insert-at-end-of-block semantics
3058 Value *S, ///< The value to be converted
3059 const Type *Ty, ///< The type to convert to
3060 const Twine &NameStr, ///< A name for the new instruction
3061 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3064 /// @brief Clone an identical PtrToIntInst
3065 virtual CastInst *clone(LLVMContext &Context) const;
3067 // Methods for support type inquiry through isa, cast, and dyn_cast:
3068 static inline bool classof(const PtrToIntInst *) { return true; }
3069 static inline bool classof(const Instruction *I) {
3070 return I->getOpcode() == PtrToInt;
3072 static inline bool classof(const Value *V) {
3073 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3077 //===----------------------------------------------------------------------===//
3078 // BitCastInst Class
3079 //===----------------------------------------------------------------------===//
3081 /// @brief This class represents a no-op cast from one type to another.
3082 class BitCastInst : public CastInst {
3083 BitCastInst(const BitCastInst &CI)
3084 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3087 /// @brief Constructor with insert-before-instruction semantics
3089 Value *S, ///< The value to be casted
3090 const Type *Ty, ///< The type to casted to
3091 const Twine &NameStr = "", ///< A name for the new instruction
3092 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3095 /// @brief Constructor with insert-at-end-of-block semantics
3097 Value *S, ///< The value to be casted
3098 const Type *Ty, ///< The type to casted to
3099 const Twine &NameStr, ///< A name for the new instruction
3100 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3103 /// @brief Clone an identical BitCastInst
3104 virtual CastInst *clone(LLVMContext &Context) const;
3106 // Methods for support type inquiry through isa, cast, and dyn_cast:
3107 static inline bool classof(const BitCastInst *) { return true; }
3108 static inline bool classof(const Instruction *I) {
3109 return I->getOpcode() == BitCast;
3111 static inline bool classof(const Value *V) {
3112 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3116 } // End llvm namespace