[clang] [analyzer][NFC] Mark the leaf CallEvent classes final and store the kind in the base class (PR #245674)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 08:50:55 PST 2026


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

Two small changes to the CallEvent hierarchy:

- Mark the leaf CallEvent classes final, so calls through them can be devirtualized.
- Store the kind in CallEvent and make getKind() non-virtual, as in other LLVM class hierarchies. Every isa/dyn_cast on a CallEvent used to be a virtual call and is now a load and a compare. The kind fits in existing padding, so the classes don't grow.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr), together these reduce the instructions of the static analyzer by 0.5-0.7%. 

>From 077b2c0fd54a7eeb9ded0094a64801c217c1432d Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Tue, 24 Nov 2026 15:03:26 +0000
Subject: [PATCH 1/2] [analyzer] Mark the leaf CallEvent classes final

None of BlockCall, CXXStaticOperatorCall, CXXMemberCall,
CXXMemberOperatorCall, CXXDestructorCall, CXXConstructorCall,
CXXInheritedConstructorCall, CXXAllocatorCall, CXXDeallocatorCall and
ObjCMethodCall have subclasses. Marking them final lets the compiler
devirtualize calls made through a pointer or reference to these types,
e.g. getOriginExpr() inside CXXAllocatorCall::getNumArgs(), or the
CallEvent accessors used by the ObjCMethodCall-typed checker callbacks.

In a release build this removes 74 of the 2021 indirect calls (blr on
arm64) in libclangStaticAnalyzerCore and 150 of the 6346 in
libclangStaticAnalyzerCheckers, and shrinks their __text by 0.06% and
0.03%.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this
reduces the instructions of the static analyzer by 0.10-0.37%, which is
small but consistent. Diagnostics are unchanged.

Assisted-by: Claude Opus 5.5
---
 .../Core/PathSensitive/CallEvent.h            | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
index f854c25ede8521..0b8c6ac3043540 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
@@ -578,7 +578,7 @@ class SimpleFunctionCall : public AnyFunctionCall {
 /// Represents a call to a block.
 ///
 /// Example: <tt>^{ statement-body }()</tt>
-class BlockCall : public CallEvent {
+class BlockCall final : public CallEvent {
   friend class CallEventManager;
 
 protected:
@@ -736,7 +736,7 @@ class CXXInstanceCall : public AnyFunctionCall {
 ///   S::operator()(1, 2);  // B
 ///   fptr(1, 2); // C
 /// \endcode
-class CXXStaticOperatorCall : public SimpleFunctionCall {
+class CXXStaticOperatorCall final : public SimpleFunctionCall {
   friend class CallEventManager;
 
 protected:
@@ -794,7 +794,7 @@ class CXXStaticOperatorCall : public SimpleFunctionCall {
 /// Represents a non-static C++ member function call.
 ///
 /// Example: \c obj.fun()
-class CXXMemberCall : public CXXInstanceCall {
+class CXXMemberCall final : public CXXInstanceCall {
   friend class CallEventManager;
 
 protected:
@@ -836,7 +836,7 @@ class CXXMemberCall : public CXXInstanceCall {
 /// implemented as a non-static member function.
 ///
 /// Example: <tt>iter + 1</tt>
-class CXXMemberOperatorCall : public CXXInstanceCall {
+class CXXMemberOperatorCall final : public CXXInstanceCall {
   friend class CallEventManager;
 
 protected:
@@ -896,7 +896,7 @@ class CXXMemberOperatorCall : public CXXInstanceCall {
 ///
 /// This can occur at the end of a scope (for automatic objects), at the end
 /// of a full-expression (for temporaries), or as part of a delete.
-class CXXDestructorCall : public CXXInstanceCall {
+class CXXDestructorCall final : public CXXInstanceCall {
   friend class CallEventManager;
 
 protected:
@@ -985,7 +985,7 @@ class AnyCXXConstructorCall : public AnyFunctionCall {
 /// Represents a call to a C++ constructor.
 ///
 /// Example: \c T(1)
-class CXXConstructorCall : public AnyCXXConstructorCall {
+class CXXConstructorCall final : public AnyCXXConstructorCall {
   friend class CallEventManager;
 
 protected:
@@ -1054,7 +1054,7 @@ class CXXConstructorCall : public AnyCXXConstructorCall {
 /// B b = X{};
 ///
 /// ... b.b is initialized to true.
-class CXXInheritedConstructorCall : public AnyCXXConstructorCall {
+class CXXInheritedConstructorCall final : public AnyCXXConstructorCall {
   friend class CallEventManager;
 
 protected:
@@ -1117,7 +1117,7 @@ class CXXInheritedConstructorCall : public AnyCXXConstructorCall {
 /// Represents the memory allocation call in a C++ new-expression.
 ///
 /// This is a call to "operator new".
-class CXXAllocatorCall : public AnyFunctionCall {
+class CXXAllocatorCall final : public AnyFunctionCall {
   friend class CallEventManager;
 
 protected:
@@ -1200,7 +1200,7 @@ class CXXAllocatorCall : public AnyFunctionCall {
 // http://lists.llvm.org/pipermail/cfe-dev/2020-April/065080.html
 // clang/test/Analysis/cxx-dynamic-memory-analysis-order.cpp
 // clang/unittests/StaticAnalyzer/CallEventTest.cpp
-class CXXDeallocatorCall : public AnyFunctionCall {
+class CXXDeallocatorCall final : public AnyFunctionCall {
   friend class CallEventManager;
 
 protected:
@@ -1246,7 +1246,7 @@ enum ObjCMessageKind { OCM_PropertyAccess, OCM_Subscript, OCM_Message };
 /// Represents any expression that calls an Objective-C method.
 ///
 /// This includes all of the kinds listed in ObjCMessageKind.
-class ObjCMethodCall : public CallEvent {
+class ObjCMethodCall final : public CallEvent {
   friend class CallEventManager;
 
   const PseudoObjectExpr *getContainingPseudoObjectExpr() const;

>From 63b8e118bc1cdc1b81fffa6289ffb3c30b6f1ebe Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Tue, 24 Nov 2026 15:31:40 +0000
Subject: [PATCH 2/2] [analyzer] Store the CallEvent kind in the base class

CallEvent::getKind() was a pure virtual function, so every isa/dyn_cast
on a CallEvent (and range checks like isa<CXXInstanceCall>, which call
getKind() twice) went through the vtable. These casts are frequent in
checker callbacks and in CallDescription::matches().

Use the usual LLVM-style RTTI instead: store the kind in a const member
of CallEvent, initialized through the constructors (the abstract
intermediate classes take the kind as a parameter, the leaf classes
pass their own), and make getKind() a non-virtual inline accessor. The
copy constructor used by cloneTo() copies the kind. The new member sits
in the padding after Foreign, so sizeof(CallEvent) and all subclasses
stay 72 bytes on arm64.

In a release build, on top of the previous commit, this removes another
15 indirect calls (blr on arm64) in libclangStaticAnalyzerCore and 117
in libclangStaticAnalyzerCheckers, and shrinks the __text of the latter
by 0.06%.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this
reduces the instructions of the static analyzer by 0.07-0.33% relative
to the previous commit, and by 0.50-0.74% together with it. Diagnostics
are unchanged.

Assisted-by: Claude Opus 5.5
---
 .../Core/PathSensitive/CallEvent.h            | 76 +++++++++----------
 1 file changed, 35 insertions(+), 41 deletions(-)

diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
index 0b8c6ac3043540..151b0afe6b4255 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
@@ -159,6 +159,7 @@ class CallEvent {
   llvm::PointerUnion<const Expr *, const Decl *> Origin;
   CFGBlock::ConstCFGElementRef ElemRef = {nullptr, 0};
   mutable std::optional<bool> Foreign; // Set by CTU analysis.
+  const Kind K;
 
 protected:
   // This is user data for subclasses.
@@ -180,18 +181,18 @@ class CallEvent {
 protected:
   friend class CallEventManager;
 
-  CallEvent(const Expr *E, ProgramStateRef state, const StackFrame *SF,
+  CallEvent(Kind K, const Expr *E, ProgramStateRef state, const StackFrame *SF,
             CFGBlock::ConstCFGElementRef ElemRef)
-      : State(std::move(state)), SF(SF), Origin(E), ElemRef(ElemRef) {}
+      : State(std::move(state)), SF(SF), Origin(E), ElemRef(ElemRef), K(K) {}
 
-  CallEvent(const Decl *D, ProgramStateRef state, const StackFrame *SF,
+  CallEvent(Kind K, const Decl *D, ProgramStateRef state, const StackFrame *SF,
             CFGBlock::ConstCFGElementRef ElemRef)
-      : State(std::move(state)), SF(SF), Origin(D), ElemRef(ElemRef) {}
+      : State(std::move(state)), SF(SF), Origin(D), ElemRef(ElemRef), K(K) {}
 
   // DO NOT MAKE PUBLIC
   CallEvent(const CallEvent &Original)
       : State(Original.State), SF(Original.SF), Origin(Original.Origin),
-        ElemRef(Original.ElemRef), Data(Original.Data),
+        ElemRef(Original.ElemRef), K(Original.K), Data(Original.Data),
         Location(Original.Location) {}
 
   /// Copies this CallEvent, with vtable intact, into a new block of memory.
@@ -225,7 +226,7 @@ class CallEvent {
   virtual ~CallEvent() = default;
 
   /// Returns the kind of call this is.
-  virtual Kind getKind() const = 0;
+  Kind getKind() const { return K; }
   virtual StringRef getKindAsString() const = 0;
 
   /// Returns the declaration of the function or method that will be
@@ -506,12 +507,12 @@ class CallEvent {
 /// FunctionDecl.
 class AnyFunctionCall : public CallEvent {
 protected:
-  AnyFunctionCall(const Expr *E, ProgramStateRef St, const StackFrame *SF,
-                  CFGBlock::ConstCFGElementRef ElemRef)
-      : CallEvent(E, St, SF, ElemRef) {}
-  AnyFunctionCall(const Decl *D, ProgramStateRef St, const StackFrame *SF,
-                  CFGBlock::ConstCFGElementRef ElemRef)
-      : CallEvent(D, St, SF, ElemRef) {}
+  AnyFunctionCall(Kind K, const Expr *E, ProgramStateRef St,
+                  const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
+      : CallEvent(K, E, St, SF, ElemRef) {}
+  AnyFunctionCall(Kind K, const Decl *D, ProgramStateRef St,
+                  const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
+      : CallEvent(K, D, St, SF, ElemRef) {}
   AnyFunctionCall(const AnyFunctionCall &Other) = default;
 
 public:
@@ -545,7 +546,10 @@ class SimpleFunctionCall : public AnyFunctionCall {
 protected:
   SimpleFunctionCall(const CallExpr *CE, ProgramStateRef St,
                      const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(CE, St, SF, ElemRef) {}
+      : SimpleFunctionCall(CE_Function, CE, St, SF, ElemRef) {}
+  SimpleFunctionCall(Kind K, const CallExpr *CE, ProgramStateRef St,
+                     const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
+      : AnyFunctionCall(K, CE, St, SF, ElemRef) {}
   SimpleFunctionCall(const SimpleFunctionCall &Other) = default;
 
   void cloneTo(void *Dest) const override {
@@ -567,7 +571,6 @@ class SimpleFunctionCall : public AnyFunctionCall {
     return getOriginExpr()->getArg(Index);
   }
 
-  Kind getKind() const override { return CE_Function; }
   StringRef getKindAsString() const override { return "SimpleFunctionCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -584,7 +587,7 @@ class BlockCall final : public CallEvent {
 protected:
   BlockCall(const CallExpr *CE, ProgramStateRef St, const StackFrame *SF,
             CFGBlock::ConstCFGElementRef ElemRef)
-      : CallEvent(CE, St, SF, ElemRef) {}
+      : CallEvent(CE_Block, CE, St, SF, ElemRef) {}
   BlockCall(const BlockCall &Other) = default;
 
   void cloneTo(void *Dest) const override { new (Dest) BlockCall(*this); }
@@ -673,7 +676,6 @@ class BlockCall final : public CallEvent {
 
   ArrayRef<ParmVarDecl *> parameters() const override;
 
-  Kind getKind() const override { return CE_Block; }
   StringRef getKindAsString() const override { return "BlockCall"; }
 
   static bool classof(const CallEvent *CA) { return CA->getKind() == CE_Block; }
@@ -683,12 +685,12 @@ class BlockCall final : public CallEvent {
 /// it is written.
 class CXXInstanceCall : public AnyFunctionCall {
 protected:
-  CXXInstanceCall(const CallExpr *CE, ProgramStateRef St, const StackFrame *SF,
-                  CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(CE, St, SF, ElemRef) {}
-  CXXInstanceCall(const FunctionDecl *D, ProgramStateRef St,
+  CXXInstanceCall(Kind K, const CallExpr *CE, ProgramStateRef St,
+                  const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
+      : AnyFunctionCall(K, CE, St, SF, ElemRef) {}
+  CXXInstanceCall(Kind K, const FunctionDecl *D, ProgramStateRef St,
                   const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(D, St, SF, ElemRef) {}
+      : AnyFunctionCall(K, D, St, SF, ElemRef) {}
   CXXInstanceCall(const CXXInstanceCall &Other) = default;
 
   void getExtraInvalidatedValues(
@@ -743,7 +745,7 @@ class CXXStaticOperatorCall final : public SimpleFunctionCall {
   CXXStaticOperatorCall(const CXXOperatorCallExpr *CE, ProgramStateRef St,
                         const StackFrame *SF,
                         CFGBlock::ConstCFGElementRef ElemRef)
-      : SimpleFunctionCall(CE, St, SF, ElemRef) {}
+      : SimpleFunctionCall(CE_CXXStaticOperator, CE, St, SF, ElemRef) {}
   CXXStaticOperatorCall(const CXXStaticOperatorCall &Other) = default;
 
   void cloneTo(void *Dest) const override {
@@ -783,7 +785,6 @@ class CXXStaticOperatorCall final : public SimpleFunctionCall {
     return getOriginExpr()->getOperator();
   }
 
-  Kind getKind() const override { return CE_CXXStaticOperator; }
   StringRef getKindAsString() const override { return "CXXStaticOperatorCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -800,7 +801,7 @@ class CXXMemberCall final : public CXXInstanceCall {
 protected:
   CXXMemberCall(const CXXMemberCallExpr *CE, ProgramStateRef St,
                 const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : CXXInstanceCall(CE, St, SF, ElemRef) {}
+      : CXXInstanceCall(CE_CXXMember, CE, St, SF, ElemRef) {}
   CXXMemberCall(const CXXMemberCall &Other) = default;
 
   void cloneTo(void *Dest) const override { new (Dest) CXXMemberCall(*this); }
@@ -824,7 +825,6 @@ class CXXMemberCall final : public CXXInstanceCall {
 
   RuntimeDefinition getRuntimeDefinition() const override;
 
-  Kind getKind() const override { return CE_CXXMember; }
   StringRef getKindAsString() const override { return "CXXMemberCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -843,7 +843,7 @@ class CXXMemberOperatorCall final : public CXXInstanceCall {
   CXXMemberOperatorCall(const CXXOperatorCallExpr *CE, ProgramStateRef St,
                         const StackFrame *SF,
                         CFGBlock::ConstCFGElementRef ElemRef)
-      : CXXInstanceCall(CE, St, SF, ElemRef) {}
+      : CXXInstanceCall(CE_CXXMemberOperator, CE, St, SF, ElemRef) {}
   CXXMemberOperatorCall(const CXXMemberOperatorCall &Other) = default;
 
   void cloneTo(void *Dest) const override {
@@ -865,7 +865,6 @@ class CXXMemberOperatorCall final : public CXXInstanceCall {
 
   const Expr *getCXXThisExpr() const override;
 
-  Kind getKind() const override { return CE_CXXMemberOperator; }
   StringRef getKindAsString() const override { return "CXXMemberOperatorCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -919,7 +918,7 @@ class CXXDestructorCall final : public CXXInstanceCall {
                     const MemRegion *Target, bool IsBaseDestructor,
                     ProgramStateRef St, const StackFrame *SF,
                     CFGBlock::ConstCFGElementRef ElemRef)
-      : CXXInstanceCall(DD, St, SF, ElemRef) {
+      : CXXInstanceCall(CE_CXXDestructor, DD, St, SF, ElemRef) {
     Data = DtorDataTy(Target, IsBaseDestructor).getOpaqueValue();
     Location = Trigger->getEndLoc();
   }
@@ -944,7 +943,6 @@ class CXXDestructorCall final : public CXXInstanceCall {
     return DtorDataTy::getFromOpaqueValue(Data).getInt();
   }
 
-  Kind getKind() const override { return CE_CXXDestructor; }
   StringRef getKindAsString() const override { return "CXXDestructorCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -956,10 +954,10 @@ class CXXDestructorCall final : public CXXInstanceCall {
 /// and inherited constructors.
 class AnyCXXConstructorCall : public AnyFunctionCall {
 protected:
-  AnyCXXConstructorCall(const Expr *E, const MemRegion *Target,
+  AnyCXXConstructorCall(Kind K, const Expr *E, const MemRegion *Target,
                         ProgramStateRef St, const StackFrame *SF,
                         CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(E, St, SF, ElemRef) {
+      : AnyFunctionCall(K, E, St, SF, ElemRef) {
     assert(E && (isa<CXXConstructExpr>(E) || isa<CXXInheritedCtorInitExpr>(E)));
     // Target may be null when the region is unknown.
     Data = Target;
@@ -1003,7 +1001,7 @@ class CXXConstructorCall final : public AnyCXXConstructorCall {
   CXXConstructorCall(const CXXConstructExpr *CE, const MemRegion *Target,
                      ProgramStateRef St, const StackFrame *SF,
                      CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyCXXConstructorCall(CE, Target, St, SF, ElemRef) {}
+      : AnyCXXConstructorCall(CE_CXXConstructor, CE, Target, St, SF, ElemRef) {}
 
   CXXConstructorCall(const CXXConstructorCall &Other) = default;
 
@@ -1026,7 +1024,6 @@ class CXXConstructorCall final : public AnyCXXConstructorCall {
     return getOriginExpr()->getArg(Index);
   }
 
-  Kind getKind() const override { return CE_CXXConstructor; }
   StringRef getKindAsString() const override { return "CXXConstructorCall"; }
 
   static bool classof(const CallEvent *CA) {
@@ -1062,7 +1059,8 @@ class CXXInheritedConstructorCall final : public AnyCXXConstructorCall {
                               const MemRegion *Target, ProgramStateRef St,
                               const StackFrame *SF,
                               CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyCXXConstructorCall(CE, Target, St, SF, ElemRef) {}
+      : AnyCXXConstructorCall(CE_CXXInheritedConstructor, CE, Target, St, SF,
+                              ElemRef) {}
 
   CXXInheritedConstructorCall(const CXXInheritedConstructorCall &Other) =
       default;
@@ -1104,7 +1102,6 @@ class CXXInheritedConstructorCall final : public AnyCXXConstructorCall {
                                getInheritingStackFrame()->getParent());
   }
 
-  Kind getKind() const override { return CE_CXXInheritedConstructor; }
   StringRef getKindAsString() const override {
     return "CXXInheritedConstructorCall";
   }
@@ -1123,7 +1120,7 @@ class CXXAllocatorCall final : public AnyFunctionCall {
 protected:
   CXXAllocatorCall(const CXXNewExpr *E, ProgramStateRef St,
                    const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(E, St, SF, ElemRef) {}
+      : AnyFunctionCall(CE_CXXAllocator, E, St, SF, ElemRef) {}
   CXXAllocatorCall(const CXXAllocatorCall &Other) = default;
 
   void cloneTo(void *Dest) const override {
@@ -1182,7 +1179,6 @@ class CXXAllocatorCall final : public AnyFunctionCall {
     return getOriginExpr()->getPlacementArg(Index);
   }
 
-  Kind getKind() const override { return CE_CXXAllocator; }
   StringRef getKindAsString() const override { return "CXXAllocatorCall"; }
 
   static bool classof(const CallEvent *CE) {
@@ -1206,7 +1202,7 @@ class CXXDeallocatorCall final : public AnyFunctionCall {
 protected:
   CXXDeallocatorCall(const CXXDeleteExpr *E, ProgramStateRef St,
                      const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : AnyFunctionCall(E, St, SF, ElemRef) {}
+      : AnyFunctionCall(CE_CXXDeallocator, E, St, SF, ElemRef) {}
   CXXDeallocatorCall(const CXXDeallocatorCall &Other) = default;
 
   void cloneTo(void *Dest) const override {
@@ -1229,7 +1225,6 @@ class CXXDeallocatorCall final : public AnyFunctionCall {
     return getOriginExpr()->getArgument();
   }
 
-  Kind getKind() const override { return CE_CXXDeallocator; }
   StringRef getKindAsString() const override { return "CXXDeallocatorCall"; }
 
   static bool classof(const CallEvent *CE) {
@@ -1254,7 +1249,7 @@ class ObjCMethodCall final : public CallEvent {
 protected:
   ObjCMethodCall(const ObjCMessageExpr *Msg, ProgramStateRef St,
                  const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
-      : CallEvent(Msg, St, SF, ElemRef) {
+      : CallEvent(CE_ObjCMessage, Msg, St, SF, ElemRef) {
     Data = nullptr;
   }
 
@@ -1343,7 +1338,6 @@ class ObjCMethodCall final : public CallEvent {
 
   ArrayRef<ParmVarDecl *> parameters() const override;
 
-  Kind getKind() const override { return CE_ObjCMessage; }
   StringRef getKindAsString() const override { return "ObjCMethodCall"; }
 
   static bool classof(const CallEvent *CA) {



More information about the cfe-commits mailing list