public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-10463] Fix problematic interaction between bitfields, unions, SSO and SRA
Date: Wed, 11 Jan 2023 19:47:11 +0000 (GMT)	[thread overview]
Message-ID: <20230111194711.782143858C83@sourceware.org> (raw)

https://gcc.gnu.org/g:01e80c4c630a5a6286a521b047d0ef80631c892c

commit r11-10463-g01e80c4c630a5a6286a521b047d0ef80631c892c
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed Jan 11 15:58:47 2023 +0100

    Fix problematic interaction between bitfields, unions, SSO and SRA
    
    The handling of bitfields by the SRA pass is peculiar and this must be taken
    into account to support the scalar_storage_order attribute.
    
    gcc/
            PR tree-optimization/108199
            * tree-sra.c (sra_modify_expr): Deal with reverse storage order
            for bit-field references.
    
    gcc/testsuite/
            * gcc.dg/sso-17.c: New test.

Diff:
---
 gcc/testsuite/gcc.dg/sso-17.c | 52 +++++++++++++++++++++++++++++++++++++++++++
 gcc/tree-sra.c                | 18 ++++++++++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/sso-17.c b/gcc/testsuite/gcc.dg/sso-17.c
new file mode 100644
index 00000000000..67e2d3793fd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/sso-17.c
@@ -0,0 +1,52 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define REV_ENDIANNESS __attribute__((scalar_storage_order("big-endian")))
+#else
+#define REV_ENDIANNESS __attribute__((scalar_storage_order("little-endian")))
+#endif
+
+typedef unsigned long long u64;
+
+union DST {
+  u64 val;
+
+  struct {
+    u64 x : 1;
+    u64 y : 1;
+    u64 r: 62;
+  } REV_ENDIANNESS;
+} REV_ENDIANNESS;
+
+
+struct SRC {
+  u64 a;
+} REV_ENDIANNESS;
+
+[[gnu::noipa]]
+void foo () {__builtin_abort();}
+
+[[gnu::noinline]]
+int bar(struct SRC *src)
+{
+  union DST dst;
+  
+  dst.val = src->a;
+
+  if (dst.y) {
+    foo();
+  }
+  return 0;
+}
+
+int main(void)
+{
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+  struct SRC t = {-1ull & (~(0x01ull<<62))};
+#else
+  struct SRC t = {-1ull & (~(0x01ull<<1))};
+#endif
+  bar(&t);
+  return 0;
+}
diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c
index dea9cf3235c..5a1fc81d301 100644
--- a/gcc/tree-sra.c
+++ b/gcc/tree-sra.c
@@ -3828,7 +3828,23 @@ sra_modify_expr (tree *expr, gimple_stmt_iterator *gsi, bool write)
 	    }
 	}
       else
-	*expr = repl;
+	{
+	  /* If we are going to replace a scalar field in a structure with
+	     reverse storage order by a stand-alone scalar, we are going to
+	     effectively byte-swap the scalar and we also need to byte-swap
+	     the portion of it represented by the bit-field.  */
+	  if (bfr && REF_REVERSE_STORAGE_ORDER (bfr))
+	    {
+	      REF_REVERSE_STORAGE_ORDER (bfr) = 0;
+	      TREE_OPERAND (bfr, 2)
+		= size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (repl)),
+			      size_binop (PLUS_EXPR, TREE_OPERAND (bfr, 1),
+						     TREE_OPERAND (bfr, 2)));
+	    }
+
+	  *expr = repl;
+	}
+
       sra_stats.exprs++;
     }
   else if (write && access->grp_to_be_debug_replaced)

                 reply	other threads:[~2023-01-11 19:47 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=20230111194711.782143858C83@sourceware.org \
    --to=ebotcazou@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).