public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] frange: flush denormals to zero for -funsafe-math-optimizations.
@ 2022-09-17  8:24 Aldy Hernandez
  2022-09-19  7:37 ` Richard Biener
  0 siblings, 1 reply; 8+ messages in thread
From: Aldy Hernandez @ 2022-09-17  8:24 UTC (permalink / raw)
  To: Jakub Jelinek, Richard Henderson
  Cc: GCC patches, Andrew MacLeod, Aldy Hernandez

Jakub has mentioned that for -funsafe-math-optimizations we may flush
denormals to zero, in which case we need to be careful to extend the
ranges to the appropriate zero.  This patch does exactly that.  For a
range of [x, -DENORMAL] we flush to [x, -0.0] and for [+DENORMAL, x]
we flush to [+0.0, x].

It is unclear whether we should do this for Alpha, since I believe
flushing to zero is the default, and the port requires -mieee for IEEE
sanity.  If so, perhaps we should add a target hook so backends are
free to request flushing to zero.

Thoughts?

gcc/ChangeLog:

	* value-range.cc (frange::flush_denormals_to_zero): New.
	(frange::set): Call flush_denormals_to_zero.
	* value-range.h (class frange): Add flush_denormals_to_zero.
---
 gcc/value-range.cc | 24 ++++++++++++++++++++++++
 gcc/value-range.h  |  1 +
 2 files changed, 25 insertions(+)

diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index 67d5d7fa90f..f285734f0e0 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2)
   return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2));
 }
 
+// Flush denormal endpoints to the appropriate 0.0.
+
+void
+frange::flush_denormals_to_zero ()
+{
+  if (undefined_p () || known_isnan ())
+    return;
+
+  // Flush [x, -DENORMAL] to [x, -0.0].
+  if (real_isdenormal (&m_max) && real_isneg (&m_max))
+    {
+      m_max = dconst0;
+      if (HONOR_SIGNED_ZEROS (m_type))
+	m_max.sign = 1;
+    }
+  // Flush [+DENORMAL, x] to [+0.0, x].
+  if (real_isdenormal (&m_min) && !real_isneg (&m_min))
+    m_min = dconst0;
+}
+
 // Setter for franges.
 
 void
@@ -317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind)
   gcc_checking_assert (tree_compare (LE_EXPR, min, max));
 
   normalize_kind ();
+
+  if (flag_unsafe_math_optimizations)
+    flush_denormals_to_zero ();
+
   if (flag_checking)
     verify_range ();
 }
diff --git a/gcc/value-range.h b/gcc/value-range.h
index 3a401f3e4e2..795b1f00fdc 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -327,6 +327,7 @@ private:
   bool union_nans (const frange &);
   bool intersect_nans (const frange &);
   bool combine_zeros (const frange &, bool union_p);
+  void flush_denormals_to_zero ();
 
   tree m_type;
   REAL_VALUE_TYPE m_min;
-- 
2.37.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-09-20  8:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-17  8:24 [PATCH] frange: flush denormals to zero for -funsafe-math-optimizations Aldy Hernandez
2022-09-19  7:37 ` Richard Biener
2022-09-19  8:25   ` Jakub Jelinek
2022-09-19 13:04   ` Aldy Hernandez
2022-09-19 13:06     ` Aldy Hernandez
2022-09-19 13:44     ` Richard Biener
2022-09-20  5:22       ` Aldy Hernandez
2022-09-20  8:26         ` Jakub Jelinek

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).