Is it OK that gcc optimizes away overflow check?

Agner Fog agner@agner.org
Tue Jul 26 16:24:00 GMT 2011


On 26-07-2011 16:43, Ian Lance Taylor wrote:
> int f (int i) { return abs (i) < 0; } 

> with -O2 -Wstrict-overflow I see this warning: foo.c:2: warning: 
> assuming signed overflow does not occur when simplifying comparison of 
> absolute value and zero So I think the compiler does warn about 
> optimizing that comparison away. 
You are right. I used -Wall. Unfortunately, -Wall sets 
-Wstrict-overflow=1 and you need |-Wstrict-overflow=2 to catch the abs 
example. Too bad that you can't even rely on -Wall to catch such a 
serious problem. I don't see why a+1<a = false should have a different 
warning level from abs(a)<0 = false.

I think that there is a big difference between optimizing a loop with an 
induction variable, as you mention, and optimizing away a branch. In a 
simple for-loop with i++, it is unlikely that the programmer intended 
any wrap-around. But if there is a branch or loop that can be optimized 
away completely, then it is either violating the programmer's intentions 
or the programmer has made a logical error. A warning would be in place 
in either case. In other words, there is a difference between (1) 
"ignoring overflow allows us to optimize an arithmetic expression or an 
induction variable", and (2) "ignoring overflow allows us to optimize 
away a branch". The latter situation should be regarded as more serious 
and therefore give a warning at a lower warning-level. Then we would be 
more likely to catch the situation where an intended overflow check is 
optimized away. I wonder if it is possible to make such a distinction in 
the compiler?

|



More information about the Gcc-help mailing list