1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
21 #include "llvm/InstrTypes.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/ParameterAttributes.h"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
44 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
45 const std::string &Name = "", Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
47 const std::string &Name, BasicBlock *InsertAtEnd);
49 // Out of line virtual method, so the vtable, etc has a home.
50 virtual ~AllocationInst();
52 /// isArrayAllocation - Return true if there is an allocation size parameter
53 /// to the allocation instruction that is not 1.
55 bool isArrayAllocation() const;
57 /// getArraySize - Get the number of element allocated, for a simple
58 /// allocation of a single element, this will return a constant 1 value.
60 const Value *getArraySize() const { return getOperand(0); }
61 Value *getArraySize() { return getOperand(0); }
63 /// getType - Overload to return most specific pointer type
65 const PointerType *getType() const {
66 return reinterpret_cast<const PointerType*>(Instruction::getType());
69 /// getAllocatedType - Return the type that is being allocated by the
72 const Type *getAllocatedType() const;
74 /// getAlignment - Return the alignment of the memory that is being allocated
75 /// by the instruction.
77 unsigned getAlignment() const { return Alignment; }
78 void setAlignment(unsigned Align) {
79 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
83 virtual Instruction *clone() const = 0;
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const AllocationInst *) { return true; }
87 static inline bool classof(const Instruction *I) {
88 return I->getOpcode() == Instruction::Alloca ||
89 I->getOpcode() == Instruction::Malloc;
91 static inline bool classof(const Value *V) {
92 return isa<Instruction>(V) && classof(cast<Instruction>(V));
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 /// MallocInst - an instruction to allocated memory on the heap
103 class MallocInst : public AllocationInst {
104 MallocInst(const MallocInst &MI);
106 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
107 const std::string &Name = "",
108 Instruction *InsertBefore = 0)
109 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
110 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
111 BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
114 MallocInst(const Type *Ty, const std::string &Name,
115 Instruction *InsertBefore = 0)
116 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
117 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
121 const std::string &Name, BasicBlock *InsertAtEnd)
122 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
123 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
124 const std::string &Name = "",
125 Instruction *InsertBefore = 0)
126 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
128 virtual MallocInst *clone() const;
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const MallocInst *) { return true; }
132 static inline bool classof(const Instruction *I) {
133 return (I->getOpcode() == Instruction::Malloc);
135 static inline bool classof(const Value *V) {
136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
141 //===----------------------------------------------------------------------===//
143 //===----------------------------------------------------------------------===//
145 /// AllocaInst - an instruction to allocate memory on the stack
147 class AllocaInst : public AllocationInst {
148 AllocaInst(const AllocaInst &);
150 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
151 const std::string &Name = "",
152 Instruction *InsertBefore = 0)
153 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
154 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
155 BasicBlock *InsertAtEnd)
156 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
158 AllocaInst(const Type *Ty, const std::string &Name,
159 Instruction *InsertBefore = 0)
160 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
161 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
165 const std::string &Name = "", Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
167 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
168 const std::string &Name, BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
171 virtual AllocaInst *clone() const;
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const AllocaInst *) { return true; }
175 static inline bool classof(const Instruction *I) {
176 return (I->getOpcode() == Instruction::Alloca);
178 static inline bool classof(const Value *V) {
179 return isa<Instruction>(V) && classof(cast<Instruction>(V));
184 //===----------------------------------------------------------------------===//
186 //===----------------------------------------------------------------------===//
188 /// FreeInst - an instruction to deallocate memory
190 class FreeInst : public UnaryInstruction {
193 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
194 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
196 virtual FreeInst *clone() const;
198 // Accessor methods for consistency with other memory operations
199 Value *getPointerOperand() { return getOperand(0); }
200 const Value *getPointerOperand() const { return getOperand(0); }
202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const FreeInst *) { return true; }
204 static inline bool classof(const Instruction *I) {
205 return (I->getOpcode() == Instruction::Free);
207 static inline bool classof(const Value *V) {
208 return isa<Instruction>(V) && classof(cast<Instruction>(V));
213 //===----------------------------------------------------------------------===//
215 //===----------------------------------------------------------------------===//
217 /// LoadInst - an instruction for reading from memory. This uses the
218 /// SubclassData field in Value to store whether or not the load is volatile.
220 class LoadInst : public UnaryInstruction {
222 LoadInst(const LoadInst &LI)
223 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
224 setVolatile(LI.isVolatile());
225 setAlignment(LI.getAlignment());
233 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
234 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
235 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
236 Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
238 Instruction *InsertBefore = 0);
239 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
240 BasicBlock *InsertAtEnd);
241 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
242 BasicBlock *InsertAtEnd);
244 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
245 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
246 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
247 Instruction *InsertBefore = 0);
248 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
249 BasicBlock *InsertAtEnd);
251 /// isVolatile - Return true if this is a load from a volatile memory
254 bool isVolatile() const { return SubclassData & 1; }
256 /// setVolatile - Specify whether this is a volatile load or not.
258 void setVolatile(bool V) {
259 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
262 virtual LoadInst *clone() const;
264 /// getAlignment - Return the alignment of the access that is being performed
266 unsigned getAlignment() const {
267 return (1 << (SubclassData>>1)) >> 1;
270 void setAlignment(unsigned Align);
272 Value *getPointerOperand() { return getOperand(0); }
273 const Value *getPointerOperand() const { return getOperand(0); }
274 static unsigned getPointerOperandIndex() { return 0U; }
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const LoadInst *) { return true; }
278 static inline bool classof(const Instruction *I) {
279 return I->getOpcode() == Instruction::Load;
281 static inline bool classof(const Value *V) {
282 return isa<Instruction>(V) && classof(cast<Instruction>(V));
287 //===----------------------------------------------------------------------===//
289 //===----------------------------------------------------------------------===//
291 /// StoreInst - an instruction for storing to memory
293 class StoreInst : public Instruction {
296 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
297 Ops[0].init(SI.Ops[0], this);
298 Ops[1].init(SI.Ops[1], this);
299 setVolatile(SI.isVolatile());
300 setAlignment(SI.getAlignment());
308 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
309 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
310 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
311 Instruction *InsertBefore = 0);
312 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
313 unsigned Align, Instruction *InsertBefore = 0);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, BasicBlock *InsertAtEnd);
319 /// isVolatile - Return true if this is a load from a volatile memory
322 bool isVolatile() const { return SubclassData & 1; }
324 /// setVolatile - Specify whether this is a volatile load or not.
326 void setVolatile(bool V) {
327 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
330 /// Transparently provide more efficient getOperand methods.
331 Value *getOperand(unsigned i) const {
332 assert(i < 2 && "getOperand() out of range!");
335 void setOperand(unsigned i, Value *Val) {
336 assert(i < 2 && "setOperand() out of range!");
339 unsigned getNumOperands() const { return 2; }
341 /// getAlignment - Return the alignment of the access that is being performed
343 unsigned getAlignment() const {
344 return (1 << (SubclassData>>1)) >> 1;
347 void setAlignment(unsigned Align);
349 virtual StoreInst *clone() const;
351 Value *getPointerOperand() { return getOperand(1); }
352 const Value *getPointerOperand() const { return getOperand(1); }
353 static unsigned getPointerOperandIndex() { return 1U; }
355 // Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const StoreInst *) { return true; }
357 static inline bool classof(const Instruction *I) {
358 return I->getOpcode() == Instruction::Store;
360 static inline bool classof(const Value *V) {
361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
366 //===----------------------------------------------------------------------===//
367 // GetElementPtrInst Class
368 //===----------------------------------------------------------------------===//
370 // checkType - Simple wrapper function to give a better assertion failure
371 // message on bad indexes for a gep instruction.
373 static inline const Type *checkType(const Type *Ty) {
374 assert(Ty && "Invalid GetElementPtrInst indices for type!");
378 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
379 /// access elements of arrays and structs
381 class GetElementPtrInst : public Instruction {
382 GetElementPtrInst(const GetElementPtrInst &GEPI)
383 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
384 0, GEPI.getNumOperands()) {
385 Use *OL = OperandList = new Use[NumOperands];
386 Use *GEPIOL = GEPI.OperandList;
387 for (unsigned i = 0, E = NumOperands; i != E; ++i)
388 OL[i].init(GEPIOL[i], this);
390 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
391 void init(Value *Ptr, Value *Idx);
393 template<typename InputIterator>
394 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
395 const std::string &Name,
396 // This argument ensures that we have an iterator we can
397 // do arithmetic on in constant time
398 std::random_access_iterator_tag) {
399 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
400 std::distance(IdxBegin, IdxEnd);
403 // This requires that the itoerator points to contiguous memory.
404 init(Ptr, &*IdxBegin, NumIdx);
407 init(Ptr, 0, NumIdx);
413 /// getIndexedType - Returns the type of the element that would be loaded with
414 /// a load instruction with the specified parameters.
416 /// A null type is returned if the indices are invalid for the specified
419 static const Type *getIndexedType(const Type *Ptr,
420 Value* const *Idx, unsigned NumIdx,
421 bool AllowStructLeaf = false);
423 template<typename InputIterator>
424 static const Type *getIndexedType(const Type *Ptr,
425 InputIterator IdxBegin,
426 InputIterator IdxEnd,
427 bool AllowStructLeaf,
428 // This argument ensures that we
429 // have an iterator we can do
430 // arithmetic on in constant time
431 std::random_access_iterator_tag) {
432 typename std::iterator_traits<InputIterator>::difference_type NumIdx =
433 std::distance(IdxBegin, IdxEnd);
436 // This requires that the iterator points to contiguous memory.
437 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
441 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
450 template<typename InputIterator>
451 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
453 const std::string &Name = "",
454 Instruction *InsertBefore =0)
455 : Instruction(PointerType::get(
456 checkType(getIndexedType(Ptr->getType(),
457 IdxBegin, IdxEnd, true)),
458 cast<PointerType>(Ptr->getType())->getAddressSpace()),
459 GetElementPtr, 0, 0, InsertBefore) {
460 init(Ptr, IdxBegin, IdxEnd, Name,
461 typename std::iterator_traits<InputIterator>::iterator_category());
463 template<typename InputIterator>
464 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
465 const std::string &Name, BasicBlock *InsertAtEnd)
466 : Instruction(PointerType::get(
467 checkType(getIndexedType(Ptr->getType(),
468 IdxBegin, IdxEnd, true)),
469 cast<PointerType>(Ptr->getType())->getAddressSpace()),
470 GetElementPtr, 0, 0, InsertAtEnd) {
471 init(Ptr, IdxBegin, IdxEnd, Name,
472 typename std::iterator_traits<InputIterator>::iterator_category());
475 /// Constructors - These two constructors are convenience methods because one
476 /// and two index getelementptr instructions are so common.
477 GetElementPtrInst(Value *Ptr, Value *Idx,
478 const std::string &Name = "", Instruction *InsertBefore =0);
479 GetElementPtrInst(Value *Ptr, Value *Idx,
480 const std::string &Name, BasicBlock *InsertAtEnd);
481 ~GetElementPtrInst();
483 virtual GetElementPtrInst *clone() const;
485 // getType - Overload to return most specific pointer type...
486 const PointerType *getType() const {
487 return reinterpret_cast<const PointerType*>(Instruction::getType());
490 /// getIndexedType - Returns the type of the element that would be loaded with
491 /// a load instruction with the specified parameters.
493 /// A null type is returned if the indices are invalid for the specified
496 template<typename InputIterator>
497 static const Type *getIndexedType(const Type *Ptr,
498 InputIterator IdxBegin,
499 InputIterator IdxEnd,
500 bool AllowStructLeaf = false) {
501 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
502 typename std::iterator_traits<InputIterator>::
503 iterator_category()));
505 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
507 inline op_iterator idx_begin() { return op_begin()+1; }
508 inline const_op_iterator idx_begin() const { return op_begin()+1; }
509 inline op_iterator idx_end() { return op_end(); }
510 inline const_op_iterator idx_end() const { return op_end(); }
512 Value *getPointerOperand() {
513 return getOperand(0);
515 const Value *getPointerOperand() const {
516 return getOperand(0);
518 static unsigned getPointerOperandIndex() {
519 return 0U; // get index for modifying correct operand
522 unsigned getNumIndices() const { // Note: always non-negative
523 return getNumOperands() - 1;
526 bool hasIndices() const {
527 return getNumOperands() > 1;
530 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
531 /// zeros. If so, the result pointer and the first operand have the same
532 /// value, just potentially different types.
533 bool hasAllZeroIndices() const;
535 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
536 /// constant integers. If so, the result pointer and the first operand have
537 /// a constant offset between them.
538 bool hasAllConstantIndices() const;
541 // Methods for support type inquiry through isa, cast, and dyn_cast:
542 static inline bool classof(const GetElementPtrInst *) { return true; }
543 static inline bool classof(const Instruction *I) {
544 return (I->getOpcode() == Instruction::GetElementPtr);
546 static inline bool classof(const Value *V) {
547 return isa<Instruction>(V) && classof(cast<Instruction>(V));
551 //===----------------------------------------------------------------------===//
553 //===----------------------------------------------------------------------===//
555 /// This instruction compares its operands according to the predicate given
556 /// to the constructor. It only operates on integers, pointers, or packed
557 /// vectors of integrals. The two operands must be the same type.
558 /// @brief Represent an integer comparison operator.
559 class ICmpInst: public CmpInst {
561 /// This enumeration lists the possible predicates for the ICmpInst. The
562 /// values in the range 0-31 are reserved for FCmpInst while values in the
563 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
564 /// predicate values are not overlapping between the classes.
566 ICMP_EQ = 32, ///< equal
567 ICMP_NE = 33, ///< not equal
568 ICMP_UGT = 34, ///< unsigned greater than
569 ICMP_UGE = 35, ///< unsigned greater or equal
570 ICMP_ULT = 36, ///< unsigned less than
571 ICMP_ULE = 37, ///< unsigned less or equal
572 ICMP_SGT = 38, ///< signed greater than
573 ICMP_SGE = 39, ///< signed greater or equal
574 ICMP_SLT = 40, ///< signed less than
575 ICMP_SLE = 41, ///< signed less or equal
576 FIRST_ICMP_PREDICATE = ICMP_EQ,
577 LAST_ICMP_PREDICATE = ICMP_SLE,
578 BAD_ICMP_PREDICATE = ICMP_SLE + 1
581 /// @brief Constructor with insert-before-instruction semantics.
583 Predicate pred, ///< The predicate to use for the comparison
584 Value *LHS, ///< The left-hand-side of the expression
585 Value *RHS, ///< The right-hand-side of the expression
586 const std::string &Name = "", ///< Name of the instruction
587 Instruction *InsertBefore = 0 ///< Where to insert
588 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
591 /// @brief Constructor with insert-at-block-end semantics.
593 Predicate pred, ///< The predicate to use for the comparison
594 Value *LHS, ///< The left-hand-side of the expression
595 Value *RHS, ///< The right-hand-side of the expression
596 const std::string &Name, ///< Name of the instruction
597 BasicBlock *InsertAtEnd ///< Block to insert into.
598 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
601 /// @brief Return the predicate for this instruction.
602 Predicate getPredicate() const { return Predicate(SubclassData); }
604 /// @brief Set the predicate for this instruction to the specified value.
605 void setPredicate(Predicate P) { SubclassData = P; }
607 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
608 /// @returns the inverse predicate for the instruction's current predicate.
609 /// @brief Return the inverse of the instruction's predicate.
610 Predicate getInversePredicate() const {
611 return getInversePredicate(getPredicate());
614 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
615 /// @returns the inverse predicate for predicate provided in \p pred.
616 /// @brief Return the inverse of a given predicate
617 static Predicate getInversePredicate(Predicate pred);
619 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
620 /// @returns the predicate that would be the result of exchanging the two
621 /// operands of the ICmpInst instruction without changing the result
623 /// @brief Return the predicate as if the operands were swapped
624 Predicate getSwappedPredicate() const {
625 return getSwappedPredicate(getPredicate());
628 /// This is a static version that you can use without an instruction
630 /// @brief Return the predicate as if the operands were swapped.
631 static Predicate getSwappedPredicate(Predicate pred);
633 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
634 /// @returns the predicate that would be the result if the operand were
635 /// regarded as signed.
636 /// @brief Return the signed version of the predicate
637 Predicate getSignedPredicate() const {
638 return getSignedPredicate(getPredicate());
641 /// This is a static version that you can use without an instruction.
642 /// @brief Return the signed version of the predicate.
643 static Predicate getSignedPredicate(Predicate pred);
645 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
646 /// @returns the predicate that would be the result if the operand were
647 /// regarded as unsigned.
648 /// @brief Return the unsigned version of the predicate
649 Predicate getUnsignedPredicate() const {
650 return getUnsignedPredicate(getPredicate());
653 /// This is a static version that you can use without an instruction.
654 /// @brief Return the unsigned version of the predicate.
655 static Predicate getUnsignedPredicate(Predicate pred);
657 /// isEquality - Return true if this predicate is either EQ or NE. This also
658 /// tests for commutativity.
659 static bool isEquality(Predicate P) {
660 return P == ICMP_EQ || P == ICMP_NE;
663 /// isEquality - Return true if this predicate is either EQ or NE. This also
664 /// tests for commutativity.
665 bool isEquality() const {
666 return isEquality(getPredicate());
669 /// @returns true if the predicate of this ICmpInst is commutative
670 /// @brief Determine if this relation is commutative.
671 bool isCommutative() const { return isEquality(); }
673 /// isRelational - Return true if the predicate is relational (not EQ or NE).
675 bool isRelational() const {
676 return !isEquality();
679 /// isRelational - Return true if the predicate is relational (not EQ or NE).
681 static bool isRelational(Predicate P) {
682 return !isEquality(P);
685 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
686 /// @brief Determine if this instruction's predicate is signed.
687 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
689 /// @returns true if the predicate provided is signed, false otherwise
690 /// @brief Determine if the predicate is signed.
691 static bool isSignedPredicate(Predicate pred);
693 /// Initialize a set of values that all satisfy the predicate with C.
694 /// @brief Make a ConstantRange for a relation with a constant value.
695 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
697 /// Exchange the two operands to this instruction in such a way that it does
698 /// not modify the semantics of the instruction. The predicate value may be
699 /// changed to retain the same result if the predicate is order dependent
701 /// @brief Swap operands and adjust predicate.
702 void swapOperands() {
703 SubclassData = getSwappedPredicate();
704 std::swap(Ops[0], Ops[1]);
707 virtual ICmpInst *clone() const;
709 // Methods for support type inquiry through isa, cast, and dyn_cast:
710 static inline bool classof(const ICmpInst *) { return true; }
711 static inline bool classof(const Instruction *I) {
712 return I->getOpcode() == Instruction::ICmp;
714 static inline bool classof(const Value *V) {
715 return isa<Instruction>(V) && classof(cast<Instruction>(V));
719 //===----------------------------------------------------------------------===//
721 //===----------------------------------------------------------------------===//
723 /// This instruction compares its operands according to the predicate given
724 /// to the constructor. It only operates on floating point values or packed
725 /// vectors of floating point values. The operands must be identical types.
726 /// @brief Represents a floating point comparison operator.
727 class FCmpInst: public CmpInst {
729 /// This enumeration lists the possible predicates for the FCmpInst. Values
730 /// in the range 0-31 are reserved for FCmpInst.
732 // Opcode U L G E Intuitive operation
733 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
734 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
735 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
736 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
737 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
738 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
739 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
740 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
741 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
742 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
743 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
744 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
745 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
746 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
747 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
748 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
749 FIRST_FCMP_PREDICATE = FCMP_FALSE,
750 LAST_FCMP_PREDICATE = FCMP_TRUE,
751 BAD_FCMP_PREDICATE = FCMP_TRUE + 1
754 /// @brief Constructor with insert-before-instruction semantics.
756 Predicate pred, ///< The predicate to use for the comparison
757 Value *LHS, ///< The left-hand-side of the expression
758 Value *RHS, ///< The right-hand-side of the expression
759 const std::string &Name = "", ///< Name of the instruction
760 Instruction *InsertBefore = 0 ///< Where to insert
761 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
764 /// @brief Constructor with insert-at-block-end semantics.
766 Predicate pred, ///< The predicate to use for the comparison
767 Value *LHS, ///< The left-hand-side of the expression
768 Value *RHS, ///< The right-hand-side of the expression
769 const std::string &Name, ///< Name of the instruction
770 BasicBlock *InsertAtEnd ///< Block to insert into.
771 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
774 /// @brief Return the predicate for this instruction.
775 Predicate getPredicate() const { return Predicate(SubclassData); }
777 /// @brief Set the predicate for this instruction to the specified value.
778 void setPredicate(Predicate P) { SubclassData = P; }
780 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
781 /// @returns the inverse predicate for the instructions current predicate.
782 /// @brief Return the inverse of the predicate
783 Predicate getInversePredicate() const {
784 return getInversePredicate(getPredicate());
787 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
788 /// @returns the inverse predicate for \p pred.
789 /// @brief Return the inverse of a given predicate
790 static Predicate getInversePredicate(Predicate pred);
792 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
793 /// @returns the predicate that would be the result of exchanging the two
794 /// operands of the ICmpInst instruction without changing the result
796 /// @brief Return the predicate as if the operands were swapped
797 Predicate getSwappedPredicate() const {
798 return getSwappedPredicate(getPredicate());
801 /// This is a static version that you can use without an instruction
803 /// @brief Return the predicate as if the operands were swapped.
804 static Predicate getSwappedPredicate(Predicate Opcode);
806 /// This also tests for commutativity. If isEquality() returns true then
807 /// the predicate is also commutative. Only the equality predicates are
809 /// @returns true if the predicate of this instruction is EQ or NE.
810 /// @brief Determine if this is an equality predicate.
811 bool isEquality() const {
812 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
813 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
815 bool isCommutative() const { return isEquality(); }
817 /// @returns true if the predicate is relational (not EQ or NE).
818 /// @brief Determine if this a relational predicate.
819 bool isRelational() const { return !isEquality(); }
821 /// Exchange the two operands to this instruction in such a way that it does
822 /// not modify the semantics of the instruction. The predicate value may be
823 /// changed to retain the same result if the predicate is order dependent
825 /// @brief Swap operands and adjust predicate.
826 void swapOperands() {
827 SubclassData = getSwappedPredicate();
828 std::swap(Ops[0], Ops[1]);
831 virtual FCmpInst *clone() const;
833 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
834 static inline bool classof(const FCmpInst *) { return true; }
835 static inline bool classof(const Instruction *I) {
836 return I->getOpcode() == Instruction::FCmp;
838 static inline bool classof(const Value *V) {
839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
843 //===----------------------------------------------------------------------===//
845 //===----------------------------------------------------------------------===//
846 /// CallInst - This class represents a function call, abstracting a target
847 /// machine's calling convention. This class uses low bit of the SubClassData
848 /// field to indicate whether or not this is a tail call. The rest of the bits
849 /// hold the calling convention of the call.
852 class CallInst : public Instruction {
853 PAListPtr ParamAttrs; ///< parameter attributes for call
854 CallInst(const CallInst &CI);
855 void init(Value *Func, Value* const *Params, unsigned NumParams);
856 void init(Value *Func, Value *Actual1, Value *Actual2);
857 void init(Value *Func, Value *Actual);
858 void init(Value *Func);
860 template<typename InputIterator>
861 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
862 const std::string &Name,
863 // This argument ensures that we have an iterator we can
864 // do arithmetic on in constant time
865 std::random_access_iterator_tag) {
866 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
868 // This requires that the iterator points to contiguous memory.
869 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
874 /// Construct a CallInst given a range of arguments. InputIterator
875 /// must be a random-access iterator pointing to contiguous storage
876 /// (e.g. a std::vector<>::iterator). Checks are made for
877 /// random-accessness but not for contiguous storage as that would
878 /// incur runtime overhead.
879 /// @brief Construct a CallInst from a range of arguments
880 template<typename InputIterator>
881 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
882 const std::string &Name = "", Instruction *InsertBefore = 0)
883 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
884 ->getElementType())->getReturnType(),
885 Instruction::Call, 0, 0, InsertBefore) {
886 init(Func, ArgBegin, ArgEnd, Name,
887 typename std::iterator_traits<InputIterator>::iterator_category());
890 /// Construct a CallInst given a range of arguments. InputIterator
891 /// must be a random-access iterator pointing to contiguous storage
892 /// (e.g. a std::vector<>::iterator). Checks are made for
893 /// random-accessness but not for contiguous storage as that would
894 /// incur runtime overhead.
895 /// @brief Construct a CallInst from a range of arguments
896 template<typename InputIterator>
897 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
898 const std::string &Name, BasicBlock *InsertAtEnd)
899 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
900 ->getElementType())->getReturnType(),
901 Instruction::Call, 0, 0, InsertAtEnd) {
902 init(Func, ArgBegin, ArgEnd, Name,
903 typename std::iterator_traits<InputIterator>::iterator_category());
906 CallInst(Value *F, Value *Actual, const std::string& Name = "",
907 Instruction *InsertBefore = 0);
908 CallInst(Value *F, Value *Actual, const std::string& Name,
909 BasicBlock *InsertAtEnd);
910 explicit CallInst(Value *F, const std::string &Name = "",
911 Instruction *InsertBefore = 0);
912 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
915 virtual CallInst *clone() const;
917 bool isTailCall() const { return SubclassData & 1; }
918 void setTailCall(bool isTailCall = true) {
919 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
922 /// getCallingConv/setCallingConv - Get or set the calling convention of this
924 unsigned getCallingConv() const { return SubclassData >> 1; }
925 void setCallingConv(unsigned CC) {
926 SubclassData = (SubclassData & 1) | (CC << 1);
929 /// getParamAttrs - Return the parameter attributes for this call.
931 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
933 /// setParamAttrs - Sets the parameter attributes for this call.
934 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
936 /// @brief Determine whether the call or the callee has the given attribute.
937 bool paramHasAttr(uint16_t i, unsigned attr) const;
939 /// @brief Extract the alignment for a call or parameter (0=unknown).
940 uint16_t getParamAlignment(uint16_t i) const;
942 /// @brief Determine if the call does not access memory.
943 bool doesNotAccessMemory() const;
945 /// @brief Determine if the call does not access or only reads memory.
946 bool onlyReadsMemory() const;
948 /// @brief Determine if the call cannot return.
949 bool doesNotReturn() const;
951 /// @brief Determine if the call cannot unwind.
952 bool doesNotThrow() const;
953 void setDoesNotThrow(bool doesNotThrow = true);
955 /// @brief Determine if the call returns a structure through first
956 /// pointer argument.
957 bool hasStructRetAttr() const;
959 /// @brief Determine if any call argument is an aggregate passed by value.
960 bool hasByValArgument() const;
962 /// getCalledFunction - Return the function being called by this instruction
963 /// if it is a direct call. If it is a call through a function pointer,
965 Function *getCalledFunction() const {
966 return dyn_cast<Function>(getOperand(0));
969 /// getCalledValue - Get a pointer to the function that is invoked by this
971 const Value *getCalledValue() const { return getOperand(0); }
972 Value *getCalledValue() { return getOperand(0); }
974 // Methods for support type inquiry through isa, cast, and dyn_cast:
975 static inline bool classof(const CallInst *) { return true; }
976 static inline bool classof(const Instruction *I) {
977 return I->getOpcode() == Instruction::Call;
979 static inline bool classof(const Value *V) {
980 return isa<Instruction>(V) && classof(cast<Instruction>(V));
984 //===----------------------------------------------------------------------===//
986 //===----------------------------------------------------------------------===//
988 /// SelectInst - This class represents the LLVM 'select' instruction.
990 class SelectInst : public Instruction {
993 void init(Value *C, Value *S1, Value *S2) {
994 Ops[0].init(C, this);
995 Ops[1].init(S1, this);
996 Ops[2].init(S2, this);
999 SelectInst(const SelectInst &SI)
1000 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
1001 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
1004 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
1005 Instruction *InsertBefore = 0)
1006 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertBefore) {
1010 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1011 BasicBlock *InsertAtEnd)
1012 : Instruction(S1->getType(), Instruction::Select, Ops, 3, InsertAtEnd) {
1017 Value *getCondition() const { return Ops[0]; }
1018 Value *getTrueValue() const { return Ops[1]; }
1019 Value *getFalseValue() const { return Ops[2]; }
1021 /// Transparently provide more efficient getOperand methods.
1022 Value *getOperand(unsigned i) const {
1023 assert(i < 3 && "getOperand() out of range!");
1026 void setOperand(unsigned i, Value *Val) {
1027 assert(i < 3 && "setOperand() out of range!");
1030 unsigned getNumOperands() const { return 3; }
1032 OtherOps getOpcode() const {
1033 return static_cast<OtherOps>(Instruction::getOpcode());
1036 virtual SelectInst *clone() const;
1038 // Methods for support type inquiry through isa, cast, and dyn_cast:
1039 static inline bool classof(const SelectInst *) { return true; }
1040 static inline bool classof(const Instruction *I) {
1041 return I->getOpcode() == Instruction::Select;
1043 static inline bool classof(const Value *V) {
1044 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1048 //===----------------------------------------------------------------------===//
1050 //===----------------------------------------------------------------------===//
1052 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1053 /// an argument of the specified type given a va_list and increments that list
1055 class VAArgInst : public UnaryInstruction {
1056 VAArgInst(const VAArgInst &VAA)
1057 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1059 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1060 Instruction *InsertBefore = 0)
1061 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1064 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1065 BasicBlock *InsertAtEnd)
1066 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1070 virtual VAArgInst *clone() const;
1072 // Methods for support type inquiry through isa, cast, and dyn_cast:
1073 static inline bool classof(const VAArgInst *) { return true; }
1074 static inline bool classof(const Instruction *I) {
1075 return I->getOpcode() == VAArg;
1077 static inline bool classof(const Value *V) {
1078 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1082 //===----------------------------------------------------------------------===//
1083 // ExtractElementInst Class
1084 //===----------------------------------------------------------------------===//
1086 /// ExtractElementInst - This instruction extracts a single (scalar)
1087 /// element from a VectorType value
1089 class ExtractElementInst : public Instruction {
1091 ExtractElementInst(const ExtractElementInst &EE) :
1092 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1093 Ops[0].init(EE.Ops[0], this);
1094 Ops[1].init(EE.Ops[1], this);
1098 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1099 Instruction *InsertBefore = 0);
1100 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1101 Instruction *InsertBefore = 0);
1102 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1103 BasicBlock *InsertAtEnd);
1104 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1105 BasicBlock *InsertAtEnd);
1107 /// isValidOperands - Return true if an extractelement instruction can be
1108 /// formed with the specified operands.
1109 static bool isValidOperands(const Value *Vec, const Value *Idx);
1111 virtual ExtractElementInst *clone() const;
1113 /// Transparently provide more efficient getOperand methods.
1114 Value *getOperand(unsigned i) const {
1115 assert(i < 2 && "getOperand() out of range!");
1118 void setOperand(unsigned i, Value *Val) {
1119 assert(i < 2 && "setOperand() out of range!");
1122 unsigned getNumOperands() const { return 2; }
1124 // Methods for support type inquiry through isa, cast, and dyn_cast:
1125 static inline bool classof(const ExtractElementInst *) { return true; }
1126 static inline bool classof(const Instruction *I) {
1127 return I->getOpcode() == Instruction::ExtractElement;
1129 static inline bool classof(const Value *V) {
1130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1134 //===----------------------------------------------------------------------===//
1135 // InsertElementInst Class
1136 //===----------------------------------------------------------------------===//
1138 /// InsertElementInst - This instruction inserts a single (scalar)
1139 /// element into a VectorType value
1141 class InsertElementInst : public Instruction {
1143 InsertElementInst(const InsertElementInst &IE);
1145 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1146 const std::string &Name = "",Instruction *InsertBefore = 0);
1147 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1148 const std::string &Name = "",Instruction *InsertBefore = 0);
1149 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1150 const std::string &Name, BasicBlock *InsertAtEnd);
1151 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1152 const std::string &Name, BasicBlock *InsertAtEnd);
1154 /// isValidOperands - Return true if an insertelement instruction can be
1155 /// formed with the specified operands.
1156 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1159 virtual InsertElementInst *clone() const;
1161 /// getType - Overload to return most specific vector type.
1163 const VectorType *getType() const {
1164 return reinterpret_cast<const VectorType*>(Instruction::getType());
1167 /// Transparently provide more efficient getOperand methods.
1168 Value *getOperand(unsigned i) const {
1169 assert(i < 3 && "getOperand() out of range!");
1172 void setOperand(unsigned i, Value *Val) {
1173 assert(i < 3 && "setOperand() out of range!");
1176 unsigned getNumOperands() const { return 3; }
1178 // Methods for support type inquiry through isa, cast, and dyn_cast:
1179 static inline bool classof(const InsertElementInst *) { return true; }
1180 static inline bool classof(const Instruction *I) {
1181 return I->getOpcode() == Instruction::InsertElement;
1183 static inline bool classof(const Value *V) {
1184 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1188 //===----------------------------------------------------------------------===//
1189 // ShuffleVectorInst Class
1190 //===----------------------------------------------------------------------===//
1192 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1195 class ShuffleVectorInst : public Instruction {
1197 ShuffleVectorInst(const ShuffleVectorInst &IE);
1199 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1200 const std::string &Name = "", Instruction *InsertBefor = 0);
1201 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1202 const std::string &Name, BasicBlock *InsertAtEnd);
1204 /// isValidOperands - Return true if a shufflevector instruction can be
1205 /// formed with the specified operands.
1206 static bool isValidOperands(const Value *V1, const Value *V2,
1209 virtual ShuffleVectorInst *clone() const;
1211 /// getType - Overload to return most specific vector type.
1213 const VectorType *getType() const {
1214 return reinterpret_cast<const VectorType*>(Instruction::getType());
1217 /// Transparently provide more efficient getOperand methods.
1218 const Value *getOperand(unsigned i) const {
1219 assert(i < 3 && "getOperand() out of range!");
1222 Value *getOperand(unsigned i) {
1223 assert(i < 3 && "getOperand() out of range!");
1226 void setOperand(unsigned i, Value *Val) {
1227 assert(i < 3 && "setOperand() out of range!");
1230 unsigned getNumOperands() const { return 3; }
1232 /// getMaskValue - Return the index from the shuffle mask for the specified
1233 /// output result. This is either -1 if the element is undef or a number less
1234 /// than 2*numelements.
1235 int getMaskValue(unsigned i) const;
1237 // Methods for support type inquiry through isa, cast, and dyn_cast:
1238 static inline bool classof(const ShuffleVectorInst *) { return true; }
1239 static inline bool classof(const Instruction *I) {
1240 return I->getOpcode() == Instruction::ShuffleVector;
1242 static inline bool classof(const Value *V) {
1243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1248 //===----------------------------------------------------------------------===//
1250 //===----------------------------------------------------------------------===//
1252 // PHINode - The PHINode class is used to represent the magical mystical PHI
1253 // node, that can not exist in nature, but can be synthesized in a computer
1254 // scientist's overactive imagination.
1256 class PHINode : public Instruction {
1257 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1258 /// the number actually in use.
1259 unsigned ReservedSpace;
1260 PHINode(const PHINode &PN);
1262 explicit PHINode(const Type *Ty, const std::string &Name = "",
1263 Instruction *InsertBefore = 0)
1264 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1269 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1270 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1277 /// reserveOperandSpace - This method can be used to avoid repeated
1278 /// reallocation of PHI operand lists by reserving space for the correct
1279 /// number of operands before adding them. Unlike normal vector reserves,
1280 /// this method can also be used to trim the operand space.
1281 void reserveOperandSpace(unsigned NumValues) {
1282 resizeOperands(NumValues*2);
1285 virtual PHINode *clone() const;
1287 /// getNumIncomingValues - Return the number of incoming edges
1289 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1291 /// getIncomingValue - Return incoming value number x
1293 Value *getIncomingValue(unsigned i) const {
1294 assert(i*2 < getNumOperands() && "Invalid value number!");
1295 return getOperand(i*2);
1297 void setIncomingValue(unsigned i, Value *V) {
1298 assert(i*2 < getNumOperands() && "Invalid value number!");
1301 unsigned getOperandNumForIncomingValue(unsigned i) {
1305 /// getIncomingBlock - Return incoming basic block number x
1307 BasicBlock *getIncomingBlock(unsigned i) const {
1308 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1310 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1311 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1313 unsigned getOperandNumForIncomingBlock(unsigned i) {
1317 /// addIncoming - Add an incoming value to the end of the PHI list
1319 void addIncoming(Value *V, BasicBlock *BB) {
1320 assert(V && "PHI node got a null value!");
1321 assert(BB && "PHI node got a null basic block!");
1322 assert(getType() == V->getType() &&
1323 "All operands to PHI node must be the same type as the PHI node!");
1324 unsigned OpNo = NumOperands;
1325 if (OpNo+2 > ReservedSpace)
1326 resizeOperands(0); // Get more space!
1327 // Initialize some new operands.
1328 NumOperands = OpNo+2;
1329 OperandList[OpNo].init(V, this);
1330 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1333 /// removeIncomingValue - Remove an incoming value. This is useful if a
1334 /// predecessor basic block is deleted. The value removed is returned.
1336 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1337 /// is true), the PHI node is destroyed and any uses of it are replaced with
1338 /// dummy values. The only time there should be zero incoming values to a PHI
1339 /// node is when the block is dead, so this strategy is sound.
1341 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1343 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1344 int Idx = getBasicBlockIndex(BB);
1345 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1346 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1349 /// getBasicBlockIndex - Return the first index of the specified basic
1350 /// block in the value list for this PHI. Returns -1 if no instance.
1352 int getBasicBlockIndex(const BasicBlock *BB) const {
1353 Use *OL = OperandList;
1354 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1355 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1359 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1360 return getIncomingValue(getBasicBlockIndex(BB));
1363 /// hasConstantValue - If the specified PHI node always merges together the
1364 /// same value, return the value, otherwise return null.
1366 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1368 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1369 static inline bool classof(const PHINode *) { return true; }
1370 static inline bool classof(const Instruction *I) {
1371 return I->getOpcode() == Instruction::PHI;
1373 static inline bool classof(const Value *V) {
1374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1377 void resizeOperands(unsigned NumOperands);
1380 //===----------------------------------------------------------------------===//
1382 //===----------------------------------------------------------------------===//
1384 //===---------------------------------------------------------------------------
1385 /// ReturnInst - Return a value (possibly void), from a function. Execution
1386 /// does not continue in this function any longer.
1388 class ReturnInst : public TerminatorInst {
1390 ReturnInst(const ReturnInst &RI);
1391 void init(Value * const* retVals, unsigned N);
1394 // ReturnInst constructors:
1395 // ReturnInst() - 'ret void' instruction
1396 // ReturnInst( null) - 'ret void' instruction
1397 // ReturnInst(Value* X) - 'ret X' instruction
1398 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1399 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1400 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1401 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1402 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
1403 // ReturnInst(Value* X, N, Inst *) - 'ret X,X+1...X+N-1', insert before I
1404 // ReturnInst(Value* X, N, BB *) - 'ret X,X+1...X+N-1', insert @ end of BB
1406 // NOTE: If the Value* passed is of type void then the constructor behaves as
1407 // if it was passed NULL.
1408 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1409 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1410 ReturnInst(Value * const* retVals, unsigned N);
1411 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore);
1412 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
1413 explicit ReturnInst(BasicBlock *InsertAtEnd);
1414 virtual ~ReturnInst();
1416 virtual ReturnInst *clone() const;
1418 Value *getOperand(unsigned n = 0) const {
1419 if (getNumOperands() > 1)
1420 return TerminatorInst::getOperand(n);
1425 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 PAListPtr 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 /// getParamAttrs - Return the parameter attributes for this invoke.
1745 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
1747 /// setParamAttrs - Set the parameter attributes for this invoke.
1749 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
1751 /// @brief Determine whether the call or the callee has the given attribute.
1752 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
1754 /// @brief Extract the alignment for a call or parameter (0=unknown).
1755 uint16_t getParamAlignment(uint16_t i) const;
1757 /// @brief Determine if the call does not access memory.
1758 bool doesNotAccessMemory() const;
1760 /// @brief Determine if the call does not access or only reads memory.
1761 bool onlyReadsMemory() const;
1763 /// @brief Determine if the call cannot return.
1764 bool doesNotReturn() const;
1766 /// @brief Determine if the call cannot unwind.
1767 bool doesNotThrow() const;
1768 void setDoesNotThrow(bool doesNotThrow = true);
1770 /// @brief Determine if the call returns a structure through first
1771 /// pointer argument.
1772 bool hasStructRetAttr() const;
1774 /// getCalledFunction - Return the function called, or null if this is an
1775 /// indirect function invocation.
1777 Function *getCalledFunction() const {
1778 return dyn_cast<Function>(getOperand(0));
1781 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1782 Value *getCalledValue() const { return getOperand(0); }
1784 // get*Dest - Return the destination basic blocks...
1785 BasicBlock *getNormalDest() const {
1786 return cast<BasicBlock>(getOperand(1));
1788 BasicBlock *getUnwindDest() const {
1789 return cast<BasicBlock>(getOperand(2));
1791 void setNormalDest(BasicBlock *B) {
1792 setOperand(1, reinterpret_cast<Value*>(B));
1795 void setUnwindDest(BasicBlock *B) {
1796 setOperand(2, reinterpret_cast<Value*>(B));
1799 BasicBlock *getSuccessor(unsigned i) const {
1800 assert(i < 2 && "Successor # out of range for invoke!");
1801 return i == 0 ? getNormalDest() : getUnwindDest();
1804 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1805 assert(idx < 2 && "Successor # out of range for invoke!");
1806 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1809 unsigned getNumSuccessors() const { return 2; }
1811 // Methods for support type inquiry through isa, cast, and dyn_cast:
1812 static inline bool classof(const InvokeInst *) { return true; }
1813 static inline bool classof(const Instruction *I) {
1814 return (I->getOpcode() == Instruction::Invoke);
1816 static inline bool classof(const Value *V) {
1817 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1820 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1821 virtual unsigned getNumSuccessorsV() const;
1822 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1826 //===----------------------------------------------------------------------===//
1828 //===----------------------------------------------------------------------===//
1830 //===---------------------------------------------------------------------------
1831 /// UnwindInst - Immediately exit the current function, unwinding the stack
1832 /// until an invoke instruction is found.
1834 class UnwindInst : public TerminatorInst {
1836 explicit UnwindInst(Instruction *InsertBefore = 0);
1837 explicit UnwindInst(BasicBlock *InsertAtEnd);
1839 virtual UnwindInst *clone() const;
1841 unsigned getNumSuccessors() const { return 0; }
1843 // Methods for support type inquiry through isa, cast, and dyn_cast:
1844 static inline bool classof(const UnwindInst *) { return true; }
1845 static inline bool classof(const Instruction *I) {
1846 return I->getOpcode() == Instruction::Unwind;
1848 static inline bool classof(const Value *V) {
1849 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1852 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1853 virtual unsigned getNumSuccessorsV() const;
1854 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1857 //===----------------------------------------------------------------------===//
1858 // UnreachableInst Class
1859 //===----------------------------------------------------------------------===//
1861 //===---------------------------------------------------------------------------
1862 /// UnreachableInst - This function has undefined behavior. In particular, the
1863 /// presence of this instruction indicates some higher level knowledge that the
1864 /// end of the block cannot be reached.
1866 class UnreachableInst : public TerminatorInst {
1868 explicit UnreachableInst(Instruction *InsertBefore = 0);
1869 explicit UnreachableInst(BasicBlock *InsertAtEnd);
1871 virtual UnreachableInst *clone() const;
1873 unsigned getNumSuccessors() const { return 0; }
1875 // Methods for support type inquiry through isa, cast, and dyn_cast:
1876 static inline bool classof(const UnreachableInst *) { return true; }
1877 static inline bool classof(const Instruction *I) {
1878 return I->getOpcode() == Instruction::Unreachable;
1880 static inline bool classof(const Value *V) {
1881 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1884 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1885 virtual unsigned getNumSuccessorsV() const;
1886 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1889 //===----------------------------------------------------------------------===//
1891 //===----------------------------------------------------------------------===//
1893 /// @brief This class represents a truncation of integer types.
1894 class TruncInst : public CastInst {
1895 /// Private copy constructor
1896 TruncInst(const TruncInst &CI)
1897 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1900 /// @brief Constructor with insert-before-instruction semantics
1902 Value *S, ///< The value to be truncated
1903 const Type *Ty, ///< The (smaller) type to truncate to
1904 const std::string &Name = "", ///< A name for the new instruction
1905 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1908 /// @brief Constructor with insert-at-end-of-block semantics
1910 Value *S, ///< The value to be truncated
1911 const Type *Ty, ///< The (smaller) type to truncate to
1912 const std::string &Name, ///< A name for the new instruction
1913 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1916 /// @brief Clone an identical TruncInst
1917 virtual CastInst *clone() const;
1919 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1920 static inline bool classof(const TruncInst *) { return true; }
1921 static inline bool classof(const Instruction *I) {
1922 return I->getOpcode() == Trunc;
1924 static inline bool classof(const Value *V) {
1925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1929 //===----------------------------------------------------------------------===//
1931 //===----------------------------------------------------------------------===//
1933 /// @brief This class represents zero extension of integer types.
1934 class ZExtInst : public CastInst {
1935 /// @brief Private copy constructor
1936 ZExtInst(const ZExtInst &CI)
1937 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1940 /// @brief Constructor with insert-before-instruction semantics
1942 Value *S, ///< The value to be zero extended
1943 const Type *Ty, ///< The type to zero extend to
1944 const std::string &Name = "", ///< A name for the new instruction
1945 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1948 /// @brief Constructor with insert-at-end semantics.
1950 Value *S, ///< The value to be zero extended
1951 const Type *Ty, ///< The type to zero extend to
1952 const std::string &Name, ///< A name for the new instruction
1953 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1956 /// @brief Clone an identical ZExtInst
1957 virtual CastInst *clone() const;
1959 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1960 static inline bool classof(const ZExtInst *) { return true; }
1961 static inline bool classof(const Instruction *I) {
1962 return I->getOpcode() == ZExt;
1964 static inline bool classof(const Value *V) {
1965 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1969 //===----------------------------------------------------------------------===//
1971 //===----------------------------------------------------------------------===//
1973 /// @brief This class represents a sign extension of integer types.
1974 class SExtInst : public CastInst {
1975 /// @brief Private copy constructor
1976 SExtInst(const SExtInst &CI)
1977 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1980 /// @brief Constructor with insert-before-instruction semantics
1982 Value *S, ///< The value to be sign extended
1983 const Type *Ty, ///< The type to sign extend to
1984 const std::string &Name = "", ///< A name for the new instruction
1985 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1988 /// @brief Constructor with insert-at-end-of-block semantics
1990 Value *S, ///< The value to be sign extended
1991 const Type *Ty, ///< The type to sign extend to
1992 const std::string &Name, ///< A name for the new instruction
1993 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
1996 /// @brief Clone an identical SExtInst
1997 virtual CastInst *clone() const;
1999 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2000 static inline bool classof(const SExtInst *) { return true; }
2001 static inline bool classof(const Instruction *I) {
2002 return I->getOpcode() == SExt;
2004 static inline bool classof(const Value *V) {
2005 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2009 //===----------------------------------------------------------------------===//
2010 // FPTruncInst Class
2011 //===----------------------------------------------------------------------===//
2013 /// @brief This class represents a truncation of floating point types.
2014 class FPTruncInst : public CastInst {
2015 FPTruncInst(const FPTruncInst &CI)
2016 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2019 /// @brief Constructor with insert-before-instruction semantics
2021 Value *S, ///< The value to be truncated
2022 const Type *Ty, ///< The type to truncate to
2023 const std::string &Name = "", ///< A name for the new instruction
2024 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2027 /// @brief Constructor with insert-before-instruction semantics
2029 Value *S, ///< The value to be truncated
2030 const Type *Ty, ///< The type to truncate to
2031 const std::string &Name, ///< A name for the new instruction
2032 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2035 /// @brief Clone an identical FPTruncInst
2036 virtual CastInst *clone() const;
2038 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2039 static inline bool classof(const FPTruncInst *) { return true; }
2040 static inline bool classof(const Instruction *I) {
2041 return I->getOpcode() == FPTrunc;
2043 static inline bool classof(const Value *V) {
2044 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2048 //===----------------------------------------------------------------------===//
2050 //===----------------------------------------------------------------------===//
2052 /// @brief This class represents an extension of floating point types.
2053 class FPExtInst : public CastInst {
2054 FPExtInst(const FPExtInst &CI)
2055 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2058 /// @brief Constructor with insert-before-instruction semantics
2060 Value *S, ///< The value to be extended
2061 const Type *Ty, ///< The type to extend to
2062 const std::string &Name = "", ///< A name for the new instruction
2063 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2066 /// @brief Constructor with insert-at-end-of-block semantics
2068 Value *S, ///< The value to be extended
2069 const Type *Ty, ///< The type to extend to
2070 const std::string &Name, ///< A name for the new instruction
2071 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2074 /// @brief Clone an identical FPExtInst
2075 virtual CastInst *clone() const;
2077 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2078 static inline bool classof(const FPExtInst *) { return true; }
2079 static inline bool classof(const Instruction *I) {
2080 return I->getOpcode() == FPExt;
2082 static inline bool classof(const Value *V) {
2083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2087 //===----------------------------------------------------------------------===//
2089 //===----------------------------------------------------------------------===//
2091 /// @brief This class represents a cast unsigned integer to floating point.
2092 class UIToFPInst : public CastInst {
2093 UIToFPInst(const UIToFPInst &CI)
2094 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2097 /// @brief Constructor with insert-before-instruction semantics
2099 Value *S, ///< The value to be converted
2100 const Type *Ty, ///< The type to convert to
2101 const std::string &Name = "", ///< A name for the new instruction
2102 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2105 /// @brief Constructor with insert-at-end-of-block semantics
2107 Value *S, ///< The value to be converted
2108 const Type *Ty, ///< The type to convert to
2109 const std::string &Name, ///< A name for the new instruction
2110 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2113 /// @brief Clone an identical UIToFPInst
2114 virtual CastInst *clone() const;
2116 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2117 static inline bool classof(const UIToFPInst *) { return true; }
2118 static inline bool classof(const Instruction *I) {
2119 return I->getOpcode() == UIToFP;
2121 static inline bool classof(const Value *V) {
2122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2126 //===----------------------------------------------------------------------===//
2128 //===----------------------------------------------------------------------===//
2130 /// @brief This class represents a cast from signed integer to floating point.
2131 class SIToFPInst : public CastInst {
2132 SIToFPInst(const SIToFPInst &CI)
2133 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2136 /// @brief Constructor with insert-before-instruction semantics
2138 Value *S, ///< The value to be converted
2139 const Type *Ty, ///< The type to convert to
2140 const std::string &Name = "", ///< A name for the new instruction
2141 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2144 /// @brief Constructor with insert-at-end-of-block semantics
2146 Value *S, ///< The value to be converted
2147 const Type *Ty, ///< The type to convert to
2148 const std::string &Name, ///< A name for the new instruction
2149 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2152 /// @brief Clone an identical SIToFPInst
2153 virtual CastInst *clone() const;
2155 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2156 static inline bool classof(const SIToFPInst *) { return true; }
2157 static inline bool classof(const Instruction *I) {
2158 return I->getOpcode() == SIToFP;
2160 static inline bool classof(const Value *V) {
2161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2165 //===----------------------------------------------------------------------===//
2167 //===----------------------------------------------------------------------===//
2169 /// @brief This class represents a cast from floating point to unsigned integer
2170 class FPToUIInst : public CastInst {
2171 FPToUIInst(const FPToUIInst &CI)
2172 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2175 /// @brief Constructor with insert-before-instruction semantics
2177 Value *S, ///< The value to be converted
2178 const Type *Ty, ///< The type to convert to
2179 const std::string &Name = "", ///< A name for the new instruction
2180 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2183 /// @brief Constructor with insert-at-end-of-block semantics
2185 Value *S, ///< The value to be converted
2186 const Type *Ty, ///< The type to convert to
2187 const std::string &Name, ///< A name for the new instruction
2188 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2191 /// @brief Clone an identical FPToUIInst
2192 virtual CastInst *clone() const;
2194 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2195 static inline bool classof(const FPToUIInst *) { return true; }
2196 static inline bool classof(const Instruction *I) {
2197 return I->getOpcode() == FPToUI;
2199 static inline bool classof(const Value *V) {
2200 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2204 //===----------------------------------------------------------------------===//
2206 //===----------------------------------------------------------------------===//
2208 /// @brief This class represents a cast from floating point to signed integer.
2209 class FPToSIInst : public CastInst {
2210 FPToSIInst(const FPToSIInst &CI)
2211 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2214 /// @brief Constructor with insert-before-instruction semantics
2216 Value *S, ///< The value to be converted
2217 const Type *Ty, ///< The type to convert to
2218 const std::string &Name = "", ///< A name for the new instruction
2219 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2222 /// @brief Constructor with insert-at-end-of-block semantics
2224 Value *S, ///< The value to be converted
2225 const Type *Ty, ///< The type to convert to
2226 const std::string &Name, ///< A name for the new instruction
2227 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2230 /// @brief Clone an identical FPToSIInst
2231 virtual CastInst *clone() const;
2233 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2234 static inline bool classof(const FPToSIInst *) { return true; }
2235 static inline bool classof(const Instruction *I) {
2236 return I->getOpcode() == FPToSI;
2238 static inline bool classof(const Value *V) {
2239 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2243 //===----------------------------------------------------------------------===//
2244 // IntToPtrInst Class
2245 //===----------------------------------------------------------------------===//
2247 /// @brief This class represents a cast from an integer to a pointer.
2248 class IntToPtrInst : public CastInst {
2249 IntToPtrInst(const IntToPtrInst &CI)
2250 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2253 /// @brief Constructor with insert-before-instruction semantics
2255 Value *S, ///< The value to be converted
2256 const Type *Ty, ///< The type to convert to
2257 const std::string &Name = "", ///< A name for the new instruction
2258 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2261 /// @brief Constructor with insert-at-end-of-block semantics
2263 Value *S, ///< The value to be converted
2264 const Type *Ty, ///< The type to convert to
2265 const std::string &Name, ///< A name for the new instruction
2266 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2269 /// @brief Clone an identical IntToPtrInst
2270 virtual CastInst *clone() const;
2272 // Methods for support type inquiry through isa, cast, and dyn_cast:
2273 static inline bool classof(const IntToPtrInst *) { return true; }
2274 static inline bool classof(const Instruction *I) {
2275 return I->getOpcode() == IntToPtr;
2277 static inline bool classof(const Value *V) {
2278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2282 //===----------------------------------------------------------------------===//
2283 // PtrToIntInst Class
2284 //===----------------------------------------------------------------------===//
2286 /// @brief This class represents a cast from a pointer to an integer
2287 class PtrToIntInst : public CastInst {
2288 PtrToIntInst(const PtrToIntInst &CI)
2289 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2292 /// @brief Constructor with insert-before-instruction semantics
2294 Value *S, ///< The value to be converted
2295 const Type *Ty, ///< The type to convert to
2296 const std::string &Name = "", ///< A name for the new instruction
2297 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2300 /// @brief Constructor with insert-at-end-of-block semantics
2302 Value *S, ///< The value to be converted
2303 const Type *Ty, ///< The type to convert to
2304 const std::string &Name, ///< A name for the new instruction
2305 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2308 /// @brief Clone an identical PtrToIntInst
2309 virtual CastInst *clone() const;
2311 // Methods for support type inquiry through isa, cast, and dyn_cast:
2312 static inline bool classof(const PtrToIntInst *) { return true; }
2313 static inline bool classof(const Instruction *I) {
2314 return I->getOpcode() == PtrToInt;
2316 static inline bool classof(const Value *V) {
2317 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2321 //===----------------------------------------------------------------------===//
2322 // BitCastInst Class
2323 //===----------------------------------------------------------------------===//
2325 /// @brief This class represents a no-op cast from one type to another.
2326 class BitCastInst : public CastInst {
2327 BitCastInst(const BitCastInst &CI)
2328 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2331 /// @brief Constructor with insert-before-instruction semantics
2333 Value *S, ///< The value to be casted
2334 const Type *Ty, ///< The type to casted to
2335 const std::string &Name = "", ///< A name for the new instruction
2336 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2339 /// @brief Constructor with insert-at-end-of-block semantics
2341 Value *S, ///< The value to be casted
2342 const Type *Ty, ///< The type to casted to
2343 const std::string &Name, ///< A name for the new instruction
2344 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2347 /// @brief Clone an identical BitCastInst
2348 virtual CastInst *clone() const;
2350 // Methods for support type inquiry through isa, cast, and dyn_cast:
2351 static inline bool classof(const BitCastInst *) { return true; }
2352 static inline bool classof(const Instruction *I) {
2353 return I->getOpcode() == BitCast;
2355 static inline bool classof(const Value *V) {
2356 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2360 //===----------------------------------------------------------------------===//
2361 // GetResultInst Class
2362 //===----------------------------------------------------------------------===//
2364 /// GetResultInst - This instruction extracts individual result value from
2365 /// aggregate value, where aggregate value is returned by CallInst.
2367 class GetResultInst : public Instruction {
2370 GetResultInst(const GetResultInst &GRI) :
2371 Instruction(GRI.getType(), Instruction::GetResult, &Aggr, 1) {
2372 Aggr.init(GRI.Aggr, this);
2377 explicit GetResultInst(Value *Aggr, unsigned index,
2378 const std::string &Name = "",
2379 Instruction *InsertBefore = 0);
2381 /// isValidOperands - Return true if an getresult instruction can be
2382 /// formed with the specified operands.
2383 static bool isValidOperands(const Value *Aggr, unsigned index);
2385 virtual GetResultInst *clone() const;
2387 Value *getAggregateValue() {
2388 return getOperand(0);
2391 const Value *getAggregateValue() const {
2392 return getOperand(0);
2395 unsigned getIndex() const {
2399 unsigned getNumOperands() const { return 1; }
2401 // Methods for support type inquiry through isa, cast, and dyn_cast:
2402 static inline bool classof(const GetResultInst *) { return true; }
2403 static inline bool classof(const Instruction *I) {
2404 return (I->getOpcode() == Instruction::GetResult);
2406 static inline bool classof(const Value *V) {
2407 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2411 } // End llvm namespace