This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is it OK that gcc optimizes away overflow check?
On 26/07/11 10:38, Agner Fog wrote:
> We need to be constructive here. We want the optimizations, but we
> also want to check if the optimizations go wrong. I think the
> compiler should be able to generate warnings for every unsafe
> optimization,
Continuing to describe these as "unsafe optimizations" is tendentious
at best, and doesn't help the discussion to continue in a polite way.
> especially when removing a branch. The compiler generates a warning
> when optimizing away if (a+5 < a) but not when optimizing away if
> (abs(a)<0).
I think you'd be surprised at just how many warnings would be
generated.
Consider
for (i = 0; i < limit; i++)
f(i * 2);
which can be rewritten to
tmp = limit * 2;
for (i = 0; i < tmp; i++)
f(i);
but only if you know integer overflow in i*2 can't occur.
This kind of thing happens all the time, in many programs. It isn't
usually a problem because the programmer "just knows" that i*2 is less
than INT_MAX.
Andrew.