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.
Level | Optimization sets available |
---|---|
O0 |
|
O1 |
|
O2 |
|
O3
(Licensed only) |
|
Os
(Licensed only) |
|
Assembly-level optimizations are described in Assembly-Level Optimizations.
The minimal code generator optimizations consist of the following.
volatile
and
whose value can be exactly determined are replaced with the numerical value.
Uninitialized global variables are assumed to contain zero prior to any assignment to
them.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.
volatile
will never be removed (see Volatile Type Qualifier). Taking the
address of a variable or referencing its assembly-domain symbol in hand-written assembly
code also constitutes use of the variable.volatile
. The assignment statement is completely
removed, as if it was never present in the original source code. No code will be
produced for it and you will not be able to set a breakpoint on that line in the
debugger.GLOBAL
directive.void
. __persistent
(see )Bank Type Qualifier. All __persistent
objects are not cleared by the runtime startup code, so this optimization will speed
execution of the program startup.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.