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