diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 698277a178861ec5263572ca3c232f0f8f79298b..b38e59a546201b8ffdcd5317cd80cccc668e76da 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2017-01-03  Jakub Jelinek  <jakub@redhat.com>
+
+	* genmatch.c (dt_node::gen_kids_1): If generic_exprs include SSA_NAME
+	and exprs_len || fns_len, emit the code for SSA_NAME next to the exprs
+	and fns handling, rather than in a separate case SSA_NAME.
+
 2017-01-02  Jeff Law  <law@redhat.com>
 
 	* config/darwin-driver.c (darwin_driver_init): Const-correctness
diff --git a/gcc/genmatch.c b/gcc/genmatch.c
index 86f40b23225a6402ac81431714baf6603c5a30d4..c163ded68764a4a0e6623774c0a22f2523673683 100644
--- a/gcc/genmatch.c
+++ b/gcc/genmatch.c
@@ -2913,6 +2913,20 @@ dt_node::gen_kids_1 (FILE *f, int indent, bool gimple,
 
       indent -= 6;
       fprintf_indent (f, indent, "    }\n");
+      /* See if there is SSA_NAME among generic_exprs and if yes, emit it
+	 here rather than in the next loop.  */
+      for (unsigned i = 0; i < generic_exprs.length (); ++i)
+	{
+	  expr *e = as_a <expr *>(generic_exprs[i]->op);
+	  id_base *op = e->operation;
+	  if (*op == SSA_NAME && (exprs_len || fns_len))
+	    {
+	      fprintf_indent (f, indent + 4, "{\n");
+	      generic_exprs[i]->gen (f, indent + 6, gimple);
+	      fprintf_indent (f, indent + 4, "}\n");
+	    }
+	}
+
       fprintf_indent (f, indent, "  break;\n");
     }
 
@@ -2922,6 +2936,9 @@ dt_node::gen_kids_1 (FILE *f, int indent, bool gimple,
       id_base *op = e->operation;
       if (*op == CONVERT_EXPR || *op == NOP_EXPR)
 	fprintf_indent (f, indent, "CASE_CONVERT:\n");
+      else if (*op == SSA_NAME && (exprs_len || fns_len))
+	/* Already handled above.  */
+	continue;
       else
 	fprintf_indent (f, indent, "case %s:\n", op->id);
       fprintf_indent (f, indent, "  {\n");