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.  Adding -0.0 never
changes a value, even when x * y is a signed zero, so fma (x, y, -0.0)
can be folded to x * y.  In each case this saves the fma and loading
the constant.

fma (x, y, +0.0) is not folded, because when x * y is -0.0 the fma
gives +0.0.

        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.c: New test.
        * gcc.dg/tree-ssa/pr126696.c: New 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: unlike x * 1.0 -> x, these folds
 keep an arithmetic operation, so a signalling NaN operand is still
 quieted and still raises invalid, as with the fma.  I'm happy to add
 one if that's preferred.

 gcc/match.pd                             | 17 ++++++++++
 gcc/testsuite/gcc.dg/pr126696.c          | 42 ++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr126696.c | 18 ++++++++++
 3 files changed, 77 insertions(+)
 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..e35b0020564 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -11201,6 +11201,23 @@ 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, fma (x, -1.0, y) -> y - x and
+   fma (x, y, -0.0) -> x * y.  Multiplying by +-1.0 and adding -0.0
+   are exact, so the single rounding of the fma is the rounding of
+   the remaining operation.  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_CST@2)
+  (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2)))
+   (mult @0 @1))))
+
 (if (canonicalize_math_after_vectorization_p ())
  (for fmas (FMA)
   (simplify
diff --git a/gcc/testsuite/gcc.dg/pr126696.c b/gcc/testsuite/gcc.dg/pr126696.c
new file mode 100644
index 00000000000..b044a024278
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr126696.c
@@ -0,0 +1,42 @@
+/* PR tree-optimization/126696 */
+/* { dg-do run } */
+/* { 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..e70e735b8e0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126696.c
@@ -0,0 +1,18 @@
+/* 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

Reply via email to