Reputation: 1931
Now I am read the book Inside The C++ Object Model
's fourth section and have some question.
An inline function like this:
inline int max(int a, int b)
{
return (a > b) ? a : b;
}
Then, a statement such as the following:
a = max(x, y);
this statement will transformed to a = (x > y) ? x : y;
But the book say when add a local variables to the inline function like this:
inline int max(int a, int b)
{
int maxval = (a > b) ? a : b;
return maxval;
}
It will transformed to
int __max_lv_maxval;
a = (__max_lv_maxval = (x > y) ? x : y), __max_lv_maxval;
And obvious the function's performance will drop.
My question is Does the compile (such as VC2010,gcc) optimize the inline function and delete the local variables?
Upvotes: 1
Views: 3162
Reputation: 26181
That book seems to be assuming the compiler inlines at a source level, this of course totally depends on the compiler. Most will begin inlining at the AST level, where transforms and certain optimizations can and will occur. This all assumes the compiler will inline the code.
If we look at your function, any decent compiler will turn them both into the same IR code, as it will compile the inline function before it is inlined, thus a temporary is required regardless (at IR level). when the IR is then actually inlined, the temporary will be folded away and instead replaced with the assignment destination of the call to max
.
When we compile down to machine code, things can change even more, not only can temporaries be removed, but the destination will most likely be a register (in this case), which will have probably been used for on of the source operands as well.
Bottom Line: This totally depends on your compiler, optimization levels and how it does variable livelyness analysis, value propagation and folding.
Upvotes: 7
Reputation:
inline int max(int a, int b) { return (a > b) ? a : b; }
Then, a statement such as the following:
a = max(x, y);
this statement will transformed to
a = (x > y) ? x : y;
It's not that simple. inline
is a hint, not a requirement. The keyword does change the rules slightly, but does not force inlining. You're also forgetting that a
and b
are also local variables to the max
function, and the same book that assumes local variables cannot be optimized away assumes those local variables can be optimized away. They may be optimised away in those cases where it is safe to do so, or they might not be, depending on the compiler and compiler options. For running in a debugger, it may make more sense to keep them. They may not be optimised away when it is unsafe to do so, for example when x
and y
are volatile
. In that case, (x > y) ? x : y
reads one of them twice, which is a potentially visible, and therefore invalid, change in behaviour.
Upvotes: 1
Reputation: 92351
Any interesting transformations we can do to the code, the compiler can do as well. And then some!
When the compiler can see that
inline int max(int a, int b)
{
return (a > b) ? a : b;
}
a = max(x, y);
can be transformed into
a = (x > y) ? x : y;
It can also easily see that storing a value into a variable that is never used later, is meaningless. The store (and the variable) can be removed without changing the result of the program.
Local variables in small functions are often held in the CPU registers, and never stored in memory at all.
Upvotes: 2
Reputation: 733
The compiler just do as you have show. if we call this inline fun like this
void example()
{
int m = max(10, 11);
}
the expansion will be
{
int __max_lv_maxval;// mangled inline local variable
int m = (__max_lv_maxval = (x > y) ? x : y), __max_lv_maxval;
}
In general, each local variable within the inline function must be introduced into the enclosing block of the call as a uniquely named variable. The GCC won't do more for you.
Upvotes: -1