Bug 21988 - GCC should transform printf("%s",foo) and printf("foo") into fputs(foo,stdout)
GCC should transform printf("%s",foo) and printf("foo") into fputs(foo,stdout)
Status: NEW
Product: gcc
Classification: Unclassified
Component: middle-end
4.1.0
: P2 enhancement
: ---
Assigned To: Not yet assigned to anyone
: missed-optimization
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-06-09 17:50 UTC by Kaveh Ghazi
Modified: 2006-10-30 02:43 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-10-30 02:43:11


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kaveh Ghazi 2005-06-09 17:50:44 UTC
GCC should optimize printf("%s",foo) and printf("foo") into fputs(foo,stdout) 
and fputs("foo",stdout) respectively.  As noted here:

http://gcc.gnu.org/ml/gcc-patches/2000-09/msg00859.html

We can capture stdout in an inline function using fixincl, perhaps adding the 
__always_inline__ attribute.  Then do the above transformation.

In at least the printf("%s", foo) case, the result fputs(foo,stdout) has the 
same number of arguments, so it might not even be a -Os problem.
Comment 1 Andrew Pinski 2005-06-09 18:15:18 UTC
Confirmed, and yes we need to do something about stdout :).
Comment 2 Kaveh Ghazi 2005-11-28 03:23:57 UTC
Getting stdout wrapped in an inline function is not hard.  I can create something fixincl or whatever to capture that.  The part I don't know how to do is expand that inline function's body into the code stream from fold_builtin_printf or expand_builtin_printf.

Just inserting the inline function call as the right parameter to fputs and calling expand() used to just magically work when we had the RTL inliner because that ran after builtin expansion.  Now with the tree inliner it's too late so we'd have to do something else extra.

Anybody have ideas on this?  It might also help with PR 24729.