From patchwork Fri Feb 26 03:02:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 62916 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp490454lbc; Thu, 25 Feb 2016 19:02:45 -0800 (PST) X-Received: by 10.98.13.86 with SMTP id v83mr68139897pfi.162.1456455765361; Thu, 25 Feb 2016 19:02:45 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id a5si16544658pat.19.2016.02.25.19.02.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Feb 2016 19:02:45 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-422207-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-return-422207-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-422207-patch=linaro.org@gcc.gnu.org; dkim=pass header.i=@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=rUCfiKoEJIrF10BxbREseGg8+c0uQuNrANmaqvTOruwmUxC01l s0xactnmxU343ejm1yDhR5Zsmxwea7hor3M5kFvnTFSPPJpqz18KNAkDc8cPZKuk T4AEp4mlyQ0BMcNtEFw0KtK2G24sDa6uLYWjq8gKKvNVWInqDNP0QOXRs= 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:to:cc :from:subject:message-id:date:mime-version:content-type; s= default; bh=FJ2DWuEQ7xV6IShB0kaJVodA0Ys=; b=FU/OJ8N0dxgUicBEayGY 6ukFpr/ZeqcfLyunnwHCsfwcQuWAza7VGkCPkLQXFNhssiW3H49AtxBKtwt6BfY5 O7Ytmi2PDif8Z0RLLOtxeCsKNSFu57a9GOemEzO5zQcLQWpNhL/psbzeBJE3B16N KJi3Z9NT70jXnaP0svFT3G0= Received: (qmail 72510 invoked by alias); 26 Feb 2016 03:02:31 -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 72494 invoked by uid 89); 26 Feb 2016 03:02:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 spammy=20160226, 2016-02-26, PLUS_EXPR, plus_expr X-HELO: mail-pf0-f180.google.com Received: from mail-pf0-f180.google.com (HELO mail-pf0-f180.google.com) (209.85.192.180) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 26 Feb 2016 03:02:29 +0000 Received: by mail-pf0-f180.google.com with SMTP id c10so45235697pfc.2 for ; Thu, 25 Feb 2016 19:02:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-type; bh=oJv+GY+kSPnUlXLNWKEyIKJu1orRHikBir9KIkPoffc=; b=IWCiyV7Yt4m9hW2ZfWFc8wfEBZzBZOawyCZXwCnBEBGtfoZRF1bLFiK8iZLQPhJTbO bydUvmj43+lI+sICpSDN6bg0X2KXXn97TC/cA/vK/6otd8yZHXdsd20uwUbYH1OxhKUp 0DsnLpVpyda0V5XzUQ5407n+NP2j3fXtCqQF6G0IZMwBIo2UFcNVgLL3iGEOzLbQbil1 oqpReOFCEgg8iKX4X04QtBBPSF7ozzSjDkMKHzPr5y5deT1lS1l8790AKVNwnW3LYaGi AWVPVGKg4zMSmH1Qs1gF2HqYnXVgMg1VBBuRdWF+3m9hZbugjMPen8xx8NgxXDHIWqnc 7gfQ== X-Gm-Message-State: AG10YOQocAn/pt0Yi03aTKkAqycO/ka7j5U/oudrFx8EEclhtN1wif9MQMTMpIjLNXTbd/+p X-Received: by 10.98.70.139 with SMTP id o11mr68218648pfi.123.1456455747243; Thu, 25 Feb 2016 19:02:27 -0800 (PST) Received: from [10.1.1.5] (58-6-183-210.dyn.iinet.net.au. [58.6.183.210]) by smtp.googlemail.com with ESMTPSA id 82sm15082413pfn.89.2016.02.25.19.02.24 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 25 Feb 2016 19:02:25 -0800 (PST) To: "gcc-patches@gcc.gnu.org" Cc: Richard Biener From: kugan Subject: [RFC][PATCH][PR40921] Convert x + (-y * z * z) into x - y * z * z Message-ID: <56CFC02F.2070801@linaro.org> Date: Fri, 26 Feb 2016 14:02:07 +1100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 X-IsSubscribed: yes Hi, This is an attempt to fix missed optimization: x + (-y * z * z) => x - y * z * z as reported in PR40921. Regression tested and bootstrapped on x86-64-linux-gnu with no new regressions. Is this OK for next stage1? Thanks, Kugan gcc/ChangeLog: 2016-02-26 Kugan Vivekanandarajah PR middle-end/40921 * tree-ssa-reassoc.c (propagate_neg_to_sub_or_add): New. (reassociate_bb): Call propagate_neg_to_sub_or_add. gcc/testsuite/ChangeLog: 2016-02-26 Kugan Vivekanandarajah PR middle-end/40921 * gcc.dg/tree-ssa/pr40921.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c b/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c index e69de29..6a3529b 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c @@ -0,0 +1,11 @@ + +/* PR middle-end/40921. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-reassoc1 -fno-rounding-math" } */ + +double foo (double x, double y, double z) +{ + return x + (-y * z*z); +} + +/* { dg-final { scan-tree-dump-times "= -" 0 "reassoc1" } } */ diff --git a/gcc/tree-ssa-reassoc.c b/gcc/tree-ssa-reassoc.c index e54700e..f99635b 100644 --- a/gcc/tree-ssa-reassoc.c +++ b/gcc/tree-ssa-reassoc.c @@ -4784,6 +4784,78 @@ transform_stmt_to_multiply (gimple_stmt_iterator *gsi, gimple *stmt, } } +/* Propagate NEGATE_EXPR to MINUS_EXPR/PLUS_EXPR when the neageted + expression is multiplied and used in MINUS_EXPR/PLUS_EXPR. */ +static void +propagate_neg_to_sub_or_add (gimple_stmt_iterator *gsi, gimple *stmt) +{ + tree lhs = gimple_assign_lhs (stmt); + tree rhs1, rhs2, mult_lhs; + gimple *use_stmt; + gimple *use_stmt2; + use_operand_p use; + enum tree_code code; + gassign *g; + + /* Note that -frounding-math should disable the proposed + optimization. */ + if (flag_rounding_math) + return; + + if (!single_imm_use (lhs, &use, &use_stmt)) + return; + + if (!is_gimple_assign (use_stmt)) + return; + + code = gimple_assign_rhs_code (use_stmt); + if (code != MULT_EXPR) + return; + mult_lhs = gimple_assign_lhs (use_stmt); + while (code == MULT_EXPR) + { + if (!single_imm_use (mult_lhs, &use, &use_stmt2)) + break; + if (!is_gimple_assign (use_stmt2)) + break; + code = gimple_assign_rhs_code (use_stmt2); + mult_lhs = gimple_assign_lhs (use_stmt2); + use_stmt = use_stmt2; + } + + if (code != PLUS_EXPR + && code != MINUS_EXPR) + return; + + lhs = gimple_assign_lhs (use_stmt); + rhs1 = gimple_assign_rhs1 (use_stmt); + rhs2 = gimple_assign_rhs2 (use_stmt); + + if (rhs1 == USE_FROM_PTR (use)) + { + if (code == MINUS_EXPR) + return; + std::swap (rhs1, rhs2); + code = MINUS_EXPR; + } + else + { + if (code == PLUS_EXPR) + code = MINUS_EXPR; + else + code = PLUS_EXPR; + } + + g = gimple_build_assign (lhs, code, rhs1, rhs2); + gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); + gsi_replace (&gsi2, g, true); + + lhs = gimple_assign_lhs (stmt); + rhs1 = gimple_assign_rhs1 (stmt); + g = gimple_build_assign (lhs, SSA_NAME, rhs1); + gsi_replace (gsi, g, true); +} + /* Reassociate expressions in basic block BB and its post-dominator as children. @@ -4809,6 +4881,11 @@ reassociate_bb (basic_block bb) { tree lhs, rhs1, rhs2; enum tree_code rhs_code = gimple_assign_rhs_code (stmt); + if (rhs_code == NEGATE_EXPR) + { + propagate_neg_to_sub_or_add (&gsi, stmt); + continue; + } /* If this is not a gimple binary expression, there is nothing for us to do with it. */ @@ -4884,6 +4961,7 @@ reassociate_bb (basic_block bb) if (rhs_code == MULT_EXPR) attempt_builtin_copysign (&ops); + if (reassoc_insert_powi_p && rhs_code == MULT_EXPR && flag_unsafe_math_optimizations)