[llvm] 4955544 - [LiveDebugValues][InstrRef][1/2] Recover more clobbered variable locations
Jeremy Morse via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 30 08:57:05 PDT 2021
Author: Jeremy Morse
Date: 2021-06-30T16:56:25+01:00
New Revision: 49555441628a0ec620581bba371e6bb20c2b3f5f
URL: https://github.com/llvm/llvm-project/commit/49555441628a0ec620581bba371e6bb20c2b3f5f
DIFF: https://github.com/llvm/llvm-project/commit/49555441628a0ec620581bba371e6bb20c2b3f5f.diff
LOG: [LiveDebugValues][InstrRef][1/2] Recover more clobbered variable locations
In various circumstances, when we clobber a register there may be
alternative locations that the value is live in. The classic example would
be a value loaded from the stack, and then clobbered: the value is still
available on the stack. InstrRefBasedLDV was coping with this at block
starts where it's forced to pick a location, however it wasn't searching
for alternative locations when values were clobbered.
This patch notifies the "Transfer Tracker" object when clobbers occur, and
it's able to find alternatives and issue DBG_VALUEs for that location. See:
the added test.
Differential Revision: https://reviews.llvm.org/D88405
Added:
llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_recover_clobbers.mir
Modified:
llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_instrref_tolocs.mir
llvm/test/DebugInfo/MIR/X86/livedebugvalues_load_in_loop.mir
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
index c29900b2c694d..83c3ceccf0640 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
@@ -1225,26 +1225,63 @@ class TransferTracker {
}
}
- /// Explicitly terminate variable locations based on \p mloc. Creates undef
- /// DBG_VALUEs for any variables that were located there, and clears
- /// #ActiveMLoc / #ActiveVLoc tracking information for that location.
- void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos) {
- assert(MTracker->isSpill(MLoc));
+ /// Account for a location \p mloc being clobbered. Examine the variable
+ /// locations that will be terminated: and try to recover them by using
+ /// another location. Optionally, given \p MakeUndef, emit a DBG_VALUE to
+ /// explicitly terminate a location if it can't be recovered.
+ void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos,
+ bool MakeUndef = true) {
auto ActiveMLocIt = ActiveMLocs.find(MLoc);
if (ActiveMLocIt == ActiveMLocs.end())
return;
+ // What was the old variable value?
+ ValueIDNum OldValue = VarLocs[MLoc.asU64()];
VarLocs[MLoc.asU64()] = ValueIDNum::EmptyValue;
+ // Examine the remaining variable locations: if we can find the same value
+ // again, we can recover the location.
+ Optional<LocIdx> NewLoc = None;
+ for (auto Loc : MTracker->locations())
+ if (Loc.Value == OldValue)
+ NewLoc = Loc.Idx;
+
+ // If there is no location, and we weren't asked to make the variable
+ // explicitly undef, then stop here.
+ if (!NewLoc && !MakeUndef)
+ return;
+
+ // Examine all the variables based on this location.
+ DenseSet<DebugVariable> NewMLocs;
for (auto &Var : ActiveMLocIt->second) {
auto ActiveVLocIt = ActiveVLocs.find(Var);
- // Create an undef. We can't feed in a nullptr DIExpression alas,
- // so use the variables last expression. Pass None as the location.
+ // Re-state the variable location: if there's no replacement then NewLoc
+ // is None and a $noreg DBG_VALUE will be created. Otherwise, a DBG_VALUE
+ // identifying the alternative location will be emitted.
const DIExpression *Expr = ActiveVLocIt->second.Properties.DIExpr;
DbgValueProperties Properties(Expr, false);
- PendingDbgValues.push_back(MTracker->emitLoc(None, Var, Properties));
- ActiveVLocs.erase(ActiveVLocIt);
+ PendingDbgValues.push_back(MTracker->emitLoc(NewLoc, Var, Properties));
+
+ // Update machine locations <=> variable locations maps. Defer updating
+ // ActiveMLocs to avoid invalidaing the ActiveMLocIt iterator.
+ if (!NewLoc) {
+ ActiveVLocs.erase(ActiveVLocIt);
+ } else {
+ ActiveVLocIt->second.Loc = *NewLoc;
+ NewMLocs.insert(Var);
+ }
}
+
+ // Commit any deferred ActiveMLoc changes.
+ if (!NewMLocs.empty())
+ for (auto &Var : NewMLocs)
+ ActiveMLocs[*NewLoc].insert(Var);
+
+ // We lazily track what locations have which values; if we've found a new
+ // location for the clobbered value, remember it.
+ if (NewLoc)
+ VarLocs[NewLoc->asU64()] = OldValue;
+
flushDbgValues(Pos, nullptr);
ActiveMLocIt->second.clear();
@@ -1899,6 +1936,32 @@ void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
for (auto *MO : RegMaskPtrs)
MTracker->writeRegMask(MO, CurBB, CurInst);
+
+ if (!TTracker)
+ return;
+
+ // When committing variable values to locations: tell transfer tracker that
+ // we've clobbered things. It may be able to recover the variable from a
+ //
diff erent location.
+
+ // Inform TTracker about any direct clobbers.
+ for (uint32_t DeadReg : DeadRegs) {
+ LocIdx Loc = MTracker->lookupOrTrackRegister(DeadReg);
+ TTracker->clobberMloc(Loc, MI.getIterator(), false);
+ }
+
+ // Look for any clobbers performed by a register mask. Only test locations
+ // that are actually being tracked.
+ for (auto L : MTracker->locations()) {
+ // Stack locations can't be clobbered by regmasks.
+ if (MTracker->isSpill(L.Idx))
+ continue;
+
+ Register Reg = MTracker->LocIdxToLocID[L.Idx];
+ for (auto *MO : RegMaskPtrs)
+ if (MO->clobbersPhysReg(Reg))
+ TTracker->clobberMloc(L.Idx, MI.getIterator(), false);
+ }
}
void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) {
@@ -2046,8 +2109,12 @@ bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
if (TTracker) {
Optional<LocIdx> MLoc = MTracker->getSpillMLoc(*Loc);
- if (MLoc)
+ if (MLoc) {
+ // Un-set this location before clobbering, so that we don't salvage
+ // the variable location back to the same place.
+ MTracker->setMLoc(*MLoc, ValueIDNum::EmptyValue);
TTracker->clobberMloc(*MLoc, MI.getIterator());
+ }
}
}
@@ -2162,6 +2229,15 @@ bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
if (EmulateOldLDV && SrcReg != DestReg)
MTracker->defReg(SrcReg, CurBB, CurInst);
+ // Finally, the copy might have clobbered variables based on the destination
+ // register. Tell TTracker about it, in case a backup location exists.
+ if (TTracker) {
+ for (MCRegAliasIterator RAI(DestReg, TRI, true); RAI.isValid(); ++RAI) {
+ LocIdx ClobberedLoc = MTracker->getRegMLoc(*RAI);
+ TTracker->clobberMloc(ClobberedLoc, MI.getIterator(), false);
+ }
+ }
+
return true;
}
diff --git a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
index 770c46ec84369..38e803d1abb55 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp
@@ -14,6 +14,7 @@
#include "llvm/CodeGen/Passes.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetMachine.h"
/// \file LiveDebugValues.cpp
@@ -33,6 +34,12 @@
using namespace llvm;
+static cl::opt<bool>
+ ForceInstrRefLDV("force-instr-ref-livedebugvalues", cl::Hidden,
+ cl::desc("Use instruction-ref based LiveDebugValues with "
+ "normal DBG_VALUE inputs"),
+ cl::init(false));
+
/// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
/// InstrRefBasedLDV to perform location propagation, via the LDVImpl
/// base class.
@@ -87,6 +94,9 @@ bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
InstrRefBased = TM.Options.ValueTrackingVariableLocations;
}
+ // Allow the user to force selection of InstrRef LDV.
+ InstrRefBased |= ForceInstrRefLDV;
+
if (InstrRefBased)
TheImpl = llvm::makeInstrRefBasedLiveDebugValues();
else
diff --git a/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_instrref_tolocs.mir b/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_instrref_tolocs.mir
index 9f77add7bc137..578cac9dc0ec4 100644
--- a/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_instrref_tolocs.mir
+++ b/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_instrref_tolocs.mir
@@ -42,7 +42,7 @@ body: |
$rbx = COPY killed $rax, debug-location !17
$rax = MOV64ri 1, debug-location !17
- ; Presently, this COPY isn't followed. Dealing with that is future work.
+ ; CHECK: DBG_VALUE $rbx, $noreg
DBG_INSTR_REF 2, 0, !16, !DIExpression(), debug-location !17
; No instruction is labelled with the number "2". This should produce an
diff --git a/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_recover_clobbers.mir b/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_recover_clobbers.mir
new file mode 100644
index 0000000000000..3d45a548e26e9
--- /dev/null
+++ b/llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_recover_clobbers.mir
@@ -0,0 +1,100 @@
+--- |
+ ; RUN: llc %s -march=x86-64 -run-pass=livedebugvalues -o - -force-instr-ref-livedebugvalues=1 -emulate-old-livedebugvalues=0 | FileCheck %s -implicit-check-not=DBG_VALUE
+
+ ;; When using instruction referencing LiveDebugValues, when a register gets
+ ;; clobbered, we should transfer variable locations to backup locations, if
+ ;; one is available.
+ ;; I've written this test in terms of DBG_VALUEs rather than DBG_INSTR_REFs
+ ;; as this is purely a LiveDebugValues feature, and should work without the
+ ;; need to use any other instructoin referencing work.
+
+ declare i32 @use() local_unnamed_addr;
+
+ define i32 @_Z8bb_to_bb() local_unnamed_addr !dbg !12 {
+ entry:
+ br label %bb1, !dbg !17
+ bb1:
+ br label %bb2, !dbg !17
+ bb2:
+ br label %bb3, !dbg !17
+ bb3:
+ br label %bb3, !dbg !17
+ bb4:
+ br label %bb3, !dbg !17
+ bb5:
+ ret i32 0, !dbg !17
+ }
+
+ !llvm.dbg.cu = !{!0}
+ !llvm.module.flags = !{!7, !8, !9, !10}
+ !llvm.ident = !{!11}
+ !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 10.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, debugInfoForProfiling: true, nameTableKind: None)
+ !1 = !DIFile(filename: "main.cpp", directory: "F:\")
+ !2 = !{}
+ !3 = !{!4}
+ !4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
+ !5 = distinct !DIGlobalVariable(name: "start", scope: !0, file: !1, line: 4, type: !6, isLocal: false, isDefinition: true)
+ !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+ !7 = !{i32 2, !"Dwarf Version", i32 4}
+ !8 = !{i32 2, !"Debug Info Version", i32 3}
+ !9 = !{i32 1, !"wchar_size", i32 2}
+ !10 = !{i32 7, !"PIC Level", i32 2}
+ !11 = !{!"clang version 10.0.0"}
+ !12 = distinct !DISubprogram(name: "bb_to_bb", linkageName: "bb_to_bb", scope: !1, file: !1, line: 6, type: !13, scopeLine: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !15)
+ !13 = !DISubroutineType(types: !14)
+ !14 = !{!6, !6}
+ !15 = !{!16}
+ !16 = !DILocalVariable(name: "myVar", scope: !12, file: !1, line: 7, type: !6)
+ !17 = !DILocation(line: 10, scope: !12)
+
+...
+---
+name: _Z8bb_to_bb
+stack:
+ - { id: 0, type: spill-slot, offset: -12, size: 4, alignment: 4 }
+body: |
+ bb.0.entry:
+ $eax = MOV32ri 0, debug-location !17
+ $eax = COPY $ebx
+ DBG_VALUE $eax, $noreg, !16, !DIExpression(), debug-location !17
+ ;; Over-write eax, we should recover its location as being in ebx.
+ $eax = MOV32ri 0, debug-location !17
+
+ ; CHECK: DBG_VALUE $eax
+ ; CHECK-NEXT: $eax = MOV32ri 0
+ ; CHECK-NEXT: DBG_VALUE $ebx
+
+ ;; The same should occur for spills.
+ $ebx = MOV32ri 2, debug-location !17
+ MOV32mr $rsp, 1, _, -12, _, killed $ebx :: (store 4 into %stack.0)
+ DBG_VALUE $ebx, $noreg, !16, !DIExpression(), debug-location !17
+ $ebx = MOV32ri 0, debug-location !17
+
+ ; CHECK-NEXT: $ebx = MOV32ri 2
+ ; CHECK-NEXT: MOV32mr $rsp
+ ; CHECK-NEXT: DBG_VALUE $ebx
+ ; CHECK-NEXT: $ebx = MOV32ri
+ ; CHECK-NEXT: DBG_VALUE $rsp
+
+ ;; Now test copies and register masks.
+ $eax = COPY $ebx
+ DBG_VALUE $ebx, $noreg, !16, !DIExpression(), debug-location !17
+ ;; Overwrite ebx with a copy.
+ $ecx = MOV32ri 1, debug-location !17
+ $ebx = COPY $ecx
+
+ ; CHECK: DBG_VALUE $ebx
+ ; CHECK-NEXT: $ecx = MOV32ri
+ ; CHECK-NEXT: $ebx = COPY
+ ; CHECK-NEXT: DBG_VALUE $eax
+
+ ;; Similarly, with a register mask
+ $ebx = COPY $eax
+ CALL64pcrel32 @use, csr_64, implicit $rsp, implicit $edi, implicit-def $rsp, debug-location !17
+
+ ; CHECK-NEXT: $ebx = COPY $eax
+ ; CHECK-NEXT: CALL64pcrel32
+ ; CHECK-NEXT: DBG_VALUE $ebx
+
+ RETQ $eax, debug-location !17
+...
diff --git a/llvm/test/DebugInfo/MIR/X86/livedebugvalues_load_in_loop.mir b/llvm/test/DebugInfo/MIR/X86/livedebugvalues_load_in_loop.mir
index 97af3bf502196..c3ef29b528beb 100644
--- a/llvm/test/DebugInfo/MIR/X86/livedebugvalues_load_in_loop.mir
+++ b/llvm/test/DebugInfo/MIR/X86/livedebugvalues_load_in_loop.mir
@@ -11,6 +11,7 @@
; CHECK-LABEL: bb.0.entry:
; CHECK: DBG_VALUE $rdi, $noreg, !16, !DIExpression()
+ ; CHECK: DBG_VALUE $rbp, $noreg, !16, !DIExpression()
; CHECK-LABEL: bb.1.bb1:
; CHECK: DBG_VALUE $rbp, $noreg, !16, !DIExpression()
; CHECK: DBG_VALUE $rbp, $noreg, !16, !DIExpression()
More information about the llvm-commits
mailing list