Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically inlined functions across translation units and gcc 4.6

Tags:

c++

c

gcc

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)

like image 873
Andrew Tomazos Avatar asked Mar 19 '12 09:03

Andrew Tomazos


1 Answers

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.

like image 79
MSalters Avatar answered Sep 29 '22 01:09

MSalters