https://gcc.gnu.org/g:bfc5dd9360f265f89234d6f5a00ab8ff007ce773

commit r17-6057-gbfc5dd9360f265f89234d6f5a00ab8ff007ce773
Author: Richard Biener <[email protected]>
Date:   Mon Nov 9 12:47:16 2026 +0100

    tree-optimization/128575 - avoid removing volatile store
    
    When DSE runs into a redundant zero initialization it can end up
    removing a volatile store.  Avoid this.
    
            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.

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-55.c | 11 +++++++++++
 gcc/tree-ssa-dse.cc                        |  5 +++--
 2 files changed, 14 insertions(+), 2 deletions(-)

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 000000000000..b1e113131615
--- /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 0623f2c90905..13cc7abe3b19 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

Reply via email to