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 /// setOpcode - Replace the opcode of the current instruction with a new one.
185 void MachineInstr::setOpcode(unsigned Op) {
186 Operands.erase(Operands.begin(), Operands.begin()+NumImplicitOps);
191 const TargetInstrDescriptor &TID = getParent()->getParent()->
192 getTarget().getInstrInfo()->get(Op);
193 if (TID.ImplicitDefs)
194 for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs) {
196 Op.opType = MachineOperand::MO_Register;
201 Op.contents.RegNo = *ImpDefs;
203 Operands.insert(Operands.begin()+NumImplicitOps, Op);
206 if (TID.ImplicitUses)
207 for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses) {
209 Op.opType = MachineOperand::MO_Register;
214 Op.contents.RegNo = *ImpUses;
216 Operands.insert(Operands.begin()+NumImplicitOps, Op);
222 void MachineInstr::dump() const {
223 std::cerr << " " << *this;
226 static inline void OutputReg(std::ostream &os, unsigned RegNo,
227 const MRegisterInfo *MRI = 0) {
228 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
230 os << "%" << MRI->get(RegNo).Name;
232 os << "%mreg(" << RegNo << ")";
234 os << "%reg" << RegNo;
237 static void print(const MachineOperand &MO, std::ostream &OS,
238 const TargetMachine *TM) {
239 const MRegisterInfo *MRI = 0;
241 if (TM) MRI = TM->getRegisterInfo();
243 switch (MO.getType()) {
244 case MachineOperand::MO_Register:
245 OutputReg(OS, MO.getReg(), MRI);
247 case MachineOperand::MO_Immediate:
248 OS << MO.getImmedValue();
250 case MachineOperand::MO_MachineBasicBlock:
252 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
253 << "," << (void*)MO.getMachineBasicBlock() << ">";
255 case MachineOperand::MO_FrameIndex:
256 OS << "<fi#" << MO.getFrameIndex() << ">";
258 case MachineOperand::MO_ConstantPoolIndex:
259 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
261 case MachineOperand::MO_JumpTableIndex:
262 OS << "<jt#" << MO.getJumpTableIndex() << ">";
264 case MachineOperand::MO_GlobalAddress:
265 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
266 if (MO.getOffset()) OS << "+" << MO.getOffset();
269 case MachineOperand::MO_ExternalSymbol:
270 OS << "<es:" << MO.getSymbolName();
271 if (MO.getOffset()) OS << "+" << MO.getOffset();
275 assert(0 && "Unrecognized operand type");
279 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
280 unsigned StartOp = 0;
282 // Specialize printing if op#0 is definition
283 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
284 ::print(getOperand(0), OS, TM);
286 ++StartOp; // Don't print this operand again!
289 // Must check if Target machine is not null because machine BB could not
290 // be attached to a Machine function yet
292 OS << TM->getInstrInfo()->getName(getOpcode());
294 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
295 const MachineOperand& mop = getOperand(i);
299 ::print(mop, OS, TM);
302 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
304 bool NeedComma = false;
305 if (mop.isImplicit()) {
306 OS << (mop.isDef() ? "imp-def" : "imp-use");
308 } else if (mop.isDef()) {
312 if (mop.isKill() || mop.isDead()) {
328 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
329 // If the instruction is embedded into a basic block, we can find the target
330 // info for the instruction.
331 if (const MachineBasicBlock *MBB = MI.getParent()) {
332 const MachineFunction *MF = MBB->getParent();
334 MI.print(os, &MF->getTarget());
340 // Otherwise, print it out in the "raw" format without symbolic register names
342 os << TargetInstrDescriptors[MI.getOpcode()].Name;
344 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
345 os << "\t" << MI.getOperand(i);
346 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
353 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
354 switch (MO.getType()) {
355 case MachineOperand::MO_Register:
356 OutputReg(OS, MO.getReg());
358 case MachineOperand::MO_Immediate:
359 OS << (long)MO.getImmedValue();
361 case MachineOperand::MO_MachineBasicBlock:
363 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
364 << "@" << (void*)MO.getMachineBasicBlock() << ">";
366 case MachineOperand::MO_FrameIndex:
367 OS << "<fi#" << MO.getFrameIndex() << ">";
369 case MachineOperand::MO_ConstantPoolIndex:
370 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
372 case MachineOperand::MO_JumpTableIndex:
373 OS << "<jt#" << MO.getJumpTableIndex() << ">";
375 case MachineOperand::MO_GlobalAddress:
376 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
378 case MachineOperand::MO_ExternalSymbol:
379 OS << "<es:" << MO.getSymbolName() << ">";
382 assert(0 && "Unrecognized operand type");