5

There is such code:

int fun1(){
   return 2 + 3;
}

inline int fun2(){  
   return 4 + 5;
}

int main(){
    int a = fun1();
    int b = fun2();
    return 0;
}

and corresponding assembly code:

    .file   "prog47.cpp"
    .text
.globl _Z4fun1v
    .type   _Z4fun1v, @function
_Z4fun1v:
.LFB0:
    .cfi_startproc
    .cfi_personality 0x0,__gxx_personality_v0
    pushl   %ebp
    .cfi_def_cfa_offset 8
    movl    %esp, %ebp
    .cfi_offset 5, -8
    .cfi_def_cfa_register 5
    movl    $5, %eax
    popl    %ebp
    ret
    .cfi_endproc
.LFE0:
    .size   _Z4fun1v, .-_Z4fun1v
    .section    .text._Z4fun2v,"axG",@progbits,_Z4fun2v,comdat
    .weak   _Z4fun2v
    .type   _Z4fun2v, @function
_Z4fun2v:
.LFB1:
    .cfi_startproc
    .cfi_personality 0x0,__gxx_personality_v0
    pushl   %ebp
    .cfi_def_cfa_offset 8
    movl    %esp, %ebp
    .cfi_offset 5, -8
    .cfi_def_cfa_register 5
    movl    $9, %eax
    popl    %ebp
    ret
    .cfi_endproc
.LFE1:
    .size   _Z4fun2v, .-_Z4fun2v
    .text
.globl main
    .type   main, @function
main:
.LFB2:
    .cfi_startproc
    .cfi_personality 0x0,__gxx_personality_v0
    pushl   %ebp
    .cfi_def_cfa_offset 8
    movl    %esp, %ebp
    .cfi_offset 5, -8
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $16, %esp
    call    _Z4fun1v
    movl    %eax, 12(%esp)
    call    _Z4fun2v        # why fun2 is called?
    movl    %eax, 8(%esp)
    movl    $0, %eax
    leave
    ret
    .cfi_endproc
.LFE2:
    .size   main, .-main
    .section    .note.GNU-stack,"",@progbits

Why function fun2 is not inlined and called like normal function? I have read that inline keyword is only hint for compiler and it doesn't have to inline function, however definition of fun2 is so simple, so it could be inlined. How to force g++ to inline functions?

scdmb
  • 15,091
  • 21
  • 85
  • 128
  • [Are you certain you need the function be inline? Or is this question just asked out of interest?] – marnir Oct 23 '11 at 14:56
  • 5
    All of the code should simply disappear since it has no useful side effects. Don't forget to enable the optimizer. – Hans Passant Oct 23 '11 at 14:57
  • 1
    Turning on some optimizations and declaring your inline functions as static would be a good start. – cyco130 Oct 23 '11 at 14:58
  • Beware function inlining is only active in -O3 mode http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/072/7269/7269t1.jpg – log0 Oct 30 '11 at 10:26
  • @cyco130, good idea. Should one always "static inline T f(...)", can it change what's inlined or not ? – denis Jun 03 '13 at 13:23

3 Answers3

14

Turn on optimizations. This is what you get for the main function with -O2 (x86_64):

0000000000400560 <main>:
  400560:   31 c0                   xor    %eax,%eax
  400562:   c3                      retq   

It's not only inline, it's removed.

Without optimizations, the compiler is much less likely to inline. (Inlining makes the code harder to debug, so having only very moderate levels of inlining with default non-optimizing options is a good idea.)

Mat
  • 202,337
  • 40
  • 393
  • 406
7

GCC has an attribute which forces inlining: always_inline

inline int fun2()  __attribute__((always_inline));
inline int fun2() {  
   return 4 + 5;
}

Will cause it to work on any optimization settings.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • 5
    I would say never force the compiler to inline. The compiler is always going to do a better job than a human when deciding what to inline. A simpler idea is just to **turn on** the optimization that will do the inlining. – Martin York Oct 23 '11 at 17:40
  • 1
    I would also recommend never forcing the compiler to inline. But I wouldn't agree that the compiler will *always* do a better job than a human. Sometimes the human will win, and the sort of human that will win is also the sort of human who knows when to disregard this advice. For the rest of us, trust the compiler :) – ObscureRobot Oct 23 '11 at 21:43
  • @LokiAstari There are a few cases where inline should be forced. It is very useful in debugging, and also when used to alias functions. Same functionality as macros except type safe. – Pubby Oct 24 '11 at 00:10
  • 1
    @ObscureRobot: Unfortately there are three types of human: 1) people who know they don't know anything about compilers. 2) People who think they know stuff about compilers 3) People who know enough about compiler to know they don't know about compilers. Non programmers fall into (1) most programmers fall into (2) – Martin York Oct 24 '11 at 01:06
  • IIRC, the "always" in "always_inline" refers the optimization level of the compiler. That is, even if optimization is turned off, the function will get inlined. – Shalom Craimer May 26 '14 at 07:59
5

inline is a compiler hint. The compiler may or may not actually inline your code.

ObscureRobot
  • 7,306
  • 2
  • 27
  • 36