OSDN Git Service

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