Re: [committed] frange: Fix up foperator_{,not_}equal::fold_range for signed zeros [PR108540]

2023-01-26 Thread Gerald Pfeifer
On Thu, 26 Jan 2023, Jakub Jelinek via Gcc-patches wrote:
> committed to trunk.

Picking a random commit: Should we consistently start using "pushed" 
instead of "committed"?

In CVS and SVN those two were the same. With GIT it's really a push (to 
trunk) we are interested in, not a commit to a local clone, isn't it?

Gerald (who just updated his scripts this week :-)


[committed] frange: Fix up foperator_{,not_}equal::fold_range for signed zeros [PR108540]

2023-01-26 Thread Jakub Jelinek via Gcc-patches
Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, approved in the
PR by Aldy, committed to trunk.

2023-01-26  Jakub Jelinek  

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.

--- gcc/range-op-float.cc.jj2023-01-16 09:39:36.191929750 +0100
+++ gcc/range-op-float.cc   2023-01-26 13:44:10.542899269 +0100
@@ -607,6 +607,10 @@ foperator_equal::fold_range (irange ,
 {
   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 (_bound ())
+  && real_iszero (_bound ()))
+   r = range_true (type);
   else
r = range_false (type);
 }
@@ -617,7 +621,18 @@ foperator_equal::fold_range (irange ,
   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 (_bound ())
+  && real_iszero (_bound ()))
+ || (real_iszero (_bound ())
+ && real_iszero (_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
   // 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 (_bound ())
+  && real_iszero (_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
   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 (_bound ())
+  && real_iszero (_bound ()))
+ || (real_iszero (_bound ())
+ && real_iszero (_bound (
+   r = range_true_and_false (type);
+ else
+   r = range_true (type);
+   }
   else
r = range_true_and_false (type);
 }
--- gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c.jj2023-01-26 
13:54:55.131463151 +0100
+++ gcc/testsuite/gcc.c-torture/execute/ieee/pr108540-1.c   2023-01-26 
13:54:31.333811525 +0100
@@ -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