From b4838d77014a4bb6e9d3cd4a1e31ab303de268b9 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Tue, 27 Oct 2015 09:30:32 +0000 Subject: [PATCH] Move pow folds to match.pd Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi. 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. From-SVN: r229408 --- gcc/ChangeLog | 8 + gcc/builtins.c | 142 +++--------------- gcc/match.pd | 56 ++++++- gcc/testsuite/ChangeLog | 4 + gcc/testsuite/gcc.dg/torture/builtin-math-1.c | 1 + 5 files changed, 88 insertions(+), 123 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 442cd85de9c..f26ea3de501 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2015-10-27 Richard Sandiford + + * 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. + 2015-10-27 Richard Sandiford * builtins.c (fold_builtin_hypot): Delete. diff --git a/gcc/builtins.c b/gcc/builtins.c index 64106a1d150..88c0576edac 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 583193ac802..a2f9405c4ac 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2480,6 +2480,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 @@ -2488,7 +2489,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) @@ -2524,6 +2529,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. @@ -2615,7 +2625,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 @@ -2767,6 +2789,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/ChangeLog b/gcc/testsuite/ChangeLog index e1cb93ca871..e808200ad42 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2015-10-27 Richard Sandiford + + * gcc.dg/torture/builtin-math-1.c: Skip at -O0. + 2015-10-27 Thomas Schwinge PR c/64765 diff --git a/gcc/testsuite/gcc.dg/torture/builtin-math-1.c b/gcc/testsuite/gcc.dg/torture/builtin-math-1.c index d052f5347bc..4ecc98d5f5c 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); -- 2.30.2