blob: 0de5501b78d30ed65c1824d8db014c7b38f3ebba [file] [log] [blame]
Chris Lattner85093632008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
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"
Chris Lattner1b989192007-12-31 04:13:23 +000015#include "llvm/Value.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/MRegisterInfo.h"
21#include "llvm/Support/LeakDetector.h"
22#include "llvm/Support/Streams.h"
23#include <ostream>
24using namespace llvm;
25
Chris Lattner7f2d3b82007-12-30 21:56:09 +000026//===----------------------------------------------------------------------===//
27// MachineOperand Implementation
28//===----------------------------------------------------------------------===//
29
Chris Lattnere45742f2008-01-01 01:12:31 +000030/// AddRegOperandToRegInfo - Add this register operand to the specified
31/// MachineRegisterInfo. If it is null, then the next/prev fields should be
32/// explicitly nulled out.
33void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
34 assert(isReg() && "Can only add reg operand to use lists");
35
36 // If the reginfo pointer is null, just explicitly null out or next/prev
37 // pointers, to ensure they are not garbage.
38 if (RegInfo == 0) {
39 Contents.Reg.Prev = 0;
40 Contents.Reg.Next = 0;
41 return;
42 }
43
44 // Otherwise, add this operand to the head of the registers use/def list.
45 MachineOperand *&Head = RegInfo->getRegUseDefListHead(getReg());
46
47 Contents.Reg.Next = Head;
48 if (Contents.Reg.Next) {
49 assert(getReg() == Contents.Reg.Next->getReg() &&
50 "Different regs on the same list!");
51 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
52 }
53
54 Contents.Reg.Prev = &Head;
55 Head = this;
56}
57
58void MachineOperand::setReg(unsigned Reg) {
59 if (getReg() == Reg) return; // No change.
60
61 // Otherwise, we have to change the register. If this operand is embedded
62 // into a machine function, we need to update the old and new register's
63 // use/def lists.
64 if (MachineInstr *MI = getParent())
65 if (MachineBasicBlock *MBB = MI->getParent())
66 if (MachineFunction *MF = MBB->getParent()) {
67 RemoveRegOperandFromRegInfo();
68 Contents.Reg.RegNo = Reg;
69 AddRegOperandToRegInfo(&MF->getRegInfo());
70 return;
71 }
72
73 // Otherwise, just change the register, no problem. :)
74 Contents.Reg.RegNo = Reg;
75}
76
77/// ChangeToImmediate - Replace this operand with a new immediate operand of
78/// the specified value. If an operand is known to be an immediate already,
79/// the setImm method should be used.
80void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
81 // If this operand is currently a register operand, and if this is in a
82 // function, deregister the operand from the register's use/def list.
83 if (isReg() && getParent() && getParent()->getParent() &&
84 getParent()->getParent()->getParent())
85 RemoveRegOperandFromRegInfo();
86
87 OpKind = MO_Immediate;
88 Contents.ImmVal = ImmVal;
89}
90
91/// ChangeToRegister - Replace this operand with a new register operand of
92/// the specified value. If an operand is known to be an register already,
93/// the setReg method should be used.
94void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
95 bool isKill, bool isDead) {
96 // If this operand is already a register operand, use setReg to update the
97 // register's use/def lists.
98 if (isReg()) {
99 setReg(Reg);
100 } else {
101 // Otherwise, change this to a register and set the reg#.
102 OpKind = MO_Register;
103 Contents.Reg.RegNo = Reg;
104
105 // If this operand is embedded in a function, add the operand to the
106 // register's use/def list.
107 if (MachineInstr *MI = getParent())
108 if (MachineBasicBlock *MBB = MI->getParent())
109 if (MachineFunction *MF = MBB->getParent())
110 AddRegOperandToRegInfo(&MF->getRegInfo());
111 }
112
113 IsDef = isDef;
114 IsImp = isImp;
115 IsKill = isKill;
116 IsDead = isDead;
117 SubReg = 0;
118}
119
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000120/// isIdenticalTo - Return true if this operand is identical to the specified
121/// operand.
122bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
123 if (getType() != Other.getType()) return false;
124
125 switch (getType()) {
126 default: assert(0 && "Unrecognized operand type");
127 case MachineOperand::MO_Register:
128 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
129 getSubReg() == Other.getSubReg();
130 case MachineOperand::MO_Immediate:
131 return getImm() == Other.getImm();
132 case MachineOperand::MO_MachineBasicBlock:
133 return getMBB() == Other.getMBB();
134 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000135 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000136 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000137 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000138 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000139 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000140 case MachineOperand::MO_GlobalAddress:
141 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
142 case MachineOperand::MO_ExternalSymbol:
143 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
144 getOffset() == Other.getOffset();
145 }
146}
147
148/// print - Print the specified machine operand.
149///
150void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
151 switch (getType()) {
152 case MachineOperand::MO_Register:
153 if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) {
154 OS << "%reg" << getReg();
155 } else {
156 // If the instruction is embedded into a basic block, we can find the
Chris Lattnere45742f2008-01-01 01:12:31 +0000157 // target info for the instruction.
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000158 if (TM == 0)
159 if (const MachineInstr *MI = getParent())
160 if (const MachineBasicBlock *MBB = MI->getParent())
161 if (const MachineFunction *MF = MBB->getParent())
162 TM = &MF->getTarget();
163
164 if (TM)
165 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
166 else
167 OS << "%mreg" << getReg();
168 }
169
170 if (isDef() || isKill() || isDead() || isImplicit()) {
171 OS << "<";
172 bool NeedComma = false;
173 if (isImplicit()) {
174 OS << (isDef() ? "imp-def" : "imp-use");
175 NeedComma = true;
176 } else if (isDef()) {
177 OS << "def";
178 NeedComma = true;
179 }
180 if (isKill() || isDead()) {
181 if (NeedComma) OS << ",";
182 if (isKill()) OS << "kill";
183 if (isDead()) OS << "dead";
184 }
185 OS << ">";
186 }
187 break;
188 case MachineOperand::MO_Immediate:
189 OS << getImm();
190 break;
191 case MachineOperand::MO_MachineBasicBlock:
192 OS << "mbb<"
Chris Lattner6017d482007-12-30 23:10:15 +0000193 << ((Value*)getMBB()->getBasicBlock())->getName()
194 << "," << (void*)getMBB() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000195 break;
196 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000197 OS << "<fi#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000198 break;
199 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000200 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000201 if (getOffset()) OS << "+" << getOffset();
202 OS << ">";
203 break;
204 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000205 OS << "<jt#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000206 break;
207 case MachineOperand::MO_GlobalAddress:
208 OS << "<ga:" << ((Value*)getGlobal())->getName();
209 if (getOffset()) OS << "+" << getOffset();
210 OS << ">";
211 break;
212 case MachineOperand::MO_ExternalSymbol:
213 OS << "<es:" << getSymbolName();
214 if (getOffset()) OS << "+" << getOffset();
215 OS << ">";
216 break;
217 default:
218 assert(0 && "Unrecognized operand type");
219 }
220}
221
222//===----------------------------------------------------------------------===//
223// MachineInstr Implementation
224//===----------------------------------------------------------------------===//
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
227/// TID NULL and no operands.
228MachineInstr::MachineInstr()
Chris Lattner7ce487f2007-12-31 04:56:33 +0000229 : TID(0), NumImplicitOps(0), Parent(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Make sure that we get added to a machine basicblock
231 LeakDetector::addGarbageObject(this);
232}
233
234void MachineInstr::addImplicitDefUseOperands() {
235 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000236 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000237 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000239 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000240 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241}
242
243/// MachineInstr ctor - This constructor create a MachineInstr and add the
244/// implicit operands. It reserves space for number of operands specified by
245/// TargetInstrDescriptor or the numOperands if it is not zero. (for
246/// instructions with variable number of operands).
Evan Chengbdf72b42007-10-13 02:23:01 +0000247MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
Chris Lattner7ce487f2007-12-31 04:56:33 +0000248 : TID(&tid), NumImplicitOps(0), Parent(0) {
Evan Chengbdf72b42007-10-13 02:23:01 +0000249 if (!NoImp && TID->ImplicitDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
251 NumImplicitOps++;
Evan Chengbdf72b42007-10-13 02:23:01 +0000252 if (!NoImp && TID->ImplicitUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
254 NumImplicitOps++;
255 Operands.reserve(NumImplicitOps + TID->numOperands);
Evan Chengbdf72b42007-10-13 02:23:01 +0000256 if (!NoImp)
257 addImplicitDefUseOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 // Make sure that we get added to a machine basicblock
259 LeakDetector::addGarbageObject(this);
260}
261
262/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
263/// MachineInstr is created and added to the end of the specified basic block.
264///
265MachineInstr::MachineInstr(MachineBasicBlock *MBB,
266 const TargetInstrDescriptor &tid)
Chris Lattner7ce487f2007-12-31 04:56:33 +0000267 : TID(&tid), NumImplicitOps(0), Parent(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 assert(MBB && "Cannot use inserting ctor with null basic block!");
269 if (TID->ImplicitDefs)
270 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
271 NumImplicitOps++;
272 if (TID->ImplicitUses)
273 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
274 NumImplicitOps++;
275 Operands.reserve(NumImplicitOps + TID->numOperands);
276 addImplicitDefUseOperands();
277 // Make sure that we get added to a machine basicblock
278 LeakDetector::addGarbageObject(this);
279 MBB->push_back(this); // Add instruction to end of basic block!
280}
281
282/// MachineInstr ctor - Copies MachineInstr arg exactly
283///
284MachineInstr::MachineInstr(const MachineInstr &MI) {
285 TID = MI.getInstrDescriptor();
286 NumImplicitOps = MI.NumImplicitOps;
287 Operands.reserve(MI.getNumOperands());
288
289 // Add operands
Chris Lattnere722c3f2007-12-30 06:11:04 +0000290 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 Operands.push_back(MI.getOperand(i));
Chris Lattnere722c3f2007-12-30 06:11:04 +0000292 Operands.back().ParentMI = this;
293 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
295 // Set parent, next, and prev to null
Chris Lattner7ce487f2007-12-31 04:56:33 +0000296 Parent = 0;
297 Prev = 0;
298 Next = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299}
300
301
302MachineInstr::~MachineInstr() {
303 LeakDetector::removeGarbageObject(this);
Chris Lattnere722c3f2007-12-30 06:11:04 +0000304#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000305 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000306 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000307 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
308 "Reg operand def/use list corrupted");
309 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000310#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311}
312
313/// getOpcode - Returns the opcode of this MachineInstr.
314///
Dan Gohman5f222be2007-09-14 20:08:19 +0000315int MachineInstr::getOpcode() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 return TID->Opcode;
317}
318
Chris Lattnere45742f2008-01-01 01:12:31 +0000319/// getRegInfo - If this instruction is embedded into a MachineFunction,
320/// return the MachineRegisterInfo object for the current function, otherwise
321/// return null.
322MachineRegisterInfo *MachineInstr::getRegInfo() {
323 if (MachineBasicBlock *MBB = getParent())
324 if (MachineFunction *MF = MBB->getParent())
325 return &MF->getRegInfo();
326 return 0;
327}
328
329/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
330/// this instruction from their respective use lists. This requires that the
331/// operands already be on their use lists.
332void MachineInstr::RemoveRegOperandsFromUseLists() {
333 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
334 if (Operands[i].isReg())
335 Operands[i].RemoveRegOperandFromRegInfo();
336 }
337}
338
339/// AddRegOperandsToUseLists - Add all of the register operands in
340/// this instruction from their respective use lists. This requires that the
341/// operands not be on their use lists yet.
342void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
343 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
344 if (Operands[i].isReg())
345 Operands[i].AddRegOperandToRegInfo(&RegInfo);
346 }
347}
348
349
350/// addOperand - Add the specified operand to the instruction. If it is an
351/// implicit operand, it is added to the end of the operand list. If it is
352/// an explicit operand it is added at the end of the explicit operand list
353/// (before the first implicit operand).
354void MachineInstr::addOperand(const MachineOperand &Op) {
355 bool isImpReg = Op.isReg() && Op.isImplicit();
356 assert((isImpReg || !OperandsComplete()) &&
357 "Trying to add an operand to a machine instr that is already done!");
358
359 // If we are adding the operand to the end of the list, our job is simpler.
360 // This is true most of the time, so this is a reasonable optimization.
361 if (isImpReg || NumImplicitOps == 0) {
362 // We can only do this optimization if we know that the operand list won't
363 // reallocate.
364 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
365 Operands.push_back(Op);
366
367 // Set the parent of the operand.
368 Operands.back().ParentMI = this;
369
370 // If the operand is a register, update the operand's use list.
371 if (Op.isReg())
372 Operands.back().AddRegOperandToRegInfo(getRegInfo());
373 return;
374 }
375 }
376
377 // Otherwise, we have to insert a real operand before any implicit ones.
378 unsigned OpNo = Operands.size()-NumImplicitOps;
379
380 MachineRegisterInfo *RegInfo = getRegInfo();
381
382 // If this instruction isn't embedded into a function, then we don't need to
383 // update any operand lists.
384 if (RegInfo == 0) {
385 // Simple insertion, no reginfo update needed for other register operands.
386 Operands.insert(Operands.begin()+OpNo, Op);
387 Operands[OpNo].ParentMI = this;
388
389 // Do explicitly set the reginfo for this operand though, to ensure the
390 // next/prev fields are properly nulled out.
391 if (Operands[OpNo].isReg())
392 Operands[OpNo].AddRegOperandToRegInfo(0);
393
394 } else if (Operands.size()+1 <= Operands.capacity()) {
395 // Otherwise, we have to remove register operands from their register use
396 // list, add the operand, then add the register operands back to their use
397 // list. This also must handle the case when the operand list reallocates
398 // to somewhere else.
399
400 // If insertion of this operand won't cause reallocation of the operand
401 // list, just remove the implicit operands, add the operand, then re-add all
402 // the rest of the operands.
403 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
404 assert(Operands[i].isReg() && "Should only be an implicit reg!");
405 Operands[i].RemoveRegOperandFromRegInfo();
406 }
407
408 // Add the operand. If it is a register, add it to the reg list.
409 Operands.insert(Operands.begin()+OpNo, Op);
410 Operands[OpNo].ParentMI = this;
411
412 if (Operands[OpNo].isReg())
413 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
414
415 // Re-add all the implicit ops.
416 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
417 assert(Operands[i].isReg() && "Should only be an implicit reg!");
418 Operands[i].AddRegOperandToRegInfo(RegInfo);
419 }
420 } else {
421 // Otherwise, we will be reallocating the operand list. Remove all reg
422 // operands from their list, then readd them after the operand list is
423 // reallocated.
424 RemoveRegOperandsFromUseLists();
425
426 Operands.insert(Operands.begin()+OpNo, Op);
427 Operands[OpNo].ParentMI = this;
428
429 // Re-add all the operands.
430 AddRegOperandsToUseLists(*RegInfo);
431 }
432}
433
434/// RemoveOperand - Erase an operand from an instruction, leaving it with one
435/// fewer operand than it started with.
436///
437void MachineInstr::RemoveOperand(unsigned OpNo) {
438 assert(OpNo < Operands.size() && "Invalid operand number");
439
440 // Special case removing the last one.
441 if (OpNo == Operands.size()-1) {
442 // If needed, remove from the reg def/use list.
443 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
444 Operands.back().RemoveRegOperandFromRegInfo();
445
446 Operands.pop_back();
447 return;
448 }
449
450 // Otherwise, we are removing an interior operand. If we have reginfo to
451 // update, remove all operands that will be shifted down from their reg lists,
452 // move everything down, then re-add them.
453 MachineRegisterInfo *RegInfo = getRegInfo();
454 if (RegInfo) {
455 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
456 if (Operands[i].isReg())
457 Operands[i].RemoveRegOperandFromRegInfo();
458 }
459 }
460
461 Operands.erase(Operands.begin()+OpNo);
462
463 if (RegInfo) {
464 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
465 if (Operands[i].isReg())
466 Operands[i].AddRegOperandToRegInfo(RegInfo);
467 }
468 }
469}
470
471
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472/// removeFromParent - This method unlinks 'this' from the containing basic
473/// block, and returns it, but does not delete it.
474MachineInstr *MachineInstr::removeFromParent() {
475 assert(getParent() && "Not embedded in a basic block!");
476 getParent()->remove(this);
477 return this;
478}
479
480
481/// OperandComplete - Return true if it's illegal to add a new operand
482///
483bool MachineInstr::OperandsComplete() const {
484 unsigned short NumOperands = TID->numOperands;
485 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
486 getNumOperands()-NumImplicitOps >= NumOperands)
487 return true; // Broken: we have all the operands of this instruction!
488 return false;
489}
490
491/// getNumExplicitOperands - Returns the number of non-implicit operands.
492///
493unsigned MachineInstr::getNumExplicitOperands() const {
494 unsigned NumOperands = TID->numOperands;
495 if ((TID->Flags & M_VARIABLE_OPS) == 0)
496 return NumOperands;
497
498 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
499 const MachineOperand &MO = getOperand(NumOperands);
500 if (!MO.isRegister() || !MO.isImplicit())
501 NumOperands++;
502 }
503 return NumOperands;
504}
505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506
507/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
508/// the specific register or -1 if it is not found. It further tightening
509/// the search criteria to a use that kills the register if isKill is true.
510int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
511 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
512 const MachineOperand &MO = getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000513 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 if (!isKill || MO.isKill())
515 return i;
516 }
517 return -1;
518}
519
520/// findRegisterDefOperand() - Returns the MachineOperand that is a def of
521/// the specific register or NULL if it is not found.
522MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
523 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
524 MachineOperand &MO = getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000525 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 return &MO;
527 }
528 return NULL;
529}
530
531/// findFirstPredOperandIdx() - Find the index of the first operand in the
532/// operand list that is used to represent the predicate. It returns -1 if
533/// none is found.
534int MachineInstr::findFirstPredOperandIdx() const {
535 const TargetInstrDescriptor *TID = getInstrDescriptor();
536 if (TID->Flags & M_PREDICABLE) {
537 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
538 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
539 return i;
540 }
541
542 return -1;
543}
544
Evan Cheng687d1082007-10-12 08:50:34 +0000545/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
546/// to two addr elimination.
547bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
548 const TargetInstrDescriptor *TID = getInstrDescriptor();
549 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
550 const MachineOperand &MO1 = getOperand(i);
551 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
552 for (unsigned j = i+1; j < e; ++j) {
553 const MachineOperand &MO2 = getOperand(j);
554 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
555 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
556 return true;
557 }
558 }
559 }
560 return false;
561}
562
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
564///
565void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
566 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
567 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000568 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 continue;
570 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
571 MachineOperand &MOp = getOperand(j);
572 if (!MOp.isIdenticalTo(MO))
573 continue;
574 if (MO.isKill())
575 MOp.setIsKill();
576 else
577 MOp.setIsDead();
578 break;
579 }
580 }
581}
582
583/// copyPredicates - Copies predicate operand(s) from MI.
584void MachineInstr::copyPredicates(const MachineInstr *MI) {
585 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
586 if (TID->Flags & M_PREDICABLE) {
587 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
588 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 // Predicated operands must be last operands.
Chris Lattner63ab1f22007-12-30 00:41:17 +0000590 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 }
592 }
593 }
594}
595
596void MachineInstr::dump() const {
597 cerr << " " << *this;
598}
599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner9607bb82007-12-30 21:31:53 +0000601 // Specialize printing if op#0 is definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 unsigned StartOp = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000603 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000604 getOperand(0).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 OS << " = ";
606 ++StartOp; // Don't print this operand again!
607 }
608
Chris Lattner9607bb82007-12-30 21:31:53 +0000609 OS << getInstrDescriptor()->Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610
611 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 if (i != StartOp)
613 OS << ",";
614 OS << " ";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000615 getOperand(i).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 }
617
618 OS << "\n";
619}
620