OSDN Git Service

* g++.dg/other/static11.C: Use cleanup-rtl-dump.
[pf3gnuchains/gcc-fork.git] / gcc / cfgloopmanip.c
1 /* Loop manipulation code for GNU compiler.
2    Copyright (C) 2002, 2003, 2004, 2005 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 "obstack.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "cfglayout.h"
31 #include "output.h"
32
33 static void duplicate_subloops (struct loops *, struct loop *, struct loop *);
34 static void copy_loops_to (struct loops *, struct loop **, int,
35                            struct loop *);
36 static void loop_redirect_edge (edge, basic_block);
37 static bool loop_delete_branch_edge (edge, int);
38 static void remove_bbs (basic_block *, int);
39 static bool rpe_enum_p (basic_block, void *);
40 static int find_path (edge, basic_block **);
41 static bool alp_enum_p (basic_block, void *);
42 static void add_loop (struct loops *, struct loop *);
43 static void fix_loop_placements (struct loops *, struct loop *);
44 static bool fix_bb_placement (struct loops *, basic_block);
45 static void fix_bb_placements (struct loops *, basic_block);
46 static void place_new_loop (struct loops *, struct loop *);
47 static void scale_loop_frequencies (struct loop *, int, int);
48 static basic_block create_preheader (struct loop *, int);
49 static void fix_irreducible_loops (basic_block);
50 static void unloop (struct loops *, struct loop *);
51
52 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
53
54 /* Splits basic block BB after INSN, returns created edge.  Updates loops
55    and dominators.  */
56 edge
57 split_loop_bb (basic_block bb, void *insn)
58 {
59   edge e;
60
61   /* Split the block.  */
62   e = split_block (bb, insn);
63
64   /* Add dest to loop.  */
65   add_bb_to_loop (e->dest, e->src->loop_father);
66
67   return e;
68 }
69
70 /* Checks whether basic block BB is dominated by DATA.  */
71 static bool
72 rpe_enum_p (basic_block bb, void *data)
73 {
74   return dominated_by_p (CDI_DOMINATORS, bb, data);
75 }
76
77 /* Remove basic blocks BBS from loop structure and dominance info,
78    and delete them afterwards.  */
79 static void
80 remove_bbs (basic_block *bbs, int nbbs)
81 {
82   int i;
83
84   for (i = 0; i < nbbs; i++)
85     {
86       remove_bb_from_loops (bbs[i]);
87       delete_basic_block (bbs[i]);
88     }
89 }
90
91 /* Find path -- i.e. the basic blocks dominated by edge E and put them
92    into array BBS, that will be allocated large enough to contain them.
93    E->dest must have exactly one predecessor for this to work (it is
94    easy to achieve and we do not put it here because we do not want to
95    alter anything by this function).  The number of basic blocks in the
96    path is returned.  */
97 static int
98 find_path (edge e, basic_block **bbs)
99 {
100   gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
101
102   /* Find bbs in the path.  */
103   *bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
104   return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs,
105                              n_basic_blocks, e->dest);
106 }
107
108 /* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
109    Let L be a loop to that BB belongs.  Then every successor of BB must either
110      1) belong to some superloop of loop L, or
111      2) be a header of loop K such that K->outer is superloop of L
112    Returns true if we had to move BB into other loop to enforce this condition,
113    false if the placement of BB was already correct (provided that placements
114    of its successors are correct).  */
115 static bool
116 fix_bb_placement (struct loops *loops, basic_block bb)
117 {
118   edge e;
119   edge_iterator ei;
120   struct loop *loop = loops->tree_root, *act;
121
122   FOR_EACH_EDGE (e, ei, bb->succs)
123     {
124       if (e->dest == EXIT_BLOCK_PTR)
125         continue;
126
127       act = e->dest->loop_father;
128       if (act->header == e->dest)
129         act = act->outer;
130
131       if (flow_loop_nested_p (loop, act))
132         loop = act;
133     }
134
135   if (loop == bb->loop_father)
136     return false;
137
138   remove_bb_from_loops (bb);
139   add_bb_to_loop (bb, loop);
140
141   return true;
142 }
143
144 /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
145    enforce condition condition stated in description of fix_bb_placement. We
146    start from basic block FROM that had some of its successors removed, so that
147    his placement no longer has to be correct, and iteratively fix placement of
148    its predecessors that may change if placement of FROM changed.  Also fix
149    placement of subloops of FROM->loop_father, that might also be altered due
150    to this change; the condition for them is similar, except that instead of
151    successors we consider edges coming out of the loops.  */
152 static void
153 fix_bb_placements (struct loops *loops, basic_block from)
154 {
155   sbitmap in_queue;
156   basic_block *queue, *qtop, *qbeg, *qend;
157   struct loop *base_loop;
158   edge e;
159
160   /* We pass through blocks back-reachable from FROM, testing whether some
161      of their successors moved to outer loop.  It may be necessary to
162      iterate several times, but it is finite, as we stop unless we move
163      the basic block up the loop structure.  The whole story is a bit
164      more complicated due to presence of subloops, those are moved using
165      fix_loop_placement.  */
166
167   base_loop = from->loop_father;
168   if (base_loop == loops->tree_root)
169     return;
170
171   in_queue = sbitmap_alloc (last_basic_block);
172   sbitmap_zero (in_queue);
173   SET_BIT (in_queue, from->index);
174   /* Prevent us from going out of the base_loop.  */
175   SET_BIT (in_queue, base_loop->header->index);
176
177   queue = xmalloc ((base_loop->num_nodes + 1) * sizeof (basic_block));
178   qtop = queue + base_loop->num_nodes + 1;
179   qbeg = queue;
180   qend = queue + 1;
181   *qbeg = from;
182
183   while (qbeg != qend)
184     {
185       edge_iterator ei;
186       from = *qbeg;
187       qbeg++;
188       if (qbeg == qtop)
189         qbeg = queue;
190       RESET_BIT (in_queue, from->index);
191
192       if (from->loop_father->header == from)
193         {
194           /* Subloop header, maybe move the loop upward.  */
195           if (!fix_loop_placement (from->loop_father))
196             continue;
197         }
198       else
199         {
200           /* Ordinary basic block.  */
201           if (!fix_bb_placement (loops, from))
202             continue;
203         }
204
205       /* Something has changed, insert predecessors into queue.  */
206       FOR_EACH_EDGE (e, ei, from->preds)
207         {
208           basic_block pred = e->src;
209           struct loop *nca;
210
211           if (TEST_BIT (in_queue, pred->index))
212             continue;
213
214           /* If it is subloop, then it either was not moved, or
215              the path up the loop tree from base_loop do not contain
216              it.  */
217           nca = find_common_loop (pred->loop_father, base_loop);
218           if (pred->loop_father != base_loop
219               && (nca == base_loop
220                   || nca != pred->loop_father))
221             pred = pred->loop_father->header;
222           else if (!flow_loop_nested_p (from->loop_father, pred->loop_father))
223             {
224               /* No point in processing it.  */
225               continue;
226             }
227
228           if (TEST_BIT (in_queue, pred->index))
229             continue;
230
231           /* Schedule the basic block.  */
232           *qend = pred;
233           qend++;
234           if (qend == qtop)
235             qend = queue;
236           SET_BIT (in_queue, pred->index);
237         }
238     }
239   free (in_queue);
240   free (queue);
241 }
242
243 /* Basic block from has lost one or more of its predecessors, so it might
244    mo longer be part irreducible loop.  Fix it and proceed recursively
245    for its successors if needed.  */
246 static void
247 fix_irreducible_loops (basic_block from)
248 {
249   basic_block bb;
250   basic_block *stack;
251   int stack_top;
252   sbitmap on_stack;
253   edge *edges, e;
254   unsigned n_edges, i;
255
256   if (!(from->flags & BB_IRREDUCIBLE_LOOP))
257     return;
258
259   on_stack = sbitmap_alloc (last_basic_block);
260   sbitmap_zero (on_stack);
261   SET_BIT (on_stack, from->index);
262   stack = xmalloc (from->loop_father->num_nodes * sizeof (basic_block));
263   stack[0] = from;
264   stack_top = 1;
265
266   while (stack_top)
267     {
268       edge_iterator ei;
269       bb = stack[--stack_top];
270       RESET_BIT (on_stack, bb->index);
271
272       FOR_EACH_EDGE (e, ei, bb->preds)
273         if (e->flags & EDGE_IRREDUCIBLE_LOOP)
274           break;
275       if (e)
276         continue;
277
278       bb->flags &= ~BB_IRREDUCIBLE_LOOP;
279       if (bb->loop_father->header == bb)
280         edges = get_loop_exit_edges (bb->loop_father, &n_edges);
281       else
282         {
283           n_edges = EDGE_COUNT (bb->succs);
284           edges = xmalloc (n_edges * sizeof (edge));
285           FOR_EACH_EDGE (e, ei, bb->succs)
286             edges[ei.index] = e;
287         }
288
289       for (i = 0; i < n_edges; i++)
290         {
291           e = edges[i];
292
293           if (e->flags & EDGE_IRREDUCIBLE_LOOP)
294             {
295               if (!flow_bb_inside_loop_p (from->loop_father, e->dest))
296                 continue;
297
298               e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
299               if (TEST_BIT (on_stack, e->dest->index))
300                 continue;
301
302               SET_BIT (on_stack, e->dest->index);
303               stack[stack_top++] = e->dest;
304             }
305         }
306       free (edges);
307     }
308
309   free (on_stack);
310   free (stack);
311 }
312
313 /* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
314    and update loop structure stored in LOOPS and dominators.  Return true if
315    we were able to remove the path, false otherwise (and nothing is affected
316    then).  */
317 bool
318 remove_path (struct loops *loops, edge e)
319 {
320   edge ae;
321   basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb;
322   int i, nrem, n_bord_bbs, n_dom_bbs;
323   sbitmap seen;
324   bool deleted;
325
326   if (!loop_delete_branch_edge (e, 0))
327     return false;
328
329   /* We need to check whether basic blocks are dominated by the edge
330      e, but we only have basic block dominators.  This is easy to
331      fix -- when e->dest has exactly one predecessor, this corresponds
332      to blocks dominated by e->dest, if not, split the edge.  */
333   if (!single_pred_p (e->dest))
334     e = single_pred_edge (loop_split_edge_with (e, NULL_RTX));
335
336   /* It may happen that by removing path we remove one or more loops
337      we belong to.  In this case first unloop the loops, then proceed
338      normally.   We may assume that e->dest is not a header of any loop,
339      as it now has exactly one predecessor.  */
340   while (e->src->loop_father->outer
341          && dominated_by_p (CDI_DOMINATORS,
342                             e->src->loop_father->latch, e->dest))
343     unloop (loops, e->src->loop_father);
344
345   /* Identify the path.  */
346   nrem = find_path (e, &rem_bbs);
347
348   n_bord_bbs = 0;
349   bord_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
350   seen = sbitmap_alloc (last_basic_block);
351   sbitmap_zero (seen);
352
353   /* Find "border" hexes -- i.e. those with predecessor in removed path.  */
354   for (i = 0; i < nrem; i++)
355     SET_BIT (seen, rem_bbs[i]->index);
356   for (i = 0; i < nrem; i++)
357     {
358       edge_iterator ei;
359       bb = rem_bbs[i];
360       FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
361         if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
362           {
363             SET_BIT (seen, ae->dest->index);
364             bord_bbs[n_bord_bbs++] = ae->dest;
365           }
366     }
367
368   /* Remove the path.  */
369   from = e->src;
370   deleted = loop_delete_branch_edge (e, 1);
371   gcc_assert (deleted);
372   dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
373
374   /* Cancel loops contained in the path.  */
375   for (i = 0; i < nrem; i++)
376     if (rem_bbs[i]->loop_father->header == rem_bbs[i])
377       cancel_loop_tree (loops, rem_bbs[i]->loop_father);
378
379   remove_bbs (rem_bbs, nrem);
380   free (rem_bbs);
381
382   /* Find blocks whose dominators may be affected.  */
383   n_dom_bbs = 0;
384   sbitmap_zero (seen);
385   for (i = 0; i < n_bord_bbs; i++)
386     {
387       basic_block ldom;
388
389       bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
390       if (TEST_BIT (seen, bb->index))
391         continue;
392       SET_BIT (seen, bb->index);
393
394       for (ldom = first_dom_son (CDI_DOMINATORS, bb);
395            ldom;
396            ldom = next_dom_son (CDI_DOMINATORS, ldom))
397         if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
398           dom_bbs[n_dom_bbs++] = ldom;
399     }
400
401   free (seen);
402
403   /* Recount dominators.  */
404   iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
405   free (dom_bbs);
406
407   /* These blocks have lost some predecessor(s), thus their irreducible
408      status could be changed.  */
409   for (i = 0; i < n_bord_bbs; i++)
410     fix_irreducible_loops (bord_bbs[i]);
411   free (bord_bbs);
412
413   /* Fix placements of basic blocks inside loops and the placement of
414      loops in the loop tree.  */
415   fix_bb_placements (loops, from);
416   fix_loop_placements (loops, from->loop_father);
417
418   return true;
419 }
420
421 /* Predicate for enumeration in add_loop.  */
422 static bool
423 alp_enum_p (basic_block bb, void *alp_header)
424 {
425   return bb != (basic_block) alp_header;
426 }
427
428 /* Given LOOP structure with filled header and latch, find the body of the
429    corresponding loop and add it to LOOPS tree.  */
430 static void
431 add_loop (struct loops *loops, struct loop *loop)
432 {
433   basic_block *bbs;
434   int i, n;
435
436   /* Add it to loop structure.  */
437   place_new_loop (loops, loop);
438   loop->level = 1;
439
440   /* Find its nodes.  */
441   bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
442   n = dfs_enumerate_from (loop->latch, 1, alp_enum_p,
443                           bbs, n_basic_blocks, loop->header);
444
445   for (i = 0; i < n; i++)
446     add_bb_to_loop (bbs[i], loop);
447   add_bb_to_loop (loop->header, loop);
448
449   free (bbs);
450 }
451
452 /* Multiply all frequencies in LOOP by NUM/DEN.  */
453 static void
454 scale_loop_frequencies (struct loop *loop, int num, int den)
455 {
456   basic_block *bbs;
457
458   bbs = get_loop_body (loop);
459   scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
460   free (bbs);
461 }
462
463 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
464    latch to header and update loop tree stored in LOOPS and dominators
465    accordingly. Everything between them plus LATCH_EDGE destination must
466    be dominated by HEADER_EDGE destination, and back-reachable from
467    LATCH_EDGE source.  HEADER_EDGE is redirected to basic block SWITCH_BB,
468    FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
469    TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
470    Returns newly created loop.  */
471
472 struct loop *
473 loopify (struct loops *loops, edge latch_edge, edge header_edge, 
474          basic_block switch_bb, edge true_edge, edge false_edge,
475          bool redirect_all_edges)
476 {
477   basic_block succ_bb = latch_edge->dest;
478   basic_block pred_bb = header_edge->src;
479   basic_block *dom_bbs, *body;
480   unsigned n_dom_bbs, i;
481   sbitmap seen;
482   struct loop *loop = xcalloc (1, sizeof (struct loop));
483   struct loop *outer = succ_bb->loop_father->outer;
484   int freq, prob, tot_prob;
485   gcov_type cnt;
486   edge e;
487   edge_iterator ei;
488
489   loop->header = header_edge->dest;
490   loop->latch = latch_edge->src;
491
492   freq = EDGE_FREQUENCY (header_edge);
493   cnt = header_edge->count;
494   prob = EDGE_SUCC (switch_bb, 0)->probability;
495   tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
496   if (tot_prob == 0)
497     tot_prob = 1;
498
499   /* Redirect edges.  */
500   loop_redirect_edge (latch_edge, loop->header);
501   loop_redirect_edge (true_edge, succ_bb);
502
503   /* During loop versioning, one of the switch_bb edge is already properly
504      set. Do not redirect it again unless redirect_all_edges is true.  */
505   if (redirect_all_edges)
506     {
507       loop_redirect_edge (header_edge, switch_bb);
508       loop_redirect_edge (false_edge, loop->header); 
509      
510       /* Update dominators.  */
511       set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
512       set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
513     }
514
515   set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
516
517   /* Compute new loop.  */
518   add_loop (loops, loop);
519   flow_loop_tree_node_add (outer, loop);
520
521   /* Add switch_bb to appropriate loop.  */
522   add_bb_to_loop (switch_bb, outer);
523
524   /* Fix frequencies.  */
525   switch_bb->frequency = freq;
526   switch_bb->count = cnt;
527   FOR_EACH_EDGE (e, ei, switch_bb->succs)
528     e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
529   scale_loop_frequencies (loop, prob, tot_prob);
530   scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
531
532   /* Update dominators of blocks outside of LOOP.  */
533   dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
534   n_dom_bbs = 0;
535   seen = sbitmap_alloc (last_basic_block);
536   sbitmap_zero (seen);
537   body = get_loop_body (loop);
538
539   for (i = 0; i < loop->num_nodes; i++)
540     SET_BIT (seen, body[i]->index);
541
542   for (i = 0; i < loop->num_nodes; i++)
543     {
544       basic_block ldom;
545
546       for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
547            ldom;
548            ldom = next_dom_son (CDI_DOMINATORS, ldom))
549         if (!TEST_BIT (seen, ldom->index))
550           {
551             SET_BIT (seen, ldom->index);
552             dom_bbs[n_dom_bbs++] = ldom;
553           }
554     }
555
556   iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
557
558   free (body);
559   free (seen);
560   free (dom_bbs);
561
562   return loop;
563 }
564
565 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
566    the LOOP was removed.  After this function, original loop latch will
567    have no successor, which caller is expected to fix somehow.  */
568 static void
569 unloop (struct loops *loops, struct loop *loop)
570 {
571   basic_block *body;
572   struct loop *ploop;
573   unsigned i, n;
574   basic_block latch = loop->latch;
575   edge *edges;
576   unsigned n_edges;
577
578   /* This is relatively straightforward.  The dominators are unchanged, as
579      loop header dominates loop latch, so the only thing we have to care of
580      is the placement of loops and basic blocks inside the loop tree.  We
581      move them all to the loop->outer, and then let fix_bb_placements do
582      its work.  */
583
584   body = get_loop_body (loop);
585   edges = get_loop_exit_edges (loop, &n_edges);
586   n = loop->num_nodes;
587   for (i = 0; i < n; i++)
588     if (body[i]->loop_father == loop)
589       {
590         remove_bb_from_loops (body[i]);
591         add_bb_to_loop (body[i], loop->outer);
592       }
593   free(body);
594
595   while (loop->inner)
596     {
597       ploop = loop->inner;
598       flow_loop_tree_node_remove (ploop);
599       flow_loop_tree_node_add (loop->outer, ploop);
600     }
601
602   /* Remove the loop and free its data.  */
603   flow_loop_tree_node_remove (loop);
604   loops->parray[loop->num] = NULL;
605   flow_loop_free (loop);
606
607   remove_edge (single_succ_edge (latch));
608   fix_bb_placements (loops, latch);
609
610   /* If the loop was inside an irreducible region, we would have to somehow
611      update the irreducible marks inside its body.  While it is certainly
612      possible to do, it is a bit complicated and this situation should be
613      very rare, so we just remark all loops in this case.  */
614   for (i = 0; i < n_edges; i++)
615     if (edges[i]->flags & EDGE_IRREDUCIBLE_LOOP)
616       break;
617   if (i != n_edges)
618     mark_irreducible_loops (loops);
619   free (edges);
620 }
621
622 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
623    FATHER of LOOP such that all of the edges coming out of LOOP belong to
624    FATHER, and set it as outer loop of LOOP.  Return 1 if placement of
625    LOOP changed.  */
626 int
627 fix_loop_placement (struct loop *loop)
628 {
629   basic_block *body;
630   unsigned i;
631   edge e;
632   edge_iterator ei;
633   struct loop *father = loop->pred[0], *act;
634
635   body = get_loop_body (loop);
636   for (i = 0; i < loop->num_nodes; i++)
637     FOR_EACH_EDGE (e, ei, body[i]->succs)
638       if (!flow_bb_inside_loop_p (loop, e->dest))
639         {
640           act = find_common_loop (loop, e->dest->loop_father);
641           if (flow_loop_nested_p (father, act))
642             father = act;
643         }
644   free (body);
645
646   if (father != loop->outer)
647     {
648       for (act = loop->outer; act != father; act = act->outer)
649         act->num_nodes -= loop->num_nodes;
650       flow_loop_tree_node_remove (loop);
651       flow_loop_tree_node_add (father, loop);
652       return 1;
653     }
654   return 0;
655 }
656
657 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
658    condition stated in description of fix_loop_placement holds for them.
659    It is used in case when we removed some edges coming out of LOOP, which
660    may cause the right placement of LOOP inside loop tree to change.  */
661 static void
662 fix_loop_placements (struct loops *loops, struct loop *loop)
663 {
664   struct loop *outer;
665
666   while (loop->outer)
667     {
668       outer = loop->outer;
669       if (!fix_loop_placement (loop))
670         break;
671
672       /* Changing the placement of a loop in the loop tree may alter the
673          validity of condition 2) of the description of fix_bb_placement
674          for its preheader, because the successor is the header and belongs
675          to the loop.  So call fix_bb_placements to fix up the placement
676          of the preheader and (possibly) of its predecessors.  */
677       fix_bb_placements (loops, loop_preheader_edge (loop)->src);
678       loop = outer;
679     }
680 }
681
682 /* Creates place for a new LOOP in LOOPS structure.  */
683 static void
684 place_new_loop (struct loops *loops, struct loop *loop)
685 {
686   loops->parray =
687     xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
688   loops->parray[loops->num] = loop;
689
690   loop->num = loops->num++;
691 }
692
693 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
694    created loop into LOOPS structure.  */
695 struct loop *
696 duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
697 {
698   struct loop *cloop;
699   cloop = xcalloc (1, sizeof (struct loop));
700   place_new_loop (loops, cloop);
701
702   /* Initialize copied loop.  */
703   cloop->level = loop->level;
704
705   /* Set it as copy of loop.  */
706   loop->copy = cloop;
707
708   /* Add it to target.  */
709   flow_loop_tree_node_add (target, cloop);
710
711   return cloop;
712 }
713
714 /* Copies structure of subloops of LOOP into TARGET loop, placing
715    newly created loops into loop tree stored in LOOPS.  */
716 static void
717 duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
718 {
719   struct loop *aloop, *cloop;
720
721   for (aloop = loop->inner; aloop; aloop = aloop->next)
722     {
723       cloop = duplicate_loop (loops, aloop, target);
724       duplicate_subloops (loops, aloop, cloop);
725     }
726 }
727
728 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
729    into TARGET loop, placing newly created loops into loop tree LOOPS.  */
730 static void
731 copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
732 {
733   struct loop *aloop;
734   int i;
735
736   for (i = 0; i < n; i++)
737     {
738       aloop = duplicate_loop (loops, copied_loops[i], target);
739       duplicate_subloops (loops, copied_loops[i], aloop);
740     }
741 }
742
743 /* Redirects edge E to basic block DEST.  */
744 static void
745 loop_redirect_edge (edge e, basic_block dest)
746 {
747   if (e->dest == dest)
748     return;
749
750   redirect_edge_and_branch_force (e, dest);
751 }
752
753 /* Deletes edge E from a branch if possible.  Unless REALLY_DELETE is set,
754    just test whether it is possible to remove the edge.  */
755 static bool
756 loop_delete_branch_edge (edge e, int really_delete)
757 {
758   basic_block src = e->src;
759   basic_block newdest;
760   int irr;
761   edge snd;
762
763   gcc_assert (EDGE_COUNT (src->succs) > 1);
764   
765   /* Cannot handle more than two exit edges.  */
766   if (EDGE_COUNT (src->succs) > 2)
767     return false;
768   /* And it must be just a simple branch.  */
769   if (!any_condjump_p (BB_END (src)))
770     return false;
771
772   snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
773   newdest = snd->dest;
774   if (newdest == EXIT_BLOCK_PTR)
775     return false;
776
777   /* Hopefully the above conditions should suffice.  */
778   if (!really_delete)
779     return true;
780
781   /* Redirecting behaves wrongly wrto this flag.  */
782   irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
783
784   if (!redirect_edge_and_branch (e, newdest))
785     return false;
786   single_succ_edge (src)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
787   single_succ_edge (src)->flags |= irr;
788   
789   return true;
790 }
791
792 /* Check whether LOOP's body can be duplicated.  */
793 bool
794 can_duplicate_loop_p (struct loop *loop)
795 {
796   int ret;
797   basic_block *bbs = get_loop_body (loop);
798
799   ret = can_copy_bbs_p (bbs, loop->num_nodes);
800   free (bbs);
801   
802   return ret;
803 }
804
805 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
806    to LOOP.  Update the single_exit information in superloops of LOOP.  */
807
808 static void
809 update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
810                                        struct loop *loop)
811 {
812   unsigned i;
813
814   for (i = 0; i < nbbs; i++)
815     bbs[i]->rbi->duplicated = 1;
816
817   for (; loop->outer; loop = loop->outer)
818     {
819       if (!loop->single_exit)
820         continue;
821
822       if (loop->single_exit->src->rbi->duplicated)
823         loop->single_exit = NULL;
824     }
825
826   for (i = 0; i < nbbs; i++)
827     bbs[i]->rbi->duplicated = 0;
828 }
829
830 /* Duplicates body of LOOP to given edge E NDUPL times.  Takes care of updating
831    LOOPS structure and dominators.  E's destination must be LOOP header for
832    this to work, i.e. it must be entry or latch edge of this loop; these are
833    unique, as the loops must have preheaders for this function to work
834    correctly (in case E is latch, the function unrolls the loop, if E is entry
835    edge, it peels the loop).  Store edges created by copying ORIG edge from
836    copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
837    original LOOP body, the other copies are numbered in order given by control
838    flow through them) into TO_REMOVE array.  Returns false if duplication is
839    impossible.  */
840 int
841 duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
842                                unsigned int ndupl, sbitmap wont_exit,
843                                edge orig, edge *to_remove,
844                                unsigned int *n_to_remove, int flags)
845 {
846   struct loop *target, *aloop;
847   struct loop **orig_loops;
848   unsigned n_orig_loops;
849   basic_block header = loop->header, latch = loop->latch;
850   basic_block *new_bbs, *bbs, *first_active;
851   basic_block new_bb, bb, first_active_latch = NULL;
852   edge ae, latch_edge;
853   edge spec_edges[2], new_spec_edges[2];
854 #define SE_LATCH 0
855 #define SE_ORIG 1
856   unsigned i, j, n;
857   int is_latch = (latch == e->src);
858   int scale_act = 0, *scale_step = NULL, scale_main = 0;
859   int p, freq_in, freq_le, freq_out_orig;
860   int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
861   int add_irreducible_flag;
862
863   gcc_assert (e->dest == loop->header);
864   gcc_assert (ndupl > 0);
865
866   if (orig)
867     {
868       /* Orig must be edge out of the loop.  */
869       gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
870       gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
871     }
872
873   bbs = get_loop_body (loop);
874
875   /* Check whether duplication is possible.  */
876   if (!can_copy_bbs_p (bbs, loop->num_nodes))
877     {
878       free (bbs);
879       return false;
880     }
881   new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes);
882
883   /* In case we are doing loop peeling and the loop is in the middle of
884      irreducible region, the peeled copies will be inside it too.  */
885   add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
886   gcc_assert (!is_latch || !add_irreducible_flag);
887
888   /* Find edge from latch.  */
889   latch_edge = loop_latch_edge (loop);
890
891   if (flags & DLTHE_FLAG_UPDATE_FREQ)
892     {
893       /* Calculate coefficients by that we have to scale frequencies
894          of duplicated loop bodies.  */
895       freq_in = header->frequency;
896       freq_le = EDGE_FREQUENCY (latch_edge);
897       if (freq_in == 0)
898         freq_in = 1;
899       if (freq_in < freq_le)
900         freq_in = freq_le;
901       freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
902       if (freq_out_orig > freq_in - freq_le)
903         freq_out_orig = freq_in - freq_le;
904       prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
905       prob_pass_wont_exit =
906               RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
907
908       scale_step = xmalloc (ndupl * sizeof (int));
909
910         for (i = 1; i <= ndupl; i++)
911           scale_step[i - 1] = TEST_BIT (wont_exit, i)
912                                 ? prob_pass_wont_exit
913                                 : prob_pass_thru;
914
915       if (is_latch)
916         {
917           prob_pass_main = TEST_BIT (wont_exit, 0)
918                                 ? prob_pass_wont_exit
919                                 : prob_pass_thru;
920           p = prob_pass_main;
921           scale_main = REG_BR_PROB_BASE;
922           for (i = 0; i < ndupl; i++)
923             {
924               scale_main += p;
925               p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
926             }
927           scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
928           scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
929         }
930       else
931         {
932           scale_main = REG_BR_PROB_BASE;
933           for (i = 0; i < ndupl; i++)
934             scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
935           scale_act = REG_BR_PROB_BASE - prob_pass_thru;
936         }
937       for (i = 0; i < ndupl; i++)
938         gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
939       gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
940                   && scale_act >= 0  && scale_act <= REG_BR_PROB_BASE);
941     }
942
943   /* Loop the new bbs will belong to.  */
944   target = e->src->loop_father;
945
946   /* Original loops.  */
947   n_orig_loops = 0;
948   for (aloop = loop->inner; aloop; aloop = aloop->next)
949     n_orig_loops++;
950   orig_loops = xcalloc (n_orig_loops, sizeof (struct loop *));
951   for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
952     orig_loops[i] = aloop;
953
954   loop->copy = target;
955
956   n = loop->num_nodes;
957
958   first_active = xmalloc (n * sizeof (basic_block));
959   if (is_latch)
960     {
961       memcpy (first_active, bbs, n * sizeof (basic_block));
962       first_active_latch = latch;
963     }
964
965   /* Update the information about single exits.  */
966   if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
967     update_single_exits_after_duplication (bbs, n, target);
968
969   /* Record exit edge in original loop body.  */
970   if (orig && TEST_BIT (wont_exit, 0))
971     to_remove[(*n_to_remove)++] = orig;
972
973   spec_edges[SE_ORIG] = orig;
974   spec_edges[SE_LATCH] = latch_edge;
975
976   for (j = 0; j < ndupl; j++)
977     {
978       /* Copy loops.  */
979       copy_loops_to (loops, orig_loops, n_orig_loops, target);
980
981       /* Copy bbs.  */
982       copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop);
983
984       for (i = 0; i < n; i++)
985         new_bbs[i]->rbi->copy_number = j + 1;
986
987       /* Note whether the blocks and edges belong to an irreducible loop.  */
988       if (add_irreducible_flag)
989         {
990           for (i = 0; i < n; i++)
991             new_bbs[i]->rbi->duplicated = 1;
992           for (i = 0; i < n; i++)
993             {
994               edge_iterator ei;
995               new_bb = new_bbs[i];
996               if (new_bb->loop_father == target)
997                 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
998
999               FOR_EACH_EDGE (ae, ei, new_bb->succs)
1000                 if (ae->dest->rbi->duplicated
1001                     && (ae->src->loop_father == target
1002                         || ae->dest->loop_father == target))
1003                   ae->flags |= EDGE_IRREDUCIBLE_LOOP;
1004             }
1005           for (i = 0; i < n; i++)
1006             new_bbs[i]->rbi->duplicated = 0;
1007         }
1008
1009       /* Redirect the special edges.  */
1010       if (is_latch)
1011         {
1012           redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
1013           redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1014                                           loop->header);
1015           set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
1016           latch = loop->latch = new_bbs[1];
1017           e = latch_edge = new_spec_edges[SE_LATCH];
1018         }
1019       else
1020         {
1021           redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1022                                           loop->header);
1023           redirect_edge_and_branch_force (e, new_bbs[0]);
1024           set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
1025           e = new_spec_edges[SE_LATCH];
1026         }
1027
1028       /* Record exit edge in this copy.  */
1029       if (orig && TEST_BIT (wont_exit, j + 1))
1030         to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
1031
1032       /* Record the first copy in the control flow order if it is not
1033          the original loop (i.e. in case of peeling).  */
1034       if (!first_active_latch)
1035         {
1036           memcpy (first_active, new_bbs, n * sizeof (basic_block));
1037           first_active_latch = new_bbs[1];
1038         }
1039
1040       /* Set counts and frequencies.  */
1041       if (flags & DLTHE_FLAG_UPDATE_FREQ)
1042         {
1043           scale_bbs_frequencies_int (new_bbs, n, scale_act, REG_BR_PROB_BASE);
1044           scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
1045         }
1046     }
1047   free (new_bbs);
1048   free (orig_loops);
1049   
1050   /* Update the original loop.  */
1051   if (!is_latch)
1052     set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1053   if (flags & DLTHE_FLAG_UPDATE_FREQ)
1054     {
1055       scale_bbs_frequencies_int (bbs, n, scale_main, REG_BR_PROB_BASE);
1056       free (scale_step);
1057     }
1058
1059   /* Update dominators of outer blocks if affected.  */
1060   for (i = 0; i < n; i++)
1061     {
1062       basic_block dominated, dom_bb, *dom_bbs;
1063       int n_dom_bbs,j;
1064
1065       bb = bbs[i];
1066       bb->rbi->copy_number = 0;
1067
1068       n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
1069       for (j = 0; j < n_dom_bbs; j++)
1070         {
1071           dominated = dom_bbs[j];
1072           if (flow_bb_inside_loop_p (loop, dominated))
1073             continue;
1074           dom_bb = nearest_common_dominator (
1075                         CDI_DOMINATORS, first_active[i], first_active_latch);
1076           set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
1077         }
1078       free (dom_bbs);
1079     }
1080   free (first_active);
1081
1082   free (bbs);
1083
1084   return true;
1085 }
1086
1087 /* A callback for make_forwarder block, to redirect all edges except for
1088    MFB_KJ_EDGE to the entry part.  E is the edge for that we should decide
1089    whether to redirect it.  */
1090
1091 static edge mfb_kj_edge;
1092 static bool
1093 mfb_keep_just (edge e)
1094 {
1095   return e != mfb_kj_edge;
1096 }
1097
1098 /* A callback for make_forwarder block, to update data structures for a basic
1099    block JUMP created by redirecting an edge (only the latch edge is being
1100    redirected).  */
1101
1102 static void
1103 mfb_update_loops (basic_block jump)
1104 {
1105   struct loop *loop = single_succ (jump)->loop_father;
1106
1107   if (dom_computed[CDI_DOMINATORS])
1108     set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
1109   add_bb_to_loop (jump, loop);
1110   loop->latch = jump;
1111 }
1112
1113 /* Creates a pre-header for a LOOP.  Returns newly created block.  Unless
1114    CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1115    entry; otherwise we also force preheader block to have only one successor.
1116    The function also updates dominators.  */
1117
1118 static basic_block
1119 create_preheader (struct loop *loop, int flags)
1120 {
1121   edge e, fallthru;
1122   basic_block dummy;
1123   struct loop *cloop, *ploop;
1124   int nentry = 0;
1125   bool irred = false;
1126   bool latch_edge_was_fallthru;
1127   edge one_succ_pred = 0;
1128   edge_iterator ei;
1129
1130   cloop = loop->outer;
1131
1132   FOR_EACH_EDGE (e, ei, loop->header->preds)
1133     {
1134       if (e->src == loop->latch)
1135         continue;
1136       irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
1137       nentry++;
1138       if (single_succ_p (e->src))
1139         one_succ_pred = e;
1140     }
1141   gcc_assert (nentry);
1142   if (nentry == 1)
1143     {
1144       /* Get an edge that is different from the one from loop->latch
1145          to loop->header.  */
1146       e = EDGE_PRED (loop->header,
1147                      EDGE_PRED (loop->header, 0)->src == loop->latch);
1148
1149       if (!(flags & CP_SIMPLE_PREHEADERS) || single_succ_p (e->src))
1150         return NULL;
1151     }
1152
1153   mfb_kj_edge = loop_latch_edge (loop);
1154   latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
1155   fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1156                                    mfb_update_loops);
1157   dummy = fallthru->src;
1158   loop->header = fallthru->dest;
1159
1160   /* The header could be a latch of some superloop(s); due to design of
1161      split_block, it would now move to fallthru->dest.  */
1162   for (ploop = loop; ploop; ploop = ploop->outer)
1163     if (ploop->latch == dummy)
1164       ploop->latch = fallthru->dest;
1165
1166   /* Try to be clever in placing the newly created preheader.  The idea is to
1167      avoid breaking any "fallthruness" relationship between blocks.
1168
1169      The preheader was created just before the header and all incoming edges
1170      to the header were redirected to the preheader, except the latch edge.
1171      So the only problematic case is when this latch edge was a fallthru
1172      edge: it is not anymore after the preheader creation so we have broken
1173      the fallthruness.  We're therefore going to look for a better place.  */
1174   if (latch_edge_was_fallthru)
1175     {
1176       if (one_succ_pred)
1177         e = one_succ_pred;
1178       else
1179         e = EDGE_PRED (dummy, 0);
1180
1181       move_block_after (dummy, e->src);
1182     }
1183
1184   loop->header->loop_father = loop;
1185   add_bb_to_loop (dummy, cloop);
1186
1187   if (irred)
1188     {
1189       dummy->flags |= BB_IRREDUCIBLE_LOOP;
1190       single_succ_edge (dummy)->flags |= EDGE_IRREDUCIBLE_LOOP;
1191     }
1192
1193   if (dump_file)
1194     fprintf (dump_file, "Created preheader block for loop %i\n",
1195              loop->num);
1196
1197   return dummy;
1198 }
1199
1200 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1201    of FLAGS see create_preheader.  */
1202 void
1203 create_preheaders (struct loops *loops, int flags)
1204 {
1205   unsigned i;
1206   for (i = 1; i < loops->num; i++)
1207     create_preheader (loops->parray[i], flags);
1208   loops->state |= LOOPS_HAVE_PREHEADERS;
1209 }
1210
1211 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1212    successor.  */
1213 void
1214 force_single_succ_latches (struct loops *loops)
1215 {
1216   unsigned i;
1217   struct loop *loop;
1218   edge e;
1219
1220   for (i = 1; i < loops->num; i++)
1221     {
1222       loop = loops->parray[i];
1223       if (loop->latch != loop->header && single_succ_p (loop->latch))
1224         continue;
1225
1226       e = find_edge (loop->latch, loop->header);
1227
1228       loop_split_edge_with (e, NULL_RTX);
1229     }
1230   loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1231 }
1232
1233 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1234    just one basic block.  Jumps in INSNS are not handled, so cfg do not have to
1235    be ok after this function.  The created block is placed on correct place
1236    in LOOPS structure and its dominator is set.  */
1237 basic_block
1238 loop_split_edge_with (edge e, rtx insns)
1239 {
1240   basic_block src, dest, new_bb;
1241   struct loop *loop_c;
1242
1243   src = e->src;
1244   dest = e->dest;
1245
1246   loop_c = find_common_loop (src->loop_father, dest->loop_father);
1247
1248   /* Create basic block for it.  */
1249
1250   new_bb = split_edge (e);
1251   add_bb_to_loop (new_bb, loop_c);
1252   new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
1253
1254   if (insns)
1255     emit_insn_after (insns, BB_END (new_bb));
1256
1257   if (dest->loop_father->latch == src)
1258     dest->loop_father->latch = new_bb;
1259
1260   return new_bb;
1261 }
1262
1263 /* Uses the natural loop discovery to recreate loop notes.  */
1264 void
1265 create_loop_notes (void)
1266 {
1267   rtx insn, head, end;
1268   struct loops loops;
1269   struct loop *loop;
1270   basic_block *first, *last, bb, pbb;
1271   struct loop **stack, **top;
1272
1273 #ifdef ENABLE_CHECKING
1274   /* Verify that there really are no loop notes.  */
1275   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1276     gcc_assert (!NOTE_P (insn) ||
1277                 NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
1278 #endif
1279
1280   flow_loops_find (&loops);
1281   free_dominance_info (CDI_DOMINATORS);
1282   if (loops.num > 1)
1283     {
1284       last = xcalloc (loops.num, sizeof (basic_block));
1285
1286       FOR_EACH_BB (bb)
1287         {
1288           for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1289             last[loop->num] = bb;
1290         }
1291
1292       first = xcalloc (loops.num, sizeof (basic_block));
1293       stack = xcalloc (loops.num, sizeof (struct loop *));
1294       top = stack;
1295
1296       FOR_EACH_BB (bb)
1297         {
1298           for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1299             {
1300               if (!first[loop->num])
1301                 {
1302                   *top++ = loop;
1303                   first[loop->num] = bb;
1304                 }
1305
1306               if (bb == last[loop->num])
1307                 {
1308                   /* Prevent loops from overlapping.  */
1309                   while (*--top != loop)
1310                     last[(*top)->num] = EXIT_BLOCK_PTR;
1311
1312                   /* If loop starts with jump into it, place the note in
1313                      front of the jump.  */
1314                   insn = PREV_INSN (BB_HEAD (first[loop->num]));
1315                   if (insn
1316                       && BARRIER_P (insn))
1317                     insn = PREV_INSN (insn);
1318                   
1319                   if (insn
1320                       && JUMP_P (insn)
1321                       && any_uncondjump_p (insn)
1322                       && onlyjump_p (insn))
1323                     {
1324                       pbb = BLOCK_FOR_INSN (insn);
1325                       gcc_assert (pbb && single_succ_p (pbb));
1326
1327                       if (!flow_bb_inside_loop_p (loop, single_succ (pbb)))
1328                         insn = BB_HEAD (first[loop->num]);
1329                     }
1330                   else
1331                     insn = BB_HEAD (first[loop->num]);
1332                     
1333                   head = BB_HEAD (first[loop->num]);
1334                   emit_note_before (NOTE_INSN_LOOP_BEG, insn);
1335                   BB_HEAD (first[loop->num]) = head;
1336
1337                   /* Position the note correctly wrto barrier.  */
1338                   insn = BB_END (last[loop->num]);
1339                   if (NEXT_INSN (insn)
1340                       && BARRIER_P (NEXT_INSN (insn)))
1341                     insn = NEXT_INSN (insn);
1342                   
1343                   end = BB_END (last[loop->num]);
1344                   emit_note_after (NOTE_INSN_LOOP_END, insn);
1345                   BB_END (last[loop->num]) = end;
1346                 }
1347             }
1348         }
1349
1350       free (first);
1351       free (last);
1352       free (stack);
1353     }
1354   flow_loops_free (&loops);
1355 }
1356
1357 /* The structure of LOOPS might have changed.  Some loops might get removed
1358    (and their headers and latches were set to NULL), loop exists might get
1359    removed (thus the loop nesting may be wrong), and some blocks and edges
1360    were changed (so the information about bb --> loop mapping does not have
1361    to be correct).  But still for the remaining loops the header dominates
1362    the latch, and loops did not get new subloobs (new loops might possibly
1363    get created, but we are not interested in them).  Fix up the mess.
1364  
1365    If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
1366    marked in it.  */
1367
1368 void
1369 fix_loop_structure (struct loops *loops, bitmap changed_bbs)
1370 {
1371   basic_block bb;
1372   struct loop *loop, *ploop;
1373   unsigned i;
1374
1375   /* Remove the old bb -> loop mapping.  */
1376   FOR_EACH_BB (bb)
1377     {
1378       bb->aux = (void *) (size_t) bb->loop_father->depth;
1379       bb->loop_father = loops->tree_root;
1380     }
1381
1382   /* Remove the dead loops from structures.  */
1383   loops->tree_root->num_nodes = n_basic_blocks + 2;
1384   for (i = 1; i < loops->num; i++)
1385     {
1386       loop = loops->parray[i];
1387       if (!loop)
1388         continue;
1389
1390       loop->num_nodes = 0;
1391       if (loop->header)
1392         continue;
1393
1394       while (loop->inner)
1395         {
1396           ploop = loop->inner;
1397           flow_loop_tree_node_remove (ploop);
1398           flow_loop_tree_node_add (loop->outer, ploop);
1399         }
1400
1401       /* Remove the loop and free its data.  */
1402       flow_loop_tree_node_remove (loop);
1403       loops->parray[loop->num] = NULL;
1404       flow_loop_free (loop);
1405     }
1406
1407   /* Rescan the bodies of loops, starting from the outermost.  */
1408   loop = loops->tree_root;
1409   while (1)
1410     {
1411       if (loop->inner)
1412         loop = loop->inner;
1413       else
1414         {
1415           while (!loop->next
1416                  && loop != loops->tree_root)
1417             loop = loop->outer;
1418           if (loop == loops->tree_root)
1419             break;
1420
1421           loop = loop->next;
1422         }
1423
1424       loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
1425     }
1426
1427   /* Now fix the loop nesting.  */
1428   for (i = 1; i < loops->num; i++)
1429     {
1430       loop = loops->parray[i];
1431       if (!loop)
1432         continue;
1433
1434       bb = loop_preheader_edge (loop)->src;
1435       if (bb->loop_father != loop->outer)
1436         {
1437           flow_loop_tree_node_remove (loop);
1438           flow_loop_tree_node_add (bb->loop_father, loop);
1439         }
1440     }
1441
1442   /* Mark the blocks whose loop has changed.  */
1443   FOR_EACH_BB (bb)
1444     {
1445       if (changed_bbs
1446           && (void *) (size_t) bb->loop_father->depth != bb->aux)
1447         bitmap_set_bit (changed_bbs, bb->index);
1448
1449       bb->aux = NULL;
1450     }
1451
1452   mark_single_exit_loops (loops);
1453   mark_irreducible_loops (loops);
1454 }