Index: include/llvm/Analysis/ScalarEvolutionExpressions.h =================================================================== --- include/llvm/Analysis/ScalarEvolutionExpressions.h +++ include/llvm/Analysis/ScalarEvolutionExpressions.h @@ -343,6 +343,10 @@ /// Return the value of this chain of recurrences at the specified /// iteration number. + /// + /// Note that SCEVCouldNotCompute is returned if evaluation attempt failed. + /// For example, if precision of \p It is less than required to perform an + /// evaluation instance of SCEVCouldNotCompute is returned. const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const; /// Return the number of iterations of this loop that produce Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -1204,6 +1204,13 @@ IntegerType *CalculationTy = IntegerType::get(SE.getContext(), CalculationBits); const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); + + // Zero extension indicates insufficient precision of incoming "It". + // In this case result of calculations may be incorrect. Go ahead and return + // SCEVCouldNotCompute. + if (isa(Dividend)) + return SE.getCouldNotCompute(); + for (unsigned i = 1; i != K; ++i) { const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); Dividend = SE.getMulExpr(Dividend, @@ -11660,7 +11667,7 @@ return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; return LoopInvariant; case scCouldNotCompute: - llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); + return LoopVariant; } llvm_unreachable("Unknown SCEV kind!"); } Index: test/Transforms/IndVarSimplify/exit_value_tests.ll =================================================================== --- test/Transforms/IndVarSimplify/exit_value_tests.ll +++ test/Transforms/IndVarSimplify/exit_value_tests.ll @@ -45,34 +45,6 @@ ret i32 %Y } -define i32 @NSquareOver2(i32 %N) { -;