How safe is gcc -O3 ?

Ian Lance Taylor iant@google.com
Tue Jul 20 14:30:00 GMT 2010


"Dr. David Kirkby" <david.kirkby@onetel.net> writes:

> How safe is it to distribute code for others to build, where you have
> selected -O3 for them? I'm a bit concerned that might be a bit
> aggressive to use as a default, given it will potentially be compiled
> by different gcc versions on different platforms.

The -O3 optoin should be safe for correct code.  An important difference
between -O2/-O3 and -O1 is that -O2 and -O3 enable strict aliasing and
strict overflow.  Those options provide better optimization for correct
code, but are far more likely to cause unexpected code generation for
incorrect code.  See the -fstrict-aliasing and -fstrict-overflow
options.

> I believe -O2 might be a more sensible default, but would welcome the
> opinions of others on the matter.

The main difference between -O3 and -O2 is that -O3 enables more
speculative optimizations.  These should not miscompile your code, but
they may cause your program to run more slowly.

Ian



More information about the Gcc-help mailing list