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