diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index a56bd90cb2c..e0ec1c0e928 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -3518,43 +3518,36 @@ dt_operand::gen_phi_on_cond (FILE *f, int indent, int depth)
 {
   fprintf_indent (f, indent,
     "basic_block _b%d = gimple_bb (_a%d);\n", depth, depth);
+  fprintf_indent (f, indent, "basic_block _b_cond_%d;\n", depth);
 
-  fprintf_indent (f, indent, "if (gimple_phi_num_args (_a%d) == 2)\n", depth);
+  fprintf_indent (f, indent, "if (gimple_phi_num_args (_a%d) == 2\n", depth);
 
-  indent += 2;
-  fprintf_indent (f, indent, "{\n");
   indent += 2;
 
   fprintf_indent (f, indent,
-    "basic_block _pb_0_%d = EDGE_PRED (_b%d, 0)->src;\n", depth, depth);
-  fprintf_indent (f, indent,
-    "basic_block _pb_1_%d = EDGE_PRED (_b%d, 1)->src;\n", depth, depth);
-  fprintf_indent (f, indent,
-    "basic_block _db_%d = safe_dyn_cast <gcond *> (*gsi_last_bb (_pb_0_%d)) ? "
-    "_pb_0_%d : _pb_1_%d;\n", depth, depth, depth, depth);
+    "  && (control_flow_graph_case_0_match (_b%d, &_b_cond_%d)\n",
+    depth, depth);
   fprintf_indent (f, indent,
-    "basic_block _other_db_%d = safe_dyn_cast <gcond *> "
-    "(*gsi_last_bb (_pb_0_%d)) ? _pb_1_%d : _pb_0_%d;\n",
-    depth, depth, depth, depth);
+    "      || control_flow_graph_case_1_match (_b%d, &_b_cond_%d)))\n",
+    depth, depth);
+
+  indent += 2;
+  fprintf_indent (f, indent, "{\n");
+  indent += 2;
 
   fprintf_indent (f, indent,
-    "gcond *_ct_%d = safe_dyn_cast <gcond *> (*gsi_last_bb (_db_%d));\n",
+    "gcond *_cond_%d = safe_dyn_cast <gcond *> (*gsi_last_bb (_b_cond_%d));\n",
     depth, depth);
-  fprintf_indent (f, indent, "if (_ct_%d"
-    " && EDGE_COUNT (_other_db_%d->preds) == 1\n", depth, depth);
-  fprintf_indent (f, indent,
-    "  && EDGE_COUNT (_other_db_%d->succs) == 1\n", depth);
-  fprintf_indent (f, indent,
-    "  && EDGE_PRED (_other_db_%d, 0)->src == _db_%d)\n", depth, depth);
+  fprintf_indent (f, indent, "if (_cond_%d)\n", depth);
 
   indent += 2;
   fprintf_indent (f, indent, "{\n");
   indent += 2;
 
   fprintf_indent (f, indent,
-    "tree _cond_lhs_%d = gimple_cond_lhs (_ct_%d);\n", depth, depth);
+    "tree _cond_lhs_%d = gimple_cond_lhs (_cond_%d);\n", depth, depth);
   fprintf_indent (f, indent,
-    "tree _cond_rhs_%d = gimple_cond_rhs (_ct_%d);\n", depth, depth);
+    "tree _cond_rhs_%d = gimple_cond_rhs (_cond_%d);\n", depth, depth);
 
   char opname_0[20];
   char opname_1[20];
@@ -3562,7 +3555,7 @@ dt_operand::gen_phi_on_cond (FILE *f, int indent, int depth)
   gen_opname (opname_0, 0);
 
   fprintf_indent (f, indent,
-    "tree %s = build2 (gimple_cond_code (_ct_%d), "
+    "tree %s = build2 (gimple_cond_code (_cond_%d), "
     "boolean_type_node, _cond_lhs_%d, _cond_rhs_%d);\n",
     opname_0, depth, depth, depth);
 
diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index 924d3f1e710..eb43e5c58cb 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -375,3 +375,118 @@ gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp, tree (*va
     return true;
   return false;
 }
+
+/*
+ * Return TRUE if the cfg matches the below layout by the given b2 in
+ * the first argument.  Or return FALSE.
+ *
+ * If return TRUE, the output argument b_out will be updated to the b0
+ * block as below example.
+ *
+ * If return FALSE, the output argument b_out will be NULL_BLOCK.
+ *
+ *    |
+ *    |
+ *    v
+ * +------+
+ * | b0:  |
+ * | def  |       +-----+
+ * | ...  |       | b1: |
+ * | cond |------>| def |
+ * +------+       | ... |
+ *    |           +-----+
+ *    |              |
+ *    v              |
+ * +-----+           |
+ * | b2: |           |
+ * | def |<----------+
+ * +-----+
+ */
+static inline bool
+control_flow_graph_case_0_match (basic_block b2, basic_block *b_out)
+{
+  *b_out = NULL;
+
+  if (EDGE_COUNT (b2->preds) != 2)
+    return false;
+
+  basic_block pred_0 = EDGE_PRED (b2, 0)->src;
+  basic_block pred_1 = EDGE_PRED (b2, 1)->src;
+
+  if (pred_0 == NULL || pred_1 == NULL)
+    return false;
+
+  if (!(EDGE_COUNT (pred_0->succs) == 2 && EDGE_COUNT (pred_1->succs) == 1)
+     && !(EDGE_COUNT (pred_0->succs) == 1 && EDGE_COUNT (pred_1->succs) == 2))
+    return false;
+
+  basic_block b0 = EDGE_COUNT (pred_0->succs) == 2 ? pred_0 : pred_1;
+  basic_block b1 = EDGE_COUNT (pred_0->succs) == 1 ? pred_0 : pred_1;
+
+  if (EDGE_COUNT (b1->preds) != 1 || EDGE_PRED (b1, 0)->src != b0)
+    return false;
+
+  *b_out = b0;
+  return true;
+}
+
+/*
+ * Return TRUE if the cfg matches the below layout by the given b3 in
+ * the first argument.  Or return FALSE.
+ *
+ * If return TRUE, the output argument b_out will be updated to the b0
+ * block as below example.
+ *
+ * If return FALSE, the output argument b_out will be NULL.
+ *
+ *    |
+ *    |
+ *    v
+ * +------+
+ * | b0:  |
+ * | ...  |       +-----+
+ * | cond |------>| b2: |
+ * +------+       | ... |
+ *    |           +-----+
+ *    |              |
+ *    v              |
+ * +-----+           |
+ * | b1: |           |
+ * | ... |           |
+ * +-----+           |
+ *    |              |
+ *    |              |
+ *    v              |
+ * +-----+           |
+ * | b3: |<----------+
+ * | ... |
+ * +-----+
+ */
+static inline bool
+control_flow_graph_case_1_match (basic_block b3, basic_block *b_out)
+{
+  *b_out = NULL;
+
+  if (EDGE_COUNT (b3->preds) != 2)
+    return false;
+
+  basic_block b1 = EDGE_PRED (b3, 0)->src;
+  basic_block b2 = EDGE_PRED (b3, 1)->src;
+
+  if (b1 == NULL || b2 == NULL)
+    return false;
+
+  if (EDGE_COUNT (b1->succs) != 1
+      || EDGE_COUNT (b1->preds) != 1
+      || EDGE_COUNT (b2->succs) != 1
+      || EDGE_COUNT (b2->preds) != 1)
+    return false;
+
+  basic_block b0 = EDGE_PRED (b1, 0)->src;
+
+  if (EDGE_COUNT (b0->succs) != 2 || EDGE_PRED (b2, 0)->src != b0)
+    return false;
+
+  *b_out = b0;
+  return true;
+}
