1

I read that compiler may not perform inlining when "return" statement does not exist in function body and also in the case where return type is other than void. If it's like inlining cannot happen with functions that return anything other than void, why it needed a "return" statement for making the function inline. Assume simple code as below:

Here the function declared as inline does not have a "return" statement in its body. Does inlining happen here? Is there any way through which we can know if the inline request has been accepted and executed by compiler?

#include<stdio.h>
inline void call()
{
    printf("*****In call*****\n");
}

main()
{
    call();
}
Divya
  • 393
  • 2
  • 5
  • 17
  • Which compiler are you using? – Evil Dog Pie May 18 '15 at 11:35
  • Hi, I'm using gcc compiler – Divya May 18 '15 at 11:37
  • 1
    break out your disassembler. – WhozCraig May 18 '15 at 11:37
  • @WhozCraig : I do not have any idea about this disassembler. can we get to know if the function is inlined or not through this? – Divya May 18 '15 at 11:40
  • 3
    I don't know where you read that claim, but it is nonsense. – Mat May 18 '15 at 11:43
  • 1
    You have either read something that is very wrong, or misunderstood what it was trying to say. – molbdnilo May 18 '15 at 11:51
  • I mean once you fix your code (see [this answer](http://stackoverflow.com/questions/16245521/c99-inline-function-in-c-file/16245669#16245669)), you can send your code through to the asembler-phase to at least see if that is putting your code inline. That may still not tell the whole story if link-time-optimization modifies even further. A running disassembly from a debugger [like this one](http://pastebin.com/vgJEafR7) will tell the real tale on your platform. – WhozCraig May 18 '15 at 11:52
  • @molbdnilo: yes..true.. I just went back and re-read the post. Thank you. – Divya May 18 '15 at 13:14
  • @WhozCraig: That was very informative. thank you. I understood how we can check if inline request is accepted by complierr or not – Divya May 18 '15 at 13:16

3 Answers3

2

This is obviously a compiler specific question, but since you are using gcc, here is what gcc produces:

.cfi_startproc
subq    $8, %rsp
.cfi_def_cfa_offset 16
movl    $.LC0, %edi
call    puts
xorl    %eax, %eax
addq    $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc

where .LC0 is your hardcoded string (complete assembly dump). As you can see there is no call to call here, so yes, gcc does inline this call with -O2.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
1

I read that compiler may not perform inlining when "return" statement does not exist in function body

This is not at all true. Compiler can certainly inline void functions, but whether it does inline any function is upto it even if you specify inline keyword.

Just see here: https://goo.gl/xEg6AK

This is the generated assembly:

.LC0:
    .string "*****In call*****"
main:
    subq    $8, %rsp
    movl    $.LC0, %edi
    call    puts
    movl    $0, %eax
    addq    $8, %rsp
    ret

GCC does inline your code when compiled with -O. It also replaced the printf call with a simple puts.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

The GCC compiler provides a number of extensions to the standard C language that allow you to add 'attributes' to functions (as well as to types and variables which are not relevant here). One of these is __attribute__((always_inline)), which overrides the compiler's inlining algorithm.

#include<stdio.h>
inline void __attribute__((always_inline)) call()
{
    printf("*****In call*****\n");
}

main()
{
    call();
}

In this case, the instruction(s) to make the call to the printf library routine will be inlined at the point the call function appears in the calling code.

Evil Dog Pie
  • 2,300
  • 2
  • 23
  • 46