blockfreq: Use better branch weights in multiexit test
[oota-llvm.git] / test / Analysis / BlockFrequencyInfo / irreducible.ll
1 ; RUN: opt < %s -analyze -block-freq | FileCheck %s
2
3 ; A loop with multiple exits isn't irreducible.  It should be handled
4 ; correctly.
5 ;
6 ; CHECK-LABEL: Printing analysis {{.*}} for function 'multiexit':
7 ; CHECK-NEXT: block-frequency-info: multiexit
8 define void @multiexit(i1 %x) {
9 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
10 entry:
11   br label %loop.1
12
13 ; CHECK-NEXT: loop.1: float = 2.0,
14 loop.1:
15   br i1 %x, label %exit.1, label %loop.2, !prof !0
16
17 ; CHECK-NEXT: loop.2: float = 1.75,
18 loop.2:
19   br i1 %x, label %exit.2, label %loop.1, !prof !1
20
21 ; CHECK-NEXT: exit.1: float = 0.25,
22 exit.1:
23   br label %return
24
25 ; CHECK-NEXT: exit.2: float = 0.75,
26 exit.2:
27   br label %return
28
29 ; CHECK-NEXT: return: float = 1.0, int = [[ENTRY]]
30 return:
31   ret void
32 }
33
34 !0 = metadata !{metadata !"branch_weights", i32 1, i32 7}
35 !1 = metadata !{metadata !"branch_weights", i32 3, i32 4}
36
37 ; The current BlockFrequencyInfo algorithm doesn't handle multiple entrances
38 ; into a loop very well.  The frequencies assigned to blocks in the loop are
39 ; predictable (and not absurd), but also not correct and therefore not worth
40 ; testing.
41 ;
42 ; There are two testcases below.
43 ;
44 ; For each testcase, I use a CHECK-NEXT/NOT combo like an XFAIL with the
45 ; granularity of a single check.  If/when this behaviour is fixed, we'll know
46 ; about it, and the test should be updated.
47 ;
48 ; Testcase #1
49 ; ===========
50 ;
51 ; In this case c1 and c2 should have frequencies of 15/7 and 13/7,
52 ; respectively.  To calculate this, consider assigning 1.0 to entry, and
53 ; distributing frequency iteratively (to infinity).  At the first iteration,
54 ; entry gives 3/4 to c1 and 1/4 to c2.  At every step after, c1 and c2 give 3/4
55 ; of what they have to each other.  Somehow, all of it comes out to exit.
56 ;
57 ;       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 + ...
58 ;       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 + ...
59 ;
60 ; Simplify by splitting up the odd and even terms of the series and taking out
61 ; factors so that the infite series matches:
62 ;
63 ;       c1 =  3/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
64 ;          +  3/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
65 ;       c2 =  1/4 *(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
66 ;          +  9/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
67 ;
68 ;       c1 = 15/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
69 ;       c2 = 13/16*(9^0/16^0 + 9^1/16^1 + 9^2/16^2 + ...)
70 ;
71 ; Since this geometric series sums to 16/7:
72 ;
73 ;       c1 = 15/7
74 ;       c2 = 13/7
75 ;
76 ; If we treat c1 and c2 as members of the same loop, the exit frequency of the
77 ; loop as a whole is 1/4, so the loop scale should be 4.  Summing c1 and c2
78 ; gives 28/7, or 4.0, which is nice confirmation of the math above.
79 ;
80 ; However, assuming c1 precedes c2 in reverse post-order, the current algorithm
81 ; returns 3/4 and 13/16, respectively.  LoopInfo ignores edges between loops
82 ; (and doesn't see any loops here at all), and -block-freq ignores the
83 ; irreducible edge from c2 to c1.
84 ;
85 ; CHECK-LABEL: Printing analysis {{.*}} for function 'multientry':
86 ; CHECK-NEXT: block-frequency-info: multientry
87 define void @multientry(i1 %x) {
88 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
89 entry:
90   br i1 %x, label %c1, label %c2, !prof !2
91
92 ; This is like a single-line XFAIL (see above).
93 ; CHECK-NEXT: c1:
94 ; CHECK-NOT: float = 2.142857{{[0-9]*}},
95 c1:
96   br i1 %x, label %c2, label %exit, !prof !2
97
98 ; This is like a single-line XFAIL (see above).
99 ; CHECK-NEXT: c2:
100 ; CHECK-NOT: float = 1.857142{{[0-9]*}},
101 c2:
102   br i1 %x, label %c1, label %exit, !prof !2
103
104 ; We still shouldn't lose any frequency.
105 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
106 exit:
107   ret void
108 }
109
110 ; Testcase #2
111 ; ===========
112 ;
113 ; In this case c1 and c2 should be treated as equals in a single loop.  The
114 ; exit frequency is 1/3, so the scaling factor for the loop should be 3.0.  The
115 ; loop is entered 2/3 of the time, and c1 and c2 split the total loop frequency
116 ; evenly (1/2), so they should each have frequencies of 1.0 (3.0*2/3*1/2).
117 ; Another way of computing this result is by assigning 1.0 to entry and showing
118 ; that c1 and c2 should accumulate frequencies of:
119 ;
120 ;       1/3   +   2/9   +   4/27  +   8/81  + ...
121 ;     2^0/3^1 + 2^1/3^2 + 2^2/3^3 + 2^3/3^4 + ...
122 ;
123 ; At the first step, c1 and c2 each get 1/3 of the entry.  At each subsequent
124 ; step, c1 and c2 each get 1/3 of what's left in c1 and c2 combined.  This
125 ; infinite series sums to 1.
126 ;
127 ; However, assuming c1 precedes c2 in reverse post-order, the current algorithm
128 ; returns 1/2 and 3/4, respectively.  LoopInfo ignores edges between loops (and
129 ; treats c1 and c2 as self-loops only), and -block-freq ignores the irreducible
130 ; edge from c2 to c1.
131 ;
132 ; Below I use a CHECK-NEXT/NOT combo like an XFAIL with the granularity of a
133 ; single check.  If/when this behaviour is fixed, we'll know about it, and the
134 ; test should be updated.
135 ;
136 ; CHECK-LABEL: Printing analysis {{.*}} for function 'crossloops':
137 ; CHECK-NEXT: block-frequency-info: crossloops
138 define void @crossloops(i2 %x) {
139 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
140 entry:
141   switch i2 %x, label %exit [ i2 1, label %c1
142                               i2 2, label %c2 ], !prof !3
143
144 ; This is like a single-line XFAIL (see above).
145 ; CHECK-NEXT: c1:
146 ; CHECK-NOT: float = 1.0,
147 c1:
148   switch i2 %x, label %exit [ i2 1, label %c1
149                               i2 2, label %c2 ], !prof !3
150
151 ; This is like a single-line XFAIL (see above).
152 ; CHECK-NEXT: c2:
153 ; CHECK-NOT: float = 1.0,
154 c2:
155   switch i2 %x, label %exit [ i2 1, label %c1
156                               i2 2, label %c2 ], !prof !3
157
158 ; We still shouldn't lose any frequency.
159 ; CHECK-NEXT: exit: float = 1.0, int = [[ENTRY]]
160 exit:
161   ret void
162 }
163
164 !2 = metadata !{metadata !"branch_weights", i32 3, i32 1}
165 !3 = metadata !{metadata !"branch_weights", i32 2, i32 2, i32 2}
166
167 ; A reducible loop with irreducible control flow inside should still have
168 ; correct exit frequency.
169 ;
170 ; CHECK-LABEL: Printing analysis {{.*}} for function 'loop_around_irreducible':
171 ; CHECK-NEXT: block-frequency-info: loop_around_irreducible
172 define void @loop_around_irreducible(i1 %x) {
173 ; CHECK-NEXT: entry: float = 1.0, int = [[ENTRY:[0-9]+]]
174 entry:
175   br label %loop
176
177 ; CHECK-NEXT: loop: float = [[HEAD:[0-9.]+]], int = [[HEADINT:[0-9]+]]
178 loop:
179   br i1 %x, label %left, label %right
180
181 ; CHECK-NEXT: left:
182 left:
183   br i1 %x, label %right, label %loop.end
184
185 ; CHECK-NEXT: right:
186 right:
187   br i1 %x, label %left, label %loop.end
188
189 ; CHECK-NEXT: loop.end: float = [[HEAD]], int = [[HEADINT]]
190 loop.end:
191   br i1 %x, label %loop, label %exit
192
193 ; CHECK-NEXT: float = 1.0, int = [[ENTRY]]
194 exit:
195   ret void
196 }