discriminator.ll 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/discriminator.prof | opt -analyze -branch-prob | FileCheck %s
  2. ; Original code
  3. ;
  4. ; 1 int foo(int i) {
  5. ; 2 int x = 0;
  6. ; 3 while (i < 100) {
  7. ; 4 if (i < 5) x--;
  8. ; 5 i++;
  9. ; 6 }
  10. ; 7 return x;
  11. ; 8 }
  12. ;
  13. ; In this test, if the loop is executed 100 times, the decrement operation
  14. ; at line 4 should only execute 5 times. This is reflected in the profile
  15. ; data for line offset 3. In Inputs/discriminator.prof, we have:
  16. ;
  17. ; 3: 100
  18. ; 3.1: 5
  19. ;
  20. ; This means that the predicate 'i < 5' (line 3) is executed 100 times,
  21. ; but the then branch (line 3.1) is only executed 5 times.
  22. define i32 @foo(i32 %i) #0 {
  23. ; CHECK: Printing analysis 'Branch Probability Analysis' for function 'foo':
  24. entry:
  25. %i.addr = alloca i32, align 4
  26. %x = alloca i32, align 4
  27. store i32 %i, i32* %i.addr, align 4
  28. store i32 0, i32* %x, align 4, !dbg !10
  29. br label %while.cond, !dbg !11
  30. while.cond: ; preds = %if.end, %entry
  31. %0 = load i32, i32* %i.addr, align 4, !dbg !12
  32. %cmp = icmp slt i32 %0, 100, !dbg !12
  33. br i1 %cmp, label %while.body, label %while.end, !dbg !12
  34. ; CHECK: edge while.cond -> while.body probability is 100 / 101 = 99.0099% [HOT edge]
  35. ; CHECK: edge while.cond -> while.end probability is 1 / 101 = 0.990099%
  36. while.body: ; preds = %while.cond
  37. %1 = load i32, i32* %i.addr, align 4, !dbg !14
  38. %cmp1 = icmp slt i32 %1, 50, !dbg !14
  39. br i1 %cmp1, label %if.then, label %if.end, !dbg !14
  40. ; CHECK: edge while.body -> if.then probability is 5 / 100 = 5%
  41. ; CHECK: edge while.body -> if.end probability is 95 / 100 = 95% [HOT edge]
  42. if.then: ; preds = %while.body
  43. %2 = load i32, i32* %x, align 4, !dbg !17
  44. %dec = add nsw i32 %2, -1, !dbg !17
  45. store i32 %dec, i32* %x, align 4, !dbg !17
  46. br label %if.end, !dbg !17
  47. if.end: ; preds = %if.then, %while.body
  48. %3 = load i32, i32* %i.addr, align 4, !dbg !19
  49. %inc = add nsw i32 %3, 1, !dbg !19
  50. store i32 %inc, i32* %i.addr, align 4, !dbg !19
  51. br label %while.cond, !dbg !20
  52. while.end: ; preds = %while.cond
  53. %4 = load i32, i32* %x, align 4, !dbg !21
  54. ret i32 %4, !dbg !21
  55. }
  56. !llvm.dbg.cu = !{!0}
  57. !llvm.module.flags = !{!7, !8}
  58. !llvm.ident = !{!9}
  59. !0 = !DICompileUnit(language: DW_LANG_C99, producer: "clang version 3.5 ", isOptimized: false, emissionKind: 0, file: !1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
  60. !1 = !DIFile(filename: "discriminator.c", directory: ".")
  61. !2 = !{}
  62. !3 = !{!4}
  63. !4 = !DISubprogram(name: "foo", line: 1, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, scopeLine: 1, file: !1, scope: !5, type: !6, function: i32 (i32)* @foo, variables: !2)
  64. !5 = !DIFile(filename: "discriminator.c", directory: ".")
  65. !6 = !DISubroutineType(types: !2)
  66. !7 = !{i32 2, !"Dwarf Version", i32 4}
  67. !8 = !{i32 1, !"Debug Info Version", i32 3}
  68. !9 = !{!"clang version 3.5 "}
  69. !10 = !DILocation(line: 2, scope: !4)
  70. !11 = !DILocation(line: 3, scope: !4)
  71. !12 = !DILocation(line: 3, scope: !13)
  72. !13 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !4)
  73. !14 = !DILocation(line: 4, scope: !15)
  74. !15 = distinct !DILexicalBlock(line: 4, column: 0, file: !1, scope: !16)
  75. !16 = distinct !DILexicalBlock(line: 3, column: 0, file: !1, scope: !4)
  76. !17 = !DILocation(line: 4, scope: !18)
  77. !18 = !DILexicalBlockFile(discriminator: 1, file: !1, scope: !15)
  78. !19 = !DILocation(line: 5, scope: !16)
  79. !20 = !DILocation(line: 6, scope: !16)
  80. !21 = !DILocation(line: 7, scope: !4)