patch to suggest putc/fputs over printf("string") or printf("\n")

Jeffrey A Law
Mon Jan 11 14:05:00 GMT 1999

  In message < >you write:
  > Jeffrey A Law <> writes:
  > > Simlarly if via attributes we can determine the string has no formatting
  > > chars, then we can make the transformation.
  > No, this is exactly the problem.  Via the attributes we can see the
  > format string and *suggest* optimizations.  But we cannot perform them
  > automatically since the string the compiler sees need not be the one
  > used at runtime.
OK.  Then the attributes are just hints.  From an optimization standpoint we
must ignore them.  Which falls back to what I said in my original message,
if the format string is not a compile-time constant, then we can not optimize.
Period.  Either we have accurate attributes, or we do not.  If the attributes
are not accurate, then we can not use them for optimization purposes.

  > If the compiler warns about this, the programmer will be able to
  > replace printf with fputs in the sources and the translator will
  > replace %% by %.  Note that the percent could stand for something else
  > but itself, e.g, in escape sequences to select character sets.
We're not talking about warnings.  We're talking about optimizations.  We could
still add the warnings if we think they are useful.

  > The point is not not getting an optimal solution from the start.
  > Optimizations like optimizing string operations can never be optimized
  > sufficently.  The compiler must always assume the worst case.
Right.  Precisely.

  >    cp = my_malloc (100);
  >    sprintf (cp, "foo");
  >    strcat (cp, "bar");
  > The compiler itself can never rewrite this to
  >    cp = my_malloc (100); 
  >    memcpy (mempcpy (cp, "foo", 3), "bar", 4);
  > since it cannot assume that there are no side-effects on buf.  There
  > are sequence points in this instructions sequence which cannot easily
  > be removed.
  > This means there is no "we will handle the other optimizations later".
Then we can't perform this particular transformation.  I wasn't planning on
it anyway.

My goal is to be able to rewrite single statements which call the printf
family of functions and where the format string is a compile time constant.
I'm not particularly concerned about multi-statement calls.

  > So let's compromise: make the automatic code rewrite optional.  Let it
  > be automatically included for -O2 or -O3 or whatever.  But if
  > -Wwarn-constant-print (or however you want to call it) is given and in
  > the case gettext is used, print a warning instead of replacing the
  > code automatically.
I don't think -W options should impact code generation.  Ever.  Certainly we
would have a flag for the optimization, which the user could turn off.


More information about the Gcc-patches mailing list