From patchwork Fri Jul 21 15:55:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 792201 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-458662-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Xktd9woM"; dkim-atps=neutral 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 3xDb411f41z9sRg for ; Sat, 22 Jul 2017 01:56:48 +1000 (AEST) 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:in-reply-to:references; q=dns; s= default; b=t3v1dXK6Qv4jwaiV7zk1neot3AwlBsko5EZ3pRCkPyaaiTdVa6LXW FMhNoGpcRi3dBChRujtBo3ng0PlB1ZqsG9Ci3HJqviFeuDLFEnTBqtZ22bn3nlC4 ewZCnw+S/zYS7Xjcb7N6iA+biFgQzkEkwrVqhcUbg4jl93X7vBrY7s= 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:in-reply-to:references; s=default; bh=PzKqs4ujl73KtV3rplTQYKdcpw4=; b=Xktd9woMysYmrSmbU7pEc0jvzBlk t5pj3CoLUkvy+sQPL3deNVbfDi1mzQUQLC8so5xyuMQwlymD6DDESK1NYfr1Raun feWpvC4+DPk+kRhKwVgbcpqqFYAwwQX3SHvW60nB1po8d1P0cyOCf1H7F6D6iyYk SKIJVXr2ZGxInv0= Received: (qmail 94405 invoked by alias); 21 Jul 2017 15:56:27 -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 94367 invoked by uid 89); 21 Jul 2017 15:56:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=5, 4, 2313 X-HELO: smtp.ispras.ru Received: from bran.ispras.ru (HELO smtp.ispras.ru) (83.149.199.196) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 21 Jul 2017 15:56:24 +0000 Received: from monopod.intra.ispras.ru (monopod.intra.ispras.ru [10.10.3.121]) by smtp.ispras.ru (Postfix) with ESMTP id C31E65FB8B for ; Fri, 21 Jul 2017 18:56:21 +0300 (MSK) From: Alexander Monakov To: gcc-patches@gcc.gnu.org Subject: [PATCH v2 2/2] combine successive multiplications by constants Date: Fri, 21 Jul 2017 18:55:50 +0300 Message-Id: <20170721155550.5846-2-amonakov@ispras.ru> In-Reply-To: <20170721155550.5846-1-amonakov@ispras.ru> References: <20170721155550.5846-1-amonakov@ispras.ru> Previous revision here: https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01090.html Reassociate X * CST1 * CST2 to X * (CST1 * CST2). Changed in this revision: - remove the check for @2 being 0 or -1 * match.pd ((X * CST1) * CST2): Simplify to X * (CST1 * CST2). testsuite: * gcc.dg/tree-ssa/assoc-2.c: Enhance. * gcc.dg/tree-ssa/slsr-4.c: Adjust. --- gcc/match.pd | 13 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c | 13 ++++++++++++- gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c | 8 ++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 39e1e5c..732b80c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -284,6 +284,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) { build_zero_cst (type); }))))) +/* Combine successive multiplications. Similar to above, but handling + overflow is different. */ +(simplify + (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) + (with { + bool overflow_p; + wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p); + } + /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN, + otherwise undefined overflow implies that @0 must be zero. */ + (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type)) + (mult @0 { wide_int_to_tree (type, mul); })))) + /* Optimize A / A to 1.0 if we don't care about NaNs or Infinities. */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c index a92c882..cc0e9d4 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c @@ -5,4 +5,15 @@ int f0(int a, int b){ return a * 33 * b * 55; } -/* { dg-final { scan-tree-dump-times "mult_expr" 2 "gimple" } } */ +int f1(int a){ + a *= 33; + return a * 55; +} + +int f2(int a, int b){ + a *= 33; + return a * b * 55; +} + +/* { dg-final { scan-tree-dump-times "mult_expr" 7 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "mult_expr" 5 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c index 17d7b4c..1e943b7 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c @@ -23,13 +23,9 @@ f (int i) foo (y); } -/* { dg-final { scan-tree-dump-times "\\* 4" 1 "slsr" } } */ -/* { dg-final { scan-tree-dump-times "\\* 10" 1 "slsr" } } */ -/* { dg-final { scan-tree-dump-times "\\+ 20;" 1 "slsr" } } */ +/* { dg-final { scan-tree-dump-times "\\* 40" 1 "slsr" } } */ /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "slsr" } } */ -/* { dg-final { scan-tree-dump-times "\\- 16;" 1 "slsr" } } */ /* { dg-final { scan-tree-dump-times "\\- 160" 1 "slsr" } } */ -/* { dg-final { scan-tree-dump-times "\\* 4" 1 "optimized" } } */ -/* { dg-final { scan-tree-dump-times "\\* 10" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\\* 40" 1 "optimized" } } */ /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "optimized" } } */ /* { dg-final { scan-tree-dump-times "\\+ 40" 1 "optimized" } } */