Bug 62103 - Incorrect folding of bitfield in a union on big endian targets
Summary: Incorrect folding of bitfield in a union on big endian targets
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.8.3
: P3 normal
Target Milestone: ---
Assignee: Thomas Preud'homme
URL:
Keywords: wrong-code
: 64822 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-08-12 02:14 UTC by Thomas Preud'homme
Modified: 2015-05-18 02:03 UTC (History)
0 users

See Also:
Host:
Target: bigendian
Build:
Known to work: 4.8.4, 4.9.2, 5.0
Known to fail: 4.8.3, 4.9.1
Last reconfirmed: 2014-08-12 00:00:00


Attachments
Testcase: bitfield in a union (235 bytes, text/plain)
2014-08-12 02:15 UTC, Thomas Preud'homme
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Preud'homme 2014-08-12 02:14:35 UTC
Folding of bitfield in a union is incorrect on big endian targets. Consider the testcase given in attachment, u.b would be folded to 0x45678 instead of 0x12345.
Comment 1 Thomas Preud'homme 2014-08-12 02:15:54 UTC
Created attachment 33298 [details]
Testcase: bitfield in a union
Comment 2 Thomas Preud'homme 2014-08-12 02:21:09 UTC
I forgot to mention the flag to use: -O1 and whatever flag is necessary to select a big endian target (for instance -mbig-endian if the target is arm little endian by default).
Comment 3 Thomas Preud'homme 2014-08-12 02:37:12 UTC
Author: thopre01
Date: Tue Aug 12 02:36:37 2014
New Revision: 213846

URL: https://gcc.gnu.org/viewcvs?rev=213846&root=gcc&view=rev
Log:
2014-08-12  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    gcc/
    PR middle-end/62103
    * gimple-fold.c (fold_ctor_reference): Don't fold in presence of
    bitfields, that is when size doesn't match the size of type or the
    size of the constructor.

    gcc/testsuite/
    PR middle-end/62103
    * gcc.c-torture/execute/bitfld-6.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-fold.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Thomas Preud'homme 2014-08-13 09:38:13 UTC
Author: thopre01
Date: Wed Aug 13 09:37:41 2014
New Revision: 213899

URL: https://gcc.gnu.org/viewcvs?rev=213899&root=gcc&view=rev
Log:
2014-08-13  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    Backport from mainline
    2014-08-12  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    gcc/
    PR middle-end/62103
    * gimple-fold.c (fold_ctor_reference): Don't fold in presence of
    bitfields, that is when size doesn't match the size of type or the
    size of the constructor.

    gcc/testsuite/
    PR middle-end/62103
    * gcc.c-torture/execute/bitfld-6.c: New test.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/gimple-fold.c
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
Comment 5 Thomas Preud'homme 2014-08-14 06:17:28 UTC
Author: thopre01
Date: Thu Aug 14 06:16:56 2014
New Revision: 213941

URL: https://gcc.gnu.org/viewcvs?rev=213941&root=gcc&view=rev
Log:
2014-08-14  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    Backport from mainline
    2014-08-12  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    gcc/
    PR middle-end/62103
    * gimple-fold.c (fold_ctor_reference): Don't fold in presence of
    bitfields, that is when size doesn't match the size of type or the
    size of the constructor.

    gcc/testsuite/
    PR middle-end/62103
    * gcc.c-torture/execute/bitfld-6.c: New test.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/gimple-fold.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
Comment 6 Thomas Preud'homme 2014-12-17 11:52:27 UTC
Fixed in all the branch currently supported
Comment 7 Jiong Wang 2015-01-27 22:31:24 UTC
*** Bug 64822 has been marked as a duplicate of this bug. ***
Comment 8 Jiong Wang 2015-01-27 22:32:08 UTC
looks like this fix is too conservative. it will disable const fold for bit-field completely. for bitfld-6/little-endian, previously, we can generated

main:
        mov     w0, 0
        ret

while after this patch, we always load from memory. any specific reason for this?

shouldn't we fix the code to let big-endian get correct folded constant?
Comment 9 Jiong Wang 2015-01-27 22:34:35 UTC
(In reply to Jiong Wang from comment #8)
> 
> while after this patch, we always load from memory. any specific reason for
> this?
> 
> shouldn't we fix the code to let big-endian get correct folded constant?

and similar bug still exist as 64822
Comment 10 Thomas Preud'homme 2015-01-28 02:58:33 UTC
(In reply to Jiong Wang from comment #8)
> looks like this fix is too conservative. it will disable const fold for
> bit-field completely. for bitfld-6/little-endian, previously, we can
> generated
> 
> main:
>         mov     w0, 0
>         ret
> 
> while after this patch, we always load from memory. any specific reason for
> this?
> 
> shouldn't we fix the code to let big-endian get correct folded constant?

We only disable const folding for initialization of bitfields in a union. The rationale was that the fix was much simpler (and could thus be backported) and accessing a bitfield in a union that was initialized with a constant rare enough that it wasn't worth the extra effort anyway. If this latter assumption proves to be wrong I have the beginning of a patch that make double_int::from_buffer and wi::from_buffer work at the bit level. It would need to be extended to do the same to ::to_buffer.
Comment 11 Thomas Preud'homme 2015-01-28 09:30:11 UTC
Ok, I have a local fix. The existing testcase didn't catch it because the precision of the bitfield is not a multiple of CHAR_BITS.
Comment 12 Thomas Preud'homme 2015-02-04 08:23:16 UTC
Author: thopre01
Date: Wed Feb  4 08:22:45 2015
New Revision: 220390

URL: https://gcc.gnu.org/viewcvs?rev=220390&root=gcc&view=rev
Log:
2015-02-04  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    gcc/
    PR middle-end/62103
    * tree-ssa-sccvn.c (fully_constant_vn_reference_p): Use TYPE_PRECISION
    to compute size of referenced value in the constant case.

    gcc/testsuite/
    PR middle-end/62103
    * gcc.c-torture/execute/bitfld-7.c: New test adapted from bitfld-6.c
    to use 24 bits for bitfield b.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/bitfld-7.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-sccvn.c
Comment 13 Thomas Preud'homme 2015-05-18 02:03:14 UTC
Fixed in all maintained branches.