Bug 6609 - gcc 3.0.3 (and other versions) generate bogus code with -O2
Summary: gcc 3.0.3 (and other versions) generate bogus code with -O2
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.0.3
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: wrong-code
Depends on:
Reported: 2002-05-08 14:56 UTC by solomon
Modified: 2005-06-05 09:46 UTC (History)
2 users (show)

See Also:
Host: sparc-sun-solaris2.7
Target: sparc-sun-solaris2.7
Build: sparc-sun-solaris2.7
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description solomon 2002-05-08 14:56:01 UTC
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 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)


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).

	See above.

 Also seen with other versions and platforms (see below).
Comment 1 solomon 2002-05-08 14:56:01 UTC
	No known fix or work-around.
Comment 2 Richard Henderson 2002-05-08 15:09:33 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug.  This violates ICO C aliasing rules.
    Use a union instead.
Comment 3 Andrew Pinski 2005-06-05 09:46:04 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-06-05 09:46:30 UTC
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***