From patchwork Tue Dec 7 13:07:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 48585 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 74B5F3858016 for ; Tue, 7 Dec 2021 13:07:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74B5F3858016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638882477; bh=0dVMr3aMr8VyL0F6Rv4Ju3SfYZR7/P8MfG+EA4s4Qfo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=U33NGWHQRk6qpMW1J+IzW6skc1p5yhBsEatL66qsy62aBhOB+AN6qIBhmVIumF62R 8eW47HBxy3MnNPQmiZRAFiNHm0LvuXG0Fox88DW0jp1ckJ6ZFYRUmi1jMy9F3nCNXn N6+7pjhi6eVxWnK7y4dkgWhE1OVrlcsKgMk2h25c= 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 9EF753858C60 for ; Tue, 7 Dec 2021 13:07:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9EF753858C60 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-out2.suse.de (Postfix) with ESMTPS id A341B1FE07 for ; Tue, 7 Dec 2021 13:07: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 8BD5E13A78 for ; Tue, 7 Dec 2021 13:07:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gp39II5cr2GzQAAAMHmgww (envelope-from ) for ; Tue, 07 Dec 2021 13:07:26 +0000 Date: Tue, 7 Dec 2021 14:07:26 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/103596 - fix missed propagation into switches Message-ID: <7966o545-4ps8-qsqn-4q75-os829nn1p5rr@fhfr.qr> MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 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 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" may_propagate_copy unnecessarily restricts propagating non-abnormals into places that currently contain an abnormal SSA name but are not the PHI argument for an abnormal edge. This causes VN to not elide a CFG path that it assumes is elided, resulting in released SSA names in the IL. The fix is to enhance the may_propagate_copy API to specify the destination is _not_ a PHI argument. I chose to not update only the relevant caller in VN and the may_propagate_copy_into_stmt API at this point because this is a regression and needs backporting. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2021-12-07 Richard Biener PR tree-optimization/103596 * tree-ssa-sccvn.c (eliminate_dom_walker::eliminate_stmt): Note we are not propagating into a PHI argument to may_propagate_copy. * tree-ssa-propagate.h (may_propagate_copy): Add argument specifying whether we propagate into a PHI arg. * tree-ssa-propagate.c (may_propagate_copy): Likewise. When not doing so we can replace an abnormal with something else. (may_propagate_into_stmt): Update may_propagate_copy calls. (replace_exp_1): Move propagation checking code to propagate_value and rename to ... (replace_exp): ... this and elide previous wrapper. (propagate_value): Perform checking with adjusted may_propagate_copy call and dispatch to replace_exp. * gcc.dg/torture/pr103596.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr103596.c | 36 ++++++++++++++ gcc/tree-ssa-propagate.c | 62 ++++++++++--------------- gcc/tree-ssa-propagate.h | 2 +- gcc/tree-ssa-sccvn.c | 2 +- 4 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr103596.c diff --git a/gcc/testsuite/gcc.dg/torture/pr103596.c b/gcc/testsuite/gcc.dg/torture/pr103596.c new file mode 100644 index 00000000000..4f65c36d121 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr103596.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ +/* { dg-additional-options "--param case-values-threshold=1" } */ + +int n; + +void +qux (int a) +{ +} + +int +baz (void) +{ + return -1; +} + +__attribute__ ((returns_twice)) int +bar (int b) +{ + if (n != 0) + { + if (b != 2) + if (b != 0) + return n + b; + + if (n == 2) + return 0; + } +} + +void +foo (void) +{ + qux (n); + bar (baz ()); +} diff --git a/gcc/tree-ssa-propagate.c b/gcc/tree-ssa-propagate.c index 6d19410caa8..1f2a17f73c9 100644 --- a/gcc/tree-ssa-propagate.c +++ b/gcc/tree-ssa-propagate.c @@ -1042,10 +1042,12 @@ substitute_and_fold_engine::substitute_and_fold (basic_block block) } -/* Return true if we may propagate ORIG into DEST, false otherwise. */ +/* Return true if we may propagate ORIG into DEST, false otherwise. + If DEST_NOT_PHI_ARG_P is true then assume the propagation does + not happen into a PHI argument which relaxes some constraints. */ bool -may_propagate_copy (tree dest, tree orig) +may_propagate_copy (tree dest, tree orig, bool dest_not_phi_arg_p) { tree type_d = TREE_TYPE (dest); tree type_o = TREE_TYPE (orig); @@ -1065,8 +1067,10 @@ may_propagate_copy (tree dest, tree orig) && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)) return false; /* Similarly if DEST flows in from an abnormal edge then the copy cannot be - propagated. */ - else if (TREE_CODE (dest) == SSA_NAME + propagated. If we know we do not propagate into a PHI argument this + does not apply. */ + else if (!dest_not_phi_arg_p + && TREE_CODE (dest) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest)) return false; @@ -1100,9 +1104,9 @@ may_propagate_copy_into_stmt (gimple *dest, tree orig) for the expression, so we delegate to may_propagate_copy. */ if (gimple_assign_single_p (dest)) - return may_propagate_copy (gimple_assign_rhs1 (dest), orig); + return may_propagate_copy (gimple_assign_rhs1 (dest), orig, true); else if (gswitch *dest_swtch = dyn_cast (dest)) - return may_propagate_copy (gimple_switch_index (dest_swtch), orig); + return may_propagate_copy (gimple_switch_index (dest_swtch), orig, true); /* In other cases, the expression is not materialized, so there is no destination to pass to may_propagate_copy. On the other @@ -1140,25 +1144,19 @@ may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED) } -/* Common code for propagate_value and replace_exp. +/* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME). - Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the - replacement is done to propagate a value or not. */ + Use this version when not const/copy propagating values. For example, + PRE uses this version when building expressions as they would appear + in specific blocks taking into account actions of PHI nodes. -static void -replace_exp_1 (use_operand_p op_p, tree val, - bool for_propagation ATTRIBUTE_UNUSED) -{ - if (flag_checking) - { - tree op = USE_FROM_PTR (op_p); - gcc_assert (!(for_propagation - && TREE_CODE (op) == SSA_NAME - && TREE_CODE (val) == SSA_NAME - && !may_propagate_copy (op, val))); - } + The statement in which an expression has been replaced should be + folded using fold_stmt_inplace. */ - if (TREE_CODE (val) == SSA_NAME) +void +replace_exp (use_operand_p op_p, tree val) +{ + if (TREE_CODE (val) == SSA_NAME || CONSTANT_CLASS_P (val)) SET_USE (op_p, val); else SET_USE (op_p, unshare_expr (val)); @@ -1174,22 +1172,10 @@ replace_exp_1 (use_operand_p op_p, tree val, void propagate_value (use_operand_p op_p, tree val) { - replace_exp_1 (op_p, val, true); -} - -/* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME). - - Use this version when not const/copy propagating values. For example, - PRE uses this version when building expressions as they would appear - in specific blocks taking into account actions of PHI nodes. - - The statement in which an expression has been replaced should be - folded using fold_stmt_inplace. */ - -void -replace_exp (use_operand_p op_p, tree val) -{ - replace_exp_1 (op_p, val, false); + if (flag_checking) + gcc_assert (may_propagate_copy (USE_FROM_PTR (op_p), val, + !is_a (USE_STMT (op_p)))); + replace_exp (op_p, val); } diff --git a/gcc/tree-ssa-propagate.h b/gcc/tree-ssa-propagate.h index 5257fbb0cf8..87a94ad17c3 100644 --- a/gcc/tree-ssa-propagate.h +++ b/gcc/tree-ssa-propagate.h @@ -65,7 +65,7 @@ enum ssa_prop_result { extern void move_ssa_defining_stmt_for_defs (gimple *, gimple *); extern bool stmt_makes_single_store (gimple *); -extern bool may_propagate_copy (tree, tree); +extern bool may_propagate_copy (tree, tree, bool = false); extern bool may_propagate_copy_into_stmt (gimple *, tree); extern bool may_propagate_copy_into_asm (tree); extern void propagate_value (use_operand_p, tree); diff --git a/gcc/tree-ssa-sccvn.c b/gcc/tree-ssa-sccvn.c index d31bf329d2e..16c93d1be78 100644 --- a/gcc/tree-ssa-sccvn.c +++ b/gcc/tree-ssa-sccvn.c @@ -6607,7 +6607,7 @@ eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi) at the definition are also available at uses. */ sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use); if (sprime && sprime != use - && may_propagate_copy (use, sprime) + && may_propagate_copy (use, sprime, true) /* We substitute into debug stmts to avoid excessive debug temporaries created by removed stmts, but we need to avoid doing so for inserted sprimes as we never want