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 — CA — Code Optimization.


The OPTIMIZE directive sets the optimization level and emphasis.

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 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.
3 Local Common Subexpression Elimination: If the same calculations are performed repetitively in an expression, the result of the first calculation is saved and reused when possible. Superfluous 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.
4 Life Variable Analysis: Redundant store operations are replaced with the use of automatic variables.
5 Constant Propagation: The values of expressions are tracked and, when possible, constant values are inserted instead of variable accesses.
6 Common Tail Merging: When there are multiple calls to a single function, some of the setup code can be reused, thereby reducing program size.
7 Strength Reduction: Array accesses are replaced by pointer operations to speed-up loops.

Invariant Code Motion: Invariant code is moved into the setup portion of a loop which causes the loop to execute faster.
8 Static Access Optimization: Re-uses the base address of prior load and store operations and uses an offset to this base address.


  • 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.
  • You may change the optimizer level on a function-by-function basis as needed. For example:
    #pragma OT(3)
    void funct_1 (void)
    #pragma OT(8)
    void func_2 (void)


#pragma ot(6, SIZE)

#pragma ot(size)
  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.