From patchwork Thu Jan 11 13:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1885615 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=gvOtpina; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=+QWPaJdD; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=Ajk0EUgF; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=b1WicObd; 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 4T9mHt1h2rz1yPp for ; Fri, 12 Jan 2024 00:49:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DE1D385782B for ; Thu, 11 Jan 2024 13:49:44 +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 [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 2F87D385800A for ; Thu, 11 Jan 2024 13:49:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F87D385800A 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 2F87D385800A 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=1704980964; cv=none; b=dKyroiLAA6AOArV90qubTDVajuCFYFWHQ1TGRIjx4KNgUAMoMRN7PAwK/IjgUxqVq5m5SYn2y6Vr3fXBEPXI5gfIGmN0sC60QCS6t3yZELBwmKXopBzwi4812vywJbMpfAYtvIw0rUVk4L7Qjg5sRIgN4v2LHnAJVCCcM6i+0VU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704980964; c=relaxed/simple; bh=4Dpm314GRlUuuDMI/Gh80NVGwtN2sFJSwHv4vNRuDME=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=qmU5NNH3rqR0+oY585blTjADuvnWArKA6ZdYDHQQ19rGsIGU0Qr3LHZp4EvZJW5XajppZnvew/jSUPrJARRWuyebXriY0JOzmxqBwRoQq4pMP8yw2QkYzcvhUKaVYAdU1fv+8Y+c3Q/1grte11ryhN3/AUsoOX4mof6Kwarjbnc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (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 0BD7121E42 for ; Thu, 11 Jan 2024 13:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1704980961; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=sKtnJvOJedNno2oLf1TBIxjcIfLcTkiuewbncbfM+oc=; b=gvOtpinaTsUue0TrrKknh3Q3ZmJ9wg6mvJPt7ALEVD/y3aCfffiCdEtj5n8WdcxhrqoVm2 c/GvxBGnxfDykfRPtMlvTpfyiRc4l6VEWdFRJKqgjkngQz2ZoqIn0WaqXGz/jZ+Al6etCJ yL8Pfu6S3FEGNpdyRYNSU+5t9SMNd+o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1704980961; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=sKtnJvOJedNno2oLf1TBIxjcIfLcTkiuewbncbfM+oc=; b=+QWPaJdDNyQnO/RAfP3e7W0RedRnl+Xpg7SyuWH1BRKOEVZj592LkuF2C+BEoVoDGBi76E Lwbk7R0eo98Ic1CQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1704980960; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=sKtnJvOJedNno2oLf1TBIxjcIfLcTkiuewbncbfM+oc=; b=Ajk0EUgFruEzPuhzQiYAvpxCgdFwEIOEbRAT/JrRwXVVt4kgq71l4NTJWy0933vCLbYVtb cagCgmPQ6x8pG8mTmcgbnv4354patf4bqHtbJunzj8g69dTC3tPOjB6XX0qwtat8JAPV+x NUp3EjsAlqIQYPiUgd1IbULDyDREKg8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1704980960; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=sKtnJvOJedNno2oLf1TBIxjcIfLcTkiuewbncbfM+oc=; b=b1WicObdATAju9VjW15PMtVqygFq7O2oZfHsvPCNn9CRVvCOknJbceGYhOWNN58n8wGiJz A30wIZwW05R8xyAw== Date: Thu, 11 Jan 2024 14:44:18 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112636 - estimate niters before header copying MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-0.60 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[tree-ssa-loop-ch.cc:url]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Score: -0.60 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Message-Id: <20240111134944.2DE1D385782B@sourceware.org> The following avoids a mismatch between an early query for maximum number of iterations of a loop and a late one when through ranger we'd get iterations estimated. Instead make sure we compute niters before querying the iteration bound. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112636 * tree-ssa-loop-ch.cc (ch_base::copy_headers): Call estimate_numbers_of_iterations before querying get_max_loop_iterations_int. (pass_ch::execute): Initialize SCEV and loops appropriately. * gcc.dg/pr112636.c: New testcase. --- gcc/testsuite/gcc.dg/pr112636.c | 13 +++++++++++++ gcc/tree-ssa-loop-ch.cc | 25 ++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr112636.c diff --git a/gcc/testsuite/gcc.dg/pr112636.c b/gcc/testsuite/gcc.dg/pr112636.c new file mode 100644 index 00000000000..284ae8f5e57 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr112636.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O -ftree-vectorize" } */ + +int a[1], b; +unsigned c; +int main() { + while (b) { + if (a[c]) + break; + c--; + } + return 0; +} diff --git a/gcc/tree-ssa-loop-ch.cc b/gcc/tree-ssa-loop-ch.cc index 3ce5cc21df2..6c6e562d5a0 100644 --- a/gcc/tree-ssa-loop-ch.cc +++ b/gcc/tree-ssa-loop-ch.cc @@ -41,6 +41,8 @@ along with GCC; see the file COPYING3. If not see #include "gimple-pretty-print.h" #include "cfganal.h" #include "tree-ssa-loop-manip.h" +#include "tree-ssa-loop-niter.h" +#include "tree-scalar-evolution.h" /* Return path query insteance for testing ranges of statements in headers of LOOP contained in basic block BB. @@ -797,7 +799,16 @@ ch_base::copy_headers (function *fun) fprintf (dump_file, "Analyzing loop %i\n", loop->num); + /* If the loop is already a do-while style one (either because it was + written as such, or because jump threading transformed it into one), + we might be in fact peeling the first iteration of the loop. This + in general is not a good idea. Also avoid touching infinite loops. */ + if (!loop_has_exit_edges (loop) + || !process_loop_p (loop)) + continue; + basic_block header = loop->header; + estimate_numbers_of_iterations (loop); if (!get_max_loop_iterations_int (loop)) { if (dump_file && (dump_flags & TDF_DETAILS)) @@ -808,14 +819,6 @@ ch_base::copy_headers (function *fun) continue; } - /* If the loop is already a do-while style one (either because it was - written as such, or because jump threading transformed it into one), - we might be in fact peeling the first iteration of the loop. This - in general is not a good idea. Also avoid touching infinite loops. */ - if (!loop_has_exit_edges (loop) - || !process_loop_p (loop)) - continue; - /* Iterate the header copying up to limit; this takes care of the cases like while (a && b) {...}, where we want to have both of the conditions copied. TODO -- handle while (a || b) - like cases, by not requiring @@ -1170,12 +1173,12 @@ ch_base::copy_headers (function *fun) unsigned int pass_ch::execute (function *fun) { - loop_optimizer_init (LOOPS_HAVE_PREHEADERS - | LOOPS_HAVE_SIMPLE_LATCHES - | LOOPS_HAVE_RECORDED_EXITS); + loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); + scev_initialize (); unsigned int res = copy_headers (fun); + scev_finalize (); loop_optimizer_finalize (); return res; }