1; 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:
35  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
37  %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
43  %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1, !dbg !30
44  %0 = load i8*, i8** %arrayidx, align 8, !dbg !30, !tbaa !31
45  %call = tail call i32 @atoi(i8* %0) #4, !dbg !30
46  tail call void @llvm.dbg.value(metadata i32 %call, i64 0, metadata !17, metadata !MDExpression()), !dbg !30
47  %cmp1 = icmp sgt i32 %call, 100, !dbg !35
48  br i1 %cmp1, label %for.body, label %if.end6, !dbg !35
49; CHECK: edge if.end -> for.body probability is 1 / 2 = 50%
50; CHECK: edge if.end -> if.end6 probability is 1 / 2 = 50%
51
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
58  tail call void @llvm.dbg.value(metadata double %add4, i64 0, metadata !18, metadata !MDExpression()), !dbg !36
59  %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
63  tail call void @llvm.dbg.value(metadata double %sub, i64 0, metadata !18, metadata !MDExpression()), !dbg !37
64  %inc = add nsw i32 %u.016, 1, !dbg !38
65  tail call void @llvm.dbg.value(metadata i32 %inc, i64 0, metadata !21, metadata !MDExpression()), !dbg !38
66  %exitcond = icmp eq i32 %inc, %call, !dbg !38
67  br i1 %exitcond, label %if.end6, label %for.body, !dbg !38
68; 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]
70
71if.end6:                                          ; preds = %for.body, %if.end
72  %result.0 = phi double [ 0.000000e+00, %if.end ], [ %sub, %for.body ]
73  %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
74  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
89declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #3
90
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}
98!llvm.module.flags = !{!25, !42}
99!llvm.ident = !{!26}
100
101!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: ".")
103!2 = !{}
104!3 = !{!4}
105!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)
108!7 = !{!8, !8, !9}
109!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)
113!12 = !{!13, !14, !15, !17, !18, !21, !23}
114!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)
126!25 = !{i32 2, !"Dwarf Version", i32 4}
127!26 = !{!"clang version 3.4 (trunk 192896) (llvm/trunk 192895)"}
128!27 = !MDLocation(line: 4, scope: !4)
129!28 = !MDLocation(line: 5, scope: !29)
130!29 = distinct !MDLexicalBlock(line: 5, column: 0, file: !1, scope: !4)
131!30 = !MDLocation(line: 8, scope: !4)
132!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"}
136!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)
143!42 = !{i32 1, !"Debug Info Version", i32 3}
144