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"
35 //===----------------------------------------------------------------------===//
36 // AllocationInst Class
37 //===----------------------------------------------------------------------===//
39 /// AllocationInst - This class is the common base class of MallocInst and
42 class AllocationInst : public UnaryInstruction {
45 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
46 const std::string &Name = "", Instruction *InsertBefore = 0);
47 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
48 const std::string &Name, 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 element 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 Alignment; }
79 void setAlignment(unsigned Align) {
80 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
84 virtual Instruction *clone() const = 0;
86 // Methods for support type inquiry through isa, cast, and dyn_cast:
87 static inline bool classof(const AllocationInst *) { return true; }
88 static inline bool classof(const Instruction *I) {
89 return I->getOpcode() == Instruction::Alloca ||
90 I->getOpcode() == Instruction::Malloc;
92 static inline bool classof(const Value *V) {
93 return isa<Instruction>(V) && classof(cast<Instruction>(V));
98 //===----------------------------------------------------------------------===//
100 //===----------------------------------------------------------------------===//
102 /// MallocInst - an instruction to allocated memory on the heap
104 class MallocInst : public AllocationInst {
105 MallocInst(const MallocInst &MI);
107 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
108 const std::string &Name = "",
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
111 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
112 BasicBlock *InsertAtEnd)
113 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
115 MallocInst(const Type *Ty, const std::string &Name,
116 Instruction *InsertBefore = 0)
117 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
118 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
121 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
122 const std::string &Name, BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
124 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
125 const std::string &Name = "",
126 Instruction *InsertBefore = 0)
127 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
129 virtual MallocInst *clone() const;
131 // Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const MallocInst *) { return true; }
133 static inline bool classof(const Instruction *I) {
134 return (I->getOpcode() == Instruction::Malloc);
136 static inline bool classof(const Value *V) {
137 return isa<Instruction>(V) && classof(cast<Instruction>(V));
142 //===----------------------------------------------------------------------===//
144 //===----------------------------------------------------------------------===//
146 /// AllocaInst - an instruction to allocate memory on the stack
148 class AllocaInst : public AllocationInst {
149 AllocaInst(const AllocaInst &);
151 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
152 const std::string &Name = "",
153 Instruction *InsertBefore = 0)
154 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
155 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
156 BasicBlock *InsertAtEnd)
157 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
159 AllocaInst(const Type *Ty, const std::string &Name,
160 Instruction *InsertBefore = 0)
161 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
162 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
163 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
166 const std::string &Name = "", Instruction *InsertBefore = 0)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
168 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
169 const std::string &Name, BasicBlock *InsertAtEnd)
170 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
172 virtual AllocaInst *clone() const;
174 // Methods for support type inquiry through isa, cast, and dyn_cast:
175 static inline bool classof(const AllocaInst *) { return true; }
176 static inline bool classof(const Instruction *I) {
177 return (I->getOpcode() == Instruction::Alloca);
179 static inline bool classof(const Value *V) {
180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 /// FreeInst - an instruction to deallocate memory
191 class FreeInst : public UnaryInstruction {
194 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
195 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
197 virtual FreeInst *clone() const;
199 // Accessor methods for consistency with other memory operations
200 Value *getPointerOperand() { return getOperand(0); }
201 const Value *getPointerOperand() const { return getOperand(0); }
203 // Methods for support type inquiry through isa, cast, and dyn_cast:
204 static inline bool classof(const FreeInst *) { return true; }
205 static inline bool classof(const Instruction *I) {
206 return (I->getOpcode() == Instruction::Free);
208 static inline bool classof(const Value *V) {
209 return isa<Instruction>(V) && classof(cast<Instruction>(V));
214 //===----------------------------------------------------------------------===//
216 //===----------------------------------------------------------------------===//
218 /// LoadInst - an instruction for reading from memory. This uses the
219 /// SubclassData field in Value to store whether or not the load is volatile.
221 class LoadInst : public UnaryInstruction {
223 LoadInst(const LoadInst &LI)
224 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
225 setVolatile(LI.isVolatile());
226 setAlignment(LI.getAlignment());
234 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
235 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
236 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
237 Instruction *InsertBefore = 0);
238 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
239 Instruction *InsertBefore = 0);
240 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
241 BasicBlock *InsertAtEnd);
242 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
243 BasicBlock *InsertAtEnd);
245 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
246 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
247 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
248 Instruction *InsertBefore = 0);
249 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
250 BasicBlock *InsertAtEnd);
252 /// isVolatile - Return true if this is a load from a volatile memory
255 bool isVolatile() const { return SubclassData & 1; }
257 /// setVolatile - Specify whether this is a volatile load or not.
259 void setVolatile(bool V) {
260 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
263 virtual LoadInst *clone() const;
265 /// getAlignment - Return the alignment of the access that is being performed
267 unsigned getAlignment() const {
268 return (1 << (SubclassData>>1)) >> 1;
271 void setAlignment(unsigned Align);
273 Value *getPointerOperand() { return getOperand(0); }
274 const Value *getPointerOperand() const { return getOperand(0); }
275 static unsigned getPointerOperandIndex() { return 0U; }
277 // Methods for support type inquiry through isa, cast, and dyn_cast:
278 static inline bool classof(const LoadInst *) { return true; }
279 static inline bool classof(const Instruction *I) {
280 return I->getOpcode() == Instruction::Load;
282 static inline bool classof(const Value *V) {
283 return isa<Instruction>(V) && classof(cast<Instruction>(V));
288 //===----------------------------------------------------------------------===//
290 //===----------------------------------------------------------------------===//
292 /// StoreInst - an instruction for storing to memory
294 class StoreInst : public Instruction {
297 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
298 Ops[0].init(SI.Ops[0], this);
299 Ops[1].init(SI.Ops[1], this);
300 setVolatile(SI.isVolatile());
301 setAlignment(SI.getAlignment());
309 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
310 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
311 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
312 Instruction *InsertBefore = 0);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
314 unsigned Align, Instruction *InsertBefore = 0);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
316 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317 unsigned Align, BasicBlock *InsertAtEnd);
320 /// isVolatile - Return true if this is a load from a volatile memory
323 bool isVolatile() const { return SubclassData & 1; }
325 /// setVolatile - Specify whether this is a volatile load or not.
327 void setVolatile(bool V) {
328 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
331 /// Transparently provide more efficient getOperand methods.
332 Value *getOperand(unsigned i) const {
333 assert(i < 2 && "getOperand() out of range!");
336 void setOperand(unsigned i, Value *Val) {
337 assert(i < 2 && "setOperand() out of range!");
340 unsigned getNumOperands() const { return 2; }
342 /// getAlignment - Return the alignment of the access that is being performed
344 unsigned getAlignment() const {
345 return (1 << (SubclassData>>1)) >> 1;
348 void setAlignment(unsigned Align);
350 virtual StoreInst *clone() const;
352 Value *getPointerOperand() { return getOperand(1); }
353 const Value *getPointerOperand() const { return getOperand(1); }
354 static unsigned getPointerOperandIndex() { return 1U; }
356 // Methods for support type inquiry through isa, cast, and dyn_cast:
357 static inline bool classof(const StoreInst *) { return true; }
358 static inline bool classof(const Instruction *I) {
359 return I->getOpcode() == Instruction::Store;
361 static inline bool classof(const Value *V) {
362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
367 //===----------------------------------------------------------------------===//
368 // GetElementPtrInst Class
369 //===----------------------------------------------------------------------===//
371 // checkType - Simple wrapper function to give a better assertion failure
372 // message on bad indexes for a gep instruction.
374 static inline const Type *checkType(const Type *Ty) {
375 assert(Ty && "Invalid GetElementPtrInst indices for type!");
379 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
380 /// access elements of arrays and structs
382 class GetElementPtrInst : public Instruction {
383 GetElementPtrInst(const GetElementPtrInst &GEPI)
384 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
385 0, GEPI.getNumOperands()) {
386 Use *OL = OperandList = new Use[NumOperands];
387 Use *GEPIOL = GEPI.OperandList;
388 for (unsigned i = 0, E = NumOperands; i != E; ++i)
389 OL[i].init(GEPIOL[i], this);
391 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
392 void init(Value *Ptr, Value *Idx);
394 template<typename InputIterator>
395 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
396 const std::string &Name,
397 // This argument ensures that we have an iterator we can
398 // do arithmetic on in constant time
399 std::random_access_iterator_tag) {
400 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
401 std::distance(IdxBegin, IdxEnd);
404 // This requires that the itoerator points to contiguous memory.
405 init(Ptr, &*IdxBegin, NumIdx);
408 init(Ptr, 0, NumIdx);
414 /// getIndexedType - Returns the type of the element that would be loaded with
415 /// a load instruction with the specified parameters.
417 /// A null type is returned if the indices are invalid for the specified
420 static const Type *getIndexedType(const Type *Ptr,
421 Value* const *Idx, unsigned NumIdx,
422 bool AllowStructLeaf = false);
424 template<typename InputIterator>
425 static const Type *getIndexedType(const Type *Ptr,
426 InputIterator IdxBegin,
427 InputIterator IdxEnd,
428 bool AllowStructLeaf,
429 // This argument ensures that we
430 // have an iterator we can do
431 // arithmetic on in constant time
432 std::random_access_iterator_tag) {
433 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
434 std::distance(IdxBegin, IdxEnd);
437 // This requires that the iterator points to contiguous memory.
438 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
442 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
447 /// Constructors - Create a getelementptr instruction with a base pointer an
448 /// list of indices. The first ctor can optionally insert before an existing
449 /// instruction, the second appends the new instruction to the specified
451 template<typename InputIterator>
452 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
453 InputIterator IdxEnd,
454 const std::string &Name = "",
455 Instruction *InsertBefore =0)
456 : Instruction(PointerType::get(
457 checkType(getIndexedType(Ptr->getType(),
458 IdxBegin, IdxEnd, true)),
459 cast<PointerType>(Ptr->getType())->getAddressSpace()),
460 GetElementPtr, 0, 0, InsertBefore) {
461 init(Ptr, IdxBegin, IdxEnd, Name,
462 typename std::iterator_traits<InputIterator>::iterator_category());
464 template<typename InputIterator>
465 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
466 const std::string &Name, BasicBlock *InsertAtEnd)
467 : Instruction(PointerType::get(
468 checkType(getIndexedType(Ptr->getType(),
469 IdxBegin, IdxEnd, true)),
470 cast<PointerType>(Ptr->getType())->getAddressSpace()),
471 GetElementPtr, 0, 0, InsertAtEnd) {
472 init(Ptr, IdxBegin, IdxEnd, Name,
473 typename std::iterator_traits<InputIterator>::iterator_category());
476 /// Constructors - These two constructors are convenience methods because one
477 /// and two index getelementptr instructions are so common.
478 GetElementPtrInst(Value *Ptr, Value *Idx,
479 const std::string &Name = "", Instruction *InsertBefore =0);
480 GetElementPtrInst(Value *Ptr, Value *Idx,
481 const std::string &Name, BasicBlock *InsertAtEnd);
482 ~GetElementPtrInst();
484 virtual GetElementPtrInst *clone() const;
486 // getType - Overload to return most specific pointer type...
487 const PointerType *getType() const {
488 return reinterpret_cast<const PointerType*>(Instruction::getType());
491 /// getIndexedType - Returns the type of the element that would be loaded with
492 /// a load instruction with the specified parameters.
494 /// A null type is returned if the indices are invalid for the specified
497 template<typename InputIterator>
498 static const Type *getIndexedType(const Type *Ptr,
499 InputIterator IdxBegin,
500 InputIterator IdxEnd,
501 bool AllowStructLeaf = false) {
502 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
503 typename std::iterator_traits<InputIterator>::
504 iterator_category()));
506 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
508 inline op_iterator idx_begin() { return op_begin()+1; }
509 inline const_op_iterator idx_begin() const { return op_begin()+1; }
510 inline op_iterator idx_end() { return op_end(); }
511 inline const_op_iterator idx_end() const { return op_end(); }
513 Value *getPointerOperand() {
514 return getOperand(0);
516 const Value *getPointerOperand() const {
517 return getOperand(0);
519 static unsigned getPointerOperandIndex() {
520 return 0U; // get index for modifying correct operand
523 unsigned getNumIndices() const { // Note: always non-negative
524 return getNumOperands() - 1;
527 bool hasIndices() const {
528 return getNumOperands() > 1;
531 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
532 /// zeros. If so, the result pointer and the first operand have the same
533 /// value, just potentially different types.
534 bool hasAllZeroIndices() const;
536 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
537 /// constant integers. If so, the result pointer and the first operand have
538 /// a constant offset between them.
539 bool hasAllConstantIndices() const;
542 // Methods for support type inquiry through isa, cast, and dyn_cast:
543 static inline bool classof(const GetElementPtrInst *) { return true; }
544 static inline bool classof(const Instruction *I) {
545 return (I->getOpcode() == Instruction::GetElementPtr);
547 static inline bool classof(const Value *V) {
548 return isa<Instruction>(V) && classof(cast<Instruction>(V));
552 //===----------------------------------------------------------------------===//
554 //===----------------------------------------------------------------------===//
556 /// This instruction compares its operands according to the predicate given
557 /// to the constructor. It only operates on integers, pointers, or packed
558 /// vectors of integrals. The two operands must be the same type.
559 /// @brief Represent an integer comparison operator.
560 class ICmpInst: public CmpInst {
562 /// This enumeration lists the possible predicates for the ICmpInst. The
563 /// values in the range 0-31 are reserved for FCmpInst while values in the
564 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
565 /// predicate values are not overlapping between the classes.
567 ICMP_EQ = 32, ///< equal
568 ICMP_NE = 33, ///< not equal
569 ICMP_UGT = 34, ///< unsigned greater than
570 ICMP_UGE = 35, ///< unsigned greater or equal
571 ICMP_ULT = 36, ///< unsigned less than
572 ICMP_ULE = 37, ///< unsigned less or equal
573 ICMP_SGT = 38, ///< signed greater than
574 ICMP_SGE = 39, ///< signed greater or equal
575 ICMP_SLT = 40, ///< signed less than
576 ICMP_SLE = 41, ///< signed less or equal
577 FIRST_ICMP_PREDICATE = ICMP_EQ,
578 LAST_ICMP_PREDICATE = ICMP_SLE,
579 BAD_ICMP_PREDICATE = ICMP_SLE + 1
582 /// @brief Constructor with insert-before-instruction semantics.
584 Predicate pred, ///< The predicate to use for the comparison
585 Value *LHS, ///< The left-hand-side of the expression
586 Value *RHS, ///< The right-hand-side of the expression
587 const std::string &Name = "", ///< Name of the instruction
588 Instruction *InsertBefore = 0 ///< Where to insert
589 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
592 /// @brief Constructor with insert-at-block-end semantics.
594 Predicate pred, ///< The predicate to use for the comparison
595 Value *LHS, ///< The left-hand-side of the expression
596 Value *RHS, ///< The right-hand-side of the expression
597 const std::string &Name, ///< Name of the instruction
598 BasicBlock *InsertAtEnd ///< Block to insert into.
599 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
602 /// @brief Return the predicate for this instruction.
603 Predicate getPredicate() const { return Predicate(SubclassData); }
605 /// @brief Set the predicate for this instruction to the specified value.
606 void setPredicate(Predicate P) { SubclassData = P; }
608 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
609 /// @returns the inverse predicate for the instruction's current predicate.
610 /// @brief Return the inverse of the instruction's predicate.
611 Predicate getInversePredicate() const {
612 return getInversePredicate(getPredicate());
615 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
616 /// @returns the inverse predicate for predicate provided in \p pred.
617 /// @brief Return the inverse of a given predicate
618 static Predicate getInversePredicate(Predicate pred);
620 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
621 /// @returns the predicate that would be the result of exchanging the two
622 /// operands of the ICmpInst instruction without changing the result
624 /// @brief Return the predicate as if the operands were swapped
625 Predicate getSwappedPredicate() const {
626 return getSwappedPredicate(getPredicate());
629 /// This is a static version that you can use without an instruction
631 /// @brief Return the predicate as if the operands were swapped.
632 static Predicate getSwappedPredicate(Predicate pred);
634 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
635 /// @returns the predicate that would be the result if the operand were
636 /// regarded as signed.
637 /// @brief Return the signed version of the predicate
638 Predicate getSignedPredicate() const {
639 return getSignedPredicate(getPredicate());
642 /// This is a static version that you can use without an instruction.
643 /// @brief Return the signed version of the predicate.
644 static Predicate getSignedPredicate(Predicate pred);
646 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
647 /// @returns the predicate that would be the result if the operand were
648 /// regarded as unsigned.
649 /// @brief Return the unsigned version of the predicate
650 Predicate getUnsignedPredicate() const {
651 return getUnsignedPredicate(getPredicate());
654 /// This is a static version that you can use without an instruction.
655 /// @brief Return the unsigned version of the predicate.
656 static Predicate getUnsignedPredicate(Predicate pred);
658 /// isEquality - Return true if this predicate is either EQ or NE. This also
659 /// tests for commutativity.
660 static bool isEquality(Predicate P) {
661 return P == ICMP_EQ || P == ICMP_NE;
664 /// isEquality - Return true if this predicate is either EQ or NE. This also
665 /// tests for commutativity.
666 bool isEquality() const {
667 return isEquality(getPredicate());
670 /// @returns true if the predicate of this ICmpInst is commutative
671 /// @brief Determine if this relation is commutative.
672 bool isCommutative() const { return isEquality(); }
674 /// isRelational - Return true if the predicate is relational (not EQ or NE).
676 bool isRelational() const {
677 return !isEquality();
680 /// isRelational - Return true if the predicate is relational (not EQ or NE).
682 static bool isRelational(Predicate P) {
683 return !isEquality(P);
686 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
687 /// @brief Determine if this instruction's predicate is signed.
688 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
690 /// @returns true if the predicate provided is signed, false otherwise
691 /// @brief Determine if the predicate is signed.
692 static bool isSignedPredicate(Predicate pred);
694 /// Initialize a set of values that all satisfy the predicate with C.
695 /// @brief Make a ConstantRange for a relation with a constant value.
696 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
698 /// Exchange the two operands to this instruction in such a way that it does
699 /// not modify the semantics of the instruction. The predicate value may be
700 /// changed to retain the same result if the predicate is order dependent
702 /// @brief Swap operands and adjust predicate.
703 void swapOperands() {
704 SubclassData = getSwappedPredicate();
705 std::swap(Ops[0], Ops[1]);
708 virtual ICmpInst *clone() const;
710 // Methods for support type inquiry through isa, cast, and dyn_cast:
711 static inline bool classof(const ICmpInst *) { return true; }
712 static inline bool classof(const Instruction *I) {
713 return I->getOpcode() == Instruction::ICmp;
715 static inline bool classof(const Value *V) {
716 return isa<Instruction>(V) && classof(cast<Instruction>(V));
720 //===----------------------------------------------------------------------===//
722 //===----------------------------------------------------------------------===//
724 /// This instruction compares its operands according to the predicate given
725 /// to the constructor. It only operates on floating point values or packed
726 /// vectors of floating point values. The operands must be identical types.
727 /// @brief Represents a floating point comparison operator.
728 class FCmpInst: public CmpInst {
730 /// This enumeration lists the possible predicates for the FCmpInst. Values
731 /// in the range 0-31 are reserved for FCmpInst.
733 // Opcode U L G E Intuitive operation
734 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
735 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
736 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
737 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
738 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
739 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
740 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
741 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
742 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
743 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
744 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
745 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
746 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
747 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
748 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
749 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
750 FIRST_FCMP_PREDICATE = FCMP_FALSE,
751 LAST_FCMP_PREDICATE = FCMP_TRUE,
752 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
755 /// @brief Constructor with insert-before-instruction semantics.
757 Predicate pred, ///< The predicate to use for the comparison
758 Value *LHS, ///< The left-hand-side of the expression
759 Value *RHS, ///< The right-hand-side of the expression
760 const std::string &Name = "", ///< Name of the instruction
761 Instruction *InsertBefore = 0 ///< Where to insert
762 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
765 /// @brief Constructor with insert-at-block-end semantics.
767 Predicate pred, ///< The predicate to use for the comparison
768 Value *LHS, ///< The left-hand-side of the expression
769 Value *RHS, ///< The right-hand-side of the expression
770 const std::string &Name, ///< Name of the instruction
771 BasicBlock *InsertAtEnd ///< Block to insert into.
772 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
775 /// @brief Return the predicate for this instruction.
776 Predicate getPredicate() const { return Predicate(SubclassData); }
778 /// @brief Set the predicate for this instruction to the specified value.
779 void setPredicate(Predicate P) { SubclassData = P; }
781 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
782 /// @returns the inverse predicate for the instructions current predicate.
783 /// @brief Return the inverse of the predicate
784 Predicate getInversePredicate() const {
785 return getInversePredicate(getPredicate());
788 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
789 /// @returns the inverse predicate for \p pred.
790 /// @brief Return the inverse of a given predicate
791 static Predicate getInversePredicate(Predicate pred);
793 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
794 /// @returns the predicate that would be the result of exchanging the two
795 /// operands of the ICmpInst instruction without changing the result
797 /// @brief Return the predicate as if the operands were swapped
798 Predicate getSwappedPredicate() const {
799 return getSwappedPredicate(getPredicate());
802 /// This is a static version that you can use without an instruction
804 /// @brief Return the predicate as if the operands were swapped.
805 static Predicate getSwappedPredicate(Predicate Opcode);
807 /// This also tests for commutativity. If isEquality() returns true then
808 /// the predicate is also commutative. Only the equality predicates are
810 /// @returns true if the predicate of this instruction is EQ or NE.
811 /// @brief Determine if this is an equality predicate.
812 bool isEquality() const {
813 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
814 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
816 bool isCommutative() const { return isEquality(); }
818 /// @returns true if the predicate is relational (not EQ or NE).
819 /// @brief Determine if this a relational predicate.
820 bool isRelational() const { return !isEquality(); }
822 /// Exchange the two operands to this instruction in such a way that it does
823 /// not modify the semantics of the instruction. The predicate value may be
824 /// changed to retain the same result if the predicate is order dependent
826 /// @brief Swap operands and adjust predicate.
827 void swapOperands() {
828 SubclassData = getSwappedPredicate();
829 std::swap(Ops[0], Ops[1]);
832 virtual FCmpInst *clone() const;
834 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
835 static inline bool classof(const FCmpInst *) { return true; }
836 static inline bool classof(const Instruction *I) {
837 return I->getOpcode() == Instruction::FCmp;
839 static inline bool classof(const Value *V) {
840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
844 //===----------------------------------------------------------------------===//
846 //===----------------------------------------------------------------------===//
847 /// CallInst - This class represents a function call, abstracting a target
848 /// machine's calling convention. This class uses low bit of the SubClassData
849 /// field to indicate whether or not this is a tail call. The rest of the bits
850 /// hold the calling convention of the call.
853 class CallInst : public Instruction {
854 const ParamAttrsList *ParamAttrs; ///< parameter attributes for call
855 CallInst(const CallInst &CI);
856 void init(Value *Func, Value* const *Params, unsigned NumParams);
857 void init(Value *Func, Value *Actual1, Value *Actual2);
858 void init(Value *Func, Value *Actual);
859 void init(Value *Func);
861 template<typename InputIterator>
862 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
863 const std::string &Name,
864 // This argument ensures that we have an iterator we can
865 // do arithmetic on in constant time
866 std::random_access_iterator_tag) {
867 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
869 // This requires that the iterator points to contiguous memory.
870 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
875 /// Construct a CallInst given a range of arguments. InputIterator
876 /// must be a random-access iterator pointing to contiguous storage
877 /// (e.g. a std::vector<>::iterator). Checks are made for
878 /// random-accessness but not for contiguous storage as that would
879 /// incur runtime overhead.
880 /// @brief Construct a CallInst from a range of arguments
881 template<typename InputIterator>
882 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
883 const std::string &Name = "", Instruction *InsertBefore = 0)
884 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
885 ->getElementType())->getReturnType(),
886 Instruction::Call, 0, 0, InsertBefore) {
887 init(Func, ArgBegin, ArgEnd, Name,
888 typename std::iterator_traits<InputIterator>::iterator_category());
891 /// Construct a CallInst given a range of arguments. InputIterator
892 /// must be a random-access iterator pointing to contiguous storage
893 /// (e.g. a std::vector<>::iterator). Checks are made for
894 /// random-accessness but not for contiguous storage as that would
895 /// incur runtime overhead.
896 /// @brief Construct a CallInst from a range of arguments
897 template<typename InputIterator>
898 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
899 const std::string &Name, BasicBlock *InsertAtEnd)
900 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
901 ->getElementType())->getReturnType(),
902 Instruction::Call, 0, 0, InsertAtEnd) {
903 init(Func, ArgBegin, ArgEnd, Name,
904 typename std::iterator_traits<InputIterator>::iterator_category());
907 CallInst(Value *F, Value *Actual, const std::string& Name = "",
908 Instruction *InsertBefore = 0);
909 CallInst(Value *F, Value *Actual, const std::string& Name,
910 BasicBlock *InsertAtEnd);
911 explicit CallInst(Value *F, const std::string &Name = "",
912 Instruction *InsertBefore = 0);
913 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
916 virtual CallInst *clone() const;
918 bool isTailCall() const { return SubclassData & 1; }
919 void setTailCall(bool isTailCall = true) {
920 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
923 /// getCallingConv/setCallingConv - Get or set the calling convention of this
925 unsigned getCallingConv() const { return SubclassData >> 1; }
926 void setCallingConv(unsigned CC) {
927 SubclassData = (SubclassData & 1) | (CC << 1);
930 /// Obtains a pointer to the ParamAttrsList object which holds the
931 /// parameter attributes information, if any.
932 /// @returns 0 if no attributes have been set.
933 /// @brief Get the parameter attributes.
934 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
936 /// Sets the parameter attributes for this CallInst. To construct a
937 /// ParamAttrsList, see ParameterAttributes.h
938 /// @brief Set the parameter attributes.
939 void setParamAttrs(const ParamAttrsList *attrs);
941 /// @brief Determine whether the call or the callee has the given attribute.
942 bool paramHasAttr(uint16_t i, unsigned attr) const;
944 /// @brief Extract the alignment for a call or parameter (0=unknown).
945 uint16_t getParamAlignment(uint16_t i) const;
947 /// @brief Determine if the call does not access memory.
948 bool doesNotAccessMemory() const;
950 /// @brief Determine if the call does not access or only reads memory.
951 bool onlyReadsMemory() const;
953 /// @brief Determine if the call cannot return.
954 bool doesNotReturn() const;
956 /// @brief Determine if the call cannot unwind.
957 bool doesNotThrow() const;
958 void setDoesNotThrow(bool doesNotThrow = true);
960 /// @brief Determine if the call returns a structure through first
961 /// pointer argument.
962 bool hasStructRetAttr() const;
964 /// @brief Determine if any call argument is an aggregate passed by value.
965 bool hasByValArgument() const;
967 /// getCalledFunction - Return the function being called by this instruction
968 /// if it is a direct call. If it is a call through a function pointer,
970 Function *getCalledFunction() const {
971 return dyn_cast<Function>(getOperand(0));
974 /// getCalledValue - Get a pointer to the function that is invoked by this
976 const Value *getCalledValue() const { return getOperand(0); }
977 Value *getCalledValue() { return getOperand(0); }
979 // Methods for support type inquiry through isa, cast, and dyn_cast:
980 static inline bool classof(const CallInst *) { return true; }
981 static inline bool classof(const Instruction *I) {
982 return I->getOpcode() == Instruction::Call;
984 static inline bool classof(const Value *V) {
985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
989 //===----------------------------------------------------------------------===//
991 //===----------------------------------------------------------------------===//
993 /// SelectInst - This class represents the LLVM 'select' instruction.
995 class SelectInst : public Instruction {
998 void init(Value *C, Value *S1, Value *S2) {
999 Ops[0].init(C, this);
1000 Ops[1].init(S1, this);
1001 Ops[2].init(S2, this);
1004 SelectInst(const SelectInst &SI)
1005 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1006 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1009 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1010 Instruction *InsertBefore = 0)
1011 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
1015 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1016 BasicBlock *InsertAtEnd)
1017 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
1022 Value *getCondition() const { return Ops[0]; }
1023 Value *getTrueValue() const { return Ops[1]; }
1024 Value *getFalseValue() const { return Ops[2]; }
1026 /// Transparently provide more efficient getOperand methods.
1027 Value *getOperand(unsigned i) const {
1028 assert(i < 3 && "getOperand() out of range!");
1031 void setOperand(unsigned i, Value *Val) {
1032 assert(i < 3 && "setOperand() out of range!");
1035 unsigned getNumOperands() const { return 3; }
1037 OtherOps getOpcode() const {
1038 return static_cast<OtherOps>(Instruction::getOpcode());
1041 virtual SelectInst *clone() const;
1043 // Methods for support type inquiry through isa, cast, and dyn_cast:
1044 static inline bool classof(const SelectInst *) { return true; }
1045 static inline bool classof(const Instruction *I) {
1046 return I->getOpcode() == Instruction::Select;
1048 static inline bool classof(const Value *V) {
1049 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1053 //===----------------------------------------------------------------------===//
1055 //===----------------------------------------------------------------------===//
1057 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1058 /// an argument of the specified type given a va_list and increments that list
1060 class VAArgInst : public UnaryInstruction {
1061 VAArgInst(const VAArgInst &VAA)
1062 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1064 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1065 Instruction *InsertBefore = 0)
1066 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1069 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1070 BasicBlock *InsertAtEnd)
1071 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1075 virtual VAArgInst *clone() const;
1077 // Methods for support type inquiry through isa, cast, and dyn_cast:
1078 static inline bool classof(const VAArgInst *) { return true; }
1079 static inline bool classof(const Instruction *I) {
1080 return I->getOpcode() == VAArg;
1082 static inline bool classof(const Value *V) {
1083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1087 //===----------------------------------------------------------------------===//
1088 // ExtractElementInst Class
1089 //===----------------------------------------------------------------------===//
1091 /// ExtractElementInst - This instruction extracts a single (scalar)
1092 /// element from a VectorType value
1094 class ExtractElementInst : public Instruction {
1096 ExtractElementInst(const ExtractElementInst &EE) :
1097 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1098 Ops[0].init(EE.Ops[0], this);
1099 Ops[1].init(EE.Ops[1], this);
1103 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1104 Instruction *InsertBefore = 0);
1105 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1106 Instruction *InsertBefore = 0);
1107 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1108 BasicBlock *InsertAtEnd);
1109 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1110 BasicBlock *InsertAtEnd);
1112 /// isValidOperands - Return true if an extractelement instruction can be
1113 /// formed with the specified operands.
1114 static bool isValidOperands(const Value *Vec, const Value *Idx);
1116 virtual ExtractElementInst *clone() const;
1118 /// Transparently provide more efficient getOperand methods.
1119 Value *getOperand(unsigned i) const {
1120 assert(i < 2 && "getOperand() out of range!");
1123 void setOperand(unsigned i, Value *Val) {
1124 assert(i < 2 && "setOperand() out of range!");
1127 unsigned getNumOperands() const { return 2; }
1129 // Methods for support type inquiry through isa, cast, and dyn_cast:
1130 static inline bool classof(const ExtractElementInst *) { return true; }
1131 static inline bool classof(const Instruction *I) {
1132 return I->getOpcode() == Instruction::ExtractElement;
1134 static inline bool classof(const Value *V) {
1135 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1139 //===----------------------------------------------------------------------===//
1140 // InsertElementInst Class
1141 //===----------------------------------------------------------------------===//
1143 /// InsertElementInst - This instruction inserts a single (scalar)
1144 /// element into a VectorType value
1146 class InsertElementInst : public Instruction {
1148 InsertElementInst(const InsertElementInst &IE);
1150 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1151 const std::string &Name = "",Instruction *InsertBefore = 0);
1152 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1153 const std::string &Name = "",Instruction *InsertBefore = 0);
1154 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1155 const std::string &Name, BasicBlock *InsertAtEnd);
1156 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1157 const std::string &Name, BasicBlock *InsertAtEnd);
1159 /// isValidOperands - Return true if an insertelement instruction can be
1160 /// formed with the specified operands.
1161 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1164 virtual InsertElementInst *clone() const;
1166 /// getType - Overload to return most specific vector type.
1168 const VectorType *getType() const {
1169 return reinterpret_cast<const VectorType*>(Instruction::getType());
1172 /// Transparently provide more efficient getOperand methods.
1173 Value *getOperand(unsigned i) const {
1174 assert(i < 3 && "getOperand() out of range!");
1177 void setOperand(unsigned i, Value *Val) {
1178 assert(i < 3 && "setOperand() out of range!");
1181 unsigned getNumOperands() const { return 3; }
1183 // Methods for support type inquiry through isa, cast, and dyn_cast:
1184 static inline bool classof(const InsertElementInst *) { return true; }
1185 static inline bool classof(const Instruction *I) {
1186 return I->getOpcode() == Instruction::InsertElement;
1188 static inline bool classof(const Value *V) {
1189 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1193 //===----------------------------------------------------------------------===//
1194 // ShuffleVectorInst Class
1195 //===----------------------------------------------------------------------===//
1197 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1200 class ShuffleVectorInst : public Instruction {
1202 ShuffleVectorInst(const ShuffleVectorInst &IE);
1204 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1205 const std::string &Name = "", Instruction *InsertBefor = 0);
1206 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1207 const std::string &Name, BasicBlock *InsertAtEnd);
1209 /// isValidOperands - Return true if a shufflevector instruction can be
1210 /// formed with the specified operands.
1211 static bool isValidOperands(const Value *V1, const Value *V2,
1214 virtual ShuffleVectorInst *clone() const;
1216 /// getType - Overload to return most specific vector type.
1218 const VectorType *getType() const {
1219 return reinterpret_cast<const VectorType*>(Instruction::getType());
1222 /// Transparently provide more efficient getOperand methods.
1223 const Value *getOperand(unsigned i) const {
1224 assert(i < 3 && "getOperand() out of range!");
1227 Value *getOperand(unsigned i) {
1228 assert(i < 3 && "getOperand() out of range!");
1231 void setOperand(unsigned i, Value *Val) {
1232 assert(i < 3 && "setOperand() out of range!");
1235 unsigned getNumOperands() const { return 3; }
1237 /// getMaskValue - Return the index from the shuffle mask for the specified
1238 /// output result. This is either -1 if the element is undef or a number less
1239 /// than 2*numelements.
1240 int getMaskValue(unsigned i) const;
1242 // Methods for support type inquiry through isa, cast, and dyn_cast:
1243 static inline bool classof(const ShuffleVectorInst *) { return true; }
1244 static inline bool classof(const Instruction *I) {
1245 return I->getOpcode() == Instruction::ShuffleVector;
1247 static inline bool classof(const Value *V) {
1248 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1253 //===----------------------------------------------------------------------===//
1255 //===----------------------------------------------------------------------===//
1257 // PHINode - The PHINode class is used to represent the magical mystical PHI
1258 // node, that can not exist in nature, but can be synthesized in a computer
1259 // scientist's overactive imagination.
1261 class PHINode : public Instruction {
1262 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1263 /// the number actually in use.
1264 unsigned ReservedSpace;
1265 PHINode(const PHINode &PN);
1267 explicit PHINode(const Type *Ty, const std::string &Name = "",
1268 Instruction *InsertBefore = 0)
1269 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1274 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1275 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1282 /// reserveOperandSpace - This method can be used to avoid repeated
1283 /// reallocation of PHI operand lists by reserving space for the correct
1284 /// number of operands before adding them. Unlike normal vector reserves,
1285 /// this method can also be used to trim the operand space.
1286 void reserveOperandSpace(unsigned NumValues) {
1287 resizeOperands(NumValues*2);
1290 virtual PHINode *clone() const;
1292 /// getNumIncomingValues - Return the number of incoming edges
1294 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1296 /// getIncomingValue - Return incoming value number x
1298 Value *getIncomingValue(unsigned i) const {
1299 assert(i*2 < getNumOperands() && "Invalid value number!");
1300 return getOperand(i*2);
1302 void setIncomingValue(unsigned i, Value *V) {
1303 assert(i*2 < getNumOperands() && "Invalid value number!");
1306 unsigned getOperandNumForIncomingValue(unsigned i) {
1310 /// getIncomingBlock - Return incoming basic block number x
1312 BasicBlock *getIncomingBlock(unsigned i) const {
1313 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1315 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1316 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1318 unsigned getOperandNumForIncomingBlock(unsigned i) {
1322 /// addIncoming - Add an incoming value to the end of the PHI list
1324 void addIncoming(Value *V, BasicBlock *BB) {
1325 assert(V && "PHI node got a null value!");
1326 assert(BB && "PHI node got a null basic block!");
1327 assert(getType() == V->getType() &&
1328 "All operands to PHI node must be the same type as the PHI node!");
1329 unsigned OpNo = NumOperands;
1330 if (OpNo+2 > ReservedSpace)
1331 resizeOperands(0); // Get more space!
1332 // Initialize some new operands.
1333 NumOperands = OpNo+2;
1334 OperandList[OpNo].init(V, this);
1335 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1338 /// removeIncomingValue - Remove an incoming value. This is useful if a
1339 /// predecessor basic block is deleted. The value removed is returned.
1341 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1342 /// is true), the PHI node is destroyed and any uses of it are replaced with
1343 /// dummy values. The only time there should be zero incoming values to a PHI
1344 /// node is when the block is dead, so this strategy is sound.
1346 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1348 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1349 int Idx = getBasicBlockIndex(BB);
1350 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1351 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1354 /// getBasicBlockIndex - Return the first index of the specified basic
1355 /// block in the value list for this PHI. Returns -1 if no instance.
1357 int getBasicBlockIndex(const BasicBlock *BB) const {
1358 Use *OL = OperandList;
1359 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1360 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1364 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1365 return getIncomingValue(getBasicBlockIndex(BB));
1368 /// hasConstantValue - If the specified PHI node always merges together the
1369 /// same value, return the value, otherwise return null.
1371 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1373 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1374 static inline bool classof(const PHINode *) { return true; }
1375 static inline bool classof(const Instruction *I) {
1376 return I->getOpcode() == Instruction::PHI;
1378 static inline bool classof(const Value *V) {
1379 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1382 void resizeOperands(unsigned NumOperands);
1385 //===----------------------------------------------------------------------===//
1387 //===----------------------------------------------------------------------===//
1389 //===---------------------------------------------------------------------------
1390 /// ReturnInst - Return a value (possibly void), from a function. Execution
1391 /// does not continue in this function any longer.
1393 class ReturnInst : public TerminatorInst {
1395 ReturnInst(const ReturnInst &RI);
1396 void init(Value * const* retVals, unsigned N);
1399 // ReturnInst constructors:
1400 // ReturnInst() - 'ret void' instruction
1401 // ReturnInst( null) - 'ret void' instruction
1402 // ReturnInst(Value* X) - 'ret X' instruction
1403 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1404 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1405 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1406 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1407 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1408 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1409 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
1411 // NOTE: If the Value* passed is of type void then the constructor behaves as
1412 // if it was passed NULL.
1413 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1414 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1415 ReturnInst(Value * const* retVals, unsigned N);
1416 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1417 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
1418 explicit ReturnInst(BasicBlock *InsertAtEnd);
1419 virtual ~ReturnInst();
1421 virtual ReturnInst *clone() const;
1423 Value *getReturnValue(unsigned n = 0) const {
1426 return getOperand(n);
1429 unsigned getNumSuccessors() const { return 0; }
1431 // Methods for support type inquiry through isa, cast, and dyn_cast:
1432 static inline bool classof(const ReturnInst *) { return true; }
1433 static inline bool classof(const Instruction *I) {
1434 return (I->getOpcode() == Instruction::Ret);
1436 static inline bool classof(const Value *V) {
1437 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1440 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1441 virtual unsigned getNumSuccessorsV() const;
1442 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1445 //===----------------------------------------------------------------------===//
1447 //===----------------------------------------------------------------------===//
1449 //===---------------------------------------------------------------------------
1450 /// BranchInst - Conditional or Unconditional Branch instruction.
1452 class BranchInst : public TerminatorInst {
1453 /// Ops list - Branches are strange. The operands are ordered:
1454 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1455 /// they don't have to check for cond/uncond branchness.
1457 BranchInst(const BranchInst &BI);
1460 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1461 // BranchInst(BB *B) - 'br B'
1462 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1463 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1464 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1465 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1466 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1467 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1468 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1469 Instruction *InsertBefore = 0);
1470 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1471 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1472 BasicBlock *InsertAtEnd);
1474 /// Transparently provide more efficient getOperand methods.
1475 Value *getOperand(unsigned i) const {
1476 assert(i < getNumOperands() && "getOperand() out of range!");
1479 void setOperand(unsigned i, Value *Val) {
1480 assert(i < getNumOperands() && "setOperand() out of range!");
1484 virtual BranchInst *clone() const;
1486 bool isUnconditional() const { return getNumOperands() == 1; }
1487 bool isConditional() const { return getNumOperands() == 3; }
1489 Value *getCondition() const {
1490 assert(isConditional() && "Cannot get condition of an uncond branch!");
1491 return getOperand(2);
1494 void setCondition(Value *V) {
1495 assert(isConditional() && "Cannot set condition of unconditional branch!");
1499 // setUnconditionalDest - Change the current branch to an unconditional branch
1500 // targeting the specified block.
1501 // FIXME: Eliminate this ugly method.
1502 void setUnconditionalDest(BasicBlock *Dest) {
1503 if (isConditional()) { // Convert this to an uncond branch.
1508 setOperand(0, reinterpret_cast<Value*>(Dest));
1511 unsigned getNumSuccessors() const { return 1+isConditional(); }
1513 BasicBlock *getSuccessor(unsigned i) const {
1514 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1515 return cast<BasicBlock>(getOperand(i));
1518 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1519 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1520 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1523 // Methods for support type inquiry through isa, cast, and dyn_cast:
1524 static inline bool classof(const BranchInst *) { return true; }
1525 static inline bool classof(const Instruction *I) {
1526 return (I->getOpcode() == Instruction::Br);
1528 static inline bool classof(const Value *V) {
1529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1532 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1533 virtual unsigned getNumSuccessorsV() const;
1534 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1537 //===----------------------------------------------------------------------===//
1539 //===----------------------------------------------------------------------===//
1541 //===---------------------------------------------------------------------------
1542 /// SwitchInst - Multiway switch
1544 class SwitchInst : public TerminatorInst {
1545 unsigned ReservedSpace;
1546 // Operand[0] = Value to switch on
1547 // Operand[1] = Default basic block destination
1548 // Operand[2n ] = Value to match
1549 // Operand[2n+1] = BasicBlock to go to on match
1550 SwitchInst(const SwitchInst &RI);
1551 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1552 void resizeOperands(unsigned No);
1554 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1555 /// switch on and a default destination. The number of additional cases can
1556 /// be specified here to make memory allocation more efficient. This
1557 /// constructor can also autoinsert before another instruction.
1558 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1559 Instruction *InsertBefore = 0);
1561 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1562 /// switch on and a default destination. The number of additional cases can
1563 /// be specified here to make memory allocation more efficient. This
1564 /// constructor also autoinserts at the end of the specified BasicBlock.
1565 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1566 BasicBlock *InsertAtEnd);
1570 // Accessor Methods for Switch stmt
1571 Value *getCondition() const { return getOperand(0); }
1572 void setCondition(Value *V) { setOperand(0, V); }
1574 BasicBlock *getDefaultDest() const {
1575 return cast<BasicBlock>(getOperand(1));
1578 /// getNumCases - return the number of 'cases' in this switch instruction.
1579 /// Note that case #0 is always the default case.
1580 unsigned getNumCases() const {
1581 return getNumOperands()/2;
1584 /// getCaseValue - Return the specified case value. Note that case #0, the
1585 /// default destination, does not have a case value.
1586 ConstantInt *getCaseValue(unsigned i) {
1587 assert(i && i < getNumCases() && "Illegal case value to get!");
1588 return getSuccessorValue(i);
1591 /// getCaseValue - Return the specified case value. Note that case #0, the
1592 /// default destination, does not have a case value.
1593 const ConstantInt *getCaseValue(unsigned i) const {
1594 assert(i && i < getNumCases() && "Illegal case value to get!");
1595 return getSuccessorValue(i);
1598 /// findCaseValue - Search all of the case values for the specified constant.
1599 /// If it is explicitly handled, return the case number of it, otherwise
1600 /// return 0 to indicate that it is handled by the default handler.
1601 unsigned findCaseValue(const ConstantInt *C) const {
1602 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1603 if (getCaseValue(i) == C)
1608 /// findCaseDest - Finds the unique case value for a given successor. Returns
1609 /// null if the successor is not found, not unique, or is the default case.
1610 ConstantInt *findCaseDest(BasicBlock *BB) {
1611 if (BB == getDefaultDest()) return NULL;
1613 ConstantInt *CI = NULL;
1614 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1615 if (getSuccessor(i) == BB) {
1616 if (CI) return NULL; // Multiple cases lead to BB.
1617 else CI = getCaseValue(i);
1623 /// addCase - Add an entry to the switch instruction...
1625 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1627 /// removeCase - This method removes the specified successor from the switch
1628 /// instruction. Note that this cannot be used to remove the default
1629 /// destination (successor #0).
1631 void removeCase(unsigned idx);
1633 virtual SwitchInst *clone() const;
1635 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1636 BasicBlock *getSuccessor(unsigned idx) const {
1637 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1638 return cast<BasicBlock>(getOperand(idx*2+1));
1640 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1641 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1642 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1645 // getSuccessorValue - Return the value associated with the specified
1647 ConstantInt *getSuccessorValue(unsigned idx) const {
1648 assert(idx < getNumSuccessors() && "Successor # out of range!");
1649 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1652 // Methods for support type inquiry through isa, cast, and dyn_cast:
1653 static inline bool classof(const SwitchInst *) { return true; }
1654 static inline bool classof(const Instruction *I) {
1655 return I->getOpcode() == Instruction::Switch;
1657 static inline bool classof(const Value *V) {
1658 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1661 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1662 virtual unsigned getNumSuccessorsV() const;
1663 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1666 //===----------------------------------------------------------------------===//
1668 //===----------------------------------------------------------------------===//
1670 //===---------------------------------------------------------------------------
1672 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1673 /// calling convention of the call.
1675 class InvokeInst : public TerminatorInst {
1676 const ParamAttrsList *ParamAttrs;
1677 InvokeInst(const InvokeInst &BI);
1678 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1679 Value* const *Args, unsigned NumArgs);
1681 template<typename InputIterator>
1682 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1683 InputIterator ArgBegin, InputIterator ArgEnd,
1684 const std::string &Name,
1685 // This argument ensures that we have an iterator we can
1686 // do arithmetic on in constant time
1687 std::random_access_iterator_tag) {
1688 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
1690 // This requires that the iterator points to contiguous memory.
1691 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
1696 /// Construct an InvokeInst given a range of arguments.
1697 /// InputIterator must be a random-access iterator pointing to
1698 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1699 /// made for random-accessness but not for contiguous storage as
1700 /// that would incur runtime overhead.
1702 /// @brief Construct an InvokeInst from a range of arguments
1703 template<typename InputIterator>
1704 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1705 InputIterator ArgBegin, InputIterator ArgEnd,
1706 const std::string &Name = "", Instruction *InsertBefore = 0)
1707 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1708 ->getElementType())->getReturnType(),
1709 Instruction::Invoke, 0, 0, InsertBefore) {
1710 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1711 typename std::iterator_traits<InputIterator>::iterator_category());
1714 /// Construct an InvokeInst given a range of arguments.
1715 /// InputIterator must be a random-access iterator pointing to
1716 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1717 /// made for random-accessness but not for contiguous storage as
1718 /// that would incur runtime overhead.
1720 /// @brief Construct an InvokeInst from a range of arguments
1721 template<typename InputIterator>
1722 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1723 InputIterator ArgBegin, InputIterator ArgEnd,
1724 const std::string &Name, BasicBlock *InsertAtEnd)
1725 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1726 ->getElementType())->getReturnType(),
1727 Instruction::Invoke, 0, 0, InsertAtEnd) {
1728 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1729 typename std::iterator_traits<InputIterator>::iterator_category());
1734 virtual InvokeInst *clone() const;
1736 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1738 unsigned getCallingConv() const { return SubclassData; }
1739 void setCallingConv(unsigned CC) {
1743 /// Obtains a pointer to the ParamAttrsList object which holds the
1744 /// parameter attributes information, if any.
1745 /// @returns 0 if no attributes have been set.
1746 /// @brief Get the parameter attributes.
1747 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
1749 /// Sets the parameter attributes for this InvokeInst. To construct a
1750 /// ParamAttrsList, see ParameterAttributes.h
1751 /// @brief Set the parameter attributes.
1752 void setParamAttrs(const ParamAttrsList *attrs);
1754 /// @brief Determine whether the call or the callee has the given attribute.
1755 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
1757 /// @brief Extract the alignment for a call or parameter (0=unknown).
1758 uint16_t getParamAlignment(uint16_t i) const;
1760 /// @brief Determine if the call does not access memory.
1761 bool doesNotAccessMemory() const;
1763 /// @brief Determine if the call does not access or only reads memory.
1764 bool onlyReadsMemory() const;
1766 /// @brief Determine if the call cannot return.
1767 bool doesNotReturn() const;
1769 /// @brief Determine if the call cannot unwind.
1770 bool doesNotThrow() const;
1771 void setDoesNotThrow(bool doesNotThrow = true);
1773 /// @brief Determine if the call returns a structure through first
1774 /// pointer argument.
1775 bool hasStructRetAttr() const;
1777 /// getCalledFunction - Return the function called, or null if this is an
1778 /// indirect function invocation.
1780 Function *getCalledFunction() const {
1781 return dyn_cast<Function>(getOperand(0));
1784 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1785 Value *getCalledValue() const { return getOperand(0); }
1787 // get*Dest - Return the destination basic blocks...
1788 BasicBlock *getNormalDest() const {
1789 return cast<BasicBlock>(getOperand(1));
1791 BasicBlock *getUnwindDest() const {
1792 return cast<BasicBlock>(getOperand(2));
1794 void setNormalDest(BasicBlock *B) {
1795 setOperand(1, reinterpret_cast<Value*>(B));
1798 void setUnwindDest(BasicBlock *B) {
1799 setOperand(2, reinterpret_cast<Value*>(B));
1802 BasicBlock *getSuccessor(unsigned i) const {
1803 assert(i < 2 && "Successor # out of range for invoke!");
1804 return i == 0 ? getNormalDest() : getUnwindDest();
1807 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1808 assert(idx < 2 && "Successor # out of range for invoke!");
1809 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1812 unsigned getNumSuccessors() const { return 2; }
1814 // Methods for support type inquiry through isa, cast, and dyn_cast:
1815 static inline bool classof(const InvokeInst *) { return true; }
1816 static inline bool classof(const Instruction *I) {
1817 return (I->getOpcode() == Instruction::Invoke);
1819 static inline bool classof(const Value *V) {
1820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1823 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1824 virtual unsigned getNumSuccessorsV() const;
1825 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1829 //===----------------------------------------------------------------------===//
1831 //===----------------------------------------------------------------------===//
1833 //===---------------------------------------------------------------------------
1834 /// UnwindInst - Immediately exit the current function, unwinding the stack
1835 /// until an invoke instruction is found.
1837 class UnwindInst : public TerminatorInst {
1839 explicit UnwindInst(Instruction *InsertBefore = 0);
1840 explicit UnwindInst(BasicBlock *InsertAtEnd);
1842 virtual UnwindInst *clone() const;
1844 unsigned getNumSuccessors() const { return 0; }
1846 // Methods for support type inquiry through isa, cast, and dyn_cast:
1847 static inline bool classof(const UnwindInst *) { return true; }
1848 static inline bool classof(const Instruction *I) {
1849 return I->getOpcode() == Instruction::Unwind;
1851 static inline bool classof(const Value *V) {
1852 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1855 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1856 virtual unsigned getNumSuccessorsV() const;
1857 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1860 //===----------------------------------------------------------------------===//
1861 // UnreachableInst Class
1862 //===----------------------------------------------------------------------===//
1864 //===---------------------------------------------------------------------------
1865 /// UnreachableInst - This function has undefined behavior. In particular, the
1866 /// presence of this instruction indicates some higher level knowledge that the
1867 /// end of the block cannot be reached.
1869 class UnreachableInst : public TerminatorInst {
1871 explicit UnreachableInst(Instruction *InsertBefore = 0);
1872 explicit UnreachableInst(BasicBlock *InsertAtEnd);
1874 virtual UnreachableInst *clone() const;
1876 unsigned getNumSuccessors() const { return 0; }
1878 // Methods for support type inquiry through isa, cast, and dyn_cast:
1879 static inline bool classof(const UnreachableInst *) { return true; }
1880 static inline bool classof(const Instruction *I) {
1881 return I->getOpcode() == Instruction::Unreachable;
1883 static inline bool classof(const Value *V) {
1884 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1887 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1888 virtual unsigned getNumSuccessorsV() const;
1889 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1892 //===----------------------------------------------------------------------===//
1894 //===----------------------------------------------------------------------===//
1896 /// @brief This class represents a truncation of integer types.
1897 class TruncInst : public CastInst {
1898 /// Private copy constructor
1899 TruncInst(const TruncInst &CI)
1900 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1903 /// @brief Constructor with insert-before-instruction semantics
1905 Value *S, ///< The value to be truncated
1906 const Type *Ty, ///< The (smaller) type to truncate to
1907 const std::string &Name = "", ///< A name for the new instruction
1908 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1911 /// @brief Constructor with insert-at-end-of-block semantics
1913 Value *S, ///< The value to be truncated
1914 const Type *Ty, ///< The (smaller) type to truncate to
1915 const std::string &Name, ///< A name for the new instruction
1916 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1919 /// @brief Clone an identical TruncInst
1920 virtual CastInst *clone() const;
1922 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1923 static inline bool classof(const TruncInst *) { return true; }
1924 static inline bool classof(const Instruction *I) {
1925 return I->getOpcode() == Trunc;
1927 static inline bool classof(const Value *V) {
1928 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1932 //===----------------------------------------------------------------------===//
1934 //===----------------------------------------------------------------------===//
1936 /// @brief This class represents zero extension of integer types.
1937 class ZExtInst : public CastInst {
1938 /// @brief Private copy constructor
1939 ZExtInst(const ZExtInst &CI)
1940 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1943 /// @brief Constructor with insert-before-instruction semantics
1945 Value *S, ///< The value to be zero extended
1946 const Type *Ty, ///< The type to zero extend to
1947 const std::string &Name = "", ///< A name for the new instruction
1948 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1951 /// @brief Constructor with insert-at-end semantics.
1953 Value *S, ///< The value to be zero extended
1954 const Type *Ty, ///< The type to zero extend to
1955 const std::string &Name, ///< A name for the new instruction
1956 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1959 /// @brief Clone an identical ZExtInst
1960 virtual CastInst *clone() const;
1962 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1963 static inline bool classof(const ZExtInst *) { return true; }
1964 static inline bool classof(const Instruction *I) {
1965 return I->getOpcode() == ZExt;
1967 static inline bool classof(const Value *V) {
1968 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1972 //===----------------------------------------------------------------------===//
1974 //===----------------------------------------------------------------------===//
1976 /// @brief This class represents a sign extension of integer types.
1977 class SExtInst : public CastInst {
1978 /// @brief Private copy constructor
1979 SExtInst(const SExtInst &CI)
1980 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1983 /// @brief Constructor with insert-before-instruction semantics
1985 Value *S, ///< The value to be sign extended
1986 const Type *Ty, ///< The type to sign extend to
1987 const std::string &Name = "", ///< A name for the new instruction
1988 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1991 /// @brief Constructor with insert-at-end-of-block semantics
1993 Value *S, ///< The value to be sign extended
1994 const Type *Ty, ///< The type to sign extend to
1995 const std::string &Name, ///< A name for the new instruction
1996 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1999 /// @brief Clone an identical SExtInst
2000 virtual CastInst *clone() const;
2002 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2003 static inline bool classof(const SExtInst *) { return true; }
2004 static inline bool classof(const Instruction *I) {
2005 return I->getOpcode() == SExt;
2007 static inline bool classof(const Value *V) {
2008 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2012 //===----------------------------------------------------------------------===//
2013 // FPTruncInst Class
2014 //===----------------------------------------------------------------------===//
2016 /// @brief This class represents a truncation of floating point types.
2017 class FPTruncInst : public CastInst {
2018 FPTruncInst(const FPTruncInst &CI)
2019 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2022 /// @brief Constructor with insert-before-instruction semantics
2024 Value *S, ///< The value to be truncated
2025 const Type *Ty, ///< The type to truncate to
2026 const std::string &Name = "", ///< A name for the new instruction
2027 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2030 /// @brief Constructor with insert-before-instruction semantics
2032 Value *S, ///< The value to be truncated
2033 const Type *Ty, ///< The type to truncate to
2034 const std::string &Name, ///< A name for the new instruction
2035 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2038 /// @brief Clone an identical FPTruncInst
2039 virtual CastInst *clone() const;
2041 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2042 static inline bool classof(const FPTruncInst *) { return true; }
2043 static inline bool classof(const Instruction *I) {
2044 return I->getOpcode() == FPTrunc;
2046 static inline bool classof(const Value *V) {
2047 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2051 //===----------------------------------------------------------------------===//
2053 //===----------------------------------------------------------------------===//
2055 /// @brief This class represents an extension of floating point types.
2056 class FPExtInst : public CastInst {
2057 FPExtInst(const FPExtInst &CI)
2058 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2061 /// @brief Constructor with insert-before-instruction semantics
2063 Value *S, ///< The value to be extended
2064 const Type *Ty, ///< The type to extend to
2065 const std::string &Name = "", ///< A name for the new instruction
2066 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2069 /// @brief Constructor with insert-at-end-of-block semantics
2071 Value *S, ///< The value to be extended
2072 const Type *Ty, ///< The type to extend to
2073 const std::string &Name, ///< A name for the new instruction
2074 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2077 /// @brief Clone an identical FPExtInst
2078 virtual CastInst *clone() const;
2080 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2081 static inline bool classof(const FPExtInst *) { return true; }
2082 static inline bool classof(const Instruction *I) {
2083 return I->getOpcode() == FPExt;
2085 static inline bool classof(const Value *V) {
2086 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2090 //===----------------------------------------------------------------------===//
2092 //===----------------------------------------------------------------------===//
2094 /// @brief This class represents a cast unsigned integer to floating point.
2095 class UIToFPInst : public CastInst {
2096 UIToFPInst(const UIToFPInst &CI)
2097 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2100 /// @brief Constructor with insert-before-instruction semantics
2102 Value *S, ///< The value to be converted
2103 const Type *Ty, ///< The type to convert to
2104 const std::string &Name = "", ///< A name for the new instruction
2105 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2108 /// @brief Constructor with insert-at-end-of-block semantics
2110 Value *S, ///< The value to be converted
2111 const Type *Ty, ///< The type to convert to
2112 const std::string &Name, ///< A name for the new instruction
2113 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2116 /// @brief Clone an identical UIToFPInst
2117 virtual CastInst *clone() const;
2119 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2120 static inline bool classof(const UIToFPInst *) { return true; }
2121 static inline bool classof(const Instruction *I) {
2122 return I->getOpcode() == UIToFP;
2124 static inline bool classof(const Value *V) {
2125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2129 //===----------------------------------------------------------------------===//
2131 //===----------------------------------------------------------------------===//
2133 /// @brief This class represents a cast from signed integer to floating point.
2134 class SIToFPInst : public CastInst {
2135 SIToFPInst(const SIToFPInst &CI)
2136 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2139 /// @brief Constructor with insert-before-instruction semantics
2141 Value *S, ///< The value to be converted
2142 const Type *Ty, ///< The type to convert to
2143 const std::string &Name = "", ///< A name for the new instruction
2144 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2147 /// @brief Constructor with insert-at-end-of-block semantics
2149 Value *S, ///< The value to be converted
2150 const Type *Ty, ///< The type to convert to
2151 const std::string &Name, ///< A name for the new instruction
2152 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2155 /// @brief Clone an identical SIToFPInst
2156 virtual CastInst *clone() const;
2158 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2159 static inline bool classof(const SIToFPInst *) { return true; }
2160 static inline bool classof(const Instruction *I) {
2161 return I->getOpcode() == SIToFP;
2163 static inline bool classof(const Value *V) {
2164 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2168 //===----------------------------------------------------------------------===//
2170 //===----------------------------------------------------------------------===//
2172 /// @brief This class represents a cast from floating point to unsigned integer
2173 class FPToUIInst : public CastInst {
2174 FPToUIInst(const FPToUIInst &CI)
2175 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2178 /// @brief Constructor with insert-before-instruction semantics
2180 Value *S, ///< The value to be converted
2181 const Type *Ty, ///< The type to convert to
2182 const std::string &Name = "", ///< A name for the new instruction
2183 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2186 /// @brief Constructor with insert-at-end-of-block semantics
2188 Value *S, ///< The value to be converted
2189 const Type *Ty, ///< The type to convert to
2190 const std::string &Name, ///< A name for the new instruction
2191 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2194 /// @brief Clone an identical FPToUIInst
2195 virtual CastInst *clone() const;
2197 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2198 static inline bool classof(const FPToUIInst *) { return true; }
2199 static inline bool classof(const Instruction *I) {
2200 return I->getOpcode() == FPToUI;
2202 static inline bool classof(const Value *V) {
2203 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2207 //===----------------------------------------------------------------------===//
2209 //===----------------------------------------------------------------------===//
2211 /// @brief This class represents a cast from floating point to signed integer.
2212 class FPToSIInst : public CastInst {
2213 FPToSIInst(const FPToSIInst &CI)
2214 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2217 /// @brief Constructor with insert-before-instruction semantics
2219 Value *S, ///< The value to be converted
2220 const Type *Ty, ///< The type to convert to
2221 const std::string &Name = "", ///< A name for the new instruction
2222 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2225 /// @brief Constructor with insert-at-end-of-block semantics
2227 Value *S, ///< The value to be converted
2228 const Type *Ty, ///< The type to convert to
2229 const std::string &Name, ///< A name for the new instruction
2230 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2233 /// @brief Clone an identical FPToSIInst
2234 virtual CastInst *clone() const;
2236 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2237 static inline bool classof(const FPToSIInst *) { return true; }
2238 static inline bool classof(const Instruction *I) {
2239 return I->getOpcode() == FPToSI;
2241 static inline bool classof(const Value *V) {
2242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2246 //===----------------------------------------------------------------------===//
2247 // IntToPtrInst Class
2248 //===----------------------------------------------------------------------===//
2250 /// @brief This class represents a cast from an integer to a pointer.
2251 class IntToPtrInst : public CastInst {
2252 IntToPtrInst(const IntToPtrInst &CI)
2253 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2256 /// @brief Constructor with insert-before-instruction semantics
2258 Value *S, ///< The value to be converted
2259 const Type *Ty, ///< The type to convert to
2260 const std::string &Name = "", ///< A name for the new instruction
2261 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2264 /// @brief Constructor with insert-at-end-of-block semantics
2266 Value *S, ///< The value to be converted
2267 const Type *Ty, ///< The type to convert to
2268 const std::string &Name, ///< A name for the new instruction
2269 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2272 /// @brief Clone an identical IntToPtrInst
2273 virtual CastInst *clone() const;
2275 // Methods for support type inquiry through isa, cast, and dyn_cast:
2276 static inline bool classof(const IntToPtrInst *) { return true; }
2277 static inline bool classof(const Instruction *I) {
2278 return I->getOpcode() == IntToPtr;
2280 static inline bool classof(const Value *V) {
2281 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2285 //===----------------------------------------------------------------------===//
2286 // PtrToIntInst Class
2287 //===----------------------------------------------------------------------===//
2289 /// @brief This class represents a cast from a pointer to an integer
2290 class PtrToIntInst : public CastInst {
2291 PtrToIntInst(const PtrToIntInst &CI)
2292 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2295 /// @brief Constructor with insert-before-instruction semantics
2297 Value *S, ///< The value to be converted
2298 const Type *Ty, ///< The type to convert to
2299 const std::string &Name = "", ///< A name for the new instruction
2300 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2303 /// @brief Constructor with insert-at-end-of-block semantics
2305 Value *S, ///< The value to be converted
2306 const Type *Ty, ///< The type to convert to
2307 const std::string &Name, ///< A name for the new instruction
2308 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2311 /// @brief Clone an identical PtrToIntInst
2312 virtual CastInst *clone() const;
2314 // Methods for support type inquiry through isa, cast, and dyn_cast:
2315 static inline bool classof(const PtrToIntInst *) { return true; }
2316 static inline bool classof(const Instruction *I) {
2317 return I->getOpcode() == PtrToInt;
2319 static inline bool classof(const Value *V) {
2320 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2324 //===----------------------------------------------------------------------===//
2325 // BitCastInst Class
2326 //===----------------------------------------------------------------------===//
2328 /// @brief This class represents a no-op cast from one type to another.
2329 class BitCastInst : public CastInst {
2330 BitCastInst(const BitCastInst &CI)
2331 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2334 /// @brief Constructor with insert-before-instruction semantics
2336 Value *S, ///< The value to be casted
2337 const Type *Ty, ///< The type to casted to
2338 const std::string &Name = "", ///< A name for the new instruction
2339 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2342 /// @brief Constructor with insert-at-end-of-block semantics
2344 Value *S, ///< The value to be casted
2345 const Type *Ty, ///< The type to casted to
2346 const std::string &Name, ///< A name for the new instruction
2347 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2350 /// @brief Clone an identical BitCastInst
2351 virtual CastInst *clone() const;
2353 // Methods for support type inquiry through isa, cast, and dyn_cast:
2354 static inline bool classof(const BitCastInst *) { return true; }
2355 static inline bool classof(const Instruction *I) {
2356 return I->getOpcode() == BitCast;
2358 static inline bool classof(const Value *V) {
2359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2363 //===----------------------------------------------------------------------===//
2364 // GetResultInst Class
2365 //===----------------------------------------------------------------------===//
2367 /// GetResultInst - This instruction extracts individual result value from
2368 /// aggregate value, where aggregate value is returned by CallInst.
2370 class GetResultInst : public Instruction {
2373 GetResultInst(const GetResultInst &GRI) :
2374 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2375 Aggr.init(GRI.Aggr, this);
2380 explicit GetResultInst(Value *Aggr, unsigned index,
2381 const std::string &Name = "",
2382 Instruction *InsertBefore = 0);
2384 /// isValidOperands - Return true if an getresult instruction can be
2385 /// formed with the specified operands.
2386 static bool isValidOperands(const Value *Aggr, unsigned index);
2388 virtual GetResultInst *clone() const;
2390 Value *getAggregateValue() {
2391 return getOperand(0);
2394 const Value *getAggregateValue() const {
2395 return getOperand(0);
2398 unsigned getIndex() const {
2402 unsigned getNumOperands() const { return 1; }
2404 // Methods for support type inquiry through isa, cast, and dyn_cast:
2405 static inline bool classof(const GetResultInst *) { return true; }
2406 static inline bool classof(const Instruction *I) {
2407 return (I->getOpcode() == Instruction::GetResult);
2409 static inline bool classof(const Value *V) {
2410 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2414 } // End llvm namespace