https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126535

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #1 from liu zhenyu <lzy20000419 at outlook dot com> ---
Here is a second testcase. It needs no libm and fails on x86_64:

    __attribute__((noipa)) int
    f (double a, double b)
    {
      if (!__builtin_isgreaterequal (a, 0.0))   /* a < 0 or a is NaN */
        if (!__builtin_islessgreater (a, b))    /* a == b or unordered */
          return b >= 0.0;
      return -1;
    }
    int
    main (void)
    {
      volatile double a = __builtin_nan (""), b = 1.0;
      int r = f (a, b);
      __builtin_printf ("%d\n", r);
      return r != 1;
    }

    $ gcc -O2 t.c && ./a.out
    0

Expected 1: a is NaN, so a u== b is true for any b, and b = 1.0 >= 0. -O1 and
-O2 -fno-tree-vrp print 1. On x86_64 it prints 0 with 13.1.0, 14.2.0 and
15.2.0, and 1 with 12.5.0. On 16.2.0 and trunk this form happens not to
trigger, but the testcase in comment 0 aborts on x86_64 at -O2 -lm with 16.2.0
and 17.0.0 (20261113) and passes with 14.2.0 and at -O0. So the two tests
together cover 13 through trunk.

Godbolt (this test, 14.2 and trunk): https://godbolt.org/z/xdscdf6zz
Godbolt (comment 0 test on x86_64, trunk and 14.2):
https://godbolt.org/z/jKqqz3cs1

The cause is in gcc/range-op-float.cc, foperator_unordered_equal::op1_range, on
the BRS_TRUE side:

        case BRS_TRUE:
          // If it's true, the result is the same as OP2 plus a NAN.
          r = op2;
          frange_add_zeros (r, type);
          r.update_nan ();

That is only correct when OP2 cannot be a NaN. If OP2 may be a NaN, x u== y is
true for every x, so OP1 has to be VARYING. Here op2 = a = [-Inf, -0x0.8p-1073]
+-NAN on the true edge of a u< 0, so evrp gives b the same range, and b >= 0.0
folds to 0:

    3->4  (T) b_4(D) : [frange] double [-Inf,
-4.94065645841246544176568792868221372365059802614324764425e-324
(-0x0.8p-1073)] +-NAN

In the comment 0 test, y = sqrt (a) may be NaN, and x picks up y's range [0,
+Inf] on the false edge of LTGT. The other unordered operators
(unordered_lt/le/gt/ge::op1_range/op2_range) already handle this with if
(op2.maybe_isnan ()) r.set_varying (type);. foperator_ltgt::op1_range BRS_FALSE
has the same copy, and op2_range of both goes through op1_range.

Suggested fix (the same shape as foperator_unordered_lt):

         case BRS_TRUE:
    -      // If it's true, the result is the same as OP2 plus a NAN.
    -      r = op2;
    -      // Add both zeros if there's the possibility of zero equality.
    -      frange_add_zeros (r, type);
    -      // Add the possibility of a NAN.
    -      r.update_nan ();
    +      // If OP2 may be a NAN, the comparison is true for any OP1.
    +      if (op2.maybe_isnan ())
    +        r.set_varying (type);
    +      else
    +        {
    +          // Otherwise the result is the same as OP2 plus a NAN.
    +          r = op2;
    +          // Add both zeros if there's the possibility of zero equality.
    +          frange_add_zeros (r, type);
    +          // Add the possibility of a NAN.
    +          r.update_nan ();
    +        }
           break;

plus the same change in foperator_ltgt::op1_range BRS_FALSE.

I checked this only on a 14.2 build: the test above and the original program
(also wrong on sparc64 at -O3 -fno-signed-zeros) are fixed, and 52 FP-range
tests (gcc.dg/tree-ssa/vrp-float*, *nan*, ...) give identical -O2 asm. I could
not check the comment 0 test with the patch, because 14.2 does not miscompile
it. The full testsuite was not run.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to