]>
Commit | Line | Data |
---|---|---|
3d436d2a | 1 | /* Loop manipulation code for GNU compiler. |
283334f0 | 2 | Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. |
3d436d2a ZD |
3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
9 | version. | |
10 | ||
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 | |
14 | for more details. | |
15 | ||
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 | |
19 | 02111-1307, USA. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "rtl.h" | |
26 | #include "hard-reg-set.h" | |
27 | #include "basic-block.h" | |
28 | #include "cfgloop.h" | |
29 | #include "cfglayout.h" | |
30 | #include "output.h" | |
31 | ||
d329e058 AJ |
32 | static struct loop * duplicate_loop (struct loops *, struct loop *, |
33 | struct loop *); | |
34 | static void duplicate_subloops (struct loops *, struct loop *, struct loop *); | |
35 | static void copy_loops_to (struct loops *, struct loop **, int, | |
36 | struct loop *); | |
37 | static void loop_redirect_edge (edge, basic_block); | |
38 | static bool loop_delete_branch_edge (edge, int); | |
d47cc544 | 39 | static void remove_bbs (basic_block *, int); |
d329e058 | 40 | static bool rpe_enum_p (basic_block, void *); |
d47cc544 | 41 | static int find_path (edge, basic_block **); |
d329e058 AJ |
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); | |
d47cc544 | 50 | static basic_block create_preheader (struct loop *, int); |
d329e058 | 51 | static void fix_irreducible_loops (basic_block); |
3d436d2a | 52 | |
617b465c ZD |
53 | /* Splits basic block BB after INSN, returns created edge. Updates loops |
54 | and dominators. */ | |
55 | edge | |
d47cc544 | 56 | split_loop_bb (basic_block bb, rtx insn) |
617b465c ZD |
57 | { |
58 | edge e; | |
617b465c ZD |
59 | |
60 | /* Split the block. */ | |
61 | e = split_block (bb, insn); | |
62 | ||
63 | /* Add dest to loop. */ | |
64 | add_bb_to_loop (e->dest, e->src->loop_father); | |
65 | ||
617b465c ZD |
66 | return e; |
67 | } | |
68 | ||
d47cc544 | 69 | /* Checks whether basic block BB is dominated by DATA. */ |
617b465c | 70 | static bool |
d329e058 | 71 | rpe_enum_p (basic_block bb, void *data) |
617b465c | 72 | { |
d47cc544 | 73 | return dominated_by_p (CDI_DOMINATORS, bb, data); |
617b465c ZD |
74 | } |
75 | ||
76 | /* Remove basic blocks BBS from loop structure and dominance info, | |
77 | and delete them afterwards. */ | |
78 | static void | |
d47cc544 | 79 | remove_bbs (basic_block *bbs, int nbbs) |
617b465c ZD |
80 | { |
81 | int i; | |
82 | ||
83 | for (i = 0; i < nbbs; i++) | |
84 | { | |
85 | remove_bb_from_loops (bbs[i]); | |
f470c378 | 86 | delete_basic_block (bbs[i]); |
617b465c ZD |
87 | } |
88 | } | |
89 | ||
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. | |
35b07080 ZD |
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 | |
95 | path is returned. */ | |
617b465c | 96 | static int |
d47cc544 | 97 | find_path (edge e, basic_block **bbs) |
617b465c | 98 | { |
617b465c | 99 | if (e->dest->pred->pred_next) |
35b07080 | 100 | abort (); |
617b465c ZD |
101 | |
102 | /* Find bbs in the path. */ | |
617b465c ZD |
103 | *bbs = xcalloc (n_basic_blocks, sizeof (basic_block)); |
104 | return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs, | |
d47cc544 | 105 | n_basic_blocks, e->dest); |
617b465c ZD |
106 | } |
107 | ||
108 | /* Fix placement of basic block BB inside loop hierarchy stored in LOOPS -- | |
109 | Let L be a loop to that BB belongs. Then every successor of BB must either | |
110 | 1) belong to some superloop of loop L, or | |
111 | 2) be a header of loop K such that K->outer is superloop of L | |
112 | Returns true if we had to move BB into other loop to enforce this condition, | |
113 | false if the placement of BB was already correct (provided that placements | |
114 | of its successors are correct). */ | |
115 | static bool | |
d329e058 | 116 | fix_bb_placement (struct loops *loops, basic_block bb) |
617b465c ZD |
117 | { |
118 | edge e; | |
119 | struct loop *loop = loops->tree_root, *act; | |
120 | ||
121 | for (e = bb->succ; e; e = e->succ_next) | |
122 | { | |
123 | if (e->dest == EXIT_BLOCK_PTR) | |
124 | continue; | |
125 | ||
126 | act = e->dest->loop_father; | |
127 | if (act->header == e->dest) | |
128 | act = act->outer; | |
129 | ||
130 | if (flow_loop_nested_p (loop, act)) | |
131 | loop = act; | |
132 | } | |
133 | ||
134 | if (loop == bb->loop_father) | |
135 | return false; | |
136 | ||
137 | remove_bb_from_loops (bb); | |
138 | add_bb_to_loop (bb, loop); | |
139 | ||
140 | return true; | |
141 | } | |
142 | ||
143 | /* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e. | |
144 | enforce condition condition stated in description of fix_bb_placement. We | |
145 | start from basic block FROM that had some of its successors removed, so that | |
146 | his placement no longer has to be correct, and iteratively fix placement of | |
147 | its predecessors that may change if placement of FROM changed. Also fix | |
148 | placement of subloops of FROM->loop_father, that might also be altered due | |
4d6922ee | 149 | to this change; the condition for them is similar, except that instead of |
617b465c ZD |
150 | successors we consider edges coming out of the loops. */ |
151 | static void | |
d329e058 | 152 | fix_bb_placements (struct loops *loops, basic_block from) |
617b465c ZD |
153 | { |
154 | sbitmap in_queue; | |
155 | basic_block *queue, *qtop, *qbeg, *qend; | |
156 | struct loop *base_loop; | |
157 | edge e; | |
158 | ||
159 | /* We pass through blocks back-reachable from FROM, testing whether some | |
160 | of their successors moved to outer loop. It may be necessary to | |
161 | iterate several times, but it is finite, as we stop unless we move | |
162 | the basic block up the loop structure. The whole story is a bit | |
163 | more complicated due to presence of subloops, those are moved using | |
164 | fix_loop_placement. */ | |
165 | ||
166 | base_loop = from->loop_father; | |
167 | if (base_loop == loops->tree_root) | |
168 | return; | |
169 | ||
170 | in_queue = sbitmap_alloc (last_basic_block); | |
171 | sbitmap_zero (in_queue); | |
172 | SET_BIT (in_queue, from->index); | |
173 | /* Prevent us from going out of the base_loop. */ | |
174 | SET_BIT (in_queue, base_loop->header->index); | |
175 | ||
35b07080 | 176 | queue = xmalloc ((base_loop->num_nodes + 1) * sizeof (basic_block)); |
617b465c ZD |
177 | qtop = queue + base_loop->num_nodes + 1; |
178 | qbeg = queue; | |
179 | qend = queue + 1; | |
180 | *qbeg = from; | |
181 | ||
182 | while (qbeg != qend) | |
183 | { | |
184 | from = *qbeg; | |
185 | qbeg++; | |
186 | if (qbeg == qtop) | |
187 | qbeg = queue; | |
188 | RESET_BIT (in_queue, from->index); | |
189 | ||
190 | if (from->loop_father->header == from) | |
191 | { | |
192 | /* Subloop header, maybe move the loop upward. */ | |
193 | if (!fix_loop_placement (from->loop_father)) | |
194 | continue; | |
195 | } | |
196 | else | |
197 | { | |
198 | /* Ordinary basic block. */ | |
199 | if (!fix_bb_placement (loops, from)) | |
200 | continue; | |
201 | } | |
202 | ||
203 | /* Something has changed, insert predecessors into queue. */ | |
204 | for (e = from->pred; e; e = e->pred_next) | |
205 | { | |
206 | basic_block pred = e->src; | |
207 | struct loop *nca; | |
208 | ||
209 | if (TEST_BIT (in_queue, pred->index)) | |
210 | continue; | |
211 | ||
d329e058 | 212 | /* If it is subloop, then it either was not moved, or |
617b465c ZD |
213 | the path up the loop tree from base_loop do not contain |
214 | it. */ | |
215 | nca = find_common_loop (pred->loop_father, base_loop); | |
216 | if (pred->loop_father != base_loop | |
217 | && (nca == base_loop | |
218 | || nca != pred->loop_father)) | |
219 | pred = pred->loop_father->header; | |
220 | else if (!flow_loop_nested_p (from->loop_father, pred->loop_father)) | |
221 | { | |
222 | /* No point in processing it. */ | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (TEST_BIT (in_queue, pred->index)) | |
227 | continue; | |
228 | ||
229 | /* Schedule the basic block. */ | |
230 | *qend = pred; | |
231 | qend++; | |
232 | if (qend == qtop) | |
233 | qend = queue; | |
234 | SET_BIT (in_queue, pred->index); | |
235 | } | |
236 | } | |
237 | free (in_queue); | |
238 | free (queue); | |
239 | } | |
240 | ||
35b07080 ZD |
241 | /* Basic block from has lost one or more of its predecessors, so it might |
242 | mo longer be part irreducible loop. Fix it and proceed recursively | |
243 | for its successors if needed. */ | |
244 | static void | |
d329e058 | 245 | fix_irreducible_loops (basic_block from) |
35b07080 ZD |
246 | { |
247 | basic_block bb; | |
248 | basic_block *stack; | |
249 | int stack_top; | |
250 | sbitmap on_stack; | |
251 | edge *edges, e; | |
252 | unsigned n_edges, i; | |
253 | ||
254 | if (!(from->flags & BB_IRREDUCIBLE_LOOP)) | |
255 | return; | |
256 | ||
257 | on_stack = sbitmap_alloc (last_basic_block); | |
258 | sbitmap_zero (on_stack); | |
259 | SET_BIT (on_stack, from->index); | |
260 | stack = xmalloc (from->loop_father->num_nodes * sizeof (basic_block)); | |
261 | stack[0] = from; | |
262 | stack_top = 1; | |
263 | ||
264 | while (stack_top) | |
265 | { | |
266 | bb = stack[--stack_top]; | |
267 | RESET_BIT (on_stack, bb->index); | |
268 | ||
269 | for (e = bb->pred; e; e = e->pred_next) | |
270 | if (e->flags & EDGE_IRREDUCIBLE_LOOP) | |
271 | break; | |
272 | if (e) | |
273 | continue; | |
274 | ||
275 | bb->flags &= ~BB_IRREDUCIBLE_LOOP; | |
276 | if (bb->loop_father->header == bb) | |
277 | edges = get_loop_exit_edges (bb->loop_father, &n_edges); | |
278 | else | |
279 | { | |
280 | n_edges = 0; | |
281 | for (e = bb->succ; e; e = e->succ_next) | |
282 | n_edges++; | |
283 | edges = xmalloc (n_edges * sizeof (edge)); | |
284 | n_edges = 0; | |
285 | for (e = bb->succ; e; e = e->succ_next) | |
286 | edges[n_edges++] = e; | |
287 | } | |
d329e058 | 288 | |
35b07080 | 289 | for (i = 0; i < n_edges; i++) |
3ed961a0 ZD |
290 | { |
291 | e = edges[i]; | |
35b07080 | 292 | |
3ed961a0 ZD |
293 | if (e->flags & EDGE_IRREDUCIBLE_LOOP) |
294 | { | |
295 | if (!flow_bb_inside_loop_p (from->loop_father, e->dest)) | |
296 | continue; | |
297 | ||
298 | e->flags &= ~EDGE_IRREDUCIBLE_LOOP; | |
299 | if (TEST_BIT (on_stack, e->dest->index)) | |
300 | continue; | |
35b07080 | 301 | |
3ed961a0 ZD |
302 | SET_BIT (on_stack, e->dest->index); |
303 | stack[stack_top++] = e->dest; | |
304 | } | |
305 | } | |
35b07080 ZD |
306 | free (edges); |
307 | } | |
308 | ||
309 | free (on_stack); | |
310 | free (stack); | |
311 | } | |
312 | ||
617b465c ZD |
313 | /* Removes path beginning at edge E, i.e. remove basic blocks dominated by E |
314 | and update loop structure stored in LOOPS and dominators. Return true if | |
315 | we were able to remove the path, false otherwise (and nothing is affected | |
316 | then). */ | |
317 | bool | |
d329e058 | 318 | remove_path (struct loops *loops, edge e) |
617b465c ZD |
319 | { |
320 | edge ae; | |
321 | basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb; | |
322 | int i, nrem, n_bord_bbs, n_dom_bbs; | |
323 | sbitmap seen; | |
324 | ||
35b07080 ZD |
325 | if (!loop_delete_branch_edge (e, 0)) |
326 | return false; | |
327 | ||
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) | |
d47cc544 | 333 | e = loop_split_edge_with (e, NULL_RTX)->pred; |
35b07080 ZD |
334 | |
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 | |
d47cc544 | 340 | && dominated_by_p (CDI_DOMINATORS, |
35b07080 ZD |
341 | e->src->loop_father->latch, e->dest)) |
342 | unloop (loops, e->src->loop_father); | |
d329e058 | 343 | |
35b07080 | 344 | /* Identify the path. */ |
d47cc544 | 345 | nrem = find_path (e, &rem_bbs); |
617b465c ZD |
346 | |
347 | n_bord_bbs = 0; | |
348 | bord_bbs = xcalloc (n_basic_blocks, sizeof (basic_block)); | |
349 | seen = sbitmap_alloc (last_basic_block); | |
350 | sbitmap_zero (seen); | |
351 | ||
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); | |
35b07080 | 355 | for (i = 0; i < nrem; i++) |
617b465c | 356 | { |
35b07080 ZD |
357 | bb = rem_bbs[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)) | |
360 | { | |
361 | SET_BIT (seen, ae->dest->index); | |
362 | bord_bbs[n_bord_bbs++] = ae->dest; | |
363 | } | |
617b465c | 364 | } |
617b465c ZD |
365 | |
366 | /* Remove the path. */ | |
367 | from = e->src; | |
35b07080 ZD |
368 | if (!loop_delete_branch_edge (e, 1)) |
369 | abort (); | |
617b465c ZD |
370 | dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block)); |
371 | ||
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); | |
376 | ||
d47cc544 | 377 | remove_bbs (rem_bbs, nrem); |
617b465c ZD |
378 | free (rem_bbs); |
379 | ||
35b07080 | 380 | /* Find blocks whose dominators may be affected. */ |
617b465c ZD |
381 | n_dom_bbs = 0; |
382 | sbitmap_zero (seen); | |
383 | for (i = 0; i < n_bord_bbs; i++) | |
384 | { | |
d47cc544 | 385 | basic_block ldom; |
617b465c | 386 | |
d47cc544 | 387 | bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]); |
617b465c ZD |
388 | if (TEST_BIT (seen, bb->index)) |
389 | continue; | |
390 | SET_BIT (seen, bb->index); | |
391 | ||
d47cc544 SB |
392 | for (ldom = first_dom_son (CDI_DOMINATORS, bb); |
393 | ldom; | |
394 | ldom = next_dom_son (CDI_DOMINATORS, ldom)) | |
395 | if (!dominated_by_p (CDI_DOMINATORS, from, ldom)) | |
396 | dom_bbs[n_dom_bbs++] = ldom; | |
617b465c ZD |
397 | } |
398 | ||
617b465c ZD |
399 | free (seen); |
400 | ||
401 | /* Recount dominators. */ | |
d47cc544 | 402 | iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs); |
617b465c ZD |
403 | free (dom_bbs); |
404 | ||
35b07080 ZD |
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]); | |
409 | free (bord_bbs); | |
410 | ||
617b465c ZD |
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 (from->loop_father); | |
415 | ||
416 | return true; | |
417 | } | |
418 | ||
419 | /* Predicate for enumeration in add_loop. */ | |
420 | static bool | |
d329e058 | 421 | alp_enum_p (basic_block bb, void *alp_header) |
617b465c ZD |
422 | { |
423 | return bb != (basic_block) alp_header; | |
424 | } | |
425 | ||
426 | /* Given LOOP structure with filled header and latch, find the body of the | |
427 | corresponding loop and add it to LOOPS tree. */ | |
428 | static void | |
d329e058 | 429 | add_loop (struct loops *loops, struct loop *loop) |
617b465c ZD |
430 | { |
431 | basic_block *bbs; | |
432 | int i, n; | |
d329e058 | 433 | |
617b465c ZD |
434 | /* Add it to loop structure. */ |
435 | place_new_loop (loops, loop); | |
436 | loop->level = 1; | |
437 | ||
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); | |
442 | ||
443 | for (i = 0; i < n; i++) | |
444 | add_bb_to_loop (bbs[i], loop); | |
445 | add_bb_to_loop (loop->header, loop); | |
446 | ||
447 | free (bbs); | |
448 | } | |
449 | ||
e0bb17a8 | 450 | /* Multiply all frequencies of basic blocks in array BBS of length NBBS |
617b465c ZD |
451 | by NUM/DEN. */ |
452 | static void | |
d329e058 | 453 | scale_bbs_frequencies (basic_block *bbs, int nbbs, int num, int den) |
617b465c ZD |
454 | { |
455 | int i; | |
456 | edge e; | |
457 | ||
458 | for (i = 0; i < nbbs; i++) | |
459 | { | |
460 | bbs[i]->frequency = (bbs[i]->frequency * num) / den; | |
461 | bbs[i]->count = (bbs[i]->count * num) / den; | |
462 | for (e = bbs[i]->succ; e; e = e->succ_next) | |
463 | e->count = (e->count * num) /den; | |
464 | } | |
465 | } | |
466 | ||
467 | /* Multiply all frequencies in LOOP by NUM/DEN. */ | |
468 | static void | |
d329e058 | 469 | scale_loop_frequencies (struct loop *loop, int num, int den) |
617b465c ZD |
470 | { |
471 | basic_block *bbs; | |
472 | ||
473 | bbs = get_loop_body (loop); | |
474 | scale_bbs_frequencies (bbs, loop->num_nodes, num, den); | |
475 | free (bbs); | |
476 | } | |
477 | ||
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, | |
50654f6c ZD |
483 | FALLTHRU_EDGE (SWITCH_BB) to original destination of HEADER_EDGE and |
484 | BRANCH_EDGE (SWITCH_BB) to original destination of LATCH_EDGE. | |
617b465c | 485 | Returns newly created loop. */ |
50654f6c | 486 | |
617b465c | 487 | struct loop * |
50654f6c ZD |
488 | loopify (struct loops *loops, edge latch_edge, edge header_edge, |
489 | basic_block switch_bb) | |
617b465c ZD |
490 | { |
491 | basic_block succ_bb = latch_edge->dest; | |
492 | basic_block pred_bb = header_edge->src; | |
493 | basic_block *dom_bbs, *body; | |
d47cc544 | 494 | unsigned n_dom_bbs, i; |
617b465c ZD |
495 | sbitmap seen; |
496 | struct loop *loop = xcalloc (1, sizeof (struct loop)); | |
497 | struct loop *outer = succ_bb->loop_father->outer; | |
498 | int freq, prob, tot_prob; | |
499 | gcov_type cnt; | |
500 | edge e; | |
501 | ||
502 | loop->header = header_edge->dest; | |
503 | loop->latch = latch_edge->src; | |
504 | ||
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; | |
509 | if (tot_prob == 0) | |
510 | tot_prob = 1; | |
511 | ||
512 | /* Redirect edges. */ | |
513 | loop_redirect_edge (latch_edge, loop->header); | |
50654f6c ZD |
514 | loop_redirect_edge (BRANCH_EDGE (switch_bb), succ_bb); |
515 | ||
617b465c | 516 | loop_redirect_edge (header_edge, switch_bb); |
50654f6c | 517 | loop_redirect_edge (FALLTHRU_EDGE (switch_bb), loop->header); |
617b465c ZD |
518 | |
519 | /* Update dominators. */ | |
d47cc544 SB |
520 | set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb); |
521 | set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb); | |
50654f6c | 522 | |
d47cc544 | 523 | set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb); |
617b465c ZD |
524 | |
525 | /* Compute new loop. */ | |
526 | add_loop (loops, loop); | |
527 | flow_loop_tree_node_add (outer, loop); | |
528 | ||
529 | /* Add switch_bb to appropriate loop. */ | |
530 | add_bb_to_loop (switch_bb, outer); | |
531 | ||
532 | /* Fix frequencies. */ | |
533 | switch_bb->frequency = freq; | |
534 | switch_bb->count = cnt; | |
535 | for (e = switch_bb->succ; e; e = e->succ_next) | |
536 | e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE; | |
537 | scale_loop_frequencies (loop, prob, tot_prob); | |
538 | scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob); | |
539 | ||
540 | /* Update dominators of blocks outside of LOOP. */ | |
541 | dom_bbs = xcalloc (n_basic_blocks, sizeof (basic_block)); | |
542 | n_dom_bbs = 0; | |
543 | seen = sbitmap_alloc (last_basic_block); | |
544 | sbitmap_zero (seen); | |
545 | body = get_loop_body (loop); | |
546 | ||
547 | for (i = 0; i < loop->num_nodes; i++) | |
548 | SET_BIT (seen, body[i]->index); | |
549 | ||
550 | for (i = 0; i < loop->num_nodes; i++) | |
551 | { | |
d47cc544 | 552 | basic_block ldom; |
617b465c | 553 | |
d47cc544 SB |
554 | for (ldom = first_dom_son (CDI_DOMINATORS, body[i]); |
555 | ldom; | |
556 | ldom = next_dom_son (CDI_DOMINATORS, ldom)) | |
557 | if (!TEST_BIT (seen, ldom->index)) | |
617b465c | 558 | { |
d47cc544 SB |
559 | SET_BIT (seen, ldom->index); |
560 | dom_bbs[n_dom_bbs++] = ldom; | |
617b465c | 561 | } |
617b465c ZD |
562 | } |
563 | ||
d47cc544 | 564 | iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs); |
617b465c ZD |
565 | |
566 | free (body); | |
567 | free (seen); | |
568 | free (dom_bbs); | |
569 | ||
570 | return loop; | |
571 | } | |
572 | ||
35b07080 ZD |
573 | /* Remove the latch edge of a LOOP and update LOOPS tree to indicate that |
574 | the LOOP was removed. After this function, original loop latch will | |
575 | have no successor, which caller is expected to fix somehow. */ | |
576 | void | |
d329e058 | 577 | unloop (struct loops *loops, struct loop *loop) |
35b07080 ZD |
578 | { |
579 | basic_block *body; | |
580 | struct loop *ploop; | |
581 | unsigned i, n; | |
582 | basic_block latch = loop->latch; | |
583 | edge *edges; | |
584 | unsigned n_edges; | |
585 | ||
e0bb17a8 | 586 | /* This is relatively straightforward. The dominators are unchanged, as |
35b07080 ZD |
587 | loop header dominates loop latch, so the only thing we have to care of |
588 | is the placement of loops and basic blocks inside the loop tree. We | |
589 | move them all to the loop->outer, and then let fix_bb_placements do | |
590 | its work. */ | |
591 | ||
592 | body = get_loop_body (loop); | |
593 | edges = get_loop_exit_edges (loop, &n_edges); | |
594 | n = loop->num_nodes; | |
595 | for (i = 0; i < n; i++) | |
596 | if (body[i]->loop_father == loop) | |
597 | { | |
598 | remove_bb_from_loops (body[i]); | |
599 | add_bb_to_loop (body[i], loop->outer); | |
600 | } | |
601 | free(body); | |
602 | ||
603 | while (loop->inner) | |
604 | { | |
605 | ploop = loop->inner; | |
606 | flow_loop_tree_node_remove (ploop); | |
607 | flow_loop_tree_node_add (loop->outer, ploop); | |
608 | } | |
609 | ||
610 | /* Remove the loop and free its data. */ | |
611 | flow_loop_tree_node_remove (loop); | |
612 | loops->parray[loop->num] = NULL; | |
613 | flow_loop_free (loop); | |
614 | ||
615 | remove_edge (latch->succ); | |
616 | fix_bb_placements (loops, latch); | |
617 | ||
618 | /* If the loop was inside an irreducible region, we would have to somehow | |
619 | update the irreducible marks inside its body. While it is certainly | |
620 | possible to do, it is a bit complicated and this situation should be | |
621 | very rare, so we just remark all loops in this case. */ | |
622 | for (i = 0; i < n_edges; i++) | |
623 | if (edges[i]->flags & EDGE_IRREDUCIBLE_LOOP) | |
624 | break; | |
625 | if (i != n_edges) | |
626 | mark_irreducible_loops (loops); | |
627 | free (edges); | |
628 | } | |
629 | ||
617b465c | 630 | /* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop |
2067c116 | 631 | FATHER of LOOP such that all of the edges coming out of LOOP belong to |
617b465c ZD |
632 | FATHER, and set it as outer loop of LOOP. Return 1 if placement of |
633 | LOOP changed. */ | |
634 | int | |
d329e058 | 635 | fix_loop_placement (struct loop *loop) |
617b465c ZD |
636 | { |
637 | basic_block *body; | |
638 | unsigned i; | |
639 | edge e; | |
640 | struct loop *father = loop->pred[0], *act; | |
641 | ||
642 | body = get_loop_body (loop); | |
643 | for (i = 0; i < loop->num_nodes; i++) | |
644 | for (e = body[i]->succ; e; e = e->succ_next) | |
645 | if (!flow_bb_inside_loop_p (loop, e->dest)) | |
646 | { | |
647 | act = find_common_loop (loop, e->dest->loop_father); | |
648 | if (flow_loop_nested_p (father, act)) | |
649 | father = act; | |
650 | } | |
651 | free (body); | |
652 | ||
653 | if (father != loop->outer) | |
654 | { | |
655 | for (act = loop->outer; act != father; act = act->outer) | |
656 | act->num_nodes -= loop->num_nodes; | |
657 | flow_loop_tree_node_remove (loop); | |
658 | flow_loop_tree_node_add (father, loop); | |
659 | return 1; | |
660 | } | |
661 | return 0; | |
662 | } | |
663 | ||
664 | /* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that | |
665 | condition stated in description of fix_loop_placement holds for them. | |
666 | It is used in case when we removed some edges coming out of LOOP, which | |
667 | may cause the right placement of LOOP inside loop tree to change. */ | |
668 | static void | |
d329e058 | 669 | fix_loop_placements (struct loop *loop) |
617b465c ZD |
670 | { |
671 | struct loop *outer; | |
672 | ||
673 | while (loop->outer) | |
674 | { | |
675 | outer = loop->outer; | |
676 | if (!fix_loop_placement (loop)) | |
677 | break; | |
678 | loop = outer; | |
679 | } | |
680 | } | |
681 | ||
682 | /* Creates place for a new LOOP in LOOPS structure. */ | |
683 | static void | |
d329e058 | 684 | place_new_loop (struct loops *loops, struct loop *loop) |
617b465c ZD |
685 | { |
686 | loops->parray = | |
687 | xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *)); | |
688 | loops->parray[loops->num] = loop; | |
689 | ||
690 | loop->num = loops->num++; | |
691 | } | |
692 | ||
693 | /* Copies copy of LOOP as subloop of TARGET loop, placing newly | |
694 | created loop into LOOPS structure. */ | |
695 | static struct loop * | |
d329e058 | 696 | duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target) |
617b465c ZD |
697 | { |
698 | struct loop *cloop; | |
699 | cloop = xcalloc (1, sizeof (struct loop)); | |
700 | place_new_loop (loops, cloop); | |
701 | ||
702 | /* Initialize copied loop. */ | |
703 | cloop->level = loop->level; | |
704 | ||
705 | /* Set it as copy of loop. */ | |
706 | loop->copy = cloop; | |
707 | ||
708 | /* Add it to target. */ | |
709 | flow_loop_tree_node_add (target, cloop); | |
710 | ||
711 | return cloop; | |
712 | } | |
713 | ||
714 | /* Copies structure of subloops of LOOP into TARGET loop, placing | |
715 | newly created loops into loop tree stored in LOOPS. */ | |
d329e058 AJ |
716 | static void |
717 | duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target) | |
617b465c ZD |
718 | { |
719 | struct loop *aloop, *cloop; | |
720 | ||
721 | for (aloop = loop->inner; aloop; aloop = aloop->next) | |
722 | { | |
723 | cloop = duplicate_loop (loops, aloop, target); | |
724 | duplicate_subloops (loops, aloop, cloop); | |
725 | } | |
726 | } | |
727 | ||
728 | /* Copies structure of subloops of N loops, stored in array COPIED_LOOPS, | |
729 | into TARGET loop, placing newly created loops into loop tree LOOPS. */ | |
d329e058 AJ |
730 | static void |
731 | copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target) | |
617b465c ZD |
732 | { |
733 | struct loop *aloop; | |
734 | int i; | |
735 | ||
736 | for (i = 0; i < n; i++) | |
737 | { | |
738 | aloop = duplicate_loop (loops, copied_loops[i], target); | |
739 | duplicate_subloops (loops, copied_loops[i], aloop); | |
740 | } | |
741 | } | |
742 | ||
743 | /* Redirects edge E to basic block DEST. */ | |
744 | static void | |
d329e058 | 745 | loop_redirect_edge (edge e, basic_block dest) |
617b465c ZD |
746 | { |
747 | if (e->dest == dest) | |
748 | return; | |
749 | ||
9ee634e3 | 750 | redirect_edge_and_branch_force (e, dest); |
617b465c ZD |
751 | } |
752 | ||
35b07080 ZD |
753 | /* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set, |
754 | just test whether it is possible to remove the edge. */ | |
617b465c | 755 | static bool |
d329e058 | 756 | loop_delete_branch_edge (edge e, int really_delete) |
617b465c ZD |
757 | { |
758 | basic_block src = e->src; | |
35b07080 ZD |
759 | int irr; |
760 | edge snd; | |
617b465c ZD |
761 | |
762 | if (src->succ->succ_next) | |
763 | { | |
764 | basic_block newdest; | |
35b07080 | 765 | |
617b465c ZD |
766 | /* Cannot handle more than two exit edges. */ |
767 | if (src->succ->succ_next->succ_next) | |
768 | return false; | |
769 | /* And it must be just a simple branch. */ | |
a813c111 | 770 | if (!any_condjump_p (BB_END (src))) |
617b465c ZD |
771 | return false; |
772 | ||
35b07080 ZD |
773 | snd = e == src->succ ? src->succ->succ_next : src->succ; |
774 | newdest = snd->dest; | |
617b465c ZD |
775 | if (newdest == EXIT_BLOCK_PTR) |
776 | return false; | |
777 | ||
35b07080 ZD |
778 | /* Hopefully the above conditions should suffice. */ |
779 | if (!really_delete) | |
780 | return true; | |
781 | ||
782 | /* Redirecting behaves wrongly wrto this flag. */ | |
783 | irr = snd->flags & EDGE_IRREDUCIBLE_LOOP; | |
d329e058 | 784 | |
9ee634e3 | 785 | if (!redirect_edge_and_branch (e, newdest)) |
35b07080 ZD |
786 | return false; |
787 | src->succ->flags &= ~EDGE_IRREDUCIBLE_LOOP; | |
788 | src->succ->flags |= irr; | |
789 | ||
790 | return true; | |
617b465c ZD |
791 | } |
792 | else | |
793 | { | |
794 | /* Cannot happen -- we are using this only to remove an edge | |
3dc575ff | 795 | from branch. */ |
617b465c ZD |
796 | abort (); |
797 | } | |
798 | ||
799 | return false; /* To avoid warning, cannot get here. */ | |
800 | } | |
801 | ||
617b465c ZD |
802 | /* Check whether LOOP's body can be duplicated. */ |
803 | bool | |
d329e058 | 804 | can_duplicate_loop_p (struct loop *loop) |
617b465c | 805 | { |
8d28e87d ZD |
806 | int ret; |
807 | basic_block *bbs = get_loop_body (loop); | |
617b465c | 808 | |
8d28e87d | 809 | ret = can_copy_bbs_p (bbs, loop->num_nodes); |
617b465c | 810 | free (bbs); |
8d28e87d ZD |
811 | |
812 | return ret; | |
617b465c ZD |
813 | } |
814 | ||
617b465c ZD |
815 | #define RDIV(X,Y) (((X) + (Y) / 2) / (Y)) |
816 | ||
8d28e87d ZD |
817 | /* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating |
818 | LOOPS structure and dominators. E's destination must be LOOP header for | |
819 | this to work, i.e. it must be entry or latch edge of this loop; these are | |
820 | unique, as the loops must have preheaders for this function to work | |
821 | correctly (in case E is latch, the function unrolls the loop, if E is entry | |
822 | edge, it peels the loop). Store edges created by copying ORIG edge from | |
823 | copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to | |
824 | original LOOP body, the other copies are numbered in order given by control | |
825 | flow through them) into TO_REMOVE array. Returns false if duplication is | |
826 | impossible. */ | |
617b465c | 827 | int |
d329e058 AJ |
828 | duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops, |
829 | unsigned int ndupl, sbitmap wont_exit, | |
830 | edge orig, edge *to_remove, | |
831 | unsigned int *n_to_remove, int flags) | |
617b465c ZD |
832 | { |
833 | struct loop *target, *aloop; | |
834 | struct loop **orig_loops; | |
835 | unsigned n_orig_loops; | |
836 | basic_block header = loop->header, latch = loop->latch; | |
837 | basic_block *new_bbs, *bbs, *first_active; | |
838 | basic_block new_bb, bb, first_active_latch = NULL; | |
8d28e87d ZD |
839 | edge ae, latch_edge; |
840 | edge spec_edges[2], new_spec_edges[2]; | |
841 | #define SE_LATCH 0 | |
842 | #define SE_ORIG 1 | |
617b465c ZD |
843 | unsigned i, j, n; |
844 | int is_latch = (latch == e->src); | |
845 | int scale_act = 0, *scale_step = NULL, scale_main = 0; | |
846 | int p, freq_in, freq_le, freq_out_orig; | |
847 | int prob_pass_thru, prob_pass_wont_exit, prob_pass_main; | |
848 | int add_irreducible_flag; | |
849 | ||
850 | if (e->dest != loop->header) | |
851 | abort (); | |
852 | if (ndupl <= 0) | |
853 | abort (); | |
854 | ||
855 | if (orig) | |
856 | { | |
857 | /* Orig must be edge out of the loop. */ | |
858 | if (!flow_bb_inside_loop_p (loop, orig->src)) | |
859 | abort (); | |
860 | if (flow_bb_inside_loop_p (loop, orig->dest)) | |
861 | abort (); | |
862 | } | |
863 | ||
864 | bbs = get_loop_body (loop); | |
865 | ||
866 | /* Check whether duplication is possible. */ | |
8d28e87d | 867 | if (!can_copy_bbs_p (bbs, loop->num_nodes)) |
617b465c | 868 | { |
8d28e87d ZD |
869 | free (bbs); |
870 | return false; | |
617b465c | 871 | } |
8d28e87d | 872 | new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes); |
617b465c | 873 | |
8d28e87d ZD |
874 | /* In case we are doing loop peeling and the loop is in the middle of |
875 | irreducible region, the peeled copies will be inside it too. */ | |
876 | add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP; | |
877 | if (is_latch && add_irreducible_flag) | |
878 | abort (); | |
617b465c ZD |
879 | |
880 | /* Find edge from latch. */ | |
881 | latch_edge = loop_latch_edge (loop); | |
882 | ||
883 | if (flags & DLTHE_FLAG_UPDATE_FREQ) | |
884 | { | |
885 | /* Calculate coefficients by that we have to scale frequencies | |
886 | of duplicated loop bodies. */ | |
887 | freq_in = header->frequency; | |
888 | freq_le = EDGE_FREQUENCY (latch_edge); | |
889 | if (freq_in == 0) | |
890 | freq_in = 1; | |
891 | if (freq_in < freq_le) | |
892 | freq_in = freq_le; | |
893 | freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le; | |
894 | if (freq_out_orig > freq_in - freq_le) | |
895 | freq_out_orig = freq_in - freq_le; | |
896 | prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in); | |
897 | prob_pass_wont_exit = | |
898 | RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in); | |
899 | ||
900 | scale_step = xmalloc (ndupl * sizeof (int)); | |
901 | ||
902 | for (i = 1; i <= ndupl; i++) | |
d329e058 | 903 | scale_step[i - 1] = TEST_BIT (wont_exit, i) |
617b465c ZD |
904 | ? prob_pass_wont_exit |
905 | : prob_pass_thru; | |
906 | ||
907 | if (is_latch) | |
908 | { | |
909 | prob_pass_main = TEST_BIT (wont_exit, 0) | |
910 | ? prob_pass_wont_exit | |
911 | : prob_pass_thru; | |
912 | p = prob_pass_main; | |
913 | scale_main = REG_BR_PROB_BASE; | |
914 | for (i = 0; i < ndupl; i++) | |
915 | { | |
916 | scale_main += p; | |
917 | p = RDIV (p * scale_step[i], REG_BR_PROB_BASE); | |
918 | } | |
919 | scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main); | |
920 | scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE); | |
921 | } | |
922 | else | |
923 | { | |
924 | scale_main = REG_BR_PROB_BASE; | |
925 | for (i = 0; i < ndupl; i++) | |
926 | scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE); | |
927 | scale_act = REG_BR_PROB_BASE - prob_pass_thru; | |
928 | } | |
929 | for (i = 0; i < ndupl; i++) | |
930 | if (scale_step[i] < 0 || scale_step[i] > REG_BR_PROB_BASE) | |
931 | abort (); | |
932 | if (scale_main < 0 || scale_main > REG_BR_PROB_BASE | |
933 | || scale_act < 0 || scale_act > REG_BR_PROB_BASE) | |
934 | abort (); | |
935 | } | |
936 | ||
937 | /* Loop the new bbs will belong to. */ | |
8d28e87d | 938 | target = e->src->loop_father; |
617b465c ZD |
939 | |
940 | /* Original loops. */ | |
941 | n_orig_loops = 0; | |
942 | for (aloop = loop->inner; aloop; aloop = aloop->next) | |
943 | n_orig_loops++; | |
944 | orig_loops = xcalloc (n_orig_loops, sizeof (struct loop *)); | |
945 | for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++) | |
946 | orig_loops[i] = aloop; | |
947 | ||
948 | loop->copy = target; | |
d329e058 | 949 | |
617b465c ZD |
950 | n = loop->num_nodes; |
951 | ||
8d28e87d | 952 | first_active = xmalloc (n * sizeof (basic_block)); |
617b465c ZD |
953 | if (is_latch) |
954 | { | |
955 | memcpy (first_active, bbs, n * sizeof (basic_block)); | |
956 | first_active_latch = latch; | |
957 | } | |
958 | ||
8d28e87d ZD |
959 | /* Record exit edge in original loop body. */ |
960 | if (orig && TEST_BIT (wont_exit, 0)) | |
961 | to_remove[(*n_to_remove)++] = orig; | |
962 | ||
963 | spec_edges[SE_ORIG] = orig; | |
964 | spec_edges[SE_LATCH] = latch_edge; | |
d329e058 | 965 | |
617b465c ZD |
966 | for (j = 0; j < ndupl; j++) |
967 | { | |
968 | /* Copy loops. */ | |
969 | copy_loops_to (loops, orig_loops, n_orig_loops, target); | |
970 | ||
971 | /* Copy bbs. */ | |
d47cc544 | 972 | copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop); |
8d28e87d | 973 | |
84d45ad1 ZD |
974 | /* Note whether the blocks and edges belong to an irreducible loop. */ |
975 | if (add_irreducible_flag) | |
976 | { | |
977 | for (i = 0; i < n; i++) | |
978 | new_bbs[i]->rbi->duplicated = 1; | |
979 | for (i = 0; i < n; i++) | |
980 | { | |
981 | new_bb = new_bbs[i]; | |
982 | if (new_bb->loop_father == target) | |
983 | new_bb->flags |= BB_IRREDUCIBLE_LOOP; | |
984 | ||
985 | for (ae = new_bb->succ; ae; ae = ae->succ_next) | |
986 | if (ae->dest->rbi->duplicated | |
987 | && (ae->src->loop_father == target | |
988 | || ae->dest->loop_father == target)) | |
989 | ae->flags |= EDGE_IRREDUCIBLE_LOOP; | |
990 | } | |
991 | for (i = 0; i < n; i++) | |
992 | new_bbs[i]->rbi->duplicated = 0; | |
993 | } | |
994 | ||
8d28e87d | 995 | /* Redirect the special edges. */ |
617b465c | 996 | if (is_latch) |
8d28e87d ZD |
997 | { |
998 | redirect_edge_and_branch_force (latch_edge, new_bbs[0]); | |
999 | redirect_edge_and_branch_force (new_spec_edges[SE_LATCH], | |
1000 | loop->header); | |
d47cc544 | 1001 | set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch); |
8d28e87d ZD |
1002 | latch = loop->latch = new_bbs[1]; |
1003 | e = latch_edge = new_spec_edges[SE_LATCH]; | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | redirect_edge_and_branch_force (new_spec_edges[SE_LATCH], | |
1008 | loop->header); | |
1009 | redirect_edge_and_branch_force (e, new_bbs[0]); | |
d47cc544 | 1010 | set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src); |
8d28e87d ZD |
1011 | e = new_spec_edges[SE_LATCH]; |
1012 | } | |
617b465c | 1013 | |
8d28e87d ZD |
1014 | /* Record exit edge in this copy. */ |
1015 | if (orig && TEST_BIT (wont_exit, j + 1)) | |
1016 | to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG]; | |
d329e058 | 1017 | |
8d28e87d ZD |
1018 | /* Record the first copy in the control flow order if it is not |
1019 | the original loop (i.e. in case of peeling). */ | |
617b465c ZD |
1020 | if (!first_active_latch) |
1021 | { | |
1022 | memcpy (first_active, new_bbs, n * sizeof (basic_block)); | |
8d28e87d | 1023 | first_active_latch = new_bbs[1]; |
617b465c | 1024 | } |
d329e058 | 1025 | |
8d28e87d ZD |
1026 | /* Set counts and frequencies. */ |
1027 | if (flags & DLTHE_FLAG_UPDATE_FREQ) | |
617b465c | 1028 | { |
8d28e87d ZD |
1029 | scale_bbs_frequencies (new_bbs, n, scale_act, REG_BR_PROB_BASE); |
1030 | scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE); | |
617b465c ZD |
1031 | } |
1032 | } | |
8d28e87d ZD |
1033 | free (new_bbs); |
1034 | free (orig_loops); | |
1035 | ||
1036 | /* Update the original loop. */ | |
1037 | if (!is_latch) | |
d47cc544 | 1038 | set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src); |
617b465c ZD |
1039 | if (flags & DLTHE_FLAG_UPDATE_FREQ) |
1040 | { | |
8d28e87d | 1041 | scale_bbs_frequencies (bbs, n, scale_main, REG_BR_PROB_BASE); |
617b465c ZD |
1042 | free (scale_step); |
1043 | } | |
617b465c | 1044 | |
8d28e87d | 1045 | /* Update dominators of outer blocks if affected. */ |
617b465c ZD |
1046 | for (i = 0; i < n; i++) |
1047 | { | |
1048 | basic_block dominated, dom_bb, *dom_bbs; | |
1049 | int n_dom_bbs,j; | |
1050 | ||
1051 | bb = bbs[i]; | |
d47cc544 | 1052 | n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs); |
617b465c ZD |
1053 | for (j = 0; j < n_dom_bbs; j++) |
1054 | { | |
1055 | dominated = dom_bbs[j]; | |
1056 | if (flow_bb_inside_loop_p (loop, dominated)) | |
1057 | continue; | |
1058 | dom_bb = nearest_common_dominator ( | |
d47cc544 SB |
1059 | CDI_DOMINATORS, first_active[i], first_active_latch); |
1060 | set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb); | |
617b465c ZD |
1061 | } |
1062 | free (dom_bbs); | |
1063 | } | |
1064 | free (first_active); | |
1065 | ||
1066 | free (bbs); | |
1067 | ||
1068 | return true; | |
1069 | } | |
1070 | ||
f470c378 ZD |
1071 | /* A callback for make_forwarder block, to redirect all edges except for |
1072 | MFB_KJ_EDGE to the entry part. E is the edge for that we should decide | |
1073 | whether to redirect it. */ | |
1074 | ||
1075 | static edge mfb_kj_edge; | |
1076 | static bool | |
1077 | mfb_keep_just (edge e) | |
1078 | { | |
1079 | return e != mfb_kj_edge; | |
1080 | } | |
1081 | ||
1082 | /* A callback for make_forwarder block, to update data structures for a basic | |
1083 | block JUMP created by redirecting an edge (only the latch edge is being | |
1084 | redirected). */ | |
1085 | ||
1086 | static void | |
1087 | mfb_update_loops (basic_block jump) | |
1088 | { | |
1089 | struct loop *loop = jump->succ->dest->loop_father; | |
1090 | ||
1091 | if (dom_computed[CDI_DOMINATORS]) | |
1092 | set_immediate_dominator (CDI_DOMINATORS, jump, jump->pred->src); | |
1093 | add_bb_to_loop (jump, loop); | |
1094 | loop->latch = jump; | |
1095 | } | |
1096 | ||
3d436d2a ZD |
1097 | /* Creates a pre-header for a LOOP. Returns newly created block. Unless |
1098 | CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single | |
1099 | entry; otherwise we also force preheader block to have only one successor. | |
f470c378 ZD |
1100 | The function also updates dominators. */ |
1101 | ||
3d436d2a | 1102 | static basic_block |
d47cc544 | 1103 | create_preheader (struct loop *loop, int flags) |
3d436d2a ZD |
1104 | { |
1105 | edge e, fallthru; | |
1106 | basic_block dummy; | |
3d436d2a ZD |
1107 | struct loop *cloop, *ploop; |
1108 | int nentry = 0; | |
f470c378 | 1109 | bool irred = false; |
3d436d2a ZD |
1110 | |
1111 | cloop = loop->outer; | |
1112 | ||
1113 | for (e = loop->header->pred; e; e = e->pred_next) | |
1114 | { | |
1115 | if (e->src == loop->latch) | |
1116 | continue; | |
f470c378 | 1117 | irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0; |
3d436d2a ZD |
1118 | nentry++; |
1119 | } | |
1120 | if (!nentry) | |
1121 | abort (); | |
1122 | if (nentry == 1) | |
1123 | { | |
1124 | for (e = loop->header->pred; e->src == loop->latch; e = e->pred_next); | |
1125 | if (!(flags & CP_SIMPLE_PREHEADERS) | |
1126 | || !e->src->succ->succ_next) | |
1127 | return NULL; | |
1128 | } | |
1129 | ||
f470c378 ZD |
1130 | mfb_kj_edge = loop_latch_edge (loop); |
1131 | fallthru = make_forwarder_block (loop->header, mfb_keep_just, | |
1132 | mfb_update_loops); | |
3d436d2a ZD |
1133 | dummy = fallthru->src; |
1134 | loop->header = fallthru->dest; | |
1135 | ||
1136 | /* The header could be a latch of some superloop(s); due to design of | |
1137 | split_block, it would now move to fallthru->dest. */ | |
1138 | for (ploop = loop; ploop; ploop = ploop->outer) | |
1139 | if (ploop->latch == dummy) | |
1140 | ploop->latch = fallthru->dest; | |
1141 | ||
f470c378 ZD |
1142 | /* Reorganize blocks so that the preheader is not stuck in the middle of the |
1143 | loop. */ | |
3d436d2a | 1144 | for (e = dummy->pred; e; e = e->pred_next) |
f470c378 ZD |
1145 | if (e->src != loop->latch) |
1146 | break; | |
1147 | move_block_after (dummy, e->src); | |
1148 | ||
1149 | loop->header->loop_father = loop; | |
1150 | add_bb_to_loop (dummy, cloop); | |
3d436d2a | 1151 | |
f470c378 | 1152 | if (irred) |
3d436d2a | 1153 | { |
f470c378 ZD |
1154 | dummy->flags |= BB_IRREDUCIBLE_LOOP; |
1155 | dummy->succ->flags |= EDGE_IRREDUCIBLE_LOOP; | |
3d436d2a ZD |
1156 | } |
1157 | ||
c263766c RH |
1158 | if (dump_file) |
1159 | fprintf (dump_file, "Created preheader block for loop %i\n", | |
3d436d2a ZD |
1160 | loop->num); |
1161 | ||
1162 | return dummy; | |
1163 | } | |
1164 | ||
617b465c ZD |
1165 | /* Create preheaders for each loop from loop tree stored in LOOPS; for meaning |
1166 | of FLAGS see create_preheader. */ | |
3d436d2a | 1167 | void |
d329e058 | 1168 | create_preheaders (struct loops *loops, int flags) |
3d436d2a ZD |
1169 | { |
1170 | unsigned i; | |
1171 | for (i = 1; i < loops->num; i++) | |
d47cc544 | 1172 | create_preheader (loops->parray[i], flags); |
3d436d2a ZD |
1173 | loops->state |= LOOPS_HAVE_PREHEADERS; |
1174 | } | |
1175 | ||
617b465c ZD |
1176 | /* Forces all loop latches of loops from loop tree LOOPS to have only single |
1177 | successor. */ | |
3d436d2a | 1178 | void |
d329e058 | 1179 | force_single_succ_latches (struct loops *loops) |
3d436d2a ZD |
1180 | { |
1181 | unsigned i; | |
1182 | struct loop *loop; | |
1183 | edge e; | |
1184 | ||
1185 | for (i = 1; i < loops->num; i++) | |
1186 | { | |
1187 | loop = loops->parray[i]; | |
977129f6 ZD |
1188 | if (loop->latch != loop->header |
1189 | && !loop->latch->succ->succ_next) | |
3d436d2a | 1190 | continue; |
d329e058 | 1191 | |
bc810602 ZD |
1192 | for (e = loop->header->pred; e->src != loop->latch; e = e->pred_next) |
1193 | continue; | |
1194 | ||
d47cc544 | 1195 | loop_split_edge_with (e, NULL_RTX); |
3d436d2a ZD |
1196 | } |
1197 | loops->state |= LOOPS_HAVE_SIMPLE_LATCHES; | |
1198 | } | |
1199 | ||
617b465c ZD |
1200 | /* A quite stupid function to put INSNS on edge E. They are supposed to form |
1201 | just one basic block. Jumps in INSNS are not handled, so cfg do not have to | |
1202 | be ok after this function. The created block is placed on correct place | |
1203 | in LOOPS structure and its dominator is set. */ | |
3d436d2a | 1204 | basic_block |
d47cc544 | 1205 | loop_split_edge_with (edge e, rtx insns) |
3d436d2a ZD |
1206 | { |
1207 | basic_block src, dest, new_bb; | |
1208 | struct loop *loop_c; | |
1209 | edge new_e; | |
d329e058 | 1210 | |
3d436d2a ZD |
1211 | src = e->src; |
1212 | dest = e->dest; | |
1213 | ||
1214 | loop_c = find_common_loop (src->loop_father, dest->loop_father); | |
1215 | ||
1216 | /* Create basic block for it. */ | |
1217 | ||
bc35512f | 1218 | new_bb = split_edge (e); |
3d436d2a ZD |
1219 | add_bb_to_loop (new_bb, loop_c); |
1220 | new_bb->flags = insns ? BB_SUPERBLOCK : 0; | |
3d436d2a | 1221 | |
bc35512f | 1222 | new_e = new_bb->succ; |
35b07080 ZD |
1223 | if (e->flags & EDGE_IRREDUCIBLE_LOOP) |
1224 | { | |
1225 | new_bb->flags |= BB_IRREDUCIBLE_LOOP; | |
1226 | new_e->flags |= EDGE_IRREDUCIBLE_LOOP; | |
1227 | } | |
3d436d2a | 1228 | |
3d436d2a | 1229 | if (insns) |
a813c111 | 1230 | emit_insn_after (insns, BB_END (new_bb)); |
3d436d2a | 1231 | |
3d436d2a ZD |
1232 | if (dest->loop_father->latch == src) |
1233 | dest->loop_father->latch = new_bb; | |
d329e058 | 1234 | |
3d436d2a ZD |
1235 | return new_bb; |
1236 | } |