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 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;
53 Operands.push_back(Op);
57 /// MachineInstr ctor - This constructor create a MachineInstr and add the
58 /// implicit operands. It reserves space for number of operands specified by
59 /// TargetInstrDescriptor or the numOperands if it is not zero. (for
60 /// instructions with variable number of operands).
61 MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
62 : TID(&tid), NumImplicitOps(0), parent(0) {
63 if (!NoImp && TID->ImplicitDefs)
64 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
66 if (!NoImp && TID->ImplicitUses)
67 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
69 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 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 /// getNumExplicitOperands - Returns the number of non-implicit operands.
145 unsigned MachineInstr::getNumExplicitOperands() const {
146 unsigned NumOperands = TID->numOperands;
147 if ((TID->Flags & M_VARIABLE_OPS) == 0)
150 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
151 const MachineOperand &MO = getOperand(NumOperands);
152 if (!MO.isRegister() || !MO.isImplicit())
158 /// isIdenticalTo - Return true if this operand is identical to the specified
160 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
161 if (getType() != Other.getType()) return false;
164 default: assert(0 && "Unrecognized operand type");
165 case MachineOperand::MO_Register:
166 return getReg() == Other.getReg() && isDef() == Other.isDef();
167 case MachineOperand::MO_Immediate:
168 return getImm() == Other.getImm();
169 case MachineOperand::MO_MachineBasicBlock:
170 return getMBB() == Other.getMBB();
171 case MachineOperand::MO_FrameIndex:
172 return getFrameIndex() == Other.getFrameIndex();
173 case MachineOperand::MO_ConstantPoolIndex:
174 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
175 getOffset() == Other.getOffset();
176 case MachineOperand::MO_JumpTableIndex:
177 return getJumpTableIndex() == Other.getJumpTableIndex();
178 case MachineOperand::MO_GlobalAddress:
179 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
180 case MachineOperand::MO_ExternalSymbol:
181 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
182 getOffset() == Other.getOffset();
186 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
187 /// the specific register or -1 if it is not found. It further tightening
188 /// the search criteria to a use that kills the register if isKill is true.
189 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
190 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
191 const MachineOperand &MO = getOperand(i);
192 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
193 if (!isKill || MO.isKill())
199 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
200 /// the specific register or NULL if it is not found.
201 MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
202 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
203 MachineOperand &MO = getOperand(i);
204 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
210 /// findFirstPredOperandIdx() - Find the index of the first operand in the
211 /// operand list that is used to represent the predicate. It returns -1 if
213 int MachineInstr::findFirstPredOperandIdx() const {
214 const TargetInstrDescriptor *TID = getInstrDescriptor();
215 if (TID->Flags & M_PREDICABLE) {
216 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
217 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
224 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
225 /// to two addr elimination.
226 bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
227 const TargetInstrDescriptor *TID = getInstrDescriptor();
228 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
229 const MachineOperand &MO1 = getOperand(i);
230 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
231 for (unsigned j = i+1; j < e; ++j) {
232 const MachineOperand &MO2 = getOperand(j);
233 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
234 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
242 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
244 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
245 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
246 const MachineOperand &MO = MI->getOperand(i);
247 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
249 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
250 MachineOperand &MOp = getOperand(j);
251 if (!MOp.isIdenticalTo(MO))
262 /// copyPredicates - Copies predicate operand(s) from MI.
263 void MachineInstr::copyPredicates(const MachineInstr *MI) {
264 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
265 if (TID->Flags & M_PREDICABLE) {
266 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
267 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
268 const MachineOperand &MO = MI->getOperand(i);
269 // Predicated operands must be last operands.
271 addRegOperand(MO.getReg(), false);
273 addImmOperand(MO.getImm());
280 void MachineInstr::dump() const {
281 cerr << " " << *this;
284 static inline void OutputReg(std::ostream &os, unsigned RegNo,
285 const MRegisterInfo *MRI = 0) {
286 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
288 os << "%" << MRI->get(RegNo).Name;
290 os << "%mreg(" << RegNo << ")";
292 os << "%reg" << RegNo;
295 static void print(const MachineOperand &MO, std::ostream &OS,
296 const TargetMachine *TM) {
297 const MRegisterInfo *MRI = 0;
299 if (TM) MRI = TM->getRegisterInfo();
301 switch (MO.getType()) {
302 case MachineOperand::MO_Register:
303 OutputReg(OS, MO.getReg(), MRI);
305 case MachineOperand::MO_Immediate:
306 OS << MO.getImmedValue();
308 case MachineOperand::MO_MachineBasicBlock:
310 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
311 << "," << (void*)MO.getMachineBasicBlock() << ">";
313 case MachineOperand::MO_FrameIndex:
314 OS << "<fi#" << MO.getFrameIndex() << ">";
316 case MachineOperand::MO_ConstantPoolIndex:
317 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
319 case MachineOperand::MO_JumpTableIndex:
320 OS << "<jt#" << MO.getJumpTableIndex() << ">";
322 case MachineOperand::MO_GlobalAddress:
323 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
324 if (MO.getOffset()) OS << "+" << MO.getOffset();
327 case MachineOperand::MO_ExternalSymbol:
328 OS << "<es:" << MO.getSymbolName();
329 if (MO.getOffset()) OS << "+" << MO.getOffset();
333 assert(0 && "Unrecognized operand type");
337 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
338 unsigned StartOp = 0;
340 // Specialize printing if op#0 is definition
341 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
342 ::print(getOperand(0), OS, TM);
343 if (getOperand(0).isDead())
346 ++StartOp; // Don't print this operand again!
352 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
353 const MachineOperand& mop = getOperand(i);
357 ::print(mop, OS, TM);
359 if (mop.isRegister()) {
360 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
362 bool NeedComma = false;
363 if (mop.isImplicit()) {
364 OS << (mop.isDef() ? "imp-def" : "imp-use");
366 } else if (mop.isDef()) {
370 if (mop.isKill() || mop.isDead()) {
386 void MachineInstr::print(std::ostream &os) const {
387 // If the instruction is embedded into a basic block, we can find the target
388 // info for the instruction.
389 if (const MachineBasicBlock *MBB = getParent()) {
390 const MachineFunction *MF = MBB->getParent();
392 print(os, &MF->getTarget());
397 // Otherwise, print it out in the "raw" format without symbolic register names
399 os << getInstrDescriptor()->Name;
401 for (unsigned i = 0, N = getNumOperands(); i < N; i++) {
402 os << "\t" << getOperand(i);
403 if (getOperand(i).isRegister() && getOperand(i).isDef())
410 void MachineOperand::print(std::ostream &OS) const {
413 OutputReg(OS, getReg());
416 OS << (long)getImmedValue();
418 case MO_MachineBasicBlock:
420 << ((Value*)getMachineBasicBlock()->getBasicBlock())->getName()
421 << "@" << (void*)getMachineBasicBlock() << ">";
424 OS << "<fi#" << getFrameIndex() << ">";
426 case MO_ConstantPoolIndex:
427 OS << "<cp#" << getConstantPoolIndex() << ">";
429 case MO_JumpTableIndex:
430 OS << "<jt#" << getJumpTableIndex() << ">";
432 case MO_GlobalAddress:
433 OS << "<ga:" << ((Value*)getGlobal())->getName() << ">";
435 case MO_ExternalSymbol:
436 OS << "<es:" << getSymbolName() << ">";
439 assert(0 && "Unrecognized operand type");