1 /* High-level loop manipulation functions.
2 Copyright (C) 2004, 2005, 2006, 2007 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "cfglayout.h"
37 #include "tree-scalar-evolution.h"
39 #include "tree-inline.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 gimple_stmt_iterator
*incr_pos
, bool after
,
53 tree
*var_before
, tree
*var_after
)
59 enum tree_code incr_op
= PLUS_EXPR
;
60 edge pe
= loop_preheader_edge (loop
);
64 var
= create_tmp_var (TREE_TYPE (base
), "ivtmp");
65 add_referenced_var (var
);
68 vb
= make_ssa_name (var
, NULL
);
71 va
= make_ssa_name (var
, NULL
);
75 /* For easier readability of the created code, produce MINUS_EXPRs
77 if (TREE_CODE (step
) == INTEGER_CST
)
79 if (TYPE_UNSIGNED (TREE_TYPE (step
)))
81 step1
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
82 if (tree_int_cst_lt (step1
, step
))
92 if (!tree_expr_nonnegative_warnv_p (step
, &ovf
)
93 && may_negate_without_overflow_p (step
))
96 step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
100 if (POINTER_TYPE_P (TREE_TYPE (base
)))
102 step
= fold_convert (sizetype
, step
);
103 if (incr_op
== MINUS_EXPR
)
104 step
= fold_build1 (NEGATE_EXPR
, sizetype
, step
);
105 incr_op
= POINTER_PLUS_EXPR
;
107 /* Gimplify the step if necessary. We put the computations in front of the
108 loop (i.e. the step should be loop invariant). */
109 step
= force_gimple_operand (step
, &stmts
, true, NULL_TREE
);
111 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
113 stmt
= gimple_build_assign_with_ops (incr_op
, va
, vb
, step
);
115 gsi_insert_after (incr_pos
, stmt
, GSI_NEW_STMT
);
117 gsi_insert_before (incr_pos
, stmt
, GSI_NEW_STMT
);
119 initial
= force_gimple_operand (base
, &stmts
, true, var
);
121 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
123 stmt
= create_phi_node (vb
, loop
->header
);
124 SSA_NAME_DEF_STMT (vb
) = stmt
;
125 add_phi_arg (stmt
, initial
, loop_preheader_edge (loop
));
126 add_phi_arg (stmt
, va
, loop_latch_edge (loop
));
129 /* Add exit phis for the USE on EXIT. */
132 add_exit_phis_edge (basic_block exit
, tree use
)
134 gimple phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
135 basic_block def_bb
= gimple_bb (def_stmt
);
136 struct loop
*def_loop
;
140 /* Check that some of the edges entering the EXIT block exits a loop in
141 that USE is defined. */
142 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
144 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
145 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
152 phi
= create_phi_node (use
, exit
);
153 create_new_def_for (gimple_phi_result (phi
), phi
,
154 gimple_phi_result_ptr (phi
));
155 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
156 add_phi_arg (phi
, use
, e
);
159 /* Add exit phis for VAR that is used in LIVEIN.
160 Exits of the loops are stored in EXITS. */
163 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
167 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (var
));
170 if (is_gimple_reg (var
))
171 bitmap_clear_bit (livein
, def_bb
->index
);
173 bitmap_set_bit (livein
, def_bb
->index
);
175 def
= BITMAP_ALLOC (NULL
);
176 bitmap_set_bit (def
, def_bb
->index
);
177 compute_global_livein (livein
, def
);
180 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
, bi
)
182 add_exit_phis_edge (BASIC_BLOCK (index
), var
);
186 /* Add exit phis for the names marked in NAMES_TO_RENAME.
187 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
188 names are used are stored in USE_BLOCKS. */
191 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
196 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
, bi
)
198 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
202 /* Returns a bitmap of all loop exit edge targets. */
205 get_loops_exits (void)
207 bitmap exits
= BITMAP_ALLOC (NULL
);
214 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
215 if (e
->src
!= ENTRY_BLOCK_PTR
216 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
218 bitmap_set_bit (exits
, bb
->index
);
226 /* For USE in BB, if it is used outside of the loop it is defined in,
227 mark it for rewrite. Record basic block BB where it is used
228 to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
231 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
,
236 struct loop
*def_loop
;
238 if (TREE_CODE (use
) != SSA_NAME
)
241 /* We don't need to keep virtual operands in loop-closed form. */
242 if (!is_gimple_reg (use
))
245 ver
= SSA_NAME_VERSION (use
);
246 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
249 def_loop
= def_bb
->loop_father
;
251 /* If the definition is not inside loop, it is not interesting. */
252 if (!loop_outer (def_loop
))
255 if (!use_blocks
[ver
])
256 use_blocks
[ver
] = BITMAP_ALLOC (NULL
);
257 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
259 bitmap_set_bit (need_phis
, ver
);
262 /* For uses in STMT, mark names that are used outside of the loop they are
263 defined to rewrite. Record the set of blocks in that the ssa
264 names are defined to USE_BLOCKS and the ssa names themselves to
268 find_uses_to_rename_stmt (gimple stmt
, bitmap
*use_blocks
, bitmap need_phis
)
272 basic_block bb
= gimple_bb (stmt
);
274 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
275 find_uses_to_rename_use (bb
, var
, use_blocks
, need_phis
);
278 /* Marks names that are used in BB and outside of the loop they are
279 defined in for rewrite. Records the set of blocks in that the ssa
280 names are defined to USE_BLOCKS. Record the SSA names that will
281 need exit PHIs in NEED_PHIS. */
284 find_uses_to_rename_bb (basic_block bb
, bitmap
*use_blocks
, bitmap need_phis
)
286 gimple_stmt_iterator bsi
;
290 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
291 for (bsi
= gsi_start_phis (e
->dest
); !gsi_end_p (bsi
); gsi_next (&bsi
))
292 find_uses_to_rename_use (bb
, PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi
), e
),
293 use_blocks
, need_phis
);
295 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
296 find_uses_to_rename_stmt (gsi_stmt (bsi
), use_blocks
, need_phis
);
299 /* Marks names that are used outside of the loop they are defined in
300 for rewrite. Records the set of blocks in that the ssa
301 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
302 scan only blocks in this set. */
305 find_uses_to_rename (bitmap changed_bbs
, bitmap
*use_blocks
, bitmap need_phis
)
311 if (changed_bbs
&& !bitmap_empty_p (changed_bbs
))
313 EXECUTE_IF_SET_IN_BITMAP (changed_bbs
, 0, index
, bi
)
315 find_uses_to_rename_bb (BASIC_BLOCK (index
), use_blocks
, need_phis
);
322 find_uses_to_rename_bb (bb
, use_blocks
, need_phis
);
327 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
328 phi nodes to ensure that no variable is used outside the loop it is
331 This strengthening of the basic ssa form has several advantages:
333 1) Updating it during unrolling/peeling/versioning is trivial, since
334 we do not need to care about the uses outside of the loop.
335 2) The behavior of all uses of an induction variable is the same.
336 Without this, you need to distinguish the case when the variable
337 is used outside of the loop it is defined in, for example
339 for (i = 0; i < 100; i++)
341 for (j = 0; j < 100; j++)
349 Looking from the outer loop with the normal SSA form, the first use of k
350 is not well-behaved, while the second one is an induction variable with
353 If CHANGED_BBS is not NULL, we look for uses outside loops only in
354 the basic blocks in this set.
356 UPDATE_FLAG is used in the call to update_ssa. See
357 TODO_update_ssa* for documentation. */
360 rewrite_into_loop_closed_ssa (bitmap changed_bbs
, unsigned update_flag
)
364 unsigned i
, old_num_ssa_names
;
365 bitmap names_to_rename
;
367 loops_state_set (LOOP_CLOSED_SSA
);
368 if (number_of_loops () <= 1)
371 loop_exits
= get_loops_exits ();
372 names_to_rename
= BITMAP_ALLOC (NULL
);
374 /* If the pass has caused the SSA form to be out-of-date, update it
376 update_ssa (update_flag
);
378 old_num_ssa_names
= num_ssa_names
;
379 use_blocks
= XCNEWVEC (bitmap
, old_num_ssa_names
);
381 /* Find the uses outside loops. */
382 find_uses_to_rename (changed_bbs
, use_blocks
, names_to_rename
);
384 /* Add the PHI nodes on exits of the loops for the names we need to
386 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
388 for (i
= 0; i
< old_num_ssa_names
; i
++)
389 BITMAP_FREE (use_blocks
[i
]);
391 BITMAP_FREE (loop_exits
);
392 BITMAP_FREE (names_to_rename
);
394 /* Fix up all the names found to be used outside their original
396 update_ssa (TODO_update_ssa
);
399 /* Check invariants of the loop closed ssa form for the USE in BB. */
402 check_loop_closed_ssa_use (basic_block bb
, tree use
)
407 if (TREE_CODE (use
) != SSA_NAME
|| !is_gimple_reg (use
))
410 def
= SSA_NAME_DEF_STMT (use
);
411 def_bb
= gimple_bb (def
);
413 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
416 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
419 check_loop_closed_ssa_stmt (basic_block bb
, gimple stmt
)
424 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
425 check_loop_closed_ssa_use (bb
, var
);
428 /* Checks that invariants of the loop closed ssa form are preserved. */
431 verify_loop_closed_ssa (void)
434 gimple_stmt_iterator bsi
;
439 if (number_of_loops () <= 1)
446 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
448 phi
= gsi_stmt (bsi
);
449 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
450 check_loop_closed_ssa_use (e
->src
,
451 PHI_ARG_DEF_FROM_EDGE (phi
, e
));
454 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
455 check_loop_closed_ssa_stmt (bb
, gsi_stmt (bsi
));
459 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
460 preserve the loop closed ssa form. The newly created block is returned. */
463 split_loop_exit_edge (edge exit
)
465 basic_block dest
= exit
->dest
;
466 basic_block bb
= split_edge (exit
);
470 gimple_stmt_iterator psi
;
472 for (psi
= gsi_start_phis (dest
); !gsi_end_p (psi
); gsi_next (&psi
))
474 phi
= gsi_stmt (psi
);
475 op_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, single_succ_edge (bb
));
477 name
= USE_FROM_PTR (op_p
);
479 /* If the argument of the PHI node is a constant, we do not need
480 to keep it inside loop. */
481 if (TREE_CODE (name
) != SSA_NAME
)
484 /* Otherwise create an auxiliary phi node that will copy the value
485 of the SSA name out of the loop. */
486 new_name
= duplicate_ssa_name (name
, NULL
);
487 new_phi
= create_phi_node (new_name
, bb
);
488 SSA_NAME_DEF_STMT (new_name
) = new_phi
;
489 add_phi_arg (new_phi
, name
, exit
);
490 SET_USE (op_p
, new_name
);
496 /* Returns the basic block in that statements should be emitted for induction
497 variables incremented at the end of the LOOP. */
500 ip_end_pos (struct loop
*loop
)
505 /* Returns the basic block in that statements should be emitted for induction
506 variables incremented just before exit condition of a LOOP. */
509 ip_normal_pos (struct loop
*loop
)
515 if (!single_pred_p (loop
->latch
))
518 bb
= single_pred (loop
->latch
);
519 last
= last_stmt (bb
);
521 || gimple_code (last
) != GIMPLE_COND
)
524 exit
= EDGE_SUCC (bb
, 0);
525 if (exit
->dest
== loop
->latch
)
526 exit
= EDGE_SUCC (bb
, 1);
528 if (flow_bb_inside_loop_p (loop
, exit
->dest
))
534 /* Stores the standard position for induction variable increment in LOOP
535 (just before the exit condition if it is available and latch block is empty,
536 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
537 the increment should be inserted after *BSI. */
540 standard_iv_increment_position (struct loop
*loop
, gimple_stmt_iterator
*bsi
,
543 basic_block bb
= ip_normal_pos (loop
), latch
= ip_end_pos (loop
);
544 gimple last
= last_stmt (latch
);
547 || (last
&& gimple_code (last
) != GIMPLE_LABEL
))
549 *bsi
= gsi_last_bb (latch
);
550 *insert_after
= true;
554 *bsi
= gsi_last_bb (bb
);
555 *insert_after
= false;
559 /* Copies phi node arguments for duplicated blocks. The index of the first
560 duplicated block is FIRST_NEW_BLOCK. */
563 copy_phi_node_args (unsigned first_new_block
)
567 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
568 BASIC_BLOCK (i
)->flags
|= BB_DUPLICATED
;
570 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
571 add_phi_args_after_copy_bb (BASIC_BLOCK (i
));
573 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
574 BASIC_BLOCK (i
)->flags
&= ~BB_DUPLICATED
;
578 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
579 updates the PHI nodes at start of the copied region. In order to
580 achieve this, only loops whose exits all lead to the same location
583 Notice that we do not completely update the SSA web after
584 duplication. The caller is responsible for calling update_ssa
585 after the loop has been duplicated. */
588 gimple_duplicate_loop_to_header_edge (struct loop
*loop
, edge e
,
589 unsigned int ndupl
, sbitmap wont_exit
,
590 edge orig
, VEC (edge
, heap
) **to_remove
,
593 unsigned first_new_block
;
595 if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES
))
597 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
600 #ifdef ENABLE_CHECKING
601 verify_loop_closed_ssa ();
604 first_new_block
= last_basic_block
;
605 if (!duplicate_loop_to_header_edge (loop
, e
, ndupl
, wont_exit
,
606 orig
, to_remove
, flags
))
609 /* Readd the removed phi args for e. */
610 flush_pending_stmts (e
);
612 /* Copy the phi node arguments. */
613 copy_phi_node_args (first_new_block
);
620 /* Returns true if we can unroll LOOP FACTOR times. Number
621 of iterations of the loop is returned in NITER. */
624 can_unroll_loop_p (struct loop
*loop
, unsigned factor
,
625 struct tree_niter_desc
*niter
)
629 /* Check whether unrolling is possible. We only want to unroll loops
630 for that we are able to determine number of iterations. We also
631 want to split the extra iterations of the loop from its end,
632 therefore we require that the loop has precisely one
635 exit
= single_dom_exit (loop
);
639 if (!number_of_iterations_exit (loop
, exit
, niter
, false)
640 || niter
->cmp
== ERROR_MARK
641 /* Scalar evolutions analysis might have copy propagated
642 the abnormal ssa names into these expressions, hence
643 emitting the computations based on them during loop
644 unrolling might create overlapping life ranges for
645 them, and failures in out-of-ssa. */
646 || contains_abnormal_ssa_name_p (niter
->may_be_zero
)
647 || contains_abnormal_ssa_name_p (niter
->control
.base
)
648 || contains_abnormal_ssa_name_p (niter
->control
.step
)
649 || contains_abnormal_ssa_name_p (niter
->bound
))
652 /* And of course, we must be able to duplicate the loop. */
653 if (!can_duplicate_loop_p (loop
))
656 /* The final loop should be small enough. */
657 if (tree_num_loop_insns (loop
, &eni_size_weights
) * factor
658 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
))
664 /* Determines the conditions that control execution of LOOP unrolled FACTOR
665 times. DESC is number of iterations of LOOP. ENTER_COND is set to
666 condition that must be true if the main loop can be entered.
667 EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
668 how the exit from the unrolled loop should be controlled. */
671 determine_exit_conditions (struct loop
*loop
, struct tree_niter_desc
*desc
,
672 unsigned factor
, tree
*enter_cond
,
673 tree
*exit_base
, tree
*exit_step
,
674 enum tree_code
*exit_cmp
, tree
*exit_bound
)
677 tree base
= desc
->control
.base
;
678 tree step
= desc
->control
.step
;
679 tree bound
= desc
->bound
;
680 tree type
= TREE_TYPE (step
);
682 tree min
= lower_bound_in_type (type
, type
);
683 tree max
= upper_bound_in_type (type
, type
);
684 enum tree_code cmp
= desc
->cmp
;
685 tree cond
= boolean_true_node
, assum
;
687 /* For pointers, do the arithmetics in the type of step (sizetype). */
688 base
= fold_convert (type
, base
);
689 bound
= fold_convert (type
, bound
);
691 *enter_cond
= boolean_false_node
;
692 *exit_base
= NULL_TREE
;
693 *exit_step
= NULL_TREE
;
694 *exit_cmp
= ERROR_MARK
;
695 *exit_bound
= NULL_TREE
;
696 gcc_assert (cmp
!= ERROR_MARK
);
698 /* We only need to be correct when we answer question
699 "Do at least FACTOR more iterations remain?" in the unrolled loop.
700 Thus, transforming BASE + STEP * i <> BOUND to
701 BASE + STEP * i < BOUND is ok. */
704 if (tree_int_cst_sign_bit (step
))
709 else if (cmp
== LT_EXPR
)
711 gcc_assert (!tree_int_cst_sign_bit (step
));
713 else if (cmp
== GT_EXPR
)
715 gcc_assert (tree_int_cst_sign_bit (step
));
720 /* The main body of the loop may be entered iff:
722 1) desc->may_be_zero is false.
723 2) it is possible to check that there are at least FACTOR iterations
724 of the loop, i.e., BOUND - step * FACTOR does not overflow.
725 3) # of iterations is at least FACTOR */
727 if (!integer_zerop (desc
->may_be_zero
))
728 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
729 invert_truthvalue (desc
->may_be_zero
),
732 bigstep
= fold_build2 (MULT_EXPR
, type
, step
,
733 build_int_cst_type (type
, factor
));
734 delta
= fold_build2 (MINUS_EXPR
, type
, bigstep
, step
);
736 assum
= fold_build2 (GE_EXPR
, boolean_type_node
,
738 fold_build2 (PLUS_EXPR
, type
, min
, delta
));
740 assum
= fold_build2 (LE_EXPR
, boolean_type_node
,
742 fold_build2 (PLUS_EXPR
, type
, max
, delta
));
743 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
745 bound
= fold_build2 (MINUS_EXPR
, type
, bound
, delta
);
746 assum
= fold_build2 (cmp
, boolean_type_node
, base
, bound
);
747 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
749 cond
= force_gimple_operand (unshare_expr (cond
), &stmts
, false, NULL_TREE
);
751 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
752 /* cond now may be a gimple comparison, which would be OK, but also any
753 other gimple rhs (say a && b). In this case we need to force it to
755 if (!is_gimple_condexpr (cond
))
757 cond
= force_gimple_operand (cond
, &stmts
, true, NULL_TREE
);
759 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
763 base
= force_gimple_operand (unshare_expr (base
), &stmts
, true, NULL_TREE
);
765 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
766 bound
= force_gimple_operand (unshare_expr (bound
), &stmts
, true, NULL_TREE
);
768 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
771 *exit_step
= bigstep
;
776 /* Scales the frequencies of all basic blocks in LOOP that are strictly
777 dominated by BB by NUM/DEN. */
780 scale_dominated_blocks_in_loop (struct loop
*loop
, basic_block bb
,
788 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
790 son
= next_dom_son (CDI_DOMINATORS
, son
))
792 if (!flow_bb_inside_loop_p (loop
, son
))
794 scale_bbs_frequencies_int (&son
, 1, num
, den
);
795 scale_dominated_blocks_in_loop (loop
, son
, num
, den
);
799 /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
800 EXIT is the exit of the loop to that DESC corresponds.
802 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
803 under that loop exits in the first iteration even if N != 0,
807 x = phi (init, next);
815 becomes (with possibly the exit conditions formulated a bit differently,
816 avoiding the need to create a new iv):
818 if (MAY_BE_ZERO || N < FACTOR)
823 x = phi (init, next);
834 } while (N >= FACTOR);
837 init' = phi (init, x);
841 x = phi (init', next);
849 Before the loop is unrolled, TRANSFORM is called for it (only for the
850 unrolled loop, but not for its versioned copy). DATA is passed to
853 /* Probability in % that the unrolled loop is entered. Just a guess. */
854 #define PROB_UNROLLED_LOOP_ENTERED 90
857 tree_transform_and_unroll_loop (struct loop
*loop
, unsigned factor
,
858 edge exit
, struct tree_niter_desc
*desc
,
859 transform_callback transform
,
863 tree ctr_before
, ctr_after
;
864 tree enter_main_cond
, exit_base
, exit_step
, exit_bound
;
865 enum tree_code exit_cmp
;
866 gimple phi_old_loop
, phi_new_loop
, phi_rest
;
867 gimple_stmt_iterator psi_old_loop
, psi_new_loop
;
868 tree init
, next
, new_init
, var
;
869 struct loop
*new_loop
;
870 basic_block rest
, exit_bb
;
871 edge old_entry
, new_entry
, old_latch
, precond_edge
, new_exit
;
873 gimple_stmt_iterator bsi
;
876 unsigned est_niter
, prob_entry
, scale_unrolled
, scale_rest
, freq_e
, freq_h
;
877 unsigned new_est_niter
, i
, prob
;
878 unsigned irr
= loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
;
880 VEC (edge
, heap
) *to_remove
= NULL
;
882 est_niter
= expected_loop_iterations (loop
);
883 determine_exit_conditions (loop
, desc
, factor
,
884 &enter_main_cond
, &exit_base
, &exit_step
,
885 &exit_cmp
, &exit_bound
);
887 /* Let us assume that the unrolled loop is quite likely to be entered. */
888 if (integer_nonzerop (enter_main_cond
))
889 prob_entry
= REG_BR_PROB_BASE
;
891 prob_entry
= PROB_UNROLLED_LOOP_ENTERED
* REG_BR_PROB_BASE
/ 100;
893 /* The values for scales should keep profile consistent, and somewhat close
896 TODO: The current value of SCALE_REST makes it appear that the loop that
897 is created by splitting the remaining iterations of the unrolled loop is
898 executed the same number of times as the original loop, and with the same
899 frequencies, which is obviously wrong. This does not appear to cause
900 problems, so we do not bother with fixing it for now. To make the profile
901 correct, we would need to change the probability of the exit edge of the
902 loop, and recompute the distribution of frequencies in its body because
903 of this change (scale the frequencies of blocks before and after the exit
904 by appropriate factors). */
905 scale_unrolled
= prob_entry
;
906 scale_rest
= REG_BR_PROB_BASE
;
908 new_loop
= loop_version (loop
, enter_main_cond
, NULL
,
909 prob_entry
, scale_unrolled
, scale_rest
, true);
910 gcc_assert (new_loop
!= NULL
);
911 update_ssa (TODO_update_ssa
);
913 /* Determine the probability of the exit edge of the unrolled loop. */
914 new_est_niter
= est_niter
/ factor
;
916 /* Without profile feedback, loops for that we do not know a better estimate
917 are assumed to roll 10 times. When we unroll such loop, it appears to
918 roll too little, and it may even seem to be cold. To avoid this, we
919 ensure that the created loop appears to roll at least 5 times (but at
920 most as many times as before unrolling). */
921 if (new_est_niter
< 5)
924 new_est_niter
= est_niter
;
929 /* Prepare the cfg and update the phi nodes. Move the loop exit to the
930 loop latch (and make its condition dummy, for the moment). */
931 rest
= loop_preheader_edge (new_loop
)->src
;
932 precond_edge
= single_pred_edge (rest
);
933 split_edge (loop_latch_edge (loop
));
934 exit_bb
= single_pred (loop
->latch
);
936 /* Since the exit edge will be removed, the frequency of all the blocks
937 in the loop that are dominated by it must be scaled by
938 1 / (1 - exit->probability). */
939 scale_dominated_blocks_in_loop (loop
, exit
->src
,
941 REG_BR_PROB_BASE
- exit
->probability
);
943 bsi
= gsi_last_bb (exit_bb
);
944 exit_if
= gimple_build_cond (EQ_EXPR
, integer_zero_node
,
946 NULL_TREE
, NULL_TREE
);
948 gsi_insert_after (&bsi
, exit_if
, GSI_NEW_STMT
);
949 new_exit
= make_edge (exit_bb
, rest
, EDGE_FALSE_VALUE
| irr
);
950 rescan_loop_exit (new_exit
, true, false);
952 /* Set the probability of new exit to the same of the old one. Fix
953 the frequency of the latch block, by scaling it back by
954 1 - exit->probability. */
955 new_exit
->count
= exit
->count
;
956 new_exit
->probability
= exit
->probability
;
957 new_nonexit
= single_pred_edge (loop
->latch
);
958 new_nonexit
->probability
= REG_BR_PROB_BASE
- exit
->probability
;
959 new_nonexit
->flags
= EDGE_TRUE_VALUE
;
960 new_nonexit
->count
-= exit
->count
;
961 if (new_nonexit
->count
< 0)
962 new_nonexit
->count
= 0;
963 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
966 old_entry
= loop_preheader_edge (loop
);
967 new_entry
= loop_preheader_edge (new_loop
);
968 old_latch
= loop_latch_edge (loop
);
969 for (psi_old_loop
= gsi_start_phis (loop
->header
),
970 psi_new_loop
= gsi_start_phis (new_loop
->header
);
971 !gsi_end_p (psi_old_loop
);
972 gsi_next (&psi_old_loop
), gsi_next (&psi_new_loop
))
974 phi_old_loop
= gsi_stmt (psi_old_loop
);
975 phi_new_loop
= gsi_stmt (psi_new_loop
);
977 init
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_entry
);
978 op
= PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop
, new_entry
);
979 gcc_assert (operand_equal_for_phi_arg_p (init
, USE_FROM_PTR (op
)));
980 next
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_latch
);
982 /* Prefer using original variable as a base for the new ssa name.
983 This is necessary for virtual ops, and useful in order to avoid
984 losing debug info for real ops. */
985 if (TREE_CODE (next
) == SSA_NAME
)
986 var
= SSA_NAME_VAR (next
);
987 else if (TREE_CODE (init
) == SSA_NAME
)
988 var
= SSA_NAME_VAR (init
);
991 var
= create_tmp_var (TREE_TYPE (init
), "unrinittmp");
992 add_referenced_var (var
);
995 new_init
= make_ssa_name (var
, NULL
);
996 phi_rest
= create_phi_node (new_init
, rest
);
997 SSA_NAME_DEF_STMT (new_init
) = phi_rest
;
999 add_phi_arg (phi_rest
, init
, precond_edge
);
1000 add_phi_arg (phi_rest
, next
, new_exit
);
1001 SET_USE (op
, new_init
);
1006 /* Transform the loop. */
1008 (*transform
) (loop
, data
);
1010 /* Unroll the loop and remove the exits in all iterations except for the
1012 wont_exit
= sbitmap_alloc (factor
);
1013 sbitmap_ones (wont_exit
);
1014 RESET_BIT (wont_exit
, factor
- 1);
1016 ok
= gimple_duplicate_loop_to_header_edge
1017 (loop
, loop_latch_edge (loop
), factor
- 1,
1018 wont_exit
, new_exit
, &to_remove
, DLTHE_FLAG_UPDATE_FREQ
);
1022 for (i
= 0; VEC_iterate (edge
, to_remove
, i
, e
); i
++)
1024 ok
= remove_path (e
);
1027 VEC_free (edge
, heap
, to_remove
);
1028 update_ssa (TODO_update_ssa
);
1030 /* Ensure that the frequencies in the loop match the new estimated
1031 number of iterations, and change the probability of the new
1033 freq_h
= loop
->header
->frequency
;
1034 freq_e
= EDGE_FREQUENCY (loop_preheader_edge (loop
));
1036 scale_loop_frequencies (loop
, freq_e
* (new_est_niter
+ 1), freq_h
);
1038 exit_bb
= single_pred (loop
->latch
);
1039 new_exit
= find_edge (exit_bb
, rest
);
1040 new_exit
->count
= loop_preheader_edge (loop
)->count
;
1041 new_exit
->probability
= REG_BR_PROB_BASE
/ (new_est_niter
+ 1);
1043 rest
->count
+= new_exit
->count
;
1044 rest
->frequency
+= EDGE_FREQUENCY (new_exit
);
1046 new_nonexit
= single_pred_edge (loop
->latch
);
1047 prob
= new_nonexit
->probability
;
1048 new_nonexit
->probability
= REG_BR_PROB_BASE
- new_exit
->probability
;
1049 new_nonexit
->count
= exit_bb
->count
- new_exit
->count
;
1050 if (new_nonexit
->count
< 0)
1051 new_nonexit
->count
= 0;
1053 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
1056 /* Finally create the new counter for number of iterations and add the new
1057 exit instruction. */
1058 bsi
= gsi_last_bb (exit_bb
);
1059 exit_if
= gsi_stmt (bsi
);
1060 create_iv (exit_base
, exit_step
, NULL_TREE
, loop
,
1061 &bsi
, false, &ctr_before
, &ctr_after
);
1062 gimple_cond_set_code (exit_if
, exit_cmp
);
1063 gimple_cond_set_lhs (exit_if
, ctr_after
);
1064 gimple_cond_set_rhs (exit_if
, exit_bound
);
1065 update_stmt (exit_if
);
1067 #ifdef ENABLE_CHECKING
1068 verify_flow_info ();
1069 verify_dominators (CDI_DOMINATORS
);
1070 verify_loop_structure ();
1071 verify_loop_closed_ssa ();
1075 /* Wrapper over tree_transform_and_unroll_loop for case we do not
1076 want to transform the loop before unrolling. The meaning
1077 of the arguments is the same as for tree_transform_and_unroll_loop. */
1080 tree_unroll_loop (struct loop
*loop
, unsigned factor
,
1081 edge exit
, struct tree_niter_desc
*desc
)
1083 tree_transform_and_unroll_loop (loop
, factor
, exit
, desc
,