18a9cb242SWouter van Oortmerssen; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 | FileCheck %s
2950a13cfSDan Gohman
3950a13cfSDan Gohman; Test the CFG stackifier pass.
4950a13cfSDan Gohman
52e64438aSDan Gohman; Explicitly disable fast-isel, since it gets implicitly enabled in the
62e64438aSDan Gohman; optnone test.
72e64438aSDan Gohman
8a5908009SSam Cleggtarget triple = "wasm32-unknown-unknown"
9950a13cfSDan Gohman
10950a13cfSDan Gohmandeclare void @something()
11950a13cfSDan Gohman
12950a13cfSDan Gohman; Test that loops are made contiguous, even in the presence of split backedges.
13950a13cfSDan Gohman
14e51c058eSDan Gohman; CHECK-LABEL: test0:
15e51c058eSDan Gohman; CHECK: loop
16442bfcecSDan Gohman; CHECK-NEXT: block
1712de0b91SDan Gohman; CHECK:      i32.lt_s
188fe7e86bSDan Gohman; CHECK-NEXT: br_if
19442bfcecSDan Gohman; CHECK-NEXT: return
20ace7a086SHeejin Ahn; CHECK-NEXT: .LBB{{[0-9]+}}_3:
21442bfcecSDan Gohman; CHECK-NEXT: end_block
22bb865231SReid Kleckner; CHECK-NEXT: i32.const
23bb865231SReid Kleckner; CHECK-NEXT: i32.add
24442bfcecSDan Gohman; CHECK-NEXT: call
25442bfcecSDan Gohman; CHECK-NEXT: br
26ace7a086SHeejin Ahn; CHECK-NEXT: .LBB{{[0-9]+}}_4:
27442bfcecSDan Gohman; CHECK-NEXT: end_loop
28950a13cfSDan Gohmandefine void @test0(i32 %n) {
29950a13cfSDan Gohmanentry:
30950a13cfSDan Gohman  br label %header
31950a13cfSDan Gohman
32950a13cfSDan Gohmanheader:
33950a13cfSDan Gohman  %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
34950a13cfSDan Gohman  %i.next = add i32 %i, 1
35950a13cfSDan Gohman
36950a13cfSDan Gohman  %c = icmp slt i32 %i.next, %n
37950a13cfSDan Gohman  br i1 %c, label %back, label %exit
38950a13cfSDan Gohman
39950a13cfSDan Gohmanexit:
40950a13cfSDan Gohman  ret void
41950a13cfSDan Gohman
42950a13cfSDan Gohmanback:
43950a13cfSDan Gohman  call void @something()
44950a13cfSDan Gohman  br label %header
45950a13cfSDan Gohman}
46950a13cfSDan Gohman
47950a13cfSDan Gohman; Same as test0, but the branch condition is reversed.
48950a13cfSDan Gohman
49e51c058eSDan Gohman; CHECK-LABEL: test1:
50e51c058eSDan Gohman; CHECK: loop
51442bfcecSDan Gohman; CHECK-NEXT: block
5212de0b91SDan Gohman; CHECK:      i32.lt_s
538fe7e86bSDan Gohman; CHECK-NEXT: br_if
54442bfcecSDan Gohman; CHECK-NEXT: return
55ace7a086SHeejin Ahn; CHECK-NEXT: .LBB{{[0-9]+}}_3:
56442bfcecSDan Gohman; CHECK-NEXT: end_block
57bb865231SReid Kleckner; CHECK-NEXT: i32.const
58bb865231SReid Kleckner; CHECK-NEXT: i32.add
59442bfcecSDan Gohman; CHECK-NEXT: call
60442bfcecSDan Gohman; CHECK-NEXT: br
61ace7a086SHeejin Ahn; CHECK-NEXT: .LBB{{[0-9]+}}_4:
62442bfcecSDan Gohman; CHECK-NEXT: end_loop
63950a13cfSDan Gohmandefine void @test1(i32 %n) {
64950a13cfSDan Gohmanentry:
65950a13cfSDan Gohman  br label %header
66950a13cfSDan Gohman
67950a13cfSDan Gohmanheader:
68950a13cfSDan Gohman  %i = phi i32 [ 0, %entry ], [ %i.next, %back ]
69950a13cfSDan Gohman  %i.next = add i32 %i, 1
70950a13cfSDan Gohman
71950a13cfSDan Gohman  %c = icmp sge i32 %i.next, %n
72950a13cfSDan Gohman  br i1 %c, label %exit, label %back
73950a13cfSDan Gohman
74950a13cfSDan Gohmanexit:
75950a13cfSDan Gohman  ret void
76950a13cfSDan Gohman
77950a13cfSDan Gohmanback:
78950a13cfSDan Gohman  call void @something()
79950a13cfSDan Gohman  br label %header
80950a13cfSDan Gohman}
81950a13cfSDan Gohman
82950a13cfSDan Gohman; Test that a simple loop is handled as expected.
83950a13cfSDan Gohman
84e51c058eSDan Gohman; CHECK-LABEL: test2:
858f59cf75SDan Gohman; CHECK-NOT: local
861d68e80fSDan Gohman; CHECK: block   {{$}}
8706b49582SDan Gohman; CHECK: br_if 0, {{[^,]+}}{{$}}
88ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_{{[0-9]+}}:
8912de0b91SDan Gohman; CHECK: loop
9012de0b91SDan Gohman; CHECK: br_if 0, $pop{{[0-9]+}}{{$}}
91ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_{{[0-9]+}}:
9212de0b91SDan Gohman; CHECK: end_loop
9312de0b91SDan Gohman; CHECK: end_block
94e51c058eSDan Gohman; CHECK: return{{$}}
95950a13cfSDan Gohmandefine void @test2(double* nocapture %p, i32 %n) {
96950a13cfSDan Gohmanentry:
97950a13cfSDan Gohman  %cmp.4 = icmp sgt i32 %n, 0
98950a13cfSDan Gohman  br i1 %cmp.4, label %for.body.preheader, label %for.end
99950a13cfSDan Gohman
100950a13cfSDan Gohmanfor.body.preheader:
101950a13cfSDan Gohman  br label %for.body
102950a13cfSDan Gohman
103950a13cfSDan Gohmanfor.body:
104950a13cfSDan Gohman  %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ]
105950a13cfSDan Gohman  %arrayidx = getelementptr inbounds double, double* %p, i32 %i.05
106950a13cfSDan Gohman  %0 = load double, double* %arrayidx, align 8
107950a13cfSDan Gohman  %mul = fmul double %0, 3.200000e+00
108950a13cfSDan Gohman  store double %mul, double* %arrayidx, align 8
109950a13cfSDan Gohman  %inc = add nuw nsw i32 %i.05, 1
110950a13cfSDan Gohman  %exitcond = icmp eq i32 %inc, %n
111950a13cfSDan Gohman  br i1 %exitcond, label %for.end.loopexit, label %for.body
112950a13cfSDan Gohman
113950a13cfSDan Gohmanfor.end.loopexit:
114950a13cfSDan Gohman  br label %for.end
115950a13cfSDan Gohman
116950a13cfSDan Gohmanfor.end:
117950a13cfSDan Gohman  ret void
118950a13cfSDan Gohman}
119950a13cfSDan Gohman
120e51c058eSDan Gohman; CHECK-LABEL: doublediamond:
1211d68e80fSDan Gohman; CHECK: block   {{$}}
1221d68e80fSDan Gohman; CHECK-NEXT: block   {{$}}
12306b49582SDan Gohman; CHECK: br_if 0, ${{[^,]+}}{{$}}
1241d68e80fSDan Gohman; CHECK: br 1{{$}}
125ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_2:
1261d68e80fSDan Gohman; CHECK-NEXT: end_block{{$}}
1271d68e80fSDan Gohman; CHECK: block   {{$}}
12806b49582SDan Gohman; CHECK: br_if 0, ${{[^,]+}}{{$}}
1291d68e80fSDan Gohman; CHECK: br 1{{$}}
130ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_4:
1311d68e80fSDan Gohman; CHECK-NEXT: end_block{{$}}
132ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_5:
1331d68e80fSDan Gohman; CHECK-NEXT: end_block{{$}}
134b6fd39a3SDan Gohman; CHECK: i32.const $push{{[0-9]+}}=, 0{{$}}
135b6fd39a3SDan Gohman; CHECK-NEXT: return $pop{{[0-9]+}}{{$}}
136950a13cfSDan Gohmandefine i32 @doublediamond(i32 %a, i32 %b, i32* %p) {
137950a13cfSDan Gohmanentry:
138950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
139950a13cfSDan Gohman  %d = icmp eq i32 %b, 0
140950a13cfSDan Gohman  store volatile i32 0, i32* %p
141950a13cfSDan Gohman  br i1 %c, label %true, label %false
142950a13cfSDan Gohmantrue:
143950a13cfSDan Gohman  store volatile i32 1, i32* %p
144950a13cfSDan Gohman  br label %exit
145950a13cfSDan Gohmanfalse:
146950a13cfSDan Gohman  store volatile i32 2, i32* %p
147950a13cfSDan Gohman  br i1 %d, label %ft, label %ff
148950a13cfSDan Gohmanft:
149950a13cfSDan Gohman  store volatile i32 3, i32* %p
150950a13cfSDan Gohman  br label %exit
151950a13cfSDan Gohmanff:
152950a13cfSDan Gohman  store volatile i32 4, i32* %p
153950a13cfSDan Gohman  br label %exit
154950a13cfSDan Gohmanexit:
155950a13cfSDan Gohman  store volatile i32 5, i32* %p
156950a13cfSDan Gohman  ret i32 0
157950a13cfSDan Gohman}
158950a13cfSDan Gohman
159e51c058eSDan Gohman; CHECK-LABEL: triangle:
1601d68e80fSDan Gohman; CHECK: block   {{$}}
16106b49582SDan Gohman; CHECK: br_if 0, $1{{$}}
162ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_2:
163c9623db8SDan Gohman; CHECK: return
164950a13cfSDan Gohmandefine i32 @triangle(i32* %p, i32 %a) {
165950a13cfSDan Gohmanentry:
166950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
167950a13cfSDan Gohman  store volatile i32 0, i32* %p
168950a13cfSDan Gohman  br i1 %c, label %true, label %exit
169950a13cfSDan Gohmantrue:
170950a13cfSDan Gohman  store volatile i32 1, i32* %p
171950a13cfSDan Gohman  br label %exit
172950a13cfSDan Gohmanexit:
173950a13cfSDan Gohman  store volatile i32 2, i32* %p
174950a13cfSDan Gohman  ret i32 0
175950a13cfSDan Gohman}
176950a13cfSDan Gohman
177e51c058eSDan Gohman; CHECK-LABEL: diamond:
1781d68e80fSDan Gohman; CHECK: block   {{$}}
1791d68e80fSDan Gohman; CHECK: block   {{$}}
18006b49582SDan Gohman; CHECK: br_if 0, $1{{$}}
1811d68e80fSDan Gohman; CHECK: br 1{{$}}
182ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_2:
183ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_3:
184b6fd39a3SDan Gohman; CHECK: i32.const $push{{[0-9]+}}=, 0{{$}}
185b6fd39a3SDan Gohman; CHECK-NEXT: return $pop{{[0-9]+}}{{$}}
186950a13cfSDan Gohmandefine i32 @diamond(i32* %p, i32 %a) {
187950a13cfSDan Gohmanentry:
188950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
189950a13cfSDan Gohman  store volatile i32 0, i32* %p
190950a13cfSDan Gohman  br i1 %c, label %true, label %false
191950a13cfSDan Gohmantrue:
192950a13cfSDan Gohman  store volatile i32 1, i32* %p
193950a13cfSDan Gohman  br label %exit
194950a13cfSDan Gohmanfalse:
195950a13cfSDan Gohman  store volatile i32 2, i32* %p
196950a13cfSDan Gohman  br label %exit
197950a13cfSDan Gohmanexit:
198950a13cfSDan Gohman  store volatile i32 3, i32* %p
199950a13cfSDan Gohman  ret i32 0
200950a13cfSDan Gohman}
201950a13cfSDan Gohman
202e51c058eSDan Gohman; CHECK-LABEL: single_block:
203950a13cfSDan Gohman; CHECK-NOT: br
20481719f85SDan Gohman; CHECK: return $pop{{[0-9]+}}{{$}}
205950a13cfSDan Gohmandefine i32 @single_block(i32* %p) {
206950a13cfSDan Gohmanentry:
207950a13cfSDan Gohman  store volatile i32 0, i32* %p
208950a13cfSDan Gohman  ret i32 0
209950a13cfSDan Gohman}
210950a13cfSDan Gohman
211e51c058eSDan Gohman; CHECK-LABEL: minimal_loop:
212950a13cfSDan Gohman; CHECK-NOT: br
213ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_1:
2142726b88cSDan Gohman; CHECK: loop i32
2157f1bdb2eSDan Gohman; CHECK: i32.store 0($0), $pop{{[0-9]+}}{{$}}
2161d68e80fSDan Gohman; CHECK: br 0{{$}}
217ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_2:
2182726b88cSDan Gohmandefine i32 @minimal_loop(i32* %p) {
219950a13cfSDan Gohmanentry:
220950a13cfSDan Gohman  store volatile i32 0, i32* %p
221950a13cfSDan Gohman  br label %loop
222950a13cfSDan Gohmanloop:
223950a13cfSDan Gohman  store volatile i32 1, i32* %p
224950a13cfSDan Gohman  br label %loop
225950a13cfSDan Gohman}
226950a13cfSDan Gohman
227e51c058eSDan Gohman; CHECK-LABEL: simple_loop:
228950a13cfSDan Gohman; CHECK-NOT: br
229ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_1:
2301d68e80fSDan Gohman; CHECK: loop    {{$}}
23106b49582SDan Gohman; CHECK: br_if 0, $pop{{[0-9]+}}{{$}}
2321d68e80fSDan Gohman; CHECK-NEXT: end_loop{{$}}
233b6fd39a3SDan Gohman; CHECK: i32.const $push{{[0-9]+}}=, 0{{$}}
234b6fd39a3SDan Gohman; CHECK-NEXT: return $pop{{[0-9]+}}{{$}}
235950a13cfSDan Gohmandefine i32 @simple_loop(i32* %p, i32 %a) {
236950a13cfSDan Gohmanentry:
237950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
238950a13cfSDan Gohman  store volatile i32 0, i32* %p
239950a13cfSDan Gohman  br label %loop
240950a13cfSDan Gohmanloop:
241950a13cfSDan Gohman  store volatile i32 1, i32* %p
242950a13cfSDan Gohman  br i1 %c, label %loop, label %exit
243950a13cfSDan Gohmanexit:
244950a13cfSDan Gohman  store volatile i32 2, i32* %p
245950a13cfSDan Gohman  ret i32 0
246950a13cfSDan Gohman}
247950a13cfSDan Gohman
248e51c058eSDan Gohman; CHECK-LABEL: doubletriangle:
2491d68e80fSDan Gohman; CHECK: block   {{$}}
25006b49582SDan Gohman; CHECK: br_if 0, $0{{$}}
2511d68e80fSDan Gohman; CHECK: block   {{$}}
25206b49582SDan Gohman; CHECK: br_if 0, $1{{$}}
253ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_3:
254ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_4:
255c9623db8SDan Gohman; CHECK: return
256950a13cfSDan Gohmandefine i32 @doubletriangle(i32 %a, i32 %b, i32* %p) {
257950a13cfSDan Gohmanentry:
258950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
259950a13cfSDan Gohman  %d = icmp eq i32 %b, 0
260950a13cfSDan Gohman  store volatile i32 0, i32* %p
261950a13cfSDan Gohman  br i1 %c, label %true, label %exit
262950a13cfSDan Gohmantrue:
263950a13cfSDan Gohman  store volatile i32 2, i32* %p
264950a13cfSDan Gohman  br i1 %d, label %tt, label %tf
265950a13cfSDan Gohmantt:
266950a13cfSDan Gohman  store volatile i32 3, i32* %p
267950a13cfSDan Gohman  br label %tf
268950a13cfSDan Gohmantf:
269950a13cfSDan Gohman  store volatile i32 4, i32* %p
270950a13cfSDan Gohman  br label %exit
271950a13cfSDan Gohmanexit:
272950a13cfSDan Gohman  store volatile i32 5, i32* %p
273950a13cfSDan Gohman  ret i32 0
274950a13cfSDan Gohman}
275950a13cfSDan Gohman
276e51c058eSDan Gohman; CHECK-LABEL: ifelse_earlyexits:
2771d68e80fSDan Gohman; CHECK: block   {{$}}
2781d68e80fSDan Gohman; CHECK: block   {{$}}
27906b49582SDan Gohman; CHECK: br_if 0, $0{{$}}
2801d68e80fSDan Gohman; CHECK: br 1{{$}}
281ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_2:
28206b49582SDan Gohman; CHECK: br_if 0, $1{{$}}
283ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_4:
284b6fd39a3SDan Gohman; CHECK: i32.const $push{{[0-9]+}}=, 0{{$}}
285b6fd39a3SDan Gohman; CHECK-NEXT: return $pop{{[0-9]+}}{{$}}
286950a13cfSDan Gohmandefine i32 @ifelse_earlyexits(i32 %a, i32 %b, i32* %p) {
287950a13cfSDan Gohmanentry:
288950a13cfSDan Gohman  %c = icmp eq i32 %a, 0
289950a13cfSDan Gohman  %d = icmp eq i32 %b, 0
290950a13cfSDan Gohman  store volatile i32 0, i32* %p
291950a13cfSDan Gohman  br i1 %c, label %true, label %false
292950a13cfSDan Gohmantrue:
293950a13cfSDan Gohman  store volatile i32 1, i32* %p
294950a13cfSDan Gohman  br label %exit
295950a13cfSDan Gohmanfalse:
296950a13cfSDan Gohman  store volatile i32 2, i32* %p
297950a13cfSDan Gohman  br i1 %d, label %ft, label %exit
298950a13cfSDan Gohmanft:
299950a13cfSDan Gohman  store volatile i32 3, i32* %p
300950a13cfSDan Gohman  br label %exit
301950a13cfSDan Gohmanexit:
302950a13cfSDan Gohman  store volatile i32 4, i32* %p
303950a13cfSDan Gohman  ret i32 0
304950a13cfSDan Gohman}
305e3e4a5ffSDan Gohman
30632807932SDan Gohman; CHECK-LABEL: doublediamond_in_a_loop:
307ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_1:
3082726b88cSDan Gohman; CHECK: loop i32{{$}}
3091d68e80fSDan Gohman; CHECK: block   {{$}}
31006b49582SDan Gohman; CHECK: br_if           0, $0{{$}}
3111d68e80fSDan Gohman; CHECK: br              1{{$}}
312ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_3:
3130cfb5f85SDan Gohman; CHECK: end_block{{$}}
3141d68e80fSDan Gohman; CHECK: block   {{$}}
31506b49582SDan Gohman; CHECK: br_if           0, $1{{$}}
3161d68e80fSDan Gohman; CHECK: br              1{{$}}
317ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_5:
3181d68e80fSDan Gohman; CHECK: br              0{{$}}
319ace7a086SHeejin Ahn; CHECK: .LBB{{[0-9]+}}_6:
3201d68e80fSDan Gohman; CHECK-NEXT: end_loop{{$}}
3212726b88cSDan Gohmandefine i32 @doublediamond_in_a_loop(i32 %a, i32 %b, i32* %p) {
32232807932SDan Gohmanentry:
32332807932SDan Gohman  br label %header
32432807932SDan Gohmanheader:
32532807932SDan Gohman  %c = icmp eq i32 %a, 0
32632807932SDan Gohman  %d = icmp eq i32 %b, 0
32732807932SDan Gohman  store volatile i32 0, i32* %p
32832807932SDan Gohman  br i1 %c, label %true, label %false
32932807932SDan Gohmantrue:
33032807932SDan Gohman  store volatile i32 1, i32* %p
33132807932SDan Gohman  br label %exit
33232807932SDan Gohmanfalse:
33332807932SDan Gohman  store volatile i32 2, i32* %p
33432807932SDan Gohman  br i1 %d, label %ft, label %ff
33532807932SDan Gohmanft:
33632807932SDan Gohman  store volatile i32 3, i32* %p
33732807932SDan Gohman  br label %exit
33832807932SDan Gohmanff:
33932807932SDan Gohman  store volatile i32 4, i32* %p
34032807932SDan Gohman  br label %exit
34132807932SDan Gohmanexit:
34232807932SDan Gohman  store volatile i32 5, i32* %p
34332807932SDan Gohman  br label %header
34432807932SDan Gohman}
34532807932SDan Gohman
346e3e4a5ffSDan Gohman; Test that nested loops are handled.
347e3e4a5ffSDan Gohman
3488fe7e86bSDan Gohman; CHECK-LABEL: test3:
3498fe7e86bSDan Gohman; CHECK: loop
3508fe7e86bSDan Gohman; CHECK-NEXT: br_if
351a4730cf0SDan Gohman; CHECK-NEXT: .LBB{{[0-9]+}}_{{[0-9]+}}:
3528fe7e86bSDan Gohman; CHECK-NEXT: loop
353e3e4a5ffSDan Gohmandeclare void @bar()
354918d0c2bSSimon Pilgrimdefine void @test3(i32 %w, i32 %x)  {
355e3e4a5ffSDan Gohmanentry:
356e3e4a5ffSDan Gohman  br i1 undef, label %outer.ph, label %exit
357e3e4a5ffSDan Gohman
358e3e4a5ffSDan Gohmanouter.ph:
359e3e4a5ffSDan Gohman  br label %outer
360e3e4a5ffSDan Gohman
361e3e4a5ffSDan Gohmanouter:
362918d0c2bSSimon Pilgrim  %tobool = icmp eq i32 %x, 0
363e3e4a5ffSDan Gohman  br i1 %tobool, label %inner, label %unreachable
364e3e4a5ffSDan Gohman
365e3e4a5ffSDan Gohmanunreachable:
366e3e4a5ffSDan Gohman  unreachable
367e3e4a5ffSDan Gohman
368e3e4a5ffSDan Gohmaninner:
369918d0c2bSSimon Pilgrim  %c = icmp eq i32 %x, %w
370e3e4a5ffSDan Gohman  br i1 %c, label %if.end, label %inner
371e3e4a5ffSDan Gohman
372e3e4a5ffSDan Gohmanexit:
373e3e4a5ffSDan Gohman  ret void
374e3e4a5ffSDan Gohman
375e3e4a5ffSDan Gohmanif.end:
376e3e4a5ffSDan Gohman  call void @bar()
377e3e4a5ffSDan Gohman  br label %outer
378e3e4a5ffSDan Gohman}
3798fe7e86bSDan Gohman
3808fe7e86bSDan Gohman; Test switch lowering and block placement.
3818fe7e86bSDan Gohman
3828fe7e86bSDan Gohman; CHECK-LABEL: test4:
38349482f82SWouter van Oortmerssen; CHECK-NEXT: .functype test4 (i32) -> (){{$}}
3841d68e80fSDan Gohman; CHECK-NEXT: block   {{$}}
385*7f50c15bSThomas Lively; CHECK-NEXT: block   {{$}}
386*7f50c15bSThomas Lively; CHECK-NEXT: br_table   $0, 1, 1, 1, 1, 1, 0{{$}}
387*7f50c15bSThomas Lively; CHECK-NEXT: .LBB{{[0-9]+}}_1:
3881d68e80fSDan Gohman; CHECK-NEXT: end_block{{$}}
389*7f50c15bSThomas Lively; CHECK-NEXT: i32.const $push[[C:[0-9]+]]=, 622{{$}}
390*7f50c15bSThomas Lively; CHECK-NEXT: i32.eq $drop=, $0, $pop[[C]]{{$}}
391*7f50c15bSThomas Lively; CHECK-NEXT: .LBB{{[0-9]+}}_2:
392755a8959SThomas Lively; CHECK-NEXT: end_block{{$}}
3938fe7e86bSDan Gohman; CHECK-NEXT: return{{$}}
3948fe7e86bSDan Gohmandefine void @test4(i32 %t) {
3958fe7e86bSDan Gohmanentry:
3968fe7e86bSDan Gohman  switch i32 %t, label %default [
3978fe7e86bSDan Gohman    i32 0, label %bb2
3988fe7e86bSDan Gohman    i32 2, label %bb2
3998fe7e86bSDan Gohman    i32 4, label %bb1
4008fe7e86bSDan Gohman    i32 622, label %bb0
4018fe7e86bSDan Gohman  ]
4028fe7e86bSDan Gohman
4038fe7e86bSDan Gohmanbb0:
4048fe7e86bSDan Gohman  ret void
4058fe7e86bSDan Gohman
4068fe7e86bSDan Gohmanbb1:
4078fe7e86bSDan Gohman  ret void
4088fe7e86bSDan Gohman
4098fe7e86bSDan Gohmanbb2:
4108fe7e86bSDan Gohman  ret void
4118fe7e86bSDan Gohman
4128fe7e86bSDan Gohmandefault:
4138fe7e86bSDan Gohman  ret void
4148fe7e86bSDan Gohman}
4158fe7e86bSDan Gohman
4168fe7e86bSDan Gohman; Test a case where the BLOCK needs to be placed before the LOOP in the
4178fe7e86bSDan Gohman; same basic block.
4188fe7e86bSDan Gohman
4198fe7e86bSDan Gohman; CHECK-LABEL: test5:
420ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
4211d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
4221d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
4233a643e8dSDan Gohman; CHECK:       br_if 1, {{[^,]+}}{{$}}
42406b49582SDan Gohman; CHECK:       br_if 0, {{[^,]+}}{{$}}
4251d68e80fSDan Gohman; CHECK-NEXT:  end_loop{{$}}
4268fe7e86bSDan Gohman; CHECK:       return{{$}}
427ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
4288fe7e86bSDan Gohman; CHECK:       return{{$}}
4298fe7e86bSDan Gohmandefine void @test5(i1 %p, i1 %q) {
4308fe7e86bSDan Gohmanentry:
4318fe7e86bSDan Gohman  br label %header
4328fe7e86bSDan Gohman
4338fe7e86bSDan Gohmanheader:
4348fe7e86bSDan Gohman  store volatile i32 0, i32* null
4358fe7e86bSDan Gohman  br i1 %p, label %more, label %alt
4368fe7e86bSDan Gohman
4378fe7e86bSDan Gohmanmore:
4388fe7e86bSDan Gohman  store volatile i32 1, i32* null
4398fe7e86bSDan Gohman  br i1 %q, label %header, label %return
4408fe7e86bSDan Gohman
4418fe7e86bSDan Gohmanalt:
4428fe7e86bSDan Gohman  store volatile i32 2, i32* null
4438fe7e86bSDan Gohman  ret void
4448fe7e86bSDan Gohman
4458fe7e86bSDan Gohmanreturn:
4468fe7e86bSDan Gohman  store volatile i32 3, i32* null
4478fe7e86bSDan Gohman  ret void
4488fe7e86bSDan Gohman}
4498fe7e86bSDan Gohman
4508fe7e86bSDan Gohman; Test an interesting case of a loop with multiple exits, which
4518fe7e86bSDan Gohman; aren't to layout successors of the loop, and one of which is to a successors
4528fe7e86bSDan Gohman; which has another predecessor.
4538fe7e86bSDan Gohman
4548fe7e86bSDan Gohman; CHECK-LABEL: test6:
455ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
4561d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
4571d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
4581d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
4598fe7e86bSDan Gohman; CHECK-NOT:   block
46006b49582SDan Gohman; CHECK:       br_if 2, {{[^,]+}}{{$}}
4618fe7e86bSDan Gohman; CHECK-NOT:   block
4623a643e8dSDan Gohman; CHECK:       br_if 1, {{[^,]+}}{{$}}
4633a643e8dSDan Gohman; CHECK-NOT:   block
46406b49582SDan Gohman; CHECK:       br_if 0, {{[^,]+}}{{$}}
4651d68e80fSDan Gohman; CHECK-NEXT:  end_loop{{$}}
4668fe7e86bSDan Gohman; CHECK-NOT:   block
4678fe7e86bSDan Gohman; CHECK:       return{{$}}
468ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_5:
4691d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
4708fe7e86bSDan Gohman; CHECK-NOT:   block
471ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_6:
4721d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
4738fe7e86bSDan Gohman; CHECK-NOT:   block
4748fe7e86bSDan Gohman; CHECK:       return{{$}}
4758fe7e86bSDan Gohmandefine void @test6(i1 %p, i1 %q) {
4768fe7e86bSDan Gohmanentry:
4778fe7e86bSDan Gohman  br label %header
4788fe7e86bSDan Gohman
4798fe7e86bSDan Gohmanheader:
4808fe7e86bSDan Gohman  store volatile i32 0, i32* null
4818fe7e86bSDan Gohman  br i1 %p, label %more, label %second
4828fe7e86bSDan Gohman
4838fe7e86bSDan Gohmanmore:
4848fe7e86bSDan Gohman  store volatile i32 1, i32* null
4858fe7e86bSDan Gohman  br i1 %q, label %evenmore, label %first
4868fe7e86bSDan Gohman
4878fe7e86bSDan Gohmanevenmore:
4888fe7e86bSDan Gohman  store volatile i32 1, i32* null
4898fe7e86bSDan Gohman  br i1 %q, label %header, label %return
4908fe7e86bSDan Gohman
4918fe7e86bSDan Gohmanreturn:
4928fe7e86bSDan Gohman  store volatile i32 2, i32* null
4938fe7e86bSDan Gohman  ret void
4948fe7e86bSDan Gohman
4958fe7e86bSDan Gohmanfirst:
4968fe7e86bSDan Gohman  store volatile i32 3, i32* null
4978fe7e86bSDan Gohman  br label %second
4988fe7e86bSDan Gohman
4998fe7e86bSDan Gohmansecond:
5008fe7e86bSDan Gohman  store volatile i32 4, i32* null
5018fe7e86bSDan Gohman  ret void
5028fe7e86bSDan Gohman}
5038fe7e86bSDan Gohman
5048fe7e86bSDan Gohman; Test a case where there are multiple backedges and multiple loop exits
5058fe7e86bSDan Gohman; that end in unreachable.
5068fe7e86bSDan Gohman
5078fe7e86bSDan Gohman; CHECK-LABEL: test7:
508ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
5091d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
5108fe7e86bSDan Gohman; CHECK-NOT:   block
5111d68e80fSDan Gohman; CHECK:       block   {{$}}
51206b49582SDan Gohman; CHECK:       br_if 0, {{[^,]+}}{{$}}
5138fe7e86bSDan Gohman; CHECK-NOT:   block
51406b49582SDan Gohman; CHECK:       br_if 1, {{[^,]+}}{{$}}
5158fe7e86bSDan Gohman; CHECK-NOT:   block
5168fe7e86bSDan Gohman; CHECK:       unreachable
517ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
5181d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
5198fe7e86bSDan Gohman; CHECK-NOT:   block
52006b49582SDan Gohman; CHECK:       br_if 0, {{[^,]+}}{{$}}
5211d68e80fSDan Gohman; CHECK-NEXT:  end_loop{{$}}
5228fe7e86bSDan Gohman; CHECK-NOT:   block
5238fe7e86bSDan Gohman; CHECK:       unreachable
5248fe7e86bSDan Gohmandefine void @test7(i1 %tobool2, i1 %tobool9) {
5258fe7e86bSDan Gohmanentry:
5268fe7e86bSDan Gohman  store volatile i32 0, i32* null
5278fe7e86bSDan Gohman  br label %loop
5288fe7e86bSDan Gohman
5298fe7e86bSDan Gohmanloop:
5308fe7e86bSDan Gohman  store volatile i32 1, i32* null
5318fe7e86bSDan Gohman  br i1 %tobool2, label %l1, label %l0
5328fe7e86bSDan Gohman
5338fe7e86bSDan Gohmanl0:
5348fe7e86bSDan Gohman  store volatile i32 2, i32* null
5358fe7e86bSDan Gohman  br i1 %tobool9, label %loop, label %u0
5368fe7e86bSDan Gohman
5378fe7e86bSDan Gohmanl1:
5388fe7e86bSDan Gohman  store volatile i32 3, i32* null
5398fe7e86bSDan Gohman  br i1 %tobool9, label %loop, label %u1
5408fe7e86bSDan Gohman
5418fe7e86bSDan Gohmanu0:
5428fe7e86bSDan Gohman  store volatile i32 4, i32* null
5438fe7e86bSDan Gohman  unreachable
5448fe7e86bSDan Gohman
5458fe7e86bSDan Gohmanu1:
5468fe7e86bSDan Gohman  store volatile i32 5, i32* null
5478fe7e86bSDan Gohman  unreachable
5488fe7e86bSDan Gohman}
5498fe7e86bSDan Gohman
5508fe7e86bSDan Gohman; Test an interesting case using nested loops and switches.
5518fe7e86bSDan Gohman
5528fe7e86bSDan Gohman; CHECK-LABEL: test8:
553ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
5542726b88cSDan Gohman; CHECK-NEXT:  loop i32{{$}}
555e5d3c15dSDan Gohman; CHECK-NEXT:  i32.const $push{{[^,]+}}, 0{{$}}
55606b49582SDan Gohman; CHECK-NEXT:  br_if    0, {{[^,]+}}{{$}}
5570cfb5f85SDan Gohman; CHECK-NEXT:  br       0{{$}}
558ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_2:
5590cfb5f85SDan Gohman; CHECK-NEXT:  end_loop{{$}}
5602726b88cSDan Gohmandefine i32 @test8() {
5618fe7e86bSDan Gohmanbb:
5628fe7e86bSDan Gohman  br label %bb1
5638fe7e86bSDan Gohman
5648fe7e86bSDan Gohmanbb1:
5658fe7e86bSDan Gohman  br i1 undef, label %bb2, label %bb3
5668fe7e86bSDan Gohman
5678fe7e86bSDan Gohmanbb2:
5688fe7e86bSDan Gohman  switch i8 undef, label %bb1 [
5698fe7e86bSDan Gohman    i8 44, label %bb2
5708fe7e86bSDan Gohman  ]
5718fe7e86bSDan Gohman
5728fe7e86bSDan Gohmanbb3:
5738fe7e86bSDan Gohman  switch i8 undef, label %bb1 [
5748fe7e86bSDan Gohman    i8 44, label %bb2
5758fe7e86bSDan Gohman  ]
5768fe7e86bSDan Gohman}
5778fe7e86bSDan Gohman
5788fe7e86bSDan Gohman; Test an interesting case using nested loops that share a bottom block.
5798fe7e86bSDan Gohman
5808fe7e86bSDan Gohman; CHECK-LABEL: test9:
581ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
5823a643e8dSDan Gohman; CHECK-NEXT:  block   {{$}}
5831d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
5848fe7e86bSDan Gohman; CHECK-NOT:   block
58506b49582SDan Gohman; CHECK:       br_if     1, {{[^,]+}}{{$}}
586ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_2:
5871d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
5888fe7e86bSDan Gohman; CHECK-NOT:   block
5891d68e80fSDan Gohman; CHECK:       block   {{$}}
5908fe7e86bSDan Gohman; CHECK-NOT:   block
59106b49582SDan Gohman; CHECK:       br_if     0, {{[^,]+}}{{$}}
5928fe7e86bSDan Gohman; CHECK-NOT:   block
5933a643e8dSDan Gohman; CHECK:       br_if     2, {{[^,]+}}{{$}}
594d85ab7fcSDan Gohman; CHECK-NEXT:  br        1{{$}}
595ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
5961d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
5978fe7e86bSDan Gohman; CHECK-NOT:   block
5983a643e8dSDan Gohman; CHECK:       br_if     1, {{[^,]+}}{{$}}
599d85ab7fcSDan Gohman; CHECK-NEXT:  br        0{{$}}
600ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_5:
6018fe7e86bSDan Gohman; CHECK-NOT:   block
6023a643e8dSDan Gohman; CHECK:       end_block
6033a643e8dSDan Gohman; CHECK-NOT:   block
6048fe7e86bSDan Gohman; CHECK:       return{{$}}
6058fe7e86bSDan Gohmandeclare i1 @a()
6068fe7e86bSDan Gohmandefine void @test9() {
6078fe7e86bSDan Gohmanentry:
6088fe7e86bSDan Gohman  store volatile i32 0, i32* null
6098fe7e86bSDan Gohman  br label %header
6108fe7e86bSDan Gohman
6118fe7e86bSDan Gohmanheader:
6128fe7e86bSDan Gohman  store volatile i32 1, i32* null
6138fe7e86bSDan Gohman  %call4 = call i1 @a()
6148fe7e86bSDan Gohman  br i1 %call4, label %header2, label %end
6158fe7e86bSDan Gohman
6168fe7e86bSDan Gohmanheader2:
6178fe7e86bSDan Gohman  store volatile i32 2, i32* null
6188fe7e86bSDan Gohman  %call = call i1 @a()
6198fe7e86bSDan Gohman  br i1 %call, label %if.then, label %if.else
6208fe7e86bSDan Gohman
6218fe7e86bSDan Gohmanif.then:
6228fe7e86bSDan Gohman  store volatile i32 3, i32* null
6238fe7e86bSDan Gohman  %call3 = call i1 @a()
6248fe7e86bSDan Gohman  br i1 %call3, label %header2, label %header
6258fe7e86bSDan Gohman
6268fe7e86bSDan Gohmanif.else:
6278fe7e86bSDan Gohman  store volatile i32 4, i32* null
6288fe7e86bSDan Gohman  %call2 = call i1 @a()
6298fe7e86bSDan Gohman  br i1 %call2, label %header2, label %header
6308fe7e86bSDan Gohman
6318fe7e86bSDan Gohmanend:
6328fe7e86bSDan Gohman  store volatile i32 5, i32* null
6338fe7e86bSDan Gohman  ret void
6348fe7e86bSDan Gohman}
6358fe7e86bSDan Gohman
6368fe7e86bSDan Gohman; Test an interesting case involving nested loops sharing a loop bottom,
6378fe7e86bSDan Gohman; and loop exits to a block with unreachable.
6388fe7e86bSDan Gohman
6398fe7e86bSDan Gohman; CHECK-LABEL: test10:
640ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
6411d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
64206b49582SDan Gohman; CHECK:       br_if     0, {{[^,]+}}{{$}}
643ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_3:
6441d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
6451d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
646ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_4:
6471d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
64889bf88c8SDan Gohman; CHECK:       br_if     0, {{[^,]+}}{{$}}
6491d68e80fSDan Gohman; CHECK-NEXT:  end_loop{{$}}
650b9f282d3SHeejin Ahn; CHECK-NEXT:  block   {{$}}
651*7f50c15bSThomas Lively; CHECK-NOT:   br_if
652*7f50c15bSThomas Lively; CHECK:       br_table   $pop{{[^,]+}}, 0, 3, 1, 2, 3
653*7f50c15bSThomas Lively; CHECK-NEXT:  .LBB{{[0-9]+}}_6:
654b9f282d3SHeejin Ahn; CHECK-NEXT:  end_block{{$}}
6551d68e80fSDan Gohman; CHECK-NEXT:  end_loop{{$}}
6568fe7e86bSDan Gohman; CHECK-NEXT:  return{{$}}
657*7f50c15bSThomas Lively; CHECK-NEXT:  .LBB{{[0-9]+}}_7:
6581d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
6591d68e80fSDan Gohman; CHECK:       br        0{{$}}
660*7f50c15bSThomas Lively; CHECK-NEXT:  .LBB{{[0-9]+}}_8:
661b9f282d3SHeejin Ahn; CHECK-NEXT:  end_loop{{$}}
6628fe7e86bSDan Gohmandefine void @test10() {
6638fe7e86bSDan Gohmanbb0:
6648fe7e86bSDan Gohman  br label %bb1
6658fe7e86bSDan Gohman
6668fe7e86bSDan Gohmanbb1:
6678fe7e86bSDan Gohman  %tmp = phi i32 [ 2, %bb0 ], [ 3, %bb3 ]
6688fe7e86bSDan Gohman  %tmp3 = phi i32 [ undef, %bb0 ], [ %tmp11, %bb3 ]
6698fe7e86bSDan Gohman  %tmp4 = icmp eq i32 %tmp3, 0
6708fe7e86bSDan Gohman  br i1 %tmp4, label %bb4, label %bb2
6718fe7e86bSDan Gohman
6728fe7e86bSDan Gohmanbb2:
6738fe7e86bSDan Gohman  br label %bb3
6748fe7e86bSDan Gohman
6758fe7e86bSDan Gohmanbb3:
6768fe7e86bSDan Gohman  %tmp11 = phi i32 [ 1, %bb5 ], [ 0, %bb2 ]
6778fe7e86bSDan Gohman  br label %bb1
6788fe7e86bSDan Gohman
6798fe7e86bSDan Gohmanbb4:
6808fe7e86bSDan Gohman  %tmp6 = phi i32 [ %tmp9, %bb5 ], [ 4, %bb1 ]
6818fe7e86bSDan Gohman  %tmp7 = phi i32 [ %tmp6, %bb5 ], [ %tmp, %bb1 ]
6828fe7e86bSDan Gohman  br label %bb5
6838fe7e86bSDan Gohman
6848fe7e86bSDan Gohmanbb5:
6858fe7e86bSDan Gohman  %tmp9 = phi i32 [ %tmp6, %bb5 ], [ %tmp7, %bb4 ]
6868fe7e86bSDan Gohman  switch i32 %tmp9, label %bb2 [
6878fe7e86bSDan Gohman    i32 0, label %bb5
6888fe7e86bSDan Gohman    i32 1, label %bb6
6898fe7e86bSDan Gohman    i32 3, label %bb4
6908fe7e86bSDan Gohman    i32 4, label %bb3
6918fe7e86bSDan Gohman  ]
6928fe7e86bSDan Gohman
6938fe7e86bSDan Gohmanbb6:
6948fe7e86bSDan Gohman  ret void
6958fe7e86bSDan Gohman}
6968fe7e86bSDan Gohman
6978fe7e86bSDan Gohman; Test a CFG DAG with interesting merging.
6988fe7e86bSDan Gohman
6998fe7e86bSDan Gohman; CHECK-LABEL: test11:
7001d68e80fSDan Gohman; CHECK:       block   {{$}}
7011d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
7021d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
7031d68e80fSDan Gohman; CHECK-NEXT:  block   {{$}}
70406b49582SDan Gohman; CHECK:       br_if        0, {{[^,]+}}{{$}}
7058fe7e86bSDan Gohman; CHECK-NOT:   block
706ed0f1138SDan Gohman; CHECK:       block   {{$}}
707c9623db8SDan Gohman; CHECK-NEXT:  i32.const
70806b49582SDan Gohman; CHECK-NEXT:  br_if        0, {{[^,]+}}{{$}}
7098fe7e86bSDan Gohman; CHECK-NOT:   block
71006b49582SDan Gohman; CHECK:       br_if        2, {{[^,]+}}{{$}}
711ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_3:
7121d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
7138fe7e86bSDan Gohman; CHECK-NOT:   block
7148fe7e86bSDan Gohman; CHECK:       return{{$}}
715ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
7161d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
7178fe7e86bSDan Gohman; CHECK-NOT:   block
71806b49582SDan Gohman; CHECK:       br_if        1, {{[^,]+}}{{$}}
719442bfcecSDan Gohman; CHECK-NOT:   block
720442bfcecSDan Gohman; CHECK:       br_if        2, {{[^,]+}}{{$}}
721ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_6:
7221d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
7238fe7e86bSDan Gohman; CHECK-NOT:   block
7248fe7e86bSDan Gohman; CHECK:       return{{$}}
725ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_7:
7261d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
7278fe7e86bSDan Gohman; CHECK-NOT:   block
7288fe7e86bSDan Gohman; CHECK:       return{{$}}
729ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_8:
7301d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
7318fe7e86bSDan Gohman; CHECK-NOT:   block
7328fe7e86bSDan Gohman; CHECK:       return{{$}}
7338fe7e86bSDan Gohmandefine void @test11() {
7348fe7e86bSDan Gohmanbb0:
7358fe7e86bSDan Gohman  store volatile i32 0, i32* null
7368fe7e86bSDan Gohman  br i1 undef, label %bb1, label %bb4
7378fe7e86bSDan Gohmanbb1:
7388fe7e86bSDan Gohman  store volatile i32 1, i32* null
7398fe7e86bSDan Gohman  br i1 undef, label %bb3, label %bb2
7408fe7e86bSDan Gohmanbb2:
7418fe7e86bSDan Gohman  store volatile i32 2, i32* null
7428fe7e86bSDan Gohman  br i1 undef, label %bb3, label %bb7
7438fe7e86bSDan Gohmanbb3:
7448fe7e86bSDan Gohman  store volatile i32 3, i32* null
7458fe7e86bSDan Gohman  ret void
7468fe7e86bSDan Gohmanbb4:
7478fe7e86bSDan Gohman  store volatile i32 4, i32* null
7488fe7e86bSDan Gohman  br i1 undef, label %bb8, label %bb5
7498fe7e86bSDan Gohmanbb5:
7508fe7e86bSDan Gohman  store volatile i32 5, i32* null
7518fe7e86bSDan Gohman  br i1 undef, label %bb6, label %bb7
7528fe7e86bSDan Gohmanbb6:
7538fe7e86bSDan Gohman  store volatile i32 6, i32* null
7548fe7e86bSDan Gohman  ret void
7558fe7e86bSDan Gohmanbb7:
7568fe7e86bSDan Gohman  store volatile i32 7, i32* null
7578fe7e86bSDan Gohman  ret void
7588fe7e86bSDan Gohmanbb8:
7598fe7e86bSDan Gohman  store volatile i32 8, i32* null
7608fe7e86bSDan Gohman  ret void
7618fe7e86bSDan Gohman}
7628fe7e86bSDan Gohman
7638fe7e86bSDan Gohman; CHECK-LABEL: test12:
764ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_1:
765*7f50c15bSThomas Lively; CHECK-NEXT:  block   {{$}}
7661d68e80fSDan Gohman; CHECK-NEXT:  loop    {{$}}
767b9f282d3SHeejin Ahn; CHECK-NEXT:  block   {{$}}
768b9f282d3SHeejin Ahn; CHECK-NEXT:  block   {{$}}
769*7f50c15bSThomas Lively; CHECK:       br_table  {{[^,]+}}, 1, 3, 3, 3, 1, 0{{$}}
770*7f50c15bSThomas Lively; CHECK-NEXT:  .LBB{{[0-9]+}}_2:
771*7f50c15bSThomas Lively; CHECK-NEXT:  end_block{{$}}
77206b49582SDan Gohman; CHECK:       br_if     0, {{[^,]+}}{{$}}
773b9f282d3SHeejin Ahn; CHECK:       br_if     2, {{[^,]+}}{{$}}
774ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
7751d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
776*7f50c15bSThomas Lively; CHECK:       br        0{{$}}
777b9f282d3SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_5:
778*7f50c15bSThomas Lively; CHECK-NEXT:  end_loop{{$}}
779b9f282d3SHeejin Ahn; CHECK-NEXT:  end_block{{$}}
780b9f282d3SHeejin Ahn; CHECK-NEXT:  return{{$}}
7818fe7e86bSDan Gohmandefine void @test12(i8* %arg) {
7828fe7e86bSDan Gohmanbb:
7838fe7e86bSDan Gohman  br label %bb1
7848fe7e86bSDan Gohman
7858fe7e86bSDan Gohmanbb1:
7868fe7e86bSDan Gohman  %tmp = phi i32 [ 0, %bb ], [ %tmp5, %bb4 ]
7878fe7e86bSDan Gohman  %tmp2 = getelementptr i8, i8* %arg, i32 %tmp
7888fe7e86bSDan Gohman  %tmp3 = load i8, i8* %tmp2
7898fe7e86bSDan Gohman  switch i8 %tmp3, label %bb7 [
7908fe7e86bSDan Gohman    i8 42, label %bb4
7918fe7e86bSDan Gohman    i8 76, label %bb4
7928fe7e86bSDan Gohman    i8 108, label %bb4
7938fe7e86bSDan Gohman    i8 104, label %bb4
7948fe7e86bSDan Gohman  ]
7958fe7e86bSDan Gohman
7968fe7e86bSDan Gohmanbb4:
7978fe7e86bSDan Gohman  %tmp5 = add i32 %tmp, 1
7988fe7e86bSDan Gohman  br label %bb1
7998fe7e86bSDan Gohman
8008fe7e86bSDan Gohmanbb7:
8018fe7e86bSDan Gohman  ret void
8028fe7e86bSDan Gohman}
803b3aa1ecaSDan Gohman
804b3aa1ecaSDan Gohman; A block can be "branched to" from another even if it is also reachable via
805b3aa1ecaSDan Gohman; fallthrough from the other. This would normally be optimized away, so use
806b3aa1ecaSDan Gohman; optnone to disable optimizations to test this case.
807b3aa1ecaSDan Gohman
808b3aa1ecaSDan Gohman; CHECK-LABEL: test13:
80949482f82SWouter van Oortmerssen; CHECK:       block   {{$}}
810442bfcecSDan Gohman; CHECK-NEXT:  block   {{$}}
811442bfcecSDan Gohman; CHECK:       br_if 0, $pop0{{$}}
8121d68e80fSDan Gohman; CHECK:       block   {{$}}
813442bfcecSDan Gohman; CHECK:       br_if 0, $pop3{{$}}
814ace7a086SHeejin Ahn; CHECK:       .LBB{{[0-9]+}}_3:
815442bfcecSDan Gohman; CHECK-NEXT:  end_block{{$}}
816442bfcecSDan Gohman; CHECK:       br_if 1, $pop{{[0-9]+}}{{$}}
817442bfcecSDan Gohman; CHECK-NEXT:  br 1{{$}}
818ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_4:
819442bfcecSDan Gohman; CHECK-NEXT:  end_block{{$}}
820b3aa1ecaSDan Gohman; CHECK-NEXT:  return{{$}}
821ace7a086SHeejin Ahn; CHECK-NEXT:  .LBB{{[0-9]+}}_5:
8221d68e80fSDan Gohman; CHECK-NEXT:  end_block{{$}}
823b3aa1ecaSDan Gohman; CHECK-NEXT:  unreachable{{$}}
824b3aa1ecaSDan Gohmandefine void @test13() noinline optnone {
825b3aa1ecaSDan Gohmanbb:
826b3aa1ecaSDan Gohman  br i1 undef, label %bb5, label %bb2
827b3aa1ecaSDan Gohmanbb1:
828b3aa1ecaSDan Gohman  unreachable
829b3aa1ecaSDan Gohmanbb2:
830b3aa1ecaSDan Gohman  br i1 undef, label %bb3, label %bb4
831b3aa1ecaSDan Gohmanbb3:
832b3aa1ecaSDan Gohman  br label %bb4
833b3aa1ecaSDan Gohmanbb4:
834b3aa1ecaSDan Gohman  %tmp = phi i1 [ false, %bb2 ], [ false, %bb3 ]
835b3aa1ecaSDan Gohman  br i1 %tmp, label %bb1, label %bb1
836b3aa1ecaSDan Gohmanbb5:
837b3aa1ecaSDan Gohman  ret void
838b3aa1ecaSDan Gohman}
8391d68e80fSDan Gohman
8401d68e80fSDan Gohman; Test a case with a single-block loop that has another loop
8411d68e80fSDan Gohman; as a successor. The end_loop for the first loop should go
8421d68e80fSDan Gohman; before the loop for the second.
8431d68e80fSDan Gohman
8441d68e80fSDan Gohman; CHECK-LABEL: test14:
845ace7a086SHeejin Ahn; CHECK:      .LBB{{[0-9]+}}_1:{{$}}
8461d68e80fSDan Gohman; CHECK-NEXT:     loop    {{$}}
847b6fd39a3SDan Gohman; CHECK-NEXT:     i32.const   $push0=, 0{{$}}
84806b49582SDan Gohman; CHECK-NEXT:     br_if       0, $pop0{{$}}
8491d68e80fSDan Gohman; CHECK-NEXT:     end_loop{{$}}
850ace7a086SHeejin Ahn; CHECK-NEXT: .LBB{{[0-9]+}}_3:{{$}}
8511d68e80fSDan Gohman; CHECK-NEXT:     loop    {{$}}
852b6fd39a3SDan Gohman; CHECK-NEXT:     i32.const   $push1=, 0{{$}}
85306b49582SDan Gohman; CHECK-NEXT:     br_if       0, $pop1{{$}}
8541d68e80fSDan Gohman; CHECK-NEXT:     end_loop{{$}}
8551d68e80fSDan Gohman; CHECK-NEXT:     return{{$}}
8561d68e80fSDan Gohmandefine void @test14() {
8571d68e80fSDan Gohmanbb:
8581d68e80fSDan Gohman  br label %bb1
8591d68e80fSDan Gohman
8601d68e80fSDan Gohmanbb1:
8611d68e80fSDan Gohman  %tmp = bitcast i1 undef to i1
8621d68e80fSDan Gohman  br i1 %tmp, label %bb3, label %bb1
8631d68e80fSDan Gohman
8641d68e80fSDan Gohmanbb3:
8651d68e80fSDan Gohman  br label %bb4
8661d68e80fSDan Gohman
8671d68e80fSDan Gohmanbb4:
8681d68e80fSDan Gohman  br i1 undef, label %bb7, label %bb48
8691d68e80fSDan Gohman
8701d68e80fSDan Gohmanbb7:
8711d68e80fSDan Gohman  br i1 undef, label %bb12, label %bb12
8721d68e80fSDan Gohman
8731d68e80fSDan Gohmanbb12:
8741d68e80fSDan Gohman  br i1 undef, label %bb17, label %bb17
8751d68e80fSDan Gohman
8761d68e80fSDan Gohmanbb17:
8771d68e80fSDan Gohman  br i1 undef, label %bb22, label %bb22
8781d68e80fSDan Gohman
8791d68e80fSDan Gohmanbb22:
8801d68e80fSDan Gohman  br i1 undef, label %bb27, label %bb27
8811d68e80fSDan Gohman
8821d68e80fSDan Gohmanbb27:
8831d68e80fSDan Gohman  br i1 undef, label %bb30, label %bb30
8841d68e80fSDan Gohman
8851d68e80fSDan Gohmanbb30:
8861d68e80fSDan Gohman  br i1 undef, label %bb35, label %bb35
8871d68e80fSDan Gohman
8881d68e80fSDan Gohmanbb35:
8891d68e80fSDan Gohman  br i1 undef, label %bb38, label %bb38
8901d68e80fSDan Gohman
8911d68e80fSDan Gohmanbb38:
8921d68e80fSDan Gohman  br i1 undef, label %bb48, label %bb48
8931d68e80fSDan Gohman
8941d68e80fSDan Gohmanbb48:
8951d68e80fSDan Gohman  %tmp49 = bitcast i1 undef to i1
8961d68e80fSDan Gohman  br i1 %tmp49, label %bb3, label %bb50
8971d68e80fSDan Gohman
8981d68e80fSDan Gohmanbb50:
8991d68e80fSDan Gohman  ret void
9001d68e80fSDan Gohman}
901a187ab2aSDan Gohman
902a187ab2aSDan Gohman; Test that a block boundary which ends one block, begins another block, and
903a187ab2aSDan Gohman; also begins a loop, has the markers placed in the correct order.
904a187ab2aSDan Gohman
905a187ab2aSDan Gohman; CHECK-LABEL: test15:
906a187ab2aSDan Gohman; CHECK:        block
907a187ab2aSDan Gohman; CHECK-NEXT:   block
908442bfcecSDan Gohman; CHECK:        br_if       0, $pop{{.*}}{{$}}
909ace7a086SHeejin Ahn; CHECK:        .LBB{{[0-9]+}}_2:
910442bfcecSDan Gohman; CHECK-NEXT:   block   {{$}}
9113a643e8dSDan Gohman; CHECK-NEXT:   block   {{$}}
912442bfcecSDan Gohman; CHECK-NEXT:   loop    {{$}}
913442bfcecSDan Gohman; CHECK:        br_if       1, $pop{{.*}}{{$}}
914d85ab7fcSDan Gohman; CHECK:        br_if       0, ${{.*}}{{$}}
915d85ab7fcSDan Gohman; CHECK-NEXT:   br          2{{$}}
916ace7a086SHeejin Ahn; CHECK-NEXT:   .LBB{{[0-9]+}}_4:
917442bfcecSDan Gohman; CHECK-NEXT:   end_loop{{$}}
918ace7a086SHeejin Ahn; CHECK:        .LBB{{[0-9]+}}_5:
919442bfcecSDan Gohman; CHECK-NEXT:   end_block{{$}}
920442bfcecSDan Gohman; CHECK:        br_if       1, $pop{{.*}}{{$}}
921442bfcecSDan Gohman; CHECK:        return{{$}}
922ace7a086SHeejin Ahn; CHECK:        .LBB{{[0-9]+}}_7:
923442bfcecSDan Gohman; CHECK-NEXT:   end_block{{$}}
924ace7a086SHeejin Ahn; CHECK:        .LBB{{[0-9]+}}_8:
925d85ab7fcSDan Gohman; CHECK-NEXT:   end_block{{$}}
926442bfcecSDan Gohman; CHECK-NEXT:   return{{$}}
927a187ab2aSDan Gohman%0 = type { i8, i32 }
928a187ab2aSDan Gohmandeclare void @test15_callee0()
929a187ab2aSDan Gohmandeclare void @test15_callee1()
930a187ab2aSDan Gohmandefine void @test15() {
931a187ab2aSDan Gohmanbb:
932a187ab2aSDan Gohman  %tmp1 = icmp eq i8 1, 0
933a187ab2aSDan Gohman  br i1 %tmp1, label %bb2, label %bb14
934a187ab2aSDan Gohman
935a187ab2aSDan Gohmanbb2:
936a187ab2aSDan Gohman  %tmp3 = phi %0** [ %tmp6, %bb5 ], [ null, %bb ]
937a187ab2aSDan Gohman  %tmp4 = icmp eq i32 0, 11
938a187ab2aSDan Gohman  br i1 %tmp4, label %bb5, label %bb8
939a187ab2aSDan Gohman
940a187ab2aSDan Gohmanbb5:
941a187ab2aSDan Gohman  %tmp = bitcast i8* null to %0**
942a187ab2aSDan Gohman  %tmp6 = getelementptr %0*, %0** %tmp3, i32 1
943a187ab2aSDan Gohman  %tmp7 = icmp eq %0** %tmp6, null
944a187ab2aSDan Gohman  br i1 %tmp7, label %bb10, label %bb2
945a187ab2aSDan Gohman
946a187ab2aSDan Gohmanbb8:
947a187ab2aSDan Gohman  %tmp9 = icmp eq %0** null, undef
948a187ab2aSDan Gohman  br label %bb10
949a187ab2aSDan Gohman
950a187ab2aSDan Gohmanbb10:
951a187ab2aSDan Gohman  %tmp11 = phi %0** [ null, %bb8 ], [ %tmp, %bb5 ]
952a187ab2aSDan Gohman  %tmp12 = icmp eq %0** null, %tmp11
953a187ab2aSDan Gohman  br i1 %tmp12, label %bb15, label %bb13
954a187ab2aSDan Gohman
955a187ab2aSDan Gohmanbb13:
956a187ab2aSDan Gohman  call void @test15_callee0()
957a187ab2aSDan Gohman  ret void
958a187ab2aSDan Gohman
959a187ab2aSDan Gohmanbb14:
960a187ab2aSDan Gohman  call void @test15_callee1()
961a187ab2aSDan Gohman  ret void
962a187ab2aSDan Gohman
963a187ab2aSDan Gohmanbb15:
964a187ab2aSDan Gohman  ret void
965a187ab2aSDan Gohman}
966