1# RUN: llc -mtriple=x86_64-unknown-unknown -run-pass=machine-sink -o - %s | FileCheck %s
2# In the first test, check various things when we sink machine instructions:
3#   a) DBG_VALUEs should sink with defs
4#   b) Undefs should be left behind
5#   c) DBG_VALUEs not immediately following the defining inst should sink too
6#   d) If we generate debug-use-before-defs through sinking, and can copy
7#      propagate to a different value, we should do that.
8#
9# Test two checks DBG_VALUEs do not sink past a DBG_VALUE in the same block
10# that redefines the variable location, while three checks that copy-propagation
11# happens in the same scenario.
12--- |
13  target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
14  target triple = "x86_64-unknown-linux-gnu"
15
16  @x = common local_unnamed_addr global i32 0, align 4, !dbg !0
17
18  ; Function Attrs: noreturn nounwind uwtable
19  define void @Process(i32* nocapture readonly %p) local_unnamed_addr !dbg !9 {
20  ; Stripped
21  entry:
22    br label %nou
23  nou:
24    br label %exit
25  exit:
26    ret void
27  }
28
29  define void @test2(i32* nocapture readonly %p) local_unnamed_addr !dbg !101 {
30  ; Stripped
31  entry:
32    br label %block1
33  block1:
34    br label %exit
35  exit:
36    ret void
37  }
38
39  define void @test3(i32* nocapture readonly %p) local_unnamed_addr !dbg !201 {
40  ; Stripped
41  entry:
42    br label %block1
43  block1:
44    br label %exit
45  exit:
46    ret void
47  }
48
49  ; Function Attrs: nounwind readnone
50  declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
51
52  !llvm.dbg.cu = !{!1}
53  !llvm.module.flags = !{!6, !7}
54  !llvm.ident = !{!8}
55
56  !0 = !DIGlobalVariableExpression(var: !DIGlobalVariable(name: "x", scope: !1, file: !2, line: 1, type: !5, isLocal: false, isDefinition: true), expr: !DIExpression())
57  !1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !3, globals: !4)
58  !2 = !DIFile(filename: "t.c", directory: "")
59  !3 = !{}
60  !4 = !{!0}
61  !5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
62  !6 = !{i32 2, !"Dwarf Version", i32 4}
63  !7 = !{i32 2, !"Debug Info Version", i32 3}
64  !8 = !{!"clang version 4.0.0 "}
65  !9 = distinct !DISubprogram(name: "Process", scope: !2, file: !2, line: 2, type: !10, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !1, retainedNodes: !15)
66  !10 = !DISubroutineType(types: !11)
67  !11 = !{null, !12}
68  !12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64)
69  !13 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !14)
70  !14 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
71  !15 = !{!16}
72  !16 = !DILocalVariable(name: "p", arg: 1, scope: !9, file: !2, line: 2, type: !12)
73  !17 = !DIExpression()
74  !18 = !DILocation(line: 2, column: 34, scope: !9)
75  !28 = !DILexicalBlockFile(scope: !9, file: !2, discriminator: 1)
76  !101 = distinct !DISubprogram(name: "test2", scope: !2, file: !2, line: 2, type: !10, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !1, retainedNodes: !102)
77  !102 = !{!103}
78  !103 = !DILocalVariable(name: "q", arg: 1, scope: !101, file: !2, line: 2, type: !12)
79  !104 = !DILocation(line: 1, column: 1, scope: !101)
80  !201 = distinct !DISubprogram(name: "test2", scope: !2, file: !2, line: 2, type: !10, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !1, retainedNodes: !202)
81  !202 = !{!203}
82  !203 = !DILocalVariable(name: "r", arg: 1, scope: !201, file: !2, line: 2, type: !12)
83  !204 = !DILocation(line: 1, column: 1, scope: !201)
84
85  ; CHECK: [[VARNUM:![0-9]+]] = !DILocalVariable(name: "p",
86  ; CHECK: [[VAR2NUM:![0-9]+]] = !DILocalVariable(name: "q",
87  ; CHECK: [[VAR3NUM:![0-9]+]] = !DILocalVariable(name: "r",
88
89...
90---
91name:            Process
92tracksRegLiveness: true
93liveins:
94  - { reg: '$rdi', virtual-reg: '%2' }
95  - { reg: '$rsi', virtual-reg: '%2' }
96body:             |
97  bb.0.entry:
98    successors: %bb.1.nou(0x80000000), %bb.2.exit
99    liveins: $rdi, $esi
100
101    ; This block should have the vreg copy sunk from it, the DBG_VALUE with it,
102    ; and a copy-prop'd DBG_VALUE left over.
103    ; CHECK-LABEL: bb.0.entry:
104    ; CHECK:       [[ARG0VREG:%[0-9]+]]:gr64 = COPY $rdi
105    ; CHECK-NEXT:  CMP32ri $esi, 0
106    ; CHECK-NEXT:  DBG_VALUE [[ARG0VREG]], $noreg, [[VARNUM]]
107    ; CHECK-NEXT:  JCC_1 %bb.1, 4
108    ; CHECK-NEXT:  JMP_1
109
110    %2:gr64 = COPY $rdi
111    %5:gr64 = COPY %2
112    CMP32ri $esi, 0, implicit-def $eflags
113    DBG_VALUE %5, $noreg, !16, !17, debug-location !18
114    JCC_1 %bb.1.nou, 4, implicit $eflags
115    JMP_1 %bb.2.exit
116
117  bb.1.nou:
118    successors: %bb.2.exit(0x80000000)
119
120    ; This block should receive the sunk copy and DBG_VALUE
121    ; CHECK-LABEL: bb.1.nou:
122    ; CHECK:       [[SUNKVREG:%[0-9]+]]:gr64 = COPY [[ARG0VREG]]
123    ; CHECK-NEXT:  DBG_VALUE [[SUNKVREG]], $noreg, [[VARNUM]]
124    ; CHECK-NEXT:  ADD64ri8
125    ; CHECK-NEXT:  JMP_1
126    %1:gr64 = ADD64ri8 %5, 4, implicit-def dead $eflags
127    JMP_1 %bb.2.exit
128
129  bb.2.exit:
130    ; The DBG_VALUE below should have its operand copy-propagated after
131    ; the copy to %5 is sunk.
132    ; CHECK-LABEL: bb.2.exit:
133    ; CHECK:       DBG_VALUE [[ARG0VREG]], $noreg, [[VARNUM]]
134    ; CHECK-NEXT:  $rax = MOV64rr [[ARG0VREG]]
135    ; CHECK-NEXT:  RET 0
136    DBG_VALUE %5, _, !16, !17, debug-location !18
137    $rax = MOV64rr %2
138    RET 0
139...
140---
141name:            test2
142tracksRegLiveness: true
143liveins:
144  - { reg: '$rdi', virtual-reg: '%2' }
145  - { reg: '$rsi', virtual-reg: '%2' }
146body:             |
147  bb.0.entry:
148    successors: %bb.1.block1, %bb.2.exit
149    liveins: $rdi, $esi
150
151    ; This block should _not_ have the first DBG_VALUE sunk out from it, as it
152    ; would pass a later DBG_VALUE of the same variable location. An undef
153    ; DBG_VALUE should be left behind though.
154    ; CHECK-LABEL: bb.0.entry:
155    ; CHECK:       [[TEST2VREG:%[0-9]+]]:gr64 = COPY $rdi
156    ; CHECK-NEXT:  CMP32ri $esi, 0
157    ; CHECK-NEXT:  DBG_VALUE $noreg, $noreg, [[VAR2NUM]]
158    ; CHECK-NEXT:  CMP32ri $esi, 0
159    ; CHECK-NEXT:  DBG_VALUE 0, $noreg, [[VAR2NUM]]
160    ; CHECK-NEXT:  JCC_1 %bb.1, 4
161    ; CHECK-NEXT:  JMP_1
162
163    %2:gr64 = COPY $rdi
164    %5:gr64 = ADD64ri8 %2, 1, implicit-def dead $eflags
165    CMP32ri $esi, 0, implicit-def $eflags
166    DBG_VALUE %5, $noreg, !103, !17, debug-location !104
167    CMP32ri $esi, 0, implicit-def $eflags
168    DBG_VALUE 0, $noreg, !103, !17, debug-location !104
169    JCC_1 %bb.1.block1, 4, implicit $eflags
170    JMP_1 %bb.2.exit
171
172  bb.1.block1:
173    successors: %bb.2.exit
174
175    ; This block should receive no DBG_VALUE.
176    ; CHECK-LABEL: bb.1.block1:
177    ; CHECK-NOT:   DBG_VALUE
178    ; CHECK:       [[SUNKVREG2:%[0-9]+]]:gr64 = ADD64ri8 [[TEST2VREG]]
179    ; CHECK-NOT:   DBG_VALUE
180    ; CHECK-NEXT:  ADD64ri8
181    ; CHECK: JMP_1 %bb.2
182    %1:gr64 = ADD64ri8 %5, 4, implicit-def dead $eflags
183    JMP_1 %bb.2.exit
184
185  bb.2.exit:
186    $rax = MOV64rr %2
187    RET 0
188...
189---
190name:            test3
191tracksRegLiveness: true
192liveins:
193  - { reg: '$rdi', virtual-reg: '%2' }
194  - { reg: '$rsi', virtual-reg: '%2' }
195body:             |
196  bb.0.entry:
197    successors: %bb.1.block1, %bb.2.exit
198    liveins: $rdi, $esi
199
200    ; The copy from %2 to %5 will sink into a later block, and the first
201    ; DBG_VALUE cannot pass the second. But, we should be able to recover
202    ; the variable location through copy propagation.
203    ; CHECK-LABEL: bb.0.entry:
204    ; CHECK:       [[TEST3VREG:%[0-9]+]]:gr64 = COPY $rdi
205    ; CHECK-NEXT:  CMP32ri $esi, 0
206    ; CHECK-NEXT:  DBG_VALUE [[TEST3VREG]], $noreg, [[VAR3NUM]]
207    ; CHECK-NEXT:  CMP32ri $esi, 0
208    ; CHECK-NEXT:  DBG_VALUE 0, $noreg, [[VAR3NUM]]
209    ; CHECK-NEXT:  JCC_1 %bb.1, 4
210    ; CHECK-NEXT:  JMP_1
211
212    %2:gr64 = COPY $rdi
213    %5:gr64 = COPY %2
214    CMP32ri $esi, 0, implicit-def $eflags
215    DBG_VALUE %5, $noreg, !203, !17, debug-location !204
216    CMP32ri $esi, 0, implicit-def $eflags
217    DBG_VALUE 0, $noreg, !203, !17, debug-location !204
218    JCC_1 %bb.1.block1, 4, implicit $eflags
219    JMP_1 %bb.2.exit
220
221  bb.1.block1:
222    successors: %bb.2.exit
223
224    ; This block should receive no DBG_VALUE.
225    ; CHECK-LABEL: bb.1.block1:
226    ; CHECK-NOT:   DBG_VALUE
227    ; CHECK:       COPY [[TEST3VREG]]
228    ; CHECK-NOT:   DBG_VALUE
229    ; CHECK-NEXT:  ADD64ri8
230    ; CHECK: JMP_1 %bb.2
231    %1:gr64 = ADD64ri8 %5, 4, implicit-def dead $eflags
232    JMP_1 %bb.2.exit
233
234  bb.2.exit:
235    $rax = MOV64rr %2
236    RET 0
237...
238