fma (x, 1.0, y) can be folded to x + y, because multiplying by 1.0 is
exact, so the fma rounds the same way as the addition. For the same
reason fma (x, -1.0, y) can be folded to y - x.
fma (x, y, 0.0) can be folded to x * y when adding that zero can be
dropped, which fold_real_zero_addition_p decides: -0.0 by default, and
either zero with -fno-signed-zeros. It is not folded with
-frounding-math, because +0.0 + -0.0 is -0.0 when rounding towards
-inf, while x * y would be +0.0.
In each case this saves the fma and loading the constant.
PR tree-optimization/126696
gcc/ChangeLog:
* match.pd (fma (x, 1.0, y) -> x + y): New simplification.
(fma (x, -1.0, y) -> y - x): Likewise.
(fma (x, y, 0.0) -> x * y): Likewise.
gcc/testsuite/ChangeLog:
* gcc.dg/pr126696-2.c: New test.
* gcc.dg/pr126696.c: New test.
* gcc.dg/tree-ssa/pr126696.c: New test.
---
Changes since v1:
- Use fold_real_zero_addition_p for the zero addend, so it is not
folded with -frounding-math, and +0.0 is folded with
-fno-signed-zeros.
- Add gcc.dg/pr126696-2.c, an FE_DOWNWARD run test.
Bootstrapped and regression tested on x86_64-pc-linux-gnu with no new
failures. I also checked the aarch64 compile-only tests with a cross
compiler.
I didn't add a HONOR_SNANS check to the +-1.0 folds: unlike
x * 1.0 -> x, they keep an arithmetic operation, so a signalling NaN is
still quieted and still raises invalid, as with the fma. The zero case
uses fold_real_zero_addition_p, so it is also not done with
-fsignaling-nans, which is slightly more conservative than needed.
gcc/match.pd | 18 ++++++++++
gcc/testsuite/gcc.dg/pr126696-2.c | 26 ++++++++++++++
gcc/testsuite/gcc.dg/pr126696.c | 43 ++++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr126696.c | 22 ++++++++++++
4 files changed, 109 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/pr126696-2.c
create mode 100644 gcc/testsuite/gcc.dg/pr126696.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126696.c
diff --git a/gcc/match.pd b/gcc/match.pd
index d1eeb283bcb..9496b56470b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -11201,6 +11201,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
{ vec_perm_indices_to_tree (build_vector_type (ssizetype, nunits),
sel); })))))
+/* fma (x, 1.0, y) -> x + y and fma (x, -1.0, y) -> y - x: multiplying
+ by +-1.0 is exact, so the single rounding of the fma is the rounding
+ of the addition or subtraction. fma (x, y, 0.0) -> x * y when adding
+ that zero can be dropped; with -frounding-math, +0.0 + -0.0 is -0.0
+ when rounding towards -inf, so use fold_real_zero_addition_p. A
+ constant multiplicand is canonicalized to the second operand. */
+(for fmas (FMA)
+ (simplify
+ (fmas @0 real_onep @1)
+ (plus @0 @1))
+ (simplify
+ (fmas @0 real_minus_onep @1)
+ (minus @1 @0))
+ (simplify
+ (fmas @0 @1 real_zerop@2)
+ (if (fold_real_zero_addition_p (type, NULL_TREE, @2, 0))
+ (mult @0 @1))))
+
(if (canonicalize_math_after_vectorization_p ())
(for fmas (FMA)
(simplify
diff --git a/gcc/testsuite/gcc.dg/pr126696-2.c
b/gcc/testsuite/gcc.dg/pr126696-2.c
new file mode 100644
index 00000000000..77aae807710
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr126696-2.c
@@ -0,0 +1,26 @@
+/* PR tree-optimization/126696 */
+/* { dg-do run } */
+/* { dg-require-effective-target c99_runtime } */
+/* { dg-require-effective-target fenv } */
+/* { dg-require-effective-target hard_float } */
+/* { dg-options "-O2 -frounding-math" } */
+
+#include <fenv.h>
+
+__attribute__((noipa)) double
+mulnz (double x, double y)
+{
+ return __builtin_fma (x, y, -0.0);
+}
+
+int
+main ()
+{
+#ifdef FE_DOWNWARD
+ fesetround (FE_DOWNWARD);
+ /* +0.0 + -0.0 is -0.0 when rounding towards -inf. */
+ if (!__builtin_signbit (mulnz (1.0, 0.0)))
+ __builtin_abort ();
+#endif
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/pr126696.c b/gcc/testsuite/gcc.dg/pr126696.c
new file mode 100644
index 00000000000..667a1ee79fd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr126696.c
@@ -0,0 +1,43 @@
+/* PR tree-optimization/126696 */
+/* { dg-do run } */
+/* { dg-require-effective-target c99_runtime } */
+/* { dg-options "-O2" } */
+
+__attribute__((noipa)) double
+add1 (double x, double y)
+{
+ return __builtin_fma (x, 1.0, y);
+}
+
+__attribute__((noipa)) double
+subm1 (double x, double y)
+{
+ return __builtin_fma (x, -1.0, y);
+}
+
+__attribute__((noipa)) double
+mulnz (double x, double y)
+{
+ return __builtin_fma (x, y, -0.0);
+}
+
+int
+main ()
+{
+ if (add1 (2.0, 3.0) != 5.0
+ || subm1 (2.0, 3.0) != 1.0
+ || mulnz (2.0, 3.0) != 6.0)
+ __builtin_abort ();
+
+ /* -0.0 * 1.0 + 0.0 is +0.0. */
+ if (__builtin_signbit (add1 (-0.0, 0.0)))
+ __builtin_abort ();
+ /* 0.0 * -1.0 + 0.0 is +0.0. */
+ if (__builtin_signbit (subm1 (0.0, 0.0)))
+ __builtin_abort ();
+ /* -1.0 * 0.0 + -0.0 is -0.0, and 1.0 * 0.0 + -0.0 is +0.0. */
+ if (!__builtin_signbit (mulnz (-1.0, 0.0))
+ || __builtin_signbit (mulnz (1.0, 0.0)))
+ __builtin_abort ();
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126696.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr126696.c
new file mode 100644
index 00000000000..087e47af6c7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126696.c
@@ -0,0 +1,22 @@
+/* PR tree-optimization/126696 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+double f1 (double x, double y) { return __builtin_fma (x, 1.0, y); }
+double f2 (double x, double y) { return __builtin_fma (1.0, x, y); }
+double f3 (double x, double y) { return __builtin_fma (x, -1.0, y); }
+double f4 (double x, double y) { return __builtin_fma (x, y, -0.0); }
+float f5 (float x, float y) { return __builtin_fmaf (x, 1.0f, y); }
+long double
+f6 (long double x, long double y)
+{
+ return __builtin_fmal (x, 1.0L, y);
+}
+
+/* fma (x, y, +0.0) is not x * y when x * y is -0.0, so it must stay. */
+double g1 (double x, double y) { return __builtin_fma (x, y, 0.0); }
+
+/* { dg-final { scan-tree-dump-times "__builtin_fma" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " \\+ " 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " - " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " \\* " 1 "optimized" } } */
--
2.43.0