Rename the opCode instance variable to Opcode
[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
33 typedef int MachineOpCode;
34
35 //===----------------------------------------------------------------------===//
36 /// MOTy - MachineOperandType - This namespace contains an enum that describes
37 /// how the machine operand is used by the instruction: is it read, defined, or
38 /// both?  Note that the MachineInstr/Operator class currently uses bool
39 /// arguments to represent this information instead of an enum.  Eventually this
40 /// should change over to use this _easier to read_ representation instead.
41 ///
42 namespace MOTy {
43   enum UseType {
44     Use,             /// This machine operand is only read by the instruction
45     Def,             /// This machine operand is only written by the instruction
46     UseAndDef        /// This machine operand is read AND written
47   };
48 }
49
50 //===----------------------------------------------------------------------===//
51 // class MachineOperand 
52 // 
53 // Purpose:
54 //   Representation of each machine instruction operand.
55 //   This class is designed so that you can allocate a vector of operands
56 //   first and initialize each one later.
57 //
58 //   E.g, for this VM instruction:
59 //              ptr = alloca type, numElements
60 //   we generate 2 machine instructions on the SPARC:
61 // 
62 //              mul Constant, Numelements -> Reg
63 //              add %sp, Reg -> Ptr
64 // 
65 //   Each instruction has 3 operands, listed above.  Of those:
66 //   -  Reg, NumElements, and Ptr are of operand type MO_Register.
67 //   -  Constant is of operand type MO_SignExtendedImmed on the SPARC.
68 //      
69 //   For the register operands, the virtual register type is as follows:
70 //      
71 //   -  Reg will be of virtual register type MO_MInstrVirtualReg.  The field
72 //      MachineInstr* minstr will point to the instruction that computes reg.
73 // 
74 //   -  %sp will be of virtual register type MO_MachineReg.
75 //      The field regNum identifies the machine register.
76 // 
77 //   -  NumElements will be of virtual register type MO_VirtualReg.
78 //      The field Value* value identifies the value.
79 // 
80 //   -  Ptr will also be of virtual register type MO_VirtualReg.
81 //      Again, the field Value* value identifies the value.
82 // 
83 //===----------------------------------------------------------------------===//
84
85 struct MachineOperand {
86   enum MachineOperandType {
87     MO_VirtualRegister,         // virtual register for *value
88     MO_MachineRegister,         // pre-assigned machine register `regNum'
89     MO_CCRegister,
90     MO_SignExtendedImmed,
91     MO_UnextendedImmed,
92     MO_PCRelativeDisp,
93     MO_MachineBasicBlock,       // MachineBasicBlock reference
94     MO_FrameIndex,              // Abstract Stack Frame Index
95     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
96     MO_ExternalSymbol,          // Name of external global symbol
97     MO_GlobalAddress,           // Address of a global value
98   };
99   
100 private:
101   // Bit fields of the flags variable used for different operand properties
102   enum {
103     DEFFLAG     = 0x01,       // this is a def of the operand
104     USEFLAG     = 0x02,       // this is a use of the operand
105     HIFLAG32    = 0x04,       // operand is %hi32(value_or_immedVal)
106     LOFLAG32    = 0x08,       // operand is %lo32(value_or_immedVal)
107     HIFLAG64    = 0x10,       // operand is %hi64(value_or_immedVal)
108     LOFLAG64    = 0x20,       // operand is %lo64(value_or_immedVal)
109     PCRELATIVE  = 0x40,       // Operand is relative to PC, not a global address
110   };
111
112 private:
113   union {
114     Value*      value;          // BasicBlockVal for a label operand.
115                                 // ConstantVal for a non-address immediate.
116                                 // Virtual register for an SSA operand,
117                                 //   including hidden operands required for
118                                 //   the generated machine code.     
119                                 // LLVM global for MO_GlobalAddress.
120
121     int64_t immedVal;           // Constant value for an explicit constant
122
123     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
124     std::string *SymbolName;    // For MO_ExternalSymbol type
125   };
126
127   char flags;                   // see bit field definitions above
128   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
129   int regNum;                   // register number for an explicit register
130                                 // will be set for a value after reg allocation
131 private:
132   MachineOperand(int64_t ImmVal = 0, MachineOperandType OpTy = MO_VirtualRegister)
133     : immedVal(ImmVal),
134       flags(0),
135       opType(OpTy),
136       regNum(-1) {}
137
138   MachineOperand(int Reg, MachineOperandType OpTy, MOTy::UseType UseTy)
139     : immedVal(0),
140       opType(OpTy),
141       regNum(Reg) {
142     switch (UseTy) {
143     case MOTy::Use:       flags = USEFLAG; break;
144     case MOTy::Def:       flags = DEFFLAG; break;
145     case MOTy::UseAndDef: flags = DEFFLAG | USEFLAG; break;
146     default: assert(0 && "Invalid value for UseTy!");
147     }
148   }
149
150   MachineOperand(Value *V, MachineOperandType OpTy, MOTy::UseType UseTy,
151                  bool isPCRelative = false)
152     : value(V), opType(OpTy), regNum(-1) {
153     switch (UseTy) {
154     case MOTy::Use:       flags = USEFLAG; break;
155     case MOTy::Def:       flags = DEFFLAG; break;
156     case MOTy::UseAndDef: flags = DEFFLAG | USEFLAG; break;
157     default: assert(0 && "Invalid value for UseTy!");
158     }
159     if (isPCRelative) flags |= PCRELATIVE;
160   }
161
162   MachineOperand(MachineBasicBlock *mbb)
163     : MBB(mbb), flags(0), opType(MO_MachineBasicBlock), regNum(-1) {}
164
165   MachineOperand(const std::string &SymName, bool isPCRelative)
166     : SymbolName(new std::string(SymName)), flags(isPCRelative ? PCRELATIVE :0),
167       opType(MO_ExternalSymbol), regNum(-1) {}
168
169 public:
170   MachineOperand(const MachineOperand &M) : immedVal(M.immedVal),
171                                             flags(M.flags),
172                                             opType(M.opType),
173                                             regNum(M.regNum) {
174     if (isExternalSymbol())
175       SymbolName = new std::string(M.getSymbolName());
176   }
177
178   ~MachineOperand() {
179     if (isExternalSymbol())
180       delete SymbolName;
181   }
182   
183   const MachineOperand &operator=(const MachineOperand &MO) {
184     if (isExternalSymbol())             // if old operand had a symbol name,
185       delete SymbolName;                // release old memory
186     immedVal = MO.immedVal;
187     flags    = MO.flags;
188     opType   = MO.opType;
189     regNum   = MO.regNum;
190     if (isExternalSymbol())
191       SymbolName = new std::string(MO.getSymbolName());
192     return *this;
193   }
194
195   /// getType - Returns the MachineOperandType for this operand.
196   /// 
197   MachineOperandType getType() const { return opType; }
198
199   /// isPCRelative - This returns the value of the PCRELATIVE flag, which
200   /// indicates whether this operand should be emitted as a PC relative value
201   /// instead of a global address.  This is used for operands of the forms:
202   /// MachineBasicBlock, GlobalAddress, ExternalSymbol
203   ///
204   bool isPCRelative() const { return (flags & PCRELATIVE) != 0; }
205
206   /// isRegister - Return true if this operand is a register operand.  The X86
207   /// backend currently can't decide whether to use MO_MR or MO_VR to represent
208   /// them, so we accept both.
209   ///
210   /// Note: The sparc backend should not use this method.
211   ///
212   bool isRegister() const {
213     return opType == MO_MachineRegister || opType == MO_VirtualRegister;
214   }
215
216   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
217   bool isPCRelativeDisp() const { return opType == MO_PCRelativeDisp; }
218   bool isImmediate() const {
219     return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
220   }
221   bool isFrameIndex() const { return opType == MO_FrameIndex; }
222   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
223   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
224   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
225
226   Value* getVRegValue() const {
227     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
228            isPCRelativeDisp());
229     return value;
230   }
231   Value* getVRegValueOrNull() const {
232     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
233             isPCRelativeDisp()) ? value : NULL;
234   }
235   int getMachineRegNum() const {
236     assert(opType == MO_MachineRegister);
237     return regNum;
238   }
239   int64_t getImmedValue() const { assert(isImmediate()); return immedVal; }
240   void setImmedValue(int64_t ImmVal) { assert(isImmediate()); immedVal=ImmVal; }
241
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            isUse           () const { return flags & USEFLAG; }
263   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
264   bool            isDef           () const { return flags & DEFFLAG; }
265   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
266   bool            isHiBits32      () const { return flags & HIFLAG32; }
267   bool            isLoBits32      () const { return flags & LOFLAG32; }
268   bool            isHiBits64      () const { return flags & HIFLAG64; }
269   bool            isLoBits64      () const { return flags & LOFLAG64; }
270
271   // used to check if a machine register has been allocated to this operand
272   bool hasAllocatedReg() const {
273     return (regNum >= 0 &&
274             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
275              opType == MO_MachineRegister));
276   }
277
278   // used to get the reg number if when one is allocated
279   int getAllocatedRegNum() const {
280     assert(hasAllocatedReg());
281     return regNum;
282   }
283
284   // ********** TODO: get rid of this duplicate code! ***********
285   unsigned getReg() const {
286     return getAllocatedRegNum();
287   }    
288   void setReg(unsigned Reg) {
289     assert(hasAllocatedReg() && "This operand cannot have a register number!");
290     regNum = Reg;
291   }    
292
293   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
294
295 private:
296
297   // Construction methods needed for fine-grain control.
298   // These must be accessed via coresponding methods in MachineInstr.
299   void markHi32()      { flags |= HIFLAG32; }
300   void markLo32()      { flags |= LOFLAG32; }
301   void markHi64()      { flags |= HIFLAG64; }
302   void markLo64()      { flags |= LOFLAG64; }
303   
304   // Replaces the Value with its corresponding physical register after
305   // register allocation is complete
306   void setRegForValue(int reg) {
307     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
308            opType == MO_MachineRegister);
309     regNum = reg;
310   }
311   
312   friend class MachineInstr;
313 };
314
315
316 //===----------------------------------------------------------------------===//
317 // class MachineInstr 
318 // 
319 // Purpose:
320 //   Representation of each machine instruction.
321 // 
322 //   MachineOpCode must be an enum, defined separately for each target.
323 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
324 // 
325 //  There are 2 kinds of operands:
326 // 
327 //  (1) Explicit operands of the machine instruction in vector operands[] 
328 // 
329 //  (2) "Implicit operands" are values implicitly used or defined by the
330 //      machine instruction, such as arguments to a CALL, return value of
331 //      a CALL (if any), and return value of a RETURN.
332 //===----------------------------------------------------------------------===//
333
334 class MachineInstr {
335   int              Opcode;              // the opcode
336   std::vector<MachineOperand> operands; // the operands
337   unsigned numImplicitRefs;             // number of implicit operands
338   MachineInstr* prev, *next;            // links for our intrusive list
339   // OperandComplete - Return true if it's illegal to add a new operand
340   bool OperandsComplete() const;
341
342   MachineInstr(const MachineInstr &);  // DO NOT IMPLEMENT
343   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
344
345 private:
346   // Intrusive list support
347   //
348   friend class ilist_traits<MachineInstr>;
349   MachineInstr() { /* this is for ilist use only to create the sentinel */ }
350   MachineInstr* getPrev() const { return prev; }
351   MachineInstr* getNext() const { return next; }
352
353   void setPrev(MachineInstr* mi) { prev = mi; }
354   void setNext(MachineInstr* mi) { next = mi; }
355
356 public:
357   MachineInstr(int Opcode, unsigned numOperands);
358
359   /// MachineInstr ctor - This constructor only does a _reserve_ of the
360   /// operands, not a resize for them.  It is expected that if you use this that
361   /// you call add* methods below to fill up the operands, instead of the Set
362   /// methods.  Eventually, the "resizing" ctors will be phased out.
363   ///
364   MachineInstr(int Opcode, unsigned numOperands, bool XX, bool YY);
365
366   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
367   /// the MachineInstr is created and added to the end of the specified basic
368   /// block.
369   ///
370   MachineInstr(MachineBasicBlock *MBB, int Opcode, unsigned numOps);
371   
372   /// Accessors for opcode.
373   ///
374   const int getOpcode() const { return Opcode; }
375
376   /// Access to 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   //
390   // Access to explicit or implicit operands of the instruction
391   // This returns the i'th entry in the operand vector.
392   // That represents the i'th explicit operand or the (i-N)'th implicit operand,
393   // depending on whether i < N or i >= N.
394   // 
395   const MachineOperand& getExplOrImplOperand(unsigned i) const {
396     assert(i < operands.size() && "getExplOrImplOperand() out of range!");
397     return (i < getNumOperands()? getOperand(i)
398                                 : getImplicitOp(i - getNumOperands()));
399   }
400
401   //
402   // Access to implicit operands of the instruction
403   // 
404   unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
405   
406   MachineOperand& getImplicitOp(unsigned i) {
407     assert(i < numImplicitRefs && "implicit ref# out of range!");
408     return operands[i + operands.size() - numImplicitRefs];
409   }
410   const MachineOperand& getImplicitOp(unsigned i) const {
411     assert(i < numImplicitRefs && "implicit ref# out of range!");
412     return operands[i + operands.size() - numImplicitRefs];
413   }
414
415   Value* getImplicitRef(unsigned i) {
416     return getImplicitOp(i).getVRegValue();
417   }
418   const Value* getImplicitRef(unsigned i) const {
419     return getImplicitOp(i).getVRegValue();
420   }
421
422   void addImplicitRef(Value* V, bool isDef = false, bool isDefAndUse = false) {
423     ++numImplicitRefs;
424     addRegOperand(V, isDef, isDefAndUse);
425   }
426   void setImplicitRef(unsigned i, Value* V) {
427     assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
428     SetMachineOperandVal(i + getNumOperands(),
429                          MachineOperand::MO_VirtualRegister, V);
430   }
431
432   //
433   // Debugging support
434   //
435   void print(std::ostream &OS, const TargetMachine &TM) const;
436   void dump() const;
437   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
438
439   //
440   // Define iterators to access the Value operands of the Machine Instruction.
441   // Note that these iterators only enumerate the explicit operands.
442   // begin() and end() are defined to produce these iterators...
443   //
444   template<class _MI, class _V> class ValOpIterator;
445   typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
446   typedef ValOpIterator<      MachineInstr*,      Value*> val_op_iterator;
447
448
449   //===--------------------------------------------------------------------===//
450   // Accessors to add operands when building up machine instructions
451   //
452
453   /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
454   /// operands list...
455   ///
456   void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
457     assert(!OperandsComplete() &&
458            "Trying to add an operand to a machine instr that is already done!");
459     operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
460              !isDef ? MOTy::Use : (isDefAndUse ? MOTy::UseAndDef : MOTy::Def)));
461   }
462
463   void addRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use,
464                      bool isPCRelative = false) {
465     assert(!OperandsComplete() &&
466            "Trying to add an operand to a machine instr that is already done!");
467     operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
468                                       UTy, isPCRelative));
469   }
470
471   void addCCRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use) {
472     assert(!OperandsComplete() &&
473            "Trying to add an operand to a machine instr that is already done!");
474     operands.push_back(MachineOperand(V, MachineOperand::MO_CCRegister, UTy,
475                                       false));
476   }
477
478
479   /// addRegOperand - Add a symbolic virtual register reference...
480   ///
481   void addRegOperand(int reg, bool isDef) {
482     assert(!OperandsComplete() &&
483            "Trying to add an operand to a machine instr that is already done!");
484     operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
485                                       isDef ? MOTy::Def : MOTy::Use));
486   }
487
488   /// addRegOperand - Add a symbolic virtual register reference...
489   ///
490   void addRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
491     assert(!OperandsComplete() &&
492            "Trying to add an operand to a machine instr that is already done!");
493     operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
494                                       UTy));
495   }
496
497   /// addPCDispOperand - Add a PC relative displacement operand to the MI
498   ///
499   void addPCDispOperand(Value *V) {
500     assert(!OperandsComplete() &&
501            "Trying to add an operand to a machine instr that is already done!");
502     operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp,
503                                       MOTy::Use));
504   }
505
506   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
507   ///
508   void addMachineRegOperand(int reg, bool isDef) {
509     assert(!OperandsComplete() &&
510            "Trying to add an operand to a machine instr that is already done!");
511     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
512                                       isDef ? MOTy::Def : MOTy::Use));
513   }
514
515   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
516   ///
517   void addMachineRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
518     assert(!OperandsComplete() &&
519            "Trying to add an operand to a machine instr that is already done!");
520     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
521                                       UTy));
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(int 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
609   void SetMachineOperandConst   (unsigned i,
610                                  MachineOperand::MachineOperandType operandType,
611                                  int64_t intValue);
612
613   void SetMachineOperandReg(unsigned i, int regNum);
614
615
616   unsigned substituteValue(const Value* oldVal, Value* newVal,
617                            bool defsOnly, bool notDefsAndUses,
618                            bool& someArgsWereIgnored);
619
620   void setOperandHi32(unsigned i) { operands[i].markHi32(); }
621   void setOperandLo32(unsigned i) { operands[i].markLo32(); }
622   void setOperandHi64(unsigned i) { operands[i].markHi64(); }
623   void setOperandLo64(unsigned i) { operands[i].markLo64(); }
624   
625   
626   // SetRegForOperand -
627   // SetRegForImplicitRef -
628   // Mark an explicit or implicit operand with its allocated physical register.
629   // 
630   void SetRegForOperand(unsigned i, int regNum);
631   void SetRegForImplicitRef(unsigned i, int regNum);
632
633   //
634   // Iterator to enumerate machine operands.
635   // 
636   template<class MITy, class VTy>
637   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
638     unsigned i;
639     MITy MI;
640     
641     void skipToNextVal() {
642       while (i < MI->getNumOperands() &&
643              !( (MI->getOperand(i).getType() == MachineOperand::MO_VirtualRegister ||
644                  MI->getOperand(i).getType() == MachineOperand::MO_CCRegister)
645                 && MI->getOperand(i).getVRegValue() != 0))
646         ++i;
647     }
648   
649     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
650       skipToNextVal();
651     }
652   
653   public:
654     typedef ValOpIterator<MITy, VTy> _Self;
655     
656     inline VTy operator*() const {
657       return MI->getOperand(i).getVRegValue();
658     }
659
660     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
661           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
662
663     inline VTy operator->() const { return operator*(); }
664
665     inline bool isUse()   const { return MI->getOperand(i).isUse(); } 
666     inline bool isDef()   const { return MI->getOperand(i).isDef(); } 
667
668     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
669     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
670
671     inline bool operator==(const _Self &y) const { 
672       return i == y.i;
673     }
674     inline bool operator!=(const _Self &y) const { 
675       return !operator==(y);
676     }
677
678     static _Self begin(MITy MI) {
679       return _Self(MI, 0);
680     }
681     static _Self end(MITy MI) {
682       return _Self(MI, MI->getNumOperands());
683     }
684   };
685
686   // define begin() and end()
687   val_op_iterator begin() { return val_op_iterator::begin(this); }
688   val_op_iterator end()   { return val_op_iterator::end(this); }
689
690   const_val_op_iterator begin() const {
691     return const_val_op_iterator::begin(this);
692   }
693   const_val_op_iterator end() const {
694     return const_val_op_iterator::end(this);
695   }
696 };
697
698
699 //===----------------------------------------------------------------------===//
700 // Debugging Support
701
702 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
703 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
704 void PrintMachineInstructions(const Function *F);
705
706 } // End llvm namespace
707
708 #endif