[llvm] [BasicAA] Reuse definitive cached results before object analysis (PR #228790)

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 17:05:30 PDT 2026


=?utf-8?q?Dávid_Bolvanský?= <david.bolvansky at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/228790 at github.com>


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Dávid Bolvanský (davidbolvansky)

<details>
<summary>Changes</summary>

Reuse definitive cached alias results before repeating underlying-object and capture analysis. Assumption-dependent entries continue through the existing path, and the recursion limit is preserved.

[Compile-time tracker](https://llvm-compile-time-tracker.com/compare.php?from=88dd82f800f46c3ec4c16932a705091434e17ae0&to=46438c38cc3a91790bf03a177a9fbc6c18228e38&stat=instructions:u) shows 0.29% fewer instructions on average for AArch64 O3, including 0.79% for lencod; x86 O3 improves by 0.07%. No alias-result or generated-IR changes were observed in the tested zstd modules.

Developed with assistance from OpenAI Codex.


---
Full diff: https://github.com/llvm/llvm-project/pull/228790.diff


1 Files Affected:

- (modified) llvm/lib/Analysis/BasicAliasAnalysis.cpp (+24-1) 


``````````diff
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 3aece3ca7d769..2261cb423a7e5 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1549,6 +1549,8 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
                                       const Value *V2, LocationSize V2Size,
                                       AAQueryInfo &AAQI,
                                       const Instruction *CtxI) {
+  constexpr unsigned MaxAliasRecursionDepth = 512;
+
   // If either of the memory references is empty, it doesn't matter what the
   // pointer values are.
   if (V1Size.isZero() || V2Size.isZero())
@@ -1572,6 +1574,27 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
   if (isValueEqualInPotentialCycles(V1, V2, AAQI))
     return AliasResult::MustAlias;
 
+  // Reuse definitive recursive results before repeating object and capture
+  // analysis. Keep assumption accounting on the existing cache path below.
+  if (AAQI.Depth < MaxAliasRecursionDepth) {
+    LocationSize CacheSize1 = V1Size, CacheSize2 = V2Size;
+    if (CacheSize1.mayBeBeforePointer() || CacheSize2.mayBeBeforePointer()) {
+      CacheSize1 = LocationSize::afterPointer();
+      CacheSize2 = LocationSize::afterPointer();
+    }
+    AAQueryInfo::LocPair Locs({V1, CacheSize1, AAQI.MayBeCrossIteration},
+                              {V2, CacheSize2, AAQI.MayBeCrossIteration});
+    const bool Swapped = V1 > V2;
+    if (Swapped)
+      std::swap(Locs.first, Locs.second);
+    const auto It = AAQI.AliasCache.find(Locs);
+    if (It != AAQI.AliasCache.end() && It->second.isDefinitive()) {
+      AliasResult Result = It->second.Result;
+      Result.swap(Swapped);
+      return Result;
+    }
+  }
+
   // Figure out what objects these things are pointing to if we can.
   const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
   const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);
@@ -1683,7 +1706,7 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
   // for recursive queries. For this reason, this limit is chosen to be large
   // enough to be very rarely hit, while still being small enough to avoid
   // stack overflows.
-  if (AAQI.Depth >= 512)
+  if (AAQI.Depth >= MaxAliasRecursionDepth)
     return AliasResult::MayAlias;
 
   // Check the cache before climbing up use-def chains. This also terminates

``````````

</details>


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


More information about the llvm-commits mailing list