11.07.2015 Views

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Writing efficient code}*p2 = 0;*p1 = 1;return *p2;With type-based alias analysis, it is assumed that a write access to the short pointed toby p1 cannot affect the long value that p2 points to. Thus, it is known at compile timethat this function returns 0. However, in non-standard-conforming C or <strong>C++</strong> code thesepointers could overlap each other by being part of the same union. By using explicitcasts, you can also force pointers of different pointer types to point to the same memorylocation.Writing efficient codeThis section contains general programming hints on how to implement functions tomake your applications robust, but at the same time facilitate compiler optimizations.The following is a list of programming techniques that will, when followed, enable thecompiler to better optimize the application.●●●●Local variables—auto variables and parameters—are preferred over static or globalvariables. The reason is that the optimizer must assume, for example, that calledfunctions may modify non-local variables. When the life spans for local variablesend, the previously occupied memory can then be reused. Globally declaredvariables will occupy data memory during the whole program execution.Avoid taking the address of local variables using the & operator. There are two mainreasons why this is inefficient. First, the variable must be placed in memory, andthus cannot be placed in a processor register. This results in larger and slower code.Second, the optimizer can no longer assume that the local variable is unaffectedover function calls.Module-local variables—variables that are declared static—are preferred overglobal variables. Also avoid taking the address of frequently accessed staticvariables.The compiler is capable of inlining functions. This means that instead of calling afunction, the compiler inserts the content of the function at the location where thefunction was called. The result is a faster, but often larger, application. Also,inlining may enable further optimizations. The compiler often inlines smallfunctions declared static. The use of the #pragma inline directive and the <strong>C++</strong>keyword inline gives you fine-grained control, and it is the preferred methodcompared to the traditional way of using preprocessor macros. This feature can bedisabled using the --no_inline command line option; see --no_inline, page 157.124<strong>MSP430</strong> <strong>IAR</strong> C/<strong>C++</strong> <strong>Compiler</strong>Reference Guide

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

Saved successfully!

Ooh no, something went wrong!