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

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 7 05:03:32 PST 2023


================
@@ -3141,6 +3141,73 @@ 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);
+  // Make sure GEPA pointer operand is a PHI.
+  if (!GEPA || !isa<PHINode>(GEPA->getPointerOperand()))
+    return false;
+
+  // Handle 2 incoming PHI values with one being a recursive GEP.
+  auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
+  if (PN->getNumIncomingValues() != 2)
+    return false;
+
+  // Recursive GEP in second incoming value. Always keep Recursive GEP as
+  // FirstInst
+  Value *FirstInst = nullptr, *SecondInst = 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 &&
+        isa<Constant>(GEPA->idx_begin())) {
+      FirstInst = PN->getIncomingValue(i);
+      SecondInst = PN->getIncomingValue(1 - i);
+      continue;
+    }
+  }
+  if (FirstInst == nullptr || SecondInst == nullptr)
+    return false;
+
+  // Other incoming node base should match the B base.
+  // SecondInstOffset >= OffsetB && FirstInstOffset > 0?
+  // SecondInstOffset <= OffsetB && FirstInstOffset < 0?
+  // Is non-equal if above are true.
+  APInt FirstInstOffset(Q.DL.getIndexTypeSizeInBits(FirstInst->getType()), 0);
+  FirstInst = FirstInst->stripAndAccumulateConstantOffsets(
+      Q.DL, FirstInstOffset, /* AllowNonInbounds */ true);
+  APInt SecondInstOffset(Q.DL.getIndexTypeSizeInBits(SecondInst->getType()), 0);
+  SecondInst = SecondInst->stripAndAccumulateConstantOffsets(
+      Q.DL, SecondInstOffset, /* AllowNonInbounds */ true);
+  APInt OffsetB(Q.DL.getIndexTypeSizeInBits(B->getType()), 0);
+  B = B->stripAndAccumulateConstantOffsets(Q.DL, OffsetB,
+                                           /* AllowNonInbounds */ true);
+  if (SecondInst == B &&
----------------
bipmis wrote:

FirstInst/Step is the recursive GEP whose pointer should be the PHI. `PN->getIncomingValue(i) == A` should handle that. We could possibly add another check to make sure this is true. 
Do you have an example which I could try. I can think of below(bit odd code but if that is what we want to test)
```
define i1 @recursiveGEP_withPtr_phisub3(ptr noundef %val1) {
entry:
  br label %while.cond.i

while.cond.i:
  %a.pn.i = phi ptr [ %test.0.i, %while.cond.i ], [ %val1, %entry ]
  %test.0.i = getelementptr <vscale x 16 x i8>, ptr %a.pn.i, i64 1
  %0 =  load i8, ptr %test.0.i, align 1
  %cmp3.not.i = icmp eq i8 %0, zeroinitializer
  br  i1 %cmp3.not.i, label %while.end.i, label %while.cond.i

while.end.i:
  %sub.ptr.lhs.cast.i = ptrtoint ptr %test.0.i to i64
  %sub.ptr.rhs.cast.i = ptrtoint ptr %val1 to i64
  %sub.ptr.sub.i = sub i64 %sub.ptr.lhs.cast.i, %sub.ptr.rhs.cast.i
  %bool = icmp eq i64 %sub.ptr.sub.i, 0
  ret i1 %bool
}
```

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


More information about the llvm-commits mailing list