1; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profsampleacc.extbinary.afdo -profile-summary-cutoff-hot=900000 -profile-sample-accurate -S | FileCheck %s --check-prefix=CALL_SUM_IS_HOT
2; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profsampleacc.extbinary.afdo -profile-summary-cutoff-hot=900000 -profile-sample-accurate -S | FileCheck %s --check-prefix=CALL_SUM_IS_HOT
3
4; RUN: llvm-profdata merge -sample -extbinary -prof-sym-list=%S/Inputs/profile-symbol-list.text %S/Inputs/profsampleacc.extbinary.afdo -o %t.symlist.afdo
5; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-summary-cutoff-hot=600000 -profile-accurate-for-symsinlist -S | FileCheck %s --check-prefix=PROFSYMLIST
6; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-accurate-for-symsinlist -profile-symbol-list-cutoff=2 -S | FileCheck %s --check-prefix=PSLCUTOFF2
7; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-accurate-for-symsinlist -profile-symbol-list-cutoff=3 -S | FileCheck %s --check-prefix=PSLCUTOFF3
8;
9; If -profile-accurate-for-symsinlist and -profile-sample-accurate both present,
10; -profile-sample-accurate will override -profile-accurate-for-symsinlist.
11; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profsampleacc.extbinary.afdo -profile-summary-cutoff-hot=900000 -profile-sample-accurate -profile-accurate-for-symsinlist -S | FileCheck %s --check-prefix=CALL_SUM_IS_HOT
12; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/profsampleacc.extbinary.afdo -profile-summary-cutoff-hot=900000 -profile-sample-accurate -profile-accurate-for-symsinlist -S | FileCheck %s --check-prefix=CALL_SUM_IS_HOT
13;
14; Original C++ test case
15;
16; #include <stdio.h>
17;
18; int sum(int x, int y) {
19;   return x + y;
20; }
21;
22; int main() {
23;   int s, i = 0;
24;   while (i++ < 20000 * 20000)
25;     if (i != 100) s = sum(i, s); else s = 30;
26;   printf("sum is %d\n", s);
27;   return 0;
28; }
29;
30@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
31
32; Check _Z3sumii's function entry count will be 0 when
33; profile-sample-accurate is enabled.
34; CALL_SUM_IS_HOT: define i32 @_Z3sumii{{.*}}!prof ![[ZERO_ID:[0-9]+]]
35;
36; Check _Z3sumii's function entry count will be nonzero when
37; profile-sample-accurate is enabled because the callsite is warm and not
38; inlined so its function entry count is adjusted to nonzero.
39; CALL_SUM_IS_WARM: define i32 @_Z3sumii{{.*}}!prof ![[NONZERO_ID:[0-9]+]]
40;
41; Check _Z3sumii's function entry count will be initialized to -1 when
42; profile-accurate-for-profsymlist is enabled and _Z3sumii exists in the
43; profile symbol list because it also shows up in the profile as inline
44; instance.
45; PROFSYMLIST: define i32 @_Z3sumii{{.*}}!prof ![[UNKNOWN_ID:[0-9]+]]
46;
47; Function Attrs: nounwind uwtable
48define i32 @_Z3sumii(i32 %x, i32 %y) #0 !dbg !4 {
49entry:
50  %x.addr = alloca i32, align 4
51  %y.addr = alloca i32, align 4
52  store i32 %x, ptr %x.addr, align 4
53  store i32 %y, ptr %y.addr, align 4
54  %0 = load i32, ptr %x.addr, align 4, !dbg !11
55  %1 = load i32, ptr %y.addr, align 4, !dbg !11
56  %add = add nsw i32 %0, %1, !dbg !11
57  ret i32 %add, !dbg !11
58}
59
60; Check -profile-symbol-list-cutoff=3 will include _Z3toov into profile
61; symbol list and -profile-symbol-list-cutoff=2 will not.
62; PSLCUTOFF2: define i32 @_Z3toov{{.*}}!prof ![[TOO_ID:[0-9]+]]
63; PSLCUTOFF3: define i32 @_Z3toov{{.*}}!prof ![[TOO_ID:[0-9]+]]
64define i32 @_Z3toov(i32 %x, i32 %y) #0 {
65entry:
66  %add = add nsw i32 %x, %y
67  ret i32 %add
68}
69
70; Function Attrs: uwtable
71define i32 @main() #0 !dbg !7 {
72entry:
73  %retval = alloca i32, align 4
74  %s = alloca i32, align 4
75  %i = alloca i32, align 4
76  store i32 0, ptr %retval
77  store i32 0, ptr %i, align 4, !dbg !12
78  br label %while.cond, !dbg !13
79
80while.cond:                                       ; preds = %if.end, %entry
81  %0 = load i32, ptr %i, align 4, !dbg !14
82  %inc = add nsw i32 %0, 1, !dbg !14
83  store i32 %inc, ptr %i, align 4, !dbg !14
84  %cmp = icmp slt i32 %0, 400000000, !dbg !14
85  br i1 %cmp, label %while.body, label %while.end, !dbg !14
86
87while.body:                                       ; preds = %while.cond
88  %1 = load i32, ptr %i, align 4, !dbg !16
89  %cmp1 = icmp ne i32 %1, 100, !dbg !16
90  br i1 %cmp1, label %if.then, label %if.else, !dbg !16
91
92; With the hot cutoff being set to 600000, the inline instance of _Z3sumii
93; in main is neither hot nor cold. Check it won't be inlined when
94; profile-sample-accurate is enabled.
95; CALL_SUM_IS_WARM: if.then:
96; CALL_SUM_IS_WARM: call i32 @_Z3sumii
97; CALL_SUM_IS_WARM: if.else:
98;
99; With the hot cutoff being set to 900000, the inline instance of _Z3sumii
100; in main is hot. Check the callsite of _Z3sumii will be inlined when
101; profile-sample-accurate is enabled.
102; CALL_SUM_IS_HOT: if.then:
103; CALL_SUM_IS_HOT-NOT: call i32 @_Z3sumii
104; CALL_SUM_IS_HOT: if.else:
105;
106; Check _Z3sumii will be inlined when profile-accurate-for-profsymlist is
107; enabled
108; PROFSYMLIST: if.then:
109; PROFSYMLIST-NOT: call i32 @_Z3sumii
110; PROFSYMLIST: if.else:
111if.then:                                          ; preds = %while.body
112  %2 = load i32, ptr %i, align 4, !dbg !18
113  %3 = load i32, ptr %s, align 4, !dbg !18
114  %call = call i32 @_Z3sumii(i32 %2, i32 %3), !dbg !18
115  store i32 %call, ptr %s, align 4, !dbg !18
116  br label %if.end, !dbg !18
117
118if.else:                                          ; preds = %while.body
119  store i32 30, ptr %s, align 4, !dbg !20
120  br label %if.end
121
122if.end:                                           ; preds = %if.else, %if.then
123  br label %while.cond, !dbg !22
124
125while.end:                                        ; preds = %while.cond
126  %4 = load i32, ptr %s, align 4, !dbg !24
127  %call2 = call i32 (ptr, ...) @printf(ptr @.str, i32 %4), !dbg !24
128  ret i32 0, !dbg !25
129}
130
131declare i32 @printf(ptr, ...) #2
132
133attributes #0 = { "use-sample-profile" }
134
135!llvm.dbg.cu = !{!0}
136!llvm.module.flags = !{!8, !9}
137!llvm.ident = !{!10}
138
139; CALL_SUM_IS_HOT: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
140; CALL_SUM_IS_WARM: ![[NONZERO_ID]] = !{!"function_entry_count", i64 5179}
141; PROFSYMLIST: ![[UNKNOWN_ID]] = !{!"function_entry_count", i64 -1}
142; PSLCUTOFF2: ![[TOO_ID]] = !{!"function_entry_count", i64 -1}
143; PSLCUTOFF3: ![[TOO_ID]] = !{!"function_entry_count", i64 0}
144!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
145!1 = !DIFile(filename: "calls.cc", directory: ".")
146!2 = !{}
147!4 = distinct !DISubprogram(name: "sum", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 3, file: !1, scope: !5, type: !6, retainedNodes: !2)
148!5 = !DIFile(filename: "calls.cc", directory: ".")
149!6 = !DISubroutineType(types: !2)
150!7 = distinct !DISubprogram(name: "main", line: 7, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 7, file: !1, scope: !5, type: !6, retainedNodes: !2)
151!8 = !{i32 2, !"Dwarf Version", i32 4}
152!9 = !{i32 1, !"Debug Info Version", i32 3}
153!10 = !{!"clang version 3.5 "}
154!11 = !DILocation(line: 4, scope: !4)
155!12 = !DILocation(line: 8, scope: !7)
156!13 = !DILocation(line: 9, scope: !7)
157!14 = !DILocation(line: 9, scope: !15)
158!15 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !7)
159!16 = !DILocation(line: 10, scope: !17)
160!17 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !7)
161!18 = !DILocation(line: 10, scope: !19)
162!19 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !17)
163!20 = !DILocation(line: 10, scope: !21)
164!21 = !DILexicalBlockFile(discriminator: 4, file: !1, scope: !17)
165!22 = !DILocation(line: 10, scope: !23)
166!23 = !DILexicalBlockFile(discriminator: 6, file: !1, scope: !17)
167!24 = !DILocation(line: 11, scope: !7)
168!25 = !DILocation(line: 12, scope: !7)
169