Keil Logo

OPTIMIZE Compiler Directive




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




Options — C51 — Code Optimization.


The OPTIMIZE directive sets the optimization level and emphasis the Cx51 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.

Simple Access Optimizing: The compiler optimizes access of internal data and bit addresses in the 8051 system.

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 Data Overlaying: Data and bit segments suitable for static overlay are identified and internally marked. The BL51 Linker/Locator has the capability, through global data flow analysis, of selecting segments which can then be overlaid.
3 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.
4 Register Variables: Automatic variables and function arguments are located in registers when possible. Reservation of data memory for these variables is omitted.

Extended Access Optimizing: Variables from the IDATA, XDATA, PDATA and CODE areas are directly included in operations. Intermediate registers are frequently unnecessary.

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.

Case/Switch Optimizing: Code involving switch and case statements is optimized using jump tables or jump strings.

Simple Loop Optimizing: Program loops that fill a memory range with a constant are converted and optimized.
5 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.
6 Loop Rotation: Program loops are rotated if the resulting program code is faster and more efficient. The loop expression of for and while loops is evaluated once at the top of the loop and then at the bottom of the loop. This optimization generates more code but speeds up execution.
7 Extended Index Access Optimizing: DPTR is used for register variables where appropriate. Pointer and array access are optimized for both execution speed and code size.
8 Common Tail Merging: When there are multiple calls to a single function, some of the setup code can be reused, thereby reducing program size.
9 Common Block Subroutines: Recurring instruction sequences are detected and converted into subroutines. The Cx51 Compiler rearranges code to obtain larger recurring sequences.
10 Rearrange Code (Linker Optimization): When detecting common block subroutines, code is rearranged to obtain larger recurring sequences.
11 Reuse of Common Exit Code (Linker Optimization): Identical exit sequences are reused. This may reduce the size of common block subroutines even further. This optimization level generates the most compact program code possible.


  • Each higher optimization level contains all of the characteristics of the preceding lower optimization level. For example, OPTIMIZE level 9 includes all optimizations of levels 0 to 8.
  • 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(9)
    void func_2 (void)
See Also




#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.