1

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

6
  • Why not compare the assembly yourself? Commented Sep 9, 2011 at 15:18
  • 3
    You ask 'where can I get a list of some "basic" optimizations made by my compiler ?' without saying what your compiler is. Commented Sep 9, 2011 at 15:18
  • @Kerrek SB: because I'm not comfortable with assembly Commented Sep 9, 2011 at 17:29
  • @Simon: If you're not comfortable with the compilation target, what sort of answer would you expect that would be useful to you? Optimizations usually only manifest in the resulting target code... Commented Sep 9, 2011 at 17:30
  • @Julian: I'm pretty sure almost all good compilers are doing approximately the same optimizations. At least in simple cases like this (I'm using clang & gcc) Commented Sep 9, 2011 at 17:31

3 Answers 3

5

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.