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