Skip to content

Commit afd7db4

Browse files
committed
[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.
1 parent 7b8130c commit afd7db4

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
@@ -3061,7 +3061,6 @@ injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate, Loop &L,
30613061
auto *InjectedCond =
30623062
ICmpInst::Create(Instruction::ICmp, Pred, LHS, RHS, "injected.cond",
30633063
Preheader->getTerminator());
3064-
auto *OldCond = TI->getCondition();
30653064

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

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

30753075
TI->eraseFromParent();
30763076
// 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)