* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / cfgloopmanip.c
blobf3e8f183373a0d5c723dc44633487da480e3bf31
1 /* Loop manipulation code for GNU compiler.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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. */
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"
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);
51 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
53 /* Splits basic block BB after INSN, returns created edge. Updates loops
54 and dominators. */
55 edge
56 split_loop_bb (basic_block bb, void *insn)
58 edge e;
60 /* Split the block. */
61 e = split_block (bb, insn);
63 /* Add dest to loop. */
64 add_bb_to_loop (e->dest, e->src->loop_father);
66 return e;
69 /* Checks whether basic block BB is dominated by DATA. */
70 static bool
71 rpe_enum_p (basic_block bb, void *data)
73 return dominated_by_p (CDI_DOMINATORS, bb, data);
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)
81 int i;
83 for (i = 0; i < nbbs; i++)
85 remove_bb_from_loops (bbs[i]);
86 delete_basic_block (bbs[i]);
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)
99 gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
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);
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)
117 edge e;
118 edge_iterator ei;
119 struct loop *loop = loops->tree_root, *act;
121 FOR_EACH_EDGE (e, ei, bb->succs)
123 if (e->dest == EXIT_BLOCK_PTR)
124 continue;
126 act = e->dest->loop_father;
127 if (act->header == e->dest)
128 act = act->outer;
130 if (flow_loop_nested_p (loop, act))
131 loop = act;
134 if (loop == bb->loop_father)
135 return false;
137 remove_bb_from_loops (bb);
138 add_bb_to_loop (bb, loop);
140 return true;
143 /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
144 enforce condition condition stated in description of fix_bb_placement. We
145 start from basic block FROM that had some of its successors removed, so that
146 his placement no longer has to be correct, and iteratively fix placement of
147 its predecessors that may change if placement of FROM changed. Also fix
148 placement of subloops of FROM->loop_father, that might also be altered due
149 to this change; the condition for them is similar, except that instead of
150 successors we consider edges coming out of the loops. */
151 static void
152 fix_bb_placements (struct loops *loops, basic_block from)
154 sbitmap in_queue;
155 basic_block *queue, *qtop, *qbeg, *qend;
156 struct loop *base_loop;
157 edge e;
159 /* We pass through blocks back-reachable from FROM, testing whether some
160 of their successors moved to outer loop. It may be necessary to
161 iterate several times, but it is finite, as we stop unless we move
162 the basic block up the loop structure. The whole story is a bit
163 more complicated due to presence of subloops, those are moved using
164 fix_loop_placement. */
166 base_loop = from->loop_father;
167 if (base_loop == loops->tree_root)
168 return;
170 in_queue = sbitmap_alloc (last_basic_block);
171 sbitmap_zero (in_queue);
172 SET_BIT (in_queue, from->index);
173 /* Prevent us from going out of the base_loop. */
174 SET_BIT (in_queue, base_loop->header->index);
176 queue = xmalloc ((base_loop->num_nodes + 1) * sizeof (basic_block));
177 qtop = queue + base_loop->num_nodes + 1;
178 qbeg = queue;
179 qend = queue + 1;
180 *qbeg = from;
182 while (qbeg != qend)
184 edge_iterator ei;
185 from = *qbeg;
186 qbeg++;
187 if (qbeg == qtop)
188 qbeg = queue;
189 RESET_BIT (in_queue, from->index);
191 if (from->loop_father->header == from)
193 /* Subloop header, maybe move the loop upward. */
194 if (!fix_loop_placement (from->loop_father))
195 continue;
197 else
199 /* Ordinary basic block. */
200 if (!fix_bb_placement (loops, from))
201 continue;
204 /* Something has changed, insert predecessors into queue. */
205 FOR_EACH_EDGE (e, ei, from->preds)
207 basic_block pred = e->src;
208 struct loop *nca;
210 if (TEST_BIT (in_queue, pred->index))
211 continue;
213 /* If it is subloop, then it either was not moved, or
214 the path up the loop tree from base_loop do not contain
215 it. */
216 nca = find_common_loop (pred->loop_father, base_loop);
217 if (pred->loop_father != base_loop
218 && (nca == base_loop
219 || nca != pred->loop_father))
220 pred = pred->loop_father->header;
221 else if (!flow_loop_nested_p (from->loop_father, pred->loop_father))
223 /* No point in processing it. */
224 continue;
227 if (TEST_BIT (in_queue, pred->index))
228 continue;
230 /* Schedule the basic block. */
231 *qend = pred;
232 qend++;
233 if (qend == qtop)
234 qend = queue;
235 SET_BIT (in_queue, pred->index);
238 free (in_queue);
239 free (queue);
242 /* Basic block from has lost one or more of its predecessors, so it might
243 mo longer be part irreducible loop. Fix it and proceed recursively
244 for its successors if needed. */
245 static void
246 fix_irreducible_loops (basic_block from)
248 basic_block bb;
249 basic_block *stack;
250 int stack_top;
251 sbitmap on_stack;
252 edge *edges, e;
253 unsigned n_edges, i;
255 if (!(from->flags & BB_IRREDUCIBLE_LOOP))
256 return;
258 on_stack = sbitmap_alloc (last_basic_block);
259 sbitmap_zero (on_stack);
260 SET_BIT (on_stack, from->index);
261 stack = xmalloc (from->loop_father->num_nodes * sizeof (basic_block));
262 stack[0] = from;
263 stack_top = 1;
265 while (stack_top)
267 edge_iterator ei;
268 bb = stack[--stack_top];
269 RESET_BIT (on_stack, bb->index);
271 FOR_EACH_EDGE (e, ei, bb->preds)
272 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
273 break;
274 if (e)
275 continue;
277 bb->flags &= ~BB_IRREDUCIBLE_LOOP;
278 if (bb->loop_father->header == bb)
279 edges = get_loop_exit_edges (bb->loop_father, &n_edges);
280 else
282 n_edges = EDGE_COUNT (bb->succs);
283 edges = xmalloc (n_edges * sizeof (edge));
284 FOR_EACH_EDGE (e, ei, bb->succs)
285 edges[ei.index] = e;
288 for (i = 0; i < n_edges; i++)
290 e = edges[i];
292 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
294 if (!flow_bb_inside_loop_p (from->loop_father, e->dest))
295 continue;
297 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
298 if (TEST_BIT (on_stack, e->dest->index))
299 continue;
301 SET_BIT (on_stack, e->dest->index);
302 stack[stack_top++] = e->dest;
305 free (edges);
308 free (on_stack);
309 free (stack);
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)
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;
325 if (!loop_delete_branch_edge (e, 0))
326 return false;
328 /* We need to check whether basic blocks are dominated by the edge
329 e, but we only have basic block dominators. This is easy to
330 fix -- when e->dest has exactly one predecessor, this corresponds
331 to blocks dominated by e->dest, if not, split the edge. */
332 if (EDGE_COUNT (e->dest->preds) > 1)
333 e = EDGE_PRED (loop_split_edge_with (e, NULL_RTX), 0);
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);
344 /* Identify the path. */
345 nrem = find_path (e, &rem_bbs);
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);
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++)
357 edge_iterator ei;
358 bb = rem_bbs[i];
359 FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
360 if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
362 SET_BIT (seen, ae->dest->index);
363 bord_bbs[n_bord_bbs++] = ae->dest;
367 /* Remove the path. */
368 from = e->src;
369 deleted = loop_delete_branch_edge (e, 1);
370 gcc_assert (deleted);
371 dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
373 /* Cancel loops contained in the path. */
374 for (i = 0; i < nrem; i++)
375 if (rem_bbs[i]->loop_father->header == rem_bbs[i])
376 cancel_loop_tree (loops, rem_bbs[i]->loop_father);
378 remove_bbs (rem_bbs, nrem);
379 free (rem_bbs);
381 /* Find blocks whose dominators may be affected. */
382 n_dom_bbs = 0;
383 sbitmap_zero (seen);
384 for (i = 0; i < n_bord_bbs; i++)
386 basic_block ldom;
388 bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
389 if (TEST_BIT (seen, bb->index))
390 continue;
391 SET_BIT (seen, bb->index);
393 for (ldom = first_dom_son (CDI_DOMINATORS, bb);
394 ldom;
395 ldom = next_dom_son (CDI_DOMINATORS, ldom))
396 if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
397 dom_bbs[n_dom_bbs++] = ldom;
400 free (seen);
402 /* Recount dominators. */
403 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
404 free (dom_bbs);
406 /* These blocks have lost some predecessor(s), thus their irreducible
407 status could be changed. */
408 for (i = 0; i < n_bord_bbs; i++)
409 fix_irreducible_loops (bord_bbs[i]);
410 free (bord_bbs);
412 /* Fix placements of basic blocks inside loops and the placement of
413 loops in the loop tree. */
414 fix_bb_placements (loops, from);
415 fix_loop_placements (loops, from->loop_father);
417 return true;
420 /* Predicate for enumeration in add_loop. */
421 static bool
422 alp_enum_p (basic_block bb, void *alp_header)
424 return bb != (basic_block) alp_header;
427 /* Given LOOP structure with filled header and latch, find the body of the
428 corresponding loop and add it to LOOPS tree. */
429 static void
430 add_loop (struct loops *loops, struct loop *loop)
432 basic_block *bbs;
433 int i, n;
435 /* Add it to loop structure. */
436 place_new_loop (loops, loop);
437 loop->level = 1;
439 /* Find its nodes. */
440 bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
441 n = dfs_enumerate_from (loop->latch, 1, alp_enum_p,
442 bbs, n_basic_blocks, loop->header);
444 for (i = 0; i < n; i++)
445 add_bb_to_loop (bbs[i], loop);
446 add_bb_to_loop (loop->header, loop);
448 free (bbs);
451 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
452 by NUM/DEN. */
453 static void
454 scale_bbs_frequencies (basic_block *bbs, int nbbs, int num, int den)
456 int i;
457 edge e;
459 for (i = 0; i < nbbs; i++)
461 edge_iterator ei;
462 bbs[i]->frequency = (bbs[i]->frequency * num) / den;
463 bbs[i]->count = RDIV (bbs[i]->count * num, den);
464 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
465 e->count = (e->count * num) /den;
469 /* Multiply all frequencies in LOOP by NUM/DEN. */
470 static void
471 scale_loop_frequencies (struct loop *loop, int num, int den)
473 basic_block *bbs;
475 bbs = get_loop_body (loop);
476 scale_bbs_frequencies (bbs, loop->num_nodes, num, den);
477 free (bbs);
480 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
481 latch to header and update loop tree stored in LOOPS and dominators
482 accordingly. Everything between them plus LATCH_EDGE destination must
483 be dominated by HEADER_EDGE destination, and back-reachable from
484 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
485 FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
486 TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
487 Returns newly created loop. */
489 struct loop *
490 loopify (struct loops *loops, edge latch_edge, edge header_edge,
491 basic_block switch_bb, edge true_edge, edge false_edge,
492 bool redirect_all_edges)
494 basic_block succ_bb = latch_edge->dest;
495 basic_block pred_bb = header_edge->src;
496 basic_block *dom_bbs, *body;
497 unsigned n_dom_bbs, i;
498 sbitmap seen;
499 struct loop *loop = xcalloc (1, sizeof (struct loop));
500 struct loop *outer = succ_bb->loop_father->outer;
501 int freq, prob, tot_prob;
502 gcov_type cnt;
503 edge e;
504 edge_iterator ei;
506 loop->header = header_edge->dest;
507 loop->latch = latch_edge->src;
509 freq = EDGE_FREQUENCY (header_edge);
510 cnt = header_edge->count;
511 prob = EDGE_SUCC (switch_bb, 0)->probability;
512 tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
513 if (tot_prob == 0)
514 tot_prob = 1;
516 /* Redirect edges. */
517 loop_redirect_edge (latch_edge, loop->header);
518 loop_redirect_edge (true_edge, succ_bb);
520 /* During loop versioning, one of the switch_bb edge is already properly
521 set. Do not redirect it again unless redirect_all_edges is true. */
522 if (redirect_all_edges)
524 loop_redirect_edge (header_edge, switch_bb);
525 loop_redirect_edge (false_edge, loop->header);
527 /* Update dominators. */
528 set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
529 set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
532 set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
534 /* Compute new loop. */
535 add_loop (loops, loop);
536 flow_loop_tree_node_add (outer, loop);
538 /* Add switch_bb to appropriate loop. */
539 add_bb_to_loop (switch_bb, outer);
541 /* Fix frequencies. */
542 switch_bb->frequency = freq;
543 switch_bb->count = cnt;
544 FOR_EACH_EDGE (e, ei, switch_bb->succs)
545 e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
546 scale_loop_frequencies (loop, prob, tot_prob);
547 scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
549 /* Update dominators of blocks outside of LOOP. */
550 dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
551 n_dom_bbs = 0;
552 seen = sbitmap_alloc (last_basic_block);
553 sbitmap_zero (seen);
554 body = get_loop_body (loop);
556 for (i = 0; i < loop->num_nodes; i++)
557 SET_BIT (seen, body[i]->index);
559 for (i = 0; i < loop->num_nodes; i++)
561 basic_block ldom;
563 for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
564 ldom;
565 ldom = next_dom_son (CDI_DOMINATORS, ldom))
566 if (!TEST_BIT (seen, ldom->index))
568 SET_BIT (seen, ldom->index);
569 dom_bbs[n_dom_bbs++] = ldom;
573 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
575 free (body);
576 free (seen);
577 free (dom_bbs);
579 return loop;
582 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
583 the LOOP was removed. After this function, original loop latch will
584 have no successor, which caller is expected to fix somehow. */
585 void
586 unloop (struct loops *loops, struct loop *loop)
588 basic_block *body;
589 struct loop *ploop;
590 unsigned i, n;
591 basic_block latch = loop->latch;
592 edge *edges;
593 unsigned n_edges;
595 /* This is relatively straightforward. The dominators are unchanged, as
596 loop header dominates loop latch, so the only thing we have to care of
597 is the placement of loops and basic blocks inside the loop tree. We
598 move them all to the loop->outer, and then let fix_bb_placements do
599 its work. */
601 body = get_loop_body (loop);
602 edges = get_loop_exit_edges (loop, &n_edges);
603 n = loop->num_nodes;
604 for (i = 0; i < n; i++)
605 if (body[i]->loop_father == loop)
607 remove_bb_from_loops (body[i]);
608 add_bb_to_loop (body[i], loop->outer);
610 free(body);
612 while (loop->inner)
614 ploop = loop->inner;
615 flow_loop_tree_node_remove (ploop);
616 flow_loop_tree_node_add (loop->outer, ploop);
619 /* Remove the loop and free its data. */
620 flow_loop_tree_node_remove (loop);
621 loops->parray[loop->num] = NULL;
622 flow_loop_free (loop);
624 remove_edge (EDGE_SUCC (latch, 0));
625 fix_bb_placements (loops, latch);
627 /* If the loop was inside an irreducible region, we would have to somehow
628 update the irreducible marks inside its body. While it is certainly
629 possible to do, it is a bit complicated and this situation should be
630 very rare, so we just remark all loops in this case. */
631 for (i = 0; i < n_edges; i++)
632 if (edges[i]->flags & EDGE_IRREDUCIBLE_LOOP)
633 break;
634 if (i != n_edges)
635 mark_irreducible_loops (loops);
636 free (edges);
639 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
640 FATHER of LOOP such that all of the edges coming out of LOOP belong to
641 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
642 LOOP changed. */
644 fix_loop_placement (struct loop *loop)
646 basic_block *body;
647 unsigned i;
648 edge e;
649 edge_iterator ei;
650 struct loop *father = loop->pred[0], *act;
652 body = get_loop_body (loop);
653 for (i = 0; i < loop->num_nodes; i++)
654 FOR_EACH_EDGE (e, ei, body[i]->succs)
655 if (!flow_bb_inside_loop_p (loop, e->dest))
657 act = find_common_loop (loop, e->dest->loop_father);
658 if (flow_loop_nested_p (father, act))
659 father = act;
661 free (body);
663 if (father != loop->outer)
665 for (act = loop->outer; act != father; act = act->outer)
666 act->num_nodes -= loop->num_nodes;
667 flow_loop_tree_node_remove (loop);
668 flow_loop_tree_node_add (father, loop);
669 return 1;
671 return 0;
674 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
675 condition stated in description of fix_loop_placement holds for them.
676 It is used in case when we removed some edges coming out of LOOP, which
677 may cause the right placement of LOOP inside loop tree to change. */
678 static void
679 fix_loop_placements (struct loops *loops, struct loop *loop)
681 struct loop *outer;
683 while (loop->outer)
685 outer = loop->outer;
686 if (!fix_loop_placement (loop))
687 break;
689 /* Changing the placement of a loop in the loop tree may alter the
690 validity of condition 2) of the description of fix_bb_placement
691 for its preheader, because the successor is the header and belongs
692 to the loop. So call fix_bb_placements to fix up the placement
693 of the preheader and (possibly) of its predecessors. */
694 fix_bb_placements (loops, loop_preheader_edge (loop)->src);
695 loop = outer;
699 /* Creates place for a new LOOP in LOOPS structure. */
700 static void
701 place_new_loop (struct loops *loops, struct loop *loop)
703 loops->parray =
704 xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
705 loops->parray[loops->num] = loop;
707 loop->num = loops->num++;
710 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
711 created loop into LOOPS structure. */
712 struct loop *
713 duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
715 struct loop *cloop;
716 cloop = xcalloc (1, sizeof (struct loop));
717 place_new_loop (loops, cloop);
719 /* Initialize copied loop. */
720 cloop->level = loop->level;
722 /* Set it as copy of loop. */
723 loop->copy = cloop;
725 /* Add it to target. */
726 flow_loop_tree_node_add (target, cloop);
728 return cloop;
731 /* Copies structure of subloops of LOOP into TARGET loop, placing
732 newly created loops into loop tree stored in LOOPS. */
733 static void
734 duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
736 struct loop *aloop, *cloop;
738 for (aloop = loop->inner; aloop; aloop = aloop->next)
740 cloop = duplicate_loop (loops, aloop, target);
741 duplicate_subloops (loops, aloop, cloop);
745 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
746 into TARGET loop, placing newly created loops into loop tree LOOPS. */
747 static void
748 copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
750 struct loop *aloop;
751 int i;
753 for (i = 0; i < n; i++)
755 aloop = duplicate_loop (loops, copied_loops[i], target);
756 duplicate_subloops (loops, copied_loops[i], aloop);
760 /* Redirects edge E to basic block DEST. */
761 static void
762 loop_redirect_edge (edge e, basic_block dest)
764 if (e->dest == dest)
765 return;
767 redirect_edge_and_branch_force (e, dest);
770 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
771 just test whether it is possible to remove the edge. */
772 static bool
773 loop_delete_branch_edge (edge e, int really_delete)
775 basic_block src = e->src;
776 basic_block newdest;
777 int irr;
778 edge snd;
780 gcc_assert (EDGE_COUNT (src->succs) > 1);
782 /* Cannot handle more than two exit edges. */
783 if (EDGE_COUNT (src->succs) > 2)
784 return false;
785 /* And it must be just a simple branch. */
786 if (!any_condjump_p (BB_END (src)))
787 return false;
789 snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
790 newdest = snd->dest;
791 if (newdest == EXIT_BLOCK_PTR)
792 return false;
794 /* Hopefully the above conditions should suffice. */
795 if (!really_delete)
796 return true;
798 /* Redirecting behaves wrongly wrto this flag. */
799 irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
801 if (!redirect_edge_and_branch (e, newdest))
802 return false;
803 EDGE_SUCC (src, 0)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
804 EDGE_SUCC (src, 0)->flags |= irr;
806 return true;
809 /* Check whether LOOP's body can be duplicated. */
810 bool
811 can_duplicate_loop_p (struct loop *loop)
813 int ret;
814 basic_block *bbs = get_loop_body (loop);
816 ret = can_copy_bbs_p (bbs, loop->num_nodes);
817 free (bbs);
819 return ret;
822 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
823 to LOOP. Update the single_exit information in superloops of LOOP. */
825 void
826 update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
827 struct loop *loop)
829 unsigned i;
831 for (i = 0; i < nbbs; i++)
832 bbs[i]->rbi->duplicated = 1;
834 for (; loop->outer; loop = loop->outer)
836 if (!loop->single_exit)
837 continue;
839 if (loop->single_exit->src->rbi->duplicated)
840 loop->single_exit = NULL;
843 for (i = 0; i < nbbs; i++)
844 bbs[i]->rbi->duplicated = 0;
847 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
848 LOOPS structure and dominators. E's destination must be LOOP header for
849 this to work, i.e. it must be entry or latch edge of this loop; these are
850 unique, as the loops must have preheaders for this function to work
851 correctly (in case E is latch, the function unrolls the loop, if E is entry
852 edge, it peels the loop). Store edges created by copying ORIG edge from
853 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
854 original LOOP body, the other copies are numbered in order given by control
855 flow through them) into TO_REMOVE array. Returns false if duplication is
856 impossible. */
858 duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
859 unsigned int ndupl, sbitmap wont_exit,
860 edge orig, edge *to_remove,
861 unsigned int *n_to_remove, int flags)
863 struct loop *target, *aloop;
864 struct loop **orig_loops;
865 unsigned n_orig_loops;
866 basic_block header = loop->header, latch = loop->latch;
867 basic_block *new_bbs, *bbs, *first_active;
868 basic_block new_bb, bb, first_active_latch = NULL;
869 edge ae, latch_edge;
870 edge spec_edges[2], new_spec_edges[2];
871 #define SE_LATCH 0
872 #define SE_ORIG 1
873 unsigned i, j, n;
874 int is_latch = (latch == e->src);
875 int scale_act = 0, *scale_step = NULL, scale_main = 0;
876 int p, freq_in, freq_le, freq_out_orig;
877 int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
878 int add_irreducible_flag;
880 gcc_assert (e->dest == loop->header);
881 gcc_assert (ndupl > 0);
883 if (orig)
885 /* Orig must be edge out of the loop. */
886 gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
887 gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
890 bbs = get_loop_body (loop);
892 /* Check whether duplication is possible. */
893 if (!can_copy_bbs_p (bbs, loop->num_nodes))
895 free (bbs);
896 return false;
898 new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes);
900 /* In case we are doing loop peeling and the loop is in the middle of
901 irreducible region, the peeled copies will be inside it too. */
902 add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
903 gcc_assert (!is_latch || !add_irreducible_flag);
905 /* Find edge from latch. */
906 latch_edge = loop_latch_edge (loop);
908 if (flags & DLTHE_FLAG_UPDATE_FREQ)
910 /* Calculate coefficients by that we have to scale frequencies
911 of duplicated loop bodies. */
912 freq_in = header->frequency;
913 freq_le = EDGE_FREQUENCY (latch_edge);
914 if (freq_in == 0)
915 freq_in = 1;
916 if (freq_in < freq_le)
917 freq_in = freq_le;
918 freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
919 if (freq_out_orig > freq_in - freq_le)
920 freq_out_orig = freq_in - freq_le;
921 prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
922 prob_pass_wont_exit =
923 RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
925 scale_step = xmalloc (ndupl * sizeof (int));
927 for (i = 1; i <= ndupl; i++)
928 scale_step[i - 1] = TEST_BIT (wont_exit, i)
929 ? prob_pass_wont_exit
930 : prob_pass_thru;
932 if (is_latch)
934 prob_pass_main = TEST_BIT (wont_exit, 0)
935 ? prob_pass_wont_exit
936 : prob_pass_thru;
937 p = prob_pass_main;
938 scale_main = REG_BR_PROB_BASE;
939 for (i = 0; i < ndupl; i++)
941 scale_main += p;
942 p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
944 scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
945 scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
947 else
949 scale_main = REG_BR_PROB_BASE;
950 for (i = 0; i < ndupl; i++)
951 scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
952 scale_act = REG_BR_PROB_BASE - prob_pass_thru;
954 for (i = 0; i < ndupl; i++)
955 gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
956 gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
957 && scale_act >= 0 && scale_act <= REG_BR_PROB_BASE);
960 /* Loop the new bbs will belong to. */
961 target = e->src->loop_father;
963 /* Original loops. */
964 n_orig_loops = 0;
965 for (aloop = loop->inner; aloop; aloop = aloop->next)
966 n_orig_loops++;
967 orig_loops = xcalloc (n_orig_loops, sizeof (struct loop *));
968 for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
969 orig_loops[i] = aloop;
971 loop->copy = target;
973 n = loop->num_nodes;
975 first_active = xmalloc (n * sizeof (basic_block));
976 if (is_latch)
978 memcpy (first_active, bbs, n * sizeof (basic_block));
979 first_active_latch = latch;
982 /* Update the information about single exits. */
983 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
984 update_single_exits_after_duplication (bbs, n, target);
986 /* Record exit edge in original loop body. */
987 if (orig && TEST_BIT (wont_exit, 0))
988 to_remove[(*n_to_remove)++] = orig;
990 spec_edges[SE_ORIG] = orig;
991 spec_edges[SE_LATCH] = latch_edge;
993 for (j = 0; j < ndupl; j++)
995 /* Copy loops. */
996 copy_loops_to (loops, orig_loops, n_orig_loops, target);
998 /* Copy bbs. */
999 copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop);
1001 for (i = 0; i < n; i++)
1002 new_bbs[i]->rbi->copy_number = j + 1;
1004 /* Note whether the blocks and edges belong to an irreducible loop. */
1005 if (add_irreducible_flag)
1007 for (i = 0; i < n; i++)
1008 new_bbs[i]->rbi->duplicated = 1;
1009 for (i = 0; i < n; i++)
1011 edge_iterator ei;
1012 new_bb = new_bbs[i];
1013 if (new_bb->loop_father == target)
1014 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
1016 FOR_EACH_EDGE (ae, ei, new_bb->succs)
1017 if (ae->dest->rbi->duplicated
1018 && (ae->src->loop_father == target
1019 || ae->dest->loop_father == target))
1020 ae->flags |= EDGE_IRREDUCIBLE_LOOP;
1022 for (i = 0; i < n; i++)
1023 new_bbs[i]->rbi->duplicated = 0;
1026 /* Redirect the special edges. */
1027 if (is_latch)
1029 redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
1030 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1031 loop->header);
1032 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
1033 latch = loop->latch = new_bbs[1];
1034 e = latch_edge = new_spec_edges[SE_LATCH];
1036 else
1038 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1039 loop->header);
1040 redirect_edge_and_branch_force (e, new_bbs[0]);
1041 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
1042 e = new_spec_edges[SE_LATCH];
1045 /* Record exit edge in this copy. */
1046 if (orig && TEST_BIT (wont_exit, j + 1))
1047 to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
1049 /* Record the first copy in the control flow order if it is not
1050 the original loop (i.e. in case of peeling). */
1051 if (!first_active_latch)
1053 memcpy (first_active, new_bbs, n * sizeof (basic_block));
1054 first_active_latch = new_bbs[1];
1057 /* Set counts and frequencies. */
1058 if (flags & DLTHE_FLAG_UPDATE_FREQ)
1060 scale_bbs_frequencies (new_bbs, n, scale_act, REG_BR_PROB_BASE);
1061 scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
1064 free (new_bbs);
1065 free (orig_loops);
1067 /* Update the original loop. */
1068 if (!is_latch)
1069 set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1070 if (flags & DLTHE_FLAG_UPDATE_FREQ)
1072 scale_bbs_frequencies (bbs, n, scale_main, REG_BR_PROB_BASE);
1073 free (scale_step);
1076 /* Update dominators of outer blocks if affected. */
1077 for (i = 0; i < n; i++)
1079 basic_block dominated, dom_bb, *dom_bbs;
1080 int n_dom_bbs,j;
1082 bb = bbs[i];
1083 bb->rbi->copy_number = 0;
1085 n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
1086 for (j = 0; j < n_dom_bbs; j++)
1088 dominated = dom_bbs[j];
1089 if (flow_bb_inside_loop_p (loop, dominated))
1090 continue;
1091 dom_bb = nearest_common_dominator (
1092 CDI_DOMINATORS, first_active[i], first_active_latch);
1093 set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
1095 free (dom_bbs);
1097 free (first_active);
1099 free (bbs);
1101 return true;
1104 /* A callback for make_forwarder block, to redirect all edges except for
1105 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1106 whether to redirect it. */
1108 static edge mfb_kj_edge;
1109 static bool
1110 mfb_keep_just (edge e)
1112 return e != mfb_kj_edge;
1115 /* A callback for make_forwarder block, to update data structures for a basic
1116 block JUMP created by redirecting an edge (only the latch edge is being
1117 redirected). */
1119 static void
1120 mfb_update_loops (basic_block jump)
1122 struct loop *loop = EDGE_SUCC (jump, 0)->dest->loop_father;
1124 if (dom_computed[CDI_DOMINATORS])
1125 set_immediate_dominator (CDI_DOMINATORS, jump, EDGE_PRED (jump, 0)->src);
1126 add_bb_to_loop (jump, loop);
1127 loop->latch = jump;
1130 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1131 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1132 entry; otherwise we also force preheader block to have only one successor.
1133 The function also updates dominators. */
1135 static basic_block
1136 create_preheader (struct loop *loop, int flags)
1138 edge e, fallthru;
1139 basic_block dummy;
1140 struct loop *cloop, *ploop;
1141 int nentry = 0;
1142 bool irred = false;
1143 edge_iterator ei;
1145 cloop = loop->outer;
1147 FOR_EACH_EDGE (e, ei, loop->header->preds)
1149 if (e->src == loop->latch)
1150 continue;
1151 irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
1152 nentry++;
1154 gcc_assert (nentry);
1155 if (nentry == 1)
1157 FOR_EACH_EDGE (e, ei, loop->header->preds)
1158 if (e->src != loop->latch)
1159 break;
1161 if (!(flags & CP_SIMPLE_PREHEADERS) || EDGE_COUNT (e->src->succs) == 1)
1162 return NULL;
1165 mfb_kj_edge = loop_latch_edge (loop);
1166 fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1167 mfb_update_loops);
1168 dummy = fallthru->src;
1169 loop->header = fallthru->dest;
1171 /* The header could be a latch of some superloop(s); due to design of
1172 split_block, it would now move to fallthru->dest. */
1173 for (ploop = loop; ploop; ploop = ploop->outer)
1174 if (ploop->latch == dummy)
1175 ploop->latch = fallthru->dest;
1177 /* Reorganize blocks so that the preheader is not stuck in the middle of the
1178 loop. */
1179 FOR_EACH_EDGE (e, ei, dummy->preds)
1180 if (e->src != loop->latch)
1181 break;
1182 move_block_after (dummy, e->src);
1184 loop->header->loop_father = loop;
1185 add_bb_to_loop (dummy, cloop);
1187 if (irred)
1189 dummy->flags |= BB_IRREDUCIBLE_LOOP;
1190 EDGE_SUCC (dummy, 0)->flags |= EDGE_IRREDUCIBLE_LOOP;
1193 if (dump_file)
1194 fprintf (dump_file, "Created preheader block for loop %i\n",
1195 loop->num);
1197 return dummy;
1200 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1201 of FLAGS see create_preheader. */
1202 void
1203 create_preheaders (struct loops *loops, int flags)
1205 unsigned i;
1206 for (i = 1; i < loops->num; i++)
1207 create_preheader (loops->parray[i], flags);
1208 loops->state |= LOOPS_HAVE_PREHEADERS;
1211 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1212 successor. */
1213 void
1214 force_single_succ_latches (struct loops *loops)
1216 unsigned i;
1217 struct loop *loop;
1218 edge e;
1220 for (i = 1; i < loops->num; i++)
1222 edge_iterator ei;
1223 loop = loops->parray[i];
1224 if (loop->latch != loop->header && EDGE_COUNT (loop->latch->succs) == 1)
1225 continue;
1227 FOR_EACH_EDGE (e, ei, loop->header->preds)
1228 if (e->src == loop->latch)
1229 break;
1231 loop_split_edge_with (e, NULL_RTX);
1233 loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1236 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1237 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1238 be ok after this function. The created block is placed on correct place
1239 in LOOPS structure and its dominator is set. */
1240 basic_block
1241 loop_split_edge_with (edge e, rtx insns)
1243 basic_block src, dest, new_bb;
1244 struct loop *loop_c;
1246 src = e->src;
1247 dest = e->dest;
1249 loop_c = find_common_loop (src->loop_father, dest->loop_father);
1251 /* Create basic block for it. */
1253 new_bb = split_edge (e);
1254 add_bb_to_loop (new_bb, loop_c);
1255 new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
1257 if (insns)
1258 emit_insn_after (insns, BB_END (new_bb));
1260 if (dest->loop_father->latch == src)
1261 dest->loop_father->latch = new_bb;
1263 return new_bb;
1266 /* Uses the natural loop discovery to recreate loop notes. */
1267 void
1268 create_loop_notes (void)
1270 rtx insn, head, end;
1271 struct loops loops;
1272 struct loop *loop;
1273 basic_block *first, *last, bb, pbb;
1274 struct loop **stack, **top;
1276 #ifdef ENABLE_CHECKING
1277 /* Verify that there really are no loop notes. */
1278 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1279 gcc_assert (!NOTE_P (insn) ||
1280 NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
1281 #endif
1283 flow_loops_find (&loops, LOOP_TREE);
1284 free_dominance_info (CDI_DOMINATORS);
1285 if (loops.num > 1)
1287 last = xcalloc (loops.num, sizeof (basic_block));
1289 FOR_EACH_BB (bb)
1291 for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1292 last[loop->num] = bb;
1295 first = xcalloc (loops.num, sizeof (basic_block));
1296 stack = xcalloc (loops.num, sizeof (struct loop *));
1297 top = stack;
1299 FOR_EACH_BB (bb)
1301 for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1303 if (!first[loop->num])
1305 *top++ = loop;
1306 first[loop->num] = bb;
1309 if (bb == last[loop->num])
1311 /* Prevent loops from overlapping. */
1312 while (*--top != loop)
1313 last[(*top)->num] = EXIT_BLOCK_PTR;
1315 /* If loop starts with jump into it, place the note in
1316 front of the jump. */
1317 insn = PREV_INSN (BB_HEAD (first[loop->num]));
1318 if (insn
1319 && BARRIER_P (insn))
1320 insn = PREV_INSN (insn);
1322 if (insn
1323 && JUMP_P (insn)
1324 && any_uncondjump_p (insn)
1325 && onlyjump_p (insn))
1327 pbb = BLOCK_FOR_INSN (insn);
1328 gcc_assert (pbb && EDGE_COUNT (pbb->succs) == 1);
1330 if (!flow_bb_inside_loop_p (loop, EDGE_SUCC (pbb, 0)->dest))
1331 insn = BB_HEAD (first[loop->num]);
1333 else
1334 insn = BB_HEAD (first[loop->num]);
1336 head = BB_HEAD (first[loop->num]);
1337 emit_note_before (NOTE_INSN_LOOP_BEG, insn);
1338 BB_HEAD (first[loop->num]) = head;
1340 /* Position the note correctly wrto barrier. */
1341 insn = BB_END (last[loop->num]);
1342 if (NEXT_INSN (insn)
1343 && BARRIER_P (NEXT_INSN (insn)))
1344 insn = NEXT_INSN (insn);
1346 end = BB_END (last[loop->num]);
1347 emit_note_after (NOTE_INSN_LOOP_END, insn);
1348 BB_END (last[loop->num]) = end;
1353 free (first);
1354 free (last);
1355 free (stack);
1357 flow_loops_free (&loops);