[clang] [analyzer] Make symbol iteration cheaper (PR #244865)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 06:59:48 PST 2026


================
@@ -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)
----------------
NagyDonat wrote:

```suggestion
      if (!isa<SymbolData>(SE))
```
I think/hope that `llvm::isa` is inlined and yields code that is equivalent to what you wrote. If the compiler cannot inline the `isa` template magic, then `if (!SymbolData::classof(SE))` is another approach, where I really don't see an obstacle to inlining.

I'm primarily suggesting this because the names of the constants `BEGIN_SYMBOLS` and `END_SYMBOLS` are really terrible IMO, they strongly look as if they delimit the range of _all_ symbol kinds and not just the `SymbolData` kinds. (I see that you added a comment to clarify that this `if` intends to skip the `SymbolData` subclasses, but I still felt it necessary to verify that this `if` doesn't skip all symbols.)

It would be nice to rename these two constants (which are as far as I see used only once in `SymbolData::classof`) to `BEGIN_DATA_SYMBOLS` and `END_DATA_SYMBOLS`. However, this probably belongs to a separate commit, and even if they are renamed, it is IMO more elegant to use `isa` or `classof` here. 

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


More information about the cfe-commits mailing list