[clang] [clang] Avoid evaluating the BitWidth expression over and over again (PR #66203)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 13 05:21:52 PDT 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/66203:

It's usually a ConstantExpr with a saved constant integer anyway, so we can just return that.

>From 7668b4e5eb09b05c50ef67b23671dc2d19e12046 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 13 Sep 2023 14:11:00 +0200
Subject: [PATCH] [clang] Avoid evaluating the BitWidth expression over and
 over again

It's usually a ConstantExpr with a saved constant integer anyway, so we
can just return that.
---
 clang/lib/AST/Decl.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 6109829cf20a678..0e964b7f1372227 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4471,6 +4471,14 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
 
 unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
   assert(isBitField() && "not a bitfield");
+
+  // Try to avoid evaluating the expression in the overwhelmingly
+  // common case that we have a ConstantExpr.
+  if (const auto *CE = dyn_cast<ConstantExpr>(getBitWidth())) {
+    assert(CE->hasAPValueResult());
+    return CE->getResultAsAPSInt().getZExtValue();
+  }
+
   return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
 }
 



More information about the cfe-commits mailing list