[Bug c/67090] New: [5/6 Regression] O2/O3 optimizes values away when different type pointers are typecasted

2015-08-01 Thread davispuh at gmail dot com
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 
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

   0x004004fa48b8ee00bb00.  movabs rax, -0x55ffecc8ff44ff12
   0x004005044889442418 mov qword [rsp + 0x18], rax
   0x00400509f20f100dd700.  movsd xmm1, qword [rip + 0xd7] ;
[0x4005e8:8]=0xaa00133700bb00ee 
   0x00400511f20f114c2410   movsd qword [rsp + 0x10], xmm1
   0x00400517c744240c.  mov dword [rsp + 0xc], 0
   0x0040051f8b54240c   mov edx, dword [rsp + 0xc] ;
[0xc:4]=-1 ; 12
   0x004005238b44240c   mov eax, dword [rsp + 0xc] ;
[0xc:4]=-1 ; 12
   0x004005274863d2 movsxd rdx, edx
   0x0040052af20f1044d418   movsd xmm0, qword [rsp + rdx*8 + 0x18]
; [0x18:8]=-1 ; 24
   0x004005304898   cdqe
   0x00400532488b74c410 mov rsi, qword [rsp + rax*8 + 0x10] ;
[0x10:8]=-1 ; 16
   0x00400537bfd4054000 mov edi, str._16lX_n_.15e_n;
"%16lX.%.15e." @ 0x4005d4
   0x0040053cb80100 mov eax, 1
   0x00400541e89afe call sym.imp.printf   
;0x004003e0() ; sym.imp.printf
   0x00400546b8 mov eax, 0
   0x0040054b4883c428   add rsp, 0x28
   0x0040054fc3 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   1004 4000    P.@...@.
0x7ffea6ee18d0  ee00 bb00 3713 00aa ee00 bb00 3713 00aa  7...7... //
values here
0x7ffea6ee18e0      9037 5a02 437f   .7Z.C...
0x7ffea6ee18f0  c819 eea6 fe7f  c819 eea6 fe7f   


When compiled incorrectly with -O2

   0x00400414bfc4054000 mov edi, str._16lX_n_.15e_n;
"%16lX.%.15e." @ 0x4005c4
   0x00400419c744240c.  mov dword [rsp + 0xc], 0
   0x00400421486354240c movsxd rdx, dword [rsp + 0xc]  ;
[0xc:4]=-1 ; 12
   0x00400426486344240c movsxd rax, dword [rsp + 0xc]  ;
[0xc:4]=-1 ; 12
   0x0040042bf20f1044d410   movsd xmm0, qword [rsp + rdx*8 + 0x10]
; [0x10:8]=-1 ; 16
   0x00400431488b74c418 mov rsi, qword [rsp + rax*8 + 0x18] ;
[0x18:8]=-1 ; 24
   0x00400436b80100 mov eax, 1
   0x0040043be8a0ff call sym.imp.printf   
;0x004003e0() ; sym.imp.printf
   0x0040044031c0   xor eax, eax
   0x004004424883c428   add rsp, 0x28
   0x00400446c3 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   5004 4000    @.@.P.@.
0x7ffd8ac419e0  d01a c48a fd7f        //
values aren't here
0x7ffd8ac419f0      90b7 be39 047f   ...9
0x7ffd8ac41a00  d81a c48a fd7f  d81a c48a fd7f   



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-cl

[Bug c/67090] [5/6 Regression] O2/O3 optimizes values away when different type pointers are typecasted

2015-08-01 Thread davispuh at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67090

--- Comment #1 from Dāvis  ---
Created attachment 36105
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36105&action=edit
Testcase

[Bug c/67090] [5/6 Regression] O2/O3 optimizes values away when different type pointers are typecasted

2015-08-01 Thread davispuh at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67090

--- Comment #3 from Dāvis  ---
(In reply to Andrew Pinski from comment #2)
> This is such an obvious violation of aliasing rules in c/c++. 
> Use memcpy or an Union (gcc extension) to get around the it. You can use
> -fno-strict-aliasing too.

Then warning (or error) would be more appropriate than silently generating
invalid code as currently there's no warning even with -Wall