branch.ll 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/branch.prof | opt -analyze -branch-prob | FileCheck %s
  2. ; Original C++ code for this test case:
  3. ;
  4. ; #include <stdio.h>
  5. ; #include <stdlib.h>
  6. ;
  7. ; int main(int argc, char *argv[]) {
  8. ; if (argc < 2)
  9. ; return 1;
  10. ; double result;
  11. ; int limit = atoi(argv[1]);
  12. ; if (limit > 100) {
  13. ; double s = 23.041968;
  14. ; for (int u = 0; u < limit; u++) {
  15. ; double x = s;
  16. ; s = x + 3.049 + (double)u;
  17. ; s -= s + 3.94 / x * 0.32;
  18. ; }
  19. ; result = s;
  20. ; } else {
  21. ; result = 0;
  22. ; }
  23. ; printf("result is %lf\n", result);
  24. ; return 0;
  25. ; }
  26. @.str = private unnamed_addr constant [15 x i8] c"result is %lf\0A\00", align 1
  27. ; Function Attrs: nounwind uwtable
  28. define i32 @main(i32 %argc, i8** nocapture readonly %argv) #0 {
  29. ; CHECK: Printing analysis 'Branch Probability Analysis' for function 'main':
  30. entry:
  31. tail call void @llvm.dbg.value(metadata i32 %argc, i64 0, metadata !13, metadata !DIExpression()), !dbg !27
  32. tail call void @llvm.dbg.value(metadata i8** %argv, i64 0, metadata !14, metadata !DIExpression()), !dbg !27
  33. %cmp = icmp slt i32 %argc, 2, !dbg !28
  34. br i1 %cmp, label %return, label %if.end, !dbg !28
  35. ; CHECK: edge entry -> return probability is 0 / 1 = 0%
  36. ; CHECK: edge entry -> if.end probability is 1 / 1 = 100%
  37. if.end: ; preds = %entry
  38. %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1, !dbg !30
  39. %0 = load i8*, i8** %arrayidx, align 8, !dbg !30, !tbaa !31
  40. %call = tail call i32 @atoi(i8* %0) #4, !dbg !30
  41. tail call void @llvm.dbg.value(metadata i32 %call, i64 0, metadata !17, metadata !DIExpression()), !dbg !30
  42. %cmp1 = icmp sgt i32 %call, 100, !dbg !35
  43. br i1 %cmp1, label %for.body, label %if.end6, !dbg !35
  44. ; CHECK: edge if.end -> for.body probability is 0 / 1 = 0%
  45. ; CHECK: edge if.end -> if.end6 probability is 1 / 1 = 100%
  46. for.body: ; preds = %if.end, %for.body
  47. %u.016 = phi i32 [ %inc, %for.body ], [ 0, %if.end ]
  48. %s.015 = phi double [ %sub, %for.body ], [ 0x40370ABE6A337A81, %if.end ]
  49. %add = fadd double %s.015, 3.049000e+00, !dbg !36
  50. %conv = sitofp i32 %u.016 to double, !dbg !36
  51. %add4 = fadd double %add, %conv, !dbg !36
  52. tail call void @llvm.dbg.value(metadata double %add4, i64 0, metadata !18, metadata !DIExpression()), !dbg !36
  53. %div = fdiv double 3.940000e+00, %s.015, !dbg !37
  54. %mul = fmul double %div, 3.200000e-01, !dbg !37
  55. %add5 = fadd double %add4, %mul, !dbg !37
  56. %sub = fsub double %add4, %add5, !dbg !37
  57. tail call void @llvm.dbg.value(metadata double %sub, i64 0, metadata !18, metadata !DIExpression()), !dbg !37
  58. %inc = add nsw i32 %u.016, 1, !dbg !38
  59. tail call void @llvm.dbg.value(metadata i32 %inc, i64 0, metadata !21, metadata !DIExpression()), !dbg !38
  60. %exitcond = icmp eq i32 %inc, %call, !dbg !38
  61. br i1 %exitcond, label %if.end6, label %for.body, !dbg !38
  62. ; CHECK: edge for.body -> if.end6 probability is 0 / 10226 = 0%
  63. ; CHECK: edge for.body -> for.body probability is 10226 / 10226 = 100% [HOT edge]
  64. if.end6: ; preds = %for.body, %if.end
  65. %result.0 = phi double [ 0.000000e+00, %if.end ], [ %sub, %for.body ]
  66. %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
  67. br label %return, !dbg !40
  68. ; CHECK: edge if.end6 -> return probability is 16 / 16 = 100% [HOT edge]
  69. return: ; preds = %entry, %if.end6
  70. %retval.0 = phi i32 [ 0, %if.end6 ], [ 1, %entry ]
  71. ret i32 %retval.0, !dbg !41
  72. }
  73. ; Function Attrs: nounwind readonly
  74. declare i32 @atoi(i8* nocapture) #1
  75. ; Function Attrs: nounwind
  76. declare i32 @printf(i8* nocapture readonly, ...) #2
  77. ; Function Attrs: nounwind readnone
  78. declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #3
  79. attributes #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" }
  80. attributes #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" }
  81. attributes #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" }
  82. attributes #3 = { nounwind readnone }
  83. attributes #4 = { nounwind readonly }
  84. !llvm.dbg.cu = !{!0}
  85. !llvm.module.flags = !{!25, !42}
  86. !llvm.ident = !{!26}
  87. !0 = !DICompileUnit(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)
  88. !1 = !DIFile(filename: "branch.cc", directory: ".")
  89. !2 = !{}
  90. !3 = !{!4}
  91. !4 = !DISubprogram(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)
  92. !5 = !DIFile(filename: "branch.cc", directory: ".")
  93. !6 = !DISubroutineType(types: !7)
  94. !7 = !{!8, !8, !9}
  95. !8 = !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
  96. !9 = !DIDerivedType(tag: DW_TAG_pointer_type, size: 64, align: 64, baseType: !10)
  97. !10 = !DIDerivedType(tag: DW_TAG_pointer_type, size: 64, align: 64, baseType: !11)
  98. !11 = !DIBasicType(tag: DW_TAG_base_type, name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
  99. !12 = !{!13, !14, !15, !17, !18, !21, !23}
  100. !13 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "argc", line: 4, arg: 1, scope: !4, file: !5, type: !8)
  101. !14 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "argv", line: 4, arg: 2, scope: !4, file: !5, type: !9)
  102. !15 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "result", line: 7, scope: !4, file: !5, type: !16)
  103. !16 = !DIBasicType(tag: DW_TAG_base_type, name: "double", size: 64, align: 64, encoding: DW_ATE_float)
  104. !17 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "limit", line: 8, scope: !4, file: !5, type: !8)
  105. !18 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "s", line: 10, scope: !19, file: !5, type: !16)
  106. !19 = distinct !DILexicalBlock(line: 9, column: 0, file: !1, scope: !20)
  107. !20 = distinct !DILexicalBlock(line: 9, column: 0, file: !1, scope: !4)
  108. !21 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "u", line: 11, scope: !22, file: !5, type: !8)
  109. !22 = distinct !DILexicalBlock(line: 11, column: 0, file: !1, scope: !19)
  110. !23 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "x", line: 12, scope: !24, file: !5, type: !16)
  111. !24 = distinct !DILexicalBlock(line: 11, column: 0, file: !1, scope: !22)
  112. !25 = !{i32 2, !"Dwarf Version", i32 4}
  113. !26 = !{!"clang version 3.4 (trunk 192896) (llvm/trunk 192895)"}
  114. !27 = !DILocation(line: 4, scope: !4)
  115. !28 = !DILocation(line: 5, scope: !29)
  116. !29 = distinct !DILexicalBlock(line: 5, column: 0, file: !1, scope: !4)
  117. !30 = !DILocation(line: 8, scope: !4)
  118. !31 = !{!32, !32, i64 0}
  119. !32 = !{!"any pointer", !33, i64 0}
  120. !33 = !{!"omnipotent char", !34, i64 0}
  121. !34 = !{!"Simple C/C++ TBAA"}
  122. !35 = !DILocation(line: 9, scope: !20)
  123. !36 = !DILocation(line: 13, scope: !24)
  124. !37 = !DILocation(line: 14, scope: !24)
  125. !38 = !DILocation(line: 11, scope: !22)
  126. !39 = !DILocation(line: 20, scope: !4)
  127. !40 = !DILocation(line: 21, scope: !4)
  128. !41 = !DILocation(line: 22, scope: !4)
  129. !42 = !{i32 1, !"Debug Info Version", i32 3}