Problems with Optimizing Compilers
The GNU C compiler has several optimization command-line options, all of which are variants of –O. Specifying –O3 turns on all available gcc optimizations, regardless of their effects on the speed-versus-size tradeoff. The command-line option –Os also optimizes the code for size. For a detailed explanation of the different gcc optimization levels, refer to the gcc online manual at http://gcc.gnu.org/onlinedocs.
Murphy’s Law dictates that the first time you enable the compiler’s optimization feature, your previously working program will suddenly fail. Perhaps the most notorious of the automatic optimizations is “dead code elimination.” This optimization eliminates code that the compiler believes to be either redundant or irrelevant. For example, adding zero to a variable requires no runtime calculation whatsoever. But you might still want the compiler to generate those “irrelevant” instructions if they perform some function that the compiler doesn’t know about.
For example, given the following block of code, most optimizing
compilers would remove the first statement because the value of *pControl is not used before it is overwritten
(on the third line):
*pControl = DISABLE;
*pData = 'a';
*pControl = ENABLE;But what if pControl and
pData are actually pointers to
memory-mapped device registers? In that case, the peripheral device
would not receive the DISABLE command before the byte of data was written. This could potentially wreak havoc on all future ...