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