716b40fbf1c1dcdadf5090636db37d8368fd9d2f
[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 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Support/LeakDetector.h"
20 #include "llvm/Support/Streams.h"
21 using namespace llvm;
22
23 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
24 /// TID NULL and no operands.
25 MachineInstr::MachineInstr()
26   : TID(0), NumImplicitOps(0), parent(0) {
27   // Make sure that we get added to a machine basicblock
28   LeakDetector::addGarbageObject(this);
29 }
30
31 void MachineInstr::addImplicitDefUseOperands() {
32   if (TID->ImplicitDefs)
33     for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) {
34       MachineOperand Op;
35       Op.opType = MachineOperand::MO_Register;
36       Op.IsDef = true;
37       Op.IsImp = true;
38       Op.IsKill = false;
39       Op.IsDead = false;
40       Op.contents.RegNo = *ImpDefs;
41       Op.offset = 0;
42       Operands.push_back(Op);
43     }
44   if (TID->ImplicitUses)
45     for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) {
46       MachineOperand Op;
47       Op.opType = MachineOperand::MO_Register;
48       Op.IsDef = false;
49       Op.IsImp = true;
50       Op.IsKill = false;
51       Op.IsDead = false;
52       Op.contents.RegNo = *ImpUses;
53       Op.offset = 0;
54       Operands.push_back(Op);
55     }
56 }
57
58 /// MachineInstr ctor - This constructor create a MachineInstr and add the
59 /// implicit operands. It reserves space for number of operands specified by
60 /// TargetInstrDescriptor or the numOperands if it is not zero. (for
61 /// instructions with variable number of operands).
62 MachineInstr::MachineInstr(const TargetInstrDescriptor &tid)
63   : TID(&tid), NumImplicitOps(0), parent(0) {
64   if (TID->ImplicitDefs)
65     for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
66       NumImplicitOps++;
67   if (TID->ImplicitUses)
68     for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
69       NumImplicitOps++;
70   Operands.reserve(NumImplicitOps + TID->numOperands);
71   addImplicitDefUseOperands();
72   // Make sure that we get added to a machine basicblock
73   LeakDetector::addGarbageObject(this);
74 }
75
76 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
77 /// MachineInstr is created and added to the end of the specified basic block.
78 ///
79 MachineInstr::MachineInstr(MachineBasicBlock *MBB,
80                            const TargetInstrDescriptor &tid)
81   : TID(&tid), NumImplicitOps(0), parent(0) {
82   assert(MBB && "Cannot use inserting ctor with null basic block!");
83   if (TID->ImplicitDefs)
84     for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
85       NumImplicitOps++;
86   if (TID->ImplicitUses)
87     for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
88       NumImplicitOps++;
89   Operands.reserve(NumImplicitOps + TID->numOperands);
90   addImplicitDefUseOperands();
91   // Make sure that we get added to a machine basicblock
92   LeakDetector::addGarbageObject(this);
93   MBB->push_back(this);  // Add instruction to end of basic block!
94 }
95
96 /// MachineInstr ctor - Copies MachineInstr arg exactly
97 ///
98 MachineInstr::MachineInstr(const MachineInstr &MI) {
99   TID = MI.getInstrDescriptor();
100   NumImplicitOps = MI.NumImplicitOps;
101   Operands.reserve(MI.getNumOperands());
102
103   // Add operands
104   for (unsigned i = 0; i != MI.getNumOperands(); ++i)
105     Operands.push_back(MI.getOperand(i));
106
107   // Set parent, next, and prev to null
108   parent = 0;
109   prev = 0;
110   next = 0;
111 }
112
113
114 MachineInstr::~MachineInstr() {
115   LeakDetector::removeGarbageObject(this);
116 }
117
118 /// getOpcode - Returns the opcode of this MachineInstr.
119 ///
120 const int MachineInstr::getOpcode() const {
121   return TID->Opcode;
122 }
123
124 /// removeFromParent - This method unlinks 'this' from the containing basic
125 /// block, and returns it, but does not delete it.
126 MachineInstr *MachineInstr::removeFromParent() {
127   assert(getParent() && "Not embedded in a basic block!");
128   getParent()->remove(this);
129   return this;
130 }
131
132
133 /// OperandComplete - Return true if it's illegal to add a new operand
134 ///
135 bool MachineInstr::OperandsComplete() const {
136   unsigned short NumOperands = TID->numOperands;
137   if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
138       getNumOperands()-NumImplicitOps >= NumOperands)
139     return true;  // Broken: we have all the operands of this instruction!
140   return false;
141 }
142
143 /// isIdenticalTo - Return true if this operand is identical to the specified
144 /// operand.
145 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
146   if (getType() != Other.getType()) return false;
147   
148   switch (getType()) {
149   default: assert(0 && "Unrecognized operand type");
150   case MachineOperand::MO_Register:
151     return getReg() == Other.getReg() && isDef() == Other.isDef();
152   case MachineOperand::MO_Immediate:
153     return getImm() == Other.getImm();
154   case MachineOperand::MO_MachineBasicBlock:
155     return getMBB() == Other.getMBB();
156   case MachineOperand::MO_FrameIndex:
157     return getFrameIndex() == Other.getFrameIndex();
158   case MachineOperand::MO_ConstantPoolIndex:
159     return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
160            getOffset() == Other.getOffset();
161   case MachineOperand::MO_JumpTableIndex:
162     return getJumpTableIndex() == Other.getJumpTableIndex();
163   case MachineOperand::MO_GlobalAddress:
164     return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
165   case MachineOperand::MO_ExternalSymbol:
166     return !strcmp(getSymbolName(), Other.getSymbolName()) &&
167            getOffset() == Other.getOffset();
168   }
169 }
170
171 /// findRegisterUseOperand() - Returns the MachineOperand that is a use of
172 /// the specific register or NULL if it is not found.
173 MachineOperand *MachineInstr::findRegisterUseOperand(unsigned Reg) {
174   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
175     MachineOperand &MO = getOperand(i);
176     if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
177       return &MO;
178   }
179   return NULL;
180 }
181   
182 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
183 ///
184 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
185   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
186     const MachineOperand &MO = MI->getOperand(i);
187     if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
188       continue;
189     for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
190       MachineOperand &MOp = getOperand(j);
191       if (!MOp.isIdenticalTo(MO))
192         continue;
193       if (MO.isKill())
194         MOp.setIsKill();
195       else
196         MOp.setIsDead();
197       break;
198     }
199   }
200 }
201
202 void MachineInstr::dump() const {
203   cerr << "  " << *this;
204 }
205
206 static inline void OutputReg(std::ostream &os, unsigned RegNo,
207                              const MRegisterInfo *MRI = 0) {
208   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
209     if (MRI)
210       os << "%" << MRI->get(RegNo).Name;
211     else
212       os << "%mreg(" << RegNo << ")";
213   } else
214     os << "%reg" << RegNo;
215 }
216
217 static void print(const MachineOperand &MO, std::ostream &OS,
218                   const TargetMachine *TM) {
219   const MRegisterInfo *MRI = 0;
220
221   if (TM) MRI = TM->getRegisterInfo();
222
223   switch (MO.getType()) {
224   case MachineOperand::MO_Register:
225     OutputReg(OS, MO.getReg(), MRI);
226     break;
227   case MachineOperand::MO_Immediate:
228     OS << MO.getImmedValue();
229     break;
230   case MachineOperand::MO_MachineBasicBlock:
231     OS << "mbb<"
232        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
233        << "," << (void*)MO.getMachineBasicBlock() << ">";
234     break;
235   case MachineOperand::MO_FrameIndex:
236     OS << "<fi#" << MO.getFrameIndex() << ">";
237     break;
238   case MachineOperand::MO_ConstantPoolIndex:
239     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
240     break;
241   case MachineOperand::MO_JumpTableIndex:
242     OS << "<jt#" << MO.getJumpTableIndex() << ">";
243     break;
244   case MachineOperand::MO_GlobalAddress:
245     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
246     if (MO.getOffset()) OS << "+" << MO.getOffset();
247     OS << ">";
248     break;
249   case MachineOperand::MO_ExternalSymbol:
250     OS << "<es:" << MO.getSymbolName();
251     if (MO.getOffset()) OS << "+" << MO.getOffset();
252     OS << ">";
253     break;
254   default:
255     assert(0 && "Unrecognized operand type");
256   }
257 }
258
259 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
260   unsigned StartOp = 0;
261
262    // Specialize printing if op#0 is definition
263   if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
264     ::print(getOperand(0), OS, TM);
265     OS << " = ";
266     ++StartOp;   // Don't print this operand again!
267   }
268
269   if (TID)
270     OS << TID->Name;
271
272   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
273     const MachineOperand& mop = getOperand(i);
274     if (i != StartOp)
275       OS << ",";
276     OS << " ";
277     ::print(mop, OS, TM);
278
279     if (mop.isReg()) {
280       if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
281         OS << "<";
282         bool NeedComma = false;
283         if (mop.isImplicit()) {
284           OS << (mop.isDef() ? "imp-def" : "imp-use");
285           NeedComma = true;
286         } else if (mop.isDef()) {
287           OS << "def";
288           NeedComma = true;
289         }
290         if (mop.isKill() || mop.isDead()) {
291           if (NeedComma)
292             OS << ",";
293           if (mop.isKill())
294             OS << "kill";
295           if (mop.isDead())
296             OS << "dead";
297         }
298         OS << ">";
299       }
300     }
301   }
302
303   OS << "\n";
304 }
305
306 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
307   // If the instruction is embedded into a basic block, we can find the target
308   // info for the instruction.
309   if (const MachineBasicBlock *MBB = MI.getParent()) {
310     const MachineFunction *MF = MBB->getParent();
311     if (MF)
312       MI.print(os, &MF->getTarget());
313     else
314       MI.print(os, 0);
315     return os;
316   }
317
318   // Otherwise, print it out in the "raw" format without symbolic register names
319   // and such.
320   os << MI.getInstrDescriptor()->Name;
321
322   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
323     os << "\t" << MI.getOperand(i);
324     if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
325       os << "<d>";
326   }
327
328   return os << "\n";
329 }
330
331 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
332   switch (MO.getType()) {
333   case MachineOperand::MO_Register:
334     OutputReg(OS, MO.getReg());
335     break;
336   case MachineOperand::MO_Immediate:
337     OS << (long)MO.getImmedValue();
338     break;
339   case MachineOperand::MO_MachineBasicBlock:
340     OS << "<mbb:"
341        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
342        << "@" << (void*)MO.getMachineBasicBlock() << ">";
343     break;
344   case MachineOperand::MO_FrameIndex:
345     OS << "<fi#" << MO.getFrameIndex() << ">";
346     break;
347   case MachineOperand::MO_ConstantPoolIndex:
348     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
349     break;
350   case MachineOperand::MO_JumpTableIndex:
351     OS << "<jt#" << MO.getJumpTableIndex() << ">";
352     break;
353   case MachineOperand::MO_GlobalAddress:
354     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
355     break;
356   case MachineOperand::MO_ExternalSymbol:
357     OS << "<es:" << MO.getSymbolName() << ">";
358     break;
359   default:
360     assert(0 && "Unrecognized operand type");
361     break;
362   }
363
364   return OS;
365 }