Include header in the compiler-neutral location
[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/Target/MachineInstrInfo.h"
13 #include "llvm/Annotation.h"
14 #include <iterator>
15 #include <ext/hash_set>
16 class Instruction;
17
18 //---------------------------------------------------------------------------
19 // class MachineOperand 
20 // 
21 // Purpose:
22 //   Representation of each machine instruction operand.
23 //   This class is designed so that you can allocate a vector of operands
24 //   first and initialize each one later.
25 //
26 //   E.g, for this VM instruction:
27 //              ptr = alloca type, numElements
28 //   we generate 2 machine instructions on the SPARC:
29 // 
30 //              mul Constant, Numelements -> Reg
31 //              add %sp, Reg -> Ptr
32 // 
33 //   Each instruction has 3 operands, listed above.  Of those:
34 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
35 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
36 //      
37 //   For the register operands, the virtual register type is as follows:
38 //      
39 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
40 //      MachineInstr* minstr will point to the instruction that computes reg.
41 // 
42 //   -  %sp will be of virtual register type MO_MachineReg.
43 //      The field regNum identifies the machine register.
44 // 
45 //   -  NumElements will be of virtual register type MO_VirtualReg.
46 //      The field Value* value identifies the value.
47 // 
48 //   -  Ptr will also be of virtual register type MO_VirtualReg.
49 //      Again, the field Value* value identifies the value.
50 // 
51 //---------------------------------------------------------------------------
52
53
54 class MachineOperand {
55 public:
56   enum MachineOperandType {
57     MO_VirtualRegister,         // virtual register for *value
58     MO_MachineRegister,         // pre-assigned machine register `regNum'
59     MO_CCRegister,
60     MO_SignExtendedImmed,
61     MO_UnextendedImmed,
62     MO_PCRelativeDisp,
63   };
64   
65 private:
66   MachineOperandType opType;
67   
68   union {
69     Value*      value;          // BasicBlockVal for a label operand.
70                                 // ConstantVal for a non-address immediate.
71                                 // Virtual register for an SSA operand,
72                                 // including hidden operands required for
73                                 // the generated machine code.     
74     int64_t immedVal;           // constant value for an explicit constant
75   };
76
77   int regNum;                   // register number for an explicit register
78                                 // will be set for a value after reg allocation
79   bool isDef;                   // is this a definition for the value?
80   bool isDefAndUse;             // is this a both a def and a use of the value?
81                                 // we assume that a non-def *must* be a use.
82 public:
83   /*ctor*/              MachineOperand  ();
84   /*ctor*/              MachineOperand  (MachineOperandType operandType,
85                                          Value* _val);
86   /*copy ctor*/         MachineOperand  (const MachineOperand&);
87   /*dtor*/              ~MachineOperand () {}
88   
89   // Accessor methods.  Caller is responsible for checking the
90   // operand type before invoking the corresponding accessor.
91   // 
92   inline MachineOperandType getOperandType() const {
93     return opType;
94   }
95   inline Value*         getVRegValue    () const {
96     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
97            opType == MO_PCRelativeDisp);
98     return value;
99   }
100   inline int            getMachineRegNum() const {
101     assert(opType == MO_MachineRegister);
102     return regNum;
103   }
104   inline int64_t        getImmedValue   () const {
105     assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
106     return immedVal;
107   }
108   inline bool           opIsDef         () const {
109     return isDef;
110   }
111   inline bool           opIsDefAndUse   () const {
112     return isDefAndUse;
113   }
114   
115 public:
116   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
117
118   
119 private:
120   // These functions are provided so that a vector of operands can be
121   // statically allocated and individual ones can be initialized later.
122   // Give class MachineInstr gets access to these functions.
123   // 
124   void                  Initialize      (MachineOperandType operandType,
125                                          Value* _val);
126   void                  InitializeConst (MachineOperandType operandType,
127                                          int64_t intValue);
128   void                  InitializeReg   (int regNum,
129                                          bool isCCReg);
130
131   // Replaces the Value with its corresponding physical register after
132   // register allocation is complete
133   void setRegForValue(int reg) {
134     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
135            opType == MO_MachineRegister);
136     regNum = reg;
137   }
138
139   friend class MachineInstr;
140
141 public:
142   
143   // used to get the reg number if when one is allocted (must be
144   // called only after reg alloc)
145   inline int  getAllocatedRegNum() const {
146     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
147            opType == MO_MachineRegister);
148     return regNum;
149   }
150 };
151
152
153 inline
154 MachineOperand::MachineOperand()
155   : opType(MO_VirtualRegister),
156     immedVal(0),
157     regNum(-1),
158     isDef(false),
159     isDefAndUse(false)
160 {}
161
162 inline
163 MachineOperand::MachineOperand(MachineOperandType operandType,
164                                Value* _val)
165   : opType(operandType),
166     immedVal(0),
167     regNum(-1),
168     isDef(false),
169     isDefAndUse(false)
170 {}
171
172 inline
173 MachineOperand::MachineOperand(const MachineOperand& mo)
174   : opType(mo.opType),
175     isDef(false),
176     isDefAndUse(false)
177 {
178   switch(opType) {
179   case MO_VirtualRegister:
180   case MO_CCRegister:           value = mo.value; break;
181   case MO_MachineRegister:      regNum = mo.regNum; break;
182   case MO_SignExtendedImmed:
183   case MO_UnextendedImmed:
184   case MO_PCRelativeDisp:       immedVal = mo.immedVal; break;
185   default: assert(0);
186   }
187 }
188
189 inline void
190 MachineOperand::Initialize(MachineOperandType operandType,
191                            Value* _val)
192 {
193   opType = operandType;
194   value = _val;
195   regNum = -1;
196 }
197
198 inline void
199 MachineOperand::InitializeConst(MachineOperandType operandType,
200                                 int64_t intValue)
201 {
202   opType = operandType;
203   value = NULL;
204   immedVal = intValue;
205   regNum = -1;
206 }
207
208 inline void
209 MachineOperand::InitializeReg(int _regNum, bool isCCReg)
210 {
211   opType = isCCReg? MO_CCRegister : MO_MachineRegister;
212   value = NULL;
213   regNum = (int) _regNum;
214 }
215
216
217 //---------------------------------------------------------------------------
218 // class MachineInstr 
219 // 
220 // Purpose:
221 //   Representation of each machine instruction.
222 // 
223 //   MachineOpCode must be an enum, defined separately for each target.
224 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
225 // 
226 //   opCodeMask is used to record variants of an instruction.
227 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
228 //      ANNUL:             if 1: Annul delay slot instruction.
229 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
230 //   Instead of creating 4 different opcodes for BNZ, we create a single
231 //   opcode and set bits in opCodeMask for each of these flags.
232 //
233 //  There are 2 kinds of operands:
234 // 
235 //  (1) Explicit operands of the machine instruction in vector operands[] 
236 // 
237 //  (2) "Implicit operands" are values implicitly used or defined by the
238 //      machine instruction, such as arguments to a CALL, return value of
239 //      a CALL (if any), and return value of a RETURN.
240 //---------------------------------------------------------------------------
241
242 class MachineInstr :  public Annotable,         // Values are annotable
243                       public NonCopyableV {     // Disable copy operations
244   MachineOpCode    opCode;              // the opcode
245   OpCodeMask       opCodeMask;          // extra bits for variants of an opcode
246   vector<MachineOperand> operands;      // the operands
247   vector<Value*>   implicitRefs;        // values implicitly referenced by this
248   vector<bool>     implicitIsDef;       //  machine instruction (eg, call args)
249   vector<bool>     implicitIsDefAndUse; //
250   hash_set<int>    regsUsed;            // all machine registers used for this
251                                         //  instruction, including regs used
252                                         //  to save values across the instr.
253 public:
254   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
255                                          OpCodeMask    _opCodeMask = 0x0);
256   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
257                                          unsigned       numOperands,
258                                          OpCodeMask    _opCodeMask = 0x0);
259   inline                ~MachineInstr   () {}
260   const MachineOpCode   getOpCode       () const { return opCode; }
261
262   //
263   // Information about explicit operands of the instruction
264   // 
265   unsigned int          getNumOperands  () const { return operands.size(); }
266   
267   bool                  operandIsDefined(unsigned i) const;
268   bool                  operandIsDefinedAndUsed(unsigned i) const;
269   
270   const MachineOperand& getOperand      (unsigned i) const;
271         MachineOperand& getOperand      (unsigned i);
272   
273   //
274   // Information about implicit operands of the instruction
275   // 
276   unsigned              getNumImplicitRefs() const{return implicitRefs.size();}
277   
278   bool                  implicitRefIsDefined(unsigned i) const;
279   bool                  implicitRefIsDefinedAndUsed(unsigned i) const;
280   
281   const Value*          getImplicitRef  (unsigned i) const;
282         Value*          getImplicitRef  (unsigned i);
283   
284   //
285   // Information about registers used in this instruction
286   // 
287   const hash_set<int>&  getRegsUsed    () const { return regsUsed; }
288         hash_set<int>&  getRegsUsed    ()       { return regsUsed; }
289   
290   //
291   // Debugging support
292   // 
293   void                  dump            () const;
294   friend std::ostream& operator<<       (std::ostream& os,
295                                          const MachineInstr& minstr);
296
297   //
298   // Define iterators to access the Value operands of the Machine Instruction.
299   // begin() and end() are defined to produce these iterators...
300   //
301   template<class _MI, class _V> class ValOpIterator;
302   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
303   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
304
305
306   // Access to set the operands when building the machine instruction
307   // 
308   void                  SetMachineOperandVal(unsigned i,
309                                              MachineOperand::MachineOperandType
310                                                operandType,
311                                              Value* _val,
312                                              bool isDef=false,
313                                              bool isDefAndUse=false);
314   void                  SetMachineOperandConst(unsigned i,
315                                            MachineOperand::MachineOperandType
316                                                  operandType,
317                                                int64_t intValue);
318   void                  SetMachineOperandReg(unsigned i, int regNum, 
319                                              bool isDef=false,
320                                              bool isDefAndUse=false,
321                                              bool isCCReg=false);
322   
323   void                  addImplicitRef   (Value* val, 
324                                           bool isDef=false,
325                                           bool isDefAndUse=false);
326   
327   void                  setImplicitRef   (unsigned i,
328                                           Value* val, 
329                                           bool isDef=false,
330                                           bool isDefAndUse=false);
331   
332   // Replaces the Value for the operand with its allocated
333   // physical register after register allocation is complete.
334   // 
335   void                  SetRegForOperand(unsigned i, int regNum);
336   
337   //
338   // Iterator to enumerate machine operands.
339   // 
340   template<class MITy, class VTy>
341   class ValOpIterator : public std::forward_iterator<VTy, ptrdiff_t> {
342     unsigned i;
343     MITy MI;
344     
345     inline void skipToNextVal() {
346       while (i < MI->getNumOperands() &&
347              !((MI->getOperand(i).getOperandType() == MachineOperand::MO_VirtualRegister ||
348                 MI->getOperand(i).getOperandType() == MachineOperand::MO_CCRegister)
349                && MI->getOperand(i).getVRegValue() != 0))
350         ++i;
351     }
352   
353     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
354       skipToNextVal();
355     }
356   
357   public:
358     typedef ValOpIterator<MITy, VTy> _Self;
359     
360     inline VTy operator*() const { return MI->getOperand(i).getVRegValue(); }
361
362     const MachineOperand &getMachineOperand() const {
363       return MI->getOperand(i);
364     }
365
366     inline VTy operator->() const { return operator*(); }
367     
368     inline bool isDef() const { return MI->getOperand(i).opIsDef(); } 
369     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse(); } 
370     
371     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
372     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
373
374     inline bool operator==(const _Self &y) const { 
375       return i == y.i;
376     }
377     inline bool operator!=(const _Self &y) const { 
378       return !operator==(y);
379     }
380
381     static _Self begin(MITy MI) {
382       return _Self(MI, 0);
383     }
384     static _Self end(MITy MI) {
385       return _Self(MI, MI->getNumOperands());
386     }
387   };
388
389   // define begin() and end()
390   val_op_iterator begin() { return val_op_iterator::begin(this); }
391   val_op_iterator end()   { return val_op_iterator::end(this); }
392
393   const_val_op_iterator begin() const {
394     return const_val_op_iterator::begin(this);
395   }
396   const_val_op_iterator end() const {
397     return const_val_op_iterator::end(this);
398   }
399 };
400
401
402 inline MachineOperand&
403 MachineInstr::getOperand(unsigned int i)
404 {
405   assert(i < operands.size() && "getOperand() out of range!");
406   return operands[i];
407 }
408
409 inline const MachineOperand&
410 MachineInstr::getOperand(unsigned int i) const
411 {
412   assert(i < operands.size() && "getOperand() out of range!");
413   return operands[i];
414 }
415
416 inline bool
417 MachineInstr::operandIsDefined(unsigned int i) const
418 {
419   return getOperand(i).opIsDef();
420 }
421
422 inline bool
423 MachineInstr::operandIsDefinedAndUsed(unsigned int i) const
424 {
425   return getOperand(i).opIsDefAndUse();
426 }
427
428 inline bool
429 MachineInstr::implicitRefIsDefined(unsigned int i) const
430 {
431   assert(i < implicitIsDef.size() && "operand out of range!");
432   return implicitIsDef[i];
433 }
434
435 inline bool
436 MachineInstr::implicitRefIsDefinedAndUsed(unsigned int i) const
437 {
438   assert(i < implicitIsDefAndUse.size() && "operand out of range!");
439   return implicitIsDefAndUse[i];
440 }
441
442 inline const Value*
443 MachineInstr::getImplicitRef(unsigned int i) const
444 {
445   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
446   return implicitRefs[i];
447 }
448
449 inline Value*
450 MachineInstr::getImplicitRef(unsigned int i)
451 {
452   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
453   return implicitRefs[i];
454 }
455
456 inline void
457 MachineInstr::addImplicitRef(Value* val, 
458                              bool isDef=false,
459                              bool isDefAndUse=false)
460 {
461   implicitRefs.push_back(val);
462   implicitIsDef.push_back(isDef);
463   implicitIsDefAndUse.push_back(isDefAndUse);
464 }
465
466 inline void
467 MachineInstr::setImplicitRef(unsigned int i,
468                              Value* val, 
469                              bool isDef=false,
470                              bool isDefAndUse=false)
471 {
472   assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
473   implicitRefs[i] = val;
474   implicitIsDef[i] = isDef;
475   implicitIsDefAndUse[i] = isDefAndUse;
476 }
477
478
479 //---------------------------------------------------------------------------
480 // Debugging Support
481 //---------------------------------------------------------------------------
482
483 std::ostream& operator<<    (std::ostream& os, const MachineInstr& minstr);
484
485 std::ostream& operator<<    (std::ostream& os, const MachineOperand& mop);
486                                          
487 void    PrintMachineInstructions(const Function *F);
488
489 #endif