From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 50A643858D32 for ; Mon, 8 May 2023 05:26:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 50A643858D32 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 (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3483uDGx025172 for ; Sun, 7 May 2023 22:26:57 -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=YmVQrQBPp68TkHOYEpfoqmKXMS/AnfQ3EfA7XbdH7F0=; b=hVv4SuEAvM8Ht/EXF+aVw9q1Du7Jn9oGdjh93C6QliuvY/ZkYWQ0Jn2n2FLPCiif10g+ KvtFfglh/BYNHoLj2OL7VxiTaXFeqHt6VYS9/mXK1yisz2NPmrtHXxFdPyT0xp05GKbD K/h4NaE9UX/8VUUCw8lVSosLdz7C642kJ74gDhwyUxiQvAOc1LvbVG9rLnhI8K7nJwnL Gy839adZ05TC+3VxZEbCmiCj3UQjA/7t7Oj3L1visNOjkSznItXTnX4xKCkuQUGehGsZ ju+mqOMZ+CjazuQgTQh1MYC9UDl3edUMTuWwKhIdRobrr8qhCi77MliE4z2kf/f/J/7w pQ== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3qdpdpkhhy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 07 May 2023 22:26:57 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 7 May 2023 22:26:55 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Sun, 7 May 2023 22:26:54 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 25D405B6ADB; Sun, 7 May 2023 22:26:53 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] Add a != MIN/MAX_VALUE_CST ? CST-+1 : a to minmax_from_comparison Date: Sun, 7 May 2023 22:26:25 -0700 Message-ID: <20230508052625.1185434-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-GUID: smUXSFLobxjjBIdPbFHtCFyIMO4WHIvg X-Proofpoint-ORIG-GUID: smUXSFLobxjjBIdPbFHtCFyIMO4WHIvg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-08_02,2023-05-05_01,2023-02-09_01 X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,PLING_QUERY,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,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: This patch adds the support for match that was implemented for PR 87913 in phiopt. It implements it by adding support to minmax_from_comparison for the check. It uses the range information if available which allows to produce MIN/MAX expression when comparing against the lower/upper bound of the range instead of lower/upper of the type. minmax-20.c is the new testcase which tests the ranges part. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * fold-const.cc (minmax_from_comparison): Add support for NE_EXPR. * match.pd ((cond (cmp (convert1? x) c1) (convert2? x) c2) pattern): Add ne as a possible cmp. ((a CMP b) ? minmax : minmax pattern): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/minmax-20.c: New test. --- gcc/fold-const.cc | 26 +++++++++++++++++++++++ gcc/match.pd | 4 ++-- gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c | 12 +++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index db54bfc5662..d90671b9975 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -173,6 +173,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X > Y - 1 equals to X >= Y. */ if (cmp == GT_EXPR) code = GE_EXPR; + /* a != MIN_RANGE ? a : MIN_RANGE+1 -> MAX_EXPR+1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int min = widest_int::from (r.lower_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (min == wi::to_widest (exp1)) + code = MAX_EXPR; + } } if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1)) { @@ -182,6 +195,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X >= Y + 1 equals to X > Y. */ if (cmp == GE_EXPR) code = GT_EXPR; + /* a != MAX_RANGE ? a : MAX_RANGE-1 -> MIN_EXPR-1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int max = widest_int::from (r.upper_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (max == wi::to_widest (exp1)) + code = MIN_EXPR; + } } } if (code != ERROR_MARK diff --git a/gcc/match.pd b/gcc/match.pd index a55ede838cd..95f7e9a6abc 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4751,7 +4751,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) Case 2) (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ -(for cmp (lt le gt ge eq) +(for cmp (lt le gt ge eq ne) (simplify (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) (with @@ -4942,7 +4942,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Optimize (a CMP b) ? minmax : minmax to minmax, c> */ (for minmax (min max) - (for cmp (lt le gt ge) + (for cmp (lt le gt ge ne) (simplify (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4)) (with diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c new file mode 100644 index 00000000000..481c375f5f9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt2" } */ + +int f(int num) +{ + if (num < 3) __builtin_unreachable(); + return num != 3 ? num : 4; +} + +/* In phiopt2 with the range information, this should be turned into + a MAX_EXPR. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt2" } } */ -- 2.31.1