Make MachineInstr.h not #include MachineInstrInfo.h
[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   /*ctor*/              MachineOperand  ();
95   /*ctor*/              MachineOperand  (MachineOperandType operandType,
96                                          Value* _val);
97   /*copy ctor*/         MachineOperand  (const MachineOperand&);
98   /*dtor*/              ~MachineOperand () {}
99   
100   // Accessor methods.  Caller is responsible for checking the
101   // operand type before invoking the corresponding accessor.
102   // 
103   inline MachineOperandType getOperandType() const {
104     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   inline bool           opIsDef         () const {
124     return flags & DEFFLAG;
125   }
126   inline bool           opIsDefAndUse   () const {
127     return flags & DEFUSEFLAG;
128   }
129   inline bool           opHiBits32      () const {
130     return flags & HIFLAG32;
131   }
132   inline bool           opLoBits32      () const {
133     return flags & LOFLAG32;
134   }
135   inline bool           opHiBits64      () const {
136     return flags & HIFLAG64;
137   }
138   inline bool           opLoBits64      () const {
139     return flags & LOFLAG64;
140   }
141
142   // used to check if a machine register has been allocated to this operand
143   inline bool   hasAllocatedReg() const {
144     return (regNum >= 0 &&
145             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
146              opType == MO_MachineRegister));
147   }
148
149   // used to get the reg number if when one is allocated
150   inline int  getAllocatedRegNum() const {
151     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
152            opType == MO_MachineRegister);
153     return regNum;
154   }
155
156   
157 public:
158   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
159
160 private:
161   // These functions are provided so that a vector of operands can be
162   // statically allocated and individual ones can be initialized later.
163   // Give class MachineInstr access to these functions.
164   // 
165   void                  Initialize      (MachineOperandType operandType,
166                                          Value* _val);
167   void                  InitializeConst (MachineOperandType operandType,
168                                          int64_t intValue);
169   void                  InitializeReg   (int regNum,
170                                          bool isCCReg);
171
172   // Construction methods needed for fine-grain control.
173   // These must be accessed via coresponding methods in MachineInstr.
174   void markDef()       { flags |= DEFFLAG; }
175   void markDefAndUse() { flags |= DEFUSEFLAG; }
176   void markHi32()      { flags |= HIFLAG32; }
177   void markLo32()      { flags |= LOFLAG32; }
178   void markHi64()      { flags |= HIFLAG64; }
179   void markLo64()      { flags |= LOFLAG64; }
180   
181   // Replaces the Value with its corresponding physical register after
182   // register allocation is complete
183   void setRegForValue(int reg) {
184     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
185            opType == MO_MachineRegister);
186     regNum = reg;
187   }
188   
189   friend class MachineInstr;
190 };
191
192
193 inline
194 MachineOperand::MachineOperand()
195   : immedVal(0), opType(MO_VirtualRegister), flags(0), regNum(-1)
196 {}
197
198 inline
199 MachineOperand::MachineOperand(MachineOperandType operandType,
200                                Value* _val)
201   : immedVal(0), opType(operandType), flags(0), regNum(-1)
202 {}
203
204 inline
205 MachineOperand::MachineOperand(const MachineOperand& mo)
206   : opType(mo.opType), flags(mo.flags)
207 {
208   switch(opType) {
209   case MO_VirtualRegister:
210   case MO_CCRegister:           value = mo.value; break;
211   case MO_MachineRegister:      regNum = mo.regNum; break;
212   case MO_SignExtendedImmed:
213   case MO_UnextendedImmed:
214   case MO_PCRelativeDisp:       immedVal = mo.immedVal; break;
215   default: assert(0);
216   }
217 }
218
219 inline void
220 MachineOperand::Initialize(MachineOperandType operandType,
221                            Value* _val)
222 {
223   opType = operandType;
224   value = _val;
225   regNum = -1;
226   flags = 0;
227 }
228
229 inline void
230 MachineOperand::InitializeConst(MachineOperandType operandType,
231                                 int64_t intValue)
232 {
233   opType = operandType;
234   value = NULL;
235   immedVal = intValue;
236   regNum = -1;
237   flags = 0;
238 }
239
240 inline void
241 MachineOperand::InitializeReg(int _regNum, bool isCCReg)
242 {
243   opType = isCCReg? MO_CCRegister : MO_MachineRegister;
244   value = NULL;
245   regNum = (int) _regNum;
246   flags = 0;
247 }
248
249
250 //---------------------------------------------------------------------------
251 // class MachineInstr 
252 // 
253 // Purpose:
254 //   Representation of each machine instruction.
255 // 
256 //   MachineOpCode must be an enum, defined separately for each target.
257 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
258 // 
259 //   opCodeMask is used to record variants of an instruction.
260 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
261 //      ANNUL:             if 1: Annul delay slot instruction.
262 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
263 //   Instead of creating 4 different opcodes for BNZ, we create a single
264 //   opcode and set bits in opCodeMask for each of these flags.
265 //
266 //  There are 2 kinds of operands:
267 // 
268 //  (1) Explicit operands of the machine instruction in vector operands[] 
269 // 
270 //  (2) "Implicit operands" are values implicitly used or defined by the
271 //      machine instruction, such as arguments to a CALL, return value of
272 //      a CALL (if any), and return value of a RETURN.
273 //---------------------------------------------------------------------------
274
275 class MachineInstr : public Annotable,         // MachineInstrs are annotable
276                      public NonCopyable {      // Disable copy operations
277   MachineOpCode    opCode;              // the opcode
278   OpCodeMask       opCodeMask;          // extra bits for variants of an opcode
279   std::vector<MachineOperand> operands; // the operands
280
281   struct ImplicitRef {
282     Value *Val;
283     bool isDef, isDefAndUse;
284
285     ImplicitRef(Value *V, bool D, bool DU) : Val(V), isDef(D), isDefAndUse(DU){}
286   };
287
288   // implicitRefs - Values implicitly referenced by this machine instruction
289   // (eg, call args)
290   std::vector<ImplicitRef> implicitRefs;
291
292   // regsUsed - all machine registers used for this instruction, including regs
293   // used to save values across the instruction.  This is a bitset of registers.
294   std::vector<bool> regsUsed;
295 public:
296   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
297                                          OpCodeMask    _opCodeMask = 0x0);
298   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
299                                          unsigned       numOperands,
300                                          OpCodeMask    _opCodeMask = 0x0);
301   inline                ~MachineInstr   () {}
302
303   // 
304   // Support to rewrite a machine instruction in place: for now, simply
305   // replace() and then set new operands with Set.*Operand methods below.
306   // 
307   void                  replace         (MachineOpCode _opCode,
308                                          unsigned       numOperands,
309                                          OpCodeMask    _opCodeMask = 0x0);
310   
311   //
312   // The op code.  Note that MachineOpCode is a target-specific type.
313   // 
314   const MachineOpCode   getOpCode       () const { return opCode; }
315
316   //
317   // Information about explicit operands of the instruction
318   // 
319   unsigned int          getNumOperands  () const { return operands.size(); }
320   
321   bool                  operandIsDefined(unsigned i) const;
322   bool                  operandIsDefinedAndUsed(unsigned i) const;
323   
324   const MachineOperand& getOperand      (unsigned i) const;
325         MachineOperand& getOperand      (unsigned i);
326   
327   //
328   // Information about implicit operands of the instruction
329   // 
330   unsigned              getNumImplicitRefs() const{ return implicitRefs.size();}
331   
332   bool                  implicitRefIsDefined(unsigned i) const;
333   bool                  implicitRefIsDefinedAndUsed(unsigned i) const;
334   
335   const Value*          getImplicitRef  (unsigned i) const;
336         Value*          getImplicitRef  (unsigned i);
337   
338   //
339   // Information about registers used in this instruction
340   // 
341   const std::vector<bool> &getRegsUsed    () const { return regsUsed; }
342   
343   // insertUsedReg - Add a register to the Used registers set...
344   void insertUsedReg(unsigned Reg) {
345     if (Reg >= regsUsed.size())
346       regsUsed.resize(Reg+1);
347     regsUsed[Reg] = true;
348   }
349
350   //
351   // Debugging support
352   // 
353   void                  dump            () const;
354   friend std::ostream& operator<<       (std::ostream& os,
355                                          const MachineInstr& minstr);
356
357   //
358   // Define iterators to access the Value operands of the Machine Instruction.
359   // begin() and end() are defined to produce these iterators...
360   //
361   template<class _MI, class _V> class ValOpIterator;
362   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
363   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
364
365
366   // Access to set the operands when building the machine instruction
367   // 
368   void                  SetMachineOperandVal(unsigned i,
369                                              MachineOperand::MachineOperandType
370                                                operandType,
371                                              Value* _val,
372                                              bool isDef=false,
373                                              bool isDefAndUse=false);
374   void                  SetMachineOperandConst(unsigned i,
375                                            MachineOperand::MachineOperandType
376                                                  operandType,
377                                                int64_t intValue);
378   void                  SetMachineOperandReg(unsigned i, int regNum, 
379                                              bool isDef=false,
380                                              bool isDefAndUse=false,
381                                              bool isCCReg=false);
382   
383   void                  addImplicitRef   (Value* val, 
384                                           bool isDef=false,
385                                           bool isDefAndUse=false);
386   
387   void                  setImplicitRef   (unsigned i,
388                                           Value* val, 
389                                           bool isDef=false,
390                                           bool isDefAndUse=false);
391
392   unsigned              substituteValue  (const Value* oldVal,
393                                           Value* newVal,
394                                           bool defsOnly = true);
395
396   void                  setOperandHi32   (unsigned i);
397   void                  setOperandLo32   (unsigned i);
398   void                  setOperandHi64   (unsigned i);
399   void                  setOperandLo64   (unsigned i);
400   
401   
402   // Replaces the Value for the operand with its allocated
403   // physical register after register allocation is complete.
404   // 
405   void                  SetRegForOperand(unsigned i, int regNum);
406   
407   //
408   // Iterator to enumerate machine operands.
409   // 
410   template<class MITy, class VTy>
411   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
412     unsigned i;
413     MITy MI;
414     
415     inline void skipToNextVal() {
416       while (i < MI->getNumOperands() &&
417              !((MI->getOperand(i).getOperandType() == MachineOperand::MO_VirtualRegister ||
418                 MI->getOperand(i).getOperandType() == MachineOperand::MO_CCRegister)
419                && MI->getOperand(i).getVRegValue() != 0))
420         ++i;
421     }
422   
423     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
424       skipToNextVal();
425     }
426   
427   public:
428     typedef ValOpIterator<MITy, VTy> _Self;
429     
430     inline VTy operator*() const {
431       return MI->getOperand(i).getVRegValue();
432     }
433
434     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
435           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
436
437     inline VTy operator->() const { return operator*(); }
438
439     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
440     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
441
442     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
443     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
444
445     inline bool operator==(const _Self &y) const { 
446       return i == y.i;
447     }
448     inline bool operator!=(const _Self &y) const { 
449       return !operator==(y);
450     }
451
452     static _Self begin(MITy MI) {
453       return _Self(MI, 0);
454     }
455     static _Self end(MITy MI) {
456       return _Self(MI, MI->getNumOperands());
457     }
458   };
459
460   // define begin() and end()
461   val_op_iterator begin() { return val_op_iterator::begin(this); }
462   val_op_iterator end()   { return val_op_iterator::end(this); }
463
464   const_val_op_iterator begin() const {
465     return const_val_op_iterator::begin(this);
466   }
467   const_val_op_iterator end() const {
468     return const_val_op_iterator::end(this);
469   }
470 };
471
472
473 inline MachineOperand&
474 MachineInstr::getOperand(unsigned int i)
475 {
476   assert(i < operands.size() && "getOperand() out of range!");
477   return operands[i];
478 }
479
480 inline const MachineOperand&
481 MachineInstr::getOperand(unsigned int i) const
482 {
483   assert(i < operands.size() && "getOperand() out of range!");
484   return operands[i];
485 }
486
487 inline bool
488 MachineInstr::operandIsDefined(unsigned int i) const
489 {
490   return getOperand(i).opIsDef();
491 }
492
493 inline bool
494 MachineInstr::operandIsDefinedAndUsed(unsigned int i) const
495 {
496   return getOperand(i).opIsDefAndUse();
497 }
498
499 inline bool
500 MachineInstr::implicitRefIsDefined(unsigned i) const
501 {
502   assert(i < implicitRefs.size() && "operand out of range!");
503   return implicitRefs[i].isDef;
504 }
505
506 inline bool
507 MachineInstr::implicitRefIsDefinedAndUsed(unsigned i) const
508 {
509   assert(i < implicitRefs.size() && "operand out of range!");
510   return implicitRefs[i].isDefAndUse;
511 }
512
513 inline const Value*
514 MachineInstr::getImplicitRef(unsigned i) const
515 {
516   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
517   return implicitRefs[i].Val;
518 }
519
520 inline Value*
521 MachineInstr::getImplicitRef(unsigned i)
522 {
523   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
524   return implicitRefs[i].Val;
525 }
526
527 inline void
528 MachineInstr::addImplicitRef(Value* val, 
529                              bool isDef,
530                              bool isDefAndUse) {
531   implicitRefs.push_back(ImplicitRef(val, isDef, isDefAndUse));
532 }
533
534 inline void
535 MachineInstr::setImplicitRef(unsigned int i,
536                              Value* val, 
537                              bool isDef,
538                              bool isDefAndUse)
539 {
540   assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
541   implicitRefs[i].Val = val;
542   implicitRefs[i].isDef = isDef;
543   implicitRefs[i].isDefAndUse = isDefAndUse;
544 }
545
546 inline void
547 MachineInstr::setOperandHi32(unsigned i)
548 {
549   operands[i].markHi32();
550 }
551
552 inline void
553 MachineInstr::setOperandLo32(unsigned i)
554 {
555   operands[i].markLo32();
556 }
557
558 inline void
559 MachineInstr::setOperandHi64(unsigned i)
560 {
561   operands[i].markHi64();
562 }
563
564 inline void
565 MachineInstr::setOperandLo64(unsigned i)
566 {
567   operands[i].markLo64();
568 }
569
570
571 //---------------------------------------------------------------------------
572 // Debugging Support
573 //---------------------------------------------------------------------------
574
575 std::ostream& operator<<    (std::ostream& os, const MachineInstr& minstr);
576
577 std::ostream& operator<<    (std::ostream& os, const MachineOperand& mop);
578                                          
579 void    PrintMachineInstructions(const Function *F);
580
581 #endif