[clang] [analyzer] Avoid redundant lookups when removing dead bindings (PR #244748)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 21 00:21:18 PST 2026
https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/244748
>From 67b4f11ec8b3e09ed3b0c4c97d66392b3af9130e Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Fri, 20 Nov 2026 23:29:52 +0000
Subject: [PATCH] [analyzer] Avoid redundant lookups when removing dead
bindings
RegionStoreManager::removeDeadBindings looked up every cluster it was
already iterating over to check whether it was visited. SymbolReaper
re-walked the dependents of symbols that were already marked live and
looked up symbols twice. This reduces the instructions of analyzing
some LLVM TUs by about 1%.
Assisted-by: Claude Opus 5.5
---
.../Core/PathSensitive/SymbolManager.h | 4 ++--
clang/lib/StaticAnalyzer/Core/RegionStore.cpp | 8 +++-----
clang/lib/StaticAnalyzer/Core/SymbolManager.cpp | 15 +++++++--------
3 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
index 62eda2aae0171c..182865960053b5 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
@@ -645,8 +645,8 @@ class SymbolReaper {
// is readable.
bool isReadableRegion(const MemRegion *region);
- /// Mark the symbols dependent on the input symbol as live.
- void markDependentsLive(SymbolRef sym);
+ /// Mark the symbols dependent on the live symbol \p LI as live.
+ void markDependentsLive(SymbolMapTy::iterator LI);
};
class SymbolVisitor {
diff --git a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
index 6994917470ce84..ee6a1d22abed79 100644
--- a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
@@ -896,9 +896,7 @@ class ClusterAnalysis {
RegionBindingsRef getRegionBindings() const { return B; }
- bool isVisited(const MemRegion *R) {
- return Visited.count(getCluster(R));
- }
+ bool isVisited(const ClusterBindings *C) const { return Visited.count(C); }
void GenerateClusters() {
// Scan the entire set of bindings and record the region clusters.
@@ -3163,10 +3161,10 @@ StoreRef RegionStoreManager::removeDeadBindings(Store store,
// We have now scanned the store, marking reachable regions and symbols
// as live. We now remove all the regions that are dead from the store
// as well as update DSymbols with the set symbols that are now dead.
- for (const MemRegion *Base : llvm::make_first_range(B)) {
+ for (const auto &[Base, Cluster] : B) {
// If the cluster has been visited, we know the region has been marked.
// Otherwise, remove the dead entry.
- if (!W.isVisited(Base))
+ if (!W.isVisited(&Cluster))
B = B.removeCluster(Base);
}
diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
index 1b217be452c6bd..bc303dc5e6a30e 100644
--- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
@@ -257,15 +257,15 @@ const SymbolRefSmallVectorTy *SymbolManager::getDependentSymbols(
return I->second.get();
}
-void SymbolReaper::markDependentsLive(SymbolRef sym) {
+void SymbolReaper::markDependentsLive(SymbolMapTy::iterator LI) {
// Do not mark dependents more then once.
- SymbolMapTy::iterator LI = TheLiving.find(sym);
- assert(LI != TheLiving.end() && "The primary symbol is not live.");
if (LI->second == HaveMarkedDependents)
return;
LI->second = HaveMarkedDependents;
- if (const SymbolRefSmallVectorTy *Deps = SymMgr.getDependentSymbols(sym)) {
+ // Marking the dependents inserts into TheLiving, invalidating LI.
+ if (const SymbolRefSmallVectorTy *Deps =
+ SymMgr.getDependentSymbols(LI->first)) {
for (const auto I : *Deps) {
if (TheLiving.contains(I))
continue;
@@ -275,8 +275,7 @@ void SymbolReaper::markDependentsLive(SymbolRef sym) {
}
void SymbolReaper::markLive(SymbolRef sym) {
- TheLiving[sym] = NotProcessed;
- markDependentsLive(sym);
+ markDependentsLive(TheLiving.try_emplace(sym, NotProcessed).first);
}
void SymbolReaper::markLive(const MemRegion *region) {
@@ -336,8 +335,8 @@ bool SymbolReaper::isReadableRegion(const MemRegion *MR) {
}
bool SymbolReaper::isLive(SymbolRef sym) {
- if (TheLiving.count(sym)) {
- markDependentsLive(sym);
+ if (auto LI = TheLiving.find(sym); LI != TheLiving.end()) {
+ markDependentsLive(LI);
return true;
}
More information about the cfe-commits
mailing list