19.3 Using Optimizations

The MPLAB XC-DSC C/C++ Compiler for dsPIC33C/E/F and dsPIC30 DSC supports general, as well as several specific, optimization options (see section 6.7.6 Options for Controlling Optimization). In most cases, only general optimization options (-On) should be used (for details, see 19.2 How to Enable Optimization).

The more a compiler optimizes the output code, the further away from the C/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/C++ program and the optimizer has exposed this.