[llvm] 786ce4c - [ValueTracking] Pull out logic for detecting if `(sub X, Y)` is non-zero; NFC

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 30 08:07:07 PDT 2023


Author: Noah Goldstein
Date: 2023-04-30T10:06:45-05:00
New Revision: 786ce4c4c534f234334b14c25fbcdb31566e79d4

URL: https://github.com/llvm/llvm-project/commit/786ce4c4c534f234334b14c25fbcdb31566e79d4
DIFF: https://github.com/llvm/llvm-project/commit/786ce4c4c534f234334b14c25fbcdb31566e79d4.diff

LOG: [ValueTracking] Pull out logic for detecting if `(sub X, Y)` is non-zero; NFC

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149406

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 85361e5f54d7..696c14607466 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2512,6 +2512,24 @@ static bool isNonZeroRecurrence(const PHINode *PN) {
   }
 }
 
+static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
+                         const Query &Q, unsigned BitWidth, Value *X,
+                         Value *Y) {
+  if (auto *C = dyn_cast<Constant>(X))
+    if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Depth, Q))
+      return true;
+
+  KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
+  if (XKnown.isUnknown())
+    return false;
+  KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
+  // If X != Y then X - Y is non zero.
+  std::optional<bool> ne = KnownBits::ne(XKnown, YKnown);
+  // If we are unable to compute if X != Y, we won't be able to do anything
+  // computing the knownbits of the sub expression so just return here.
+  return ne && *ne;
+}
+
 static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
                            unsigned Depth, const Query &Q,
                            const KnownBits &KnownVal) {
@@ -2705,25 +2723,9 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
             Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
       return isKnownNonZero(I->getOperand(0), Depth, Q);
     break;
-  case Instruction::Sub: {
-    if (auto *C = dyn_cast<Constant>(I->getOperand(0)))
-      if (C->isNullValue() &&
-          isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
-        return true;
-
-    KnownBits XKnown =
-        computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
-    if (!XKnown.isUnknown()) {
-      KnownBits YKnown =
-          computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
-      // If X != Y then X - Y is non zero.
-      std::optional<bool> ne = KnownBits::ne(XKnown, YKnown);
-      // If we are unable to compute if X != Y, we won't be able to do anything
-      // computing the knownbits of the sub expression so just return here.
-      return ne && *ne;
-    }
-    return false;
-  }
+  case Instruction::Sub:
+    return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
+                        I->getOperand(1));
   case Instruction::Or:
     // X | Y != 0 if X != 0 or Y != 0.
     return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||


        


More information about the llvm-commits mailing list