lexdene/gcc_five_minute — Issue #2: -O
Author: lexdene
Created: 2011-10-10 06:25:52+00:00
State: open

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

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 turns on the following optimization flags:

-fauto-inc-dec -fcprop-registers -fdce -fdefer-pop -fdelayed-branch
-fdse -fguess-branch-probability -fif-conversion2 -fif-conversion
-fipa-pure-const -fipa-reference -fmerge-constants -fshrink-wrap
-fsplit-wide-types -ftree-builtin-call-dce -ftree-ccp -ftree-ch
-ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse
-ftree-forwprop -ftree-fre -ftree-phiprop -ftree-sra -ftree-pta
-ftree-ter -funit-at-a-time

-O also turns on -fomit-frame-pointer on machines where doing so
does not interfere with debugging.

-O2 Optimize even more. GCC performs nearly all supported
optimizations that do not involve a space-speed tradeoff. As
compared to -O, this option increases both compilation time and the
performance of the generated code.

-O2 turns on all optimization flags specified by -O. It also turns
on the following optimization flags: -fthread-jumps
-falign-functions -falign-jumps -falign-loops -falign-labels
-fcaller-saves -fcrossjumping -fcse-follow-jumps -fcse-skip-blocks
-fdelete-null-pointer-checks -fexpensive-optimizations -fgcse
-fgcse-lm -finline-small-functions -findirect-inlining -fipa-sra
-foptimize-sibling-calls -fpeephole2 -fregmove -freorder-blocks
-freorder-functions -frerun-cse-after-loop -fsched-interblock
-fsched-spec -fschedule-insns -fschedule-insns2 -fstrict-aliasing
-fstrict-overflow -ftree-if-to-switch-conversion
-ftree-switch-conversion -ftree-pre -ftree-vrp

Please note the warning under -fgcse about invoking -O2 on programs
that use computed gotos.

NOTE: In Ubuntu 8.10 and later versions, -D_FORTIFY_SOURCE=2 is set
by default, and is activated when -O is set to 2 or higher. This
enables additional compile-time and run-time checks for several
libc functions. To disable, specify either -U_FORTIFY_SOURCE or
-D_FORTIFY_SOURCE=0.

-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, -ftree-vectorize and
-fipa-cp-clone 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.

-Os disables the following optimization flags: -falign-functions
-falign-jumps -falign-loops -falign-labels -freorder-blocks
-freorder-blocks-and-partition -fprefetch-loop-arrays
-ftree-vect-loop-version

If you use multiple -O options, with or without level numbers, the
last such option is the one that is effective.