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