From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 928B1385AF9A for ; Fri, 7 Jul 2023 17:56:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 928B1385AF9A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=marvell.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=marvell.com Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 367BgZ5w021154 for ; Fri, 7 Jul 2023 10:56:33 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=hEjmRnD+bqR86BTU8KnVtHuWJMbP6KIriDVDeniMo8o=; b=QpTWEY95OPjJp+1NWxfB3THETu67lp+LWY60jq5OKS4qWwQcTWAvf3MfUyperSaQV1C0 2c3twXft63gV8FBlgpE/qppEEt5ZUQcQe1k4ocPcH/kIH+3JqINt5hcpmuu7kTpH3MKN 5huWcvEsO1dRqTi/3/VOtl0384igwiP7riRTlO+ErxYaC2AAPtO6OIXJanyqE/BbD2tR 7ePnjLwTEl0jHh7TfOyWYw8WGEPlDUxvlc9IAIppu7iVT2GHGwPHF4iRth+UUnFRfYcn /RyX+HcfWGScVAlt3hvhzIpnOLfWe3iROBVKGMMYW3+P9EAanr7dHB0s6p9542tPVokc Ag== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3rpj2hs1ht-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 07 Jul 2023 10:56:33 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 7 Jul 2023 10:56:31 -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; Fri, 7 Jul 2023 10:56:31 -0700 Received: from vpnclient.wrightpinski.org (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 989AA3F7070; Fri, 7 Jul 2023 10:56:31 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] Fix PR 110539: missed optimization after moving two_value to match.pd Date: Fri, 7 Jul 2023 10:56:22 -0700 Message-ID: <20230707175622.702351-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-ORIG-GUID: SC5XOBpjPaQ_N6vJhgXxzlY_4qC3RjcW X-Proofpoint-GUID: SC5XOBpjPaQ_N6vJhgXxzlY_4qC3RjcW 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-07_12,2023-07-06_02,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,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: When I moved two_value to match.pd, I removed the check for the {0,+-1} as I had placed it after the {0,+-1} case for cond in match.pd. In the case of {0,+-1} and non boolean, before we would optmize those case to just `(convert)a` but after we would get `(convert)(a != 0)` which was not handled anyways to just `(convert)a`. So this adds a pattern to match `(convert)(zeroone != 0)` and simplify to `(convert)zeroone`. In the bug report, we do finally optimize `(convert)(zeroone != 0)` to `zeroone` in VRP2 but that in itself is too late and we miss other optimizations that would have happened. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/110539 * match.pd ((convert)(zeroone !=/== 0)): Match and simplify to ((convert)zeroone)){,^1}. gcc/testsuite/ChangeLog: PR tree-optimization/110539 * 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/match.pd | 15 +++++ 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 | 70 ++++++++++++++++++++++ 4 files changed, 109 insertions(+) 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 diff --git a/gcc/match.pd b/gcc/match.pd index c709153217a..87767a7778b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2060,6 +2060,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop)) (vec_cond @0 @2 @3))) +#if GIMPLE +/* This cannot be done on generic as fold has the + exact opposite transformation: + `Fold ~X & 1 as (X & 1) == 0.` + `Fold (X ^ 1) & 1 as (X & 1) == 0.` */ +/* (convert)(zeroone != 0) into (convert)zeroone */ +/* (convert)(zeroone == 0) into (convert)(zeroone^1) */ +(for neeq (ne eq) + (simplify + (convert (neeq zero_one_valued_p@0 integer_zerop)) + (if (neeq == NE_EXPR) + (convert @0) + (convert (bit_xor @0 { build_one_cst (TREE_TYPE (@0)); } ))))) +#endif + /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */ (simplify (bit_and:c (convert? (negate zero_one_valued_p@0)) @1) 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..c8ef6f56dcd --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110539-3.c @@ -0,0 +1,70 @@ +/* { 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 we optimize dom2 because phiopt2 we just get: + _12 = i_7; + if (i_7 == _12) + goto ; [50.00%] + else + goto ; [50.00%] + */ + +/* { dg-final { scan-tree-dump-not "foo \\(\\)" "optimized"} } */ -- 2.31.1