Lines Matching refs:loop

1 /* Natural loop discovery code for GNU compiler.
56 /* Dump loop related CFG information. */
81 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
85 return (loop_depth (loop) > odepth
86 && (*loop->superloops)[odepth] == outer);
89 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
92 struct loop *
93 superloop_at_depth (struct loop *loop, unsigned depth)
95 unsigned ldepth = loop_depth (loop);
100 return loop;
102 return (*loop->superloops)[depth];
108 get_loop_latch_edges (const struct loop *loop)
114 FOR_EACH_EDGE (e, ei, loop->header->preds)
116 if (dominated_by_p (CDI_DOMINATORS, e->src, loop->header))
123 /* Dump the loop information specified by LOOP to the stream FILE
127 flow_loop_dump (const struct loop *loop, FILE *file,
128 void (*loop_dump_aux) (const struct loop *, FILE *, int),
136 if (! loop || ! loop->header)
139 fprintf (file, ";;\n;; Loop %d\n", loop->num);
141 fprintf (file, ";; header %d, ", loop->header->index);
142 if (loop->latch)
143 fprintf (file, "latch %d\n", loop->latch->index);
147 latches = get_loop_latch_edges (loop);
155 loop_depth (loop), (long) (loop_outer (loop)
156 ? loop_outer (loop)->num : -1));
159 bbs = get_loop_body (loop);
160 for (i = 0; i < loop->num_nodes; i++)
166 loop_dump_aux (loop, file, verbose);
169 /* Dump the loop information about loops to the stream FILE,
173 flow_loops_dump (FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
175 struct loop *loop;
182 FOR_EACH_LOOP (loop, LI_INCLUDE_ROOT)
184 flow_loop_dump (loop, file, loop_dump_aux, verbose);
194 flow_loop_free (struct loop *loop)
198 vec_free (loop->superloops);
200 /* Break the list of the loop exit records. They will be freed when the
203 loop structure. */
204 for (exit = loop->exits->next; exit != loop->exits; exit = next)
211 ggc_free (loop->exits);
212 ggc_free (loop);
223 loop_p loop;
225 /* Free the loop descriptors. */
226 FOR_EACH_VEC_SAFE_ELT (loops->larray, i, loop)
228 if (!loop)
231 flow_loop_free (loop);
239 Return the number of nodes within the loop. */
242 flow_loop_nodes_find (basic_block header, struct loop *loop)
249 header->loop_father = loop;
251 FOR_EACH_EDGE (latch, latch_ei, loop->header->preds)
253 if (latch->src->loop_father == loop
254 || !dominated_by_p (CDI_DOMINATORS, latch->src, loop->header))
259 latch->src->loop_father = loop;
273 if (ancestor->loop_father != loop)
275 ancestor->loop_father = loop;
287 /* Records the vector of superloops of the loop LOOP, whose immediate
291 establish_preds (struct loop *loop, struct loop *father)
297 loop->superloops = 0;
298 vec_alloc (loop->superloops, depth);
300 loop->superloops->quick_push (ploop);
301 loop->superloops->quick_push (father);
303 for (ploop = loop->inner; ploop; ploop = ploop->next)
304 establish_preds (ploop, loop);
307 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
308 added loop. If LOOP has some children, take care of that their
312 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
314 loop->next = father->inner;
315 father->inner = loop;
317 establish_preds (loop, father);
320 /* Remove LOOP from the loop hierarchy tree. */
323 flow_loop_tree_node_remove (struct loop *loop)
325 struct loop *prev, *father;
327 father = loop_outer (loop);
329 /* Remove loop from the list of sons. */
330 if (father->inner == loop)
331 father->inner = loop->next;
334 for (prev = father->inner; prev->next != loop; prev = prev->next)
336 prev->next = loop->next;
339 loop->superloops = NULL;
342 /* Allocates and returns new loop structure. */
344 struct loop *
347 struct loop *loop = ggc_cleared_alloc<struct loop> ();
349 loop->exits = ggc_cleared_alloc<loop_exit> ();
350 loop->exits->next = loop->exits->prev = loop->exits;
351 loop->can_be_parallel = false;
352 loop->nb_iterations_upper_bound = 0;
353 loop->nb_iterations_estimate = 0;
354 return loop;
358 (including the root of the loop tree). */
364 struct loop *root;
369 /* Dummy loop containing whole function. */
381 /* Returns whether HEADER is a loop header. */
390 loop (not worth the problems). */
395 by this block. A natural loop has a single entry
397 loop. It also has single back edge to the header
412 If LOOPS is non-NULL then the loop structures for already recorded loops
435 /* Ensure that loop exits were released. */
443 /* The root loop node contains all basic-blocks. */
451 /* Gather all loop headers in reverse completion order and allocate
452 loop structures for loops that are not already present. */
459 struct loop *loop;
461 /* The current active loop tree has valid loop-fathers for
466 loop = header->loop_father;
467 /* If we found an existing loop remove it from the
468 loop tree. It is going to be inserted again
470 flow_loop_tree_node_remove (loop);
474 /* Otherwise allocate a new loop structure for the loop. */
475 loop = alloc_loop ();
476 /* ??? We could re-use unused loop slots here. */
477 loop->num = loops->larray->length ();
478 vec_safe_push (loops->larray, loop);
479 loop->header = header;
484 "loop %d with header %d\n",
485 loop->num, header->index);
488 loop->latch = NULL;
489 larray.safe_push (loop);
492 /* Make blocks part of the loop root node at start. */
498 /* Now iterate over the loops found, insert them into the loop tree
502 struct loop *loop = larray[i];
503 basic_block header = loop->header;
507 flow_loop_tree_node_add (header->loop_father, loop);
508 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
516 if (flow_bb_inside_loop_p (loop, latch))
518 if (loop->latch != NULL)
521 loop->latch = NULL;
524 loop->latch = latch;
533 considered to belong to inner loop with same header. */
546 on the loop structure of the program, so it does not make much sense to
547 derive the loop structure from it). */
582 loop (which is the case where we would lose the most performance by not
583 disambiguating the loop). More precisely, we look for the following
590 find_subloop_latch_edge_by_ivs (struct loop *loop ATTRIBUTE_UNUSED, vec<edge> latches)
611 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
621 if (!bb || !flow_bb_inside_loop_p (loop, bb))
642 find_subloop_latch_edge (struct loop *loop)
644 vec<edge> latches = get_loop_latch_edges (loop);
656 latch = find_subloop_latch_edge_by_ivs (loop, latches);
676 form_subloop (struct loop *loop, edge latch)
680 struct loop *new_loop;
683 FOR_EACH_EDGE (e, ei, loop->header->preds)
688 new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
692 loop->header = new_entry->src;
694 /* Find the blocks and subloops that belong to the new loop, and add it to
695 the appropriate place in the loop tree. */
699 add_loop (new_loop, loop);
706 merge_latch_edges (struct loop *loop)
708 vec<edge> latches = get_loop_latch_edges (loop);
715 loop->latch = latches[0]->src;
719 fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
724 latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
728 loop->header = latch->dest;
729 loop->latch = latch->src;
739 disambiguate_multiple_latches (struct loop *loop)
750 F, then rescan the loops and try again for the outer loop.
752 entry edges to R, thus making F the single latch of the loop. */
755 fprintf (dump_file, "Disambiguating loop %d with multiple latches\n",
756 loop->num);
762 e = find_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), loop->header);
768 e = find_subloop_latch_edge (loop);
772 form_subloop (loop, e);
775 merge_latch_edges (loop);
783 struct loop *loop;
785 FOR_EACH_LOOP (loop, 0)
787 if (!loop->latch)
788 disambiguate_multiple_latches (loop);
794 flow_bb_inside_loop_p (const struct loop *loop, const_basic_block bb)
796 struct loop *source_loop;
803 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
810 const struct loop *const loop = (const struct loop *) glb_loop;
811 return (bb != loop->header
812 && dominated_by_p (CDI_DOMINATORS, bb, loop->header));
818 loop tree root, and its size must be at most MAX_SIZE. The blocks
823 get_loop_body_with_size (const struct loop *loop, basic_block *body,
826 return dfs_enumerate_from (loop->header, 1, glb_enum_p,
827 body, max_size, loop);
835 get_loop_body (const struct loop *loop)
840 gcc_assert (loop->num_nodes);
842 body = XNEWVEC (basic_block, loop->num_nodes);
844 if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
847 special-case the fake loop that contains the whole function. */
848 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks_for_fn (cfun));
849 body[tv++] = loop->header;
855 tv = get_loop_body_with_size (loop, body, loop->num_nodes);
857 gcc_assert (tv == loop->num_nodes);
865 fill_sons_in_loop (const struct loop *loop, basic_block bb,
875 if (!flow_bb_inside_loop_p (loop, son))
878 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
883 fill_sons_in_loop (loop, son, tovisit, tv);
887 fill_sons_in_loop (loop, postpone, tovisit, tv);
890 /* Gets body of a LOOP (that must be different from the outermost loop)
895 get_loop_body_in_dom_order (const struct loop *loop)
900 gcc_assert (loop->num_nodes);
902 tovisit = XNEWVEC (basic_block, loop->num_nodes);
904 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
907 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
909 gcc_assert (tv == (int) loop->num_nodes);
917 get_loop_body_in_custom_order (const struct loop *loop,
920 basic_block *bbs = get_loop_body (loop);
922 qsort (bbs, loop->num_nodes, sizeof (basic_block), bb_comparator);
930 get_loop_body_in_bfs_order (const struct loop *loop)
938 gcc_assert (loop->num_nodes);
939 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
941 blocks = XNEWVEC (basic_block, loop->num_nodes);
944 bb = loop->header;
945 while (i < loop->num_nodes)
956 if (flow_bb_inside_loop_p (loop, e->dest))
988 /* Frees the list of loop exit descriptions EX. */
1005 /* Returns the list of records for E as an exit of a loop. */
1013 /* Updates the lists of loop exits in that E appears.
1016 If NEW_EDGE is true and E is not a loop exit, we
1017 do not try to remove it from loop exit lists. */
1023 struct loop *aloop, *cloop;
1070 /* For each loop, record list of exit edges, and start maintaining these
1130 /* Releases lists of loop exits. */
1144 get_loop_exit_edges (const struct loop *loop)
1153 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
1156 scan the body of the loop. */
1159 for (exit = loop->exits->next; exit->e; exit = exit->next)
1164 body = get_loop_body (loop);
1165 for (i = 0; i < loop->num_nodes; i++)
1168 if (!flow_bb_inside_loop_p (loop, e->dest))
1180 num_loop_branches (const struct loop *loop)
1185 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
1187 body = get_loop_body (loop);
1189 for (i = 0; i < loop->num_nodes; i++)
1199 add_bb_to_loop (basic_block bb, struct loop *loop)
1207 bb->loop_father = loop;
1208 loop->num_nodes++;
1209 FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
1227 struct loop *loop = bb->loop_father;
1232 gcc_assert (loop != NULL);
1233 loop->num_nodes--;
1234 FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
1248 /* Finds nearest common ancestor in loop tree for given loops. */
1249 struct loop *
1250 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1276 delete_loop (struct loop *loop)
1278 /* Remove the loop from structure. */
1279 flow_loop_tree_node_remove (loop);
1281 /* Remove loop from loops array. */
1282 (*current_loops->larray)[loop->num] = NULL;
1284 /* Free loop data. */
1285 flow_loop_free (loop);
1291 cancel_loop (struct loop *loop)
1295 struct loop *outer = loop_outer (loop);
1297 gcc_assert (!loop->inner);
1300 bbs = get_loop_body (loop);
1301 for (i = 0; i < loop->num_nodes; i++)
1305 delete_loop (loop);
1310 cancel_loop_tree (struct loop *loop)
1312 while (loop->inner)
1313 cancel_loop_tree (loop->inner);
1314 cancel_loop (loop);
1319 -- results of get_loop_body really belong to the loop
1320 -- loop header have just single entry edge and single latch edge
1321 -- loop latches have only single successor that is header of their loop
1323 -- the cached loop depth and loop father of each bb is correct
1331 struct loop *loop;
1341 error ("loop verification on loop tree that needs fixup");
1357 error ("loop with header %d marked for removal", bb->index);
1362 error ("loop with header %d not in loop tree", bb->index);
1368 error ("non-loop with header %d not marked for removal", bb->index);
1372 /* Check the recorded loop father and sizes of loops. */
1376 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1380 if (loop->header == NULL)
1382 error ("removed loop %d in loop tree", loop->num);
1387 n = get_loop_body_with_size (loop, bbs, n_basic_blocks_for_fn (cfun));
1388 if (loop->num_nodes != n)
1390 error ("size of loop %d should be %d, not %d",
1391 loop->num, n, loop->num_nodes);
1399 if (!flow_bb_inside_loop_p (loop, bb))
1401 error ("bb %d does not belong to loop %d",
1402 bb->index, loop->num);
1406 /* Ignore this block if it is in an inner loop. */
1411 if (bb->loop_father != loop)
1413 error ("bb %d has father loop %d, should be loop %d",
1414 bb->index, bb->loop_father->num, loop->num);
1423 FOR_EACH_LOOP (loop, 0)
1425 i = loop->num;
1426 if (loop->header == NULL)
1428 if (!bb_loop_header_p (loop->header))
1430 error ("loop %d%'s header is not a loop header", i);
1434 && EDGE_COUNT (loop->header->preds) != 2)
1436 error ("loop %d%'s header does not have exactly 2 entries", i);
1439 if (loop->latch)
1441 if (!find_edge (loop->latch, loop->header))
1443 error ("loop %d%'s latch does not have an edge to its header", i);
1446 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, loop->header))
1448 error ("loop %d%'s latch is not dominated by its header", i);
1454 if (!single_succ_p (loop->latch))
1456 error ("loop %d%'s latch does not have exactly 1 successor", i);
1459 if (single_succ (loop->latch) != loop->header)
1461 error ("loop %d%'s latch does not have header as successor", i);
1464 if (loop->latch->loop_father != loop)
1466 error ("loop %d%'s latch does not belong directly to it", i);
1470 if (loop->header->loop_father != loop)
1472 error ("loop %d%'s header does not belong directly to it", i);
1476 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1478 error ("loop %d%'s latch is marked as part of irreducible region", i);
1542 /* Check the recorded loop exits. */
1543 FOR_EACH_LOOP (loop, 0)
1545 if (!loop->exits || loop->exits->e != NULL)
1547 error ("corrupted head of the exits list of loop %d",
1548 loop->num);
1555 for (mexit = loop->exits, exit = mexit->next, i = 0;
1563 if (exit != loop->exits)
1565 error ("corrupted exits list of loop %d", loop->num);
1572 if (loop->exits->next != loop->exits)
1574 error ("nonempty exits list of loop %d, but exits are not recorded",
1575 loop->num);
1609 for (loop = bb->loop_father;
1610 loop != e->dest->loop_father
1611 /* When a loop exit is also an entry edge which
1613 then the last loop exited is the outer loop
1614 of the loop entered. */
1615 && loop != loop_outer (e->dest->loop_father);
1616 loop = loop_outer (loop))
1619 sizes[loop->num]++;
1633 error ("too many loop exits recorded");
1637 FOR_EACH_LOOP (loop, 0)
1640 for (exit = loop->exits->next; exit->e; exit = exit->next)
1642 if (eloops != sizes[loop->num])
1644 error ("%d exits recorded for loop %d (having %d exits)",
1645 eloops, loop->num, sizes[loop->num]);
1661 loop_latch_edge (const struct loop *loop)
1663 return find_edge (loop->latch, loop->header);
1668 loop_preheader_edge (const struct loop *loop)
1675 FOR_EACH_EDGE (e, ei, loop->header->preds)
1676 if (e->src != loop->latch)
1685 loop_exit_edge_p (const struct loop *loop, const_edge e)
1687 return (flow_bb_inside_loop_p (loop, e->src)
1688 && !flow_bb_inside_loop_p (loop, e->dest));
1696 single_exit (const struct loop *loop)
1698 struct loop_exit *exit = loop->exits->next;
1703 if (exit->e && exit->next == loop->exits)
1712 loop_exits_to_bb_p (struct loop *loop, basic_block bb)
1718 if (loop_exit_edge_p (loop, e))
1727 loop_exits_from_bb_p (struct loop *loop, basic_block bb)
1733 if (loop_exit_edge_p (loop, e))
1739 /* Return location corresponding to the loop control condition if possible. */
1742 get_loop_location (struct loop *loop)
1748 /* For a for or while loop, we would like to return the location
1750 for the branch guarding the loop back-edge. */
1752 /* If this is a simple loop with an in_edge, then the loop control
1754 desc = get_simple_loop_desc (loop);
1763 /* If loop has a single exit, then the loop control branch
1765 if ((exit = single_exit (loop)))
1774 FOR_BB_INSNS_REVERSE (loop->latch, insn)
1779 /* Finally, if none of the above identifies the loop control branch,
1780 return the first location in the loop header. */
1781 FOR_BB_INSNS (loop->header, insn)
1792 of iterations. UPPER is true if we are sure the loop iterates at most
1796 record_niter_bound (struct loop *loop, const widest_int &i_bound,
1802 && (!loop->any_upper_bound
1803 || wi::ltu_p (i_bound, loop->nb_iterations_upper_bound)))
1805 loop->any_upper_bound = true;
1806 loop->nb_iterations_upper_bound = i_bound;
1809 && (!loop->any_estimate
1810 || wi::ltu_p (i_bound, loop->nb_iterations_estimate)))
1812 loop->any_estimate = true;
1813 loop->nb_iterations_estimate = i_bound;
1818 if (loop->any_upper_bound
1819 && loop->any_estimate
1820 && wi::ltu_p (loop->nb_iterations_upper_bound,
1821 loop->nb_iterations_estimate))
1822 loop->nb_iterations_estimate = loop->nb_iterations_upper_bound;
1830 get_estimated_loop_iterations_int (struct loop *loop)
1835 if (!get_estimated_loop_iterations (loop, &nit))
1846 in the LOOP. For statements before the loop exit, this exceeds
1850 max_stmt_executions_int (struct loop *loop)
1852 HOST_WIDE_INT nit = get_max_loop_iterations_int (loop);
1869 get_estimated_loop_iterations (struct loop *loop, widest_int *nit)
1873 if (!loop->any_estimate)
1875 if (loop->header->count)
1878 (expected_loop_iterations_unbounded (loop) + 1);
1884 *nit = loop->nb_iterations_estimate;
1893 get_max_loop_iterations (struct loop *loop, widest_int *nit)
1895 if (!loop->any_upper_bound)
1898 *nit = loop->nb_iterations_upper_bound;
1907 get_max_loop_iterations_int (struct loop *loop)
1912 if (!get_max_loop_iterations (loop, &nit))
1922 /* Returns the loop depth of the loop BB belongs to. */
1933 mark_loop_for_removal (loop_p loop)
1935 if (loop->header == NULL)
1937 loop->former_header = loop->header;
1938 loop->header = NULL;
1939 loop->latch = NULL;