Skip to content

Commit 709c52b

Browse files
committed
[DebugInfo][DWARF] Emit a single location instead of a location list
for variables in nested scopes (including inlined functions) if there is a single location which covers the entire scope and the scope is contained in a single block. Based on work by @jmorse. Reviewed By: vsk, aprantl Differential Revision: https://reviews.llvm.org/D79571
1 parent 54a8524 commit 709c52b

File tree

4 files changed

+400
-5
lines changed

4 files changed

+400
-5
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,6 @@ static bool validThroughout(LexicalScopes &LScopes,
14791479
if (LSRange.size() == 0)
14801480
return false;
14811481

1482-
// If this range is neither open ended nor a constant, then it is not a
1483-
// candidate for being validThroughout.
1484-
if (RangeEnd && !DbgValue->getOperand(0).isImm())
1485-
return false;
14861482

14871483
// Determine if the DBG_VALUE is valid at the beginning of its lexical block.
14881484
const MachineInstr *LScopeBegin = LSRange.front().first;
@@ -1524,7 +1520,28 @@ static bool validThroughout(LexicalScopes &LScopes,
15241520
if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
15251521
return true;
15261522

1527-
return false;
1523+
// Now check for situations where an "open-ended" DBG_VALUE isn't enough to
1524+
// determine eligibility for a single location, e.g. nested scopes, inlined
1525+
// functions.
1526+
// FIXME: For now we just handle a simple (but common) case where the scope
1527+
// is contained in MBB. We could be smarter here.
1528+
//
1529+
// At this point we know that our scope ends in MBB. So, if RangeEnd exists
1530+
// outside of the block we can ignore it; the location is just leaking outside
1531+
// its scope.
1532+
assert(LScopeEnd->getParent() == MBB && "Scope ends outside MBB");
1533+
if (RangeEnd->getParent() != DbgValue->getParent())
1534+
return true;
1535+
1536+
// The location range and variable's enclosing scope are both contained within
1537+
// MBB, test if location terminates before end of scope.
1538+
for (auto I = RangeEnd->getIterator(); I != MBB->end(); ++I)
1539+
if (&*I == LScopeEnd)
1540+
return false;
1541+
1542+
// There's a single location which starts at the scope start, and ends at or
1543+
// after the scope end.
1544+
return true;
15281545
}
15291546

15301547
/// Build the location list for all DBG_VALUEs in the function that
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# RUN: llc -start-after=livedebugvalues --filetype=obj %s -o - \
2+
# RUN: | llvm-dwarfdump -v -name=param - | FileCheck %s
3+
#
4+
# Generated with opt -sroa -inline, llc -stop-after=livedebugvalues, with some
5+
# metadata removed by hand.
6+
#
7+
# int glob;
8+
# __attribute__((always_inline))
9+
# static void inline_me(int param) {
10+
# {
11+
# int local = param;
12+
# glob = local;
13+
# }
14+
# }
15+
# int fun(int number) {
16+
# inline_me(number);
17+
# if (number)
18+
# return 0;
19+
# return 1;
20+
# }
21+
#
22+
# The inlined parameter 'param' is available for the entirety of its enclosing
23+
# scope. We expect to see a single location entry despite the fact that the
24+
# final instruction in that scope belongs to a dominated scope.
25+
#
26+
# Except for 'param', all DILocalVariable metadata has been removed.
27+
#
28+
# Ignore first entry (abstract), we want to look at the concrete instance.
29+
# CHECK: DW_TAG_formal_parameter [
30+
# CHECK: DW_TAG_formal_parameter [
31+
# CHECK-NEXT: DW_AT_location [DW_FORM_exprloc] (DW_OP_reg5 RDI)
32+
# CHECK-NEXT: DW_AT_abstract_origin {{.*}} "param"
33+
34+
--- |
35+
target triple = "x86_64-unknown-linux-gnu"
36+
37+
@glob = dso_local global i32 0, align 4, !dbg !0
38+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
39+
declare void @llvm.dbg.value(metadata, metadata, metadata)
40+
define dso_local i32 @fun(i32 %number) !dbg !11 {
41+
entry:
42+
call void @llvm.dbg.value(metadata i32 %number, metadata !17, metadata !DIExpression()), !dbg !24
43+
store i32 %number, i32* @glob, align 4, !dbg !27
44+
%tobool = icmp ne i32 %number, 0, !dbg !32
45+
br i1 %tobool, label %return, label %if.end, !dbg !34
46+
47+
if.end: ; preds = %entry
48+
br label %return, !dbg !35
49+
50+
return: ; preds = %entry, %if.end
51+
%retval.0 = phi i32 [ 1, %if.end ], [ 0, %entry ], !dbg !16
52+
ret i32 %retval.0, !dbg !36
53+
}
54+
55+
!llvm.dbg.cu = !{!2}
56+
!llvm.module.flags = !{!7, !8, !9}
57+
!llvm.ident = !{!10}
58+
59+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
60+
!1 = distinct !DIGlobalVariable(name: "glob", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
61+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
62+
!3 = !DIFile(filename: "test.c", directory: "/")
63+
!4 = !{}
64+
!5 = !{!0}
65+
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
66+
!7 = !{i32 7, !"Dwarf Version", i32 4}
67+
!8 = !{i32 2, !"Debug Info Version", i32 3}
68+
!9 = !{i32 1, !"wchar_size", i32 4}
69+
!10 = !{!"clang version 11.0.0"}
70+
!11 = distinct !DISubprogram(name: "fun", scope: !3, file: !3, line: 9, type: !12, scopeLine: 9, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !14)
71+
!12 = !DISubroutineType(types: !13)
72+
!13 = !{!6, !6}
73+
!14 = !{}
74+
!16 = !DILocation(line: 0, scope: !11)
75+
!17 = !DILocalVariable(name: "param", arg: 1, scope: !18, file: !3, line: 3, type: !6)
76+
!18 = distinct !DISubprogram(name: "inline_me", scope: !3, file: !3, line: 3, type: !19, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !21)
77+
!19 = !DISubroutineType(types: !20)
78+
!20 = !{null, !6}
79+
!21 = !{!17}
80+
!23 = distinct !DILexicalBlock(scope: !18, file: !3, line: 4, column: 3)
81+
!24 = !DILocation(line: 0, scope: !18, inlinedAt: !25)
82+
!25 = distinct !DILocation(line: 10, column: 3, scope: !11)
83+
!26 = !DILocation(line: 0, scope: !23, inlinedAt: !25)
84+
!27 = !DILocation(line: 6, column: 10, scope: !23, inlinedAt: !25)
85+
!32 = !DILocation(line: 11, column: 7, scope: !33)
86+
!33 = distinct !DILexicalBlock(scope: !11, file: !3, line: 11, column: 7)
87+
!34 = !DILocation(line: 11, column: 7, scope: !11)
88+
!35 = !DILocation(line: 13, column: 3, scope: !11)
89+
!36 = !DILocation(line: 14, column: 1, scope: !11)
90+
91+
...
92+
---
93+
name: fun
94+
body: |
95+
bb.0.entry:
96+
successors: %bb.2(0x50000000), %bb.1(0x30000000)
97+
liveins: $edi
98+
99+
DBG_VALUE $edi, $noreg, !17, !DIExpression(), debug-location !24
100+
MOV32mr $rip, 1, $noreg, @glob, $noreg, renamable $edi, debug-location !27 :: (store 4 into @glob)
101+
renamable $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags
102+
TEST32rr killed renamable $edi, renamable $edi, implicit-def $eflags, debug-location !32
103+
JCC_1 %bb.1, 4, implicit $eflags, debug-location !34
104+
105+
bb.2.return:
106+
liveins: $eax
107+
108+
RETQ $eax, debug-location !36
109+
110+
bb.1.if.end:
111+
renamable $eax = MOV32ri 1
112+
RETQ $eax, debug-location !36
113+
114+
...
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# RUN: llc -start-after=livedebugvalues --filetype=obj %s -o - \
2+
# RUN: | llvm-dwarfdump -v -name=parama - | FileCheck %s
3+
#
4+
# Generated with -O2, llc -stop-after=livedebugvalues, with IR modified from
5+
# this source:
6+
#
7+
# int globa, globb;
8+
# void ext();
9+
# static void set(int parama, int paramb) {
10+
# globa = parama;
11+
# globb = paramb;
12+
# }
13+
# void funone(int one, int two) {
14+
# two = two + one;
15+
# // 'two = ...' sunk between the inlined assignments to globa and globa.
16+
# set(one, two);
17+
# if (two > 0)
18+
# return ext();
19+
# }
20+
# void funtwo(int one, int two) {
21+
# two = one + two;
22+
# // 'two = ...' sunk between the inlined assignments to globa and globa,
23+
# // and rdi is clobbered by the sunk 'two = ...'.
24+
# set(one, two);
25+
# if (one < two)
26+
# return ext();
27+
# }
28+
#
29+
# Check that the 'parama' is available for the entire inlined scope in funone,
30+
# and that it is unavailable in the second DW_AT_ranges segment of the inlined
31+
# scope in funtwo. In both cases the inlined scope 'set' is intrerrupted by an
32+
# instruction from the calling function.
33+
#
34+
# Except for 'parama', all DILocalVariable metadata has been removed.
35+
#
36+
# Ignore first entry (abstract), we want to look at the concrete instances.
37+
# CHECK: DW_TAG_formal_parameter [
38+
# CHECK: DW_TAG_formal_parameter [
39+
# CHECK-NEXT: DW_AT_location [DW_FORM_exprloc] (DW_OP_reg5 RDI)
40+
# CHECK-NEXT: DW_AT_abstract_origin {{.*}} "parama"
41+
# CHECK: DW_TAG_formal_parameter [
42+
# CHECK-NEXT: DW_AT_location [DW_FORM_sec_offset]
43+
# CHECK-NEXT: [0x{{[0-9a-b]+}}, 0x{{[0-9a-b]+}})
44+
# CHECK-NEXT: DW_AT_abstract_origin {{.*}} "parama"
45+
46+
--- |
47+
target triple = "x86_64-unknown-linux-gnu"
48+
49+
@globa = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0
50+
@globb = dso_local local_unnamed_addr global i32 0, align 4, !dbg !10
51+
52+
declare !dbg !6 dso_local void @ext(...) local_unnamed_addr
53+
declare void @llvm.dbg.value(metadata, metadata, metadata)
54+
55+
define dso_local void @funone(i32 %one, i32 %two) local_unnamed_addr !dbg !17 {
56+
entry:
57+
call void @llvm.dbg.value(metadata i32 %one, metadata !20, metadata !DIExpression()), !dbg !23
58+
store i32 %one, i32* @globa, align 4, !dbg !25
59+
%add = add nsw i32 %two, %one, !dbg !30
60+
store i32 %add, i32* @globb, align 4, !dbg !31
61+
%cmp = icmp sgt i32 %add, 0, !dbg !32
62+
br i1 %cmp, label %if.then, label %if.end, !dbg !34
63+
64+
if.then: ; preds = %entry
65+
tail call void (...) @ext(), !dbg !35
66+
ret void, !dbg !36
67+
68+
if.end: ; preds = %entry
69+
ret void, !dbg !36
70+
}
71+
72+
define dso_local void @funtwo(i32 %one, i32 %two) local_unnamed_addr !dbg !37 {
73+
entry:
74+
call void @llvm.dbg.value(metadata i32 %one, metadata !20, metadata !DIExpression()), !dbg !41
75+
store i32 %one, i32* @globa, align 4, !dbg !43
76+
%add = add nsw i32 %two, %one, !dbg !44
77+
store i32 %add, i32* @globb, align 4, !dbg !45
78+
%cmp = icmp sgt i32 %two, 0, !dbg !46
79+
br i1 %cmp, label %if.then, label %if.end, !dbg !48
80+
81+
if.then: ; preds = %entry
82+
tail call void (...) @ext(), !dbg !49
83+
ret void, !dbg !50
84+
85+
if.end: ; preds = %entry
86+
ret void, !dbg !50
87+
}
88+
89+
!llvm.dbg.cu = !{!2}
90+
!llvm.module.flags = !{!13, !14, !15}
91+
!llvm.ident = !{!16}
92+
93+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
94+
!1 = distinct !DIGlobalVariable(name: "globa", scope: !2, file: !3, line: 16, type: !12, isLocal: false, isDefinition: true)
95+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !5, globals: !9, splitDebugInlining: false, nameTableKind: None)
96+
!3 = !DIFile(filename: "test.c", directory: "/")
97+
!4 = !{}
98+
!5 = !{!6}
99+
!6 = !DISubprogram(name: "ext", scope: !3, file: !3, line: 17, type: !7, spFlags: DISPFlagOptimized, retainedNodes: !4)
100+
!7 = !DISubroutineType(types: !8)
101+
!8 = !{null, null}
102+
!9 = !{!0, !10}
103+
!10 = !DIGlobalVariableExpression(var: !11, expr: !DIExpression())
104+
!11 = distinct !DIGlobalVariable(name: "globb", scope: !2, file: !3, line: 16, type: !12, isLocal: false, isDefinition: true)
105+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
106+
!13 = !{i32 7, !"Dwarf Version", i32 4}
107+
!14 = !{i32 2, !"Debug Info Version", i32 3}
108+
!15 = !{i32 1, !"wchar_size", i32 4}
109+
!16 = !{!"clang version 11.0.0"}
110+
!17 = distinct !DISubprogram(name: "funone", scope: !3, file: !3, line: 22, type: !18, scopeLine: 22, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !4)
111+
!18 = !DISubroutineType(types: !19)
112+
!19 = !{null, !12, !12}
113+
!20 = !DILocalVariable(name: "parama", arg: 1, scope: !21, file: !3, line: 18, type: !12)
114+
!21 = distinct !DISubprogram(name: "set", scope: !3, file: !3, line: 18, type: !18, scopeLine: 18, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !22)
115+
!22 = !{!20}
116+
!23 = !DILocation(line: 0, scope: !21, inlinedAt: !24)
117+
!24 = distinct !DILocation(line: 24, column: 3, scope: !17)
118+
!25 = !DILocation(line: 19, column: 9, scope: !21, inlinedAt: !24)
119+
!30 = !DILocation(line: 23, column: 13, scope: !17)
120+
!31 = !DILocation(line: 20, column: 9, scope: !21, inlinedAt: !24)
121+
!32 = !DILocation(line: 25, column: 11, scope: !33)
122+
!33 = distinct !DILexicalBlock(scope: !17, file: !3, line: 25, column: 7)
123+
!34 = !DILocation(line: 25, column: 7, scope: !17)
124+
!35 = !DILocation(line: 26, column: 12, scope: !33)
125+
!36 = !DILocation(line: 27, column: 1, scope: !17)
126+
!37 = distinct !DISubprogram(name: "funtwo", scope: !3, file: !3, line: 28, type: !18, scopeLine: 28, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !38)
127+
!38 = !{}
128+
!41 = !DILocation(line: 0, scope: !21, inlinedAt: !42)
129+
!42 = distinct !DILocation(line: 30, column: 3, scope: !37)
130+
!43 = !DILocation(line: 19, column: 9, scope: !21, inlinedAt: !42)
131+
!44 = !DILocation(line: 29, column: 13, scope: !37)
132+
!45 = !DILocation(line: 20, column: 9, scope: !21, inlinedAt: !42)
133+
!46 = !DILocation(line: 31, column: 11, scope: !47)
134+
!47 = distinct !DILexicalBlock(scope: !37, file: !3, line: 31, column: 7)
135+
!48 = !DILocation(line: 31, column: 7, scope: !37)
136+
!49 = !DILocation(line: 32, column: 12, scope: !47)
137+
!50 = !DILocation(line: 33, column: 1, scope: !37)
138+
139+
...
140+
---
141+
name: funone
142+
body: |
143+
bb.0.entry:
144+
successors: %bb.2(0x50000000), %bb.1(0x30000000)
145+
liveins: $edi, $esi
146+
147+
DBG_VALUE $edi, $noreg, !20, !DIExpression(), debug-location !23
148+
MOV32mr $rip, 1, $noreg, @globa, $noreg, renamable $edi, debug-location !25 :: (store 4 into @globa)
149+
renamable $esi = ADD32rr killed renamable $esi, killed renamable $edi, implicit-def $eflags, debug-location !32
150+
MOV32mr $rip, 1, $noreg, @globb, $noreg, killed renamable $esi, debug-location !31 :: (store 4 into @globb)
151+
JCC_1 %bb.1, 14, implicit $eflags, debug-location !34
152+
153+
bb.2.if.then:
154+
dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !35
155+
TAILJMPd64 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit killed $al, debug-location !35
156+
157+
bb.1.if.end:
158+
RETQ debug-location !36
159+
160+
...
161+
---
162+
name: funtwo
163+
body: |
164+
bb.0.entry:
165+
successors: %bb.2(0x50000000), %bb.1(0x30000000)
166+
liveins: $edi, $esi
167+
168+
DBG_VALUE $edi, $noreg, !20, !DIExpression(), debug-location !41
169+
MOV32mr $rip, 1, $noreg, @globa, $noreg, renamable $edi, debug-location !43 :: (store 4 into @globa)
170+
renamable $edi = nsw ADD32rr killed renamable $edi, renamable $esi, implicit-def dead $eflags, debug-location !44
171+
MOV32mr $rip, 1, $noreg, @globb, $noreg, killed renamable $edi, debug-location !45 :: (store 4 into @globb)
172+
TEST32rr killed renamable $esi, renamable $esi, implicit-def $eflags, debug-location !46
173+
JCC_1 %bb.1, 14, implicit $eflags, debug-location !48
174+
175+
bb.2.if.then:
176+
dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !49
177+
TAILJMPd64 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit killed $al, debug-location !49
178+
179+
bb.1.if.end:
180+
RETQ debug-location !50
181+
182+
...

0 commit comments

Comments
 (0)