[PATCH 1/2] [Gimple] Simplify (trunc)fmax/fmin((extend)a, (extend)b) to MAX/MIN(a,b)

Hongtao Liu crazylht@gmail.com
Mon Nov 8 01:36:57 GMT 2021


On Fri, Nov 5, 2021 at 5:52 PM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Fri, Nov 5, 2021 at 6:38 AM liuhongt <hongtao.liu@intel.com> wrote:
> >
> > a and b are same type as trunc type and has less precision than
> > extend type, the transformation is guarded by flag_finite_math_only.
> >
> > Bootstrapped and regtested under x86_64-pc-linux-gnu{-m32,}
> > Ok for trunk?
> >
> > gcc/ChangeLog:
> >
> >         PR target/102464
> >         * match.pd: Simplify (trunc)fmax/fmin((extend)a, (extend)b) to
> >         MAX/MIN(a,b)
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * gcc.target/i386/pr102464-maxmin.c: New test.
> > ---
> >  gcc/match.pd                                  | 14 ++++++
> >  .../gcc.target/i386/pr102464-maxmin.c         | 44 +++++++++++++++++++
> >  2 files changed, 58 insertions(+)
> >  create mode 100644 gcc/testsuite/gcc.target/i386/pr102464-maxmin.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index f63079023d0..857ce7f712a 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -6182,6 +6182,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >         && direct_internal_fn_supported_p (IFN_COPYSIGN,
> >                                           type, OPTIMIZE_FOR_BOTH))
> >      (IFN_COPYSIGN @0 @1))))
> > +
> > +(for maxmin (max min)
> > + (simplify
> > +  (convert (maxmin (convert@2 @0) (convert @1)))
> > +   (if (flag_finite_math_only
>
> I suppose you are concerned about infinities, not about NaNs.
> Please use !HONOR_INFINITIES (@2) then (in general testing
> flag_* is frowned upon).  You may want to do the FLOAT_TYPE_P
> tests first.
I'm concerned about NANs since MAX/MIN_EXPR are different from IEEE
minimum and maximum operations at NAN operations.
So i think i'd use MODE_HAS_NANS(@2)?
>
> > +       && optimize
> > +       && FLOAT_TYPE_P (type)
> > +       && FLOAT_TYPE_P (TREE_TYPE (@2))
> > +       && types_match (type, TREE_TYPE (@0))
> > +       && types_match (type, TREE_TYPE (@1))
> > +       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
> > +       && optab_handler (maxmin == MAX_EXPR ? smax_optab : smin_optab,
> > +                       TYPE_MODE (type)) != CODE_FOR_nothing)
> > +    (maxmin @0 @1))))
> >  #endif
> >
> >  (for froms (XFLOORL XCEILL XROUNDL XRINTL)
> > diff --git a/gcc/testsuite/gcc.target/i386/pr102464-maxmin.c b/gcc/testsuite/gcc.target/i386/pr102464-maxmin.c
> > new file mode 100644
> > index 00000000000..37867235a6c
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/i386/pr102464-maxmin.c
> > @@ -0,0 +1,44 @@
> > +/* PR target/102464.  */
> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -mavx512fp16 -mavx512vl -ffast-math -ftree-vectorize -mtune=generic -mfpmath=sse" } */
> > +/* { dg-final { scan-assembler-times "vmaxph" 3 } }  */
> > +/* { dg-final { scan-assembler-times "vminph" 3 } }  */
> > +/* { dg-final { scan-assembler-times "vmaxsh" 3 } }  */
> > +/* { dg-final { scan-assembler-times "vminsh" 3 } }  */
> > +/* { dg-final { scan-assembler-times "vmaxps" 2 } }  */
> > +/* { dg-final { scan-assembler-times "vminps" 2 } }  */
> > +/* { dg-final { scan-assembler-times "vmaxss" 2 } }  */
> > +/* { dg-final { scan-assembler-times "vminss" 2 } }  */
> > +/* { dg-final { scan-assembler-times "vmaxpd" 1 } }  */
> > +/* { dg-final { scan-assembler-times "vminpd" 1 } }  */
> > +/* { dg-final { scan-assembler-times "vmaxsd" 1 } }  */
> > +/* { dg-final { scan-assembler-times "vminsd" 1 } }  */
> > +
> > +#include<math.h>
> > +#define FOO(CODE,TYPE,SUFFIX)                                          \
> > +  void                                                                 \
> > +  foo_vect_##CODE##TYPE##SUFFIX (TYPE* __restrict a, TYPE* b, TYPE* c) \
> > +  {                                                                    \
> > +    for (int i = 0; i != 8; i++)                                       \
> > +      a[i] = CODE##SUFFIX (b[i], c[i]);                                        \
> > +  }                                                                    \
> > +  TYPE                                                                 \
> > +  foo_##CODE##TYPE##SUFFIX (TYPE b, TYPE c)                            \
> > +  {                                                                    \
> > +    return CODE##l (b, c);                                             \
> > +  }
> > +
> > +FOO (fmax, _Float16, f);
> > +FOO (fmax, _Float16,);
> > +FOO (fmax, _Float16, l);
> > +FOO (fmin, _Float16, f);
> > +FOO (fmin, _Float16,);
> > +FOO (fmin, _Float16, l);
> > +
> > +FOO (fmax, float,);
> > +FOO (fmax, float, l);
> > +FOO (fmin, float,);
> > +FOO (fmin, float, l);
> > +
> > +FOO (fmax, double, l);
> > +FOO (fmin, double, l);
> > --
> > 2.18.1
> >



--
BR,
Hongtao


More information about the Gcc-patches mailing list