Fairly major overhaul of MachineInstr & Operand classes
[oota-llvm.git] / include / llvm / CodeGen / MachineInstr.h
1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*--=//
2 //
3 // This file contains the declaration of the MachineInstr class, which is the
4 // basic representation for all target dependant machine instructions used by
5 // the back end.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
10 #define LLVM_CODEGEN_MACHINEINSTR_H
11
12 #include "llvm/Annotation.h"
13 #include "Support/iterator"
14 #include "Support/NonCopyable.h"
15 #include <vector>
16 class Value;
17 class Function;
18
19 typedef int MachineOpCode;
20 typedef int OpCodeMask;
21 typedef int InstrSchedClass;
22
23 //---------------------------------------------------------------------------
24 // class MachineOperand 
25 // 
26 // Purpose:
27 //   Representation of each machine instruction operand.
28 //   This class is designed so that you can allocate a vector of operands
29 //   first and initialize each one later.
30 //
31 //   E.g, for this VM instruction:
32 //              ptr = alloca type, numElements
33 //   we generate 2 machine instructions on the SPARC:
34 // 
35 //              mul Constant, Numelements -> Reg
36 //              add %sp, Reg -> Ptr
37 // 
38 //   Each instruction has 3 operands, listed above.  Of those:
39 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
40 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
41 //      
42 //   For the register operands, the virtual register type is as follows:
43 //      
44 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
45 //      MachineInstr* minstr will point to the instruction that computes reg.
46 // 
47 //   -  %sp will be of virtual register type MO_MachineReg.
48 //      The field regNum identifies the machine register.
49 // 
50 //   -  NumElements will be of virtual register type MO_VirtualReg.
51 //      The field Value* value identifies the value.
52 // 
53 //   -  Ptr will also be of virtual register type MO_VirtualReg.
54 //      Again, the field Value* value identifies the value.
55 // 
56 //---------------------------------------------------------------------------
57
58
59 class MachineOperand {
60 public:
61   enum MachineOperandType {
62     MO_VirtualRegister,         // virtual register for *value
63     MO_MachineRegister,         // pre-assigned machine register `regNum'
64     MO_CCRegister,
65     MO_SignExtendedImmed,
66     MO_UnextendedImmed,
67     MO_PCRelativeDisp,
68   };
69   
70 private:
71   // Bit fields of the flags variable used for different operand properties
72   static const char DEFFLAG    = 0x1;  // this is a def of the operand
73   static const char DEFUSEFLAG = 0x2;  // this is both a def and a use
74   static const char HIFLAG32   = 0x4;  // operand is %hi32(value_or_immedVal)
75   static const char LOFLAG32   = 0x8;  // operand is %lo32(value_or_immedVal)
76   static const char HIFLAG64   = 0x10; // operand is %hi64(value_or_immedVal)
77   static const char LOFLAG64   = 0x20; // operand is %lo64(value_or_immedVal)
78   
79 private:
80   union {
81     Value*      value;          // BasicBlockVal for a label operand.
82                                 // ConstantVal for a non-address immediate.
83                                 // Virtual register for an SSA operand,
84                                 // including hidden operands required for
85                                 // the generated machine code.     
86     int64_t immedVal;           // constant value for an explicit constant
87   };
88
89   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
90   char flags;                   // see bit field definitions above
91   int regNum;                   // register number for an explicit register
92                                 // will be set for a value after reg allocation
93 public:
94   MachineOperand() : immedVal(0), opType(MO_VirtualRegister),
95                      flags(0), regNum(-1) {}
96   MachineOperand(const MachineOperand &M)
97     : immedVal(M.immedVal), opType(M.opType), flags(M.flags), regNum(M.regNum) {
98   }
99   ~MachineOperand() {}
100   
101   // Accessor methods.  Caller is responsible for checking the
102   // operand type before invoking the corresponding accessor.
103   // 
104   MachineOperandType getOperandType() const { return opType; }
105
106   inline Value*         getVRegValue    () const {
107     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
108            opType == MO_PCRelativeDisp);
109     return value;
110   }
111   inline Value*         getVRegValueOrNull() const {
112     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
113             opType == MO_PCRelativeDisp)? value : NULL;
114   }
115   inline int            getMachineRegNum() const {
116     assert(opType == MO_MachineRegister);
117     return regNum;
118   }
119   inline int64_t        getImmedValue   () const {
120     assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
121     return immedVal;
122   }
123   bool          opIsDef         () const { return flags & DEFFLAG; }
124   bool          opIsDefAndUse   () const { return flags & DEFUSEFLAG; }
125   bool          opHiBits32      () const { return flags & HIFLAG32; }
126   bool          opLoBits32      () const { return flags & LOFLAG32; }
127   bool          opHiBits64      () const { return flags & HIFLAG64; }
128   bool          opLoBits64      () const { return flags & LOFLAG64; }
129
130   // used to check if a machine register has been allocated to this operand
131   inline bool   hasAllocatedReg() const {
132     return (regNum >= 0 &&
133             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
134              opType == MO_MachineRegister));
135   }
136
137   // used to get the reg number if when one is allocated
138   inline int  getAllocatedRegNum() const {
139     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
140            opType == MO_MachineRegister);
141     return regNum;
142   }
143
144   
145   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
146
147 private:
148
149   // Construction methods needed for fine-grain control.
150   // These must be accessed via coresponding methods in MachineInstr.
151   void markDef()       { flags |= DEFFLAG; }
152   void markDefAndUse() { flags |= DEFUSEFLAG; }
153   void markHi32()      { flags |= HIFLAG32; }
154   void markLo32()      { flags |= LOFLAG32; }
155   void markHi64()      { flags |= HIFLAG64; }
156   void markLo64()      { flags |= LOFLAG64; }
157   
158   // Replaces the Value with its corresponding physical register after
159   // register allocation is complete
160   void setRegForValue(int reg) {
161     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
162            opType == MO_MachineRegister);
163     regNum = reg;
164   }
165   
166   friend class MachineInstr;
167 };
168
169
170 //---------------------------------------------------------------------------
171 // class MachineInstr 
172 // 
173 // Purpose:
174 //   Representation of each machine instruction.
175 // 
176 //   MachineOpCode must be an enum, defined separately for each target.
177 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
178 // 
179 //   opCodeMask is used to record variants of an instruction.
180 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
181 //      ANNUL:             if 1: Annul delay slot instruction.
182 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
183 //   Instead of creating 4 different opcodes for BNZ, we create a single
184 //   opcode and set bits in opCodeMask for each of these flags.
185 //
186 //  There are 2 kinds of operands:
187 // 
188 //  (1) Explicit operands of the machine instruction in vector operands[] 
189 // 
190 //  (2) "Implicit operands" are values implicitly used or defined by the
191 //      machine instruction, such as arguments to a CALL, return value of
192 //      a CALL (if any), and return value of a RETURN.
193 //---------------------------------------------------------------------------
194
195 class MachineInstr : public Annotable,         // MachineInstrs are annotable
196                      public NonCopyable {      // Disable copy operations
197   MachineOpCode    opCode;              // the opcode
198   OpCodeMask       opCodeMask;          // extra bits for variants of an opcode
199   std::vector<MachineOperand> operands; // the operands
200
201   struct ImplicitRef {
202     Value *Val;
203     bool isDef, isDefAndUse;
204
205     ImplicitRef(Value *V, bool D, bool DU) : Val(V), isDef(D), isDefAndUse(DU){}
206   };
207
208   // implicitRefs - Values implicitly referenced by this machine instruction
209   // (eg, call args)
210   std::vector<ImplicitRef> implicitRefs;
211
212   // regsUsed - all machine registers used for this instruction, including regs
213   // used to save values across the instruction.  This is a bitset of registers.
214   std::vector<bool> regsUsed;
215 public:
216   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
217                                          OpCodeMask    _opCodeMask = 0);
218   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
219                                          unsigned       numOperands,
220                                          OpCodeMask    _opCodeMask = 0);
221   inline                ~MachineInstr   () {}
222
223   // 
224   // Support to rewrite a machine instruction in place: for now, simply
225   // replace() and then set new operands with Set.*Operand methods below.
226   // 
227   void                  replace         (MachineOpCode _opCode,
228                                          unsigned       numOperands,
229                                          OpCodeMask    _opCodeMask = 0x0);
230   
231   //
232   // The opcode.
233   // 
234   const MachineOpCode getOpCode() const { return opCode; }
235
236   //
237   // Information about explicit operands of the instruction
238   // 
239   unsigned getNumOperands() const { return operands.size(); }
240   
241   bool operandIsDefined(unsigned i) const {
242     return getOperand(i).opIsDef();
243   }
244
245   bool operandIsDefinedAndUsed(unsigned i) const {
246     return getOperand(i).opIsDefAndUse();
247   }
248   
249   const MachineOperand& getOperand(unsigned i) const {
250     assert(i < operands.size() && "getOperand() out of range!");
251     return operands[i];
252   }
253   MachineOperand& getOperand(unsigned i) {
254     assert(i < operands.size() && "getOperand() out of range!");
255     return operands[i];
256   }
257   
258   //
259   // Information about implicit operands of the instruction
260   // 
261   unsigned getNumImplicitRefs() const{ return implicitRefs.size();}
262   
263   const Value* getImplicitRef(unsigned i) const {
264     assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
265     return implicitRefs[i].Val;
266   }
267   Value* getImplicitRef(unsigned i) {
268     assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
269     return implicitRefs[i].Val;
270   }
271
272   bool implicitRefIsDefined(unsigned i) const {
273     assert(i < implicitRefs.size() && "implicitRefIsDefined() out of range!");
274     return implicitRefs[i].isDef;
275   }
276   bool implicitRefIsDefinedAndUsed(unsigned i) const {
277     assert(i < implicitRefs.size() && "implicitRefIsDef&Used() out of range!");
278     return implicitRefs[i].isDefAndUse;
279   }
280   
281   void addImplicitRef(Value* V, bool isDef=false, bool isDefAndUse=false) {
282     implicitRefs.push_back(ImplicitRef(V, isDef, isDefAndUse));
283   }
284   
285   void setImplicitRef(unsigned i, Value* V, bool isDef=false,
286                       bool isDefAndUse=false) {
287     assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
288     implicitRefs[i] = ImplicitRef(V, isDef, isDefAndUse);
289   }
290   
291   //
292   // Information about registers used in this instruction
293   // 
294   const std::vector<bool> &getRegsUsed() const { return regsUsed; }
295   
296   // insertUsedReg - Add a register to the Used registers set...
297   void insertUsedReg(unsigned Reg) {
298     if (Reg >= regsUsed.size())
299       regsUsed.resize(Reg+1);
300     regsUsed[Reg] = true;
301   }
302
303   //
304   // Debugging support
305   // 
306   void dump() const;
307   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
308
309   //
310   // Define iterators to access the Value operands of the Machine Instruction.
311   // begin() and end() are defined to produce these iterators...
312   //
313   template<class _MI, class _V> class ValOpIterator;
314   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
315   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
316
317
318   // Access to set the operands when building the machine instruction
319   // 
320   void SetMachineOperandVal(unsigned i,
321                             MachineOperand::MachineOperandType operandType,
322                             Value* V, bool isDef=false, bool isDefAndUse=false);
323   void SetMachineOperandConst(unsigned i,
324                               MachineOperand::MachineOperandType operandType,
325                               int64_t intValue);
326   void SetMachineOperandReg(unsigned i, int regNum, bool isDef=false,
327                             bool isDefAndUse=false, bool isCCReg=false);
328
329   unsigned substituteValue(const Value* oldVal, Value* newVal,
330                            bool defsOnly = true);
331
332   void setOperandHi32(unsigned i) { operands[i].markHi32(); }
333   void setOperandLo32(unsigned i) { operands[i].markLo32(); }
334   void setOperandHi64(unsigned i) { operands[i].markHi64(); }
335   void setOperandLo64(unsigned i) { operands[i].markLo64(); }
336   
337   
338   // SetRegForOperand - Replaces the Value for the operand with its allocated
339   // physical register after register allocation is complete.
340   // 
341   void SetRegForOperand(unsigned i, int regNum);
342   
343   //
344   // Iterator to enumerate machine operands.
345   // 
346   template<class MITy, class VTy>
347   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
348     unsigned i;
349     MITy MI;
350     
351     inline void skipToNextVal() {
352       while (i < MI->getNumOperands() &&
353              !((MI->getOperand(i).getOperandType() == MachineOperand::MO_VirtualRegister ||
354                 MI->getOperand(i).getOperandType() == MachineOperand::MO_CCRegister)
355                && MI->getOperand(i).getVRegValue() != 0))
356         ++i;
357     }
358   
359     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
360       skipToNextVal();
361     }
362   
363   public:
364     typedef ValOpIterator<MITy, VTy> _Self;
365     
366     inline VTy operator*() const {
367       return MI->getOperand(i).getVRegValue();
368     }
369
370     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
371           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
372
373     inline VTy operator->() const { return operator*(); }
374
375     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
376     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
377
378     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
379     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
380
381     inline bool operator==(const _Self &y) const { 
382       return i == y.i;
383     }
384     inline bool operator!=(const _Self &y) const { 
385       return !operator==(y);
386     }
387
388     static _Self begin(MITy MI) {
389       return _Self(MI, 0);
390     }
391     static _Self end(MITy MI) {
392       return _Self(MI, MI->getNumOperands());
393     }
394   };
395
396   // define begin() and end()
397   val_op_iterator begin() { return val_op_iterator::begin(this); }
398   val_op_iterator end()   { return val_op_iterator::end(this); }
399
400   const_val_op_iterator begin() const {
401     return const_val_op_iterator::begin(this);
402   }
403   const_val_op_iterator end() const {
404     return const_val_op_iterator::end(this);
405   }
406 };
407
408 //---------------------------------------------------------------------------
409 // Debugging Support
410 //---------------------------------------------------------------------------
411
412 std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
413
414 std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
415                                          
416 void PrintMachineInstructions(const Function *F);
417
418 #endif