Remove a 'using' directives from a header file. Using directives cause
[oota-llvm.git] / include / llvm / CodeGen / MachineInstr.h
1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*--=//
2 //
3 // This file contains the declaration of the MachineInstr class, which is the
4 // basic representation for all target dependant machine instructions used by
5 // the back end.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
10 #define LLVM_CODEGEN_MACHINEINSTR_H
11
12 #include "llvm/Target/MachineInstrInfo.h"
13 #include "llvm/Annotation.h"
14 #include <Support/iterator>
15 #include <Support/hash_set>
16 class Instruction;
17
18 //---------------------------------------------------------------------------
19 // class MachineOperand 
20 // 
21 // Purpose:
22 //   Representation of each machine instruction operand.
23 //   This class is designed so that you can allocate a vector of operands
24 //   first and initialize each one later.
25 //
26 //   E.g, for this VM instruction:
27 //              ptr = alloca type, numElements
28 //   we generate 2 machine instructions on the SPARC:
29 // 
30 //              mul Constant, Numelements -> Reg
31 //              add %sp, Reg -> Ptr
32 // 
33 //   Each instruction has 3 operands, listed above.  Of those:
34 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
35 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
36 //      
37 //   For the register operands, the virtual register type is as follows:
38 //      
39 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
40 //      MachineInstr* minstr will point to the instruction that computes reg.
41 // 
42 //   -  %sp will be of virtual register type MO_MachineReg.
43 //      The field regNum identifies the machine register.
44 // 
45 //   -  NumElements will be of virtual register type MO_VirtualReg.
46 //      The field Value* value identifies the value.
47 // 
48 //   -  Ptr will also be of virtual register type MO_VirtualReg.
49 //      Again, the field Value* value identifies the value.
50 // 
51 //---------------------------------------------------------------------------
52
53
54 class MachineOperand {
55 public:
56   enum MachineOperandType {
57     MO_VirtualRegister,         // virtual register for *value
58     MO_MachineRegister,         // pre-assigned machine register `regNum'
59     MO_CCRegister,
60     MO_SignExtendedImmed,
61     MO_UnextendedImmed,
62     MO_PCRelativeDisp,
63   };
64   
65 private:
66   // Bit fields of the flags variable used for different operand properties
67   static const char DEFFLAG    = 0x1;  // this is a def of the operand
68   static const char DEFUSEFLAG = 0x2;  // this is both a def and a use
69   static const char HIFLAG32   = 0x4;  // operand is %hi32(value_or_immedVal)
70   static const char LOFLAG32   = 0x8;  // operand is %lo32(value_or_immedVal)
71   static const char HIFLAG64   = 0x10; // operand is %hi64(value_or_immedVal)
72   static const char LOFLAG64   = 0x20; // operand is %lo64(value_or_immedVal)
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   int regNum;                   // register number for an explicit register
87                                 // will be set for a value after reg allocation
88   char flags;                   // see bit field definitions above
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 Value*         getVRegValueOrNull() const {
109     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
110             opType == MO_PCRelativeDisp)? value : NULL;
111   }
112   inline int            getMachineRegNum() const {
113     assert(opType == MO_MachineRegister);
114     return regNum;
115   }
116   inline int64_t        getImmedValue   () const {
117     assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
118     return immedVal;
119   }
120   inline bool           opIsDef         () const {
121     return flags & DEFFLAG;
122   }
123   inline bool           opIsDefAndUse   () const {
124     return flags & DEFUSEFLAG;
125   }
126   inline bool           opHiBits32      () const {
127     return flags & HIFLAG32;
128   }
129   inline bool           opLoBits32      () const {
130     return flags & LOFLAG32;
131   }
132   inline bool           opHiBits64      () const {
133     return flags & HIFLAG64;
134   }
135   inline bool           opLoBits64      () const {
136     return flags & LOFLAG64;
137   }
138
139   // used to check if a machine register has been allocated to this operand
140   inline bool   hasAllocatedReg() const {
141     return (regNum >= 0 &&
142             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
143              opType == MO_MachineRegister));
144   }
145
146   // used to get the reg number if when one is allocated
147   inline int  getAllocatedRegNum() const {
148     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
149            opType == MO_MachineRegister);
150     return regNum;
151   }
152
153   
154 public:
155   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
156
157 private:
158   // These functions are provided so that a vector of operands can be
159   // statically allocated and individual ones can be initialized later.
160   // Give class MachineInstr access to these functions.
161   // 
162   void                  Initialize      (MachineOperandType operandType,
163                                          Value* _val);
164   void                  InitializeConst (MachineOperandType operandType,
165                                          int64_t intValue);
166   void                  InitializeReg   (int regNum,
167                                          bool isCCReg);
168
169   // Construction methods needed for fine-grain control.
170   // These must be accessed via coresponding methods in MachineInstr.
171   void markDef()       { flags |= DEFFLAG; }
172   void markDefAndUse() { flags |= DEFUSEFLAG; }
173   void markHi32()      { flags |= HIFLAG32; }
174   void markLo32()      { flags |= LOFLAG32; }
175   void markHi64()      { flags |= HIFLAG64; }
176   void markLo64()      { flags |= LOFLAG64; }
177   
178   // Replaces the Value with its corresponding physical register after
179   // register allocation is complete
180   void setRegForValue(int reg) {
181     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
182            opType == MO_MachineRegister);
183     regNum = reg;
184   }
185   
186   friend class MachineInstr;
187 };
188
189
190 inline
191 MachineOperand::MachineOperand()
192   : opType(MO_VirtualRegister),
193     immedVal(0),
194     regNum(-1),
195     flags(0)
196 {}
197
198 inline
199 MachineOperand::MachineOperand(MachineOperandType operandType,
200                                Value* _val)
201   : opType(operandType),
202     immedVal(0),
203     regNum(-1),
204     flags(0)
205 {}
206
207 inline
208 MachineOperand::MachineOperand(const MachineOperand& mo)
209   : opType(mo.opType),
210     flags(mo.flags)
211 {
212   switch(opType) {
213   case MO_VirtualRegister:
214   case MO_CCRegister:           value = mo.value; break;
215   case MO_MachineRegister:      regNum = mo.regNum; break;
216   case MO_SignExtendedImmed:
217   case MO_UnextendedImmed:
218   case MO_PCRelativeDisp:       immedVal = mo.immedVal; break;
219   default: assert(0);
220   }
221 }
222
223 inline void
224 MachineOperand::Initialize(MachineOperandType operandType,
225                            Value* _val)
226 {
227   opType = operandType;
228   value = _val;
229   regNum = -1;
230   flags = 0;
231 }
232
233 inline void
234 MachineOperand::InitializeConst(MachineOperandType operandType,
235                                 int64_t intValue)
236 {
237   opType = operandType;
238   value = NULL;
239   immedVal = intValue;
240   regNum = -1;
241   flags = 0;
242 }
243
244 inline void
245 MachineOperand::InitializeReg(int _regNum, bool isCCReg)
246 {
247   opType = isCCReg? MO_CCRegister : MO_MachineRegister;
248   value = NULL;
249   regNum = (int) _regNum;
250   flags = 0;
251 }
252
253
254 //---------------------------------------------------------------------------
255 // class MachineInstr 
256 // 
257 // Purpose:
258 //   Representation of each machine instruction.
259 // 
260 //   MachineOpCode must be an enum, defined separately for each target.
261 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
262 // 
263 //   opCodeMask is used to record variants of an instruction.
264 //   E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
265 //      ANNUL:             if 1: Annul delay slot instruction.
266 //      PREDICT-NOT-TAKEN: if 1: predict branch not taken.
267 //   Instead of creating 4 different opcodes for BNZ, we create a single
268 //   opcode and set bits in opCodeMask for each of these flags.
269 //
270 //  There are 2 kinds of operands:
271 // 
272 //  (1) Explicit operands of the machine instruction in vector operands[] 
273 // 
274 //  (2) "Implicit operands" are values implicitly used or defined by the
275 //      machine instruction, such as arguments to a CALL, return value of
276 //      a CALL (if any), and return value of a RETURN.
277 //---------------------------------------------------------------------------
278
279 class MachineInstr :  public Annotable,         // Values are annotable
280                       public NonCopyable {      // Disable copy operations
281   MachineOpCode    opCode;              // the opcode
282   OpCodeMask       opCodeMask;          // extra bits for variants of an opcode
283   std::vector<MachineOperand> operands; // the operands
284   std::vector<Value*>   implicitRefs;   // values implicitly referenced by this
285   std::vector<bool>     implicitIsDef;  //  machine instruction (eg, call args)
286   std::vector<bool>     implicitIsDefAndUse;
287   hash_set<int>    regsUsed;            // all machine registers used for this
288                                         //  instruction, including regs used
289                                         //  to save values across the instr.
290 public:
291   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
292                                          OpCodeMask    _opCodeMask = 0x0);
293   /*ctor*/              MachineInstr    (MachineOpCode _opCode,
294                                          unsigned       numOperands,
295                                          OpCodeMask    _opCodeMask = 0x0);
296   inline                ~MachineInstr   () {}
297
298   // 
299   // Support to rewrite a machine instruction in place: for now, simply
300   // replace() and then set new operands with Set.*Operand methods below.
301   // 
302   void                  replace         (MachineOpCode _opCode,
303                                          unsigned       numOperands,
304                                          OpCodeMask    _opCodeMask = 0x0);
305   
306   //
307   // The op code.  Note that MachineOpCode is a target-specific type.
308   // 
309   const MachineOpCode   getOpCode       () const { return opCode; }
310
311   //
312   // Information about explicit operands of the instruction
313   // 
314   unsigned int          getNumOperands  () const { return operands.size(); }
315   
316   bool                  operandIsDefined(unsigned i) const;
317   bool                  operandIsDefinedAndUsed(unsigned i) const;
318   
319   const MachineOperand& getOperand      (unsigned i) const;
320         MachineOperand& getOperand      (unsigned i);
321   
322   //
323   // Information about implicit operands of the instruction
324   // 
325   unsigned              getNumImplicitRefs() const{return implicitRefs.size();}
326   
327   bool                  implicitRefIsDefined(unsigned i) const;
328   bool                  implicitRefIsDefinedAndUsed(unsigned i) const;
329   
330   const Value*          getImplicitRef  (unsigned i) const;
331         Value*          getImplicitRef  (unsigned i);
332   
333   //
334   // Information about registers used in this instruction
335   // 
336   const hash_set<int>&  getRegsUsed    () const { return regsUsed; }
337         hash_set<int>&  getRegsUsed    ()       { return regsUsed; }
338   
339   //
340   // Debugging support
341   // 
342   void                  dump            () const;
343   friend std::ostream& operator<<       (std::ostream& os,
344                                          const MachineInstr& minstr);
345
346   //
347   // Define iterators to access the Value operands of the Machine Instruction.
348   // begin() and end() are defined to produce these iterators...
349   //
350   template<class _MI, class _V> class ValOpIterator;
351   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
352   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
353
354
355   // Access to set the operands when building the machine instruction
356   // 
357   void                  SetMachineOperandVal(unsigned i,
358                                              MachineOperand::MachineOperandType
359                                                operandType,
360                                              Value* _val,
361                                              bool isDef=false,
362                                              bool isDefAndUse=false);
363   void                  SetMachineOperandConst(unsigned i,
364                                            MachineOperand::MachineOperandType
365                                                  operandType,
366                                                int64_t intValue);
367   void                  SetMachineOperandReg(unsigned i, int regNum, 
368                                              bool isDef=false,
369                                              bool isDefAndUse=false,
370                                              bool isCCReg=false);
371   
372   void                  addImplicitRef   (Value* val, 
373                                           bool isDef=false,
374                                           bool isDefAndUse=false);
375   
376   void                  setImplicitRef   (unsigned i,
377                                           Value* val, 
378                                           bool isDef=false,
379                                           bool isDefAndUse=false);
380
381   unsigned              substituteValue  (const Value* oldVal,
382                                           Value* newVal,
383                                           bool defsOnly = true);
384
385   void                  setOperandHi32   (unsigned i);
386   void                  setOperandLo32   (unsigned i);
387   void                  setOperandHi64   (unsigned i);
388   void                  setOperandLo64   (unsigned i);
389   
390   
391   // Replaces the Value for the operand with its allocated
392   // physical register after register allocation is complete.
393   // 
394   void                  SetRegForOperand(unsigned i, int regNum);
395   
396   //
397   // Iterator to enumerate machine operands.
398   // 
399   template<class MITy, class VTy>
400   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
401     unsigned i;
402     MITy MI;
403     
404     inline void skipToNextVal() {
405       while (i < MI->getNumOperands() &&
406              !((MI->getOperand(i).getOperandType() == MachineOperand::MO_VirtualRegister ||
407                 MI->getOperand(i).getOperandType() == MachineOperand::MO_CCRegister)
408                && MI->getOperand(i).getVRegValue() != 0))
409         ++i;
410     }
411   
412     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
413       skipToNextVal();
414     }
415   
416   public:
417     typedef ValOpIterator<MITy, VTy> _Self;
418     
419     inline VTy operator*() const {
420       return MI->getOperand(i).getVRegValue();
421     }
422
423     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
424           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
425
426     inline VTy operator->() const { return operator*(); }
427
428     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
429     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
430
431     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
432     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
433
434     inline bool operator==(const _Self &y) const { 
435       return i == y.i;
436     }
437     inline bool operator!=(const _Self &y) const { 
438       return !operator==(y);
439     }
440
441     static _Self begin(MITy MI) {
442       return _Self(MI, 0);
443     }
444     static _Self end(MITy MI) {
445       return _Self(MI, MI->getNumOperands());
446     }
447   };
448
449   // define begin() and end()
450   val_op_iterator begin() { return val_op_iterator::begin(this); }
451   val_op_iterator end()   { return val_op_iterator::end(this); }
452
453   const_val_op_iterator begin() const {
454     return const_val_op_iterator::begin(this);
455   }
456   const_val_op_iterator end() const {
457     return const_val_op_iterator::end(this);
458   }
459 };
460
461
462 inline MachineOperand&
463 MachineInstr::getOperand(unsigned int i)
464 {
465   assert(i < operands.size() && "getOperand() out of range!");
466   return operands[i];
467 }
468
469 inline const MachineOperand&
470 MachineInstr::getOperand(unsigned int i) const
471 {
472   assert(i < operands.size() && "getOperand() out of range!");
473   return operands[i];
474 }
475
476 inline bool
477 MachineInstr::operandIsDefined(unsigned int i) const
478 {
479   return getOperand(i).opIsDef();
480 }
481
482 inline bool
483 MachineInstr::operandIsDefinedAndUsed(unsigned int i) const
484 {
485   return getOperand(i).opIsDefAndUse();
486 }
487
488 inline bool
489 MachineInstr::implicitRefIsDefined(unsigned int i) const
490 {
491   assert(i < implicitIsDef.size() && "operand out of range!");
492   return implicitIsDef[i];
493 }
494
495 inline bool
496 MachineInstr::implicitRefIsDefinedAndUsed(unsigned int i) const
497 {
498   assert(i < implicitIsDefAndUse.size() && "operand out of range!");
499   return implicitIsDefAndUse[i];
500 }
501
502 inline const Value*
503 MachineInstr::getImplicitRef(unsigned int i) const
504 {
505   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
506   return implicitRefs[i];
507 }
508
509 inline Value*
510 MachineInstr::getImplicitRef(unsigned int i)
511 {
512   assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
513   return implicitRefs[i];
514 }
515
516 inline void
517 MachineInstr::addImplicitRef(Value* val, 
518                              bool isDef,
519                              bool isDefAndUse)
520 {
521   implicitRefs.push_back(val);
522   implicitIsDef.push_back(isDef);
523   implicitIsDefAndUse.push_back(isDefAndUse);
524 }
525
526 inline void
527 MachineInstr::setImplicitRef(unsigned int i,
528                              Value* val, 
529                              bool isDef,
530                              bool isDefAndUse)
531 {
532   assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
533   implicitRefs[i] = val;
534   implicitIsDef[i] = isDef;
535   implicitIsDefAndUse[i] = isDefAndUse;
536 }
537
538 inline void
539 MachineInstr::setOperandHi32(unsigned i)
540 {
541   operands[i].markHi32();
542 }
543
544 inline void
545 MachineInstr::setOperandLo32(unsigned i)
546 {
547   operands[i].markLo32();
548 }
549
550 inline void
551 MachineInstr::setOperandHi64(unsigned i)
552 {
553   operands[i].markHi64();
554 }
555
556 inline void
557 MachineInstr::setOperandLo64(unsigned i)
558 {
559   operands[i].markLo64();
560 }
561
562
563 //---------------------------------------------------------------------------
564 // Debugging Support
565 //---------------------------------------------------------------------------
566
567 std::ostream& operator<<    (std::ostream& os, const MachineInstr& minstr);
568
569 std::ostream& operator<<    (std::ostream& os, const MachineOperand& mop);
570                                          
571 void    PrintMachineInstructions(const Function *F);
572
573 #endif