[clang] [analyzer][NFC] Remove padding from MemRegion and SymExpr (PR #246313)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 03:20:05 PST 2026


https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/246313

>From 8688af78e90625c3bce1a2cfd95ee38f537ac5a9 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Wed, 25 Nov 2026 23:46:04 +0000
Subject: [PATCH 1/2] [analyzer][NFC] Remove padding from MemRegion and SymExpr

Cache the region offset without std::optional, using a null region for
"not computed", and make the SymExpr kind 8 bits so that the opcode of
UnarySymExpr and BinarySymExpr fits into its tail padding. This shrinks
MemRegion and its subclasses by 8 bytes, and on Itanium ABI targets also
UnarySymExpr and BinarySymExpr.

Assisted-by: Claude Opus 5.5
---
 .../Core/PathSensitive/MemRegion.h            |  3 ++-
 .../Core/PathSensitive/SymExpr.h              | 11 +++++++---
 .../Core/PathSensitive/SymbolManager.h        | 20 +++++++++++++------
 clang/lib/StaticAnalyzer/Core/MemRegion.cpp   |  6 ++++--
 .../lib/StaticAnalyzer/Core/SymbolManager.cpp |  2 +-
 5 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
index 15f0afc94579ac..b811482c44fe15 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
@@ -106,7 +106,8 @@ class MemRegion : public llvm::FoldingSetNode {
 
 private:
   const Kind kind;
-  mutable std::optional<RegionOffset> cachedOffset;
+  /// Invalid (null region) until computed; a computed offset is always valid.
+  mutable RegionOffset cachedOffset;
 
 protected:
   MemRegion(Kind k) : kind(k) {}
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
index ee07814b48a3f6..4ae182381bd5c9 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
@@ -41,7 +41,6 @@ class SymExpr : public llvm::FoldingSetNode {
   };
 
 private:
-  Kind K;
   /// A unique identifier for this symbol.
   ///
   /// It is useful for SymbolData to easily differentiate multiple symbols, but
@@ -54,7 +53,7 @@ class SymExpr : public llvm::FoldingSetNode {
   const SymbolID Sym;
 
 protected:
-  SymExpr(Kind k, SymbolID Sym) : K(k), Sym(Sym) {}
+  SymExpr(Kind k, SymbolID Sym) : Sym(Sym), K(k) {}
 
   static bool isValidTypeForSymbol(QualType T) {
     // FIXME: Depending on whether we choose to deprecate structural symbols,
@@ -64,10 +63,16 @@ class SymExpr : public llvm::FoldingSetNode {
 
   mutable unsigned Complexity = 0;
 
+private:
+  // Declared last so that subclasses can put small fields into the tail
+  // padding.
+  LLVM_PREFERRED_TYPE(Kind)
+  unsigned K : 8;
+
 public:
   virtual ~SymExpr() = default;
 
-  Kind getKind() const { return K; }
+  Kind getKind() const { return static_cast<Kind>(K); }
 
   /// Get a unique identifier for this symbol.
   /// The ID is unique across all SymExprs in a SymbolManager.
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
index 01a980bdbc0f2c..91365e8ba4b61b 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
@@ -335,14 +335,16 @@ class SymbolCast final : public SymExpr {
 
 /// Represents a symbolic expression involving a unary operator.
 class UnarySymExpr final : public SymExpr {
+  // Declared first so that it can use the tail padding of SymExpr.
+  LLVM_PREFERRED_TYPE(UnaryOperatorKind)
+  unsigned Op : 8;
   const SymExpr *Operand;
-  UnaryOperator::Opcode Op;
   QualType T;
 
   friend class SymExprAllocator;
   UnarySymExpr(SymbolID Sym, const SymExpr *In, UnaryOperator::Opcode Op,
                QualType T)
-      : SymExpr(ClassKind, Sym), Operand(In), Op(Op), T(T) {
+      : SymExpr(ClassKind, Sym), Op(Op), Operand(In), T(T) {
     // Note, some unary operators are modeled as a binary operator. E.g. ++x is
     // modeled as x + 1.
     assert((Op == UO_Minus || Op == UO_Not) && "non-supported unary expression");
@@ -361,7 +363,9 @@ class UnarySymExpr final : public SymExpr {
   }
 
   const SymExpr *getOperand() const { return Operand; }
-  UnaryOperator::Opcode getOpcode() const { return Op; }
+  UnaryOperator::Opcode getOpcode() const {
+    return static_cast<UnaryOperator::Opcode>(Op);
+  }
   QualType getType() const override { return T; }
 
   void dumpToStream(raw_ostream &os) const override;
@@ -375,7 +379,7 @@ class UnarySymExpr final : public SymExpr {
   }
 
   void Profile(llvm::FoldingSetNodeID &ID) override {
-    Profile(ID, Operand, Op, T);
+    Profile(ID, Operand, getOpcode(), T);
   }
 
   // Implement isa<T> support.
@@ -386,7 +390,9 @@ class UnarySymExpr final : public SymExpr {
 
 /// Represents a symbolic expression involving a binary operator
 class BinarySymExpr : public SymExpr {
-  BinaryOperator::Opcode Op;
+  // Uses the tail padding of SymExpr.
+  LLVM_PREFERRED_TYPE(BinaryOperatorKind)
+  unsigned Op : 8;
   QualType T;
 
 protected:
@@ -404,7 +410,9 @@ class BinarySymExpr : public SymExpr {
   // generation of virtual functions.
   QualType getType() const override { return T; }
 
-  BinaryOperator::Opcode getOpcode() const { return Op; }
+  BinaryOperator::Opcode getOpcode() const {
+    return static_cast<BinaryOperator::Opcode>(Op);
+  }
 
   // Implement isa<T> support.
   static bool classof(const SymExpr *SE) { return classof(SE->getKind()); }
diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
index 944601d5083e4b..3417f842ccbbfd 100644
--- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
+++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
@@ -1756,9 +1756,11 @@ static RegionOffset calculateOffset(const MemRegion *R) {
 }
 
 RegionOffset MemRegion::getAsOffset() const {
-  if (!cachedOffset)
+  if (!cachedOffset.isValid()) {
     cachedOffset = calculateOffset(this);
-  return *cachedOffset;
+    assert(cachedOffset.isValid());
+  }
+  return cachedOffset;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
index b0a98d3ff918b2..d6fd178ccacb8f 100644
--- a/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
@@ -67,7 +67,7 @@ void SymbolCast::dumpToStream(raw_ostream &os) const {
 }
 
 void UnarySymExpr::dumpToStream(raw_ostream &os) const {
-  os << UnaryOperator::getOpcodeStr(Op);
+  os << UnaryOperator::getOpcodeStr(getOpcode());
   bool Binary = isa<BinarySymExpr>(Operand);
   if (Binary)
     os << '(';

>From 809aeea825b0ba9fec9a0d0308b92e65c6bbb433 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Thu, 26 Nov 2026 11:17:19 +0000
Subject: [PATCH 2/2] [analyzer][NFC] Address review comments

Assisted-by: Claude Opus 5.5
---
 .../include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h  | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
index 4ae182381bd5c9..3786b88325ab67 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
@@ -64,8 +64,7 @@ class SymExpr : public llvm::FoldingSetNode {
   mutable unsigned Complexity = 0;
 
 private:
-  // Declared last so that subclasses can put small fields into the tail
-  // padding.
+  // Declared last so that subclasses can put small fields into tail padding.
   LLVM_PREFERRED_TYPE(Kind)
   unsigned K : 8;
 



More information about the cfe-commits mailing list