1 ; RUN: opt < %s -analyze -block-freq | FileCheck %s
2 ; RUN: opt < %s -passes='print<block-freq>' -disable-output 2>&1 | FileCheck %s
4 ; A loop with multiple exits isn't irreducible. It should be handled
7 ; CHECK-LABEL: Printing analysis {{.*}} for function 'multiexit':
8 ; CHECK-NEXT: block-frequency-info: multiexit
9 define void @multiexit(i1 %x) {
10 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
14 ; CHECK-NEXT: loop.1: float = 2.0,
16 br i1 %x, label %exit.1, label %loop.2, !prof !0
18 ; CHECK-NEXT: loop.2: float = 1.75,
20 br i1 %x, label %exit.2, label %loop.1, !prof !1
22 ; CHECK-NEXT: exit.1: float = 0.25,
26 ; CHECK-NEXT: exit.2: float = 0.75,
30 ; CHECK-NEXT: return: float = 1.0, int = [[ENTRY]]
35 !0 = !{!"branch_weights", i32 1, i32 7}
36 !1 = !{!"branch_weights", i32 3, i32 4}
38 ; Irreducible control flow
39 ; ========================
41 ; LoopInfo defines a loop as a non-trivial SCC dominated by a single block,
42 ; called the header. A given loop, L, can have sub-loops, which are loops
43 ; within the subgraph of L that excludes the header.
45 ; In addition to loops, -block-freq has limited support for irreducible SCCs,
46 ; which are SCCs with multiple entry blocks. Irreducible SCCs are discovered
47 ; on they fly, and modelled as loops with multiple headers.
49 ; The headers of irreducible sub-SCCs consist of its entry blocks and all nodes
50 ; that are targets of a backedge within it (excluding backedges within true
53 ; -block-freq is currently designed to act like a block is inserted that
54 ; intercepts all the edges to the headers. All backedges and entries point to
55 ; this block. Its successors are the headers, which split the frequency
58 ; There are a number of testcases below. Only the first two have detailed
64 ; In this case c1 and c2 should have frequencies of 15/7 and 13/7,
65 ; respectively. To calculate this, consider assigning 1.0 to entry, and
66 ; distributing frequency iteratively (to infinity). At the first iteration,
67 ; entry gives 3/4 to c1 and 1/4 to c2. At every step after, c1 and c2 give 3/4
68 ; of what they have to each other. Somehow, all of it comes out to exit.
70 ; 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 + ...
71 ; 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 + ...
73 ; Simplify by splitting up the odd and even terms of the series and taking out
74 ; factors so that the infite series matches:
76 ; c1 = 3/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
77 ; + 3/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
78 ; c2 = 1/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
79 ; + 9/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
81 ; c1 = 15/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
82 ; c2 = 13/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
84 ; Since this geometric series sums to 16/7:
89 ; If we treat c1 and c2 as members of the same loop, the exit frequency of the
90 ; loop as a whole is 1/4, so the loop scale should be 4. Summing c1 and c2
91 ; gives 28/7, or 4.0, which is nice confirmation of the math above.
93 ; -block-freq currently treats the two nodes as equals.
94 define void @multientry(i1 %x) {
95 ; CHECK-LABEL: Printing analysis {{.*}} for function 'multientry':
96 ; CHECK-NEXT: block-frequency-info: multientry
98 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
99 br i1 %x, label %c1, label %c2, !prof !2
102 ; CHECK-NEXT: c1: float = 2.0,
103 ; The "correct" answer is: float = 2.142857{{[0-9]*}},
104 br i1 %x, label %c2, label %exit, !prof !2
107 ; CHECK-NEXT: c2: float = 2.0,
108 ; The "correct" answer is: float = 1.857142{{[0-9]*}},
109 br i1 %x, label %c1, label %exit, !prof !2
112 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
116 !2 = !{!"branch_weights", i32 3, i32 1}
121 ; In this case c1 and c2 should be treated as equals in a single loop. The
122 ; exit frequency is 1/3, so the scaling factor for the loop should be 3.0. The
123 ; loop is entered 2/3 of the time, and c1 and c2 split the total loop frequency
124 ; evenly (1/2), so they should each have frequencies of 1.0 (3.0*2/3*1/2).
125 ; Another way of computing this result is by assigning 1.0 to entry and showing
126 ; that c1 and c2 should accumulate frequencies of:
128 ; 1/3 + 2/9 + 4/27 + 8/81 + ...
129 ; 2^0/3^1 + 2^1/3^2 + 2^2/3^3 + 2^3/3^4 + ...
131 ; At the first step, c1 and c2 each get 1/3 of the entry. At each subsequent
132 ; step, c1 and c2 each get 1/3 of what's left in c1 and c2 combined. This
133 ; infinite series sums to 1.
134 define void @crossloops(i2 %x) {
135 ; CHECK-LABEL: Printing analysis {{.*}} for function 'crossloops':
136 ; CHECK-NEXT: block-frequency-info: crossloops
138 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
139 switch i2 %x, label %exit [ i2 1, label %c1
140 i2 2, label %c2 ], !prof !3
143 ; CHECK-NEXT: c1: float = 1.0,
144 switch i2 %x, label %exit [ i2 1, label %c1
145 i2 2, label %c2 ], !prof !3
148 ; CHECK-NEXT: c2: float = 1.0,
149 switch i2 %x, label %exit [ i2 1, label %c1
150 i2 2, label %c2 ], !prof !3
153 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
157 !3 = !{!"branch_weights", i32 2, i32 2, i32 2}
159 ; A true loop with irreducible control flow inside.
160 define void @loop_around_irreducible(i1 %x) {
161 ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_irreducible':
162 ; CHECK-NEXT: block-frequency-info: loop_around_irreducible
164 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
168 ; CHECK-NEXT: loop: float = 4.0, int = [[HEAD:[0-9]+]]
169 br i1 %x, label %left, label %right, !prof !4
172 ; CHECK-NEXT: left: float = 8.0,
173 br i1 %x, label %right, label %loop.end, !prof !5
176 ; CHECK-NEXT: right: float = 8.0,
177 br i1 %x, label %left, label %loop.end, !prof !5
180 ; CHECK-NEXT: loop.end: float = 4.0, int = [[HEAD]]
181 br i1 %x, label %loop, label %exit, !prof !5
184 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
187 !4 = !{!"branch_weights", i32 1, i32 1}
188 !5 = !{!"branch_weights", i32 3, i32 1}
190 ; Two unrelated irreducible SCCs.
191 define void @two_sccs(i1 %x) {
192 ; CHECK-LABEL: Printing analysis {{.*}} for function 'two_sccs':
193 ; CHECK-NEXT: block-frequency-info: two_sccs
195 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
196 br i1 %x, label %a, label %b, !prof !6
199 ; CHECK-NEXT: a: float = 0.75,
200 br i1 %x, label %a.left, label %a.right, !prof !7
203 ; CHECK-NEXT: a.left: float = 1.5,
204 br i1 %x, label %a.right, label %exit, !prof !6
207 ; CHECK-NEXT: a.right: float = 1.5,
208 br i1 %x, label %a.left, label %exit, !prof !6
211 ; CHECK-NEXT: b: float = 0.25,
212 br i1 %x, label %b.left, label %b.right, !prof !7
215 ; CHECK-NEXT: b.left: float = 0.625,
216 br i1 %x, label %b.right, label %exit, !prof !8
219 ; CHECK-NEXT: b.right: float = 0.625,
220 br i1 %x, label %b.left, label %exit, !prof !8
223 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
226 !6 = !{!"branch_weights", i32 3, i32 1}
227 !7 = !{!"branch_weights", i32 1, i32 1}
228 !8 = !{!"branch_weights", i32 4, i32 1}
230 ; A true loop inside irreducible control flow.
231 define void @loop_inside_irreducible(i1 %x) {
232 ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_inside_irreducible':
233 ; CHECK-NEXT: block-frequency-info: loop_inside_irreducible
235 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
236 br i1 %x, label %left, label %right, !prof !9
239 ; CHECK-NEXT: left: float = 2.0,
240 br i1 %x, label %right, label %exit, !prof !10
243 ; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
247 ; CHECK-NEXT: loop: float = 6.0,
248 br i1 %x, label %loop, label %right.end, !prof !11
251 ; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
252 br i1 %x, label %left, label %exit, !prof !10
255 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
258 !9 = !{!"branch_weights", i32 1, i32 1}
259 !10 = !{!"branch_weights", i32 3, i32 1}
260 !11 = !{!"branch_weights", i32 2, i32 1}
262 ; Irreducible control flow in a branch that's in a true loop.
263 define void @loop_around_branch_with_irreducible(i1 %x) {
264 ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_branch_with_irreducible':
265 ; CHECK-NEXT: block-frequency-info: loop_around_branch_with_irreducible
267 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
271 ; CHECK-NEXT: loop: float = 2.0, int = [[LOOP:[0-9]+]]
272 br i1 %x, label %normal, label %irreducible.entry, !prof !12
275 ; CHECK-NEXT: normal: float = 1.5,
279 ; CHECK-NEXT: irreducible.entry: float = 0.5, int = [[IRREDUCIBLE:[0-9]+]]
280 br i1 %x, label %left, label %right, !prof !13
283 ; CHECK-NEXT: left: float = 1.0,
284 br i1 %x, label %right, label %irreducible.exit, !prof !12
287 ; CHECK-NEXT: right: float = 1.0,
288 br i1 %x, label %left, label %irreducible.exit, !prof !12
291 ; CHECK-NEXT: irreducible.exit: float = 0.5, int = [[IRREDUCIBLE]]
295 ; CHECK-NEXT: loop.end: float = 2.0, int = [[LOOP]]
296 br i1 %x, label %loop, label %exit, !prof !13
299 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
302 !12 = !{!"branch_weights", i32 3, i32 1}
303 !13 = !{!"branch_weights", i32 1, i32 1}
305 ; Irreducible control flow between two true loops.
306 define void @loop_around_branch_with_irreducible_around_loop(i1 %x) {
307 ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_branch_with_irreducible_around_loop':
308 ; CHECK-NEXT: block-frequency-info: loop_around_branch_with_irreducible_around_loop
310 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
314 ; CHECK-NEXT: loop: float = 3.0, int = [[LOOP:[0-9]+]]
315 br i1 %x, label %normal, label %irreducible, !prof !14
318 ; CHECK-NEXT: normal: float = 2.0,
322 ; CHECK-NEXT: irreducible: float = 1.0,
323 br i1 %x, label %left, label %right, !prof !15
326 ; CHECK-NEXT: left: float = 2.0,
327 br i1 %x, label %right, label %loop.end, !prof !16
330 ; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
334 ; CHECK-NEXT: right.loop: float = 10.0,
335 br i1 %x, label %right.loop, label %right.end, !prof !17
338 ; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
339 br i1 %x, label %left, label %loop.end, !prof !16
342 ; CHECK-NEXT: loop.end: float = 3.0, int = [[LOOP]]
343 br i1 %x, label %loop, label %exit, !prof !14
346 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
349 !14 = !{!"branch_weights", i32 2, i32 1}
350 !15 = !{!"branch_weights", i32 1, i32 1}
351 !16 = !{!"branch_weights", i32 3, i32 1}
352 !17 = !{!"branch_weights", i32 4, i32 1}
354 ; An irreducible SCC with a non-header.
355 define void @nonheader(i1 %x) {
356 ; CHECK-LABEL: Printing analysis {{.*}} for function 'nonheader':
357 ; CHECK-NEXT: block-frequency-info: nonheader
359 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
360 br i1 %x, label %left, label %right, !prof !18
363 ; CHECK-NEXT: left: float = 1.0,
364 br i1 %x, label %bottom, label %exit, !prof !19
367 ; CHECK-NEXT: right: float = 1.0,
368 br i1 %x, label %bottom, label %exit, !prof !20
371 ; CHECK-NEXT: bottom: float = 1.0,
372 br i1 %x, label %left, label %right, !prof !18
375 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
378 !18 = !{!"branch_weights", i32 1, i32 1}
379 !19 = !{!"branch_weights", i32 1, i32 3}
380 !20 = !{!"branch_weights", i32 3, i32 1}
382 ; An irreducible SCC with an irreducible sub-SCC. In the current version of
383 ; -block-freq, this means an extra header.
385 ; This testcases uses non-trivial branch weights. The CHECK statements here
386 ; will start to fail if we change -block-freq to be more accurate. Currently,
387 ; loop headers are affected by the weight of their corresponding back edges.
388 define void @nonentry_header(i1 %x, i2 %y) {
389 ; CHECK-LABEL: Printing analysis {{.*}} for function 'nonentry_header':
390 ; CHECK-NEXT: block-frequency-info: nonentry_header
392 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
393 br i1 %x, label %left, label %right, !prof !21
396 ; CHECK-NEXT: left: float = 0.14
397 br i1 %x, label %top, label %bottom, !prof !22
400 ; CHECK-NEXT: right: float = 0.42
401 br i1 %x, label %top, label %bottom, !prof !22
404 ; CHECK-NEXT: top: float = 8.43
405 switch i2 %y, label %exit [ i2 0, label %left
407 i2 2, label %bottom ], !prof !23
410 ; CHECK-NEXT: bottom: float = 4.5,
414 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
417 !21 = !{!"branch_weights", i32 2, i32 1}
418 !22 = !{!"branch_weights", i32 1, i32 1}
419 !23 = !{!"branch_weights", i32 8, i32 1, i32 3, i32 12}