8.3.2.4 Optimization

  • There is a general switch ‘-O<optimization_level>’ which specifies the level of optimization used when generating the code:
    • -Os

      This option requests space-orientated optimizations. This option requests all supported optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size, which might slow the program execution, such as procedural abstraction optimizations.

      This level is available only for licensed compilers.

    • -O0

      This option performs only rudimentary optimization - the default optimization level if no -O option is specified. The compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results with the optimization level selected.

      Statements are independent when compiling with this optimization level. If stopping the program with a breakpoint between statements, you can assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you would expect from the source code.

      The compiler only allocates variables declared in registers.

    • -O1 or -O

      This option reduces code size and execution time but still allows a reasonable level of debug-ability. This level is available for unlicensed as well as licensed compilers.

    • -O2

      At this level, the compiler performs nearly all supported optimizations that do not involve a space-speed trade-off. This level is available for unlicensed as well as licensed compilers.

    • -O3

      This option requests all supported optimizations that reduce execution time but might increase program size. This level is available only for licensed compilers.

    • -Og

      This option disables optimizations that severely interfere with debugging, offering a reasonable optimization level while maintaining fast compilation and a good debugging experience.

  • 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 be slower.

    • -fshort-enums

      Allocate to an enum type only as many bytes as it needs for the declared range of possible values. Specifically, the enum type will be equivalent to the minimum integer type, having enough room.