basic.ll 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
  2. define i32 @test1(i32 %i, i32* %a) {
  3. ; CHECK: Printing analysis {{.*}} for function 'test1'
  4. entry:
  5. br label %body
  6. ; CHECK: edge entry -> body probability is 16 / 16 = 100%
  7. body:
  8. %iv = phi i32 [ 0, %entry ], [ %next, %body ]
  9. %base = phi i32 [ 0, %entry ], [ %sum, %body ]
  10. %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
  11. %0 = load i32, i32* %arrayidx
  12. %sum = add nsw i32 %0, %base
  13. %next = add i32 %iv, 1
  14. %exitcond = icmp eq i32 %next, %i
  15. br i1 %exitcond, label %exit, label %body
  16. ; CHECK: edge body -> exit probability is 4 / 128
  17. ; CHECK: edge body -> body probability is 124 / 128
  18. exit:
  19. ret i32 %sum
  20. }
  21. define i32 @test2(i32 %i, i32 %a, i32 %b) {
  22. ; CHECK: Printing analysis {{.*}} for function 'test2'
  23. entry:
  24. %cond = icmp ult i32 %i, 42
  25. br i1 %cond, label %then, label %else, !prof !0
  26. ; CHECK: edge entry -> then probability is 64 / 68
  27. ; CHECK: edge entry -> else probability is 4 / 68
  28. then:
  29. br label %exit
  30. ; CHECK: edge then -> exit probability is 16 / 16 = 100%
  31. else:
  32. br label %exit
  33. ; CHECK: edge else -> exit probability is 16 / 16 = 100%
  34. exit:
  35. %result = phi i32 [ %a, %then ], [ %b, %else ]
  36. ret i32 %result
  37. }
  38. !0 = !{!"branch_weights", i32 64, i32 4}
  39. define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
  40. ; CHECK: Printing analysis {{.*}} for function 'test3'
  41. entry:
  42. switch i32 %i, label %case_a [ i32 1, label %case_b
  43. i32 2, label %case_c
  44. i32 3, label %case_d
  45. i32 4, label %case_e ], !prof !1
  46. ; CHECK: edge entry -> case_a probability is 4 / 80
  47. ; CHECK: edge entry -> case_b probability is 4 / 80
  48. ; CHECK: edge entry -> case_c probability is 64 / 80
  49. ; CHECK: edge entry -> case_d probability is 4 / 80
  50. ; CHECK: edge entry -> case_e probability is 4 / 80
  51. case_a:
  52. br label %exit
  53. ; CHECK: edge case_a -> exit probability is 16 / 16 = 100%
  54. case_b:
  55. br label %exit
  56. ; CHECK: edge case_b -> exit probability is 16 / 16 = 100%
  57. case_c:
  58. br label %exit
  59. ; CHECK: edge case_c -> exit probability is 16 / 16 = 100%
  60. case_d:
  61. br label %exit
  62. ; CHECK: edge case_d -> exit probability is 16 / 16 = 100%
  63. case_e:
  64. br label %exit
  65. ; CHECK: edge case_e -> exit probability is 16 / 16 = 100%
  66. exit:
  67. %result = phi i32 [ %a, %case_a ],
  68. [ %b, %case_b ],
  69. [ %c, %case_c ],
  70. [ %d, %case_d ],
  71. [ %e, %case_e ]
  72. ret i32 %result
  73. }
  74. !1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
  75. define i32 @test4(i32 %x) nounwind uwtable readnone ssp {
  76. ; CHECK: Printing analysis {{.*}} for function 'test4'
  77. entry:
  78. %conv = sext i32 %x to i64
  79. switch i64 %conv, label %return [
  80. i64 0, label %sw.bb
  81. i64 1, label %sw.bb
  82. i64 2, label %sw.bb
  83. i64 5, label %sw.bb1
  84. ], !prof !2
  85. ; CHECK: edge entry -> return probability is 7 / 85
  86. ; CHECK: edge entry -> sw.bb probability is 14 / 85
  87. ; CHECK: edge entry -> sw.bb1 probability is 64 / 85
  88. sw.bb:
  89. br label %return
  90. sw.bb1:
  91. br label %return
  92. return:
  93. %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ]
  94. ret i32 %retval.0
  95. }
  96. !2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64}
  97. declare void @coldfunc() cold
  98. define i32 @test5(i32 %a, i32 %b, i1 %flag) {
  99. ; CHECK: Printing analysis {{.*}} for function 'test5'
  100. entry:
  101. br i1 %flag, label %then, label %else
  102. ; CHECK: edge entry -> then probability is 4 / 68
  103. ; CHECK: edge entry -> else probability is 64 / 68
  104. then:
  105. call void @coldfunc()
  106. br label %exit
  107. ; CHECK: edge then -> exit probability is 16 / 16 = 100%
  108. else:
  109. br label %exit
  110. ; CHECK: edge else -> exit probability is 16 / 16 = 100%
  111. exit:
  112. %result = phi i32 [ %a, %then ], [ %b, %else ]
  113. ret i32 %result
  114. }
  115. declare i32 @regular_function(i32 %i)
  116. define i32 @test_cold_call_sites(i32* %a) {
  117. ; Test that edges to blocks post-dominated by cold call sites
  118. ; are marked as not expected to be taken.
  119. ; TODO(dnovillo) The calls to regular_function should not be merged, but
  120. ; they are currently being merged. Convert this into a code generation test
  121. ; after that is fixed.
  122. ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
  123. ; CHECK: edge entry -> then probability is 4 / 68 = 5.88235%
  124. ; CHECK: edge entry -> else probability is 64 / 68 = 94.1176% [HOT edge]
  125. entry:
  126. %gep1 = getelementptr i32, i32* %a, i32 1
  127. %val1 = load i32, i32* %gep1
  128. %cond1 = icmp ugt i32 %val1, 1
  129. br i1 %cond1, label %then, label %else
  130. then:
  131. ; This function is not declared cold, but this call site is.
  132. %val4 = call i32 @regular_function(i32 %val1) cold
  133. br label %exit
  134. else:
  135. %gep2 = getelementptr i32, i32* %a, i32 2
  136. %val2 = load i32, i32* %gep2
  137. %val3 = call i32 @regular_function(i32 %val2)
  138. br label %exit
  139. exit:
  140. %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
  141. ret i32 %ret
  142. }
  143. define i32 @zero1(i32 %i, i32 %a, i32 %b) {
  144. ; CHECK: Printing analysis {{.*}} for function 'zero1'
  145. entry:
  146. %cond = icmp eq i32 %i, 0
  147. br i1 %cond, label %then, label %else
  148. ; CHECK: edge entry -> then probability is 12 / 32
  149. ; CHECK: edge entry -> else probability is 20 / 32
  150. then:
  151. br label %exit
  152. else:
  153. br label %exit
  154. exit:
  155. %result = phi i32 [ %a, %then ], [ %b, %else ]
  156. ret i32 %result
  157. }
  158. define i32 @zero2(i32 %i, i32 %a, i32 %b) {
  159. ; CHECK: Printing analysis {{.*}} for function 'zero2'
  160. entry:
  161. %cond = icmp ne i32 %i, -1
  162. br i1 %cond, label %then, label %else
  163. ; CHECK: edge entry -> then probability is 20 / 32
  164. ; CHECK: edge entry -> else probability is 12 / 32
  165. then:
  166. br label %exit
  167. else:
  168. br label %exit
  169. exit:
  170. %result = phi i32 [ %a, %then ], [ %b, %else ]
  171. ret i32 %result
  172. }
  173. define i32 @zero3(i32 %i, i32 %a, i32 %b) {
  174. ; CHECK: Printing analysis {{.*}} for function 'zero3'
  175. entry:
  176. ; AND'ing with a single bit bitmask essentially leads to a bool comparison,
  177. ; meaning we don't have probability information.
  178. %and = and i32 %i, 2
  179. %tobool = icmp eq i32 %and, 0
  180. br i1 %tobool, label %then, label %else
  181. ; CHECK: edge entry -> then probability is 16 / 32
  182. ; CHECK: edge entry -> else probability is 16 / 32
  183. then:
  184. ; AND'ing with other bitmask might be something else, so we still assume the
  185. ; usual probabilities.
  186. %and2 = and i32 %i, 5
  187. %tobool2 = icmp eq i32 %and2, 0
  188. br i1 %tobool2, label %else, label %exit
  189. ; CHECK: edge then -> else probability is 12 / 32
  190. ; CHECK: edge then -> exit probability is 20 / 32
  191. else:
  192. br label %exit
  193. exit:
  194. %result = phi i32 [ %a, %then ], [ %b, %else ]
  195. ret i32 %result
  196. }