]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfgloop.c
* cfganal.c (flow_depth_first_order_compute, dfs_enumerate_from,
[thirdparty/gcc.git] / gcc / cfgloop.c
1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "toplev.h"
29 #include "cfgloop.h"
30 #include "flags.h"
31 #include "tree.h"
32 #include "tree-flow.h"
33
34 /* Ratio of frequencies of edges so that one of more latch edges is
35 considered to belong to inner loop with same header. */
36 #define HEAVY_EDGE_RATIO 8
37
38 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
39 #define LATCH_EDGE(E) (*(int *) (E)->aux)
40
41 static void flow_loops_cfg_dump (const struct loops *, FILE *);
42 static void flow_loop_entry_edges_find (struct loop *);
43 static void flow_loop_exit_edges_find (struct loop *);
44 static int flow_loop_nodes_find (basic_block, struct loop *);
45 static void flow_loop_pre_header_scan (struct loop *);
46 static basic_block flow_loop_pre_header_find (basic_block);
47 static int flow_loop_level_compute (struct loop *);
48 static int flow_loops_level_compute (struct loops *);
49 static void establish_preds (struct loop *);
50 static void canonicalize_loop_headers (void);
51 static bool glb_enum_p (basic_block, void *);
52 \f
53 /* Dump loop related CFG information. */
54
55 static void
56 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
57 {
58 int i;
59 basic_block bb;
60
61 if (! loops->num || ! file)
62 return;
63
64 FOR_EACH_BB (bb)
65 {
66 edge succ;
67
68 fprintf (file, ";; %d succs { ", bb->index);
69 for (succ = bb->succ; succ; succ = succ->succ_next)
70 fprintf (file, "%d ", succ->dest->index);
71 fprintf (file, "}\n");
72 }
73
74 /* Dump the DFS node order. */
75 if (loops->cfg.dfs_order)
76 {
77 fputs (";; DFS order: ", file);
78 for (i = 0; i < n_basic_blocks; i++)
79 fprintf (file, "%d ", loops->cfg.dfs_order[i]);
80
81 fputs ("\n", file);
82 }
83
84 /* Dump the reverse completion node order. */
85 if (loops->cfg.rc_order)
86 {
87 fputs (";; RC order: ", file);
88 for (i = 0; i < n_basic_blocks; i++)
89 fprintf (file, "%d ", loops->cfg.rc_order[i]);
90
91 fputs ("\n", file);
92 }
93 }
94
95 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
96
97 bool
98 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
99 {
100 return (loop->depth > outer->depth
101 && loop->pred[outer->depth] == outer);
102 }
103
104 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
105 loops within LOOP. */
106
107 struct loop *
108 superloop_at_depth (struct loop *loop, unsigned depth)
109 {
110 gcc_assert (depth <= (unsigned) loop->depth);
111
112 if (depth == (unsigned) loop->depth)
113 return loop;
114
115 return loop->pred[depth];
116 }
117
118 /* Dump the loop information specified by LOOP to the stream FILE
119 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
120
121 void
122 flow_loop_dump (const struct loop *loop, FILE *file,
123 void (*loop_dump_aux) (const struct loop *, FILE *, int),
124 int verbose)
125 {
126 basic_block *bbs;
127 unsigned i;
128
129 if (! loop || ! loop->header)
130 return;
131
132 fprintf (file, ";;\n;; Loop %d:%s\n", loop->num,
133 loop->invalid ? " invalid" : "");
134
135 fprintf (file, ";; header %d, latch %d, pre-header %d\n",
136 loop->header->index, loop->latch->index,
137 loop->pre_header ? loop->pre_header->index : -1);
138 fprintf (file, ";; depth %d, level %d, outer %ld\n",
139 loop->depth, loop->level,
140 (long) (loop->outer ? loop->outer->num : -1));
141
142 if (loop->pre_header_edges)
143 flow_edge_list_print (";; pre-header edges", loop->pre_header_edges,
144 loop->num_pre_header_edges, file);
145
146 flow_edge_list_print (";; entry edges", loop->entry_edges,
147 loop->num_entries, file);
148 fprintf (file, ";; nodes:");
149 bbs = get_loop_body (loop);
150 for (i = 0; i < loop->num_nodes; i++)
151 fprintf (file, " %d", bbs[i]->index);
152 free (bbs);
153 fprintf (file, "\n");
154 flow_edge_list_print (";; exit edges", loop->exit_edges,
155 loop->num_exits, file);
156
157 if (loop_dump_aux)
158 loop_dump_aux (loop, file, verbose);
159 }
160
161 /* Dump the loop information specified by LOOPS to the stream FILE,
162 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
163
164 void
165 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
166 {
167 int i;
168 int num_loops;
169
170 num_loops = loops->num;
171 if (! num_loops || ! file)
172 return;
173
174 fprintf (file, ";; %d loops found, %d levels\n",
175 num_loops, loops->levels);
176
177 for (i = 0; i < num_loops; i++)
178 {
179 struct loop *loop = loops->parray[i];
180
181 if (!loop)
182 continue;
183
184 flow_loop_dump (loop, file, loop_dump_aux, verbose);
185 }
186
187 if (verbose)
188 flow_loops_cfg_dump (loops, file);
189 }
190
191 /* Free data allocated for LOOP. */
192 void
193 flow_loop_free (struct loop *loop)
194 {
195 if (loop->pre_header_edges)
196 free (loop->pre_header_edges);
197 if (loop->entry_edges)
198 free (loop->entry_edges);
199 if (loop->exit_edges)
200 free (loop->exit_edges);
201 if (loop->pred)
202 free (loop->pred);
203 free (loop);
204 }
205
206 /* Free all the memory allocated for LOOPS. */
207
208 void
209 flow_loops_free (struct loops *loops)
210 {
211 if (loops->parray)
212 {
213 unsigned i;
214
215 gcc_assert (loops->num);
216
217 /* Free the loop descriptors. */
218 for (i = 0; i < loops->num; i++)
219 {
220 struct loop *loop = loops->parray[i];
221
222 if (!loop)
223 continue;
224
225 flow_loop_free (loop);
226 }
227
228 free (loops->parray);
229 loops->parray = NULL;
230
231 if (loops->cfg.dfs_order)
232 free (loops->cfg.dfs_order);
233 if (loops->cfg.rc_order)
234 free (loops->cfg.rc_order);
235
236 }
237 }
238
239 /* Find the entry edges into the LOOP. */
240
241 static void
242 flow_loop_entry_edges_find (struct loop *loop)
243 {
244 edge e;
245 int num_entries;
246
247 num_entries = 0;
248 for (e = loop->header->pred; e; e = e->pred_next)
249 {
250 if (flow_loop_outside_edge_p (loop, e))
251 num_entries++;
252 }
253
254 gcc_assert (num_entries);
255
256 loop->entry_edges = xmalloc (num_entries * sizeof (edge *));
257
258 num_entries = 0;
259 for (e = loop->header->pred; e; e = e->pred_next)
260 {
261 if (flow_loop_outside_edge_p (loop, e))
262 loop->entry_edges[num_entries++] = e;
263 }
264
265 loop->num_entries = num_entries;
266 }
267
268 /* Find the exit edges from the LOOP. */
269
270 static void
271 flow_loop_exit_edges_find (struct loop *loop)
272 {
273 edge e;
274 basic_block node, *bbs;
275 unsigned num_exits, i;
276
277 loop->exit_edges = NULL;
278 loop->num_exits = 0;
279
280 /* Check all nodes within the loop to see if there are any
281 successors not in the loop. Note that a node may have multiple
282 exiting edges. */
283 num_exits = 0;
284 bbs = get_loop_body (loop);
285 for (i = 0; i < loop->num_nodes; i++)
286 {
287 node = bbs[i];
288 for (e = node->succ; e; e = e->succ_next)
289 {
290 basic_block dest = e->dest;
291
292 if (!flow_bb_inside_loop_p (loop, dest))
293 num_exits++;
294 }
295 }
296
297 if (! num_exits)
298 {
299 free (bbs);
300 return;
301 }
302
303 loop->exit_edges = xmalloc (num_exits * sizeof (edge *));
304
305 /* Store all exiting edges into an array. */
306 num_exits = 0;
307 for (i = 0; i < loop->num_nodes; i++)
308 {
309 node = bbs[i];
310 for (e = node->succ; e; e = e->succ_next)
311 {
312 basic_block dest = e->dest;
313
314 if (!flow_bb_inside_loop_p (loop, dest))
315 {
316 e->flags |= EDGE_LOOP_EXIT;
317 loop->exit_edges[num_exits++] = e;
318 }
319 }
320 }
321 free (bbs);
322 loop->num_exits = num_exits;
323 }
324
325 /* Find the nodes contained within the LOOP with header HEADER.
326 Return the number of nodes within the loop. */
327
328 static int
329 flow_loop_nodes_find (basic_block header, struct loop *loop)
330 {
331 basic_block *stack;
332 int sp;
333 int num_nodes = 1;
334
335 header->loop_father = loop;
336 header->loop_depth = loop->depth;
337
338 if (loop->latch->loop_father != loop)
339 {
340 stack = xmalloc (n_basic_blocks * sizeof (basic_block));
341 sp = 0;
342 num_nodes++;
343 stack[sp++] = loop->latch;
344 loop->latch->loop_father = loop;
345 loop->latch->loop_depth = loop->depth;
346
347 while (sp)
348 {
349 basic_block node;
350 edge e;
351
352 node = stack[--sp];
353
354 for (e = node->pred; e; e = e->pred_next)
355 {
356 basic_block ancestor = e->src;
357
358 if (ancestor != ENTRY_BLOCK_PTR
359 && ancestor->loop_father != loop)
360 {
361 ancestor->loop_father = loop;
362 ancestor->loop_depth = loop->depth;
363 num_nodes++;
364 stack[sp++] = ancestor;
365 }
366 }
367 }
368 free (stack);
369 }
370 return num_nodes;
371 }
372
373 /* For each loop in the lOOPS tree that has just a single exit
374 record the exit edge. */
375
376 void
377 mark_single_exit_loops (struct loops *loops)
378 {
379 basic_block bb;
380 edge e;
381 struct loop *loop;
382 unsigned i;
383
384 for (i = 1; i < loops->num; i++)
385 {
386 loop = loops->parray[i];
387 if (loop)
388 loop->single_exit = NULL;
389 }
390
391 FOR_EACH_BB (bb)
392 {
393 if (bb->loop_father == loops->tree_root)
394 continue;
395 for (e = bb->succ; e; e = e->succ_next)
396 {
397 if (e->dest == EXIT_BLOCK_PTR)
398 continue;
399
400 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
401 continue;
402
403 for (loop = bb->loop_father;
404 loop != e->dest->loop_father;
405 loop = loop->outer)
406 {
407 /* If we have already seen an exit, mark this by the edge that
408 surely does not occur as any exit. */
409 if (loop->single_exit)
410 loop->single_exit = ENTRY_BLOCK_PTR->succ;
411 else
412 loop->single_exit = e;
413 }
414 }
415 }
416
417 for (i = 1; i < loops->num; i++)
418 {
419 loop = loops->parray[i];
420 if (!loop)
421 continue;
422
423 if (loop->single_exit == ENTRY_BLOCK_PTR->succ)
424 loop->single_exit = NULL;
425 }
426
427 loops->state |= LOOPS_HAVE_MARKED_SINGLE_EXITS;
428 }
429
430 /* Find the root node of the loop pre-header extended basic block and
431 the edges along the trace from the root node to the loop header. */
432
433 static void
434 flow_loop_pre_header_scan (struct loop *loop)
435 {
436 int num;
437 basic_block ebb;
438 edge e;
439
440 loop->num_pre_header_edges = 0;
441 if (loop->num_entries != 1)
442 return;
443
444 ebb = loop->entry_edges[0]->src;
445 if (ebb == ENTRY_BLOCK_PTR)
446 return;
447
448 /* Count number of edges along trace from loop header to
449 root of pre-header extended basic block. Usually this is
450 only one or two edges. */
451 for (num = 1; ebb->pred->src != ENTRY_BLOCK_PTR && ! ebb->pred->pred_next;
452 num++)
453 ebb = ebb->pred->src;
454
455 loop->pre_header_edges = xmalloc (num * sizeof (edge));
456 loop->num_pre_header_edges = num;
457
458 /* Store edges in order that they are followed. The source of the first edge
459 is the root node of the pre-header extended basic block and the
460 destination of the last last edge is the loop header. */
461 for (e = loop->entry_edges[0]; num; e = e->src->pred)
462 loop->pre_header_edges[--num] = e;
463 }
464
465 /* Return the block for the pre-header of the loop with header
466 HEADER. Return NULL if there is no pre-header. */
467
468 static basic_block
469 flow_loop_pre_header_find (basic_block header)
470 {
471 basic_block pre_header;
472 edge e;
473
474 /* If block p is a predecessor of the header and is the only block
475 that the header does not dominate, then it is the pre-header. */
476 pre_header = NULL;
477 for (e = header->pred; e; e = e->pred_next)
478 {
479 basic_block node = e->src;
480
481 if (node != ENTRY_BLOCK_PTR
482 && ! dominated_by_p (CDI_DOMINATORS, node, header))
483 {
484 if (pre_header == NULL)
485 pre_header = node;
486 else
487 {
488 /* There are multiple edges into the header from outside
489 the loop so there is no pre-header block. */
490 pre_header = NULL;
491 break;
492 }
493 }
494 }
495
496 return pre_header;
497 }
498
499 static void
500 establish_preds (struct loop *loop)
501 {
502 struct loop *ploop, *father = loop->outer;
503
504 loop->depth = father->depth + 1;
505 if (loop->pred)
506 free (loop->pred);
507 loop->pred = xmalloc (sizeof (struct loop *) * loop->depth);
508 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
509 loop->pred[father->depth] = father;
510
511 for (ploop = loop->inner; ploop; ploop = ploop->next)
512 establish_preds (ploop);
513 }
514
515 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
516 added loop. If LOOP has some children, take care of that their
517 pred field will be initialized correctly. */
518
519 void
520 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
521 {
522 loop->next = father->inner;
523 father->inner = loop;
524 loop->outer = father;
525
526 establish_preds (loop);
527 }
528
529 /* Remove LOOP from the loop hierarchy tree. */
530
531 void
532 flow_loop_tree_node_remove (struct loop *loop)
533 {
534 struct loop *prev, *father;
535
536 father = loop->outer;
537 loop->outer = NULL;
538
539 /* Remove loop from the list of sons. */
540 if (father->inner == loop)
541 father->inner = loop->next;
542 else
543 {
544 for (prev = father->inner; prev->next != loop; prev = prev->next);
545 prev->next = loop->next;
546 }
547
548 loop->depth = -1;
549 free (loop->pred);
550 loop->pred = NULL;
551 }
552
553 /* Helper function to compute loop nesting depth and enclosed loop level
554 for the natural loop specified by LOOP. Returns the loop level. */
555
556 static int
557 flow_loop_level_compute (struct loop *loop)
558 {
559 struct loop *inner;
560 int level = 1;
561
562 if (! loop)
563 return 0;
564
565 /* Traverse loop tree assigning depth and computing level as the
566 maximum level of all the inner loops of this loop. The loop
567 level is equivalent to the height of the loop in the loop tree
568 and corresponds to the number of enclosed loop levels (including
569 itself). */
570 for (inner = loop->inner; inner; inner = inner->next)
571 {
572 int ilevel = flow_loop_level_compute (inner) + 1;
573
574 if (ilevel > level)
575 level = ilevel;
576 }
577
578 loop->level = level;
579 return level;
580 }
581
582 /* Compute the loop nesting depth and enclosed loop level for the loop
583 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
584 level. */
585
586 static int
587 flow_loops_level_compute (struct loops *loops)
588 {
589 return flow_loop_level_compute (loops->tree_root);
590 }
591
592 /* Scan a single natural loop specified by LOOP collecting information
593 about it specified by FLAGS. */
594
595 int
596 flow_loop_scan (struct loop *loop, int flags)
597 {
598 if (flags & LOOP_ENTRY_EDGES)
599 {
600 /* Find edges which enter the loop header.
601 Note that the entry edges should only
602 enter the header of a natural loop. */
603 flow_loop_entry_edges_find (loop);
604 }
605
606 if (flags & LOOP_EXIT_EDGES)
607 {
608 /* Find edges which exit the loop. */
609 flow_loop_exit_edges_find (loop);
610 }
611
612 if (flags & LOOP_PRE_HEADER)
613 {
614 /* Look to see if the loop has a pre-header node. */
615 loop->pre_header = flow_loop_pre_header_find (loop->header);
616
617 /* Find the blocks within the extended basic block of
618 the loop pre-header. */
619 flow_loop_pre_header_scan (loop);
620 }
621
622 return 1;
623 }
624
625 /* A callback to update latch and header info for basic block JUMP created
626 by redirecting an edge. */
627
628 static void
629 update_latch_info (basic_block jump)
630 {
631 alloc_aux_for_block (jump, sizeof (int));
632 HEADER_BLOCK (jump) = 0;
633 alloc_aux_for_edge (jump->pred, sizeof (int));
634 LATCH_EDGE (jump->pred) = 0;
635 set_immediate_dominator (CDI_DOMINATORS, jump, jump->pred->src);
636 }
637
638 /* A callback for make_forwarder block, to redirect all edges except for
639 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
640 whether to redirect it. */
641
642 static edge mfb_kj_edge;
643 static bool
644 mfb_keep_just (edge e)
645 {
646 return e != mfb_kj_edge;
647 }
648
649 /* A callback for make_forwarder block, to redirect the latch edges into an
650 entry part. E is the edge for that we should decide whether to redirect
651 it. */
652
653 static bool
654 mfb_keep_nonlatch (edge e)
655 {
656 return LATCH_EDGE (e);
657 }
658
659 /* Takes care of merging natural loops with shared headers. */
660
661 static void
662 canonicalize_loop_headers (void)
663 {
664 basic_block header;
665 edge e;
666
667 alloc_aux_for_blocks (sizeof (int));
668 alloc_aux_for_edges (sizeof (int));
669
670 /* Split blocks so that each loop has only single latch. */
671 FOR_EACH_BB (header)
672 {
673 int num_latches = 0;
674 int have_abnormal_edge = 0;
675
676 for (e = header->pred; e; e = e->pred_next)
677 {
678 basic_block latch = e->src;
679
680 if (e->flags & EDGE_ABNORMAL)
681 have_abnormal_edge = 1;
682
683 if (latch != ENTRY_BLOCK_PTR
684 && dominated_by_p (CDI_DOMINATORS, latch, header))
685 {
686 num_latches++;
687 LATCH_EDGE (e) = 1;
688 }
689 }
690 if (have_abnormal_edge)
691 HEADER_BLOCK (header) = 0;
692 else
693 HEADER_BLOCK (header) = num_latches;
694 }
695
696 if (HEADER_BLOCK (ENTRY_BLOCK_PTR->succ->dest))
697 {
698 basic_block bb;
699
700 /* We could not redirect edges freely here. On the other hand,
701 we can simply split the edge from entry block. */
702 bb = split_edge (ENTRY_BLOCK_PTR->succ);
703
704 alloc_aux_for_edge (bb->succ, sizeof (int));
705 LATCH_EDGE (bb->succ) = 0;
706 alloc_aux_for_block (bb, sizeof (int));
707 HEADER_BLOCK (bb) = 0;
708 }
709
710 FOR_EACH_BB (header)
711 {
712 int max_freq, is_heavy;
713 edge heavy, tmp_edge;
714
715 if (HEADER_BLOCK (header) <= 1)
716 continue;
717
718 /* Find a heavy edge. */
719 is_heavy = 1;
720 heavy = NULL;
721 max_freq = 0;
722 for (e = header->pred; e; e = e->pred_next)
723 if (LATCH_EDGE (e) &&
724 EDGE_FREQUENCY (e) > max_freq)
725 max_freq = EDGE_FREQUENCY (e);
726 for (e = header->pred; e; e = e->pred_next)
727 if (LATCH_EDGE (e) &&
728 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
729 {
730 if (heavy)
731 {
732 is_heavy = 0;
733 break;
734 }
735 else
736 heavy = e;
737 }
738
739 if (is_heavy)
740 {
741 /* Split out the heavy edge, and create inner loop for it. */
742 mfb_kj_edge = heavy;
743 tmp_edge = make_forwarder_block (header, mfb_keep_just,
744 update_latch_info);
745 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
746 HEADER_BLOCK (tmp_edge->dest) = 1;
747 alloc_aux_for_edge (tmp_edge, sizeof (int));
748 LATCH_EDGE (tmp_edge) = 0;
749 HEADER_BLOCK (header)--;
750 }
751
752 if (HEADER_BLOCK (header) > 1)
753 {
754 /* Create a new latch block. */
755 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
756 update_latch_info);
757 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
758 HEADER_BLOCK (tmp_edge->src) = 0;
759 HEADER_BLOCK (tmp_edge->dest) = 1;
760 alloc_aux_for_edge (tmp_edge, sizeof (int));
761 LATCH_EDGE (tmp_edge) = 1;
762 }
763 }
764
765 free_aux_for_blocks ();
766 free_aux_for_edges ();
767
768 #ifdef ENABLE_CHECKING
769 verify_dominators (CDI_DOMINATORS);
770 #endif
771 }
772
773 /* Find all the natural loops in the function and save in LOOPS structure and
774 recalculate loop_depth information in basic block structures. FLAGS
775 controls which loop information is collected. Return the number of natural
776 loops found. */
777
778 int
779 flow_loops_find (struct loops *loops, int flags)
780 {
781 int i;
782 int b;
783 int num_loops;
784 edge e;
785 sbitmap headers;
786 int *dfs_order;
787 int *rc_order;
788 basic_block header;
789 basic_block bb;
790
791 /* This function cannot be repeatedly called with different
792 flags to build up the loop information. The loop tree
793 must always be built if this function is called. */
794 gcc_assert (flags & LOOP_TREE);
795
796 memset (loops, 0, sizeof *loops);
797
798 /* Taking care of this degenerate case makes the rest of
799 this code simpler. */
800 if (n_basic_blocks == 0)
801 return 0;
802
803 dfs_order = NULL;
804 rc_order = NULL;
805
806 /* Ensure that the dominators are computed. */
807 calculate_dominance_info (CDI_DOMINATORS);
808
809 /* Join loops with shared headers. */
810 canonicalize_loop_headers ();
811
812 /* Count the number of loop headers. This should be the
813 same as the number of natural loops. */
814 headers = sbitmap_alloc (last_basic_block);
815 sbitmap_zero (headers);
816
817 num_loops = 0;
818 FOR_EACH_BB (header)
819 {
820 int more_latches = 0;
821
822 header->loop_depth = 0;
823
824 /* If we have an abnormal predecessor, do not consider the
825 loop (not worth the problems). */
826 for (e = header->pred; e; e = e->pred_next)
827 if (e->flags & EDGE_ABNORMAL)
828 break;
829 if (e)
830 continue;
831
832 for (e = header->pred; e; e = e->pred_next)
833 {
834 basic_block latch = e->src;
835
836 gcc_assert (!(e->flags & EDGE_ABNORMAL));
837
838 /* Look for back edges where a predecessor is dominated
839 by this block. A natural loop has a single entry
840 node (header) that dominates all the nodes in the
841 loop. It also has single back edge to the header
842 from a latch node. */
843 if (latch != ENTRY_BLOCK_PTR
844 && dominated_by_p (CDI_DOMINATORS, latch, header))
845 {
846 /* Shared headers should be eliminated by now. */
847 gcc_assert (!more_latches);
848 more_latches = 1;
849 SET_BIT (headers, header->index);
850 num_loops++;
851 }
852 }
853 }
854
855 /* Allocate loop structures. */
856 loops->parray = xcalloc (num_loops + 1, sizeof (struct loop *));
857
858 /* Dummy loop containing whole function. */
859 loops->parray[0] = xcalloc (1, sizeof (struct loop));
860 loops->parray[0]->next = NULL;
861 loops->parray[0]->inner = NULL;
862 loops->parray[0]->outer = NULL;
863 loops->parray[0]->depth = 0;
864 loops->parray[0]->pred = NULL;
865 loops->parray[0]->num_nodes = n_basic_blocks + 2;
866 loops->parray[0]->latch = EXIT_BLOCK_PTR;
867 loops->parray[0]->header = ENTRY_BLOCK_PTR;
868 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
869 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
870
871 loops->tree_root = loops->parray[0];
872
873 /* Find and record information about all the natural loops
874 in the CFG. */
875 loops->num = 1;
876 FOR_EACH_BB (bb)
877 bb->loop_father = loops->tree_root;
878
879 if (num_loops)
880 {
881 /* Compute depth first search order of the CFG so that outer
882 natural loops will be found before inner natural loops. */
883 dfs_order = xmalloc (n_basic_blocks * sizeof (int));
884 rc_order = xmalloc (n_basic_blocks * sizeof (int));
885 flow_depth_first_order_compute (dfs_order, rc_order);
886
887 /* Save CFG derived information to avoid recomputing it. */
888 loops->cfg.dfs_order = dfs_order;
889 loops->cfg.rc_order = rc_order;
890
891 num_loops = 1;
892
893 for (b = 0; b < n_basic_blocks; b++)
894 {
895 struct loop *loop;
896
897 /* Search the nodes of the CFG in reverse completion order
898 so that we can find outer loops first. */
899 if (!TEST_BIT (headers, rc_order[b]))
900 continue;
901
902 header = BASIC_BLOCK (rc_order[b]);
903
904 loop = loops->parray[num_loops] = xcalloc (1, sizeof (struct loop));
905
906 loop->header = header;
907 loop->num = num_loops;
908 num_loops++;
909
910 /* Look for the latch for this header block. */
911 for (e = header->pred; e; e = e->pred_next)
912 {
913 basic_block latch = e->src;
914
915 if (latch != ENTRY_BLOCK_PTR
916 && dominated_by_p (CDI_DOMINATORS, latch, header))
917 {
918 loop->latch = latch;
919 break;
920 }
921 }
922
923 flow_loop_tree_node_add (header->loop_father, loop);
924 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
925 }
926
927 /* Assign the loop nesting depth and enclosed loop level for each
928 loop. */
929 loops->levels = flow_loops_level_compute (loops);
930
931 /* Scan the loops. */
932 for (i = 1; i < num_loops; i++)
933 flow_loop_scan (loops->parray[i], flags);
934
935 loops->num = num_loops;
936 }
937
938 sbitmap_free (headers);
939
940 loops->state = 0;
941 #ifdef ENABLE_CHECKING
942 verify_flow_info ();
943 verify_loop_structure (loops);
944 #endif
945
946 return loops->num;
947 }
948
949 /* Update the information regarding the loops in the CFG
950 specified by LOOPS. */
951
952 int
953 flow_loops_update (struct loops *loops, int flags)
954 {
955 /* One day we may want to update the current loop data. For now
956 throw away the old stuff and rebuild what we need. */
957 if (loops->parray)
958 flow_loops_free (loops);
959
960 return flow_loops_find (loops, flags);
961 }
962
963 /* Return nonzero if basic block BB belongs to LOOP. */
964 bool
965 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
966 {
967 struct loop *source_loop;
968
969 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
970 return 0;
971
972 source_loop = bb->loop_father;
973 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
974 }
975
976 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
977
978 bool
979 flow_loop_outside_edge_p (const struct loop *loop, edge e)
980 {
981 gcc_assert (e->dest == loop->header);
982 return !flow_bb_inside_loop_p (loop, e->src);
983 }
984
985 /* Enumeration predicate for get_loop_body. */
986 static bool
987 glb_enum_p (basic_block bb, void *glb_header)
988 {
989 return bb != (basic_block) glb_header;
990 }
991
992 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
993 order against direction of edges from latch. Specially, if
994 header != latch, latch is the 1-st block. */
995 basic_block *
996 get_loop_body (const struct loop *loop)
997 {
998 basic_block *tovisit, bb;
999 unsigned tv = 0;
1000
1001 gcc_assert (loop->num_nodes);
1002
1003 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
1004 tovisit[tv++] = loop->header;
1005
1006 if (loop->latch == EXIT_BLOCK_PTR)
1007 {
1008 /* There may be blocks unreachable from EXIT_BLOCK. */
1009 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks + 2);
1010 FOR_EACH_BB (bb)
1011 tovisit[tv++] = bb;
1012 tovisit[tv++] = EXIT_BLOCK_PTR;
1013 }
1014 else if (loop->latch != loop->header)
1015 {
1016 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
1017 tovisit + 1, loop->num_nodes - 1,
1018 loop->header) + 1;
1019 }
1020
1021 gcc_assert (tv == loop->num_nodes);
1022 return tovisit;
1023 }
1024
1025 /* Fills dominance descendants inside LOOP of the basic block BB into
1026 array TOVISIT from index *TV. */
1027
1028 static void
1029 fill_sons_in_loop (const struct loop *loop, basic_block bb,
1030 basic_block *tovisit, int *tv)
1031 {
1032 basic_block son, postpone = NULL;
1033
1034 tovisit[(*tv)++] = bb;
1035 for (son = first_dom_son (CDI_DOMINATORS, bb);
1036 son;
1037 son = next_dom_son (CDI_DOMINATORS, son))
1038 {
1039 if (!flow_bb_inside_loop_p (loop, son))
1040 continue;
1041
1042 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
1043 {
1044 postpone = son;
1045 continue;
1046 }
1047 fill_sons_in_loop (loop, son, tovisit, tv);
1048 }
1049
1050 if (postpone)
1051 fill_sons_in_loop (loop, postpone, tovisit, tv);
1052 }
1053
1054 /* Gets body of a LOOP (that must be different from the outermost loop)
1055 sorted by dominance relation. Additionally, if a basic block s dominates
1056 the latch, then only blocks dominated by s are be after it. */
1057
1058 basic_block *
1059 get_loop_body_in_dom_order (const struct loop *loop)
1060 {
1061 basic_block *tovisit;
1062 int tv;
1063
1064 gcc_assert (loop->num_nodes);
1065
1066 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
1067
1068 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
1069
1070 tv = 0;
1071 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
1072
1073 gcc_assert (tv == (int) loop->num_nodes);
1074
1075 return tovisit;
1076 }
1077
1078 /* Get body of a LOOP in breadth first sort order. */
1079
1080 basic_block *
1081 get_loop_body_in_bfs_order (const struct loop *loop)
1082 {
1083 basic_block *blocks;
1084 basic_block bb;
1085 bitmap visited;
1086 unsigned int i = 0;
1087 unsigned int vc = 1;
1088
1089 gcc_assert (loop->num_nodes);
1090 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
1091
1092 blocks = xcalloc (loop->num_nodes, sizeof (basic_block));
1093 visited = BITMAP_XMALLOC ();
1094
1095 bb = loop->header;
1096 while (i < loop->num_nodes)
1097 {
1098 edge e;
1099
1100 if (!bitmap_bit_p (visited, bb->index))
1101 {
1102 /* This basic block is now visited */
1103 bitmap_set_bit (visited, bb->index);
1104 blocks[i++] = bb;
1105 }
1106
1107 for (e = bb->succ; e; e = e->succ_next)
1108 {
1109 if (flow_bb_inside_loop_p (loop, e->dest))
1110 {
1111 if (!bitmap_bit_p (visited, e->dest->index))
1112 {
1113 bitmap_set_bit (visited, e->dest->index);
1114 blocks[i++] = e->dest;
1115 }
1116 }
1117 }
1118
1119 gcc_assert (i >= vc);
1120
1121 bb = blocks[vc++];
1122 }
1123
1124 BITMAP_XFREE (visited);
1125 return blocks;
1126 }
1127
1128 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1129 edge *
1130 get_loop_exit_edges (const struct loop *loop, unsigned int *n_edges)
1131 {
1132 edge *edges, e;
1133 unsigned i, n;
1134 basic_block * body;
1135
1136 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
1137
1138 body = get_loop_body (loop);
1139 n = 0;
1140 for (i = 0; i < loop->num_nodes; i++)
1141 for (e = body[i]->succ; e; e = e->succ_next)
1142 if (!flow_bb_inside_loop_p (loop, e->dest))
1143 n++;
1144 edges = xmalloc (n * sizeof (edge));
1145 *n_edges = n;
1146 n = 0;
1147 for (i = 0; i < loop->num_nodes; i++)
1148 for (e = body[i]->succ; e; e = e->succ_next)
1149 if (!flow_bb_inside_loop_p (loop, e->dest))
1150 edges[n++] = e;
1151 free (body);
1152
1153 return edges;
1154 }
1155
1156 /* Counts the number of conditional branches inside LOOP. */
1157
1158 unsigned
1159 num_loop_branches (const struct loop *loop)
1160 {
1161 unsigned i, n;
1162 basic_block * body;
1163
1164 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
1165
1166 body = get_loop_body (loop);
1167 n = 0;
1168 for (i = 0; i < loop->num_nodes; i++)
1169 if (body[i]->succ && body[i]->succ->succ_next)
1170 n++;
1171 free (body);
1172
1173 return n;
1174 }
1175
1176 /* Adds basic block BB to LOOP. */
1177 void
1178 add_bb_to_loop (basic_block bb, struct loop *loop)
1179 {
1180 int i;
1181
1182 bb->loop_father = loop;
1183 bb->loop_depth = loop->depth;
1184 loop->num_nodes++;
1185 for (i = 0; i < loop->depth; i++)
1186 loop->pred[i]->num_nodes++;
1187 }
1188
1189 /* Remove basic block BB from loops. */
1190 void
1191 remove_bb_from_loops (basic_block bb)
1192 {
1193 int i;
1194 struct loop *loop = bb->loop_father;
1195
1196 loop->num_nodes--;
1197 for (i = 0; i < loop->depth; i++)
1198 loop->pred[i]->num_nodes--;
1199 bb->loop_father = NULL;
1200 bb->loop_depth = 0;
1201 }
1202
1203 /* Finds nearest common ancestor in loop tree for given loops. */
1204 struct loop *
1205 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1206 {
1207 if (!loop_s) return loop_d;
1208 if (!loop_d) return loop_s;
1209
1210 if (loop_s->depth < loop_d->depth)
1211 loop_d = loop_d->pred[loop_s->depth];
1212 else if (loop_s->depth > loop_d->depth)
1213 loop_s = loop_s->pred[loop_d->depth];
1214
1215 while (loop_s != loop_d)
1216 {
1217 loop_s = loop_s->outer;
1218 loop_d = loop_d->outer;
1219 }
1220 return loop_s;
1221 }
1222
1223 /* Cancels the LOOP; it must be innermost one. */
1224 void
1225 cancel_loop (struct loops *loops, struct loop *loop)
1226 {
1227 basic_block *bbs;
1228 unsigned i;
1229
1230 gcc_assert (!loop->inner);
1231
1232 /* Move blocks up one level (they should be removed as soon as possible). */
1233 bbs = get_loop_body (loop);
1234 for (i = 0; i < loop->num_nodes; i++)
1235 bbs[i]->loop_father = loop->outer;
1236
1237 /* Remove the loop from structure. */
1238 flow_loop_tree_node_remove (loop);
1239
1240 /* Remove loop from loops array. */
1241 loops->parray[loop->num] = NULL;
1242
1243 /* Free loop data. */
1244 flow_loop_free (loop);
1245 }
1246
1247 /* Cancels LOOP and all its subloops. */
1248 void
1249 cancel_loop_tree (struct loops *loops, struct loop *loop)
1250 {
1251 while (loop->inner)
1252 cancel_loop_tree (loops, loop->inner);
1253 cancel_loop (loops, loop);
1254 }
1255
1256 /* Checks that LOOPS are all right:
1257 -- sizes of loops are all right
1258 -- results of get_loop_body really belong to the loop
1259 -- loop header have just single entry edge and single latch edge
1260 -- loop latches have only single successor that is header of their loop
1261 -- irreducible loops are correctly marked
1262 */
1263 void
1264 verify_loop_structure (struct loops *loops)
1265 {
1266 unsigned *sizes, i, j;
1267 sbitmap irreds;
1268 basic_block *bbs, bb;
1269 struct loop *loop;
1270 int err = 0;
1271 edge e;
1272
1273 /* Check sizes. */
1274 sizes = xcalloc (loops->num, sizeof (int));
1275 sizes[0] = 2;
1276
1277 FOR_EACH_BB (bb)
1278 for (loop = bb->loop_father; loop; loop = loop->outer)
1279 sizes[loop->num]++;
1280
1281 for (i = 0; i < loops->num; i++)
1282 {
1283 if (!loops->parray[i])
1284 continue;
1285
1286 if (loops->parray[i]->num_nodes != sizes[i])
1287 {
1288 error ("Size of loop %d should be %d, not %d.",
1289 i, sizes[i], loops->parray[i]->num_nodes);
1290 err = 1;
1291 }
1292 }
1293
1294 /* Check get_loop_body. */
1295 for (i = 1; i < loops->num; i++)
1296 {
1297 loop = loops->parray[i];
1298 if (!loop)
1299 continue;
1300 bbs = get_loop_body (loop);
1301
1302 for (j = 0; j < loop->num_nodes; j++)
1303 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1304 {
1305 error ("Bb %d do not belong to loop %d.",
1306 bbs[j]->index, i);
1307 err = 1;
1308 }
1309 free (bbs);
1310 }
1311
1312 /* Check headers and latches. */
1313 for (i = 1; i < loops->num; i++)
1314 {
1315 loop = loops->parray[i];
1316 if (!loop)
1317 continue;
1318
1319 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1320 && (!loop->header->pred->pred_next
1321 || loop->header->pred->pred_next->pred_next))
1322 {
1323 error ("Loop %d's header does not have exactly 2 entries.", i);
1324 err = 1;
1325 }
1326 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1327 {
1328 if (!loop->latch->succ
1329 || loop->latch->succ->succ_next)
1330 {
1331 error ("Loop %d's latch does not have exactly 1 successor.", i);
1332 err = 1;
1333 }
1334 if (loop->latch->succ->dest != loop->header)
1335 {
1336 error ("Loop %d's latch does not have header as successor.", i);
1337 err = 1;
1338 }
1339 if (loop->latch->loop_father != loop)
1340 {
1341 error ("Loop %d's latch does not belong directly to it.", i);
1342 err = 1;
1343 }
1344 }
1345 if (loop->header->loop_father != loop)
1346 {
1347 error ("Loop %d's header does not belong directly to it.", i);
1348 err = 1;
1349 }
1350 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1351 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1352 {
1353 error ("Loop %d's latch is marked as part of irreducible region.", i);
1354 err = 1;
1355 }
1356 }
1357
1358 /* Check irreducible loops. */
1359 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1360 {
1361 /* Record old info. */
1362 irreds = sbitmap_alloc (last_basic_block);
1363 FOR_EACH_BB (bb)
1364 {
1365 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1366 SET_BIT (irreds, bb->index);
1367 else
1368 RESET_BIT (irreds, bb->index);
1369 for (e = bb->succ; e; e = e->succ_next)
1370 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1371 e->flags |= EDGE_ALL_FLAGS + 1;
1372 }
1373
1374 /* Recount it. */
1375 mark_irreducible_loops (loops);
1376
1377 /* Compare. */
1378 FOR_EACH_BB (bb)
1379 {
1380 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1381 && !TEST_BIT (irreds, bb->index))
1382 {
1383 error ("Basic block %d should be marked irreducible.", bb->index);
1384 err = 1;
1385 }
1386 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1387 && TEST_BIT (irreds, bb->index))
1388 {
1389 error ("Basic block %d should not be marked irreducible.", bb->index);
1390 err = 1;
1391 }
1392 for (e = bb->succ; e; e = e->succ_next)
1393 {
1394 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1395 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1396 {
1397 error ("Edge from %d to %d should be marked irreducible.",
1398 e->src->index, e->dest->index);
1399 err = 1;
1400 }
1401 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1402 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1403 {
1404 error ("Edge from %d to %d should not be marked irreducible.",
1405 e->src->index, e->dest->index);
1406 err = 1;
1407 }
1408 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1409 }
1410 }
1411 free (irreds);
1412 }
1413
1414 /* Check the single_exit. */
1415 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1416 {
1417 memset (sizes, 0, sizeof (unsigned) * loops->num);
1418 FOR_EACH_BB (bb)
1419 {
1420 if (bb->loop_father == loops->tree_root)
1421 continue;
1422 for (e = bb->succ; e; e = e->succ_next)
1423 {
1424 if (e->dest == EXIT_BLOCK_PTR)
1425 continue;
1426
1427 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1428 continue;
1429
1430 for (loop = bb->loop_father;
1431 loop != e->dest->loop_father;
1432 loop = loop->outer)
1433 {
1434 sizes[loop->num]++;
1435 if (loop->single_exit
1436 && loop->single_exit != e)
1437 {
1438 error ("Wrong single exit %d->%d recorded for loop %d.",
1439 loop->single_exit->src->index,
1440 loop->single_exit->dest->index,
1441 loop->num);
1442 error ("Right exit is %d->%d.",
1443 e->src->index, e->dest->index);
1444 err = 1;
1445 }
1446 }
1447 }
1448 }
1449
1450 for (i = 1; i < loops->num; i++)
1451 {
1452 loop = loops->parray[i];
1453 if (!loop)
1454 continue;
1455
1456 if (sizes[i] == 1
1457 && !loop->single_exit)
1458 {
1459 error ("Single exit not recorded for loop %d.", loop->num);
1460 err = 1;
1461 }
1462
1463 if (sizes[i] != 1
1464 && loop->single_exit)
1465 {
1466 error ("Loop %d should not have single exit (%d -> %d).",
1467 loop->num,
1468 loop->single_exit->src->index,
1469 loop->single_exit->dest->index);
1470 err = 1;
1471 }
1472 }
1473 }
1474
1475 gcc_assert (!err);
1476
1477 free (sizes);
1478 }
1479
1480 /* Returns latch edge of LOOP. */
1481 edge
1482 loop_latch_edge (const struct loop *loop)
1483 {
1484 edge e;
1485
1486 for (e = loop->header->pred; e->src != loop->latch; e = e->pred_next)
1487 continue;
1488
1489 return e;
1490 }
1491
1492 /* Returns preheader edge of LOOP. */
1493 edge
1494 loop_preheader_edge (const struct loop *loop)
1495 {
1496 edge e;
1497
1498 for (e = loop->header->pred; e->src == loop->latch; e = e->pred_next)
1499 continue;
1500
1501 return e;
1502 }