blob: d7ce92f12d8714aab8c46f84fa24d86f34338e3f [file] [log] [blame]
Diego Novillo8d6568b2013-11-13 12:22:21 +00001; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/branch.prof | opt -analyze -branch-prob | FileCheck %s
2
3; Original C++ code for this test case:
4;
5; #include <stdio.h>
6; #include <stdlib.h>
7;
8; int main(int argc, char *argv[]) {
9; if (argc < 2)
10; return 1;
11; double result;
12; int limit = atoi(argv[1]);
13; if (limit > 100) {
14; double s = 23.041968;
15; for (int u = 0; u < limit; u++) {
16; double x = s;
17; s = x + 3.049 + (double)u;
18; s -= s + 3.94 / x * 0.32;
19; }
20; result = s;
21; } else {
22; result = 0;
23; }
24; printf("result is %lf\n", result);
25; return 0;
26; }
27
28@.str = private unnamed_addr constant [15 x i8] c"result is %lf\0A\00", align 1
29
30; Function Attrs: nounwind uwtable
31define i32 @main(i32 %argc, i8** nocapture readonly %argv) #0 {
32; CHECK: Printing analysis 'Branch Probability Analysis' for function 'main':
33
34entry:
Duncan P. N. Exon Smith166121a2015-03-15 01:21:30 +000035 tail call void @llvm.dbg.value(metadata i32 %argc, i64 0, metadata !13, metadata !MDExpression()), !dbg !27
36 tail call void @llvm.dbg.value(metadata i8** %argv, i64 0, metadata !14, metadata !MDExpression()), !dbg !27
Diego Novillo8d6568b2013-11-13 12:22:21 +000037 %cmp = icmp slt i32 %argc, 2, !dbg !28
38 br i1 %cmp, label %return, label %if.end, !dbg !28
39; CHECK: edge entry -> return probability is 1 / 2 = 50%
40; CHECK: edge entry -> if.end probability is 1 / 2 = 50%
41
42if.end: ; preds = %entry
David Blaikie79e6c742015-02-27 19:29:02 +000043 %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1, !dbg !30
David Blaikiea79ac142015-02-27 21:17:42 +000044 %0 = load i8*, i8** %arrayidx, align 8, !dbg !30, !tbaa !31
Diego Novillo8d6568b2013-11-13 12:22:21 +000045 %call = tail call i32 @atoi(i8* %0) #4, !dbg !30
Duncan P. N. Exon Smith166121a2015-03-15 01:21:30 +000046 tail call void @llvm.dbg.value(metadata i32 %call, i64 0, metadata !17, metadata !MDExpression()), !dbg !30
Diego Novillo8d6568b2013-11-13 12:22:21 +000047 %cmp1 = icmp sgt i32 %call, 100, !dbg !35
48 br i1 %cmp1, label %for.body, label %if.end6, !dbg !35
Diego Novillo0accb3d2014-01-10 23:23:46 +000049; CHECK: edge if.end -> for.body probability is 1 / 2 = 50%
50; CHECK: edge if.end -> if.end6 probability is 1 / 2 = 50%
Diego Novillo8d6568b2013-11-13 12:22:21 +000051
52for.body: ; preds = %if.end, %for.body
53 %u.016 = phi i32 [ %inc, %for.body ], [ 0, %if.end ]
54 %s.015 = phi double [ %sub, %for.body ], [ 0x40370ABE6A337A81, %if.end ]
55 %add = fadd double %s.015, 3.049000e+00, !dbg !36
56 %conv = sitofp i32 %u.016 to double, !dbg !36
57 %add4 = fadd double %add, %conv, !dbg !36
Duncan P. N. Exon Smith166121a2015-03-15 01:21:30 +000058 tail call void @llvm.dbg.value(metadata double %add4, i64 0, metadata !18, metadata !MDExpression()), !dbg !36
Diego Novillo8d6568b2013-11-13 12:22:21 +000059 %div = fdiv double 3.940000e+00, %s.015, !dbg !37
60 %mul = fmul double %div, 3.200000e-01, !dbg !37
61 %add5 = fadd double %add4, %mul, !dbg !37
62 %sub = fsub double %add4, %add5, !dbg !37
Duncan P. N. Exon Smith166121a2015-03-15 01:21:30 +000063 tail call void @llvm.dbg.value(metadata double %sub, i64 0, metadata !18, metadata !MDExpression()), !dbg !37
Diego Novillo8d6568b2013-11-13 12:22:21 +000064 %inc = add nsw i32 %u.016, 1, !dbg !38
Duncan P. N. Exon Smith166121a2015-03-15 01:21:30 +000065 tail call void @llvm.dbg.value(metadata i32 %inc, i64 0, metadata !21, metadata !MDExpression()), !dbg !38
Diego Novillo8d6568b2013-11-13 12:22:21 +000066 %exitcond = icmp eq i32 %inc, %call, !dbg !38
67 br i1 %exitcond, label %if.end6, label %for.body, !dbg !38
Diego Novillo0accb3d2014-01-10 23:23:46 +000068; CHECK: edge for.body -> if.end6 probability is 1 / 10227 = 0.00977804
69; CHECK: edge for.body -> for.body probability is 10226 / 10227 = 99.9902% [HOT edge]
Diego Novillo8d6568b2013-11-13 12:22:21 +000070
71if.end6: ; preds = %for.body, %if.end
72 %result.0 = phi double [ 0.000000e+00, %if.end ], [ %sub, %for.body ]
David Blaikief72d05b2015-03-13 18:20:45 +000073 %call7 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %result.0), !dbg !39
Diego Novillo8d6568b2013-11-13 12:22:21 +000074 br label %return, !dbg !40
75; CHECK: edge if.end6 -> return probability is 16 / 16 = 100% [HOT edge]
76
77return: ; preds = %entry, %if.end6
78 %retval.0 = phi i32 [ 0, %if.end6 ], [ 1, %entry ]
79 ret i32 %retval.0, !dbg !41
80}
81
82; Function Attrs: nounwind readonly
83declare i32 @atoi(i8* nocapture) #1
84
85; Function Attrs: nounwind
86declare i32 @printf(i8* nocapture readonly, ...) #2
87
88; Function Attrs: nounwind readnone
Adrian Prantl87b7eb92014-10-01 18:55:02 +000089declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #3
Diego Novillo8d6568b2013-11-13 12:22:21 +000090
91attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
92attributes #1 = { nounwind readonly "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
93attributes #2 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
94attributes #3 = { nounwind readnone }
95attributes #4 = { nounwind readonly }
96
97!llvm.dbg.cu = !{!0}
Manman Ren409558f2013-11-22 21:49:45 +000098!llvm.module.flags = !{!25, !42}
Diego Novillo8d6568b2013-11-13 12:22:21 +000099!llvm.ident = !{!26}
100
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000101!0 = !MDCompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.4 (trunk 192896) (llvm/trunk 192895)", isOptimized: true, emissionKind: 0, file: !1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
102!1 = !MDFile(filename: "branch.cc", directory: ".")
Duncan P. N. Exon Smith988a7f82015-03-27 20:46:33 +0000103!2 = !{}
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000104!3 = !{!4}
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000105!4 = !MDSubprogram(name: "main", line: 4, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: true, scopeLine: 4, file: !1, scope: !5, type: !6, function: i32 (i32, i8**)* @main, variables: !12)
106!5 = !MDFile(filename: "branch.cc", directory: ".")
107!6 = !MDSubroutineType(types: !7)
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000108!7 = !{!8, !8, !9}
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000109!8 = !MDBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
110!9 = !MDDerivedType(tag: DW_TAG_pointer_type, size: 64, align: 64, baseType: !10)
111!10 = !MDDerivedType(tag: DW_TAG_pointer_type, size: 64, align: 64, baseType: !11)
112!11 = !MDBasicType(tag: DW_TAG_base_type, name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000113!12 = !{!13, !14, !15, !17, !18, !21, !23}
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000114!13 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "argc", line: 4, arg: 1, scope: !4, file: !5, type: !8)
115!14 = !MDLocalVariable(tag: DW_TAG_arg_variable, name: "argv", line: 4, arg: 2, scope: !4, file: !5, type: !9)
116!15 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "result", line: 7, scope: !4, file: !5, type: !16)
117!16 = !MDBasicType(tag: DW_TAG_base_type, name: "double", size: 64, align: 64, encoding: DW_ATE_float)
118!17 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "limit", line: 8, scope: !4, file: !5, type: !8)
119!18 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "s", line: 10, scope: !19, file: !5, type: !16)
120!19 = distinct !MDLexicalBlock(line: 9, column: 0, file: !1, scope: !20)
121!20 = distinct !MDLexicalBlock(line: 9, column: 0, file: !1, scope: !4)
122!21 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "u", line: 11, scope: !22, file: !5, type: !8)
123!22 = distinct !MDLexicalBlock(line: 11, column: 0, file: !1, scope: !19)
124!23 = !MDLocalVariable(tag: DW_TAG_auto_variable, name: "x", line: 12, scope: !24, file: !5, type: !16)
125!24 = distinct !MDLexicalBlock(line: 11, column: 0, file: !1, scope: !22)
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000126!25 = !{i32 2, !"Dwarf Version", i32 4}
127!26 = !{!"clang version 3.4 (trunk 192896) (llvm/trunk 192895)"}
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000128!27 = !MDLocation(line: 4, scope: !4)
129!28 = !MDLocation(line: 5, scope: !29)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000130!29 = distinct !MDLexicalBlock(line: 5, column: 0, file: !1, scope: !4)
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000131!30 = !MDLocation(line: 8, scope: !4)
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000132!31 = !{!32, !32, i64 0}
133!32 = !{!"any pointer", !33, i64 0}
134!33 = !{!"omnipotent char", !34, i64 0}
135!34 = !{!"Simple C/C++ TBAA"}
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000136!35 = !MDLocation(line: 9, scope: !20)
137!36 = !MDLocation(line: 13, scope: !24)
138!37 = !MDLocation(line: 14, scope: !24)
139!38 = !MDLocation(line: 11, scope: !22)
140!39 = !MDLocation(line: 20, scope: !4)
141!40 = !MDLocation(line: 21, scope: !4)
142!41 = !MDLocation(line: 22, scope: !4)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000143!42 = !{i32 1, !"Debug Info Version", i32 3}