This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strlen optimizations based on whether stpcpy is declared?

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]