From patchwork Thu Apr 27 13:30:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 88260 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp269658vqo; Thu, 27 Apr 2023 06:31:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4PTBiXmPbYToQUrdai7W28JpJvV8Bj+1RUGuKGu0g3mApjahjpQZn+1KFWXuNw67ZJMsJe X-Received: by 2002:a17:907:25cc:b0:953:457c:7976 with SMTP id ae12-20020a17090725cc00b00953457c7976mr1650640ejc.23.1682602270887; Thu, 27 Apr 2023 06:31:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682602270; cv=none; d=google.com; s=arc-20160816; b=A9kpkq0rb9vxjUT+KHTEK7Ouqq3BJsRahgPowUoIR1kUTbZF6wQ5gJVhYz5h6YL7p/ xBvhxVmS3mc6eNuVChY6Jq8iuWaxplOuGeujp7PWNZo0jTpdaLTeLQCN0UsBlH02HiXm G2f3Pf1ErQWuZxHJmEDS3xK/oFSZ5OPSmUh5U7eBiGlhh5OfBJWSVEyJnWJ8xLdh7pNq 6IvWDg7/hrfPjRDa4ZCvW4Hd4S2i//p8ETc5//hSMJyLAhYyT5mat9YUDfRbeJPzx5Jj fbaeB3YWv9bSajBXNwhh6izYCgZJJgmZGyuNughjGXbIYpuyMVcLynSRGjcxnX4A+zr2 417w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=GAJkTH4j4WV3vdWXIBYi6Sh0O9unsZ+Q79D3WwtrrkI=; b=LL7qVk+VDLB//fDKioryl/YfhpHUpBq2iCPMpNhb3/g5mUErYNM/l7RLuLxCGGh0VU GLWAR9Ardqn8+STKZywBHvFpvct4GvJEVsYPetfbFuYPwI+180ibj/cuT0W+NJXMf6FB 3SvzYf59qxBxCRzxQV7JLxHvRI+PPKOiN1iR7qjX77T8mCHXiW6oo49vFaqrpHnW0VL3 K/PTZhBftpoDz4qm9ITuYk+nZcTTLNO+5dNTPP5wmRLbMfr60l4RGv1+w3rPnl/OIO4i p/aiksCrH5AQXiRVg263jwr0hNEV7yBXBXNCHdPlxQ4AwzmqdE3veg6cLTc7/LtTFJ77 fD3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FtYbalTA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bm4-20020a0564020b0400b005047e14ecc4si13749433edb.224.2023.04.27.06.31.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 06:31:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FtYbalTA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8A8413858C2D for ; Thu, 27 Apr 2023 13:31:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A8413858C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682602269; bh=GAJkTH4j4WV3vdWXIBYi6Sh0O9unsZ+Q79D3WwtrrkI=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FtYbalTAZCkjyQe5fIiOx0lvlGuBgoQdRdGTiRCa/D6JdkHT7Z1VtzHJobyfZhWs0 5T7CIOLA1Sr0VI0abggbACmJsOkYMbuQdfOC8UU/z/lE6p47dVdvlZwyEbDAxDYyrB OvlV0PrgSMdbTTw96QE0cZUtkZ9FkZjtry32epkI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5361D3858D33 for ; Thu, 27 Apr 2023 13:30:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5361D3858D33 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-197-sDuNbw7vOICWJALtT9swwQ-1; Thu, 27 Apr 2023 09:30:22 -0400 X-MC-Unique: sDuNbw7vOICWJALtT9swwQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2ED32817230 for ; Thu, 27 Apr 2023 13:30:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.156]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 86DE5492B03; Thu, 27 Apr 2023 13:30:21 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 33RDUIxf2760027 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 27 Apr 2023 15:30:19 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 33RDUIS52760026; Thu, 27 Apr 2023 15:30:18 +0200 Date: Thu, 27 Apr 2023 15:30:17 +0200 To: Aldy Hernandez Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] gimple-range-op: Handle sqrt (basic bounds only) Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764336359036094045?= X-GMAIL-MSGID: =?utf-8?q?1764336359036094045?= Hi! The following patch adds sqrt support (but similarly to sincos, only dumb basic ranges only). Ok for trunk if it passes bootstrap/regtest? Will improve this incrementally and sin/cos as well. 2023-04-27 Jakub Jelinek * gimple-range-op.cc (class cfn_sqrt): New type. (op_cfn_sqrt): New variable. (gimple_range_op_handler::maybe_builtin_call): Handle CASE_CFN_SQRT{,_FN}. * gcc.dg/tree-ssa/range-sqrt.c: New test. Jakub --- gcc/gimple-range-op.cc.jj 2023-04-27 11:57:09.865879982 +0200 +++ gcc/gimple-range-op.cc 2023-04-27 15:15:05.089787859 +0200 @@ -400,6 +400,83 @@ public: } } op_cfn_copysign; +class cfn_sqrt : public range_operator_float +{ +public: + using range_operator_float::fold_range; + using range_operator_float::op1_range; + virtual bool fold_range (frange &r, tree type, + const frange &lh, const frange &, + relation_trio) const final override + { + if (lh.undefined_p ()) + return false; + if (lh.known_isnan () || real_less (&lh.upper_bound (), &dconstm0)) + { + r.set_nan (type); + return true; + } + unsigned bulps + = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), true); + if (bulps == ~0U) + r.set_varying (type); + else if (bulps == 0) + r.set (type, dconstm0, dconstinf); + else + { + REAL_VALUE_TYPE boundmin = dconstm0; + while (bulps--) + frange_nextafter (TYPE_MODE (type), boundmin, dconstninf); + r.set (type, boundmin, dconstinf); + } + if (!lh.maybe_isnan () && !real_less (&lh.lower_bound (), &dconst0)) + r.clear_nan (); + return true; + } + virtual bool op1_range (frange &r, tree type, + const frange &lhs, const frange &, + relation_trio) const final override + { + if (lhs.undefined_p ()) + return false; + + // A known NAN means the input is [-INF,-0.) U +-NAN. + if (lhs.known_isnan ()) + { + known_nan: + REAL_VALUE_TYPE ub = dconstm0; + frange_nextafter (TYPE_MODE (type), ub, dconstninf); + r.set (type, dconstninf, ub); + // No r.flush_denormals_to_zero (); here - it is a reverse op. + return true; + } + + // Results outside of [-0.0, +Inf] are impossible. + const REAL_VALUE_TYPE &ub = lhs.upper_bound (); + if (real_less (&ub, &dconstm0)) + { + if (!lhs.maybe_isnan ()) + r.set_undefined (); + else + // If lhs could be NAN and finite result is impossible, + // the range is like lhs.known_isnan () above. + goto known_nan; + return true; + } + + if (!lhs.maybe_isnan ()) + { + // If NAN is not valid result, the input cannot include either + // a NAN nor values smaller than -0. + r.set (type, dconstm0, dconstinf, nan_state (false, false)); + return true; + } + + r.set_varying (type); + return true; + } +} op_cfn_sqrt; + class cfn_sincos : public range_operator_float { public: @@ -961,6 +1038,13 @@ gimple_range_op_handler::maybe_builtin_c m_valid = true; break; + CASE_CFN_SQRT: + CASE_CFN_SQRT_FN: + m_op1 = gimple_call_arg (call, 0); + m_float = &op_cfn_sqrt; + m_valid = true; + break; + CASE_CFN_SIN: CASE_CFN_SIN_FN: m_op1 = gimple_call_arg (call, 0); --- gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c.jj 2023-04-27 15:10:09.285102144 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c 2023-04-27 15:12:01.478465821 +0200 @@ -0,0 +1,41 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp -fno-thread-jumps" } + +#include + +void use (double); +void link_error (); + +void +foo (double x) +{ + if (__builtin_isnan (x)) + __builtin_unreachable (); + x = sqrt (x); + if (x < -0.0) + link_error (); + use (x); +} + +void +bar (double x) +{ + if (!__builtin_isnan (sqrt (x))) + { + if (__builtin_isnan (x)) + link_error (); + if (x < -0.0) + link_error (); + } +} + +void +stool (double x) +{ + double res1 = sqrt (x); + double res2 = __builtin_sqrt (x); + if (res1 < -0.0 || res2 < -0.0) + link_error (); +} + +// { dg-final { scan-tree-dump-not "link_error" "evrp" { target { { *-*-linux* } && { glibc } } } } }