First of all, the problem is not the if
; as you saw, gcc
sees through the if
and manages to pass 30
straight to printf
.
Now, gcc
does have some logic to handle special cases of printf
(in particular, it does optimize printf("something\n")
and even printf("%s\n", "something")
to puts("something")
), but it is extremely specific and doesn't go much further; printf("Hello %s\n", "world")
, for example, is left as-is. Even worse, any of the variants above without a trailing newline are left untouched, even if they could be transformed to fputs("something", stdout)
.
I imagine that this comes down to two main problems:
the two cases above are extremely easy patterns to implement and happen quite frequently, but for the rest probably it's rarely worth the effort; if the string is constant and the performance is important, the programmer can take care of it easily - actually, if the performance of printf
is critical he shouldn't be relying on this kind of optimization, which may break at the slightest change of format string.
If you ask me, even just the puts
optimizations above are already "going for the style points": you are not really going to gain serious performance in anything but artificial test cases.
When you start to go outside the realm of %s\n
, printf
is a minefield, because it has a strong dependency on the runtime environment; in particular, many printf
specifiers are (unfortunately) affected by the locale, plus there are a host of implementation-specific quirks and specifiers (and gcc
can work with printf
from glibc, musl, mingw/msvcrt, ... - and at compile time you cannot invoke the target C runtime - think when you are cross-compiling).
I agree that this simple %d
case is probably safe, but I can see why they probably decided to avoid being overly smart and only perform the dumbest and safest optimizations here.
For the curious reader, here is where this optimization is actually implemented; as you can see, the function matches a restricted number of very simple cases (and GIMPLE aside, hasn't changed a lot since this nice article outlining them was written). Incidentally, the source actually explains why they couldn't implement the fputs
variant for the non-newline case (there's no easy way to reference the stdout
global at that compilation stage).
gcc
does not (cannot..?) predict theprintf
output. – Aediledouble
type and the format was%4.3f
. This would require the compiler, to actually run your code, which it does not. – Letreeceprintf("i is equal to %d\n", 30);
into aputs()
, which I would presume to be quite a bit simpler. – Giamoi
on the last line introduces a data dependency in the execution, whereas replacing it with30
would have no such dependency. In more complicated, concurrent executions that use consume memory ordering, such "optimizations" may have surprising effects. – Circumjacentprintf
? GCC apparently doesn't and there's really no good reason to. – Cordova