From patchwork Tue Nov 14 14:13:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 164926 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp1888908vqg; Tue, 14 Nov 2023 06:13:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEE3Q5wdQokAbUdU3446HRlIBpxzAFATCe5iZsiAEq0Lm/AtCLtlTbDBxMMEpKO1RFRADR6 X-Received: by 2002:a05:620a:2482:b0:77a:4462:1026 with SMTP id i2-20020a05620a248200b0077a44621026mr2523921qkn.60.1699971233470; Tue, 14 Nov 2023 06:13:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699971233; cv=pass; d=google.com; s=arc-20160816; b=CPatjE0YKuo6i9G0E2+kFtT3I8TAO5ZUUTn2n4KbkIG+lxuaaBpBkbPRT/1MmeQ81k xoJkwxprLGWlyDQSoOWpyvc6ZTHlhju+Yt9aNTuqsV4fWo2Hb+Cg981Ox2wlrFT3jF0F VUr79Z30qshr6LAF6q3CEO2diea4iAFJ/PXCb1DB5z6god23BTej3MN7+890kRt9SmgB tuF7YQe+30CDKTMFWrp7kIQUaPWuCxU+KEpssGTZPMZjCzWZTRW9HQtSiq8dD/4Zwhxi pqOyrjbyWmFHhwEPAWvsj92KkJT4hAaQgliwUS00O6XhkkPRRgqrCqe+5Wy/D5v/keo1 Gtyg== 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:cc:to:from:date:dkim-signature:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=vnS+fRYDA4D7NKwsNwmX/yYY6SqO0GPLG+MK6TdPO8Y=; fh=+H+PfslJQ8SfgojuI5pxEuPHAhp/Ml9gJNsp0+tS7IU=; b=USSg6cMwRPxomT1heu7ZiTOWlMPhLI7p8JCpQhTzF96XHQjT4/qzvOZ5vvdQSW5UDp EQV7nSAikxwY7xvYRn3dy9LKThV3HYMYS87tEKNrRSHiWMxb7QfVraxXEfULVwbupZKR 0BnXGdhfhiRttHQ83ykIKc3ea/fwPN6AxU23G4CJEHHccMFoZDbkI8ybyoS4PISuc3Rs P22lgOcb/iW0+johQ67dkgLKtY9TncYZ/3MIn9YoRFdD07ijMBm38IUn2wqR1kebFrJ2 sfKXtWGcuHUDT/TeIrSEcXq3YUNNLBTV+3fJBPW7IYOdJCxB1Sy300nGhZODwHev2mD3 V5Gw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=l1omj952; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=pass (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 s13-20020a05620a080d00b00767f1da20c3si6542080qks.500.2023.11.14.06.13.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 06:13:53 -0800 (PST) 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; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=l1omj952; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=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 F3BC2385840E for ; Tue, 14 Nov 2023 14:13:52 +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.220.29]) by sourceware.org (Postfix) with ESMTPS id 60AA03858D35 for ; Tue, 14 Nov 2023 14:13:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60AA03858D35 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 60AA03858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699971210; cv=none; b=Ru+KP9lqJLGS9pj0U9kjyinxVZGCiixak0KeVH3HdlLnl5f5y13W+xssJR1YxSTZsg6ZQyb7NiVJghjS7xwo/2ugkYQ61i7RuWkYPIhMyWHBw8bBq4NgnS+BAxuiTDyK6iFYJYt+oq8PbEW0l5dLogiVFhWsQAuJlPJjMxxHXeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699971210; c=relaxed/simple; bh=LLNTtkPRcEq0ozphcBL/WtnXbnjzzl29BAGOdmEDqBU=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version; b=JDp5e7PY08vhyNgm1Cwjcu6fVUBtSiXswr/Wh4CFZ/XPEAFpJhjbvRZWBcjf3LNcothkArkS94i8vCRUE5gQ+tJtUqyZynCoFU27q/SXxKbueCCLceY6FT3mPGkRAxCK9MbhBUy16JShI571Qs53kHkige2h7mYpYk04jzmCfDc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 1DD861F88C; Tue, 14 Nov 2023 14:13:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1699971207; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=vnS+fRYDA4D7NKwsNwmX/yYY6SqO0GPLG+MK6TdPO8Y=; b=l1omj952HubJhH1lj1srck0mbo+e4RSFLO8wSKgYbQWUWw7JFKK+ladnN4f4dN5/7f21dz /Rv+5v/NumfHyuwHlG6sDJUxs/U8bXBlvdItPL3goN/Zd1WOuytl3hb9/iajujlaG6TJqU AmAKzeY3wEs3ymDOkv8qZ1rGwx6H1/Q= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1699971207; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=vnS+fRYDA4D7NKwsNwmX/yYY6SqO0GPLG+MK6TdPO8Y=; b=qX6VosCYfSYubilKeo55f8nebZIjK08xCiO72041hPXVmmCZ60go86bV/Nb4vZqQQ+MkDP AOs7krjsQafAhvAw== 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 F13DD2C56F; Tue, 14 Nov 2023 14:13:26 +0000 (UTC) Date: Tue, 14 Nov 2023 14:13:27 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: Jan Hubicka Subject: [PATCH] tree-optimization/111233 - loop splitting miscompile User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out2.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out2.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)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.20)[suse.de]; R_SPF_SOFTFAIL(0.60)[~all:c]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MX_GOOD(-0.01)[]; RCPT_COUNT_TWO(0.00)[2]; VIOLATED_DIRECT_SPF(3.50)[]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; NEURAL_HAM_SHORT(-1.00)[-1.000]; 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: 1DD861F88C X-Spam-Status: No, score=-10.7 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: <20231114141352.F3BC2385840E@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782549036190322464 X-GMAIL-MSGID: 1782549036190322464 The change in r14-2852-gf5fb9ff2396fd4 failed to update patch_loop_exit to compensate for rewriting of a NE/EQ_EXPR to a new code. Fixed with the following. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/111233 PR tree-optimization/111652 PR tree-optimization/111727 PR tree-optimization/111838 PR tree-optimization/112113 * tree-ssa-loop-split.cc (patch_loop_exit): Get the new guard code instead of the old guard stmt. (split_loop): Adjust. * gcc.dg/torture/pr111233.c: New testcase. * gcc.dg/torture/pr111652.c: Likewise. * gcc.dg/torture/pr111727.c: Likewise. * gcc.dg/torture/pr111838.c: Likewise. * gcc.dg/torture/pr112113.c: Likewise. --- gcc/testsuite/gcc.dg/torture/pr111233.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr111652.c | 15 +++++++++++++++ gcc/testsuite/gcc.dg/torture/pr111727.c | 14 ++++++++++++++ gcc/testsuite/gcc.dg/torture/pr111838.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr112113.c | 16 ++++++++++++++++ gcc/tree-ssa-loop-split.cc | 9 ++++----- 6 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr111233.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr111652.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr111727.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr111838.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr112113.c diff --git a/gcc/testsuite/gcc.dg/torture/pr111233.c b/gcc/testsuite/gcc.dg/torture/pr111233.c new file mode 100644 index 00000000000..30934033ae0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111233.c @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fsplit-loops" } */ + +int a, c, f; +char b, g; +int *d = &c; +long e; +int main() +{ + for (; e != 25; e++) { + f = -17; + for (; f <= 0; f = f + 7) { + g = f ? 0 : b; + a = *d; + } + } + if (a != 0) + __builtin_abort (); +} diff --git a/gcc/testsuite/gcc.dg/torture/pr111652.c b/gcc/testsuite/gcc.dg/torture/pr111652.c new file mode 100644 index 00000000000..ebca9c79816 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111652.c @@ -0,0 +1,15 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fsplit-loops" } */ + +volatile int a; +int b; +int main() { + for (; b < 5; b += 3) { + b && a; + if (b < 4) + a--; + } + if (b != 6) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr111727.c b/gcc/testsuite/gcc.dg/torture/pr111727.c new file mode 100644 index 00000000000..fb68e197e2b --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111727.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fsplit-loops" } */ + +int a, b; +int main() +{ + for (; a < 4; a += 2) + if (a > 2) + while (b++); + ; + if (a != 4) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr111838.c b/gcc/testsuite/gcc.dg/torture/pr111838.c new file mode 100644 index 00000000000..67007d9742d --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111838.c @@ -0,0 +1,17 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fsplit-loops" } */ + +int a, b, c; +volatile char d; +int main() +{ + for (; b < 1; b++) + for (char e = -17; e < 1; e += 5) + { + if (e ? a % e : 0) + d; + for (c = 0; c < 1; c++) + ; + } + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr112113.c b/gcc/testsuite/gcc.dg/torture/pr112113.c new file mode 100644 index 00000000000..96cd75f1be2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr112113.c @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* The -Waggressive-loop-optimizations diagnostic is spurious, missed + constant propagation after final value replacement. */ +/* { dg-additional-options "-Wno-aggressive-loop-optimizations -fsplit-loops" } */ + +volatile int a; +int main() +{ + for (int b = 0; b < 33; b += 3) { + if (b > 31) + a++; + } + if (a != 0) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc index 64464802c1e..2e2adb67260 100644 --- a/gcc/tree-ssa-loop-split.cc +++ b/gcc/tree-ssa-loop-split.cc @@ -194,13 +194,12 @@ split_at_bb_p (class loop *loop, basic_block bb, tree *border, affine_iv *iv, also be true/false in the next iteration. */ static void -patch_loop_exit (class loop *loop, gcond *guard, tree nextval, tree newbound, - bool initial_true) +patch_loop_exit (class loop *loop, tree_code guard_code, tree nextval, + tree newbound, bool initial_true) { edge exit = single_exit (loop); gcond *stmt = as_a (*gsi_last_bb (exit->src)); - gimple_cond_set_condition (stmt, gimple_cond_code (guard), - nextval, newbound); + gimple_cond_set_condition (stmt, guard_code, nextval, newbound); update_stmt (stmt); edge stay = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit); @@ -745,7 +744,7 @@ split_loop (class loop *loop1) gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop1), stmts); tree guard_next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop1)); - patch_loop_exit (loop1, guard_stmt, guard_next, newend, initial_true); + patch_loop_exit (loop1, guard_code, guard_next, newend, initial_true); /* Finally patch out the two copies of the condition to be always true/false (or opposite). */