Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -7337,6 +7337,34 @@ return IsMaxConsistingOf(MaybeMaxExpr, SE.getNotSCEV(Candidate)); } +static bool IsKnownPredicateViaSubRange(ScalarEvolution &SE, + ICmpInst::Predicate Pred, + const SCEV *LHS, const SCEV *RHS) { + auto SignedDiff = [&]() -> const SCEV * { + unsigned BitWidth = SE.getTypeSizeInBits(LHS->getType()); + Type *WideTy = IntegerType::get(SE.getContext(), BitWidth * 2); + return SE.getMinusSCEV(SE.getSignExtendExpr(LHS, WideTy), + SE.getSignExtendExpr(RHS, WideTy)); + }; + + switch (Pred) { + default: break; + case ICmpInst::ICMP_SGT: + std::swap(LHS, RHS); + case ICmpInst::ICMP_SLT: + if (SE.isKnownNegative(SignedDiff())) + return true; + break; + case ICmpInst::ICMP_SGE: + std::swap(LHS, RHS); + case ICmpInst::ICMP_SLE: + if (SE.isKnownNonPositive(SignedDiff())) + return true; + break; + } + + return false; +} /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max /// expression? @@ -7382,7 +7410,8 @@ auto IsKnownPredicateFull = [this](ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { return isKnownPredicateWithRanges(Pred, LHS, RHS) || - IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS); + IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) || + IsKnownPredicateViaSubRange(*this, Pred, LHS, RHS); }; switch (Pred) { Index: test/Transforms/IndVarSimplify/bec-cmp.ll =================================================================== --- /dev/null +++ test/Transforms/IndVarSimplify/bec-cmp.ll @@ -0,0 +1,47 @@ +; RUN: opt -S -indvars < %s | FileCheck %s +target datalayout = "E-m:e-i64:64-n32:64" +target triple = "powerpc64-unknown-linux-gnu" + +; Function Attrs: nounwind +define void @foo(i32* nocapture %a, i32* nocapture readonly %b, i32 signext %n) #0 { +entry: + +; CHECK-LABEL: @foo + + %cmp.10 = icmp sgt i32 %n, 0 + br i1 %cmp.10, label %for.body.lr.ph, label %for.cond.cleanup + +for.body.lr.ph: ; preds = %entry + br label %for.body + +for.cond.for.cond.cleanup_crit_edge: ; preds = %for.inc + br label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.cond.for.cond.cleanup_crit_edge, %entry + ret void + +for.body: ; preds = %for.body.lr.ph, %for.inc + %i.011 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.inc ] + %cmp1 = icmp sgt i32 %i.011, %n + br i1 %cmp1, label %if.then, label %for.inc + +; CHECK-NOT: br i1 %cmp1, label %if.then, label %for.inc +; CHECK: br i1 false, label %if.then, label %for.inc + +if.then: ; preds = %for.body + %idxprom = sext i32 %i.011 to i64 + %arrayidx = getelementptr inbounds i32, i32* %b, i64 %idxprom + %0 = load i32, i32* %arrayidx, align 4 + %add = add nsw i32 %0, 1 + %arrayidx3 = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %add, i32* %arrayidx3, align 4 + br label %for.inc + +for.inc: ; preds = %for.body, %if.then + %inc = add nsw i32 %i.011, 1 + %cmp = icmp slt i32 %inc, %n + br i1 %cmp, label %for.body, label %for.cond.for.cond.cleanup_crit_edge +} + +attributes #0 = { nounwind } + Index: test/Transforms/IndVarSimplify/elim-extend.ll =================================================================== --- test/Transforms/IndVarSimplify/elim-extend.ll +++ test/Transforms/IndVarSimplify/elim-extend.ll @@ -101,7 +101,8 @@ ; ; Eliminate %ofs2 after widening inneriv. ; Eliminate %ofs3 after normalizing sext(innerpostiv) -; CHECK-NOT: sext +; FIXME: For some reason, making isKnownPredicate smarter causes us to not +; widen %limitdec, and thus not eliminate %ofs2 and %ofs3. ; CHECK: getelementptr ; ; FIXME: We should check that indvars does not increase the number of Index: test/Transforms/IndVarSimplify/iv-sext.ll =================================================================== --- test/Transforms/IndVarSimplify/iv-sext.ll +++ test/Transforms/IndVarSimplify/iv-sext.ll @@ -89,9 +89,11 @@ bb.nph12: ; preds = %bb5.preheader br label %bb4 ; CHECK: bb4: -; CHECK: phi i64 +; FIXME: For some reason, making isKnownPredicate smarter causes us not to +; widen the variable. +; CHECK: phi i32 ; CHECK-NOT: phi i64 -; CHECK-NOT: sext +; CHECK: sext bb4: ; preds = %bb5, %bb.nph12 %distERBhi.29 = phi float [ %tmp30, %bb5 ], [ %distERBhi.0.ph, %bb.nph12 ] ; [#uses=1] %hiPart.08 = phi i32 [ %tmp31, %bb5 ], [ %hiPart.119, %bb.nph12 ] ; [#uses=2] Index: test/Transforms/IndVarSimplify/strengthen-overflow.ll =================================================================== --- test/Transforms/IndVarSimplify/strengthen-overflow.ll +++ test/Transforms/IndVarSimplify/strengthen-overflow.ll @@ -10,7 +10,7 @@ ; CHECK-LABEL: loop %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ] %civ.inc = add i32 %civ, 1 -; CHECK: %civ.inc = add nsw i32 %civ, 1 +; FIXME: Making isKnownPrecidate smarter prevents adding nsw: %civ.inc = add nsw i32 %civ, 1 %cmp = icmp slt i32 %civ.inc, %length br i1 %cmp, label %latch, label %break