From patchwork Tue Jun 20 06:59:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 110264 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3475255vqr; Tue, 20 Jun 2023 00:00:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5CIrQHM1bQMdHhQYDF8pkG48fho3/Ef3e6kJkb7eBHEkDpmJs+kxJYBALrK/fWp7eKPtX3 X-Received: by 2002:a17:907:9627:b0:988:3943:aaa4 with SMTP id gb39-20020a170907962700b009883943aaa4mr7736415ejc.29.1687244440205; Tue, 20 Jun 2023 00:00:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687244440; cv=none; d=google.com; s=arc-20160816; b=ByT0BP//7c8eKoyNfvqqNSfLnEX1BUz7TujT7/cWcmHbZSqzhLQmjaPmE+2EYCqzJB rn7pbqcMRf/2FlvV6Av1DXbIptopEmBaai9mnaU5MACbFFA7AfgyvevUX/fKgloAsGbO iDLZQtuPlc37s0cOANqsBFrZXxfC8X2typpJwLpp4wikZ4HSyL7U9MIpcZFyK4QdHEA2 RPSHj84LvJ0rnTq3M+WklQjD9+jBAsRDnfKt9vq4/wEDWvRNvvsM7aRGLusY4y4S08X5 em/dkS1wIqG4j8L41NX/1t3RoXqRn3QZ520Or/DWP9KMrm/PBrL55TRgO69Xr0dR3cGb yXJA== 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:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=0uHajsVAk8t/A5Nt4sRiYKRTFJP7nlyPBbfdpf2xg+M=; b=ZwJap+ijfEA62yfAhdQM3Fu04SETw5rEB2fozOETJqrxJlu90niH2S79Di1164c/iB 5elmaMZsPF5gRO1sQ4eZFc+F/fAPOt+WxgcGVDpk2fQa+n3tQeoHSHS/77CabipqlQWO qIG8S7/TPFCZCCMp63/pgv2t3WmlDqvYaoQOIQIDN/1ffHm+KqNR6DraSzcBp3VsfQTo kAI/p4TZF9s4K7OIagmTXF6o6dJhFjTCbBM3DAQYqIdwAEWjFjuAI5WYYqldUEHRmgzt 55ppT6qKnKZD73AC7kRM79d59TnOVALFDvDuUY6vBOOFSYTIPmfh86PgY6LvWfASNoVd 7uYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=htJwtSiZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p6-20020a1709060dc600b009893209a983si419482eji.650.2023.06.20.00.00.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 00:00:40 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=htJwtSiZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 10D7638582A3 for ; Tue, 20 Jun 2023 07:00:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10D7638582A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687244439; bh=0uHajsVAk8t/A5Nt4sRiYKRTFJP7nlyPBbfdpf2xg+M=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=htJwtSiZ6FwejjL2hIDywfOTNxQosiknlrJNhXlWFJh76mpK9+0A3nUIjPdfp5EgM lGcNxXzfosiuQI5iRrX+/prEXbl4n/OGXdObhnvRAhLSVzEGZv4c5yFm6eQ0V71xWE 6IgXpksG0+kVrSf6qr6xqqjnk9bYLnE2R0VG/ZuU= 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 89A2E3858D1E for ; Tue, 20 Jun 2023 06:59:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89A2E3858D1E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id B6EA8218A4; Tue, 20 Jun 2023 06:59:55 +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 AAF0F2C141; Tue, 20 Jun 2023 06:59:55 +0000 (UTC) Date: Tue, 20 Jun 2023 06:59:55 +0000 (UTC) To: gcc-patches@gcc.gnu.org cc: Jan Hubicka Subject: [PATCH] Improve DSE to handle stores before __builtin_unreachable () 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: <20230620070039.10D7638582A3@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769204026475936613?= X-GMAIL-MSGID: =?utf-8?q?1769204026475936613?= 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. Bootstrapped and tested on x86_64-unknown-linux-gnu. As you can see from the testcase I had to adjust this possibly can lead to more severe issues when one forgets a return (the C++ frontend places builtin_unreachable () there). I'm still planning to push this improvement unless I hear objections. Thanks, Richard. * 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. --- 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(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c 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;