This is the mail archive of the gcc-patches@gcc.gnu.org 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]

Re: [PATCH] Optimize sprintf into strcpy if possible


 > From: Jakub Jelinek <jakub@redhat.com>
 > 
 > On Fri, Apr 06, 2001 at 04:43:15PM -0400, Kaveh R. Ghazi wrote:
 > >  > This patch optimizes sprintf (x, "%s", y) into strcpy (x, y) and also
 > >  > sprintf (x, "foowithoutpercentcharacter") into strcpy (x, "foo....").
 > > 
 > > 
 > > One reason I didn't write this one when I initially did the stdio opts
 > > was that I worried what happens when e.g. on older hosts sprintf is
 > > declared in stdio.h to return char* ?
 > > 
 > > I think sunos4 is one such host, can you please test it there to make
 > > sure it at least degrades gracefully (or perhaps make it actually
 > > work?)
 > 
 > I don't have access to SunOS at all, is the prototype
 > char *sprintf();
 > there?

Yes, although fixproto will add the standard parameters wrapped by
_PARAMS().  You can probably temporarily fake it for a test.


 > For the sprintf optimization, we don't really care about return
 > value (only optimize if it is not used, since strcpy has different
 > result than sprintf),

Yup, most/all of the stdio builtins are only valid if the return type
is ignored.


 > so I think the easiest way would be to teach duplicate_decls that
 > if certain tree node is used for return value, it should just
 > silently override the builtin with the new prototype.

There is already code in duplicate_decls to allow builtins if the
return type or the first argument have the same MODE.  I took
advantage of this to declare builtin fprintf with a void* instead of a
FILE* stream.  I don't know if the MODE of char* will match an int
though for the return type of sprintf.

Assuming that works, or you tweek it to work, IMHO you still need to
generalize it to work with g++.  I.e. builtin fprintf currently
doesn't work with g++ for this reason and neither will builtin sprintf
without some help.

The best thing to do would be to make a separate function (in
c-common.c) which checks whether the MODEs of the return type and/or
*all* arguments (not just the first) match.  Then call it from
duplicate_decls() of both languages.  (Checking all arguments would
allow me to declare the parameters for builtin fputs, whose stream is
in the *second* argument.)

If it sounds like I'm asking you to clean up something I should have
done, you'd be correct. :-) If you tackle this I'd be very grateful,
its one of the leftovers I never fixed in my builtin stdio work.

		--Kaveh
--
Kaveh R. Ghazi			Engagement Manager / Project Services
ghazi@caip.rutgers.edu		Qwest Internet Solutions


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