1 //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Methods common to all machine instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/Constants.h"
16 #include "llvm/InlineAsm.h"
17 #include "llvm/Value.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineMemOperand.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetInstrDesc.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/Analysis/DebugInfo.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/LeakDetector.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/ADT/FoldingSet.h"
35 //===----------------------------------------------------------------------===//
36 // MachineOperand Implementation
37 //===----------------------------------------------------------------------===//
39 /// AddRegOperandToRegInfo - Add this register operand to the specified
40 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
41 /// explicitly nulled out.
42 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
43 assert(isReg() && "Can only add reg operand to use lists");
45 // If the reginfo pointer is null, just explicitly null out or next/prev
46 // pointers, to ensure they are not garbage.
48 Contents.Reg.Prev = 0;
49 Contents.Reg.Next = 0;
53 // Otherwise, add this operand to the head of the registers use/def list.
54 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
56 // For SSA values, we prefer to keep the definition at the start of the list.
57 // we do this by skipping over the definition if it is at the head of the
59 if (*Head && (*Head)->isDef())
60 Head = &(*Head)->Contents.Reg.Next;
62 Contents.Reg.Next = *Head;
63 if (Contents.Reg.Next) {
64 assert(getReg() == Contents.Reg.Next->getReg() &&
65 "Different regs on the same list!");
66 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
69 Contents.Reg.Prev = Head;
73 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
74 /// MachineRegisterInfo it is linked with.
75 void MachineOperand::RemoveRegOperandFromRegInfo() {
76 assert(isOnRegUseList() && "Reg operand is not on a use list");
77 // Unlink this from the doubly linked list of operands.
78 MachineOperand *NextOp = Contents.Reg.Next;
79 *Contents.Reg.Prev = NextOp;
81 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
82 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
84 Contents.Reg.Prev = 0;
85 Contents.Reg.Next = 0;
88 void MachineOperand::setReg(unsigned Reg) {
89 if (getReg() == Reg) return; // No change.
91 // Otherwise, we have to change the register. If this operand is embedded
92 // into a machine function, we need to update the old and new register's
94 if (MachineInstr *MI = getParent())
95 if (MachineBasicBlock *MBB = MI->getParent())
96 if (MachineFunction *MF = MBB->getParent()) {
97 RemoveRegOperandFromRegInfo();
98 Contents.Reg.RegNo = Reg;
99 AddRegOperandToRegInfo(&MF->getRegInfo());
103 // Otherwise, just change the register, no problem. :)
104 Contents.Reg.RegNo = Reg;
107 /// ChangeToImmediate - Replace this operand with a new immediate operand of
108 /// the specified value. If an operand is known to be an immediate already,
109 /// the setImm method should be used.
110 void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
111 // If this operand is currently a register operand, and if this is in a
112 // function, deregister the operand from the register's use/def list.
113 if (isReg() && getParent() && getParent()->getParent() &&
114 getParent()->getParent()->getParent())
115 RemoveRegOperandFromRegInfo();
117 OpKind = MO_Immediate;
118 Contents.ImmVal = ImmVal;
121 /// ChangeToRegister - Replace this operand with a new register operand of
122 /// the specified value. If an operand is known to be an register already,
123 /// the setReg method should be used.
124 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
125 bool isKill, bool isDead, bool isUndef) {
126 // If this operand is already a register operand, use setReg to update the
127 // register's use/def lists.
129 assert(!isEarlyClobber());
132 // Otherwise, change this to a register and set the reg#.
133 OpKind = MO_Register;
134 Contents.Reg.RegNo = Reg;
136 // If this operand is embedded in a function, add the operand to the
137 // register's use/def list.
138 if (MachineInstr *MI = getParent())
139 if (MachineBasicBlock *MBB = MI->getParent())
140 if (MachineFunction *MF = MBB->getParent())
141 AddRegOperandToRegInfo(&MF->getRegInfo());
149 IsEarlyClobber = false;
153 /// isIdenticalTo - Return true if this operand is identical to the specified
155 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
156 if (getType() != Other.getType() ||
157 getTargetFlags() != Other.getTargetFlags())
161 default: llvm_unreachable("Unrecognized operand type");
162 case MachineOperand::MO_Register:
163 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
164 getSubReg() == Other.getSubReg();
165 case MachineOperand::MO_Immediate:
166 return getImm() == Other.getImm();
167 case MachineOperand::MO_FPImmediate:
168 return getFPImm() == Other.getFPImm();
169 case MachineOperand::MO_MachineBasicBlock:
170 return getMBB() == Other.getMBB();
171 case MachineOperand::MO_FrameIndex:
172 return getIndex() == Other.getIndex();
173 case MachineOperand::MO_ConstantPoolIndex:
174 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
175 case MachineOperand::MO_JumpTableIndex:
176 return getIndex() == Other.getIndex();
177 case MachineOperand::MO_GlobalAddress:
178 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
179 case MachineOperand::MO_ExternalSymbol:
180 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
181 getOffset() == Other.getOffset();
185 /// print - Print the specified machine operand.
187 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
189 case MachineOperand::MO_Register:
190 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
191 OS << "%reg" << getReg();
193 // If the instruction is embedded into a basic block, we can find the
194 // target info for the instruction.
196 if (const MachineInstr *MI = getParent())
197 if (const MachineBasicBlock *MBB = MI->getParent())
198 if (const MachineFunction *MF = MBB->getParent())
199 TM = &MF->getTarget();
202 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
204 OS << "%mreg" << getReg();
207 if (getSubReg() != 0)
208 OS << ':' << getSubReg();
210 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
213 bool NeedComma = false;
215 if (NeedComma) OS << ',';
216 OS << (isDef() ? "imp-def" : "imp-use");
218 } else if (isDef()) {
219 if (NeedComma) OS << ',';
220 if (isEarlyClobber())
221 OS << "earlyclobber,";
225 if (isKill() || isDead() || isUndef()) {
226 if (NeedComma) OS << ',';
227 if (isKill()) OS << "kill";
228 if (isDead()) OS << "dead";
230 if (isKill() || isDead())
238 case MachineOperand::MO_Immediate:
241 case MachineOperand::MO_FPImmediate:
242 if (getFPImm()->getType() == Type::getFloatTy(getFPImm()->getContext()))
243 OS << getFPImm()->getValueAPF().convertToFloat();
245 OS << getFPImm()->getValueAPF().convertToDouble();
247 case MachineOperand::MO_MachineBasicBlock:
249 << ((Value*)getMBB()->getBasicBlock())->getName()
250 << "," << (void*)getMBB() << '>';
252 case MachineOperand::MO_FrameIndex:
253 OS << "<fi#" << getIndex() << '>';
255 case MachineOperand::MO_ConstantPoolIndex:
256 OS << "<cp#" << getIndex();
257 if (getOffset()) OS << "+" << getOffset();
260 case MachineOperand::MO_JumpTableIndex:
261 OS << "<jt#" << getIndex() << '>';
263 case MachineOperand::MO_GlobalAddress:
264 OS << "<ga:" << ((Value*)getGlobal())->getName();
265 if (getOffset()) OS << "+" << getOffset();
268 case MachineOperand::MO_ExternalSymbol:
269 OS << "<es:" << getSymbolName();
270 if (getOffset()) OS << "+" << getOffset();
274 llvm_unreachable("Unrecognized operand type");
277 if (unsigned TF = getTargetFlags())
278 OS << "[TF=" << TF << ']';
281 //===----------------------------------------------------------------------===//
282 // MachineMemOperand Implementation
283 //===----------------------------------------------------------------------===//
285 MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
286 int64_t o, uint64_t s, unsigned int a)
287 : Offset(o), Size(s), V(v),
288 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
289 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
290 assert((isLoad() || isStore()) && "Not a load/store!");
293 /// Profile - Gather unique data for the object.
295 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
296 ID.AddInteger(Offset);
299 ID.AddInteger(Flags);
302 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
303 // The Value and Offset may differ due to CSE. But the flags and size
304 // should be the same.
305 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
306 assert(MMO->getSize() == getSize() && "Size mismatch!");
308 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
309 // Update the alignment value.
310 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
311 // Also update the base and offset, because the new alignment may
312 // not be applicable with the old ones.
314 Offset = MMO->getOffset();
318 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
319 assert((MMO.isLoad() || MMO.isStore()) &&
320 "SV has to be a load, store or both.");
322 if (MMO.isVolatile())
331 // Print the address information.
336 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
338 // If the alignment of the memory reference itself differs from the alignment
339 // of the base pointer, print the base alignment explicitly, next to the base
341 if (MMO.getBaseAlignment() != MMO.getAlignment())
342 OS << "(align=" << MMO.getBaseAlignment() << ")";
344 if (MMO.getOffset() != 0)
345 OS << "+" << MMO.getOffset();
348 // Print the alignment of the reference.
349 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
350 MMO.getBaseAlignment() != MMO.getSize())
351 OS << "(align=" << MMO.getAlignment() << ")";
356 //===----------------------------------------------------------------------===//
357 // MachineInstr Implementation
358 //===----------------------------------------------------------------------===//
360 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
361 /// TID NULL and no operands.
362 MachineInstr::MachineInstr()
363 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
364 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
365 // Make sure that we get added to a machine basicblock
366 LeakDetector::addGarbageObject(this);
369 void MachineInstr::addImplicitDefUseOperands() {
370 if (TID->ImplicitDefs)
371 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
372 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
373 if (TID->ImplicitUses)
374 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
375 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
378 /// MachineInstr ctor - This constructor create a MachineInstr and add the
379 /// implicit operands. It reserves space for number of operands specified by
380 /// TargetInstrDesc or the numOperands if it is not zero. (for
381 /// instructions with variable number of operands).
382 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
383 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
384 debugLoc(DebugLoc::getUnknownLoc()) {
385 if (!NoImp && TID->getImplicitDefs())
386 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
388 if (!NoImp && TID->getImplicitUses())
389 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
391 Operands.reserve(NumImplicitOps + TID->getNumOperands());
393 addImplicitDefUseOperands();
394 // Make sure that we get added to a machine basicblock
395 LeakDetector::addGarbageObject(this);
398 /// MachineInstr ctor - As above, but with a DebugLoc.
399 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
401 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
402 Parent(0), debugLoc(dl) {
403 if (!NoImp && TID->getImplicitDefs())
404 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
406 if (!NoImp && TID->getImplicitUses())
407 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
409 Operands.reserve(NumImplicitOps + TID->getNumOperands());
411 addImplicitDefUseOperands();
412 // Make sure that we get added to a machine basicblock
413 LeakDetector::addGarbageObject(this);
416 /// MachineInstr ctor - Work exactly the same as the ctor two above, except
417 /// that the MachineInstr is created and added to the end of the specified
420 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
421 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
422 debugLoc(DebugLoc::getUnknownLoc()) {
423 assert(MBB && "Cannot use inserting ctor with null basic block!");
424 if (TID->ImplicitDefs)
425 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
427 if (TID->ImplicitUses)
428 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
430 Operands.reserve(NumImplicitOps + TID->getNumOperands());
431 addImplicitDefUseOperands();
432 // Make sure that we get added to a machine basicblock
433 LeakDetector::addGarbageObject(this);
434 MBB->push_back(this); // Add instruction to end of basic block!
437 /// MachineInstr ctor - As above, but with a DebugLoc.
439 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
440 const TargetInstrDesc &tid)
441 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
442 Parent(0), debugLoc(dl) {
443 assert(MBB && "Cannot use inserting ctor with null basic block!");
444 if (TID->ImplicitDefs)
445 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
447 if (TID->ImplicitUses)
448 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
450 Operands.reserve(NumImplicitOps + TID->getNumOperands());
451 addImplicitDefUseOperands();
452 // Make sure that we get added to a machine basicblock
453 LeakDetector::addGarbageObject(this);
454 MBB->push_back(this); // Add instruction to end of basic block!
457 /// MachineInstr ctor - Copies MachineInstr arg exactly
459 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
460 : TID(&MI.getDesc()), NumImplicitOps(0),
461 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
462 Parent(0), debugLoc(MI.getDebugLoc()) {
463 Operands.reserve(MI.getNumOperands());
466 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
467 addOperand(MI.getOperand(i));
468 NumImplicitOps = MI.NumImplicitOps;
470 // Set parent to null.
473 LeakDetector::addGarbageObject(this);
476 MachineInstr::~MachineInstr() {
477 LeakDetector::removeGarbageObject(this);
479 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
480 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
481 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
482 "Reg operand def/use list corrupted");
487 /// getRegInfo - If this instruction is embedded into a MachineFunction,
488 /// return the MachineRegisterInfo object for the current function, otherwise
490 MachineRegisterInfo *MachineInstr::getRegInfo() {
491 if (MachineBasicBlock *MBB = getParent())
492 return &MBB->getParent()->getRegInfo();
496 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
497 /// this instruction from their respective use lists. This requires that the
498 /// operands already be on their use lists.
499 void MachineInstr::RemoveRegOperandsFromUseLists() {
500 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
501 if (Operands[i].isReg())
502 Operands[i].RemoveRegOperandFromRegInfo();
506 /// AddRegOperandsToUseLists - Add all of the register operands in
507 /// this instruction from their respective use lists. This requires that the
508 /// operands not be on their use lists yet.
509 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
510 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
511 if (Operands[i].isReg())
512 Operands[i].AddRegOperandToRegInfo(&RegInfo);
517 /// addOperand - Add the specified operand to the instruction. If it is an
518 /// implicit operand, it is added to the end of the operand list. If it is
519 /// an explicit operand it is added at the end of the explicit operand list
520 /// (before the first implicit operand).
521 void MachineInstr::addOperand(const MachineOperand &Op) {
522 bool isImpReg = Op.isReg() && Op.isImplicit();
523 assert((isImpReg || !OperandsComplete()) &&
524 "Trying to add an operand to a machine instr that is already done!");
526 MachineRegisterInfo *RegInfo = getRegInfo();
528 // If we are adding the operand to the end of the list, our job is simpler.
529 // This is true most of the time, so this is a reasonable optimization.
530 if (isImpReg || NumImplicitOps == 0) {
531 // We can only do this optimization if we know that the operand list won't
533 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
534 Operands.push_back(Op);
536 // Set the parent of the operand.
537 Operands.back().ParentMI = this;
539 // If the operand is a register, update the operand's use list.
541 Operands.back().AddRegOperandToRegInfo(RegInfo);
546 // Otherwise, we have to insert a real operand before any implicit ones.
547 unsigned OpNo = Operands.size()-NumImplicitOps;
549 // If this instruction isn't embedded into a function, then we don't need to
550 // update any operand lists.
552 // Simple insertion, no reginfo update needed for other register operands.
553 Operands.insert(Operands.begin()+OpNo, Op);
554 Operands[OpNo].ParentMI = this;
556 // Do explicitly set the reginfo for this operand though, to ensure the
557 // next/prev fields are properly nulled out.
558 if (Operands[OpNo].isReg())
559 Operands[OpNo].AddRegOperandToRegInfo(0);
561 } else if (Operands.size()+1 <= Operands.capacity()) {
562 // Otherwise, we have to remove register operands from their register use
563 // list, add the operand, then add the register operands back to their use
564 // list. This also must handle the case when the operand list reallocates
565 // to somewhere else.
567 // If insertion of this operand won't cause reallocation of the operand
568 // list, just remove the implicit operands, add the operand, then re-add all
569 // the rest of the operands.
570 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
571 assert(Operands[i].isReg() && "Should only be an implicit reg!");
572 Operands[i].RemoveRegOperandFromRegInfo();
575 // Add the operand. If it is a register, add it to the reg list.
576 Operands.insert(Operands.begin()+OpNo, Op);
577 Operands[OpNo].ParentMI = this;
579 if (Operands[OpNo].isReg())
580 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
582 // Re-add all the implicit ops.
583 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
584 assert(Operands[i].isReg() && "Should only be an implicit reg!");
585 Operands[i].AddRegOperandToRegInfo(RegInfo);
588 // Otherwise, we will be reallocating the operand list. Remove all reg
589 // operands from their list, then readd them after the operand list is
591 RemoveRegOperandsFromUseLists();
593 Operands.insert(Operands.begin()+OpNo, Op);
594 Operands[OpNo].ParentMI = this;
596 // Re-add all the operands.
597 AddRegOperandsToUseLists(*RegInfo);
601 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
602 /// fewer operand than it started with.
604 void MachineInstr::RemoveOperand(unsigned OpNo) {
605 assert(OpNo < Operands.size() && "Invalid operand number");
607 // Special case removing the last one.
608 if (OpNo == Operands.size()-1) {
609 // If needed, remove from the reg def/use list.
610 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
611 Operands.back().RemoveRegOperandFromRegInfo();
617 // Otherwise, we are removing an interior operand. If we have reginfo to
618 // update, remove all operands that will be shifted down from their reg lists,
619 // move everything down, then re-add them.
620 MachineRegisterInfo *RegInfo = getRegInfo();
622 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
623 if (Operands[i].isReg())
624 Operands[i].RemoveRegOperandFromRegInfo();
628 Operands.erase(Operands.begin()+OpNo);
631 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
632 if (Operands[i].isReg())
633 Operands[i].AddRegOperandToRegInfo(RegInfo);
638 /// addMemOperand - Add a MachineMemOperand to the machine instruction.
639 /// This function should be used only occasionally. The setMemRefs function
640 /// is the primary method for setting up a MachineInstr's MemRefs list.
641 void MachineInstr::addMemOperand(MachineFunction &MF,
642 MachineMemOperand *MO) {
643 mmo_iterator OldMemRefs = MemRefs;
644 mmo_iterator OldMemRefsEnd = MemRefsEnd;
646 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
647 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
648 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
650 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
651 NewMemRefs[NewNum - 1] = MO;
653 MemRefs = NewMemRefs;
654 MemRefsEnd = NewMemRefsEnd;
657 /// removeFromParent - This method unlinks 'this' from the containing basic
658 /// block, and returns it, but does not delete it.
659 MachineInstr *MachineInstr::removeFromParent() {
660 assert(getParent() && "Not embedded in a basic block!");
661 getParent()->remove(this);
666 /// eraseFromParent - This method unlinks 'this' from the containing basic
667 /// block, and deletes it.
668 void MachineInstr::eraseFromParent() {
669 assert(getParent() && "Not embedded in a basic block!");
670 getParent()->erase(this);
674 /// OperandComplete - Return true if it's illegal to add a new operand
676 bool MachineInstr::OperandsComplete() const {
677 unsigned short NumOperands = TID->getNumOperands();
678 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
679 return true; // Broken: we have all the operands of this instruction!
683 /// getNumExplicitOperands - Returns the number of non-implicit operands.
685 unsigned MachineInstr::getNumExplicitOperands() const {
686 unsigned NumOperands = TID->getNumOperands();
687 if (!TID->isVariadic())
690 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
691 const MachineOperand &MO = getOperand(i);
692 if (!MO.isReg() || !MO.isImplicit())
699 /// isLabel - Returns true if the MachineInstr represents a label.
701 bool MachineInstr::isLabel() const {
702 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
703 getOpcode() == TargetInstrInfo::EH_LABEL ||
704 getOpcode() == TargetInstrInfo::GC_LABEL;
707 /// isDebugLabel - Returns true if the MachineInstr represents a debug label.
709 bool MachineInstr::isDebugLabel() const {
710 return getOpcode() == TargetInstrInfo::DBG_LABEL;
713 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
714 /// the specific register or -1 if it is not found. It further tightens
715 /// the search criteria to a use that kills the register if isKill is true.
716 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
717 const TargetRegisterInfo *TRI) const {
718 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
719 const MachineOperand &MO = getOperand(i);
720 if (!MO.isReg() || !MO.isUse())
722 unsigned MOReg = MO.getReg();
727 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
728 TargetRegisterInfo::isPhysicalRegister(Reg) &&
729 TRI->isSubRegister(MOReg, Reg)))
730 if (!isKill || MO.isKill())
736 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
737 /// the specified register or -1 if it is not found. If isDead is true, defs
738 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
739 /// also checks if there is a def of a super-register.
740 int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
741 const TargetRegisterInfo *TRI) const {
742 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
743 const MachineOperand &MO = getOperand(i);
744 if (!MO.isReg() || !MO.isDef())
746 unsigned MOReg = MO.getReg();
749 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
750 TargetRegisterInfo::isPhysicalRegister(Reg) &&
751 TRI->isSubRegister(MOReg, Reg)))
752 if (!isDead || MO.isDead())
758 /// findFirstPredOperandIdx() - Find the index of the first operand in the
759 /// operand list that is used to represent the predicate. It returns -1 if
761 int MachineInstr::findFirstPredOperandIdx() const {
762 const TargetInstrDesc &TID = getDesc();
763 if (TID.isPredicable()) {
764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
765 if (TID.OpInfo[i].isPredicate())
772 /// isRegTiedToUseOperand - Given the index of a register def operand,
773 /// check if the register def is tied to a source operand, due to either
774 /// two-address elimination or inline assembly constraints. Returns the
775 /// first tied use operand index by reference is UseOpIdx is not null.
777 isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
778 if (getOpcode() == TargetInstrInfo::INLINEASM) {
779 assert(DefOpIdx >= 2);
780 const MachineOperand &MO = getOperand(DefOpIdx);
781 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
783 // Determine the actual operand index that corresponds to this index.
785 unsigned DefPart = 0;
786 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
787 const MachineOperand &FMO = getOperand(i);
788 // After the normal asm operands there may be additional imp-def regs.
791 // Skip over this def.
792 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
793 unsigned PrevDef = i + 1;
794 i = PrevDef + NumOps;
796 DefPart = DefOpIdx - PrevDef;
801 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
802 const MachineOperand &FMO = getOperand(i);
805 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
808 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
811 *UseOpIdx = (unsigned)i + 1 + DefPart;
818 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
819 const TargetInstrDesc &TID = getDesc();
820 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
821 const MachineOperand &MO = getOperand(i);
822 if (MO.isReg() && MO.isUse() &&
823 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
825 *UseOpIdx = (unsigned)i;
832 /// isRegTiedToDefOperand - Return true if the operand of the specified index
833 /// is a register use and it is tied to an def operand. It also returns the def
834 /// operand index by reference.
836 isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
837 if (getOpcode() == TargetInstrInfo::INLINEASM) {
838 const MachineOperand &MO = getOperand(UseOpIdx);
839 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
842 // Find the flag operand corresponding to UseOpIdx
843 unsigned FlagIdx, NumOps=0;
844 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
845 const MachineOperand &UFMO = getOperand(FlagIdx);
846 // After the normal asm operands there may be additional imp-def regs.
849 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
850 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
851 if (UseOpIdx < FlagIdx+NumOps+1)
854 if (FlagIdx >= UseOpIdx)
856 const MachineOperand &UFMO = getOperand(FlagIdx);
858 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
863 // Remember to adjust the index. First operand is asm string, then there
864 // is a flag for each.
866 const MachineOperand &FMO = getOperand(DefIdx);
868 // Skip over this def.
869 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
872 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
878 const TargetInstrDesc &TID = getDesc();
879 if (UseOpIdx >= TID.getNumOperands())
881 const MachineOperand &MO = getOperand(UseOpIdx);
882 if (!MO.isReg() || !MO.isUse())
884 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
888 *DefOpIdx = (unsigned)DefIdx;
892 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
894 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
895 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
896 const MachineOperand &MO = MI->getOperand(i);
897 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
899 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
900 MachineOperand &MOp = getOperand(j);
901 if (!MOp.isIdenticalTo(MO))
912 /// copyPredicates - Copies predicate operand(s) from MI.
913 void MachineInstr::copyPredicates(const MachineInstr *MI) {
914 const TargetInstrDesc &TID = MI->getDesc();
915 if (!TID.isPredicable())
917 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
918 if (TID.OpInfo[i].isPredicate()) {
919 // Predicated operands must be last operands.
920 addOperand(MI->getOperand(i));
925 /// isSafeToMove - Return true if it is safe to move this instruction. If
926 /// SawStore is set to true, it means that there is a store (or call) between
927 /// the instruction's location and its intended destination.
928 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
929 bool &SawStore) const {
930 // Ignore stuff that we obviously can't move.
931 if (TID->mayStore() || TID->isCall()) {
935 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
938 // See if this instruction does a load. If so, we have to guarantee that the
939 // loaded value doesn't change between the load and the its intended
940 // destination. The check for isInvariantLoad gives the targe the chance to
941 // classify the load as always returning a constant, e.g. a constant pool
943 if (TID->mayLoad() && !TII->isInvariantLoad(this))
944 // Otherwise, this is a real load. If there is a store between the load and
945 // end of block, or if the load is volatile, we can't move it.
946 return !SawStore && !hasVolatileMemoryRef();
951 /// isSafeToReMat - Return true if it's safe to rematerialize the specified
952 /// instruction which defined the specified register instead of copying it.
953 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
954 unsigned DstReg) const {
955 bool SawStore = false;
956 if (!getDesc().isRematerializable() ||
957 !TII->isTriviallyReMaterializable(this) ||
958 !isSafeToMove(TII, SawStore))
960 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
961 const MachineOperand &MO = getOperand(i);
964 // FIXME: For now, do not remat any instruction with register operands.
965 // Later on, we can loosen the restriction is the register operands have
966 // not been modified between the def and use. Note, this is different from
967 // MachineSink because the code is no longer in two-address form (at least
971 else if (!MO.isDead() && MO.getReg() != DstReg)
977 /// hasVolatileMemoryRef - Return true if this instruction may have a
978 /// volatile memory reference, or if the information describing the
979 /// memory reference is not available. Return false if it is known to
980 /// have no volatile memory references.
981 bool MachineInstr::hasVolatileMemoryRef() const {
982 // An instruction known never to access memory won't have a volatile access.
983 if (!TID->mayStore() &&
986 !TID->hasUnmodeledSideEffects())
989 // Otherwise, if the instruction has no memory reference information,
990 // conservatively assume it wasn't preserved.
991 if (memoperands_empty())
994 // Check the memory reference information for volatile references.
995 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
996 if ((*I)->isVolatile())
1002 void MachineInstr::dump() const {
1003 errs() << " " << *this;
1006 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
1007 // Specialize printing if op#0 is definition
1008 unsigned StartOp = 0;
1009 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
1010 getOperand(0).print(OS, TM);
1012 ++StartOp; // Don't print this operand again!
1015 OS << getDesc().getName();
1017 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
1021 getOperand(i).print(OS, TM);
1024 if (!memoperands_empty()) {
1026 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1034 if (!debugLoc.isUnknown()) {
1035 const MachineFunction *MF = getParent()->getParent();
1036 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
1037 DICompileUnit CU(DLT.CompileUnit);
1038 std::string Dir, Fn;
1040 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << ","
1048 bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
1049 const TargetRegisterInfo *RegInfo,
1050 bool AddIfNotFound) {
1051 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
1052 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
1054 SmallVector<unsigned,4> DeadOps;
1055 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1056 MachineOperand &MO = getOperand(i);
1057 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
1059 unsigned Reg = MO.getReg();
1063 if (Reg == IncomingReg) {
1066 // The register is already marked kill.
1068 if (isPhysReg && isRegTiedToDefOperand(i))
1069 // Two-address uses of physregs must not be marked kill.
1074 } else if (hasAliases && MO.isKill() &&
1075 TargetRegisterInfo::isPhysicalRegister(Reg)) {
1076 // A super-register kill already exists.
1077 if (RegInfo->isSuperRegister(IncomingReg, Reg))
1079 if (RegInfo->isSubRegister(IncomingReg, Reg))
1080 DeadOps.push_back(i);
1084 // Trim unneeded kill operands.
1085 while (!DeadOps.empty()) {
1086 unsigned OpIdx = DeadOps.back();
1087 if (getOperand(OpIdx).isImplicit())
1088 RemoveOperand(OpIdx);
1090 getOperand(OpIdx).setIsKill(false);
1094 // If not found, this means an alias of one of the operands is killed. Add a
1095 // new implicit operand if required.
1096 if (!Found && AddIfNotFound) {
1097 addOperand(MachineOperand::CreateReg(IncomingReg,
1106 bool MachineInstr::addRegisterDead(unsigned IncomingReg,
1107 const TargetRegisterInfo *RegInfo,
1108 bool AddIfNotFound) {
1109 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
1110 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
1112 SmallVector<unsigned,4> DeadOps;
1113 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1114 MachineOperand &MO = getOperand(i);
1115 if (!MO.isReg() || !MO.isDef())
1117 unsigned Reg = MO.getReg();
1121 if (Reg == IncomingReg) {
1124 // The register is already marked dead.
1129 } else if (hasAliases && MO.isDead() &&
1130 TargetRegisterInfo::isPhysicalRegister(Reg)) {
1131 // There exists a super-register that's marked dead.
1132 if (RegInfo->isSuperRegister(IncomingReg, Reg))
1134 if (RegInfo->getSubRegisters(IncomingReg) &&
1135 RegInfo->getSuperRegisters(Reg) &&
1136 RegInfo->isSubRegister(IncomingReg, Reg))
1137 DeadOps.push_back(i);
1141 // Trim unneeded dead operands.
1142 while (!DeadOps.empty()) {
1143 unsigned OpIdx = DeadOps.back();
1144 if (getOperand(OpIdx).isImplicit())
1145 RemoveOperand(OpIdx);
1147 getOperand(OpIdx).setIsDead(false);
1151 // If not found, this means an alias of one of the operands is dead. Add a
1152 // new implicit operand if required.
1153 if (Found || !AddIfNotFound)
1156 addOperand(MachineOperand::CreateReg(IncomingReg,