Move signbit folds to match.pd
diff mbox

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

Commit Message

Richard Sandiford Oct. 26, 2015, 9:52 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_signbit): Delete.
	(fold_builtin_2): Handle constant signbit arguments here.
	* match.pd: Add rules previously handled by fold_builtin_signbit.

gcc/testsuite/
	PR tree-optimization/68097
	* gcc.dg/torture/builtin-nonneg-1.c: Skip at -O0.  Add
	--param max-ssa-name-query-depth=3 to dg-options.

Comments

Richard Biener Oct. 26, 2015, 10:17 a.m. UTC | #1
On Mon, Oct 26, 2015 at 10:52 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_signbit): Delete.
>         (fold_builtin_2): Handle constant signbit arguments here.
>         * match.pd: Add rules previously handled by fold_builtin_signbit.
>
> gcc/testsuite/
>         PR tree-optimization/68097
>         * gcc.dg/torture/builtin-nonneg-1.c: Skip at -O0.  Add
>         --param max-ssa-name-query-depth=3 to dg-options.
>
> diff --git a/gcc/builtins.c b/gcc/builtins.c
> index e5a00ee..ae7e7ef 100644
> --- a/gcc/builtins.c
> +++ b/gcc/builtins.c
> @@ -162,7 +162,6 @@ static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
>  static tree fold_builtin_memcmp (location_t, tree, tree, tree);
>  static tree fold_builtin_strcmp (location_t, tree, tree);
>  static tree fold_builtin_strncmp (location_t, tree, tree, tree);
> -static tree fold_builtin_signbit (location_t, tree, tree);
>  static tree fold_builtin_isascii (location_t, tree);
>  static tree fold_builtin_toascii (location_t, tree);
>  static tree fold_builtin_isdigit (location_t, tree);
> @@ -7782,40 +7781,6 @@ fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
>    return NULL_TREE;
>  }
>
> -/* Fold function call to builtin signbit, signbitf or signbitl with argument
> -   ARG.  Return NULL_TREE if no simplification can be made.  */
> -
> -static tree
> -fold_builtin_signbit (location_t loc, tree arg, tree type)
> -{
> -  if (!validate_arg (arg, REAL_TYPE))
> -    return NULL_TREE;
> -
> -  /* If ARG is a compile-time constant, determine the result.  */
> -  if (TREE_CODE (arg) == REAL_CST
> -      && !TREE_OVERFLOW (arg))
> -    {
> -      REAL_VALUE_TYPE c;
> -
> -      c = TREE_REAL_CST (arg);
> -      return (REAL_VALUE_NEGATIVE (c)
> -             ? build_one_cst (type)
> -             : build_zero_cst (type));
> -    }
> -
> -  /* If ARG is non-negative, the result is always zero.  */
> -  if (tree_expr_nonnegative_p (arg))
> -    return omit_one_operand_loc (loc, type, integer_zero_node, arg);
> -
> -  /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
> -  if (!HONOR_SIGNED_ZEROS (arg))
> -    return fold_convert (type,
> -                        fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
> -                       build_real (TREE_TYPE (arg), dconst0)));
> -
> -  return NULL_TREE;
> -}
> -
>  /* Fold function call to builtin copysign, copysignf or copysignl with
>     arguments ARG1 and ARG2.  Return NULL_TREE if no simplification can
>     be made.  */
> @@ -9124,7 +9089,11 @@ fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
>        return fold_builtin_bitop (fndecl, arg0);
>
>      CASE_FLT_FN (BUILT_IN_SIGNBIT):
> -      return fold_builtin_signbit (loc, arg0, type);
> +      if (TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0))
> +       return (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))
> +               ? build_one_cst (type)
> +               : build_zero_cst (type));
> +      break;
>
>      CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
>        return fold_builtin_significand (loc, arg0, type);
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 303889b..e50f5bb 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -102,6 +102,7 @@ DEFINE_MATH_FN (COPYSIGN)
>  DEFINE_MATH_FN (CABS)
>  DEFINE_MATH_FN (TRUNC)
>  DEFINE_MATH_FN (NEARBYINT)
> +DEFINE_MATH_FN (SIGNBIT)
>
>  DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
>  DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
> @@ -2924,3 +2925,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
>     (op @0 (ext @1 @2)))))
>
> +(simplify
> + /* signbit(x) -> 0 if x is nonnegative.  */
> + (SIGNBIT tree_expr_nonnegative_p@0)
> + { integer_zero_node; })
> +
> +(simplify
> + /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
> + (SIGNBIT @0)
> + (if (!HONOR_SIGNED_ZEROS (@0))
> +  (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
> diff --git a/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c b/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
> index 8a3286a..46f6fa6 100644
> --- a/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
> +++ b/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
> @@ -6,7 +6,11 @@
>     Written by Kaveh Ghazi, 2004-03-10.  */
>
>  /* { dg-do link } */
> -/* { dg-options "-ffast-math" } */
> +/* This test needs more recursion than the default.  PR 68097 is about
> +   adding proper range information for reals, so that no recursion
> +   would be necessary.  */
> +/* { dg-options "-ffast-math --param max-ssa-name-query-depth=3" } */
> +/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
>
>  #define PROTOTYPE_RTYPE(FN,RTYPE) extern RTYPE FN(double); \
>    extern RTYPE FN##f(float); \
>

Patch
diff mbox

diff --git a/gcc/builtins.c b/gcc/builtins.c
index e5a00ee..ae7e7ef 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -162,7 +162,6 @@  static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
 static tree fold_builtin_strcmp (location_t, tree, tree);
 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
-static tree fold_builtin_signbit (location_t, tree, tree);
 static tree fold_builtin_isascii (location_t, tree);
 static tree fold_builtin_toascii (location_t, tree);
 static tree fold_builtin_isdigit (location_t, tree);
@@ -7782,40 +7781,6 @@  fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
   return NULL_TREE;
 }
 
-/* Fold function call to builtin signbit, signbitf or signbitl with argument
-   ARG.  Return NULL_TREE if no simplification can be made.  */
-
-static tree
-fold_builtin_signbit (location_t loc, tree arg, tree type)
-{
-  if (!validate_arg (arg, REAL_TYPE))
-    return NULL_TREE;
-
-  /* If ARG is a compile-time constant, determine the result.  */
-  if (TREE_CODE (arg) == REAL_CST
-      && !TREE_OVERFLOW (arg))
-    {
-      REAL_VALUE_TYPE c;
-
-      c = TREE_REAL_CST (arg);
-      return (REAL_VALUE_NEGATIVE (c)
-	      ? build_one_cst (type)
-	      : build_zero_cst (type));
-    }
-
-  /* If ARG is non-negative, the result is always zero.  */
-  if (tree_expr_nonnegative_p (arg))
-    return omit_one_operand_loc (loc, type, integer_zero_node, arg);
-
-  /* If ARG's format doesn't have signed zeros, return "arg < 0.0".  */
-  if (!HONOR_SIGNED_ZEROS (arg))
-    return fold_convert (type,
-			 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
-			build_real (TREE_TYPE (arg), dconst0)));
-
-  return NULL_TREE;
-}
-
 /* Fold function call to builtin copysign, copysignf or copysignl with
    arguments ARG1 and ARG2.  Return NULL_TREE if no simplification can
    be made.  */
@@ -9124,7 +9089,11 @@  fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
       return fold_builtin_bitop (fndecl, arg0);
 
     CASE_FLT_FN (BUILT_IN_SIGNBIT):
-      return fold_builtin_signbit (loc, arg0, type);
+      if (TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0))
+	return (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))
+		? build_one_cst (type)
+		: build_zero_cst (type));
+      break;
 
     CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
       return fold_builtin_significand (loc, arg0, type);
diff --git a/gcc/match.pd b/gcc/match.pd
index 303889b..e50f5bb 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -102,6 +102,7 @@  DEFINE_MATH_FN (COPYSIGN)
 DEFINE_MATH_FN (CABS)
 DEFINE_MATH_FN (TRUNC)
 DEFINE_MATH_FN (NEARBYINT)
+DEFINE_MATH_FN (SIGNBIT)
 
 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
@@ -2924,3 +2925,13 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
    (op @0 (ext @1 @2)))))
 
+(simplify
+ /* signbit(x) -> 0 if x is nonnegative.  */
+ (SIGNBIT tree_expr_nonnegative_p@0)
+ { integer_zero_node; })
+
+(simplify
+ /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
+ (SIGNBIT @0)
+ (if (!HONOR_SIGNED_ZEROS (@0))
+  (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c b/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
index 8a3286a..46f6fa6 100644
--- a/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
+++ b/gcc/testsuite/gcc.dg/torture/builtin-nonneg-1.c
@@ -6,7 +6,11 @@ 
    Written by Kaveh Ghazi, 2004-03-10.  */
 
 /* { dg-do link } */
-/* { dg-options "-ffast-math" } */
+/* This test needs more recursion than the default.  PR 68097 is about
+   adding proper range information for reals, so that no recursion
+   would be necessary.  */
+/* { dg-options "-ffast-math --param max-ssa-name-query-depth=3" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
 
 #define PROTOTYPE_RTYPE(FN,RTYPE) extern RTYPE FN(double); \
   extern RTYPE FN##f(float); \