xref: /llvm-project/llvm/lib/CodeGen/MachineInstr.cpp (revision 979bbf48d53e3943f2efbd81a7baa33a5687c3a1)
1 //===-- MachineInstr.cpp --------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Methods common to all machine instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 
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 <iostream>
21 
22 using namespace llvm;
23 
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.
27 //
28 // FIXME: This should be a property of the target so that more than one target
29 // at a time can be active...
30 //
31 namespace llvm {
32   extern const TargetInstrDescriptor *TargetInstrDescriptors;
33 }
34 
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.
39 ///
40 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41   : Opcode(opcode), parent(0) {
42   Operands.reserve(numOperands);
43   // Make sure that we get added to a machine basicblock
44   LeakDetector::addGarbageObject(this);
45 }
46 
47 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
48 /// MachineInstr is created and added to the end of the specified basic block.
49 ///
50 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
51                            unsigned numOperands)
52   : Opcode(opcode), parent(0) {
53   assert(MBB && "Cannot use inserting ctor with null basic block!");
54   Operands.reserve(numOperands);
55   // Make sure that we get added to a machine basicblock
56   LeakDetector::addGarbageObject(this);
57   MBB->push_back(this);  // Add instruction to end of basic block!
58 }
59 
60 /// MachineInstr ctor - Copies MachineInstr arg exactly
61 ///
62 MachineInstr::MachineInstr(const MachineInstr &MI) {
63   Opcode = MI.getOpcode();
64   Operands.reserve(MI.getNumOperands());
65 
66   // Add operands
67   for (unsigned i = 0; i != MI.getNumOperands(); ++i)
68     Operands.push_back(MI.getOperand(i));
69 
70   // Set parent, next, and prev to null
71   parent = 0;
72   prev = 0;
73   next = 0;
74 }
75 
76 
77 MachineInstr::~MachineInstr() {
78   LeakDetector::removeGarbageObject(this);
79 }
80 
81 /// removeFromParent - This method unlinks 'this' from the containing basic
82 /// block, and returns it, but does not delete it.
83 MachineInstr *MachineInstr::removeFromParent() {
84   assert(getParent() && "Not embedded in a basic block!");
85   getParent()->remove(this);
86   return this;
87 }
88 
89 
90 /// OperandComplete - Return true if it's illegal to add a new operand
91 ///
92 bool MachineInstr::OperandsComplete() const {
93   int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
94   if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
95       getNumOperands() >= (unsigned)NumOperands)
96     return true;  // Broken: we have all the operands of this instruction!
97   return false;
98 }
99 
100 /// isIdenticalTo - Return true if this operand is identical to the specified
101 /// operand.
102 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
103   if (getType() != Other.getType()) return false;
104 
105   switch (getType()) {
106   default: assert(0 && "Unrecognized operand type");
107   case MachineOperand::MO_Register:
108     return getReg() == Other.getReg() && isDef() == Other.isDef();
109   case MachineOperand::MO_Immediate:
110     return getImm() == Other.getImm();
111   case MachineOperand::MO_MachineBasicBlock:
112     return getMBB() == Other.getMBB();
113   case MachineOperand::MO_FrameIndex:
114     return getFrameIndex() == Other.getFrameIndex();
115   case MachineOperand::MO_ConstantPoolIndex:
116     return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
117            getOffset() == Other.getOffset();
118   case MachineOperand::MO_JumpTableIndex:
119     return getJumpTableIndex() == Other.getJumpTableIndex();
120   case MachineOperand::MO_GlobalAddress:
121     return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
122   case MachineOperand::MO_ExternalSymbol:
123     return !strcmp(getSymbolName(), Other.getSymbolName()) &&
124            getOffset() == Other.getOffset();
125   }
126 }
127 
128 /// addImplicitDefUseOperands - Add all implicit def and use operands to
129 /// this instruction.
130 void MachineInstr::addImplicitDefUseOperands() {
131   const TargetInstrDescriptor &TID = TargetInstrDescriptors[Opcode];
132   if (TID.ImplicitDefs)
133     for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
134       addRegOperand(*ImpDefs, true, true);
135   if (TID.ImplicitUses)
136     for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
137       addRegOperand(*ImpUses, false, true);
138 }
139 
140 
141 void MachineInstr::dump() const {
142   std::cerr << "  " << *this;
143 }
144 
145 static inline void OutputReg(std::ostream &os, unsigned RegNo,
146                              const MRegisterInfo *MRI = 0) {
147   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
148     if (MRI)
149       os << "%" << MRI->get(RegNo).Name;
150     else
151       os << "%mreg(" << RegNo << ")";
152   } else
153     os << "%reg" << RegNo;
154 }
155 
156 static void print(const MachineOperand &MO, std::ostream &OS,
157                   const TargetMachine *TM) {
158   const MRegisterInfo *MRI = 0;
159 
160   if (TM) MRI = TM->getRegisterInfo();
161 
162   switch (MO.getType()) {
163   case MachineOperand::MO_Register:
164     OutputReg(OS, MO.getReg(), MRI);
165     break;
166   case MachineOperand::MO_Immediate:
167     OS << MO.getImmedValue();
168     break;
169   case MachineOperand::MO_MachineBasicBlock:
170     OS << "mbb<"
171        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
172        << "," << (void*)MO.getMachineBasicBlock() << ">";
173     break;
174   case MachineOperand::MO_FrameIndex:
175     OS << "<fi#" << MO.getFrameIndex() << ">";
176     break;
177   case MachineOperand::MO_ConstantPoolIndex:
178     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
179     break;
180   case MachineOperand::MO_JumpTableIndex:
181     OS << "<jt#" << MO.getJumpTableIndex() << ">";
182     break;
183   case MachineOperand::MO_GlobalAddress:
184     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
185     if (MO.getOffset()) OS << "+" << MO.getOffset();
186     OS << ">";
187     break;
188   case MachineOperand::MO_ExternalSymbol:
189     OS << "<es:" << MO.getSymbolName();
190     if (MO.getOffset()) OS << "+" << MO.getOffset();
191     OS << ">";
192     break;
193   default:
194     assert(0 && "Unrecognized operand type");
195   }
196 }
197 
198 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
199   unsigned StartOp = 0;
200 
201    // Specialize printing if op#0 is definition
202   if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
203     ::print(getOperand(0), OS, TM);
204     OS << " = ";
205     ++StartOp;   // Don't print this operand again!
206   }
207 
208   // Must check if Target machine is not null because machine BB could not
209   // be attached to a Machine function yet
210   if (TM)
211     OS << TM->getInstrInfo()->getName(getOpcode());
212 
213   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
214     const MachineOperand& mop = getOperand(i);
215     if (i != StartOp)
216       OS << ",";
217     OS << " ";
218     ::print(mop, OS, TM);
219 
220     if (mop.isReg()) {
221       if (mop.isImplicit())
222         OS << (mop.isDef() ? "<imp-def>" : "<imp-use>");
223       else if (mop.isDef())
224         OS << "<def>";
225     }
226   }
227 
228   OS << "\n";
229 }
230 
231 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
232   // If the instruction is embedded into a basic block, we can find the target
233   // info for the instruction.
234   if (const MachineBasicBlock *MBB = MI.getParent()) {
235     const MachineFunction *MF = MBB->getParent();
236     if (MF)
237       MI.print(os, &MF->getTarget());
238     else
239       MI.print(os, 0);
240     return os;
241   }
242 
243   // Otherwise, print it out in the "raw" format without symbolic register names
244   // and such.
245   os << TargetInstrDescriptors[MI.getOpcode()].Name;
246 
247   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
248     os << "\t" << MI.getOperand(i);
249     if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
250       os << "<d>";
251   }
252 
253   return os << "\n";
254 }
255 
256 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
257   switch (MO.getType()) {
258   case MachineOperand::MO_Register:
259     OutputReg(OS, MO.getReg());
260     break;
261   case MachineOperand::MO_Immediate:
262     OS << (long)MO.getImmedValue();
263     break;
264   case MachineOperand::MO_MachineBasicBlock:
265     OS << "<mbb:"
266        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
267        << "@" << (void*)MO.getMachineBasicBlock() << ">";
268     break;
269   case MachineOperand::MO_FrameIndex:
270     OS << "<fi#" << MO.getFrameIndex() << ">";
271     break;
272   case MachineOperand::MO_ConstantPoolIndex:
273     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
274     break;
275   case MachineOperand::MO_JumpTableIndex:
276     OS << "<jt#" << MO.getJumpTableIndex() << ">";
277     break;
278   case MachineOperand::MO_GlobalAddress:
279     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
280     break;
281   case MachineOperand::MO_ExternalSymbol:
282     OS << "<es:" << MO.getSymbolName() << ">";
283     break;
284   default:
285     assert(0 && "Unrecognized operand type");
286     break;
287   }
288 
289   return OS;
290 }
291