Small space optimization: Make MachineOperands take 16 bytes instead of
[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
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   // Bit fields of the flags variable used for different operand properties
67   static const char DEFFLAG    = 0x1;  // this is a def of the operand
68   static const char DEFUSEFLAG = 0x2;  // this is both a def and a use
69   static const char HIFLAG32   = 0x4;  // operand is %hi32(value_or_immedVal)
70   static const char LOFLAG32   = 0x8;  // operand is %lo32(value_or_immedVal)
71   static const char HIFLAG64   = 0x10; // operand is %hi64(value_or_immedVal)
72   static const char LOFLAG64   = 0x20; // operand is %lo64(value_or_immedVal)
73   
74 private:
75   union {
76     Value*      value;          // BasicBlockVal for a label operand.
77                                 // ConstantVal for a non-address immediate.
78                                 // Virtual register for an SSA operand,
79                                 // including hidden operands required for
80                                 // the generated machine code.     
81     int64_t immedVal;           // constant value for an explicit constant
82   };
83
84   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
85   char flags;                   // see bit field definitions above
86   int regNum;                   // register number for an explicit register
87                                 // will be set for a value after reg allocation
88 public:
89   /*ctor*/              MachineOperand  ();
90   /*ctor*/              MachineOperand  (MachineOperandType operandType,
91                                          Value* _val);
92   /*copy ctor*/         MachineOperand  (const MachineOperand&);
93   /*dtor*/              ~MachineOperand () {}
94   
95   // Accessor methods.  Caller is responsible for checking the
96   // operand type before invoking the corresponding accessor.
97   // 
98   inline MachineOperandType getOperandType() const {
99     return opType;
100   }
101   inline Value*         getVRegValue    () const {
102     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
103            opType == MO_PCRelativeDisp);
104     return value;
105   }
106   inline Value*         getVRegValueOrNull() const {
107     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
108             opType == MO_PCRelativeDisp)? value : NULL;
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 check if a machine register has been allocated to this operand
138   inline bool   hasAllocatedReg() const {
139     return (regNum >= 0 &&
140             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
141              opType == MO_MachineRegister));
142   }
143
144   // used to get the reg number if when one is allocated
145   inline int  getAllocatedRegNum() const {
146     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
147            opType == MO_MachineRegister);
148     return regNum;
149   }
150
151   
152 public:
153   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
154
155 private:
156   // These functions are provided so that a vector of operands can be
157   // statically allocated and individual ones can be initialized later.
158   // Give class MachineInstr access to these functions.
159   // 
160   void                  Initialize      (MachineOperandType operandType,
161                                          Value* _val);
162   void                  InitializeConst (MachineOperandType operandType,
163                                          int64_t intValue);
164   void                  InitializeReg   (int regNum,
165                                          bool isCCReg);
166
167   // Construction methods needed for fine-grain control.
168   // These must be accessed via coresponding methods in MachineInstr.
169   void markDef()       { flags |= DEFFLAG; }
170   void markDefAndUse() { flags |= DEFUSEFLAG; }
171   void markHi32()      { flags |= HIFLAG32; }
172   void markLo32()      { flags |= LOFLAG32; }
173   void markHi64()      { flags |= HIFLAG64; }
174   void markLo64()      { flags |= LOFLAG64; }
175   
176   // Replaces the Value with its corresponding physical register after
177   // register allocation is complete
178   void setRegForValue(int reg) {
179     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
180            opType == MO_MachineRegister);
181     regNum = reg;
182   }
183   
184   friend class MachineInstr;
185 };
186
187
188 inline
189 MachineOperand::MachineOperand()
190   : immedVal(0), opType(MO_VirtualRegister), flags(0), regNum(-1)
191 {}
192
193 inline
194 MachineOperand::MachineOperand(MachineOperandType operandType,
195                                Value* _val)
196   : immedVal(0), opType(operandType), flags(0), regNum(-1)
197 {}
198
199 inline
200 MachineOperand::MachineOperand(const MachineOperand& mo)
201   : opType(mo.opType), 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,         // MachineInstrs are annotable
271                       public NonCopyable {      // Disable copy operations
272   MachineOpCode    opCode;              // the opcode
273   OpCodeMask       opCodeMask;          // extra bits for variants of an opcode
274   std::vector<MachineOperand> operands; // the operands
275   std::vector<Value*>   implicitRefs;   // values implicitly referenced by this
276   std::vector<bool>     implicitIsDef;  //  machine instruction (eg, call args)
277   std::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
289   // 
290   // Support to rewrite a machine instruction in place: for now, simply
291   // replace() and then set new operands with Set.*Operand methods below.
292   // 
293   void                  replace         (MachineOpCode _opCode,
294                                          unsigned       numOperands,
295                                          OpCodeMask    _opCodeMask = 0x0);
296   
297   //
298   // The op code.  Note that MachineOpCode is a target-specific type.
299   // 
300   const MachineOpCode   getOpCode       () const { return opCode; }
301
302   //
303   // Information about explicit operands of the instruction
304   // 
305   unsigned int          getNumOperands  () const { return operands.size(); }
306   
307   bool                  operandIsDefined(unsigned i) const;
308   bool                  operandIsDefinedAndUsed(unsigned i) const;
309   
310   const MachineOperand& getOperand      (unsigned i) const;
311         MachineOperand& getOperand      (unsigned i);
312   
313   //
314   // Information about implicit operands of the instruction
315   // 
316   unsigned              getNumImplicitRefs() const{return implicitRefs.size();}
317   
318   bool                  implicitRefIsDefined(unsigned i) const;
319   bool                  implicitRefIsDefinedAndUsed(unsigned i) const;
320   
321   const Value*          getImplicitRef  (unsigned i) const;
322         Value*          getImplicitRef  (unsigned i);
323   
324   //
325   // Information about registers used in this instruction
326   // 
327   const hash_set<int>&  getRegsUsed    () const { return regsUsed; }
328         hash_set<int>&  getRegsUsed    ()       { return regsUsed; }
329   
330   //
331   // Debugging support
332   // 
333   void                  dump            () const;
334   friend std::ostream& operator<<       (std::ostream& os,
335                                          const MachineInstr& minstr);
336
337   //
338   // Define iterators to access the Value operands of the Machine Instruction.
339   // begin() and end() are defined to produce these iterators...
340   //
341   template<class _MI, class _V> class ValOpIterator;
342   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
343   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
344
345
346   // Access to set the operands when building the machine instruction
347   // 
348   void                  SetMachineOperandVal(unsigned i,
349                                              MachineOperand::MachineOperandType
350                                                operandType,
351                                              Value* _val,
352                                              bool isDef=false,
353                                              bool isDefAndUse=false);
354   void                  SetMachineOperandConst(unsigned i,
355                                            MachineOperand::MachineOperandType
356                                                  operandType,
357                                                int64_t intValue);
358   void                  SetMachineOperandReg(unsigned i, int regNum, 
359                                              bool isDef=false,
360                                              bool isDefAndUse=false,
361                                              bool isCCReg=false);
362   
363   void                  addImplicitRef   (Value* val, 
364                                           bool isDef=false,
365                                           bool isDefAndUse=false);
366   
367   void                  setImplicitRef   (unsigned i,
368                                           Value* val, 
369                                           bool isDef=false,
370                                           bool isDefAndUse=false);
371
372   unsigned              substituteValue  (const Value* oldVal,
373                                           Value* newVal,
374                                           bool defsOnly = true);
375
376   void                  setOperandHi32   (unsigned i);
377   void                  setOperandLo32   (unsigned i);
378   void                  setOperandHi64   (unsigned i);
379   void                  setOperandLo64   (unsigned i);
380   
381   
382   // Replaces the Value for the operand with its allocated
383   // physical register after register allocation is complete.
384   // 
385   void                  SetRegForOperand(unsigned i, int regNum);
386   
387   //
388   // Iterator to enumerate machine operands.
389   // 
390   template<class MITy, class VTy>
391   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
392     unsigned i;
393     MITy MI;
394     
395     inline void skipToNextVal() {
396       while (i < MI->getNumOperands() &&
397              !((MI->getOperand(i).getOperandType() == MachineOperand::MO_VirtualRegister ||
398                 MI->getOperand(i).getOperandType() == MachineOperand::MO_CCRegister)
399                && MI->getOperand(i).getVRegValue() != 0))
400         ++i;
401     }
402   
403     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
404       skipToNextVal();
405     }
406   
407   public:
408     typedef ValOpIterator<MITy, VTy> _Self;
409     
410     inline VTy operator*() const {
411       return MI->getOperand(i).getVRegValue();
412     }
413
414     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
415           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
416
417     inline VTy operator->() const { return operator*(); }
418
419     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
420     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
421
422     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
423     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
424
425     inline bool operator==(const _Self &y) const { 
426       return i == y.i;
427     }
428     inline bool operator!=(const _Self &y) const { 
429       return !operator==(y);
430     }
431
432     static _Self begin(MITy MI) {
433       return _Self(MI, 0);
434     }
435     static _Self end(MITy MI) {
436       return _Self(MI, MI->getNumOperands());
437     }
438   };
439
440   // define begin() and end()
441   val_op_iterator begin() { return val_op_iterator::begin(this); }
442   val_op_iterator end()   { return val_op_iterator::end(this); }
443
444   const_val_op_iterator begin() const {
445     return const_val_op_iterator::begin(this);
446   }
447   const_val_op_iterator end() const {
448     return const_val_op_iterator::end(this);
449   }
450 };
451
452
453 inline MachineOperand&
454 MachineInstr::getOperand(unsigned int i)
455 {
456   assert(i < operands.size() && "getOperand() out of range!");
457   return operands[i];
458 }
459
460 inline const MachineOperand&
461 MachineInstr::getOperand(unsigned int i) const
462 {
463   assert(i < operands.size() && "getOperand() out of range!");
464   return operands[i];
465 }
466
467 inline bool
468 MachineInstr::operandIsDefined(unsigned int i) const
469 {
470   return getOperand(i).opIsDef();
471 }
472
473 inline bool
474 MachineInstr::operandIsDefinedAndUsed(unsigned int i) const
475 {
476   return getOperand(i).opIsDefAndUse();
477 }
478
479 inline bool
480 MachineInstr::implicitRefIsDefined(unsigned int i) const
481 {
482   assert(i < implicitIsDef.size() && "operand out of range!");
483   return implicitIsDef[i];
484 }
485
486 inline bool
487 MachineInstr::implicitRefIsDefinedAndUsed(unsigned int i) const
488 {
489   assert(i < implicitIsDefAndUse.size() && "operand out of range!");
490   return implicitIsDefAndUse[i];
491 }
492
493 inline const Value*
494 MachineInstr::getImplicitRef(unsigned int i) const
495 {
496   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
497   return implicitRefs[i];
498 }
499
500 inline Value*
501 MachineInstr::getImplicitRef(unsigned int i)
502 {
503   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
504   return implicitRefs[i];
505 }
506
507 inline void
508 MachineInstr::addImplicitRef(Value* val, 
509                              bool isDef,
510                              bool isDefAndUse)
511 {
512   implicitRefs.push_back(val);
513   implicitIsDef.push_back(isDef);
514   implicitIsDefAndUse.push_back(isDefAndUse);
515 }
516
517 inline void
518 MachineInstr::setImplicitRef(unsigned int i,
519                              Value* val, 
520                              bool isDef,
521                              bool isDefAndUse)
522 {
523   assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
524   implicitRefs[i] = val;
525   implicitIsDef[i] = isDef;
526   implicitIsDefAndUse[i] = isDefAndUse;
527 }
528
529 inline void
530 MachineInstr::setOperandHi32(unsigned i)
531 {
532   operands[i].markHi32();
533 }
534
535 inline void
536 MachineInstr::setOperandLo32(unsigned i)
537 {
538   operands[i].markLo32();
539 }
540
541 inline void
542 MachineInstr::setOperandHi64(unsigned i)
543 {
544   operands[i].markHi64();
545 }
546
547 inline void
548 MachineInstr::setOperandLo64(unsigned i)
549 {
550   operands[i].markLo64();
551 }
552
553
554 //---------------------------------------------------------------------------
555 // Debugging Support
556 //---------------------------------------------------------------------------
557
558 std::ostream& operator<<    (std::ostream& os, const MachineInstr& minstr);
559
560 std::ostream& operator<<    (std::ostream& os, const MachineOperand& mop);
561                                          
562 void    PrintMachineInstructions(const Function *F);
563
564 #endif