Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -8265,11 +8265,31 @@ return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D); } -/// Find the roots of the quadratic equation for the given quadratic chrec -/// {L,+,M,+,N}. This returns either the two roots (which might be the same) or -/// two SCEVCouldNotCompute objects. -static Optional> -SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { +/// Let q(n) be the value of the quadratic chrec {L,+,M,+,N} after n +/// iterations. The values L, M, N are assumed to be signed. +/// +/// This function finds the smallest number n, such that +/// (a) n >= 0 and q(n) = 0, or +/// (b) n >= 1 and q(n-1) and q(n) belong to two different intervals +/// [Rk, Rk+R), where R = 2^BW, and k is an integer. +/// (The idea here is to find when q(n) "overflows" 2^BW, while at the +/// same time "allowing" subtraction. In unsigned modulo arithmetic a +/// subtraction would always count as an overflow, but here we want to +/// allow values to decrease and increase as long as they are within +/// the same interval). +/// +/// This function returns None when +/// (a) either of the coefficients are not constant, or +/// (b) after finding k that minimizes the positive solution to q(n) = 2kR, +/// both solutions are contained between two consecutive integers. +/// +/// There are cases where q(n) > B, and q(n+1) < B by the virtue of *signed* +/// overflow. This function will *not* find such an n, however it may find +/// a value of n satisfying the inequalities due to an *unsigned* overflow. +/// Because of that, the returned value needs to be verified to actually +/// solve the original scenario. +static Optional +SolveQuadraticEquation(const SCEVAddRecExpr *AddRec) { assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); const SCEVConstant *LC = dyn_cast(AddRec->getOperand(0)); const SCEVConstant *MC = dyn_cast(AddRec->getOperand(1)); @@ -8280,54 +8300,215 @@ return None; uint32_t BitWidth = LC->getAPInt().getBitWidth(); - const APInt &L = LC->getAPInt(); - const APInt &M = MC->getAPInt(); - const APInt &N = NC->getAPInt(); - APInt Two(BitWidth, 2); - - // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C - - // The A coefficient is N/2 - APInt A = N.sdiv(Two); + APInt L = LC->getAPInt(); + APInt M = MC->getAPInt(); + APInt N = NC->getAPInt(); + assert(!N.isNullValue() && "This is not a quadratic chrec"); + + // The result of APInt arithmetic has the same bit width as the operands, + // so it can actually lose high bits. A product of two n-bit integers needs + // 2n-1 bits to represent the full value. The conversion used here from + // n-bit chrec coefficients produces n+2-bit quadratic coefficients. + // The operation done below (on quadratic coefficients) that can produce + // the largest value is the evaluation of the equation during bisection, + // which needs 3 times the bitwidth of the coefficient. + // The total number of required bits is 3(n+2) = 3n+6. + // + // The purpose of this extension is to simulate the set Z of all integers, + // where n+1 > n for all n in Z. In Z it makes sense to talk about positive + // and negative numbers (not so much in a modulo arithmetic). The method + // used to solve the equation is based on the standard formula for real + // numbers, and uses the concepts of "positive" and "negative" with their + // usual meanings. + uint32_t NewWidth = 3*BitWidth + 6; + N = N.sext(NewWidth); + M = M.sext(NewWidth); + L = L.sext(NewWidth); + + // It is preferable to return a value that has the same bit width as the + // AddRec's coefficients. If the solution fits in the original bit width, + // truncate it (except for i1). + auto TruncToOrig = [BitWidth] (const APInt &V) { + if (BitWidth > 1 && V.isIntN(BitWidth)) + return V.trunc(BitWidth); + return V; + }; - // The B coefficient is M-N/2 - APInt B = M; - B -= A; // A is the same as N/2. + // The increments are M, M+N, M+2N, ..., so the accumulated values are + // L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is, + // L+M, L+2M+N, L+3M+3N, ... + // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N. + // + // The equation Acc = 0 is then + // L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0. + // In a quadratic form it becomes: + // N n^2 + (2M-N) n + 2L = 0. + + APInt A = N; + APInt B = 2 * M - A; + APInt C = 2 * L; + APInt R = APInt::getOneBitSet(NewWidth, BitWidth); + + // Make A > 0 for simplicity. + if (A.isNegative()) { + A.negate(); + B.negate(); + C.negate(); + } + LLVM_DEBUG(dbgs() << __func__ << ": solving " << A << "x^2 + " << B + << "x + " << C << ", bw:" << BitWidth << " for AddRec: " + << *AddRec << '\n'); + + // Identify 0 as a (non)solution immediately. + if (C.isNullValue()) { + LLVM_DEBUG(dbgs() << __func__ << ": zero solution\n"); + return TruncToOrig(APInt(BitWidth, 0)); + } + + // Solving an equation q(x) = 0 with coefficients in modular arithmetic + // is really solving a set of equations q(x) = 2kR for k = 0, 1, 2, ..., + // and R = 2^BitWidth. (It's 2R because the equation was multiplied by 2.) + // Since we're trying not only to find exact solutions, but also values + // that "wrap around", such a set will always have a solution, i.e. an x + // that satisfies at least one of the equations, or such that |q(x)| + // exceeds 2kR, while |q(x-1)| for the same k does not. + // + // We need to find a value k, such that Ax^2 + Bx + C = 2kR will have a + // positive solution n (in the above sense), and also such that the n + // will be the least among all solutions corresponding to k = 0, 1, ... + // (more precisely, the least element in the set + // { n(k) | k is such that a solution n(k) exists }). + // + // Consider the parabola (over real numbers) that corresponds to the + // quadratic equation. Since A > 0, the arms of the parabola will point + // up. Picking different values of k will shift it up and down by 2R. + // + // We want to shift the parabola in such a way as to reduce the problem + // of solving q(x) = 2kR to solving shifted_q(x) = 0. + // (The interesting solutions are the ceilings of the real number + // solutions.) + + APInt TwoA = 2 * A; + APInt TwoR = 2 * R; + APInt SqrB = B * B; + bool PickLow; + + auto RoundUp = [] (const APInt &V, const APInt &A) { + assert(A.isStrictlyPositive()); + APInt T = V.abs().urem(A); + if (T.isNullValue()) + return V; + return V.isNegative() ? V+T : V+(A-T); + }; - // The C coefficient is L. - const APInt& C = L; + // The vertex of the parabola is at -B/2A, but since A > 0, it's negative + // iff B is positive. + if (B.isNonNegative()) { + // If B >= 0, the vertex it at a negative location (or at 0), so in + // order to have a non-negative solution we need to pick k that makes + // C-2kR negative. To satisfy all the requirements for the solution + // that we are looking for, it needs to be closest to 0 of all k. + C = C.srem(TwoR); + if (C.isStrictlyPositive()) + C -= TwoR; + // Pick the greater solution. + PickLow = false; + } else { + // If B < 0, the vertex is at a positive location. For any solution + // to exist, the discriminant must be non-negative. This means that + // C-2kR <= B^2/4A is a necessary condition for k, i.e. there is a + // lower bound on values of k: 2kR >= C - B^2/4A. + APInt Low2kR = C - SqrB.udiv(2*TwoA); // udiv because all values > 0. + // Round Low2kR up (towards +inf) to the nearest 2kR. + Low2kR = RoundUp(Low2kR, TwoR); + + // If there exists k meeting the condition above, and such that + // C-2kR > 0, there will be two positive real number solutions of + // q(x) = 2kR. Out of all such values of k, pick the one that makes + // C-2kR closest to 0, (i.e. pick maximum k such that C-2kR > 0). + // In other words, find maximum k such that Low2kR <= 2kR < C. + if (C.sgt(Low2kR)) { + // If Low2kR < C, then such a k is guaranteed to exist because + // Low2kR itself is a multiple of 2R. + C -= -RoundUp(-C, TwoR); // C = C - RoundDown(C, 2R) + // Pick the smaller solution. + PickLow = true; + } else { + // If C-2kR < 0 for all potential k's, it means that one solution + // will be negative, while the other will be positive. The positive + // solution will shift towards 0 if the parabola is moved up. + // Pick the 2kR closest to the lower bound (i.e. make C-2kR closest + // to 0, or in other words, out of all parabolas that have solutions, + // pick the one that is the farthest "up"). + // Since Low2kR is itself a multiple of 2R, simply take C-Low2kR. + C -= Low2kR; // Subtract Low2kR first. + // Pick the greater solution. + PickLow = false; + } + } + + LLVM_DEBUG(dbgs() << __func__ << ": updated coefficients " << A << "x^2 + " + << B << "x + " << C << " bw:" << BitWidth+1 << '\n'); + + APInt D = SqrB - 4*A*C; + assert(D.isNonNegative() && "Negative discriminant"); + APInt SQ = D.sqrt(); + + APInt Q = SQ * SQ; + bool InexactSQ = Q != D; + // The calculated SQ may actually be greater than the exact (non-integer) + // value. If that's the case, decremement SQ to get a value that is lower. + if (Q.sgt(D)) + SQ -= 1; + + APInt X; + APInt Rem; + + // SQ is rounded down (i.e SQ * SQ <= D), so the roots may be inexact. + // When using the quadratic formula directly, the calculated low root + // may be greater than the exact one, since we would be subtracting SQ. + // To make sure that the calculated root is not greater than the exact + // one, subtract SQ+1 when calculating the low root (for inexact value + // of SQ). + if (PickLow) + APInt::sdivrem(-B - (SQ+InexactSQ), TwoA, X, Rem); + else + APInt::sdivrem(-B + SQ, TwoA, X, Rem); - // Compute the B^2-4ac term. - APInt SqrtTerm = B; - SqrtTerm *= B; - SqrtTerm -= 4 * (A * C); + // The updated coefficients should be such that the (exact) solution is + // positive. Since APInt division rounds towards 0, the calculated one + // can be 0, but cannot be negative. + assert(X.isNonNegative() && "Solution should be non-negative"); - if (SqrtTerm.isNegative()) { - // The loop is provably infinite. - return None; + if (!InexactSQ && Rem.isNullValue()) { + LLVM_DEBUG(dbgs() << __func__ << ": solution (root): " << X << '\n'); + return TruncToOrig(X); } - // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest - // integer value or else APInt::sqrt() will assert. - APInt SqrtVal = SqrtTerm.sqrt(); - - // Compute the two solutions for the quadratic formula. - // The divisions must be performed as signed divisions. - APInt NegB = -std::move(B); - APInt TwoA = std::move(A); - TwoA <<= 1; - if (TwoA.isNullValue()) + assert((SQ*SQ).sle(D) && "SQ = |_sqrt(D)_|, so SQ*SQ <= D"); + // The exact value of the square root of D should be between SQ and SQ+1. + // This implies that the solution should be between that corresponding to + // SQ (i.e. X) and that corresponding to SQ+1. + // + // The calculated X cannot be greater than the exact (real) solution. + // Actually it must be strictly less than the exact solution, while + // X+1 will be greater than or equal to it. + + APInt VX = (A*X + B)*X + C; + APInt VY = VX + TwoA*X + A + B; + bool SignChange = VX.isNegative() != VY.isNegative() || + VX.isNullValue() != VY.isNullValue(); + // If the sign did not change between X and X+1, X is not a valid solution. + // This could happen when the actual (exact) roots don't have an integer + // between them, so they would both be contained between X and X+1. + if (!SignChange) { + LLVM_DEBUG(dbgs() << __func__ << ": no valid solution\n"); return None; + } - LLVMContext &Context = SE.getContext(); - - ConstantInt *Solution1 = - ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); - ConstantInt *Solution2 = - ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); - - return std::make_pair(cast(SE.getConstant(Solution1)), - cast(SE.getConstant(Solution2))); + X += 1; + LLVM_DEBUG(dbgs() << __func__ << ": solution (wrap): " << X << '\n'); + return TruncToOrig(X); } ScalarEvolution::ExitLimit @@ -8362,23 +8543,15 @@ // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of // the quadratic equation to solve it. if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { - if (auto Roots = SolveQuadraticEquation(AddRec, *this)) { - const SCEVConstant *R1 = Roots->first; - const SCEVConstant *R2 = Roots->second; - // Pick the smallest positive root value. - if (ConstantInt *CB = dyn_cast(ConstantExpr::getICmp( - CmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) { - if (!CB->getZExtValue()) - std::swap(R1, R2); // R1 is the minimum root now. - - // We can only use this value if the chrec ends up with an exact zero - // value at this index. When solving for "X*X != 5", for example, we - // should not accept a root of 2. - const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); - if (Val->isZero()) - // We found a quadratic root! - return ExitLimit(R1, R1, false, Predicates); - } + if (auto S = SolveQuadraticEquation(AddRec)) { + // We can only use this value if the chrec ends up with an exact zero + // value at this index. When solving for "X*X != 5", for example, we + // should not accept a root of 2. + const auto *R = cast(getConstant(S.getValue())); + const SCEV *Val = AddRec->evaluateAtIteration(R, *this); + if (Val->isZero()) + // We found a quadratic root! + return ExitLimit(R, R, false, Predicates); } return getCouldNotCompute(); } @@ -10486,52 +10659,65 @@ ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) && "Linear scev computation is off in a bad way!"); return SE.getConstant(ExitValue); - } else if (isQuadratic()) { + } + + if (isQuadratic()) { // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the // quadratic equation to solve it. To do this, we must frame our problem in - // terms of figuring out when zero is crossed, instead of when - // Range.getUpper() is crossed. + // terms of figuring out when zero is crossed, instead of when the upper or + // the lower bound of Range is crossed. SmallVector NewOps(op_begin(), op_end()); - NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); - const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop(), FlagAnyWrap); - - // Next, solve the constructed addrec - if (auto Roots = - SolveQuadraticEquation(cast(NewAddRec), SE)) { - const SCEVConstant *R1 = Roots->first; - const SCEVConstant *R2 = Roots->second; - // Pick the smallest positive root value. - if (ConstantInt *CB = dyn_cast(ConstantExpr::getICmp( - ICmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) { - if (!CB->getZExtValue()) - std::swap(R1, R2); // R1 is the minimum root now. - - // Make sure the root is not off by one. The returned iteration should - // not be in the range, but the previous one should be. When solving - // for "X*X < 5", for example, we should not return a root of 2. - ConstantInt *R1Val = - EvaluateConstantChrecAtConstant(this, R1->getValue(), SE); - if (Range.contains(R1Val->getValue())) { - // The next iteration must be out of the range... - ConstantInt *NextVal = - ConstantInt::get(SE.getContext(), R1->getAPInt() + 1); - - R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); - if (!Range.contains(R1Val->getValue())) - return SE.getConstant(NextVal); - return SE.getCouldNotCompute(); // Something strange happened - } + assert(NewOps[0]->isZero() && "Starting value of addrec should be 0"); + + auto IsValidSolution = [this,&Range,&SE] (const Optional &R) { + if (!R.hasValue()) + return false; - // If R1 was not in the range, then it is a good return value. Make - // sure that R1-1 WAS in the range though, just in case. - ConstantInt *NextVal = - ConstantInt::get(SE.getContext(), R1->getAPInt() - 1); - R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); - if (Range.contains(R1Val->getValue())) - return R1; - return SE.getCouldNotCompute(); // Something strange happened + // Make sure the root is not off by one. The returned iteration should + // not be in the range, but the previous one should be. When solving + // for "X*X < 5", for example, we should not return a root of 2. + + ConstantInt *Arg0 = ConstantInt::get(SE.getContext(), R.getValue()); + ConstantInt *Val0 = EvaluateConstantChrecAtConstant(this, Arg0, SE); + if (Range.contains(Val0->getValue())) { + // This can happen when the actual exit condition happens after + // several unsigned overflows, e.g. accumulating positive values + // until the result is exactly 0. The returned root will be the + // number of iterations until the first such overflow occurs. + return false; } + + // If Val(R) was not in the range, then it may be a good return value. + // Check if Val(R-1) was in the range. If the solution corresponds to + // crossing the "wrong" bound, it may be outside of the range. + ConstantInt *Arg1 = ConstantInt::get(SE.getContext(), R.getValue()-1); + ConstantInt *Val1 = EvaluateConstantChrecAtConstant(this, Arg1, SE); + return Range.contains(Val1->getValue()); + }; + + NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); + const SCEV *Upper = SE.getAddRecExpr(NewOps, getLoop(), FlagAnyWrap); + Optional SU = SolveQuadraticEquation(cast(Upper)); + bool UpperValid = IsValidSolution(SU); + + NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getLower()-1)); + const SCEV *Lower = SE.getAddRecExpr(NewOps, getLoop(), FlagAnyWrap); + Optional SL = SolveQuadraticEquation(cast(Lower)); + bool LowerValid = IsValidSolution(SL); + + if (UpperValid != LowerValid) + return UpperValid ? SE.getConstant(SU.getValue()) + : SE.getConstant(SL.getValue()); + + if (!UpperValid) { + // Neither solution is valid. + return SE.getCouldNotCompute(); } + + // Both are valid. Return the smaller one. + const APInt &U = SU.getValue(), &L = SL.getValue(); + assert(U.isNonNegative() && L.isNonNegative()); + return U.ult(L) ? SE.getConstant(U) : SE.getConstant(L); } return SE.getCouldNotCompute(); Index: test/Analysis/ScalarEvolution/solve-quadratic-i1.ll =================================================================== --- /dev/null +++ test/Analysis/ScalarEvolution/solve-quadratic-i1.ll @@ -0,0 +1,100 @@ +; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'f0': +; CHECK-NEXT: Classifying expressions for: @f0 +; CHECK-NEXT: %v0 = phi i16 [ 2, %b0 ], [ %v2, %b1 ] +; CHECK-NEXT: --> {2,+,1}<%b1> U: [2,4) S: [2,4) Exits: 3 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v1 = phi i16 [ 1, %b0 ], [ %v3, %b1 ] +; CHECK-NEXT: --> {1,+,2,+,1}<%b1> U: full-set S: full-set Exits: 3 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v2 = add nsw i16 %v0, 1 +; CHECK-NEXT: --> {3,+,1}<%b1> U: [3,5) S: [3,5) Exits: 4 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v3 = add nsw i16 %v1, %v0 +; CHECK-NEXT: --> {3,+,3,+,1}<%b1> U: full-set S: full-set Exits: 6 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v4 = and i16 %v3, 1 +; CHECK-NEXT: --> (zext i1 {true,+,true,+,true}<%b1> to i16) U: [0,2) S: [0,2) Exits: 0 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: Determining loop execution counts for: @f0 +; CHECK-NEXT: Loop %b1: backedge-taken count is 1 +; CHECK-NEXT: Loop %b1: max backedge-taken count is 1 +; CHECK-NEXT: Loop %b1: Predicated backedge-taken count is 1 +; CHECK-NEXT: Predicates: +; CHECK-EMPTY: +; CHECK-NEXT: Loop %b1: Trip multiple is 2 +define void @f0() { +b0: + br label %b1 + +b1: ; preds = %b1, %b0 + %v0 = phi i16 [ 2, %b0 ], [ %v2, %b1 ] + %v1 = phi i16 [ 1, %b0 ], [ %v3, %b1 ] + %v2 = add nsw i16 %v0, 1 + %v3 = add nsw i16 %v1, %v0 + %v4 = and i16 %v3, 1 + %v5 = icmp ne i16 %v4, 0 + br i1 %v5, label %b1, label %b2 + +b2: ; preds = %b1 + ret void +} + +@g0 = common dso_local global i16 0, align 2 +@g1 = common dso_local global i32 0, align 4 +@g2 = common dso_local global i32* null, align 8 + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'f1': +; CHECK-NEXT: Classifying expressions for: @f1 +; CHECK-NEXT: %v0 = phi i16 [ 0, %b0 ], [ %v3, %b1 ] +; CHECK-NEXT: --> {0,+,3,+,1}<%b1> U: full-set S: full-set Exits: 7 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v1 = phi i32 [ 3, %b0 ], [ %v6, %b1 ] +; CHECK-NEXT: --> {3,+,1}<%b1> U: [3,6) S: [3,6) Exits: 5 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v2 = trunc i32 %v1 to i16 +; CHECK-NEXT: --> {3,+,1}<%b1> U: [3,6) S: [3,6) Exits: 5 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v3 = add i16 %v0, %v2 +; CHECK-NEXT: --> {3,+,4,+,1}<%b1> U: full-set S: full-set Exits: 12 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v4 = and i16 %v3, 1 +; CHECK-NEXT: --> (zext i1 {true,+,false,+,true}<%b1> to i16) U: [0,2) S: [0,2) Exits: 0 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v6 = add nuw nsw i32 %v1, 1 +; CHECK-NEXT: --> {4,+,1}<%b1> U: [4,7) S: [4,7) Exits: 6 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v7 = phi i32 [ %v1, %b1 ] +; CHECK-NEXT: --> %v7 U: [3,6) S: [3,6) +; CHECK-NEXT: %v8 = phi i16 [ %v3, %b1 ] +; CHECK-NEXT: --> %v8 U: full-set S: full-set +; CHECK-NEXT: Determining loop execution counts for: @f1 +; CHECK-NEXT: Loop %b3: Unpredictable backedge-taken count. +; CHECK-NEXT: Loop %b3: Unpredictable max backedge-taken count. +; CHECK-NEXT: Loop %b3: Unpredictable predicated backedge-taken count. +; CHECK-NEXT: Loop %b1: backedge-taken count is 2 +; CHECK-NEXT: Loop %b1: max backedge-taken count is 2 +; CHECK-NEXT: Loop %b1: Predicated backedge-taken count is 2 +; CHECK-NEXT: Predicates: +; CHECK-EMPTY: +; CHECK-NEXT: Loop %b1: Trip multiple is 3 +define void @f1() #0 { +b0: + store i16 0, i16* @g0, align 2 + store i32* @g1, i32** @g2, align 8 + br label %b1 + +b1: ; preds = %b1, %b0 + %v0 = phi i16 [ 0, %b0 ], [ %v3, %b1 ] + %v1 = phi i32 [ 3, %b0 ], [ %v6, %b1 ] + %v2 = trunc i32 %v1 to i16 + %v3 = add i16 %v0, %v2 + %v4 = and i16 %v3, 1 + %v5 = icmp eq i16 %v4, 0 + %v6 = add nuw nsw i32 %v1, 1 + br i1 %v5, label %b2, label %b1 + +b2: ; preds = %b1 + %v7 = phi i32 [ %v1, %b1 ] + %v8 = phi i16 [ %v3, %b1 ] + store i32 %v7, i32* @g1, align 4 + store i16 %v8, i16* @g0, align 2 + br label %b3 + +b3: ; preds = %b3, %b2 + br label %b3 +} + +attributes #0 = { nounwind uwtable "target-cpu"="x86-64" } Index: test/Analysis/ScalarEvolution/solve-quadratic-overflow.ll =================================================================== --- /dev/null +++ test/Analysis/ScalarEvolution/solve-quadratic-overflow.ll @@ -0,0 +1,51 @@ +; RUN: opt -analyze -scalar-evolution -S < %s | FileCheck %s + +; The exit value from this loop was originally calculated as 0. +; The actual exit condition is 256*256 == 0 (in i16). + +; CHECK: Printing analysis 'Scalar Evolution Analysis' for function 'f0': +; CHECK-NEXT: Classifying expressions for: @f0 +; CHECK-NEXT: %v1 = phi i16 [ 0, %b0 ], [ %v2, %b1 ] +; CHECK-NEXT: --> {0,+,-1}<%b1> U: [-255,1) S: [-255,1) Exits: -255 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v2 = add i16 %v1, -1 +; CHECK-NEXT: --> {-1,+,-1}<%b1> U: [-256,0) S: [-256,0) Exits: -256 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v3 = mul i16 %v2, %v2 +; CHECK-NEXT: --> {1,+,3,+,2}<%b1> U: full-set S: full-set Exits: 0 LoopDispositions: { %b1: Computable } +; CHECK-NEXT: %v5 = phi i16 [ %v2, %b1 ] +; CHECK-NEXT: --> %v5 U: [-256,0) S: [-256,0) +; CHECK-NEXT: %v6 = phi i16 [ %v3, %b1 ] +; CHECK-NEXT: --> %v6 U: full-set S: full-set +; CHECK-NEXT: %v7 = sext i16 %v5 to i32 +; CHECK-NEXT: --> (sext i16 %v5 to i32) U: [-256,0) S: [-256,0) +; CHECK-NEXT: Determining loop execution counts for: @f0 +; CHECK-NEXT: Loop %b1: backedge-taken count is 255 +; CHECK-NEXT: Loop %b1: max backedge-taken count is 255 +; CHECK-NEXT: Loop %b1: Predicated backedge-taken count is 255 +; CHECK-NEXT: Predicates: +; CHECK-EMPTY: +; CHECK-NEXT: Loop %b1: Trip multiple is 256 + + +@g0 = global i32 0, align 4 +@g1 = global i16 0, align 2 + +define signext i32 @f0() { +b0: + br label %b1 + +b1: ; preds = %b1, %b0 + %v1 = phi i16 [ 0, %b0 ], [ %v2, %b1 ] + %v2 = add i16 %v1, -1 + %v3 = mul i16 %v2, %v2 + %v4 = icmp eq i16 %v3, 0 + br i1 %v4, label %b2, label %b1 + +b2: ; preds = %b1 + %v5 = phi i16 [ %v2, %b1 ] + %v6 = phi i16 [ %v3, %b1 ] + %v7 = sext i16 %v5 to i32 + store i32 %v7, i32* @g0, align 4 + store i16 %v6, i16* @g1, align 2 + ret i32 0 +} + Index: test/Analysis/ScalarEvolution/solve-quadratic.ll =================================================================== --- /dev/null +++ test/Analysis/ScalarEvolution/solve-quadratic.ll @@ -0,0 +1,262 @@ +; RUN: opt -analyze -scalar-evolution -S -debug-only=scalar-evolution < %s 2>&1 | FileCheck %s +; REQUIRES: asserts + +; Use the following template to get a chrec {L,+,M,+,N}. +; +; define signext i32 @func() { +; entry: +; br label %loop +; +; loop: +; %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] +; %inc = phi i32 [ X, %entry ], [ %inc1, %loop ] +; %acc = phi i32 [ Y, %entry ], [ %acc1, %loop ] +; %ivr1 = add i32 %ivr, %inc +; %inc1 = add i32 %inc, Z ; M = inc1 = inc + N = X + N +; %acc1 = add i32 %acc, %inc ; L = acc1 = X + Y +; %and = and i32 %acc1, 2^W-1 ; iW +; %cond = icmp eq i32 %and, 0 +; br i1 %cond, label %exit, label %loop +; +; exit: +; %rv = phi i32 [ %acc1, %loop ] +; ret i32 %rv +; } +; +; From +; X + Y = L +; X + Z = M +; Z = N +; get +; X = M - N +; Y = N - M + L +; Z = N + +; The connection between the chrec coefficients {L,+,M,+,N} and the quadratic +; coefficients is that the quadratic equation is N x^2 + (2M-N) x + 2L = 0, +; where the equation was multiplied by 2 (if needed) to make 2L an even +; number. + +; Quadratic equation: 2x^2 + 2x + 4 in i4, solution (wrap): 4 +; {14,+,14,+,14} -> X=0, Y=14, Z=14 +; +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test01' +; CHECK: SolveQuadraticEquation: solving 2x^2 + 2x + 4, bw:4 for AddRec: {-2,+,-2,+,-2}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 2x^2 + 2x + -28 bw:5 +; CHECK: SolveQuadraticEquation: solution (wrap): 4 +; CHECK: Loop %loop: Unpredictable backedge-taken count +define signext i32 @test01() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ 0, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ 14, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 14 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, 15 + %cond = icmp eq i32 %and, 0 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; Quadratic equation: 1x^2 + -73x + -146 in i32, solution (wrap): 75 +; {-72,+,-36,+,1} -> X=-37, Y=-35, Z=1 +; +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test02': +; CHECK: SolveQuadraticEquation: solving 1x^2 + -73x + -146, bw:32 for AddRec: {-73,+,-36,+,1}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 1x^2 + -73x + -146 bw:33 +; CHECK: SolveQuadraticEquation: solution (wrap): 75 +; CHECK: Loop %loop: backedge-taken count is 75 +define signext i32 @test02() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ -37, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ -35, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 1 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, -1 + %cond = icmp sgt i32 %and, 0 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; Quadratic equation: 2x^2 - 4x + 34 in i4, solution (exact): 1. +; {17,+,-1,+,2} -> X=-3, Y=20, Z=2 +; +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test03': +; CHECK: SolveQuadraticEquation: solving 2x^2 + -4x + 2, bw:4 for AddRec: {1,+,-1,+,2}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 2x^2 + -4x + 2 bw:5 +; CHECK: SolveQuadraticEquation: solution (root): 1 +; CHECK: Loop %loop: backedge-taken count is 1 +define signext i32 @test03() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ -3, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ 20, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 2 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, 15 + %cond = icmp eq i32 %and, 0 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; Quadratic equation 4x^2 + 2x + 2 in i16, solution (wrap): 181 +; {1,+,3,+,4} -> X=-1, Y=2, Z=4 (i16) +; +; This is an example where the returned solution is the first time an +; unsigned wrap occurs, whereas the actual exit condition occurs much +; later. The number of iterations returned by SolveQuadraticEquation +; is 181, but the loop will iterate 37174 times. +; +; Here is a C code that corresponds to this case that calculates the number +; of iterations: +; +; int test04() { +; int c = 0; +; int ivr = 0; +; int inc = -1; +; int acc = 2; +; +; while (acc & 0xffff) { +; c++; +; ivr += inc; +; inc += 4; +; acc += inc; +; } +; +; return c; +; } +; + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test04': +; CHECK: SolveQuadraticEquation: solving 4x^2 + 2x + 2, bw:16 for AddRec: {1,+,3,+,4}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 4x^2 + 2x + -131070 bw:17 +; CHECK: SolveQuadraticEquation: solution (wrap): 181 +; CHECK: Loop %loop: Unpredictable backedge-taken count. +define signext i32 @test04() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ -1, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ 2, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 4 + %acc1 = add i32 %acc, %inc + %and = trunc i32 %acc1 to i16 + %cond = icmp eq i16 %and, 0 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; A case with signed arithmetic, but unsigned comparison. + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test05': +; CHECK: SolveQuadraticEquation: solving 1x^2 + 1x + -4, bw:32 for AddRec: {2,+,-1,+,-1}<%loop +; CHECK: SolveQuadraticEquation: updated coefficients 1x^2 + 1x + -4 bw:33 +; CHECK: SolveQuadraticEquation: solution (wrap): 2 +; CHECK: Loop %loop: backedge-taken count is 2 + +define signext i32 @test05() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ 0, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ -1, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, -1 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, -1 + %cond = icmp ule i32 %and, -3 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; A test that used to crash with one of the earlier versions of the code. + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test06': +; CHECK: SolveQuadraticEquation: solving 1x^2 + -199999x + -4294967294, bw:32 for AddRec: {-2147483647,+,-99999,+,1}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 1x^2 + -199999x + 4294967298 bw:33 +; CHECK: SolveQuadraticEquation: solution (wrap): 24469 +; CHECK: Loop %loop: backedge-taken count is 24469 +define signext i32 @test06() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ -100000, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ 100000, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 1 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, -1 + %cond = icmp sgt i32 %and, 5 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} + +; The equation +; 532052752x^2 + -450429774x + 71188414 = 0 +; has two exact solutions (up to two decimal digits): 0.21 and 0.64. +; Since there is no integer between them, there is no integer n that either +; solves the equation exactly, or changes the sign of it between n and n+1. + +; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test07': +; CHECK: SolveQuadraticEquation: solving 532052752x^2 + -450429774x + 71188414, bw:32 for AddRec: {35594207,+,40811489,+,532052752}<%loop> +; CHECK: SolveQuadraticEquation: updated coefficients 532052752x^2 + -450429774x + 71188414 bw:33 +; CHECK: SolveQuadraticEquation: no valid solution +; CHECK: Loop %loop: Unpredictable backedge-taken count. +define signext i32 @test07() { +entry: + br label %loop + +loop: + %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ] + %inc = phi i32 [ -491241263, %entry ], [ %inc1, %loop ] + %acc = phi i32 [ 526835470, %entry ], [ %acc1, %loop ] + %ivr1 = add i32 %ivr, %inc + %inc1 = add i32 %inc, 532052752 + %acc1 = add i32 %acc, %inc + %and = and i32 %acc1, -1 + %cond = icmp eq i32 %and, 0 + br i1 %cond, label %exit, label %loop + +exit: + %rv = phi i32 [ %acc1, %loop ] + ret i32 %rv +} +