[gcc r17-3305] c++: INIT_EXPR trial constexpr folding

Patrick Palka via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:d5634bbbaaa5c0a23988703d0d62f66c6df561bd

commit r17-3305-gd5634bbbaaa5c0a23988703d0d62f66c6df561bd
Author: Patrick Palka <[email protected]>
Date:   Fri Aug 14 15:53:23 2026 -0400

    c++: INIT_EXPR trial constexpr folding
    
    This patch adds trial constexpr folding of INIT_EXPR that mirrors the
    existing TARGET_EXPR folding added by r15-6052.  Since local copy-init
    is represented as INIT_EXPR, this addresses the xfails in
    is_constant_evaluated3{a,b}.C.
    
    gcc/cp/ChangeLog:
    
            * cp-gimplify.cc (cp_fold_r) <case INIT_EXPR>: Add trial
            maybe_constant_init logic like for TARGET_EXPR.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/opt/is_constant_evaluated3a.C: Remove xfail.
            * g++.dg/opt/is_constant_evaluated3b.C: Likewise.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/cp-gimplify.cc                              | 12 ++++++++++++
 gcc/testsuite/g++.dg/opt/is_constant_evaluated3a.C |  2 +-
 gcc/testsuite/g++.dg/opt/is_constant_evaluated3b.C |  2 +-
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index 23a1b7ea2ccd..9dec376bec43 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -1735,6 +1735,18 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
 	 here rather than in cp_genericize to avoid problems with the invisible
 	 reference transition.  */
     case INIT_EXPR:
+      if (!flag_no_inline
+	  && (data->flags & ff_genericize))
+	{
+	  tree to = TREE_OPERAND (*stmt_p, 0);
+	  tree &from = TREE_OPERAND (*stmt_p, 1);
+	  tree folded = maybe_constant_init (from, to,
+					     (data->flags & ff_mce_false
+					      ? mce_false : mce_unknown));
+	  if (folded != from && TREE_CONSTANT (folded))
+	    from = folded;
+	}
+
       if (data->flags & ff_genericize)
 	cp_genericize_init_expr (stmt_p);
       break;
diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3a.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3a.C
index 4ec3ecdf7b5d..e5c8b7f57895 100644
--- a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3a.C
+++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3a.C
@@ -17,7 +17,7 @@ constexpr void f() {
   A a5{};
 }
 
-// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
+// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" } }
diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3b.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3b.C
index ac6d21d632e1..ff708e23832b 100644
--- a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3b.C
+++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3b.C
@@ -17,7 +17,7 @@ auto f = [] {
   A a5{};
 };
 
-// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } }
+// { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" } }
 // { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" } }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.