1 /* High-level loop manipulation functions.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 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 if (TREE_CODE (base
) == ADDR_EXPR
)
103 mark_addressable (TREE_OPERAND (base
, 0));
104 step
= fold_convert (sizetype
, step
);
105 if (incr_op
== MINUS_EXPR
)
106 step
= fold_build1 (NEGATE_EXPR
, sizetype
, step
);
107 incr_op
= POINTER_PLUS_EXPR
;
109 /* Gimplify the step if necessary. We put the computations in front of the
110 loop (i.e. the step should be loop invariant). */
111 step
= force_gimple_operand (step
, &stmts
, true, NULL_TREE
);
113 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
115 stmt
= gimple_build_assign_with_ops (incr_op
, va
, vb
, step
);
117 gsi_insert_after (incr_pos
, stmt
, GSI_NEW_STMT
);
119 gsi_insert_before (incr_pos
, stmt
, GSI_NEW_STMT
);
121 initial
= force_gimple_operand (base
, &stmts
, true, var
);
123 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
125 stmt
= create_phi_node (vb
, loop
->header
);
126 SSA_NAME_DEF_STMT (vb
) = stmt
;
127 add_phi_arg (stmt
, initial
, loop_preheader_edge (loop
));
128 add_phi_arg (stmt
, va
, loop_latch_edge (loop
));
131 /* Add exit phis for the USE on EXIT. */
134 add_exit_phis_edge (basic_block exit
, tree use
)
136 gimple phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
137 basic_block def_bb
= gimple_bb (def_stmt
);
138 struct loop
*def_loop
;
142 /* Check that some of the edges entering the EXIT block exits a loop in
143 that USE is defined. */
144 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
146 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
147 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
154 phi
= create_phi_node (use
, exit
);
155 create_new_def_for (gimple_phi_result (phi
), phi
,
156 gimple_phi_result_ptr (phi
));
157 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
158 add_phi_arg (phi
, use
, e
);
161 /* Add exit phis for VAR that is used in LIVEIN.
162 Exits of the loops are stored in EXITS. */
165 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
169 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (var
));
172 if (is_gimple_reg (var
))
173 bitmap_clear_bit (livein
, def_bb
->index
);
175 bitmap_set_bit (livein
, def_bb
->index
);
177 def
= BITMAP_ALLOC (NULL
);
178 bitmap_set_bit (def
, def_bb
->index
);
179 compute_global_livein (livein
, def
);
182 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
, bi
)
184 add_exit_phis_edge (BASIC_BLOCK (index
), var
);
188 /* Add exit phis for the names marked in NAMES_TO_RENAME.
189 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
190 names are used are stored in USE_BLOCKS. */
193 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
198 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
, bi
)
200 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
204 /* Returns a bitmap of all loop exit edge targets. */
207 get_loops_exits (void)
209 bitmap exits
= BITMAP_ALLOC (NULL
);
216 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
217 if (e
->src
!= ENTRY_BLOCK_PTR
218 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
220 bitmap_set_bit (exits
, bb
->index
);
228 /* For USE in BB, if it is used outside of the loop it is defined in,
229 mark it for rewrite. Record basic block BB where it is used
230 to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
233 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
,
238 struct loop
*def_loop
;
240 if (TREE_CODE (use
) != SSA_NAME
)
243 /* We don't need to keep virtual operands in loop-closed form. */
244 if (!is_gimple_reg (use
))
247 ver
= SSA_NAME_VERSION (use
);
248 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
251 def_loop
= def_bb
->loop_father
;
253 /* If the definition is not inside a loop, it is not interesting. */
254 if (!loop_outer (def_loop
))
257 /* If the use is not outside of the loop it is defined in, it is not
259 if (flow_bb_inside_loop_p (def_loop
, bb
))
262 if (!use_blocks
[ver
])
263 use_blocks
[ver
] = BITMAP_ALLOC (NULL
);
264 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
266 bitmap_set_bit (need_phis
, ver
);
269 /* For uses in STMT, mark names that are used outside of the loop they are
270 defined to rewrite. Record the set of blocks in that the ssa
271 names are defined to USE_BLOCKS and the ssa names themselves to
275 find_uses_to_rename_stmt (gimple stmt
, bitmap
*use_blocks
, bitmap need_phis
)
279 basic_block bb
= gimple_bb (stmt
);
281 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
282 find_uses_to_rename_use (bb
, var
, use_blocks
, need_phis
);
285 /* Marks names that are used in BB and outside of the loop they are
286 defined in for rewrite. Records the set of blocks in that the ssa
287 names are defined to USE_BLOCKS. Record the SSA names that will
288 need exit PHIs in NEED_PHIS. */
291 find_uses_to_rename_bb (basic_block bb
, bitmap
*use_blocks
, bitmap need_phis
)
293 gimple_stmt_iterator bsi
;
297 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
298 for (bsi
= gsi_start_phis (e
->dest
); !gsi_end_p (bsi
); gsi_next (&bsi
))
299 find_uses_to_rename_use (bb
, PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi
), e
),
300 use_blocks
, need_phis
);
302 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
303 find_uses_to_rename_stmt (gsi_stmt (bsi
), use_blocks
, need_phis
);
306 /* Marks names that are used outside of the loop they are defined in
307 for rewrite. Records the set of blocks in that the ssa
308 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
309 scan only blocks in this set. */
312 find_uses_to_rename (bitmap changed_bbs
, bitmap
*use_blocks
, bitmap need_phis
)
318 if (changed_bbs
&& !bitmap_empty_p (changed_bbs
))
320 EXECUTE_IF_SET_IN_BITMAP (changed_bbs
, 0, index
, bi
)
322 find_uses_to_rename_bb (BASIC_BLOCK (index
), use_blocks
, need_phis
);
329 find_uses_to_rename_bb (bb
, use_blocks
, need_phis
);
334 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
335 phi nodes to ensure that no variable is used outside the loop it is
338 This strengthening of the basic ssa form has several advantages:
340 1) Updating it during unrolling/peeling/versioning is trivial, since
341 we do not need to care about the uses outside of the loop.
342 2) The behavior of all uses of an induction variable is the same.
343 Without this, you need to distinguish the case when the variable
344 is used outside of the loop it is defined in, for example
346 for (i = 0; i < 100; i++)
348 for (j = 0; j < 100; j++)
356 Looking from the outer loop with the normal SSA form, the first use of k
357 is not well-behaved, while the second one is an induction variable with
360 If CHANGED_BBS is not NULL, we look for uses outside loops only in
361 the basic blocks in this set.
363 UPDATE_FLAG is used in the call to update_ssa. See
364 TODO_update_ssa* for documentation. */
367 rewrite_into_loop_closed_ssa (bitmap changed_bbs
, unsigned update_flag
)
371 unsigned i
, old_num_ssa_names
;
372 bitmap names_to_rename
;
374 loops_state_set (LOOP_CLOSED_SSA
);
375 if (number_of_loops () <= 1)
378 loop_exits
= get_loops_exits ();
379 names_to_rename
= BITMAP_ALLOC (NULL
);
381 /* If the pass has caused the SSA form to be out-of-date, update it
383 update_ssa (update_flag
);
385 old_num_ssa_names
= num_ssa_names
;
386 use_blocks
= XCNEWVEC (bitmap
, old_num_ssa_names
);
388 /* Find the uses outside loops. */
389 find_uses_to_rename (changed_bbs
, use_blocks
, names_to_rename
);
391 /* Add the PHI nodes on exits of the loops for the names we need to
393 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
395 for (i
= 0; i
< old_num_ssa_names
; i
++)
396 BITMAP_FREE (use_blocks
[i
]);
398 BITMAP_FREE (loop_exits
);
399 BITMAP_FREE (names_to_rename
);
401 /* Fix up all the names found to be used outside their original
403 update_ssa (TODO_update_ssa
);
406 /* Check invariants of the loop closed ssa form for the USE in BB. */
409 check_loop_closed_ssa_use (basic_block bb
, tree use
)
414 if (TREE_CODE (use
) != SSA_NAME
|| !is_gimple_reg (use
))
417 def
= SSA_NAME_DEF_STMT (use
);
418 def_bb
= gimple_bb (def
);
420 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
423 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
426 check_loop_closed_ssa_stmt (basic_block bb
, gimple stmt
)
431 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
432 check_loop_closed_ssa_use (bb
, var
);
435 /* Checks that invariants of the loop closed ssa form are preserved. */
438 verify_loop_closed_ssa (void)
441 gimple_stmt_iterator bsi
;
446 if (number_of_loops () <= 1)
453 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
455 phi
= gsi_stmt (bsi
);
456 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
457 check_loop_closed_ssa_use (e
->src
,
458 PHI_ARG_DEF_FROM_EDGE (phi
, e
));
461 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
462 check_loop_closed_ssa_stmt (bb
, gsi_stmt (bsi
));
466 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
467 preserve the loop closed ssa form. The newly created block is returned. */
470 split_loop_exit_edge (edge exit
)
472 basic_block dest
= exit
->dest
;
473 basic_block bb
= split_edge (exit
);
477 gimple_stmt_iterator psi
;
479 for (psi
= gsi_start_phis (dest
); !gsi_end_p (psi
); gsi_next (&psi
))
481 phi
= gsi_stmt (psi
);
482 op_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, single_succ_edge (bb
));
484 name
= USE_FROM_PTR (op_p
);
486 /* If the argument of the PHI node is a constant, we do not need
487 to keep it inside loop. */
488 if (TREE_CODE (name
) != SSA_NAME
)
491 /* Otherwise create an auxiliary phi node that will copy the value
492 of the SSA name out of the loop. */
493 new_name
= duplicate_ssa_name (name
, NULL
);
494 new_phi
= create_phi_node (new_name
, bb
);
495 SSA_NAME_DEF_STMT (new_name
) = new_phi
;
496 add_phi_arg (new_phi
, name
, exit
);
497 SET_USE (op_p
, new_name
);
503 /* Returns the basic block in that statements should be emitted for induction
504 variables incremented at the end of the LOOP. */
507 ip_end_pos (struct loop
*loop
)
512 /* Returns the basic block in that statements should be emitted for induction
513 variables incremented just before exit condition of a LOOP. */
516 ip_normal_pos (struct loop
*loop
)
522 if (!single_pred_p (loop
->latch
))
525 bb
= single_pred (loop
->latch
);
526 last
= last_stmt (bb
);
528 || gimple_code (last
) != GIMPLE_COND
)
531 exit
= EDGE_SUCC (bb
, 0);
532 if (exit
->dest
== loop
->latch
)
533 exit
= EDGE_SUCC (bb
, 1);
535 if (flow_bb_inside_loop_p (loop
, exit
->dest
))
541 /* Stores the standard position for induction variable increment in LOOP
542 (just before the exit condition if it is available and latch block is empty,
543 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
544 the increment should be inserted after *BSI. */
547 standard_iv_increment_position (struct loop
*loop
, gimple_stmt_iterator
*bsi
,
550 basic_block bb
= ip_normal_pos (loop
), latch
= ip_end_pos (loop
);
551 gimple last
= last_stmt (latch
);
554 || (last
&& gimple_code (last
) != GIMPLE_LABEL
))
556 *bsi
= gsi_last_bb (latch
);
557 *insert_after
= true;
561 *bsi
= gsi_last_bb (bb
);
562 *insert_after
= false;
566 /* Copies phi node arguments for duplicated blocks. The index of the first
567 duplicated block is FIRST_NEW_BLOCK. */
570 copy_phi_node_args (unsigned first_new_block
)
574 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
575 BASIC_BLOCK (i
)->flags
|= BB_DUPLICATED
;
577 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
578 add_phi_args_after_copy_bb (BASIC_BLOCK (i
));
580 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
581 BASIC_BLOCK (i
)->flags
&= ~BB_DUPLICATED
;
585 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
586 updates the PHI nodes at start of the copied region. In order to
587 achieve this, only loops whose exits all lead to the same location
590 Notice that we do not completely update the SSA web after
591 duplication. The caller is responsible for calling update_ssa
592 after the loop has been duplicated. */
595 gimple_duplicate_loop_to_header_edge (struct loop
*loop
, edge e
,
596 unsigned int ndupl
, sbitmap wont_exit
,
597 edge orig
, VEC (edge
, heap
) **to_remove
,
600 unsigned first_new_block
;
602 if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES
))
604 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
607 #ifdef ENABLE_CHECKING
608 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
))
609 verify_loop_closed_ssa ();
612 first_new_block
= last_basic_block
;
613 if (!duplicate_loop_to_header_edge (loop
, e
, ndupl
, wont_exit
,
614 orig
, to_remove
, flags
))
617 /* Readd the removed phi args for e. */
618 flush_pending_stmts (e
);
620 /* Copy the phi node arguments. */
621 copy_phi_node_args (first_new_block
);
628 /* Returns true if we can unroll LOOP FACTOR times. Number
629 of iterations of the loop is returned in NITER. */
632 can_unroll_loop_p (struct loop
*loop
, unsigned factor
,
633 struct tree_niter_desc
*niter
)
637 /* Check whether unrolling is possible. We only want to unroll loops
638 for that we are able to determine number of iterations. We also
639 want to split the extra iterations of the loop from its end,
640 therefore we require that the loop has precisely one
643 exit
= single_dom_exit (loop
);
647 if (!number_of_iterations_exit (loop
, exit
, niter
, false)
648 || niter
->cmp
== ERROR_MARK
649 /* Scalar evolutions analysis might have copy propagated
650 the abnormal ssa names into these expressions, hence
651 emitting the computations based on them during loop
652 unrolling might create overlapping life ranges for
653 them, and failures in out-of-ssa. */
654 || contains_abnormal_ssa_name_p (niter
->may_be_zero
)
655 || contains_abnormal_ssa_name_p (niter
->control
.base
)
656 || contains_abnormal_ssa_name_p (niter
->control
.step
)
657 || contains_abnormal_ssa_name_p (niter
->bound
))
660 /* And of course, we must be able to duplicate the loop. */
661 if (!can_duplicate_loop_p (loop
))
664 /* The final loop should be small enough. */
665 if (tree_num_loop_insns (loop
, &eni_size_weights
) * factor
666 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
))
672 /* Determines the conditions that control execution of LOOP unrolled FACTOR
673 times. DESC is number of iterations of LOOP. ENTER_COND is set to
674 condition that must be true if the main loop can be entered.
675 EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
676 how the exit from the unrolled loop should be controlled. */
679 determine_exit_conditions (struct loop
*loop
, struct tree_niter_desc
*desc
,
680 unsigned factor
, tree
*enter_cond
,
681 tree
*exit_base
, tree
*exit_step
,
682 enum tree_code
*exit_cmp
, tree
*exit_bound
)
685 tree base
= desc
->control
.base
;
686 tree step
= desc
->control
.step
;
687 tree bound
= desc
->bound
;
688 tree type
= TREE_TYPE (step
);
690 tree min
= lower_bound_in_type (type
, type
);
691 tree max
= upper_bound_in_type (type
, type
);
692 enum tree_code cmp
= desc
->cmp
;
693 tree cond
= boolean_true_node
, assum
;
695 /* For pointers, do the arithmetics in the type of step (sizetype). */
696 base
= fold_convert (type
, base
);
697 bound
= fold_convert (type
, bound
);
699 *enter_cond
= boolean_false_node
;
700 *exit_base
= NULL_TREE
;
701 *exit_step
= NULL_TREE
;
702 *exit_cmp
= ERROR_MARK
;
703 *exit_bound
= NULL_TREE
;
704 gcc_assert (cmp
!= ERROR_MARK
);
706 /* We only need to be correct when we answer question
707 "Do at least FACTOR more iterations remain?" in the unrolled loop.
708 Thus, transforming BASE + STEP * i <> BOUND to
709 BASE + STEP * i < BOUND is ok. */
712 if (tree_int_cst_sign_bit (step
))
717 else if (cmp
== LT_EXPR
)
719 gcc_assert (!tree_int_cst_sign_bit (step
));
721 else if (cmp
== GT_EXPR
)
723 gcc_assert (tree_int_cst_sign_bit (step
));
728 /* The main body of the loop may be entered iff:
730 1) desc->may_be_zero is false.
731 2) it is possible to check that there are at least FACTOR iterations
732 of the loop, i.e., BOUND - step * FACTOR does not overflow.
733 3) # of iterations is at least FACTOR */
735 if (!integer_zerop (desc
->may_be_zero
))
736 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
737 invert_truthvalue (desc
->may_be_zero
),
740 bigstep
= fold_build2 (MULT_EXPR
, type
, step
,
741 build_int_cst_type (type
, factor
));
742 delta
= fold_build2 (MINUS_EXPR
, type
, bigstep
, step
);
744 assum
= fold_build2 (GE_EXPR
, boolean_type_node
,
746 fold_build2 (PLUS_EXPR
, type
, min
, delta
));
748 assum
= fold_build2 (LE_EXPR
, boolean_type_node
,
750 fold_build2 (PLUS_EXPR
, type
, max
, delta
));
751 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
753 bound
= fold_build2 (MINUS_EXPR
, type
, bound
, delta
);
754 assum
= fold_build2 (cmp
, boolean_type_node
, base
, bound
);
755 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
757 cond
= force_gimple_operand (unshare_expr (cond
), &stmts
, false, NULL_TREE
);
759 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
760 /* cond now may be a gimple comparison, which would be OK, but also any
761 other gimple rhs (say a && b). In this case we need to force it to
763 if (!is_gimple_condexpr (cond
))
765 cond
= force_gimple_operand (cond
, &stmts
, true, NULL_TREE
);
767 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
771 base
= force_gimple_operand (unshare_expr (base
), &stmts
, true, NULL_TREE
);
773 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
774 bound
= force_gimple_operand (unshare_expr (bound
), &stmts
, true, NULL_TREE
);
776 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
779 *exit_step
= bigstep
;
784 /* Scales the frequencies of all basic blocks in LOOP that are strictly
785 dominated by BB by NUM/DEN. */
788 scale_dominated_blocks_in_loop (struct loop
*loop
, basic_block bb
,
796 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
798 son
= next_dom_son (CDI_DOMINATORS
, son
))
800 if (!flow_bb_inside_loop_p (loop
, son
))
802 scale_bbs_frequencies_int (&son
, 1, num
, den
);
803 scale_dominated_blocks_in_loop (loop
, son
, num
, den
);
807 /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
808 EXIT is the exit of the loop to that DESC corresponds.
810 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
811 under that loop exits in the first iteration even if N != 0,
815 x = phi (init, next);
823 becomes (with possibly the exit conditions formulated a bit differently,
824 avoiding the need to create a new iv):
826 if (MAY_BE_ZERO || N < FACTOR)
831 x = phi (init, next);
842 } while (N >= FACTOR);
845 init' = phi (init, x);
849 x = phi (init', next);
857 Before the loop is unrolled, TRANSFORM is called for it (only for the
858 unrolled loop, but not for its versioned copy). DATA is passed to
861 /* Probability in % that the unrolled loop is entered. Just a guess. */
862 #define PROB_UNROLLED_LOOP_ENTERED 90
865 tree_transform_and_unroll_loop (struct loop
*loop
, unsigned factor
,
866 edge exit
, struct tree_niter_desc
*desc
,
867 transform_callback transform
,
871 tree ctr_before
, ctr_after
;
872 tree enter_main_cond
, exit_base
, exit_step
, exit_bound
;
873 enum tree_code exit_cmp
;
874 gimple phi_old_loop
, phi_new_loop
, phi_rest
;
875 gimple_stmt_iterator psi_old_loop
, psi_new_loop
;
876 tree init
, next
, new_init
, var
;
877 struct loop
*new_loop
;
878 basic_block rest
, exit_bb
;
879 edge old_entry
, new_entry
, old_latch
, precond_edge
, new_exit
;
881 gimple_stmt_iterator bsi
;
884 unsigned est_niter
, prob_entry
, scale_unrolled
, scale_rest
, freq_e
, freq_h
;
885 unsigned new_est_niter
, i
, prob
;
886 unsigned irr
= loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
;
888 VEC (edge
, heap
) *to_remove
= NULL
;
890 est_niter
= expected_loop_iterations (loop
);
891 determine_exit_conditions (loop
, desc
, factor
,
892 &enter_main_cond
, &exit_base
, &exit_step
,
893 &exit_cmp
, &exit_bound
);
895 /* Let us assume that the unrolled loop is quite likely to be entered. */
896 if (integer_nonzerop (enter_main_cond
))
897 prob_entry
= REG_BR_PROB_BASE
;
899 prob_entry
= PROB_UNROLLED_LOOP_ENTERED
* REG_BR_PROB_BASE
/ 100;
901 /* The values for scales should keep profile consistent, and somewhat close
904 TODO: The current value of SCALE_REST makes it appear that the loop that
905 is created by splitting the remaining iterations of the unrolled loop is
906 executed the same number of times as the original loop, and with the same
907 frequencies, which is obviously wrong. This does not appear to cause
908 problems, so we do not bother with fixing it for now. To make the profile
909 correct, we would need to change the probability of the exit edge of the
910 loop, and recompute the distribution of frequencies in its body because
911 of this change (scale the frequencies of blocks before and after the exit
912 by appropriate factors). */
913 scale_unrolled
= prob_entry
;
914 scale_rest
= REG_BR_PROB_BASE
;
916 new_loop
= loop_version (loop
, enter_main_cond
, NULL
,
917 prob_entry
, scale_unrolled
, scale_rest
, true);
918 gcc_assert (new_loop
!= NULL
);
919 update_ssa (TODO_update_ssa
);
921 /* Determine the probability of the exit edge of the unrolled loop. */
922 new_est_niter
= est_niter
/ factor
;
924 /* Without profile feedback, loops for that we do not know a better estimate
925 are assumed to roll 10 times. When we unroll such loop, it appears to
926 roll too little, and it may even seem to be cold. To avoid this, we
927 ensure that the created loop appears to roll at least 5 times (but at
928 most as many times as before unrolling). */
929 if (new_est_niter
< 5)
932 new_est_niter
= est_niter
;
937 /* Prepare the cfg and update the phi nodes. Move the loop exit to the
938 loop latch (and make its condition dummy, for the moment). */
939 rest
= loop_preheader_edge (new_loop
)->src
;
940 precond_edge
= single_pred_edge (rest
);
941 split_edge (loop_latch_edge (loop
));
942 exit_bb
= single_pred (loop
->latch
);
944 /* Since the exit edge will be removed, the frequency of all the blocks
945 in the loop that are dominated by it must be scaled by
946 1 / (1 - exit->probability). */
947 scale_dominated_blocks_in_loop (loop
, exit
->src
,
949 REG_BR_PROB_BASE
- exit
->probability
);
951 bsi
= gsi_last_bb (exit_bb
);
952 exit_if
= gimple_build_cond (EQ_EXPR
, integer_zero_node
,
954 NULL_TREE
, NULL_TREE
);
956 gsi_insert_after (&bsi
, exit_if
, GSI_NEW_STMT
);
957 new_exit
= make_edge (exit_bb
, rest
, EDGE_FALSE_VALUE
| irr
);
958 rescan_loop_exit (new_exit
, true, false);
960 /* Set the probability of new exit to the same of the old one. Fix
961 the frequency of the latch block, by scaling it back by
962 1 - exit->probability. */
963 new_exit
->count
= exit
->count
;
964 new_exit
->probability
= exit
->probability
;
965 new_nonexit
= single_pred_edge (loop
->latch
);
966 new_nonexit
->probability
= REG_BR_PROB_BASE
- exit
->probability
;
967 new_nonexit
->flags
= EDGE_TRUE_VALUE
;
968 new_nonexit
->count
-= exit
->count
;
969 if (new_nonexit
->count
< 0)
970 new_nonexit
->count
= 0;
971 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
974 old_entry
= loop_preheader_edge (loop
);
975 new_entry
= loop_preheader_edge (new_loop
);
976 old_latch
= loop_latch_edge (loop
);
977 for (psi_old_loop
= gsi_start_phis (loop
->header
),
978 psi_new_loop
= gsi_start_phis (new_loop
->header
);
979 !gsi_end_p (psi_old_loop
);
980 gsi_next (&psi_old_loop
), gsi_next (&psi_new_loop
))
982 phi_old_loop
= gsi_stmt (psi_old_loop
);
983 phi_new_loop
= gsi_stmt (psi_new_loop
);
985 init
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_entry
);
986 op
= PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop
, new_entry
);
987 gcc_assert (operand_equal_for_phi_arg_p (init
, USE_FROM_PTR (op
)));
988 next
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_latch
);
990 /* Prefer using original variable as a base for the new ssa name.
991 This is necessary for virtual ops, and useful in order to avoid
992 losing debug info for real ops. */
993 if (TREE_CODE (next
) == SSA_NAME
)
994 var
= SSA_NAME_VAR (next
);
995 else if (TREE_CODE (init
) == SSA_NAME
)
996 var
= SSA_NAME_VAR (init
);
999 var
= create_tmp_var (TREE_TYPE (init
), "unrinittmp");
1000 add_referenced_var (var
);
1003 new_init
= make_ssa_name (var
, NULL
);
1004 phi_rest
= create_phi_node (new_init
, rest
);
1005 SSA_NAME_DEF_STMT (new_init
) = phi_rest
;
1007 add_phi_arg (phi_rest
, init
, precond_edge
);
1008 add_phi_arg (phi_rest
, next
, new_exit
);
1009 SET_USE (op
, new_init
);
1014 /* Transform the loop. */
1016 (*transform
) (loop
, data
);
1018 /* Unroll the loop and remove the exits in all iterations except for the
1020 wont_exit
= sbitmap_alloc (factor
);
1021 sbitmap_ones (wont_exit
);
1022 RESET_BIT (wont_exit
, factor
- 1);
1024 ok
= gimple_duplicate_loop_to_header_edge
1025 (loop
, loop_latch_edge (loop
), factor
- 1,
1026 wont_exit
, new_exit
, &to_remove
, DLTHE_FLAG_UPDATE_FREQ
);
1030 for (i
= 0; VEC_iterate (edge
, to_remove
, i
, e
); i
++)
1032 ok
= remove_path (e
);
1035 VEC_free (edge
, heap
, to_remove
);
1036 update_ssa (TODO_update_ssa
);
1038 /* Ensure that the frequencies in the loop match the new estimated
1039 number of iterations, and change the probability of the new
1041 freq_h
= loop
->header
->frequency
;
1042 freq_e
= EDGE_FREQUENCY (loop_preheader_edge (loop
));
1044 scale_loop_frequencies (loop
, freq_e
* (new_est_niter
+ 1), freq_h
);
1046 exit_bb
= single_pred (loop
->latch
);
1047 new_exit
= find_edge (exit_bb
, rest
);
1048 new_exit
->count
= loop_preheader_edge (loop
)->count
;
1049 new_exit
->probability
= REG_BR_PROB_BASE
/ (new_est_niter
+ 1);
1051 rest
->count
+= new_exit
->count
;
1052 rest
->frequency
+= EDGE_FREQUENCY (new_exit
);
1054 new_nonexit
= single_pred_edge (loop
->latch
);
1055 prob
= new_nonexit
->probability
;
1056 new_nonexit
->probability
= REG_BR_PROB_BASE
- new_exit
->probability
;
1057 new_nonexit
->count
= exit_bb
->count
- new_exit
->count
;
1058 if (new_nonexit
->count
< 0)
1059 new_nonexit
->count
= 0;
1061 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
1064 /* Finally create the new counter for number of iterations and add the new
1065 exit instruction. */
1066 bsi
= gsi_last_bb (exit_bb
);
1067 exit_if
= gsi_stmt (bsi
);
1068 create_iv (exit_base
, exit_step
, NULL_TREE
, loop
,
1069 &bsi
, false, &ctr_before
, &ctr_after
);
1070 gimple_cond_set_code (exit_if
, exit_cmp
);
1071 gimple_cond_set_lhs (exit_if
, ctr_after
);
1072 gimple_cond_set_rhs (exit_if
, exit_bound
);
1073 update_stmt (exit_if
);
1075 #ifdef ENABLE_CHECKING
1076 verify_flow_info ();
1077 verify_dominators (CDI_DOMINATORS
);
1078 verify_loop_structure ();
1079 verify_loop_closed_ssa ();
1083 /* Wrapper over tree_transform_and_unroll_loop for case we do not
1084 want to transform the loop before unrolling. The meaning
1085 of the arguments is the same as for tree_transform_and_unroll_loop. */
1088 tree_unroll_loop (struct loop
*loop
, unsigned factor
,
1089 edge exit
, struct tree_niter_desc
*desc
)
1091 tree_transform_and_unroll_loop (loop
, factor
, exit
, desc
,