[clang] [analyzer] Compute callee properties once per CallDescriptionMap lookup (PR #245630)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 06:48:23 PST 2026


https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/245630

CallDescriptionMap::lookup matched the call against every entry with CallDescription::matches, which looked up the callee declaration and the parameters through virtual CallEvent methods for each entry, and for C library entries recomputed the builtin ID, the builtin name string, whether the builtin is a predefined library function and the declaration context checks of isCLibraryFunction.

Compute the callee, the argument and parameter counts and whether it is a method once per lookup, and the C library function data at most once. Split CheckerContext::isCLibraryFunction into private helpers for the callee-dependent parts and the name matching, which CallDescription can reuse; the public API and the matching are the same as before, including the exact-only matching of predefined library functions. Also check the cheap isHardenedVariantOf before isCLibraryFunction.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 5.1-7.8% and the cycles by 3.5-5.8%; peak memory is unchanged within noise. Most of the win comes from looking up the callee once per lookup instead of once per entry: that part alone gives 4.9-7.2%. 

Assisted-by: Claude Opus 5.5

>From de20a8f90e69f3010e18dec59c0fef717f2fc2b8 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Sun, 22 Nov 2026 19:38:29 +0000
Subject: [PATCH] [analyzer] Compute callee properties once per
 CallDescriptionMap lookup

CallDescriptionMap::lookup matched the call against every entry with
CallDescription::matches, which looked up the callee declaration and the
parameters through virtual CallEvent methods for each entry, and for C
library entries recomputed the builtin ID, the builtin name string, whether
the builtin is a predefined library function and the declaration context
checks of isCLibraryFunction.

Compute the callee, the argument and parameter counts and whether it is a
method once per lookup, and the C library function data at most once. Split
CheckerContext::isCLibraryFunction into private helpers for the
callee-dependent parts and the name matching, which CallDescription can
reuse; the public API and the matching are the same as before, including
the exact-only matching of predefined library functions. Also check the
cheap isHardenedVariantOf before isCLibraryFunction.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces
the instructions of the static analyzer by 5.1-7.8% and the cycles by
3.5-5.8%; peak memory is unchanged within noise. Most of the win comes from
looking up the callee once per lookup instead of once per entry: that part
alone gives 4.9-7.2%. The analyzer statistics and the diagnostics are
unchanged.

Assisted-by: Claude Opus 5.5
---
 .../Core/PathSensitive/CallDescription.h      | 50 +++++++++++---
 .../Core/PathSensitive/CheckerContext.h       | 27 ++++++++
 .../StaticAnalyzer/Core/CallDescription.cpp   | 68 ++++++++++++++-----
 .../StaticAnalyzer/Core/CheckerContext.cpp    | 63 +++++++++++------
 4 files changed, 161 insertions(+), 47 deletions(-)

diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
index 3c242898ef6cdf..319de16e0b9926 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h
@@ -80,6 +80,35 @@ class CallDescription {
   MaybeCount RequiredParams;
   Mode MatchAs;
 
+  template <typename T> friend class CallDescriptionMap;
+
+  /// The properties of a call that are needed for matching it. They are
+  /// computed once when a call is matched against many CallDescriptions.
+  class Callee {
+    /// The callee-dependent data of CheckerContext::isCLibraryFunction.
+    struct CLibraryInfo {
+      std::string BuiltinName; // Empty if not a builtin.
+      bool IsPredefinedLibFunction;
+      bool IsCLibraryDecl;
+    };
+    /// Computed on first use, as only C library entries need it.
+    mutable std::optional<CLibraryInfo> CLib;
+
+    const CLibraryInfo &getCLibraryInfo() const;
+
+  public:
+    const FunctionDecl *FD = nullptr;
+    size_t ArgCount = 0;
+    size_t ParamCount = 0;
+    bool IsMethod = false;
+
+    explicit Callee(const CallEvent &Call);
+    explicit Callee(const CallExpr &CE);
+
+    /// Equivalent to CheckerContext::isCLibraryFunction(FD, Name).
+    bool isCLibraryFunction(StringRef Name) const;
+  };
+
 public:
   /// Constructs a CallDescription object.
   ///
@@ -182,8 +211,7 @@ class CallDescription {
   /// @}
 
 private:
-  bool matchesImpl(const FunctionDecl *Callee, size_t ArgCount,
-                   size_t ParamCount) const;
+  bool matchesImpl(const Callee &C) const;
 
   bool matchNameOnly(const NamedDecl *ND) const;
   bool matchQualifiedNameParts(const Decl *D) const;
@@ -220,13 +248,7 @@ template <typename T> class CallDescriptionMap {
   CallDescriptionMap &operator=(CallDescriptionMap &&) = default;
 
   [[nodiscard]] const T *lookup(const CallEvent &Call) const {
-    // Slow path: linear lookup.
-    // TODO: Implement some sort of fast path.
-    for (const std::pair<CallDescription, T> &I : LinearMap)
-      if (I.first.matches(Call))
-        return &I.second;
-
-    return nullptr;
+    return lookupImpl(CallDescription::Callee(Call));
   }
 
   /// When available, always prefer lookup with a CallEvent! This function
@@ -242,10 +264,18 @@ template <typename T> class CallDescriptionMap {
   /// CallEvent::getNumArgs), the called function if it was called through a
   /// function pointer, and other information not available syntactically.
   [[nodiscard]] const T *lookupAsWritten(const CallExpr &Call) const {
+    return lookupImpl(CallDescription::Callee(Call));
+  }
+
+private:
+  const T *lookupImpl(const CallDescription::Callee &C) const {
+    if (!C.FD)
+      return nullptr;
+
     // Slow path: linear lookup.
     // TODO: Implement some sort of fast path.
     for (const std::pair<CallDescription, T> &I : LinearMap)
-      if (I.first.matchesAsWritten(Call))
+      if (I.first.matchesImpl(C))
         return &I.second;
 
     return nullptr;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 02b59ced4321fe..3c406c45293ed5 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -435,6 +435,33 @@ class CheckerContext {
   std::string getMacroNameOrSpelling(SourceLocation &Loc);
 
 private:
+  // CallDescription matches a function against many names, so it computes the
+  // callee-dependent parts of isCLibraryFunction only once.
+  friend class CallDescription;
+
+  /// Equivalent to isCLibraryFunction(FD, Name), for callers that match the
+  /// same function against many names: with BId = FD->getBuiltinID(),
+  /// \p BuiltinName must be getBuiltinName(FD, BId), \p IsPredefinedLibFunction
+  /// must be isPredefinedLibFunction(FD, BId) and \p IsCLibraryDecl must be
+  /// isCLibraryDecl(FD).
+  static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
+                                 StringRef BuiltinName,
+                                 bool IsPredefinedLibFunction,
+                                 bool IsCLibraryDecl);
+
+  /// Returns the name of the builtin \p BId of \p FD, or an empty string if
+  /// \p BId is 0.
+  static std::string getBuiltinName(const FunctionDecl *FD, unsigned BId);
+
+  /// Returns true if \p BId is a builtin that is a predefined library function
+  /// (e.g. getlogin_r), for which no fuzzy name matching is done.
+  static bool isPredefinedLibFunction(const FunctionDecl *FD, unsigned BId);
+
+  /// Returns true if \p FD is declared like a C library function: it has an
+  /// identifier, it is in the top-level or the \c std namespace, and it is
+  /// externally visible or inline.
+  static bool isCLibraryDecl(const FunctionDecl *FD);
+
   ExplodedNode *addTransitionImpl(const ProgramStateRef &State, bool MarkAsSink,
                                   ExplodedNode *P, const ProgramPointTag *Tag) {
     Changed = true;
diff --git a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
index 9199e764bb2f36..234ceaaea05dfc 100644
--- a/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallDescription.cpp
@@ -47,24 +47,55 @@ ento::CallDescription::CallDescription(Mode MatchAs,
                   [](StringRef From) { return From.str(); });
 }
 
-bool ento::CallDescription::matches(const CallEvent &Call) const {
+ento::CallDescription::Callee::Callee(const CallEvent &Call) {
   // FIXME: Add ObjC Message support.
   if (Call.getKind() == CE_ObjCMessage)
-    return false;
+    return;
 
-  const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
+  FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
   if (!FD)
-    return false;
+    return;
 
-  return matchesImpl(FD, Call.getNumArgs(), Call.parameters().size());
+  ArgCount = Call.getNumArgs();
+  // Same as Call.parameters().size() for calls of FunctionDecls.
+  ParamCount = FD->param_size();
+  IsMethod = isa<CXXMethodDecl>(FD);
 }
 
-bool ento::CallDescription::matchesAsWritten(const CallExpr &CE) const {
-  const auto *FD = dyn_cast_or_null<FunctionDecl>(CE.getCalleeDecl());
+ento::CallDescription::Callee::Callee(const CallExpr &CE) {
+  FD = dyn_cast_or_null<FunctionDecl>(CE.getCalleeDecl());
   if (!FD)
-    return false;
+    return;
+
+  ArgCount = CE.getNumArgs();
+  ParamCount = FD->param_size();
+  IsMethod = isa<CXXMethodDecl>(FD);
+}
+
+const ento::CallDescription::Callee::CLibraryInfo &
+ento::CallDescription::Callee::getCLibraryInfo() const {
+  if (!CLib) {
+    unsigned BId = FD->getBuiltinID();
+    CLib = CLibraryInfo{CheckerContext::getBuiltinName(FD, BId),
+                        CheckerContext::isPredefinedLibFunction(FD, BId),
+                        CheckerContext::isCLibraryDecl(FD)};
+  }
+  return *CLib;
+}
 
-  return matchesImpl(FD, CE.getNumArgs(), FD->param_size());
+bool ento::CallDescription::Callee::isCLibraryFunction(StringRef Name) const {
+  const CLibraryInfo &Info = getCLibraryInfo();
+  return CheckerContext::isCLibraryFunction(FD, Name, Info.BuiltinName,
+                                            Info.IsPredefinedLibFunction,
+                                            Info.IsCLibraryDecl);
+}
+
+bool ento::CallDescription::matches(const CallEvent &Call) const {
+  return matchesImpl(Callee(Call));
+}
+
+bool ento::CallDescription::matchesAsWritten(const CallExpr &CE) const {
+  return matchesImpl(Callee(CE));
 }
 
 bool ento::CallDescription::matchNameOnly(const NamedDecl *ND) const {
@@ -139,24 +170,25 @@ bool ento::CallDescription::matchQualifiedNameParts(const Decl *D) const {
   return QualifierPartsIt == QualifierPartsEndIt;
 }
 
-bool ento::CallDescription::matchesImpl(const FunctionDecl *FD, size_t ArgCount,
-                                        size_t ParamCount) const {
+bool ento::CallDescription::matchesImpl(const Callee &C) const {
+  const FunctionDecl *FD = C.FD;
   if (!FD)
     return false;
 
-  const bool isMethod = isa<CXXMethodDecl>(FD);
-
-  if (MatchAs == Mode::SimpleFunc && isMethod)
+  if (MatchAs == Mode::SimpleFunc && C.IsMethod)
     return false;
 
-  if (MatchAs == Mode::CXXMethod && !isMethod)
+  if (MatchAs == Mode::CXXMethod && !C.IsMethod)
     return false;
 
+  const size_t ArgCount = C.ArgCount;
+  const size_t ParamCount = C.ParamCount;
+
   if (MatchAs == Mode::CLibraryMaybeHardened) {
     // In addition to accepting FOO() with CLibrary rules, we also want to
     // accept calls to __FOO_chk() and __builtin___FOO_chk().
-    if (CheckerContext::isCLibraryFunction(FD) &&
-        CheckerContext::isHardenedVariantOf(FD, getFunctionName())) {
+    if (CheckerContext::isHardenedVariantOf(FD, getFunctionName()) &&
+        C.isCLibraryFunction(StringRef())) {
       // Check that the actual argument/parameter counts are greater or equal
       // to the required counts. (Setting a requirement to std::nullopt matches
       // anything, so in that case value_or ensures that the value is compared
@@ -171,7 +203,7 @@ bool ento::CallDescription::matchesImpl(const FunctionDecl *FD, size_t ArgCount,
     return false;
 
   if (MatchAs == Mode::CLibrary || MatchAs == Mode::CLibraryMaybeHardened)
-    return CheckerContext::isCLibraryFunction(FD, getFunctionName());
+    return C.isCLibraryFunction(getFunctionName());
 
   if (!matchNameOnly(FD))
     return false;
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index 2c99d45f10304e..3cc15910c92fb9 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -48,22 +48,43 @@ StringRef CheckerContext::getDeclDescription(const Decl *D) {
 
 bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
                                         StringRef Name) {
+  unsigned BId = FD->getBuiltinID();
+  return isCLibraryFunction(FD, Name, getBuiltinName(FD, BId),
+                            isPredefinedLibFunction(FD, BId),
+                            isCLibraryDecl(FD));
+}
+
+std::string CheckerContext::getBuiltinName(const FunctionDecl *FD,
+                                           unsigned BId) {
+  if (BId == 0)
+    return std::string();
+  return FD->getASTContext().BuiltinInfo.getName(BId);
+}
+
+bool CheckerContext::isPredefinedLibFunction(const FunctionDecl *FD,
+                                             unsigned BId) {
+  return BId != 0 &&
+         FD->getASTContext().BuiltinInfo.isPredefinedLibFunction(BId);
+}
+
+bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
+                                        StringRef BuiltinName,
+                                        bool IsPredefinedLibFunction,
+                                        bool IsCLibraryDecl) {
   // To avoid false positives (Ex: finding user defined functions with
   // similar names), only perform fuzzy name matching when it's a builtin.
   // Using a string compare is slow, we might want to switch on BuiltinID here.
-  unsigned BId = FD->getBuiltinID();
-  if (BId != 0) {
+  if (!BuiltinName.empty()) {
     if (Name.empty())
       return true;
-    const Builtin::Context &BuiltinInfo = FD->getASTContext().BuiltinInfo;
-    std::string BName = BuiltinInfo.getName(BId);
+    StringRef BName = BuiltinName;
     // Accept exact match.
     if (BName == Name)
       return true;
     // Only perform fuzzy name matching for non-library builtins (e.g.
     // __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
     // library functions (e.g. objc_read_weak vs read, getlogin_r vs getlogin).
-    if (!BuiltinInfo.isPredefinedLibFunction(BId)) {
+    if (!IsPredefinedLibFunction) {
       size_t start = BName.find(Name);
       if (start != StringRef::npos) {
         //    v-- match starts here
@@ -86,6 +107,23 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
     }
   }
 
+  if (!IsCLibraryDecl)
+    return false;
+
+  if (Name.empty())
+    return true;
+
+  StringRef FName = FD->getName();
+  if (FName == Name)
+    return true;
+
+  if (FName.starts_with("__inline") && FName.contains(Name))
+    return true;
+
+  return false;
+}
+
+bool CheckerContext::isCLibraryDecl(const FunctionDecl *FD) {
   const IdentifierInfo *II = FD->getIdentifier();
   // If this is a special C++ name without IdentifierInfo, it can't be a
   // C library function.
@@ -102,20 +140,7 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
   // If this function is not externally visible, it is not a C library function.
   // Note that we make an exception for inline functions, which may be
   // declared in header files without external linkage.
-  if (!FD->isInlined() && !FD->isExternallyVisible())
-    return false;
-
-  if (Name.empty())
-    return true;
-
-  StringRef FName = II->getName();
-  if (FName == Name)
-    return true;
-
-  if (FName.starts_with("__inline") && FName.contains(Name))
-    return true;
-
-  return false;
+  return FD->isInlined() || FD->isExternallyVisible();
 }
 
 bool CheckerContext::isHardenedVariantOf(const FunctionDecl *FD,



More information about the cfe-commits mailing list