When DSE runs into a redundant zero initialization it can end up
removing a volatile store.  Avoid this.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

        PR tree-optimization/128575
        * tree-ssa-dse.cc (dse_optimize_redundant_stores): Do not
        eliminate volatile stores.

        * gcc.dg/tree-ssa/ssa-dse-55.c: New testcase.
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c | 11 +++++++++++
 gcc/tree-ssa-dse.cc                        |  5 +++--
 2 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c 
b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c
new file mode 100644
index 00000000000..b1e11313161
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-tree-optimized" } */
+
+void scrub_int (int *p)
+{
+  *p = 0;
+  *(volatile int *)p = 0;
+}
+
+/* { dg-final { scan-tree-dump-not " = 0;" "optimized" } } */
+/* { dg-final { scan-tree-dump " ={v} 0;" "optimized" } } */
diff --git a/gcc/tree-ssa-dse.cc b/gcc/tree-ssa-dse.cc
index 0623f2c9090..13cc7abe3b1 100644
--- a/gcc/tree-ssa-dse.cc
+++ b/gcc/tree-ssa-dse.cc
@@ -911,8 +911,9 @@ dse_optimize_redundant_stores (gimple *stmt)
       tree fndecl;
       if ((is_gimple_assign (use_stmt)
           && gimple_vdef (use_stmt)
-          && (gimple_assign_single_p (use_stmt)
-              && initializer_zerop (gimple_assign_rhs1 (use_stmt))))
+          && gimple_assign_single_p (use_stmt)
+          && !gimple_has_volatile_ops (use_stmt)
+          && initializer_zerop (gimple_assign_rhs1 (use_stmt)))
          || (gimple_call_builtin_p (use_stmt, BUILT_IN_NORMAL)
              && (fndecl = gimple_call_fndecl (use_stmt)) != NULL
              && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET

base-commit: d5444af5d6939181794ea2188cdd6e3b6579978a
-- 
2.51.0

Reply via email to