Index: gcc/tree-vect-loop.c
===================================================================
--- gcc/tree-vect-loop.c	2018-06-18 15:24:10.457472575 +0100
+++ gcc/tree-vect-loop.c	2018-06-18 15:24:21.001379616 +0100
@@ -224,10 +224,10 @@ vect_determine_vf_for_stmt (stmt_vec_inf
   if (STMT_VINFO_IN_PATTERN_P (stmt_info)
       && STMT_VINFO_RELATED_STMT (stmt_info))
     {
+      gimple *pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
       stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
 
       /* If a pattern statement has def stmts, analyze them too.  */
-      gimple *pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
       for (gimple_stmt_iterator si = gsi_start (pattern_def_seq);
 	   !gsi_end_p (si); gsi_next (&si))
 	{
@@ -2259,10 +2259,10 @@ vect_analyze_loop_2 (loop_vec_info loop_
 	  STMT_SLP_TYPE (stmt_info) = loop_vect;
 	  if (STMT_VINFO_IN_PATTERN_P (stmt_info))
 	    {
+	      gimple *pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
 	      stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
 	      STMT_SLP_TYPE (stmt_info) = loop_vect;
-	      for (gimple_stmt_iterator pi
-		     = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
+	      for (gimple_stmt_iterator pi = gsi_start (pattern_def_seq);
 		   !gsi_end_p (pi); gsi_next (&pi))
 		{
 		  gimple *pstmt = gsi_stmt (pi);
Index: gcc/tree-vect-stmts.c
===================================================================
--- gcc/tree-vect-stmts.c	2018-06-18 15:24:10.461472540 +0100
+++ gcc/tree-vect-stmts.c	2018-06-18 15:24:21.005379580 +0100
@@ -9892,29 +9892,26 @@ free_stmt_vec_info (gimple *stmt)
      too.  */
   if (STMT_VINFO_IN_PATTERN_P (stmt_info))
     {
+      if (gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info))
+	for (gimple_stmt_iterator si = gsi_start (seq);
+	     !gsi_end_p (si); gsi_next (&si))
+	  {
+	    gimple *seq_stmt = gsi_stmt (si);
+	    gimple_set_bb (seq_stmt, NULL);
+	    tree lhs = gimple_get_lhs (seq_stmt);
+	    if (lhs && TREE_CODE (lhs) == SSA_NAME)
+	      release_ssa_name (lhs);
+	    free_stmt_vec_info (seq_stmt);
+	  }
       stmt_vec_info patt_info
 	= vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
       if (patt_info)
 	{
-	  gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
 	  gimple *patt_stmt = STMT_VINFO_STMT (patt_info);
 	  gimple_set_bb (patt_stmt, NULL);
 	  tree lhs = gimple_get_lhs (patt_stmt);
 	  if (lhs && TREE_CODE (lhs) == SSA_NAME)
 	    release_ssa_name (lhs);
-	  if (seq)
-	    {
-	      gimple_stmt_iterator si;
-	      for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
-		{
-		  gimple *seq_stmt = gsi_stmt (si);
-		  gimple_set_bb (seq_stmt, NULL);
-		  lhs = gimple_get_lhs (seq_stmt);
-		  if (lhs && TREE_CODE (lhs) == SSA_NAME)
-		    release_ssa_name (lhs);
-		  free_stmt_vec_info (seq_stmt);
-		}
-	    }
 	  free_stmt_vec_info (patt_stmt);
 	}
     }
Index: gcc/tree-vect-patterns.c
===================================================================
--- gcc/tree-vect-patterns.c	2018-06-18 15:22:36.898297310 +0100
+++ gcc/tree-vect-patterns.c	2018-06-18 15:24:21.001379616 +0100
@@ -405,13 +405,13 @@ vect_recog_dot_prod_pattern (vec<gimple
       stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
       if (gimple_assign_rhs_code (stmt) != WIDEN_MULT_EXPR)
         return NULL;
+      STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (last_stmt))
+	= STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo);
       stmt_vinfo = vinfo_for_stmt (stmt);
       gcc_assert (stmt_vinfo);
       gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_internal_def);
       oprnd00 = gimple_assign_rhs1 (stmt);
       oprnd01 = gimple_assign_rhs2 (stmt);
-      STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (last_stmt))
-	  = STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo);
     }
   else
     {
@@ -4433,28 +4433,23 @@ vect_mark_pattern_stmts (gimple *orig_st
   STMT_VINFO_VECTYPE (pattern_stmt_info) = pattern_vectype;
   STMT_VINFO_IN_PATTERN_P (orig_stmt_info) = true;
   STMT_VINFO_RELATED_STMT (orig_stmt_info) = pattern_stmt;
-  STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info)
-    = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info);
-  if (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info))
-    {
-      gimple_stmt_iterator si;
-      for (si = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info));
-	   !gsi_end_p (si); gsi_next (&si))
-	{
-	  def_stmt = gsi_stmt (si);
-	  def_stmt_info = vinfo_for_stmt (def_stmt);
-	  if (def_stmt_info == NULL)
-	    {
-	      def_stmt_info = new_stmt_vec_info (def_stmt, vinfo);
-	      set_vinfo_for_stmt (def_stmt, def_stmt_info);
-	    }
-	  gimple_set_bb (def_stmt, gimple_bb (orig_stmt));
-	  STMT_VINFO_RELATED_STMT (def_stmt_info) = orig_stmt;
-	  STMT_VINFO_DEF_TYPE (def_stmt_info) = vect_internal_def;
-	  if (STMT_VINFO_VECTYPE (def_stmt_info) == NULL_TREE)
-	    STMT_VINFO_VECTYPE (def_stmt_info) = pattern_vectype;
-	}
-    }
+  if (gimple *def_seq = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info))
+    for (gimple_stmt_iterator si = gsi_start (def_seq);
+	 !gsi_end_p (si); gsi_next (&si))
+      {
+	def_stmt = gsi_stmt (si);
+	def_stmt_info = vinfo_for_stmt (def_stmt);
+	if (def_stmt_info == NULL)
+	  {
+	    def_stmt_info = new_stmt_vec_info (def_stmt, vinfo);
+	    set_vinfo_for_stmt (def_stmt, def_stmt_info);
+	  }
+	gimple_set_bb (def_stmt, gimple_bb (orig_stmt));
+	STMT_VINFO_RELATED_STMT (def_stmt_info) = orig_stmt;
+	STMT_VINFO_DEF_TYPE (def_stmt_info) = vect_internal_def;
+	if (STMT_VINFO_VECTYPE (def_stmt_info) == NULL_TREE)
+	  STMT_VINFO_VECTYPE (def_stmt_info) = pattern_vectype;
+      }
 }
 
 /* Function vect_pattern_recog_1
