From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 102531 invoked by alias); 27 Jun 2015 13:36:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 102521 invoked by uid 89); 27 Jun 2015 13:36:45 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.1 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail2-relais-roc.national.inria.fr Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Sat, 27 Jun 2015 13:36:44 +0000 Received: from ip-0.net-81-220-131.standre.rev.numericable.fr (HELO laptop-mg.local) ([81.220.131.0]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 27 Jun 2015 15:36:41 +0200 Date: Sat, 27 Jun 2015 14:53:00 -0000 From: Marc Glisse Reply-To: gcc-patches@gcc.gnu.org To: Marek Polacek cc: GCC Patches , Richard Biener Subject: Re: [PATCH] Move X - (X / Y) * Y folding to match.pd In-Reply-To: <20150626162940.GI10139@redhat.com> Message-ID: References: <20150626162940.GI10139@redhat.com> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-SW-Source: 2015-06/txt/msg02014.txt.bz2 On Fri, 26 Jun 2015, Marek Polacek wrote: > This is an attempt to move one pattern from fold-const.c to match.pd. > It ought to be 1:1, but is not, e.g. with this patch we won't fold e.g. > > int > f (int a, int b) > { > return a - (unsigned) ((a / b) * b) > } > > anymore, but we're able to fold > > int > ff (int a, unsigned int b) > { > return a - ((a / b) * b); > } > > and fold-const.c is not. I played around with converts, but didn't find > anything that would work well. Any suggestions how to make this pattern > better? Anything wrong with this? +/* X - (X / Y) * Y is the same as X % Y. */ +(simplify + (minus (convert? @0) (convert? (mult (trunc_div @0 @1) @1))) + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + (convert (trunc_mod @0 @1)))) (the other div/mod pairs could benefit from the same transformation as long as there are no conversions, but the conversion seems easier to handle with trunc_) > diff --git gcc/match.pd gcc/match.pd > index b2f8429..2bc158b 100644 > --- gcc/match.pd > +++ gcc/match.pd > @@ -238,6 +238,12 @@ along with GCC; see the file COPYING3. If not see > && tree_nop_conversion_p (type, TREE_TYPE (@1))) > (trunc_mod @0 (convert @1)))) > > +/* X - (X / Y) * Y is the same as X % Y. */ > +(simplify > + (minus @0 (mult (trunc_div @0 @1) @1)) > + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) > + (trunc_mod @0 @1))) > + > /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, > i.e. "X % C" into "X & (C - 1)", if X and C are positive. > Also optimize A % (C << N) where C is a power of 2, > > Marek > -- Marc Glisse