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