GCC Optimizer Bug

Lawrence Chen, P.Eng. lchen@opentext.com
Mon Aug 2 14:05:00 GMT 1999


If I compile 
a piece of code in the form to test that some bits are set and/or some other 
bits are set or not set...the optimizer will set the test to a constant result 
either logical one or zero and emit the warning:
 
file.c:####: 
warning: comparison is always one
file.c:####: 
warning: comparison is always zero
 
But, its 
not.
 
The test is 
always of the form:
 
if 
((some_int & some_mask) == 0 && (some_int & (-2147483647-1)) == 
0)
 
The 
'&&' could be an '||' and the '=='s could be '!=', but it seems to 
always be if test an int for certain bits and test it again for the highest 
bit.  The optimizer turns the the comparison into a 
constant.
 
The above 
yields "comparison is always zero"
 
If the test 
is:
 
if 
((some_int & some_mask) != 0 || (some_int & (-2147483647-1)) != 
0)
 
yields 
"comparison is always one"
 
It also only 
appears to happens if the two comparisons of the same value are next to each 
other; if I reorder the comparisons so that there is one inbetween using a 
different value (such as a different position in an array or a totally different 
variable), then there is no compiler/optimizer warning.
 
i.e.:
 
if ( 
(some_int & some_mask) != 0 || (some_int & (-2147483647-1)) != 0 || 
other_int != 0)
 
generates 
the warning, but:
 
if ( 
(some_int & some_mask) != 0 || other_int != 0 || (some_int & 
(-2147483647-1)) != 0)
 
doesn't 
generate the warning.   --   Name: Lawrence Chen, 
P.Eng.             
Email: LChen@OpenText.com   Addr: Open Text, BASIS 
Division         Phone: 
(614)761-7449   Fax: 761-7269         5080 
Tuttle Crossing Blvd.          ICQ: 
12129673         Dublin, OH  
43016                   
URL: http://www.OpenText.com/basis 
 



More information about the Gcc-bugs mailing list