public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: more mce_false folding from cp_fully_fold_init [PR108243]
Date: Tue, 21 Feb 2023 14:10:36 -0500	[thread overview]
Message-ID: <20230221191036.1140927-1-ppalka@redhat.com> (raw)

We should also fold the overall initializer passed to cp_fully_fold_init
with mce_false, which enables folding of the copy-initialization of
'a1' in the below testcase (the initializer here is an AGGR_INIT_EXPR).

Unfortunately this doesn't help with direct- or default-initialization
because we don't call cp_fully_fold_init in that case, and even if we
did the initializer in that case is expressed as a bare CALL_EXPR
instead of an AGGR_INIT_EXPR, which cp_fully_fold_init can't really
fold.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

	PR c++/108243

gcc/cp/ChangeLog:

	* cp-gimplify.cc (cp_fully_fold): Add an internal overload that
	additionally takes and propagate an mce_value parameter, and
	define the existing public overload in terms of it.
	(cp_fully_fold_init): Pass mce_false to cp_fully_fold.

gcc/testsuite/ChangeLog:

	* g++.dg/opt/is_constant_evaluated3.C: New test.
---
 gcc/cp/cp-gimplify.cc                         | 14 +++++++----
 .../g++.dg/opt/is_constant_evaluated3.C       | 23 +++++++++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index 32fe53521cc..5d5c6efb856 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -2447,8 +2447,8 @@ cp_fold_rvalue (tree x)
 
 /* Perform folding on expression X.  */
 
-tree
-cp_fully_fold (tree x)
+static tree
+cp_fully_fold (tree x, mce_value manifestly_const_eval)
 {
   if (processing_template_decl)
     return x;
@@ -2456,7 +2456,7 @@ cp_fully_fold (tree x)
      have to call both.  */
   if (cxx_dialect >= cxx11)
     {
-      x = maybe_constant_value (x);
+      x = maybe_constant_value (x, /*decl=*/NULL_TREE, manifestly_const_eval);
       /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
 	 a TARGET_EXPR; undo that here.  */
       if (TREE_CODE (x) == TARGET_EXPR)
@@ -2469,6 +2469,12 @@ cp_fully_fold (tree x)
   return cp_fold_rvalue (x);
 }
 
+tree
+cp_fully_fold (tree x)
+{
+  return cp_fully_fold (x, mce_unknown);
+}
+
 /* Likewise, but also fold recursively, which cp_fully_fold doesn't perform
    in some cases.  */
 
@@ -2477,7 +2483,7 @@ cp_fully_fold_init (tree x)
 {
   if (processing_template_decl)
     return x;
-  x = cp_fully_fold (x);
+  x = cp_fully_fold (x, mce_false);
   cp_fold_data data (ff_mce_false);
   cp_walk_tree (&x, cp_fold_r, &data, NULL);
   return x;
diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C
new file mode 100644
index 00000000000..0a1e46e5638
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C
@@ -0,0 +1,23 @@
+// PR c++/108243
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-O -fdump-tree-original" }
+
+struct A {
+  constexpr A(int n) : n(n), m(__builtin_is_constant_evaluated()) { }
+  constexpr A() : A(42) { }
+  int n, m;
+};
+
+int main() {
+  A a1 = {42};
+  A a2{42};
+  A a3(42);
+  A a4;
+  A a5{};
+}
+
+// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" } }
+// { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
+// { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
+// { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
+// { dg-final { scan-tree-dump "a5 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
-- 
2.39.2.501.gd9d677b2d8


             reply	other threads:[~2023-02-21 19:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-21 19:10 Patrick Palka [this message]
2023-03-02 16:36 ` Jason Merrill

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=20230221191036.1140927-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).