This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Exploiting knowing sizes of string.
- From: Richard Earnshaw <Richard dot Earnshaw at foss dot arm dot com>
- To: Jakub Jelinek <jakub at redhat dot com>, OndÅej BÃlka <neleai at seznam dot cz>
- Cc: Joseph Myers <joseph at codesourcery dot com>, gcc at gcc dot gnu dot org
- Date: Thu, 04 Jun 2015 21:06:40 +0100
- Subject: Re: Exploiting knowing sizes of string.
- Authentication-results: sourceware.org; auth=none
- References: <20150604105929 dot GA19141 at domone> <alpine dot DEB dot 2 dot 10 dot 1506041206200 dot 31862 at digraph dot polyomino dot org dot uk> <20150604123319 dot GF10247 at tucnak dot redhat dot com> <20150604125331 dot GA22076 at domone> <20150604125920 dot GG10247 at tucnak dot redhat dot com> <20150604142304 dot GA23875 at domone> <5570757D dot 8050809 at foss dot arm dot com> <alpine dot DEB dot 2 dot 10 dot 1506041559020 dot 12011 at digraph dot polyomino dot org dot uk> <20150604163632 dot GA26222 at domone> <20150604195759 dot GN10247 at tucnak dot redhat dot com>
On 04/06/15 20:57, Jakub Jelinek wrote:
> On Thu, Jun 04, 2015 at 06:36:33PM +0200, OndÅej BÃlka wrote:
>> On Thu, Jun 04, 2015 at 04:01:50PM +0000, Joseph Myers wrote:
>>> On Thu, 4 Jun 2015, Richard Earnshaw wrote:
>>>
>>>>> Change that into
>>>>>
>>>>> int foo(char *s)
>>>>> {
>>>>> int l = strlen (s);
>>>>> char *p = memchr (s, 'a', l);
>>>>> return p+l;
>>>>> }
>
>> And Joseph you shouldn't restrict yourself only to values that are
>> present in variables to cover case where its implicit one from strcpy
>> converted to stpcpy.
>
> memchr isn't handled in that pass right now at all, of course it could be
> added, shouldn't be really hard. Feel free to file a PR and/or write
> a patch.
>
> As for e.g. the inlining of the first (or a few more) iterations of strcmp
> etc., that is certainly something that can be done in the compiler too and
> the compiler should have much better information whether to do it or not,
> as it shouldn't be done for -Os, or for basic blocks or functions predicted
> cold, because it enlarges the code size quite a lot.
You should also be wary of making the strings passed to the library
functions not naturally aligned. That can result in the code in the
library having to take a much slower path to regain any alignment done
by peeling the initial iteration(s). And if you're going to pass the
full string(s) anyway, then you'd better be pretty sure that doing the
check before the call is really likely to succeed.
R.
> For strcmp in particular, right now we handle the cast of two string
> literals at compile time, or e.g. strcmp (str, ""). For anything further,
> supposedly it should check if the target has inline expansion of strcmp
> (e.g. s390 or sh have them), then inlining one iteration is hardly
> beneficial.
>
> Jakub
>