From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 991B23858D3C; Tue, 20 Jun 2023 11:18:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 991B23858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687259924; bh=GbpmQOmZgdyzlLQt2b/CfO3NwOiB79Po1PkA9BHB8UM=; h=From:To:Subject:Date:From; b=KTgL3PEuC2YldKak9DA5YqxqW4IvOKqlaqzLyDcOjyeWmKRUG4I+amCgfS16mbGyg GoKischrNcOBJVFR+nLVXoZ83UzCVm8xuv2ApBQbkPAc+whhJO69IRNdZo/TavDWQg NpRYC59N60O0462ziNabuT3tleJaPpMrDZJuIjMQ= 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-1982] Improve DSE to handle stores before __builtin_unreachable () X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: 85107abeb71bbf6edc7dfb7691527be104c11735 X-Git-Newrev: 9d597e00757eedfb6b6cf5e0b5138b8029aeb28e Message-Id: <20230620111844.991B23858D3C@sourceware.org> Date: Tue, 20 Jun 2023 11:18:44 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9d597e00757eedfb6b6cf5e0b5138b8029aeb28e commit r14-1982-g9d597e00757eedfb6b6cf5e0b5138b8029aeb28e Author: Richard Biener Date: Mon Jun 19 14:19:47 2023 +0200 Improve DSE to handle stores before __builtin_unreachable () DSE isn't good at identifying program points that end lifetime of variables that are not associated with virtual operands. But at least for those that end basic-blocks we can handle the simple case where this ending is in the same basic-block as the definition we want to elide. That should catch quite some common cases already. * tree-ssa-dse.cc (dse_classify_store): When we found no defs and the basic-block with the original definition ends in __builtin_unreachable[_trap] the store is dead. * gcc.dg/tree-ssa/ssa-dse-47.c: New testcase. * c-c++-common/asan/pr106558.c: Avoid undefined behavior due to missing return. Diff: --- gcc/testsuite/c-c++-common/asan/pr106558.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c | 17 +++++++++++++++++ gcc/tree-ssa-dse.cc | 21 ++++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/gcc/testsuite/c-c++-common/asan/pr106558.c b/gcc/testsuite/c-c++-common/asan/pr106558.c index d82b2dc7a83..c8cefdf09ff 100644 --- a/gcc/testsuite/c-c++-common/asan/pr106558.c +++ b/gcc/testsuite/c-c++-common/asan/pr106558.c @@ -8,7 +8,7 @@ int **c = &b; int d[1]; int *e = &d[1]; -static int f(int *g) { +static void f(int *g) { *b = e; *c = e; *b = 2; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c new file mode 100644 index 00000000000..659f1d0d415 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-dse1-details" } */ + +int a; +int b[3]; +void test() +{ + if (a > 0) + { + b[0] = 0; + b[1] = 1; + b[2] = 2; + __builtin_unreachable (); + } +} + +/* { dg-final { scan-tree-dump-times "Deleted dead store" 3 "dse1" } } */ diff --git a/gcc/tree-ssa-dse.cc b/gcc/tree-ssa-dse.cc index eabe8ba4522..3c7a2e9992d 100644 --- a/gcc/tree-ssa-dse.cc +++ b/gcc/tree-ssa-dse.cc @@ -1118,7 +1118,26 @@ dse_classify_store (ao_ref *ref, gimple *stmt, if (defs.is_empty ()) { if (ref_may_alias_global_p (ref, false)) - return DSE_STORE_LIVE; + { + basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (defvar)); + /* Assume that BUILT_IN_UNREACHABLE and BUILT_IN_UNREACHABLE_TRAP + do not need to keep (global) memory side-effects live. + We do not have virtual operands on BUILT_IN_UNREACHABLE + but we can do poor mans reachability when the last + definition we want to elide is in the block that ends + in such a call. */ + if (EDGE_COUNT (def_bb->succs) == 0) + if (gcall *last = dyn_cast (*gsi_last_bb (def_bb))) + if (gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE) + || gimple_call_builtin_p (last, + BUILT_IN_UNREACHABLE_TRAP)) + { + if (by_clobber_p) + *by_clobber_p = false; + return DSE_STORE_DEAD; + } + return DSE_STORE_LIVE; + } if (by_clobber_p) *by_clobber_p = false;