From patchwork Thu May 30 02:46:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 1941518 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=gQYedn6m; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VqVz54xLPz20QD for ; Thu, 30 May 2024 12:47:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C5B2385F024 for ; Thu, 30 May 2024 02:47:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 6E733385E83F for ; Thu, 30 May 2024 02:47:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E733385E83F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E733385E83F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717037227; cv=none; b=GH+LjcseamtUj5Hppjl2v8inkMPOLVZjKRmRm9kMVYSapbhxl5R9J71vYFrwm+Q05c0B2hKJnP8qsbHnm1/97xYXBWuDLDBA5eQj9R1yWMga76vx6PfkHz++PBORxvKg55XdhdHyHj72VzSz7gz2BCQNHJmAJTjxXGpm1dPPkTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717037227; c=relaxed/simple; bh=6JYj+vOVOkyjes4ZAnMe9Cq3ilV5Wf0F3GIud/ypqmU=; h=DKIM-Signature:Message-ID:Date:To:From:Subject:MIME-Version; b=gJXMvuSowFGQ/SV5u1AoVCdfdev5mA0Jl5OhIc9Sd7cAI7/CbvB4nNxAtixYvz0a/HLX6cW9pRMBqYtwejBMogGv7sEIQPHd8a8qxkFNVsYLZNzYKfRxluRheKtKeeSQjt+8Bi4dVdkZ1jYzr8CkHkwfAnfG8gNr/ebPYI1/Uz8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0353724.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 44U27tt4011980; Thu, 30 May 2024 02:47:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc : content-transfer-encoding : content-type : date : from : message-id : mime-version : subject : to; s=pp1; bh=XjDIrVyiyK6KQDJOkKvPiT4eYqaBMsuZlVA0TRsM3fw=; b=gQYedn6m3uVQaYOYcOWGR5RR/P9BiuTB7h3LBY+WUr1dxTJrIHXzCUW30Uq0/88zjG4r J5TRMz1GlH+ke/gLibMErDrSyV0QJifZMKPK7L5NRcmBRQyhsVKHE16wVIS3GWMaZHLL evSoi5zv1y1+TjsqV6BqPI3RXmaRqWD3riko3JKAvIEcp6XVofJ54zbAKYvQlzfn7anG oCKquDAK+lD7G4FQBiVs66T5N1ncfpvHdd3kpV4J4NMECo9YS4XbchKlVODPxe3Zk92L HtROO0uuSsrQVDz3bTHXUUel64viXUumdv+beaxN+dylRvkzFwTHBhcedjAtUCtb6iqm Vg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yegd382v5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 May 2024 02:47:01 +0000 Received: from m0353724.ppops.net (m0353724.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 44U2l0LJ005146; Thu, 30 May 2024 02:47:01 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yegd382ux-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 May 2024 02:47:00 +0000 Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 44U14aE5028975; Thu, 30 May 2024 02:47:00 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3ydpayqg54-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 May 2024 02:47:00 +0000 Received: from smtpav06.fra02v.mail.ibm.com (smtpav06.fra02v.mail.ibm.com [10.20.54.105]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 44U2ksXE52494846 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 May 2024 02:46:56 GMT Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 854F020063; Thu, 30 May 2024 02:46:54 +0000 (GMT) Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E448D20049; Thu, 30 May 2024 02:46:51 +0000 (GMT) Received: from [9.200.103.244] (unknown [9.200.103.244]) by smtpav06.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 30 May 2024 02:46:51 +0000 (GMT) Message-ID: Date: Thu, 30 May 2024 10:46:50 +0800 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches Cc: Segher Boessenkool , David , "Kewen.Lin" , Peter Bergner , Mikael Morin , "MacLeod, Andrew" , Aldy Hernandez , Jakub Jelinek , Richard Sandiford From: HAO CHEN GUI Subject: [PATCH-3v2] Value Range: Add range op for builtin isnormal X-TM-AS-GCONF: 00 X-Proofpoint-GUID: dIkIR4jF5WXzHGaXNpzZzx1p2c6c2yel X-Proofpoint-ORIG-GUID: WcFT6FFd6fyaNA1H4A08IG-hQGzZYP97 X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-29_16,2024-05-28_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 priorityscore=1501 lowpriorityscore=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxlogscore=948 phishscore=0 clxscore=1015 mlxscore=0 spamscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2405010000 definitions=main-2405300020 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, This patch adds the range op for builtin isnormal. It also adds two help function in frange to detect range of normal floating-point and range of subnormal or zero. Compared to previous version, the main change is to set the range to 1 if it's normal number otherwise to 0. https://gcc.gnu.org/pipermail/gcc-patches/2024-May/652221.html Bootstrapped and tested on x86 and powerpc64-linux BE and LE with no regressions. Is it OK for the trunk? Thanks Gui Haochen ChangeLog Value Range: Add range op for builtin isnormal The former patch adds optab for builtin isnormal. Thus builtin isnormal might not be folded at front end. So the range op for isnormal is needed for value range analysis. This patch adds range op for builtin isnormal. gcc/ * gimple-range-op.cc (class cfn_isfinite): New. (op_cfn_finite): New variables. (gimple_range_op_handler::maybe_builtin_call): Handle CFN_BUILT_IN_ISFINITE. * value-range.h (class frange): Declare known_isnormal and known_isdenormal_or_zero. (frange::known_isnormal): Define. (frange::known_isdenormal_or_zero): Define. gcc/testsuite/ * gcc/testsuite/gcc.dg/tree-ssa/range-isnormal.c: New test. patch.diff diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index 5ec5c828fa4..6787f532f11 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -1289,6 +1289,61 @@ public: } } op_cfn_isfinite; +//Implement range operator for CFN_BUILT_IN_ISNORMAL +class cfn_isnormal : public range_operator +{ +public: + using range_operator::fold_range; + using range_operator::op1_range; + virtual bool fold_range (irange &r, tree type, const frange &op1, + const irange &, relation_trio) const override + { + if (op1.undefined_p ()) + return false; + + if (op1.known_isnormal ()) + { + wide_int one = wi::one (TYPE_PRECISION (type)); + r.set (type, one, one); + return true; + } + + if (op1.known_isnan () + || op1.known_isinf () + || op1.known_isdenormal_or_zero ()) + { + r.set_zero (type); + return true; + } + + r.set_varying (type); + return true; + } + virtual bool op1_range (frange &r, tree type, const irange &lhs, + const frange &, relation_trio) const override + { + if (lhs.undefined_p ()) + return false; + + if (lhs.zero_p ()) + { + r.set_varying (type); + return true; + } + + if (!range_includes_zero_p (lhs)) + { + nan_state nan (false); + r.set (type, real_min_representable (type), + real_max_representable (type), nan); + return true; + } + + r.set_varying (type); + return true; + } +} op_cfn_isnormal; + // Implement range operator for CFN_BUILT_IN_ class cfn_parity : public range_operator { @@ -1391,6 +1446,11 @@ gimple_range_op_handler::maybe_builtin_call () m_operator = &op_cfn_isfinite; break; + case CFN_BUILT_IN_ISNORMAL: + m_op1 = gimple_call_arg (call, 0); + m_operator = &op_cfn_isnormal; + break; + CASE_CFN_COPYSIGN_ALL: m_op1 = gimple_call_arg (call, 0); m_op2 = gimple_call_arg (call, 1); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/range-isnormal.c b/gcc/testsuite/gcc.dg/tree-ssa/range-isnormal.c new file mode 100644 index 00000000000..c4df4d839b0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/range-isnormal.c @@ -0,0 +1,37 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ + +#include +void link_error(); + +void test1 (double x) +{ + if (x < __DBL_MAX__ && x > __DBL_MIN__ && !__builtin_isnormal (x)) + link_error (); + + if (x < -__DBL_MIN__ && x > -__DBL_MAX__ && !__builtin_isnormal (x)) + link_error (); +} + +void test2 (float x) +{ + if (x < __FLT_MAX__ && x > __FLT_MIN__ && !__builtin_isnormal (x)) + link_error (); + + if (x < -__FLT_MIN__ && x > - __FLT_MAX__ && !__builtin_isnormal (x)) + link_error (); +} + +void test3 (double x) +{ + if (__builtin_isnormal (x) && __builtin_isinf (x)) + link_error (); +} + +void test4 (float x) +{ + if (__builtin_isnormal (x) && __builtin_isinf (x)) + link_error (); +} + +/* { dg-final { scan-tree-dump-not "link_error" "evrp" } } */ diff --git a/gcc/value-range.h b/gcc/value-range.h index 37ce91dc52d..1443d1906e5 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -588,6 +588,8 @@ public: bool maybe_isinf () const; bool signbit_p (bool &signbit) const; bool nan_signbit_p (bool &signbit) const; + bool known_isnormal () const; + bool known_isdenormal_or_zero () const; protected: virtual bool contains_p (tree cst) const override; @@ -1650,6 +1652,33 @@ frange::known_isfinite () const return (!maybe_isnan () && !real_isinf (&m_min) && !real_isinf (&m_max)); } +// Return TRUE if range is known to be normal. + +inline bool +frange::known_isnormal () const +{ + if (!known_isfinite ()) + return false; + + machine_mode mode = TYPE_MODE (type ()); + return (!real_isdenormal (&m_min, mode) && !real_isdenormal (&m_max, mode) + && !real_iszero (&m_min) && !real_iszero (&m_max) + && (!real_isneg (&m_min) || real_isneg (&m_max))); +} + +// Return TRUE if range is known to be denormal. + +inline bool +frange::known_isdenormal_or_zero () const +{ + if (!known_isfinite ()) + return false; + + machine_mode mode = TYPE_MODE (type ()); + return ((real_isdenormal (&m_min, mode) || real_iszero (&m_min)) + && (real_isdenormal (&m_max, mode) || real_iszero (&m_max))); +} + // Return TRUE if range may be infinite. inline bool