xref: /llvm-project/llvm/test/DebugInfo/MIR/InstrRef/single-assign-propagation.mir (revision 40a4cbb0f200e5e0bafbd58d55c2da6daab9515d)
1# RUN: llc %s -mtriple=x86_64 -run-pass=livedebugvalues -o - \
2# RUN:       -experimental-debug-variable-locations=true \
3# RUN:     | FileCheck %s -implicit-check-not=DBG_VALUE \
4# RUN:        --check-prefixes=CHECK,COMMON
5#
6# This test is designed to stimulate a simplification of variable-value
7# propagation in InstrRefBasedLDV. When we only have a single assignment of
8# a variable, we don't have to completely enumerate all the PHIs the variable
9# has and then value propagate through them: the variable value is available
10# only in those blocks dominated by the assignment. Example in this test:
11#
12#             entry
13#            /  |  \
14#           /   v   \
15#          /   bb1   \    <--- Single variable assignment happens here
16#         /   /   \   \
17#        /   v     v   \
18#        | bb2     bb3 |
19#        |   \    /    |
20#        |    v  v     |
21#        |     bb4     |
22#         \   /   \    /
23#          v v     v  v
24#           bb5    bb6    <---  Dominance frontier is here
25#
26# The general InstrRefBasedLDV algorithm takes this CFG, places PHIs at bb5 and
27# bb6, then value-propagates until it's determined that they can't be
28# eliminated. Then, we determine that there's no machine location for such PHIs,
29# and no variable location is emitted.
30#
31# The fast way of doing this: observe that the variable value can never extend
32# past the dominance frontier of the block where the assignment happens. So
33# just propagate the variable value into the dominated blocks, and avoid the
34# general algorithm.
35#
36# Doing so introduces a functional change: VarLocBasedLDV won't propagate
37# variable locations through out-of-scope blocks, and InstrRefBasedLDV tries to
38# replicate VarLocBasedLDV most of the time. In the MIR below, bb2 is out of
39# scope. This effectively becomes another variable assignment (of undef), which
40# requires full SSA and value propagation to emulate what VarLocBasedLDV did.
41# Applying this speed optimisation makes us diverge from what VarLocBasedLDV
42# does by not treating the out-of-scope block as an effective undef assignment,
43# hence this test.
44#
45# CHECK-LABEL: bb.1:
46# CHECK:       DBG_VALUE
47# CHECK-LABEL: bb.2:
48## No location here because it's out-of-scope.
49# CHECK-LABEL: bb.3:
50# CHECK:       DBG_VALUE
51# CHECK-LABEL: bb.4:
52# CHECK:       DBG_VALUE
53#
54## VarLocBasedLDV will take the DBG_VALUE in the assignment block, propagate
55## to bb.3, but not into bb.4 because of the intervening out-of-scope block.
56## Disabled actual testing of this because it's just for comparison purposes.
57#
58# varloc-label: bb.1:
59# varloc:       DBG_VALUE
60# varloc-label: bb.2:
61## No location here because it's out-of-scope.
62# varloc-label: bb.3:
63# varloc:       DBG_VALUE
64#
65## Common tail for 'test2' -- this is checking that the assignment of undef or
66## $noreg in single-assignment mode doesn't lead to trouble further down the
67## line, specifically assertion failures.
68#
69# COMMON-LABEL: name: test2
70# COMMON: DBG_VALUE $noreg
71--- |
72  define i32 @_Z8bb_to_bb() local_unnamed_addr !dbg !12 {
73  entry:
74    ret i32 0, !dbg !17
75  }
76
77  define i32 @test2() local_unnamed_addr !dbg !112 {
78  entry:
79    ret i32 0, !dbg !117
80  }
81
82  !llvm.dbg.cu = !{!0}
83  !llvm.module.flags = !{!7, !8, !9, !10}
84  !llvm.ident = !{!11}
85
86  !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, debugInfoForProfiling: true, nameTableKind: None)
87  !1 = !DIFile(filename: "main.cpp", directory: "F:\")
88  !2 = !{}
89  !3 = !{!4}
90  !4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
91  !5 = distinct !DIGlobalVariable(name: "start", scope: !0, file: !1, line: 4, type: !6, isLocal: false, isDefinition: true)
92  !6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
93  !7 = !{i32 2, !"Dwarf Version", i32 4}
94  !8 = !{i32 2, !"Debug Info Version", i32 3}
95  !9 = !{i32 1, !"wchar_size", i32 2}
96  !10 = !{i32 7, !"PIC Level", i32 2}
97  !11 = !{!"clang"}
98  !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)
99  !13 = !DISubroutineType(types: !14)
100  !14 = !{!6, !6}
101  !15 = !{!16}
102  !16 = !DILocalVariable(name: "myVar", scope: !18, file: !1, line: 7, type: !6)
103  !17 = !DILocation(line: 10, scope: !18)
104  !18 = distinct !DILexicalBlock(scope: !12, file: !1, line: 1, column: 1)
105  !19 = distinct !DILexicalBlock(scope: !12, file: !1, line: 1, column: 1)
106  !20 = !DILocation(line: 10, scope: !19)
107  !112 = distinct !DISubprogram(name: "test2", linkageName: "102", scope: !1, file: !1, line: 6, type: !13, scopeLine: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !115)
108  !115 = !{!116}
109  !116 = !DILocalVariable(name: "myVar", scope: !118, file: !1, line: 7, type: !6)
110  !117 = !DILocation(line: 10, scope: !118)
111  !118 = distinct !DILexicalBlock(scope: !112, file: !1, line: 1, column: 1)
112  !119 = distinct !DILexicalBlock(scope: !112, file: !1, line: 1, column: 1)
113  !120 = !DILocation(line: 10, scope: !119)
114
115...
116---
117name: _Z8bb_to_bb
118debugInstrRef: true
119debugValueSubstitutions:
120  - { srcinst: 4, srcop: 0, dstinst: 3, dstop: 0, subreg: 0 }
121body:  |
122  bb.0.entry:
123    successors: %bb.1, %bb.5, %bb.6
124
125    $rax = MOV64ri 1, debug-instr-number 1, debug-location !17
126    JCC_1 %bb.5, 1, implicit $eflags
127    JCC_1 %bb.6, 2, implicit $eflags
128    JMP_1 %bb.1
129
130  bb.1:
131    successors: %bb.2, %bb.3
132
133    DBG_VALUE $rax, $noreg, !16, !DIExpression(), debug-location !17
134    JCC_1 %bb.3, 1, implicit $eflags, debug-location !17
135
136  bb.2:
137    successors: %bb.4
138
139    JMP_1 %bb.4, debug-location !20
140
141  bb.3:
142    successors: %bb.4
143
144    JMP_1 %bb.4, debug-location !17
145
146  bb.4:
147    successors: %bb.5, %bb.6
148
149    JCC_1 %bb.5, 1, implicit $eflags, debug-location !17
150    JMP_1 %bb.6, debug-location !17
151
152  bb.5:
153    RET 0, debug-location !17
154
155  bb.6:
156    RET 0, debug-location !17
157...
158---
159name: test2
160debugValueSubstitutions:
161  - { srcinst: 4, srcop: 0, dstinst: 3, dstop: 0, subreg: 0 }
162body:  |
163  bb.0.entry:
164    successors: %bb.1, %bb.5, %bb.6
165
166    $rax = MOV64ri 1, debug-instr-number 1, debug-location !117
167    JCC_1 %bb.5, 1, implicit $eflags
168    JCC_1 %bb.6, 2, implicit $eflags
169    JMP_1 %bb.1
170
171  bb.1:
172    successors: %bb.2, %bb.3
173
174    DBG_VALUE $noreg, $noreg, !116, !DIExpression(), debug-location !117
175    JCC_1 %bb.3, 1, implicit $eflags, debug-location !117
176
177  bb.2:
178    successors: %bb.4
179
180    JMP_1 %bb.4, debug-location !120
181
182  bb.3:
183    successors: %bb.4
184
185    JMP_1 %bb.4, debug-location !117
186
187  bb.4:
188    successors: %bb.5, %bb.6
189
190    JCC_1 %bb.5, 1, implicit $eflags, debug-location !117
191    JMP_1 %bb.6, debug-location !117
192
193  bb.5:
194    RET 0, debug-location !117
195
196  bb.6:
197    RET 0, debug-location !117
198