expr.c (expand_assignment): Handle misaligned scalar writes to memory through top...
authorMartin Jambor <mjambor@suse.cz>
Tue, 13 Mar 2012 14:29:42 +0000 (15:29 +0100)
committerMartin Jambor <jamborm@gcc.gnu.org>
Tue, 13 Mar 2012 14:29:42 +0000 (15:29 +0100)
2012-03-13  Martin Jambor  <mjambor@suse.cz>

* expr.c (expand_assignment): Handle misaligned scalar writes to
memory through top-level MEM_REFs by calling store_bit_field.

* testsuite/gcc.dg/misaligned-expand-2.c: New test.

From-SVN: r185336

gcc/ChangeLog
gcc/expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/misaligned-expand-2.c [new file with mode: 0644]

index d5c679cf479fafcde585dd259d769190efdca109..19bd4ed062c6023caddab254f03b6fff85e58203 100644 (file)
@@ -1,3 +1,8 @@
+2012-03-13  Martin Jambor  <mjambor@suse.cz>
+
+       * expr.c (expand_assignment): Handle misaligned scalar writes to
+       memory through top-level MEM_REFs by calling store_bit_field.
+
 2012-03-13  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/52134
index e6fc100f9f8f7330778f4c35698aabd7f2840c04..59b76a4b5f5cc2811e00cfd280cf4b2c2644e7e8 100644 (file)
@@ -4593,10 +4593,12 @@ expand_assignment (tree to, tree from, bool nontemporal)
   if ((TREE_CODE (to) == MEM_REF
        || TREE_CODE (to) == TARGET_MEM_REF)
       && mode != BLKmode
+      && !mem_ref_refers_to_non_mem_p (to)
       && ((align = get_object_or_type_alignment (to))
          < GET_MODE_ALIGNMENT (mode))
-      && ((icode = optab_handler (movmisalign_optab, mode))
-         != CODE_FOR_nothing))
+      && (((icode = optab_handler (movmisalign_optab, mode))
+          != CODE_FOR_nothing)
+         || SLOW_UNALIGNED_ACCESS (mode, align)))
     {
       addr_space_t as
        = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (to, 0))));
@@ -4639,11 +4641,17 @@ expand_assignment (tree to, tree from, bool nontemporal)
       if (TREE_THIS_VOLATILE (to))
        MEM_VOLATILE_P (mem) = 1;
 
-      create_fixed_operand (&ops[0], mem);
-      create_input_operand (&ops[1], reg, mode);
-      /* The movmisalign<mode> pattern cannot fail, else the assignment would
-        silently be omitted.  */
-      expand_insn (icode, 2, ops);
+      if (icode != CODE_FOR_nothing)
+       {
+         create_fixed_operand (&ops[0], mem);
+         create_input_operand (&ops[1], reg, mode);
+         /* The movmisalign<mode> pattern cannot fail, else the assignment
+            would silently be omitted.  */
+         expand_insn (icode, 2, ops);
+       }
+      else
+       store_bit_field (mem, GET_MODE_BITSIZE (mode),
+                        0, 0, 0, mode, reg);
       return;
     }
 
index 1e6c43facdc8845c48d2bb7fab2b77d8d568e4e7..96b775ea7468e6aaae0cc507716d2fbb90b006d7 100644 (file)
@@ -1,3 +1,7 @@
+2012-03-13  Martin Jambor  <mjambor@suse.cz>
+
+       * gcc.dg/misaligned-expand-2.c: New test.
+
 2012-03-13  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/52134
diff --git a/gcc/testsuite/gcc.dg/misaligned-expand-2.c b/gcc/testsuite/gcc.dg/misaligned-expand-2.c
new file mode 100644 (file)
index 0000000..f3ae97f
--- /dev/null
@@ -0,0 +1,42 @@
+/* Test that expand can generate correct stores to misaligned data even on
+   strict alignment platforms.  */
+
+/* { dg-do run } */
+/* { dg-options "-O0" } */
+
+extern void abort ();
+
+typedef unsigned int myint __attribute__((aligned(1)));
+
+void
+foo (myint *p, unsigned int i)
+{
+  *p = i;
+}
+
+#define cst 0xdeadbeef
+#define NUM 8
+
+struct blah
+{
+  char c;
+  myint i[NUM];
+};
+
+struct blah g;
+
+#define cst 0xdeadbeef
+
+int
+main (int argc, char **argv)
+{
+  int k;
+
+  for (k = 0; k < NUM; k++)
+    {
+      foo (&g.i[k], cst);
+      if (g.i[k] != cst)
+       abort ();
+    }
+  return 0;
+}