1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 /// isArrayAllocation - Return true if there is an allocation size parameter
51 /// to the allocation instruction that is not 1.
53 bool isArrayAllocation() const;
55 /// getArraySize - Get the number of element allocated, for a simple
56 /// allocation of a single element, this will return a constant 1 value.
58 inline const Value *getArraySize() const { return getOperand(0); }
59 inline Value *getArraySize() { return getOperand(0); }
61 /// getType - Overload to return most specific pointer type
63 inline const PointerType *getType() const {
64 return reinterpret_cast<const PointerType*>(Instruction::getType());
67 /// getAllocatedType - Return the type that is being allocated by the
70 const Type *getAllocatedType() const;
72 /// getAlignment - Return the alignment of the memory that is being allocated
73 /// by the instruction.
75 unsigned getAlignment() const { return Alignment; }
76 void setAlignment(unsigned Align) {
77 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
81 virtual Instruction *clone() const = 0;
83 // Methods for support type inquiry through isa, cast, and dyn_cast:
84 static inline bool classof(const AllocationInst *) { return true; }
85 static inline bool classof(const Instruction *I) {
86 return I->getOpcode() == Instruction::Alloca ||
87 I->getOpcode() == Instruction::Malloc;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// MallocInst - an instruction to allocated memory on the heap
101 class MallocInst : public AllocationInst {
102 MallocInst(const MallocInst &MI);
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const std::string &Name = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
108 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
109 BasicBlock *InsertAtEnd)
110 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
112 MallocInst(const Type *Ty, const std::string &Name,
113 Instruction *InsertBefore = 0)
114 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
115 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
116 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
118 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
119 const std::string &Name, BasicBlock *InsertAtEnd)
120 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
121 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
122 const std::string &Name = "",
123 Instruction *InsertBefore = 0)
124 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
126 virtual MallocInst *clone() const;
128 // Methods for support type inquiry through isa, cast, and dyn_cast:
129 static inline bool classof(const MallocInst *) { return true; }
130 static inline bool classof(const Instruction *I) {
131 return (I->getOpcode() == Instruction::Malloc);
133 static inline bool classof(const Value *V) {
134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
139 //===----------------------------------------------------------------------===//
141 //===----------------------------------------------------------------------===//
143 /// AllocaInst - an instruction to allocate memory on the stack
145 class AllocaInst : public AllocationInst {
146 AllocaInst(const AllocaInst &);
148 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
149 const std::string &Name = "",
150 Instruction *InsertBefore = 0)
151 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
152 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
153 BasicBlock *InsertAtEnd)
154 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
156 AllocaInst(const Type *Ty, const std::string &Name,
157 Instruction *InsertBefore = 0)
158 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
159 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
160 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163 const std::string &Name = "", Instruction *InsertBefore = 0)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
166 const std::string &Name, BasicBlock *InsertAtEnd)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
169 virtual AllocaInst *clone() const;
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const AllocaInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return (I->getOpcode() == Instruction::Alloca);
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
182 //===----------------------------------------------------------------------===//
184 //===----------------------------------------------------------------------===//
186 /// FreeInst - an instruction to deallocate memory
188 class FreeInst : public UnaryInstruction {
191 static void destroyThis(FreeInst* v) {
192 UnaryInstruction::destroyThis(v);
196 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
197 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
199 virtual FreeInst *clone() const;
201 // Accessor methods for consistency with other memory operations
202 Value *getPointerOperand() { return getOperand(0); }
203 const Value *getPointerOperand() const { return getOperand(0); }
205 // Methods for support type inquiry through isa, cast, and dyn_cast:
206 static inline bool classof(const FreeInst *) { return true; }
207 static inline bool classof(const Instruction *I) {
208 return (I->getOpcode() == Instruction::Free);
210 static inline bool classof(const Value *V) {
211 return isa<Instruction>(V) && classof(cast<Instruction>(V));
216 //===----------------------------------------------------------------------===//
218 //===----------------------------------------------------------------------===//
220 /// LoadInst - an instruction for reading from memory. This uses the
221 /// SubclassData field in Value to store whether or not the load is volatile.
223 class LoadInst : public UnaryInstruction {
225 LoadInst(const LoadInst &LI)
226 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
227 setVolatile(LI.isVolatile());
228 setAlignment(LI.getAlignment());
236 static void destroyThis(LoadInst* v) {
237 UnaryInstruction::destroyThis(v);
241 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
242 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
243 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
244 Instruction *InsertBefore = 0);
245 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
246 Instruction *InsertBefore = 0);
247 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
248 BasicBlock *InsertAtEnd);
249 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
250 BasicBlock *InsertAtEnd);
252 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
253 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
254 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
255 Instruction *InsertBefore = 0);
256 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
257 BasicBlock *InsertAtEnd);
259 /// isVolatile - Return true if this is a load from a volatile memory
262 bool isVolatile() const { return SubclassData & 1; }
264 /// setVolatile - Specify whether this is a volatile load or not.
266 void setVolatile(bool V) {
267 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
270 virtual LoadInst *clone() const;
272 /// getAlignment - Return the alignment of the access that is being performed
274 unsigned getAlignment() const {
275 return (1 << (SubclassData>>1)) >> 1;
278 void setAlignment(unsigned Align);
280 Value *getPointerOperand() { return getOperand(0); }
281 const Value *getPointerOperand() const { return getOperand(0); }
282 static unsigned getPointerOperandIndex() { return 0U; }
284 // Methods for support type inquiry through isa, cast, and dyn_cast:
285 static inline bool classof(const LoadInst *) { return true; }
286 static inline bool classof(const Instruction *I) {
287 return I->getOpcode() == Instruction::Load;
289 static inline bool classof(const Value *V) {
290 return isa<Instruction>(V) && classof(cast<Instruction>(V));
295 //===----------------------------------------------------------------------===//
297 //===----------------------------------------------------------------------===//
299 /// StoreInst - an instruction for storing to memory
301 class StoreInst : public Instruction {
304 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
305 Ops[0].init(SI.Ops[0], this);
306 Ops[1].init(SI.Ops[1], this);
307 setVolatile(SI.isVolatile());
308 setAlignment(SI.getAlignment());
316 static void destroyThis(StoreInst* v) {
317 Instruction::destroyThis(v);
321 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
322 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
323 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
324 Instruction *InsertBefore = 0);
325 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
326 unsigned Align, Instruction *InsertBefore = 0);
327 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
328 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
329 unsigned Align, BasicBlock *InsertAtEnd);
332 /// isVolatile - Return true if this is a load from a volatile memory
335 bool isVolatile() const { return SubclassData & 1; }
337 /// setVolatile - Specify whether this is a volatile load or not.
339 void setVolatile(bool V) {
340 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
343 /// Transparently provide more efficient getOperand methods.
344 Value *getOperand(unsigned i) const {
345 assert(i < 2 && "getOperand() out of range!");
348 void setOperand(unsigned i, Value *Val) {
349 assert(i < 2 && "setOperand() out of range!");
352 unsigned getNumOperands() const { return 2; }
354 /// getAlignment - Return the alignment of the access that is being performed
356 unsigned getAlignment() const {
357 return (1 << (SubclassData>>1)) >> 1;
360 void setAlignment(unsigned Align);
362 virtual StoreInst *clone() const;
364 Value *getPointerOperand() { return getOperand(1); }
365 const Value *getPointerOperand() const { return getOperand(1); }
366 static unsigned getPointerOperandIndex() { return 1U; }
368 // Methods for support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const StoreInst *) { return true; }
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
379 //===----------------------------------------------------------------------===//
380 // GetElementPtrInst Class
381 //===----------------------------------------------------------------------===//
383 // checkType - Simple wrapper function to give a better assertion failure
384 // message on bad indexes for a gep instruction.
386 static inline const Type *checkType(const Type *Ty) {
387 assert(Ty && "Invalid GetElementPtrInst indices for type!");
391 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
392 /// access elements of arrays and structs
394 class GetElementPtrInst : public Instruction {
395 GetElementPtrInst(const GetElementPtrInst &GEPI)
396 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
397 0, GEPI.getNumOperands()) {
398 Use *OL = OperandList = new Use[NumOperands];
399 Use *GEPIOL = GEPI.OperandList;
400 for (unsigned i = 0, E = NumOperands; i != E; ++i)
401 OL[i].init(GEPIOL[i], this);
403 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
404 void init(Value *Ptr, Value *Idx);
406 template<typename InputIterator>
407 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
408 const std::string &Name,
409 // This argument ensures that we have an iterator we can
410 // do arithmetic on in constant time
411 std::random_access_iterator_tag) {
412 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
413 std::distance(IdxBegin, IdxEnd);
416 // This requires that the itoerator points to contiguous memory.
417 init(Ptr, &*IdxBegin, NumIdx);
420 init(Ptr, 0, NumIdx);
426 /// getIndexedType - Returns the type of the element that would be loaded with
427 /// a load instruction with the specified parameters.
429 /// A null type is returned if the indices are invalid for the specified
432 static const Type *getIndexedType(const Type *Ptr,
433 Value* const *Idx, unsigned NumIdx,
434 bool AllowStructLeaf = false);
436 template<typename InputIterator>
437 static const Type *getIndexedType(const Type *Ptr,
438 InputIterator IdxBegin,
439 InputIterator IdxEnd,
440 bool AllowStructLeaf,
441 // This argument ensures that we
442 // have an iterator we can do
443 // arithmetic on in constant time
444 std::random_access_iterator_tag) {
445 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
446 std::distance(IdxBegin, IdxEnd);
449 // This requires that the iterator points to contiguous memory.
450 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
454 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
459 static void destroyThis(GetElementPtrInst*v);
462 /// Constructors - Create a getelementptr instruction with a base pointer an
463 /// list of indices. The first ctor can optionally insert before an existing
464 /// instruction, the second appends the new instruction to the specified
466 template<typename InputIterator>
467 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
468 InputIterator IdxEnd,
469 const std::string &Name = "",
470 Instruction *InsertBefore =0)
471 : Instruction(PointerType::get(
472 checkType(getIndexedType(Ptr->getType(),
473 IdxBegin, IdxEnd, true))),
474 GetElementPtr, 0, 0, InsertBefore) {
475 init(Ptr, IdxBegin, IdxEnd, Name,
476 typename std::iterator_traits<InputIterator>::iterator_category());
478 template<typename InputIterator>
479 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
480 const std::string &Name, BasicBlock *InsertAtEnd)
481 : Instruction(PointerType::get(
482 checkType(getIndexedType(Ptr->getType(),
483 IdxBegin, IdxEnd, true))),
484 GetElementPtr, 0, 0, InsertAtEnd) {
485 init(Ptr, IdxBegin, IdxEnd, Name,
486 typename std::iterator_traits<InputIterator>::iterator_category());
489 /// Constructors - These two constructors are convenience methods because one
490 /// and two index getelementptr instructions are so common.
491 GetElementPtrInst(Value *Ptr, Value *Idx,
492 const std::string &Name = "", Instruction *InsertBefore =0);
493 GetElementPtrInst(Value *Ptr, Value *Idx,
494 const std::string &Name, BasicBlock *InsertAtEnd);
496 virtual GetElementPtrInst *clone() const;
498 // getType - Overload to return most specific pointer type...
499 inline const PointerType *getType() const {
500 return reinterpret_cast<const PointerType*>(Instruction::getType());
503 /// getIndexedType - Returns the type of the element that would be loaded with
504 /// a load instruction with the specified parameters.
506 /// A null type is returned if the indices are invalid for the specified
509 template<typename InputIterator>
510 static const Type *getIndexedType(const Type *Ptr,
511 InputIterator IdxBegin,
512 InputIterator IdxEnd,
513 bool AllowStructLeaf = false) {
514 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
515 typename std::iterator_traits<InputIterator>::
516 iterator_category()));
518 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
520 inline op_iterator idx_begin() { return op_begin()+1; }
521 inline const_op_iterator idx_begin() const { return op_begin()+1; }
522 inline op_iterator idx_end() { return op_end(); }
523 inline const_op_iterator idx_end() const { return op_end(); }
525 Value *getPointerOperand() {
526 return getOperand(0);
528 const Value *getPointerOperand() const {
529 return getOperand(0);
531 static unsigned getPointerOperandIndex() {
532 return 0U; // get index for modifying correct operand
535 inline unsigned getNumIndices() const { // Note: always non-negative
536 return getNumOperands() - 1;
539 inline bool hasIndices() const {
540 return getNumOperands() > 1;
543 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
544 /// zeros. If so, the result pointer and the first operand have the same
545 /// value, just potentially different types.
546 bool hasAllZeroIndices() const;
548 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
549 /// constant integers. If so, the result pointer and the first operand have
550 /// a constant offset between them.
551 bool hasAllConstantIndices() const;
554 // Methods for support type inquiry through isa, cast, and dyn_cast:
555 static inline bool classof(const GetElementPtrInst *) { return true; }
556 static inline bool classof(const Instruction *I) {
557 return (I->getOpcode() == Instruction::GetElementPtr);
559 static inline bool classof(const Value *V) {
560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
564 //===----------------------------------------------------------------------===//
566 //===----------------------------------------------------------------------===//
568 /// This instruction compares its operands according to the predicate given
569 /// to the constructor. It only operates on integers, pointers, or packed
570 /// vectors of integrals. The two operands must be the same type.
571 /// @brief Represent an integer comparison operator.
572 class ICmpInst: public CmpInst {
574 static void destroyThis(ICmpInst* v) {
575 CmpInst::destroyThis(v);
579 /// This enumeration lists the possible predicates for the ICmpInst. The
580 /// values in the range 0-31 are reserved for FCmpInst while values in the
581 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
582 /// predicate values are not overlapping between the classes.
584 ICMP_EQ = 32, ///< equal
585 ICMP_NE = 33, ///< not equal
586 ICMP_UGT = 34, ///< unsigned greater than
587 ICMP_UGE = 35, ///< unsigned greater or equal
588 ICMP_ULT = 36, ///< unsigned less than
589 ICMP_ULE = 37, ///< unsigned less or equal
590 ICMP_SGT = 38, ///< signed greater than
591 ICMP_SGE = 39, ///< signed greater or equal
592 ICMP_SLT = 40, ///< signed less than
593 ICMP_SLE = 41, ///< signed less or equal
594 FIRST_ICMP_PREDICATE = ICMP_EQ,
595 LAST_ICMP_PREDICATE = ICMP_SLE,
596 BAD_ICMP_PREDICATE = ICMP_SLE + 1
599 /// @brief Constructor with insert-before-instruction semantics.
601 Predicate pred, ///< The predicate to use for the comparison
602 Value *LHS, ///< The left-hand-side of the expression
603 Value *RHS, ///< The right-hand-side of the expression
604 const std::string &Name = "", ///< Name of the instruction
605 Instruction *InsertBefore = 0 ///< Where to insert
606 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
609 /// @brief Constructor with insert-at-block-end semantics.
611 Predicate pred, ///< The predicate to use for the comparison
612 Value *LHS, ///< The left-hand-side of the expression
613 Value *RHS, ///< The right-hand-side of the expression
614 const std::string &Name, ///< Name of the instruction
615 BasicBlock *InsertAtEnd ///< Block to insert into.
616 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
619 /// @brief Return the predicate for this instruction.
620 Predicate getPredicate() const { return Predicate(SubclassData); }
622 /// @brief Set the predicate for this instruction to the specified value.
623 void setPredicate(Predicate P) { SubclassData = P; }
625 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
626 /// @returns the inverse predicate for the instruction's current predicate.
627 /// @brief Return the inverse of the instruction's predicate.
628 Predicate getInversePredicate() const {
629 return getInversePredicate(getPredicate());
632 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
633 /// @returns the inverse predicate for predicate provided in \p pred.
634 /// @brief Return the inverse of a given predicate
635 static Predicate getInversePredicate(Predicate pred);
637 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
638 /// @returns the predicate that would be the result of exchanging the two
639 /// operands of the ICmpInst instruction without changing the result
641 /// @brief Return the predicate as if the operands were swapped
642 Predicate getSwappedPredicate() const {
643 return getSwappedPredicate(getPredicate());
646 /// This is a static version that you can use without an instruction
648 /// @brief Return the predicate as if the operands were swapped.
649 static Predicate getSwappedPredicate(Predicate pred);
651 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
652 /// @returns the predicate that would be the result if the operand were
653 /// regarded as signed.
654 /// @brief Return the signed version of the predicate
655 Predicate getSignedPredicate() const {
656 return getSignedPredicate(getPredicate());
659 /// This is a static version that you can use without an instruction.
660 /// @brief Return the signed version of the predicate.
661 static Predicate getSignedPredicate(Predicate pred);
663 /// isEquality - Return true if this predicate is either EQ or NE. This also
664 /// tests for commutativity.
665 static bool isEquality(Predicate P) {
666 return P == ICMP_EQ || P == ICMP_NE;
669 /// isEquality - Return true if this predicate is either EQ or NE. This also
670 /// tests for commutativity.
671 bool isEquality() const {
672 return isEquality(getPredicate());
675 /// @returns true if the predicate of this ICmpInst is commutative
676 /// @brief Determine if this relation is commutative.
677 bool isCommutative() const { return isEquality(); }
679 /// isRelational - Return true if the predicate is relational (not EQ or NE).
681 bool isRelational() const {
682 return !isEquality();
685 /// isRelational - Return true if the predicate is relational (not EQ or NE).
687 static bool isRelational(Predicate P) {
688 return !isEquality(P);
691 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
692 /// @brief Determine if this instruction's predicate is signed.
693 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
695 /// @returns true if the predicate provided is signed, false otherwise
696 /// @brief Determine if the predicate is signed.
697 static bool isSignedPredicate(Predicate pred);
699 /// Initialize a set of values that all satisfy the predicate with C.
700 /// @brief Make a ConstantRange for a relation with a constant value.
701 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
703 /// Exchange the two operands to this instruction in such a way that it does
704 /// not modify the semantics of the instruction. The predicate value may be
705 /// changed to retain the same result if the predicate is order dependent
707 /// @brief Swap operands and adjust predicate.
708 void swapOperands() {
709 SubclassData = getSwappedPredicate();
710 std::swap(Ops[0], Ops[1]);
713 virtual ICmpInst *clone() const;
715 // Methods for support type inquiry through isa, cast, and dyn_cast:
716 static inline bool classof(const ICmpInst *) { return true; }
717 static inline bool classof(const Instruction *I) {
718 return I->getOpcode() == Instruction::ICmp;
720 static inline bool classof(const Value *V) {
721 return isa<Instruction>(V) && classof(cast<Instruction>(V));
725 //===----------------------------------------------------------------------===//
727 //===----------------------------------------------------------------------===//
729 /// This instruction compares its operands according to the predicate given
730 /// to the constructor. It only operates on floating point values or packed
731 /// vectors of floating point values. The operands must be identical types.
732 /// @brief Represents a floating point comparison operator.
733 class FCmpInst: public CmpInst {
735 static void destroyThis(FCmpInst* v) {
736 CmpInst::destroyThis(v);
740 /// This enumeration lists the possible predicates for the FCmpInst. Values
741 /// in the range 0-31 are reserved for FCmpInst.
743 // Opcode U L G E Intuitive operation
744 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
745 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
746 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
747 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
748 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
749 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
750 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
751 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
752 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
753 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
754 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
755 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
756 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
757 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
758 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
759 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
760 FIRST_FCMP_PREDICATE = FCMP_FALSE,
761 LAST_FCMP_PREDICATE = FCMP_TRUE,
762 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
765 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert
772 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
775 /// @brief Constructor with insert-at-block-end semantics.
777 Predicate pred, ///< The predicate to use for the comparison
778 Value *LHS, ///< The left-hand-side of the expression
779 Value *RHS, ///< The right-hand-side of the expression
780 const std::string &Name, ///< Name of the instruction
781 BasicBlock *InsertAtEnd ///< Block to insert into.
782 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
785 /// @brief Return the predicate for this instruction.
786 Predicate getPredicate() const { return Predicate(SubclassData); }
788 /// @brief Set the predicate for this instruction to the specified value.
789 void setPredicate(Predicate P) { SubclassData = P; }
791 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
792 /// @returns the inverse predicate for the instructions current predicate.
793 /// @brief Return the inverse of the predicate
794 Predicate getInversePredicate() const {
795 return getInversePredicate(getPredicate());
798 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
799 /// @returns the inverse predicate for \p pred.
800 /// @brief Return the inverse of a given predicate
801 static Predicate getInversePredicate(Predicate pred);
803 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
804 /// @returns the predicate that would be the result of exchanging the two
805 /// operands of the ICmpInst instruction without changing the result
807 /// @brief Return the predicate as if the operands were swapped
808 Predicate getSwappedPredicate() const {
809 return getSwappedPredicate(getPredicate());
812 /// This is a static version that you can use without an instruction
814 /// @brief Return the predicate as if the operands were swapped.
815 static Predicate getSwappedPredicate(Predicate Opcode);
817 /// This also tests for commutativity. If isEquality() returns true then
818 /// the predicate is also commutative. Only the equality predicates are
820 /// @returns true if the predicate of this instruction is EQ or NE.
821 /// @brief Determine if this is an equality predicate.
822 bool isEquality() const {
823 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
824 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
826 bool isCommutative() const { return isEquality(); }
828 /// @returns true if the predicate is relational (not EQ or NE).
829 /// @brief Determine if this a relational predicate.
830 bool isRelational() const { return !isEquality(); }
832 /// Exchange the two operands to this instruction in such a way that it does
833 /// not modify the semantics of the instruction. The predicate value may be
834 /// changed to retain the same result if the predicate is order dependent
836 /// @brief Swap operands and adjust predicate.
837 void swapOperands() {
838 SubclassData = getSwappedPredicate();
839 std::swap(Ops[0], Ops[1]);
842 virtual FCmpInst *clone() const;
844 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
845 static inline bool classof(const FCmpInst *) { return true; }
846 static inline bool classof(const Instruction *I) {
847 return I->getOpcode() == Instruction::FCmp;
849 static inline bool classof(const Value *V) {
850 return isa<Instruction>(V) && classof(cast<Instruction>(V));
854 //===----------------------------------------------------------------------===//
856 //===----------------------------------------------------------------------===//
857 /// CallInst - This class represents a function call, abstracting a target
858 /// machine's calling convention. This class uses low bit of the SubClassData
859 /// field to indicate whether or not this is a tail call. The rest of the bits
860 /// hold the calling convention of the call.
863 class CallInst : public Instruction {
864 const ParamAttrsList *ParamAttrs; ///< parameter attributes for call
865 CallInst(const CallInst &CI);
866 void init(Value *Func, Value* const *Params, unsigned NumParams);
867 void init(Value *Func, Value *Actual1, Value *Actual2);
868 void init(Value *Func, Value *Actual);
869 void init(Value *Func);
871 template<typename InputIterator>
872 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
873 const std::string &Name,
874 // This argument ensures that we have an iterator we can
875 // do arithmetic on in constant time
876 std::random_access_iterator_tag) {
877 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
879 // This requires that the iterator points to contiguous memory.
880 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
885 static void destroyThis(CallInst*v);
888 /// Construct a CallInst given a range of arguments. InputIterator
889 /// must be a random-access iterator pointing to contiguous storage
890 /// (e.g. a std::vector<>::iterator). Checks are made for
891 /// random-accessness but not for contiguous storage as that would
892 /// incur runtime overhead.
893 /// @brief Construct a CallInst from a range of arguments
894 template<typename InputIterator>
895 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
896 const std::string &Name = "", Instruction *InsertBefore = 0)
897 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
898 ->getElementType())->getReturnType(),
899 Instruction::Call, 0, 0, InsertBefore) {
900 init(Func, ArgBegin, ArgEnd, Name,
901 typename std::iterator_traits<InputIterator>::iterator_category());
904 /// Construct a CallInst given a range of arguments. InputIterator
905 /// must be a random-access iterator pointing to contiguous storage
906 /// (e.g. a std::vector<>::iterator). Checks are made for
907 /// random-accessness but not for contiguous storage as that would
908 /// incur runtime overhead.
909 /// @brief Construct a CallInst from a range of arguments
910 template<typename InputIterator>
911 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
912 const std::string &Name, BasicBlock *InsertAtEnd)
913 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
914 ->getElementType())->getReturnType(),
915 Instruction::Call, 0, 0, InsertAtEnd) {
916 init(Func, ArgBegin, ArgEnd, Name,
917 typename std::iterator_traits<InputIterator>::iterator_category());
920 CallInst(Value *F, Value *Actual, const std::string& Name = "",
921 Instruction *InsertBefore = 0);
922 CallInst(Value *F, Value *Actual, const std::string& Name,
923 BasicBlock *InsertAtEnd);
924 explicit CallInst(Value *F, const std::string &Name = "",
925 Instruction *InsertBefore = 0);
926 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
928 virtual CallInst *clone() const;
930 bool isTailCall() const { return SubclassData & 1; }
931 void setTailCall(bool isTailCall = true) {
932 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
935 /// getCallingConv/setCallingConv - Get or set the calling convention of this
937 unsigned getCallingConv() const { return SubclassData >> 1; }
938 void setCallingConv(unsigned CC) {
939 SubclassData = (SubclassData & 1) | (CC << 1);
942 /// Obtains a pointer to the ParamAttrsList object which holds the
943 /// parameter attributes information, if any.
944 /// @returns 0 if no attributes have been set.
945 /// @brief Get the parameter attributes.
946 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
948 /// Sets the parameter attributes for this CallInst. To construct a
949 /// ParamAttrsList, see ParameterAttributes.h
950 /// @brief Set the parameter attributes.
951 void setParamAttrs(const ParamAttrsList *attrs);
953 /// @brief Determine whether the call or the callee has the given attribute.
954 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
956 /// @brief Determine if the call does not access memory.
957 bool doesNotAccessMemory() const {
958 return paramHasAttr(0, ParamAttr::ReadNone);
961 /// @brief Determine if the call does not access or only reads memory.
962 bool onlyReadsMemory() const {
963 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
966 /// @brief Determine if the call cannot unwind.
967 bool isNoUnwind() const {
968 return paramHasAttr(0, ParamAttr::NoUnwind);
971 /// @brief Determine if the call returns a structure.
972 bool isStructReturn() const {
973 // Be friendly and also check the callee.
974 return paramHasAttr(1, ParamAttr::StructRet);
977 /// getCalledFunction - Return the function being called by this instruction
978 /// if it is a direct call. If it is a call through a function pointer,
980 Function *getCalledFunction() const {
981 return dyn_cast<Function>(getOperand(0));
984 /// getCalledValue - Get a pointer to the function that is invoked by this
986 inline const Value *getCalledValue() const { return getOperand(0); }
987 inline Value *getCalledValue() { return getOperand(0); }
989 // Methods for support type inquiry through isa, cast, and dyn_cast:
990 static inline bool classof(const CallInst *) { return true; }
991 static inline bool classof(const Instruction *I) {
992 return I->getOpcode() == Instruction::Call;
994 static inline bool classof(const Value *V) {
995 return isa<Instruction>(V) && classof(cast<Instruction>(V));
999 //===----------------------------------------------------------------------===//
1001 //===----------------------------------------------------------------------===//
1003 /// SelectInst - This class represents the LLVM 'select' instruction.
1005 class SelectInst : public Instruction {
1008 void init(Value *C, Value *S1, Value *S2) {
1009 Ops[0].init(C, this);
1010 Ops[1].init(S1, this);
1011 Ops[2].init(S2, this);
1014 SelectInst(const SelectInst &SI)
1015 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1016 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1019 static void destroyThis(SelectInst* v) {
1020 Instruction::destroyThis(v);
1024 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1025 Instruction *InsertBefore = 0)
1026 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
1030 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1031 BasicBlock *InsertAtEnd)
1032 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
1037 Value *getCondition() const { return Ops[0]; }
1038 Value *getTrueValue() const { return Ops[1]; }
1039 Value *getFalseValue() const { return Ops[2]; }
1041 /// Transparently provide more efficient getOperand methods.
1042 Value *getOperand(unsigned i) const {
1043 assert(i < 3 && "getOperand() out of range!");
1046 void setOperand(unsigned i, Value *Val) {
1047 assert(i < 3 && "setOperand() out of range!");
1050 unsigned getNumOperands() const { return 3; }
1052 OtherOps getOpcode() const {
1053 return static_cast<OtherOps>(Instruction::getOpcode());
1056 virtual SelectInst *clone() const;
1058 // Methods for support type inquiry through isa, cast, and dyn_cast:
1059 static inline bool classof(const SelectInst *) { return true; }
1060 static inline bool classof(const Instruction *I) {
1061 return I->getOpcode() == Instruction::Select;
1063 static inline bool classof(const Value *V) {
1064 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1068 //===----------------------------------------------------------------------===//
1070 //===----------------------------------------------------------------------===//
1072 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1073 /// an argument of the specified type given a va_list and increments that list
1075 class VAArgInst : public UnaryInstruction {
1076 VAArgInst(const VAArgInst &VAA)
1077 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1079 static void destroyThis(VAArgInst* v) {
1080 UnaryInstruction::destroyThis(v);
1084 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1085 Instruction *InsertBefore = 0)
1086 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1089 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1090 BasicBlock *InsertAtEnd)
1091 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1095 virtual VAArgInst *clone() const;
1097 // Methods for support type inquiry through isa, cast, and dyn_cast:
1098 static inline bool classof(const VAArgInst *) { return true; }
1099 static inline bool classof(const Instruction *I) {
1100 return I->getOpcode() == VAArg;
1102 static inline bool classof(const Value *V) {
1103 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1107 //===----------------------------------------------------------------------===//
1108 // ExtractElementInst Class
1109 //===----------------------------------------------------------------------===//
1111 /// ExtractElementInst - This instruction extracts a single (scalar)
1112 /// element from a VectorType value
1114 class ExtractElementInst : public Instruction {
1116 ExtractElementInst(const ExtractElementInst &EE) :
1117 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1118 Ops[0].init(EE.Ops[0], this);
1119 Ops[1].init(EE.Ops[1], this);
1123 static void destroyThis(ExtractElementInst* v) {
1124 Instruction::destroyThis(v);
1128 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1129 Instruction *InsertBefore = 0);
1130 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1131 Instruction *InsertBefore = 0);
1132 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1133 BasicBlock *InsertAtEnd);
1134 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1135 BasicBlock *InsertAtEnd);
1137 /// isValidOperands - Return true if an extractelement instruction can be
1138 /// formed with the specified operands.
1139 static bool isValidOperands(const Value *Vec, const Value *Idx);
1141 virtual ExtractElementInst *clone() const;
1143 /// Transparently provide more efficient getOperand methods.
1144 Value *getOperand(unsigned i) const {
1145 assert(i < 2 && "getOperand() out of range!");
1148 void setOperand(unsigned i, Value *Val) {
1149 assert(i < 2 && "setOperand() out of range!");
1152 unsigned getNumOperands() const { return 2; }
1154 // Methods for support type inquiry through isa, cast, and dyn_cast:
1155 static inline bool classof(const ExtractElementInst *) { return true; }
1156 static inline bool classof(const Instruction *I) {
1157 return I->getOpcode() == Instruction::ExtractElement;
1159 static inline bool classof(const Value *V) {
1160 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1164 //===----------------------------------------------------------------------===//
1165 // InsertElementInst Class
1166 //===----------------------------------------------------------------------===//
1168 /// InsertElementInst - This instruction inserts a single (scalar)
1169 /// element into a VectorType value
1171 class InsertElementInst : public Instruction {
1173 InsertElementInst(const InsertElementInst &IE);
1175 static void destroyThis(InsertElementInst* v) {
1176 Instruction::destroyThis(v);
1180 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1181 const std::string &Name = "",Instruction *InsertBefore = 0);
1182 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1183 const std::string &Name = "",Instruction *InsertBefore = 0);
1184 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1185 const std::string &Name, BasicBlock *InsertAtEnd);
1186 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1187 const std::string &Name, BasicBlock *InsertAtEnd);
1189 /// isValidOperands - Return true if an insertelement instruction can be
1190 /// formed with the specified operands.
1191 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1194 virtual InsertElementInst *clone() const;
1196 /// getType - Overload to return most specific vector type.
1198 inline const VectorType *getType() const {
1199 return reinterpret_cast<const VectorType*>(Instruction::getType());
1202 /// Transparently provide more efficient getOperand methods.
1203 Value *getOperand(unsigned i) const {
1204 assert(i < 3 && "getOperand() out of range!");
1207 void setOperand(unsigned i, Value *Val) {
1208 assert(i < 3 && "setOperand() out of range!");
1211 unsigned getNumOperands() const { return 3; }
1213 // Methods for support type inquiry through isa, cast, and dyn_cast:
1214 static inline bool classof(const InsertElementInst *) { return true; }
1215 static inline bool classof(const Instruction *I) {
1216 return I->getOpcode() == Instruction::InsertElement;
1218 static inline bool classof(const Value *V) {
1219 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1223 //===----------------------------------------------------------------------===//
1224 // ShuffleVectorInst Class
1225 //===----------------------------------------------------------------------===//
1227 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1230 class ShuffleVectorInst : public Instruction {
1232 ShuffleVectorInst(const ShuffleVectorInst &IE);
1234 static void destroyThis(ShuffleVectorInst* v) {
1235 Instruction::destroyThis(v);
1239 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1240 const std::string &Name = "", Instruction *InsertBefor = 0);
1241 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1242 const std::string &Name, BasicBlock *InsertAtEnd);
1244 /// isValidOperands - Return true if a shufflevector instruction can be
1245 /// formed with the specified operands.
1246 static bool isValidOperands(const Value *V1, const Value *V2,
1249 virtual ShuffleVectorInst *clone() const;
1251 /// getType - Overload to return most specific vector type.
1253 inline const VectorType *getType() const {
1254 return reinterpret_cast<const VectorType*>(Instruction::getType());
1257 /// Transparently provide more efficient getOperand methods.
1258 Value *getOperand(unsigned i) const {
1259 assert(i < 3 && "getOperand() out of range!");
1262 void setOperand(unsigned i, Value *Val) {
1263 assert(i < 3 && "setOperand() out of range!");
1266 unsigned getNumOperands() const { return 3; }
1268 // Methods for support type inquiry through isa, cast, and dyn_cast:
1269 static inline bool classof(const ShuffleVectorInst *) { return true; }
1270 static inline bool classof(const Instruction *I) {
1271 return I->getOpcode() == Instruction::ShuffleVector;
1273 static inline bool classof(const Value *V) {
1274 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1279 //===----------------------------------------------------------------------===//
1281 //===----------------------------------------------------------------------===//
1283 // PHINode - The PHINode class is used to represent the magical mystical PHI
1284 // node, that can not exist in nature, but can be synthesized in a computer
1285 // scientist's overactive imagination.
1287 class PHINode : public Instruction {
1288 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1289 /// the number actually in use.
1290 unsigned ReservedSpace;
1291 PHINode(const PHINode &PN);
1293 static void destroyThis(PHINode*);
1296 explicit PHINode(const Type *Ty, const std::string &Name = "",
1297 Instruction *InsertBefore = 0)
1298 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1303 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1304 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1309 /// reserveOperandSpace - This method can be used to avoid repeated
1310 /// reallocation of PHI operand lists by reserving space for the correct
1311 /// number of operands before adding them. Unlike normal vector reserves,
1312 /// this method can also be used to trim the operand space.
1313 void reserveOperandSpace(unsigned NumValues) {
1314 resizeOperands(NumValues*2);
1317 virtual PHINode *clone() const;
1319 /// getNumIncomingValues - Return the number of incoming edges
1321 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1323 /// getIncomingValue - Return incoming value number x
1325 Value *getIncomingValue(unsigned i) const {
1326 assert(i*2 < getNumOperands() && "Invalid value number!");
1327 return getOperand(i*2);
1329 void setIncomingValue(unsigned i, Value *V) {
1330 assert(i*2 < getNumOperands() && "Invalid value number!");
1333 unsigned getOperandNumForIncomingValue(unsigned i) {
1337 /// getIncomingBlock - Return incoming basic block number x
1339 BasicBlock *getIncomingBlock(unsigned i) const {
1340 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1342 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1343 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1345 unsigned getOperandNumForIncomingBlock(unsigned i) {
1349 /// addIncoming - Add an incoming value to the end of the PHI list
1351 void addIncoming(Value *V, BasicBlock *BB) {
1352 assert(getType() == V->getType() &&
1353 "All operands to PHI node must be the same type as the PHI node!");
1354 unsigned OpNo = NumOperands;
1355 if (OpNo+2 > ReservedSpace)
1356 resizeOperands(0); // Get more space!
1357 // Initialize some new operands.
1358 NumOperands = OpNo+2;
1359 OperandList[OpNo].init(V, this);
1360 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1363 /// removeIncomingValue - Remove an incoming value. This is useful if a
1364 /// predecessor basic block is deleted. The value removed is returned.
1366 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1367 /// is true), the PHI node is destroyed and any uses of it are replaced with
1368 /// dummy values. The only time there should be zero incoming values to a PHI
1369 /// node is when the block is dead, so this strategy is sound.
1371 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1373 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1374 int Idx = getBasicBlockIndex(BB);
1375 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1376 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1379 /// getBasicBlockIndex - Return the first index of the specified basic
1380 /// block in the value list for this PHI. Returns -1 if no instance.
1382 int getBasicBlockIndex(const BasicBlock *BB) const {
1383 Use *OL = OperandList;
1384 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1385 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1389 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1390 return getIncomingValue(getBasicBlockIndex(BB));
1393 /// hasConstantValue - If the specified PHI node always merges together the
1394 /// same value, return the value, otherwise return null.
1396 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1398 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1399 static inline bool classof(const PHINode *) { return true; }
1400 static inline bool classof(const Instruction *I) {
1401 return I->getOpcode() == Instruction::PHI;
1403 static inline bool classof(const Value *V) {
1404 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1407 void resizeOperands(unsigned NumOperands);
1410 //===----------------------------------------------------------------------===//
1412 //===----------------------------------------------------------------------===//
1414 //===---------------------------------------------------------------------------
1415 /// ReturnInst - Return a value (possibly void), from a function. Execution
1416 /// does not continue in this function any longer.
1418 class ReturnInst : public TerminatorInst {
1419 Use RetVal; // Return Value: null if 'void'.
1420 ReturnInst(const ReturnInst &RI);
1421 void init(Value *RetVal);
1424 // ReturnInst constructors:
1425 // ReturnInst() - 'ret void' instruction
1426 // ReturnInst( null) - 'ret void' instruction
1427 // ReturnInst(Value* X) - 'ret X' instruction
1428 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1429 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1430 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1431 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1433 // NOTE: If the Value* passed is of type void then the constructor behaves as
1434 // if it was passed NULL.
1435 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1436 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1437 explicit ReturnInst(BasicBlock *InsertAtEnd);
1439 virtual ReturnInst *clone() const;
1441 // Transparently provide more efficient getOperand methods.
1442 Value *getOperand(unsigned i) const {
1443 assert(i < getNumOperands() && "getOperand() out of range!");
1446 void setOperand(unsigned i, Value *Val) {
1447 assert(i < getNumOperands() && "setOperand() out of range!");
1451 Value *getReturnValue() const { return RetVal; }
1453 unsigned getNumSuccessors() const { return 0; }
1455 // Methods for support type inquiry through isa, cast, and dyn_cast:
1456 static inline bool classof(const ReturnInst *) { return true; }
1457 static inline bool classof(const Instruction *I) {
1458 return (I->getOpcode() == Instruction::Ret);
1460 static inline bool classof(const Value *V) {
1461 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1464 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1465 virtual unsigned getNumSuccessorsV() const;
1466 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1469 //===----------------------------------------------------------------------===//
1471 //===----------------------------------------------------------------------===//
1473 //===---------------------------------------------------------------------------
1474 /// BranchInst - Conditional or Unconditional Branch instruction.
1476 class BranchInst : public TerminatorInst {
1477 /// Ops list - Branches are strange. The operands are ordered:
1478 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1479 /// they don't have to check for cond/uncond branchness.
1481 BranchInst(const BranchInst &BI);
1484 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1485 // BranchInst(BB *B) - 'br B'
1486 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1487 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1488 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1489 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1490 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1491 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1492 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1493 Instruction *InsertBefore = 0);
1494 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1495 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1496 BasicBlock *InsertAtEnd);
1498 /// Transparently provide more efficient getOperand methods.
1499 Value *getOperand(unsigned i) const {
1500 assert(i < getNumOperands() && "getOperand() out of range!");
1503 void setOperand(unsigned i, Value *Val) {
1504 assert(i < getNumOperands() && "setOperand() out of range!");
1508 virtual BranchInst *clone() const;
1510 inline bool isUnconditional() const { return getNumOperands() == 1; }
1511 inline bool isConditional() const { return getNumOperands() == 3; }
1513 inline Value *getCondition() const {
1514 assert(isConditional() && "Cannot get condition of an uncond branch!");
1515 return getOperand(2);
1518 void setCondition(Value *V) {
1519 assert(isConditional() && "Cannot set condition of unconditional branch!");
1523 // setUnconditionalDest - Change the current branch to an unconditional branch
1524 // targeting the specified block.
1525 // FIXME: Eliminate this ugly method.
1526 void setUnconditionalDest(BasicBlock *Dest) {
1527 if (isConditional()) { // Convert this to an uncond branch.
1532 setOperand(0, reinterpret_cast<Value*>(Dest));
1535 unsigned getNumSuccessors() const { return 1+isConditional(); }
1537 BasicBlock *getSuccessor(unsigned i) const {
1538 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1539 return cast<BasicBlock>(getOperand(i));
1542 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1543 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1544 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1547 // Methods for support type inquiry through isa, cast, and dyn_cast:
1548 static inline bool classof(const BranchInst *) { return true; }
1549 static inline bool classof(const Instruction *I) {
1550 return (I->getOpcode() == Instruction::Br);
1552 static inline bool classof(const Value *V) {
1553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1556 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1557 virtual unsigned getNumSuccessorsV() const;
1558 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1561 //===----------------------------------------------------------------------===//
1563 //===----------------------------------------------------------------------===//
1565 //===---------------------------------------------------------------------------
1566 /// SwitchInst - Multiway switch
1568 class SwitchInst : public TerminatorInst {
1569 unsigned ReservedSpace;
1570 // Operand[0] = Value to switch on
1571 // Operand[1] = Default basic block destination
1572 // Operand[2n ] = Value to match
1573 // Operand[2n+1] = BasicBlock to go to on match
1574 SwitchInst(const SwitchInst &RI);
1575 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1576 void resizeOperands(unsigned No);
1578 static void destroyThis(SwitchInst*v);
1581 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1582 /// switch on and a default destination. The number of additional cases can
1583 /// be specified here to make memory allocation more efficient. This
1584 /// constructor can also autoinsert before another instruction.
1585 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1586 Instruction *InsertBefore = 0);
1588 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1589 /// switch on and a default destination. The number of additional cases can
1590 /// be specified here to make memory allocation more efficient. This
1591 /// constructor also autoinserts at the end of the specified BasicBlock.
1592 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1593 BasicBlock *InsertAtEnd);
1596 // Accessor Methods for Switch stmt
1597 inline Value *getCondition() const { return getOperand(0); }
1598 void setCondition(Value *V) { setOperand(0, V); }
1600 inline BasicBlock *getDefaultDest() const {
1601 return cast<BasicBlock>(getOperand(1));
1604 /// getNumCases - return the number of 'cases' in this switch instruction.
1605 /// Note that case #0 is always the default case.
1606 unsigned getNumCases() const {
1607 return getNumOperands()/2;
1610 /// getCaseValue - Return the specified case value. Note that case #0, the
1611 /// default destination, does not have a case value.
1612 ConstantInt *getCaseValue(unsigned i) {
1613 assert(i && i < getNumCases() && "Illegal case value to get!");
1614 return getSuccessorValue(i);
1617 /// getCaseValue - Return the specified case value. Note that case #0, the
1618 /// default destination, does not have a case value.
1619 const ConstantInt *getCaseValue(unsigned i) const {
1620 assert(i && i < getNumCases() && "Illegal case value to get!");
1621 return getSuccessorValue(i);
1624 /// findCaseValue - Search all of the case values for the specified constant.
1625 /// If it is explicitly handled, return the case number of it, otherwise
1626 /// return 0 to indicate that it is handled by the default handler.
1627 unsigned findCaseValue(const ConstantInt *C) const {
1628 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1629 if (getCaseValue(i) == C)
1634 /// findCaseDest - Finds the unique case value for a given successor. Returns
1635 /// null if the successor is not found, not unique, or is the default case.
1636 ConstantInt *findCaseDest(BasicBlock *BB) {
1637 if (BB == getDefaultDest()) return NULL;
1639 ConstantInt *CI = NULL;
1640 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1641 if (getSuccessor(i) == BB) {
1642 if (CI) return NULL; // Multiple cases lead to BB.
1643 else CI = getCaseValue(i);
1649 /// addCase - Add an entry to the switch instruction...
1651 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1653 /// removeCase - This method removes the specified successor from the switch
1654 /// instruction. Note that this cannot be used to remove the default
1655 /// destination (successor #0).
1657 void removeCase(unsigned idx);
1659 virtual SwitchInst *clone() const;
1661 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1662 BasicBlock *getSuccessor(unsigned idx) const {
1663 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1664 return cast<BasicBlock>(getOperand(idx*2+1));
1666 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1667 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1668 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1671 // getSuccessorValue - Return the value associated with the specified
1673 inline ConstantInt *getSuccessorValue(unsigned idx) const {
1674 assert(idx < getNumSuccessors() && "Successor # out of range!");
1675 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1678 // Methods for support type inquiry through isa, cast, and dyn_cast:
1679 static inline bool classof(const SwitchInst *) { return true; }
1680 static inline bool classof(const Instruction *I) {
1681 return I->getOpcode() == Instruction::Switch;
1683 static inline bool classof(const Value *V) {
1684 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1687 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1688 virtual unsigned getNumSuccessorsV() const;
1689 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1692 //===----------------------------------------------------------------------===//
1694 //===----------------------------------------------------------------------===//
1696 //===---------------------------------------------------------------------------
1698 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1699 /// calling convention of the call.
1701 class InvokeInst : public TerminatorInst {
1702 const ParamAttrsList *ParamAttrs;
1703 InvokeInst(const InvokeInst &BI);
1704 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1705 Value* const *Args, unsigned NumArgs);
1707 template<typename InputIterator>
1708 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1709 InputIterator ArgBegin, InputIterator ArgEnd,
1710 const std::string &Name,
1711 // This argument ensures that we have an iterator we can
1712 // do arithmetic on in constant time
1713 std::random_access_iterator_tag) {
1714 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
1716 // This requires that the iterator points to contiguous memory.
1717 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
1722 static void destroyThis(InvokeInst*v);
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 = "", Instruction *InsertBefore = 0)
1736 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1737 ->getElementType())->getReturnType(),
1738 Instruction::Invoke, 0, 0, InsertBefore) {
1739 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1740 typename std::iterator_traits<InputIterator>::iterator_category());
1743 /// Construct an InvokeInst given a range of arguments.
1744 /// InputIterator must be a random-access iterator pointing to
1745 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
1746 /// made for random-accessness but not for contiguous storage as
1747 /// that would incur runtime overhead.
1749 /// @brief Construct an InvokeInst from a range of arguments
1750 template<typename InputIterator>
1751 InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
1752 InputIterator ArgBegin, InputIterator ArgEnd,
1753 const std::string &Name, BasicBlock *InsertAtEnd)
1754 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
1755 ->getElementType())->getReturnType(),
1756 Instruction::Invoke, 0, 0, InsertAtEnd) {
1757 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
1758 typename std::iterator_traits<InputIterator>::iterator_category());
1761 virtual InvokeInst *clone() const;
1763 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1765 unsigned getCallingConv() const { return SubclassData; }
1766 void setCallingConv(unsigned CC) {
1770 /// Obtains a pointer to the ParamAttrsList object which holds the
1771 /// parameter attributes information, if any.
1772 /// @returns 0 if no attributes have been set.
1773 /// @brief Get the parameter attributes.
1774 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
1776 /// Sets the parameter attributes for this InvokeInst. To construct a
1777 /// ParamAttrsList, see ParameterAttributes.h
1778 /// @brief Set the parameter attributes.
1779 void setParamAttrs(const ParamAttrsList *attrs);
1781 /// @brief Determine whether the call or the callee has the given attribute.
1782 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
1784 /// @brief Determine if the call does not access memory.
1785 bool doesNotAccessMemory() const {
1786 return paramHasAttr(0, ParamAttr::ReadNone);
1789 /// @brief Determine if the call does not access or only reads memory.
1790 bool onlyReadsMemory() const {
1791 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1794 /// @brief Determine if the call cannot unwind.
1795 bool isNoUnwind() const {
1796 return paramHasAttr(0, ParamAttr::NoUnwind);
1799 /// @brief Determine if the call returns a structure.
1800 bool isStructReturn() const {
1801 // Be friendly and also check the callee.
1802 return paramHasAttr(1, ParamAttr::StructRet);
1805 /// getCalledFunction - Return the function called, or null if this is an
1806 /// indirect function invocation.
1808 Function *getCalledFunction() const {
1809 return dyn_cast<Function>(getOperand(0));
1812 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1813 inline Value *getCalledValue() const { return getOperand(0); }
1815 // get*Dest - Return the destination basic blocks...
1816 BasicBlock *getNormalDest() const {
1817 return cast<BasicBlock>(getOperand(1));
1819 BasicBlock *getUnwindDest() const {
1820 return cast<BasicBlock>(getOperand(2));
1822 void setNormalDest(BasicBlock *B) {
1823 setOperand(1, reinterpret_cast<Value*>(B));
1826 void setUnwindDest(BasicBlock *B) {
1827 setOperand(2, reinterpret_cast<Value*>(B));
1830 inline BasicBlock *getSuccessor(unsigned i) const {
1831 assert(i < 2 && "Successor # out of range for invoke!");
1832 return i == 0 ? getNormalDest() : getUnwindDest();
1835 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1836 assert(idx < 2 && "Successor # out of range for invoke!");
1837 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1840 unsigned getNumSuccessors() const { return 2; }
1842 // Methods for support type inquiry through isa, cast, and dyn_cast:
1843 static inline bool classof(const InvokeInst *) { return true; }
1844 static inline bool classof(const Instruction *I) {
1845 return (I->getOpcode() == Instruction::Invoke);
1847 static inline bool classof(const Value *V) {
1848 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1851 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1852 virtual unsigned getNumSuccessorsV() const;
1853 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1857 //===----------------------------------------------------------------------===//
1859 //===----------------------------------------------------------------------===//
1861 //===---------------------------------------------------------------------------
1862 /// UnwindInst - Immediately exit the current function, unwinding the stack
1863 /// until an invoke instruction is found.
1865 class UnwindInst : public TerminatorInst {
1867 explicit UnwindInst(Instruction *InsertBefore = 0);
1868 explicit UnwindInst(BasicBlock *InsertAtEnd);
1870 virtual UnwindInst *clone() const;
1872 unsigned getNumSuccessors() const { return 0; }
1874 // Methods for support type inquiry through isa, cast, and dyn_cast:
1875 static inline bool classof(const UnwindInst *) { return true; }
1876 static inline bool classof(const Instruction *I) {
1877 return I->getOpcode() == Instruction::Unwind;
1879 static inline bool classof(const Value *V) {
1880 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1883 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1884 virtual unsigned getNumSuccessorsV() const;
1885 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1888 //===----------------------------------------------------------------------===//
1889 // UnreachableInst Class
1890 //===----------------------------------------------------------------------===//
1892 //===---------------------------------------------------------------------------
1893 /// UnreachableInst - This function has undefined behavior. In particular, the
1894 /// presence of this instruction indicates some higher level knowledge that the
1895 /// end of the block cannot be reached.
1897 class UnreachableInst : public TerminatorInst {
1899 explicit UnreachableInst(Instruction *InsertBefore = 0);
1900 explicit UnreachableInst(BasicBlock *InsertAtEnd);
1902 virtual UnreachableInst *clone() const;
1904 unsigned getNumSuccessors() const { return 0; }
1906 // Methods for support type inquiry through isa, cast, and dyn_cast:
1907 static inline bool classof(const UnreachableInst *) { return true; }
1908 static inline bool classof(const Instruction *I) {
1909 return I->getOpcode() == Instruction::Unreachable;
1911 static inline bool classof(const Value *V) {
1912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1915 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1916 virtual unsigned getNumSuccessorsV() const;
1917 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1920 //===----------------------------------------------------------------------===//
1922 //===----------------------------------------------------------------------===//
1924 /// @brief This class represents a truncation of integer types.
1925 class TruncInst : public CastInst {
1926 /// Private copy constructor
1927 TruncInst(const TruncInst &CI)
1928 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1931 static void destroyThis(TruncInst* v) {
1932 CastInst::destroyThis(v);
1936 /// @brief Constructor with insert-before-instruction semantics
1938 Value *S, ///< The value to be truncated
1939 const Type *Ty, ///< The (smaller) type to truncate to
1940 const std::string &Name = "", ///< A name for the new instruction
1941 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1944 /// @brief Constructor with insert-at-end-of-block semantics
1946 Value *S, ///< The value to be truncated
1947 const Type *Ty, ///< The (smaller) type to truncate to
1948 const std::string &Name, ///< A name for the new instruction
1949 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1952 /// @brief Clone an identical TruncInst
1953 virtual CastInst *clone() const;
1955 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1956 static inline bool classof(const TruncInst *) { return true; }
1957 static inline bool classof(const Instruction *I) {
1958 return I->getOpcode() == Trunc;
1960 static inline bool classof(const Value *V) {
1961 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1965 //===----------------------------------------------------------------------===//
1967 //===----------------------------------------------------------------------===//
1969 /// @brief This class represents zero extension of integer types.
1970 class ZExtInst : public CastInst {
1971 /// @brief Private copy constructor
1972 ZExtInst(const ZExtInst &CI)
1973 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1976 static void destroyThis(ZExtInst* v) {
1977 CastInst::destroyThis(v);
1981 /// @brief Constructor with insert-before-instruction semantics
1983 Value *S, ///< The value to be zero extended
1984 const Type *Ty, ///< The type to zero extend to
1985 const std::string &Name = "", ///< A name for the new instruction
1986 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1989 /// @brief Constructor with insert-at-end semantics.
1991 Value *S, ///< The value to be zero extended
1992 const Type *Ty, ///< The type to zero extend to
1993 const std::string &Name, ///< A name for the new instruction
1994 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1997 /// @brief Clone an identical ZExtInst
1998 virtual CastInst *clone() const;
2000 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2001 static inline bool classof(const ZExtInst *) { return true; }
2002 static inline bool classof(const Instruction *I) {
2003 return I->getOpcode() == ZExt;
2005 static inline bool classof(const Value *V) {
2006 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2010 //===----------------------------------------------------------------------===//
2012 //===----------------------------------------------------------------------===//
2014 /// @brief This class represents a sign extension of integer types.
2015 class SExtInst : public CastInst {
2016 /// @brief Private copy constructor
2017 SExtInst(const SExtInst &CI)
2018 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2021 static void destroyThis(SExtInst* v) {
2022 CastInst::destroyThis(v);
2026 /// @brief Constructor with insert-before-instruction semantics
2028 Value *S, ///< The value to be sign extended
2029 const Type *Ty, ///< The type to sign extend to
2030 const std::string &Name = "", ///< A name for the new instruction
2031 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2034 /// @brief Constructor with insert-at-end-of-block semantics
2036 Value *S, ///< The value to be sign extended
2037 const Type *Ty, ///< The type to sign extend to
2038 const std::string &Name, ///< A name for the new instruction
2039 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2042 /// @brief Clone an identical SExtInst
2043 virtual CastInst *clone() const;
2045 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2046 static inline bool classof(const SExtInst *) { return true; }
2047 static inline bool classof(const Instruction *I) {
2048 return I->getOpcode() == SExt;
2050 static inline bool classof(const Value *V) {
2051 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2055 //===----------------------------------------------------------------------===//
2056 // FPTruncInst Class
2057 //===----------------------------------------------------------------------===//
2059 /// @brief This class represents a truncation of floating point types.
2060 class FPTruncInst : public CastInst {
2061 FPTruncInst(const FPTruncInst &CI)
2062 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2065 static void destroyThis(FPTruncInst* v) {
2066 CastInst::destroyThis(v);
2070 /// @brief Constructor with insert-before-instruction semantics
2072 Value *S, ///< The value to be truncated
2073 const Type *Ty, ///< The type to truncate to
2074 const std::string &Name = "", ///< A name for the new instruction
2075 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2078 /// @brief Constructor with insert-before-instruction semantics
2080 Value *S, ///< The value to be truncated
2081 const Type *Ty, ///< The type to truncate to
2082 const std::string &Name, ///< A name for the new instruction
2083 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2086 /// @brief Clone an identical FPTruncInst
2087 virtual CastInst *clone() const;
2089 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2090 static inline bool classof(const FPTruncInst *) { return true; }
2091 static inline bool classof(const Instruction *I) {
2092 return I->getOpcode() == FPTrunc;
2094 static inline bool classof(const Value *V) {
2095 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2099 //===----------------------------------------------------------------------===//
2101 //===----------------------------------------------------------------------===//
2103 /// @brief This class represents an extension of floating point types.
2104 class FPExtInst : public CastInst {
2105 FPExtInst(const FPExtInst &CI)
2106 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2109 static void destroyThis(FPExtInst* v) {
2110 CastInst::destroyThis(v);
2114 /// @brief Constructor with insert-before-instruction semantics
2116 Value *S, ///< The value to be extended
2117 const Type *Ty, ///< The type to extend to
2118 const std::string &Name = "", ///< A name for the new instruction
2119 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2122 /// @brief Constructor with insert-at-end-of-block semantics
2124 Value *S, ///< The value to be extended
2125 const Type *Ty, ///< The type to extend to
2126 const std::string &Name, ///< A name for the new instruction
2127 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2130 /// @brief Clone an identical FPExtInst
2131 virtual CastInst *clone() const;
2133 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2134 static inline bool classof(const FPExtInst *) { return true; }
2135 static inline bool classof(const Instruction *I) {
2136 return I->getOpcode() == FPExt;
2138 static inline bool classof(const Value *V) {
2139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2143 //===----------------------------------------------------------------------===//
2145 //===----------------------------------------------------------------------===//
2147 /// @brief This class represents a cast unsigned integer to floating point.
2148 class UIToFPInst : public CastInst {
2149 UIToFPInst(const UIToFPInst &CI)
2150 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2153 static void destroyThis(UIToFPInst* v) {
2154 CastInst::destroyThis(v);
2158 /// @brief Constructor with insert-before-instruction semantics
2160 Value *S, ///< The value to be converted
2161 const Type *Ty, ///< The type to convert to
2162 const std::string &Name = "", ///< A name for the new instruction
2163 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2166 /// @brief Constructor with insert-at-end-of-block semantics
2168 Value *S, ///< The value to be converted
2169 const Type *Ty, ///< The type to convert to
2170 const std::string &Name, ///< A name for the new instruction
2171 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2174 /// @brief Clone an identical UIToFPInst
2175 virtual CastInst *clone() const;
2177 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2178 static inline bool classof(const UIToFPInst *) { return true; }
2179 static inline bool classof(const Instruction *I) {
2180 return I->getOpcode() == UIToFP;
2182 static inline bool classof(const Value *V) {
2183 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2187 //===----------------------------------------------------------------------===//
2189 //===----------------------------------------------------------------------===//
2191 /// @brief This class represents a cast from signed integer to floating point.
2192 class SIToFPInst : public CastInst {
2193 SIToFPInst(const SIToFPInst &CI)
2194 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2197 static void destroyThis(SIToFPInst* v) {
2198 CastInst::destroyThis(v);
2202 /// @brief Constructor with insert-before-instruction semantics
2204 Value *S, ///< The value to be converted
2205 const Type *Ty, ///< The type to convert to
2206 const std::string &Name = "", ///< A name for the new instruction
2207 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2210 /// @brief Constructor with insert-at-end-of-block semantics
2212 Value *S, ///< The value to be converted
2213 const Type *Ty, ///< The type to convert to
2214 const std::string &Name, ///< A name for the new instruction
2215 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2218 /// @brief Clone an identical SIToFPInst
2219 virtual CastInst *clone() const;
2221 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2222 static inline bool classof(const SIToFPInst *) { return true; }
2223 static inline bool classof(const Instruction *I) {
2224 return I->getOpcode() == SIToFP;
2226 static inline bool classof(const Value *V) {
2227 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2231 //===----------------------------------------------------------------------===//
2233 //===----------------------------------------------------------------------===//
2235 /// @brief This class represents a cast from floating point to unsigned integer
2236 class FPToUIInst : public CastInst {
2237 FPToUIInst(const FPToUIInst &CI)
2238 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2241 static void destroyThis(FPToUIInst* v) {
2242 CastInst::destroyThis(v);
2246 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2254 /// @brief Constructor with insert-at-end-of-block semantics
2256 Value *S, ///< The value to be converted
2257 const Type *Ty, ///< The type to convert to
2258 const std::string &Name, ///< A name for the new instruction
2259 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2262 /// @brief Clone an identical FPToUIInst
2263 virtual CastInst *clone() const;
2265 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2266 static inline bool classof(const FPToUIInst *) { return true; }
2267 static inline bool classof(const Instruction *I) {
2268 return I->getOpcode() == FPToUI;
2270 static inline bool classof(const Value *V) {
2271 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2275 //===----------------------------------------------------------------------===//
2277 //===----------------------------------------------------------------------===//
2279 /// @brief This class represents a cast from floating point to signed integer.
2280 class FPToSIInst : public CastInst {
2281 FPToSIInst(const FPToSIInst &CI)
2282 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2285 static void destroyThis(FPToSIInst* v) {
2286 CastInst::destroyThis(v);
2290 /// @brief Constructor with insert-before-instruction semantics
2292 Value *S, ///< The value to be converted
2293 const Type *Ty, ///< The type to convert to
2294 const std::string &Name = "", ///< A name for the new instruction
2295 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2298 /// @brief Constructor with insert-at-end-of-block semantics
2300 Value *S, ///< The value to be converted
2301 const Type *Ty, ///< The type to convert to
2302 const std::string &Name, ///< A name for the new instruction
2303 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2306 /// @brief Clone an identical FPToSIInst
2307 virtual CastInst *clone() const;
2309 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2310 static inline bool classof(const FPToSIInst *) { return true; }
2311 static inline bool classof(const Instruction *I) {
2312 return I->getOpcode() == FPToSI;
2314 static inline bool classof(const Value *V) {
2315 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2319 //===----------------------------------------------------------------------===//
2320 // IntToPtrInst Class
2321 //===----------------------------------------------------------------------===//
2323 /// @brief This class represents a cast from an integer to a pointer.
2324 class IntToPtrInst : public CastInst {
2325 IntToPtrInst(const IntToPtrInst &CI)
2326 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2329 static void destroyThis(IntToPtrInst* v) {
2330 CastInst::destroyThis(v);
2334 /// @brief Constructor with insert-before-instruction semantics
2336 Value *S, ///< The value to be converted
2337 const Type *Ty, ///< The type to convert 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 converted
2345 const Type *Ty, ///< The type to convert 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 IntToPtrInst
2351 virtual CastInst *clone() const;
2353 // Methods for support type inquiry through isa, cast, and dyn_cast:
2354 static inline bool classof(const IntToPtrInst *) { return true; }
2355 static inline bool classof(const Instruction *I) {
2356 return I->getOpcode() == IntToPtr;
2358 static inline bool classof(const Value *V) {
2359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2363 //===----------------------------------------------------------------------===//
2364 // PtrToIntInst Class
2365 //===----------------------------------------------------------------------===//
2367 /// @brief This class represents a cast from a pointer to an integer
2368 class PtrToIntInst : public CastInst {
2369 PtrToIntInst(const PtrToIntInst &CI)
2370 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2373 static void destroyThis(PtrToIntInst* v) {
2374 CastInst::destroyThis(v);
2378 /// @brief Constructor with insert-before-instruction semantics
2380 Value *S, ///< The value to be converted
2381 const Type *Ty, ///< The type to convert to
2382 const std::string &Name = "", ///< A name for the new instruction
2383 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2386 /// @brief Constructor with insert-at-end-of-block semantics
2388 Value *S, ///< The value to be converted
2389 const Type *Ty, ///< The type to convert to
2390 const std::string &Name, ///< A name for the new instruction
2391 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2394 /// @brief Clone an identical PtrToIntInst
2395 virtual CastInst *clone() const;
2397 // Methods for support type inquiry through isa, cast, and dyn_cast:
2398 static inline bool classof(const PtrToIntInst *) { return true; }
2399 static inline bool classof(const Instruction *I) {
2400 return I->getOpcode() == PtrToInt;
2402 static inline bool classof(const Value *V) {
2403 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2407 //===----------------------------------------------------------------------===//
2408 // BitCastInst Class
2409 //===----------------------------------------------------------------------===//
2411 /// @brief This class represents a no-op cast from one type to another.
2412 class BitCastInst : public CastInst {
2413 BitCastInst(const BitCastInst &CI)
2414 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2417 static void destroyThis(BitCastInst* v) {
2418 CastInst::destroyThis(v);
2422 /// @brief Constructor with insert-before-instruction semantics
2424 Value *S, ///< The value to be casted
2425 const Type *Ty, ///< The type to casted to
2426 const std::string &Name = "", ///< A name for the new instruction
2427 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2430 /// @brief Constructor with insert-at-end-of-block semantics
2432 Value *S, ///< The value to be casted
2433 const Type *Ty, ///< The type to casted to
2434 const std::string &Name, ///< A name for the new instruction
2435 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2438 /// @brief Clone an identical BitCastInst
2439 virtual CastInst *clone() const;
2441 // Methods for support type inquiry through isa, cast, and dyn_cast:
2442 static inline bool classof(const BitCastInst *) { return true; }
2443 static inline bool classof(const Instruction *I) {
2444 return I->getOpcode() == BitCast;
2446 static inline bool classof(const Value *V) {
2447 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2451 } // End llvm namespace