From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vk1-xa36.google.com (mail-vk1-xa36.google.com [IPv6:2607:f8b0:4864:20::a36]) by sourceware.org (Postfix) with ESMTPS id C85E73955CBC for ; Tue, 10 May 2022 11:12:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C85E73955CBC Received: by mail-vk1-xa36.google.com with SMTP id o132so8238511vko.11 for ; Tue, 10 May 2022 04:12:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=wnphr0U5NgLvLcetChzZzAobwm8cugOgeJTTyMvOyg4=; b=CJwREy/WR6nvM+lHiwazsE9Zsw++BxPZbLxlIH5WaLjjDqqmfH7e7KqGTlxZ0Fmj6U DbcAEpFAa9qKWwWrh8xvR+tJwD45KxFwuDjovP/XtYRu8/LM8psaY2SF5I7mGydeVkYE 14Y9/iq2ij8QCkDGEqG8+QOB4NRVtuysfIY97pai3Nq8BkLX8CqhLWf4VWaQkUGri8Zm cyhQ1vxhHdg2WOoqUvX95uqBjp3V3QslxmIQtlm+b6J2smvHhUy3IEr4W5hjpAlaQZLJ yvrvSq8gDcLezQAH6l7j5V5M7uFfVY3De4JE0Xhmao30br6UqcnDj4owQC0nO4KDYeD9 uKZg== X-Gm-Message-State: AOAM530sSm7ZnhUys9dy/Hyk77i2RD82rZll3fOaNoNwljg44Pp/3lDu W+oMUocwmk235NtDj0RLdvrpwJBpl79VijgZQ44= X-Google-Smtp-Source: ABdhPJzZJp6EwD7KuXUKiib0D1mCmYyjPvv2hfrPf9L9oUJEif7kWngHFyRjiphDk4+RvDHe8M3SdTxrBFWlkIAuUYg= X-Received: by 2002:a05:6122:695:b0:352:be3b:c07 with SMTP id n21-20020a056122069500b00352be3b0c07mr4433374vkq.40.1652181137095; Tue, 10 May 2022 04:12:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Tue, 10 May 2022 13:12:05 +0200 Message-ID: Subject: Re: [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414] To: HAO CHEN GUI Cc: gcc-patches , Segher Boessenkool , David , Peter Bergner , "Kewen.Lin" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 May 2022 11:12:19 -0000 On Tue, May 10, 2022 at 10:10 AM HAO CHEN GUI wrote= : > > Hi, > This patch skips constant folding for fmin/max when either argument > is sNaN. According to C standard, > fmin(sNaN, sNaN=EF=BC=89=3D qNaN, fmin(sNaN, NaN) =3D 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 > > gcc/ > PR target/105414 > * match.pd (minmax): Skip constant folding for fmin/fmax when bot= h > 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 othe= r > + one. Avoid the transformation if we get (and honor) a signalling Na= N. */ > (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/pr105= 414.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 =3D __builtin_nans (""); > + > + if (issignaling (fmin (a, a))) > + __builtin_abort (); > + > + if (issignaling (fmax (a, a))) > + __builtin_abort (); > + > + double b =3D __builtin_nan (""); > + > + if (issignaling (fmin (a, b))) > + __builtin_abort (); > + > + if (issignaling (fmax (a, b))) > + __builtin_abort (); > + > + return 0; > +}