1 //===-- llvm/Instruction.h - Instruction class definition --------*- C++ -*--=//
3 // This file contains the declaration of the Instruction class, which is the
4 // base class for all of the VM instructions.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_INSTRUCTION_H
9 #define LLVM_INSTRUCTION_H
11 #include "llvm/User.h"
17 class Instruction : public User {
19 unsigned iType; // InstructionType
21 friend class ValueHolder<Instruction,BasicBlock>;
22 inline void setParent(BasicBlock *P) { Parent = P; }
25 Instruction(const Type *Ty, unsigned iType, const string &Name = "");
26 virtual ~Instruction(); // Virtual dtor == good.
28 // Specialize setName to handle symbol table majik...
29 virtual void setName(const string &name);
31 // clone() - Create a copy of 'this' instruction that is identical in all ways
32 // except the following:
33 // * The instruction has no parent
34 // * The instruction has no name
36 virtual Instruction *clone() const = 0;
38 // Accessor methods...
40 inline const BasicBlock *getParent() const { return Parent; }
41 inline BasicBlock *getParent() { return Parent; }
42 bool hasSideEffects() const { return false; } // Memory & Call insts = true
44 // ---------------------------------------------------------------------------
45 // Implement the User interface
46 // if i > the number of operands, then getOperand() returns 0, and setOperand
47 // returns false. setOperand() may also return false if the operand is of
50 inline Value *getOperand(unsigned i) {
51 return (Value*)((const Instruction *)this)->getOperand(i);
53 virtual const Value *getOperand(unsigned i) const = 0;
54 virtual bool setOperand(unsigned i, Value *Val) = 0;
55 virtual unsigned getNumOperands() const = 0;
57 // ---------------------------------------------------------------------------
58 // Operand Iterator interface...
60 template <class _Inst, class _Val> class OperandIterator;
61 typedef OperandIterator<Instruction *, Value *> op_iterator;
62 typedef OperandIterator<const Instruction *, const Value *> op_const_iterator;
64 inline op_iterator op_begin() ;
65 inline op_const_iterator op_begin() const;
66 inline op_iterator op_end() ;
67 inline op_const_iterator op_end() const;
70 // ---------------------------------------------------------------------------
71 // Subclass classification... getInstType() returns a member of
72 // one of the enums that is coming soon (down below)...
74 virtual string getOpcode() const = 0;
76 unsigned getInstType() const { return iType; }
77 inline bool isTerminator() const { // Instance of TerminatorInst?
78 return iType >= FirstTermOp && iType < NumTermOps;
80 inline bool isDefinition() const { return !isTerminator(); }
81 inline bool isUnaryOp() const {
82 return iType >= FirstUnaryOp && iType < NumUnaryOps;
84 inline bool isBinaryOp() const {
85 return iType >= FirstBinaryOp && iType < NumBinaryOps;
88 static Instruction *getBinaryOperator(unsigned Op, Value *S1, Value *S2);
89 static Instruction *getUnaryOperator (unsigned Op, Value *Source);
92 //----------------------------------------------------------------------
93 // Exported enumerations...
95 enum TermOps { // These terminate basic blocks
98 NumTermOps // Must remain at end of enum
102 FirstUnaryOp = NumTermOps,
103 Neg = NumTermOps, Not,
105 // Type conversions...
107 ToUByteTy , ToSByteTy, ToUShortTy, ToShortTy,
108 ToUInt , ToInt, ToULongTy , ToLongTy,
110 ToFloatTy , ToDoubleTy, ToArrayTy , ToPointerTy,
112 NumUnaryOps // Must remain at end of enum
116 // Standard binary operators...
117 FirstBinaryOp = NumUnaryOps,
118 Add = NumUnaryOps, Sub, Mul, Div, Rem,
120 // Logical operators...
123 // Binary comparison operators...
124 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT,
130 FirstMemoryOp = NumBinaryOps,
131 Malloc = NumBinaryOps, Free, // Heap management instructions
132 Alloca, // Stack management instruction
134 Load, Store, // Memory manipulation instructions.
136 GetField, PutField, // Structure manipulation instructions
142 FirstOtherOp = NumMemoryOps,
143 PHINode = NumMemoryOps, // PHI node instruction
144 Call, // Call a function
146 Shl, Shr, // Shift operations...
148 NumOps, // Must be the last 'op' defined.
149 UserOp1, UserOp2 // May be used internally to a pass...
153 template <class _Inst, class _Val> // Operand Iterator Implementation
154 class OperandIterator {
158 typedef OperandIterator<_Inst, _Val> _Self;
159 typedef forward_iterator_tag iterator_category;
160 typedef _Val pointer;
162 inline OperandIterator(_Inst T) : Inst(T), idx(0) {} // begin iterator
163 inline OperandIterator(_Inst T, bool)
164 : Inst(T), idx(Inst->getNumOperands()) {} // end iterator
166 inline bool operator==(const _Self& x) const { return idx == x.idx; }
167 inline bool operator!=(const _Self& x) const { return !operator==(x); }
169 inline pointer operator*() const { return Inst->getOperand(idx); }
170 inline pointer *operator->() const { return &(operator*()); }
172 inline _Self& operator++() { ++idx; return *this; } // Preincrement
173 inline _Self operator++(int) { // Postincrement
174 _Self tmp = *this; ++*this; return tmp;
177 inline _Self& operator--() { --idx; return *this; } // Predecrement
178 inline _Self operator--(int) { // Postdecrement
179 _Self tmp = *this; --*this; return tmp;
185 inline Instruction::op_iterator Instruction::op_begin() {
186 return op_iterator(this);
188 inline Instruction::op_const_iterator Instruction::op_begin() const {
189 return op_const_iterator(this);
191 inline Instruction::op_iterator Instruction::op_end() {
192 return op_iterator(this,true);
194 inline Instruction::op_const_iterator Instruction::op_end() const {
195 return op_const_iterator(this,true);