public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>,
	gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: quadratic constexpr behavior for left-assoc logical exprs [PR102780]
Date: Tue, 26 Oct 2021 15:29:02 -0400	[thread overview]
Message-ID: <CADzB+2mv5jJ008_E4nb4mHacOJ5D3jSMTW+59CQWZ1Xqcs-Bjw@mail.gmail.com> (raw)
In-Reply-To: <20211026184544.GA304296@tucnak>

On Tue, Oct 26, 2021 at 2:46 PM Jakub Jelinek via Gcc-patches <
gcc-patches@gcc.gnu.org> wrote:

> On Tue, Oct 26, 2021 at 01:44:18PM -0400, Patrick Palka via Gcc-patches
> wrote:
> > In the testcase below the two left fold expressions each expand into a
> > logical expression with 1024 terms, for which potential_const_expr_1
> > takes more than a minute to return true.  This is because p_c_e_1
> > performs trial evaluation of the first operand of a &&/|| in order to
> > determine whether to consider its second operand.  And because the
> > expanded expression is left-associated, this trial evaluation causes
> > p_c_e_1 to be quadratic in the number of terms of the expression.
> >
> > This patch fixes this quadratic behavior by making p_c_e_1 recurse only
> > into the first operand of a &&/|| when checking for potentiality.  This
> > means we'll consider more non-constant expressions as potentially
> > constant compared to the trial evaluation approach, but that should be
> > harmless as later constexpr evaluation of the expression will deem it
> > non-constant anyway.
>

Fair, especially now that it seems that C++23 is removing the diagnostic
for a constexpr function that can't produce a constant expression.  And as
more functions become constexpr, the trial evaluation gets more expensive
to get to the point of failure.  I wonder if we want to stop calling
cxx_eval_outermost_constant_expression from pot_c_e_1 entirely, only check
whether the operand is already constant.

For very large && or || expressions (but not mixed or with ! etc.) another
> possibility would be to check if the lhs or rhs have the same code and if
> so, linearize the trees and recurse only on the leafs (trees with other
> TREE_CODE) and stop when first expression isn't equal to tmp.
>

This would be good if we wanted to keep the evaluation.

Jason

  reply	other threads:[~2021-10-26 19:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-26 17:44 Patrick Palka
2021-10-26 18:45 ` Jakub Jelinek
2021-10-26 19:29   ` Jason Merrill [this message]
2021-10-26 20:01     ` Jakub Jelinek
2021-10-26 21:07     ` Patrick Palka
2021-10-26 21:11       ` Jakub Jelinek
2021-10-27 18:54         ` Patrick Palka
2021-10-27 20:37           ` Jason Merrill
2021-10-27 21:10             ` Patrick Palka
2021-10-27 21:51               ` Jason Merrill
2021-10-28 16:40                 ` Patrick Palka
2021-10-28 17:38                   ` Jakub Jelinek
2021-10-28 19:35                     ` Patrick Palka
2021-10-29 11:59                       ` Jakub Jelinek
2021-11-01 18:45                         ` Patrick Palka
2021-11-01 19:08                           ` Patrick Palka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADzB+2mv5jJ008_E4nb4mHacOJ5D3jSMTW+59CQWZ1Xqcs-Bjw@mail.gmail.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).