From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 20AAA385DC3B for ; Tue, 19 Dec 2023 12:32:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20AAA385DC3B 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 20AAA385DC3B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702989148; cv=none; b=pCLjCJsFoKb1CzVUIY7piy5HlWaqnegZwoYKeaEWRKoDdsYLmBGDIZFsy5+ihG3fTZ4+X0W38s4/HLwrd4g0s7O1q2s59tJN3/uiy3Q3deoDRqOlBrw3SZ2czoQUQuH4VaeYJbnnACBVDWv78qgTbGvKUGUETjUyS2fP/Y+q5mE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702989148; c=relaxed/simple; bh=Y/rtnuS1gZhltTg9s2vJdl1wIbwzuefnZATC2vLPGGM=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=YNOki1rvZZ8JQiDD+3XHoLbV9mJq3ZLLaeRJAQB7Dq/w5aRoYnUqA3G59b1/QGoTFP3hbn48YTrm464Ws+wwUCpXrflcIQKRYTdl/2sSZBXTSbbLSU2TTGes6Y0MSt7ftivBok7eG9NPhPGMXpci/7T8hm99/0Xixo0Xd1CQOLw= 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-out2.suse.de (Postfix) with ESMTPS id B923F1FD63 for ; Tue, 19 Dec 2023 12:32:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702989145; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pPFymyV/5QcUEZJhxovqp5GXe+SxB4AyyfL4sEwlShw=; b=ITBw47delOxCrsw7nrqVIEalpWmgAad04b86WKUFs9Al/3E3uFIh9mAYmJ6m4V2CKUE/wO mfxLwp6LHTzjrDdLnq+ytjCNw4Gzgj3MN6MVrsk1qU6Hj4uXpr1/+gOA9ZsSHr5bI2o3SU hLaQnbn8CCvK0AaREODHZaMQ3fvBPSw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702989145; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pPFymyV/5QcUEZJhxovqp5GXe+SxB4AyyfL4sEwlShw=; b=5icStCiPfdJmHHYqJCB3iJjd3P5e7zFuvxZzSZ8dyS6sESSwpM2dHttlhMwZYDI4kvRfba lTWWvm/RtITG5FDg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702989144; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pPFymyV/5QcUEZJhxovqp5GXe+SxB4AyyfL4sEwlShw=; b=BkTBA+kQsCT2JFUGmnIOHewEsM5rfB7rW7Y6YveIndFqA+8BZNYtxMfoQD6PjNwByPGqgE wFHI+IEWN/e6QCXIFwVuYm/QUNXn+wyygC9+fcKOx59vhpBYDpyD0cqg5Kn9iEFEV2JOR9 o94EbOyPyb5+h2P2Hqdtl8jMidLSe+s= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702989144; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pPFymyV/5QcUEZJhxovqp5GXe+SxB4AyyfL4sEwlShw=; b=TsNnjHxe7BDz/PNzPWHSqYULXmjkYVBL77AdKcPXFf2YDNGk/hQJuyQVbUKBXK0esdBvXd InK5CDCVYEfzAUAQ== Date: Tue, 19 Dec 2023 13:31:13 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/113080 - missing final value replacement MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Level: Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -1.79 X-Spamd-Result: default: False [-1.79 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; 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]; NEURAL_HAM_SHORT(-0.19)[-0.965]; DBL_BLOCKED_OPENRESOLVER(0.00)[tree-scalar-evolution.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-Status: No, score=-11.0 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 List-Id: Message-ID: <20231219123113.s3rSPYN0QZ6MrW301i22sa3O2WTNJeqUTj_uWxucOXU@z> When performing final value replacement we guard against exponential (temporary) code growth due to unsharing of trees (SCEV heavily relies on tree sharing). The following relaxes this a tiny bit to cover some more optimizations and puts in comments as to what the real fix would be. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/113080 * tree-scalar-evolution.cc (expression_expensive_p): Allow a tiny bit of growth due to expansion of shared trees. (final_value_replacement_loop): Add comment. * gcc.dg/tree-ssa/sccp-3.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/sccp-3.c | 20 ++++++++++++++++++++ gcc/tree-scalar-evolution.cc | 11 ++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sccp-3.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sccp-3.c b/gcc/testsuite/gcc.dg/tree-ssa/sccp-3.c new file mode 100644 index 00000000000..b8c67427f16 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sccp-3.c @@ -0,0 +1,20 @@ +/* PR/113080 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int a,b,n; +int w; +void fun1(int t) +{ + for(int i=0;i<100;i++) + { + a+=w; + b-=w; + t+=a+b; + } + n=t; +} + +/* We should apply final value replacement to all reductions and + elide the loop. */ +/* { dg-final { scan-tree-dump-times " cache.elements ()); + /* ??? Both the explicit unsharing and gimplification of expr will + expand shared trees to multiple copies. + Guard against exponential growth by counting the visits and + comparing againt the number of original nodes. Allow a tiny + bit of duplication to catch some additional optimizations. */ + || expanded_size > (cache.elements () + 1)); } /* Match.pd function to match bitwise inductive expression. @@ -3867,6 +3872,10 @@ final_value_replacement_loop (class loop *loop) fprintf (dump_file, "\n"); } any = true; + /* ??? Here we'd like to have a unshare_expr that would assign + shared sub-trees to new temporary variables either gimplified + to a GIMPLE sequence or to a statement list (keeping this a + GENERIC interface). */ def = unshare_expr (def); remove_phi_node (&psi, false); -- 2.35.3