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
21 #include "llvm/InstrTypes.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/ParameterAttributes.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, unsigned iTy, unsigned Align,
44 const std::string &Name = "", Instruction *InsertBefore = 0);
45 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
46 const std::string &Name, BasicBlock *InsertAtEnd);
48 // Out of line virtual method, so the vtable, etc has a home.
49 virtual ~AllocationInst();
51 /// isArrayAllocation - Return true if there is an allocation size parameter
52 /// to the allocation instruction that is not 1.
54 bool isArrayAllocation() const;
56 /// getArraySize - Get the number of element allocated, for a simple
57 /// allocation of a single element, this will return a constant 1 value.
59 const Value *getArraySize() const { return getOperand(0); }
60 Value *getArraySize() { return getOperand(0); }
62 /// getType - Overload to return most specific pointer type
64 const PointerType *getType() const {
65 return reinterpret_cast<const PointerType*>(Instruction::getType());
68 /// getAllocatedType - Return the type that is being allocated by the
71 const Type *getAllocatedType() const;
73 /// getAlignment - Return the alignment of the memory that is being allocated
74 /// by the instruction.
76 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
77 void setAlignment(unsigned Align);
79 virtual Instruction *clone() const = 0;
81 // Methods for support type inquiry through isa, cast, and dyn_cast:
82 static inline bool classof(const AllocationInst *) { return true; }
83 static inline bool classof(const Instruction *I) {
84 return I->getOpcode() == Instruction::Alloca ||
85 I->getOpcode() == Instruction::Malloc;
87 static inline bool classof(const Value *V) {
88 return isa<Instruction>(V) && classof(cast<Instruction>(V));
93 //===----------------------------------------------------------------------===//
95 //===----------------------------------------------------------------------===//
97 /// MallocInst - an instruction to allocated memory on the heap
99 class MallocInst : public AllocationInst {
100 MallocInst(const MallocInst &MI);
102 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
103 const std::string &Name = "",
104 Instruction *InsertBefore = 0)
105 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
106 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
107 BasicBlock *InsertAtEnd)
108 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
110 MallocInst(const Type *Ty, const std::string &Name,
111 Instruction *InsertBefore = 0)
112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
113 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
114 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
116 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
117 const std::string &Name, BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
119 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
120 const std::string &Name = "",
121 Instruction *InsertBefore = 0)
122 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
124 virtual MallocInst *clone() const;
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const MallocInst *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Malloc);
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 //===----------------------------------------------------------------------===//
139 //===----------------------------------------------------------------------===//
141 /// AllocaInst - an instruction to allocate memory on the stack
143 class AllocaInst : public AllocationInst {
144 AllocaInst(const AllocaInst &);
146 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
147 const std::string &Name = "",
148 Instruction *InsertBefore = 0)
149 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
150 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
151 BasicBlock *InsertAtEnd)
152 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
154 AllocaInst(const Type *Ty, const std::string &Name,
155 Instruction *InsertBefore = 0)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
157 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
158 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
160 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
161 const std::string &Name = "", Instruction *InsertBefore = 0)
162 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
163 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
164 const std::string &Name, BasicBlock *InsertAtEnd)
165 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
167 virtual AllocaInst *clone() const;
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const AllocaInst *) { return true; }
171 static inline bool classof(const Instruction *I) {
172 return (I->getOpcode() == Instruction::Alloca);
174 static inline bool classof(const Value *V) {
175 return isa<Instruction>(V) && classof(cast<Instruction>(V));
180 //===----------------------------------------------------------------------===//
182 //===----------------------------------------------------------------------===//
184 /// FreeInst - an instruction to deallocate memory
186 class FreeInst : public UnaryInstruction {
189 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
190 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
192 virtual FreeInst *clone() const;
194 // Accessor methods for consistency with other memory operations
195 Value *getPointerOperand() { return getOperand(0); }
196 const Value *getPointerOperand() const { return getOperand(0); }
198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static inline bool classof(const FreeInst *) { return true; }
200 static inline bool classof(const Instruction *I) {
201 return (I->getOpcode() == Instruction::Free);
203 static inline bool classof(const Value *V) {
204 return isa<Instruction>(V) && classof(cast<Instruction>(V));
209 //===----------------------------------------------------------------------===//
211 //===----------------------------------------------------------------------===//
213 /// LoadInst - an instruction for reading from memory. This uses the
214 /// SubclassData field in Value to store whether or not the load is volatile.
216 class LoadInst : public UnaryInstruction {
218 LoadInst(const LoadInst &LI)
219 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
220 setVolatile(LI.isVolatile());
221 setAlignment(LI.getAlignment());
229 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
230 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
231 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
232 Instruction *InsertBefore = 0);
233 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
234 Instruction *InsertBefore = 0);
235 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
236 BasicBlock *InsertAtEnd);
237 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
238 BasicBlock *InsertAtEnd);
240 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
241 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
242 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
243 Instruction *InsertBefore = 0);
244 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
245 BasicBlock *InsertAtEnd);
247 /// isVolatile - Return true if this is a load from a volatile memory
250 bool isVolatile() const { return SubclassData & 1; }
252 /// setVolatile - Specify whether this is a volatile load or not.
254 void setVolatile(bool V) {
255 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
258 virtual LoadInst *clone() const;
260 /// getAlignment - Return the alignment of the access that is being performed
262 unsigned getAlignment() const {
263 return (1 << (SubclassData>>1)) >> 1;
266 void setAlignment(unsigned Align);
268 Value *getPointerOperand() { return getOperand(0); }
269 const Value *getPointerOperand() const { return getOperand(0); }
270 static unsigned getPointerOperandIndex() { return 0U; }
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const LoadInst *) { return true; }
274 static inline bool classof(const Instruction *I) {
275 return I->getOpcode() == Instruction::Load;
277 static inline bool classof(const Value *V) {
278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
283 //===----------------------------------------------------------------------===//
285 //===----------------------------------------------------------------------===//
287 /// StoreInst - an instruction for storing to memory
289 class StoreInst : public Instruction {
292 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
293 Ops[0].init(SI.Ops[0], this);
294 Ops[1].init(SI.Ops[1], this);
295 setVolatile(SI.isVolatile());
296 setAlignment(SI.getAlignment());
304 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
305 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
306 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
307 Instruction *InsertBefore = 0);
308 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
309 unsigned Align, Instruction *InsertBefore = 0);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
312 unsigned Align, BasicBlock *InsertAtEnd);
315 /// isVolatile - Return true if this is a load from a volatile memory
318 bool isVolatile() const { return SubclassData & 1; }
320 /// setVolatile - Specify whether this is a volatile load or not.
322 void setVolatile(bool V) {
323 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
326 /// Transparently provide more efficient getOperand methods.
327 Value *getOperand(unsigned i) const {
328 assert(i < 2 && "getOperand() out of range!");
331 void setOperand(unsigned i, Value *Val) {
332 assert(i < 2 && "setOperand() out of range!");
335 unsigned getNumOperands() const { return 2; }
337 /// getAlignment - Return the alignment of the access that is being performed
339 unsigned getAlignment() const {
340 return (1 << (SubclassData>>1)) >> 1;
343 void setAlignment(unsigned Align);
345 virtual StoreInst *clone() const;
347 Value *getPointerOperand() { return getOperand(1); }
348 const Value *getPointerOperand() const { return getOperand(1); }
349 static unsigned getPointerOperandIndex() { return 1U; }
351 // Methods for support type inquiry through isa, cast, and dyn_cast:
352 static inline bool classof(const StoreInst *) { return true; }
353 static inline bool classof(const Instruction *I) {
354 return I->getOpcode() == Instruction::Store;
356 static inline bool classof(const Value *V) {
357 return isa<Instruction>(V) && classof(cast<Instruction>(V));
362 //===----------------------------------------------------------------------===//
363 // GetElementPtrInst Class
364 //===----------------------------------------------------------------------===//
366 // checkType - Simple wrapper function to give a better assertion failure
367 // message on bad indexes for a gep instruction.
369 static inline const Type *checkType(const Type *Ty) {
370 assert(Ty && "Invalid GetElementPtrInst indices for type!");
374 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
375 /// access elements of arrays and structs
377 class GetElementPtrInst : public Instruction {
378 GetElementPtrInst(const GetElementPtrInst &GEPI)
379 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
380 0, GEPI.getNumOperands()) {
381 Use *OL = OperandList = new Use[NumOperands];
382 Use *GEPIOL = GEPI.OperandList;
383 for (unsigned i = 0, E = NumOperands; i != E; ++i)
384 OL[i].init(GEPIOL[i], this);
386 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
387 void init(Value *Ptr, Value *Idx);
389 template<typename InputIterator>
390 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
391 const std::string &Name,
392 // This argument ensures that we have an iterator we can
393 // do arithmetic on in constant time
394 std::random_access_iterator_tag) {
395 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
396 std::distance(IdxBegin, IdxEnd);
399 // This requires that the itoerator points to contiguous memory.
400 init(Ptr, &*IdxBegin, NumIdx);
403 init(Ptr, 0, NumIdx);
409 /// getIndexedType - Returns the type of the element that would be loaded with
410 /// a load instruction with the specified parameters.
412 /// A null type is returned if the indices are invalid for the specified
415 static const Type *getIndexedType(const Type *Ptr,
416 Value* const *Idx, unsigned NumIdx,
417 bool AllowStructLeaf = false);
419 template<typename InputIterator>
420 static const Type *getIndexedType(const Type *Ptr,
421 InputIterator IdxBegin,
422 InputIterator IdxEnd,
423 bool AllowStructLeaf,
424 // This argument ensures that we
425 // have an iterator we can do
426 // arithmetic on in constant time
427 std::random_access_iterator_tag) {
428 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
429 std::distance(IdxBegin, IdxEnd);
432 // This requires that the iterator points to contiguous memory.
433 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
437 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
442 /// Constructors - Create a getelementptr instruction with a base pointer an
443 /// list of indices. The first ctor can optionally insert before an existing
444 /// instruction, the second appends the new instruction to the specified
446 template<typename InputIterator>
447 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
448 InputIterator IdxEnd,
449 const std::string &Name = "",
450 Instruction *InsertBefore =0)
451 : Instruction(PointerType::get(
452 checkType(getIndexedType(Ptr->getType(),
453 IdxBegin, IdxEnd, true)),
454 cast<PointerType>(Ptr->getType())->getAddressSpace()),
455 GetElementPtr, 0, 0, InsertBefore) {
456 init(Ptr, IdxBegin, IdxEnd, Name,
457 typename std::iterator_traits<InputIterator>::iterator_category());
459 template<typename InputIterator>
460 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
461 const std::string &Name, BasicBlock *InsertAtEnd)
462 : Instruction(PointerType::get(
463 checkType(getIndexedType(Ptr->getType(),
464 IdxBegin, IdxEnd, true)),
465 cast<PointerType>(Ptr->getType())->getAddressSpace()),
466 GetElementPtr, 0, 0, InsertAtEnd) {
467 init(Ptr, IdxBegin, IdxEnd, Name,
468 typename std::iterator_traits<InputIterator>::iterator_category());
471 /// Constructors - These two constructors are convenience methods because one
472 /// and two index getelementptr instructions are so common.
473 GetElementPtrInst(Value *Ptr, Value *Idx,
474 const std::string &Name = "", Instruction *InsertBefore =0);
475 GetElementPtrInst(Value *Ptr, Value *Idx,
476 const std::string &Name, BasicBlock *InsertAtEnd);
477 ~GetElementPtrInst();
479 virtual GetElementPtrInst *clone() const;
481 // getType - Overload to return most specific pointer type...
482 const PointerType *getType() const {
483 return reinterpret_cast<const PointerType*>(Instruction::getType());
486 /// getIndexedType - Returns the type of the element that would be loaded with
487 /// a load instruction with the specified parameters.
489 /// A null type is returned if the indices are invalid for the specified
492 template<typename InputIterator>
493 static const Type *getIndexedType(const Type *Ptr,
494 InputIterator IdxBegin,
495 InputIterator IdxEnd,
496 bool AllowStructLeaf = false) {
497 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
498 typename std::iterator_traits<InputIterator>::
499 iterator_category()));
501 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
503 inline op_iterator idx_begin() { return op_begin()+1; }
504 inline const_op_iterator idx_begin() const { return op_begin()+1; }
505 inline op_iterator idx_end() { return op_end(); }
506 inline const_op_iterator idx_end() const { return op_end(); }
508 Value *getPointerOperand() {
509 return getOperand(0);
511 const Value *getPointerOperand() const {
512 return getOperand(0);
514 static unsigned getPointerOperandIndex() {
515 return 0U; // get index for modifying correct operand
518 unsigned getNumIndices() const { // Note: always non-negative
519 return getNumOperands() - 1;
522 bool hasIndices() const {
523 return getNumOperands() > 1;
526 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
527 /// zeros. If so, the result pointer and the first operand have the same
528 /// value, just potentially different types.
529 bool hasAllZeroIndices() const;
531 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
532 /// constant integers. If so, the result pointer and the first operand have
533 /// a constant offset between them.
534 bool hasAllConstantIndices() const;
537 // Methods for support type inquiry through isa, cast, and dyn_cast:
538 static inline bool classof(const GetElementPtrInst *) { return true; }
539 static inline bool classof(const Instruction *I) {
540 return (I->getOpcode() == Instruction::GetElementPtr);
542 static inline bool classof(const Value *V) {
543 return isa<Instruction>(V) && classof(cast<Instruction>(V));
547 //===----------------------------------------------------------------------===//
549 //===----------------------------------------------------------------------===//
551 /// This instruction compares its operands according to the predicate given
552 /// to the constructor. It only operates on integers, pointers, or packed
553 /// vectors of integrals. The two operands must be the same type.
554 /// @brief Represent an integer comparison operator.
555 class ICmpInst: public CmpInst {
557 /// This enumeration lists the possible predicates for the ICmpInst. The
558 /// values in the range 0-31 are reserved for FCmpInst while values in the
559 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
560 /// predicate values are not overlapping between the classes.
562 ICMP_EQ = 32, ///< equal
563 ICMP_NE = 33, ///< not equal
564 ICMP_UGT = 34, ///< unsigned greater than
565 ICMP_UGE = 35, ///< unsigned greater or equal
566 ICMP_ULT = 36, ///< unsigned less than
567 ICMP_ULE = 37, ///< unsigned less or equal
568 ICMP_SGT = 38, ///< signed greater than
569 ICMP_SGE = 39, ///< signed greater or equal
570 ICMP_SLT = 40, ///< signed less than
571 ICMP_SLE = 41, ///< signed less or equal
572 FIRST_ICMP_PREDICATE = ICMP_EQ,
573 LAST_ICMP_PREDICATE = ICMP_SLE,
574 BAD_ICMP_PREDICATE = ICMP_SLE + 1
577 /// @brief Constructor with insert-before-instruction semantics.
579 Predicate pred, ///< The predicate to use for the comparison
580 Value *LHS, ///< The left-hand-side of the expression
581 Value *RHS, ///< The right-hand-side of the expression
582 const std::string &Name = "", ///< Name of the instruction
583 Instruction *InsertBefore = 0 ///< Where to insert
584 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
587 /// @brief Constructor with insert-at-block-end semantics.
589 Predicate pred, ///< The predicate to use for the comparison
590 Value *LHS, ///< The left-hand-side of the expression
591 Value *RHS, ///< The right-hand-side of the expression
592 const std::string &Name, ///< Name of the instruction
593 BasicBlock *InsertAtEnd ///< Block to insert into.
594 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
597 /// @brief Return the predicate for this instruction.
598 Predicate getPredicate() const { return Predicate(SubclassData); }
600 /// @brief Set the predicate for this instruction to the specified value.
601 void setPredicate(Predicate P) { SubclassData = P; }
603 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
604 /// @returns the inverse predicate for the instruction's current predicate.
605 /// @brief Return the inverse of the instruction's predicate.
606 Predicate getInversePredicate() const {
607 return getInversePredicate(getPredicate());
610 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
611 /// @returns the inverse predicate for predicate provided in \p pred.
612 /// @brief Return the inverse of a given predicate
613 static Predicate getInversePredicate(Predicate pred);
615 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
616 /// @returns the predicate that would be the result of exchanging the two
617 /// operands of the ICmpInst instruction without changing the result
619 /// @brief Return the predicate as if the operands were swapped
620 Predicate getSwappedPredicate() const {
621 return getSwappedPredicate(getPredicate());
624 /// This is a static version that you can use without an instruction
626 /// @brief Return the predicate as if the operands were swapped.
627 static Predicate getSwappedPredicate(Predicate pred);
629 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
630 /// @returns the predicate that would be the result if the operand were
631 /// regarded as signed.
632 /// @brief Return the signed version of the predicate
633 Predicate getSignedPredicate() const {
634 return getSignedPredicate(getPredicate());
637 /// This is a static version that you can use without an instruction.
638 /// @brief Return the signed version of the predicate.
639 static Predicate getSignedPredicate(Predicate pred);
641 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
642 /// @returns the predicate that would be the result if the operand were
643 /// regarded as unsigned.
644 /// @brief Return the unsigned version of the predicate
645 Predicate getUnsignedPredicate() const {
646 return getUnsignedPredicate(getPredicate());
649 /// This is a static version that you can use without an instruction.
650 /// @brief Return the unsigned version of the predicate.
651 static Predicate getUnsignedPredicate(Predicate pred);
653 /// isEquality - Return true if this predicate is either EQ or NE. This also
654 /// tests for commutativity.
655 static bool isEquality(Predicate P) {
656 return P == ICMP_EQ || P == ICMP_NE;
659 /// isEquality - Return true if this predicate is either EQ or NE. This also
660 /// tests for commutativity.
661 bool isEquality() const {
662 return isEquality(getPredicate());
665 /// @returns true if the predicate of this ICmpInst is commutative
666 /// @brief Determine if this relation is commutative.
667 bool isCommutative() const { return isEquality(); }
669 /// isRelational - Return true if the predicate is relational (not EQ or NE).
671 bool isRelational() const {
672 return !isEquality();
675 /// isRelational - Return true if the predicate is relational (not EQ or NE).
677 static bool isRelational(Predicate P) {
678 return !isEquality(P);
681 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
682 /// @brief Determine if this instruction's predicate is signed.
683 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
685 /// @returns true if the predicate provided is signed, false otherwise
686 /// @brief Determine if the predicate is signed.
687 static bool isSignedPredicate(Predicate pred);
689 /// Initialize a set of values that all satisfy the predicate with C.
690 /// @brief Make a ConstantRange for a relation with a constant value.
691 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
693 /// Exchange the two operands to this instruction in such a way that it does
694 /// not modify the semantics of the instruction. The predicate value may be
695 /// changed to retain the same result if the predicate is order dependent
697 /// @brief Swap operands and adjust predicate.
698 void swapOperands() {
699 SubclassData = getSwappedPredicate();
700 std::swap(Ops[0], Ops[1]);
703 virtual ICmpInst *clone() const;
705 // Methods for support type inquiry through isa, cast, and dyn_cast:
706 static inline bool classof(const ICmpInst *) { return true; }
707 static inline bool classof(const Instruction *I) {
708 return I->getOpcode() == Instruction::ICmp;
710 static inline bool classof(const Value *V) {
711 return isa<Instruction>(V) && classof(cast<Instruction>(V));
715 //===----------------------------------------------------------------------===//
717 //===----------------------------------------------------------------------===//
719 /// This instruction compares its operands according to the predicate given
720 /// to the constructor. It only operates on floating point values or packed
721 /// vectors of floating point values. The operands must be identical types.
722 /// @brief Represents a floating point comparison operator.
723 class FCmpInst: public CmpInst {
725 /// This enumeration lists the possible predicates for the FCmpInst. Values
726 /// in the range 0-31 are reserved for FCmpInst.
728 // Opcode U L G E Intuitive operation
729 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
730 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
731 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
732 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
733 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
734 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
735 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
736 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
737 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
738 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
739 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
740 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
741 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
742 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
743 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
744 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
745 FIRST_FCMP_PREDICATE = FCMP_FALSE,
746 LAST_FCMP_PREDICATE = FCMP_TRUE,
747 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
750 /// @brief Constructor with insert-before-instruction semantics.
752 Predicate pred, ///< The predicate to use for the comparison
753 Value *LHS, ///< The left-hand-side of the expression
754 Value *RHS, ///< The right-hand-side of the expression
755 const std::string &Name = "", ///< Name of the instruction
756 Instruction *InsertBefore = 0 ///< Where to insert
757 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
760 /// @brief Constructor with insert-at-block-end semantics.
762 Predicate pred, ///< The predicate to use for the comparison
763 Value *LHS, ///< The left-hand-side of the expression
764 Value *RHS, ///< The right-hand-side of the expression
765 const std::string &Name, ///< Name of the instruction
766 BasicBlock *InsertAtEnd ///< Block to insert into.
767 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
770 /// @brief Return the predicate for this instruction.
771 Predicate getPredicate() const { return Predicate(SubclassData); }
773 /// @brief Set the predicate for this instruction to the specified value.
774 void setPredicate(Predicate P) { SubclassData = P; }
776 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
777 /// @returns the inverse predicate for the instructions current predicate.
778 /// @brief Return the inverse of the predicate
779 Predicate getInversePredicate() const {
780 return getInversePredicate(getPredicate());
783 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
784 /// @returns the inverse predicate for \p pred.
785 /// @brief Return the inverse of a given predicate
786 static Predicate getInversePredicate(Predicate pred);
788 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
789 /// @returns the predicate that would be the result of exchanging the two
790 /// operands of the ICmpInst instruction without changing the result
792 /// @brief Return the predicate as if the operands were swapped
793 Predicate getSwappedPredicate() const {
794 return getSwappedPredicate(getPredicate());
797 /// This is a static version that you can use without an instruction
799 /// @brief Return the predicate as if the operands were swapped.
800 static Predicate getSwappedPredicate(Predicate Opcode);
802 /// This also tests for commutativity. If isEquality() returns true then
803 /// the predicate is also commutative. Only the equality predicates are
805 /// @returns true if the predicate of this instruction is EQ or NE.
806 /// @brief Determine if this is an equality predicate.
807 bool isEquality() const {
808 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
809 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
811 bool isCommutative() const { return isEquality(); }
813 /// @returns true if the predicate is relational (not EQ or NE).
814 /// @brief Determine if this a relational predicate.
815 bool isRelational() const { return !isEquality(); }
817 /// Exchange the two operands to this instruction in such a way that it does
818 /// not modify the semantics of the instruction. The predicate value may be
819 /// changed to retain the same result if the predicate is order dependent
821 /// @brief Swap operands and adjust predicate.
822 void swapOperands() {
823 SubclassData = getSwappedPredicate();
824 std::swap(Ops[0], Ops[1]);
827 virtual FCmpInst *clone() const;
829 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
830 static inline bool classof(const FCmpInst *) { return true; }
831 static inline bool classof(const Instruction *I) {
832 return I->getOpcode() == Instruction::FCmp;
834 static inline bool classof(const Value *V) {
835 return isa<Instruction>(V) && classof(cast<Instruction>(V));
839 //===----------------------------------------------------------------------===//
841 //===----------------------------------------------------------------------===//
842 /// CallInst - This class represents a function call, abstracting a target
843 /// machine's calling convention. This class uses low bit of the SubClassData
844 /// field to indicate whether or not this is a tail call. The rest of the bits
845 /// hold the calling convention of the call.
848 class CallInst : public Instruction {
849 PAListPtr ParamAttrs; ///< parameter attributes for call
850 CallInst(const CallInst &CI);
851 void init(Value *Func, Value* const *Params, unsigned NumParams);
852 void init(Value *Func, Value *Actual1, Value *Actual2);
853 void init(Value *Func, Value *Actual);
854 void init(Value *Func);
856 template<typename InputIterator>
857 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
858 const std::string &Name,
859 // This argument ensures that we have an iterator we can
860 // do arithmetic on in constant time
861 std::random_access_iterator_tag) {
862 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
864 // This requires that the iterator points to contiguous memory.
865 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
870 /// Construct a CallInst given a range of arguments. InputIterator
871 /// must be a random-access iterator pointing to contiguous storage
872 /// (e.g. a std::vector<>::iterator). Checks are made for
873 /// random-accessness but not for contiguous storage as that would
874 /// incur runtime overhead.
875 /// @brief Construct a CallInst from a range of arguments
876 template<typename InputIterator>
877 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
878 const std::string &Name = "", Instruction *InsertBefore = 0)
879 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
880 ->getElementType())->getReturnType(),
881 Instruction::Call, 0, 0, InsertBefore) {
882 init(Func, ArgBegin, ArgEnd, Name,
883 typename std::iterator_traits<InputIterator>::iterator_category());
886 /// Construct a CallInst given a range of arguments. InputIterator
887 /// must be a random-access iterator pointing to contiguous storage
888 /// (e.g. a std::vector<>::iterator). Checks are made for
889 /// random-accessness but not for contiguous storage as that would
890 /// incur runtime overhead.
891 /// @brief Construct a CallInst from a range of arguments
892 template<typename InputIterator>
893 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
894 const std::string &Name, BasicBlock *InsertAtEnd)
895 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
896 ->getElementType())->getReturnType(),
897 Instruction::Call, 0, 0, InsertAtEnd) {
898 init(Func, ArgBegin, ArgEnd, Name,
899 typename std::iterator_traits<InputIterator>::iterator_category());
902 CallInst(Value *F, Value *Actual, const std::string& Name = "",
903 Instruction *InsertBefore = 0);
904 CallInst(Value *F, Value *Actual, const std::string& Name,
905 BasicBlock *InsertAtEnd);
906 explicit CallInst(Value *F, const std::string &Name = "",
907 Instruction *InsertBefore = 0);
908 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
911 virtual CallInst *clone() const;
913 bool isTailCall() const { return SubclassData & 1; }
914 void setTailCall(bool isTailCall = true) {
915 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
918 /// getCallingConv/setCallingConv - Get or set the calling convention of this
920 unsigned getCallingConv() const { return SubclassData >> 1; }
921 void setCallingConv(unsigned CC) {
922 SubclassData = (SubclassData & 1) | (CC << 1);
925 /// getParamAttrs - Return the parameter attributes for this call.
927 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
929 /// setParamAttrs - Sets the parameter attributes for this call.
930 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
932 /// @brief Determine whether the call or the callee has the given attribute.
933 bool paramHasAttr(unsigned i, unsigned attr) const;
935 /// @brief Extract the alignment for a call or parameter (0=unknown).
936 unsigned getParamAlignment(unsigned i) const {
937 return ParamAttrs.getParamAlignment(i);
940 /// @brief Determine if the call does not access memory.
941 bool doesNotAccessMemory() const {
942 return paramHasAttr(0, ParamAttr::ReadNone);
945 /// @brief Determine if the call does not access or only reads memory.
946 bool onlyReadsMemory() const {
947 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
950 /// @brief Determine if the call cannot return.
951 bool doesNotReturn() const {
952 return paramHasAttr(0, ParamAttr::NoReturn);
955 /// @brief Determine if the call cannot unwind.
956 bool doesNotThrow() const {
957 return paramHasAttr(0, ParamAttr::NoUnwind);
959 void setDoesNotThrow(bool doesNotThrow = true);
961 /// @brief Determine if the call returns a structure through first
962 /// pointer argument.
963 bool hasStructRetAttr() const {
964 // Be friendly and also check the callee.
965 return paramHasAttr(1, ParamAttr::StructRet);
968 /// @brief Determine if any call argument is an aggregate passed by value.
969 bool hasByValArgument() const {
970 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
973 /// getCalledFunction - Return the function being called by this instruction
974 /// if it is a direct call. If it is a call through a function pointer,
976 Function *getCalledFunction() const {
977 return dyn_cast<Function>(getOperand(0));
980 /// getCalledValue - Get a pointer to the function that is invoked by this
982 const Value *getCalledValue() const { return getOperand(0); }
983 Value *getCalledValue() { return getOperand(0); }
985 // Methods for support type inquiry through isa, cast, and dyn_cast:
986 static inline bool classof(const CallInst *) { return true; }
987 static inline bool classof(const Instruction *I) {
988 return I->getOpcode() == Instruction::Call;
990 static inline bool classof(const Value *V) {
991 return isa<Instruction>(V) && classof(cast<Instruction>(V));
995 //===----------------------------------------------------------------------===//
997 //===----------------------------------------------------------------------===//
999 /// SelectInst - This class represents the LLVM 'select' instruction.
1001 class SelectInst : public Instruction {
1004 void init(Value *C, Value *S1, Value *S2) {
1005 Ops[0].init(C, this);
1006 Ops[1].init(S1, this);
1007 Ops[2].init(S2, this);
1010 SelectInst(const SelectInst &SI)
1011 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1012 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1015 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1016 Instruction *InsertBefore = 0)
1017 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
1021 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1022 BasicBlock *InsertAtEnd)
1023 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
1028 Value *getCondition() const { return Ops[0]; }
1029 Value *getTrueValue() const { return Ops[1]; }
1030 Value *getFalseValue() const { return Ops[2]; }
1032 /// Transparently provide more efficient getOperand methods.
1033 Value *getOperand(unsigned i) const {
1034 assert(i < 3 && "getOperand() out of range!");
1037 void setOperand(unsigned i, Value *Val) {
1038 assert(i < 3 && "setOperand() out of range!");
1041 unsigned getNumOperands() const { return 3; }
1043 OtherOps getOpcode() const {
1044 return static_cast<OtherOps>(Instruction::getOpcode());
1047 virtual SelectInst *clone() const;
1049 // Methods for support type inquiry through isa, cast, and dyn_cast:
1050 static inline bool classof(const SelectInst *) { return true; }
1051 static inline bool classof(const Instruction *I) {
1052 return I->getOpcode() == Instruction::Select;
1054 static inline bool classof(const Value *V) {
1055 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1059 //===----------------------------------------------------------------------===//
1061 //===----------------------------------------------------------------------===//
1063 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1064 /// an argument of the specified type given a va_list and increments that list
1066 class VAArgInst : public UnaryInstruction {
1067 VAArgInst(const VAArgInst &VAA)
1068 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1070 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1071 Instruction *InsertBefore = 0)
1072 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1075 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1076 BasicBlock *InsertAtEnd)
1077 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1081 virtual VAArgInst *clone() const;
1083 // Methods for support type inquiry through isa, cast, and dyn_cast:
1084 static inline bool classof(const VAArgInst *) { return true; }
1085 static inline bool classof(const Instruction *I) {
1086 return I->getOpcode() == VAArg;
1088 static inline bool classof(const Value *V) {
1089 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1093 //===----------------------------------------------------------------------===//
1094 // ExtractElementInst Class
1095 //===----------------------------------------------------------------------===//
1097 /// ExtractElementInst - This instruction extracts a single (scalar)
1098 /// element from a VectorType value
1100 class ExtractElementInst : public Instruction {
1102 ExtractElementInst(const ExtractElementInst &EE) :
1103 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1104 Ops[0].init(EE.Ops[0], this);
1105 Ops[1].init(EE.Ops[1], this);
1109 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1110 Instruction *InsertBefore = 0);
1111 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1112 Instruction *InsertBefore = 0);
1113 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1114 BasicBlock *InsertAtEnd);
1115 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1116 BasicBlock *InsertAtEnd);
1118 /// isValidOperands - Return true if an extractelement instruction can be
1119 /// formed with the specified operands.
1120 static bool isValidOperands(const Value *Vec, const Value *Idx);
1122 virtual ExtractElementInst *clone() const;
1124 /// Transparently provide more efficient getOperand methods.
1125 Value *getOperand(unsigned i) const {
1126 assert(i < 2 && "getOperand() out of range!");
1129 void setOperand(unsigned i, Value *Val) {
1130 assert(i < 2 && "setOperand() out of range!");
1133 unsigned getNumOperands() const { return 2; }
1135 // Methods for support type inquiry through isa, cast, and dyn_cast:
1136 static inline bool classof(const ExtractElementInst *) { return true; }
1137 static inline bool classof(const Instruction *I) {
1138 return I->getOpcode() == Instruction::ExtractElement;
1140 static inline bool classof(const Value *V) {
1141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1145 //===----------------------------------------------------------------------===//
1146 // InsertElementInst Class
1147 //===----------------------------------------------------------------------===//
1149 /// InsertElementInst - This instruction inserts a single (scalar)
1150 /// element into a VectorType value
1152 class InsertElementInst : public Instruction {
1154 InsertElementInst(const InsertElementInst &IE);
1156 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1157 const std::string &Name = "",Instruction *InsertBefore = 0);
1158 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1159 const std::string &Name = "",Instruction *InsertBefore = 0);
1160 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1161 const std::string &Name, BasicBlock *InsertAtEnd);
1162 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1163 const std::string &Name, BasicBlock *InsertAtEnd);
1165 /// isValidOperands - Return true if an insertelement instruction can be
1166 /// formed with the specified operands.
1167 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1170 virtual InsertElementInst *clone() const;
1172 /// getType - Overload to return most specific vector type.
1174 const VectorType *getType() const {
1175 return reinterpret_cast<const VectorType*>(Instruction::getType());
1178 /// Transparently provide more efficient getOperand methods.
1179 Value *getOperand(unsigned i) const {
1180 assert(i < 3 && "getOperand() out of range!");
1183 void setOperand(unsigned i, Value *Val) {
1184 assert(i < 3 && "setOperand() out of range!");
1187 unsigned getNumOperands() const { return 3; }
1189 // Methods for support type inquiry through isa, cast, and dyn_cast:
1190 static inline bool classof(const InsertElementInst *) { return true; }
1191 static inline bool classof(const Instruction *I) {
1192 return I->getOpcode() == Instruction::InsertElement;
1194 static inline bool classof(const Value *V) {
1195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1199 //===----------------------------------------------------------------------===//
1200 // ShuffleVectorInst Class
1201 //===----------------------------------------------------------------------===//
1203 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1206 class ShuffleVectorInst : public Instruction {
1208 ShuffleVectorInst(const ShuffleVectorInst &IE);
1210 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1211 const std::string &Name = "", Instruction *InsertBefor = 0);
1212 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1213 const std::string &Name, BasicBlock *InsertAtEnd);
1215 /// isValidOperands - Return true if a shufflevector instruction can be
1216 /// formed with the specified operands.
1217 static bool isValidOperands(const Value *V1, const Value *V2,
1220 virtual ShuffleVectorInst *clone() const;
1222 /// getType - Overload to return most specific vector type.
1224 const VectorType *getType() const {
1225 return reinterpret_cast<const VectorType*>(Instruction::getType());
1228 /// Transparently provide more efficient getOperand methods.
1229 const Value *getOperand(unsigned i) const {
1230 assert(i < 3 && "getOperand() out of range!");
1233 Value *getOperand(unsigned i) {
1234 assert(i < 3 && "getOperand() out of range!");
1237 void setOperand(unsigned i, Value *Val) {
1238 assert(i < 3 && "setOperand() out of range!");
1241 unsigned getNumOperands() const { return 3; }
1243 /// getMaskValue - Return the index from the shuffle mask for the specified
1244 /// output result. This is either -1 if the element is undef or a number less
1245 /// than 2*numelements.
1246 int getMaskValue(unsigned i) const;
1248 // Methods for support type inquiry through isa, cast, and dyn_cast:
1249 static inline bool classof(const ShuffleVectorInst *) { return true; }
1250 static inline bool classof(const Instruction *I) {
1251 return I->getOpcode() == Instruction::ShuffleVector;
1253 static inline bool classof(const Value *V) {
1254 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1259 //===----------------------------------------------------------------------===//
1261 //===----------------------------------------------------------------------===//
1263 // PHINode - The PHINode class is used to represent the magical mystical PHI
1264 // node, that can not exist in nature, but can be synthesized in a computer
1265 // scientist's overactive imagination.
1267 class PHINode : public Instruction {
1268 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1269 /// the number actually in use.
1270 unsigned ReservedSpace;
1271 PHINode(const PHINode &PN);
1273 explicit PHINode(const Type *Ty, const std::string &Name = "",
1274 Instruction *InsertBefore = 0)
1275 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1280 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1281 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1288 /// reserveOperandSpace - This method can be used to avoid repeated
1289 /// reallocation of PHI operand lists by reserving space for the correct
1290 /// number of operands before adding them. Unlike normal vector reserves,
1291 /// this method can also be used to trim the operand space.
1292 void reserveOperandSpace(unsigned NumValues) {
1293 resizeOperands(NumValues*2);
1296 virtual PHINode *clone() const;
1298 /// getNumIncomingValues - Return the number of incoming edges
1300 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1302 /// getIncomingValue - Return incoming value number x
1304 Value *getIncomingValue(unsigned i) const {
1305 assert(i*2 < getNumOperands() && "Invalid value number!");
1306 return getOperand(i*2);
1308 void setIncomingValue(unsigned i, Value *V) {
1309 assert(i*2 < getNumOperands() && "Invalid value number!");
1312 unsigned getOperandNumForIncomingValue(unsigned i) {
1316 /// getIncomingBlock - Return incoming basic block number x
1318 BasicBlock *getIncomingBlock(unsigned i) const {
1319 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1321 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1322 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1324 unsigned getOperandNumForIncomingBlock(unsigned i) {
1328 /// addIncoming - Add an incoming value to the end of the PHI list
1330 void addIncoming(Value *V, BasicBlock *BB) {
1331 assert(V && "PHI node got a null value!");
1332 assert(BB && "PHI node got a null basic block!");
1333 assert(getType() == V->getType() &&
1334 "All operands to PHI node must be the same type as the PHI node!");
1335 unsigned OpNo = NumOperands;
1336 if (OpNo+2 > ReservedSpace)
1337 resizeOperands(0); // Get more space!
1338 // Initialize some new operands.
1339 NumOperands = OpNo+2;
1340 OperandList[OpNo].init(V, this);
1341 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1344 /// removeIncomingValue - Remove an incoming value. This is useful if a
1345 /// predecessor basic block is deleted. The value removed is returned.
1347 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1348 /// is true), the PHI node is destroyed and any uses of it are replaced with
1349 /// dummy values. The only time there should be zero incoming values to a PHI
1350 /// node is when the block is dead, so this strategy is sound.
1352 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1354 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1355 int Idx = getBasicBlockIndex(BB);
1356 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1357 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1360 /// getBasicBlockIndex - Return the first index of the specified basic
1361 /// block in the value list for this PHI. Returns -1 if no instance.
1363 int getBasicBlockIndex(const BasicBlock *BB) const {
1364 Use *OL = OperandList;
1365 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1366 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1370 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1371 return getIncomingValue(getBasicBlockIndex(BB));
1374 /// hasConstantValue - If the specified PHI node always merges together the
1375 /// same value, return the value, otherwise return null.
1377 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1379 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1380 static inline bool classof(const PHINode *) { return true; }
1381 static inline bool classof(const Instruction *I) {
1382 return I->getOpcode() == Instruction::PHI;
1384 static inline bool classof(const Value *V) {
1385 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1388 void resizeOperands(unsigned NumOperands);
1391 //===----------------------------------------------------------------------===//
1393 //===----------------------------------------------------------------------===//
1395 //===---------------------------------------------------------------------------
1396 /// ReturnInst - Return a value (possibly void), from a function. Execution
1397 /// does not continue in this function any longer.
1399 class ReturnInst : public TerminatorInst {
1401 ReturnInst(const ReturnInst &RI);
1402 void init(Value * const* retVals, unsigned N);
1405 // ReturnInst constructors:
1406 // ReturnInst() - 'ret void' instruction
1407 // ReturnInst( null) - 'ret void' instruction
1408 // ReturnInst(Value* X) - 'ret X' instruction
1409 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1410 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1411 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1412 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1413 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1414 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1415 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
1417 // NOTE: If the Value* passed is of type void then the constructor behaves as
1418 // if it was passed NULL.
1419 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1420 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1421 ReturnInst(Value * const* retVals, unsigned N);
1422 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1423 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
1424 explicit ReturnInst(BasicBlock *InsertAtEnd);
1425 virtual ~ReturnInst();
1427 virtual ReturnInst *clone() const;
1429 Value *getOperand(unsigned n = 0) const {
1430 if (getNumOperands() > 1)
1431 return TerminatorInst::getOperand(n);
1436 Value *getReturnValue(unsigned n = 0) const {
1437 return getOperand(n);
1440 unsigned getNumSuccessors() const { return 0; }
1442 // Methods for support type inquiry through isa, cast, and dyn_cast:
1443 static inline bool classof(const ReturnInst *) { return true; }
1444 static inline bool classof(const Instruction *I) {
1445 return (I->getOpcode() == Instruction::Ret);
1447 static inline bool classof(const Value *V) {
1448 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1451 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1452 virtual unsigned getNumSuccessorsV() const;
1453 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1456 //===----------------------------------------------------------------------===//
1458 //===----------------------------------------------------------------------===//
1460 //===---------------------------------------------------------------------------
1461 /// BranchInst - Conditional or Unconditional Branch instruction.
1463 class BranchInst : public TerminatorInst {
1464 /// Ops list - Branches are strange. The operands are ordered:
1465 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1466 /// they don't have to check for cond/uncond branchness.
1468 BranchInst(const BranchInst &BI);
1471 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1472 // BranchInst(BB *B) - 'br B'
1473 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1474 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1475 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1476 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1477 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1478 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1479 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1480 Instruction *InsertBefore = 0);
1481 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1482 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1483 BasicBlock *InsertAtEnd);
1485 /// Transparently provide more efficient getOperand methods.
1486 Value *getOperand(unsigned i) const {
1487 assert(i < getNumOperands() && "getOperand() out of range!");
1490 void setOperand(unsigned i, Value *Val) {
1491 assert(i < getNumOperands() && "setOperand() out of range!");
1495 virtual BranchInst *clone() const;
1497 bool isUnconditional() const { return getNumOperands() == 1; }
1498 bool isConditional() const { return getNumOperands() == 3; }
1500 Value *getCondition() const {
1501 assert(isConditional() && "Cannot get condition of an uncond branch!");
1502 return getOperand(2);
1505 void setCondition(Value *V) {
1506 assert(isConditional() && "Cannot set condition of unconditional branch!");
1510 // setUnconditionalDest - Change the current branch to an unconditional branch
1511 // targeting the specified block.
1512 // FIXME: Eliminate this ugly method.
1513 void setUnconditionalDest(BasicBlock *Dest) {
1514 if (isConditional()) { // Convert this to an uncond branch.
1519 setOperand(0, reinterpret_cast<Value*>(Dest));
1522 unsigned getNumSuccessors() const { return 1+isConditional(); }
1524 BasicBlock *getSuccessor(unsigned i) const {
1525 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1526 return cast<BasicBlock>(getOperand(i));
1529 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1530 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1531 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1534 // Methods for support type inquiry through isa, cast, and dyn_cast:
1535 static inline bool classof(const BranchInst *) { return true; }
1536 static inline bool classof(const Instruction *I) {
1537 return (I->getOpcode() == Instruction::Br);
1539 static inline bool classof(const Value *V) {
1540 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1543 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1544 virtual unsigned getNumSuccessorsV() const;
1545 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1548 //===----------------------------------------------------------------------===//
1550 //===----------------------------------------------------------------------===//
1552 //===---------------------------------------------------------------------------
1553 /// SwitchInst - Multiway switch
1555 class SwitchInst : public TerminatorInst {
1556 unsigned ReservedSpace;
1557 // Operand[0] = Value to switch on
1558 // Operand[1] = Default basic block destination
1559 // Operand[2n ] = Value to match
1560 // Operand[2n+1] = BasicBlock to go to on match
1561 SwitchInst(const SwitchInst &RI);
1562 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1563 void resizeOperands(unsigned No);
1565 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1566 /// switch on and a default destination. The number of additional cases can
1567 /// be specified here to make memory allocation more efficient. This
1568 /// constructor can also autoinsert before another instruction.
1569 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1570 Instruction *InsertBefore = 0);
1572 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1573 /// switch on and a default destination. The number of additional cases can
1574 /// be specified here to make memory allocation more efficient. This
1575 /// constructor also autoinserts at the end of the specified BasicBlock.
1576 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1577 BasicBlock *InsertAtEnd);
1581 // Accessor Methods for Switch stmt
1582 Value *getCondition() const { return getOperand(0); }
1583 void setCondition(Value *V) { setOperand(0, V); }
1585 BasicBlock *getDefaultDest() const {
1586 return cast<BasicBlock>(getOperand(1));
1589 /// getNumCases - return the number of 'cases' in this switch instruction.
1590 /// Note that case #0 is always the default case.
1591 unsigned getNumCases() const {
1592 return getNumOperands()/2;
1595 /// getCaseValue - Return the specified case value. Note that case #0, the
1596 /// default destination, does not have a case value.
1597 ConstantInt *getCaseValue(unsigned i) {
1598 assert(i && i < getNumCases() && "Illegal case value to get!");
1599 return getSuccessorValue(i);
1602 /// getCaseValue - Return the specified case value. Note that case #0, the
1603 /// default destination, does not have a case value.
1604 const ConstantInt *getCaseValue(unsigned i) const {
1605 assert(i && i < getNumCases() && "Illegal case value to get!");
1606 return getSuccessorValue(i);
1609 /// findCaseValue - Search all of the case values for the specified constant.
1610 /// If it is explicitly handled, return the case number of it, otherwise
1611 /// return 0 to indicate that it is handled by the default handler.
1612 unsigned findCaseValue(const ConstantInt *C) const {
1613 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1614 if (getCaseValue(i) == C)
1619 /// findCaseDest - Finds the unique case value for a given successor. Returns
1620 /// null if the successor is not found, not unique, or is the default case.
1621 ConstantInt *findCaseDest(BasicBlock *BB) {
1622 if (BB == getDefaultDest()) return NULL;
1624 ConstantInt *CI = NULL;
1625 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1626 if (getSuccessor(i) == BB) {
1627 if (CI) return NULL; // Multiple cases lead to BB.
1628 else CI = getCaseValue(i);
1634 /// addCase - Add an entry to the switch instruction...
1636 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1638 /// removeCase - This method removes the specified successor from the switch
1639 /// instruction. Note that this cannot be used to remove the default
1640 /// destination (successor #0).
1642 void removeCase(unsigned idx);
1644 virtual SwitchInst *clone() const;
1646 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1647 BasicBlock *getSuccessor(unsigned idx) const {
1648 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1649 return cast<BasicBlock>(getOperand(idx*2+1));
1651 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1652 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1653 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1656 // getSuccessorValue - Return the value associated with the specified
1658 ConstantInt *getSuccessorValue(unsigned idx) const {
1659 assert(idx < getNumSuccessors() && "Successor # out of range!");
1660 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1663 // Methods for support type inquiry through isa, cast, and dyn_cast:
1664 static inline bool classof(const SwitchInst *) { return true; }
1665 static inline bool classof(const Instruction *I) {
1666 return I->getOpcode() == Instruction::Switch;
1668 static inline bool classof(const Value *V) {
1669 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1672 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1673 virtual unsigned getNumSuccessorsV() const;
1674 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1677 //===----------------------------------------------------------------------===//
1679 //===----------------------------------------------------------------------===//
1681 //===---------------------------------------------------------------------------
1683 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1684 /// calling convention of the call.
1686 class InvokeInst : public TerminatorInst {
1687 PAListPtr ParamAttrs;
1688 InvokeInst(const InvokeInst &BI);
1689 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1690 Value* const *Args, unsigned NumArgs);
1692 template<typename InputIterator>
1693 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1694 InputIterator ArgBegin, InputIterator ArgEnd,
1695 const std::string &Name,
1696 // This argument ensures that we have an iterator we can
1697 // do arithmetic on in constant time
1698 std::random_access_iterator_tag) {
1699 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
1701 // This requires that the iterator points to contiguous memory.
1702 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
1707 /// Construct an InvokeInst given a range of arguments.
1708 /// InputIterator must be a random-access iterator pointing to
1709 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1710 /// made for random-accessness but not for contiguous storage as
1711 /// that would incur runtime overhead.
1713 /// @brief Construct an InvokeInst from a range of arguments
1714 template<typename InputIterator>
1715 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1716 InputIterator ArgBegin, InputIterator ArgEnd,
1717 const std::string &Name = "", Instruction *InsertBefore = 0)
1718 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1719 ->getElementType())->getReturnType(),
1720 Instruction::Invoke, 0, 0, InsertBefore) {
1721 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1722 typename std::iterator_traits<InputIterator>::iterator_category());
1725 /// Construct an InvokeInst given a range of arguments.
1726 /// InputIterator must be a random-access iterator pointing to
1727 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1728 /// made for random-accessness but not for contiguous storage as
1729 /// that would incur runtime overhead.
1731 /// @brief Construct an InvokeInst from a range of arguments
1732 template<typename InputIterator>
1733 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1734 InputIterator ArgBegin, InputIterator ArgEnd,
1735 const std::string &Name, BasicBlock *InsertAtEnd)
1736 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1737 ->getElementType())->getReturnType(),
1738 Instruction::Invoke, 0, 0, InsertAtEnd) {
1739 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1740 typename std::iterator_traits<InputIterator>::iterator_category());
1745 virtual InvokeInst *clone() const;
1747 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1749 unsigned getCallingConv() const { return SubclassData; }
1750 void setCallingConv(unsigned CC) {
1754 /// getParamAttrs - Return the parameter attributes for this invoke.
1756 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
1758 /// setParamAttrs - Set the parameter attributes for this invoke.
1760 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
1762 /// @brief Determine whether the call or the callee has the given attribute.
1763 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
1765 /// @brief Extract the alignment for a call or parameter (0=unknown).
1766 unsigned getParamAlignment(unsigned i) const {
1767 return ParamAttrs.getParamAlignment(i);
1770 /// @brief Determine if the call does not access memory.
1771 bool doesNotAccessMemory() const {
1772 return paramHasAttr(0, ParamAttr::ReadNone);
1775 /// @brief Determine if the call does not access or only reads memory.
1776 bool onlyReadsMemory() const {
1777 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1780 /// @brief Determine if the call cannot return.
1781 bool doesNotReturn() const {
1782 return paramHasAttr(0, ParamAttr::NoReturn);
1785 /// @brief Determine if the call cannot unwind.
1786 bool doesNotThrow() const {
1787 return paramHasAttr(0, ParamAttr::NoUnwind);
1789 void setDoesNotThrow(bool doesNotThrow = true);
1791 /// @brief Determine if the call returns a structure through first
1792 /// pointer argument.
1793 bool hasStructRetAttr() const {
1794 // Be friendly and also check the callee.
1795 return paramHasAttr(1, ParamAttr::StructRet);
1798 /// getCalledFunction - Return the function called, or null if this is an
1799 /// indirect function invocation.
1801 Function *getCalledFunction() const {
1802 return dyn_cast<Function>(getOperand(0));
1805 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1806 Value *getCalledValue() const { return getOperand(0); }
1808 // get*Dest - Return the destination basic blocks...
1809 BasicBlock *getNormalDest() const {
1810 return cast<BasicBlock>(getOperand(1));
1812 BasicBlock *getUnwindDest() const {
1813 return cast<BasicBlock>(getOperand(2));
1815 void setNormalDest(BasicBlock *B) {
1816 setOperand(1, reinterpret_cast<Value*>(B));
1819 void setUnwindDest(BasicBlock *B) {
1820 setOperand(2, reinterpret_cast<Value*>(B));
1823 BasicBlock *getSuccessor(unsigned i) const {
1824 assert(i < 2 && "Successor # out of range for invoke!");
1825 return i == 0 ? getNormalDest() : getUnwindDest();
1828 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1829 assert(idx < 2 && "Successor # out of range for invoke!");
1830 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1833 unsigned getNumSuccessors() const { return 2; }
1835 // Methods for support type inquiry through isa, cast, and dyn_cast:
1836 static inline bool classof(const InvokeInst *) { return true; }
1837 static inline bool classof(const Instruction *I) {
1838 return (I->getOpcode() == Instruction::Invoke);
1840 static inline bool classof(const Value *V) {
1841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1844 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1845 virtual unsigned getNumSuccessorsV() const;
1846 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1850 //===----------------------------------------------------------------------===//
1852 //===----------------------------------------------------------------------===//
1854 //===---------------------------------------------------------------------------
1855 /// UnwindInst - Immediately exit the current function, unwinding the stack
1856 /// until an invoke instruction is found.
1858 class UnwindInst : public TerminatorInst {
1860 explicit UnwindInst(Instruction *InsertBefore = 0);
1861 explicit UnwindInst(BasicBlock *InsertAtEnd);
1863 virtual UnwindInst *clone() const;
1865 unsigned getNumSuccessors() const { return 0; }
1867 // Methods for support type inquiry through isa, cast, and dyn_cast:
1868 static inline bool classof(const UnwindInst *) { return true; }
1869 static inline bool classof(const Instruction *I) {
1870 return I->getOpcode() == Instruction::Unwind;
1872 static inline bool classof(const Value *V) {
1873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1876 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1877 virtual unsigned getNumSuccessorsV() const;
1878 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1881 //===----------------------------------------------------------------------===//
1882 // UnreachableInst Class
1883 //===----------------------------------------------------------------------===//
1885 //===---------------------------------------------------------------------------
1886 /// UnreachableInst - This function has undefined behavior. In particular, the
1887 /// presence of this instruction indicates some higher level knowledge that the
1888 /// end of the block cannot be reached.
1890 class UnreachableInst : public TerminatorInst {
1892 explicit UnreachableInst(Instruction *InsertBefore = 0);
1893 explicit UnreachableInst(BasicBlock *InsertAtEnd);
1895 virtual UnreachableInst *clone() const;
1897 unsigned getNumSuccessors() const { return 0; }
1899 // Methods for support type inquiry through isa, cast, and dyn_cast:
1900 static inline bool classof(const UnreachableInst *) { return true; }
1901 static inline bool classof(const Instruction *I) {
1902 return I->getOpcode() == Instruction::Unreachable;
1904 static inline bool classof(const Value *V) {
1905 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1908 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1909 virtual unsigned getNumSuccessorsV() const;
1910 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1913 //===----------------------------------------------------------------------===//
1915 //===----------------------------------------------------------------------===//
1917 /// @brief This class represents a truncation of integer types.
1918 class TruncInst : public CastInst {
1919 /// Private copy constructor
1920 TruncInst(const TruncInst &CI)
1921 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1924 /// @brief Constructor with insert-before-instruction semantics
1926 Value *S, ///< The value to be truncated
1927 const Type *Ty, ///< The (smaller) type to truncate to
1928 const std::string &Name = "", ///< A name for the new instruction
1929 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1932 /// @brief Constructor with insert-at-end-of-block semantics
1934 Value *S, ///< The value to be truncated
1935 const Type *Ty, ///< The (smaller) type to truncate to
1936 const std::string &Name, ///< A name for the new instruction
1937 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1940 /// @brief Clone an identical TruncInst
1941 virtual CastInst *clone() const;
1943 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1944 static inline bool classof(const TruncInst *) { return true; }
1945 static inline bool classof(const Instruction *I) {
1946 return I->getOpcode() == Trunc;
1948 static inline bool classof(const Value *V) {
1949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1953 //===----------------------------------------------------------------------===//
1955 //===----------------------------------------------------------------------===//
1957 /// @brief This class represents zero extension of integer types.
1958 class ZExtInst : public CastInst {
1959 /// @brief Private copy constructor
1960 ZExtInst(const ZExtInst &CI)
1961 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1964 /// @brief Constructor with insert-before-instruction semantics
1966 Value *S, ///< The value to be zero extended
1967 const Type *Ty, ///< The type to zero extend to
1968 const std::string &Name = "", ///< A name for the new instruction
1969 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1972 /// @brief Constructor with insert-at-end semantics.
1974 Value *S, ///< The value to be zero extended
1975 const Type *Ty, ///< The type to zero extend to
1976 const std::string &Name, ///< A name for the new instruction
1977 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1980 /// @brief Clone an identical ZExtInst
1981 virtual CastInst *clone() const;
1983 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1984 static inline bool classof(const ZExtInst *) { return true; }
1985 static inline bool classof(const Instruction *I) {
1986 return I->getOpcode() == ZExt;
1988 static inline bool classof(const Value *V) {
1989 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1993 //===----------------------------------------------------------------------===//
1995 //===----------------------------------------------------------------------===//
1997 /// @brief This class represents a sign extension of integer types.
1998 class SExtInst : public CastInst {
1999 /// @brief Private copy constructor
2000 SExtInst(const SExtInst &CI)
2001 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2004 /// @brief Constructor with insert-before-instruction semantics
2006 Value *S, ///< The value to be sign extended
2007 const Type *Ty, ///< The type to sign extend to
2008 const std::string &Name = "", ///< A name for the new instruction
2009 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2012 /// @brief Constructor with insert-at-end-of-block semantics
2014 Value *S, ///< The value to be sign extended
2015 const Type *Ty, ///< The type to sign extend to
2016 const std::string &Name, ///< A name for the new instruction
2017 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2020 /// @brief Clone an identical SExtInst
2021 virtual CastInst *clone() const;
2023 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2024 static inline bool classof(const SExtInst *) { return true; }
2025 static inline bool classof(const Instruction *I) {
2026 return I->getOpcode() == SExt;
2028 static inline bool classof(const Value *V) {
2029 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2033 //===----------------------------------------------------------------------===//
2034 // FPTruncInst Class
2035 //===----------------------------------------------------------------------===//
2037 /// @brief This class represents a truncation of floating point types.
2038 class FPTruncInst : public CastInst {
2039 FPTruncInst(const FPTruncInst &CI)
2040 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2043 /// @brief Constructor with insert-before-instruction semantics
2045 Value *S, ///< The value to be truncated
2046 const Type *Ty, ///< The type to truncate to
2047 const std::string &Name = "", ///< A name for the new instruction
2048 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2051 /// @brief Constructor with insert-before-instruction semantics
2053 Value *S, ///< The value to be truncated
2054 const Type *Ty, ///< The type to truncate to
2055 const std::string &Name, ///< A name for the new instruction
2056 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2059 /// @brief Clone an identical FPTruncInst
2060 virtual CastInst *clone() const;
2062 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2063 static inline bool classof(const FPTruncInst *) { return true; }
2064 static inline bool classof(const Instruction *I) {
2065 return I->getOpcode() == FPTrunc;
2067 static inline bool classof(const Value *V) {
2068 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2072 //===----------------------------------------------------------------------===//
2074 //===----------------------------------------------------------------------===//
2076 /// @brief This class represents an extension of floating point types.
2077 class FPExtInst : public CastInst {
2078 FPExtInst(const FPExtInst &CI)
2079 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2082 /// @brief Constructor with insert-before-instruction semantics
2084 Value *S, ///< The value to be extended
2085 const Type *Ty, ///< The type to extend to
2086 const std::string &Name = "", ///< A name for the new instruction
2087 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2090 /// @brief Constructor with insert-at-end-of-block semantics
2092 Value *S, ///< The value to be extended
2093 const Type *Ty, ///< The type to extend to
2094 const std::string &Name, ///< A name for the new instruction
2095 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2098 /// @brief Clone an identical FPExtInst
2099 virtual CastInst *clone() const;
2101 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2102 static inline bool classof(const FPExtInst *) { return true; }
2103 static inline bool classof(const Instruction *I) {
2104 return I->getOpcode() == FPExt;
2106 static inline bool classof(const Value *V) {
2107 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2111 //===----------------------------------------------------------------------===//
2113 //===----------------------------------------------------------------------===//
2115 /// @brief This class represents a cast unsigned integer to floating point.
2116 class UIToFPInst : public CastInst {
2117 UIToFPInst(const UIToFPInst &CI)
2118 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2121 /// @brief Constructor with insert-before-instruction semantics
2123 Value *S, ///< The value to be converted
2124 const Type *Ty, ///< The type to convert to
2125 const std::string &Name = "", ///< A name for the new instruction
2126 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2129 /// @brief Constructor with insert-at-end-of-block semantics
2131 Value *S, ///< The value to be converted
2132 const Type *Ty, ///< The type to convert to
2133 const std::string &Name, ///< A name for the new instruction
2134 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2137 /// @brief Clone an identical UIToFPInst
2138 virtual CastInst *clone() const;
2140 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2141 static inline bool classof(const UIToFPInst *) { return true; }
2142 static inline bool classof(const Instruction *I) {
2143 return I->getOpcode() == UIToFP;
2145 static inline bool classof(const Value *V) {
2146 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2150 //===----------------------------------------------------------------------===//
2152 //===----------------------------------------------------------------------===//
2154 /// @brief This class represents a cast from signed integer to floating point.
2155 class SIToFPInst : public CastInst {
2156 SIToFPInst(const SIToFPInst &CI)
2157 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2160 /// @brief Constructor with insert-before-instruction semantics
2162 Value *S, ///< The value to be converted
2163 const Type *Ty, ///< The type to convert to
2164 const std::string &Name = "", ///< A name for the new instruction
2165 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2168 /// @brief Constructor with insert-at-end-of-block semantics
2170 Value *S, ///< The value to be converted
2171 const Type *Ty, ///< The type to convert to
2172 const std::string &Name, ///< A name for the new instruction
2173 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2176 /// @brief Clone an identical SIToFPInst
2177 virtual CastInst *clone() const;
2179 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2180 static inline bool classof(const SIToFPInst *) { return true; }
2181 static inline bool classof(const Instruction *I) {
2182 return I->getOpcode() == SIToFP;
2184 static inline bool classof(const Value *V) {
2185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2189 //===----------------------------------------------------------------------===//
2191 //===----------------------------------------------------------------------===//
2193 /// @brief This class represents a cast from floating point to unsigned integer
2194 class FPToUIInst : public CastInst {
2195 FPToUIInst(const FPToUIInst &CI)
2196 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2199 /// @brief Constructor with insert-before-instruction semantics
2201 Value *S, ///< The value to be converted
2202 const Type *Ty, ///< The type to convert to
2203 const std::string &Name = "", ///< A name for the new instruction
2204 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2207 /// @brief Constructor with insert-at-end-of-block semantics
2209 Value *S, ///< The value to be converted
2210 const Type *Ty, ///< The type to convert to
2211 const std::string &Name, ///< A name for the new instruction
2212 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2215 /// @brief Clone an identical FPToUIInst
2216 virtual CastInst *clone() const;
2218 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2219 static inline bool classof(const FPToUIInst *) { return true; }
2220 static inline bool classof(const Instruction *I) {
2221 return I->getOpcode() == FPToUI;
2223 static inline bool classof(const Value *V) {
2224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2228 //===----------------------------------------------------------------------===//
2230 //===----------------------------------------------------------------------===//
2232 /// @brief This class represents a cast from floating point to signed integer.
2233 class FPToSIInst : public CastInst {
2234 FPToSIInst(const FPToSIInst &CI)
2235 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2238 /// @brief Constructor with insert-before-instruction semantics
2240 Value *S, ///< The value to be converted
2241 const Type *Ty, ///< The type to convert to
2242 const std::string &Name = "", ///< A name for the new instruction
2243 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2246 /// @brief Constructor with insert-at-end-of-block semantics
2248 Value *S, ///< The value to be converted
2249 const Type *Ty, ///< The type to convert to
2250 const std::string &Name, ///< A name for the new instruction
2251 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2254 /// @brief Clone an identical FPToSIInst
2255 virtual CastInst *clone() const;
2257 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2258 static inline bool classof(const FPToSIInst *) { return true; }
2259 static inline bool classof(const Instruction *I) {
2260 return I->getOpcode() == FPToSI;
2262 static inline bool classof(const Value *V) {
2263 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2267 //===----------------------------------------------------------------------===//
2268 // IntToPtrInst Class
2269 //===----------------------------------------------------------------------===//
2271 /// @brief This class represents a cast from an integer to a pointer.
2272 class IntToPtrInst : public CastInst {
2273 IntToPtrInst(const IntToPtrInst &CI)
2274 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2277 /// @brief Constructor with insert-before-instruction semantics
2279 Value *S, ///< The value to be converted
2280 const Type *Ty, ///< The type to convert to
2281 const std::string &Name = "", ///< A name for the new instruction
2282 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2285 /// @brief Constructor with insert-at-end-of-block semantics
2287 Value *S, ///< The value to be converted
2288 const Type *Ty, ///< The type to convert to
2289 const std::string &Name, ///< A name for the new instruction
2290 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2293 /// @brief Clone an identical IntToPtrInst
2294 virtual CastInst *clone() const;
2296 // Methods for support type inquiry through isa, cast, and dyn_cast:
2297 static inline bool classof(const IntToPtrInst *) { return true; }
2298 static inline bool classof(const Instruction *I) {
2299 return I->getOpcode() == IntToPtr;
2301 static inline bool classof(const Value *V) {
2302 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2306 //===----------------------------------------------------------------------===//
2307 // PtrToIntInst Class
2308 //===----------------------------------------------------------------------===//
2310 /// @brief This class represents a cast from a pointer to an integer
2311 class PtrToIntInst : public CastInst {
2312 PtrToIntInst(const PtrToIntInst &CI)
2313 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2316 /// @brief Constructor with insert-before-instruction semantics
2318 Value *S, ///< The value to be converted
2319 const Type *Ty, ///< The type to convert to
2320 const std::string &Name = "", ///< A name for the new instruction
2321 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2324 /// @brief Constructor with insert-at-end-of-block semantics
2326 Value *S, ///< The value to be converted
2327 const Type *Ty, ///< The type to convert to
2328 const std::string &Name, ///< A name for the new instruction
2329 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2332 /// @brief Clone an identical PtrToIntInst
2333 virtual CastInst *clone() const;
2335 // Methods for support type inquiry through isa, cast, and dyn_cast:
2336 static inline bool classof(const PtrToIntInst *) { return true; }
2337 static inline bool classof(const Instruction *I) {
2338 return I->getOpcode() == PtrToInt;
2340 static inline bool classof(const Value *V) {
2341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2345 //===----------------------------------------------------------------------===//
2346 // BitCastInst Class
2347 //===----------------------------------------------------------------------===//
2349 /// @brief This class represents a no-op cast from one type to another.
2350 class BitCastInst : public CastInst {
2351 BitCastInst(const BitCastInst &CI)
2352 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2355 /// @brief Constructor with insert-before-instruction semantics
2357 Value *S, ///< The value to be casted
2358 const Type *Ty, ///< The type to casted to
2359 const std::string &Name = "", ///< A name for the new instruction
2360 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2363 /// @brief Constructor with insert-at-end-of-block semantics
2365 Value *S, ///< The value to be casted
2366 const Type *Ty, ///< The type to casted to
2367 const std::string &Name, ///< A name for the new instruction
2368 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2371 /// @brief Clone an identical BitCastInst
2372 virtual CastInst *clone() const;
2374 // Methods for support type inquiry through isa, cast, and dyn_cast:
2375 static inline bool classof(const BitCastInst *) { return true; }
2376 static inline bool classof(const Instruction *I) {
2377 return I->getOpcode() == BitCast;
2379 static inline bool classof(const Value *V) {
2380 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2384 //===----------------------------------------------------------------------===//
2385 // GetResultInst Class
2386 //===----------------------------------------------------------------------===//
2388 /// GetResultInst - This instruction extracts individual result value from
2389 /// aggregate value, where aggregate value is returned by CallInst.
2391 class GetResultInst : public Instruction {
2394 GetResultInst(const GetResultInst &GRI) :
2395 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2396 Aggr.init(GRI.Aggr, this);
2401 explicit GetResultInst(Value *Aggr, unsigned index,
2402 const std::string &Name = "",
2403 Instruction *InsertBefore = 0);
2405 /// isValidOperands - Return true if an getresult instruction can be
2406 /// formed with the specified operands.
2407 static bool isValidOperands(const Value *Aggr, unsigned index);
2409 virtual GetResultInst *clone() const;
2411 Value *getAggregateValue() {
2412 return getOperand(0);
2415 const Value *getAggregateValue() const {
2416 return getOperand(0);
2419 unsigned getIndex() const {
2423 unsigned getNumOperands() const { return 1; }
2425 // Methods for support type inquiry through isa, cast, and dyn_cast:
2426 static inline bool classof(const GetResultInst *) { return true; }
2427 static inline bool classof(const Instruction *I) {
2428 return (I->getOpcode() == Instruction::GetResult);
2430 static inline bool classof(const Value *V) {
2431 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2435 } // End llvm namespace