Factor this code out of llvm-prof
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
1 //===-- MachineInstr.cpp --------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
11
12 #include "llvm/CodeGen/MachineInstr.h"
13 #include "llvm/CodeGen/MachineBasicBlock.h"
14 #include "llvm/Value.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/MRegisterInfo.h"
18
19 namespace llvm {
20
21 // Global variable holding an array of descriptors for machine instructions.
22 // The actual object needs to be created separately for each target machine.
23 // This variable is initialized and reset by class TargetInstrInfo.
24 // 
25 // FIXME: This should be a property of the target so that more than one target
26 // at a time can be active...
27 //
28 extern const TargetInstrDescriptor *TargetInstrDescriptors;
29
30 // Constructor for instructions with variable #operands
31 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned  numOperands)
32   : opCode(OpCode),
33     opCodeFlags(0),
34     operands(numOperands, MachineOperand()),
35     numImplicitRefs(0)
36 {
37 }
38
39 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
40 /// not a resize for them.  It is expected that if you use this that you call
41 /// add* methods below to fill up the operands, instead of the Set methods.
42 /// Eventually, the "resizing" ctors will be phased out.
43 ///
44 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
45                            bool XX, bool YY)
46   : opCode(Opcode),
47     opCodeFlags(0),
48     numImplicitRefs(0)
49 {
50   operands.reserve(numOperands);
51 }
52
53 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
54 /// MachineInstr is created and added to the end of the specified basic block.
55 ///
56 MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
57                            unsigned numOperands)
58   : opCode(Opcode),
59     opCodeFlags(0),
60     numImplicitRefs(0)
61 {
62   assert(MBB && "Cannot use inserting ctor with null basic block!");
63   operands.reserve(numOperands);
64   MBB->push_back(this);  // Add instruction to end of basic block!
65 }
66
67
68 // OperandComplete - Return true if it's illegal to add a new operand
69 bool MachineInstr::OperandsComplete() const
70 {
71   int NumOperands = TargetInstrDescriptors[opCode].numOperands;
72   if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
73     return true;  // Broken: we have all the operands of this instruction!
74   return false;
75 }
76
77
78 // 
79 // Support for replacing opcode and operands of a MachineInstr in place.
80 // This only resets the size of the operand vector and initializes it.
81 // The new operands must be set explicitly later.
82 // 
83 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
84 {
85   assert(getNumImplicitRefs() == 0 &&
86          "This is probably broken because implicit refs are going to be lost.");
87   opCode = Opcode;
88   operands.clear();
89   operands.resize(numOperands, MachineOperand());
90 }
91
92 void MachineInstr::SetMachineOperandVal(unsigned i,
93                                         MachineOperand::MachineOperandType opTy,
94                                         Value* V) {
95   assert(i < operands.size());          // may be explicit or implicit op
96   operands[i].opType = opTy;
97   operands[i].value = V;
98   operands[i].regNum = -1;
99 }
100
101 void
102 MachineInstr::SetMachineOperandConst(unsigned i,
103                                 MachineOperand::MachineOperandType operandType,
104                                      int64_t intValue)
105 {
106   assert(i < getNumOperands());          // must be explicit op
107   assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
108          "immed. constant cannot be defined");
109
110   operands[i].opType = operandType;
111   operands[i].value = NULL;
112   operands[i].immedVal = intValue;
113   operands[i].regNum = -1;
114   operands[i].flags = 0;
115 }
116
117 void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
118   assert(i < getNumOperands());          // must be explicit op
119
120   operands[i].opType = MachineOperand::MO_MachineRegister;
121   operands[i].value = NULL;
122   operands[i].regNum = regNum;
123 }
124
125 void
126 MachineInstr::SetRegForOperand(unsigned i, int regNum)
127 {
128   assert(i < getNumOperands());          // must be explicit op
129   operands[i].setRegForValue(regNum);
130 }
131
132 void
133 MachineInstr::SetRegForImplicitRef(unsigned i, int regNum)
134 {
135   getImplicitOp(i).setRegForValue(regNum);
136 }
137
138
139 // Substitute all occurrences of Value* oldVal with newVal in all operands
140 // and all implicit refs.
141 // If defsOnly == true, substitute defs only.
142 unsigned
143 MachineInstr::substituteValue(const Value* oldVal, Value* newVal,
144                               bool defsOnly, bool notDefsAndUses,
145                               bool& someArgsWereIgnored)
146 {
147   assert((!defsOnly || !notDefsAndUses) &&
148          "notDefsAndUses is irrelevant if defsOnly == true.");
149   
150   unsigned numSubst = 0;
151
152   // Substitute operands
153   for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
154     if (*O == oldVal)
155       if (!defsOnly ||
156           notDefsAndUses && (O.isDef() && !O.isUse()) ||
157           !notDefsAndUses && O.isDef())
158         {
159           O.getMachineOperand().value = newVal;
160           ++numSubst;
161         }
162       else
163         someArgsWereIgnored = true;
164
165   // Substitute implicit refs
166   for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
167     if (getImplicitRef(i) == oldVal)
168       if (!defsOnly ||
169           notDefsAndUses && (getImplicitOp(i).isDef() && !getImplicitOp(i).isUse()) ||
170           !notDefsAndUses && getImplicitOp(i).isDef())
171         {
172           getImplicitOp(i).value = newVal;
173           ++numSubst;
174         }
175       else
176         someArgsWereIgnored = true;
177
178   return numSubst;
179 }
180
181
182 void
183 MachineInstr::dump() const 
184 {
185   std::cerr << "  " << *this;
186 }
187
188 static inline std::ostream&
189 OutputValue(std::ostream &os, const Value* val)
190 {
191   os << "(val ";
192   os << (void*) val;                    // print address always
193   if (val && val->hasName())
194     os << " " << val->getName() << ")"; // print name also, if available
195   return os;
196 }
197
198 static inline void OutputReg(std::ostream &os, unsigned RegNo,
199                              const MRegisterInfo *MRI = 0) {
200   if (MRI) {
201     if (RegNo < MRegisterInfo::FirstVirtualRegister)
202       os << "%" << MRI->get(RegNo).Name;
203     else
204       os << "%reg" << RegNo;
205   } else
206     os << "%mreg(" << RegNo << ")";
207 }
208
209 static void print(const MachineOperand &MO, std::ostream &OS,
210                   const TargetMachine &TM) {
211   const MRegisterInfo *MRI = TM.getRegisterInfo();
212   bool CloseParen = true;
213   if (MO.isHiBits32())
214     OS << "%lm(";
215   else if (MO.isLoBits32())
216     OS << "%lo(";
217   else if (MO.isHiBits64())
218     OS << "%hh(";
219   else if (MO.isLoBits64())
220     OS << "%hm(";
221   else
222     CloseParen = false;
223   
224   switch (MO.getType()) {
225   case MachineOperand::MO_VirtualRegister:
226     if (MO.getVRegValue()) {
227       OS << "%reg";
228       OutputValue(OS, MO.getVRegValue());
229       if (MO.hasAllocatedReg())
230         OS << "==";
231     }
232     if (MO.hasAllocatedReg())
233       OutputReg(OS, MO.getAllocatedRegNum(), MRI);
234     break;
235   case MachineOperand::MO_CCRegister:
236     OS << "%ccreg";
237     OutputValue(OS, MO.getVRegValue());
238     if (MO.hasAllocatedReg()) {
239       OS << "==";
240       OutputReg(OS, MO.getAllocatedRegNum(), MRI);
241     }
242     break;
243   case MachineOperand::MO_MachineRegister:
244     OutputReg(OS, MO.getMachineRegNum(), MRI);
245     break;
246   case MachineOperand::MO_SignExtendedImmed:
247     OS << (long)MO.getImmedValue();
248     break;
249   case MachineOperand::MO_UnextendedImmed:
250     OS << (long)MO.getImmedValue();
251     break;
252   case MachineOperand::MO_PCRelativeDisp: {
253     const Value* opVal = MO.getVRegValue();
254     bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
255     OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
256     if (opVal->hasName())
257       OS << opVal->getName();
258     else
259       OS << (const void*) opVal;
260     OS << ")";
261     break;
262   }
263   case MachineOperand::MO_MachineBasicBlock:
264     OS << "bb<"
265        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
266        << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
267     break;
268   case MachineOperand::MO_FrameIndex:
269     OS << "<fi#" << MO.getFrameIndex() << ">";
270     break;
271   case MachineOperand::MO_ConstantPoolIndex:
272     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
273     break;
274   case MachineOperand::MO_GlobalAddress:
275     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
276     break;
277   case MachineOperand::MO_ExternalSymbol:
278     OS << "<es:" << MO.getSymbolName() << ">";
279     break;
280   default:
281     assert(0 && "Unrecognized operand type");
282   }
283
284   if (CloseParen)
285     OS << ")";
286 }
287
288 void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const {
289   unsigned StartOp = 0;
290
291    // Specialize printing if op#0 is definition
292   if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
293       llvm::print(getOperand(0), OS, TM);
294     OS << " = ";
295     ++StartOp;   // Don't print this operand again!
296   }
297   OS << TM.getInstrInfo().getName(getOpcode());
298   
299   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
300     const MachineOperand& mop = getOperand(i);
301     if (i != StartOp)
302       OS << ",";
303     OS << " ";
304     llvm::print(mop, OS, TM);
305     
306     if (mop.isDef())
307       if (mop.isUse())
308         OS << "<def&use>";
309       else
310         OS << "<def>";
311   }
312     
313   // code for printing implicit references
314   if (getNumImplicitRefs()) {
315     OS << "\tImplicitRefs: ";
316     for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
317       OS << "\t";
318       OutputValue(OS, getImplicitRef(i));
319       if (getImplicitOp(i).isDef())
320           if (getImplicitOp(i).isUse())
321             OS << "<def&use>";
322           else
323             OS << "<def>";
324     }
325   }
326   
327   OS << "\n";
328 }
329
330
331 std::ostream &operator<<(std::ostream& os, const MachineInstr& MI)
332 {
333   os << TargetInstrDescriptors[MI.opCode].Name;
334   
335   for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) {
336     os << "\t" << MI.getOperand(i);
337     if (MI.getOperand(i).isDef())
338       if (MI.getOperand(i).isUse())
339         os << "<d&u>";
340       else
341         os << "<d>";
342   }
343   
344   // code for printing implicit references
345   unsigned NumOfImpRefs = MI.getNumImplicitRefs();
346   if (NumOfImpRefs > 0) {
347     os << "\tImplicit: ";
348     for (unsigned z=0; z < NumOfImpRefs; z++) {
349       OutputValue(os, MI.getImplicitRef(z)); 
350       if (MI.getImplicitOp(z).isDef())
351           if (MI.getImplicitOp(z).isUse())
352             os << "<d&u>";
353           else
354             os << "<d>";
355       os << "\t";
356     }
357   }
358   
359   return os << "\n";
360 }
361
362 std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO)
363 {
364   if (MO.isHiBits32())
365     OS << "%lm(";
366   else if (MO.isLoBits32())
367     OS << "%lo(";
368   else if (MO.isHiBits64())
369     OS << "%hh(";
370   else if (MO.isLoBits64())
371     OS << "%hm(";
372   
373   switch (MO.getType())
374     {
375     case MachineOperand::MO_VirtualRegister:
376       if (MO.hasAllocatedReg())
377         OutputReg(OS, MO.getAllocatedRegNum());
378
379       if (MO.getVRegValue()) {
380         if (MO.hasAllocatedReg()) OS << "==";
381         OS << "%vreg";
382         OutputValue(OS, MO.getVRegValue());
383       }
384       break;
385     case MachineOperand::MO_CCRegister:
386       OS << "%ccreg";
387       OutputValue(OS, MO.getVRegValue());
388       if (MO.hasAllocatedReg()) {
389         OS << "==";
390         OutputReg(OS, MO.getAllocatedRegNum());
391       }
392       break;
393     case MachineOperand::MO_MachineRegister:
394       OutputReg(OS, MO.getMachineRegNum());
395       break;
396     case MachineOperand::MO_SignExtendedImmed:
397       OS << (long)MO.getImmedValue();
398       break;
399     case MachineOperand::MO_UnextendedImmed:
400       OS << (long)MO.getImmedValue();
401       break;
402     case MachineOperand::MO_PCRelativeDisp:
403       {
404         const Value* opVal = MO.getVRegValue();
405         bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
406         OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
407         if (opVal->hasName())
408           OS << opVal->getName();
409         else
410           OS << (const void*) opVal;
411         OS << ")";
412         break;
413       }
414     case MachineOperand::MO_MachineBasicBlock:
415       OS << "bb<"
416          << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
417          << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
418       break;
419     case MachineOperand::MO_FrameIndex:
420       OS << "<fi#" << MO.getFrameIndex() << ">";
421       break;
422     case MachineOperand::MO_ConstantPoolIndex:
423       OS << "<cp#" << MO.getConstantPoolIndex() << ">";
424       break;
425     case MachineOperand::MO_GlobalAddress:
426       OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
427       break;
428     case MachineOperand::MO_ExternalSymbol:
429       OS << "<es:" << MO.getSymbolName() << ">";
430       break;
431     default:
432       assert(0 && "Unrecognized operand type");
433       break;
434     }
435   
436   if (MO.flags &
437       (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 | 
438        MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
439     OS << ")";
440   
441   return OS;
442 }
443
444 } // End llvm namespace