public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Bernd Schmidt <bschmidt@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>, Nick Clifton <nickc@redhat.com>
Subject: Re: Thoughts on memcmp expansion (PR43052)
Date: Mon, 18 Jan 2016 09:22:00 -0000	[thread overview]
Message-ID: <CAFiYyc1NX9V9sM=cij75qAGYqGpXGM=1ZioB8cxWNZxsW=uJeQ@mail.gmail.com> (raw)
In-Reply-To: <56992541.3090402@redhat.com>

On Fri, Jan 15, 2016 at 5:58 PM, Bernd Schmidt <bschmidt@redhat.com> wrote:
> PR43052 is a PR complaining about how the rep cmpsb expansion that gcc uses
> for memcmp is slower than the library function. As is so often the case, if
> you investigate a bit, you can find a lot of issues with the current
> situation in the compiler.
>
> This PR was accidentally fixed by a patch by Nick which disabled the use of
> cmpstrnsi for memcmp expansion, on the grounds that cmpstrnsi could stop
> looking after seeing a null byte, which would be invalid for memcmp, so only
> cmpmemsi should be used. This fix was for an out-of-tree target.
>
> I believe the rep cmpsb sequence used by i386 would actually be valid, so we
> could duplicate the cmpstrn pattern to also match cmpmem and be done - but
> that would then again cause the performance problem described in the PR, so
> it's probably not a good idea.
>
> One question Richard posed in the comments: why aren't we optimizing small
> constant size memcmps other than size 1 to *s == *q? The reason is the
> return value of memcmp, which implies byte-sized operation (incidentally,
> the use of SImode in the cmpmem/cmpstr patterns is really odd). It's
> possible to work around this, but expansion becomes a little more tricky
> (subtract after bswap, maybe). Still, the current code generation is lame.
>
> So, for gcc-6, I think we shouldn't do anything. The PR is fixed, and
> there's no easy bug-fix that can be done to improve matters. Not sure
> whether to keep the PR open or create a new one for the remaining issues.
> For the next stage1, I'm attaching a proof-of-concept patch that does the
> following:
>  * notice if memcmp results are only used for equality comparison
>    against zero
>  * if so, replace with a different builtin __memcmp_eq
>  * Expand __memcmp_eq for small constant sizes with loads and
>    comparison, fall back to a memcmp call.
>
> The whole thing could be extended to work for sizes larger than an int,
> along the lines of memcpy expansion controlled by move ratio etc. Thoughts?

See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171 - the
inline expansion
for small sizes and equality compares should be done on GIMPLE.  Today the
strlen pass might be an appropriate place to do this given its
superior knowledge
about string lengths.

The idea of turning eq feeding memcmp into a special memcmp_eq is good but
you have to avoid doing that too early - otherwise you'd lose on

  res = memcmp (p, q, sz);
  if (memcmp (p, q, sz) == 0)
   ...

that is, you have to make sure CSE got the chance to common the two calls.
This is why I think this kind of transform needs to happen in specific places
(like during strlen opt) rather than in generic folding.

Richard.

>
> Bernd

  reply	other threads:[~2016-01-18  9:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-15 16:58 Bernd Schmidt
2016-01-18  9:22 ` Richard Biener [this message]
2016-04-28 18:36   ` Bernd Schmidt
2016-05-02 12:52     ` Richard Biener
2016-05-02 12:57       ` Bernd Schmidt
2016-05-02 13:14         ` Richard Biener
2016-05-12 17:14           ` Bernd Schmidt
2016-05-13 10:20             ` Richard Biener
2016-05-13 13:05               ` Bernd Schmidt
2016-05-13 13:07                 ` Richard Biener
2016-05-13 13:13                   ` Bernd Schmidt
2016-05-13 13:53                     ` Joseph Myers
2016-05-13 14:00                       ` Bernd Schmidt
2016-05-13 20:41                         ` Joseph Myers
2016-05-31 23:50                           ` Bernd Schmidt
2016-01-18 12:22 ` Nick Clifton
2016-01-19 21:36 ` Jeff Law
2016-05-30 11:29 ` Florian Weimer
2016-05-31 15:05   ` Bernd Schmidt

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='CAFiYyc1NX9V9sM=cij75qAGYqGpXGM=1ZioB8cxWNZxsW=uJeQ@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=bschmidt@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=nickc@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).