Bug 30724

Summary: Wrong code from -O2 taking ptr=(struct *)&int_proc_parm on x86_64
Product: gcc Reporter: Karl Schendel <schendel>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: major CC: cdfrey, chiabaut, gcc-bugs, gcc2eran, hans.buchmann.wantuch, lucifer_ww, sb, schendel, sorenj, takahisa.yokota
Priority: P3    
Version: 4.1.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Karl Schendel 2007-02-07 03:39:06 UTC
gcc -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.1.0 --enable-ssp --disable-libssp --enable-java-awt=gtk --enable-gtk-cairo --disable-libjava-multilib --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --without-system-libunwind --with-cpu=generic --host=x86_64-suse-linux
Thread model: posix
gcc version 4.1.0 (SUSE Linux)
------------
Compile the following with cc -O2, gets wrong code.
If you use -fno-strict-aliasing or -fno-schedule-insns2 it works.
cc -O1 works.  cc -O1 -fstrict-aliaseing -fschedule-insns2 fails.
I might have prioritized this critical except I found the ref to
-fno-strict-aliasing in the 4.1.2rc1 thread...

/* -O2 compiles the fetch of lx_id->id_id before it stores
** external_lx_id into the stack:

        movzwl  6(%rsp), %eax   // oops, too soon
        movl    %edi, 4(%rsp)
        testw   %ax, %ax  //  tests garbage

*/

typedef int LG_LXID;
typedef int i4;
typedef unsigned short u_i2;
typedef struct _LG_ID {
    u_i2 foo;
    u_i2 id_id;
} LG_ID;


void
LGend(
LG_LXID             external_lx_id,
i4                  flag)
{
    LG_ID               *lx_id = (LG_ID*)&external_lx_id;

    if (lx_id->id_id == 0 || (i4)lx_id->id_id > flag)
        printf("barfo");
}
Comment 1 Andrew Pinski 2007-02-07 06:51:53 UTC
If you use -fno-strict-aliasing and it works, that should tell you something right there really.

Anyways you are violating C/C++ aliasing rules, see PR 21920.

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