From patchwork Tue Jun 6 07:21:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 103595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3205190vqr; Tue, 6 Jun 2023 00:22:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4syoVpXgtkaihm7N1Igd7/6ImKZCeH70dR2Z5tnLBrWcYTm4ZYbsAkLpZJ6jBNgNaQdqLx X-Received: by 2002:aa7:cd55:0:b0:514:8e6f:7113 with SMTP id v21-20020aa7cd55000000b005148e6f7113mr1098850edw.22.1686036122069; Tue, 06 Jun 2023 00:22:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686036122; cv=none; d=google.com; s=arc-20160816; b=ZN1KXQNFwc1IITwbnG69H9T83tEcbJXUNMmQACIIl6CMBAV0jy5Q5fuUpxzYKLxxGD cThjYX/IJ/lRWe2ex1s+Ra76UkwZImNo7ByuVf/30hkImkz3urEIp9ogYUMTOKpIImJy WQsv1U9kpaXxpt0CLlO0VuXMS1162x5EQ9LB+PNyjKz5tsR2zRMjywZJQOY8jYpUUG1r BtCwfRrEO+SOOg5Jr0rjEbJgrzqRCX+5YjUcRYvHTjxgiHVco3orvJeGdbOjbPnvEgb2 7G6ze4kq/EmK50ePY7gA6wuxbpTmD7/+g6oqw9G1Ka6XoC5ZEnuS+IvYAOdD/yFcn85P KIyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=wskYmdlAqn0YmMgLpUKdMtQk6FEzyxhE9vAa/0gh6SM=; b=CIZZGEn0aEjtkAMRYHTfs9Vd5low/3cRCzH9DIm28ellNwj1vPdrL2hiZlUMjmj4zw o6r2HTgFic2009FtXnfiYUnW+Kgh8vKC6BbO0lvTfq1x2nhbX9Qc5m8E3lyh5BXfVi5w B40kr99eyICHlFCaBxhhoOjkWAazjPR4q0ZafOvNVaolZ45LtmgmD1JgBY2/60I5Q9hT WLza3XD2JZrWJYqAOVVMipDdnoq/XE7BWH2XeE5qA24cANbuC+L2QZwl9zHS7pyncck5 4SR0YP2pq8mZg1T6XNAelUO5E3c0hy/semOjA1B0goa08w4F/OTR/qw5tjmQCzveJhl0 jy3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BNup6mEh; 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 a1-20020a50e701000000b00516337c4ba2si6429982edn.630.2023.06.06.00.22.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 00:22:02 -0700 (PDT) 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=BNup6mEh; 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 63CA83857019 for ; Tue, 6 Jun 2023 07:21:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 63CA83857019 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686036111; bh=wskYmdlAqn0YmMgLpUKdMtQk6FEzyxhE9vAa/0gh6SM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BNup6mEhJQLElEAKENescvjv6xtqBcUmJ5S1hSSeeKgJs5cYgNdd5AEU5J8/JSEbZ +oBm0fNj7QYiB8DM3TB6wxTzA3M0D1y6C1O6kE+lxNB5j/1Vs30fdbZrtTyr3USaZ2 bgQzvkNVIyLR3MUfYyJRMPic76Hs0omO1TkwNgso= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 45080385841C for ; Tue, 6 Jun 2023 07:21:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 45080385841C Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id EB103218B5 for ; Tue, 6 Jun 2023 07:21:06 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id DF0DC2C141 for ; Tue, 6 Jun 2023 07:21:06 +0000 (UTC) Date: Tue, 6 Jun 2023 07:21:06 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] middle-end/110055 - avoid CLOBBERing static variables User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230606072151.63CA83857019@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767937012647137970?= X-GMAIL-MSGID: =?utf-8?q?1767937012647137970?= The gimplifier can elide initialized constant automatic variables to static storage in which case TARGET_EXPR gimplification needs to avoid emitting a CLOBBER for them since their lifetime is no longer limited. Failing to do so causes spurious dangling-pointer diagnostics on the added testcase for some targets. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR middle-end/110055 * gimplify.cc (gimplify_target_expr): Do not emit CLOBBERs for variables which have static storage duration after gimplifying their initializers. * g++.dg/warn/Wdangling-pointer-pr110055.C: New testcase. --- gcc/gimplify.cc | 4 +++- .../g++.dg/warn/Wdangling-pointer-pr110055.C | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index d0d16a24820..d7cfa6321a0 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -7173,8 +7173,10 @@ gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) gimplify_and_add (init, &init_pre_p); /* Add a clobber for the temporary going out of scope, like - gimplify_bind_expr. */ + gimplify_bind_expr. But only if we did not promote the + temporary to static storage. */ if (gimplify_ctxp->in_cleanup_point_expr + && !TREE_STATIC (temp) && needs_to_live_in_memory (temp)) { if (flag_stack_reuse == SR_ALL) diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C new file mode 100644 index 00000000000..77dbbf380b6 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-pointer-pr110055.C @@ -0,0 +1,16 @@ +// { dg-do compile } +// { dg-require-effective-target c++11 } +// { dg-options "-O3 -fno-exceptions -Wdangling-pointer" } + +#include +#include + +struct Data { + std::vector v = {1, 1}; +}; + +int main() +{ + Data a; + Data b; +}