https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118340

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppa...@gcc.gnu.org>:

https://gcc.gnu.org/g:e71c0157478e49188cd754693dcc2059d63573e9

commit r16-1187-ge71c0157478e49188cd754693dcc2059d63573e9
Author: Patrick Palka <ppa...@redhat.com>
Date:   Thu Jun 5 11:06:04 2025 -0400

    c++: quadratic constexpr folding of arith expr [PR118340]

    Here the PR's testcase demonstrates that the cp_fully_fold calls in
    cp_build_binary_op (for diagnosing arithmetic overflow) lead to
    quadratic behavior when building up a large arithmetic constant
    expression.  The problem is ultimately that maybe_constant_value's
    caching doesn't reuse intermediate values, unlike cp_fold.  (And
    unfortunately we can't leverage the cp_fold cache in this call site
    because here we want to evaluate constexpr calls even in -O0, which
    cp_fold avoids.)

    This patch fixes this by making maybe_constant_value look up each
    operand of the given expression to see if we've previously reduced it,
    and if so, rebuild the expression using the (presumably) reduced
    operands and evaluate that.  After this patch each version of the
    testcase from the PR compiles in ~0.1s on my machine.

            PR c++/118340

    gcc/cp/ChangeLog:

            * constexpr.cc (maybe_constant_value): First try looking up each
            operand in the cv_cache and reusing the result.

    Reviewed-by: Jason Merrill <ja...@redhat.com>

Reply via email to