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 (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 (dominance_info
, basic_block
*, int);
40 static bool rpe_enum_p (basic_block
, void *);
41 static int find_path (edge
, dominance_info
, 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 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
*, dominance_info
, int);
51 static void fix_irreducible_loops (basic_block
);
53 /* Splits basic block BB after INSN, returns created edge. Updates loops
56 split_loop_bb (struct loops
*loops
, 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
);
69 add_to_dominance_info (loops
->cfg
.dom
, e
->dest
);
70 n_dom_bbs
= get_dominated_by (loops
->cfg
.dom
, e
->src
, &dom_bbs
);
71 for (i
= 0; i
< n_dom_bbs
; i
++)
72 set_immediate_dominator (loops
->cfg
.dom
, dom_bbs
[i
], e
->dest
);
74 set_immediate_dominator (loops
->cfg
.dom
, e
->dest
, e
->src
);
79 /* Checks whether basic block BB is dominated by RPE->DOM, where
80 RPE is passed through DATA. */
88 rpe_enum_p (basic_block bb
, void *data
)
90 struct rpe_data
*rpe
= data
;
91 return dominated_by_p (rpe
->doms
, bb
, rpe
->dom
);
94 /* Remove basic blocks BBS from loop structure and dominance info,
95 and delete them afterwards. */
97 remove_bbs (dominance_info dom
, basic_block
*bbs
, int nbbs
)
101 for (i
= 0; i
< nbbs
; i
++)
103 remove_bb_from_loops (bbs
[i
]);
104 delete_from_dominance_info (dom
, bbs
[i
]);
105 delete_block (bbs
[i
]);
109 /* Find path -- i.e. the basic blocks dominated by edge E and put them
110 into array BBS, that will be allocated large enough to contain them.
111 E->dest must have exactly one predecessor for this to work (it is
112 easy to achieve and we do not put it here because we do not want to
113 alter anything by this function). The number of basic blocks in the
116 find_path (edge e
, dominance_info doms
, basic_block
**bbs
)
120 if (e
->dest
->pred
->pred_next
)
123 /* Find bbs in the path. */
126 *bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
127 return dfs_enumerate_from (e
->dest
, 0, rpe_enum_p
, *bbs
,
128 n_basic_blocks
, &rpe
);
131 /* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
132 Let L be a loop to that BB belongs. Then every successor of BB must either
133 1) belong to some superloop of loop L, or
134 2) be a header of loop K such that K->outer is superloop of L
135 Returns true if we had to move BB into other loop to enforce this condition,
136 false if the placement of BB was already correct (provided that placements
137 of its successors are correct). */
139 fix_bb_placement (struct loops
*loops
, basic_block bb
)
142 struct loop
*loop
= loops
->tree_root
, *act
;
144 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
146 if (e
->dest
== EXIT_BLOCK_PTR
)
149 act
= e
->dest
->loop_father
;
150 if (act
->header
== e
->dest
)
153 if (flow_loop_nested_p (loop
, act
))
157 if (loop
== bb
->loop_father
)
160 remove_bb_from_loops (bb
);
161 add_bb_to_loop (bb
, loop
);
166 /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
167 enforce condition condition stated in description of fix_bb_placement. We
168 start from basic block FROM that had some of its successors removed, so that
169 his placement no longer has to be correct, and iteratively fix placement of
170 its predecessors that may change if placement of FROM changed. Also fix
171 placement of subloops of FROM->loop_father, that might also be altered due
172 to this change; the condition for them is similar, except that instead of
173 successors we consider edges coming out of the loops. */
175 fix_bb_placements (struct loops
*loops
, basic_block from
)
178 basic_block
*queue
, *qtop
, *qbeg
, *qend
;
179 struct loop
*base_loop
;
182 /* We pass through blocks back-reachable from FROM, testing whether some
183 of their successors moved to outer loop. It may be necessary to
184 iterate several times, but it is finite, as we stop unless we move
185 the basic block up the loop structure. The whole story is a bit
186 more complicated due to presence of subloops, those are moved using
187 fix_loop_placement. */
189 base_loop
= from
->loop_father
;
190 if (base_loop
== loops
->tree_root
)
193 in_queue
= sbitmap_alloc (last_basic_block
);
194 sbitmap_zero (in_queue
);
195 SET_BIT (in_queue
, from
->index
);
196 /* Prevent us from going out of the base_loop. */
197 SET_BIT (in_queue
, base_loop
->header
->index
);
199 queue
= xmalloc ((base_loop
->num_nodes
+ 1) * sizeof (basic_block
));
200 qtop
= queue
+ base_loop
->num_nodes
+ 1;
211 RESET_BIT (in_queue
, from
->index
);
213 if (from
->loop_father
->header
== from
)
215 /* Subloop header, maybe move the loop upward. */
216 if (!fix_loop_placement (from
->loop_father
))
221 /* Ordinary basic block. */
222 if (!fix_bb_placement (loops
, from
))
226 /* Something has changed, insert predecessors into queue. */
227 for (e
= from
->pred
; e
; e
= e
->pred_next
)
229 basic_block pred
= e
->src
;
232 if (TEST_BIT (in_queue
, pred
->index
))
235 /* If it is subloop, then it either was not moved, or
236 the path up the loop tree from base_loop do not contain
238 nca
= find_common_loop (pred
->loop_father
, base_loop
);
239 if (pred
->loop_father
!= base_loop
241 || nca
!= pred
->loop_father
))
242 pred
= pred
->loop_father
->header
;
243 else if (!flow_loop_nested_p (from
->loop_father
, pred
->loop_father
))
245 /* No point in processing it. */
249 if (TEST_BIT (in_queue
, pred
->index
))
252 /* Schedule the basic block. */
257 SET_BIT (in_queue
, pred
->index
);
264 /* Basic block from has lost one or more of its predecessors, so it might
265 mo longer be part irreducible loop. Fix it and proceed recursively
266 for its successors if needed. */
268 fix_irreducible_loops (basic_block from
)
277 if (!(from
->flags
& BB_IRREDUCIBLE_LOOP
))
280 on_stack
= sbitmap_alloc (last_basic_block
);
281 sbitmap_zero (on_stack
);
282 SET_BIT (on_stack
, from
->index
);
283 stack
= xmalloc (from
->loop_father
->num_nodes
* sizeof (basic_block
));
289 bb
= stack
[--stack_top
];
290 RESET_BIT (on_stack
, bb
->index
);
292 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
293 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
298 bb
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
299 if (bb
->loop_father
->header
== bb
)
300 edges
= get_loop_exit_edges (bb
->loop_father
, &n_edges
);
304 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
306 edges
= xmalloc (n_edges
* sizeof (edge
));
308 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
309 edges
[n_edges
++] = e
;
312 for (i
= 0; i
< n_edges
; i
++)
316 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
318 if (!flow_bb_inside_loop_p (from
->loop_father
, e
->dest
))
321 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
322 if (TEST_BIT (on_stack
, e
->dest
->index
))
325 SET_BIT (on_stack
, e
->dest
->index
);
326 stack
[stack_top
++] = e
->dest
;
336 /* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
337 and update loop structure stored in LOOPS and dominators. Return true if
338 we were able to remove the path, false otherwise (and nothing is affected
341 remove_path (struct loops
*loops
, edge e
)
344 basic_block
*rem_bbs
, *bord_bbs
, *dom_bbs
, from
, bb
;
345 int i
, nrem
, n_bord_bbs
, n_dom_bbs
;
348 if (!loop_delete_branch_edge (e
, 0))
351 /* We need to check whether basic blocks are dominated by the edge
352 e, but we only have basic block dominators. This is easy to
353 fix -- when e->dest has exactly one predecessor, this corresponds
354 to blocks dominated by e->dest, if not, split the edge. */
355 if (e
->dest
->pred
->pred_next
)
356 e
= loop_split_edge_with (e
, NULL_RTX
, loops
)->pred
;
358 /* It may happen that by removing path we remove one or more loops
359 we belong to. In this case first unloop the loops, then proceed
360 normally. We may assume that e->dest is not a header of any loop,
361 as it now has exactly one predecessor. */
362 while (e
->src
->loop_father
->outer
363 && dominated_by_p (loops
->cfg
.dom
,
364 e
->src
->loop_father
->latch
, e
->dest
))
365 unloop (loops
, e
->src
->loop_father
);
367 /* Identify the path. */
368 nrem
= find_path (e
, loops
->cfg
.dom
, &rem_bbs
);
371 bord_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
372 seen
= sbitmap_alloc (last_basic_block
);
375 /* Find "border" hexes -- i.e. those with predecessor in removed path. */
376 for (i
= 0; i
< nrem
; i
++)
377 SET_BIT (seen
, rem_bbs
[i
]->index
);
378 for (i
= 0; i
< nrem
; i
++)
381 for (ae
= rem_bbs
[i
]->succ
; ae
; ae
= ae
->succ_next
)
382 if (ae
->dest
!= EXIT_BLOCK_PTR
&& !TEST_BIT (seen
, ae
->dest
->index
))
384 SET_BIT (seen
, ae
->dest
->index
);
385 bord_bbs
[n_bord_bbs
++] = ae
->dest
;
389 /* Remove the path. */
391 if (!loop_delete_branch_edge (e
, 1))
393 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
395 /* Cancel loops contained in the path. */
396 for (i
= 0; i
< nrem
; i
++)
397 if (rem_bbs
[i
]->loop_father
->header
== rem_bbs
[i
])
398 cancel_loop_tree (loops
, rem_bbs
[i
]->loop_father
);
400 remove_bbs (loops
->cfg
.dom
, rem_bbs
, nrem
);
403 /* Find blocks whose dominators may be affected. */
406 for (i
= 0; i
< n_bord_bbs
; i
++)
411 bb
= get_immediate_dominator (loops
->cfg
.dom
, bord_bbs
[i
]);
412 if (TEST_BIT (seen
, bb
->index
))
414 SET_BIT (seen
, bb
->index
);
416 nldom
= get_dominated_by (loops
->cfg
.dom
, bb
, &ldom
);
417 for (j
= 0; j
< nldom
; j
++)
418 if (!dominated_by_p (loops
->cfg
.dom
, from
, ldom
[j
]))
419 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
425 /* Recount dominators. */
426 iterate_fix_dominators (loops
->cfg
.dom
, dom_bbs
, n_dom_bbs
);
429 /* These blocks have lost some predecessor(s), thus their irreducible
430 status could be changed. */
431 for (i
= 0; i
< n_bord_bbs
; i
++)
432 fix_irreducible_loops (bord_bbs
[i
]);
435 /* Fix placements of basic blocks inside loops and the placement of
436 loops in the loop tree. */
437 fix_bb_placements (loops
, from
);
438 fix_loop_placements (from
->loop_father
);
443 /* Predicate for enumeration in add_loop. */
445 alp_enum_p (basic_block bb
, void *alp_header
)
447 return bb
!= (basic_block
) alp_header
;
450 /* Given LOOP structure with filled header and latch, find the body of the
451 corresponding loop and add it to LOOPS tree. */
453 add_loop (struct loops
*loops
, struct loop
*loop
)
458 /* Add it to loop structure. */
459 place_new_loop (loops
, loop
);
462 /* Find its nodes. */
463 bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
464 n
= dfs_enumerate_from (loop
->latch
, 1, alp_enum_p
,
465 bbs
, n_basic_blocks
, loop
->header
);
467 for (i
= 0; i
< n
; i
++)
468 add_bb_to_loop (bbs
[i
], loop
);
469 add_bb_to_loop (loop
->header
, loop
);
474 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
477 scale_bbs_frequencies (basic_block
*bbs
, int nbbs
, int num
, int den
)
482 for (i
= 0; i
< nbbs
; i
++)
484 bbs
[i
]->frequency
= (bbs
[i
]->frequency
* num
) / den
;
485 bbs
[i
]->count
= (bbs
[i
]->count
* num
) / den
;
486 for (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
487 e
->count
= (e
->count
* num
) /den
;
491 /* Multiply all frequencies in LOOP by NUM/DEN. */
493 scale_loop_frequencies (struct loop
*loop
, int num
, int den
)
497 bbs
= get_loop_body (loop
);
498 scale_bbs_frequencies (bbs
, loop
->num_nodes
, num
, den
);
502 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
503 latch to header and update loop tree stored in LOOPS and dominators
504 accordingly. Everything between them plus LATCH_EDGE destination must
505 be dominated by HEADER_EDGE destination, and back-reachable from
506 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
507 SWITCH_BB->succ to original destination of LATCH_EDGE and
508 SWITCH_BB->succ->succ_next to original destination of HEADER_EDGE.
509 Returns newly created loop. */
511 loopify (struct loops
*loops
, edge latch_edge
, edge header_edge
, basic_block switch_bb
)
513 basic_block succ_bb
= latch_edge
->dest
;
514 basic_block pred_bb
= header_edge
->src
;
515 basic_block
*dom_bbs
, *body
;
516 unsigned n_dom_bbs
, i
, j
;
518 struct loop
*loop
= xcalloc (1, sizeof (struct loop
));
519 struct loop
*outer
= succ_bb
->loop_father
->outer
;
520 int freq
, prob
, tot_prob
;
524 loop
->header
= header_edge
->dest
;
525 loop
->latch
= latch_edge
->src
;
527 freq
= EDGE_FREQUENCY (header_edge
);
528 cnt
= header_edge
->count
;
529 prob
= switch_bb
->succ
->probability
;
530 tot_prob
= prob
+ switch_bb
->succ
->succ_next
->probability
;
534 /* Redirect edges. */
535 loop_redirect_edge (latch_edge
, loop
->header
);
536 loop_redirect_edge (header_edge
, switch_bb
);
537 loop_redirect_edge (switch_bb
->succ
->succ_next
, loop
->header
);
538 loop_redirect_edge (switch_bb
->succ
, succ_bb
);
540 /* Update dominators. */
541 set_immediate_dominator (loops
->cfg
.dom
, switch_bb
, pred_bb
);
542 set_immediate_dominator (loops
->cfg
.dom
, loop
->header
, switch_bb
);
543 set_immediate_dominator (loops
->cfg
.dom
, succ_bb
, switch_bb
);
545 /* Compute new loop. */
546 add_loop (loops
, loop
);
547 flow_loop_tree_node_add (outer
, loop
);
549 /* Add switch_bb to appropriate loop. */
550 add_bb_to_loop (switch_bb
, outer
);
552 /* Fix frequencies. */
553 switch_bb
->frequency
= freq
;
554 switch_bb
->count
= cnt
;
555 for (e
= switch_bb
->succ
; e
; e
= e
->succ_next
)
556 e
->count
= (switch_bb
->count
* e
->probability
) / REG_BR_PROB_BASE
;
557 scale_loop_frequencies (loop
, prob
, tot_prob
);
558 scale_loop_frequencies (succ_bb
->loop_father
, tot_prob
- prob
, tot_prob
);
560 /* Update dominators of blocks outside of LOOP. */
561 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
563 seen
= sbitmap_alloc (last_basic_block
);
565 body
= get_loop_body (loop
);
567 for (i
= 0; i
< loop
->num_nodes
; i
++)
568 SET_BIT (seen
, body
[i
]->index
);
570 for (i
= 0; i
< loop
->num_nodes
; i
++)
575 nldom
= get_dominated_by (loops
->cfg
.dom
, body
[i
], &ldom
);
576 for (j
= 0; j
< nldom
; j
++)
577 if (!TEST_BIT (seen
, ldom
[j
]->index
))
579 SET_BIT (seen
, ldom
[j
]->index
);
580 dom_bbs
[n_dom_bbs
++] = ldom
[j
];
585 iterate_fix_dominators (loops
->cfg
.dom
, dom_bbs
, n_dom_bbs
);
594 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
595 the LOOP was removed. After this function, original loop latch will
596 have no successor, which caller is expected to fix somehow. */
598 unloop (struct loops
*loops
, struct loop
*loop
)
603 basic_block latch
= loop
->latch
;
607 /* This is relatively straightforward. The dominators are unchanged, as
608 loop header dominates loop latch, so the only thing we have to care of
609 is the placement of loops and basic blocks inside the loop tree. We
610 move them all to the loop->outer, and then let fix_bb_placements do
613 body
= get_loop_body (loop
);
614 edges
= get_loop_exit_edges (loop
, &n_edges
);
616 for (i
= 0; i
< n
; i
++)
617 if (body
[i
]->loop_father
== loop
)
619 remove_bb_from_loops (body
[i
]);
620 add_bb_to_loop (body
[i
], loop
->outer
);
627 flow_loop_tree_node_remove (ploop
);
628 flow_loop_tree_node_add (loop
->outer
, ploop
);
631 /* Remove the loop and free its data. */
632 flow_loop_tree_node_remove (loop
);
633 loops
->parray
[loop
->num
] = NULL
;
634 flow_loop_free (loop
);
636 remove_edge (latch
->succ
);
637 fix_bb_placements (loops
, latch
);
639 /* If the loop was inside an irreducible region, we would have to somehow
640 update the irreducible marks inside its body. While it is certainly
641 possible to do, it is a bit complicated and this situation should be
642 very rare, so we just remark all loops in this case. */
643 for (i
= 0; i
< n_edges
; i
++)
644 if (edges
[i
]->flags
& EDGE_IRREDUCIBLE_LOOP
)
647 mark_irreducible_loops (loops
);
651 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
652 FATHER of LOOP such that all of the edges coming out of LOOP belong to
653 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
656 fix_loop_placement (struct loop
*loop
)
661 struct loop
*father
= loop
->pred
[0], *act
;
663 body
= get_loop_body (loop
);
664 for (i
= 0; i
< loop
->num_nodes
; i
++)
665 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
666 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
668 act
= find_common_loop (loop
, e
->dest
->loop_father
);
669 if (flow_loop_nested_p (father
, act
))
674 if (father
!= loop
->outer
)
676 for (act
= loop
->outer
; act
!= father
; act
= act
->outer
)
677 act
->num_nodes
-= loop
->num_nodes
;
678 flow_loop_tree_node_remove (loop
);
679 flow_loop_tree_node_add (father
, loop
);
685 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
686 condition stated in description of fix_loop_placement holds for them.
687 It is used in case when we removed some edges coming out of LOOP, which
688 may cause the right placement of LOOP inside loop tree to change. */
690 fix_loop_placements (struct loop
*loop
)
697 if (!fix_loop_placement (loop
))
703 /* Creates place for a new LOOP in LOOPS structure. */
705 place_new_loop (struct loops
*loops
, struct loop
*loop
)
708 xrealloc (loops
->parray
, (loops
->num
+ 1) * sizeof (struct loop
*));
709 loops
->parray
[loops
->num
] = loop
;
711 loop
->num
= loops
->num
++;
714 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
715 created loop into LOOPS structure. */
717 duplicate_loop (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
720 cloop
= xcalloc (1, sizeof (struct loop
));
721 place_new_loop (loops
, cloop
);
723 /* Initialize copied loop. */
724 cloop
->level
= loop
->level
;
726 /* Set it as copy of loop. */
729 /* Add it to target. */
730 flow_loop_tree_node_add (target
, cloop
);
735 /* Copies structure of subloops of LOOP into TARGET loop, placing
736 newly created loops into loop tree stored in LOOPS. */
738 duplicate_subloops (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
740 struct loop
*aloop
, *cloop
;
742 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
744 cloop
= duplicate_loop (loops
, aloop
, target
);
745 duplicate_subloops (loops
, aloop
, cloop
);
749 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
750 into TARGET loop, placing newly created loops into loop tree LOOPS. */
752 copy_loops_to (struct loops
*loops
, struct loop
**copied_loops
, int n
, struct loop
*target
)
757 for (i
= 0; i
< n
; i
++)
759 aloop
= duplicate_loop (loops
, copied_loops
[i
], target
);
760 duplicate_subloops (loops
, copied_loops
[i
], aloop
);
764 /* Redirects edge E to basic block DEST. */
766 loop_redirect_edge (edge e
, basic_block dest
)
771 redirect_edge_and_branch_force (e
, dest
);
774 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
775 just test whether it is possible to remove the edge. */
777 loop_delete_branch_edge (edge e
, int really_delete
)
779 basic_block src
= e
->src
;
783 if (src
->succ
->succ_next
)
787 /* Cannot handle more than two exit edges. */
788 if (src
->succ
->succ_next
->succ_next
)
790 /* And it must be just a simple branch. */
791 if (!any_condjump_p (src
->end
))
794 snd
= e
== src
->succ
? src
->succ
->succ_next
: src
->succ
;
796 if (newdest
== EXIT_BLOCK_PTR
)
799 /* Hopefully the above conditions should suffice. */
803 /* Redirecting behaves wrongly wrto this flag. */
804 irr
= snd
->flags
& EDGE_IRREDUCIBLE_LOOP
;
806 if (!redirect_edge_and_branch (e
, newdest
))
808 src
->succ
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
809 src
->succ
->flags
|= irr
;
815 /* Cannot happen -- we are using this only to remove an edge
820 return false; /* To avoid warning, cannot get here. */
823 /* Check whether LOOP's body can be duplicated. */
825 can_duplicate_loop_p (struct loop
*loop
)
828 basic_block
*bbs
= get_loop_body (loop
);
830 ret
= can_copy_bbs_p (bbs
, loop
->num_nodes
);
836 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
838 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
839 LOOPS structure and dominators. E's destination must be LOOP header for
840 this to work, i.e. it must be entry or latch edge of this loop; these are
841 unique, as the loops must have preheaders for this function to work
842 correctly (in case E is latch, the function unrolls the loop, if E is entry
843 edge, it peels the loop). Store edges created by copying ORIG edge from
844 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
845 original LOOP body, the other copies are numbered in order given by control
846 flow through them) into TO_REMOVE array. Returns false if duplication is
849 duplicate_loop_to_header_edge (struct loop
*loop
, edge e
, struct loops
*loops
,
850 unsigned int ndupl
, sbitmap wont_exit
,
851 edge orig
, edge
*to_remove
,
852 unsigned int *n_to_remove
, int flags
)
854 struct loop
*target
, *aloop
;
855 struct loop
**orig_loops
;
856 unsigned n_orig_loops
;
857 basic_block header
= loop
->header
, latch
= loop
->latch
;
858 basic_block
*new_bbs
, *bbs
, *first_active
;
859 basic_block new_bb
, bb
, first_active_latch
= NULL
;
861 edge spec_edges
[2], new_spec_edges
[2];
865 int is_latch
= (latch
== e
->src
);
866 int scale_act
= 0, *scale_step
= NULL
, scale_main
= 0;
867 int p
, freq_in
, freq_le
, freq_out_orig
;
868 int prob_pass_thru
, prob_pass_wont_exit
, prob_pass_main
;
869 int add_irreducible_flag
;
871 if (e
->dest
!= loop
->header
)
878 /* Orig must be edge out of the loop. */
879 if (!flow_bb_inside_loop_p (loop
, orig
->src
))
881 if (flow_bb_inside_loop_p (loop
, orig
->dest
))
885 bbs
= get_loop_body (loop
);
887 /* Check whether duplication is possible. */
888 if (!can_copy_bbs_p (bbs
, loop
->num_nodes
))
893 new_bbs
= xmalloc (sizeof (basic_block
) * loop
->num_nodes
);
895 /* In case we are doing loop peeling and the loop is in the middle of
896 irreducible region, the peeled copies will be inside it too. */
897 add_irreducible_flag
= e
->flags
& EDGE_IRREDUCIBLE_LOOP
;
898 if (is_latch
&& add_irreducible_flag
)
901 /* Find edge from latch. */
902 latch_edge
= loop_latch_edge (loop
);
904 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
906 /* Calculate coefficients by that we have to scale frequencies
907 of duplicated loop bodies. */
908 freq_in
= header
->frequency
;
909 freq_le
= EDGE_FREQUENCY (latch_edge
);
912 if (freq_in
< freq_le
)
914 freq_out_orig
= orig
? EDGE_FREQUENCY (orig
) : freq_in
- freq_le
;
915 if (freq_out_orig
> freq_in
- freq_le
)
916 freq_out_orig
= freq_in
- freq_le
;
917 prob_pass_thru
= RDIV (REG_BR_PROB_BASE
* freq_le
, freq_in
);
918 prob_pass_wont_exit
=
919 RDIV (REG_BR_PROB_BASE
* (freq_le
+ freq_out_orig
), freq_in
);
921 scale_step
= xmalloc (ndupl
* sizeof (int));
923 for (i
= 1; i
<= ndupl
; i
++)
924 scale_step
[i
- 1] = TEST_BIT (wont_exit
, i
)
925 ? prob_pass_wont_exit
930 prob_pass_main
= TEST_BIT (wont_exit
, 0)
931 ? prob_pass_wont_exit
934 scale_main
= REG_BR_PROB_BASE
;
935 for (i
= 0; i
< ndupl
; i
++)
938 p
= RDIV (p
* scale_step
[i
], REG_BR_PROB_BASE
);
940 scale_main
= RDIV (REG_BR_PROB_BASE
* REG_BR_PROB_BASE
, scale_main
);
941 scale_act
= RDIV (scale_main
* prob_pass_main
, REG_BR_PROB_BASE
);
945 scale_main
= REG_BR_PROB_BASE
;
946 for (i
= 0; i
< ndupl
; i
++)
947 scale_main
= RDIV (scale_main
* scale_step
[i
], REG_BR_PROB_BASE
);
948 scale_act
= REG_BR_PROB_BASE
- prob_pass_thru
;
950 for (i
= 0; i
< ndupl
; i
++)
951 if (scale_step
[i
] < 0 || scale_step
[i
] > REG_BR_PROB_BASE
)
953 if (scale_main
< 0 || scale_main
> REG_BR_PROB_BASE
954 || scale_act
< 0 || scale_act
> REG_BR_PROB_BASE
)
958 /* Loop the new bbs will belong to. */
959 target
= e
->src
->loop_father
;
961 /* Original loops. */
963 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
965 orig_loops
= xcalloc (n_orig_loops
, sizeof (struct loop
*));
966 for (aloop
= loop
->inner
, i
= 0; aloop
; aloop
= aloop
->next
, i
++)
967 orig_loops
[i
] = aloop
;
973 first_active
= xmalloc (n
* sizeof (basic_block
));
976 memcpy (first_active
, bbs
, n
* sizeof (basic_block
));
977 first_active_latch
= latch
;
980 /* Record exit edge in original loop body. */
981 if (orig
&& TEST_BIT (wont_exit
, 0))
982 to_remove
[(*n_to_remove
)++] = orig
;
984 spec_edges
[SE_ORIG
] = orig
;
985 spec_edges
[SE_LATCH
] = latch_edge
;
987 for (j
= 0; j
< ndupl
; j
++)
990 copy_loops_to (loops
, orig_loops
, n_orig_loops
, target
);
993 copy_bbs (bbs
, n
, new_bbs
, spec_edges
, 2, new_spec_edges
, loop
, loops
);
995 /* Note whether the blocks and edges belong to an irreducible loop. */
996 if (add_irreducible_flag
)
998 for (i
= 0; i
< n
; i
++)
999 new_bbs
[i
]->rbi
->duplicated
= 1;
1000 for (i
= 0; i
< n
; i
++)
1002 new_bb
= new_bbs
[i
];
1003 if (new_bb
->loop_father
== target
)
1004 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1006 for (ae
= new_bb
->succ
; ae
; ae
= ae
->succ_next
)
1007 if (ae
->dest
->rbi
->duplicated
1008 && (ae
->src
->loop_father
== target
1009 || ae
->dest
->loop_father
== target
))
1010 ae
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1012 for (i
= 0; i
< n
; i
++)
1013 new_bbs
[i
]->rbi
->duplicated
= 0;
1016 /* Redirect the special edges. */
1019 redirect_edge_and_branch_force (latch_edge
, new_bbs
[0]);
1020 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1022 set_immediate_dominator (loops
->cfg
.dom
, new_bbs
[0], latch
);
1023 latch
= loop
->latch
= new_bbs
[1];
1024 e
= latch_edge
= new_spec_edges
[SE_LATCH
];
1028 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1030 redirect_edge_and_branch_force (e
, new_bbs
[0]);
1031 set_immediate_dominator (loops
->cfg
.dom
, new_bbs
[0], e
->src
);
1032 e
= new_spec_edges
[SE_LATCH
];
1035 /* Record exit edge in this copy. */
1036 if (orig
&& TEST_BIT (wont_exit
, j
+ 1))
1037 to_remove
[(*n_to_remove
)++] = new_spec_edges
[SE_ORIG
];
1039 /* Record the first copy in the control flow order if it is not
1040 the original loop (i.e. in case of peeling). */
1041 if (!first_active_latch
)
1043 memcpy (first_active
, new_bbs
, n
* sizeof (basic_block
));
1044 first_active_latch
= new_bbs
[1];
1047 /* Set counts and frequencies. */
1048 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1050 scale_bbs_frequencies (new_bbs
, n
, scale_act
, REG_BR_PROB_BASE
);
1051 scale_act
= RDIV (scale_act
* scale_step
[j
], REG_BR_PROB_BASE
);
1057 /* Update the original loop. */
1059 set_immediate_dominator (loops
->cfg
.dom
, e
->dest
, e
->src
);
1060 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1062 scale_bbs_frequencies (bbs
, n
, scale_main
, REG_BR_PROB_BASE
);
1066 /* Update dominators of outer blocks if affected. */
1067 for (i
= 0; i
< n
; i
++)
1069 basic_block dominated
, dom_bb
, *dom_bbs
;
1073 n_dom_bbs
= get_dominated_by (loops
->cfg
.dom
, bb
, &dom_bbs
);
1074 for (j
= 0; j
< n_dom_bbs
; j
++)
1076 dominated
= dom_bbs
[j
];
1077 if (flow_bb_inside_loop_p (loop
, dominated
))
1079 dom_bb
= nearest_common_dominator (
1080 loops
->cfg
.dom
, first_active
[i
], first_active_latch
);
1081 set_immediate_dominator (loops
->cfg
.dom
, dominated
, dom_bb
);
1085 free (first_active
);
1092 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1093 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1094 entry; otherwise we also force preheader block to have only one successor.
1095 The function also updates dominators stored in DOM. */
1097 create_preheader (struct loop
*loop
, dominance_info dom
, int flags
)
1101 basic_block jump
, src
= 0;
1102 struct loop
*cloop
, *ploop
;
1106 cloop
= loop
->outer
;
1108 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1110 if (e
->src
== loop
->latch
)
1118 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
);
1119 if (!(flags
& CP_SIMPLE_PREHEADERS
)
1120 || !e
->src
->succ
->succ_next
)
1124 insn
= first_insn_after_basic_block_note (loop
->header
);
1126 insn
= PREV_INSN (insn
);
1128 insn
= get_last_insn ();
1129 if (insn
== loop
->header
->end
)
1131 /* Split_block would not split block after its end. */
1132 emit_note_after (NOTE_INSN_DELETED
, insn
);
1134 fallthru
= split_block (loop
->header
, insn
);
1135 dummy
= fallthru
->src
;
1136 loop
->header
= fallthru
->dest
;
1138 /* The header could be a latch of some superloop(s); due to design of
1139 split_block, it would now move to fallthru->dest. */
1140 for (ploop
= loop
; ploop
; ploop
= ploop
->outer
)
1141 if (ploop
->latch
== dummy
)
1142 ploop
->latch
= fallthru
->dest
;
1144 add_to_dominance_info (dom
, fallthru
->dest
);
1146 /* Redirect edges. */
1147 for (e
= dummy
->pred
; e
; e
= e
->pred_next
)
1150 if (src
== loop
->latch
)
1156 dummy
->frequency
-= EDGE_FREQUENCY (e
);
1157 dummy
->count
-= e
->count
;
1158 fallthru
->count
-= e
->count
;
1159 jump
= redirect_edge_and_branch_force (e
, loop
->header
);
1162 add_to_dominance_info (dom
, jump
);
1163 set_immediate_dominator (dom
, jump
, src
);
1164 add_bb_to_loop (jump
, loop
);
1168 /* Update structures. */
1169 redirect_immediate_dominators (dom
, dummy
, loop
->header
);
1170 set_immediate_dominator (dom
, loop
->header
, dummy
);
1171 loop
->header
->loop_father
= loop
;
1172 add_bb_to_loop (dummy
, cloop
);
1174 fprintf (rtl_dump_file
, "Created preheader block for loop %i\n",
1180 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1181 of FLAGS see create_preheader. */
1183 create_preheaders (struct loops
*loops
, int flags
)
1186 for (i
= 1; i
< loops
->num
; i
++)
1187 create_preheader (loops
->parray
[i
], loops
->cfg
.dom
, flags
);
1188 loops
->state
|= LOOPS_HAVE_PREHEADERS
;
1191 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1194 force_single_succ_latches (struct loops
*loops
)
1200 for (i
= 1; i
< loops
->num
; i
++)
1202 loop
= loops
->parray
[i
];
1203 if (loop
->latch
!= loop
->header
1204 && !loop
->latch
->succ
->succ_next
)
1207 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1210 loop_split_edge_with (e
, NULL_RTX
, loops
);
1212 loops
->state
|= LOOPS_HAVE_SIMPLE_LATCHES
;
1215 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1216 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1217 be ok after this function. The created block is placed on correct place
1218 in LOOPS structure and its dominator is set. */
1220 loop_split_edge_with (edge e
, rtx insns
, struct loops
*loops
)
1222 basic_block src
, dest
, new_bb
;
1223 struct loop
*loop_c
;
1229 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
1231 /* Create basic block for it. */
1233 new_bb
= split_edge (e
);
1234 add_to_dominance_info (loops
->cfg
.dom
, new_bb
);
1235 add_bb_to_loop (new_bb
, loop_c
);
1236 new_bb
->flags
= insns
? BB_SUPERBLOCK
: 0;
1238 new_e
= new_bb
->succ
;
1239 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1241 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1242 new_e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1246 emit_insn_after (insns
, new_bb
->end
);
1248 set_immediate_dominator (loops
->cfg
.dom
, new_bb
, src
);
1249 set_immediate_dominator (loops
->cfg
.dom
, dest
,
1250 recount_dominator (loops
->cfg
.dom
, dest
));
1252 if (dest
->loop_father
->latch
== src
)
1253 dest
->loop_father
->latch
= new_bb
;