4.8 Function inlining with the linker

The linker inlines functions depending on what options you specify and the content of the input files.

The linker can inline small functions in place of a branch instruction to that function. For the linker to be able to do this, the function (without the return instruction) must fit in the four bytes of the branch instruction.

The following options are available to control function inlining:

  • --inline and --no_inline command-line options allow you to control branch inlining. However, --no_inline only turns off inlining for user-supplied objects. The linker still inlines functions from the ARM® C Library by default.
  • --inline_type=type command-line option gives you more control over inlining. You can also inline functions from the ARM C Library, and turn off inlining completely. This option overrides --inline if both are present on the command-line.

If branch inlining optimization is enabled, the linker scans each function call in the image and then inlines as appropriate. When the linker finds a suitable function to inline, it replaces the function call with the instruction from the function that is being called.

The linker applies branch inlining optimization before any unused sections are eliminated so that inlined sections can also be removed if they are no longer called.

Note:

The linker can inline two 16-bit encoded Thumb instructions in place of the 32-bit encoded Thumb BL instruction.

Use the --info=inline command-line option to list all the inlined functions.

For example, consider the following source files:

bar.c
----------------------------------------------
int myIncrement(int a)
{
    return a+1;
}

main.c
----------------------------------------------
extern int myIncrement(int);

int main()
{
    int i=1;
    i=myIncrement(i);
}

Linking with the --inline option shows that the short function myIncrement() is inlined:

armcc -c bar.c main.c
armlink  --inline --info=inline main.o bar.o

==============================================================================

Small function inlining results

Inlined function myIncrement from bar.o at offset 0x8 in main.o(.text).
Inlined Instruction: ADD      r0,r0,#1

Use fromelf to compare the results of linking with and without --inline:

armlink main.o bar.o
fromelf --disassemble __image.axf

...     
main PROC
|L1.164|
        PUSH     {r4,lr}
        MOV      r0,#1
        BL       |L1.184|   ; Branch to function
        MOV      r0,#0
        POP      {r4,pc}
        ENDP

myIncrement PROC
|L1.184|
        ADD      r0,r0,#1
        BX       lr
        ENDP
...     
        
armlink main.o bar.o --inline
fromelf --disassemble __image.axf

...
main PROC
|L1.164|
        PUSH     {r4,lr}
        MOV      r0,#1
        ADD      r0,r0,#1   ; Inlined function 
        MOV      r0,#0
        POP      {r4,pc}
        ENDP

...
Non-ConfidentialPDF file icon PDF versionARM DUI0474M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.