Reputation: 18870
I know in some cases, the explicitly defined inline function will be converted to the regular function with calling stacks by the compiler. By how do I know this is the case? (for my C++ code)
BTW, in what circumstances will the compiler transform an inline function to a regular function?
Upvotes: 2
Views: 218
Reputation: 52538
Why would an inline function be compiled as regular?
“inline” is just a hint. Semantically it means there could be multiple implementations of the function, for example if an inline function contains a static variable, that can exist multiple times.
Recursive inline functions cannot be completely inlined. They might be partially inline, say factorial(n) might do n/4 function calls.
The compiler can decide whether there is a benefit from inlining. There is extra code. Inlining 10 calls in a row could be slower than calling them. A loop in a normal function might take advantage of all registers and run at high speed, but the same function inlined inside another might create too much register pressure.
And if you have nested inline functions, you can choose one that is implemented as a regular function including inlined functions once, getting most of the speed gain with little of the extra code size.
Upvotes: 0
Reputation: 129344
The compiler will DEFINITELY produce a regular function if the inline function has it's address taken (that is, you are making a function pointer to it, or something similar). And of course, if the function is virtual
, it also has to exist as a standalone function since a virtual
function call is not always possible to inline.
Other than that, it's entirely up to the compiler - it uses all manner of heuristics, such as "How big is the function?", "how many times is it called?", "how much 'gain' is there from inlining it?". If a function is called many times, and is quite large, it is probably left as a standalone function. If the function is only called a few times, or it's tiny, then it's inlined [assuming the compiler actually CAN inline it, of course - as described above, it may not be able to]. Also, nearly all compilers need to "see" the source code of the function to inline the function, but both GCC/G++ and MSVC do have otions for "whole program optimisation" that is intended to overcome this issue.
In other words, you can only "know" by reading the resulting machine code - in some cases, there are also extended "warning" or "notes" messages that you can enable to tell the compiler to give you information about "I did not inline function Func1
because ...insert some reason here...".
Upvotes: 0
Reputation: 208333
The keyword inline
does not mean that the function will be inlined (or not). It means allow for multiple definitions in different translation units of the same program. The compiler will not transform an inline into a non-inline, it will follow the rules set in the standard and (almost) orthogonally determine whether to inline or not your functions.
As of whether to determine if a function was inlined or not, the guaranteed way to do so is checking the generated object code (or assembly). Checking whether there is an out-of-line definition of the function won't help, as many compilers will generate that out-of-line definition even if the code was actually inlined.
Upvotes: 2
Reputation:
You run nm
or otool
on the executable, and if you see the name of the function, then it has been defined. That doesn't mean that it hasn't indeed been inlined at all (it's possible that the compiler inlines a function but also generates an independent function body because for example one assigns a function pointer to it). For that, you need to examine the actual generated assembly code.
Upvotes: 4