Disable some patterns for fold-left reductions (PR 83965)
authorRichard Sandiford <richard.sandiford@linaro.org>
Tue, 23 Jan 2018 10:42:10 +0000 (10:42 +0000)
committerRichard Sandiford <rsandifo@gcc.gnu.org>
Tue, 23 Jan 2018 10:42:10 +0000 (10:42 +0000)
In this PR we recognised a PLUS_EXPR as a fold-left reduction,
then applied pattern matching to convert it to a WIDEN_SUM_EXPR.
We need to keep the original code in this case since we implement
the reduction using scalar rather than vector operations.

2018-01-23  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
PR tree-optimization/83965
* tree-vect-patterns.c (vect_reassociating_reduction_p): New function.
(vect_recog_dot_prod_pattern, vect_recog_sad_pattern): Use it
instead of checking only for a reduction.
(vect_recog_widen_sum_pattern): Likewise.

gcc/testsuite/
PR tree-optimization/83965
* gcc.dg/vect/pr83965.c: New test.

From-SVN: r256976

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/vect/pr83965.c [new file with mode: 0644]
gcc/tree-vect-patterns.c

index 747c3b48ab9fc185a19583792250aca63ba29410..bf5743b4dc47bd0fdda53d1985457a2b39b18247 100644 (file)
@@ -1,3 +1,11 @@
+2018-01-23  Richard Sandiford  <richard.sandiford@linaro.org>
+
+       PR tree-optimization/83965
+       * tree-vect-patterns.c (vect_reassociating_reduction_p): New function.
+       (vect_recog_dot_prod_pattern, vect_recog_sad_pattern): Use it
+       instead of checking only for a reduction.
+       (vect_recog_widen_sum_pattern): Likewise.
+
 2018-01-23  Jan Hubicka  <hubicka@ucw.cz>
 
        * predict.c (probably_never_executed): Only use precise profile info.
index 760422aee58fca75dfcae07e6a6b72c70d74d69c..b42031bbd65101c8ffeba41a39249911b7d3ad45 100644 (file)
@@ -1,3 +1,8 @@
+2018-01-23  Richard Sandiford  <richard.sandiford@linaro.org>
+
+       PR tree-optimization/83965
+       * gcc.dg/vect/pr83965.c: New test.
+
 2018-01-23  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/83963
diff --git a/gcc/testsuite/gcc.dg/vect/pr83965.c b/gcc/testsuite/gcc.dg/vect/pr83965.c
new file mode 100644 (file)
index 0000000..5bf0f90
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-ftrapv" } */
+
+int
+mac (const short *a, const short *b, int sqr, int *sum)
+{
+  int i;
+  int dotp = *sum;
+
+  for (i = 0; i < 150; i++)
+    {
+      dotp += b[i] * a[i];
+      sqr += b[i] * b[i];
+    }
+
+  *sum = dotp;
+  return sqr;
+}
index 0831b7e6978b4655df84b62aed55e23ce174cf03..1279352125df827c0c4bdb2cbff495e92e2b7f54 100644 (file)
@@ -217,6 +217,16 @@ vect_recog_temp_ssa_var (tree type, gimple *stmt)
   return make_temp_ssa_name (type, stmt, "patt");
 }
 
+/* Return true if STMT_VINFO describes a reduction for which reassociation
+   is allowed.  */
+
+static bool
+vect_reassociating_reduction_p (stmt_vec_info stmt_vinfo)
+{
+  return (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
+         && STMT_VINFO_REDUC_TYPE (stmt_vinfo) != FOLD_LEFT_REDUCTION);
+}
+
 /* Function vect_recog_dot_prod_pattern
 
    Try to find the following pattern:
@@ -336,7 +346,7 @@ vect_recog_dot_prod_pattern (vec<gimple *> *stmts, tree *type_in,
     {
       gimple *def_stmt;
 
-      if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def
+      if (!vect_reassociating_reduction_p (stmt_vinfo)
          && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo))
        return NULL;
       oprnd0 = gimple_assign_rhs1 (last_stmt);
@@ -557,7 +567,7 @@ vect_recog_sad_pattern (vec<gimple *> *stmts, tree *type_in,
     {
       gimple *def_stmt;
 
-      if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def
+      if (!vect_reassociating_reduction_p (stmt_vinfo)
          && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo))
        return NULL;
       plus_oprnd0 = gimple_assign_rhs1 (last_stmt);
@@ -1181,7 +1191,7 @@ vect_recog_widen_sum_pattern (vec<gimple *> *stmts, tree *type_in,
   if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR)
     return NULL;
 
-  if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def
+  if (!vect_reassociating_reduction_p (stmt_vinfo)
       && ! STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_vinfo))
     return NULL;