From patchwork Tue Apr 18 13:12:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 67915 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 BDC77385735C for ; Tue, 18 Apr 2023 13:13:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDC77385735C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681823613; bh=522JHeI86VkIT4uW457TbIqqWj5Ucd5mT/d/fQoNz84=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=plZmAJyptyDD0McSm48unFts2HpPMV70DxSkuqN3QzUB3bcDdrbkJD6Jy9jkEZQsB JxVkfi/oR9zISFW5HceES2NPZR3ajJJQh9xqn//aaAVHvuk8uWSGGgQu7LQ0S1AOd1 eDO63aNxoSUg25udc1nqud8w/1l0e/l61wWg36+8= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id EACB83858D1E for ; Tue, 18 Apr 2023 13:13:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EACB83858D1E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-530-ii-lMuwJMP-djaLDxLftoA-1; Tue, 18 Apr 2023 09:12:58 -0400 X-MC-Unique: ii-lMuwJMP-djaLDxLftoA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9EAEE85530D for ; Tue, 18 Apr 2023 13:12:58 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.194.164]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3DD6640C6E6E; Tue, 18 Apr 2023 13:12:58 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 33IDCuHf311004 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 15:12:56 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 33IDCup7311003; Tue, 18 Apr 2023 15:12:56 +0200 To: GCC patches Cc: Andrew MacLeod , Jakub Jelinek , Aldy Hernandez Subject: [PATCH] Implement range-op entry for sin/cos. Date: Tue, 18 Apr 2023 15:12:50 +0200 Message-Id: <20230418131250.310916-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" [I don't know why I keep poking at floats. I must really like the pain. Jakub, are you OK with this patch for trunk?] This is the range-op entry for sin/cos. It is meant to serve as an example of what we can do for glibc math functions. It is by no means exhaustive, just a stub to restrict the return range from sin/cos to [-1.0, 1.0] with appropriate smarts of NANs. As can be seen in the testcase, we see sin() as well as __builtin_sin() in the IL, and can resolve the resulting range accordingly. gcc/ChangeLog: * gimple-range-op.cc (class cfn_sincos): New. (gimple_range_op_handler::maybe_builtin_call): Add case for sin/cos. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/range-sincos.c: New test. --- gcc/gimple-range-op.cc | 63 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c | 40 +++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index 4ca32a7b5d5..36390f2645e 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -402,6 +402,60 @@ public: } } op_cfn_copysign; +class cfn_sincos : 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.known_isnan () || lh.known_isinf ()) + { + r.set_nan (type); + return true; + } + r.set (type, dconstm1, dconst1); + if (!lh.maybe_isnan ()) + 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.maybe_isnan ()) + { + // If NAN is not valid result, the input cannot include either + // a NAN nor a +-INF. + REAL_VALUE_TYPE lb = real_min_representable (type); + REAL_VALUE_TYPE ub = real_max_representable (type); + r.set (type, lb, ub, nan_state (false, false)); + return true; + } + // A known NAN means the input is [-INF,-INF][+INF,+INF] U +-NAN, + // which we can't currently represent. + if (lhs.known_isnan ()) + { + r.set_varying (type); + return true; + } + // Results outside of [-1.0, +1.0] are impossible. + REAL_VALUE_TYPE lb = lhs.lower_bound (); + REAL_VALUE_TYPE ub = lhs.upper_bound (); + if (real_less (&lb, &dconstm1) + || real_less (&dconst1, &ub)) + { + r.set_undefined (); + return true; + } + + r.set_varying (type); + return true; + } +} op_cfn_sincos; + // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER. class cfn_toupper_tolower : public range_operator { @@ -880,6 +934,15 @@ gimple_range_op_handler::maybe_builtin_call () m_valid = true; break; + CASE_CFN_SIN: + CASE_CFN_SIN_FN: + CASE_CFN_COS: + CASE_CFN_COS_FN: + m_op1 = gimple_call_arg (call, 0); + m_float = &op_cfn_sincos; + m_valid = true; + break; + case CFN_BUILT_IN_TOUPPER: case CFN_BUILT_IN_TOLOWER: // Only proceed If the argument is compatible with the LHS. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c b/gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c new file mode 100644 index 00000000000..50fbac1b68f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c @@ -0,0 +1,40 @@ +// { 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 = sin (x); + if (x < -1.0 || x > 1.0) + link_error (); + use (x); +} + +void bar(double x) +{ + if (!__builtin_isnan (sin (x))) + { + if (__builtin_isnan (x)) + link_error (); + if (__builtin_isinf (x)) + link_error (); + } +} + +void stool (double x) +{ + double res1 = sin (x); + double res2 = __builtin_sin (x); + if (res1 < -1.0 || res2 < -1.0) + link_error (); + if (res1 > 1.0 || res2 > 1.0) + link_error (); +} + +// { dg-final { scan-tree-dump-not "link_error" "evrp" } }