fnptr.ll 6.2 KB

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