Skip to content

Commit 9daf077

Browse files
fhahnmemfrob
authored andcommitted
[LoopUnswitch] Avoid partially unswitching too aggressively.
This patch adds additional checks to avoid partial unswitching in cases where it won't be profitable, e.g. because the path directly exits the loop anyways.
1 parent 63b5b25 commit 9daf077

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Transforms/Scalar/LoopUnswitch.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ hasPartialIVCondition(Loop *L, MemorySSA &MSSA, AAResults *AA) {
725725
WorkList.append(succ_begin(Current), succ_end(Current));
726726
}
727727

728+
// Require at least 2 blocks on a path through the loop. This skips
729+
// paths that directly exit the loop.
730+
if (Seen.size() < 2)
731+
return false;
732+
728733
// Next, check if there are any MemoryDefs that are on the path through
729734
// the loop (in the Seen set) and they may-alias any of the locations in
730735
// AccessedLocs. If that is the case, they may modify the condition and
@@ -761,6 +766,11 @@ hasPartialIVCondition(Loop *L, MemorySSA &MSSA, AAResults *AA) {
761766
return true;
762767
};
763768

769+
// If we branch to the same successor, partial unswitching will not be
770+
// beneficial.
771+
if (TI->getSuccessor(0) == TI->getSuccessor(1))
772+
return {};
773+
764774
if (HasNoClobbersOnPath(TI->getSuccessor(0), L->getHeader(), AccessesToCheck))
765775
return {ToDuplicate, ConstantInt::getTrue(TI->getContext())};
766776
if (HasNoClobbersOnPath(TI->getSuccessor(1), L->getHeader(), AccessesToCheck))

llvm/test/Transforms/LoopUnswitch/partial-unswitch.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,7 @@ exit:
831831
define i32 @no_partial_unswitch_true_successor_exit(i32* %ptr, i32 %N) {
832832
; CHECK-LABEL: @no_partial_unswitch_true_successor_exit
833833
; CHECK-LABEL: entry:
834-
; CHECK-NEXT: [[LV:%[0-9]+]] = load i32, i32* %ptr, align 4
835-
; CHECK-NEXT: [[C:%[0-9]+]] = icmp eq i32 [[LV]], 100
836-
; CHECK-NEXT: br i1 [[C]],
834+
; CHECK-NEXT: br label %loop.header
837835
;
838836
entry:
839837
br label %loop.header
@@ -860,9 +858,7 @@ exit:
860858
define i32 @no_partial_unswitch_true_same_successor(i32* %ptr, i32 %N) {
861859
; CHECK-LABEL: @no_partial_unswitch_true_same_successor
862860
; CHECK-LABEL: entry:
863-
; CHECK-NEXT: [[LV:%[0-9]+]] = load i32, i32* %ptr, align 4
864-
; CHECK-NEXT: [[C:%[0-9]+]] = icmp eq i32 [[LV]], 100
865-
; CHECK-NEXT: br i1 [[C]],
861+
; CHECK-NEXT: br label %loop.header
866862
;
867863
entry:
868864
br label %loop.header

0 commit comments

Comments
 (0)