[llvm] [InstSimplify] Consider `dereferenceable(N)` when simplifying pointer equalities (PR #203867)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 07:31:25 PDT 2026


================
@@ -2766,23 +2785,29 @@ static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
   if (ICmpInst::isEquality(Pred)) {
     // Different non-empty allocations that exist at the same time have
     // different addresses (if the program can tell). If the offsets are
-    // within the bounds of their allocations (and not one-past-the-end!
-    // so we can't use inbounds!), and their allocations aren't the same,
+    // within the bounds of their allocations (and not one-past-the-end,
+    // so inbounds is not sufficient), and their allocations aren't the same,
     // the pointers are not equal.
     if (haveNonOverlappingStorage(LHS, RHS)) {
       uint64_t LHSSize, RHSSize;
       ObjectSizeOpts Opts;
       Opts.EvalMode = ObjectSizeOpts::Mode::Min;
-      auto *F = [](Value *V) -> Function * {
-        if (auto *I = dyn_cast<Instruction>(V))
-          return I->getFunction();
-        if (auto *A = dyn_cast<Argument>(V))
-          return A->getParent();
-        return nullptr;
-      }(LHS);
+      const Function *F = Q.CxtI ? Q.CxtI->getFunction() : nullptr;
       Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
-      if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
-          getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
+
+      // Size of object V, falling back to `dereferenceable(N)` attribute on an
+      // argument when getObjectSize cannot determine a concrete size.
+      auto GetKnownSize = [&](Value *V, uint64_t &Size) {
+        if (getObjectSize(V, Size, DL, TLI, Opts) && Size != 0)
+          return true;
+        if (auto *A = dyn_cast<Argument>(V)) {
+          Size = A->getDereferenceableBytes();
+          return true;
+        }
----------------
antoniofrighetto wrote:

Sure thing (had avoided getPointerDereferenceableBytes() as we only allow when `A->getDereferenceableBytes() > 0` holds above, but this looks clearer).

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


More information about the llvm-commits mailing list