From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 91952385828D; Thu, 2 Mar 2023 19:05:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91952385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677783911; bh=8OfcS30sgwd2C7bX7e+0EjU6WDis5WLvwg2fZIRXsUY=; h=From:To:Subject:Date:From; b=GZ0ezUG0cimKr9l0zU4tQb4FHy+kgn09zk/sMqzpzqxr6R1UdpTpkEt8YyRSmuKWa Pa3C4WM34UGpdayohGz3DEKCn7FbuZhS1A3MAWFz6heu3317U1XF0PdS4niEdp3ePS u3r37ijULlsxnRqS4iBBXC20ODvdaz58HBt5HRPM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6422] c++: more mce_false folding from cp_fully_fold_init [PR108243] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: cbaa1d9c218d9c0b5e34e510a462ff4e299a0f3f X-Git-Newrev: 5425159d176a7a92afc932cbb22d8822667099c4 Message-Id: <20230302190511.91952385828D@sourceware.org> Date: Thu, 2 Mar 2023 19:05:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5425159d176a7a92afc932cbb22d8822667099c4 commit r13-6422-g5425159d176a7a92afc932cbb22d8822667099c4 Author: Patrick Palka Date: Thu Mar 2 14:04:50 2023 -0500 c++: more mce_false folding from cp_fully_fold_init [PR108243] We should also fold the overall initializer passed to cp_fully_fold_init with mce_false, which allows 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. PR c++/108243 PR c++/97553 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. Diff: --- gcc/cp/cp-gimplify.cc | 19 ++++++++++++++----- gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 921b0e4b120..b995b4f81a5 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -2449,8 +2449,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; @@ -2458,7 +2458,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) @@ -2468,7 +2468,16 @@ cp_fully_fold (tree x) && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x)) x = TREE_OPERAND (x, 0); } - return cp_fold_rvalue (x); + fold_flags_t flags = ff_none; + if (manifestly_const_eval == mce_false) + flags |= ff_mce_false; + return cp_fold_rvalue (x, flags); +} + +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 @@ -2479,7 +2488,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 *-*-* } } }