7.6.1.5 Optimization
- There is a general switch
‘-O<optimization_level>’ which specifies the level of optimization used when
generating the code:
-Os
Signal that the generated code should be optimized for code size. The compiler will not care about the execution performance of the generated code.
-O0
No optimization. This is the default. GCC will generate code that is easy to debug but slower and larger than with the incremental optimization levels outlined below.
-O1 or -O
This will optimize the code for both speed and size. Most statements will be executed in the same order as in the C/C++ code, and most variables can be found in the generated code, making the code quite suitable for debugging.
-O2
Turn on most optimizations in GCC except for some optimizations that might drastically increase code size, which enables instruction scheduling, which allows instructions to be shuffled around to minimize CPU stall cycles because of data hazards and dependencies, for CPU architectures that might benefit from this. Overall this option makes the code small and fast but hard to debug.
-O3
Turn on some extra performance optimizations that might drastically increase code size but increase performance compared to the -O2 and -O1 optimization levels. This includes performing function inlining.
- Other optimization options
-ffunction-sections
-fdata-sections
Place each function or data item into its section in the output file if the target supports arbitrary sections. The function name or the name of the data item determines the section's name in the output file.
Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker will create a larger object and executable files and will also be slower.
-funroll-loops
If code size is not a concern, then some extra performance might be obtained by making GCC unroll loops by using the ‘-funroll-loops’’ switch in addition to the ‘-O3’ switch.