OSDN Git Service

* config/i386/i386.c (x86_schedule): Fix typo, m_K6 intead of m_K8.
[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 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 (EDGE_COUNT (e->dest->preds) > 1)
334     e = EDGE_PRED (loop_split_edge_with (e, NULL_RTX), 0);
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 of basic blocks in array BBS of length NBBS
453    by NUM/DEN.  */
454 static void
455 scale_bbs_frequencies (basic_block *bbs, int nbbs, int num, int den)
456 {
457   int i;
458   edge e;
459
460   for (i = 0; i < nbbs; i++)
461     {
462       edge_iterator ei;
463       bbs[i]->frequency = (bbs[i]->frequency * num) / den;
464       bbs[i]->count = RDIV (bbs[i]->count * num, den);
465       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
466         e->count = (e->count * num) /den;
467     }
468 }
469
470 /* Multiply all frequencies in LOOP by NUM/DEN.  */
471 static void
472 scale_loop_frequencies (struct loop *loop, int num, int den)
473 {
474   basic_block *bbs;
475
476   bbs = get_loop_body (loop);
477   scale_bbs_frequencies (bbs, loop->num_nodes, num, den);
478   free (bbs);
479 }
480
481 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
482    latch to header and update loop tree stored in LOOPS and dominators
483    accordingly. Everything between them plus LATCH_EDGE destination must
484    be dominated by HEADER_EDGE destination, and back-reachable from
485    LATCH_EDGE source.  HEADER_EDGE is redirected to basic block SWITCH_BB,
486    FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
487    TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
488    Returns newly created loop.  */
489
490 struct loop *
491 loopify (struct loops *loops, edge latch_edge, edge header_edge, 
492          basic_block switch_bb, edge true_edge, edge false_edge,
493          bool redirect_all_edges)
494 {
495   basic_block succ_bb = latch_edge->dest;
496   basic_block pred_bb = header_edge->src;
497   basic_block *dom_bbs, *body;
498   unsigned n_dom_bbs, i;
499   sbitmap seen;
500   struct loop *loop = xcalloc (1, sizeof (struct loop));
501   struct loop *outer = succ_bb->loop_father->outer;
502   int freq, prob, tot_prob;
503   gcov_type cnt;
504   edge e;
505   edge_iterator ei;
506
507   loop->header = header_edge->dest;
508   loop->latch = latch_edge->src;
509
510   freq = EDGE_FREQUENCY (header_edge);
511   cnt = header_edge->count;
512   prob = EDGE_SUCC (switch_bb, 0)->probability;
513   tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
514   if (tot_prob == 0)
515     tot_prob = 1;
516
517   /* Redirect edges.  */
518   loop_redirect_edge (latch_edge, loop->header);
519   loop_redirect_edge (true_edge, succ_bb);
520
521   /* During loop versioning, one of the switch_bb edge is already properly
522      set. Do not redirect it again unless redirect_all_edges is true.  */
523   if (redirect_all_edges)
524     {
525       loop_redirect_edge (header_edge, switch_bb);
526       loop_redirect_edge (false_edge, loop->header); 
527      
528       /* Update dominators.  */
529       set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
530       set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
531     }
532
533   set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
534
535   /* Compute new loop.  */
536   add_loop (loops, loop);
537   flow_loop_tree_node_add (outer, loop);
538
539   /* Add switch_bb to appropriate loop.  */
540   add_bb_to_loop (switch_bb, outer);
541
542   /* Fix frequencies.  */
543   switch_bb->frequency = freq;
544   switch_bb->count = cnt;
545   FOR_EACH_EDGE (e, ei, switch_bb->succs)
546     e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
547   scale_loop_frequencies (loop, prob, tot_prob);
548   scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
549
550   /* Update dominators of blocks outside of LOOP.  */
551   dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
552   n_dom_bbs = 0;
553   seen = sbitmap_alloc (last_basic_block);
554   sbitmap_zero (seen);
555   body = get_loop_body (loop);
556
557   for (i = 0; i < loop->num_nodes; i++)
558     SET_BIT (seen, body[i]->index);
559
560   for (i = 0; i < loop->num_nodes; i++)
561     {
562       basic_block ldom;
563
564       for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
565            ldom;
566            ldom = next_dom_son (CDI_DOMINATORS, ldom))
567         if (!TEST_BIT (seen, ldom->index))
568           {
569             SET_BIT (seen, ldom->index);
570             dom_bbs[n_dom_bbs++] = ldom;
571           }
572     }
573
574   iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
575
576   free (body);
577   free (seen);
578   free (dom_bbs);
579
580   return loop;
581 }
582
583 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
584    the LOOP was removed.  After this function, original loop latch will
585    have no successor, which caller is expected to fix somehow.  */
586 static void
587 unloop (struct loops *loops, struct loop *loop)
588 {
589   basic_block *body;
590   struct loop *ploop;
591   unsigned i, n;
592   basic_block latch = loop->latch;
593   edge *edges;
594   unsigned n_edges;
595
596   /* This is relatively straightforward.  The dominators are unchanged, as
597      loop header dominates loop latch, so the only thing we have to care of
598      is the placement of loops and basic blocks inside the loop tree.  We
599      move them all to the loop->outer, and then let fix_bb_placements do
600      its work.  */
601
602   body = get_loop_body (loop);
603   edges = get_loop_exit_edges (loop, &n_edges);
604   n = loop->num_nodes;
605   for (i = 0; i < n; i++)
606     if (body[i]->loop_father == loop)
607       {
608         remove_bb_from_loops (body[i]);
609         add_bb_to_loop (body[i], loop->outer);
610       }
611   free(body);
612
613   while (loop->inner)
614     {
615       ploop = loop->inner;
616       flow_loop_tree_node_remove (ploop);
617       flow_loop_tree_node_add (loop->outer, ploop);
618     }
619
620   /* Remove the loop and free its data.  */
621   flow_loop_tree_node_remove (loop);
622   loops->parray[loop->num] = NULL;
623   flow_loop_free (loop);
624
625   remove_edge (EDGE_SUCC (latch, 0));
626   fix_bb_placements (loops, latch);
627
628   /* If the loop was inside an irreducible region, we would have to somehow
629      update the irreducible marks inside its body.  While it is certainly
630      possible to do, it is a bit complicated and this situation should be
631      very rare, so we just remark all loops in this case.  */
632   for (i = 0; i < n_edges; i++)
633     if (edges[i]->flags & EDGE_IRREDUCIBLE_LOOP)
634       break;
635   if (i != n_edges)
636     mark_irreducible_loops (loops);
637   free (edges);
638 }
639
640 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
641    FATHER of LOOP such that all of the edges coming out of LOOP belong to
642    FATHER, and set it as outer loop of LOOP.  Return 1 if placement of
643    LOOP changed.  */
644 int
645 fix_loop_placement (struct loop *loop)
646 {
647   basic_block *body;
648   unsigned i;
649   edge e;
650   edge_iterator ei;
651   struct loop *father = loop->pred[0], *act;
652
653   body = get_loop_body (loop);
654   for (i = 0; i < loop->num_nodes; i++)
655     FOR_EACH_EDGE (e, ei, body[i]->succs)
656       if (!flow_bb_inside_loop_p (loop, e->dest))
657         {
658           act = find_common_loop (loop, e->dest->loop_father);
659           if (flow_loop_nested_p (father, act))
660             father = act;
661         }
662   free (body);
663
664   if (father != loop->outer)
665     {
666       for (act = loop->outer; act != father; act = act->outer)
667         act->num_nodes -= loop->num_nodes;
668       flow_loop_tree_node_remove (loop);
669       flow_loop_tree_node_add (father, loop);
670       return 1;
671     }
672   return 0;
673 }
674
675 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
676    condition stated in description of fix_loop_placement holds for them.
677    It is used in case when we removed some edges coming out of LOOP, which
678    may cause the right placement of LOOP inside loop tree to change.  */
679 static void
680 fix_loop_placements (struct loops *loops, struct loop *loop)
681 {
682   struct loop *outer;
683
684   while (loop->outer)
685     {
686       outer = loop->outer;
687       if (!fix_loop_placement (loop))
688         break;
689
690       /* Changing the placement of a loop in the loop tree may alter the
691          validity of condition 2) of the description of fix_bb_placement
692          for its preheader, because the successor is the header and belongs
693          to the loop.  So call fix_bb_placements to fix up the placement
694          of the preheader and (possibly) of its predecessors.  */
695       fix_bb_placements (loops, loop_preheader_edge (loop)->src);
696       loop = outer;
697     }
698 }
699
700 /* Creates place for a new LOOP in LOOPS structure.  */
701 static void
702 place_new_loop (struct loops *loops, struct loop *loop)
703 {
704   loops->parray =
705     xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
706   loops->parray[loops->num] = loop;
707
708   loop->num = loops->num++;
709 }
710
711 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
712    created loop into LOOPS structure.  */
713 struct loop *
714 duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
715 {
716   struct loop *cloop;
717   cloop = xcalloc (1, sizeof (struct loop));
718   place_new_loop (loops, cloop);
719
720   /* Initialize copied loop.  */
721   cloop->level = loop->level;
722
723   /* Set it as copy of loop.  */
724   loop->copy = cloop;
725
726   /* Add it to target.  */
727   flow_loop_tree_node_add (target, cloop);
728
729   return cloop;
730 }
731
732 /* Copies structure of subloops of LOOP into TARGET loop, placing
733    newly created loops into loop tree stored in LOOPS.  */
734 static void
735 duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
736 {
737   struct loop *aloop, *cloop;
738
739   for (aloop = loop->inner; aloop; aloop = aloop->next)
740     {
741       cloop = duplicate_loop (loops, aloop, target);
742       duplicate_subloops (loops, aloop, cloop);
743     }
744 }
745
746 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
747    into TARGET loop, placing newly created loops into loop tree LOOPS.  */
748 static void
749 copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
750 {
751   struct loop *aloop;
752   int i;
753
754   for (i = 0; i < n; i++)
755     {
756       aloop = duplicate_loop (loops, copied_loops[i], target);
757       duplicate_subloops (loops, copied_loops[i], aloop);
758     }
759 }
760
761 /* Redirects edge E to basic block DEST.  */
762 static void
763 loop_redirect_edge (edge e, basic_block dest)
764 {
765   if (e->dest == dest)
766     return;
767
768   redirect_edge_and_branch_force (e, dest);
769 }
770
771 /* Deletes edge E from a branch if possible.  Unless REALLY_DELETE is set,
772    just test whether it is possible to remove the edge.  */
773 static bool
774 loop_delete_branch_edge (edge e, int really_delete)
775 {
776   basic_block src = e->src;
777   basic_block newdest;
778   int irr;
779   edge snd;
780
781   gcc_assert (EDGE_COUNT (src->succs) > 1);
782   
783   /* Cannot handle more than two exit edges.  */
784   if (EDGE_COUNT (src->succs) > 2)
785     return false;
786   /* And it must be just a simple branch.  */
787   if (!any_condjump_p (BB_END (src)))
788     return false;
789
790   snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
791   newdest = snd->dest;
792   if (newdest == EXIT_BLOCK_PTR)
793     return false;
794
795   /* Hopefully the above conditions should suffice.  */
796   if (!really_delete)
797     return true;
798
799   /* Redirecting behaves wrongly wrto this flag.  */
800   irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
801
802   if (!redirect_edge_and_branch (e, newdest))
803     return false;
804   EDGE_SUCC (src, 0)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
805   EDGE_SUCC (src, 0)->flags |= irr;
806   
807   return true;
808 }
809
810 /* Check whether LOOP's body can be duplicated.  */
811 bool
812 can_duplicate_loop_p (struct loop *loop)
813 {
814   int ret;
815   basic_block *bbs = get_loop_body (loop);
816
817   ret = can_copy_bbs_p (bbs, loop->num_nodes);
818   free (bbs);
819   
820   return ret;
821 }
822
823 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
824    to LOOP.  Update the single_exit information in superloops of LOOP.  */
825
826 static void
827 update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
828                                        struct loop *loop)
829 {
830   unsigned i;
831
832   for (i = 0; i < nbbs; i++)
833     bbs[i]->rbi->duplicated = 1;
834
835   for (; loop->outer; loop = loop->outer)
836     {
837       if (!loop->single_exit)
838         continue;
839
840       if (loop->single_exit->src->rbi->duplicated)
841         loop->single_exit = NULL;
842     }
843
844   for (i = 0; i < nbbs; i++)
845     bbs[i]->rbi->duplicated = 0;
846 }
847
848 /* Duplicates body of LOOP to given edge E NDUPL times.  Takes care of updating
849    LOOPS structure and dominators.  E's destination must be LOOP header for
850    this to work, i.e. it must be entry or latch edge of this loop; these are
851    unique, as the loops must have preheaders for this function to work
852    correctly (in case E is latch, the function unrolls the loop, if E is entry
853    edge, it peels the loop).  Store edges created by copying ORIG edge from
854    copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
855    original LOOP body, the other copies are numbered in order given by control
856    flow through them) into TO_REMOVE array.  Returns false if duplication is
857    impossible.  */
858 int
859 duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
860                                unsigned int ndupl, sbitmap wont_exit,
861                                edge orig, edge *to_remove,
862                                unsigned int *n_to_remove, int flags)
863 {
864   struct loop *target, *aloop;
865   struct loop **orig_loops;
866   unsigned n_orig_loops;
867   basic_block header = loop->header, latch = loop->latch;
868   basic_block *new_bbs, *bbs, *first_active;
869   basic_block new_bb, bb, first_active_latch = NULL;
870   edge ae, latch_edge;
871   edge spec_edges[2], new_spec_edges[2];
872 #define SE_LATCH 0
873 #define SE_ORIG 1
874   unsigned i, j, n;
875   int is_latch = (latch == e->src);
876   int scale_act = 0, *scale_step = NULL, scale_main = 0;
877   int p, freq_in, freq_le, freq_out_orig;
878   int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
879   int add_irreducible_flag;
880
881   gcc_assert (e->dest == loop->header);
882   gcc_assert (ndupl > 0);
883
884   if (orig)
885     {
886       /* Orig must be edge out of the loop.  */
887       gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
888       gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
889     }
890
891   bbs = get_loop_body (loop);
892
893   /* Check whether duplication is possible.  */
894   if (!can_copy_bbs_p (bbs, loop->num_nodes))
895     {
896       free (bbs);
897       return false;
898     }
899   new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes);
900
901   /* In case we are doing loop peeling and the loop is in the middle of
902      irreducible region, the peeled copies will be inside it too.  */
903   add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
904   gcc_assert (!is_latch || !add_irreducible_flag);
905
906   /* Find edge from latch.  */
907   latch_edge = loop_latch_edge (loop);
908
909   if (flags & DLTHE_FLAG_UPDATE_FREQ)
910     {
911       /* Calculate coefficients by that we have to scale frequencies
912          of duplicated loop bodies.  */
913       freq_in = header->frequency;
914       freq_le = EDGE_FREQUENCY (latch_edge);
915       if (freq_in == 0)
916         freq_in = 1;
917       if (freq_in < freq_le)
918         freq_in = freq_le;
919       freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
920       if (freq_out_orig > freq_in - freq_le)
921         freq_out_orig = freq_in - freq_le;
922       prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
923       prob_pass_wont_exit =
924               RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
925
926       scale_step = xmalloc (ndupl * sizeof (int));
927
928         for (i = 1; i <= ndupl; i++)
929           scale_step[i - 1] = TEST_BIT (wont_exit, i)
930                                 ? prob_pass_wont_exit
931                                 : prob_pass_thru;
932
933       if (is_latch)
934         {
935           prob_pass_main = TEST_BIT (wont_exit, 0)
936                                 ? prob_pass_wont_exit
937                                 : prob_pass_thru;
938           p = prob_pass_main;
939           scale_main = REG_BR_PROB_BASE;
940           for (i = 0; i < ndupl; i++)
941             {
942               scale_main += p;
943               p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
944             }
945           scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
946           scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
947         }
948       else
949         {
950           scale_main = REG_BR_PROB_BASE;
951           for (i = 0; i < ndupl; i++)
952             scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
953           scale_act = REG_BR_PROB_BASE - prob_pass_thru;
954         }
955       for (i = 0; i < ndupl; i++)
956         gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
957       gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
958                   && scale_act >= 0  && scale_act <= REG_BR_PROB_BASE);
959     }
960
961   /* Loop the new bbs will belong to.  */
962   target = e->src->loop_father;
963
964   /* Original loops.  */
965   n_orig_loops = 0;
966   for (aloop = loop->inner; aloop; aloop = aloop->next)
967     n_orig_loops++;
968   orig_loops = xcalloc (n_orig_loops, sizeof (struct loop *));
969   for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
970     orig_loops[i] = aloop;
971
972   loop->copy = target;
973
974   n = loop->num_nodes;
975
976   first_active = xmalloc (n * sizeof (basic_block));
977   if (is_latch)
978     {
979       memcpy (first_active, bbs, n * sizeof (basic_block));
980       first_active_latch = latch;
981     }
982
983   /* Update the information about single exits.  */
984   if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
985     update_single_exits_after_duplication (bbs, n, target);
986
987   /* Record exit edge in original loop body.  */
988   if (orig && TEST_BIT (wont_exit, 0))
989     to_remove[(*n_to_remove)++] = orig;
990
991   spec_edges[SE_ORIG] = orig;
992   spec_edges[SE_LATCH] = latch_edge;
993
994   for (j = 0; j < ndupl; j++)
995     {
996       /* Copy loops.  */
997       copy_loops_to (loops, orig_loops, n_orig_loops, target);
998
999       /* Copy bbs.  */
1000       copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop);
1001
1002       for (i = 0; i < n; i++)
1003         new_bbs[i]->rbi->copy_number = j + 1;
1004
1005       /* Note whether the blocks and edges belong to an irreducible loop.  */
1006       if (add_irreducible_flag)
1007         {
1008           for (i = 0; i < n; i++)
1009             new_bbs[i]->rbi->duplicated = 1;
1010           for (i = 0; i < n; i++)
1011             {
1012               edge_iterator ei;
1013               new_bb = new_bbs[i];
1014               if (new_bb->loop_father == target)
1015                 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
1016
1017               FOR_EACH_EDGE (ae, ei, new_bb->succs)
1018                 if (ae->dest->rbi->duplicated
1019                     && (ae->src->loop_father == target
1020                         || ae->dest->loop_father == target))
1021                   ae->flags |= EDGE_IRREDUCIBLE_LOOP;
1022             }
1023           for (i = 0; i < n; i++)
1024             new_bbs[i]->rbi->duplicated = 0;
1025         }
1026
1027       /* Redirect the special edges.  */
1028       if (is_latch)
1029         {
1030           redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
1031           redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1032                                           loop->header);
1033           set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
1034           latch = loop->latch = new_bbs[1];
1035           e = latch_edge = new_spec_edges[SE_LATCH];
1036         }
1037       else
1038         {
1039           redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1040                                           loop->header);
1041           redirect_edge_and_branch_force (e, new_bbs[0]);
1042           set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
1043           e = new_spec_edges[SE_LATCH];
1044         }
1045
1046       /* Record exit edge in this copy.  */
1047       if (orig && TEST_BIT (wont_exit, j + 1))
1048         to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
1049
1050       /* Record the first copy in the control flow order if it is not
1051          the original loop (i.e. in case of peeling).  */
1052       if (!first_active_latch)
1053         {
1054           memcpy (first_active, new_bbs, n * sizeof (basic_block));
1055           first_active_latch = new_bbs[1];
1056         }
1057
1058       /* Set counts and frequencies.  */
1059       if (flags & DLTHE_FLAG_UPDATE_FREQ)
1060         {
1061           scale_bbs_frequencies (new_bbs, n, scale_act, REG_BR_PROB_BASE);
1062           scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
1063         }
1064     }
1065   free (new_bbs);
1066   free (orig_loops);
1067   
1068   /* Update the original loop.  */
1069   if (!is_latch)
1070     set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1071   if (flags & DLTHE_FLAG_UPDATE_FREQ)
1072     {
1073       scale_bbs_frequencies (bbs, n, scale_main, REG_BR_PROB_BASE);
1074       free (scale_step);
1075     }
1076
1077   /* Update dominators of outer blocks if affected.  */
1078   for (i = 0; i < n; i++)
1079     {
1080       basic_block dominated, dom_bb, *dom_bbs;
1081       int n_dom_bbs,j;
1082
1083       bb = bbs[i];
1084       bb->rbi->copy_number = 0;
1085
1086       n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
1087       for (j = 0; j < n_dom_bbs; j++)
1088         {
1089           dominated = dom_bbs[j];
1090           if (flow_bb_inside_loop_p (loop, dominated))
1091             continue;
1092           dom_bb = nearest_common_dominator (
1093                         CDI_DOMINATORS, first_active[i], first_active_latch);
1094           set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
1095         }
1096       free (dom_bbs);
1097     }
1098   free (first_active);
1099
1100   free (bbs);
1101
1102   return true;
1103 }
1104
1105 /* A callback for make_forwarder block, to redirect all edges except for
1106    MFB_KJ_EDGE to the entry part.  E is the edge for that we should decide
1107    whether to redirect it.  */
1108
1109 static edge mfb_kj_edge;
1110 static bool
1111 mfb_keep_just (edge e)
1112 {
1113   return e != mfb_kj_edge;
1114 }
1115
1116 /* A callback for make_forwarder block, to update data structures for a basic
1117    block JUMP created by redirecting an edge (only the latch edge is being
1118    redirected).  */
1119
1120 static void
1121 mfb_update_loops (basic_block jump)
1122 {
1123   struct loop *loop = EDGE_SUCC (jump, 0)->dest->loop_father;
1124
1125   if (dom_computed[CDI_DOMINATORS])
1126     set_immediate_dominator (CDI_DOMINATORS, jump, EDGE_PRED (jump, 0)->src);
1127   add_bb_to_loop (jump, loop);
1128   loop->latch = jump;
1129 }
1130
1131 /* Creates a pre-header for a LOOP.  Returns newly created block.  Unless
1132    CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1133    entry; otherwise we also force preheader block to have only one successor.
1134    The function also updates dominators.  */
1135
1136 static basic_block
1137 create_preheader (struct loop *loop, int flags)
1138 {
1139   edge e, fallthru;
1140   basic_block dummy;
1141   struct loop *cloop, *ploop;
1142   int nentry = 0;
1143   bool irred = false;
1144   edge_iterator ei;
1145
1146   cloop = loop->outer;
1147
1148   FOR_EACH_EDGE (e, ei, loop->header->preds)
1149     {
1150       if (e->src == loop->latch)
1151         continue;
1152       irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
1153       nentry++;
1154     }
1155   gcc_assert (nentry);
1156   if (nentry == 1)
1157     {
1158       FOR_EACH_EDGE (e, ei, loop->header->preds)
1159         if (e->src != loop->latch)
1160           break;
1161
1162       if (!(flags & CP_SIMPLE_PREHEADERS) || EDGE_COUNT (e->src->succs) == 1)
1163         return NULL;
1164     }
1165
1166   mfb_kj_edge = loop_latch_edge (loop);
1167   fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1168                                    mfb_update_loops);
1169   dummy = fallthru->src;
1170   loop->header = fallthru->dest;
1171
1172   /* The header could be a latch of some superloop(s); due to design of
1173      split_block, it would now move to fallthru->dest.  */
1174   for (ploop = loop; ploop; ploop = ploop->outer)
1175     if (ploop->latch == dummy)
1176       ploop->latch = fallthru->dest;
1177
1178   /* Reorganize blocks so that the preheader is not stuck in the middle of the
1179      loop.  */
1180   FOR_EACH_EDGE (e, ei, dummy->preds)
1181     if (e->src != loop->latch)
1182       break;
1183   move_block_after (dummy, e->src);
1184
1185   loop->header->loop_father = loop;
1186   add_bb_to_loop (dummy, cloop);
1187
1188   if (irred)
1189     {
1190       dummy->flags |= BB_IRREDUCIBLE_LOOP;
1191       EDGE_SUCC (dummy, 0)->flags |= EDGE_IRREDUCIBLE_LOOP;
1192     }
1193
1194   if (dump_file)
1195     fprintf (dump_file, "Created preheader block for loop %i\n",
1196              loop->num);
1197
1198   return dummy;
1199 }
1200
1201 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1202    of FLAGS see create_preheader.  */
1203 void
1204 create_preheaders (struct loops *loops, int flags)
1205 {
1206   unsigned i;
1207   for (i = 1; i < loops->num; i++)
1208     create_preheader (loops->parray[i], flags);
1209   loops->state |= LOOPS_HAVE_PREHEADERS;
1210 }
1211
1212 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1213    successor.  */
1214 void
1215 force_single_succ_latches (struct loops *loops)
1216 {
1217   unsigned i;
1218   struct loop *loop;
1219   edge e;
1220
1221   for (i = 1; i < loops->num; i++)
1222     {
1223       edge_iterator ei;
1224       loop = loops->parray[i];
1225       if (loop->latch != loop->header && EDGE_COUNT (loop->latch->succs) == 1)
1226         continue;
1227
1228       FOR_EACH_EDGE (e, ei, loop->header->preds)
1229         if (e->src == loop->latch)
1230           break;
1231
1232       loop_split_edge_with (e, NULL_RTX);
1233     }
1234   loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1235 }
1236
1237 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1238    just one basic block.  Jumps in INSNS are not handled, so cfg do not have to
1239    be ok after this function.  The created block is placed on correct place
1240    in LOOPS structure and its dominator is set.  */
1241 basic_block
1242 loop_split_edge_with (edge e, rtx insns)
1243 {
1244   basic_block src, dest, new_bb;
1245   struct loop *loop_c;
1246
1247   src = e->src;
1248   dest = e->dest;
1249
1250   loop_c = find_common_loop (src->loop_father, dest->loop_father);
1251
1252   /* Create basic block for it.  */
1253
1254   new_bb = split_edge (e);
1255   add_bb_to_loop (new_bb, loop_c);
1256   new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
1257
1258   if (insns)
1259     emit_insn_after (insns, BB_END (new_bb));
1260
1261   if (dest->loop_father->latch == src)
1262     dest->loop_father->latch = new_bb;
1263
1264   return new_bb;
1265 }
1266
1267 /* Uses the natural loop discovery to recreate loop notes.  */
1268 void
1269 create_loop_notes (void)
1270 {
1271   rtx insn, head, end;
1272   struct loops loops;
1273   struct loop *loop;
1274   basic_block *first, *last, bb, pbb;
1275   struct loop **stack, **top;
1276
1277 #ifdef ENABLE_CHECKING
1278   /* Verify that there really are no loop notes.  */
1279   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1280     gcc_assert (!NOTE_P (insn) ||
1281                 NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
1282 #endif
1283
1284   flow_loops_find (&loops, LOOP_TREE);
1285   free_dominance_info (CDI_DOMINATORS);
1286   if (loops.num > 1)
1287     {
1288       last = xcalloc (loops.num, sizeof (basic_block));
1289
1290       FOR_EACH_BB (bb)
1291         {
1292           for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1293             last[loop->num] = bb;
1294         }
1295
1296       first = xcalloc (loops.num, sizeof (basic_block));
1297       stack = xcalloc (loops.num, sizeof (struct loop *));
1298       top = stack;
1299
1300       FOR_EACH_BB (bb)
1301         {
1302           for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1303             {
1304               if (!first[loop->num])
1305                 {
1306                   *top++ = loop;
1307                   first[loop->num] = bb;
1308                 }
1309
1310               if (bb == last[loop->num])
1311                 {
1312                   /* Prevent loops from overlapping.  */
1313                   while (*--top != loop)
1314                     last[(*top)->num] = EXIT_BLOCK_PTR;
1315
1316                   /* If loop starts with jump into it, place the note in
1317                      front of the jump.  */
1318                   insn = PREV_INSN (BB_HEAD (first[loop->num]));
1319                   if (insn
1320                       && BARRIER_P (insn))
1321                     insn = PREV_INSN (insn);
1322                   
1323                   if (insn
1324                       && JUMP_P (insn)
1325                       && any_uncondjump_p (insn)
1326                       && onlyjump_p (insn))
1327                     {
1328                       pbb = BLOCK_FOR_INSN (insn);
1329                       gcc_assert (pbb && EDGE_COUNT (pbb->succs) == 1);
1330
1331                       if (!flow_bb_inside_loop_p (loop, EDGE_SUCC (pbb, 0)->dest))
1332                         insn = BB_HEAD (first[loop->num]);
1333                     }
1334                   else
1335                     insn = BB_HEAD (first[loop->num]);
1336                     
1337                   head = BB_HEAD (first[loop->num]);
1338                   emit_note_before (NOTE_INSN_LOOP_BEG, insn);
1339                   BB_HEAD (first[loop->num]) = head;
1340
1341                   /* Position the note correctly wrto barrier.  */
1342                   insn = BB_END (last[loop->num]);
1343                   if (NEXT_INSN (insn)
1344                       && BARRIER_P (NEXT_INSN (insn)))
1345                     insn = NEXT_INSN (insn);
1346                   
1347                   end = BB_END (last[loop->num]);
1348                   emit_note_after (NOTE_INSN_LOOP_END, insn);
1349                   BB_END (last[loop->num]) = end;
1350                 }
1351             }
1352         }
1353
1354       free (first);
1355       free (last);
1356       free (stack);
1357     }
1358   flow_loops_free (&loops);
1359 }