From patchwork Wed Feb 12 12:03:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 106370 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 013A1385840B for ; Wed, 12 Feb 2025 12:04:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 013A1385840B Authentication-Results: sourceware.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=KvtPQEN+; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=53g4YOci; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=Kwc3BmnN; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=s7ha0mGd 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 88D263858C52 for ; Wed, 12 Feb 2025 12:03:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88D263858C52 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 88D263858C52 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=1739361784; cv=none; b=OTiKiSe3bRVGf9Wq3axtAUWXDKfqk1F/DSw0o4It63erHVU2bMFXo6dO3iRB77d+TK99gVNYHZZrl6F5s+sfTX0HdmgXL8n0aBpL7ALZKJBd5A69gdkxlpUhzKxv4ivUizE/zyrDItQk0Z5kX0KPO0X8bu4C8sN3jOhNrSx+CXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739361784; c=relaxed/simple; bh=XPX45Q8aLhZiGX0RbSdyEBivnwoS2OpmoQleXJu3akw=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=xKLX7UtGuYYytMLInOUrpBUT3OsWBHF13aXqZhb+oPeeXayID4d4HUQ3lqzf2rj+5zC6eDAflrIhxWzuoRY59DjWvnNXS/9CiyMpe7/bq+Qy/eM4M3i7lnn6Zvhv161CGJOfcD3xs6H3T26K8xOI+u0tQALt1hIDvaMpS+28Ncs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88D263858C52 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 433481F831; Wed, 12 Feb 2025 12:03:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1739361783; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=CseOEbxUW3iCmjxcz8OMX9mLziRm10itT9RnHVvkVWs=; b=KvtPQEN+7ovFr+3rQR70xDjXG2a7mmljc6/gH88XteN/WMUchEd2obNPPK9a/AD8OJDxY4 zSTslrfAYpPwqeDilCicDED+IqEQivYOTcZn1WNDh/4kvbOTXdBkEHjPZC125OYnGn8K2H xUpIhO6CLrizx9wnb0Kklu4fc22i6WU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1739361783; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=CseOEbxUW3iCmjxcz8OMX9mLziRm10itT9RnHVvkVWs=; b=53g4YOci2zGDbQbxbUtVfC5sTyHoE8fhl6kQiIejQr096/Ee1K7ymKHINLQmEXhi77S9LH ebaD1yUA9wVLrvBQ== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1739361782; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=CseOEbxUW3iCmjxcz8OMX9mLziRm10itT9RnHVvkVWs=; b=Kwc3BmnNOFGnaedoXepkkh1fmMXjBgrHuUeSltz26gXq954QYAkvS/MCEK2XgqvKhiFZYm JlJ4Qz3Aqy+1eMiFC2gAgoFNgfiMeuNHFfsPMH2ebXFXKV+7MkIGGzATAyuHT6ClLjftFG 8izz42YhNahqIwB/cqYWG7aKFGvDK08= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1739361782; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=CseOEbxUW3iCmjxcz8OMX9mLziRm10itT9RnHVvkVWs=; b=s7ha0mGd+OlNx8ft6NoSFr0ihEZ8SyCvteFj446/GF83Wa0GBTRIWIYf3XHCuxhphTv3Af Dw5ITFs1JixIGuDA== Date: Wed, 12 Feb 2025 13:03:02 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: jeffreyalaw@gmail.com Subject: [PATCH] tree-optimization/86270 - improve SSA coalescing for loop exit test MIME-Version: 1.0 X-Spam-Score: -0.99 X-Spamd-Result: default: False [-0.99 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.995]; NEURAL_HAM_LONG(-0.19)[-0.187]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_ZERO(0.00)[0]; ARC_NA(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; MISSING_XM_UA(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FREEMAIL_CC(0.00)[gmail.com]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+] X-Spam-Level: X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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~patchwork=sourceware.org@gcc.gnu.org Message-Id: <20250212120401.013A1385840B@sourceware.org> The PR indicates a very specific issue with regard to SSA coalescing failures because there's a pre IV increment loop exit test. While IVOPTs created the desired IL we later simplify the exit test into the undesirable form again. The following fixes this up during RTL expansion where we try to improve coalescing of IVs. That seems easier that trying to avoid the simplification with some weird heuristics (it could also have been written this way). Bootstrapped and tested on x86_64-unknown-linux-gnu. OK for trunk? Thanks, Richard. PR tree-optimization/86270 * tree-outof-ssa.cc (insert_backedge_copies): Pattern match a single conflict in a loop condition and adjust that avoiding the conflict if possible. * gcc.target/i386/pr86270.c: Adjust to check for no reg-reg copies as well. --- gcc/testsuite/gcc.target/i386/pr86270.c | 3 ++ gcc/tree-outof-ssa.cc | 49 ++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/gcc/testsuite/gcc.target/i386/pr86270.c b/gcc/testsuite/gcc.target/i386/pr86270.c index 68562446fa4..89b9aeb317a 100644 --- a/gcc/testsuite/gcc.target/i386/pr86270.c +++ b/gcc/testsuite/gcc.target/i386/pr86270.c @@ -13,3 +13,6 @@ test () /* Check we do not split the backedge but keep nice loop form. */ /* { dg-final { scan-assembler-times "L\[0-9\]+:" 2 } } */ +/* Check we do not end up with reg-reg moves from a pre-increment IV + exit test. */ +/* { dg-final { scan-assembler-not "mov\[lq\]\?\t%\?\[er\].x, %\?\[er\].x" } } */ diff --git a/gcc/tree-outof-ssa.cc b/gcc/tree-outof-ssa.cc index d340d4ba529..f285c81599e 100644 --- a/gcc/tree-outof-ssa.cc +++ b/gcc/tree-outof-ssa.cc @@ -1259,10 +1259,9 @@ insert_backedge_copies (void) if (gimple_nop_p (def) || gimple_code (def) == GIMPLE_PHI) continue; - tree name = copy_ssa_name (result); - gimple *stmt = gimple_build_assign (name, result); imm_use_iterator imm_iter; gimple *use_stmt; + auto_vec uses; /* The following matches trivially_conflicts_p. */ FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, result) { @@ -1273,11 +1272,51 @@ insert_backedge_copies (void) { use_operand_p use; FOR_EACH_IMM_USE_ON_STMT (use, imm_iter) - SET_USE (use, name); + uses.safe_push (use); } } - gimple_stmt_iterator gsi = gsi_for_stmt (def); - gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); + /* When there is just a conflicting statement try to + adjust that to refer to the new definition. + In particular for now handle a conflict with the + use in a (exit) condition with a NE compare, + replacing a pre-IV-increment compare with a + post-IV-increment one. */ + if (uses.length () == 1 + && is_a (USE_STMT (uses[0])) + && gimple_cond_code (USE_STMT (uses[0])) == NE_EXPR + && is_gimple_assign (def) + && gimple_assign_rhs1 (def) == result + && (gimple_assign_rhs_code (def) == PLUS_EXPR + || gimple_assign_rhs_code (def) == MINUS_EXPR + || gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR) + && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST) + { + gcond *cond = as_a (USE_STMT (uses[0])); + tree *adj; + if (gimple_cond_lhs (cond) == result) + adj = gimple_cond_rhs_ptr (cond); + else + adj = gimple_cond_lhs_ptr (cond); + tree name = copy_ssa_name (result); + gimple *stmt + = gimple_build_assign (name, + gimple_assign_rhs_code (def), + *adj, gimple_assign_rhs2 (def)); + gimple_stmt_iterator gsi = gsi_for_stmt (cond); + gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); + *adj = name; + SET_USE (uses[0], arg); + update_stmt (cond); + } + else + { + tree name = copy_ssa_name (result); + gimple *stmt = gimple_build_assign (name, result); + gimple_stmt_iterator gsi = gsi_for_stmt (def); + gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); + for (auto use : uses) + SET_USE (use, name); + } } } }