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
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 void
00050 create_iv (tree base, tree step, tree var, struct loop *loop,
00051 block_stmt_iterator *incr_pos, bool after,
00052 tree *var_before, tree *var_after)
00053 {
00054 tree stmt, initial, step1, stmts;
00055 tree vb, va;
00056 enum tree_code incr_op = PLUS_EXPR;
00057
00058 if (!var)
00059 {
00060 var = create_tmp_var (TREE_TYPE (base), "ivtmp");
00061 add_referenced_tmp_var (var);
00062 }
00063
00064 vb = make_ssa_name (var, NULL_TREE);
00065 if (var_before)
00066 *var_before = vb;
00067 va = make_ssa_name (var, NULL_TREE);
00068 if (var_after)
00069 *var_after = va;
00070
00071
00072
00073 if (TREE_CODE (step) == INTEGER_CST)
00074 {
00075 if (TYPE_UNSIGNED (TREE_TYPE (step)))
00076 {
00077 step1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
00078 if (tree_int_cst_lt (step1, step))
00079 {
00080 incr_op = MINUS_EXPR;
00081 step = step1;
00082 }
00083 }
00084 else
00085 {
00086 if (!tree_expr_nonnegative_p (step)
00087 && may_negate_without_overflow_p (step))
00088 {
00089 incr_op = MINUS_EXPR;
00090 step = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
00091 }
00092 }
00093 }
00094
00095 stmt = build2 (MODIFY_EXPR, void_type_node, va,
00096 build2 (incr_op, TREE_TYPE (base),
00097 vb, step));
00098 SSA_NAME_DEF_STMT (va) = stmt;
00099 if (after)
00100 bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
00101 else
00102 bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
00103
00104 initial = force_gimple_operand (base, &stmts, true, var);
00105 if (stmts)
00106 {
00107 edge pe = loop_preheader_edge (loop);
00108
00109 bsi_insert_on_edge_immediate_loop (pe, stmts);
00110 }
00111
00112 stmt = create_phi_node (vb, loop->header);
00113 SSA_NAME_DEF_STMT (vb) = stmt;
00114 add_phi_arg (stmt, initial, loop_preheader_edge (loop));
00115 add_phi_arg (stmt, va, loop_latch_edge (loop));
00116 }
00117
00118
00119
00120 static void
00121 add_exit_phis_edge (basic_block exit, tree use)
00122 {
00123 tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
00124 basic_block def_bb = bb_for_stmt (def_stmt);
00125 struct loop *def_loop;
00126 edge e;
00127 edge_iterator ei;
00128
00129
00130
00131 FOR_EACH_EDGE (e, ei, exit->preds)
00132 {
00133 def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
00134 if (!flow_bb_inside_loop_p (def_loop, e->dest))
00135 break;
00136 }
00137
00138 if (!e)
00139 return;
00140
00141 phi = create_phi_node (use, exit);
00142
00143 FOR_EACH_EDGE (e, ei, exit->preds)
00144 add_phi_arg (phi, use, e);
00145
00146 SSA_NAME_DEF_STMT (use) = def_stmt;
00147 }
00148
00149
00150
00151
00152 static void
00153 add_exit_phis_var (tree var, bitmap livein, bitmap exits)
00154 {
00155 bitmap def;
00156 unsigned index;
00157 basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
00158 bitmap_iterator bi;
00159
00160 bitmap_clear_bit (livein, def_bb->index);
00161
00162 def = BITMAP_ALLOC (NULL);
00163 bitmap_set_bit (def, def_bb->index);
00164 compute_global_livein (livein, def);
00165 BITMAP_FREE (def);
00166
00167 EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index, bi)
00168 {
00169 add_exit_phis_edge (BASIC_BLOCK (index), var);
00170 }
00171 }
00172
00173
00174
00175
00176
00177 static void
00178 add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
00179 {
00180 unsigned i;
00181 bitmap_iterator bi;
00182
00183 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
00184 {
00185 add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
00186 }
00187 }
00188
00189
00190
00191 static bitmap
00192 get_loops_exits (void)
00193 {
00194 bitmap exits = BITMAP_ALLOC (NULL);
00195 basic_block bb;
00196 edge e;
00197 edge_iterator ei;
00198
00199 FOR_EACH_BB (bb)
00200 {
00201 FOR_EACH_EDGE (e, ei, bb->preds)
00202 if (e->src != ENTRY_BLOCK_PTR
00203 && !flow_bb_inside_loop_p (e->src->loop_father, bb))
00204 {
00205 bitmap_set_bit (exits, bb->index);
00206 break;
00207 }
00208 }
00209
00210 return exits;
00211 }
00212
00213
00214
00215
00216
00217 static void
00218 find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks)
00219 {
00220 unsigned ver;
00221 basic_block def_bb;
00222 struct loop *def_loop;
00223
00224 if (TREE_CODE (use) != SSA_NAME)
00225 return;
00226
00227 ver = SSA_NAME_VERSION (use);
00228 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
00229 if (!def_bb)
00230 return;
00231 def_loop = def_bb->loop_father;
00232
00233
00234 if (!def_loop->outer)
00235 return;
00236
00237 if (!use_blocks[ver])
00238 use_blocks[ver] = BITMAP_ALLOC (NULL);
00239 bitmap_set_bit (use_blocks[ver], bb->index);
00240
00241 if (!flow_bb_inside_loop_p (def_loop, bb))
00242 mark_for_rewrite (use);
00243 }
00244
00245
00246
00247
00248
00249 static void
00250 find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks)
00251 {
00252 ssa_op_iter iter;
00253 tree var;
00254 basic_block bb = bb_for_stmt (stmt);
00255
00256 get_stmt_operands (stmt);
00257
00258 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
00259 find_uses_to_rename_use (bb, var, use_blocks);
00260 }
00261
00262
00263
00264
00265
00266 static void
00267 find_uses_to_rename (bitmap *use_blocks)
00268 {
00269 basic_block bb;
00270 block_stmt_iterator bsi;
00271 tree phi;
00272 unsigned i;
00273
00274 FOR_EACH_BB (bb)
00275 {
00276 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00277 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
00278 find_uses_to_rename_use (EDGE_PRED (bb, i)->src,
00279 PHI_ARG_DEF (phi, i), use_blocks);
00280
00281 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00282 find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks);
00283 }
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 void
00313 rewrite_into_loop_closed_ssa (void)
00314 {
00315 bitmap loop_exits = get_loops_exits ();
00316 bitmap *use_blocks;
00317 unsigned i;
00318 bitmap names_to_rename;
00319
00320 gcc_assert (!any_marked_for_rewrite_p ());
00321
00322 use_blocks = xcalloc (num_ssa_names, sizeof (bitmap));
00323
00324
00325 find_uses_to_rename (use_blocks);
00326
00327
00328
00329 names_to_rename = marked_ssa_names ();
00330 add_exit_phis (names_to_rename, use_blocks, loop_exits);
00331
00332 for (i = 0; i < num_ssa_names; i++)
00333 BITMAP_FREE (use_blocks[i]);
00334 free (use_blocks);
00335 BITMAP_FREE (loop_exits);
00336 BITMAP_FREE (names_to_rename);
00337
00338
00339 rewrite_ssa_into_ssa ();
00340 }
00341
00342
00343
00344 static void
00345 check_loop_closed_ssa_use (basic_block bb, tree use)
00346 {
00347 tree def;
00348 basic_block def_bb;
00349
00350 if (TREE_CODE (use) != SSA_NAME)
00351 return;
00352
00353 def = SSA_NAME_DEF_STMT (use);
00354 def_bb = bb_for_stmt (def);
00355 gcc_assert (!def_bb
00356 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
00357 }
00358
00359
00360
00361 static void
00362 check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
00363 {
00364 ssa_op_iter iter;
00365 tree var;
00366
00367 get_stmt_operands (stmt);
00368
00369 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
00370 check_loop_closed_ssa_use (bb, var);
00371 }
00372
00373
00374
00375 void
00376 verify_loop_closed_ssa (void)
00377 {
00378 basic_block bb;
00379 block_stmt_iterator bsi;
00380 tree phi;
00381 unsigned i;
00382
00383 verify_ssa ();
00384
00385 FOR_EACH_BB (bb)
00386 {
00387 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00388 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
00389 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
00390 PHI_ARG_DEF (phi, i));
00391
00392 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00393 check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));
00394 }
00395 }
00396
00397
00398
00399
00400 void
00401 split_loop_exit_edge (edge exit)
00402 {
00403 basic_block dest = exit->dest;
00404 basic_block bb = loop_split_edge_with (exit, NULL);
00405 tree phi, new_phi, new_name, name;
00406 use_operand_p op_p;
00407
00408 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
00409 {
00410 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, EDGE_SUCC (bb, 0));
00411
00412 name = USE_FROM_PTR (op_p);
00413
00414
00415
00416 if (TREE_CODE (name) != SSA_NAME)
00417 continue;
00418
00419
00420
00421 new_name = duplicate_ssa_name (name, NULL);
00422 new_phi = create_phi_node (new_name, bb);
00423 SSA_NAME_DEF_STMT (new_name) = new_phi;
00424 add_phi_arg (new_phi, name, exit);
00425 SET_USE (op_p, new_name);
00426 }
00427 }
00428
00429
00430
00431
00432 basic_block
00433 bsi_insert_on_edge_immediate_loop (edge e, tree stmt)
00434 {
00435 basic_block src, dest, new_bb;
00436 struct loop *loop_c;
00437
00438 src = e->src;
00439 dest = e->dest;
00440
00441 loop_c = find_common_loop (src->loop_father, dest->loop_father);
00442
00443 new_bb = bsi_insert_on_edge_immediate (e, stmt);
00444
00445 if (!new_bb)
00446 return NULL;
00447
00448 add_bb_to_loop (new_bb, loop_c);
00449 if (dest->loop_father->latch == src)
00450 dest->loop_father->latch = new_bb;
00451
00452 return new_bb;
00453 }
00454
00455
00456
00457
00458 basic_block
00459 ip_end_pos (struct loop *loop)
00460 {
00461 return loop->latch;
00462 }
00463
00464
00465
00466
00467 basic_block
00468 ip_normal_pos (struct loop *loop)
00469 {
00470 tree last;
00471 basic_block bb;
00472 edge exit;
00473
00474 if (EDGE_COUNT (loop->latch->preds) > 1)
00475 return NULL;
00476
00477 bb = EDGE_PRED (loop->latch, 0)->src;
00478 last = last_stmt (bb);
00479 if (TREE_CODE (last) != COND_EXPR)
00480 return NULL;
00481
00482 exit = EDGE_SUCC (bb, 0);
00483 if (exit->dest == loop->latch)
00484 exit = EDGE_SUCC (bb, 1);
00485
00486 if (flow_bb_inside_loop_p (loop, exit->dest))
00487 return NULL;
00488
00489 return bb;
00490 }
00491
00492
00493
00494
00495
00496
00497 void
00498 standard_iv_increment_position (struct loop *loop, block_stmt_iterator *bsi,
00499 bool *insert_after)
00500 {
00501 basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
00502 tree last = last_stmt (latch);
00503
00504 if (!bb
00505 || (last && TREE_CODE (last) != LABEL_EXPR))
00506 {
00507 *bsi = bsi_last (latch);
00508 *insert_after = true;
00509 }
00510 else
00511 {
00512 *bsi = bsi_last (bb);
00513 *insert_after = false;
00514 }
00515 }
00516
00517
00518
00519
00520 static void
00521 copy_phi_node_args (unsigned first_new_block)
00522 {
00523 unsigned i;
00524
00525 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00526 BASIC_BLOCK (i)->rbi->duplicated = 1;
00527
00528 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00529 add_phi_args_after_copy_bb (BASIC_BLOCK (i));
00530
00531 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00532 BASIC_BLOCK (i)->rbi->duplicated = 0;
00533 }
00534
00535
00536
00537
00538
00539 static void
00540 rename_variables (unsigned first_new_block, bitmap definitions)
00541 {
00542 unsigned i, copy_number = 0;
00543 basic_block bb;
00544 htab_t ssa_name_map = NULL;
00545
00546 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00547 {
00548 bb = BASIC_BLOCK (i);
00549
00550
00551
00552 if (copy_number != (unsigned) bb->rbi->copy_number)
00553 {
00554 allocate_ssa_names (definitions, &ssa_name_map);
00555 copy_number = bb->rbi->copy_number;
00556 }
00557
00558 rewrite_to_new_ssa_names_bb (bb, ssa_name_map);
00559 }
00560
00561 htab_delete (ssa_name_map);
00562 }
00563
00564
00565
00566 static void
00567 set_phi_def_stmts (basic_block bb)
00568 {
00569 tree phi;
00570
00571 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00572 SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = phi;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 bool
00584 tree_duplicate_loop_to_header_edge (struct loop *loop, edge e,
00585 struct loops *loops,
00586 unsigned int ndupl, sbitmap wont_exit,
00587 edge orig, edge *to_remove,
00588 unsigned int *n_to_remove, int flags)
00589 {
00590 unsigned first_new_block;
00591 basic_block bb;
00592 unsigned i;
00593 bitmap definitions;
00594
00595 if (!(loops->state & LOOPS_HAVE_SIMPLE_LATCHES))
00596 return false;
00597 if (!(loops->state & LOOPS_HAVE_PREHEADERS))
00598 return false;
00599
00600 #ifdef ENABLE_CHECKING
00601 verify_loop_closed_ssa ();
00602 #endif
00603
00604 gcc_assert (!any_marked_for_rewrite_p ());
00605
00606 first_new_block = last_basic_block;
00607 if (!duplicate_loop_to_header_edge (loop, e, loops, ndupl, wont_exit,
00608 orig, to_remove, n_to_remove, flags))
00609 return false;
00610
00611
00612 flush_pending_stmts (e);
00613
00614
00615 copy_phi_node_args (first_new_block);
00616
00617
00618 definitions = marked_ssa_names ();
00619 rename_variables (first_new_block, definitions);
00620 unmark_all_for_rewrite ();
00621 BITMAP_FREE (definitions);
00622
00623
00624
00625
00626
00627
00628
00629 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
00630 {
00631 bb = BASIC_BLOCK (i);
00632 set_phi_def_stmts (bb);
00633 if (bb->rbi->copy_number == 1)
00634 set_phi_def_stmts (bb->rbi->original);
00635 }
00636
00637 scev_reset ();
00638 #ifdef ENABLE_CHECKING
00639 verify_loop_closed_ssa ();
00640 #endif
00641
00642 return true;
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 static void
00659 lv_adjust_loop_header_phi (basic_block first, basic_block second,
00660 basic_block new_head, edge e)
00661 {
00662 tree phi1, phi2;
00663
00664
00665
00666
00667 for (phi2 = phi_nodes (second), phi1 = phi_nodes (first);
00668 phi2 && phi1;
00669 phi2 = PHI_CHAIN (phi2), phi1 = PHI_CHAIN (phi1))
00670 {
00671 edge e2 = find_edge (new_head, second);
00672
00673 if (e2)
00674 {
00675 tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
00676 add_phi_arg (phi1, def, e);
00677 }
00678 }
00679 }
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695 static basic_block
00696 lv_adjust_loop_entry_edge (basic_block first_head,
00697 basic_block second_head,
00698 edge e,
00699 tree cond_expr)
00700 {
00701 block_stmt_iterator bsi;
00702 basic_block new_head = NULL;
00703 tree goto1 = NULL_TREE;
00704 tree goto2 = NULL_TREE;
00705 tree new_cond_expr = NULL_TREE;
00706 edge e0, e1;
00707
00708 gcc_assert (e->dest == second_head);
00709
00710
00711
00712 new_head = split_edge (e);
00713
00714
00715 goto1 = build1 (GOTO_EXPR, void_type_node, tree_block_label (first_head));
00716 goto2 = build1 (GOTO_EXPR, void_type_node, tree_block_label (second_head));
00717 new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr, goto1, goto2);
00718
00719
00720 bsi = bsi_start (new_head);
00721 bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
00722
00723
00724
00725 e0 = EDGE_SUCC (new_head, 0);
00726 e0->flags &= ~EDGE_FALLTHRU;
00727 e0->flags |= EDGE_FALSE_VALUE;
00728 e1 = make_edge (new_head, first_head, EDGE_TRUE_VALUE);
00729 set_immediate_dominator (CDI_DOMINATORS, first_head, new_head);
00730 set_immediate_dominator (CDI_DOMINATORS, second_head, new_head);
00731
00732
00733 lv_adjust_loop_header_phi (first_head, second_head, new_head, e1);
00734
00735 return new_head;
00736 }
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747 struct loop *
00748 tree_ssa_loop_version (struct loops *loops, struct loop * loop,
00749 tree cond_expr, basic_block *condition_bb)
00750 {
00751 edge entry, latch_edge, exit, true_edge, false_edge;
00752 basic_block first_head, second_head;
00753 int irred_flag;
00754 struct loop *nloop;
00755
00756
00757 if (loop->inner)
00758 return NULL;
00759
00760
00761 entry = loop_preheader_edge (loop);
00762
00763
00764 first_head = entry->dest;
00765
00766
00767 irred_flag = entry->flags & EDGE_IRREDUCIBLE_LOOP;
00768 entry->flags &= ~EDGE_IRREDUCIBLE_LOOP;
00769 if (!tree_duplicate_loop_to_header_edge (loop, entry, loops, 1,
00770 NULL, NULL, NULL, NULL, 0))
00771 {
00772 entry->flags |= irred_flag;
00773 return NULL;
00774 }
00775
00776
00777
00778 second_head = entry->dest;
00779
00780
00781 *condition_bb = lv_adjust_loop_entry_edge (first_head, second_head, entry,
00782 cond_expr);
00783
00784 latch_edge = EDGE_SUCC (loop->latch->rbi->copy, 0);
00785
00786 extract_true_false_edges_from_block (*condition_bb, &true_edge, &false_edge);
00787 nloop = loopify (loops,
00788 latch_edge,
00789 EDGE_PRED (loop->header->rbi->copy, 0),
00790 *condition_bb, true_edge, false_edge,
00791 false );
00792
00793 exit = loop->single_exit;
00794 if (exit)
00795 nloop->single_exit = find_edge (exit->src->rbi->copy, exit->dest);
00796
00797
00798 flush_pending_stmts (latch_edge);
00799
00800
00801 extract_true_false_edges_from_block (*condition_bb, &true_edge, &false_edge);
00802 flush_pending_stmts (false_edge);
00803
00804
00805 if (irred_flag)
00806 {
00807 (*condition_bb)->flags |= BB_IRREDUCIBLE_LOOP;
00808 loop_preheader_edge (loop)->flags |= EDGE_IRREDUCIBLE_LOOP;
00809 loop_preheader_edge (nloop)->flags |= EDGE_IRREDUCIBLE_LOOP;
00810 EDGE_PRED ((*condition_bb), 0)->flags |= EDGE_IRREDUCIBLE_LOOP;
00811 }
00812
00813
00814
00815
00816 loop_split_edge_with (loop_preheader_edge (loop), NULL);
00817 loop_split_edge_with (loop_preheader_edge (nloop), NULL);
00818
00819 return nloop;
00820 }