c/6609: gcc 3.0.3 (and other versions) generate bogus code with -O2
solomon@conceptshopping.com
solomon@conceptshopping.com
Wed May 8 14:56:00 GMT 2002
>Number: 6609
>Category: c
>Synopsis: gcc 3.0.3 (and other versions) generate bogus code with -O2
>Confidential: no
>Severity: critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Wed May 08 14:56:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Marvin Solomon
>Release: 3.0.3
>Organization:
Concept Shopping, Inc. and University of Wisconsin
>Environment:
System: SunOS lisle 5.7 Generic_106541-08 sun4u sparc SUNW,Ultra-5_10
Architecture: sun4
host: sparc-sun-solaris2.7
build: sparc-sun-solaris2.7
target: sparc-sun-solaris2.7
configured with: ../configure
Also seen with other versions and platforms (see below).
>Description:
Compile the following program with "gcc -O2" and run it.
#include <stdio.h>
void bug(long long upc) {
unsigned long *p = (unsigned long *)(&upc);
printf("%08lx %08lx\n", p[0], p[1]);
printf("%08lx %08lx\n", p[0], p[1]);
}
int main() {
long long u = 0x0123456789abcdefll;
bug(u);
return 0;
}
The result on the indicated platform is
00000000 89abcdef
01234567 89abcdef
Note that the two printfs print different things, and the first one is wrong.
It prints the correct results without out optimication, and with -O1 and
(curiously) -O3.
I've tried this on a variety of platforms with a variety of gcc releases,
including
Linux solomon-csi 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
Linux gjetost.cs.wisc.edu 2.4.18-csl2smp #1 SMP Thu Apr 11 09:20:41 CDT
2002 i686 unknown
Configured with: /s/gcc-3.0.3/src/gcc-3.0.3/configure
--prefix=/s/gcc-3.0.3/i386_rh72 --enable-shared --enable-threads
Thread model: posix
gcc version 3.0.3
In all cases, -O2 gives incorrect (and different) results for the first
printf and correct results for the second printf. Usually -O3 also
generates incorrect code. In fact the only version I've found that compiles
this code correctly is
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
>How-To-Repeat:
See above.
>Fix:
No known fix or work-around.
>Release-Note:
>Audit-Trail:
>Unformatted:
Also seen with other versions and platforms (see below).
More information about the Gcc-bugs
mailing list