User account creation filtered due to spam.

Bug 37214 - Weird operation reordering when compiling with -O3/O2 leading to erroneous result
Summary: Weird operation reordering when compiling with -O3/O2 leading to erroneous re...
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.3
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2008-08-23 21:46 UTC by Cuero Bugot
Modified: 2008-08-24 01:03 UTC (History)
26 users (show)

See Also:
Target: x86_64-linux-gnu
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 Cuero Bugot 2008-08-23 21:46:59 UTC
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)
	*(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:
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)
 /usr/lib/gcc/x86_64-linux-gnu/4.2.3/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ -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
Comment 1 Andreas Schwab 2008-08-23 23:07:15 UTC
You are violating the C/C++ aliasing rules.

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