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 std::string &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const std::string &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 std::string &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const std::string &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const std::string &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const std::string &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const std::string &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const std::string &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 std::string &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const std::string &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const std::string &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const std::string &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const std::string &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 std::string &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 std::string &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
261 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
262 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
263 bool isVolatile = false, Instruction *InsertBefore = 0);
264 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
265 BasicBlock *InsertAtEnd);
267 /// isVolatile - Return true if this is a load from a volatile memory
270 bool isVolatile() const { return SubclassData & 1; }
272 /// setVolatile - Specify whether this is a volatile load or not.
274 void setVolatile(bool V) {
275 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
278 virtual LoadInst *clone(LLVMContext &Context) const;
280 /// getAlignment - Return the alignment of the access that is being performed
282 unsigned getAlignment() const {
283 return (1 << (SubclassData>>1)) >> 1;
286 void setAlignment(unsigned Align);
288 Value *getPointerOperand() { return getOperand(0); }
289 const Value *getPointerOperand() const { return getOperand(0); }
290 static unsigned getPointerOperandIndex() { return 0U; }
292 // Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const LoadInst *) { return true; }
294 static inline bool classof(const Instruction *I) {
295 return I->getOpcode() == Instruction::Load;
297 static inline bool classof(const Value *V) {
298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
303 //===----------------------------------------------------------------------===//
305 //===----------------------------------------------------------------------===//
307 /// StoreInst - an instruction for storing to memory
309 class StoreInst : public Instruction {
310 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
312 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
314 Op<0>() = SI.Op<0>();
315 Op<1>() = SI.Op<1>();
316 setVolatile(SI.isVolatile());
317 setAlignment(SI.getAlignment());
325 // allocate space for exactly two operands
326 void *operator new(size_t s) {
327 return User::operator new(s, 2);
329 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
330 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
331 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
332 Instruction *InsertBefore = 0);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
334 unsigned Align, Instruction *InsertBefore = 0);
335 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, BasicBlock *InsertAtEnd);
340 /// isVolatile - Return true if this is a load from a volatile memory
343 bool isVolatile() const { return SubclassData & 1; }
345 /// setVolatile - Specify whether this is a volatile load or not.
347 void setVolatile(bool V) {
348 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
354 /// getAlignment - Return the alignment of the access that is being performed
356 unsigned getAlignment() const {
357 return (1 << (SubclassData>>1)) >> 1;
360 void setAlignment(unsigned Align);
362 virtual StoreInst *clone(LLVMContext &Context) const;
364 Value *getPointerOperand() { return getOperand(1); }
365 const Value *getPointerOperand() const { return getOperand(1); }
366 static unsigned getPointerOperandIndex() { return 1U; }
368 // Methods for support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const StoreInst *) { return true; }
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
379 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
384 //===----------------------------------------------------------------------===//
385 // GetElementPtrInst Class
386 //===----------------------------------------------------------------------===//
388 // checkType - Simple wrapper function to give a better assertion failure
389 // message on bad indexes for a gep instruction.
391 static inline const Type *checkType(const Type *Ty) {
392 assert(Ty && "Invalid GetElementPtrInst indices for type!");
396 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
397 /// access elements of arrays and structs
399 class GetElementPtrInst : public Instruction {
400 GetElementPtrInst(const GetElementPtrInst &GEPI);
401 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
402 const std::string &NameStr);
403 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
405 template<typename InputIterator>
406 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
407 const std::string &NameStr,
408 // This argument ensures that we have an iterator we can
409 // do arithmetic on in constant time
410 std::random_access_iterator_tag) {
411 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
414 // This requires that the iterator points to contiguous memory.
415 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
416 // we have to build an array here
419 init(Ptr, 0, NumIdx, NameStr);
423 /// getIndexedType - Returns the type of the element that would be loaded with
424 /// a load instruction with the specified parameters.
426 /// Null is returned if the indices are invalid for the specified
429 template<typename InputIterator>
430 static const Type *getIndexedType(const Type *Ptr,
431 InputIterator IdxBegin,
432 InputIterator IdxEnd,
433 // This argument ensures that we
434 // have an iterator we can do
435 // arithmetic on in constant time
436 std::random_access_iterator_tag) {
437 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
440 // This requires that the iterator points to contiguous memory.
441 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
443 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
450 template<typename InputIterator>
451 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
454 const std::string &NameStr,
455 Instruction *InsertBefore);
456 template<typename InputIterator>
457 inline GetElementPtrInst(Value *Ptr,
458 InputIterator IdxBegin, InputIterator IdxEnd,
460 const std::string &NameStr, BasicBlock *InsertAtEnd);
462 /// Constructors - These two constructors are convenience methods because one
463 /// and two index getelementptr instructions are so common.
464 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
465 Instruction *InsertBefore = 0);
466 GetElementPtrInst(Value *Ptr, Value *Idx,
467 const std::string &NameStr, BasicBlock *InsertAtEnd);
469 template<typename InputIterator>
470 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
471 InputIterator IdxEnd,
472 const std::string &NameStr = "",
473 Instruction *InsertBefore = 0) {
474 typename std::iterator_traits<InputIterator>::difference_type Values =
475 1 + std::distance(IdxBegin, IdxEnd);
477 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
479 template<typename InputIterator>
480 static GetElementPtrInst *Create(Value *Ptr,
481 InputIterator IdxBegin, InputIterator IdxEnd,
482 const std::string &NameStr,
483 BasicBlock *InsertAtEnd) {
484 typename std::iterator_traits<InputIterator>::difference_type Values =
485 1 + std::distance(IdxBegin, IdxEnd);
487 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
490 /// Constructors - These two creators are convenience methods because one
491 /// index getelementptr instructions are so common.
492 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
493 const std::string &NameStr = "",
494 Instruction *InsertBefore = 0) {
495 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
497 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
498 const std::string &NameStr,
499 BasicBlock *InsertAtEnd) {
500 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
503 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
505 /// Transparently provide more efficient getOperand methods.
506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
508 // getType - Overload to return most specific pointer type...
509 const PointerType *getType() const {
510 return reinterpret_cast<const PointerType*>(Instruction::getType());
513 /// getIndexedType - Returns the type of the element that would be loaded with
514 /// a load instruction with the specified parameters.
516 /// Null is returned if the indices are invalid for the specified
519 template<typename InputIterator>
520 static const Type *getIndexedType(const Type *Ptr,
521 InputIterator IdxBegin,
522 InputIterator IdxEnd) {
523 return getIndexedType(Ptr, IdxBegin, IdxEnd,
524 typename std::iterator_traits<InputIterator>::
525 iterator_category());
528 static const Type *getIndexedType(const Type *Ptr,
529 Value* const *Idx, unsigned NumIdx);
531 static const Type *getIndexedType(const Type *Ptr,
532 uint64_t const *Idx, unsigned NumIdx);
534 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
536 inline op_iterator idx_begin() { return op_begin()+1; }
537 inline const_op_iterator idx_begin() const { return op_begin()+1; }
538 inline op_iterator idx_end() { return op_end(); }
539 inline const_op_iterator idx_end() const { return op_end(); }
541 Value *getPointerOperand() {
542 return getOperand(0);
544 const Value *getPointerOperand() const {
545 return getOperand(0);
547 static unsigned getPointerOperandIndex() {
548 return 0U; // get index for modifying correct operand
551 /// getPointerOperandType - Method to return the pointer operand as a
553 const PointerType *getPointerOperandType() const {
554 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
558 unsigned getNumIndices() const { // Note: always non-negative
559 return getNumOperands() - 1;
562 bool hasIndices() const {
563 return getNumOperands() > 1;
566 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
567 /// zeros. If so, the result pointer and the first operand have the same
568 /// value, just potentially different types.
569 bool hasAllZeroIndices() const;
571 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
572 /// constant integers. If so, the result pointer and the first operand have
573 /// a constant offset between them.
574 bool hasAllConstantIndices() const;
576 /// hasNoPointerOverflow - Return true if this GetElementPtr is known to
577 /// never have overflow in the pointer addition portions of its effective
578 /// computation. GetElementPtr computation involves several phases;
579 /// overflow can be considered to occur in index typecasting, array index
580 /// scaling, and the addition of the base pointer with offsets. This flag
581 /// only applies to the last of these. The operands are added to the base
582 /// pointer one at a time from left to right. This function returns false
583 /// if any of these additions results in an address value which is not
584 /// known to be within the allocated address space that the base pointer
585 /// points into, or within one element (of the original allocation) past
587 bool hasNoPointerOverflow() const {
588 return SubclassOptionalData & (1 << 0);
590 void setHasNoPointerOverflow(bool B) {
591 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
594 // Methods for support type inquiry through isa, cast, and dyn_cast:
595 static inline bool classof(const GetElementPtrInst *) { return true; }
596 static inline bool classof(const Instruction *I) {
597 return (I->getOpcode() == Instruction::GetElementPtr);
599 static inline bool classof(const Value *V) {
600 return isa<Instruction>(V) && classof(cast<Instruction>(V));
605 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
608 template<typename InputIterator>
609 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
610 InputIterator IdxBegin,
611 InputIterator IdxEnd,
613 const std::string &NameStr,
614 Instruction *InsertBefore)
615 : Instruction(PointerType::get(checkType(
616 getIndexedType(Ptr->getType(),
618 cast<PointerType>(Ptr->getType())
619 ->getAddressSpace()),
621 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
622 Values, InsertBefore) {
623 init(Ptr, IdxBegin, IdxEnd, NameStr,
624 typename std::iterator_traits<InputIterator>::iterator_category());
626 template<typename InputIterator>
627 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
628 InputIterator IdxBegin,
629 InputIterator IdxEnd,
631 const std::string &NameStr,
632 BasicBlock *InsertAtEnd)
633 : Instruction(PointerType::get(checkType(
634 getIndexedType(Ptr->getType(),
636 cast<PointerType>(Ptr->getType())
637 ->getAddressSpace()),
639 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
640 Values, InsertAtEnd) {
641 init(Ptr, IdxBegin, IdxEnd, NameStr,
642 typename std::iterator_traits<InputIterator>::iterator_category());
646 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
649 //===----------------------------------------------------------------------===//
651 //===----------------------------------------------------------------------===//
653 /// This instruction compares its operands according to the predicate given
654 /// to the constructor. It only operates on integers or pointers. The operands
655 /// must be identical types.
656 /// @brief Represent an integer comparison operator.
657 class ICmpInst: public CmpInst {
659 /// @brief Constructor with insert-before-instruction semantics.
661 Instruction *InsertBefore, ///< Where to insert
662 Predicate pred, ///< The predicate to use for the comparison
663 Value *LHS, ///< The left-hand-side of the expression
664 Value *RHS, ///< The right-hand-side of the expression
665 const std::string &NameStr = "" ///< Name of the instruction
666 ) : CmpInst(InsertBefore->getParent()->getContext()->
667 makeCmpResultType(LHS->getType()),
668 Instruction::ICmp, pred, LHS, RHS, NameStr,
670 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
671 pred <= CmpInst::LAST_ICMP_PREDICATE &&
672 "Invalid ICmp predicate value");
673 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
674 "Both operands to ICmp instruction are not of the same type!");
675 // Check that the operands are the right type
676 assert((getOperand(0)->getType()->isIntOrIntVector() ||
677 isa<PointerType>(getOperand(0)->getType())) &&
678 "Invalid operand types for ICmp instruction");
681 /// @brief Constructor with insert-at-end semantics.
683 BasicBlock &InsertAtEnd, ///< Block to insert into.
684 Predicate pred, ///< The predicate to use for the comparison
685 Value *LHS, ///< The left-hand-side of the expression
686 Value *RHS, ///< The right-hand-side of the expression
687 const std::string &NameStr = "" ///< Name of the instruction
688 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
689 Instruction::ICmp, pred, LHS, RHS, NameStr,
691 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
692 pred <= CmpInst::LAST_ICMP_PREDICATE &&
693 "Invalid ICmp predicate value");
694 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
695 "Both operands to ICmp instruction are not of the same type!");
696 // Check that the operands are the right type
697 assert((getOperand(0)->getType()->isIntOrIntVector() ||
698 isa<PointerType>(getOperand(0)->getType())) &&
699 "Invalid operand types for ICmp instruction");
702 /// @brief Constructor with no-insertion semantics
704 LLVMContext &Context, ///< Context to construct within
705 Predicate pred, ///< The predicate to use for the comparison
706 Value *LHS, ///< The left-hand-side of the expression
707 Value *RHS, ///< The right-hand-side of the expression
708 const std::string &NameStr = "" ///< Name of the instruction
709 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
710 Instruction::ICmp, pred, LHS, RHS, NameStr) {
711 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
712 pred <= CmpInst::LAST_ICMP_PREDICATE &&
713 "Invalid ICmp predicate value");
714 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
715 "Both operands to ICmp instruction are not of the same type!");
716 // Check that the operands are the right type
717 assert((getOperand(0)->getType()->isIntOrIntVector() ||
718 isa<PointerType>(getOperand(0)->getType())) &&
719 "Invalid operand types for ICmp instruction");
722 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
723 /// @returns the predicate that would be the result if the operand were
724 /// regarded as signed.
725 /// @brief Return the signed version of the predicate
726 Predicate getSignedPredicate() const {
727 return getSignedPredicate(getPredicate());
730 /// This is a static version that you can use without an instruction.
731 /// @brief Return the signed version of the predicate.
732 static Predicate getSignedPredicate(Predicate pred);
734 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
735 /// @returns the predicate that would be the result if the operand were
736 /// regarded as unsigned.
737 /// @brief Return the unsigned version of the predicate
738 Predicate getUnsignedPredicate() const {
739 return getUnsignedPredicate(getPredicate());
742 /// This is a static version that you can use without an instruction.
743 /// @brief Return the unsigned version of the predicate.
744 static Predicate getUnsignedPredicate(Predicate pred);
746 /// isEquality - Return true if this predicate is either EQ or NE. This also
747 /// tests for commutativity.
748 static bool isEquality(Predicate P) {
749 return P == ICMP_EQ || P == ICMP_NE;
752 /// isEquality - Return true if this predicate is either EQ or NE. This also
753 /// tests for commutativity.
754 bool isEquality() const {
755 return isEquality(getPredicate());
758 /// @returns true if the predicate of this ICmpInst is commutative
759 /// @brief Determine if this relation is commutative.
760 bool isCommutative() const { return isEquality(); }
762 /// isRelational - Return true if the predicate is relational (not EQ or NE).
764 bool isRelational() const {
765 return !isEquality();
768 /// isRelational - Return true if the predicate is relational (not EQ or NE).
770 static bool isRelational(Predicate P) {
771 return !isEquality(P);
774 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
775 /// @brief Determine if this instruction's predicate is signed.
776 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
778 /// @returns true if the predicate provided is signed, false otherwise
779 /// @brief Determine if the predicate is signed.
780 static bool isSignedPredicate(Predicate pred);
782 /// @returns true if the specified compare predicate is
783 /// true when both operands are equal...
784 /// @brief Determine if the icmp is true when both operands are equal
785 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
786 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
787 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
788 pred == ICmpInst::ICMP_SLE;
791 /// @returns true if the specified compare instruction is
792 /// true when both operands are equal...
793 /// @brief Determine if the ICmpInst returns true when both operands are equal
794 bool isTrueWhenEqual() {
795 return isTrueWhenEqual(getPredicate());
798 /// Initialize a set of values that all satisfy the predicate with C.
799 /// @brief Make a ConstantRange for a relation with a constant value.
800 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
802 /// Exchange the two operands to this instruction in such a way that it does
803 /// not modify the semantics of the instruction. The predicate value may be
804 /// changed to retain the same result if the predicate is order dependent
806 /// @brief Swap operands and adjust predicate.
807 void swapOperands() {
808 SubclassData = getSwappedPredicate();
809 Op<0>().swap(Op<1>());
812 virtual ICmpInst *clone(LLVMContext &Context) const;
814 // Methods for support type inquiry through isa, cast, and dyn_cast:
815 static inline bool classof(const ICmpInst *) { return true; }
816 static inline bool classof(const Instruction *I) {
817 return I->getOpcode() == Instruction::ICmp;
819 static inline bool classof(const Value *V) {
820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
825 //===----------------------------------------------------------------------===//
827 //===----------------------------------------------------------------------===//
829 /// This instruction compares its operands according to the predicate given
830 /// to the constructor. It only operates on floating point values or packed
831 /// vectors of floating point values. The operands must be identical types.
832 /// @brief Represents a floating point comparison operator.
833 class FCmpInst: public CmpInst {
835 /// @brief Constructor with insert-before-instruction semantics.
837 Instruction *InsertBefore, ///< Where to insert
838 Predicate pred, ///< The predicate to use for the comparison
839 Value *LHS, ///< The left-hand-side of the expression
840 Value *RHS, ///< The right-hand-side of the expression
841 const std::string &NameStr = "" ///< Name of the instruction
842 ) : CmpInst(InsertBefore->getParent()->getContext()->
843 makeCmpResultType(LHS->getType()),
844 Instruction::FCmp, pred, LHS, RHS, NameStr,
846 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
847 "Invalid FCmp predicate value");
848 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
849 "Both operands to FCmp instruction are not of the same type!");
850 // Check that the operands are the right type
851 assert(getOperand(0)->getType()->isFPOrFPVector() &&
852 "Invalid operand types for FCmp instruction");
855 /// @brief Constructor with insert-at-end semantics.
857 BasicBlock &InsertAtEnd, ///< Block to insert into.
858 Predicate pred, ///< The predicate to use for the comparison
859 Value *LHS, ///< The left-hand-side of the expression
860 Value *RHS, ///< The right-hand-side of the expression
861 const std::string &NameStr = "" ///< Name of the instruction
862 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
863 Instruction::FCmp, pred, LHS, RHS, NameStr,
865 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
866 "Invalid FCmp predicate value");
867 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
868 "Both operands to FCmp instruction are not of the same type!");
869 // Check that the operands are the right type
870 assert(getOperand(0)->getType()->isFPOrFPVector() &&
871 "Invalid operand types for FCmp instruction");
874 /// @brief Constructor with no-insertion semantics
876 LLVMContext &Context, ///< Context to build in
877 Predicate pred, ///< The predicate to use for the comparison
878 Value *LHS, ///< The left-hand-side of the expression
879 Value *RHS, ///< The right-hand-side of the expression
880 const std::string &NameStr = "" ///< Name of the instruction
881 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
882 Instruction::FCmp, pred, LHS, RHS, NameStr) {
883 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
884 "Invalid FCmp predicate value");
885 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
886 "Both operands to FCmp instruction are not of the same type!");
887 // Check that the operands are the right type
888 assert(getOperand(0)->getType()->isFPOrFPVector() &&
889 "Invalid operand types for FCmp instruction");
892 /// @returns true if the predicate of this instruction is EQ or NE.
893 /// @brief Determine if this is an equality predicate.
894 bool isEquality() const {
895 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
896 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
899 /// @returns true if the predicate of this instruction is commutative.
900 /// @brief Determine if this is a commutative predicate.
901 bool isCommutative() const {
902 return isEquality() ||
903 SubclassData == FCMP_FALSE ||
904 SubclassData == FCMP_TRUE ||
905 SubclassData == FCMP_ORD ||
906 SubclassData == FCMP_UNO;
909 /// @returns true if the predicate is relational (not EQ or NE).
910 /// @brief Determine if this a relational predicate.
911 bool isRelational() const { return !isEquality(); }
913 /// Exchange the two operands to this instruction in such a way that it does
914 /// not modify the semantics of the instruction. The predicate value may be
915 /// changed to retain the same result if the predicate is order dependent
917 /// @brief Swap operands and adjust predicate.
918 void swapOperands() {
919 SubclassData = getSwappedPredicate();
920 Op<0>().swap(Op<1>());
923 virtual FCmpInst *clone(LLVMContext &Context) const;
925 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
926 static inline bool classof(const FCmpInst *) { return true; }
927 static inline bool classof(const Instruction *I) {
928 return I->getOpcode() == Instruction::FCmp;
930 static inline bool classof(const Value *V) {
931 return isa<Instruction>(V) && classof(cast<Instruction>(V));
936 //===----------------------------------------------------------------------===//
938 //===----------------------------------------------------------------------===//
939 /// CallInst - This class represents a function call, abstracting a target
940 /// machine's calling convention. This class uses low bit of the SubClassData
941 /// field to indicate whether or not this is a tail call. The rest of the bits
942 /// hold the calling convention of the call.
945 class CallInst : public Instruction {
946 AttrListPtr AttributeList; ///< parameter attributes for call
947 CallInst(const CallInst &CI);
948 void init(Value *Func, Value* const *Params, unsigned NumParams);
949 void init(Value *Func, Value *Actual1, Value *Actual2);
950 void init(Value *Func, Value *Actual);
951 void init(Value *Func);
953 template<typename InputIterator>
954 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
955 const std::string &NameStr,
956 // This argument ensures that we have an iterator we can
957 // do arithmetic on in constant time
958 std::random_access_iterator_tag) {
959 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
961 // This requires that the iterator points to contiguous memory.
962 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
966 /// Construct a CallInst given a range of arguments. InputIterator
967 /// must be a random-access iterator pointing to contiguous storage
968 /// (e.g. a std::vector<>::iterator). Checks are made for
969 /// random-accessness but not for contiguous storage as that would
970 /// incur runtime overhead.
971 /// @brief Construct a CallInst from a range of arguments
972 template<typename InputIterator>
973 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
974 const std::string &NameStr, Instruction *InsertBefore);
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 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
984 const std::string &NameStr, BasicBlock *InsertAtEnd);
986 CallInst(Value *F, Value *Actual, const std::string& NameStr,
987 Instruction *InsertBefore);
988 CallInst(Value *F, Value *Actual, const std::string& NameStr,
989 BasicBlock *InsertAtEnd);
990 explicit CallInst(Value *F, const std::string &NameStr,
991 Instruction *InsertBefore);
992 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
994 template<typename InputIterator>
995 static CallInst *Create(Value *Func,
996 InputIterator ArgBegin, InputIterator ArgEnd,
997 const std::string &NameStr = "",
998 Instruction *InsertBefore = 0) {
999 return new((unsigned)(ArgEnd - ArgBegin + 1))
1000 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1002 template<typename InputIterator>
1003 static CallInst *Create(Value *Func,
1004 InputIterator ArgBegin, InputIterator ArgEnd,
1005 const std::string &NameStr, BasicBlock *InsertAtEnd) {
1006 return new((unsigned)(ArgEnd - ArgBegin + 1))
1007 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1009 static CallInst *Create(Value *F, Value *Actual,
1010 const std::string& NameStr = "",
1011 Instruction *InsertBefore = 0) {
1012 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1014 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1015 BasicBlock *InsertAtEnd) {
1016 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1018 static CallInst *Create(Value *F, const std::string &NameStr = "",
1019 Instruction *InsertBefore = 0) {
1020 return new(1) CallInst(F, NameStr, InsertBefore);
1022 static CallInst *Create(Value *F, const std::string &NameStr,
1023 BasicBlock *InsertAtEnd) {
1024 return new(1) CallInst(F, NameStr, InsertAtEnd);
1029 bool isTailCall() const { return SubclassData & 1; }
1030 void setTailCall(bool isTC = true) {
1031 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1034 virtual CallInst *clone(LLVMContext &Context) const;
1036 /// Provide fast operand accessors
1037 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1039 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1041 unsigned getCallingConv() const { return SubclassData >> 1; }
1042 void setCallingConv(unsigned CC) {
1043 SubclassData = (SubclassData & 1) | (CC << 1);
1046 /// getAttributes - Return the parameter attributes for this call.
1048 const AttrListPtr &getAttributes() const { return AttributeList; }
1050 /// setAttributes - Set the parameter attributes for this call.
1052 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1054 /// addAttribute - adds the attribute to the list of attributes.
1055 void addAttribute(unsigned i, Attributes attr);
1057 /// removeAttribute - removes the attribute from the list of attributes.
1058 void removeAttribute(unsigned i, Attributes attr);
1060 /// @brief Determine whether the call or the callee has the given attribute.
1061 bool paramHasAttr(unsigned i, Attributes attr) const;
1063 /// @brief Extract the alignment for a call or parameter (0=unknown).
1064 unsigned getParamAlignment(unsigned i) const {
1065 return AttributeList.getParamAlignment(i);
1068 /// @brief Determine if the call does not access memory.
1069 bool doesNotAccessMemory() const {
1070 return paramHasAttr(~0, Attribute::ReadNone);
1072 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1073 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1074 else removeAttribute(~0, Attribute::ReadNone);
1077 /// @brief Determine if the call does not access or only reads memory.
1078 bool onlyReadsMemory() const {
1079 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1081 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1082 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1083 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1086 /// @brief Determine if the call cannot return.
1087 bool doesNotReturn() const {
1088 return paramHasAttr(~0, Attribute::NoReturn);
1090 void setDoesNotReturn(bool DoesNotReturn = true) {
1091 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1092 else removeAttribute(~0, Attribute::NoReturn);
1095 /// @brief Determine if the call cannot unwind.
1096 bool doesNotThrow() const {
1097 return paramHasAttr(~0, Attribute::NoUnwind);
1099 void setDoesNotThrow(bool DoesNotThrow = true) {
1100 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1101 else removeAttribute(~0, Attribute::NoUnwind);
1104 /// @brief Determine if the call returns a structure through first
1105 /// pointer argument.
1106 bool hasStructRetAttr() const {
1107 // Be friendly and also check the callee.
1108 return paramHasAttr(1, Attribute::StructRet);
1111 /// @brief Determine if any call argument is an aggregate passed by value.
1112 bool hasByValArgument() const {
1113 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1116 /// getCalledFunction - Return the function called, or null if this is an
1117 /// indirect function invocation.
1119 Function *getCalledFunction() const {
1120 return dyn_cast<Function>(Op<0>());
1123 /// getCalledValue - Get a pointer to the function that is invoked by this
1125 const Value *getCalledValue() const { return Op<0>(); }
1126 Value *getCalledValue() { return Op<0>(); }
1128 // Methods for support type inquiry through isa, cast, and dyn_cast:
1129 static inline bool classof(const CallInst *) { return true; }
1130 static inline bool classof(const Instruction *I) {
1131 return I->getOpcode() == Instruction::Call;
1133 static inline bool classof(const Value *V) {
1134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1139 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1142 template<typename InputIterator>
1143 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1144 const std::string &NameStr, BasicBlock *InsertAtEnd)
1145 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1146 ->getElementType())->getReturnType(),
1148 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1149 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1150 init(Func, ArgBegin, ArgEnd, NameStr,
1151 typename std::iterator_traits<InputIterator>::iterator_category());
1154 template<typename InputIterator>
1155 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1156 const std::string &NameStr, Instruction *InsertBefore)
1157 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1158 ->getElementType())->getReturnType(),
1160 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1161 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1162 init(Func, ArgBegin, ArgEnd, NameStr,
1163 typename std::iterator_traits<InputIterator>::iterator_category());
1166 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1168 //===----------------------------------------------------------------------===//
1170 //===----------------------------------------------------------------------===//
1172 /// SelectInst - This class represents the LLVM 'select' instruction.
1174 class SelectInst : public Instruction {
1175 void init(Value *C, Value *S1, Value *S2) {
1176 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1182 SelectInst(const SelectInst &SI)
1183 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1184 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1186 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1187 Instruction *InsertBefore)
1188 : Instruction(S1->getType(), Instruction::Select,
1189 &Op<0>(), 3, InsertBefore) {
1193 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1194 BasicBlock *InsertAtEnd)
1195 : Instruction(S1->getType(), Instruction::Select,
1196 &Op<0>(), 3, InsertAtEnd) {
1201 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1202 const std::string &NameStr = "",
1203 Instruction *InsertBefore = 0) {
1204 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1206 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1207 const std::string &NameStr,
1208 BasicBlock *InsertAtEnd) {
1209 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1212 Value *getCondition() const { return Op<0>(); }
1213 Value *getTrueValue() const { return Op<1>(); }
1214 Value *getFalseValue() const { return Op<2>(); }
1216 /// areInvalidOperands - Return a string if the specified operands are invalid
1217 /// for a select operation, otherwise return null.
1218 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1220 /// Transparently provide more efficient getOperand methods.
1221 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1223 OtherOps getOpcode() const {
1224 return static_cast<OtherOps>(Instruction::getOpcode());
1227 virtual SelectInst *clone(LLVMContext &Context) const;
1229 // Methods for support type inquiry through isa, cast, and dyn_cast:
1230 static inline bool classof(const SelectInst *) { return true; }
1231 static inline bool classof(const Instruction *I) {
1232 return I->getOpcode() == Instruction::Select;
1234 static inline bool classof(const Value *V) {
1235 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1240 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1243 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1245 //===----------------------------------------------------------------------===//
1247 //===----------------------------------------------------------------------===//
1249 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1250 /// an argument of the specified type given a va_list and increments that list
1252 class VAArgInst : public UnaryInstruction {
1253 VAArgInst(const VAArgInst &VAA)
1254 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1256 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1257 Instruction *InsertBefore = 0)
1258 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1261 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1262 BasicBlock *InsertAtEnd)
1263 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1267 virtual VAArgInst *clone(LLVMContext &Context) const;
1269 // Methods for support type inquiry through isa, cast, and dyn_cast:
1270 static inline bool classof(const VAArgInst *) { return true; }
1271 static inline bool classof(const Instruction *I) {
1272 return I->getOpcode() == VAArg;
1274 static inline bool classof(const Value *V) {
1275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1279 //===----------------------------------------------------------------------===//
1280 // ExtractElementInst Class
1281 //===----------------------------------------------------------------------===//
1283 /// ExtractElementInst - This instruction extracts a single (scalar)
1284 /// element from a VectorType value
1286 class ExtractElementInst : public Instruction {
1287 ExtractElementInst(const ExtractElementInst &EE) :
1288 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1289 Op<0>() = EE.Op<0>();
1290 Op<1>() = EE.Op<1>();
1294 // allocate space for exactly two operands
1295 void *operator new(size_t s) {
1296 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1298 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1299 Instruction *InsertBefore = 0);
1300 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1301 BasicBlock *InsertAtEnd);
1303 /// isValidOperands - Return true if an extractelement instruction can be
1304 /// formed with the specified operands.
1305 static bool isValidOperands(const Value *Vec, const Value *Idx);
1307 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1309 /// Transparently provide more efficient getOperand methods.
1310 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1312 // Methods for support type inquiry through isa, cast, and dyn_cast:
1313 static inline bool classof(const ExtractElementInst *) { return true; }
1314 static inline bool classof(const Instruction *I) {
1315 return I->getOpcode() == Instruction::ExtractElement;
1317 static inline bool classof(const Value *V) {
1318 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1323 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1326 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1328 //===----------------------------------------------------------------------===//
1329 // InsertElementInst Class
1330 //===----------------------------------------------------------------------===//
1332 /// InsertElementInst - This instruction inserts a single (scalar)
1333 /// element into a VectorType value
1335 class InsertElementInst : public Instruction {
1336 InsertElementInst(const InsertElementInst &IE);
1337 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1338 const std::string &NameStr = "",
1339 Instruction *InsertBefore = 0);
1340 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1341 const std::string &NameStr, BasicBlock *InsertAtEnd);
1343 static InsertElementInst *Create(const InsertElementInst &IE) {
1344 return new(IE.getNumOperands()) InsertElementInst(IE);
1346 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1347 const std::string &NameStr = "",
1348 Instruction *InsertBefore = 0) {
1349 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1351 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1352 const std::string &NameStr,
1353 BasicBlock *InsertAtEnd) {
1354 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1357 /// isValidOperands - Return true if an insertelement instruction can be
1358 /// formed with the specified operands.
1359 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1362 virtual InsertElementInst *clone(LLVMContext &Context) const;
1364 /// getType - Overload to return most specific vector type.
1366 const VectorType *getType() const {
1367 return reinterpret_cast<const VectorType*>(Instruction::getType());
1370 /// Transparently provide more efficient getOperand methods.
1371 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1373 // Methods for support type inquiry through isa, cast, and dyn_cast:
1374 static inline bool classof(const InsertElementInst *) { return true; }
1375 static inline bool classof(const Instruction *I) {
1376 return I->getOpcode() == Instruction::InsertElement;
1378 static inline bool classof(const Value *V) {
1379 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1384 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1387 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1389 //===----------------------------------------------------------------------===//
1390 // ShuffleVectorInst Class
1391 //===----------------------------------------------------------------------===//
1393 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1396 class ShuffleVectorInst : public Instruction {
1397 ShuffleVectorInst(const ShuffleVectorInst &IE);
1399 // allocate space for exactly three operands
1400 void *operator new(size_t s) {
1401 return User::operator new(s, 3);
1403 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1404 const std::string &NameStr = "",
1405 Instruction *InsertBefor = 0);
1406 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1407 const std::string &NameStr, BasicBlock *InsertAtEnd);
1409 /// isValidOperands - Return true if a shufflevector instruction can be
1410 /// formed with the specified operands.
1411 static bool isValidOperands(const Value *V1, const Value *V2,
1414 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1416 /// getType - Overload to return most specific vector type.
1418 const VectorType *getType() const {
1419 return reinterpret_cast<const VectorType*>(Instruction::getType());
1422 /// Transparently provide more efficient getOperand methods.
1423 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1425 /// getMaskValue - Return the index from the shuffle mask for the specified
1426 /// output result. This is either -1 if the element is undef or a number less
1427 /// than 2*numelements.
1428 int getMaskValue(unsigned i) const;
1430 // Methods for support type inquiry through isa, cast, and dyn_cast:
1431 static inline bool classof(const ShuffleVectorInst *) { return true; }
1432 static inline bool classof(const Instruction *I) {
1433 return I->getOpcode() == Instruction::ShuffleVector;
1435 static inline bool classof(const Value *V) {
1436 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1441 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1444 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1446 //===----------------------------------------------------------------------===//
1447 // ExtractValueInst Class
1448 //===----------------------------------------------------------------------===//
1450 /// ExtractValueInst - This instruction extracts a struct member or array
1451 /// element value from an aggregate value.
1453 class ExtractValueInst : public UnaryInstruction {
1454 SmallVector<unsigned, 4> Indices;
1456 ExtractValueInst(const ExtractValueInst &EVI);
1457 void init(const unsigned *Idx, unsigned NumIdx,
1458 const std::string &NameStr);
1459 void init(unsigned Idx, const std::string &NameStr);
1461 template<typename InputIterator>
1462 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1463 const std::string &NameStr,
1464 // This argument ensures that we have an iterator we can
1465 // do arithmetic on in constant time
1466 std::random_access_iterator_tag) {
1467 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1469 // There's no fundamental reason why we require at least one index
1470 // (other than weirdness with &*IdxBegin being invalid; see
1471 // getelementptr's init routine for example). But there's no
1472 // present need to support it.
1473 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1475 // This requires that the iterator points to contiguous memory.
1476 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1477 // we have to build an array here
1480 /// getIndexedType - Returns the type of the element that would be extracted
1481 /// with an extractvalue instruction with the specified parameters.
1483 /// Null is returned if the indices are invalid for the specified
1486 static const Type *getIndexedType(const Type *Agg,
1487 const unsigned *Idx, unsigned NumIdx);
1489 template<typename InputIterator>
1490 static const Type *getIndexedType(const Type *Ptr,
1491 InputIterator IdxBegin,
1492 InputIterator IdxEnd,
1493 // This argument ensures that we
1494 // have an iterator we can do
1495 // arithmetic on in constant time
1496 std::random_access_iterator_tag) {
1497 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1500 // This requires that the iterator points to contiguous memory.
1501 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1503 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1506 /// Constructors - Create a extractvalue instruction with a base aggregate
1507 /// value and a list of indices. The first ctor can optionally insert before
1508 /// an existing instruction, the second appends the new instruction to the
1509 /// specified BasicBlock.
1510 template<typename InputIterator>
1511 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1512 InputIterator IdxEnd,
1513 const std::string &NameStr,
1514 Instruction *InsertBefore);
1515 template<typename InputIterator>
1516 inline ExtractValueInst(Value *Agg,
1517 InputIterator IdxBegin, InputIterator IdxEnd,
1518 const std::string &NameStr, BasicBlock *InsertAtEnd);
1520 // allocate space for exactly one operand
1521 void *operator new(size_t s) {
1522 return User::operator new(s, 1);
1526 template<typename InputIterator>
1527 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1528 InputIterator IdxEnd,
1529 const std::string &NameStr = "",
1530 Instruction *InsertBefore = 0) {
1532 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1534 template<typename InputIterator>
1535 static ExtractValueInst *Create(Value *Agg,
1536 InputIterator IdxBegin, InputIterator IdxEnd,
1537 const std::string &NameStr,
1538 BasicBlock *InsertAtEnd) {
1539 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1542 /// Constructors - These two creators are convenience methods because one
1543 /// index extractvalue instructions are much more common than those with
1545 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1546 const std::string &NameStr = "",
1547 Instruction *InsertBefore = 0) {
1548 unsigned Idxs[1] = { Idx };
1549 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1551 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1552 const std::string &NameStr,
1553 BasicBlock *InsertAtEnd) {
1554 unsigned Idxs[1] = { Idx };
1555 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1558 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1560 /// getIndexedType - Returns the type of the element that would be extracted
1561 /// with an extractvalue instruction with the specified parameters.
1563 /// Null is returned if the indices are invalid for the specified
1566 template<typename InputIterator>
1567 static const Type *getIndexedType(const Type *Ptr,
1568 InputIterator IdxBegin,
1569 InputIterator IdxEnd) {
1570 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1571 typename std::iterator_traits<InputIterator>::
1572 iterator_category());
1574 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1576 typedef const unsigned* idx_iterator;
1577 inline idx_iterator idx_begin() const { return Indices.begin(); }
1578 inline idx_iterator idx_end() const { return Indices.end(); }
1580 Value *getAggregateOperand() {
1581 return getOperand(0);
1583 const Value *getAggregateOperand() const {
1584 return getOperand(0);
1586 static unsigned getAggregateOperandIndex() {
1587 return 0U; // get index for modifying correct operand
1590 unsigned getNumIndices() const { // Note: always non-negative
1591 return (unsigned)Indices.size();
1594 bool hasIndices() const {
1598 // Methods for support type inquiry through isa, cast, and dyn_cast:
1599 static inline bool classof(const ExtractValueInst *) { return true; }
1600 static inline bool classof(const Instruction *I) {
1601 return I->getOpcode() == Instruction::ExtractValue;
1603 static inline bool classof(const Value *V) {
1604 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1608 template<typename InputIterator>
1609 ExtractValueInst::ExtractValueInst(Value *Agg,
1610 InputIterator IdxBegin,
1611 InputIterator IdxEnd,
1612 const std::string &NameStr,
1613 Instruction *InsertBefore)
1614 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1616 ExtractValue, Agg, InsertBefore) {
1617 init(IdxBegin, IdxEnd, NameStr,
1618 typename std::iterator_traits<InputIterator>::iterator_category());
1620 template<typename InputIterator>
1621 ExtractValueInst::ExtractValueInst(Value *Agg,
1622 InputIterator IdxBegin,
1623 InputIterator IdxEnd,
1624 const std::string &NameStr,
1625 BasicBlock *InsertAtEnd)
1626 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1628 ExtractValue, Agg, InsertAtEnd) {
1629 init(IdxBegin, IdxEnd, NameStr,
1630 typename std::iterator_traits<InputIterator>::iterator_category());
1634 //===----------------------------------------------------------------------===//
1635 // InsertValueInst Class
1636 //===----------------------------------------------------------------------===//
1638 /// InsertValueInst - This instruction inserts a struct field of array element
1639 /// value into an aggregate value.
1641 class InsertValueInst : public Instruction {
1642 SmallVector<unsigned, 4> Indices;
1644 void *operator new(size_t, unsigned); // Do not implement
1645 InsertValueInst(const InsertValueInst &IVI);
1646 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1647 const std::string &NameStr);
1648 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1650 template<typename InputIterator>
1651 void init(Value *Agg, Value *Val,
1652 InputIterator IdxBegin, InputIterator IdxEnd,
1653 const std::string &NameStr,
1654 // This argument ensures that we have an iterator we can
1655 // do arithmetic on in constant time
1656 std::random_access_iterator_tag) {
1657 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1659 // There's no fundamental reason why we require at least one index
1660 // (other than weirdness with &*IdxBegin being invalid; see
1661 // getelementptr's init routine for example). But there's no
1662 // present need to support it.
1663 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1665 // This requires that the iterator points to contiguous memory.
1666 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1667 // we have to build an array here
1670 /// Constructors - Create a insertvalue instruction with a base aggregate
1671 /// value, a value to insert, and a list of indices. The first ctor can
1672 /// optionally insert before an existing instruction, the second appends
1673 /// the new instruction to the specified BasicBlock.
1674 template<typename InputIterator>
1675 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1676 InputIterator IdxEnd,
1677 const std::string &NameStr,
1678 Instruction *InsertBefore);
1679 template<typename InputIterator>
1680 inline InsertValueInst(Value *Agg, Value *Val,
1681 InputIterator IdxBegin, InputIterator IdxEnd,
1682 const std::string &NameStr, BasicBlock *InsertAtEnd);
1684 /// Constructors - These two constructors are convenience methods because one
1685 /// and two index insertvalue instructions are so common.
1686 InsertValueInst(Value *Agg, Value *Val,
1687 unsigned Idx, const std::string &NameStr = "",
1688 Instruction *InsertBefore = 0);
1689 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1690 const std::string &NameStr, BasicBlock *InsertAtEnd);
1692 // allocate space for exactly two operands
1693 void *operator new(size_t s) {
1694 return User::operator new(s, 2);
1697 template<typename InputIterator>
1698 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1699 InputIterator IdxEnd,
1700 const std::string &NameStr = "",
1701 Instruction *InsertBefore = 0) {
1702 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1703 NameStr, InsertBefore);
1705 template<typename InputIterator>
1706 static InsertValueInst *Create(Value *Agg, Value *Val,
1707 InputIterator IdxBegin, InputIterator IdxEnd,
1708 const std::string &NameStr,
1709 BasicBlock *InsertAtEnd) {
1710 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1711 NameStr, InsertAtEnd);
1714 /// Constructors - These two creators are convenience methods because one
1715 /// index insertvalue instructions are much more common than those with
1717 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1718 const std::string &NameStr = "",
1719 Instruction *InsertBefore = 0) {
1720 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1722 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1723 const std::string &NameStr,
1724 BasicBlock *InsertAtEnd) {
1725 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1728 virtual InsertValueInst *clone(LLVMContext &Context) const;
1730 /// Transparently provide more efficient getOperand methods.
1731 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1733 typedef const unsigned* idx_iterator;
1734 inline idx_iterator idx_begin() const { return Indices.begin(); }
1735 inline idx_iterator idx_end() const { return Indices.end(); }
1737 Value *getAggregateOperand() {
1738 return getOperand(0);
1740 const Value *getAggregateOperand() const {
1741 return getOperand(0);
1743 static unsigned getAggregateOperandIndex() {
1744 return 0U; // get index for modifying correct operand
1747 Value *getInsertedValueOperand() {
1748 return getOperand(1);
1750 const Value *getInsertedValueOperand() const {
1751 return getOperand(1);
1753 static unsigned getInsertedValueOperandIndex() {
1754 return 1U; // get index for modifying correct operand
1757 unsigned getNumIndices() const { // Note: always non-negative
1758 return (unsigned)Indices.size();
1761 bool hasIndices() const {
1765 // Methods for support type inquiry through isa, cast, and dyn_cast:
1766 static inline bool classof(const InsertValueInst *) { return true; }
1767 static inline bool classof(const Instruction *I) {
1768 return I->getOpcode() == Instruction::InsertValue;
1770 static inline bool classof(const Value *V) {
1771 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1776 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1779 template<typename InputIterator>
1780 InsertValueInst::InsertValueInst(Value *Agg,
1782 InputIterator IdxBegin,
1783 InputIterator IdxEnd,
1784 const std::string &NameStr,
1785 Instruction *InsertBefore)
1786 : Instruction(Agg->getType(), InsertValue,
1787 OperandTraits<InsertValueInst>::op_begin(this),
1789 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1790 typename std::iterator_traits<InputIterator>::iterator_category());
1792 template<typename InputIterator>
1793 InsertValueInst::InsertValueInst(Value *Agg,
1795 InputIterator IdxBegin,
1796 InputIterator IdxEnd,
1797 const std::string &NameStr,
1798 BasicBlock *InsertAtEnd)
1799 : Instruction(Agg->getType(), InsertValue,
1800 OperandTraits<InsertValueInst>::op_begin(this),
1802 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1803 typename std::iterator_traits<InputIterator>::iterator_category());
1806 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1808 //===----------------------------------------------------------------------===//
1810 //===----------------------------------------------------------------------===//
1812 // PHINode - The PHINode class is used to represent the magical mystical PHI
1813 // node, that can not exist in nature, but can be synthesized in a computer
1814 // scientist's overactive imagination.
1816 class PHINode : public Instruction {
1817 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1818 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1819 /// the number actually in use.
1820 unsigned ReservedSpace;
1821 PHINode(const PHINode &PN);
1822 // allocate space for exactly zero operands
1823 void *operator new(size_t s) {
1824 return User::operator new(s, 0);
1826 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1827 Instruction *InsertBefore = 0)
1828 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1833 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1834 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1839 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1840 Instruction *InsertBefore = 0) {
1841 return new PHINode(Ty, NameStr, InsertBefore);
1843 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1844 BasicBlock *InsertAtEnd) {
1845 return new PHINode(Ty, NameStr, InsertAtEnd);
1849 /// reserveOperandSpace - This method can be used to avoid repeated
1850 /// reallocation of PHI operand lists by reserving space for the correct
1851 /// number of operands before adding them. Unlike normal vector reserves,
1852 /// this method can also be used to trim the operand space.
1853 void reserveOperandSpace(unsigned NumValues) {
1854 resizeOperands(NumValues*2);
1857 virtual PHINode *clone(LLVMContext &Context) const;
1859 /// Provide fast operand accessors
1860 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1862 /// getNumIncomingValues - Return the number of incoming edges
1864 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1866 /// getIncomingValue - Return incoming value number x
1868 Value *getIncomingValue(unsigned i) const {
1869 assert(i*2 < getNumOperands() && "Invalid value number!");
1870 return getOperand(i*2);
1872 void setIncomingValue(unsigned i, Value *V) {
1873 assert(i*2 < getNumOperands() && "Invalid value number!");
1876 static unsigned getOperandNumForIncomingValue(unsigned i) {
1879 static unsigned getIncomingValueNumForOperand(unsigned i) {
1880 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1884 /// getIncomingBlock - Return incoming basic block corresponding
1885 /// to value use iterator
1887 template <typename U>
1888 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1889 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1890 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1892 /// getIncomingBlock - Return incoming basic block number x
1894 BasicBlock *getIncomingBlock(unsigned i) const {
1895 return static_cast<BasicBlock*>(getOperand(i*2+1));
1897 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1898 setOperand(i*2+1, BB);
1900 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1903 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1904 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1908 /// addIncoming - Add an incoming value to the end of the PHI list
1910 void addIncoming(Value *V, BasicBlock *BB) {
1911 assert(V && "PHI node got a null value!");
1912 assert(BB && "PHI node got a null basic block!");
1913 assert(getType() == V->getType() &&
1914 "All operands to PHI node must be the same type as the PHI node!");
1915 unsigned OpNo = NumOperands;
1916 if (OpNo+2 > ReservedSpace)
1917 resizeOperands(0); // Get more space!
1918 // Initialize some new operands.
1919 NumOperands = OpNo+2;
1920 OperandList[OpNo] = V;
1921 OperandList[OpNo+1] = BB;
1924 /// removeIncomingValue - Remove an incoming value. This is useful if a
1925 /// predecessor basic block is deleted. The value removed is returned.
1927 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1928 /// is true), the PHI node is destroyed and any uses of it are replaced with
1929 /// dummy values. The only time there should be zero incoming values to a PHI
1930 /// node is when the block is dead, so this strategy is sound.
1932 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1934 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1935 int Idx = getBasicBlockIndex(BB);
1936 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1937 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1940 /// getBasicBlockIndex - Return the first index of the specified basic
1941 /// block in the value list for this PHI. Returns -1 if no instance.
1943 int getBasicBlockIndex(const BasicBlock *BB) const {
1944 Use *OL = OperandList;
1945 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1946 if (OL[i+1].get() == BB) return i/2;
1950 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1951 return getIncomingValue(getBasicBlockIndex(BB));
1954 /// hasConstantValue - If the specified PHI node always merges together the
1955 /// same value, return the value, otherwise return null.
1957 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1959 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1960 static inline bool classof(const PHINode *) { return true; }
1961 static inline bool classof(const Instruction *I) {
1962 return I->getOpcode() == Instruction::PHI;
1964 static inline bool classof(const Value *V) {
1965 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1968 void resizeOperands(unsigned NumOperands);
1972 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1975 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1978 //===----------------------------------------------------------------------===//
1980 //===----------------------------------------------------------------------===//
1982 //===---------------------------------------------------------------------------
1983 /// ReturnInst - Return a value (possibly void), from a function. Execution
1984 /// does not continue in this function any longer.
1986 class ReturnInst : public TerminatorInst {
1987 ReturnInst(const ReturnInst &RI);
1990 // ReturnInst constructors:
1991 // ReturnInst() - 'ret void' instruction
1992 // ReturnInst( null) - 'ret void' instruction
1993 // ReturnInst(Value* X) - 'ret X' instruction
1994 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1995 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1996 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1997 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1999 // NOTE: If the Value* passed is of type void then the constructor behaves as
2000 // if it was passed NULL.
2001 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2002 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2003 explicit ReturnInst(BasicBlock *InsertAtEnd);
2005 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2006 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2008 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2009 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2011 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2012 return new(0) ReturnInst(InsertAtEnd);
2014 virtual ~ReturnInst();
2016 virtual ReturnInst *clone(LLVMContext &Context) const;
2018 /// Provide fast operand accessors
2019 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2021 /// Convenience accessor
2022 Value *getReturnValue(unsigned n = 0) const {
2023 return n < getNumOperands()
2028 unsigned getNumSuccessors() const { return 0; }
2030 // Methods for support type inquiry through isa, cast, and dyn_cast:
2031 static inline bool classof(const ReturnInst *) { return true; }
2032 static inline bool classof(const Instruction *I) {
2033 return (I->getOpcode() == Instruction::Ret);
2035 static inline bool classof(const Value *V) {
2036 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2039 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2040 virtual unsigned getNumSuccessorsV() const;
2041 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2045 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2048 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2050 //===----------------------------------------------------------------------===//
2052 //===----------------------------------------------------------------------===//
2054 //===---------------------------------------------------------------------------
2055 /// BranchInst - Conditional or Unconditional Branch instruction.
2057 class BranchInst : public TerminatorInst {
2058 /// Ops list - Branches are strange. The operands are ordered:
2059 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2060 /// they don't have to check for cond/uncond branchness. These are mostly
2061 /// accessed relative from op_end().
2062 BranchInst(const BranchInst &BI);
2064 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2065 // BranchInst(BB *B) - 'br B'
2066 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2067 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2068 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2069 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2070 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2071 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2072 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2073 Instruction *InsertBefore = 0);
2074 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2075 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2076 BasicBlock *InsertAtEnd);
2078 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2079 return new(1, true) BranchInst(IfTrue, InsertBefore);
2081 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2082 Value *Cond, Instruction *InsertBefore = 0) {
2083 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2085 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2086 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2088 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2089 Value *Cond, BasicBlock *InsertAtEnd) {
2090 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2095 /// Transparently provide more efficient getOperand methods.
2096 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2098 virtual BranchInst *clone(LLVMContext &Context) const;
2100 bool isUnconditional() const { return getNumOperands() == 1; }
2101 bool isConditional() const { return getNumOperands() == 3; }
2103 Value *getCondition() const {
2104 assert(isConditional() && "Cannot get condition of an uncond branch!");
2108 void setCondition(Value *V) {
2109 assert(isConditional() && "Cannot set condition of unconditional branch!");
2113 // setUnconditionalDest - Change the current branch to an unconditional branch
2114 // targeting the specified block.
2115 // FIXME: Eliminate this ugly method.
2116 void setUnconditionalDest(BasicBlock *Dest) {
2118 if (isConditional()) { // Convert this to an uncond branch.
2122 OperandList = op_begin();
2126 unsigned getNumSuccessors() const { return 1+isConditional(); }
2128 BasicBlock *getSuccessor(unsigned i) const {
2129 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2130 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2133 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2134 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2135 *(&Op<-1>() - idx) = NewSucc;
2138 // Methods for support type inquiry through isa, cast, and dyn_cast:
2139 static inline bool classof(const BranchInst *) { return true; }
2140 static inline bool classof(const Instruction *I) {
2141 return (I->getOpcode() == Instruction::Br);
2143 static inline bool classof(const Value *V) {
2144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2147 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2148 virtual unsigned getNumSuccessorsV() const;
2149 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2153 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2155 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2157 //===----------------------------------------------------------------------===//
2159 //===----------------------------------------------------------------------===//
2161 //===---------------------------------------------------------------------------
2162 /// SwitchInst - Multiway switch
2164 class SwitchInst : public TerminatorInst {
2165 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2166 unsigned ReservedSpace;
2167 // Operand[0] = Value to switch on
2168 // Operand[1] = Default basic block destination
2169 // Operand[2n ] = Value to match
2170 // Operand[2n+1] = BasicBlock to go to on match
2171 SwitchInst(const SwitchInst &RI);
2172 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2173 void resizeOperands(unsigned No);
2174 // allocate space for exactly zero operands
2175 void *operator new(size_t s) {
2176 return User::operator new(s, 0);
2178 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2179 /// switch on and a default destination. The number of additional cases can
2180 /// be specified here to make memory allocation more efficient. This
2181 /// constructor can also autoinsert before another instruction.
2182 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2183 Instruction *InsertBefore = 0);
2185 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2186 /// switch on and a default destination. The number of additional cases can
2187 /// be specified here to make memory allocation more efficient. This
2188 /// constructor also autoinserts at the end of the specified BasicBlock.
2189 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2190 BasicBlock *InsertAtEnd);
2192 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2193 unsigned NumCases, Instruction *InsertBefore = 0) {
2194 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2196 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2197 unsigned NumCases, BasicBlock *InsertAtEnd) {
2198 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2202 /// Provide fast operand accessors
2203 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2205 // Accessor Methods for Switch stmt
2206 Value *getCondition() const { return getOperand(0); }
2207 void setCondition(Value *V) { setOperand(0, V); }
2209 BasicBlock *getDefaultDest() const {
2210 return cast<BasicBlock>(getOperand(1));
2213 /// getNumCases - return the number of 'cases' in this switch instruction.
2214 /// Note that case #0 is always the default case.
2215 unsigned getNumCases() const {
2216 return getNumOperands()/2;
2219 /// getCaseValue - Return the specified case value. Note that case #0, the
2220 /// default destination, does not have a case value.
2221 ConstantInt *getCaseValue(unsigned i) {
2222 assert(i && i < getNumCases() && "Illegal case value to get!");
2223 return getSuccessorValue(i);
2226 /// getCaseValue - Return the specified case value. Note that case #0, the
2227 /// default destination, does not have a case value.
2228 const ConstantInt *getCaseValue(unsigned i) const {
2229 assert(i && i < getNumCases() && "Illegal case value to get!");
2230 return getSuccessorValue(i);
2233 /// findCaseValue - Search all of the case values for the specified constant.
2234 /// If it is explicitly handled, return the case number of it, otherwise
2235 /// return 0 to indicate that it is handled by the default handler.
2236 unsigned findCaseValue(const ConstantInt *C) const {
2237 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2238 if (getCaseValue(i) == C)
2243 /// findCaseDest - Finds the unique case value for a given successor. Returns
2244 /// null if the successor is not found, not unique, or is the default case.
2245 ConstantInt *findCaseDest(BasicBlock *BB) {
2246 if (BB == getDefaultDest()) return NULL;
2248 ConstantInt *CI = NULL;
2249 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2250 if (getSuccessor(i) == BB) {
2251 if (CI) return NULL; // Multiple cases lead to BB.
2252 else CI = getCaseValue(i);
2258 /// addCase - Add an entry to the switch instruction...
2260 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2262 /// removeCase - This method removes the specified successor from the switch
2263 /// instruction. Note that this cannot be used to remove the default
2264 /// destination (successor #0).
2266 void removeCase(unsigned idx);
2268 virtual SwitchInst *clone(LLVMContext &Context) const;
2270 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2271 BasicBlock *getSuccessor(unsigned idx) const {
2272 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2273 return cast<BasicBlock>(getOperand(idx*2+1));
2275 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2276 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2277 setOperand(idx*2+1, NewSucc);
2280 // getSuccessorValue - Return the value associated with the specified
2282 ConstantInt *getSuccessorValue(unsigned idx) const {
2283 assert(idx < getNumSuccessors() && "Successor # out of range!");
2284 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2287 // Methods for support type inquiry through isa, cast, and dyn_cast:
2288 static inline bool classof(const SwitchInst *) { return true; }
2289 static inline bool classof(const Instruction *I) {
2290 return I->getOpcode() == Instruction::Switch;
2292 static inline bool classof(const Value *V) {
2293 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2296 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2297 virtual unsigned getNumSuccessorsV() const;
2298 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2302 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2305 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2308 //===----------------------------------------------------------------------===//
2310 //===----------------------------------------------------------------------===//
2312 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2313 /// calling convention of the call.
2315 class InvokeInst : public TerminatorInst {
2316 AttrListPtr AttributeList;
2317 InvokeInst(const InvokeInst &BI);
2318 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2319 Value* const *Args, unsigned NumArgs);
2321 template<typename InputIterator>
2322 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2323 InputIterator ArgBegin, InputIterator ArgEnd,
2324 const std::string &NameStr,
2325 // This argument ensures that we have an iterator we can
2326 // do arithmetic on in constant time
2327 std::random_access_iterator_tag) {
2328 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2330 // This requires that the iterator points to contiguous memory.
2331 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2335 /// Construct an InvokeInst given a range of arguments.
2336 /// InputIterator must be a random-access iterator pointing to
2337 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2338 /// made for random-accessness but not for contiguous storage as
2339 /// that would incur runtime overhead.
2341 /// @brief Construct an InvokeInst from a range of arguments
2342 template<typename InputIterator>
2343 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2344 InputIterator ArgBegin, InputIterator ArgEnd,
2346 const std::string &NameStr, Instruction *InsertBefore);
2348 /// Construct an InvokeInst given a range of arguments.
2349 /// InputIterator must be a random-access iterator pointing to
2350 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2351 /// made for random-accessness but not for contiguous storage as
2352 /// that would incur runtime overhead.
2354 /// @brief Construct an InvokeInst from a range of arguments
2355 template<typename InputIterator>
2356 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2357 InputIterator ArgBegin, InputIterator ArgEnd,
2359 const std::string &NameStr, BasicBlock *InsertAtEnd);
2361 template<typename InputIterator>
2362 static InvokeInst *Create(Value *Func,
2363 BasicBlock *IfNormal, BasicBlock *IfException,
2364 InputIterator ArgBegin, InputIterator ArgEnd,
2365 const std::string &NameStr = "",
2366 Instruction *InsertBefore = 0) {
2367 unsigned Values(ArgEnd - ArgBegin + 3);
2368 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2369 Values, NameStr, InsertBefore);
2371 template<typename InputIterator>
2372 static InvokeInst *Create(Value *Func,
2373 BasicBlock *IfNormal, BasicBlock *IfException,
2374 InputIterator ArgBegin, InputIterator ArgEnd,
2375 const std::string &NameStr,
2376 BasicBlock *InsertAtEnd) {
2377 unsigned Values(ArgEnd - ArgBegin + 3);
2378 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2379 Values, NameStr, InsertAtEnd);
2382 virtual InvokeInst *clone(LLVMContext &Context) const;
2384 /// Provide fast operand accessors
2385 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2387 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2389 unsigned getCallingConv() const { return SubclassData; }
2390 void setCallingConv(unsigned CC) {
2394 /// getAttributes - Return the parameter attributes for this invoke.
2396 const AttrListPtr &getAttributes() const { return AttributeList; }
2398 /// setAttributes - Set the parameter attributes for this invoke.
2400 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2402 /// addAttribute - adds the attribute to the list of attributes.
2403 void addAttribute(unsigned i, Attributes attr);
2405 /// removeAttribute - removes the attribute from the list of attributes.
2406 void removeAttribute(unsigned i, Attributes attr);
2408 /// @brief Determine whether the call or the callee has the given attribute.
2409 bool paramHasAttr(unsigned i, Attributes attr) const;
2411 /// @brief Extract the alignment for a call or parameter (0=unknown).
2412 unsigned getParamAlignment(unsigned i) const {
2413 return AttributeList.getParamAlignment(i);
2416 /// @brief Determine if the call does not access memory.
2417 bool doesNotAccessMemory() const {
2418 return paramHasAttr(~0, Attribute::ReadNone);
2420 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2421 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2422 else removeAttribute(~0, Attribute::ReadNone);
2425 /// @brief Determine if the call does not access or only reads memory.
2426 bool onlyReadsMemory() const {
2427 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2429 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2430 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2431 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2434 /// @brief Determine if the call cannot return.
2435 bool doesNotReturn() const {
2436 return paramHasAttr(~0, Attribute::NoReturn);
2438 void setDoesNotReturn(bool DoesNotReturn = true) {
2439 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2440 else removeAttribute(~0, Attribute::NoReturn);
2443 /// @brief Determine if the call cannot unwind.
2444 bool doesNotThrow() const {
2445 return paramHasAttr(~0, Attribute::NoUnwind);
2447 void setDoesNotThrow(bool DoesNotThrow = true) {
2448 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2449 else removeAttribute(~0, Attribute::NoUnwind);
2452 /// @brief Determine if the call returns a structure through first
2453 /// pointer argument.
2454 bool hasStructRetAttr() const {
2455 // Be friendly and also check the callee.
2456 return paramHasAttr(1, Attribute::StructRet);
2459 /// @brief Determine if any call argument is an aggregate passed by value.
2460 bool hasByValArgument() const {
2461 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2464 /// getCalledFunction - Return the function called, or null if this is an
2465 /// indirect function invocation.
2467 Function *getCalledFunction() const {
2468 return dyn_cast<Function>(getOperand(0));
2471 /// getCalledValue - Get a pointer to the function that is invoked by this
2473 const Value *getCalledValue() const { return getOperand(0); }
2474 Value *getCalledValue() { return getOperand(0); }
2476 // get*Dest - Return the destination basic blocks...
2477 BasicBlock *getNormalDest() const {
2478 return cast<BasicBlock>(getOperand(1));
2480 BasicBlock *getUnwindDest() const {
2481 return cast<BasicBlock>(getOperand(2));
2483 void setNormalDest(BasicBlock *B) {
2487 void setUnwindDest(BasicBlock *B) {
2491 BasicBlock *getSuccessor(unsigned i) const {
2492 assert(i < 2 && "Successor # out of range for invoke!");
2493 return i == 0 ? getNormalDest() : getUnwindDest();
2496 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2497 assert(idx < 2 && "Successor # out of range for invoke!");
2498 setOperand(idx+1, NewSucc);
2501 unsigned getNumSuccessors() const { return 2; }
2503 // Methods for support type inquiry through isa, cast, and dyn_cast:
2504 static inline bool classof(const InvokeInst *) { return true; }
2505 static inline bool classof(const Instruction *I) {
2506 return (I->getOpcode() == Instruction::Invoke);
2508 static inline bool classof(const Value *V) {
2509 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2512 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2513 virtual unsigned getNumSuccessorsV() const;
2514 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2518 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2521 template<typename InputIterator>
2522 InvokeInst::InvokeInst(Value *Func,
2523 BasicBlock *IfNormal, BasicBlock *IfException,
2524 InputIterator ArgBegin, InputIterator ArgEnd,
2526 const std::string &NameStr, Instruction *InsertBefore)
2527 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2528 ->getElementType())->getReturnType(),
2529 Instruction::Invoke,
2530 OperandTraits<InvokeInst>::op_end(this) - Values,
2531 Values, InsertBefore) {
2532 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2533 typename std::iterator_traits<InputIterator>::iterator_category());
2535 template<typename InputIterator>
2536 InvokeInst::InvokeInst(Value *Func,
2537 BasicBlock *IfNormal, BasicBlock *IfException,
2538 InputIterator ArgBegin, InputIterator ArgEnd,
2540 const std::string &NameStr, BasicBlock *InsertAtEnd)
2541 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2542 ->getElementType())->getReturnType(),
2543 Instruction::Invoke,
2544 OperandTraits<InvokeInst>::op_end(this) - Values,
2545 Values, InsertAtEnd) {
2546 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2547 typename std::iterator_traits<InputIterator>::iterator_category());
2550 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2552 //===----------------------------------------------------------------------===//
2554 //===----------------------------------------------------------------------===//
2556 //===---------------------------------------------------------------------------
2557 /// UnwindInst - Immediately exit the current function, unwinding the stack
2558 /// until an invoke instruction is found.
2560 class UnwindInst : public TerminatorInst {
2561 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2563 // allocate space for exactly zero operands
2564 void *operator new(size_t s) {
2565 return User::operator new(s, 0);
2567 explicit UnwindInst(Instruction *InsertBefore = 0);
2568 explicit UnwindInst(BasicBlock *InsertAtEnd);
2570 virtual UnwindInst *clone(LLVMContext &Context) const;
2572 unsigned getNumSuccessors() const { return 0; }
2574 // Methods for support type inquiry through isa, cast, and dyn_cast:
2575 static inline bool classof(const UnwindInst *) { return true; }
2576 static inline bool classof(const Instruction *I) {
2577 return I->getOpcode() == Instruction::Unwind;
2579 static inline bool classof(const Value *V) {
2580 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2583 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2584 virtual unsigned getNumSuccessorsV() const;
2585 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2588 //===----------------------------------------------------------------------===//
2589 // UnreachableInst Class
2590 //===----------------------------------------------------------------------===//
2592 //===---------------------------------------------------------------------------
2593 /// UnreachableInst - This function has undefined behavior. In particular, the
2594 /// presence of this instruction indicates some higher level knowledge that the
2595 /// end of the block cannot be reached.
2597 class UnreachableInst : public TerminatorInst {
2598 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2600 // allocate space for exactly zero operands
2601 void *operator new(size_t s) {
2602 return User::operator new(s, 0);
2604 explicit UnreachableInst(Instruction *InsertBefore = 0);
2605 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2607 virtual UnreachableInst *clone(LLVMContext &Context) const;
2609 unsigned getNumSuccessors() const { return 0; }
2611 // Methods for support type inquiry through isa, cast, and dyn_cast:
2612 static inline bool classof(const UnreachableInst *) { return true; }
2613 static inline bool classof(const Instruction *I) {
2614 return I->getOpcode() == Instruction::Unreachable;
2616 static inline bool classof(const Value *V) {
2617 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2620 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2621 virtual unsigned getNumSuccessorsV() const;
2622 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2625 //===----------------------------------------------------------------------===//
2627 //===----------------------------------------------------------------------===//
2629 /// @brief This class represents a truncation of integer types.
2630 class TruncInst : public CastInst {
2631 /// Private copy constructor
2632 TruncInst(const TruncInst &CI)
2633 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2636 /// @brief Constructor with insert-before-instruction semantics
2638 Value *S, ///< The value to be truncated
2639 const Type *Ty, ///< The (smaller) type to truncate to
2640 const std::string &NameStr = "", ///< A name for the new instruction
2641 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2644 /// @brief Constructor with insert-at-end-of-block semantics
2646 Value *S, ///< The value to be truncated
2647 const Type *Ty, ///< The (smaller) type to truncate to
2648 const std::string &NameStr, ///< A name for the new instruction
2649 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2652 /// @brief Clone an identical TruncInst
2653 virtual CastInst *clone(LLVMContext &Context) const;
2655 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2656 static inline bool classof(const TruncInst *) { return true; }
2657 static inline bool classof(const Instruction *I) {
2658 return I->getOpcode() == Trunc;
2660 static inline bool classof(const Value *V) {
2661 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2665 //===----------------------------------------------------------------------===//
2667 //===----------------------------------------------------------------------===//
2669 /// @brief This class represents zero extension of integer types.
2670 class ZExtInst : public CastInst {
2671 /// @brief Private copy constructor
2672 ZExtInst(const ZExtInst &CI)
2673 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2676 /// @brief Constructor with insert-before-instruction semantics
2678 Value *S, ///< The value to be zero extended
2679 const Type *Ty, ///< The type to zero extend to
2680 const std::string &NameStr = "", ///< A name for the new instruction
2681 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2684 /// @brief Constructor with insert-at-end semantics.
2686 Value *S, ///< The value to be zero extended
2687 const Type *Ty, ///< The type to zero extend to
2688 const std::string &NameStr, ///< A name for the new instruction
2689 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2692 /// @brief Clone an identical ZExtInst
2693 virtual CastInst *clone(LLVMContext &Context) const;
2695 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2696 static inline bool classof(const ZExtInst *) { return true; }
2697 static inline bool classof(const Instruction *I) {
2698 return I->getOpcode() == ZExt;
2700 static inline bool classof(const Value *V) {
2701 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2705 //===----------------------------------------------------------------------===//
2707 //===----------------------------------------------------------------------===//
2709 /// @brief This class represents a sign extension of integer types.
2710 class SExtInst : public CastInst {
2711 /// @brief Private copy constructor
2712 SExtInst(const SExtInst &CI)
2713 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2716 /// @brief Constructor with insert-before-instruction semantics
2718 Value *S, ///< The value to be sign extended
2719 const Type *Ty, ///< The type to sign extend to
2720 const std::string &NameStr = "", ///< A name for the new instruction
2721 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2724 /// @brief Constructor with insert-at-end-of-block semantics
2726 Value *S, ///< The value to be sign extended
2727 const Type *Ty, ///< The type to sign extend to
2728 const std::string &NameStr, ///< A name for the new instruction
2729 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2732 /// @brief Clone an identical SExtInst
2733 virtual CastInst *clone(LLVMContext &Context) const;
2735 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2736 static inline bool classof(const SExtInst *) { return true; }
2737 static inline bool classof(const Instruction *I) {
2738 return I->getOpcode() == SExt;
2740 static inline bool classof(const Value *V) {
2741 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2745 //===----------------------------------------------------------------------===//
2746 // FPTruncInst Class
2747 //===----------------------------------------------------------------------===//
2749 /// @brief This class represents a truncation of floating point types.
2750 class FPTruncInst : public CastInst {
2751 FPTruncInst(const FPTruncInst &CI)
2752 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2755 /// @brief Constructor with insert-before-instruction semantics
2757 Value *S, ///< The value to be truncated
2758 const Type *Ty, ///< The type to truncate to
2759 const std::string &NameStr = "", ///< A name for the new instruction
2760 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2763 /// @brief Constructor with insert-before-instruction semantics
2765 Value *S, ///< The value to be truncated
2766 const Type *Ty, ///< The type to truncate to
2767 const std::string &NameStr, ///< A name for the new instruction
2768 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2771 /// @brief Clone an identical FPTruncInst
2772 virtual CastInst *clone(LLVMContext &Context) const;
2774 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2775 static inline bool classof(const FPTruncInst *) { return true; }
2776 static inline bool classof(const Instruction *I) {
2777 return I->getOpcode() == FPTrunc;
2779 static inline bool classof(const Value *V) {
2780 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2784 //===----------------------------------------------------------------------===//
2786 //===----------------------------------------------------------------------===//
2788 /// @brief This class represents an extension of floating point types.
2789 class FPExtInst : public CastInst {
2790 FPExtInst(const FPExtInst &CI)
2791 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2794 /// @brief Constructor with insert-before-instruction semantics
2796 Value *S, ///< The value to be extended
2797 const Type *Ty, ///< The type to extend to
2798 const std::string &NameStr = "", ///< A name for the new instruction
2799 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2802 /// @brief Constructor with insert-at-end-of-block semantics
2804 Value *S, ///< The value to be extended
2805 const Type *Ty, ///< The type to extend to
2806 const std::string &NameStr, ///< A name for the new instruction
2807 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2810 /// @brief Clone an identical FPExtInst
2811 virtual CastInst *clone(LLVMContext &Context) const;
2813 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2814 static inline bool classof(const FPExtInst *) { return true; }
2815 static inline bool classof(const Instruction *I) {
2816 return I->getOpcode() == FPExt;
2818 static inline bool classof(const Value *V) {
2819 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2823 //===----------------------------------------------------------------------===//
2825 //===----------------------------------------------------------------------===//
2827 /// @brief This class represents a cast unsigned integer to floating point.
2828 class UIToFPInst : public CastInst {
2829 UIToFPInst(const UIToFPInst &CI)
2830 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2833 /// @brief Constructor with insert-before-instruction semantics
2835 Value *S, ///< The value to be converted
2836 const Type *Ty, ///< The type to convert to
2837 const std::string &NameStr = "", ///< A name for the new instruction
2838 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2841 /// @brief Constructor with insert-at-end-of-block semantics
2843 Value *S, ///< The value to be converted
2844 const Type *Ty, ///< The type to convert to
2845 const std::string &NameStr, ///< A name for the new instruction
2846 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2849 /// @brief Clone an identical UIToFPInst
2850 virtual CastInst *clone(LLVMContext &Context) const;
2852 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2853 static inline bool classof(const UIToFPInst *) { return true; }
2854 static inline bool classof(const Instruction *I) {
2855 return I->getOpcode() == UIToFP;
2857 static inline bool classof(const Value *V) {
2858 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2862 //===----------------------------------------------------------------------===//
2864 //===----------------------------------------------------------------------===//
2866 /// @brief This class represents a cast from signed integer to floating point.
2867 class SIToFPInst : public CastInst {
2868 SIToFPInst(const SIToFPInst &CI)
2869 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2872 /// @brief Constructor with insert-before-instruction semantics
2874 Value *S, ///< The value to be converted
2875 const Type *Ty, ///< The type to convert to
2876 const std::string &NameStr = "", ///< A name for the new instruction
2877 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2880 /// @brief Constructor with insert-at-end-of-block semantics
2882 Value *S, ///< The value to be converted
2883 const Type *Ty, ///< The type to convert to
2884 const std::string &NameStr, ///< A name for the new instruction
2885 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2888 /// @brief Clone an identical SIToFPInst
2889 virtual CastInst *clone(LLVMContext &Context) const;
2891 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2892 static inline bool classof(const SIToFPInst *) { return true; }
2893 static inline bool classof(const Instruction *I) {
2894 return I->getOpcode() == SIToFP;
2896 static inline bool classof(const Value *V) {
2897 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2901 //===----------------------------------------------------------------------===//
2903 //===----------------------------------------------------------------------===//
2905 /// @brief This class represents a cast from floating point to unsigned integer
2906 class FPToUIInst : public CastInst {
2907 FPToUIInst(const FPToUIInst &CI)
2908 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2911 /// @brief Constructor with insert-before-instruction semantics
2913 Value *S, ///< The value to be converted
2914 const Type *Ty, ///< The type to convert to
2915 const std::string &NameStr = "", ///< A name for the new instruction
2916 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2919 /// @brief Constructor with insert-at-end-of-block semantics
2921 Value *S, ///< The value to be converted
2922 const Type *Ty, ///< The type to convert to
2923 const std::string &NameStr, ///< A name for the new instruction
2924 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2927 /// @brief Clone an identical FPToUIInst
2928 virtual CastInst *clone(LLVMContext &Context) const;
2930 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2931 static inline bool classof(const FPToUIInst *) { return true; }
2932 static inline bool classof(const Instruction *I) {
2933 return I->getOpcode() == FPToUI;
2935 static inline bool classof(const Value *V) {
2936 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2940 //===----------------------------------------------------------------------===//
2942 //===----------------------------------------------------------------------===//
2944 /// @brief This class represents a cast from floating point to signed integer.
2945 class FPToSIInst : public CastInst {
2946 FPToSIInst(const FPToSIInst &CI)
2947 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2950 /// @brief Constructor with insert-before-instruction semantics
2952 Value *S, ///< The value to be converted
2953 const Type *Ty, ///< The type to convert to
2954 const std::string &NameStr = "", ///< A name for the new instruction
2955 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2958 /// @brief Constructor with insert-at-end-of-block semantics
2960 Value *S, ///< The value to be converted
2961 const Type *Ty, ///< The type to convert to
2962 const std::string &NameStr, ///< A name for the new instruction
2963 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2966 /// @brief Clone an identical FPToSIInst
2967 virtual CastInst *clone(LLVMContext &Context) const;
2969 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2970 static inline bool classof(const FPToSIInst *) { return true; }
2971 static inline bool classof(const Instruction *I) {
2972 return I->getOpcode() == FPToSI;
2974 static inline bool classof(const Value *V) {
2975 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2979 //===----------------------------------------------------------------------===//
2980 // IntToPtrInst Class
2981 //===----------------------------------------------------------------------===//
2983 /// @brief This class represents a cast from an integer to a pointer.
2984 class IntToPtrInst : public CastInst {
2985 IntToPtrInst(const IntToPtrInst &CI)
2986 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2989 /// @brief Constructor with insert-before-instruction semantics
2991 Value *S, ///< The value to be converted
2992 const Type *Ty, ///< The type to convert to
2993 const std::string &NameStr = "", ///< A name for the new instruction
2994 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2997 /// @brief Constructor with insert-at-end-of-block semantics
2999 Value *S, ///< The value to be converted
3000 const Type *Ty, ///< The type to convert to
3001 const std::string &NameStr, ///< A name for the new instruction
3002 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3005 /// @brief Clone an identical IntToPtrInst
3006 virtual CastInst *clone(LLVMContext &Context) const;
3008 // Methods for support type inquiry through isa, cast, and dyn_cast:
3009 static inline bool classof(const IntToPtrInst *) { return true; }
3010 static inline bool classof(const Instruction *I) {
3011 return I->getOpcode() == IntToPtr;
3013 static inline bool classof(const Value *V) {
3014 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3018 //===----------------------------------------------------------------------===//
3019 // PtrToIntInst Class
3020 //===----------------------------------------------------------------------===//
3022 /// @brief This class represents a cast from a pointer to an integer
3023 class PtrToIntInst : public CastInst {
3024 PtrToIntInst(const PtrToIntInst &CI)
3025 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3028 /// @brief Constructor with insert-before-instruction semantics
3030 Value *S, ///< The value to be converted
3031 const Type *Ty, ///< The type to convert to
3032 const std::string &NameStr = "", ///< A name for the new instruction
3033 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3036 /// @brief Constructor with insert-at-end-of-block semantics
3038 Value *S, ///< The value to be converted
3039 const Type *Ty, ///< The type to convert to
3040 const std::string &NameStr, ///< A name for the new instruction
3041 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3044 /// @brief Clone an identical PtrToIntInst
3045 virtual CastInst *clone(LLVMContext &Context) const;
3047 // Methods for support type inquiry through isa, cast, and dyn_cast:
3048 static inline bool classof(const PtrToIntInst *) { return true; }
3049 static inline bool classof(const Instruction *I) {
3050 return I->getOpcode() == PtrToInt;
3052 static inline bool classof(const Value *V) {
3053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3057 //===----------------------------------------------------------------------===//
3058 // BitCastInst Class
3059 //===----------------------------------------------------------------------===//
3061 /// @brief This class represents a no-op cast from one type to another.
3062 class BitCastInst : public CastInst {
3063 BitCastInst(const BitCastInst &CI)
3064 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3067 /// @brief Constructor with insert-before-instruction semantics
3069 Value *S, ///< The value to be casted
3070 const Type *Ty, ///< The type to casted to
3071 const std::string &NameStr = "", ///< A name for the new instruction
3072 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3075 /// @brief Constructor with insert-at-end-of-block semantics
3077 Value *S, ///< The value to be casted
3078 const Type *Ty, ///< The type to casted to
3079 const std::string &NameStr, ///< A name for the new instruction
3080 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3083 /// @brief Clone an identical BitCastInst
3084 virtual CastInst *clone(LLVMContext &Context) const;
3086 // Methods for support type inquiry through isa, cast, and dyn_cast:
3087 static inline bool classof(const BitCastInst *) { return true; }
3088 static inline bool classof(const Instruction *I) {
3089 return I->getOpcode() == BitCast;
3091 static inline bool classof(const Value *V) {
3092 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3096 } // End llvm namespace