[llvm] [BasicAA] Compute minimal access extents lazily (PR #226931)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 03:22:14 PDT 2026


================
@@ -117,11 +117,33 @@ static std::optional<TypeSize> getObjectSize(const Value *V,
   return std::nullopt;
 }
 
+/// Return the minimal extent from \p V to the end of the underlying object,
+/// assuming the result is used in an aliasing query. E.g., we do use the query
+/// location size and the fact that null pointers cannot alias here.
+static TypeSize getMinimalExtentFrom(const Value &V,
+                                     const LocationSize &LocSize,
+                                     const DataLayout &DL,
+                                     bool NullIsValidLoc) {
+  // If we have dereferenceability information we know a lower bound for the
+  // extent as accesses for a lower offset would be valid. We need to exclude
+  // the "or null" part if null is a valid pointer. We can ignore frees, as an
+  // access after free would be undefined behavior.
+  bool CanBeNull;
+  uint64_t DerefBytes =
+      V.getPointerDereferenceableBytes(DL, CanBeNull, /*CanBeFreed=*/nullptr);
+  DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
+  // If queried with a precise location size, we assume that location size to be
+  // accessed, thus valid.
+  if (LocSize.isPrecise())
+    DerefBytes = std::max(DerefBytes, LocSize.getValue().getKnownMinValue());
+  return TypeSize::getFixed(DerefBytes);
+}
+
 /// Returns true if we can prove that the object specified by V is smaller than
 /// Size. Bails out early unless the root object is passed as the first
 /// parameter.
----------------
antoniofrighetto wrote:

Nit: we may also want to rephrase the doc-comment as follows now.
```suggestion
/// Returns true if we can prove that the object specified by V is smaller than
/// the minimal extent accessed from OtherV with size OtherSize. Bails out early
/// unless the root object is passed as the first parameter.
```

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


More information about the llvm-commits mailing list