1 ; RUN: llc < %s -O0 -asm-verbose=false -verify-machineinstrs -disable-block-placement -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s
3 ; Test irreducible CFG handling.
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
8 ; A simple loop with two entries.
12 ; CHECK: i32.const $[[REG:[^,]+]]=
13 ; CHECK: br_table $[[REG]],
14 define void @test0(double* %arg, i32 %arg1, i32 %arg2, i32 %arg3) {
16 %tmp = icmp eq i32 %arg2, 0
17 br i1 %tmp, label %bb6, label %bb3
20 %tmp4 = getelementptr double, double* %arg, i32 %arg3
21 %tmp5 = load double, double* %tmp4, align 4
24 bb6: ; preds = %bb13, %bb
25 %tmp7 = phi i32 [ %tmp18, %bb13 ], [ 0, %bb ]
26 %tmp8 = icmp slt i32 %tmp7, %arg1
27 br i1 %tmp8, label %bb9, label %bb19
30 %tmp10 = getelementptr double, double* %arg, i32 %tmp7
31 %tmp11 = load double, double* %tmp10, align 4
32 %tmp12 = fmul double %tmp11, 2.300000e+00
33 store double %tmp12, double* %tmp10, align 4
36 bb13: ; preds = %bb9, %bb3
37 %tmp14 = phi double [ %tmp5, %bb3 ], [ %tmp12, %bb9 ]
38 %tmp15 = phi i32 [ undef, %bb3 ], [ %tmp7, %bb9 ]
39 %tmp16 = getelementptr double, double* %arg, i32 %tmp15
40 %tmp17 = fadd double %tmp14, 1.300000e+00
41 store double %tmp17, double* %tmp16, align 4
42 %tmp18 = add nsw i32 %tmp15, 1
49 ; A simple loop with two entries and an inner natural loop.
53 ; CHECK: i32.const $[[REG:[^,]+]]=
54 ; CHECK: br_table $[[REG]],
55 define void @test1(double* %arg, i32 %arg1, i32 %arg2, i32 %arg3) {
57 %tmp = icmp eq i32 %arg2, 0
58 br i1 %tmp, label %bb6, label %bb3
61 %tmp4 = getelementptr double, double* %arg, i32 %arg3
62 %tmp5 = load double, double* %tmp4, align 4
65 bb6: ; preds = %bb13, %bb
66 %tmp7 = phi i32 [ %tmp18, %bb13 ], [ 0, %bb ]
67 %tmp8 = icmp slt i32 %tmp7, %arg1
68 br i1 %tmp8, label %bb9, label %bb19
71 %tmp10 = getelementptr double, double* %arg, i32 %tmp7
72 %tmp11 = load double, double* %tmp10, align 4
73 %tmp12 = fmul double %tmp11, 2.300000e+00
74 store double %tmp12, double* %tmp10, align 4
77 bb10: ; preds = %bb10, %bb9
78 %p = phi i32 [ 0, %bb9 ], [ %pn, %bb10 ]
80 %c = icmp slt i32 %pn, 256
81 br i1 %c, label %bb10, label %bb13
83 bb13: ; preds = %bb10, %bb3
84 %tmp14 = phi double [ %tmp5, %bb3 ], [ %tmp12, %bb10 ]
85 %tmp15 = phi i32 [ undef, %bb3 ], [ %tmp7, %bb10 ]
86 %tmp16 = getelementptr double, double* %arg, i32 %tmp15
87 %tmp17 = fadd double %tmp14, 1.300000e+00
88 store double %tmp17, double* %tmp16, align 4
89 %tmp18 = add nsw i32 %tmp15, 1
96 ; A simple loop 2 blocks that are both entries: A1 and A2.
97 ; Even though A1 and A2 both have 3 predecessors (A0, A1, and A2), not 6 but
98 ; only 4 new routing blocks to the dispatch block should be generated.
100 ; CHECK-LABEL: test2:
102 ; CHECK: i32.const $[[REG:[^,]+]]=
103 ; CHECK: i32.const $[[REG]]=
104 ; CHECK: br_table $[[REG]],
105 ; CHECK: i32.const $[[REG]]=
106 ; CHECK: i32.const $[[REG]]=
107 ; CHECK-NOT: i32.const $[[REG]]=
108 define i32 @test2(i32) {
113 %a0a = tail call i32 @test2(i32 1)
114 %a0b = icmp eq i32 %a0a, 0
115 br i1 %a0b, label %A1, label %A2
117 A1: ; preds = %A2, %A1, %A0
118 %a1a = tail call i32 @test2(i32 2)
119 %a1b = icmp eq i32 %a1a, 0
120 br i1 %a1b, label %A1, label %A2
122 A2: ; preds = %A2, %A1, %A0
123 %a2a = tail call i32 @test2(i32 3)
124 %a2b = icmp eq i32 %a2a, 0
125 br i1 %a2b, label %A1, label %A2
128 ; An interesting loop with inner loop and if-else structure too.
130 ; CHECK-LABEL: test3:
132 define void @test3(i32 %ws) {
134 %ws.addr = alloca i32, align 4
135 store volatile i32 %ws, i32* %ws.addr, align 4
136 %0 = load volatile i32, i32* %ws.addr, align 4
137 %tobool = icmp ne i32 %0, 0
138 br i1 %tobool, label %if.then, label %if.end
140 if.then: ; preds = %entry
143 if.end: ; preds = %entry
144 %1 = load volatile i32, i32* %ws.addr, align 4
145 %tobool1 = icmp ne i32 %1, 0
146 br i1 %tobool1, label %if.end9, label %if.then2
148 if.then2: ; preds = %if.end
151 for.cond: ; preds = %wynn, %if.then7, %if.then2
152 %2 = load volatile i32, i32* %ws.addr, align 4
153 %tobool3 = icmp ne i32 %2, 0
154 br i1 %tobool3, label %if.then4, label %if.end5
156 if.then4: ; preds = %for.cond
159 if.end5: ; preds = %if.then4, %for.cond
160 %3 = load volatile i32, i32* %ws.addr, align 4
161 %tobool6 = icmp ne i32 %3, 0
162 br i1 %tobool6, label %if.then7, label %if.end8
164 if.then7: ; preds = %if.end5
167 if.end8: ; preds = %if.end5
170 wynn: ; preds = %if.end8, %if.then
173 if.end9: ; preds = %if.end
177 ; Multi-level irreducibility, after reducing in the main scope we must then
178 ; reduce in the inner loop that we just created.
181 define void @pi_next() {
183 br i1 undef, label %sw.bb5, label %return
185 sw.bb5: ; preds = %entry
186 br i1 undef, label %if.then.i49, label %if.else.i52
188 if.then.i49: ; preds = %sw.bb5
189 br label %for.inc197.i
191 if.else.i52: ; preds = %sw.bb5
192 br label %for.cond57.i
194 for.cond57.i: ; preds = %for.inc205.i, %if.else.i52
195 store i32 0, i32* undef, align 4
196 br label %for.cond65.i
198 for.cond65.i: ; preds = %for.inc201.i, %for.cond57.i
199 br i1 undef, label %for.body70.i, label %for.inc205.i
201 for.body70.i: ; preds = %for.cond65.i
202 br label %for.cond76.i
204 for.cond76.i: ; preds = %for.inc197.i, %for.body70.i
205 %0 = phi i32 [ %inc199.i, %for.inc197.i ], [ 0, %for.body70.i ]
206 %cmp81.i = icmp slt i32 %0, 0
207 br i1 %cmp81.i, label %for.body82.i, label %for.inc201.i
209 for.body82.i: ; preds = %for.cond76.i
210 br label %for.inc197.i
212 for.inc197.i: ; preds = %for.body82.i, %if.then.i49
213 %inc199.i = add nsw i32 undef, 1
214 br label %for.cond76.i
216 for.inc201.i: ; preds = %for.cond76.i
217 br label %for.cond65.i
219 for.inc205.i: ; preds = %for.cond65.i
220 br label %for.cond57.i
222 return: ; preds = %entry
226 ; A more complx case of irreducible control flow, two interacting loops.
227 ; CHECK: ps_hints_apply
229 define void @ps_hints_apply() {
233 psh: ; preds = %entry
234 br i1 undef, label %for.cond, label %for.body
236 for.body: ; preds = %psh
239 do.body: ; preds = %do.cond, %for.body
240 %cmp118 = icmp eq i32* undef, undef
241 br i1 %cmp118, label %Skip, label %do.cond
243 do.cond: ; preds = %do.body
246 for.cond: ; preds = %Skip, %psh
249 for.body39: ; preds = %for.cond
250 br i1 undef, label %Skip, label %do.body45
252 do.body45: ; preds = %for.body39
255 Skip: ; preds = %for.body39, %do.body
259 ; A simple sequence of loops with blocks in between, that should not be
260 ; misinterpreted as irreducible control flow.
261 ; CHECK: fannkuch_worker
262 ; CHECK-NOT: br_table
263 define i32 @fannkuch_worker(i8* %_arg) {
267 do.body: ; preds = %do.cond, %for.cond
270 for.cond1: ; preds = %for.cond1, %do.body
271 br i1 true, label %for.cond1, label %for.end
273 for.end: ; preds = %for.cond1
276 do.cond: ; preds = %for.end
277 br i1 true, label %do.body, label %do.end
279 do.end: ; preds = %do.cond
282 for.cond2: ; preds = %for.end6, %do.end
285 for.cond3: ; preds = %for.cond3, %for.cond2
286 br i1 true, label %for.cond3, label %for.end6
288 for.end6: ; preds = %for.cond3
291 return: ; No predecessors!
295 ; Test an interesting pattern of nested irreducibility.
299 define void @func_2() {
301 br i1 undef, label %lbl_937, label %if.else787
303 lbl_937: ; preds = %for.body978, %entry
306 if.else787: ; preds = %entry
309 if.end965: ; preds = %if.else787, %lbl_937
310 br label %for.cond967
312 for.cond967: ; preds = %for.end1035, %if.end965
313 br label %for.cond975
315 for.cond975: ; preds = %if.end984, %for.cond967
316 br i1 undef, label %for.body978, label %for.end1035
318 for.body978: ; preds = %for.cond975
319 br i1 undef, label %lbl_937, label %if.end984
321 if.end984: ; preds = %for.body978
322 br label %for.cond975
324 for.end1035: ; preds = %for.cond975
325 br label %for.cond967