From patchwork Mon Jun 26 02:09:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 780578 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 3wwsvk4dnGz9s4s for ; Mon, 26 Jun 2017 12:10:09 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="f3mWQGEK"; 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 :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; q=dns; s=default; b=pTYD9CF0hzIS8N8 y2oDyOiT6Qt8BWDHUligVyqi9frLuUXr/HuvoJDXzx5FoT4quskyicWXnREIU61l urFNIMgVsao61pbQP9thdS8iMDrmxwNT/QeVfXCm/TQwUPmZ+Jzkx4cfDfUsJ/kL PDJHT+650jPpyqU3jBJZvUwz/hZE= 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 :mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; s=default; bh=PKI6IB44O2AUdquVjkmd9 3zkI3Y=; b=f3mWQGEKguEnbeufqud0V7NheGT4i1tj4Cl5fupKRzI31IYwnkjhb lolhc1k7Te44YbQasnGdxPTBhjQNc5jVFr4POpb9eJAzQbGB6rm2e3sug7021kj7 5/dKtrJiIhAOx0KJtCngdZ7BHnNmZfvrB+CtP7M60RXPu9vO68ySIY= Received: (qmail 24459 invoked by alias); 26 Jun 2017 02:09:59 -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 24432 invoked by uid 89); 26 Jun 2017 02:09:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-9.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*ian X-HELO: mail-yw0-f194.google.com Received: from mail-yw0-f194.google.com (HELO mail-yw0-f194.google.com) (209.85.161.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Jun 2017 02:09:56 +0000 Received: by mail-yw0-f194.google.com with SMTP id g124so2278020ywg.1 for ; Sun, 25 Jun 2017 19:09:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=/njMgUOdMx3uCEgI7koN57jRE1gy4Vz1Q/mzHRYbXag=; b=Gjh+6dW4rWY/nDeQvKsKQhS+ejRXqvNjZvPLh5TxtnPQOhC2deRcHY6jouQ3b3qL40 rxZNkzJXrVRD1+72adYSI/jiqoDte9BrfasEXbN2gXWA2GMmL43iXRE1tDmfSY4LhvpJ KIfP0o05HsDXmnAEaYa9aNIF2RqE84W0vWw2SNVPWu/+3lB6y2mnqT0pmEpRmb+XaIIt Csm6UM+TtVnU9Q7EVqcDCd2mhOhoFevVFZSislzVjZCRYdpYRcvEUxDUEzjPjiclWTTn VwRHqP71t6HoPhel7YxoZkg7Y7MT3/JVUjvxeXwzqNWdLBmBlrh6lpiquEJPvY9fjYXm RUMg== X-Gm-Message-State: AKS2vOySpdQbMNNicqwZtpvjADNR7lG63cZrLdNj/JkwsvnzbfTbjfMg 5F7ZXrsyDKPxh+RMGLs3Qzh+3SCqlw== X-Received: by 10.129.130.71 with SMTP id s68mr14791265ywf.42.1498442994797; Sun, 25 Jun 2017 19:09:54 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.47.200 with HTTP; Sun, 25 Jun 2017 19:09:54 -0700 (PDT) In-Reply-To: References: From: Andrew Pinski Date: Sun, 25 Jun 2017 19:09:54 -0700 Message-ID: Subject: Re: [GCC][PATCH][mid-end] Optimize x * copysign (1.0, y) [Patch (1/2)] To: Tamar Christina Cc: GCC Patches , nd , "law@redhat.com" , "ian@airs.com" , "rguenther@suse.de" X-IsSubscribed: yes On Sat, Jun 24, 2017 at 4:53 PM, Andrew Pinski wrote: > On Mon, Jun 12, 2017 at 12:56 AM, Tamar Christina > wrote: >> Hi All, >> >> this patch implements a optimization rewriting >> >> x * copysign (1.0, y) and >> x * copysign (-1.0, y) > > > This reminds me: > copysign(-1.0, y) can be just optimized to: > copysign(1.0, y) > > I did that in my patch here: > https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01860.html I updated the patch to handle all constants and not just -1.0. > > This should allow you to reduce the number of patterns needed to match here. > Note I still think we could do this in expand without a new > builtin/internal function. > I might go and code that up soonish. Also something like attached (NOTE this is NOT a full patch and needs the xorsign optabs part of your patch) should work for the expand side rather than creating a new builtin. There still needs to handling of the vector based copysign. But you should get the general idea. I would like to see more of these special expand patterns really. NOTE you can remove the target hook part and just check if xorsign optab is there. I don't know if that is what we want to do if not allow for generic expanding of this. Thanks, Andrew Pinski > > Thanks, > Andrew > >> >> to: >> >> x ^ (y & (1 << sign_bit_position)) >> >> This is done by creating a special builtin during matching and generate the >> appropriate instructions during expand. This new builtin is called XORSIGN. >> >> The expansion of xorsign depends on if the backend has an appropriate optab >> available. If this is not the case then we use a modified version of the existing >> copysign which does not take the abs value of the first argument as a fall back. >> >> This patch is a revival of a previous patch >> https://gcc.gnu.org/ml/gcc-patches/2015-10/msg00069.html >> >> Bootstrapped on both aarch64-none-linux-gnu and x86_64 with no issues. >> Regression done on aarch64-none-linux-gnu and no regressions. >> >> Ok for trunk? >> >> gcc/ >> 2017-06-07 Tamar Christina >> >> * builtins.def (BUILT_IN_XORSIGN, BUILT_IN_XORSIGNF): New. >> (BUILT_IN_XORSIGNL, BUILT_IN_XORSIGN_FLOAT_NX): Likewise. >> * match.pd (mult (COPYSIGN:s real_onep @0) @1): New simplifier. >> (mult (COPYSIGN:s real_mus_onep @0) @1): Likewise. >> (copysigns @0 (negate @1)): Likewise. >> * builtins.c (expand_builtin_copysign): Promoted local to argument. >> (expand_builtin): Added CASE_FLT_FN_FLOATN_NX (BUILT_IN_XORSIGN) and >> CASE_FLT_FN (BUILT_IN_XORSIGN). >> (BUILT_IN_COPYSIGN): Updated function call. >> * optabs.h (expand_copysign): New bool. >> (expand_xorsign): New. >> * optabs.def (xorsign_optab): New. >> * optabs.c (expand_copysign): New parameter. >> * fortran/f95-lang.c (xorsignl, xorsign, xorsignf): New. >> * fortran/mathbuiltins.def (XORSIGN): New. >> >> gcc/testsuite/ >> 2017-06-07 Tamar Christina >> >> * gcc.dg/tree-ssa/xorsign.c: New. >> * gcc.dg/xorsign_exec.c: New. >> * gcc.dg/vec-xorsign_exec.c: New. >> * gcc.dg/tree-ssa/reassoc-39.c (f2, f3): Updated constant to 2. Index: gcc/expr.c =================================================================== --- gcc/expr.c (revision 249619) +++ gcc/expr.c (working copy) @@ -8182,6 +8182,59 @@ return NULL_RTX; } +static bool +is_copysign_call_with_1 (gimple *call) +{ + if (!is_gimple_call (call)) + return false; + + if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)) + { + gcall *c = as_a (call); + tree decl = gimple_call_fndecl (call); + switch (DECL_FUNCTION_CODE (decl)) + { + CASE_FLT_FN (BUILT_IN_COPYSIGN): + CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN): + return real_one_p (gimple_call_arg (c, 0)); + default: + return false; + } + } +} + +static rtx +maybe_expand_mult_copysign (tree treeop0, tree treeop1, rtx target) +{ + tree type = TREE_TYPE (treeop0); + rtx op0, op1; + + if (!SCALAR_FLOAT_TYPE_P (type) + && VECTOR_FLOAT_TYPE_P (type)) + return NULL; + + if (HONOR_SNANS (type)) + return NULL; + + if (!targetm.expand_mult_copysign_xor ()) + return NULL; + + if (TREE_CODE (treeop0) == SSA_NAME) + { + gimple *call0 = SSA_NAME_DEF_STMT (treeop0); + if (is_copysign_call_with_1 (call0)) + { + gcall *c = as_a (call0); + treeop0 = gimple_call_arg (c, 1); + expand_operands (treeop1, treeop0, NULL_RTX, &op0, &op1, EXPAND_NORMAL); + return expand_copysign (op0, op1, target, true); + } + } + + return NULL; +} + + rtx expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode, enum expand_modifier modifier) @@ -8791,6 +8844,10 @@ if (modifier == EXPAND_STACK_PARM) target = 0; + temp = maybe_expand_mult_copysign (treeop0, treeop1); + if (temp) + return temp; + expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL); return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp)); Index: gcc/target.def =================================================================== --- gcc/target.def (revision 249619) +++ gcc/target.def (working copy) @@ -2640,6 +2640,13 @@ default_have_conditional_execution) DEFHOOK +(expand_mult_copysign_xor, + "This target hook returns true if the target wants to use xor's\n +to expand x * copysign (1., y) as x ^ (y & (1 << sign_bit_position)).\n" + bool, (void), + hook_bool_void_false) + +DEFHOOK (gen_ccmp_first, "This function prepares to emit a comparison insn for the first compare in a\n\ sequence of conditional comparisions. It returns an appropriate comparison\n\