开发者

Compiler optimizations of inlined functions in C++

When a function which take a pointer in argument is inlined, does the compiler remove the indirection during the optimization process ? Of course when it makes sense..

Here is an obvious example:

inline void say_hello (开发者_如何转开发person* p) {
    std::cout << "hello " << p->name << std::endl;
}

int main () {
    person goldorak;
    goldorak.name = "Goldorak";

    say_hello(&goldorak);
    return 0;
}

This case is trivial but if the compiler does the optimization is there some cases in which it doesn't ?

Bonus: where can I get a list of some "basic" optimizations made by my compiler ?

Ps: I'm just curious


I'm assuming GCC, and so the link you are looking for is http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

And to quote: (this may not be what you were getting at)

-findirect-inlining Inline also indirect calls that are discovered to be known at compile time thanks to previous inlining. This option has any effect only when inlining itself is turned on by the -finline-functions or -finline-small-functions options.

Enabled at level -O2.

The equivilent documentation for Visual Studio compilers (including C++) http://msdn.microsoft.com/en-us/library/k1ack8f1.aspx (you can follow the links for more info)


A good compiler would do this. It would be a two-stage process, though. First it would inline the function. A later phase could then realize that the only use of the struct is to temporarily hold name and eliminate it.


Yes, in my experience this is true for both pointers (C and C++) and references (C++ only of course) - pretty much any decent compiler will optimise away redundant indirection. Even Visual Studio does this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜