public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-5397] frange: Fix up foperator_{, not_}equal::fold_range for signed zeros [PR108540]
Date: Thu, 26 Jan 2023 16:28:32 +0000 (GMT)	[thread overview]
Message-ID: <20230126162832.C3CDE3858C83@sourceware.org> (raw)

https://gcc.gnu.org/g:09176201ec6a21c25b1edb07f19f83be22a123f9

commit r13-5397-g09176201ec6a21c25b1edb07f19f83be22a123f9
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jan 26 17:21:22 2023 +0100

    frange: Fix up foperator_{,not_}equal::fold_range for signed zeros [PR108540]
    
    The following testcases are miscompiled, because threader sees some
    SSA_NAME would have -0.0 value and when computing range of SSA_NAME == 0.0
    foperator_equal::fold_range sees one operand has [-0.0, -0.0] singleton
    range, the other [0.0, 0.0], they aren't equal (frange operator== uses
    real_identical etc. rather than real comparisons) and so it thinks they
    compare unequal.  With signed zeros -0.0 == 0.0 is true though, so we
    need to special case the both ranges singleton code.
    Similarly, if we see op1 range being say [-42.0, -0.0] and op2 range
    [0.0, 42.0], we'd check that the intersection of the two ranges is empty
    (that is correct) and fold the result of == between such operands to
    [0, 0] which is wrong, because -0.0 == 0.0, it needs to be [0, 1].
    Similarly for foperator_not_equal::fold_range.
    
    2023-01-26  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/108540
            * range-op-float.cc (foperator_equal::fold_range): If both op1 and op2
            are singletons, use range_true even if op1 != op2
            when one range is [-0.0, -0.0] and another [0.0, 0.0].  Similarly,
            even if intersection of the ranges is empty and one has
            zero low bound and another zero high bound, use range_true_and_false
            rather than range_false.
            (foperator_not_equal::fold_range): If both op1 and op2
            are singletons, use range_false even if op1 != op2
            when one range is [-0.0, -0.0] and another [0.0, 0.0].  Similarly,
            even if intersection of the ranges is empty and one has
            zero low bound and another zero high bound, use range_true_and_false
            rather than range_true.
    
            * gcc.c-torture/execute/ieee/pr108540-1.c: New test.
            * gcc.c-torture/execute/ieee/pr108540-2.c: New test.

Diff:
---
 gcc/range-op-float.cc                              | 40 +++++++++--
 .../gcc.c-torture/execute/ieee/pr108540-1.c        | 84 ++++++++++++++++++++++
 .../gcc.c-torture/execute/ieee/pr108540-2.c        | 23 ++++++
 3 files changed, 142 insertions(+), 5 deletions(-)

diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
index 74ac4658378..2db83aeb2fc 100644
--- a/gcc/range-op-float.cc
+++ b/gcc/range-op-float.cc
@@ -607,6 +607,10 @@ foperator_equal::fold_range (irange &r, tree type,
     {
       if (op1 == op2)
 	r = range_true (type);
+      // If one operand is -0.0 and other 0.0, they are still equal.
+      else if (real_iszero (&op1.lower_bound ())
+	       && real_iszero (&op2.lower_bound ()))
+	r = range_true (type);
       else
 	r = range_false (type);
     }
@@ -617,7 +621,18 @@ foperator_equal::fold_range (irange &r, tree type,
       frange tmp = op1;
       tmp.intersect (op2);
       if (tmp.undefined_p ())
-	r = range_false (type);
+	{
+	  // If one range is [whatever, -0.0] and another
+	  // [0.0, whatever2], we don't know anything either,
+	  // because -0.0 == 0.0.
+	  if ((real_iszero (&op1.upper_bound ())
+	       && real_iszero (&op2.lower_bound ()))
+	      || (real_iszero (&op1.lower_bound ())
+		  && real_iszero (&op2.upper_bound ())))
+	    r = range_true_and_false (type);
+	  else
+	    r = range_false (type);
+	}
       else
 	r = range_true_and_false (type);
     }
@@ -708,10 +723,14 @@ foperator_not_equal::fold_range (irange &r, tree type,
   // consist of a single value, and then compare them.
   else if (op1.singleton_p () && op2.singleton_p ())
     {
-      if (op1 != op2)
-	r = range_true (type);
-      else
+      if (op1 == op2)
 	r = range_false (type);
+      // If one operand is -0.0 and other 0.0, they are still equal.
+      else if (real_iszero (&op1.lower_bound ())
+	       && real_iszero (&op2.lower_bound ()))
+	r = range_false (type);
+      else
+	r = range_true (type);
     }
   else if (!maybe_isnan (op1, op2))
     {
@@ -720,7 +739,18 @@ foperator_not_equal::fold_range (irange &r, tree type,
       frange tmp = op1;
       tmp.intersect (op2);
       if (tmp.undefined_p ())
-	r = range_true (type);
+	{
+	  // If one range is [whatever, -0.0] and another
+	  // [0.0, whatever2], we don't know anything either,
+	  // because -0.0 == 0.0.
+	  if ((real_iszero (&op1.upper_bound ())
+	       && real_iszero (&op2.lower_bound ()))
+	      || (real_iszero (&op1.lower_bound ())
+		  && real_iszero (&op2.upper_bound ())))
+	    r = range_true_and_false (type);
+	  else
+	    r = range_true (type);
+	}
       else
 	r = range_true_and_false (type);
     }
diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c b/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c
new file mode 100644
index 00000000000..ebd4c502ee5
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c
@@ -0,0 +1,84 @@
+/* PR tree-optimization/108540 */
+
+__attribute__((noipa)) void
+bar (const char *cp, unsigned long size, char sign, int dsgn)
+{
+  if (__builtin_strcmp (cp, "ZERO") != 0 || size != 4 || sign != '-' || dsgn != 1)
+    __builtin_abort ();
+}
+
+__attribute__((noipa)) void
+foo (int x, int ch, double d)
+{
+  const char *cp = "";
+  unsigned long size = 0;
+  char sign = '\0';
+  switch (x)
+    {
+    case 42:
+      if (__builtin_isinf (d))
+	{
+	  if (d < 0)
+	    sign = '-';
+	  cp = "Inf";
+	  size = 3;
+	  break;
+	}
+      if (__builtin_isnan (d))
+	{
+	  cp = "NaN";
+	  size = 3;
+	  break;
+	}
+      if (d < 0)
+	{
+	  d = -d;
+	  sign = '-';
+	}
+      else if (d == 0.0 && __builtin_signbit (d))
+	sign = '-';
+      else
+	sign = '\0';
+      if (ch == 'a' || ch == 'A')
+	{
+	  union U { long long l; double d; } u;
+	  int dsgn;
+	  u.d = d;
+	  if (u.l < 0)
+	    {
+	      dsgn = 1;
+	      u.l &= 0x7fffffffffffffffLL;
+	    }
+	  else
+	    dsgn = 0;
+	  if (__builtin_isinf (d))
+	    {
+	      cp = "INF";
+	      size = 3;
+	    }
+	  else if (__builtin_isnan (d))
+	    {
+	      cp = "NAN";
+	      size = 3;
+	    }
+	  else if (d == 0)
+	    {
+	      cp = "ZERO";
+	      size = 4;
+	    }
+	  else
+	    {
+	      cp = "WRONG";
+	      size = 5;
+	    }
+	  bar (cp, size, sign, dsgn);
+	}
+    }
+}
+
+int
+main ()
+{
+  foo (42, 'a', -0.0);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-2.c b/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-2.c
new file mode 100644
index 00000000000..f1b13c9060a
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-2.c
@@ -0,0 +1,23 @@
+/* PR tree-optimization/108540 */
+
+__attribute__((noipa)) int
+foo (int x, double d)
+{
+  if (x == 42)
+    d = -0.0;
+  if (d == 0.0)
+    return 42;
+  return 12;
+}
+
+int
+main ()
+{
+  if (foo (42, 5.0) != 42
+      || foo (42, 0.0) != 42
+      || foo (42, -0.0) != 42
+      || foo (10, 5.0) != 12
+      || foo (10, 0.0) != 42
+      || foo (10, -0.0) != 42)
+    __builtin_abort ();
+}

                 reply	other threads:[~2023-01-26 16:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230126162832.C3CDE3858C83@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@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).