OSDN Git Service

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