This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: binary compiled with -O1 and w/ individual optimization flags are not the same
- From: Brian Dessent <brian at dessent dot net>
- To: CSights <csights at fastmail dot fm>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 29 Feb 2008 09:40:09 -0800
- Subject: Re: binary compiled with -O1 and w/ individual optimization flags are not the same
- References: <200802291215.47293.csights@fastmail.fm>
- Reply-to: gcc-help at gcc dot gnu dot org
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