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 @@ -11939,6 +11939,10 @@ } 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/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 @@ -458,7 +458,7 @@ define void @max_overflow_se(i8 %n) mustprogress { ; CHECK-LABEL: Determining loop execution counts for: @max_overflow_se -; CHECK: Loop %loop: backedge-taken count is (-126 + (126 smax %n)) +; CHECK: Loop %loop: backedge-taken count is 0 ; CHECK: Loop %loop: max backedge-taken count is 0 entry: br label %loop