OSDN Git Service

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