Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit aa5b816

Browse files
bjopealexcrichton
authored andcommitted
[DebugInfo] Discard invalid DBG_VALUE instructions in LiveDebugVariables
Summary: This is a workaround for pr36417 https://bugs.llvm.org/show_bug.cgi?id=36417 LiveDebugVariables will now verify that the DBG_VALUE instructions are sane (prior to register allocation) by asking LIS if a virtual register used in the DBG_VALUE is live (or dead def) in the slot index before the DBG_VALUE. If it isn't sane the DBG_VALUE is discarded. One pass that was identified as introducing non-sane DBG_VALUE instructtons, when analysing pr36417, was the DAG->DAG Instruction Selection. It sometimes inserts DBG_VALUE instructions referring to a virtual register that is defined later in the same basic block. So it is a use before def kind of problem. The DBG_VALUE is typically inserted in the beginning of a basic block when this happens. The problem can be seen in the test case test/DebugInfo/X86/dbg-value-inlined-parameter.ll Reviewers: aprantl, rnk, probinson Reviewed By: aprantl Subscribers: vsk, davide, alexcrichton, Ka-Ka, eraman, llvm-commits, JDevlieghere Differential Revision: https://reviews.llvm.org/D43956 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326769 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5f6a0da commit aa5b816

File tree

3 files changed

+181
-4
lines changed

3 files changed

+181
-4
lines changed

lib/CodeGen/LiveDebugVariables.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,39 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
514514
return false;
515515
}
516516

517+
// Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
518+
// register that hasn't been defined yet. If we do not remove those here, then
519+
// the re-insertion of the DBG_VALUE instruction after register allocation
520+
// will be incorrect.
521+
// TODO: If earlier passes are corrected to generate sane debug information
522+
// (and if the machine verifier is improved to catch this), then these checks
523+
// could be removed or replaced by asserts.
524+
bool Discard = false;
525+
if (MI.getOperand(0).isReg() &&
526+
TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) {
527+
const unsigned Reg = MI.getOperand(0).getReg();
528+
if (!LIS->hasInterval(Reg)) {
529+
// The DBG_VALUE is described by a virtual register that does not have a
530+
// live interval. Discard the DBG_VALUE.
531+
Discard = true;
532+
DEBUG(dbgs() << "Discarding debug info (no LIS interval): "
533+
<< Idx << " " << MI);
534+
} else {
535+
// The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
536+
// is defined dead at Idx (where Idx is the slot index for the instruction
537+
// preceeding the DBG_VALUE).
538+
const LiveInterval &LI = LIS->getInterval(Reg);
539+
LiveQueryResult LRQ = LI.Query(Idx);
540+
if (!LRQ.valueOutOrDead()) {
541+
// We have found a DBG_VALUE with the value in a virtual register that
542+
// is not live. Discard the DBG_VALUE.
543+
Discard = true;
544+
DEBUG(dbgs() << "Discarding debug info (reg not live): "
545+
<< Idx << " " << MI);
546+
}
547+
}
548+
}
549+
517550
// Get or create the UserValue for (variable,offset) here.
518551
bool IsIndirect = MI.getOperand(1).isImm();
519552
if (IsIndirect)
@@ -522,7 +555,10 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
522555
const DIExpression *Expr = MI.getDebugExpression();
523556
UserValue *UV =
524557
getUserValue(Var, Expr, MI.getDebugLoc());
525-
UV->addDef(Idx, MI.getOperand(0), IsIndirect);
558+
if (!Discard)
559+
UV->addDef(Idx, MI.getOperand(0), IsIndirect);
560+
else
561+
UV->addDef(Idx, MachineOperand::CreateReg(0U, RegState::Debug), false);
526562
return true;
527563
}
528564

test/DebugInfo/X86/dbg-value-inlined-parameter.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
;CHECK-NEXT: DW_AT_call_line
3333

3434
;CHECK: DW_TAG_formal_parameter
35-
;FIXME: Linux shouldn't drop this parameter either...
3635
;CHECK-NOT: DW_TAG
37-
;DARWIN: DW_AT_abstract_origin {{.*}} "sp"
38-
;DARWIN: DW_TAG_formal_parameter
36+
;FIXME: Shouldn't drop this parameter...
37+
;XCHECK: DW_AT_abstract_origin {{.*}} "sp"
38+
;XCHECK: DW_TAG_formal_parameter
3939
;CHECK: DW_AT_abstract_origin {{.*}} "nums"
4040
;CHECK-NOT: DW_TAG_formal_parameter
4141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# RUN: llc -mtriple=x86_64-linux-gnu -start-before greedy -stop-after virtregrewriter -o - %s | FileCheck %s
2+
3+
--- |
4+
; ModuleID = '<stdin>'
5+
source_filename = "test/DebugInfo/X86/dbg-value-inlined-parameter.ll"
6+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
7+
target triple = "x86_64-apple-darwin"
8+
9+
%struct.S1 = type { float*, i32 }
10+
11+
@p = common global %struct.S1 zeroinitializer, align 8, !dbg !0
12+
13+
; Function Attrs: nounwind optsize ssp
14+
define void @foobar() !dbg !15 {
15+
entry:
16+
tail call void @llvm.dbg.value(metadata %struct.S1* @p, metadata !18, metadata !DIExpression()) , !dbg !25
17+
ret void, !dbg !32
18+
}
19+
20+
; Function Attrs: nounwind readnone speculatable
21+
declare void @llvm.dbg.value(metadata, metadata, metadata) #2
22+
23+
!llvm.dbg.cu = !{!2}
24+
!llvm.module.flags = !{!14}
25+
26+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
27+
!1 = !DIGlobalVariable(name: "p", scope: !2, file: !3, line: 14, type: !6, isLocal: false, isDefinition: true)
28+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 2.9 (trunk 125693)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !4, globals: !5, imports: !4)
29+
!3 = !DIFile(filename: "nm2.c", directory: "/private/tmp")
30+
!4 = !{}
31+
!5 = !{!0}
32+
!6 = !DIDerivedType(tag: DW_TAG_typedef, name: "S1", scope: !2, file: !3, line: 4, baseType: !7)
33+
!7 = !DICompositeType(tag: DW_TAG_structure_type, name: "S1", scope: !2, file: !3, line: 1, size: 128, align: 64, elements: !8)
34+
!8 = !{!9, !12}
35+
!9 = !DIDerivedType(tag: DW_TAG_member, name: "m", scope: !3, file: !3, line: 2, baseType: !10, size: 64, align: 64)
36+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, scope: !2, baseType: !11, size: 64, align: 64)
37+
!11 = !DIBasicType(name: "float", size: 32, align: 32, encoding: DW_ATE_float)
38+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "nums", scope: !3, file: !3, line: 3, baseType: !13, size: 32, align: 32, offset: 64)
39+
!13 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
40+
!14 = !{i32 1, !"Debug Info Version", i32 3}
41+
!15 = distinct !DISubprogram(name: "foobar", scope: !3, file: !3, line: 15, type: !16, isLocal: false, isDefinition: true, virtualIndex: 6, isOptimized: true, unit: !2)
42+
!16 = !DISubroutineType(types: !17)
43+
!17 = !{null}
44+
!18 = !DILocalVariable(name: "sp", arg: 1, scope: !19, file: !3, line: 7, type: !24)
45+
!19 = distinct !DISubprogram(name: "foo", scope: !3, file: !3, line: 8, type: !20, isLocal: false, isDefinition: true, scopeLine: 8, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: true, unit: !2, variables: !22)
46+
!20 = !DISubroutineType(types: !21)
47+
!21 = !{!13}
48+
!22 = !{!18, !23}
49+
!23 = !DILocalVariable(name: "nums", arg: 2, scope: !19, file: !3, line: 7, type: !13)
50+
!24 = !DIDerivedType(tag: DW_TAG_pointer_type, scope: !2, baseType: !6, size: 64, align: 64)
51+
!25 = !DILocation(line: 7, column: 13, scope: !19, inlinedAt: !26)
52+
!26 = !DILocation(line: 16, column: 3, scope: !27)
53+
!27 = distinct !DILexicalBlock(scope: !15, file: !3, line: 15, column: 15)
54+
!32 = !DILocation(line: 17, column: 1, scope: !27)
55+
56+
...
57+
---
58+
name: foobar
59+
tracksRegLiveness: true
60+
body: |
61+
bb.0:
62+
%1:gr64 = IMPLICIT_DEF
63+
%2:gr64 = IMPLICIT_DEF
64+
65+
bb.1:
66+
; This DBG_VALUE will be discarded (use before def of %0).
67+
DBG_VALUE debug-use %0, debug-use $noreg, !18, !DIExpression(), debug-location !25
68+
%0:gr64 = IMPLICIT_DEF
69+
%0:gr64 = IMPLICIT_DEF
70+
%0:gr64 = IMPLICIT_DEF
71+
%0:gr64 = IMPLICIT_DEF
72+
73+
bb.2:
74+
; This DBG_VALUE will be discarded (%1 is defined earlier, but it is not live in, so we do not know where %1 is stored).
75+
DBG_VALUE debug-use %1, debug-use $noreg, !18, !DIExpression(), debug-location !25
76+
%1:gr64 = IMPLICIT_DEF
77+
%1:gr64 = IMPLICIT_DEF
78+
%1:gr64 = IMPLICIT_DEF
79+
%1:gr64 = IMPLICIT_DEF
80+
; This DBG_VALUE is kept, even if %1 is dead, it was defined in the prev instruction,
81+
; so the value should be available for as long as the register allocated to %1 is live.
82+
DBG_VALUE debug-use %1, debug-use $noreg, !18, !DIExpression(), debug-location !25
83+
84+
bb.3:
85+
%1:gr64 = IMPLICIT_DEF
86+
DBG_VALUE 0, debug-use $noreg, !23, !DIExpression(), debug-location !25
87+
; This DBG_VALUE is kept, even if %1 is dead, it was defined in the prev non-dbg instruction,
88+
; so the value should be available for as long as the register allocated to %1 is live.
89+
DBG_VALUE debug-use %1, debug-use $noreg, !18, !DIExpression(), debug-location !25
90+
91+
bb.4:
92+
; All DBG_VALUEs here should survive. %2 is livein as it was defined in bb.0, and it has use/def in the BTS64rr instruction.
93+
DBG_VALUE debug-use %2, debug-use $noreg, !18, !DIExpression(), debug-location !25
94+
%2:gr64 = BTS64rr %2, 0, implicit-def $eflags
95+
DBG_VALUE 0, debug-use $noreg, !23, !DIExpression(), debug-location !25
96+
DBG_VALUE debug-use %2, debug-use $noreg, !18, !DIExpression(), debug-location !25
97+
%2:gr64 = BTS64rr %2, 0, implicit-def $eflags
98+
DBG_VALUE debug-use %2, debug-use $noreg, !18, !DIExpression(), debug-location !25
99+
%2:gr64 = BTS64rr %2, 0, implicit-def $eflags
100+
DBG_VALUE debug-use %2, debug-use $noreg, !18, !DIExpression(), debug-location !25
101+
102+
bb.5:
103+
RET 0, debug-location !32
104+
...
105+
106+
# CHECK-LABEL: name: foobar
107+
108+
# CHECK-LABEL: bb.1:
109+
## After solving https://bugs.llvm.org/show_bug.cgi?id=36579 we expect to get a
110+
## DBG_VALUE debug-use $noreg
111+
## here.
112+
# CHECK-NOT: DBG_VALUE
113+
114+
# CHECK-LABEL: bb.2:
115+
## After solving https://bugs.llvm.org/show_bug.cgi?id=36579 we expect to get a
116+
## DBG_VALUE debug-use $noreg
117+
## here.
118+
# CHECK-NOT: DBG_VALUE
119+
# CHECK: dead renamable $rcx = IMPLICIT_DEF
120+
# CHECK-NEXT: dead renamable $rcx = IMPLICIT_DEF
121+
# CHECK-NEXT: dead renamable $rcx = IMPLICIT_DEF
122+
# CHECK-NEXT: dead renamable $rcx = IMPLICIT_DEF
123+
# CHECK-NEXT: DBG_VALUE debug-use $rcx, debug-use $noreg, !18, !DIExpression()
124+
125+
# CHECK-LABEL: bb.3:
126+
# CHECK: dead renamable $rcx = IMPLICIT_DEF
127+
# CHECK-NEXT: DBG_VALUE 0, debug-use $noreg, !23, !DIExpression()
128+
# CHECK-NEXT: DBG_VALUE debug-use $rcx, debug-use $noreg, !18, !DIExpression()
129+
130+
# CHECK-LABEL: bb.4:
131+
# CHECK: liveins: $rax
132+
# CHECK: DBG_VALUE debug-use $rax, debug-use $noreg, !18, !DIExpression()
133+
# CHECK-NEXT: renamable $rax = BTS64rr killed renamable $rax, 0, implicit-def $eflags
134+
# CHECK-NEXT: DBG_VALUE 0, debug-use $noreg, !23, !DIExpression()
135+
# CHECK-NEXT: DBG_VALUE debug-use $rax, debug-use $noreg, !18, !DIExpression()
136+
# CHECK-NEXT: renamable $rax = BTS64rr killed renamable $rax, 0, implicit-def $eflags
137+
# CHECK-NEXT: DBG_VALUE debug-use $rax, debug-use $noreg, !18, !DIExpression()
138+
# CHECK-NEXT: dead renamable $rax = BTS64rr killed renamable $rax, 0, implicit-def $eflags
139+
140+
# CHECK-LABEL: bb.5:
141+
# CHECK-NEXT: RET 0

0 commit comments

Comments
 (0)