Fix for PR2607: SCEV miscomputing the loop count for loops with an
SGT exit condition. Essentially, the correct way to flip an inequality
in 2's complement is the not operator, not the negation operator.
That said, the difference only affects cases involving INT_MIN.
Also, enhance the pre-test search logic to be a bit smarter about
inequalities flipped with a not operator, so it can eliminate the smax
from the iteration count for simple loops.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54184 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp
index 737d9b3..14c1389 100644
--- a/lib/Analysis/ScalarEvolution.cpp
+++ b/lib/Analysis/ScalarEvolution.cpp
@@ -1976,8 +1976,8 @@
break;
}
case ICmpInst::ICMP_SGT: {
- SCEVHandle TC = HowManyLessThans(SE.getNegativeSCEV(LHS),
- SE.getNegativeSCEV(RHS), L, true);
+ SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
+ SE.getNotSCEV(RHS), L, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
@@ -2724,7 +2724,11 @@
if (!PreCondLHS->getType()->isInteger()) return false;
- return LHS == getSCEV(PreCondLHS) && RHS == getSCEV(PreCondRHS);
+ SCEVHandle PreCondLHSSCEV = getSCEV(PreCondLHS);
+ SCEVHandle PreCondRHSSCEV = getSCEV(PreCondRHS);
+ return (LHS == PreCondLHSSCEV && RHS == PreCondRHSSCEV) ||
+ (LHS == SE.getNotSCEV(PreCondRHSSCEV) &&
+ RHS == SE.getNotSCEV(PreCondLHSSCEV));
}
/// HowManyLessThans - Return the number of times a backedge containing the