This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Move pow folds to match.pd


On Mon, Oct 26, 2015 at 10:44 AM, Richard Sandiford
<rdsandiford@googlemail.com> wrote:
> Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi.
> OK to install?

Ok.

Thanks,
Richard.

> Thanks,
> Richard
>
>
> gcc/
>         * builtins.c (fold_builtin_pow): Delete in favor of...
>         (fold_const_builtin_pow): ...this new function.  Only handle constant
>         arguments.
>         (fold_builtin_2): Update accordingly.
>         * match.pd: Add rules previously handled by fold_builtin_pow.
>
> gcc/testsuite/
>         * gcc.dg/torture/builtin-math-1.c: Skip at -O0.
>
> diff --git a/gcc/builtins.c b/gcc/builtins.c
> index 64106a1..88c0576 100644
> --- a/gcc/builtins.c
> +++ b/gcc/builtins.c
> @@ -156,7 +156,6 @@ static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
>  static bool validate_arg (const_tree, enum tree_code code);
>  static rtx expand_builtin_fabs (tree, rtx, rtx);
>  static rtx expand_builtin_signbit (tree, rtx);
> -static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
>  static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
>  static tree fold_builtin_bitop (tree, tree);
>  static tree fold_builtin_strchr (location_t, tree, tree, tree);
> @@ -7478,7 +7477,7 @@ fold_builtin_bswap (tree fndecl, tree arg)
>  /* Fold a builtin function call to pow, powf, or powl.  Return
>     NULL_TREE if no simplification can be made.  */
>  static tree
> -fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
> +fold_const_builtin_pow (tree arg0, tree arg1, tree type)
>  {
>    tree res;
>
> @@ -7490,127 +7489,28 @@ fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
>    if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
>      return res;
>
> -  /* Optimize pow(1.0,y) = 1.0.  */
> -  if (real_onep (arg0))
> -    return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
> -
> -  if (TREE_CODE (arg1) == REAL_CST
> +  /* Check for an integer exponent.  */
> +  if (TREE_CODE (arg0) == REAL_CST
> +      && !TREE_OVERFLOW (arg0)
> +      && TREE_CODE (arg1) == REAL_CST
>        && !TREE_OVERFLOW (arg1))
>      {
> -      REAL_VALUE_TYPE cint;
> -      REAL_VALUE_TYPE c;
> -      HOST_WIDE_INT n;
> -
> -      c = TREE_REAL_CST (arg1);
> -
> -      /* Optimize pow(x,0.0) = 1.0.  */
> -      if (real_equal (&c, &dconst0))
> -       return omit_one_operand_loc (loc, type, build_real (type, dconst1),
> -                                arg0);
> -
> -      /* Optimize pow(x,1.0) = x.  */
> -      if (real_equal (&c, &dconst1))
> -       return arg0;
> -
> -      /* Optimize pow(x,-1.0) = 1.0/x.  */
> -      if (real_equal (&c, &dconstm1))
> -       return fold_build2_loc (loc, RDIV_EXPR, type,
> -                           build_real (type, dconst1), arg0);
> -
> -      /* Optimize pow(x,0.5) = sqrt(x).  */
> -      if (flag_unsafe_math_optimizations
> -         && real_equal (&c, &dconsthalf))
> +      REAL_VALUE_TYPE cint1;
> +      const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (arg0);
> +      const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (arg1);
> +      HOST_WIDE_INT n1 = real_to_integer (c1);
> +      real_from_integer (&cint1, VOIDmode, n1, SIGNED);
> +      /* Attempt to evaluate pow at compile-time, unless this should
> +        raise an exception.  */
> +      if (real_identical (c1, &cint1)
> +         && (n1 > 0
> +             || (!flag_trapping_math && !flag_errno_math)
> +             || !real_equal (c0, &dconst0)))
>         {
> -         tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
> -
> -         if (sqrtfn != NULL_TREE)
> -           return build_call_expr_loc (loc, sqrtfn, 1, arg0);
> -       }
> -
> -      /* Optimize pow(x,1.0/3.0) = cbrt(x).  */
> -      if (flag_unsafe_math_optimizations)
> -       {
> -         const REAL_VALUE_TYPE dconstroot
> -           = real_value_truncate (TYPE_MODE (type), dconst_third ());
> -
> -         if (real_equal (&c, &dconstroot))
> -           {
> -             tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
> -             if (cbrtfn != NULL_TREE)
> -               return build_call_expr_loc (loc, cbrtfn, 1, arg0);
> -           }
> -       }
> -
> -      /* Check for an integer exponent.  */
> -      n = real_to_integer (&c);
> -      real_from_integer (&cint, VOIDmode, n, SIGNED);
> -      if (real_identical (&c, &cint))
> -       {
> -         /* Attempt to evaluate pow at compile-time, unless this should
> -            raise an exception.  */
> -         if (TREE_CODE (arg0) == REAL_CST
> -             && !TREE_OVERFLOW (arg0)
> -             && (n > 0
> -                 || (!flag_trapping_math && !flag_errno_math)
> -                 || !real_equal (&TREE_REAL_CST (arg0), &dconst0)))
> -           {
> -             REAL_VALUE_TYPE x;
> -             bool inexact;
> -
> -             x = TREE_REAL_CST (arg0);
> -             inexact = real_powi (&x, TYPE_MODE (type), &x, n);
> -             if (flag_unsafe_math_optimizations || !inexact)
> -               return build_real (type, x);
> -           }
> -       }
> -    }
> -
> -  if (flag_unsafe_math_optimizations)
> -    {
> -      const enum built_in_function fcode = builtin_mathfn_code (arg0);
> -
> -      /* Optimize pow(expN(x),y) = expN(x*y).  */
> -      if (BUILTIN_EXPONENT_P (fcode))
> -       {
> -         tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
> -         tree arg = CALL_EXPR_ARG (arg0, 0);
> -         arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
> -         return build_call_expr_loc (loc, expfn, 1, arg);
> -       }
> -
> -      /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
> -      if (BUILTIN_SQRT_P (fcode))
> -       {
> -         tree narg0 = CALL_EXPR_ARG (arg0, 0);
> -         tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
> -                                   build_real (type, dconsthalf));
> -         return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
> -       }
> -
> -      /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative.  */
> -      if (BUILTIN_CBRT_P (fcode))
> -       {
> -         tree arg = CALL_EXPR_ARG (arg0, 0);
> -         if (tree_expr_nonnegative_p (arg))
> -           {
> -             tree c = build_real_truncate (type, dconst_third ());
> -             tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1, c);
> -             return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
> -           }
> -       }
> -
> -      /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative.  */
> -      if (fcode == BUILT_IN_POW
> -         || fcode == BUILT_IN_POWF
> -         || fcode == BUILT_IN_POWL)
> -       {
> -         tree arg00 = CALL_EXPR_ARG (arg0, 0);
> -         if (tree_expr_nonnegative_p (arg00))
> -           {
> -             tree arg01 = CALL_EXPR_ARG (arg0, 1);
> -             tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
> -             return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
> -           }
> +         REAL_VALUE_TYPE x;
> +         bool inexact = real_powi (&x, TYPE_MODE (type), c0, n1);
> +         if (flag_unsafe_math_optimizations || !inexact)
> +           return build_real (type, x);
>         }
>      }
>
> @@ -9476,7 +9376,7 @@ fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
>        return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
>
>      CASE_FLT_FN (BUILT_IN_POW):
> -      return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
> +      return fold_const_builtin_pow (arg0, arg1, type);
>
>      CASE_FLT_FN (BUILT_IN_POWI):
>        return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8cd55f6..b681573 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2481,6 +2481,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>   (for sqrts (SQRT)
>        cbrts (CBRT)
> +      pows (POW)
>        exps (EXP EXP2 EXP10 POW10)
>    /* sqrt(expN(x)) -> expN(x*0.5).  */
>    (simplify
> @@ -2489,7 +2490,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    /* cbrt(expN(x)) -> expN(x/3).  */
>    (simplify
>     (cbrts (exps @0))
> -   (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))))
> +   (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
> +  /* pow(expN(x), y) -> expN(x*y).  */
> +  (simplify
> +   (pows (exps @0) @1)
> +   (exps (mult @0 @1))))
>
>   /* tan(atan(x)) -> x.  */
>   (for tans (TAN)
> @@ -2525,6 +2530,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (hypot:c @0 real_zerop@1)
>   (abs @0))
>
> +/* pow(1,x) -> 1.  */
> +(simplify
> + (POW real_onep@0 @1)
> + @0)
> +
>  /* Canonicalization of sequences of math builtins.  These rules represent
>     IL simplifications but are not necessarily optimizations.
>
> @@ -2616,7 +2626,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
>    (simplify
>     (cbrts (pows tree_expr_nonnegative_p@0 @1))
> -   (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))
> +   (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
> +  /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
> +  (simplify
> +   (pows (sqrts @0) @1)
> +   (pows @0 (mult @1 { build_real (type, dconsthalf); })))
> +  /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
> +  (simplify
> +   (pows (cbrts tree_expr_nonnegative_p@0) @1)
> +   (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
> +  /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
> +  (simplify
> +   (pows (pows tree_expr_nonnegative_p@0 @1) @2)
> +   (pows @0 (mult @1 @2))))
>
>   /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
>   (simplify
> @@ -2768,6 +2790,36 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (real_isinf (TREE_REAL_CST_PTR (@1)))
>    { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
>
> +(for pows (POW)
> +     sqrts (SQRT)
> +     cbrts (CBRT)
> + (simplify
> +  (pows @0 REAL_CST@1)
> +  (with {
> +    const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
> +    REAL_VALUE_TYPE tmp;
> +   }
> +   (switch
> +    /* pow(x,0) -> 1.  */
> +    (if (real_equal (value, &dconst0))
> +     { build_real (type, dconst1); })
> +    /* pow(x,1) -> x.  */
> +    (if (real_equal (value, &dconst1))
> +     @0)
> +    /* pow(x,-1) -> 1/x.  */
> +    (if (real_equal (value, &dconstm1))
> +     (rdiv { build_real (type, dconst1); } @0))
> +    /* pow(x,0.5) -> sqrt(x).  */
> +    (if (flag_unsafe_math_optimizations
> +        && canonicalize_math_p ()
> +        && real_equal (value, &dconsthalf))
> +     (sqrts @0))
> +    /* pow(x,1/3) -> cbrt(x).  */
> +    (if (flag_unsafe_math_optimizations
> +        && canonicalize_math_p ()
> +        && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
> +            real_equal (value, &tmp)))
> +     (cbrts @0))))))
>
>  /* Narrowing of arithmetic and logical operations.
>
> diff --git a/gcc/testsuite/gcc.dg/torture/builtin-math-1.c b/gcc/testsuite/gcc.dg/torture/builtin-math-1.c
> index d052f53..4ecc98d 100644
> --- a/gcc/testsuite/gcc.dg/torture/builtin-math-1.c
> +++ b/gcc/testsuite/gcc.dg/torture/builtin-math-1.c
> @@ -6,6 +6,7 @@
>     Written by Roger Sayle, 16th August 2002.  */
>
>  /* { dg-do link } */
> +/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
>
>  extern double atan (double);
>  extern float atanf (float);
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]