From 0ca48de26c46281ae9fd15a042cc19cdf316bee6 Mon Sep 17 00:00:00 2001 From: Jeremy Morse Date: Fri, 4 Oct 2019 09:38:05 +0000 Subject: [PATCH] [DebugInfo] LiveDebugValues: defer DBG_VALUE creation during analysis When transfering variable locations from one place to another, LiveDebugValues immediately creates a DBG_VALUE representing that transfer. This causes trouble if the variable location should subsequently be invalidated by a loop back-edge, such as in the added test case: the transfer DBG_VALUE from a now-invalid location is used as proof that the variable location is correct. This is effectively a self-fulfilling prophesy. To avoid this, defer the insertion of transfer DBG_VALUEs until after analysis has completed. Some of those transfers are still sketchy, but we don't propagate them into other blocks now. Differential Revision: https://reviews.llvm.org/D67393 llvm-svn: 373720 --- llvm/lib/CodeGen/LiveDebugValues.cpp | 15 ++- .../X86/live-debug-values-bad-transfer.mir | 103 ++++++++++++++++++ 2 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 llvm/test/DebugInfo/MIR/X86/live-debug-values-bad-transfer.mir diff --git a/llvm/lib/CodeGen/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues.cpp index 0bdc62a345cb..8d523377fb40 100644 --- a/llvm/lib/CodeGen/LiveDebugValues.cpp +++ b/llvm/lib/CodeGen/LiveDebugValues.cpp @@ -986,9 +986,6 @@ bool LiveDebugValues::transferTerminator(MachineBasicBlock *CurMBB, const VarLocMap &VarLocIDs) { bool Changed = false; - if (OpenRanges.empty()) - return false; - LLVM_DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) { // Copy OpenRanges to OutLocs, if not already present. @@ -1362,11 +1359,6 @@ bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { DebugEntryVals, OverlapFragments, SeenFragments); OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs); - // Add any DBG_VALUE instructions necessitated by spills. - for (auto &TR : Transfers) - MBB->insertAfterBundle(TR.TransferInst->getIterator(), TR.DebugInst); - Transfers.clear(); - LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after propagating", dbgs())); LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, @@ -1387,6 +1379,13 @@ bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { assert(Pending.empty() && "Pending should be empty"); } + // Add any DBG_VALUE instructions created by location transfers. + for (auto &TR : Transfers) { + auto *MBB = TR.TransferInst->getParent(); + MBB->insertAfterBundle(TR.TransferInst->getIterator(), TR.DebugInst); + } + Transfers.clear(); + // Deferred inlocs will not have had any DBG_VALUE insts created; do // that now. flushPendingLocs(PendingInLocs, VarLocIDs); diff --git a/llvm/test/DebugInfo/MIR/X86/live-debug-values-bad-transfer.mir b/llvm/test/DebugInfo/MIR/X86/live-debug-values-bad-transfer.mir new file mode 100644 index 000000000000..1d978b9c4553 --- /dev/null +++ b/llvm/test/DebugInfo/MIR/X86/live-debug-values-bad-transfer.mir @@ -0,0 +1,103 @@ +# RUN: llc %s -mtriple=x86_64-unknown-unknown -o - -run-pass=livedebugvalues | FileCheck %s --implicit-check-not=DBG_VALUE +# +# Test that the DBG_VALUE of ecx below does not get propagated. It is considered +# live-in on LiveDebugValues' first pass through the loop, but on the second it +# should be removed from the InLocs set because it gets clobbered inside the +# loop. There should be no transfer from ecx to ebx -- this is ensured by the +# FileCheck implicit-check-not option. +# +# FIXME: we successfully prevent the false location (ebx) from being +# propagated into block 2, but the original transfer isn't yet eliminated. +# Thus we get no DBG_VALUe in block 2, but an invalid one in block 1. +# +# CHECK-LABEL: name: foo +# CHECK-LABEL: bb.0.entry: +# CHECK: $ecx = MOV32ri 0 +# CHECK-NEXT: DBG_VALUE +# CHECK-LABEL: bb.1.loop: +# CHECK: $ebx = COPY killed $ecx +# CHECK-NEXT: DBG_VALUE + +--- | + source_filename = "live-debug-values-remove-range.ll" + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + + declare void @llvm.dbg.value(metadata, metadata, metadata) + + define i32 @foo(i32 %bar) !dbg !4 { + entry: + br label %loop + loop: + br label %loop + exit: + ret i32 %bar + } + + !llvm.module.flags = !{!0, !1} + !llvm.dbg.cu = !{!2} + + !0 = !{i32 2, !"Debug Info Version", i32 3} + !1 = !{i32 2, !"Dwarf Version", i32 4} + !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "beards", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug) + !3 = !DIFile(filename: "bees.cpp", directory: ".") + !4 = distinct !DISubprogram(name: "nope", scope: !3, file: !3, line: 1, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !8) + !5 = !DISubroutineType(types: !6) + !6 = !{!7} + !7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) + !8 = !{!9} + !9 = !DILocalVariable(name: "thin", scope: !4, file: !3, line: 1, type: !7) + !10 = !DILocation(line: 1, scope: !4) + +... +--- +name: foo +alignment: 4 +tracksRegLiveness: true +liveins: + - { reg: '$edi' } +frameInfo: + stackSize: 8 + offsetAdjustment: -8 + maxAlignment: 1 + adjustsStack: true + hasCalls: true + maxCallFrameSize: 0 + cvBytesOfCalleeSavedRegisters: 8 +fixedStack: + - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16, callee-saved-register: '$rbx' } +machineFunctionInfo: {} +body: | + bb.0.entry: + liveins: $edi, $rbx + + frame-setup PUSH64r killed $rbx, implicit-def $rsp, implicit $rsp + CFI_INSTRUCTION def_cfa_offset 16 + CFI_INSTRUCTION offset $rbx, -16 + $ebx = MOV32rr $edi + $eax = MOV32ri 0 + $ecx = MOV32ri 0 + DBG_VALUE $ecx, $noreg, !9, !DIExpression(), debug-location !10 + $edi = MOV32ri 0 + $esi = MOV32ri 0 + + bb.1.loop: + successors: %bb.1, %bb.2 + liveins: $ebx, $eax, $ecx, $edi, $esi + + $eax = COPY $ecx + $ebx = COPY killed $ecx + $ecx = COPY killed $edi + $edi = COPY killed $esi + $esi = MOV32ri 1 + TEST8ri killed renamable $al, 1, implicit-def $eflags + JCC_1 %bb.1, 5, implicit killed $eflags + + bb.2.exit: + liveins: $ebx + + $eax = MOV32rr killed $ebx + $rbx = frame-destroy POP64r implicit-def $rsp, implicit $rsp + CFI_INSTRUCTION def_cfa_offset 8 + RETQ $eax + +...