gcc 4.4.1 and 4.2.4 failed to generate the correct code under -O0; gcc 3.4.6
works fine.

---BEGIN code ---
#include <stdio.h>

int main( )
{
    int a, b;
    a = 1;
    b = 2;

    register int *pa = &a;
    register int *pb = &b;

    /* Swap a and b using pointers pa and pb */
    *pa ^= *pb ^= *pa ^= *pb;

    printf( "%d %d\n", a, b );
    return 0;
}
---END code ---

Symptom
----------------------------
The expected end result is a = 2 and b = 1, but the code generated by gcc 4.4.1
and 4.2.4 produces a = 0 and b = 1.


The Incorrect Assembly code
----------------------------
The relevant portion of the assembly code produced by 'gcc -S -O0' is

---BEGIN code ---
        movl    $1, -20(%rbp)
        movl    $2, -24(%rbp)
        leaq    -20(%rbp), %rbx
        .cfi_offset 3, -32
        .cfi_offset 12, -24
        leaq    -24(%rbp), %r12
        movl    (%rbx), %edx
        movl    (%r12), %ecx
        movl    (%rbx), %esi
        movl    (%r12), %eax
        xorl    %esi, %eax
        movl    %eax, (%rbx)
        movl    (%rbx), %eax
        xorl    %ecx, %eax
        movl    %eax, (%r12)
        movl    (%r12), %eax
        xorl    %edx, %eax
        movl    %eax, (%rbx)
---END code ---

The last xorl code is incorrect; the correct one should be "xorl (%rbx) %eax".
The problem with the incorrect code is that even though the value pointed to by
%rbx (or pa in the C code) has been changed, the above xorl code is still using
the old saved value of (%rbx), or *pa, instead of its most current value.

Below is the options given when gcc was built
-----------------------------------------------
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror
--with-arch-32=i486 --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu


-- 
           Summary: gcc failed to generate correct code under -O0
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jasonzou1983 at yahoo dot com
  GCC host triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43474

Reply via email to