https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67090
Bug ID: 67090 Summary: [5/6 Regression] O2/O3 optimizes values away when different type pointers are typecasted Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: davispuh at gmail dot com Target Milestone: --- GCC 5.2.0 and also latest trunk 6.0 (compiled from git commit 9b6b471, svn r226241) using O2 or O3 generates invalid code by optimizing value away when it's pointer is casted to different type pointer. Here's a testcase, basically cast (double *) to (long *) and (long *) to (double *) #include <stdio.h> int main(int argc, char **argv) { long l = 0xAA00133700BB00EE; double d = (double)-2.190302448144169e-106; volatile int i = 0; // this to prevent optimization, could be also argc-1 printf("%16lX\n%.15e\n", ((long*)&d)[i], ((double*)&l)[i]); return 0; } Compiling it with gcc -O1 or with clang -O3 produces correct output: AA00133700BB00EE -2.190302448144169e-106 But when compiling with gcc -O2 (or -O3) produces incorrect output 0 6.952967796700164e-310 When compiled correctly with -O1 0x004004fa 48b8ee00bb00. movabs rax, -0x55ffecc8ff44ff12 0x00400504 4889442418 mov qword [rsp + 0x18], rax 0x00400509 f20f100dd700. movsd xmm1, qword [rip + 0xd7] ; [0x4005e8:8]=0xaa00133700bb00ee 0x00400511 f20f114c2410 movsd qword [rsp + 0x10], xmm1 0x00400517 c744240c0000. mov dword [rsp + 0xc], 0 0x0040051f 8b54240c mov edx, dword [rsp + 0xc] ; [0xc:4]=-1 ; 12 0x00400523 8b44240c mov eax, dword [rsp + 0xc] ; [0xc:4]=-1 ; 12 0x00400527 4863d2 movsxd rdx, edx 0x0040052a f20f1044d418 movsd xmm0, qword [rsp + rdx*8 + 0x18] ; [0x18:8]=-1 ; 24 0x00400530 4898 cdqe 0x00400532 488b74c410 mov rsi, qword [rsp + rax*8 + 0x10] ; [0x10:8]=-1 ; 16 0x00400537 bfd4054000 mov edi, str._16lX_n_.15e_n ; "%16lX.%.15e." @ 0x4005d4 0x0040053c b801000000 mov eax, 1 0x00400541 e89afeffff call sym.imp.printf ;0x004003e0() ; sym.imp.printf 0x00400546 b800000000 mov eax, 0 0x0040054b 4883c428 add rsp, 0x28 0x0040054f c3 ret memory at rsp (0x7ffea6ee18c0) - offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x7ffea6ee18c0 5005 4000 0000 0000 1004 4000 0000 0000 P.@.......@..... 0x7ffea6ee18d0 ee00 bb00 3713 00aa ee00 bb00 3713 00aa ....7.......7... // values here 0x7ffea6ee18e0 0000 0000 0000 0000 9037 5a02 437f 0000 .........7Z.C... 0x7ffea6ee18f0 c819 eea6 fe7f 0000 c819 eea6 fe7f 0000 ................ When compiled incorrectly with -O2 0x00400414 bfc4054000 mov edi, str._16lX_n_.15e_n ; "%16lX.%.15e." @ 0x4005c4 0x00400419 c744240c0000. mov dword [rsp + 0xc], 0 0x00400421 486354240c movsxd rdx, dword [rsp + 0xc] ; [0xc:4]=-1 ; 12 0x00400426 486344240c movsxd rax, dword [rsp + 0xc] ; [0xc:4]=-1 ; 12 0x0040042b f20f1044d410 movsd xmm0, qword [rsp + rdx*8 + 0x10] ; [0x10:8]=-1 ; 16 0x00400431 488b74c418 mov rsi, qword [rsp + rax*8 + 0x18] ; [0x18:8]=-1 ; 24 0x00400436 b801000000 mov eax, 1 0x0040043b e8a0ffffff call sym.imp.printf ;0x004003e0() ; sym.imp.printf 0x00400440 31c0 xor eax, eax 0x00400442 4883c428 add rsp, 0x28 0x00400446 c3 ret memory at rsp (0x7ffd8ac419d0) - offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF 0x7ffd8ac419d0 4005 4000 0000 0000 5004 4000 0000 0000 @.@.....P.@..... 0x7ffd8ac419e0 d01a c48a fd7f 0000 0000 0000 0000 0000 ................ // values aren't here 0x7ffd8ac419f0 0000 0000 0000 0000 90b7 be39 047f 0000 ...........9.... 0x7ffd8ac41a00 d81a c48a fd7f 0000 d81a c48a fd7f 0000 ................ Running x86_64 Arch Linux, gcc -v COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/6.0.140195.9b6b471/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /mnt/AUR/gcc-multilib-trunk-git/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-multilib --disable-werror --enable-checking=release : (reconfigured) /mnt/AUR/gcc-multilib-trunk-git/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-multilib --disable-werror --enable-checking=release Thread model: posix gcc version 6.0.140195.9b6b471 20150727 (experimental) (GCC)