Keil Logo

OPTIMIZE Compiler Directive




A decimal number between 0 and 8 enclosed in parentheses optionally followed by SIZE or SPEED to specify code size or execution speed emphasis.




Options — C166 — Code Optimization.


The OPTIMIZE directive sets the optimization level and emphasis the C166 Compiler uses when generating object code.

Level Description
0 Constant Folding: The compiler performs calculations that reduce expressions to numeric constants, where possible. This includes calculations of run-time addresses.

Jump Optimizing: The compiler always extends jumps to the final target. Jumps to jumps are eliminated.
1 Dead Code Elimination: Unused code fragments and artifacts are eliminated.

Jump Negation: Conditional jumps are closely examined to see if they can be streamlined or eliminated by the inversion of the test logic.
2 Access Optimizing: Variables accesses are directly included in operations. Intermediate registers are frequently unnecessary.
3 Unreachable Code Analysis: Code that cannot be reached because of constant conditional expressions or unreferenced labels is removed.

Register Variables: Automatic variables and function arguments are located in registers when possible. Reservation of data memory for these variables is omitted.
4 Local Common Subexpression Elimination: The compiler detects multiple uses of the same expression or subexpression. The result of the first expression is saved and reused when possible. Superfluous expression calculations are eliminated from the code.

Global Common Subexpression Elimination: Identical subexpressions within a function are calculated only once when possible. The intermediate result is stored in a register and reused.

Case/Switch Optimizing: Code involving switch and case statements is optimized using jump tables or jump strings.
5 Life Variable Analysis: Redundant store operations are replaced with the use of automatic variables.
6 Constant Propagation: The values of expressions are tracked and, when possible, constant values are inserted instead of variable accesses.

Peephole Optimizing: Redundant MOV instructions are removed. This includes unnecessary loading of objects from the memory as well as load operations with constants. Complex operations are replaced by simple operations when memory space or execution time can be saved.
7 Common Tail Merging: When there are multiple calls to a single function, some of the setup code can be reused, thereby reducing program size. The compiler analyzes the generated code function-by-function and locates common heads and tails. If the compiler detects common code sequences, it replaces one code sequence by a jump instruction to the other equivalent code sequence. This situation frequently occurs with switch/case statements. While analyzing the code, the compiler also replaces sequences with shorter instructions.
8 Register Variable Lifetime Analysis: Register variables are relocated in CPU registers to improve execution speed and code density.


  • Each higher optimization level contains all of the characteristics of the preceding lower optimization level. For example, OPTIMIZE level 7 includes all optimizations of levels 0 to 6.
See Also




#pragma ot(6, SIZE)

#pragma ot(size)

Related Knowledgebase Articles

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.