1 /* Loop manipulation code for GNU compiler.
2 Copyright (C) 2002, 2003 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"
27 #include "basic-block.h"
29 #include "cfglayout.h"
32 static struct loop
* duplicate_loop
PARAMS ((struct loops
*,
33 struct loop
*, struct loop
*));
34 static void duplicate_subloops
PARAMS ((struct loops
*, struct loop
*,
36 static void copy_loops_to
PARAMS ((struct loops
*, struct loop
**,
38 static void loop_redirect_edge
PARAMS ((edge
, basic_block
));
39 static bool loop_delete_branch_edge
PARAMS ((edge
));
40 static void copy_bbs
PARAMS ((basic_block
*, int, edge
,
42 struct loops
*, edge
*,
44 static void remove_bbs
PARAMS ((dominance_info
, basic_block
*,
46 static bool rpe_enum_p
PARAMS ((basic_block
, void *));
47 static int find_path
PARAMS ((edge
, dominance_info
,
49 static bool alp_enum_p
PARAMS ((basic_block
, void *));
50 static void add_loop
PARAMS ((struct loops
*, struct loop
*));
51 static void fix_loop_placements
PARAMS ((struct loop
*));
52 static bool fix_bb_placement
PARAMS ((struct loops
*, basic_block
));
53 static void fix_bb_placements
PARAMS ((struct loops
*, basic_block
));
54 static void place_new_loop
PARAMS ((struct loops
*, struct loop
*));
55 static void scale_loop_frequencies
PARAMS ((struct loop
*, int, int));
56 static void scale_bbs_frequencies
PARAMS ((basic_block
*, int, int, int));
57 static void record_exit_edges
PARAMS ((edge
, basic_block
*, int,
58 edge
*, unsigned *, int));
59 static basic_block create_preheader
PARAMS ((struct loop
*, dominance_info
,
62 /* Splits basic block BB after INSN, returns created edge. Updates loops
65 split_loop_bb (loops
, bb
, insn
)
74 /* Split the block. */
75 e
= split_block (bb
, insn
);
77 /* Add dest to loop. */
78 add_bb_to_loop (e
->dest
, e
->src
->loop_father
);
81 add_to_dominance_info (loops
->cfg
.dom
, e
->dest
);
82 n_dom_bbs
= get_dominated_by (loops
->cfg
.dom
, e
->src
, &dom_bbs
);
83 for (i
= 0; i
< n_dom_bbs
; i
++)
84 set_immediate_dominator (loops
->cfg
.dom
, dom_bbs
[i
], e
->dest
);
86 set_immediate_dominator (loops
->cfg
.dom
, e
->dest
, e
->src
);
88 /* Take care of RBI. */
89 alloc_aux_for_block (e
->dest
, sizeof (struct reorder_block_def
));
94 /* Checks whether basic block BB is dominated by RPE->DOM, where
95 RPE is passed through DATA. */
103 rpe_enum_p (bb
, data
)
107 struct rpe_data
*rpe
= data
;
108 return dominated_by_p (rpe
->doms
, bb
, rpe
->dom
);
111 /* Remove basic blocks BBS from loop structure and dominance info,
112 and delete them afterwards. */
114 remove_bbs (dom
, bbs
, nbbs
)
121 for (i
= 0; i
< nbbs
; i
++)
123 remove_bb_from_loops (bbs
[i
]);
124 delete_from_dominance_info (dom
, bbs
[i
]);
125 flow_delete_block (bbs
[i
]);
129 /* Find path -- i.e. the basic blocks dominated by edge E and put them
130 into array BBS, that will be allocated large enough to contain them.
131 The number of basic blocks in the path is returned. */
133 find_path (e
, doms
, bbs
)
141 if (e
->dest
->pred
->pred_next
)
143 for (ae
= e
->dest
->pred
; ae
; ae
= ae
->pred_next
)
144 if (ae
!= e
&& !dominated_by_p (doms
, ae
->src
, e
->dest
))
149 /* The path is formed just by the edge. */
154 /* Find bbs in the path. */
157 *bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
158 return dfs_enumerate_from (e
->dest
, 0, rpe_enum_p
, *bbs
,
159 n_basic_blocks
, &rpe
);
162 /* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
163 Let L be a loop to that BB belongs. Then every successor of BB must either
164 1) belong to some superloop of loop L, or
165 2) be a header of loop K such that K->outer is superloop of L
166 Returns true if we had to move BB into other loop to enforce this condition,
167 false if the placement of BB was already correct (provided that placements
168 of its successors are correct). */
170 fix_bb_placement (loops
, bb
)
175 struct loop
*loop
= loops
->tree_root
, *act
;
177 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
179 if (e
->dest
== EXIT_BLOCK_PTR
)
182 act
= e
->dest
->loop_father
;
183 if (act
->header
== e
->dest
)
186 if (flow_loop_nested_p (loop
, act
))
190 if (loop
== bb
->loop_father
)
193 remove_bb_from_loops (bb
);
194 add_bb_to_loop (bb
, loop
);
199 /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
200 enforce condition condition stated in description of fix_bb_placement. We
201 start from basic block FROM that had some of its successors removed, so that
202 his placement no longer has to be correct, and iteratively fix placement of
203 its predecessors that may change if placement of FROM changed. Also fix
204 placement of subloops of FROM->loop_father, that might also be altered due
205 to this change; the condition for them is simmilar, except that instead of
206 successors we consider edges coming out of the loops. */
208 fix_bb_placements (loops
, from
)
213 basic_block
*queue
, *qtop
, *qbeg
, *qend
;
214 struct loop
*base_loop
;
217 /* We pass through blocks back-reachable from FROM, testing whether some
218 of their successors moved to outer loop. It may be necessary to
219 iterate several times, but it is finite, as we stop unless we move
220 the basic block up the loop structure. The whole story is a bit
221 more complicated due to presence of subloops, those are moved using
222 fix_loop_placement. */
224 base_loop
= from
->loop_father
;
225 if (base_loop
== loops
->tree_root
)
228 in_queue
= sbitmap_alloc (last_basic_block
);
229 sbitmap_zero (in_queue
);
230 SET_BIT (in_queue
, from
->index
);
231 /* Prevent us from going out of the base_loop. */
232 SET_BIT (in_queue
, base_loop
->header
->index
);
234 queue
= xcalloc (base_loop
->num_nodes
+ 1, sizeof (basic_block
));
235 qtop
= queue
+ base_loop
->num_nodes
+ 1;
246 RESET_BIT (in_queue
, from
->index
);
248 if (from
->loop_father
->header
== from
)
250 /* Subloop header, maybe move the loop upward. */
251 if (!fix_loop_placement (from
->loop_father
))
256 /* Ordinary basic block. */
257 if (!fix_bb_placement (loops
, from
))
261 /* Something has changed, insert predecessors into queue. */
262 for (e
= from
->pred
; e
; e
= e
->pred_next
)
264 basic_block pred
= e
->src
;
267 if (TEST_BIT (in_queue
, pred
->index
))
270 /* If it is subloop, then it either was not moved, or
271 the path up the loop tree from base_loop do not contain
273 nca
= find_common_loop (pred
->loop_father
, base_loop
);
274 if (pred
->loop_father
!= base_loop
276 || nca
!= pred
->loop_father
))
277 pred
= pred
->loop_father
->header
;
278 else if (!flow_loop_nested_p (from
->loop_father
, pred
->loop_father
))
280 /* No point in processing it. */
284 if (TEST_BIT (in_queue
, pred
->index
))
287 /* Schedule the basic block. */
292 SET_BIT (in_queue
, pred
->index
);
299 /* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
300 and update loop structure stored in LOOPS and dominators. Return true if
301 we were able to remove the path, false otherwise (and nothing is affected
304 remove_path (loops
, e
)
309 basic_block
*rem_bbs
, *bord_bbs
, *dom_bbs
, from
, bb
;
310 int i
, nrem
, n_bord_bbs
, n_dom_bbs
;
313 /* First identify the path. */
314 nrem
= find_path (e
, loops
->cfg
.dom
, &rem_bbs
);
317 bord_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
318 seen
= sbitmap_alloc (last_basic_block
);
321 /* Find "border" hexes -- i.e. those with predecessor in removed path. */
322 for (i
= 0; i
< nrem
; i
++)
323 SET_BIT (seen
, rem_bbs
[i
]->index
);
326 for (i
= 0; i
< nrem
; i
++)
329 for (ae
= rem_bbs
[i
]->succ
; ae
; ae
= ae
->succ_next
)
330 if (ae
->dest
!= EXIT_BLOCK_PTR
&& !TEST_BIT (seen
, ae
->dest
->index
))
332 SET_BIT (seen
, ae
->dest
->index
);
333 bord_bbs
[n_bord_bbs
++] = ae
->dest
;
337 else if (e
->dest
!= EXIT_BLOCK_PTR
)
338 bord_bbs
[n_bord_bbs
++] = e
->dest
;
340 /* Remove the path. */
342 if (!loop_delete_branch_edge (e
))
349 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
351 /* Cancel loops contained in the path. */
352 for (i
= 0; i
< nrem
; i
++)
353 if (rem_bbs
[i
]->loop_father
->header
== rem_bbs
[i
])
354 cancel_loop_tree (loops
, rem_bbs
[i
]->loop_father
);
356 remove_bbs (loops
->cfg
.dom
, rem_bbs
, nrem
);
359 /* Find blocks with whose dominators may be affected. */
362 for (i
= 0; i
< n_bord_bbs
; i
++)
367 bb
= get_immediate_dominator (loops
->cfg
.dom
, bord_bbs
[i
]);
368 if (TEST_BIT (seen
, bb
->index
))
370 SET_BIT (seen
, bb
->index
);
372 nldom
= get_dominated_by (loops
->cfg
.dom
, bb
, &ldom
);
373 for (j
= 0; j
< nldom
; j
++)
374 if (!dominated_by_p (loops
->cfg
.dom
, from
, ldom
[j
]))
375 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
382 /* Recount dominators. */
383 iterate_fix_dominators (loops
->cfg
.dom
, dom_bbs
, n_dom_bbs
);
386 /* Fix placements of basic blocks inside loops and the placement of
387 loops in the loop tree. */
388 fix_bb_placements (loops
, from
);
389 fix_loop_placements (from
->loop_father
);
394 /* Predicate for enumeration in add_loop. */
396 alp_enum_p (bb
, alp_header
)
400 return bb
!= (basic_block
) alp_header
;
403 /* Given LOOP structure with filled header and latch, find the body of the
404 corresponding loop and add it to LOOPS tree. */
406 add_loop (loops
, loop
)
413 /* Add it to loop structure. */
414 place_new_loop (loops
, loop
);
417 /* Find its nodes. */
418 bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
419 n
= dfs_enumerate_from (loop
->latch
, 1, alp_enum_p
,
420 bbs
, n_basic_blocks
, loop
->header
);
422 for (i
= 0; i
< n
; i
++)
423 add_bb_to_loop (bbs
[i
], loop
);
424 add_bb_to_loop (loop
->header
, loop
);
429 /* Multiply all frequencies of basic blocks in array BBS of lenght NBBS
432 scale_bbs_frequencies (bbs
, nbbs
, num
, den
)
441 for (i
= 0; i
< nbbs
; i
++)
443 bbs
[i
]->frequency
= (bbs
[i
]->frequency
* num
) / den
;
444 bbs
[i
]->count
= (bbs
[i
]->count
* num
) / den
;
445 for (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
446 e
->count
= (e
->count
* num
) /den
;
450 /* Multiply all frequencies in LOOP by NUM/DEN. */
452 scale_loop_frequencies (loop
, num
, den
)
459 bbs
= get_loop_body (loop
);
460 scale_bbs_frequencies (bbs
, loop
->num_nodes
, num
, den
);
464 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
465 latch to header and update loop tree stored in LOOPS and dominators
466 accordingly. Everything between them plus LATCH_EDGE destination must
467 be dominated by HEADER_EDGE destination, and back-reachable from
468 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
469 SWITCH_BB->succ to original destination of LATCH_EDGE and
470 SWITCH_BB->succ->succ_next to original destination of HEADER_EDGE.
471 Returns newly created loop. */
473 loopify (loops
, latch_edge
, header_edge
, switch_bb
)
477 basic_block switch_bb
;
479 basic_block succ_bb
= latch_edge
->dest
;
480 basic_block pred_bb
= header_edge
->src
;
481 basic_block
*dom_bbs
, *body
;
482 unsigned n_dom_bbs
, i
, j
;
484 struct loop
*loop
= xcalloc (1, sizeof (struct loop
));
485 struct loop
*outer
= succ_bb
->loop_father
->outer
;
486 int freq
, prob
, tot_prob
;
490 loop
->header
= header_edge
->dest
;
491 loop
->latch
= latch_edge
->src
;
493 freq
= EDGE_FREQUENCY (header_edge
);
494 cnt
= header_edge
->count
;
495 prob
= switch_bb
->succ
->probability
;
496 tot_prob
= prob
+ switch_bb
->succ
->succ_next
->probability
;
500 /* Redirect edges. */
501 loop_redirect_edge (latch_edge
, loop
->header
);
502 loop_redirect_edge (header_edge
, switch_bb
);
503 loop_redirect_edge (switch_bb
->succ
->succ_next
, loop
->header
);
504 loop_redirect_edge (switch_bb
->succ
, succ_bb
);
506 /* Update dominators. */
507 set_immediate_dominator (loops
->cfg
.dom
, switch_bb
, pred_bb
);
508 set_immediate_dominator (loops
->cfg
.dom
, loop
->header
, switch_bb
);
509 set_immediate_dominator (loops
->cfg
.dom
, succ_bb
, switch_bb
);
511 /* Compute new loop. */
512 add_loop (loops
, loop
);
513 flow_loop_tree_node_add (outer
, loop
);
515 /* Add switch_bb to appropriate loop. */
516 add_bb_to_loop (switch_bb
, outer
);
518 /* Fix frequencies. */
519 switch_bb
->frequency
= freq
;
520 switch_bb
->count
= cnt
;
521 for (e
= switch_bb
->succ
; e
; e
= e
->succ_next
)
522 e
->count
= (switch_bb
->count
* e
->probability
) / REG_BR_PROB_BASE
;
523 scale_loop_frequencies (loop
, prob
, tot_prob
);
524 scale_loop_frequencies (succ_bb
->loop_father
, tot_prob
- prob
, tot_prob
);
526 /* Update dominators of blocks outside of LOOP. */
527 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
529 seen
= sbitmap_alloc (last_basic_block
);
531 body
= get_loop_body (loop
);
533 for (i
= 0; i
< loop
->num_nodes
; i
++)
534 SET_BIT (seen
, body
[i
]->index
);
536 for (i
= 0; i
< loop
->num_nodes
; i
++)
541 nldom
= get_dominated_by (loops
->cfg
.dom
, body
[i
], &ldom
);
542 for (j
= 0; j
< nldom
; j
++)
543 if (!TEST_BIT (seen
, ldom
[j
]->index
))
545 SET_BIT (seen
, ldom
[j
]->index
);
546 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
551 iterate_fix_dominators (loops
->cfg
.dom
, dom_bbs
, n_dom_bbs
);
560 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
561 FATHER of LOOP such that all of the edges comming out of LOOP belong to
562 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
565 fix_loop_placement (loop
)
571 struct loop
*father
= loop
->pred
[0], *act
;
573 body
= get_loop_body (loop
);
574 for (i
= 0; i
< loop
->num_nodes
; i
++)
575 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
576 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
578 act
= find_common_loop (loop
, e
->dest
->loop_father
);
579 if (flow_loop_nested_p (father
, act
))
584 if (father
!= loop
->outer
)
586 for (act
= loop
->outer
; act
!= father
; act
= act
->outer
)
587 act
->num_nodes
-= loop
->num_nodes
;
588 flow_loop_tree_node_remove (loop
);
589 flow_loop_tree_node_add (father
, loop
);
595 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
596 condition stated in description of fix_loop_placement holds for them.
597 It is used in case when we removed some edges coming out of LOOP, which
598 may cause the right placement of LOOP inside loop tree to change. */
600 fix_loop_placements (loop
)
608 if (!fix_loop_placement (loop
))
614 /* Creates place for a new LOOP in LOOPS structure. */
616 place_new_loop (loops
, loop
)
621 xrealloc (loops
->parray
, (loops
->num
+ 1) * sizeof (struct loop
*));
622 loops
->parray
[loops
->num
] = loop
;
624 loop
->num
= loops
->num
++;
627 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
628 created loop into LOOPS structure. */
630 duplicate_loop (loops
, loop
, target
)
636 cloop
= xcalloc (1, sizeof (struct loop
));
637 place_new_loop (loops
, cloop
);
639 /* Initialize copied loop. */
640 cloop
->level
= loop
->level
;
642 /* Set it as copy of loop. */
645 /* Add it to target. */
646 flow_loop_tree_node_add (target
, cloop
);
651 /* Copies structure of subloops of LOOP into TARGET loop, placing
652 newly created loops into loop tree stored in LOOPS. */
654 duplicate_subloops (loops
, loop
, target
)
659 struct loop
*aloop
, *cloop
;
661 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
663 cloop
= duplicate_loop (loops
, aloop
, target
);
664 duplicate_subloops (loops
, aloop
, cloop
);
668 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
669 into TARGET loop, placing newly created loops into loop tree LOOPS. */
671 copy_loops_to (loops
, copied_loops
, n
, target
)
673 struct loop
**copied_loops
;
680 for (i
= 0; i
< n
; i
++)
682 aloop
= duplicate_loop (loops
, copied_loops
[i
], target
);
683 duplicate_subloops (loops
, copied_loops
[i
], aloop
);
687 /* Redirects edge E to basic block DEST. */
689 loop_redirect_edge (e
, dest
)
696 cfg_layout_redirect_edge (e
, dest
);
699 /* Deletes edge E from a branch if possible. */
701 loop_delete_branch_edge (e
)
704 basic_block src
= e
->src
;
706 if (src
->succ
->succ_next
)
709 /* Cannot handle more than two exit edges. */
710 if (src
->succ
->succ_next
->succ_next
)
712 /* And it must be just a simple branch. */
713 if (!any_condjump_p (src
->end
))
716 newdest
= (e
== src
->succ
717 ? src
->succ
->succ_next
->dest
: src
->succ
->dest
);
718 if (newdest
== EXIT_BLOCK_PTR
)
721 return cfg_layout_redirect_edge (e
, newdest
);
725 /* Cannot happen -- we are using this only to remove an edge
730 return false; /* To avoid warning, cannot get here. */
733 /* Duplicates N basic blocks stored in array BBS (they form a body of
734 duplicated loop). Newly created basic blocks are placed into array NEW_BBS
735 that we allocate. Edges from basic blocks in BBS are also duplicated and
736 copies of those of them that lead into BBS are redirected to appropriate
737 newly created block. The function also assigns bbs into loops and updates
738 dominators. If ADD_IRREDUCIBLE_FLAG is set, newly created basic blocks that
739 are not members of any inner loop are marked irreducible.
741 Additionally, we perform following manipulation with edges:
742 We have two special edges given. LATCH_EDGE is the latch edge of the
743 duplicated loop and leads into its header (one of blocks in BBS);
744 it does not have neccessarily lead from one of the blocks, because
745 we may be copying the loop body several times in unrolling.
746 Edge ENTRY leads also leads to header, and it is either latch or entry
747 edge. Copy of LATCH_EDGE is redirected to header and is stored in
748 HEADER_EDGE, the ENTRY edge is redirected into copy of header and
749 returned as COPY_HEADER_EDGE. The effect is following:
750 if LATCH_EDGE == ENTRY, then the loop is unrolled by one copy,
751 HEADER_EDGE is latch of a new loop, COPY_HEADER_EDGE leads from original
752 latch source to first block in copy.
753 if LATCH_EDGE != ENTRY, then the loop is peeled by one copy,
754 HEADER_EDGE is entry edge of the loop, COPY_HEADER_EDGE leads from
755 original entry block to first block in peeled copy.
758 copy_bbs (bbs
, n
, entry
, latch_edge
, new_bbs
, loops
, header_edge
, copy_header_edge
, add_irreducible_flag
)
763 basic_block
**new_bbs
;
766 edge
*copy_header_edge
;
767 int add_irreducible_flag
;
770 basic_block bb
, new_bb
, header
= entry
->dest
, dom_bb
;
773 /* Duplicate bbs, update dominators, assign bbs to loops. */
774 (*new_bbs
) = xcalloc (n
, sizeof (basic_block
));
775 for (i
= 0; i
< n
; i
++)
779 new_bb
= (*new_bbs
)[i
] = cfg_layout_duplicate_bb (bb
, NULL
);
780 RBI (new_bb
)->duplicated
= 1;
782 add_bb_to_loop (new_bb
, bb
->loop_father
->copy
);
783 add_to_dominance_info (loops
->cfg
.dom
, new_bb
);
784 /* Possibly set header. */
785 if (bb
->loop_father
->header
== bb
&& bb
!= header
)
786 new_bb
->loop_father
->header
= new_bb
;
788 if (bb
->loop_father
->latch
== bb
&&
789 bb
->loop_father
!= header
->loop_father
)
790 new_bb
->loop_father
->latch
= new_bb
;
791 /* Take care of irreducible loops. */
792 if (add_irreducible_flag
793 && bb
->loop_father
== header
->loop_father
)
794 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
797 /* Set dominators. */
798 for (i
= 0; i
< n
; i
++)
801 new_bb
= (*new_bbs
)[i
];
804 /* For anything else than loop header, just copy it. */
805 dom_bb
= get_immediate_dominator (loops
->cfg
.dom
, bb
);
806 dom_bb
= RBI (dom_bb
)->copy
;
810 /* Copy of header is dominated by entry source. */
815 set_immediate_dominator (loops
->cfg
.dom
, new_bb
, dom_bb
);
818 /* Redirect edges. */
819 for (i
= 0; i
< n
; i
++)
822 new_bb
= (*new_bbs
)[i
];
824 for (e
= bb
->pred
; e
; e
= e_pred
)
826 basic_block src
= e
->src
;
828 e_pred
= e
->pred_next
;
830 if (!RBI (src
)->duplicated
)
833 /* Leads to copied loop and it is not latch edge, redirect it. */
835 loop_redirect_edge (e
, new_bb
);
839 /* Redirect header edge. */
840 bb
= RBI (latch_edge
->src
)->copy
;
841 for (e
= bb
->succ
; e
->dest
!= latch_edge
->dest
; e
= e
->succ_next
);
843 loop_redirect_edge (*header_edge
, header
);
845 /* Redirect entry to copy of header. */
846 loop_redirect_edge (entry
, RBI (header
)->copy
);
847 *copy_header_edge
= entry
;
849 /* Clear information about duplicates. */
850 for (i
= 0; i
< n
; i
++)
851 RBI ((*new_bbs
)[i
])->duplicated
= 0;
854 /* Check whether LOOP's body can be duplicated. */
856 can_duplicate_loop_p (loop
)
862 bbs
= get_loop_body (loop
);
864 for (i
= 0; i
< loop
->num_nodes
; i
++)
868 /* In case loop contains abnormal edge we can not redirect,
869 we can't perform duplication. */
871 for (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
872 if ((e
->flags
& EDGE_ABNORMAL
)
873 && flow_bb_inside_loop_p (loop
, e
->dest
))
879 if (!cfg_layout_can_duplicate_bb_p (bbs
[i
]))
890 /* Record edges, leading from NBBS basic blocks stored in BBS, that were created
891 by copying ORIG edge (or just ORIG edge if IS_ORIG is set).
892 If ORIG is NULL, then record all edges coming outside of BBS. Store them
893 into TO_REMOVE array that must be large enough to hold them all; their
894 number is returned in N_TO_REMOVE. */
896 record_exit_edges (orig
, bbs
, nbbs
, to_remove
, n_to_remove
, is_orig
)
901 unsigned *n_to_remove
;
912 to_remove
[(*n_to_remove
)++] = orig
;
916 for (e
= RBI (orig
->src
)->copy
->succ
; e
; e
= e
->succ_next
)
917 if (e
->dest
== orig
->dest
)
922 to_remove
[(*n_to_remove
)++] = e
;
926 my_blocks
= sbitmap_alloc (last_basic_block
);
927 sbitmap_zero (my_blocks
);
928 for (i
= 0; i
< nbbs
; i
++)
929 SET_BIT (my_blocks
, bbs
[i
]->index
);
931 for (i
= 0; i
< nbbs
; i
++)
932 for (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
933 if (e
->dest
== EXIT_BLOCK_PTR
||
934 !TEST_BIT (my_blocks
, e
->dest
->index
))
935 to_remove
[(*n_to_remove
)++] = e
;
942 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
944 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of
945 updating LOOPS structure and dominators. E's destination must be LOOP
946 header for this to work, i.e. it must be entry or latch edge of this loop;
947 these are unique, as the loops must have preheaders for this function to
948 work correctly (in case E is latch, the function unrolls the loop, if E is
949 entry edge, it peels the loop). Store edges created by copying ORIG edge
950 (if NULL, then all edges leaving loop) from copies corresponding to set
951 bits in WONT_EXIT bitmap (bit 0 corresponds to original LOOP body, the
952 other copies are numbered in order given by control flow through them)
953 into TO_REMOVE array. Returns false if duplication is impossible. */
955 duplicate_loop_to_header_edge (loop
, e
, loops
, ndupl
, wont_exit
, orig
,
956 to_remove
, n_to_remove
, flags
)
964 unsigned *n_to_remove
;
967 struct loop
*target
, *aloop
;
968 struct loop
**orig_loops
;
969 unsigned n_orig_loops
;
970 basic_block header
= loop
->header
, latch
= loop
->latch
;
971 basic_block
*new_bbs
, *bbs
, *first_active
;
972 basic_block new_bb
, bb
, first_active_latch
= NULL
;
973 edge ae
, latch_edge
, he
;
975 int is_latch
= (latch
== e
->src
);
976 int scale_act
= 0, *scale_step
= NULL
, scale_main
= 0;
977 int p
, freq_in
, freq_le
, freq_out_orig
;
978 int prob_pass_thru
, prob_pass_wont_exit
, prob_pass_main
;
979 int add_irreducible_flag
;
981 if (e
->dest
!= loop
->header
)
988 /* Orig must be edge out of the loop. */
989 if (!flow_bb_inside_loop_p (loop
, orig
->src
))
991 if (flow_bb_inside_loop_p (loop
, orig
->dest
))
995 bbs
= get_loop_body (loop
);
997 /* Check whether duplication is possible. */
999 for (i
= 0; i
< loop
->num_nodes
; i
++)
1001 if (!cfg_layout_can_duplicate_bb_p (bbs
[i
]))
1008 add_irreducible_flag
= !is_latch
&& (e
->src
->flags
& BB_IRREDUCIBLE_LOOP
);
1010 /* Find edge from latch. */
1011 latch_edge
= loop_latch_edge (loop
);
1013 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1015 /* Calculate coefficients by that we have to scale frequencies
1016 of duplicated loop bodies. */
1017 freq_in
= header
->frequency
;
1018 freq_le
= EDGE_FREQUENCY (latch_edge
);
1021 if (freq_in
< freq_le
)
1023 freq_out_orig
= orig
? EDGE_FREQUENCY (orig
) : freq_in
- freq_le
;
1024 if (freq_out_orig
> freq_in
- freq_le
)
1025 freq_out_orig
= freq_in
- freq_le
;
1026 prob_pass_thru
= RDIV (REG_BR_PROB_BASE
* freq_le
, freq_in
);
1027 prob_pass_wont_exit
=
1028 RDIV (REG_BR_PROB_BASE
* (freq_le
+ freq_out_orig
), freq_in
);
1030 scale_step
= xmalloc (ndupl
* sizeof (int));
1032 for (i
= 1; i
<= ndupl
; i
++)
1033 scale_step
[i
- 1] = TEST_BIT (wont_exit
, i
)
1034 ? prob_pass_wont_exit
1039 prob_pass_main
= TEST_BIT (wont_exit
, 0)
1040 ? prob_pass_wont_exit
1043 scale_main
= REG_BR_PROB_BASE
;
1044 for (i
= 0; i
< ndupl
; i
++)
1047 p
= RDIV (p
* scale_step
[i
], REG_BR_PROB_BASE
);
1049 scale_main
= RDIV (REG_BR_PROB_BASE
* REG_BR_PROB_BASE
, scale_main
);
1050 scale_act
= RDIV (scale_main
* prob_pass_main
, REG_BR_PROB_BASE
);
1054 scale_main
= REG_BR_PROB_BASE
;
1055 for (i
= 0; i
< ndupl
; i
++)
1056 scale_main
= RDIV (scale_main
* scale_step
[i
], REG_BR_PROB_BASE
);
1057 scale_act
= REG_BR_PROB_BASE
- prob_pass_thru
;
1059 for (i
= 0; i
< ndupl
; i
++)
1060 if (scale_step
[i
] < 0 || scale_step
[i
] > REG_BR_PROB_BASE
)
1062 if (scale_main
< 0 || scale_main
> REG_BR_PROB_BASE
1063 || scale_act
< 0 || scale_act
> REG_BR_PROB_BASE
)
1067 /* Loop the new bbs will belong to. */
1068 target
= find_common_loop (e
->src
->loop_father
, e
->dest
->loop_father
);
1070 /* Original loops. */
1072 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
1074 orig_loops
= xcalloc (n_orig_loops
, sizeof (struct loop
*));
1075 for (aloop
= loop
->inner
, i
= 0; aloop
; aloop
= aloop
->next
, i
++)
1076 orig_loops
[i
] = aloop
;
1078 loop
->copy
= target
;
1080 /* Original basic blocks. */
1081 n
= loop
->num_nodes
;
1083 first_active
= xcalloc(n
, sizeof (basic_block
));
1086 memcpy (first_active
, bbs
, n
* sizeof (basic_block
));
1087 first_active_latch
= latch
;
1090 /* Record exit edges in original loop body. */
1091 if (TEST_BIT (wont_exit
, 0))
1092 record_exit_edges (orig
, bbs
, n
, to_remove
, n_to_remove
, true);
1094 for (j
= 0; j
< ndupl
; j
++)
1097 copy_loops_to (loops
, orig_loops
, n_orig_loops
, target
);
1100 copy_bbs (bbs
, n
, e
, latch_edge
, &new_bbs
, loops
,
1101 &e
, &he
, add_irreducible_flag
);
1103 loop
->latch
= RBI (latch
)->copy
;
1105 /* Record exit edges in this copy. */
1106 if (TEST_BIT (wont_exit
, j
+ 1))
1107 record_exit_edges (orig
, new_bbs
, n
, to_remove
, n_to_remove
, false);
1109 /* Set counts and frequencies. */
1110 for (i
= 0; i
< n
; i
++)
1112 new_bb
= new_bbs
[i
];
1115 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1117 new_bb
->count
= RDIV (scale_act
* bb
->count
, REG_BR_PROB_BASE
);
1118 new_bb
->frequency
= RDIV (scale_act
* bb
->frequency
,
1123 new_bb
->count
= bb
->count
;
1124 new_bb
->frequency
= bb
->frequency
;
1127 for (ae
= new_bb
->succ
; ae
; ae
= ae
->succ_next
)
1128 ae
->count
= RDIV (new_bb
->count
* ae
->probability
,
1131 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1132 scale_act
= RDIV (scale_act
* scale_step
[j
], REG_BR_PROB_BASE
);
1134 if (!first_active_latch
)
1136 memcpy (first_active
, new_bbs
, n
* sizeof (basic_block
));
1137 first_active_latch
= RBI (latch
)->copy
;
1142 /* Original loop header is dominated by latch copy
1143 if we duplicated on its only entry edge. */
1144 if (!is_latch
&& !header
->pred
->pred_next
->pred_next
)
1145 set_immediate_dominator (loops
->cfg
.dom
, header
, RBI (latch
)->copy
);
1146 if (is_latch
&& j
== 0)
1148 /* Update edge from latch. */
1149 for (latch_edge
= RBI (header
)->copy
->pred
;
1150 latch_edge
->src
!= latch
;
1151 latch_edge
= latch_edge
->pred_next
);
1154 /* Now handle original loop. */
1156 /* Update edge counts. */
1157 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1159 for (i
= 0; i
< n
; i
++)
1162 bb
->count
= RDIV (scale_main
* bb
->count
, REG_BR_PROB_BASE
);
1163 bb
->frequency
= RDIV (scale_main
* bb
->frequency
, REG_BR_PROB_BASE
);
1164 for (ae
= bb
->succ
; ae
; ae
= ae
->succ_next
)
1165 ae
->count
= RDIV (bb
->count
* ae
->probability
, REG_BR_PROB_BASE
);
1171 /* Update dominators of other blocks if affected. */
1172 for (i
= 0; i
< n
; i
++)
1174 basic_block dominated
, dom_bb
, *dom_bbs
;
1178 n_dom_bbs
= get_dominated_by (loops
->cfg
.dom
, bb
, &dom_bbs
);
1179 for (j
= 0; j
< n_dom_bbs
; j
++)
1181 dominated
= dom_bbs
[j
];
1182 if (flow_bb_inside_loop_p (loop
, dominated
))
1184 dom_bb
= nearest_common_dominator (
1185 loops
->cfg
.dom
, first_active
[i
], first_active_latch
);
1186 set_immediate_dominator (loops
->cfg
.dom
, dominated
, dom_bb
);
1190 free (first_active
);
1197 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1198 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1199 entry; otherwise we also force preheader block to have only one successor.
1200 The function also updates dominators stored in DOM. */
1202 create_preheader (loop
, dom
, flags
)
1209 basic_block jump
, src
= 0;
1210 struct loop
*cloop
, *ploop
;
1214 cloop
= loop
->outer
;
1216 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1218 if (e
->src
== loop
->latch
)
1226 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
);
1227 if (!(flags
& CP_SIMPLE_PREHEADERS
)
1228 || !e
->src
->succ
->succ_next
)
1232 insn
= first_insn_after_basic_block_note (loop
->header
);
1234 insn
= PREV_INSN (insn
);
1236 insn
= get_last_insn ();
1237 if (insn
== loop
->header
->end
)
1239 /* Split_block would not split block after its end. */
1240 emit_note_after (NOTE_INSN_DELETED
, insn
);
1242 if (flags
& CP_INSIDE_CFGLAYOUT
)
1243 fallthru
= cfg_layout_split_block (loop
->header
, insn
);
1245 fallthru
= split_block (loop
->header
, insn
);
1246 dummy
= fallthru
->src
;
1247 loop
->header
= fallthru
->dest
;
1249 /* The header could be a latch of some superloop(s); due to design of
1250 split_block, it would now move to fallthru->dest. */
1251 for (ploop
= loop
; ploop
; ploop
= ploop
->outer
)
1252 if (ploop
->latch
== dummy
)
1253 ploop
->latch
= fallthru
->dest
;
1255 add_to_dominance_info (dom
, fallthru
->dest
);
1257 /* Redirect edges. */
1258 for (e
= dummy
->pred
; e
; e
= e
->pred_next
)
1261 if (src
== loop
->latch
)
1267 dummy
->frequency
-= EDGE_FREQUENCY (e
);
1268 dummy
->count
-= e
->count
;
1269 fallthru
->count
-= e
->count
;
1270 if (flags
& CP_INSIDE_CFGLAYOUT
)
1271 cfg_layout_redirect_edge (e
, loop
->header
);
1274 jump
= redirect_edge_and_branch_force (e
, loop
->header
);
1277 add_to_dominance_info (dom
, jump
);
1278 set_immediate_dominator (dom
, jump
, src
);
1279 add_bb_to_loop (jump
, loop
);
1284 /* Update structures. */
1285 redirect_immediate_dominators (dom
, dummy
, loop
->header
);
1286 set_immediate_dominator (dom
, loop
->header
, dummy
);
1287 loop
->header
->loop_father
= loop
;
1288 add_bb_to_loop (dummy
, cloop
);
1290 fprintf (rtl_dump_file
, "Created preheader block for loop %i\n",
1296 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1297 of FLAGS see create_preheader. */
1299 create_preheaders (loops
, flags
)
1300 struct loops
*loops
;
1304 for (i
= 1; i
< loops
->num
; i
++)
1305 create_preheader (loops
->parray
[i
], loops
->cfg
.dom
, flags
);
1306 loops
->state
|= LOOPS_HAVE_PREHEADERS
;
1309 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1312 force_single_succ_latches (loops
)
1313 struct loops
*loops
;
1319 for (i
= 1; i
< loops
->num
; i
++)
1321 loop
= loops
->parray
[i
];
1322 if (!loop
->latch
->succ
->succ_next
)
1325 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1328 loop_split_edge_with (e
, NULL_RTX
, loops
);
1330 loops
->state
|= LOOPS_HAVE_SIMPLE_LATCHES
;
1333 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1334 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1335 be ok after this function. The created block is placed on correct place
1336 in LOOPS structure and its dominator is set. */
1338 loop_split_edge_with (e
, insns
, loops
)
1341 struct loops
*loops
;
1343 basic_block src
, dest
, new_bb
;
1344 struct loop
*loop_c
;
1350 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
1352 /* Create basic block for it. */
1354 new_bb
= create_basic_block (NULL_RTX
, NULL_RTX
, EXIT_BLOCK_PTR
->prev_bb
);
1355 add_to_dominance_info (loops
->cfg
.dom
, new_bb
);
1356 add_bb_to_loop (new_bb
, loop_c
);
1357 new_bb
->flags
= insns
? BB_SUPERBLOCK
: 0;
1358 if (src
->flags
& BB_IRREDUCIBLE_LOOP
)
1360 /* We expect simple preheaders here. */
1361 if ((dest
->flags
& BB_IRREDUCIBLE_LOOP
)
1362 || dest
->loop_father
->header
== dest
)
1363 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1366 new_e
= make_edge (new_bb
, dest
, EDGE_FALLTHRU
);
1367 new_e
->probability
= REG_BR_PROB_BASE
;
1368 new_e
->count
= e
->count
;
1370 new_bb
->count
= e
->count
;
1371 new_bb
->frequency
= EDGE_FREQUENCY (e
);
1372 cfg_layout_redirect_edge (e
, new_bb
);
1374 alloc_aux_for_block (new_bb
, sizeof (struct reorder_block_def
));
1379 insns
= get_insns ();
1381 emit_insn_after (insns
, new_bb
->end
);
1384 set_immediate_dominator (loops
->cfg
.dom
, new_bb
, src
);
1385 set_immediate_dominator (loops
->cfg
.dom
, dest
,
1386 recount_dominator (loops
->cfg
.dom
, dest
));
1388 if (dest
->loop_father
->latch
== src
)
1389 dest
->loop_father
->latch
= new_bb
;