[PATCH] D48283: [SCEV] Properly solve quadratic equations

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 19 12:17:20 PDT 2018


efriedma added a comment.

The general problem we're trying to solve in getNumIterationsInRange is how many iterations the SCEV `{L,+,M,+,N}` is in the range [R,S].  There's a simple, inefficient algorithm for this: just call evaluateAtIteration repeatedly until the value isn't in range.  But that's obviously way too expensive, and I don't know how to solve the general problem more efficiently.

So let's take a subset of the problem.  The simplest subset is SCEVs where the last value in range is before the point of the first unsigned overflow.  We can solve this by normalizing the range to [R, UINT_MAX], then finding the first iteration where `{L,+,M,+,N}` has unsigned overflow, then checking whether the iteration after the overflow is in range.  This can be computed either using a straight binary search, or by converting it to an inequality over real numbers and using algebra.  I thought this is what you were aiming for, since it's very close to what you've implemented?

We can generalize that a bit by adding some additional code in the case where we fail to compute a solution: apply a binary NOT to both the SCEV and the range, and try again.

Another possible subset is SCEVs where the last value in range is before the point of the first signed overflow.  Basically, normalize the range to [R, INT_MAX], treat the coefficients and range as signed numbers, solve the system of inequalities over real numbers, then verify the solution applies to the original modular problem.  This is a little more complicated because the vertex of the parabola could be inside the range.


Repository:
  rL LLVM

https://reviews.llvm.org/D48283





More information about the llvm-commits mailing list