If I don't declare a function f as inlined. Just as follows:
A.h:
X f(Y y);
A.cpp:
X f(Y y)
{
...
}
Then in a different translation unit:
B.cpp:
#include "A.h"
Z g(W w)
{
...
... f(...) ...
...
}
Then I compile the two translation units A.o and B.o with gcc 4.6, and then link them also through gcc. (Maybe with -O3 to both steps)
Will gcc consider inlining the function for performance at link time? Or is it too late?
In a code review someone suggested that I shouldn't declare my functions as inline as the compiler knows better than I do when to inline. I was always under the impression unless the function is defined in the header than the compiler doesn't have the option to inline it.
(If the answer differs for C mode, C++ mode, or gnu++0x mode please point this out also)
The feature is called Link Time Optimization(LTO) and is not enabled by default in GCC 4.6
[edit]
With LTO enabled, GCC will save a "GIMPLE" representation of X f(Y y)
in A.obj
. This representation is slightly more processed than the usual C++ pre-processing, but not a lot. In particular, it's not translated into assembly yet. As a result, the linker can still inline it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With