1*9880d681SAndroid Build Coastguard Worker; The two profiles used in this test are the same but encoded in different 2*9880d681SAndroid Build Coastguard Worker; formats. This checks that we produce the same profile annotations regardless 3*9880d681SAndroid Build Coastguard Worker; of the profile format. 4*9880d681SAndroid Build Coastguard Worker; 5*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/fnptr.prof | opt -analyze -branch-prob | FileCheck %s 6*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/fnptr.binprof | opt -analyze -branch-prob | FileCheck %s 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/fnptr.prof | opt -analyze -branch-prob | FileCheck %s 9*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/fnptr.binprof | opt -analyze -branch-prob | FileCheck %s 10*9880d681SAndroid Build Coastguard Worker 11*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.body3 -> if.then probability is 0x1a4f3959 / 0x80000000 = 20.55% 12*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.body3 -> if.else probability is 0x65b0c6a7 / 0x80000000 = 79.45% 13*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.inc -> for.inc12 probability is 0x20dc8dc9 / 0x80000000 = 25.67% 14*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.inc -> for.body3 probability is 0x5f237237 / 0x80000000 = 74.33% 15*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.inc12 -> for.end14 probability is 0x00000000 / 0x80000000 = 0.00% 16*9880d681SAndroid Build Coastguard Worker; CHECK: edge for.inc12 -> for.cond1.preheader probability is 0x80000000 / 0x80000000 = 100.00% 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard Worker; Original C++ test case. 19*9880d681SAndroid Build Coastguard Worker; 20*9880d681SAndroid Build Coastguard Worker; #include <stdlib.h> 21*9880d681SAndroid Build Coastguard Worker; #include <math.h> 22*9880d681SAndroid Build Coastguard Worker; #include <stdio.h> 23*9880d681SAndroid Build Coastguard Worker; 24*9880d681SAndroid Build Coastguard Worker; #define N 10000 25*9880d681SAndroid Build Coastguard Worker; #define M 6000 26*9880d681SAndroid Build Coastguard Worker; 27*9880d681SAndroid Build Coastguard Worker; double foo(int x) { 28*9880d681SAndroid Build Coastguard Worker; return x * sin((double)x); 29*9880d681SAndroid Build Coastguard Worker; } 30*9880d681SAndroid Build Coastguard Worker; 31*9880d681SAndroid Build Coastguard Worker; double bar(int x) { 32*9880d681SAndroid Build Coastguard Worker; return x - cos((double)x); 33*9880d681SAndroid Build Coastguard Worker; } 34*9880d681SAndroid Build Coastguard Worker; 35*9880d681SAndroid Build Coastguard Worker; int main() { 36*9880d681SAndroid Build Coastguard Worker; double (*fptr)(int); 37*9880d681SAndroid Build Coastguard Worker; double S = 0; 38*9880d681SAndroid Build Coastguard Worker; for (int i = 0; i < N; i++) 39*9880d681SAndroid Build Coastguard Worker; for (int j = 0; j < M; j++) { 40*9880d681SAndroid Build Coastguard Worker; fptr = (rand() % 100 < 30) ? foo : bar; 41*9880d681SAndroid Build Coastguard Worker; if (rand() % 100 < 10) 42*9880d681SAndroid Build Coastguard Worker; S += (*fptr)(i + j * 300); 43*9880d681SAndroid Build Coastguard Worker; else 44*9880d681SAndroid Build Coastguard Worker; S += (*fptr)(i - j / 840); 45*9880d681SAndroid Build Coastguard Worker; } 46*9880d681SAndroid Build Coastguard Worker; printf("S = %lf\n", S); 47*9880d681SAndroid Build Coastguard Worker; return 0; 48*9880d681SAndroid Build Coastguard Worker; } 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard Worker@.str = private unnamed_addr constant [9 x i8] c"S = %lf\0A\00", align 1 51*9880d681SAndroid Build Coastguard Worker 52*9880d681SAndroid Build Coastguard Workerdefine double @_Z3fooi(i32 %x) #0 !dbg !3 { 53*9880d681SAndroid Build Coastguard Workerentry: 54*9880d681SAndroid Build Coastguard Worker %conv = sitofp i32 %x to double, !dbg !2 55*9880d681SAndroid Build Coastguard Worker %call = tail call double @sin(double %conv) #3, !dbg !8 56*9880d681SAndroid Build Coastguard Worker %mul = fmul double %conv, %call, !dbg !8 57*9880d681SAndroid Build Coastguard Worker ret double %mul, !dbg !8 58*9880d681SAndroid Build Coastguard Worker} 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard Workerdeclare double @sin(double) #1 61*9880d681SAndroid Build Coastguard Worker 62*9880d681SAndroid Build Coastguard Workerdefine double @_Z3bari(i32 %x) #0 !dbg !10 { 63*9880d681SAndroid Build Coastguard Workerentry: 64*9880d681SAndroid Build Coastguard Worker %conv = sitofp i32 %x to double, !dbg !9 65*9880d681SAndroid Build Coastguard Worker %call = tail call double @cos(double %conv) #3, !dbg !11 66*9880d681SAndroid Build Coastguard Worker %sub = fsub double %conv, %call, !dbg !11 67*9880d681SAndroid Build Coastguard Worker ret double %sub, !dbg !11 68*9880d681SAndroid Build Coastguard Worker} 69*9880d681SAndroid Build Coastguard Worker 70*9880d681SAndroid Build Coastguard Workerdeclare double @cos(double) #1 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard Workerdefine i32 @main() #2 !dbg !13 { 73*9880d681SAndroid Build Coastguard Workerentry: 74*9880d681SAndroid Build Coastguard Worker br label %for.cond1.preheader, !dbg !12 75*9880d681SAndroid Build Coastguard Worker 76*9880d681SAndroid Build Coastguard Workerfor.cond1.preheader: ; preds = %for.inc12, %entry 77*9880d681SAndroid Build Coastguard Worker %i.025 = phi i32 [ 0, %entry ], [ %inc13, %for.inc12 ] 78*9880d681SAndroid Build Coastguard Worker %S.024 = phi double [ 0.000000e+00, %entry ], [ %S.2.lcssa, %for.inc12 ] 79*9880d681SAndroid Build Coastguard Worker br label %for.body3, !dbg !14 80*9880d681SAndroid Build Coastguard Worker 81*9880d681SAndroid Build Coastguard Workerfor.body3: ; preds = %for.inc, %for.cond1.preheader 82*9880d681SAndroid Build Coastguard Worker %j.023 = phi i32 [ 0, %for.cond1.preheader ], [ %inc, %for.inc ] 83*9880d681SAndroid Build Coastguard Worker %S.122 = phi double [ %S.024, %for.cond1.preheader ], [ %S.2, %for.inc ] 84*9880d681SAndroid Build Coastguard Worker %call = tail call i32 @rand() #3, !dbg !15 85*9880d681SAndroid Build Coastguard Worker %rem = srem i32 %call, 100, !dbg !15 86*9880d681SAndroid Build Coastguard Worker %cmp4 = icmp slt i32 %rem, 30, !dbg !15 87*9880d681SAndroid Build Coastguard Worker %_Z3fooi._Z3bari = select i1 %cmp4, double (i32)* @_Z3fooi, double (i32)* @_Z3bari, !dbg !15 88*9880d681SAndroid Build Coastguard Worker %call5 = tail call i32 @rand() #3, !dbg !16 89*9880d681SAndroid Build Coastguard Worker %rem6 = srem i32 %call5, 100, !dbg !16 90*9880d681SAndroid Build Coastguard Worker %cmp7 = icmp slt i32 %rem6, 10, !dbg !16 91*9880d681SAndroid Build Coastguard Worker br i1 %cmp7, label %if.then, label %if.else, !dbg !16, !prof !17 92*9880d681SAndroid Build Coastguard Worker 93*9880d681SAndroid Build Coastguard Workerif.then: ; preds = %for.body3 94*9880d681SAndroid Build Coastguard Worker %mul = mul nsw i32 %j.023, 300, !dbg !18 95*9880d681SAndroid Build Coastguard Worker %add = add nsw i32 %mul, %i.025, !dbg !18 96*9880d681SAndroid Build Coastguard Worker %call8 = tail call double %_Z3fooi._Z3bari(i32 %add), !dbg !18 97*9880d681SAndroid Build Coastguard Worker br label %for.inc, !dbg !18 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Workerif.else: ; preds = %for.body3 100*9880d681SAndroid Build Coastguard Worker %div = sdiv i32 %j.023, 840, !dbg !19 101*9880d681SAndroid Build Coastguard Worker %sub = sub nsw i32 %i.025, %div, !dbg !19 102*9880d681SAndroid Build Coastguard Worker %call10 = tail call double %_Z3fooi._Z3bari(i32 %sub), !dbg !19 103*9880d681SAndroid Build Coastguard Worker br label %for.inc 104*9880d681SAndroid Build Coastguard Worker 105*9880d681SAndroid Build Coastguard Workerfor.inc: ; preds = %if.then, %if.else 106*9880d681SAndroid Build Coastguard Worker %call8.pn = phi double [ %call8, %if.then ], [ %call10, %if.else ] 107*9880d681SAndroid Build Coastguard Worker %S.2 = fadd double %S.122, %call8.pn, !dbg !18 108*9880d681SAndroid Build Coastguard Worker %inc = add nsw i32 %j.023, 1, !dbg !20 109*9880d681SAndroid Build Coastguard Worker %exitcond = icmp eq i32 %j.023, 5999, !dbg !14 110*9880d681SAndroid Build Coastguard Worker br i1 %exitcond, label %for.inc12, label %for.body3, !dbg !14, !prof !21 111*9880d681SAndroid Build Coastguard Worker 112*9880d681SAndroid Build Coastguard Workerfor.inc12: ; preds = %for.inc 113*9880d681SAndroid Build Coastguard Worker %S.2.lcssa = phi double [ %S.2, %for.inc ] 114*9880d681SAndroid Build Coastguard Worker %inc13 = add nsw i32 %i.025, 1, !dbg !22 115*9880d681SAndroid Build Coastguard Worker %exitcond26 = icmp eq i32 %i.025, 9999, !dbg !12 116*9880d681SAndroid Build Coastguard Worker br i1 %exitcond26, label %for.end14, label %for.cond1.preheader, !dbg !12, !prof !23 117*9880d681SAndroid Build Coastguard Worker 118*9880d681SAndroid Build Coastguard Workerfor.end14: ; preds = %for.inc12 119*9880d681SAndroid Build Coastguard Worker %S.2.lcssa.lcssa = phi double [ %S.2.lcssa, %for.inc12 ] 120*9880d681SAndroid Build Coastguard Worker %call15 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %S.2.lcssa.lcssa), !dbg !24 121*9880d681SAndroid Build Coastguard Worker ret i32 0, !dbg !25 122*9880d681SAndroid Build Coastguard Worker} 123*9880d681SAndroid Build Coastguard Worker 124*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind 125*9880d681SAndroid Build Coastguard Workerdeclare i32 @rand() #1 126*9880d681SAndroid Build Coastguard Worker 127*9880d681SAndroid Build Coastguard Worker; Function Attrs: nounwind 128*9880d681SAndroid Build Coastguard Workerdeclare i32 @printf(i8* nocapture readonly, ...) #1 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard Worker!llvm.module.flags = !{!0} 131*9880d681SAndroid Build Coastguard Worker!llvm.ident = !{!1} 132*9880d681SAndroid Build Coastguard Worker!llvm.dbg.cu = !{!26} 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard Worker!0 = !{i32 2, !"Debug Info Version", i32 3} 135*9880d681SAndroid Build Coastguard Worker!1 = !{!"clang version 3.6.0 "} 136*9880d681SAndroid Build Coastguard Worker!2 = !DILocation(line: 9, column: 3, scope: !3) 137*9880d681SAndroid Build Coastguard Worker!3 = distinct !DISubprogram(name: "foo", line: 8, isLocal: false, isDefinition: true, flags: DIFlagPrototyped, isOptimized: true, unit: !26, scopeLine: 8, file: !4, scope: !5, type: !6, variables: !7) 138*9880d681SAndroid Build Coastguard Worker!4 = !DIFile(filename: "fnptr.cc", directory: ".") 139*9880d681SAndroid Build Coastguard Worker!5 = !DIFile(filename: "fnptr.cc", directory: ".") 140*9880d681SAndroid Build Coastguard Worker!6 = !DISubroutineType(types: !7) 141*9880d681SAndroid Build Coastguard Worker!7 = !{} 142*9880d681SAndroid Build Coastguard Worker!8 = !DILocation(line: 9, column: 14, scope: !3) 143*9880d681SAndroid Build Coastguard Worker!9 = !DILocation(line: 13, column: 3, scope: !10) 144*9880d681SAndroid Build Coastguard Worker!10 = distinct !DISubprogram(name: "bar", line: 12, isLocal: false, isDefinition: true, flags: DIFlagPrototyped, isOptimized: true, unit: !26, scopeLine: 12, file: !4, scope: !5, type: !6, variables: !7) 145*9880d681SAndroid Build Coastguard Worker!11 = !DILocation(line: 13, column: 14, scope: !10) 146*9880d681SAndroid Build Coastguard Worker!12 = !DILocation(line: 19, column: 3, scope: !13) 147*9880d681SAndroid Build Coastguard Worker!13 = distinct !DISubprogram(name: "main", line: 16, isLocal: false, isDefinition: true, flags: DIFlagPrototyped, isOptimized: true, unit: !26, scopeLine: 16, file: !4, scope: !5, type: !6, variables: !7) 148*9880d681SAndroid Build Coastguard Worker!14 = !DILocation(line: 20, column: 5, scope: !13) 149*9880d681SAndroid Build Coastguard Worker!15 = !DILocation(line: 21, column: 15, scope: !13) 150*9880d681SAndroid Build Coastguard Worker!16 = !DILocation(line: 22, column: 11, scope: !13) 151*9880d681SAndroid Build Coastguard Worker!17 = !{!"branch_weights", i32 534, i32 2064} 152*9880d681SAndroid Build Coastguard Worker!18 = !DILocation(line: 23, column: 14, scope: !13) 153*9880d681SAndroid Build Coastguard Worker!19 = !DILocation(line: 25, column: 14, scope: !13) 154*9880d681SAndroid Build Coastguard Worker!20 = !DILocation(line: 20, column: 28, scope: !13) 155*9880d681SAndroid Build Coastguard Worker!21 = !{!"branch_weights", i32 0, i32 1075} 156*9880d681SAndroid Build Coastguard Worker!22 = !DILocation(line: 19, column: 26, scope: !13) 157*9880d681SAndroid Build Coastguard Worker!23 = !{!"branch_weights", i32 0, i32 534} 158*9880d681SAndroid Build Coastguard Worker!24 = !DILocation(line: 27, column: 3, scope: !13) 159*9880d681SAndroid Build Coastguard Worker!25 = !DILocation(line: 28, column: 3, scope: !13) 160*9880d681SAndroid Build Coastguard Worker!26 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: FullDebug, file: !4) 161