public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] Replace finite_operands_p with maybe_isnan.
@ 2022-10-20 14:13 Aldy Hernandez
  2022-10-20 14:13 ` [COMMITTED] Do not set NAN flags for VARYING ranges when !HONOR_NANS Aldy Hernandez
  2022-10-20 18:56 ` [COMMITTED] Replace finite_operands_p with maybe_isnan Mikael Morin
  0 siblings, 2 replies; 5+ messages in thread
From: Aldy Hernandez @ 2022-10-20 14:13 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, Aldy Hernandez

The finite_operands_p function was incorrectly named, as it only
returned TRUE when !NAN.  This was leftover from the initial
implementation of frange.  Using the maybe_isnan() nomenclature is
more consistent and easier to understand.

gcc/ChangeLog:

	* range-op-float.cc (finite_operand_p): Remove.
	(finite_operands_p): Rename to...
	(maybe_isnan): ...this.
	(frelop_early_resolve): Use maybe_isnan instead of finite_operands_p.
	(foperator_equal::fold_range): Same.
	(foperator_equal::op1_range): Same.
	(foperator_not_equal::fold_range): Same.
	(foperator_lt::fold_range): Same.
	(foperator_le::fold_range): Same.
	(foperator_gt::fold_range): Same.
	(foperator_ge::fold_range): Same.
---
 gcc/range-op-float.cc | 41 ++++++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
index 0605a908684..2a4a99ba467 100644
--- a/gcc/range-op-float.cc
+++ b/gcc/range-op-float.cc
@@ -166,20 +166,15 @@ range_operator_float::op1_op2_relation (const frange &lhs ATTRIBUTE_UNUSED) cons
   return VREL_VARYING;
 }
 
-// Return TRUE if OP1 is known to be free of NANs.
+// Return TRUE if OP1 and OP2 may be a NAN.
 
 static inline bool
-finite_operand_p (const frange &op1)
+maybe_isnan (const frange &op1, const frange &op2)
 {
-  return flag_finite_math_only || !op1.maybe_isnan ();
-}
-
-// Return TRUE if OP1 and OP2 are known to be free of NANs.
+  if (flag_finite_math_only)
+    return false;
 
-static inline bool
-finite_operands_p (const frange &op1, const frange &op2)
-{
-  return flag_finite_math_only || (!op1.maybe_isnan () && !op2.maybe_isnan ());
+  return op1.maybe_isnan () || op2.maybe_isnan ();
 }
 
 // Floating version of relop_early_resolve that takes into account NAN
@@ -196,7 +191,7 @@ frelop_early_resolve (irange &r, tree type,
 
   // We can fold relations from the oracle when we know both operands
   // are free of NANs, or when -ffinite-math-only.
-  return (finite_operands_p (op1, op2)
+  return (!maybe_isnan (op1, op2)
 	  && relop_early_resolve (r, type, op1, op2, rel, my_rel));
 }
 
@@ -391,7 +386,7 @@ foperator_equal::fold_range (irange &r, tree type,
       else
 	r = range_false (type);
     }
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       // If ranges do not intersect, we know the range is not equal,
       // otherwise we don't know anything for sure.
@@ -441,7 +436,7 @@ foperator_equal::op1_range (frange &r, tree type,
       // If the result is false, the only time we know anything is
       // if OP2 is a constant.
       else if (op2.singleton_p ()
-	       || (finite_operand_p (op2) && op2.zero_p ()))
+	       || (!op2.maybe_isnan () && op2.zero_p ()))
 	{
 	  REAL_VALUE_TYPE tmp = op2.lower_bound ();
 	  r.set (type, tmp, tmp, VR_ANTI_RANGE);
@@ -494,7 +489,7 @@ foperator_not_equal::fold_range (irange &r, tree type,
       else
 	r = range_false (type);
     }
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       // If ranges do not intersect, we know the range is not equal,
       // otherwise we don't know anything for sure.
@@ -590,7 +585,7 @@ foperator_lt::fold_range (irange &r, tree type,
 
   if (op1.known_isnan () || op2.known_isnan ())
     r = range_false (type);
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       if (real_less (&op1.upper_bound (), &op2.lower_bound ()))
 	r = range_true (type);
@@ -706,7 +701,7 @@ foperator_le::fold_range (irange &r, tree type,
 
   if (op1.known_isnan () || op2.known_isnan ())
     r = range_false (type);
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       if (real_compare (LE_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
 	r = range_true (type);
@@ -814,7 +809,7 @@ foperator_gt::fold_range (irange &r, tree type,
 
   if (op1.known_isnan () || op2.known_isnan ())
     r = range_false (type);
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       if (real_compare (GT_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
 	r = range_true (type);
@@ -930,7 +925,7 @@ foperator_ge::fold_range (irange &r, tree type,
 
   if (op1.known_isnan () || op2.known_isnan ())
     r = range_false (type);
-  else if (finite_operands_p (op1, op2))
+  else if (!maybe_isnan (op1, op2))
     {
       if (real_compare (GE_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
 	r = range_true (type);
@@ -1302,7 +1297,7 @@ public:
       return false;
     // The result is the same as the ordered version when the
     // comparison is true or when the operands cannot be NANs.
-    if (finite_operands_p (op1, op2) || r == range_true (type))
+    if (!maybe_isnan (op1, op2) || r == range_true (type))
       return true;
     else
       {
@@ -1331,7 +1326,7 @@ public:
       return false;
     // The result is the same as the ordered version when the
     // comparison is true or when the operands cannot be NANs.
-    if (finite_operands_p (op1, op2) || r == range_true (type))
+    if (!maybe_isnan (op1, op2) || r == range_true (type))
       return true;
     else
       {
@@ -1412,7 +1407,7 @@ public:
       return false;
     // The result is the same as the ordered version when the
     // comparison is true or when the operands cannot be NANs.
-    if (finite_operands_p (op1, op2) || r == range_true (type))
+    if (!maybe_isnan (op1, op2) || r == range_true (type))
       return true;
     else
       {
@@ -1495,7 +1490,7 @@ public:
       return false;
     // The result is the same as the ordered version when the
     // comparison is true or when the operands cannot be NANs.
-    if (finite_operands_p (op1, op2) || r == range_true (type))
+    if (!maybe_isnan (op1, op2) || r == range_true (type))
       return true;
     else
       {
@@ -1577,7 +1572,7 @@ public:
       return false;
     // The result is the same as the ordered version when the
     // comparison is true or when the operands cannot be NANs.
-    if (finite_operands_p (op1, op2) || r == range_true (type))
+    if (!maybe_isnan (op1, op2) || r == range_true (type))
       return true;
     else
       {
-- 
2.37.3


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

* [COMMITTED] Do not set NAN flags for VARYING ranges when !HONOR_NANS.
  2022-10-20 14:13 [COMMITTED] Replace finite_operands_p with maybe_isnan Aldy Hernandez
@ 2022-10-20 14:13 ` Aldy Hernandez
  2022-10-20 18:56 ` [COMMITTED] Replace finite_operands_p with maybe_isnan Mikael Morin
  1 sibling, 0 replies; 5+ messages in thread
From: Aldy Hernandez @ 2022-10-20 14:13 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, Aldy Hernandez

Since NANs can't appear in ranges for !HONOR_NANS, there's no reason
to set them in a VARYING range.

gcc/ChangeLog:

	* value-range.h (frange::set_varying): Do not set NAN flags for
	!HONOR_NANS.
	* value-range.cc (frange::normalize_kind): Adjust for no NAN when
	!HONOR_NANS.
	(frange::verify_range): Same.
	* range-op-float.cc (maybe_isnan): Remove flag_finite_math_only check.
---
 gcc/range-op-float.cc |  3 ---
 gcc/value-range.cc    | 11 ++++++++---
 gcc/value-range.h     | 12 ++++++++++--
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
index 2a4a99ba467..a9e74c86877 100644
--- a/gcc/range-op-float.cc
+++ b/gcc/range-op-float.cc
@@ -171,9 +171,6 @@ range_operator_float::op1_op2_relation (const frange &lhs ATTRIBUTE_UNUSED) cons
 static inline bool
 maybe_isnan (const frange &op1, const frange &op2)
 {
-  if (flag_finite_math_only)
-    return false;
-
   return op1.maybe_isnan () || op2.maybe_isnan ();
 }
 
diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index 511cd0ad767..bcda4987307 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -388,7 +388,7 @@ frange::normalize_kind ()
       && frange_val_is_min (m_min, m_type)
       && frange_val_is_max (m_max, m_type))
     {
-      if (m_pos_nan && m_neg_nan)
+      if (!HONOR_NANS (m_type) || (m_pos_nan && m_neg_nan))
 	{
 	  set_varying (m_type);
 	  return true;
@@ -396,7 +396,7 @@ frange::normalize_kind ()
     }
   else if (m_kind == VR_VARYING)
     {
-      if (!m_pos_nan || !m_neg_nan)
+      if (HONOR_NANS (m_type) && (!m_pos_nan || !m_neg_nan))
 	{
 	  m_kind = VR_RANGE;
 	  m_min = frange_val_min (m_type);
@@ -712,14 +712,19 @@ frange::supports_type_p (const_tree type) const
 void
 frange::verify_range ()
 {
+  if (flag_finite_math_only)
+    gcc_checking_assert (!maybe_isnan ());
   switch (m_kind)
     {
     case VR_UNDEFINED:
       gcc_checking_assert (!m_type);
       return;
     case VR_VARYING:
+      if (flag_finite_math_only)
+	gcc_checking_assert (!m_pos_nan && !m_neg_nan);
+      else
+	gcc_checking_assert (m_pos_nan && m_neg_nan);
       gcc_checking_assert (m_type);
-      gcc_checking_assert (m_pos_nan && m_neg_nan);
       gcc_checking_assert (frange_val_is_min (m_min, m_type));
       gcc_checking_assert (frange_val_is_max (m_max, m_type));
       return;
diff --git a/gcc/value-range.h b/gcc/value-range.h
index 60b989b2b50..b48542a68aa 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -1103,8 +1103,16 @@ frange::set_varying (tree type)
   m_type = type;
   m_min = frange_val_min (type);
   m_max = frange_val_max (type);
-  m_pos_nan = true;
-  m_neg_nan = true;
+  if (HONOR_NANS (m_type))
+    {
+      m_pos_nan = true;
+      m_neg_nan = true;
+    }
+  else
+    {
+      m_pos_nan = false;
+      m_neg_nan = false;
+    }
 }
 
 inline void
-- 
2.37.3


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

* Re: [COMMITTED] Replace finite_operands_p with maybe_isnan.
  2022-10-20 14:13 [COMMITTED] Replace finite_operands_p with maybe_isnan Aldy Hernandez
  2022-10-20 14:13 ` [COMMITTED] Do not set NAN flags for VARYING ranges when !HONOR_NANS Aldy Hernandez
@ 2022-10-20 18:56 ` Mikael Morin
  2022-10-20 18:58   ` Mikael Morin
  1 sibling, 1 reply; 5+ messages in thread
From: Mikael Morin @ 2022-10-20 18:56 UTC (permalink / raw)
  To: Aldy Hernandez, GCC patches

Hello,

Le 20/10/2022 à 16:13, Aldy Hernandez via Gcc-patches a écrit :
> The finite_operands_p function was incorrectly named, as it only
> returned TRUE when !NAN.  This was leftover from the initial
> implementation of frange.  Using the maybe_isnan() nomenclature is
> more consistent and easier to understand.
> 
(...)
> 
> diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
> index 0605a908684..2a4a99ba467 100644
> --- a/gcc/range-op-float.cc
> +++ b/gcc/range-op-float.cc
(...)
> @@ -441,7 +436,7 @@ foperator_equal::op1_range (frange &r, tree type,
>         // If the result is false, the only time we know anything is
>         // if OP2 is a constant.
>         else if (op2.singleton_p ()
> -	       || (finite_operand_p (op2) && op2.zero_p ()))
> +	       || (!op2.maybe_isnan () && op2.zero_p ()))
>   	{
>   	  REAL_VALUE_TYPE tmp = op2.lower_bound ();
>   	  r.set (type, tmp, tmp, VR_ANTI_RANGE);

Doesn't this miss a check of flag_finite_math_only to be strictly 
equivalent?  You keep that check for the two-arguments case, so I guess 
it's not redundant?

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

* Re: [COMMITTED] Replace finite_operands_p with maybe_isnan.
  2022-10-20 18:56 ` [COMMITTED] Replace finite_operands_p with maybe_isnan Mikael Morin
@ 2022-10-20 18:58   ` Mikael Morin
  2022-10-20 19:17     ` Aldy Hernandez
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Morin @ 2022-10-20 18:58 UTC (permalink / raw)
  To: gcc-patches

Le 20/10/2022 à 20:56, Mikael Morin a écrit :
> 
> Doesn't this miss a check of flag_finite_math_only to be strictly 
> equivalent?  You keep that check for the two-arguments case, so I guess 
> it's not redundant?

Well, the check is removed in the follow-up patch, so maybe is is after all.

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

* Re: [COMMITTED] Replace finite_operands_p with maybe_isnan.
  2022-10-20 18:58   ` Mikael Morin
@ 2022-10-20 19:17     ` Aldy Hernandez
  0 siblings, 0 replies; 5+ messages in thread
From: Aldy Hernandez @ 2022-10-20 19:17 UTC (permalink / raw)
  To: Mikael Morin; +Cc: gcc-patches

On Thu, Oct 20, 2022 at 8:59 PM Mikael Morin <morin-mikael@orange.fr> wrote:
>
> Le 20/10/2022 à 20:56, Mikael Morin a écrit :
> >
> > Doesn't this miss a check of flag_finite_math_only to be strictly
> > equivalent?  You keep that check for the two-arguments case, so I guess
> > it's not redundant?
>
> Well, the check is removed in the follow-up patch, so maybe is is after all.

Good catch.  I should indeed have added a check there, but as you
noted it was about to be removed ;-).

Thanks.
Aldy


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

end of thread, other threads:[~2022-10-20 19:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-20 14:13 [COMMITTED] Replace finite_operands_p with maybe_isnan Aldy Hernandez
2022-10-20 14:13 ` [COMMITTED] Do not set NAN flags for VARYING ranges when !HONOR_NANS Aldy Hernandez
2022-10-20 18:56 ` [COMMITTED] Replace finite_operands_p with maybe_isnan Mikael Morin
2022-10-20 18:58   ` Mikael Morin
2022-10-20 19:17     ` Aldy Hernandez

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