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