From patchwork Thu Oct 1 13:57:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Greenhalgh X-Patchwork-Id: 54368 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f72.google.com (mail-la0-f72.google.com [209.85.215.72]) by patches.linaro.org (Postfix) with ESMTPS id B9B5B205D0 for ; Thu, 1 Oct 2015 13:57:45 +0000 (UTC) Received: by laer8 with SMTP id r8sf36730550lae.2 for ; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:sender :delivered-to:from:to:subject:date:message-id:mime-version :content-type:x-original-sender:x-original-authentication-results; bh=wnbCUeC/czN2AMcqGuv2av901P42Bexqo9/2T13BGzQ=; b=YXJv5E0YExVtIgQcvBOz/X5L2UYSXHx3edU/g5ypXvmWmhp7V+M2BPfcLKAj881Qwu ZQt/tTDIVbelr3DIckGu8nEWc8iHPXKPMoSxWOtlr70AdCp1eE4yQpWN08GrmBYVZ3n8 8aowDpOn6imXy8wE9FH1jRDXu9ollgOTB0wWbKYDNrohsuBU8afzmB7v1ZhC+FRnrlNI 64HybvA0RFsBnDfXkvOpZkcXh8Zdo7Avw4UX5sSrsyn/E3dWi3TwlVT8cym+9WEtNrZK t6+91U9XBB+LZTi1a4F0kgNY2HjgQqk3RK8MuZsa6gjOlOCLQn44AVAB/Kskkrf5LBA4 kJlw== X-Gm-Message-State: ALoCoQmJl5Fbz19vChVkeiNAIYfVouzV5L3fQWIU6qpSNuu3sUyS0WAR4PUqo/9icpXhJNiUb85t X-Received: by 10.180.210.212 with SMTP id mw20mr501830wic.3.1443707864447; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.141.198 with SMTP id p189ls117756lfd.79.gmail; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) X-Received: by 10.112.236.3 with SMTP id uq3mr2965970lbc.69.1443707864144; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) Received: from mail-lb0-x22b.google.com (mail-lb0-x22b.google.com. [2a00:1450:4010:c04::22b]) by mx.google.com with ESMTPS id dm8si2921763lac.39.2015.10.01.06.57.44 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Oct 2015 06:57:44 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22b as permitted sender) client-ip=2a00:1450:4010:c04::22b; Received: by lbbmp1 with SMTP id mp1so9768978lbb.1 for ; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) X-Received: by 10.25.19.73 with SMTP id j70mr1938612lfi.29.1443707864006; Thu, 01 Oct 2015 06:57:44 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp628298lbq; Thu, 1 Oct 2015 06:57:42 -0700 (PDT) X-Received: by 10.107.17.30 with SMTP id z30mr13013209ioi.97.1443707862763; Thu, 01 Oct 2015 06:57:42 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 35si4913920iok.27.2015.10.01.06.57.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Oct 2015 06:57:42 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-408848-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 104388 invoked by alias); 1 Oct 2015 13:57:30 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list 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 103749 invoked by uid 89); 1 Oct 2015 13:57:30 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham 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; Thu, 01 Oct 2015 13:57:28 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-8-MNQVKOBmTTyjqpn9Y9yW2Q-1; Thu, 01 Oct 2015 14:57:23 +0100 Received: from e107456-lin.cambridge.arm.com ([10.1.2.79]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Thu, 1 Oct 2015 14:57:23 +0100 From: James Greenhalgh To: gcc-patches@gcc.gnu.org Subject: [Patch match.pd] Add a simplify rule for x * copysign (1.0, y); Date: Thu, 1 Oct 2015 14:57:15 +0100 Message-Id: <1443707835-6888-1-git-send-email-james.greenhalgh@arm.com> MIME-Version: 1.0 X-MC-Unique: MNQVKOBmTTyjqpn9Y9yW2Q-1 X-IsSubscribed: yes X-Original-Sender: james.greenhalgh@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::22b as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 Hi, If it is cheap enough to treat a floating-point value as an integer and to do bitwise arithmetic on it (as it is for AArch64) we can rewrite: x * copysign (1.0, y) as: x ^ (y & (1 << sign_bit_position)) This patch implements that rewriting rule in match.pd, and a testcase expecting the transform. This is worth about 6% in 481.wrf for AArch64. I don't don't know enough about the x86 microarchitectures to know how productive this transformation is there. In Spec2006FP I didn't see any interesting results in either direction. Looking at code generation for the testcase I add, I think the x86 code generation looks worse, but I can't understand why it doesn't use a vector-side xor and load the mask vector-side. With that fixed up I think the code generation would look better - though as I say, I'm not an expert here... Bootstrapped on both aarch64-none-linux-gnu and x86_64 with no issues. OK for trunk? Thanks, James --- gcc/ 2015-10-01 James Greenhalgh * match.pd (mult (COPYSIGN:s real_onep @0) @1): New simplifier. gcc/testsuite/ 2015-10-01 James Greenhalgh * gcc.dg/tree-ssa/copysign.c: New. diff --git a/gcc/match.pd b/gcc/match.pd index bd5c267..d51ad2e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -61,6 +61,7 @@ along with GCC; see the file COPYING3. If not see (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL) (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL) (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL) +(define_operator_list COPYSIGN BUILT_IN_COPYSIGNF BUILT_IN_COPYSIGN BUILT_IN_COPYSIGNL) /* Simplifications of operations with one constant operand and simplifications to constants or single values. */ @@ -2079,6 +2080,21 @@ along with GCC; see the file COPYING3. If not see /* Simplification of math builtins. */ +/* Simplify x * copysign (1.0, y) -> x ^ (y & (1 << sign_bit_position)). */ +(simplify + (mult:c (COPYSIGN:s real_onep @0) @1) + (with + { + wide_int m = wi::min_value (TYPE_PRECISION (type), SIGNED); + tree tt + = build_nonstandard_integer_type (TYPE_PRECISION (type), + false); + tree mask = wide_int_to_tree (tt, m); + } + (view_convert (bit_xor (view_convert:tt @1) + (bit_and (view_convert:tt @0) + { mask; }))))) + /* fold_builtin_logarithm */ (if (flag_unsafe_math_optimizations) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/copysign.c b/gcc/testsuite/gcc.dg/tree-ssa/copysign.c new file mode 100644 index 0000000..b67f3c1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/copysign.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-gimple" } */ + +double +foo_d (double x, double y) +{ + return x * __builtin_copysign (1.0, y); +} + +float +foo_f (float x, float y) +{ + return x * __builtin_copysignf (1.0f, y); +} + +long double +foo_l (long double x, long double y) +{ + return x * __builtin_copysignl (1.0, y); +} + +/* { dg-final { scan-tree-dump-not "copysign" "gimple"} } */