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

--- Comment #3 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:b242df8741d2328e7fb2b8216dd96ad03b91c8b1

commit r17-6348-gb242df8741d2328e7fb2b8216dd96ad03b91c8b1
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 12 01:24:05 2026 -0800

    tree-optimization/128424 - do not unswitch on trapping conditions

    Loop unswitching accepts any loop-invariant condition and evaluates it
    in front of the loop.  For a condition that may trap, such as a
    floating-point comparison with -ftrapping-math, this raises an
    exception on paths where the original program never evaluates the
    condition:

      for (i = 0; i < n; i++)
        if (a[i])
          if (x < 1.0)   /* x is NaN, a[] is all zero.  */
            ...

    becomes

      if (x < 1.0)
        for (...) ...

    and now raises FE_INVALID.

    Only the header of a loop is known to execute once the loop is entered.
    Reject conditions that may trap unless they are in the header of the
    loop being unswitched.  Also stop outer loop unswitching on such a
    condition, since the enclosing loop may not be entered.

    This disables unswitching of non-header floating-point comparisons
    with the default -ftrapping-math.  With -fno-trapping-math the
    conditions are still unswitched.

    Bootstrapped and tested on aarch64-none-linux-gnu.

    gcc/ChangeLog:

            PR tree-optimization/128424
            * tree-ssa-loop-unswitch.cc (find_unswitching_predicates_for_bb):
            Reject conditions that may trap unless they are in the loop
            header.  Do not hoist them out of enclosing loops.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/128424
            * gcc.dg/torture/pr128424.c: New test.
            * gcc.dg/tree-ssa/loop-unswitch-trap-1.c: New test.
            * gcc.dg/tree-ssa/loop-unswitch-trap-2.c: New test.

    Signed-off-by: Kyrylo Tkachov <[email protected]>

Reply via email to