1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Methods common to all machine instructions.
12 //===----------------------------------------------------------------------===//
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"
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);
31 void MachineInstr::addImplicitDefUseOperands() {
32 if (TID->ImplicitDefs)
33 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) {
35 Op.opType = MachineOperand::MO_Register;
40 Op.contents.RegNo = *ImpDefs;
42 Operands.push_back(Op);
44 if (TID->ImplicitUses)
45 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) {
47 Op.opType = MachineOperand::MO_Register;
52 Op.contents.RegNo = *ImpUses;
54 Operands.push_back(Op);
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)
67 if (TID->ImplicitUses)
68 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
70 Operands.reserve(NumImplicitOps + TID->numOperands);
71 addImplicitDefUseOperands();
72 // Make sure that we get added to a machine basicblock
73 LeakDetector::addGarbageObject(this);
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.
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)
86 if (TID->ImplicitUses)
87 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
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!
96 /// MachineInstr ctor - Copies MachineInstr arg exactly
98 MachineInstr::MachineInstr(const MachineInstr &MI) {
99 TID = MI.getInstrDescriptor();
100 NumImplicitOps = MI.NumImplicitOps;
101 Operands.reserve(MI.getNumOperands());
104 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
105 Operands.push_back(MI.getOperand(i));
107 // Set parent, next, and prev to null
114 MachineInstr::~MachineInstr() {
115 LeakDetector::removeGarbageObject(this);
118 /// getOpcode - Returns the opcode of this MachineInstr.
120 const int MachineInstr::getOpcode() const {
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);
133 /// OperandComplete - Return true if it's illegal to add a new operand
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!
143 /// isIdenticalTo - Return true if this operand is identical to the specified
145 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
146 if (getType() != Other.getType()) return false;
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();
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)
182 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
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()))
189 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
190 MachineOperand &MOp = getOperand(j);
191 if (!MOp.isIdenticalTo(MO))
202 void MachineInstr::dump() const {
203 cerr << " " << *this;
206 static inline void OutputReg(std::ostream &os, unsigned RegNo,
207 const MRegisterInfo *MRI = 0) {
208 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
210 os << "%" << MRI->get(RegNo).Name;
212 os << "%mreg(" << RegNo << ")";
214 os << "%reg" << RegNo;
217 static void print(const MachineOperand &MO, std::ostream &OS,
218 const TargetMachine *TM) {
219 const MRegisterInfo *MRI = 0;
221 if (TM) MRI = TM->getRegisterInfo();
223 switch (MO.getType()) {
224 case MachineOperand::MO_Register:
225 OutputReg(OS, MO.getReg(), MRI);
227 case MachineOperand::MO_Immediate:
228 OS << MO.getImmedValue();
230 case MachineOperand::MO_MachineBasicBlock:
232 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
233 << "," << (void*)MO.getMachineBasicBlock() << ">";
235 case MachineOperand::MO_FrameIndex:
236 OS << "<fi#" << MO.getFrameIndex() << ">";
238 case MachineOperand::MO_ConstantPoolIndex:
239 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
241 case MachineOperand::MO_JumpTableIndex:
242 OS << "<jt#" << MO.getJumpTableIndex() << ">";
244 case MachineOperand::MO_GlobalAddress:
245 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
246 if (MO.getOffset()) OS << "+" << MO.getOffset();
249 case MachineOperand::MO_ExternalSymbol:
250 OS << "<es:" << MO.getSymbolName();
251 if (MO.getOffset()) OS << "+" << MO.getOffset();
255 assert(0 && "Unrecognized operand type");
259 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
260 unsigned StartOp = 0;
262 // Specialize printing if op#0 is definition
263 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
264 ::print(getOperand(0), OS, TM);
266 ++StartOp; // Don't print this operand again!
272 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
273 const MachineOperand& mop = getOperand(i);
277 ::print(mop, OS, TM);
280 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
282 bool NeedComma = false;
283 if (mop.isImplicit()) {
284 OS << (mop.isDef() ? "imp-def" : "imp-use");
286 } else if (mop.isDef()) {
290 if (mop.isKill() || mop.isDead()) {
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();
312 MI.print(os, &MF->getTarget());
318 // Otherwise, print it out in the "raw" format without symbolic register names
320 os << MI.getInstrDescriptor()->Name;
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())
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());
336 case MachineOperand::MO_Immediate:
337 OS << (long)MO.getImmedValue();
339 case MachineOperand::MO_MachineBasicBlock:
341 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
342 << "@" << (void*)MO.getMachineBasicBlock() << ">";
344 case MachineOperand::MO_FrameIndex:
345 OS << "<fi#" << MO.getFrameIndex() << ">";
347 case MachineOperand::MO_ConstantPoolIndex:
348 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
350 case MachineOperand::MO_JumpTableIndex:
351 OS << "<jt#" << MO.getJumpTableIndex() << ">";
353 case MachineOperand::MO_GlobalAddress:
354 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
356 case MachineOperand::MO_ExternalSymbol:
357 OS << "<es:" << MO.getSymbolName() << ">";
360 assert(0 && "Unrecognized operand type");