Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out which functions were inlined

When compiling C++ with GCC 4.4 or MSVC is it possible to get the compiler to emit messages when a function is inlined?

like image 593
Jack Nock Avatar asked Jul 15 '10 00:07

Jack Nock


People also ask

How do you check if a function has been inlined?

The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only. So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not. Save this answer.

When can a function be inlined?

Only when you want the function to be defined in a header. More exactly only when the function's definition can show up in multiple translation units. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code.

What does it mean for a function to be inlined?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Are static functions inlined?

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.


1 Answers

With g++, I don't think you can make g++ report that, but you can examine the resulting binary with any tool that shows symbols, nm for example:

#include <iostream>
struct T {
        void print() const;
};
void T::print() const { std::cout << " test\n" ; }
int main()
{
        T t;
        t.print();
}

~ $ g++ -O3  -Wall -Wextra -pedantic -o test test.cc
~ $ nm test | grep print
0000000000400800 t _GLOBAL__I__ZNK1T5printEv
0000000000400830 T _ZNK1T5printEv

vs

#include <iostream>
struct T {
        void print() const { std::cout << " test\n" ; }
};
int main()
{
        T t;
        t.print();
}
~ $ g++ -O3  -Wall -Wextra -pedantic -o test test.cc
~ $ nm test | grep print

(no output from nm in the second case)

EDIT: Also, profilers may be of use. gprof shows, on these two examples:

0.00      0.00     0.00        1     0.00     0.00  global constructors keyed to _ZNK1T5printEv
0.00      0.00     0.00        1     0.00     0.00  T::print() const

vs. just

0.00      0.00     0.00        1     0.00     0.00  global constructors keyed to main
like image 166
Cubbi Avatar answered Sep 18 '22 05:09

Cubbi