diff --git a/gcc/match.pd b/gcc/match.pd index 97399e580a4..8b44b5cc92c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -229,13 +229,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify x - x. This is unsafe for certain floats even in non-IEEE formats. In IEEE, it is unsafe because it does wrong for NaNs. + PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD. Also note that operand_equal_p is always false if an operand is volatile. */ (simplify (minus @0 @0) (if (!FLOAT_TYPE_P (type) || (!tree_expr_maybe_nan_p (@0) - && !tree_expr_maybe_infinite_p (@0))) + && !tree_expr_maybe_infinite_p (@0) + && (!HONOR_SIGN_DEPENDENT_ROUNDING (type) + || !HONOR_SIGNED_ZEROS (type)))) { build_zero_cst (type); })) (simplify (pointer_diff @@0 @0) diff --git a/gcc/testsuite/gcc.dg/pr98420.c b/gcc/testsuite/gcc.dg/pr98420.c new file mode 100644 index 00000000000..c289b845d63 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr98420.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -ffinite-math-only -frounding-math -fdump-tree-optimized" } */ +double foo (double a) +{ + return a - a; +} + +/* { dg-final { scan-tree-dump " = a_\[0-9\]\\(D\\) - a_\[0-9\]\\(D\\);" "optimized" } } */