1; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/branch.prof | opt -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s 2; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/branch.prof -overwrite-existing-weights=1 | opt -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s --check-prefix=OVW 3 4; Original C++ code for this test case: 5; 6; #include <stdio.h> 7; #include <stdlib.h> 8 9; int main(int argc, char *argv[]) { 10; if (argc < 2) 11; return 1; 12; double result; 13; int limit = atoi(argv[1]); 14; if (limit > 100) { 15; double s = 23.041968 * atoi(argv[2]); 16; for (int u = 0; u < limit; u++) { 17; double x = s; 18; s = x + 3.049 + (double)u; 19; s -= s + 3.94 / x * 0.32; 20; } 21; result = s; 22; } else { 23; result = atoi(argv[2]); 24; } 25; printf("result is %lf\n", result); 26; return 0; 27; } 28 29@.str = private unnamed_addr constant [15 x i8] c"result is %lf\0A\00", align 1 30 31; Function Attrs: uwtable 32define i32 @main(i32 %argc, ptr %argv) #0 !dbg !6 { 33; CHECK: Printing analysis {{.*}} for function 'main': 34 35entry: 36 %retval = alloca i32, align 4 37 %argc.addr = alloca i32, align 4 38 %argv.addr = alloca ptr, align 8 39 %result = alloca double, align 8 40 %limit = alloca i32, align 4 41 %s = alloca double, align 8 42 %u = alloca i32, align 4 43 %x = alloca double, align 8 44 store i32 0, ptr %retval, align 4 45 store i32 %argc, ptr %argc.addr, align 4 46 call void @llvm.dbg.declare(metadata ptr %argc.addr, metadata !16, metadata !17), !dbg !18 47 store ptr %argv, ptr %argv.addr, align 8 48 call void @llvm.dbg.declare(metadata ptr %argv.addr, metadata !19, metadata !17), !dbg !20 49 %0 = load i32, ptr %argc.addr, align 4, !dbg !21 50 %cmp = icmp slt i32 %0, 2, !dbg !23 51 br i1 %cmp, label %if.then, label %if.end, !dbg !24 52; CHECK: edge %entry -> %if.then probability is 0x4ccf6b16 / 0x80000000 = 60.01% 53; CHECK: edge %entry -> %if.end probability is 0x333094ea / 0x80000000 = 39.99% 54 55if.then: ; preds = %entry 56 store i32 1, ptr %retval, align 4, !dbg !25 57 br label %return, !dbg !25 58 59if.end: ; preds = %entry 60 call void @llvm.dbg.declare(metadata ptr %result, metadata !26, metadata !17), !dbg !27 61 call void @llvm.dbg.declare(metadata ptr %limit, metadata !28, metadata !17), !dbg !29 62 %1 = load ptr, ptr %argv.addr, align 8, !dbg !30 63 %arrayidx = getelementptr inbounds ptr, ptr %1, i64 1, !dbg !30 64 %2 = load ptr, ptr %arrayidx, align 8, !dbg !30 65 %call = call i32 @atoi(ptr %2) #4, !dbg !31 66 store i32 %call, ptr %limit, align 4, !dbg !29 67 %3 = load i32, ptr %limit, align 4, !dbg !32 68 %cmp1 = icmp sgt i32 %3, 100, !dbg !34 69 br i1 %cmp1, label %if.then.2, label %if.else, !dbg !35 70; CHECK: edge %if.end -> %if.then.2 probability is 0x6652c748 / 0x80000000 = 79.94% 71; CHECK: edge %if.end -> %if.else probability is 0x19ad38b8 / 0x80000000 = 20.06% 72 73if.then.2: ; preds = %if.end 74 call void @llvm.dbg.declare(metadata ptr %s, metadata !36, metadata !17), !dbg !38 75 %4 = load ptr, ptr %argv.addr, align 8, !dbg !39 76 %arrayidx3 = getelementptr inbounds ptr, ptr %4, i64 2, !dbg !39 77 %5 = load ptr, ptr %arrayidx3, align 8, !dbg !39 78 %call4 = call i32 @atoi(ptr %5) #4, !dbg !40 79 %conv = sitofp i32 %call4 to double, !dbg !40 80 %mul = fmul double 0x40370ABE6A337A81, %conv, !dbg !41 81 store double %mul, ptr %s, align 8, !dbg !38 82 call void @llvm.dbg.declare(metadata ptr %u, metadata !42, metadata !17), !dbg !44 83 store i32 0, ptr %u, align 4, !dbg !44 84 br label %for.cond, !dbg !45 85 86for.cond: ; preds = %for.inc, %if.then.2 87 %6 = load i32, ptr %u, align 4, !dbg !46 88 %7 = load i32, ptr %limit, align 4, !dbg !48 89 %cmp5 = icmp slt i32 %6, %7, !dbg !49 90 br i1 %cmp5, label %for.body, label %for.end, !dbg !50, !prof !80 91; CHECK: edge %for.cond -> %for.body probability is 0x73333333 / 0x80000000 = 90.00% 92; CHECK: edge %for.cond -> %for.end probability is 0x0ccccccd / 0x80000000 = 10.00% 93; OVW: edge %for.cond -> %for.body probability is 0x76b3f3be / 0x80000000 = 92.74% 94; OVW: edge %for.cond -> %for.end probability is 0x094c0c42 / 0x80000000 = 7.26% 95 96for.body: ; preds = %for.cond 97 call void @llvm.dbg.declare(metadata ptr %x, metadata !51, metadata !17), !dbg !53 98 %8 = load double, ptr %s, align 8, !dbg !54 99 store double %8, ptr %x, align 8, !dbg !53 100 %9 = load double, ptr %x, align 8, !dbg !55 101 %add = fadd double %9, 3.049000e+00, !dbg !56 102 %10 = load i32, ptr %u, align 4, !dbg !57 103 %conv6 = sitofp i32 %10 to double, !dbg !57 104 %add7 = fadd double %add, %conv6, !dbg !58 105 store double %add7, ptr %s, align 8, !dbg !59 106 %11 = load double, ptr %s, align 8, !dbg !60 107 %12 = load double, ptr %x, align 8, !dbg !61 108 %div = fdiv double 3.940000e+00, %12, !dbg !62 109 %mul8 = fmul double %div, 3.200000e-01, !dbg !63 110 %add9 = fadd double %11, %mul8, !dbg !64 111 %13 = load double, ptr %s, align 8, !dbg !65 112 %sub = fsub double %13, %add9, !dbg !65 113 store double %sub, ptr %s, align 8, !dbg !65 114 br label %for.inc, !dbg !66 115 116for.inc: ; preds = %for.body 117 %14 = load i32, ptr %u, align 4, !dbg !67 118 %inc = add nsw i32 %14, 1, !dbg !67 119 store i32 %inc, ptr %u, align 4, !dbg !67 120 br label %for.cond, !dbg !68 121 122for.end: ; preds = %for.cond 123 %15 = load double, ptr %s, align 8, !dbg !69 124 store double %15, ptr %result, align 8, !dbg !70 125 br label %if.end.13, !dbg !71 126 127if.else: ; preds = %if.end 128 %16 = load ptr, ptr %argv.addr, align 8, !dbg !72 129 %arrayidx10 = getelementptr inbounds ptr, ptr %16, i64 2, !dbg !72 130 %17 = load ptr, ptr %arrayidx10, align 8, !dbg !72 131 %call11 = call i32 @atoi(ptr %17) #4, !dbg !74 132 %conv12 = sitofp i32 %call11 to double, !dbg !74 133 store double %conv12, ptr %result, align 8, !dbg !75 134 br label %if.end.13 135 136if.end.13: ; preds = %if.else, %for.end 137 %18 = load double, ptr %result, align 8, !dbg !76 138 %call14 = call i32 (ptr, ...) @printf(ptr @.str, double %18), !dbg !77 139 store i32 0, ptr %retval, align 4, !dbg !78 140 br label %return, !dbg !78 141 142return: ; preds = %if.end.13, %if.then 143 %19 = load i32, ptr %retval, align 4, !dbg !79 144 ret i32 %19, !dbg !79 145} 146 147; Function Attrs: nounwind readnone 148declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 149 150; Function Attrs: nounwind readonly 151declare i32 @atoi(ptr) #2 152 153declare i32 @printf(ptr, ...) #3 154 155attributes #0 = { uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="all" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" "use-sample-profile" } 156attributes #1 = { nounwind readnone } 157attributes #2 = { nounwind readonly "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="all" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 158attributes #3 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="all" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 159attributes #4 = { nounwind readonly } 160 161!llvm.dbg.cu = !{!0} 162!llvm.module.flags = !{!13, !14} 163!llvm.ident = !{!15} 164 165!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 248211) (llvm/trunk 248217)", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug, enums: !2, retainedTypes: !3) 166!1 = !DIFile(filename: "test.cc", directory: "/ssd/llvm_commit") 167!2 = !{} 168!3 = !{!4} 169!4 = !DIBasicType(name: "double", size: 64, align: 64, encoding: DW_ATE_float) 170!6 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 4, type: !7, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2) 171!7 = !DISubroutineType(types: !8) 172!8 = !{!9, !9, !10} 173!9 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) 174!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64, align: 64) 175!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64, align: 64) 176!12 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) 177!13 = !{i32 2, !"Dwarf Version", i32 4} 178!14 = !{i32 2, !"Debug Info Version", i32 3} 179!15 = !{!"clang version 3.8.0 (trunk 248211) (llvm/trunk 248217)"} 180!16 = !DILocalVariable(name: "argc", arg: 1, scope: !6, file: !1, line: 4, type: !9) 181!17 = !DIExpression() 182!18 = !DILocation(line: 4, column: 15, scope: !6) 183!19 = !DILocalVariable(name: "argv", arg: 2, scope: !6, file: !1, line: 4, type: !10) 184!20 = !DILocation(line: 4, column: 27, scope: !6) 185!21 = !DILocation(line: 5, column: 8, scope: !22) 186!22 = distinct !DILexicalBlock(scope: !6, file: !1, line: 5, column: 8) 187!23 = !DILocation(line: 5, column: 13, scope: !22) 188!24 = !DILocation(line: 5, column: 8, scope: !6) 189!25 = !DILocation(line: 6, column: 6, scope: !22) 190!26 = !DILocalVariable(name: "result", scope: !6, file: !1, line: 7, type: !4) 191!27 = !DILocation(line: 7, column: 11, scope: !6) 192!28 = !DILocalVariable(name: "limit", scope: !6, file: !1, line: 8, type: !9) 193!29 = !DILocation(line: 8, column: 8, scope: !6) 194!30 = !DILocation(line: 8, column: 21, scope: !6) 195!31 = !DILocation(line: 8, column: 16, scope: !6) 196!32 = !DILocation(line: 9, column: 8, scope: !33) 197!33 = distinct !DILexicalBlock(scope: !6, file: !1, line: 9, column: 8) 198!34 = !DILocation(line: 9, column: 14, scope: !33) 199!35 = !DILocation(line: 9, column: 8, scope: !6) 200!36 = !DILocalVariable(name: "s", scope: !37, file: !1, line: 10, type: !4) 201!37 = distinct !DILexicalBlock(scope: !33, file: !1, line: 9, column: 21) 202!38 = !DILocation(line: 10, column: 13, scope: !37) 203!39 = !DILocation(line: 10, column: 34, scope: !37) 204!40 = !DILocation(line: 10, column: 29, scope: !37) 205!41 = !DILocation(line: 10, column: 27, scope: !37) 206!42 = !DILocalVariable(name: "u", scope: !43, file: !1, line: 11, type: !9) 207!43 = distinct !DILexicalBlock(scope: !37, file: !1, line: 11, column: 6) 208!44 = !DILocation(line: 11, column: 15, scope: !43) 209!45 = !DILocation(line: 11, column: 11, scope: !43) 210!46 = !DILocation(line: 11, column: 22, scope: !47) 211!47 = distinct !DILexicalBlock(scope: !43, file: !1, line: 11, column: 6) 212!48 = !DILocation(line: 11, column: 26, scope: !47) 213!49 = !DILocation(line: 11, column: 24, scope: !47) 214!50 = !DILocation(line: 11, column: 6, scope: !43) 215!51 = !DILocalVariable(name: "x", scope: !52, file: !1, line: 12, type: !4) 216!52 = distinct !DILexicalBlock(scope: !47, file: !1, line: 11, column: 38) 217!53 = !DILocation(line: 12, column: 15, scope: !52) 218!54 = !DILocation(line: 12, column: 19, scope: !52) 219!55 = !DILocation(line: 13, column: 12, scope: !52) 220!56 = !DILocation(line: 13, column: 14, scope: !52) 221!57 = !DILocation(line: 13, column: 32, scope: !52) 222!58 = !DILocation(line: 13, column: 22, scope: !52) 223!59 = !DILocation(line: 13, column: 10, scope: !52) 224!60 = !DILocation(line: 14, column: 13, scope: !52) 225!61 = !DILocation(line: 14, column: 24, scope: !52) 226!62 = !DILocation(line: 14, column: 22, scope: !52) 227!63 = !DILocation(line: 14, column: 26, scope: !52) 228!64 = !DILocation(line: 14, column: 15, scope: !52) 229!65 = !DILocation(line: 14, column: 10, scope: !52) 230!66 = !DILocation(line: 15, column: 6, scope: !52) 231!67 = !DILocation(line: 11, column: 34, scope: !47) 232!68 = !DILocation(line: 11, column: 6, scope: !47) 233!69 = !DILocation(line: 16, column: 15, scope: !37) 234!70 = !DILocation(line: 16, column: 13, scope: !37) 235!71 = !DILocation(line: 17, column: 4, scope: !37) 236!72 = !DILocation(line: 18, column: 20, scope: !73) 237!73 = distinct !DILexicalBlock(scope: !33, file: !1, line: 17, column: 11) 238!74 = !DILocation(line: 18, column: 15, scope: !73) 239!75 = !DILocation(line: 18, column: 13, scope: !73) 240!76 = !DILocation(line: 20, column: 30, scope: !6) 241!77 = !DILocation(line: 20, column: 4, scope: !6) 242!78 = !DILocation(line: 21, column: 4, scope: !6) 243!79 = !DILocation(line: 22, column: 2, scope: !6) 244!80 = !{!"branch_weights", i32 90, i32 10} 245