From patchwork Thu Oct 12 09:01:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 151837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp1081874vqb; Thu, 12 Oct 2023 02:01:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTTCdi5i5+oQhIu+zsVrTWjf9p6+XGWdZ81EWrcjBQSVFA5LQmRZOhxmAWvm1YWZdXbACJ X-Received: by 2002:aa7:c98b:0:b0:52a:38c3:1b4b with SMTP id c11-20020aa7c98b000000b0052a38c31b4bmr20002952edt.15.1697101310121; Thu, 12 Oct 2023 02:01:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697101310; cv=none; d=google.com; s=arc-20160816; b=ndUe7X79nX83hZknzjzealx6qTJhWi+fYnj0fkARtMwUVc3PQCS94oLPjPL5NPyh5O 11OSBxq1/SBp7AmUqPISCkjKdXxjx9EHeAjTtxjraM5LKj19/sN6PPD2Lrpmuj6PLrCN wkwOS07SBYNW39acxWdh3H8NrPCD/wpD9RYyJHnbe4Ldg5oDAp+OICWzT76/0TckKCIb t5skLl5A5aQ7YjMutqxdCkddf6eTbB6fUWQtowUG19/7uKFk4+HXN0y1JUfE+7Jx+kfv Ao3XDd+KfR4dWhElgVXEj2A3w8tyGmLo1usLZwTHFkqxKrabI+kmfdGYKqK8PRk1Kemj Jwcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:cc:to:from:date:dmarc-filter:delivered-to; bh=7/c12vhzheqM0CoS+aBbKZEMdkiJezfshYDpsx8RYBQ=; fh=+H+PfslJQ8SfgojuI5pxEuPHAhp/Ml9gJNsp0+tS7IU=; b=ChAWkJ09b09CVj1wHvGKNpu+UREe3XjHudAThdJL/boKgiH1c7l2ymQtDvKI7v2hVv DtevrmOVFdaUO6Izeeol6pmbXgGWCtNe38H68i9+XK6MoVt8/2hCte9cNMh88oD2ZlnO BFclaWvDESBLx3xSCTjJXWepE2CY05XEIyijnAamg12FliGZOffwQy8qKAmiQrcjLw87 jd+dMLiBQ+feiH7W7Fz/Di/IJR97BQWw7YnXVaSPyukubcodA8S+L5UAHSYk6KHPk71T lqSYPu8XDS+x8zyqe+Nlqt76SDTITbRLH9+I2Eyg5AEEPY4KAVwKwwXbj/Z1U+iETAbC xtRg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n4-20020aa7c444000000b00535b62d9546si7246065edr.400.2023.10.12.02.01.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 02:01:50 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 196C73856DC8 for ; Thu, 12 Oct 2023 09:01:45 +0000 (GMT) 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 CAA3D3858CDA for ; Thu, 12 Oct 2023 09:01:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CAA3D3858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 064B72188D; Thu, 12 Oct 2023 09:01:18 +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 CB02C2C92D; Thu, 12 Oct 2023 09:01:17 +0000 (UTC) Date: Thu, 12 Oct 2023 09:01:17 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: Jan Hubicka Subject: [PATCH] tree-optimization/111773 - avoid CD-DCE of noreturn special calls User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out1.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out1.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [-0.01 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.20)[suse.de]; R_SPF_SOFTFAIL(0.60)[~all:c]; MISSING_MID(2.50)[]; VIOLATED_DIRECT_SPF(3.50)[]; MX_GOOD(-0.01)[]; RCPT_COUNT_TWO(0.00)[2]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -0.01 X-Rspamd-Queue-Id: 064B72188D X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, MISSING_MID, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231012090145.196C73856DC8@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779539703255969392 X-GMAIL-MSGID: 1779539703255969392 The support to elide calls to allocation functions in DCE runs into the issue that when implementations are discovered noreturn we end up DCEing the calls anyway, leaving blocks without termination and without outgoing edges which is both invalid IL and wrong-code when as in the example the noreturn call would throw. The following avoids taking advantage of both noreturn and the ability to elide allocation at the same time. For the testcase it's valid to throw or return 10 by eliding the allocation. But we have to do either where currently we'd run off the function. Bootstrapped and tested on x86_64-unknown-linux-gnu. Honza, any objections here? Thanks, Richard. PR tree-optimization/111773 * tree-ssa-dce.cc (mark_stmt_if_obviously_necessary): Do not elide noreturn calls that are reflected to the IL. * g++.dg/torture/pr111773.C: New testcase. --- gcc/testsuite/g++.dg/torture/pr111773.C | 31 +++++++++++++++++++++++++ gcc/tree-ssa-dce.cc | 8 +++++++ 2 files changed, 39 insertions(+) create mode 100644 gcc/testsuite/g++.dg/torture/pr111773.C diff --git a/gcc/testsuite/g++.dg/torture/pr111773.C b/gcc/testsuite/g++.dg/torture/pr111773.C new file mode 100644 index 00000000000..af8c687252c --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr111773.C @@ -0,0 +1,31 @@ +// { dg-do run } + +#include + +void* operator new(std::size_t sz) +{ + throw std::bad_alloc{}; +} + +int __attribute__((noipa)) foo () +{ + int* p1 = static_cast(::operator new(sizeof(int))); + return 10; +} + +int main() +{ + int res; + try + { + res = foo (); + } + catch (...) + { + return 0; + } + + if (res != 10) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc index f0b02456132..bbdf9312c9f 100644 --- a/gcc/tree-ssa-dce.cc +++ b/gcc/tree-ssa-dce.cc @@ -221,6 +221,14 @@ mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive) case GIMPLE_CALL: { + /* Never elide a noreturn call we pruned control-flow for. */ + if ((gimple_call_flags (stmt) & ECF_NORETURN) + && gimple_call_ctrl_altering_p (stmt)) + { + mark_stmt_necessary (stmt, true); + return; + } + tree callee = gimple_call_fndecl (stmt); if (callee != NULL_TREE && fndecl_built_in_p (callee, BUILT_IN_NORMAL))