Exploiting knowing sizes of string.

Jakub Jelinek jakub@redhat.com
Thu Jun 4 19:58:00 GMT 2015

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


More information about the Gcc mailing list