From 6149ad2f8e20e31e789663a422756f2911d7aae7 Mon Sep 17 00:00:00 2001 From: Rose Date: Thu, 10 Jul 2025 22:05:10 -0400 Subject: [PATCH] [ValueTracking] For NUW, X + Y are not 0 if they are nonequal In NUW, X + Y is not 0 if X and Y are nonequal. --- llvm/lib/Analysis/ValueTracking.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 21f844c4d2f45..154dce6f1dfd5 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -2833,19 +2833,27 @@ static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, if (matchOpWithOpEqZero(X, Y)) return true; - if (NUW) + if (NUW) { + // X + Y != 0 if X != Y, and X + Y is NUW. + if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth)) + return true; return isKnownNonZero(Y, DemandedElts, Q, Depth) || isKnownNonZero(X, DemandedElts, Q, Depth); + } KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth); KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth); // If X and Y are both non-negative (as signed values) then their sum is not // zero unless both X and Y are zero. - if (XKnown.isNonNegative() && YKnown.isNonNegative()) + if (XKnown.isNonNegative() && YKnown.isNonNegative()) { if (isKnownNonZero(Y, DemandedElts, Q, Depth) || isKnownNonZero(X, DemandedElts, Q, Depth)) return true; + // X + Y != 0 if X != Y, and X + Y are both nonnegative. + if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth)) + return true; + } // If X and Y are both negative (as signed values) then their sum is not // zero unless both X and Y equal INT_MIN. @@ -2859,6 +2867,9 @@ static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, // to INT_MIN. if (YKnown.One.intersects(Mask)) return true; + // X + Y != 0 because if they are not equal, they cannot both be INT_MIN. + if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth)) + return true; } // The sum of a non-negative number and a power of two is not zero.