2013-12-02 Bernd Edlinger <bernd.edlinger@hotmail.de>
authoredlinger <edlinger@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 2 Dec 2013 19:50:55 +0000 (19:50 +0000)
committeredlinger <edlinger@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 2 Dec 2013 19:50:55 +0000 (19:50 +0000)
        Fix C++0x memory model for unaligned fields in packed, aligned(4)
        structures with -fno-strict-volatile-bitfields on STRICT_ALIGNMENT
        targets like arm-none-eabi.
        * expr.c (expand_assignment): Handle normal fields like bit regions.

testsuite:
2013-12-02  Bernd Edlinger  <bernd.edlinger@hotmail.de>

        * gcc.dg/pr56997-4.c: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@205597 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr56997-4.c [new file with mode: 0644]

index 84153b0..21b8aa3 100644 (file)
@@ -1,5 +1,12 @@
 2013-12-02  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
+       Fix C++0x memory model for unaligned fields in packed, aligned(4)
+       structures with -fno-strict-volatile-bitfields on STRICT_ALIGNMENT
+       targets like arm-none-eabi.
+       * expr.c (expand_assignment): Handle normal fields like bit regions.
+
+2013-12-02  Bernd Edlinger  <bernd.edlinger@hotmail.de>
+
        PR target/58115
        * function.c (invoke_set_current_function_hook): Call
        targetm.set_current_function after setting this_fn_optabs.
index aeff2ca..c0539da 100644 (file)
@@ -4824,6 +4824,17 @@ expand_assignment (tree to, tree from, bool nontemporal)
       if (TREE_CODE (to) == COMPONENT_REF
          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
        get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
+      /* The C++ memory model naturally applies to byte-aligned fields.
+        However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
+        BITSIZE are not byte-aligned, there is no need to limit the range
+        we can access.  This can occur with packed structures in Ada.  */
+      else if (bitsize > 0
+              && bitsize % BITS_PER_UNIT == 0
+              && bitpos % BITS_PER_UNIT == 0)
+       {
+         bitregion_start = bitpos;
+         bitregion_end = bitpos + bitsize - 1;
+       }
 
       to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
 
index 9c7be13..f92967a 100644 (file)
@@ -1,3 +1,7 @@
+2013-12-02  Bernd Edlinger  <bernd.edlinger@hotmail.de>
+
+       * gcc.dg/pr56997-4.c: New testcase.
+
 2013-12-02  Marek Polacek  <polacek@redhat.com>
 
        * c-c++-common/ubsan/vla-1.c: Split the tests into individual
diff --git a/gcc/testsuite/gcc.dg/pr56997-4.c b/gcc/testsuite/gcc.dg/pr56997-4.c
new file mode 100644 (file)
index 0000000..38f6248
--- /dev/null
@@ -0,0 +1,23 @@
+/* Test volatile access to unaligned field.  */
+/* { dg-do compile } */
+/* { dg-options "-fno-strict-volatile-bitfields -fdump-rtl-final" } */
+
+#define test_type unsigned short
+
+typedef struct s{
+ unsigned char Prefix[1];
+ volatile test_type Type;
+}__attribute((__packed__,__aligned__(4))) ss;
+
+extern volatile ss v;
+
+void
+foo (test_type u)
+{
+  v.Type = u;
+}
+
+/* The C++ memory model forbids data store race conditions outside the
+   unaligned data member, therefore only QI or HI access is allowed, no SI.  */
+/* { dg-final { scan-rtl-dump-not "mem/v(/.)*:SI" "final" } } */
+/* { dg-final { cleanup-rtl-dump "final" } } */