public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/7557: gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / pointer aliasing
@ 2002-08-09  9:26 Johannes Stezenbach
  0 siblings, 0 replies; 2+ messages in thread
From: Johannes Stezenbach @ 2002-08-09  9:26 UTC (permalink / raw)
  To: gcc-gnats, debian-gcc


>Number:         7557
>Category:       optimization
>Synopsis:       gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / pointer aliasing
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Fri Aug 09 09:16:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Johannes Stezenbach
>Release:        3.1.1 (Debian testing/unstable)
>Organization:
>Environment:
System: Linux hell 2.4.19pre1 #3 Sun May 12 19:15:17 CEST 2002 i686 unknown unknown GNU/Linux
Architecture: i686

	
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: /mnt/data/gcc-3.1/gcc-3.1-3.1.1ds3/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.1 --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --enable-clocale=gnu --enable-__cxa_atexit --enable-threads=posix --enable-java-gc=boehm --enable-objc-gc i386-linux
>Description:
The attached sample program produces wrong output when
compiled with -O2. Output produced when compiled with-O is
correct.
I'm attaching the C source as well as compressed preprcessor output.
 
$ gcc-3.1 -Wall -O2 bug.c -o bug
$ ./bug
8 8
0xbeaddeef 0x34127856
0xa307401c 0x12345678
 
$ gcc-3.1 -Wall -O bug.c -o bug
$ ./bug
8 8
0xbeaddeef 0x34127856
0xdeadbeef 0x12345678

$ gcc-3.1 -v -Wall -O2 bug.c -o bug
Reading specs from /usr/lib/gcc-lib/i386-linux/3.1.1/specs
Configured with: /mnt/data/gcc-3.1/gcc-3.1-3.1.1ds3/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.1 --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --enable-clocale=gnu --enable-__cxa_atexit --enable-threads=posix --enable-java-gc=boehm --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.1.1
 /usr/lib/gcc-lib/i386-linux/3.1.1/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=1 -D__GNUC_PATCHLEVEL__=1 -D__ELF__ -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__OPTIMIZE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ bug.c -quiet -dumpbase bug.c -O2 -Wall -version -o /home/js/tmp/ccA9puym.s
GNU CPP version 3.1.1 (cpplib) (i386 Linux/ELF)
GNU C version 3.1.1 (i386-linux)
	compiled by GNU C version 3.1.1.
ignoring nonexistent directory "/usr/i386-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc-lib/i386-linux/3.1.1/include
 /usr/include
End of search list.
 as -V -Qy -o /home/js/tmp/ccA3HBCF.o /home/js/tmp/ccA9puym.s
GNU assembler version 2.12.90.0.15 (i386-linux) using BFD version 2.12.90.0.15 20020717 Debian GNU/Linux
 /usr/lib/gcc-lib/i386-linux/3.1.1/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o bug /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crt1.o /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crti.o /usr/lib/gcc-lib/i386-linux/3.1.1/crtbegin.o -L/usr/lib/gcc-lib/i386-linux/3.1.1 -L/usr/lib/gcc-lib/i386-linux/3.1.1/../../.. /home/js/tmp/ccA3HBCF.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-linux/3.1.1/crtend.o /usr/lib/gcc-lib/i386-linux/3.1.1/../../../crtn.o



>How-To-Repeat:
/* gcc-3.1.1 bug: wrong results with -O2 */

#include <stdio.h>
#include <asm/byteorder.h>

struct foo {
	unsigned int a: 8,
                     b:24;
	unsigned int c:16,
                     d:16;
};
struct foo_swabbed {
	unsigned int b:24,
                     a: 8;
	unsigned int d:16,
                     c:16;
};

static void swab_foo(struct foo *f)
{
        struct foo_swabbed fs = *(struct foo_swabbed *) f;
        unsigned int *p = (unsigned int *)&fs;

        p[0] = __swab32(p[0]);
        p[1] = __swab32(p[1]);
        f->a = fs.a;
        f->b = fs.b;
        f->c = fs.c;
        f->d = fs.d;
}

int main(void)
{
	struct foo f = { 0xef, 0xbeadde,  0x7856, 0x3412 };
        unsigned int *p = (unsigned int *)&f;
	printf("%u %u\n", sizeof(struct foo), sizeof(struct foo_swabbed));
	printf("%#010x %#010x\n", p[0], p[1]);
	swab_foo(&f);
	printf("%#010x %#010x\n", p[0], p[1]);
	return 0;
}

>Fix:
	
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: optimization/7557: gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / pointer aliasing
@ 2002-08-09 13:46 sirl
  0 siblings, 0 replies; 2+ messages in thread
From: sirl @ 2002-08-09 13:46 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, js, nobody

Synopsis: gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / pointer aliasing

State-Changed-From-To: open->closed
State-Changed-By: sirl
State-Changed-When: Fri Aug  9 12:05:00 2002
State-Changed-Why:
    You have written undefined C code (hint: the compiler is
    free to assume that fs, *f and *p are diffrent objects),
    either fixup your code or compile with -fno-strict-aliasing.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7557


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-08-09 19:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-09  9:26 optimization/7557: gcc-3.1.1 (debian/i386): wrong code with -O2 / bitfields / pointer aliasing Johannes Stezenbach
2002-08-09 13:46 sirl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).