5

I read that with inline functions where ever the function call is made we replace the function call with the body of the function definition.

According to the above explanation there should not be any function call when inline is user.

If that is the case Why do I see three call instructions in the assembly code ?

#include <iostream>                                                                  
                                                                                     
inline int add(int x, int y)                                                         
{                                                                                    
        return x+ y;                                                                 
}                                                                                    
                                                                                     
int main()                                                                           
{                                                                                    
        add(8,9);                                                                    
        add(20,10);                                                                  
        add(100,233);                                                                
}

meow@vikkyhacks ~/Arena/c/temp $ g++ -c a.cpp
meow@vikkyhacks ~/Arena/c/temp $ objdump -M intel -d a.o
0000000000000000 <main>:
   0:   55                      push   rbp
   1:   48 89 e5                mov    rbp,rsp
   4:   be 09 00 00 00          mov    esi,0x9
   9:   bf 08 00 00 00          mov    edi,0x8
   e:   e8 00 00 00 00          call   13 <main+0x13>
  13:   be 0a 00 00 00          mov    esi,0xa
  18:   bf 14 00 00 00          mov    edi,0x14
  1d:   e8 00 00 00 00          call   22 <main+0x22>
  22:   be e9 00 00 00          mov    esi,0xe9
  27:   bf 64 00 00 00          mov    edi,0x64
  2c:   e8 00 00 00 00          call   31 <main+0x31>
  31:   b8 00 00 00 00          mov    eax,0x0
  36:   5d                      pop    rbp
  37:   c3                      ret  

NOTE

Complete dump of the object file is here

Community
  • 1
  • 1
vikkyhacks
  • 3,190
  • 9
  • 32
  • 47
  • 8
    `inline` is just a suggestion. It's not a requirement. If you want to force it to inline, see: http://stackoverflow.com/questions/8381293/how-do-i-force-gcc-to-inline-a-function – Mysticial Jun 11 '14 at 18:44
  • 4
    @Mysticial this isn't jumping to a function or anywhere useful, however. It jumps to the next line. – John Dvorak Jun 11 '14 at 18:46
  • 3
    @JanDvorak Looks like the OP didn't turn on optimizations. – Mysticial Jun 11 '14 at 18:46
  • 1
    @Mysticial That can explain those useless argument moves, but where has the addition gone? Also, how did GCC produce _this_? – John Dvorak Jun 11 '14 at 18:48
  • 1
    @JanDvorak lol, dead code. I guess GCC still does *some* things even without optimizations. – Mysticial Jun 11 '14 at 18:49
  • @Mysticial filling registers and never using them, sure. But I can't imagine any plausible chain of events that would cause a compiler to call the next line unless it thought it might need the IP on the stack (which doesn't seem to be the case). It even shifts `[SP+x]` references (what if FPO was on?). Some weird form of buffer overflow protection? – John Dvorak Jun 11 '14 at 18:52
  • @JanDvorak I have a suspicion that since the return value is never used, GCC is omitting the entire portion of the IR DAG that holds the addition and the return value. The function call remains, but since there are no basic blocks in the function, it goes to the next basic block which is the next line in `main()`. That's just my guess. I have no idea exactly how GCC's IR -> code-gen pass works. – Mysticial Jun 11 '14 at 18:55
  • @JanDvorak Looks like fjardon has this one. – Mysticial Jun 11 '14 at 18:58
  • @Mysticial confirmed. Maybe the downvotes came from those who knew the answer? – John Dvorak Jun 11 '14 at 18:59
  • @JanDvorak I didn't downvote, but from what I've seen: Any low-level or performance related question that is not done with optimizations tends to get downvoted. – Mysticial Jun 11 '14 at 19:01

1 Answers1

6
  • You did not optimize so the calls are not inlined
  • You produced an object file (not a .exe) so the calls are not resolved. What you see is a dummy call whose address will be filled by the linker
  • If you compile a full executable you will see the correct addresses for the jumps

See page 28 of: http://www.cs.princeton.edu/courses/archive/spr04/cos217/lectures/Assembler.pdf

fjardon
  • 7,921
  • 22
  • 31