xref: /llvm-project/llvm/test/CodeGen/X86/sink-local-value.ll (revision 10b03e66629aedad79a804e22d23b575077303b3)
1; RUN: llc -O0 < %s | FileCheck %s
2; RUN: llc --try-experimental-debuginfo-iterators -O0 < %s | FileCheck %s
3
4target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
5target triple = "i386-linux-gnu"
6
7; Try some simple cases that show how local value sinking improves line tables.
8
9@sink_across = external dso_local global i32
10
11declare void @simple_callee(i32, i32)
12
13define void @simple() !dbg !5 {
14  store i32 44, ptr @sink_across, !dbg !7
15  call void @simple_callee(i32 13, i32 55), !dbg !8
16  ret void, !dbg !9
17}
18
19; CHECK-LABEL: simple:
20; CHECK-NOT: movl $13,
21; CHECK: .loc 1 1 1 prologue_end
22; CHECK: movl $44, sink_across
23; CHECK: .loc 1 2 1
24; CHECK: movl $13,
25; CHECK: movl $55,
26; CHECK: calll simple_callee
27
28declare void @simple_reg_callee(i32 inreg, i32 inreg)
29
30define void @simple_reg() !dbg !10 {
31  store i32 44, ptr @sink_across, !dbg !11
32  call void @simple_reg_callee(i32 inreg 13, i32 inreg 55), !dbg !12
33  ret void, !dbg !13
34}
35
36; CHECK-LABEL: simple_reg:
37; CHECK: .loc 1 4 1 prologue_end
38; CHECK: movl $44, sink_across
39; CHECK: .loc 1 5 1
40; CHECK: movl $13,
41; CHECK: movl $55,
42; CHECK: calll simple_reg_callee
43
44; There are two interesting cases where local values have no uses but are not
45; dead: when the local value is directly used by a phi, and when the local
46; value is used by a no-op cast instruction. In these cases, we get side tables
47; referring to the local value vreg that we need to check.
48
49define ptr @phi_const(i32 %c) !dbg !14 {
50entry:
51  %tobool = icmp eq i32 %c, 0, !dbg !20
52  call void @llvm.dbg.value(metadata i1 %tobool, metadata !16, metadata !DIExpression()), !dbg !20
53  br i1 %tobool, label %if.else, label %if.then, !dbg !21
54
55if.then:                                          ; preds = %entry
56  br label %if.end, !dbg !22
57
58if.else:                                          ; preds = %entry
59  br label %if.end, !dbg !23
60
61if.end:                                           ; preds = %if.else, %if.then
62  %r.0 = phi ptr [ inttoptr (i32 42 to ptr), %if.then ], [ inttoptr (i32 1 to ptr), %if.else ], !dbg !24
63  call void @llvm.dbg.value(metadata ptr %r.0, metadata !18, metadata !DIExpression()), !dbg !24
64  ret ptr %r.0, !dbg !25
65}
66
67; CHECK-LABEL: phi_const:
68; CHECK:                                 # %entry
69; CHECK: cmpl    $0,
70; CHECK:                                 # %if.then
71; CHECK: movl    $42,
72; CHECK: jmp
73; CHECK:                                 # %if.else
74; CHECK: movl    $1,
75; CHECK:                                 # %if.end
76
77define ptr @phi_const_cast(i32 %c) !dbg !26 {
78entry:
79  %tobool = icmp eq i32 %c, 0, !dbg !32
80  call void @llvm.dbg.value(metadata i1 %tobool, metadata !28, metadata !DIExpression()), !dbg !32
81  br i1 %tobool, label %if.else, label %if.then, !dbg !33
82
83if.then:                                          ; preds = %entry
84  %v42 = inttoptr i32 42 to ptr, !dbg !34
85  call void @llvm.dbg.value(metadata ptr %v42, metadata !29, metadata !DIExpression()), !dbg !34
86  br label %if.end, !dbg !35
87
88if.else:                                          ; preds = %entry
89  %v1 = inttoptr i32 1 to ptr, !dbg !36
90  call void @llvm.dbg.value(metadata ptr %v1, metadata !30, metadata !DIExpression()), !dbg !36
91  br label %if.end, !dbg !37
92
93if.end:                                           ; preds = %if.else, %if.then
94  %r.0 = phi ptr [ %v42, %if.then ], [ %v1, %if.else ], !dbg !38
95  call void @llvm.dbg.value(metadata ptr %r.0, metadata !31, metadata !DIExpression()), !dbg !38
96  ret ptr %r.0, !dbg !39
97}
98
99; CHECK-LABEL: phi_const_cast:
100; CHECK:                                 # %entry
101; CHECK: cmpl    $0,
102; CHECK:                                 # %if.then
103; CHECK: movl    $42, %[[REG:[a-z]+]]
104; CHECK: #DEBUG_VALUE: phi_const_cast:4 <- $[[REG]]
105; CHECK: jmp
106; CHECK:                                 # %if.else
107; CHECK: movl    $1, %[[REG:[a-z]+]]
108; CHECK: #DEBUG_VALUE: phi_const_cast:5 <- $[[REG]]
109; CHECK:                                 # %if.end
110
111declare void @may_throw() local_unnamed_addr #1
112
113declare i32 @__gxx_personality_v0(...)
114
115define i32 @invoke_phi() personality ptr @__gxx_personality_v0 {
116entry:
117  store i32 42, ptr @sink_across
118  invoke void @may_throw()
119          to label %try.cont unwind label %lpad
120
121lpad:                                             ; preds = %entry
122  %0 = landingpad { ptr, i32 }
123          catch ptr null
124  store i32 42, ptr @sink_across
125  br label %try.cont
126
127try.cont:                                         ; preds = %entry, %lpad
128  %r.0 = phi i32 [ 13, %entry ], [ 55, %lpad ]
129  ret i32 %r.0
130}
131
132; The constant materialization should be *after* the stores to sink_across, but
133; before any EH_LABEL.
134
135; CHECK-LABEL: invoke_phi:
136; CHECK:         movl    $42, sink_across
137; CHECK:         movl    $13, %{{[a-z]*}}
138; CHECK: .Ltmp{{.*}}:
139; CHECK:         calll   may_throw
140; CHECK: .Ltmp{{.*}}:
141; CHECK:         jmp     .LBB{{.*}}
142; CHECK: .LBB{{.*}}:                                # %lpad
143; CHECK:         movl    $42, sink_across
144; CHECK:         movl    $55, %{{[a-z]*}}
145; CHECK: .LBB{{.*}}:                                # %try.cont
146; CHECK:         retl
147
148
149define i32 @lpad_phi() personality ptr @__gxx_personality_v0 {
150entry:
151  store i32 42, ptr @sink_across
152  invoke void @may_throw()
153          to label %try.cont unwind label %lpad
154
155lpad:                                             ; preds = %entry
156  %p = phi i32 [ 11, %entry ]  ; Trivial, but -O0 keeps it
157  %0 = landingpad { ptr, i32 }
158          catch ptr null
159  store i32 %p, ptr @sink_across
160  br label %try.cont
161
162try.cont:                                         ; preds = %entry, %lpad
163  %r.0 = phi i32 [ 13, %entry ], [ 55, %lpad ]
164  ret i32 %r.0
165}
166
167; The constant materialization should be *after* the stores to sink_across, but
168; before any EH_LABEL.
169
170; CHECK-LABEL: lpad_phi:
171; CHECK:         movl    $42, sink_across
172; CHECK:         movl    $13, %{{[a-z]*}}
173; CHECK: .Ltmp{{.*}}:
174; CHECK:         calll   may_throw
175; CHECK: .Ltmp{{.*}}:
176; CHECK:         jmp     .LBB{{.*}}
177; CHECK: .LBB{{.*}}:                                # %lpad
178; CHECK-NEXT: .Ltmp{{.*}}:
179; CHECK:         movl    {{.*}}, sink_across
180; CHECK:         movl    $55, %{{[a-z]*}}
181; CHECK: .LBB{{.*}}:                                # %try.cont
182; CHECK:         retl
183
184
185; Function Attrs: nounwind readnone speculatable
186declare void @llvm.dbg.value(metadata, metadata, metadata) #0
187
188attributes #0 = { nounwind readnone speculatable }
189
190!llvm.dbg.cu = !{!0}
191!llvm.debugify = !{!3, !4}
192!llvm.module.flags = !{!52, !53}
193
194!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
195!1 = !DIFile(filename: "../llvm/test/CodeGen/X86/sink-local-value.ll", directory: "/")
196!2 = !{}
197!3 = !{i32 27}
198!4 = !{i32 8}
199!5 = distinct !DISubprogram(name: "simple", linkageName: "simple", scope: null, file: !1, line: 1, type: !6, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !2)
200!6 = !DISubroutineType(types: !2)
201!7 = !DILocation(line: 1, column: 1, scope: !5)
202!8 = !DILocation(line: 2, column: 1, scope: !5)
203!9 = !DILocation(line: 3, column: 1, scope: !5)
204!10 = distinct !DISubprogram(name: "simple_reg", linkageName: "simple_reg", scope: null, file: !1, line: 4, type: !6, isLocal: false, isDefinition: true, scopeLine: 4, isOptimized: true, unit: !0, retainedNodes: !2)
205!11 = !DILocation(line: 4, column: 1, scope: !10)
206!12 = !DILocation(line: 5, column: 1, scope: !10)
207!13 = !DILocation(line: 6, column: 1, scope: !10)
208!14 = distinct !DISubprogram(name: "phi_const", linkageName: "phi_const", scope: null, file: !1, line: 7, type: !6, isLocal: false, isDefinition: true, scopeLine: 7, isOptimized: true, unit: !0, retainedNodes: !15)
209!15 = !{!16, !18}
210!16 = !DILocalVariable(name: "1", scope: !14, file: !1, line: 7, type: !17)
211!17 = !DIBasicType(name: "ty8", size: 8, encoding: DW_ATE_unsigned)
212!18 = !DILocalVariable(name: "2", scope: !14, file: !1, line: 11, type: !19)
213!19 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
214!20 = !DILocation(line: 7, column: 1, scope: !14)
215!21 = !DILocation(line: 8, column: 1, scope: !14)
216!22 = !DILocation(line: 9, column: 1, scope: !14)
217!23 = !DILocation(line: 10, column: 1, scope: !14)
218!24 = !DILocation(line: 11, column: 1, scope: !14)
219!25 = !DILocation(line: 12, column: 1, scope: !14)
220!26 = distinct !DISubprogram(name: "phi_const_cast", linkageName: "phi_const_cast", scope: null, file: !1, line: 13, type: !6, isLocal: false, isDefinition: true, scopeLine: 13, isOptimized: true, unit: !0, retainedNodes: !27)
221!27 = !{!28, !29, !30, !31}
222!28 = !DILocalVariable(name: "3", scope: !26, file: !1, line: 13, type: !17)
223!29 = !DILocalVariable(name: "4", scope: !26, file: !1, line: 15, type: !19)
224!30 = !DILocalVariable(name: "5", scope: !26, file: !1, line: 17, type: !19)
225!31 = !DILocalVariable(name: "6", scope: !26, file: !1, line: 19, type: !19)
226!32 = !DILocation(line: 13, column: 1, scope: !26)
227!33 = !DILocation(line: 14, column: 1, scope: !26)
228!34 = !DILocation(line: 15, column: 1, scope: !26)
229!35 = !DILocation(line: 16, column: 1, scope: !26)
230!36 = !DILocation(line: 17, column: 1, scope: !26)
231!37 = !DILocation(line: 18, column: 1, scope: !26)
232!38 = !DILocation(line: 19, column: 1, scope: !26)
233!39 = !DILocation(line: 20, column: 1, scope: !26)
234!40 = distinct !DISubprogram(name: "invoke_phi", linkageName: "invoke_phi", scope: null, file: !1, line: 21, type: !6, isLocal: false, isDefinition: true, scopeLine: 21, isOptimized: true, unit: !0, retainedNodes: !41)
235!41 = !{!42, !44}
236!42 = !DILocalVariable(name: "7", scope: !40, file: !1, line: 23, type: !43)
237!43 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
238!44 = !DILocalVariable(name: "8", scope: !40, file: !1, line: 26, type: !19)
239!45 = !DILocation(line: 21, column: 1, scope: !40)
240!46 = !DILocation(line: 22, column: 1, scope: !40)
241!47 = !DILocation(line: 23, column: 1, scope: !40)
242!48 = !DILocation(line: 24, column: 1, scope: !40)
243!49 = !DILocation(line: 25, column: 1, scope: !40)
244!50 = !DILocation(line: 26, column: 1, scope: !40)
245!51 = !DILocation(line: 27, column: 1, scope: !40)
246!52 = !{i32 2, !"Dwarf Version", i32 4}
247!53 = !{i32 2, !"Debug Info Version", i32 3}
248