'inf/inf' should raise an invalid operation exception at runtime.  So it
should not be folded during compilation unless -fno-trapping-math is
used.

gcc/
        PR middle-end/95115
        * fold-const.cc (const_binop): Do not fold "inf/inf".

gcc/testsuite
        * gcc.dg/pr95115.c: New test.
---
 gcc/fold-const.cc              |  8 ++++++++
 gcc/testsuite/gcc.dg/pr95115.c | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr95115.c

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index fd9c6352d4f..0e99d5a2e3d 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -1283,6 +1283,14 @@ const_binop (enum tree_code code, tree arg1, tree arg2)
          && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
        return NULL_TREE;
 
+      /* Don't perform "inf/inf", because it would raise an invalid
+        operation exception (IEEE 754 section 7.2 clause d).  */
+      if (code == RDIV_EXPR
+         && flag_trapping_math
+         && REAL_VALUE_ISINF (d1)
+         && REAL_VALUE_ISINF (d2))
+       return NULL_TREE;
+
       /* If either operand is a NaN, just return it.  Otherwise, set up
         for floating-point trap; we return an overflow.  */
       if (REAL_VALUE_ISNAN (d1))
diff --git a/gcc/testsuite/gcc.dg/pr95115.c b/gcc/testsuite/gcc.dg/pr95115.c
new file mode 100644
index 00000000000..46a95dfb698
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr95115.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -ftrapping-math" } */
+/* { dg-add-options ieee } */
+/* { dg-require-effective-target fenv_exceptions } */
+
+#include <fenv.h>
+#include <stdlib.h>
+
+double
+x (void)
+{
+  double d = __builtin_inf ();
+  return d / d;
+}
+
+int
+main (void)
+{
+  double r = x ();
+  if (!__builtin_isnan (r))
+       abort ();
+  if (!fetestexcept (FE_INVALID))
+       abort ();
+  exit (0);
+}
-- 
2.35.1


Reply via email to