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: 187384 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1457280dyi; Thu, 11 Jan 2024 05:50:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3OEdlkjpiEUU48FnKtsvZBXE3ytduXdRqwYrdL40f1yQapzkz4/DX4eSNDLr6Z8AW/mHV X-Received: by 2002:a81:4325:0:b0:5f6:bc81:fc1a with SMTP id q37-20020a814325000000b005f6bc81fc1amr601676ywa.103.1704981012855; Thu, 11 Jan 2024 05:50:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704981012; cv=pass; d=google.com; s=arc-20160816; b=mm/VxF7RQlwqT72KfllZwMuO7UbcrXmCk8N5xwmz/u2PeyIIawSnU42ZBBAn7v49Rv 4egRMFcie/ojnIbVIU4FF/pvJV93W+RGkOXN7TS7/kSazCNIyZV5xgDs+XOYmbm0QViF mYJN5Ke7t/Gw0P7Br8zY8ZKuk5PRLzBGZB4/syNVQbZkKFc7u4pUqB3C13HxAb5f73Zj kq4HfcIE0kzubWQfLTrQWVXHeybCo/4qjzyIq8593pT/fyOftJTXgD7pAbIqX9mwlXnz VaYHYNCadgk9UZynYofhPeAnPEWsQIaJYJmbu6Q/rLt4CXMWHsYjgX5iPEE6+2zWnS8X 1ORA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :subject:to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=sKtnJvOJedNno2oLf1TBIxjcIfLcTkiuewbncbfM+oc=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=gRztrkdc6WuMiQR3qDPHyGYwg52xCPYM1B4wOXsNBgjJSGnCFr74INJBFOAQ7w6kuM zHdPOyA6EkQxp1qvPv3RK+Aa10Hy7dzihjHJ7vVi29PxMarbA186BDq5N2mtAF+j3mja mrneZ+sBdnNfU/G5fC0KDpSLgl98tdFP2SwrkfPjB+Fj9FmVQNrCuKDkCQXUbmiWoTA3 BXHbl5N98pqFV6x5h2ex7kxRTA1FHDUlcJrAPTngUJVo4vBCzvJjwQipUF7T1mHfDu9h IPGgGnEg+aPK7QqdjPeMrZX5Pu/SSt+dlMs7Ad7XJWgrjmp2EHQS9deLuGf7AGotaycY aAJg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=gvOtpina; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Ajk0EUgF; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=b1WicObd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g14-20020a05620a108e00b0078324a9ede1si895706qkk.524.2024.01.11.05.50.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 05:50:12 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=gvOtpina; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Ajk0EUgF; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=b1WicObd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8A68B3857BB0 for ; Thu, 11 Jan 2024 13:50:12 +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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20240111135012.8A68B3857BB0@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787802170456881408 X-GMAIL-MSGID: 1787802170456881408 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; }