• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -analyze -block-freq | FileCheck %s
2; RUN: opt < %s -passes='print<block-freq>' -disable-output 2>&1 | FileCheck %s
3
4; A loop with multiple exits isn't irreducible.  It should be handled
5; correctly.
6;
7; CHECK-LABEL: Printing analysis {{.*}} for function 'multiexit':
8; CHECK-NEXT: block-frequency-info: multiexit
9define void @multiexit(i1 %x) {
10; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
11entry:
12  br label %loop.1
13
14; CHECK-NEXT: loop.1: float = 2.0,
15loop.1:
16  br i1 %x, label %exit.1, label %loop.2, !prof !0
17
18; CHECK-NEXT: loop.2: float = 1.75,
19loop.2:
20  br i1 %x, label %exit.2, label %loop.1, !prof !1
21
22; CHECK-NEXT: exit.1: float = 0.25,
23exit.1:
24  br label %return
25
26; CHECK-NEXT: exit.2: float = 0.75,
27exit.2:
28  br label %return
29
30; CHECK-NEXT: return: float = 1.0, int = [[ENTRY]]
31return:
32  ret void
33}
34
35!0 = !{!"branch_weights", i32 1, i32 7}
36!1 = !{!"branch_weights", i32 3, i32 4}
37
38; Irreducible control flow
39; ========================
40;
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.
44;
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.
48;
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
51; sub-loops).
52;
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
56; evenly.
57;
58; There are a number of testcases below.  Only the first two have detailed
59; explanations.
60;
61; Testcase #1
62; ===========
63;
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.
69;
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 + ...
72;
73; Simplify by splitting up the odd and even terms of the series and taking out
74; factors so that the infite series matches:
75;
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 + ...)
80;
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 + ...)
83;
84; Since this geometric series sums to 16/7:
85;
86;       c1 = 15/7
87;       c2 = 13/7
88;
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.
92;
93; -block-freq currently treats the two nodes as equals.
94define void @multientry(i1 %x) {
95; CHECK-LABEL: Printing analysis {{.*}} for function 'multientry':
96; CHECK-NEXT: block-frequency-info: multientry
97entry:
98; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
99  br i1 %x, label %c1, label %c2, !prof !2
100
101c1:
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
105
106c2:
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
110
111exit:
112; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
113  ret void
114}
115
116!2 = !{!"branch_weights", i32 3, i32 1}
117
118; Testcase #2
119; ===========
120;
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:
127;
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 + ...
130;
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.
134define void @crossloops(i2 %x) {
135; CHECK-LABEL: Printing analysis {{.*}} for function 'crossloops':
136; CHECK-NEXT: block-frequency-info: crossloops
137entry:
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
141
142c1:
143; CHECK-NEXT: c1: float = 1.0,
144  switch i2 %x, label %exit [ i2 1, label %c1
145                              i2 2, label %c2 ], !prof !3
146
147c2:
148; CHECK-NEXT: c2: float = 1.0,
149  switch i2 %x, label %exit [ i2 1, label %c1
150                              i2 2, label %c2 ], !prof !3
151
152exit:
153; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
154  ret void
155}
156
157!3 = !{!"branch_weights", i32 2, i32 2, i32 2}
158
159; A true loop with irreducible control flow inside.
160define 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
163entry:
164; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
165  br label %loop
166
167loop:
168; CHECK-NEXT: loop: float = 4.0, int = [[HEAD:[0-9]+]]
169  br i1 %x, label %left, label %right, !prof !4
170
171left:
172; CHECK-NEXT: left: float = 8.0,
173  br i1 %x, label %right, label %loop.end, !prof !5
174
175right:
176; CHECK-NEXT: right: float = 8.0,
177  br i1 %x, label %left, label %loop.end, !prof !5
178
179loop.end:
180; CHECK-NEXT: loop.end: float = 4.0, int = [[HEAD]]
181  br i1 %x, label %loop, label %exit, !prof !5
182
183exit:
184; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
185  ret void
186}
187!4 = !{!"branch_weights", i32 1, i32 1}
188!5 = !{!"branch_weights", i32 3, i32 1}
189
190; Two unrelated irreducible SCCs.
191define void @two_sccs(i1 %x) {
192; CHECK-LABEL: Printing analysis {{.*}} for function 'two_sccs':
193; CHECK-NEXT: block-frequency-info: two_sccs
194entry:
195; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
196  br i1 %x, label %a, label %b, !prof !6
197
198a:
199; CHECK-NEXT: a: float = 0.75,
200  br i1 %x, label %a.left, label %a.right, !prof !7
201
202a.left:
203; CHECK-NEXT: a.left: float = 1.5,
204  br i1 %x, label %a.right, label %exit, !prof !6
205
206a.right:
207; CHECK-NEXT: a.right: float = 1.5,
208  br i1 %x, label %a.left, label %exit, !prof !6
209
210b:
211; CHECK-NEXT: b: float = 0.25,
212  br i1 %x, label %b.left, label %b.right, !prof !7
213
214b.left:
215; CHECK-NEXT: b.left: float = 0.625,
216  br i1 %x, label %b.right, label %exit, !prof !8
217
218b.right:
219; CHECK-NEXT: b.right: float = 0.625,
220  br i1 %x, label %b.left, label %exit, !prof !8
221
222exit:
223; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
224  ret void
225}
226!6 = !{!"branch_weights", i32 3, i32 1}
227!7 = !{!"branch_weights", i32 1, i32 1}
228!8 = !{!"branch_weights", i32 4, i32 1}
229
230; A true loop inside irreducible control flow.
231define 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
234entry:
235; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
236  br i1 %x, label %left, label %right, !prof !9
237
238left:
239; CHECK-NEXT: left: float = 2.0,
240  br i1 %x, label %right, label %exit, !prof !10
241
242right:
243; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
244  br label %loop
245
246loop:
247; CHECK-NEXT: loop: float = 6.0,
248  br i1 %x, label %loop, label %right.end, !prof !11
249
250right.end:
251; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
252  br i1 %x, label %left, label %exit, !prof !10
253
254exit:
255; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
256  ret void
257}
258!9 = !{!"branch_weights", i32 1, i32 1}
259!10 = !{!"branch_weights", i32 3, i32 1}
260!11 = !{!"branch_weights", i32 2, i32 1}
261
262; Irreducible control flow in a branch that's in a true loop.
263define 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
266entry:
267; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
268  br label %loop
269
270loop:
271; CHECK-NEXT: loop: float = 2.0, int = [[LOOP:[0-9]+]]
272  br i1 %x, label %normal, label %irreducible.entry, !prof !12
273
274normal:
275; CHECK-NEXT: normal: float = 1.5,
276  br label %loop.end
277
278irreducible.entry:
279; CHECK-NEXT: irreducible.entry: float = 0.5, int = [[IRREDUCIBLE:[0-9]+]]
280  br i1 %x, label %left, label %right, !prof !13
281
282left:
283; CHECK-NEXT: left: float = 1.0,
284  br i1 %x, label %right, label %irreducible.exit, !prof !12
285
286right:
287; CHECK-NEXT: right: float = 1.0,
288  br i1 %x, label %left, label %irreducible.exit, !prof !12
289
290irreducible.exit:
291; CHECK-NEXT: irreducible.exit: float = 0.5, int = [[IRREDUCIBLE]]
292  br label %loop.end
293
294loop.end:
295; CHECK-NEXT: loop.end: float = 2.0, int = [[LOOP]]
296  br i1 %x, label %loop, label %exit, !prof !13
297
298exit:
299; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
300  ret void
301}
302!12 = !{!"branch_weights", i32 3, i32 1}
303!13 = !{!"branch_weights", i32 1, i32 1}
304
305; Irreducible control flow between two true loops.
306define 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
309entry:
310; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
311  br label %loop
312
313loop:
314; CHECK-NEXT: loop: float = 3.0, int = [[LOOP:[0-9]+]]
315  br i1 %x, label %normal, label %irreducible, !prof !14
316
317normal:
318; CHECK-NEXT: normal: float = 2.0,
319  br label %loop.end
320
321irreducible:
322; CHECK-NEXT: irreducible: float = 1.0,
323  br i1 %x, label %left, label %right, !prof !15
324
325left:
326; CHECK-NEXT: left: float = 2.0,
327  br i1 %x, label %right, label %loop.end, !prof !16
328
329right:
330; CHECK-NEXT: right: float = 2.0, int = [[RIGHT:[0-9]+]]
331  br label %right.loop
332
333right.loop:
334; CHECK-NEXT: right.loop: float = 10.0,
335  br i1 %x, label %right.loop, label %right.end, !prof !17
336
337right.end:
338; CHECK-NEXT: right.end: float = 2.0, int = [[RIGHT]]
339  br i1 %x, label %left, label %loop.end, !prof !16
340
341loop.end:
342; CHECK-NEXT: loop.end: float = 3.0, int = [[LOOP]]
343  br i1 %x, label %loop, label %exit, !prof !14
344
345exit:
346; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
347  ret void
348}
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}
353
354; An irreducible SCC with a non-header.
355define void @nonheader(i1 %x) {
356; CHECK-LABEL: Printing analysis {{.*}} for function 'nonheader':
357; CHECK-NEXT: block-frequency-info: nonheader
358entry:
359; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
360  br i1 %x, label %left, label %right, !prof !18
361
362left:
363; CHECK-NEXT: left: float = 1.0,
364  br i1 %x, label %bottom, label %exit, !prof !19
365
366right:
367; CHECK-NEXT: right: float = 1.0,
368  br i1 %x, label %bottom, label %exit, !prof !20
369
370bottom:
371; CHECK-NEXT: bottom: float = 1.0,
372  br i1 %x, label %left, label %right, !prof !18
373
374exit:
375; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
376  ret void
377}
378!18 = !{!"branch_weights", i32 1, i32 1}
379!19 = !{!"branch_weights", i32 1, i32 3}
380!20 = !{!"branch_weights", i32 3, i32 1}
381
382; An irreducible SCC with an irreducible sub-SCC.  In the current version of
383; -block-freq, this means an extra header.
384;
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.
388define void @nonentry_header(i1 %x, i2 %y) {
389; CHECK-LABEL: Printing analysis {{.*}} for function 'nonentry_header':
390; CHECK-NEXT: block-frequency-info: nonentry_header
391entry:
392; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
393  br i1 %x, label %left, label %right, !prof !21
394
395left:
396; CHECK-NEXT: left: float = 0.14
397  br i1 %x, label %top, label %bottom, !prof !22
398
399right:
400; CHECK-NEXT: right: float = 0.42
401  br i1 %x, label %top, label %bottom, !prof !22
402
403top:
404; CHECK-NEXT: top: float = 8.43
405  switch i2 %y, label %exit [ i2 0, label %left
406                              i2 1, label %right
407                              i2 2, label %bottom ], !prof !23
408
409bottom:
410; CHECK-NEXT: bottom: float = 4.5,
411  br label %top
412
413exit:
414; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
415  ret void
416}
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}
420