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 // Global variable holding an array of descriptors for machine instructions.
25 // The actual object needs to be created separately for each target machine.
26 // This variable is initialized and reset by class TargetInstrInfo.
28 // FIXME: This should be a property of the target so that more than one target
29 // at a time can be active...
32 extern const TargetInstrDescriptor *TargetInstrDescriptors;
35 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
36 /// opcode 0 and no operands.
37 MachineInstr::MachineInstr()
38 : Opcode(0), NumImplicitOps(0), parent(0) {
39 // Make sure that we get added to a machine basicblock
40 LeakDetector::addGarbageObject(this);
43 void MachineInstr::addImplicitDefUseOperands(const TargetInstrDescriptor &TID) {
45 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs) {
47 Op.opType = MachineOperand::MO_Register;
52 Op.contents.RegNo = *ImpDefs;
54 Operands.push_back(Op);
57 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses) {
59 Op.opType = MachineOperand::MO_Register;
64 Op.contents.RegNo = *ImpUses;
66 Operands.push_back(Op);
70 /// MachineInstr ctor - This constructor create a MachineInstr and add the
71 /// implicit operands. It reserves space for number of operands specified by
72 /// TargetInstrDescriptor or the numOperands if it is not zero. (for
73 /// instructions with variable number of operands).
74 MachineInstr::MachineInstr(const TargetInstrDescriptor &TID)
75 : Opcode(TID.Opcode), NumImplicitOps(0), parent(0) {
77 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
80 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
82 Operands.reserve(NumImplicitOps + TID.numOperands);
83 addImplicitDefUseOperands(TID);
84 // Make sure that we get added to a machine basicblock
85 LeakDetector::addGarbageObject(this);
88 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
89 /// MachineInstr is created and added to the end of the specified basic block.
91 MachineInstr::MachineInstr(MachineBasicBlock *MBB,
92 const TargetInstrDescriptor &TID)
93 : Opcode(TID.Opcode), NumImplicitOps(0), parent(0) {
94 assert(MBB && "Cannot use inserting ctor with null basic block!");
96 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
99 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
101 Operands.reserve(NumImplicitOps + TID.numOperands);
102 addImplicitDefUseOperands(TID);
103 // Make sure that we get added to a machine basicblock
104 LeakDetector::addGarbageObject(this);
105 MBB->push_back(this); // Add instruction to end of basic block!
108 /// MachineInstr ctor - Copies MachineInstr arg exactly
110 MachineInstr::MachineInstr(const MachineInstr &MI) {
111 Opcode = MI.getOpcode();
112 NumImplicitOps = MI.NumImplicitOps;
113 Operands.reserve(MI.getNumOperands());
116 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
117 Operands.push_back(MI.getOperand(i));
119 // Set parent, next, and prev to null
126 MachineInstr::~MachineInstr() {
127 LeakDetector::removeGarbageObject(this);
130 /// removeFromParent - This method unlinks 'this' from the containing basic
131 /// block, and returns it, but does not delete it.
132 MachineInstr *MachineInstr::removeFromParent() {
133 assert(getParent() && "Not embedded in a basic block!");
134 getParent()->remove(this);
139 /// OperandComplete - Return true if it's illegal to add a new operand
141 bool MachineInstr::OperandsComplete() const {
142 unsigned short NumOperands = TargetInstrDescriptors[Opcode].numOperands;
143 if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
144 getNumOperands()-NumImplicitOps >= NumOperands)
145 return true; // Broken: we have all the operands of this instruction!
149 /// isIdenticalTo - Return true if this operand is identical to the specified
151 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
152 if (getType() != Other.getType()) return false;
155 default: assert(0 && "Unrecognized operand type");
156 case MachineOperand::MO_Register:
157 return getReg() == Other.getReg() && isDef() == Other.isDef();
158 case MachineOperand::MO_Immediate:
159 return getImm() == Other.getImm();
160 case MachineOperand::MO_MachineBasicBlock:
161 return getMBB() == Other.getMBB();
162 case MachineOperand::MO_FrameIndex:
163 return getFrameIndex() == Other.getFrameIndex();
164 case MachineOperand::MO_ConstantPoolIndex:
165 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
166 getOffset() == Other.getOffset();
167 case MachineOperand::MO_JumpTableIndex:
168 return getJumpTableIndex() == Other.getJumpTableIndex();
169 case MachineOperand::MO_GlobalAddress:
170 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
171 case MachineOperand::MO_ExternalSymbol:
172 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
173 getOffset() == Other.getOffset();
177 void MachineInstr::dump() const {
178 llvm_cerr << " " << *this;
181 static inline void OutputReg(std::ostream &os, unsigned RegNo,
182 const MRegisterInfo *MRI = 0) {
183 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
185 os << "%" << MRI->get(RegNo).Name;
187 os << "%mreg(" << RegNo << ")";
189 os << "%reg" << RegNo;
192 static void print(const MachineOperand &MO, std::ostream &OS,
193 const TargetMachine *TM) {
194 const MRegisterInfo *MRI = 0;
196 if (TM) MRI = TM->getRegisterInfo();
198 switch (MO.getType()) {
199 case MachineOperand::MO_Register:
200 OutputReg(OS, MO.getReg(), MRI);
202 case MachineOperand::MO_Immediate:
203 OS << MO.getImmedValue();
205 case MachineOperand::MO_MachineBasicBlock:
207 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
208 << "," << (void*)MO.getMachineBasicBlock() << ">";
210 case MachineOperand::MO_FrameIndex:
211 OS << "<fi#" << MO.getFrameIndex() << ">";
213 case MachineOperand::MO_ConstantPoolIndex:
214 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
216 case MachineOperand::MO_JumpTableIndex:
217 OS << "<jt#" << MO.getJumpTableIndex() << ">";
219 case MachineOperand::MO_GlobalAddress:
220 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
221 if (MO.getOffset()) OS << "+" << MO.getOffset();
224 case MachineOperand::MO_ExternalSymbol:
225 OS << "<es:" << MO.getSymbolName();
226 if (MO.getOffset()) OS << "+" << MO.getOffset();
230 assert(0 && "Unrecognized operand type");
234 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
235 unsigned StartOp = 0;
237 // Specialize printing if op#0 is definition
238 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
239 ::print(getOperand(0), OS, TM);
241 ++StartOp; // Don't print this operand again!
244 // Must check if Target machine is not null because machine BB could not
245 // be attached to a Machine function yet
247 OS << TM->getInstrInfo()->getName(getOpcode());
249 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
250 const MachineOperand& mop = getOperand(i);
254 ::print(mop, OS, TM);
257 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
259 bool NeedComma = false;
260 if (mop.isImplicit()) {
261 OS << (mop.isDef() ? "imp-def" : "imp-use");
263 } else if (mop.isDef()) {
267 if (mop.isKill() || mop.isDead()) {
283 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
284 // If the instruction is embedded into a basic block, we can find the target
285 // info for the instruction.
286 if (const MachineBasicBlock *MBB = MI.getParent()) {
287 const MachineFunction *MF = MBB->getParent();
289 MI.print(os, &MF->getTarget());
295 // Otherwise, print it out in the "raw" format without symbolic register names
297 os << TargetInstrDescriptors[MI.getOpcode()].Name;
299 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
300 os << "\t" << MI.getOperand(i);
301 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
308 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
309 switch (MO.getType()) {
310 case MachineOperand::MO_Register:
311 OutputReg(OS, MO.getReg());
313 case MachineOperand::MO_Immediate:
314 OS << (long)MO.getImmedValue();
316 case MachineOperand::MO_MachineBasicBlock:
318 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
319 << "@" << (void*)MO.getMachineBasicBlock() << ">";
321 case MachineOperand::MO_FrameIndex:
322 OS << "<fi#" << MO.getFrameIndex() << ">";
324 case MachineOperand::MO_ConstantPoolIndex:
325 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
327 case MachineOperand::MO_JumpTableIndex:
328 OS << "<jt#" << MO.getJumpTableIndex() << ">";
330 case MachineOperand::MO_GlobalAddress:
331 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
333 case MachineOperand::MO_ExternalSymbol:
334 OS << "<es:" << MO.getSymbolName() << ">";
337 assert(0 && "Unrecognized operand type");