Hi,

Jiufu Guo via Gcc-patches <gcc-patches@gcc.gnu.org> writes:

> Hi,
>
> Richard Biener <rguent...@suse.de> 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?

Oh, I may misunderstand here.
You may say: M could be with a range too, then we can check if
"X - N * M" has a valid range or possible wrap/overflow. 

BR,
Jeff (Jiufu Guo)

>
>
> 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)) >> 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 <limits.h>
>>> +#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 <limits.h>
>>> +#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;
>>> +}
>>> 

Reply via email to