From patchwork Mon Nov 11 12:02:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 2009790 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=sqQccFtp; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=IMhu6o4/; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=sqQccFtp; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=IMhu6o4/; 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 4Xn7VL2Yjyz1xty for ; Mon, 11 Nov 2024 23:03:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 378143858D28 for ; Mon, 11 Nov 2024 12:03:20 +0000 (GMT) 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 [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 6F80F3858D21 for ; Mon, 11 Nov 2024 12:02:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F80F3858D21 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 6F80F3858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731326581; cv=none; b=MhmYlXHvFYlMWz96kJkABqZA6QYQE8/fRAYQNdOVLEkUemD8vz83cWaRCl3DyTCMuzy6bCO9XR/DZEtPS0WMbY7SeIewlAHrwoeJiIcdSD6U3qH2qehPX7ZK+vMM8WyZ9Tncd137sPIE0+mIEqTjSidmDHT3P71Tjz8d0sIvKjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731326581; c=relaxed/simple; bh=gfCBpd5jPmShurJEACFQvX7SfF4Nlgt6rXYNGuc2sVs=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=pHUyiGGG1jQSl0q0+LfafszvyRuSl7KvPl6Q8Lc0wDWgeC1XVOU401Raj5ANxNTQG8SKmGjcMc1U81dzWfByevmzNavELvShfHUtWNwIXTD6BJW+COhah+FYWUhTyBTZu0BFHOTzWltyyZFANm3tYiuTjM80iyog3vCHrXmop3w= 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-out1.suse.de (Postfix) with ESMTPS id 1C188219A1 for ; Mon, 11 Nov 2024 12:02:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1731326578; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=5a/LCnz+1Jw0nHbPrqfXA0r3QoL8Cx8I2q6AhIMbNvM=; b=sqQccFtpAkHWOjVVeXsKjEO1r17xu0nTYNBjWG8aqtzwDblhfuXdkWj7oy1GQv5+0i5nXy hoUjA+q6qJed/Dj9f7kOgcwVuRJGeiA/QstXHN39SJYtcLOf/tW4x10hhZEIWnYvtFQHGm RiOXGfQ7Aq49Hx4d37W6dNp2axKhs4M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1731326578; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=5a/LCnz+1Jw0nHbPrqfXA0r3QoL8Cx8I2q6AhIMbNvM=; b=IMhu6o4/nqRaGq+Df5PuVEBmTXNa4ktVIlBB4usc9+um0xtIl322il6jEz7MspK2oHI4Od ItwhKPpJ6AnKlZAA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1731326578; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=5a/LCnz+1Jw0nHbPrqfXA0r3QoL8Cx8I2q6AhIMbNvM=; b=sqQccFtpAkHWOjVVeXsKjEO1r17xu0nTYNBjWG8aqtzwDblhfuXdkWj7oy1GQv5+0i5nXy hoUjA+q6qJed/Dj9f7kOgcwVuRJGeiA/QstXHN39SJYtcLOf/tW4x10hhZEIWnYvtFQHGm RiOXGfQ7Aq49Hx4d37W6dNp2axKhs4M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1731326578; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=5a/LCnz+1Jw0nHbPrqfXA0r3QoL8Cx8I2q6AhIMbNvM=; b=IMhu6o4/nqRaGq+Df5PuVEBmTXNa4ktVIlBB4usc9+um0xtIl322il6jEz7MspK2oHI4Od ItwhKPpJ6AnKlZAA== Date: Mon, 11 Nov 2024 13:02:58 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/117510 - fix guard hoisting validity check MIME-Version: 1.0 X-Spamd-Result: default: False [-1.12 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_LONG(-0.32)[-0.319]; NEURAL_HAM_SHORT(-0.20)[-0.995]; 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-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: <20241111120320.378143858D28@sourceware.org> For the loop in the testcase we currently fail to hoist the guard check of the inner loop (m > 0) out of the outer loop because find_loop_guard checks all blocks of the outer loop for side-effects, including those that are skipped by the guard. This usually is harmless as the guard does not skip any blocks in the outer loop but in this case store-motion was applied to the inner loop and thus there's now a skipped store in the outer loop. The following properly skips blocks that are dominated by the entry to the skipped region. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/117510 * tree-ssa-loop-unswitch.cc (find_loop_guard): Only check not skipped blocks for side-effects. * gcc.dg/vect/vect-outer-pr117510.c: New testcase. --- gcc/testsuite/gcc.dg/vect/vect-outer-pr117510.c | 13 +++++++++++++ gcc/tree-ssa-loop-unswitch.cc | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/vect/vect-outer-pr117510.c diff --git a/gcc/testsuite/gcc.dg/vect/vect-outer-pr117510.c b/gcc/testsuite/gcc.dg/vect/vect-outer-pr117510.c new file mode 100644 index 00000000000..e50b67ce040 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/vect-outer-pr117510.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_double } */ +/* { dg-additional-options "-O3" } */ + +void f(int n, int m, double *a) +{ + a = __builtin_assume_aligned (a, __BIGGEST_ALIGNMENT__); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + a[i] += 2*a[i] + j; +} + +/* { dg-final { scan-tree-dump "OUTER LOOP VECTORIZED" "vect" } } */ diff --git a/gcc/tree-ssa-loop-unswitch.cc b/gcc/tree-ssa-loop-unswitch.cc index 847f7ac739f..88516fdb0a1 100644 --- a/gcc/tree-ssa-loop-unswitch.cc +++ b/gcc/tree-ssa-loop-unswitch.cc @@ -1256,7 +1256,11 @@ find_loop_guard (class loop *loop, vec &dbg_to_reset) guard_edge = NULL; goto end; } - if (!empty_bb_without_guard_p (loop, bb, dbg_to_reset)) + /* If any of the not skipped blocks has side-effects or defs with + uses outside of the loop we cannot hoist the guard. */ + if (!dominated_by_p (CDI_DOMINATORS, + bb, guard_edge == te ? fe->dest : te->dest) + && !empty_bb_without_guard_p (loop, bb, dbg_to_reset)) { if (dump_enabled_p ()) dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,