1 /* Vectorizer Specific Loop Manipulations
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "tree-pass.h"
31 #include "fold-const.h"
34 #include "gimple-iterator.h"
35 #include "gimplify-me.h"
37 #include "tree-ssa-loop-manip.h"
38 #include "tree-into-ssa.h"
41 #include "tree-scalar-evolution.h"
42 #include "tree-vectorizer.h"
44 /*************************************************************************
45 Simple Loop Peeling Utilities
47 Utilities to support loop peeling for vectorization purposes.
48 *************************************************************************/
51 /* Renames the use *OP_P. */
54 rename_use_op (use_operand_p op_p
)
58 if (TREE_CODE (USE_FROM_PTR (op_p
)) != SSA_NAME
)
61 new_name
= get_current_def (USE_FROM_PTR (op_p
));
63 /* Something defined outside of the loop. */
67 /* An ordinary ssa name defined in the loop. */
69 SET_USE (op_p
, new_name
);
73 /* Renames the variables in basic block BB. Allow renaming of PHI argumnets
74 on edges incoming from outer-block header if RENAME_FROM_OUTER_LOOP is
78 rename_variables_in_bb (basic_block bb
, bool rename_from_outer_loop
)
85 struct loop
*loop
= bb
->loop_father
;
86 struct loop
*outer_loop
= NULL
;
88 if (rename_from_outer_loop
)
91 outer_loop
= loop_outer (loop
);
94 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
97 stmt
= gsi_stmt (gsi
);
98 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
99 rename_use_op (use_p
);
102 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
104 if (!flow_bb_inside_loop_p (loop
, e
->src
)
105 && (!rename_from_outer_loop
|| e
->src
!= outer_loop
->header
))
107 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
109 rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (gsi
.phi (), e
));
120 /* A stack of values to be adjusted in debug stmts. We have to
121 process them LIFO, so that the closest substitution applies. If we
122 processed them FIFO, without the stack, we might substitute uses
123 with a PHI DEF that would soon become non-dominant, and when we got
124 to the suitable one, it wouldn't have anything to substitute any
126 static vec
<adjust_info
, va_heap
> adjust_vec
;
128 /* Adjust any debug stmts that referenced AI->from values to use the
129 loop-closed AI->to, if the references are dominated by AI->bb and
130 not by the definition of AI->from. */
133 adjust_debug_stmts_now (adjust_info
*ai
)
135 basic_block bbphi
= ai
->bb
;
136 tree orig_def
= ai
->from
;
137 tree new_def
= ai
->to
;
138 imm_use_iterator imm_iter
;
140 basic_block bbdef
= gimple_bb (SSA_NAME_DEF_STMT (orig_def
));
142 gcc_assert (dom_info_available_p (CDI_DOMINATORS
));
144 /* Adjust any debug stmts that held onto non-loop-closed
146 FOR_EACH_IMM_USE_STMT (stmt
, imm_iter
, orig_def
)
151 if (!is_gimple_debug (stmt
))
154 gcc_assert (gimple_debug_bind_p (stmt
));
156 bbuse
= gimple_bb (stmt
);
159 || dominated_by_p (CDI_DOMINATORS
, bbuse
, bbphi
))
161 || dominated_by_p (CDI_DOMINATORS
, bbuse
, bbdef
)))
164 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
165 SET_USE (use_p
, new_def
);
168 gimple_debug_bind_reset_value (stmt
);
175 /* Adjust debug stmts as scheduled before. */
178 adjust_vec_debug_stmts (void)
180 if (!MAY_HAVE_DEBUG_STMTS
)
183 gcc_assert (adjust_vec
.exists ());
185 while (!adjust_vec
.is_empty ())
187 adjust_debug_stmts_now (&adjust_vec
.last ());
191 adjust_vec
.release ();
194 /* Adjust any debug stmts that referenced FROM values to use the
195 loop-closed TO, if the references are dominated by BB and not by
196 the definition of FROM. If adjust_vec is non-NULL, adjustments
197 will be postponed until adjust_vec_debug_stmts is called. */
200 adjust_debug_stmts (tree from
, tree to
, basic_block bb
)
204 if (MAY_HAVE_DEBUG_STMTS
205 && TREE_CODE (from
) == SSA_NAME
206 && ! SSA_NAME_IS_DEFAULT_DEF (from
)
207 && ! virtual_operand_p (from
))
213 if (adjust_vec
.exists ())
214 adjust_vec
.safe_push (ai
);
216 adjust_debug_stmts_now (&ai
);
220 /* Change E's phi arg in UPDATE_PHI to NEW_DEF, and record information
221 to adjust any debug stmts that referenced the old phi arg,
222 presumably non-loop-closed references left over from other
226 adjust_phi_and_debug_stmts (gimple
*update_phi
, edge e
, tree new_def
)
228 tree orig_def
= PHI_ARG_DEF_FROM_EDGE (update_phi
, e
);
230 SET_PHI_ARG_DEF (update_phi
, e
->dest_idx
, new_def
);
232 if (MAY_HAVE_DEBUG_STMTS
)
233 adjust_debug_stmts (orig_def
, PHI_RESULT (update_phi
),
234 gimple_bb (update_phi
));
238 /* Update PHI nodes for a guard of the LOOP.
241 - LOOP, GUARD_EDGE: LOOP is a loop for which we added guard code that
242 controls whether LOOP is to be executed. GUARD_EDGE is the edge that
243 originates from the guard-bb, skips LOOP and reaches the (unique) exit
244 bb of LOOP. This loop-exit-bb is an empty bb with one successor.
245 We denote this bb NEW_MERGE_BB because before the guard code was added
246 it had a single predecessor (the LOOP header), and now it became a merge
247 point of two paths - the path that ends with the LOOP exit-edge, and
248 the path that ends with GUARD_EDGE.
249 - NEW_EXIT_BB: New basic block that is added by this function between LOOP
250 and NEW_MERGE_BB. It is used to place loop-closed-ssa-form exit-phis.
252 ===> The CFG before the guard-code was added:
255 if (exit_loop) goto update_bb
256 else goto LOOP_header_bb
259 ==> The CFG after the guard-code was added:
261 if (LOOP_guard_condition) goto new_merge_bb
262 else goto LOOP_header_bb
265 if (exit_loop_condition) goto new_merge_bb
266 else goto LOOP_header_bb
271 ==> The CFG after this function:
273 if (LOOP_guard_condition) goto new_merge_bb
274 else goto LOOP_header_bb
277 if (exit_loop_condition) goto new_exit_bb
278 else goto LOOP_header_bb
285 1. creates and updates the relevant phi nodes to account for the new
286 incoming edge (GUARD_EDGE) into NEW_MERGE_BB. This involves:
287 1.1. Create phi nodes at NEW_MERGE_BB.
288 1.2. Update the phi nodes at the successor of NEW_MERGE_BB (denoted
289 UPDATE_BB). UPDATE_BB was the exit-bb of LOOP before NEW_MERGE_BB
290 2. preserves loop-closed-ssa-form by creating the required phi nodes
291 at the exit of LOOP (i.e, in NEW_EXIT_BB).
293 There are two flavors to this function:
295 slpeel_update_phi_nodes_for_guard1:
296 Here the guard controls whether we enter or skip LOOP, where LOOP is a
297 prolog_loop (loop1 below), and the new phis created in NEW_MERGE_BB are
298 for variables that have phis in the loop header.
300 slpeel_update_phi_nodes_for_guard2:
301 Here the guard controls whether we enter or skip LOOP, where LOOP is an
302 epilog_loop (loop2 below), and the new phis created in NEW_MERGE_BB are
303 for variables that have phis in the loop exit.
305 I.E., the overall structure is:
308 guard1 (goto loop1/merge1_bb)
311 guard2 (goto merge1_bb/merge2_bb)
318 slpeel_update_phi_nodes_for_guard1 takes care of creating phis in
319 loop1_exit_bb and merge1_bb. These are entry phis (phis for the vars
320 that have phis in loop1->header).
322 slpeel_update_phi_nodes_for_guard2 takes care of creating phis in
323 loop2_exit_bb and merge2_bb. These are exit phis (phis for the vars
324 that have phis in next_bb). It also adds some of these phis to
327 slpeel_update_phi_nodes_for_guard1 is always called before
328 slpeel_update_phi_nodes_for_guard2. They are both needed in order
329 to create correct data-flow and loop-closed-ssa-form.
331 Generally slpeel_update_phi_nodes_for_guard1 creates phis for variables
332 that change between iterations of a loop (and therefore have a phi-node
333 at the loop entry), whereas slpeel_update_phi_nodes_for_guard2 creates
334 phis for variables that are used out of the loop (and therefore have
335 loop-closed exit phis). Some variables may be both updated between
336 iterations and used after the loop. This is why in loop1_exit_bb we
337 may need both entry_phis (created by slpeel_update_phi_nodes_for_guard1)
338 and exit phis (created by slpeel_update_phi_nodes_for_guard2).
340 - IS_NEW_LOOP: if IS_NEW_LOOP is true, then LOOP is a newly created copy of
341 an original loop. i.e., we have:
344 guard_bb (goto LOOP/new_merge)
350 If IS_NEW_LOOP is false, then LOOP is an original loop, in which case we
354 guard_bb (goto LOOP/new_merge)
360 The SSA names defined in the original loop have a current
361 reaching definition that records the corresponding new ssa-name
362 used in the new duplicated loop copy.
365 /* Function slpeel_update_phi_nodes_for_guard1
368 - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above.
369 - DEFS - a bitmap of ssa names to mark new names for which we recorded
372 In the context of the overall structure, we have:
375 guard1 (goto loop1/merge1_bb)
378 guard2 (goto merge1_bb/merge2_bb)
385 For each name updated between loop iterations (i.e - for each name that has
386 an entry (loop-header) phi in LOOP) we create a new phi in:
387 1. merge1_bb (to account for the edge from guard1)
388 2. loop1_exit_bb (an exit-phi to keep LOOP in loop-closed form)
392 slpeel_update_phi_nodes_for_guard1 (edge guard_edge
, struct loop
*loop
,
393 bool is_new_loop
, basic_block
*new_exit_bb
)
395 gphi
*orig_phi
, *new_phi
;
396 gphi
*update_phi
, *update_phi2
;
397 tree guard_arg
, loop_arg
;
398 basic_block new_merge_bb
= guard_edge
->dest
;
399 edge e
= EDGE_SUCC (new_merge_bb
, 0);
400 basic_block update_bb
= e
->dest
;
401 basic_block orig_bb
= loop
->header
;
403 tree current_new_name
;
404 gphi_iterator gsi_orig
, gsi_update
;
406 /* Create new bb between loop and new_merge_bb. */
407 *new_exit_bb
= split_edge (single_exit (loop
));
409 new_exit_e
= EDGE_SUCC (*new_exit_bb
, 0);
411 for (gsi_orig
= gsi_start_phis (orig_bb
),
412 gsi_update
= gsi_start_phis (update_bb
);
413 !gsi_end_p (gsi_orig
) && !gsi_end_p (gsi_update
);
414 gsi_next (&gsi_orig
), gsi_next (&gsi_update
))
416 source_location loop_locus
, guard_locus
;
418 orig_phi
= gsi_orig
.phi ();
419 update_phi
= gsi_update
.phi ();
421 /** 1. Handle new-merge-point phis **/
423 /* 1.1. Generate new phi node in NEW_MERGE_BB: */
424 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
425 new_phi
= create_phi_node (new_res
, new_merge_bb
);
427 /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge
428 of LOOP. Set the two phi args in NEW_PHI for these edges: */
429 loop_arg
= PHI_ARG_DEF_FROM_EDGE (orig_phi
, EDGE_SUCC (loop
->latch
, 0));
430 loop_locus
= gimple_phi_arg_location_from_edge (orig_phi
,
431 EDGE_SUCC (loop
->latch
,
433 guard_arg
= PHI_ARG_DEF_FROM_EDGE (orig_phi
, loop_preheader_edge (loop
));
435 = gimple_phi_arg_location_from_edge (orig_phi
,
436 loop_preheader_edge (loop
));
438 add_phi_arg (new_phi
, loop_arg
, new_exit_e
, loop_locus
);
439 add_phi_arg (new_phi
, guard_arg
, guard_edge
, guard_locus
);
441 /* 1.3. Update phi in successor block. */
442 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi
, e
) == loop_arg
443 || PHI_ARG_DEF_FROM_EDGE (update_phi
, e
) == guard_arg
);
444 adjust_phi_and_debug_stmts (update_phi
, e
, PHI_RESULT (new_phi
));
445 update_phi2
= new_phi
;
448 /** 2. Handle loop-closed-ssa-form phis **/
450 if (virtual_operand_p (PHI_RESULT (orig_phi
)))
453 /* 2.1. Generate new phi node in NEW_EXIT_BB: */
454 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
455 new_phi
= create_phi_node (new_res
, *new_exit_bb
);
457 /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */
458 add_phi_arg (new_phi
, loop_arg
, single_exit (loop
), loop_locus
);
460 /* 2.3. Update phi in successor of NEW_EXIT_BB: */
461 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2
, new_exit_e
) == loop_arg
);
462 adjust_phi_and_debug_stmts (update_phi2
, new_exit_e
,
463 PHI_RESULT (new_phi
));
465 /* 2.4. Record the newly created name with set_current_def.
466 We want to find a name such that
467 name = get_current_def (orig_loop_name)
468 and to set its current definition as follows:
469 set_current_def (name, new_phi_name)
471 If LOOP is a new loop then loop_arg is already the name we're
472 looking for. If LOOP is the original loop, then loop_arg is
473 the orig_loop_name and the relevant name is recorded in its
474 current reaching definition. */
476 current_new_name
= loop_arg
;
479 current_new_name
= get_current_def (loop_arg
);
480 /* current_def is not available only if the variable does not
481 change inside the loop, in which case we also don't care
482 about recording a current_def for it because we won't be
483 trying to create loop-exit-phis for it. */
484 if (!current_new_name
)
487 tree new_name
= get_current_def (current_new_name
);
488 /* Because of peeled_chrec optimization it is possible that we have
489 set this earlier. Verify the PHI has the same value. */
492 gimple
*phi
= SSA_NAME_DEF_STMT (new_name
);
493 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
494 && gimple_bb (phi
) == *new_exit_bb
495 && (PHI_ARG_DEF_FROM_EDGE (phi
, single_exit (loop
))
500 set_current_def (current_new_name
, PHI_RESULT (new_phi
));
505 /* Function slpeel_update_phi_nodes_for_guard2
508 - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above.
510 In the context of the overall structure, we have:
513 guard1 (goto loop1/merge1_bb)
516 guard2 (goto merge1_bb/merge2_bb)
523 For each name used out side the loop (i.e - for each name that has an exit
524 phi in next_bb) we create a new phi in:
525 1. merge2_bb (to account for the edge from guard_bb)
526 2. loop2_exit_bb (an exit-phi to keep LOOP in loop-closed form)
527 3. guard2 bb (an exit phi to keep the preceding loop in loop-closed form),
528 if needed (if it wasn't handled by slpeel_update_phis_nodes_for_phi1).
532 slpeel_update_phi_nodes_for_guard2 (edge guard_edge
, struct loop
*loop
,
533 bool is_new_loop
, basic_block
*new_exit_bb
)
535 gphi
*orig_phi
, *new_phi
;
536 gphi
*update_phi
, *update_phi2
;
537 tree guard_arg
, loop_arg
;
538 basic_block new_merge_bb
= guard_edge
->dest
;
539 edge e
= EDGE_SUCC (new_merge_bb
, 0);
540 basic_block update_bb
= e
->dest
;
542 tree orig_def
, orig_def_new_name
;
543 tree new_name
, new_name2
;
547 /* Create new bb between loop and new_merge_bb. */
548 *new_exit_bb
= split_edge (single_exit (loop
));
550 new_exit_e
= EDGE_SUCC (*new_exit_bb
, 0);
552 for (gsi
= gsi_start_phis (update_bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
555 update_phi
= gsi
.phi ();
556 orig_phi
= update_phi
;
557 orig_def
= PHI_ARG_DEF_FROM_EDGE (orig_phi
, e
);
558 /* This loop-closed-phi actually doesn't represent a use
559 out of the loop - the phi arg is a constant. */
560 if (TREE_CODE (orig_def
) != SSA_NAME
)
562 orig_def_new_name
= get_current_def (orig_def
);
565 /** 1. Handle new-merge-point phis **/
567 /* 1.1. Generate new phi node in NEW_MERGE_BB: */
568 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
569 new_phi
= create_phi_node (new_res
, new_merge_bb
);
571 /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge
572 of LOOP. Set the two PHI args in NEW_PHI for these edges: */
574 new_name2
= NULL_TREE
;
575 if (orig_def_new_name
)
577 new_name
= orig_def_new_name
;
578 /* Some variables have both loop-entry-phis and loop-exit-phis.
579 Such variables were given yet newer names by phis placed in
580 guard_bb by slpeel_update_phi_nodes_for_guard1. I.e:
581 new_name2 = get_current_def (get_current_def (orig_name)). */
582 new_name2
= get_current_def (new_name
);
587 guard_arg
= orig_def
;
592 guard_arg
= new_name
;
596 guard_arg
= new_name2
;
598 add_phi_arg (new_phi
, loop_arg
, new_exit_e
, UNKNOWN_LOCATION
);
599 add_phi_arg (new_phi
, guard_arg
, guard_edge
, UNKNOWN_LOCATION
);
601 /* 1.3. Update phi in successor block. */
602 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi
, e
) == orig_def
);
603 adjust_phi_and_debug_stmts (update_phi
, e
, PHI_RESULT (new_phi
));
604 update_phi2
= new_phi
;
607 /** 2. Handle loop-closed-ssa-form phis **/
609 /* 2.1. Generate new phi node in NEW_EXIT_BB: */
610 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
611 new_phi
= create_phi_node (new_res
, *new_exit_bb
);
613 /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */
614 add_phi_arg (new_phi
, loop_arg
, single_exit (loop
), UNKNOWN_LOCATION
);
616 /* 2.3. Update phi in successor of NEW_EXIT_BB: */
617 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2
, new_exit_e
) == loop_arg
);
618 adjust_phi_and_debug_stmts (update_phi2
, new_exit_e
,
619 PHI_RESULT (new_phi
));
622 /** 3. Handle loop-closed-ssa-form phis for first loop **/
624 /* 3.1. Find the relevant names that need an exit-phi in
625 GUARD_BB, i.e. names for which
626 slpeel_update_phi_nodes_for_guard1 had not already created a
627 phi node. This is the case for names that are used outside
628 the loop (and therefore need an exit phi) but are not updated
629 across loop iterations (and therefore don't have a
632 slpeel_update_phi_nodes_for_guard1 is responsible for
633 creating loop-exit phis in GUARD_BB for names that have a
634 loop-header-phi. When such a phi is created we also record
635 the new name in its current definition. If this new name
636 exists, then guard_arg was set to this new name (see 1.2
637 above). Therefore, if guard_arg is not this new name, this
638 is an indication that an exit-phi in GUARD_BB was not yet
639 created, so we take care of it here. */
640 if (guard_arg
== new_name2
)
644 /* 3.2. Generate new phi node in GUARD_BB: */
645 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
646 new_phi
= create_phi_node (new_res
, guard_edge
->src
);
648 /* 3.3. GUARD_BB has one incoming edge: */
649 gcc_assert (EDGE_COUNT (guard_edge
->src
->preds
) == 1);
650 add_phi_arg (new_phi
, arg
, EDGE_PRED (guard_edge
->src
, 0),
653 /* 3.4. Update phi in successor of GUARD_BB: */
654 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2
, guard_edge
)
656 adjust_phi_and_debug_stmts (update_phi2
, guard_edge
,
657 PHI_RESULT (new_phi
));
662 /* Make the LOOP iterate NITERS times. This is done by adding a new IV
663 that starts at zero, increases by one and its limit is NITERS.
665 Assumption: the exit-condition of LOOP is the last stmt in the loop. */
668 slpeel_make_loop_iterate_ntimes (struct loop
*loop
, tree niters
)
670 tree indx_before_incr
, indx_after_incr
;
673 edge exit_edge
= single_exit (loop
);
674 gimple_stmt_iterator loop_cond_gsi
;
675 gimple_stmt_iterator incr_gsi
;
677 tree init
= build_int_cst (TREE_TYPE (niters
), 0);
678 tree step
= build_int_cst (TREE_TYPE (niters
), 1);
679 source_location loop_loc
;
682 orig_cond
= get_loop_exit_condition (loop
);
683 gcc_assert (orig_cond
);
684 loop_cond_gsi
= gsi_for_stmt (orig_cond
);
686 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
687 create_iv (init
, step
, NULL_TREE
, loop
,
688 &incr_gsi
, insert_after
, &indx_before_incr
, &indx_after_incr
);
690 indx_after_incr
= force_gimple_operand_gsi (&loop_cond_gsi
, indx_after_incr
,
691 true, NULL_TREE
, true,
693 niters
= force_gimple_operand_gsi (&loop_cond_gsi
, niters
, true, NULL_TREE
,
694 true, GSI_SAME_STMT
);
696 code
= (exit_edge
->flags
& EDGE_TRUE_VALUE
) ? GE_EXPR
: LT_EXPR
;
697 cond_stmt
= gimple_build_cond (code
, indx_after_incr
, niters
, NULL_TREE
,
700 gsi_insert_before (&loop_cond_gsi
, cond_stmt
, GSI_SAME_STMT
);
702 /* Remove old loop exit test: */
703 gsi_remove (&loop_cond_gsi
, true);
704 free_stmt_vec_info (orig_cond
);
706 loop_loc
= find_loop_location (loop
);
707 if (dump_enabled_p ())
709 if (LOCATION_LOCUS (loop_loc
) != UNKNOWN_LOCATION
)
710 dump_printf (MSG_NOTE
, "\nloop at %s:%d: ", LOCATION_FILE (loop_loc
),
711 LOCATION_LINE (loop_loc
));
712 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, cond_stmt
, 0);
713 dump_printf (MSG_NOTE
, "\n");
715 loop
->nb_iterations
= niters
;
718 /* Helper routine of slpeel_tree_duplicate_loop_to_edge_cfg.
719 For all PHI arguments in FROM->dest and TO->dest from those
720 edges ensure that TO->dest PHI arguments have current_def
724 slpeel_duplicate_current_defs_from_edges (edge from
, edge to
)
726 gimple_stmt_iterator gsi_from
, gsi_to
;
728 for (gsi_from
= gsi_start_phis (from
->dest
),
729 gsi_to
= gsi_start_phis (to
->dest
);
730 !gsi_end_p (gsi_from
) && !gsi_end_p (gsi_to
);
731 gsi_next (&gsi_from
), gsi_next (&gsi_to
))
733 gimple
*from_phi
= gsi_stmt (gsi_from
);
734 gimple
*to_phi
= gsi_stmt (gsi_to
);
735 tree from_arg
= PHI_ARG_DEF_FROM_EDGE (from_phi
, from
);
736 tree to_arg
= PHI_ARG_DEF_FROM_EDGE (to_phi
, to
);
737 if (TREE_CODE (from_arg
) == SSA_NAME
738 && TREE_CODE (to_arg
) == SSA_NAME
739 && get_current_def (to_arg
) == NULL_TREE
)
740 set_current_def (to_arg
, get_current_def (from_arg
));
745 /* Given LOOP this function generates a new copy of it and puts it
746 on E which is either the entry or exit of LOOP. If SCALAR_LOOP is
747 non-NULL, assume LOOP and SCALAR_LOOP are equivalent and copy the
748 basic blocks from SCALAR_LOOP instead of LOOP, but to either the
749 entry or exit of LOOP. */
752 slpeel_tree_duplicate_loop_to_edge_cfg (struct loop
*loop
,
753 struct loop
*scalar_loop
, edge e
)
755 struct loop
*new_loop
;
756 basic_block
*new_bbs
, *bbs
;
759 basic_block exit_dest
;
761 bool duplicate_outer_loop
= false;
763 exit
= single_exit (loop
);
764 at_exit
= (e
== exit
);
765 if (!at_exit
&& e
!= loop_preheader_edge (loop
))
768 if (scalar_loop
== NULL
)
771 bbs
= XNEWVEC (basic_block
, scalar_loop
->num_nodes
+ 1);
772 get_loop_body_with_size (scalar_loop
, bbs
, scalar_loop
->num_nodes
);
773 /* Allow duplication of outer loops. */
774 if (scalar_loop
->inner
)
775 duplicate_outer_loop
= true;
776 /* Check whether duplication is possible. */
777 if (!can_copy_bbs_p (bbs
, scalar_loop
->num_nodes
))
783 /* Generate new loop structure. */
784 new_loop
= duplicate_loop (scalar_loop
, loop_outer (scalar_loop
));
785 duplicate_subloops (scalar_loop
, new_loop
);
787 exit_dest
= exit
->dest
;
788 was_imm_dom
= (get_immediate_dominator (CDI_DOMINATORS
,
789 exit_dest
) == loop
->header
?
792 /* Also copy the pre-header, this avoids jumping through hoops to
793 duplicate the loop entry PHI arguments. Create an empty
794 pre-header unconditionally for this. */
795 basic_block preheader
= split_edge (loop_preheader_edge (scalar_loop
));
796 edge entry_e
= single_pred_edge (preheader
);
797 bbs
[scalar_loop
->num_nodes
] = preheader
;
798 new_bbs
= XNEWVEC (basic_block
, scalar_loop
->num_nodes
+ 1);
800 exit
= single_exit (scalar_loop
);
801 copy_bbs (bbs
, scalar_loop
->num_nodes
+ 1, new_bbs
,
802 &exit
, 1, &new_exit
, NULL
,
804 exit
= single_exit (loop
);
805 basic_block new_preheader
= new_bbs
[scalar_loop
->num_nodes
];
807 add_phi_args_after_copy (new_bbs
, scalar_loop
->num_nodes
+ 1, NULL
);
809 if (scalar_loop
!= loop
)
811 /* If we copied from SCALAR_LOOP rather than LOOP, SSA_NAMEs from
812 SCALAR_LOOP will have current_def set to SSA_NAMEs in the new_loop,
813 but LOOP will not. slpeel_update_phi_nodes_for_guard{1,2} expects
814 the LOOP SSA_NAMEs (on the exit edge and edge from latch to
815 header) to have current_def set, so copy them over. */
816 slpeel_duplicate_current_defs_from_edges (single_exit (scalar_loop
),
818 slpeel_duplicate_current_defs_from_edges (EDGE_SUCC (scalar_loop
->latch
,
820 EDGE_SUCC (loop
->latch
, 0));
823 if (at_exit
) /* Add the loop copy at exit. */
825 if (scalar_loop
!= loop
)
828 new_exit
= redirect_edge_and_branch (new_exit
, exit_dest
);
830 for (gsi
= gsi_start_phis (exit_dest
); !gsi_end_p (gsi
);
833 gphi
*phi
= gsi
.phi ();
834 tree orig_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
835 location_t orig_locus
836 = gimple_phi_arg_location_from_edge (phi
, e
);
838 add_phi_arg (phi
, orig_arg
, new_exit
, orig_locus
);
841 redirect_edge_and_branch_force (e
, new_preheader
);
842 flush_pending_stmts (e
);
843 set_immediate_dominator (CDI_DOMINATORS
, new_preheader
, e
->src
);
844 if (was_imm_dom
|| duplicate_outer_loop
)
845 set_immediate_dominator (CDI_DOMINATORS
, exit_dest
, new_exit
->src
);
847 /* And remove the non-necessary forwarder again. Keep the other
848 one so we have a proper pre-header for the loop at the exit edge. */
849 redirect_edge_pred (single_succ_edge (preheader
),
850 single_pred (preheader
));
851 delete_basic_block (preheader
);
852 set_immediate_dominator (CDI_DOMINATORS
, scalar_loop
->header
,
853 loop_preheader_edge (scalar_loop
)->src
);
855 else /* Add the copy at entry. */
857 if (scalar_loop
!= loop
)
859 /* Remove the non-necessary forwarder of scalar_loop again. */
860 redirect_edge_pred (single_succ_edge (preheader
),
861 single_pred (preheader
));
862 delete_basic_block (preheader
);
863 set_immediate_dominator (CDI_DOMINATORS
, scalar_loop
->header
,
864 loop_preheader_edge (scalar_loop
)->src
);
865 preheader
= split_edge (loop_preheader_edge (loop
));
866 entry_e
= single_pred_edge (preheader
);
869 redirect_edge_and_branch_force (entry_e
, new_preheader
);
870 flush_pending_stmts (entry_e
);
871 set_immediate_dominator (CDI_DOMINATORS
, new_preheader
, entry_e
->src
);
873 redirect_edge_and_branch_force (new_exit
, preheader
);
874 flush_pending_stmts (new_exit
);
875 set_immediate_dominator (CDI_DOMINATORS
, preheader
, new_exit
->src
);
877 /* And remove the non-necessary forwarder again. Keep the other
878 one so we have a proper pre-header for the loop at the exit edge. */
879 redirect_edge_pred (single_succ_edge (new_preheader
),
880 single_pred (new_preheader
));
881 delete_basic_block (new_preheader
);
882 set_immediate_dominator (CDI_DOMINATORS
, new_loop
->header
,
883 loop_preheader_edge (new_loop
)->src
);
886 for (unsigned i
= 0; i
< scalar_loop
->num_nodes
+ 1; i
++)
887 rename_variables_in_bb (new_bbs
[i
], duplicate_outer_loop
);
889 if (scalar_loop
!= loop
)
891 /* Update new_loop->header PHIs, so that on the preheader
892 edge they are the ones from loop rather than scalar_loop. */
893 gphi_iterator gsi_orig
, gsi_new
;
894 edge orig_e
= loop_preheader_edge (loop
);
895 edge new_e
= loop_preheader_edge (new_loop
);
897 for (gsi_orig
= gsi_start_phis (loop
->header
),
898 gsi_new
= gsi_start_phis (new_loop
->header
);
899 !gsi_end_p (gsi_orig
) && !gsi_end_p (gsi_new
);
900 gsi_next (&gsi_orig
), gsi_next (&gsi_new
))
902 gphi
*orig_phi
= gsi_orig
.phi ();
903 gphi
*new_phi
= gsi_new
.phi ();
904 tree orig_arg
= PHI_ARG_DEF_FROM_EDGE (orig_phi
, orig_e
);
905 location_t orig_locus
906 = gimple_phi_arg_location_from_edge (orig_phi
, orig_e
);
908 add_phi_arg (new_phi
, orig_arg
, new_e
, orig_locus
);
915 checking_verify_dominators (CDI_DOMINATORS
);
921 /* Given the condition statement COND, put it as the last statement
922 of GUARD_BB; EXIT_BB is the basic block to skip the loop;
923 Assumes that this is the single exit of the guarded loop.
924 Returns the skip edge, inserts new stmts on the COND_EXPR_STMT_LIST. */
927 slpeel_add_loop_guard (basic_block guard_bb
, tree cond
,
928 gimple_seq cond_expr_stmt_list
,
929 basic_block exit_bb
, basic_block dom_bb
,
932 gimple_stmt_iterator gsi
;
935 gimple_seq gimplify_stmt_list
= NULL
;
937 enter_e
= EDGE_SUCC (guard_bb
, 0);
938 enter_e
->flags
&= ~EDGE_FALLTHRU
;
939 enter_e
->flags
|= EDGE_FALSE_VALUE
;
940 gsi
= gsi_last_bb (guard_bb
);
942 cond
= force_gimple_operand_1 (cond
, &gimplify_stmt_list
, is_gimple_condexpr
,
944 if (gimplify_stmt_list
)
945 gimple_seq_add_seq (&cond_expr_stmt_list
, gimplify_stmt_list
);
946 cond_stmt
= gimple_build_cond_from_tree (cond
, NULL_TREE
, NULL_TREE
);
947 if (cond_expr_stmt_list
)
948 gsi_insert_seq_after (&gsi
, cond_expr_stmt_list
, GSI_NEW_STMT
);
950 gsi
= gsi_last_bb (guard_bb
);
951 gsi_insert_after (&gsi
, cond_stmt
, GSI_NEW_STMT
);
953 /* Add new edge to connect guard block to the merge/loop-exit block. */
954 new_e
= make_edge (guard_bb
, exit_bb
, EDGE_TRUE_VALUE
);
956 new_e
->count
= guard_bb
->count
;
957 new_e
->probability
= probability
;
958 new_e
->count
= apply_probability (enter_e
->count
, probability
);
959 enter_e
->count
-= new_e
->count
;
960 enter_e
->probability
= inverse_probability (probability
);
961 set_immediate_dominator (CDI_DOMINATORS
, exit_bb
, dom_bb
);
966 /* This function verifies that the following restrictions apply to LOOP:
967 (1) it consists of exactly 2 basic blocks - header, and an empty latch
968 for innermost loop and 5 basic blocks for outer-loop.
969 (2) it is single entry, single exit
970 (3) its exit condition is the last stmt in the header
971 (4) E is the entry/exit edge of LOOP.
975 slpeel_can_duplicate_loop_p (const struct loop
*loop
, const_edge e
)
977 edge exit_e
= single_exit (loop
);
978 edge entry_e
= loop_preheader_edge (loop
);
979 gcond
*orig_cond
= get_loop_exit_condition (loop
);
980 gimple_stmt_iterator loop_exit_gsi
= gsi_last_bb (exit_e
->src
);
981 unsigned int num_bb
= loop
->inner
? 5 : 2;
983 /* All loops have an outer scope; the only case loop->outer is NULL is for
984 the function itself. */
985 if (!loop_outer (loop
)
986 || loop
->num_nodes
!= num_bb
987 || !empty_block_p (loop
->latch
)
988 || !single_exit (loop
)
989 /* Verify that new loop exit condition can be trivially modified. */
990 || (!orig_cond
|| orig_cond
!= gsi_stmt (loop_exit_gsi
))
991 || (e
!= exit_e
&& e
!= entry_e
))
998 slpeel_checking_verify_cfg_after_peeling (struct loop
*first_loop
,
999 struct loop
*second_loop
)
1004 basic_block loop1_exit_bb
= single_exit (first_loop
)->dest
;
1005 basic_block loop2_entry_bb
= loop_preheader_edge (second_loop
)->src
;
1006 basic_block loop1_entry_bb
= loop_preheader_edge (first_loop
)->src
;
1008 /* A guard that controls whether the second_loop is to be executed or skipped
1009 is placed in first_loop->exit. first_loop->exit therefore has two
1010 successors - one is the preheader of second_loop, and the other is a bb
1013 gcc_assert (EDGE_COUNT (loop1_exit_bb
->succs
) == 2);
1015 /* 1. Verify that one of the successors of first_loop->exit is the preheader
1018 /* The preheader of new_loop is expected to have two predecessors:
1019 first_loop->exit and the block that precedes first_loop. */
1021 gcc_assert (EDGE_COUNT (loop2_entry_bb
->preds
) == 2
1022 && ((EDGE_PRED (loop2_entry_bb
, 0)->src
== loop1_exit_bb
1023 && EDGE_PRED (loop2_entry_bb
, 1)->src
== loop1_entry_bb
)
1024 || (EDGE_PRED (loop2_entry_bb
, 1)->src
== loop1_exit_bb
1025 && EDGE_PRED (loop2_entry_bb
, 0)->src
== loop1_entry_bb
)));
1027 /* Verify that the other successor of first_loop->exit is after the
1032 /* If the run time cost model check determines that vectorization is
1033 not profitable and hence scalar loop should be generated then set
1034 FIRST_NITERS to prologue peeled iterations. This will allow all the
1035 iterations to be executed in the prologue peeled scalar loop. */
1038 set_prologue_iterations (basic_block bb_before_first_loop
,
1045 basic_block cond_bb
, then_bb
;
1046 tree var
, prologue_after_cost_adjust_name
;
1047 gimple_stmt_iterator gsi
;
1049 edge e_true
, e_false
, e_fallthru
;
1051 gimple_seq stmts
= NULL
;
1052 tree cost_pre_condition
= NULL_TREE
;
1053 tree scalar_loop_iters
=
1054 unshare_expr (LOOP_VINFO_NITERS_UNCHANGED (loop_vec_info_for_loop (loop
)));
1056 e
= single_pred_edge (bb_before_first_loop
);
1057 cond_bb
= split_edge (e
);
1059 e
= single_pred_edge (bb_before_first_loop
);
1060 then_bb
= split_edge (e
);
1061 set_immediate_dominator (CDI_DOMINATORS
, then_bb
, cond_bb
);
1063 e_false
= make_single_succ_edge (cond_bb
, bb_before_first_loop
,
1065 set_immediate_dominator (CDI_DOMINATORS
, bb_before_first_loop
, cond_bb
);
1067 e_true
= EDGE_PRED (then_bb
, 0);
1068 e_true
->flags
&= ~EDGE_FALLTHRU
;
1069 e_true
->flags
|= EDGE_TRUE_VALUE
;
1071 e_true
->probability
= probability
;
1072 e_false
->probability
= inverse_probability (probability
);
1073 e_true
->count
= apply_probability (cond_bb
->count
, probability
);
1074 e_false
->count
= cond_bb
->count
- e_true
->count
;
1075 then_bb
->frequency
= EDGE_FREQUENCY (e_true
);
1076 then_bb
->count
= e_true
->count
;
1078 e_fallthru
= EDGE_SUCC (then_bb
, 0);
1079 e_fallthru
->count
= then_bb
->count
;
1081 gsi
= gsi_last_bb (cond_bb
);
1082 cost_pre_condition
=
1083 fold_build2 (LE_EXPR
, boolean_type_node
, scalar_loop_iters
,
1084 build_int_cst (TREE_TYPE (scalar_loop_iters
), th
));
1085 cost_pre_condition
=
1086 force_gimple_operand_gsi_1 (&gsi
, cost_pre_condition
, is_gimple_condexpr
,
1087 NULL_TREE
, false, GSI_CONTINUE_LINKING
);
1088 cond_stmt
= gimple_build_cond_from_tree (cost_pre_condition
,
1089 NULL_TREE
, NULL_TREE
);
1090 gsi_insert_after (&gsi
, cond_stmt
, GSI_NEW_STMT
);
1092 var
= create_tmp_var (TREE_TYPE (scalar_loop_iters
),
1093 "prologue_after_cost_adjust");
1094 prologue_after_cost_adjust_name
=
1095 force_gimple_operand (scalar_loop_iters
, &stmts
, false, var
);
1097 gsi
= gsi_last_bb (then_bb
);
1099 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
1101 newphi
= create_phi_node (var
, bb_before_first_loop
);
1102 add_phi_arg (newphi
, prologue_after_cost_adjust_name
, e_fallthru
,
1104 add_phi_arg (newphi
, *first_niters
, e_false
, UNKNOWN_LOCATION
);
1106 *first_niters
= PHI_RESULT (newphi
);
1109 /* Function slpeel_tree_peel_loop_to_edge.
1111 Peel the first (last) iterations of LOOP into a new prolog (epilog) loop
1112 that is placed on the entry (exit) edge E of LOOP. After this transformation
1113 we have two loops one after the other - first-loop iterates FIRST_NITERS
1114 times, and second-loop iterates the remainder NITERS - FIRST_NITERS times.
1115 If the cost model indicates that it is profitable to emit a scalar
1116 loop instead of the vector one, then the prolog (epilog) loop will iterate
1117 for the entire unchanged scalar iterations of the loop.
1120 - LOOP: the loop to be peeled.
1121 - SCALAR_LOOP: if non-NULL, the alternate loop from which basic blocks
1123 - E: the exit or entry edge of LOOP.
1124 If it is the entry edge, we peel the first iterations of LOOP. In this
1125 case first-loop is LOOP, and second-loop is the newly created loop.
1126 If it is the exit edge, we peel the last iterations of LOOP. In this
1127 case, first-loop is the newly created loop, and second-loop is LOOP.
1128 - NITERS: the number of iterations that LOOP iterates.
1129 - FIRST_NITERS: the number of iterations that the first-loop should iterate.
1130 - UPDATE_FIRST_LOOP_COUNT: specified whether this function is responsible
1131 for updating the loop bound of the first-loop to FIRST_NITERS. If it
1132 is false, the caller of this function may want to take care of this
1133 (this can be useful if we don't want new stmts added to first-loop).
1134 - TH: cost model profitability threshold of iterations for vectorization.
1135 - CHECK_PROFITABILITY: specify whether cost model check has not occurred
1136 during versioning and hence needs to occur during
1137 prologue generation or whether cost model check
1138 has not occurred during prologue generation and hence
1139 needs to occur during epilogue generation.
1140 - BOUND1 is the upper bound on number of iterations of the first loop (if known)
1141 - BOUND2 is the upper bound on number of iterations of the second loop (if known)
1145 The function returns a pointer to the new loop-copy, or NULL if it failed
1146 to perform the transformation.
1148 The function generates two if-then-else guards: one before the first loop,
1149 and the other before the second loop:
1151 if (FIRST_NITERS == 0) then skip the first loop,
1152 and go directly to the second loop.
1153 The second guard is:
1154 if (FIRST_NITERS == NITERS) then skip the second loop.
1156 If the optional COND_EXPR and COND_EXPR_STMT_LIST arguments are given
1157 then the generated condition is combined with COND_EXPR and the
1158 statements in COND_EXPR_STMT_LIST are emitted together with it.
1160 FORNOW only simple loops are supported (see slpeel_can_duplicate_loop_p).
1161 FORNOW the resulting code will not be in loop-closed-ssa form.
1164 static struct loop
*
1165 slpeel_tree_peel_loop_to_edge (struct loop
*loop
, struct loop
*scalar_loop
,
1166 edge e
, tree
*first_niters
,
1167 tree niters
, bool update_first_loop_count
,
1168 unsigned int th
, bool check_profitability
,
1169 tree cond_expr
, gimple_seq cond_expr_stmt_list
,
1170 int bound1
, int bound2
)
1172 struct loop
*new_loop
= NULL
, *first_loop
, *second_loop
;
1174 tree pre_condition
= NULL_TREE
;
1175 basic_block bb_before_second_loop
, bb_after_second_loop
;
1176 basic_block bb_before_first_loop
;
1177 basic_block bb_between_loops
;
1178 basic_block new_exit_bb
;
1180 edge exit_e
= single_exit (loop
);
1181 source_location loop_loc
;
1182 /* There are many aspects to how likely the first loop is going to be executed.
1183 Without histogram we can't really do good job. Simply set it to
1184 2/3, so the first loop is not reordered to the end of function and
1185 the hot path through stays short. */
1186 int first_guard_probability
= 2 * REG_BR_PROB_BASE
/ 3;
1187 int second_guard_probability
= 2 * REG_BR_PROB_BASE
/ 3;
1188 int probability_of_second_loop
;
1190 if (!slpeel_can_duplicate_loop_p (loop
, e
))
1193 /* We might have a queued need to update virtual SSA form. As we
1194 delete the update SSA machinery below after doing a regular
1195 incremental SSA update during loop copying make sure we don't
1197 ??? Needing to update virtual SSA form by renaming is unfortunate
1198 but not all of the vectorizer code inserting new loads / stores
1199 properly assigns virtual operands to those statements. */
1200 update_ssa (TODO_update_ssa_only_virtuals
);
1202 /* If the loop has a virtual PHI, but exit bb doesn't, create a virtual PHI
1203 in the exit bb and rename all the uses after the loop. This simplifies
1204 the *guard[12] routines, which assume loop closed SSA form for all PHIs
1205 (but normally loop closed SSA form doesn't require virtual PHIs to be
1206 in the same form). Doing this early simplifies the checking what
1207 uses should be renamed. */
1208 for (gsi
= gsi_start_phis (loop
->header
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1209 if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi
))))
1211 gphi
*phi
= gsi
.phi ();
1212 for (gsi
= gsi_start_phis (exit_e
->dest
);
1213 !gsi_end_p (gsi
); gsi_next (&gsi
))
1214 if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi
))))
1216 if (gsi_end_p (gsi
))
1218 tree new_vop
= copy_ssa_name (PHI_RESULT (phi
));
1219 gphi
*new_phi
= create_phi_node (new_vop
, exit_e
->dest
);
1220 tree vop
= PHI_ARG_DEF_FROM_EDGE (phi
, EDGE_SUCC (loop
->latch
, 0));
1221 imm_use_iterator imm_iter
;
1223 use_operand_p use_p
;
1225 add_phi_arg (new_phi
, vop
, exit_e
, UNKNOWN_LOCATION
);
1226 gimple_phi_set_result (new_phi
, new_vop
);
1227 FOR_EACH_IMM_USE_STMT (stmt
, imm_iter
, vop
)
1229 && !flow_bb_inside_loop_p (loop
, gimple_bb (stmt
)))
1230 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
1231 SET_USE (use_p
, new_vop
);
1236 /* 1. Generate a copy of LOOP and put it on E (E is the entry/exit of LOOP).
1237 Resulting CFG would be:
1250 if (!(new_loop
= slpeel_tree_duplicate_loop_to_edge_cfg (loop
, scalar_loop
,
1253 loop_loc
= find_loop_location (loop
);
1254 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loop_loc
,
1255 "tree_duplicate_loop_to_edge_cfg failed.\n");
1259 if (MAY_HAVE_DEBUG_STMTS
)
1261 gcc_assert (!adjust_vec
.exists ());
1262 adjust_vec
.create (32);
1267 /* NEW_LOOP was placed after LOOP. */
1269 second_loop
= new_loop
;
1273 /* NEW_LOOP was placed before LOOP. */
1274 first_loop
= new_loop
;
1278 /* 2. Add the guard code in one of the following ways:
1280 2.a Add the guard that controls whether the first loop is executed.
1281 This occurs when this function is invoked for prologue or epilogue
1282 generation and when the cost model check can be done at compile time.
1284 Resulting CFG would be:
1286 bb_before_first_loop:
1287 if (FIRST_NITERS == 0) GOTO bb_before_second_loop
1294 bb_before_second_loop:
1302 2.b Add the cost model check that allows the prologue
1303 to iterate for the entire unchanged scalar
1304 iterations of the loop in the event that the cost
1305 model indicates that the scalar loop is more
1306 profitable than the vector one. This occurs when
1307 this function is invoked for prologue generation
1308 and the cost model check needs to be done at run
1311 Resulting CFG after prologue peeling would be:
1313 if (scalar_loop_iterations <= th)
1314 FIRST_NITERS = scalar_loop_iterations
1316 bb_before_first_loop:
1317 if (FIRST_NITERS == 0) GOTO bb_before_second_loop
1324 bb_before_second_loop:
1332 2.c Add the cost model check that allows the epilogue
1333 to iterate for the entire unchanged scalar
1334 iterations of the loop in the event that the cost
1335 model indicates that the scalar loop is more
1336 profitable than the vector one. This occurs when
1337 this function is invoked for epilogue generation
1338 and the cost model check needs to be done at run
1339 time. This check is combined with any pre-existing
1340 check in COND_EXPR to avoid versioning.
1342 Resulting CFG after prologue peeling would be:
1344 bb_before_first_loop:
1345 if ((scalar_loop_iterations <= th)
1347 FIRST_NITERS == 0) GOTO bb_before_second_loop
1354 bb_before_second_loop:
1363 bb_before_first_loop
= split_edge (loop_preheader_edge (first_loop
));
1364 /* Loop copying insterted a forwarder block for us here. */
1365 bb_before_second_loop
= single_exit (first_loop
)->dest
;
1367 probability_of_second_loop
= (inverse_probability (first_guard_probability
)
1368 + combine_probabilities (second_guard_probability
,
1369 first_guard_probability
));
1370 /* Theoretically preheader edge of first loop and exit edge should have
1371 same frequencies. Loop exit probablities are however easy to get wrong.
1372 It is safer to copy value from original loop entry. */
1373 bb_before_second_loop
->frequency
1374 = combine_probabilities (bb_before_first_loop
->frequency
,
1375 probability_of_second_loop
);
1376 bb_before_second_loop
->count
1377 = apply_probability (bb_before_first_loop
->count
,
1378 probability_of_second_loop
);
1379 single_succ_edge (bb_before_second_loop
)->count
1380 = bb_before_second_loop
->count
;
1382 /* Epilogue peeling. */
1383 if (!update_first_loop_count
)
1385 loop_vec_info loop_vinfo
= loop_vec_info_for_loop (loop
);
1386 tree scalar_loop_iters
= LOOP_VINFO_NITERSM1 (loop_vinfo
);
1387 unsigned limit
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) - 1;
1388 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
1390 if (check_profitability
1394 fold_build2 (LT_EXPR
, boolean_type_node
, scalar_loop_iters
,
1395 build_int_cst (TREE_TYPE (scalar_loop_iters
), limit
));
1399 fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
,
1401 fold_build1 (TRUTH_NOT_EXPR
, boolean_type_node
,
1406 /* Prologue peeling. */
1409 if (check_profitability
)
1410 set_prologue_iterations (bb_before_first_loop
, first_niters
,
1411 loop
, th
, first_guard_probability
);
1414 fold_build2 (LE_EXPR
, boolean_type_node
, *first_niters
,
1415 build_int_cst (TREE_TYPE (*first_niters
), 0));
1418 skip_e
= slpeel_add_loop_guard (bb_before_first_loop
, pre_condition
,
1419 cond_expr_stmt_list
,
1420 bb_before_second_loop
, bb_before_first_loop
,
1421 inverse_probability (first_guard_probability
));
1422 scale_loop_profile (first_loop
, first_guard_probability
,
1423 check_profitability
&& (int)th
> bound1
? th
: bound1
);
1424 slpeel_update_phi_nodes_for_guard1 (skip_e
, first_loop
,
1425 first_loop
== new_loop
,
1429 /* 3. Add the guard that controls whether the second loop is executed.
1430 Resulting CFG would be:
1432 bb_before_first_loop:
1433 if (FIRST_NITERS == 0) GOTO bb_before_second_loop (skip first loop)
1441 if (FIRST_NITERS == NITERS) GOTO bb_after_second_loop (skip second loop)
1442 GOTO bb_before_second_loop
1444 bb_before_second_loop:
1450 bb_after_second_loop:
1455 bb_between_loops
= new_exit_bb
;
1456 bb_after_second_loop
= split_edge (single_exit (second_loop
));
1459 fold_build2 (EQ_EXPR
, boolean_type_node
, *first_niters
, niters
);
1460 skip_e
= slpeel_add_loop_guard (bb_between_loops
, pre_condition
, NULL
,
1461 bb_after_second_loop
, bb_before_first_loop
,
1462 inverse_probability (second_guard_probability
));
1463 scale_loop_profile (second_loop
, probability_of_second_loop
, bound2
);
1464 slpeel_update_phi_nodes_for_guard2 (skip_e
, second_loop
,
1465 second_loop
== new_loop
, &new_exit_bb
);
1467 /* 4. Make first-loop iterate FIRST_NITERS times, if requested.
1469 if (update_first_loop_count
)
1470 slpeel_make_loop_iterate_ntimes (first_loop
, *first_niters
);
1472 delete_update_ssa ();
1474 adjust_vec_debug_stmts ();
1479 /* Function vect_get_loop_location.
1481 Extract the location of the loop in the source code.
1482 If the loop is not well formed for vectorization, an estimated
1483 location is calculated.
1484 Return the loop location if succeed and NULL if not. */
1487 find_loop_location (struct loop
*loop
)
1489 gimple
*stmt
= NULL
;
1491 gimple_stmt_iterator si
;
1494 return UNKNOWN_LOCATION
;
1496 stmt
= get_loop_exit_condition (loop
);
1499 && LOCATION_LOCUS (gimple_location (stmt
)) > BUILTINS_LOCATION
)
1500 return gimple_location (stmt
);
1502 /* If we got here the loop is probably not "well formed",
1503 try to estimate the loop location */
1506 return UNKNOWN_LOCATION
;
1510 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1512 stmt
= gsi_stmt (si
);
1513 if (LOCATION_LOCUS (gimple_location (stmt
)) > BUILTINS_LOCATION
)
1514 return gimple_location (stmt
);
1517 return UNKNOWN_LOCATION
;
1521 /* Function vect_can_advance_ivs_p
1523 In case the number of iterations that LOOP iterates is unknown at compile
1524 time, an epilog loop will be generated, and the loop induction variables
1525 (IVs) will be "advanced" to the value they are supposed to take just before
1526 the epilog loop. Here we check that the access function of the loop IVs
1527 and the expression that represents the loop bound are simple enough.
1528 These restrictions will be relaxed in the future. */
1531 vect_can_advance_ivs_p (loop_vec_info loop_vinfo
)
1533 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1534 basic_block bb
= loop
->header
;
1538 /* Analyze phi functions of the loop header. */
1540 if (dump_enabled_p ())
1541 dump_printf_loc (MSG_NOTE
, vect_location
, "vect_can_advance_ivs_p:\n");
1542 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1544 tree evolution_part
;
1547 if (dump_enabled_p ())
1549 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
1550 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1551 dump_printf (MSG_NOTE
, "\n");
1554 /* Skip virtual phi's. The data dependences that are associated with
1555 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
1557 if (virtual_operand_p (PHI_RESULT (phi
)))
1559 if (dump_enabled_p ())
1560 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1561 "virtual phi. skip.\n");
1565 /* Skip reduction phis. */
1567 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (phi
)) == vect_reduction_def
)
1569 if (dump_enabled_p ())
1570 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1571 "reduc phi. skip.\n");
1575 /* Analyze the evolution function. */
1578 = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (vinfo_for_stmt (phi
));
1579 if (evolution_part
== NULL_TREE
)
1581 if (dump_enabled_p ())
1582 dump_printf (MSG_MISSED_OPTIMIZATION
,
1583 "No access function or evolution.\n");
1587 /* FORNOW: We do not transform initial conditions of IVs
1588 which evolution functions are a polynomial of degree >= 2. */
1590 if (tree_is_chrec (evolution_part
))
1598 /* Function vect_update_ivs_after_vectorizer.
1600 "Advance" the induction variables of LOOP to the value they should take
1601 after the execution of LOOP. This is currently necessary because the
1602 vectorizer does not handle induction variables that are used after the
1603 loop. Such a situation occurs when the last iterations of LOOP are
1605 1. We introduced new uses after LOOP for IVs that were not originally used
1606 after LOOP: the IVs of LOOP are now used by an epilog loop.
1607 2. LOOP is going to be vectorized; this means that it will iterate N/VF
1608 times, whereas the loop IVs should be bumped N times.
1611 - LOOP - a loop that is going to be vectorized. The last few iterations
1612 of LOOP were peeled.
1613 - NITERS - the number of iterations that LOOP executes (before it is
1614 vectorized). i.e, the number of times the ivs should be bumped.
1615 - UPDATE_E - a successor edge of LOOP->exit that is on the (only) path
1616 coming out from LOOP on which there are uses of the LOOP ivs
1617 (this is the path from LOOP->exit to epilog_loop->preheader).
1619 The new definitions of the ivs are placed in LOOP->exit.
1620 The phi args associated with the edge UPDATE_E in the bb
1621 UPDATE_E->dest are updated accordingly.
1623 Assumption 1: Like the rest of the vectorizer, this function assumes
1624 a single loop exit that has a single predecessor.
1626 Assumption 2: The phi nodes in the LOOP header and in update_bb are
1627 organized in the same order.
1629 Assumption 3: The access function of the ivs is simple enough (see
1630 vect_can_advance_ivs_p). This assumption will be relaxed in the future.
1632 Assumption 4: Exactly one of the successors of LOOP exit-bb is on a path
1633 coming out of LOOP on which the ivs of LOOP are used (this is the path
1634 that leads to the epilog loop; other paths skip the epilog loop). This
1635 path starts with the edge UPDATE_E, and its destination (denoted update_bb)
1636 needs to have its phis updated.
1640 vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo
, tree niters
,
1643 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1644 basic_block exit_bb
= single_exit (loop
)->dest
;
1646 gphi_iterator gsi
, gsi1
;
1647 basic_block update_bb
= update_e
->dest
;
1649 gcc_checking_assert (vect_can_advance_ivs_p (loop_vinfo
));
1651 /* Make sure there exists a single-predecessor exit bb: */
1652 gcc_assert (single_pred_p (exit_bb
));
1654 for (gsi
= gsi_start_phis (loop
->header
), gsi1
= gsi_start_phis (update_bb
);
1655 !gsi_end_p (gsi
) && !gsi_end_p (gsi1
);
1656 gsi_next (&gsi
), gsi_next (&gsi1
))
1659 tree step_expr
, off
;
1661 tree var
, ni
, ni_name
;
1662 gimple_stmt_iterator last_gsi
;
1663 stmt_vec_info stmt_info
;
1667 if (dump_enabled_p ())
1669 dump_printf_loc (MSG_NOTE
, vect_location
,
1670 "vect_update_ivs_after_vectorizer: phi: ");
1671 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1672 dump_printf (MSG_NOTE
, "\n");
1675 /* Skip virtual phi's. */
1676 if (virtual_operand_p (PHI_RESULT (phi
)))
1678 if (dump_enabled_p ())
1679 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1680 "virtual phi. skip.\n");
1684 /* Skip reduction phis. */
1685 stmt_info
= vinfo_for_stmt (phi
);
1686 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
1688 if (dump_enabled_p ())
1689 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1690 "reduc phi. skip.\n");
1694 type
= TREE_TYPE (gimple_phi_result (phi
));
1695 step_expr
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info
);
1696 step_expr
= unshare_expr (step_expr
);
1698 /* FORNOW: We do not support IVs whose evolution function is a polynomial
1699 of degree >= 2 or exponential. */
1700 gcc_assert (!tree_is_chrec (step_expr
));
1702 init_expr
= PHI_ARG_DEF_FROM_EDGE (phi
, loop_preheader_edge (loop
));
1704 off
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
1705 fold_convert (TREE_TYPE (step_expr
), niters
),
1707 if (POINTER_TYPE_P (type
))
1708 ni
= fold_build_pointer_plus (init_expr
, off
);
1710 ni
= fold_build2 (PLUS_EXPR
, type
,
1711 init_expr
, fold_convert (type
, off
));
1713 var
= create_tmp_var (type
, "tmp");
1715 last_gsi
= gsi_last_bb (exit_bb
);
1716 ni_name
= force_gimple_operand_gsi (&last_gsi
, ni
, false, var
,
1717 true, GSI_SAME_STMT
);
1719 /* Fix phi expressions in the successor bb. */
1720 adjust_phi_and_debug_stmts (phi1
, update_e
, ni_name
);
1724 /* Function vect_do_peeling_for_loop_bound
1726 Peel the last iterations of the loop represented by LOOP_VINFO.
1727 The peeled iterations form a new epilog loop. Given that the loop now
1728 iterates NITERS times, the new epilog loop iterates
1729 NITERS % VECTORIZATION_FACTOR times.
1731 The original loop will later be made to iterate
1732 NITERS / VECTORIZATION_FACTOR times (this value is placed into RATIO).
1734 COND_EXPR and COND_EXPR_STMT_LIST are combined with a new generated
1738 vect_do_peeling_for_loop_bound (loop_vec_info loop_vinfo
,
1739 tree ni_name
, tree ratio_mult_vf_name
,
1740 unsigned int th
, bool check_profitability
)
1742 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1743 struct loop
*scalar_loop
= LOOP_VINFO_SCALAR_LOOP (loop_vinfo
);
1744 struct loop
*new_loop
;
1746 basic_block preheader
;
1749 tree cond_expr
= NULL_TREE
;
1750 gimple_seq cond_expr_stmt_list
= NULL
;
1752 if (dump_enabled_p ())
1753 dump_printf_loc (MSG_NOTE
, vect_location
,
1754 "=== vect_do_peeling_for_loop_bound ===\n");
1756 initialize_original_copy_tables ();
1758 loop_num
= loop
->num
;
1761 = slpeel_tree_peel_loop_to_edge (loop
, scalar_loop
, single_exit (loop
),
1762 &ratio_mult_vf_name
, ni_name
, false,
1763 th
, check_profitability
,
1764 cond_expr
, cond_expr_stmt_list
,
1765 0, LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
1766 gcc_assert (new_loop
);
1767 gcc_assert (loop_num
== loop
->num
);
1768 slpeel_checking_verify_cfg_after_peeling (loop
, new_loop
);
1770 /* A guard that controls whether the new_loop is to be executed or skipped
1771 is placed in LOOP->exit. LOOP->exit therefore has two successors - one
1772 is the preheader of NEW_LOOP, where the IVs from LOOP are used. The other
1773 is a bb after NEW_LOOP, where these IVs are not used. Find the edge that
1774 is on the path where the LOOP IVs are used and need to be updated. */
1776 preheader
= loop_preheader_edge (new_loop
)->src
;
1777 if (EDGE_PRED (preheader
, 0)->src
== single_exit (loop
)->dest
)
1778 update_e
= EDGE_PRED (preheader
, 0);
1780 update_e
= EDGE_PRED (preheader
, 1);
1782 /* Update IVs of original loop as if they were advanced
1783 by ratio_mult_vf_name steps. */
1784 vect_update_ivs_after_vectorizer (loop_vinfo
, ratio_mult_vf_name
, update_e
);
1786 /* For vectorization factor N, we need to copy last N-1 values in epilogue
1787 and this means N-2 loopback edge executions.
1789 PEELING_FOR_GAPS works by subtracting last iteration and thus the epilogue
1790 will execute at least LOOP_VINFO_VECT_FACTOR times. */
1791 max_iter
= (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
1792 ? LOOP_VINFO_VECT_FACTOR (loop_vinfo
) * 2
1793 : LOOP_VINFO_VECT_FACTOR (loop_vinfo
)) - 2;
1794 if (check_profitability
)
1795 max_iter
= MAX (max_iter
, (int) th
- 1);
1796 record_niter_bound (new_loop
, max_iter
, false, true);
1797 dump_printf (MSG_NOTE
,
1798 "Setting upper bound of nb iterations for epilogue "
1799 "loop to %d\n", max_iter
);
1801 /* After peeling we have to reset scalar evolution analyzer. */
1804 free_original_copy_tables ();
1808 /* Function vect_gen_niters_for_prolog_loop
1810 Set the number of iterations for the loop represented by LOOP_VINFO
1811 to the minimum between LOOP_NITERS (the original iteration count of the loop)
1812 and the misalignment of DR - the data reference recorded in
1813 LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO). As a result, after the execution of
1814 this loop, the data reference DR will refer to an aligned location.
1816 The following computation is generated:
1818 If the misalignment of DR is known at compile time:
1819 addr_mis = int mis = DR_MISALIGNMENT (dr);
1820 Else, compute address misalignment in bytes:
1821 addr_mis = addr & (vectype_align - 1)
1823 prolog_niters = min (LOOP_NITERS, ((VF - addr_mis/elem_size)&(VF-1))/step)
1825 (elem_size = element type size; an element is the scalar element whose type
1826 is the inner type of the vectype)
1828 When the step of the data-ref in the loop is not 1 (as in interleaved data
1829 and SLP), the number of iterations of the prolog must be divided by the step
1830 (which is equal to the size of interleaved group).
1832 The above formulas assume that VF == number of elements in the vector. This
1833 may not hold when there are multiple-types in the loop.
1834 In this case, for some data-references in the loop the VF does not represent
1835 the number of elements that fit in the vector. Therefore, instead of VF we
1836 use TYPE_VECTOR_SUBPARTS. */
1839 vect_gen_niters_for_prolog_loop (loop_vec_info loop_vinfo
, tree loop_niters
, int *bound
)
1841 struct data_reference
*dr
= LOOP_VINFO_UNALIGNED_DR (loop_vinfo
);
1842 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1845 tree iters
, iters_name
;
1848 gimple
*dr_stmt
= DR_STMT (dr
);
1849 stmt_vec_info stmt_info
= vinfo_for_stmt (dr_stmt
);
1850 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1851 int vectype_align
= TYPE_ALIGN (vectype
) / BITS_PER_UNIT
;
1852 tree niters_type
= TREE_TYPE (loop_niters
);
1853 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1855 pe
= loop_preheader_edge (loop
);
1857 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) > 0)
1859 int npeel
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
1861 if (dump_enabled_p ())
1862 dump_printf_loc (MSG_NOTE
, vect_location
,
1863 "known peeling = %d.\n", npeel
);
1865 iters
= build_int_cst (niters_type
, npeel
);
1866 *bound
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
1870 gimple_seq new_stmts
= NULL
;
1871 bool negative
= tree_int_cst_compare (DR_STEP (dr
), size_zero_node
) < 0;
1872 tree offset
= negative
1873 ? size_int (-TYPE_VECTOR_SUBPARTS (vectype
) + 1) : size_zero_node
;
1874 tree start_addr
= vect_create_addr_base_for_vector_ref (dr_stmt
,
1875 &new_stmts
, offset
, loop
);
1876 tree type
= unsigned_type_for (TREE_TYPE (start_addr
));
1877 tree vectype_align_minus_1
= build_int_cst (type
, vectype_align
- 1);
1878 HOST_WIDE_INT elem_size
=
1879 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype
)));
1880 tree elem_size_log
= build_int_cst (type
, exact_log2 (elem_size
));
1881 tree nelements_minus_1
= build_int_cst (type
, nelements
- 1);
1882 tree nelements_tree
= build_int_cst (type
, nelements
);
1886 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, new_stmts
);
1887 gcc_assert (!new_bb
);
1889 /* Create: byte_misalign = addr & (vectype_align - 1) */
1891 fold_build2 (BIT_AND_EXPR
, type
, fold_convert (type
, start_addr
),
1892 vectype_align_minus_1
);
1894 /* Create: elem_misalign = byte_misalign / element_size */
1896 fold_build2 (RSHIFT_EXPR
, type
, byte_misalign
, elem_size_log
);
1898 /* Create: (niters_type) (nelements - elem_misalign)&(nelements - 1) */
1900 iters
= fold_build2 (MINUS_EXPR
, type
, elem_misalign
, nelements_tree
);
1902 iters
= fold_build2 (MINUS_EXPR
, type
, nelements_tree
, elem_misalign
);
1903 iters
= fold_build2 (BIT_AND_EXPR
, type
, iters
, nelements_minus_1
);
1904 iters
= fold_convert (niters_type
, iters
);
1908 /* Create: prolog_loop_niters = min (iters, loop_niters) */
1909 /* If the loop bound is known at compile time we already verified that it is
1910 greater than vf; since the misalignment ('iters') is at most vf, there's
1911 no need to generate the MIN_EXPR in this case. */
1912 if (TREE_CODE (loop_niters
) != INTEGER_CST
)
1913 iters
= fold_build2 (MIN_EXPR
, niters_type
, iters
, loop_niters
);
1915 if (dump_enabled_p ())
1917 dump_printf_loc (MSG_NOTE
, vect_location
,
1918 "niters for prolog loop: ");
1919 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, iters
);
1920 dump_printf (MSG_NOTE
, "\n");
1923 var
= create_tmp_var (niters_type
, "prolog_loop_niters");
1925 iters_name
= force_gimple_operand (iters
, &stmts
, false, var
);
1927 /* Insert stmt on loop preheader edge. */
1930 basic_block new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
1931 gcc_assert (!new_bb
);
1938 /* Function vect_update_init_of_dr
1940 NITERS iterations were peeled from LOOP. DR represents a data reference
1941 in LOOP. This function updates the information recorded in DR to
1942 account for the fact that the first NITERS iterations had already been
1943 executed. Specifically, it updates the OFFSET field of DR. */
1946 vect_update_init_of_dr (struct data_reference
*dr
, tree niters
)
1948 tree offset
= DR_OFFSET (dr
);
1950 niters
= fold_build2 (MULT_EXPR
, sizetype
,
1951 fold_convert (sizetype
, niters
),
1952 fold_convert (sizetype
, DR_STEP (dr
)));
1953 offset
= fold_build2 (PLUS_EXPR
, sizetype
,
1954 fold_convert (sizetype
, offset
), niters
);
1955 DR_OFFSET (dr
) = offset
;
1959 /* Function vect_update_inits_of_drs
1961 NITERS iterations were peeled from the loop represented by LOOP_VINFO.
1962 This function updates the information recorded for the data references in
1963 the loop to account for the fact that the first NITERS iterations had
1964 already been executed. Specifically, it updates the initial_condition of
1965 the access_function of all the data_references in the loop. */
1968 vect_update_inits_of_drs (loop_vec_info loop_vinfo
, tree niters
)
1971 vec
<data_reference_p
> datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1972 struct data_reference
*dr
;
1974 if (dump_enabled_p ())
1975 dump_printf_loc (MSG_NOTE
, vect_location
,
1976 "=== vect_update_inits_of_dr ===\n");
1978 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1979 vect_update_init_of_dr (dr
, niters
);
1983 /* Function vect_do_peeling_for_alignment
1985 Peel the first 'niters' iterations of the loop represented by LOOP_VINFO.
1986 'niters' is set to the misalignment of one of the data references in the
1987 loop, thereby forcing it to refer to an aligned location at the beginning
1988 of the execution of this loop. The data reference for which we are
1989 peeling is recorded in LOOP_VINFO_UNALIGNED_DR. */
1992 vect_do_peeling_for_alignment (loop_vec_info loop_vinfo
, tree ni_name
,
1993 unsigned int th
, bool check_profitability
)
1995 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1996 struct loop
*scalar_loop
= LOOP_VINFO_SCALAR_LOOP (loop_vinfo
);
1997 tree niters_of_prolog_loop
;
1998 tree wide_prolog_niters
;
1999 struct loop
*new_loop
;
2003 if (dump_enabled_p ())
2004 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2005 "loop peeled for vectorization to enhance"
2008 initialize_original_copy_tables ();
2010 gimple_seq stmts
= NULL
;
2011 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
2012 niters_of_prolog_loop
= vect_gen_niters_for_prolog_loop (loop_vinfo
,
2016 /* Peel the prolog loop and iterate it niters_of_prolog_loop. */
2018 slpeel_tree_peel_loop_to_edge (loop
, scalar_loop
,
2019 loop_preheader_edge (loop
),
2020 &niters_of_prolog_loop
, ni_name
, true,
2021 th
, check_profitability
, NULL_TREE
, NULL
,
2024 gcc_assert (new_loop
);
2025 slpeel_checking_verify_cfg_after_peeling (new_loop
, loop
);
2026 /* For vectorization factor N, we need to copy at most N-1 values
2027 for alignment and this means N-2 loopback edge executions. */
2028 max_iter
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) - 2;
2029 if (check_profitability
)
2030 max_iter
= MAX (max_iter
, (int) th
- 1);
2031 record_niter_bound (new_loop
, max_iter
, false, true);
2032 dump_printf (MSG_NOTE
,
2033 "Setting upper bound of nb iterations for prologue "
2034 "loop to %d\n", max_iter
);
2036 /* Update number of times loop executes. */
2037 LOOP_VINFO_NITERS (loop_vinfo
) = fold_build2 (MINUS_EXPR
,
2038 TREE_TYPE (ni_name
), ni_name
, niters_of_prolog_loop
);
2039 LOOP_VINFO_NITERSM1 (loop_vinfo
) = fold_build2 (MINUS_EXPR
,
2040 TREE_TYPE (ni_name
),
2041 LOOP_VINFO_NITERSM1 (loop_vinfo
), niters_of_prolog_loop
);
2043 if (types_compatible_p (sizetype
, TREE_TYPE (niters_of_prolog_loop
)))
2044 wide_prolog_niters
= niters_of_prolog_loop
;
2047 gimple_seq seq
= NULL
;
2048 edge pe
= loop_preheader_edge (loop
);
2049 tree wide_iters
= fold_convert (sizetype
, niters_of_prolog_loop
);
2050 tree var
= create_tmp_var (sizetype
, "prolog_loop_adjusted_niters");
2051 wide_prolog_niters
= force_gimple_operand (wide_iters
, &seq
, false,
2055 /* Insert stmt on loop preheader edge. */
2056 basic_block new_bb
= gsi_insert_seq_on_edge_immediate (pe
, seq
);
2057 gcc_assert (!new_bb
);
2061 /* Update the init conditions of the access functions of all data refs. */
2062 vect_update_inits_of_drs (loop_vinfo
, wide_prolog_niters
);
2064 /* After peeling we have to reset scalar evolution analyzer. */
2067 free_original_copy_tables ();
2071 /* Function vect_create_cond_for_align_checks.
2073 Create a conditional expression that represents the alignment checks for
2074 all of data references (array element references) whose alignment must be
2078 COND_EXPR - input conditional expression. New conditions will be chained
2079 with logical AND operation.
2080 LOOP_VINFO - two fields of the loop information are used.
2081 LOOP_VINFO_PTR_MASK is the mask used to check the alignment.
2082 LOOP_VINFO_MAY_MISALIGN_STMTS contains the refs to be checked.
2085 COND_EXPR_STMT_LIST - statements needed to construct the conditional
2087 The returned value is the conditional expression to be used in the if
2088 statement that controls which version of the loop gets executed at runtime.
2090 The algorithm makes two assumptions:
2091 1) The number of bytes "n" in a vector is a power of 2.
2092 2) An address "a" is aligned if a%n is zero and that this
2093 test can be done as a&(n-1) == 0. For example, for 16
2094 byte vectors the test is a&0xf == 0. */
2097 vect_create_cond_for_align_checks (loop_vec_info loop_vinfo
,
2099 gimple_seq
*cond_expr_stmt_list
)
2101 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2102 vec
<gimple
*> may_misalign_stmts
2103 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
);
2105 int mask
= LOOP_VINFO_PTR_MASK (loop_vinfo
);
2108 tree int_ptrsize_type
;
2110 tree or_tmp_name
= NULL_TREE
;
2114 tree part_cond_expr
;
2116 /* Check that mask is one less than a power of 2, i.e., mask is
2117 all zeros followed by all ones. */
2118 gcc_assert ((mask
!= 0) && ((mask
& (mask
+1)) == 0));
2120 int_ptrsize_type
= signed_type_for (ptr_type_node
);
2122 /* Create expression (mask & (dr_1 || ... || dr_n)) where dr_i is the address
2123 of the first vector of the i'th data reference. */
2125 FOR_EACH_VEC_ELT (may_misalign_stmts
, i
, ref_stmt
)
2127 gimple_seq new_stmt_list
= NULL
;
2130 tree new_or_tmp_name
;
2131 gimple
*addr_stmt
, *or_stmt
;
2132 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (ref_stmt
);
2133 tree vectype
= STMT_VINFO_VECTYPE (stmt_vinfo
);
2134 bool negative
= tree_int_cst_compare
2135 (DR_STEP (STMT_VINFO_DATA_REF (stmt_vinfo
)), size_zero_node
) < 0;
2136 tree offset
= negative
2137 ? size_int (-TYPE_VECTOR_SUBPARTS (vectype
) + 1) : size_zero_node
;
2139 /* create: addr_tmp = (int)(address_of_first_vector) */
2141 vect_create_addr_base_for_vector_ref (ref_stmt
, &new_stmt_list
,
2143 if (new_stmt_list
!= NULL
)
2144 gimple_seq_add_seq (cond_expr_stmt_list
, new_stmt_list
);
2146 sprintf (tmp_name
, "addr2int%d", i
);
2147 addr_tmp_name
= make_temp_ssa_name (int_ptrsize_type
, NULL
, tmp_name
);
2148 addr_stmt
= gimple_build_assign (addr_tmp_name
, NOP_EXPR
, addr_base
);
2149 gimple_seq_add_stmt (cond_expr_stmt_list
, addr_stmt
);
2151 /* The addresses are OR together. */
2153 if (or_tmp_name
!= NULL_TREE
)
2155 /* create: or_tmp = or_tmp | addr_tmp */
2156 sprintf (tmp_name
, "orptrs%d", i
);
2157 new_or_tmp_name
= make_temp_ssa_name (int_ptrsize_type
, NULL
, tmp_name
);
2158 or_stmt
= gimple_build_assign (new_or_tmp_name
, BIT_IOR_EXPR
,
2159 or_tmp_name
, addr_tmp_name
);
2160 gimple_seq_add_stmt (cond_expr_stmt_list
, or_stmt
);
2161 or_tmp_name
= new_or_tmp_name
;
2164 or_tmp_name
= addr_tmp_name
;
2168 mask_cst
= build_int_cst (int_ptrsize_type
, mask
);
2170 /* create: and_tmp = or_tmp & mask */
2171 and_tmp_name
= make_temp_ssa_name (int_ptrsize_type
, NULL
, "andmask");
2173 and_stmt
= gimple_build_assign (and_tmp_name
, BIT_AND_EXPR
,
2174 or_tmp_name
, mask_cst
);
2175 gimple_seq_add_stmt (cond_expr_stmt_list
, and_stmt
);
2177 /* Make and_tmp the left operand of the conditional test against zero.
2178 if and_tmp has a nonzero bit then some address is unaligned. */
2179 ptrsize_zero
= build_int_cst (int_ptrsize_type
, 0);
2180 part_cond_expr
= fold_build2 (EQ_EXPR
, boolean_type_node
,
2181 and_tmp_name
, ptrsize_zero
);
2183 *cond_expr
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
2184 *cond_expr
, part_cond_expr
);
2186 *cond_expr
= part_cond_expr
;
2189 /* Function vect_create_cond_for_alias_checks.
2191 Create a conditional expression that represents the run-time checks for
2192 overlapping of address ranges represented by a list of data references
2193 relations passed as input.
2196 COND_EXPR - input conditional expression. New conditions will be chained
2197 with logical AND operation. If it is NULL, then the function
2198 is used to return the number of alias checks.
2199 LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs
2203 COND_EXPR - conditional expression.
2205 The returned COND_EXPR is the conditional expression to be used in the if
2206 statement that controls which version of the loop gets executed at runtime.
2210 vect_create_cond_for_alias_checks (loop_vec_info loop_vinfo
, tree
* cond_expr
)
2212 vec
<dr_with_seg_len_pair_t
> comp_alias_ddrs
=
2213 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
);
2214 tree part_cond_expr
;
2216 /* Create expression
2217 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2218 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2222 ((store_ptr_n + store_segment_length_n) <= load_ptr_n)
2223 || (load_ptr_n + load_segment_length_n) <= store_ptr_n)) */
2225 if (comp_alias_ddrs
.is_empty ())
2228 for (size_t i
= 0, s
= comp_alias_ddrs
.length (); i
< s
; ++i
)
2230 const dr_with_seg_len
& dr_a
= comp_alias_ddrs
[i
].first
;
2231 const dr_with_seg_len
& dr_b
= comp_alias_ddrs
[i
].second
;
2232 tree segment_length_a
= dr_a
.seg_len
;
2233 tree segment_length_b
= dr_b
.seg_len
;
2236 = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a
.dr
), dr_a
.offset
);
2238 = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b
.dr
), dr_b
.offset
);
2240 if (dump_enabled_p ())
2242 dump_printf_loc (MSG_NOTE
, vect_location
,
2243 "create runtime check for data references ");
2244 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dr_a
.dr
));
2245 dump_printf (MSG_NOTE
, " and ");
2246 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dr_b
.dr
));
2247 dump_printf (MSG_NOTE
, "\n");
2250 tree seg_a_min
= addr_base_a
;
2251 tree seg_a_max
= fold_build_pointer_plus (addr_base_a
, segment_length_a
);
2252 /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
2253 bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
2255 if (tree_int_cst_compare (DR_STEP (dr_a
.dr
), size_zero_node
) < 0)
2257 tree unit_size
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_a
.dr
)));
2258 seg_a_min
= fold_build_pointer_plus (seg_a_max
, unit_size
);
2259 seg_a_max
= fold_build_pointer_plus (addr_base_a
, unit_size
);
2262 tree seg_b_min
= addr_base_b
;
2263 tree seg_b_max
= fold_build_pointer_plus (addr_base_b
, segment_length_b
);
2264 if (tree_int_cst_compare (DR_STEP (dr_b
.dr
), size_zero_node
) < 0)
2266 tree unit_size
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_b
.dr
)));
2267 seg_b_min
= fold_build_pointer_plus (seg_b_max
, unit_size
);
2268 seg_b_max
= fold_build_pointer_plus (addr_base_b
, unit_size
);
2272 fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
,
2273 fold_build2 (LE_EXPR
, boolean_type_node
, seg_a_max
, seg_b_min
),
2274 fold_build2 (LE_EXPR
, boolean_type_node
, seg_b_max
, seg_a_min
));
2277 *cond_expr
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
2278 *cond_expr
, part_cond_expr
);
2280 *cond_expr
= part_cond_expr
;
2283 if (dump_enabled_p ())
2284 dump_printf_loc (MSG_NOTE
, vect_location
,
2285 "created %u versioning for alias checks.\n",
2286 comp_alias_ddrs
.length ());
2290 /* Function vect_loop_versioning.
2292 If the loop has data references that may or may not be aligned or/and
2293 has data reference relations whose independence was not proven then
2294 two versions of the loop need to be generated, one which is vectorized
2295 and one which isn't. A test is then generated to control which of the
2296 loops is executed. The test checks for the alignment of all of the
2297 data references that may or may not be aligned. An additional
2298 sequence of runtime tests is generated for each pairs of DDRs whose
2299 independence was not proven. The vectorized version of loop is
2300 executed only if both alias and alignment tests are passed.
2302 The test generated to check which version of loop is executed
2303 is modified to also check for profitability as indicated by the
2304 cost model initially.
2306 The versioning precondition(s) are placed in *COND_EXPR and
2307 *COND_EXPR_STMT_LIST. */
2310 vect_loop_versioning (loop_vec_info loop_vinfo
,
2311 unsigned int th
, bool check_profitability
)
2313 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2314 struct loop
*scalar_loop
= LOOP_VINFO_SCALAR_LOOP (loop_vinfo
);
2315 basic_block condition_bb
;
2317 gimple_stmt_iterator cond_exp_gsi
;
2318 basic_block merge_bb
;
2319 basic_block new_exit_bb
;
2321 gphi
*orig_phi
, *new_phi
;
2322 tree cond_expr
= NULL_TREE
;
2323 gimple_seq cond_expr_stmt_list
= NULL
;
2325 unsigned prob
= 4 * REG_BR_PROB_BASE
/ 5;
2326 gimple_seq gimplify_stmt_list
= NULL
;
2327 tree scalar_loop_iters
= LOOP_VINFO_NITERS (loop_vinfo
);
2328 bool version_align
= LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
);
2329 bool version_alias
= LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
);
2331 if (check_profitability
)
2333 cond_expr
= fold_build2 (GT_EXPR
, boolean_type_node
, scalar_loop_iters
,
2334 build_int_cst (TREE_TYPE (scalar_loop_iters
), th
));
2335 cond_expr
= force_gimple_operand_1 (cond_expr
, &cond_expr_stmt_list
,
2336 is_gimple_condexpr
, NULL_TREE
);
2340 vect_create_cond_for_align_checks (loop_vinfo
, &cond_expr
,
2341 &cond_expr_stmt_list
);
2344 vect_create_cond_for_alias_checks (loop_vinfo
, &cond_expr
);
2346 cond_expr
= force_gimple_operand_1 (cond_expr
, &gimplify_stmt_list
,
2347 is_gimple_condexpr
, NULL_TREE
);
2348 gimple_seq_add_seq (&cond_expr_stmt_list
, gimplify_stmt_list
);
2350 initialize_original_copy_tables ();
2354 basic_block preheader
, scalar_preheader
;
2356 /* We don't want to scale SCALAR_LOOP's frequencies, we need to
2357 scale LOOP's frequencies instead. */
2358 loop_version (scalar_loop
, cond_expr
, &condition_bb
,
2359 prob
, REG_BR_PROB_BASE
, REG_BR_PROB_BASE
- prob
, true);
2360 scale_loop_frequencies (loop
, prob
, REG_BR_PROB_BASE
);
2361 /* CONDITION_BB was created above SCALAR_LOOP's preheader,
2362 while we need to move it above LOOP's preheader. */
2363 e
= loop_preheader_edge (loop
);
2364 scalar_e
= loop_preheader_edge (scalar_loop
);
2365 gcc_assert (empty_block_p (e
->src
)
2366 && single_pred_p (e
->src
));
2367 gcc_assert (empty_block_p (scalar_e
->src
)
2368 && single_pred_p (scalar_e
->src
));
2369 gcc_assert (single_pred_p (condition_bb
));
2371 scalar_preheader
= scalar_e
->src
;
2372 scalar_e
= find_edge (condition_bb
, scalar_preheader
);
2373 e
= single_pred_edge (preheader
);
2374 redirect_edge_and_branch_force (single_pred_edge (condition_bb
),
2376 redirect_edge_and_branch_force (scalar_e
, preheader
);
2377 redirect_edge_and_branch_force (e
, condition_bb
);
2378 set_immediate_dominator (CDI_DOMINATORS
, condition_bb
,
2379 single_pred (condition_bb
));
2380 set_immediate_dominator (CDI_DOMINATORS
, scalar_preheader
,
2381 single_pred (scalar_preheader
));
2382 set_immediate_dominator (CDI_DOMINATORS
, preheader
,
2386 loop_version (loop
, cond_expr
, &condition_bb
,
2387 prob
, prob
, REG_BR_PROB_BASE
- prob
, true);
2389 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOCATION
2390 && dump_enabled_p ())
2393 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2394 "loop versioned for vectorization because of "
2395 "possible aliasing\n");
2397 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2398 "loop versioned for vectorization to enhance "
2402 free_original_copy_tables ();
2404 /* Loop versioning violates an assumption we try to maintain during
2405 vectorization - that the loop exit block has a single predecessor.
2406 After versioning, the exit block of both loop versions is the same
2407 basic block (i.e. it has two predecessors). Just in order to simplify
2408 following transformations in the vectorizer, we fix this situation
2409 here by adding a new (empty) block on the exit-edge of the loop,
2410 with the proper loop-exit phis to maintain loop-closed-form.
2411 If loop versioning wasn't done from loop, but scalar_loop instead,
2412 merge_bb will have already just a single successor. */
2414 merge_bb
= single_exit (loop
)->dest
;
2415 if (scalar_loop
== NULL
|| EDGE_COUNT (merge_bb
->preds
) >= 2)
2417 gcc_assert (EDGE_COUNT (merge_bb
->preds
) >= 2);
2418 new_exit_bb
= split_edge (single_exit (loop
));
2419 new_exit_e
= single_exit (loop
);
2420 e
= EDGE_SUCC (new_exit_bb
, 0);
2422 for (gsi
= gsi_start_phis (merge_bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2425 orig_phi
= gsi
.phi ();
2426 new_res
= copy_ssa_name (PHI_RESULT (orig_phi
));
2427 new_phi
= create_phi_node (new_res
, new_exit_bb
);
2428 arg
= PHI_ARG_DEF_FROM_EDGE (orig_phi
, e
);
2429 add_phi_arg (new_phi
, arg
, new_exit_e
,
2430 gimple_phi_arg_location_from_edge (orig_phi
, e
));
2431 adjust_phi_and_debug_stmts (orig_phi
, e
, PHI_RESULT (new_phi
));
2435 /* End loop-exit-fixes after versioning. */
2437 if (cond_expr_stmt_list
)
2439 cond_exp_gsi
= gsi_last_bb (condition_bb
);
2440 gsi_insert_seq_before (&cond_exp_gsi
, cond_expr_stmt_list
,
2443 update_ssa (TODO_update_ssa
);