This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c/6609: gcc 3.0.3 (and other versions) generate bogus code with -O2

>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
>Originator:     Marvin Solomon
>Release:        3.0.3
Concept Shopping, Inc. and University of Wisconsin
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).
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;
        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,
    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 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)

	See above.
	No known fix or work-around.
 Also seen with other versions and platforms (see below).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]