From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 7975A3857C75 for ; Thu, 26 Nov 2020 13:56:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7975A3857C75 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=roger@nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:In-Reply-To:References:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=02548sRjYnWCbVBxz+MDx3fgCCClaPMfyO7+Fg5iURI=; b=W7B5V4PoApQ90alP/d7JrjKE8 84xdtEKD2Dl4HymF+OSPMuUMxshlok2dpkyUJRBDa9Kom8nKJTDC7XU8UJraeEFj91m2VbNcUS2dd unwVGFNUfKMbcYy9oHgb7n9jA7ATlZ6p4p9bZ/Y6nv0pDSM3uTN+jTZ1ybjveKgw45Xu1qG2Iyxx+ UZL09eebm1KUhOEW7e4d7FbnMAEd9RJTeDx7whTRVOd1Vzz2zi6NEtdbZLp5ECRx1EIZWFt85pCtI KSr6BlvUlGWoiRwdWicuobUozGhzjoxof+1N4bBoCrk7SbNNLN4dEcLG7GXTtNGZs1aMp3Vnpo9Xd cAxhA+vxQ==; Received: from host86-148-67-112.range86-148.btcentralplus.com ([86.148.67.112]:58415 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kiHkj-0005Re-3b; Thu, 26 Nov 2020 08:56:05 -0500 From: "Roger Sayle" To: "'Richard Biener'" , "'Jakub Jelinek'" Cc: "'Joseph S. Myers'" , "'Jason Merrill'" , References: <20201126083155.GN3788@tucnak> <20201126094903.GV3788@tucnak> In-Reply-To: Subject: RE: [PATCH] fold-const: Don't consider NaN non-negative [PR97965] Date: Thu, 26 Nov 2020 13:56:03 -0000 Message-ID: <02b501d6c3fb$e248fc90$a6daf5b0$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_02B6_01D6C3FB.E24998D0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQLL5N5q6NtZ9ly9leL4d+phv6886wJdci8XAn4rGigBsUZ6yqe7vkbw Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Thu, 26 Nov 2020 13:56:09 -0000 This is a multipart message in MIME format. ------=_NextPart_000_02B6_01D6C3FB.E24998D0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Many thanks for including me on this discussion. It's extremely interesting... NaNs have a sign-bit, so copyexpr (and negate and ABS_EXPR) are well-defined, and it's reasonable for nonnegative_p to reflect this. IMHO, the true bug is that we can't fold away (any) comparisons against NaN when flag_trapping_math, irrespective of qNaN, -qNaN, sNaN or -sNaN. My completely untested solution is the attached patch. My apologies, I'm not even set up to compile things on the laptop that I'm composing this e-mail on, but my notes/proposals on tackling PR97965 are easier expressed as the actual suggested changes/edits. [Forgive me if I've made a typo]. As Joseph correctly points out, signaling NaNs are a red herring here, as both sNaNs and qNaNs may trap during regular (ordered) comparisons. The piece that I'll leave to the (C++) front-end folks to argue, is whether constexpr implies -fno-trapping-math like initializers, c.f. START_FOLD_INIT/END_FOLD_INIT in fold-const.c. This controls whether the test case should be consistently true or consistently false, but the patches above address Jakub's concern in the PR that things should at least be consistent. I hope this helps. I'm happy to spin this patch myself but it may take a little while. Hopefully, this is sufficient to point folks in the right (or one possible) direction. Best regards, Roger -- Roger Sayle NextMove Software Limited Cambridge, UK -----Original Message----- From: Richard Biener Sent: 26 November 2020 10:04 To: Jakub Jelinek Cc: Joseph S. Myers ; Jason Merrill ; gcc-patches@gcc.gnu.org; roger@nextmovesoftware.com Subject: Re: [PATCH] fold-const: Don't consider NaN non-negative [PR97965] On Thu, 26 Nov 2020, Jakub Jelinek wrote: > On Thu, Nov 26, 2020 at 09:16:29AM +0000, Richard Biener wrote: > > > So, I really don't know if we want this or not, posting it for discussions. > > > > Is copysign (x, NaN) supposed to be well-defined? We'd stop folding > > this then, no? > > Yes, we'd stop folding several cases with NaNs. > > > I think the ABS_EXPR < 0 to false folding is simply incomplete > > and should first check whether the operands are ordered? That said, > > NaN is nonnegative but NaN < 0 isn't false(?) > > > > So I don't think the patch is good. > > Another possibility (if we have this optimization already in match.pd > too) would be to only optimize the < 0 case in GENERIC if !HONOR_NANS > like the >= 0 case is and only optimize it in GIMPLE. Though with the > default -ftrapping-math I think even optimizing qNaN < 0 to 0 is > incorrect, even that should raise invalid exception, shouldn't it? > So perhaps add a defaulted argument to the *nonnegative* APIs that > would say whether unordered is ok or not? Roger recently added some exhaustive changes in related areas, so let's see if he has anything to say here. Richard. ------=_NextPart_000_02B6_01D6C3FB.E24998D0 Content-Type: text/plain; name="patch.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patch.txt" diff --git a/gcc/fold-const.c b/gcc/fold-const.c=0A= index 632a241a964..b76e80c02a3 100644=0A= --- a/gcc/fold-const.c=0A= +++ b/gcc/fold-const.c=0A= @@ -12007,8 +12007,8 @@ fold_binary_loc (location_t loc, enum tree_code = code, tree type,=0A= strict_overflow_p =3D false;=0A= if (code =3D=3D GE_EXPR=0A= && (integer_zerop (arg1)=0A= - || (! HONOR_NANS (arg0)=0A= - && real_zerop (arg1)))=0A= + || (real_zerop (arg1)=0A= + && !tree_expr_maybe_nan_p (arg0)))=0A= && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))=0A= {=0A= if (strict_overflow_p)=0A= @@ -12024,7 +12024,10 @@ fold_binary_loc (location_t loc, enum tree_code = code, tree type,=0A= /* Convert ABS_EXPR < 0 to false. */=0A= strict_overflow_p =3D false;=0A= if (code =3D=3D LT_EXPR=0A= - && (integer_zerop (arg1) || real_zerop (arg1))=0A= + && (integer_zerop (arg1)=0A= + || (real_zerop (arg1)=0A= + && (!flag_trapping_math=0A= + || !tree_expr_maybe_nan_p (arg0))))=0A= && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))=0A= {=0A= if (strict_overflow_p)=0A= diff --git a/gcc/match.pd b/gcc/match.pd=0A= index f8b65154a9e..dec19ed2d57 100644=0A= --- a/gcc/match.pd=0A= +++ b/gcc/match.pd=0A= @@ -3998,7 +3998,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))=0A= /* x !=3D NaN is always true, other ops are always false. */=0A= (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))=0A= - && ! HONOR_SNANS (@1))=0A= + && ! flag_trapping_math)=0A= { constant_boolean_node (cmp =3D=3D NE_EXPR, type); })=0A= /* Fold comparisons against infinity. */=0A= (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))=0A= diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c=0A= index 47e7aebda8a..ab53570ac8c 100644=0A= --- a/gcc/simplify-rtx.c=0A= +++ b/gcc/simplify-rtx.c=0A= @@ -5732,12 +5732,13 @@ simplify_const_relational_operation (enum = rtx_code code,=0A= if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1))=0A= switch (code)=0A= {=0A= + case NE:=0A= + return flag_trapping_math ? 0 : const_true_rtx;=0A= case UNEQ:=0A= case UNLT:=0A= case UNGT:=0A= case UNLE:=0A= case UNGE:=0A= - case NE:=0A= case UNORDERED:=0A= return const_true_rtx;=0A= case EQ:=0A= @@ -5746,6 +5747,7 @@ simplify_const_relational_operation (enum rtx_code = code,=0A= case LE:=0A= case GE:=0A= case LTGT:=0A= + return flag_trapping_math ? 0 : const0_rtx;=0A= case ORDERED:=0A= return const0_rtx;=0A= default:=0A= ------=_NextPart_000_02B6_01D6C3FB.E24998D0--