From patchwork Wed Jul 12 16:08:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1806872 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=crUZ1sNA; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1N2r2WFnz20Ph for ; Thu, 13 Jul 2023 02:08:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 21CDC3858413 for ; Wed, 12 Jul 2023 16:08:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 21CDC3858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689178130; bh=KEDKrCCPPZQwCEQBhOR9AbZ2RCcsc1kVofbDNnoYOi8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=crUZ1sNAl/0O8Ec5KG99NsbbDuiWYlLR6sh7//69cqFrNsca+AT9AW7Op+fAUc6/W d04RKHuVLezpo5EHDBvo8eKnVZo2BqX2vh7LDjvc20ZtjV69aBujaFp2BLEOr1xtep NeQfLcPcIhwe6oGLUXgRUvFLKoMDU01/82bJ1LxA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 89E243858D20 for ; Wed, 12 Jul 2023 16:08:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89E243858D20 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36CE9Jt9024720 for ; Wed, 12 Jul 2023 09:08:28 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3rsb9aknh8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 12 Jul 2023 09:08:27 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 12 Jul 2023 09:08:25 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 12 Jul 2023 09:08:25 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 3592C3F703F; Wed, 12 Jul 2023 09:08:25 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] Fix part of PR 110293: `A NEEQ (A NEEQ CST)` part Date: Wed, 12 Jul 2023 09:08:02 -0700 Message-ID: <20230712160802.998150-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: sBu_zFZW5cFFqNc3Yjzcf6GtRlgVfm32 X-Proofpoint-ORIG-GUID: sBu_zFZW5cFFqNc3Yjzcf6GtRlgVfm32 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-12_11,2023-07-11_01,2023-05-22_02 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SCC_5_SHORT_WORD_LINES, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This fixes part of PR 110293, for the outer comparison case being `!=` or `==`. In turn PR 110539 is able to be optimized again as the if statement for `(a&1) == ((a & 1) != 0)` gets optimized to `false` early enough to allow FRE/DOM to do a CSE for memory store/load. OK? Bootstrapped and tested on x86_64-linux with no regressions. gcc/ChangeLog: PR tree-optimization/110293 PR tree-optimization/110539 * match.pd: Expand the `x != (typeof x)(x == 0)` pattern to handle where the inner and outer comparsions are either `!=` or `==` and handle other constants than 0. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr110293-1.c: New test. * gcc.dg/tree-ssa/pr110539-1.c: New test. * gcc.dg/tree-ssa/pr110539-2.c: New test. * gcc.dg/tree-ssa/pr110539-3.c: New test. * gcc.dg/tree-ssa/pr110539-4.c: New test. --- gcc/match.pd | 39 ++++++++-- gcc/testsuite/gcc.dg/tree-ssa/pr110293-1.c | 58 +++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110539-1.c | 12 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr110539-2.c | 12 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c | 75 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110539-4.c | 82 ++++++++++++++++++++++ 6 files changed, 274 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110293-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110539-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110539-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110539-4.c diff --git a/gcc/match.pd b/gcc/match.pd index 8543f777a28..351d9285e92 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6429,10 +6429,41 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (TYPE_UNSIGNED (TREE_TYPE (@0))) { constant_boolean_node (false, type); })) -/* x != (typeof x)(x == 0) is always true. */ -(simplify - (ne:c @0 (convert (eq @0 integer_zerop))) - { constant_boolean_node (true, type); }) +/* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */ +/* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */ +/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x != 0) */ +/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x != 1) */ +(for outer (ne eq) + (for inner (ne eq) + (simplify + (outer:c @0 (convert (inner @0 INTEGER_CST@1))) + (with { + bool cst1 = integer_onep (@1); + bool cst0 = integer_zerop (@1); + bool innereq = inner == EQ_EXPR; + bool outereq = outer == EQ_EXPR; + } + (switch + (if (innereq ? cst0 : cst1) + { constant_boolean_node (!outereq, type); }) + (if (innereq ? cst1 : cst0) + (with { + tree utype = unsigned_type_for (TREE_TYPE (@0)); + tree ucst1 = build_one_cst (utype); + } + (if (!outereq) + (gt (convert:utype @0) { ucst1; }) + (le (convert:utype @0) { ucst1; }) + ) + ) + ) + (if (innereq) + (ne @0 { build_zero_cst (TREE_TYPE (@0)); })) + (ne @0 { build_one_cst (TREE_TYPE (@0)); })) + ) + ) + ) +) (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) /* If the second operand is NaN, the result is constant. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110293-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110293-1.c new file mode 100644 index 00000000000..24aea1a2d03 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110293-1.c @@ -0,0 +1,58 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ + +_Bool eqeq0(unsigned x) +{ + return x == (x == 0); +} +_Bool eqeq1(unsigned x) +{ + return x == (x == 1); +} +_Bool eqeq2(unsigned x) +{ + return x == (x == 2); +} + +_Bool neeq0(unsigned x) +{ + return x != (x == 0); +} +_Bool neeq1(unsigned x) +{ + return x != (x == 1); +} +_Bool neeq2(unsigned x) +{ + return x != (x == 2); +} + +_Bool eqne0(unsigned x) +{ + return x == (x != 0); +} +_Bool eqne1(unsigned x) +{ + return x == (x != 1); +} +_Bool eqne2(unsigned x) +{ + return x == (x != 2); +} + +_Bool nene0(unsigned x) +{ + return x != (x != 0); +} +_Bool nene1(unsigned x) +{ + return x != (x != 1); +} +_Bool nene2(unsigned x) +{ + return x != (x != 2); +} + +/* All of these functions should have removed the inner most comparison which + means all of the conversions from bool to unsigned should have been removed too. */ +/* { dg-final { scan-tree-dump-not "nop_expr," "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110539-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-1.c new file mode 100644 index 00000000000..6ba864cdd13 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b != 0; + return c == b; +} + +/* This should be optimized to just return 1; */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump "return 1;" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110539-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-2.c new file mode 100644 index 00000000000..17874d349ef --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b == 0; + return c == b; +} + +/* This should be optimized to just return 0; */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump "return 0;" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c new file mode 100644 index 00000000000..e2bd4dfb45d --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c @@ -0,0 +1,75 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +void foo(void); +static int a, c = 1; +static short b; +static int *d = &c, *e = &a; +static int **f = &d; +void __assert_fail() __attribute__((__noreturn__)); +static void g(short h) { + if (*d) + ; + else { + if (e) __assert_fail(); + if (a) { + __builtin_unreachable(); + } else + __assert_fail(); + } + if ((((0, 0) || h) == h) + b) *f = 0; +} +int main() { + int i = 0 != 10 & a; + g(i); + *e = 9; + e = 0; + if (d == 0) + ; + else + foo(); + ; +} +/* The call to foo should be optimized away. */ +/* The missed optimization at -O2 here was: + int b = a & 1; + int c = b != 0; + int d = c == b; + not being optimized to 1 early enough, it is done in vrp2 but + that is too late. + In phiopt2 we got: + _17 = i_7 != 0; + _12 = (int) _17; + if (i_7 == _12) + goto ; [50.00%] + else + goto ; [50.00%] + + [local count: 268435456]: + d = 0B; + + [local count: 536870913]: + e.1_3 = e; + *e.1_3 = 9; + e = 0B; + d.2_4 = d; + if (d.2_4 == 0B) + + The first if is not optimized before, until vrp2 which is + too late as there are no passes which will then find the + load of d in `d.2_4 = d;` was `0B` after vrp2. + + Now in forwprop3 (after phiopt2), we optimize: + _17 = i_7 != 0; + _12 = (int) _17; + if (i_7 == _12) + into just: + _t = (unsigned)i_7; + if (_t <= 1) + + And then during ccp3, that is optimized away and that is optimized + early enough now that the load `d.2_4 = d;` is optimizd to just + `d.2_4 = 0B;` + */ + +/* { dg-final { scan-tree-dump-not "foo \\(\\)" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110539-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-4.c new file mode 100644 index 00000000000..2c03dcc87fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-4.c @@ -0,0 +1,82 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fstrict-aliasing -fdump-tree-optimized" } */ + +/* This is a small variant of pr110539-3.c using -O1 -fstrict-aliasing + rather than -O2. Just to show VRP and PRE is not needed to optimize + the call to foo away. */ + + +void foo(void); +static int a, c = 1; +static short b; +static int *d = &c, *e = &a; +static int **f = &d; +void __assert_fail() __attribute__((__noreturn__)); +static void g(int h) { + if (*d) + ; + else { + if (e) __assert_fail(); + if (a) { + __builtin_unreachable(); + } else + __assert_fail(); + } + if (((h!=0) == h) + b) *f = 0; +} + +int main() { + int i = 0 != 10 & a; + g(i); + *e = 9; + e = 0; + if (d == 0) + ; + else + foo(); + ; +} + +/* The call to foo should be optimized away. */ +/* The missed optimization at -O1 here was: + int b = a & 1; + int c = b != 0; + int d = c == b; + not being optimized to 1 early enough, it is done in vrp2 but + that is too late. + In phiopt2 we got: + _17 = i_7 != 0; + _12 = (int) _17; + if (i_7 == _12) + goto ; [50.00%] + else + goto ; [50.00%] + + [local count: 268435456]: + d = 0B; + + [local count: 536870913]: + e.1_3 = e; + *e.1_3 = 9; + e = 0B; + d.2_4 = d; + if (d.2_4 == 0B) + + The first if is not optimized before, until vrp2 which is + too late as there are no passes which will then find the + load of d in `d.2_4 = d;` was `0B` after vrp2. + + Now in forwprop3 (after phiopt2), we optimize: + _17 = i_7 != 0; + _12 = (int) _17; + if (i_7 == _12) + into just: + _t = (unsigned)i_7; + if (_t <= 1) + + And then during ccp3, that is optimized away and that is optimized + early enough now that the load `d.2_4 = d;` is optimizd to just + `d.2_4 = 0B;` + */ + +/* { dg-final { scan-tree-dump-not "foo \\(\\)" "optimized"} } */