From patchwork Fri Aug 27 07:51:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1521534 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: 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=HlLhVaMd; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 4GwsNv0JwFz9sPf for ; Fri, 27 Aug 2021 17:51:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6592C3857011 for ; Fri, 27 Aug 2021 07:51:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6592C3857011 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1630050699; bh=o8itznp3cE0gQHG7EZf2DS7LNluI4fyqjvljEcfeSuQ=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HlLhVaMdaXWIoNTJmD8rxKPojHbrmVY7RnsRX/LPYW4Xf7509EQsqOTMbJH1GBoy+ nZJhTJKEWgOSwxaf+HNtaGiDDYchmLEYAE6UWtUmBaK9ldptSqUHuTAwqXcY2SGpBi GeoaL1ngE6WvTvxx3U9rdPbMKhgtdqJw3IDPBULo= 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 CDA6B385842D for ; Fri, 27 Aug 2021 07:51:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CDA6B385842D 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 BD76D2210C for ; Fri, 27 Aug 2021 07:51:09 +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 A6E3113D5A for ; Fri, 27 Aug 2021 07:51:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MBefJ22ZKGF4LwAAMHmgww (envelope-from ) for ; Fri, 27 Aug 2021 07:51:09 +0000 Date: Fri, 27 Aug 2021 09:51:09 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/45178 - DCE of dead control flow in infinite loop Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 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 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" This fixes DCE to be able to elide dead control flow in an infinite loop without an exit edge. This special situation is handled well by the code finding an edge to preserve since there's no chance it will find the exit edge and make the loop finite. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2021-08-27 Richard Biener PR tree-optimization/45178 * tree-ssa-dce.c (find_obviously_necessary_stmts): For infinite loops without exit do not mark control dependent edges of the latch necessary. * gcc.dg/tree-ssa/ssa-dce-3.c: Adjust testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-3.c | 9 +++++---- gcc/tree-ssa-dce.c | 14 +++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-3.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-3.c index 863aa79b4eb..fdfe37e0abd 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-3.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-3.c @@ -21,11 +21,12 @@ int main(void) return 0; } -/* We now can prove the infiniteness of the loop during CCP and fail - to eliminate the code inside the infinite loop because we start - by marking the j % 7 condition as useful. See PR45178. */ +/* We now can prove the infiniteness of the loop during CCP but we + still want to eliminate the code inside the infinite loop. See PR45178. */ /* We should eliminate the inner condition, but the loop must be preserved - as it is infinite. Therefore there should be just one goto and no PHI. */ + as it is infinite. Therefore there should be just one goto and no PHI + and no if. */ /* { dg-final { scan-tree-dump-times "PHI " 0 "cddce1" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "cddce1" } } */ /* { dg-final { scan-tree-dump-times "goto" 1 "cddce1" } } */ diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c index 0778eb9704a..c4907af923c 100644 --- a/gcc/tree-ssa-dce.c +++ b/gcc/tree-ssa-dce.c @@ -414,7 +414,9 @@ find_obviously_necessary_stmts (bool aggressive) if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE)) return; - /* Prevent the empty possibly infinite loops from being removed. */ + /* Prevent the empty possibly infinite loops from being removed. This is + needed to make the logic in remove_dead_stmt work to identify the + correct edge to keep when removing a controlling condition. */ if (aggressive) { if (mark_irreducible_loops ()) @@ -426,17 +428,19 @@ find_obviously_necessary_stmts (bool aggressive) && (e->flags & EDGE_IRREDUCIBLE_LOOP)) { if (dump_file) - fprintf (dump_file, "Marking back edge of irreducible loop %i->%i\n", - e->src->index, e->dest->index); + fprintf (dump_file, "Marking back edge of irreducible " + "loop %i->%i\n", e->src->index, e->dest->index); mark_control_dependent_edges_necessary (e->dest, false); } } for (auto loop : loops_list (cfun, 0)) - if (!finite_loop_p (loop)) + /* For loops without an exit do not mark any condition. */ + if (loop->exits->next && !finite_loop_p (loop)) { if (dump_file) - fprintf (dump_file, "cannot prove finiteness of loop %i\n", loop->num); + fprintf (dump_file, "cannot prove finiteness of loop %i\n", + loop->num); mark_control_dependent_edges_necessary (loop->latch, false); } }