From patchwork Wed Apr 6 11:15:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 52670 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 641293858016 for ; Wed, 6 Apr 2022 11:15:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 641293858016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649243751; bh=s2GlMNl4fMh2oxE8jKVVylYtVTq6GeQH3fGxhAvihpY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pBqSrBk6Cy1iAsq/AWCV3EFyk5OpBCdMLdbJGzOGzBT5Kp6MUOQDDwNvmBj3yzeMP Xdr/LRINsxFQcsMsxianWyMJlqB6HrrjFvTjW/RONAeIzFclhQyw/EFejwKbJ6RwnT nAupZlZc+KBgKZnVx2LGdfPHDG7+XrFgTbwqfGCE= 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 20A6F3858C54 for ; Wed, 6 Apr 2022 11:15:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 20A6F3858C54 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 F1BF21F85A for ; Wed, 6 Apr 2022 11:15:19 +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 DF519139F5 for ; Wed, 6 Apr 2022 11:15:19 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id OXVkNUd2TWJIPQAAMHmgww (envelope-from ) for ; Wed, 06 Apr 2022 11:15:19 +0000 Date: Wed, 6 Apr 2022 13:15:19 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/105173 - fix insertion logic in reassoc MIME-Version: 1.0 Message-Id: <20220406111519.DF519139F5@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" The find_insert_point logic around deciding whether to insert before or after the found insertion point does not handle the case of _12 = ..;, _12, 1.0 well. The following puts the logic into find_insert_point itself instead. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2022-04-06 Richard Biener PR tree-optimization/105173 * tree-ssa-reassoc.cc (find_insert_point): Get extra insert_before output argument and compute it. (insert_stmt_before_use): Adjust. (rewrite_expr_tree): Likewise. * gcc.dg/pr105173.c: New testcase. --- gcc/testsuite/gcc.dg/pr105173.c | 12 ++++++++++ gcc/tree-ssa-reassoc.cc | 41 +++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr105173.c diff --git a/gcc/testsuite/gcc.dg/pr105173.c b/gcc/testsuite/gcc.dg/pr105173.c new file mode 100644 index 00000000000..3effb2996b0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr105173.c @@ -0,0 +1,12 @@ +/* { dg-do compile { target dfp } } */ +/* { dg-options "-Ofast" } */ + +int i; + +int +foo(char c, _Decimal32 d) +{ + d *= i; + d *= -(_Decimal64)c; + return d; +} diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index 0d55fc7e2d8..4ab3c3361f9 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -5185,17 +5185,26 @@ swap_ops_for_binary_stmt (const vec &ops, } /* If definition of RHS1 or RHS2 dominates STMT, return the later of those - two definitions, otherwise return STMT. */ + two definitions, otherwise return STMT. Sets INSERT_BEFORE to indicate + whether RHS1 op RHS2 can be inserted before or needs to be inserted + after the returned stmt. */ static inline gimple * -find_insert_point (gimple *stmt, tree rhs1, tree rhs2) +find_insert_point (gimple *stmt, tree rhs1, tree rhs2, bool &insert_before) { + insert_before = true; if (TREE_CODE (rhs1) == SSA_NAME && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs1))) - stmt = SSA_NAME_DEF_STMT (rhs1); + { + stmt = SSA_NAME_DEF_STMT (rhs1); + insert_before = false; + } if (TREE_CODE (rhs2) == SSA_NAME && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs2))) - stmt = SSA_NAME_DEF_STMT (rhs2); + { + stmt = SSA_NAME_DEF_STMT (rhs2); + insert_before = false; + } return stmt; } @@ -5207,7 +5216,8 @@ insert_stmt_before_use (gimple *stmt, gimple *stmt_to_insert) gcc_assert (is_gimple_assign (stmt_to_insert)); tree rhs1 = gimple_assign_rhs1 (stmt_to_insert); tree rhs2 = gimple_assign_rhs2 (stmt_to_insert); - gimple *insert_point = find_insert_point (stmt, rhs1, rhs2); + bool insert_before; + gimple *insert_point = find_insert_point (stmt, rhs1, rhs2, insert_before); gimple_stmt_iterator gsi = gsi_for_stmt (insert_point); gimple_set_uid (stmt_to_insert, gimple_uid (insert_point)); @@ -5215,7 +5225,7 @@ insert_stmt_before_use (gimple *stmt, gimple *stmt_to_insert) the point where operand rhs1 or rhs2 is defined. In this case, stmt_to_insert has to be inserted afterwards. This would only happen when the stmt insertion point is flexible. */ - if (stmt == insert_point) + if (insert_before) gsi_insert_before (&gsi, stmt_to_insert, GSI_NEW_STMT); else insert_stmt_after (stmt_to_insert, insert_point); @@ -5275,22 +5285,25 @@ rewrite_expr_tree (gimple *stmt, enum tree_code rhs_code, unsigned int opindex, return lhs), force creation of a new SSA_NAME. */ if (changed || ((rhs1 != oe2->op || rhs2 != oe1->op) && opindex)) { + bool insert_before; gimple *insert_point - = find_insert_point (stmt, oe1->op, oe2->op); + = find_insert_point (stmt, oe1->op, oe2->op, insert_before); lhs = make_ssa_name (TREE_TYPE (lhs)); stmt = gimple_build_assign (lhs, rhs_code, oe1->op, oe2->op); gimple_set_uid (stmt, uid); gimple_set_visited (stmt, true); - if (insert_point == gsi_stmt (gsi)) + if (insert_before) gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); else insert_stmt_after (stmt, insert_point); } else { - gcc_checking_assert (find_insert_point (stmt, oe1->op, oe2->op) + bool insert_before; + gcc_checking_assert (find_insert_point (stmt, oe1->op, oe2->op, + insert_before) == stmt); gimple_assign_set_rhs1 (stmt, oe1->op); gimple_assign_set_rhs2 (stmt, oe2->op); @@ -5346,21 +5359,25 @@ rewrite_expr_tree (gimple *stmt, enum tree_code rhs_code, unsigned int opindex, { gimple_stmt_iterator gsi = gsi_for_stmt (stmt); unsigned int uid = gimple_uid (stmt); - gimple *insert_point = find_insert_point (stmt, new_rhs1, oe->op); + bool insert_before; + gimple *insert_point = find_insert_point (stmt, new_rhs1, oe->op, + insert_before); lhs = make_ssa_name (TREE_TYPE (lhs)); stmt = gimple_build_assign (lhs, rhs_code, new_rhs1, oe->op); gimple_set_uid (stmt, uid); gimple_set_visited (stmt, true); - if (insert_point == gsi_stmt (gsi)) + if (insert_before) gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); else insert_stmt_after (stmt, insert_point); } else { - gcc_checking_assert (find_insert_point (stmt, new_rhs1, oe->op) + bool insert_before; + gcc_checking_assert (find_insert_point (stmt, new_rhs1, oe->op, + insert_before) == stmt); gimple_assign_set_rhs1 (stmt, new_rhs1); gimple_assign_set_rhs2 (stmt, oe->op);