https://gcc.gnu.org/g:465c1ac0dddb92d38be6b602d1732478cc16e7ae

commit r17-5918-g465c1ac0dddb92d38be6b602d1732478cc16e7ae
Author: Richard Biener <[email protected]>
Date:   Tue Nov 3 15:34:16 2026 +0100

    tree-optimization/128398 - hosting of trapping FP comparisons
    
    The following fixes vn_nary_may_trap to properly handle FP comparisons
    and also conversions from float to integer as PR128237 notes.  It
    adjusts gimple_could_trap_p_1 accordingly.  The functions still
    differ in their default handling of FP expressions not explicitly
    handled in operation_could_trap_helper_p which I believe is a
    bit of an unfortunate interface erroring on the "wrong" side for
    both users.  This should be addressed separately.  This does
    not address the core issue of PR128237 with regard to FP to
    integer conversion builtins.
    
            PR tree-optimization/128398
            PR tree-optimization/128237
            * tree-ssa-sccvn.cc (vn_nary_may_trap): Set fp_operation
            correctly for comparisons and FIX_TRUNC_EXPR.  Also do not
            trap for VEC_COND_EXPR.
            * gimple.cc (gimple_could_trap_p_1): Likewise for
            FIX_TRUNC_EXPR and VEC_COND_EXPR.
    
            * gcc.dg/torture/pr128398.c: New testcase.

Diff:
---
 gcc/gimple.cc                           |  5 +++--
 gcc/testsuite/gcc.dg/torture/pr128398.c | 28 ++++++++++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                   |  9 ++++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/gcc/gimple.cc b/gcc/gimple.cc
index 2295f353472b..6509c02a9c5b 100644
--- a/gcc/gimple.cc
+++ b/gcc/gimple.cc
@@ -2446,12 +2446,13 @@ gimple_could_trap_p_1 (const gimple *s, bool 
include_mem, bool include_stores)
       op = gimple_assign_rhs_code (s);
 
       /* COND_EXPR does not trap.  */
-      if (op == COND_EXPR)
+      if (op == COND_EXPR || op == VEC_COND_EXPR)
        return false;
 
       /* For comparisons we need to check rhs operand types instead of lhs type
          (which is BOOLEAN_TYPE).  */
-      if (TREE_CODE_CLASS (op) == tcc_comparison)
+      if (TREE_CODE_CLASS (op) == tcc_comparison
+         || op == FIX_TRUNC_EXPR)
        t = TREE_TYPE (gimple_assign_rhs1 (s));
       else
        t = TREE_TYPE (gimple_assign_lhs (s));
diff --git a/gcc/testsuite/gcc.dg/torture/pr128398.c 
b/gcc/testsuite/gcc.dg/torture/pr128398.c
new file mode 100644
index 000000000000..2724302b7f02
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128398.c
@@ -0,0 +1,28 @@
+/* { dg-do run { target *-*-*gnu* } } */
+/* { dg-additional-options "-D_GNU_SOURCE" } */
+/* { dg-require-effective-target fenv_exceptions } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+__attribute__((noipa)) void maybe_exit (int c) { if (c) exit (0); }
+
+__attribute__((noipa)) int f (double a, double b, int c)
+{
+  int r;
+  if (c > 5)
+    {
+      maybe_exit (c);
+      r = a < b;
+    }
+  else
+    r = (a < b) * c + 1;
+  return r;
+}
+
+int main (void)
+{
+  feenableexcept (FE_INVALID);
+  f (__builtin_nan (""), 1.0, 7);
+  return 1;
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 49557bb637a2..f8a68d4e4fd6 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -7004,11 +7004,18 @@ vn_nary_may_trap (vn_nary_op_t nary)
   bool handled, ret;
   unsigned i;
 
+  if (nary->opcode == COND_EXPR || nary->opcode == VEC_COND_EXPR)
+    return false;
+
   if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
       || TREE_CODE_CLASS (nary->opcode) == tcc_unary
       || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
     {
-      type = nary->type;
+      if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
+         || nary->opcode == FIX_TRUNC_EXPR)
+       type = TREE_TYPE (nary->op[0]);
+      else
+       type = nary->type;
       fp_operation = FLOAT_TYPE_P (type);
       if (fp_operation)
        {

Reply via email to