This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/12243] New: loop optimization incorrectly treats expression as constant
- From: "selinger at mathstat dot uottawa dot ca" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 11 Sep 2003 01:50:20 -0000
- Subject: [Bug c/12243] New: loop optimization incorrectly treats expression as constant
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12243
Summary: loop optimization incorrectly treats expression as
constant
Product: gcc
Version: 3.3.1
Status: UNCONFIRMED
Severity: critical
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: selinger at mathstat dot uottawa dot ca
CC: gcc-bugs at gcc dot gnu dot org,selinger at mathstat dot
uottawa dot ca
GCC host triplet: any
The code below should terminate in the second loop iteration. When compiled with
-O2 or -O3 on certain architectures (see below), it enters an infinite loop
instead. Inspection of the generated assembly code reveals that the expression
marked (***) is wrongly treated as a constant expression and moved out of the loop.
Note that this is not a "floating point bug" in the usual sense: all floating
point numbers in this example are in fact small integers, and round-off errors
do not occur. Nevertheless, this particular bug seems to occur only with
floating point numbers.
Compiler versions where I have reproduced the bug:
gcc version 3.3.1 (host: linux-i386, --target=sparc-sun-solaris2)
gcc version 2.96 20000731 (Red Hat Linux 7.2 2.96-112.7.1)
gcc version 2.95.4 20011002 (Debian prerelease)
I assume that this is an architecture independent bug which has been present
since at least 2.95.4; however, this particular sample code seems to reproduce
the bug only on certain architecture/compiler combinations. For example, I could
not reproduce it on gcc 3.3.1/i386 or gcc 2.95.4/sparc.
How to reproduce: gcc -O3 sample.c
Code: (no #include's are necessary)
int z=0;
int main() {
double Q[2][2];
double det;
int i, j;
double x;
/* to the zero 2x2-matrix, keep adding the identity matrix until the
determinant is non-zero */
if (z == 0) {
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
Q[i][j] = 0;
}
}
}
while(1) {
det = Q[0][0]*Q[1][1] - Q[0][1]*Q[1][0]; /* (***) */
if (det != 0.0) {
x = 0;
break;
}
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
Q[i][j] += (i==j ? 1 : 0);
}
}
}
return 0;
}