Question

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

Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top