Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-16 Thread Jeff Law
On 09/16/2016 07:28 AM, Wilco Dijkstra wrote: I noticed rawmemchr taking non-trivial amounts of time in various profiles despite no use of rawmemchr in any of the source code. It's apparently a common idiom to use strchr (s, 0) to find the end of a string. A bit of a surprise, but it is what it

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-16 Thread Wilco Dijkstra
Bernd Schmidt wrote: > On 09/15/2016 03:38 PM, Wilco Dijkstra wrote: > > __rawmemchr is not the fastest on any target I tried, including x86, > > Interesting. Care to share your test program? I just looked at the libc > sources and strlen/rawmemchr are practically identical code so I'd > expect a

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Wilco Dijkstra
Jakub Jelinek > > Those are the generic definitions, all targets that care about performance > obviously should replace them with assembly code. No, that's exactly my point, it is not true that it is always best to write assembly code. For example there is absolutely no benefit in writing an opt

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Jakub Jelinek
On Thu, Sep 15, 2016 at 03:27:52PM +, Wilco Dijkstra wrote: > That's best done GCC as a general optimization as currently mempcpy is not > handled efficiently (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70140), > and it avoids having to repeat this for every C library out there... > > g

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Wilco Dijkstra
From: Jakub Jelinek > On Thu, Sep 15, 2016 at 02:55:48PM +, Wilco Dijkstra wrote: >> stpcpy is not conceptually the same, but for mempcpy, yes. By default >> it's converted into memcpy in the GLIBC headers and the generic >> implementation. >> >> stpcpy uses strlen and memcpy which is genera

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Wilco Dijkstra
Jakub Jelinek wrote: On Thu, Sep 15, 2016 at 03:16:52PM +0100, Szabolcs Nagy wrote: > > > > from libc point of view, rawmemchr is a rarely used > > nonstandard function that should be optimized for size. > > (glibc does not do this now, but it should in my opinion.) > > rawmemchr with 0 is to strl

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Jakub Jelinek
On Thu, Sep 15, 2016 at 02:55:48PM +, Wilco Dijkstra wrote: > Jakub Jelinek wrote: > On Thu, Sep 15, 2016 at 03:16:52PM +0100, Szabolcs Nagy wrote: > > > > > > from libc point of view, rawmemchr is a rarely used > > > nonstandard function that should be optimized for size. > > > (glibc does no

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Jakub Jelinek
On Thu, Sep 15, 2016 at 03:16:52PM +0100, Szabolcs Nagy wrote: > On 15/09/16 14:49, Jakub Jelinek wrote: > > On Thu, Sep 15, 2016 at 01:38:45PM +, Wilco Dijkstra wrote: > >> __rawmemchr is not the fastest on any target I tried, including x86, so > >> GLIBC's > >> current default behaviour of a

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Szabolcs Nagy
On 15/09/16 14:49, Jakub Jelinek wrote: > On Thu, Sep 15, 2016 at 01:38:45PM +, Wilco Dijkstra wrote: >> __rawmemchr is not the fastest on any target I tried, including x86, so >> GLIBC's >> current default behaviour of always using __rawmemchr is inefficient. GCC >> doesn't >> support __rawm

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Bernd Schmidt
On 09/15/2016 03:38 PM, Wilco Dijkstra wrote: __rawmemchr is not the fastest on any target I tried, including x86, Interesting. Care to share your test program? I just looked at the libc sources and strlen/rawmemchr are practically identical code so I'd expect any difference to be lost in the

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Jakub Jelinek
On Thu, Sep 15, 2016 at 01:38:45PM +, Wilco Dijkstra wrote: > __rawmemchr is not the fastest on any target I tried, including x86, so > GLIBC's > current default behaviour of always using __rawmemchr is inefficient. GCC > doesn't > support __rawmemchr at all, so the strlen optimization can't

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Wilco Dijkstra
Richard Biener wrote: > On Wed, Sep 14, 2016 at 3:45 PM, Jakub Jelinek wrote: > > On Wed, Sep 14, 2016 at 03:41:33PM +0200, Richard Biener wrote: > >> > We've seen several different proposals for where/how to do this > >> > simplification, why did you > >> > say strlenopt is best? It would be an

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Jakub Jelinek
On Thu, Sep 15, 2016 at 09:52:34AM +0200, Richard Biener wrote: > On Wed, Sep 14, 2016 at 3:45 PM, Jakub Jelinek wrote: > > On Wed, Sep 14, 2016 at 03:41:33PM +0200, Richard Biener wrote: > >> > We've seen several different proposals for where/how to do this > >> > simplification, why did you > >

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-15 Thread Richard Biener
On Wed, Sep 14, 2016 at 3:45 PM, Jakub Jelinek wrote: > On Wed, Sep 14, 2016 at 03:41:33PM +0200, Richard Biener wrote: >> > We've seen several different proposals for where/how to do this >> > simplification, why did you >> > say strlenopt is best? It would be an unconditional strchr (a, 0) -> a

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-14 Thread Szabolcs Nagy
On 14/09/16 14:45, Jakub Jelinek wrote: > I think for the middle-end, using strchr (a, 0) as canonical instead of a + > strlen (a) > is better, and at expansion time we can decide what to use (a + strlen (a) > if you'd expand strlen inline, rather than as a function call, or > __rawmemchr (which i

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-14 Thread Jakub Jelinek
On Wed, Sep 14, 2016 at 03:41:33PM +0200, Richard Biener wrote: > > We've seen several different proposals for where/how to do this > > simplification, why did you > > say strlenopt is best? It would be an unconditional strchr (a, 0) -> a + > > strlen (a) rewrite, > > ie. completely unrelated to

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-14 Thread Richard Biener
On Tue, Sep 13, 2016 at 2:36 PM, Wilco Dijkstra wrote: > Richard Biener wrote: >> On Wed, May 18, 2016 at 2:29 PM, Wilco Dijkstra >> wrote: >>> Richard Biener wrote: >>> Yeah ;) I'm currently bootstrapping/testing the patch that makes it possible to write all this in match.pd.

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-13 Thread Oleg Endo
On Tue, 2016-09-13 at 12:36 +, Wilco Dijkstra wrote: > Richard Biener wrote: > > > > On Wed, May 18, 2016 at 2:29 PM, Wilco Dijkstra > .com> wrote: > > > > > > Richard Biener wrote: > > > > > > > > > > > Yeah ;)  I'm currently bootstrapping/testing the patch that > > > > makes it possible

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-09-13 Thread Wilco Dijkstra
Richard Biener wrote: > On Wed, May 18, 2016 at 2:29 PM, Wilco Dijkstra > wrote: >> Richard Biener wrote: >> >>> Yeah ;) I'm currently bootstrapping/testing the patch that makes it >>> possible to >>> write all this in match.pd. >> >> So what was the conclusion? Improving match.pd to be able t

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-05-18 Thread Richard Biener
On Wed, May 18, 2016 at 2:29 PM, Wilco Dijkstra wrote: > Richard Biener wrote: >> >> Yeah ;) I'm currently bootstrapping/testing the patch that makes it >> possible to >> write all this in match.pd. > > So what was the conclusion? Improving match.pd to be able to handle more cases > like this se

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-05-18 Thread Wilco Dijkstra
Richard Biener wrote: > > Yeah ;) I'm currently bootstrapping/testing the patch that makes it possible > to > write all this in match.pd. So what was the conclusion? Improving match.pd to be able to handle more cases like this seems like a nice thing. Wilco

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-05-04 Thread Wilco Dijkstra
Richard Biener wrote: > > Yeah ;) I'm currently bootstrapping/testing the patch that makes it possible > to > write all this in match.pd. So did that pass bootstrap? It would be good to decide how to proceed with this. Wilco

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Wilco Dijkstra
Jakub Jelinek wrote: > On Wed, Apr 20, 2016 at 11:17:06AM +, Wilco Dijkstra wrote: >> 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. > > This is the same stuff as has been discussed

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Jakub Jelinek
On Wed, Apr 20, 2016 at 11:17:06AM +, Wilco Dijkstra wrote: > 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. This is the same stuff as has been discussed for mempcpy, rawmemchr is the

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Richard Biener
On Wed, Apr 20, 2016 at 1:56 PM, Wilco Dijkstra wrote: > Richard Biener wrote: >> Better - comments below. Jakub objections to the usefulness of the transform >> remain - we do have the strlen pass that uses some global knowledge to decide >> on profitability. One could argue that for -Os doing

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Wilco Dijkstra
Richard Biener wrote: > Better - comments below. Jakub objections to the usefulness of the transform > remain - we do have the strlen pass that uses some global knowledge to decide > on profitability. One could argue that for -Os doing the reverse transform is > profitable? In what way would it

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Wilco Dijkstra
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 s

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Richard Biener
On Wed, Apr 20, 2016 at 12:33 PM, Jakub Jelinek wrote: > On Wed, Apr 20, 2016 at 11:44:08AM +0200, Richard Biener wrote: >> (simplify >> (BUILT_IN_STRCHR @0 integer_zerop) >> (pointer_plus @0 (BUILT_IN_STRLEN:size_type_node @0))) > > I still don't like this transformation and would very much pre

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Jakub Jelinek
On Wed, Apr 20, 2016 at 11:44:08AM +0200, Richard Biener wrote: > (simplify > (BUILT_IN_STRCHR @0 integer_zerop) > (pointer_plus @0 (BUILT_IN_STRLEN:size_type_node @0))) I still don't like this transformation and would very much prefer to see using rawmemchr instead on targets that provide it, a

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Richard Biener
On Wed, Apr 20, 2016 at 10:45 AM, Richard Biener wrote: > On Tue, Apr 19, 2016 at 6:32 PM, Wilco Dijkstra > wrote: >> Richard Biener wrote: >>> >>> This folding should be added to gimple-fold.c:gimple_fold_builtin instead, >>> the builtins.c foldings are purerly for folding to constants nowadays

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-20 Thread Richard Biener
On Tue, Apr 19, 2016 at 6:32 PM, Wilco Dijkstra wrote: > Richard Biener wrote: >> >> This folding should be added to gimple-fold.c:gimple_fold_builtin instead, >> the builtins.c foldings are purerly for folding to constants nowadays. > > So is this better? It's a lot more verbose for something so

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-19 Thread Wilco Dijkstra
Richard Biener wrote: > > This folding should be added to gimple-fold.c:gimple_fold_builtin instead, > the builtins.c foldings are purerly for folding to constants nowadays. So is this better? It's a lot more verbose for something so simple... Unfortunately match.pd doesn't support this kind of th

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-19 Thread Richard Biener
On Mon, Apr 18, 2016 at 7:00 PM, Wilco Dijkstra wrote: > Optimize strchr (s, 0) to s + strlen (s). strchr (s, 0) appears a common > idiom for finding the end of a string, however it is not a very efficient > way of doing so. Strlen is a much simpler operation which is significantly > faster (eg.

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-18 Thread Oleg Endo
On Mon, 2016-04-18 at 17:00 +, Wilco Dijkstra wrote: > Optimize strchr (s, 0) to s + strlen (s). strchr (s, 0) appears a > common > idiom for finding the end of a string, however it is not a very > efficient > way of doing so. Strlen is a much simpler operation which is > significantly > fast

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-18 Thread Wilco Dijkstra
Jakub Jelinek wrote: > On Mon, Apr 18, 2016 at 05:00:45PM +, Wilco Dijkstra wrote: >> Optimize strchr (s, 0) to s + strlen (s). strchr (s, 0) appears a common >> idiom for finding the end of a string, however it is not a very efficient >> way of doing so. Strlen is a much simpler operation w

Re: [PATCH] Optimize strchr (s, 0) to strlen

2016-04-18 Thread Jakub Jelinek
On Mon, Apr 18, 2016 at 05:00:45PM +, Wilco Dijkstra wrote: > Optimize strchr (s, 0) to s + strlen (s). strchr (s, 0) appears a common > idiom for finding the end of a string, however it is not a very efficient > way of doing so. Strlen is a much simpler operation which is significantly > fas

[PATCH] Optimize strchr (s, 0) to strlen

2016-04-18 Thread Wilco Dijkstra
Optimize strchr (s, 0) to s + strlen (s). strchr (s, 0) appears a common idiom for finding the end of a string, however it is not a very efficient way of doing so. Strlen is a much simpler operation which is significantly faster (eg. on x86 strlen is 50% faster for strings of 8 bytes and about tw