diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -7635,6 +7635,12 @@ const SCEV *E, const SCEV *M, bool MaxOrZero, ArrayRef *> PredSetList) : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) { + // If we prove the max count is zero, so is the symbolic bound. This happens + // in practice due to differences in a) how context sensitive we've chosen + // to be and b) how we reason about bounds impied by UB. + if (MaxNotTaken->isZero()) + ExactNotTaken = MaxNotTaken; + assert((isa(ExactNotTaken) || !isa(MaxNotTaken)) && "Exact is not allowed to be less precise than Max"); @@ -11939,10 +11945,6 @@ } else { MaxBECount = computeMaxBECountForLT( Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); - // If we prove the max count is zero, so is the symbolic bound. This can - // happen due to differences in how we reason about bounds impied by UB. - if (MaxBECount->isZero()) - BECount = MaxBECount; } if (isa(MaxBECount) && diff --git a/llvm/lib/Target/ARM/MVETailPredication.cpp b/llvm/lib/Target/ARM/MVETailPredication.cpp --- a/llvm/lib/Target/ARM/MVETailPredication.cpp +++ b/llvm/lib/Target/ARM/MVETailPredication.cpp @@ -293,14 +293,18 @@ // Check for equality of TC and Ceil by calculating SCEV expression // TC - Ceil and test it for zero. // - bool Zero = SE->getMinusSCEV( - SE->getBackedgeTakenCount(L), - SE->getUDivExpr(SE->getAddExpr(SE->getMulExpr(Ceil, VW), - SE->getNegativeSCEV(VW)), - VW)) - ->isZero(); - - if (!Zero) { + const SCEV *Sub = + SE->getMinusSCEV(SE->getBackedgeTakenCount(L), + SE->getUDivExpr(SE->getAddExpr(SE->getMulExpr(Ceil, VW), + SE->getNegativeSCEV(VW)), + VW)); + + // Use context sensitive facts about the path to the loop to refine. This + // comes up as the backedge taken count can incorporate context sensitive + // reasoning, and our RHS just above doesn't. + Sub = SE->applyLoopGuards(Sub, L); + + if (!Sub->isZero()) { LLVM_DEBUG(dbgs() << "ARM TP: possible overflow in sub expression.\n"); return false; } diff --git a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll --- a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll +++ b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll @@ -523,7 +523,7 @@ ; of context sensativity. define void @ne_zero_max_btc(i32 %a) { ; CHECK-LABEL: Determining loop execution counts for: @ne_zero_max_btc -; CHECK: Loop %for.body: backedge-taken count is (-1 + (zext i32 (1 umax (1 smin %a)) to i64)) +; CHECK: Loop %for.body: backedge-taken count is 0 ; CHECK: Loop %for.body: max backedge-taken count is 0 entry: %cmp = icmp slt i32 %a, 1