Remove separate vector of implicit refs from MachineInstr, and
[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
21 //---------------------------------------------------------------------------
22 // class MachineOperand 
23 // 
24 // Purpose:
25 //   Representation of each machine instruction operand.
26 //   This class is designed so that you can allocate a vector of operands
27 //   first and initialize each one later.
28 //
29 //   E.g, for this VM instruction:
30 //              ptr = alloca type, numElements
31 //   we generate 2 machine instructions on the SPARC:
32 // 
33 //              mul Constant, Numelements -> Reg
34 //              add %sp, Reg -> Ptr
35 // 
36 //   Each instruction has 3 operands, listed above.  Of those:
37 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
38 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
39 //      
40 //   For the register operands, the virtual register type is as follows:
41 //      
42 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
43 //      MachineInstr* minstr will point to the instruction that computes reg.
44 // 
45 //   -  %sp will be of virtual register type MO_MachineReg.
46 //      The field regNum identifies the machine register.
47 // 
48 //   -  NumElements will be of virtual register type MO_VirtualReg.
49 //      The field Value* value identifies the value.
50 // 
51 //   -  Ptr will also be of virtual register type MO_VirtualReg.
52 //      Again, the field Value* value identifies the value.
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   union {
78     Value*      value;          // BasicBlockVal for a label operand.
79                                 // ConstantVal for a non-address immediate.
80                                 // Virtual register for an SSA operand,
81                                 // including hidden operands required for
82                                 // the generated machine code.     
83     int64_t immedVal;           // constant value for an explicit constant
84   };
85
86   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
87   char flags;                   // see bit field definitions above
88   int regNum;                   // register number for an explicit register
89                                 // will be set for a value after reg allocation
90 private:
91   MachineOperand()
92     : immedVal(0),
93       opType(MO_VirtualRegister),
94       flags(0),
95       regNum(-1) {}
96
97   MachineOperand(int64_t ImmVal, MachineOperandType OpTy)
98     : immedVal(ImmVal),
99       opType(OpTy),
100       flags(0),
101       regNum(-1) {}
102
103   MachineOperand(int Reg, MachineOperandType OpTy, bool isDef = false)
104     : immedVal(0),
105       opType(OpTy),
106       flags(isDef ? DEFFLAG : 0),
107       regNum(Reg) {}
108
109   MachineOperand(Value *V, MachineOperandType OpTy,
110                  bool isDef = false, bool isDNU = false)
111     : value(V),
112       opType(OpTy),
113       regNum(-1) {
114     flags = (isDef ? DEFFLAG : 0) | (isDNU ? DEFUSEFLAG : 0);
115   }
116
117 public:
118   MachineOperand(const MachineOperand &M)
119     : immedVal(M.immedVal),
120       opType(M.opType),
121       flags(M.flags),
122       regNum(M.regNum) {}
123
124   ~MachineOperand() {}
125   
126   // Accessor methods.  Caller is responsible for checking the
127   // operand type before invoking the corresponding accessor.
128   // 
129   MachineOperandType getType() const { return opType; }
130
131   inline Value*         getVRegValue    () const {
132     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
133            opType == MO_PCRelativeDisp);
134     return value;
135   }
136   inline Value*         getVRegValueOrNull() const {
137     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
138             opType == MO_PCRelativeDisp)? value : NULL;
139   }
140   inline int            getMachineRegNum() const {
141     assert(opType == MO_MachineRegister);
142     return regNum;
143   }
144   inline int64_t        getImmedValue   () const {
145     assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
146     return immedVal;
147   }
148   bool          opIsDef         () const { return flags & DEFFLAG; }
149   bool          opIsDefAndUse   () const { return flags & DEFUSEFLAG; }
150   bool          opHiBits32      () const { return flags & HIFLAG32; }
151   bool          opLoBits32      () const { return flags & LOFLAG32; }
152   bool          opHiBits64      () const { return flags & HIFLAG64; }
153   bool          opLoBits64      () const { return flags & LOFLAG64; }
154
155   // used to check if a machine register has been allocated to this operand
156   inline bool   hasAllocatedReg() const {
157     return (regNum >= 0 &&
158             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
159              opType == MO_MachineRegister));
160   }
161
162   // used to get the reg number if when one is allocated
163   inline int  getAllocatedRegNum() const {
164     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
165            opType == MO_MachineRegister);
166     return regNum;
167   }
168
169   
170   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
171
172 private:
173
174   // Construction methods needed for fine-grain control.
175   // These must be accessed via coresponding methods in MachineInstr.
176   void markDef()       { flags |= DEFFLAG; }
177   void markDefAndUse() { flags |= DEFUSEFLAG; }
178   void markHi32()      { flags |= HIFLAG32; }
179   void markLo32()      { flags |= LOFLAG32; }
180   void markHi64()      { flags |= HIFLAG64; }
181   void markLo64()      { flags |= LOFLAG64; }
182   
183   // Replaces the Value with its corresponding physical register after
184   // register allocation is complete
185   void setRegForValue(int reg) {
186     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
187            opType == MO_MachineRegister);
188     regNum = reg;
189   }
190   
191   friend class MachineInstr;
192 };
193
194
195 //---------------------------------------------------------------------------
196 // class MachineInstr 
197 // 
198 // Purpose:
199 //   Representation of each machine instruction.
200 // 
201 //   MachineOpCode must be an enum, defined separately for each target.
202 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
203 // 
204 //  There are 2 kinds of operands:
205 // 
206 //  (1) Explicit operands of the machine instruction in vector operands[] 
207 // 
208 //  (2) "Implicit operands" are values implicitly used or defined by the
209 //      machine instruction, such as arguments to a CALL, return value of
210 //      a CALL (if any), and return value of a RETURN.
211 //---------------------------------------------------------------------------
212
213 class MachineInstr: public NonCopyable {      // Disable copy operations
214
215   MachineOpCode    opCode;              // the opcode
216   std::vector<MachineOperand> operands; // the operands
217   unsigned numImplicitRefs;             // number of implicit operands
218
219   MachineOperand& getImplicitOp(unsigned i) {
220     assert(i < numImplicitRefs && "implicit ref# out of range!");
221     return operands[i + operands.size() - numImplicitRefs];
222   }
223   const MachineOperand& getImplicitOp(unsigned i) const {
224     assert(i < numImplicitRefs && "implicit ref# out of range!");
225     return operands[i + operands.size() - numImplicitRefs];
226   }
227
228   // regsUsed - all machine registers used for this instruction, including regs
229   // used to save values across the instruction.  This is a bitset of registers.
230   std::vector<bool> regsUsed;
231
232   // OperandComplete - Return true if it's illegal to add a new operand
233   bool OperandsComplete() const;
234
235 public:
236   MachineInstr(MachineOpCode Opcode);
237   MachineInstr(MachineOpCode Opcode, unsigned numOperands);
238
239   /// MachineInstr ctor - This constructor only does a _reserve_ of the
240   /// operands, not a resize for them.  It is expected that if you use this that
241   /// you call add* methods below to fill up the operands, instead of the Set
242   /// methods.
243   ///
244   MachineInstr(MachineOpCode Opcode, unsigned numOperands, bool XX, bool YY);
245
246   // 
247   // Support to rewrite a machine instruction in place: for now, simply
248   // replace() and then set new operands with Set.*Operand methods below.
249   // 
250   void replace(MachineOpCode Opcode, unsigned numOperands);
251   
252   //
253   // The opcode.
254   // 
255   const MachineOpCode getOpCode() const { return opCode; }
256
257   //
258   // Information about explicit operands of the instruction
259   // 
260   unsigned getNumOperands() const { return operands.size() - numImplicitRefs; }
261   
262   const MachineOperand& getOperand(unsigned i) const {
263     assert(i < getNumOperands() && "getOperand() out of range!");
264     return operands[i];
265   }
266   MachineOperand& getOperand(unsigned i) {
267     assert(i < getNumOperands() && "getOperand() out of range!");
268     return operands[i];
269   }
270
271   MachineOperand::MachineOperandType getOperandType(unsigned i) const {
272     return getOperand(i).getType();
273   }
274
275   bool operandIsDefined(unsigned i) const {
276     return getOperand(i).opIsDef();
277   }
278
279   bool operandIsDefinedAndUsed(unsigned i) const {
280     return getOperand(i).opIsDefAndUse();
281   }
282
283   //
284   // Information about implicit operands of the instruction
285   // 
286   unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
287   
288   const Value* getImplicitRef(unsigned i) const {
289     return getImplicitOp(i).getVRegValue();
290   }
291   Value* getImplicitRef(unsigned i) {
292     return getImplicitOp(i).getVRegValue();
293   }
294
295   bool implicitRefIsDefined(unsigned i) const {
296     return getImplicitOp(i).opIsDef();
297   }
298   bool implicitRefIsDefinedAndUsed(unsigned i) const {
299     return getImplicitOp(i).opIsDefAndUse();
300   }
301   inline void addImplicitRef    (Value* V,
302                                  bool isDef=false,bool isDefAndUse=false);
303   inline void setImplicitRef    (unsigned i, Value* V,
304                                  bool isDef=false, bool isDefAndUse=false);
305
306   //
307   // Information about registers used in this instruction
308   // 
309   const std::vector<bool> &getRegsUsed() const { return regsUsed; }
310   
311   // insertUsedReg - Add a register to the Used registers set...
312   void insertUsedReg(unsigned Reg) {
313     if (Reg >= regsUsed.size())
314       regsUsed.resize(Reg+1);
315     regsUsed[Reg] = true;
316   }
317
318   //
319   // Debugging support
320   // 
321   void dump() const;
322   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
323
324   //
325   // Define iterators to access the Value operands of the Machine Instruction.
326   // Note that these iterators only enumerate the explicit operands.
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   // Access to set the operands when building the machine instruction
334   // 
335   void SetMachineOperandVal     (unsigned i,
336                                  MachineOperand::MachineOperandType operandType,
337                                  Value* V,
338                                  bool isDef=false,
339                                  bool isDefAndUse=false);
340
341   void SetMachineOperandConst   (unsigned i,
342                                  MachineOperand::MachineOperandType operandType,
343                                  int64_t intValue);
344
345   void SetMachineOperandReg     (unsigned i,
346                                  int regNum,
347                                  bool isDef=false);
348
349   //===--------------------------------------------------------------------===//
350   // Accessors to add operands when building up machine instructions
351   //
352
353   /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
354   /// operands list...
355   ///
356   void addRegOperand(Value *V, bool isDef=false, bool isDefAndUse=false) {
357     assert(!OperandsComplete() &&
358            "Trying to add an operand to a machine instr that is already done!");
359     operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
360                                       isDef, isDefAndUse));
361   }
362
363   /// addRegOperand - Add a symbolic virtual register reference...
364   ///
365   void addRegOperand(int reg) {
366     assert(!OperandsComplete() &&
367            "Trying to add an operand to a machine instr that is already done!");
368     operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister));
369   }
370
371   /// addPCDispOperand - Add a PC relative displacement operand to the MI
372   ///
373   void addPCDispOperand(Value *V) {
374     assert(!OperandsComplete() &&
375            "Trying to add an operand to a machine instr that is already done!");
376     operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp));
377   }
378
379   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
380   ///
381   void addMachineRegOperand(int reg, bool isDef=false) {
382     assert(!OperandsComplete() &&
383            "Trying to add an operand to a machine instr that is already done!");
384     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
385                                       isDef));
386     insertUsedReg(reg);
387   }
388
389   /// addZeroExtImmOperand - Add a zero extended constant argument to the
390   /// machine instruction.
391   ///
392   void addZeroExtImmOperand(int64_t intValue) {
393     assert(!OperandsComplete() &&
394            "Trying to add an operand to a machine instr that is already done!");
395     operands.push_back(MachineOperand(intValue,
396                                       MachineOperand::MO_UnextendedImmed));
397   }
398
399   /// addSignExtImmOperand - Add a zero extended constant argument to the
400   /// machine instruction.
401   ///
402   void addSignExtImmOperand(int64_t intValue) {
403     assert(!OperandsComplete() &&
404            "Trying to add an operand to a machine instr that is already done!");
405     operands.push_back(MachineOperand(intValue,
406                                       MachineOperand::MO_SignExtendedImmed));
407   }
408
409
410   unsigned substituteValue(const Value* oldVal, Value* newVal,
411                            bool defsOnly = true);
412
413   void setOperandHi32(unsigned i) { operands[i].markHi32(); }
414   void setOperandLo32(unsigned i) { operands[i].markLo32(); }
415   void setOperandHi64(unsigned i) { operands[i].markHi64(); }
416   void setOperandLo64(unsigned i) { operands[i].markLo64(); }
417   
418   
419   // SetRegForOperand - Replaces the Value for the operand with its allocated
420   // physical register after register allocation is complete.
421   // 
422   void SetRegForOperand(unsigned i, int regNum);
423
424   //
425   // Iterator to enumerate machine operands.
426   // 
427   template<class MITy, class VTy>
428   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
429     unsigned i;
430     MITy MI;
431     
432     void skipToNextVal() {
433       while (i < MI->getNumOperands() &&
434              !( (MI->getOperandType(i) == MachineOperand::MO_VirtualRegister ||
435                  MI->getOperandType(i) == MachineOperand::MO_CCRegister)
436                 && MI->getOperand(i).getVRegValue() != 0))
437         ++i;
438     }
439   
440     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
441       skipToNextVal();
442     }
443   
444   public:
445     typedef ValOpIterator<MITy, VTy> _Self;
446     
447     inline VTy operator*() const {
448       return MI->getOperand(i).getVRegValue();
449     }
450
451     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
452           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
453
454     inline VTy operator->() const { return operator*(); }
455
456     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
457     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
458
459     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
460     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
461
462     inline bool operator==(const _Self &y) const { 
463       return i == y.i;
464     }
465     inline bool operator!=(const _Self &y) const { 
466       return !operator==(y);
467     }
468
469     static _Self begin(MITy MI) {
470       return _Self(MI, 0);
471     }
472     static _Self end(MITy MI) {
473       return _Self(MI, MI->getNumOperands());
474     }
475   };
476
477   // define begin() and end()
478   val_op_iterator begin() { return val_op_iterator::begin(this); }
479   val_op_iterator end()   { return val_op_iterator::end(this); }
480
481   const_val_op_iterator begin() const {
482     return const_val_op_iterator::begin(this);
483   }
484   const_val_op_iterator end() const {
485     return const_val_op_iterator::end(this);
486   }
487 };
488
489
490 // Define here to enable inlining of the functions used.
491 // 
492 void MachineInstr::addImplicitRef(Value* V,
493                                   bool isDef,
494                                   bool isDefAndUse)
495 {
496   ++numImplicitRefs;
497   addRegOperand(V, isDef, isDefAndUse);
498 }
499
500 void MachineInstr::setImplicitRef(unsigned i,
501                                   Value* V,
502                                   bool isDef,
503                                   bool isDefAndUse)
504 {
505   assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
506   SetMachineOperandVal(i + getNumImplicitRefs(),
507                        MachineOperand::MO_VirtualRegister,
508                        V, isDef, isDefAndUse);
509 }
510
511
512 //---------------------------------------------------------------------------
513 // Debugging Support
514 //---------------------------------------------------------------------------
515
516 std::ostream& operator<<        (std::ostream& os,
517                                  const MachineInstr& minstr);
518
519 std::ostream& operator<<        (std::ostream& os,
520                                  const MachineOperand& mop);
521                                          
522 void PrintMachineInstructions   (const Function *F);
523
524 #endif