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]> Diff: --- gcc/testsuite/gcc.dg/torture/pr128424.c | 48 ++++++++++++++++++++++ .../gcc.dg/tree-ssa/loop-unswitch-trap-1.c | 20 +++++++++ .../gcc.dg/tree-ssa/loop-unswitch-trap-2.c | 20 +++++++++ gcc/tree-ssa-loop-unswitch.cc | 10 +++++ 4 files changed, 98 insertions(+) diff --git a/gcc/testsuite/gcc.dg/torture/pr128424.c b/gcc/testsuite/gcc.dg/torture/pr128424.c new file mode 100644 index 000000000000..83af42bf3031 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128424.c @@ -0,0 +1,48 @@ +/* { dg-do run } */ +/* { dg-require-effective-target fenv_exceptions } */ +/* { dg-additional-options "-ftrapping-math" } */ + +#include <fenv.h> + +volatile int sink; +int a[16]; +int b[16]; + +__attribute__((noipa)) void +f (double x, int n) +{ + for (int i = 0; i < n; i++) + if (a[i]) + { + if (x < 1.0) + sink = 1; + else + sink = 2; + } +} + +__attribute__((noipa)) void +g (double x, int n, int m) +{ + for (int j = 0; j < m; j++) + if (b[j]) + for (int i = 0; i < n; i++) + if (x < 1.0) + sink = 1; + else + sink = i; +} + +int +main (void) +{ + volatile double nan = __builtin_nan (""); + feclearexcept (FE_INVALID); + f (nan, 16); + if (fetestexcept (FE_INVALID)) + __builtin_abort (); + g (nan, 16, 16); + if (fetestexcept (FE_INVALID)) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-1.c b/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-1.c new file mode 100644 index 000000000000..8accd2524fa6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-1.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fno-trapping-math -fdump-tree-unswitch-details" } */ + +volatile int sink; +int a[16]; + +void +f (double x, int n) +{ + for (int i = 0; i < n; i++) + if (a[i]) + { + if (x < 1.0) + sink = 1; + else + sink = 2; + } +} + +/* { dg-final { scan-tree-dump "unswitching loop" "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-2.c b/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-2.c new file mode 100644 index 000000000000..c80b1b18ee3c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/loop-unswitch-trap-2.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ftrapping-math -fdump-tree-unswitch-details" } */ + +volatile int sink; +int a[16]; + +void +f (double x, int n) +{ + for (int i = 0; i < n; i++) + if (a[i]) + { + if (x < 1.0) + sink = 1; + else + sink = 2; + } +} + +/* { dg-final { scan-tree-dump-not "unswitching loop" "unswitch" } } */ diff --git a/gcc/tree-ssa-loop-unswitch.cc b/gcc/tree-ssa-loop-unswitch.cc index 0f600a3701f3..9bfc9711baac 100644 --- a/gcc/tree-ssa-loop-unswitch.cc +++ b/gcc/tree-ssa-loop-unswitch.cc @@ -524,6 +524,16 @@ find_unswitching_predicates_for_bb (basic_block bb, class loop *loop, if (is_maybe_undefined (use, stmt, loop)) return; } + /* A condition that may trap must not be evaluated where the original + program does not evaluate it. Only the header of LOOP is known to + execute once LOOP is entered, so require that, and do not hoist the + condition out of any enclosing loop, which might not be entered. */ + if (gimple_could_trap_p (stmt)) + { + if (bb != loop->header) + return; + outer_loop = loop; + } /* Narrow OUTER_LOOP. */ if (outer_loop != loop) FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
