1; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s
2; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml
3; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s
4; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml
5
6; Original test case.
7;
8;     1    #include <stdlib.h>
9;     2
10;     3    long long foo() {
11;     4      long long int sum = 0;
12;     5      for (int i = 0; i < 500000000; i++)
13;     6        if (i < 1000)
14;     7          sum -= i;
15;     8        else
16;     9          sum += -i * rand();
17;    10      return sum;
18;    11    }
19;    12
20;    13    int main() { return foo() > 0; }
21
22; We are expecting foo() to be inlined in main() (almost all the cycles are
23; spent inside foo).
24; CHECK: remark: remarks.cc:13:21: '_Z3foov' inlined into 'main' to match profiling context with (cost=130, threshold=2147483647) at callsite main:0:21;
25; CHECK: remark: remarks.cc:9:19: 'rand' inlined into 'main' to match profiling context with (cost=always): always inline attribute at callsite _Z3foov:6:19 @ main:0:21;
26
27; The back edge for the loop is the hottest edge in the loop subgraph.
28; CHECK: remark: remarks.cc:6:9: most popular destination for conditional branches at remarks.cc:5:3
29
30; The predicate almost always chooses the 'else' branch.
31; CHECK: remark: remarks.cc:9:15: most popular destination for conditional branches at remarks.cc:6:9
32
33; Checking to see if YAML file is generated and contains remarks
34;YAML:       --- !Passed
35;YAML-NEXT:  Pass:            sample-profile-inline
36;YAML-NEXT:  Name:            Inlined
37;YAML-NEXT:  DebugLoc:        { File: remarks.cc, Line: 13, Column: 21 }
38;YAML-NEXT:  Function:        main
39;YAML-NEXT:  Args:
40;YAML-NEXT:    - String:          ''''
41;YAML-NEXT:    - Callee:          _Z3foov
42;YAML-NEXT:      DebugLoc:        { File: remarks.cc, Line: 3, Column: 0 }
43;YAML-NEXT:    - String:          ''' inlined into '
44;YAML-NEXT:    - Caller:          main
45;YAML-NEXT:        DebugLoc:        { File: remarks.cc, Line: 13, Column: 0 }
46;YAML-NEXT:    - String:          ''''
47;YAML-NEXT:    - String:          ' to match profiling context'
48;YAML-NEXT:    - String:          ' with '
49;YAML-NEXT:    - String:          '(cost='
50;YAML-NEXT:    - Cost:            '130'
51;YAML-NEXT:    - String:          ', threshold='
52;YAML-NEXT:    - Threshold:       '2147483647'
53;YAML-NEXT:    - String:          ')'
54;YAML-NEXT:    - String:          ' at callsite '
55;YAML-NEXT:    - String:          main
56;YAML-NEXT:    - String:          ':'
57;YAML-NEXT:    - Line:            '0'
58;YAML-NEXT:    - String:          ':'
59;YAML-NEXT:    - Column:          '21'
60;YAML-NEXT:    - String:          ';'
61;YAML-NEXT:  ...
62;YAML:       --- !Passed
63;YAML-NEXT:  Pass:            sample-profile-inline
64;YAML-NEXT:  Name:            AlwaysInline
65;YAML-NEXT:  DebugLoc:        { File: remarks.cc, Line: 9, Column: 19 }
66;YAML-NEXT:  Function:        main
67;YAML-NEXT:  Args:
68;YAML-NEXT:    - String:          ''''
69;YAML-NEXT:    - Callee:          rand
70;YAML-NEXT:      DebugLoc:        { File: remarks.cc, Line: 90, Column: 0 }
71;YAML-NEXT:    - String:          ''' inlined into '''
72;YAML-NEXT:    - Caller:          main
73;YAML-NEXT:      DebugLoc:        { File: remarks.cc, Line: 13, Column: 0 }
74;YAML-NEXT:    - String:          ''''
75;YAML-NEXT:    - String:          ' to match profiling context'
76;YAML-NEXT:    - String:          ' with '
77;YAML-NEXT:    - String:          '(cost=always)'
78;YAML-NEXT:    - String:          ': '
79;YAML-NEXT:    - Reason:          always inline attribute
80;YAML-NEXT:    - String:          ' at callsite '
81;YAML-NEXT:    - String:          _Z3foov
82;YAML-NEXT:    - String:          ':'
83;YAML-NEXT:    - Line:            '6'
84;YAML-NEXT:    - String:          ':'
85;YAML-NEXT:    - Column:          '19'
86;YAML-NEXT:    - String:          ' @ '
87;YAML-NEXT:    - String:          main
88;YAML-NEXT:    - String:          ':'
89;YAML-NEXT:    - Line:            '0'
90;YAML-NEXT:    - String:          ':'
91;YAML-NEXT:    - Column:          '21'
92;YAML-NEXT:    - String:          ';'
93;YAML:  --- !Analysis
94;YAML-NEXT:  Pass:            sample-profile
95;YAML-NEXT:  Name:            AppliedSamples
96;YAML-NEXT:  DebugLoc:        { File: remarks.cc, Line: 5, Column: 8 }
97;YAML-NEXT:  Function:        main
98;YAML-NEXT:  Args:
99;YAML-NEXT:    - String:          'Applied '
100;YAML-NEXT:    - NumSamples:      '18305'
101;YAML-NEXT:    - String:          ' samples from profile (offset: '
102;YAML-NEXT:    - LineOffset:      '2'
103;YAML-NEXT:    - String:          ')'
104;YAML-NEXT:  ...
105;YAML:  --- !Passed
106;YAML-NEXT:  Pass:            sample-profile
107;YAML-NEXT:  Name:            PopularDest
108;YAML-NEXT:  DebugLoc:        { File: remarks.cc, Line: 6, Column: 9 }
109;YAML-NEXT:  Function:        main
110;YAML-NEXT:  Args:
111;YAML-NEXT:    - String:          'most popular destination for conditional branches at '
112;YAML-NEXT:    - CondBranchesLoc: 'remarks.cc:5:3'
113;YAML-NEXT:      DebugLoc:        { File: remarks.cc, Line: 5, Column: 3 }
114;YAML-NEXT:  ...
115
116; Function Attrs: nounwind uwtable
117define i64 @_Z3foov() #0 !dbg !4 {
118entry:
119  %sum = alloca i64, align 8
120  %i = alloca i32, align 4
121  %0 = bitcast i64* %sum to i8*, !dbg !19
122  call void @llvm.lifetime.start.p0i8(i64 8, i8* %0) #4, !dbg !19
123  call void @llvm.dbg.declare(metadata i64* %sum, metadata !9, metadata !20), !dbg !21
124  store i64 0, i64* %sum, align 8, !dbg !21, !tbaa !22
125  %1 = bitcast i32* %i to i8*, !dbg !26
126  call void @llvm.lifetime.start.p0i8(i64 4, i8* %1) #4, !dbg !26
127  call void @llvm.dbg.declare(metadata i32* %i, metadata !10, metadata !20), !dbg !27
128  store i32 0, i32* %i, align 4, !dbg !27, !tbaa !28
129  br label %for.cond, !dbg !26
130
131for.cond:                                         ; preds = %for.inc, %entry
132  %2 = load i32, i32* %i, align 4, !dbg !30, !tbaa !28
133  %cmp = icmp slt i32 %2, 500000000, !dbg !34
134  br i1 %cmp, label %for.body, label %for.cond.cleanup, !dbg !35
135
136for.cond.cleanup:                                 ; preds = %for.cond
137  %3 = bitcast i32* %i to i8*, !dbg !36
138  call void @llvm.lifetime.end.p0i8(i64 4, i8* %3) #4, !dbg !36
139  br label %for.end
140
141for.body:                                         ; preds = %for.cond
142  %4 = load i32, i32* %i, align 4, !dbg !38, !tbaa !28
143  %cmp1 = icmp slt i32 %4, 1000, !dbg !40
144  br i1 %cmp1, label %if.then, label %if.else, !dbg !41
145
146if.then:                                          ; preds = %for.body
147  %5 = load i32, i32* %i, align 4, !dbg !42, !tbaa !28
148  %conv = sext i32 %5 to i64, !dbg !42
149  %6 = load i64, i64* %sum, align 8, !dbg !43, !tbaa !22
150  %sub = sub nsw i64 %6, %conv, !dbg !43
151  store i64 %sub, i64* %sum, align 8, !dbg !43, !tbaa !22
152  br label %if.end, !dbg !44
153
154if.else:                                          ; preds = %for.body
155  %7 = load i32, i32* %i, align 4, !dbg !45, !tbaa !28
156  %sub2 = sub nsw i32 0, %7, !dbg !46
157  %call = call i32 @rand() #4, !dbg !47
158  %mul = mul nsw i32 %sub2, %call, !dbg !48
159  %conv3 = sext i32 %mul to i64, !dbg !46
160  %8 = load i64, i64* %sum, align 8, !dbg !49, !tbaa !22
161  %add = add nsw i64 %8, %conv3, !dbg !49
162  store i64 %add, i64* %sum, align 8, !dbg !49, !tbaa !22
163  br label %if.end
164
165if.end:                                           ; preds = %if.else, %if.then
166  br label %for.inc, !dbg !50
167
168for.inc:                                          ; preds = %if.end
169  %9 = load i32, i32* %i, align 4, !dbg !51, !tbaa !28
170  %inc = add nsw i32 %9, 1, !dbg !51
171  store i32 %inc, i32* %i, align 4, !dbg !51, !tbaa !28
172  br label %for.cond, !dbg !52
173
174for.end:                                          ; preds = %for.cond.cleanup
175  %10 = load i64, i64* %sum, align 8, !dbg !53, !tbaa !22
176  %11 = bitcast i64* %sum to i8*, !dbg !54
177  call void @llvm.lifetime.end.p0i8(i64 8, i8* %11) #4, !dbg !54
178  ret i64 %10, !dbg !55
179}
180
181; Function Attrs: nounwind argmemonly
182declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1
183
184; Function Attrs: nounwind readnone
185declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
186
187; Function Attrs: nounwind
188define i32 @rand() #3 !dbg !59 {
189  ret i32 1
190}
191
192; Function Attrs: nounwind argmemonly
193declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
194
195; Function Attrs: nounwind uwtable
196define i32 @main() #0 !dbg !13 {
197entry:
198  %retval = alloca i32, align 4
199  store i32 0, i32* %retval, align 4
200  %call = call i64 @_Z3foov(), !dbg !56
201  %cmp = icmp sgt i64 %call, 0, !dbg !57
202  %conv = zext i1 %cmp to i32, !dbg !56
203  ret i32 %conv, !dbg !58
204}
205
206attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" "use-sample-profile" }
207attributes #1 = { nounwind argmemonly }
208attributes #2 = { nounwind readnone }
209attributes #3 = { nounwind alwaysinline "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
210attributes #4 = { nounwind }
211
212!llvm.dbg.cu = !{!0}
213!llvm.module.flags = !{!16, !17}
214!llvm.ident = !{!18}
215
216!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 251041) (llvm/trunk 251053)", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2)
217!1 = !DIFile(filename: "remarks.cc", directory: ".")
218!2 = !{}
219!4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8)
220!5 = !DISubroutineType(types: !6)
221!6 = !{!7}
222!7 = !DIBasicType(name: "long long int", size: 64, align: 64, encoding: DW_ATE_signed)
223!8 = !{!9, !10}
224!9 = !DILocalVariable(name: "sum", scope: !4, file: !1, line: 4, type: !7)
225!10 = !DILocalVariable(name: "i", scope: !11, file: !1, line: 5, type: !12)
226!11 = distinct !DILexicalBlock(scope: !4, file: !1, line: 5, column: 3)
227!12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
228!13 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 13, type: !14, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2)
229!14 = !DISubroutineType(types: !15)
230!15 = !{!12}
231!16 = !{i32 2, !"Dwarf Version", i32 4}
232!17 = !{i32 2, !"Debug Info Version", i32 3}
233!18 = !{!"clang version 3.8.0 (trunk 251041) (llvm/trunk 251053)"}
234!19 = !DILocation(line: 4, column: 3, scope: !4)
235!20 = !DIExpression()
236!21 = !DILocation(line: 4, column: 17, scope: !4)
237!22 = !{!23, !23, i64 0}
238!23 = !{!"long long", !24, i64 0}
239!24 = !{!"omnipotent char", !25, i64 0}
240!25 = !{!"Simple C/C++ TBAA"}
241!26 = !DILocation(line: 5, column: 8, scope: !11)
242!27 = !DILocation(line: 5, column: 12, scope: !11)
243!28 = !{!29, !29, i64 0}
244!29 = !{!"int", !24, i64 0}
245!30 = !DILocation(line: 5, column: 19, scope: !31)
246!31 = !DILexicalBlockFile(scope: !32, file: !1, discriminator: 3)
247!32 = !DILexicalBlockFile(scope: !33, file: !1, discriminator: 1)
248!33 = distinct !DILexicalBlock(scope: !11, file: !1, line: 5, column: 3)
249!34 = !DILocation(line: 5, column: 21, scope: !33)
250!35 = !DILocation(line: 5, column: 3, scope: !11)
251!36 = !DILocation(line: 5, column: 3, scope: !37)
252!37 = !DILexicalBlockFile(scope: !33, file: !1, discriminator: 2)
253!38 = !DILocation(line: 6, column: 9, scope: !39)
254!39 = distinct !DILexicalBlock(scope: !33, file: !1, line: 6, column: 9)
255!40 = !DILocation(line: 6, column: 11, scope: !39)
256!41 = !DILocation(line: 6, column: 9, scope: !33)
257!42 = !DILocation(line: 7, column: 14, scope: !39)
258!43 = !DILocation(line: 7, column: 11, scope: !39)
259!44 = !DILocation(line: 7, column: 7, scope: !39)
260!45 = !DILocation(line: 9, column: 15, scope: !39)
261!46 = !DILocation(line: 9, column: 14, scope: !39)
262!47 = !DILocation(line: 9, column: 19, scope: !39)
263!48 = !DILocation(line: 9, column: 17, scope: !39)
264!49 = !DILocation(line: 9, column: 11, scope: !39)
265!50 = !DILocation(line: 6, column: 13, scope: !39)
266!51 = !DILocation(line: 5, column: 35, scope: !33)
267!52 = !DILocation(line: 5, column: 3, scope: !33)
268!53 = !DILocation(line: 10, column: 10, scope: !4)
269!54 = !DILocation(line: 11, column: 1, scope: !4)
270!55 = !DILocation(line: 10, column: 3, scope: !4)
271!56 = !DILocation(line: 13, column: 21, scope: !13)
272!57 = !DILocation(line: 13, column: 27, scope: !13)
273!58 = !DILocation(line: 13, column: 14, scope: !13)
274!59 = distinct !DISubprogram(name: "rand", linkageName: "rand", scope: !1, file: !1, line: 90, type: !5, isLocal: false, isDefinition: true, scopeLine: 90, flags: DIFlagPrototyped, isOptimized: true, unit: !0)
275