1 ; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>),verify<loops>' -S < %s | FileCheck %s
2 ; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),verify<loops>' -S < %s | FileCheck %s
3 ; RUN: opt -passes='simple-loop-unswitch<nontrivial>' -verify-memoryssa -S < %s | FileCheck %s
10 declare void @sink1(i32)
11 declare void @sink2(i32)
14 declare i32 @cond.i32()
16 ; Negative test: we cannot unswitch convergent calls.
17 define void @test_no_unswitch_convergent(ptr %ptr, i1 %cond) {
18 ; CHECK-LABEL: @test_no_unswitch_convergent(
22 ; CHECK-NEXT: br label %loop_begin
24 ; We shouldn't have unswitched into any other block either.
25 ; CHECK-NOT: br i1 %cond
28 br i1 %cond, label %loop_a, label %loop_b
30 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
33 call i32 @a() convergent
41 %v = load i1, ptr %ptr
42 br i1 %v, label %loop_begin, label %loop_exit
48 ; Negative test: we cannot unswitch noduplicate calls.
49 define void @test_no_unswitch_noduplicate(ptr %ptr, i1 %cond) {
50 ; CHECK-LABEL: @test_no_unswitch_noduplicate(
54 ; CHECK-NEXT: br label %loop_begin
56 ; We shouldn't have unswitched into any other block either.
57 ; CHECK-NOT: br i1 %cond
60 br i1 %cond, label %loop_a, label %loop_b
62 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
65 call i32 @a() noduplicate
73 %v = load i1, ptr %ptr
74 br i1 %v, label %loop_begin, label %loop_exit
80 declare i32 @__CxxFrameHandler3(...)
82 ; Negative test: we cannot unswitch when tokens are used across blocks as we
83 ; might introduce PHIs.
84 define void @test_no_unswitch_cross_block_token(ptr %ptr, i1 %cond) nounwind personality ptr @__CxxFrameHandler3 {
85 ; CHECK-LABEL: @test_no_unswitch_cross_block_token(
89 ; CHECK-NEXT: br label %loop_begin
91 ; We shouldn't have unswitched into any other block either.
92 ; CHECK-NOT: br i1 %cond
95 br i1 %cond, label %loop_a, label %loop_b
97 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
109 to label %loop_latch unwind label %loop_catch
115 %catch = catchswitch within none [label %loop_catch_latch, label %loop_exit] unwind to caller
118 %catchpad_latch = catchpad within %catch []
119 catchret from %catchpad_latch to label %loop_begin
122 %catchpad_exit = catchpad within %catch []
123 catchret from %catchpad_exit to label %exit
130 ; Non-trivial loop unswitching where there are two distinct trivial conditions
131 ; to unswitch within the loop.
132 define i32 @test1(ptr %ptr, i1 %cond1, i1 %cond2) {
133 ; CHECK-LABEL: @test1(
137 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
140 br i1 %cond1, label %loop_a, label %loop_b
145 ; The 'loop_a' unswitched loop.
147 ; CHECK: entry.split.us:
148 ; CHECK-NEXT: br label %loop_begin.us
150 ; CHECK: loop_begin.us:
151 ; CHECK-NEXT: br label %loop_a.us
154 ; CHECK-NEXT: call i32 @a()
155 ; CHECK-NEXT: br label %latch.us
158 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
159 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
161 ; CHECK: loop_exit.split.us:
162 ; CHECK-NEXT: br label %loop_exit
165 br i1 %cond2, label %loop_b_a, label %loop_b_b
166 ; The second unswitched condition.
168 ; CHECK: entry.split:
169 ; CHECK-NEXT: br i1 %cond2, label %entry.split.split.us, label %entry.split.split
174 ; The 'loop_b_a' unswitched loop.
176 ; CHECK: entry.split.split.us:
177 ; CHECK-NEXT: br label %loop_begin.us1
179 ; CHECK: loop_begin.us1:
180 ; CHECK-NEXT: br label %loop_b.us
183 ; CHECK-NEXT: br label %loop_b_a.us
185 ; CHECK: loop_b_a.us:
186 ; CHECK-NEXT: call i32 @b()
187 ; CHECK-NEXT: br label %latch.us2
190 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
191 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us1, label %loop_exit.split.split.us
193 ; CHECK: loop_exit.split.split.us:
194 ; CHECK-NEXT: br label %loop_exit.split
199 ; The 'loop_b_b' unswitched loop.
201 ; CHECK: entry.split.split:
202 ; CHECK-NEXT: br label %loop_begin
205 ; CHECK-NEXT: br label %loop_b
208 ; CHECK-NEXT: br label %loop_b_b
211 ; CHECK-NEXT: call i32 @c()
212 ; CHECK-NEXT: br label %latch
215 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
216 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split.split
218 ; CHECK: loop_exit.split.split:
219 ; CHECK-NEXT: br label %loop_exit.split
222 %v = load i1, ptr %ptr
223 br i1 %v, label %loop_begin, label %loop_exit
227 ; CHECK: loop_exit.split:
228 ; CHECK-NEXT: br label %loop_exit
234 define i32 @test2(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr, ptr %c.ptr) {
235 ; CHECK-LABEL: @test2(
239 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
242 %v = load i1, ptr %ptr
243 br i1 %cond1, label %loop_a, label %loop_b
246 %a = load i32, ptr %a.ptr
247 %ac = load i32, ptr %c.ptr
248 br i1 %v, label %loop_begin, label %loop_exit
249 ; The 'loop_a' unswitched loop.
251 ; CHECK: entry.split.us:
252 ; CHECK-NEXT: br label %loop_begin.us
254 ; CHECK: loop_begin.us:
255 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
256 ; CHECK-NEXT: br label %loop_a.us
259 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
260 ; CHECK-NEXT: %[[AC:.*]] = load i32, ptr %c.ptr
261 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge.us, label %loop_exit.split.us
263 ; CHECK: loop_exit.split.us:
264 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a.us ]
265 ; CHECK-NEXT: %[[AC_LCSSA:.*]] = phi i32 [ %[[AC]], %loop_a.us ]
266 ; CHECK-NEXT: br label %loop_exit
269 %b = load i32, ptr %b.ptr
270 %bc = load i32, ptr %c.ptr
271 br i1 %v, label %loop_begin, label %loop_exit
272 ; The 'loop_b' unswitched loop.
274 ; CHECK: entry.split:
275 ; CHECK-NEXT: br label %loop_begin
278 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
279 ; CHECK-NEXT: br label %loop_b
282 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
283 ; CHECK-NEXT: %[[BC:.*]] = load i32, ptr %c.ptr
284 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge, label %loop_exit.split
286 ; CHECK: loop_exit.split:
287 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
288 ; CHECK-NEXT: %[[BC_LCSSA:.*]] = phi i32 [ %[[BC]], %loop_b ]
289 ; CHECK-NEXT: br label %loop_exit
292 %ab.phi = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
293 %c.phi = phi i32 [ %ac, %loop_a ], [ %bc, %loop_b ]
294 %result = add i32 %ab.phi, %c.phi
297 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
298 ; CHECK-NEXT: %[[C_PHI:.*]] = phi i32 [ %[[BC_LCSSA]], %loop_exit.split ], [ %[[AC_LCSSA]], %loop_exit.split.us ]
299 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[AB_PHI]], %[[C_PHI]]
300 ; CHECK-NEXT: ret i32 %[[RESULT]]
303 ; Test a non-trivial unswitch of an exiting edge to an exit block with other
304 ; in-loop predecessors.
305 define i32 @test3a(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
306 ; CHECK-LABEL: @test3a(
310 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
313 %v = load i1, ptr %ptr
314 %a = load i32, ptr %a.ptr
315 br i1 %cond1, label %loop_exit, label %loop_b
316 ; The 'loop_exit' clone.
318 ; CHECK: entry.split.us:
319 ; CHECK-NEXT: br label %loop_begin.us
321 ; CHECK: loop_begin.us:
322 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
323 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
324 ; CHECK-NEXT: br label %loop_exit.split.us
326 ; CHECK: loop_exit.split.us:
327 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
328 ; CHECK-NEXT: br label %loop_exit
331 %b = load i32, ptr %b.ptr
332 br i1 %v, label %loop_begin, label %loop_exit
333 ; The 'loop_b' unswitched loop.
335 ; CHECK: entry.split:
336 ; CHECK-NEXT: br label %loop_begin
339 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
340 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
341 ; CHECK-NEXT: br label %loop_b
344 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
345 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
347 ; CHECK: loop_exit.split:
348 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
349 ; CHECK-NEXT: br label %loop_exit
352 %ab.phi = phi i32 [ %a, %loop_begin ], [ %b, %loop_b ]
355 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
356 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
359 ; Test a non-trivial unswitch of an exiting edge to an exit block with other
360 ; in-loop predecessors. This is the same as @test3a but with the reversed order
361 ; of successors so that the exiting edge is *not* the cloned edge.
362 define i32 @test3b(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
363 ; CHECK-LABEL: @test3b(
367 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
370 %v = load i1, ptr %ptr
371 %a = load i32, ptr %a.ptr
372 br i1 %cond1, label %loop_b, label %loop_exit
373 ; The 'loop_b' unswitched loop.
375 ; CHECK: entry.split.us:
376 ; CHECK-NEXT: br label %loop_begin.us
378 ; CHECK: loop_begin.us:
379 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
380 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
381 ; CHECK-NEXT: br label %loop_b.us
384 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
385 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
387 ; CHECK: loop_exit.split.us:
388 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b.us ]
389 ; CHECK-NEXT: br label %loop_exit
392 %b = load i32, ptr %b.ptr
393 br i1 %v, label %loop_begin, label %loop_exit
394 ; The original loop, now non-looping due to unswitching..
396 ; CHECK: entry.split:
397 ; CHECK-NEXT: br label %loop_begin
400 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
401 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
402 ; CHECK-NEXT: br label %loop_exit.split
404 ; CHECK: loop_exit.split:
405 ; CHECK-NEXT: br label %loop_exit
408 %ab.phi = phi i32 [ %b, %loop_b ], [ %a, %loop_begin ]
411 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A]], %loop_exit.split ], [ %[[B_LCSSA]], %loop_exit.split.us ]
412 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
415 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
416 ; in-loop predecessors.
417 define void @test4a(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
418 ; CHECK-LABEL: @test4a(
422 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
425 %v = load i1, ptr %ptr
426 %a = load i32, ptr %a.ptr
427 br i1 %cond1, label %loop_exit1, label %loop_b
428 ; The 'loop_exit' clone.
430 ; CHECK: entry.split.us:
431 ; CHECK-NEXT: br label %loop_begin.us
433 ; CHECK: loop_begin.us:
434 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
435 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
436 ; CHECK-NEXT: br label %loop_exit1.split.us
438 ; CHECK: loop_exit1.split.us:
439 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
440 ; CHECK-NEXT: br label %loop_exit1
443 %b = load i32, ptr %b.ptr
444 br i1 %v, label %loop_begin, label %loop_exit2
445 ; The 'loop_b' unswitched loop.
447 ; CHECK: entry.split:
448 ; CHECK-NEXT: br label %loop_begin
451 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
452 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
453 ; CHECK-NEXT: br label %loop_b
456 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
457 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit2
460 %a.phi = phi i32 [ %a, %loop_begin ]
461 call void @sink1(i32 %a.phi)
464 ; CHECK-NEXT: call void @sink1(i32 %[[A_LCSSA]])
465 ; CHECK-NEXT: ret void
468 %b.phi = phi i32 [ %b, %loop_b ]
469 call void @sink2(i32 %b.phi)
472 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
473 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
474 ; CHECK-NEXT: ret void
477 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
478 ; in-loop predecessors. This is the same as @test4a but with the edges reversed
479 ; so that the exiting edge is *not* the cloned edge.
480 define void @test4b(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
481 ; CHECK-LABEL: @test4b(
485 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
488 %v = load i1, ptr %ptr
489 %a = load i32, ptr %a.ptr
490 br i1 %cond1, label %loop_b, label %loop_exit1
491 ; The 'loop_b' clone.
493 ; CHECK: entry.split.us:
494 ; CHECK-NEXT: br label %loop_begin.us
496 ; CHECK: loop_begin.us:
497 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
498 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
499 ; CHECK-NEXT: br label %loop_b.us
502 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
503 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit2.split.us
505 ; CHECK: loop_exit2.split.us:
506 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b.us ]
507 ; CHECK-NEXT: br label %loop_exit2
510 %b = load i32, ptr %b.ptr
511 br i1 %v, label %loop_begin, label %loop_exit2
512 ; The 'loop_exit' unswitched path.
514 ; CHECK: entry.split:
515 ; CHECK-NEXT: br label %loop_begin
518 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
519 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
520 ; CHECK-NEXT: br label %loop_exit1
523 %a.phi = phi i32 [ %a, %loop_begin ]
524 call void @sink1(i32 %a.phi)
527 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ]
528 ; CHECK-NEXT: call void @sink1(i32 %[[A_PHI]])
529 ; CHECK-NEXT: ret void
532 %b.phi = phi i32 [ %b, %loop_b ]
533 call void @sink2(i32 %b.phi)
536 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
537 ; CHECK-NEXT: ret void
540 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
541 ; in-loop predecessors. This is the same as @test4a but with a common merge
542 ; block after the independent loop exits. This requires a different structural
543 ; update to the dominator tree.
544 define void @test4c(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
545 ; CHECK-LABEL: @test4c(
549 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
552 %v = load i1, ptr %ptr
553 %a = load i32, ptr %a.ptr
554 br i1 %cond1, label %loop_exit1, label %loop_b
555 ; The 'loop_exit' clone.
557 ; CHECK: entry.split.us:
558 ; CHECK-NEXT: br label %loop_begin.us
560 ; CHECK: loop_begin.us:
561 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
562 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
563 ; CHECK-NEXT: br label %loop_exit1.split.us
565 ; CHECK: loop_exit1.split.us:
566 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
567 ; CHECK-NEXT: br label %loop_exit1
570 %b = load i32, ptr %b.ptr
571 br i1 %v, label %loop_begin, label %loop_exit2
572 ; The 'loop_b' unswitched loop.
574 ; CHECK: entry.split:
575 ; CHECK-NEXT: br label %loop_begin
578 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
579 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
580 ; CHECK-NEXT: br label %loop_b
583 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
584 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit2
587 %a.phi = phi i32 [ %a, %loop_begin ]
588 call void @sink1(i32 %a.phi)
591 ; CHECK-NEXT: call void @sink1(i32 %[[A_LCSSA]])
592 ; CHECK-NEXT: br label %exit
595 %b.phi = phi i32 [ %b, %loop_b ]
596 call void @sink2(i32 %b.phi)
599 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
600 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
601 ; CHECK-NEXT: br label %exit
606 ; CHECK-NEXT: ret void
609 ; Test that we can unswitch a condition out of multiple layers of a loop nest.
610 define i32 @test5(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
611 ; CHECK-LABEL: @test5(
615 ; CHECK-NEXT: br i1 %cond1, label %loop_begin.split.us, label %entry.split
617 ; CHECK: entry.split:
618 ; CHECK-NEXT: br label %loop_begin
621 ; CHECK-NEXT: br label %loop_begin.split
624 br label %inner_loop_begin
627 %v = load i1, ptr %ptr
628 %a = load i32, ptr %a.ptr
629 br i1 %cond1, label %loop_exit, label %inner_loop_b
630 ; The 'loop_exit' clone.
632 ; CHECK: loop_begin.split.us:
633 ; CHECK-NEXT: br label %inner_loop_begin.us
635 ; CHECK: inner_loop_begin.us:
636 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
637 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
638 ; CHECK-NEXT: br label %loop_exit.loopexit.split.us
640 ; CHECK: loop_exit.loopexit.split.us:
641 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
642 ; CHECK-NEXT: br label %loop_exit
645 %b = load i32, ptr %b.ptr
646 br i1 %v, label %inner_loop_begin, label %loop_latch
647 ; The 'inner_loop_b' unswitched loop.
649 ; CHECK: loop_begin.split:
650 ; CHECK-NEXT: br label %inner_loop_begin
652 ; CHECK: inner_loop_begin:
653 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
654 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
655 ; CHECK-NEXT: br label %inner_loop_b
657 ; CHECK: inner_loop_b:
658 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
659 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_begin, label %loop_latch
662 %b.phi = phi i32 [ %b, %inner_loop_b ]
663 %v2 = load i1, ptr %ptr
664 br i1 %v2, label %loop_begin, label %loop_exit
666 ; CHECK-NEXT: %[[B_INNER_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_b ]
667 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr
668 ; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.loopexit1
671 %ab.phi = phi i32 [ %a, %inner_loop_begin ], [ %b.phi, %loop_latch ]
673 ; CHECK: loop_exit.loopexit:
674 ; CHECK-NEXT: br label %loop_exit
676 ; CHECK: loop_exit.loopexit1:
677 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_INNER_LCSSA]], %loop_latch ]
678 ; CHECK-NEXT: br label %loop_exit
681 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.loopexit ], [ %[[B_LCSSA]], %loop_exit.loopexit1 ]
682 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
685 ; Test that we can unswitch a condition where we end up only cloning some of
686 ; the nested loops and needing to delete some of the nested loops.
687 define i32 @test6(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
688 ; CHECK-LABEL: @test6(
692 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
695 %v = load i1, ptr %ptr
696 br i1 %cond1, label %loop_a, label %loop_b
699 br label %loop_a_inner
702 %va = load i1, ptr %ptr
703 %a = load i32, ptr %a.ptr
704 br i1 %va, label %loop_a_inner, label %loop_a_inner_exit
707 %a.lcssa = phi i32 [ %a, %loop_a_inner ]
709 ; The 'loop_a' cloned loop.
711 ; CHECK: entry.split.us:
712 ; CHECK-NEXT: br label %loop_begin.us
714 ; CHECK: loop_begin.us:
715 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
716 ; CHECK-NEXT: br label %loop_a.us
719 ; CHECK-NEXT: br label %loop_a_inner.us
721 ; CHECK: loop_a_inner.us
722 ; CHECK-NEXT: %[[VA:.*]] = load i1, ptr %ptr
723 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
724 ; CHECK-NEXT: br i1 %[[VA]], label %loop_a_inner.us, label %loop_a_inner_exit.us
726 ; CHECK: loop_a_inner_exit.us:
727 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a_inner.us ]
728 ; CHECK-NEXT: br label %latch.us
731 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %loop_a_inner_exit.us ]
732 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
734 ; CHECK: loop_exit.split.us:
735 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_PHI]], %latch.us ]
736 ; CHECK-NEXT: br label %loop_exit
739 br label %loop_b_inner
742 %vb = load i1, ptr %ptr
743 %b = load i32, ptr %b.ptr
744 br i1 %vb, label %loop_b_inner, label %loop_b_inner_exit
747 %b.lcssa = phi i32 [ %b, %loop_b_inner ]
751 %ab.phi = phi i32 [ %a.lcssa, %loop_a_inner_exit ], [ %b.lcssa, %loop_b_inner_exit ]
752 br i1 %v, label %loop_begin, label %loop_exit
753 ; The 'loop_b' unswitched loop.
755 ; CHECK: entry.split:
756 ; CHECK-NEXT: br label %loop_begin
759 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
760 ; CHECK-NEXT: br label %loop_b
763 ; CHECK-NEXT: br label %loop_b_inner
765 ; CHECK: loop_b_inner
766 ; CHECK-NEXT: %[[VB:.*]] = load i1, ptr %ptr
767 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
768 ; CHECK-NEXT: br i1 %[[VB]], label %loop_b_inner, label %loop_b_inner_exit
770 ; CHECK: loop_b_inner_exit:
771 ; CHECK-NEXT: %[[B_INNER_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b_inner ]
772 ; CHECK-NEXT: br label %latch
775 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
777 ; CHECK: loop_exit.split:
778 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_INNER_LCSSA]], %latch ]
779 ; CHECK-NEXT: br label %loop_exit
782 %ab.lcssa = phi i32 [ %ab.phi, %latch ]
785 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
786 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
789 ; Test that when unswitching a deeply nested loop condition in a way that
790 ; produces a non-loop clone that can reach multiple exit blocks which are part
791 ; of different outer loops we correctly divide the cloned loop blocks between
792 ; the outer loops based on reachability.
793 define i32 @test7a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
794 ; CHECK-LABEL: @test7a(
798 ; CHECK-NEXT: br label %loop_begin
801 %a = load i32, ptr %a.ptr
802 br label %inner_loop_begin
804 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
805 ; CHECK-NEXT: br label %inner_loop_begin
808 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
809 %cond = load i1, ptr %cond.ptr
810 %b = load i32, ptr %b.ptr
811 br label %inner_inner_loop_begin
812 ; CHECK: inner_loop_begin:
813 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
814 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
815 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
816 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
817 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
819 inner_inner_loop_begin:
820 %v1 = load i1, ptr %ptr
821 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
824 %v2 = load i1, ptr %ptr
825 br i1 %v2, label %loop_exit, label %inner_inner_loop_c
828 %v3 = load i1, ptr %ptr
829 br i1 %v3, label %inner_inner_loop_exit, label %inner_inner_loop_c
832 %v4 = load i1, ptr %ptr
833 br i1 %v4, label %inner_loop_exit, label %inner_inner_loop_d
836 br i1 %cond, label %inner_loop_exit, label %inner_inner_loop_begin
837 ; The cloned copy that always exits with the adjustments required to fix up
840 ; CHECK: inner_loop_begin.split.us:
841 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
843 ; CHECK: inner_inner_loop_begin.us:
844 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
845 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
847 ; CHECK: inner_inner_loop_b.us:
848 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
849 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_c.us.loopexit
851 ; CHECK: inner_inner_loop_a.us:
852 ; CHECK-NEXT: %[[A_NEW_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_begin.us ]
853 ; CHECK-NEXT: %[[B_NEW_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_begin.us ]
854 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
855 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %inner_inner_loop_c.us
857 ; CHECK: inner_inner_loop_c.us.loopexit:
858 ; CHECK-NEXT: br label %inner_inner_loop_c.us
860 ; CHECK: inner_inner_loop_c.us:
861 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
862 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split.us, label %inner_inner_loop_d.us
864 ; CHECK: inner_inner_loop_d.us:
865 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split
867 ; CHECK: inner_inner_loop_exit.split.us:
868 ; CHECK-NEXT: br label %inner_inner_loop_exit
870 ; CHECK: loop_exit.split.us:
871 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_NEW_LCSSA]], %inner_inner_loop_a.us ]
872 ; CHECK-NEXT: %[[B_LCSSA_US:.*]] = phi i32 [ %[[B_NEW_LCSSA]], %inner_inner_loop_a.us ]
873 ; CHECK-NEXT: br label %loop_exit
875 ; CHECK: inner_loop_exit.loopexit.split.us:
876 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
878 ; The original copy that continues to loop.
880 ; CHECK: inner_loop_begin.split:
881 ; CHECK-NEXT: br label %inner_inner_loop_begin
883 ; CHECK: inner_inner_loop_begin:
884 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
885 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
887 ; CHECK: inner_inner_loop_a:
888 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
889 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %inner_inner_loop_c
891 ; CHECK: inner_inner_loop_b:
892 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
893 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split, label %inner_inner_loop_c
895 ; CHECK: inner_inner_loop_c:
896 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
897 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split, label %inner_inner_loop_d
899 ; CHECK: inner_inner_loop_d:
900 ; CHECK-NEXT: br label %inner_inner_loop_begin
902 ; CHECK: inner_inner_loop_exit.split:
903 ; CHECK-NEXT: br label %inner_inner_loop_exit
905 inner_inner_loop_exit:
906 %a2 = load i32, ptr %a.ptr
907 %v5 = load i1, ptr %ptr
908 br i1 %v5, label %inner_loop_exit, label %inner_loop_begin
909 ; CHECK: inner_inner_loop_exit:
910 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
911 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
912 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
916 ; CHECK: inner_loop_exit.loopexit.split:
917 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
919 ; CHECK: inner_loop_exit.loopexit:
920 ; CHECK-NEXT: br label %inner_loop_exit
922 ; CHECK: inner_loop_exit.loopexit1:
923 ; CHECK-NEXT: br label %inner_loop_exit
925 ; CHECK: inner_loop_exit:
926 ; CHECK-NEXT: br label %loop_begin
929 %a.lcssa = phi i32 [ %a.phi, %inner_inner_loop_a ]
930 %b.lcssa = phi i32 [ %b, %inner_inner_loop_a ]
931 %result = add i32 %a.lcssa, %b.lcssa
933 ; CHECK: loop_exit.split:
934 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a ]
935 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_a ]
936 ; CHECK-NEXT: br label %loop_exit
939 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
940 ; CHECK-NEXT: %[[B_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[B_LCSSA_US]], %loop_exit.split.us ]
941 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[A_PHI]], %[[B_PHI]]
942 ; CHECK-NEXT: ret i32 %[[RESULT]]
945 ; Same pattern as @test7a but here the original loop becomes a non-loop that
946 ; can reach multiple exit blocks which are part of different outer loops.
947 define i32 @test7b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
948 ; CHECK-LABEL: @test7b(
952 ; CHECK-NEXT: br label %loop_begin
955 %a = load i32, ptr %a.ptr
956 br label %inner_loop_begin
958 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
959 ; CHECK-NEXT: br label %inner_loop_begin
962 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
963 %cond = load i1, ptr %cond.ptr
964 %b = load i32, ptr %b.ptr
965 br label %inner_inner_loop_begin
966 ; CHECK: inner_loop_begin:
967 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
968 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
969 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
970 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
971 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
973 inner_inner_loop_begin:
974 %v1 = load i1, ptr %ptr
975 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
978 %v2 = load i1, ptr %ptr
979 br i1 %v2, label %loop_exit, label %inner_inner_loop_c
982 %v3 = load i1, ptr %ptr
983 br i1 %v3, label %inner_inner_loop_exit, label %inner_inner_loop_c
986 %v4 = load i1, ptr %ptr
987 br i1 %v4, label %inner_loop_exit, label %inner_inner_loop_d
990 br i1 %cond, label %inner_inner_loop_begin, label %inner_loop_exit
991 ; The cloned copy that continues looping.
993 ; CHECK: inner_loop_begin.split.us:
994 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
996 ; CHECK: inner_inner_loop_begin.us:
997 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
998 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
1000 ; CHECK: inner_inner_loop_b.us:
1001 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1002 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_c.us
1004 ; CHECK: inner_inner_loop_a.us:
1005 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1006 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %inner_inner_loop_c.us
1008 ; CHECK: inner_inner_loop_c.us:
1009 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1010 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split.us, label %inner_inner_loop_d.us
1012 ; CHECK: inner_inner_loop_d.us:
1013 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1015 ; CHECK: inner_inner_loop_exit.split.us:
1016 ; CHECK-NEXT: br label %inner_inner_loop_exit
1018 ; CHECK: loop_exit.split.us:
1019 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a.us ]
1020 ; CHECK-NEXT: %[[B_LCSSA_US:.*]] = phi i32 [ %[[B]], %inner_inner_loop_a.us ]
1021 ; CHECK-NEXT: br label %loop_exit
1023 ; CHECK: inner_loop_exit.loopexit.split.us:
1024 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1026 ; The original copy that now always exits and needs adjustments for exit
1029 ; CHECK: inner_loop_begin.split:
1030 ; CHECK-NEXT: br label %inner_inner_loop_begin
1032 ; CHECK: inner_inner_loop_begin:
1033 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1034 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1036 ; CHECK: inner_inner_loop_a:
1037 ; CHECK-NEXT: %[[A_NEW_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_begin ]
1038 ; CHECK-NEXT: %[[B_NEW_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_begin ]
1039 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1040 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %inner_inner_loop_c
1042 ; CHECK: inner_inner_loop_b:
1043 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1044 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split, label %inner_inner_loop_c.loopexit
1046 ; CHECK: inner_inner_loop_c.loopexit:
1047 ; CHECK-NEXT: br label %inner_inner_loop_c
1049 ; CHECK: inner_inner_loop_c:
1050 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1051 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split, label %inner_inner_loop_d
1053 ; CHECK: inner_inner_loop_d:
1054 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split
1056 ; CHECK: inner_inner_loop_exit.split:
1057 ; CHECK-NEXT: br label %inner_inner_loop_exit
1059 inner_inner_loop_exit:
1060 %a2 = load i32, ptr %a.ptr
1061 %v5 = load i1, ptr %ptr
1062 br i1 %v5, label %inner_loop_exit, label %inner_loop_begin
1063 ; CHECK: inner_inner_loop_exit:
1064 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1065 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1066 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1069 br label %loop_begin
1070 ; CHECK: inner_loop_exit.loopexit.split:
1071 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1073 ; CHECK: inner_loop_exit.loopexit:
1074 ; CHECK-NEXT: br label %inner_loop_exit
1076 ; CHECK: inner_loop_exit.loopexit1:
1077 ; CHECK-NEXT: br label %inner_loop_exit
1079 ; CHECK: inner_loop_exit:
1080 ; CHECK-NEXT: br label %loop_begin
1083 %a.lcssa = phi i32 [ %a.phi, %inner_inner_loop_a ]
1084 %b.lcssa = phi i32 [ %b, %inner_inner_loop_a ]
1085 %result = add i32 %a.lcssa, %b.lcssa
1087 ; CHECK: loop_exit.split:
1088 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_NEW_LCSSA]], %inner_inner_loop_a ]
1089 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_NEW_LCSSA]], %inner_inner_loop_a ]
1090 ; CHECK-NEXT: br label %loop_exit
1093 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
1094 ; CHECK-NEXT: %[[B_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[B_LCSSA_US]], %loop_exit.split.us ]
1095 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[A_PHI]], %[[B_PHI]]
1096 ; CHECK-NEXT: ret i32 %[[RESULT]]
1099 ; Test that when the exit block set of an inner loop changes to start at a less
1100 ; high level of the loop nest we correctly hoist the loop up the nest.
1101 define i32 @test8a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1102 ; CHECK-LABEL: @test8a(
1104 br label %loop_begin
1105 ; CHECK-NEXT: entry:
1106 ; CHECK-NEXT: br label %loop_begin
1109 %a = load i32, ptr %a.ptr
1110 br label %inner_loop_begin
1111 ; CHECK: loop_begin:
1112 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1113 ; CHECK-NEXT: br label %inner_loop_begin
1116 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
1117 %cond = load i1, ptr %cond.ptr
1118 %b = load i32, ptr %b.ptr
1119 br label %inner_inner_loop_begin
1120 ; CHECK: inner_loop_begin:
1121 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
1122 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1123 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1124 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1125 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
1127 inner_inner_loop_begin:
1128 %v1 = load i1, ptr %ptr
1129 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
1132 %v2 = load i1, ptr %ptr
1133 br i1 %v2, label %inner_inner_loop_latch, label %inner_loop_exit
1136 br i1 %cond, label %inner_inner_loop_latch, label %inner_inner_loop_exit
1138 inner_inner_loop_latch:
1139 br label %inner_inner_loop_begin
1140 ; The cloned region is now an exit from the inner loop.
1142 ; CHECK: inner_loop_begin.split.us:
1143 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_begin ]
1144 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1146 ; CHECK: inner_inner_loop_begin.us:
1147 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1148 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
1150 ; CHECK: inner_inner_loop_b.us:
1151 ; CHECK-NEXT: br label %inner_inner_loop_latch.us
1153 ; CHECK: inner_inner_loop_a.us:
1154 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1155 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch.us, label %inner_loop_exit.loopexit.split.us
1157 ; CHECK: inner_inner_loop_latch.us:
1158 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1160 ; CHECK: inner_loop_exit.loopexit.split.us:
1161 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_a.us ]
1162 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1164 ; The original region exits the loop earlier.
1166 ; CHECK: inner_loop_begin.split:
1167 ; CHECK-NEXT: br label %inner_inner_loop_begin
1169 ; CHECK: inner_inner_loop_begin:
1170 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1171 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1173 ; CHECK: inner_inner_loop_a:
1174 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1175 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch, label %inner_loop_exit.loopexit.split
1177 ; CHECK: inner_inner_loop_b:
1178 ; CHECK-NEXT: br label %inner_inner_loop_exit
1180 ; CHECK: inner_inner_loop_latch:
1181 ; CHECK-NEXT: br label %inner_inner_loop_begin
1183 inner_inner_loop_exit:
1184 %a2 = load i32, ptr %a.ptr
1185 %v4 = load i1, ptr %ptr
1186 br i1 %v4, label %inner_loop_exit, label %inner_loop_begin
1187 ; CHECK: inner_inner_loop_exit:
1188 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1189 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1190 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1193 %v5 = load i1, ptr %ptr
1194 br i1 %v5, label %loop_exit, label %loop_begin
1195 ; CHECK: inner_loop_exit.loopexit.split:
1196 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a ]
1197 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1199 ; CHECK: inner_loop_exit.loopexit:
1200 ; CHECK-NEXT: %[[A_INNER_US_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit.split ], [ %[[A_INNER_LCSSA_US]], %inner_loop_exit.loopexit.split.us ]
1201 ; CHECK-NEXT: br label %inner_loop_exit
1203 ; CHECK: inner_loop_exit.loopexit1:
1204 ; CHECK-NEXT: %[[A_INNER_LCSSA2:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_exit ]
1205 ; CHECK-NEXT: br label %inner_loop_exit
1207 ; CHECK: inner_loop_exit:
1208 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA2]], %inner_loop_exit.loopexit1 ], [ %[[A_INNER_US_PHI]], %inner_loop_exit.loopexit ]
1209 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1210 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit, label %loop_begin
1213 %a.lcssa = phi i32 [ %a.phi, %inner_loop_exit ]
1216 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1217 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1220 ; Same pattern as @test8a but where the original loop looses an exit block and
1221 ; needs to be hoisted up the nest.
1222 define i32 @test8b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1223 ; CHECK-LABEL: @test8b(
1225 br label %loop_begin
1226 ; CHECK-NEXT: entry:
1227 ; CHECK-NEXT: br label %loop_begin
1230 %a = load i32, ptr %a.ptr
1231 br label %inner_loop_begin
1232 ; CHECK: loop_begin:
1233 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1234 ; CHECK-NEXT: br label %inner_loop_begin
1237 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
1238 %cond = load i1, ptr %cond.ptr
1239 %b = load i32, ptr %b.ptr
1240 br label %inner_inner_loop_begin
1241 ; CHECK: inner_loop_begin:
1242 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
1243 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1244 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1245 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1246 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
1248 inner_inner_loop_begin:
1249 %v1 = load i1, ptr %ptr
1250 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
1253 %v2 = load i1, ptr %ptr
1254 br i1 %v2, label %inner_inner_loop_latch, label %inner_loop_exit
1257 br i1 %cond, label %inner_inner_loop_exit, label %inner_inner_loop_latch
1259 inner_inner_loop_latch:
1260 br label %inner_inner_loop_begin
1261 ; The cloned region is similar to before but with one earlier exit.
1263 ; CHECK: inner_loop_begin.split.us:
1264 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1266 ; CHECK: inner_inner_loop_begin.us:
1267 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1268 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
1270 ; CHECK: inner_inner_loop_b.us:
1271 ; CHECK-NEXT: br label %inner_inner_loop_exit.split.us
1273 ; CHECK: inner_inner_loop_a.us:
1274 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1275 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch.us, label %inner_loop_exit.loopexit.split.us
1277 ; CHECK: inner_inner_loop_latch.us:
1278 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1280 ; CHECK: inner_inner_loop_exit.split.us:
1281 ; CHECK-NEXT: br label %inner_inner_loop_exit
1283 ; CHECK: inner_loop_exit.loopexit.split.us:
1284 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a.us ]
1285 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1287 ; The original region is now an exit in the preheader.
1289 ; CHECK: inner_loop_begin.split:
1290 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_begin ]
1291 ; CHECK-NEXT: br label %inner_inner_loop_begin
1293 ; CHECK: inner_inner_loop_begin:
1294 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1295 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1297 ; CHECK: inner_inner_loop_a:
1298 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1299 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch, label %inner_loop_exit.loopexit.split
1301 ; CHECK: inner_inner_loop_b:
1302 ; CHECK-NEXT: br label %inner_inner_loop_latch
1304 ; CHECK: inner_inner_loop_latch:
1305 ; CHECK-NEXT: br label %inner_inner_loop_begin
1307 inner_inner_loop_exit:
1308 %a2 = load i32, ptr %a.ptr
1309 %v4 = load i1, ptr %ptr
1310 br i1 %v4, label %inner_loop_exit, label %inner_loop_begin
1311 ; CHECK: inner_inner_loop_exit:
1312 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1313 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1314 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1317 %v5 = load i1, ptr %ptr
1318 br i1 %v5, label %loop_exit, label %loop_begin
1319 ; CHECK: inner_loop_exit.loopexit.split:
1320 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_a ]
1321 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1323 ; CHECK: inner_loop_exit.loopexit:
1324 ; CHECK-NEXT: %[[A_INNER_US_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit.split ], [ %[[A_INNER_LCSSA_US]], %inner_loop_exit.loopexit.split.us ]
1325 ; CHECK-NEXT: br label %inner_loop_exit
1327 ; CHECK: inner_loop_exit.loopexit1:
1328 ; CHECK-NEXT: %[[A_INNER_LCSSA2:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_exit ]
1329 ; CHECK-NEXT: br label %inner_loop_exit
1331 ; CHECK: inner_loop_exit:
1332 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA2]], %inner_loop_exit.loopexit1 ], [ %[[A_INNER_US_PHI]], %inner_loop_exit.loopexit ]
1333 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1334 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit, label %loop_begin
1337 %a.lcssa = phi i32 [ %a.phi, %inner_loop_exit ]
1340 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1341 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1344 ; Test for when unswitching produces a clone of an inner loop but
1345 ; the clone no longer has an exiting edge *at all* and loops infinitely.
1346 ; Because it doesn't ever exit to the outer loop it is no longer an inner loop
1347 ; but needs to be hoisted up the nest to be a top-level loop.
1348 define i32 @test9a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1349 ; CHECK-LABEL: @test9a(
1351 br label %loop_begin
1352 ; CHECK-NEXT: entry:
1353 ; CHECK-NEXT: br label %loop_begin
1356 %b = load i32, ptr %b.ptr
1357 %cond = load i1, ptr %cond.ptr
1358 br label %inner_loop_begin
1359 ; CHECK: loop_begin:
1360 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1361 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1362 ; CHECK-NEXT: br i1 %[[COND]], label %loop_begin.split.us, label %loop_begin.split
1365 %a = load i32, ptr %a.ptr
1366 br i1 %cond, label %inner_loop_latch, label %inner_loop_exit
1369 call void @sink1(i32 %b)
1370 br label %inner_loop_begin
1371 ; The cloned inner loop ends up as an infinite loop and thus being a top-level
1372 ; loop with the preheader as an exit block of the outer loop.
1374 ; CHECK: loop_begin.split.us
1375 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_begin ]
1376 ; CHECK-NEXT: br label %inner_loop_begin.us
1378 ; CHECK: inner_loop_begin.us:
1379 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1380 ; CHECK-NEXT: br label %inner_loop_latch.us
1382 ; CHECK: inner_loop_latch.us:
1383 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1384 ; CHECK-NEXT: br label %inner_loop_begin.us
1386 ; The original loop becomes boring non-loop code.
1388 ; CHECK: loop_begin.split
1389 ; CHECK-NEXT: br label %inner_loop_begin
1391 ; CHECK: inner_loop_begin:
1392 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1393 ; CHECK-NEXT: br label %inner_loop_exit
1396 %a.inner_lcssa = phi i32 [ %a, %inner_loop_begin ]
1397 %v = load i1, ptr %ptr
1398 br i1 %v, label %loop_begin, label %loop_exit
1399 ; CHECK: inner_loop_exit:
1400 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin ]
1401 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1402 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1405 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1408 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit ]
1409 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1412 ; The same core pattern as @test9a, but instead of the cloned loop becoming an
1413 ; infinite loop, the original loop has its only exit unswitched and the
1414 ; original loop becomes infinite and must be hoisted out of the loop nest.
1415 define i32 @test9b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1416 ; CHECK-LABEL: @test9b(
1418 br label %loop_begin
1419 ; CHECK-NEXT: entry:
1420 ; CHECK-NEXT: br label %loop_begin
1423 %b = load i32, ptr %b.ptr
1424 %cond = load i1, ptr %cond.ptr
1425 br label %inner_loop_begin
1426 ; CHECK: loop_begin:
1427 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1428 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1429 ; CHECK-NEXT: br i1 %[[COND]], label %loop_begin.split.us, label %loop_begin.split
1432 %a = load i32, ptr %a.ptr
1433 br i1 %cond, label %inner_loop_exit, label %inner_loop_latch
1436 call void @sink1(i32 %b)
1437 br label %inner_loop_begin
1438 ; The cloned inner loop becomes a boring non-loop.
1440 ; CHECK: loop_begin.split.us
1441 ; CHECK-NEXT: br label %inner_loop_begin.us
1443 ; CHECK: inner_loop_begin.us:
1444 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1445 ; CHECK-NEXT: br label %inner_loop_exit.split.us
1447 ; CHECK: inner_loop_exit.split.us
1448 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
1449 ; CHECK-NEXT: br label %inner_loop_exit
1451 ; The original loop becomes an infinite loop and thus a top-level loop with the
1452 ; preheader as an exit block for the outer loop.
1454 ; CHECK: loop_begin.split
1455 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_begin ]
1456 ; CHECK-NEXT: br label %inner_loop_begin
1458 ; CHECK: inner_loop_begin:
1459 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1460 ; CHECK-NEXT: br label %inner_loop_latch
1462 ; CHECK: inner_loop_latch:
1463 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1464 ; CHECK-NEXT: br label %inner_loop_begin
1467 %a.inner_lcssa = phi i32 [ %a, %inner_loop_begin ]
1468 %v = load i1, ptr %ptr
1469 br i1 %v, label %loop_begin, label %loop_exit
1470 ; CHECK: inner_loop_exit:
1471 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1472 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1475 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1478 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %inner_loop_exit ]
1479 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1482 ; Test that requires re-forming dedicated exits for the cloned loop.
1483 define i32 @test10a(ptr %ptr, i1 %cond, ptr %a.ptr) {
1484 ; CHECK-LABEL: @test10a(
1486 br label %loop_begin
1487 ; CHECK-NEXT: entry:
1488 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
1489 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
1492 %a = load i32, ptr %a.ptr
1493 %v1 = load i1, ptr %ptr
1494 br i1 %v1, label %loop_a, label %loop_b
1497 %v2 = load i1, ptr %ptr
1498 br i1 %v2, label %loop_exit, label %loop_begin
1501 br i1 %cond, label %loop_exit, label %loop_begin
1502 ; The cloned loop with one edge as a direct exit.
1504 ; CHECK: entry.split.us:
1505 ; CHECK-NEXT: br label %loop_begin.us
1507 ; CHECK: loop_begin.us:
1508 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1509 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1510 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
1513 ; CHECK-NEXT: %[[A_LCSSA_B:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
1514 ; CHECK-NEXT: br label %loop_exit.split.us
1517 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1518 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us.loopexit, label %loop_begin.backedge.us
1520 ; CHECK: loop_begin.backedge.us:
1521 ; CHECK-NEXT: br label %loop_begin.us
1523 ; CHECK: loop_exit.split.us.loopexit:
1524 ; CHECK-NEXT: %[[A_LCSSA_A:.*]] = phi i32 [ %[[A]], %loop_a.us ]
1525 ; CHECK-NEXT: br label %loop_exit
1527 ; CHECK: loop_exit.split.us:
1528 ; CHECK-NEXT: %[[A_PHI_US:.*]] = phi i32 [ %[[A_LCSSA_B]], %loop_b.us ], [ %[[A_LCSSA_A]], %loop_exit.split.us.loopexit ]
1529 ; CHECK-NEXT: br label %loop_exit
1531 ; The original loop without one 'loop_exit' edge.
1533 ; CHECK: entry.split:
1534 ; CHECK-NEXT: br label %loop_begin
1536 ; CHECK: loop_begin:
1537 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1538 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1539 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
1542 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1543 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %loop_begin.backedge
1545 ; CHECK: loop_begin.backedge:
1546 ; CHECK-NEXT: br label %loop_begin
1549 ; CHECK-NEXT: br label %loop_begin.backedge
1551 ; CHECK: loop_exit.split:
1552 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
1553 ; CHECK-NEXT: br label %loop_exit
1556 %a.lcssa = phi i32 [ %a, %loop_a ], [ %a, %loop_b ]
1559 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_PHI_US]], %loop_exit.split.us ]
1560 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1563 ; Test that requires re-forming dedicated exits for the original loop.
1564 define i32 @test10b(ptr %ptr, i1 %cond, ptr %a.ptr) {
1565 ; CHECK-LABEL: @test10b(
1567 br label %loop_begin
1568 ; CHECK-NEXT: entry:
1569 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
1570 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
1573 %a = load i32, ptr %a.ptr
1574 %v1 = load i1, ptr %ptr
1575 br i1 %v1, label %loop_a, label %loop_b
1578 %v2 = load i1, ptr %ptr
1579 br i1 %v2, label %loop_begin, label %loop_exit
1582 br i1 %cond, label %loop_begin, label %loop_exit
1583 ; The cloned loop without one of the exits.
1585 ; CHECK: entry.split.us:
1586 ; CHECK-NEXT: br label %loop_begin.us
1588 ; CHECK: loop_begin.us:
1589 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1590 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1591 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
1594 ; CHECK-NEXT: br label %loop_begin.backedge.us
1597 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1598 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge.us, label %loop_exit.split.us
1600 ; CHECK: loop_begin.backedge.us:
1601 ; CHECK-NEXT: br label %loop_begin.us
1603 ; CHECK: loop_exit.split.us:
1604 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
1605 ; CHECK-NEXT: br label %loop_exit
1607 ; The original loop without one 'loop_exit' edge.
1609 ; CHECK: entry.split:
1610 ; CHECK-NEXT: br label %loop_begin
1612 ; CHECK: loop_begin:
1613 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1614 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1615 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
1618 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1619 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge, label %loop_exit.split.loopexit
1621 ; CHECK: loop_begin.backedge:
1622 ; CHECK-NEXT: br label %loop_begin
1625 ; CHECK-NEXT: %[[A_LCSSA_B:.*]] = phi i32 [ %[[A]], %loop_begin ]
1626 ; CHECK-NEXT: br label %loop_exit.split
1628 ; CHECK: loop_exit.split.loopexit:
1629 ; CHECK-NEXT: %[[A_LCSSA_A:.*]] = phi i32 [ %[[A]], %loop_a ]
1630 ; CHECK-NEXT: br label %loop_exit.split
1632 ; CHECK: loop_exit.split:
1633 ; CHECK-NEXT: %[[A_PHI_SPLIT:.*]] = phi i32 [ %[[A_LCSSA_B]], %loop_b ], [ %[[A_LCSSA_A]], %loop_exit.split.loopexit ]
1634 ; CHECK-NEXT: br label %loop_exit
1637 %a.lcssa = phi i32 [ %a, %loop_a ], [ %a, %loop_b ]
1640 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_PHI_SPLIT]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
1641 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1644 ; Check that if a cloned inner loop after unswitching doesn't loop and directly
1645 ; exits even an outer loop, we don't add the cloned preheader to the outer
1646 ; loop and do add the needed LCSSA phi nodes for the new exit block from the
1648 define i32 @test11a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1649 ; CHECK-LABEL: @test11a(
1651 br label %loop_begin
1652 ; CHECK-NEXT: entry:
1653 ; CHECK-NEXT: br label %loop_begin
1656 %b = load i32, ptr %b.ptr
1657 %v1 = load i1, ptr %ptr
1658 br i1 %v1, label %loop_latch, label %inner_loop_ph
1659 ; CHECK: loop_begin:
1660 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1661 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1662 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %inner_loop_ph
1665 %cond = load i1, ptr %cond.ptr
1666 br label %inner_loop_begin
1667 ; CHECK: inner_loop_ph:
1668 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1669 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1670 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_ph.split.us, label %inner_loop_ph.split
1673 call void @sink1(i32 %b)
1674 %a = load i32, ptr %a.ptr
1675 br i1 %cond, label %loop_exit, label %inner_loop_a
1678 %v2 = load i1, ptr %ptr
1679 br i1 %v2, label %inner_loop_exit, label %inner_loop_begin
1680 ; The cloned path doesn't actually loop and is an exit from the outer loop as
1683 ; CHECK: inner_loop_ph.split.us:
1684 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_ph ]
1685 ; CHECK-NEXT: br label %inner_loop_begin.us
1687 ; CHECK: inner_loop_begin.us:
1688 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1689 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1690 ; CHECK-NEXT: br label %loop_exit.loopexit.split.us
1692 ; CHECK: loop_exit.loopexit.split.us:
1693 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
1694 ; CHECK-NEXT: br label %loop_exit.loopexit
1696 ; The original remains a loop losing the exit edge.
1698 ; CHECK: inner_loop_ph.split:
1699 ; CHECK-NEXT: br label %inner_loop_begin
1701 ; CHECK: inner_loop_begin:
1702 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1703 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1704 ; CHECK-NEXT: br label %inner_loop_a
1706 ; CHECK: inner_loop_a:
1707 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1708 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit, label %inner_loop_begin
1711 %a.inner_lcssa = phi i32 [ %a, %inner_loop_a ]
1712 %v3 = load i1, ptr %ptr
1713 br i1 %v3, label %loop_latch, label %loop_exit
1714 ; CHECK: inner_loop_exit:
1715 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_a ]
1716 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1717 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %loop_exit.loopexit1
1720 br label %loop_begin
1721 ; CHECK: loop_latch:
1722 ; CHECK-NEXT: br label %loop_begin
1725 %a.lcssa = phi i32 [ %a, %inner_loop_begin ], [ %a.inner_lcssa, %inner_loop_exit ]
1727 ; CHECK: loop_exit.loopexit:
1728 ; CHECK-NEXT: br label %loop_exit
1730 ; CHECK: loop_exit.loopexit1:
1731 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit ]
1732 ; CHECK-NEXT: br label %loop_exit
1735 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %loop_exit.loopexit ], [ %[[A_LCSSA]], %loop_exit.loopexit1 ]
1736 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1739 ; Check that if the original inner loop after unswitching doesn't loop and
1740 ; directly exits even an outer loop, we remove the original preheader from the
1741 ; outer loop and add needed LCSSA phi nodes for the new exit block from the
1743 define i32 @test11b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1744 ; CHECK-LABEL: @test11b(
1746 br label %loop_begin
1747 ; CHECK-NEXT: entry:
1748 ; CHECK-NEXT: br label %loop_begin
1751 %b = load i32, ptr %b.ptr
1752 %v1 = load i1, ptr %ptr
1753 br i1 %v1, label %loop_latch, label %inner_loop_ph
1754 ; CHECK: loop_begin:
1755 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1756 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1757 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %inner_loop_ph
1760 %cond = load i1, ptr %cond.ptr
1761 br label %inner_loop_begin
1762 ; CHECK: inner_loop_ph:
1763 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1764 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1765 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_ph.split.us, label %inner_loop_ph.split
1768 call void @sink1(i32 %b)
1769 %a = load i32, ptr %a.ptr
1770 br i1 %cond, label %inner_loop_a, label %loop_exit
1773 %v2 = load i1, ptr %ptr
1774 br i1 %v2, label %inner_loop_exit, label %inner_loop_begin
1775 ; The cloned path continues to loop without the exit out of the entire nest.
1777 ; CHECK: inner_loop_ph.split.us:
1778 ; CHECK-NEXT: br label %inner_loop_begin.us
1780 ; CHECK: inner_loop_begin.us:
1781 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1782 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1783 ; CHECK-NEXT: br label %inner_loop_a.us
1785 ; CHECK: inner_loop_a.us:
1786 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1787 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.split.us, label %inner_loop_begin.us
1789 ; CHECK: inner_loop_exit.split.us:
1790 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_a.us ]
1791 ; CHECK-NEXT: br label %inner_loop_exit
1793 ; The original remains a loop losing the exit edge.
1795 ; CHECK: inner_loop_ph.split:
1796 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_ph ]
1797 ; CHECK-NEXT: br label %inner_loop_begin
1799 ; CHECK: inner_loop_begin:
1800 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1801 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1802 ; CHECK-NEXT: br label %loop_exit.loopexit
1805 %a.inner_lcssa = phi i32 [ %a, %inner_loop_a ]
1806 %v3 = load i1, ptr %ptr
1807 br i1 %v3, label %loop_latch, label %loop_exit
1808 ; CHECK: inner_loop_exit:
1809 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1810 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %loop_exit.loopexit1
1813 br label %loop_begin
1814 ; CHECK: loop_latch:
1815 ; CHECK-NEXT: br label %loop_begin
1818 %a.lcssa = phi i32 [ %a, %inner_loop_begin ], [ %a.inner_lcssa, %inner_loop_exit ]
1820 ; CHECK: loop_exit.loopexit:
1821 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin ]
1822 ; CHECK-NEXT: br label %loop_exit
1824 ; CHECK: loop_exit.loopexit1:
1825 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %inner_loop_exit ]
1826 ; CHECK-NEXT: br label %loop_exit
1829 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.loopexit ], [ %[[A_LCSSA_US]], %loop_exit.loopexit1 ]
1830 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1833 ; Like test11a, but checking that when the whole thing is wrapped in yet
1834 ; another loop, we correctly attribute the cloned preheader to that outermost
1835 ; loop rather than only handling the case where the preheader is not in any loop
1837 define i32 @test12a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1838 ; CHECK-LABEL: @test12a(
1840 br label %loop_begin
1841 ; CHECK-NEXT: entry:
1842 ; CHECK-NEXT: br label %loop_begin
1845 br label %inner_loop_begin
1846 ; CHECK: loop_begin:
1847 ; CHECK-NEXT: br label %inner_loop_begin
1850 %b = load i32, ptr %b.ptr
1851 %v1 = load i1, ptr %ptr
1852 br i1 %v1, label %inner_loop_latch, label %inner_inner_loop_ph
1853 ; CHECK: inner_loop_begin:
1854 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1855 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1856 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_inner_loop_ph
1858 inner_inner_loop_ph:
1859 %cond = load i1, ptr %cond.ptr
1860 br label %inner_inner_loop_begin
1861 ; CHECK: inner_inner_loop_ph:
1862 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1863 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1864 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_inner_loop_ph.split.us, label %inner_inner_loop_ph.split
1866 inner_inner_loop_begin:
1867 call void @sink1(i32 %b)
1868 %a = load i32, ptr %a.ptr
1869 br i1 %cond, label %inner_loop_exit, label %inner_inner_loop_a
1872 %v2 = load i1, ptr %ptr
1873 br i1 %v2, label %inner_inner_loop_exit, label %inner_inner_loop_begin
1874 ; The cloned path doesn't actually loop and is an exit from the outer loop as
1877 ; CHECK: inner_inner_loop_ph.split.us:
1878 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_ph ]
1879 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1881 ; CHECK: inner_inner_loop_begin.us:
1882 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1883 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1884 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split.us
1886 ; CHECK: inner_loop_exit.loopexit.split.us:
1887 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_inner_loop_begin.us ]
1888 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1890 ; The original remains a loop losing the exit edge.
1892 ; CHECK: inner_inner_loop_ph.split:
1893 ; CHECK-NEXT: br label %inner_inner_loop_begin
1895 ; CHECK: inner_inner_loop_begin:
1896 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1897 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1898 ; CHECK-NEXT: br label %inner_inner_loop_a
1900 ; CHECK: inner_inner_loop_a:
1901 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1902 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit, label %inner_inner_loop_begin
1904 inner_inner_loop_exit:
1905 %a.inner_inner_lcssa = phi i32 [ %a, %inner_inner_loop_a ]
1906 %v3 = load i1, ptr %ptr
1907 br i1 %v3, label %inner_loop_latch, label %inner_loop_exit
1908 ; CHECK: inner_inner_loop_exit:
1909 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_inner_loop_a ]
1910 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1911 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_loop_exit.loopexit1
1914 br label %inner_loop_begin
1915 ; CHECK: inner_loop_latch:
1916 ; CHECK-NEXT: br label %inner_loop_begin
1919 %a.inner_lcssa = phi i32 [ %a, %inner_inner_loop_begin ], [ %a.inner_inner_lcssa, %inner_inner_loop_exit ]
1920 %v4 = load i1, ptr %ptr
1921 br i1 %v4, label %loop_begin, label %loop_exit
1922 ; CHECK: inner_loop_exit.loopexit:
1923 ; CHECK-NEXT: br label %inner_loop_exit
1925 ; CHECK: inner_loop_exit.loopexit1:
1926 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_exit ]
1927 ; CHECK-NEXT: br label %inner_loop_exit
1929 ; CHECK: inner_loop_exit:
1930 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA_US]], %inner_loop_exit.loopexit ], [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit1 ]
1931 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1932 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1935 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1938 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1939 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1942 ; Like test11b, but checking that when the whole thing is wrapped in yet
1943 ; another loop, we correctly sink the preheader to the outermost loop rather
1944 ; than only handling the case where the preheader is completely removed from
1946 define i32 @test12b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1947 ; CHECK-LABEL: @test12b(
1949 br label %loop_begin
1950 ; CHECK-NEXT: entry:
1951 ; CHECK-NEXT: br label %loop_begin
1954 br label %inner_loop_begin
1955 ; CHECK: loop_begin:
1956 ; CHECK-NEXT: br label %inner_loop_begin
1959 %b = load i32, ptr %b.ptr
1960 %v1 = load i1, ptr %ptr
1961 br i1 %v1, label %inner_loop_latch, label %inner_inner_loop_ph
1962 ; CHECK: inner_loop_begin:
1963 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1964 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1965 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_inner_loop_ph
1967 inner_inner_loop_ph:
1968 %cond = load i1, ptr %cond.ptr
1969 br label %inner_inner_loop_begin
1970 ; CHECK: inner_inner_loop_ph:
1971 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1972 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1973 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_inner_loop_ph.split.us, label %inner_inner_loop_ph.split
1975 inner_inner_loop_begin:
1976 call void @sink1(i32 %b)
1977 %a = load i32, ptr %a.ptr
1978 br i1 %cond, label %inner_inner_loop_a, label %inner_loop_exit
1981 %v2 = load i1, ptr %ptr
1982 br i1 %v2, label %inner_inner_loop_exit, label %inner_inner_loop_begin
1983 ; The cloned path continues to loop without the exit out of the entire nest.
1985 ; CHECK: inner_inner_loop_ph.split.us:
1986 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1988 ; CHECK: inner_inner_loop_begin.us:
1989 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1990 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1991 ; CHECK-NEXT: br label %inner_inner_loop_a.us
1993 ; CHECK: inner_inner_loop_a.us:
1994 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1995 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_begin.us
1997 ; CHECK: inner_inner_loop_exit.split.us:
1998 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_inner_loop_a.us ]
1999 ; CHECK-NEXT: br label %inner_inner_loop_exit
2001 ; The original remains a loop losing the exit edge.
2003 ; CHECK: inner_inner_loop_ph.split:
2004 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_ph ]
2005 ; CHECK-NEXT: br label %inner_inner_loop_begin
2007 ; CHECK: inner_inner_loop_begin:
2008 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
2009 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2010 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
2012 inner_inner_loop_exit:
2013 %a.inner_inner_lcssa = phi i32 [ %a, %inner_inner_loop_a ]
2014 %v3 = load i1, ptr %ptr
2015 br i1 %v3, label %inner_loop_latch, label %inner_loop_exit
2016 ; CHECK: inner_inner_loop_exit:
2017 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2018 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_loop_exit.loopexit1
2021 br label %inner_loop_begin
2022 ; CHECK: inner_loop_latch:
2023 ; CHECK-NEXT: br label %inner_loop_begin
2026 %a.inner_lcssa = phi i32 [ %a, %inner_inner_loop_begin ], [ %a.inner_inner_lcssa, %inner_inner_loop_exit ]
2027 %v4 = load i1, ptr %ptr
2028 br i1 %v4, label %loop_begin, label %loop_exit
2029 ; CHECK: inner_loop_exit.loopexit:
2030 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_inner_loop_begin ]
2031 ; CHECK-NEXT: br label %inner_loop_exit
2033 ; CHECK: inner_loop_exit.loopexit1:
2034 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA_US]], %inner_inner_loop_exit ]
2035 ; CHECK-NEXT: br label %inner_loop_exit
2037 ; CHECK: inner_loop_exit:
2038 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit ], [ %[[A_INNER_LCSSA_US]], %inner_loop_exit.loopexit1 ]
2039 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2040 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
2043 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
2046 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
2047 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
2050 ; Test where the cloned loop has an inner loop that has to be traversed to form
2051 ; the cloned loop, and where this inner loop has multiple blocks, and where the
2052 ; exiting block that connects the inner loop to the cloned loop is not the header
2053 ; block. This ensures that we correctly handle interesting corner cases of
2054 ; traversing back to the header when establishing the cloned loop.
2055 define i32 @test13a(ptr %ptr, i1 %cond, ptr %a.ptr, ptr %b.ptr) {
2056 ; CHECK-LABEL: @test13a(
2058 br label %loop_begin
2059 ; CHECK-NEXT: entry:
2060 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
2061 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
2064 %a = load i32, ptr %a.ptr
2065 %v1 = load i1, ptr %ptr
2066 br i1 %v1, label %loop_a, label %loop_b
2069 %v2 = load i1, ptr %ptr
2070 br i1 %v2, label %loop_exit, label %loop_latch
2073 %b = load i32, ptr %b.ptr
2074 br i1 %cond, label %loop_b_inner_ph, label %loop_exit
2077 br label %loop_b_inner_header
2079 loop_b_inner_header:
2080 %v3 = load i1, ptr %ptr
2081 br i1 %v3, label %loop_b_inner_latch, label %loop_b_inner_body
2084 %v4 = load i1, ptr %ptr
2085 br i1 %v4, label %loop_b_inner_latch, label %loop_b_inner_exit
2088 br label %loop_b_inner_header
2091 br label %loop_latch
2094 br label %loop_begin
2095 ; The cloned loop contains an inner loop within it.
2097 ; CHECK: entry.split.us:
2098 ; CHECK-NEXT: br label %loop_begin.us
2100 ; CHECK: loop_begin.us:
2101 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2102 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2103 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
2106 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2107 ; CHECK-NEXT: br label %loop_b_inner_ph.us
2109 ; CHECK: loop_b_inner_ph.us:
2110 ; CHECK-NEXT: br label %loop_b_inner_header.us
2112 ; CHECK: loop_b_inner_header.us:
2113 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2114 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch.us, label %loop_b_inner_body.us
2116 ; CHECK: loop_b_inner_body.us:
2117 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2118 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch.us, label %loop_b_inner_exit.us
2120 ; CHECK: loop_b_inner_exit.us:
2121 ; CHECK-NEXT: br label %loop_latch.us
2123 ; CHECK: loop_b_inner_latch.us:
2124 ; CHECK-NEXT: br label %loop_b_inner_header.us
2127 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2128 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %loop_latch.us
2130 ; CHECK: loop_latch.us:
2131 ; CHECK-NEXT: br label %loop_begin.us
2133 ; CHECK: loop_exit.split.us:
2134 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
2135 ; CHECK-NEXT: br label %loop_exit
2137 ; And the original loop no longer contains an inner loop.
2139 ; CHECK: entry.split:
2140 ; CHECK-NEXT: br label %loop_begin
2142 ; CHECK: loop_begin:
2143 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2144 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2145 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
2148 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2149 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.loopexit, label %loop_latch
2152 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2153 ; CHECK-NEXT: br label %loop_exit.split
2155 ; CHECK: loop_latch:
2156 ; CHECK-NEXT: br label %loop_begin
2159 %lcssa = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
2161 ; CHECK: loop_exit.split.loopexit:
2162 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
2163 ; CHECK-NEXT: br label %loop_exit.split
2165 ; CHECK: loop_exit.split:
2166 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B]], %loop_b ], [ %[[A_LCSSA]], %loop_exit.split.loopexit ]
2167 ; CHECK-NEXT: br label %loop_exit
2170 ; CHECK-NEXT: %[[AB_PHI_US:.*]] = phi i32 [ %[[AB_PHI]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
2171 ; CHECK-NEXT: ret i32 %[[AB_PHI_US]]
2174 ; Test where the original loop has an inner loop that has to be traversed to
2175 ; rebuild the loop, and where this inner loop has multiple blocks, and where
2176 ; the exiting block that connects the inner loop to the original loop is not
2177 ; the header block. This ensures that we correctly handle interesting corner
2178 ; cases of traversing back to the header when re-establishing the original loop
2179 ; still exists after unswitching.
2180 define i32 @test13b(ptr %ptr, i1 %cond, ptr %a.ptr, ptr %b.ptr) {
2181 ; CHECK-LABEL: @test13b(
2183 br label %loop_begin
2184 ; CHECK-NEXT: entry:
2185 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
2186 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
2189 %a = load i32, ptr %a.ptr
2190 %v1 = load i1, ptr %ptr
2191 br i1 %v1, label %loop_a, label %loop_b
2194 %v2 = load i1, ptr %ptr
2195 br i1 %v2, label %loop_exit, label %loop_latch
2198 %b = load i32, ptr %b.ptr
2199 br i1 %cond, label %loop_exit, label %loop_b_inner_ph
2202 br label %loop_b_inner_header
2204 loop_b_inner_header:
2205 %v3 = load i1, ptr %ptr
2206 br i1 %v3, label %loop_b_inner_latch, label %loop_b_inner_body
2209 %v4 = load i1, ptr %ptr
2210 br i1 %v4, label %loop_b_inner_latch, label %loop_b_inner_exit
2213 br label %loop_b_inner_header
2216 br label %loop_latch
2219 br label %loop_begin
2220 ; The cloned loop doesn't contain an inner loop.
2222 ; CHECK: entry.split.us:
2223 ; CHECK-NEXT: br label %loop_begin.us
2225 ; CHECK: loop_begin.us:
2226 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2227 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2228 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
2231 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2232 ; CHECK-NEXT: br label %loop_exit.split.us
2235 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2236 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us.loopexit, label %loop_latch.us
2238 ; CHECK: loop_latch.us:
2239 ; CHECK-NEXT: br label %loop_begin.us
2241 ; CHECK: loop_exit.split.us.loopexit:
2242 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
2243 ; CHECK-NEXT: br label %loop_exit.split.us
2245 ; CHECK: loop_exit.split.us:
2246 ; CHECK-NEXT: %[[AB_PHI_US:.*]] = phi i32 [ %[[B]], %loop_b.us ], [ %[[A_LCSSA_US]], %loop_exit.split.us.loopexit ]
2247 ; CHECK-NEXT: br label %loop_exit
2249 ; But the original loop contains an inner loop that must be traversed.;
2251 ; CHECK: entry.split:
2252 ; CHECK-NEXT: br label %loop_begin
2254 ; CHECK: loop_begin:
2255 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2256 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2257 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
2260 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2261 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %loop_latch
2264 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2265 ; CHECK-NEXT: br label %loop_b_inner_ph
2267 ; CHECK: loop_b_inner_ph:
2268 ; CHECK-NEXT: br label %loop_b_inner_header
2270 ; CHECK: loop_b_inner_header:
2271 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2272 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch, label %loop_b_inner_body
2274 ; CHECK: loop_b_inner_body:
2275 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2276 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch, label %loop_b_inner_exit
2278 ; CHECK: loop_b_inner_latch:
2279 ; CHECK-NEXT: br label %loop_b_inner_header
2281 ; CHECK: loop_b_inner_exit:
2282 ; CHECK-NEXT: br label %loop_latch
2284 ; CHECK: loop_latch:
2285 ; CHECK-NEXT: br label %loop_begin
2288 %lcssa = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
2290 ; CHECK: loop_exit.split:
2291 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
2292 ; CHECK-NEXT: br label %loop_exit
2295 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[AB_PHI_US]], %loop_exit.split.us ]
2296 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
2299 define i32 @test20(ptr %var, i32 %cond1, i32 %cond2) {
2300 ; CHECK-LABEL: @test20(
2302 br label %loop_begin
2303 ; CHECK-NEXT: entry:
2304 ; CHECK-NEXT: switch i32 %cond2, label %[[ENTRY_SPLIT_EXIT:.*]] [
2305 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
2306 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_A]]
2307 ; CHECK-NEXT: i32 13, label %[[ENTRY_SPLIT_B:.*]]
2308 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_A]]
2309 ; CHECK-NEXT: i32 42, label %[[ENTRY_SPLIT_C:.*]]
2313 %var_val = load i32, ptr %var
2314 switch i32 %cond2, label %loop_exit [
2315 i32 0, label %loop_a
2316 i32 1, label %loop_a
2317 i32 13, label %loop_b
2318 i32 2, label %loop_a
2319 i32 42, label %loop_c
2324 br label %loop_latch
2325 ; Unswitched 'a' loop.
2327 ; CHECK: [[ENTRY_SPLIT_A]]:
2328 ; CHECK-NEXT: br label %[[LOOP_BEGIN_A:.*]]
2330 ; CHECK: [[LOOP_BEGIN_A]]:
2331 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2332 ; CHECK-NEXT: br label %[[LOOP_A:.*]]
2334 ; CHECK: [[LOOP_A]]:
2335 ; CHECK-NEXT: call i32 @a()
2336 ; CHECK-NEXT: br label %[[LOOP_LATCH_A:.*]]
2338 ; CHECK: [[LOOP_LATCH_A]]:
2339 ; CHECK: br label %[[LOOP_BEGIN_A]]
2343 br label %loop_latch
2344 ; Unswitched 'b' loop.
2346 ; CHECK: [[ENTRY_SPLIT_B]]:
2347 ; CHECK-NEXT: br label %[[LOOP_BEGIN_B:.*]]
2349 ; CHECK: [[LOOP_BEGIN_B]]:
2350 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2351 ; CHECK-NEXT: br label %[[LOOP_B:.*]]
2353 ; CHECK: [[LOOP_B]]:
2354 ; CHECK-NEXT: call i32 @b()
2355 ; CHECK-NEXT: br label %[[LOOP_LATCH_B:.*]]
2357 ; CHECK: [[LOOP_LATCH_B]]:
2358 ; CHECK: br label %[[LOOP_BEGIN_B]]
2361 call i32 @c() noreturn nounwind
2362 br label %loop_latch
2363 ; Unswitched 'c' loop.
2365 ; CHECK: [[ENTRY_SPLIT_C]]:
2366 ; CHECK-NEXT: br label %[[LOOP_BEGIN_C:.*]]
2368 ; CHECK: [[LOOP_BEGIN_C]]:
2369 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2370 ; CHECK-NEXT: br label %[[LOOP_C:.*]]
2372 ; CHECK: [[LOOP_C]]:
2373 ; CHECK-NEXT: call i32 @c()
2374 ; CHECK-NEXT: br label %[[LOOP_LATCH_C:.*]]
2376 ; CHECK: [[LOOP_LATCH_C]]:
2377 ; CHECK: br label %[[LOOP_BEGIN_C]]
2380 br label %loop_begin
2383 %lcssa = phi i32 [ %var_val, %loop_begin ]
2385 ; Unswitched exit edge (no longer a loop).
2387 ; CHECK: [[ENTRY_SPLIT_EXIT]]:
2388 ; CHECK-NEXT: br label %loop_begin
2390 ; CHECK: loop_begin:
2391 ; CHECK-NEXT: %[[V:.*]] = load i32, ptr %var
2392 ; CHECK-NEXT: br label %loop_exit
2395 ; CHECK-NEXT: %[[LCSSA:.*]] = phi i32 [ %[[V]], %loop_begin ]
2396 ; CHECK-NEXT: ret i32 %[[LCSSA]]
2399 ; Negative test: we do not switch when the loop contains unstructured control
2400 ; flows as it would significantly complicate the process as novel loops might
2402 define void @test_no_unswitch_unstructured_cfg(ptr %ptr, i1 %cond) {
2403 ; CHECK-LABEL: @test_no_unswitch_unstructured_cfg(
2405 br label %loop_begin
2408 br i1 %cond, label %loop_left, label %loop_right
2411 %v1 = load i1, ptr %ptr
2412 br i1 %v1, label %loop_right, label %loop_merge
2415 %v2 = load i1, ptr %ptr
2416 br i1 %v2, label %loop_left, label %loop_merge
2419 %v3 = load i1, ptr %ptr
2420 br i1 %v3, label %loop_latch, label %loop_exit
2423 br label %loop_begin
2429 ; A test reduced out of 403.gcc with interesting nested loops that trigger
2430 ; multiple unswitches. A key component of this test is that there are multiple
2431 ; paths to reach an inner loop after unswitching, and one of them is via the
2432 ; predecessors of the unswitched loop header. That can allow us to find the loop
2433 ; through multiple different paths.
2434 define void @test21(i1 %a, i1 %b) {
2435 ; CHECK-LABEL: @test21(
2438 ; CHECK-NOT: br i1 %a
2440 ; CHECK: %[[FROZEN:.+]] = freeze i1 %a
2441 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[BB_SPLIT_US:.*]], label %[[BB_SPLIT:.*]]
2443 ; CHECK-NOT: br i1 %a
2444 ; CHECK-NOT: br i1 %b
2446 ; CHECK: [[BB_SPLIT]]:
2449 ; CHECK-NOT: br i1 %a
2450 ; CHECK-NOT: br i1 %b
2453 %tmp1.0 = phi i32 [ 0, %bb ], [ %tmp1.3, %bb23 ]
2457 %tmp.0 = phi i1 [ true, %bb3 ], [ false, %bb19 ]
2458 %tmp1.1 = phi i32 [ %tmp1.0, %bb3 ], [ %tmp1.2.lcssa, %bb19 ]
2459 br i1 %tmp.0, label %bb11.preheader, label %bb23
2462 br i1 %a, label %bb19, label %bb14.lr.ph
2468 %tmp2.02 = phi i32 [ 0, %bb14.lr.ph ], [ 1, %bb14 ]
2469 br i1 %b, label %bb11.bb19_crit_edge, label %bb14
2471 bb11.bb19_crit_edge:
2472 %split = phi i32 [ %tmp2.02, %bb14 ]
2476 %tmp1.2.lcssa = phi i32 [ %split, %bb11.bb19_crit_edge ], [ %tmp1.1, %bb11.preheader ]
2477 %tmp21 = icmp eq i32 %tmp1.2.lcssa, 0
2478 br i1 %tmp21, label %bb23, label %bb7
2481 %tmp1.3 = phi i32 [ %tmp1.2.lcssa, %bb19 ], [ %tmp1.1, %bb7 ]
2485 ; A test reduced out of 400.perlbench that when unswitching the `%stop`
2486 ; condition clones a loop nest outside of a containing loop. This excercises a
2487 ; different cloning path from our other test cases and in turn verifying the
2488 ; resulting structure can catch any failures to correctly clone these nested
2492 declare i32 @h(i32 %arg)
2493 define void @test22(i32 %arg) {
2494 ; CHECK-LABEL: define void @test22(
2496 br label %loop1.header
2499 %stop = phi i1 [ true, %loop1.latch ], [ false, %entry ]
2500 %i = phi i32 [ %i.lcssa, %loop1.latch ], [ %arg, %entry ]
2501 ; CHECK: %[[I:.*]] = phi i32 [ %{{.*}}, %loop1.latch ], [ %arg, %entry ]
2502 br i1 %stop, label %loop1.exit, label %loop1.body.loop2.ph
2503 ; CHECK: br i1 %stop, label %loop1.exit, label %loop1.body.loop2.ph
2505 loop1.body.loop2.ph:
2506 br label %loop2.header
2507 ; Just check that the we unswitched the key condition and that leads to the
2508 ; inner loop header.
2510 ; CHECK: loop1.body.loop2.ph:
2511 ; CHECK-NEXT: br i1 %stop, label %[[SPLIT_US:.*]], label %[[SPLIT:.*]]
2513 ; CHECK: [[SPLIT_US]]:
2514 ; CHECK-NEXT: br label %[[LOOP2_HEADER_US:.*]]
2516 ; CHECK: [[LOOP2_HEADER_US]]:
2517 ; CHECK-NEXT: %{{.*}} = phi i32 [ %[[I]], %[[SPLIT_US]] ]
2520 ; CHECK-NEXT: br label %[[LOOP2_HEADER:.*]]
2522 ; CHECK: [[LOOP2_HEADER]]:
2523 ; CHECK-NEXT: %{{.*}} = phi i32 [ %[[I]], %[[SPLIT]] ]
2526 %i.inner = phi i32 [ %i, %loop1.body.loop2.ph ], [ %i.next, %loop2.latch ]
2527 br label %loop3.header
2530 %sw = call i32 @h(i32 %i.inner)
2531 switch i32 %sw, label %loop3.exit [
2532 i32 32, label %loop3.header
2533 i32 59, label %loop2.latch
2534 i32 36, label %loop1.latch
2538 %i.next = add i32 %i.inner, 1
2539 br i1 %stop, label %loop2.exit, label %loop2.header
2542 %i.lcssa = phi i32 [ %i.inner, %loop3.header ]
2543 br label %loop1.header
2558 ; Test that when we are unswitching and need to rebuild the loop block set we
2559 ; correctly skip past inner loops. We want to use the inner loop to efficiently
2560 ; skip whole subregions of the outer loop blocks but just because the header of
2561 ; the outer loop is also the preheader of an inner loop shouldn't confuse this
2563 define void @test23(i1 %arg, ptr %ptr) {
2564 ; CHECK-LABEL: define void @test23(
2566 br label %outer.header
2568 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %arg
2569 ; CHECK-NEXT: br i1 %[[FROZEN]],
2571 ; Just verify that we unswitched the correct bits. We should call `@f` twice in
2572 ; one unswitch and `@f` and then `@g` in the other.
2584 br label %inner.header
2588 br label %inner.latch
2591 %inner.cond = load i1, ptr %ptr
2592 br i1 %inner.cond, label %inner.header, label %outer.body
2595 br i1 %arg, label %outer.body.left, label %outer.body.right
2599 br label %outer.latch
2603 br label %outer.latch
2606 %outer.cond = load i1, ptr %ptr
2607 br i1 %outer.cond, label %outer.header, label %exit
2613 ; Non-trivial loop unswitching where there are two invariant conditions, but the
2614 ; second one is only in the cloned copy of the loop after unswitching.
2615 define i32 @test24(ptr %ptr, i1 %cond1, i1 %cond2) {
2616 ; CHECK-LABEL: @test24(
2618 br label %loop_begin
2619 ; CHECK-NEXT: entry:
2620 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
2623 br i1 %cond1, label %loop_a, label %loop_b
2626 br i1 %cond2, label %loop_a_a, label %loop_a_c
2627 ; The second unswitched condition.
2629 ; CHECK: entry.split.us:
2630 ; CHECK-NEXT: br i1 %cond2, label %entry.split.us.split.us, label %entry.split.us.split
2635 ; The 'loop_a_a' unswitched loop.
2637 ; CHECK: entry.split.us.split.us:
2638 ; CHECK-NEXT: br label %loop_begin.us.us
2640 ; CHECK: loop_begin.us.us:
2641 ; CHECK-NEXT: br label %loop_a.us.us
2643 ; CHECK: loop_a.us.us:
2644 ; CHECK-NEXT: br label %loop_a_a.us.us
2646 ; CHECK: loop_a_a.us.us:
2647 ; CHECK-NEXT: call i32 @a()
2648 ; CHECK-NEXT: br label %latch.us.us
2650 ; CHECK: latch.us.us:
2651 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2652 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us.us, label %loop_exit.split.us.split.us
2654 ; CHECK: loop_exit.split.us.split.us:
2655 ; CHECK-NEXT: br label %loop_exit.split
2660 ; The 'loop_a_c' unswitched loop.
2662 ; CHECK: entry.split.us.split:
2663 ; CHECK-NEXT: br label %loop_begin.us
2665 ; CHECK: loop_begin.us:
2666 ; CHECK-NEXT: br label %loop_a.us
2669 ; CHECK-NEXT: br label %loop_a_c.us
2671 ; CHECK: loop_a_c.us:
2672 ; CHECK-NEXT: call i32 @c()
2673 ; CHECK-NEXT: br label %latch
2676 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2677 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us.split
2679 ; CHECK: loop_exit.split.us.split:
2680 ; CHECK-NEXT: br label %loop_exit.split
2685 ; The 'loop_b' unswitched loop.
2687 ; CHECK: entry.split:
2688 ; CHECK-NEXT: br label %loop_begin
2690 ; CHECK: loop_begin:
2691 ; CHECK-NEXT: br label %loop_b
2694 ; CHECK-NEXT: call i32 @b()
2695 ; CHECK-NEXT: br label %latch
2698 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2699 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
2701 ; CHECK: loop_exit.split:
2702 ; CHECK-NEXT: br label %loop_exit
2705 %v = load i1, ptr %ptr
2706 br i1 %v, label %loop_begin, label %loop_exit
2714 ; Non-trivial partial loop unswitching of an invariant input to an 'or'.
2715 define i32 @test25(ptr %ptr, i1 %cond) {
2716 ; CHECK-LABEL: @test25(
2718 br label %loop_begin
2719 ; CHECK-NEXT: entry:
2720 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %cond
2721 ; CHECK-NEXT: br i1 [[FROZEN]], label %entry.split.us, label %entry.split
2724 %v1 = load i1, ptr %ptr
2725 %cond_or = or i1 %v1, %cond
2726 br i1 %cond_or, label %loop_a, label %loop_b
2731 ; The 'loop_a' unswitched loop.
2733 ; CHECK: entry.split.us:
2734 ; CHECK-NEXT: br label %loop_begin.us
2736 ; CHECK: loop_begin.us:
2737 ; CHECK-NEXT: br label %loop_a.us
2740 ; CHECK-NEXT: call i32 @a()
2741 ; CHECK-NEXT: br label %latch.us
2744 ; CHECK-NEXT: %[[V2_US:.*]] = load i1, ptr %ptr
2745 ; CHECK-NEXT: br i1 %[[V2_US]], label %loop_begin.us, label %loop_exit.split.us
2747 ; CHECK: loop_exit.split.us:
2748 ; CHECK-NEXT: br label %loop_exit
2753 ; The original loop.
2755 ; CHECK: entry.split:
2756 ; CHECK-NEXT: br label %loop_begin
2758 ; CHECK: loop_begin:
2759 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr
2760 ; CHECK-NEXT: %[[OR:.*]] = or i1 %[[V1]], false
2761 ; CHECK-NEXT: br i1 %[[OR]], label %loop_a, label %loop_b
2764 ; CHECK-NEXT: call i32 @a()
2765 ; CHECK-NEXT: br label %latch
2768 ; CHECK-NEXT: call i32 @b()
2769 ; CHECK-NEXT: br label %latch
2772 %v2 = load i1, ptr %ptr
2773 br i1 %v2, label %loop_begin, label %loop_exit
2775 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr
2776 ; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.split
2780 ; CHECK: loop_exit.split:
2781 ; CHECK-NEXT: br label %loop_exit
2787 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `and`
2789 define i32 @test26(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2, i1 %cond3) {
2790 ; CHECK-LABEL: @test26(
2792 br label %loop_begin
2793 ; CHECK-NEXT: entry:
2794 ; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2795 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2796 ; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C3_FR]], [[C1_FR]]
2797 ; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
2800 %v1 = load i1, ptr %ptr1
2801 %v2 = load i1, ptr %ptr2
2802 %cond_and1 = and i1 %v1, %cond1
2803 %cond_or1 = or i1 %v2, %cond2
2804 %cond_and2 = and i1 %cond_and1, %cond_or1
2805 %cond_and3 = and i1 %cond_and2, %cond3
2806 br i1 %cond_and3, label %loop_a, label %loop_b
2807 ; The 'loop_b' unswitched loop.
2809 ; CHECK: entry.split.us:
2810 ; CHECK-NEXT: br label %loop_begin.us
2812 ; CHECK: loop_begin.us:
2813 ; CHECK-NEXT: br label %loop_b.us
2816 ; CHECK-NEXT: call i32 @b()
2817 ; CHECK-NEXT: br label %latch.us
2820 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3
2821 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
2823 ; CHECK: loop_exit.split.us:
2824 ; CHECK-NEXT: br label %loop_exit
2826 ; The original loop.
2828 ; CHECK: entry.split:
2829 ; CHECK-NEXT: br label %loop_begin
2831 ; CHECK: loop_begin:
2832 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
2833 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
2834 ; CHECK-NEXT: %[[AND1:.*]] = and i1 %[[V1]], true
2835 ; CHECK-NEXT: %[[OR1:.*]] = or i1 %[[V2]], %cond2
2836 ; CHECK-NEXT: %[[AND2:.*]] = and i1 %[[AND1]], %[[OR1]]
2837 ; CHECK-NEXT: %[[AND3:.*]] = and i1 %[[AND2]], true
2838 ; CHECK-NEXT: br i1 %[[AND3]], label %loop_a, label %loop_b
2844 ; CHECK-NEXT: call i32 @a()
2845 ; CHECK-NEXT: br label %latch
2851 ; CHECK-NEXT: call i32 @b()
2852 ; CHECK-NEXT: br label %latch
2855 %v3 = load i1, ptr %ptr3
2856 br i1 %v3, label %loop_begin, label %loop_exit
2858 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3
2859 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
2863 ; CHECK: loop_exit.split:
2864 ; CHECK-NEXT: br label %loop_exit
2870 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `or`
2871 ; chain. Basically an inverted version of corresponding `and` test (test26).
2872 define i32 @test27(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2, i1 %cond3) {
2873 ; CHECK-LABEL: @test27(
2875 br label %loop_begin
2876 ; CHECK-NEXT: entry:
2877 ; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2878 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2879 ; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C3_FR]], [[C1_FR]]
2880 ; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
2883 %v1 = load i1, ptr %ptr1
2884 %v2 = load i1, ptr %ptr2
2885 %cond_or1 = or i1 %v1, %cond1
2886 %cond_and1 = and i1 %v2, %cond2
2887 %cond_or2 = or i1 %cond_or1, %cond_and1
2888 %cond_or3 = or i1 %cond_or2, %cond3
2889 br i1 %cond_or3, label %loop_b, label %loop_a
2890 ; The 'loop_b' unswitched loop.
2892 ; CHECK: entry.split.us:
2893 ; CHECK-NEXT: br label %loop_begin.us
2895 ; CHECK: loop_begin.us:
2896 ; CHECK-NEXT: br label %loop_b.us
2899 ; CHECK-NEXT: call i32 @b()
2900 ; CHECK-NEXT: br label %latch.us
2903 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3
2904 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
2906 ; CHECK: loop_exit.split.us:
2907 ; CHECK-NEXT: br label %loop_exit
2909 ; The original loop.
2911 ; CHECK: entry.split:
2912 ; CHECK-NEXT: br label %loop_begin
2914 ; CHECK: loop_begin:
2915 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
2916 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
2917 ; CHECK-NEXT: %[[OR1:.*]] = or i1 %[[V1]], false
2918 ; CHECK-NEXT: %[[AND1:.*]] = and i1 %[[V2]], %cond2
2919 ; CHECK-NEXT: %[[OR2:.*]] = or i1 %[[OR1]], %[[AND1]]
2920 ; CHECK-NEXT: %[[OR3:.*]] = or i1 %[[OR2]], false
2921 ; CHECK-NEXT: br i1 %[[OR3]], label %loop_b, label %loop_a
2927 ; CHECK-NEXT: call i32 @a()
2928 ; CHECK-NEXT: br label %latch
2934 ; CHECK-NEXT: call i32 @b()
2935 ; CHECK-NEXT: br label %latch
2938 %v3 = load i1, ptr %ptr3
2939 br i1 %v3, label %loop_begin, label %loop_exit
2941 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3
2942 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
2946 ; CHECK: loop_exit.split:
2947 ; CHECK-NEXT: br label %loop_exit
2953 ; Non-trivial unswitching of a switch.
2954 define i32 @test28(ptr %ptr, i32 %cond) {
2955 ; CHECK-LABEL: @test28(
2957 br label %loop_begin
2958 ; CHECK-NEXT: entry:
2959 ; CHECK-NEXT: switch i32 %cond, label %[[ENTRY_SPLIT_LATCH:.*]] [
2960 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
2961 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_B:.*]]
2962 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_C:.*]]
2966 switch i32 %cond, label %latch [
2967 i32 0, label %loop_a
2968 i32 1, label %loop_b
2969 i32 2, label %loop_c
2975 ; Unswitched 'a' loop.
2977 ; CHECK: [[ENTRY_SPLIT_A]]:
2978 ; CHECK-NEXT: br label %[[LOOP_BEGIN_A:.*]]
2980 ; CHECK: [[LOOP_BEGIN_A]]:
2981 ; CHECK-NEXT: br label %[[LOOP_A:.*]]
2983 ; CHECK: [[LOOP_A]]:
2984 ; CHECK-NEXT: call i32 @a()
2985 ; CHECK-NEXT: br label %[[LOOP_LATCH_A:.*]]
2987 ; CHECK: [[LOOP_LATCH_A]]:
2988 ; CHECK-NEXT: %[[V_A:.*]] = load i1, ptr %ptr
2989 ; CHECK: br i1 %[[V_A]], label %[[LOOP_BEGIN_A]], label %[[LOOP_EXIT_A:.*]]
2991 ; CHECK: [[LOOP_EXIT_A]]:
2992 ; CHECK-NEXT: br label %loop_exit
2997 ; Unswitched 'b' loop.
2999 ; CHECK: [[ENTRY_SPLIT_B]]:
3000 ; CHECK-NEXT: br label %[[LOOP_BEGIN_B:.*]]
3002 ; CHECK: [[LOOP_BEGIN_B]]:
3003 ; CHECK-NEXT: br label %[[LOOP_B:.*]]
3005 ; CHECK: [[LOOP_B]]:
3006 ; CHECK-NEXT: call i32 @b()
3007 ; CHECK-NEXT: br label %[[LOOP_LATCH_B:.*]]
3009 ; CHECK: [[LOOP_LATCH_B]]:
3010 ; CHECK-NEXT: %[[V_B:.*]] = load i1, ptr %ptr
3011 ; CHECK: br i1 %[[V_B]], label %[[LOOP_BEGIN_B]], label %[[LOOP_EXIT_B:.*]]
3013 ; CHECK: [[LOOP_EXIT_B]]:
3014 ; CHECK-NEXT: br label %loop_exit
3019 ; Unswitched 'c' loop.
3021 ; CHECK: [[ENTRY_SPLIT_C]]:
3022 ; CHECK-NEXT: br label %[[LOOP_BEGIN_C:.*]]
3024 ; CHECK: [[LOOP_BEGIN_C]]:
3025 ; CHECK-NEXT: br label %[[LOOP_C:.*]]
3027 ; CHECK: [[LOOP_C]]:
3028 ; CHECK-NEXT: call i32 @c()
3029 ; CHECK-NEXT: br label %[[LOOP_LATCH_C:.*]]
3031 ; CHECK: [[LOOP_LATCH_C]]:
3032 ; CHECK-NEXT: %[[V_C:.*]] = load i1, ptr %ptr
3033 ; CHECK: br i1 %[[V_C]], label %[[LOOP_BEGIN_C]], label %[[LOOP_EXIT_C:.*]]
3035 ; CHECK: [[LOOP_EXIT_C]]:
3036 ; CHECK-NEXT: br label %loop_exit
3039 %v = load i1, ptr %ptr
3040 br i1 %v, label %loop_begin, label %loop_exit
3041 ; Unswitched the 'latch' only loop.
3043 ; CHECK: [[ENTRY_SPLIT_LATCH]]:
3044 ; CHECK-NEXT: br label %[[LOOP_BEGIN_LATCH:.*]]
3046 ; CHECK: [[LOOP_BEGIN_LATCH]]:
3047 ; CHECK-NEXT: br label %[[LOOP_LATCH_LATCH:.*]]
3049 ; CHECK: [[LOOP_LATCH_LATCH]]:
3050 ; CHECK-NEXT: %[[V_LATCH:.*]] = load i1, ptr %ptr
3051 ; CHECK: br i1 %[[V_LATCH]], label %[[LOOP_BEGIN_LATCH]], label %[[LOOP_EXIT_LATCH:.*]]
3053 ; CHECK: [[LOOP_EXIT_LATCH]]:
3054 ; CHECK-NEXT: br label %loop_exit
3059 ; CHECK-NEXT: ret i32 0
3062 ; A test case designed to exercise unusual properties of switches: they
3063 ; can introduce multiple edges to successors. These need lots of special case
3064 ; handling as they get collapsed in many cases (domtree, the unswitch itself)
3065 ; but not in all cases (the PHI node operands).
3066 define i32 @test29(i32 %arg) {
3067 ; CHECK-LABEL: @test29(
3070 ; CHECK-NEXT: entry:
3071 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i32 %arg
3072 ; CHECK-NEXT: switch i32 %[[FROZEN]], label %[[ENTRY_SPLIT_C:.*]] [
3073 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
3074 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_A]]
3075 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_B:.*]]
3076 ; CHECK-NEXT: i32 3, label %[[ENTRY_SPLIT_C]]
3080 %tmp = call i32 @d()
3081 %cmp1 = icmp eq i32 %tmp, 0
3082 ; We set up a chain through all the successors of the switch that doesn't
3083 ; involve the switch so that we can have interesting PHI nodes in them.
3084 br i1 %cmp1, label %body.a, label %dispatch
3087 ; Switch with multiple successors. We arrange the last successor to be the
3088 ; default to make the test case easier to read. This has a duplicate edge
3089 ; both to the default destination (which is completely superfluous but
3090 ; technically valid IR) and to a regular successor.
3091 switch i32 %arg, label %body.c [
3092 i32 0, label %body.a
3093 i32 1, label %body.a
3094 i32 2, label %body.b
3095 i32 3, label %body.c
3099 %tmp.a.phi = phi i32 [ 0, %header ], [ %tmp, %dispatch ], [ %tmp, %dispatch ]
3100 %tmp.a = call i32 @a()
3101 %tmp.a.sum = add i32 %tmp.a.phi, %tmp.a
3103 ; Unswitched 'a' loop.
3105 ; CHECK: [[ENTRY_SPLIT_A]]:
3106 ; CHECK-NEXT: br label %[[HEADER_A:.*]]
3108 ; CHECK: [[HEADER_A]]:
3109 ; CHECK-NEXT: %[[TMP_A:.*]] = call i32 @d()
3110 ; CHECK-NEXT: %[[CMP1_A:.*]] = icmp eq i32 %[[TMP_A]], 0
3111 ; CHECK-NEXT: br i1 %[[CMP1_A]], label %[[BODY_A_A:.*]], label %[[DISPATCH_A:.*]]
3113 ; CHECK: [[DISPATCH_A]]:
3114 ; CHECK-NEXT: br label %[[BODY_A_A]]
3116 ; CHECK: [[BODY_A_A]]:
3117 ; CHECK-NEXT: %[[TMP_A_PHI_A:.*]] = phi i32 [ 0, %[[HEADER_A]] ], [ %[[TMP_A]], %[[DISPATCH_A]] ]
3118 ; CHECK-NEXT: %[[TMP_A_A:.*]] = call i32 @a()
3119 ; CHECK-NEXT: %[[TMP_A_SUM_A:.*]] = add i32 %[[TMP_A_PHI_A]], %[[TMP_A_A]]
3120 ; CHECK-NEXT: br label %[[BODY_B_A:.*]]
3122 ; CHECK: [[BODY_B_A]]:
3123 ; CHECK-NEXT: %[[TMP_B_PHI_A:.*]] = phi i32 [ %[[TMP_A_SUM_A]], %[[BODY_A_A]] ]
3124 ; CHECK-NEXT: %[[TMP_B_A:.*]] = call i32 @b()
3125 ; CHECK-NEXT: %[[TMP_B_SUM_A:.*]] = add i32 %[[TMP_B_PHI_A]], %[[TMP_B_A]]
3126 ; CHECK-NEXT: br label %[[BODY_C_A:.*]]
3128 ; CHECK: [[BODY_C_A]]:
3129 ; CHECK-NEXT: %[[TMP_C_PHI_A:.*]] = phi i32 [ %[[TMP_B_SUM_A]], %[[BODY_B_A]] ]
3130 ; CHECK-NEXT: %[[TMP_C_A:.*]] = call i32 @c()
3131 ; CHECK-NEXT: %[[TMP_C_SUM_A:.*]] = add i32 %[[TMP_C_PHI_A]], %[[TMP_C_A]]
3132 ; CHECK-NEXT: br label %[[LATCH_A:.*]]
3134 ; CHECK: [[LATCH_A]]:
3135 ; CHECK-NEXT: %[[CMP2_A:.*]] = icmp slt i32 %[[TMP_C_SUM_A]], 42
3136 ; CHECK: br i1 %[[CMP2_A]], label %[[HEADER_A]], label %[[LOOP_EXIT_A:.*]]
3138 ; CHECK: [[LOOP_EXIT_A]]:
3139 ; CHECK-NEXT: %[[LCSSA_A:.*]] = phi i32 [ %[[TMP_C_SUM_A]], %[[LATCH_A]] ]
3140 ; CHECK-NEXT: br label %exit
3143 %tmp.b.phi = phi i32 [ %tmp, %dispatch ], [ %tmp.a.sum, %body.a ]
3144 %tmp.b = call i32 @b()
3145 %tmp.b.sum = add i32 %tmp.b.phi, %tmp.b
3147 ; Unswitched 'b' loop.
3149 ; CHECK: [[ENTRY_SPLIT_B]]:
3150 ; CHECK-NEXT: br label %[[HEADER_B:.*]]
3152 ; CHECK: [[HEADER_B]]:
3153 ; CHECK-NEXT: %[[TMP_B:.*]] = call i32 @d()
3154 ; CHECK-NEXT: %[[CMP1_B:.*]] = icmp eq i32 %[[TMP_B]], 0
3155 ; CHECK-NEXT: br i1 %[[CMP1_B]], label %[[BODY_A_B:.*]], label %[[DISPATCH_B:.*]]
3157 ; CHECK: [[DISPATCH_B]]:
3158 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3160 ; CHECK: [[BODY_A_B]]:
3161 ; CHECK-NEXT: %[[TMP_A_PHI_B:.*]] = phi i32 [ 0, %[[HEADER_B]] ]
3162 ; CHECK-NEXT: %[[TMP_A_B:.*]] = call i32 @a()
3163 ; CHECK-NEXT: %[[TMP_A_SUM_B:.*]] = add i32 %[[TMP_A_PHI_B]], %[[TMP_A_B]]
3164 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3166 ; CHECK: [[BODY_B_B]]:
3167 ; CHECK-NEXT: %[[TMP_B_PHI_B:.*]] = phi i32 [ %[[TMP_B]], %[[DISPATCH_B]] ], [ %[[TMP_A_SUM_B]], %[[BODY_A_B]] ]
3168 ; CHECK-NEXT: %[[TMP_B_B:.*]] = call i32 @b()
3169 ; CHECK-NEXT: %[[TMP_B_SUM_B:.*]] = add i32 %[[TMP_B_PHI_B]], %[[TMP_B_B]]
3170 ; CHECK-NEXT: br label %[[BODY_C_B:.*]]
3172 ; CHECK: [[BODY_C_B]]:
3173 ; CHECK-NEXT: %[[TMP_C_PHI_B:.*]] = phi i32 [ %[[TMP_B_SUM_B]], %[[BODY_B_B]] ]
3174 ; CHECK-NEXT: %[[TMP_C_B:.*]] = call i32 @c()
3175 ; CHECK-NEXT: %[[TMP_C_SUM_B:.*]] = add i32 %[[TMP_C_PHI_B]], %[[TMP_C_B]]
3176 ; CHECK-NEXT: br label %[[LATCH_B:.*]]
3178 ; CHECK: [[LATCH_B]]:
3179 ; CHECK-NEXT: %[[CMP2_B:.*]] = icmp slt i32 %[[TMP_C_SUM_B]], 42
3180 ; CHECK: br i1 %[[CMP2_B]], label %[[HEADER_B]], label %[[LOOP_EXIT_B:.*]]
3182 ; CHECK: [[LOOP_EXIT_B]]:
3183 ; CHECK-NEXT: %[[LCSSA_B:.*]] = phi i32 [ %[[TMP_C_SUM_B]], %[[LATCH_B]] ]
3184 ; CHECK-NEXT: br label %[[EXIT_SPLIT:.*]]
3187 %tmp.c.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ], [ %tmp.b.sum, %body.b ]
3188 %tmp.c = call i32 @c()
3189 %tmp.c.sum = add i32 %tmp.c.phi, %tmp.c
3191 ; Unswitched 'c' loop.
3193 ; CHECK: [[ENTRY_SPLIT_C]]:
3194 ; CHECK-NEXT: br label %[[HEADER_C:.*]]
3196 ; CHECK: [[HEADER_C]]:
3197 ; CHECK-NEXT: %[[TMP_C:.*]] = call i32 @d()
3198 ; CHECK-NEXT: %[[CMP1_C:.*]] = icmp eq i32 %[[TMP_C]], 0
3199 ; CHECK-NEXT: br i1 %[[CMP1_C]], label %[[BODY_A_C:.*]], label %[[DISPATCH_C:.*]]
3201 ; CHECK: [[DISPATCH_C]]:
3202 ; CHECK-NEXT: br label %[[BODY_C_C:.*]]
3204 ; CHECK: [[BODY_A_C]]:
3205 ; CHECK-NEXT: %[[TMP_A_PHI_C:.*]] = phi i32 [ 0, %[[HEADER_C]] ]
3206 ; CHECK-NEXT: %[[TMP_A_C:.*]] = call i32 @a()
3207 ; CHECK-NEXT: %[[TMP_A_SUM_C:.*]] = add i32 %[[TMP_A_PHI_C]], %[[TMP_A_C]]
3208 ; CHECK-NEXT: br label %[[BODY_B_C:.*]]
3210 ; CHECK: [[BODY_B_C]]:
3211 ; CHECK-NEXT: %[[TMP_B_PHI_C:.*]] = phi i32 [ %[[TMP_A_SUM_C]], %[[BODY_A_C]] ]
3212 ; CHECK-NEXT: %[[TMP_B_C:.*]] = call i32 @b()
3213 ; CHECK-NEXT: %[[TMP_B_SUM_C:.*]] = add i32 %[[TMP_B_PHI_C]], %[[TMP_B_C]]
3214 ; CHECK-NEXT: br label %[[BODY_C_C:.*]]
3216 ; CHECK: [[BODY_C_C]]:
3217 ; CHECK-NEXT: %[[TMP_C_PHI_C:.*]] = phi i32 [ %[[TMP_C]], %[[DISPATCH_C]] ], [ %[[TMP_B_SUM_C]], %[[BODY_B_C]] ]
3218 ; CHECK-NEXT: %[[TMP_C_C:.*]] = call i32 @c()
3219 ; CHECK-NEXT: %[[TMP_C_SUM_C:.*]] = add i32 %[[TMP_C_PHI_C]], %[[TMP_C_C]]
3220 ; CHECK-NEXT: br label %[[LATCH_C:.*]]
3222 ; CHECK: [[LATCH_C]]:
3223 ; CHECK-NEXT: %[[CMP2_C:.*]] = icmp slt i32 %[[TMP_C_SUM_C]], 42
3224 ; CHECK: br i1 %[[CMP2_C]], label %[[HEADER_C]], label %[[LOOP_EXIT_C:.*]]
3226 ; CHECK: [[LOOP_EXIT_C]]:
3227 ; CHECK-NEXT: %[[LCSSA_C:.*]] = phi i32 [ %[[TMP_C_SUM_C]], %[[LATCH_C]] ]
3228 ; CHECK-NEXT: br label %[[EXIT_SPLIT]]
3231 %cmp2 = icmp slt i32 %tmp.c.sum, 42
3232 br i1 %cmp2, label %header, label %exit
3235 %lcssa.phi = phi i32 [ %tmp.c.sum, %latch ]
3237 ; CHECK: [[EXIT_SPLIT]]:
3238 ; CHECK-NEXT: %[[EXIT_PHI1:.*]] = phi i32 [ %[[LCSSA_C]], %[[LOOP_EXIT_C]] ], [ %[[LCSSA_B]], %[[LOOP_EXIT_B]] ]
3239 ; CHECK-NEXT: br label %exit
3242 ; CHECK-NEXT: %[[EXIT_PHI2:.*]] = phi i32 [ %[[EXIT_PHI1]], %[[EXIT_SPLIT]] ], [ %[[LCSSA_A]], %[[LOOP_EXIT_A]] ]
3243 ; CHECK-NEXT: ret i32 %[[EXIT_PHI2]]
3246 ; Similar to @test29 but designed to have one of the duplicate edges be
3247 ; a loop exit edge as those can in some cases be special. Among other things,
3248 ; this includes an LCSSA phi with multiple entries despite being a dedicated
3250 define i32 @test30(i32 %arg) {
3251 ; CHECK-LABEL: define i32 @test30(
3254 ; CHECK-NEXT: entry:
3255 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i32 %arg
3256 ; CHECK-NEXT: switch i32 %[[FROZEN]], label %[[ENTRY_SPLIT_EXIT:.*]] [
3257 ; CHECK-NEXT: i32 -1, label %[[ENTRY_SPLIT_EXIT]]
3258 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
3259 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_B:.*]]
3260 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_B]]
3264 %tmp = call i32 @d()
3265 %cmp1 = icmp eq i32 %tmp, 0
3266 br i1 %cmp1, label %body.a, label %dispatch
3269 switch i32 %arg, label %loop.exit1 [
3270 i32 -1, label %loop.exit1
3271 i32 0, label %body.a
3272 i32 1, label %body.b
3273 i32 2, label %body.b
3277 %tmp.a.phi = phi i32 [ 0, %header ], [ %tmp, %dispatch ]
3278 %tmp.a = call i32 @a()
3279 %tmp.a.sum = add i32 %tmp.a.phi, %tmp.a
3281 ; Unswitched 'a' loop.
3283 ; CHECK: [[ENTRY_SPLIT_A]]:
3284 ; CHECK-NEXT: br label %[[HEADER_A:.*]]
3286 ; CHECK: [[HEADER_A]]:
3287 ; CHECK-NEXT: %[[TMP_A:.*]] = call i32 @d()
3288 ; CHECK-NEXT: %[[CMP1_A:.*]] = icmp eq i32 %[[TMP_A]], 0
3289 ; CHECK-NEXT: br i1 %[[CMP1_A]], label %[[BODY_A_A:.*]], label %[[DISPATCH_A:.*]]
3291 ; CHECK: [[DISPATCH_A]]:
3292 ; CHECK-NEXT: br label %[[BODY_A_A]]
3294 ; CHECK: [[BODY_A_A]]:
3295 ; CHECK-NEXT: %[[TMP_A_PHI_A:.*]] = phi i32 [ 0, %[[HEADER_A]] ], [ %[[TMP_A]], %[[DISPATCH_A]] ]
3296 ; CHECK-NEXT: %[[TMP_A_A:.*]] = call i32 @a()
3297 ; CHECK-NEXT: %[[TMP_A_SUM_A:.*]] = add i32 %[[TMP_A_PHI_A]], %[[TMP_A_A]]
3298 ; CHECK-NEXT: br label %[[BODY_B_A:.*]]
3300 ; CHECK: [[BODY_B_A]]:
3301 ; CHECK-NEXT: %[[TMP_B_PHI_A:.*]] = phi i32 [ %[[TMP_A_SUM_A]], %[[BODY_A_A]] ]
3302 ; CHECK-NEXT: %[[TMP_B_A:.*]] = call i32 @b()
3303 ; CHECK-NEXT: %[[TMP_B_SUM_A:.*]] = add i32 %[[TMP_B_PHI_A]], %[[TMP_B_A]]
3304 ; CHECK-NEXT: br label %[[LATCH_A:.*]]
3306 ; CHECK: [[LATCH_A]]:
3307 ; CHECK-NEXT: %[[CMP2_A:.*]] = icmp slt i32 %[[TMP_B_SUM_A]], 42
3308 ; CHECK: br i1 %[[CMP2_A]], label %[[HEADER_A]], label %[[LOOP_EXIT_A:.*]]
3310 ; CHECK: [[LOOP_EXIT_A]]:
3311 ; CHECK-NEXT: %[[LCSSA_A:.*]] = phi i32 [ %[[TMP_B_SUM_A]], %[[LATCH_A]] ]
3312 ; CHECK-NEXT: br label %loop.exit2
3315 %tmp.b.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ], [ %tmp.a.sum, %body.a ]
3316 %tmp.b = call i32 @b()
3317 %tmp.b.sum = add i32 %tmp.b.phi, %tmp.b
3319 ; Unswitched 'b' loop.
3321 ; CHECK: [[ENTRY_SPLIT_B]]:
3322 ; CHECK-NEXT: br label %[[HEADER_B:.*]]
3324 ; CHECK: [[HEADER_B]]:
3325 ; CHECK-NEXT: %[[TMP_B:.*]] = call i32 @d()
3326 ; CHECK-NEXT: %[[CMP1_B:.*]] = icmp eq i32 %[[TMP_B]], 0
3327 ; CHECK-NEXT: br i1 %[[CMP1_B]], label %[[BODY_A_B:.*]], label %[[DISPATCH_B:.*]]
3329 ; CHECK: [[DISPATCH_B]]:
3330 ; CHECK-NEXT: br label %[[BODY_B_B]]
3332 ; CHECK: [[BODY_A_B]]:
3333 ; CHECK-NEXT: %[[TMP_A_PHI_B:.*]] = phi i32 [ 0, %[[HEADER_B]] ]
3334 ; CHECK-NEXT: %[[TMP_A_B:.*]] = call i32 @a()
3335 ; CHECK-NEXT: %[[TMP_A_SUM_B:.*]] = add i32 %[[TMP_A_PHI_B]], %[[TMP_A_B]]
3336 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3338 ; CHECK: [[BODY_B_B]]:
3339 ; CHECK-NEXT: %[[TMP_B_PHI_B:.*]] = phi i32 [ %[[TMP_B]], %[[DISPATCH_B]] ], [ %[[TMP_A_SUM_B]], %[[BODY_A_B]] ]
3340 ; CHECK-NEXT: %[[TMP_B_B:.*]] = call i32 @b()
3341 ; CHECK-NEXT: %[[TMP_B_SUM_B:.*]] = add i32 %[[TMP_B_PHI_B]], %[[TMP_B_B]]
3342 ; CHECK-NEXT: br label %[[LATCH_B:.*]]
3344 ; CHECK: [[LATCH_B]]:
3345 ; CHECK-NEXT: %[[CMP2_B:.*]] = icmp slt i32 %[[TMP_B_SUM_B]], 42
3346 ; CHECK: br i1 %[[CMP2_B]], label %[[HEADER_B]], label %[[LOOP_EXIT_B:.*]]
3348 ; CHECK: [[LOOP_EXIT_B]]:
3349 ; CHECK-NEXT: %[[LCSSA_B:.*]] = phi i32 [ %[[TMP_B_SUM_B]], %[[LATCH_B]] ]
3350 ; CHECK-NEXT: br label %[[LOOP_EXIT2_SPLIT:.*]]
3353 %cmp2 = icmp slt i32 %tmp.b.sum, 42
3354 br i1 %cmp2, label %header, label %loop.exit2
3357 %l1.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ]
3359 ; Unswitched 'exit' loop.
3361 ; CHECK: [[ENTRY_SPLIT_EXIT]]:
3362 ; CHECK-NEXT: br label %[[HEADER_EXIT:.*]]
3364 ; CHECK: [[HEADER_EXIT]]:
3365 ; CHECK-NEXT: %[[TMP_EXIT:.*]] = call i32 @d()
3366 ; CHECK-NEXT: %[[CMP1_EXIT:.*]] = icmp eq i32 %[[TMP_EXIT]], 0
3367 ; CHECK-NEXT: br i1 %[[CMP1_EXIT]], label %[[BODY_A_EXIT:.*]], label %[[DISPATCH_EXIT:.*]]
3369 ; CHECK: [[DISPATCH_EXIT]]:
3370 ; CHECK-NEXT: %[[TMP_LCSSA:.*]] = phi i32 [ %[[TMP_EXIT]], %[[HEADER_EXIT]] ]
3371 ; CHECK-NEXT: br label %loop.exit1
3373 ; CHECK: [[BODY_A_EXIT]]:
3374 ; CHECK-NEXT: %[[TMP_A_PHI_EXIT:.*]] = phi i32 [ 0, %[[HEADER_EXIT]] ]
3375 ; CHECK-NEXT: %[[TMP_A_EXIT:.*]] = call i32 @a()
3376 ; CHECK-NEXT: %[[TMP_A_SUM_EXIT:.*]] = add i32 %[[TMP_A_PHI_EXIT]], %[[TMP_A_EXIT]]
3377 ; CHECK-NEXT: br label %[[BODY_B_EXIT:.*]]
3379 ; CHECK: [[BODY_B_EXIT]]:
3380 ; CHECK-NEXT: %[[TMP_B_PHI_EXIT:.*]] = phi i32 [ %[[TMP_A_SUM_EXIT]], %[[BODY_A_EXIT]] ]
3381 ; CHECK-NEXT: %[[TMP_B_EXIT:.*]] = call i32 @b()
3382 ; CHECK-NEXT: %[[TMP_B_SUM_EXIT:.*]] = add i32 %[[TMP_B_PHI_EXIT]], %[[TMP_B_EXIT]]
3383 ; CHECK-NEXT: br label %[[LATCH_EXIT:.*]]
3385 ; CHECK: [[LATCH_EXIT]]:
3386 ; CHECK-NEXT: %[[CMP2_EXIT:.*]] = icmp slt i32 %[[TMP_B_SUM_EXIT]], 42
3387 ; CHECK: br i1 %[[CMP2_EXIT]], label %[[HEADER_EXIT]], label %[[LOOP_EXIT_EXIT:.*]]
3389 ; CHECK: loop.exit1:
3390 ; CHECK-NEXT: %[[L1_PHI:.*]] = phi i32 [ %[[TMP_LCSSA]], %[[DISPATCH_EXIT]] ]
3391 ; CHECK-NEXT: br label %exit
3393 ; CHECK: [[LOOP_EXIT_EXIT]]:
3394 ; CHECK-NEXT: %[[L2_PHI:.*]] = phi i32 [ %[[TMP_B_SUM_EXIT]], %[[LATCH_EXIT]] ]
3395 ; CHECK-NEXT: br label %[[LOOP_EXIT2_SPLIT]]
3398 %l2.phi = phi i32 [ %tmp.b.sum, %latch ]
3400 ; CHECK: [[LOOP_EXIT2_SPLIT]]:
3401 ; CHECK-NEXT: %[[LOOP_EXIT_PHI1:.*]] = phi i32 [ %[[L2_PHI]], %[[LOOP_EXIT_EXIT]] ], [ %[[LCSSA_B]], %[[LOOP_EXIT_B]] ]
3402 ; CHECK-NEXT: br label %loop.exit2
3404 ; CHECK: loop.exit2:
3405 ; CHECK-NEXT: %[[LOOP_EXIT_PHI2:.*]] = phi i32 [ %[[LOOP_EXIT_PHI1]], %[[LOOP_EXIT2_SPLIT]] ], [ %[[LCSSA_A]], %[[LOOP_EXIT_A]] ]
3406 ; CHECK-NEXT: br label %exit
3409 %l.phi = phi i32 [ %l1.phi, %loop.exit1 ], [ %l2.phi, %loop.exit2 ]
3412 ; CHECK-NEXT: %[[EXIT_PHI:.*]] = phi i32 [ %[[L1_PHI]], %loop.exit1 ], [ %[[LOOP_EXIT_PHI2]], %loop.exit2 ]
3413 ; CHECK-NEXT: ret i32 %[[EXIT_PHI]]
3416 ; Unswitch will not actually change the loop nest from:
3418 define void @hoist_inner_loop0() {
3419 ; CHECK-LABEL: define void @hoist_inner_loop0(
3423 ; CHECK-NEXT: br label %a.header
3428 ; CHECK-NEXT: br label %b.header
3431 %v1 = call i1 @cond()
3434 ; CHECK-NEXT: %v1 = call i1 @cond()
3435 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3436 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3438 ; CHECK: [[B_HEADER_SPLIT_US]]:
3439 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3441 ; CHECK: [[C_HEADER_US]]:
3442 ; CHECK-NEXT: call i32 @c()
3443 ; CHECK-NEXT: br label %[[B_LATCH_SPLIT_US:.*]]
3445 ; CHECK: [[B_LATCH_SPLIT_US]]:
3446 ; CHECK-NEXT: br label %b.latch
3448 ; CHECK: [[B_HEADER_SPLIT]]:
3449 ; CHECK-NEXT: br label %c.header
3453 br i1 %v1, label %b.latch, label %c.latch
3455 ; CHECK-NEXT: call i32 @c()
3456 ; CHECK-NEXT: br label %c.latch
3459 %v2 = call i1 @cond()
3460 br i1 %v2, label %c.header, label %b.latch
3462 ; CHECK-NEXT: %v2 = call i1 @cond()
3463 ; CHECK-NEXT: br i1 %v2, label %c.header, label %[[B_LATCH_SPLIT:.*]]
3466 %v3 = call i1 @cond()
3467 br i1 %v3, label %b.header, label %a.latch
3468 ; CHECK: [[B_LATCH_SPLIT]]:
3469 ; CHECK-NEXT: br label %b.latch
3472 ; CHECK-NEXT: %v3 = call i1 @cond()
3473 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
3478 ; CHECK-NEXT: br label %a.header
3483 ; CHECK-NEXT: ret void
3486 ; Unswitch will transform the loop nest from:
3490 define void @hoist_inner_loop1(ptr %ptr) {
3491 ; CHECK-LABEL: define void @hoist_inner_loop1(
3495 ; CHECK-NEXT: br label %a.header
3498 %x.a = load i32, ptr %ptr
3501 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3502 ; CHECK-NEXT: br label %b.header
3505 %x.b = load i32, ptr %ptr
3506 %v1 = call i1 @cond()
3509 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3510 ; CHECK-NEXT: %v1 = call i1 @cond()
3511 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3512 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3514 ; CHECK: [[B_HEADER_SPLIT_US]]:
3515 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3517 ; CHECK: [[C_HEADER_US]]:
3518 ; CHECK-NEXT: call i32 @c()
3519 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3521 ; CHECK: [[B_LATCH_US]]:
3522 ; CHECK-NEXT: br label %b.latch
3524 ; CHECK: [[B_HEADER_SPLIT]]:
3525 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3526 ; CHECK-NEXT: br label %c.header
3530 br i1 %v1, label %b.latch, label %c.latch
3532 ; CHECK-NEXT: call i32 @c()
3533 ; CHECK-NEXT: br label %c.latch
3536 ; Use values from other loops to check LCSSA form.
3537 store i32 %x.a, ptr %ptr
3538 store i32 %x.b, ptr %ptr
3539 %v2 = call i1 @cond()
3540 br i1 %v2, label %c.header, label %a.exit.c
3542 ; CHECK-NEXT: store i32 %x.a, ptr %ptr
3543 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3544 ; CHECK-NEXT: %v2 = call i1 @cond()
3545 ; CHECK-NEXT: br i1 %v2, label %c.header, label %a.exit.c
3548 %v3 = call i1 @cond()
3549 br i1 %v3, label %b.header, label %a.exit.b
3551 ; CHECK-NEXT: %v3 = call i1 @cond()
3552 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.exit.b
3557 ; CHECK-NEXT: br label %a.latch
3562 ; CHECK-NEXT: br label %a.latch
3567 ; CHECK-NEXT: br label %a.header
3572 ; CHECK-NEXT: ret void
3575 ; Unswitch will transform the loop nest from:
3579 define void @hoist_inner_loop2(ptr %ptr) {
3580 ; CHECK-LABEL: define void @hoist_inner_loop2(
3584 ; CHECK-NEXT: br label %a.header
3587 %x.a = load i32, ptr %ptr
3590 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3591 ; CHECK-NEXT: br label %b.header
3594 %x.b = load i32, ptr %ptr
3595 %v1 = call i1 @cond()
3598 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3599 ; CHECK-NEXT: %v1 = call i1 @cond()
3600 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3601 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3603 ; CHECK: [[B_HEADER_SPLIT_US]]:
3604 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3606 ; CHECK: [[C_HEADER_US]]:
3607 ; CHECK-NEXT: call i32 @c()
3608 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3610 ; CHECK: [[B_LATCH_US]]:
3611 ; CHECK-NEXT: br label %b.latch
3613 ; CHECK: [[B_HEADER_SPLIT]]:
3614 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3615 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3616 ; CHECK-NEXT: br label %c.header
3620 br i1 %v1, label %b.latch, label %c.latch
3622 ; CHECK-NEXT: call i32 @c()
3623 ; CHECK-NEXT: br label %c.latch
3626 ; Use values from other loops to check LCSSA form.
3627 store i32 %x.a, ptr %ptr
3628 store i32 %x.b, ptr %ptr
3629 %v2 = call i1 @cond()
3630 br i1 %v2, label %c.header, label %exit
3632 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
3633 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3634 ; CHECK-NEXT: %v2 = call i1 @cond()
3635 ; CHECK-NEXT: br i1 %v2, label %c.header, label %exit
3638 %v3 = call i1 @cond()
3639 br i1 %v3, label %b.header, label %a.latch
3641 ; CHECK-NEXT: %v3 = call i1 @cond()
3642 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
3647 ; CHECK-NEXT: br label %a.header
3652 ; CHECK-NEXT: ret void
3655 ; Same as @hoist_inner_loop2 but with a nested loop inside the hoisted loop.
3656 ; Unswitch will transform the loop nest from:
3660 define void @hoist_inner_loop3(ptr %ptr) {
3661 ; CHECK-LABEL: define void @hoist_inner_loop3(
3665 ; CHECK-NEXT: br label %a.header
3668 %x.a = load i32, ptr %ptr
3671 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3672 ; CHECK-NEXT: br label %b.header
3675 %x.b = load i32, ptr %ptr
3676 %v1 = call i1 @cond()
3679 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3680 ; CHECK-NEXT: %v1 = call i1 @cond()
3681 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3682 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3684 ; CHECK: [[B_HEADER_SPLIT_US]]:
3685 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3687 ; CHECK: [[C_HEADER_US]]:
3688 ; CHECK-NEXT: call i32 @c()
3689 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3691 ; CHECK: [[B_LATCH_US]]:
3692 ; CHECK-NEXT: br label %b.latch
3694 ; CHECK: [[B_HEADER_SPLIT]]:
3695 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3696 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3697 ; CHECK-NEXT: br label %c.header
3701 br i1 %v1, label %b.latch, label %c.body
3703 ; CHECK-NEXT: call i32 @c()
3704 ; CHECK-NEXT: br label %c.body
3707 %x.c = load i32, ptr %ptr
3710 ; CHECK-NEXT: %x.c = load i32, ptr %ptr
3711 ; CHECK-NEXT: br label %d.header
3714 ; Use values from other loops to check LCSSA form.
3715 store i32 %x.a, ptr %ptr
3716 store i32 %x.b, ptr %ptr
3717 store i32 %x.c, ptr %ptr
3718 %v2 = call i1 @cond()
3719 br i1 %v2, label %d.header, label %c.latch
3721 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
3722 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3723 ; CHECK-NEXT: store i32 %x.c, ptr %ptr
3724 ; CHECK-NEXT: %v2 = call i1 @cond()
3725 ; CHECK-NEXT: br i1 %v2, label %d.header, label %c.latch
3728 %v3 = call i1 @cond()
3729 br i1 %v3, label %c.header, label %exit
3731 ; CHECK-NEXT: %v3 = call i1 @cond()
3732 ; CHECK-NEXT: br i1 %v3, label %c.header, label %exit
3735 %v4 = call i1 @cond()
3736 br i1 %v4, label %b.header, label %a.latch
3738 ; CHECK-NEXT: %v4 = call i1 @cond()
3739 ; CHECK-NEXT: br i1 %v4, label %b.header, label %a.latch
3744 ; CHECK-NEXT: br label %a.header
3749 ; CHECK-NEXT: ret void
3752 ; This test is designed to exercise checking multiple remaining exits from the
3753 ; loop being unswitched.
3754 ; Unswitch will transform the loop nest from:
3758 define void @hoist_inner_loop4() {
3759 ; CHECK-LABEL: define void @hoist_inner_loop4(
3763 ; CHECK-NEXT: br label %a.header
3768 ; CHECK-NEXT: br label %b.header
3773 ; CHECK-NEXT: br label %c.header
3776 %v1 = call i1 @cond()
3779 ; CHECK-NEXT: %v1 = call i1 @cond()
3780 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %v1
3781 ; CHECK-NEXT: br i1 [[FROZEN]], label %[[C_HEADER_SPLIT_US:.*]], label %[[C_HEADER_SPLIT:.*]]
3783 ; CHECK: [[C_HEADER_SPLIT_US]]:
3784 ; CHECK-NEXT: br label %[[D_HEADER_US:.*]]
3786 ; CHECK: [[D_HEADER_US]]:
3787 ; CHECK-NEXT: call i32 @d()
3788 ; CHECK-NEXT: br label %[[C_LATCH_US:.*]]
3790 ; CHECK: [[C_LATCH_US]]:
3791 ; CHECK-NEXT: br label %c.latch
3793 ; CHECK: [[C_HEADER_SPLIT]]:
3794 ; CHECK-NEXT: br label %d.header
3798 br i1 %v1, label %c.latch, label %d.exiting1
3800 ; CHECK-NEXT: call i32 @d()
3801 ; CHECK-NEXT: br label %d.exiting1
3804 %v2 = call i1 @cond()
3805 br i1 %v2, label %d.exiting2, label %a.latch
3806 ; CHECK: d.exiting1:
3807 ; CHECK-NEXT: %v2 = call i1 @cond()
3808 ; CHECK-NEXT: br i1 %v2, label %d.exiting2, label %a.latch
3811 %v3 = call i1 @cond()
3812 br i1 %v3, label %d.exiting3, label %loopexit.d
3813 ; CHECK: d.exiting2:
3814 ; CHECK-NEXT: %v3 = call i1 @cond()
3815 ; CHECK-NEXT: br i1 %v3, label %d.exiting3, label %loopexit.d
3818 %v4 = call i1 @cond()
3819 br i1 %v4, label %d.latch, label %b.latch
3820 ; CHECK: d.exiting3:
3821 ; CHECK-NEXT: %v4 = call i1 @cond()
3822 ; CHECK-NEXT: br i1 %v4, label %d.latch, label %b.latch
3827 ; CHECK-NEXT: br label %d.header
3830 %v5 = call i1 @cond()
3831 br i1 %v5, label %c.header, label %loopexit.c
3833 ; CHECK-NEXT: %v5 = call i1 @cond()
3834 ; CHECK-NEXT: br i1 %v5, label %c.header, label %loopexit.c
3839 ; CHECK-NEXT: br label %b.header
3844 ; CHECK-NEXT: br label %a.header
3848 ; CHECK: loopexit.d:
3849 ; CHECK-NEXT: br label %exit
3853 ; CHECK: loopexit.c:
3854 ; CHECK-NEXT: br label %exit
3859 ; CHECK-NEXT: ret void
3862 ; Unswitch will transform the loop nest from:
3866 define void @hoist_inner_loop5(ptr %ptr) {
3867 ; CHECK-LABEL: define void @hoist_inner_loop5(
3871 ; CHECK-NEXT: br label %a.header
3874 %x.a = load i32, ptr %ptr
3877 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3878 ; CHECK-NEXT: br label %b.header
3881 %x.b = load i32, ptr %ptr
3884 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3885 ; CHECK-NEXT: br label %c.header
3888 %x.c = load i32, ptr %ptr
3889 %v1 = call i1 @cond()
3892 ; CHECK-NEXT: %x.c = load i32, ptr %ptr
3893 ; CHECK-NEXT: %v1 = call i1 @cond()
3894 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %v1
3895 ; CHECK-NEXT: br i1 [[FROZEN]], label %[[C_HEADER_SPLIT_US:.*]], label %[[C_HEADER_SPLIT:.*]]
3897 ; CHECK: [[C_HEADER_SPLIT_US]]:
3898 ; CHECK-NEXT: br label %[[D_HEADER_US:.*]]
3900 ; CHECK: [[D_HEADER_US]]:
3901 ; CHECK-NEXT: call i32 @d()
3902 ; CHECK-NEXT: br label %[[C_LATCH_US:.*]]
3904 ; CHECK: [[C_LATCH_US]]:
3905 ; CHECK-NEXT: br label %c.latch
3907 ; CHECK: [[C_HEADER_SPLIT]]:
3908 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %c.header ]
3909 ; CHECK-NEXT: %[[X_C_LCSSA:.*]] = phi i32 [ %x.c, %c.header ]
3910 ; CHECK-NEXT: br label %d.header
3914 br i1 %v1, label %c.latch, label %d.latch
3916 ; CHECK-NEXT: call i32 @d()
3917 ; CHECK-NEXT: br label %d.latch
3920 ; Use values from other loops to check LCSSA form.
3921 store i32 %x.a, ptr %ptr
3922 store i32 %x.b, ptr %ptr
3923 store i32 %x.c, ptr %ptr
3924 %v2 = call i1 @cond()
3925 br i1 %v2, label %d.header, label %a.latch
3927 ; CHECK-NEXT: store i32 %x.a, ptr %ptr
3928 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3929 ; CHECK-NEXT: store i32 %[[X_C_LCSSA]], ptr %ptr
3930 ; CHECK-NEXT: %v2 = call i1 @cond()
3931 ; CHECK-NEXT: br i1 %v2, label %d.header, label %a.latch
3934 %v3 = call i1 @cond()
3935 br i1 %v3, label %c.header, label %b.latch
3937 ; CHECK-NEXT: %v3 = call i1 @cond()
3938 ; CHECK-NEXT: br i1 %v3, label %c.header, label %b.latch
3943 ; CHECK-NEXT: br label %b.header
3948 ; CHECK-NEXT: br label %a.header
3953 ; CHECK-NEXT: ret void
3956 define void @hoist_inner_loop_switch(ptr %ptr) {
3957 ; CHECK-LABEL: define void @hoist_inner_loop_switch(
3961 ; CHECK-NEXT: br label %a.header
3964 %x.a = load i32, ptr %ptr
3967 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3968 ; CHECK-NEXT: br label %b.header
3971 %x.b = load i32, ptr %ptr
3972 %v1 = call i32 @cond.i32()
3975 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3976 ; CHECK-NEXT: %v1 = call i32 @cond.i32()
3977 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i32 %v1
3978 ; CHECK-NEXT: switch i32 [[FROZEN]], label %[[B_HEADER_SPLIT:.*]] [
3979 ; CHECK-NEXT: i32 1, label %[[B_HEADER_SPLIT_US:.*]]
3980 ; CHECK-NEXT: i32 2, label %[[B_HEADER_SPLIT_US]]
3981 ; CHECK-NEXT: i32 3, label %[[B_HEADER_SPLIT_US]]
3984 ; CHECK: [[B_HEADER_SPLIT_US]]:
3985 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3987 ; CHECK: [[C_HEADER_US]]:
3988 ; CHECK-NEXT: call i32 @c()
3989 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3991 ; CHECK: [[B_LATCH_US]]:
3992 ; CHECK-NEXT: br label %b.latch
3994 ; CHECK: [[B_HEADER_SPLIT]]:
3995 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3996 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3997 ; CHECK-NEXT: br label %c.header
4001 switch i32 %v1, label %c.latch [
4002 i32 1, label %b.latch
4003 i32 2, label %b.latch
4004 i32 3, label %b.latch
4007 ; CHECK-NEXT: call i32 @c()
4008 ; CHECK-NEXT: br label %c.latch
4011 ; Use values from other loops to check LCSSA form.
4012 store i32 %x.a, ptr %ptr
4013 store i32 %x.b, ptr %ptr
4014 %v2 = call i1 @cond()
4015 br i1 %v2, label %c.header, label %exit
4017 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
4018 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
4019 ; CHECK-NEXT: %v2 = call i1 @cond()
4020 ; CHECK-NEXT: br i1 %v2, label %c.header, label %exit
4023 %v3 = call i1 @cond()
4024 br i1 %v3, label %b.header, label %a.latch
4026 ; CHECK-NEXT: %v3 = call i1 @cond()
4027 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
4032 ; CHECK-NEXT: br label %a.header
4037 ; CHECK-NEXT: ret void
4040 ; A devilish pattern. This is a crafty, crafty test case designed to risk
4041 ; creating indirect cycles with trivial and non-trivial unswitching. The inner
4042 ; loop has a switch with a trivial exit edge that can be unswitched, but the
4043 ; rest of the switch cannot be unswitched because its cost is too high.
4044 ; However, the unswitching of the trivial edge creates a new switch in the
4045 ; outer loop. *This* switch isn't trivial, but has a low cost to unswitch. When
4046 ; we unswitch this switch from the outer loop, we will remove it completely and
4047 ; create a clone of the inner loop on one side. This clone will then again be
4048 ; viable for unswitching the inner-most loop. This lets us check that the
4049 ; unswitching doesn't end up cycling infinitely even when the cycle is
4050 ; indirect and due to revisiting a loop after cloning.
4051 define void @test31(i32 %arg) {
4052 ; CHECK-LABEL: define void @test31(
4054 br label %outer.header
4055 ; CHECK-NEXT: entry:
4056 ; CHECK-NEXT: switch i32 %arg, label %[[ENTRY_SPLIT:.*]] [
4057 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_US:.*]]
4058 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_US]]
4061 ; CHECK: [[ENTRY_SPLIT_US]]:
4062 ; CHECK-NEXT: switch i32 %arg, label %[[ENTRY_SPLIT_US_SPLIT:.*]] [
4063 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_US_SPLIT_US:.*]]
4067 br label %inner.header
4070 switch i32 %arg, label %inner.loopexit1 [
4071 i32 1, label %inner.body1
4072 i32 2, label %inner.body2
4077 br label %inner.latch
4078 ; The (super convoluted) fully unswitched loop around `@a`.
4080 ; CHECK: [[ENTRY_SPLIT_US_SPLIT_US]]:
4081 ; CHECK-NEXT: br label %[[OUTER_HEADER_US_US:.*]]
4083 ; CHECK: [[OUTER_HEADER_US_US]]:
4084 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_US_US:.*]]
4086 ; CHECK: [[OUTER_LATCH_US_US:.*]]:
4087 ; CHECK-NEXT: %[[OUTER_COND_US_US:.*]] = call i1 @cond()
4088 ; CHECK-NEXT: br i1 %[[OUTER_COND_US_US]], label %[[OUTER_HEADER_US_US]], label %[[EXIT_SPLIT_US_SPLIT_US:.*]]
4090 ; CHECK: [[OUTER_HEADER_SPLIT_US_US]]:
4091 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_SPLIT_US_US_US:.*]]
4093 ; CHECK: [[INNER_LOOPEXIT2_US_US:.*]]:
4094 ; CHECK-NEXT: br label %[[OUTER_LATCH_US_US]]
4096 ; CHECK: [[OUTER_HEADER_SPLIT_SPLIT_US_US_US]]:
4097 ; CHECK-NEXT: br label %[[INNER_HEADER_US_US_US:.*]]
4099 ; CHECK: [[INNER_HEADER_US_US_US]]:
4100 ; CHECK-NEXT: br label %[[INNER_BODY1_US_US_US:.*]]
4102 ; CHECK: [[INNER_BODY1_US_US_US]]:
4103 ; CHECK-NEXT: %[[A:.*]] = call i32 @a()
4104 ; CHECK-NEXT: br label %[[INNER_LATCH_US_US_US:.*]]
4106 ; CHECK: [[INNER_LATCH_US_US_US]]:
4107 ; CHECK-NEXT: %[[PHI_A:.*]] = phi i32 [ %[[A]], %[[INNER_BODY1_US_US_US]] ]
4108 ; CHECK-NEXT: call void @sink1(i32 0)
4109 ; CHECK-NEXT: call void @sink1(i32 0)
4110 ; CHECK-NEXT: call void @sink1(i32 0)
4111 ; CHECK-NEXT: call void @sink1(i32 0)
4112 ; CHECK-NEXT: call void @sink1(i32 0)
4113 ; CHECK-NEXT: call void @sink1(i32 0)
4114 ; CHECK-NEXT: call void @sink1(i32 0)
4115 ; CHECK-NEXT: call void @sink1(i32 0)
4116 ; CHECK-NEXT: call void @sink1(i32 0)
4117 ; CHECK-NEXT: call void @sink1(i32 0)
4118 ; CHECK-NEXT: call void @sink1(i32 %[[PHI_A]])
4119 ; CHECK-NEXT: %[[INNER_COND_US_US_US:.*]] = call i1 @cond()
4120 ; CHECK-NEXT: br i1 %[[INNER_COND_US_US_US]], label %[[INNER_HEADER_US_US_US]], label %[[INNER_LOOPEXIT2_SPLIT_US_US_US:.*]]
4122 ; CHECK: [[INNER_LOOPEXIT2_SPLIT_US_US_US]]:
4123 ; CHECK-NEXT: br label %[[INNER_LOOPEXIT2_US_US]]
4125 ; CHECK: [[EXIT_SPLIT_US_SPLIT_US]]:
4126 ; CHECK-NEXT: br label %[[EXIT_SPLIT_US:.*]]
4131 br label %inner.latch
4132 ; The fully unswitched loop around `@b`.
4134 ; CHECK: [[ENTRY_SPLIT_US_SPLIT]]:
4135 ; CHECK-NEXT: br label %[[OUTER_HEADER_US:.*]]
4137 ; CHECK: [[OUTER_HEADER_US]]:
4138 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_US:.*]]
4140 ; CHECK: [[INNER_HEADER_US:.*]]:
4141 ; CHECK-NEXT: br label %[[INNER_BODY2_US:.*]]
4143 ; CHECK: [[INNER_BODY2_US]]:
4144 ; CHECK-NEXT: %[[B:.*]] = call i32 @b()
4145 ; CHECK-NEXT: br label %[[INNER_LATCH_US:.*]]
4147 ; CHECK: [[INNER_LATCH_US]]:
4148 ; CHECK-NEXT: call void @sink1(i32 0)
4149 ; CHECK-NEXT: call void @sink1(i32 0)
4150 ; CHECK-NEXT: call void @sink1(i32 0)
4151 ; CHECK-NEXT: call void @sink1(i32 0)
4152 ; CHECK-NEXT: call void @sink1(i32 0)
4153 ; CHECK-NEXT: call void @sink1(i32 0)
4154 ; CHECK-NEXT: call void @sink1(i32 0)
4155 ; CHECK-NEXT: call void @sink1(i32 0)
4156 ; CHECK-NEXT: call void @sink1(i32 0)
4157 ; CHECK-NEXT: call void @sink1(i32 0)
4158 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
4159 ; CHECK-NEXT: %[[INNER_COND_US:.*]] = call i1 @cond()
4160 ; CHECK-NEXT: br i1 %[[INNER_COND_US]], label %[[INNER_HEADER_US]], label %[[INNER_LOOPEXIT2_SPLIT_US:.*]]
4162 ; CHECK: [[INNER_LOOPEXIT2_SPLIT_US]]:
4163 ; CHECK-NEXT: br label %[[INNER_LOOPEXIT2_US:.*]]
4165 ; CHECK: [[OUTER_LATCH_US:.*]]:
4166 ; CHECK-NEXT: %[[OUTER_COND_US:.*]] = call i1 @cond()
4167 ; CHECK-NEXT: br i1 %[[OUTER_COND_US]], label %[[OUTER_HEADER_US]], label %[[EXIT_SPLIT_US_SPLIT:.*]]
4169 ; CHECK: [[OUTER_HEADER_SPLIT_US]]:
4170 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_SPLIT_US:.*]]
4172 ; CHECK: [[OUTER_HEADER_SPLIT_SPLIT_US]]:
4173 ; CHECK-NEXT: br label %[[INNER_HEADER_US]]
4175 ; CHECK: [[INNER_LOOPEXIT2_US]]:
4176 ; CHECK-NEXT: br label %[[OUTER_LATCH_US]]
4178 ; CHECK: [[EXIT_SPLIT_US]]:
4179 ; CHECK-NEXT: br label %exit
4182 %phi = phi i32 [ %a, %inner.body1 ], [ %b, %inner.body2 ]
4183 ; Make 10 junk calls here to ensure we're over the "50" cost threshold of
4184 ; non-trivial unswitching for this inner switch.
4185 call void @sink1(i32 0)
4186 call void @sink1(i32 0)
4187 call void @sink1(i32 0)
4188 call void @sink1(i32 0)
4189 call void @sink1(i32 0)
4190 call void @sink1(i32 0)
4191 call void @sink1(i32 0)
4192 call void @sink1(i32 0)
4193 call void @sink1(i32 0)
4194 call void @sink1(i32 0)
4195 call void @sink1(i32 %phi)
4196 %inner.cond = call i1 @cond()
4197 br i1 %inner.cond, label %inner.header, label %inner.loopexit2
4200 br label %outer.latch
4201 ; The unswitched `loopexit1` path.
4203 ; CHECK: [[ENTRY_SPLIT]]:
4204 ; CHECK-NEXT: br label %[[OUTER_HEADER:.*]]
4206 ; CHECK: outer.header:
4207 ; CHECK-NEXT: br label %inner.loopexit1
4209 ; CHECK: inner.loopexit1:
4210 ; CHECK-NEXT: br label %outer.latch
4212 ; CHECK: outer.latch:
4213 ; CHECK-NEXT: %outer.cond = call i1 @cond()
4214 ; CHECK-NEXT: br i1 %outer.cond, label %outer.header, label %[[EXIT_SPLIT:.*]]
4216 ; CHECK: [[EXIT_SPLIT]]:
4217 ; CHECK-NEXT: br label %exit
4220 br label %outer.latch
4223 %outer.cond = call i1 @cond()
4224 br i1 %outer.cond, label %outer.header, label %exit
4229 ; CHECK-NEXT: ret void
4232 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `and`
4233 ; chain (select version).
4234 define i32 @test32(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2) {
4235 ; CHECK-LABEL: @test32(
4237 br label %loop_begin
4238 ; CHECK-NEXT: entry:
4239 ; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4240 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4241 ; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C2_FR]], [[C1_FR]]
4242 ; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
4245 %v1 = load i1, ptr %ptr1
4246 %v2 = load i1, ptr %ptr2
4247 %cond_and1 = select i1 %v1, i1 %cond1, i1 false
4248 %cond_and2 = select i1 %cond_and1, i1 %cond2, i1 false
4249 br i1 %cond_and2, label %loop_a, label %loop_b
4250 ; The 'loop_b' unswitched loop.
4252 ; CHECK: entry.split.us:
4253 ; CHECK-NEXT: br label %loop_begin.us
4255 ; CHECK: loop_begin.us:
4256 ; CHECK-NEXT: %[[V2_US]] = load i1, ptr %ptr2, align 1
4257 ; CHECK-NEXT: br label %loop_b.us
4260 ; CHECK-NEXT: call i32 @b()
4261 ; CHECK-NEXT: br label %latch.us
4264 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3, align 1
4265 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
4267 ; CHECK: loop_exit.split.us:
4268 ; CHECK-NEXT: br label %loop_exit
4270 ; The original loop.
4272 ; CHECK: entry.split:
4273 ; CHECK-NEXT: br label %loop_begin
4275 ; CHECK: loop_begin:
4276 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
4277 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
4278 ; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
4279 ; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
4280 ; CHECK-NEXT: br i1 %[[AND2]], label %loop_a, label %loop_b
4286 ; CHECK-NEXT: call i32 @a()
4287 ; CHECK-NEXT: br label %latch
4293 ; CHECK-NEXT: call i32 @b()
4294 ; CHECK-NEXT: br label %latch
4297 %v3 = load i1, ptr %ptr3
4298 br i1 %v3, label %loop_begin, label %loop_exit
4300 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3, align 1
4301 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
4305 ; CHECK: loop_exit.split:
4306 ; CHECK-NEXT: br label %loop_exit
4312 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `or`
4313 ; chain (select version).
4314 define i32 @test33(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2) {
4315 ; CHECK-LABEL: @test33(
4317 br label %loop_begin
4318 ; CHECK-NEXT: entry:
4319 ; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4320 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4321 ; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C2_FR]], [[C1_FR]]
4322 ; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
4325 %v1 = load i1, ptr %ptr1
4326 %v2 = load i1, ptr %ptr2
4327 %cond_and1 = select i1 %v1, i1 true, i1 %cond1
4328 %cond_and2 = select i1 %cond_and1, i1 true, i1 %cond2
4329 br i1 %cond_and2, label %loop_b, label %loop_a
4330 ; The 'loop_b' unswitched loop.
4332 ; CHECK: entry.split.us:
4333 ; CHECK-NEXT: br label %loop_begin.us
4335 ; CHECK: loop_begin.us:
4336 ; CHECK-NEXT: %[[V2_US]] = load i1, ptr %ptr2, align 1
4337 ; CHECK-NEXT: br label %loop_b.us
4340 ; CHECK-NEXT: call i32 @b()
4341 ; CHECK-NEXT: br label %latch.us
4344 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3, align 1
4345 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
4347 ; CHECK: loop_exit.split.us:
4348 ; CHECK-NEXT: br label %loop_exit
4350 ; The original loop.
4352 ; CHECK: entry.split:
4353 ; CHECK-NEXT: br label %loop_begin
4355 ; CHECK: loop_begin:
4356 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
4357 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
4358 ; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
4359 ; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
4360 ; CHECK-NEXT: br i1 %[[AND2]], label %loop_b, label %loop_a
4366 ; CHECK-NEXT: call i32 @a()
4367 ; CHECK-NEXT: br label %latch
4373 ; CHECK-NEXT: call i32 @b()
4374 ; CHECK-NEXT: br label %latch
4377 %v3 = load i1, ptr %ptr3
4378 br i1 %v3, label %loop_begin, label %loop_exit
4380 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3, align 1
4381 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
4385 ; CHECK: loop_exit.split:
4386 ; CHECK-NEXT: br label %loop_exit