https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/244865
SymExpr::symbols() returned an llvm::iterator_range, which stores both iterators, and each iterator owned a SmallVector worklist, so every range-for copied worklists. The iterator operations were also out of line, and comparing against the end iterator compared whole worklists. Return a symbol_range that owns the worklist instead, and make the iterator a pointer to it. The range is neither copyable nor movable, and begin() is lifetimebound, so -Wdangling catches iterators that outlive a temporary range. The iterators are single-pass and only meant to be compared against end(). Also make the iterator operations inline and skip expand() for SymbolData, which has no operands. On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 3.9-5.1% and the cycles by 2.8-4.4%. Peak memory is unchanged, and so are the analyzer statistics and diagnostics. Assisted-by: Claude Opus 5.5 From b00b66a95071f6403a9656822d8b6f0189d9eeb8 Mon Sep 17 00:00:00 2001 From: Gabor Horvath <[email protected]> Date: Sat, 21 Nov 2026 01:37:23 +0000 Subject: [PATCH] [analyzer] Make symbol iteration cheaper SymExpr::symbols() returned an llvm::iterator_range, which stores both iterators, and each iterator owned a SmallVector worklist, so every range-for copied worklists. The iterator operations were also out of line, and comparing against the end iterator compared whole worklists. Return a symbol_range that owns the worklist instead, and make the iterator a pointer to it. The range is neither copyable nor movable, and begin() is lifetimebound, so -Wdangling catches iterators that outlive a temporary range. The iterators are single-pass and only meant to be compared against end(). Also make the iterator operations inline and skip expand() for SymbolData, which has no operands. On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 3.9-5.1% and the cycles by 2.8-4.4%. Peak memory is unchanged, and so are the analyzer statistics and diagnostics. Assisted-by: Claude Opus 5.5 --- .../StaticAnalyzer/Core/PathSensitive/SVals.h | 7 +-- .../Core/PathSensitive/SymExpr.h | 57 +++++++++++++++---- .../lib/StaticAnalyzer/Core/SymbolManager.cpp | 39 +++---------- 3 files changed, 56 insertions(+), 47 deletions(-) diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h index 0561a2b8d1d77b..0f4c50d323c5f7 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h @@ -159,11 +159,8 @@ class SVal { void dumpToStream(raw_ostream &OS) const; void dump() const; - llvm::iterator_range<SymExpr::symbol_iterator> symbols() const { - if (const SymExpr *SE = getAsSymbol(/*IncludeBaseRegions=*/true)) - return SE->symbols(); - SymExpr::symbol_iterator end{}; - return llvm::make_range(end, end); + SymExpr::symbol_range symbols() const { + return SymExpr::symbol_range(getAsSymbol(/*IncludeBaseRegions=*/true)); } /// Try to get a reasonable type for the given value. diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h index 6233a22d2ca2b6..26d145deed94df 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h @@ -18,6 +18,7 @@ #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Support/Compiler.h" #include <cassert> namespace clang { @@ -88,25 +89,61 @@ class SymExpr : public llvm::FoldingSetNode { /// For SymbolData, it's the symbol itself; for expressions, it's the /// expression symbol and all the operands in it. Note, SymbolDerived is /// treated as SymbolData - the iterator will NOT visit the parent region. + /// + /// The worklist is owned by the symbol_range, so the iterators are + /// single-pass and only meant to be compared against the end iterator. class symbol_iterator { - SmallVector<const SymExpr *, 5> itr; + SmallVectorImpl<const SymExpr *> *WL = nullptr; - void expand(); + void expand(const SymExpr *SE); + bool atEnd() const { return !WL || WL->empty(); } public: symbol_iterator() = default; - symbol_iterator(const SymExpr *SE); + explicit symbol_iterator(SmallVectorImpl<const SymExpr *> *WL) : WL(WL) {} + + symbol_iterator &operator++() { + assert(!atEnd() && "attempting to iterate on an 'end' iterator"); + const SymExpr *SE = WL->pop_back_val(); + // SymbolData has no operands, which is by far the most common case. + Kind K = SE->getKind(); + if (K < BEGIN_SYMBOLS || K > END_SYMBOLS) + expand(SE); + return *this; + } + + const SymExpr *operator*() const { + assert(!atEnd() && "attempting to dereference an 'end' iterator"); + return WL->back(); + } + + bool operator==(const symbol_iterator &X) const { + return atEnd() == X.atEnd(); + } + bool operator!=(const symbol_iterator &X) const { return !(*this == X); } + }; - symbol_iterator &operator++(); - const SymExpr *operator*(); + /// A single-pass range of symbol_iterator that owns the worklist. + class symbol_range { + SmallVector<const SymExpr *, 5> Worklist; - bool operator==(const symbol_iterator &X) const; - bool operator!=(const symbol_iterator &X) const; + public: + explicit symbol_range(const SymExpr *Root) { + if (Root) + Worklist.push_back(Root); + } + symbol_range(const symbol_range &) = delete; + symbol_range &operator=(const symbol_range &) = delete; + symbol_range(symbol_range &&) = delete; + symbol_range &operator=(symbol_range &&) = delete; + + symbol_iterator begin() LLVM_LIFETIME_BOUND { + return symbol_iterator(&Worklist); + } + symbol_iterator end() { return symbol_iterator(); } }; - llvm::iterator_range<symbol_iterator> symbols() const { - return llvm::make_range(symbol_iterator(this), symbol_iterator()); - } + symbol_range symbols() const { return symbol_range(this); } virtual unsigned computeComplexity() const = 0; diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp index 1b217be452c6bd..8f5c2c11e31fc1 100644 --- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp +++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp @@ -149,32 +149,7 @@ void SymbolRegionValue::dumpToStream(raw_ostream &os) const { os << getKindStr() << getSymbolID() << '<' << getType() << ' ' << R << '>'; } -bool SymExpr::symbol_iterator::operator==(const symbol_iterator &X) const { - return itr == X.itr; -} - -bool SymExpr::symbol_iterator::operator!=(const symbol_iterator &X) const { - return itr != X.itr; -} - -SymExpr::symbol_iterator::symbol_iterator(const SymExpr *SE) { - itr.push_back(SE); -} - -SymExpr::symbol_iterator &SymExpr::symbol_iterator::operator++() { - assert(!itr.empty() && "attempting to iterate on an 'end' iterator"); - expand(); - return *this; -} - -SymbolRef SymExpr::symbol_iterator::operator*() { - assert(!itr.empty() && "attempting to dereference an 'end' iterator"); - return itr.back(); -} - -void SymExpr::symbol_iterator::expand() { - const SymExpr *SE = itr.pop_back_val(); - +void SymExpr::symbol_iterator::expand(const SymExpr *SE) { switch (SE->getKind()) { case SymExpr::SymbolRegionValueKind: case SymExpr::SymbolConjuredKind: @@ -183,21 +158,21 @@ void SymExpr::symbol_iterator::expand() { case SymExpr::SymbolMetadataKind: return; case SymExpr::SymbolCastKind: - itr.push_back(cast<SymbolCast>(SE)->getOperand()); + WL->push_back(cast<SymbolCast>(SE)->getOperand()); return; case SymExpr::UnarySymExprKind: - itr.push_back(cast<UnarySymExpr>(SE)->getOperand()); + WL->push_back(cast<UnarySymExpr>(SE)->getOperand()); return; case SymExpr::SymIntExprKind: - itr.push_back(cast<SymIntExpr>(SE)->getLHS()); + WL->push_back(cast<SymIntExpr>(SE)->getLHS()); return; case SymExpr::IntSymExprKind: - itr.push_back(cast<IntSymExpr>(SE)->getRHS()); + WL->push_back(cast<IntSymExpr>(SE)->getRHS()); return; case SymExpr::SymSymExprKind: { const auto *x = cast<SymSymExpr>(SE); - itr.push_back(x->getLHS()); - itr.push_back(x->getRHS()); + WL->push_back(x->getLHS()); + WL->push_back(x->getRHS()); return; } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
