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

Dávid Bolvanský via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 02:37:38 PDT 2026


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

`aliasCheck` computes the minimal extent of an access before calling `isObjectSmallerThan`, even though the latter immediately returns false when the other pointer is not an identified object.

Move the extent computation behind the existing `isIdentifiedObject` check. This preserves the result: whenever the extent can affect the answer, the same value is computed and used as before; otherwise only unused work is skipped.

Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=7cb518a5a1acf0c2c1df7b5ed207518c45046916&to=1c28ba1fde3233237c484012803e94713a8d58b6&stat=instructions%3Au

>From 1c28ba1fde3233237c484012803e94713a8d58b6 Mon Sep 17 00:00:00 2001
From: David Bolvansky <david.bolvansky at gmail.com>
Date: Mon, 28 Sep 2026 10:15:24 +0200
Subject: [PATCH] BasicAA: Compute minimal access extents lazily

---
 llvm/lib/Analysis/BasicAliasAnalysis.cpp | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index fd9a7bb935170..462f69d3a5ad6 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -120,7 +120,13 @@ static std::optional<TypeSize> getObjectSize(const Value *V,
 /// 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.
-static bool isObjectSmallerThan(const Value *V, TypeSize Size,
+static TypeSize getMinimalExtentFrom(const Value &V,
+                                     const LocationSize &LocSize,
+                                     const DataLayout &DL,
+                                     bool NullIsValidLoc);
+
+static bool isObjectSmallerThan(const Value *V, const Value &OtherV,
+                                LocationSize OtherSize,
                                 const DataLayout &DL,
                                 const TargetLibraryInfo &TLI,
                                 bool NullIsValidLoc) {
@@ -147,6 +153,9 @@ static bool isObjectSmallerThan(const Value *V, TypeSize Size,
   if (!isIdentifiedObject(V))
     return false;
 
+  TypeSize Size =
+      getMinimalExtentFrom(OtherV, OtherSize, DL, NullIsValidLoc);
+
   // This function needs to use the aligned object size because we allow
   // reads a bit past the end given sufficient alignment.
   std::optional<TypeSize> ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
@@ -1615,12 +1624,8 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
   // If the size of one access is larger than the entire object on the other
   // side, then we know such behavior is undefined and can assume no alias.
   bool NullIsValidLocation = NullPointerIsDefined(&F);
-  if ((isObjectSmallerThan(
-          O2, getMinimalExtentFrom(*V1, V1Size, DL, NullIsValidLocation), DL,
-          TLI, NullIsValidLocation)) ||
-      (isObjectSmallerThan(
-          O1, getMinimalExtentFrom(*V2, V2Size, DL, NullIsValidLocation), DL,
-          TLI, NullIsValidLocation)))
+  if (isObjectSmallerThan(O2, *V1, V1Size, DL, TLI, NullIsValidLocation) ||
+      isObjectSmallerThan(O1, *V2, V2Size, DL, TLI, NullIsValidLocation))
     return AliasResult::NoAlias;
 
   if (EnableSeparateStorageAnalysis) {



More information about the llvm-commits mailing list