1 /* Loop manipulation code for GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005 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
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
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
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "cfglayout.h"
33 static void duplicate_subloops (struct loops *, struct loop *, struct loop *);
34 static void copy_loops_to (struct loops *, struct loop **, int,
36 static void loop_redirect_edge (edge, basic_block);
37 static bool loop_delete_branch_edge (edge, int);
38 static void remove_bbs (basic_block *, int);
39 static bool rpe_enum_p (basic_block, void *);
40 static int find_path (edge, basic_block **);
41 static bool alp_enum_p (basic_block, void *);
42 static void add_loop (struct loops *, struct loop *);
43 static void fix_loop_placements (struct loops *, struct loop *);
44 static bool fix_bb_placement (struct loops *, basic_block);
45 static void fix_bb_placements (struct loops *, basic_block);
46 static void place_new_loop (struct loops *, struct loop *);
47 static void scale_loop_frequencies (struct loop *, int, int);
48 static basic_block create_preheader (struct loop *, int);
49 static void fix_irreducible_loops (basic_block);
50 static void unloop (struct loops *, struct loop *);
52 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
54 /* Splits basic block BB after INSN, returns created edge. Updates loops
57 split_loop_bb (basic_block bb, void *insn)
61 /* Split the block. */
62 e = split_block (bb, insn);
64 /* Add dest to loop. */
65 add_bb_to_loop (e->dest, e->src->loop_father);
70 /* Checks whether basic block BB is dominated by DATA. */
72 rpe_enum_p (basic_block bb, void *data)
74 return dominated_by_p (CDI_DOMINATORS, bb, data);
77 /* Remove basic blocks BBS from loop structure and dominance info,
78 and delete them afterwards. */
80 remove_bbs (basic_block *bbs, int nbbs)
84 for (i = 0; i < nbbs; i++)
86 remove_bb_from_loops (bbs[i]);
87 delete_basic_block (bbs[i]);
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
98 find_path (edge e, basic_block **bbs)
100 gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
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);
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). */
116 fix_bb_placement (struct loops *loops, basic_block bb)
120 struct loop *loop = loops->tree_root, *act;
122 FOR_EACH_EDGE (e, ei, bb->succs)
124 if (e->dest == EXIT_BLOCK_PTR)
127 act = e->dest->loop_father;
128 if (act->header == e->dest)
131 if (flow_loop_nested_p (loop, act))
135 if (loop == bb->loop_father)
138 remove_bb_from_loops (bb);
139 add_bb_to_loop (bb, loop);
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. */
153 fix_bb_placements (struct loops *loops, basic_block from)
156 basic_block *queue, *qtop, *qbeg, *qend;
157 struct loop *base_loop;
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. */
167 base_loop = from->loop_father;
168 if (base_loop == loops->tree_root)
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);
177 queue = xmalloc ((base_loop->num_nodes + 1) * sizeof (basic_block));
178 qtop = queue + base_loop->num_nodes + 1;
190 RESET_BIT (in_queue, from->index);
192 if (from->loop_father->header == from)
194 /* Subloop header, maybe move the loop upward. */
195 if (!fix_loop_placement (from->loop_father))
200 /* Ordinary basic block. */
201 if (!fix_bb_placement (loops, from))
205 /* Something has changed, insert predecessors into queue. */
206 FOR_EACH_EDGE (e, ei, from->preds)
208 basic_block pred = e->src;
211 if (TEST_BIT (in_queue, pred->index))
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
217 nca = find_common_loop (pred->loop_father, base_loop);
218 if (pred->loop_father != 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))
224 /* No point in processing it. */
228 if (TEST_BIT (in_queue, pred->index))
231 /* Schedule the basic block. */
236 SET_BIT (in_queue, pred->index);
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. */
247 fix_irreducible_loops (basic_block from)
256 if (!(from->flags & BB_IRREDUCIBLE_LOOP))
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));
269 bb = stack[--stack_top];
270 RESET_BIT (on_stack, bb->index);
272 FOR_EACH_EDGE (e, ei, bb->preds)
273 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
278 bb->flags &= ~BB_IRREDUCIBLE_LOOP;
279 if (bb->loop_father->header == bb)
280 edges = get_loop_exit_edges (bb->loop_father, &n_edges);
283 n_edges = EDGE_COUNT (bb->succs);
284 edges = xmalloc (n_edges * sizeof (edge));
285 FOR_EACH_EDGE (e, ei, bb->succs)
289 for (i = 0; i < n_edges; i++)
293 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
295 if (!flow_bb_inside_loop_p (from->loop_father, e->dest))
298 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
299 if (TEST_BIT (on_stack, e->dest->index))
302 SET_BIT (on_stack, e->dest->index);
303 stack[stack_top++] = e->dest;
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
318 remove_path (struct loops *loops, edge e)
321 basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb;
322 int i, nrem, n_bord_bbs, n_dom_bbs;
326 if (!loop_delete_branch_edge (e, 0))
329 /* We need to check whether basic blocks are dominated by the edge
330 e, but we only have basic block dominators. This is easy to
331 fix -- when e->dest has exactly one predecessor, this corresponds
332 to blocks dominated by e->dest, if not, split the edge. */
333 if (!single_pred_p (e->dest))
334 e = single_pred_edge (loop_split_edge_with (e, NULL_RTX));
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);
345 /* Identify the path. */
346 nrem = find_path (e, &rem_bbs);
349 bord_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
350 seen = sbitmap_alloc (last_basic_block);
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++)
360 FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
361 if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
363 SET_BIT (seen, ae->dest->index);
364 bord_bbs[n_bord_bbs++] = ae->dest;
368 /* Remove the path. */
370 deleted = loop_delete_branch_edge (e, 1);
371 gcc_assert (deleted);
372 dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
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);
379 remove_bbs (rem_bbs, nrem);
382 /* Find blocks whose dominators may be affected. */
385 for (i = 0; i < n_bord_bbs; i++)
389 bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
390 if (TEST_BIT (seen, bb->index))
392 SET_BIT (seen, bb->index);
394 for (ldom = first_dom_son (CDI_DOMINATORS, bb);
396 ldom = next_dom_son (CDI_DOMINATORS, ldom))
397 if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
398 dom_bbs[n_dom_bbs++] = ldom;
403 /* Recount dominators. */
404 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
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]);
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);
421 /* Predicate for enumeration in add_loop. */
423 alp_enum_p (basic_block bb, void *alp_header)
425 return bb != (basic_block) alp_header;
428 /* Given LOOP structure with filled header and latch, find the body of the
429 corresponding loop and add it to LOOPS tree. */
431 add_loop (struct loops *loops, struct loop *loop)
436 /* Add it to loop structure. */
437 place_new_loop (loops, loop);
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);
445 for (i = 0; i < n; i++)
446 add_bb_to_loop (bbs[i], loop);
447 add_bb_to_loop (loop->header, loop);
452 /* Multiply all frequencies in LOOP by NUM/DEN. */
454 scale_loop_frequencies (struct loop *loop, int num, int den)
458 bbs = get_loop_body (loop);
459 scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
463 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
464 latch to header and update loop tree stored in LOOPS and dominators
465 accordingly. Everything between them plus LATCH_EDGE destination must
466 be dominated by HEADER_EDGE destination, and back-reachable from
467 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
468 FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
469 TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
470 Returns newly created loop. */
473 loopify (struct loops *loops, edge latch_edge, edge header_edge,
474 basic_block switch_bb, edge true_edge, edge false_edge,
475 bool redirect_all_edges)
477 basic_block succ_bb = latch_edge->dest;
478 basic_block pred_bb = header_edge->src;
479 basic_block *dom_bbs, *body;
480 unsigned n_dom_bbs, i;
482 struct loop *loop = xcalloc (1, sizeof (struct loop));
483 struct loop *outer = succ_bb->loop_father->outer;
484 int freq, prob, tot_prob;
489 loop->header = header_edge->dest;
490 loop->latch = latch_edge->src;
492 freq = EDGE_FREQUENCY (header_edge);
493 cnt = header_edge->count;
494 prob = EDGE_SUCC (switch_bb, 0)->probability;
495 tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
499 /* Redirect edges. */
500 loop_redirect_edge (latch_edge, loop->header);
501 loop_redirect_edge (true_edge, succ_bb);
503 /* During loop versioning, one of the switch_bb edge is already properly
504 set. Do not redirect it again unless redirect_all_edges is true. */
505 if (redirect_all_edges)
507 loop_redirect_edge (header_edge, switch_bb);
508 loop_redirect_edge (false_edge, loop->header);
510 /* Update dominators. */
511 set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
512 set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
515 set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
517 /* Compute new loop. */
518 add_loop (loops, loop);
519 flow_loop_tree_node_add (outer, loop);
521 /* Add switch_bb to appropriate loop. */
522 add_bb_to_loop (switch_bb, outer);
524 /* Fix frequencies. */
525 switch_bb->frequency = freq;
526 switch_bb->count = cnt;
527 FOR_EACH_EDGE (e, ei, switch_bb->succs)
528 e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
529 scale_loop_frequencies (loop, prob, tot_prob);
530 scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
532 /* Update dominators of blocks outside of LOOP. */
533 dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block));
535 seen = sbitmap_alloc (last_basic_block);
537 body = get_loop_body (loop);
539 for (i = 0; i < loop->num_nodes; i++)
540 SET_BIT (seen, body[i]->index);
542 for (i = 0; i < loop->num_nodes; i++)
546 for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
548 ldom = next_dom_son (CDI_DOMINATORS, ldom))
549 if (!TEST_BIT (seen, ldom->index))
551 SET_BIT (seen, ldom->index);
552 dom_bbs[n_dom_bbs++] = ldom;
556 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
565 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
566 the LOOP was removed. After this function, original loop latch will
567 have no successor, which caller is expected to fix somehow. */
569 unloop (struct loops *loops, struct loop *loop)
574 basic_block latch = loop->latch;
578 /* This is relatively straightforward. The dominators are unchanged, as
579 loop header dominates loop latch, so the only thing we have to care of
580 is the placement of loops and basic blocks inside the loop tree. We
581 move them all to the loop->outer, and then let fix_bb_placements do
584 body = get_loop_body (loop);
585 edges = get_loop_exit_edges (loop, &n_edges);
587 for (i = 0; i < n; i++)
588 if (body[i]->loop_father == loop)
590 remove_bb_from_loops (body[i]);
591 add_bb_to_loop (body[i], loop->outer);
598 flow_loop_tree_node_remove (ploop);
599 flow_loop_tree_node_add (loop->outer, ploop);
602 /* Remove the loop and free its data. */
603 flow_loop_tree_node_remove (loop);
604 loops->parray[loop->num] = NULL;
605 flow_loop_free (loop);
607 remove_edge (single_succ_edge (latch));
608 fix_bb_placements (loops, latch);
610 /* If the loop was inside an irreducible region, we would have to somehow
611 update the irreducible marks inside its body. While it is certainly
612 possible to do, it is a bit complicated and this situation should be
613 very rare, so we just remark all loops in this case. */
614 for (i = 0; i < n_edges; i++)
615 if (edges[i]->flags & EDGE_IRREDUCIBLE_LOOP)
618 mark_irreducible_loops (loops);
622 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
623 FATHER of LOOP such that all of the edges coming out of LOOP belong to
624 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
627 fix_loop_placement (struct loop *loop)
633 struct loop *father = loop->pred[0], *act;
635 body = get_loop_body (loop);
636 for (i = 0; i < loop->num_nodes; i++)
637 FOR_EACH_EDGE (e, ei, body[i]->succs)
638 if (!flow_bb_inside_loop_p (loop, e->dest))
640 act = find_common_loop (loop, e->dest->loop_father);
641 if (flow_loop_nested_p (father, act))
646 if (father != loop->outer)
648 for (act = loop->outer; act != father; act = act->outer)
649 act->num_nodes -= loop->num_nodes;
650 flow_loop_tree_node_remove (loop);
651 flow_loop_tree_node_add (father, loop);
657 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
658 condition stated in description of fix_loop_placement holds for them.
659 It is used in case when we removed some edges coming out of LOOP, which
660 may cause the right placement of LOOP inside loop tree to change. */
662 fix_loop_placements (struct loops *loops, struct loop *loop)
669 if (!fix_loop_placement (loop))
672 /* Changing the placement of a loop in the loop tree may alter the
673 validity of condition 2) of the description of fix_bb_placement
674 for its preheader, because the successor is the header and belongs
675 to the loop. So call fix_bb_placements to fix up the placement
676 of the preheader and (possibly) of its predecessors. */
677 fix_bb_placements (loops, loop_preheader_edge (loop)->src);
682 /* Creates place for a new LOOP in LOOPS structure. */
684 place_new_loop (struct loops *loops, struct loop *loop)
687 xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
688 loops->parray[loops->num] = loop;
690 loop->num = loops->num++;
693 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
694 created loop into LOOPS structure. */
696 duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
699 cloop = xcalloc (1, sizeof (struct loop));
700 place_new_loop (loops, cloop);
702 /* Initialize copied loop. */
703 cloop->level = loop->level;
705 /* Set it as copy of loop. */
708 /* Add it to target. */
709 flow_loop_tree_node_add (target, cloop);
714 /* Copies structure of subloops of LOOP into TARGET loop, placing
715 newly created loops into loop tree stored in LOOPS. */
717 duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
719 struct loop *aloop, *cloop;
721 for (aloop = loop->inner; aloop; aloop = aloop->next)
723 cloop = duplicate_loop (loops, aloop, target);
724 duplicate_subloops (loops, aloop, cloop);
728 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
729 into TARGET loop, placing newly created loops into loop tree LOOPS. */
731 copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
736 for (i = 0; i < n; i++)
738 aloop = duplicate_loop (loops, copied_loops[i], target);
739 duplicate_subloops (loops, copied_loops[i], aloop);
743 /* Redirects edge E to basic block DEST. */
745 loop_redirect_edge (edge e, basic_block dest)
750 redirect_edge_and_branch_force (e, dest);
753 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
754 just test whether it is possible to remove the edge. */
756 loop_delete_branch_edge (edge e, int really_delete)
758 basic_block src = e->src;
763 gcc_assert (EDGE_COUNT (src->succs) > 1);
765 /* Cannot handle more than two exit edges. */
766 if (EDGE_COUNT (src->succs) > 2)
768 /* And it must be just a simple branch. */
769 if (!any_condjump_p (BB_END (src)))
772 snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
774 if (newdest == EXIT_BLOCK_PTR)
777 /* Hopefully the above conditions should suffice. */
781 /* Redirecting behaves wrongly wrto this flag. */
782 irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
784 if (!redirect_edge_and_branch (e, newdest))
786 single_succ_edge (src)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
787 single_succ_edge (src)->flags |= irr;
792 /* Check whether LOOP's body can be duplicated. */
794 can_duplicate_loop_p (struct loop *loop)
797 basic_block *bbs = get_loop_body (loop);
799 ret = can_copy_bbs_p (bbs, loop->num_nodes);
805 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
806 to LOOP. Update the single_exit information in superloops of LOOP. */
809 update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
814 for (i = 0; i < nbbs; i++)
815 bbs[i]->rbi->duplicated = 1;
817 for (; loop->outer; loop = loop->outer)
819 if (!loop->single_exit)
822 if (loop->single_exit->src->rbi->duplicated)
823 loop->single_exit = NULL;
826 for (i = 0; i < nbbs; i++)
827 bbs[i]->rbi->duplicated = 0;
830 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
831 LOOPS structure and dominators. E's destination must be LOOP header for
832 this to work, i.e. it must be entry or latch edge of this loop; these are
833 unique, as the loops must have preheaders for this function to work
834 correctly (in case E is latch, the function unrolls the loop, if E is entry
835 edge, it peels the loop). Store edges created by copying ORIG edge from
836 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
837 original LOOP body, the other copies are numbered in order given by control
838 flow through them) into TO_REMOVE array. Returns false if duplication is
841 duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
842 unsigned int ndupl, sbitmap wont_exit,
843 edge orig, edge *to_remove,
844 unsigned int *n_to_remove, int flags)
846 struct loop *target, *aloop;
847 struct loop **orig_loops;
848 unsigned n_orig_loops;
849 basic_block header = loop->header, latch = loop->latch;
850 basic_block *new_bbs, *bbs, *first_active;
851 basic_block new_bb, bb, first_active_latch = NULL;
853 edge spec_edges[2], new_spec_edges[2];
857 int is_latch = (latch == e->src);
858 int scale_act = 0, *scale_step = NULL, scale_main = 0;
859 int p, freq_in, freq_le, freq_out_orig;
860 int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
861 int add_irreducible_flag;
863 gcc_assert (e->dest == loop->header);
864 gcc_assert (ndupl > 0);
868 /* Orig must be edge out of the loop. */
869 gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
870 gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
873 bbs = get_loop_body (loop);
875 /* Check whether duplication is possible. */
876 if (!can_copy_bbs_p (bbs, loop->num_nodes))
881 new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes);
883 /* In case we are doing loop peeling and the loop is in the middle of
884 irreducible region, the peeled copies will be inside it too. */
885 add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
886 gcc_assert (!is_latch || !add_irreducible_flag);
888 /* Find edge from latch. */
889 latch_edge = loop_latch_edge (loop);
891 if (flags & DLTHE_FLAG_UPDATE_FREQ)
893 /* Calculate coefficients by that we have to scale frequencies
894 of duplicated loop bodies. */
895 freq_in = header->frequency;
896 freq_le = EDGE_FREQUENCY (latch_edge);
899 if (freq_in < freq_le)
901 freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
902 if (freq_out_orig > freq_in - freq_le)
903 freq_out_orig = freq_in - freq_le;
904 prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
905 prob_pass_wont_exit =
906 RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
908 scale_step = xmalloc (ndupl * sizeof (int));
910 for (i = 1; i <= ndupl; i++)
911 scale_step[i - 1] = TEST_BIT (wont_exit, i)
912 ? prob_pass_wont_exit
917 prob_pass_main = TEST_BIT (wont_exit, 0)
918 ? prob_pass_wont_exit
921 scale_main = REG_BR_PROB_BASE;
922 for (i = 0; i < ndupl; i++)
925 p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
927 scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
928 scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
932 scale_main = REG_BR_PROB_BASE;
933 for (i = 0; i < ndupl; i++)
934 scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
935 scale_act = REG_BR_PROB_BASE - prob_pass_thru;
937 for (i = 0; i < ndupl; i++)
938 gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
939 gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
940 && scale_act >= 0 && scale_act <= REG_BR_PROB_BASE);
943 /* Loop the new bbs will belong to. */
944 target = e->src->loop_father;
946 /* Original loops. */
948 for (aloop = loop->inner; aloop; aloop = aloop->next)
950 orig_loops = xcalloc (n_orig_loops, sizeof (struct loop *));
951 for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
952 orig_loops[i] = aloop;
958 first_active = xmalloc (n * sizeof (basic_block));
961 memcpy (first_active, bbs, n * sizeof (basic_block));
962 first_active_latch = latch;
965 /* Update the information about single exits. */
966 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
967 update_single_exits_after_duplication (bbs, n, target);
969 /* Record exit edge in original loop body. */
970 if (orig && TEST_BIT (wont_exit, 0))
971 to_remove[(*n_to_remove)++] = orig;
973 spec_edges[SE_ORIG] = orig;
974 spec_edges[SE_LATCH] = latch_edge;
976 for (j = 0; j < ndupl; j++)
979 copy_loops_to (loops, orig_loops, n_orig_loops, target);
982 copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop);
984 for (i = 0; i < n; i++)
985 new_bbs[i]->rbi->copy_number = j + 1;
987 /* Note whether the blocks and edges belong to an irreducible loop. */
988 if (add_irreducible_flag)
990 for (i = 0; i < n; i++)
991 new_bbs[i]->rbi->duplicated = 1;
992 for (i = 0; i < n; i++)
996 if (new_bb->loop_father == target)
997 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
999 FOR_EACH_EDGE (ae, ei, new_bb->succs)
1000 if (ae->dest->rbi->duplicated
1001 && (ae->src->loop_father == target
1002 || ae->dest->loop_father == target))
1003 ae->flags |= EDGE_IRREDUCIBLE_LOOP;
1005 for (i = 0; i < n; i++)
1006 new_bbs[i]->rbi->duplicated = 0;
1009 /* Redirect the special edges. */
1012 redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
1013 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1015 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
1016 latch = loop->latch = new_bbs[1];
1017 e = latch_edge = new_spec_edges[SE_LATCH];
1021 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1023 redirect_edge_and_branch_force (e, new_bbs[0]);
1024 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
1025 e = new_spec_edges[SE_LATCH];
1028 /* Record exit edge in this copy. */
1029 if (orig && TEST_BIT (wont_exit, j + 1))
1030 to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
1032 /* Record the first copy in the control flow order if it is not
1033 the original loop (i.e. in case of peeling). */
1034 if (!first_active_latch)
1036 memcpy (first_active, new_bbs, n * sizeof (basic_block));
1037 first_active_latch = new_bbs[1];
1040 /* Set counts and frequencies. */
1041 if (flags & DLTHE_FLAG_UPDATE_FREQ)
1043 scale_bbs_frequencies_int (new_bbs, n, scale_act, REG_BR_PROB_BASE);
1044 scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
1050 /* Update the original loop. */
1052 set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1053 if (flags & DLTHE_FLAG_UPDATE_FREQ)
1055 scale_bbs_frequencies_int (bbs, n, scale_main, REG_BR_PROB_BASE);
1059 /* Update dominators of outer blocks if affected. */
1060 for (i = 0; i < n; i++)
1062 basic_block dominated, dom_bb, *dom_bbs;
1066 bb->rbi->copy_number = 0;
1068 n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
1069 for (j = 0; j < n_dom_bbs; j++)
1071 dominated = dom_bbs[j];
1072 if (flow_bb_inside_loop_p (loop, dominated))
1074 dom_bb = nearest_common_dominator (
1075 CDI_DOMINATORS, first_active[i], first_active_latch);
1076 set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
1080 free (first_active);
1087 /* A callback for make_forwarder block, to redirect all edges except for
1088 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1089 whether to redirect it. */
1091 static edge mfb_kj_edge;
1093 mfb_keep_just (edge e)
1095 return e != mfb_kj_edge;
1098 /* A callback for make_forwarder block, to update data structures for a basic
1099 block JUMP created by redirecting an edge (only the latch edge is being
1103 mfb_update_loops (basic_block jump)
1105 struct loop *loop = single_succ (jump)->loop_father;
1107 if (dom_computed[CDI_DOMINATORS])
1108 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
1109 add_bb_to_loop (jump, loop);
1113 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1114 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1115 entry; otherwise we also force preheader block to have only one successor.
1116 The function also updates dominators. */
1119 create_preheader (struct loop *loop, int flags)
1123 struct loop *cloop, *ploop;
1126 bool latch_edge_was_fallthru;
1127 edge one_succ_pred = 0;
1130 cloop = loop->outer;
1132 FOR_EACH_EDGE (e, ei, loop->header->preds)
1134 if (e->src == loop->latch)
1136 irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
1138 if (single_succ_p (e->src))
1141 gcc_assert (nentry);
1144 /* Get an edge that is different from the one from loop->latch
1146 e = EDGE_PRED (loop->header,
1147 EDGE_PRED (loop->header, 0)->src == loop->latch);
1149 if (!(flags & CP_SIMPLE_PREHEADERS) || single_succ_p (e->src))
1153 mfb_kj_edge = loop_latch_edge (loop);
1154 latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
1155 fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1157 dummy = fallthru->src;
1158 loop->header = fallthru->dest;
1160 /* The header could be a latch of some superloop(s); due to design of
1161 split_block, it would now move to fallthru->dest. */
1162 for (ploop = loop; ploop; ploop = ploop->outer)
1163 if (ploop->latch == dummy)
1164 ploop->latch = fallthru->dest;
1166 /* Try to be clever in placing the newly created preheader. The idea is to
1167 avoid breaking any "fallthruness" relationship between blocks.
1169 The preheader was created just before the header and all incoming edges
1170 to the header were redirected to the preheader, except the latch edge.
1171 So the only problematic case is when this latch edge was a fallthru
1172 edge: it is not anymore after the preheader creation so we have broken
1173 the fallthruness. We're therefore going to look for a better place. */
1174 if (latch_edge_was_fallthru)
1179 e = EDGE_PRED (dummy, 0);
1181 move_block_after (dummy, e->src);
1184 loop->header->loop_father = loop;
1185 add_bb_to_loop (dummy, cloop);
1189 dummy->flags |= BB_IRREDUCIBLE_LOOP;
1190 single_succ_edge (dummy)->flags |= EDGE_IRREDUCIBLE_LOOP;
1194 fprintf (dump_file, "Created preheader block for loop %i\n",
1200 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1201 of FLAGS see create_preheader. */
1203 create_preheaders (struct loops *loops, int flags)
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
1214 force_single_succ_latches (struct loops *loops)
1220 for (i = 1; i < loops->num; i++)
1222 loop = loops->parray[i];
1223 if (loop->latch != loop->header && single_succ_p (loop->latch))
1226 e = find_edge (loop->latch, loop->header);
1228 loop_split_edge_with (e, NULL_RTX);
1230 loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1233 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1234 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1235 be ok after this function. The created block is placed on correct place
1236 in LOOPS structure and its dominator is set. */
1238 loop_split_edge_with (edge e, rtx insns)
1240 basic_block src, dest, new_bb;
1241 struct loop *loop_c;
1246 loop_c = find_common_loop (src->loop_father, dest->loop_father);
1248 /* Create basic block for it. */
1250 new_bb = split_edge (e);
1251 add_bb_to_loop (new_bb, loop_c);
1252 new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
1255 emit_insn_after (insns, BB_END (new_bb));
1257 if (dest->loop_father->latch == src)
1258 dest->loop_father->latch = new_bb;
1263 /* Uses the natural loop discovery to recreate loop notes. */
1265 create_loop_notes (void)
1267 rtx insn, head, end;
1270 basic_block *first, *last, bb, pbb;
1271 struct loop **stack, **top;
1273 #ifdef ENABLE_CHECKING
1274 /* Verify that there really are no loop notes. */
1275 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1276 gcc_assert (!NOTE_P (insn) ||
1277 NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
1280 flow_loops_find (&loops);
1281 free_dominance_info (CDI_DOMINATORS);
1284 last = xcalloc (loops.num, sizeof (basic_block));
1288 for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1289 last[loop->num] = bb;
1292 first = xcalloc (loops.num, sizeof (basic_block));
1293 stack = xcalloc (loops.num, sizeof (struct loop *));
1298 for (loop = bb->loop_father; loop->outer; loop = loop->outer)
1300 if (!first[loop->num])
1303 first[loop->num] = bb;
1306 if (bb == last[loop->num])
1308 /* Prevent loops from overlapping. */
1309 while (*--top != loop)
1310 last[(*top)->num] = EXIT_BLOCK_PTR;
1312 /* If loop starts with jump into it, place the note in
1313 front of the jump. */
1314 insn = PREV_INSN (BB_HEAD (first[loop->num]));
1316 && BARRIER_P (insn))
1317 insn = PREV_INSN (insn);
1321 && any_uncondjump_p (insn)
1322 && onlyjump_p (insn))
1324 pbb = BLOCK_FOR_INSN (insn);
1325 gcc_assert (pbb && single_succ_p (pbb));
1327 if (!flow_bb_inside_loop_p (loop, single_succ (pbb)))
1328 insn = BB_HEAD (first[loop->num]);
1331 insn = BB_HEAD (first[loop->num]);
1333 head = BB_HEAD (first[loop->num]);
1334 emit_note_before (NOTE_INSN_LOOP_BEG, insn);
1335 BB_HEAD (first[loop->num]) = head;
1337 /* Position the note correctly wrto barrier. */
1338 insn = BB_END (last[loop->num]);
1339 if (NEXT_INSN (insn)
1340 && BARRIER_P (NEXT_INSN (insn)))
1341 insn = NEXT_INSN (insn);
1343 end = BB_END (last[loop->num]);
1344 emit_note_after (NOTE_INSN_LOOP_END, insn);
1345 BB_END (last[loop->num]) = end;
1354 flow_loops_free (&loops);
1357 /* The structure of LOOPS might have changed. Some loops might get removed
1358 (and their headers and latches were set to NULL), loop exists might get
1359 removed (thus the loop nesting may be wrong), and some blocks and edges
1360 were changed (so the information about bb --> loop mapping does not have
1361 to be correct). But still for the remaining loops the header dominates
1362 the latch, and loops did not get new subloobs (new loops might possibly
1363 get created, but we are not interested in them). Fix up the mess.
1365 If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
1369 fix_loop_structure (struct loops *loops, bitmap changed_bbs)
1372 struct loop *loop, *ploop;
1375 /* Remove the old bb -> loop mapping. */
1378 bb->aux = (void *) (size_t) bb->loop_father->depth;
1379 bb->loop_father = loops->tree_root;
1382 /* Remove the dead loops from structures. */
1383 loops->tree_root->num_nodes = n_basic_blocks + 2;
1384 for (i = 1; i < loops->num; i++)
1386 loop = loops->parray[i];
1390 loop->num_nodes = 0;
1396 ploop = loop->inner;
1397 flow_loop_tree_node_remove (ploop);
1398 flow_loop_tree_node_add (loop->outer, ploop);
1401 /* Remove the loop and free its data. */
1402 flow_loop_tree_node_remove (loop);
1403 loops->parray[loop->num] = NULL;
1404 flow_loop_free (loop);
1407 /* Rescan the bodies of loops, starting from the outermost. */
1408 loop = loops->tree_root;
1416 && loop != loops->tree_root)
1418 if (loop == loops->tree_root)
1424 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
1427 /* Now fix the loop nesting. */
1428 for (i = 1; i < loops->num; i++)
1430 loop = loops->parray[i];
1434 bb = loop_preheader_edge (loop)->src;
1435 if (bb->loop_father != loop->outer)
1437 flow_loop_tree_node_remove (loop);
1438 flow_loop_tree_node_add (bb->loop_father, loop);
1442 /* Mark the blocks whose loop has changed. */
1446 && (void *) (size_t) bb->loop_father->depth != bb->aux)
1447 bitmap_set_bit (changed_bbs, bb->index);
1452 mark_single_exit_loops (loops);
1453 mark_irreducible_loops (loops);