4.2. Selecting Compiler Options¶
After your application has been fully debugged and is working properly, it is time to begin the optimization process. First, you need to select appropriate compiler options.
Before beginning the process of selecting compiler options, you may want to make a list of files that contain performance-critical code and a list of files that contain code whose performance is not critical.
4.2.1. Selecting Compiler Options for Best Performance¶
For those files and functions where performance is critical, you'll want to use compiler options that tell the compiler to perform optimizations that improve performance. The following compiler options affect performance. See the C7000 C/C++ Compiler User's Guide (SPRUIG8) for more details on command-line options.
--opt_level=3 (-o3) The compiler performs function-level optimization at
--opt_level=2
and file-level optimization and function inlining at--opt_level=3
. At both--opt_level=2
and--opt_level=3
, the compiler performs various loop optimizations, such as software pipelining, vectorization, and loop coalescing. By default, the--opt_level
switches optimize for performance. Such optimization can increase code size. If code size is an issue, do not reduce the level of optimization. Instead use the--opt_for_speed
(-mf
) switch to change the optimization goal (performance versus code size) and the-oi
option to control the amount of automatic inlining.--opt_level=4 (-o4) Consider using this option to perform optimizations across all files at link-time. Using this option can increase compile time significantly. If used for any step, this option must be used at all compilation and linking steps. Source files can be compiled separately, as long as they are all compiled with
--opt_level=4
. This optimization level cannot be used with--program_level_compile
(-pm
).--gen_func_subsections (-mo) Consider using this option if the source code uses many functions that are never called. This option places each function in its own input subsection, so the linker can exclude that function from the executable if it is never referenced. However, this optimization can increase code size, because there are minimum section alignment requirements the compiler must apply.
--opt_for_speed=4 (-mf4) or higher When performance is the top concern, use these options when compiling files with functions that are executed frequently or are performance-critical. This tells the compiler to optimize for performance, even at the expense of code size.
Do not use the --disable_software_pipelining
(-mu
) option if you are concerned about performance. This option turns off software pipelining. Software pipelining is critical to achieving high performance on most inner loops. This option can be a debugging tool, as it makes the assembly code easier to understand. See Software Pipelining to learn more about pipelining.
4.2.2. Selecting Compiler Options for Lowest Code Size¶
For those files and functions where the smallest code size is important, or performance is not critical, consider using compiler options that tell the compiler to favor lower code size over higher performance.
In addition to the options in the previous section, the following options will have effects on code size.
--opt_for_speed=0 (-mf0) or --opt_for_speed=1 (-mf1) If code size is a concern, use these options when compiling files with functions that are not executed often or are not critical to performance. This tells the compiler to optimize for code size instead of performance. There are many optimizations that are affected by this compiler flag, but the most impactful optimizations that are changed with a varying
--opt_for_speed
level are software pipelining, automatic inlining, vectorization, and unrolling.--auto_inline=size The compiler automatically inlines small functions when using
-o3
. In order to make targeted code size reductions where automatic inlining is playing a role, the--auto_inline=size
option can be used. This option has no effect at-o2
or below, as automatic inlining is disabled at-o2
and below. For more information about the--auto_inline=size
option, see the C7000 C/C++ Compiler User's Guide (SPRUIG8). Note that the compiler does reduce the amount of automatic inlining as the--opt_for_speed
level is reduced.
Note
Do not lower the optimization level (--opt_level
) in an attempt to lower code size.
4.2.3. Understanding the Performance and Code Size Tradeoff¶
Several major compiler optimizations for C7000 result in a tradeoff between
code size and performance. For example, the compiler's use of software pipelining
to improve performance of inner loops will result in larger code size than
if software pipelining was not used. The code size/performance tradeoff can
be controlled with the use of the --opt_for_speed
/-mf
compiler option.
Our testing has found that the code size difference between
--opt_for_speed=5
/-mf5
and --opt_for_speed=0
/-mf0
results
in an average code size reduction of approximately 40%. Your results
will likely vary. The performance reduction when going from
--opt_for_speed=5
/-mf5
to --opt_for_speed=0
/-mf0
varies widely
and depends on the application/algorithm and the type of
optimizations the compiler is able to perform on that particular
application/algorithm. In some cases, especially with code that doesn't loop,
or has little computation, performance may degrade insignificantly.
In other cases, performance will degrade by an order of magnitude or more.
We encourage the user to use
--opt_for_speed=4
/-mf4
or higher with performance-critical code and
--opt_for_speed=1
/-mf1
or lower when code size is a primary concern.
Please see the C7000 C/C++ Compiler User's Guide (SPRUIG8)
(see the sections "Invoking Optimization" and "Controlling Code Size
Versus Speed") for more information on the --opt_level
option and the --opt_for_speed
option.
4.2.4. Compiler Options that Provide Additional Information¶
The following options provide additional information for debugging and performance evaluation purposes:
--src_interlist (-s) This option causes the compiler to emit into the compiler-generated assembly files a copy of what the source code looks like after high-level optimization. This output is placed in the assembly files as comments among the assembly code. The comments output from the optimizer look like C code and show the high-level transformations that have been applied such as inlining, loop coalescing, and vectorization. This option can be useful in helping you understand the assembly code and some of what the compiler is doing to optimize the performance of the code. This option turns on the
--keep_asm
(-k
) option, so the compiler-generated assembly (.asm) files will not be deleted.--debug_software_pipeline (-mw) This option emits extra information about software-pipelined loops into the compiler-generated assembly file, including the single-scheduled iteration of the loop. This information is used in loop tuning examples presented later in this document. This option turns on the
--keep_asm
(-k
) option, so the compiler-generated assembly (.asm) files will not be deleted.--gen_opt_info=2 (-on2) This option creates a .nfo file with the same base name as the .obj file. This file contains summary information regarding the high-level optimizations that have been applied, as well as providing advice.