7.3.2.4 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 code size optimized. The compiler will not care about the execution performance of the generated code.

    • -O0

      No optimization. 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 execute in the same order as in the C/C++ code, and most variables are in the generated code, making the code quite suitable for debugging, which is the default.

    • -O2

      Turn on most optimizations in GCC except for some optimizations that might drastically increase code size, enabling 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, including 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 larger objects and executable files and be slower.

    • -funroll-loops

      Perform loop unrolling when the iteration count is known. Obtain extra performance by making GCC unroll loops using the ‘-funroll-loops’’ and ‘-O3’ switches if code size is not a concern.