From patchwork Tue Jun 25 13:31:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1952110 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=JpMEVT/y; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=TnWkHduA; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=JpMEVT/y; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=TnWkHduA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W7m2z6V0Nz20X6 for ; Tue, 25 Jun 2024 23:32:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F08B3870869 for ; Tue, 25 Jun 2024 13:32:10 +0000 (GMT) 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 [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 3E9AC386182A for ; Tue, 25 Jun 2024 13:31:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3E9AC386182A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3E9AC386182A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719322310; cv=none; b=a1ZCF6a+tmuJdEU3YYsauGMgDjjgJ1VF5c3utoR0dxtkDEf7SZOUmZZVNrMiiB2QKPPvXhGtVrJhUZIV7SdDwzcWwtKQ108U4r5tLUBuhnsjJb6XfhAylkzIgTEuE+qFFJ3CLy3h4pZy9hdakP44cQkz+p7rTdnAX7pkhARiBA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719322310; c=relaxed/simple; bh=fa4ni26IUk0VZvpecDMnMzDZgqrVTqPyaerv9fIFyKQ=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=Vfw9dCgArFjkvQZjF/Rh3PgJ3fAa1JcwhAa69Z+QDjEF1I1OzoYVs8qYW1J9+Y2Pe6++mdOtQQ4YmghcxNorVTfSPIWajXaThm6n5fWH3WpS+6cEif3aQ+/ts4QnpTohUX/KNcc8u/qPNLzzCC0r6ULVdyENiaUZRiWTfvt+z44= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from murzim.nue2.suse.org (unknown [10.168.4.243]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id 3A38C1F863 for ; Tue, 25 Jun 2024 13:31:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1719322307; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pk1g3qbE7oUu/67xRmngLJXdxsZjHU8tVZSRyV80+9Y=; b=JpMEVT/yuDRgfZa7UwU03aQ2oi0m3TJBdlaErHcs1F+zJEQxcBObl/ccDY3o6USdPtvrq3 1Ecxxz3dvfPdZU/PL7IamBKbANn+mGYPdPoHMB1cpCF/0ezG1WMDNAHXpCfK7eyfRk6+2W cinghQiqlsxugL3P37KyZrUasZUoPxY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1719322307; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pk1g3qbE7oUu/67xRmngLJXdxsZjHU8tVZSRyV80+9Y=; b=TnWkHduAnXDEA0M0N1q2MUJKqrjv3nFoC9SI/YDEoy5p5+PZvBfJ4qkwex4gsAHZBENVMS eKuePaJ7un61v0DA== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1719322307; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pk1g3qbE7oUu/67xRmngLJXdxsZjHU8tVZSRyV80+9Y=; b=JpMEVT/yuDRgfZa7UwU03aQ2oi0m3TJBdlaErHcs1F+zJEQxcBObl/ccDY3o6USdPtvrq3 1Ecxxz3dvfPdZU/PL7IamBKbANn+mGYPdPoHMB1cpCF/0ezG1WMDNAHXpCfK7eyfRk6+2W cinghQiqlsxugL3P37KyZrUasZUoPxY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1719322307; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pk1g3qbE7oUu/67xRmngLJXdxsZjHU8tVZSRyV80+9Y=; b=TnWkHduAnXDEA0M0N1q2MUJKqrjv3nFoC9SI/YDEoy5p5+PZvBfJ4qkwex4gsAHZBENVMS eKuePaJ7un61v0DA== Date: Tue, 25 Jun 2024 15:31:47 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/115629 - missed tail merging MIME-Version: 1.0 X-Spam-Score: 2.68 X-Spamd-Result: default: False [2.68 / 50.00]; NEURAL_SPAM_LONG(3.48)[0.995]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.996]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_ZERO(0.00)[0]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[] X-Spam-Status: No, score=-10.5 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Message-Id: <20240625133210.2F08B3870869@sourceware.org> The following fixes a missed tail-merging observed for the testcase in PR115629. The issue is that when deps_ok_for_redirect doesn't compute both would be valid prevailing blocks it rejects the merge. The following instead makes sure to record the working block as prevailing. Also stmt comparison fails for indirect references and is not handling memory references thoroughly, failing to unify array indices and pointers indirected. The following attempts to fix this. Bootstrapped and tested on x86_64-unknown-linux-gnu. I'll push tomorrow if there are no comments or failures reported from CI. PR tree-optimization/115629 * tree-ssa-tail-merge.cc (gimple_equal_p): Handle memory references better. (deps_ok_for_redirect): Handle the case not both blocks are considered a valid prevailing block. * gcc.dg/tree-ssa/tail-merge-1.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/tail-merge-1.c | 14 ++++ gcc/tree-ssa-tail-merge.cc | 69 +++++++++++++++++--- 2 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/tail-merge-1.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/tail-merge-1.c b/gcc/testsuite/gcc.dg/tree-ssa/tail-merge-1.c new file mode 100644 index 00000000000..e5670c33ba3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/tail-merge-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dce4" } */ + +void foo1 (int *restrict a, int *restrict b, int *restrict c, + int *restrict d, int *restrict res, int n) +{ + for (int i = 0; i < n; i++) + res[i] = a[i] ? b[i] : (c[i] ? b[i] : d[i]); +} + +/* After tail-merging (run during PRE) we should end up merging the two + blocks dereferencing 'b', ending up with two iftmp assigns and the + iftmp PHI def. */ +/* { dg-final { scan-tree-dump-times "iftmp\[^\r\n\]* = " 3 "dce4" } } */ diff --git a/gcc/tree-ssa-tail-merge.cc b/gcc/tree-ssa-tail-merge.cc index c8b4a79294d..27e7c6a37b2 100644 --- a/gcc/tree-ssa-tail-merge.cc +++ b/gcc/tree-ssa-tail-merge.cc @@ -1188,7 +1188,52 @@ gimple_equal_p (same_succ *same_succ, gimple *s1, gimple *s2) { t1 = gimple_arg (s1, i); t2 = gimple_arg (s2, i); - if (!gimple_operand_equal_value_p (t1, t2)) + while (handled_component_p (t1) && handled_component_p (t2)) + { + if (TREE_CODE (t1) != TREE_CODE (t2) + || TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2)) + return false; + switch (TREE_CODE (t1)) + { + case COMPONENT_REF: + if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1) + || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 2), + TREE_OPERAND (t2, 2))) + return false; + break; + case ARRAY_REF: + case ARRAY_RANGE_REF: + if (!gimple_operand_equal_value_p (TREE_OPERAND (t1, 3), + TREE_OPERAND (t2, 3))) + return false; + /* Fallthru. */ + case BIT_FIELD_REF: + if (!gimple_operand_equal_value_p (TREE_OPERAND (t1, 1), + TREE_OPERAND (t2, 1)) + || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 2), + TREE_OPERAND (t2, 2))) + return false; + break; + case REALPART_EXPR: + case IMAGPART_EXPR: + case VIEW_CONVERT_EXPR: + break; + default: + gcc_unreachable (); + } + t1 = TREE_OPERAND (t1, 0); + t2 = TREE_OPERAND (t2, 0); + } + if (TREE_CODE (t1) == MEM_REF && TREE_CODE (t2) == MEM_REF) + { + if (TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2) + || TYPE_ALIGN (TREE_TYPE (t1)) != TYPE_ALIGN (TREE_TYPE (t2)) + || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 0), + TREE_OPERAND (t2, 0)) + || TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) + return false; + } + else if (!gimple_operand_equal_value_p (t1, t2)) return false; } return true; @@ -1462,16 +1507,24 @@ deps_ok_for_redirect_from_bb_to_bb (basic_block from, basic_block to) replacement are dominates by their defs. */ static bool -deps_ok_for_redirect (basic_block bb1, basic_block bb2) +deps_ok_for_redirect (basic_block &bb1, basic_block &bb2) { - if (BB_CLUSTER (bb1) != NULL) - bb1 = BB_CLUSTER (bb1)->rep_bb; + basic_block b1 = bb1; + basic_block b2 = bb2; + if (BB_CLUSTER (b1) != NULL) + b1 = BB_CLUSTER (b1)->rep_bb; - if (BB_CLUSTER (bb2) != NULL) - bb2 = BB_CLUSTER (bb2)->rep_bb; + if (BB_CLUSTER (b2) != NULL) + b2 = BB_CLUSTER (b2)->rep_bb; - return (deps_ok_for_redirect_from_bb_to_bb (bb1, bb2) - && deps_ok_for_redirect_from_bb_to_bb (bb2, bb1)); + if (deps_ok_for_redirect_from_bb_to_bb (b1, b2)) + return true; + if (deps_ok_for_redirect_from_bb_to_bb (b2, b1)) + { + std::swap (bb1, bb2); + return true; + } + return false; } /* Within SAME_SUCC->bbs, find clusters of bbs which can be merged. */