Skip to content

[ValueTracking] For NUW, X + Y are not 0 if they are nonequal #148101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jul 11, 2025

In NUW, X + Y is not 0 if X and Y are nonequal.

@AZero13 AZero13 requested a review from nikic as a code owner July 11, 2025 02:15
@llvmbot llvmbot added llvm:SelectionDAG SelectionDAGISel as well llvm:analysis Includes value tracking, cost tables and constant folding labels Jul 11, 2025
@AZero13 AZero13 marked this pull request as draft July 11, 2025 02:15
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-selectiondag

Author: AZero13 (AZero13)

Changes

In NUW, X + Y is not 0 if X and Y are nonequal.


Full diff: https://github.com/llvm/llvm-project/pull/148101.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+13-2)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+6-1)
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.
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index c1356239ad206..bd17b1d570858 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6015,10 +6015,15 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
     break;
 
   case ISD::ADD:
-    if (Op->getFlags().hasNoUnsignedWrap())
+    if (Op->getFlags().hasNoUnsignedWrap()) {
       if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
           isKnownNeverZero(Op.getOperand(0), Depth + 1))
         return true;
+      std::optional<bool> ne =
+          KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
+                        computeKnownBits(Op.getOperand(1), Depth + 1));
+      return ne && *ne;
+    }
     // TODO: There are a lot more cases we can prove for add.
     break;
 

In NUW, X + Y is not 0 if X and Y are nonequal.
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tests

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler way to state this:

Suggested change
// X + Y != 0 if X != Y, and X + Y is NUW.
// If X != Y then X +nuw Y != 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants