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

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 29 15:06:25 PDT 2023


goldstein.w.n updated this revision to Diff 518240.
goldstein.w.n added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149406/new/

https://reviews.llvm.org/D149406

Files:
  llvm/lib/Analysis/ValueTracking.cpp


Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2512,6 +2512,24 @@
   }
 }
 
+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 @@
             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) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149406.518240.patch
Type: text/x-patch
Size: 2378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230429/91c8bd15/attachment.bin>


More information about the llvm-commits mailing list