From 27315aa68fbb97cc8cbb71da2ca54b9e8052b057 Mon Sep 17 00:00:00 2001
From: Richard Guenther <rguenther@suse.de>
Date: Fri, 22 Jul 2011 11:55:30 +0000
Subject: [PATCH] re PR middle-end/45819 (unexpected unaligned access to
 volatile int)

2011-07-22  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/45819
	* tree-ssa-forwprop.c (forward_propagate_addr_expr_1): Properly
	preserve volatile and notrap flags.

	* gcc.dg/pr45819.c: New testcase.

From-SVN: r176623
---
 gcc/ChangeLog                  |  6 ++++++
 gcc/testsuite/ChangeLog        |  5 +++++
 gcc/testsuite/gcc.dg/pr45819.c | 20 ++++++++++++++++++++
 gcc/tree-ssa-forwprop.c        |  8 ++++++--
 4 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr45819.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index dfba411ffe39..cb9c3ae8d942 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2011-07-22  Richard Guenther  <rguenther@suse.de>
+
+	PR tree-optimization/45819
+	* tree-ssa-forwprop.c (forward_propagate_addr_expr_1): Properly
+	preserve volatile and notrap flags.
+
 2011-07-22  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 	    Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index bae0698f5a5b..d4f641e48678 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2011-07-22  Richard Guenther  <rguenther@suse.de>
+
+	PR tree-optimization/45819
+	* gcc.dg/pr45819.c: New testcase.
+
 2011-07-22  Uros Bizjak  <ubizjak@gmail.com>
 
 	* lib/target-supports.exp (check_ifunc_available): Rewrite.
diff --git a/gcc/testsuite/gcc.dg/pr45819.c b/gcc/testsuite/gcc.dg/pr45819.c
new file mode 100644
index 000000000000..de968003ef20
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr45819.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+struct ehci_regs {
+    char x;
+    unsigned int port_status[0];
+} __attribute__ ((packed));
+
+struct ehci_hcd {
+    struct ehci_regs *regs;
+};
+
+int ehci_hub_control (struct ehci_hcd *ehci, int wIndex)
+{
+  unsigned int *status_reg = &ehci->regs->port_status[wIndex];
+  return *(volatile unsigned int *)status_reg;
+}
+
+/* { dg-final { scan-tree-dump "={v}" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c
index c08cb18e7afe..40db9285b988 100644
--- a/gcc/tree-ssa-forwprop.c
+++ b/gcc/tree-ssa-forwprop.c
@@ -875,6 +875,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs,
 	    }
 	  *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
 				   new_base, new_offset);
+	  TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
+	  TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
 	  gimple_assign_set_lhs (use_stmt,
 				 unshare_expr (TREE_OPERAND (def_rhs, 0)));
 	  *def_rhs_basep = saved;
@@ -927,9 +929,9 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs,
 	  tidy_after_forward_propagate_addr (use_stmt);
 	  return res;
 	}
-      /* If the LHS is a plain dereference and the value type is the same as
+      /* If the RHS is a plain dereference and the value type is the same as
          that of the pointed-to type of the address we can put the
-	 dereferenced address on the LHS preserving the original alias-type.  */
+	 dereferenced address on the RHS preserving the original alias-type.  */
       else if (gimple_assign_rhs1 (use_stmt) == rhs
 	       && useless_type_conversion_p
 		    (TREE_TYPE (gimple_assign_lhs (use_stmt)),
@@ -954,6 +956,8 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs,
 	    }
 	  *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
 				   new_base, new_offset);
+	  TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
+	  TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
 	  gimple_assign_set_rhs1 (use_stmt,
 				  unshare_expr (TREE_OPERAND (def_rhs, 0)));
 	  *def_rhs_basep = saved;
-- 
GitLab