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
19 #include "llvm/InstrTypes.h"
28 //===----------------------------------------------------------------------===//
29 // AllocationInst Class
30 //===----------------------------------------------------------------------===//
32 /// AllocationInst - This class is the common base class of MallocInst and
35 class AllocationInst : public UnaryInstruction {
38 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
39 const std::string &Name = "", Instruction *InsertBefore = 0);
40 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
41 const std::string &Name, BasicBlock *InsertAtEnd);
43 // Out of line virtual method, so the vtable, etc has a home.
44 virtual ~AllocationInst();
46 /// isArrayAllocation - Return true if there is an allocation size parameter
47 /// to the allocation instruction that is not 1.
49 bool isArrayAllocation() const;
51 /// getArraySize - Get the number of element allocated, for a simple
52 /// allocation of a single element, this will return a constant 1 value.
54 inline const Value *getArraySize() const { return getOperand(0); }
55 inline Value *getArraySize() { return getOperand(0); }
57 /// getType - Overload to return most specific pointer type
59 inline const PointerType *getType() const {
60 return reinterpret_cast<const PointerType*>(Instruction::getType());
63 /// getAllocatedType - Return the type that is being allocated by the
66 const Type *getAllocatedType() const;
68 /// getAlignment - Return the alignment of the memory that is being allocated
69 /// by the instruction.
71 unsigned getAlignment() const { return Alignment; }
72 void setAlignment(unsigned Align) {
73 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
77 virtual Instruction *clone() const = 0;
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 /// MallocInst - an instruction to allocated memory on the heap
97 class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101 const std::string &Name = "",
102 Instruction *InsertBefore = 0)
103 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
105 BasicBlock *InsertAtEnd)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
108 MallocInst(const Type *Ty, const std::string &Name,
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
114 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
115 const std::string &Name, BasicBlock *InsertAtEnd)
116 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &Name = "",
119 Instruction *InsertBefore = 0)
120 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
122 virtual MallocInst *clone() const;
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const MallocInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Malloc);
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 //===----------------------------------------------------------------------===//
137 //===----------------------------------------------------------------------===//
139 /// AllocaInst - an instruction to allocate memory on the stack
141 class AllocaInst : public AllocationInst {
142 AllocaInst(const AllocaInst &);
144 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
145 const std::string &Name = "",
146 Instruction *InsertBefore = 0)
147 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
148 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
149 BasicBlock *InsertAtEnd)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
152 AllocaInst(const Type *Ty, const std::string &Name,
153 Instruction *InsertBefore = 0)
154 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
155 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
158 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
159 const std::string &Name = "", Instruction *InsertBefore = 0)
160 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
161 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
162 const std::string &Name, BasicBlock *InsertAtEnd)
163 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
165 virtual AllocaInst *clone() const;
167 // Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const AllocaInst *) { return true; }
169 static inline bool classof(const Instruction *I) {
170 return (I->getOpcode() == Instruction::Alloca);
172 static inline bool classof(const Value *V) {
173 return isa<Instruction>(V) && classof(cast<Instruction>(V));
178 //===----------------------------------------------------------------------===//
180 //===----------------------------------------------------------------------===//
182 /// FreeInst - an instruction to deallocate memory
184 class FreeInst : public UnaryInstruction {
187 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
188 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
190 virtual FreeInst *clone() const;
192 virtual bool mayWriteToMemory() const { return true; }
194 // Methods for support type inquiry through isa, cast, and dyn_cast:
195 static inline bool classof(const FreeInst *) { return true; }
196 static inline bool classof(const Instruction *I) {
197 return (I->getOpcode() == Instruction::Free);
199 static inline bool classof(const Value *V) {
200 return isa<Instruction>(V) && classof(cast<Instruction>(V));
205 //===----------------------------------------------------------------------===//
207 //===----------------------------------------------------------------------===//
209 /// LoadInst - an instruction for reading from memory. This uses the
210 /// SubclassData field in Value to store whether or not the load is volatile.
212 class LoadInst : public UnaryInstruction {
213 LoadInst(const LoadInst &LI)
214 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
215 setVolatile(LI.isVolatile());
223 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
224 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
225 explicit LoadInst(Value *Ptr, const std::string &Name = "",
226 bool isVolatile = false, Instruction *InsertBefore = 0);
227 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
228 BasicBlock *InsertAtEnd);
230 /// isVolatile - Return true if this is a load from a volatile memory
233 bool isVolatile() const { return SubclassData; }
235 /// setVolatile - Specify whether this is a volatile load or not.
237 void setVolatile(bool V) { SubclassData = V; }
239 virtual LoadInst *clone() const;
241 virtual bool mayWriteToMemory() const { return isVolatile(); }
243 Value *getPointerOperand() { return getOperand(0); }
244 const Value *getPointerOperand() const { return getOperand(0); }
245 static unsigned getPointerOperandIndex() { return 0U; }
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const LoadInst *) { return true; }
249 static inline bool classof(const Instruction *I) {
250 return I->getOpcode() == Instruction::Load;
252 static inline bool classof(const Value *V) {
253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 //===----------------------------------------------------------------------===//
260 //===----------------------------------------------------------------------===//
262 /// StoreInst - an instruction for storing to memory
264 class StoreInst : public Instruction {
266 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
267 Ops[0].init(SI.Ops[0], this);
268 Ops[1].init(SI.Ops[1], this);
269 setVolatile(SI.isVolatile());
276 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
277 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
283 /// isVolatile - Return true if this is a load from a volatile memory
286 bool isVolatile() const { return SubclassData; }
288 /// setVolatile - Specify whether this is a volatile load or not.
290 void setVolatile(bool V) { SubclassData = V; }
292 /// Transparently provide more efficient getOperand methods.
293 Value *getOperand(unsigned i) const {
294 assert(i < 2 && "getOperand() out of range!");
297 void setOperand(unsigned i, Value *Val) {
298 assert(i < 2 && "setOperand() out of range!");
301 unsigned getNumOperands() const { return 2; }
304 virtual StoreInst *clone() const;
306 virtual bool mayWriteToMemory() const { return true; }
308 Value *getPointerOperand() { return getOperand(1); }
309 const Value *getPointerOperand() const { return getOperand(1); }
310 static unsigned getPointerOperandIndex() { return 1U; }
312 // Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const StoreInst *) { return true; }
314 static inline bool classof(const Instruction *I) {
315 return I->getOpcode() == Instruction::Store;
317 static inline bool classof(const Value *V) {
318 return isa<Instruction>(V) && classof(cast<Instruction>(V));
323 //===----------------------------------------------------------------------===//
324 // GetElementPtrInst Class
325 //===----------------------------------------------------------------------===//
327 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
328 /// access elements of arrays and structs
330 class GetElementPtrInst : public Instruction {
331 GetElementPtrInst(const GetElementPtrInst &GEPI)
332 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
333 0, GEPI.getNumOperands()) {
334 Use *OL = OperandList = new Use[NumOperands];
335 Use *GEPIOL = GEPI.OperandList;
336 for (unsigned i = 0, E = NumOperands; i != E; ++i)
337 OL[i].init(GEPIOL[i], this);
339 void init(Value *Ptr, const std::vector<Value*> &Idx);
340 void init(Value *Ptr, Value *Idx0, Value *Idx1);
341 void init(Value *Ptr, Value *Idx);
343 /// Constructors - Create a getelementptr instruction with a base pointer an
344 /// list of indices. The first ctor can optionally insert before an existing
345 /// instruction, the second appends the new instruction to the specified
347 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
348 const std::string &Name = "", Instruction *InsertBefore =0);
349 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
350 const std::string &Name, BasicBlock *InsertAtEnd);
352 /// Constructors - These two constructors are convenience methods because one
353 /// and two index getelementptr instructions are so common.
354 GetElementPtrInst(Value *Ptr, Value *Idx,
355 const std::string &Name = "", Instruction *InsertBefore =0);
356 GetElementPtrInst(Value *Ptr, Value *Idx,
357 const std::string &Name, BasicBlock *InsertAtEnd);
358 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
359 const std::string &Name = "", Instruction *InsertBefore =0);
360 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
361 const std::string &Name, BasicBlock *InsertAtEnd);
362 ~GetElementPtrInst();
364 virtual GetElementPtrInst *clone() const;
366 // getType - Overload to return most specific pointer type...
367 inline const PointerType *getType() const {
368 return reinterpret_cast<const PointerType*>(Instruction::getType());
371 /// getIndexedType - Returns the type of the element that would be loaded with
372 /// a load instruction with the specified parameters.
374 /// A null type is returned if the indices are invalid for the specified
377 static const Type *getIndexedType(const Type *Ptr,
378 const std::vector<Value*> &Indices,
379 bool AllowStructLeaf = false);
380 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
381 bool AllowStructLeaf = false);
382 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
384 inline op_iterator idx_begin() { return op_begin()+1; }
385 inline const_op_iterator idx_begin() const { return op_begin()+1; }
386 inline op_iterator idx_end() { return op_end(); }
387 inline const_op_iterator idx_end() const { return op_end(); }
389 Value *getPointerOperand() {
390 return getOperand(0);
392 const Value *getPointerOperand() const {
393 return getOperand(0);
395 static unsigned getPointerOperandIndex() {
396 return 0U; // get index for modifying correct operand
399 inline unsigned getNumIndices() const { // Note: always non-negative
400 return getNumOperands() - 1;
403 inline bool hasIndices() const {
404 return getNumOperands() > 1;
407 // Methods for support type inquiry through isa, cast, and dyn_cast:
408 static inline bool classof(const GetElementPtrInst *) { return true; }
409 static inline bool classof(const Instruction *I) {
410 return (I->getOpcode() == Instruction::GetElementPtr);
412 static inline bool classof(const Value *V) {
413 return isa<Instruction>(V) && classof(cast<Instruction>(V));
417 //===----------------------------------------------------------------------===//
419 //===----------------------------------------------------------------------===//
421 /// This instruction compares its operands according to the predicate given
422 /// to the constructor. It only operates on integers, pointers, or packed
423 /// vectors of integrals. The two operands must be the same type.
424 /// @brief Represent an integer comparison operator.
425 class ICmpInst: public CmpInst {
427 /// This enumeration lists the possible predicates for the ICmpInst. The
428 /// values in the range 0-31 are reserved for FCmpInst while values in the
429 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
430 /// predicate values are not overlapping between the classes.
432 ICMP_EQ = 32, ///< equal
433 ICMP_NE = 33, ///< not equal
434 ICMP_UGT = 34, ///< unsigned greater than
435 ICMP_UGE = 35, ///< unsigned greater or equal
436 ICMP_ULT = 36, ///< unsigned less than
437 ICMP_ULE = 37, ///< unsigned less or equal
438 ICMP_SGT = 38, ///< signed greater than
439 ICMP_SGE = 39, ///< signed greater or equal
440 ICMP_SLT = 40, ///< signed less than
441 ICMP_SLE = 41, ///< signed less or equal
442 FIRST_ICMP_PREDICATE = ICMP_EQ,
443 LAST_ICMP_PREDICATE = ICMP_SLE
446 /// @brief Constructor with insert-before-instruction semantics.
448 Predicate pred, ///< The predicate to use for the comparison
449 Value *LHS, ///< The left-hand-side of the expression
450 Value *RHS, ///< The right-hand-side of the expression
451 const std::string &Name = "", ///< Name of the instruction
452 Instruction *InsertBefore = 0 ///< Where to insert
453 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
456 /// @brief Constructor with insert-at-block-end semantics.
458 Predicate pred, ///< The predicate to use for the comparison
459 Value *LHS, ///< The left-hand-side of the expression
460 Value *RHS, ///< The right-hand-side of the expression
461 const std::string &Name, ///< Name of the instruction
462 BasicBlock *InsertAtEnd ///< Block to insert into.
463 ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
466 /// @brief Return the predicate for this instruction.
467 Predicate getPredicate() const { return Predicate(SubclassData); }
469 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
470 /// @returns the inverse predicate for the instruction's current predicate.
471 /// @brief Return the inverse of the instruction's predicate.
472 Predicate getInversePredicate() const {
473 return getInversePredicate(getPredicate());
476 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
477 /// @returns the inverse predicate for predicate provided in \p pred.
478 /// @brief Return the inverse of a given predicate
479 static Predicate getInversePredicate(Predicate pred);
481 /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
482 /// @returns the predicate that would be the result of exchanging the two
483 /// operands of the ICmpInst instruction without changing the result
485 /// @brief Return the predicate as if the operands were swapped
486 Predicate getSwappedPredicate() const {
487 return getSwappedPredicate(getPredicate());
490 /// This is a static version that you can use without an instruction
492 /// @brief Return the predicate as if the operands were swapped.
493 static Predicate getSwappedPredicate(Predicate Opcode);
495 /// This also tests for commutativity. If isEquality() returns true then
496 /// the predicate is also commutative. Only the equality predicates are
498 /// @returns true if the predicate of this instruction is EQ or NE.
499 /// @brief Determine if this is an equality predicate.
500 bool isEquality() const {
501 return SubclassData == ICMP_EQ || SubclassData == ICMP_NE;
503 bool isCommutative() const { return isEquality(); }
505 /// @returns true if the predicate is relational (not EQ or NE).
506 /// @brief Determine if this a relational predicate.
507 bool isRelational() const {
508 return !isEquality();
511 /// Exchange the two operands to this instruction in such a way that it does
512 /// not modify the semantics of the instruction. The predicate value may be
513 /// changed to retain the same result if the predicate is order dependent
515 /// @brief Swap operands and adjust predicate.
516 void swapOperands() {
517 SubclassData = getSwappedPredicate();
518 std::swap(Ops[0], Ops[1]);
521 // Methods for support type inquiry through isa, cast, and dyn_cast:
522 static inline bool classof(const ICmpInst *) { return true; }
523 static inline bool classof(const Instruction *I) {
524 return I->getOpcode() == Instruction::ICmp;
526 static inline bool classof(const Value *V) {
527 return isa<Instruction>(V) && classof(cast<Instruction>(V));
531 //===----------------------------------------------------------------------===//
533 //===----------------------------------------------------------------------===//
535 /// This instruction compares its operands according to the predicate given
536 /// to the constructor. It only operates on floating point values or packed
537 /// vectors of floating point values. The operands must be identical types.
538 /// @brief Represents a floating point comparison operator.
539 class FCmpInst: public CmpInst {
541 /// This enumeration lists the possible predicates for the FCmpInst. Values
542 /// in the range 0-31 are reserved for FCmpInst.
544 // Opcode U L G E Intuitive operation
545 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
546 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
547 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
548 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
549 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
550 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
551 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
552 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
553 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
554 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
555 FCMP_UGT =10, ///< 1 0 1 0 True if unordered or greater than
556 FCMP_UGE =11, ///< 1 0 1 1 True if unordered, greater than, or equal
557 FCMP_ULT =12, ///< 1 1 0 0 True if unordered or less than
558 FCMP_ULE =13, ///< 1 1 0 1 True if unordered, less than, or equal
559 FCMP_UNE =14, ///< 1 1 1 0 True if unordered or not equal
560 FCMP_TRUE =15, ///< 1 1 1 1 Always true (always folded)
561 FIRST_FCMP_PREDICATE = FCMP_FALSE,
562 LAST_FCMP_PREDICATE = FCMP_TRUE
565 /// @brief Constructor with insert-before-instruction semantics.
567 Predicate pred, ///< The predicate to use for the comparison
568 Value *LHS, ///< The left-hand-side of the expression
569 Value *RHS, ///< The right-hand-side of the expression
570 const std::string &Name = "", ///< Name of the instruction
571 Instruction *InsertBefore = 0 ///< Where to insert
572 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
575 /// @brief Constructor with insert-at-block-end semantics.
577 Predicate pred, ///< The predicate to use for the comparison
578 Value *LHS, ///< The left-hand-side of the expression
579 Value *RHS, ///< The right-hand-side of the expression
580 const std::string &Name, ///< Name of the instruction
581 BasicBlock *InsertAtEnd ///< Block to insert into.
582 ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
585 /// @brief Return the predicate for this instruction.
586 Predicate getPredicate() const { return Predicate(SubclassData); }
588 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
589 /// @returns the inverse predicate for the instructions current predicate.
590 /// @brief Return the inverse of the predicate
591 Predicate getInversePredicate() const {
592 return getInversePredicate(getPredicate());
595 /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
596 /// @returns the inverse predicate for \p pred.
597 /// @brief Return the inverse of a given predicate
598 static Predicate getInversePredicate(Predicate pred);
600 /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
601 /// @returns the predicate that would be the result of exchanging the two
602 /// operands of the ICmpInst instruction without changing the result
604 /// @brief Return the predicate as if the operands were swapped
605 Predicate getSwappedPredicate() const {
606 return getSwappedPredicate(getPredicate());
609 /// This is a static version that you can use without an instruction
611 /// @brief Return the predicate as if the operands were swapped.
612 static Predicate getSwappedPredicate(Predicate Opcode);
614 /// This also tests for commutativity. If isEquality() returns true then
615 /// the predicate is also commutative. Only the equality predicates are
617 /// @returns true if the predicate of this instruction is EQ or NE.
618 /// @brief Determine if this is an equality predicate.
619 bool isEquality() const {
620 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
621 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
623 bool isCommutative() const { return isEquality(); }
625 /// @returns true if the predicate is relational (not EQ or NE).
626 /// @brief Determine if this a relational predicate.
627 bool isRelational() const { return !isEquality(); }
629 /// Exchange the two operands to this instruction in such a way that it does
630 /// not modify the semantics of the instruction. The predicate value may be
631 /// changed to retain the same result if the predicate is order dependent
633 /// @brief Swap operands and adjust predicate.
634 void swapOperands() {
635 SubclassData = getSwappedPredicate();
636 std::swap(Ops[0], Ops[1]);
639 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
640 static inline bool classof(const FCmpInst *) { return true; }
641 static inline bool classof(const Instruction *I) {
642 return I->getOpcode() == Instruction::FCmp;
644 static inline bool classof(const Value *V) {
645 return isa<Instruction>(V) && classof(cast<Instruction>(V));
650 //===----------------------------------------------------------------------===//
652 //===----------------------------------------------------------------------===//
654 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
657 class SetCondInst : public BinaryOperator {
659 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
660 const std::string &Name = "", Instruction *InsertBefore = 0);
661 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
662 const std::string &Name, BasicBlock *InsertAtEnd);
664 /// getInverseCondition - Return the inverse of the current condition opcode.
665 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
667 BinaryOps getInverseCondition() const {
668 return getInverseCondition(getOpcode());
671 /// getInverseCondition - Static version that you can use without an
672 /// instruction available.
674 static BinaryOps getInverseCondition(BinaryOps Opcode);
676 /// getSwappedCondition - Return the condition opcode that would be the result
677 /// of exchanging the two operands of the setcc instruction without changing
678 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
680 BinaryOps getSwappedCondition() const {
681 return getSwappedCondition(getOpcode());
684 /// getSwappedCondition - Static version that you can use without an
685 /// instruction available.
687 static BinaryOps getSwappedCondition(BinaryOps Opcode);
689 /// isEquality - Return true if this comparison is an ==/!= comparison.
691 bool isEquality() const {
692 return getOpcode() == SetEQ || getOpcode() == SetNE;
695 /// isRelational - Return true if this comparison is a </>/<=/>= comparison.
697 bool isRelational() const {
698 return !isEquality();
701 // Methods for support type inquiry through isa, cast, and dyn_cast:
702 static inline bool classof(const SetCondInst *) { return true; }
703 static inline bool classof(const Instruction *I) {
704 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
705 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
706 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
708 static inline bool classof(const Value *V) {
709 return isa<Instruction>(V) && classof(cast<Instruction>(V));
713 //===----------------------------------------------------------------------===//
715 //===----------------------------------------------------------------------===//
717 /// CastInst - This class represents a cast from Operand[0] to the type of
718 /// the instruction (i->getType()).
720 class CastInst : public UnaryInstruction {
721 CastInst(const CastInst &CI)
722 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
725 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
726 Instruction *InsertBefore = 0)
727 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
729 CastInst(Value *S, const Type *Ty, const std::string &Name,
730 BasicBlock *InsertAtEnd)
731 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
734 /// isTruncIntCast - Return true if this is a truncating integer cast
735 /// instruction, e.g. a cast from long to uint.
736 bool isTruncIntCast() const;
739 virtual CastInst *clone() const;
741 // Methods for support type inquiry through isa, cast, and dyn_cast:
742 static inline bool classof(const CastInst *) { return true; }
743 static inline bool classof(const Instruction *I) {
744 return I->getOpcode() == Cast;
746 static inline bool classof(const Value *V) {
747 return isa<Instruction>(V) && classof(cast<Instruction>(V));
752 //===----------------------------------------------------------------------===//
754 //===----------------------------------------------------------------------===//
756 /// CallInst - This class represents a function call, abstracting a target
757 /// machine's calling convention. This class uses low bit of the SubClassData
758 /// field to indicate whether or not this is a tail call. The rest of the bits
759 /// hold the calling convention of the call.
761 class CallInst : public Instruction {
762 CallInst(const CallInst &CI);
763 void init(Value *Func, const std::vector<Value*> &Params);
764 void init(Value *Func, Value *Actual1, Value *Actual2);
765 void init(Value *Func, Value *Actual);
766 void init(Value *Func);
769 CallInst(Value *F, const std::vector<Value*> &Par,
770 const std::string &Name = "", Instruction *InsertBefore = 0);
771 CallInst(Value *F, const std::vector<Value*> &Par,
772 const std::string &Name, BasicBlock *InsertAtEnd);
774 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
775 // actuals, respectively.
776 CallInst(Value *F, Value *Actual1, Value *Actual2,
777 const std::string& Name = "", Instruction *InsertBefore = 0);
778 CallInst(Value *F, Value *Actual1, Value *Actual2,
779 const std::string& Name, BasicBlock *InsertAtEnd);
780 CallInst(Value *F, Value *Actual, const std::string& Name = "",
781 Instruction *InsertBefore = 0);
782 CallInst(Value *F, Value *Actual, const std::string& Name,
783 BasicBlock *InsertAtEnd);
784 explicit CallInst(Value *F, const std::string &Name = "",
785 Instruction *InsertBefore = 0);
786 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
789 virtual CallInst *clone() const;
790 bool mayWriteToMemory() const { return true; }
792 bool isTailCall() const { return SubclassData & 1; }
793 void setTailCall(bool isTailCall = true) {
794 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
797 /// getCallingConv/setCallingConv - Get or set the calling convention of this
799 unsigned getCallingConv() const { return SubclassData >> 1; }
800 void setCallingConv(unsigned CC) {
801 SubclassData = (SubclassData & 1) | (CC << 1);
804 /// getCalledFunction - Return the function being called by this instruction
805 /// if it is a direct call. If it is a call through a function pointer,
807 Function *getCalledFunction() const {
808 return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
811 // getCalledValue - Get a pointer to a method that is invoked by this inst.
812 inline const Value *getCalledValue() const { return getOperand(0); }
813 inline Value *getCalledValue() { return getOperand(0); }
815 // Methods for support type inquiry through isa, cast, and dyn_cast:
816 static inline bool classof(const CallInst *) { return true; }
817 static inline bool classof(const Instruction *I) {
818 return I->getOpcode() == Instruction::Call;
820 static inline bool classof(const Value *V) {
821 return isa<Instruction>(V) && classof(cast<Instruction>(V));
826 //===----------------------------------------------------------------------===//
828 //===----------------------------------------------------------------------===//
830 /// ShiftInst - This class represents left and right shift instructions.
832 class ShiftInst : public Instruction {
834 ShiftInst(const ShiftInst &SI)
835 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
836 Ops[0].init(SI.Ops[0], this);
837 Ops[1].init(SI.Ops[1], this);
839 void init(OtherOps Opcode, Value *S, Value *SA) {
840 assert((Opcode == Shl || Opcode == LShr || Opcode == AShr) &&
841 "ShiftInst Opcode invalid!");
842 Ops[0].init(S, this);
843 Ops[1].init(SA, this);
847 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
848 Instruction *InsertBefore = 0)
849 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
852 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
853 BasicBlock *InsertAtEnd)
854 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
858 OtherOps getOpcode() const {
859 return static_cast<OtherOps>(Instruction::getOpcode());
862 /// Transparently provide more efficient getOperand methods.
863 Value *getOperand(unsigned i) const {
864 assert(i < 2 && "getOperand() out of range!");
867 void setOperand(unsigned i, Value *Val) {
868 assert(i < 2 && "setOperand() out of range!");
871 unsigned getNumOperands() const { return 2; }
873 /// isLogicalShift - Return true if this is a logical shift left or a logical
875 bool isLogicalShift() const {
876 unsigned opcode = getOpcode();
877 return opcode == Instruction::Shl || opcode == Instruction::LShr;
881 /// isArithmeticShift - Return true if this is a sign-extending shift right
883 bool isArithmeticShift() const {
884 return !isLogicalShift();
888 virtual ShiftInst *clone() const;
890 // Methods for support type inquiry through isa, cast, and dyn_cast:
891 static inline bool classof(const ShiftInst *) { return true; }
892 static inline bool classof(const Instruction *I) {
893 return (I->getOpcode() == Instruction::LShr) |
894 (I->getOpcode() == Instruction::AShr) |
895 (I->getOpcode() == Instruction::Shl);
897 static inline bool classof(const Value *V) {
898 return isa<Instruction>(V) && classof(cast<Instruction>(V));
902 //===----------------------------------------------------------------------===//
904 //===----------------------------------------------------------------------===//
906 /// SelectInst - This class represents the LLVM 'select' instruction.
908 class SelectInst : public Instruction {
911 void init(Value *C, Value *S1, Value *S2) {
912 Ops[0].init(C, this);
913 Ops[1].init(S1, this);
914 Ops[2].init(S2, this);
917 SelectInst(const SelectInst &SI)
918 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
919 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
922 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
923 Instruction *InsertBefore = 0)
924 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
925 Name, InsertBefore) {
928 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
929 BasicBlock *InsertAtEnd)
930 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
935 Value *getCondition() const { return Ops[0]; }
936 Value *getTrueValue() const { return Ops[1]; }
937 Value *getFalseValue() const { return Ops[2]; }
939 /// Transparently provide more efficient getOperand methods.
940 Value *getOperand(unsigned i) const {
941 assert(i < 3 && "getOperand() out of range!");
944 void setOperand(unsigned i, Value *Val) {
945 assert(i < 3 && "setOperand() out of range!");
948 unsigned getNumOperands() const { return 3; }
950 OtherOps getOpcode() const {
951 return static_cast<OtherOps>(Instruction::getOpcode());
954 virtual SelectInst *clone() const;
956 // Methods for support type inquiry through isa, cast, and dyn_cast:
957 static inline bool classof(const SelectInst *) { return true; }
958 static inline bool classof(const Instruction *I) {
959 return I->getOpcode() == Instruction::Select;
961 static inline bool classof(const Value *V) {
962 return isa<Instruction>(V) && classof(cast<Instruction>(V));
966 //===----------------------------------------------------------------------===//
968 //===----------------------------------------------------------------------===//
970 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
971 /// an argument of the specified type given a va_list and increments that list
973 class VAArgInst : public UnaryInstruction {
974 VAArgInst(const VAArgInst &VAA)
975 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
977 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
978 Instruction *InsertBefore = 0)
979 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
981 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
982 BasicBlock *InsertAtEnd)
983 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
986 virtual VAArgInst *clone() const;
987 bool mayWriteToMemory() const { return true; }
989 // Methods for support type inquiry through isa, cast, and dyn_cast:
990 static inline bool classof(const VAArgInst *) { return true; }
991 static inline bool classof(const Instruction *I) {
992 return I->getOpcode() == VAArg;
994 static inline bool classof(const Value *V) {
995 return isa<Instruction>(V) && classof(cast<Instruction>(V));
999 //===----------------------------------------------------------------------===//
1000 // ExtractElementInst Class
1001 //===----------------------------------------------------------------------===//
1003 /// ExtractElementInst - This instruction extracts a single (scalar)
1004 /// element from a PackedType value
1006 class ExtractElementInst : public Instruction {
1008 ExtractElementInst(const ExtractElementInst &EE) :
1009 Instruction(EE.getType(), ExtractElement, Ops, 2) {
1010 Ops[0].init(EE.Ops[0], this);
1011 Ops[1].init(EE.Ops[1], this);
1015 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1016 Instruction *InsertBefore = 0);
1017 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1018 Instruction *InsertBefore = 0);
1019 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1020 BasicBlock *InsertAtEnd);
1021 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1022 BasicBlock *InsertAtEnd);
1024 /// isValidOperands - Return true if an extractelement instruction can be
1025 /// formed with the specified operands.
1026 static bool isValidOperands(const Value *Vec, const Value *Idx);
1028 virtual ExtractElementInst *clone() const;
1030 virtual bool mayWriteToMemory() const { return false; }
1032 /// Transparently provide more efficient getOperand methods.
1033 Value *getOperand(unsigned i) const {
1034 assert(i < 2 && "getOperand() out of range!");
1037 void setOperand(unsigned i, Value *Val) {
1038 assert(i < 2 && "setOperand() out of range!");
1041 unsigned getNumOperands() const { return 2; }
1043 // Methods for support type inquiry through isa, cast, and dyn_cast:
1044 static inline bool classof(const ExtractElementInst *) { return true; }
1045 static inline bool classof(const Instruction *I) {
1046 return I->getOpcode() == Instruction::ExtractElement;
1048 static inline bool classof(const Value *V) {
1049 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1053 //===----------------------------------------------------------------------===//
1054 // InsertElementInst Class
1055 //===----------------------------------------------------------------------===//
1057 /// InsertElementInst - This instruction inserts a single (scalar)
1058 /// element into a PackedType value
1060 class InsertElementInst : public Instruction {
1062 InsertElementInst(const InsertElementInst &IE);
1064 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1065 const std::string &Name = "",Instruction *InsertBefore = 0);
1066 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1067 const std::string &Name = "",Instruction *InsertBefore = 0);
1068 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1069 const std::string &Name, BasicBlock *InsertAtEnd);
1070 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1071 const std::string &Name, BasicBlock *InsertAtEnd);
1073 /// isValidOperands - Return true if an insertelement instruction can be
1074 /// formed with the specified operands.
1075 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1078 virtual InsertElementInst *clone() const;
1080 virtual bool mayWriteToMemory() const { return false; }
1082 /// getType - Overload to return most specific packed type.
1084 inline const PackedType *getType() const {
1085 return reinterpret_cast<const PackedType*>(Instruction::getType());
1088 /// Transparently provide more efficient getOperand methods.
1089 Value *getOperand(unsigned i) const {
1090 assert(i < 3 && "getOperand() out of range!");
1093 void setOperand(unsigned i, Value *Val) {
1094 assert(i < 3 && "setOperand() out of range!");
1097 unsigned getNumOperands() const { return 3; }
1099 // Methods for support type inquiry through isa, cast, and dyn_cast:
1100 static inline bool classof(const InsertElementInst *) { return true; }
1101 static inline bool classof(const Instruction *I) {
1102 return I->getOpcode() == Instruction::InsertElement;
1104 static inline bool classof(const Value *V) {
1105 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1109 //===----------------------------------------------------------------------===//
1110 // ShuffleVectorInst Class
1111 //===----------------------------------------------------------------------===//
1113 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1116 class ShuffleVectorInst : public Instruction {
1118 ShuffleVectorInst(const ShuffleVectorInst &IE);
1120 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1121 const std::string &Name = "", Instruction *InsertBefor = 0);
1122 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1123 const std::string &Name, BasicBlock *InsertAtEnd);
1125 /// isValidOperands - Return true if a shufflevector instruction can be
1126 /// formed with the specified operands.
1127 static bool isValidOperands(const Value *V1, const Value *V2,
1130 virtual ShuffleVectorInst *clone() const;
1132 virtual bool mayWriteToMemory() const { return false; }
1134 /// getType - Overload to return most specific packed type.
1136 inline const PackedType *getType() const {
1137 return reinterpret_cast<const PackedType*>(Instruction::getType());
1140 /// Transparently provide more efficient getOperand methods.
1141 Value *getOperand(unsigned i) const {
1142 assert(i < 3 && "getOperand() out of range!");
1145 void setOperand(unsigned i, Value *Val) {
1146 assert(i < 3 && "setOperand() out of range!");
1149 unsigned getNumOperands() const { return 3; }
1151 // Methods for support type inquiry through isa, cast, and dyn_cast:
1152 static inline bool classof(const ShuffleVectorInst *) { return true; }
1153 static inline bool classof(const Instruction *I) {
1154 return I->getOpcode() == Instruction::ShuffleVector;
1156 static inline bool classof(const Value *V) {
1157 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1162 //===----------------------------------------------------------------------===//
1164 //===----------------------------------------------------------------------===//
1166 // PHINode - The PHINode class is used to represent the magical mystical PHI
1167 // node, that can not exist in nature, but can be synthesized in a computer
1168 // scientist's overactive imagination.
1170 class PHINode : public Instruction {
1171 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1172 /// the number actually in use.
1173 unsigned ReservedSpace;
1174 PHINode(const PHINode &PN);
1176 explicit PHINode(const Type *Ty, const std::string &Name = "",
1177 Instruction *InsertBefore = 0)
1178 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
1182 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1183 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
1189 /// reserveOperandSpace - This method can be used to avoid repeated
1190 /// reallocation of PHI operand lists by reserving space for the correct
1191 /// number of operands before adding them. Unlike normal vector reserves,
1192 /// this method can also be used to trim the operand space.
1193 void reserveOperandSpace(unsigned NumValues) {
1194 resizeOperands(NumValues*2);
1197 virtual PHINode *clone() const;
1199 /// getNumIncomingValues - Return the number of incoming edges
1201 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1203 /// getIncomingValue - Return incoming value number x
1205 Value *getIncomingValue(unsigned i) const {
1206 assert(i*2 < getNumOperands() && "Invalid value number!");
1207 return getOperand(i*2);
1209 void setIncomingValue(unsigned i, Value *V) {
1210 assert(i*2 < getNumOperands() && "Invalid value number!");
1213 unsigned getOperandNumForIncomingValue(unsigned i) {
1217 /// getIncomingBlock - Return incoming basic block number x
1219 BasicBlock *getIncomingBlock(unsigned i) const {
1220 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1222 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1223 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1225 unsigned getOperandNumForIncomingBlock(unsigned i) {
1229 /// addIncoming - Add an incoming value to the end of the PHI list
1231 void addIncoming(Value *V, BasicBlock *BB) {
1232 assert(getType() == V->getType() &&
1233 "All operands to PHI node must be the same type as the PHI node!");
1234 unsigned OpNo = NumOperands;
1235 if (OpNo+2 > ReservedSpace)
1236 resizeOperands(0); // Get more space!
1237 // Initialize some new operands.
1238 NumOperands = OpNo+2;
1239 OperandList[OpNo].init(V, this);
1240 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1243 /// removeIncomingValue - Remove an incoming value. This is useful if a
1244 /// predecessor basic block is deleted. The value removed is returned.
1246 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1247 /// is true), the PHI node is destroyed and any uses of it are replaced with
1248 /// dummy values. The only time there should be zero incoming values to a PHI
1249 /// node is when the block is dead, so this strategy is sound.
1251 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1253 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1254 int Idx = getBasicBlockIndex(BB);
1255 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1256 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1259 /// getBasicBlockIndex - Return the first index of the specified basic
1260 /// block in the value list for this PHI. Returns -1 if no instance.
1262 int getBasicBlockIndex(const BasicBlock *BB) const {
1263 Use *OL = OperandList;
1264 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1265 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1269 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1270 return getIncomingValue(getBasicBlockIndex(BB));
1273 /// hasConstantValue - If the specified PHI node always merges together the
1274 /// same value, return the value, otherwise return null.
1276 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1278 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1279 static inline bool classof(const PHINode *) { return true; }
1280 static inline bool classof(const Instruction *I) {
1281 return I->getOpcode() == Instruction::PHI;
1283 static inline bool classof(const Value *V) {
1284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1287 void resizeOperands(unsigned NumOperands);
1290 //===----------------------------------------------------------------------===//
1292 //===----------------------------------------------------------------------===//
1294 //===---------------------------------------------------------------------------
1295 /// ReturnInst - Return a value (possibly void), from a function. Execution
1296 /// does not continue in this function any longer.
1298 class ReturnInst : public TerminatorInst {
1299 Use RetVal; // Possibly null retval.
1300 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1301 RI.getNumOperands()) {
1302 if (RI.getNumOperands())
1303 RetVal.init(RI.RetVal, this);
1306 void init(Value *RetVal);
1309 // ReturnInst constructors:
1310 // ReturnInst() - 'ret void' instruction
1311 // ReturnInst( null) - 'ret void' instruction
1312 // ReturnInst(Value* X) - 'ret X' instruction
1313 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1314 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1315 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1316 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1318 // NOTE: If the Value* passed is of type void then the constructor behaves as
1319 // if it was passed NULL.
1320 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1321 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1324 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1325 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1328 explicit ReturnInst(BasicBlock *InsertAtEnd)
1329 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1332 virtual ReturnInst *clone() const;
1334 // Transparently provide more efficient getOperand methods.
1335 Value *getOperand(unsigned i) const {
1336 assert(i < getNumOperands() && "getOperand() out of range!");
1339 void setOperand(unsigned i, Value *Val) {
1340 assert(i < getNumOperands() && "setOperand() out of range!");
1344 Value *getReturnValue() const { return RetVal; }
1346 unsigned getNumSuccessors() const { return 0; }
1348 // Methods for support type inquiry through isa, cast, and dyn_cast:
1349 static inline bool classof(const ReturnInst *) { return true; }
1350 static inline bool classof(const Instruction *I) {
1351 return (I->getOpcode() == Instruction::Ret);
1353 static inline bool classof(const Value *V) {
1354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1357 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1358 virtual unsigned getNumSuccessorsV() const;
1359 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1362 //===----------------------------------------------------------------------===//
1364 //===----------------------------------------------------------------------===//
1366 //===---------------------------------------------------------------------------
1367 /// BranchInst - Conditional or Unconditional Branch instruction.
1369 class BranchInst : public TerminatorInst {
1370 /// Ops list - Branches are strange. The operands are ordered:
1371 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1372 /// they don't have to check for cond/uncond branchness.
1374 BranchInst(const BranchInst &BI);
1377 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1378 // BranchInst(BB *B) - 'br B'
1379 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1380 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1381 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1382 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1383 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1384 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
1385 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1386 assert(IfTrue != 0 && "Branch destination may not be null!");
1387 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1389 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1390 Instruction *InsertBefore = 0)
1391 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1392 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1393 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1394 Ops[2].init(Cond, this);
1400 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
1401 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1402 assert(IfTrue != 0 && "Branch destination may not be null!");
1403 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1406 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1407 BasicBlock *InsertAtEnd)
1408 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1409 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1410 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1411 Ops[2].init(Cond, this);
1418 /// Transparently provide more efficient getOperand methods.
1419 Value *getOperand(unsigned i) const {
1420 assert(i < getNumOperands() && "getOperand() out of range!");
1423 void setOperand(unsigned i, Value *Val) {
1424 assert(i < getNumOperands() && "setOperand() out of range!");
1428 virtual BranchInst *clone() const;
1430 inline bool isUnconditional() const { return getNumOperands() == 1; }
1431 inline bool isConditional() const { return getNumOperands() == 3; }
1433 inline Value *getCondition() const {
1434 assert(isConditional() && "Cannot get condition of an uncond branch!");
1435 return getOperand(2);
1438 void setCondition(Value *V) {
1439 assert(isConditional() && "Cannot set condition of unconditional branch!");
1443 // setUnconditionalDest - Change the current branch to an unconditional branch
1444 // targeting the specified block.
1445 // FIXME: Eliminate this ugly method.
1446 void setUnconditionalDest(BasicBlock *Dest) {
1447 if (isConditional()) { // Convert this to an uncond branch.
1452 setOperand(0, reinterpret_cast<Value*>(Dest));
1455 unsigned getNumSuccessors() const { return 1+isConditional(); }
1457 BasicBlock *getSuccessor(unsigned i) const {
1458 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1459 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1460 cast<BasicBlock>(getOperand(1));
1463 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1464 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1465 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1468 // Methods for support type inquiry through isa, cast, and dyn_cast:
1469 static inline bool classof(const BranchInst *) { return true; }
1470 static inline bool classof(const Instruction *I) {
1471 return (I->getOpcode() == Instruction::Br);
1473 static inline bool classof(const Value *V) {
1474 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1477 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1478 virtual unsigned getNumSuccessorsV() const;
1479 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1482 //===----------------------------------------------------------------------===//
1484 //===----------------------------------------------------------------------===//
1486 //===---------------------------------------------------------------------------
1487 /// SwitchInst - Multiway switch
1489 class SwitchInst : public TerminatorInst {
1490 unsigned ReservedSpace;
1491 // Operand[0] = Value to switch on
1492 // Operand[1] = Default basic block destination
1493 // Operand[2n ] = Value to match
1494 // Operand[2n+1] = BasicBlock to go to on match
1495 SwitchInst(const SwitchInst &RI);
1496 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1497 void resizeOperands(unsigned No);
1499 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1500 /// switch on and a default destination. The number of additional cases can
1501 /// be specified here to make memory allocation more efficient. This
1502 /// constructor can also autoinsert before another instruction.
1503 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1504 Instruction *InsertBefore = 0)
1505 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1506 init(Value, Default, NumCases);
1509 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1510 /// switch on and a default destination. The number of additional cases can
1511 /// be specified here to make memory allocation more efficient. This
1512 /// constructor also autoinserts at the end of the specified BasicBlock.
1513 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1514 BasicBlock *InsertAtEnd)
1515 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1516 init(Value, Default, NumCases);
1521 // Accessor Methods for Switch stmt
1522 inline Value *getCondition() const { return getOperand(0); }
1523 void setCondition(Value *V) { setOperand(0, V); }
1525 inline BasicBlock *getDefaultDest() const {
1526 return cast<BasicBlock>(getOperand(1));
1529 /// getNumCases - return the number of 'cases' in this switch instruction.
1530 /// Note that case #0 is always the default case.
1531 unsigned getNumCases() const {
1532 return getNumOperands()/2;
1535 /// getCaseValue - Return the specified case value. Note that case #0, the
1536 /// default destination, does not have a case value.
1537 ConstantInt *getCaseValue(unsigned i) {
1538 assert(i && i < getNumCases() && "Illegal case value to get!");
1539 return getSuccessorValue(i);
1542 /// getCaseValue - Return the specified case value. Note that case #0, the
1543 /// default destination, does not have a case value.
1544 const ConstantInt *getCaseValue(unsigned i) const {
1545 assert(i && i < getNumCases() && "Illegal case value to get!");
1546 return getSuccessorValue(i);
1549 /// findCaseValue - Search all of the case values for the specified constant.
1550 /// If it is explicitly handled, return the case number of it, otherwise
1551 /// return 0 to indicate that it is handled by the default handler.
1552 unsigned findCaseValue(const ConstantInt *C) const {
1553 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1554 if (getCaseValue(i) == C)
1559 /// findCaseDest - Finds the unique case value for a given successor. Returns
1560 /// null if the successor is not found, not unique, or is the default case.
1561 ConstantInt *findCaseDest(BasicBlock *BB) {
1562 if (BB == getDefaultDest()) return NULL;
1564 ConstantInt *CI = NULL;
1565 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1566 if (getSuccessor(i) == BB) {
1567 if (CI) return NULL; // Multiple cases lead to BB.
1568 else CI = getCaseValue(i);
1574 /// addCase - Add an entry to the switch instruction...
1576 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1578 /// removeCase - This method removes the specified successor from the switch
1579 /// instruction. Note that this cannot be used to remove the default
1580 /// destination (successor #0).
1582 void removeCase(unsigned idx);
1584 virtual SwitchInst *clone() const;
1586 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1587 BasicBlock *getSuccessor(unsigned idx) const {
1588 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1589 return cast<BasicBlock>(getOperand(idx*2+1));
1591 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1592 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1593 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1596 // getSuccessorValue - Return the value associated with the specified
1598 inline ConstantInt *getSuccessorValue(unsigned idx) const {
1599 assert(idx < getNumSuccessors() && "Successor # out of range!");
1600 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1603 // Methods for support type inquiry through isa, cast, and dyn_cast:
1604 static inline bool classof(const SwitchInst *) { return true; }
1605 static inline bool classof(const Instruction *I) {
1606 return I->getOpcode() == Instruction::Switch;
1608 static inline bool classof(const Value *V) {
1609 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1612 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1613 virtual unsigned getNumSuccessorsV() const;
1614 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1617 //===----------------------------------------------------------------------===//
1619 //===----------------------------------------------------------------------===//
1621 //===---------------------------------------------------------------------------
1623 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1624 /// calling convention of the call.
1626 class InvokeInst : public TerminatorInst {
1627 InvokeInst(const InvokeInst &BI);
1628 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1629 const std::vector<Value*> &Params);
1631 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1632 const std::vector<Value*> &Params, const std::string &Name = "",
1633 Instruction *InsertBefore = 0);
1634 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1635 const std::vector<Value*> &Params, const std::string &Name,
1636 BasicBlock *InsertAtEnd);
1639 virtual InvokeInst *clone() const;
1641 bool mayWriteToMemory() const { return true; }
1643 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1645 unsigned getCallingConv() const { return SubclassData; }
1646 void setCallingConv(unsigned CC) {
1650 /// getCalledFunction - Return the function called, or null if this is an
1651 /// indirect function invocation.
1653 Function *getCalledFunction() const {
1654 return dyn_cast<Function>(getOperand(0));
1657 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1658 inline Value *getCalledValue() const { return getOperand(0); }
1660 // get*Dest - Return the destination basic blocks...
1661 BasicBlock *getNormalDest() const {
1662 return cast<BasicBlock>(getOperand(1));
1664 BasicBlock *getUnwindDest() const {
1665 return cast<BasicBlock>(getOperand(2));
1667 void setNormalDest(BasicBlock *B) {
1668 setOperand(1, reinterpret_cast<Value*>(B));
1671 void setUnwindDest(BasicBlock *B) {
1672 setOperand(2, reinterpret_cast<Value*>(B));
1675 inline BasicBlock *getSuccessor(unsigned i) const {
1676 assert(i < 2 && "Successor # out of range for invoke!");
1677 return i == 0 ? getNormalDest() : getUnwindDest();
1680 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1681 assert(idx < 2 && "Successor # out of range for invoke!");
1682 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1685 unsigned getNumSuccessors() const { return 2; }
1687 // Methods for support type inquiry through isa, cast, and dyn_cast:
1688 static inline bool classof(const InvokeInst *) { return true; }
1689 static inline bool classof(const Instruction *I) {
1690 return (I->getOpcode() == Instruction::Invoke);
1692 static inline bool classof(const Value *V) {
1693 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1696 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1697 virtual unsigned getNumSuccessorsV() const;
1698 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1702 //===----------------------------------------------------------------------===//
1704 //===----------------------------------------------------------------------===//
1706 //===---------------------------------------------------------------------------
1707 /// UnwindInst - Immediately exit the current function, unwinding the stack
1708 /// until an invoke instruction is found.
1710 class UnwindInst : public TerminatorInst {
1712 explicit UnwindInst(Instruction *InsertBefore = 0)
1713 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1715 explicit UnwindInst(BasicBlock *InsertAtEnd)
1716 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1719 virtual UnwindInst *clone() const;
1721 unsigned getNumSuccessors() const { return 0; }
1723 // Methods for support type inquiry through isa, cast, and dyn_cast:
1724 static inline bool classof(const UnwindInst *) { return true; }
1725 static inline bool classof(const Instruction *I) {
1726 return I->getOpcode() == Instruction::Unwind;
1728 static inline bool classof(const Value *V) {
1729 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1732 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1733 virtual unsigned getNumSuccessorsV() const;
1734 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1737 //===----------------------------------------------------------------------===//
1738 // UnreachableInst Class
1739 //===----------------------------------------------------------------------===//
1741 //===---------------------------------------------------------------------------
1742 /// UnreachableInst - This function has undefined behavior. In particular, the
1743 /// presence of this instruction indicates some higher level knowledge that the
1744 /// end of the block cannot be reached.
1746 class UnreachableInst : public TerminatorInst {
1748 explicit UnreachableInst(Instruction *InsertBefore = 0)
1749 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1751 explicit UnreachableInst(BasicBlock *InsertAtEnd)
1752 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1755 virtual UnreachableInst *clone() const;
1757 unsigned getNumSuccessors() const { return 0; }
1759 // Methods for support type inquiry through isa, cast, and dyn_cast:
1760 static inline bool classof(const UnreachableInst *) { return true; }
1761 static inline bool classof(const Instruction *I) {
1762 return I->getOpcode() == Instruction::Unreachable;
1764 static inline bool classof(const Value *V) {
1765 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1768 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1769 virtual unsigned getNumSuccessorsV() const;
1770 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1773 } // End llvm namespace