From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 9997B3858D1E; Wed, 17 May 2023 06:10:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9997B3858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0353726.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34H6ALG1003404; Wed, 17 May 2023 06:10:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=pp1; bh=azrbE6HYTiAXybCq3WwvvZXtdSErJfboPjkXXUwgCRk=; b=Xw6i66YHIiY4uqvWTC/tZdsjKQYMpGUOw3mA2Nz1vUvIJS4nxY+DxYiHKayHQn5W3qL6 N4rewYHV5LW3QifVPqqM3rsdOJ5i4gWrxj42iu7+NWTtKtTYiJGvNzjq2oiAejet9i2F b62UvYmt/Bcfsk9h61peEj3Tcd/KFSsZ6NbZeryUMKirFBxpGW3KfeilxDivD0/Hfw9f IpbQNB5BB1s8LLCBAijbbwZps6ekoPXtuiFKXX9v3HV7+fftrdcSpEPD3PGwx9lOu+QZ 4xIDLTmQWi6VoFRrnAI/L7XJIrqXAwDBW5M7K+jY9xpOeYsuSv+EIg/xeCtF+YE/NQeO Lw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qms850jmt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 17 May 2023 06:10:58 +0000 Received: from m0353726.ppops.net (m0353726.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 34H5wmKf006528; Wed, 17 May 2023 06:10:57 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qms850jkt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 17 May 2023 06:10:57 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 34H0baam023481; Wed, 17 May 2023 06:10:55 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma04ams.nl.ibm.com (PPS) with ESMTPS id 3qj264t0qh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 17 May 2023 06:10:55 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 34H6Aq7a28508696 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 17 May 2023 06:10:53 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C351F20043; Wed, 17 May 2023 06:10:52 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 890CB2004E; Wed, 17 May 2023 06:10:51 +0000 (GMT) Received: from ltcden2-lp1.aus.stglabs.ibm.com (unknown [9.3.90.43]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 17 May 2023 06:10:51 +0000 (GMT) From: Jiufu Guo To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, jeffreyalaw@gmail.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org, guojiufu@linux.ibm.com Subject: [PATCH] Optimized "(X - N * M) / N + M" to "X / N" if valid Date: Wed, 17 May 2023 14:10:50 +0800 Message-Id: <20230517061050.3778864-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: sieRmxRyFB8X3QYU0ZTcOkUh8gIFazi- X-Proofpoint-ORIG-GUID: _GUYCHjFV-6F-ltjgKv7hyyQQ1irom2z X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-16_14,2023-05-16_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 phishscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 clxscore=1015 mlxscore=0 lowpriorityscore=0 suspectscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305170051 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_MSPIKE_H2,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: Hi, This patch tries to optimize "(X - N * M) / N + M" to "X / N". As per the discussions in PR108757, we know this transformation is valid only under some conditions. For C code, "/" towards zero (trunc_div), and "X - N * M" maybe wrap/overflow/underflow. So, it is valid that "X - N * M" does not cross zero and does not wrap/overflow/underflow. This patch also handles the case when "N" is the power of 2, where "(X - N * M) / N" is "(X - N * M) >> log2(N)". Bootstrap & regtest pass on ppc64{,le} and x86_64. Is this ok for trunk? BR, Jeff (Jiufu) PR tree-optimization/108757 gcc/ChangeLog: * gimple-match-head.cc (optimize_x_minus_NM_div_N_plus_M): New function. * match.pd ((X - N * M) / N + M): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/pr108757-1.c: New test. * gcc.dg/pr108757-2.c: New test. * gcc.dg/pr108757.h: New test. --- gcc/gimple-match-head.cc | 54 ++++++++++ gcc/match.pd | 22 ++++ gcc/testsuite/gcc.dg/pr108757-1.c | 17 ++++ gcc/testsuite/gcc.dg/pr108757-2.c | 18 ++++ gcc/testsuite/gcc.dg/pr108757.h | 160 ++++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr108757-1.c create mode 100644 gcc/testsuite/gcc.dg/pr108757-2.c create mode 100644 gcc/testsuite/gcc.dg/pr108757.h diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc index b08cd891a13..680a4cb2fc6 100644 --- a/gcc/gimple-match-head.cc +++ b/gcc/gimple-match-head.cc @@ -224,3 +224,57 @@ optimize_successive_divisions_p (tree divisor, tree inner_div) } return true; } + +/* Return true if "(X - N * M) / N + M" can be optimized into "X / N". + Otherwise return false. + + For unsigned, + If sign bit of M is 0 (clz is 0), valid range is [N*M, MAX]. + If sign bit of M is 1, valid range is [0, MAX - N*(-M)]. + + For signed, + If N*M > 0, valid range: [MIN+N*M, 0] + [N*M, MAX] + If N*M < 0, valid range: [MIN, -(-N*M)] + [0, MAX - (-N*M)]. */ + +static bool +optimize_x_minus_NM_div_N_plus_M (tree x, wide_int n, wide_int m, tree type) +{ + wide_int max = wi::max_value (type); + signop sgn = TYPE_SIGN (type); + wide_int nm; + wi::overflow_type ovf; + if (TYPE_UNSIGNED (type) && wi::clz (m) == 0) + nm = wi::mul (n, -m, sgn, &ovf); + else + nm = wi::mul (n, m, sgn, &ovf); + + if (ovf != wi::OVF_NONE) + return false; + + value_range vr0; + if (!get_range_query (cfun)->range_of_expr (vr0, x) || vr0.varying_p () + || vr0.undefined_p ()) + return false; + + wide_int wmin0 = vr0.lower_bound (); + wide_int wmax0 = vr0.upper_bound (); + wide_int min = wi::min_value (type); + + /* unsigned */ + if ((TYPE_UNSIGNED (type))) + /* M > 0 (clz != 0): [N*M, MAX], M < 0 : [0, MAX-N*(-M)] */ + return wi::clz (m) != 0 ? wi::ge_p (wmin0, nm, sgn) + : wi::le_p (wmax0, max - nm, sgn); + + /* signed, N*M > 0 */ + else if (wi::gt_p (nm, 0, sgn)) + /* [N*M, MAX] or [MIN+N*M, 0] */ + return wi::ge_p (wmin0, nm, sgn) + || (wi::ge_p (wmin0, min + nm, sgn) && wi::le_p (wmax0, 0, sgn)); + + /* signed, N*M < 0 */ + /* [MIN, N*M] or [0, MAX + N*M]*/ + else + return wi::le_p (wmax0, nm, sgn) + || (wi::ge_p (wmin0, 0, sgn) && wi::le_p (wmax0, max - (-nm), sgn)); +} diff --git a/gcc/match.pd b/gcc/match.pd index ceae1c34abc..1aaa5530577 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -881,6 +881,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +#if GIMPLE +/* Simplify ((t + -N*M) / N + M) -> t / N. */ +(for div (trunc_div exact_div) + (simplify + (plus (div (plus @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) + (with {wide_int n = wi::to_wide (@2); wide_int m = wi::to_wide (@3);} + (if (INTEGRAL_TYPE_P (type) + && n * m == -wi::to_wide (@1) + && optimize_x_minus_NM_div_N_plus_M (@0, n, m, type)) + (div @0 @2))))) + +/* Simplify ((t + -(M<> N + M) -> t >> N. */ +(simplify + (plus (rshift (plus @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) + (with {wide_int n = wi::to_wide (@2); wide_int m = wi::to_wide (@3);} + (if (INTEGRAL_TYPE_P (type) + && (m << n) == -wi::to_wide (@1) + && optimize_x_minus_NM_div_N_plus_M (@0, + wi::one (TYPE_PRECISION (type)) << n, m, type)) + (rshift @0 @2)))) +#endif + (for op (negate abs) /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ (for coss (COS COSH) diff --git a/gcc/testsuite/gcc.dg/pr108757-1.c b/gcc/testsuite/gcc.dg/pr108757-1.c new file mode 100644 index 00000000000..349318a7c82 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757-1.c @@ -0,0 +1,17 @@ +/* PR tree-optimization/108757 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include +#define N 4 +#define M 3 +#define GAP 0 +typedef unsigned int UINT; +typedef int INT; +#define UMAX UINT_MAX +#define IMAX INT_MAX +#define IMIN INT_MIN +#include "pr108757.h" + +/* { dg-final { scan-tree-dump-not " = x_\[0-9\]\\(D\\) \\+ " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " = b_\[0-9\] \\+ " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr108757-2.c b/gcc/testsuite/gcc.dg/pr108757-2.c new file mode 100644 index 00000000000..7302e9c606c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757-2.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/108757 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include +#define N 4 +#define M 3 +#define GAP 2 +typedef unsigned int UINT; +typedef int INT; +#define UMAX UINT_MAX +#define IMAX INT_MAX +#define IMIN INT_MIN +#include "pr108757.h" + +/* { dg-final { scan-tree-dump-times " = x_\[0-9\]\\(D\\) \\+ " 16 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " = b_\[0-9\] \\+ " 16 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/pr108757.h b/gcc/testsuite/gcc.dg/pr108757.h new file mode 100644 index 00000000000..9e12d106aad --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757.h @@ -0,0 +1,160 @@ +#define NOINLINE __attribute__ ((noinline)) +UINT NOINLINE +opt_u1 (UINT x) +{ + if (x < (M * N) - GAP) + return 0; + UINT a = x - (M * N); + UINT b = a / N; + return b + M; +} + +UINT NOINLINE +opt_u2 (UINT x) +{ + if (x > (UMAX - (M * N) + GAP)) + return 0; + UINT a = x + (M * N); + UINT b = a / N; + return b - M; +} + +INT NOINLINE +opt_s1 (INT x) +{ + if (x < (M * N) - GAP) + return 0; + INT a = x - (M * N); + INT b = a / N; + return b + M; +} + +INT NOINLINE +opt_s2 (INT x) +{ + if (x < IMIN + (M * N) - GAP || x > 0) + return 0; + INT a = x - (M * N); + INT b = a / N; + return b + M; +} + +INT NOINLINE +opt_s3 (INT x) +{ + if (x < (M * N) - GAP) + return 0; + INT a = x - (M * N); + INT b = a / -N; + return b + -M; +} + +INT NOINLINE +opt_s4 (INT x) +{ + if (x < IMIN + (M * N) - GAP || x > 0) + return 0; + INT a = x - (M * N); + INT b = a / -N; + return b + -M; +} + +INT NOINLINE +opt_s5 (INT x) +{ + if (x > (-M * N) + GAP) + return 0; + INT a = x - (-M * N); + INT b = a / N; + return b + -M; +} + +INT NOINLINE +opt_s6 (INT x) +{ + if (x > IMAX - (M * N) + GAP || x < 0) + return 0; + INT a = x - (-M * N); + INT b = a / N; + return b + -M; +} + +INT NOINLINE +opt_s7 (INT x) +{ + if (x > (M * -N) + GAP) + return 0; + INT a = x - (M * -N); + INT b = a / -N; + return b + M; +} + +INT NOINLINE +opt_s8 (INT x) +{ + if (x > IMAX - (M * N) + GAP || x < 0) + return 0; + INT a = x - (M * -N); + INT b = a / -N; + return b + M; +} + +UINT NOINLINE +opt_u3 (UINT x) +{ + if (x < (M << N) - GAP) + return 0; + UINT a = x - (M << N); + UINT b = a >> N; + return b + M; +} + +UINT NOINLINE +opt_u4 (UINT x) +{ + if (x > (UMAX - (M << N)) + GAP) + return 0; + UINT a = x + (M << N); + UINT b = a >> N; + return b - M; +} + +INT NOINLINE +opt_s9 (INT x) +{ + if (x < (M << N) - GAP) + return 0; + INT a = x - (M << N); + INT b = a >> N; + return b + M; +} + +INT NOINLINE +opt_s10 (INT x) +{ + if (x < IMIN + (M << N) - GAP || x > 0) + return 0; + INT a = x - (M << N); + INT b = a >> N; + return b + M; +} + +INT NOINLINE +opt_s11 (INT x) +{ + if (x > (-M << N) + GAP) + return 0; + INT a = x - (-M << N); + INT b = a >> N; + return b + -M; +} + +INT NOINLINE +opt_s12 (INT x) +{ + if (x > IMAX - (M << N) + GAP || x < 0) + return 0; + INT a = x - (-M << N); + INT b = a >> N; + return b + -M; +} -- 2.31.1