blob: 5950c7c09a480f862eaa034d8c427db84d4ef16b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 "llvm/Support/Streams.h"
21#include <ostream>
22using namespace llvm;
23
24/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
25/// TID NULL and no operands.
26MachineInstr::MachineInstr()
27 : TID(0), NumImplicitOps(0), parent(0) {
28 // Make sure that we get added to a machine basicblock
29 LeakDetector::addGarbageObject(this);
30}
31
32void MachineInstr::addImplicitDefUseOperands() {
33 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +000034 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +000035 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +000037 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +000038 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039}
40
41/// MachineInstr ctor - This constructor create a MachineInstr and add the
42/// implicit operands. It reserves space for number of operands specified by
43/// TargetInstrDescriptor or the numOperands if it is not zero. (for
44/// instructions with variable number of operands).
Evan Chengbdf72b42007-10-13 02:23:01 +000045MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 : TID(&tid), NumImplicitOps(0), parent(0) {
Evan Chengbdf72b42007-10-13 02:23:01 +000047 if (!NoImp && TID->ImplicitDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
49 NumImplicitOps++;
Evan Chengbdf72b42007-10-13 02:23:01 +000050 if (!NoImp && TID->ImplicitUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
52 NumImplicitOps++;
53 Operands.reserve(NumImplicitOps + TID->numOperands);
Evan Chengbdf72b42007-10-13 02:23:01 +000054 if (!NoImp)
55 addImplicitDefUseOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 // Make sure that we get added to a machine basicblock
57 LeakDetector::addGarbageObject(this);
58}
59
60/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
61/// MachineInstr is created and added to the end of the specified basic block.
62///
63MachineInstr::MachineInstr(MachineBasicBlock *MBB,
64 const TargetInstrDescriptor &tid)
65 : TID(&tid), NumImplicitOps(0), parent(0) {
66 assert(MBB && "Cannot use inserting ctor with null basic block!");
67 if (TID->ImplicitDefs)
68 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
69 NumImplicitOps++;
70 if (TID->ImplicitUses)
71 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
72 NumImplicitOps++;
73 Operands.reserve(NumImplicitOps + TID->numOperands);
74 addImplicitDefUseOperands();
75 // Make sure that we get added to a machine basicblock
76 LeakDetector::addGarbageObject(this);
77 MBB->push_back(this); // Add instruction to end of basic block!
78}
79
80/// MachineInstr ctor - Copies MachineInstr arg exactly
81///
82MachineInstr::MachineInstr(const MachineInstr &MI) {
83 TID = MI.getInstrDescriptor();
84 NumImplicitOps = MI.NumImplicitOps;
85 Operands.reserve(MI.getNumOperands());
86
87 // Add operands
88 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
89 Operands.push_back(MI.getOperand(i));
90
91 // Set parent, next, and prev to null
92 parent = 0;
93 prev = 0;
94 next = 0;
95}
96
97
98MachineInstr::~MachineInstr() {
99 LeakDetector::removeGarbageObject(this);
100}
101
102/// getOpcode - Returns the opcode of this MachineInstr.
103///
Dan Gohman5f222be2007-09-14 20:08:19 +0000104int MachineInstr::getOpcode() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 return TID->Opcode;
106}
107
108/// removeFromParent - This method unlinks 'this' from the containing basic
109/// block, and returns it, but does not delete it.
110MachineInstr *MachineInstr::removeFromParent() {
111 assert(getParent() && "Not embedded in a basic block!");
112 getParent()->remove(this);
113 return this;
114}
115
116
117/// OperandComplete - Return true if it's illegal to add a new operand
118///
119bool MachineInstr::OperandsComplete() const {
120 unsigned short NumOperands = TID->numOperands;
121 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
122 getNumOperands()-NumImplicitOps >= NumOperands)
123 return true; // Broken: we have all the operands of this instruction!
124 return false;
125}
126
127/// getNumExplicitOperands - Returns the number of non-implicit operands.
128///
129unsigned MachineInstr::getNumExplicitOperands() const {
130 unsigned NumOperands = TID->numOperands;
131 if ((TID->Flags & M_VARIABLE_OPS) == 0)
132 return NumOperands;
133
134 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
135 const MachineOperand &MO = getOperand(NumOperands);
136 if (!MO.isRegister() || !MO.isImplicit())
137 NumOperands++;
138 }
139 return NumOperands;
140}
141
142/// isIdenticalTo - Return true if this operand is identical to the specified
143/// operand.
144bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
145 if (getType() != Other.getType()) return false;
146
147 switch (getType()) {
148 default: assert(0 && "Unrecognized operand type");
149 case MachineOperand::MO_Register:
150 return getReg() == Other.getReg() && isDef() == Other.isDef();
151 case MachineOperand::MO_Immediate:
152 return getImm() == Other.getImm();
153 case MachineOperand::MO_MachineBasicBlock:
154 return getMBB() == Other.getMBB();
155 case MachineOperand::MO_FrameIndex:
156 return getFrameIndex() == Other.getFrameIndex();
157 case MachineOperand::MO_ConstantPoolIndex:
158 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
159 getOffset() == Other.getOffset();
160 case MachineOperand::MO_JumpTableIndex:
161 return getJumpTableIndex() == Other.getJumpTableIndex();
162 case MachineOperand::MO_GlobalAddress:
163 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
164 case MachineOperand::MO_ExternalSymbol:
165 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
166 getOffset() == Other.getOffset();
167 }
168}
169
170/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
171/// the specific register or -1 if it is not found. It further tightening
172/// the search criteria to a use that kills the register if isKill is true.
173int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
174 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
175 const MachineOperand &MO = getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000176 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 if (!isKill || MO.isKill())
178 return i;
179 }
180 return -1;
181}
182
183/// findRegisterDefOperand() - Returns the MachineOperand that is a def of
184/// the specific register or NULL if it is not found.
185MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
186 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
187 MachineOperand &MO = getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000188 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 return &MO;
190 }
191 return NULL;
192}
193
194/// findFirstPredOperandIdx() - Find the index of the first operand in the
195/// operand list that is used to represent the predicate. It returns -1 if
196/// none is found.
197int MachineInstr::findFirstPredOperandIdx() const {
198 const TargetInstrDescriptor *TID = getInstrDescriptor();
199 if (TID->Flags & M_PREDICABLE) {
200 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
201 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
202 return i;
203 }
204
205 return -1;
206}
207
Evan Cheng687d1082007-10-12 08:50:34 +0000208/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
209/// to two addr elimination.
210bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
211 const TargetInstrDescriptor *TID = getInstrDescriptor();
212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
213 const MachineOperand &MO1 = getOperand(i);
214 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
215 for (unsigned j = i+1; j < e; ++j) {
216 const MachineOperand &MO2 = getOperand(j);
217 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
218 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
219 return true;
220 }
221 }
222 }
223 return false;
224}
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
227///
228void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
229 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
230 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000231 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 continue;
233 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
234 MachineOperand &MOp = getOperand(j);
235 if (!MOp.isIdenticalTo(MO))
236 continue;
237 if (MO.isKill())
238 MOp.setIsKill();
239 else
240 MOp.setIsDead();
241 break;
242 }
243 }
244}
245
246/// copyPredicates - Copies predicate operand(s) from MI.
247void MachineInstr::copyPredicates(const MachineInstr *MI) {
248 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
249 if (TID->Flags & M_PREDICABLE) {
250 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
251 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 // Predicated operands must be last operands.
Chris Lattner63ab1f22007-12-30 00:41:17 +0000253 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255 }
256 }
257}
258
259void MachineInstr::dump() const {
260 cerr << " " << *this;
261}
262
263static inline void OutputReg(std::ostream &os, unsigned RegNo,
264 const MRegisterInfo *MRI = 0) {
265 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
266 if (MRI)
267 os << "%" << MRI->get(RegNo).Name;
268 else
269 os << "%mreg(" << RegNo << ")";
270 } else
271 os << "%reg" << RegNo;
272}
273
274static void print(const MachineOperand &MO, std::ostream &OS,
275 const TargetMachine *TM) {
276 const MRegisterInfo *MRI = 0;
277
278 if (TM) MRI = TM->getRegisterInfo();
279
280 switch (MO.getType()) {
281 case MachineOperand::MO_Register:
282 OutputReg(OS, MO.getReg(), MRI);
283 break;
284 case MachineOperand::MO_Immediate:
285 OS << MO.getImmedValue();
286 break;
287 case MachineOperand::MO_MachineBasicBlock:
288 OS << "mbb<"
289 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
290 << "," << (void*)MO.getMachineBasicBlock() << ">";
291 break;
292 case MachineOperand::MO_FrameIndex:
293 OS << "<fi#" << MO.getFrameIndex() << ">";
294 break;
295 case MachineOperand::MO_ConstantPoolIndex:
296 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
297 break;
298 case MachineOperand::MO_JumpTableIndex:
299 OS << "<jt#" << MO.getJumpTableIndex() << ">";
300 break;
301 case MachineOperand::MO_GlobalAddress:
302 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
303 if (MO.getOffset()) OS << "+" << MO.getOffset();
304 OS << ">";
305 break;
306 case MachineOperand::MO_ExternalSymbol:
307 OS << "<es:" << MO.getSymbolName();
308 if (MO.getOffset()) OS << "+" << MO.getOffset();
309 OS << ">";
310 break;
311 default:
312 assert(0 && "Unrecognized operand type");
313 }
314}
315
316void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
317 unsigned StartOp = 0;
318
319 // Specialize printing if op#0 is definition
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000320 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 ::print(getOperand(0), OS, TM);
322 if (getOperand(0).isDead())
323 OS << "<dead>";
324 OS << " = ";
325 ++StartOp; // Don't print this operand again!
326 }
327
328 if (TID)
329 OS << TID->Name;
330
331 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
332 const MachineOperand& mop = getOperand(i);
333 if (i != StartOp)
334 OS << ",";
335 OS << " ";
336 ::print(mop, OS, TM);
337
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000338 if (mop.isRegister()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
340 OS << "<";
341 bool NeedComma = false;
342 if (mop.isImplicit()) {
343 OS << (mop.isDef() ? "imp-def" : "imp-use");
344 NeedComma = true;
345 } else if (mop.isDef()) {
346 OS << "def";
347 NeedComma = true;
348 }
349 if (mop.isKill() || mop.isDead()) {
350 if (NeedComma)
351 OS << ",";
352 if (mop.isKill())
353 OS << "kill";
354 if (mop.isDead())
355 OS << "dead";
356 }
357 OS << ">";
358 }
359 }
360 }
361
362 OS << "\n";
363}
364
365void MachineInstr::print(std::ostream &os) const {
366 // If the instruction is embedded into a basic block, we can find the target
367 // info for the instruction.
368 if (const MachineBasicBlock *MBB = getParent()) {
369 const MachineFunction *MF = MBB->getParent();
370 if (MF)
371 print(os, &MF->getTarget());
372 else
373 print(os, 0);
374 }
375
376 // Otherwise, print it out in the "raw" format without symbolic register names
377 // and such.
378 os << getInstrDescriptor()->Name;
379
380 for (unsigned i = 0, N = getNumOperands(); i < N; i++) {
381 os << "\t" << getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000382 if (getOperand(i).isRegister() && getOperand(i).isDef())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 os << "<d>";
384 }
385
386 os << "\n";
387}
388
389void MachineOperand::print(std::ostream &OS) const {
390 switch (getType()) {
391 case MO_Register:
392 OutputReg(OS, getReg());
393 break;
394 case MO_Immediate:
395 OS << (long)getImmedValue();
396 break;
397 case MO_MachineBasicBlock:
398 OS << "<mbb:"
399 << ((Value*)getMachineBasicBlock()->getBasicBlock())->getName()
400 << "@" << (void*)getMachineBasicBlock() << ">";
401 break;
402 case MO_FrameIndex:
403 OS << "<fi#" << getFrameIndex() << ">";
404 break;
405 case MO_ConstantPoolIndex:
406 OS << "<cp#" << getConstantPoolIndex() << ">";
407 break;
408 case MO_JumpTableIndex:
409 OS << "<jt#" << getJumpTableIndex() << ">";
410 break;
411 case MO_GlobalAddress:
412 OS << "<ga:" << ((Value*)getGlobal())->getName() << ">";
413 break;
414 case MO_ExternalSymbol:
415 OS << "<es:" << getSymbolName() << ">";
416 break;
417 default:
418 assert(0 && "Unrecognized operand type");
419 break;
420 }
421}
422