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

commit r17-6342-ga859e978a52ee6b9828d6dabfc26d5f8511970c3
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 12 01:08:46 2026 -0800

    tree-optimization/128147 - Do not sink trapping stmts when dead exceptions 
are kept
    
    With -fnon-call-exceptions -fno-delete-dead-exceptions a load that may
    trap must execute even when its value is unused.  Sinking only checks
    gimple_has_side_effects, which is false for a plain load, so in
    
      int f (int *p, int c)
      {
        int x = *p;
        if (c)
          return x;
        return 0;
      }
    
    sink moves the load into the conditional block:
    
      bb 2:  _4 = *p_3(D);  if (c_5(D) != 0) ...
      =>
      bb 3:  _4 = *p_3(D);  return _4;
    
    The trap is then lost on the c == 0 path.  Refuse to sink statements
    that may throw unless dead exceptions can be deleted.  This is the same
    condition sink_code_in_bb uses before removing a dead statement.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128147
            * tree-ssa-sink.cc (statement_sink_location): Do not sink
            statements that may throw when !cfun->can_delete_dead_exceptions.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128147
            * g++.dg/torture/pr128147.C: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/testsuite/g++.dg/torture/pr128147.C | 39 +++++++++++++++++++++++++++++++++
 gcc/tree-ssa-sink.cc                    |  6 +++++
 2 files changed, 45 insertions(+)

diff --git a/gcc/testsuite/g++.dg/torture/pr128147.C 
b/gcc/testsuite/g++.dg/torture/pr128147.C
new file mode 100644
index 000000000000..20d13a64072a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr128147.C
@@ -0,0 +1,39 @@
+// { dg-do run { target { { *-*-linux* *-*-gnu* } && { mmap && signal } } } }
+// { dg-additional-options "-fnon-call-exceptions -fno-delete-dead-exceptions" 
}
+
+#include <signal.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+static void
+on_segv (int)
+{
+  throw 1;
+}
+
+__attribute__((noipa)) int
+f (int *p, int c)
+{
+  int x = *p;
+  if (c)
+    return x;
+  return 0;
+}
+
+int
+main ()
+{
+  int *p = (int *) mmap (0, 4096, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
+                        -1, 0);
+  if (p == MAP_FAILED || signal (SIGSEGV, on_segv) == SIG_ERR)
+    return 0;
+  try
+    {
+      f (p, 0);
+    }
+  catch (int)
+    {
+      return 0;
+    }
+  abort ();
+}
diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc
index 2c6cad2687ce..dffb91accd7c 100644
--- a/gcc/tree-ssa-sink.cc
+++ b/gcc/tree-ssa-sink.cc
@@ -324,6 +324,10 @@ statement_sink_location (gimple *stmt, basic_block frombb,
 
      We can't sink statements that have volatile operands.
 
+     We can't sink statements that may throw when dead exceptions must be
+     preserved, since sinking into a conditional block drops the exception
+     on the paths that bypass it.
+
      We don't want to sink dead code, so anything with 0 immediate uses is not
      sunk.
 
@@ -335,6 +339,8 @@ statement_sink_location (gimple *stmt, basic_block frombb,
   */
   if (stmt_ends_bb_p (stmt)
       || gimple_has_side_effects (stmt)
+      || (!cfun->can_delete_dead_exceptions
+         && stmt_could_throw_p (cfun, stmt))
       || (cfun->has_local_explicit_reg_vars
          && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt))) == BLKmode))
     return false;

Reply via email to