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.
This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check no new failures. Ok for mainline? 2022-02-08 Roger Sayle <ro...@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. Thanks in advance (and sorry for the breakage/thinko). Roger --
diff --git a/gcc/match.pd b/gcc/match.pd index 7bbb801..4fe5909 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -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. diff --git a/gcc/testsuite/gcc.dg/pr104420-1.c b/gcc/testsuite/gcc.dg/pr104420-1.c new file mode 100644 index 0000000..48385fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-1.c @@ -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" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-2.c b/gcc/testsuite/gcc.dg/pr104420-2.c new file mode 100644 index 0000000..49d0189 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-2.c @@ -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" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-3.c b/gcc/testsuite/gcc.dg/pr104420-3.c new file mode 100644 index 0000000..962dfff --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-3.c @@ -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" } } */ diff --git a/gcc/testsuite/gcc.dg/pr104420-4.c b/gcc/testsuite/gcc.dg/pr104420-4.c new file mode 100644 index 0000000..95ed0cc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104420-4.c @@ -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" } } */ diff --git a/gcc/testsuite/gcc.dg/pr96392.c b/gcc/testsuite/gcc.dg/pr96392.c index 662bacb..fb7de21 100644 --- a/gcc/testsuite/gcc.dg/pr96392.c +++ b/gcc/testsuite/gcc.dg/pr96392.c @@ -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" } } */