1 /* High-level loop manipulation functions.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
36 #include "tree-pass.h"
37 #include "cfglayout.h"
38 #include "tree-scalar-evolution.h"
40 /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
41 It is expected that neither BASE nor STEP are shared with other expressions
42 (unless the sharing rules allow this). Use VAR as a base var_decl for it
43 (if NULL, a new temporary will be created). The increment will occur at
44 INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
45 AFTER can be computed using standard_iv_increment_position. The ssa versions
46 of the variable before and after increment will be stored in VAR_BEFORE and
47 VAR_AFTER (unless they are NULL). */
50 create_iv (tree base, tree step, tree var, struct loop *loop,
51 block_stmt_iterator *incr_pos, bool after,
52 tree *var_before, tree *var_after)
54 tree stmt, initial, step1, stmts;
56 enum tree_code incr_op = PLUS_EXPR;
60 var = create_tmp_var (TREE_TYPE (base), "ivtmp");
61 add_referenced_tmp_var (var);
64 vb = make_ssa_name (var, NULL_TREE);
67 va = make_ssa_name (var, NULL_TREE);
71 /* For easier readability of the created code, produce MINUS_EXPRs
73 if (TREE_CODE (step) == INTEGER_CST)
75 if (TYPE_UNSIGNED (TREE_TYPE (step)))
77 step1 = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
78 if (tree_int_cst_lt (step1, step))
86 if (!tree_expr_nonnegative_p (step)
87 && may_negate_without_overflow_p (step))
90 step = fold (build1 (NEGATE_EXPR, TREE_TYPE (step), step));
95 stmt = build2 (MODIFY_EXPR, void_type_node, va,
96 build2 (incr_op, TREE_TYPE (base),
98 SSA_NAME_DEF_STMT (va) = stmt;
100 bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
102 bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
104 initial = force_gimple_operand (base, &stmts, true, var);
107 edge pe = loop_preheader_edge (loop);
109 bsi_insert_on_edge_immediate_loop (pe, stmts);
112 stmt = create_phi_node (vb, loop->header);
113 SSA_NAME_DEF_STMT (vb) = stmt;
114 add_phi_arg (stmt, initial, loop_preheader_edge (loop));
115 add_phi_arg (stmt, va, loop_latch_edge (loop));
118 /* Add exit phis for the USE on EXIT. */
121 add_exit_phis_edge (basic_block exit, tree use)
123 tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
124 basic_block def_bb = bb_for_stmt (def_stmt);
125 struct loop *def_loop;
129 /* Check that some of the edges entering the EXIT block exits a loop in
130 that USE is defined. */
131 FOR_EACH_EDGE (e, ei, exit->preds)
133 def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
134 if (!flow_bb_inside_loop_p (def_loop, e->dest))
141 phi = create_phi_node (use, exit);
143 FOR_EACH_EDGE (e, ei, exit->preds)
144 add_phi_arg (phi, use, e);
146 SSA_NAME_DEF_STMT (use) = def_stmt;
149 /* Add exit phis for VAR that is used in LIVEIN.
150 Exits of the loops are stored in EXITS. */
153 add_exit_phis_var (tree var, bitmap livein, bitmap exits)
157 basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
160 if (is_gimple_reg (var))
161 bitmap_clear_bit (livein, def_bb->index);
163 bitmap_set_bit (livein, def_bb->index);
165 def = BITMAP_ALLOC (NULL);
166 bitmap_set_bit (def, def_bb->index);
167 compute_global_livein (livein, def);
170 EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index, bi)
172 add_exit_phis_edge (BASIC_BLOCK (index), var);
176 /* Add exit phis for the names marked in NAMES_TO_RENAME.
177 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
178 names are used are stored in USE_BLOCKS. */
181 add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
186 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
188 add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
192 /* Returns a bitmap of all loop exit edge targets. */
195 get_loops_exits (void)
197 bitmap exits = BITMAP_ALLOC (NULL);
204 FOR_EACH_EDGE (e, ei, bb->preds)
205 if (e->src != ENTRY_BLOCK_PTR
206 && !flow_bb_inside_loop_p (e->src->loop_father, bb))
208 bitmap_set_bit (exits, bb->index);
216 /* For USE in BB, if it is used outside of the loop it is defined in,
217 mark it for rewrite. Record basic block BB where it is used
221 find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks)
225 struct loop *def_loop;
227 if (TREE_CODE (use) != SSA_NAME)
230 ver = SSA_NAME_VERSION (use);
231 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
234 def_loop = def_bb->loop_father;
236 /* If the definition is not inside loop, it is not interesting. */
237 if (!def_loop->outer)
240 if (!use_blocks[ver])
241 use_blocks[ver] = BITMAP_ALLOC (NULL);
242 bitmap_set_bit (use_blocks[ver], bb->index);
244 if (!flow_bb_inside_loop_p (def_loop, bb))
245 mark_for_rewrite (use);
248 /* For uses in STMT, mark names that are used outside of the loop they are
249 defined to rewrite. Record the set of blocks in that the ssa
250 names are defined to USE_BLOCKS. */
253 find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks)
257 basic_block bb = bb_for_stmt (stmt);
259 get_stmt_operands (stmt);
261 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
262 find_uses_to_rename_use (bb, var, use_blocks);
265 /* Marks names that are used in BB and outside of the loop they are
266 defined in for rewrite. Records the set of blocks in that the ssa
267 names are defined to USE_BLOCKS. */
270 find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks)
272 block_stmt_iterator bsi;
277 FOR_EACH_EDGE (e, ei, bb->succs)
278 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
279 find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
282 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
283 find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks);
286 /* Marks names that are used outside of the loop they are defined in
287 for rewrite. Records the set of blocks in that the ssa
288 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
289 scan only blocks in this set. */
292 find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks)
300 EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
302 find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks);
309 find_uses_to_rename_bb (bb, use_blocks);
314 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
315 phi nodes to ensure that no variable is used outside the loop it is
318 This strengthening of the basic ssa form has several advantages:
320 1) Updating it during unrolling/peeling/versioning is trivial, since
321 we do not need to care about the uses outside of the loop.
322 2) The behavior of all uses of an induction variable is the same.
323 Without this, you need to distinguish the case when the variable
324 is used outside of the loop it is defined in, for example
326 for (i = 0; i < 100; i++)
328 for (j = 0; j < 100; j++)
336 Looking from the outer loop with the normal SSA form, the first use of k
337 is not well-behaved, while the second one is an induction variable with
340 If CHANGED_BBS is not NULL, we look for uses outside loops only in
341 the basic blocks in this set. */
344 rewrite_into_loop_closed_ssa (bitmap changed_bbs)
346 bitmap loop_exits = get_loops_exits ();
349 bitmap names_to_rename;
351 gcc_assert (!any_marked_for_rewrite_p ());
353 use_blocks = xcalloc (num_ssa_names, sizeof (bitmap));
355 /* Find the uses outside loops. */
356 find_uses_to_rename (changed_bbs, use_blocks);
358 if (!any_marked_for_rewrite_p ())
361 BITMAP_FREE (loop_exits);
365 /* Add the phi nodes on exits of the loops for the names we need to
367 names_to_rename = marked_ssa_names ();
368 add_exit_phis (names_to_rename, use_blocks, loop_exits);
370 for (i = 0; i < num_ssa_names; i++)
371 BITMAP_FREE (use_blocks[i]);
373 BITMAP_FREE (loop_exits);
374 BITMAP_FREE (names_to_rename);
376 /* Do the rewriting. */
377 rewrite_ssa_into_ssa ();
380 /* Check invariants of the loop closed ssa form for the USE in BB. */
383 check_loop_closed_ssa_use (basic_block bb, tree use)
388 if (TREE_CODE (use) != SSA_NAME)
391 def = SSA_NAME_DEF_STMT (use);
392 def_bb = bb_for_stmt (def);
394 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
397 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
400 check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
405 get_stmt_operands (stmt);
407 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
408 check_loop_closed_ssa_use (bb, var);
411 /* Checks that invariants of the loop closed ssa form are preserved. */
414 verify_loop_closed_ssa (void)
417 block_stmt_iterator bsi;
425 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
426 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
427 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
428 PHI_ARG_DEF (phi, i));
430 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
431 check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));
435 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
436 preserve the loop closed ssa form. */
439 split_loop_exit_edge (edge exit)
441 basic_block dest = exit->dest;
442 basic_block bb = loop_split_edge_with (exit, NULL);
443 tree phi, new_phi, new_name, name;
446 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
448 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
450 name = USE_FROM_PTR (op_p);
452 /* If the argument of the phi node is a constant, we do not need
453 to keep it inside loop. */
454 if (TREE_CODE (name) != SSA_NAME)
457 /* Otherwise create an auxiliary phi node that will copy the value
458 of the ssa name out of the loop. */
459 new_name = duplicate_ssa_name (name, NULL);
460 new_phi = create_phi_node (new_name, bb);
461 SSA_NAME_DEF_STMT (new_name) = new_phi;
462 add_phi_arg (new_phi, name, exit);
463 SET_USE (op_p, new_name);
467 /* Insert statement STMT to the edge E and update the loop structures.
468 Returns the newly created block (if any). */
471 bsi_insert_on_edge_immediate_loop (edge e, tree stmt)
473 basic_block src, dest, new_bb;
479 loop_c = find_common_loop (src->loop_father, dest->loop_father);
481 new_bb = bsi_insert_on_edge_immediate (e, stmt);
486 add_bb_to_loop (new_bb, loop_c);
487 if (dest->loop_father->latch == src)
488 dest->loop_father->latch = new_bb;
493 /* Returns the basic block in that statements should be emitted for induction
494 variables incremented at the end of the LOOP. */
497 ip_end_pos (struct loop *loop)
502 /* Returns the basic block in that statements should be emitted for induction
503 variables incremented just before exit condition of a LOOP. */
506 ip_normal_pos (struct loop *loop)
512 if (!single_pred_p (loop->latch))
515 bb = single_pred (loop->latch);
516 last = last_stmt (bb);
517 if (TREE_CODE (last) != COND_EXPR)
520 exit = EDGE_SUCC (bb, 0);
521 if (exit->dest == loop->latch)
522 exit = EDGE_SUCC (bb, 1);
524 if (flow_bb_inside_loop_p (loop, exit->dest))
530 /* Stores the standard position for induction variable increment in LOOP
531 (just before the exit condition if it is available and latch block is empty,
532 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
533 the increment should be inserted after *BSI. */
536 standard_iv_increment_position (struct loop *loop, block_stmt_iterator *bsi,
539 basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
540 tree last = last_stmt (latch);
543 || (last && TREE_CODE (last) != LABEL_EXPR))
545 *bsi = bsi_last (latch);
546 *insert_after = true;
550 *bsi = bsi_last (bb);
551 *insert_after = false;
555 /* Copies phi node arguments for duplicated blocks. The index of the first
556 duplicated block is FIRST_NEW_BLOCK. */
559 copy_phi_node_args (unsigned first_new_block)
563 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
564 BASIC_BLOCK (i)->rbi->duplicated = 1;
566 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
567 add_phi_args_after_copy_bb (BASIC_BLOCK (i));
569 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
570 BASIC_BLOCK (i)->rbi->duplicated = 0;
573 /* Renames variables in the area copied by tree_duplicate_loop_to_header_edge.
574 FIRST_NEW_BLOCK is the first block in the copied area. DEFINITIONS is
575 a bitmap of all ssa names defined inside the loop. */
578 rename_variables (unsigned first_new_block, bitmap definitions)
580 unsigned i, copy_number = 0;
582 htab_t ssa_name_map = NULL;
584 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
586 bb = BASIC_BLOCK (i);
588 /* We assume that first come all blocks from the first copy, then all
589 blocks from the second copy, etc. */
590 if (copy_number != (unsigned) bb->rbi->copy_number)
592 allocate_ssa_names (definitions, &ssa_name_map);
593 copy_number = bb->rbi->copy_number;
596 rewrite_to_new_ssa_names_bb (bb, ssa_name_map);
599 htab_delete (ssa_name_map);
602 /* Sets SSA_NAME_DEF_STMT for results of all phi nodes in BB. */
605 set_phi_def_stmts (basic_block bb)
609 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
610 SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = phi;
613 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also updates
614 ssa. In order to achieve this, only loops whose exits all lead to the same
615 location are handled.
617 FIXME: we create some degenerate phi nodes that could be avoided by copy
618 propagating them instead. Unfortunately this is not completely
619 straightforward due to problems with constant folding. */
622 tree_duplicate_loop_to_header_edge (struct loop *loop, edge e,
624 unsigned int ndupl, sbitmap wont_exit,
625 edge orig, edge *to_remove,
626 unsigned int *n_to_remove, int flags)
628 unsigned first_new_block;
633 if (!(loops->state & LOOPS_HAVE_SIMPLE_LATCHES))
635 if (!(loops->state & LOOPS_HAVE_PREHEADERS))
638 #ifdef ENABLE_CHECKING
639 verify_loop_closed_ssa ();
642 gcc_assert (!any_marked_for_rewrite_p ());
644 first_new_block = last_basic_block;
645 if (!duplicate_loop_to_header_edge (loop, e, loops, ndupl, wont_exit,
646 orig, to_remove, n_to_remove, flags))
649 /* Readd the removed phi args for e. */
650 flush_pending_stmts (e);
652 /* Copy the phi node arguments. */
653 copy_phi_node_args (first_new_block);
655 /* Rename the variables. */
656 definitions = marked_ssa_names ();
657 rename_variables (first_new_block, definitions);
658 unmark_all_for_rewrite ();
659 BITMAP_FREE (definitions);
661 /* For some time we have the identical ssa names as results in multiple phi
662 nodes. When phi node is resized, it sets SSA_NAME_DEF_STMT of its result
663 to the new copy. This means that we cannot easily ensure that the ssa
664 names defined in those phis are pointing to the right one -- so just
665 recompute SSA_NAME_DEF_STMT for them. */
667 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
669 bb = BASIC_BLOCK (i);
670 set_phi_def_stmts (bb);
671 if (bb->rbi->copy_number == 1)
672 set_phi_def_stmts (bb->rbi->original);
676 #ifdef ENABLE_CHECKING
677 verify_loop_closed_ssa ();