Pattern recognition can leave stale STMT_VINFO_RELATED_STMT entries
around which confuses the correctness asserting I put into
get_{earlier,later}_stmt.  The following makes sure to clear
those again.  To make things more complicated pattern recognition
sometimes scans forward ... which runs into the issue that a later
attempt to detect a pattern might overwrite an earlier pattern.
The patch disallows that as well.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

I've just noticed a larger series from Richard S. and hope this
doesn't interfere.

Richard.

>From 3537753ed4924b2220b9dd51023edb6c0c5fc3b4 Mon Sep 17 00:00:00 2001
From: Richard Guenther <rguent...@suse.de>
Date: Mon, 18 Jun 2018 10:53:35 +0200
Subject: [PATCH] fix-pr86179

2018-06-19  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/86179
        * tree-vect-patterns.c (vect_pattern_recog_1): Clean up
        after failed recognition.

        * gcc.dg/pr86179.c: New testcase.

diff --git a/gcc/testsuite/gcc.dg/pr86179.c b/gcc/testsuite/gcc.dg/pr86179.c
new file mode 100644
index 00000000000..6a90f09e6e8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr86179.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+
+void c(int *d, char *g)
+{
+  char *a, *b, *e;
+  int f;
+  for (; f; f -= 8) {
+      *d++ = *e++ | (unsigned)*g++ << 8 | (unsigned)*b++ << 16 |
+         (unsigned)*a++ << 24;
+      *d++ = *e++ | (unsigned)*g++ << 8 | (unsigned)*b++ << 16 |
+         (unsigned)*a++ << 24;
+  }
+}
diff --git a/gcc/tree-vect-patterns.c b/gcc/tree-vect-patterns.c
index 6621392b7e8..1eb3afa9542 100644
--- a/gcc/tree-vect-patterns.c
+++ b/gcc/tree-vect-patterns.c
@@ -4496,7 +4496,18 @@ vect_pattern_recog_1 (vect_recog_func *recog_func,
   stmts_to_replace->quick_push (stmt);
   pattern_stmt = recog_func->fn (stmts_to_replace, &type_in, &type_out);
   if (!pattern_stmt)
-    return false;
+    {
+      /* Clear related stmt info that analysis might have noted for
+         to be replaced stmts.  */
+      for (i = 0; stmts_to_replace->iterate (i, &stmt)
+          && (unsigned) i < stmts_to_replace->length ();
+          i++)
+       {
+         stmt_info = vinfo_for_stmt (stmt);
+         STMT_VINFO_RELATED_STMT (stmt_info) = NULL;
+       }
+      return false;
+    }
 
   stmt = stmts_to_replace->last ();
   stmt_info = vinfo_for_stmt (stmt);
@@ -4668,7 +4679,6 @@ vect_pattern_recog (vec_info *vinfo)
   gimple_stmt_iterator si;
   unsigned int i, j;
   auto_vec<gimple *, 1> stmts_to_replace;
-  gimple *stmt;
 
   if (dump_enabled_p ())
     dump_printf_loc (MSG_NOTE, vect_location,
@@ -4687,6 +4697,10 @@ vect_pattern_recog (vec_info *vinfo)
          basic_block bb = bbs[i];
          for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
            {
+             gimple *stmt = gsi_stmt (si);
+             stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
+             if (stmt_info && STMT_VINFO_IN_PATTERN_P (stmt_info))
+               continue;
              /* Scan over all generic vect_recog_xxx_pattern functions.  */
              for (j = 0; j < NUM_PATTERNS; j++)
                if (vect_pattern_recog_1 (&vect_vect_recog_func_ptrs[j], si,
@@ -4701,9 +4715,11 @@ vect_pattern_recog (vec_info *vinfo)
       for (si = bb_vinfo->region_begin;
           gsi_stmt (si) != gsi_stmt (bb_vinfo->region_end); gsi_next (&si))
        {
-         if ((stmt = gsi_stmt (si))
-             && vinfo_for_stmt (stmt)
-             && !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
+         gimple *stmt = gsi_stmt (si);
+         stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
+         if (stmt_info
+             && (!STMT_VINFO_VECTORIZABLE (stmt_info)
+                 || STMT_VINFO_IN_PATTERN_P (stmt_info)))
            continue;
 
          /* Scan over all generic vect_recog_xxx_pattern functions.  */

Reply via email to