Fix an overflow bug in MachineBranchProbabilityInfo. This pass relied on
[oota-llvm.git] / test / CodeGen / X86 / block-placement.ll
1 ; RUN: llc -march=x86 -enable-block-placement < %s | FileCheck %s
2
3 declare void @error(i32 %i, i32 %a, i32 %b)
4
5 define i32 @test_ifchains(i32 %i, i32* %a, i32 %b) {
6 ; Test a chain of ifs, where the block guarded by the if is error handling code
7 ; that is not expected to run.
8 ; CHECK: test_ifchains:
9 ; CHECK: %entry
10 ; CHECK: %else1
11 ; CHECK: %else2
12 ; CHECK: %else3
13 ; CHECK: %else4
14 ; CHECK: %exit
15 ; CHECK: %then1
16 ; CHECK: %then2
17 ; CHECK: %then3
18 ; CHECK: %then4
19 ; CHECK: %then5
20
21 entry:
22   %gep1 = getelementptr i32* %a, i32 1
23   %val1 = load i32* %gep1
24   %cond1 = icmp ugt i32 %val1, 1
25   br i1 %cond1, label %then1, label %else1, !prof !0
26
27 then1:
28   call void @error(i32 %i, i32 1, i32 %b)
29   br label %else1
30
31 else1:
32   %gep2 = getelementptr i32* %a, i32 2
33   %val2 = load i32* %gep2
34   %cond2 = icmp ugt i32 %val2, 2
35   br i1 %cond2, label %then2, label %else2, !prof !0
36
37 then2:
38   call void @error(i32 %i, i32 1, i32 %b)
39   br label %else2
40
41 else2:
42   %gep3 = getelementptr i32* %a, i32 3
43   %val3 = load i32* %gep3
44   %cond3 = icmp ugt i32 %val3, 3
45   br i1 %cond3, label %then3, label %else3, !prof !0
46
47 then3:
48   call void @error(i32 %i, i32 1, i32 %b)
49   br label %else3
50
51 else3:
52   %gep4 = getelementptr i32* %a, i32 4
53   %val4 = load i32* %gep4
54   %cond4 = icmp ugt i32 %val4, 4
55   br i1 %cond4, label %then4, label %else4, !prof !0
56
57 then4:
58   call void @error(i32 %i, i32 1, i32 %b)
59   br label %else4
60
61 else4:
62   %gep5 = getelementptr i32* %a, i32 3
63   %val5 = load i32* %gep5
64   %cond5 = icmp ugt i32 %val5, 3
65   br i1 %cond5, label %then5, label %exit, !prof !0
66
67 then5:
68   call void @error(i32 %i, i32 1, i32 %b)
69   br label %exit
70
71 exit:
72   ret i32 %b
73 }
74
75 define i32 @test_loop_cold_blocks(i32 %i, i32* %a) {
76 ; Check that we sink cold loop blocks after the hot loop body.
77 ; CHECK: test_loop_cold_blocks:
78 ; CHECK: %entry
79 ; CHECK: %body1
80 ; CHECK: %body2
81 ; CHECK: %body3
82 ; CHECK: %unlikely1
83 ; CHECK: %unlikely2
84 ; CHECK: %exit
85
86 entry:
87   br label %body1
88
89 body1:
90   %iv = phi i32 [ 0, %entry ], [ %next, %body3 ]
91   %base = phi i32 [ 0, %entry ], [ %sum, %body3 ]
92   %unlikelycond1 = icmp slt i32 %base, 42
93   br i1 %unlikelycond1, label %unlikely1, label %body2, !prof !0
94
95 unlikely1:
96   call void @error(i32 %i, i32 1, i32 %base)
97   br label %body2
98
99 body2:
100   %unlikelycond2 = icmp sgt i32 %base, 21
101   br i1 %unlikelycond2, label %unlikely2, label %body3, !prof !0
102
103 unlikely2:
104   call void @error(i32 %i, i32 2, i32 %base)
105   br label %body3
106
107 body3:
108   %arrayidx = getelementptr inbounds i32* %a, i32 %iv
109   %0 = load i32* %arrayidx
110   %sum = add nsw i32 %0, %base
111   %next = add i32 %iv, 1
112   %exitcond = icmp eq i32 %next, %i
113   br i1 %exitcond, label %exit, label %body1
114
115 exit:
116   ret i32 %sum
117 }
118
119 !0 = metadata !{metadata !"branch_weights", i32 4, i32 64}
120
121 define i32 @test_loop_early_exits(i32 %i, i32* %a) {
122 ; Check that we sink early exit blocks out of loop bodies.
123 ; CHECK: test_loop_early_exits:
124 ; CHECK: %entry
125 ; CHECK: %body1
126 ; CHECK: %body2
127 ; CHECK: %body3
128 ; CHECK: %body4
129 ; CHECK: %exit
130 ; CHECK: %bail1
131 ; CHECK: %bail2
132 ; CHECK: %bail3
133
134 entry:
135   br label %body1
136
137 body1:
138   %iv = phi i32 [ 0, %entry ], [ %next, %body4 ]
139   %base = phi i32 [ 0, %entry ], [ %sum, %body4 ]
140   %bailcond1 = icmp eq i32 %base, 42
141   br i1 %bailcond1, label %bail1, label %body2
142
143 bail1:
144   ret i32 -1
145
146 body2:
147   %bailcond2 = icmp eq i32 %base, 43
148   br i1 %bailcond2, label %bail2, label %body3
149
150 bail2:
151   ret i32 -2
152
153 body3:
154   %bailcond3 = icmp eq i32 %base, 44
155   br i1 %bailcond3, label %bail3, label %body4
156
157 bail3:
158   ret i32 -3
159
160 body4:
161   %arrayidx = getelementptr inbounds i32* %a, i32 %iv
162   %0 = load i32* %arrayidx
163   %sum = add nsw i32 %0, %base
164   %next = add i32 %iv, 1
165   %exitcond = icmp eq i32 %next, %i
166   br i1 %exitcond, label %exit, label %body1
167
168 exit:
169   ret i32 %sum
170 }
171
172 define i32 @test_loop_align(i32 %i, i32* %a) {
173 ; Check that we provide basic loop body alignment with the block placement
174 ; pass.
175 ; CHECK: test_loop_align:
176 ; CHECK: %entry
177 ; CHECK: .align [[ALIGN:[0-9]+]],
178 ; CHECK-NEXT: %body
179 ; CHECK: %exit
180
181 entry:
182   br label %body
183
184 body:
185   %iv = phi i32 [ 0, %entry ], [ %next, %body ]
186   %base = phi i32 [ 0, %entry ], [ %sum, %body ]
187   %arrayidx = getelementptr inbounds i32* %a, i32 %iv
188   %0 = load i32* %arrayidx
189   %sum = add nsw i32 %0, %base
190   %next = add i32 %iv, 1
191   %exitcond = icmp eq i32 %next, %i
192   br i1 %exitcond, label %exit, label %body
193
194 exit:
195   ret i32 %sum
196 }
197
198 define i32 @test_nested_loop_align(i32 %i, i32* %a, i32* %b) {
199 ; Check that we provide nested loop body alignment.
200 ; CHECK: test_nested_loop_align:
201 ; CHECK: %entry
202 ; CHECK: .align [[ALIGN]],
203 ; CHECK-NEXT: %loop.body.1
204 ; CHECK: .align [[ALIGN]],
205 ; CHECK-NEXT: %inner.loop.body
206 ; CHECK-NOT: .align
207 ; CHECK: %exit
208
209 entry:
210   br label %loop.body.1
211
212 loop.body.1:
213   %iv = phi i32 [ 0, %entry ], [ %next, %loop.body.2 ]
214   %arrayidx = getelementptr inbounds i32* %a, i32 %iv
215   %bidx = load i32* %arrayidx
216   br label %inner.loop.body
217
218 inner.loop.body:
219   %inner.iv = phi i32 [ 0, %loop.body.1 ], [ %inner.next, %inner.loop.body ]
220   %base = phi i32 [ 0, %loop.body.1 ], [ %sum, %inner.loop.body ]
221   %scaled_idx = mul i32 %bidx, %iv
222   %inner.arrayidx = getelementptr inbounds i32* %b, i32 %scaled_idx
223   %0 = load i32* %inner.arrayidx
224   %sum = add nsw i32 %0, %base
225   %inner.next = add i32 %iv, 1
226   %inner.exitcond = icmp eq i32 %inner.next, %i
227   br i1 %inner.exitcond, label %loop.body.2, label %inner.loop.body
228
229 loop.body.2:
230   %next = add i32 %iv, 1
231   %exitcond = icmp eq i32 %next, %i
232   br i1 %exitcond, label %exit, label %loop.body.1
233
234 exit:
235   ret i32 %sum
236 }
237
238 define void @unnatural_cfg1() {
239 ; Test that we can handle a loop with an inner unnatural loop at the end of
240 ; a function. This is a gross CFG reduced out of the single source GCC.
241 ; CHECK: unnatural_cfg1
242 ; CHECK: %entry
243 ; CHECK: %loop.body1
244 ; CHECK: %loop.body3
245 ; CHECK: %loop.body2
246
247 entry:
248   br label %loop.header
249
250 loop.header:
251   br label %loop.body1
252
253 loop.body1:
254   br i1 undef, label %loop.body3, label %loop.body2
255
256 loop.body2:
257   %ptr = load i32** undef, align 4
258   br label %loop.body3
259
260 loop.body3:
261   %myptr = phi i32* [ %ptr2, %loop.body5 ], [ %ptr, %loop.body2 ], [ undef, %loop.body1 ]
262   %bcmyptr = bitcast i32* %myptr to i32*
263   %val = load i32* %bcmyptr, align 4
264   %comp = icmp eq i32 %val, 48
265   br i1 %comp, label %loop.body4, label %loop.body5
266
267 loop.body4:
268   br i1 undef, label %loop.header, label %loop.body5
269
270 loop.body5:
271   %ptr2 = load i32** undef, align 4
272   br label %loop.body3
273 }
274
275 define i32 @problematic_switch() {
276 ; This function's CFG caused overlow in the machine branch probability
277 ; calculation, triggering asserts. Make sure we don't crash on it.
278 ; CHECK: problematic_switch
279
280 entry:
281   switch i32 undef, label %exit [
282     i32 879, label %bogus
283     i32 877, label %step
284     i32 876, label %step
285     i32 875, label %step
286     i32 874, label %step
287     i32 873, label %step
288     i32 872, label %step
289     i32 868, label %step
290     i32 867, label %step
291     i32 866, label %step
292     i32 861, label %step
293     i32 860, label %step
294     i32 856, label %step
295     i32 855, label %step
296     i32 854, label %step
297     i32 831, label %step
298     i32 830, label %step
299     i32 829, label %step
300     i32 828, label %step
301     i32 815, label %step
302     i32 814, label %step
303     i32 811, label %step
304     i32 806, label %step
305     i32 805, label %step
306     i32 804, label %step
307     i32 803, label %step
308     i32 802, label %step
309     i32 801, label %step
310     i32 800, label %step
311     i32 799, label %step
312     i32 798, label %step
313     i32 797, label %step
314     i32 796, label %step
315     i32 795, label %step
316   ]
317 bogus:
318   unreachable
319 step:
320   br label %exit
321 exit:
322   %merge = phi i32 [ 3, %step ], [ 6, %entry ]
323   ret i32 %merge
324 }