From patchwork Mon Jan 23 11:16:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 47119 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1549233wrn; Mon, 23 Jan 2023 03:17:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXuS4ainXKXOspexlwn4jhZiJpJwqoOMGRS3DxTSM2kFxyqFq8/4xcEcSzjt7DMVW00YxJ8l X-Received: by 2002:a17:907:c498:b0:862:1709:a06f with SMTP id tp24-20020a170907c49800b008621709a06fmr23352655ejc.37.1674472637377; Mon, 23 Jan 2023 03:17:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674472637; cv=none; d=google.com; s=arc-20160816; b=XaL+ozNnUJZWf9tOa1J4B8V6myUCsZVN+oCr2j0U+T0Bmmb5WL/6mSp2hPexDtvvGm CyyZmkx2y5flvl3yAPupQE6E+9hVBmPSoX5gF0WNQao0hCUH99NfyDLq0Il8mlCeem/d sjYNl4JDPWDOcojVfbiYwWKkacc3jQKxGCYOZrYN8SoJsKhsQxT3YSuo6RKg1OkSmcqB ljZCUWWjDQjn9ie4oZd0wy5hF0yh48bJxeeFyf7jT3BZf7ZDX9Q5CNyT3sLbHnKuSM+n HsudwD0sansbXMGJbkRC5bhR4A6ABsj29Do4gqwHCdkUQ3wupMr+R7555v3i1kT8TZsk BrAQ== 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-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=UFvY2GXe88H+cMqreuezBovqTSd7FKpFYJ+Ajy5wqIU=; b=J05W69fG/yqfct+/Kgl9Ake45UKVwUWnkQX0e1xZTIBo/1ZzXhMMVrY9ir1Uukk4n1 z85XbcuKYSE4x+dLgP8bU2nKJnngtJkerM+PkhQim1LlGASgSyve0Y5otjHy4lSGJLTM f5oJvCTvBye+HEb7oZS+5/rFRbNlCMEm/jCSJrP2wDCVw3ccOrjpYw9KkV2oTSFlEnpN 5atCz5PL5xAvhmxj/3c4BtALd47jFCI6cpe7N7TZa+UdnEY6V7WHoypvfofqBe7Vv2Ty AMtdDxPCUoO3ZAZEsZKd7bcurQaDJnlmWhHTKP6EKpx2J50MPpSGavEis3YDvS09hODT bIZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=nvA2n1m0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id wz1-20020a170906fe4100b0084d4733f909si45419163ejb.964.2023.01.23.03.17.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 03:17:17 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=nvA2n1m0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 107023858C27 for ; Mon, 23 Jan 2023 11:17:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 107023858C27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674472636; bh=UFvY2GXe88H+cMqreuezBovqTSd7FKpFYJ+Ajy5wqIU=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=nvA2n1m0kqeVoNM44CCYoVW1XEhlEGm4iiUNwmQdSuNihEJKCUJh9ISe29n4pt5qN eseYTSYfYlknLSAy5pnA2CJOz0qW7SxZ15WtT7Kf2xogSx+Il4zIzWKDBS86I/+XV1 1scGyy5AvOwADMKGPBlUK7aZWfUS5cw7jTvGPz1M= 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 2B2363858D32 for ; Mon, 23 Jan 2023 11:16:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B2363858D32 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-212-jYKSBenZMlSeQ9T8zrib3A-1; Mon, 23 Jan 2023 06:16:25 -0500 X-MC-Unique: jYKSBenZMlSeQ9T8zrib3A-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8FD9B18A6460; Mon, 23 Jan 2023 11:16:25 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4B439492C3C; Mon, 23 Jan 2023 11:16:25 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 30NBGMc61546299 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 23 Jan 2023 12:16:22 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 30NBGK9P1546298; Mon, 23 Jan 2023 12:16:20 +0100 Date: Mon, 23 Jan 2023 12:16:20 +0100 To: Jason Merrill , Jan Hubicka , Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, cgraphbuild: Handle DECL_VALUE_EXPRs in record_reference [PR108474] Message-ID: References: <799bfe17-b60d-7353-d676-5296e64b3769@redhat.com> <6f8e0faf-55eb-cf20-e796-a1828197bb23@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1755811820154216143?= X-GMAIL-MSGID: =?utf-8?q?1755811820154216143?= On Mon, Jan 23, 2023 at 09:25:50AM +0100, Jakub Jelinek via Gcc-patches wrote: > On Sun, Jan 22, 2023 at 07:19:07PM -0500, Jason Merrill wrote: > > > I was just trying to be careful, because unfortunately this spot > > > doesn't mean it really is only expanded in static var DECL_INITIAL, > > > it can make it into dynamic initializers, and most of DECL_VALUE_EXPRs > > > appear only in runtime code, otherwise we'd have much more of these issues. > > > > But it shouldn't be harmful anywhere, right? > > > > > But if you think it is ok, I'll test tonight a version just with > > > if (!data->genericize && DECL_HAS_VALUE_EXPR_P (stmt) > > > > OK with that change. > > > > Though, actually, why not instead fix expand_expr_real_1 (and staticp) to > > look through DECL_VALUE_EXPR? > > I'm afraid that is way too late. GIMPLE optimizations don't try to > regimplify expressions they take from DECL_INITIAL of static vars, they > just unshare them and use them. So, if this is to be done in the generic > code, it would need to be done by cgraph around the time when it gimplifies > function if there is any such point for variables. I believe the right spot is record_reference, which is called through walk_tree from record_references_in_initializer which is called from varpool_node::analyze. The new tests as well as g++.dg/init/pr53932.C pass with it, will do full bootstrap/regtest soon. What do you think about this? 2023-01-23 Jakub Jelinek PR c++/108474 * cgraphbuild.cc: Include gimplify.h. (record_reference): Replace VAR_DECLs with DECL_HAS_VALUE_EXPR_P with their corresponding DECL_VALUE_EXPR expressions after unsharing. * cp-gimplify.cc (cp_fold_r): Revert 2023-01-19 changes. * g++.dg/cpp1z/decomp57.C: New test. * g++.dg/cpp1z/decomp58.C: New test. Jakub --- gcc/cgraphbuild.cc.jj 2023-01-02 09:32:34.889104620 +0100 +++ gcc/cgraphbuild.cc 2023-01-23 12:10:35.042067571 +0100 @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. #include "gimple-walk.h" #include "ipa-utils.h" #include "except.h" +#include "gimplify.h" /* Context of record_reference. */ struct record_reference_ctx @@ -79,6 +80,17 @@ record_reference (tree *tp, int *walk_su if (VAR_P (decl)) { + /* Replace vars with their DECL_VALUE_EXPR if any. + This is normally done during gimplification, but + static var initializers are never gimplified. */ + if (DECL_HAS_VALUE_EXPR_P (decl)) + { + tree *p; + for (p = tp; *p != decl; p = &TREE_OPERAND (*p, 0)) + ; + *p = unshare_expr (DECL_VALUE_EXPR (decl)); + return record_reference (tp, walk_subtrees, data); + } varpool_node *vnode = varpool_node::get_create (decl); ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR); } --- gcc/cp/cp-gimplify.cc.jj 2023-01-23 11:47:49.889875804 +0100 +++ gcc/cp/cp-gimplify.cc 2023-01-23 11:49:01.227841759 +0100 @@ -1010,16 +1010,6 @@ cp_fold_r (tree *stmt_p, int *walk_subtr } break; - case VAR_DECL: - /* In initializers replace anon union artificial VAR_DECLs - with their DECL_VALUE_EXPRs, as nothing will do it later. */ - if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize) - { - *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt)); - break; - } - break; - default: break; } --- gcc/testsuite/g++.dg/cpp1z/decomp57.C.jj 2023-01-23 11:48:36.367202107 +0100 +++ gcc/testsuite/g++.dg/cpp1z/decomp57.C 2023-01-23 11:48:36.367202107 +0100 @@ -0,0 +1,27 @@ +// PR c++/108474 +// { dg-do link { target c++17 } } + +struct T { int i, j; }; +T h; +auto [i, j] = h; +int &r = i; +int s = i; +int *t = &i; + +void +foo (int **p, int *q) +{ + static int &u = i; + static int v = i; + static int *w = &i; + int &x = i; + int y = i; + int *z = &i; + *p = &i; + *q = i; +} + +int +main () +{ +} --- gcc/testsuite/g++.dg/cpp1z/decomp58.C.jj 2023-01-23 11:48:36.367202107 +0100 +++ gcc/testsuite/g++.dg/cpp1z/decomp58.C 2023-01-23 11:48:36.367202107 +0100 @@ -0,0 +1,39 @@ +// PR c++/108474 +// { dg-do link { target c++17 } } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + template int& get() { return i; } +}; + +template <> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +struct A a; +auto [i, j] = a; +int &r = i; +int s = i; +int *t = &i; + +void +foo (int **p, int *q) +{ + static int &u = i; + static int v = i; + static int *w = &i; + int &x = i; + int y = i; + int *z = &i; + *p = &i; + *q = i; +} + +int +main () +{ +}