From patchwork Mon Oct 26 09:38:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 535778 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 4E908140E4A for ; Mon, 26 Oct 2015 20:38:53 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=QVlYDYQU; 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=rHpaEtLTwbdjizM8 wMYSfX/yrEXeaD/9ys8/i0W8uzfs1Ynw57Bk6taC7vZX+E3Yi6TmZRWmg2G9ZNct woEsa/FDYJmHqLlK7BwfRAJmS+9wBtX9Od02WhiQu8mp3Gs2g75XfXHGqRGcWIax YGXUQVjNTQnZr8s+jy81q3y2xVc= 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=IdDSGslX4aZ3lfw85+Im0s Uz/YE=; b=QVlYDYQUvH60wZvXYg/upSPW5EKcswaUlWgVgx7PVQupPh842ftmQB SwrD2/66pwCKj9CBhcD1K+piThvCKeHpGB7h4jYi5e5g6U6/bIkpVl74iCwEW575 Eimk310bJWyOXuAxMRezsXsMGnl2eIdb62nKsmK1Y2CAuICEwScL4= Received: (qmail 66134 invoked by alias); 26 Oct 2015 09:38:39 -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 66121 invoked by uid 89); 26 Oct 2015 09:38:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=BAYES_00, 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) (207.82.80.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Oct 2015 09:38:37 +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-36-cbqcX7YqRou8_YNOtlmlzA-1; Mon, 26 Oct 2015 09:38:31 +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:38:31 +0000 From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Move hypot folds to match.pd Date: Mon, 26 Oct 2015 09:38:30 +0000 Message-ID: <87wpuavujt.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: cbqcX7YqRou8_YNOtlmlzA-1 Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi. OK to install? Thanks, Richard gcc/ * builtins.c (fold_builtin_hypot): Delete. (fold_builtin_2): Handle constant hypot arguments here. * match.pd: Fold hypot(x, 0) and hypot(0, x) to x. Canonicalize hypot(x, x) to fabs(x)*sqrt(2). diff --git a/gcc/builtins.c b/gcc/builtins.c index f947f1e..64106a1 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -7475,39 +7475,6 @@ fold_builtin_bswap (tree fndecl, tree arg) return NULL_TREE; } -/* Fold a builtin function call to hypot, hypotf, or hypotl. Return - NULL_TREE if no simplification can be made. */ - -static tree -fold_builtin_hypot (location_t loc, tree arg0, tree arg1, tree type) -{ - tree res; - - if (!validate_arg (arg0, REAL_TYPE) - || !validate_arg (arg1, REAL_TYPE)) - return NULL_TREE; - - /* Calculate the result when the argument is a constant. */ - if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot))) - return res; - - /* If either argument is zero, hypot is fabs of the other. */ - if (real_zerop (arg0)) - return fold_build1_loc (loc, ABS_EXPR, type, arg1); - else if (real_zerop (arg1)) - return fold_build1_loc (loc, ABS_EXPR, type, arg0); - - /* hypot(x,x) -> fabs(x)*sqrt(2). */ - if (flag_unsafe_math_optimizations - && operand_equal_p (arg0, arg1, OEP_PURE_SAME)) - return fold_build2_loc (loc, MULT_EXPR, type, - fold_build1_loc (loc, ABS_EXPR, type, arg0), - build_real_truncate (type, dconst_sqrt2 ())); - - return NULL_TREE; -} - - /* Fold a builtin function call to pow, powf, or powl. Return NULL_TREE if no simplification can be made. */ static tree @@ -9456,7 +9423,10 @@ fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1) break; CASE_FLT_FN (BUILT_IN_HYPOT): - return fold_builtin_hypot (loc, arg0, arg1, type); + if (validate_arg (arg0, REAL_TYPE) + && validate_arg (arg1, REAL_TYPE)) + return do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot); + break; CASE_FLT_FN (BUILT_IN_CPOW): if (validate_arg (arg0, COMPLEX_TYPE) diff --git a/gcc/match.pd b/gcc/match.pd index 00c6e7c..8cd55f6 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2520,6 +2520,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (RINT integer_valued_real_p@0) @0)) +/* hypot(x,0) and hypot(0,x) -> abs(x). */ +(simplify + (hypot:c @0 real_zerop@1) + (abs @0)) + /* Canonicalization of sequences of math builtins. These rules represent IL simplifications but are not necessarily optimizations. @@ -2618,6 +2623,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (CABS (complex @0 @0)) (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) + /* hypot(x,x) -> fabs(x)*sqrt(2). */ + (simplify + (HYPOT @0 @0) + (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) + /* cexp(x+yi) -> exp(x)*cexpi(y). */ (for cexps (CEXP) exps (EXP)