[clang] 79ef948 - [analyzer][NFC] Remove padding from MemRegion and SymExpr (#246313)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 03:59:16 PST 2026
Author: Gábor Horváth
Date: 2026-11-26T11:59:10Z
New Revision: 79ef948a89e1beace5b35652c98ff55b2f2f2632
URL: https://github.com/llvm/llvm-project/commit/79ef948a89e1beace5b35652c98ff55b2f2f2632
DIFF: https://github.com/llvm/llvm-project/commit/79ef948a89e1beace5b35652c98ff55b2f2f2632.diff
LOG: [analyzer][NFC] Remove padding from MemRegion and SymExpr (#246313)
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
---------
Co-authored-by: Gabor Horvath <gaborh at apple.com>
Added:
Modified:
clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
clang/lib/StaticAnalyzer/Core/MemRegion.cpp
clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
Removed:
################################################################################
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..3786b88325ab67 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
diff erentiate 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,15 @@ class SymExpr : public llvm::FoldingSetNode {
mutable unsigned Complexity = 0;
+private:
+ // Declared last so that subclasses can put small fields into 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 << '(';
More information about the cfe-commits
mailing list