Move expN folds to match.pd
diff mbox

Message ID 87fv0yvu0f.fsf@e105548-lin.cambridge.arm.com
State New
Headers show

Commit Message

Richard Sandiford Oct. 26, 2015, 9:50 a.m. UTC
Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi.
OK to install?

Thanks,
Richard


gcc/
	* builtins.c (fold_builtin_exponent): Delete.
	(fold_builtin_2): Handle constant expN arguments here.
	* match.pd: Fold expN(logN(x)) -> x.

Comments

Richard Biener Oct. 26, 2015, 10:15 a.m. UTC | #1
On Mon, Oct 26, 2015 at 10:50 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_exponent): Delete.
>         (fold_builtin_2): Handle constant expN arguments here.
>         * match.pd: Fold expN(logN(x)) -> x.
>
> diff --git a/gcc/builtins.c b/gcc/builtins.c
> index 3d39d43..e5a00ee 100644
> --- a/gcc/builtins.c
> +++ b/gcc/builtins.c
> @@ -7516,47 +7516,6 @@ fold_const_builtin_pow (tree arg0, tree arg1, tree type)
>    return NULL_TREE;
>  }
>
> -/* A subroutine of fold_builtin to fold the various exponent
> -   functions.  Return NULL_TREE if no simplification can be made.
> -   FUNC is the corresponding MPFR exponent function.  */
> -
> -static tree
> -fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
> -                      int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
> -{
> -  if (validate_arg (arg, REAL_TYPE))
> -    {
> -      tree type = TREE_TYPE (TREE_TYPE (fndecl));
> -      tree res;
> -
> -      /* Calculate the result when the argument is a constant.  */
> -      if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
> -       return res;
> -
> -      /* Optimize expN(logN(x)) = x.  */
> -      if (flag_unsafe_math_optimizations)
> -       {
> -         const enum built_in_function fcode = builtin_mathfn_code (arg);
> -
> -         if ((func == mpfr_exp
> -              && (fcode == BUILT_IN_LOG
> -                  || fcode == BUILT_IN_LOGF
> -                  || fcode == BUILT_IN_LOGL))
> -             || (func == mpfr_exp2
> -                 && (fcode == BUILT_IN_LOG2
> -                     || fcode == BUILT_IN_LOG2F
> -                     || fcode == BUILT_IN_LOG2L))
> -             || (func == mpfr_exp10
> -                 && (fcode == BUILT_IN_LOG10
> -                     || fcode == BUILT_IN_LOG10F
> -                     || fcode == BUILT_IN_LOG10L)))
> -           return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
> -       }
> -    }
> -
> -  return NULL_TREE;
> -}
> -
>  /* Fold function call to builtin memchr.  ARG1, ARG2 and LEN are the
>     arguments to the call, and TYPE is its return type.
>     Return NULL_TREE if no simplification can be made.  */
> @@ -9004,14 +8963,20 @@ fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
>      break;
>
>      CASE_FLT_FN (BUILT_IN_EXP):
> -      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
> +      if (validate_arg (arg0, REAL_TYPE))
> +       return do_mpfr_arg1 (arg0, type, mpfr_exp, NULL, NULL, 0);
> +      break;
>
>      CASE_FLT_FN (BUILT_IN_EXP2):
> -      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
> +      if (validate_arg (arg0, REAL_TYPE))
> +       return do_mpfr_arg1 (arg0, type, mpfr_exp2, NULL, NULL, 0);
> +      break;
>
>      CASE_FLT_FN (BUILT_IN_EXP10):
>      CASE_FLT_FN (BUILT_IN_POW10):
> -      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
> +      if (validate_arg (arg0, REAL_TYPE))
> +       return do_mpfr_arg1 (arg0, type, mpfr_exp10, NULL, NULL, 0);
> +      break;
>
>      CASE_FLT_FN (BUILT_IN_EXPM1):
>        if (validate_arg (arg0, REAL_TYPE))
> diff --git a/gcc/match.pd b/gcc/match.pd
> index a8adffb..303889b 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2408,12 +2408,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>     (rdiv @0 (exps:s @1))
>      (mult @0 (exps (negate @1)))))
>
> - /* Special case, optimize logN(expN(x)) = x.  */
>   (for logs (LOG LOG2 LOG10 LOG10)
>        exps (EXP EXP2 EXP10 POW10)
> +  /* logN(expN(x)) -> x.  */
>    (simplify
>     (logs (exps @0))
> -    @0))
> +   @0)
> +  /* expN(logN(x)) -> x.  */
> +  (simplify
> +   (exps (logs @0))
> +   @0))
>
>   /* Optimize logN(func()) for various exponential functions.  We
>      want to determine the value "x" and the power "exponent" in
>
Marc Glisse Oct. 26, 2015, 10:32 a.m. UTC | #2
On Mon, 26 Oct 2015, Richard Sandiford wrote:

> +  /* expN(logN(x)) -> x.  */
> +  (simplify
> +   (exps (logs @0))
> +   @0))

We are not very consistent about it, but wasn't there an idea that we 
should use non_lvalue in most such places?
Richard Biener Oct. 26, 2015, 11:25 a.m. UTC | #3
On Mon, Oct 26, 2015 at 11:32 AM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Mon, 26 Oct 2015, Richard Sandiford wrote:
>
>> +  /* expN(logN(x)) -> x.  */
>> +  (simplify
>> +   (exps (logs @0))
>> +   @0))
>
>
> We are not very consistent about it, but wasn't there an idea that we should
> use non_lvalue in most such places?

Hmm, probably.  IIRC the builtins.c code always wraps a NOP_EXPR around
constants.

OTOH I hope that the delayed folding branch will get merged for C++ and thus
we can get rid of _all_ NON_VALUE_EXPRs produced by folding.  At least the
C FE no longer requires them.

Richard.

> --
> Marc Glisse

Patch
diff mbox

diff --git a/gcc/builtins.c b/gcc/builtins.c
index 3d39d43..e5a00ee 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -7516,47 +7516,6 @@  fold_const_builtin_pow (tree arg0, tree arg1, tree type)
   return NULL_TREE;
 }
 
-/* A subroutine of fold_builtin to fold the various exponent
-   functions.  Return NULL_TREE if no simplification can be made.
-   FUNC is the corresponding MPFR exponent function.  */
-
-static tree
-fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
-		       int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
-{
-  if (validate_arg (arg, REAL_TYPE))
-    {
-      tree type = TREE_TYPE (TREE_TYPE (fndecl));
-      tree res;
-
-      /* Calculate the result when the argument is a constant.  */
-      if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
-	return res;
-
-      /* Optimize expN(logN(x)) = x.  */
-      if (flag_unsafe_math_optimizations)
-	{
-	  const enum built_in_function fcode = builtin_mathfn_code (arg);
-
-	  if ((func == mpfr_exp
-	       && (fcode == BUILT_IN_LOG
-		   || fcode == BUILT_IN_LOGF
-		   || fcode == BUILT_IN_LOGL))
-	      || (func == mpfr_exp2
-		  && (fcode == BUILT_IN_LOG2
-		      || fcode == BUILT_IN_LOG2F
-		      || fcode == BUILT_IN_LOG2L))
-	      || (func == mpfr_exp10
-		  && (fcode == BUILT_IN_LOG10
-		      || fcode == BUILT_IN_LOG10F
-		      || fcode == BUILT_IN_LOG10L)))
-	    return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
-	}
-    }
-
-  return NULL_TREE;
-}
-
 /* Fold function call to builtin memchr.  ARG1, ARG2 and LEN are the
    arguments to the call, and TYPE is its return type.
    Return NULL_TREE if no simplification can be made.  */
@@ -9004,14 +8963,20 @@  fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
     break;
 
     CASE_FLT_FN (BUILT_IN_EXP):
-      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
+      if (validate_arg (arg0, REAL_TYPE))
+	return do_mpfr_arg1 (arg0, type, mpfr_exp, NULL, NULL, 0);
+      break;
 
     CASE_FLT_FN (BUILT_IN_EXP2):
-      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
+      if (validate_arg (arg0, REAL_TYPE))
+	return do_mpfr_arg1 (arg0, type, mpfr_exp2, NULL, NULL, 0);
+      break;
 
     CASE_FLT_FN (BUILT_IN_EXP10):
     CASE_FLT_FN (BUILT_IN_POW10):
-      return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
+      if (validate_arg (arg0, REAL_TYPE))
+	return do_mpfr_arg1 (arg0, type, mpfr_exp10, NULL, NULL, 0);
+      break;
 
     CASE_FLT_FN (BUILT_IN_EXPM1):
       if (validate_arg (arg0, REAL_TYPE))
diff --git a/gcc/match.pd b/gcc/match.pd
index a8adffb..303889b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2408,12 +2408,16 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (rdiv @0 (exps:s @1))
     (mult @0 (exps (negate @1)))))
 
- /* Special case, optimize logN(expN(x)) = x.  */
  (for logs (LOG LOG2 LOG10 LOG10)
       exps (EXP EXP2 EXP10 POW10)
+  /* logN(expN(x)) -> x.  */
   (simplify
    (logs (exps @0))
-    @0))
+   @0)
+  /* expN(logN(x)) -> x.  */
+  (simplify
+   (exps (logs @0))
+   @0))
 
  /* Optimize logN(func()) for various exponential functions.  We
     want to determine the value "x" and the power "exponent" in