From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7808) id C538138515FA; Wed, 11 May 2022 01:32:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C538138515FA MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: HaoChen Gui To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-280] This patch skips constant folding for fmin/max when either argument is sNaN. According to C standard X-Act-Checkin: gcc X-Git-Author: Haochen Gui X-Git-Refname: refs/heads/master X-Git-Oldrev: e877898911574037af5aaa68ff6451ec5ced20e9 X-Git-Newrev: 344e425340e3c8e4539b43bf8f661e02c5a5b9a0 Message-Id: <20220511013241.C538138515FA@sourceware.org> Date: Wed, 11 May 2022 01:32:41 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2022 01:32:41 -0000 https://gcc.gnu.org/g:344e425340e3c8e4539b43bf8f661e02c5a5b9a0 commit r13-280-g344e425340e3c8e4539b43bf8f661e02c5a5b9a0 Author: Haochen Gui Date: Mon May 9 17:34:23 2022 +0800 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. 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. Diff: --- gcc/match.pd | 17 ++++++++++++----- gcc/testsuite/gcc.dg/pr105414.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 1fdd98b375e..632243ea92e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3089,10 +3089,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) @@ -3192,12 +3198,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 +#include + +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; +}