When folding memory operands in machine instructions be careful to
[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   /// getUseType - Returns the MachineOperandUseType of this operand.
201   ///
202   MOTy::UseType getUseType() const {
203       return isUse() ^ isDef() ? MOTy::UseAndDef :
204           (isUse() ? MOTy::Use : MOTy::Def);
205   }
206
207   /// isPCRelative - This returns the value of the PCRELATIVE flag, which
208   /// indicates whether this operand should be emitted as a PC relative value
209   /// instead of a global address.  This is used for operands of the forms:
210   /// MachineBasicBlock, GlobalAddress, ExternalSymbol
211   ///
212   bool isPCRelative() const { return (flags & PCRELATIVE) != 0; }
213
214   /// isRegister - Return true if this operand is a register operand.  The X86
215   /// backend currently can't decide whether to use MO_MR or MO_VR to represent
216   /// them, so we accept both.
217   ///
218   /// Note: The sparc backend should not use this method.
219   ///
220   bool isRegister() const {
221     return opType == MO_MachineRegister || opType == MO_VirtualRegister;
222   }
223
224   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
225   bool isPCRelativeDisp() const { return opType == MO_PCRelativeDisp; }
226   bool isImmediate() const {
227     return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
228   }
229   bool isFrameIndex() const { return opType == MO_FrameIndex; }
230   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
231   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
232   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
233
234   Value* getVRegValue() const {
235     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
236            isPCRelativeDisp());
237     return value;
238   }
239   Value* getVRegValueOrNull() const {
240     return (opType == MO_VirtualRegister || opType == MO_CCRegister || 
241             isPCRelativeDisp()) ? value : NULL;
242   }
243   int getMachineRegNum() const {
244     assert(opType == MO_MachineRegister);
245     return regNum;
246   }
247   int64_t getImmedValue() const { assert(isImmediate()); return immedVal; }
248   void setImmedValue(int64_t ImmVal) { assert(isImmediate()); immedVal=ImmVal; }
249
250   MachineBasicBlock *getMachineBasicBlock() const {
251     assert(isMachineBasicBlock() && "Can't get MBB in non-MBB operand!");
252     return MBB;
253   }
254   int getFrameIndex() const { assert(isFrameIndex()); return immedVal; }
255   unsigned getConstantPoolIndex() const {
256     assert(isConstantPoolIndex());
257     return immedVal;
258   }
259
260   GlobalValue *getGlobal() const {
261     assert(isGlobalAddress());
262     return (GlobalValue*)value;
263   }
264
265   const std::string &getSymbolName() const {
266     assert(isExternalSymbol());
267     return *SymbolName;
268   }
269
270   bool            isUse           () const { return flags & USEFLAG; }
271   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
272   bool            isDef           () const { return flags & DEFFLAG; }
273   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
274   bool            isHiBits32      () const { return flags & HIFLAG32; }
275   bool            isLoBits32      () const { return flags & LOFLAG32; }
276   bool            isHiBits64      () const { return flags & HIFLAG64; }
277   bool            isLoBits64      () const { return flags & LOFLAG64; }
278
279   // used to check if a machine register has been allocated to this operand
280   bool hasAllocatedReg() const {
281     return (regNum >= 0 &&
282             (opType == MO_VirtualRegister || opType == MO_CCRegister || 
283              opType == MO_MachineRegister));
284   }
285
286   // used to get the reg number if when one is allocated
287   unsigned getReg() const {
288     assert(hasAllocatedReg());
289     return regNum;
290   }
291
292   // ********** TODO: get rid of this duplicate code! ***********
293   void setReg(unsigned Reg) {
294     assert(hasAllocatedReg() && "This operand cannot have a register number!");
295     regNum = Reg;
296   }    
297
298   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
299
300 private:
301
302   // Construction methods needed for fine-grain control.
303   // These must be accessed via coresponding methods in MachineInstr.
304   void markHi32()      { flags |= HIFLAG32; }
305   void markLo32()      { flags |= LOFLAG32; }
306   void markHi64()      { flags |= HIFLAG64; }
307   void markLo64()      { flags |= LOFLAG64; }
308   
309   // Replaces the Value with its corresponding physical register after
310   // register allocation is complete
311   void setRegForValue(int reg) {
312     assert(opType == MO_VirtualRegister || opType == MO_CCRegister || 
313            opType == MO_MachineRegister);
314     regNum = reg;
315   }
316   
317   friend class MachineInstr;
318 };
319
320
321 //===----------------------------------------------------------------------===//
322 // class MachineInstr 
323 // 
324 // Purpose:
325 //   Representation of each machine instruction.
326 // 
327 //   MachineOpCode must be an enum, defined separately for each target.
328 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
329 // 
330 //  There are 2 kinds of operands:
331 // 
332 //  (1) Explicit operands of the machine instruction in vector operands[] 
333 // 
334 //  (2) "Implicit operands" are values implicitly used or defined by the
335 //      machine instruction, such as arguments to a CALL, return value of
336 //      a CALL (if any), and return value of a RETURN.
337 //===----------------------------------------------------------------------===//
338
339 class MachineInstr {
340   short            Opcode;              // the opcode
341   unsigned char numImplicitRefs;        // number of implicit operands
342   std::vector<MachineOperand> operands; // the operands
343   MachineInstr* prev, *next;            // links for our intrusive list
344   MachineBasicBlock* parent;            // pointer to the owning basic block
345   // OperandComplete - Return true if it's illegal to add a new operand
346   bool OperandsComplete() const;
347
348   MachineInstr(const MachineInstr &);  // DO NOT IMPLEMENT
349   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
350
351 private:
352   // Intrusive list support
353   //
354   friend class ilist_traits<MachineInstr>;
355
356 public:
357   MachineInstr(short 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(short 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, short Opcode, unsigned numOps);
371   
372   ~MachineInstr();
373
374   const MachineBasicBlock* getParent() const { return parent; }
375   MachineBasicBlock* getParent() { return parent; }
376
377   /// Accessors for opcode.
378   ///
379   const int getOpcode() const { return Opcode; }
380
381   /// Access to explicit operands of the instruction.
382   ///
383   unsigned getNumOperands() const { return operands.size() - numImplicitRefs; }
384   
385   const MachineOperand& getOperand(unsigned i) const {
386     assert(i < getNumOperands() && "getOperand() out of range!");
387     return operands[i];
388   }
389   MachineOperand& getOperand(unsigned i) {
390     assert(i < getNumOperands() && "getOperand() out of range!");
391     return operands[i];
392   }
393
394   //
395   // Access to explicit or implicit operands of the instruction
396   // This returns the i'th entry in the operand vector.
397   // That represents the i'th explicit operand or the (i-N)'th implicit operand,
398   // depending on whether i < N or i >= N.
399   // 
400   const MachineOperand& getExplOrImplOperand(unsigned i) const {
401     assert(i < operands.size() && "getExplOrImplOperand() out of range!");
402     return (i < getNumOperands()? getOperand(i)
403                                 : getImplicitOp(i - getNumOperands()));
404   }
405
406   //
407   // Access to implicit operands of the instruction
408   // 
409   unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
410   
411   MachineOperand& getImplicitOp(unsigned i) {
412     assert(i < numImplicitRefs && "implicit ref# out of range!");
413     return operands[i + operands.size() - numImplicitRefs];
414   }
415   const MachineOperand& getImplicitOp(unsigned i) const {
416     assert(i < numImplicitRefs && "implicit ref# out of range!");
417     return operands[i + operands.size() - numImplicitRefs];
418   }
419
420   Value* getImplicitRef(unsigned i) {
421     return getImplicitOp(i).getVRegValue();
422   }
423   const Value* getImplicitRef(unsigned i) const {
424     return getImplicitOp(i).getVRegValue();
425   }
426
427   void addImplicitRef(Value* V, bool isDef = false, bool isDefAndUse = false) {
428     ++numImplicitRefs;
429     addRegOperand(V, isDef, isDefAndUse);
430   }
431   void setImplicitRef(unsigned i, Value* V) {
432     assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
433     SetMachineOperandVal(i + getNumOperands(),
434                          MachineOperand::MO_VirtualRegister, V);
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   }
519
520   /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
521   ///
522   void addMachineRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
523     assert(!OperandsComplete() &&
524            "Trying to add an operand to a machine instr that is already done!");
525     operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
526                                       UTy));
527   }
528
529   /// addZeroExtImmOperand - Add a zero extended constant argument to the
530   /// machine instruction.
531   ///
532   void addZeroExtImmOperand(int64_t intValue) {
533     assert(!OperandsComplete() &&
534            "Trying to add an operand to a machine instr that is already done!");
535     operands.push_back(MachineOperand(intValue,
536                                       MachineOperand::MO_UnextendedImmed));
537   }
538
539   /// addSignExtImmOperand - Add a zero extended constant argument to the
540   /// machine instruction.
541   ///
542   void addSignExtImmOperand(int64_t intValue) {
543     assert(!OperandsComplete() &&
544            "Trying to add an operand to a machine instr that is already done!");
545     operands.push_back(MachineOperand(intValue,
546                                       MachineOperand::MO_SignExtendedImmed));
547   }
548
549   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
550     assert(!OperandsComplete() &&
551            "Trying to add an operand to a machine instr that is already done!");
552     operands.push_back(MachineOperand(MBB));
553   }
554
555   /// addFrameIndexOperand - Add an abstract frame index to the instruction
556   ///
557   void addFrameIndexOperand(unsigned Idx) {
558     assert(!OperandsComplete() &&
559            "Trying to add an operand to a machine instr that is already done!");
560     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
561   }
562
563   /// addConstantPoolndexOperand - Add a constant pool object index to the
564   /// instruction.
565   ///
566   void addConstantPoolIndexOperand(unsigned I) {
567     assert(!OperandsComplete() &&
568            "Trying to add an operand to a machine instr that is already done!");
569     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
570   }
571
572   void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative) {
573     assert(!OperandsComplete() &&
574            "Trying to add an operand to a machine instr that is already done!");
575     operands.push_back(MachineOperand((Value*)GV,
576                                       MachineOperand::MO_GlobalAddress,
577                                       MOTy::Use, isPCRelative));
578   }
579
580   /// addExternalSymbolOperand - Add an external symbol operand to this instr
581   ///
582   void addExternalSymbolOperand(const std::string &SymName, bool isPCRelative) {
583     operands.push_back(MachineOperand(SymName, isPCRelative));
584   }
585
586   //===--------------------------------------------------------------------===//
587   // Accessors used to modify instructions in place.
588   //
589   // FIXME: Move this stuff to MachineOperand itself!
590
591   /// replace - Support to rewrite a machine instruction in place: for now,
592   /// simply replace() and then set new operands with Set.*Operand methods
593   /// below.
594   /// 
595   void replace(short Opcode, unsigned numOperands);
596
597   /// setOpcode - Replace the opcode of the current instruction with a new one.
598   ///
599   void setOpcode(unsigned Op) { Opcode = Op; }
600
601   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
602   /// fewer operand than it started with.
603   ///
604   void RemoveOperand(unsigned i) {
605     operands.erase(operands.begin()+i);
606   }
607
608   // Access to set the operands when building the machine instruction
609   // 
610   void SetMachineOperandVal     (unsigned i,
611                                  MachineOperand::MachineOperandType operandType,
612                                  Value* V);
613
614   void SetMachineOperandConst   (unsigned i,
615                                  MachineOperand::MachineOperandType operandType,
616                                  int64_t intValue);
617
618   void SetMachineOperandReg(unsigned i, int regNum);
619
620
621   unsigned substituteValue(const Value* oldVal, Value* newVal,
622                            bool defsOnly, bool notDefsAndUses,
623                            bool& someArgsWereIgnored);
624
625   void setOperandHi32(unsigned i) { operands[i].markHi32(); }
626   void setOperandLo32(unsigned i) { operands[i].markLo32(); }
627   void setOperandHi64(unsigned i) { operands[i].markHi64(); }
628   void setOperandLo64(unsigned i) { operands[i].markLo64(); }
629   
630   
631   // SetRegForOperand -
632   // SetRegForImplicitRef -
633   // Mark an explicit or implicit operand with its allocated physical register.
634   // 
635   void SetRegForOperand(unsigned i, int regNum);
636   void SetRegForImplicitRef(unsigned i, int regNum);
637
638   //
639   // Iterator to enumerate machine operands.
640   // 
641   template<class MITy, class VTy>
642   class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
643     unsigned i;
644     MITy MI;
645     
646     void skipToNextVal() {
647       while (i < MI->getNumOperands() &&
648              !( (MI->getOperand(i).getType() == MachineOperand::MO_VirtualRegister ||
649                  MI->getOperand(i).getType() == MachineOperand::MO_CCRegister)
650                 && MI->getOperand(i).getVRegValue() != 0))
651         ++i;
652     }
653   
654     inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
655       skipToNextVal();
656     }
657   
658   public:
659     typedef ValOpIterator<MITy, VTy> _Self;
660     
661     inline VTy operator*() const {
662       return MI->getOperand(i).getVRegValue();
663     }
664
665     const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
666           MachineOperand &getMachineOperand()       { return MI->getOperand(i);}
667
668     inline VTy operator->() const { return operator*(); }
669
670     inline bool isUse()   const { return MI->getOperand(i).isUse(); } 
671     inline bool isDef()   const { return MI->getOperand(i).isDef(); } 
672
673     inline _Self& operator++() { i++; skipToNextVal(); return *this; }
674     inline _Self  operator++(int) { _Self tmp = *this; ++*this; return tmp; }
675
676     inline bool operator==(const _Self &y) const { 
677       return i == y.i;
678     }
679     inline bool operator!=(const _Self &y) const { 
680       return !operator==(y);
681     }
682
683     static _Self begin(MITy MI) {
684       return _Self(MI, 0);
685     }
686     static _Self end(MITy MI) {
687       return _Self(MI, MI->getNumOperands());
688     }
689   };
690
691   // define begin() and end()
692   val_op_iterator begin() { return val_op_iterator::begin(this); }
693   val_op_iterator end()   { return val_op_iterator::end(this); }
694
695   const_val_op_iterator begin() const {
696     return const_val_op_iterator::begin(this);
697   }
698   const_val_op_iterator end() const {
699     return const_val_op_iterator::end(this);
700   }
701 };
702
703 //===----------------------------------------------------------------------===//
704 // Debugging Support
705
706 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
707 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
708 void PrintMachineInstructions(const Function *F);
709
710 } // End llvm namespace
711
712 #endif