how to turn on the compiler optimizer?

hi masters,
i'm reading inside the c++ object model,
in chapter 3, it mentions things such like "turn on the compiler optimizer".
is there a "switch" such that i can turn on and off the optimizer of compilers, e.g. g++, vc2008?
In g++ use the flag -O3. I don't know about VC stuff.
Thanks Galik,
1. any difference turning this on and off?
2. is it on by default?
3. if not, why not keep it as on by default?
It makes a huge difference in the code generated. It is not on by default.

It is very important to turn optimisation OFF when running a debugger. The code paths are completely rewritten by the optimiser so following along in the source code inside the debugger becomes very strange.

So I use -O0 (optimiser off) during development and -O3 only for a release.
Thank you!
then my understanding is that -O0 is mandatory during development and -O3 is mandatory for a release.
right?
neglect either is a bad habit, right?
Optimization is never mandatory, but it's an EXCELLENT idea. -O0 is also not mandatory during development, but if you're going to be using a debugger (which is an EXCELLENT idea), then it's an EXCELLENT idea as well (this one is actually closer to mandatory, because depending on the degree of optimization, it can become a nightmare to debug).

Another switch for GCC (that I listed in a clone thread in the Beginner's section) is -Os, which turns on all the features for -O2 that do not "typically" increase size, and then tries to optimize the code for size.

A list:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

-Albatross
Last edited on
Albatross,
many thanks!!!
Well, that's not the whole story. There are in-between optimisations -O1 & -O2. Different levels perform different types of optimisation which may impact the size of the executable.

If you want to optimise for space rather than speed then there is also -Os, makes the code as small as possible.

This is from the GCC man page:


Options That Control Optimization

These options control various sorts of optimizations.

Without any optimization option, the compiler’s goal is to reduce the
cost of compilation and to make debugging produce the expected results.
Statements are independent: if you stop the program with a breakpoint
between statements, you can then assign a new value to any variable or
change the program counter to any other statement in the function and
get exactly the results you would expect from the source code.

Turning on optimization flags 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 compiler performs optimization based on the knowledge it has of the
program. Compiling multiple files at once to a single output file mode
allows the compiler to use information gained from all of the files
when compiling each of them.

Not all optimizations are controlled directly by a flag. Only
optimizations that have a flag are listed.

-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
-finline-small-functions -fipa-pure-const -fipa-reference
-fmerge-constants -fsplit-wide-types -ftree-builtin-call-dce
-ftree-ccp -ftree-ch -ftree-copyrename -ftree-dce
-ftree-dominator-opts -ftree-dse -ftree-fre -ftree-sra -ftree-ter
-funit-at-a-time

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

-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 -findirect-inlining -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-switch-conversion -ftree-pre -ftree-vrp

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

-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.

-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


Topic archived. No new replies allowed.