+ /* Find the candidate for the latch edge. */
+ for (i = 1; VEC_iterate (edge, latches, i, e); i++)
+ if (dominated_by_p (CDI_DOMINATORS, latch->src, e->src))
+ latch = e;
+
+ /* Verify that it dominates all the latch edges. */
+ for (i = 0; VEC_iterate (edge, latches, i, e); i++)
+ if (!dominated_by_p (CDI_DOMINATORS, e->src, latch->src))
+ return NULL;
+
+ /* Check for a phi node that would deny that this is a latch edge of
+ a subloop. */
+ for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
+ {
+ phi = gsi_stmt (psi);
+ lop = PHI_ARG_DEF_FROM_EDGE (phi, latch);
+
+ /* Ignore the values that are not changed inside the subloop. */
+ if (TREE_CODE (lop) != SSA_NAME
+ || SSA_NAME_DEF_STMT (lop) == phi)
+ continue;
+ bb = gimple_bb (SSA_NAME_DEF_STMT (lop));
+ if (!bb || !flow_bb_inside_loop_p (loop, bb))
+ continue;
+
+ for (i = 0; VEC_iterate (edge, latches, i, e); i++)
+ if (e != latch
+ && PHI_ARG_DEF_FROM_EDGE (phi, e) == lop)
+ return NULL;
+ }
+
+ if (dump_file)
+ fprintf (dump_file,
+ "Found latch edge %d -> %d using iv structure.\n",
+ latch->src->index, latch->dest->index);
+ return latch;
+}
+
+/* If we can determine that one of the several latch edges of LOOP behaves
+ as a latch edge of a separate subloop, returns this edge. Otherwise
+ returns NULL. */
+
+static edge
+find_subloop_latch_edge (struct loop *loop)
+{
+ VEC (edge, heap) *latches = get_loop_latch_edges (loop);
+ edge latch = NULL;
+
+ if (VEC_length (edge, latches) > 1)
+ {
+ latch = find_subloop_latch_edge_by_profile (latches);
+
+ if (!latch
+ /* We consider ivs to guess the latch edge only in SSA. Perhaps we
+ should use cfghook for this, but it is hard to imagine it would
+ be useful elsewhere. */
+ && current_ir_type () == IR_GIMPLE)
+ latch = find_subloop_latch_edge_by_ivs (loop, latches);
+ }
+
+ VEC_free (edge, heap, latches);
+ return latch;
+}
+
+/* Callback for make_forwarder_block. Returns true if the edge E is marked
+ in the set MFB_REIS_SET. */
+
+static struct pointer_set_t *mfb_reis_set;
+static bool
+mfb_redirect_edges_in_set (edge e)
+{
+ return pointer_set_contains (mfb_reis_set, e);
+}
+
+/* Creates a subloop of LOOP with latch edge LATCH. */
+
+static void
+form_subloop (struct loop *loop, edge latch)
+{
+ edge_iterator ei;
+ edge e, new_entry;
+ struct loop *new_loop;
+
+ mfb_reis_set = pointer_set_create ();
+ FOR_EACH_EDGE (e, ei, loop->header->preds)
+ {
+ if (e != latch)
+ pointer_set_insert (mfb_reis_set, e);
+ }
+ new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
+ NULL);
+ pointer_set_destroy (mfb_reis_set);
+
+ loop->header = new_entry->src;
+
+ /* Find the blocks and subloops that belong to the new loop, and add it to
+ the appropriate place in the loop tree. */
+ new_loop = alloc_loop ();
+ new_loop->header = new_entry->dest;
+ new_loop->latch = latch->src;
+ add_loop (new_loop, loop);
+}
+
+/* Make all the latch edges of LOOP to go to a single forwarder block --
+ a new latch of LOOP. */
+
+static void
+merge_latch_edges (struct loop *loop)
+{
+ VEC (edge, heap) *latches = get_loop_latch_edges (loop);
+ edge latch, e;
+ unsigned i;
+
+ gcc_assert (VEC_length (edge, latches) > 0);
+
+ if (VEC_length (edge, latches) == 1)
+ loop->latch = VEC_index (edge, latches, 0)->src;
+ else
+ {
+ if (dump_file)
+ fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
+
+ mfb_reis_set = pointer_set_create ();
+ for (i = 0; VEC_iterate (edge, latches, i, e); i++)
+ pointer_set_insert (mfb_reis_set, e);
+ latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
+ NULL);
+ pointer_set_destroy (mfb_reis_set);
+
+ loop->header = latch->dest;
+ loop->latch = latch->src;
+ }
+
+ VEC_free (edge, heap, latches);
+}
+
+/* LOOP may have several latch edges. Transform it into (possibly several)
+ loops with single latch edge. */
+
+static void
+disambiguate_multiple_latches (struct loop *loop)
+{
+ edge e;
+
+ /* We eliminate the multiple latches by splitting the header to the forwarder
+ block F and the rest R, and redirecting the edges. There are two cases:
+
+ 1) If there is a latch edge E that corresponds to a subloop (we guess
+ that based on profile -- if it is taken much more often than the
+ remaining edges; and on trees, using the information about induction
+ variables of the loops), we redirect E to R, all the remaining edges to
+ F, then rescan the loops and try again for the outer loop.
+ 2) If there is no such edge, we redirect all latch edges to F, and the
+ entry edges to R, thus making F the single latch of the loop. */
+
+ if (dump_file)
+ fprintf (dump_file, "Disambiguating loop %d with multiple latches\n",
+ loop->num);
+
+ /* During latch merging, we may need to redirect the entry edges to a new
+ block. This would cause problems if the entry edge was the one from the
+ entry block. To avoid having to handle this case specially, split
+ such entry edge. */
+ e = find_edge (ENTRY_BLOCK_PTR, loop->header);
+ if (e)
+ split_edge (e);
+
+ while (1)
+ {
+ e = find_subloop_latch_edge (loop);
+ if (!e)
+ break;
+
+ form_subloop (loop, e);
+ }
+
+ merge_latch_edges (loop);
+}
+
+/* Split loops with multiple latch edges. */
+
+void
+disambiguate_loops_with_multiple_latches (void)
+{
+ loop_iterator li;
+ struct loop *loop;
+
+ FOR_EACH_LOOP (li, loop, 0)
+ {
+ if (!loop->latch)
+ disambiguate_multiple_latches (loop);
+ }
+}
+
+/* Return nonzero if basic block BB belongs to LOOP. */
+bool
+flow_bb_inside_loop_p (const struct loop *loop, const_basic_block bb)
+{
+ struct loop *source_loop;
+
+ if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
+ return 0;
+
+ source_loop = bb->loop_father;
+ return loop == source_loop || flow_loop_nested_p (loop, source_loop);
+}
+
+/* Enumeration predicate for get_loop_body_with_size. */
+static bool
+glb_enum_p (const_basic_block bb, const void *glb_loop)
+{
+ const struct loop *const loop = (const struct loop *) glb_loop;
+ return (bb != loop->header
+ && dominated_by_p (CDI_DOMINATORS, bb, loop->header));
+}
+
+/* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
+ order against direction of edges from latch. Specially, if
+ header != latch, latch is the 1-st block. LOOP cannot be the fake
+ loop tree root, and its size must be at most MAX_SIZE. The blocks
+ in the LOOP body are stored to BODY, and the size of the LOOP is
+ returned. */
+
+unsigned
+get_loop_body_with_size (const struct loop *loop, basic_block *body,
+ unsigned max_size)
+{
+ return dfs_enumerate_from (loop->header, 1, glb_enum_p,
+ body, max_size, loop);
+}
+
+/* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs