Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -7452,6 +7452,25 @@ return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS); } +static CmpInst::Predicate GetSignedPredicate(CmpInst::Predicate P) { + switch (P) { + default: + return CmpInst::BAD_ICMP_PREDICATE; + + case CmpInst::ICMP_UGT: + return CmpInst::ICMP_SGT; + + case CmpInst::ICMP_UGE: + return CmpInst::ICMP_SGE; + + case CmpInst::ICMP_ULT: + return CmpInst::ICMP_SLT; + + case CmpInst::ICMP_ULE: + return CmpInst::ICMP_SLE; + } +} + bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, ICmpInst::Predicate FoundPred, @@ -7512,6 +7531,12 @@ RHS, LHS, FoundLHS, FoundRHS); } + // Unsigned comparison is the same as signed comparison when both the operands + // are non-negative. + if (GetSignedPredicate(FoundPred) == Pred && isKnownNonNegative(FoundLHS) && + isKnownNonNegative(FoundRHS)) + return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); + // Check if we can make progress by sharpening ranges. if (FoundPred == ICmpInst::ICMP_NE && (isa(FoundLHS) || isa(FoundRHS))) { Index: test/Transforms/IndVarSimplify/eliminate-comparison.ll =================================================================== --- test/Transforms/IndVarSimplify/eliminate-comparison.ll +++ test/Transforms/IndVarSimplify/eliminate-comparison.ll @@ -529,5 +529,55 @@ ret void } +define void @func_24(i32* %length.ptr) { +; CHECK-LABEL: @func_24( + entry: + %length = load i32, i32* %length.ptr, !range !0 + %entry.cond = icmp ult i32 4, %length + br i1 %entry.cond, label %loop, label %leave + + loop: +; CHECK: loop: + %iv = phi i32 [ 4, %entry ], [ %iv.inc, %be ] + %iv.inc = add i32 %iv, 1 + %range.check = icmp slt i32 %iv, %length + br i1 %range.check, label %be, label %leave +; CHECK: br i1 true, label %be, label %leave.loopexit +; CHECK: be: + + be: + call void @side_effect() + %be.cond = icmp slt i32 %iv.inc, %length + br i1 %be.cond, label %loop, label %leave + + leave: + ret void +} + +define void @func_25(i32* %init.ptr) { +; CHECK-LABEL: @func_25( + entry: + %init = load i32, i32* %init.ptr, !range !0 + %entry.cond = icmp ugt i32 %init, 4 + br i1 %entry.cond, label %loop, label %leave + + loop: +; CHECK: loop: + %iv = phi i32 [ %init, %entry ], [ %iv.dec, %be ] + %iv.dec = add i32 %iv, -1 + %range.check = icmp sgt i32 %iv, 4 + br i1 %range.check, label %be, label %leave +; CHECK: br i1 true, label %be, label %leave.loopexit +; CHECK: be: + + be: + call void @side_effect() + %be.cond = icmp sgt i32 %iv.dec, 4 + br i1 %be.cond, label %loop, label %leave + + leave: + ret void +} + !0 = !{i32 0, i32 2147483647}