Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -261,9 +261,10 @@ const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop); const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop); - // If the condition is always true or always false, replace it with - // a constant value. - if (auto Ev = SE->evaluatePredicate(Pred, S, X)) { + // If the condition is always true or always false in the given context, + // replace it with a constant value. + // TODO: We can sharpen the context to common dominator of all ICmp's users. + if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, ICmp)) { ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev)); DeadInsts.emplace_back(ICmp); LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); Index: llvm/test/Transforms/IndVarSimplify/checks_against_min_value.ll =================================================================== --- llvm/test/Transforms/IndVarSimplify/checks_against_min_value.ll +++ llvm/test/Transforms/IndVarSimplify/checks_against_min_value.ll @@ -15,8 +15,7 @@ ; CHECK-NEXT: [[CHECK:%.*]] = icmp slt i32 [[IV_NEXT]], [[IV]] ; CHECK-NEXT: br i1 [[CHECK]], label [[GUARDED]], label [[FAIL:%.*]] ; CHECK: guarded: -; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[IV]], -2147483648 -; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]] +; CHECK-NEXT: br i1 true, label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]] ; CHECK: exit.loopexit: ; CHECK-NEXT: br label [[EXIT]] ; CHECK: exit: @@ -58,8 +57,7 @@ ; CHECK-NEXT: [[CHECK:%.*]] = icmp ult i32 [[IV_NEXT]], [[IV]] ; CHECK-NEXT: br i1 [[CHECK]], label [[GUARDED]], label [[FAIL:%.*]] ; CHECK: guarded: -; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[IV]], 0 -; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]] +; CHECK-NEXT: br i1 true, label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]] ; CHECK: exit.loopexit: ; CHECK-NEXT: br label [[EXIT]] ; CHECK: exit: Index: llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll =================================================================== --- llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll +++ llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll @@ -951,11 +951,9 @@ ; CHECK-NEXT: [[C1:%.*]] = icmp ne i32 [[IV]], 0 ; CHECK-NEXT: br i1 [[C1]], label [[CHECKED_1:%.*]], label [[FAIL:%.*]] ; CHECK: checked.1: -; CHECK-NEXT: [[C2:%.*]] = icmp ne i32 [[IV]], 0 -; CHECK-NEXT: br i1 [[C2]], label [[CHECKED_2:%.*]], label [[FAIL]] +; CHECK-NEXT: br i1 true, label [[CHECKED_2:%.*]], label [[FAIL]] ; CHECK: checked.2: -; CHECK-NEXT: [[C3:%.*]] = icmp ne i32 [[IV]], 0 -; CHECK-NEXT: br i1 [[C3]], label [[BACKEDGE]], label [[FAIL]] +; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[FAIL]] ; CHECK: backedge: ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 758394 ; CHECK-NEXT: [[LOOP_COND:%.*]] = call i1 @cond_func() @@ -1003,11 +1001,9 @@ ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[IV]], 0 ; CHECK-NEXT: br i1 [[C1]], label [[CHECKED_1:%.*]], label [[FAIL:%.*]] ; CHECK: checked.1: -; CHECK-NEXT: [[C2:%.*]] = icmp slt i32 [[IV]], 1 -; CHECK-NEXT: br i1 [[C2]], label [[CHECKED_2:%.*]], label [[FAIL]] +; CHECK-NEXT: br i1 true, label [[CHECKED_2:%.*]], label [[FAIL]] ; CHECK: checked.2: -; CHECK-NEXT: [[C3:%.*]] = icmp slt i32 [[IV]], 2 -; CHECK-NEXT: br i1 [[C3]], label [[BACKEDGE]], label [[FAIL]] +; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[FAIL]] ; CHECK: backedge: ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 758394 ; CHECK-NEXT: [[LOOP_COND:%.*]] = call i1 @cond_func()