tree-optimization/126150 - ICE with replace_uses_by

Message ID 23rsqono-338o-07r1-2rns-9n2r87o174o4@fhfr.qr
State Committed
Commit 210b4eca880c88e7f7ec970b68a93a2a28bad1be
Headers
Series tree-optimization/126150 - ICE with replace_uses_by |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Build failed
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Richard Biener July 8, 2026, 12:33 p.m. UTC
  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.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

Richard.

	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.
---
 gcc/testsuite/g++.dg/pr126150.C | 8 ++++++++
 gcc/tree-cfg.cc                 | 6 +++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/pr126150.C
  

Patch

diff --git a/gcc/testsuite/g++.dg/pr126150.C b/gcc/testsuite/g++.dg/pr126150.C
new file mode 100644
index 00000000000..0ca536058b5
--- /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 dc07dde8cfd..15e02691946 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -1893,6 +1893,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)
     {
@@ -1950,10 +1951,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.  */