Remove two arguments that are never specified
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
1 //===-- MachineInstr.cpp --------------------------------------------------===//
2 // 
3 //===----------------------------------------------------------------------===//
4
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/Value.h"
7 #include "llvm/Target/MachineInstrInfo.h"  // FIXME: shouldn't need this!
8 using std::cerr;
9
10
11 // Constructor for instructions with fixed #operands (nearly all)
12 MachineInstr::MachineInstr(MachineOpCode _opCode,
13                            OpCodeMask    _opCodeMask)
14   : opCode(_opCode),
15     opCodeMask(_opCodeMask),
16     operands(TargetInstrDescriptors[_opCode].numOperands)
17 {
18   assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
19 }
20
21 // Constructor for instructions with variable #operands
22 MachineInstr::MachineInstr(MachineOpCode _opCode,
23                            unsigned      numOperands,
24                            OpCodeMask    _opCodeMask)
25   : opCode(_opCode),
26     opCodeMask(_opCodeMask),
27     operands(numOperands)
28 {
29 }
30
31 // 
32 // Support for replacing opcode and operands of a MachineInstr in place.
33 // This only resets the size of the operand vector and initializes it.
34 // The new operands must be set explicitly later.
35 // 
36 void
37 MachineInstr::replace(MachineOpCode _opCode,
38                       unsigned      numOperands,
39                       OpCodeMask    _opCodeMask)
40 {
41   opCode = _opCode;
42   opCodeMask = _opCodeMask;
43   operands.clear();
44   operands.resize(numOperands);
45 }
46
47 void
48 MachineInstr::SetMachineOperandVal(unsigned int i,
49                                    MachineOperand::MachineOperandType opType,
50                                    Value* V,
51                                    bool isdef,
52                                    bool isDefAndUse)
53 {
54   assert(i < operands.size());
55   operands[i].opType = opType;
56   operands[i].value = V;
57   operands[i].regNum = -1;
58   operands[i].flags = 0;
59
60   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
61     operands[i].markDef();
62   if (isDefAndUse)
63     operands[i].markDefAndUse();
64 }
65
66 void
67 MachineInstr::SetMachineOperandConst(unsigned i,
68                                 MachineOperand::MachineOperandType operandType,
69                                      int64_t intValue)
70 {
71   assert(i < operands.size());
72   assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
73          "immed. constant cannot be defined");
74
75   operands[i].opType = operandType;
76   operands[i].value = NULL;
77   operands[i].immedVal = intValue;
78   operands[i].regNum = -1;
79   operands[i].flags = 0;
80 }
81
82 void
83 MachineInstr::SetMachineOperandReg(unsigned i,
84                                    int regNum,
85                                    bool isdef) {
86   assert(i < operands.size());
87
88   operands[i].opType = MachineOperand::MO_MachineRegister;
89   operands[i].value = NULL;
90   operands[i].regNum = regNum;
91   operands[i].flags = 0;
92
93   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
94     operands[i].markDef();
95   insertUsedReg(regNum);
96 }
97
98 void
99 MachineInstr::SetRegForOperand(unsigned i, int regNum)
100 {
101   operands[i].setRegForValue(regNum);
102   insertUsedReg(regNum);
103 }
104
105
106 // Subsitute all occurrences of Value* oldVal with newVal in all operands
107 // and all implicit refs.  If defsOnly == true, substitute defs only.
108 unsigned
109 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
110 {
111   unsigned numSubst = 0;
112
113   // Subsitute operands
114   for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
115     if (*O == oldVal)
116       if (!defsOnly || O.isDef())
117         {
118           O.getMachineOperand().value = newVal;
119           ++numSubst;
120         }
121
122   // Subsitute implicit refs
123   for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
124     if (getImplicitRef(i) == oldVal)
125       if (!defsOnly || implicitRefIsDefined(i))
126         {
127           implicitRefs[i].Val = newVal;
128           ++numSubst;
129         }
130
131   return numSubst;
132 }
133
134
135 void
136 MachineInstr::dump() const 
137 {
138   cerr << "  " << *this;
139 }
140
141 static inline std::ostream&
142 OutputValue(std::ostream &os, const Value* val)
143 {
144   os << "(val ";
145   if (val && val->hasName())
146     return os << val->getName() << ")";
147   else
148     return os << (void*) val << ")";              // print address only
149 }
150
151 static inline std::ostream&
152 OutputReg(std::ostream &os, unsigned int regNum)
153 {
154   return os << "%mreg(" << regNum << ")";
155 }
156
157 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
158 {
159   os << TargetInstrDescriptors[minstr.opCode].opCodeString;
160   
161   for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
162     os << "\t" << minstr.getOperand(i);
163     if( minstr.operandIsDefined(i) ) 
164       os << "*";
165     if( minstr.operandIsDefinedAndUsed(i) ) 
166       os << "*";
167   }
168   
169   // code for printing implict references
170   unsigned NumOfImpRefs =  minstr.getNumImplicitRefs();
171   if(  NumOfImpRefs > 0 ) {
172     os << "\tImplicit: ";
173     for(unsigned z=0; z < NumOfImpRefs; z++) {
174       OutputValue(os, minstr.getImplicitRef(z)); 
175       if( minstr.implicitRefIsDefined(z)) os << "*";
176       if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
177       os << "\t";
178     }
179   }
180   
181   return os << "\n";
182 }
183
184 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
185 {
186   if (mop.opHiBits32())
187     os << "%lm(";
188   else if (mop.opLoBits32())
189     os << "%lo(";
190   else if (mop.opHiBits64())
191     os << "%hh(";
192   else if (mop.opLoBits64())
193     os << "%hm(";
194   
195   switch(mop.opType)
196     {
197     case MachineOperand::MO_VirtualRegister:
198       os << "%reg";
199       OutputValue(os, mop.getVRegValue());
200       if (mop.hasAllocatedReg())
201         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
202       break;
203     case MachineOperand::MO_CCRegister:
204       os << "%ccreg";
205       OutputValue(os, mop.getVRegValue());
206       if (mop.hasAllocatedReg())
207         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
208       break;
209     case MachineOperand::MO_MachineRegister:
210       OutputReg(os, mop.getMachineRegNum());
211       break;
212     case MachineOperand::MO_SignExtendedImmed:
213       os << (long)mop.immedVal;
214       break;
215     case MachineOperand::MO_UnextendedImmed:
216       os << (long)mop.immedVal;
217       break;
218     case MachineOperand::MO_PCRelativeDisp:
219       {
220         const Value* opVal = mop.getVRegValue();
221         bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
222         os << "%disp(" << (isLabel? "label " : "addr-of-val ");
223         if (opVal->hasName())
224           os << opVal->getName();
225         else
226           os << (const void*) opVal;
227         os << ")";
228         break;
229       }
230     default:
231       assert(0 && "Unrecognized operand type");
232       break;
233     }
234   
235   if (mop.flags &
236       (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 | 
237        MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
238     os << ")";
239   
240   return os;
241 }