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