[llvm] [BasicAA] Reuse definitive cached results before object analysis (PR #228790)
Dávid Bolvanský via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 3 17:04:45 PDT 2026
https://github.com/davidbolvansky created https://github.com/llvm/llvm-project/pull/228790
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.
>From 46438c38cc3a91790bf03a177a9fbc6c18228e38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= <david.bolvansky at gmail.com>
Date: Sun, 4 Oct 2026 00:05:14 +0200
Subject: [PATCH 1/2] [BasicAA] Experiment with early definitive cache lookups
---
llvm/lib/Analysis/BasicAliasAnalysis.cpp | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 3aece3ca7d769..d5fa30879bb1f 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1572,6 +1572,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 < 512) {
+ 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});
+ bool Swapped = V1 > V2;
+ if (Swapped)
+ std::swap(Locs.first, Locs.second);
+ 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);
>From a6b265cbfc2bd6cb43ce3e4655615ff34e809ef6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= <david.bolvansky at gmail.com>
Date: Sun, 4 Oct 2026 02:03:31 +0200
Subject: [PATCH 2/2] [BasicAA] Share the alias recursion limit with early
cache lookup
---
llvm/lib/Analysis/BasicAliasAnalysis.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index d5fa30879bb1f..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())
@@ -1574,7 +1576,7 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
// Reuse definitive recursive results before repeating object and capture
// analysis. Keep assumption accounting on the existing cache path below.
- if (AAQI.Depth < 512) {
+ if (AAQI.Depth < MaxAliasRecursionDepth) {
LocationSize CacheSize1 = V1Size, CacheSize2 = V2Size;
if (CacheSize1.mayBeBeforePointer() || CacheSize2.mayBeBeforePointer()) {
CacheSize1 = LocationSize::afterPointer();
@@ -1582,10 +1584,10 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
}
AAQueryInfo::LocPair Locs({V1, CacheSize1, AAQI.MayBeCrossIteration},
{V2, CacheSize2, AAQI.MayBeCrossIteration});
- bool Swapped = V1 > V2;
+ const bool Swapped = V1 > V2;
if (Swapped)
std::swap(Locs.first, Locs.second);
- auto It = AAQI.AliasCache.find(Locs);
+ const auto It = AAQI.AliasCache.find(Locs);
if (It != AAQI.AliasCache.end() && It->second.isDefinitive()) {
AliasResult Result = It->second.Result;
Result.swap(Swapped);
@@ -1704,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
More information about the llvm-commits
mailing list