public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@redhat.com>
To: GCC patches <gcc-patches@gcc.gnu.org>
Cc: Andrew MacLeod <amacleod@redhat.com>, Aldy Hernandez <aldyh@redhat.com>
Subject: [COMMITTED] Implement ABS_EXPR operator for frange.
Date: Tue, 11 Oct 2022 15:51:36 +0200	[thread overview]
Message-ID: <20221011135136.369644-4-aldyh@redhat.com> (raw)
In-Reply-To: <20221011135136.369644-1-aldyh@redhat.com>

Implementing ABS_EXPR allows us to fold certain __builtin_inf calls
since they are expanded into calls to involving ABS_EXPR.

This is an adaptation of the integer version.

gcc/ChangeLog:

	* range-op-float.cc (class foperator_abs): New.
	(floating_op_table::floating_op_table): Add ABS_EXPR entry.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/vrp-float-abs-1.c: New test.
---
 gcc/range-op-float.cc                         | 91 +++++++++++++++++++
 .../gcc.dg/tree-ssa/vrp-float-abs-1.c         | 17 ++++
 2 files changed, 108 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c

diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
index ef51b7538e3..283eb134c78 100644
--- a/gcc/range-op-float.cc
+++ b/gcc/range-op-float.cc
@@ -1132,6 +1132,95 @@ foperator_ordered::op1_range (frange &r, tree type,
   return true;
 }
 
+class foperator_abs : public range_operator_float
+{
+  using range_operator_float::fold_range;
+  using range_operator_float::op1_range;
+public:
+  bool fold_range (frange &r, tree type,
+		   const frange &op1, const frange &,
+		   relation_kind) const final override;
+  bool op1_range (frange &r, tree type,
+		  const frange &lhs, const frange &op2,
+		  relation_kind rel) const final override;
+} fop_abs;
+
+bool
+foperator_abs::fold_range (frange &r, tree type,
+			   const frange &op1, const frange &op2,
+			   relation_kind) const
+{
+  if (empty_range_varying (r, type, op1, op2))
+    return true;
+  if (op1.known_isnan ())
+    {
+      r.set_nan (type, /*sign=*/false);
+      return true;
+    }
+
+  const REAL_VALUE_TYPE lh_lb = op1.lower_bound ();
+  const REAL_VALUE_TYPE lh_ub = op1.upper_bound ();
+  // Handle the easy case where everything is positive.
+  if (real_compare (GE_EXPR, &lh_lb, &dconst0)
+      && !real_iszero (&lh_lb, /*sign=*/true)
+      && !op1.maybe_isnan (/*sign=*/true))
+    {
+      r = op1;
+      return true;
+    }
+
+  REAL_VALUE_TYPE min = real_value_abs (&lh_lb);
+  REAL_VALUE_TYPE max = real_value_abs (&lh_ub);
+  // If the range contains zero then we know that the minimum value in the
+  // range will be zero.
+  if (real_compare (LE_EXPR, &lh_lb, &dconst0)
+      && real_compare (GE_EXPR, &lh_ub, &dconst0))
+    {
+      if (real_compare (GT_EXPR, &min, &max))
+	max = min;
+      min = dconst0;
+    }
+  else
+    {
+      // If the range was reversed, swap MIN and MAX.
+      if (real_compare (GT_EXPR, &min, &max))
+	std::swap (min, max);
+    }
+
+  r.set (type, min, max);
+  if (op1.maybe_isnan ())
+    r.update_nan (/*sign=*/false);
+  else
+    r.clear_nan ();
+  return true;
+}
+
+bool
+foperator_abs::op1_range (frange &r, tree type,
+			  const frange &lhs, const frange &op2,
+			  relation_kind) const
+{
+  if (empty_range_varying (r, type, lhs, op2))
+    return true;
+  if (lhs.known_isnan ())
+    {
+      r.set_nan (type);
+      return true;
+    }
+
+  // Start with the positives because negatives are an impossible result.
+  frange positives (type, dconst0, frange_val_max (type));
+  positives.update_nan (/*sign=*/false);
+  positives.intersect (lhs);
+  r = positives;
+  // Then add the negative of each pair:
+  // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
+  r.union_ (frange (type,
+		    real_value_negate (&positives.upper_bound ()),
+		    real_value_negate (&positives.lower_bound ())));
+  return true;
+}
+
 class foperator_unordered_lt : public range_operator_float
 {
   using range_operator_float::fold_range;
@@ -1502,6 +1591,8 @@ floating_op_table::floating_op_table ()
   set (UNEQ_EXPR, fop_unordered_equal);
   set (ORDERED_EXPR, fop_ordered);
   set (UNORDERED_EXPR, fop_unordered);
+
+  set (ABS_EXPR, fop_abs);
 }
 
 // Return a pointer to the range_operator_float instance, if there is
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c
new file mode 100644
index 00000000000..4b7b75833e0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c
@@ -0,0 +1,17 @@
+// { dg-do compile }
+// { dg-options "-O2 -fno-thread-jumps -fdump-tree-evrp" }
+
+void link_error ();
+
+void
+foo (double x, double y)
+{
+  if (x > y && __builtin_signbit (y) == 0)
+    {
+      // y == +INF is impossible.
+      if (__builtin_isinf (y))
+        link_error ();
+    }
+}
+
+// { dg-final { scan-tree-dump-not "link_error" "evrp" } }
-- 
2.37.3


      parent reply	other threads:[~2022-10-11 13:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11 13:51 [COMMITTED] Move TRUE case first in range-op.cc Aldy Hernandez
2022-10-11 13:51 ` [COMMITTED] Share common ordered comparison code with UN*_EXPR Aldy Hernandez
2022-10-11 13:51 ` [COMMITTED] Implement op1_range operators for unordered comparisons Aldy Hernandez
2022-10-11 13:57   ` Aldy Hernandez
2022-10-11 13:51 ` Aldy Hernandez [this message]

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=20221011135136.369644-4-aldyh@redhat.com \
    --to=aldyh@redhat.com \
    --cc=amacleod@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).