2 instruction are inverted in the compiled code so the result is wrong.
The compilation is ok when using no optimization, but the problem happen with
-O3 and -O2.


Here is the function to compile:

void test2(uint32_t* source, uint32_t* dest)
{
        *dest=*source;
        *(uint16_t*)dest = (*(uint16_t*)dest)<<1;

}

You could guess that the shift should happen after the copy.

Here is what you get when dissassembling:
0000000000400500 <test2>:
  400500:       66 d1 26                shlw   (%rsi)
  400503:       8b 07                   mov    (%rdi),%eax
  400505:       89 06                   mov    %eax,(%rsi)
  400507:       c3                      retq
  400508:       0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
  40050f:       00



Here is the information on my configuration:

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
 /usr/lib/gcc/x86_64-linux-gnu/4.2.3/cc1 -E -quiet -v test.c -mtune=generic
-Wall -O2 -fpch-preprocess -o test.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.2.3/include
 /usr/include
End of search list.
 /usr/lib/gcc/x86_64-linux-gnu/4.2.3/cc1 -fpreprocessed test.i -quiet -dumpbase
test.c -mtune=generic -auxbase test -O2 -Wall -version -fstack-protector
-fstack-protector -o test.s
GNU C version 4.2.3 (Ubuntu 4.2.3-2ubuntu7) (x86_64-linux-gnu)
        compiled by GNU C version 4.2.3 (Ubuntu 4.2.3-2ubuntu7).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128576
Compiler executable checksum: 04286fa30e0b1c736cc2bb914c15518c
test.c: In function &#8216;main&#8217;:
test.c:67: warning: implicit declaration of function &#8216;srand&#8217;
 as --traditional-format -V -Qy -o test.o test.s
GNU assembler version 2.18.0 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.18.0.20080103
 /usr/lib/gcc/x86_64-linux-gnu/4.2.3/collect2 --eh-frame-hdr -m elf_x86_64
--hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test
/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.2.3/crtbegin.o
-L/usr/lib/gcc/x86_64-linux-gnu/4.2.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.2.3
-L/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../lib -L/lib/../lib
-L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../.. test.o -lgcc
--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/x86_64-linux-gnu/4.2.3/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../lib/crtn.o


-- 
           Summary: Weird operation reordering when compiling with -O3/O2
                    leading to errornous result
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cuerob at free dot fr
GCC target triplet: x86_64-linux-gnu


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

Reply via email to