MachineInstr.cpp revision 438f7bc67cf235ccee7e6f7ac7f4ae2186eb8020
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 <iostream>
21
22using namespace llvm;
23
24// Global variable holding an array of descriptors for machine instructions.
25// The actual object needs to be created separately for each target machine.
26// This variable is initialized and reset by class TargetInstrInfo.
27//
28// FIXME: This should be a property of the target so that more than one target
29// at a time can be active...
30//
31namespace llvm {
32  extern const TargetInstrDescriptor *TargetInstrDescriptors;
33}
34
35/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
36/// not a resize for them.  It is expected that if you use this that you call
37/// add* methods below to fill up the operands, instead of the Set methods.
38/// Eventually, the "resizing" ctors will be phased out.
39///
40MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41  : Opcode(opcode), parent(0) {
42  Operands.reserve(numOperands);
43  // Make sure that we get added to a machine basicblock
44  LeakDetector::addGarbageObject(this);
45}
46
47/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
48/// MachineInstr is created and added to the end of the specified basic block.
49///
50MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
51                           unsigned numOperands)
52  : Opcode(opcode), parent(0) {
53  assert(MBB && "Cannot use inserting ctor with null basic block!");
54  Operands.reserve(numOperands);
55  // Make sure that we get added to a machine basicblock
56  LeakDetector::addGarbageObject(this);
57  MBB->push_back(this);  // Add instruction to end of basic block!
58}
59
60/// MachineInstr ctor - Copies MachineInstr arg exactly
61///
62MachineInstr::MachineInstr(const MachineInstr &MI) {
63  Opcode = MI.getOpcode();
64  Operands.reserve(MI.getNumOperands());
65
66  // Add operands
67  for (unsigned i = 0; i != MI.getNumOperands(); ++i)
68    Operands.push_back(MI.getOperand(i));
69
70  // Set parent, next, and prev to null
71  parent = 0;
72  prev = 0;
73  next = 0;
74}
75
76
77MachineInstr::~MachineInstr() {
78  LeakDetector::removeGarbageObject(this);
79}
80
81/// removeFromParent - This method unlinks 'this' from the containing basic
82/// block, and returns it, but does not delete it.
83MachineInstr *MachineInstr::removeFromParent() {
84  assert(getParent() && "Not embedded in a basic block!");
85  getParent()->remove(this);
86  return this;
87}
88
89
90/// OperandComplete - Return true if it's illegal to add a new operand
91///
92bool MachineInstr::OperandsComplete() const {
93  int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
94  if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
95      getNumOperands() >= (unsigned)NumOperands)
96    return true;  // Broken: we have all the operands of this instruction!
97  return false;
98}
99
100/// isIdenticalTo - Return true if this operand is identical to the specified
101/// operand.
102bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
103  if (getType() != Other.getType()) return false;
104
105  switch (getType()) {
106  default: assert(0 && "Unrecognized operand type");
107  case MachineOperand::MO_Register:
108    return getReg() == Other.getReg() && isDef() == Other.isDef();
109  case MachineOperand::MO_Immediate:
110    return getImm() == Other.getImm();
111  case MachineOperand::MO_MachineBasicBlock:
112    return getMBB() == Other.getMBB();
113  case MachineOperand::MO_FrameIndex:
114    return getFrameIndex() == Other.getFrameIndex();
115  case MachineOperand::MO_ConstantPoolIndex:
116    return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
117           getOffset() == Other.getOffset();
118  case MachineOperand::MO_JumpTableIndex:
119    return getJumpTableIndex() == Other.getJumpTableIndex();
120  case MachineOperand::MO_GlobalAddress:
121    return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
122  case MachineOperand::MO_ExternalSymbol:
123    return !strcmp(getSymbolName(), Other.getSymbolName()) &&
124           getOffset() == Other.getOffset();
125  }
126}
127
128
129void MachineInstr::dump() const {
130  std::cerr << "  " << *this;
131}
132
133static inline void OutputReg(std::ostream &os, unsigned RegNo,
134                             const MRegisterInfo *MRI = 0) {
135  if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
136    if (MRI)
137      os << "%" << MRI->get(RegNo).Name;
138    else
139      os << "%mreg(" << RegNo << ")";
140  } else
141    os << "%reg" << RegNo;
142}
143
144static void print(const MachineOperand &MO, std::ostream &OS,
145                  const TargetMachine *TM) {
146  const MRegisterInfo *MRI = 0;
147
148  if (TM) MRI = TM->getRegisterInfo();
149
150  switch (MO.getType()) {
151  case MachineOperand::MO_Register:
152    OutputReg(OS, MO.getReg(), MRI);
153    break;
154  case MachineOperand::MO_Immediate:
155    OS << MO.getImmedValue();
156    break;
157  case MachineOperand::MO_MachineBasicBlock:
158    OS << "mbb<"
159       << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
160       << "," << (void*)MO.getMachineBasicBlock() << ">";
161    break;
162  case MachineOperand::MO_FrameIndex:
163    OS << "<fi#" << MO.getFrameIndex() << ">";
164    break;
165  case MachineOperand::MO_ConstantPoolIndex:
166    OS << "<cp#" << MO.getConstantPoolIndex() << ">";
167    break;
168  case MachineOperand::MO_JumpTableIndex:
169    OS << "<jt#" << MO.getJumpTableIndex() << ">";
170    break;
171  case MachineOperand::MO_GlobalAddress:
172    OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
173    if (MO.getOffset()) OS << "+" << MO.getOffset();
174    OS << ">";
175    break;
176  case MachineOperand::MO_ExternalSymbol:
177    OS << "<es:" << MO.getSymbolName();
178    if (MO.getOffset()) OS << "+" << MO.getOffset();
179    OS << ">";
180    break;
181  default:
182    assert(0 && "Unrecognized operand type");
183  }
184}
185
186void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
187  unsigned StartOp = 0;
188
189   // Specialize printing if op#0 is definition
190  if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
191    ::print(getOperand(0), OS, TM);
192    OS << " = ";
193    ++StartOp;   // Don't print this operand again!
194  }
195
196  // Must check if Target machine is not null because machine BB could not
197  // be attached to a Machine function yet
198  if (TM)
199    OS << TM->getInstrInfo()->getName(getOpcode());
200
201  for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
202    const MachineOperand& mop = getOperand(i);
203    if (i != StartOp)
204      OS << ",";
205    OS << " ";
206    ::print(mop, OS, TM);
207
208    if (mop.isReg()) {
209      if (mop.isImplicit())
210        OS << (mop.isDef() ? "<imp-def>" : "<imp-use>");
211      else if (mop.isDef())
212        OS << "<def>";
213    }
214  }
215
216  OS << "\n";
217}
218
219std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
220  // If the instruction is embedded into a basic block, we can find the target
221  // info for the instruction.
222  if (const MachineBasicBlock *MBB = MI.getParent()) {
223    const MachineFunction *MF = MBB->getParent();
224    if (MF)
225      MI.print(os, &MF->getTarget());
226    else
227      MI.print(os, 0);
228    return os;
229  }
230
231  // Otherwise, print it out in the "raw" format without symbolic register names
232  // and such.
233  os << TargetInstrDescriptors[MI.getOpcode()].Name;
234
235  for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
236    os << "\t" << MI.getOperand(i);
237    if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
238      os << "<d>";
239  }
240
241  return os << "\n";
242}
243
244std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
245  switch (MO.getType()) {
246  case MachineOperand::MO_Register:
247    OutputReg(OS, MO.getReg());
248    break;
249  case MachineOperand::MO_Immediate:
250    OS << (long)MO.getImmedValue();
251    break;
252  case MachineOperand::MO_MachineBasicBlock:
253    OS << "<mbb:"
254       << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
255       << "@" << (void*)MO.getMachineBasicBlock() << ">";
256    break;
257  case MachineOperand::MO_FrameIndex:
258    OS << "<fi#" << MO.getFrameIndex() << ">";
259    break;
260  case MachineOperand::MO_ConstantPoolIndex:
261    OS << "<cp#" << MO.getConstantPoolIndex() << ">";
262    break;
263  case MachineOperand::MO_JumpTableIndex:
264    OS << "<jt#" << MO.getJumpTableIndex() << ">";
265    break;
266  case MachineOperand::MO_GlobalAddress:
267    OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
268    break;
269  case MachineOperand::MO_ExternalSymbol:
270    OS << "<es:" << MO.getSymbolName() << ">";
271    break;
272  default:
273    assert(0 && "Unrecognized operand type");
274    break;
275  }
276
277  return OS;
278}
279