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, 51 Franklin Street, Fifth Floor, 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"
41 /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
42 It is expected that neither BASE nor STEP are shared with other expressions
43 (unless the sharing rules allow this). Use VAR as a base var_decl for it
44 (if NULL, a new temporary will be created). The increment will occur at
45 INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
46 AFTER can be computed using standard_iv_increment_position. The ssa versions
47 of the variable before and after increment will be stored in VAR_BEFORE and
48 VAR_AFTER (unless they are NULL). */
51 create_iv (tree base
, tree step
, tree var
, struct loop
*loop
,
52 block_stmt_iterator
*incr_pos
, bool after
,
53 tree
*var_before
, tree
*var_after
)
55 tree stmt
, initial
, step1
, stmts
;
57 enum tree_code incr_op
= PLUS_EXPR
;
58 edge pe
= loop_preheader_edge (loop
);
62 var
= create_tmp_var (TREE_TYPE (base
), "ivtmp");
63 add_referenced_tmp_var (var
);
66 vb
= make_ssa_name (var
, NULL_TREE
);
69 va
= make_ssa_name (var
, NULL_TREE
);
73 /* For easier readability of the created code, produce MINUS_EXPRs
75 if (TREE_CODE (step
) == INTEGER_CST
)
77 if (TYPE_UNSIGNED (TREE_TYPE (step
)))
79 step1
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
80 if (tree_int_cst_lt (step1
, step
))
88 if (!tree_expr_nonnegative_p (step
)
89 && may_negate_without_overflow_p (step
))
92 step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
97 /* Gimplify the step if necessary. We put the computations in front of the
98 loop (i.e. the step should be loop invariant). */
99 step
= force_gimple_operand (step
, &stmts
, true, var
);
101 bsi_insert_on_edge_immediate_loop (pe
, stmts
);
103 stmt
= build2 (MODIFY_EXPR
, void_type_node
, va
,
104 build2 (incr_op
, TREE_TYPE (base
),
106 SSA_NAME_DEF_STMT (va
) = stmt
;
108 bsi_insert_after (incr_pos
, stmt
, BSI_NEW_STMT
);
110 bsi_insert_before (incr_pos
, stmt
, BSI_NEW_STMT
);
112 initial
= force_gimple_operand (base
, &stmts
, true, var
);
114 bsi_insert_on_edge_immediate_loop (pe
, stmts
);
116 stmt
= create_phi_node (vb
, loop
->header
);
117 SSA_NAME_DEF_STMT (vb
) = stmt
;
118 add_phi_arg (stmt
, initial
, loop_preheader_edge (loop
));
119 add_phi_arg (stmt
, va
, loop_latch_edge (loop
));
122 /* Add exit phis for the USE on EXIT. */
125 add_exit_phis_edge (basic_block exit
, tree use
)
127 tree phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
128 basic_block def_bb
= bb_for_stmt (def_stmt
);
129 struct loop
*def_loop
;
133 /* Check that some of the edges entering the EXIT block exits a loop in
134 that USE is defined. */
135 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
137 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
138 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
145 phi
= create_phi_node (use
, exit
);
146 create_new_def_for (PHI_RESULT (phi
), phi
, PHI_RESULT_PTR (phi
));
147 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
148 add_phi_arg (phi
, use
, e
);
151 /* Add exit phis for VAR that is used in LIVEIN.
152 Exits of the loops are stored in EXITS. */
155 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
159 basic_block def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (var
));
162 if (is_gimple_reg (var
))
163 bitmap_clear_bit (livein
, def_bb
->index
);
165 bitmap_set_bit (livein
, def_bb
->index
);
167 def
= BITMAP_ALLOC (NULL
);
168 bitmap_set_bit (def
, def_bb
->index
);
169 compute_global_livein (livein
, def
);
172 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
, bi
)
174 add_exit_phis_edge (BASIC_BLOCK (index
), var
);
178 /* Add exit phis for the names marked in NAMES_TO_RENAME.
179 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
180 names are used are stored in USE_BLOCKS. */
183 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
188 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
, bi
)
190 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
194 /* Returns a bitmap of all loop exit edge targets. */
197 get_loops_exits (void)
199 bitmap exits
= BITMAP_ALLOC (NULL
);
206 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
207 if (e
->src
!= ENTRY_BLOCK_PTR
208 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
210 bitmap_set_bit (exits
, bb
->index
);
218 /* For USE in BB, if it is used outside of the loop it is defined in,
219 mark it for rewrite. Record basic block BB where it is used
220 to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
223 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
,
228 struct loop
*def_loop
;
230 if (TREE_CODE (use
) != SSA_NAME
)
233 /* We don't need to keep virtual operands in loop-closed form. */
234 if (!is_gimple_reg (use
))
237 ver
= SSA_NAME_VERSION (use
);
238 def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (use
));
241 def_loop
= def_bb
->loop_father
;
243 /* If the definition is not inside loop, it is not interesting. */
244 if (!def_loop
->outer
)
247 if (!use_blocks
[ver
])
248 use_blocks
[ver
] = BITMAP_ALLOC (NULL
);
249 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
251 bitmap_set_bit (need_phis
, ver
);
254 /* For uses in STMT, mark names that are used outside of the loop they are
255 defined to rewrite. Record the set of blocks in that the ssa
256 names are defined to USE_BLOCKS and the ssa names themselves to
260 find_uses_to_rename_stmt (tree stmt
, bitmap
*use_blocks
, bitmap need_phis
)
264 basic_block bb
= bb_for_stmt (stmt
);
266 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
| SSA_OP_ALL_KILLS
)
267 find_uses_to_rename_use (bb
, var
, use_blocks
, need_phis
);
270 /* Marks names that are used in BB and outside of the loop they are
271 defined in for rewrite. Records the set of blocks in that the ssa
272 names are defined to USE_BLOCKS. Record the SSA names that will
273 need exit PHIs in NEED_PHIS. */
276 find_uses_to_rename_bb (basic_block bb
, bitmap
*use_blocks
, bitmap need_phis
)
278 block_stmt_iterator bsi
;
283 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
284 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
285 find_uses_to_rename_use (bb
, PHI_ARG_DEF_FROM_EDGE (phi
, e
),
286 use_blocks
, need_phis
);
288 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
289 find_uses_to_rename_stmt (bsi_stmt (bsi
), use_blocks
, need_phis
);
292 /* Marks names that are used outside of the loop they are defined in
293 for rewrite. Records the set of blocks in that the ssa
294 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
295 scan only blocks in this set. */
298 find_uses_to_rename (bitmap changed_bbs
, bitmap
*use_blocks
, bitmap need_phis
)
304 if (changed_bbs
&& !bitmap_empty_p (changed_bbs
))
306 EXECUTE_IF_SET_IN_BITMAP (changed_bbs
, 0, index
, bi
)
308 find_uses_to_rename_bb (BASIC_BLOCK (index
), use_blocks
, need_phis
);
315 find_uses_to_rename_bb (bb
, use_blocks
, need_phis
);
320 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
321 phi nodes to ensure that no variable is used outside the loop it is
324 This strengthening of the basic ssa form has several advantages:
326 1) Updating it during unrolling/peeling/versioning is trivial, since
327 we do not need to care about the uses outside of the loop.
328 2) The behavior of all uses of an induction variable is the same.
329 Without this, you need to distinguish the case when the variable
330 is used outside of the loop it is defined in, for example
332 for (i = 0; i < 100; i++)
334 for (j = 0; j < 100; j++)
342 Looking from the outer loop with the normal SSA form, the first use of k
343 is not well-behaved, while the second one is an induction variable with
346 If CHANGED_BBS is not NULL, we look for uses outside loops only in
347 the basic blocks in this set.
349 UPDATE_FLAG is used in the call to update_ssa. See
350 TODO_update_ssa* for documentation. */
353 rewrite_into_loop_closed_ssa (bitmap changed_bbs
, unsigned update_flag
)
355 bitmap loop_exits
= get_loops_exits ();
357 unsigned i
, old_num_ssa_names
;
358 bitmap names_to_rename
= BITMAP_ALLOC (NULL
);
360 /* If the pass has caused the SSA form to be out-of-date, update it
362 update_ssa (update_flag
);
364 old_num_ssa_names
= num_ssa_names
;
365 use_blocks
= XCNEWVEC (bitmap
, old_num_ssa_names
);
367 /* Find the uses outside loops. */
368 find_uses_to_rename (changed_bbs
, use_blocks
, names_to_rename
);
370 /* Add the PHI nodes on exits of the loops for the names we need to
372 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
374 for (i
= 0; i
< old_num_ssa_names
; i
++)
375 BITMAP_FREE (use_blocks
[i
]);
377 BITMAP_FREE (loop_exits
);
378 BITMAP_FREE (names_to_rename
);
380 /* Fix up all the names found to be used outside their original
382 update_ssa (TODO_update_ssa
);
385 /* Check invariants of the loop closed ssa form for the USE in BB. */
388 check_loop_closed_ssa_use (basic_block bb
, tree use
)
393 if (TREE_CODE (use
) != SSA_NAME
|| !is_gimple_reg (use
))
396 def
= SSA_NAME_DEF_STMT (use
);
397 def_bb
= bb_for_stmt (def
);
399 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
402 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
405 check_loop_closed_ssa_stmt (basic_block bb
, tree stmt
)
410 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
| SSA_OP_ALL_KILLS
)
411 check_loop_closed_ssa_use (bb
, var
);
414 /* Checks that invariants of the loop closed ssa form are preserved. */
417 verify_loop_closed_ssa (void)
420 block_stmt_iterator bsi
;
424 if (current_loops
== NULL
)
431 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
432 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
433 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi
, i
)->src
,
434 PHI_ARG_DEF (phi
, i
));
436 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
437 check_loop_closed_ssa_stmt (bb
, bsi_stmt (bsi
));
441 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
442 preserve the loop closed ssa form. */
445 split_loop_exit_edge (edge exit
)
447 basic_block dest
= exit
->dest
;
448 basic_block bb
= loop_split_edge_with (exit
, NULL
);
449 tree phi
, new_phi
, new_name
, name
;
452 for (phi
= phi_nodes (dest
); phi
; phi
= PHI_CHAIN (phi
))
454 op_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, single_succ_edge (bb
));
456 name
= USE_FROM_PTR (op_p
);
458 /* If the argument of the phi node is a constant, we do not need
459 to keep it inside loop. */
460 if (TREE_CODE (name
) != SSA_NAME
)
463 /* Otherwise create an auxiliary phi node that will copy the value
464 of the ssa name out of the loop. */
465 new_name
= duplicate_ssa_name (name
, NULL
);
466 new_phi
= create_phi_node (new_name
, bb
);
467 SSA_NAME_DEF_STMT (new_name
) = new_phi
;
468 add_phi_arg (new_phi
, name
, exit
);
469 SET_USE (op_p
, new_name
);
473 /* Insert statement STMT to the edge E and update the loop structures.
474 Returns the newly created block (if any). */
477 bsi_insert_on_edge_immediate_loop (edge e
, tree stmt
)
479 basic_block src
, dest
, new_bb
;
485 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
487 new_bb
= bsi_insert_on_edge_immediate (e
, stmt
);
492 add_bb_to_loop (new_bb
, loop_c
);
493 if (dest
->loop_father
->latch
== src
)
494 dest
->loop_father
->latch
= new_bb
;
499 /* Returns the basic block in that statements should be emitted for induction
500 variables incremented at the end of the LOOP. */
503 ip_end_pos (struct loop
*loop
)
508 /* Returns the basic block in that statements should be emitted for induction
509 variables incremented just before exit condition of a LOOP. */
512 ip_normal_pos (struct loop
*loop
)
518 if (!single_pred_p (loop
->latch
))
521 bb
= single_pred (loop
->latch
);
522 last
= last_stmt (bb
);
523 if (TREE_CODE (last
) != COND_EXPR
)
526 exit
= EDGE_SUCC (bb
, 0);
527 if (exit
->dest
== loop
->latch
)
528 exit
= EDGE_SUCC (bb
, 1);
530 if (flow_bb_inside_loop_p (loop
, exit
->dest
))
536 /* Stores the standard position for induction variable increment in LOOP
537 (just before the exit condition if it is available and latch block is empty,
538 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
539 the increment should be inserted after *BSI. */
542 standard_iv_increment_position (struct loop
*loop
, block_stmt_iterator
*bsi
,
545 basic_block bb
= ip_normal_pos (loop
), latch
= ip_end_pos (loop
);
546 tree last
= last_stmt (latch
);
549 || (last
&& TREE_CODE (last
) != LABEL_EXPR
))
551 *bsi
= bsi_last (latch
);
552 *insert_after
= true;
556 *bsi
= bsi_last (bb
);
557 *insert_after
= false;
561 /* Copies phi node arguments for duplicated blocks. The index of the first
562 duplicated block is FIRST_NEW_BLOCK. */
565 copy_phi_node_args (unsigned first_new_block
)
569 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
570 BASIC_BLOCK (i
)->flags
|= BB_DUPLICATED
;
572 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
573 add_phi_args_after_copy_bb (BASIC_BLOCK (i
));
575 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
576 BASIC_BLOCK (i
)->flags
&= ~BB_DUPLICATED
;
580 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
581 updates the PHI nodes at start of the copied region. In order to
582 achieve this, only loops whose exits all lead to the same location
585 Notice that we do not completely update the SSA web after
586 duplication. The caller is responsible for calling update_ssa
587 after the loop has been duplicated. */
590 tree_duplicate_loop_to_header_edge (struct loop
*loop
, edge e
,
592 unsigned int ndupl
, sbitmap wont_exit
,
593 edge orig
, edge
*to_remove
,
594 unsigned int *n_to_remove
, int flags
)
596 unsigned first_new_block
;
598 if (!(loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
))
600 if (!(loops
->state
& LOOPS_HAVE_PREHEADERS
))
603 #ifdef ENABLE_CHECKING
604 verify_loop_closed_ssa ();
607 first_new_block
= last_basic_block
;
608 if (!duplicate_loop_to_header_edge (loop
, e
, loops
, ndupl
, wont_exit
,
609 orig
, to_remove
, n_to_remove
, flags
))
612 /* Readd the removed phi args for e. */
613 flush_pending_stmts (e
);
615 /* Copy the phi node arguments. */
616 copy_phi_node_args (first_new_block
);
623 /* Build if (COND) goto THEN_LABEL; else goto ELSE_LABEL; */
626 build_if_stmt (tree cond
, tree then_label
, tree else_label
)
628 return build3 (COND_EXPR
, void_type_node
,
630 build1 (GOTO_EXPR
, void_type_node
, then_label
),
631 build1 (GOTO_EXPR
, void_type_node
, else_label
));
634 /* Returns true if we can unroll LOOP FACTOR times. Number
635 of iterations of the loop is returned in NITER. */
638 can_unroll_loop_p (struct loop
*loop
, unsigned factor
,
639 struct tree_niter_desc
*niter
)
643 /* Check whether unrolling is possible. We only want to unroll loops
644 for that we are able to determine number of iterations. We also
645 want to split the extra iterations of the loop from its end,
646 therefore we require that the loop has precisely one
649 exit
= single_dom_exit (loop
);
653 if (!number_of_iterations_exit (loop
, exit
, niter
, false)
654 || niter
->cmp
== ERROR_MARK
)
657 /* And of course, we must be able to duplicate the loop. */
658 if (!can_duplicate_loop_p (loop
))
661 /* The final loop should be small enough. */
662 if (tree_num_loop_insns (loop
) * factor
663 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
))
669 /* Determines the conditions that control execution of LOOP unrolled FACTOR
670 times. DESC is number of iterations of LOOP. ENTER_COND is set to
671 condition that must be true if the main loop can be entered.
672 EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
673 how the exit from the unrolled loop should be controlled. */
676 determine_exit_conditions (struct loop
*loop
, struct tree_niter_desc
*desc
,
677 unsigned factor
, tree
*enter_cond
,
678 tree
*exit_base
, tree
*exit_step
,
679 enum tree_code
*exit_cmp
, tree
*exit_bound
)
682 tree base
= desc
->control
.base
;
683 tree step
= desc
->control
.step
;
684 tree bound
= desc
->bound
;
685 tree type
= TREE_TYPE (base
);
687 tree min
= lower_bound_in_type (type
, type
);
688 tree max
= upper_bound_in_type (type
, type
);
689 enum tree_code cmp
= desc
->cmp
;
690 tree cond
= boolean_true_node
, assum
;
692 *enter_cond
= boolean_false_node
;
693 *exit_base
= NULL_TREE
;
694 *exit_step
= NULL_TREE
;
695 *exit_cmp
= ERROR_MARK
;
696 *exit_bound
= NULL_TREE
;
697 gcc_assert (cmp
!= ERROR_MARK
);
699 /* We only need to be correct when we answer question
700 "Do at least FACTOR more iterations remain?" in the unrolled loop.
701 Thus, transforming BASE + STEP * i <> BOUND to
702 BASE + STEP * i < BOUND is ok. */
705 if (tree_int_cst_sign_bit (step
))
710 else if (cmp
== LT_EXPR
)
712 gcc_assert (!tree_int_cst_sign_bit (step
));
714 else if (cmp
== GT_EXPR
)
716 gcc_assert (tree_int_cst_sign_bit (step
));
721 /* The main body of the loop may be entered iff:
723 1) desc->may_be_zero is false.
724 2) it is possible to check that there are at least FACTOR iterations
725 of the loop, i.e., BOUND - step * FACTOR does not overflow.
726 3) # of iterations is at least FACTOR */
728 if (!zero_p (desc
->may_be_zero
))
729 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
730 invert_truthvalue (desc
->may_be_zero
),
733 bigstep
= fold_build2 (MULT_EXPR
, type
, step
,
734 build_int_cst_type (type
, factor
));
735 delta
= fold_build2 (MINUS_EXPR
, type
, bigstep
, step
);
737 assum
= fold_build2 (GE_EXPR
, boolean_type_node
,
739 fold_build2 (PLUS_EXPR
, type
, min
, delta
));
741 assum
= fold_build2 (LE_EXPR
, boolean_type_node
,
743 fold_build2 (PLUS_EXPR
, type
, max
, delta
));
744 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
746 bound
= fold_build2 (MINUS_EXPR
, type
, bound
, delta
);
747 assum
= fold_build2 (cmp
, boolean_type_node
, base
, bound
);
748 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
750 cond
= force_gimple_operand (unshare_expr (cond
), &stmts
, false, NULL_TREE
);
752 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop
), stmts
);
753 /* cond now may be a gimple comparison, which would be OK, but also any
754 other gimple rhs (say a && b). In this case we need to force it to
756 if (!is_gimple_condexpr (cond
))
758 cond
= force_gimple_operand (cond
, &stmts
, true, NULL_TREE
);
760 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop
), stmts
);
764 base
= force_gimple_operand (unshare_expr (base
), &stmts
, true, NULL_TREE
);
766 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop
), stmts
);
767 bound
= force_gimple_operand (unshare_expr (bound
), &stmts
, true, NULL_TREE
);
769 bsi_insert_on_edge_immediate_loop (loop_preheader_edge (loop
), stmts
);
772 *exit_step
= bigstep
;
777 /* Unroll LOOP FACTOR times. LOOPS is the loops tree. DESC describes
778 number of iterations of LOOP. EXIT is the exit of the loop to that
781 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
782 under that loop exits in the first iteration even if N != 0,
786 x = phi (init, next);
794 becomes (with possibly the exit conditions formulated a bit differently,
795 avoiding the need to create a new iv):
797 if (MAY_BE_ZERO || N < FACTOR)
802 x = phi (init, next);
813 } while (N >= FACTOR);
816 init' = phi (init, x);
820 x = phi (init', next);
829 tree_unroll_loop (struct loops
*loops
, struct loop
*loop
, unsigned factor
,
830 edge exit
, struct tree_niter_desc
*desc
)
832 tree dont_exit
, exit_if
, ctr_before
, ctr_after
;
833 tree enter_main_cond
, exit_base
, exit_step
, exit_bound
;
834 enum tree_code exit_cmp
;
835 tree phi_old_loop
, phi_new_loop
, phi_rest
, init
, next
, new_init
, var
;
836 struct loop
*new_loop
;
837 basic_block rest
, exit_bb
;
838 edge old_entry
, new_entry
, old_latch
, precond_edge
, new_exit
;
839 edge nonexit
, new_nonexit
;
840 block_stmt_iterator bsi
;
846 est_niter
= expected_loop_iterations (loop
);
847 determine_exit_conditions (loop
, desc
, factor
,
848 &enter_main_cond
, &exit_base
, &exit_step
,
849 &exit_cmp
, &exit_bound
);
851 new_loop
= loop_version (loops
, loop
, enter_main_cond
, NULL
, true);
852 gcc_assert (new_loop
!= NULL
);
853 update_ssa (TODO_update_ssa
);
855 /* Unroll the loop and remove the old exits. */
856 dont_exit
= ((exit
->flags
& EDGE_TRUE_VALUE
)
858 : boolean_true_node
);
859 if (exit
== EDGE_SUCC (exit
->src
, 0))
860 nonexit
= EDGE_SUCC (exit
->src
, 1);
862 nonexit
= EDGE_SUCC (exit
->src
, 0);
863 nonexit
->probability
= REG_BR_PROB_BASE
;
864 exit
->probability
= 0;
865 nonexit
->count
+= exit
->count
;
867 exit_if
= last_stmt (exit
->src
);
868 COND_EXPR_COND (exit_if
) = dont_exit
;
869 update_stmt (exit_if
);
871 wont_exit
= sbitmap_alloc (factor
);
872 sbitmap_ones (wont_exit
);
873 ok
= tree_duplicate_loop_to_header_edge
874 (loop
, loop_latch_edge (loop
), loops
, factor
- 1,
875 wont_exit
, NULL
, NULL
, NULL
, DLTHE_FLAG_UPDATE_FREQ
);
878 update_ssa (TODO_update_ssa
);
880 /* Prepare the cfg and update the phi nodes. */
881 rest
= loop_preheader_edge (new_loop
)->src
;
882 precond_edge
= single_pred_edge (rest
);
883 loop_split_edge_with (loop_latch_edge (loop
), NULL
);
884 exit_bb
= single_pred (loop
->latch
);
886 new_exit
= make_edge (exit_bb
, rest
, EDGE_FALSE_VALUE
);
887 new_exit
->count
= loop_preheader_edge (loop
)->count
;
888 est_niter
= est_niter
/ factor
+ 1;
889 new_exit
->probability
= REG_BR_PROB_BASE
/ est_niter
;
891 new_nonexit
= single_pred_edge (loop
->latch
);
892 new_nonexit
->flags
= EDGE_TRUE_VALUE
;
893 new_nonexit
->probability
= REG_BR_PROB_BASE
- new_exit
->probability
;
895 old_entry
= loop_preheader_edge (loop
);
896 new_entry
= loop_preheader_edge (new_loop
);
897 old_latch
= loop_latch_edge (loop
);
898 for (phi_old_loop
= phi_nodes (loop
->header
),
899 phi_new_loop
= phi_nodes (new_loop
->header
);
901 phi_old_loop
= PHI_CHAIN (phi_old_loop
),
902 phi_new_loop
= PHI_CHAIN (phi_new_loop
))
904 init
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_entry
);
905 op
= PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop
, new_entry
);
906 gcc_assert (operand_equal_for_phi_arg_p (init
, USE_FROM_PTR (op
)));
907 next
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_latch
);
909 /* Prefer using original variable as a base for the new ssa name.
910 This is necessary for virtual ops, and useful in order to avoid
911 losing debug info for real ops. */
912 if (TREE_CODE (next
) == SSA_NAME
)
913 var
= SSA_NAME_VAR (next
);
914 else if (TREE_CODE (init
) == SSA_NAME
)
915 var
= SSA_NAME_VAR (init
);
918 var
= create_tmp_var (TREE_TYPE (init
), "unrinittmp");
919 add_referenced_tmp_var (var
);
922 new_init
= make_ssa_name (var
, NULL_TREE
);
923 phi_rest
= create_phi_node (new_init
, rest
);
924 SSA_NAME_DEF_STMT (new_init
) = phi_rest
;
926 add_phi_arg (phi_rest
, init
, precond_edge
);
927 add_phi_arg (phi_rest
, next
, new_exit
);
928 SET_USE (op
, new_init
);
931 /* Finally create the new counter for number of iterations and add the new
933 bsi
= bsi_last (exit_bb
);
934 create_iv (exit_base
, exit_step
, NULL_TREE
, loop
,
935 &bsi
, true, &ctr_before
, &ctr_after
);
936 exit_if
= build_if_stmt (build2 (exit_cmp
, boolean_type_node
, ctr_after
,
938 tree_block_label (loop
->latch
),
939 tree_block_label (rest
));
940 bsi_insert_after (&bsi
, exit_if
, BSI_NEW_STMT
);
943 verify_dominators (CDI_DOMINATORS
);
944 verify_loop_structure (loops
);
945 verify_loop_closed_ssa ();