public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414]
@ 2022-05-10  8:10 HAO CHEN GUI
  2022-05-10 11:12 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: HAO CHEN GUI @ 2022-05-10  8:10 UTC (permalink / raw)
  To: gcc-patches
  Cc: Segher Boessenkool, David, Peter Bergner, Kewen.Lin, Richard Biener

Hi,
   This patch skips constant folding for fmin/max when either argument
is sNaN. According to C standard,
   fmin(sNaN, sNaN)= qNaN, fmin(sNaN, NaN) = qNaN
   So signaling NaN should be tested and skipped for fmin/max in match.pd.

   The V2 patch splits the for loop and keeps MIN/MAX_EXPR unchanged.

   Bootstrapped and tested on ppc64 Linux BE and LE with no regressions.
Is this okay for trunk? Any recommendations? Thanks a lot.

ChangeLog
2022-05-10 Haochen Gui <guihaoc@linux.ibm.com>

gcc/
	PR target/105414
	* match.pd (minmax): Skip constant folding for fmin/fmax when both
	arguments are sNaN or one is sNaN and another is NaN.

gcc/testsuite/
	PR target/105414
	* gcc.dg/pr105414.c: New.


patch.diff
diff --git a/gcc/match.pd b/gcc/match.pd
index 6d691d302b3..6fb8806412a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3095,10 +3095,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)

 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */

-(for minmax (min max FMIN_ALL FMAX_ALL)
+(for minmax (min max)
  (simplify
   (minmax @0 @0)
   @0))
+/* For fmin() and fmax(), skip folding when both are sNaN.  */
+(for minmax (FMIN_ALL FMAX_ALL)
+ (simplify
+  (minmax @0 @0)
+  (if (!tree_expr_maybe_signaling_nan_p (@0))
+    @0)))
 /* min(max(x,y),y) -> y.  */
 (simplify
  (min:c (max:c @0 @1) @1)
@@ -3198,12 +3204,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (minmax @1 (convert @2)))))

 (for minmax (FMIN_ALL FMAX_ALL)
- /* If either argument is NaN, return the other one.  Avoid the
-    transformation if we get (and honor) a signalling NaN.  */
+ /* If either argument is NaN and other one is not sNaN, return the other
+    one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
  (simplify
   (minmax:c @0 REAL_CST@1)
-  (if (real_isnan (TREE_REAL_CST_PTR (@1))
-       && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
+   (if (real_isnan (TREE_REAL_CST_PTR (@1))
+       && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
+       && !tree_expr_maybe_signaling_nan_p (@0))
    @0)))
 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
    functions to return the numeric arg if the other one is NaN.
diff --git a/gcc/testsuite/gcc.dg/pr105414.c b/gcc/testsuite/gcc.dg/pr105414.c
new file mode 100644
index 00000000000..78772700acf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr105414.c
@@ -0,0 +1,30 @@
+/* { dg-do run { target { *-*-linux* *-*-gnu* } } } */
+/* { dg-options "-O1 -fsignaling-nans -lm" } */
+/* { dg-add-options ieee } */
+/* { dg-require-effective-target issignaling } */
+
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <math.h>
+
+int main()
+{
+  double a = __builtin_nans ("");
+
+  if (issignaling (fmin (a, a)))
+    __builtin_abort ();
+
+  if (issignaling (fmax (a, a)))
+    __builtin_abort ();
+
+  double b = __builtin_nan ("");
+
+  if (issignaling (fmin (a, b)))
+    __builtin_abort ();
+
+  if (issignaling (fmax (a, b)))
+    __builtin_abort ();
+
+  return 0;
+}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414]
  2022-05-10  8:10 [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414] HAO CHEN GUI
@ 2022-05-10 11:12 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-05-10 11:12 UTC (permalink / raw)
  To: HAO CHEN GUI
  Cc: gcc-patches, Segher Boessenkool, David, Peter Bergner, Kewen.Lin

On Tue, May 10, 2022 at 10:10 AM HAO CHEN GUI <guihaoc@linux.ibm.com> wrote:
>
> Hi,
>    This patch skips constant folding for fmin/max when either argument
> is sNaN. According to C standard,
>    fmin(sNaN, sNaN)= qNaN, fmin(sNaN, NaN) = qNaN
>    So signaling NaN should be tested and skipped for fmin/max in match.pd.
>
>    The V2 patch splits the for loop and keeps MIN/MAX_EXPR unchanged.
>
>    Bootstrapped and tested on ppc64 Linux BE and LE with no regressions.
> Is this okay for trunk? Any recommendations? Thanks a lot.

OK.

Richard.

> ChangeLog
> 2022-05-10 Haochen Gui <guihaoc@linux.ibm.com>
>
> gcc/
>         PR target/105414
>         * match.pd (minmax): Skip constant folding for fmin/fmax when both
>         arguments are sNaN or one is sNaN and another is NaN.
>
> gcc/testsuite/
>         PR target/105414
>         * gcc.dg/pr105414.c: New.
>
>
> patch.diff
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 6d691d302b3..6fb8806412a 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3095,10 +3095,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
>
> -(for minmax (min max FMIN_ALL FMAX_ALL)
> +(for minmax (min max)
>   (simplify
>    (minmax @0 @0)
>    @0))
> +/* For fmin() and fmax(), skip folding when both are sNaN.  */
> +(for minmax (FMIN_ALL FMAX_ALL)
> + (simplify
> +  (minmax @0 @0)
> +  (if (!tree_expr_maybe_signaling_nan_p (@0))
> +    @0)))
>  /* min(max(x,y),y) -> y.  */
>  (simplify
>   (min:c (max:c @0 @1) @1)
> @@ -3198,12 +3204,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>     (minmax @1 (convert @2)))))
>
>  (for minmax (FMIN_ALL FMAX_ALL)
> - /* If either argument is NaN, return the other one.  Avoid the
> -    transformation if we get (and honor) a signalling NaN.  */
> + /* If either argument is NaN and other one is not sNaN, return the other
> +    one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
>   (simplify
>    (minmax:c @0 REAL_CST@1)
> -  (if (real_isnan (TREE_REAL_CST_PTR (@1))
> -       && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
> +   (if (real_isnan (TREE_REAL_CST_PTR (@1))
> +       && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
> +       && !tree_expr_maybe_signaling_nan_p (@0))
>     @0)))
>  /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
>     functions to return the numeric arg if the other one is NaN.
> diff --git a/gcc/testsuite/gcc.dg/pr105414.c b/gcc/testsuite/gcc.dg/pr105414.c
> new file mode 100644
> index 00000000000..78772700acf
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr105414.c
> @@ -0,0 +1,30 @@
> +/* { dg-do run { target { *-*-linux* *-*-gnu* } } } */
> +/* { dg-options "-O1 -fsignaling-nans -lm" } */
> +/* { dg-add-options ieee } */
> +/* { dg-require-effective-target issignaling } */
> +
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <math.h>
> +
> +int main()
> +{
> +  double a = __builtin_nans ("");
> +
> +  if (issignaling (fmin (a, a)))
> +    __builtin_abort ();
> +
> +  if (issignaling (fmax (a, a)))
> +    __builtin_abort ();
> +
> +  double b = __builtin_nan ("");
> +
> +  if (issignaling (fmin (a, b)))
> +    __builtin_abort ();
> +
> +  if (issignaling (fmax (a, b)))
> +    __builtin_abort ();
> +
> +  return 0;
> +}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-10 11:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10  8:10 [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414] HAO CHEN GUI
2022-05-10 11:12 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).