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