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 struct loop
* duplicate_loop (struct loops
*, struct loop
*,
34 static void duplicate_subloops (struct loops
*, struct loop
*, struct loop
*);
35 static void copy_loops_to (struct loops
*, struct loop
**, int,
37 static void loop_redirect_edge (edge
, basic_block
);
38 static bool loop_delete_branch_edge (edge
, int);
39 static void remove_bbs (basic_block
*, int);
40 static bool rpe_enum_p (basic_block
, void *);
41 static int find_path (edge
, basic_block
**);
42 static bool alp_enum_p (basic_block
, void *);
43 static void add_loop (struct loops
*, struct loop
*);
44 static void fix_loop_placements (struct loops
*, struct loop
*);
45 static bool fix_bb_placement (struct loops
*, basic_block
);
46 static void fix_bb_placements (struct loops
*, basic_block
);
47 static void place_new_loop (struct loops
*, struct loop
*);
48 static void scale_loop_frequencies (struct loop
*, int, int);
49 static void scale_bbs_frequencies (basic_block
*, int, int, int);
50 static basic_block
create_preheader (struct loop
*, int);
51 static void fix_irreducible_loops (basic_block
);
53 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
55 /* Splits basic block BB after INSN, returns created edge. Updates loops
58 split_loop_bb (basic_block bb
, rtx insn
)
62 /* Split the block. */
63 e
= split_block (bb
, insn
);
65 /* Add dest to loop. */
66 add_bb_to_loop (e
->dest
, e
->src
->loop_father
);
71 /* Checks whether basic block BB is dominated by DATA. */
73 rpe_enum_p (basic_block bb
, void *data
)
75 return dominated_by_p (CDI_DOMINATORS
, bb
, data
);
78 /* Remove basic blocks BBS from loop structure and dominance info,
79 and delete them afterwards. */
81 remove_bbs (basic_block
*bbs
, int nbbs
)
85 for (i
= 0; i
< nbbs
; i
++)
87 remove_bb_from_loops (bbs
[i
]);
88 delete_basic_block (bbs
[i
]);
92 /* Find path -- i.e. the basic blocks dominated by edge E and put them
93 into array BBS, that will be allocated large enough to contain them.
94 E->dest must have exactly one predecessor for this to work (it is
95 easy to achieve and we do not put it here because we do not want to
96 alter anything by this function). The number of basic blocks in the
99 find_path (edge e
, basic_block
**bbs
)
101 gcc_assert (!e
->dest
->pred
->pred_next
);
103 /* Find bbs in the path. */
104 *bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
105 return dfs_enumerate_from (e
->dest
, 0, rpe_enum_p
, *bbs
,
106 n_basic_blocks
, e
->dest
);
109 /* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
110 Let L be a loop to that BB belongs. Then every successor of BB must either
111 1) belong to some superloop of loop L, or
112 2) be a header of loop K such that K->outer is superloop of L
113 Returns true if we had to move BB into other loop to enforce this condition,
114 false if the placement of BB was already correct (provided that placements
115 of its successors are correct). */
117 fix_bb_placement (struct loops
*loops
, basic_block bb
)
120 struct loop
*loop
= loops
->tree_root
, *act
;
122 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
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;
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 (e
= from
->pred
; e
; e
= e
->pred_next
)
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
));
267 bb
= stack
[--stack_top
];
268 RESET_BIT (on_stack
, bb
->index
);
270 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
271 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
276 bb
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
277 if (bb
->loop_father
->header
== bb
)
278 edges
= get_loop_exit_edges (bb
->loop_father
, &n_edges
);
282 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
284 edges
= xmalloc (n_edges
* sizeof (edge
));
286 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
287 edges
[n_edges
++] = e
;
290 for (i
= 0; i
< n_edges
; i
++)
294 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
296 if (!flow_bb_inside_loop_p (from
->loop_father
, e
->dest
))
299 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
300 if (TEST_BIT (on_stack
, e
->dest
->index
))
303 SET_BIT (on_stack
, e
->dest
->index
);
304 stack
[stack_top
++] = e
->dest
;
314 /* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
315 and update loop structure stored in LOOPS and dominators. Return true if
316 we were able to remove the path, false otherwise (and nothing is affected
319 remove_path (struct loops
*loops
, edge e
)
322 basic_block
*rem_bbs
, *bord_bbs
, *dom_bbs
, from
, bb
;
323 int i
, nrem
, n_bord_bbs
, n_dom_bbs
;
327 if (!loop_delete_branch_edge (e
, 0))
330 /* We need to check whether basic blocks are dominated by the edge
331 e, but we only have basic block dominators. This is easy to
332 fix -- when e->dest has exactly one predecessor, this corresponds
333 to blocks dominated by e->dest, if not, split the edge. */
334 if (e
->dest
->pred
->pred_next
)
335 e
= loop_split_edge_with (e
, NULL_RTX
)->pred
;
337 /* It may happen that by removing path we remove one or more loops
338 we belong to. In this case first unloop the loops, then proceed
339 normally. We may assume that e->dest is not a header of any loop,
340 as it now has exactly one predecessor. */
341 while (e
->src
->loop_father
->outer
342 && dominated_by_p (CDI_DOMINATORS
,
343 e
->src
->loop_father
->latch
, e
->dest
))
344 unloop (loops
, e
->src
->loop_father
);
346 /* Identify the path. */
347 nrem
= find_path (e
, &rem_bbs
);
350 bord_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
351 seen
= sbitmap_alloc (last_basic_block
);
354 /* Find "border" hexes -- i.e. those with predecessor in removed path. */
355 for (i
= 0; i
< nrem
; i
++)
356 SET_BIT (seen
, rem_bbs
[i
]->index
);
357 for (i
= 0; i
< nrem
; i
++)
360 for (ae
= rem_bbs
[i
]->succ
; ae
; ae
= ae
->succ_next
)
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 of basic blocks in array BBS of length NBBS
455 scale_bbs_frequencies (basic_block
*bbs
, int nbbs
, int num
, int den
)
460 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 (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
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
)
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
;
504 loop
->header
= header_edge
->dest
;
505 loop
->latch
= latch_edge
->src
;
507 freq
= EDGE_FREQUENCY (header_edge
);
508 cnt
= header_edge
->count
;
509 prob
= switch_bb
->succ
->probability
;
510 tot_prob
= prob
+ switch_bb
->succ
->succ_next
->probability
;
514 /* Redirect edges. */
515 loop_redirect_edge (latch_edge
, loop
->header
);
516 loop_redirect_edge (BRANCH_EDGE (switch_bb
), succ_bb
);
518 loop_redirect_edge (header_edge
, switch_bb
);
519 loop_redirect_edge (FALLTHRU_EDGE (switch_bb
), loop
->header
);
521 /* Update dominators. */
522 set_immediate_dominator (CDI_DOMINATORS
, switch_bb
, pred_bb
);
523 set_immediate_dominator (CDI_DOMINATORS
, loop
->header
, switch_bb
);
525 set_immediate_dominator (CDI_DOMINATORS
, succ_bb
, switch_bb
);
527 /* Compute new loop. */
528 add_loop (loops
, loop
);
529 flow_loop_tree_node_add (outer
, loop
);
531 /* Add switch_bb to appropriate loop. */
532 add_bb_to_loop (switch_bb
, outer
);
534 /* Fix frequencies. */
535 switch_bb
->frequency
= freq
;
536 switch_bb
->count
= cnt
;
537 for (e
= switch_bb
->succ
; e
; e
= e
->succ_next
)
538 e
->count
= (switch_bb
->count
* e
->probability
) / REG_BR_PROB_BASE
;
539 scale_loop_frequencies (loop
, prob
, tot_prob
);
540 scale_loop_frequencies (succ_bb
->loop_father
, tot_prob
- prob
, tot_prob
);
542 /* Update dominators of blocks outside of LOOP. */
543 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
545 seen
= sbitmap_alloc (last_basic_block
);
547 body
= get_loop_body (loop
);
549 for (i
= 0; i
< loop
->num_nodes
; i
++)
550 SET_BIT (seen
, body
[i
]->index
);
552 for (i
= 0; i
< loop
->num_nodes
; i
++)
556 for (ldom
= first_dom_son (CDI_DOMINATORS
, body
[i
]);
558 ldom
= next_dom_son (CDI_DOMINATORS
, ldom
))
559 if (!TEST_BIT (seen
, ldom
->index
))
561 SET_BIT (seen
, ldom
->index
);
562 dom_bbs
[n_dom_bbs
++] = ldom
;
566 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
575 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
576 the LOOP was removed. After this function, original loop latch will
577 have no successor, which caller is expected to fix somehow. */
579 unloop (struct loops
*loops
, struct loop
*loop
)
584 basic_block latch
= loop
->latch
;
588 /* This is relatively straightforward. The dominators are unchanged, as
589 loop header dominates loop latch, so the only thing we have to care of
590 is the placement of loops and basic blocks inside the loop tree. We
591 move them all to the loop->outer, and then let fix_bb_placements do
594 body
= get_loop_body (loop
);
595 edges
= get_loop_exit_edges (loop
, &n_edges
);
597 for (i
= 0; i
< n
; i
++)
598 if (body
[i
]->loop_father
== loop
)
600 remove_bb_from_loops (body
[i
]);
601 add_bb_to_loop (body
[i
], loop
->outer
);
608 flow_loop_tree_node_remove (ploop
);
609 flow_loop_tree_node_add (loop
->outer
, ploop
);
612 /* Remove the loop and free its data. */
613 flow_loop_tree_node_remove (loop
);
614 loops
->parray
[loop
->num
] = NULL
;
615 flow_loop_free (loop
);
617 remove_edge (latch
->succ
);
618 fix_bb_placements (loops
, latch
);
620 /* If the loop was inside an irreducible region, we would have to somehow
621 update the irreducible marks inside its body. While it is certainly
622 possible to do, it is a bit complicated and this situation should be
623 very rare, so we just remark all loops in this case. */
624 for (i
= 0; i
< n_edges
; i
++)
625 if (edges
[i
]->flags
& EDGE_IRREDUCIBLE_LOOP
)
628 mark_irreducible_loops (loops
);
632 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
633 FATHER of LOOP such that all of the edges coming out of LOOP belong to
634 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
637 fix_loop_placement (struct loop
*loop
)
642 struct loop
*father
= loop
->pred
[0], *act
;
644 body
= get_loop_body (loop
);
645 for (i
= 0; i
< loop
->num_nodes
; i
++)
646 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
647 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
649 act
= find_common_loop (loop
, e
->dest
->loop_father
);
650 if (flow_loop_nested_p (father
, act
))
655 if (father
!= loop
->outer
)
657 for (act
= loop
->outer
; act
!= father
; act
= act
->outer
)
658 act
->num_nodes
-= loop
->num_nodes
;
659 flow_loop_tree_node_remove (loop
);
660 flow_loop_tree_node_add (father
, loop
);
666 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
667 condition stated in description of fix_loop_placement holds for them.
668 It is used in case when we removed some edges coming out of LOOP, which
669 may cause the right placement of LOOP inside loop tree to change. */
671 fix_loop_placements (struct loops
*loops
, struct loop
*loop
)
678 if (!fix_loop_placement (loop
))
681 /* Changing the placement of a loop in the loop tree may alter the
682 validity of condition 2) of the description of fix_bb_placement
683 for its preheader, because the successor is the header and belongs
684 to the loop. So call fix_bb_placements to fix up the placement
685 of the preheader and (possibly) of its predecessors. */
686 fix_bb_placements (loops
, loop_preheader_edge (loop
)->src
);
691 /* Creates place for a new LOOP in LOOPS structure. */
693 place_new_loop (struct loops
*loops
, struct loop
*loop
)
696 xrealloc (loops
->parray
, (loops
->num
+ 1) * sizeof (struct loop
*));
697 loops
->parray
[loops
->num
] = loop
;
699 loop
->num
= loops
->num
++;
702 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
703 created loop into LOOPS structure. */
705 duplicate_loop (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
708 cloop
= xcalloc (1, sizeof (struct loop
));
709 place_new_loop (loops
, cloop
);
711 /* Initialize copied loop. */
712 cloop
->level
= loop
->level
;
714 /* Set it as copy of loop. */
717 /* Add it to target. */
718 flow_loop_tree_node_add (target
, cloop
);
723 /* Copies structure of subloops of LOOP into TARGET loop, placing
724 newly created loops into loop tree stored in LOOPS. */
726 duplicate_subloops (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
728 struct loop
*aloop
, *cloop
;
730 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
732 cloop
= duplicate_loop (loops
, aloop
, target
);
733 duplicate_subloops (loops
, aloop
, cloop
);
737 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
738 into TARGET loop, placing newly created loops into loop tree LOOPS. */
740 copy_loops_to (struct loops
*loops
, struct loop
**copied_loops
, int n
, struct loop
*target
)
745 for (i
= 0; i
< n
; i
++)
747 aloop
= duplicate_loop (loops
, copied_loops
[i
], target
);
748 duplicate_subloops (loops
, copied_loops
[i
], aloop
);
752 /* Redirects edge E to basic block DEST. */
754 loop_redirect_edge (edge e
, basic_block dest
)
759 redirect_edge_and_branch_force (e
, dest
);
762 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
763 just test whether it is possible to remove the edge. */
765 loop_delete_branch_edge (edge e
, int really_delete
)
767 basic_block src
= e
->src
;
772 gcc_assert (src
->succ
->succ_next
);
774 /* Cannot handle more than two exit edges. */
775 if (src
->succ
->succ_next
->succ_next
)
777 /* And it must be just a simple branch. */
778 if (!any_condjump_p (BB_END (src
)))
781 snd
= e
== src
->succ
? src
->succ
->succ_next
: src
->succ
;
783 if (newdest
== EXIT_BLOCK_PTR
)
786 /* Hopefully the above conditions should suffice. */
790 /* Redirecting behaves wrongly wrto this flag. */
791 irr
= snd
->flags
& EDGE_IRREDUCIBLE_LOOP
;
793 if (!redirect_edge_and_branch (e
, newdest
))
795 src
->succ
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
796 src
->succ
->flags
|= irr
;
801 /* Check whether LOOP's body can be duplicated. */
803 can_duplicate_loop_p (struct loop
*loop
)
806 basic_block
*bbs
= get_loop_body (loop
);
808 ret
= can_copy_bbs_p (bbs
, loop
->num_nodes
);
814 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
815 to LOOP. Update the single_exit information in superloops of LOOP. */
818 update_single_exits_after_duplication (basic_block
*bbs
, unsigned nbbs
,
823 for (i
= 0; i
< nbbs
; i
++)
824 bbs
[i
]->rbi
->duplicated
= 1;
826 for (; loop
->outer
; loop
= loop
->outer
)
828 if (!loop
->single_exit
)
831 if (loop
->single_exit
->src
->rbi
->duplicated
)
832 loop
->single_exit
= NULL
;
835 for (i
= 0; i
< nbbs
; i
++)
836 bbs
[i
]->rbi
->duplicated
= 0;
840 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
841 LOOPS structure and dominators. E's destination must be LOOP header for
842 this to work, i.e. it must be entry or latch edge of this loop; these are
843 unique, as the loops must have preheaders for this function to work
844 correctly (in case E is latch, the function unrolls the loop, if E is entry
845 edge, it peels the loop). Store edges created by copying ORIG edge from
846 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
847 original LOOP body, the other copies are numbered in order given by control
848 flow through them) into TO_REMOVE array. Returns false if duplication is
851 duplicate_loop_to_header_edge (struct loop
*loop
, edge e
, struct loops
*loops
,
852 unsigned int ndupl
, sbitmap wont_exit
,
853 edge orig
, edge
*to_remove
,
854 unsigned int *n_to_remove
, int flags
)
856 struct loop
*target
, *aloop
;
857 struct loop
**orig_loops
;
858 unsigned n_orig_loops
;
859 basic_block header
= loop
->header
, latch
= loop
->latch
;
860 basic_block
*new_bbs
, *bbs
, *first_active
;
861 basic_block new_bb
, bb
, first_active_latch
= NULL
;
863 edge spec_edges
[2], new_spec_edges
[2];
867 int is_latch
= (latch
== e
->src
);
868 int scale_act
= 0, *scale_step
= NULL
, scale_main
= 0;
869 int p
, freq_in
, freq_le
, freq_out_orig
;
870 int prob_pass_thru
, prob_pass_wont_exit
, prob_pass_main
;
871 int add_irreducible_flag
;
873 gcc_assert (e
->dest
== loop
->header
);
874 gcc_assert (ndupl
> 0);
878 /* Orig must be edge out of the loop. */
879 gcc_assert (flow_bb_inside_loop_p (loop
, orig
->src
));
880 gcc_assert (!flow_bb_inside_loop_p (loop
, orig
->dest
));
883 bbs
= get_loop_body (loop
);
885 /* Check whether duplication is possible. */
886 if (!can_copy_bbs_p (bbs
, loop
->num_nodes
))
891 new_bbs
= xmalloc (sizeof (basic_block
) * loop
->num_nodes
);
893 /* In case we are doing loop peeling and the loop is in the middle of
894 irreducible region, the peeled copies will be inside it too. */
895 add_irreducible_flag
= e
->flags
& EDGE_IRREDUCIBLE_LOOP
;
896 gcc_assert (!is_latch
|| !add_irreducible_flag
);
898 /* Find edge from latch. */
899 latch_edge
= loop_latch_edge (loop
);
901 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
903 /* Calculate coefficients by that we have to scale frequencies
904 of duplicated loop bodies. */
905 freq_in
= header
->frequency
;
906 freq_le
= EDGE_FREQUENCY (latch_edge
);
909 if (freq_in
< freq_le
)
911 freq_out_orig
= orig
? EDGE_FREQUENCY (orig
) : freq_in
- freq_le
;
912 if (freq_out_orig
> freq_in
- freq_le
)
913 freq_out_orig
= freq_in
- freq_le
;
914 prob_pass_thru
= RDIV (REG_BR_PROB_BASE
* freq_le
, freq_in
);
915 prob_pass_wont_exit
=
916 RDIV (REG_BR_PROB_BASE
* (freq_le
+ freq_out_orig
), freq_in
);
918 scale_step
= xmalloc (ndupl
* sizeof (int));
920 for (i
= 1; i
<= ndupl
; i
++)
921 scale_step
[i
- 1] = TEST_BIT (wont_exit
, i
)
922 ? prob_pass_wont_exit
927 prob_pass_main
= TEST_BIT (wont_exit
, 0)
928 ? prob_pass_wont_exit
931 scale_main
= REG_BR_PROB_BASE
;
932 for (i
= 0; i
< ndupl
; i
++)
935 p
= RDIV (p
* scale_step
[i
], REG_BR_PROB_BASE
);
937 scale_main
= RDIV (REG_BR_PROB_BASE
* REG_BR_PROB_BASE
, scale_main
);
938 scale_act
= RDIV (scale_main
* prob_pass_main
, REG_BR_PROB_BASE
);
942 scale_main
= REG_BR_PROB_BASE
;
943 for (i
= 0; i
< ndupl
; i
++)
944 scale_main
= RDIV (scale_main
* scale_step
[i
], REG_BR_PROB_BASE
);
945 scale_act
= REG_BR_PROB_BASE
- prob_pass_thru
;
947 for (i
= 0; i
< ndupl
; i
++)
948 gcc_assert (scale_step
[i
] >= 0 && scale_step
[i
] <= REG_BR_PROB_BASE
);
949 gcc_assert (scale_main
>= 0 && scale_main
<= REG_BR_PROB_BASE
950 && scale_act
>= 0 && scale_act
<= REG_BR_PROB_BASE
);
953 /* Loop the new bbs will belong to. */
954 target
= e
->src
->loop_father
;
956 /* Original loops. */
958 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
960 orig_loops
= xcalloc (n_orig_loops
, sizeof (struct loop
*));
961 for (aloop
= loop
->inner
, i
= 0; aloop
; aloop
= aloop
->next
, i
++)
962 orig_loops
[i
] = aloop
;
968 first_active
= xmalloc (n
* sizeof (basic_block
));
971 memcpy (first_active
, bbs
, n
* sizeof (basic_block
));
972 first_active_latch
= latch
;
975 /* Update the information about single exits. */
976 if (loops
->state
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
977 update_single_exits_after_duplication (bbs
, n
, target
);
979 /* Record exit edge in original loop body. */
980 if (orig
&& TEST_BIT (wont_exit
, 0))
981 to_remove
[(*n_to_remove
)++] = orig
;
983 spec_edges
[SE_ORIG
] = orig
;
984 spec_edges
[SE_LATCH
] = latch_edge
;
986 for (j
= 0; j
< ndupl
; j
++)
989 copy_loops_to (loops
, orig_loops
, n_orig_loops
, target
);
992 copy_bbs (bbs
, n
, new_bbs
, spec_edges
, 2, new_spec_edges
, loop
);
994 /* Note whether the blocks and edges belong to an irreducible loop. */
995 if (add_irreducible_flag
)
997 for (i
= 0; i
< n
; i
++)
998 new_bbs
[i
]->rbi
->duplicated
= 1;
999 for (i
= 0; i
< n
; i
++)
1001 new_bb
= new_bbs
[i
];
1002 if (new_bb
->loop_father
== target
)
1003 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1005 for (ae
= new_bb
->succ
; ae
; ae
= ae
->succ_next
)
1006 if (ae
->dest
->rbi
->duplicated
1007 && (ae
->src
->loop_father
== target
1008 || ae
->dest
->loop_father
== target
))
1009 ae
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1011 for (i
= 0; i
< n
; i
++)
1012 new_bbs
[i
]->rbi
->duplicated
= 0;
1015 /* Redirect the special edges. */
1018 redirect_edge_and_branch_force (latch_edge
, new_bbs
[0]);
1019 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1021 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], latch
);
1022 latch
= loop
->latch
= new_bbs
[1];
1023 e
= latch_edge
= new_spec_edges
[SE_LATCH
];
1027 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1029 redirect_edge_and_branch_force (e
, new_bbs
[0]);
1030 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], e
->src
);
1031 e
= new_spec_edges
[SE_LATCH
];
1034 /* Record exit edge in this copy. */
1035 if (orig
&& TEST_BIT (wont_exit
, j
+ 1))
1036 to_remove
[(*n_to_remove
)++] = new_spec_edges
[SE_ORIG
];
1038 /* Record the first copy in the control flow order if it is not
1039 the original loop (i.e. in case of peeling). */
1040 if (!first_active_latch
)
1042 memcpy (first_active
, new_bbs
, n
* sizeof (basic_block
));
1043 first_active_latch
= new_bbs
[1];
1046 /* Set counts and frequencies. */
1047 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1049 scale_bbs_frequencies (new_bbs
, n
, scale_act
, REG_BR_PROB_BASE
);
1050 scale_act
= RDIV (scale_act
* scale_step
[j
], REG_BR_PROB_BASE
);
1056 /* Update the original loop. */
1058 set_immediate_dominator (CDI_DOMINATORS
, e
->dest
, e
->src
);
1059 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1061 scale_bbs_frequencies (bbs
, n
, scale_main
, REG_BR_PROB_BASE
);
1065 /* Update dominators of outer blocks if affected. */
1066 for (i
= 0; i
< n
; i
++)
1068 basic_block dominated
, dom_bb
, *dom_bbs
;
1072 n_dom_bbs
= get_dominated_by (CDI_DOMINATORS
, bb
, &dom_bbs
);
1073 for (j
= 0; j
< n_dom_bbs
; j
++)
1075 dominated
= dom_bbs
[j
];
1076 if (flow_bb_inside_loop_p (loop
, dominated
))
1078 dom_bb
= nearest_common_dominator (
1079 CDI_DOMINATORS
, first_active
[i
], first_active_latch
);
1080 set_immediate_dominator (CDI_DOMINATORS
, dominated
, dom_bb
);
1084 free (first_active
);
1091 /* A callback for make_forwarder block, to redirect all edges except for
1092 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1093 whether to redirect it. */
1095 static edge mfb_kj_edge
;
1097 mfb_keep_just (edge e
)
1099 return e
!= mfb_kj_edge
;
1102 /* A callback for make_forwarder block, to update data structures for a basic
1103 block JUMP created by redirecting an edge (only the latch edge is being
1107 mfb_update_loops (basic_block jump
)
1109 struct loop
*loop
= jump
->succ
->dest
->loop_father
;
1111 if (dom_computed
[CDI_DOMINATORS
])
1112 set_immediate_dominator (CDI_DOMINATORS
, jump
, jump
->pred
->src
);
1113 add_bb_to_loop (jump
, loop
);
1117 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1118 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1119 entry; otherwise we also force preheader block to have only one successor.
1120 The function also updates dominators. */
1123 create_preheader (struct loop
*loop
, int flags
)
1127 struct loop
*cloop
, *ploop
;
1131 cloop
= loop
->outer
;
1133 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1135 if (e
->src
== loop
->latch
)
1137 irred
|= (e
->flags
& EDGE_IRREDUCIBLE_LOOP
) != 0;
1140 gcc_assert (nentry
);
1143 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
);
1144 if (!(flags
& CP_SIMPLE_PREHEADERS
)
1145 || !e
->src
->succ
->succ_next
)
1149 mfb_kj_edge
= loop_latch_edge (loop
);
1150 fallthru
= make_forwarder_block (loop
->header
, mfb_keep_just
,
1152 dummy
= fallthru
->src
;
1153 loop
->header
= fallthru
->dest
;
1155 /* The header could be a latch of some superloop(s); due to design of
1156 split_block, it would now move to fallthru->dest. */
1157 for (ploop
= loop
; ploop
; ploop
= ploop
->outer
)
1158 if (ploop
->latch
== dummy
)
1159 ploop
->latch
= fallthru
->dest
;
1161 /* Reorganize blocks so that the preheader is not stuck in the middle of the
1163 for (e
= dummy
->pred
; e
; e
= e
->pred_next
)
1164 if (e
->src
!= loop
->latch
)
1166 move_block_after (dummy
, e
->src
);
1168 loop
->header
->loop_father
= loop
;
1169 add_bb_to_loop (dummy
, cloop
);
1173 dummy
->flags
|= BB_IRREDUCIBLE_LOOP
;
1174 dummy
->succ
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1178 fprintf (dump_file
, "Created preheader block for loop %i\n",
1184 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1185 of FLAGS see create_preheader. */
1187 create_preheaders (struct loops
*loops
, int flags
)
1190 for (i
= 1; i
< loops
->num
; i
++)
1191 create_preheader (loops
->parray
[i
], flags
);
1192 loops
->state
|= LOOPS_HAVE_PREHEADERS
;
1195 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1198 force_single_succ_latches (struct loops
*loops
)
1204 for (i
= 1; i
< loops
->num
; i
++)
1206 loop
= loops
->parray
[i
];
1207 if (loop
->latch
!= loop
->header
1208 && !loop
->latch
->succ
->succ_next
)
1211 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1214 loop_split_edge_with (e
, NULL_RTX
);
1216 loops
->state
|= LOOPS_HAVE_SIMPLE_LATCHES
;
1219 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1220 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1221 be ok after this function. The created block is placed on correct place
1222 in LOOPS structure and its dominator is set. */
1224 loop_split_edge_with (edge e
, rtx insns
)
1226 basic_block src
, dest
, new_bb
;
1227 struct loop
*loop_c
;
1233 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
1235 /* Create basic block for it. */
1237 new_bb
= split_edge (e
);
1238 add_bb_to_loop (new_bb
, loop_c
);
1239 new_bb
->flags
= insns
? BB_SUPERBLOCK
: 0;
1241 new_e
= new_bb
->succ
;
1242 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1244 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1245 new_e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1249 emit_insn_after (insns
, BB_END (new_bb
));
1251 if (dest
->loop_father
->latch
== src
)
1252 dest
->loop_father
->latch
= new_bb
;
1257 /* Uses the natural loop discovery to recreate loop notes. */
1259 create_loop_notes (void)
1261 rtx insn
, head
, end
;
1264 basic_block
*first
, *last
, bb
, pbb
;
1265 struct loop
**stack
, **top
;
1267 #ifdef ENABLE_CHECKING
1268 /* Verify that there really are no loop notes. */
1269 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1270 gcc_assert (!NOTE_P (insn
) ||
1271 NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
1274 flow_loops_find (&loops
, LOOP_TREE
);
1275 free_dominance_info (CDI_DOMINATORS
);
1278 last
= xcalloc (loops
.num
, sizeof (basic_block
));
1282 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1283 last
[loop
->num
] = bb
;
1286 first
= xcalloc (loops
.num
, sizeof (basic_block
));
1287 stack
= xcalloc (loops
.num
, sizeof (struct loop
*));
1292 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1294 if (!first
[loop
->num
])
1297 first
[loop
->num
] = bb
;
1300 if (bb
== last
[loop
->num
])
1302 /* Prevent loops from overlapping. */
1303 while (*--top
!= loop
)
1304 last
[(*top
)->num
] = EXIT_BLOCK_PTR
;
1306 /* If loop starts with jump into it, place the note in
1307 front of the jump. */
1308 insn
= PREV_INSN (BB_HEAD (first
[loop
->num
]));
1310 && BARRIER_P (insn
))
1311 insn
= PREV_INSN (insn
);
1315 && any_uncondjump_p (insn
)
1316 && onlyjump_p (insn
))
1318 pbb
= BLOCK_FOR_INSN (insn
);
1319 gcc_assert (pbb
&& pbb
->succ
&& !pbb
->succ
->succ_next
);
1321 if (!flow_bb_inside_loop_p (loop
, pbb
->succ
->dest
))
1322 insn
= BB_HEAD (first
[loop
->num
]);
1325 insn
= BB_HEAD (first
[loop
->num
]);
1327 head
= BB_HEAD (first
[loop
->num
]);
1328 emit_note_before (NOTE_INSN_LOOP_BEG
, insn
);
1329 BB_HEAD (first
[loop
->num
]) = head
;
1331 /* Position the note correctly wrto barrier. */
1332 insn
= BB_END (last
[loop
->num
]);
1333 if (NEXT_INSN (insn
)
1334 && BARRIER_P (NEXT_INSN (insn
)))
1335 insn
= NEXT_INSN (insn
);
1337 end
= BB_END (last
[loop
->num
]);
1338 emit_note_after (NOTE_INSN_LOOP_END
, insn
);
1339 BB_END (last
[loop
->num
]) = end
;
1348 flow_loops_free (&loops
);