https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128147

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kyrylo Tkachov <[email protected]>:

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]>

Reply via email to