Committed for compliation. Not yet final.
[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/CodeGen/TargetMachine.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 class MachineOperand {
63 public:
64   enum MachineOperandType {
65     MO_VirtualRegister,         // virtual register for *value
66     MO_MachineRegister,         // pre-assigned machine register `regNum'
67     MO_CCRegister,
68     MO_SignExtendedImmed,
69     MO_UnextendedImmed,
70     MO_PCRelativeDisp,
71   };
72   
73 private:
74   MachineOperandType opType;
75   
76   union {
77     Value*      value;          // BasicBlockVal for a label operand.
78                                 // ConstantVal for a non-address immediate.
79                                 // Virtual register for an SSA operand,
80                                 // including hidden operands required for
81                                 // the generated machine code.
82     
83     unsigned int regNum;        // register number for an explicit register
84   
85     int64_t immedVal;           // constant value for an explicit constant
86   };
87
88   bool isDef;                   // is this a defition for the value
89                                 // made public for faster access
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     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   // this replaces a value with a register after register allcoation
143   void setRegForValue(int Reg) {
144     assert(opType == MO_VirtualRegister || opType == MO_CCRegister);
145     opType =  MO_MachineRegister;
146     regNum = Reg;
147   }
148
149 };
150
151
152 inline
153 MachineOperand::MachineOperand()
154   : opType(MO_VirtualRegister),
155     value(NULL),
156     regNum(0),
157     immedVal(0),
158     isDef(false)
159 {}
160
161 inline
162 MachineOperand::MachineOperand(MachineOperandType operandType,
163                                Value* _val)
164   : opType(operandType),
165     value(_val),
166     regNum(0),
167     immedVal(0),
168     isDef(false)
169 {}
170
171 inline
172 MachineOperand::MachineOperand(const MachineOperand& mo)
173   : opType(mo.opType),
174     isDef(false)
175 {
176   switch(opType) {
177   case MO_VirtualRegister:
178   case MO_CCRegister:           value = mo.value; break;
179   case MO_MachineRegister:      regNum = mo.regNum; break;
180   case MO_SignExtendedImmed:
181   case MO_UnextendedImmed:
182   case MO_PCRelativeDisp:       immedVal = mo.immedVal; break;
183   default: assert(0);
184   }
185 }
186
187 inline void
188 MachineOperand::Initialize(MachineOperandType operandType,
189                            Value* _val)
190 {
191   opType = operandType;
192   value = _val;
193 }
194
195 inline void
196 MachineOperand::InitializeConst(MachineOperandType operandType,
197                                 int64_t intValue)
198 {
199   opType = operandType;
200   value = NULL;
201   immedVal = intValue;
202 }
203
204 inline void
205 MachineOperand::InitializeReg(unsigned int _regNum)
206 {
207   opType = MO_MachineRegister;
208   value = NULL;
209   regNum = _regNum;
210 }
211
212
213 //---------------------------------------------------------------------------
214 // class MachineInstr 
215 // 
216 // Purpose:
217 //   Representation of each machine instruction.
218 // 
219 //   MachineOpCode must be an enum, defined separately for each target.
220 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
221 // 
222 //   opCodeMask is used to record variants of an instruction.
223 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
224 //      ANNUL:             if 1: Annul delay slot instruction.
225 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
226 //   Instead of creating 4 different opcodes for BNZ, we create a single
227 //   opcode and set bits in opCodeMask for each of these flags.
228 //---------------------------------------------------------------------------
229
230 class MachineInstr : public NonCopyable {
231 private:
232   MachineOpCode opCode;
233   OpCodeMask    opCodeMask;             // extra bits for variants of an opcode
234   vector<MachineOperand> operands;
235   
236 public:
237   typedef ValOpIterator<const MachineInstr, const Value> val_op_const_iterator;
238   typedef ValOpIterator<      MachineInstr,       Value> val_op_iterator;
239   
240 public:
241   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
242                                          OpCodeMask    _opCodeMask = 0x0);
243   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
244                                          unsigned       numOperands,
245                                          OpCodeMask    _opCodeMask = 0x0);
246   inline                ~MachineInstr   () {}
247   
248   const MachineOpCode   getOpCode       () const;
249   
250   unsigned int          getNumOperands  () const;
251   
252   const MachineOperand& getOperand      (unsigned int i) const;
253         MachineOperand& getOperand      (unsigned int i);
254   
255   bool                  operandIsDefined(unsigned int i) const;
256   
257   void                  dump            (unsigned int indent = 0) const;
258
259
260
261
262   
263 public:
264   friend ostream& operator<<(ostream& os, const MachineInstr& minstr);
265   friend val_op_const_iterator;
266   friend val_op_iterator;
267
268 public:
269   // Access to set the operands when building the machine instruction
270   void                  SetMachineOperand(unsigned int i,
271                               MachineOperand::MachineOperandType operandType,
272                               Value* _val, bool isDef=false);
273   void                  SetMachineOperand(unsigned int i,
274                               MachineOperand::MachineOperandType operandType,
275                               int64_t intValue, bool isDef=false);
276   void                  SetMachineOperand(unsigned int i,
277                                           unsigned int regNum, 
278                                           bool isDef=false);
279 };
280
281 inline const MachineOpCode
282 MachineInstr::getOpCode() const
283 {
284   return opCode;
285 }
286
287 inline unsigned int
288 MachineInstr::getNumOperands() const
289 {
290   return operands.size();
291 }
292
293 inline MachineOperand&
294 MachineInstr::getOperand(unsigned int i)
295 {
296   assert(i < operands.size() && "getOperand() out of range!");
297   return operands[i];
298 }
299
300 inline const MachineOperand&
301 MachineInstr::getOperand(unsigned int i) const
302 {
303   assert(i < operands.size() && "getOperand() out of range!");
304   return operands[i];
305 }
306
307 inline bool
308 MachineInstr::operandIsDefined(unsigned int i) const
309 {
310   return getOperand(i).opIsDef();
311 }
312
313
314 template<class _MI, class _V>
315 class ValOpIterator : public std::forward_iterator<_V, ptrdiff_t> {
316 private:
317   unsigned int i;
318   int resultPos;
319   _MI* minstr;
320   
321   inline void   skipToNextVal() {
322     while (i < minstr->getNumOperands() &&
323            ! ((minstr->operands[i].opType == MachineOperand::MO_VirtualRegister
324                || minstr->operands[i].opType == MachineOperand::MO_CCRegister)
325               && minstr->operands[i].value != NULL))
326       ++i;
327   }
328   
329 public:
330   typedef ValOpIterator<_MI, _V> _Self;
331   
332   inline ValOpIterator(_MI* _minstr) : i(0), minstr(_minstr) {
333     resultPos = TargetInstrDescriptors[minstr->opCode].resultPos;
334     skipToNextVal();
335   };
336   
337   inline _V*    operator*()  const { return minstr->getOperand(i).getVRegValue();}
338   inline _V*    operator->() const { return operator*(); }
339   //  inline bool       isDef   ()   const { return (((int) i) == resultPos); }
340   
341   inline bool   isDef   ()   const { return minstr->getOperand(i).isDef; } 
342   inline bool   done    ()   const { return (i == minstr->getNumOperands()); }
343   
344   inline _Self& operator++()       { i++; skipToNextVal(); return *this; }
345   inline _Self  operator++(int)    { _Self tmp = *this; ++*this; return tmp; }
346 };
347
348
349 //---------------------------------------------------------------------------
350 // class MachineCodeForVMInstr
351 // 
352 // Purpose:
353 //   Representation of the sequence of machine instructions created
354 //   for a single VM instruction.  Additionally records any temporary 
355 //   "values" used as intermediate values in this sequence.
356 //   Note that such values should be treated as pure SSA values with
357 //   no interpretation of their operands (i.e., as a TmpInstruction object
358 //   which actually represents such a value).
359 // 
360 //---------------------------------------------------------------------------
361
362 class MachineCodeForVMInstr: public vector<MachineInstr*>
363 {
364 private:
365   vector<Value*> tempVec;
366   
367 public:
368   /*ctor*/      MachineCodeForVMInstr   ()      {}
369   /*ctor*/      ~MachineCodeForVMInstr  ();
370   
371   const vector<Value*>&
372                 getTempValues           () const { return tempVec; }
373   
374   void          addTempValue            (Value* val)
375                                                  { tempVec.push_back(val); }
376
377   // dropAllReferences() - This function drops all references within
378   // temporary (hidden) instructions created in implementing the original
379   // VM intruction.  This ensures there are no remaining "uses" within
380   // these hidden instructions, before the values of a method are freed.
381   //
382   // Make this inline because it has to be called from class Instruction
383   // and inlining it avoids a serious circurality in link order.
384   inline void dropAllReferences() {
385     for (unsigned i=0, N=tempVec.size(); i < N; i++)
386     if (tempVec[i]->getValueType() == Value::InstructionVal)
387       ((Instruction*) tempVec[i])->dropAllReferences();
388   }
389 };
390
391 inline
392 MachineCodeForVMInstr::~MachineCodeForVMInstr()
393 {
394   // Free the Value objects created to hold intermediate values
395   for (unsigned i=0, N=tempVec.size(); i < N; i++)
396     delete tempVec[i];
397   
398   // Free the MachineInstr objects allocated, if any.
399   for (unsigned i=0, N=this->size(); i < N; i++)
400     delete (*this)[i];
401 }
402
403
404 //---------------------------------------------------------------------------
405 // class MachineCodeForBasicBlock
406 // 
407 // Purpose:
408 //   Representation of the sequence of machine instructions created
409 //   for a basic block.
410 //---------------------------------------------------------------------------
411
412
413 class MachineCodeForBasicBlock: public vector<MachineInstr*> {
414 public:
415   typedef vector<MachineInstr*>::iterator iterator;
416   typedef vector<MachineInstr*>::const_iterator const_iterator;
417 };
418
419
420 //---------------------------------------------------------------------------
421 // Target-independent utility routines for creating machine instructions
422 //---------------------------------------------------------------------------
423
424
425 //------------------------------------------------------------------------ 
426 // Function Set2OperandsFromInstr
427 // Function Set3OperandsFromInstr
428 // 
429 // For the common case of 2- and 3-operand arithmetic/logical instructions,
430 // set the m/c instr. operands directly from the VM instruction's operands.
431 // Check whether the first or second operand is 0 and can use a dedicated
432 // "0" register.
433 // Check whether the second operand should use an immediate field or register.
434 // (First and third operands are never immediates for such instructions.)
435 // 
436 // Arguments:
437 // canDiscardResult: Specifies that the result operand can be discarded
438 //                   by using the dedicated "0"
439 // 
440 // op1position, op2position and resultPosition: Specify in which position
441 //                   in the machine instruction the 3 operands (arg1, arg2
442 //                   and result) should go.
443 // 
444 // RETURN VALUE: unsigned int flags, where
445 //      flags & 0x01    => operand 1 is constant and needs a register
446 //      flags & 0x02    => operand 2 is constant and needs a register
447 //------------------------------------------------------------------------ 
448
449 void            Set2OperandsFromInstr   (MachineInstr* minstr,
450                                          InstructionNode* vmInstrNode,
451                                          const TargetMachine& targetMachine,
452                                          bool canDiscardResult = false,
453                                          int op1Position = 0,
454                                          int resultPosition = 1);
455
456 void            Set3OperandsFromInstr   (MachineInstr* minstr,
457                                          InstructionNode* vmInstrNode,
458                                          const TargetMachine& targetMachine,
459                                          bool canDiscardResult = false,
460                                          int op1Position = 0,
461                                          int op2Position = 1,
462                                          int resultPosition = 2);
463
464 MachineOperand::MachineOperandType
465                 ChooseRegOrImmed(Value* val,
466                                  MachineOpCode opCode,
467                                  const TargetMachine& targetMachine,
468                                  bool canUseImmed,
469                                  unsigned int& getMachineRegNum,
470                                  int64_t& getImmedValue);
471
472
473 ostream& operator<<(ostream& os, const MachineInstr& minstr);
474
475
476 ostream& operator<<(ostream& os, const MachineOperand& mop);
477                                          
478
479 void    PrintMachineInstructions        (const Method *const method);
480
481
482 //**************************************************************************/
483
484 #endif