1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Methods common to all machine instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/Value.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/MRegisterInfo.h"
20 #include "llvm/Support/LeakDetector.h"
21 #include "llvm/Support/Streams.h"
25 //===----------------------------------------------------------------------===//
26 // MachineOperand Implementation
27 //===----------------------------------------------------------------------===//
29 /// isIdenticalTo - Return true if this operand is identical to the specified
31 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
32 if (getType() != Other.getType()) return false;
35 default: assert(0 && "Unrecognized operand type");
36 case MachineOperand::MO_Register:
37 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
38 getSubReg() == Other.getSubReg();
39 case MachineOperand::MO_Immediate:
40 return getImm() == Other.getImm();
41 case MachineOperand::MO_MachineBasicBlock:
42 return getMBB() == Other.getMBB();
43 case MachineOperand::MO_FrameIndex:
44 return getIndex() == Other.getIndex();
45 case MachineOperand::MO_ConstantPoolIndex:
46 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
47 case MachineOperand::MO_JumpTableIndex:
48 return getIndex() == Other.getIndex();
49 case MachineOperand::MO_GlobalAddress:
50 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
51 case MachineOperand::MO_ExternalSymbol:
52 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
53 getOffset() == Other.getOffset();
57 /// print - Print the specified machine operand.
59 void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
61 case MachineOperand::MO_Register:
62 if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) {
63 OS << "%reg" << getReg();
65 // If the instruction is embedded into a basic block, we can find the
67 // info for the instruction.
69 if (const MachineInstr *MI = getParent())
70 if (const MachineBasicBlock *MBB = MI->getParent())
71 if (const MachineFunction *MF = MBB->getParent())
72 TM = &MF->getTarget();
75 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
77 OS << "%mreg" << getReg();
80 if (isDef() || isKill() || isDead() || isImplicit()) {
82 bool NeedComma = false;
84 OS << (isDef() ? "imp-def" : "imp-use");
90 if (isKill() || isDead()) {
91 if (NeedComma) OS << ",";
92 if (isKill()) OS << "kill";
93 if (isDead()) OS << "dead";
98 case MachineOperand::MO_Immediate:
101 case MachineOperand::MO_MachineBasicBlock:
103 << ((Value*)getMBB()->getBasicBlock())->getName()
104 << "," << (void*)getMBB() << ">";
106 case MachineOperand::MO_FrameIndex:
107 OS << "<fi#" << getIndex() << ">";
109 case MachineOperand::MO_ConstantPoolIndex:
110 OS << "<cp#" << getIndex();
111 if (getOffset()) OS << "+" << getOffset();
114 case MachineOperand::MO_JumpTableIndex:
115 OS << "<jt#" << getIndex() << ">";
117 case MachineOperand::MO_GlobalAddress:
118 OS << "<ga:" << ((Value*)getGlobal())->getName();
119 if (getOffset()) OS << "+" << getOffset();
122 case MachineOperand::MO_ExternalSymbol:
123 OS << "<es:" << getSymbolName();
124 if (getOffset()) OS << "+" << getOffset();
128 assert(0 && "Unrecognized operand type");
132 //===----------------------------------------------------------------------===//
133 // MachineInstr Implementation
134 //===----------------------------------------------------------------------===//
136 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
137 /// TID NULL and no operands.
138 MachineInstr::MachineInstr()
139 : TID(0), NumImplicitOps(0), Parent(0) {
140 // Make sure that we get added to a machine basicblock
141 LeakDetector::addGarbageObject(this);
144 void MachineInstr::addImplicitDefUseOperands() {
145 if (TID->ImplicitDefs)
146 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
147 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
148 if (TID->ImplicitUses)
149 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
150 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
153 /// MachineInstr ctor - This constructor create a MachineInstr and add the
154 /// implicit operands. It reserves space for number of operands specified by
155 /// TargetInstrDescriptor or the numOperands if it is not zero. (for
156 /// instructions with variable number of operands).
157 MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
158 : TID(&tid), NumImplicitOps(0), Parent(0) {
159 if (!NoImp && TID->ImplicitDefs)
160 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
162 if (!NoImp && TID->ImplicitUses)
163 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
165 Operands.reserve(NumImplicitOps + TID->numOperands);
167 addImplicitDefUseOperands();
168 // Make sure that we get added to a machine basicblock
169 LeakDetector::addGarbageObject(this);
172 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
173 /// MachineInstr is created and added to the end of the specified basic block.
175 MachineInstr::MachineInstr(MachineBasicBlock *MBB,
176 const TargetInstrDescriptor &tid)
177 : TID(&tid), NumImplicitOps(0), Parent(0) {
178 assert(MBB && "Cannot use inserting ctor with null basic block!");
179 if (TID->ImplicitDefs)
180 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
182 if (TID->ImplicitUses)
183 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
185 Operands.reserve(NumImplicitOps + TID->numOperands);
186 addImplicitDefUseOperands();
187 // Make sure that we get added to a machine basicblock
188 LeakDetector::addGarbageObject(this);
189 MBB->push_back(this); // Add instruction to end of basic block!
192 /// MachineInstr ctor - Copies MachineInstr arg exactly
194 MachineInstr::MachineInstr(const MachineInstr &MI) {
195 TID = MI.getInstrDescriptor();
196 NumImplicitOps = MI.NumImplicitOps;
197 Operands.reserve(MI.getNumOperands());
200 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
201 Operands.push_back(MI.getOperand(i));
202 Operands.back().ParentMI = this;
205 // Set parent, next, and prev to null
212 MachineInstr::~MachineInstr() {
213 LeakDetector::removeGarbageObject(this);
215 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
216 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
220 /// getOpcode - Returns the opcode of this MachineInstr.
222 int MachineInstr::getOpcode() const {
226 /// removeFromParent - This method unlinks 'this' from the containing basic
227 /// block, and returns it, but does not delete it.
228 MachineInstr *MachineInstr::removeFromParent() {
229 assert(getParent() && "Not embedded in a basic block!");
230 getParent()->remove(this);
235 /// OperandComplete - Return true if it's illegal to add a new operand
237 bool MachineInstr::OperandsComplete() const {
238 unsigned short NumOperands = TID->numOperands;
239 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
240 getNumOperands()-NumImplicitOps >= NumOperands)
241 return true; // Broken: we have all the operands of this instruction!
245 /// getNumExplicitOperands - Returns the number of non-implicit operands.
247 unsigned MachineInstr::getNumExplicitOperands() const {
248 unsigned NumOperands = TID->numOperands;
249 if ((TID->Flags & M_VARIABLE_OPS) == 0)
252 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
253 const MachineOperand &MO = getOperand(NumOperands);
254 if (!MO.isRegister() || !MO.isImplicit())
261 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
262 /// the specific register or -1 if it is not found. It further tightening
263 /// the search criteria to a use that kills the register if isKill is true.
264 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
265 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
266 const MachineOperand &MO = getOperand(i);
267 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
268 if (!isKill || MO.isKill())
274 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
275 /// the specific register or NULL if it is not found.
276 MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
277 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
278 MachineOperand &MO = getOperand(i);
279 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
285 /// findFirstPredOperandIdx() - Find the index of the first operand in the
286 /// operand list that is used to represent the predicate. It returns -1 if
288 int MachineInstr::findFirstPredOperandIdx() const {
289 const TargetInstrDescriptor *TID = getInstrDescriptor();
290 if (TID->Flags & M_PREDICABLE) {
291 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
292 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
299 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
300 /// to two addr elimination.
301 bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
302 const TargetInstrDescriptor *TID = getInstrDescriptor();
303 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
304 const MachineOperand &MO1 = getOperand(i);
305 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
306 for (unsigned j = i+1; j < e; ++j) {
307 const MachineOperand &MO2 = getOperand(j);
308 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
309 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
317 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
319 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
320 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
321 const MachineOperand &MO = MI->getOperand(i);
322 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
324 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
325 MachineOperand &MOp = getOperand(j);
326 if (!MOp.isIdenticalTo(MO))
337 /// copyPredicates - Copies predicate operand(s) from MI.
338 void MachineInstr::copyPredicates(const MachineInstr *MI) {
339 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
340 if (TID->Flags & M_PREDICABLE) {
341 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
342 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
343 // Predicated operands must be last operands.
344 addOperand(MI->getOperand(i));
350 void MachineInstr::dump() const {
351 cerr << " " << *this;
354 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
355 // Specialize printing if op#0 is definition
356 unsigned StartOp = 0;
357 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
358 getOperand(0).print(OS, TM);
360 ++StartOp; // Don't print this operand again!
363 OS << getInstrDescriptor()->Name;
365 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
369 getOperand(i).print(OS, TM);