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