From patchwork Fri Apr 8 12:08:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 52736 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99C093853809 for ; Fri, 8 Apr 2022 12:08:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99C093853809 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649419737; bh=ysLhhOCQv/zvHGGRWKze2PWlZAOdxUwQPA+mk8gVKOg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=XnuxOHdW8R3L2wyVoZdAVhVBUl2WM2NyYyjG/NQ51EvKhD8kLp3lsDT40IgUBE56z /AXnK//E7a39Z16b3ziJgQNpryf2MuY6NebG0SEc82dYlC+XzVMtME8DylTsuDpf8j /eMNCrDTI8M9+h8nDj931phFlS/oEVdeSZEPROVM= 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 [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id C2C473858D28 for ; Fri, 8 Apr 2022 12:08:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2C473858D28 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4155D210F5 for ; Fri, 8 Apr 2022 12:08:26 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 2E25D132B9 for ; Fri, 8 Apr 2022 12:08:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id nqAICrolUGLgCgAAMHmgww (envelope-from ) for ; Fri, 08 Apr 2022 12:08:26 +0000 Date: Fri, 8 Apr 2022 14:08:25 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/105198 - wrong code with predictive commoning MIME-Version: 1.0 Message-Id: <20220408120826.2E25D132B9@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When predictive commoning looks for a looparound PHI it tries to match the entry value definition (a load) up with the appropriate member of the chain. But it fails to consider stmts clobbering the very same memory location inbetween the load and loop entry. In theory we could be more clever on must aliases that would be also picked up from a load (so not exactly stmt_kills_ref_p) and use the stored value from that if it is an exact match. But we currently have no way to propagate this information inside predcom. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2022-04-08 Richard Biener PR tree-optimization/105198 * tree-predcom.c (find_looparound_phi): Check whether the found memory location of the entry value is clobbered inbetween the value we want to use and loop entry. * gcc.dg/torture/pr105198.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr105198.c | 26 +++++++++++++++++ gcc/tree-predcom.cc | 38 +++++++++++++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr105198.c diff --git a/gcc/testsuite/gcc.dg/torture/pr105198.c b/gcc/testsuite/gcc.dg/torture/pr105198.c new file mode 100644 index 00000000000..91f92afc163 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr105198.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fno-tree-pre -fpredictive-commoning" } */ + +static __attribute__ ((noipa)) void +next_set(int *x, int n, int k) +{ + int j = k - 1; + int tmp = x[j]++; + while (j > 0) + { + if (x[j] < n - (k - 1 -j)) + break; + j--; + x[j]++; + tmp = x[j]; + } + if (tmp != 2 || j != 1 || x[0] != 0 || x[1] != 2 || x[2] != 5) + __builtin_abort (); +} + +int main() +{ + int x[3] = {0, 1, 4}; + next_set(x, 5, 3); + return 0; +} diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc index bb3a1cb68fc..fb457250bbd 100644 --- a/gcc/tree-predcom.cc +++ b/gcc/tree-predcom.cc @@ -1377,7 +1377,6 @@ gphi * pcom_worker::find_looparound_phi (dref ref, dref root) { tree name, init, init_ref; - gphi *phi = NULL; gimple *init_stmt; edge latch = loop_latch_edge (m_loop); struct data_reference init_dr; @@ -1395,14 +1394,19 @@ pcom_worker::find_looparound_phi (dref ref, dref root) if (!name) return NULL; + tree entry_vuse = NULL_TREE; + gphi *phi = NULL; for (psi = gsi_start_phis (m_loop->header); !gsi_end_p (psi); gsi_next (&psi)) { - phi = psi.phi (); - if (PHI_ARG_DEF_FROM_EDGE (phi, latch) == name) + gphi *p = psi.phi (); + if (PHI_ARG_DEF_FROM_EDGE (p, latch) == name) + phi = p; + else if (virtual_operand_p (gimple_phi_result (p))) + entry_vuse = PHI_ARG_DEF_FROM_EDGE (p, loop_preheader_edge (m_loop)); + if (phi && entry_vuse) break; } - - if (gsi_end_p (psi)) + if (!phi || !entry_vuse) return NULL; init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (m_loop)); @@ -1430,6 +1434,30 @@ pcom_worker::find_looparound_phi (dref ref, dref root) if (!valid_initializer_p (&init_dr, ref->distance + 1, root->ref)) return NULL; + /* Make sure nothing clobbers the location we re-use the initial value + from. */ + if (entry_vuse != gimple_vuse (init_stmt)) + { + ao_ref ref; + ao_ref_init (&ref, init_ref); + unsigned limit = param_sccvn_max_alias_queries_per_access; + tree vdef = entry_vuse; + do + { + gimple *def = SSA_NAME_DEF_STMT (vdef); + if (limit-- == 0 || gimple_code (def) == GIMPLE_PHI) + return NULL; + if (stmt_may_clobber_ref_p_1 (def, &ref)) + /* When the stmt is an assign to init_ref we could in theory + use its RHS for the initial value of the looparound PHI + we replace in prepare_initializers_chain, but we have + no convenient place to store this info at the moment. */ + return NULL; + vdef = gimple_vuse (def); + } + while (vdef != gimple_vuse (init_stmt)); + } + return phi; }