blob: e62a8c88ef921295107ca349d864b7e421e1a4d0 [file] [log] [blame]
Dan Gohman950a13c2015-09-16 16:51:30 +00001; RUN: llc < %s -asm-verbose=false | FileCheck %s
2
3; Test the CFG stackifier pass.
4
5target datalayout = "e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8declare void @something()
9
10; Test that loops are made contiguous, even in the presence of split backedges.
11
Dan Gohmane51c0582015-10-06 00:27:55 +000012; CHECK-LABEL: test0:
13; CHECK: loop
Dan Gohman3cb66c82015-11-06 21:32:42 +000014; CHECK: i32.add
JF Bastien3b0177c2015-10-20 00:37:42 +000015; CHECK: br_if
Dan Gohmane51c0582015-10-06 00:27:55 +000016; CHECK: call
Dan Gohmancf4748f2015-11-12 17:04:33 +000017; CHECK: br BB0_1{{$}}
Dan Gohmane51c0582015-10-06 00:27:55 +000018; CHECK: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000019define void @test0(i32 %n) {
20entry:
21 br label %header
22
23header:
24 %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
25 %i.next = add i32 %i, 1
26
27 %c = icmp slt i32 %i.next, %n
28 br i1 %c, label %back, label %exit
29
30exit:
31 ret void
32
33back:
34 call void @something()
35 br label %header
36}
37
38; Same as test0, but the branch condition is reversed.
39
Dan Gohmane51c0582015-10-06 00:27:55 +000040; CHECK-LABEL: test1:
41; CHECK: loop
Dan Gohman3cb66c82015-11-06 21:32:42 +000042; CHECK: i32.add
JF Bastien3b0177c2015-10-20 00:37:42 +000043; CHECK: br_if
Dan Gohmane51c0582015-10-06 00:27:55 +000044; CHECK: call
Dan Gohmancf4748f2015-11-12 17:04:33 +000045; CHECK: br BB1_1{{$}}
Dan Gohmane51c0582015-10-06 00:27:55 +000046; CHECK: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000047define void @test1(i32 %n) {
48entry:
49 br label %header
50
51header:
52 %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
53 %i.next = add i32 %i, 1
54
55 %c = icmp sge i32 %i.next, %n
56 br i1 %c, label %exit, label %back
57
58exit:
59 ret void
60
61back:
62 call void @something()
63 br label %header
64}
65
66; Test that a simple loop is handled as expected.
67
Dan Gohmane51c0582015-10-06 00:27:55 +000068; CHECK-LABEL: test2:
Dan Gohmancf4748f2015-11-12 17:04:33 +000069; CHECK: block BB2_2{{$}}
Derek Schuff4ed47782015-11-16 21:04:51 +000070; CHECK: br_if {{.*}}, BB2_2{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000071; CHECK: BB2_1:
Dan Gohman4ba48162015-11-18 16:12:01 +000072; CHECK: br_if $pop{{[0-9]+}}, BB2_1{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000073; CHECK: BB2_2:
Dan Gohmane51c0582015-10-06 00:27:55 +000074; CHECK: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000075define void @test2(double* nocapture %p, i32 %n) {
76entry:
77 %cmp.4 = icmp sgt i32 %n, 0
78 br i1 %cmp.4, label %for.body.preheader, label %for.end
79
80for.body.preheader:
81 br label %for.body
82
83for.body:
84 %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ]
85 %arrayidx = getelementptr inbounds double, double* %p, i32 %i.05
86 %0 = load double, double* %arrayidx, align 8
87 %mul = fmul double %0, 3.200000e+00
88 store double %mul, double* %arrayidx, align 8
89 %inc = add nuw nsw i32 %i.05, 1
90 %exitcond = icmp eq i32 %inc, %n
91 br i1 %exitcond, label %for.end.loopexit, label %for.body
92
93for.end.loopexit:
94 br label %for.end
95
96for.end:
97 ret void
98}
99
Dan Gohmane51c0582015-10-06 00:27:55 +0000100; CHECK-LABEL: doublediamond:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000101; CHECK: block BB3_5{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000102; CHECK: block BB3_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000103; CHECK: br_if $pop{{[0-9]+}}, BB3_2{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000104; CHECK: block BB3_4{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000105; CHECK: br_if $pop{{[0-9]+}}, BB3_4{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000106; CHECK: br BB3_5{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000107; CHECK: BB3_4:
108; CHECK: BB3_5:
Dan Gohman4ba48162015-11-18 16:12:01 +0000109; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000110define i32 @doublediamond(i32 %a, i32 %b, i32* %p) {
111entry:
112 %c = icmp eq i32 %a, 0
113 %d = icmp eq i32 %b, 0
114 store volatile i32 0, i32* %p
115 br i1 %c, label %true, label %false
116true:
117 store volatile i32 1, i32* %p
118 br label %exit
119false:
120 store volatile i32 2, i32* %p
121 br i1 %d, label %ft, label %ff
122ft:
123 store volatile i32 3, i32* %p
124 br label %exit
125ff:
126 store volatile i32 4, i32* %p
127 br label %exit
128exit:
129 store volatile i32 5, i32* %p
130 ret i32 0
131}
132
Dan Gohmane51c0582015-10-06 00:27:55 +0000133; CHECK-LABEL: triangle:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000134; CHECK: block BB4_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000135; CHECK: br_if $pop{{[0-9]+}}, BB4_2{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000136; CHECK: BB4_2:
Dan Gohman4ba48162015-11-18 16:12:01 +0000137; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000138define i32 @triangle(i32* %p, i32 %a) {
139entry:
140 %c = icmp eq i32 %a, 0
141 store volatile i32 0, i32* %p
142 br i1 %c, label %true, label %exit
143true:
144 store volatile i32 1, i32* %p
145 br label %exit
146exit:
147 store volatile i32 2, i32* %p
148 ret i32 0
149}
150
Dan Gohmane51c0582015-10-06 00:27:55 +0000151; CHECK-LABEL: diamond:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000152; CHECK: block BB5_3{{$}}
153; CHECK: block BB5_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000154; CHECK: br_if $pop{{[0-9]+}}, BB5_2{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000155; CHECK: br BB5_3{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000156; CHECK: BB5_2:
157; CHECK: BB5_3:
Dan Gohman4ba48162015-11-18 16:12:01 +0000158; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000159define i32 @diamond(i32* %p, i32 %a) {
160entry:
161 %c = icmp eq i32 %a, 0
162 store volatile i32 0, i32* %p
163 br i1 %c, label %true, label %false
164true:
165 store volatile i32 1, i32* %p
166 br label %exit
167false:
168 store volatile i32 2, i32* %p
169 br label %exit
170exit:
171 store volatile i32 3, i32* %p
172 ret i32 0
173}
174
Dan Gohmane51c0582015-10-06 00:27:55 +0000175; CHECK-LABEL: single_block:
Dan Gohman950a13c2015-09-16 16:51:30 +0000176; CHECK-NOT: br
Dan Gohman4ba48162015-11-18 16:12:01 +0000177; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000178define i32 @single_block(i32* %p) {
179entry:
180 store volatile i32 0, i32* %p
181 ret i32 0
182}
183
Dan Gohmane51c0582015-10-06 00:27:55 +0000184; CHECK-LABEL: minimal_loop:
Dan Gohman950a13c2015-09-16 16:51:30 +0000185; CHECK-NOT: br
186; CHECK: BB7_1:
Dan Gohman4ba48162015-11-18 16:12:01 +0000187; CHECK: i32.store $0, $pop{{[0-9]+}}{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000188; CHECK: br BB7_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000189; CHECK: BB7_2:
Dan Gohman950a13c2015-09-16 16:51:30 +0000190define i32 @minimal_loop(i32* %p) {
191entry:
192 store volatile i32 0, i32* %p
193 br label %loop
194loop:
195 store volatile i32 1, i32* %p
196 br label %loop
197}
198
Dan Gohmane51c0582015-10-06 00:27:55 +0000199; CHECK-LABEL: simple_loop:
Dan Gohman950a13c2015-09-16 16:51:30 +0000200; CHECK-NOT: br
201; CHECK: BB8_1:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000202; CHECK: loop BB8_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000203; CHECK: br_if $pop{{[0-9]+}}, BB8_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000204; CHECK: BB8_2:
Dan Gohman4ba48162015-11-18 16:12:01 +0000205; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000206define i32 @simple_loop(i32* %p, i32 %a) {
207entry:
208 %c = icmp eq i32 %a, 0
209 store volatile i32 0, i32* %p
210 br label %loop
211loop:
212 store volatile i32 1, i32* %p
213 br i1 %c, label %loop, label %exit
214exit:
215 store volatile i32 2, i32* %p
216 ret i32 0
217}
218
Dan Gohmane51c0582015-10-06 00:27:55 +0000219; CHECK-LABEL: doubletriangle:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000220; CHECK: block BB9_4{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000221; CHECK: br_if $pop{{[0-9]+}}, BB9_4{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000222; CHECK: block BB9_3{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000223; CHECK: br_if $pop{{[0-9]+}}, BB9_3{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000224; CHECK: BB9_3:
225; CHECK: BB9_4:
Dan Gohman4ba48162015-11-18 16:12:01 +0000226; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000227define i32 @doubletriangle(i32 %a, i32 %b, i32* %p) {
228entry:
229 %c = icmp eq i32 %a, 0
230 %d = icmp eq i32 %b, 0
231 store volatile i32 0, i32* %p
232 br i1 %c, label %true, label %exit
233true:
234 store volatile i32 2, i32* %p
235 br i1 %d, label %tt, label %tf
236tt:
237 store volatile i32 3, i32* %p
238 br label %tf
239tf:
240 store volatile i32 4, i32* %p
241 br label %exit
242exit:
243 store volatile i32 5, i32* %p
244 ret i32 0
245}
246
Dan Gohmane51c0582015-10-06 00:27:55 +0000247; CHECK-LABEL: ifelse_earlyexits:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000248; CHECK: block BB10_4{{$}}
249; CHECK: block BB10_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000250; CHECK: br_if $pop{{[0-9]+}}, BB10_2{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000251; CHECK: br BB10_4{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000252; CHECK: BB10_2:
Dan Gohman4ba48162015-11-18 16:12:01 +0000253; CHECK: br_if $pop{{[0-9]+}}, BB10_4{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000254; CHECK: BB10_4:
Dan Gohman4ba48162015-11-18 16:12:01 +0000255; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000256define i32 @ifelse_earlyexits(i32 %a, i32 %b, i32* %p) {
257entry:
258 %c = icmp eq i32 %a, 0
259 %d = icmp eq i32 %b, 0
260 store volatile i32 0, i32* %p
261 br i1 %c, label %true, label %false
262true:
263 store volatile i32 1, i32* %p
264 br label %exit
265false:
266 store volatile i32 2, i32* %p
267 br i1 %d, label %ft, label %exit
268ft:
269 store volatile i32 3, i32* %p
270 br label %exit
271exit:
272 store volatile i32 4, i32* %p
273 ret i32 0
274}
Dan Gohmane3e4a5f2015-10-02 21:11:36 +0000275
Dan Gohman32807932015-11-23 16:19:56 +0000276; CHECK-LABEL: doublediamond_in_a_loop:
277; CHECK: BB11_1:
278; CHECK: loop BB11_7{{$}}
279; CHECK: block BB11_6{{$}}
280; CHECK: block BB11_3{{$}}
281; CHECK: br_if $pop{{.*}}, BB11_3{{$}}
282; CHECK: br BB11_6{{$}}
283; CHECK: BB11_3:
284; CHECK: block BB11_5{{$}}
285; CHECK: br_if $pop{{.*}}, BB11_5{{$}}
286; CHECK: br BB11_6{{$}}
287; CHECK: BB11_5:
288; CHECK: BB11_6:
289; CHECK: br BB11_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000290; CHECK: BB11_7:
Dan Gohman32807932015-11-23 16:19:56 +0000291define i32 @doublediamond_in_a_loop(i32 %a, i32 %b, i32* %p) {
292entry:
293 br label %header
294header:
295 %c = icmp eq i32 %a, 0
296 %d = icmp eq i32 %b, 0
297 store volatile i32 0, i32* %p
298 br i1 %c, label %true, label %false
299true:
300 store volatile i32 1, i32* %p
301 br label %exit
302false:
303 store volatile i32 2, i32* %p
304 br i1 %d, label %ft, label %ff
305ft:
306 store volatile i32 3, i32* %p
307 br label %exit
308ff:
309 store volatile i32 4, i32* %p
310 br label %exit
311exit:
312 store volatile i32 5, i32* %p
313 br label %header
314}
315
Dan Gohmane3e4a5f2015-10-02 21:11:36 +0000316; Test that nested loops are handled.
317
318declare void @bar()
319
320define void @test3(i32 %w) {
321entry:
322 br i1 undef, label %outer.ph, label %exit
323
324outer.ph:
325 br label %outer
326
327outer:
328 %tobool = icmp eq i32 undef, 0
329 br i1 %tobool, label %inner, label %unreachable
330
331unreachable:
332 unreachable
333
334inner:
335 %c = icmp eq i32 undef, %w
336 br i1 %c, label %if.end, label %inner
337
338exit:
339 ret void
340
341if.end:
342 call void @bar()
343 br label %outer
344}