Sasha
Sasha

Reputation:

Meaning of gcc -O2

I see this flag a lot in the makefiles. What does it mean and when should it be used?

Upvotes: 21

Views: 41167

Answers (5)

RYUZAKI
RYUZAKI

Reputation: 127

Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Turning on optimization makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.

The default is optimization off. This results in the fastest compile time, but the compiler makes absolutely no attempt to optimize, and the generated programs are considerably larger and slower than when optimization is enabled. There are various -O switches (the permitted forms are -O0, -O1, -O2, -O3, and -Os) in GCC to control the optimization level:

-O0 No optimization; generates unoptimized code but has the fastest compilation time. This is default.

-O1 Moderate optimization; optimizes reasonably well but does not degrade compilation time significantly. It takes a lot more memory for large function.

-O2 GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify

-O3 Full optimization as in -O2; also uses more aggressive automatic inlining of subprograms within a unit and attempts to vectorize loops. It also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize options.

-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

To learn more about flags/options used at various optimization levels and their details: Options That Control Optimization

Upvotes: 1

Mekk
Mekk

Reputation: 1459

As per the man page:

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed 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.

In human words: it is the highest truly safe way of optimization. -O3 makes reorganizations which can be troublesome at times. The subject as such is fairly deep.

Upvotes: 3

Wadih M.
Wadih M.

Reputation: 13462

Compilers can use various optimization techniques like loop unrolling, CPU pipeline optimizations to find useless code and avoid data hazards to speed up your code. For example, a loop that happens a fixed amount of times will be converted to contiguous code without the loop control overhead. Or if all the loop iterations are independent, some code parallelization is possible.

Setting the optimization level to 2 tells how much energy the compiler should spend looking for those optimizations. The possible values range from 1 to 3.

You can learn more about what the compiler can do to optimize your code: Optimizing compiler

Upvotes: 3

vartec
vartec

Reputation: 134591

Optimization level 2. The maximum is 3. See: Options That Control Optimization

Note, that in a few years ago -O3 could cause some glitches by excessively "optimizing" the code. AFAIK, that's no longer true with modern versions of GCC. But with inertia, -O2 is considered "the maximum safe".

Upvotes: 15

Can Berk Güder
Can Berk Güder

Reputation: 113310

Optimization level 2.

From the GCC man page:

-O1 Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function.

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed 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 -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize options.

-O0 Reduce compilation time and make debugging produce the expected results. This is the default.

-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

Upvotes: 43

Related Questions