blob: 4c86c55a8f8569a088a414162ca534405fd3244d [file] [log] [blame]
Dan Gohmanf0b165a2015-12-05 03:03:35 +00001; RUN: llc < %s -asm-verbose=false -disable-block-placement | FileCheck %s
2; RUN: llc < %s -asm-verbose=false | FileCheck -check-prefix=OPT %s
Dan Gohman950a13c2015-09-16 16:51:30 +00003
4; Test the CFG stackifier pass.
5
6target datalayout = "e-p:32:32-i64:64-n32:64-S128"
7target triple = "wasm32-unknown-unknown"
8
9declare void @something()
10
11; Test that loops are made contiguous, even in the presence of split backedges.
12
Dan Gohmane51c0582015-10-06 00:27:55 +000013; CHECK-LABEL: test0:
14; CHECK: loop
Dan Gohman3cb66c82015-11-06 21:32:42 +000015; CHECK: i32.add
Dan Gohmanf0b165a2015-12-05 03:03:35 +000016; CHECK-NOT: br
JF Bastien3b0177c2015-10-20 00:37:42 +000017; CHECK: br_if
Dan Gohmanf0b165a2015-12-05 03:03:35 +000018; CHECK-NOT: br
Dan Gohmane51c0582015-10-06 00:27:55 +000019; CHECK: call
Dan Gohmancf4748f2015-11-12 17:04:33 +000020; CHECK: br BB0_1{{$}}
Dan Gohmane51c0582015-10-06 00:27:55 +000021; CHECK: return{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +000022; OPT-LABEL: test0:
23; OPT: loop
24; OPT: i32.add
25; OPT-NOT: br
26; OPT: br_if
27; OPT-NOT: br
28; OPT: call
29; OPT: br BB0_1{{$}}
30; OPT: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000031define void @test0(i32 %n) {
32entry:
33 br label %header
34
35header:
36 %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
37 %i.next = add i32 %i, 1
38
39 %c = icmp slt i32 %i.next, %n
40 br i1 %c, label %back, label %exit
41
42exit:
43 ret void
44
45back:
46 call void @something()
47 br label %header
48}
49
50; Same as test0, but the branch condition is reversed.
51
Dan Gohmane51c0582015-10-06 00:27:55 +000052; CHECK-LABEL: test1:
53; CHECK: loop
Dan Gohman3cb66c82015-11-06 21:32:42 +000054; CHECK: i32.add
Dan Gohmanf0b165a2015-12-05 03:03:35 +000055; CHECK-NOT: br
JF Bastien3b0177c2015-10-20 00:37:42 +000056; CHECK: br_if
Dan Gohmanf0b165a2015-12-05 03:03:35 +000057; CHECK-NOT: br
Dan Gohmane51c0582015-10-06 00:27:55 +000058; CHECK: call
Dan Gohmancf4748f2015-11-12 17:04:33 +000059; CHECK: br BB1_1{{$}}
Dan Gohmane51c0582015-10-06 00:27:55 +000060; CHECK: return{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +000061; OPT-LABEL: test1:
62; OPT: loop
63; OPT: i32.add
64; OPT-NOT: br
65; OPT: br_if
66; OPT-NOT: br
67; OPT: call
68; OPT: br BB1_1{{$}}
69; OPT: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000070define void @test1(i32 %n) {
71entry:
72 br label %header
73
74header:
75 %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
76 %i.next = add i32 %i, 1
77
78 %c = icmp sge i32 %i.next, %n
79 br i1 %c, label %exit, label %back
80
81exit:
82 ret void
83
84back:
85 call void @something()
86 br label %header
87}
88
89; Test that a simple loop is handled as expected.
90
Dan Gohmane51c0582015-10-06 00:27:55 +000091; CHECK-LABEL: test2:
Dan Gohmancf4748f2015-11-12 17:04:33 +000092; CHECK: block BB2_2{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +000093; CHECK: br_if {{[^,]*}}, BB2_2{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000094; CHECK: BB2_1:
Dan Gohmanf0b165a2015-12-05 03:03:35 +000095; CHECK: br_if ${{[0-9]+}}, BB2_1{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +000096; CHECK: BB2_2:
Dan Gohmane51c0582015-10-06 00:27:55 +000097; CHECK: return{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +000098; OPT-LABEL: test2:
99; OPT: block BB2_2{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000100; OPT: br_if {{[^,]*}}, BB2_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000101; OPT: BB2_1:
102; OPT: br_if ${{[0-9]+}}, BB2_1{{$}}
103; OPT: BB2_2:
104; OPT: return{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000105define void @test2(double* nocapture %p, i32 %n) {
106entry:
107 %cmp.4 = icmp sgt i32 %n, 0
108 br i1 %cmp.4, label %for.body.preheader, label %for.end
109
110for.body.preheader:
111 br label %for.body
112
113for.body:
114 %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ]
115 %arrayidx = getelementptr inbounds double, double* %p, i32 %i.05
116 %0 = load double, double* %arrayidx, align 8
117 %mul = fmul double %0, 3.200000e+00
118 store double %mul, double* %arrayidx, align 8
119 %inc = add nuw nsw i32 %i.05, 1
120 %exitcond = icmp eq i32 %inc, %n
121 br i1 %exitcond, label %for.end.loopexit, label %for.body
122
123for.end.loopexit:
124 br label %for.end
125
126for.end:
127 ret void
128}
129
Dan Gohmane51c0582015-10-06 00:27:55 +0000130; CHECK-LABEL: doublediamond:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000131; CHECK: block BB3_5{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000132; CHECK: block BB3_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000133; CHECK: br_if $0, BB3_2{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000134; CHECK: block BB3_4{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000135; CHECK: br_if $1, BB3_4{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000136; CHECK: br BB3_5{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000137; CHECK: BB3_4:
138; CHECK: BB3_5:
Dan Gohman4ba48162015-11-18 16:12:01 +0000139; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000140; OPT-LABEL: doublediamond:
141; OPT: block BB3_5{{$}}
142; OPT: block BB3_4{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000143; OPT: br_if {{[^,]*}}, BB3_4{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000144; OPT: block BB3_3{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000145; OPT: br_if {{[^,]*}}, BB3_3{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000146; OPT: br BB3_5{{$}}
147; OPT: BB3_4:
148; OPT: BB3_5:
149; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000150define i32 @doublediamond(i32 %a, i32 %b, i32* %p) {
151entry:
152 %c = icmp eq i32 %a, 0
153 %d = icmp eq i32 %b, 0
154 store volatile i32 0, i32* %p
155 br i1 %c, label %true, label %false
156true:
157 store volatile i32 1, i32* %p
158 br label %exit
159false:
160 store volatile i32 2, i32* %p
161 br i1 %d, label %ft, label %ff
162ft:
163 store volatile i32 3, i32* %p
164 br label %exit
165ff:
166 store volatile i32 4, i32* %p
167 br label %exit
168exit:
169 store volatile i32 5, i32* %p
170 ret i32 0
171}
172
Dan Gohmane51c0582015-10-06 00:27:55 +0000173; CHECK-LABEL: triangle:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000174; CHECK: block BB4_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000175; CHECK: br_if $1, BB4_2{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000176; CHECK: BB4_2:
Dan Gohman4ba48162015-11-18 16:12:01 +0000177; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000178; OPT-LABEL: triangle:
179; OPT: block BB4_2{{$}}
180; OPT: br_if $1, BB4_2{{$}}
181; OPT: BB4_2:
182; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000183define i32 @triangle(i32* %p, i32 %a) {
184entry:
185 %c = icmp eq i32 %a, 0
186 store volatile i32 0, i32* %p
187 br i1 %c, label %true, label %exit
188true:
189 store volatile i32 1, i32* %p
190 br label %exit
191exit:
192 store volatile i32 2, i32* %p
193 ret i32 0
194}
195
Dan Gohmane51c0582015-10-06 00:27:55 +0000196; CHECK-LABEL: diamond:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000197; CHECK: block BB5_3{{$}}
198; CHECK: block BB5_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000199; CHECK: br_if $1, BB5_2{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000200; CHECK: br BB5_3{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000201; CHECK: BB5_2:
202; CHECK: BB5_3:
Dan Gohman4ba48162015-11-18 16:12:01 +0000203; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000204; OPT-LABEL: diamond:
205; OPT: block BB5_3{{$}}
206; OPT: block BB5_2{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000207; OPT: br_if {{[^,]*}}, BB5_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000208; OPT: br BB5_3{{$}}
209; OPT: BB5_2:
210; OPT: BB5_3:
211; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000212define i32 @diamond(i32* %p, i32 %a) {
213entry:
214 %c = icmp eq i32 %a, 0
215 store volatile i32 0, i32* %p
216 br i1 %c, label %true, label %false
217true:
218 store volatile i32 1, i32* %p
219 br label %exit
220false:
221 store volatile i32 2, i32* %p
222 br label %exit
223exit:
224 store volatile i32 3, i32* %p
225 ret i32 0
226}
227
Dan Gohmane51c0582015-10-06 00:27:55 +0000228; CHECK-LABEL: single_block:
Dan Gohman950a13c2015-09-16 16:51:30 +0000229; CHECK-NOT: br
Dan Gohman81719f82015-11-25 16:55:01 +0000230; CHECK: return $pop{{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000231; OPT-LABEL: single_block:
232; OPT-NOT: br
233; OPT: return $pop{{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000234define i32 @single_block(i32* %p) {
235entry:
236 store volatile i32 0, i32* %p
237 ret i32 0
238}
239
Dan Gohmane51c0582015-10-06 00:27:55 +0000240; CHECK-LABEL: minimal_loop:
Dan Gohman950a13c2015-09-16 16:51:30 +0000241; CHECK-NOT: br
242; CHECK: BB7_1:
Derek Schuff9d779522015-12-05 00:26:39 +0000243; CHECK: i32.store $discard=, 0($0), $pop{{[0-9]+}}{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000244; CHECK: br BB7_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000245; CHECK: BB7_2:
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000246; OPT-LABEL: minimal_loop:
247; OPT-NOT: br
248; OPT: BB7_1:
249; OPT: i32.store $discard=, 0($0), $pop{{[0-9]+}}{{$}}
250; OPT: br BB7_1{{$}}
251; OPT: BB7_2:
Dan Gohman950a13c2015-09-16 16:51:30 +0000252define i32 @minimal_loop(i32* %p) {
253entry:
254 store volatile i32 0, i32* %p
255 br label %loop
256loop:
257 store volatile i32 1, i32* %p
258 br label %loop
259}
260
Dan Gohmane51c0582015-10-06 00:27:55 +0000261; CHECK-LABEL: simple_loop:
Dan Gohman950a13c2015-09-16 16:51:30 +0000262; CHECK-NOT: br
263; CHECK: BB8_1:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000264; CHECK: loop BB8_2{{$}}
Dan Gohman4ba48162015-11-18 16:12:01 +0000265; CHECK: br_if $pop{{[0-9]+}}, BB8_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000266; CHECK: BB8_2:
Dan Gohman4ba48162015-11-18 16:12:01 +0000267; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000268; OPT-LABEL: simple_loop:
269; OPT-NOT: br
270; OPT: BB8_1:
271; OPT: loop BB8_2{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000272; OPT: br_if {{[^,]*}}, BB8_1{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000273; OPT: BB8_2:
274; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000275define i32 @simple_loop(i32* %p, i32 %a) {
276entry:
277 %c = icmp eq i32 %a, 0
278 store volatile i32 0, i32* %p
279 br label %loop
280loop:
281 store volatile i32 1, i32* %p
282 br i1 %c, label %loop, label %exit
283exit:
284 store volatile i32 2, i32* %p
285 ret i32 0
286}
287
Dan Gohmane51c0582015-10-06 00:27:55 +0000288; CHECK-LABEL: doubletriangle:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000289; CHECK: block BB9_4{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000290; CHECK: br_if $0, BB9_4{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000291; CHECK: block BB9_3{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000292; CHECK: br_if $1, BB9_3{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000293; CHECK: BB9_3:
294; CHECK: BB9_4:
Dan Gohman4ba48162015-11-18 16:12:01 +0000295; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000296; OPT-LABEL: doubletriangle:
297; OPT: block BB9_4{{$}}
298; OPT: br_if $0, BB9_4{{$}}
299; OPT: block BB9_3{{$}}
300; OPT: br_if $1, BB9_3{{$}}
301; OPT: BB9_3:
302; OPT: BB9_4:
303; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000304define i32 @doubletriangle(i32 %a, i32 %b, i32* %p) {
305entry:
306 %c = icmp eq i32 %a, 0
307 %d = icmp eq i32 %b, 0
308 store volatile i32 0, i32* %p
309 br i1 %c, label %true, label %exit
310true:
311 store volatile i32 2, i32* %p
312 br i1 %d, label %tt, label %tf
313tt:
314 store volatile i32 3, i32* %p
315 br label %tf
316tf:
317 store volatile i32 4, i32* %p
318 br label %exit
319exit:
320 store volatile i32 5, i32* %p
321 ret i32 0
322}
323
Dan Gohmane51c0582015-10-06 00:27:55 +0000324; CHECK-LABEL: ifelse_earlyexits:
Dan Gohmancf4748f2015-11-12 17:04:33 +0000325; CHECK: block BB10_4{{$}}
326; CHECK: block BB10_2{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000327; CHECK: br_if $0, BB10_2{{$}}
Dan Gohmancf4748f2015-11-12 17:04:33 +0000328; CHECK: br BB10_4{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000329; CHECK: BB10_2:
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000330; CHECK: br_if $1, BB10_4{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000331; CHECK: BB10_4:
Dan Gohman4ba48162015-11-18 16:12:01 +0000332; CHECK: return ${{[0-9]+}}{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000333; OPT-LABEL: ifelse_earlyexits:
334; OPT: block BB10_4{{$}}
335; OPT: block BB10_3{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000336; OPT: br_if {{[^,]*}}, BB10_3{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000337; OPT: br_if $1, BB10_4{{$}}
338; OPT: br BB10_4{{$}}
339; OPT: BB10_3:
340; OPT: BB10_4:
341; OPT: return ${{[0-9]+}}{{$}}
Dan Gohman950a13c2015-09-16 16:51:30 +0000342define i32 @ifelse_earlyexits(i32 %a, i32 %b, i32* %p) {
343entry:
344 %c = icmp eq i32 %a, 0
345 %d = icmp eq i32 %b, 0
346 store volatile i32 0, i32* %p
347 br i1 %c, label %true, label %false
348true:
349 store volatile i32 1, i32* %p
350 br label %exit
351false:
352 store volatile i32 2, i32* %p
353 br i1 %d, label %ft, label %exit
354ft:
355 store volatile i32 3, i32* %p
356 br label %exit
357exit:
358 store volatile i32 4, i32* %p
359 ret i32 0
360}
Dan Gohmane3e4a5f2015-10-02 21:11:36 +0000361
Dan Gohman32807932015-11-23 16:19:56 +0000362; CHECK-LABEL: doublediamond_in_a_loop:
363; CHECK: BB11_1:
364; CHECK: loop BB11_7{{$}}
365; CHECK: block BB11_6{{$}}
366; CHECK: block BB11_3{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000367; CHECK: br_if $0, BB11_3{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000368; CHECK: br BB11_6{{$}}
369; CHECK: BB11_3:
370; CHECK: block BB11_5{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000371; CHECK: br_if $1, BB11_5{{$}}
Dan Gohman32807932015-11-23 16:19:56 +0000372; CHECK: br BB11_6{{$}}
373; CHECK: BB11_5:
374; CHECK: BB11_6:
375; CHECK: br BB11_1{{$}}
Dan Gohmanf6857222015-11-23 19:12:37 +0000376; CHECK: BB11_7:
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000377; OPT-LABEL: doublediamond_in_a_loop:
378; OPT: BB11_1:
379; OPT: loop BB11_7{{$}}
380; OPT: block BB11_6{{$}}
381; OPT: block BB11_5{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000382; OPT: br_if {{[^,]*}}, BB11_5{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000383; OPT: block BB11_4{{$}}
Dan Gohman6ddce712015-12-06 19:31:44 +0000384; OPT: br_if {{[^,]*}}, BB11_4{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000385; OPT: br BB11_6{{$}}
386; OPT: BB11_4:
387; OPT: br BB11_6{{$}}
388; OPT: BB11_5:
389; OPT: BB11_6:
390; OPT: br BB11_1{{$}}
391; OPT: BB11_7:
Dan Gohman32807932015-11-23 16:19:56 +0000392define i32 @doublediamond_in_a_loop(i32 %a, i32 %b, i32* %p) {
393entry:
394 br label %header
395header:
396 %c = icmp eq i32 %a, 0
397 %d = icmp eq i32 %b, 0
398 store volatile i32 0, i32* %p
399 br i1 %c, label %true, label %false
400true:
401 store volatile i32 1, i32* %p
402 br label %exit
403false:
404 store volatile i32 2, i32* %p
405 br i1 %d, label %ft, label %ff
406ft:
407 store volatile i32 3, i32* %p
408 br label %exit
409ff:
410 store volatile i32 4, i32* %p
411 br label %exit
412exit:
413 store volatile i32 5, i32* %p
414 br label %header
415}
416
Dan Gohmane3e4a5f2015-10-02 21:11:36 +0000417; Test that nested loops are handled.
418
419declare void @bar()
420
421define void @test3(i32 %w) {
422entry:
423 br i1 undef, label %outer.ph, label %exit
424
425outer.ph:
426 br label %outer
427
428outer:
429 %tobool = icmp eq i32 undef, 0
430 br i1 %tobool, label %inner, label %unreachable
431
432unreachable:
433 unreachable
434
435inner:
436 %c = icmp eq i32 undef, %w
437 br i1 %c, label %if.end, label %inner
438
439exit:
440 ret void
441
442if.end:
443 call void @bar()
444 br label %outer
445}