From: Aldy Hernandez <aldyh@redhat.com>
To: GCC patches <gcc-patches@gcc.gnu.org>
Cc: Andrew MacLeod <amacleod@redhat.com>,
Jakub Jelinek <jakub@redhat.com>,
Aldy Hernandez <aldyh@redhat.com>
Subject: [PATCH] Implement range-op entry for sin/cos.
Date: Tue, 18 Apr 2023 15:12:50 +0200 [thread overview]
Message-ID: <20230418131250.310916-1-aldyh@redhat.com> (raw)
[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 <math.h>
+
+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" } }
--
2.39.2
next reply other threads:[~2023-04-18 13:13 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-18 13:12 Aldy Hernandez [this message]
2023-04-20 12:59 ` Jakub Jelinek
2023-04-20 13:17 ` Siddhesh Poyarekar
2023-04-20 14:02 ` Jakub Jelinek
2023-04-20 14:20 ` Jakub Jelinek
2023-04-20 15:22 ` Siddhesh Poyarekar
2023-04-20 15:52 ` Jakub Jelinek
2023-04-20 17:57 ` Siddhesh Poyarekar
2023-04-21 1:14 ` Siddhesh Poyarekar
2023-04-21 6:52 ` Jakub Jelinek
2023-04-21 11:20 ` Siddhesh Poyarekar
2023-04-25 8:59 ` Aldy Hernandez
2023-04-24 16:03 ` Jakub Jelinek
2023-04-24 16:05 ` Siddhesh Poyarekar
2023-04-24 16:09 ` Jakub Jelinek
2023-04-24 16:33 ` Jeff Law
2023-04-21 16:40 ` Jakub Jelinek
2023-04-21 20:43 ` Mikael Morin
2023-04-21 20:45 ` Jakub Jelinek
2023-04-25 9:10 ` Aldy Hernandez
2023-04-25 9:08 ` Aldy Hernandez
2023-04-27 11:13 ` [PATCH] v2: " Jakub Jelinek
2023-04-27 11:46 ` Aldy Hernandez
2023-04-27 11:53 ` Jakub Jelinek
2023-04-27 12:03 ` Aldy Hernandez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230418131250.310916-1-aldyh@redhat.com \
--to=aldyh@redhat.com \
--cc=amacleod@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).