aah134
aah134

Reputation: 860

compiler limitations, when inlining a function

I know that after adding inline keyword to a class member function, compiler decide to inline if the "function isn't complicated" or if there are "a lot of statements" in the function body.

  1. What considered is to be "a lot of statements"?

  2. The compiler also cannot perform inlining if the address of the function is taken implicitly or explicitly. Can anyone explain this point?

  3. And because inline is just a suggestion, how do I test what functions were not inlined?

Upvotes: 0

Views: 365

Answers (4)

Mike Seymour
Mike Seymour

Reputation: 254751

  1. It's entirely up to the compiler designer to decide what heuristics to use to decide whether to inline a function call. Hopefully, it's based on the resulting code size rather than the number of "statements", which is a completely meaningless metric. Hopefully, it also includes an estimate of the performance gain to weigh against any increase in code size.

  2. If the address is taken, then there must be a non-inline instance of the function so that there's something to take the address of. However, this doesn't prevent any calls to the function from being inlined, if the compiler decides that they should be.

  3. You can disassemble the generated code to see whether there's a function call. This may be tricky if the calling function is large, or if it was itself inlined in another function. Most compilers have a (non-standard) way to force inlining; but only use this if you're sure you know better than the compiler, and have the measurements to prove it.

Upvotes: 2

Matteo Italia
Matteo Italia

Reputation: 126957

  1. It's completely up to the compiler; this will probably depend from the optimization settings, the peculiarities of the target architecture (e.g. the cost of a function call vs the potential cost of cache misses due to increased code size) and how well the function can be integrated with the rest of the code at the specific call site.

  2. If a function is always inlined it ceases to exist as a function by itself, since its instructions are put directly on the call site, with the rest of the caller code; for this reason, such a function does not have an address anymore, since it's blended with its callers. But what typically happens when you ask for a function address is that the compiler will inline the function wherever it likes best, but still emit it also as a "regular function" with its address for usage via function pointer.

  3. You can disassemble the generated executable, or ask to your compiler to emit assembly instead of building the finished executable (with gcc it's the -S option) and check it manually. Still, notice that examining optimized binaries is not trivial - since inlined code is blended with the callers code, it may be difficult to find out exactly where the inlined function went, and at the same time you may also find around non-inlined copies of functions for usage via function pointers (as explained above).

Upvotes: 4

James Kanze
James Kanze

Reputation: 154047

  1. Whether a compiler generates the code inline or not is up to the compiler. Most modern compilers will generate pretty much anything that isn't recursive inline.

  2. This is simply false. When the address is taken, the compiler must generate an out of line copy for the address, but that doesn't prevent it from inlining when the function is called.

  3. You don't.

Upvotes: 4

Joohwan
Joohwan

Reputation: 2512

As to your question about when a compiler might inline check out this thread:

Does the compiler decide when to inline my functions (in C++)?

Upvotes: 1

Related Questions