diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 632a241a964..b76e80c02a3 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -12007,8 +12007,8 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, strict_overflow_p = false; if (code == GE_EXPR && (integer_zerop (arg1) - || (! HONOR_NANS (arg0) - && real_zerop (arg1))) + || (real_zerop (arg1) + && !tree_expr_maybe_nan_p (arg0))) && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) { if (strict_overflow_p) @@ -12024,7 +12024,10 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, /* Convert ABS_EXPR < 0 to false. */ strict_overflow_p = false; if (code == LT_EXPR - && (integer_zerop (arg1) || real_zerop (arg1)) + && (integer_zerop (arg1) + || (real_zerop (arg1) + && (!flag_trapping_math + || !tree_expr_maybe_nan_p (arg0)))) && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) { if (strict_overflow_p) diff --git a/gcc/match.pd b/gcc/match.pd index b6dfc24af88..ef1bfb42d12 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4037,7 +4037,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) /* x != NaN is always true, other ops are always false. */ (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) - && ! HONOR_SNANS (@1)) + && (!flag_trapping_math + || ((cmp == EQ_EXPR || cmp == NE_EXPR) + && !tree_expr_maybe_signaling_nan_p (@1) + && !tree_expr_maybe_signaling_nan_p (@0)))) { constant_boolean_node (cmp == NE_EXPR, type); }) /* Fold comparisons against infinity. */ (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index 47e7aebda8a..dd97a353bff 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -5732,20 +5732,31 @@ simplify_const_relational_operation (enum rtx_code code, if (REAL_VALUE_ISNAN (*d0) || REAL_VALUE_ISNAN (*d1)) switch (code) { + case NE: + if (flag_trapping_math + && (REAL_VALUE_ISSIGNALING_NAN (*d0) + || REAL_VALUE_ISSIGNALING_NAN (*d1))) + return 0; + return const_true_rtx; case UNEQ: case UNLT: case UNGT: case UNLE: case UNGE: - case NE: case UNORDERED: return const_true_rtx; case EQ: + if (flag_trapping_math + && (REAL_VALUE_ISSIGNALING_NAN (*d0) + || REAL_VALUE_ISSIGNALING_NAN (*d1))) + return 0; + return const0_rtx; case LT: case GT: case LE: case GE: case LTGT: + return flag_trapping_math ? 0 : const0_rtx; case ORDERED: return const0_rtx; default: