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 -verify-memoryssa < %s | FileCheck %s
9 declare void @sink1(i32)
10 declare void @sink2(i32)
13 declare i32 @cond.i32()
15 ; Negative test: we cannot unswitch convergent calls.
16 define void @test_no_unswitch_convergent(ptr %ptr, i1 %cond) {
17 ; CHECK-LABEL: @test_no_unswitch_convergent(
21 ; CHECK-NEXT: br label %loop_begin
23 ; We shouldn't have unswitched into any other block either.
24 ; CHECK-NOT: br i1 %cond
27 br i1 %cond, label %loop_a, label %loop_b
29 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
32 call i32 @a() convergent
40 %v = load i1, ptr %ptr
41 br i1 %v, label %loop_begin, label %loop_exit
47 ; Negative test: we cannot unswitch noduplicate calls.
48 define void @test_no_unswitch_noduplicate(ptr %ptr, i1 %cond) {
49 ; CHECK-LABEL: @test_no_unswitch_noduplicate(
53 ; CHECK-NEXT: br label %loop_begin
55 ; We shouldn't have unswitched into any other block either.
56 ; CHECK-NOT: br i1 %cond
59 br i1 %cond, label %loop_a, label %loop_b
61 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
64 call i32 @a() noduplicate
72 %v = load i1, ptr %ptr
73 br i1 %v, label %loop_begin, label %loop_exit
79 declare i32 @__CxxFrameHandler3(...)
81 ; Negative test: we cannot unswitch when tokens are used across blocks as we
82 ; might introduce PHIs.
83 define void @test_no_unswitch_cross_block_token(ptr %ptr, i1 %cond) nounwind personality ptr @__CxxFrameHandler3 {
84 ; CHECK-LABEL: @test_no_unswitch_cross_block_token(
88 ; CHECK-NEXT: br label %loop_begin
90 ; We shouldn't have unswitched into any other block either.
91 ; CHECK-NOT: br i1 %cond
94 br i1 %cond, label %loop_a, label %loop_b
96 ; CHECK-NEXT: br i1 %cond, label %loop_a, label %loop_b
108 to label %loop_latch unwind label %loop_catch
114 %catch = catchswitch within none [label %loop_catch_latch, label %loop_exit] unwind to caller
117 %catchpad_latch = catchpad within %catch []
118 catchret from %catchpad_latch to label %loop_begin
121 %catchpad_exit = catchpad within %catch []
122 catchret from %catchpad_exit to label %exit
129 ; Non-trivial loop unswitching where there are two distinct trivial conditions
130 ; to unswitch within the loop.
131 define i32 @test1(ptr %ptr, i1 %cond1, i1 %cond2) {
132 ; CHECK-LABEL: @test1(
136 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
139 br i1 %cond1, label %loop_a, label %loop_b
144 ; The 'loop_a' unswitched loop.
146 ; CHECK: entry.split.us:
147 ; CHECK-NEXT: br label %loop_begin.us
149 ; CHECK: loop_begin.us:
150 ; CHECK-NEXT: br label %loop_a.us
153 ; CHECK-NEXT: call i32 @a()
154 ; CHECK-NEXT: br label %latch.us
157 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
158 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
160 ; CHECK: loop_exit.split.us:
161 ; CHECK-NEXT: br label %loop_exit
164 br i1 %cond2, label %loop_b_a, label %loop_b_b
165 ; The second unswitched condition.
167 ; CHECK: entry.split:
168 ; CHECK-NEXT: br i1 %cond2, label %entry.split.split.us, label %entry.split.split
173 ; The 'loop_b_a' unswitched loop.
175 ; CHECK: entry.split.split.us:
176 ; CHECK-NEXT: br label %loop_begin.us1
178 ; CHECK: loop_begin.us1:
179 ; CHECK-NEXT: br label %loop_b.us
182 ; CHECK-NEXT: br label %loop_b_a.us
184 ; CHECK: loop_b_a.us:
185 ; CHECK-NEXT: call i32 @b()
186 ; CHECK-NEXT: br label %latch.us2
189 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
190 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us1, label %loop_exit.split.split.us
192 ; CHECK: loop_exit.split.split.us:
193 ; CHECK-NEXT: br label %loop_exit.split
198 ; The 'loop_b_b' unswitched loop.
200 ; CHECK: entry.split.split:
201 ; CHECK-NEXT: br label %loop_begin
204 ; CHECK-NEXT: br label %loop_b
207 ; CHECK-NEXT: br label %loop_b_b
210 ; CHECK-NEXT: call i32 @c()
211 ; CHECK-NEXT: br label %latch
214 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
215 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split.split
217 ; CHECK: loop_exit.split.split:
218 ; CHECK-NEXT: br label %loop_exit.split
221 %v = load i1, ptr %ptr
222 br i1 %v, label %loop_begin, label %loop_exit
226 ; CHECK: loop_exit.split:
227 ; CHECK-NEXT: br label %loop_exit
233 define i32 @test2(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr, ptr %c.ptr) {
234 ; CHECK-LABEL: @test2(
238 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
241 %v = load i1, ptr %ptr
242 br i1 %cond1, label %loop_a, label %loop_b
245 %a = load i32, ptr %a.ptr
246 %ac = load i32, ptr %c.ptr
247 br i1 %v, label %loop_begin, label %loop_exit
248 ; The 'loop_a' unswitched loop.
250 ; CHECK: entry.split.us:
251 ; CHECK-NEXT: br label %loop_begin.us
253 ; CHECK: loop_begin.us:
254 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
255 ; CHECK-NEXT: br label %loop_a.us
258 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
259 ; CHECK-NEXT: %[[AC:.*]] = load i32, ptr %c.ptr
260 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge.us, label %loop_exit.split.us
262 ; CHECK: loop_exit.split.us:
263 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a.us ]
264 ; CHECK-NEXT: %[[AC_LCSSA:.*]] = phi i32 [ %[[AC]], %loop_a.us ]
265 ; CHECK-NEXT: br label %loop_exit
268 %b = load i32, ptr %b.ptr
269 %bc = load i32, ptr %c.ptr
270 br i1 %v, label %loop_begin, label %loop_exit
271 ; The 'loop_b' unswitched loop.
273 ; CHECK: entry.split:
274 ; CHECK-NEXT: br label %loop_begin
277 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
278 ; CHECK-NEXT: br label %loop_b
281 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
282 ; CHECK-NEXT: %[[BC:.*]] = load i32, ptr %c.ptr
283 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge, label %loop_exit.split
285 ; CHECK: loop_exit.split:
286 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
287 ; CHECK-NEXT: %[[BC_LCSSA:.*]] = phi i32 [ %[[BC]], %loop_b ]
288 ; CHECK-NEXT: br label %loop_exit
291 %ab.phi = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
292 %c.phi = phi i32 [ %ac, %loop_a ], [ %bc, %loop_b ]
293 %result = add i32 %ab.phi, %c.phi
296 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
297 ; CHECK-NEXT: %[[C_PHI:.*]] = phi i32 [ %[[BC_LCSSA]], %loop_exit.split ], [ %[[AC_LCSSA]], %loop_exit.split.us ]
298 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[AB_PHI]], %[[C_PHI]]
299 ; CHECK-NEXT: ret i32 %[[RESULT]]
302 ; Test a non-trivial unswitch of an exiting edge to an exit block with other
303 ; in-loop predecessors.
304 define i32 @test3a(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
305 ; CHECK-LABEL: @test3a(
309 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
312 %v = load i1, ptr %ptr
313 %a = load i32, ptr %a.ptr
314 br i1 %cond1, label %loop_exit, label %loop_b
315 ; The 'loop_exit' clone.
317 ; CHECK: entry.split.us:
318 ; CHECK-NEXT: br label %loop_begin.us
320 ; CHECK: loop_begin.us:
321 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
322 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
323 ; CHECK-NEXT: br label %loop_exit.split.us
325 ; CHECK: loop_exit.split.us:
326 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
327 ; CHECK-NEXT: br label %loop_exit
330 %b = load i32, ptr %b.ptr
331 br i1 %v, label %loop_begin, label %loop_exit
332 ; The 'loop_b' unswitched loop.
334 ; CHECK: entry.split:
335 ; CHECK-NEXT: br label %loop_begin
338 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
339 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
340 ; CHECK-NEXT: br label %loop_b
343 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
344 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
346 ; CHECK: loop_exit.split:
347 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
348 ; CHECK-NEXT: br label %loop_exit
351 %ab.phi = phi i32 [ %a, %loop_begin ], [ %b, %loop_b ]
354 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
355 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
358 ; Test a non-trivial unswitch of an exiting edge to an exit block with other
359 ; in-loop predecessors. This is the same as @test3a but with the reversed order
360 ; of successors so that the exiting edge is *not* the cloned edge.
361 define i32 @test3b(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
362 ; CHECK-LABEL: @test3b(
366 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
369 %v = load i1, ptr %ptr
370 %a = load i32, ptr %a.ptr
371 br i1 %cond1, label %loop_b, label %loop_exit
372 ; The 'loop_b' unswitched loop.
374 ; CHECK: entry.split.us:
375 ; CHECK-NEXT: br label %loop_begin.us
377 ; CHECK: loop_begin.us:
378 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
379 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
380 ; CHECK-NEXT: br label %loop_b.us
383 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
384 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
386 ; CHECK: loop_exit.split.us:
387 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b.us ]
388 ; CHECK-NEXT: br label %loop_exit
391 %b = load i32, ptr %b.ptr
392 br i1 %v, label %loop_begin, label %loop_exit
393 ; The original loop, now non-looping due to unswitching..
395 ; CHECK: entry.split:
396 ; CHECK-NEXT: br label %loop_begin
399 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
400 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
401 ; CHECK-NEXT: br label %loop_exit.split
403 ; CHECK: loop_exit.split:
404 ; CHECK-NEXT: br label %loop_exit
407 %ab.phi = phi i32 [ %b, %loop_b ], [ %a, %loop_begin ]
410 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A]], %loop_exit.split ], [ %[[B_LCSSA]], %loop_exit.split.us ]
411 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
414 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
415 ; in-loop predecessors.
416 define void @test4a(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
417 ; CHECK-LABEL: @test4a(
421 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
424 %v = load i1, ptr %ptr
425 %a = load i32, ptr %a.ptr
426 br i1 %cond1, label %loop_exit1, label %loop_b
427 ; The 'loop_exit' clone.
429 ; CHECK: entry.split.us:
430 ; CHECK-NEXT: br label %loop_begin.us
432 ; CHECK: loop_begin.us:
433 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
434 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
435 ; CHECK-NEXT: br label %loop_exit1.split.us
437 ; CHECK: loop_exit1.split.us:
438 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
439 ; CHECK-NEXT: br label %loop_exit1
442 %b = load i32, ptr %b.ptr
443 br i1 %v, label %loop_begin, label %loop_exit2
444 ; The 'loop_b' unswitched loop.
446 ; CHECK: entry.split:
447 ; CHECK-NEXT: br label %loop_begin
450 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
451 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
452 ; CHECK-NEXT: br label %loop_b
455 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
456 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit2
459 %a.phi = phi i32 [ %a, %loop_begin ]
460 call void @sink1(i32 %a.phi)
463 ; CHECK-NEXT: call void @sink1(i32 %[[A_LCSSA]])
464 ; CHECK-NEXT: ret void
467 %b.phi = phi i32 [ %b, %loop_b ]
468 call void @sink2(i32 %b.phi)
471 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
472 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
473 ; CHECK-NEXT: ret void
476 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
477 ; in-loop predecessors. This is the same as @test4a but with the edges reversed
478 ; so that the exiting edge is *not* the cloned edge.
479 define void @test4b(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
480 ; CHECK-LABEL: @test4b(
484 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
487 %v = load i1, ptr %ptr
488 %a = load i32, ptr %a.ptr
489 br i1 %cond1, label %loop_b, label %loop_exit1
490 ; The 'loop_b' clone.
492 ; CHECK: entry.split.us:
493 ; CHECK-NEXT: br label %loop_begin.us
495 ; CHECK: loop_begin.us:
496 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
497 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
498 ; CHECK-NEXT: br label %loop_b.us
501 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
502 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit2.split.us
504 ; CHECK: loop_exit2.split.us:
505 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b.us ]
506 ; CHECK-NEXT: br label %loop_exit2
509 %b = load i32, ptr %b.ptr
510 br i1 %v, label %loop_begin, label %loop_exit2
511 ; The 'loop_exit' unswitched path.
513 ; CHECK: entry.split:
514 ; CHECK-NEXT: br label %loop_begin
517 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
518 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
519 ; CHECK-NEXT: br label %loop_exit1
522 %a.phi = phi i32 [ %a, %loop_begin ]
523 call void @sink1(i32 %a.phi)
526 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ]
527 ; CHECK-NEXT: call void @sink1(i32 %[[A_PHI]])
528 ; CHECK-NEXT: ret void
531 %b.phi = phi i32 [ %b, %loop_b ]
532 call void @sink2(i32 %b.phi)
535 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
536 ; CHECK-NEXT: ret void
539 ; Test a non-trivial unswitch of an exiting edge to an exit block with no other
540 ; in-loop predecessors. This is the same as @test4a but with a common merge
541 ; block after the independent loop exits. This requires a different structural
542 ; update to the dominator tree.
543 define void @test4c(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
544 ; CHECK-LABEL: @test4c(
548 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
551 %v = load i1, ptr %ptr
552 %a = load i32, ptr %a.ptr
553 br i1 %cond1, label %loop_exit1, label %loop_b
554 ; The 'loop_exit' clone.
556 ; CHECK: entry.split.us:
557 ; CHECK-NEXT: br label %loop_begin.us
559 ; CHECK: loop_begin.us:
560 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
561 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
562 ; CHECK-NEXT: br label %loop_exit1.split.us
564 ; CHECK: loop_exit1.split.us:
565 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
566 ; CHECK-NEXT: br label %loop_exit1
569 %b = load i32, ptr %b.ptr
570 br i1 %v, label %loop_begin, label %loop_exit2
571 ; The 'loop_b' unswitched loop.
573 ; CHECK: entry.split:
574 ; CHECK-NEXT: br label %loop_begin
577 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
578 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
579 ; CHECK-NEXT: br label %loop_b
582 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
583 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit2
586 %a.phi = phi i32 [ %a, %loop_begin ]
587 call void @sink1(i32 %a.phi)
590 ; CHECK-NEXT: call void @sink1(i32 %[[A_LCSSA]])
591 ; CHECK-NEXT: br label %exit
594 %b.phi = phi i32 [ %b, %loop_b ]
595 call void @sink2(i32 %b.phi)
598 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b ]
599 ; CHECK-NEXT: call void @sink2(i32 %[[B_LCSSA]])
600 ; CHECK-NEXT: br label %exit
605 ; CHECK-NEXT: ret void
608 ; Test that we can unswitch a condition out of multiple layers of a loop nest.
609 define i32 @test5(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
610 ; CHECK-LABEL: @test5(
614 ; CHECK-NEXT: br i1 %cond1, label %loop_begin.split.us, label %entry.split
616 ; CHECK: entry.split:
617 ; CHECK-NEXT: br label %loop_begin
620 ; CHECK-NEXT: br label %loop_begin.split
623 br label %inner_loop_begin
626 %v = load i1, ptr %ptr
627 %a = load i32, ptr %a.ptr
628 br i1 %cond1, label %loop_exit, label %inner_loop_b
629 ; The 'loop_exit' clone.
631 ; CHECK: loop_begin.split.us:
632 ; CHECK-NEXT: br label %inner_loop_begin.us
634 ; CHECK: inner_loop_begin.us:
635 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
636 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
637 ; CHECK-NEXT: br label %loop_exit.loopexit.split.us
639 ; CHECK: loop_exit.loopexit.split.us:
640 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
641 ; CHECK-NEXT: br label %loop_exit
644 %b = load i32, ptr %b.ptr
645 br i1 %v, label %inner_loop_begin, label %loop_latch
646 ; The 'inner_loop_b' unswitched loop.
648 ; CHECK: loop_begin.split:
649 ; CHECK-NEXT: br label %inner_loop_begin
651 ; CHECK: inner_loop_begin:
652 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
653 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
654 ; CHECK-NEXT: br label %inner_loop_b
656 ; CHECK: inner_loop_b:
657 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
658 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_begin, label %loop_latch
661 %b.phi = phi i32 [ %b, %inner_loop_b ]
662 %v2 = load i1, ptr %ptr
663 br i1 %v2, label %loop_begin, label %loop_exit
665 ; CHECK-NEXT: %[[B_INNER_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_b ]
666 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr
667 ; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.loopexit1
670 %ab.phi = phi i32 [ %a, %inner_loop_begin ], [ %b.phi, %loop_latch ]
672 ; CHECK: loop_exit.loopexit:
673 ; CHECK-NEXT: br label %loop_exit
675 ; CHECK: loop_exit.loopexit1:
676 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_INNER_LCSSA]], %loop_latch ]
677 ; CHECK-NEXT: br label %loop_exit
680 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.loopexit ], [ %[[B_LCSSA]], %loop_exit.loopexit1 ]
681 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
684 ; Test that we can unswitch a condition where we end up only cloning some of
685 ; the nested loops and needing to delete some of the nested loops.
686 define i32 @test6(ptr %ptr, i1 %cond1, ptr %a.ptr, ptr %b.ptr) {
687 ; CHECK-LABEL: @test6(
691 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
694 %v = load i1, ptr %ptr
695 br i1 %cond1, label %loop_a, label %loop_b
698 br label %loop_a_inner
701 %va = load i1, ptr %ptr
702 %a = load i32, ptr %a.ptr
703 br i1 %va, label %loop_a_inner, label %loop_a_inner_exit
706 %a.lcssa = phi i32 [ %a, %loop_a_inner ]
708 ; The 'loop_a' cloned loop.
710 ; CHECK: entry.split.us:
711 ; CHECK-NEXT: br label %loop_begin.us
713 ; CHECK: loop_begin.us:
714 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
715 ; CHECK-NEXT: br label %loop_a.us
718 ; CHECK-NEXT: br label %loop_a_inner.us
720 ; CHECK: loop_a_inner.us
721 ; CHECK-NEXT: %[[VA:.*]] = load i1, ptr %ptr
722 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
723 ; CHECK-NEXT: br i1 %[[VA]], label %loop_a_inner.us, label %loop_a_inner_exit.us
725 ; CHECK: loop_a_inner_exit.us:
726 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a_inner.us ]
727 ; CHECK-NEXT: br label %latch.us
730 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %loop_a_inner_exit.us ]
731 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us
733 ; CHECK: loop_exit.split.us:
734 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_PHI]], %latch.us ]
735 ; CHECK-NEXT: br label %loop_exit
738 br label %loop_b_inner
741 %vb = load i1, ptr %ptr
742 %b = load i32, ptr %b.ptr
743 br i1 %vb, label %loop_b_inner, label %loop_b_inner_exit
746 %b.lcssa = phi i32 [ %b, %loop_b_inner ]
750 %ab.phi = phi i32 [ %a.lcssa, %loop_a_inner_exit ], [ %b.lcssa, %loop_b_inner_exit ]
751 br i1 %v, label %loop_begin, label %loop_exit
752 ; The 'loop_b' unswitched loop.
754 ; CHECK: entry.split:
755 ; CHECK-NEXT: br label %loop_begin
758 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
759 ; CHECK-NEXT: br label %loop_b
762 ; CHECK-NEXT: br label %loop_b_inner
764 ; CHECK: loop_b_inner
765 ; CHECK-NEXT: %[[VB:.*]] = load i1, ptr %ptr
766 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
767 ; CHECK-NEXT: br i1 %[[VB]], label %loop_b_inner, label %loop_b_inner_exit
769 ; CHECK: loop_b_inner_exit:
770 ; CHECK-NEXT: %[[B_INNER_LCSSA:.*]] = phi i32 [ %[[B]], %loop_b_inner ]
771 ; CHECK-NEXT: br label %latch
774 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
776 ; CHECK: loop_exit.split:
777 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_INNER_LCSSA]], %latch ]
778 ; CHECK-NEXT: br label %loop_exit
781 %ab.lcssa = phi i32 [ %ab.phi, %latch ]
784 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA]], %loop_exit.split.us ]
785 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
788 ; Test that when unswitching a deeply nested loop condition in a way that
789 ; produces a non-loop clone that can reach multiple exit blocks which are part
790 ; of different outer loops we correctly divide the cloned loop blocks between
791 ; the outer loops based on reachability.
792 define i32 @test7a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
793 ; CHECK-LABEL: @test7a(
797 ; CHECK-NEXT: br label %loop_begin
800 %a = load i32, ptr %a.ptr
801 br label %inner_loop_begin
803 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
804 ; CHECK-NEXT: br label %inner_loop_begin
807 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
808 %cond = load i1, ptr %cond.ptr
809 %b = load i32, ptr %b.ptr
810 br label %inner_inner_loop_begin
811 ; CHECK: inner_loop_begin:
812 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
813 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
814 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
815 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
816 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
818 inner_inner_loop_begin:
819 %v1 = load i1, ptr %ptr
820 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
823 %v2 = load i1, ptr %ptr
824 br i1 %v2, label %loop_exit, label %inner_inner_loop_c
827 %v3 = load i1, ptr %ptr
828 br i1 %v3, label %inner_inner_loop_exit, label %inner_inner_loop_c
831 %v4 = load i1, ptr %ptr
832 br i1 %v4, label %inner_loop_exit, label %inner_inner_loop_d
835 br i1 %cond, label %inner_loop_exit, label %inner_inner_loop_begin
836 ; The cloned copy that always exits with the adjustments required to fix up
839 ; CHECK: inner_loop_begin.split.us:
840 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
842 ; CHECK: inner_inner_loop_begin.us:
843 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
844 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
846 ; CHECK: inner_inner_loop_b.us:
847 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
848 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_c.us.loopexit
850 ; CHECK: inner_inner_loop_a.us:
851 ; CHECK-NEXT: %[[A_NEW_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_begin.us ]
852 ; CHECK-NEXT: %[[B_NEW_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_begin.us ]
853 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
854 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %inner_inner_loop_c.us
856 ; CHECK: inner_inner_loop_c.us.loopexit:
857 ; CHECK-NEXT: br label %inner_inner_loop_c.us
859 ; CHECK: inner_inner_loop_c.us:
860 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
861 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split.us, label %inner_inner_loop_d.us
863 ; CHECK: inner_inner_loop_d.us:
864 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split
866 ; CHECK: inner_inner_loop_exit.split.us:
867 ; CHECK-NEXT: br label %inner_inner_loop_exit
869 ; CHECK: loop_exit.split.us:
870 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_NEW_LCSSA]], %inner_inner_loop_a.us ]
871 ; CHECK-NEXT: %[[B_LCSSA_US:.*]] = phi i32 [ %[[B_NEW_LCSSA]], %inner_inner_loop_a.us ]
872 ; CHECK-NEXT: br label %loop_exit
874 ; CHECK: inner_loop_exit.loopexit.split.us:
875 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
877 ; The original copy that continues to loop.
879 ; CHECK: inner_loop_begin.split:
880 ; CHECK-NEXT: br label %inner_inner_loop_begin
882 ; CHECK: inner_inner_loop_begin:
883 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
884 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
886 ; CHECK: inner_inner_loop_a:
887 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
888 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %inner_inner_loop_c
890 ; CHECK: inner_inner_loop_b:
891 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
892 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split, label %inner_inner_loop_c
894 ; CHECK: inner_inner_loop_c:
895 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
896 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split, label %inner_inner_loop_d
898 ; CHECK: inner_inner_loop_d:
899 ; CHECK-NEXT: br label %inner_inner_loop_begin
901 ; CHECK: inner_inner_loop_exit.split:
902 ; CHECK-NEXT: br label %inner_inner_loop_exit
904 inner_inner_loop_exit:
905 %a2 = load i32, ptr %a.ptr
906 %v5 = load i1, ptr %ptr
907 br i1 %v5, label %inner_loop_exit, label %inner_loop_begin
908 ; CHECK: inner_inner_loop_exit:
909 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
910 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
911 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
915 ; CHECK: inner_loop_exit.loopexit.split:
916 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
918 ; CHECK: inner_loop_exit.loopexit:
919 ; CHECK-NEXT: br label %inner_loop_exit
921 ; CHECK: inner_loop_exit.loopexit1:
922 ; CHECK-NEXT: br label %inner_loop_exit
924 ; CHECK: inner_loop_exit:
925 ; CHECK-NEXT: br label %loop_begin
928 %a.lcssa = phi i32 [ %a.phi, %inner_inner_loop_a ]
929 %b.lcssa = phi i32 [ %b, %inner_inner_loop_a ]
930 %result = add i32 %a.lcssa, %b.lcssa
932 ; CHECK: loop_exit.split:
933 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a ]
934 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_a ]
935 ; CHECK-NEXT: br label %loop_exit
938 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
939 ; CHECK-NEXT: %[[B_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[B_LCSSA_US]], %loop_exit.split.us ]
940 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[A_PHI]], %[[B_PHI]]
941 ; CHECK-NEXT: ret i32 %[[RESULT]]
944 ; Same pattern as @test7a but here the original loop becomes a non-loop that
945 ; can reach multiple exit blocks which are part of different outer loops.
946 define i32 @test7b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
947 ; CHECK-LABEL: @test7b(
951 ; CHECK-NEXT: br label %loop_begin
954 %a = load i32, ptr %a.ptr
955 br label %inner_loop_begin
957 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
958 ; CHECK-NEXT: br label %inner_loop_begin
961 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
962 %cond = load i1, ptr %cond.ptr
963 %b = load i32, ptr %b.ptr
964 br label %inner_inner_loop_begin
965 ; CHECK: inner_loop_begin:
966 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
967 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
968 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
969 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
970 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
972 inner_inner_loop_begin:
973 %v1 = load i1, ptr %ptr
974 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
977 %v2 = load i1, ptr %ptr
978 br i1 %v2, label %loop_exit, label %inner_inner_loop_c
981 %v3 = load i1, ptr %ptr
982 br i1 %v3, label %inner_inner_loop_exit, label %inner_inner_loop_c
985 %v4 = load i1, ptr %ptr
986 br i1 %v4, label %inner_loop_exit, label %inner_inner_loop_d
989 br i1 %cond, label %inner_inner_loop_begin, label %inner_loop_exit
990 ; The cloned copy that continues looping.
992 ; CHECK: inner_loop_begin.split.us:
993 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
995 ; CHECK: inner_inner_loop_begin.us:
996 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
997 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
999 ; CHECK: inner_inner_loop_b.us:
1000 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1001 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_c.us
1003 ; CHECK: inner_inner_loop_a.us:
1004 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1005 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %inner_inner_loop_c.us
1007 ; CHECK: inner_inner_loop_c.us:
1008 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1009 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split.us, label %inner_inner_loop_d.us
1011 ; CHECK: inner_inner_loop_d.us:
1012 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1014 ; CHECK: inner_inner_loop_exit.split.us:
1015 ; CHECK-NEXT: br label %inner_inner_loop_exit
1017 ; CHECK: loop_exit.split.us:
1018 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a.us ]
1019 ; CHECK-NEXT: %[[B_LCSSA_US:.*]] = phi i32 [ %[[B]], %inner_inner_loop_a.us ]
1020 ; CHECK-NEXT: br label %loop_exit
1022 ; CHECK: inner_loop_exit.loopexit.split.us:
1023 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1025 ; The original copy that now always exits and needs adjustments for exit
1028 ; CHECK: inner_loop_begin.split:
1029 ; CHECK-NEXT: br label %inner_inner_loop_begin
1031 ; CHECK: inner_inner_loop_begin:
1032 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1033 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1035 ; CHECK: inner_inner_loop_a:
1036 ; CHECK-NEXT: %[[A_NEW_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_begin ]
1037 ; CHECK-NEXT: %[[B_NEW_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_begin ]
1038 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1039 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %inner_inner_loop_c
1041 ; CHECK: inner_inner_loop_b:
1042 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1043 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split, label %inner_inner_loop_c.loopexit
1045 ; CHECK: inner_inner_loop_c.loopexit:
1046 ; CHECK-NEXT: br label %inner_inner_loop_c
1048 ; CHECK: inner_inner_loop_c:
1049 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1050 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit.split, label %inner_inner_loop_d
1052 ; CHECK: inner_inner_loop_d:
1053 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split
1055 ; CHECK: inner_inner_loop_exit.split:
1056 ; CHECK-NEXT: br label %inner_inner_loop_exit
1058 inner_inner_loop_exit:
1059 %a2 = load i32, ptr %a.ptr
1060 %v5 = load i1, ptr %ptr
1061 br i1 %v5, label %inner_loop_exit, label %inner_loop_begin
1062 ; CHECK: inner_inner_loop_exit:
1063 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1064 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1065 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1068 br label %loop_begin
1069 ; CHECK: inner_loop_exit.loopexit.split:
1070 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1072 ; CHECK: inner_loop_exit.loopexit:
1073 ; CHECK-NEXT: br label %inner_loop_exit
1075 ; CHECK: inner_loop_exit.loopexit1:
1076 ; CHECK-NEXT: br label %inner_loop_exit
1078 ; CHECK: inner_loop_exit:
1079 ; CHECK-NEXT: br label %loop_begin
1082 %a.lcssa = phi i32 [ %a.phi, %inner_inner_loop_a ]
1083 %b.lcssa = phi i32 [ %b, %inner_inner_loop_a ]
1084 %result = add i32 %a.lcssa, %b.lcssa
1086 ; CHECK: loop_exit.split:
1087 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_NEW_LCSSA]], %inner_inner_loop_a ]
1088 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B_NEW_LCSSA]], %inner_inner_loop_a ]
1089 ; CHECK-NEXT: br label %loop_exit
1092 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
1093 ; CHECK-NEXT: %[[B_PHI:.*]] = phi i32 [ %[[B_LCSSA]], %loop_exit.split ], [ %[[B_LCSSA_US]], %loop_exit.split.us ]
1094 ; CHECK-NEXT: %[[RESULT:.*]] = add i32 %[[A_PHI]], %[[B_PHI]]
1095 ; CHECK-NEXT: ret i32 %[[RESULT]]
1098 ; Test that when the exit block set of an inner loop changes to start at a less
1099 ; high level of the loop nest we correctly hoist the loop up the nest.
1100 define i32 @test8a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1101 ; CHECK-LABEL: @test8a(
1103 br label %loop_begin
1104 ; CHECK-NEXT: entry:
1105 ; CHECK-NEXT: br label %loop_begin
1108 %a = load i32, ptr %a.ptr
1109 br label %inner_loop_begin
1110 ; CHECK: loop_begin:
1111 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1112 ; CHECK-NEXT: br label %inner_loop_begin
1115 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
1116 %cond = load i1, ptr %cond.ptr
1117 %b = load i32, ptr %b.ptr
1118 br label %inner_inner_loop_begin
1119 ; CHECK: inner_loop_begin:
1120 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
1121 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1122 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1123 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1124 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
1126 inner_inner_loop_begin:
1127 %v1 = load i1, ptr %ptr
1128 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
1131 %v2 = load i1, ptr %ptr
1132 br i1 %v2, label %inner_inner_loop_latch, label %inner_loop_exit
1135 br i1 %cond, label %inner_inner_loop_latch, label %inner_inner_loop_exit
1137 inner_inner_loop_latch:
1138 br label %inner_inner_loop_begin
1139 ; The cloned region is now an exit from the inner loop.
1141 ; CHECK: inner_loop_begin.split.us:
1142 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_begin ]
1143 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1145 ; CHECK: inner_inner_loop_begin.us:
1146 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1147 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
1149 ; CHECK: inner_inner_loop_b.us:
1150 ; CHECK-NEXT: br label %inner_inner_loop_latch.us
1152 ; CHECK: inner_inner_loop_a.us:
1153 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1154 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch.us, label %inner_loop_exit.loopexit.split.us
1156 ; CHECK: inner_inner_loop_latch.us:
1157 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1159 ; CHECK: inner_loop_exit.loopexit.split.us:
1160 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_a.us ]
1161 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1163 ; The original region exits the loop earlier.
1165 ; CHECK: inner_loop_begin.split:
1166 ; CHECK-NEXT: br label %inner_inner_loop_begin
1168 ; CHECK: inner_inner_loop_begin:
1169 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1170 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1172 ; CHECK: inner_inner_loop_a:
1173 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1174 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch, label %inner_loop_exit.loopexit.split
1176 ; CHECK: inner_inner_loop_b:
1177 ; CHECK-NEXT: br label %inner_inner_loop_exit
1179 ; CHECK: inner_inner_loop_latch:
1180 ; CHECK-NEXT: br label %inner_inner_loop_begin
1182 inner_inner_loop_exit:
1183 %a2 = load i32, ptr %a.ptr
1184 %v4 = load i1, ptr %ptr
1185 br i1 %v4, label %inner_loop_exit, label %inner_loop_begin
1186 ; CHECK: inner_inner_loop_exit:
1187 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1188 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1189 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1192 %v5 = load i1, ptr %ptr
1193 br i1 %v5, label %loop_exit, label %loop_begin
1194 ; CHECK: inner_loop_exit.loopexit.split:
1195 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a ]
1196 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1198 ; CHECK: inner_loop_exit.loopexit:
1199 ; 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 ]
1200 ; CHECK-NEXT: br label %inner_loop_exit
1202 ; CHECK: inner_loop_exit.loopexit1:
1203 ; CHECK-NEXT: %[[A_INNER_LCSSA2:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_exit ]
1204 ; CHECK-NEXT: br label %inner_loop_exit
1206 ; CHECK: inner_loop_exit:
1207 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA2]], %inner_loop_exit.loopexit1 ], [ %[[A_INNER_US_PHI]], %inner_loop_exit.loopexit ]
1208 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1209 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit, label %loop_begin
1212 %a.lcssa = phi i32 [ %a.phi, %inner_loop_exit ]
1215 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1216 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1219 ; Same pattern as @test8a but where the original loop looses an exit block and
1220 ; needs to be hoisted up the nest.
1221 define i32 @test8b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1222 ; CHECK-LABEL: @test8b(
1224 br label %loop_begin
1225 ; CHECK-NEXT: entry:
1226 ; CHECK-NEXT: br label %loop_begin
1229 %a = load i32, ptr %a.ptr
1230 br label %inner_loop_begin
1231 ; CHECK: loop_begin:
1232 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1233 ; CHECK-NEXT: br label %inner_loop_begin
1236 %a.phi = phi i32 [ %a, %loop_begin ], [ %a2, %inner_inner_loop_exit ]
1237 %cond = load i1, ptr %cond.ptr
1238 %b = load i32, ptr %b.ptr
1239 br label %inner_inner_loop_begin
1240 ; CHECK: inner_loop_begin:
1241 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A]], %loop_begin ], [ %[[A2:.*]], %inner_inner_loop_exit ]
1242 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1243 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1244 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1245 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_begin.split.us, label %inner_loop_begin.split
1247 inner_inner_loop_begin:
1248 %v1 = load i1, ptr %ptr
1249 br i1 %v1, label %inner_inner_loop_a, label %inner_inner_loop_b
1252 %v2 = load i1, ptr %ptr
1253 br i1 %v2, label %inner_inner_loop_latch, label %inner_loop_exit
1256 br i1 %cond, label %inner_inner_loop_exit, label %inner_inner_loop_latch
1258 inner_inner_loop_latch:
1259 br label %inner_inner_loop_begin
1260 ; The cloned region is similar to before but with one earlier exit.
1262 ; CHECK: inner_loop_begin.split.us:
1263 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1265 ; CHECK: inner_inner_loop_begin.us:
1266 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1267 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a.us, label %inner_inner_loop_b.us
1269 ; CHECK: inner_inner_loop_b.us:
1270 ; CHECK-NEXT: br label %inner_inner_loop_exit.split.us
1272 ; CHECK: inner_inner_loop_a.us:
1273 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1274 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch.us, label %inner_loop_exit.loopexit.split.us
1276 ; CHECK: inner_inner_loop_latch.us:
1277 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1279 ; CHECK: inner_inner_loop_exit.split.us:
1280 ; CHECK-NEXT: br label %inner_inner_loop_exit
1282 ; CHECK: inner_loop_exit.loopexit.split.us:
1283 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_a.us ]
1284 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1286 ; The original region is now an exit in the preheader.
1288 ; CHECK: inner_loop_begin.split:
1289 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_begin ]
1290 ; CHECK-NEXT: br label %inner_inner_loop_begin
1292 ; CHECK: inner_inner_loop_begin:
1293 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1294 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_a, label %inner_inner_loop_b
1296 ; CHECK: inner_inner_loop_a:
1297 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1298 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_latch, label %inner_loop_exit.loopexit.split
1300 ; CHECK: inner_inner_loop_b:
1301 ; CHECK-NEXT: br label %inner_inner_loop_latch
1303 ; CHECK: inner_inner_loop_latch:
1304 ; CHECK-NEXT: br label %inner_inner_loop_begin
1306 inner_inner_loop_exit:
1307 %a2 = load i32, ptr %a.ptr
1308 %v4 = load i1, ptr %ptr
1309 br i1 %v4, label %inner_loop_exit, label %inner_loop_begin
1310 ; CHECK: inner_inner_loop_exit:
1311 ; CHECK-NEXT: %[[A2]] = load i32, ptr %a.ptr
1312 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1313 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.loopexit1, label %inner_loop_begin
1316 %v5 = load i1, ptr %ptr
1317 br i1 %v5, label %loop_exit, label %loop_begin
1318 ; CHECK: inner_loop_exit.loopexit.split:
1319 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_a ]
1320 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1322 ; CHECK: inner_loop_exit.loopexit:
1323 ; 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 ]
1324 ; CHECK-NEXT: br label %inner_loop_exit
1326 ; CHECK: inner_loop_exit.loopexit1:
1327 ; CHECK-NEXT: %[[A_INNER_LCSSA2:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_inner_loop_exit ]
1328 ; CHECK-NEXT: br label %inner_loop_exit
1330 ; CHECK: inner_loop_exit:
1331 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA2]], %inner_loop_exit.loopexit1 ], [ %[[A_INNER_US_PHI]], %inner_loop_exit.loopexit ]
1332 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1333 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit, label %loop_begin
1336 %a.lcssa = phi i32 [ %a.phi, %inner_loop_exit ]
1339 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1340 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1343 ; Test for when unswitching produces a clone of an inner loop but
1344 ; the clone no longer has an exiting edge *at all* and loops infinitely.
1345 ; Because it doesn't ever exit to the outer loop it is no longer an inner loop
1346 ; but needs to be hoisted up the nest to be a top-level loop.
1347 define i32 @test9a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1348 ; CHECK-LABEL: @test9a(
1350 br label %loop_begin
1351 ; CHECK-NEXT: entry:
1352 ; CHECK-NEXT: br label %loop_begin
1355 %b = load i32, ptr %b.ptr
1356 %cond = load i1, ptr %cond.ptr
1357 br label %inner_loop_begin
1358 ; CHECK: loop_begin:
1359 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1360 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1361 ; CHECK-NEXT: br i1 %[[COND]], label %loop_begin.split.us, label %loop_begin.split
1364 %a = load i32, ptr %a.ptr
1365 br i1 %cond, label %inner_loop_latch, label %inner_loop_exit
1368 call void @sink1(i32 %b)
1369 br label %inner_loop_begin
1370 ; The cloned inner loop ends up as an infinite loop and thus being a top-level
1371 ; loop with the preheader as an exit block of the outer loop.
1373 ; CHECK: loop_begin.split.us
1374 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_begin ]
1375 ; CHECK-NEXT: br label %inner_loop_begin.us
1377 ; CHECK: inner_loop_begin.us:
1378 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1379 ; CHECK-NEXT: br label %inner_loop_latch.us
1381 ; CHECK: inner_loop_latch.us:
1382 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1383 ; CHECK-NEXT: br label %inner_loop_begin.us
1385 ; The original loop becomes boring non-loop code.
1387 ; CHECK: loop_begin.split
1388 ; CHECK-NEXT: br label %inner_loop_begin
1390 ; CHECK: inner_loop_begin:
1391 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1392 ; CHECK-NEXT: br label %inner_loop_exit
1395 %a.inner_lcssa = phi i32 [ %a, %inner_loop_begin ]
1396 %v = load i1, ptr %ptr
1397 br i1 %v, label %loop_begin, label %loop_exit
1398 ; CHECK: inner_loop_exit:
1399 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin ]
1400 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1401 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1404 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1407 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit ]
1408 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1411 ; The same core pattern as @test9a, but instead of the cloned loop becoming an
1412 ; infinite loop, the original loop has its only exit unswitched and the
1413 ; original loop becomes infinite and must be hoisted out of the loop nest.
1414 define i32 @test9b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1415 ; CHECK-LABEL: @test9b(
1417 br label %loop_begin
1418 ; CHECK-NEXT: entry:
1419 ; CHECK-NEXT: br label %loop_begin
1422 %b = load i32, ptr %b.ptr
1423 %cond = load i1, ptr %cond.ptr
1424 br label %inner_loop_begin
1425 ; CHECK: loop_begin:
1426 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1427 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1428 ; CHECK-NEXT: br i1 %[[COND]], label %loop_begin.split.us, label %loop_begin.split
1431 %a = load i32, ptr %a.ptr
1432 br i1 %cond, label %inner_loop_exit, label %inner_loop_latch
1435 call void @sink1(i32 %b)
1436 br label %inner_loop_begin
1437 ; The cloned inner loop becomes a boring non-loop.
1439 ; CHECK: loop_begin.split.us
1440 ; CHECK-NEXT: br label %inner_loop_begin.us
1442 ; CHECK: inner_loop_begin.us:
1443 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1444 ; CHECK-NEXT: br label %inner_loop_exit.split.us
1446 ; CHECK: inner_loop_exit.split.us
1447 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
1448 ; CHECK-NEXT: br label %inner_loop_exit
1450 ; The original loop becomes an infinite loop and thus a top-level loop with the
1451 ; preheader as an exit block for the outer loop.
1453 ; CHECK: loop_begin.split
1454 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %loop_begin ]
1455 ; CHECK-NEXT: br label %inner_loop_begin
1457 ; CHECK: inner_loop_begin:
1458 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1459 ; CHECK-NEXT: br label %inner_loop_latch
1461 ; CHECK: inner_loop_latch:
1462 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1463 ; CHECK-NEXT: br label %inner_loop_begin
1466 %a.inner_lcssa = phi i32 [ %a, %inner_loop_begin ]
1467 %v = load i1, ptr %ptr
1468 br i1 %v, label %loop_begin, label %loop_exit
1469 ; CHECK: inner_loop_exit:
1470 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1471 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1474 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1477 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %inner_loop_exit ]
1478 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1481 ; Test that requires re-forming dedicated exits for the cloned loop.
1482 define i32 @test10a(ptr %ptr, i1 %cond, ptr %a.ptr) {
1483 ; CHECK-LABEL: @test10a(
1485 br label %loop_begin
1486 ; CHECK-NEXT: entry:
1487 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
1488 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
1491 %a = load i32, ptr %a.ptr
1492 %v1 = load i1, ptr %ptr
1493 br i1 %v1, label %loop_a, label %loop_b
1496 %v2 = load i1, ptr %ptr
1497 br i1 %v2, label %loop_exit, label %loop_begin
1500 br i1 %cond, label %loop_exit, label %loop_begin
1501 ; The cloned loop with one edge as a direct exit.
1503 ; CHECK: entry.split.us:
1504 ; CHECK-NEXT: br label %loop_begin.us
1506 ; CHECK: loop_begin.us:
1507 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1508 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1509 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
1512 ; CHECK-NEXT: %[[A_LCSSA_B:.*]] = phi i32 [ %[[A]], %loop_begin.us ]
1513 ; CHECK-NEXT: br label %loop_exit.split.us
1516 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1517 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us.loopexit, label %loop_begin.backedge.us
1519 ; CHECK: loop_begin.backedge.us:
1520 ; CHECK-NEXT: br label %loop_begin.us
1522 ; CHECK: loop_exit.split.us.loopexit:
1523 ; CHECK-NEXT: %[[A_LCSSA_A:.*]] = phi i32 [ %[[A]], %loop_a.us ]
1524 ; CHECK-NEXT: br label %loop_exit
1526 ; CHECK: loop_exit.split.us:
1527 ; CHECK-NEXT: %[[A_PHI_US:.*]] = phi i32 [ %[[A_LCSSA_B]], %loop_b.us ], [ %[[A_LCSSA_A]], %loop_exit.split.us.loopexit ]
1528 ; CHECK-NEXT: br label %loop_exit
1530 ; The original loop without one 'loop_exit' edge.
1532 ; CHECK: entry.split:
1533 ; CHECK-NEXT: br label %loop_begin
1535 ; CHECK: loop_begin:
1536 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1537 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1538 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
1541 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1542 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %loop_begin.backedge
1544 ; CHECK: loop_begin.backedge:
1545 ; CHECK-NEXT: br label %loop_begin
1548 ; CHECK-NEXT: br label %loop_begin.backedge
1550 ; CHECK: loop_exit.split:
1551 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
1552 ; CHECK-NEXT: br label %loop_exit
1555 %a.lcssa = phi i32 [ %a, %loop_a ], [ %a, %loop_b ]
1558 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[A_PHI_US]], %loop_exit.split.us ]
1559 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1562 ; Test that requires re-forming dedicated exits for the original loop.
1563 define i32 @test10b(ptr %ptr, i1 %cond, ptr %a.ptr) {
1564 ; CHECK-LABEL: @test10b(
1566 br label %loop_begin
1567 ; CHECK-NEXT: entry:
1568 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
1569 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
1572 %a = load i32, ptr %a.ptr
1573 %v1 = load i1, ptr %ptr
1574 br i1 %v1, label %loop_a, label %loop_b
1577 %v2 = load i1, ptr %ptr
1578 br i1 %v2, label %loop_begin, label %loop_exit
1581 br i1 %cond, label %loop_begin, label %loop_exit
1582 ; The cloned loop without one of the exits.
1584 ; CHECK: entry.split.us:
1585 ; CHECK-NEXT: br label %loop_begin.us
1587 ; CHECK: loop_begin.us:
1588 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1589 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1590 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
1593 ; CHECK-NEXT: br label %loop_begin.backedge.us
1596 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1597 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge.us, label %loop_exit.split.us
1599 ; CHECK: loop_begin.backedge.us:
1600 ; CHECK-NEXT: br label %loop_begin.us
1602 ; CHECK: loop_exit.split.us:
1603 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
1604 ; CHECK-NEXT: br label %loop_exit
1606 ; The original loop without one 'loop_exit' edge.
1608 ; CHECK: entry.split:
1609 ; CHECK-NEXT: br label %loop_begin
1611 ; CHECK: loop_begin:
1612 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1613 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1614 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
1617 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1618 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.backedge, label %loop_exit.split.loopexit
1620 ; CHECK: loop_begin.backedge:
1621 ; CHECK-NEXT: br label %loop_begin
1624 ; CHECK-NEXT: %[[A_LCSSA_B:.*]] = phi i32 [ %[[A]], %loop_begin ]
1625 ; CHECK-NEXT: br label %loop_exit.split
1627 ; CHECK: loop_exit.split.loopexit:
1628 ; CHECK-NEXT: %[[A_LCSSA_A:.*]] = phi i32 [ %[[A]], %loop_a ]
1629 ; CHECK-NEXT: br label %loop_exit.split
1631 ; CHECK: loop_exit.split:
1632 ; CHECK-NEXT: %[[A_PHI_SPLIT:.*]] = phi i32 [ %[[A_LCSSA_B]], %loop_b ], [ %[[A_LCSSA_A]], %loop_exit.split.loopexit ]
1633 ; CHECK-NEXT: br label %loop_exit
1636 %a.lcssa = phi i32 [ %a, %loop_a ], [ %a, %loop_b ]
1639 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_PHI_SPLIT]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
1640 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1643 ; Check that if a cloned inner loop after unswitching doesn't loop and directly
1644 ; exits even an outer loop, we don't add the cloned preheader to the outer
1645 ; loop and do add the needed LCSSA phi nodes for the new exit block from the
1647 define i32 @test11a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1648 ; CHECK-LABEL: @test11a(
1650 br label %loop_begin
1651 ; CHECK-NEXT: entry:
1652 ; CHECK-NEXT: br label %loop_begin
1655 %b = load i32, ptr %b.ptr
1656 %v1 = load i1, ptr %ptr
1657 br i1 %v1, label %loop_latch, label %inner_loop_ph
1658 ; CHECK: loop_begin:
1659 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1660 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1661 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %inner_loop_ph
1664 %cond = load i1, ptr %cond.ptr
1665 br label %inner_loop_begin
1666 ; CHECK: inner_loop_ph:
1667 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1668 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1669 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_ph.split.us, label %inner_loop_ph.split
1672 call void @sink1(i32 %b)
1673 %a = load i32, ptr %a.ptr
1674 br i1 %cond, label %loop_exit, label %inner_loop_a
1677 %v2 = load i1, ptr %ptr
1678 br i1 %v2, label %inner_loop_exit, label %inner_loop_begin
1679 ; The cloned path doesn't actually loop and is an exit from the outer loop as
1682 ; CHECK: inner_loop_ph.split.us:
1683 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_ph ]
1684 ; CHECK-NEXT: br label %inner_loop_begin.us
1686 ; CHECK: inner_loop_begin.us:
1687 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1688 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1689 ; CHECK-NEXT: br label %loop_exit.loopexit.split.us
1691 ; CHECK: loop_exit.loopexit.split.us:
1692 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_begin.us ]
1693 ; CHECK-NEXT: br label %loop_exit.loopexit
1695 ; The original remains a loop losing the exit edge.
1697 ; CHECK: inner_loop_ph.split:
1698 ; CHECK-NEXT: br label %inner_loop_begin
1700 ; CHECK: inner_loop_begin:
1701 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1702 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1703 ; CHECK-NEXT: br label %inner_loop_a
1705 ; CHECK: inner_loop_a:
1706 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1707 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit, label %inner_loop_begin
1710 %a.inner_lcssa = phi i32 [ %a, %inner_loop_a ]
1711 %v3 = load i1, ptr %ptr
1712 br i1 %v3, label %loop_latch, label %loop_exit
1713 ; CHECK: inner_loop_exit:
1714 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_a ]
1715 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1716 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %loop_exit.loopexit1
1719 br label %loop_begin
1720 ; CHECK: loop_latch:
1721 ; CHECK-NEXT: br label %loop_begin
1724 %a.lcssa = phi i32 [ %a, %inner_loop_begin ], [ %a.inner_lcssa, %inner_loop_exit ]
1726 ; CHECK: loop_exit.loopexit:
1727 ; CHECK-NEXT: br label %loop_exit
1729 ; CHECK: loop_exit.loopexit1:
1730 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit ]
1731 ; CHECK-NEXT: br label %loop_exit
1734 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %loop_exit.loopexit ], [ %[[A_LCSSA]], %loop_exit.loopexit1 ]
1735 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1738 ; Check that if the original inner loop after unswitching doesn't loop and
1739 ; directly exits even an outer loop, we remove the original preheader from the
1740 ; outer loop and add needed LCSSA phi nodes for the new exit block from the
1742 define i32 @test11b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1743 ; CHECK-LABEL: @test11b(
1745 br label %loop_begin
1746 ; CHECK-NEXT: entry:
1747 ; CHECK-NEXT: br label %loop_begin
1750 %b = load i32, ptr %b.ptr
1751 %v1 = load i1, ptr %ptr
1752 br i1 %v1, label %loop_latch, label %inner_loop_ph
1753 ; CHECK: loop_begin:
1754 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1755 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1756 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %inner_loop_ph
1759 %cond = load i1, ptr %cond.ptr
1760 br label %inner_loop_begin
1761 ; CHECK: inner_loop_ph:
1762 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1763 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1764 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_loop_ph.split.us, label %inner_loop_ph.split
1767 call void @sink1(i32 %b)
1768 %a = load i32, ptr %a.ptr
1769 br i1 %cond, label %inner_loop_a, label %loop_exit
1772 %v2 = load i1, ptr %ptr
1773 br i1 %v2, label %inner_loop_exit, label %inner_loop_begin
1774 ; The cloned path continues to loop without the exit out of the entire nest.
1776 ; CHECK: inner_loop_ph.split.us:
1777 ; CHECK-NEXT: br label %inner_loop_begin.us
1779 ; CHECK: inner_loop_begin.us:
1780 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1781 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1782 ; CHECK-NEXT: br label %inner_loop_a.us
1784 ; CHECK: inner_loop_a.us:
1785 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1786 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_exit.split.us, label %inner_loop_begin.us
1788 ; CHECK: inner_loop_exit.split.us:
1789 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_loop_a.us ]
1790 ; CHECK-NEXT: br label %inner_loop_exit
1792 ; The original remains a loop losing the exit edge.
1794 ; CHECK: inner_loop_ph.split:
1795 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_loop_ph ]
1796 ; CHECK-NEXT: br label %inner_loop_begin
1798 ; CHECK: inner_loop_begin:
1799 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1800 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1801 ; CHECK-NEXT: br label %loop_exit.loopexit
1804 %a.inner_lcssa = phi i32 [ %a, %inner_loop_a ]
1805 %v3 = load i1, ptr %ptr
1806 br i1 %v3, label %loop_latch, label %loop_exit
1807 ; CHECK: inner_loop_exit:
1808 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1809 ; CHECK-NEXT: br i1 %[[V]], label %loop_latch, label %loop_exit.loopexit1
1812 br label %loop_begin
1813 ; CHECK: loop_latch:
1814 ; CHECK-NEXT: br label %loop_begin
1817 %a.lcssa = phi i32 [ %a, %inner_loop_begin ], [ %a.inner_lcssa, %inner_loop_exit ]
1819 ; CHECK: loop_exit.loopexit:
1820 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %inner_loop_begin ]
1821 ; CHECK-NEXT: br label %loop_exit
1823 ; CHECK: loop_exit.loopexit1:
1824 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_LCSSA_US]], %inner_loop_exit ]
1825 ; CHECK-NEXT: br label %loop_exit
1828 ; CHECK-NEXT: %[[A_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.loopexit ], [ %[[A_LCSSA_US]], %loop_exit.loopexit1 ]
1829 ; CHECK-NEXT: ret i32 %[[A_PHI]]
1832 ; Like test11a, but checking that when the whole thing is wrapped in yet
1833 ; another loop, we correctly attribute the cloned preheader to that outermost
1834 ; loop rather than only handling the case where the preheader is not in any loop
1836 define i32 @test12a(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1837 ; CHECK-LABEL: @test12a(
1839 br label %loop_begin
1840 ; CHECK-NEXT: entry:
1841 ; CHECK-NEXT: br label %loop_begin
1844 br label %inner_loop_begin
1845 ; CHECK: loop_begin:
1846 ; CHECK-NEXT: br label %inner_loop_begin
1849 %b = load i32, ptr %b.ptr
1850 %v1 = load i1, ptr %ptr
1851 br i1 %v1, label %inner_loop_latch, label %inner_inner_loop_ph
1852 ; CHECK: inner_loop_begin:
1853 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1854 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1855 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_inner_loop_ph
1857 inner_inner_loop_ph:
1858 %cond = load i1, ptr %cond.ptr
1859 br label %inner_inner_loop_begin
1860 ; CHECK: inner_inner_loop_ph:
1861 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1862 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1863 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_inner_loop_ph.split.us, label %inner_inner_loop_ph.split
1865 inner_inner_loop_begin:
1866 call void @sink1(i32 %b)
1867 %a = load i32, ptr %a.ptr
1868 br i1 %cond, label %inner_loop_exit, label %inner_inner_loop_a
1871 %v2 = load i1, ptr %ptr
1872 br i1 %v2, label %inner_inner_loop_exit, label %inner_inner_loop_begin
1873 ; The cloned path doesn't actually loop and is an exit from the outer loop as
1876 ; CHECK: inner_inner_loop_ph.split.us:
1877 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_ph ]
1878 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1880 ; CHECK: inner_inner_loop_begin.us:
1881 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
1882 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1883 ; CHECK-NEXT: br label %inner_loop_exit.loopexit.split.us
1885 ; CHECK: inner_loop_exit.loopexit.split.us:
1886 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_inner_loop_begin.us ]
1887 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
1889 ; The original remains a loop losing the exit edge.
1891 ; CHECK: inner_inner_loop_ph.split:
1892 ; CHECK-NEXT: br label %inner_inner_loop_begin
1894 ; CHECK: inner_inner_loop_begin:
1895 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1896 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1897 ; CHECK-NEXT: br label %inner_inner_loop_a
1899 ; CHECK: inner_inner_loop_a:
1900 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1901 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit, label %inner_inner_loop_begin
1903 inner_inner_loop_exit:
1904 %a.inner_inner_lcssa = phi i32 [ %a, %inner_inner_loop_a ]
1905 %v3 = load i1, ptr %ptr
1906 br i1 %v3, label %inner_loop_latch, label %inner_loop_exit
1907 ; CHECK: inner_inner_loop_exit:
1908 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_inner_loop_a ]
1909 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1910 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_loop_exit.loopexit1
1913 br label %inner_loop_begin
1914 ; CHECK: inner_loop_latch:
1915 ; CHECK-NEXT: br label %inner_loop_begin
1918 %a.inner_lcssa = phi i32 [ %a, %inner_inner_loop_begin ], [ %a.inner_inner_lcssa, %inner_inner_loop_exit ]
1919 %v4 = load i1, ptr %ptr
1920 br i1 %v4, label %loop_begin, label %loop_exit
1921 ; CHECK: inner_loop_exit.loopexit:
1922 ; CHECK-NEXT: br label %inner_loop_exit
1924 ; CHECK: inner_loop_exit.loopexit1:
1925 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA]], %inner_inner_loop_exit ]
1926 ; CHECK-NEXT: br label %inner_loop_exit
1928 ; CHECK: inner_loop_exit:
1929 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA_US]], %inner_loop_exit.loopexit ], [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit1 ]
1930 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1931 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
1934 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
1937 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
1938 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
1941 ; Like test11b, but checking that when the whole thing is wrapped in yet
1942 ; another loop, we correctly sink the preheader to the outermost loop rather
1943 ; than only handling the case where the preheader is completely removed from
1945 define i32 @test12b(ptr %ptr, ptr %cond.ptr, ptr %a.ptr, ptr %b.ptr) {
1946 ; CHECK-LABEL: @test12b(
1948 br label %loop_begin
1949 ; CHECK-NEXT: entry:
1950 ; CHECK-NEXT: br label %loop_begin
1953 br label %inner_loop_begin
1954 ; CHECK: loop_begin:
1955 ; CHECK-NEXT: br label %inner_loop_begin
1958 %b = load i32, ptr %b.ptr
1959 %v1 = load i1, ptr %ptr
1960 br i1 %v1, label %inner_loop_latch, label %inner_inner_loop_ph
1961 ; CHECK: inner_loop_begin:
1962 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
1963 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1964 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_inner_loop_ph
1966 inner_inner_loop_ph:
1967 %cond = load i1, ptr %cond.ptr
1968 br label %inner_inner_loop_begin
1969 ; CHECK: inner_inner_loop_ph:
1970 ; CHECK-NEXT: %[[COND:.*]] = load i1, ptr %cond.ptr
1971 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %[[COND]]
1972 ; CHECK-NEXT: br i1 %[[FROZEN]], label %inner_inner_loop_ph.split.us, label %inner_inner_loop_ph.split
1974 inner_inner_loop_begin:
1975 call void @sink1(i32 %b)
1976 %a = load i32, ptr %a.ptr
1977 br i1 %cond, label %inner_inner_loop_a, label %inner_loop_exit
1980 %v2 = load i1, ptr %ptr
1981 br i1 %v2, label %inner_inner_loop_exit, label %inner_inner_loop_begin
1982 ; The cloned path continues to loop without the exit out of the entire nest.
1984 ; CHECK: inner_inner_loop_ph.split.us:
1985 ; CHECK-NEXT: br label %inner_inner_loop_begin.us
1987 ; CHECK: inner_inner_loop_begin.us:
1988 ; CHECK-NEXT: call void @sink1(i32 %[[B]])
1989 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
1990 ; CHECK-NEXT: br label %inner_inner_loop_a.us
1992 ; CHECK: inner_inner_loop_a.us:
1993 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
1994 ; CHECK-NEXT: br i1 %[[V]], label %inner_inner_loop_exit.split.us, label %inner_inner_loop_begin.us
1996 ; CHECK: inner_inner_loop_exit.split.us:
1997 ; CHECK-NEXT: %[[A_INNER_INNER_LCSSA_US:.*]] = phi i32 [ %[[A]], %inner_inner_loop_a.us ]
1998 ; CHECK-NEXT: br label %inner_inner_loop_exit
2000 ; The original remains a loop losing the exit edge.
2002 ; CHECK: inner_inner_loop_ph.split:
2003 ; CHECK-NEXT: %[[B_LCSSA:.*]] = phi i32 [ %[[B]], %inner_inner_loop_ph ]
2004 ; CHECK-NEXT: br label %inner_inner_loop_begin
2006 ; CHECK: inner_inner_loop_begin:
2007 ; CHECK-NEXT: call void @sink1(i32 %[[B_LCSSA]])
2008 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2009 ; CHECK-NEXT: br label %inner_loop_exit.loopexit
2011 inner_inner_loop_exit:
2012 %a.inner_inner_lcssa = phi i32 [ %a, %inner_inner_loop_a ]
2013 %v3 = load i1, ptr %ptr
2014 br i1 %v3, label %inner_loop_latch, label %inner_loop_exit
2015 ; CHECK: inner_inner_loop_exit:
2016 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2017 ; CHECK-NEXT: br i1 %[[V]], label %inner_loop_latch, label %inner_loop_exit.loopexit1
2020 br label %inner_loop_begin
2021 ; CHECK: inner_loop_latch:
2022 ; CHECK-NEXT: br label %inner_loop_begin
2025 %a.inner_lcssa = phi i32 [ %a, %inner_inner_loop_begin ], [ %a.inner_inner_lcssa, %inner_inner_loop_exit ]
2026 %v4 = load i1, ptr %ptr
2027 br i1 %v4, label %loop_begin, label %loop_exit
2028 ; CHECK: inner_loop_exit.loopexit:
2029 ; CHECK-NEXT: %[[A_INNER_LCSSA:.*]] = phi i32 [ %[[A]], %inner_inner_loop_begin ]
2030 ; CHECK-NEXT: br label %inner_loop_exit
2032 ; CHECK: inner_loop_exit.loopexit1:
2033 ; CHECK-NEXT: %[[A_INNER_LCSSA_US:.*]] = phi i32 [ %[[A_INNER_INNER_LCSSA_US]], %inner_inner_loop_exit ]
2034 ; CHECK-NEXT: br label %inner_loop_exit
2036 ; CHECK: inner_loop_exit:
2037 ; CHECK-NEXT: %[[A_INNER_PHI:.*]] = phi i32 [ %[[A_INNER_LCSSA]], %inner_loop_exit.loopexit ], [ %[[A_INNER_LCSSA_US]], %inner_loop_exit.loopexit1 ]
2038 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2039 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit
2042 %a.lcssa = phi i32 [ %a.inner_lcssa, %inner_loop_exit ]
2045 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A_INNER_PHI]], %inner_loop_exit ]
2046 ; CHECK-NEXT: ret i32 %[[A_LCSSA]]
2049 ; Test where the cloned loop has an inner loop that has to be traversed to form
2050 ; the cloned loop, and where this inner loop has multiple blocks, and where the
2051 ; exiting block that connects the inner loop to the cloned loop is not the header
2052 ; block. This ensures that we correctly handle interesting corner cases of
2053 ; traversing back to the header when establishing the cloned loop.
2054 define i32 @test13a(ptr %ptr, i1 %cond, ptr %a.ptr, ptr %b.ptr) {
2055 ; CHECK-LABEL: @test13a(
2057 br label %loop_begin
2058 ; CHECK-NEXT: entry:
2059 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
2060 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
2063 %a = load i32, ptr %a.ptr
2064 %v1 = load i1, ptr %ptr
2065 br i1 %v1, label %loop_a, label %loop_b
2068 %v2 = load i1, ptr %ptr
2069 br i1 %v2, label %loop_exit, label %loop_latch
2072 %b = load i32, ptr %b.ptr
2073 br i1 %cond, label %loop_b_inner_ph, label %loop_exit
2076 br label %loop_b_inner_header
2078 loop_b_inner_header:
2079 %v3 = load i1, ptr %ptr
2080 br i1 %v3, label %loop_b_inner_latch, label %loop_b_inner_body
2083 %v4 = load i1, ptr %ptr
2084 br i1 %v4, label %loop_b_inner_latch, label %loop_b_inner_exit
2087 br label %loop_b_inner_header
2090 br label %loop_latch
2093 br label %loop_begin
2094 ; The cloned loop contains an inner loop within it.
2096 ; CHECK: entry.split.us:
2097 ; CHECK-NEXT: br label %loop_begin.us
2099 ; CHECK: loop_begin.us:
2100 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2101 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2102 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
2105 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2106 ; CHECK-NEXT: br label %loop_b_inner_ph.us
2108 ; CHECK: loop_b_inner_ph.us:
2109 ; CHECK-NEXT: br label %loop_b_inner_header.us
2111 ; CHECK: loop_b_inner_header.us:
2112 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2113 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch.us, label %loop_b_inner_body.us
2115 ; CHECK: loop_b_inner_body.us:
2116 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2117 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch.us, label %loop_b_inner_exit.us
2119 ; CHECK: loop_b_inner_exit.us:
2120 ; CHECK-NEXT: br label %loop_latch.us
2122 ; CHECK: loop_b_inner_latch.us:
2123 ; CHECK-NEXT: br label %loop_b_inner_header.us
2126 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2127 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us, label %loop_latch.us
2129 ; CHECK: loop_latch.us:
2130 ; CHECK-NEXT: br label %loop_begin.us
2132 ; CHECK: loop_exit.split.us:
2133 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
2134 ; CHECK-NEXT: br label %loop_exit
2136 ; And the original loop no longer contains an inner loop.
2138 ; CHECK: entry.split:
2139 ; CHECK-NEXT: br label %loop_begin
2141 ; CHECK: loop_begin:
2142 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2143 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2144 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
2147 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2148 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.loopexit, label %loop_latch
2151 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2152 ; CHECK-NEXT: br label %loop_exit.split
2154 ; CHECK: loop_latch:
2155 ; CHECK-NEXT: br label %loop_begin
2158 %lcssa = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
2160 ; CHECK: loop_exit.split.loopexit:
2161 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
2162 ; CHECK-NEXT: br label %loop_exit.split
2164 ; CHECK: loop_exit.split:
2165 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[B]], %loop_b ], [ %[[A_LCSSA]], %loop_exit.split.loopexit ]
2166 ; CHECK-NEXT: br label %loop_exit
2169 ; CHECK-NEXT: %[[AB_PHI_US:.*]] = phi i32 [ %[[AB_PHI]], %loop_exit.split ], [ %[[A_LCSSA_US]], %loop_exit.split.us ]
2170 ; CHECK-NEXT: ret i32 %[[AB_PHI_US]]
2173 ; Test where the original loop has an inner loop that has to be traversed to
2174 ; rebuild the loop, and where this inner loop has multiple blocks, and where
2175 ; the exiting block that connects the inner loop to the original loop is not
2176 ; the header block. This ensures that we correctly handle interesting corner
2177 ; cases of traversing back to the header when re-establishing the original loop
2178 ; still exists after unswitching.
2179 define i32 @test13b(ptr %ptr, i1 %cond, ptr %a.ptr, ptr %b.ptr) {
2180 ; CHECK-LABEL: @test13b(
2182 br label %loop_begin
2183 ; CHECK-NEXT: entry:
2184 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %cond
2185 ; CHECK-NEXT: br i1 %[[FROZEN]], label %entry.split.us, label %entry.split
2188 %a = load i32, ptr %a.ptr
2189 %v1 = load i1, ptr %ptr
2190 br i1 %v1, label %loop_a, label %loop_b
2193 %v2 = load i1, ptr %ptr
2194 br i1 %v2, label %loop_exit, label %loop_latch
2197 %b = load i32, ptr %b.ptr
2198 br i1 %cond, label %loop_exit, label %loop_b_inner_ph
2201 br label %loop_b_inner_header
2203 loop_b_inner_header:
2204 %v3 = load i1, ptr %ptr
2205 br i1 %v3, label %loop_b_inner_latch, label %loop_b_inner_body
2208 %v4 = load i1, ptr %ptr
2209 br i1 %v4, label %loop_b_inner_latch, label %loop_b_inner_exit
2212 br label %loop_b_inner_header
2215 br label %loop_latch
2218 br label %loop_begin
2219 ; The cloned loop doesn't contain an inner loop.
2221 ; CHECK: entry.split.us:
2222 ; CHECK-NEXT: br label %loop_begin.us
2224 ; CHECK: loop_begin.us:
2225 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2226 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2227 ; CHECK-NEXT: br i1 %[[V]], label %loop_a.us, label %loop_b.us
2230 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2231 ; CHECK-NEXT: br label %loop_exit.split.us
2234 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2235 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split.us.loopexit, label %loop_latch.us
2237 ; CHECK: loop_latch.us:
2238 ; CHECK-NEXT: br label %loop_begin.us
2240 ; CHECK: loop_exit.split.us.loopexit:
2241 ; CHECK-NEXT: %[[A_LCSSA_US:.*]] = phi i32 [ %[[A]], %loop_a.us ]
2242 ; CHECK-NEXT: br label %loop_exit.split.us
2244 ; CHECK: loop_exit.split.us:
2245 ; CHECK-NEXT: %[[AB_PHI_US:.*]] = phi i32 [ %[[B]], %loop_b.us ], [ %[[A_LCSSA_US]], %loop_exit.split.us.loopexit ]
2246 ; CHECK-NEXT: br label %loop_exit
2248 ; But the original loop contains an inner loop that must be traversed.;
2250 ; CHECK: entry.split:
2251 ; CHECK-NEXT: br label %loop_begin
2253 ; CHECK: loop_begin:
2254 ; CHECK-NEXT: %[[A:.*]] = load i32, ptr %a.ptr
2255 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2256 ; CHECK-NEXT: br i1 %[[V]], label %loop_a, label %loop_b
2259 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2260 ; CHECK-NEXT: br i1 %[[V]], label %loop_exit.split, label %loop_latch
2263 ; CHECK-NEXT: %[[B:.*]] = load i32, ptr %b.ptr
2264 ; CHECK-NEXT: br label %loop_b_inner_ph
2266 ; CHECK: loop_b_inner_ph:
2267 ; CHECK-NEXT: br label %loop_b_inner_header
2269 ; CHECK: loop_b_inner_header:
2270 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2271 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch, label %loop_b_inner_body
2273 ; CHECK: loop_b_inner_body:
2274 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2275 ; CHECK-NEXT: br i1 %[[V]], label %loop_b_inner_latch, label %loop_b_inner_exit
2277 ; CHECK: loop_b_inner_latch:
2278 ; CHECK-NEXT: br label %loop_b_inner_header
2280 ; CHECK: loop_b_inner_exit:
2281 ; CHECK-NEXT: br label %loop_latch
2283 ; CHECK: loop_latch:
2284 ; CHECK-NEXT: br label %loop_begin
2287 %lcssa = phi i32 [ %a, %loop_a ], [ %b, %loop_b ]
2289 ; CHECK: loop_exit.split:
2290 ; CHECK-NEXT: %[[A_LCSSA:.*]] = phi i32 [ %[[A]], %loop_a ]
2291 ; CHECK-NEXT: br label %loop_exit
2294 ; CHECK-NEXT: %[[AB_PHI:.*]] = phi i32 [ %[[A_LCSSA]], %loop_exit.split ], [ %[[AB_PHI_US]], %loop_exit.split.us ]
2295 ; CHECK-NEXT: ret i32 %[[AB_PHI]]
2298 define i32 @test20(ptr %var, i32 %cond1, i32 %cond2) {
2299 ; CHECK-LABEL: @test20(
2301 br label %loop_begin
2302 ; CHECK-NEXT: entry:
2303 ; CHECK-NEXT: switch i32 %cond2, label %[[ENTRY_SPLIT_EXIT:.*]] [
2304 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
2305 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_A]]
2306 ; CHECK-NEXT: i32 13, label %[[ENTRY_SPLIT_B:.*]]
2307 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_A]]
2308 ; CHECK-NEXT: i32 42, label %[[ENTRY_SPLIT_C:.*]]
2312 %var_val = load i32, ptr %var
2313 switch i32 %cond2, label %loop_exit [
2314 i32 0, label %loop_a
2315 i32 1, label %loop_a
2316 i32 13, label %loop_b
2317 i32 2, label %loop_a
2318 i32 42, label %loop_c
2323 br label %loop_latch
2324 ; Unswitched 'a' loop.
2326 ; CHECK: [[ENTRY_SPLIT_A]]:
2327 ; CHECK-NEXT: br label %[[LOOP_BEGIN_A:.*]]
2329 ; CHECK: [[LOOP_BEGIN_A]]:
2330 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2331 ; CHECK-NEXT: br label %[[LOOP_A:.*]]
2333 ; CHECK: [[LOOP_A]]:
2334 ; CHECK-NEXT: call i32 @a()
2335 ; CHECK-NEXT: br label %[[LOOP_LATCH_A:.*]]
2337 ; CHECK: [[LOOP_LATCH_A]]:
2338 ; CHECK: br label %[[LOOP_BEGIN_A]]
2342 br label %loop_latch
2343 ; Unswitched 'b' loop.
2345 ; CHECK: [[ENTRY_SPLIT_B]]:
2346 ; CHECK-NEXT: br label %[[LOOP_BEGIN_B:.*]]
2348 ; CHECK: [[LOOP_BEGIN_B]]:
2349 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2350 ; CHECK-NEXT: br label %[[LOOP_B:.*]]
2352 ; CHECK: [[LOOP_B]]:
2353 ; CHECK-NEXT: call i32 @b()
2354 ; CHECK-NEXT: br label %[[LOOP_LATCH_B:.*]]
2356 ; CHECK: [[LOOP_LATCH_B]]:
2357 ; CHECK: br label %[[LOOP_BEGIN_B]]
2360 call i32 @c() noreturn nounwind
2361 br label %loop_latch
2362 ; Unswitched 'c' loop.
2364 ; CHECK: [[ENTRY_SPLIT_C]]:
2365 ; CHECK-NEXT: br label %[[LOOP_BEGIN_C:.*]]
2367 ; CHECK: [[LOOP_BEGIN_C]]:
2368 ; CHECK-NEXT: %{{.*}} = load i32, ptr %var
2369 ; CHECK-NEXT: br label %[[LOOP_C:.*]]
2371 ; CHECK: [[LOOP_C]]:
2372 ; CHECK-NEXT: call i32 @c()
2373 ; CHECK-NEXT: br label %[[LOOP_LATCH_C:.*]]
2375 ; CHECK: [[LOOP_LATCH_C]]:
2376 ; CHECK: br label %[[LOOP_BEGIN_C]]
2379 br label %loop_begin
2382 %lcssa = phi i32 [ %var_val, %loop_begin ]
2384 ; Unswitched exit edge (no longer a loop).
2386 ; CHECK: [[ENTRY_SPLIT_EXIT]]:
2387 ; CHECK-NEXT: br label %loop_begin
2389 ; CHECK: loop_begin:
2390 ; CHECK-NEXT: %[[V:.*]] = load i32, ptr %var
2391 ; CHECK-NEXT: br label %loop_exit
2394 ; CHECK-NEXT: %[[LCSSA:.*]] = phi i32 [ %[[V]], %loop_begin ]
2395 ; CHECK-NEXT: ret i32 %[[LCSSA]]
2398 ; Negative test: we do not switch when the loop contains unstructured control
2399 ; flows as it would significantly complicate the process as novel loops might
2401 define void @test_no_unswitch_unstructured_cfg(ptr %ptr, i1 %cond) {
2402 ; CHECK-LABEL: @test_no_unswitch_unstructured_cfg(
2404 br label %loop_begin
2407 br i1 %cond, label %loop_left, label %loop_right
2410 %v1 = load i1, ptr %ptr
2411 br i1 %v1, label %loop_right, label %loop_merge
2414 %v2 = load i1, ptr %ptr
2415 br i1 %v2, label %loop_left, label %loop_merge
2418 %v3 = load i1, ptr %ptr
2419 br i1 %v3, label %loop_latch, label %loop_exit
2422 br label %loop_begin
2428 ; A test reduced out of 403.gcc with interesting nested loops that trigger
2429 ; multiple unswitches. A key component of this test is that there are multiple
2430 ; paths to reach an inner loop after unswitching, and one of them is via the
2431 ; predecessors of the unswitched loop header. That can allow us to find the loop
2432 ; through multiple different paths.
2433 define void @test21(i1 %a, i1 %b) {
2434 ; CHECK-LABEL: @test21(
2437 ; CHECK-NOT: br i1 %a
2439 ; CHECK: %[[FROZEN:.+]] = freeze i1 %a
2440 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[BB_SPLIT_US:.*]], label %[[BB_SPLIT:.*]]
2442 ; CHECK-NOT: br i1 %a
2443 ; CHECK-NOT: br i1 %b
2445 ; CHECK: [[BB_SPLIT]]:
2448 ; CHECK-NOT: br i1 %a
2449 ; CHECK-NOT: br i1 %b
2452 %tmp1.0 = phi i32 [ 0, %bb ], [ %tmp1.3, %bb23 ]
2456 %tmp.0 = phi i1 [ true, %bb3 ], [ false, %bb19 ]
2457 %tmp1.1 = phi i32 [ %tmp1.0, %bb3 ], [ %tmp1.2.lcssa, %bb19 ]
2458 br i1 %tmp.0, label %bb11.preheader, label %bb23
2461 br i1 %a, label %bb19, label %bb14.lr.ph
2467 %tmp2.02 = phi i32 [ 0, %bb14.lr.ph ], [ 1, %bb14 ]
2468 br i1 %b, label %bb11.bb19_crit_edge, label %bb14
2470 bb11.bb19_crit_edge:
2471 %split = phi i32 [ %tmp2.02, %bb14 ]
2475 %tmp1.2.lcssa = phi i32 [ %split, %bb11.bb19_crit_edge ], [ %tmp1.1, %bb11.preheader ]
2476 %tmp21 = icmp eq i32 %tmp1.2.lcssa, 0
2477 br i1 %tmp21, label %bb23, label %bb7
2480 %tmp1.3 = phi i32 [ %tmp1.2.lcssa, %bb19 ], [ %tmp1.1, %bb7 ]
2484 ; A test reduced out of 400.perlbench that when unswitching the `%stop`
2485 ; condition clones a loop nest outside of a containing loop. This excercises a
2486 ; different cloning path from our other test cases and in turn verifying the
2487 ; resulting structure can catch any failures to correctly clone these nested
2491 declare i32 @h(i32 %arg)
2492 define void @test22(i32 %arg) {
2493 ; CHECK-LABEL: define void @test22(
2495 br label %loop1.header
2498 %stop = phi i1 [ true, %loop1.latch ], [ false, %entry ]
2499 %i = phi i32 [ %i.lcssa, %loop1.latch ], [ %arg, %entry ]
2500 ; CHECK: %[[I:.*]] = phi i32 [ %{{.*}}, %loop1.latch ], [ %arg, %entry ]
2501 br i1 %stop, label %loop1.exit, label %loop1.body.loop2.ph
2502 ; CHECK: br i1 %stop, label %loop1.exit, label %loop1.body.loop2.ph
2504 loop1.body.loop2.ph:
2505 br label %loop2.header
2506 ; Just check that the we unswitched the key condition and that leads to the
2507 ; inner loop header.
2509 ; CHECK: loop1.body.loop2.ph:
2510 ; CHECK-NEXT: br i1 %stop, label %[[SPLIT_US:.*]], label %[[SPLIT:.*]]
2512 ; CHECK: [[SPLIT_US]]:
2513 ; CHECK-NEXT: br label %[[LOOP2_HEADER_US:.*]]
2515 ; CHECK: [[LOOP2_HEADER_US]]:
2516 ; CHECK-NEXT: %{{.*}} = phi i32 [ %[[I]], %[[SPLIT_US]] ]
2519 ; CHECK-NEXT: br label %[[LOOP2_HEADER:.*]]
2521 ; CHECK: [[LOOP2_HEADER]]:
2522 ; CHECK-NEXT: %{{.*}} = phi i32 [ %[[I]], %[[SPLIT]] ]
2525 %i.inner = phi i32 [ %i, %loop1.body.loop2.ph ], [ %i.next, %loop2.latch ]
2526 br label %loop3.header
2529 %sw = call i32 @h(i32 %i.inner)
2530 switch i32 %sw, label %loop3.exit [
2531 i32 32, label %loop3.header
2532 i32 59, label %loop2.latch
2533 i32 36, label %loop1.latch
2537 %i.next = add i32 %i.inner, 1
2538 br i1 %stop, label %loop2.exit, label %loop2.header
2541 %i.lcssa = phi i32 [ %i.inner, %loop3.header ]
2542 br label %loop1.header
2557 ; Test that when we are unswitching and need to rebuild the loop block set we
2558 ; correctly skip past inner loops. We want to use the inner loop to efficiently
2559 ; skip whole subregions of the outer loop blocks but just because the header of
2560 ; the outer loop is also the preheader of an inner loop shouldn't confuse this
2562 define void @test23(i1 %arg, ptr %ptr) {
2563 ; CHECK-LABEL: define void @test23(
2565 br label %outer.header
2567 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %arg
2568 ; CHECK-NEXT: br i1 %[[FROZEN]],
2570 ; Just verify that we unswitched the correct bits. We should call `@f` twice in
2571 ; one unswitch and `@f` and then `@g` in the other.
2583 br label %inner.header
2587 br label %inner.latch
2590 %inner.cond = load i1, ptr %ptr
2591 br i1 %inner.cond, label %inner.header, label %outer.body
2594 br i1 %arg, label %outer.body.left, label %outer.body.right
2598 br label %outer.latch
2602 br label %outer.latch
2605 %outer.cond = load i1, ptr %ptr
2606 br i1 %outer.cond, label %outer.header, label %exit
2612 ; Non-trivial loop unswitching where there are two invariant conditions, but the
2613 ; second one is only in the cloned copy of the loop after unswitching.
2614 define i32 @test24(ptr %ptr, i1 %cond1, i1 %cond2) {
2615 ; CHECK-LABEL: @test24(
2617 br label %loop_begin
2618 ; CHECK-NEXT: entry:
2619 ; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
2622 br i1 %cond1, label %loop_a, label %loop_b
2625 br i1 %cond2, label %loop_a_a, label %loop_a_c
2626 ; The second unswitched condition.
2628 ; CHECK: entry.split.us:
2629 ; CHECK-NEXT: br i1 %cond2, label %entry.split.us.split.us, label %entry.split.us.split
2634 ; The 'loop_a_a' unswitched loop.
2636 ; CHECK: entry.split.us.split.us:
2637 ; CHECK-NEXT: br label %loop_begin.us.us
2639 ; CHECK: loop_begin.us.us:
2640 ; CHECK-NEXT: br label %loop_a.us.us
2642 ; CHECK: loop_a.us.us:
2643 ; CHECK-NEXT: br label %loop_a_a.us.us
2645 ; CHECK: loop_a_a.us.us:
2646 ; CHECK-NEXT: call i32 @a()
2647 ; CHECK-NEXT: br label %latch.us.us
2649 ; CHECK: latch.us.us:
2650 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2651 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us.us, label %loop_exit.split.us.split.us
2653 ; CHECK: loop_exit.split.us.split.us:
2654 ; CHECK-NEXT: br label %loop_exit.split
2659 ; The 'loop_a_c' unswitched loop.
2661 ; CHECK: entry.split.us.split:
2662 ; CHECK-NEXT: br label %loop_begin.us
2664 ; CHECK: loop_begin.us:
2665 ; CHECK-NEXT: br label %loop_a.us
2668 ; CHECK-NEXT: br label %loop_a_c.us
2670 ; CHECK: loop_a_c.us:
2671 ; CHECK-NEXT: call i32 @c()
2672 ; CHECK-NEXT: br label %latch
2675 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2676 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin.us, label %loop_exit.split.us.split
2678 ; CHECK: loop_exit.split.us.split:
2679 ; CHECK-NEXT: br label %loop_exit.split
2684 ; The 'loop_b' unswitched loop.
2686 ; CHECK: entry.split:
2687 ; CHECK-NEXT: br label %loop_begin
2689 ; CHECK: loop_begin:
2690 ; CHECK-NEXT: br label %loop_b
2693 ; CHECK-NEXT: call i32 @b()
2694 ; CHECK-NEXT: br label %latch
2697 ; CHECK-NEXT: %[[V:.*]] = load i1, ptr %ptr
2698 ; CHECK-NEXT: br i1 %[[V]], label %loop_begin, label %loop_exit.split
2700 ; CHECK: loop_exit.split:
2701 ; CHECK-NEXT: br label %loop_exit
2704 %v = load i1, ptr %ptr
2705 br i1 %v, label %loop_begin, label %loop_exit
2713 ; Non-trivial partial loop unswitching of an invariant input to an 'or'.
2714 define i32 @test25(ptr %ptr, i1 %cond) {
2715 ; CHECK-LABEL: @test25(
2717 br label %loop_begin
2718 ; CHECK-NEXT: entry:
2719 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %cond
2720 ; CHECK-NEXT: br i1 [[FROZEN]], label %entry.split.us, label %entry.split
2723 %v1 = load i1, ptr %ptr
2724 %cond_or = or i1 %v1, %cond
2725 br i1 %cond_or, label %loop_a, label %loop_b
2730 ; The 'loop_a' unswitched loop.
2732 ; CHECK: entry.split.us:
2733 ; CHECK-NEXT: br label %loop_begin.us
2735 ; CHECK: loop_begin.us:
2736 ; CHECK-NEXT: br label %loop_a.us
2739 ; CHECK-NEXT: call i32 @a()
2740 ; CHECK-NEXT: br label %latch.us
2743 ; CHECK-NEXT: %[[V2_US:.*]] = load i1, ptr %ptr
2744 ; CHECK-NEXT: br i1 %[[V2_US]], label %loop_begin.us, label %loop_exit.split.us
2746 ; CHECK: loop_exit.split.us:
2747 ; CHECK-NEXT: br label %loop_exit
2752 ; The original loop.
2754 ; CHECK: entry.split:
2755 ; CHECK-NEXT: br label %loop_begin
2757 ; CHECK: loop_begin:
2758 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr
2759 ; CHECK-NEXT: %[[OR:.*]] = or i1 %[[V1]], false
2760 ; CHECK-NEXT: br i1 %[[OR]], label %loop_a, label %loop_b
2763 ; CHECK-NEXT: call i32 @a()
2764 ; CHECK-NEXT: br label %latch
2767 ; CHECK-NEXT: call i32 @b()
2768 ; CHECK-NEXT: br label %latch
2771 %v2 = load i1, ptr %ptr
2772 br i1 %v2, label %loop_begin, label %loop_exit
2774 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr
2775 ; CHECK-NEXT: br i1 %[[V2]], label %loop_begin, label %loop_exit.split
2779 ; CHECK: loop_exit.split:
2780 ; CHECK-NEXT: br label %loop_exit
2786 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `and`
2788 define i32 @test26(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2, i1 %cond3) {
2789 ; CHECK-LABEL: @test26(
2791 br label %loop_begin
2792 ; CHECK-NEXT: entry:
2793 ; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2794 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2795 ; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C3_FR]], [[C1_FR]]
2796 ; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
2799 %v1 = load i1, ptr %ptr1
2800 %v2 = load i1, ptr %ptr2
2801 %cond_and1 = and i1 %v1, %cond1
2802 %cond_or1 = or i1 %v2, %cond2
2803 %cond_and2 = and i1 %cond_and1, %cond_or1
2804 %cond_and3 = and i1 %cond_and2, %cond3
2805 br i1 %cond_and3, label %loop_a, label %loop_b
2806 ; The 'loop_b' unswitched loop.
2808 ; CHECK: entry.split.us:
2809 ; CHECK-NEXT: br label %loop_begin.us
2811 ; CHECK: loop_begin.us:
2812 ; CHECK-NEXT: br label %loop_b.us
2815 ; CHECK-NEXT: call i32 @b()
2816 ; CHECK-NEXT: br label %latch.us
2819 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3
2820 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
2822 ; CHECK: loop_exit.split.us:
2823 ; CHECK-NEXT: br label %loop_exit
2825 ; The original loop.
2827 ; CHECK: entry.split:
2828 ; CHECK-NEXT: br label %loop_begin
2830 ; CHECK: loop_begin:
2831 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
2832 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
2833 ; CHECK-NEXT: %[[AND1:.*]] = and i1 %[[V1]], true
2834 ; CHECK-NEXT: %[[OR1:.*]] = or i1 %[[V2]], %cond2
2835 ; CHECK-NEXT: %[[AND2:.*]] = and i1 %[[AND1]], %[[OR1]]
2836 ; CHECK-NEXT: %[[AND3:.*]] = and i1 %[[AND2]], true
2837 ; CHECK-NEXT: br i1 %[[AND3]], label %loop_a, label %loop_b
2843 ; CHECK-NEXT: call i32 @a()
2844 ; CHECK-NEXT: br label %latch
2850 ; CHECK-NEXT: call i32 @b()
2851 ; CHECK-NEXT: br label %latch
2854 %v3 = load i1, ptr %ptr3
2855 br i1 %v3, label %loop_begin, label %loop_exit
2857 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3
2858 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
2862 ; CHECK: loop_exit.split:
2863 ; CHECK-NEXT: br label %loop_exit
2869 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `or`
2870 ; chain. Basically an inverted version of corresponding `and` test (test26).
2871 define i32 @test27(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2, i1 %cond3) {
2872 ; CHECK-LABEL: @test27(
2874 br label %loop_begin
2875 ; CHECK-NEXT: entry:
2876 ; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2877 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2878 ; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C3_FR]], [[C1_FR]]
2879 ; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
2882 %v1 = load i1, ptr %ptr1
2883 %v2 = load i1, ptr %ptr2
2884 %cond_or1 = or i1 %v1, %cond1
2885 %cond_and1 = and i1 %v2, %cond2
2886 %cond_or2 = or i1 %cond_or1, %cond_and1
2887 %cond_or3 = or i1 %cond_or2, %cond3
2888 br i1 %cond_or3, label %loop_b, label %loop_a
2889 ; The 'loop_b' unswitched loop.
2891 ; CHECK: entry.split.us:
2892 ; CHECK-NEXT: br label %loop_begin.us
2894 ; CHECK: loop_begin.us:
2895 ; CHECK-NEXT: br label %loop_b.us
2898 ; CHECK-NEXT: call i32 @b()
2899 ; CHECK-NEXT: br label %latch.us
2902 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3
2903 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
2905 ; CHECK: loop_exit.split.us:
2906 ; CHECK-NEXT: br label %loop_exit
2908 ; The original loop.
2910 ; CHECK: entry.split:
2911 ; CHECK-NEXT: br label %loop_begin
2913 ; CHECK: loop_begin:
2914 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
2915 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
2916 ; CHECK-NEXT: %[[OR1:.*]] = or i1 %[[V1]], false
2917 ; CHECK-NEXT: %[[AND1:.*]] = and i1 %[[V2]], %cond2
2918 ; CHECK-NEXT: %[[OR2:.*]] = or i1 %[[OR1]], %[[AND1]]
2919 ; CHECK-NEXT: %[[OR3:.*]] = or i1 %[[OR2]], false
2920 ; CHECK-NEXT: br i1 %[[OR3]], label %loop_b, label %loop_a
2926 ; CHECK-NEXT: call i32 @a()
2927 ; CHECK-NEXT: br label %latch
2933 ; CHECK-NEXT: call i32 @b()
2934 ; CHECK-NEXT: br label %latch
2937 %v3 = load i1, ptr %ptr3
2938 br i1 %v3, label %loop_begin, label %loop_exit
2940 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3
2941 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
2945 ; CHECK: loop_exit.split:
2946 ; CHECK-NEXT: br label %loop_exit
2952 ; Non-trivial unswitching of a switch.
2953 define i32 @test28(ptr %ptr, i32 %cond) {
2954 ; CHECK-LABEL: @test28(
2956 br label %loop_begin
2957 ; CHECK-NEXT: entry:
2958 ; CHECK-NEXT: switch i32 %cond, label %[[ENTRY_SPLIT_LATCH:.*]] [
2959 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
2960 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_B:.*]]
2961 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_C:.*]]
2965 switch i32 %cond, label %latch [
2966 i32 0, label %loop_a
2967 i32 1, label %loop_b
2968 i32 2, label %loop_c
2974 ; Unswitched 'a' loop.
2976 ; CHECK: [[ENTRY_SPLIT_A]]:
2977 ; CHECK-NEXT: br label %[[LOOP_BEGIN_A:.*]]
2979 ; CHECK: [[LOOP_BEGIN_A]]:
2980 ; CHECK-NEXT: br label %[[LOOP_A:.*]]
2982 ; CHECK: [[LOOP_A]]:
2983 ; CHECK-NEXT: call i32 @a()
2984 ; CHECK-NEXT: br label %[[LOOP_LATCH_A:.*]]
2986 ; CHECK: [[LOOP_LATCH_A]]:
2987 ; CHECK-NEXT: %[[V_A:.*]] = load i1, ptr %ptr
2988 ; CHECK: br i1 %[[V_A]], label %[[LOOP_BEGIN_A]], label %[[LOOP_EXIT_A:.*]]
2990 ; CHECK: [[LOOP_EXIT_A]]:
2991 ; CHECK-NEXT: br label %loop_exit
2996 ; Unswitched 'b' loop.
2998 ; CHECK: [[ENTRY_SPLIT_B]]:
2999 ; CHECK-NEXT: br label %[[LOOP_BEGIN_B:.*]]
3001 ; CHECK: [[LOOP_BEGIN_B]]:
3002 ; CHECK-NEXT: br label %[[LOOP_B:.*]]
3004 ; CHECK: [[LOOP_B]]:
3005 ; CHECK-NEXT: call i32 @b()
3006 ; CHECK-NEXT: br label %[[LOOP_LATCH_B:.*]]
3008 ; CHECK: [[LOOP_LATCH_B]]:
3009 ; CHECK-NEXT: %[[V_B:.*]] = load i1, ptr %ptr
3010 ; CHECK: br i1 %[[V_B]], label %[[LOOP_BEGIN_B]], label %[[LOOP_EXIT_B:.*]]
3012 ; CHECK: [[LOOP_EXIT_B]]:
3013 ; CHECK-NEXT: br label %loop_exit
3018 ; Unswitched 'c' loop.
3020 ; CHECK: [[ENTRY_SPLIT_C]]:
3021 ; CHECK-NEXT: br label %[[LOOP_BEGIN_C:.*]]
3023 ; CHECK: [[LOOP_BEGIN_C]]:
3024 ; CHECK-NEXT: br label %[[LOOP_C:.*]]
3026 ; CHECK: [[LOOP_C]]:
3027 ; CHECK-NEXT: call i32 @c()
3028 ; CHECK-NEXT: br label %[[LOOP_LATCH_C:.*]]
3030 ; CHECK: [[LOOP_LATCH_C]]:
3031 ; CHECK-NEXT: %[[V_C:.*]] = load i1, ptr %ptr
3032 ; CHECK: br i1 %[[V_C]], label %[[LOOP_BEGIN_C]], label %[[LOOP_EXIT_C:.*]]
3034 ; CHECK: [[LOOP_EXIT_C]]:
3035 ; CHECK-NEXT: br label %loop_exit
3038 %v = load i1, ptr %ptr
3039 br i1 %v, label %loop_begin, label %loop_exit
3040 ; Unswitched the 'latch' only loop.
3042 ; CHECK: [[ENTRY_SPLIT_LATCH]]:
3043 ; CHECK-NEXT: br label %[[LOOP_BEGIN_LATCH:.*]]
3045 ; CHECK: [[LOOP_BEGIN_LATCH]]:
3046 ; CHECK-NEXT: br label %[[LOOP_LATCH_LATCH:.*]]
3048 ; CHECK: [[LOOP_LATCH_LATCH]]:
3049 ; CHECK-NEXT: %[[V_LATCH:.*]] = load i1, ptr %ptr
3050 ; CHECK: br i1 %[[V_LATCH]], label %[[LOOP_BEGIN_LATCH]], label %[[LOOP_EXIT_LATCH:.*]]
3052 ; CHECK: [[LOOP_EXIT_LATCH]]:
3053 ; CHECK-NEXT: br label %loop_exit
3058 ; CHECK-NEXT: ret i32 0
3061 ; A test case designed to exercise unusual properties of switches: they
3062 ; can introduce multiple edges to successors. These need lots of special case
3063 ; handling as they get collapsed in many cases (domtree, the unswitch itself)
3064 ; but not in all cases (the PHI node operands).
3065 define i32 @test29(i32 %arg) {
3066 ; CHECK-LABEL: @test29(
3069 ; CHECK-NEXT: entry:
3070 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i32 %arg
3071 ; CHECK-NEXT: switch i32 %[[FROZEN]], label %[[ENTRY_SPLIT_C:.*]] [
3072 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
3073 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_A]]
3074 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_B:.*]]
3075 ; CHECK-NEXT: i32 3, label %[[ENTRY_SPLIT_C]]
3079 %tmp = call i32 @d()
3080 %cmp1 = icmp eq i32 %tmp, 0
3081 ; We set up a chain through all the successors of the switch that doesn't
3082 ; involve the switch so that we can have interesting PHI nodes in them.
3083 br i1 %cmp1, label %body.a, label %dispatch
3086 ; Switch with multiple successors. We arrange the last successor to be the
3087 ; default to make the test case easier to read. This has a duplicate edge
3088 ; both to the default destination (which is completely superfluous but
3089 ; technically valid IR) and to a regular successor.
3090 switch i32 %arg, label %body.c [
3091 i32 0, label %body.a
3092 i32 1, label %body.a
3093 i32 2, label %body.b
3094 i32 3, label %body.c
3098 %tmp.a.phi = phi i32 [ 0, %header ], [ %tmp, %dispatch ], [ %tmp, %dispatch ]
3099 %tmp.a = call i32 @a()
3100 %tmp.a.sum = add i32 %tmp.a.phi, %tmp.a
3102 ; Unswitched 'a' loop.
3104 ; CHECK: [[ENTRY_SPLIT_A]]:
3105 ; CHECK-NEXT: br label %[[HEADER_A:.*]]
3107 ; CHECK: [[HEADER_A]]:
3108 ; CHECK-NEXT: %[[TMP_A:.*]] = call i32 @d()
3109 ; CHECK-NEXT: %[[CMP1_A:.*]] = icmp eq i32 %[[TMP_A]], 0
3110 ; CHECK-NEXT: br i1 %[[CMP1_A]], label %[[BODY_A_A:.*]], label %[[DISPATCH_A:.*]]
3112 ; CHECK: [[DISPATCH_A]]:
3113 ; CHECK-NEXT: br label %[[BODY_A_A]]
3115 ; CHECK: [[BODY_A_A]]:
3116 ; CHECK-NEXT: %[[TMP_A_PHI_A:.*]] = phi i32 [ 0, %[[HEADER_A]] ], [ %[[TMP_A]], %[[DISPATCH_A]] ]
3117 ; CHECK-NEXT: %[[TMP_A_A:.*]] = call i32 @a()
3118 ; CHECK-NEXT: %[[TMP_A_SUM_A:.*]] = add i32 %[[TMP_A_PHI_A]], %[[TMP_A_A]]
3119 ; CHECK-NEXT: br label %[[BODY_B_A:.*]]
3121 ; CHECK: [[BODY_B_A]]:
3122 ; CHECK-NEXT: %[[TMP_B_PHI_A:.*]] = phi i32 [ %[[TMP_A_SUM_A]], %[[BODY_A_A]] ]
3123 ; CHECK-NEXT: %[[TMP_B_A:.*]] = call i32 @b()
3124 ; CHECK-NEXT: %[[TMP_B_SUM_A:.*]] = add i32 %[[TMP_B_PHI_A]], %[[TMP_B_A]]
3125 ; CHECK-NEXT: br label %[[BODY_C_A:.*]]
3127 ; CHECK: [[BODY_C_A]]:
3128 ; CHECK-NEXT: %[[TMP_C_PHI_A:.*]] = phi i32 [ %[[TMP_B_SUM_A]], %[[BODY_B_A]] ]
3129 ; CHECK-NEXT: %[[TMP_C_A:.*]] = call i32 @c()
3130 ; CHECK-NEXT: %[[TMP_C_SUM_A:.*]] = add i32 %[[TMP_C_PHI_A]], %[[TMP_C_A]]
3131 ; CHECK-NEXT: br label %[[LATCH_A:.*]]
3133 ; CHECK: [[LATCH_A]]:
3134 ; CHECK-NEXT: %[[CMP2_A:.*]] = icmp slt i32 %[[TMP_C_SUM_A]], 42
3135 ; CHECK: br i1 %[[CMP2_A]], label %[[HEADER_A]], label %[[LOOP_EXIT_A:.*]]
3137 ; CHECK: [[LOOP_EXIT_A]]:
3138 ; CHECK-NEXT: %[[LCSSA_A:.*]] = phi i32 [ %[[TMP_C_SUM_A]], %[[LATCH_A]] ]
3139 ; CHECK-NEXT: br label %exit
3142 %tmp.b.phi = phi i32 [ %tmp, %dispatch ], [ %tmp.a.sum, %body.a ]
3143 %tmp.b = call i32 @b()
3144 %tmp.b.sum = add i32 %tmp.b.phi, %tmp.b
3146 ; Unswitched 'b' loop.
3148 ; CHECK: [[ENTRY_SPLIT_B]]:
3149 ; CHECK-NEXT: br label %[[HEADER_B:.*]]
3151 ; CHECK: [[HEADER_B]]:
3152 ; CHECK-NEXT: %[[TMP_B:.*]] = call i32 @d()
3153 ; CHECK-NEXT: %[[CMP1_B:.*]] = icmp eq i32 %[[TMP_B]], 0
3154 ; CHECK-NEXT: br i1 %[[CMP1_B]], label %[[BODY_A_B:.*]], label %[[DISPATCH_B:.*]]
3156 ; CHECK: [[DISPATCH_B]]:
3157 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3159 ; CHECK: [[BODY_A_B]]:
3160 ; CHECK-NEXT: %[[TMP_A_PHI_B:.*]] = phi i32 [ 0, %[[HEADER_B]] ]
3161 ; CHECK-NEXT: %[[TMP_A_B:.*]] = call i32 @a()
3162 ; CHECK-NEXT: %[[TMP_A_SUM_B:.*]] = add i32 %[[TMP_A_PHI_B]], %[[TMP_A_B]]
3163 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3165 ; CHECK: [[BODY_B_B]]:
3166 ; CHECK-NEXT: %[[TMP_B_PHI_B:.*]] = phi i32 [ %[[TMP_B]], %[[DISPATCH_B]] ], [ %[[TMP_A_SUM_B]], %[[BODY_A_B]] ]
3167 ; CHECK-NEXT: %[[TMP_B_B:.*]] = call i32 @b()
3168 ; CHECK-NEXT: %[[TMP_B_SUM_B:.*]] = add i32 %[[TMP_B_PHI_B]], %[[TMP_B_B]]
3169 ; CHECK-NEXT: br label %[[BODY_C_B:.*]]
3171 ; CHECK: [[BODY_C_B]]:
3172 ; CHECK-NEXT: %[[TMP_C_PHI_B:.*]] = phi i32 [ %[[TMP_B_SUM_B]], %[[BODY_B_B]] ]
3173 ; CHECK-NEXT: %[[TMP_C_B:.*]] = call i32 @c()
3174 ; CHECK-NEXT: %[[TMP_C_SUM_B:.*]] = add i32 %[[TMP_C_PHI_B]], %[[TMP_C_B]]
3175 ; CHECK-NEXT: br label %[[LATCH_B:.*]]
3177 ; CHECK: [[LATCH_B]]:
3178 ; CHECK-NEXT: %[[CMP2_B:.*]] = icmp slt i32 %[[TMP_C_SUM_B]], 42
3179 ; CHECK: br i1 %[[CMP2_B]], label %[[HEADER_B]], label %[[LOOP_EXIT_B:.*]]
3181 ; CHECK: [[LOOP_EXIT_B]]:
3182 ; CHECK-NEXT: %[[LCSSA_B:.*]] = phi i32 [ %[[TMP_C_SUM_B]], %[[LATCH_B]] ]
3183 ; CHECK-NEXT: br label %[[EXIT_SPLIT:.*]]
3186 %tmp.c.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ], [ %tmp.b.sum, %body.b ]
3187 %tmp.c = call i32 @c()
3188 %tmp.c.sum = add i32 %tmp.c.phi, %tmp.c
3190 ; Unswitched 'c' loop.
3192 ; CHECK: [[ENTRY_SPLIT_C]]:
3193 ; CHECK-NEXT: br label %[[HEADER_C:.*]]
3195 ; CHECK: [[HEADER_C]]:
3196 ; CHECK-NEXT: %[[TMP_C:.*]] = call i32 @d()
3197 ; CHECK-NEXT: %[[CMP1_C:.*]] = icmp eq i32 %[[TMP_C]], 0
3198 ; CHECK-NEXT: br i1 %[[CMP1_C]], label %[[BODY_A_C:.*]], label %[[DISPATCH_C:.*]]
3200 ; CHECK: [[DISPATCH_C]]:
3201 ; CHECK-NEXT: br label %[[BODY_C_C:.*]]
3203 ; CHECK: [[BODY_A_C]]:
3204 ; CHECK-NEXT: %[[TMP_A_PHI_C:.*]] = phi i32 [ 0, %[[HEADER_C]] ]
3205 ; CHECK-NEXT: %[[TMP_A_C:.*]] = call i32 @a()
3206 ; CHECK-NEXT: %[[TMP_A_SUM_C:.*]] = add i32 %[[TMP_A_PHI_C]], %[[TMP_A_C]]
3207 ; CHECK-NEXT: br label %[[BODY_B_C:.*]]
3209 ; CHECK: [[BODY_B_C]]:
3210 ; CHECK-NEXT: %[[TMP_B_PHI_C:.*]] = phi i32 [ %[[TMP_A_SUM_C]], %[[BODY_A_C]] ]
3211 ; CHECK-NEXT: %[[TMP_B_C:.*]] = call i32 @b()
3212 ; CHECK-NEXT: %[[TMP_B_SUM_C:.*]] = add i32 %[[TMP_B_PHI_C]], %[[TMP_B_C]]
3213 ; CHECK-NEXT: br label %[[BODY_C_C:.*]]
3215 ; CHECK: [[BODY_C_C]]:
3216 ; CHECK-NEXT: %[[TMP_C_PHI_C:.*]] = phi i32 [ %[[TMP_C]], %[[DISPATCH_C]] ], [ %[[TMP_B_SUM_C]], %[[BODY_B_C]] ]
3217 ; CHECK-NEXT: %[[TMP_C_C:.*]] = call i32 @c()
3218 ; CHECK-NEXT: %[[TMP_C_SUM_C:.*]] = add i32 %[[TMP_C_PHI_C]], %[[TMP_C_C]]
3219 ; CHECK-NEXT: br label %[[LATCH_C:.*]]
3221 ; CHECK: [[LATCH_C]]:
3222 ; CHECK-NEXT: %[[CMP2_C:.*]] = icmp slt i32 %[[TMP_C_SUM_C]], 42
3223 ; CHECK: br i1 %[[CMP2_C]], label %[[HEADER_C]], label %[[LOOP_EXIT_C:.*]]
3225 ; CHECK: [[LOOP_EXIT_C]]:
3226 ; CHECK-NEXT: %[[LCSSA_C:.*]] = phi i32 [ %[[TMP_C_SUM_C]], %[[LATCH_C]] ]
3227 ; CHECK-NEXT: br label %[[EXIT_SPLIT]]
3230 %cmp2 = icmp slt i32 %tmp.c.sum, 42
3231 br i1 %cmp2, label %header, label %exit
3234 %lcssa.phi = phi i32 [ %tmp.c.sum, %latch ]
3236 ; CHECK: [[EXIT_SPLIT]]:
3237 ; CHECK-NEXT: %[[EXIT_PHI1:.*]] = phi i32 [ %[[LCSSA_C]], %[[LOOP_EXIT_C]] ], [ %[[LCSSA_B]], %[[LOOP_EXIT_B]] ]
3238 ; CHECK-NEXT: br label %exit
3241 ; CHECK-NEXT: %[[EXIT_PHI2:.*]] = phi i32 [ %[[EXIT_PHI1]], %[[EXIT_SPLIT]] ], [ %[[LCSSA_A]], %[[LOOP_EXIT_A]] ]
3242 ; CHECK-NEXT: ret i32 %[[EXIT_PHI2]]
3245 ; Similar to @test29 but designed to have one of the duplicate edges be
3246 ; a loop exit edge as those can in some cases be special. Among other things,
3247 ; this includes an LCSSA phi with multiple entries despite being a dedicated
3249 define i32 @test30(i32 %arg) {
3250 ; CHECK-LABEL: define i32 @test30(
3253 ; CHECK-NEXT: entry:
3254 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i32 %arg
3255 ; CHECK-NEXT: switch i32 %[[FROZEN]], label %[[ENTRY_SPLIT_EXIT:.*]] [
3256 ; CHECK-NEXT: i32 -1, label %[[ENTRY_SPLIT_EXIT]]
3257 ; CHECK-NEXT: i32 0, label %[[ENTRY_SPLIT_A:.*]]
3258 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_B:.*]]
3259 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_B]]
3263 %tmp = call i32 @d()
3264 %cmp1 = icmp eq i32 %tmp, 0
3265 br i1 %cmp1, label %body.a, label %dispatch
3268 switch i32 %arg, label %loop.exit1 [
3269 i32 -1, label %loop.exit1
3270 i32 0, label %body.a
3271 i32 1, label %body.b
3272 i32 2, label %body.b
3276 %tmp.a.phi = phi i32 [ 0, %header ], [ %tmp, %dispatch ]
3277 %tmp.a = call i32 @a()
3278 %tmp.a.sum = add i32 %tmp.a.phi, %tmp.a
3280 ; Unswitched 'a' loop.
3282 ; CHECK: [[ENTRY_SPLIT_A]]:
3283 ; CHECK-NEXT: br label %[[HEADER_A:.*]]
3285 ; CHECK: [[HEADER_A]]:
3286 ; CHECK-NEXT: %[[TMP_A:.*]] = call i32 @d()
3287 ; CHECK-NEXT: %[[CMP1_A:.*]] = icmp eq i32 %[[TMP_A]], 0
3288 ; CHECK-NEXT: br i1 %[[CMP1_A]], label %[[BODY_A_A:.*]], label %[[DISPATCH_A:.*]]
3290 ; CHECK: [[DISPATCH_A]]:
3291 ; CHECK-NEXT: br label %[[BODY_A_A]]
3293 ; CHECK: [[BODY_A_A]]:
3294 ; CHECK-NEXT: %[[TMP_A_PHI_A:.*]] = phi i32 [ 0, %[[HEADER_A]] ], [ %[[TMP_A]], %[[DISPATCH_A]] ]
3295 ; CHECK-NEXT: %[[TMP_A_A:.*]] = call i32 @a()
3296 ; CHECK-NEXT: %[[TMP_A_SUM_A:.*]] = add i32 %[[TMP_A_PHI_A]], %[[TMP_A_A]]
3297 ; CHECK-NEXT: br label %[[BODY_B_A:.*]]
3299 ; CHECK: [[BODY_B_A]]:
3300 ; CHECK-NEXT: %[[TMP_B_PHI_A:.*]] = phi i32 [ %[[TMP_A_SUM_A]], %[[BODY_A_A]] ]
3301 ; CHECK-NEXT: %[[TMP_B_A:.*]] = call i32 @b()
3302 ; CHECK-NEXT: %[[TMP_B_SUM_A:.*]] = add i32 %[[TMP_B_PHI_A]], %[[TMP_B_A]]
3303 ; CHECK-NEXT: br label %[[LATCH_A:.*]]
3305 ; CHECK: [[LATCH_A]]:
3306 ; CHECK-NEXT: %[[CMP2_A:.*]] = icmp slt i32 %[[TMP_B_SUM_A]], 42
3307 ; CHECK: br i1 %[[CMP2_A]], label %[[HEADER_A]], label %[[LOOP_EXIT_A:.*]]
3309 ; CHECK: [[LOOP_EXIT_A]]:
3310 ; CHECK-NEXT: %[[LCSSA_A:.*]] = phi i32 [ %[[TMP_B_SUM_A]], %[[LATCH_A]] ]
3311 ; CHECK-NEXT: br label %loop.exit2
3314 %tmp.b.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ], [ %tmp.a.sum, %body.a ]
3315 %tmp.b = call i32 @b()
3316 %tmp.b.sum = add i32 %tmp.b.phi, %tmp.b
3318 ; Unswitched 'b' loop.
3320 ; CHECK: [[ENTRY_SPLIT_B]]:
3321 ; CHECK-NEXT: br label %[[HEADER_B:.*]]
3323 ; CHECK: [[HEADER_B]]:
3324 ; CHECK-NEXT: %[[TMP_B:.*]] = call i32 @d()
3325 ; CHECK-NEXT: %[[CMP1_B:.*]] = icmp eq i32 %[[TMP_B]], 0
3326 ; CHECK-NEXT: br i1 %[[CMP1_B]], label %[[BODY_A_B:.*]], label %[[DISPATCH_B:.*]]
3328 ; CHECK: [[DISPATCH_B]]:
3329 ; CHECK-NEXT: br label %[[BODY_B_B]]
3331 ; CHECK: [[BODY_A_B]]:
3332 ; CHECK-NEXT: %[[TMP_A_PHI_B:.*]] = phi i32 [ 0, %[[HEADER_B]] ]
3333 ; CHECK-NEXT: %[[TMP_A_B:.*]] = call i32 @a()
3334 ; CHECK-NEXT: %[[TMP_A_SUM_B:.*]] = add i32 %[[TMP_A_PHI_B]], %[[TMP_A_B]]
3335 ; CHECK-NEXT: br label %[[BODY_B_B:.*]]
3337 ; CHECK: [[BODY_B_B]]:
3338 ; CHECK-NEXT: %[[TMP_B_PHI_B:.*]] = phi i32 [ %[[TMP_B]], %[[DISPATCH_B]] ], [ %[[TMP_A_SUM_B]], %[[BODY_A_B]] ]
3339 ; CHECK-NEXT: %[[TMP_B_B:.*]] = call i32 @b()
3340 ; CHECK-NEXT: %[[TMP_B_SUM_B:.*]] = add i32 %[[TMP_B_PHI_B]], %[[TMP_B_B]]
3341 ; CHECK-NEXT: br label %[[LATCH_B:.*]]
3343 ; CHECK: [[LATCH_B]]:
3344 ; CHECK-NEXT: %[[CMP2_B:.*]] = icmp slt i32 %[[TMP_B_SUM_B]], 42
3345 ; CHECK: br i1 %[[CMP2_B]], label %[[HEADER_B]], label %[[LOOP_EXIT_B:.*]]
3347 ; CHECK: [[LOOP_EXIT_B]]:
3348 ; CHECK-NEXT: %[[LCSSA_B:.*]] = phi i32 [ %[[TMP_B_SUM_B]], %[[LATCH_B]] ]
3349 ; CHECK-NEXT: br label %[[LOOP_EXIT2_SPLIT:.*]]
3352 %cmp2 = icmp slt i32 %tmp.b.sum, 42
3353 br i1 %cmp2, label %header, label %loop.exit2
3356 %l1.phi = phi i32 [ %tmp, %dispatch ], [ %tmp, %dispatch ]
3358 ; Unswitched 'exit' loop.
3360 ; CHECK: [[ENTRY_SPLIT_EXIT]]:
3361 ; CHECK-NEXT: br label %[[HEADER_EXIT:.*]]
3363 ; CHECK: [[HEADER_EXIT]]:
3364 ; CHECK-NEXT: %[[TMP_EXIT:.*]] = call i32 @d()
3365 ; CHECK-NEXT: %[[CMP1_EXIT:.*]] = icmp eq i32 %[[TMP_EXIT]], 0
3366 ; CHECK-NEXT: br i1 %[[CMP1_EXIT]], label %[[BODY_A_EXIT:.*]], label %[[DISPATCH_EXIT:.*]]
3368 ; CHECK: [[DISPATCH_EXIT]]:
3369 ; CHECK-NEXT: %[[TMP_LCSSA:.*]] = phi i32 [ %[[TMP_EXIT]], %[[HEADER_EXIT]] ]
3370 ; CHECK-NEXT: br label %loop.exit1
3372 ; CHECK: [[BODY_A_EXIT]]:
3373 ; CHECK-NEXT: %[[TMP_A_PHI_EXIT:.*]] = phi i32 [ 0, %[[HEADER_EXIT]] ]
3374 ; CHECK-NEXT: %[[TMP_A_EXIT:.*]] = call i32 @a()
3375 ; CHECK-NEXT: %[[TMP_A_SUM_EXIT:.*]] = add i32 %[[TMP_A_PHI_EXIT]], %[[TMP_A_EXIT]]
3376 ; CHECK-NEXT: br label %[[BODY_B_EXIT:.*]]
3378 ; CHECK: [[BODY_B_EXIT]]:
3379 ; CHECK-NEXT: %[[TMP_B_PHI_EXIT:.*]] = phi i32 [ %[[TMP_A_SUM_EXIT]], %[[BODY_A_EXIT]] ]
3380 ; CHECK-NEXT: %[[TMP_B_EXIT:.*]] = call i32 @b()
3381 ; CHECK-NEXT: %[[TMP_B_SUM_EXIT:.*]] = add i32 %[[TMP_B_PHI_EXIT]], %[[TMP_B_EXIT]]
3382 ; CHECK-NEXT: br label %[[LATCH_EXIT:.*]]
3384 ; CHECK: [[LATCH_EXIT]]:
3385 ; CHECK-NEXT: %[[CMP2_EXIT:.*]] = icmp slt i32 %[[TMP_B_SUM_EXIT]], 42
3386 ; CHECK: br i1 %[[CMP2_EXIT]], label %[[HEADER_EXIT]], label %[[LOOP_EXIT_EXIT:.*]]
3388 ; CHECK: loop.exit1:
3389 ; CHECK-NEXT: %[[L1_PHI:.*]] = phi i32 [ %[[TMP_LCSSA]], %[[DISPATCH_EXIT]] ]
3390 ; CHECK-NEXT: br label %exit
3392 ; CHECK: [[LOOP_EXIT_EXIT]]:
3393 ; CHECK-NEXT: %[[L2_PHI:.*]] = phi i32 [ %[[TMP_B_SUM_EXIT]], %[[LATCH_EXIT]] ]
3394 ; CHECK-NEXT: br label %[[LOOP_EXIT2_SPLIT]]
3397 %l2.phi = phi i32 [ %tmp.b.sum, %latch ]
3399 ; CHECK: [[LOOP_EXIT2_SPLIT]]:
3400 ; CHECK-NEXT: %[[LOOP_EXIT_PHI1:.*]] = phi i32 [ %[[L2_PHI]], %[[LOOP_EXIT_EXIT]] ], [ %[[LCSSA_B]], %[[LOOP_EXIT_B]] ]
3401 ; CHECK-NEXT: br label %loop.exit2
3403 ; CHECK: loop.exit2:
3404 ; CHECK-NEXT: %[[LOOP_EXIT_PHI2:.*]] = phi i32 [ %[[LOOP_EXIT_PHI1]], %[[LOOP_EXIT2_SPLIT]] ], [ %[[LCSSA_A]], %[[LOOP_EXIT_A]] ]
3405 ; CHECK-NEXT: br label %exit
3408 %l.phi = phi i32 [ %l1.phi, %loop.exit1 ], [ %l2.phi, %loop.exit2 ]
3411 ; CHECK-NEXT: %[[EXIT_PHI:.*]] = phi i32 [ %[[L1_PHI]], %loop.exit1 ], [ %[[LOOP_EXIT_PHI2]], %loop.exit2 ]
3412 ; CHECK-NEXT: ret i32 %[[EXIT_PHI]]
3415 ; Unswitch will not actually change the loop nest from:
3417 define void @hoist_inner_loop0() {
3418 ; CHECK-LABEL: define void @hoist_inner_loop0(
3422 ; CHECK-NEXT: br label %a.header
3427 ; CHECK-NEXT: br label %b.header
3430 %v1 = call i1 @cond()
3433 ; CHECK-NEXT: %v1 = call i1 @cond()
3434 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3435 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3437 ; CHECK: [[B_HEADER_SPLIT_US]]:
3438 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3440 ; CHECK: [[C_HEADER_US]]:
3441 ; CHECK-NEXT: call i32 @c()
3442 ; CHECK-NEXT: br label %[[B_LATCH_SPLIT_US:.*]]
3444 ; CHECK: [[B_LATCH_SPLIT_US]]:
3445 ; CHECK-NEXT: br label %b.latch
3447 ; CHECK: [[B_HEADER_SPLIT]]:
3448 ; CHECK-NEXT: br label %c.header
3452 br i1 %v1, label %b.latch, label %c.latch
3454 ; CHECK-NEXT: call i32 @c()
3455 ; CHECK-NEXT: br label %c.latch
3458 %v2 = call i1 @cond()
3459 br i1 %v2, label %c.header, label %b.latch
3461 ; CHECK-NEXT: %v2 = call i1 @cond()
3462 ; CHECK-NEXT: br i1 %v2, label %c.header, label %[[B_LATCH_SPLIT:.*]]
3465 %v3 = call i1 @cond()
3466 br i1 %v3, label %b.header, label %a.latch
3467 ; CHECK: [[B_LATCH_SPLIT]]:
3468 ; CHECK-NEXT: br label %b.latch
3471 ; CHECK-NEXT: %v3 = call i1 @cond()
3472 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
3477 ; CHECK-NEXT: br label %a.header
3482 ; CHECK-NEXT: ret void
3485 ; Unswitch will transform the loop nest from:
3489 define void @hoist_inner_loop1(ptr %ptr) {
3490 ; CHECK-LABEL: define void @hoist_inner_loop1(
3494 ; CHECK-NEXT: br label %a.header
3497 %x.a = load i32, ptr %ptr
3500 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3501 ; CHECK-NEXT: br label %b.header
3504 %x.b = load i32, ptr %ptr
3505 %v1 = call i1 @cond()
3508 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3509 ; CHECK-NEXT: %v1 = call i1 @cond()
3510 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3511 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3513 ; CHECK: [[B_HEADER_SPLIT_US]]:
3514 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3516 ; CHECK: [[C_HEADER_US]]:
3517 ; CHECK-NEXT: call i32 @c()
3518 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3520 ; CHECK: [[B_LATCH_US]]:
3521 ; CHECK-NEXT: br label %b.latch
3523 ; CHECK: [[B_HEADER_SPLIT]]:
3524 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3525 ; CHECK-NEXT: br label %c.header
3529 br i1 %v1, label %b.latch, label %c.latch
3531 ; CHECK-NEXT: call i32 @c()
3532 ; CHECK-NEXT: br label %c.latch
3535 ; Use values from other loops to check LCSSA form.
3536 store i32 %x.a, ptr %ptr
3537 store i32 %x.b, ptr %ptr
3538 %v2 = call i1 @cond()
3539 br i1 %v2, label %c.header, label %a.exit.c
3541 ; CHECK-NEXT: store i32 %x.a, ptr %ptr
3542 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3543 ; CHECK-NEXT: %v2 = call i1 @cond()
3544 ; CHECK-NEXT: br i1 %v2, label %c.header, label %a.exit.c
3547 %v3 = call i1 @cond()
3548 br i1 %v3, label %b.header, label %a.exit.b
3550 ; CHECK-NEXT: %v3 = call i1 @cond()
3551 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.exit.b
3556 ; CHECK-NEXT: br label %a.latch
3561 ; CHECK-NEXT: br label %a.latch
3566 ; CHECK-NEXT: br label %a.header
3571 ; CHECK-NEXT: ret void
3574 ; Unswitch will transform the loop nest from:
3578 define void @hoist_inner_loop2(ptr %ptr) {
3579 ; CHECK-LABEL: define void @hoist_inner_loop2(
3583 ; CHECK-NEXT: br label %a.header
3586 %x.a = load i32, ptr %ptr
3589 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3590 ; CHECK-NEXT: br label %b.header
3593 %x.b = load i32, ptr %ptr
3594 %v1 = call i1 @cond()
3597 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3598 ; CHECK-NEXT: %v1 = call i1 @cond()
3599 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3600 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3602 ; CHECK: [[B_HEADER_SPLIT_US]]:
3603 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3605 ; CHECK: [[C_HEADER_US]]:
3606 ; CHECK-NEXT: call i32 @c()
3607 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3609 ; CHECK: [[B_LATCH_US]]:
3610 ; CHECK-NEXT: br label %b.latch
3612 ; CHECK: [[B_HEADER_SPLIT]]:
3613 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3614 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3615 ; CHECK-NEXT: br label %c.header
3619 br i1 %v1, label %b.latch, label %c.latch
3621 ; CHECK-NEXT: call i32 @c()
3622 ; CHECK-NEXT: br label %c.latch
3625 ; Use values from other loops to check LCSSA form.
3626 store i32 %x.a, ptr %ptr
3627 store i32 %x.b, ptr %ptr
3628 %v2 = call i1 @cond()
3629 br i1 %v2, label %c.header, label %exit
3631 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
3632 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3633 ; CHECK-NEXT: %v2 = call i1 @cond()
3634 ; CHECK-NEXT: br i1 %v2, label %c.header, label %exit
3637 %v3 = call i1 @cond()
3638 br i1 %v3, label %b.header, label %a.latch
3640 ; CHECK-NEXT: %v3 = call i1 @cond()
3641 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
3646 ; CHECK-NEXT: br label %a.header
3651 ; CHECK-NEXT: ret void
3654 ; Same as @hoist_inner_loop2 but with a nested loop inside the hoisted loop.
3655 ; Unswitch will transform the loop nest from:
3659 define void @hoist_inner_loop3(ptr %ptr) {
3660 ; CHECK-LABEL: define void @hoist_inner_loop3(
3664 ; CHECK-NEXT: br label %a.header
3667 %x.a = load i32, ptr %ptr
3670 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3671 ; CHECK-NEXT: br label %b.header
3674 %x.b = load i32, ptr %ptr
3675 %v1 = call i1 @cond()
3678 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3679 ; CHECK-NEXT: %v1 = call i1 @cond()
3680 ; CHECK-NEXT: %[[FROZEN:.+]] = freeze i1 %v1
3681 ; CHECK-NEXT: br i1 %[[FROZEN]], label %[[B_HEADER_SPLIT_US:.*]], label %[[B_HEADER_SPLIT:.*]]
3683 ; CHECK: [[B_HEADER_SPLIT_US]]:
3684 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3686 ; CHECK: [[C_HEADER_US]]:
3687 ; CHECK-NEXT: call i32 @c()
3688 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3690 ; CHECK: [[B_LATCH_US]]:
3691 ; CHECK-NEXT: br label %b.latch
3693 ; CHECK: [[B_HEADER_SPLIT]]:
3694 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3695 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3696 ; CHECK-NEXT: br label %c.header
3700 br i1 %v1, label %b.latch, label %c.body
3702 ; CHECK-NEXT: call i32 @c()
3703 ; CHECK-NEXT: br label %c.body
3706 %x.c = load i32, ptr %ptr
3709 ; CHECK-NEXT: %x.c = load i32, ptr %ptr
3710 ; CHECK-NEXT: br label %d.header
3713 ; Use values from other loops to check LCSSA form.
3714 store i32 %x.a, ptr %ptr
3715 store i32 %x.b, ptr %ptr
3716 store i32 %x.c, ptr %ptr
3717 %v2 = call i1 @cond()
3718 br i1 %v2, label %d.header, label %c.latch
3720 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
3721 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3722 ; CHECK-NEXT: store i32 %x.c, ptr %ptr
3723 ; CHECK-NEXT: %v2 = call i1 @cond()
3724 ; CHECK-NEXT: br i1 %v2, label %d.header, label %c.latch
3727 %v3 = call i1 @cond()
3728 br i1 %v3, label %c.header, label %exit
3730 ; CHECK-NEXT: %v3 = call i1 @cond()
3731 ; CHECK-NEXT: br i1 %v3, label %c.header, label %exit
3734 %v4 = call i1 @cond()
3735 br i1 %v4, label %b.header, label %a.latch
3737 ; CHECK-NEXT: %v4 = call i1 @cond()
3738 ; CHECK-NEXT: br i1 %v4, label %b.header, label %a.latch
3743 ; CHECK-NEXT: br label %a.header
3748 ; CHECK-NEXT: ret void
3751 ; This test is designed to exercise checking multiple remaining exits from the
3752 ; loop being unswitched.
3753 ; Unswitch will transform the loop nest from:
3757 define void @hoist_inner_loop4() {
3758 ; CHECK-LABEL: define void @hoist_inner_loop4(
3762 ; CHECK-NEXT: br label %a.header
3767 ; CHECK-NEXT: br label %b.header
3772 ; CHECK-NEXT: br label %c.header
3775 %v1 = call i1 @cond()
3778 ; CHECK-NEXT: %v1 = call i1 @cond()
3779 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %v1
3780 ; CHECK-NEXT: br i1 [[FROZEN]], label %[[C_HEADER_SPLIT_US:.*]], label %[[C_HEADER_SPLIT:.*]]
3782 ; CHECK: [[C_HEADER_SPLIT_US]]:
3783 ; CHECK-NEXT: br label %[[D_HEADER_US:.*]]
3785 ; CHECK: [[D_HEADER_US]]:
3786 ; CHECK-NEXT: call i32 @d()
3787 ; CHECK-NEXT: br label %[[C_LATCH_US:.*]]
3789 ; CHECK: [[C_LATCH_US]]:
3790 ; CHECK-NEXT: br label %c.latch
3792 ; CHECK: [[C_HEADER_SPLIT]]:
3793 ; CHECK-NEXT: br label %d.header
3797 br i1 %v1, label %c.latch, label %d.exiting1
3799 ; CHECK-NEXT: call i32 @d()
3800 ; CHECK-NEXT: br label %d.exiting1
3803 %v2 = call i1 @cond()
3804 br i1 %v2, label %d.exiting2, label %a.latch
3805 ; CHECK: d.exiting1:
3806 ; CHECK-NEXT: %v2 = call i1 @cond()
3807 ; CHECK-NEXT: br i1 %v2, label %d.exiting2, label %a.latch
3810 %v3 = call i1 @cond()
3811 br i1 %v3, label %d.exiting3, label %loopexit.d
3812 ; CHECK: d.exiting2:
3813 ; CHECK-NEXT: %v3 = call i1 @cond()
3814 ; CHECK-NEXT: br i1 %v3, label %d.exiting3, label %loopexit.d
3817 %v4 = call i1 @cond()
3818 br i1 %v4, label %d.latch, label %b.latch
3819 ; CHECK: d.exiting3:
3820 ; CHECK-NEXT: %v4 = call i1 @cond()
3821 ; CHECK-NEXT: br i1 %v4, label %d.latch, label %b.latch
3826 ; CHECK-NEXT: br label %d.header
3829 %v5 = call i1 @cond()
3830 br i1 %v5, label %c.header, label %loopexit.c
3832 ; CHECK-NEXT: %v5 = call i1 @cond()
3833 ; CHECK-NEXT: br i1 %v5, label %c.header, label %loopexit.c
3838 ; CHECK-NEXT: br label %b.header
3843 ; CHECK-NEXT: br label %a.header
3847 ; CHECK: loopexit.d:
3848 ; CHECK-NEXT: br label %exit
3852 ; CHECK: loopexit.c:
3853 ; CHECK-NEXT: br label %exit
3858 ; CHECK-NEXT: ret void
3861 ; Unswitch will transform the loop nest from:
3865 define void @hoist_inner_loop5(ptr %ptr) {
3866 ; CHECK-LABEL: define void @hoist_inner_loop5(
3870 ; CHECK-NEXT: br label %a.header
3873 %x.a = load i32, ptr %ptr
3876 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3877 ; CHECK-NEXT: br label %b.header
3880 %x.b = load i32, ptr %ptr
3883 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3884 ; CHECK-NEXT: br label %c.header
3887 %x.c = load i32, ptr %ptr
3888 %v1 = call i1 @cond()
3891 ; CHECK-NEXT: %x.c = load i32, ptr %ptr
3892 ; CHECK-NEXT: %v1 = call i1 @cond()
3893 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %v1
3894 ; CHECK-NEXT: br i1 [[FROZEN]], label %[[C_HEADER_SPLIT_US:.*]], label %[[C_HEADER_SPLIT:.*]]
3896 ; CHECK: [[C_HEADER_SPLIT_US]]:
3897 ; CHECK-NEXT: br label %[[D_HEADER_US:.*]]
3899 ; CHECK: [[D_HEADER_US]]:
3900 ; CHECK-NEXT: call i32 @d()
3901 ; CHECK-NEXT: br label %[[C_LATCH_US:.*]]
3903 ; CHECK: [[C_LATCH_US]]:
3904 ; CHECK-NEXT: br label %c.latch
3906 ; CHECK: [[C_HEADER_SPLIT]]:
3907 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %c.header ]
3908 ; CHECK-NEXT: %[[X_C_LCSSA:.*]] = phi i32 [ %x.c, %c.header ]
3909 ; CHECK-NEXT: br label %d.header
3913 br i1 %v1, label %c.latch, label %d.latch
3915 ; CHECK-NEXT: call i32 @d()
3916 ; CHECK-NEXT: br label %d.latch
3919 ; Use values from other loops to check LCSSA form.
3920 store i32 %x.a, ptr %ptr
3921 store i32 %x.b, ptr %ptr
3922 store i32 %x.c, ptr %ptr
3923 %v2 = call i1 @cond()
3924 br i1 %v2, label %d.header, label %a.latch
3926 ; CHECK-NEXT: store i32 %x.a, ptr %ptr
3927 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
3928 ; CHECK-NEXT: store i32 %[[X_C_LCSSA]], ptr %ptr
3929 ; CHECK-NEXT: %v2 = call i1 @cond()
3930 ; CHECK-NEXT: br i1 %v2, label %d.header, label %a.latch
3933 %v3 = call i1 @cond()
3934 br i1 %v3, label %c.header, label %b.latch
3936 ; CHECK-NEXT: %v3 = call i1 @cond()
3937 ; CHECK-NEXT: br i1 %v3, label %c.header, label %b.latch
3942 ; CHECK-NEXT: br label %b.header
3947 ; CHECK-NEXT: br label %a.header
3952 ; CHECK-NEXT: ret void
3955 define void @hoist_inner_loop_switch(ptr %ptr) {
3956 ; CHECK-LABEL: define void @hoist_inner_loop_switch(
3960 ; CHECK-NEXT: br label %a.header
3963 %x.a = load i32, ptr %ptr
3966 ; CHECK-NEXT: %x.a = load i32, ptr %ptr
3967 ; CHECK-NEXT: br label %b.header
3970 %x.b = load i32, ptr %ptr
3971 %v1 = call i32 @cond.i32()
3974 ; CHECK-NEXT: %x.b = load i32, ptr %ptr
3975 ; CHECK-NEXT: %v1 = call i32 @cond.i32()
3976 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i32 %v1
3977 ; CHECK-NEXT: switch i32 [[FROZEN]], label %[[B_HEADER_SPLIT:.*]] [
3978 ; CHECK-NEXT: i32 1, label %[[B_HEADER_SPLIT_US:.*]]
3979 ; CHECK-NEXT: i32 2, label %[[B_HEADER_SPLIT_US]]
3980 ; CHECK-NEXT: i32 3, label %[[B_HEADER_SPLIT_US]]
3983 ; CHECK: [[B_HEADER_SPLIT_US]]:
3984 ; CHECK-NEXT: br label %[[C_HEADER_US:.*]]
3986 ; CHECK: [[C_HEADER_US]]:
3987 ; CHECK-NEXT: call i32 @c()
3988 ; CHECK-NEXT: br label %[[B_LATCH_US:.*]]
3990 ; CHECK: [[B_LATCH_US]]:
3991 ; CHECK-NEXT: br label %b.latch
3993 ; CHECK: [[B_HEADER_SPLIT]]:
3994 ; CHECK-NEXT: %[[X_A_LCSSA:.*]] = phi i32 [ %x.a, %b.header ]
3995 ; CHECK-NEXT: %[[X_B_LCSSA:.*]] = phi i32 [ %x.b, %b.header ]
3996 ; CHECK-NEXT: br label %c.header
4000 switch i32 %v1, label %c.latch [
4001 i32 1, label %b.latch
4002 i32 2, label %b.latch
4003 i32 3, label %b.latch
4006 ; CHECK-NEXT: call i32 @c()
4007 ; CHECK-NEXT: br label %c.latch
4010 ; Use values from other loops to check LCSSA form.
4011 store i32 %x.a, ptr %ptr
4012 store i32 %x.b, ptr %ptr
4013 %v2 = call i1 @cond()
4014 br i1 %v2, label %c.header, label %exit
4016 ; CHECK-NEXT: store i32 %[[X_A_LCSSA]], ptr %ptr
4017 ; CHECK-NEXT: store i32 %[[X_B_LCSSA]], ptr %ptr
4018 ; CHECK-NEXT: %v2 = call i1 @cond()
4019 ; CHECK-NEXT: br i1 %v2, label %c.header, label %exit
4022 %v3 = call i1 @cond()
4023 br i1 %v3, label %b.header, label %a.latch
4025 ; CHECK-NEXT: %v3 = call i1 @cond()
4026 ; CHECK-NEXT: br i1 %v3, label %b.header, label %a.latch
4031 ; CHECK-NEXT: br label %a.header
4036 ; CHECK-NEXT: ret void
4039 ; A devilish pattern. This is a crafty, crafty test case designed to risk
4040 ; creating indirect cycles with trivial and non-trivial unswitching. The inner
4041 ; loop has a switch with a trivial exit edge that can be unswitched, but the
4042 ; rest of the switch cannot be unswitched because its cost is too high.
4043 ; However, the unswitching of the trivial edge creates a new switch in the
4044 ; outer loop. *This* switch isn't trivial, but has a low cost to unswitch. When
4045 ; we unswitch this switch from the outer loop, we will remove it completely and
4046 ; create a clone of the inner loop on one side. This clone will then again be
4047 ; viable for unswitching the inner-most loop. This lets us check that the
4048 ; unswitching doesn't end up cycling infinitely even when the cycle is
4049 ; indirect and due to revisiting a loop after cloning.
4050 define void @test31(i32 %arg) {
4051 ; CHECK-LABEL: define void @test31(
4053 br label %outer.header
4054 ; CHECK-NEXT: entry:
4055 ; CHECK-NEXT: switch i32 %arg, label %[[ENTRY_SPLIT:.*]] [
4056 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_US:.*]]
4057 ; CHECK-NEXT: i32 2, label %[[ENTRY_SPLIT_US]]
4060 ; CHECK: [[ENTRY_SPLIT_US]]:
4061 ; CHECK-NEXT: switch i32 %arg, label %[[ENTRY_SPLIT_US_SPLIT:.*]] [
4062 ; CHECK-NEXT: i32 1, label %[[ENTRY_SPLIT_US_SPLIT_US:.*]]
4066 br label %inner.header
4069 switch i32 %arg, label %inner.loopexit1 [
4070 i32 1, label %inner.body1
4071 i32 2, label %inner.body2
4076 br label %inner.latch
4077 ; The (super convoluted) fully unswitched loop around `@a`.
4079 ; CHECK: [[ENTRY_SPLIT_US_SPLIT_US]]:
4080 ; CHECK-NEXT: br label %[[OUTER_HEADER_US_US:.*]]
4082 ; CHECK: [[OUTER_HEADER_US_US]]:
4083 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_US_US:.*]]
4085 ; CHECK: [[OUTER_LATCH_US_US:.*]]:
4086 ; CHECK-NEXT: %[[OUTER_COND_US_US:.*]] = call i1 @cond()
4087 ; CHECK-NEXT: br i1 %[[OUTER_COND_US_US]], label %[[OUTER_HEADER_US_US]], label %[[EXIT_SPLIT_US_SPLIT_US:.*]]
4089 ; CHECK: [[OUTER_HEADER_SPLIT_US_US]]:
4090 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_SPLIT_US_US_US:.*]]
4092 ; CHECK: [[INNER_LOOPEXIT2_US_US:.*]]:
4093 ; CHECK-NEXT: br label %[[OUTER_LATCH_US_US]]
4095 ; CHECK: [[OUTER_HEADER_SPLIT_SPLIT_US_US_US]]:
4096 ; CHECK-NEXT: br label %[[INNER_HEADER_US_US_US:.*]]
4098 ; CHECK: [[INNER_HEADER_US_US_US]]:
4099 ; CHECK-NEXT: br label %[[INNER_BODY1_US_US_US:.*]]
4101 ; CHECK: [[INNER_BODY1_US_US_US]]:
4102 ; CHECK-NEXT: %[[A:.*]] = call i32 @a()
4103 ; CHECK-NEXT: br label %[[INNER_LATCH_US_US_US:.*]]
4105 ; CHECK: [[INNER_LATCH_US_US_US]]:
4106 ; CHECK-NEXT: %[[PHI_A:.*]] = phi i32 [ %[[A]], %[[INNER_BODY1_US_US_US]] ]
4107 ; CHECK-NEXT: call void @sink1(i32 0)
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 %[[PHI_A]])
4118 ; CHECK-NEXT: %[[INNER_COND_US_US_US:.*]] = call i1 @cond()
4119 ; CHECK-NEXT: br i1 %[[INNER_COND_US_US_US]], label %[[INNER_HEADER_US_US_US]], label %[[INNER_LOOPEXIT2_SPLIT_US_US_US:.*]]
4121 ; CHECK: [[INNER_LOOPEXIT2_SPLIT_US_US_US]]:
4122 ; CHECK-NEXT: br label %[[INNER_LOOPEXIT2_US_US]]
4124 ; CHECK: [[EXIT_SPLIT_US_SPLIT_US]]:
4125 ; CHECK-NEXT: br label %[[EXIT_SPLIT_US:.*]]
4130 br label %inner.latch
4131 ; The fully unswitched loop around `@b`.
4133 ; CHECK: [[ENTRY_SPLIT_US_SPLIT]]:
4134 ; CHECK-NEXT: br label %[[OUTER_HEADER_US:.*]]
4136 ; CHECK: [[OUTER_HEADER_US]]:
4137 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_US:.*]]
4139 ; CHECK: [[INNER_HEADER_US:.*]]:
4140 ; CHECK-NEXT: br label %[[INNER_BODY2_US:.*]]
4142 ; CHECK: [[INNER_BODY2_US]]:
4143 ; CHECK-NEXT: %[[B:.*]] = call i32 @b()
4144 ; CHECK-NEXT: br label %[[INNER_LATCH_US:.*]]
4146 ; CHECK: [[INNER_LATCH_US]]:
4147 ; CHECK-NEXT: call void @sink1(i32 0)
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 %[[B]])
4158 ; CHECK-NEXT: %[[INNER_COND_US:.*]] = call i1 @cond()
4159 ; CHECK-NEXT: br i1 %[[INNER_COND_US]], label %[[INNER_HEADER_US]], label %[[INNER_LOOPEXIT2_SPLIT_US:.*]]
4161 ; CHECK: [[INNER_LOOPEXIT2_SPLIT_US]]:
4162 ; CHECK-NEXT: br label %[[INNER_LOOPEXIT2_US:.*]]
4164 ; CHECK: [[OUTER_LATCH_US:.*]]:
4165 ; CHECK-NEXT: %[[OUTER_COND_US:.*]] = call i1 @cond()
4166 ; CHECK-NEXT: br i1 %[[OUTER_COND_US]], label %[[OUTER_HEADER_US]], label %[[EXIT_SPLIT_US_SPLIT:.*]]
4168 ; CHECK: [[OUTER_HEADER_SPLIT_US]]:
4169 ; CHECK-NEXT: br label %[[OUTER_HEADER_SPLIT_SPLIT_US:.*]]
4171 ; CHECK: [[OUTER_HEADER_SPLIT_SPLIT_US]]:
4172 ; CHECK-NEXT: br label %[[INNER_HEADER_US]]
4174 ; CHECK: [[INNER_LOOPEXIT2_US]]:
4175 ; CHECK-NEXT: br label %[[OUTER_LATCH_US]]
4177 ; CHECK: [[EXIT_SPLIT_US]]:
4178 ; CHECK-NEXT: br label %exit
4181 %phi = phi i32 [ %a, %inner.body1 ], [ %b, %inner.body2 ]
4182 ; Make 10 junk calls here to ensure we're over the "50" cost threshold of
4183 ; non-trivial unswitching for this inner switch.
4184 call void @sink1(i32 0)
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 %phi)
4195 %inner.cond = call i1 @cond()
4196 br i1 %inner.cond, label %inner.header, label %inner.loopexit2
4199 br label %outer.latch
4200 ; The unswitched `loopexit1` path.
4202 ; CHECK: [[ENTRY_SPLIT]]:
4203 ; CHECK-NEXT: br label %[[OUTER_HEADER:.*]]
4205 ; CHECK: outer.header:
4206 ; CHECK-NEXT: br label %inner.loopexit1
4208 ; CHECK: inner.loopexit1:
4209 ; CHECK-NEXT: br label %outer.latch
4211 ; CHECK: outer.latch:
4212 ; CHECK-NEXT: %outer.cond = call i1 @cond()
4213 ; CHECK-NEXT: br i1 %outer.cond, label %outer.header, label %[[EXIT_SPLIT:.*]]
4215 ; CHECK: [[EXIT_SPLIT]]:
4216 ; CHECK-NEXT: br label %exit
4219 br label %outer.latch
4222 %outer.cond = call i1 @cond()
4223 br i1 %outer.cond, label %outer.header, label %exit
4228 ; CHECK-NEXT: ret void
4231 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `and`
4232 ; chain (select version).
4233 define i32 @test32(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2) {
4234 ; CHECK-LABEL: @test32(
4236 br label %loop_begin
4237 ; CHECK-NEXT: entry:
4238 ; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4239 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4240 ; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C2_FR]], [[C1_FR]]
4241 ; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
4244 %v1 = load i1, ptr %ptr1
4245 %v2 = load i1, ptr %ptr2
4246 %cond_and1 = select i1 %v1, i1 %cond1, i1 false
4247 %cond_and2 = select i1 %cond_and1, i1 %cond2, i1 false
4248 br i1 %cond_and2, label %loop_a, label %loop_b
4249 ; The 'loop_b' unswitched loop.
4251 ; CHECK: entry.split.us:
4252 ; CHECK-NEXT: br label %loop_begin.us
4254 ; CHECK: loop_begin.us:
4255 ; CHECK-NEXT: %[[V2_US]] = load i1, ptr %ptr2, align 1
4256 ; CHECK-NEXT: br label %loop_b.us
4259 ; CHECK-NEXT: call i32 @b()
4260 ; CHECK-NEXT: br label %latch.us
4263 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3, align 1
4264 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
4266 ; CHECK: loop_exit.split.us:
4267 ; CHECK-NEXT: br label %loop_exit
4269 ; The original loop.
4271 ; CHECK: entry.split:
4272 ; CHECK-NEXT: br label %loop_begin
4274 ; CHECK: loop_begin:
4275 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
4276 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
4277 ; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
4278 ; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
4279 ; CHECK-NEXT: br i1 %[[AND2]], label %loop_a, label %loop_b
4285 ; CHECK-NEXT: call i32 @a()
4286 ; CHECK-NEXT: br label %latch
4292 ; CHECK-NEXT: call i32 @b()
4293 ; CHECK-NEXT: br label %latch
4296 %v3 = load i1, ptr %ptr3
4297 br i1 %v3, label %loop_begin, label %loop_exit
4299 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3, align 1
4300 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
4304 ; CHECK: loop_exit.split:
4305 ; CHECK-NEXT: br label %loop_exit
4311 ; Non-trivial partial loop unswitching of multiple invariant inputs to an `or`
4312 ; chain (select version).
4313 define i32 @test33(ptr %ptr1, ptr %ptr2, ptr %ptr3, i1 %cond1, i1 %cond2) {
4314 ; CHECK-LABEL: @test33(
4316 br label %loop_begin
4317 ; CHECK-NEXT: entry:
4318 ; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4319 ; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4320 ; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C2_FR]], [[C1_FR]]
4321 ; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
4324 %v1 = load i1, ptr %ptr1
4325 %v2 = load i1, ptr %ptr2
4326 %cond_and1 = select i1 %v1, i1 true, i1 %cond1
4327 %cond_and2 = select i1 %cond_and1, i1 true, i1 %cond2
4328 br i1 %cond_and2, label %loop_b, label %loop_a
4329 ; The 'loop_b' unswitched loop.
4331 ; CHECK: entry.split.us:
4332 ; CHECK-NEXT: br label %loop_begin.us
4334 ; CHECK: loop_begin.us:
4335 ; CHECK-NEXT: %[[V2_US]] = load i1, ptr %ptr2, align 1
4336 ; CHECK-NEXT: br label %loop_b.us
4339 ; CHECK-NEXT: call i32 @b()
4340 ; CHECK-NEXT: br label %latch.us
4343 ; CHECK-NEXT: %[[V3_US:.*]] = load i1, ptr %ptr3, align 1
4344 ; CHECK-NEXT: br i1 %[[V3_US]], label %loop_begin.us, label %loop_exit.split.us
4346 ; CHECK: loop_exit.split.us:
4347 ; CHECK-NEXT: br label %loop_exit
4349 ; The original loop.
4351 ; CHECK: entry.split:
4352 ; CHECK-NEXT: br label %loop_begin
4354 ; CHECK: loop_begin:
4355 ; CHECK-NEXT: %[[V1:.*]] = load i1, ptr %ptr1
4356 ; CHECK-NEXT: %[[V2:.*]] = load i1, ptr %ptr2
4357 ; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
4358 ; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
4359 ; CHECK-NEXT: br i1 %[[AND2]], label %loop_b, label %loop_a
4365 ; CHECK-NEXT: call i32 @a()
4366 ; CHECK-NEXT: br label %latch
4372 ; CHECK-NEXT: call i32 @b()
4373 ; CHECK-NEXT: br label %latch
4376 %v3 = load i1, ptr %ptr3
4377 br i1 %v3, label %loop_begin, label %loop_exit
4379 ; CHECK-NEXT: %[[V3:.*]] = load i1, ptr %ptr3, align 1
4380 ; CHECK-NEXT: br i1 %[[V3]], label %loop_begin, label %loop_exit.split
4384 ; CHECK: loop_exit.split:
4385 ; CHECK-NEXT: br label %loop_exit