Bug 33205

Summary: optimization bug: unsigned bitfield expands with non-zero padding bits
Product: gcc Reporter: Ivanov Pavel <xk-corpse>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: aoliva, aph, bergner, fang, gcc-bugs, ismail, janis, jason, mark, pinskia, rguenth, tromey, xk-corpse
Priority: P3 Keywords: wrong-code
Version: 4.2.1   
Target Milestone: ---   
Host: i686-pc-linux-gnu Target: powerpc-603-linux-gnu
Build: i686-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed:

Description Ivanov Pavel 2007-08-27 16:02:56 UTC
test.cxx
------------------------------------------------------
#include <iostream>

struct A
{
    unsigned short x:14;
    unsigned short y:14;
};


extern unsigned n;

int main()
{
    A a;

    a.x = n & 0x3FFF;
    a.y = (n>>14) & 0x3FFF;

    unsigned expected = n & 0x3FFF;

    if( a.x != expected )
    {
        std::cout << "test failed: "
            << " (x=" << a.x
            << ") != " << expected
            << ", y=" << a.y
            << std::endl;
        return 1;
    }

    std::cout << "test succedeed" << std::endl;

    return 0;
}

unsigned n = 0xFE7B4873U;
------------------------------------------------------

output:
test failed:  (x=18547) != 2163, y=14829

14-bit unsigned field expands with non-zero padding bits


appears with gcc-4.2.1, gcc-4.1.2, gcc-4.2-20070822
does not appear with gcc-3.4.6

gcc was configured and built with crosstool-0.43 (with glibc-2.3.2)

powerpc-603-linux-gnu-g++ -v
Using built-in specs.
Target: powerpc-603-linux-gnu
Configured with: /home/corpse/tmp/interlink/crosstool-0.43/build/powerpc-603-linux-gnu/gcc-4.2.1-glibc-2.3.2/gcc-4.2.1/configure --target=powerpc-603-linux-gnu --host=i686-host_pc-linux-gnu --prefix=/opt/compilers/gcc-4.2.1-glibc-2.3.2/powerpc-603-linux-gnu --with-cpu=603 --enable-cxx-flags=-mcpu=603 --with-headers=/opt/compilers/gcc-4.2.1-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include --with-local-prefix=/opt/compilers/gcc-4.2.1-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-__cxa_atexit --enable-languages=c,c++ --enable-shared --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.2.1
Comment 1 Ivanov Pavel 2007-08-28 08:45:45 UTC
i'm sorry, i forgot to say that bug appears with any optimization level enabled (-O1, -O2, -O3) and does not appear with optimization disabled (-O0)
Comment 2 Richard Biener 2007-08-28 09:39:07 UTC
This works for me on a x86_64 target.
Comment 3 Ivanov Pavel 2007-08-28 11:25:06 UTC
(In reply to comment #2)
> This works for me on a x86_64 target.
> 

For me it works on i686-pc-linux (with cross-compiler, configured with the same method as above - crosstool-0.43 + glibc-2.3.2), works on i386-mingw32 (with cross-compiler linux->mingw32), but does not work on powerpc.
Comment 4 Richard Biener 2008-02-04 16:49:56 UTC
Still works for me on x86_64/i686.  Any ppc guys around to confirm this?
Comment 5 Peter Bergner 2008-02-05 16:45:47 UTC
This works for me using latest mainline, but using a compiler built with revision 131553, it fails.  I'll try and see if we're just getting lucky now or whether it has been fixed since then.

Janis, in the meantime, can you run a reg hunt on the attached testcase starting at revision 131553?
Comment 6 Janis Johnson 2008-02-07 17:32:23 UTC
A regression hunt on powerpc-linux showed that the test starts passing with:

    http://gcc.gnu.org/viewcvs?view=rev&rev=131823
    r131823 | rguenth | 2008-01-25 12:06:31 +0000 (Fri, 25 Jan 2008)

That's a fix for 33887.
Comment 7 Richard Biener 2008-02-07 18:03:27 UTC
Ok, that's quite likely.

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