From patchwork Sat Jul 15 17:15:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 788933 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 3x8x6W2wLYz9s7m for ; Sun, 16 Jul 2017 03:16:18 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="MNeEAH7s"; 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:date :from:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; q=dns; s=default; b=mO05eTPRvpxji8X5 3yfPnekP3eFDWEPXbs8StwhLasTiXIhZ3JpSUO2JRSGV0xHCfCrVeK22bLILeQBJ KypkbdYdY4wIkC7xwVYixRhmTIJn1Eo3faWE/6jTVKsxqmpvHdsGS4VRo2VKjRn+ eKbeCI8MpgpHr1W9D6N7qDQ4Aj0= 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:date :from:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; s=default; bh=6GgwFZsbe5TPBH31mVZch/ K1Z2g=; b=MNeEAH7s9Y2BTm7wpFbOBblURjdX5gi9V2rreOxecFtMZeTPF+S62A 59rdE+fLFJ0EfpxXpiVUsUoz5fxYLxQcowyFTM9bopU6AN563tNKgOHgJQH7dmHB X4j25yKMcLY5/VMEC9jjz2doEQKzLyX13PGUqykQfXVT4d8UWsCF8= Received: (qmail 14271 invoked by alias); 15 Jul 2017 17:16: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 14256 invoked by uid 89); 15 Jul 2017 17:16:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.6 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=substitute 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; Sat, 15 Jul 2017 17:16:08 +0000 Received: from monopod.intra.ispras.ru (monopod.intra.ispras.ru [10.10.3.121]) by smtp.ispras.ru (Postfix) with ESMTP id 807F25FB31; Sat, 15 Jul 2017 20:16:06 +0300 (MSK) Date: Sat, 15 Jul 2017 20:15:38 +0300 (MSK) From: Alexander Monakov To: Marc Glisse cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] match.pd: reassociate multiplications with constants In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20.13 (LNX 116 2015-12-14) MIME-Version: 1.0 On Thu, 13 Jul 2017, Marc Glisse wrote: > X*big*big where abs(big*big)>abs(INT_MIN) can be optimized to 0 I'm not sure that would be a win, eliminating X prevents the compiler from deducing that X must be zero (if overflow invokes undefined behavior). > the only hard case is when the product of the constants is -INT_MIN, which we > could turn into X<<31 for instance (sadly loses range info), or (-X)*INT_MIN > or whatever. That would make a nice follow-up, if you are interested. Here's a patch that combines constants, but doesn't handle this case, (I'm not sure we want to handle it, would it be useful in practice?) and neither does substitute zero on overflow, per the above concern. (slsr-4.c needs to be adjusted due to new simplifications) * match.pd ((X * CST1) * CST2): Simplify to X * (CST1 * CST2) if the product does not overflow. testsuite: * gcc.dg/tree-ssa/assoc-2.c: Enhance. * gcc.dg/tree-ssa/slsr-4.c: Adjust. diff --git a/gcc/match.pd b/gcc/match.pd index 36045f1..7f384db 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -283,6 +283,17 @@ 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); + } + (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" } } */