This is the mail archive of the
mailing list for the GCC project.
Re: Is it OK that gcc optimizes away overflow check?
- From: Andrew Haley <aph at redhat dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 25 Jul 2011 10:43:05 +0100
- Subject: Re: Is it OK that gcc optimizes away overflow check?
- References: <4E2B2B72.email@example.com>
On 23/07/11 21:13, Agner Fog wrote:
> I have a program where I check for integer overflow. The program failed,
> and I found that gcc has optimized away the overflow check. I filed a
> bug report and got the answer:
>> Integer overflow is undefined. You have to check before the fact, or compile
>>> with -fwrapv.
> ( http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49820 )
> 5). I have tested many different C++ compilers, and gcc turned out to be
> the one that optimizes best.
Well yes, and one of the reasons for this is that we take advantage of
integer overflow being undefined. There is an entire class of
optimizations (loop induction variable optimization) that is difficult
of impossible without taking advantage of this. We don't do this kind
of thing without good reason.
We can't promise you that in every case gcc will do what you would
like it to do, However, we have thought long and hard about it.