public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10622] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062]
Date: Tue, 10 May 2022 08:18:54 +0000 (GMT)	[thread overview]
Message-ID: <20220510081854.4BCC338346A8@sourceware.org> (raw)

https://gcc.gnu.org/g:0a278a8e2a95abe2bdad50ffd820b31363758a78

commit r10-10622-g0a278a8e2a95abe2bdad50ffd820b31363758a78
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jun 16 12:17:55 2021 +0200

    stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062]
    
    The following testcase is miscompiled on x86_64-linux, the bitfield store
    is implemented as a RMW 64-bit operation at d+24 when the d variable has
    size of only 28 bytes and scheduling moves in between the R and W part
    a store to a different variable that happens to be right after the d
    variable.
    
    The reason for this is that we weren't creating
    DECL_BIT_FIELD_REPRESENTATIVEs for bitfields in unions.
    
    The following patch does create them, but treats all such bitfields as if
    they were in a structure where the particular bitfield is the only field.
    
    2021-06-16  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/101062
            * stor-layout.c (finish_bitfield_representative): For fields in unions
            assume nextf is always NULL.
            (finish_bitfield_layout): Compute bit field representatives also in
            unions, but handle it as if each bitfield was the only field in the
            aggregate.
    
            * gcc.dg/pr101062.c: New test.
    
    (cherry picked from commit b4b50bf2864e09f028a39a3f460222632c4d7348)

Diff:
---
 gcc/stor-layout.c               | 26 +++++++++++++++-----------
 gcc/testsuite/gcc.dg/pr101062.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index 57c8a2516d9..e805e6912a7 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -2059,9 +2059,14 @@ finish_bitfield_representative (tree repr, tree field)
   bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
 
   /* Now nothing tells us how to pad out bitsize ...  */
-  nextf = DECL_CHAIN (field);
-  while (nextf && TREE_CODE (nextf) != FIELD_DECL)
-    nextf = DECL_CHAIN (nextf);
+  if (TREE_CODE (DECL_CONTEXT (field)) == RECORD_TYPE)
+    {
+      nextf = DECL_CHAIN (field);
+      while (nextf && TREE_CODE (nextf) != FIELD_DECL)
+	nextf = DECL_CHAIN (nextf);
+    }
+  else
+    nextf = NULL_TREE;
   if (nextf)
     {
       tree maxsize;
@@ -2154,13 +2159,6 @@ finish_bitfield_layout (tree t)
   tree field, prev;
   tree repr = NULL_TREE;
 
-  /* Unions would be special, for the ease of type-punning optimizations
-     we could use the underlying type as hint for the representative
-     if the bitfield would fit and the representative would not exceed
-     the union in size.  */
-  if (TREE_CODE (t) != RECORD_TYPE)
-    return;
-
   for (prev = NULL_TREE, field = TYPE_FIELDS (t);
        field; field = DECL_CHAIN (field))
     {
@@ -2220,7 +2218,13 @@ finish_bitfield_layout (tree t)
       if (repr)
 	DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
 
-      prev = field;
+      if (TREE_CODE (t) == RECORD_TYPE)
+	prev = field;
+      else if (repr)
+	{
+	  finish_bitfield_representative (repr, field);
+	  repr = NULL_TREE;
+	}
     }
 
   if (repr)
diff --git a/gcc/testsuite/gcc.dg/pr101062.c b/gcc/testsuite/gcc.dg/pr101062.c
new file mode 100644
index 00000000000..6c37ed88885
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101062.c
@@ -0,0 +1,29 @@
+/* PR middle-end/101062 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-toplevel-reorder -frename-registers" } */
+
+union U { signed b : 5; };
+int c;
+volatile union U d[7] = { { 8 } };
+short e = 1;
+
+__attribute__((noipa)) void
+foo ()
+{
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  e = 0;
+  c = 0;
+}
+
+int
+main ()
+{
+  foo ();
+  if (e != 0)
+    __builtin_abort ();
+  return 0;
+}


                 reply	other threads:[~2022-05-10  8:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220510081854.4BCC338346A8@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).