From patchwork Mon Oct 23 10:34:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 156834 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1199985vqx; Mon, 23 Oct 2023 03:35:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkq6y0mpAzo6rBokPV7BHZFi5JIrCPWSXpHvtaXBAE+IXhxOxuvhMHwNx/6uU7QH4eshHX X-Received: by 2002:ad4:5966:0:b0:66d:1927:4e6b with SMTP id eq6-20020ad45966000000b0066d19274e6bmr10703190qvb.56.1698057304288; Mon, 23 Oct 2023 03:35:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698057304; cv=pass; d=google.com; s=arc-20160816; b=SBAIPhWkOjG07/SFfu6Q0F2mpyRYYeZ0X5VnXd6N594sNuK4KvAh9Gh2yt6TWt8iy0 2yezaZniCnG/glYGM/G9Fs1fjco89EQyqzaKstBEeYfFN+JTxF8oCyYcQHFFQa8CAn7T zFD96X6M4VNKqeY0RdmPBhg0kMTf05LJwFK5c8XaIisLpX8x/V9LutZHn+nBZ9g8mfLr DsGczZudSbRKlW70OD5cUP0l07AXEq5XH6g7v1rIQFMQHZhLs7m5fJNwo+/8lw9LmTGO V/z856rGr3H0VrFVUe90l1u/ZGYmRxMvVjZJNlBp3YNZnK2cSO7mGdcaUq5QFQMd0w4N 4R5A== 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 :user-agent:subject:to:from:date:arc-filter:dmarc-filter :delivered-to; bh=fWbCekHzWjwsPqUZ04eR7HPWm9RmqqcN6Je6iBjE1go=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=L1m1ngbNmGQ2HCRHGGNdYRezgUvQEwlUyfW3r1j35op3aHJ1uXc8JR7k0FJQjGMDyU QThotvmft9UDgQWTdwL0wh/1llQsw0lV+BKNkInH5OxeNLla1cb8DD+EwQ76wmJWh7EO x6uQk5FxW9kzZawZYZQMfTOGv24RoY0O4LpVYbf1XRay1j6e6MnDHvA+mxDCEVrEK4bp CKCv7UzxaFzkr7kHYUQoLNus3hsxYppTA467D7de0OLbo1EJexI9jOXQ3isuZdn9cfL2 Y6U1dCrMtfOH7co5OcaVFw7TbJhUqOHIy+IWJr/zTVoWBC4on7hZl4aBrlhGTkXyUhZA Az+A== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m4-20020ad44b64000000b0066d1e8d7d7asi5226640qvx.191.2023.10.23.03.35.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 03:35:04 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (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 0C30D3858414 for ; Mon, 23 Oct 2023 10:35:04 +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 [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 4E2AD3858401 for ; Mon, 23 Oct 2023 10:34:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E2AD3858401 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 4E2AD3858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698057280; cv=none; b=QUgZEH6ycIbI9FwLma5CbnSjKgvXBn4h47xqIIzjfdC3BDMADspY1GQna/RGQ7UupOOtaeghRbmzZ6M7li19RyZ6AWy14Wk6MbvYFFnQ2eauLISm5u/I/VL7LKJGZQdG6Uc4Txluj2TTiP4LTLsYnKNdEDoonTLpX36pZ5/aOUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698057280; c=relaxed/simple; bh=3qvRD7qC4LsQHmJbB8nA427OLKq289juSqdY+75wjok=; h=Date:From:To:Subject:MIME-Version; b=rCtJdexWuv6vfBO50jED7ojafMjkis1ELzwJKDi8rahhuSAUK4b9I47aP3k9sRcHY1u7EMPIUXB+LMlwDnx0tDpOfpQNZM03i7iYpgGjcpiXxXb83sTUWwOXwC2SDtjKIPYxfINhwmnahsRkGm6i2xaR15mhdLZyDdHxUAiMEIk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id B177521AD7 for ; Mon, 23 Oct 2023 10:34:36 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 77B9A2CEDE for ; Mon, 23 Oct 2023 10:34:36 +0000 (UTC) Date: Mon, 23 Oct 2023 10:34:36 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111917 - bougs IL after guard hoisting User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out1.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out1.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [-0.01 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; R_SPF_SOFTFAIL(0.60)[~all:c]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; VIOLATED_DIRECT_SPF(3.50)[]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; DMARC_NA(0.20)[suse.de]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -0.01 X-Rspamd-Queue-Id: B177521AD7 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, MISSING_MID, 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 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: <20231023103504.0C30D3858414@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780542136130967697 X-GMAIL-MSGID: 1780542136130967697 The unswitching code to hoist guards inserts conditions in wrong places. The following fixes this, simplifying code. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/111917 * tree-ssa-loop-unswitch.cc (hoist_guard): Always insert new conditional after last stmt. * gcc.dg/torture/pr111917.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr111917.c | 23 +++++++++++++++++++++++ gcc/tree-ssa-loop-unswitch.cc | 5 +---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr111917.c diff --git a/gcc/testsuite/gcc.dg/torture/pr111917.c b/gcc/testsuite/gcc.dg/torture/pr111917.c new file mode 100644 index 00000000000..532e30200b5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111917.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-funswitch-loops" } */ + +long t; +long a() { + long b = t, c = t; + for (; b < 31; b++) + c <<= 1; + return c; +} +long t1; +static +int d() { + if (!t1) + return 0; +e: +f: + for (; a();) + ; + goto f; + return 0; +} +int main() { d(); } diff --git a/gcc/tree-ssa-loop-unswitch.cc b/gcc/tree-ssa-loop-unswitch.cc index 619b50fb4bb..b4611ac8256 100644 --- a/gcc/tree-ssa-loop-unswitch.cc +++ b/gcc/tree-ssa-loop-unswitch.cc @@ -1455,10 +1455,7 @@ hoist_guard (class loop *loop, edge guard) cond_stmt = as_a (stmt); extract_true_false_edges_from_block (guard_bb, &te, &fe); /* Insert guard to PRE_HEADER. */ - if (!empty_block_p (pre_header)) - gsi = gsi_last_bb (pre_header); - else - gsi = gsi_start_bb (pre_header); + gsi = gsi_last_bb (pre_header); /* Create copy of COND_STMT. */ new_cond_stmt = gimple_build_cond (gimple_cond_code (cond_stmt), gimple_cond_lhs (cond_stmt),