Skip to content

Commit 773f136

Browse files
nikictru
authored andcommitted
[SimpleLoopUnswitch] Fix reversed branch during condition injection
The in-loop successor is only on the left after a potential condition inversion. As we re-use the old condition as-is, we should also reuse the old successors as-is. Fixes #63962. (cherry picked from commit afd7db4)
1 parent 4362f3e commit 773f136

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,6 @@ injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate, Loop &L,
30603060
auto *InjectedCond =
30613061
ICmpInst::Create(Instruction::ICmp, Pred, LHS, RHS, "injected.cond",
30623062
Preheader->getTerminator());
3063-
auto *OldCond = TI->getCondition();
30643063

30653064
BasicBlock *CheckBlock = BasicBlock::Create(Ctx, BB->getName() + ".check",
30663065
BB->getParent(), InLoopSucc);
@@ -3069,7 +3068,8 @@ injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate, Loop &L,
30693068
Builder.CreateCondBr(InjectedCond, InLoopSucc, CheckBlock);
30703069

30713070
Builder.SetInsertPoint(CheckBlock);
3072-
auto *NewTerm = Builder.CreateCondBr(OldCond, InLoopSucc, OutOfLoopSucc);
3071+
auto *NewTerm = Builder.CreateCondBr(TI->getCondition(), TI->getSuccessor(0),
3072+
TI->getSuccessor(1));
30733073

30743074
TI->eraseFromParent();
30753075
// Prevent infinite unswitching.

llvm/test/Transforms/SimpleLoopUnswitch/inject-invariant-conditions.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ define i32 @test_02_inverse(ptr noundef %p, i32 noundef %n, i32 noundef %limit,
465465
; CHECK-NEXT: br i1 [[BOUND_CHECK]], label [[GUARDED:%.*]], label [[COMMON_RET]], !prof [[PROF1]]
466466
; CHECK: guarded:
467467
; CHECK-NEXT: [[RANGE_CHECK:%.*]] = icmp uge i32 [[EL]], [[X]]
468-
; CHECK-NEXT: br i1 [[RANGE_CHECK]], label [[BACKEDGE]], label [[COMMON_RET]], !llvm.invariant.condition.injection.disabled !0
468+
; CHECK-NEXT: br i1 [[RANGE_CHECK]], label [[COMMON_RET]], label [[BACKEDGE]], !llvm.invariant.condition.injection.disabled !0
469469
; CHECK: backedge:
470470
; CHECK-NEXT: [[ARR_PTR:%.*]] = getelementptr i32, ptr [[ARR]], i32 [[EL]]
471471
; CHECK-NEXT: store i32 [[IV]], ptr [[ARR_PTR]], align 4

0 commit comments

Comments
 (0)