From 9224d322a27fa383d9c33d4eee4d8aa7842a939b Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Fri, 13 Nov 2020 15:42:02 +0700 Subject: [PATCH] [IndVars] Fix branches exiting by true with invariant conditions Forgot to invert the condition for them. --- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 3 +++ llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index cc3bf87..4c31064 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1432,6 +1432,9 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) { Rewriter.setInsertPoint(BI); auto *LHSV = Rewriter.expandCodeFor(InvariantLHS); auto *RHSV = Rewriter.expandCodeFor(InvariantRHS); + bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB)); + if (ExitIfTrue) + InvariantPred = ICmpInst::getInversePredicate(InvariantPred); IRBuilder<> Builder(BI); auto *NewCond = Builder.CreateICmp(InvariantPred, LHSV, RHSV, BI->getCondition()->getName()); diff --git a/llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll b/llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll index 664af7a..b0629ed 100644 --- a/llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll +++ b/llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll @@ -529,7 +529,7 @@ define void @test_can_predicate_simple_unsigned_inverted(i32* %p, i32* %arr) { ; CHECK-NEXT: br i1 [[ZERO_COND]], label [[EXIT:%.*]], label [[RANGE_CHECK_BLOCK:%.*]] ; CHECK: range_check_block: ; CHECK-NEXT: [[IV_NEXT]] = sub i32 [[IV]], 1 -; CHECK-NEXT: [[RANGE_CHECK1:%.*]] = icmp ult i32 [[TMP0]], [[LEN]] +; CHECK-NEXT: [[RANGE_CHECK1:%.*]] = icmp uge i32 [[TMP0]], [[LEN]] ; CHECK-NEXT: br i1 [[RANGE_CHECK1]], label [[FAIL:%.*]], label [[BACKEDGE]] ; CHECK: backedge: ; CHECK-NEXT: [[EL_PTR:%.*]] = getelementptr i32, i32* [[P]], i32 [[IV]] -- 2.7.4