Ten GCC optimization flags that must be known
The GCC provides the capability to generate optimized compiled code. When you are running gcc to compile your code you can choose between a large number of optimization levels using the following switches:
- O0 - This is the default option, this option says to the compiler “Do not optimize” or “Remove all previous declared optimizations”. If you have done a good work on your source code you should apply this option, or better you shouldn't apply any option, and you should be happy otherwise in the following paragraph you will find something useful.
- O or O1 - With O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. O also turns on fomitframepointer on machines where doing so does not interfere with debugging. This option should solve the most part of your problems, this option could generate side effects, so before you decide to use this option you should try to use the options described at paragraph 8.1.6, 8.1.7, 8.1.8, 8.1.9, 8.1.10. All these options can be ored.
- O2 - GCC performs nearly all supported optimizations that do not involve a spacespeed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify O2. As compared to O, this option increases both compilation time and the performance of the generated code.
- O3 - Optimize yet more. O3 turns on all optimizations specified by O2 and also turns on the finlinefunctions, funswitchloops and fgcseafterreload options.
- Os - This option says to the compiler “Optimize to reduce the size”. This option enables all O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.
- finlinefunctions - Integrate all simple functions into their callers. The compiler heuristically decides which functions are simple enough to be worth integrating in this way.
- funrollloops - Unroll loops whose number of iterations can be determined at compile time or upon entry to the loop. funrollloops implies both fstrengthreduce and freruncseafterloop. This option makes code larger, and may or may not make it run faster.
- floopoptimize - Perform loop optimizations: move constant expressions out of loops, simplify exit test conditions and optionally do strengthreduction as well. Enabled at levels O, O2, O3, Os.
- ftreepre - Perform Partial Redundancy Elimination (PRE) on trees. This flag is enabled by default at O2 and O3.
- ftreefre - Perform Full Redundancy Elimination (FRE) on trees. The difference between FRE and PRE is that FRE only considers expressions that are computed on all paths leading to the redundant computation. This analysis faster than PRE, though it exposes fewer redundancies. This flag is enabled by default at O and higher.
GCC provides a lt other switches, but I can say these are the most important ones.