From patchwork Fri Feb 26 03:02:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 62917 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp490675lbc; Thu, 25 Feb 2016 19:03:24 -0800 (PST) X-Received: by 10.98.0.194 with SMTP id 185mr67771966pfa.139.1456455804704; Thu, 25 Feb 2016 19:03:24 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 29si16558750pft.41.2016.02.25.19.03.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Feb 2016 19:03:24 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-422208-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-422208-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-422208-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:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=DhOYwz+TEYroXOvU8PqfbtdZnjQvl/14lGoeaqMUK0mUmvO4TH tfdJ8uXSeKDBQ/ayogNrqTz027msNVoD8NHEnl6TaXz16SGRwar/j1Xz49/UrHBd Ihzit/MPY1yKlbcwexmOybQ6XwmKyudnZ+BLUtMFEyOxuENy76zBHfW3M= 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 :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=3gIwXCAS34Geb45xwKSStEsQiNU=; b=FWUe30Nex3/JgRhanFZT MuZpLe/RpF2p4SOXcwkttKsHHfzKSt1S7+GTT+ARSdopvL/m0NG7CSrz/x2bOQ9Z qNzNCU5u43yFuxRck4c57tASK97Vyak+RFE/fMGOdi9SFGJeK1xqmLcSeuzWPb9i VVrRqWO/5pnDftPUd2CApSs= Received: (qmail 74935 invoked by alias); 26 Feb 2016 03:03:11 -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 74912 invoked by uid 89); 26 Feb 2016 03:03:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL, BAYES_00, GAPPY_SUBJECT, SPF_PASS autolearn=no version=3.3.2 spammy=tmp2, inculding, 197, H*MI:56CFC059 X-HELO: mail-pf0-f177.google.com Received: from mail-pf0-f177.google.com (HELO mail-pf0-f177.google.com) (209.85.192.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 26 Feb 2016 03:03:10 +0000 Received: by mail-pf0-f177.google.com with SMTP id q63so43769793pfb.0 for ; Thu, 25 Feb 2016 19:03:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:cc:message-id:date:user-agent :mime-version:content-type; bh=L/0Ui37nYHvzuMbQHslJZTzeZLtbcLqEPKWdsHLxbkM=; b=m8o0ehkIgPLp2gR8IHCrBsEZEoRui7lhjTrEFVPnXqeC8zY0E6G6MLqKVrEOEtFlyX GMjjQe/FRbNEq292KcQIKL3DYrKLEjES21XikcOxsUaJxFEiJV2E9fQQcaUEtF5btssC f41e1sicuT8nFpS6WbPIfJ7XPc7bAFv+1FoeDFPRw92Botun1MjRYsdDzxlTZnbX1uCC w0idRcFrdr8zNp2VNqzZeT0s24d0o4307DsWd6CW0/vCv9PAsEhMMlC/xjEV2e1p2bgQ Wju1AHHbHEOBshwuUV2SLPnxCmHXXcvxxIdPrKsaJItRUmjmsoZWIvwAtr2norBoHYcY BERQ== X-Gm-Message-State: AG10YOT1otrlBAMWDtsI/ynDn6nzrhg8pH03jc0BjSSdpVH5IOYS2iyfD7t7zN7BtqzTsTo3 X-Received: by 10.98.42.20 with SMTP id q20mr68816381pfq.146.1456455788528; Thu, 25 Feb 2016 19:03:08 -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 w9sm15115492pfa.21.2016.02.25.19.03.06 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 25 Feb 2016 19:03:07 -0800 (PST) From: kugan Subject: [RFC][PATCH][PR63586] Convert x+x+x+x into 4*x To: "gcc-patches@gcc.gnu.org" Cc: Richard Biener Message-ID: <56CFC059.40108@linaro.org> Date: Fri, 26 Feb 2016 14:02:49 +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+x+x+x -> 4*x as reported in PR63586. Regression tested and bootstrapped on x86-64-linux-gnu with no new regressions. Is this OK for next stage1? Thanks, Kugan gcc/testsuite/ChangeLog: 2016-02-26 Kugan Vivekanandarajah PR middle-end/63586 * gcc.dg/tree-ssa/reassoc-14.c: Fix multiply count. gcc/ChangeLog: 2016-02-26 Kugan Vivekanandarajah PR middle-end/63586 * tree-ssa-reassoc.c (transform_add_to_multiply): New. (reassociate_bb): Call transform_add_to_multiply. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-14.c b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-14.c index 62802d1..16ebc86 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-14.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-14.c @@ -19,6 +19,7 @@ unsigned int test2 (unsigned int x, unsigned int y, unsigned int z, return tmp1 + tmp2 + tmp3; } -/* There should be one multiplication left in test1 and three in test2. */ +/* There should be two multiplication left in test1 (inculding one generated + when converting addition to multiplication) and three in test2. */ -/* { dg-final { scan-tree-dump-times "\\\*" 4 "reassoc1" } } */ +/* { dg-final { scan-tree-dump-times "\\\*" 5 "reassoc1" } } */ diff --git a/gcc/tree-ssa-reassoc.c b/gcc/tree-ssa-reassoc.c index dfd0da1..2454b9d 100644 --- a/gcc/tree-ssa-reassoc.c +++ b/gcc/tree-ssa-reassoc.c @@ -1698,6 +1698,61 @@ eliminate_redundant_comparison (enum tree_code opcode, return false; } +/* Recursively transoform repeated addition of same values into multiply with + constant. */ +static void +transform_add_to_multiply (gimple_stmt_iterator *gsi, gimple *stmt, vec *ops) +{ + operand_entry *oe; + tree op = NULL_TREE; + int i, start = -1, end = 0, count = 0; + + /* Look for repeated operands. */ + FOR_EACH_VEC_ELT (*ops, i, oe) + { + if (start == -1) + { + count = 1; + op = oe->op; + start = i; + } + else if (operand_equal_p (oe->op, op, 0)) + { + count++; + end = i; + } + else if (count == 1) + { + count = 1; + op = oe->op; + start = i; + } + else + break; + } + + if (count > 1) + { + /* Convert repeated operand addition to multiplication. */ + for (i = end; i >= start; --i) + ops->unordered_remove (i); + tree tmp = make_temp_ssa_name (TREE_TYPE (op), NULL, "reassocmul"); + gassign *mul_stmt = gimple_build_assign (tmp, MULT_EXPR, + op, build_int_cst (TREE_TYPE(op), count)); + gimple_set_location (mul_stmt, gimple_location (stmt)); + gimple_set_uid (mul_stmt, gimple_uid (stmt)); + gsi_insert_before (gsi, mul_stmt, GSI_SAME_STMT); + oe = operand_entry_pool.allocate (); + oe->op = tmp; + oe->rank = get_rank (op) * count; + oe->id = 0; + oe->count = 1; + ops->safe_push (oe); + transform_add_to_multiply (gsi, stmt, ops); + } +} + + /* Perform various identities and other optimizations on the list of operand entries, stored in OPS. The tree code for the binary operation between all the operands is OPCODE. */ @@ -4854,6 +4909,12 @@ reassociate_bb (basic_block bb) && flag_unsafe_math_optimizations) powi_result = attempt_builtin_powi (stmt, &ops); + if (rhs_code == PLUS_EXPR) + { + transform_add_to_multiply (&gsi, stmt, &ops); + ops.qsort (sort_by_operand_rank); + } + /* If the operand vector is now empty, all operands were consumed by the __builtin_powi optimization. */ if (ops.length () == 0)