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"
24 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
25 /// TID NULL and no operands.
26 MachineInstr::MachineInstr()
27 : TID(0), NumImplicitOps(0), parent(0) {
28 // Make sure that we get added to a machine basicblock
29 LeakDetector::addGarbageObject(this);
32 void MachineInstr::addImplicitDefUseOperands() {
33 if (TID->ImplicitDefs)
34 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) {
36 Op.opType = MachineOperand::MO_Register;
41 Op.contents.RegNo = *ImpDefs;
43 Operands.push_back(Op);
45 if (TID->ImplicitUses)
46 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) {
48 Op.opType = MachineOperand::MO_Register;
53 Op.contents.RegNo = *ImpUses;
55 Operands.push_back(Op);
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).
63 MachineInstr::MachineInstr(const TargetInstrDescriptor &tid)
64 : TID(&tid), NumImplicitOps(0), parent(0) {
65 if (TID->ImplicitDefs)
66 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
68 if (TID->ImplicitUses)
69 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
71 Operands.reserve(NumImplicitOps + TID->numOperands);
72 addImplicitDefUseOperands();
73 // Make sure that we get added to a machine basicblock
74 LeakDetector::addGarbageObject(this);
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.
80 MachineInstr::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)
87 if (TID->ImplicitUses)
88 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
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!
97 /// MachineInstr ctor - Copies MachineInstr arg exactly
99 MachineInstr::MachineInstr(const MachineInstr &MI) {
100 TID = MI.getInstrDescriptor();
101 NumImplicitOps = MI.NumImplicitOps;
102 Operands.reserve(MI.getNumOperands());
105 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
106 Operands.push_back(MI.getOperand(i));
108 // Set parent, next, and prev to null
115 MachineInstr::~MachineInstr() {
116 LeakDetector::removeGarbageObject(this);
119 /// getOpcode - Returns the opcode of this MachineInstr.
121 const int MachineInstr::getOpcode() const {
125 /// removeFromParent - This method unlinks 'this' from the containing basic
126 /// block, and returns it, but does not delete it.
127 MachineInstr *MachineInstr::removeFromParent() {
128 assert(getParent() && "Not embedded in a basic block!");
129 getParent()->remove(this);
134 /// OperandComplete - Return true if it's illegal to add a new operand
136 bool 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!
144 /// isIdenticalTo - Return true if this operand is identical to the specified
146 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
147 if (getType() != Other.getType()) return false;
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();
172 /// findRegisterUseOperand() - Returns the MachineOperand that is a use of
173 /// the specific register or NULL if it is not found.
174 MachineOperand *MachineInstr::findRegisterUseOperand(unsigned Reg) {
175 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
176 MachineOperand &MO = getOperand(i);
177 if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
183 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
185 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
186 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
187 const MachineOperand &MO = MI->getOperand(i);
188 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
190 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
191 MachineOperand &MOp = getOperand(j);
192 if (!MOp.isIdenticalTo(MO))
203 void MachineInstr::dump() const {
204 llvm_cerr << " " << *this;
207 static inline void OutputReg(std::ostream &os, unsigned RegNo,
208 const MRegisterInfo *MRI = 0) {
209 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
211 os << "%" << MRI->get(RegNo).Name;
213 os << "%mreg(" << RegNo << ")";
215 os << "%reg" << RegNo;
218 static void print(const MachineOperand &MO, std::ostream &OS,
219 const TargetMachine *TM) {
220 const MRegisterInfo *MRI = 0;
222 if (TM) MRI = TM->getRegisterInfo();
224 switch (MO.getType()) {
225 case MachineOperand::MO_Register:
226 OutputReg(OS, MO.getReg(), MRI);
228 case MachineOperand::MO_Immediate:
229 OS << MO.getImmedValue();
231 case MachineOperand::MO_MachineBasicBlock:
233 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
234 << "," << (void*)MO.getMachineBasicBlock() << ">";
236 case MachineOperand::MO_FrameIndex:
237 OS << "<fi#" << MO.getFrameIndex() << ">";
239 case MachineOperand::MO_ConstantPoolIndex:
240 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
242 case MachineOperand::MO_JumpTableIndex:
243 OS << "<jt#" << MO.getJumpTableIndex() << ">";
245 case MachineOperand::MO_GlobalAddress:
246 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
247 if (MO.getOffset()) OS << "+" << MO.getOffset();
250 case MachineOperand::MO_ExternalSymbol:
251 OS << "<es:" << MO.getSymbolName();
252 if (MO.getOffset()) OS << "+" << MO.getOffset();
256 assert(0 && "Unrecognized operand type");
260 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
261 unsigned StartOp = 0;
263 // Specialize printing if op#0 is definition
264 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
265 ::print(getOperand(0), OS, TM);
267 ++StartOp; // Don't print this operand again!
273 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
274 const MachineOperand& mop = getOperand(i);
278 ::print(mop, OS, TM);
281 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
283 bool NeedComma = false;
284 if (mop.isImplicit()) {
285 OS << (mop.isDef() ? "imp-def" : "imp-use");
287 } else if (mop.isDef()) {
291 if (mop.isKill() || mop.isDead()) {
307 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
308 // If the instruction is embedded into a basic block, we can find the target
309 // info for the instruction.
310 if (const MachineBasicBlock *MBB = MI.getParent()) {
311 const MachineFunction *MF = MBB->getParent();
313 MI.print(os, &MF->getTarget());
319 // Otherwise, print it out in the "raw" format without symbolic register names
321 os << MI.getInstrDescriptor()->Name;
323 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
324 os << "\t" << MI.getOperand(i);
325 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
332 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
333 switch (MO.getType()) {
334 case MachineOperand::MO_Register:
335 OutputReg(OS, MO.getReg());
337 case MachineOperand::MO_Immediate:
338 OS << (long)MO.getImmedValue();
340 case MachineOperand::MO_MachineBasicBlock:
342 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
343 << "@" << (void*)MO.getMachineBasicBlock() << ">";
345 case MachineOperand::MO_FrameIndex:
346 OS << "<fi#" << MO.getFrameIndex() << ">";
348 case MachineOperand::MO_ConstantPoolIndex:
349 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
351 case MachineOperand::MO_JumpTableIndex:
352 OS << "<jt#" << MO.getJumpTableIndex() << ">";
354 case MachineOperand::MO_GlobalAddress:
355 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
357 case MachineOperand::MO_ExternalSymbol:
358 OS << "<es:" << MO.getSymbolName() << ">";
361 assert(0 && "Unrecognized operand type");