--reg alloc code added
[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/InstInfo.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     
84     unsigned int regNum;        // register number for an explicit register
85   
86     int64_t immedVal;           // constant value for an explicit constant
87   };
88
89   bool isDef;                   // is this a defition for the value
90                                 // made public for faster access
91   
92 public:
93   /*ctor*/              MachineOperand  ();
94   /*ctor*/              MachineOperand  (MachineOperandType operandType,
95                                          Value* _val);
96   /*copy ctor*/         MachineOperand  (const MachineOperand&);
97   /*dtor*/              ~MachineOperand () {}
98   
99   // Accessor methods.  Caller is responsible for checking the
100   // operand type before invoking the corresponding accessor.
101   // 
102   inline MachineOperandType getOperandType      () const {
103     return opType;
104   }
105   inline Value*         getVRegValue    () const {
106     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
107            opType == MO_PCRelativeDisp);
108     return value;
109   }
110   inline unsigned int           getMachineRegNum() const {
111     assert(opType == MO_MachineRegister);
112     return regNum;
113   }
114   inline int64_t        getImmedValue   () const {
115     assert(opType >= MO_SignExtendedImmed || opType <= MO_PCRelativeDisp);
116     return immedVal;
117   }
118   inline bool           opIsDef         () const {
119     return isDef;
120   }
121   
122 public:
123   friend ostream& operator<<(ostream& os, const MachineOperand& mop);
124
125   
126 private:
127   // These functions are provided so that a vector of operands can be
128   // statically allocated and individual ones can be initialized later.
129   // Give class MachineInstr gets access to these functions.
130   // 
131   void                  Initialize      (MachineOperandType operandType,
132                                          Value* _val);
133   void                  InitializeConst (MachineOperandType operandType,
134                                          int64_t intValue);
135   void                  InitializeReg   (unsigned int regNum);
136
137   friend class MachineInstr;
138   friend class ValOpIterator<const MachineInstr, const Value>;
139   friend class ValOpIterator<      MachineInstr,       Value>;
140
141
142 public:
143
144  
145 };
146
147
148 inline
149 MachineOperand::MachineOperand()
150   : opType(MO_VirtualRegister),
151     value(NULL),
152     regNum(0),
153     immedVal(0),
154     isDef(false)
155 {}
156
157 inline
158 MachineOperand::MachineOperand(MachineOperandType operandType,
159                                Value* _val)
160   : opType(operandType),
161     value(_val),
162     regNum(0),
163     immedVal(0),
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 }
190
191 inline void
192 MachineOperand::InitializeConst(MachineOperandType operandType,
193                                 int64_t intValue)
194 {
195   opType = operandType;
196   value = NULL;
197   immedVal = intValue;
198 }
199
200 inline void
201 MachineOperand::InitializeReg(unsigned int _regNum)
202 {
203   opType = MO_MachineRegister;
204   value = NULL;
205   regNum = _regNum;
206 }
207
208
209 //---------------------------------------------------------------------------
210 // class MachineInstr 
211 // 
212 // Purpose:
213 //   Representation of each machine instruction.
214 // 
215 //   MachineOpCode must be an enum, defined separately for each target.
216 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
217 // 
218 //   opCodeMask is used to record variants of an instruction.
219 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
220 //      ANNUL:             if 1: Annul delay slot instruction.
221 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
222 //   Instead of creating 4 different opcodes for BNZ, we create a single
223 //   opcode and set bits in opCodeMask for each of these flags.
224 //---------------------------------------------------------------------------
225
226 class MachineInstr : public NonCopyable {
227 private:
228   MachineOpCode opCode;
229   OpCodeMask    opCodeMask;             // extra bits for variants of an opcode
230   vector<MachineOperand> operands;
231   
232 public:
233   typedef ValOpIterator<const MachineInstr, const Value> val_op_const_iterator;
234   typedef ValOpIterator<const MachineInstr,       Value> val_op_iterator;
235   
236 public:
237   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
238                                          OpCodeMask    _opCodeMask = 0x0);
239   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
240                                          unsigned       numOperands,
241                                          OpCodeMask    _opCodeMask = 0x0);
242   inline                ~MachineInstr   () {}
243   
244   const MachineOpCode   getOpCode       () const;
245   
246   unsigned int          getNumOperands  () const;
247   
248   const MachineOperand& getOperand      (unsigned int i) const;
249         MachineOperand& getOperand      (unsigned int i);
250   
251   bool                  operandIsDefined(unsigned int i) const;
252   
253   void                  dump            (unsigned int indent = 0) const;
254
255
256
257
258   
259 public:
260   friend ostream& operator<<(ostream& os, const MachineInstr& minstr);
261   friend val_op_const_iterator;
262   friend val_op_iterator;
263
264 public:
265   // Access to set the operands when building the machine instruction
266   void                  SetMachineOperand(unsigned int i,
267                               MachineOperand::MachineOperandType operandType,
268                               Value* _val, bool isDef=false);
269   void                  SetMachineOperand(unsigned int i,
270                               MachineOperand::MachineOperandType operandType,
271                               int64_t intValue, bool isDef=false);
272   void                  SetMachineOperand(unsigned int i,
273                                           unsigned int regNum, 
274                                           bool isDef=false);
275 };
276
277 inline const MachineOpCode
278 MachineInstr::getOpCode() const
279 {
280   return opCode;
281 }
282
283 inline unsigned int
284 MachineInstr::getNumOperands() const
285 {
286   return operands.size();
287 }
288
289 inline MachineOperand&
290 MachineInstr::getOperand(unsigned int i)
291 {
292   assert(i < operands.size() && "getOperand() out of range!");
293   return operands[i];
294 }
295
296 inline const MachineOperand&
297 MachineInstr::getOperand(unsigned int i) const
298 {
299   assert(i < operands.size() && "getOperand() out of range!");
300   return operands[i];
301 }
302
303 inline bool
304 MachineInstr::operandIsDefined(unsigned int i) const
305 {
306   return getOperand(i).opIsDef();
307 }
308
309
310 template<class _MI, class _V>
311 class ValOpIterator : public std::forward_iterator<_V, ptrdiff_t> {
312 private:
313   unsigned int i;
314   int resultPos;
315   _MI* minstr;
316   
317   inline void   skipToNextVal() {
318     while (i < minstr->getNumOperands() &&
319            ! ((minstr->operands[i].opType == MachineOperand::MO_VirtualRegister
320                || minstr->operands[i].opType == MachineOperand::MO_CCRegister)
321               && minstr->operands[i].value != NULL))
322       ++i;
323   }
324   
325 public:
326   typedef ValOpIterator<_MI, _V> _Self;
327   
328   inline ValOpIterator(_MI* _minstr) : i(0), minstr(_minstr) {
329     resultPos = TargetInstrDescriptors[minstr->opCode].resultPos;
330     skipToNextVal();
331   };
332   
333   inline _V*    operator*()  const { return minstr->getOperand(i).getVRegValue();}
334
335   const MachineOperand & getMachineOperand() const { return minstr->getOperand(i);  }
336
337   inline _V*    operator->() const { return operator*(); }
338   //  inline bool       isDef   ()   const { return (((int) i) == resultPos); }
339   
340   inline bool   isDef   ()   const { return minstr->getOperand(i).isDef; } 
341   inline bool   done    ()   const { return (i == minstr->getNumOperands()); }
342   
343   inline _Self& operator++()       { i++; skipToNextVal(); return *this; }
344   inline _Self  operator++(int)    { _Self tmp = *this; ++*this; return tmp; }
345 };
346
347
348 //---------------------------------------------------------------------------
349 // class MachineCodeForVMInstr
350 // 
351 // Purpose:
352 //   Representation of the sequence of machine instructions created
353 //   for a single VM instruction.  Additionally records any temporary 
354 //   "values" used as intermediate values in this sequence.
355 //   Note that such values should be treated as pure SSA values with
356 //   no interpretation of their operands (i.e., as a TmpInstruction object
357 //   which actually represents such a value).
358 // 
359 //---------------------------------------------------------------------------
360
361 class MachineCodeForVMInstr: public vector<MachineInstr*>
362 {
363 private:
364   vector<Value*> tempVec;
365   
366 public:
367   /*ctor*/      MachineCodeForVMInstr   ()      {}
368   /*ctor*/      ~MachineCodeForVMInstr  ();
369   
370   const vector<Value*>&
371                 getTempValues           () const { return tempVec; }
372   
373   void          addTempValue            (Value* val)
374                                                  { tempVec.push_back(val); }
375
376   // dropAllReferences() - This function drops all references within
377   // temporary (hidden) instructions created in implementing the original
378   // VM intruction.  This ensures there are no remaining "uses" within
379   // these hidden instructions, before the values of a method are freed.
380   //
381   // Make this inline because it has to be called from class Instruction
382   // and inlining it avoids a serious circurality in link order.
383   inline void dropAllReferences() {
384     for (unsigned i=0, N=tempVec.size(); i < N; i++)
385       if (Instruction *I = tempVec[i]->castInstruction())
386         I->dropAllReferences();
387   }
388 };
389
390 inline
391 MachineCodeForVMInstr::~MachineCodeForVMInstr()
392 {
393   // Free the Value objects created to hold intermediate values
394   for (unsigned i=0, N=tempVec.size(); i < N; i++)
395     delete tempVec[i];
396   
397   // Free the MachineInstr objects allocated, if any.
398   for (unsigned i=0, N=this->size(); i < N; i++)
399     delete (*this)[i];
400 }
401
402
403 //---------------------------------------------------------------------------
404 // class MachineCodeForBasicBlock
405 // 
406 // Purpose:
407 //   Representation of the sequence of machine instructions created
408 //   for a basic block.
409 //---------------------------------------------------------------------------
410
411
412 class MachineCodeForBasicBlock: public vector<MachineInstr*> {
413 public:
414   typedef vector<MachineInstr*>::iterator iterator;
415   typedef vector<const MachineInstr*>::const_iterator const_iterator;
416 };
417
418
419 //---------------------------------------------------------------------------
420 // Target-independent utility routines for creating machine instructions
421 //---------------------------------------------------------------------------
422
423
424 //------------------------------------------------------------------------ 
425 // Function Set2OperandsFromInstr
426 // Function Set3OperandsFromInstr
427 // 
428 // For the common case of 2- and 3-operand arithmetic/logical instructions,
429 // set the m/c instr. operands directly from the VM instruction's operands.
430 // Check whether the first or second operand is 0 and can use a dedicated
431 // "0" register.
432 // Check whether the second operand should use an immediate field or register.
433 // (First and third operands are never immediates for such instructions.)
434 // 
435 // Arguments:
436 // canDiscardResult: Specifies that the result operand can be discarded
437 //                   by using the dedicated "0"
438 // 
439 // op1position, op2position and resultPosition: Specify in which position
440 //                   in the machine instruction the 3 operands (arg1, arg2
441 //                   and result) should go.
442 // 
443 // RETURN VALUE: unsigned int flags, where
444 //      flags & 0x01    => operand 1 is constant and needs a register
445 //      flags & 0x02    => operand 2 is constant and needs a register
446 //------------------------------------------------------------------------ 
447
448 void            Set2OperandsFromInstr   (MachineInstr* minstr,
449                                          InstructionNode* vmInstrNode,
450                                          const TargetMachine& targetMachine,
451                                          bool canDiscardResult = false,
452                                          int op1Position = 0,
453                                          int resultPosition = 1);
454
455 void            Set3OperandsFromInstr   (MachineInstr* minstr,
456                                          InstructionNode* vmInstrNode,
457                                          const TargetMachine& targetMachine,
458                                          bool canDiscardResult = false,
459                                          int op1Position = 0,
460                                          int op2Position = 1,
461                                          int resultPosition = 2);
462
463 MachineOperand::MachineOperandType
464                 ChooseRegOrImmed(Value* val,
465                                  MachineOpCode opCode,
466                                  const TargetMachine& targetMachine,
467                                  bool canUseImmed,
468                                  unsigned int& getMachineRegNum,
469                                  int64_t& getImmedValue);
470
471
472 ostream& operator<<(ostream& os, const MachineInstr& minstr);
473
474
475 ostream& operator<<(ostream& os, const MachineOperand& mop);
476                                          
477
478 void    PrintMachineInstructions        (const Method *method);
479
480
481 //**************************************************************************/
482
483 #endif