Skip to content

Commit 81d8ef2

Browse files
committedOct 1, 2018
[DebugInfo][Dexter] Incorrect DBG_VALUE after MCP dead copy instruction removal.
When MachineCopyPropagation eliminates a dead 'copy', its associated debug information becomes invalid. as the recorded register has been removed. It causes the debugger to display wrong variable value. Differential Revision: https://reviews.llvm.org/D52614 llvm-svn: 343445
1 parent ce4caff commit 81d8ef2

File tree

5 files changed

+126
-6
lines changed

5 files changed

+126
-6
lines changed
 

‎llvm/include/llvm/CodeGen/MachineInstr.h

+4
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,10 @@ class MachineInstr
15441544
/// Scan instructions following MI and collect any matching DBG_VALUEs.
15451545
void collectDebugValues(SmallVectorImpl<MachineInstr *> &DbgValues);
15461546

1547+
/// Find all DBG_VALUEs immediately following this instruction that point
1548+
/// to a register def in this instruction and point them to \p Reg instead.
1549+
void changeDebugValuesDefReg(unsigned Reg);
1550+
15471551
private:
15481552
/// If this instruction is embedded into a MachineFunction, return the
15491553
/// MachineRegisterInfo object for the current function, otherwise

‎llvm/lib/CodeGen/MachineCSE.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,8 @@ bool MachineCSE::PerformTrivialCopyPropagation(MachineInstr *MI,
181181
LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
182182
LLVM_DEBUG(dbgs() << "*** to: " << *MI);
183183

184-
// Collect matching debug values.
185-
SmallVector<MachineInstr *, 2> DbgValues;
186-
DefMI->collectDebugValues(DbgValues);
187-
// Propagate SrcReg to debug value instructions.
188-
for (auto *DBI : DbgValues)
189-
DBI->getOperand(0).setReg(SrcReg);
184+
// Update matching debug values.
185+
DefMI->changeDebugValuesDefReg(SrcReg);
190186

191187
// Propagate SrcReg of copies to MI.
192188
MO.setReg(SrcReg);

‎llvm/lib/CodeGen/MachineCopyPropagation.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
598598
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
599599
MaybeDead->dump());
600600
assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
601+
602+
// Update matching debug values.
603+
assert(MaybeDead->isCopy());
604+
MaybeDead->changeDebugValuesDefReg(MaybeDead->getOperand(1).getReg());
605+
601606
MaybeDead->eraseFromParent();
602607
Changed = true;
603608
++NumDeletes;

‎llvm/lib/CodeGen/MachineInstr.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -2092,3 +2092,13 @@ void MachineInstr::collectDebugValues(
20922092
DbgValues.push_back(&*DI);
20932093
}
20942094
}
2095+
2096+
void MachineInstr::changeDebugValuesDefReg(unsigned Reg) {
2097+
// Collect matching debug values.
2098+
SmallVector<MachineInstr *, 2> DbgValues;
2099+
collectDebugValues(DbgValues);
2100+
2101+
// Propagate Reg to debug value instructions.
2102+
for (auto *DBI : DbgValues)
2103+
DBI->getOperand(0).setReg(Reg);
2104+
}

‎llvm/test/CodeGen/MIR/X86/pr38773.mir

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=machine-cp | FileCheck %s
2+
3+
# When MachineCopyPropagation eliminates a dead 'copy', its associated debug
4+
# information becomes invalid. as the recorded register has been removed.
5+
# It causes the debugger to display wrong variable value.
6+
#
7+
# When in the debugger, on the line "return read1;", the value of "read1"
8+
# is reported as '4', where it should be '1'.
9+
#
10+
# MIR generated with:
11+
# clang -S -g -O2 -emit-llvm pr38773.cpp -o pr38773.ll -mllvm
12+
# llc pr38773.ll -stop-after=tailduplication -simplify-mir
13+
#
14+
# // pr38773.cpp
15+
# int main() {
16+
# volatile int foo = 4;
17+
# int read1 = foo;
18+
# int read2 = foo;
19+
#
20+
# switch ((read1 == 4) ? 3 : 1) {
21+
# case 1:
22+
# read1 *= read2;
23+
# break;
24+
# case 3:
25+
# read1 /= read2;
26+
# break;
27+
# }
28+
#
29+
# return read1;
30+
# }
31+
#
32+
# Update the register for the '@llvm.dbg.value' associated with 'read1', when
33+
# the 'copy' is removed, to be the 'source' register.
34+
35+
--- |
36+
; ModuleID = 'pr38773.ll'
37+
source_filename = "pr38773.cpp"
38+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
39+
target triple = "x86_64-pc-linux-gnu"
40+
41+
define dso_local i32 @main() local_unnamed_addr !dbg !7 {
42+
entry:
43+
%foo = alloca i32, align 4
44+
store volatile i32 4, i32* %foo, align 4
45+
%foo.0.foo.0. = load volatile i32, i32* %foo, align 4
46+
%foo.0.foo.0.6 = load volatile i32, i32* %foo, align 4
47+
%cmp = icmp eq i32 %foo.0.foo.0., 4
48+
br i1 %cmp, label %sw.bb1, label %sw.bb
49+
50+
sw.bb: ; preds = %entry
51+
%mul = mul nsw i32 %foo.0.foo.0.6, %foo.0.foo.0.
52+
br label %sw.epilog
53+
54+
sw.bb1: ; preds = %entry
55+
%div = sdiv i32 4, %foo.0.foo.0.6
56+
call void @llvm.dbg.value(metadata i32 %div, metadata !12, metadata !DIExpression()), !dbg !13
57+
br label %sw.epilog
58+
59+
sw.epilog: ; preds = %sw.bb1, %sw.bb
60+
%read1.0 = phi i32 [ %div, %sw.bb1 ], [ %mul, %sw.bb ]
61+
call void @llvm.dbg.value(metadata i32 %read1.0, metadata !12, metadata !DIExpression()), !dbg !13
62+
ret i32 %read1.0
63+
}
64+
65+
declare void @llvm.dbg.value(metadata, metadata, metadata) #0
66+
67+
!llvm.dbg.cu = !{!0}
68+
!llvm.module.flags = !{!3, !4, !5}
69+
!llvm.ident = !{!6}
70+
71+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 8.0.0 (trunk 343183)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
72+
!1 = !DIFile(filename: "pr38773.cpp", directory: ".")
73+
!2 = !{}
74+
!3 = !{i32 2, !"Dwarf Version", i32 4}
75+
!4 = !{i32 2, !"Debug Info Version", i32 3}
76+
!5 = !{i32 1, !"wchar_size", i32 4}
77+
!6 = !{!"clang version 8.0.0 (trunk 343183)"}
78+
!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !11)
79+
!8 = !DISubroutineType(types: !9)
80+
!9 = !{!10}
81+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
82+
!11 = !{!12}
83+
!12 = !DILocalVariable(name: "read1", scope: !7, file: !1, line: 3, type: !10)
84+
!13 = !DILocation(line: 3, column: 7, scope: !7)
85+
86+
...
87+
---
88+
name: main
89+
90+
body: |
91+
92+
bb.2.sw.bb1:
93+
liveins: $ecx
94+
95+
$eax = MOV32ri 4
96+
$edx = MOV32r0 implicit-def dead $eflags
97+
IDIV32r killed renamable $ecx, implicit-def $eax, implicit-def dead $edx, implicit-def dead $eflags, implicit $eax, implicit killed $edx
98+
renamable $ecx = COPY $eax
99+
; CHECK: IDIV32r killed renamable $ecx
100+
; CHECK-NEXT: DBG_VALUE debug-use $eax, debug-use $noreg, !12, !DIExpression(), debug-location !13
101+
DBG_VALUE debug-use $ecx, debug-use $noreg, !12, !DIExpression(), debug-location !13
102+
$eax = COPY killed renamable $ecx
103+
RET 0, $eax
104+
105+
...

0 commit comments

Comments
 (0)
Please sign in to comment.