From patchwork Tue May 10 08:10:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 53698 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4F6CA3836004 for ; Tue, 10 May 2022 08:16:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F6CA3836004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652170593; bh=+JFHF7yXY8V5W796T0aN414lwgkCGYXXLTenc8HyIQw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=m5YlLYS8r0lYUP5TrcVgVhfLzjqJVmfNFjVY9mfMKz84ymvb8Yew+HEW0WQKCkUKO xLt5YN188lDKHPgLZHEA4t1T0Jwu2sMyoACwb+k30Rl05kxTN8c87EYLoB88TA5/WG Pj9b4b5U8JuZlSpRk50J6aNmmIBGj9r39skg6i2w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 5200438346AB for ; Tue, 10 May 2022 08:10:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5200438346AB Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24A7b9aq014629; Tue, 10 May 2022 08:10:20 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3fyhyjjjqv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 10 May 2022 08:10:20 +0000 Received: from m0098396.ppops.net (m0098396.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 24A85wA5024737; Tue, 10 May 2022 08:10:19 GMT Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3fyhyjjjq1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 10 May 2022 08:10:19 +0000 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 24A87C4E017740; Tue, 10 May 2022 08:10:17 GMT Received: from b06avi18878370.portsmouth.uk.ibm.com (b06avi18878370.portsmouth.uk.ibm.com [9.149.26.194]) by ppma06ams.nl.ibm.com with ESMTP id 3fwg1j3pj6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 10 May 2022 08:10:17 +0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06avi18878370.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 24A89vcn33685958 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 10 May 2022 08:09:57 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F31E7A4051; Tue, 10 May 2022 08:10:13 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C9F70A404D; Tue, 10 May 2022 08:10:11 +0000 (GMT) Received: from [9.197.225.138] (unknown [9.197.225.138]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 10 May 2022 08:10:11 +0000 (GMT) Message-ID: Date: Tue, 10 May 2022 16:10:10 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Content-Language: en-US To: gcc-patches Subject: [PATCH v2] Skip constant folding for fmin/max when either argument is sNaN [PR105414] X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 7YQH1q-RxalMJez8q_qkGG4WHZAtafTu X-Proofpoint-GUID: 6BLeCIqP8OiQjo0Hvr5U1QlzTZWeerQq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-10_01,2022-05-09_02,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 adultscore=0 priorityscore=1501 lowpriorityscore=0 malwarescore=0 bulkscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 clxscore=1015 impostorscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205100034 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, 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-Patchwork-Original-From: HAO CHEN GUI via Gcc-patches From: HAO CHEN GUI Reply-To: HAO CHEN GUI Cc: Peter Bergner , David , Segher Boessenkool Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 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 +#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; +}