00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "rtl.h"
00026 #include "hard-reg-set.h"
00027 #include "obstack.h"
00028 #include "basic-block.h"
00029 #include "cfgloop.h"
00030 #include "cfglayout.h"
00031 #include "cfghooks.h"
00032 #include "output.h"
00033
00034 static void duplicate_subloops (struct loops *, struct loop *, struct loop *);
00035 static void copy_loops_to (struct loops *, struct loop **, int,
00036 struct loop *);
00037 static void loop_redirect_edge (edge, basic_block);
00038 static bool loop_delete_branch_edge (edge, int);
00039 static void remove_bbs (basic_block *, int);
00040 static bool rpe_enum_p (basic_block, void *);
00041 static int find_path (edge, basic_block **);
00042 static bool alp_enum_p (basic_block, void *);
00043 static void add_loop (struct loops *, struct loop *);
00044 static void fix_loop_placements (struct loops *, struct loop *, bool *);
00045 static bool fix_bb_placement (struct loops *, basic_block);
00046 static void fix_bb_placements (struct loops *, basic_block, bool *);
00047 static void place_new_loop (struct loops *, struct loop *);
00048 static void scale_loop_frequencies (struct loop *, int, int);
00049 static basic_block create_preheader (struct loop *, int);
00050 static void unloop (struct loops *, struct loop *, bool *);
00051
00052 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
00053
00054
00055 static bool
00056 rpe_enum_p (basic_block bb, void *data)
00057 {
00058 return dominated_by_p (CDI_DOMINATORS, bb, data);
00059 }
00060
00061
00062
00063 static void
00064 remove_bbs (basic_block *bbs, int nbbs)
00065 {
00066 int i;
00067
00068 for (i = 0; i < nbbs; i++)
00069 {
00070 remove_bb_from_loops (bbs[i]);
00071 delete_basic_block (bbs[i]);
00072 }
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 static int
00082 find_path (edge e, basic_block **bbs)
00083 {
00084 gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
00085
00086
00087 *bbs = XCNEWVEC (basic_block, n_basic_blocks);
00088 return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs,
00089 n_basic_blocks, e->dest);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099 static bool
00100 fix_bb_placement (struct loops *loops, basic_block bb)
00101 {
00102 edge e;
00103 edge_iterator ei;
00104 struct loop *loop = loops->tree_root, *act;
00105
00106 FOR_EACH_EDGE (e, ei, bb->succs)
00107 {
00108 if (e->dest == EXIT_BLOCK_PTR)
00109 continue;
00110
00111 act = e->dest->loop_father;
00112 if (act->header == e->dest)
00113 act = act->outer;
00114
00115 if (flow_loop_nested_p (loop, act))
00116 loop = act;
00117 }
00118
00119 if (loop == bb->loop_father)
00120 return false;
00121
00122 remove_bb_from_loops (bb);
00123 add_bb_to_loop (bb, loop);
00124
00125 return true;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 static void
00141 fix_bb_placements (struct loops *loops, basic_block from,
00142 bool *irred_invalidated)
00143 {
00144 sbitmap in_queue;
00145 basic_block *queue, *qtop, *qbeg, *qend;
00146 struct loop *base_loop;
00147 edge e;
00148
00149
00150
00151
00152
00153
00154
00155
00156 base_loop = from->loop_father;
00157 if (base_loop == loops->tree_root)
00158 return;
00159
00160 in_queue = sbitmap_alloc (last_basic_block);
00161 sbitmap_zero (in_queue);
00162 SET_BIT (in_queue, from->index);
00163
00164 SET_BIT (in_queue, base_loop->header->index);
00165
00166 queue = XNEWVEC (basic_block, base_loop->num_nodes + 1);
00167 qtop = queue + base_loop->num_nodes + 1;
00168 qbeg = queue;
00169 qend = queue + 1;
00170 *qbeg = from;
00171
00172 while (qbeg != qend)
00173 {
00174 edge_iterator ei;
00175 from = *qbeg;
00176 qbeg++;
00177 if (qbeg == qtop)
00178 qbeg = queue;
00179 RESET_BIT (in_queue, from->index);
00180
00181 if (from->loop_father->header == from)
00182 {
00183
00184 if (!fix_loop_placement (from->loop_father))
00185 continue;
00186 }
00187 else
00188 {
00189
00190 if (!fix_bb_placement (loops, from))
00191 continue;
00192 }
00193
00194 FOR_EACH_EDGE (e, ei, from->succs)
00195 {
00196 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
00197 *irred_invalidated = true;
00198 }
00199
00200
00201 FOR_EACH_EDGE (e, ei, from->preds)
00202 {
00203 basic_block pred = e->src;
00204 struct loop *nca;
00205
00206 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
00207 *irred_invalidated = true;
00208
00209 if (TEST_BIT (in_queue, pred->index))
00210 continue;
00211
00212
00213
00214
00215 nca = find_common_loop (pred->loop_father, base_loop);
00216 if (pred->loop_father != base_loop
00217 && (nca == base_loop
00218 || nca != pred->loop_father))
00219 pred = pred->loop_father->header;
00220 else if (!flow_loop_nested_p (from->loop_father, pred->loop_father))
00221 {
00222
00223 continue;
00224 }
00225
00226 if (TEST_BIT (in_queue, pred->index))
00227 continue;
00228
00229
00230 *qend = pred;
00231 qend++;
00232 if (qend == qtop)
00233 qend = queue;
00234 SET_BIT (in_queue, pred->index);
00235 }
00236 }
00237 free (in_queue);
00238 free (queue);
00239 }
00240
00241
00242
00243
00244
00245 bool
00246 remove_path (struct loops *loops, edge e)
00247 {
00248 edge ae;
00249 basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb;
00250 int i, nrem, n_bord_bbs, n_dom_bbs;
00251 sbitmap seen;
00252 bool deleted, irred_invalidated = false;
00253
00254 if (!loop_delete_branch_edge (e, 0))
00255 return false;
00256
00257
00258
00259
00260
00261
00262 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
00263 irred_invalidated = true;
00264
00265
00266
00267
00268
00269 if (!single_pred_p (e->dest))
00270 e = single_pred_edge (loop_split_edge_with (e, NULL_RTX));
00271
00272
00273
00274
00275
00276 while (e->src->loop_father->outer
00277 && dominated_by_p (CDI_DOMINATORS,
00278 e->src->loop_father->latch, e->dest))
00279 unloop (loops, e->src->loop_father, &irred_invalidated);
00280
00281
00282 nrem = find_path (e, &rem_bbs);
00283
00284 n_bord_bbs = 0;
00285 bord_bbs = XCNEWVEC (basic_block, n_basic_blocks);
00286 seen = sbitmap_alloc (last_basic_block);
00287 sbitmap_zero (seen);
00288
00289
00290 for (i = 0; i < nrem; i++)
00291 SET_BIT (seen, rem_bbs[i]->index);
00292 for (i = 0; i < nrem; i++)
00293 {
00294 edge_iterator ei;
00295 bb = rem_bbs[i];
00296 FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
00297 if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
00298 {
00299 SET_BIT (seen, ae->dest->index);
00300 bord_bbs[n_bord_bbs++] = ae->dest;
00301
00302 if (ae->flags & EDGE_IRREDUCIBLE_LOOP)
00303 irred_invalidated = true;
00304 }
00305 }
00306
00307
00308 from = e->src;
00309 deleted = loop_delete_branch_edge (e, 1);
00310 gcc_assert (deleted);
00311 dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
00312
00313
00314 for (i = 0; i < nrem; i++)
00315 if (rem_bbs[i]->loop_father->header == rem_bbs[i])
00316 cancel_loop_tree (loops, rem_bbs[i]->loop_father);
00317
00318 remove_bbs (rem_bbs, nrem);
00319 free (rem_bbs);
00320
00321
00322 n_dom_bbs = 0;
00323 sbitmap_zero (seen);
00324 for (i = 0; i < n_bord_bbs; i++)
00325 {
00326 basic_block ldom;
00327
00328 bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
00329 if (TEST_BIT (seen, bb->index))
00330 continue;
00331 SET_BIT (seen, bb->index);
00332
00333 for (ldom = first_dom_son (CDI_DOMINATORS, bb);
00334 ldom;
00335 ldom = next_dom_son (CDI_DOMINATORS, ldom))
00336 if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
00337 dom_bbs[n_dom_bbs++] = ldom;
00338 }
00339
00340 free (seen);
00341
00342
00343 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
00344 free (dom_bbs);
00345 free (bord_bbs);
00346
00347
00348
00349 fix_bb_placements (loops, from, &irred_invalidated);
00350 fix_loop_placements (loops, from->loop_father, &irred_invalidated);
00351
00352 if (irred_invalidated
00353 && (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) != 0)
00354 mark_irreducible_loops (loops);
00355
00356 return true;
00357 }
00358
00359
00360 static bool
00361 alp_enum_p (basic_block bb, void *alp_header)
00362 {
00363 return bb != (basic_block) alp_header;
00364 }
00365
00366
00367
00368 static void
00369 add_loop (struct loops *loops, struct loop *loop)
00370 {
00371 basic_block *bbs;
00372 int i, n;
00373
00374
00375 place_new_loop (loops, loop);
00376 loop->level = 1;
00377
00378
00379 bbs = XCNEWVEC (basic_block, n_basic_blocks);
00380 n = dfs_enumerate_from (loop->latch, 1, alp_enum_p,
00381 bbs, n_basic_blocks, loop->header);
00382
00383 for (i = 0; i < n; i++)
00384 add_bb_to_loop (bbs[i], loop);
00385 add_bb_to_loop (loop->header, loop);
00386
00387 free (bbs);
00388 }
00389
00390
00391 static void
00392 scale_loop_frequencies (struct loop *loop, int num, int den)
00393 {
00394 basic_block *bbs;
00395
00396 bbs = get_loop_body (loop);
00397 scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
00398 free (bbs);
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 struct loop *
00411 loopify (struct loops *loops, edge latch_edge, edge header_edge,
00412 basic_block switch_bb, edge true_edge, edge false_edge,
00413 bool redirect_all_edges)
00414 {
00415 basic_block succ_bb = latch_edge->dest;
00416 basic_block pred_bb = header_edge->src;
00417 basic_block *dom_bbs, *body;
00418 unsigned n_dom_bbs, i;
00419 sbitmap seen;
00420 struct loop *loop = XCNEW (struct loop);
00421 struct loop *outer = succ_bb->loop_father->outer;
00422 int freq, prob, tot_prob;
00423 gcov_type cnt;
00424 edge e;
00425 edge_iterator ei;
00426
00427 loop->header = header_edge->dest;
00428 loop->latch = latch_edge->src;
00429
00430 freq = EDGE_FREQUENCY (header_edge);
00431 cnt = header_edge->count;
00432 prob = EDGE_SUCC (switch_bb, 0)->probability;
00433 tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
00434 if (tot_prob == 0)
00435 tot_prob = 1;
00436
00437
00438 loop_redirect_edge (latch_edge, loop->header);
00439 loop_redirect_edge (true_edge, succ_bb);
00440
00441
00442
00443 if (redirect_all_edges)
00444 {
00445 loop_redirect_edge (header_edge, switch_bb);
00446 loop_redirect_edge (false_edge, loop->header);
00447
00448
00449 set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
00450 set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
00451 }
00452
00453 set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
00454
00455
00456 add_loop (loops, loop);
00457 flow_loop_tree_node_add (outer, loop);
00458
00459
00460 add_bb_to_loop (switch_bb, outer);
00461
00462
00463 switch_bb->frequency = freq;
00464 switch_bb->count = cnt;
00465 FOR_EACH_EDGE (e, ei, switch_bb->succs)
00466 e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
00467 scale_loop_frequencies (loop, prob, tot_prob);
00468 scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
00469
00470
00471 dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
00472 n_dom_bbs = 0;
00473 seen = sbitmap_alloc (last_basic_block);
00474 sbitmap_zero (seen);
00475 body = get_loop_body (loop);
00476
00477 for (i = 0; i < loop->num_nodes; i++)
00478 SET_BIT (seen, body[i]->index);
00479
00480 for (i = 0; i < loop->num_nodes; i++)
00481 {
00482 basic_block ldom;
00483
00484 for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
00485 ldom;
00486 ldom = next_dom_son (CDI_DOMINATORS, ldom))
00487 if (!TEST_BIT (seen, ldom->index))
00488 {
00489 SET_BIT (seen, ldom->index);
00490 dom_bbs[n_dom_bbs++] = ldom;
00491 }
00492 }
00493
00494 iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
00495
00496 free (body);
00497 free (seen);
00498 free (dom_bbs);
00499
00500 return loop;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510 static void
00511 unloop (struct loops *loops, struct loop *loop, bool *irred_invalidated)
00512 {
00513 basic_block *body;
00514 struct loop *ploop;
00515 unsigned i, n;
00516 basic_block latch = loop->latch;
00517 bool dummy = false;
00518
00519 if (loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
00520 *irred_invalidated = true;
00521
00522
00523
00524
00525
00526
00527
00528 body = get_loop_body (loop);
00529 n = loop->num_nodes;
00530 for (i = 0; i < n; i++)
00531 if (body[i]->loop_father == loop)
00532 {
00533 remove_bb_from_loops (body[i]);
00534 add_bb_to_loop (body[i], loop->outer);
00535 }
00536 free(body);
00537
00538 while (loop->inner)
00539 {
00540 ploop = loop->inner;
00541 flow_loop_tree_node_remove (ploop);
00542 flow_loop_tree_node_add (loop->outer, ploop);
00543 }
00544
00545
00546 flow_loop_tree_node_remove (loop);
00547 loops->parray[loop->num] = NULL;
00548 flow_loop_free (loop);
00549
00550 remove_edge (single_succ_edge (latch));
00551
00552
00553
00554
00555 fix_bb_placements (loops, latch, &dummy);
00556 }
00557
00558
00559
00560
00561
00562
00563 int
00564 fix_loop_placement (struct loop *loop)
00565 {
00566 basic_block *body;
00567 unsigned i;
00568 edge e;
00569 edge_iterator ei;
00570 struct loop *father = loop->pred[0], *act;
00571
00572 body = get_loop_body (loop);
00573 for (i = 0; i < loop->num_nodes; i++)
00574 FOR_EACH_EDGE (e, ei, body[i]->succs)
00575 if (!flow_bb_inside_loop_p (loop, e->dest))
00576 {
00577 act = find_common_loop (loop, e->dest->loop_father);
00578 if (flow_loop_nested_p (father, act))
00579 father = act;
00580 }
00581 free (body);
00582
00583 if (father != loop->outer)
00584 {
00585 for (act = loop->outer; act != father; act = act->outer)
00586 act->num_nodes -= loop->num_nodes;
00587 flow_loop_tree_node_remove (loop);
00588 flow_loop_tree_node_add (father, loop);
00589 return 1;
00590 }
00591 return 0;
00592 }
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602 static void
00603 fix_loop_placements (struct loops *loops, struct loop *loop,
00604 bool *irred_invalidated)
00605 {
00606 struct loop *outer;
00607
00608 while (loop->outer)
00609 {
00610 outer = loop->outer;
00611 if (!fix_loop_placement (loop))
00612 break;
00613
00614
00615
00616
00617
00618
00619 fix_bb_placements (loops, loop_preheader_edge (loop)->src,
00620 irred_invalidated);
00621 loop = outer;
00622 }
00623 }
00624
00625
00626 static void
00627 place_new_loop (struct loops *loops, struct loop *loop)
00628 {
00629 loops->parray =
00630 xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
00631 loops->parray[loops->num] = loop;
00632
00633 loop->num = loops->num++;
00634 }
00635
00636
00637
00638 struct loop *
00639 duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
00640 {
00641 struct loop *cloop;
00642 cloop = XCNEW (struct loop);
00643 place_new_loop (loops, cloop);
00644
00645
00646 cloop->level = loop->level;
00647
00648
00649 loop->copy = cloop;
00650
00651
00652 flow_loop_tree_node_add (target, cloop);
00653
00654 return cloop;
00655 }
00656
00657
00658
00659 static void
00660 duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
00661 {
00662 struct loop *aloop, *cloop;
00663
00664 for (aloop = loop->inner; aloop; aloop = aloop->next)
00665 {
00666 cloop = duplicate_loop (loops, aloop, target);
00667 duplicate_subloops (loops, aloop, cloop);
00668 }
00669 }
00670
00671
00672
00673 static void
00674 copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
00675 {
00676 struct loop *aloop;
00677 int i;
00678
00679 for (i = 0; i < n; i++)
00680 {
00681 aloop = duplicate_loop (loops, copied_loops[i], target);
00682 duplicate_subloops (loops, copied_loops[i], aloop);
00683 }
00684 }
00685
00686
00687 static void
00688 loop_redirect_edge (edge e, basic_block dest)
00689 {
00690 if (e->dest == dest)
00691 return;
00692
00693 redirect_edge_and_branch_force (e, dest);
00694 }
00695
00696
00697
00698 static bool
00699 loop_delete_branch_edge (edge e, int really_delete)
00700 {
00701 basic_block src = e->src;
00702 basic_block newdest;
00703 int irr;
00704 edge snd;
00705
00706 gcc_assert (EDGE_COUNT (src->succs) > 1);
00707
00708
00709 if (EDGE_COUNT (src->succs) > 2)
00710 return false;
00711
00712 if (!any_condjump_p (BB_END (src)))
00713 return false;
00714
00715 snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
00716 newdest = snd->dest;
00717 if (newdest == EXIT_BLOCK_PTR)
00718 return false;
00719
00720
00721 if (!really_delete)
00722 return true;
00723
00724
00725 irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
00726
00727 if (!redirect_edge_and_branch (e, newdest))
00728 return false;
00729 single_succ_edge (src)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
00730 single_succ_edge (src)->flags |= irr;
00731
00732 return true;
00733 }
00734
00735
00736 bool
00737 can_duplicate_loop_p (struct loop *loop)
00738 {
00739 int ret;
00740 basic_block *bbs = get_loop_body (loop);
00741
00742 ret = can_copy_bbs_p (bbs, loop->num_nodes);
00743 free (bbs);
00744
00745 return ret;
00746 }
00747
00748
00749
00750
00751 static void
00752 update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
00753 struct loop *loop)
00754 {
00755 unsigned i;
00756
00757 for (i = 0; i < nbbs; i++)
00758 bbs[i]->flags |= BB_DUPLICATED;
00759
00760 for (; loop->outer; loop = loop->outer)
00761 {
00762 if (!loop->single_exit)
00763 continue;
00764
00765 if (loop->single_exit->src->flags & BB_DUPLICATED)
00766 loop->single_exit = NULL;
00767 }
00768
00769 for (i = 0; i < nbbs; i++)
00770 bbs[i]->flags &= ~BB_DUPLICATED;
00771 }
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 bool
00784 duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
00785 unsigned int ndupl, sbitmap wont_exit,
00786 edge orig, edge *to_remove,
00787 unsigned int *n_to_remove, int flags)
00788 {
00789 struct loop *target, *aloop;
00790 struct loop **orig_loops;
00791 unsigned n_orig_loops;
00792 basic_block header = loop->header, latch = loop->latch;
00793 basic_block *new_bbs, *bbs, *first_active;
00794 basic_block new_bb, bb, first_active_latch = NULL;
00795 edge ae, latch_edge;
00796 edge spec_edges[2], new_spec_edges[2];
00797 #define SE_LATCH 0
00798 #define SE_ORIG 1
00799 unsigned i, j, n;
00800 int is_latch = (latch == e->src);
00801 int scale_act = 0, *scale_step = NULL, scale_main = 0;
00802 int p, freq_in, freq_le, freq_out_orig;
00803 int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
00804 int add_irreducible_flag;
00805 basic_block place_after;
00806
00807 gcc_assert (e->dest == loop->header);
00808 gcc_assert (ndupl > 0);
00809
00810 if (orig)
00811 {
00812
00813 gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
00814 gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
00815 }
00816
00817 n = loop->num_nodes;
00818 bbs = get_loop_body_in_dom_order (loop);
00819 gcc_assert (bbs[0] == loop->header);
00820 gcc_assert (bbs[n - 1] == loop->latch);
00821
00822
00823 if (!can_copy_bbs_p (bbs, loop->num_nodes))
00824 {
00825 free (bbs);
00826 return false;
00827 }
00828 new_bbs = XNEWVEC (basic_block, loop->num_nodes);
00829
00830
00831
00832 add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
00833 gcc_assert (!is_latch || !add_irreducible_flag);
00834
00835
00836 latch_edge = loop_latch_edge (loop);
00837
00838 if (flags & DLTHE_FLAG_UPDATE_FREQ)
00839 {
00840
00841
00842 freq_in = header->frequency;
00843 freq_le = EDGE_FREQUENCY (latch_edge);
00844 if (freq_in == 0)
00845 freq_in = 1;
00846 if (freq_in < freq_le)
00847 freq_in = freq_le;
00848 freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
00849 if (freq_out_orig > freq_in - freq_le)
00850 freq_out_orig = freq_in - freq_le;
00851 prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
00852 prob_pass_wont_exit =
00853 RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
00854
00855 scale_step = XNEWVEC (int, ndupl);
00856
00857 for (i = 1; i <= ndupl; i++)
00858 scale_step[i - 1] = TEST_BIT (wont_exit, i)
00859 ? prob_pass_wont_exit
00860 : prob_pass_thru;
00861
00862
00863
00864 if (flags & DLTHE_FLAG_COMPLETTE_PEEL)
00865 {
00866 int wanted_freq = EDGE_FREQUENCY (e);
00867
00868 if (wanted_freq > freq_in)
00869 wanted_freq = freq_in;
00870
00871 gcc_assert (!is_latch);
00872
00873
00874
00875
00876 scale_act = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
00877
00878
00879 for (i = 0; i < ndupl; i++)
00880 wanted_freq = RDIV (wanted_freq * scale_step[i], REG_BR_PROB_BASE);
00881 scale_main = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
00882 }
00883 else if (is_latch)
00884 {
00885 prob_pass_main = TEST_BIT (wont_exit, 0)
00886 ? prob_pass_wont_exit
00887 : prob_pass_thru;
00888 p = prob_pass_main;
00889 scale_main = REG_BR_PROB_BASE;
00890 for (i = 0; i < ndupl; i++)
00891 {
00892 scale_main += p;
00893 p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
00894 }
00895 scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
00896 scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
00897 }
00898 else
00899 {
00900 scale_main = REG_BR_PROB_BASE;
00901 for (i = 0; i < ndupl; i++)
00902 scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
00903 scale_act = REG_BR_PROB_BASE - prob_pass_thru;
00904 }
00905 for (i = 0; i < ndupl; i++)
00906 gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
00907 gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
00908 && scale_act >= 0 && scale_act <= REG_BR_PROB_BASE);
00909 }
00910
00911
00912 target = e->src->loop_father;
00913
00914
00915 n_orig_loops = 0;
00916 for (aloop = loop->inner; aloop; aloop = aloop->next)
00917 n_orig_loops++;
00918 orig_loops = XCNEWVEC (struct loop *, n_orig_loops);
00919 for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
00920 orig_loops[i] = aloop;
00921
00922 loop->copy = target;
00923
00924 first_active = XNEWVEC (basic_block, n);
00925 if (is_latch)
00926 {
00927 memcpy (first_active, bbs, n * sizeof (basic_block));
00928 first_active_latch = latch;
00929 }
00930
00931
00932 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
00933 update_single_exits_after_duplication (bbs, n, target);
00934
00935
00936 if (orig && TEST_BIT (wont_exit, 0))
00937 to_remove[(*n_to_remove)++] = orig;
00938
00939 spec_edges[SE_ORIG] = orig;
00940 spec_edges[SE_LATCH] = latch_edge;
00941
00942 place_after = e->src;
00943 for (j = 0; j < ndupl; j++)
00944 {
00945
00946 copy_loops_to (loops, orig_loops, n_orig_loops, target);
00947
00948
00949 copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop,
00950 place_after);
00951 place_after = new_spec_edges[SE_LATCH]->src;
00952
00953 if (flags & DLTHE_RECORD_COPY_NUMBER)
00954 for (i = 0; i < n; i++)
00955 {
00956 gcc_assert (!new_bbs[i]->aux);
00957 new_bbs[i]->aux = (void *)(size_t)(j + 1);
00958 }
00959
00960
00961 if (add_irreducible_flag)
00962 {
00963 for (i = 0; i < n; i++)
00964 new_bbs[i]->flags |= BB_DUPLICATED;
00965 for (i = 0; i < n; i++)
00966 {
00967 edge_iterator ei;
00968 new_bb = new_bbs[i];
00969 if (new_bb->loop_father == target)
00970 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
00971
00972 FOR_EACH_EDGE (ae, ei, new_bb->succs)
00973 if ((ae->dest->flags & BB_DUPLICATED)
00974 && (ae->src->loop_father == target
00975 || ae->dest->loop_father == target))
00976 ae->flags |= EDGE_IRREDUCIBLE_LOOP;
00977 }
00978 for (i = 0; i < n; i++)
00979 new_bbs[i]->flags &= ~BB_DUPLICATED;
00980 }
00981
00982
00983 if (is_latch)
00984 {
00985 redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
00986 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
00987 loop->header);
00988 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
00989 latch = loop->latch = new_bbs[n - 1];
00990 e = latch_edge = new_spec_edges[SE_LATCH];
00991 }
00992 else
00993 {
00994 redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
00995 loop->header);
00996 redirect_edge_and_branch_force (e, new_bbs[0]);
00997 set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
00998 e = new_spec_edges[SE_LATCH];
00999 }
01000
01001
01002 if (orig && TEST_BIT (wont_exit, j + 1))
01003 to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
01004
01005
01006
01007 if (!first_active_latch)
01008 {
01009 memcpy (first_active, new_bbs, n * sizeof (basic_block));
01010 first_active_latch = new_bbs[n - 1];
01011 }
01012
01013
01014 if (flags & DLTHE_FLAG_UPDATE_FREQ)
01015 {
01016 scale_bbs_frequencies_int (new_bbs, n, scale_act, REG_BR_PROB_BASE);
01017 scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
01018 }
01019 }
01020 free (new_bbs);
01021 free (orig_loops);
01022
01023
01024 if (!is_latch)
01025 set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
01026 if (flags & DLTHE_FLAG_UPDATE_FREQ)
01027 {
01028 scale_bbs_frequencies_int (bbs, n, scale_main, REG_BR_PROB_BASE);
01029 free (scale_step);
01030 }
01031
01032
01033 for (i = 0; i < n; i++)
01034 {
01035 basic_block dominated, dom_bb, *dom_bbs;
01036 int n_dom_bbs,j;
01037
01038 bb = bbs[i];
01039 bb->aux = 0;
01040
01041 n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
01042 for (j = 0; j < n_dom_bbs; j++)
01043 {
01044 dominated = dom_bbs[j];
01045 if (flow_bb_inside_loop_p (loop, dominated))
01046 continue;
01047 dom_bb = nearest_common_dominator (
01048 CDI_DOMINATORS, first_active[i], first_active_latch);
01049 set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
01050 }
01051 free (dom_bbs);
01052 }
01053 free (first_active);
01054
01055 free (bbs);
01056
01057 return true;
01058 }
01059
01060
01061
01062
01063
01064 static edge mfb_kj_edge;
01065 static bool
01066 mfb_keep_just (edge e)
01067 {
01068 return e != mfb_kj_edge;
01069 }
01070
01071
01072
01073
01074
01075 static void
01076 mfb_update_loops (basic_block jump)
01077 {
01078 struct loop *loop = single_succ (jump)->loop_father;
01079
01080 if (dom_computed[CDI_DOMINATORS])
01081 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
01082 add_bb_to_loop (jump, loop);
01083 loop->latch = jump;
01084 }
01085
01086
01087
01088
01089
01090
01091 static basic_block
01092 create_preheader (struct loop *loop, int flags)
01093 {
01094 edge e, fallthru;
01095 basic_block dummy;
01096 struct loop *cloop, *ploop;
01097 int nentry = 0;
01098 bool irred = false;
01099 bool latch_edge_was_fallthru;
01100 edge one_succ_pred = 0;
01101 edge_iterator ei;
01102
01103 cloop = loop->outer;
01104
01105 FOR_EACH_EDGE (e, ei, loop->header->preds)
01106 {
01107 if (e->src == loop->latch)
01108 continue;
01109 irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
01110 nentry++;
01111 if (single_succ_p (e->src))
01112 one_succ_pred = e;
01113 }
01114 gcc_assert (nentry);
01115 if (nentry == 1)
01116 {
01117
01118
01119 e = EDGE_PRED (loop->header,
01120 EDGE_PRED (loop->header, 0)->src == loop->latch);
01121
01122 if (!(flags & CP_SIMPLE_PREHEADERS) || single_succ_p (e->src))
01123 return NULL;
01124 }
01125
01126 mfb_kj_edge = loop_latch_edge (loop);
01127 latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
01128 fallthru = make_forwarder_block (loop->header, mfb_keep_just,
01129 mfb_update_loops);
01130 dummy = fallthru->src;
01131 loop->header = fallthru->dest;
01132
01133
01134
01135 for (ploop = loop; ploop; ploop = ploop->outer)
01136 if (ploop->latch == dummy)
01137 ploop->latch = fallthru->dest;
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147 if (latch_edge_was_fallthru)
01148 {
01149 if (one_succ_pred)
01150 e = one_succ_pred;
01151 else
01152 e = EDGE_PRED (dummy, 0);
01153
01154 move_block_after (dummy, e->src);
01155 }
01156
01157 loop->header->loop_father = loop;
01158 add_bb_to_loop (dummy, cloop);
01159
01160 if (irred)
01161 {
01162 dummy->flags |= BB_IRREDUCIBLE_LOOP;
01163 single_succ_edge (dummy)->flags |= EDGE_IRREDUCIBLE_LOOP;
01164 }
01165
01166 if (dump_file)
01167 fprintf (dump_file, "Created preheader block for loop %i\n",
01168 loop->num);
01169
01170 return dummy;
01171 }
01172
01173
01174
01175 void
01176 create_preheaders (struct loops *loops, int flags)
01177 {
01178 unsigned i;
01179 for (i = 1; i < loops->num; i++)
01180 create_preheader (loops->parray[i], flags);
01181 loops->state |= LOOPS_HAVE_PREHEADERS;
01182 }
01183
01184
01185
01186 void
01187 force_single_succ_latches (struct loops *loops)
01188 {
01189 unsigned i;
01190 struct loop *loop;
01191 edge e;
01192
01193 for (i = 1; i < loops->num; i++)
01194 {
01195 loop = loops->parray[i];
01196 if (loop->latch != loop->header && single_succ_p (loop->latch))
01197 continue;
01198
01199 e = find_edge (loop->latch, loop->header);
01200
01201 loop_split_edge_with (e, NULL_RTX);
01202 }
01203 loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
01204 }
01205
01206
01207
01208
01209
01210 basic_block
01211 loop_split_edge_with (edge e, rtx insns)
01212 {
01213 basic_block src, dest, new_bb;
01214 struct loop *loop_c;
01215
01216 src = e->src;
01217 dest = e->dest;
01218
01219 loop_c = find_common_loop (src->loop_father, dest->loop_father);
01220
01221
01222
01223 new_bb = split_edge (e);
01224 add_bb_to_loop (new_bb, loop_c);
01225 new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
01226
01227 if (insns)
01228 emit_insn_after (insns, BB_END (new_bb));
01229
01230 if (dest->loop_father->latch == src)
01231 dest->loop_father->latch = new_bb;
01232
01233 return new_bb;
01234 }
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251 static basic_block
01252 lv_adjust_loop_entry_edge (basic_block first_head,
01253 basic_block second_head,
01254 edge e,
01255 void *cond_expr)
01256 {
01257 basic_block new_head = NULL;
01258 edge e1;
01259
01260 gcc_assert (e->dest == second_head);
01261
01262
01263
01264 new_head = split_edge (e);
01265
01266
01267 lv_add_condition_to_bb (first_head, second_head, new_head,
01268 cond_expr);
01269
01270
01271 e1 = make_edge (new_head, first_head, ir_type () ? EDGE_TRUE_VALUE : 0);
01272 set_immediate_dominator (CDI_DOMINATORS, first_head, new_head);
01273 set_immediate_dominator (CDI_DOMINATORS, second_head, new_head);
01274
01275
01276 lv_adjust_loop_header_phi (first_head, second_head, new_head, e1);
01277
01278 return new_head;
01279 }
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293 struct loop *
01294 loop_version (struct loops *loops, struct loop * loop,
01295 void *cond_expr, basic_block *condition_bb,
01296 bool place_after)
01297 {
01298 basic_block first_head, second_head;
01299 edge entry, latch_edge, exit, true_edge, false_edge;
01300 int irred_flag;
01301 struct loop *nloop;
01302 basic_block cond_bb;
01303
01304
01305 if (loop->inner)
01306 return NULL;
01307
01308
01309 entry = loop_preheader_edge (loop);
01310 irred_flag = entry->flags & EDGE_IRREDUCIBLE_LOOP;
01311 entry->flags &= ~EDGE_IRREDUCIBLE_LOOP;
01312
01313
01314 first_head = entry->dest;
01315
01316
01317 if (!cfg_hook_duplicate_loop_to_header_edge (loop, entry, loops, 1,
01318 NULL, NULL, NULL, NULL, 0))
01319 return NULL;
01320
01321
01322
01323 second_head = entry->dest;
01324
01325
01326 cond_bb = lv_adjust_loop_entry_edge (first_head, second_head,
01327 entry, cond_expr);
01328 if (condition_bb)
01329 *condition_bb = cond_bb;
01330
01331 if (!cond_bb)
01332 {
01333 entry->flags |= irred_flag;
01334 return NULL;
01335 }
01336
01337 latch_edge = single_succ_edge (get_bb_copy (loop->latch));
01338
01339 extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
01340 nloop = loopify (loops,
01341 latch_edge,
01342 single_pred_edge (get_bb_copy (loop->header)),
01343 cond_bb, true_edge, false_edge,
01344 false );
01345
01346 exit = loop->single_exit;
01347 if (exit)
01348 nloop->single_exit = find_edge (get_bb_copy (exit->src), exit->dest);
01349
01350
01351 lv_flush_pending_stmts (latch_edge);
01352
01353
01354 extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
01355 lv_flush_pending_stmts (false_edge);
01356
01357 if (irred_flag)
01358 {
01359 cond_bb->flags |= BB_IRREDUCIBLE_LOOP;
01360 loop_preheader_edge (loop)->flags |= EDGE_IRREDUCIBLE_LOOP;
01361 loop_preheader_edge (nloop)->flags |= EDGE_IRREDUCIBLE_LOOP;
01362 single_pred_edge (cond_bb)->flags |= EDGE_IRREDUCIBLE_LOOP;
01363 }
01364
01365 if (place_after)
01366 {
01367 basic_block *bbs = get_loop_body_in_dom_order (nloop), after;
01368 unsigned i;
01369
01370 after = loop->latch;
01371
01372 for (i = 0; i < nloop->num_nodes; i++)
01373 {
01374 move_block_after (bbs[i], after);
01375 after = bbs[i];
01376 }
01377 free (bbs);
01378 }
01379
01380
01381
01382
01383 loop_split_edge_with (loop_preheader_edge (loop), NULL);
01384 loop_split_edge_with (loop_preheader_edge (nloop), NULL);
01385
01386 return nloop;
01387 }
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400 void
01401 fix_loop_structure (struct loops *loops, bitmap changed_bbs)
01402 {
01403 basic_block bb;
01404 struct loop *loop, *ploop;
01405 unsigned i;
01406
01407
01408 FOR_EACH_BB (bb)
01409 {
01410 bb->aux = (void *) (size_t) bb->loop_father->depth;
01411 bb->loop_father = loops->tree_root;
01412 }
01413
01414
01415 loops->tree_root->num_nodes = n_basic_blocks;
01416 for (i = 1; i < loops->num; i++)
01417 {
01418 loop = loops->parray[i];
01419 if (!loop)
01420 continue;
01421
01422 loop->num_nodes = 0;
01423 if (loop->header)
01424 continue;
01425
01426 while (loop->inner)
01427 {
01428 ploop = loop->inner;
01429 flow_loop_tree_node_remove (ploop);
01430 flow_loop_tree_node_add (loop->outer, ploop);
01431 }
01432
01433
01434 flow_loop_tree_node_remove (loop);
01435 loops->parray[loop->num] = NULL;
01436 flow_loop_free (loop);
01437 }
01438
01439
01440 loop = loops->tree_root;
01441 while (1)
01442 {
01443 if (loop->inner)
01444 loop = loop->inner;
01445 else
01446 {
01447 while (!loop->next
01448 && loop != loops->tree_root)
01449 loop = loop->outer;
01450 if (loop == loops->tree_root)
01451 break;
01452
01453 loop = loop->next;
01454 }
01455
01456 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
01457 }
01458
01459
01460 for (i = 1; i < loops->num; i++)
01461 {
01462 loop = loops->parray[i];
01463 if (!loop)
01464 continue;
01465
01466 bb = loop_preheader_edge (loop)->src;
01467 if (bb->loop_father != loop->outer)
01468 {
01469 flow_loop_tree_node_remove (loop);
01470 flow_loop_tree_node_add (bb->loop_father, loop);
01471 }
01472 }
01473
01474
01475 FOR_EACH_BB (bb)
01476 {
01477 if (changed_bbs
01478 && (void *) (size_t) bb->loop_father->depth != bb->aux)
01479 bitmap_set_bit (changed_bbs, bb->index);
01480
01481 bb->aux = NULL;
01482 }
01483
01484 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
01485 mark_single_exit_loops (loops);
01486 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
01487 mark_irreducible_loops (loops);
01488 }