From patchwork Tue Dec 19 12:31:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 180905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1901624dyi; Tue, 19 Dec 2023 04:32:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUkWueOZkg+dMK5kqEXXTHpW1eU3E39o6ehL1YfFzm4uMD9P5Nu5QjAN83w7Knub+JPWTS X-Received: by 2002:ac8:5aca:0:b0:417:b269:4689 with SMTP id d10-20020ac85aca000000b00417b2694689mr25648842qtd.53.1702989171957; Tue, 19 Dec 2023 04:32:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702989171; cv=pass; d=google.com; s=arc-20160816; b=DVdcNZZY2Wayy36EJCwPrAf6fvD9uSQHx5z/rA1oOpPv35JvBu24xIL1LNVr6rqxZt U1pXuwjC8Y61vC/UgEvzj5zYwXM05k4TGs165F4ESGyGUGWApg4p0UyJyVsMRIVC1S61 GhQSaosNv/xT6VgfeVIary9vS6PzwRGj7xDFHXJehMkHD0U1Au/xN4oG5/Ym3tVbe7AE gzB2UcEqfvblpPMXNpFlk5JdPRSGGw94/6/ZULpNrqc6/0wyg1cnY5O6kLYJUjad7Gdf qnDmk0gTpTHFoJP1xf8J7VaDpt1MGuFKWq902gtNMqRTjTYJUH3aMqnDbSjTuk1VoaNc eGvw== 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=pPFymyV/5QcUEZJhxovqp5GXe+SxB4AyyfL4sEwlShw=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=W52WgRokDPjqbymA5bs/Qi9NKlQvwA6YAHrVJfxgiSEYf5p/qYyat4fL8AKp4L0Hid OqmeHVODzKcUIifiQUFkLNKLg/Yf0Mh8Iwlqsb+CxU6mHcT2FULGJWZJxYexz4OnKzvw CD2HpNWK8sW/iiY8KKyO2TgFcrEmIWq+WZCBb8NBTAyFiUaISWeba0NT/YRO9n8Ne9qJ TiffKr1zgNNSPZ1izG4OC+39rMcMSwfm5Rxi84CwdseAwqsLE6gdDZQmtBovEON5BSjV e0hD61M7PyL4DYBcoTmW7lnbY+xZYkzjP0oIaftdddY9fjeMR8K9PquZZHLaw7gs6Cz3 JHew== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ITBw47de; dkim=neutral (no key) header.i=@suse.de; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=BkTBA+kQ; dkim=neutral (no key) header.i=@suse.de; 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 ca17-20020a05622a1f1100b004238e29de8asi27569983qtb.557.2023.12.19.04.32.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 04:32:51 -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=ITBw47de; dkim=neutral (no key) header.i=@suse.de; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=BkTBA+kQ; dkim=neutral (no key) header.i=@suse.de; 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 29740386076C for ; Tue, 19 Dec 2023 12:32:51 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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 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: <20231219123251.29740386076C@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785713573748121975 X-GMAIL-MSGID: 1785713573748121975 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);