[llvm] [ValueTracking] isNonZero sub of ptr2int's with recursive GEP (PR #68680)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 10:24:08 PDT 2023


================
@@ -2410,9 +2410,76 @@ static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
       .isNonZero();
 }
 
+static bool isNonZeroSubWithRecursiveGEP(const SimplifyQuery &Q, Value *X,
+                                         Value *Y) {
+  // Handle sub(PtrtoInt(LHS), PtrtoInt(RHS))
+  // Where LHS is a recursive GEP for an incoming value of PHI indicating a
+  // loop. RHS can be a ptr/GEP.
+  // If the PHI has 2 incoming values, one of it being the recursive GEP
+  // and other a ptr at same base and at an same/higher offset than RHS we are
+  // only incrementing the pointer further in loop if offset of recursive GEP is
+  // greater than 0.
+  Value *LHS, *RHS;
+  // sub(PtrtoInt(LHS), PtrtoInt(RHS))
+  if (match(X, m_PtrToInt(m_Value(LHS))) &&
+      match(Y, m_PtrToInt(m_Value(RHS)))) {
+    GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS);
+    // Make sure LHS GEP pointer operand is a PHI. If sub(A,B) is non zero, so
+    // is sub(B,A)
+    if (!LHSGEP || !isa<PHINode>(LHSGEP->getPointerOperand())) {
+      LHSGEP = dyn_cast<GEPOperator>(RHS);
+      std::swap(LHS, RHS);
+    }
----------------
goldsteinn wrote:

Imo variables should by `X` / `Y` instead of `LHS`/`RHS` they get swapped around.
The `LHS`/`RHS` variable names make it seem like their side matters.

https://github.com/llvm/llvm-project/pull/68680


More information about the llvm-commits mailing list