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
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 void duplicate_subloops (struct loops
*, struct loop
*, struct loop
*);
33 static void copy_loops_to (struct loops
*, struct loop
**, int,
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
56 split_loop_bb (basic_block bb
, void *insn
)
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
);
69 /* Checks whether basic block BB is dominated by DATA. */
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. */
79 remove_bbs (basic_block
*bbs
, int nbbs
)
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
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). */
115 fix_bb_placement (struct loops
*loops
, basic_block bb
)
119 struct loop
*loop
= loops
->tree_root
, *act
;
121 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
123 if (e
->dest
== EXIT_BLOCK_PTR
)
126 act
= e
->dest
->loop_father
;
127 if (act
->header
== e
->dest
)
130 if (flow_loop_nested_p (loop
, act
))
134 if (loop
== bb
->loop_father
)
137 remove_bb_from_loops (bb
);
138 add_bb_to_loop (bb
, loop
);
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. */
152 fix_bb_placements (struct loops
*loops
, basic_block from
)
155 basic_block
*queue
, *qtop
, *qbeg
, *qend
;
156 struct loop
*base_loop
;
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
)
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;
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
))
199 /* Ordinary basic block. */
200 if (!fix_bb_placement (loops
, from
))
204 /* Something has changed, insert predecessors into queue. */
205 FOR_EACH_EDGE (e
, ei
, from
->preds
)
207 basic_block pred
= e
->src
;
210 if (TEST_BIT (in_queue
, pred
->index
))
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
216 nca
= find_common_loop (pred
->loop_father
, base_loop
);
217 if (pred
->loop_father
!= 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. */
227 if (TEST_BIT (in_queue
, pred
->index
))
230 /* Schedule the basic block. */
235 SET_BIT (in_queue
, pred
->index
);
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. */
246 fix_irreducible_loops (basic_block from
)
255 if (!(from
->flags
& BB_IRREDUCIBLE_LOOP
))
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
));
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
)
277 bb
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
278 if (bb
->loop_father
->header
== bb
)
279 edges
= get_loop_exit_edges (bb
->loop_father
, &n_edges
);
282 n_edges
= EDGE_COUNT (bb
->succs
);
283 edges
= xmalloc (n_edges
* sizeof (edge
));
284 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
288 for (i
= 0; i
< n_edges
; i
++)
292 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
294 if (!flow_bb_inside_loop_p (from
->loop_father
, e
->dest
))
297 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
298 if (TEST_BIT (on_stack
, e
->dest
->index
))
301 SET_BIT (on_stack
, e
->dest
->index
);
302 stack
[stack_top
++] = e
->dest
;
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
317 remove_path (struct loops
*loops
, edge e
)
320 basic_block
*rem_bbs
, *bord_bbs
, *dom_bbs
, from
, bb
;
321 int i
, nrem
, n_bord_bbs
, n_dom_bbs
;
325 if (!loop_delete_branch_edge (e
, 0))
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
);
348 bord_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
349 seen
= sbitmap_alloc (last_basic_block
);
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
++)
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. */
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
);
381 /* Find blocks whose dominators may be affected. */
384 for (i
= 0; i
< n_bord_bbs
; i
++)
388 bb
= get_immediate_dominator (CDI_DOMINATORS
, bord_bbs
[i
]);
389 if (TEST_BIT (seen
, bb
->index
))
391 SET_BIT (seen
, bb
->index
);
393 for (ldom
= first_dom_son (CDI_DOMINATORS
, bb
);
395 ldom
= next_dom_son (CDI_DOMINATORS
, ldom
))
396 if (!dominated_by_p (CDI_DOMINATORS
, from
, ldom
))
397 dom_bbs
[n_dom_bbs
++] = ldom
;
402 /* Recount dominators. */
403 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_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
]);
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
);
420 /* Predicate for enumeration in add_loop. */
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. */
430 add_loop (struct loops
*loops
, struct loop
*loop
)
435 /* Add it to loop structure. */
436 place_new_loop (loops
, loop
);
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
);
451 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
454 scale_bbs_frequencies (basic_block
*bbs
, int nbbs
, int num
, int den
)
459 for (i
= 0; i
< nbbs
; i
++)
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. */
471 scale_loop_frequencies (struct loop
*loop
, int num
, int den
)
475 bbs
= get_loop_body (loop
);
476 scale_bbs_frequencies (bbs
, loop
->num_nodes
, num
, den
);
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 FALLTHRU_EDGE (SWITCH_BB) to original destination of HEADER_EDGE and
486 BRANCH_EDGE (SWITCH_BB) to original destination of LATCH_EDGE.
487 Returns newly created loop. */
490 loopify (struct loops
*loops
, edge latch_edge
, edge header_edge
,
491 basic_block switch_bb
, bool redirect_all_edges
)
493 basic_block succ_bb
= latch_edge
->dest
;
494 basic_block pred_bb
= header_edge
->src
;
495 basic_block
*dom_bbs
, *body
;
496 unsigned n_dom_bbs
, i
;
498 struct loop
*loop
= xcalloc (1, sizeof (struct loop
));
499 struct loop
*outer
= succ_bb
->loop_father
->outer
;
500 int freq
, prob
, tot_prob
;
505 loop
->header
= header_edge
->dest
;
506 loop
->latch
= latch_edge
->src
;
508 freq
= EDGE_FREQUENCY (header_edge
);
509 cnt
= header_edge
->count
;
510 prob
= EDGE_SUCC (switch_bb
, 0)->probability
;
511 tot_prob
= prob
+ EDGE_SUCC (switch_bb
, 1)->probability
;
515 /* Redirect edges. */
516 loop_redirect_edge (latch_edge
, loop
->header
);
517 loop_redirect_edge (BRANCH_EDGE (switch_bb
), succ_bb
);
519 /* During loop versioning, one of the switch_bb edge is already properly
520 set. Do not redirect it again unless redirect_all_edges is true. */
521 if (redirect_all_edges
)
523 loop_redirect_edge (header_edge
, switch_bb
);
524 loop_redirect_edge (FALLTHRU_EDGE (switch_bb
), loop
->header
);
526 /* Update dominators. */
527 set_immediate_dominator (CDI_DOMINATORS
, switch_bb
, pred_bb
);
528 set_immediate_dominator (CDI_DOMINATORS
, loop
->header
, switch_bb
);
531 set_immediate_dominator (CDI_DOMINATORS
, succ_bb
, switch_bb
);
533 /* Compute new loop. */
534 add_loop (loops
, loop
);
535 flow_loop_tree_node_add (outer
, loop
);
537 /* Add switch_bb to appropriate loop. */
538 add_bb_to_loop (switch_bb
, outer
);
540 /* Fix frequencies. */
541 switch_bb
->frequency
= freq
;
542 switch_bb
->count
= cnt
;
543 FOR_EACH_EDGE (e
, ei
, switch_bb
->succs
)
544 e
->count
= (switch_bb
->count
* e
->probability
) / REG_BR_PROB_BASE
;
545 scale_loop_frequencies (loop
, prob
, tot_prob
);
546 scale_loop_frequencies (succ_bb
->loop_father
, tot_prob
- prob
, tot_prob
);
548 /* Update dominators of blocks outside of LOOP. */
549 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
551 seen
= sbitmap_alloc (last_basic_block
);
553 body
= get_loop_body (loop
);
555 for (i
= 0; i
< loop
->num_nodes
; i
++)
556 SET_BIT (seen
, body
[i
]->index
);
558 for (i
= 0; i
< loop
->num_nodes
; i
++)
562 for (ldom
= first_dom_son (CDI_DOMINATORS
, body
[i
]);
564 ldom
= next_dom_son (CDI_DOMINATORS
, ldom
))
565 if (!TEST_BIT (seen
, ldom
->index
))
567 SET_BIT (seen
, ldom
->index
);
568 dom_bbs
[n_dom_bbs
++] = ldom
;
572 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
581 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
582 the LOOP was removed. After this function, original loop latch will
583 have no successor, which caller is expected to fix somehow. */
585 unloop (struct loops
*loops
, struct loop
*loop
)
590 basic_block latch
= loop
->latch
;
594 /* This is relatively straightforward. The dominators are unchanged, as
595 loop header dominates loop latch, so the only thing we have to care of
596 is the placement of loops and basic blocks inside the loop tree. We
597 move them all to the loop->outer, and then let fix_bb_placements do
600 body
= get_loop_body (loop
);
601 edges
= get_loop_exit_edges (loop
, &n_edges
);
603 for (i
= 0; i
< n
; i
++)
604 if (body
[i
]->loop_father
== loop
)
606 remove_bb_from_loops (body
[i
]);
607 add_bb_to_loop (body
[i
], loop
->outer
);
614 flow_loop_tree_node_remove (ploop
);
615 flow_loop_tree_node_add (loop
->outer
, ploop
);
618 /* Remove the loop and free its data. */
619 flow_loop_tree_node_remove (loop
);
620 loops
->parray
[loop
->num
] = NULL
;
621 flow_loop_free (loop
);
623 remove_edge (EDGE_SUCC (latch
, 0));
624 fix_bb_placements (loops
, latch
);
626 /* If the loop was inside an irreducible region, we would have to somehow
627 update the irreducible marks inside its body. While it is certainly
628 possible to do, it is a bit complicated and this situation should be
629 very rare, so we just remark all loops in this case. */
630 for (i
= 0; i
< n_edges
; i
++)
631 if (edges
[i
]->flags
& EDGE_IRREDUCIBLE_LOOP
)
634 mark_irreducible_loops (loops
);
638 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
639 FATHER of LOOP such that all of the edges coming out of LOOP belong to
640 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
643 fix_loop_placement (struct loop
*loop
)
649 struct loop
*father
= loop
->pred
[0], *act
;
651 body
= get_loop_body (loop
);
652 for (i
= 0; i
< loop
->num_nodes
; i
++)
653 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
654 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
656 act
= find_common_loop (loop
, e
->dest
->loop_father
);
657 if (flow_loop_nested_p (father
, act
))
662 if (father
!= loop
->outer
)
664 for (act
= loop
->outer
; act
!= father
; act
= act
->outer
)
665 act
->num_nodes
-= loop
->num_nodes
;
666 flow_loop_tree_node_remove (loop
);
667 flow_loop_tree_node_add (father
, loop
);
673 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
674 condition stated in description of fix_loop_placement holds for them.
675 It is used in case when we removed some edges coming out of LOOP, which
676 may cause the right placement of LOOP inside loop tree to change. */
678 fix_loop_placements (struct loops
*loops
, struct loop
*loop
)
685 if (!fix_loop_placement (loop
))
688 /* Changing the placement of a loop in the loop tree may alter the
689 validity of condition 2) of the description of fix_bb_placement
690 for its preheader, because the successor is the header and belongs
691 to the loop. So call fix_bb_placements to fix up the placement
692 of the preheader and (possibly) of its predecessors. */
693 fix_bb_placements (loops
, loop_preheader_edge (loop
)->src
);
698 /* Creates place for a new LOOP in LOOPS structure. */
700 place_new_loop (struct loops
*loops
, struct loop
*loop
)
703 xrealloc (loops
->parray
, (loops
->num
+ 1) * sizeof (struct loop
*));
704 loops
->parray
[loops
->num
] = loop
;
706 loop
->num
= loops
->num
++;
709 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
710 created loop into LOOPS structure. */
712 duplicate_loop (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
715 cloop
= xcalloc (1, sizeof (struct loop
));
716 place_new_loop (loops
, cloop
);
718 /* Initialize copied loop. */
719 cloop
->level
= loop
->level
;
721 /* Set it as copy of loop. */
724 /* Add it to target. */
725 flow_loop_tree_node_add (target
, cloop
);
730 /* Copies structure of subloops of LOOP into TARGET loop, placing
731 newly created loops into loop tree stored in LOOPS. */
733 duplicate_subloops (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
735 struct loop
*aloop
, *cloop
;
737 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
739 cloop
= duplicate_loop (loops
, aloop
, target
);
740 duplicate_subloops (loops
, aloop
, cloop
);
744 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
745 into TARGET loop, placing newly created loops into loop tree LOOPS. */
747 copy_loops_to (struct loops
*loops
, struct loop
**copied_loops
, int n
, struct loop
*target
)
752 for (i
= 0; i
< n
; i
++)
754 aloop
= duplicate_loop (loops
, copied_loops
[i
], target
);
755 duplicate_subloops (loops
, copied_loops
[i
], aloop
);
759 /* Redirects edge E to basic block DEST. */
761 loop_redirect_edge (edge e
, basic_block dest
)
766 redirect_edge_and_branch_force (e
, dest
);
769 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
770 just test whether it is possible to remove the edge. */
772 loop_delete_branch_edge (edge e
, int really_delete
)
774 basic_block src
= e
->src
;
779 gcc_assert (EDGE_COUNT (src
->succs
) > 1);
781 /* Cannot handle more than two exit edges. */
782 if (EDGE_COUNT (src
->succs
) > 2)
784 /* And it must be just a simple branch. */
785 if (!any_condjump_p (BB_END (src
)))
788 snd
= e
== EDGE_SUCC (src
, 0) ? EDGE_SUCC (src
, 1) : EDGE_SUCC (src
, 0);
790 if (newdest
== EXIT_BLOCK_PTR
)
793 /* Hopefully the above conditions should suffice. */
797 /* Redirecting behaves wrongly wrto this flag. */
798 irr
= snd
->flags
& EDGE_IRREDUCIBLE_LOOP
;
800 if (!redirect_edge_and_branch (e
, newdest
))
802 EDGE_SUCC (src
, 0)->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
803 EDGE_SUCC (src
, 0)->flags
|= irr
;
808 /* Check whether LOOP's body can be duplicated. */
810 can_duplicate_loop_p (struct loop
*loop
)
813 basic_block
*bbs
= get_loop_body (loop
);
815 ret
= can_copy_bbs_p (bbs
, loop
->num_nodes
);
821 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
822 to LOOP. Update the single_exit information in superloops of LOOP. */
825 update_single_exits_after_duplication (basic_block
*bbs
, unsigned nbbs
,
830 for (i
= 0; i
< nbbs
; i
++)
831 bbs
[i
]->rbi
->duplicated
= 1;
833 for (; loop
->outer
; loop
= loop
->outer
)
835 if (!loop
->single_exit
)
838 if (loop
->single_exit
->src
->rbi
->duplicated
)
839 loop
->single_exit
= NULL
;
842 for (i
= 0; i
< nbbs
; i
++)
843 bbs
[i
]->rbi
->duplicated
= 0;
846 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
847 LOOPS structure and dominators. E's destination must be LOOP header for
848 this to work, i.e. it must be entry or latch edge of this loop; these are
849 unique, as the loops must have preheaders for this function to work
850 correctly (in case E is latch, the function unrolls the loop, if E is entry
851 edge, it peels the loop). Store edges created by copying ORIG edge from
852 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
853 original LOOP body, the other copies are numbered in order given by control
854 flow through them) into TO_REMOVE array. Returns false if duplication is
857 duplicate_loop_to_header_edge (struct loop
*loop
, edge e
, struct loops
*loops
,
858 unsigned int ndupl
, sbitmap wont_exit
,
859 edge orig
, edge
*to_remove
,
860 unsigned int *n_to_remove
, int flags
)
862 struct loop
*target
, *aloop
;
863 struct loop
**orig_loops
;
864 unsigned n_orig_loops
;
865 basic_block header
= loop
->header
, latch
= loop
->latch
;
866 basic_block
*new_bbs
, *bbs
, *first_active
;
867 basic_block new_bb
, bb
, first_active_latch
= NULL
;
869 edge spec_edges
[2], new_spec_edges
[2];
873 int is_latch
= (latch
== e
->src
);
874 int scale_act
= 0, *scale_step
= NULL
, scale_main
= 0;
875 int p
, freq_in
, freq_le
, freq_out_orig
;
876 int prob_pass_thru
, prob_pass_wont_exit
, prob_pass_main
;
877 int add_irreducible_flag
;
879 gcc_assert (e
->dest
== loop
->header
);
880 gcc_assert (ndupl
> 0);
884 /* Orig must be edge out of the loop. */
885 gcc_assert (flow_bb_inside_loop_p (loop
, orig
->src
));
886 gcc_assert (!flow_bb_inside_loop_p (loop
, orig
->dest
));
889 bbs
= get_loop_body (loop
);
891 /* Check whether duplication is possible. */
892 if (!can_copy_bbs_p (bbs
, loop
->num_nodes
))
897 new_bbs
= xmalloc (sizeof (basic_block
) * loop
->num_nodes
);
899 /* In case we are doing loop peeling and the loop is in the middle of
900 irreducible region, the peeled copies will be inside it too. */
901 add_irreducible_flag
= e
->flags
& EDGE_IRREDUCIBLE_LOOP
;
902 gcc_assert (!is_latch
|| !add_irreducible_flag
);
904 /* Find edge from latch. */
905 latch_edge
= loop_latch_edge (loop
);
907 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
909 /* Calculate coefficients by that we have to scale frequencies
910 of duplicated loop bodies. */
911 freq_in
= header
->frequency
;
912 freq_le
= EDGE_FREQUENCY (latch_edge
);
915 if (freq_in
< freq_le
)
917 freq_out_orig
= orig
? EDGE_FREQUENCY (orig
) : freq_in
- freq_le
;
918 if (freq_out_orig
> freq_in
- freq_le
)
919 freq_out_orig
= freq_in
- freq_le
;
920 prob_pass_thru
= RDIV (REG_BR_PROB_BASE
* freq_le
, freq_in
);
921 prob_pass_wont_exit
=
922 RDIV (REG_BR_PROB_BASE
* (freq_le
+ freq_out_orig
), freq_in
);
924 scale_step
= xmalloc (ndupl
* sizeof (int));
926 for (i
= 1; i
<= ndupl
; i
++)
927 scale_step
[i
- 1] = TEST_BIT (wont_exit
, i
)
928 ? prob_pass_wont_exit
933 prob_pass_main
= TEST_BIT (wont_exit
, 0)
934 ? prob_pass_wont_exit
937 scale_main
= REG_BR_PROB_BASE
;
938 for (i
= 0; i
< ndupl
; i
++)
941 p
= RDIV (p
* scale_step
[i
], REG_BR_PROB_BASE
);
943 scale_main
= RDIV (REG_BR_PROB_BASE
* REG_BR_PROB_BASE
, scale_main
);
944 scale_act
= RDIV (scale_main
* prob_pass_main
, REG_BR_PROB_BASE
);
948 scale_main
= REG_BR_PROB_BASE
;
949 for (i
= 0; i
< ndupl
; i
++)
950 scale_main
= RDIV (scale_main
* scale_step
[i
], REG_BR_PROB_BASE
);
951 scale_act
= REG_BR_PROB_BASE
- prob_pass_thru
;
953 for (i
= 0; i
< ndupl
; i
++)
954 gcc_assert (scale_step
[i
] >= 0 && scale_step
[i
] <= REG_BR_PROB_BASE
);
955 gcc_assert (scale_main
>= 0 && scale_main
<= REG_BR_PROB_BASE
956 && scale_act
>= 0 && scale_act
<= REG_BR_PROB_BASE
);
959 /* Loop the new bbs will belong to. */
960 target
= e
->src
->loop_father
;
962 /* Original loops. */
964 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
966 orig_loops
= xcalloc (n_orig_loops
, sizeof (struct loop
*));
967 for (aloop
= loop
->inner
, i
= 0; aloop
; aloop
= aloop
->next
, i
++)
968 orig_loops
[i
] = aloop
;
974 first_active
= xmalloc (n
* sizeof (basic_block
));
977 memcpy (first_active
, bbs
, n
* sizeof (basic_block
));
978 first_active_latch
= latch
;
981 /* Update the information about single exits. */
982 if (loops
->state
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
983 update_single_exits_after_duplication (bbs
, n
, target
);
985 /* Record exit edge in original loop body. */
986 if (orig
&& TEST_BIT (wont_exit
, 0))
987 to_remove
[(*n_to_remove
)++] = orig
;
989 spec_edges
[SE_ORIG
] = orig
;
990 spec_edges
[SE_LATCH
] = latch_edge
;
992 for (j
= 0; j
< ndupl
; j
++)
995 copy_loops_to (loops
, orig_loops
, n_orig_loops
, target
);
998 copy_bbs (bbs
, n
, new_bbs
, spec_edges
, 2, new_spec_edges
, loop
);
1000 for (i
= 0; i
< n
; i
++)
1001 new_bbs
[i
]->rbi
->copy_number
= j
+ 1;
1003 /* Note whether the blocks and edges belong to an irreducible loop. */
1004 if (add_irreducible_flag
)
1006 for (i
= 0; i
< n
; i
++)
1007 new_bbs
[i
]->rbi
->duplicated
= 1;
1008 for (i
= 0; i
< n
; i
++)
1011 new_bb
= new_bbs
[i
];
1012 if (new_bb
->loop_father
== target
)
1013 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1015 FOR_EACH_EDGE (ae
, ei
, new_bb
->succs
)
1016 if (ae
->dest
->rbi
->duplicated
1017 && (ae
->src
->loop_father
== target
1018 || ae
->dest
->loop_father
== target
))
1019 ae
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1021 for (i
= 0; i
< n
; i
++)
1022 new_bbs
[i
]->rbi
->duplicated
= 0;
1025 /* Redirect the special edges. */
1028 redirect_edge_and_branch_force (latch_edge
, new_bbs
[0]);
1029 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1031 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], latch
);
1032 latch
= loop
->latch
= new_bbs
[1];
1033 e
= latch_edge
= new_spec_edges
[SE_LATCH
];
1037 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1039 redirect_edge_and_branch_force (e
, new_bbs
[0]);
1040 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], e
->src
);
1041 e
= new_spec_edges
[SE_LATCH
];
1044 /* Record exit edge in this copy. */
1045 if (orig
&& TEST_BIT (wont_exit
, j
+ 1))
1046 to_remove
[(*n_to_remove
)++] = new_spec_edges
[SE_ORIG
];
1048 /* Record the first copy in the control flow order if it is not
1049 the original loop (i.e. in case of peeling). */
1050 if (!first_active_latch
)
1052 memcpy (first_active
, new_bbs
, n
* sizeof (basic_block
));
1053 first_active_latch
= new_bbs
[1];
1056 /* Set counts and frequencies. */
1057 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1059 scale_bbs_frequencies (new_bbs
, n
, scale_act
, REG_BR_PROB_BASE
);
1060 scale_act
= RDIV (scale_act
* scale_step
[j
], REG_BR_PROB_BASE
);
1066 /* Update the original loop. */
1068 set_immediate_dominator (CDI_DOMINATORS
, e
->dest
, e
->src
);
1069 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1071 scale_bbs_frequencies (bbs
, n
, scale_main
, REG_BR_PROB_BASE
);
1075 /* Update dominators of outer blocks if affected. */
1076 for (i
= 0; i
< n
; i
++)
1078 basic_block dominated
, dom_bb
, *dom_bbs
;
1082 bb
->rbi
->copy_number
= 0;
1084 n_dom_bbs
= get_dominated_by (CDI_DOMINATORS
, bb
, &dom_bbs
);
1085 for (j
= 0; j
< n_dom_bbs
; j
++)
1087 dominated
= dom_bbs
[j
];
1088 if (flow_bb_inside_loop_p (loop
, dominated
))
1090 dom_bb
= nearest_common_dominator (
1091 CDI_DOMINATORS
, first_active
[i
], first_active_latch
);
1092 set_immediate_dominator (CDI_DOMINATORS
, dominated
, dom_bb
);
1096 free (first_active
);
1103 /* A callback for make_forwarder block, to redirect all edges except for
1104 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1105 whether to redirect it. */
1107 static edge mfb_kj_edge
;
1109 mfb_keep_just (edge e
)
1111 return e
!= mfb_kj_edge
;
1114 /* A callback for make_forwarder block, to update data structures for a basic
1115 block JUMP created by redirecting an edge (only the latch edge is being
1119 mfb_update_loops (basic_block jump
)
1121 struct loop
*loop
= EDGE_SUCC (jump
, 0)->dest
->loop_father
;
1123 if (dom_computed
[CDI_DOMINATORS
])
1124 set_immediate_dominator (CDI_DOMINATORS
, jump
, EDGE_PRED (jump
, 0)->src
);
1125 add_bb_to_loop (jump
, loop
);
1129 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1130 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1131 entry; otherwise we also force preheader block to have only one successor.
1132 The function also updates dominators. */
1135 create_preheader (struct loop
*loop
, int flags
)
1139 struct loop
*cloop
, *ploop
;
1144 cloop
= loop
->outer
;
1146 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1148 if (e
->src
== loop
->latch
)
1150 irred
|= (e
->flags
& EDGE_IRREDUCIBLE_LOOP
) != 0;
1153 gcc_assert (nentry
);
1156 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1157 if (e
->src
!= loop
->latch
)
1160 if (!(flags
& CP_SIMPLE_PREHEADERS
) || EDGE_COUNT (e
->src
->succs
) == 1)
1164 mfb_kj_edge
= loop_latch_edge (loop
);
1165 fallthru
= make_forwarder_block (loop
->header
, mfb_keep_just
,
1167 dummy
= fallthru
->src
;
1168 loop
->header
= fallthru
->dest
;
1170 /* The header could be a latch of some superloop(s); due to design of
1171 split_block, it would now move to fallthru->dest. */
1172 for (ploop
= loop
; ploop
; ploop
= ploop
->outer
)
1173 if (ploop
->latch
== dummy
)
1174 ploop
->latch
= fallthru
->dest
;
1176 /* Reorganize blocks so that the preheader is not stuck in the middle of the
1178 FOR_EACH_EDGE (e
, ei
, dummy
->preds
)
1179 if (e
->src
!= loop
->latch
)
1181 move_block_after (dummy
, e
->src
);
1183 loop
->header
->loop_father
= loop
;
1184 add_bb_to_loop (dummy
, cloop
);
1188 dummy
->flags
|= BB_IRREDUCIBLE_LOOP
;
1189 EDGE_SUCC (dummy
, 0)->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1193 fprintf (dump_file
, "Created preheader block for loop %i\n",
1199 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1200 of FLAGS see create_preheader. */
1202 create_preheaders (struct loops
*loops
, int flags
)
1205 for (i
= 1; i
< loops
->num
; i
++)
1206 create_preheader (loops
->parray
[i
], flags
);
1207 loops
->state
|= LOOPS_HAVE_PREHEADERS
;
1210 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1213 force_single_succ_latches (struct loops
*loops
)
1219 for (i
= 1; i
< loops
->num
; i
++)
1222 loop
= loops
->parray
[i
];
1223 if (loop
->latch
!= loop
->header
&& EDGE_COUNT (loop
->latch
->succs
) == 1)
1226 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1227 if (e
->src
== loop
->latch
)
1230 loop_split_edge_with (e
, NULL_RTX
);
1232 loops
->state
|= LOOPS_HAVE_SIMPLE_LATCHES
;
1235 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1236 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1237 be ok after this function. The created block is placed on correct place
1238 in LOOPS structure and its dominator is set. */
1240 loop_split_edge_with (edge e
, rtx insns
)
1242 basic_block src
, dest
, new_bb
;
1243 struct loop
*loop_c
;
1248 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
1250 /* Create basic block for it. */
1252 new_bb
= split_edge (e
);
1253 add_bb_to_loop (new_bb
, loop_c
);
1254 new_bb
->flags
|= (insns
? BB_SUPERBLOCK
: 0);
1257 emit_insn_after (insns
, BB_END (new_bb
));
1259 if (dest
->loop_father
->latch
== src
)
1260 dest
->loop_father
->latch
= new_bb
;
1265 /* Uses the natural loop discovery to recreate loop notes. */
1267 create_loop_notes (void)
1269 rtx insn
, head
, end
;
1272 basic_block
*first
, *last
, bb
, pbb
;
1273 struct loop
**stack
, **top
;
1275 #ifdef ENABLE_CHECKING
1276 /* Verify that there really are no loop notes. */
1277 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1278 gcc_assert (!NOTE_P (insn
) ||
1279 NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
1282 flow_loops_find (&loops
, LOOP_TREE
);
1283 free_dominance_info (CDI_DOMINATORS
);
1286 last
= xcalloc (loops
.num
, sizeof (basic_block
));
1290 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1291 last
[loop
->num
] = bb
;
1294 first
= xcalloc (loops
.num
, sizeof (basic_block
));
1295 stack
= xcalloc (loops
.num
, sizeof (struct loop
*));
1300 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1302 if (!first
[loop
->num
])
1305 first
[loop
->num
] = bb
;
1308 if (bb
== last
[loop
->num
])
1310 /* Prevent loops from overlapping. */
1311 while (*--top
!= loop
)
1312 last
[(*top
)->num
] = EXIT_BLOCK_PTR
;
1314 /* If loop starts with jump into it, place the note in
1315 front of the jump. */
1316 insn
= PREV_INSN (BB_HEAD (first
[loop
->num
]));
1318 && BARRIER_P (insn
))
1319 insn
= PREV_INSN (insn
);
1323 && any_uncondjump_p (insn
)
1324 && onlyjump_p (insn
))
1326 pbb
= BLOCK_FOR_INSN (insn
);
1327 gcc_assert (pbb
&& EDGE_COUNT (pbb
->succs
) == 1);
1329 if (!flow_bb_inside_loop_p (loop
, EDGE_SUCC (pbb
, 0)->dest
))
1330 insn
= BB_HEAD (first
[loop
->num
]);
1333 insn
= BB_HEAD (first
[loop
->num
]);
1335 head
= BB_HEAD (first
[loop
->num
]);
1336 emit_note_before (NOTE_INSN_LOOP_BEG
, insn
);
1337 BB_HEAD (first
[loop
->num
]) = head
;
1339 /* Position the note correctly wrto barrier. */
1340 insn
= BB_END (last
[loop
->num
]);
1341 if (NEXT_INSN (insn
)
1342 && BARRIER_P (NEXT_INSN (insn
)))
1343 insn
= NEXT_INSN (insn
);
1345 end
= BB_END (last
[loop
->num
]);
1346 emit_note_after (NOTE_INSN_LOOP_END
, insn
);
1347 BB_END (last
[loop
->num
]) = end
;
1356 flow_loops_free (&loops
);