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 "tree.h"
00026 #include "rtl.h"
00027 #include "tm_p.h"
00028 #include "hard-reg-set.h"
00029 #include "basic-block.h"
00030 #include "output.h"
00031 #include "diagnostic.h"
00032 #include "tree-flow.h"
00033 #include "tree-dump.h"
00034 #include "timevar.h"
00035 #include "cfgloop.h"
00036 #include "tree-pass.h"
00037 #include "cfglayout.h"
00038 #include "tree-scalar-evolution.h"
00039 #include "params.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 void
00051 create_iv (tree base, tree step, tree var, struct loop *loop,
00052 block_stmt_iterator *incr_pos, bool after,
00053 tree *var_before, tree *var_after)
00054 {
00055 tree stmt, initial, step1, stmts;
00056 tree vb, va;
00057 enum tree_code incr_op = PLUS_EXPR;
00058 edge pe = loop_preheader_edge (loop);
00059
00060 if (!var)
00061 {
00062 var = create_tmp_var (TREE_TYPE (base), "ivtmp");
00063 add_referenced_var (var);
00064 }
00065
00066 vb = make_ssa_name (var, NULL_TREE);
00067 if (var_before)
00068 *var_before = vb;
00069 va = make_ssa_name (var, NULL_TREE);
00070 if (var_after)
00071 *var_after = va;
00072
00073
00074
00075 if (TREE_CODE (step) == INTEGER_CST)
00076 {
00077 if (TYPE_UNSIGNED (TREE_TYPE (step)))
00078 {
00079 step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
00080 if (tree_int_cst_lt (step1, step))
00081 {
00082 incr_op = MINUS_EXPR;
00083 step = step1;
00084 }
00085 }
00086 else
00087 {
00088 bool ovf;
00089
00090 if (!tree_expr_nonnegative_warnv_p (step, &ovf)
00091 && may_negate_without_overflow_p (step))
00092 {
00093 incr_op = MINUS_EXPR;
00094 step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
00095 }
00096 }
00097 }
00098
00099
00100
00101 step = force_gimple_operand (step, &stmts, true, var);
00102 if (stmts)
00103 bsi_insert_on_edge_immediate_loop (pe, stmts);
00104
00105 stmt = build2 (MODIFY_EXPR, void_type_node, va,
00106 build2 (incr_op, TREE_TYPE (base),
00107 vb, step));
00108 SSA_NAME_DEF_STMT (va) = stmt;
00109 if (after)
00110 bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
00111 else
00112 bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
00113
00114 initial = force_gimple_operand (base, &stmts, true, var);
00115 if (stmts)
00116 bsi_insert_on_edge_immediate_loop (pe, stmts);
00117
00118 stmt = create_phi_node (vb, loop->header);
00119 SSA_NAME_DEF_STMT (vb) = stmt;
00120 add_phi_arg (stmt, initial, loop_preheader_edge (loop));
00121 add_phi_arg (stmt, va, loop_latch_edge (loop));
00122 }
00123
00124
00125
00126 static void
00127 add_exit_phis_edge (basic_block exit, tree use)
00128 {
00129 tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
00130 basic_block def_bb = bb_for_stmt (def_stmt);
00131 struct loop *def_loop;
00132 edge e;
00133 edge_iterator ei;
00134
00135
00136
00137 FOR_EACH_EDGE (e, ei, exit->preds)
00138 {
00139 def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
00140 if (!flow_bb_inside_loop_p (def_loop, e->dest))
00141 break;
00142 }
00143
00144 if (!e)
00145 return;
00146
00147 phi = create_phi_node (use, exit);
00148 create_new_def_for (PHI_RESULT (phi), phi, PHI_RESULT_PTR (phi));
00149 FOR_EACH_EDGE (e, ei, exit->preds)
00150 add_phi_arg (phi, use, e);
00151 }
00152
00153
00154
00155
00156 static void
00157 add_exit_phis_var (tree var, bitmap livein, bitmap exits)
00158 {
00159 bitmap def;
00160 unsigned index;
00161 basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
00162 bitmap_iterator bi;
00163
00164 if (is_gimple_reg (var))
00165 bitmap_clear_bit (livein, def_bb->index);
00166 else
00167 bitmap_set_bit (livein, def_bb->index);
00168
00169 def = BITMAP_ALLOC (NULL);
00170 bitmap_set_bit (def, def_bb->index);
00171 compute_global_livein (livein, def);
00172 BITMAP_FREE (def);
00173
00174 EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index, bi)
00175 {
00176 add_exit_phis_edge (BASIC_BLOCK (index), var);
00177 }
00178 }
00179
00180
00181
00182
00183
00184 static void
00185 add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
00186 {
00187 unsigned i;
00188 bitmap_iterator bi;
00189
00190 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
00191 {
00192 add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
00193 }
00194 }
00195
00196
00197
00198 static bitmap
00199 get_loops_exits (void)
00200 {
00201 bitmap exits = BITMAP_ALLOC (NULL);
00202 basic_block bb;
00203 edge e;
00204 edge_iterator ei;
00205
00206 FOR_EACH_BB (bb)
00207 {
00208 FOR_EACH_EDGE (e, ei, bb->preds)
00209 if (e->src != ENTRY_BLOCK_PTR
00210 && !flow_bb_inside_loop_p (e->src->loop_father, bb))
00211 {
00212 bitmap_set_bit (exits, bb->index);
00213 break;
00214 }
00215 }
00216
00217 return exits;
00218 }
00219
00220
00221
00222
00223
00224 static void
00225 find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks,
00226 bitmap need_phis)
00227 {
00228 unsigned ver;
00229 basic_block def_bb;
00230 struct loop *def_loop;
00231
00232 if (TREE_CODE (use) != SSA_NAME)
00233 return;
00234
00235
00236 if (!is_gimple_reg (use))
00237 return;
00238
00239 ver = SSA_NAME_VERSION (use);
00240 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
00241 if (!def_bb)
00242 return;
00243 def_loop = def_bb->loop_father;
00244
00245
00246 if (!def_loop->outer)
00247 return;
00248
00249 if (!use_blocks[ver])
00250 use_blocks[ver] = BITMAP_ALLOC (NULL);
00251 bitmap_set_bit (use_blocks[ver], bb->index);
00252
00253 bitmap_set_bit (need_phis, ver);
00254 }
00255
00256
00257
00258
00259
00260
00261 static void
00262 find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks, bitmap need_phis)
00263 {
00264 ssa_op_iter iter;
00265 tree var;
00266 basic_block bb = bb_for_stmt (stmt);
00267
00268 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
00269 find_uses_to_rename_use (bb, var, use_blocks, need_phis);
00270 }
00271
00272
00273
00274
00275
00276
00277 static void
00278 find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks, bitmap need_phis)
00279 {
00280 block_stmt_iterator bsi;
00281 edge e;
00282 edge_iterator ei;
00283 tree phi;
00284
00285 FOR_EACH_EDGE (e, ei, bb->succs)
00286 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
00287 find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
00288 use_blocks, need_phis);
00289
00290 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00291 find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks, need_phis);
00292 }
00293
00294
00295
00296
00297
00298
00299 static void
00300 find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks, bitmap need_phis)
00301 {
00302 basic_block bb;
00303 unsigned index;
00304 bitmap_iterator bi;
00305
00306 if (changed_bbs && !bitmap_empty_p (changed_bbs))
00307 {
00308 EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
00309 {
00310 find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks, need_phis);
00311 }
00312 }
00313 else
00314 {
00315 FOR_EACH_BB (bb)
00316 {
00317 find_uses_to_rename_bb (bb, use_blocks, need_phis);
00318 }
00319 }
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 void
00355 rewrite_into_loop_closed_ssa (bitmap changed_bbs, unsigned update_flag)
00356 {
00357 bitmap loop_exits = get_loops_exits ();
00358 bitmap *use_blocks;
00359 unsigned i, old_num_ssa_names;
00360 bitmap names_to_rename = BITMAP_ALLOC (NULL);
00361
00362
00363
00364 update_ssa (update_flag);
00365
00366 old_num_ssa_names = num_ssa_names;
00367 use_blocks = XCNEWVEC (bitmap, old_num_ssa_names);
00368
00369
00370 find_uses_to_rename (changed_bbs, use_blocks, names_to_rename);
00371
00372
00373
00374 add_exit_phis (names_to_rename, use_blocks, loop_exits);
00375
00376 for (i = 0; i < old_num_ssa_names; i++)
00377 BITMAP_FREE (use_blocks[i]);
00378 free (use_blocks);
00379 BITMAP_FREE (loop_exits);
00380 BITMAP_FREE (names_to_rename);
00381
00382
00383
00384 update_ssa (TODO_update_ssa);
00385 }
00386
00387
00388
00389 static void
00390 check_loop_closed_ssa_use (basic_block bb, tree use)
00391 {
00392 tree def;
00393 basic_block def_bb;
00394
00395 if (TREE_CODE (use) != SSA_NAME || !is_gimple_reg (use))
00396 return;
00397
00398 def = SSA_NAME_DEF_STMT (use);
00399 def_bb = bb_for_stmt (def);
00400 gcc_assert (!def_bb
00401 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
00402 }
00403
00404
00405
00406 static void
00407 check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
00408 {
00409 ssa_op_iter iter;
00410 tree var;
00411
00412 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
00413 check_loop_closed_ssa_use (bb, var);
00414 }
00415
00416
00417
00418 void
00419 verify_loop_closed_ssa (void)
00420 {
00421 basic_block bb;
00422 block_stmt_iterator bsi;
00423 tree phi;
00424 unsigned i;
00425
00426 if (current_loops == NULL)
00427 return;
00428
00429 verify_ssa (false);
00430
00431 FOR_EACH_BB (bb)
00432 {
00433 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00434 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
00435 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
00436 PHI_ARG_DEF (phi, i));
00437
00438 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00439 check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));
00440 }
00441 }
00442
00443
00444
00445
00446 void
00447 split_loop_exit_edge (edge exit)
00448 {
00449 basic_block dest = exit->dest;
00450 basic_block bb = loop_split_edge_with (exit, NULL);
00451 tree phi, new_phi, new_name, name;
00452 use_operand_p op_p;
00453
00454 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
00455 {
00456 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
00457
00458 name = USE_FROM_PTR (op_p);
00459
00460
00461
00462 if (TREE_CODE (name) != SSA_NAME)
00463 continue;
00464
00465
00466
00467 new_name = duplicate_ssa_name (name, NULL);
00468 new_phi = create_phi_node (new_name, bb);
00469 SSA_NAME_DEF_STMT (new_name) = new_phi;
00470 add_phi_arg (new_phi, name, exit);
00471 SET_USE (op_p, new_name);
00472 }
00473 }
00474
00475
00476
00477
00478 basic_block
00479 bsi_insert_on_edge_immediate_loop (edge e, tree stmt)
00480 {
00481 basic_block src, dest, new_bb;
00482 struct loop *loop_c;
00483
00484 src = e->src;
00485 dest = e->dest;
00486
00487 loop_c = find_common_loop (src->loop_father, dest->loop_father);
00488
00489 new_bb = bsi_insert_on_edge_immediate (e, stmt);
00490
00491 if (!new_bb)
00492 return NULL;
00493
00494 add_bb_to_loop (new_bb, loop_c);
00495 if (dest->loop_father->latch == src)
00496 dest->loop_father->latch = new_bb;
00497
00498 return new_bb;
00499 }
00500
00501
00502
00503
00504 basic_block
00505 ip_end_pos (struct loop *loop)
00506 {
00507 return loop->latch;
00508 }
00509
00510
00511
00512
00513 basic_block
00514 ip_normal_pos (struct loop *loop)
00515 {
00516 tree last;
00517 basic_block bb;
00518 edge exit;
00519
00520 if (!single_pred_p (loop->latch))
00521 return NULL;
00522
00523 bb = single_pred (loop->latch);
00524 last = last_stmt (bb);
00525 if (TREE_CODE (last) != COND_EXPR)
00526 return NULL;
00527
00528 exit = EDGE_SUCC (bb, 0);
00529 if (exit->dest == loop->latch)
00530 exit = EDGE_SUCC (bb, 1);
00531
00532 if (flow_bb_inside_loop_p (loop, exit->dest))
00533 return NULL;
00534
00535 return bb;
00536 }
00537
00538
00539
00540
00541
00542
00543 void
00544 standard_iv_increment_position (struct loop *loop, block_stmt_iterator *bsi,
00545 bool *insert_after)
00546 {
00547 basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
00548 tree last = last_stmt (latch);
00549
00550 if (!bb
00551 || (last && TREE_CODE (last) != LABEL_EXPR))
00552 {
00553 *bsi = bsi_last (latch);
00554 *insert_after = true;
00555 }
00556 else
00557 {
00558 *bsi = bsi_last (bb);
00559 *insert_after = false;
00560 }
00561 }
00562
00563
00564
00565
00566 static void
00567 copy_phi_node_args (unsigned first_new_block)
00568 {
00569 unsigned i;
00570
00571 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00572 BASIC_BLOCK (i)->flags |= BB_DUPLICATED;
00573
00574 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00575 add_phi_args_after_copy_bb (BASIC_BLOCK (i));
00576
00577 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00578 BASIC_BLOCK (i)->flags &= ~BB_DUPLICATED;
00579 }
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 bool
00592 tree_duplicate_loop_to_header_edge (struct loop *loop, edge e,
00593 struct loops *loops,
00594 unsigned int ndupl, sbitmap wont_exit,
00595 edge orig, edge *to_remove,
00596 unsigned int *n_to_remove, int flags)
00597 {
00598 unsigned first_new_block;
00599
00600 if (!(loops->state & LOOPS_HAVE_SIMPLE_LATCHES))
00601 return false;
00602 if (!(loops->state & LOOPS_HAVE_PREHEADERS))
00603 return false;
00604
00605 #ifdef ENABLE_CHECKING
00606 verify_loop_closed_ssa ();
00607 #endif
00608
00609 first_new_block = last_basic_block;
00610 if (!duplicate_loop_to_header_edge (loop, e, loops, ndupl, wont_exit,
00611 orig, to_remove, n_to_remove, flags))
00612 return false;
00613
00614
00615 flush_pending_stmts (e);
00616
00617
00618 copy_phi_node_args (first_new_block);
00619
00620 scev_reset ();
00621
00622 return true;
00623 }
00624
00625
00626
00627 static tree
00628 build_if_stmt (tree cond, tree then_label, tree else_label)
00629 {
00630 return build3 (COND_EXPR, void_type_node,
00631 cond,
00632 build1 (GOTO_EXPR, void_type_node, then_label),
00633 build1 (GOTO_EXPR, void_type_node, else_label));
00634 }
00635
00636
00637
00638
00639 bool
00640 can_unroll_loop_p (struct loop *loop, unsigned factor,
00641 struct tree_niter_desc *niter)
00642 {
00643 edge exit;
00644
00645
00646
00647
00648
00649
00650
00651 exit = single_dom_exit (loop);
00652 if (!exit)
00653 return false;
00654
00655 if (!number_of_iterations_exit (loop, exit, niter, false)
00656 || niter->cmp == ERROR_MARK
00657
00658
00659
00660
00661
00662 || contains_abnormal_ssa_name_p (niter->may_be_zero)
00663 || contains_abnormal_ssa_name_p (niter->control.base)
00664 || contains_abnormal_ssa_name_p (niter->control.step)
00665 || contains_abnormal_ssa_name_p (niter->bound))
00666 return false;
00667
00668
00669 if (!can_duplicate_loop_p (loop))
00670 return false;
00671
00672
00673 if (tree_num_loop_insns (loop) * factor
00674 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS))
00675 return false;
00676
00677 return true;
00678 }
00679
00680
00681
00682
00683
00684
00685
00686 static void
00687 determine_exit_conditions (struct loop *loop, struct tree_niter_desc *desc,
00688 unsigned factor, tree *enter_cond,
00689 tree *exit_base, tree *exit_step,
00690 enum tree_code *exit_cmp, tree *exit_bound)
00691 {
00692 tree stmts;
00693 tree base = desc->control.base;
00694 tree step = desc->control.step;
00695 tree bound = desc->bound;
00696 tree type = TREE_TYPE (base);
00697 tree bigstep, delta;
00698 tree min = lower_bound_in_type (type, type);
00699 tree max = upper_bound_in_type (type, type);
00700 enum tree_code cmp = desc->cmp;
00701 tree cond = boolean_true_node, assum;
00702
00703 *enter_cond = boolean_false_node;
00704 *exit_base = NULL_TREE;
00705 *exit_step = NULL_TREE;
00706 *exit_cmp = ERROR_MARK;
00707 *exit_bound = NULL_TREE;
00708 gcc_assert (cmp != ERROR_MARK);
00709
00710
00711
00712
00713
00714 if (cmp == NE_EXPR)
00715 {
00716 if (tree_int_cst_sign_bit (step))
00717 cmp = GT_EXPR;
00718 else
00719 cmp = LT_EXPR;
00720 }
00721 else if (cmp == LT_EXPR)
00722 {
00723 gcc_assert (!tree_int_cst_sign_bit (step));
00724 }
00725 else if (cmp == GT_EXPR)
00726 {
00727 gcc_assert (tree_int_cst_sign_bit (step));
00728 }
00729 else
00730 gcc_unreachable ();
00731
00732
00733
00734
00735
00736
00737
00738
00739 if (!zero_p (desc->may_be_zero))
00740 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
00741 invert_truthvalue (desc->may_be_zero),
00742 cond);
00743
00744 bigstep = fold_build2 (MULT_EXPR, type, step,
00745 build_int_cst_type (type, factor));
00746 delta = fold_build2 (MINUS_EXPR, type, bigstep, step);
00747 if (cmp == LT_EXPR)
00748 assum = fold_build2 (GE_EXPR, boolean_type_node,
00749 bound,
00750 fold_build2 (PLUS_EXPR, type, min, delta));
00751 else
00752 assum = fold_build2 (LE_EXPR, boolean_type_node,
00753 bound,
00754 fold_build2 (PLUS_EXPR, type, max, delta));
00755 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
00756
00757 bound = fold_build2 (MINUS_EXPR, type, bound, delta);
00758 assum = fold_build2 (cmp, boolean_type_node, base, bound);
00759 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
00760
00761 cond = force_gimple_operand (unshare_expr (cond), &stmts, false, NULL_TREE);
00762 if (stmts)
00763 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop), stmts);
00764
00765
00766
00767 if (!is_gimple_condexpr (cond))
00768 {
00769 cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
00770 if (stmts)
00771 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop), stmts);
00772 }
00773 *enter_cond = cond;
00774
00775 base = force_gimple_operand (unshare_expr (base), &stmts, true, NULL_TREE);
00776 if (stmts)
00777 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop), stmts);
00778 bound = force_gimple_operand (unshare_expr (bound), &stmts, true, NULL_TREE);
00779 if (stmts)
00780 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop), stmts);
00781
00782 *exit_base = base;
00783 *exit_step = bigstep;
00784 *exit_cmp = cmp;
00785 *exit_bound = bound;
00786 }
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839 void
00840 tree_unroll_loop (struct loops *loops, struct loop *loop, unsigned factor,
00841 edge exit, struct tree_niter_desc *desc)
00842 {
00843 tree dont_exit, exit_if, ctr_before, ctr_after;
00844 tree enter_main_cond, exit_base, exit_step, exit_bound;
00845 enum tree_code exit_cmp;
00846 tree phi_old_loop, phi_new_loop, phi_rest, init, next, new_init, var;
00847 struct loop *new_loop;
00848 basic_block rest, exit_bb;
00849 edge old_entry, new_entry, old_latch, precond_edge, new_exit;
00850 edge nonexit, new_nonexit;
00851 block_stmt_iterator bsi;
00852 use_operand_p op;
00853 bool ok;
00854 unsigned est_niter;
00855 unsigned irr = loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP;
00856 sbitmap wont_exit;
00857
00858 est_niter = expected_loop_iterations (loop);
00859 determine_exit_conditions (loop, desc, factor,
00860 &enter_main_cond, &exit_base, &exit_step,
00861 &exit_cmp, &exit_bound);
00862
00863 new_loop = loop_version (loops, loop, enter_main_cond, NULL, true);
00864 gcc_assert (new_loop != NULL);
00865 update_ssa (TODO_update_ssa);
00866
00867
00868 dont_exit = ((exit->flags & EDGE_TRUE_VALUE)
00869 ? boolean_false_node
00870 : boolean_true_node);
00871 if (exit == EDGE_SUCC (exit->src, 0))
00872 nonexit = EDGE_SUCC (exit->src, 1);
00873 else
00874 nonexit = EDGE_SUCC (exit->src, 0);
00875 nonexit->probability = REG_BR_PROB_BASE;
00876 exit->probability = 0;
00877 nonexit->count += exit->count;
00878 exit->count = 0;
00879 exit_if = last_stmt (exit->src);
00880 COND_EXPR_COND (exit_if) = dont_exit;
00881 update_stmt (exit_if);
00882
00883 wont_exit = sbitmap_alloc (factor);
00884 sbitmap_ones (wont_exit);
00885 ok = tree_duplicate_loop_to_header_edge
00886 (loop, loop_latch_edge (loop), loops, factor - 1,
00887 wont_exit, NULL, NULL, NULL, DLTHE_FLAG_UPDATE_FREQ);
00888 free (wont_exit);
00889 gcc_assert (ok);
00890 update_ssa (TODO_update_ssa);
00891
00892
00893 rest = loop_preheader_edge (new_loop)->src;
00894 precond_edge = single_pred_edge (rest);
00895 loop_split_edge_with (loop_latch_edge (loop), NULL);
00896 exit_bb = single_pred (loop->latch);
00897
00898 new_exit = make_edge (exit_bb, rest, EDGE_FALSE_VALUE | irr);
00899 new_exit->count = loop_preheader_edge (loop)->count;
00900 est_niter = est_niter / factor + 1;
00901 new_exit->probability = REG_BR_PROB_BASE / est_niter;
00902
00903 new_nonexit = single_pred_edge (loop->latch);
00904 new_nonexit->flags = EDGE_TRUE_VALUE;
00905 new_nonexit->probability = REG_BR_PROB_BASE - new_exit->probability;
00906
00907 old_entry = loop_preheader_edge (loop);
00908 new_entry = loop_preheader_edge (new_loop);
00909 old_latch = loop_latch_edge (loop);
00910 for (phi_old_loop = phi_nodes (loop->header),
00911 phi_new_loop = phi_nodes (new_loop->header);
00912 phi_old_loop;
00913 phi_old_loop = PHI_CHAIN (phi_old_loop),
00914 phi_new_loop = PHI_CHAIN (phi_new_loop))
00915 {
00916 init = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_entry);
00917 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop, new_entry);
00918 gcc_assert (operand_equal_for_phi_arg_p (init, USE_FROM_PTR (op)));
00919 next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch);
00920
00921
00922
00923
00924 if (TREE_CODE (next) == SSA_NAME)
00925 var = SSA_NAME_VAR (next);
00926 else if (TREE_CODE (init) == SSA_NAME)
00927 var = SSA_NAME_VAR (init);
00928 else
00929 {
00930 var = create_tmp_var (TREE_TYPE (init), "unrinittmp");
00931 add_referenced_var (var);
00932 }
00933
00934 new_init = make_ssa_name (var, NULL_TREE);
00935 phi_rest = create_phi_node (new_init, rest);
00936 SSA_NAME_DEF_STMT (new_init) = phi_rest;
00937
00938 add_phi_arg (phi_rest, init, precond_edge);
00939 add_phi_arg (phi_rest, next, new_exit);
00940 SET_USE (op, new_init);
00941 }
00942
00943
00944
00945 bsi = bsi_last (exit_bb);
00946 create_iv (exit_base, exit_step, NULL_TREE, loop,
00947 &bsi, true, &ctr_before, &ctr_after);
00948 exit_if = build_if_stmt (build2 (exit_cmp, boolean_type_node, ctr_after,
00949 exit_bound),
00950 tree_block_label (loop->latch),
00951 tree_block_label (rest));
00952 bsi_insert_after (&bsi, exit_if, BSI_NEW_STMT);
00953
00954 verify_flow_info ();
00955 verify_dominators (CDI_DOMINATORS);
00956 verify_loop_structure (loops);
00957 verify_loop_closed_ssa ();
00958 }