[gcc r16-9274] tree-optimization/126150 - ICE with replace_uses_by

Richard Biener via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:1d91a0b0ec53863c8754e04142b1ee6f37b20751

commit r16-9274-g1d91a0b0ec53863c8754e04142b1ee6f37b20751
Author: Richard Biener <[email protected]>
Date:   Wed Jul 8 13:57:17 2026 +0200

    tree-optimization/126150 - ICE with replace_uses_by
    
    replace_uses_by triggers eventual BB removal from inside a
    FOR_EACH_IMM_USE_STMT iteration.  That's a no-go since we might
    eliminate uses in the list we are currently processing.  The
    fix is to defer EH edge purging.
    
            PR tree-optimization/126150
            * tree-cfg.cc (replace_uses_by): Delay purging of EH edges
            until after FOR_EACH_IMM_USE_STMT finished.
    
            * g++.dg/pr126150.C: New testcase.
    
    (cherry picked from commit 210b4eca880c88e7f7ec970b68a93a2a28bad1be)

Diff:
---
 gcc/testsuite/g++.dg/pr126150.C | 8 ++++++++
 gcc/tree-cfg.cc                 | 6 +++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/g++.dg/pr126150.C b/gcc/testsuite/g++.dg/pr126150.C
new file mode 100644
index 000000000000..0ca536058b51
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr126150.C
@@ -0,0 +1,8 @@
+// { dg-do compile }
+// { dg-options "-fnon-call-exceptions -O2 --param=max-completely-peel-loop-nest-depth=0" }
+
+struct B {
+  B() { t = 0; }
+  ~B();
+  int t;
+} w3[1][2];
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 2e61e68c988e..e42e22cf769f 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -1940,6 +1940,7 @@ replace_uses_by (tree name, tree val)
   use_operand_p use;
   gimple *stmt;
   edge e;
+  auto_bitmap eh_cleanup_bbs;
 
   FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
     {
@@ -1997,10 +1998,13 @@ replace_uses_by (tree name, tree val)
 	    }
 
 	  if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
-	    gimple_purge_dead_eh_edges (gimple_bb (stmt));
+	    bitmap_set_bit (eh_cleanup_bbs, gimple_bb (stmt)->index);
 	}
     }
 
+  if (!bitmap_empty_p (eh_cleanup_bbs))
+    gimple_purge_all_dead_eh_edges (eh_cleanup_bbs);
+
   gcc_checking_assert (has_zero_uses (name));
 
   /* Also update the trees stored in loop structures.  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.