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