1 ; RUN: opt < %s -S -loop-unroll -verify-loop-info | FileCheck %s
3 ; Unit tests for LoopInfo::updateUnloop.
5 declare i1 @check() nounwind
7 ; Ensure that tail->inner is removed and rely on verify-loopinfo to
10 ; CHECK-LABEL: @skiplevelexit(
14 define void @skiplevelexit() nounwind {
22 %iv = phi i32 [ 0, %outer ], [ %inc, %tail ]
24 call zeroext i1 @check()
25 br i1 true, label %outer.backedge, label %tail
28 br i1 false, label %inner, label %exit
37 ; Remove the middle loop of a triply nested loop tree.
38 ; Ensure that only the middle loop is removed and rely on verify-loopinfo to
41 ; CHECK-LABEL: @unloopNested(
44 ; CHECK: br i1 %cmp3, label %if.then, label %if.end
46 ; CHECK: while.end14.i:
47 ; CHECK: br i1 %call15.i, label %if.end.i, label %exit
48 ; Middle loop control should no longer reach %while.cond.
49 ; Now it is the outer loop backedge.
51 ; CHECK: br label %while.cond.outer
52 define void @unloopNested() {
54 br label %while.cond.outer
60 %cmp = call zeroext i1 @check()
61 br i1 %cmp, label %while.body, label %while.end
64 %cmp3 = call zeroext i1 @check()
65 br i1 %cmp3, label %if.then, label %if.end
71 %cmp.i48 = call zeroext i1 @check()
72 br i1 %cmp.i48, label %if.then.i, label %if.else20.i
75 %cmp8.i = call zeroext i1 @check()
76 br i1 %cmp8.i, label %merge, label %if.else.i
82 %cmp25.i = call zeroext i1 @check()
83 br i1 %cmp25.i, label %merge, label %if.else28.i
89 br label %while.cond2.i
92 %cmp.i = call zeroext i1 @check()
93 br i1 %cmp.i, label %while.cond2.backedge.i, label %while.end.i
95 while.cond2.backedge.i:
96 br label %while.cond2.i
99 %cmp1114.i = call zeroext i1 @check()
100 br i1 %cmp1114.i, label %while.body12.lr.ph.i, label %while.end14.i
102 while.body12.lr.ph.i:
103 br label %while.end14.i
106 %call15.i = call zeroext i1 @check()
107 br i1 %call15.i, label %if.end.i, label %exit
110 br label %while.cond2.backedge.i
113 br i1 false, label %while.cond, label %if.else
116 br label %while.cond.outer
125 ; Remove the middle loop of a deeply nested loop tree.
126 ; Ensure that only the middle loop is removed and rely on verify-loopinfo to
129 ; This test must be disabled until trip count computation can be optimized...
130 ; rdar:14038809 [SCEV]: Optimize trip count computation for multi-exit loops.
131 ; CHECKFIXME-LABEL: @unloopDeepNested(
132 ; Inner-inner loop control.
133 ; CHECKFIXME: while.cond.us.i:
134 ; CHECKFIXME: br i1 %cmp.us.i, label %next_data.exit, label %while.body.us.i
135 ; CHECKFIXME: if.then.us.i:
136 ; CHECKFIXME: br label %while.cond.us.i
138 ; CHECKFIXME: if.else.i:
139 ; CHECKFIXME: br label %while.cond.outer.i
140 ; Middle loop control (removed).
141 ; CHECKFIXME: valid_data.exit:
143 ; CHECKFIXME: %cmp = call zeroext i1 @check()
144 ; Outer loop control.
145 ; CHECKFIXME: copy_data.exit:
146 ; CHECKFIXME: br i1 %cmp38, label %if.then39, label %while.cond.outer
147 ; Outer-outer loop tail.
148 ; CHECKFIXME: while.cond.outer.outer.backedge:
149 ; CHECKFIXME: br label %while.cond.outer.outer
150 define void @unloopDeepNested() nounwind {
151 for.cond8.preheader.i:
152 %cmp113.i = call zeroext i1 @check()
153 br i1 %cmp113.i, label %make_data.exit, label %for.body13.lr.ph.i
156 br label %make_data.exit
159 br label %while.cond.outer.outer
161 while.cond.outer.outer:
162 br label %while.cond.outer
168 br label %while.cond.outer.i
171 %tmp192.ph.i = call zeroext i1 @check()
172 br i1 %tmp192.ph.i, label %while.cond.outer.split.us.i, label %while.body.loopexit
174 while.cond.outer.split.us.i:
175 br label %while.cond.us.i
178 %cmp.us.i = call zeroext i1 @check()
179 br i1 %cmp.us.i, label %next_data.exit, label %while.body.us.i
182 %cmp7.us.i = call zeroext i1 @check()
183 br i1 %cmp7.us.i, label %if.then.us.i, label %if.else.i
186 br label %while.cond.us.i
189 br label %while.cond.outer.i
192 %tmp192.ph.i.lcssa28 = call zeroext i1 @check()
193 br i1 %tmp192.ph.i.lcssa28, label %while.end, label %while.body
199 br label %while.cond.i
202 %cmp.i = call zeroext i1 @check()
203 br i1 %cmp.i, label %valid_data.exit, label %while.body.i
206 %cmp7.i = call zeroext i1 @check()
207 br i1 %cmp7.i, label %valid_data.exit, label %if.end.i
210 br label %while.cond.i
213 br i1 true, label %if.then, label %while.cond
216 %cmp = call zeroext i1 @check()
217 br i1 %cmp, label %if.then12, label %if.end
223 %tobool3.i = call zeroext i1 @check()
224 br i1 %tobool3.i, label %copy_data.exit, label %while.body.lr.ph.i
227 br label %copy_data.exit
230 %cmp38 = call zeroext i1 @check()
231 br i1 %cmp38, label %if.then39, label %while.cond.outer
234 %cmp5.i = call zeroext i1 @check()
235 br i1 %cmp5.i, label %while.cond.outer.outer.backedge, label %for.cond8.preheader.i8.thread
237 for.cond8.preheader.i8.thread:
238 br label %while.cond.outer.outer.backedge
240 while.cond.outer.outer.backedge:
241 br label %while.cond.outer.outer
247 ; Remove a nested loop with irreducible control flow.
248 ; Ensure that only the middle loop is removed and rely on verify-loopinfo to
251 ; CHECK-LABEL: @unloopIrreducible(
254 ; CHECK: br label %for.cond103t
255 ; Nested loop (removed).
257 ; CHECK: br label %for.inc163
258 define void @unloopIrreducible() nounwind {
264 %cmp2113 = call zeroext i1 @check()
265 br i1 %cmp2113, label %for.body22.lr.ph, label %for.inc163
277 %cmp424 = call zeroext i1 @check()
278 br i1 %cmp424, label %for.body43.lr.ph, label %for.end93
284 %cmp96 = call zeroext i1 @check()
285 br i1 %cmp96, label %if.then97, label %for.cond103
288 br label %for.cond103t
291 br label %for.cond103
294 %cmp105 = call zeroext i1 @check()
295 br i1 %cmp105, label %for.body106, label %for.end120
298 %cmp108 = call zeroext i1 @check()
299 br i1 %cmp108, label %if.then109, label %for.inc117
305 br label %for.cond103t
311 br i1 false, label %for.body22, label %for.cond15.for.inc163_crit_edge
313 for.cond15.for.inc163_crit_edge:
317 %cmp12 = call zeroext i1 @check()
318 br i1 %cmp12, label %for.body, label %for.end166
325 ; Remove a loop whose exit branches into a sibling loop.
326 ; Ensure that only the loop is removed and rely on verify-loopinfo to
329 ; CHECK-LABEL: @unloopCriticalEdge(
330 ; CHECK: while.cond.outer.i.loopexit.split:
331 ; CHECK: br label %while.body
333 ; CHECK: br label %for.end78
334 define void @unloopCriticalEdge() nounwind {
339 br i1 undef, label %for.body35, label %for.end94
342 br label %while.cond.i.preheader
344 while.cond.i.preheader:
345 br i1 undef, label %while.cond.i.preheader.split, label %while.cond.outer.i.loopexit.split
347 while.cond.i.preheader.split:
348 br label %while.cond.i
351 br i1 true, label %while.cond.i, label %while.cond.outer.i.loopexit
353 while.cond.outer.i.loopexit:
354 br label %while.cond.outer.i.loopexit.split
356 while.cond.outer.i.loopexit.split:
357 br i1 false, label %while.cond.i.preheader, label %Func2.exit
363 br i1 false, label %while.body, label %while.end
369 br i1 undef, label %Proc2.exit, label %for.cond.i.preheader
371 for.cond.i.preheader:
384 ; Test UnloopUpdater::removeBlocksFromAncestors.
386 ; Check that the loop backedge is removed from the middle loop 1699,
387 ; but not the inner loop 1676.
388 ; CHECK: while.body1694:
389 ; CHECK: br label %while.cond1676
390 ; CHECK: while.end1699:
391 ; CHECK: br label %sw.default1711
392 define void @removeSubloopBlocks() nounwind {
394 br label %tryagain.outer
396 tryagain.outer: ; preds = %sw.bb304, %entry
399 tryagain: ; preds = %while.end1699, %tryagain.outer
400 br i1 undef, label %sw.bb1669, label %sw.bb304
402 sw.bb304: ; preds = %tryagain
403 br i1 undef, label %return, label %tryagain.outer
405 sw.bb1669: ; preds = %tryagain
406 br i1 undef, label %sw.default1711, label %while.cond1676
408 while.cond1676: ; preds = %while.body1694, %sw.bb1669
409 br i1 undef, label %while.end1699, label %while.body1694
411 while.body1694: ; preds = %while.cond1676
412 br label %while.cond1676
414 while.end1699: ; preds = %while.cond1676
415 br i1 false, label %tryagain, label %sw.default1711
417 sw.default1711: ; preds = %while.end1699, %sw.bb1669, %tryagain
420 defchar: ; preds = %sw.default1711, %sw.bb376
421 br i1 undef, label %if.end2413, label %if.then2368
423 if.then2368: ; preds = %defchar
426 if.end2413: ; preds = %defchar
429 return: ; preds = %sw.bb304
433 ; PR11335: the most deeply nested block should be removed from the outer loop.
434 ; CHECK-LABEL: @removeSubloopBlocks2(
438 define void @removeSubloopBlocks2() nounwind {
440 %tobool.i = icmp ne i32 undef, 0
443 lbl_616.loopexit: ; preds = %for.cond
446 lbl_616: ; preds = %lbl_616.loopexit, %entry
449 for.cond: ; preds = %for.cond3, %lbl_616
450 br i1 false, label %for.cond1.preheader, label %lbl_616.loopexit
452 for.cond1.preheader: ; preds = %for.cond
455 for.cond1.loopexit: ; preds = %for.cond.i
458 for.cond1: ; preds = %for.cond1.loopexit, %for.cond1.preheader
459 br i1 false, label %for.body2, label %for.cond3
461 for.body2: ; preds = %for.cond1
464 for.cond.i: ; preds = %for.cond.i, %for.body2
465 br i1 %tobool.i, label %for.cond.i, label %for.cond1.loopexit
467 for.cond3: ; preds = %for.cond1
468 br i1 false, label %for.cond, label %if.end
470 if.end: ; preds = %for.cond3