propagate.ll 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/propagate.prof | opt -analyze -branch-prob | FileCheck %s
  2. ; Original C++ code for this test case:
  3. ;
  4. ; #include <stdio.h>
  5. ;
  6. ; long foo(int x, int y, long N) {
  7. ; if (x < y) {
  8. ; return y - x;
  9. ; } else {
  10. ; for (long i = 0; i < N; i++) {
  11. ; if (i > N / 3)
  12. ; x--;
  13. ; if (i > N / 4) {
  14. ; y++;
  15. ; x += 3;
  16. ; } else {
  17. ; for (unsigned j = 0; j < i; j++) {
  18. ; x += j;
  19. ; y -= 3;
  20. ; }
  21. ; }
  22. ; }
  23. ; }
  24. ; return y * x;
  25. ; }
  26. ;
  27. ; int main() {
  28. ; int x = 5678;
  29. ; int y = 1234;
  30. ; long N = 999999;
  31. ; printf("foo(%d, %d, %ld) = %ld\n", x, y, N, foo(x, y, N));
  32. ; return 0;
  33. ; }
  34. ; ModuleID = 'propagate.cc'
  35. target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
  36. target triple = "x86_64-unknown-linux-gnu"
  37. @.str = private unnamed_addr constant [24 x i8] c"foo(%d, %d, %ld) = %ld\0A\00", align 1
  38. ; Function Attrs: nounwind uwtable
  39. define i64 @_Z3fooiil(i32 %x, i32 %y, i64 %N) #0 {
  40. entry:
  41. %retval = alloca i64, align 8
  42. %x.addr = alloca i32, align 4
  43. %y.addr = alloca i32, align 4
  44. %N.addr = alloca i64, align 8
  45. %i = alloca i64, align 8
  46. %j = alloca i32, align 4
  47. store i32 %x, i32* %x.addr, align 4
  48. store i32 %y, i32* %y.addr, align 4
  49. store i64 %N, i64* %N.addr, align 8
  50. %0 = load i32, i32* %x.addr, align 4, !dbg !11
  51. %1 = load i32, i32* %y.addr, align 4, !dbg !11
  52. %cmp = icmp slt i32 %0, %1, !dbg !11
  53. br i1 %cmp, label %if.then, label %if.else, !dbg !11
  54. if.then: ; preds = %entry
  55. %2 = load i32, i32* %y.addr, align 4, !dbg !13
  56. %3 = load i32, i32* %x.addr, align 4, !dbg !13
  57. %sub = sub nsw i32 %2, %3, !dbg !13
  58. %conv = sext i32 %sub to i64, !dbg !13
  59. store i64 %conv, i64* %retval, !dbg !13
  60. br label %return, !dbg !13
  61. if.else: ; preds = %entry
  62. store i64 0, i64* %i, align 8, !dbg !15
  63. br label %for.cond, !dbg !15
  64. for.cond: ; preds = %for.inc16, %if.else
  65. %4 = load i64, i64* %i, align 8, !dbg !15
  66. %5 = load i64, i64* %N.addr, align 8, !dbg !15
  67. %cmp1 = icmp slt i64 %4, %5, !dbg !15
  68. br i1 %cmp1, label %for.body, label %for.end18, !dbg !15
  69. ; CHECK: edge for.cond -> for.body probability is 10 / 10 = 100% [HOT edge]
  70. ; CHECK: edge for.cond -> for.end18 probability is 0 / 10 = 0%
  71. for.body: ; preds = %for.cond
  72. %6 = load i64, i64* %i, align 8, !dbg !18
  73. %7 = load i64, i64* %N.addr, align 8, !dbg !18
  74. %div = sdiv i64 %7, 3, !dbg !18
  75. %cmp2 = icmp sgt i64 %6, %div, !dbg !18
  76. br i1 %cmp2, label %if.then3, label %if.end, !dbg !18
  77. ; CHECK: edge for.body -> if.then3 probability is 1 / 5 = 20%
  78. ; CHECK: edge for.body -> if.end probability is 4 / 5 = 80%
  79. if.then3: ; preds = %for.body
  80. %8 = load i32, i32* %x.addr, align 4, !dbg !21
  81. %dec = add nsw i32 %8, -1, !dbg !21
  82. store i32 %dec, i32* %x.addr, align 4, !dbg !21
  83. br label %if.end, !dbg !21
  84. if.end: ; preds = %if.then3, %for.body
  85. %9 = load i64, i64* %i, align 8, !dbg !22
  86. %10 = load i64, i64* %N.addr, align 8, !dbg !22
  87. %div4 = sdiv i64 %10, 4, !dbg !22
  88. %cmp5 = icmp sgt i64 %9, %div4, !dbg !22
  89. br i1 %cmp5, label %if.then6, label %if.else7, !dbg !22
  90. ; CHECK: edge if.end -> if.then6 probability is 3 / 6342 = 0.0473037%
  91. ; CHECK: edge if.end -> if.else7 probability is 6339 / 6342 = 99.9527% [HOT edge]
  92. if.then6: ; preds = %if.end
  93. %11 = load i32, i32* %y.addr, align 4, !dbg !24
  94. %inc = add nsw i32 %11, 1, !dbg !24
  95. store i32 %inc, i32* %y.addr, align 4, !dbg !24
  96. %12 = load i32, i32* %x.addr, align 4, !dbg !26
  97. %add = add nsw i32 %12, 3, !dbg !26
  98. store i32 %add, i32* %x.addr, align 4, !dbg !26
  99. br label %if.end15, !dbg !27
  100. if.else7: ; preds = %if.end
  101. store i32 0, i32* %j, align 4, !dbg !28
  102. br label %for.cond8, !dbg !28
  103. for.cond8: ; preds = %for.inc, %if.else7
  104. %13 = load i32, i32* %j, align 4, !dbg !28
  105. %conv9 = zext i32 %13 to i64, !dbg !28
  106. %14 = load i64, i64* %i, align 8, !dbg !28
  107. %cmp10 = icmp slt i64 %conv9, %14, !dbg !28
  108. br i1 %cmp10, label %for.body11, label %for.end, !dbg !28
  109. ; CHECK: edge for.cond8 -> for.body11 probability is 16191 / 16191 = 100% [HOT edge]
  110. ; CHECK: edge for.cond8 -> for.end probability is 0 / 16191 = 0%
  111. for.body11: ; preds = %for.cond8
  112. %15 = load i32, i32* %j, align 4, !dbg !31
  113. %16 = load i32, i32* %x.addr, align 4, !dbg !31
  114. %add12 = add i32 %16, %15, !dbg !31
  115. store i32 %add12, i32* %x.addr, align 4, !dbg !31
  116. %17 = load i32, i32* %y.addr, align 4, !dbg !33
  117. %sub13 = sub nsw i32 %17, 3, !dbg !33
  118. store i32 %sub13, i32* %y.addr, align 4, !dbg !33
  119. br label %for.inc, !dbg !34
  120. for.inc: ; preds = %for.body11
  121. %18 = load i32, i32* %j, align 4, !dbg !28
  122. %inc14 = add i32 %18, 1, !dbg !28
  123. store i32 %inc14, i32* %j, align 4, !dbg !28
  124. br label %for.cond8, !dbg !28
  125. for.end: ; preds = %for.cond8
  126. br label %if.end15
  127. if.end15: ; preds = %for.end, %if.then6
  128. br label %for.inc16, !dbg !35
  129. for.inc16: ; preds = %if.end15
  130. %19 = load i64, i64* %i, align 8, !dbg !15
  131. %inc17 = add nsw i64 %19, 1, !dbg !15
  132. store i64 %inc17, i64* %i, align 8, !dbg !15
  133. br label %for.cond, !dbg !15
  134. for.end18: ; preds = %for.cond
  135. br label %if.end19
  136. if.end19: ; preds = %for.end18
  137. %20 = load i32, i32* %y.addr, align 4, !dbg !36
  138. %21 = load i32, i32* %x.addr, align 4, !dbg !36
  139. %mul = mul nsw i32 %20, %21, !dbg !36
  140. %conv20 = sext i32 %mul to i64, !dbg !36
  141. store i64 %conv20, i64* %retval, !dbg !36
  142. br label %return, !dbg !36
  143. return: ; preds = %if.end19, %if.then
  144. %22 = load i64, i64* %retval, !dbg !37
  145. ret i64 %22, !dbg !37
  146. }
  147. ; Function Attrs: uwtable
  148. define i32 @main() #1 {
  149. entry:
  150. %retval = alloca i32, align 4
  151. %x = alloca i32, align 4
  152. %y = alloca i32, align 4
  153. %N = alloca i64, align 8
  154. store i32 0, i32* %retval
  155. store i32 5678, i32* %x, align 4, !dbg !38
  156. store i32 1234, i32* %y, align 4, !dbg !39
  157. store i64 999999, i64* %N, align 8, !dbg !40
  158. %0 = load i32, i32* %x, align 4, !dbg !41
  159. %1 = load i32, i32* %y, align 4, !dbg !41
  160. %2 = load i64, i64* %N, align 8, !dbg !41
  161. %3 = load i32, i32* %x, align 4, !dbg !41
  162. %4 = load i32, i32* %y, align 4, !dbg !41
  163. %5 = load i64, i64* %N, align 8, !dbg !41
  164. %call = call i64 @_Z3fooiil(i32 %3, i32 %4, i64 %5), !dbg !41
  165. %call1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i32 0, i32 0), i32 %0, i32 %1, i64 %2, i64 %call), !dbg !41
  166. ret i32 0, !dbg !42
  167. }
  168. declare i32 @printf(i8*, ...) #2
  169. attributes #0 = { nounwind uwtable "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" "unsafe-fp-math"="false" "use-soft-float"="false" }
  170. attributes #1 = { uwtable "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" "unsafe-fp-math"="false" "use-soft-float"="false" }
  171. attributes #2 = { "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" "unsafe-fp-math"="false" "use-soft-float"="false" }
  172. !llvm.dbg.cu = !{!0}
  173. !llvm.module.flags = !{!8, !9}
  174. !llvm.ident = !{!10}
  175. !0 = !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: 0, file: !1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2)
  176. !1 = !DIFile(filename: "propagate.cc", directory: ".")
  177. !2 = !{}
  178. !3 = !{!4, !7}
  179. !4 = !DISubprogram(name: "foo", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, scopeLine: 3, file: !1, scope: !5, type: !6, function: i64 (i32, i32, i64)* @_Z3fooiil, variables: !2)
  180. !5 = !DIFile(filename: "propagate.cc", directory: ".")
  181. !6 = !DISubroutineType(types: !{null})
  182. !7 = !DISubprogram(name: "main", line: 24, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, scopeLine: 24, file: !1, scope: !5, type: !6, function: i32 ()* @main, variables: !2)
  183. !8 = !{i32 2, !"Dwarf Version", i32 4}
  184. !9 = !{i32 1, !"Debug Info Version", i32 3}
  185. !10 = !{!"clang version 3.5 "}
  186. !11 = !DILocation(line: 4, scope: !12)
  187. !12 = distinct !DILexicalBlock(line: 4, column: 0, file: !1, scope: !4)
  188. !13 = !DILocation(line: 5, scope: !14)
  189. !14 = distinct !DILexicalBlock(line: 4, column: 0, file: !1, scope: !12)
  190. !15 = !DILocation(line: 7, scope: !16)
  191. !16 = distinct !DILexicalBlock(line: 7, column: 0, file: !1, scope: !17)
  192. !17 = distinct !DILexicalBlock(line: 6, column: 0, file: !1, scope: !12)
  193. !18 = !DILocation(line: 8, scope: !19)
  194. !19 = distinct !DILexicalBlock(line: 8, column: 0, file: !1, scope: !20)
  195. !20 = distinct !DILexicalBlock(line: 7, column: 0, file: !1, scope: !16)
  196. !21 = !DILocation(line: 9, scope: !19)
  197. !22 = !DILocation(line: 10, scope: !23)
  198. !23 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !20)
  199. !24 = !DILocation(line: 11, scope: !25)
  200. !25 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !23)
  201. !26 = !DILocation(line: 12, scope: !25)
  202. !27 = !DILocation(line: 13, scope: !25)
  203. !28 = !DILocation(line: 14, scope: !29)
  204. !29 = distinct !DILexicalBlock(line: 14, column: 0, file: !1, scope: !30)
  205. !30 = distinct !DILexicalBlock(line: 13, column: 0, file: !1, scope: !23)
  206. !31 = !DILocation(line: 15, scope: !32)
  207. !32 = distinct !DILexicalBlock(line: 14, column: 0, file: !1, scope: !29)
  208. !33 = !DILocation(line: 16, scope: !32)
  209. !34 = !DILocation(line: 17, scope: !32)
  210. !35 = !DILocation(line: 19, scope: !20)
  211. !36 = !DILocation(line: 21, scope: !4)
  212. !37 = !DILocation(line: 22, scope: !4)
  213. !38 = !DILocation(line: 25, scope: !7)
  214. !39 = !DILocation(line: 26, scope: !7)
  215. !40 = !DILocation(line: 27, scope: !7)
  216. !41 = !DILocation(line: 28, scope: !7)
  217. !42 = !DILocation(line: 29, scope: !7)