Optimizations

The optimizations in the MPLAB XC8 compiler can be broadly grouped into C-level optimizations performed on the source code before conversion into assembly and assembly-level optimizations performed on the assembly code generated by the compiler.

The C-level optimizations are performed early during the code generation phase and so have flow-on benefits: performing one optimization might mean that another can then be applied. As these optimizations are applied before the debug information has been produced, they have less impact on source-level debugging of programs.

Some of these optimizations are integral to the code generation process and so cannot be disabled via an option. Suggestions as to how specific optimizations can be defeated are given in the sections below.

If your compiler is unlicensed, some of the optimization levels are disabled (see Options for Controlling Optimization). Even if they are enabled, optimizations can only be applied if very specific conditions are met. As a result, you might see that some lines of code are optimized, but other similar lines are not.

The optimization level determines the available optimizations, which are listed in the Table 1 table.

Table 1. Optimization Level Sets
Level Optimization sets available
O0
  • Rudimentary Optimization
O1
  • Minimal code generator optimizations
O2
  • All generic code generator optimizations
  • Minimal assembly optimizations
O3
 (Licensed only)
  • All generic and speed-specific code generator optimizations
  • All generic and speed-specific assembler optimizations
Os
 (Licensed only)
  • All generic and space-specific code generator optimizations
  • All generic and space-specific assembler optimizations

Assembly-level optimizations are described in Assembly-Level Optimizations.

The minimal code generator optimizations consist of the following.

The following is a list of more advanced code generation (C-level) optimizations, which simplify C expressions or code produced from C expressions. These are applied across the entire program, not just on a module-by-module basis.

MPLAB X IDE or other IDEs can indicate incorrect values when watching variables if optimizations hold a variable in a register. Try to use the ELF/DWARF debug file format to minimize such occurrences. Check the assembly list file to see if registers are used in the routine that is being debugged.