public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Marek Polacek <polacek@redhat.com>
Subject: Re: [PATCH] Move X - (X / Y) * Y folding to match.pd
Date: Mon, 29 Jun 2015 07:58:00 -0000	[thread overview]
Message-ID: <alpine.LSU.2.11.1506290931290.26650@zhemvz.fhfr.qr> (raw)
In-Reply-To: <alpine.DEB.2.20.1506271524590.1949@laptop-mg.saclay.inria.fr>

On Sat, 27 Jun 2015, Marc Glisse wrote:

> 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.

Interesting.

> >  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))))

Yes.  Eventually even (convert? (mult (convert1? (trunc_div ...)?
Of course with matching @0 between the two operands of the minus
you constrain types quite a bit.

I'd say just single-step through fold and see what types it get
present when folding a - (unsigned) ((a / b) * b).

Thanks,
Richard.

> (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
> > 
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Dilip Upmanyu, Graham Norton, HRB 21284 (AG Nuernberg)

  reply	other threads:[~2015-06-29  7:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26 16:39 Marek Polacek
2015-06-27 14:53 ` Marc Glisse
2015-06-29  7:58   ` Richard Biener [this message]
2015-06-29 14:04     ` Marek Polacek
2015-06-29 14:09       ` Richard Biener
2015-06-29 22:26       ` Marc Glisse
2015-06-30  7:45         ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LSU.2.11.1506290931290.26650@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).