What the compiler does for a function that contains only one function call

As the name implies, what happens if I have:

void a(uint8_t i) {
  b(i, 0);
}

Will the compiler replace the call with (i) with b (i, 0)?

In addition, in any case, it would be considered good practice to replace the above:

#define a(i) b(i, 0)
+4
source share
3 answers

This is pretty easy to verify. If the call ais in the same compilation unit, most compilers optimize it. Let's see what happens:

$ cat > foo.c
void b(int, int);

void
a(int a)
{
        b(a, 0);
}

void
foo(void)
{
        a(17);
}

Then compile it only with assembler with some basic optimizations (I added omit-frame-pointer to create cleaner output, you can make sure that it would happen the same way without this flag):

$ cc -fomit-frame-pointer -S -O2 foo.c

( , , ):

$ cat foo.s
a:
    xorl    %esi, %esi
    jmp b
foo:
    xorl    %esi, %esi
    movl    $17, %edi
    jmp b

, , a, b ( , , jmp jmp). foo a .

, , gcc, , clang . , - , , , .

+3

, (, ..) .

a() - - inline a(). , , , . - .

​​ static ( ), a() , ( ). , .

​​ inline ( ), . inline - , , , . , , .

, a() (, ), .

, ( ) . C - .

, . C, .

. (, , ..), , .

. , 0 , b(), a(), . , , a() - . , .

++ b() 0 .;)

+2

:

inline void a(uint8_t i) {
  b(i, 0);
}

Therefore, type calls a(i)will indeed be replaced byb(i, 0)

+1
source

Source: https://habr.com/ru/post/1620089/


All Articles