https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107890

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is always a trade off here.
We made the decision that signed integer overflow is undefined because we want
to do optimizations. Gcc does provide an option which makes them behave well
defined at runtime, -fwrapv . This is similar to strict aliasing with respect
to optimizations in the sense it is hard to decide if the optimizations is
being done will break what people assumptions are. This is part of the reason
why there is a specifications (standard) so people can write to it.
There are other undefined behavior that gcc has started to take advantage of
(e.g. in c++ if there is no return with a value in a function with that returns
non-void). The question is where do you draw the line with respect to undefined
behaviors, the answer is complex sometimes, especially if you are optimizing.

In this example the range of x is known to be positive so multiplying by
another positive # gives a positive result and then dividing by a positive
value still is positive. The check for negative result is optimized away.

Reply via email to