This is the mail archive of the
mailing list for the GCC project.
Re: strlen optimizations based on whether stpcpy is declared?
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Alan Modra <amodra at gmail dot com>
- Cc: Martin Sebor <msebor at gmail dot com>, GCC Mailing List <gcc at gcc dot gnu dot org>
- Date: Mon, 2 Oct 2017 12:40:56 +0200
- Subject: Re: strlen optimizations based on whether stpcpy is declared?
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jakub at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3002C81DFE
- References: <firstname.lastname@example.org> <20171002071153.GR1701@tucnak> <20171002103506.GT25070@bubble.grove.modra.org>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Mon, Oct 02, 2017 at 09:05:06PM +1030, Alan Modra wrote:
> > > and taking different paths depending on whether or not the test
> > > succeeds.
> > >
> > > As far as can see, the tests have been there since the pass was
> > > added, but I don't understand from the comments in the file what
> > > their purpose is or why optimization decisions involving one set
> > > of functions (I think strcpy and strcat at a minimum) are based
> > > on whether another function has been declared or not.
> > >
> > > Can you explain what they're for?
> > The reason is that stpcpy is not a standard C function, so in non-POSIX
> > environments one could have stpcpy with completely unrelated prototype
> > used for something else. In such case we don't want to introduce stpcpy
> > into a TU that didn't have such a call. So, we use the existence of
> > a matching prototype as a sign that stpcpy can be synthetized.
> Why is the test for stpcpy being declared done for the strcpy cases
> rather than the stpcpy cases?
Because the optimization is about strcpy followed by some call that would
like to know the length of the string, so we want to replace the strcpy call
by stpcpy and use the lhs of the stpcpy call - the first argument as the
length instead of yet another strlen call (or similar).
If we don't know that stpcpy is available and can be safely used, we can't
The reason why a matching prototype is sufficient is that if you have a
matching prototype (and no -fno-builtin-stpcpy or -fno-builtin) and have
calls to that function in your code, GCC considers it a builtin and
optimizes them according to the behavior of the builtin.