From: Ezra Sitorus <[email protected]>

Add a new pattern to split up negative product pattern to make it easier
to match in further negative dot product matcher.

A previous attempt went like this:
(a*b - c*d) + acc -> a*b + (acc + (-b * c))
but this caused an infinite simplification loop during bootstrap build.

gcc/ChangeLog:
        * match.pd: Add new pattern.

gcc/testsuite/ChangeLog:
        * gcc.dg/tree-ssa/neg-dot-prod-1.c: Add new test.
---
 gcc/match.pd                                  |  8 ++++++++
 .../gcc.dg/tree-ssa/neg-dot-prod-1.c          | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-dot-prod-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 032f29dcbc25..185b4b7c6089 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -13407,3 +13407,11 @@ and,
  (if (INTEGRAL_TYPE_P (type))
    (with { tree itype = TREE_TYPE (@2); }
     (convert (minus @2 (convert:itype @1))))))
+
+(simplify
+ (plus:c (minus (mult:s @0 @1) (mult:s @2 @3)) @4)
+ (if (INTEGRAL_TYPE_P (type)
+     && !TYPE_OVERFLOW_TRAPS (type)
+     && !TYPE_OVERFLOW_SANITIZED (type)
+     && !TYPE_SATURATING (type))
+   (minus (plus @4 (mult @0 @1)) (mult @2 @3))))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-dot-prod-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/neg-dot-prod-1.c
new file mode 100644
index 000000000000..289312ddafc7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-dot-prod-1.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-gimple" } */
+
+int
+f1 (int acc, int a, int b, int c, int d)
+{
+  return acc + (a * b - c * d);
+}
+
+int
+f2 (int acc, int a, int b, int c, int d)
+{
+  return (a * b - c * d) + acc;
+}
+
+/* The pattern in match.pd should convert the above to
+   (acc + a*b) - c*d  */
+/* { dg-final { scan-tree-dump
+   "_[0-9]+ = acc(_[0-9]+)? \\+ _[0-9]+;" 2 "gimple" } } */
-- 
2.53.0

Reply via email to