irreducible.ll 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. ; RUN: opt < %s -analyze -block-freq | FileCheck %s
  2. ; A loop with multiple exits isn't irreducible. It should be handled
  3. ; correctly.
  4. ;
  5. ; CHECK-LABEL: Printing analysis {{.*}} for function 'multiexit':
  6. ; CHECK-NEXT: block-frequency-info: multiexit
  7. define void @multiexit(i1 %x) {
  8. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  9. entry:
  10. br label %loop.1
  11. ; CHECK-NEXT: loop.1: float = 2.0,
  12. loop.1:
  13. br i1 %x, label %exit.1, label %loop.2, !prof !0
  14. ; CHECK-NEXT: loop.2: float = 1.75,
  15. loop.2:
  16. br i1 %x, label %exit.2, label %loop.1, !prof !1
  17. ; CHECK-NEXT: exit.1: float = 0.25,
  18. exit.1:
  19. br label %return
  20. ; CHECK-NEXT: exit.2: float = 0.75,
  21. exit.2:
  22. br label %return
  23. ; CHECK-NEXT: return: float = 1.0, int = [[ENTRY]]
  24. return:
  25. ret void
  26. }
  27. !0 = !{!"branch_weights", i32 1, i32 7}
  28. !1 = !{!"branch_weights", i32 3, i32 4}
  29. ; Irreducible control flow
  30. ; ========================
  31. ;
  32. ; LoopInfo defines a loop as a non-trivial SCC dominated by a single block,
  33. ; called the header. A given loop, L, can have sub-loops, which are loops
  34. ; within the subgraph of L that excludes the header.
  35. ;
  36. ; In addition to loops, -block-freq has limited support for irreducible SCCs,
  37. ; which are SCCs with multiple entry blocks. Irreducible SCCs are discovered
  38. ; on they fly, and modelled as loops with multiple headers.
  39. ;
  40. ; The headers of irreducible sub-SCCs consist of its entry blocks and all nodes
  41. ; that are targets of a backedge within it (excluding backedges within true
  42. ; sub-loops).
  43. ;
  44. ; -block-freq is currently designed to act like a block is inserted that
  45. ; intercepts all the edges to the headers. All backedges and entries point to
  46. ; this block. Its successors are the headers, which split the frequency
  47. ; evenly.
  48. ;
  49. ; There are a number of testcases below. Only the first two have detailed
  50. ; explanations.
  51. ;
  52. ; Testcase #1
  53. ; ===========
  54. ;
  55. ; In this case c1 and c2 should have frequencies of 15/7 and 13/7,
  56. ; respectively. To calculate this, consider assigning 1.0 to entry, and
  57. ; distributing frequency iteratively (to infinity). At the first iteration,
  58. ; entry gives 3/4 to c1 and 1/4 to c2. At every step after, c1 and c2 give 3/4
  59. ; of what they have to each other. Somehow, all of it comes out to exit.
  60. ;
  61. ; c1 = 3/4 + 1/4*3/4 + 3/4*3^2/4^2 + 1/4*3^3/4^3 + 3/4*3^3/4^3 + ...
  62. ; c2 = 1/4 + 3/4*3/4 + 1/4*3^2/4^2 + 3/4*3^3/4^3 + 1/4*3^3/4^3 + ...
  63. ;
  64. ; Simplify by splitting up the odd and even terms of the series and taking out
  65. ; factors so that the infite series matches:
  66. ;
  67. ; c1 = 3/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  68. ; + 3/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  69. ; c2 = 1/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  70. ; + 9/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  71. ;
  72. ; c1 = 15/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  73. ; c2 = 13/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
  74. ;
  75. ; Since this geometric series sums to 16/7:
  76. ;
  77. ; c1 = 15/7
  78. ; c2 = 13/7
  79. ;
  80. ; If we treat c1 and c2 as members of the same loop, the exit frequency of the
  81. ; loop as a whole is 1/4, so the loop scale should be 4. Summing c1 and c2
  82. ; gives 28/7, or 4.0, which is nice confirmation of the math above.
  83. ;
  84. ; -block-freq currently treats the two nodes as equals.
  85. define void @multientry(i1 %x) {
  86. ; CHECK-LABEL: Printing analysis {{.*}} for function 'multientry':
  87. ; CHECK-NEXT: block-frequency-info: multientry
  88. entry:
  89. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  90. br i1 %x, label %c1, label %c2, !prof !2
  91. c1:
  92. ; CHECK-NEXT: c1: float = 2.0,
  93. ; The "correct" answer is: float = 2.142857{{[0-9]*}},
  94. br i1 %x, label %c2, label %exit, !prof !2
  95. c2:
  96. ; CHECK-NEXT: c2: float = 2.0,
  97. ; The "correct" answer is: float = 1.857142{{[0-9]*}},
  98. br i1 %x, label %c1, label %exit, !prof !2
  99. exit:
  100. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  101. ret void
  102. }
  103. !2 = !{!"branch_weights", i32 3, i32 1}
  104. ; Testcase #2
  105. ; ===========
  106. ;
  107. ; In this case c1 and c2 should be treated as equals in a single loop. The
  108. ; exit frequency is 1/3, so the scaling factor for the loop should be 3.0. The
  109. ; loop is entered 2/3 of the time, and c1 and c2 split the total loop frequency
  110. ; evenly (1/2), so they should each have frequencies of 1.0 (3.0*2/3*1/2).
  111. ; Another way of computing this result is by assigning 1.0 to entry and showing
  112. ; that c1 and c2 should accumulate frequencies of:
  113. ;
  114. ; 1/3 + 2/9 + 4/27 + 8/81 + ...
  115. ; 2^0/3^1 + 2^1/3^2 + 2^2/3^3 + 2^3/3^4 + ...
  116. ;
  117. ; At the first step, c1 and c2 each get 1/3 of the entry. At each subsequent
  118. ; step, c1 and c2 each get 1/3 of what's left in c1 and c2 combined. This
  119. ; infinite series sums to 1.
  120. define void @crossloops(i2 %x) {
  121. ; CHECK-LABEL: Printing analysis {{.*}} for function 'crossloops':
  122. ; CHECK-NEXT: block-frequency-info: crossloops
  123. entry:
  124. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  125. switch i2 %x, label %exit [ i2 1, label %c1
  126. i2 2, label %c2 ], !prof !3
  127. c1:
  128. ; CHECK-NEXT: c1: float = 1.0,
  129. switch i2 %x, label %exit [ i2 1, label %c1
  130. i2 2, label %c2 ], !prof !3
  131. c2:
  132. ; CHECK-NEXT: c2: float = 1.0,
  133. switch i2 %x, label %exit [ i2 1, label %c1
  134. i2 2, label %c2 ], !prof !3
  135. exit:
  136. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  137. ret void
  138. }
  139. !3 = !{!"branch_weights", i32 2, i32 2, i32 2}
  140. ; A true loop with irreducible control flow inside.
  141. define void @loop_around_irreducible(i1 %x) {
  142. ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_irreducible':
  143. ; CHECK-NEXT: block-frequency-info: loop_around_irreducible
  144. entry:
  145. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  146. br label %loop
  147. loop:
  148. ; CHECK-NEXT: loop: float = 4.0, int = [[HEAD:[0-9]+]]
  149. br i1 %x, label %left, label %right, !prof !4
  150. left:
  151. ; CHECK-NEXT: left: float = 8.0,
  152. br i1 %x, label %right, label %loop.end, !prof !5
  153. right:
  154. ; CHECK-NEXT: right: float = 8.0,
  155. br i1 %x, label %left, label %loop.end, !prof !5
  156. loop.end:
  157. ; CHECK-NEXT: loop.end: float = 4.0, int = [[HEAD]]
  158. br i1 %x, label %loop, label %exit, !prof !5
  159. exit:
  160. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  161. ret void
  162. }
  163. !4 = !{!"branch_weights", i32 1, i32 1}
  164. !5 = !{!"branch_weights", i32 3, i32 1}
  165. ; Two unrelated irreducible SCCs.
  166. define void @two_sccs(i1 %x) {
  167. ; CHECK-LABEL: Printing analysis {{.*}} for function 'two_sccs':
  168. ; CHECK-NEXT: block-frequency-info: two_sccs
  169. entry:
  170. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  171. br i1 %x, label %a, label %b, !prof !6
  172. a:
  173. ; CHECK-NEXT: a: float = 0.75,
  174. br i1 %x, label %a.left, label %a.right, !prof !7
  175. a.left:
  176. ; CHECK-NEXT: a.left: float = 1.5,
  177. br i1 %x, label %a.right, label %exit, !prof !6
  178. a.right:
  179. ; CHECK-NEXT: a.right: float = 1.5,
  180. br i1 %x, label %a.left, label %exit, !prof !6
  181. b:
  182. ; CHECK-NEXT: b: float = 0.25,
  183. br i1 %x, label %b.left, label %b.right, !prof !7
  184. b.left:
  185. ; CHECK-NEXT: b.left: float = 0.625,
  186. br i1 %x, label %b.right, label %exit, !prof !8
  187. b.right:
  188. ; CHECK-NEXT: b.right: float = 0.625,
  189. br i1 %x, label %b.left, label %exit, !prof !8
  190. exit:
  191. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  192. ret void
  193. }
  194. !6 = !{!"branch_weights", i32 3, i32 1}
  195. !7 = !{!"branch_weights", i32 1, i32 1}
  196. !8 = !{!"branch_weights", i32 4, i32 1}
  197. ; A true loop inside irreducible control flow.
  198. define void @loop_inside_irreducible(i1 %x) {
  199. ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_inside_irreducible':
  200. ; CHECK-NEXT: block-frequency-info: loop_inside_irreducible
  201. entry:
  202. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  203. br i1 %x, label %left, label %right, !prof !9
  204. left:
  205. ; CHECK-NEXT: left: float = 2.0,
  206. br i1 %x, label %right, label %exit, !prof !10
  207. right:
  208. ; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
  209. br label %loop
  210. loop:
  211. ; CHECK-NEXT: loop: float = 6.0,
  212. br i1 %x, label %loop, label %right.end, !prof !11
  213. right.end:
  214. ; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
  215. br i1 %x, label %left, label %exit, !prof !10
  216. exit:
  217. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  218. ret void
  219. }
  220. !9 = !{!"branch_weights", i32 1, i32 1}
  221. !10 = !{!"branch_weights", i32 3, i32 1}
  222. !11 = !{!"branch_weights", i32 2, i32 1}
  223. ; Irreducible control flow in a branch that's in a true loop.
  224. define void @loop_around_branch_with_irreducible(i1 %x) {
  225. ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_branch_with_irreducible':
  226. ; CHECK-NEXT: block-frequency-info: loop_around_branch_with_irreducible
  227. entry:
  228. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  229. br label %loop
  230. loop:
  231. ; CHECK-NEXT: loop: float = 2.0, int = [[LOOP:[0-9]+]]
  232. br i1 %x, label %normal, label %irreducible.entry, !prof !12
  233. normal:
  234. ; CHECK-NEXT: normal: float = 1.5,
  235. br label %loop.end
  236. irreducible.entry:
  237. ; CHECK-NEXT: irreducible.entry: float = 0.5, int = [[IRREDUCIBLE:[0-9]+]]
  238. br i1 %x, label %left, label %right, !prof !13
  239. left:
  240. ; CHECK-NEXT: left: float = 1.0,
  241. br i1 %x, label %right, label %irreducible.exit, !prof !12
  242. right:
  243. ; CHECK-NEXT: right: float = 1.0,
  244. br i1 %x, label %left, label %irreducible.exit, !prof !12
  245. irreducible.exit:
  246. ; CHECK-NEXT: irreducible.exit: float = 0.5, int = [[IRREDUCIBLE]]
  247. br label %loop.end
  248. loop.end:
  249. ; CHECK-NEXT: loop.end: float = 2.0, int = [[LOOP]]
  250. br i1 %x, label %loop, label %exit, !prof !13
  251. exit:
  252. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  253. ret void
  254. }
  255. !12 = !{!"branch_weights", i32 3, i32 1}
  256. !13 = !{!"branch_weights", i32 1, i32 1}
  257. ; Irreducible control flow between two true loops.
  258. define void @loop_around_branch_with_irreducible_around_loop(i1 %x) {
  259. ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_branch_with_irreducible_around_loop':
  260. ; CHECK-NEXT: block-frequency-info: loop_around_branch_with_irreducible_around_loop
  261. entry:
  262. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  263. br label %loop
  264. loop:
  265. ; CHECK-NEXT: loop: float = 3.0, int = [[LOOP:[0-9]+]]
  266. br i1 %x, label %normal, label %irreducible, !prof !14
  267. normal:
  268. ; CHECK-NEXT: normal: float = 2.0,
  269. br label %loop.end
  270. irreducible:
  271. ; CHECK-NEXT: irreducible: float = 1.0,
  272. br i1 %x, label %left, label %right, !prof !15
  273. left:
  274. ; CHECK-NEXT: left: float = 2.0,
  275. br i1 %x, label %right, label %loop.end, !prof !16
  276. right:
  277. ; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
  278. br label %right.loop
  279. right.loop:
  280. ; CHECK-NEXT: right.loop: float = 10.0,
  281. br i1 %x, label %right.loop, label %right.end, !prof !17
  282. right.end:
  283. ; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
  284. br i1 %x, label %left, label %loop.end, !prof !16
  285. loop.end:
  286. ; CHECK-NEXT: loop.end: float = 3.0, int = [[LOOP]]
  287. br i1 %x, label %loop, label %exit, !prof !14
  288. exit:
  289. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  290. ret void
  291. }
  292. !14 = !{!"branch_weights", i32 2, i32 1}
  293. !15 = !{!"branch_weights", i32 1, i32 1}
  294. !16 = !{!"branch_weights", i32 3, i32 1}
  295. !17 = !{!"branch_weights", i32 4, i32 1}
  296. ; An irreducible SCC with a non-header.
  297. define void @nonheader(i1 %x) {
  298. ; CHECK-LABEL: Printing analysis {{.*}} for function 'nonheader':
  299. ; CHECK-NEXT: block-frequency-info: nonheader
  300. entry:
  301. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  302. br i1 %x, label %left, label %right, !prof !18
  303. left:
  304. ; CHECK-NEXT: left: float = 1.0,
  305. br i1 %x, label %bottom, label %exit, !prof !19
  306. right:
  307. ; CHECK-NEXT: right: float = 1.0,
  308. br i1 %x, label %bottom, label %exit, !prof !20
  309. bottom:
  310. ; CHECK-NEXT: bottom: float = 1.0,
  311. br i1 %x, label %left, label %right, !prof !18
  312. exit:
  313. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  314. ret void
  315. }
  316. !18 = !{!"branch_weights", i32 1, i32 1}
  317. !19 = !{!"branch_weights", i32 1, i32 3}
  318. !20 = !{!"branch_weights", i32 3, i32 1}
  319. ; An irreducible SCC with an irreducible sub-SCC. In the current version of
  320. ; -block-freq, this means an extra header.
  321. ;
  322. ; This testcases uses non-trivial branch weights. The CHECK statements here
  323. ; will start to fail if we change -block-freq to be more accurate. Currently,
  324. ; loop headers are affected by the weight of their corresponding back edges.
  325. define void @nonentry_header(i1 %x, i2 %y) {
  326. ; CHECK-LABEL: Printing analysis {{.*}} for function 'nonentry_header':
  327. ; CHECK-NEXT: block-frequency-info: nonentry_header
  328. entry:
  329. ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
  330. br i1 %x, label %left, label %right, !prof !21
  331. left:
  332. ; CHECK-NEXT: left: float = 0.14
  333. br i1 %x, label %top, label %bottom, !prof !22
  334. right:
  335. ; CHECK-NEXT: right: float = 0.42
  336. br i1 %x, label %top, label %bottom, !prof !22
  337. top:
  338. ; CHECK-NEXT: top: float = 8.43
  339. switch i2 %y, label %exit [ i2 0, label %left
  340. i2 1, label %right
  341. i2 2, label %bottom ], !prof !23
  342. bottom:
  343. ; CHECK-NEXT: bottom: float = 4.5,
  344. br label %top
  345. exit:
  346. ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
  347. ret void
  348. }
  349. !21 = !{!"branch_weights", i32 2, i32 1}
  350. !22 = !{!"branch_weights", i32 1, i32 1}
  351. !23 = !{!"branch_weights", i32 8, i32 1, i32 3, i32 12}