Skip to content

Commit 6bd2b70

Browse files
committed
[SimpleLoopUnswitch] Add freeze if branch execs for partial unswitching.
We cannot skip the freezing the condition if the unswitched branch executes, if the condition is a chain of ANDs/ORs. For example, if if we have an AND %c1, %c2 with %c1 == undef and %c2 == 0, there would be no branch on undef in the original code, but a branch on undef if we unswitch %c1. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D124603
1 parent b910cf9 commit 6bd2b70

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,9 +2321,9 @@ static void unswitchNontrivialInvariants(
23212321
buildPartialInvariantUnswitchConditionalBranch(
23222322
*SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU);
23232323
else {
2324-
buildPartialUnswitchConditionalBranch(*SplitBB, Invariants, Direction,
2325-
*ClonedPH, *LoopPH, InsertFreeze,
2326-
BI, &AC, DT);
2324+
buildPartialUnswitchConditionalBranch(
2325+
*SplitBB, Invariants, Direction, *ClonedPH, *LoopPH,
2326+
FreezeLoopUnswitchCond, BI, &AC, DT);
23272327
}
23282328
DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH});
23292329

llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,7 +2717,8 @@ define i32 @test25(i1* %ptr, i1 %cond) {
27172717
entry:
27182718
br label %loop_begin
27192719
; CHECK-NEXT: entry:
2720-
; CHECK-NEXT: br i1 %cond, label %entry.split.us, label %entry.split
2720+
; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 %cond
2721+
; CHECK-NEXT: br i1 [[FROZEN]], label %entry.split.us, label %entry.split
27212722

27222723
loop_begin:
27232724
%v1 = load i1, i1* %ptr
@@ -2790,7 +2791,9 @@ define i32 @test26(i1* %ptr1, i1* %ptr2, i1* %ptr3, i1 %cond1, i1 %cond2, i1 %co
27902791
entry:
27912792
br label %loop_begin
27922793
; CHECK-NEXT: entry:
2793-
; CHECK-NEXT: %[[INV_AND:.*]] = and i1 %cond3, %cond1
2794+
; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2795+
; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2796+
; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C3_FR]], [[C1_FR]]
27942797
; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
27952798

27962799
loop_begin:
@@ -2871,7 +2874,9 @@ define i32 @test27(i1* %ptr1, i1* %ptr2, i1* %ptr3, i1 %cond1, i1 %cond2, i1 %co
28712874
entry:
28722875
br label %loop_begin
28732876
; CHECK-NEXT: entry:
2874-
; CHECK-NEXT: %[[INV_OR:.*]] = or i1 %cond3, %cond1
2877+
; CHECK-NEXT: [[C3_FR:%.+]] = freeze i1 %cond3
2878+
; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
2879+
; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C3_FR]], [[C1_FR]]
28752880
; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
28762881

28772882
loop_begin:
@@ -4231,7 +4236,9 @@ define i32 @test32(i1* %ptr1, i1* %ptr2, i1* %ptr3, i1 %cond1, i1 %cond2) {
42314236
entry:
42324237
br label %loop_begin
42334238
; CHECK-NEXT: entry:
4234-
; CHECK-NEXT: %[[INV_AND:.*]] = and i1 %cond2, %cond1
4239+
; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4240+
; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4241+
; CHECK-NEXT: %[[INV_AND:.*]] = and i1 [[C2_FR]], [[C1_FR]]
42354242
; CHECK-NEXT: br i1 %[[INV_AND]], label %entry.split, label %entry.split.us
42364243

42374244
loop_begin:
@@ -4309,7 +4316,9 @@ define i32 @test33(i1* %ptr1, i1* %ptr2, i1* %ptr3, i1 %cond1, i1 %cond2) {
43094316
entry:
43104317
br label %loop_begin
43114318
; CHECK-NEXT: entry:
4312-
; CHECK-NEXT: %[[INV_OR:.*]] = or i1 %cond2, %cond1
4319+
; CHECK-NEXT: [[C2_FR:%.+]] = freeze i1 %cond2
4320+
; CHECK-NEXT: [[C1_FR:%.+]] = freeze i1 %cond1
4321+
; CHECK-NEXT: %[[INV_OR:.*]] = or i1 [[C2_FR]], [[C1_FR]]
43134322
; CHECK-NEXT: br i1 %[[INV_OR]], label %entry.split.us, label %entry.split
43144323

43154324
loop_begin:

0 commit comments

Comments
 (0)