Skip to content

Commit e8209b2

Browse files
[MachineSink] Drop debug info for instructions deleted by sink-and-fold (llvm#71443)
After performing sink-and-fold over a COPY, the original instruction is replaced with one that produces its output in the destination of the copy. Its value is still available (in a hard register), so if there are debug instructions which refer to the (now deleted) virtual register they could be updated to refer to the hard register, in principle. However, it's not clear how to do that, moreover in some cases the debug instructions may need to be replicated proportionally to the number of the COPY instructions replaced and in some extreme cases we can end up with quadratic increase in the number of debug instructions, e.g: int f(int); void g(int x) { int y = x + 1; int t0 = y; f(t0); int t1 = y; f(t1); }
1 parent 215bacb commit e8209b2

File tree

4 files changed

+162
-326
lines changed

4 files changed

+162
-326
lines changed

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -510,30 +510,23 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
510510
LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
511511
SinkDst->dump());
512512
if (SinkDst->isCopy()) {
513+
// TODO: After performing the sink-and-fold, the original instruction is
514+
// deleted. Its value is still available (in a hard register), so if there
515+
// are debug instructions which refer to the (now deleted) virtual
516+
// register they could be updated to refer to the hard register, in
517+
// principle. However, it's not clear how to do that, moreover in some
518+
// cases the debug instructions may need to be replicated proportionally
519+
// to the number of the COPY instructions replaced and in some extreme
520+
// cases we can end up with quadratic increase in the number of debug
521+
// instructions.
522+
513523
// Sink a copy of the instruction, replacing a COPY instruction.
514524
MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
515525
Register DstReg = SinkDst->getOperand(0).getReg();
516526
TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI, *TRI);
517-
// If the original instruction did not have source location, reuse a one
518-
// from the COPY.
527+
// Reuse the source location from the COPY.
519528
New = &*std::prev(InsertPt);
520-
if (const DebugLoc &NewLoc = New->getDebugLoc(); !NewLoc)
521-
New->setDebugLoc(SinkDst->getDebugLoc());
522-
// Sink DBG_VALUEs, which refer to the original instruction's destination
523-
// (DefReg).
524-
MachineBasicBlock &SinkMBB = *SinkDst->getParent();
525-
auto &DbgUsers = SeenDbgUsers[DefReg];
526-
for (auto &U : DbgUsers) {
527-
MachineInstr *DbgMI = U.getPointer();
528-
if (U.getInt())
529-
continue;
530-
MachineInstr *NewDbgMI = SinkDst->getMF()->CloneMachineInstr(DbgMI);
531-
SinkMBB.insertAfter(InsertPt, NewDbgMI);
532-
for (auto &SrcMO : DbgMI->getDebugOperandsForReg(DefReg)) {
533-
auto &DstMO = NewDbgMI->getOperand(SrcMO.getOperandNo());
534-
DstMO.setReg(DstReg);
535-
}
536-
}
529+
New->setDebugLoc(SinkDst->getDebugLoc());
537530
} else {
538531
// Fold instruction into the addressing mode of a memory instruction.
539532
New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);

llvm/test/CodeGen/AArch64/sink-and-fold-dbg-value-crash.mir

Lines changed: 0 additions & 304 deletions
This file was deleted.

0 commit comments

Comments
 (0)