This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Optimize strchr (s, 0) to strlen
- From: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>, Jakub Jelinek <jakub at redhat dot com>
- Cc: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>, nd <nd at arm dot com>
- Date: Wed, 20 Apr 2016 11:17:06 +0000
- Subject: Re: [PATCH] Optimize strchr (s, 0) to strlen
- Authentication-results: sourceware.org; auth=none
- Nodisclaimer: True
- References: <AM3PR08MB0088CA61259F65FAAB4D8196836B0 at AM3PR08MB0088 dot eurprd08 dot prod dot outlook dot com> <CAFiYyc1rGd2KWOaN4RTG45Y1uUp6O0A5qOm=i5ma0BZSK5CrXw at mail dot gmail dot com> <AM3PR08MB00881BE3867DF3FC5B5B7530836C0 at AM3PR08MB0088 dot eurprd08 dot prod dot outlook dot com> <CAFiYyc0cUOs19FV-2PnYxfba4_N8Qwox2tkgXZJEw2obe20zgg at mail dot gmail dot com> <CAFiYyc0FvFEUibja6ObDWw9rYf5Cu6puU3gaRrneNzpgkcEgtg at mail dot gmail dot com>,<20160420103325 dot GD2850 at laptop dot zalov dot cz>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
Jakub Jelinek wrote:
> I still don't like this transformation and would very much prefer to see
> using rawmemchr instead on targets that provide it, and also this is
> something that IMHO should be done in the tree-ssa-strlen.c pass together
> with the other optimizations in there. Similarly to stpcpy, which is also
> non-standard (in POSIX, but not in C), we should just look at headers if
> rawmemchr is defined with compatible prototype.
Can you quantify "don't like"? I benchmarked rawmemchr on a few targets
and it's slower than strlen, so it's hard to guess what you don't like about it.
Several targets don't even have an assembly implementation of rawmemchr, so
looking at the header would not be sufficient to determine rawmemchr is fast, let
alone as fast as strlen.
The tree-ssa-strlen pass seems to optimize repeated calls to strlen, or strcpy
after a strlen, so I'm not sure how this is related - this is a local transformation
like the foldings in builtin.c/gimple-fold.c.
> Also, strrchr (s, 0) should be folded to strchr (s, 0) or handled the same
> like that one.
GCC converts strrchr (s, 0) to strchr (s, 0) which then gets optimized. I checked
this happens as expected with both versions of my patch.
> And, while x = strchr (s, 0) to x = rawmemchr (s, 0) is a reasonable -Os
> transformation, x = s + strlen (s) is not, it makes code usually larger
> (especially because it increases register pressure across the call).
Indeed, that's why my transformation is disabled with -Os.
Wilco