xref: /llvm-project/llvm/test/Transforms/SampleProfile/indirect-call.ll (revision d38d6ca1798b339a8d292e91f8e0708c334769ff)
1; RUN: opt -S %s -passes=sample-profile -sample-profile-file=%S/Inputs/indirect-call.prof | FileCheck %s
2; RUN: opt -S %s -passes=sample-profile -sample-profile-file=%S/Inputs/indirect-call.extbinary.afdo | FileCheck %s
3
4; CHECK-LABEL: @test
5define void @test(ptr) #0 !dbg !3 {
6  %2 = alloca ptr
7  store ptr %0, ptr %2
8  %3 = load ptr, ptr %2
9  ; CHECK: call {{.*}}, !prof ![[PROF:[0-9]+]]
10  call void %3(), !dbg !4
11  ret void
12}
13
14; CHECK-LABEL: @test_inline
15; If the indirect call is promoted and inlined in profile, we should promote and inline it.
16define void @test_inline(ptr, ptr %x) #0 !dbg !6 {
17  %2 = alloca ptr
18  store ptr %0, ptr %2
19  %3 = load ptr, ptr %2
20; CHECK: icmp {{.*}} @foo_inline2
21; CHECK: br {{.*}} !prof ![[BR1:[0-9]+]]
22; CHECK: if.true.direct_targ:
23; CHECK-NOT: call
24; CHECK: if.false.orig_indirect:
25; CHECK: icmp {{.*}} @foo_inline1
26; CHECK: br {{.*}} !prof ![[BR2:[0-9]+]]
27; CHECK: if.true.direct_targ1:
28; CHECK-NOT: call
29; CHECK: if.false.orig_indirect2:
30; CHECK: call {{.*}} !prof ![[VP:[0-9]+]]
31  call ptr %3(ptr %x), !dbg !7
32  ret void
33}
34
35; CHECK-LABEL: @test_inline_strip
36; If the indirect call is promoted and inlined in profile, and the callee name
37; is stripped we should promote and inline it.
38define void @test_inline_strip(ptr, ptr %x) #0 !dbg !8 {
39  %2 = alloca ptr
40  store ptr %0, ptr %2
41  %3 = load ptr, ptr %2
42; CHECK: icmp {{.*}} @foo_inline_strip.suffix
43; CHECK: if.true.direct_targ:
44; CHECK-NOT: call
45; CHECK: if.false.orig_indirect:
46; CHECK: call
47  call ptr %3(ptr %x), !dbg !9
48  ret void
49}
50
51; CHECK-LABEL: @test_inline_strip_conflict
52; If the indirect call is promoted and inlined in profile, and the callee name
53; is stripped, but have more than 1 potential match, we should not promote.
54define void @test_inline_strip_conflict(ptr, ptr %x) #0 !dbg !10 {
55  %2 = alloca ptr
56  store ptr %0, ptr %2
57  %3 = load ptr, ptr %2
58; CHECK-NOT: if.true.direct_targ:
59  call ptr %3(ptr %x), !dbg !11
60  ret void
61}
62
63; CHECK-LABEL: @test_noinline
64; If the indirect call target is not available, we should not promote it.
65define void @test_noinline(ptr) #0 !dbg !12 {
66  %2 = alloca ptr
67  store ptr %0, ptr %2
68  %3 = load ptr, ptr %2
69; CHECK-NOT: icmp
70; CHECK: call
71  call void %3(), !dbg !13
72  ret void
73}
74
75; CHECK-LABEL: @test_noinline_bitcast
76; If the indirect call has been promoted to a direct call with bitcast,
77; do not inline it.
78define float @test_noinline_bitcast(ptr) #0 !dbg !26 {
79  %2 = alloca ptr
80  store ptr %0, ptr %2
81; CHECK: icmp
82; CHECK: call
83  %3 = load ptr, ptr %2
84  %4 = call float %3(), !dbg !27
85  ret float %4
86}
87
88; CHECK-LABEL: @test_norecursive_inline
89; If the indirect call target is the caller, we should not promote it.
90define void @test_norecursive_inline() #0 !dbg !24 {
91; CHECK-NOT: icmp
92; CHECK: call
93  %1 = load ptr, ptr @y, align 8
94  call void %1(), !dbg !25
95  ret void
96}
97
98define ptr @return_arg(ptr readnone returned) #0 !dbg !29{
99  ret ptr %0
100}
101
102; CHECK-LABEL: @return_arg_caller
103; When the promoted indirect call returns a parameter that was defined by the
104; return value of a previous direct call. Checks both direct call and promoted
105; indirect call are inlined.
106define ptr @return_arg_caller(ptr nocapture) #0 !dbg !30{
107; CHECK-NOT: call ptr @foo_inline1
108; CHECK: if.true.direct_targ:
109; CHECK-NOT: call
110; CHECK: if.false.orig_indirect:
111; CHECK: call
112  %2 = call ptr @foo_inline1(ptr null), !dbg !31
113  %cmp = icmp ne ptr %2, null
114  br i1 %cmp, label %then, label %else
115
116then:
117  %3 = tail call ptr %0(ptr %2), !dbg !32
118  ret ptr %3
119
120else:
121  ret ptr null
122}
123
124; CHECK-LABEL: @branch_prof_valid
125; Check the conditional branch generated by indirect call promotion won't
126; have invalid profile like !{!"branch_weights", i32 0, i32 0}.
127define void @branch_prof_valid(ptr %t0) #0 !dbg !33 {
128  %t1 = alloca ptr
129  store ptr %t0, ptr %t1
130  %t2 = load ptr, ptr %t1
131  ; CHECK-NOT: call {{.*}}
132  ; CHECK: br i1 {{.*}}, label %if.true.direct_targ, label %if.false.orig_indirect, {{.*}}, !prof ![[BR3:[0-9]+]]
133  call void %t2(), !dbg !34
134  ret void
135}
136
137@x = global i32 0, align 4
138@y = global ptr null, align 8
139
140define ptr @foo_inline1(ptr %x) #0 !dbg !14 {
141  ret ptr %x
142}
143
144define ptr @foo_inline_strip.suffix(ptr %x) #0 !dbg !15 {
145  ret ptr %x
146}
147
148define ptr @foo_inline_strip_conflict.suffix1(ptr %x) #0 !dbg !16 {
149  ret ptr %x
150}
151
152define ptr @foo_inline_strip_conflict.suffix2(ptr %x) #0 !dbg !17 {
153  ret ptr %x
154}
155
156define ptr @foo_inline_strip_conflict.suffix3(ptr %x) #0 !dbg !18 {
157  ret ptr %x
158}
159
160define ptr @foo_inline2(ptr %x) #0 !dbg !19 {
161  ret ptr %x
162}
163
164define void @foo_inline3() #0 !dbg !35 {
165  ret void
166}
167
168define i32 @foo_noinline(i32 %x) #0 !dbg !20 {
169  ret i32 %x
170}
171
172define void @foo_direct() #0 !dbg !21 {
173  ret void
174}
175
176define i32 @foo_direct_i32() #0 !dbg !28 {
177  ret i32 0;
178}
179
180; CHECK-LABEL: @test_direct
181; We should not promote a direct call.
182define void @test_direct() #0 !dbg !22 {
183; CHECK-NOT: icmp
184; CHECK: call
185  call void @foo_alias(), !dbg !23
186  ret void
187}
188
189@foo_alias = alias void (), ptr @foo_direct
190
191attributes #0 = {"use-sample-profile"}
192
193!llvm.dbg.cu = !{!0}
194!llvm.module.flags = !{!2}
195
196!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1)
197!1 = !DIFile(filename: "test.cc", directory: "/")
198!2 = !{i32 2, !"Debug Info Version", i32 3}
199!3 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 3, unit: !0)
200!4 = !DILocation(line: 4, scope: !3)
201!5 = !DILocation(line: 6, scope: !3)
202; CHECK: ![[PROF]] = !{!"VP", i32 0, i64 3457, i64 9191153033785521275, i64 2059, i64 -1069303473483922844, i64 1398}
203; CHECK: ![[BR1]] = !{!"branch_weights", i32 4000, i32 4000}
204; CHECK: ![[BR2]] = !{!"branch_weights", i32 3000, i32 1000}
205; CHECK: ![[VP]] = !{!"VP", i32 0, i64 8000, i64 7476224446746900038, i64 -1, i64 925324185419832389, i64 -1, i64 -6391416044382067764, i64 1000}
206; CHECK: ![[BR3]] = !{!"branch_weights", i32 1, i32 0}
207!6 = distinct !DISubprogram(name: "test_inline", scope: !1, file: !1, line: 6, unit: !0)
208!7 = !DILocation(line: 7, scope: !6)
209!8 = distinct !DISubprogram(name: "test_inline_strip", scope: !1, file: !1, line: 8, unit: !0)
210!9 = !DILocation(line: 9, scope: !8)
211!10 = distinct !DISubprogram(name: "test_inline_strip_conflict", scope: !1, file: !1, line: 10, unit: !0)
212!11 = !DILocation(line: 11, scope: !10)
213!12 = distinct !DISubprogram(name: "test_noinline", scope: !1, file: !1, line: 12, unit: !0)
214!13 = !DILocation(line: 13, scope: !12)
215!14 = distinct !DISubprogram(name: "foo_inline1", scope: !1, file: !1, line: 11, unit: !0)
216!15 = distinct !DISubprogram(name: "foo_inline_strip.suffix", scope: !1, file: !1, line: 1, unit: !0)
217!16 = distinct !DISubprogram(name: "foo_inline_strip_conflict.suffix1", scope: !1, file: !1, line: 1, unit: !0)
218!17 = distinct !DISubprogram(name: "foo_inline_strip_conflict.suffix2", scope: !1, file: !1, line: 1, unit: !0)
219!18 = distinct !DISubprogram(name: "foo_inline_strip_conflict.suffix3", scope: !1, file: !1, line: 1, unit: !0)
220!19 = distinct !DISubprogram(name: "foo_inline2", scope: !1, file: !1, line: 19, unit: !0)
221!20 = distinct !DISubprogram(name: "foo_noinline", scope: !1, file: !1, line: 20, unit: !0)
222!21 = distinct !DISubprogram(name: "foo_direct", scope: !1, file: !1, line: 21, unit: !0)
223!22 = distinct !DISubprogram(name: "test_direct", scope: !1, file: !1, line: 22, unit: !0)
224!23 = !DILocation(line: 23, scope: !22)
225!24 = distinct !DISubprogram(name: "test_norecursive_inline", scope: !1, file: !1, line: 12, unit: !0)
226!25 = !DILocation(line: 13, scope: !24)
227!26 = distinct !DISubprogram(name: "test_noinline_bitcast", scope: !1, file: !1, line: 12, unit: !0)
228!27 = !DILocation(line: 13, scope: !26)
229!28 = distinct !DISubprogram(name: "foo_direct_i32", scope: !1, file: !1, line: 11, unit: !0)
230!29 = distinct !DISubprogram(name: "return_arg", scope: !1, file: !1, line: 11, unit: !0)
231!30 = distinct !DISubprogram(name: "return_arg_caller", scope: !1, file: !1, line: 11, unit: !0)
232!31 = !DILocation(line: 12, scope: !30)
233!32 = !DILocation(line: 13, scope: !30)
234!33 = distinct !DISubprogram(name: "branch_prof_valid", scope: !1, file: !1, line: 25, unit: !0)
235!34 = !DILocation(line: 27, scope: !33)
236!35 = distinct !DISubprogram(name: "foo_inline3", scope: !1, file: !1, line: 29, unit: !0)
237