1 /* High-level loop manipulation functions.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "tree-flow.h"
30 #include "tree-dump.h"
33 #include "tree-pass.h"
34 #include "cfglayout.h"
35 #include "tree-scalar-evolution.h"
37 #include "tree-inline.h"
38 #include "langhooks.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 gimple_stmt_iterator
*incr_pos
, bool after
,
52 tree
*var_before
, tree
*var_after
)
58 enum tree_code incr_op
= PLUS_EXPR
;
59 edge pe
= loop_preheader_edge (loop
);
63 var
= create_tmp_var (TREE_TYPE (base
), "ivtmp");
64 add_referenced_var (var
);
67 vb
= make_ssa_name (var
, NULL
);
70 va
= make_ssa_name (var
, NULL
);
74 /* For easier readability of the created code, produce MINUS_EXPRs
76 if (TREE_CODE (step
) == INTEGER_CST
)
78 if (TYPE_UNSIGNED (TREE_TYPE (step
)))
80 step1
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
81 if (tree_int_cst_lt (step1
, step
))
91 if (!tree_expr_nonnegative_warnv_p (step
, &ovf
)
92 && may_negate_without_overflow_p (step
))
95 step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
99 if (POINTER_TYPE_P (TREE_TYPE (base
)))
101 if (TREE_CODE (base
) == ADDR_EXPR
)
102 mark_addressable (TREE_OPERAND (base
, 0));
103 step
= convert_to_ptrofftype (step
);
104 if (incr_op
== MINUS_EXPR
)
105 step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
);
106 incr_op
= POINTER_PLUS_EXPR
;
108 /* Gimplify the step if necessary. We put the computations in front of the
109 loop (i.e. the step should be loop invariant). */
110 step
= force_gimple_operand (step
, &stmts
, true, NULL_TREE
);
112 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
114 stmt
= gimple_build_assign_with_ops (incr_op
, va
, vb
, step
);
116 gsi_insert_after (incr_pos
, stmt
, GSI_NEW_STMT
);
118 gsi_insert_before (incr_pos
, stmt
, GSI_NEW_STMT
);
120 initial
= force_gimple_operand (base
, &stmts
, true, var
);
122 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
124 stmt
= create_phi_node (vb
, loop
->header
);
125 SSA_NAME_DEF_STMT (vb
) = stmt
;
126 add_phi_arg (stmt
, initial
, loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
127 add_phi_arg (stmt
, va
, loop_latch_edge (loop
), UNKNOWN_LOCATION
);
130 /* Add exit phis for the USE on EXIT. */
133 add_exit_phis_edge (basic_block exit
, tree use
)
135 gimple phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
136 basic_block def_bb
= gimple_bb (def_stmt
);
137 struct loop
*def_loop
;
141 /* Check that some of the edges entering the EXIT block exits a loop in
142 that USE is defined. */
143 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
145 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
146 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
153 phi
= create_phi_node (use
, exit
);
154 create_new_def_for (gimple_phi_result (phi
), phi
,
155 gimple_phi_result_ptr (phi
));
156 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
157 add_phi_arg (phi
, use
, e
, UNKNOWN_LOCATION
);
160 /* Add exit phis for VAR that is used in LIVEIN.
161 Exits of the loops are stored in EXITS. */
164 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
168 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (var
));
171 if (is_gimple_reg (var
))
172 bitmap_clear_bit (livein
, def_bb
->index
);
174 bitmap_set_bit (livein
, def_bb
->index
);
176 def
= BITMAP_ALLOC (NULL
);
177 bitmap_set_bit (def
, def_bb
->index
);
178 compute_global_livein (livein
, def
);
181 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
, bi
)
183 add_exit_phis_edge (BASIC_BLOCK (index
), var
);
187 /* Add exit phis for the names marked in NAMES_TO_RENAME.
188 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
189 names are used are stored in USE_BLOCKS. */
192 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
197 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
, bi
)
199 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
203 /* Returns a bitmap of all loop exit edge targets. */
206 get_loops_exits (void)
208 bitmap exits
= BITMAP_ALLOC (NULL
);
215 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
216 if (e
->src
!= ENTRY_BLOCK_PTR
217 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
219 bitmap_set_bit (exits
, bb
->index
);
227 /* For USE in BB, if it is used outside of the loop it is defined in,
228 mark it for rewrite. Record basic block BB where it is used
229 to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
232 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
,
237 struct loop
*def_loop
;
239 if (TREE_CODE (use
) != SSA_NAME
)
242 /* We don't need to keep virtual operands in loop-closed form. */
243 if (!is_gimple_reg (use
))
246 ver
= SSA_NAME_VERSION (use
);
247 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (use
));
250 def_loop
= def_bb
->loop_father
;
252 /* If the definition is not inside a loop, it is not interesting. */
253 if (!loop_outer (def_loop
))
256 /* If the use is not outside of the loop it is defined in, it is not
258 if (flow_bb_inside_loop_p (def_loop
, bb
))
261 if (!use_blocks
[ver
])
262 use_blocks
[ver
] = BITMAP_ALLOC (NULL
);
263 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
265 bitmap_set_bit (need_phis
, ver
);
268 /* For uses in STMT, mark names that are used outside of the loop they are
269 defined to rewrite. Record the set of blocks in that the ssa
270 names are defined to USE_BLOCKS and the ssa names themselves to
274 find_uses_to_rename_stmt (gimple stmt
, bitmap
*use_blocks
, bitmap need_phis
)
278 basic_block bb
= gimple_bb (stmt
);
280 if (is_gimple_debug (stmt
))
283 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
284 find_uses_to_rename_use (bb
, var
, use_blocks
, need_phis
);
287 /* Marks names that are used in BB and outside of the loop they are
288 defined in for rewrite. Records the set of blocks in that the ssa
289 names are defined to USE_BLOCKS. Record the SSA names that will
290 need exit PHIs in NEED_PHIS. */
293 find_uses_to_rename_bb (basic_block bb
, bitmap
*use_blocks
, bitmap need_phis
)
295 gimple_stmt_iterator bsi
;
299 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
300 for (bsi
= gsi_start_phis (e
->dest
); !gsi_end_p (bsi
); gsi_next (&bsi
))
301 find_uses_to_rename_use (bb
, PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi
), e
),
302 use_blocks
, need_phis
);
304 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
305 find_uses_to_rename_stmt (gsi_stmt (bsi
), use_blocks
, need_phis
);
308 /* Marks names that are used outside of the loop they are defined in
309 for rewrite. Records the set of blocks in that the ssa
310 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
311 scan only blocks in this set. */
314 find_uses_to_rename (bitmap changed_bbs
, bitmap
*use_blocks
, bitmap need_phis
)
320 if (changed_bbs
&& !bitmap_empty_p (changed_bbs
))
322 EXECUTE_IF_SET_IN_BITMAP (changed_bbs
, 0, index
, bi
)
324 find_uses_to_rename_bb (BASIC_BLOCK (index
), use_blocks
, need_phis
);
331 find_uses_to_rename_bb (bb
, use_blocks
, need_phis
);
336 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
337 phi nodes to ensure that no variable is used outside the loop it is
340 This strengthening of the basic ssa form has several advantages:
342 1) Updating it during unrolling/peeling/versioning is trivial, since
343 we do not need to care about the uses outside of the loop.
344 2) The behavior of all uses of an induction variable is the same.
345 Without this, you need to distinguish the case when the variable
346 is used outside of the loop it is defined in, for example
348 for (i = 0; i < 100; i++)
350 for (j = 0; j < 100; j++)
358 Looking from the outer loop with the normal SSA form, the first use of k
359 is not well-behaved, while the second one is an induction variable with
362 If CHANGED_BBS is not NULL, we look for uses outside loops only in
363 the basic blocks in this set.
365 UPDATE_FLAG is used in the call to update_ssa. See
366 TODO_update_ssa* for documentation. */
369 rewrite_into_loop_closed_ssa (bitmap changed_bbs
, unsigned update_flag
)
373 unsigned i
, old_num_ssa_names
;
374 bitmap names_to_rename
;
376 loops_state_set (LOOP_CLOSED_SSA
);
377 if (number_of_loops () <= 1)
380 loop_exits
= get_loops_exits ();
381 names_to_rename
= BITMAP_ALLOC (NULL
);
383 /* If the pass has caused the SSA form to be out-of-date, update it
385 update_ssa (update_flag
);
387 old_num_ssa_names
= num_ssa_names
;
388 use_blocks
= XCNEWVEC (bitmap
, old_num_ssa_names
);
390 /* Find the uses outside loops. */
391 find_uses_to_rename (changed_bbs
, use_blocks
, names_to_rename
);
393 /* Add the PHI nodes on exits of the loops for the names we need to
395 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
397 for (i
= 0; i
< old_num_ssa_names
; i
++)
398 BITMAP_FREE (use_blocks
[i
]);
400 BITMAP_FREE (loop_exits
);
401 BITMAP_FREE (names_to_rename
);
403 /* Fix up all the names found to be used outside their original
405 update_ssa (TODO_update_ssa
);
408 /* Check invariants of the loop closed ssa form for the USE in BB. */
411 check_loop_closed_ssa_use (basic_block bb
, tree use
)
416 if (TREE_CODE (use
) != SSA_NAME
|| !is_gimple_reg (use
))
419 def
= SSA_NAME_DEF_STMT (use
);
420 def_bb
= gimple_bb (def
);
422 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
425 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
428 check_loop_closed_ssa_stmt (basic_block bb
, gimple stmt
)
433 if (is_gimple_debug (stmt
))
436 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
437 check_loop_closed_ssa_use (bb
, var
);
440 /* Checks that invariants of the loop closed ssa form are preserved.
441 Call verify_ssa when VERIFY_SSA_P is true. */
444 verify_loop_closed_ssa (bool verify_ssa_p
)
447 gimple_stmt_iterator bsi
;
452 if (number_of_loops () <= 1)
458 timevar_push (TV_VERIFY_LOOP_CLOSED
);
462 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
464 phi
= gsi_stmt (bsi
);
465 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
466 check_loop_closed_ssa_use (e
->src
,
467 PHI_ARG_DEF_FROM_EDGE (phi
, e
));
470 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
471 check_loop_closed_ssa_stmt (bb
, gsi_stmt (bsi
));
474 timevar_pop (TV_VERIFY_LOOP_CLOSED
);
477 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
478 preserve the loop closed ssa form. The newly created block is returned. */
481 split_loop_exit_edge (edge exit
)
483 basic_block dest
= exit
->dest
;
484 basic_block bb
= split_edge (exit
);
488 gimple_stmt_iterator psi
;
489 source_location locus
;
491 for (psi
= gsi_start_phis (dest
); !gsi_end_p (psi
); gsi_next (&psi
))
493 phi
= gsi_stmt (psi
);
494 op_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, single_succ_edge (bb
));
495 locus
= gimple_phi_arg_location_from_edge (phi
, single_succ_edge (bb
));
497 name
= USE_FROM_PTR (op_p
);
499 /* If the argument of the PHI node is a constant, we do not need
500 to keep it inside loop. */
501 if (TREE_CODE (name
) != SSA_NAME
)
504 /* Otherwise create an auxiliary phi node that will copy the value
505 of the SSA name out of the loop. */
506 new_name
= duplicate_ssa_name (name
, NULL
);
507 new_phi
= create_phi_node (new_name
, bb
);
508 SSA_NAME_DEF_STMT (new_name
) = new_phi
;
509 add_phi_arg (new_phi
, name
, exit
, locus
);
510 SET_USE (op_p
, new_name
);
516 /* Returns the basic block in that statements should be emitted for induction
517 variables incremented at the end of the LOOP. */
520 ip_end_pos (struct loop
*loop
)
525 /* Returns the basic block in that statements should be emitted for induction
526 variables incremented just before exit condition of a LOOP. */
529 ip_normal_pos (struct loop
*loop
)
535 if (!single_pred_p (loop
->latch
))
538 bb
= single_pred (loop
->latch
);
539 last
= last_stmt (bb
);
541 || gimple_code (last
) != GIMPLE_COND
)
544 exit
= EDGE_SUCC (bb
, 0);
545 if (exit
->dest
== loop
->latch
)
546 exit
= EDGE_SUCC (bb
, 1);
548 if (flow_bb_inside_loop_p (loop
, exit
->dest
))
554 /* Stores the standard position for induction variable increment in LOOP
555 (just before the exit condition if it is available and latch block is empty,
556 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
557 the increment should be inserted after *BSI. */
560 standard_iv_increment_position (struct loop
*loop
, gimple_stmt_iterator
*bsi
,
563 basic_block bb
= ip_normal_pos (loop
), latch
= ip_end_pos (loop
);
564 gimple last
= last_stmt (latch
);
567 || (last
&& gimple_code (last
) != GIMPLE_LABEL
))
569 *bsi
= gsi_last_bb (latch
);
570 *insert_after
= true;
574 *bsi
= gsi_last_bb (bb
);
575 *insert_after
= false;
579 /* Copies phi node arguments for duplicated blocks. The index of the first
580 duplicated block is FIRST_NEW_BLOCK. */
583 copy_phi_node_args (unsigned first_new_block
)
587 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
588 BASIC_BLOCK (i
)->flags
|= BB_DUPLICATED
;
590 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
591 add_phi_args_after_copy_bb (BASIC_BLOCK (i
));
593 for (i
= first_new_block
; i
< (unsigned) last_basic_block
; i
++)
594 BASIC_BLOCK (i
)->flags
&= ~BB_DUPLICATED
;
598 /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
599 updates the PHI nodes at start of the copied region. In order to
600 achieve this, only loops whose exits all lead to the same location
603 Notice that we do not completely update the SSA web after
604 duplication. The caller is responsible for calling update_ssa
605 after the loop has been duplicated. */
608 gimple_duplicate_loop_to_header_edge (struct loop
*loop
, edge e
,
609 unsigned int ndupl
, sbitmap wont_exit
,
610 edge orig
, VEC (edge
, heap
) **to_remove
,
613 unsigned first_new_block
;
615 if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES
))
617 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
))
620 #ifdef ENABLE_CHECKING
621 if (loops_state_satisfies_p (LOOP_CLOSED_SSA
))
622 verify_loop_closed_ssa (true);
625 first_new_block
= last_basic_block
;
626 if (!duplicate_loop_to_header_edge (loop
, e
, ndupl
, wont_exit
,
627 orig
, to_remove
, flags
))
630 /* Readd the removed phi args for e. */
631 flush_pending_stmts (e
);
633 /* Copy the phi node arguments. */
634 copy_phi_node_args (first_new_block
);
641 /* Returns true if we can unroll LOOP FACTOR times. Number
642 of iterations of the loop is returned in NITER. */
645 can_unroll_loop_p (struct loop
*loop
, unsigned factor
,
646 struct tree_niter_desc
*niter
)
650 /* Check whether unrolling is possible. We only want to unroll loops
651 for that we are able to determine number of iterations. We also
652 want to split the extra iterations of the loop from its end,
653 therefore we require that the loop has precisely one
656 exit
= single_dom_exit (loop
);
660 if (!number_of_iterations_exit (loop
, exit
, niter
, false)
661 || niter
->cmp
== ERROR_MARK
662 /* Scalar evolutions analysis might have copy propagated
663 the abnormal ssa names into these expressions, hence
664 emitting the computations based on them during loop
665 unrolling might create overlapping life ranges for
666 them, and failures in out-of-ssa. */
667 || contains_abnormal_ssa_name_p (niter
->may_be_zero
)
668 || contains_abnormal_ssa_name_p (niter
->control
.base
)
669 || contains_abnormal_ssa_name_p (niter
->control
.step
)
670 || contains_abnormal_ssa_name_p (niter
->bound
))
673 /* And of course, we must be able to duplicate the loop. */
674 if (!can_duplicate_loop_p (loop
))
677 /* The final loop should be small enough. */
678 if (tree_num_loop_insns (loop
, &eni_size_weights
) * factor
679 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS
))
685 /* Determines the conditions that control execution of LOOP unrolled FACTOR
686 times. DESC is number of iterations of LOOP. ENTER_COND is set to
687 condition that must be true if the main loop can be entered.
688 EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
689 how the exit from the unrolled loop should be controlled. */
692 determine_exit_conditions (struct loop
*loop
, struct tree_niter_desc
*desc
,
693 unsigned factor
, tree
*enter_cond
,
694 tree
*exit_base
, tree
*exit_step
,
695 enum tree_code
*exit_cmp
, tree
*exit_bound
)
698 tree base
= desc
->control
.base
;
699 tree step
= desc
->control
.step
;
700 tree bound
= desc
->bound
;
701 tree type
= TREE_TYPE (step
);
703 tree min
= lower_bound_in_type (type
, type
);
704 tree max
= upper_bound_in_type (type
, type
);
705 enum tree_code cmp
= desc
->cmp
;
706 tree cond
= boolean_true_node
, assum
;
708 /* For pointers, do the arithmetics in the type of step. */
709 base
= fold_convert (type
, base
);
710 bound
= fold_convert (type
, bound
);
712 *enter_cond
= boolean_false_node
;
713 *exit_base
= NULL_TREE
;
714 *exit_step
= NULL_TREE
;
715 *exit_cmp
= ERROR_MARK
;
716 *exit_bound
= NULL_TREE
;
717 gcc_assert (cmp
!= ERROR_MARK
);
719 /* We only need to be correct when we answer question
720 "Do at least FACTOR more iterations remain?" in the unrolled loop.
721 Thus, transforming BASE + STEP * i <> BOUND to
722 BASE + STEP * i < BOUND is ok. */
725 if (tree_int_cst_sign_bit (step
))
730 else if (cmp
== LT_EXPR
)
732 gcc_assert (!tree_int_cst_sign_bit (step
));
734 else if (cmp
== GT_EXPR
)
736 gcc_assert (tree_int_cst_sign_bit (step
));
741 /* The main body of the loop may be entered iff:
743 1) desc->may_be_zero is false.
744 2) it is possible to check that there are at least FACTOR iterations
745 of the loop, i.e., BOUND - step * FACTOR does not overflow.
746 3) # of iterations is at least FACTOR */
748 if (!integer_zerop (desc
->may_be_zero
))
749 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
750 invert_truthvalue (desc
->may_be_zero
),
753 bigstep
= fold_build2 (MULT_EXPR
, type
, step
,
754 build_int_cst_type (type
, factor
));
755 delta
= fold_build2 (MINUS_EXPR
, type
, bigstep
, step
);
757 assum
= fold_build2 (GE_EXPR
, boolean_type_node
,
759 fold_build2 (PLUS_EXPR
, type
, min
, delta
));
761 assum
= fold_build2 (LE_EXPR
, boolean_type_node
,
763 fold_build2 (PLUS_EXPR
, type
, max
, delta
));
764 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
766 bound
= fold_build2 (MINUS_EXPR
, type
, bound
, delta
);
767 assum
= fold_build2 (cmp
, boolean_type_node
, base
, bound
);
768 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, assum
, cond
);
770 cond
= force_gimple_operand (unshare_expr (cond
), &stmts
, false, NULL_TREE
);
772 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
773 /* cond now may be a gimple comparison, which would be OK, but also any
774 other gimple rhs (say a && b). In this case we need to force it to
776 if (!is_gimple_condexpr (cond
))
778 cond
= force_gimple_operand (cond
, &stmts
, true, NULL_TREE
);
780 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
784 base
= force_gimple_operand (unshare_expr (base
), &stmts
, true, NULL_TREE
);
786 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
787 bound
= force_gimple_operand (unshare_expr (bound
), &stmts
, true, NULL_TREE
);
789 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
792 *exit_step
= bigstep
;
797 /* Scales the frequencies of all basic blocks in LOOP that are strictly
798 dominated by BB by NUM/DEN. */
801 scale_dominated_blocks_in_loop (struct loop
*loop
, basic_block bb
,
809 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
811 son
= next_dom_son (CDI_DOMINATORS
, son
))
813 if (!flow_bb_inside_loop_p (loop
, son
))
815 scale_bbs_frequencies_int (&son
, 1, num
, den
);
816 scale_dominated_blocks_in_loop (loop
, son
, num
, den
);
820 /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
821 EXIT is the exit of the loop to that DESC corresponds.
823 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
824 under that loop exits in the first iteration even if N != 0,
828 x = phi (init, next);
836 becomes (with possibly the exit conditions formulated a bit differently,
837 avoiding the need to create a new iv):
839 if (MAY_BE_ZERO || N < FACTOR)
844 x = phi (init, next);
855 } while (N >= FACTOR);
858 init' = phi (init, x);
862 x = phi (init', next);
870 Before the loop is unrolled, TRANSFORM is called for it (only for the
871 unrolled loop, but not for its versioned copy). DATA is passed to
874 /* Probability in % that the unrolled loop is entered. Just a guess. */
875 #define PROB_UNROLLED_LOOP_ENTERED 90
878 tree_transform_and_unroll_loop (struct loop
*loop
, unsigned factor
,
879 edge exit
, struct tree_niter_desc
*desc
,
880 transform_callback transform
,
884 tree ctr_before
, ctr_after
;
885 tree enter_main_cond
, exit_base
, exit_step
, exit_bound
;
886 enum tree_code exit_cmp
;
887 gimple phi_old_loop
, phi_new_loop
, phi_rest
;
888 gimple_stmt_iterator psi_old_loop
, psi_new_loop
;
889 tree init
, next
, new_init
, var
;
890 struct loop
*new_loop
;
891 basic_block rest
, exit_bb
;
892 edge old_entry
, new_entry
, old_latch
, precond_edge
, new_exit
;
894 gimple_stmt_iterator bsi
;
897 unsigned est_niter
, prob_entry
, scale_unrolled
, scale_rest
, freq_e
, freq_h
;
898 unsigned new_est_niter
, i
, prob
;
899 unsigned irr
= loop_preheader_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
;
901 VEC (edge
, heap
) *to_remove
= NULL
;
903 est_niter
= expected_loop_iterations (loop
);
904 determine_exit_conditions (loop
, desc
, factor
,
905 &enter_main_cond
, &exit_base
, &exit_step
,
906 &exit_cmp
, &exit_bound
);
908 /* Let us assume that the unrolled loop is quite likely to be entered. */
909 if (integer_nonzerop (enter_main_cond
))
910 prob_entry
= REG_BR_PROB_BASE
;
912 prob_entry
= PROB_UNROLLED_LOOP_ENTERED
* REG_BR_PROB_BASE
/ 100;
914 /* The values for scales should keep profile consistent, and somewhat close
917 TODO: The current value of SCALE_REST makes it appear that the loop that
918 is created by splitting the remaining iterations of the unrolled loop is
919 executed the same number of times as the original loop, and with the same
920 frequencies, which is obviously wrong. This does not appear to cause
921 problems, so we do not bother with fixing it for now. To make the profile
922 correct, we would need to change the probability of the exit edge of the
923 loop, and recompute the distribution of frequencies in its body because
924 of this change (scale the frequencies of blocks before and after the exit
925 by appropriate factors). */
926 scale_unrolled
= prob_entry
;
927 scale_rest
= REG_BR_PROB_BASE
;
929 new_loop
= loop_version (loop
, enter_main_cond
, NULL
,
930 prob_entry
, scale_unrolled
, scale_rest
, true);
931 gcc_assert (new_loop
!= NULL
);
932 update_ssa (TODO_update_ssa
);
934 /* Determine the probability of the exit edge of the unrolled loop. */
935 new_est_niter
= est_niter
/ factor
;
937 /* Without profile feedback, loops for that we do not know a better estimate
938 are assumed to roll 10 times. When we unroll such loop, it appears to
939 roll too little, and it may even seem to be cold. To avoid this, we
940 ensure that the created loop appears to roll at least 5 times (but at
941 most as many times as before unrolling). */
942 if (new_est_niter
< 5)
945 new_est_niter
= est_niter
;
950 /* Prepare the cfg and update the phi nodes. Move the loop exit to the
951 loop latch (and make its condition dummy, for the moment). */
952 rest
= loop_preheader_edge (new_loop
)->src
;
953 precond_edge
= single_pred_edge (rest
);
954 split_edge (loop_latch_edge (loop
));
955 exit_bb
= single_pred (loop
->latch
);
957 /* Since the exit edge will be removed, the frequency of all the blocks
958 in the loop that are dominated by it must be scaled by
959 1 / (1 - exit->probability). */
960 scale_dominated_blocks_in_loop (loop
, exit
->src
,
962 REG_BR_PROB_BASE
- exit
->probability
);
964 bsi
= gsi_last_bb (exit_bb
);
965 exit_if
= gimple_build_cond (EQ_EXPR
, integer_zero_node
,
967 NULL_TREE
, NULL_TREE
);
969 gsi_insert_after (&bsi
, exit_if
, GSI_NEW_STMT
);
970 new_exit
= make_edge (exit_bb
, rest
, EDGE_FALSE_VALUE
| irr
);
971 rescan_loop_exit (new_exit
, true, false);
973 /* Set the probability of new exit to the same of the old one. Fix
974 the frequency of the latch block, by scaling it back by
975 1 - exit->probability. */
976 new_exit
->count
= exit
->count
;
977 new_exit
->probability
= exit
->probability
;
978 new_nonexit
= single_pred_edge (loop
->latch
);
979 new_nonexit
->probability
= REG_BR_PROB_BASE
- exit
->probability
;
980 new_nonexit
->flags
= EDGE_TRUE_VALUE
;
981 new_nonexit
->count
-= exit
->count
;
982 if (new_nonexit
->count
< 0)
983 new_nonexit
->count
= 0;
984 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
987 old_entry
= loop_preheader_edge (loop
);
988 new_entry
= loop_preheader_edge (new_loop
);
989 old_latch
= loop_latch_edge (loop
);
990 for (psi_old_loop
= gsi_start_phis (loop
->header
),
991 psi_new_loop
= gsi_start_phis (new_loop
->header
);
992 !gsi_end_p (psi_old_loop
);
993 gsi_next (&psi_old_loop
), gsi_next (&psi_new_loop
))
995 phi_old_loop
= gsi_stmt (psi_old_loop
);
996 phi_new_loop
= gsi_stmt (psi_new_loop
);
998 init
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_entry
);
999 op
= PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop
, new_entry
);
1000 gcc_assert (operand_equal_for_phi_arg_p (init
, USE_FROM_PTR (op
)));
1001 next
= PHI_ARG_DEF_FROM_EDGE (phi_old_loop
, old_latch
);
1003 /* Prefer using original variable as a base for the new ssa name.
1004 This is necessary for virtual ops, and useful in order to avoid
1005 losing debug info for real ops. */
1006 if (TREE_CODE (next
) == SSA_NAME
1007 && useless_type_conversion_p (TREE_TYPE (next
),
1009 var
= SSA_NAME_VAR (next
);
1010 else if (TREE_CODE (init
) == SSA_NAME
1011 && useless_type_conversion_p (TREE_TYPE (init
),
1013 var
= SSA_NAME_VAR (init
);
1014 else if (useless_type_conversion_p (TREE_TYPE (next
), TREE_TYPE (init
)))
1016 var
= create_tmp_var (TREE_TYPE (next
), "unrinittmp");
1017 add_referenced_var (var
);
1021 var
= create_tmp_var (TREE_TYPE (init
), "unrinittmp");
1022 add_referenced_var (var
);
1025 new_init
= make_ssa_name (var
, NULL
);
1026 phi_rest
= create_phi_node (new_init
, rest
);
1027 SSA_NAME_DEF_STMT (new_init
) = phi_rest
;
1029 add_phi_arg (phi_rest
, init
, precond_edge
, UNKNOWN_LOCATION
);
1030 add_phi_arg (phi_rest
, next
, new_exit
, UNKNOWN_LOCATION
);
1031 SET_USE (op
, new_init
);
1036 /* Transform the loop. */
1038 (*transform
) (loop
, data
);
1040 /* Unroll the loop and remove the exits in all iterations except for the
1042 wont_exit
= sbitmap_alloc (factor
);
1043 sbitmap_ones (wont_exit
);
1044 RESET_BIT (wont_exit
, factor
- 1);
1046 ok
= gimple_duplicate_loop_to_header_edge
1047 (loop
, loop_latch_edge (loop
), factor
- 1,
1048 wont_exit
, new_exit
, &to_remove
, DLTHE_FLAG_UPDATE_FREQ
);
1052 FOR_EACH_VEC_ELT (edge
, to_remove
, i
, e
)
1054 ok
= remove_path (e
);
1057 VEC_free (edge
, heap
, to_remove
);
1058 update_ssa (TODO_update_ssa
);
1060 /* Ensure that the frequencies in the loop match the new estimated
1061 number of iterations, and change the probability of the new
1063 freq_h
= loop
->header
->frequency
;
1064 freq_e
= EDGE_FREQUENCY (loop_preheader_edge (loop
));
1066 scale_loop_frequencies (loop
, freq_e
* (new_est_niter
+ 1), freq_h
);
1068 exit_bb
= single_pred (loop
->latch
);
1069 new_exit
= find_edge (exit_bb
, rest
);
1070 new_exit
->count
= loop_preheader_edge (loop
)->count
;
1071 new_exit
->probability
= REG_BR_PROB_BASE
/ (new_est_niter
+ 1);
1073 rest
->count
+= new_exit
->count
;
1074 rest
->frequency
+= EDGE_FREQUENCY (new_exit
);
1076 new_nonexit
= single_pred_edge (loop
->latch
);
1077 prob
= new_nonexit
->probability
;
1078 new_nonexit
->probability
= REG_BR_PROB_BASE
- new_exit
->probability
;
1079 new_nonexit
->count
= exit_bb
->count
- new_exit
->count
;
1080 if (new_nonexit
->count
< 0)
1081 new_nonexit
->count
= 0;
1083 scale_bbs_frequencies_int (&loop
->latch
, 1, new_nonexit
->probability
,
1086 /* Finally create the new counter for number of iterations and add the new
1087 exit instruction. */
1088 bsi
= gsi_last_nondebug_bb (exit_bb
);
1089 exit_if
= gsi_stmt (bsi
);
1090 create_iv (exit_base
, exit_step
, NULL_TREE
, loop
,
1091 &bsi
, false, &ctr_before
, &ctr_after
);
1092 gimple_cond_set_code (exit_if
, exit_cmp
);
1093 gimple_cond_set_lhs (exit_if
, ctr_after
);
1094 gimple_cond_set_rhs (exit_if
, exit_bound
);
1095 update_stmt (exit_if
);
1097 #ifdef ENABLE_CHECKING
1098 verify_flow_info ();
1099 verify_dominators (CDI_DOMINATORS
);
1100 verify_loop_structure ();
1101 verify_loop_closed_ssa (true);
1105 /* Wrapper over tree_transform_and_unroll_loop for case we do not
1106 want to transform the loop before unrolling. The meaning
1107 of the arguments is the same as for tree_transform_and_unroll_loop. */
1110 tree_unroll_loop (struct loop
*loop
, unsigned factor
,
1111 edge exit
, struct tree_niter_desc
*desc
)
1113 tree_transform_and_unroll_loop (loop
, factor
, exit
, desc
,
1117 /* Rewrite the phi node at position PSI in function of the main
1118 induction variable MAIN_IV and insert the generated code at GSI. */
1121 rewrite_phi_with_iv (loop_p loop
,
1122 gimple_stmt_iterator
*psi
,
1123 gimple_stmt_iterator
*gsi
,
1127 gimple stmt
, phi
= gsi_stmt (*psi
);
1128 tree atype
, mtype
, val
, res
= PHI_RESULT (phi
);
1130 if (!is_gimple_reg (res
) || res
== main_iv
)
1136 if (!simple_iv (loop
, loop
, res
, &iv
, true))
1142 remove_phi_node (psi
, false);
1144 atype
= TREE_TYPE (res
);
1145 mtype
= POINTER_TYPE_P (atype
) ? sizetype
: atype
;
1146 val
= fold_build2 (MULT_EXPR
, mtype
, unshare_expr (iv
.step
),
1147 fold_convert (mtype
, main_iv
));
1148 val
= fold_build2 (POINTER_TYPE_P (atype
)
1149 ? POINTER_PLUS_EXPR
: PLUS_EXPR
,
1150 atype
, unshare_expr (iv
.base
), val
);
1151 val
= force_gimple_operand_gsi (gsi
, val
, false, NULL_TREE
, true,
1153 stmt
= gimple_build_assign (res
, val
);
1154 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
1155 SSA_NAME_DEF_STMT (res
) = stmt
;
1158 /* Rewrite all the phi nodes of LOOP in function of the main induction
1159 variable MAIN_IV. */
1162 rewrite_all_phi_nodes_with_iv (loop_p loop
, tree main_iv
)
1165 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
1166 gimple_stmt_iterator psi
;
1168 for (i
= 0; i
< loop
->num_nodes
; i
++)
1170 basic_block bb
= bbs
[i
];
1171 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
1173 if (bb
->loop_father
!= loop
)
1176 for (psi
= gsi_start_phis (bb
); !gsi_end_p (psi
); )
1177 rewrite_phi_with_iv (loop
, &psi
, &gsi
, main_iv
);
1183 /* Bases all the induction variables in LOOP on a single induction
1184 variable (unsigned with base 0 and step 1), whose final value is
1185 compared with *NIT. When the IV type precision has to be larger
1186 than *NIT type precision, *NIT is converted to the larger type, the
1187 conversion code is inserted before the loop, and *NIT is updated to
1188 the new definition. When BUMP_IN_LATCH is true, the induction
1189 variable is incremented in the loop latch, otherwise it is
1190 incremented in the loop header. Return the induction variable that
1194 canonicalize_loop_ivs (struct loop
*loop
, tree
*nit
, bool bump_in_latch
)
1196 unsigned precision
= TYPE_PRECISION (TREE_TYPE (*nit
));
1197 unsigned original_precision
= precision
;
1198 tree type
, var_before
;
1199 gimple_stmt_iterator gsi
, psi
;
1201 edge exit
= single_dom_exit (loop
);
1203 enum machine_mode mode
;
1204 bool unsigned_p
= false;
1206 for (psi
= gsi_start_phis (loop
->header
);
1207 !gsi_end_p (psi
); gsi_next (&psi
))
1209 gimple phi
= gsi_stmt (psi
);
1210 tree res
= PHI_RESULT (phi
);
1213 type
= TREE_TYPE (res
);
1214 if (!is_gimple_reg (res
)
1215 || (!INTEGRAL_TYPE_P (type
)
1216 && !POINTER_TYPE_P (type
))
1217 || TYPE_PRECISION (type
) < precision
)
1220 uns
= POINTER_TYPE_P (type
) | TYPE_UNSIGNED (type
);
1222 if (TYPE_PRECISION (type
) > precision
)
1227 precision
= TYPE_PRECISION (type
);
1230 mode
= smallest_mode_for_size (precision
, MODE_INT
);
1231 precision
= GET_MODE_PRECISION (mode
);
1232 type
= build_nonstandard_integer_type (precision
, unsigned_p
);
1234 if (original_precision
!= precision
)
1236 *nit
= fold_convert (type
, *nit
);
1237 *nit
= force_gimple_operand (*nit
, &stmts
, true, NULL_TREE
);
1239 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
1243 gsi
= gsi_last_bb (loop
->latch
);
1245 gsi
= gsi_last_nondebug_bb (loop
->header
);
1246 create_iv (build_int_cst_type (type
, 0), build_int_cst (type
, 1), NULL_TREE
,
1247 loop
, &gsi
, bump_in_latch
, &var_before
, NULL
);
1249 rewrite_all_phi_nodes_with_iv (loop
, var_before
);
1251 stmt
= last_stmt (exit
->src
);
1252 /* Make the loop exit if the control condition is not satisfied. */
1253 if (exit
->flags
& EDGE_TRUE_VALUE
)
1257 extract_true_false_edges_from_block (exit
->src
, &te
, &fe
);
1258 te
->flags
= EDGE_FALSE_VALUE
;
1259 fe
->flags
= EDGE_TRUE_VALUE
;
1261 gimple_cond_set_code (stmt
, LT_EXPR
);
1262 gimple_cond_set_lhs (stmt
, var_before
);
1263 gimple_cond_set_rhs (stmt
, *nit
);