20.3 Using Optimizations

The MPLAB XC16 C compiler supports general, as well as several specific, optimization options (see section Options for Controlling Optimization). In most cases, only general optimization options (-On) should be used (for details, see How to Enable Optimization).

The more a compiler optimizes the output code, the further away from the C program the code might become. This is the nature of optimization and is often exasperated by a weakness of debug information to represent these changes. Optimizations tend to:

  • convert structures into scalar variables (to remove unused members)
  • re-order flow, or duplicate it, for speed improvements
  • capture results, or partial results, for re-use later
  • remove unreachable code
  • remove unused variables or promote an object to a register variable
  • remove code that has no externally visible effect

Many of these transformations can make debugging code much more difficult. Some of them can turn a “working” program into something that no-longer executes correctly; typically, this means that the working program is not a well-formed C program and the optimizer has exposed this.