5b75df7381ea4eee6fcfc71b3369a1c69ddd2e3e
[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/Annotation.h"
13 #include "llvm/Target/MRegisterInfo.h"
14 #include "Support/iterator"
15 #include "Support/NonCopyable.h"
16 #include <vector>
17 class Value;
18 class Function;
19 class MachineBasicBlock;
20 class TargetMachine;
21 class GlobalValue;
22
23 typedef int MachineOpCode;
24
25 /// MOTy - MachineOperandType - This namespace contains an enum that describes
26 /// how the machine operand is used by the instruction: is it read, defined, or
27 /// both?  Note that the MachineInstr/Operator class currently uses bool
28 /// arguments to represent this information instead of an enum.  Eventually this
29 /// should change over to use this _easier to read_ representation instead.
30 ///
31 namespace MOTy {
32   enum UseType {
33     Use,             /// This machine operand is only read by the instruction
34     Def,             /// This machine operand is only written by the instruction
35     UseAndDef        /// This machine operand is read AND written
36   };
37 }
38
39 //---------------------------------------------------------------------------
40 // class MachineOperand 
41 // 
42 // Purpose:
43 //   Representation of each machine instruction operand.
44 //   This class is designed so that you can allocate a vector of operands
45 //   first and initialize each one later.
46 //
47 //   E.g, for this VM instruction:
48 //              ptr = alloca type, numElements
49 //   we generate 2 machine instructions on the SPARC:
50 // 
51 //              mul Constant, Numelements -> Reg
52 //              add %sp, Reg -> Ptr
53 // 
54 //   Each instruction has 3 operands, listed above.  Of those:
55 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
56 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
57 //      
58 //   For the register operands, the virtual register type is as follows:
59 //      
60 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
61 //      MachineInstr* minstr will point to the instruction that computes reg.
62 // 
63 //   -  %sp will be of virtual register type MO_MachineReg.
64 //      The field regNum identifies the machine register.
65 // 
66 //   -  NumElements will be of virtual register type MO_VirtualReg.
67 //      The field Value* value identifies the value.
68 // 
69 //   -  Ptr will also be of virtual register type MO_VirtualReg.
70 //      Again, the field Value* value identifies the value.
71 // 
72 //---------------------------------------------------------------------------
73
74 struct MachineOperand {
75   enum MachineOperandType {
76     MO_VirtualRegister,         // virtual register for *value
77     MO_MachineRegister,         // pre-assigned machine register `regNum'
78     MO_CCRegister,
79     MO_SignExtendedImmed,
80     MO_UnextendedImmed,
81     MO_PCRelativeDisp,
82     MO_MachineBasicBlock,       // MachineBasicBlock reference
83     MO_FrameIndex,              // Abstract Stack Frame Index
84     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
85     MO_ExternalSymbol,          // Name of external global symbol
86     MO_GlobalAddress,           // Address of a global value
87   };
88   
89 private:
90   // Bit fields of the flags variable used for different operand properties
91   enum {
92     DEFFLAG    = 0x01,        // this is a def of the operand
93     DEFUSEFLAG = 0x02,        // this is both a def and a use
94     HIFLAG32   = 0x04,        // operand is %hi32(value_or_immedVal)
95     LOFLAG32   = 0x08,        // operand is %lo32(value_or_immedVal)
96     HIFLAG64   = 0x10,        // operand is %hi64(value_or_immedVal)
97     LOFLAG64   = 0x20,        // operand is %lo64(value_or_immedVal)
98     PCRELATIVE = 0x40,        // Operand is relative to PC, not a global address
99   
100     USEDEFMASK = 0x03,
101   };
102
103 private:
104   union {
105     Value*      value;          // BasicBlockVal for a label operand.
106                                 // ConstantVal for a non-address immediate.
107                                 // Virtual register for an SSA operand,
108                                 //   including hidden operands required for
109                                 //   the generated machine code.     
110                                 // LLVM global for MO_GlobalAddress.
111
112     int64_t immedVal;           // Constant value for an explicit constant
113
114     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
115     std::string *SymbolName;    // For MO_ExternalSymbol type
116   };
117
118   char flags;                   // see bit field definitions above
119   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
120   int regNum;                   // register number for an explicit register
121                                 // will be set for a value after reg allocation
122 private:
123   MachineOperand()
124     : immedVal(0),
125       flags(0),
126       opType(MO_VirtualRegister),
127       regNum(-1) {}
128
129   MachineOperand(int64_t ImmVal, MachineOperandType OpTy)
130     : immedVal(ImmVal),
131       flags(0),
132       opType(OpTy),
133       regNum(-1) {}
134
135   MachineOperand(int Reg, MachineOperandType OpTy, MOTy::UseType UseTy)
136     : immedVal(0),
137       opType(OpTy),
138       regNum(Reg) {
139     switch (UseTy) {
140     case MOTy::Use:       flags = 0; break;
141     case MOTy::Def:       flags = DEFFLAG; break;
142     case MOTy::UseAndDef: flags = DEFUSEFLAG; break;
143     default: assert(0 && "Invalid value for UseTy!");
144     }
145   }
146
147   MachineOperand(Value *V, MachineOperandType OpTy, MOTy::UseType UseTy,
148                  bool isPCRelative = false)
149     : value(V), opType(OpTy), regNum(-1) {
150     switch (UseTy) {
151     case MOTy::Use:       flags = 0; break;
152     case MOTy::Def:       flags = DEFFLAG; break;
153     case MOTy::UseAndDef: flags = DEFUSEFLAG; break;
154     default: assert(0 && "Invalid value for UseTy!");
155     }
156     if (isPCRelative) flags |= PCRELATIVE;
157   }
158
159   MachineOperand(MachineBasicBlock *mbb)
160     : MBB(mbb), flags(0), opType(MO_MachineBasicBlock), regNum(-1) {}
161
162   MachineOperand(const std::string &SymName, bool isPCRelative)
163     : SymbolName(new std::string(SymName)), flags(isPCRelative ? PCRELATIVE :0),
164       opType(MO_ExternalSymbol), regNum(-1) {}
165
166 public:
167   MachineOperand(const MachineOperand &M) : immedVal(M.immedVal),
168                                             flags(M.flags),
169                                             opType(M.opType),
170                                             regNum(M.regNum) {
171     if (isExternalSymbol())
172       SymbolName = new std::string(M.getSymbolName());
173   }
174
175   ~MachineOperand() {
176     if (isExternalSymbol())
177       delete SymbolName;
178   }
179   
180   const MachineOperand &operator=(const MachineOperand &MO) {
181     immedVal = MO.immedVal;
182     flags    = MO.flags;
183     opType   = MO.opType;
184     regNum   = MO.regNum;
185     if (isExternalSymbol())
186       SymbolName = new std::string(MO.getSymbolName());
187     return *this;
188   }
189
190   // Accessor methods.  Caller is responsible for checking the
191   // operand type before invoking the corresponding accessor.
192   // 
193   MachineOperandType getType() const { return opType; }
194
195   /// isPCRelative - This returns the value of the PCRELATIVE flag, which
196   /// indicates whether this operand should be emitted as a PC relative value
197   /// instead of a global address.  This is used for operands of the forms:
198   /// MachineBasicBlock, GlobalAddress, ExternalSymbol
199   ///
200   bool isPCRelative() const { return (flags & PCRELATIVE) != 0; }
201
202
203   // This is to finally stop caring whether we have a virtual or machine
204   // register -- an easier interface is to simply call both virtual and machine
205   // registers essentially the same, yet be able to distinguish when
206   // necessary. Thus the instruction selector can just add registers without
207   // abandon, and the register allocator won't be confused.
208   bool isVirtualRegister() const {
209     return (opType == MO_VirtualRegister || opType == MO_MachineRegister) 
210       && regNum >= MRegisterInfo::FirstVirtualRegister;
211   }
212   bool isPhysicalRegister() const {
213     return (opType == MO_VirtualRegister || opType == MO_MachineRegister) 
214       && (unsigned)regNum < MRegisterInfo::FirstVirtualRegister;
215   }
216   bool isRegister() const { return isVirtualRegister() || isPhysicalRegister();}
217   bool isMachineRegister() const { return !isVirtualRegister(); }
218   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
219   bool isPCRelativeDisp() const { return opType == MO_PCRelativeDisp; }
220   bool isImmediate() const {
221     return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
222   }
223   bool isFrameIndex() const { return opType == MO_FrameIndex; }
224   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
225   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
226   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
227
228   Value* getVRegValue() const {
229     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
230            isPCRelativeDisp());
231     return value;
232   }
233   Value* getVRegValueOrNull() const {
234     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
235             isPCRelativeDisp()) ? value : NULL;
236   }
237   int getMachineRegNum() const {
238     assert(opType == MO_MachineRegister);
239     return regNum;
240   }
241   int64_t getImmedValue() const { assert(isImmediate()); return immedVal; }
242   MachineBasicBlock *getMachineBasicBlock() const {
243     assert(isMachineBasicBlock() && "Can't get MBB in non-MBB operand!");
244     return MBB;
245   }
246   int getFrameIndex() const { assert(isFrameIndex()); return immedVal; }
247   unsigned getConstantPoolIndex() const {
248     assert(isConstantPoolIndex());
249     return immedVal;
250   }
251
252   GlobalValue *getGlobal() const {
253     assert(isGlobalAddress());
254     return (GlobalValue*)value;
255   }
256
257   const std::string &getSymbolName() const {
258     assert(isExternalSymbol());
259     return *SymbolName;
260   }
261
262   bool          opIsUse         () const { return (flags & USEDEFMASK) == 0; }
263   bool          opIsDef         () const { return flags & DEFFLAG; }
264   bool          opIsDefAndUse   () const { return flags & DEFUSEFLAG; }
265   bool          opHiBits32      () const { return flags & HIFLAG32; }
266   bool          opLoBits32      () const { return flags & LOFLAG32; }
267   bool          opHiBits64      () const { return flags & HIFLAG64; }
268   bool          opLoBits64      () const { return flags & LOFLAG64; }
269
270   // used to check if a machine register has been allocated to this operand
271   bool hasAllocatedReg() const {
272     return (regNum >= 0 &&
273             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
274              opType == MO_MachineRegister));
275   }
276
277   // used to get the reg number if when one is allocated
278   int getAllocatedRegNum() const {
279     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
280            opType == MO_MachineRegister);
281     return regNum;
282   }
283
284   unsigned getReg() const {
285     assert(hasAllocatedReg() && "Cannot call MachineOperand::getReg()!");
286     return regNum;
287   }    
288   
289   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
290
291 private:
292
293   // Construction methods needed for fine-grain control.
294   // These must be accessed via coresponding methods in MachineInstr.
295   void markHi32()      { flags |= HIFLAG32; }
296   void markLo32()      { flags |= LOFLAG32; }
297   void markHi64()      { flags |= HIFLAG64; }
298   void markLo64()      { flags |= LOFLAG64; }
299   
300   // Replaces the Value with its corresponding physical register after
301   // register allocation is complete
302   void setRegForValue(int reg) {
303     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
304            opType == MO_MachineRegister);
305     regNum = reg;
306   }
307   
308   friend class MachineInstr;
309 };
310
311
312 //---------------------------------------------------------------------------
313 // class MachineInstr 
314 // 
315 // Purpose:
316 //   Representation of each machine instruction.
317 // 
318 //   MachineOpCode must be an enum, defined separately for each target.
319 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
320 // 
321 //  There are 2 kinds of operands:
322 // 
323 //  (1) Explicit operands of the machine instruction in vector operands[] 
324 // 
325 //  (2) "Implicit operands" are values implicitly used or defined by the
326 //      machine instruction, such as arguments to a CALL, return value of
327 //      a CALL (if any), and return value of a RETURN.
328 //---------------------------------------------------------------------------
329
330 class MachineInstr: public NonCopyable {      // Disable copy operations
331
332   MachineOpCode    opCode;              // the opcode
333   std::vector<MachineOperand> operands; // the operands
334   unsigned numImplicitRefs;             // number of implicit operands
335
336   MachineOperand& getImplicitOp(unsigned i) {
337     assert(i < numImplicitRefs && "implicit ref# out of range!");
338     return operands[i + operands.size() - numImplicitRefs];
339   }
340   const MachineOperand& getImplicitOp(unsigned i) const {
341     assert(i < numImplicitRefs && "implicit ref# out of range!");
342     return operands[i + operands.size() - numImplicitRefs];
343   }
344
345   // regsUsed - all machine registers used for this instruction, including regs
346   // used to save values across the instruction.  This is a bitset of registers.
347   std::vector<bool> regsUsed;
348
349   // OperandComplete - Return true if it's illegal to add a new operand
350   bool OperandsComplete() const;
351
352 public:
353   MachineInstr(MachineOpCode Opcode);
354   MachineInstr(MachineOpCode Opcode, unsigned numOperands);
355
356   /// MachineInstr ctor - This constructor only does a _reserve_ of the
357   /// operands, not a resize for them.  It is expected that if you use this that
358   /// you call add* methods below to fill up the operands, instead of the Set
359   /// methods.  Eventually, the "resizing" ctors will be phased out.
360   ///
361   MachineInstr(MachineOpCode Opcode, unsigned numOperands, bool XX, bool YY);
362
363   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
364   /// the MachineInstr is created and added to the end of the specified basic
365   /// block.
366   ///
367   MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode, unsigned numOps);
368   
369
370   // The opcode.
371   // 
372   const MachineOpCode getOpcode() const { return opCode; }
373   const MachineOpCode getOpCode() const { return opCode; }
374
375   //
376   // Information about explicit operands of the instruction
377   // 
378   unsigned getNumOperands() const { return operands.size() - numImplicitRefs; }
379   
380   const MachineOperand& getOperand(unsigned i) const {
381     assert(i < getNumOperands() && "getOperand() out of range!");
382     return operands[i];
383   }
384   MachineOperand& getOperand(unsigned i) {
385     assert(i < getNumOperands() && "getOperand() out of range!");
386     return operands[i];
387   }
388
389   // FIXME: ELIMINATE
390   MachineOperand::MachineOperandType getOperandType(unsigned i) const {
391     return getOperand(i).getType();
392   }
393
394   // FIXME: ELIMINATE: Misleading name: Definition not defined.
395   bool operandIsDefined(unsigned i) const {
396     return getOperand(i).opIsDef();
397   }
398
399   bool operandIsDefinedAndUsed(unsigned i) const {
400     return getOperand(i).opIsDefAndUse();
401   }
402
403   //
404   // Information about implicit operands of the instruction
405   // 
406   unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
407   
408   const Value* getImplicitRef(unsigned i) const {
409     return getImplicitOp(i).getVRegValue();
410   }
411   Value* getImplicitRef(unsigned i) {
412     return getImplicitOp(i).getVRegValue();
413   }
414
415   bool implicitRefIsDefined(unsigned i) const {
416     return getImplicitOp(i).opIsDef();
417   }
418   bool implicitRefIsDefinedAndUsed(unsigned i) const {
419     return getImplicitOp(i).opIsDefAndUse();
420   }
421   inline void addImplicitRef    (Value* V,
422                                  bool isDef=false,bool isDefAndUse=false);
423   inline void setImplicitRef    (unsigned i, Value* V,
424                                  bool isDef=false, bool isDefAndUse=false);
425
426   //
427   // Information about registers used in this instruction
428   // 
429   const std::vector<bool> &getRegsUsed() const { return regsUsed; }
430   
431   // insertUsedReg - Add a register to the Used registers set...
432   void insertUsedReg(unsigned Reg) {
433     if (Reg >= regsUsed.size())
434       regsUsed.resize(Reg+1);
435     regsUsed[Reg] = true;
436   }
437
438   //
439   // Debugging support
440   //
441   void print(std::ostream &OS, const TargetMachine &TM) const;
442   void dump() const;
443   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
444
445   //
446   // Define iterators to access the Value operands of the Machine Instruction.
447   // Note that these iterators only enumerate the explicit operands.
448   // begin() and end() are defined to produce these iterators...
449   //
450   template<class _MI, class _V> class ValOpIterator;
451   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
452   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
453
454
455   //===--------------------------------------------------------------------===//
456   // Accessors to add operands when building up machine instructions
457   //
458
459   /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
460   /// operands list...
461   ///
462   void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
463     assert(!OperandsComplete() &&
464            "Trying to add an operand to a machine instr that is already done!");
465     operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
466              !isDef ? MOTy::Use : (isDefAndUse ? MOTy::UseAndDef : MOTy::Def)));
467   }
468
469   void addRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use,
470                      bool isPCRelative = false) {
471     assert(!OperandsComplete() &&
472            "Trying to add an operand to a machine instr that is already done!");
473     operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
474                                       UTy, isPCRelative));
475   }
476
477   /// addRegOperand - Add a symbolic virtual register reference...
478   ///
479   void addRegOperand(int reg, bool isDef) {
480     assert(!OperandsComplete() &&
481            "Trying to add an operand to a machine instr that is already done!");
482     operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
483                                       isDef ? MOTy::Def : MOTy::Use));
484   }
485
486   /// addRegOperand - Add a symbolic virtual register reference...
487   ///
488   void addRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
489     assert(!OperandsComplete() &&
490            "Trying to add an operand to a machine instr that is already done!");
491     operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
492                                       UTy));
493   }
494
495   /// addPCDispOperand - Add a PC relative displacement operand to the MI
496   ///
497   void addPCDispOperand(Value *V) {
498     assert(!OperandsComplete() &&
499            "Trying to add an operand to a machine instr that is already done!");
500     operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp,
501                                       MOTy::Use));
502   }
503
504   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
505   ///
506   void addMachineRegOperand(int reg, bool isDef) {
507     assert(!OperandsComplete() &&
508            "Trying to add an operand to a machine instr that is already done!");
509     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
510                                       isDef ? MOTy::Def : MOTy::Use));
511     insertUsedReg(reg);
512   }
513
514   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
515   ///
516   void addMachineRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
517     assert(!OperandsComplete() &&
518            "Trying to add an operand to a machine instr that is already done!");
519     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
520                                       UTy));
521     insertUsedReg(reg);
522   }
523
524   /// addZeroExtImmOperand - Add a zero extended constant argument to the
525   /// machine instruction.
526   ///
527   void addZeroExtImmOperand(int64_t intValue) {
528     assert(!OperandsComplete() &&
529            "Trying to add an operand to a machine instr that is already done!");
530     operands.push_back(MachineOperand(intValue,
531                                       MachineOperand::MO_UnextendedImmed));
532   }
533
534   /// addSignExtImmOperand - Add a zero extended constant argument to the
535   /// machine instruction.
536   ///
537   void addSignExtImmOperand(int64_t intValue) {
538     assert(!OperandsComplete() &&
539            "Trying to add an operand to a machine instr that is already done!");
540     operands.push_back(MachineOperand(intValue,
541                                       MachineOperand::MO_SignExtendedImmed));
542   }
543
544   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
545     assert(!OperandsComplete() &&
546            "Trying to add an operand to a machine instr that is already done!");
547     operands.push_back(MachineOperand(MBB));
548   }
549
550   /// addFrameIndexOperand - Add an abstract frame index to the instruction
551   ///
552   void addFrameIndexOperand(unsigned Idx) {
553     assert(!OperandsComplete() &&
554            "Trying to add an operand to a machine instr that is already done!");
555     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
556   }
557
558   /// addConstantPoolndexOperand - Add a constant pool object index to the
559   /// instruction.
560   ///
561   void addConstantPoolIndexOperand(unsigned I) {
562     assert(!OperandsComplete() &&
563            "Trying to add an operand to a machine instr that is already done!");
564     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
565   }
566
567   void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative) {
568     assert(!OperandsComplete() &&
569            "Trying to add an operand to a machine instr that is already done!");
570     operands.push_back(MachineOperand((Value*)GV,
571                                       MachineOperand::MO_GlobalAddress,
572                                       MOTy::Use, isPCRelative));
573   }
574
575   /// addExternalSymbolOperand - Add an external symbol operand to this instr
576   ///
577   void addExternalSymbolOperand(const std::string &SymName, bool isPCRelative) {
578     operands.push_back(MachineOperand(SymName, isPCRelative));
579   }
580
581   //===--------------------------------------------------------------------===//
582   // Accessors used to modify instructions in place.
583   //
584   // FIXME: Move this stuff to MachineOperand itself!
585
586   /// replace - Support to rewrite a machine instruction in place: for now,
587   /// simply replace() and then set new operands with Set.*Operand methods
588   /// below.
589   /// 
590   void replace(MachineOpCode Opcode, unsigned numOperands);
591
592   /// setOpcode - Replace the opcode of the current instruction with a new one.
593   ///
594   void setOpcode(unsigned Op) { opCode = Op; }
595
596   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
597   /// fewer operand than it started with.
598   ///
599   void RemoveOperand(unsigned i) {
600     operands.erase(operands.begin()+i);
601   }
602
603   // Access to set the operands when building the machine instruction
604   // 
605   void SetMachineOperandVal     (unsigned i,
606                                  MachineOperand::MachineOperandType operandType,
607                                  Value* V,
608                                  bool isDef=false,
609                                  bool isDefAndUse=false);
610
611   void SetMachineOperandConst   (unsigned i,
612                                  MachineOperand::MachineOperandType operandType,
613                                  int64_t intValue);
614
615   void SetMachineOperandReg     (unsigned i,
616                                  int regNum,
617                                  bool isDef=false);
618
619
620   unsigned substituteValue(const Value* oldVal, Value* newVal,
621                            bool defsOnly = true);
622
623   void setOperandHi32(unsigned i) { operands[i].markHi32(); }
624   void setOperandLo32(unsigned i) { operands[i].markLo32(); }
625   void setOperandHi64(unsigned i) { operands[i].markHi64(); }
626   void setOperandLo64(unsigned i) { operands[i].markLo64(); }
627   
628   
629   // SetRegForOperand - Replaces the Value for the operand with its allocated
630   // physical register after register allocation is complete.
631   // 
632   void SetRegForOperand(unsigned i, int regNum);
633
634   //
635   // Iterator to enumerate machine operands.
636   // 
637   template<class MITy, class VTy>
638   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
639     unsigned i;
640     MITy MI;
641     
642     void skipToNextVal() {
643       while (i < MI->getNumOperands() &&
644              !( (MI->getOperandType(i) == MachineOperand::MO_VirtualRegister ||
645                  MI->getOperandType(i) == MachineOperand::MO_CCRegister)
646                 && MI->getOperand(i).getVRegValue() != 0))
647         ++i;
648     }
649   
650     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
651       skipToNextVal();
652     }
653   
654   public:
655     typedef ValOpIterator<MITy, VTy> _Self;
656     
657     inline VTy operator*() const {
658       return MI->getOperand(i).getVRegValue();
659     }
660
661     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
662           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
663
664     inline VTy operator->() const { return operator*(); }
665
666     inline bool isDef()       const { return MI->getOperand(i).opIsDef(); } 
667     inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
668
669     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
670     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
671
672     inline bool operator==(const _Self &y) const { 
673       return i == y.i;
674     }
675     inline bool operator!=(const _Self &y) const { 
676       return !operator==(y);
677     }
678
679     static _Self begin(MITy MI) {
680       return _Self(MI, 0);
681     }
682     static _Self end(MITy MI) {
683       return _Self(MI, MI->getNumOperands());
684     }
685   };
686
687   // define begin() and end()
688   val_op_iterator begin() { return val_op_iterator::begin(this); }
689   val_op_iterator end()   { return val_op_iterator::end(this); }
690
691   const_val_op_iterator begin() const {
692     return const_val_op_iterator::begin(this);
693   }
694   const_val_op_iterator end() const {
695     return const_val_op_iterator::end(this);
696   }
697 };
698
699
700 // Define here to enable inlining of the functions used.
701 // 
702 void MachineInstr::addImplicitRef(Value* V,
703                                   bool isDef,
704                                   bool isDefAndUse)
705 {
706   ++numImplicitRefs;
707   addRegOperand(V, isDef, isDefAndUse);
708 }
709
710 void MachineInstr::setImplicitRef(unsigned i,
711                                   Value* V,
712                                   bool isDef,
713                                   bool isDefAndUse)
714 {
715   assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
716   SetMachineOperandVal(i + getNumOperands(),
717                        MachineOperand::MO_VirtualRegister,
718                        V, isDef, isDefAndUse);
719 }
720
721
722 //---------------------------------------------------------------------------
723 // Debugging Support
724 //---------------------------------------------------------------------------
725
726 std::ostream& operator<<        (std::ostream& os,
727                                  const MachineInstr& minstr);
728
729 std::ostream& operator<<        (std::ostream& os,
730                                  const MachineOperand& mop);
731                                          
732 void PrintMachineInstructions   (const Function *F);
733
734 #endif