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