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"
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 only does a _reserve_ of the operands,
36 /// not a resize for them. It is expected that if you use this that you call
37 /// add* methods below to fill up the operands, instead of the Set methods.
38 /// Eventually, the "resizing" ctors will be phased out.
40 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41 : Opcode(opcode), NumImplicitOps(0), parent(0) {
42 Operands.reserve(numOperands);
43 // Make sure that we get added to a machine basicblock
44 LeakDetector::addGarbageObject(this);
47 void MachineInstr::addImplicitDefUseOperands(const TargetInstrDescriptor &TID) {
49 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs) {
51 Op.opType = MachineOperand::MO_Register;
56 Op.contents.RegNo = *ImpDefs;
58 Operands.push_back(Op);
61 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses) {
63 Op.opType = MachineOperand::MO_Register;
68 Op.contents.RegNo = *ImpUses;
70 Operands.push_back(Op);
74 /// MachineInstr ctor - This constructor create a MachineInstr and add the
75 /// implicit operands. It reserves space for numOperand operands.
76 MachineInstr::MachineInstr(const TargetInstrInfo &TII, short opcode,
78 : Opcode(opcode), NumImplicitOps(0), parent(0) {
79 const TargetInstrDescriptor &TID = TII.get(opcode);
81 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
84 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
86 Operands.reserve(NumImplicitOps + numOperands);
87 addImplicitDefUseOperands(TID);
88 // Make sure that we get added to a machine basicblock
89 LeakDetector::addGarbageObject(this);
92 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
93 /// MachineInstr is created and added to the end of the specified basic block.
95 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
97 : Opcode(opcode), NumImplicitOps(0), parent(0) {
98 assert(MBB && "Cannot use inserting ctor with null basic block!");
99 const TargetInstrDescriptor &TID = MBB->getParent()->getTarget().
100 getInstrInfo()->get(opcode);
101 if (TID.ImplicitDefs)
102 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
104 if (TID.ImplicitUses)
105 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
107 Operands.reserve(NumImplicitOps + numOperands);
108 addImplicitDefUseOperands(TID);
109 // Make sure that we get added to a machine basicblock
110 LeakDetector::addGarbageObject(this);
111 MBB->push_back(this); // Add instruction to end of basic block!
114 /// MachineInstr ctor - Copies MachineInstr arg exactly
116 MachineInstr::MachineInstr(const MachineInstr &MI) {
117 Opcode = MI.getOpcode();
118 NumImplicitOps = MI.NumImplicitOps;
119 Operands.reserve(MI.getNumOperands());
122 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
123 Operands.push_back(MI.getOperand(i));
125 // Set parent, next, and prev to null
132 MachineInstr::~MachineInstr() {
133 LeakDetector::removeGarbageObject(this);
136 /// removeFromParent - This method unlinks 'this' from the containing basic
137 /// block, and returns it, but does not delete it.
138 MachineInstr *MachineInstr::removeFromParent() {
139 assert(getParent() && "Not embedded in a basic block!");
140 getParent()->remove(this);
145 /// OperandComplete - Return true if it's illegal to add a new operand
147 bool MachineInstr::OperandsComplete() const {
148 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
149 if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
150 getNumOperands()-NumImplicitOps >= (unsigned)NumOperands)
151 return true; // Broken: we have all the operands of this instruction!
155 /// isIdenticalTo - Return true if this operand is identical to the specified
157 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
158 if (getType() != Other.getType()) return false;
161 default: assert(0 && "Unrecognized operand type");
162 case MachineOperand::MO_Register:
163 return getReg() == Other.getReg() && isDef() == Other.isDef();
164 case MachineOperand::MO_Immediate:
165 return getImm() == Other.getImm();
166 case MachineOperand::MO_MachineBasicBlock:
167 return getMBB() == Other.getMBB();
168 case MachineOperand::MO_FrameIndex:
169 return getFrameIndex() == Other.getFrameIndex();
170 case MachineOperand::MO_ConstantPoolIndex:
171 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
172 getOffset() == Other.getOffset();
173 case MachineOperand::MO_JumpTableIndex:
174 return getJumpTableIndex() == Other.getJumpTableIndex();
175 case MachineOperand::MO_GlobalAddress:
176 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
177 case MachineOperand::MO_ExternalSymbol:
178 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
179 getOffset() == Other.getOffset();
183 void MachineInstr::dump() const {
184 std::cerr << " " << *this;
187 static inline void OutputReg(std::ostream &os, unsigned RegNo,
188 const MRegisterInfo *MRI = 0) {
189 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
191 os << "%" << MRI->get(RegNo).Name;
193 os << "%mreg(" << RegNo << ")";
195 os << "%reg" << RegNo;
198 static void print(const MachineOperand &MO, std::ostream &OS,
199 const TargetMachine *TM) {
200 const MRegisterInfo *MRI = 0;
202 if (TM) MRI = TM->getRegisterInfo();
204 switch (MO.getType()) {
205 case MachineOperand::MO_Register:
206 OutputReg(OS, MO.getReg(), MRI);
208 case MachineOperand::MO_Immediate:
209 OS << MO.getImmedValue();
211 case MachineOperand::MO_MachineBasicBlock:
213 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
214 << "," << (void*)MO.getMachineBasicBlock() << ">";
216 case MachineOperand::MO_FrameIndex:
217 OS << "<fi#" << MO.getFrameIndex() << ">";
219 case MachineOperand::MO_ConstantPoolIndex:
220 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
222 case MachineOperand::MO_JumpTableIndex:
223 OS << "<jt#" << MO.getJumpTableIndex() << ">";
225 case MachineOperand::MO_GlobalAddress:
226 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
227 if (MO.getOffset()) OS << "+" << MO.getOffset();
230 case MachineOperand::MO_ExternalSymbol:
231 OS << "<es:" << MO.getSymbolName();
232 if (MO.getOffset()) OS << "+" << MO.getOffset();
236 assert(0 && "Unrecognized operand type");
240 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
241 unsigned StartOp = 0;
243 // Specialize printing if op#0 is definition
244 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
245 ::print(getOperand(0), OS, TM);
247 ++StartOp; // Don't print this operand again!
250 // Must check if Target machine is not null because machine BB could not
251 // be attached to a Machine function yet
253 OS << TM->getInstrInfo()->getName(getOpcode());
255 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
256 const MachineOperand& mop = getOperand(i);
260 ::print(mop, OS, TM);
263 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
265 bool NeedComma = false;
266 if (mop.isImplicit()) {
267 OS << (mop.isDef() ? "imp-def" : "imp-use");
269 } else if (mop.isDef()) {
273 if (mop.isKill() || mop.isDead()) {
289 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
290 // If the instruction is embedded into a basic block, we can find the target
291 // info for the instruction.
292 if (const MachineBasicBlock *MBB = MI.getParent()) {
293 const MachineFunction *MF = MBB->getParent();
295 MI.print(os, &MF->getTarget());
301 // Otherwise, print it out in the "raw" format without symbolic register names
303 os << TargetInstrDescriptors[MI.getOpcode()].Name;
305 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
306 os << "\t" << MI.getOperand(i);
307 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
314 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
315 switch (MO.getType()) {
316 case MachineOperand::MO_Register:
317 OutputReg(OS, MO.getReg());
319 case MachineOperand::MO_Immediate:
320 OS << (long)MO.getImmedValue();
322 case MachineOperand::MO_MachineBasicBlock:
324 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
325 << "@" << (void*)MO.getMachineBasicBlock() << ">";
327 case MachineOperand::MO_FrameIndex:
328 OS << "<fi#" << MO.getFrameIndex() << ">";
330 case MachineOperand::MO_ConstantPoolIndex:
331 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
333 case MachineOperand::MO_JumpTableIndex:
334 OS << "<jt#" << MO.getJumpTableIndex() << ">";
336 case MachineOperand::MO_GlobalAddress:
337 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
339 case MachineOperand::MO_ExternalSymbol:
340 OS << "<es:" << MO.getSymbolName() << ">";
343 assert(0 && "Unrecognized operand type");