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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 12 07:21:12 PST 2023


================
@@ -3141,6 +3141,64 @@ static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
          isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
 }
 
+// Check to see if A is both a GEP and is the incoming value for a PHI in the
+// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
+// one of them being the recursive GEP A and the other a ptr at same base and at
+// the same/higher offset than B we are only incrementing the pointer further in
+// loop if offset of recursive GEP is greater than 0.
+static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
+                                               unsigned Depth,
+                                               const SimplifyQuery &Q) {
+  if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
+    return false;
+
+  auto *GEPA = dyn_cast<GEPOperator>(A);
+  if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
+    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) {
+      Step = PN->getIncomingValue(i);
+      Start = PN->getIncomingValue(1 - i);
+      continue;
+    }
+  }
+  if (Start == nullptr || Step == nullptr)
+    return false;
+
+  // Other incoming node base should match the B base.
+  // StartOffset >= OffsetB && StepOffset > 0?
+  // StartOffset <= OffsetB && StepOffset < 0?
+  // Is non-equal if above are true.
+  // Restrict to inbounds GEPs for now. The use of
+  // stripAndAccumulateInBoundsConstantOffsets works fine. Alternatively
----------------
david-arm wrote:

I'm not sure it's necessary to add the last sentence. Perhaps you can just say:

```
  // We use stripAndAccumulateInBoundsConstantOffsets to restrict the optimisation   
  // to inbounds GEPs only for now.
```

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


More information about the llvm-commits mailing list