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