From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 033243846410 for ; Thu, 25 Apr 2024 13:19:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 033243846410 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 033243846410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714051198; cv=none; b=vmYjQvSj634YKAXlx92dTAUptV25sJz6GWJosde4vQ+0v4RoicKdpZF2HrIV4DHcXfp0R9p+mMgiBlczAUwkfEYihUvyhi0AyNgmsY/GaESiy7vcg34GzWQFaGBuGbWfoLmRchTEUTnXbUkPBOGtCHItefeHnBkDSohVHTVIDLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714051198; c=relaxed/simple; bh=D/mUgVU3CGBliybm1kZ3Vo8OTiA4FHU/yEFcWTwoBAk=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=MlcUWJgcp6V1XDOnm8AoQe0h3BFgSFo8xvFP8YGL8abjVbDZNr9d6aJLbCJeLHAs82/yuGgCtJnkIWxIebGMoIaCIZOPhMnuV4szIXgp/FkArIMIS4XR3FrzRcNvO+xq+yvNF7kx2aiyhfiQEbK1qdP5f+r6urZxCSfs8NxcxE0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (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 BF94533B9B for ; Thu, 25 Apr 2024 13:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714051195; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=rUfYULLgQfvXt6h9nZ7C9HK+nzEbBwlEJ60xAlVU1nA=; b=KRK3sBvY3ploh6FzXATFaPyNw+OsDnRaJ60sl6edgwx1CZl+QllkeBio3CvQG7zWUrPp5i rbqLT04YACrg/t5rnx1l9/3ZXP9Kpt0TprCOr7+bLSiz0VcXAGmNGF/PpKyPV9DgVaNN8D T0Tj+ZfyCU/+YoezW4sJbdFQ2AvBXCE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714051195; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=rUfYULLgQfvXt6h9nZ7C9HK+nzEbBwlEJ60xAlVU1nA=; b=QltEkL9sexKzh0pIfxeZcrRHwlwoQ1e//hypsG47Uj7pC01272Br3YOwCPPXQ39fL+Q2SR wWFQwEEilcIq9bCg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714051195; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=rUfYULLgQfvXt6h9nZ7C9HK+nzEbBwlEJ60xAlVU1nA=; b=KRK3sBvY3ploh6FzXATFaPyNw+OsDnRaJ60sl6edgwx1CZl+QllkeBio3CvQG7zWUrPp5i rbqLT04YACrg/t5rnx1l9/3ZXP9Kpt0TprCOr7+bLSiz0VcXAGmNGF/PpKyPV9DgVaNN8D T0Tj+ZfyCU/+YoezW4sJbdFQ2AvBXCE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714051195; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=rUfYULLgQfvXt6h9nZ7C9HK+nzEbBwlEJ60xAlVU1nA=; b=QltEkL9sexKzh0pIfxeZcrRHwlwoQ1e//hypsG47Uj7pC01272Br3YOwCPPXQ39fL+Q2SR wWFQwEEilcIq9bCg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B0DF41393C for ; Thu, 25 Apr 2024 13:19:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id BdCtJntYKmZmbgAAD6G6ig (envelope-from ) for ; Thu, 25 Apr 2024 13:19:55 +0000 Date: Thu, 25 Apr 2024 15:19:51 +0200 (CEST) From: Richard Biener X-X-Sender: rguenther@rguenther-XPS-13-9380 To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/114792 - order loops to unloops in CH User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII Message-Id: <20240425131955.B0DF41393C@imap1.dmz-prg2.suse.org> X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; RCVD_TLS_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+] X-Spam-Score: -4.30 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: When we use unloop_loops we have to make sure to have loops ordered inner to outer as otherwise we can wreck inner loop structure where unlooping relies on that being intact. The following re-sorts the vector of to unloop loops after copy-header as that adds to the vector in two places and the wrong order. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/114792 * tree-ssa-loop-ch.cc (ch_order_loops): New function. (ch_base::copy_headers): Sort loops to unloop inner-to-outer. * gcc.dg/torture/pr114792.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr114792.c | 16 ++++++++++++++++ gcc/tree-ssa-loop-ch.cc | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/torture/pr114792.c diff --git a/gcc/testsuite/gcc.dg/torture/pr114792.c b/gcc/testsuite/gcc.dg/torture/pr114792.c new file mode 100644 index 00000000000..4990e76eb2a --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr114792.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fno-tree-ccp -fno-tree-copy-prop" } */ + +int a; +int main() { + int b = -1, c; + for (; b >= 0; b++) { + for (c = 0; c; c++) { + L: + while (a) + if (a) + goto L; + } + } + return 0; +} diff --git a/gcc/tree-ssa-loop-ch.cc b/gcc/tree-ssa-loop-ch.cc index b7ef485c4cc..d7dd3e5459d 100644 --- a/gcc/tree-ssa-loop-ch.cc +++ b/gcc/tree-ssa-loop-ch.cc @@ -759,6 +759,21 @@ protected: bool process_loop_p (class loop *loop) final override; }; // class pass_ch_vect +/* Sort comparator to order loops after the specified order. */ + +static int +ch_order_loops (const void *a_, const void *b_, void *order_) +{ + int *order = (int *)order_; + const class loop *a = *(const class loop * const *)a_; + const class loop *b = *(const class loop * const *)b_; + if (a->num == b->num) + return 0; + if (order[a->num] < order[b->num]) + return -1; + return 1; +} + /* For all loops, copy the condition at the end of the loop body in front of the loop. This is beneficial since it increases efficiency of code motion optimizations. It also saves one jump on entry to the loop. */ @@ -1152,6 +1167,16 @@ ch_base::copy_headers (function *fun) } if (!loops_to_unloop.is_empty ()) { + /* Make sure loops are ordered inner to outer for unlooping. */ + if (loops_to_unloop.length () != 1) + { + auto_vec order; + order.safe_grow (number_of_loops (cfun), true); + int i = 0; + for (auto loop : loops_list (cfun, LI_FROM_INNERMOST)) + order[loop->num] = i++; + loops_to_unloop.sort (ch_order_loops, order.address ()); + } bool irred_invalidated; auto_bitmap lc_invalidated; auto_vec edges_to_remove; -- 2.25.1