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