re PR tree-optimization/80334 (Segfault when taking address of copy of unaligned...
authorRichard Biener <rguenther@suse.de>
Thu, 6 Apr 2017 13:56:35 +0000 (13:56 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 6 Apr 2017 13:56:35 +0000 (13:56 +0000)
2017-04-06  Richard Biener  <rguenther@suse.de>

PR tree-optimization/80334
* tree-ssa-loop-ivopts.c (rewrite_use_address): Properly
preserve alignment of accesses.

* g++.dg/torture/pr80334.C: New testcase.

From-SVN: r246731

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/torture/pr80334.C [new file with mode: 0644]
gcc/tree-ssa-loop-ivopts.c

index 020d9f8707dd89cef825bffddd0b6e139e621116..d4f0c937d44a63bc6f598d0b245f4547e647dae0 100644 (file)
@@ -1,3 +1,9 @@
+2017-04-06  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/80334
+       * tree-ssa-loop-ivopts.c (rewrite_use_address): Properly
+       preserve alignment of accesses.
+
 2017-04-06  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/80262
index 32f5507a33273d852cc3ef9bef4ea2ddbf794706..e5b327469e8c9ed0d7c74edb3cfd700710462658 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-06  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/80334
+       * g++.dg/torture/pr80334.C: New testcase.
+
 2017-04-06  Martin Liska  <mliska@suse.cz>
 
        PR sanitizer/80166
diff --git a/gcc/testsuite/g++.dg/torture/pr80334.C b/gcc/testsuite/g++.dg/torture/pr80334.C
new file mode 100644 (file)
index 0000000..de50c46
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-do run }
+
+struct A { alignas(16) char c; };
+struct B { A unpacked; char d; } __attribute__((packed));
+
+char x;
+
+int
+main()
+{
+  alignas(16) B b[3];
+  for (int i = 0; i < 3; i++) b[i].unpacked.c = 'a' + i;
+  for (int i = 0; i < 3; i++)
+    {
+      auto a = new A(b[i].unpacked);
+      x = a->c;
+    }
+}
index 72e3ff98a3e7a707a55d9bb70d0e727feb4e6778..d5bd0362f25ad31d8907de186e77f16e7e3d7cc9 100644 (file)
@@ -7396,7 +7396,11 @@ rewrite_use_address (struct ivopts_data *data,
     base_hint = var_at_stmt (data->current_loop, cand, use->stmt);
 
   iv = var_at_stmt (data->current_loop, cand, use->stmt);
-  ref = create_mem_ref (&bsi, TREE_TYPE (*use->op_p), &aff,
+  tree type = TREE_TYPE (*use->op_p);
+  unsigned int align = get_object_alignment (*use->op_p);
+  if (align != TYPE_ALIGN (type))
+    type = build_aligned_type (type, align);
+  ref = create_mem_ref (&bsi, type, &aff,
                        reference_alias_ptr_type (*use->op_p),
                        iv, base_hint, data->speed);
   copy_ref_info (ref, *use->op_p);