From patchwork Mon Oct 26 09:44:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 535780 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 59017140E31 for ; Mon, 26 Oct 2015 20:45:08 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=SvJ0rKSX; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=GS/XyH9Eq3RUhfyb yoUnb3tpS9oV+F7gfZarbdJ/qowTo7eIo8rWHDZsJbcl7IHVZxbFC4zPd7VifMfO gbGrcvaymeOXEeOHap+TbQWULYhuCmvaZoU4nIad5DJ1LSXQtndPBeH6lcGrKknz goXjFC0p6efoX1Yu8DgIf1uKd8Y= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=zqVMr2Fpsj1OwiFIkdxsXD TqJE8=; b=SvJ0rKSXlk9YQosk20K2uzQEQpYCxgqhEIPDy0MVtRvlN5BCWEraYL d5/m1L4ylnPQH7l1rdB3OTg9vjo9JjROD+rG00OYe5sbzw7MMjkMaMtWqFISrmRh gKZZWcPZEe+h0MfV2q8/Lixpr/1jOYUk+4l+UjemzCfOcQMlrxYAQ= Received: (qmail 107562 invoked by alias); 26 Oct 2015 09:44:54 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 107549 invoked by uid 89); 26 Oct 2015 09:44:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.1 required=5.0 tests=AWL, BAYES_50, FREEMAIL_FROM, SPF_SOFTFAIL autolearn=no version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (146.101.78.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Oct 2015 09:44:51 +0000 Received: from cam-owa2.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-27-DzwQxKm_TMSt1d-oeyG-zw-1; Mon, 26 Oct 2015 09:44:43 +0000 Received: from localhost ([10.1.2.79]) by cam-owa2.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 26 Oct 2015 09:44:43 +0000 From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Move pow folds to match.pd Date: Mon, 26 Oct 2015 09:44:43 +0000 Message-ID: <87oafmvu9g.fsf@e105548-lin.cambridge.arm.com> User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 X-MC-Unique: DzwQxKm_TMSt1d-oeyG-zw-1 Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi. OK to install? 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);