From patchwork Thu Sep 14 11:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 139507 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp271441vqi; Thu, 14 Sep 2023 04:17:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFmmEEKkVrHJhtl2DSpcEC6eknCPIm0YhbTw6vBZO7GfSI7OL7S76j6jw0CHJC6mud3J80C X-Received: by 2002:a2e:998b:0:b0:2b6:cb55:72bc with SMTP id w11-20020a2e998b000000b002b6cb5572bcmr5066836lji.1.1694690242951; Thu, 14 Sep 2023 04:17:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694690242; cv=none; d=google.com; s=arc-20160816; b=KY7LmwDCXTCviiV6v18OuFH1XIfmD2J4Bge4Vm9eiuv4SadctRUkM6M6w6ucJlrsIY pIitwamwdMTBUl9pLwE35rIFi+FsvyYUHxReQCDV1rV1TrNTr+zWfXONn3mkj/BXUYlr MTyax0G/jBAq4BGLn4TDxyZb1qrHZAgOGoLCBZ9psN2hkhyWp5KeljkV2h3N2R8WXmBp AUYu4vY3E9+45WvH9AeLt5Q4UCFU+7lnZApunKG3SaVK7pCLppZ07XRFTB4BoaKhEVZK DFX2MEkfFixS4EMjIum0GR58K5FHDG1fI0AohaBzPn9cpvpl9AHNAMFU9QAm6ABwh7s8 l6UQ== 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=330W8O8S30gQsVsotlOE8CmPlUWK1CJ4WMSUfFWL3no=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=GVfYwEtg6frn0Qozz+dBadKI73F0coY0wTOnt6g4wjuvAgQBgRWczT1b9VZMvOXHmy 5oWKzZOtKL8zL1BnOZvS2166RW/7q6Zo6/AWjvadUqpbbVf3YRwr54rCBfU/xRu28TlC 6tVerVg4TWJK9hgRhmm8Fh4lhswFktRuEMv2SsYe5teS50z8KZ2jjwkD+ics8fjvhM6F 5s7enh90B2JpHSgd2vZbzX8XBaTgGkxvZByHUmIKPE85Hxo3h7qwwv/YQHYH8+DZ3VAM hWHEpZ14IKRj3uknpjmsOb4ZcAkJDJOdcrTduKqsHUwJ7/3ydrAM5dlFC9QuyW7MMiGB JSQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QD49gfCt; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ox30-20020a170907101e00b009a1aea8249fsi1185099ejb.314.2023.09.14.04.17.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 04:17:22 -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=QD49gfCt; 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 32C2D3857B8E for ; Thu, 14 Sep 2023 11:17:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32C2D3857B8E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694690240; bh=330W8O8S30gQsVsotlOE8CmPlUWK1CJ4WMSUfFWL3no=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=QD49gfCt3dVOEQWPkkdTV+iHj477/4eukBRVhHO+X2adXNC6P1tiAMCPcVMBVSDtD HpsZpz51OHX5REUilxTuJSiiFDLEQp/rVsz8G0xX0ZgvFDrYAU2j8eT/oUW9pqTyuo YzYgrTs1g8/GWM2T5VxPgDf+MjvWwFLHtnunZ+9Q= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 981613858D20 for ; Thu, 14 Sep 2023 11:16:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 981613858D20 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id A75421F459 for ; Thu, 14 Sep 2023 11:16:35 +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 94CFA2C143 for ; Thu, 14 Sep 2023 11:16:35 +0000 (UTC) Date: Thu, 14 Sep 2023 11:16:35 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111294 - better DCE after forwprop User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 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 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: , 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: <20230914111720.32C2D3857B8E@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777011515956121309 X-GMAIL-MSGID: 1777011515956121309 The following adds more aggressive DCE to forwprop to clean up dead stmts when folding a stmt leaves some operands unused. The patch uses simple_dce_from_worklist for this purpose, queueing original operands before substitution and folding, but only if we folded the stmt. This removes one dead stmt biasing threading costs in a later pass but it doesn't resolve the optimization issue in the PR yet. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/111294 * tree-ssa-forwprop.cc (pass_forwprop::execute): Track operands that eventually become dead and use simple_dce_from_worklist to remove their definitions if they did so. * gcc.dg/tree-ssa/evrp10.c: Adjust. * gcc.dg/tree-ssa/evrp6.c: Likewise. * gcc.dg/tree-ssa/forwprop-31.c: Likewise. * gcc.dg/tree-ssa/neg-cast-3.c: Likewise. --- gcc/testsuite/gcc.dg/tree-ssa/evrp10.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/evrp6.c | 5 ++-- gcc/testsuite/gcc.dg/tree-ssa/forwprop-31.c | 3 +-- gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c | 4 +-- gcc/tree-ssa-forwprop.cc | 27 +++++++++++++++++---- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp10.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp10.c index 6ca00e4adaa..776c80c684f 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/evrp10.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp10.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fdump-tree-evrp" }*/ +/* { dg-options "-O2 -fdump-tree-evrp -fno-tree-forwprop" }*/ typedef __INT32_TYPE__ int32_t; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp6.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp6.c index aaeec68866e..0f9561b6a72 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/evrp6.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp6.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fdump-tree-evrp-details" } */ +/* { dg-options "-O2 -fdump-tree-evrp-details -fdump-tree-mergephi1" } */ extern void abort (void); @@ -18,4 +18,5 @@ foo (int k, int j) return j; } -/* { dg-final { scan-tree-dump "\\\[12, \\+INF" "evrp" } } */ +/* { dg-final { scan-tree-dump "\\\[11, \\+INF" "evrp" } } */ +/* { dg-final { scan-tree-dump-not "abort" "mergephi1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-31.c b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-31.c index edf80264884..40cc86383fa 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-31.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-31.c @@ -9,6 +9,5 @@ int foo (int x) return w - z; /* becomes 0 */ } -/* Only z = x + 1 is retained. */ -/* { dg-final { scan-tree-dump-times " = " 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " = " 0 "forwprop1" } } */ /* { dg-final { scan-tree-dump "return 0;" "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c index 7b23ca85d1f..61b89403a93 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c @@ -10,6 +10,4 @@ unsigned f(_Bool a) } /* There should be no cast to int at all. */ -/* Forwprop1 does not remove all of the statements. */ -/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "forwprop1" { xfail *-*-* } } } */ -/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "forwprop1" } } */ diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc index 94ca47a9726..d4e9202a2d4 100644 --- a/gcc/tree-ssa-forwprop.cc +++ b/gcc/tree-ssa-forwprop.cc @@ -53,6 +53,7 @@ along with GCC; see the file COPYING3. If not see #include "cgraph.h" #include "tree-ssa.h" #include "gimple-range.h" +#include "tree-ssa-dce.h" /* This pass propagates the RHS of assignment statements into use sites of the LHS of the assignment. It's basically a specialized @@ -3502,8 +3503,9 @@ pass_forwprop::execute (function *fun) |= EDGE_EXECUTABLE; auto_vec to_fixup; auto_vec to_remove; + auto_bitmap simple_dce_worklist; + auto_bitmap need_ab_cleanup; to_purge = BITMAP_ALLOC (NULL); - bitmap need_ab_cleanup = BITMAP_ALLOC (NULL); for (int i = 0; i < postorder_num; ++i) { gimple_stmt_iterator gsi; @@ -3902,10 +3904,14 @@ pass_forwprop::execute (function *fun) { tree use = USE_FROM_PTR (usep); tree val = fwprop_ssa_val (use); - if (val && val != use && may_propagate_copy (use, val)) + if (val && val != use) { - propagate_value (usep, val); - substituted_p = true; + bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use)); + if (may_propagate_copy (use, val)) + { + propagate_value (usep, val); + substituted_p = true; + } } } if (substituted_p @@ -3925,6 +3931,11 @@ pass_forwprop::execute (function *fun) && gimple_call_noreturn_p (stmt)); changed = false; + auto_vec uses; + FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE) + if (uses.space (1)) + uses.quick_push (USE_FROM_PTR (usep)); + if (fold_stmt (&gsi, fwprop_ssa_val)) { changed = true; @@ -3935,6 +3946,12 @@ pass_forwprop::execute (function *fun) if (gimple_cond_true_p (cond) || gimple_cond_false_p (cond)) cfg_changed = true; + /* Queue old uses for simple DCE. */ + for (tree use : uses) + if (TREE_CODE (use) == SSA_NAME + && !SSA_NAME_IS_DEFAULT_DEF (use)) + bitmap_set_bit (simple_dce_worklist, + SSA_NAME_VERSION (use)); } if (changed || substituted_p) @@ -4115,6 +4132,7 @@ pass_forwprop::execute (function *fun) release_defs (stmt); } } + simple_dce_from_worklist (simple_dce_worklist, to_purge); /* Fixup stmts that became noreturn calls. This may require splitting blocks and thus isn't possible during the walk. Do this @@ -4135,7 +4153,6 @@ pass_forwprop::execute (function *fun) cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge); cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup); BITMAP_FREE (to_purge); - BITMAP_FREE (need_ab_cleanup); if (get_range_query (fun) != get_global_range_query ()) disable_ranger (fun);