]> gcc.gnu.org Git - gcc.git/blame - gcc/cfgloopmanip.c
cfgloopmanip.c (update_single_exit_for_duplicated_loop, [...]): New functions.
[gcc.git] / gcc / cfgloopmanip.c
CommitLineData
3d436d2a 1/* Loop manipulation code for GNU compiler.
613c5cd0 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3d436d2a
ZD
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
3d436d2a
ZD
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"
7932a3db 27#include "obstack.h"
3d436d2a
ZD
28#include "basic-block.h"
29#include "cfgloop.h"
30#include "cfglayout.h"
1cb7dfc3 31#include "cfghooks.h"
3d436d2a
ZD
32#include "output.h"
33
d329e058
AJ
34static void duplicate_subloops (struct loops *, struct loop *, struct loop *);
35static void copy_loops_to (struct loops *, struct loop **, int,
36 struct loop *);
37static void loop_redirect_edge (edge, basic_block);
38static bool loop_delete_branch_edge (edge, int);
d47cc544 39static void remove_bbs (basic_block *, int);
d329e058 40static bool rpe_enum_p (basic_block, void *);
d47cc544 41static int find_path (edge, basic_block **);
d329e058
AJ
42static bool alp_enum_p (basic_block, void *);
43static void add_loop (struct loops *, struct loop *);
dc14f191 44static void fix_loop_placements (struct loops *, struct loop *, bool *);
d329e058 45static bool fix_bb_placement (struct loops *, basic_block);
dc14f191 46static void fix_bb_placements (struct loops *, basic_block, bool *);
d329e058
AJ
47static void place_new_loop (struct loops *, struct loop *);
48static void scale_loop_frequencies (struct loop *, int, int);
d47cc544 49static basic_block create_preheader (struct loop *, int);
dc14f191 50static void unloop (struct loops *, struct loop *, bool *);
3d436d2a 51
bade3a00
JH
52#define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
53
d47cc544 54/* Checks whether basic block BB is dominated by DATA. */
617b465c 55static bool
d329e058 56rpe_enum_p (basic_block bb, void *data)
617b465c 57{
d47cc544 58 return dominated_by_p (CDI_DOMINATORS, bb, data);
617b465c
ZD
59}
60
61/* Remove basic blocks BBS from loop structure and dominance info,
62 and delete them afterwards. */
63static void
d47cc544 64remove_bbs (basic_block *bbs, int nbbs)
617b465c
ZD
65{
66 int i;
67
68 for (i = 0; i < nbbs; i++)
69 {
70 remove_bb_from_loops (bbs[i]);
f470c378 71 delete_basic_block (bbs[i]);
617b465c
ZD
72 }
73}
74
75/* Find path -- i.e. the basic blocks dominated by edge E and put them
76 into array BBS, that will be allocated large enough to contain them.
35b07080
ZD
77 E->dest must have exactly one predecessor for this to work (it is
78 easy to achieve and we do not put it here because we do not want to
79 alter anything by this function). The number of basic blocks in the
80 path is returned. */
617b465c 81static int
d47cc544 82find_path (edge e, basic_block **bbs)
617b465c 83{
628f6a4e 84 gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
617b465c
ZD
85
86 /* Find bbs in the path. */
5ed6ace5 87 *bbs = XCNEWVEC (basic_block, n_basic_blocks);
617b465c 88 return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs,
d47cc544 89 n_basic_blocks, e->dest);
617b465c
ZD
90}
91
92/* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
93 Let L be a loop to that BB belongs. Then every successor of BB must either
94 1) belong to some superloop of loop L, or
95 2) be a header of loop K such that K->outer is superloop of L
96 Returns true if we had to move BB into other loop to enforce this condition,
97 false if the placement of BB was already correct (provided that placements
98 of its successors are correct). */
99static bool
d329e058 100fix_bb_placement (struct loops *loops, basic_block bb)
617b465c
ZD
101{
102 edge e;
628f6a4e 103 edge_iterator ei;
617b465c
ZD
104 struct loop *loop = loops->tree_root, *act;
105
628f6a4e 106 FOR_EACH_EDGE (e, ei, bb->succs)
617b465c
ZD
107 {
108 if (e->dest == EXIT_BLOCK_PTR)
109 continue;
110
111 act = e->dest->loop_father;
112 if (act->header == e->dest)
113 act = act->outer;
114
115 if (flow_loop_nested_p (loop, act))
116 loop = act;
117 }
118
119 if (loop == bb->loop_father)
120 return false;
121
122 remove_bb_from_loops (bb);
123 add_bb_to_loop (bb, loop);
124
125 return true;
126}
127
128/* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
129 enforce condition condition stated in description of fix_bb_placement. We
130 start from basic block FROM that had some of its successors removed, so that
131 his placement no longer has to be correct, and iteratively fix placement of
132 its predecessors that may change if placement of FROM changed. Also fix
133 placement of subloops of FROM->loop_father, that might also be altered due
4d6922ee 134 to this change; the condition for them is similar, except that instead of
dc14f191
ZD
135 successors we consider edges coming out of the loops.
136
137 If the changes may invalidate the information about irreducible regions,
138 IRRED_INVALIDATED is set to true. */
139
617b465c 140static void
dc14f191
ZD
141fix_bb_placements (struct loops *loops, basic_block from,
142 bool *irred_invalidated)
617b465c
ZD
143{
144 sbitmap in_queue;
145 basic_block *queue, *qtop, *qbeg, *qend;
146 struct loop *base_loop;
147 edge e;
148
149 /* We pass through blocks back-reachable from FROM, testing whether some
150 of their successors moved to outer loop. It may be necessary to
151 iterate several times, but it is finite, as we stop unless we move
152 the basic block up the loop structure. The whole story is a bit
153 more complicated due to presence of subloops, those are moved using
154 fix_loop_placement. */
155
156 base_loop = from->loop_father;
157 if (base_loop == loops->tree_root)
158 return;
159
160 in_queue = sbitmap_alloc (last_basic_block);
161 sbitmap_zero (in_queue);
162 SET_BIT (in_queue, from->index);
163 /* Prevent us from going out of the base_loop. */
164 SET_BIT (in_queue, base_loop->header->index);
165
5ed6ace5 166 queue = XNEWVEC (basic_block, base_loop->num_nodes + 1);
617b465c
ZD
167 qtop = queue + base_loop->num_nodes + 1;
168 qbeg = queue;
169 qend = queue + 1;
170 *qbeg = from;
171
172 while (qbeg != qend)
173 {
628f6a4e 174 edge_iterator ei;
617b465c
ZD
175 from = *qbeg;
176 qbeg++;
177 if (qbeg == qtop)
178 qbeg = queue;
179 RESET_BIT (in_queue, from->index);
180
181 if (from->loop_father->header == from)
182 {
183 /* Subloop header, maybe move the loop upward. */
184 if (!fix_loop_placement (from->loop_father))
185 continue;
186 }
187 else
188 {
189 /* Ordinary basic block. */
190 if (!fix_bb_placement (loops, from))
191 continue;
192 }
193
dc14f191
ZD
194 FOR_EACH_EDGE (e, ei, from->succs)
195 {
196 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
197 *irred_invalidated = true;
198 }
199
617b465c 200 /* Something has changed, insert predecessors into queue. */
628f6a4e 201 FOR_EACH_EDGE (e, ei, from->preds)
617b465c
ZD
202 {
203 basic_block pred = e->src;
204 struct loop *nca;
205
dc14f191
ZD
206 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
207 *irred_invalidated = true;
208
617b465c
ZD
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
241/* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
242 and update loop structure stored in LOOPS and dominators. Return true if
243 we were able to remove the path, false otherwise (and nothing is affected
244 then). */
245bool
d329e058 246remove_path (struct loops *loops, edge e)
617b465c
ZD
247{
248 edge ae;
249 basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb;
250 int i, nrem, n_bord_bbs, n_dom_bbs;
251 sbitmap seen;
dc14f191 252 bool deleted, irred_invalidated = false;
617b465c 253
35b07080
ZD
254 if (!loop_delete_branch_edge (e, 0))
255 return false;
256
dc14f191
ZD
257 /* Keep track of whether we need to update information about irreducible
258 regions. This is the case if the removed area is a part of the
259 irreducible region, or if the set of basic blocks that belong to a loop
260 that is inside an irreducible region is changed, or if such a loop is
261 removed. */
262 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
263 irred_invalidated = true;
264
35b07080
ZD
265 /* We need to check whether basic blocks are dominated by the edge
266 e, but we only have basic block dominators. This is easy to
267 fix -- when e->dest has exactly one predecessor, this corresponds
268 to blocks dominated by e->dest, if not, split the edge. */
c5cbcccf
ZD
269 if (!single_pred_p (e->dest))
270 e = single_pred_edge (loop_split_edge_with (e, NULL_RTX));
35b07080
ZD
271
272 /* It may happen that by removing path we remove one or more loops
273 we belong to. In this case first unloop the loops, then proceed
274 normally. We may assume that e->dest is not a header of any loop,
275 as it now has exactly one predecessor. */
276 while (e->src->loop_father->outer
d47cc544 277 && dominated_by_p (CDI_DOMINATORS,
35b07080 278 e->src->loop_father->latch, e->dest))
dc14f191 279 unloop (loops, e->src->loop_father, &irred_invalidated);
d329e058 280
35b07080 281 /* Identify the path. */
d47cc544 282 nrem = find_path (e, &rem_bbs);
617b465c
ZD
283
284 n_bord_bbs = 0;
5ed6ace5 285 bord_bbs = XCNEWVEC (basic_block, n_basic_blocks);
617b465c
ZD
286 seen = sbitmap_alloc (last_basic_block);
287 sbitmap_zero (seen);
288
289 /* Find "border" hexes -- i.e. those with predecessor in removed path. */
290 for (i = 0; i < nrem; i++)
291 SET_BIT (seen, rem_bbs[i]->index);
35b07080 292 for (i = 0; i < nrem; i++)
617b465c 293 {
628f6a4e 294 edge_iterator ei;
35b07080 295 bb = rem_bbs[i];
628f6a4e 296 FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
35b07080
ZD
297 if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
298 {
299 SET_BIT (seen, ae->dest->index);
300 bord_bbs[n_bord_bbs++] = ae->dest;
dc14f191
ZD
301
302 if (ae->flags & EDGE_IRREDUCIBLE_LOOP)
303 irred_invalidated = true;
35b07080 304 }
617b465c 305 }
617b465c
ZD
306
307 /* Remove the path. */
308 from = e->src;
341c100f
NS
309 deleted = loop_delete_branch_edge (e, 1);
310 gcc_assert (deleted);
5ed6ace5 311 dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
617b465c
ZD
312
313 /* Cancel loops contained in the path. */
314 for (i = 0; i < nrem; i++)
315 if (rem_bbs[i]->loop_father->header == rem_bbs[i])
316 cancel_loop_tree (loops, rem_bbs[i]->loop_father);
317
d47cc544 318 remove_bbs (rem_bbs, nrem);
617b465c
ZD
319 free (rem_bbs);
320
35b07080 321 /* Find blocks whose dominators may be affected. */
617b465c
ZD
322 n_dom_bbs = 0;
323 sbitmap_zero (seen);
324 for (i = 0; i < n_bord_bbs; i++)
325 {
d47cc544 326 basic_block ldom;
617b465c 327
d47cc544 328 bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
617b465c
ZD
329 if (TEST_BIT (seen, bb->index))
330 continue;
331 SET_BIT (seen, bb->index);
332
d47cc544
SB
333 for (ldom = first_dom_son (CDI_DOMINATORS, bb);
334 ldom;
335 ldom = next_dom_son (CDI_DOMINATORS, ldom))
336 if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
337 dom_bbs[n_dom_bbs++] = ldom;
617b465c
ZD
338 }
339
617b465c
ZD
340 free (seen);
341
342 /* Recount dominators. */
d47cc544 343 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
617b465c 344 free (dom_bbs);
35b07080
ZD
345 free (bord_bbs);
346
617b465c
ZD
347 /* Fix placements of basic blocks inside loops and the placement of
348 loops in the loop tree. */
dc14f191
ZD
349 fix_bb_placements (loops, from, &irred_invalidated);
350 fix_loop_placements (loops, from->loop_father, &irred_invalidated);
351
352 if (irred_invalidated
353 && (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) != 0)
354 mark_irreducible_loops (loops);
617b465c
ZD
355
356 return true;
357}
358
359/* Predicate for enumeration in add_loop. */
360static bool
d329e058 361alp_enum_p (basic_block bb, void *alp_header)
617b465c
ZD
362{
363 return bb != (basic_block) alp_header;
364}
365
366/* Given LOOP structure with filled header and latch, find the body of the
367 corresponding loop and add it to LOOPS tree. */
368static void
d329e058 369add_loop (struct loops *loops, struct loop *loop)
617b465c
ZD
370{
371 basic_block *bbs;
372 int i, n;
d329e058 373
617b465c
ZD
374 /* Add it to loop structure. */
375 place_new_loop (loops, loop);
376 loop->level = 1;
377
378 /* Find its nodes. */
5ed6ace5 379 bbs = XCNEWVEC (basic_block, n_basic_blocks);
617b465c
ZD
380 n = dfs_enumerate_from (loop->latch, 1, alp_enum_p,
381 bbs, n_basic_blocks, loop->header);
382
383 for (i = 0; i < n; i++)
384 add_bb_to_loop (bbs[i], loop);
385 add_bb_to_loop (loop->header, loop);
386
387 free (bbs);
388}
389
617b465c
ZD
390/* Multiply all frequencies in LOOP by NUM/DEN. */
391static void
d329e058 392scale_loop_frequencies (struct loop *loop, int num, int den)
617b465c
ZD
393{
394 basic_block *bbs;
395
396 bbs = get_loop_body (loop);
33156717 397 scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
617b465c
ZD
398 free (bbs);
399}
400
401/* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
402 latch to header and update loop tree stored in LOOPS and dominators
403 accordingly. Everything between them plus LATCH_EDGE destination must
404 be dominated by HEADER_EDGE destination, and back-reachable from
405 LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
5132abc2
KH
406 FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
407 TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
617b465c 408 Returns newly created loop. */
50654f6c 409
617b465c 410struct loop *
c22cacf3 411loopify (struct loops *loops, edge latch_edge, edge header_edge,
5132abc2
KH
412 basic_block switch_bb, edge true_edge, edge false_edge,
413 bool redirect_all_edges)
617b465c
ZD
414{
415 basic_block succ_bb = latch_edge->dest;
416 basic_block pred_bb = header_edge->src;
417 basic_block *dom_bbs, *body;
d47cc544 418 unsigned n_dom_bbs, i;
617b465c 419 sbitmap seen;
5ed6ace5 420 struct loop *loop = XCNEW (struct loop);
617b465c
ZD
421 struct loop *outer = succ_bb->loop_father->outer;
422 int freq, prob, tot_prob;
423 gcov_type cnt;
424 edge e;
628f6a4e 425 edge_iterator ei;
617b465c
ZD
426
427 loop->header = header_edge->dest;
428 loop->latch = latch_edge->src;
429
430 freq = EDGE_FREQUENCY (header_edge);
431 cnt = header_edge->count;
628f6a4e
BE
432 prob = EDGE_SUCC (switch_bb, 0)->probability;
433 tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
617b465c
ZD
434 if (tot_prob == 0)
435 tot_prob = 1;
436
437 /* Redirect edges. */
438 loop_redirect_edge (latch_edge, loop->header);
5132abc2 439 loop_redirect_edge (true_edge, succ_bb);
50654f6c 440
92fc4a2f
ZD
441 /* During loop versioning, one of the switch_bb edge is already properly
442 set. Do not redirect it again unless redirect_all_edges is true. */
443 if (redirect_all_edges)
444 {
445 loop_redirect_edge (header_edge, switch_bb);
c22cacf3
MS
446 loop_redirect_edge (false_edge, loop->header);
447
92fc4a2f
ZD
448 /* Update dominators. */
449 set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
450 set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
451 }
50654f6c 452
d47cc544 453 set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
617b465c
ZD
454
455 /* Compute new loop. */
456 add_loop (loops, loop);
457 flow_loop_tree_node_add (outer, loop);
458
459 /* Add switch_bb to appropriate loop. */
460 add_bb_to_loop (switch_bb, outer);
461
462 /* Fix frequencies. */
463 switch_bb->frequency = freq;
464 switch_bb->count = cnt;
628f6a4e 465 FOR_EACH_EDGE (e, ei, switch_bb->succs)
617b465c
ZD
466 e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
467 scale_loop_frequencies (loop, prob, tot_prob);
468 scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
469
470 /* Update dominators of blocks outside of LOOP. */
5ed6ace5 471 dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
617b465c
ZD
472 n_dom_bbs = 0;
473 seen = sbitmap_alloc (last_basic_block);
474 sbitmap_zero (seen);
475 body = get_loop_body (loop);
476
477 for (i = 0; i < loop->num_nodes; i++)
478 SET_BIT (seen, body[i]->index);
479
480 for (i = 0; i < loop->num_nodes; i++)
481 {
d47cc544 482 basic_block ldom;
617b465c 483
d47cc544
SB
484 for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
485 ldom;
486 ldom = next_dom_son (CDI_DOMINATORS, ldom))
487 if (!TEST_BIT (seen, ldom->index))
617b465c 488 {
d47cc544
SB
489 SET_BIT (seen, ldom->index);
490 dom_bbs[n_dom_bbs++] = ldom;
617b465c 491 }
617b465c
ZD
492 }
493
d47cc544 494 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
617b465c
ZD
495
496 free (body);
497 free (seen);
498 free (dom_bbs);
499
500 return loop;
501}
502
35b07080
ZD
503/* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
504 the LOOP was removed. After this function, original loop latch will
dc14f191
ZD
505 have no successor, which caller is expected to fix somehow.
506
507 If this may cause the information about irreducible regions to become
508 invalid, IRRED_INVALIDATED is set to true. */
509
25a6c68b 510static void
dc14f191 511unloop (struct loops *loops, struct loop *loop, bool *irred_invalidated)
35b07080
ZD
512{
513 basic_block *body;
514 struct loop *ploop;
515 unsigned i, n;
516 basic_block latch = loop->latch;
dc14f191
ZD
517 bool dummy = false;
518
519 if (loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
520 *irred_invalidated = true;
35b07080 521
e0bb17a8 522 /* This is relatively straightforward. The dominators are unchanged, as
35b07080
ZD
523 loop header dominates loop latch, so the only thing we have to care of
524 is the placement of loops and basic blocks inside the loop tree. We
525 move them all to the loop->outer, and then let fix_bb_placements do
526 its work. */
527
528 body = get_loop_body (loop);
35b07080
ZD
529 n = loop->num_nodes;
530 for (i = 0; i < n; i++)
531 if (body[i]->loop_father == loop)
532 {
533 remove_bb_from_loops (body[i]);
534 add_bb_to_loop (body[i], loop->outer);
535 }
536 free(body);
537
538 while (loop->inner)
539 {
540 ploop = loop->inner;
541 flow_loop_tree_node_remove (ploop);
542 flow_loop_tree_node_add (loop->outer, ploop);
543 }
544
545 /* Remove the loop and free its data. */
546 flow_loop_tree_node_remove (loop);
547 loops->parray[loop->num] = NULL;
548 flow_loop_free (loop);
549
c5cbcccf 550 remove_edge (single_succ_edge (latch));
dc14f191
ZD
551
552 /* We do not pass IRRED_INVALIDATED to fix_bb_placements here, as even if
553 there is an irreducible region inside the cancelled loop, the flags will
554 be still correct. */
555 fix_bb_placements (loops, latch, &dummy);
35b07080
ZD
556}
557
617b465c 558/* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
2067c116 559 FATHER of LOOP such that all of the edges coming out of LOOP belong to
dc14f191 560 FATHER, and set it as outer loop of LOOP. Return true if placement of
617b465c 561 LOOP changed. */
dc14f191 562
617b465c 563int
d329e058 564fix_loop_placement (struct loop *loop)
617b465c
ZD
565{
566 basic_block *body;
567 unsigned i;
568 edge e;
628f6a4e 569 edge_iterator ei;
617b465c
ZD
570 struct loop *father = loop->pred[0], *act;
571
572 body = get_loop_body (loop);
573 for (i = 0; i < loop->num_nodes; i++)
628f6a4e 574 FOR_EACH_EDGE (e, ei, body[i]->succs)
617b465c
ZD
575 if (!flow_bb_inside_loop_p (loop, e->dest))
576 {
577 act = find_common_loop (loop, e->dest->loop_father);
578 if (flow_loop_nested_p (father, act))
579 father = act;
580 }
581 free (body);
582
583 if (father != loop->outer)
584 {
585 for (act = loop->outer; act != father; act = act->outer)
586 act->num_nodes -= loop->num_nodes;
587 flow_loop_tree_node_remove (loop);
588 flow_loop_tree_node_add (father, loop);
589 return 1;
590 }
591 return 0;
592}
593
594/* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
595 condition stated in description of fix_loop_placement holds for them.
596 It is used in case when we removed some edges coming out of LOOP, which
dc14f191
ZD
597 may cause the right placement of LOOP inside loop tree to change.
598
599 IRRED_INVALIDATED is set to true if a change in the loop structures might
600 invalidate the information about irreducible regions. */
601
617b465c 602static void
dc14f191
ZD
603fix_loop_placements (struct loops *loops, struct loop *loop,
604 bool *irred_invalidated)
617b465c
ZD
605{
606 struct loop *outer;
607
608 while (loop->outer)
609 {
610 outer = loop->outer;
611 if (!fix_loop_placement (loop))
c22cacf3 612 break;
1548580c
EB
613
614 /* Changing the placement of a loop in the loop tree may alter the
615 validity of condition 2) of the description of fix_bb_placement
616 for its preheader, because the successor is the header and belongs
617 to the loop. So call fix_bb_placements to fix up the placement
618 of the preheader and (possibly) of its predecessors. */
dc14f191
ZD
619 fix_bb_placements (loops, loop_preheader_edge (loop)->src,
620 irred_invalidated);
617b465c
ZD
621 loop = outer;
622 }
623}
624
625/* Creates place for a new LOOP in LOOPS structure. */
626static void
d329e058 627place_new_loop (struct loops *loops, struct loop *loop)
617b465c
ZD
628{
629 loops->parray =
630 xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
631 loops->parray[loops->num] = loop;
632
633 loop->num = loops->num++;
634}
635
636/* Copies copy of LOOP as subloop of TARGET loop, placing newly
637 created loop into LOOPS structure. */
f67d92e9 638struct loop *
d329e058 639duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
617b465c
ZD
640{
641 struct loop *cloop;
5ed6ace5 642 cloop = XCNEW (struct loop);
617b465c
ZD
643 place_new_loop (loops, cloop);
644
645 /* Initialize copied loop. */
646 cloop->level = loop->level;
647
648 /* Set it as copy of loop. */
649 loop->copy = cloop;
650
651 /* Add it to target. */
652 flow_loop_tree_node_add (target, cloop);
653
654 return cloop;
655}
656
657/* Copies structure of subloops of LOOP into TARGET loop, placing
658 newly created loops into loop tree stored in LOOPS. */
d329e058
AJ
659static void
660duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
617b465c
ZD
661{
662 struct loop *aloop, *cloop;
663
664 for (aloop = loop->inner; aloop; aloop = aloop->next)
665 {
666 cloop = duplicate_loop (loops, aloop, target);
667 duplicate_subloops (loops, aloop, cloop);
668 }
669}
670
671/* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
672 into TARGET loop, placing newly created loops into loop tree LOOPS. */
d329e058
AJ
673static void
674copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
617b465c
ZD
675{
676 struct loop *aloop;
677 int i;
678
679 for (i = 0; i < n; i++)
680 {
681 aloop = duplicate_loop (loops, copied_loops[i], target);
682 duplicate_subloops (loops, copied_loops[i], aloop);
683 }
684}
685
686/* Redirects edge E to basic block DEST. */
687static void
d329e058 688loop_redirect_edge (edge e, basic_block dest)
617b465c
ZD
689{
690 if (e->dest == dest)
691 return;
692
9ee634e3 693 redirect_edge_and_branch_force (e, dest);
617b465c
ZD
694}
695
35b07080
ZD
696/* Deletes edge E from a branch if possible. Unless REALLY_DELETE is set,
697 just test whether it is possible to remove the edge. */
617b465c 698static bool
d329e058 699loop_delete_branch_edge (edge e, int really_delete)
617b465c
ZD
700{
701 basic_block src = e->src;
341c100f 702 basic_block newdest;
35b07080
ZD
703 int irr;
704 edge snd;
617b465c 705
628f6a4e 706 gcc_assert (EDGE_COUNT (src->succs) > 1);
c22cacf3 707
341c100f 708 /* Cannot handle more than two exit edges. */
628f6a4e 709 if (EDGE_COUNT (src->succs) > 2)
341c100f
NS
710 return false;
711 /* And it must be just a simple branch. */
712 if (!any_condjump_p (BB_END (src)))
713 return false;
35b07080 714
628f6a4e 715 snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
341c100f
NS
716 newdest = snd->dest;
717 if (newdest == EXIT_BLOCK_PTR)
718 return false;
d329e058 719
341c100f
NS
720 /* Hopefully the above conditions should suffice. */
721 if (!really_delete)
722 return true;
35b07080 723
341c100f
NS
724 /* Redirecting behaves wrongly wrto this flag. */
725 irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
617b465c 726
341c100f
NS
727 if (!redirect_edge_and_branch (e, newdest))
728 return false;
c5cbcccf
ZD
729 single_succ_edge (src)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
730 single_succ_edge (src)->flags |= irr;
c22cacf3 731
341c100f 732 return true;
617b465c
ZD
733}
734
617b465c
ZD
735/* Check whether LOOP's body can be duplicated. */
736bool
d329e058 737can_duplicate_loop_p (struct loop *loop)
617b465c 738{
8d28e87d
ZD
739 int ret;
740 basic_block *bbs = get_loop_body (loop);
617b465c 741
8d28e87d 742 ret = can_copy_bbs_p (bbs, loop->num_nodes);
617b465c 743 free (bbs);
c22cacf3 744
8d28e87d 745 return ret;
617b465c
ZD
746}
747
82b85a85
ZD
748/* The NBBS blocks in BBS will get duplicated and the copies will be placed
749 to LOOP. Update the single_exit information in superloops of LOOP. */
750
25a6c68b 751static void
82b85a85
ZD
752update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
753 struct loop *loop)
754{
755 unsigned i;
756
757 for (i = 0; i < nbbs; i++)
6580ee77 758 bbs[i]->flags |= BB_DUPLICATED;
82b85a85
ZD
759
760 for (; loop->outer; loop = loop->outer)
761 {
762 if (!loop->single_exit)
763 continue;
764
6580ee77 765 if (loop->single_exit->src->flags & BB_DUPLICATED)
82b85a85
ZD
766 loop->single_exit = NULL;
767 }
768
769 for (i = 0; i < nbbs; i++)
6580ee77 770 bbs[i]->flags &= ~BB_DUPLICATED;
82b85a85
ZD
771}
772
c0493b13
ZD
773/* Updates single exit information for the copy of LOOP. */
774
775static void
776update_single_exit_for_duplicated_loop (struct loop *loop)
777{
778 struct loop *copy = loop->copy;
779 basic_block src, dest;
780 edge exit = loop->single_exit;
781
782 if (!exit)
783 return;
784
785 src = get_bb_copy (exit->src);
786 dest = exit->dest;
787 if (dest->flags & BB_DUPLICATED)
788 dest = get_bb_copy (dest);
789
790 exit = find_edge (src, dest);
791 gcc_assert (exit != NULL);
792 copy->single_exit = exit;
793}
794
795/* Updates single exit information for copies of ORIG_LOOPS and their subloops.
796 N is the number of the loops in the ORIG_LOOPS array. */
797
798static void
799update_single_exit_for_duplicated_loops (struct loop *orig_loops[], unsigned n)
800{
801 unsigned i;
802
803 for (i = 0; i < n; i++)
804 update_single_exit_for_duplicated_loop (orig_loops[i]);
805}
806
8d28e87d
ZD
807/* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
808 LOOPS structure and dominators. E's destination must be LOOP header for
809 this to work, i.e. it must be entry or latch edge of this loop; these are
810 unique, as the loops must have preheaders for this function to work
811 correctly (in case E is latch, the function unrolls the loop, if E is entry
812 edge, it peels the loop). Store edges created by copying ORIG edge from
813 copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
814 original LOOP body, the other copies are numbered in order given by control
815 flow through them) into TO_REMOVE array. Returns false if duplication is
816 impossible. */
1cb7dfc3 817bool
d329e058
AJ
818duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
819 unsigned int ndupl, sbitmap wont_exit,
820 edge orig, edge *to_remove,
821 unsigned int *n_to_remove, int flags)
617b465c
ZD
822{
823 struct loop *target, *aloop;
824 struct loop **orig_loops;
825 unsigned n_orig_loops;
826 basic_block header = loop->header, latch = loop->latch;
827 basic_block *new_bbs, *bbs, *first_active;
828 basic_block new_bb, bb, first_active_latch = NULL;
8d28e87d
ZD
829 edge ae, latch_edge;
830 edge spec_edges[2], new_spec_edges[2];
831#define SE_LATCH 0
832#define SE_ORIG 1
617b465c
ZD
833 unsigned i, j, n;
834 int is_latch = (latch == e->src);
835 int scale_act = 0, *scale_step = NULL, scale_main = 0;
836 int p, freq_in, freq_le, freq_out_orig;
837 int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
838 int add_irreducible_flag;
b9a66240 839 basic_block place_after;
617b465c 840
341c100f
NS
841 gcc_assert (e->dest == loop->header);
842 gcc_assert (ndupl > 0);
617b465c
ZD
843
844 if (orig)
845 {
846 /* Orig must be edge out of the loop. */
341c100f
NS
847 gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
848 gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
617b465c
ZD
849 }
850
b9a66240
ZD
851 n = loop->num_nodes;
852 bbs = get_loop_body_in_dom_order (loop);
853 gcc_assert (bbs[0] == loop->header);
854 gcc_assert (bbs[n - 1] == loop->latch);
617b465c
ZD
855
856 /* Check whether duplication is possible. */
8d28e87d 857 if (!can_copy_bbs_p (bbs, loop->num_nodes))
617b465c 858 {
8d28e87d
ZD
859 free (bbs);
860 return false;
617b465c 861 }
5ed6ace5 862 new_bbs = XNEWVEC (basic_block, loop->num_nodes);
617b465c 863
8d28e87d
ZD
864 /* In case we are doing loop peeling and the loop is in the middle of
865 irreducible region, the peeled copies will be inside it too. */
866 add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
341c100f 867 gcc_assert (!is_latch || !add_irreducible_flag);
617b465c
ZD
868
869 /* Find edge from latch. */
870 latch_edge = loop_latch_edge (loop);
871
872 if (flags & DLTHE_FLAG_UPDATE_FREQ)
873 {
874 /* Calculate coefficients by that we have to scale frequencies
875 of duplicated loop bodies. */
876 freq_in = header->frequency;
877 freq_le = EDGE_FREQUENCY (latch_edge);
878 if (freq_in == 0)
879 freq_in = 1;
880 if (freq_in < freq_le)
881 freq_in = freq_le;
882 freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
883 if (freq_out_orig > freq_in - freq_le)
884 freq_out_orig = freq_in - freq_le;
885 prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
886 prob_pass_wont_exit =
887 RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
888
5ed6ace5 889 scale_step = XNEWVEC (int, ndupl);
617b465c
ZD
890
891 for (i = 1; i <= ndupl; i++)
d329e058 892 scale_step[i - 1] = TEST_BIT (wont_exit, i)
617b465c
ZD
893 ? prob_pass_wont_exit
894 : prob_pass_thru;
895
a4d05547 896 /* Complete peeling is special as the probability of exit in last
c22cacf3 897 copy becomes 1. */
178df94f
JH
898 if (flags & DLTHE_FLAG_COMPLETTE_PEEL)
899 {
900 int wanted_freq = EDGE_FREQUENCY (e);
901
902 if (wanted_freq > freq_in)
903 wanted_freq = freq_in;
904
905 gcc_assert (!is_latch);
a4d05547 906 /* First copy has frequency of incoming edge. Each subsequent
178df94f
JH
907 frequency should be reduced by prob_pass_wont_exit. Caller
908 should've managed the flags so all except for original loop
909 has won't exist set. */
910 scale_act = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
911 /* Now simulate the duplication adjustments and compute header
912 frequency of the last copy. */
913 for (i = 0; i < ndupl; i++)
c22cacf3 914 wanted_freq = RDIV (wanted_freq * scale_step[i], REG_BR_PROB_BASE);
178df94f
JH
915 scale_main = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
916 }
917 else if (is_latch)
617b465c
ZD
918 {
919 prob_pass_main = TEST_BIT (wont_exit, 0)
920 ? prob_pass_wont_exit
921 : prob_pass_thru;
922 p = prob_pass_main;
923 scale_main = REG_BR_PROB_BASE;
924 for (i = 0; i < ndupl; i++)
925 {
926 scale_main += p;
927 p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
928 }
929 scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
930 scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
931 }
932 else
933 {
934 scale_main = REG_BR_PROB_BASE;
935 for (i = 0; i < ndupl; i++)
936 scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
937 scale_act = REG_BR_PROB_BASE - prob_pass_thru;
938 }
939 for (i = 0; i < ndupl; i++)
341c100f
NS
940 gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
941 gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
942 && scale_act >= 0 && scale_act <= REG_BR_PROB_BASE);
617b465c
ZD
943 }
944
945 /* Loop the new bbs will belong to. */
8d28e87d 946 target = e->src->loop_father;
617b465c
ZD
947
948 /* Original loops. */
949 n_orig_loops = 0;
950 for (aloop = loop->inner; aloop; aloop = aloop->next)
951 n_orig_loops++;
5ed6ace5 952 orig_loops = XCNEWVEC (struct loop *, n_orig_loops);
617b465c
ZD
953 for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
954 orig_loops[i] = aloop;
955
956 loop->copy = target;
d329e058 957
5ed6ace5 958 first_active = XNEWVEC (basic_block, n);
617b465c
ZD
959 if (is_latch)
960 {
961 memcpy (first_active, bbs, n * sizeof (basic_block));
962 first_active_latch = latch;
963 }
964
82b85a85
ZD
965 /* Update the information about single exits. */
966 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
967 update_single_exits_after_duplication (bbs, n, target);
968
8d28e87d
ZD
969 /* Record exit edge in original loop body. */
970 if (orig && TEST_BIT (wont_exit, 0))
971 to_remove[(*n_to_remove)++] = orig;
972
973 spec_edges[SE_ORIG] = orig;
974 spec_edges[SE_LATCH] = latch_edge;
d329e058 975
b9a66240 976 place_after = e->src;
617b465c
ZD
977 for (j = 0; j < ndupl; j++)
978 {
979 /* Copy loops. */
980 copy_loops_to (loops, orig_loops, n_orig_loops, target);
981
982 /* Copy bbs. */
b9a66240
ZD
983 copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop,
984 place_after);
985 place_after = new_spec_edges[SE_LATCH]->src;
8d28e87d 986
c0493b13
ZD
987 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
988 {
989 for (i = 0; i < n; i++)
990 bbs[i]->flags |= BB_DUPLICATED;
991 update_single_exit_for_duplicated_loops (orig_loops, n_orig_loops);
992 for (i = 0; i < n; i++)
993 bbs[i]->flags &= ~BB_DUPLICATED;
994 }
995
7f7b1718
JH
996 if (flags & DLTHE_RECORD_COPY_NUMBER)
997 for (i = 0; i < n; i++)
998 {
999 gcc_assert (!new_bbs[i]->aux);
1000 new_bbs[i]->aux = (void *)(size_t)(j + 1);
1001 }
113d659a 1002
84d45ad1
ZD
1003 /* Note whether the blocks and edges belong to an irreducible loop. */
1004 if (add_irreducible_flag)
1005 {
1006 for (i = 0; i < n; i++)
6580ee77 1007 new_bbs[i]->flags |= BB_DUPLICATED;
84d45ad1
ZD
1008 for (i = 0; i < n; i++)
1009 {
628f6a4e 1010 edge_iterator ei;
84d45ad1
ZD
1011 new_bb = new_bbs[i];
1012 if (new_bb->loop_father == target)
1013 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
1014
628f6a4e 1015 FOR_EACH_EDGE (ae, ei, new_bb->succs)
6580ee77 1016 if ((ae->dest->flags & BB_DUPLICATED)
84d45ad1
ZD
1017 && (ae->src->loop_father == target
1018 || ae->dest->loop_father == target))
1019 ae->flags |= EDGE_IRREDUCIBLE_LOOP;
1020 }
1021 for (i = 0; i < n; i++)
6580ee77 1022 new_bbs[i]->flags &= ~BB_DUPLICATED;
84d45ad1
ZD
1023 }
1024
8d28e87d 1025 /* Redirect the special edges. */
617b465c 1026 if (is_latch)
8d28e87d
ZD
1027 {
1028 redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
1029 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1030 loop->header);
d47cc544 1031 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
b9a66240 1032 latch = loop->latch = new_bbs[n - 1];
8d28e87d
ZD
1033 e = latch_edge = new_spec_edges[SE_LATCH];
1034 }
1035 else
1036 {
1037 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
1038 loop->header);
1039 redirect_edge_and_branch_force (e, new_bbs[0]);
d47cc544 1040 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
8d28e87d
ZD
1041 e = new_spec_edges[SE_LATCH];
1042 }
617b465c 1043
8d28e87d
ZD
1044 /* Record exit edge in this copy. */
1045 if (orig && TEST_BIT (wont_exit, j + 1))
1046 to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
d329e058 1047
8d28e87d
ZD
1048 /* Record the first copy in the control flow order if it is not
1049 the original loop (i.e. in case of peeling). */
617b465c
ZD
1050 if (!first_active_latch)
1051 {
1052 memcpy (first_active, new_bbs, n * sizeof (basic_block));
b9a66240 1053 first_active_latch = new_bbs[n - 1];
617b465c 1054 }
d329e058 1055
8d28e87d
ZD
1056 /* Set counts and frequencies. */
1057 if (flags & DLTHE_FLAG_UPDATE_FREQ)
617b465c 1058 {
33156717 1059 scale_bbs_frequencies_int (new_bbs, n, scale_act, REG_BR_PROB_BASE);
8d28e87d 1060 scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
617b465c
ZD
1061 }
1062 }
8d28e87d
ZD
1063 free (new_bbs);
1064 free (orig_loops);
c22cacf3 1065
8d28e87d
ZD
1066 /* Update the original loop. */
1067 if (!is_latch)
d47cc544 1068 set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
617b465c
ZD
1069 if (flags & DLTHE_FLAG_UPDATE_FREQ)
1070 {
33156717 1071 scale_bbs_frequencies_int (bbs, n, scale_main, REG_BR_PROB_BASE);
617b465c
ZD
1072 free (scale_step);
1073 }
617b465c 1074
8d28e87d 1075 /* Update dominators of outer blocks if affected. */
617b465c
ZD
1076 for (i = 0; i < n; i++)
1077 {
1078 basic_block dominated, dom_bb, *dom_bbs;
1079 int n_dom_bbs,j;
1080
1081 bb = bbs[i];
6580ee77 1082 bb->aux = 0;
113d659a 1083
d47cc544 1084 n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
617b465c
ZD
1085 for (j = 0; j < n_dom_bbs; j++)
1086 {
1087 dominated = dom_bbs[j];
1088 if (flow_bb_inside_loop_p (loop, dominated))
1089 continue;
1090 dom_bb = nearest_common_dominator (
d47cc544 1091 CDI_DOMINATORS, first_active[i], first_active_latch);
c22cacf3 1092 set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
617b465c
ZD
1093 }
1094 free (dom_bbs);
1095 }
1096 free (first_active);
1097
1098 free (bbs);
1099
1100 return true;
1101}
1102
f470c378
ZD
1103/* A callback for make_forwarder block, to redirect all edges except for
1104 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
1105 whether to redirect it. */
1106
1107static edge mfb_kj_edge;
1108static bool
1109mfb_keep_just (edge e)
1110{
1111 return e != mfb_kj_edge;
1112}
1113
1114/* A callback for make_forwarder block, to update data structures for a basic
1115 block JUMP created by redirecting an edge (only the latch edge is being
1116 redirected). */
1117
1118static void
1119mfb_update_loops (basic_block jump)
1120{
c5cbcccf 1121 struct loop *loop = single_succ (jump)->loop_father;
f470c378
ZD
1122
1123 if (dom_computed[CDI_DOMINATORS])
c5cbcccf 1124 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
f470c378
ZD
1125 add_bb_to_loop (jump, loop);
1126 loop->latch = jump;
1127}
1128
3d436d2a
ZD
1129/* Creates a pre-header for a LOOP. Returns newly created block. Unless
1130 CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1131 entry; otherwise we also force preheader block to have only one successor.
f470c378
ZD
1132 The function also updates dominators. */
1133
3d436d2a 1134static basic_block
d47cc544 1135create_preheader (struct loop *loop, int flags)
3d436d2a
ZD
1136{
1137 edge e, fallthru;
1138 basic_block dummy;
3d436d2a
ZD
1139 struct loop *cloop, *ploop;
1140 int nentry = 0;
f470c378 1141 bool irred = false;
c15bc84b
EB
1142 bool latch_edge_was_fallthru;
1143 edge one_succ_pred = 0;
628f6a4e 1144 edge_iterator ei;
3d436d2a
ZD
1145
1146 cloop = loop->outer;
1147
628f6a4e 1148 FOR_EACH_EDGE (e, ei, loop->header->preds)
3d436d2a
ZD
1149 {
1150 if (e->src == loop->latch)
1151 continue;
f470c378 1152 irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
3d436d2a 1153 nentry++;
c5cbcccf 1154 if (single_succ_p (e->src))
c15bc84b 1155 one_succ_pred = e;
3d436d2a 1156 }
341c100f 1157 gcc_assert (nentry);
3d436d2a
ZD
1158 if (nentry == 1)
1159 {
b057216d
KH
1160 /* Get an edge that is different from the one from loop->latch
1161 to loop->header. */
1162 e = EDGE_PRED (loop->header,
1163 EDGE_PRED (loop->header, 0)->src == loop->latch);
628f6a4e 1164
c5cbcccf 1165 if (!(flags & CP_SIMPLE_PREHEADERS) || single_succ_p (e->src))
3d436d2a
ZD
1166 return NULL;
1167 }
1168
f470c378 1169 mfb_kj_edge = loop_latch_edge (loop);
c15bc84b 1170 latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
f470c378
ZD
1171 fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1172 mfb_update_loops);
3d436d2a
ZD
1173 dummy = fallthru->src;
1174 loop->header = fallthru->dest;
1175
1176 /* The header could be a latch of some superloop(s); due to design of
1177 split_block, it would now move to fallthru->dest. */
1178 for (ploop = loop; ploop; ploop = ploop->outer)
1179 if (ploop->latch == dummy)
1180 ploop->latch = fallthru->dest;
1181
c15bc84b
EB
1182 /* Try to be clever in placing the newly created preheader. The idea is to
1183 avoid breaking any "fallthruness" relationship between blocks.
1184
1185 The preheader was created just before the header and all incoming edges
1186 to the header were redirected to the preheader, except the latch edge.
1187 So the only problematic case is when this latch edge was a fallthru
1188 edge: it is not anymore after the preheader creation so we have broken
1189 the fallthruness. We're therefore going to look for a better place. */
1190 if (latch_edge_was_fallthru)
1191 {
1192 if (one_succ_pred)
1193 e = one_succ_pred;
1194 else
1195 e = EDGE_PRED (dummy, 0);
1196
1197 move_block_after (dummy, e->src);
1198 }
f470c378
ZD
1199
1200 loop->header->loop_father = loop;
1201 add_bb_to_loop (dummy, cloop);
3d436d2a 1202
f470c378 1203 if (irred)
3d436d2a 1204 {
f470c378 1205 dummy->flags |= BB_IRREDUCIBLE_LOOP;
c5cbcccf 1206 single_succ_edge (dummy)->flags |= EDGE_IRREDUCIBLE_LOOP;
3d436d2a
ZD
1207 }
1208
c263766c
RH
1209 if (dump_file)
1210 fprintf (dump_file, "Created preheader block for loop %i\n",
3d436d2a
ZD
1211 loop->num);
1212
1213 return dummy;
1214}
1215
617b465c
ZD
1216/* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1217 of FLAGS see create_preheader. */
3d436d2a 1218void
d329e058 1219create_preheaders (struct loops *loops, int flags)
3d436d2a
ZD
1220{
1221 unsigned i;
1222 for (i = 1; i < loops->num; i++)
d47cc544 1223 create_preheader (loops->parray[i], flags);
3d436d2a
ZD
1224 loops->state |= LOOPS_HAVE_PREHEADERS;
1225}
1226
617b465c
ZD
1227/* Forces all loop latches of loops from loop tree LOOPS to have only single
1228 successor. */
3d436d2a 1229void
d329e058 1230force_single_succ_latches (struct loops *loops)
3d436d2a
ZD
1231{
1232 unsigned i;
1233 struct loop *loop;
1234 edge e;
1235
1236 for (i = 1; i < loops->num; i++)
1237 {
1238 loop = loops->parray[i];
c5cbcccf 1239 if (loop->latch != loop->header && single_succ_p (loop->latch))
3d436d2a 1240 continue;
d329e058 1241
9ff3d2de 1242 e = find_edge (loop->latch, loop->header);
bc810602 1243
d47cc544 1244 loop_split_edge_with (e, NULL_RTX);
3d436d2a
ZD
1245 }
1246 loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1247}
1248
617b465c
ZD
1249/* A quite stupid function to put INSNS on edge E. They are supposed to form
1250 just one basic block. Jumps in INSNS are not handled, so cfg do not have to
1251 be ok after this function. The created block is placed on correct place
1252 in LOOPS structure and its dominator is set. */
3d436d2a 1253basic_block
d47cc544 1254loop_split_edge_with (edge e, rtx insns)
3d436d2a
ZD
1255{
1256 basic_block src, dest, new_bb;
1257 struct loop *loop_c;
d329e058 1258
3d436d2a
ZD
1259 src = e->src;
1260 dest = e->dest;
1261
1262 loop_c = find_common_loop (src->loop_father, dest->loop_father);
1263
1264 /* Create basic block for it. */
1265
bc35512f 1266 new_bb = split_edge (e);
3d436d2a 1267 add_bb_to_loop (new_bb, loop_c);
a746fd8c 1268 new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
3d436d2a 1269
3d436d2a 1270 if (insns)
a813c111 1271 emit_insn_after (insns, BB_END (new_bb));
3d436d2a 1272
3d436d2a
ZD
1273 if (dest->loop_father->latch == src)
1274 dest->loop_father->latch = new_bb;
d329e058 1275
3d436d2a
ZD
1276 return new_bb;
1277}
6de9cd9a 1278
1cb7dfc3
MH
1279/* This function is called from loop_version. It splits the entry edge
1280 of the loop we want to version, adds the versioning condition, and
1281 adjust the edges to the two versions of the loop appropriately.
1282 e is an incoming edge. Returns the basic block containing the
1283 condition.
1284
1285 --- edge e ---- > [second_head]
1286
1287 Split it and insert new conditional expression and adjust edges.
1288
1289 --- edge e ---> [cond expr] ---> [first_head]
c22cacf3
MS
1290 |
1291 +---------> [second_head]
1cb7dfc3
MH
1292*/
1293
1294static basic_block
1295lv_adjust_loop_entry_edge (basic_block first_head,
1296 basic_block second_head,
1297 edge e,
ee39680c 1298 void *cond_expr)
1cb7dfc3
MH
1299{
1300 basic_block new_head = NULL;
1301 edge e1;
1302
1303 gcc_assert (e->dest == second_head);
1304
1305 /* Split edge 'e'. This will create a new basic block, where we can
1306 insert conditional expr. */
1307 new_head = split_edge (e);
1308
1309
1310 lv_add_condition_to_bb (first_head, second_head, new_head,
1311 cond_expr);
1312
766613a4 1313 /* Don't set EDGE_TRUE_VALUE in RTL mode, as it's invalid there. */
52bca999
SB
1314 e1 = make_edge (new_head, first_head,
1315 current_ir_type () == IR_GIMPLE ? EDGE_TRUE_VALUE : 0);
1cb7dfc3
MH
1316 set_immediate_dominator (CDI_DOMINATORS, first_head, new_head);
1317 set_immediate_dominator (CDI_DOMINATORS, second_head, new_head);
1318
1319 /* Adjust loop header phi nodes. */
1320 lv_adjust_loop_header_phi (first_head, second_head, new_head, e1);
1321
1322 return new_head;
1323}
1324
1325/* Main entry point for Loop Versioning transformation.
c22cacf3 1326
b9a66240
ZD
1327 This transformation given a condition and a loop, creates
1328 -if (condition) { loop_copy1 } else { loop_copy2 },
1329 where loop_copy1 is the loop transformed in one way, and loop_copy2
1330 is the loop transformed in another way (or unchanged). 'condition'
1331 may be a run time test for things that were not resolved by static
1332 analysis (overlapping ranges (anti-aliasing), alignment, etc.).
1333
1334 If PLACE_AFTER is true, we place the new loop after LOOP in the
1335 instruction stream, otherwise it is placed before LOOP. */
1cb7dfc3
MH
1336
1337struct loop *
c22cacf3 1338loop_version (struct loops *loops, struct loop * loop,
b9a66240
ZD
1339 void *cond_expr, basic_block *condition_bb,
1340 bool place_after)
1cb7dfc3
MH
1341{
1342 basic_block first_head, second_head;
1343 edge entry, latch_edge, exit, true_edge, false_edge;
1344 int irred_flag;
1345 struct loop *nloop;
b9a66240 1346 basic_block cond_bb;
1cb7dfc3
MH
1347
1348 /* CHECKME: Loop versioning does not handle nested loop at this point. */
1349 if (loop->inner)
1350 return NULL;
1351
1352 /* Record entry and latch edges for the loop */
1353 entry = loop_preheader_edge (loop);
1354 irred_flag = entry->flags & EDGE_IRREDUCIBLE_LOOP;
1355 entry->flags &= ~EDGE_IRREDUCIBLE_LOOP;
c22cacf3 1356
1cb7dfc3
MH
1357 /* Note down head of loop as first_head. */
1358 first_head = entry->dest;
1359
1360 /* Duplicate loop. */
1361 if (!cfg_hook_duplicate_loop_to_header_edge (loop, entry, loops, 1,
c22cacf3 1362 NULL, NULL, NULL, NULL, 0))
1cb7dfc3
MH
1363 return NULL;
1364
1365 /* After duplication entry edge now points to new loop head block.
1366 Note down new head as second_head. */
1367 second_head = entry->dest;
1368
1369 /* Split loop entry edge and insert new block with cond expr. */
b9a66240
ZD
1370 cond_bb = lv_adjust_loop_entry_edge (first_head, second_head,
1371 entry, cond_expr);
1372 if (condition_bb)
1373 *condition_bb = cond_bb;
1374
1375 if (!cond_bb)
1cb7dfc3
MH
1376 {
1377 entry->flags |= irred_flag;
1378 return NULL;
1379 }
1380
6580ee77 1381 latch_edge = single_succ_edge (get_bb_copy (loop->latch));
c22cacf3 1382
b9a66240 1383 extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
1cb7dfc3
MH
1384 nloop = loopify (loops,
1385 latch_edge,
6580ee77 1386 single_pred_edge (get_bb_copy (loop->header)),
b9a66240 1387 cond_bb, true_edge, false_edge,
1cb7dfc3
MH
1388 false /* Do not redirect all edges. */);
1389
1390 exit = loop->single_exit;
1391 if (exit)
6580ee77 1392 nloop->single_exit = find_edge (get_bb_copy (exit->src), exit->dest);
1cb7dfc3 1393
c22cacf3 1394 /* loopify redirected latch_edge. Update its PENDING_STMTS. */
1cb7dfc3
MH
1395 lv_flush_pending_stmts (latch_edge);
1396
c22cacf3 1397 /* loopify redirected condition_bb's succ edge. Update its PENDING_STMTS. */
b9a66240 1398 extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
1cb7dfc3
MH
1399 lv_flush_pending_stmts (false_edge);
1400 /* Adjust irreducible flag. */
1401 if (irred_flag)
1402 {
b9a66240 1403 cond_bb->flags |= BB_IRREDUCIBLE_LOOP;
1cb7dfc3
MH
1404 loop_preheader_edge (loop)->flags |= EDGE_IRREDUCIBLE_LOOP;
1405 loop_preheader_edge (nloop)->flags |= EDGE_IRREDUCIBLE_LOOP;
b9a66240
ZD
1406 single_pred_edge (cond_bb)->flags |= EDGE_IRREDUCIBLE_LOOP;
1407 }
1408
1409 if (place_after)
1410 {
1411 basic_block *bbs = get_loop_body_in_dom_order (nloop), after;
1412 unsigned i;
1413
1414 after = loop->latch;
1415
1416 for (i = 0; i < nloop->num_nodes; i++)
1417 {
1418 move_block_after (bbs[i], after);
1419 after = bbs[i];
1420 }
1421 free (bbs);
1cb7dfc3
MH
1422 }
1423
c22cacf3
MS
1424 /* At this point condition_bb is loop predheader with two successors,
1425 first_head and second_head. Make sure that loop predheader has only
1cb7dfc3
MH
1426 one successor. */
1427 loop_split_edge_with (loop_preheader_edge (loop), NULL);
1428 loop_split_edge_with (loop_preheader_edge (nloop), NULL);
1429
1430 return nloop;
1431}
1432
2b271002
ZD
1433/* The structure of LOOPS might have changed. Some loops might get removed
1434 (and their headers and latches were set to NULL), loop exists might get
1435 removed (thus the loop nesting may be wrong), and some blocks and edges
1436 were changed (so the information about bb --> loop mapping does not have
1437 to be correct). But still for the remaining loops the header dominates
1438 the latch, and loops did not get new subloobs (new loops might possibly
1439 get created, but we are not interested in them). Fix up the mess.
c22cacf3 1440
2b271002
ZD
1441 If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
1442 marked in it. */
1443
1444void
1445fix_loop_structure (struct loops *loops, bitmap changed_bbs)
1446{
1447 basic_block bb;
1448 struct loop *loop, *ploop;
1449 unsigned i;
1450
1451 /* Remove the old bb -> loop mapping. */
1452 FOR_EACH_BB (bb)
1453 {
1454 bb->aux = (void *) (size_t) bb->loop_father->depth;
1455 bb->loop_father = loops->tree_root;
1456 }
1457
1458 /* Remove the dead loops from structures. */
c22cacf3 1459 loops->tree_root->num_nodes = n_basic_blocks;
2b271002
ZD
1460 for (i = 1; i < loops->num; i++)
1461 {
1462 loop = loops->parray[i];
1463 if (!loop)
1464 continue;
1465
1466 loop->num_nodes = 0;
1467 if (loop->header)
1468 continue;
1469
1470 while (loop->inner)
1471 {
1472 ploop = loop->inner;
1473 flow_loop_tree_node_remove (ploop);
1474 flow_loop_tree_node_add (loop->outer, ploop);
1475 }
1476
1477 /* Remove the loop and free its data. */
1478 flow_loop_tree_node_remove (loop);
1479 loops->parray[loop->num] = NULL;
1480 flow_loop_free (loop);
1481 }
1482
1483 /* Rescan the bodies of loops, starting from the outermost. */
1484 loop = loops->tree_root;
1485 while (1)
1486 {
1487 if (loop->inner)
1488 loop = loop->inner;
1489 else
1490 {
1491 while (!loop->next
1492 && loop != loops->tree_root)
1493 loop = loop->outer;
1494 if (loop == loops->tree_root)
1495 break;
1496
1497 loop = loop->next;
1498 }
1499
1500 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
1501 }
1502
1503 /* Now fix the loop nesting. */
1504 for (i = 1; i < loops->num; i++)
1505 {
1506 loop = loops->parray[i];
1507 if (!loop)
1508 continue;
1509
1510 bb = loop_preheader_edge (loop)->src;
1511 if (bb->loop_father != loop->outer)
1512 {
1513 flow_loop_tree_node_remove (loop);
1514 flow_loop_tree_node_add (bb->loop_father, loop);
1515 }
1516 }
1517
1518 /* Mark the blocks whose loop has changed. */
1519 FOR_EACH_BB (bb)
1520 {
1521 if (changed_bbs
1522 && (void *) (size_t) bb->loop_father->depth != bb->aux)
1523 bitmap_set_bit (changed_bbs, bb->index);
1524
1525 bb->aux = NULL;
1526 }
1527
d78f3f78
ZD
1528 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1529 mark_single_exit_loops (loops);
1530 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1531 mark_irreducible_loops (loops);
2b271002 1532}
This page took 1.071176 seconds and 5 git commands to generate.