1; RUN: opt < %s -passes='require<profile-summary>,cgscc(inline)' -S -inline-threshold=50 | FileCheck %s
2
3; This tests that the function count of a function gets properly scaled after
4; inlining a call chain leading to the function.
5; Function a calls c with count 200 (C1)
6; Function c calls e with count 250 (C2)
7; Entry count of e is 500 (C3)
8; Entry count of c is 500 (C4)
9; Function b calls c with count 300 (C5)
10; c->e inlining does not happen since the cost exceeds threshold.
11; c then inlined into a.
12; e now gets inlined into a (through c) since the branch condition in e is now
13; known and hence the cost gets reduced.
14; Estimated count of a->e callsite = C2 * (C1 / C4)
15; Estimated count of a->e callsite = 250 * (200 / 500) = 100
16; Remaining count of e = C3 - 100 = 500 - 100 = 400
17; Remaining count of c = C4 - C1 - C5 = 500 - 200 - 300 = 0
18
19@data = external global i32
20
21define i32 @a(i32 %a1) !prof !1 {
22  %a2 = call i32 @c(i32 %a1, i32 1)
23  ret i32 %a2
24}
25
26define i32 @b(i32 %b1) !prof !2 {
27  %b2 = call i32 @c(i32 %b1, i32 %b1)
28  ret i32 %b2
29}
30
31declare void @ext();
32
33; CHECK: @c(i32 %c1, i32 %c100) !prof [[COUNT1:![0-9]+]]
34define i32 @c(i32 %c1, i32 %c100) !prof !3 {
35  call void @ext()
36  %cond = icmp sle i32 %c1, 1
37  br i1 %cond, label %cond_true, label %cond_false
38
39cond_false:
40  ret i32 0
41
42cond_true:
43  %c11 = call i32 @e(i32 %c100)
44  ret i32 %c11
45}
46
47
48; CHECK: @e(i32 %c1) !prof [[COUNT2:![0-9]+]]
49define i32 @e(i32 %c1) !prof !4 {
50  %cond = icmp sle i32 %c1, 1
51  br i1 %cond, label %cond_true, label %cond_false
52
53cond_false:
54  call void @ext()
55  %c2 = load i32, i32* @data, align 4
56  %c3 = add i32 %c1, %c2
57  %c4 = mul i32 %c3, %c2
58  %c5 = add i32 %c4, %c2
59  %c6 = mul i32 %c5, %c2
60  %c7 = add i32 %c6, %c2
61  %c8 = mul i32 %c7, %c2
62  %c9 = add i32 %c8, %c2
63  %c10 = mul i32 %c9, %c2
64  ret i32 %c10
65
66cond_true:
67  ret i32 0
68}
69
70!llvm.module.flags = !{!0}
71; CHECK: [[COUNT1]] = !{!"function_entry_count", i64 0}
72; CHECK: [[COUNT2]] = !{!"function_entry_count", i64 400}
73!0 = !{i32 1, !"MaxFunctionCount", i32 5000}
74!1 = !{!"function_entry_count", i64 200}
75!2 = !{!"function_entry_count", i64 300}
76!3 = !{!"function_entry_count", i64 500}
77!4 = !{!"function_entry_count", i64 500}
78
79