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 DD37A3858CD1; Wed, 31 May 2023 09:18:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD37A3858CD1 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 (m0353728.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34V96vCk001609; Wed, 31 May 2023 09:18:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : in-reply-to : references : date : message-id : mime-version : content-type; s=pp1; bh=I6ySgJLKxQlnaLagcOyHZEdRtF9IBelJoHOMlJgKRJ0=; b=bcX+pD67IO/aXgjgv4zrLrOOCLlMhNOniwZu902G+BdIL7Pc3InMc/hK+Tn9+JipOkdm VkAaNA+r71wRWmrlLUtY3XP2JWIjVQ6V18lBVqtUACo2omsPautkEFjITuyRj07TvBPp n5MXznVOs81du0mUuuMZszCcmJ2IgFZV2rogW/LBk6vQmc52M3I5ZwfwQE3z7M5nEFqs AkjSkZ8GsWLCF3UFuu2g6KqjMkGmuaUKWvfrnoExTSZzX/j6SNaiC/s6rDb/m3p8fyVr asB684jlTC3RnB/8IUfVzKdAIVp+DGVSQsk3qSy11IftmITGEyl5ZLzYHSchOlVA/VEP VQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qwrpvpr42-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 May 2023 09:18:23 +0000 Received: from m0353728.ppops.net (m0353728.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 34V973Fk002689; Wed, 31 May 2023 09:18:22 GMT Received: from ppma01dal.us.ibm.com (83.d6.3fa9.ip4.static.sl-reverse.com [169.63.214.131]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qwrpvpr3u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 May 2023 09:18:22 +0000 Received: from pps.filterd (ppma01dal.us.ibm.com [127.0.0.1]) by ppma01dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 34V6Q0QK019428; Wed, 31 May 2023 09:18:21 GMT Received: from smtprelay04.dal12v.mail.ibm.com ([9.208.130.102]) by ppma01dal.us.ibm.com (PPS) with ESMTPS id 3qu9g6n55d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 31 May 2023 09:18:21 +0000 Received: from smtpav06.wdc07v.mail.ibm.com (smtpav06.wdc07v.mail.ibm.com [10.39.53.233]) by smtprelay04.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 34V9IKC758393034 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 31 May 2023 09:18:20 GMT Received: from smtpav06.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6C6FF58066; Wed, 31 May 2023 09:18:20 +0000 (GMT) Received: from smtpav06.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0731258054; Wed, 31 May 2023 09:18:20 +0000 (GMT) Received: from ltcden2-lp1.aus.stglabs.ibm.com (unknown [9.3.90.43]) by smtpav06.wdc07v.mail.ibm.com (Postfix) with ESMTPS; Wed, 31 May 2023 09:18:19 +0000 (GMT) From: Jiufu Guo To: Richard Biener Cc: gcc-patches@gcc.gnu.org, jeffreyalaw@gmail.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org Subject: Re: [PATCH] Optimized "(X - N * M) / N + M" to "X / N" if valid In-Reply-To: (Richard Biener's message of "Tue, 30 May 2023 09:48:51 +0000 (UTC)") References: <20230517061050.3778864-1-guojiufu@linux.ibm.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Date: Wed, 31 May 2023 17:18:17 +0800 Message-ID: <7nsfbcx3ja.fsf@ltcden2-lp1.aus.stglabs.ibm.com> MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 X-Proofpoint-GUID: nS7-jrdf-Ndh8AGGtFHLELCT0kIxT98D X-Proofpoint-ORIG-GUID: pqDl8OPwDaqwlZLqrmsZe3J-CRULE-xo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-31_04,2023-05-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 lowpriorityscore=0 suspectscore=0 spamscore=0 mlxscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 priorityscore=1501 bulkscore=0 adultscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305310078 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,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, Richard Biener writes: > On Wed, 17 May 2023, Jiufu Guo wrote: > >> Hi, >> >> This patch tries to optimize "(X - N * M) / N + M" to "X / N". > > But if that's valid why not make the transform simpler and transform > (X - N * M) / N to X / N - M instead? Great catch! If "N * M" is not constant, "X / N - M" would be better than "(X - N * M) / N". If "N, M" are constants, "(X - N * M) / N" and "X / N - M" may be similar; while for this case, "X / N - M" should also be fine! I would try to update accordingly. > > You use the same optimize_x_minus_NM_div_N_plus_M validator for > the division and shift variants but the overflow rules are different, > so I'm not sure that's warranted. I'd also prefer to not split out > the validator to a different file - iff then the appropriate file > is fold-const.cc, not gimple-match-head.cc (I see we're a bit > inconsistent here, for pure gimple matches gimple-fold.cc would > be another place). Thanks for pointing out this! For shift, I guess you may concern that: 1. if the right operand is negative or is greater than or equal to the type width. 2. if it is a signed negative value. They may UB or 'sign bit shift'? This patch assumes it is ok to do the transform. I may have more check to see if this is really ok, and hope some one can point out if this is invalid. "(X - N * M) >> log2(N)" ==> " X >> log2(N) - M". I split out the validator just because: it is shared for division and shift :). And it seems gimple-match-head.cc and generic-match-head.cc, may be introduced for match.pd. So, I put it into gimple-match-head.cc. > > Since you use range information why is the transform restricted > to constant M? If M is a variable, the range for "X" is varying_p. I did not find the method to get the bounds for "X" (or for "X - N * M") to check no wraps. Any suggestions? Again, thanks for your great help! BR, Jeff (Jiufu Guo) > > Richard. > >> 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; >> +} >>