[clang] [analyzer] Avoid redundant lookups when removing dead bindings (PR #244748)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 12:50:42 PST 2026
https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/244748
>From d22e706ccb55c1e139aba86b30780d1d4b7434e5 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 1/3] [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 | 6 ++----
clang/lib/StaticAnalyzer/Core/SymbolManager.cpp | 15 +++++++--------
3 files changed, 11 insertions(+), 14 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 0b313e2a6dc117..37516c12843d9b 100644
--- a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
@@ -902,9 +902,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.
@@ -3170,7 +3168,7 @@ StoreRef RegionStoreManager::removeDeadBindings(Store store,
// 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.
// A cluster that has been visited has been marked live.
- B = B.filterClusters([&](const auto &E) { return W.isVisited(E.first); });
+ B = B.filterClusters([&](const auto &E) { return W.isVisited(&E.second); });
return StoreRef(B.asStore(), *this);
}
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;
}
>From 0f8c4cf823a11f300366a47ef943fb8f3dbab575 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Mon, 23 Nov 2026 12:24:26 +0000
Subject: [PATCH 2/3] [analyzer][NFC] Capitalize the symbol parameter of
markLive and isLive
Assisted-by: Claude Opus 5.5
---
.../Core/PathSensitive/SymbolManager.h | 4 +--
.../lib/StaticAnalyzer/Core/SymbolManager.cpp | 36 +++++++++----------
2 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
index 182865960053b5..acc6baa83df84e 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
@@ -597,7 +597,7 @@ class SymbolReaper {
/// It might return null.
const StackFrame *getStackFrame() const { return SF; }
- bool isLive(SymbolRef sym);
+ bool isLive(SymbolRef Sym);
bool isLiveRegion(const MemRegion *region);
bool isLive(const Expr *ExprVal, const StackFrame *SF) const;
bool isLive(const VarRegion *VR, bool includeStoreBindings = false) const;
@@ -607,7 +607,7 @@ class SymbolReaper {
/// This should never be
/// used by checkers, only by the state infrastructure such as the store and
/// environment. Checkers should instead use metadata symbols and markInUse.
- void markLive(SymbolRef sym);
+ void markLive(SymbolRef Sym);
/// Marks a symbol as important to a checker.
///
diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
index bc303dc5e6a30e..76b3fdee3c4fcf 100644
--- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
@@ -274,8 +274,8 @@ void SymbolReaper::markDependentsLive(SymbolMapTy::iterator LI) {
}
}
-void SymbolReaper::markLive(SymbolRef sym) {
- markDependentsLive(TheLiving.try_emplace(sym, NotProcessed).first);
+void SymbolReaper::markLive(SymbolRef Sym) {
+ markDependentsLive(TheLiving.try_emplace(Sym, NotProcessed).first);
}
void SymbolReaper::markLive(const MemRegion *region) {
@@ -334,53 +334,53 @@ bool SymbolReaper::isReadableRegion(const MemRegion *MR) {
return isLiveRegion(MR) || isLazilyCopiedRegion(MR);
}
-bool SymbolReaper::isLive(SymbolRef sym) {
- if (auto LI = TheLiving.find(sym); LI != TheLiving.end()) {
+bool SymbolReaper::isLive(SymbolRef Sym) {
+ if (auto LI = TheLiving.find(Sym); LI != TheLiving.end()) {
markDependentsLive(LI);
return true;
}
bool KnownLive;
- switch (sym->getKind()) {
+ switch (Sym->getKind()) {
case SymExpr::SymbolRegionValueKind:
- KnownLive = isReadableRegion(cast<SymbolRegionValue>(sym)->getRegion());
+ KnownLive = isReadableRegion(cast<SymbolRegionValue>(Sym)->getRegion());
break;
case SymExpr::SymbolConjuredKind:
KnownLive = false;
break;
case SymExpr::SymbolDerivedKind:
- KnownLive = isLive(cast<SymbolDerived>(sym)->getParentSymbol());
+ KnownLive = isLive(cast<SymbolDerived>(Sym)->getParentSymbol());
break;
case SymExpr::SymbolExtentKind:
- KnownLive = isLiveRegion(cast<SymbolExtent>(sym)->getRegion());
+ KnownLive = isLiveRegion(cast<SymbolExtent>(Sym)->getRegion());
break;
case SymExpr::SymbolMetadataKind:
- KnownLive = MetadataInUse.count(sym) &&
- isLiveRegion(cast<SymbolMetadata>(sym)->getRegion());
+ KnownLive = MetadataInUse.count(Sym) &&
+ isLiveRegion(cast<SymbolMetadata>(Sym)->getRegion());
if (KnownLive)
- MetadataInUse.erase(sym);
+ MetadataInUse.erase(Sym);
break;
case SymExpr::SymIntExprKind:
- KnownLive = isLive(cast<SymIntExpr>(sym)->getLHS());
+ KnownLive = isLive(cast<SymIntExpr>(Sym)->getLHS());
break;
case SymExpr::IntSymExprKind:
- KnownLive = isLive(cast<IntSymExpr>(sym)->getRHS());
+ KnownLive = isLive(cast<IntSymExpr>(Sym)->getRHS());
break;
case SymExpr::SymSymExprKind:
- KnownLive = isLive(cast<SymSymExpr>(sym)->getLHS()) &&
- isLive(cast<SymSymExpr>(sym)->getRHS());
+ KnownLive = isLive(cast<SymSymExpr>(Sym)->getLHS()) &&
+ isLive(cast<SymSymExpr>(Sym)->getRHS());
break;
case SymExpr::SymbolCastKind:
- KnownLive = isLive(cast<SymbolCast>(sym)->getOperand());
+ KnownLive = isLive(cast<SymbolCast>(Sym)->getOperand());
break;
case SymExpr::UnarySymExprKind:
- KnownLive = isLive(cast<UnarySymExpr>(sym)->getOperand());
+ KnownLive = isLive(cast<UnarySymExpr>(Sym)->getOperand());
break;
}
if (KnownLive)
- markLive(sym);
+ markLive(Sym);
return KnownLive;
}
>From f69b69f789abf59287278a8233f43a2fc6c4e67d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20Horv=C3=A1th?= <xazax.hun at gmail.com>
Date: Mon, 23 Nov 2026 13:33:11 +0000
Subject: [PATCH 3/3] Update clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: Balázs Benics <benicsbalazs at gmail.com>
---
clang/lib/StaticAnalyzer/Core/SymbolManager.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
index 76b3fdee3c4fcf..6cd40a845c329f 100644
--- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
@@ -264,8 +264,7 @@ void SymbolReaper::markDependentsLive(SymbolMapTy::iterator LI) {
LI->second = HaveMarkedDependents;
// Marking the dependents inserts into TheLiving, invalidating LI.
- if (const SymbolRefSmallVectorTy *Deps =
- SymMgr.getDependentSymbols(LI->first)) {
+ if (const auto *Deps = SymMgr.getDependentSymbols(LI->first)) {
for (const auto I : *Deps) {
if (TheLiving.contains(I))
continue;
More information about the cfe-commits
mailing list