Bug 64811 - fold_inf_compare bogus for NaNs (given -ftrapping-math)
Summary: fold_inf_compare bogus for NaNs (given -ftrapping-math)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2015-01-27 02:17 UTC by Joseph S. Myers
Modified: 2018-01-09 13:27 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph S. Myers 2015-01-27 02:17:52 UTC
Various optimizations in fold_inf_compare can cause "invalid" floating-point exceptions to be present when they should be absent, or vice versa.

Folding x > +Inf to 0 should not be about HONOR_SNANS - ordered comparisons of both quiet and signaling NaNs should raise invalid.  So if -ftrapping-math and HONOR_NANS, this is invalid (given a strict interpretation of -ftrapping-math, anyway).

x <= +Inf is not the same as x == x, because again that loses an exception (equality comparisons don't raise exceptions except for signaling NaNs).

x == +Inf is not the same as x > DBL_MAX, and a similar issue applies with the x != +Inf case - that transformation causes a spurious exception (in particular, I've seen this in __gcc_qadd for powerpc-linux-gnu soft-float, causing glibc testsuite failures, which is how I found this bug), not just a missing exception.  (The existing HONOR_NANS code for NE_EXPR is only about the result, not the exceptions raised.)
Comment 1 Joseph S. Myers 2018-01-09 13:26:09 UTC
Author: jsm28
Date: Tue Jan  9 13:25:38 2018
New Revision: 256380

URL: https://gcc.gnu.org/viewcvs?rev=256380&root=gcc&view=rev
Log:
Fix folding of Inf/NaN comparisons for -ftrapping-math (PR tree-optimization/64811).

The folding of comparisons against Inf (to constants or comparisons
with the maximum finite value) has various cases where it introduces
or loses "invalid" exceptions for comparisons with NaNs.

Folding x > +Inf to 0 should not be about HONOR_SNANS - ordered
comparisons of both quiet and signaling NaNs should raise invalid.

x <= +Inf is not the same as x == x, because again that loses an
exception (equality comparisons don't raise exceptions except for
signaling NaNs).

x == +Inf is not the same as x > DBL_MAX, and a similar issue applies
with the x != +Inf case - that transformation causes a spurious
exception.

This patch fixes the conditionals on the folding to avoid such
introducing or losing exceptions.

Bootstrapped with no regressions on x86_64-pc-linux-gnu (where the
cases involving spurious exceptions wouldn't have failed anyway before
GCC 8 because of unordered comparisons wrongly always having formerly
been used by the back end).  Also tested for powerpc-linux-gnu
soft-float that this fixes many glibc math/ test failures that arose
in that configuration because this folding affected the IBM long
double support in libgcc (no such failures appeared for hard-float
because of the bug of powerpc hard-float always using unordered
comparisons) - some failures remain, but I believe them to be
unrelated.

	PR tree-optimization/64811
gcc:
	* match.pd: When optimizing comparisons with Inf, avoid
	introducing or losing exceptions from comparisons with NaN.

gcc/testsuite:
	* gcc.dg/torture/inf-compare-1.c, gcc.dg/torture/inf-compare-2.c,
	gcc.dg/torture/inf-compare-3.c, gcc.dg/torture/inf-compare-4.c,
	gcc.dg/torture/inf-compare-5.c, gcc.dg/torture/inf-compare-6.c,
	gcc.dg/torture/inf-compare-7.c, gcc.dg/torture/inf-compare-8.c:
	New tests.
	* gcc.c-torture/execute/ieee/fp-cmp-7.x: New file.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-7.x
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-1.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-2.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-3.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-4.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-5.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-6.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-7.c
    trunk/gcc/testsuite/gcc.dg/torture/inf-compare-8.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
Comment 2 Joseph S. Myers 2018-01-09 13:27:46 UTC
Fixed for GCC 8.