From 7f945bf89ed3d108340cee56e0318a2c43a1ae98 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Wed, 10 Sep 2025 11:45:08 +0100 Subject: [PATCH] Optimize constant folding in wide expression expansion (#6381) Do not apply V3Const in V3Expand after a function if nothing was expanded in it. Also fix statistics counter while at it. Inspired by #6379, follow up from #6111 --- src/V3Expand.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/V3Expand.cpp b/src/V3Expand.cpp index fe522db32..374ecfdc9 100644 --- a/src/V3Expand.cpp +++ b/src/V3Expand.cpp @@ -95,8 +95,8 @@ class ExpandVisitor final : public VNVisitor { bool doExpandWide(AstNode* nodep) { if (isImpure(nodep)) return false; - ++m_statWides; if (nodep->widthWords() <= v3Global.opt.expandLimit()) { + ++m_statWides; m_statWideWords += nodep->widthWords(); return true; } else { @@ -393,10 +393,11 @@ class ExpandVisitor final : public VNVisitor { VL_RESTORER(m_nTmps); m_funcp = nodep; m_nTmps = 0; + const VDouble0 statWidesBefore = m_statWides; iterateChildren(nodep); - // Constant fold here, as Ast size can likely be reduced - if (v3Global.opt.fConstEager()) { + // Constant fold here if anything was expanded, as Ast size can likely be reduced + if (v3Global.opt.fConstEager() && m_statWides != statWidesBefore) { AstNode* const editedp = V3Const::constifyEditCpp(nodep); UASSERT_OBJ(editedp == nodep, editedp, "Should not have replaced CFunc"); }