[llvm] [ValueTracking] isNonEqual Pointers with with a recursive GEP (PR #70459)

David Green via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 8 04:29:46 PST 2023


================
@@ -3141,6 +3141,63 @@ static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
          isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
 }
 
+static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
+                                               unsigned Depth,
+                                               const SimplifyQuery &Q) {
+  // Where A is a recursive GEP for an incoming value of PHI indicating a
+  // loop. B can be a ptr/GEP.
+  // If the PHI has 2 incoming values, one of it being the recursive GEP A
+  // and other a ptr at same base and at an same/higher offset than B we are
+  // only incrementing the pointer further in loop if offset of recursive GEP is
+  // greater than 0.
+  if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
+    return false;
+
+  auto *GEPA = dyn_cast<GEPOperator>(A);
+  // Is of type GEP.
+  if (!GEPA)
+    return false;
+
+  // Handle 2 incoming PHI values with one being a recursive GEP.
+  auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
+  if (!PN || PN->getNumIncomingValues() != 2)
+    return false;
+
+  // Recursive GEP in second incoming value. Always keep Recursive GEP as
+  // Step
+  Value *Start = nullptr, *Step = nullptr;
+  for (unsigned i = 0; i != 2; ++i) {
+    // Check if A is a Recursive GEP in one of the incoming values.
+    if (PN->getIncomingValue(i) == A && GEPA->getNumIndices() == 1 &&
----------------
davemgreen wrote:

Could the GEPA->getNumIndices and constant offset be checked above in the `if (!GEPA)`?

The loop might not be needed too, if it just grabbed the Step and Start, swapped them if necessary and then checked Step was A. It's probably OK either way though, the end result is the same and it might not be a big difference in the code.

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


More information about the llvm-commits mailing list