User guide

Running GCC
47
String dump of section '.GCC.command.line':
[ 0] hello.c
[ 8] -mtune=generic
[ 17] -O3
[ 1b] -Wall
[ 21] -frecord-gcc-switches
It is very important to test and try different options with a representative data set. Often, different
modules or objects can be compiled with different optimization flags in order to produce optimal
results. Refer to Section 4.1.6.5, “Using Profile Feedback to Tune Optimization Heuristics.” for
additional optimization tuning.
4.1.6.5. Using Profile Feedback to Tune Optimization Heuristics.
During the transformation of a typical set of source code into an executable, tens of hundreds of
choices must be made about the importance of speed in one part of code over another, or code size
as opposed to code speed. By default, these choices are made by the compiler using reasonable
heuristics, tuned over time to produce the optimum runtime performance. However, GCC also has
a way to teach the compiler to optimize executables for a specific machine in a specific production
environment. This feature is called profile feedback.
Profile feedback is used to tune optimizations such as:
Inlining
Branch prediction
Instruction scheduling
Inter-procedural constant propagation
determining of hot or cold functions
Profile feedback compiles a program first to generate a program that is run and analyzed and then a
second time to optimize with the gathered data.
Procedure 4.4. Using Profile Feedback
1. Step One
The application must be instrumented to produce profiling information by compiling it with -
fprofile-generate.
2. Step Two
Run the application to accumulate and save the profiling information.
3. Step Three
Recompile the application with -fprofile-use.
Step three will use the profile information gathered in step one to tune the compiler's heuristics while
optimizing the code into a final executable.
Procedure 4.5. Compiling a Program with Profiling Feedback
1. Compile source.c to include profiling instrumentation:
gcc source.c -fprofile-generate -O2 -o executable
2. Run executable to gather profiling information:
./executable