binary compiled with -O1 and w/ individual optimization flags are not the same
Brian Dessent
brian@dessent.net
Fri Feb 29 20:15:00 GMT 2008
CSights wrote:
> I'm trying to debug some mismatching results from a program compiled with
> O1,2,3) and without (-O0 or nothing) optimization flags.
> My thought was to individually turn on optimization flags and see which one
> changes the program's output.
-O is not just a combination of a bunch of -f flags. It doesn't work
that way. There are optimizations that are controlled directly by -O,
with no corresponding -f. The manual says this at the top of
<http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html>:
> Not all optimizations are controlled directly by a flag. Only optimizations that have a flag are listed.
If you have a program that behaves differently with and without
optimization, then it's probably relying on undefined behavior. A
common mistake is to violate the C aliasing rules. Compile your code
with -O2 -fno-strict-aliasing and see if that makes the problem go
away. If it does that's a good indication that it's an aliasing issue.
Then compile with -O2 -Wstrict-aliasing (i.e. remove
-fno-strict-aliasing) and see if any of the warnings give you a clue as
to the problem. You can try -Wstrict-aliasing=1 if the default did not
give any warnings, at the cost of potentially more false positives.
Brian
More information about the Gcc-help
mailing list