0

Can you please tell me why the following function func1 is not getting inlined?

Code

#include <stdio.h>
#include <stdlib.h>

static inline int func1(int a) {
    return a*2;
}

int main(int argc, char **argv) {

    int value = strtol(argv[1], NULL, 0);

    value = func1(value);

    printf("value: %d\n", value);

    return 0;
}

Run

$ gcc -Wall -o main main.c
$ objdump -D main | grep func1
0000000000000700 <func1>:
 742:   e8 b9 ff ff ff          callq  700 <func1>
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
Gábor
  • 371
  • 3
  • 8
  • 9
    Enable optimization and it probably will inline it. – Barmar Oct 16 '19 at 21:03
  • 3
    ..Probably even without marking it `inline`. – Eugene Sh. Oct 16 '19 at 21:09
  • OT: regarding: `int value = strtol(argv[1], NULL, 0);` 10 Do not access beyond `argv[0]` with out first checking `argc` to assure the user actually entered the expected number of command line parameters 2) should check the error indication from `strtol()` to assure some number was actually input 3) `strtol()` returns a `long int`, not an `int` – user3629249 Oct 16 '19 at 21:37
  • the posted code does not compile! The main reason is it is missing the statement: `#include – user3629249 Oct 16 '19 at 21:41
  • https://gcc.gnu.org/onlinedocs/gcc/Inline.html – EsmaeelE Oct 17 '19 at 00:32
  • Define function as `static inline int __attribute__((always_inline))func1(int a) { return a*2; }` without any optimize inline func1 and objdump shows nothing – EsmaeelE Oct 17 '19 at 00:33
  • Enable optimization `gcc -Wall -o main main.c -O3` make func1 inline. – EsmaeelE Oct 17 '19 at 00:49
  • Related: https://stackoverflow.com/questions/3800339/why-didnt-o3-gcc-optimization-inline-this-function – EsmaeelE Oct 17 '19 at 00:50

2 Answers2

2

inline is literally a mere "suggestion".

Per 6.7.4 Function specifiers, paragraph 6 of the C standard:

A function declared with an inline function specifier is an inline function. Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

As inline is just a suggestion, a function with the inline specifier may or may not be inlined as the implementation determines.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • yes, I understand it is a suggestion only but what will be inlined if this simple function is not? And what is the reason for that this function is not got inlined? – Gábor Oct 16 '19 at 21:20
  • @Gábor The function didn't get inlined because the compiler you used, with the options you passed it, didn't inline it. That's really the only "why" there is. If you want a function inlined, you have to take care to use a compiler that inlines functions and use it with the arguments necessary to make the inlining you want actually happen. Barmer got right to the point with the first comment. – Andrew Henle Oct 16 '19 at 21:25
2

I believe it is not inlined because one is not doing optimisation, (for debugging, I assume.) From the GCC online docs,

GCC does not inline any functions when not optimizing unless you specify the ‘always_inline’ attribute for the function

Neil
  • 1,767
  • 2
  • 16
  • 22