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