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;
42 Op.auxInfo.subReg = 0;
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;
54 Op.auxInfo.subReg = 0;
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, bool NoImp)
64 : TID(&tid), NumImplicitOps(0), parent(0) {
65 if (!NoImp && TID->ImplicitDefs)
66 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
68 if (!NoImp && TID->ImplicitUses)
69 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
71 Operands.reserve(NumImplicitOps + TID->numOperands);
73 addImplicitDefUseOperands();
74 // Make sure that we get added to a machine basicblock
75 LeakDetector::addGarbageObject(this);
78 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
79 /// MachineInstr is created and added to the end of the specified basic block.
81 MachineInstr::MachineInstr(MachineBasicBlock *MBB,
82 const TargetInstrDescriptor &tid)
83 : TID(&tid), NumImplicitOps(0), parent(0) {
84 assert(MBB && "Cannot use inserting ctor with null basic block!");
85 if (TID->ImplicitDefs)
86 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
88 if (TID->ImplicitUses)
89 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
91 Operands.reserve(NumImplicitOps + TID->numOperands);
92 addImplicitDefUseOperands();
93 // Make sure that we get added to a machine basicblock
94 LeakDetector::addGarbageObject(this);
95 MBB->push_back(this); // Add instruction to end of basic block!
98 /// MachineInstr ctor - Copies MachineInstr arg exactly
100 MachineInstr::MachineInstr(const MachineInstr &MI) {
101 TID = MI.getInstrDescriptor();
102 NumImplicitOps = MI.NumImplicitOps;
103 Operands.reserve(MI.getNumOperands());
106 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
107 Operands.push_back(MI.getOperand(i));
109 // Set parent, next, and prev to null
116 MachineInstr::~MachineInstr() {
117 LeakDetector::removeGarbageObject(this);
120 /// getOpcode - Returns the opcode of this MachineInstr.
122 int MachineInstr::getOpcode() const {
126 /// removeFromParent - This method unlinks 'this' from the containing basic
127 /// block, and returns it, but does not delete it.
128 MachineInstr *MachineInstr::removeFromParent() {
129 assert(getParent() && "Not embedded in a basic block!");
130 getParent()->remove(this);
135 /// OperandComplete - Return true if it's illegal to add a new operand
137 bool MachineInstr::OperandsComplete() const {
138 unsigned short NumOperands = TID->numOperands;
139 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
140 getNumOperands()-NumImplicitOps >= NumOperands)
141 return true; // Broken: we have all the operands of this instruction!
145 /// getNumExplicitOperands - Returns the number of non-implicit operands.
147 unsigned MachineInstr::getNumExplicitOperands() const {
148 unsigned NumOperands = TID->numOperands;
149 if ((TID->Flags & M_VARIABLE_OPS) == 0)
152 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
153 const MachineOperand &MO = getOperand(NumOperands);
154 if (!MO.isRegister() || !MO.isImplicit())
160 /// isIdenticalTo - Return true if this operand is identical to the specified
162 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
163 if (getType() != Other.getType()) return false;
166 default: assert(0 && "Unrecognized operand type");
167 case MachineOperand::MO_Register:
168 return getReg() == Other.getReg() && isDef() == Other.isDef();
169 case MachineOperand::MO_Immediate:
170 return getImm() == Other.getImm();
171 case MachineOperand::MO_MachineBasicBlock:
172 return getMBB() == Other.getMBB();
173 case MachineOperand::MO_FrameIndex:
174 return getFrameIndex() == Other.getFrameIndex();
175 case MachineOperand::MO_ConstantPoolIndex:
176 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
177 getOffset() == Other.getOffset();
178 case MachineOperand::MO_JumpTableIndex:
179 return getJumpTableIndex() == Other.getJumpTableIndex();
180 case MachineOperand::MO_GlobalAddress:
181 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
182 case MachineOperand::MO_ExternalSymbol:
183 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
184 getOffset() == Other.getOffset();
188 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
189 /// the specific register or -1 if it is not found. It further tightening
190 /// the search criteria to a use that kills the register if isKill is true.
191 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
192 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
193 const MachineOperand &MO = getOperand(i);
194 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
195 if (!isKill || MO.isKill())
201 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
202 /// the specific register or NULL if it is not found.
203 MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
204 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
205 MachineOperand &MO = getOperand(i);
206 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
212 /// findFirstPredOperandIdx() - Find the index of the first operand in the
213 /// operand list that is used to represent the predicate. It returns -1 if
215 int MachineInstr::findFirstPredOperandIdx() const {
216 const TargetInstrDescriptor *TID = getInstrDescriptor();
217 if (TID->Flags & M_PREDICABLE) {
218 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
219 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
226 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
227 /// to two addr elimination.
228 bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
229 const TargetInstrDescriptor *TID = getInstrDescriptor();
230 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
231 const MachineOperand &MO1 = getOperand(i);
232 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
233 for (unsigned j = i+1; j < e; ++j) {
234 const MachineOperand &MO2 = getOperand(j);
235 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
236 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
244 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
246 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
247 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
248 const MachineOperand &MO = MI->getOperand(i);
249 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
251 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
252 MachineOperand &MOp = getOperand(j);
253 if (!MOp.isIdenticalTo(MO))
264 /// copyPredicates - Copies predicate operand(s) from MI.
265 void MachineInstr::copyPredicates(const MachineInstr *MI) {
266 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
267 if (TID->Flags & M_PREDICABLE) {
268 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
269 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
270 const MachineOperand &MO = MI->getOperand(i);
271 // Predicated operands must be last operands.
273 addRegOperand(MO.getReg(), false);
275 addImmOperand(MO.getImm());
282 void MachineInstr::dump() const {
283 cerr << " " << *this;
286 static inline void OutputReg(std::ostream &os, unsigned RegNo,
287 const MRegisterInfo *MRI = 0) {
288 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
290 os << "%" << MRI->get(RegNo).Name;
292 os << "%mreg(" << RegNo << ")";
294 os << "%reg" << RegNo;
297 static void print(const MachineOperand &MO, std::ostream &OS,
298 const TargetMachine *TM) {
299 const MRegisterInfo *MRI = 0;
301 if (TM) MRI = TM->getRegisterInfo();
303 switch (MO.getType()) {
304 case MachineOperand::MO_Register:
305 OutputReg(OS, MO.getReg(), MRI);
307 case MachineOperand::MO_Immediate:
308 OS << MO.getImmedValue();
310 case MachineOperand::MO_MachineBasicBlock:
312 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
313 << "," << (void*)MO.getMachineBasicBlock() << ">";
315 case MachineOperand::MO_FrameIndex:
316 OS << "<fi#" << MO.getFrameIndex() << ">";
318 case MachineOperand::MO_ConstantPoolIndex:
319 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
321 case MachineOperand::MO_JumpTableIndex:
322 OS << "<jt#" << MO.getJumpTableIndex() << ">";
324 case MachineOperand::MO_GlobalAddress:
325 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
326 if (MO.getOffset()) OS << "+" << MO.getOffset();
329 case MachineOperand::MO_ExternalSymbol:
330 OS << "<es:" << MO.getSymbolName();
331 if (MO.getOffset()) OS << "+" << MO.getOffset();
335 assert(0 && "Unrecognized operand type");
339 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
340 unsigned StartOp = 0;
342 // Specialize printing if op#0 is definition
343 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
344 ::print(getOperand(0), OS, TM);
345 if (getOperand(0).isDead())
348 ++StartOp; // Don't print this operand again!
354 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
355 const MachineOperand& mop = getOperand(i);
359 ::print(mop, OS, TM);
361 if (mop.isRegister()) {
362 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
364 bool NeedComma = false;
365 if (mop.isImplicit()) {
366 OS << (mop.isDef() ? "imp-def" : "imp-use");
368 } else if (mop.isDef()) {
372 if (mop.isKill() || mop.isDead()) {
388 void MachineInstr::print(std::ostream &os) const {
389 // If the instruction is embedded into a basic block, we can find the target
390 // info for the instruction.
391 if (const MachineBasicBlock *MBB = getParent()) {
392 const MachineFunction *MF = MBB->getParent();
394 print(os, &MF->getTarget());
399 // Otherwise, print it out in the "raw" format without symbolic register names
401 os << getInstrDescriptor()->Name;
403 for (unsigned i = 0, N = getNumOperands(); i < N; i++) {
404 os << "\t" << getOperand(i);
405 if (getOperand(i).isRegister() && getOperand(i).isDef())
412 void MachineOperand::print(std::ostream &OS) const {
415 OutputReg(OS, getReg());
418 OS << (long)getImmedValue();
420 case MO_MachineBasicBlock:
422 << ((Value*)getMachineBasicBlock()->getBasicBlock())->getName()
423 << "@" << (void*)getMachineBasicBlock() << ">";
426 OS << "<fi#" << getFrameIndex() << ">";
428 case MO_ConstantPoolIndex:
429 OS << "<cp#" << getConstantPoolIndex() << ">";
431 case MO_JumpTableIndex:
432 OS << "<jt#" << getJumpTableIndex() << ">";
434 case MO_GlobalAddress:
435 OS << "<ga:" << ((Value*)getGlobal())->getName() << ">";
437 case MO_ExternalSymbol:
438 OS << "<es:" << getSymbolName() << ">";
441 assert(0 && "Unrecognized operand type");