Initial revision
[oota-llvm.git] / include / llvm / Instruction.h
1 //===-- llvm/Instruction.h - Instruction class definition --------*- C++ -*--=//
2 //
3 // This file contains the declaration of the Instruction class, which is the
4 // base class for all of the VM instructions.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_INSTRUCTION_H
9 #define LLVM_INSTRUCTION_H
10
11 #include "llvm/User.h"
12
13 class Type;
14 class BasicBlock;
15 class Method;
16
17 class Instruction : public User {
18   BasicBlock *Parent;
19   unsigned iType;      // InstructionType
20
21   friend class ValueHolder<Instruction,BasicBlock>;
22   inline void setParent(BasicBlock *P) { Parent = P; }
23
24 public:
25   Instruction(const Type *Ty, unsigned iType, const string &Name = "");
26   virtual ~Instruction();  // Virtual dtor == good.
27
28   // Specialize setName to handle symbol table majik...
29   virtual void setName(const string &name);
30
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
35   //
36   virtual Instruction *clone() const = 0;
37
38   // Accessor methods...
39   //
40   inline const BasicBlock *getParent() const { return Parent; }
41   inline       BasicBlock *getParent()       { return Parent; }
42   bool hasSideEffects() const { return false; }  // Memory & Call insts = true
43
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
48   // the wrong type.
49   //
50   inline Value *getOperand(unsigned i) {
51     return (Value*)((const Instruction *)this)->getOperand(i);
52   }
53   virtual const Value *getOperand(unsigned i) const = 0;
54   virtual bool setOperand(unsigned i, Value *Val) = 0;
55   virtual unsigned getNumOperands() const = 0;
56
57   // ---------------------------------------------------------------------------
58   // Operand Iterator interface...
59   //
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;
63
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;
68
69
70   // ---------------------------------------------------------------------------
71   // Subclass classification... getInstType() returns a member of 
72   // one of the enums that is coming soon (down below)...
73   //
74   virtual string getOpcode() const = 0;
75
76   unsigned getInstType() const { return iType; }
77   inline bool isTerminator() const {   // Instance of TerminatorInst?
78     return iType >= FirstTermOp && iType < NumTermOps; 
79   }
80   inline bool isDefinition() const { return !isTerminator(); }
81   inline bool isUnaryOp() const {
82     return iType >= FirstUnaryOp && iType < NumUnaryOps;
83   }
84   inline bool isBinaryOp() const {
85     return iType >= FirstBinaryOp && iType < NumBinaryOps;
86   }
87
88   static Instruction *getBinaryOperator(unsigned Op, Value *S1, Value *S2);
89   static Instruction *getUnaryOperator (unsigned Op, Value *Source);
90
91
92   //----------------------------------------------------------------------
93   // Exported enumerations...
94   //
95   enum TermOps {       // These terminate basic blocks
96     FirstTermOp = 1,
97     Ret = 1, Br, Switch, 
98     NumTermOps         // Must remain at end of enum
99   };
100
101   enum UnaryOps {
102     FirstUnaryOp = NumTermOps,
103     Neg          = NumTermOps, Not, 
104     
105     // Type conversions...
106     ToBoolTy  , 
107     ToUByteTy , ToSByteTy,  ToUShortTy, ToShortTy,
108     ToUInt    , ToInt,      ToULongTy , ToLongTy,
109
110     ToFloatTy , ToDoubleTy, ToArrayTy , ToPointerTy,
111
112     NumUnaryOps        // Must remain at end of enum
113   };
114
115   enum BinaryOps {
116     // Standard binary operators...
117     FirstBinaryOp = NumUnaryOps,
118     Add = NumUnaryOps, Sub, Mul, Div, Rem,
119
120     // Logical operators...
121     And, Or, Xor,
122
123     // Binary comparison operators...
124     SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT,
125
126     NumBinaryOps
127   };
128
129   enum MemoryOps {
130     FirstMemoryOp = NumBinaryOps,
131     Malloc = NumBinaryOps, Free,     // Heap management instructions
132     Alloca,                          // Stack management instruction
133
134     Load, Store,                     // Memory manipulation instructions.
135
136     GetField, PutField,              // Structure manipulation instructions
137
138     NumMemoryOps
139   };
140
141   enum OtherOps {
142     FirstOtherOp = NumMemoryOps,
143     PHINode      = NumMemoryOps,     // PHI node instruction
144     Call,                            // Call a function
145
146     Shl, Shr,                        // Shift operations...
147
148     NumOps,                          // Must be the last 'op' defined.
149     UserOp1, UserOp2                 // May be used internally to a pass...
150   };
151
152 public:
153   template <class _Inst, class _Val>         // Operand Iterator Implementation
154   class OperandIterator {
155     const _Inst Inst;
156     unsigned idx;
157   public:
158     typedef OperandIterator<_Inst, _Val> _Self;
159     typedef forward_iterator_tag iterator_category;
160     typedef _Val pointer;
161     
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
165     
166     inline bool operator==(const _Self& x) const { return idx == x.idx; }
167     inline bool operator!=(const _Self& x) const { return !operator==(x); }
168
169     inline pointer operator*() const { return Inst->getOperand(idx); }
170     inline pointer *operator->() const { return &(operator*()); }
171     
172     inline _Self& operator++() { ++idx; return *this; } // Preincrement
173     inline _Self operator++(int) { // Postincrement
174       _Self tmp = *this; ++*this; return tmp; 
175     }
176
177     inline _Self& operator--() { --idx; return *this; }  // Predecrement
178     inline _Self operator--(int) { // Postdecrement
179       _Self tmp = *this; --*this; return tmp;
180     }
181   };
182
183 };
184
185 inline Instruction::op_iterator       Instruction::op_begin()       {
186   return op_iterator(this);
187 }
188 inline Instruction::op_const_iterator Instruction::op_begin() const {
189   return op_const_iterator(this);
190 }
191 inline Instruction::op_iterator       Instruction::op_end()         {
192   return op_iterator(this,true);
193 }
194 inline Instruction::op_const_iterator Instruction::op_end()   const {
195   return op_const_iterator(this,true);
196 }
197
198
199 #endif