1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/gcc-simple.afdo -S | FileCheck %s 2*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/gcc-simple.afdo -S | FileCheck %s 3*9880d681SAndroid Build Coastguard Worker; XFAIL: powerpc64-, s390x, mips-, mips64-, sparc 4*9880d681SAndroid Build Coastguard Worker; Original code: 5*9880d681SAndroid Build Coastguard Worker; 6*9880d681SAndroid Build Coastguard Worker; #include <stdlib.h> 7*9880d681SAndroid Build Coastguard Worker; 8*9880d681SAndroid Build Coastguard Worker; long long int foo(long i) { 9*9880d681SAndroid Build Coastguard Worker; if (rand() < 500) return 2; else if (rand() > 5000) return 10; else return 90; 10*9880d681SAndroid Build Coastguard Worker; } 11*9880d681SAndroid Build Coastguard Worker; 12*9880d681SAndroid Build Coastguard Worker; int main() { 13*9880d681SAndroid Build Coastguard Worker; long long int sum = 0; 14*9880d681SAndroid Build Coastguard Worker; for (int k = 0; k < 3000; k++) 15*9880d681SAndroid Build Coastguard Worker; for (int i = 0; i < 200000; i++) sum += foo(i); 16*9880d681SAndroid Build Coastguard Worker; return sum > 0 ? 0 : 1; 17*9880d681SAndroid Build Coastguard Worker; } 18*9880d681SAndroid Build Coastguard Worker; 19*9880d681SAndroid Build Coastguard Worker; This test was compiled down to bytecode at -O0 to avoid inlining foo() into 20*9880d681SAndroid Build Coastguard Worker; main(). The profile was generated using a GCC-generated binary (also compiled 21*9880d681SAndroid Build Coastguard Worker; at -O0). The conversion from the Linux Perf profile to the GCC autofdo 22*9880d681SAndroid Build Coastguard Worker; profile used the converter at https://github.com/google/autofdo 23*9880d681SAndroid Build Coastguard Worker; 24*9880d681SAndroid Build Coastguard Worker; $ gcc -g -O0 gcc-simple.cc -o gcc-simple 25*9880d681SAndroid Build Coastguard Worker; $ perf record -b ./gcc-simple 26*9880d681SAndroid Build Coastguard Worker; $ create_gcov --binary=gcc-simple --gcov=gcc-simple.afdo 27*9880d681SAndroid Build Coastguard Worker 28*9880d681SAndroid Build Coastguard Workerdefine i64 @_Z3fool(i64 %i) #0 !dbg !4 { 29*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[EC1:[0-9]+]] 30*9880d681SAndroid Build Coastguard Workerentry: 31*9880d681SAndroid Build Coastguard Worker %retval = alloca i64, align 8 32*9880d681SAndroid Build Coastguard Worker %i.addr = alloca i64, align 8 33*9880d681SAndroid Build Coastguard Worker store i64 %i, i64* %i.addr, align 8 34*9880d681SAndroid Build Coastguard Worker call void @llvm.dbg.declare(metadata i64* %i.addr, metadata !16, metadata !17), !dbg !18 35*9880d681SAndroid Build Coastguard Worker %call = call i32 @rand() #3, !dbg !19 36*9880d681SAndroid Build Coastguard Worker %cmp = icmp slt i32 %call, 500, !dbg !21 37*9880d681SAndroid Build Coastguard Worker br i1 %cmp, label %if.then, label %if.else, !dbg !22 38*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[PROF1:[0-9]+]] 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard Workerif.then: ; preds = %entry 41*9880d681SAndroid Build Coastguard Worker store i64 2, i64* %retval, align 8, !dbg !23 42*9880d681SAndroid Build Coastguard Worker br label %return, !dbg !23 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Workerif.else: ; preds = %entry 45*9880d681SAndroid Build Coastguard Worker %call1 = call i32 @rand() #3, !dbg !25 46*9880d681SAndroid Build Coastguard Worker %cmp2 = icmp sgt i32 %call1, 5000, !dbg !28 47*9880d681SAndroid Build Coastguard Worker br i1 %cmp2, label %if.then.3, label %if.else.4, !dbg !29 48*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[PROF2:[0-9]+]] 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard Workerif.then.3: ; preds = %if.else 51*9880d681SAndroid Build Coastguard Worker store i64 10, i64* %retval, align 8, !dbg !30 52*9880d681SAndroid Build Coastguard Worker br label %return, !dbg !30 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard Workerif.else.4: ; preds = %if.else 55*9880d681SAndroid Build Coastguard Worker store i64 90, i64* %retval, align 8, !dbg !32 56*9880d681SAndroid Build Coastguard Worker br label %return, !dbg !32 57*9880d681SAndroid Build Coastguard Worker 58*9880d681SAndroid Build Coastguard Workerreturn: ; preds = %if.else.4, %if.then.3, %if.then 59*9880d681SAndroid Build Coastguard Worker %0 = load i64, i64* %retval, align 8, !dbg !34 60*9880d681SAndroid Build Coastguard Worker ret i64 %0, !dbg !34 61*9880d681SAndroid Build Coastguard Worker} 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind readnone 64*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.dbg.declare(metadata, metadata, metadata) #1 65*9880d681SAndroid Build Coastguard Worker 66*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind 67*9880d681SAndroid Build Coastguard Workerdeclare i32 @rand() #2 68*9880d681SAndroid Build Coastguard Worker 69*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind uwtable 70*9880d681SAndroid Build Coastguard Workerdefine i32 @main() #0 !dbg !9 { 71*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[EC2:[0-9]+]] 72*9880d681SAndroid Build Coastguard Workerentry: 73*9880d681SAndroid Build Coastguard Worker %retval = alloca i32, align 4 74*9880d681SAndroid Build Coastguard Worker %sum = alloca i64, align 8 75*9880d681SAndroid Build Coastguard Worker %k = alloca i32, align 4 76*9880d681SAndroid Build Coastguard Worker %i = alloca i32, align 4 77*9880d681SAndroid Build Coastguard Worker store i32 0, i32* %retval, align 4 78*9880d681SAndroid Build Coastguard Worker call void @llvm.dbg.declare(metadata i64* %sum, metadata !35, metadata !17), !dbg !36 79*9880d681SAndroid Build Coastguard Worker store i64 0, i64* %sum, align 8, !dbg !36 80*9880d681SAndroid Build Coastguard Worker call void @llvm.dbg.declare(metadata i32* %k, metadata !37, metadata !17), !dbg !39 81*9880d681SAndroid Build Coastguard Worker store i32 0, i32* %k, align 4, !dbg !39 82*9880d681SAndroid Build Coastguard Worker br label %for.cond, !dbg !40 83*9880d681SAndroid Build Coastguard Worker 84*9880d681SAndroid Build Coastguard Workerfor.cond: ; preds = %for.inc.4, %entry 85*9880d681SAndroid Build Coastguard Worker %0 = load i32, i32* %k, align 4, !dbg !41 86*9880d681SAndroid Build Coastguard Worker %cmp = icmp slt i32 %0, 3000, !dbg !45 87*9880d681SAndroid Build Coastguard Worker br i1 %cmp, label %for.body, label %for.end.6, !dbg !46 88*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[PROF3:[0-9]+]] 89*9880d681SAndroid Build Coastguard Worker 90*9880d681SAndroid Build Coastguard Workerfor.body: ; preds = %for.cond 91*9880d681SAndroid Build Coastguard Worker call void @llvm.dbg.declare(metadata i32* %i, metadata !47, metadata !17), !dbg !49 92*9880d681SAndroid Build Coastguard Worker store i32 0, i32* %i, align 4, !dbg !49 93*9880d681SAndroid Build Coastguard Worker br label %for.cond.1, !dbg !50 94*9880d681SAndroid Build Coastguard Worker 95*9880d681SAndroid Build Coastguard Workerfor.cond.1: ; preds = %for.inc, %for.body 96*9880d681SAndroid Build Coastguard Worker %1 = load i32, i32* %i, align 4, !dbg !51 97*9880d681SAndroid Build Coastguard Worker %cmp2 = icmp slt i32 %1, 200000, !dbg !55 98*9880d681SAndroid Build Coastguard Worker br i1 %cmp2, label %for.body.3, label %for.end, !dbg !56 99*9880d681SAndroid Build Coastguard Worker; CHECK: !prof ![[PROF4:[0-9]+]] 100*9880d681SAndroid Build Coastguard Worker 101*9880d681SAndroid Build Coastguard Workerfor.body.3: ; preds = %for.cond.1 102*9880d681SAndroid Build Coastguard Worker %2 = load i32, i32* %i, align 4, !dbg !57 103*9880d681SAndroid Build Coastguard Worker %conv = sext i32 %2 to i64, !dbg !57 104*9880d681SAndroid Build Coastguard Worker %call = call i64 @_Z3fool(i64 %conv), !dbg !59 105*9880d681SAndroid Build Coastguard Worker %3 = load i64, i64* %sum, align 8, !dbg !60 106*9880d681SAndroid Build Coastguard Worker %add = add nsw i64 %3, %call, !dbg !60 107*9880d681SAndroid Build Coastguard Worker store i64 %add, i64* %sum, align 8, !dbg !60 108*9880d681SAndroid Build Coastguard Worker br label %for.inc, !dbg !61 109*9880d681SAndroid Build Coastguard Worker 110*9880d681SAndroid Build Coastguard Workerfor.inc: ; preds = %for.body.3 111*9880d681SAndroid Build Coastguard Worker %4 = load i32, i32* %i, align 4, !dbg !62 112*9880d681SAndroid Build Coastguard Worker %inc = add nsw i32 %4, 1, !dbg !62 113*9880d681SAndroid Build Coastguard Worker store i32 %inc, i32* %i, align 4, !dbg !62 114*9880d681SAndroid Build Coastguard Worker br label %for.cond.1, !dbg !64 115*9880d681SAndroid Build Coastguard Worker 116*9880d681SAndroid Build Coastguard Workerfor.end: ; preds = %for.cond.1 117*9880d681SAndroid Build Coastguard Worker br label %for.inc.4, !dbg !65 118*9880d681SAndroid Build Coastguard Worker 119*9880d681SAndroid Build Coastguard Workerfor.inc.4: ; preds = %for.end 120*9880d681SAndroid Build Coastguard Worker %5 = load i32, i32* %k, align 4, !dbg !67 121*9880d681SAndroid Build Coastguard Worker %inc5 = add nsw i32 %5, 1, !dbg !67 122*9880d681SAndroid Build Coastguard Worker store i32 %inc5, i32* %k, align 4, !dbg !67 123*9880d681SAndroid Build Coastguard Worker br label %for.cond, !dbg !68 124*9880d681SAndroid Build Coastguard Worker 125*9880d681SAndroid Build Coastguard Workerfor.end.6: ; preds = %for.cond 126*9880d681SAndroid Build Coastguard Worker %6 = load i64, i64* %sum, align 8, !dbg !69 127*9880d681SAndroid Build Coastguard Worker %cmp7 = icmp sgt i64 %6, 0, !dbg !70 128*9880d681SAndroid Build Coastguard Worker %cond = select i1 %cmp7, i32 0, i32 1, !dbg !69 129*9880d681SAndroid Build Coastguard Worker ret i32 %cond, !dbg !71 130*9880d681SAndroid Build Coastguard Worker} 131*9880d681SAndroid Build Coastguard Worker 132*9880d681SAndroid Build Coastguard Worker; CHECK ![[EC1]] = !{!"function_entry_count", i64 24108} 133*9880d681SAndroid Build Coastguard Worker; CHECK ![[PROF1]] = !{!"branch_weights", i32 1, i32 30124} 134*9880d681SAndroid Build Coastguard Worker; CHECK ![[PROF2]] = !{!"branch_weights", i32 30177, i32 29579} 135*9880d681SAndroid Build Coastguard Worker; CHECK ![[EC2]] = !{!"function_entry_count", i64 0} 136*9880d681SAndroid Build Coastguard Worker; CHECK ![[PROF3]] = !{!"branch_weights", i32 1, i32 1} 137*9880d681SAndroid Build Coastguard Worker; CHECK ![[PROF4]] = !{!"branch_weights", i32 1, i32 20238} 138*9880d681SAndroid Build Coastguard Worker 139*9880d681SAndroid Build Coastguard Workerattributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "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" } 140*9880d681SAndroid Build Coastguard Workerattributes #1 = { nounwind readnone } 141*9880d681SAndroid Build Coastguard Workerattributes #2 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "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" } 142*9880d681SAndroid Build Coastguard Workerattributes #3 = { nounwind } 143*9880d681SAndroid Build Coastguard Worker 144*9880d681SAndroid Build Coastguard Worker!llvm.dbg.cu = !{!0} 145*9880d681SAndroid Build Coastguard Worker!llvm.module.flags = !{!13, !14} 146*9880d681SAndroid Build Coastguard Worker!llvm.ident = !{!15} 147*9880d681SAndroid Build Coastguard Worker 148*9880d681SAndroid Build Coastguard Worker!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 247554) (llvm/trunk 247557)", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug, enums: !2) 149*9880d681SAndroid Build Coastguard Worker!1 = !DIFile(filename: "discriminator.cc", directory: "/usr/local/google/home/dnovillo/llvm/test/autofdo") 150*9880d681SAndroid Build Coastguard Worker!2 = !{} 151*9880d681SAndroid Build Coastguard Worker!4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fool", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) 152*9880d681SAndroid Build Coastguard Worker!5 = !DISubroutineType(types: !6) 153*9880d681SAndroid Build Coastguard Worker!6 = !{!7, !8} 154*9880d681SAndroid Build Coastguard Worker!7 = !DIBasicType(name: "long long int", size: 64, align: 64, encoding: DW_ATE_signed) 155*9880d681SAndroid Build Coastguard Worker!8 = !DIBasicType(name: "long int", size: 64, align: 64, encoding: DW_ATE_signed) 156*9880d681SAndroid Build Coastguard Worker!9 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !10, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) 157*9880d681SAndroid Build Coastguard Worker!10 = !DISubroutineType(types: !11) 158*9880d681SAndroid Build Coastguard Worker!11 = !{!12} 159*9880d681SAndroid Build Coastguard Worker!12 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) 160*9880d681SAndroid Build Coastguard Worker!13 = !{i32 2, !"Dwarf Version", i32 4} 161*9880d681SAndroid Build Coastguard Worker!14 = !{i32 2, !"Debug Info Version", i32 3} 162*9880d681SAndroid Build Coastguard Worker!15 = !{!"clang version 3.8.0 (trunk 247554) (llvm/trunk 247557)"} 163*9880d681SAndroid Build Coastguard Worker!16 = !DILocalVariable(name: "i", arg: 1, scope: !4, file: !1, line: 3, type: !8) 164*9880d681SAndroid Build Coastguard Worker!17 = !DIExpression() 165*9880d681SAndroid Build Coastguard Worker!18 = !DILocation(line: 3, column: 24, scope: !4) 166*9880d681SAndroid Build Coastguard Worker!19 = !DILocation(line: 4, column: 7, scope: !20) 167*9880d681SAndroid Build Coastguard Worker!20 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 7) 168*9880d681SAndroid Build Coastguard Worker!21 = !DILocation(line: 4, column: 14, scope: !20) 169*9880d681SAndroid Build Coastguard Worker!22 = !DILocation(line: 4, column: 7, scope: !4) 170*9880d681SAndroid Build Coastguard Worker!23 = !DILocation(line: 4, column: 21, scope: !24) 171*9880d681SAndroid Build Coastguard Worker!24 = !DILexicalBlockFile(scope: !20, file: !1, discriminator: 1) 172*9880d681SAndroid Build Coastguard Worker!25 = !DILocation(line: 4, column: 40, scope: !26) 173*9880d681SAndroid Build Coastguard Worker!26 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 2) 174*9880d681SAndroid Build Coastguard Worker!27 = distinct !DILexicalBlock(scope: !20, file: !1, line: 4, column: 40) 175*9880d681SAndroid Build Coastguard Worker!28 = !DILocation(line: 4, column: 47, scope: !27) 176*9880d681SAndroid Build Coastguard Worker!29 = !DILocation(line: 4, column: 40, scope: !20) 177*9880d681SAndroid Build Coastguard Worker!30 = !DILocation(line: 4, column: 55, scope: !31) 178*9880d681SAndroid Build Coastguard Worker!31 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 3) 179*9880d681SAndroid Build Coastguard Worker!32 = !DILocation(line: 4, column: 71, scope: !33) 180*9880d681SAndroid Build Coastguard Worker!33 = !DILexicalBlockFile(scope: !27, file: !1, discriminator: 4) 181*9880d681SAndroid Build Coastguard Worker!34 = !DILocation(line: 5, column: 1, scope: !4) 182*9880d681SAndroid Build Coastguard Worker!35 = !DILocalVariable(name: "sum", scope: !9, file: !1, line: 8, type: !7) 183*9880d681SAndroid Build Coastguard Worker!36 = !DILocation(line: 8, column: 17, scope: !9) 184*9880d681SAndroid Build Coastguard Worker!37 = !DILocalVariable(name: "k", scope: !38, file: !1, line: 9, type: !12) 185*9880d681SAndroid Build Coastguard Worker!38 = distinct !DILexicalBlock(scope: !9, file: !1, line: 9, column: 3) 186*9880d681SAndroid Build Coastguard Worker!39 = !DILocation(line: 9, column: 12, scope: !38) 187*9880d681SAndroid Build Coastguard Worker!40 = !DILocation(line: 9, column: 8, scope: !38) 188*9880d681SAndroid Build Coastguard Worker!41 = !DILocation(line: 9, column: 19, scope: !42) 189*9880d681SAndroid Build Coastguard Worker!42 = !DILexicalBlockFile(scope: !43, file: !1, discriminator: 2) 190*9880d681SAndroid Build Coastguard Worker!43 = !DILexicalBlockFile(scope: !44, file: !1, discriminator: 1) 191*9880d681SAndroid Build Coastguard Worker!44 = distinct !DILexicalBlock(scope: !38, file: !1, line: 9, column: 3) 192*9880d681SAndroid Build Coastguard Worker!45 = !DILocation(line: 9, column: 21, scope: !44) 193*9880d681SAndroid Build Coastguard Worker!46 = !DILocation(line: 9, column: 3, scope: !38) 194*9880d681SAndroid Build Coastguard Worker!47 = !DILocalVariable(name: "i", scope: !48, file: !1, line: 10, type: !12) 195*9880d681SAndroid Build Coastguard Worker!48 = distinct !DILexicalBlock(scope: !44, file: !1, line: 10, column: 5) 196*9880d681SAndroid Build Coastguard Worker!49 = !DILocation(line: 10, column: 14, scope: !48) 197*9880d681SAndroid Build Coastguard Worker!50 = !DILocation(line: 10, column: 10, scope: !48) 198*9880d681SAndroid Build Coastguard Worker!51 = !DILocation(line: 10, column: 21, scope: !52) 199*9880d681SAndroid Build Coastguard Worker!52 = !DILexicalBlockFile(scope: !53, file: !1, discriminator: 5) 200*9880d681SAndroid Build Coastguard Worker!53 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 1) 201*9880d681SAndroid Build Coastguard Worker!54 = distinct !DILexicalBlock(scope: !48, file: !1, line: 10, column: 5) 202*9880d681SAndroid Build Coastguard Worker!55 = !DILocation(line: 10, column: 23, scope: !54) 203*9880d681SAndroid Build Coastguard Worker!56 = !DILocation(line: 10, column: 5, scope: !48) 204*9880d681SAndroid Build Coastguard Worker!57 = !DILocation(line: 10, column: 49, scope: !58) 205*9880d681SAndroid Build Coastguard Worker!58 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 2) 206*9880d681SAndroid Build Coastguard Worker!59 = !DILocation(line: 10, column: 45, scope: !54) 207*9880d681SAndroid Build Coastguard Worker!60 = !DILocation(line: 10, column: 42, scope: !54) 208*9880d681SAndroid Build Coastguard Worker!61 = !DILocation(line: 10, column: 38, scope: !54) 209*9880d681SAndroid Build Coastguard Worker!62 = !DILocation(line: 10, column: 34, scope: !63) 210*9880d681SAndroid Build Coastguard Worker!63 = !DILexicalBlockFile(scope: !54, file: !1, discriminator: 4) 211*9880d681SAndroid Build Coastguard Worker!64 = !DILocation(line: 10, column: 5, scope: !54) 212*9880d681SAndroid Build Coastguard Worker!65 = !DILocation(line: 10, column: 50, scope: !66) 213*9880d681SAndroid Build Coastguard Worker!66 = !DILexicalBlockFile(scope: !48, file: !1, discriminator: 3) 214*9880d681SAndroid Build Coastguard Worker!67 = !DILocation(line: 9, column: 30, scope: !44) 215*9880d681SAndroid Build Coastguard Worker!68 = !DILocation(line: 9, column: 3, scope: !44) 216*9880d681SAndroid Build Coastguard Worker!69 = !DILocation(line: 11, column: 10, scope: !9) 217*9880d681SAndroid Build Coastguard Worker!70 = !DILocation(line: 11, column: 14, scope: !9) 218*9880d681SAndroid Build Coastguard Worker!71 = !DILocation(line: 11, column: 3, scope: !9) 219