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 (!e
->dest
->pred
->pred_next
);
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
)
118 struct loop
*loop
= loops
->tree_root
, *act
;
120 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
122 if (e
->dest
== EXIT_BLOCK_PTR
)
125 act
= e
->dest
->loop_father
;
126 if (act
->header
== e
->dest
)
129 if (flow_loop_nested_p (loop
, act
))
133 if (loop
== bb
->loop_father
)
136 remove_bb_from_loops (bb
);
137 add_bb_to_loop (bb
, loop
);
142 /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
143 enforce condition condition stated in description of fix_bb_placement. We
144 start from basic block FROM that had some of its successors removed, so that
145 his placement no longer has to be correct, and iteratively fix placement of
146 its predecessors that may change if placement of FROM changed. Also fix
147 placement of subloops of FROM->loop_father, that might also be altered due
148 to this change; the condition for them is similar, except that instead of
149 successors we consider edges coming out of the loops. */
151 fix_bb_placements (struct loops
*loops
, basic_block from
)
154 basic_block
*queue
, *qtop
, *qbeg
, *qend
;
155 struct loop
*base_loop
;
158 /* We pass through blocks back-reachable from FROM, testing whether some
159 of their successors moved to outer loop. It may be necessary to
160 iterate several times, but it is finite, as we stop unless we move
161 the basic block up the loop structure. The whole story is a bit
162 more complicated due to presence of subloops, those are moved using
163 fix_loop_placement. */
165 base_loop
= from
->loop_father
;
166 if (base_loop
== loops
->tree_root
)
169 in_queue
= sbitmap_alloc (last_basic_block
);
170 sbitmap_zero (in_queue
);
171 SET_BIT (in_queue
, from
->index
);
172 /* Prevent us from going out of the base_loop. */
173 SET_BIT (in_queue
, base_loop
->header
->index
);
175 queue
= xmalloc ((base_loop
->num_nodes
+ 1) * sizeof (basic_block
));
176 qtop
= queue
+ base_loop
->num_nodes
+ 1;
187 RESET_BIT (in_queue
, from
->index
);
189 if (from
->loop_father
->header
== from
)
191 /* Subloop header, maybe move the loop upward. */
192 if (!fix_loop_placement (from
->loop_father
))
197 /* Ordinary basic block. */
198 if (!fix_bb_placement (loops
, from
))
202 /* Something has changed, insert predecessors into queue. */
203 for (e
= from
->pred
; e
; e
= e
->pred_next
)
205 basic_block pred
= e
->src
;
208 if (TEST_BIT (in_queue
, pred
->index
))
211 /* If it is subloop, then it either was not moved, or
212 the path up the loop tree from base_loop do not contain
214 nca
= find_common_loop (pred
->loop_father
, base_loop
);
215 if (pred
->loop_father
!= base_loop
217 || nca
!= pred
->loop_father
))
218 pred
= pred
->loop_father
->header
;
219 else if (!flow_loop_nested_p (from
->loop_father
, pred
->loop_father
))
221 /* No point in processing it. */
225 if (TEST_BIT (in_queue
, pred
->index
))
228 /* Schedule the basic block. */
233 SET_BIT (in_queue
, pred
->index
);
240 /* Basic block from has lost one or more of its predecessors, so it might
241 mo longer be part irreducible loop. Fix it and proceed recursively
242 for its successors if needed. */
244 fix_irreducible_loops (basic_block from
)
253 if (!(from
->flags
& BB_IRREDUCIBLE_LOOP
))
256 on_stack
= sbitmap_alloc (last_basic_block
);
257 sbitmap_zero (on_stack
);
258 SET_BIT (on_stack
, from
->index
);
259 stack
= xmalloc (from
->loop_father
->num_nodes
* sizeof (basic_block
));
265 bb
= stack
[--stack_top
];
266 RESET_BIT (on_stack
, bb
->index
);
268 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
269 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
274 bb
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
275 if (bb
->loop_father
->header
== bb
)
276 edges
= get_loop_exit_edges (bb
->loop_father
, &n_edges
);
280 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
282 edges
= xmalloc (n_edges
* sizeof (edge
));
284 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
285 edges
[n_edges
++] = e
;
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 (e
->dest
->pred
->pred_next
)
333 e
= loop_split_edge_with (e
, NULL_RTX
)->pred
;
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
++)
358 for (ae
= rem_bbs
[i
]->succ
; ae
; ae
= ae
->succ_next
)
359 if (ae
->dest
!= EXIT_BLOCK_PTR
&& !TEST_BIT (seen
, ae
->dest
->index
))
361 SET_BIT (seen
, ae
->dest
->index
);
362 bord_bbs
[n_bord_bbs
++] = ae
->dest
;
366 /* Remove the path. */
368 deleted
= loop_delete_branch_edge (e
, 1);
369 gcc_assert (deleted
);
370 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
372 /* Cancel loops contained in the path. */
373 for (i
= 0; i
< nrem
; i
++)
374 if (rem_bbs
[i
]->loop_father
->header
== rem_bbs
[i
])
375 cancel_loop_tree (loops
, rem_bbs
[i
]->loop_father
);
377 remove_bbs (rem_bbs
, nrem
);
380 /* Find blocks whose dominators may be affected. */
383 for (i
= 0; i
< n_bord_bbs
; i
++)
387 bb
= get_immediate_dominator (CDI_DOMINATORS
, bord_bbs
[i
]);
388 if (TEST_BIT (seen
, bb
->index
))
390 SET_BIT (seen
, bb
->index
);
392 for (ldom
= first_dom_son (CDI_DOMINATORS
, bb
);
394 ldom
= next_dom_son (CDI_DOMINATORS
, ldom
))
395 if (!dominated_by_p (CDI_DOMINATORS
, from
, ldom
))
396 dom_bbs
[n_dom_bbs
++] = ldom
;
401 /* Recount dominators. */
402 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
405 /* These blocks have lost some predecessor(s), thus their irreducible
406 status could be changed. */
407 for (i
= 0; i
< n_bord_bbs
; i
++)
408 fix_irreducible_loops (bord_bbs
[i
]);
411 /* Fix placements of basic blocks inside loops and the placement of
412 loops in the loop tree. */
413 fix_bb_placements (loops
, from
);
414 fix_loop_placements (loops
, from
->loop_father
);
419 /* Predicate for enumeration in add_loop. */
421 alp_enum_p (basic_block bb
, void *alp_header
)
423 return bb
!= (basic_block
) alp_header
;
426 /* Given LOOP structure with filled header and latch, find the body of the
427 corresponding loop and add it to LOOPS tree. */
429 add_loop (struct loops
*loops
, struct loop
*loop
)
434 /* Add it to loop structure. */
435 place_new_loop (loops
, loop
);
438 /* Find its nodes. */
439 bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
440 n
= dfs_enumerate_from (loop
->latch
, 1, alp_enum_p
,
441 bbs
, n_basic_blocks
, loop
->header
);
443 for (i
= 0; i
< n
; i
++)
444 add_bb_to_loop (bbs
[i
], loop
);
445 add_bb_to_loop (loop
->header
, loop
);
450 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
453 scale_bbs_frequencies (basic_block
*bbs
, int nbbs
, int num
, int den
)
458 for (i
= 0; i
< nbbs
; i
++)
460 bbs
[i
]->frequency
= (bbs
[i
]->frequency
* num
) / den
;
461 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* num
, den
);
462 for (e
= bbs
[i
]->succ
; e
; e
= e
->succ_next
)
463 e
->count
= (e
->count
* num
) /den
;
467 /* Multiply all frequencies in LOOP by NUM/DEN. */
469 scale_loop_frequencies (struct loop
*loop
, int num
, int den
)
473 bbs
= get_loop_body (loop
);
474 scale_bbs_frequencies (bbs
, loop
->num_nodes
, num
, den
);
478 /* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
479 latch to header and update loop tree stored in LOOPS and dominators
480 accordingly. Everything between them plus LATCH_EDGE destination must
481 be dominated by HEADER_EDGE destination, and back-reachable from
482 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
483 FALLTHRU_EDGE (SWITCH_BB) to original destination of HEADER_EDGE and
484 BRANCH_EDGE (SWITCH_BB) to original destination of LATCH_EDGE.
485 Returns newly created loop. */
488 loopify (struct loops
*loops
, edge latch_edge
, edge header_edge
,
489 basic_block switch_bb
, bool redirect_all_edges
)
491 basic_block succ_bb
= latch_edge
->dest
;
492 basic_block pred_bb
= header_edge
->src
;
493 basic_block
*dom_bbs
, *body
;
494 unsigned n_dom_bbs
, i
;
496 struct loop
*loop
= xcalloc (1, sizeof (struct loop
));
497 struct loop
*outer
= succ_bb
->loop_father
->outer
;
498 int freq
, prob
, tot_prob
;
502 loop
->header
= header_edge
->dest
;
503 loop
->latch
= latch_edge
->src
;
505 freq
= EDGE_FREQUENCY (header_edge
);
506 cnt
= header_edge
->count
;
507 prob
= switch_bb
->succ
->probability
;
508 tot_prob
= prob
+ switch_bb
->succ
->succ_next
->probability
;
512 /* Redirect edges. */
513 loop_redirect_edge (latch_edge
, loop
->header
);
514 loop_redirect_edge (BRANCH_EDGE (switch_bb
), succ_bb
);
516 /* During loop versioning, one of the switch_bb edge is already properly
517 set. Do not redirect it again unless redirect_all_edges is true. */
518 if (redirect_all_edges
)
520 loop_redirect_edge (header_edge
, switch_bb
);
521 loop_redirect_edge (FALLTHRU_EDGE (switch_bb
), loop
->header
);
523 /* Update dominators. */
524 set_immediate_dominator (CDI_DOMINATORS
, switch_bb
, pred_bb
);
525 set_immediate_dominator (CDI_DOMINATORS
, loop
->header
, switch_bb
);
528 set_immediate_dominator (CDI_DOMINATORS
, succ_bb
, switch_bb
);
530 /* Compute new loop. */
531 add_loop (loops
, loop
);
532 flow_loop_tree_node_add (outer
, loop
);
534 /* Add switch_bb to appropriate loop. */
535 add_bb_to_loop (switch_bb
, outer
);
537 /* Fix frequencies. */
538 switch_bb
->frequency
= freq
;
539 switch_bb
->count
= cnt
;
540 for (e
= switch_bb
->succ
; e
; e
= e
->succ_next
)
541 e
->count
= (switch_bb
->count
* e
->probability
) / REG_BR_PROB_BASE
;
542 scale_loop_frequencies (loop
, prob
, tot_prob
);
543 scale_loop_frequencies (succ_bb
->loop_father
, tot_prob
- prob
, tot_prob
);
545 /* Update dominators of blocks outside of LOOP. */
546 dom_bbs
= xcalloc (n_basic_blocks
, sizeof (basic_block
));
548 seen
= sbitmap_alloc (last_basic_block
);
550 body
= get_loop_body (loop
);
552 for (i
= 0; i
< loop
->num_nodes
; i
++)
553 SET_BIT (seen
, body
[i
]->index
);
555 for (i
= 0; i
< loop
->num_nodes
; i
++)
559 for (ldom
= first_dom_son (CDI_DOMINATORS
, body
[i
]);
561 ldom
= next_dom_son (CDI_DOMINATORS
, ldom
))
562 if (!TEST_BIT (seen
, ldom
->index
))
564 SET_BIT (seen
, ldom
->index
);
565 dom_bbs
[n_dom_bbs
++] = ldom
;
569 iterate_fix_dominators (CDI_DOMINATORS
, dom_bbs
, n_dom_bbs
);
578 /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
579 the LOOP was removed. After this function, original loop latch will
580 have no successor, which caller is expected to fix somehow. */
582 unloop (struct loops
*loops
, struct loop
*loop
)
587 basic_block latch
= loop
->latch
;
591 /* This is relatively straightforward. The dominators are unchanged, as
592 loop header dominates loop latch, so the only thing we have to care of
593 is the placement of loops and basic blocks inside the loop tree. We
594 move them all to the loop->outer, and then let fix_bb_placements do
597 body
= get_loop_body (loop
);
598 edges
= get_loop_exit_edges (loop
, &n_edges
);
600 for (i
= 0; i
< n
; i
++)
601 if (body
[i
]->loop_father
== loop
)
603 remove_bb_from_loops (body
[i
]);
604 add_bb_to_loop (body
[i
], loop
->outer
);
611 flow_loop_tree_node_remove (ploop
);
612 flow_loop_tree_node_add (loop
->outer
, ploop
);
615 /* Remove the loop and free its data. */
616 flow_loop_tree_node_remove (loop
);
617 loops
->parray
[loop
->num
] = NULL
;
618 flow_loop_free (loop
);
620 remove_edge (latch
->succ
);
621 fix_bb_placements (loops
, latch
);
623 /* If the loop was inside an irreducible region, we would have to somehow
624 update the irreducible marks inside its body. While it is certainly
625 possible to do, it is a bit complicated and this situation should be
626 very rare, so we just remark all loops in this case. */
627 for (i
= 0; i
< n_edges
; i
++)
628 if (edges
[i
]->flags
& EDGE_IRREDUCIBLE_LOOP
)
631 mark_irreducible_loops (loops
);
635 /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
636 FATHER of LOOP such that all of the edges coming out of LOOP belong to
637 FATHER, and set it as outer loop of LOOP. Return 1 if placement of
640 fix_loop_placement (struct loop
*loop
)
645 struct loop
*father
= loop
->pred
[0], *act
;
647 body
= get_loop_body (loop
);
648 for (i
= 0; i
< loop
->num_nodes
; i
++)
649 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
650 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
652 act
= find_common_loop (loop
, e
->dest
->loop_father
);
653 if (flow_loop_nested_p (father
, act
))
658 if (father
!= loop
->outer
)
660 for (act
= loop
->outer
; act
!= father
; act
= act
->outer
)
661 act
->num_nodes
-= loop
->num_nodes
;
662 flow_loop_tree_node_remove (loop
);
663 flow_loop_tree_node_add (father
, loop
);
669 /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
670 condition stated in description of fix_loop_placement holds for them.
671 It is used in case when we removed some edges coming out of LOOP, which
672 may cause the right placement of LOOP inside loop tree to change. */
674 fix_loop_placements (struct loops
*loops
, struct loop
*loop
)
681 if (!fix_loop_placement (loop
))
684 /* Changing the placement of a loop in the loop tree may alter the
685 validity of condition 2) of the description of fix_bb_placement
686 for its preheader, because the successor is the header and belongs
687 to the loop. So call fix_bb_placements to fix up the placement
688 of the preheader and (possibly) of its predecessors. */
689 fix_bb_placements (loops
, loop_preheader_edge (loop
)->src
);
694 /* Creates place for a new LOOP in LOOPS structure. */
696 place_new_loop (struct loops
*loops
, struct loop
*loop
)
699 xrealloc (loops
->parray
, (loops
->num
+ 1) * sizeof (struct loop
*));
700 loops
->parray
[loops
->num
] = loop
;
702 loop
->num
= loops
->num
++;
705 /* Copies copy of LOOP as subloop of TARGET loop, placing newly
706 created loop into LOOPS structure. */
708 duplicate_loop (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
711 cloop
= xcalloc (1, sizeof (struct loop
));
712 place_new_loop (loops
, cloop
);
714 /* Initialize copied loop. */
715 cloop
->level
= loop
->level
;
717 /* Set it as copy of loop. */
720 /* Add it to target. */
721 flow_loop_tree_node_add (target
, cloop
);
726 /* Copies structure of subloops of LOOP into TARGET loop, placing
727 newly created loops into loop tree stored in LOOPS. */
729 duplicate_subloops (struct loops
*loops
, struct loop
*loop
, struct loop
*target
)
731 struct loop
*aloop
, *cloop
;
733 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
735 cloop
= duplicate_loop (loops
, aloop
, target
);
736 duplicate_subloops (loops
, aloop
, cloop
);
740 /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
741 into TARGET loop, placing newly created loops into loop tree LOOPS. */
743 copy_loops_to (struct loops
*loops
, struct loop
**copied_loops
, int n
, struct loop
*target
)
748 for (i
= 0; i
< n
; i
++)
750 aloop
= duplicate_loop (loops
, copied_loops
[i
], target
);
751 duplicate_subloops (loops
, copied_loops
[i
], aloop
);
755 /* Redirects edge E to basic block DEST. */
757 loop_redirect_edge (edge e
, basic_block dest
)
762 redirect_edge_and_branch_force (e
, dest
);
765 /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
766 just test whether it is possible to remove the edge. */
768 loop_delete_branch_edge (edge e
, int really_delete
)
770 basic_block src
= e
->src
;
775 gcc_assert (src
->succ
->succ_next
);
777 /* Cannot handle more than two exit edges. */
778 if (src
->succ
->succ_next
->succ_next
)
780 /* And it must be just a simple branch. */
781 if (!any_condjump_p (BB_END (src
)))
784 snd
= e
== src
->succ
? src
->succ
->succ_next
: src
->succ
;
786 if (newdest
== EXIT_BLOCK_PTR
)
789 /* Hopefully the above conditions should suffice. */
793 /* Redirecting behaves wrongly wrto this flag. */
794 irr
= snd
->flags
& EDGE_IRREDUCIBLE_LOOP
;
796 if (!redirect_edge_and_branch (e
, newdest
))
798 src
->succ
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
799 src
->succ
->flags
|= irr
;
804 /* Check whether LOOP's body can be duplicated. */
806 can_duplicate_loop_p (struct loop
*loop
)
809 basic_block
*bbs
= get_loop_body (loop
);
811 ret
= can_copy_bbs_p (bbs
, loop
->num_nodes
);
817 /* The NBBS blocks in BBS will get duplicated and the copies will be placed
818 to LOOP. Update the single_exit information in superloops of LOOP. */
821 update_single_exits_after_duplication (basic_block
*bbs
, unsigned nbbs
,
826 for (i
= 0; i
< nbbs
; i
++)
827 bbs
[i
]->rbi
->duplicated
= 1;
829 for (; loop
->outer
; loop
= loop
->outer
)
831 if (!loop
->single_exit
)
834 if (loop
->single_exit
->src
->rbi
->duplicated
)
835 loop
->single_exit
= NULL
;
838 for (i
= 0; i
< nbbs
; i
++)
839 bbs
[i
]->rbi
->duplicated
= 0;
842 /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
843 LOOPS structure and dominators. E's destination must be LOOP header for
844 this to work, i.e. it must be entry or latch edge of this loop; these are
845 unique, as the loops must have preheaders for this function to work
846 correctly (in case E is latch, the function unrolls the loop, if E is entry
847 edge, it peels the loop). Store edges created by copying ORIG edge from
848 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
849 original LOOP body, the other copies are numbered in order given by control
850 flow through them) into TO_REMOVE array. Returns false if duplication is
853 duplicate_loop_to_header_edge (struct loop
*loop
, edge e
, struct loops
*loops
,
854 unsigned int ndupl
, sbitmap wont_exit
,
855 edge orig
, edge
*to_remove
,
856 unsigned int *n_to_remove
, int flags
)
858 struct loop
*target
, *aloop
;
859 struct loop
**orig_loops
;
860 unsigned n_orig_loops
;
861 basic_block header
= loop
->header
, latch
= loop
->latch
;
862 basic_block
*new_bbs
, *bbs
, *first_active
;
863 basic_block new_bb
, bb
, first_active_latch
= NULL
;
865 edge spec_edges
[2], new_spec_edges
[2];
869 int is_latch
= (latch
== e
->src
);
870 int scale_act
= 0, *scale_step
= NULL
, scale_main
= 0;
871 int p
, freq_in
, freq_le
, freq_out_orig
;
872 int prob_pass_thru
, prob_pass_wont_exit
, prob_pass_main
;
873 int add_irreducible_flag
;
875 gcc_assert (e
->dest
== loop
->header
);
876 gcc_assert (ndupl
> 0);
880 /* Orig must be edge out of the loop. */
881 gcc_assert (flow_bb_inside_loop_p (loop
, orig
->src
));
882 gcc_assert (!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 gcc_assert (!is_latch
|| !add_irreducible_flag
);
900 /* Find edge from latch. */
901 latch_edge
= loop_latch_edge (loop
);
903 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
905 /* Calculate coefficients by that we have to scale frequencies
906 of duplicated loop bodies. */
907 freq_in
= header
->frequency
;
908 freq_le
= EDGE_FREQUENCY (latch_edge
);
911 if (freq_in
< freq_le
)
913 freq_out_orig
= orig
? EDGE_FREQUENCY (orig
) : freq_in
- freq_le
;
914 if (freq_out_orig
> freq_in
- freq_le
)
915 freq_out_orig
= freq_in
- freq_le
;
916 prob_pass_thru
= RDIV (REG_BR_PROB_BASE
* freq_le
, freq_in
);
917 prob_pass_wont_exit
=
918 RDIV (REG_BR_PROB_BASE
* (freq_le
+ freq_out_orig
), freq_in
);
920 scale_step
= xmalloc (ndupl
* sizeof (int));
922 for (i
= 1; i
<= ndupl
; i
++)
923 scale_step
[i
- 1] = TEST_BIT (wont_exit
, i
)
924 ? prob_pass_wont_exit
929 prob_pass_main
= TEST_BIT (wont_exit
, 0)
930 ? prob_pass_wont_exit
933 scale_main
= REG_BR_PROB_BASE
;
934 for (i
= 0; i
< ndupl
; i
++)
937 p
= RDIV (p
* scale_step
[i
], REG_BR_PROB_BASE
);
939 scale_main
= RDIV (REG_BR_PROB_BASE
* REG_BR_PROB_BASE
, scale_main
);
940 scale_act
= RDIV (scale_main
* prob_pass_main
, REG_BR_PROB_BASE
);
944 scale_main
= REG_BR_PROB_BASE
;
945 for (i
= 0; i
< ndupl
; i
++)
946 scale_main
= RDIV (scale_main
* scale_step
[i
], REG_BR_PROB_BASE
);
947 scale_act
= REG_BR_PROB_BASE
- prob_pass_thru
;
949 for (i
= 0; i
< ndupl
; i
++)
950 gcc_assert (scale_step
[i
] >= 0 && scale_step
[i
] <= REG_BR_PROB_BASE
);
951 gcc_assert (scale_main
>= 0 && scale_main
<= REG_BR_PROB_BASE
952 && scale_act
>= 0 && scale_act
<= REG_BR_PROB_BASE
);
955 /* Loop the new bbs will belong to. */
956 target
= e
->src
->loop_father
;
958 /* Original loops. */
960 for (aloop
= loop
->inner
; aloop
; aloop
= aloop
->next
)
962 orig_loops
= xcalloc (n_orig_loops
, sizeof (struct loop
*));
963 for (aloop
= loop
->inner
, i
= 0; aloop
; aloop
= aloop
->next
, i
++)
964 orig_loops
[i
] = aloop
;
970 first_active
= xmalloc (n
* sizeof (basic_block
));
973 memcpy (first_active
, bbs
, n
* sizeof (basic_block
));
974 first_active_latch
= latch
;
977 /* Update the information about single exits. */
978 if (loops
->state
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
979 update_single_exits_after_duplication (bbs
, n
, target
);
981 /* Record exit edge in original loop body. */
982 if (orig
&& TEST_BIT (wont_exit
, 0))
983 to_remove
[(*n_to_remove
)++] = orig
;
985 spec_edges
[SE_ORIG
] = orig
;
986 spec_edges
[SE_LATCH
] = latch_edge
;
988 for (j
= 0; j
< ndupl
; j
++)
991 copy_loops_to (loops
, orig_loops
, n_orig_loops
, target
);
994 copy_bbs (bbs
, n
, new_bbs
, spec_edges
, 2, new_spec_edges
, loop
);
996 for (i
= 0; i
< n
; i
++)
997 new_bbs
[i
]->rbi
->copy_number
= j
+ 1;
999 /* Note whether the blocks and edges belong to an irreducible loop. */
1000 if (add_irreducible_flag
)
1002 for (i
= 0; i
< n
; i
++)
1003 new_bbs
[i
]->rbi
->duplicated
= 1;
1004 for (i
= 0; i
< n
; i
++)
1006 new_bb
= new_bbs
[i
];
1007 if (new_bb
->loop_father
== target
)
1008 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1010 for (ae
= new_bb
->succ
; ae
; ae
= ae
->succ_next
)
1011 if (ae
->dest
->rbi
->duplicated
1012 && (ae
->src
->loop_father
== target
1013 || ae
->dest
->loop_father
== target
))
1014 ae
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1016 for (i
= 0; i
< n
; i
++)
1017 new_bbs
[i
]->rbi
->duplicated
= 0;
1020 /* Redirect the special edges. */
1023 redirect_edge_and_branch_force (latch_edge
, new_bbs
[0]);
1024 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1026 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], latch
);
1027 latch
= loop
->latch
= new_bbs
[1];
1028 e
= latch_edge
= new_spec_edges
[SE_LATCH
];
1032 redirect_edge_and_branch_force (new_spec_edges
[SE_LATCH
],
1034 redirect_edge_and_branch_force (e
, new_bbs
[0]);
1035 set_immediate_dominator (CDI_DOMINATORS
, new_bbs
[0], e
->src
);
1036 e
= new_spec_edges
[SE_LATCH
];
1039 /* Record exit edge in this copy. */
1040 if (orig
&& TEST_BIT (wont_exit
, j
+ 1))
1041 to_remove
[(*n_to_remove
)++] = new_spec_edges
[SE_ORIG
];
1043 /* Record the first copy in the control flow order if it is not
1044 the original loop (i.e. in case of peeling). */
1045 if (!first_active_latch
)
1047 memcpy (first_active
, new_bbs
, n
* sizeof (basic_block
));
1048 first_active_latch
= new_bbs
[1];
1051 /* Set counts and frequencies. */
1052 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1054 scale_bbs_frequencies (new_bbs
, n
, scale_act
, REG_BR_PROB_BASE
);
1055 scale_act
= RDIV (scale_act
* scale_step
[j
], REG_BR_PROB_BASE
);
1061 /* Update the original loop. */
1063 set_immediate_dominator (CDI_DOMINATORS
, e
->dest
, e
->src
);
1064 if (flags
& DLTHE_FLAG_UPDATE_FREQ
)
1066 scale_bbs_frequencies (bbs
, n
, scale_main
, REG_BR_PROB_BASE
);
1070 /* Update dominators of outer blocks if affected. */
1071 for (i
= 0; i
< n
; i
++)
1073 basic_block dominated
, dom_bb
, *dom_bbs
;
1077 bb
->rbi
->copy_number
= 0;
1079 n_dom_bbs
= get_dominated_by (CDI_DOMINATORS
, bb
, &dom_bbs
);
1080 for (j
= 0; j
< n_dom_bbs
; j
++)
1082 dominated
= dom_bbs
[j
];
1083 if (flow_bb_inside_loop_p (loop
, dominated
))
1085 dom_bb
= nearest_common_dominator (
1086 CDI_DOMINATORS
, first_active
[i
], first_active_latch
);
1087 set_immediate_dominator (CDI_DOMINATORS
, dominated
, dom_bb
);
1091 free (first_active
);
1098 /* A callback for make_forwarder block, to redirect all edges except for
1099 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1100 whether to redirect it. */
1102 static edge mfb_kj_edge
;
1104 mfb_keep_just (edge e
)
1106 return e
!= mfb_kj_edge
;
1109 /* A callback for make_forwarder block, to update data structures for a basic
1110 block JUMP created by redirecting an edge (only the latch edge is being
1114 mfb_update_loops (basic_block jump
)
1116 struct loop
*loop
= jump
->succ
->dest
->loop_father
;
1118 if (dom_computed
[CDI_DOMINATORS
])
1119 set_immediate_dominator (CDI_DOMINATORS
, jump
, jump
->pred
->src
);
1120 add_bb_to_loop (jump
, loop
);
1124 /* Creates a pre-header for a LOOP. Returns newly created block. Unless
1125 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1126 entry; otherwise we also force preheader block to have only one successor.
1127 The function also updates dominators. */
1130 create_preheader (struct loop
*loop
, int flags
)
1134 struct loop
*cloop
, *ploop
;
1138 cloop
= loop
->outer
;
1140 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1142 if (e
->src
== loop
->latch
)
1144 irred
|= (e
->flags
& EDGE_IRREDUCIBLE_LOOP
) != 0;
1147 gcc_assert (nentry
);
1150 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
);
1151 if (!(flags
& CP_SIMPLE_PREHEADERS
)
1152 || !e
->src
->succ
->succ_next
)
1156 mfb_kj_edge
= loop_latch_edge (loop
);
1157 fallthru
= make_forwarder_block (loop
->header
, mfb_keep_just
,
1159 dummy
= fallthru
->src
;
1160 loop
->header
= fallthru
->dest
;
1162 /* The header could be a latch of some superloop(s); due to design of
1163 split_block, it would now move to fallthru->dest. */
1164 for (ploop
= loop
; ploop
; ploop
= ploop
->outer
)
1165 if (ploop
->latch
== dummy
)
1166 ploop
->latch
= fallthru
->dest
;
1168 /* Reorganize blocks so that the preheader is not stuck in the middle of the
1170 for (e
= dummy
->pred
; e
; e
= e
->pred_next
)
1171 if (e
->src
!= loop
->latch
)
1173 move_block_after (dummy
, e
->src
);
1175 loop
->header
->loop_father
= loop
;
1176 add_bb_to_loop (dummy
, cloop
);
1180 dummy
->flags
|= BB_IRREDUCIBLE_LOOP
;
1181 dummy
->succ
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1185 fprintf (dump_file
, "Created preheader block for loop %i\n",
1191 /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1192 of FLAGS see create_preheader. */
1194 create_preheaders (struct loops
*loops
, int flags
)
1197 for (i
= 1; i
< loops
->num
; i
++)
1198 create_preheader (loops
->parray
[i
], flags
);
1199 loops
->state
|= LOOPS_HAVE_PREHEADERS
;
1202 /* Forces all loop latches of loops from loop tree LOOPS to have only single
1205 force_single_succ_latches (struct loops
*loops
)
1211 for (i
= 1; i
< loops
->num
; i
++)
1213 loop
= loops
->parray
[i
];
1214 if (loop
->latch
!= loop
->header
1215 && !loop
->latch
->succ
->succ_next
)
1218 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1221 loop_split_edge_with (e
, NULL_RTX
);
1223 loops
->state
|= LOOPS_HAVE_SIMPLE_LATCHES
;
1226 /* A quite stupid function to put INSNS on edge E. They are supposed to form
1227 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1228 be ok after this function. The created block is placed on correct place
1229 in LOOPS structure and its dominator is set. */
1231 loop_split_edge_with (edge e
, rtx insns
)
1233 basic_block src
, dest
, new_bb
;
1234 struct loop
*loop_c
;
1240 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
1242 /* Create basic block for it. */
1244 new_bb
= split_edge (e
);
1245 add_bb_to_loop (new_bb
, loop_c
);
1246 new_bb
->flags
= insns
? BB_SUPERBLOCK
: 0;
1248 new_e
= new_bb
->succ
;
1249 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1251 new_bb
->flags
|= BB_IRREDUCIBLE_LOOP
;
1252 new_e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1256 emit_insn_after (insns
, BB_END (new_bb
));
1258 if (dest
->loop_father
->latch
== src
)
1259 dest
->loop_father
->latch
= new_bb
;
1264 /* Uses the natural loop discovery to recreate loop notes. */
1266 create_loop_notes (void)
1268 rtx insn
, head
, end
;
1271 basic_block
*first
, *last
, bb
, pbb
;
1272 struct loop
**stack
, **top
;
1274 #ifdef ENABLE_CHECKING
1275 /* Verify that there really are no loop notes. */
1276 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1277 gcc_assert (!NOTE_P (insn
) ||
1278 NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
1281 flow_loops_find (&loops
, LOOP_TREE
);
1282 free_dominance_info (CDI_DOMINATORS
);
1285 last
= xcalloc (loops
.num
, sizeof (basic_block
));
1289 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1290 last
[loop
->num
] = bb
;
1293 first
= xcalloc (loops
.num
, sizeof (basic_block
));
1294 stack
= xcalloc (loops
.num
, sizeof (struct loop
*));
1299 for (loop
= bb
->loop_father
; loop
->outer
; loop
= loop
->outer
)
1301 if (!first
[loop
->num
])
1304 first
[loop
->num
] = bb
;
1307 if (bb
== last
[loop
->num
])
1309 /* Prevent loops from overlapping. */
1310 while (*--top
!= loop
)
1311 last
[(*top
)->num
] = EXIT_BLOCK_PTR
;
1313 /* If loop starts with jump into it, place the note in
1314 front of the jump. */
1315 insn
= PREV_INSN (BB_HEAD (first
[loop
->num
]));
1317 && BARRIER_P (insn
))
1318 insn
= PREV_INSN (insn
);
1322 && any_uncondjump_p (insn
)
1323 && onlyjump_p (insn
))
1325 pbb
= BLOCK_FOR_INSN (insn
);
1326 gcc_assert (pbb
&& pbb
->succ
&& !pbb
->succ
->succ_next
);
1328 if (!flow_bb_inside_loop_p (loop
, pbb
->succ
->dest
))
1329 insn
= BB_HEAD (first
[loop
->num
]);
1332 insn
= BB_HEAD (first
[loop
->num
]);
1334 head
= BB_HEAD (first
[loop
->num
]);
1335 emit_note_before (NOTE_INSN_LOOP_BEG
, insn
);
1336 BB_HEAD (first
[loop
->num
]) = head
;
1338 /* Position the note correctly wrto barrier. */
1339 insn
= BB_END (last
[loop
->num
]);
1340 if (NEXT_INSN (insn
)
1341 && BARRIER_P (NEXT_INSN (insn
)))
1342 insn
= NEXT_INSN (insn
);
1344 end
= BB_END (last
[loop
->num
]);
1345 emit_note_after (NOTE_INSN_LOOP_END
, insn
);
1346 BB_END (last
[loop
->num
]) = end
;
1355 flow_loops_free (&loops
);