Index: lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp =================================================================== --- lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp @@ -804,6 +804,12 @@ SE.getConstant(Min)); } +static bool isKnownNonNegative(const SCEV *BoundSCEV, Loop *L, + ScalarEvolution &SE) { + const SCEV *Zero = SE.getConstant(BoundSCEV->getType(), 0, true); + return SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, BoundSCEV, Zero); +} + Optional LoopStructure::parseLoopStructure(ScalarEvolution &SE, BranchProbabilityInfo *BPI, Loop &L, @@ -963,8 +969,8 @@ // If both parts are known non-negative, it is profitable to use // unsigned comparison in increasing loop. This allows us to make the // comparison check against "RightSCEV + 1" more optimistic. - if (SE.isKnownNonNegative(IndVarStart) && - SE.isKnownNonNegative(RightSCEV)) + if (isKnownNonNegative(IndVarStart, &L, SE) && + isKnownNonNegative(RightSCEV, &L, SE)) Pred = ICmpInst::ICMP_ULT; else Pred = ICmpInst::ICMP_SLT; Index: test/Transforms/IRCE/variable-loop-bounds.ll =================================================================== --- test/Transforms/IRCE/variable-loop-bounds.ll +++ test/Transforms/IRCE/variable-loop-bounds.ll @@ -55,7 +55,7 @@ ; CHECK-LABEL: test_inc_ne ; CHECK: main.exit.selector: ; CHECK: [[PSEUDO_PHI:%[^ ]+]] = phi i32 [ %inc, %for.inc ] -; CHECK: [[COND:%[^ ]+]] = icmp slt i32 [[PSEUDO_PHI]], %N +; CHECK: [[COND:%[^ ]+]] = icmp ult i32 [[PSEUDO_PHI]], %N ; CHECK: br i1 [[COND]], label %main.pseudo.exit, label %for.cond.cleanup.loopexit define void @test_inc_ne(i32* nocapture %a, i32* nocapture readonly %b, i32* nocapture readonly %c, i32 %N) { entry: