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