[clang] [clang][bytecode] Optimize classify() further (PR #140735)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 08:18:42 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/140735
>From a1979e88d05ed198700b90c3b5530d451cde9c4c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 20 May 2025 12:35:30 +0200
Subject: [PATCH] [clang][bytecode] Optimize classify() further
Try to do as few checks as possible. Check for builtin types only once,
then look at the BuiltinType Kind. For integers, we cache the int and
long size, since those are used a lot and the ASTContext::getIntWidth()
call is costly.
---
clang/lib/AST/ByteCode/Context.cpp | 128 +++++++++++++++++++----------
clang/lib/AST/ByteCode/Context.h | 3 +
2 files changed, 87 insertions(+), 44 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index c70a5259b77e2..6b8f4e31e4ff9 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -21,7 +21,12 @@
using namespace clang;
using namespace clang::interp;
-Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
+Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {
+ this->IntWidth = Ctx.getTargetInfo().getIntWidth();
+ this->LongWidth = Ctx.getTargetInfo().getLongWidth();
+ assert(Ctx.getTargetInfo().getCharWidth() == 8 &&
+ "We're assuming 8 bit chars");
+}
Context::~Context() {}
@@ -216,55 +221,91 @@ bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
-std::optional<PrimType> Context::classify(QualType T) const {
- if (T->isBooleanType())
- return PT_Bool;
-
- if (T->isSignedIntegerOrEnumerationType()) {
- switch (Ctx.getIntWidth(T)) {
- case 64:
- return PT_Sint64;
- case 32:
- return PT_Sint32;
- case 16:
- return PT_Sint16;
- case 8:
- return PT_Sint8;
- default:
- return PT_IntAPS;
- }
+static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
+ switch (BitWidth) {
+ case 64:
+ return PT_Sint64;
+ case 32:
+ return PT_Sint32;
+ case 16:
+ return PT_Sint16;
+ case 8:
+ return PT_Sint8;
+ default:
+ return PT_IntAPS;
}
+ llvm_unreachable("Unhandled BitWidth");
+}
+
+static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
+ switch (BitWidth) {
+ case 64:
+ return PT_Uint64;
+ case 32:
+ return PT_Uint32;
+ case 16:
+ return PT_Uint16;
+ case 8:
+ return PT_Uint8;
+ default:
+ return PT_IntAP;
+ }
+ llvm_unreachable("Unhandled BitWidth");
+}
+
+std::optional<PrimType> Context::classify(QualType T) const {
- if (T->isUnsignedIntegerOrEnumerationType()) {
- switch (Ctx.getIntWidth(T)) {
- case 64:
- return PT_Uint64;
- case 32:
- return PT_Uint32;
- case 16:
- return PT_Uint16;
- case 8:
- return PT_Uint8;
- case 1:
- // Might happen for enum types.
+ if (const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType())) {
+ auto Kind = BT->getKind();
+ if (Kind == BuiltinType::Bool)
return PT_Bool;
- default:
- return PT_IntAP;
- }
+ if (Kind == BuiltinType::NullPtr)
+ return PT_Ptr;
+ if (Kind == BuiltinType::BoundMember)
+ return PT_MemberPtr;
+
+ // Just trying to avoid the ASTContext::getIntWidth call below.
+ if (Kind == BuiltinType::Int)
+ return integralTypeToPrimTypeS(this->IntWidth);
+ if (Kind == BuiltinType::UInt)
+ return integralTypeToPrimTypeU(this->IntWidth);
+ if (Kind == BuiltinType::Long)
+ return integralTypeToPrimTypeS(this->LongWidth);
+ if (Kind == BuiltinType::ULong)
+ return integralTypeToPrimTypeU(this->LongWidth);
+ if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S)
+ return integralTypeToPrimTypeS(8);
+ if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U ||
+ Kind == BuiltinType::Char8)
+ return integralTypeToPrimTypeU(8);
+
+ if (BT->isSignedInteger())
+ return integralTypeToPrimTypeS(Ctx.getIntWidth(T));
+ if (BT->isUnsignedInteger())
+ return integralTypeToPrimTypeU(Ctx.getIntWidth(T));
+
+ if (BT->isFloatingPoint())
+ return PT_Float;
}
- if (T->isNullPtrType())
+ if (T->isPointerOrReferenceType())
return PT_Ptr;
- if (T->isRealFloatingType())
- return PT_Float;
+ if (T->isMemberPointerType())
+ return PT_MemberPtr;
- if (T->isFunctionPointerType() || T->isFunctionReferenceType() ||
- T->isFunctionType() || T->isBlockPointerType())
- return PT_Ptr;
+ if (const auto *BT = T->getAs<BitIntType>()) {
+ if (BT->isSigned())
+ return integralTypeToPrimTypeS(BT->getNumBits());
+ return integralTypeToPrimTypeU(BT->getNumBits());
+ }
- if (T->isPointerOrReferenceType() || T->isObjCObjectPointerType())
- return PT_Ptr;
+ if (const auto *ET = T->getAs<EnumType>()) {
+ const auto *D = ET->getDecl();
+ if (!D->isComplete())
+ return std::nullopt;
+ return classify(D->getIntegerType());
+ }
if (const auto *AT = T->getAs<AtomicType>())
return classify(AT->getValueType());
@@ -272,9 +313,8 @@ std::optional<PrimType> Context::classify(QualType T) const {
if (const auto *DT = dyn_cast<DecltypeType>(T))
return classify(DT->getUnderlyingType());
- if (T->isSpecificBuiltinType(BuiltinType::BoundMember) ||
- T->isMemberPointerType())
- return PT_MemberPtr;
+ if (T->isObjCObjectPointerType() || T->isBlockPointerType())
+ return PT_Ptr;
if (T->isFixedPointType())
return PT_FixedPoint;
diff --git a/clang/lib/AST/ByteCode/Context.h b/clang/lib/AST/ByteCode/Context.h
index fc778f9796a79..9a604ce8ebbe9 100644
--- a/clang/lib/AST/ByteCode/Context.h
+++ b/clang/lib/AST/ByteCode/Context.h
@@ -137,6 +137,9 @@ class Context final {
std::unique_ptr<Program> P;
/// ID identifying an evaluation.
unsigned EvalID = 0;
+ /// Cached widths (in bits) of common types, for a faster classify().
+ unsigned IntWidth;
+ unsigned LongWidth;
};
} // namespace interp
More information about the cfe-commits
mailing list