From patchwork Mon Feb 20 19:46:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 59647 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1482359wrn; Mon, 20 Feb 2023 11:47:32 -0800 (PST) X-Google-Smtp-Source: AK7set/rK3DC8sPcuSjFAMwmAENuSl71U6cE8fEpEKGix07lZyzmCTYxORO3Xw5gc+P0D/5f6wux X-Received: by 2002:aa7:db82:0:b0:4ad:5217:a191 with SMTP id u2-20020aa7db82000000b004ad5217a191mr1960941edt.42.1676922452003; Mon, 20 Feb 2023 11:47:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676922451; cv=none; d=google.com; s=arc-20160816; b=oCGGTQdcvDqWqhXNd9i85lMDHDPS7YmLi7qqh+33rRy0sj/2tnMe0sUvdloKqc/HOF lwepXCTVepuC7lqOVBhEPB6TByUJ4fv+m09pZ7vQKLQZ8PsRjCCvUjtCq5ukgzqDahMY lF4OsujMAmm8k0ixQG7N8yAsUXhPi/1bTSTd4/iyl0azOd+B8b5G3zwKTYvKJGnyrZRl 1vF496jomYIXmvJkc1ukgKcfF+EZK4HdNrThPAd5ZB7j+I1uw9DHfRvv+kNOXeqA86uZ S3YfYXHmkWV08noV4zmXYi9ltnZz9UUvqciZYYDSLBfO2UNxCm+PCX59AH6mPcTj11a+ oDZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=6uUYBppdVoWq6w6lUo5e8vrbjHxG/ElQK5ApjxIifrA=; b=K4NjSQI01pAEYI7pI592+0jVfr6sJCXK46aPuN/EN/dMo13hfhCdIQA8B38GX3LsDm BVdaaD3t2DzkepP9xMdkvjP7+lAqd7z8OGwjGprMBPZK0CxLljmPATLCpaVKLd+RUnsy xhZV+Qm3CHFp1/2r+vVP7yFCii0HzEt7SAXcZQfz87swRRyMTXAMyZwM25njRLFvk8lD YMwJj4oDvOnT2+ZQevYX9BOIyVgUKpmjANpI1QRGlUTvp1+CMPT3pbBz+tBy1Dff9P6D X+X0DKFJzADKhXYZEujsQArCvSsjAE3O1evErSrE25R86C6sKzOc6OiUHGsmK+5AhUme XWyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="S/2buheD"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z25-20020aa7c659000000b004ace655ef51si16251989edr.192.2023.02.20.11.47.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 11:47:31 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="S/2buheD"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 160FF384DD34 for ; Mon, 20 Feb 2023 19:47:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 160FF384DD34 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676922451; bh=6uUYBppdVoWq6w6lUo5e8vrbjHxG/ElQK5ApjxIifrA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=S/2buheDR47FFcRjko6YFZ1c4KyzxJ6IW6dhHB6sBZcIVLAM3gz1UXCJ/QPqCCvtG ugIIkPW/0rtv7h/RCqqojg1LaT2brT7w7X4nkLI6IIfYmaLP45wWs6+7H7MUWOULeB 8Xuqu1qlwhdKtBl9v8lJ6YxUQxItIiZ8nKea+/Cg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A282E384FB76 for ; Mon, 20 Feb 2023 19:46:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A282E384FB76 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-502-FcXm-pD8Ovm68SRXP_MUDA-1; Mon, 20 Feb 2023 14:46:45 -0500 X-MC-Unique: FcXm-pD8Ovm68SRXP_MUDA-1 Received: by mail-qt1-f198.google.com with SMTP id fv15-20020a05622a4a0f00b003ba0dc5d798so588483qtb.22 for ; Mon, 20 Feb 2023 11:46:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6uUYBppdVoWq6w6lUo5e8vrbjHxG/ElQK5ApjxIifrA=; b=XTvnV8KNz7EGXxnvrGyPXncfugU9TYBbuaokSNV2H80nJz/W1XZTbP/8k/YVYT4nmw By6b24HE2LUNsuQO88ljbcSGy+Iv/vhgYXQrVgDyYftRkNLNO7gvO15iVqqJAjGKgGzq 6YcTC4RXwcyDmd4qxdCOhDiWJ5/Og7iWM/hdjJ8NvRvQyxyHhNKLuND7uN+RzGbitBLY p/AbW4jDY8g4leg3zU8cdgq6Ovm9WpwKKvt3+eD2nC3vh6pLH6Q0pRzp9PvqCIvbKx66 nu3MxoO30Ov2/WX38eKoLFBCUXeHg27huYYcE/jezVDc9XF/khJ9UjdxOztQJvXh6yIv Jswg== X-Gm-Message-State: AO0yUKVR7AiwjIVRHNPXDv8FzVfN66RY7owBhFjhGNGGsGsQl4zJv0gO JOIE+yTWKMU2l098dJtHfgWPXY9Aa2zVYWbUyZxFtFOikzitm5+IFrgLa5kk4W5w7uCCKxNPs5j 6HZx2vmM3NiumRiBAC5CRt5lgdWBFCyJUShRTzOxCzjUxfy+HSGkpWmqDz57N3MbQ+DY2lOUZ X-Received: by 2002:a05:622a:1aa0:b0:3bd:156f:6679 with SMTP id s32-20020a05622a1aa000b003bd156f6679mr23682029qtc.21.1676922404232; Mon, 20 Feb 2023 11:46:44 -0800 (PST) X-Received: by 2002:a05:622a:1aa0:b0:3bd:156f:6679 with SMTP id s32-20020a05622a1aa000b003bd156f6679mr23682006qtc.21.1676922403879; Mon, 20 Feb 2023 11:46:43 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id u7-20020ae9d807000000b0073baa5ec0besm7505351qkf.115.2023.02.20.11.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 11:46:43 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: constant non-copy-init is manifestly constant [PR108243] Date: Mon, 20 Feb 2023 14:46:41 -0500 Message-Id: <20230220194641.4172416-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.2.501.gd9d677b2d8 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758380637161494900?= X-GMAIL-MSGID: =?utf-8?q?1758380637161494900?= According to [basic.start.static]/2 and [expr.const]/2, a variable with static storage duration initialized with a constant initializer has constant initialization, and such an initializer is manifestly constant-evaluated. We're already getting this right with copy initialization because in that case check_initializer would consistently call store_init_value (which for TREE_STATIC variables calls fold_non_dependent_init with m_c_e=true). But for direct (or default) initialization, we don't always call store_init_value. We instead however always call maybe_constant_init from expand_default_init[1], albeit with m_c_e=false which means we don't always get the "manifestly constant-evaluated" part right for copy-init. This patch fixes this by simply passing m_c_e=true to this call to maybe_constant_init for static storage duration variables, mirroring what store_init_value basically does. [1]: this maybe_constant_init call isn't reached in the copy-init case because there init is a CONSTRUCTOR rather than a TREE_LIST so expand_default_init exits early returning an INIT_EXPR. This INIT_EXPR is ultimately what causes us to consistently hit the store_init_value code path from check_initializer in the copy-init case. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? Would it be suitable to backport this to the 12 branch since it should only affect C++20 code? PR c++/108243 gcc/cp/ChangeLog: * init.cc (expand_default_init): Pass m_c_e=true instead of =false to maybe_constant_init when initializing a variable with static storage duration. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/is-constant-evaluated14.C: New test. --- gcc/cp/init.cc | 5 +- .../g++.dg/cpp2a/is-constant-evaluated14.C | 140 ++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 52e96fbe590..705a5b3bdb6 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -2203,7 +2203,10 @@ expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, tree fn = get_callee_fndecl (rval); if (fn && DECL_DECLARED_CONSTEXPR_P (fn)) { - tree e = maybe_constant_init (rval, exp); + bool manifestly_const_eval = false; + if (VAR_P (exp) && TREE_STATIC (exp)) + manifestly_const_eval = true; + tree e = maybe_constant_init (rval, exp, manifestly_const_eval); if (TREE_CONSTANT (e)) rval = cp_build_init_expr (exp, e); } diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C new file mode 100644 index 00000000000..365bca3fd9a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C @@ -0,0 +1,140 @@ +// PR c++/108243 +// Verify a variable with static storage duration initialized with a +// constant initializer has constant initialization, and the initializer +// is manifestly constant-evaluated. +// { dg-do run { target c++11 } } +// { dg-additional-options "-fdump-tree-original" } + +#include + +struct A { + constexpr A(int n) : n(n), m(__builtin_is_constant_evaluated()) { } + constexpr A() : A(42) { } + void verify_mce() const { + if (m != 1) __builtin_abort(); + } + int n; + int m; +}; + +A a1 = {42}; +A a2{42}; +A a3(42); +A a4; +A a5{}; + +void f() { + static A a1 = {42}; + static A a2{42}; + static A a3(42); + static A a4; + static A a5{}; + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); +} + +template +void g() { + static A a1 = {42}; + static A a2{42}; + static A a3(42); + static A a4; + static A a5{}; + static A a6 = {N...}; + static A a7{N...}; + static A a8(N...); + for (auto& a : {a1, a2, a3, a4, a5, a6, a7, a8}) + a.verify_mce(); +} + +struct B { + static A a1; + static A a2; + static A a3; + static A a4; + static A a5; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + } +}; + +A B::a1 = {42}; +A B::a2{42}; +A B::a3(42); +A B::a4; +A B::a5{}; + +template +struct BT { + static A a1; + static A a2; + static A a3; + static A a4; + static A a5; + static A a6; + static A a7; + static A a8; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + } +}; + +template A BT::a1 = {42}; +template A BT::a2{42}; +template A BT::a3(42); +template A BT::a4; +template A BT::a5{}; +template A BT::a6 = {N...}; +template A BT::a7{N...}; +template A BT::a8(N...); + +#if __cpp_inline_variables +struct BI { + static inline A a1 = {42}; + static inline A a2{42}; + static inline A a3; + static inline A a4{}; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4}) + a.verify_mce(); + } +}; + +template +struct BIT { + static inline A a1 = {42}; + static inline A a2{42}; + static inline A a3; + static inline A a4{}; + static inline A a5 = {N...}; + static inline A a6{N...}; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5, a6}) + a.verify_mce(); + } +}; +#endif + +int main() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + + f(); + g<42>(); + g<>(); + + B::verify_mce(); + BT<42>::verify_mce(); + BT<>::verify_mce(); + +#if __cpp_inline_variables + BI::verify_mce(); + BIT<42>::verify_mce(); + BIT<>::verify_mce(); +#endif +} + +// { dg-final { scan-tree-dump-not "static initializers for" "original" } } +// { dg-final { scan-tree-dump-not "cxa_guard_acquire" "original" } }