
CAVR-4
122
Taking advantage of the compilation system
AVR® IAR C/C++ Compiler
Reference Guide
CONTROLLING COMPILER OPTIMIZATIONS
The AVR IAR C/C++ Compiler allows you to specify whether generated code should
be optimized for size or for speed, at a selectable optimization level. The purpose of
optimization is to reduce the code size and to improve the execution speed. When only
one of these two goals can be reached, the compiler prioritizes according to the settings
you specify. Note that one optimization sometimes enables other optimizations to be
performed, and an application may become smaller even when optimizing for speed
rather than size.
The following table describes the optimization levels:
By default, the same optimization level for an entire project or file is used, but you
should consider using different optimization settings for different files in a project. For
example, put code that must execute very quickly into a separate file and compile it for
minimal execution time (maximum speed), and the rest of the code for minimal code
size. This will give a small program, which is still fast enough where it matters. The
#pragma optimize directive allows you to fine-tune the optimization for specific
functions, such as time-critical functions.
A high level of optimization will result in increased compile time, and may also make
debugging more difficult, since it will be less clear how the generated code relates to the
source code. At any time, if you experience difficulties when debugging your code, try
lowering the optimization level.
Optimization level Description
None (Best debug support) Variables live through their entire scope
Low Dead code elimination
Redundant label elimination
Redundant branch elimination
Medium Live-dead analysis and optimization
Code hoisting
Register content analysis and optimization
Code motion
Common subexpression elimination
Clustering of variables
High (Maximum optimization) Peephole optimization
Cross jumping
Cross call (when optimizing for size)
Function inlining
Type-based alias analysis
Table 30: Compiler optimization levels