From patchwork Wed Oct 7 14:55:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1378043 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4C5y7V2nRWz9sTD for ; Thu, 8 Oct 2020 01:55:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B8A4E384242A; Wed, 7 Oct 2020 14:55:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 50C973857026 for ; Wed, 7 Oct 2020 14:55:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 50C973857026 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rguenther@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 2668CAFD4 for ; Wed, 7 Oct 2020 14:55:25 +0000 (UTC) Date: Wed, 7 Oct 2020 16:55:21 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/97307 - improve sinking of loads Message-ID: User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This improves the heuristics finding a sink location for loads that does not cross any store. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2020-10-07 Richard Biener PR tree-optimization/97307 * tree-ssa-sink.c (statement_sink_location): Change heuristic for not skipping stores to look for virtual definitions rather than uses. * gcc.dg/tree-ssa/ssa-sink-17.c: New testcase. * gcc.dg/vect/pr65947-3.c: XFAIL. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-17.c | 15 +++++++ gcc/testsuite/gcc.dg/vect/pr65947-3.c | 5 ++- gcc/tree-ssa-sink.c | 45 +++++++++++---------- 3 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-17.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-17.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-17.c new file mode 100644 index 00000000000..cf2e2a0f766 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-17.c @@ -0,0 +1,15 @@ +/* PR tree-optimization/97307 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-sink-details" } */ + +int pure_f(int a, int b) __attribute__((pure)); +int my_f(int a, int b) +{ + int x = pure_f(a, b); + if (a > 0) + return x; + return a; +} + +/* We should sink the call to pure_f to the if block. */ +/* { dg-final { scan-tree-dump "Sinking # VUSE" "sink" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/pr65947-3.c b/gcc/testsuite/gcc.dg/vect/pr65947-3.c index 6b4077e1a62..8a2608cf0f1 100644 --- a/gcc/testsuite/gcc.dg/vect/pr65947-3.c +++ b/gcc/testsuite/gcc.dg/vect/pr65947-3.c @@ -51,6 +51,9 @@ main (void) return 0; } -/* { dg-final { scan-tree-dump-times "LOOP VECTORIZED" 2 "vect" } } */ +/* XFAILed because of the fix for PR97307 which sinks the load of a[i], preventing + if-conversion to happen. */ +/* { dg-final { scan-tree-dump-times "LOOP VECTORIZED" 2 "vect" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "LOOP VECTORIZED" 1 "vect" } } */ /* { dg-final { scan-tree-dump-times "optimizing condition reduction with FOLD_EXTRACT_LAST" 2 "vect" { target vect_fold_extract_last } } } */ /* { dg-final { scan-tree-dump-not "condition expression based on integer induction." "vect" } } */ diff --git a/gcc/tree-ssa-sink.c b/gcc/tree-ssa-sink.c index 4cc5195f2f8..ba8e5577e9c 100644 --- a/gcc/tree-ssa-sink.c +++ b/gcc/tree-ssa-sink.c @@ -369,10 +369,9 @@ statement_sink_location (gimple *stmt, basic_block frombb, return false; /* If this is a load then do not sink past any stores. - ??? This is overly simple but cheap. We basically look - for an existing load with the same VUSE in the path to one - of the sink candidate blocks and we adjust commondom to the - nearest to commondom. */ + Look for virtual definitions in the path from frombb to the sink + location computed from the real uses and if found, adjust + that it a common dominator. */ if (gimple_vuse (stmt)) { /* Do not sink loads from hard registers. */ @@ -383,29 +382,33 @@ statement_sink_location (gimple *stmt, basic_block frombb, imm_use_iterator imm_iter; use_operand_p use_p; - basic_block found = NULL; FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vuse (stmt)) { gimple *use_stmt = USE_STMT (use_p); basic_block bb = gimple_bb (use_stmt); - /* For PHI nodes the block we know sth about - is the incoming block with the use. */ + /* For PHI nodes the block we know sth about is the incoming block + with the use. */ if (gimple_code (use_stmt) == GIMPLE_PHI) - bb = EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src; - /* Any dominator of commondom would be ok with - adjusting commondom to that block. */ - bb = nearest_common_dominator (CDI_DOMINATORS, bb, commondom); - if (!found) - found = bb; - else if (dominated_by_p (CDI_DOMINATORS, bb, found)) - found = bb; - /* If we can't improve, stop. */ - if (found == commondom) - break; + { + /* In case the PHI node post-dominates the current insert location + we can disregard it. */ + if (commondom != bb + && dominated_by_p (CDI_POST_DOMINATORS, commondom, bb)) + continue; + bb = EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src; + } + else if (!gimple_vdef (use_stmt)) + continue; + /* If the use is not dominated by the path entry it is not on + the path. */ + if (!dominated_by_p (CDI_DOMINATORS, bb, frombb)) + continue; + /* There is no easy way to disregard defs not on the path from + frombb to commondom so just consider them all. */ + commondom = nearest_common_dominator (CDI_DOMINATORS, bb, commondom); + if (commondom == frombb) + return false; } - commondom = found; - if (commondom == frombb) - return false; } /* Our common dominator has to be dominated by frombb in order to be a