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

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 13 05:23:02 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang
            
<details>
<summary>Changes</summary>
It's usually a ConstantExpr with a saved constant integer anyway, so we can just return that.
--
Full diff: https://github.com/llvm/llvm-project/pull/66203.diff

1 Files Affected:

- (modified) clang/lib/AST/Decl.cpp (+8) 


<pre>
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();
 }
 
</pre>
</details>


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


More information about the cfe-commits mailing list