29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Compiler</strong> Coding Practices<br />

5.21 Automatic function inlining and static functions<br />

At -O2 and -O3 levels of optimization, or when --autoline is specified, <strong>the</strong> compiler can<br />

automatically inline functions if it is practical and possible to do so, even if <strong>the</strong> functions are not<br />

declared as __inline or inline. This works best <strong>for</strong> static functions, because if all use of a static<br />

function can be inlined, no out-of-line copy is required. Unless a function is explicitly declared<br />

as static (or __inline), <strong>the</strong> compiler has to retain <strong>the</strong> out-of-line version of it in <strong>the</strong> object file<br />

in case it is called from some o<strong>the</strong>r module.<br />

It is best to mark all non-inline functions as static if <strong>the</strong>y are not used outside <strong>the</strong> translation unit<br />

where <strong>the</strong>y are defined (a translation unit being <strong>the</strong> preprocessed output of a source file toge<strong>the</strong>r<br />

with all of <strong>the</strong> headers and source files included as a result of <strong>the</strong> #include directive). Typically,<br />

you do not want to place definitions of non-inline functions in header files.<br />

If you fail to declare functions that are never called from outside a module as static, code can<br />

be adversely affected. In particular, you might have:<br />

• A larger code size, because out-of-line versions of functions are retained in <strong>the</strong> image.<br />

When a function is automatically inlined, both <strong>the</strong> in-line version and an out-of-line<br />

version of <strong>the</strong> function might end up in <strong>the</strong> final image, unless <strong>the</strong> function is declared as<br />

static. This might increase code size.<br />

• An unnecessarily complicated debug view, because <strong>the</strong>re are both inline versions and<br />

out-of-line versions of functions to display.<br />

Retaining both inline and out-of-line copies of a function in code can sometimes be<br />

confusing when setting breakpoints or single-stepping in a debug view. The debugger has<br />

to display both in-line and out-of-line versions in its interleaved source view so that you<br />

can see what is happening when stepping through ei<strong>the</strong>r <strong>the</strong> in-line or out-of-line version.<br />

Because of <strong>the</strong>se problems, declare non-inline functions as static when you are sure that <strong>the</strong>y<br />

can never be called from ano<strong>the</strong>r module.<br />

5.21.1 See also<br />

Concepts<br />

• Inline functions on page 5-29<br />

• <strong>Compiler</strong> decisions on function inlining on page 5-30<br />

• Automatic function inlining and multifile compilation on page 5-34<br />

• Restriction on overriding compiler decisions about function inlining on page 5-35.<br />

Reference<br />

<strong>Compiler</strong> Reference:<br />

• --autoinline, --no_autoinline on page 3-14<br />

• -Onum on page 3-71.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-32<br />

ID061811<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!