From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 830423858D1E; Fri, 13 Oct 2023 06:34:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 830423858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697178886; bh=hBiaf5rJoUwKt+aLSbJQkOsYbPNu61D/9BEm27a/46k=; h=From:To:Subject:Date:From; b=xmTfwJxjCc8TZBWNA1VXjiRC2LEgXbfnccxPJv9yvUzEW8ORyTNyXHT+Ns4Y+JS1x Yn3IxAn7IlGD+HBANH35UbxLUWrVdSJ72MgseB2kJP61LfevHX0bH3gnyoHLHqiYT1 IHNHP9UKRmBwCRUM9qIK5ppQeQ6IK91lNZxI0jVs= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-4611] tree-optimization/111773 - avoid CD-DCE of noreturn special calls X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: 6b580560a0e393e3b65567263e2cf5477d670592 X-Git-Newrev: 35b5bb475375dba4ea9101d6db13a6012c4e84ca Message-Id: <20231013063446.830423858D1E@sourceware.org> Date: Fri, 13 Oct 2023 06:34:46 +0000 (GMT) List-Id: https://gcc.gnu.org/g:35b5bb475375dba4ea9101d6db13a6012c4e84ca commit r14-4611-g35b5bb475375dba4ea9101d6db13a6012c4e84ca Author: Richard Biener Date: Thu Oct 12 10:13:58 2023 +0200 tree-optimization/111773 - avoid CD-DCE of noreturn special calls 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. 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. Diff: --- gcc/testsuite/g++.dg/torture/pr111773.C | 31 +++++++++++++++++++++++++++++++ gcc/tree-ssa-dce.cc | 8 ++++++++ 2 files changed, 39 insertions(+) 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))