[PATCH] PR tree-optimization/104420: Fix checks for constant folding X*0.0

This patch resolves PR tree-optimization/104420, which is a P1 regression
where, as observed by Jakub Jelinek, the conditions for constant folding
x*0.0 are incorrect (following my patch for PR tree-optimization/96392).
The multiplication x*0.0 may yield a negative zero result, -0.0, if X is
negative (not just if x may be negative zero).  Hence (without -ffast-math)
(int)x*0.0 can't be optimized to 0.0, but (unsigned)x*0.0 can be constant
folded.  This adds a bunch of test cases to confirm the desired behaviour,
and removes an incorrect test from gcc.dg/pr96392.c which checked for the
wrong behaviour.

2022-02-09  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	PR tree-optimization/104420
	* match.pd (mult @0 real_zerop): Tweak conditions for constant
	folding X*0.0 (or X*-0.0) to HONOR_SIGNED_ZEROS when appropriate.

gcc/testsuite/ChangeLog
	PR tree-optimization/104420
	* gcc.dg/pr104420-1.c: New test case.
	* gcc.dg/pr104420-2.c: New test case.
	* gcc.dg/pr104420-3.c: New test case.
	* gcc.dg/pr104420-4.c: New test case.
	* gcc.dg/pr96392.c: Remove incorrect test.
This commit is contained in:
Roger Sayle 2022-02-09 14:21:08 +00:00
parent be9cd0ca8a
commit 2d3c477599
6 changed files with 41 additions and 8 deletions

View File

@ -262,8 +262,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(mult @0 real_zerop@1)
(if (!tree_expr_maybe_nan_p (@0)
&& (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
&& !tree_expr_maybe_real_minus_zero_p (@0)
&& !tree_expr_maybe_real_minus_zero_p (@1))
&& (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
@1))
/* In IEEE floating point, x*1 is not equivalent to x for snans.

View File

@ -0,0 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
/* { dg-add-options ieee } */
double f(int a)
{
return a * 0.0;
}
/* { dg-final { scan-tree-dump " \\\* 0.0" "optimized" } } */

View File

@ -0,0 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
/* { dg-add-options ieee } */
double f(int a)
{
return a * -0.0;
}
/* { dg-final { scan-tree-dump " \\\* -0.0" "optimized" } } */

View File

@ -0,0 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
/* { dg-add-options ieee } */
double f(unsigned int a)
{
return a * 0.0;
}
/* { dg-final { scan-tree-dump "return 0.0" "optimized" } } */

View File

@ -0,0 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */
/* { dg-add-options ieee } */
double f(unsigned int a)
{
return a * -0.0;
}
/* { dg-final { scan-tree-dump "return -0.0" "optimized" } } */

View File

@ -12,11 +12,6 @@ double sub0(int x)
return x - 0.0;
}
double mult0(int x)
{
return 0.0 * x;
}
double negate(int x)
{
return 0.0 - x;
@ -29,5 +24,4 @@ double subtract(int x)
/* { dg-final { scan-tree-dump-not " \\+ " "optimized" } } */
/* { dg-final { scan-tree-dump-not " \\- " "optimized" } } */
/* { dg-final { scan-tree-dump-not " \\* " "optimized" } } */