From patchwork Tue Feb 15 12:22:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1593065 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=YfWL4LN9; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JygG06jg7z9sFh for ; Tue, 15 Feb 2022 23:22:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A13D385802D for ; Tue, 15 Feb 2022 12:22:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A13D385802D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644927749; bh=HsEUYh2Dygoh79+CMxZGd2b01obXooC6lsAS0fyaYEE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YfWL4LN9IjmWbsWE1fBPC7aL+2khYsUYm4yV6ZTpQbQ5Zl2GoGt+uh4Dss3ub0l9+ hX4HrClfza2RCdEGhsOIIFPZD+lWuYTl6G4wFOt67wmRypp9FOYqyvUvcYtRgSpqoN J9lsYbV4khXiYEMovl7yDpgN4od2+eVeg2F4gMPk= 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 [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 4927E3858D20 for ; Tue, 15 Feb 2022 12:22:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4927E3858D20 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-out1.suse.de (Postfix) with ESMTPS id 26CBD210E0 for ; Tue, 15 Feb 2022 12:22:08 +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 13FED13C73 for ; Tue, 15 Feb 2022 12:22:08 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id g0ujA/CaC2LNWwAAMHmgww (envelope-from ) for ; Tue, 15 Feb 2022 12:22:08 +0000 Date: Tue, 15 Feb 2022 13:22:07 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/104543 - fix unroll-and-jam precondition MIME-Version: 1.0 Message-Id: <20220215122208.13FED13C73@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" We have to make sure that outer loop exits come after the inner loop since we otherwise will put it into the fused loop body. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2022-02-15 Richard Biener PR tree-optimization/104543 * gimple-loop-jam.cc (unroll_jam_possible_p): Check outer loop exits come after the inner loop. * gcc.dg/torture/pr104543.c: New testcase. --- gcc/gimple-loop-jam.cc | 10 ++++++++-- gcc/testsuite/gcc.dg/torture/pr104543.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr104543.c diff --git a/gcc/gimple-loop-jam.cc b/gcc/gimple-loop-jam.cc index d9a77727665..e33dd9091df 100644 --- a/gcc/gimple-loop-jam.cc +++ b/gcc/gimple-loop-jam.cc @@ -198,7 +198,8 @@ unroll_jam_possible_p (class loop *outer, class loop *loop) if (!empty_block_p (loop->latch)) return false; - if (!single_exit (loop)) + edge exit; + if (!(exit = single_exit (loop))) return false; /* We need a perfect nest. Quick check for adjacent inner loops. */ @@ -259,7 +260,12 @@ unroll_jam_possible_p (class loop *outer, class loop *loop) n = get_loop_body_with_size (outer, bbs, n_basic_blocks_for_fn (cfun)); for (i = 0; i < n; i++) - if (bbs[i]->loop_father == outer && bb_prevents_fusion_p (bbs[i])) + if (bbs[i]->loop_father == outer + && (bb_prevents_fusion_p (bbs[i]) + /* Outer loop exits must come after the inner loop, otherwise + we'll put the outer loop exit into the fused inner loop. */ + || (loop_exits_from_bb_p (outer, bbs[i]) + && !dominated_by_p (CDI_DOMINATORS, bbs[i], exit->src)))) break; free (bbs); if (i != n) diff --git a/gcc/testsuite/gcc.dg/torture/pr104543.c b/gcc/testsuite/gcc.dg/torture/pr104543.c new file mode 100644 index 00000000000..48963514f77 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr104543.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-additional-options "-floop-unroll-and-jam" } */ + +int a[3], b, c; +static int e() +{ + if (!c) { + for (b = 0; b < 3; b++) + for (c = 0; c < 3; c++) + a[c] ^= 1; + return -1; + } + return 0; +} +int main() +{ + e(); + if (a[1] != 1) + __builtin_abort(); + return 0; +}