PR tree-optimization/66718
[official-gcc.git] / gcc / tree-vect-loop-manip.c
blob58d2c7feeff1f59da4bf84fe45f2a22987c9c453
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfganal.h"
37 #include "basic-block.h"
38 #include "gimple-pretty-print.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "gimple.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimplify-me.h"
46 #include "gimple-ssa.h"
47 #include "tree-cfg.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "tree-ssa-loop-manip.h"
53 #include "tree-into-ssa.h"
54 #include "tree-ssa.h"
55 #include "tree-pass.h"
56 #include "cfgloop.h"
57 #include "diagnostic-core.h"
58 #include "tree-scalar-evolution.h"
59 #include "tree-vectorizer.h"
60 #include "langhooks.h"
62 /*************************************************************************
63 Simple Loop Peeling Utilities
65 Utilities to support loop peeling for vectorization purposes.
66 *************************************************************************/
69 /* Renames the use *OP_P. */
71 static void
72 rename_use_op (use_operand_p op_p)
74 tree new_name;
76 if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
77 return;
79 new_name = get_current_def (USE_FROM_PTR (op_p));
81 /* Something defined outside of the loop. */
82 if (!new_name)
83 return;
85 /* An ordinary ssa name defined in the loop. */
87 SET_USE (op_p, new_name);
91 /* Renames the variables in basic block BB. */
93 static void
94 rename_variables_in_bb (basic_block bb)
96 gimple stmt;
97 use_operand_p use_p;
98 ssa_op_iter iter;
99 edge e;
100 edge_iterator ei;
101 struct loop *loop = bb->loop_father;
103 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
104 gsi_next (&gsi))
106 stmt = gsi_stmt (gsi);
107 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
108 rename_use_op (use_p);
111 FOR_EACH_EDGE (e, ei, bb->preds)
113 if (!flow_bb_inside_loop_p (loop, e->src))
114 continue;
115 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
116 gsi_next (&gsi))
117 rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (gsi.phi (), e));
122 typedef struct
124 tree from, to;
125 basic_block bb;
126 } adjust_info;
128 /* A stack of values to be adjusted in debug stmts. We have to
129 process them LIFO, so that the closest substitution applies. If we
130 processed them FIFO, without the stack, we might substitute uses
131 with a PHI DEF that would soon become non-dominant, and when we got
132 to the suitable one, it wouldn't have anything to substitute any
133 more. */
134 static vec<adjust_info, va_heap> adjust_vec;
136 /* Adjust any debug stmts that referenced AI->from values to use the
137 loop-closed AI->to, if the references are dominated by AI->bb and
138 not by the definition of AI->from. */
140 static void
141 adjust_debug_stmts_now (adjust_info *ai)
143 basic_block bbphi = ai->bb;
144 tree orig_def = ai->from;
145 tree new_def = ai->to;
146 imm_use_iterator imm_iter;
147 gimple stmt;
148 basic_block bbdef = gimple_bb (SSA_NAME_DEF_STMT (orig_def));
150 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
152 /* Adjust any debug stmts that held onto non-loop-closed
153 references. */
154 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, orig_def)
156 use_operand_p use_p;
157 basic_block bbuse;
159 if (!is_gimple_debug (stmt))
160 continue;
162 gcc_assert (gimple_debug_bind_p (stmt));
164 bbuse = gimple_bb (stmt);
166 if ((bbuse == bbphi
167 || dominated_by_p (CDI_DOMINATORS, bbuse, bbphi))
168 && !(bbuse == bbdef
169 || dominated_by_p (CDI_DOMINATORS, bbuse, bbdef)))
171 if (new_def)
172 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
173 SET_USE (use_p, new_def);
174 else
176 gimple_debug_bind_reset_value (stmt);
177 update_stmt (stmt);
183 /* Adjust debug stmts as scheduled before. */
185 static void
186 adjust_vec_debug_stmts (void)
188 if (!MAY_HAVE_DEBUG_STMTS)
189 return;
191 gcc_assert (adjust_vec.exists ());
193 while (!adjust_vec.is_empty ())
195 adjust_debug_stmts_now (&adjust_vec.last ());
196 adjust_vec.pop ();
199 adjust_vec.release ();
202 /* Adjust any debug stmts that referenced FROM values to use the
203 loop-closed TO, if the references are dominated by BB and not by
204 the definition of FROM. If adjust_vec is non-NULL, adjustments
205 will be postponed until adjust_vec_debug_stmts is called. */
207 static void
208 adjust_debug_stmts (tree from, tree to, basic_block bb)
210 adjust_info ai;
212 if (MAY_HAVE_DEBUG_STMTS
213 && TREE_CODE (from) == SSA_NAME
214 && ! SSA_NAME_IS_DEFAULT_DEF (from)
215 && ! virtual_operand_p (from))
217 ai.from = from;
218 ai.to = to;
219 ai.bb = bb;
221 if (adjust_vec.exists ())
222 adjust_vec.safe_push (ai);
223 else
224 adjust_debug_stmts_now (&ai);
228 /* Change E's phi arg in UPDATE_PHI to NEW_DEF, and record information
229 to adjust any debug stmts that referenced the old phi arg,
230 presumably non-loop-closed references left over from other
231 transformations. */
233 static void
234 adjust_phi_and_debug_stmts (gimple update_phi, edge e, tree new_def)
236 tree orig_def = PHI_ARG_DEF_FROM_EDGE (update_phi, e);
238 SET_PHI_ARG_DEF (update_phi, e->dest_idx, new_def);
240 if (MAY_HAVE_DEBUG_STMTS)
241 adjust_debug_stmts (orig_def, PHI_RESULT (update_phi),
242 gimple_bb (update_phi));
246 /* Update PHI nodes for a guard of the LOOP.
248 Input:
249 - LOOP, GUARD_EDGE: LOOP is a loop for which we added guard code that
250 controls whether LOOP is to be executed. GUARD_EDGE is the edge that
251 originates from the guard-bb, skips LOOP and reaches the (unique) exit
252 bb of LOOP. This loop-exit-bb is an empty bb with one successor.
253 We denote this bb NEW_MERGE_BB because before the guard code was added
254 it had a single predecessor (the LOOP header), and now it became a merge
255 point of two paths - the path that ends with the LOOP exit-edge, and
256 the path that ends with GUARD_EDGE.
257 - NEW_EXIT_BB: New basic block that is added by this function between LOOP
258 and NEW_MERGE_BB. It is used to place loop-closed-ssa-form exit-phis.
260 ===> The CFG before the guard-code was added:
261 LOOP_header_bb:
262 loop_body
263 if (exit_loop) goto update_bb
264 else goto LOOP_header_bb
265 update_bb:
267 ==> The CFG after the guard-code was added:
268 guard_bb:
269 if (LOOP_guard_condition) goto new_merge_bb
270 else goto LOOP_header_bb
271 LOOP_header_bb:
272 loop_body
273 if (exit_loop_condition) goto new_merge_bb
274 else goto LOOP_header_bb
275 new_merge_bb:
276 goto update_bb
277 update_bb:
279 ==> The CFG after this function:
280 guard_bb:
281 if (LOOP_guard_condition) goto new_merge_bb
282 else goto LOOP_header_bb
283 LOOP_header_bb:
284 loop_body
285 if (exit_loop_condition) goto new_exit_bb
286 else goto LOOP_header_bb
287 new_exit_bb:
288 new_merge_bb:
289 goto update_bb
290 update_bb:
292 This function:
293 1. creates and updates the relevant phi nodes to account for the new
294 incoming edge (GUARD_EDGE) into NEW_MERGE_BB. This involves:
295 1.1. Create phi nodes at NEW_MERGE_BB.
296 1.2. Update the phi nodes at the successor of NEW_MERGE_BB (denoted
297 UPDATE_BB). UPDATE_BB was the exit-bb of LOOP before NEW_MERGE_BB
298 2. preserves loop-closed-ssa-form by creating the required phi nodes
299 at the exit of LOOP (i.e, in NEW_EXIT_BB).
301 There are two flavors to this function:
303 slpeel_update_phi_nodes_for_guard1:
304 Here the guard controls whether we enter or skip LOOP, where LOOP is a
305 prolog_loop (loop1 below), and the new phis created in NEW_MERGE_BB are
306 for variables that have phis in the loop header.
308 slpeel_update_phi_nodes_for_guard2:
309 Here the guard controls whether we enter or skip LOOP, where LOOP is an
310 epilog_loop (loop2 below), and the new phis created in NEW_MERGE_BB are
311 for variables that have phis in the loop exit.
313 I.E., the overall structure is:
315 loop1_preheader_bb:
316 guard1 (goto loop1/merge1_bb)
317 loop1
318 loop1_exit_bb:
319 guard2 (goto merge1_bb/merge2_bb)
320 merge1_bb
321 loop2
322 loop2_exit_bb
323 merge2_bb
324 next_bb
326 slpeel_update_phi_nodes_for_guard1 takes care of creating phis in
327 loop1_exit_bb and merge1_bb. These are entry phis (phis for the vars
328 that have phis in loop1->header).
330 slpeel_update_phi_nodes_for_guard2 takes care of creating phis in
331 loop2_exit_bb and merge2_bb. These are exit phis (phis for the vars
332 that have phis in next_bb). It also adds some of these phis to
333 loop1_exit_bb.
335 slpeel_update_phi_nodes_for_guard1 is always called before
336 slpeel_update_phi_nodes_for_guard2. They are both needed in order
337 to create correct data-flow and loop-closed-ssa-form.
339 Generally slpeel_update_phi_nodes_for_guard1 creates phis for variables
340 that change between iterations of a loop (and therefore have a phi-node
341 at the loop entry), whereas slpeel_update_phi_nodes_for_guard2 creates
342 phis for variables that are used out of the loop (and therefore have
343 loop-closed exit phis). Some variables may be both updated between
344 iterations and used after the loop. This is why in loop1_exit_bb we
345 may need both entry_phis (created by slpeel_update_phi_nodes_for_guard1)
346 and exit phis (created by slpeel_update_phi_nodes_for_guard2).
348 - IS_NEW_LOOP: if IS_NEW_LOOP is true, then LOOP is a newly created copy of
349 an original loop. i.e., we have:
351 orig_loop
352 guard_bb (goto LOOP/new_merge)
353 new_loop <-- LOOP
354 new_exit
355 new_merge
356 next_bb
358 If IS_NEW_LOOP is false, then LOOP is an original loop, in which case we
359 have:
361 new_loop
362 guard_bb (goto LOOP/new_merge)
363 orig_loop <-- LOOP
364 new_exit
365 new_merge
366 next_bb
368 The SSA names defined in the original loop have a current
369 reaching definition that that records the corresponding new
370 ssa-name used in the new duplicated loop copy.
373 /* Function slpeel_update_phi_nodes_for_guard1
375 Input:
376 - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above.
377 - DEFS - a bitmap of ssa names to mark new names for which we recorded
378 information.
380 In the context of the overall structure, we have:
382 loop1_preheader_bb:
383 guard1 (goto loop1/merge1_bb)
384 LOOP-> loop1
385 loop1_exit_bb:
386 guard2 (goto merge1_bb/merge2_bb)
387 merge1_bb
388 loop2
389 loop2_exit_bb
390 merge2_bb
391 next_bb
393 For each name updated between loop iterations (i.e - for each name that has
394 an entry (loop-header) phi in LOOP) we create a new phi in:
395 1. merge1_bb (to account for the edge from guard1)
396 2. loop1_exit_bb (an exit-phi to keep LOOP in loop-closed form)
399 static void
400 slpeel_update_phi_nodes_for_guard1 (edge guard_edge, struct loop *loop,
401 bool is_new_loop, basic_block *new_exit_bb)
403 gphi *orig_phi, *new_phi;
404 gphi *update_phi, *update_phi2;
405 tree guard_arg, loop_arg;
406 basic_block new_merge_bb = guard_edge->dest;
407 edge e = EDGE_SUCC (new_merge_bb, 0);
408 basic_block update_bb = e->dest;
409 basic_block orig_bb = loop->header;
410 edge new_exit_e;
411 tree current_new_name;
412 gphi_iterator gsi_orig, gsi_update;
414 /* Create new bb between loop and new_merge_bb. */
415 *new_exit_bb = split_edge (single_exit (loop));
417 new_exit_e = EDGE_SUCC (*new_exit_bb, 0);
419 for (gsi_orig = gsi_start_phis (orig_bb),
420 gsi_update = gsi_start_phis (update_bb);
421 !gsi_end_p (gsi_orig) && !gsi_end_p (gsi_update);
422 gsi_next (&gsi_orig), gsi_next (&gsi_update))
424 source_location loop_locus, guard_locus;
425 tree new_res;
426 orig_phi = gsi_orig.phi ();
427 update_phi = gsi_update.phi ();
429 /** 1. Handle new-merge-point phis **/
431 /* 1.1. Generate new phi node in NEW_MERGE_BB: */
432 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
433 new_phi = create_phi_node (new_res, new_merge_bb);
435 /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge
436 of LOOP. Set the two phi args in NEW_PHI for these edges: */
437 loop_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, EDGE_SUCC (loop->latch, 0));
438 loop_locus = gimple_phi_arg_location_from_edge (orig_phi,
439 EDGE_SUCC (loop->latch,
440 0));
441 guard_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, loop_preheader_edge (loop));
442 guard_locus
443 = gimple_phi_arg_location_from_edge (orig_phi,
444 loop_preheader_edge (loop));
446 add_phi_arg (new_phi, loop_arg, new_exit_e, loop_locus);
447 add_phi_arg (new_phi, guard_arg, guard_edge, guard_locus);
449 /* 1.3. Update phi in successor block. */
450 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi, e) == loop_arg
451 || PHI_ARG_DEF_FROM_EDGE (update_phi, e) == guard_arg);
452 adjust_phi_and_debug_stmts (update_phi, e, PHI_RESULT (new_phi));
453 update_phi2 = new_phi;
456 /** 2. Handle loop-closed-ssa-form phis **/
458 if (virtual_operand_p (PHI_RESULT (orig_phi)))
459 continue;
461 /* 2.1. Generate new phi node in NEW_EXIT_BB: */
462 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
463 new_phi = create_phi_node (new_res, *new_exit_bb);
465 /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */
466 add_phi_arg (new_phi, loop_arg, single_exit (loop), loop_locus);
468 /* 2.3. Update phi in successor of NEW_EXIT_BB: */
469 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, new_exit_e) == loop_arg);
470 adjust_phi_and_debug_stmts (update_phi2, new_exit_e,
471 PHI_RESULT (new_phi));
473 /* 2.4. Record the newly created name with set_current_def.
474 We want to find a name such that
475 name = get_current_def (orig_loop_name)
476 and to set its current definition as follows:
477 set_current_def (name, new_phi_name)
479 If LOOP is a new loop then loop_arg is already the name we're
480 looking for. If LOOP is the original loop, then loop_arg is
481 the orig_loop_name and the relevant name is recorded in its
482 current reaching definition. */
483 if (is_new_loop)
484 current_new_name = loop_arg;
485 else
487 current_new_name = get_current_def (loop_arg);
488 /* current_def is not available only if the variable does not
489 change inside the loop, in which case we also don't care
490 about recording a current_def for it because we won't be
491 trying to create loop-exit-phis for it. */
492 if (!current_new_name)
493 continue;
495 tree new_name = get_current_def (current_new_name);
496 /* Because of peeled_chrec optimization it is possible that we have
497 set this earlier. Verify the PHI has the same value. */
498 if (new_name)
500 gimple phi = SSA_NAME_DEF_STMT (new_name);
501 gcc_assert (gimple_code (phi) == GIMPLE_PHI
502 && gimple_bb (phi) == *new_exit_bb
503 && (PHI_ARG_DEF_FROM_EDGE (phi, single_exit (loop))
504 == loop_arg));
505 continue;
508 set_current_def (current_new_name, PHI_RESULT (new_phi));
513 /* Function slpeel_update_phi_nodes_for_guard2
515 Input:
516 - GUARD_EDGE, LOOP, IS_NEW_LOOP, NEW_EXIT_BB - as explained above.
518 In the context of the overall structure, we have:
520 loop1_preheader_bb:
521 guard1 (goto loop1/merge1_bb)
522 loop1
523 loop1_exit_bb:
524 guard2 (goto merge1_bb/merge2_bb)
525 merge1_bb
526 LOOP-> loop2
527 loop2_exit_bb
528 merge2_bb
529 next_bb
531 For each name used out side the loop (i.e - for each name that has an exit
532 phi in next_bb) we create a new phi in:
533 1. merge2_bb (to account for the edge from guard_bb)
534 2. loop2_exit_bb (an exit-phi to keep LOOP in loop-closed form)
535 3. guard2 bb (an exit phi to keep the preceding loop in loop-closed form),
536 if needed (if it wasn't handled by slpeel_update_phis_nodes_for_phi1).
539 static void
540 slpeel_update_phi_nodes_for_guard2 (edge guard_edge, struct loop *loop,
541 bool is_new_loop, basic_block *new_exit_bb)
543 gphi *orig_phi, *new_phi;
544 gphi *update_phi, *update_phi2;
545 tree guard_arg, loop_arg;
546 basic_block new_merge_bb = guard_edge->dest;
547 edge e = EDGE_SUCC (new_merge_bb, 0);
548 basic_block update_bb = e->dest;
549 edge new_exit_e;
550 tree orig_def, orig_def_new_name;
551 tree new_name, new_name2;
552 tree arg;
553 gphi_iterator gsi;
555 /* Create new bb between loop and new_merge_bb. */
556 *new_exit_bb = split_edge (single_exit (loop));
558 new_exit_e = EDGE_SUCC (*new_exit_bb, 0);
560 for (gsi = gsi_start_phis (update_bb); !gsi_end_p (gsi); gsi_next (&gsi))
562 tree new_res;
563 update_phi = gsi.phi ();
564 orig_phi = update_phi;
565 orig_def = PHI_ARG_DEF_FROM_EDGE (orig_phi, e);
566 /* This loop-closed-phi actually doesn't represent a use
567 out of the loop - the phi arg is a constant. */
568 if (TREE_CODE (orig_def) != SSA_NAME)
569 continue;
570 orig_def_new_name = get_current_def (orig_def);
571 arg = NULL_TREE;
573 /** 1. Handle new-merge-point phis **/
575 /* 1.1. Generate new phi node in NEW_MERGE_BB: */
576 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
577 new_phi = create_phi_node (new_res, new_merge_bb);
579 /* 1.2. NEW_MERGE_BB has two incoming edges: GUARD_EDGE and the exit-edge
580 of LOOP. Set the two PHI args in NEW_PHI for these edges: */
581 new_name = orig_def;
582 new_name2 = NULL_TREE;
583 if (orig_def_new_name)
585 new_name = orig_def_new_name;
586 /* Some variables have both loop-entry-phis and loop-exit-phis.
587 Such variables were given yet newer names by phis placed in
588 guard_bb by slpeel_update_phi_nodes_for_guard1. I.e:
589 new_name2 = get_current_def (get_current_def (orig_name)). */
590 new_name2 = get_current_def (new_name);
593 if (is_new_loop)
595 guard_arg = orig_def;
596 loop_arg = new_name;
598 else
600 guard_arg = new_name;
601 loop_arg = orig_def;
603 if (new_name2)
604 guard_arg = new_name2;
606 add_phi_arg (new_phi, loop_arg, new_exit_e, UNKNOWN_LOCATION);
607 add_phi_arg (new_phi, guard_arg, guard_edge, UNKNOWN_LOCATION);
609 /* 1.3. Update phi in successor block. */
610 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi, e) == orig_def);
611 adjust_phi_and_debug_stmts (update_phi, e, PHI_RESULT (new_phi));
612 update_phi2 = new_phi;
615 /** 2. Handle loop-closed-ssa-form phis **/
617 /* 2.1. Generate new phi node in NEW_EXIT_BB: */
618 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
619 new_phi = create_phi_node (new_res, *new_exit_bb);
621 /* 2.2. NEW_EXIT_BB has one incoming edge: the exit-edge of the loop. */
622 add_phi_arg (new_phi, loop_arg, single_exit (loop), UNKNOWN_LOCATION);
624 /* 2.3. Update phi in successor of NEW_EXIT_BB: */
625 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, new_exit_e) == loop_arg);
626 adjust_phi_and_debug_stmts (update_phi2, new_exit_e,
627 PHI_RESULT (new_phi));
630 /** 3. Handle loop-closed-ssa-form phis for first loop **/
632 /* 3.1. Find the relevant names that need an exit-phi in
633 GUARD_BB, i.e. names for which
634 slpeel_update_phi_nodes_for_guard1 had not already created a
635 phi node. This is the case for names that are used outside
636 the loop (and therefore need an exit phi) but are not updated
637 across loop iterations (and therefore don't have a
638 loop-header-phi).
640 slpeel_update_phi_nodes_for_guard1 is responsible for
641 creating loop-exit phis in GUARD_BB for names that have a
642 loop-header-phi. When such a phi is created we also record
643 the new name in its current definition. If this new name
644 exists, then guard_arg was set to this new name (see 1.2
645 above). Therefore, if guard_arg is not this new name, this
646 is an indication that an exit-phi in GUARD_BB was not yet
647 created, so we take care of it here. */
648 if (guard_arg == new_name2)
649 continue;
650 arg = guard_arg;
652 /* 3.2. Generate new phi node in GUARD_BB: */
653 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
654 new_phi = create_phi_node (new_res, guard_edge->src);
656 /* 3.3. GUARD_BB has one incoming edge: */
657 gcc_assert (EDGE_COUNT (guard_edge->src->preds) == 1);
658 add_phi_arg (new_phi, arg, EDGE_PRED (guard_edge->src, 0),
659 UNKNOWN_LOCATION);
661 /* 3.4. Update phi in successor of GUARD_BB: */
662 gcc_assert (PHI_ARG_DEF_FROM_EDGE (update_phi2, guard_edge)
663 == guard_arg);
664 adjust_phi_and_debug_stmts (update_phi2, guard_edge,
665 PHI_RESULT (new_phi));
670 /* Make the LOOP iterate NITERS times. This is done by adding a new IV
671 that starts at zero, increases by one and its limit is NITERS.
673 Assumption: the exit-condition of LOOP is the last stmt in the loop. */
675 void
676 slpeel_make_loop_iterate_ntimes (struct loop *loop, tree niters)
678 tree indx_before_incr, indx_after_incr;
679 gcond *cond_stmt;
680 gcond *orig_cond;
681 edge exit_edge = single_exit (loop);
682 gimple_stmt_iterator loop_cond_gsi;
683 gimple_stmt_iterator incr_gsi;
684 bool insert_after;
685 tree init = build_int_cst (TREE_TYPE (niters), 0);
686 tree step = build_int_cst (TREE_TYPE (niters), 1);
687 source_location loop_loc;
688 enum tree_code code;
690 orig_cond = get_loop_exit_condition (loop);
691 gcc_assert (orig_cond);
692 loop_cond_gsi = gsi_for_stmt (orig_cond);
694 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
695 create_iv (init, step, NULL_TREE, loop,
696 &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr);
698 indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
699 true, NULL_TREE, true,
700 GSI_SAME_STMT);
701 niters = force_gimple_operand_gsi (&loop_cond_gsi, niters, true, NULL_TREE,
702 true, GSI_SAME_STMT);
704 code = (exit_edge->flags & EDGE_TRUE_VALUE) ? GE_EXPR : LT_EXPR;
705 cond_stmt = gimple_build_cond (code, indx_after_incr, niters, NULL_TREE,
706 NULL_TREE);
708 gsi_insert_before (&loop_cond_gsi, cond_stmt, GSI_SAME_STMT);
710 /* Remove old loop exit test: */
711 gsi_remove (&loop_cond_gsi, true);
712 free_stmt_vec_info (orig_cond);
714 loop_loc = find_loop_location (loop);
715 if (dump_enabled_p ())
717 if (LOCATION_LOCUS (loop_loc) != UNKNOWN_LOCATION)
718 dump_printf (MSG_NOTE, "\nloop at %s:%d: ", LOCATION_FILE (loop_loc),
719 LOCATION_LINE (loop_loc));
720 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, cond_stmt, 0);
721 dump_printf (MSG_NOTE, "\n");
723 loop->nb_iterations = niters;
726 /* Helper routine of slpeel_tree_duplicate_loop_to_edge_cfg.
727 For all PHI arguments in FROM->dest and TO->dest from those
728 edges ensure that TO->dest PHI arguments have current_def
729 to that in from. */
731 static void
732 slpeel_duplicate_current_defs_from_edges (edge from, edge to)
734 gimple_stmt_iterator gsi_from, gsi_to;
736 for (gsi_from = gsi_start_phis (from->dest),
737 gsi_to = gsi_start_phis (to->dest);
738 !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
739 gsi_next (&gsi_from), gsi_next (&gsi_to))
741 gimple from_phi = gsi_stmt (gsi_from);
742 gimple to_phi = gsi_stmt (gsi_to);
743 tree from_arg = PHI_ARG_DEF_FROM_EDGE (from_phi, from);
744 tree to_arg = PHI_ARG_DEF_FROM_EDGE (to_phi, to);
745 if (TREE_CODE (from_arg) == SSA_NAME
746 && TREE_CODE (to_arg) == SSA_NAME
747 && get_current_def (to_arg) == NULL_TREE)
748 set_current_def (to_arg, get_current_def (from_arg));
753 /* Given LOOP this function generates a new copy of it and puts it
754 on E which is either the entry or exit of LOOP. If SCALAR_LOOP is
755 non-NULL, assume LOOP and SCALAR_LOOP are equivalent and copy the
756 basic blocks from SCALAR_LOOP instead of LOOP, but to either the
757 entry or exit of LOOP. */
759 struct loop *
760 slpeel_tree_duplicate_loop_to_edge_cfg (struct loop *loop,
761 struct loop *scalar_loop, edge e)
763 struct loop *new_loop;
764 basic_block *new_bbs, *bbs;
765 bool at_exit;
766 bool was_imm_dom;
767 basic_block exit_dest;
768 edge exit, new_exit;
770 exit = single_exit (loop);
771 at_exit = (e == exit);
772 if (!at_exit && e != loop_preheader_edge (loop))
773 return NULL;
775 if (scalar_loop == NULL)
776 scalar_loop = loop;
778 bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1);
779 get_loop_body_with_size (scalar_loop, bbs, scalar_loop->num_nodes);
781 /* Check whether duplication is possible. */
782 if (!can_copy_bbs_p (bbs, scalar_loop->num_nodes))
784 free (bbs);
785 return NULL;
788 /* Generate new loop structure. */
789 new_loop = duplicate_loop (scalar_loop, loop_outer (scalar_loop));
790 duplicate_subloops (scalar_loop, new_loop);
792 exit_dest = exit->dest;
793 was_imm_dom = (get_immediate_dominator (CDI_DOMINATORS,
794 exit_dest) == loop->header ?
795 true : false);
797 /* Also copy the pre-header, this avoids jumping through hoops to
798 duplicate the loop entry PHI arguments. Create an empty
799 pre-header unconditionally for this. */
800 basic_block preheader = split_edge (loop_preheader_edge (scalar_loop));
801 edge entry_e = single_pred_edge (preheader);
802 bbs[scalar_loop->num_nodes] = preheader;
803 new_bbs = XNEWVEC (basic_block, scalar_loop->num_nodes + 1);
805 exit = single_exit (scalar_loop);
806 copy_bbs (bbs, scalar_loop->num_nodes + 1, new_bbs,
807 &exit, 1, &new_exit, NULL,
808 e->src, true);
809 exit = single_exit (loop);
810 basic_block new_preheader = new_bbs[scalar_loop->num_nodes];
812 add_phi_args_after_copy (new_bbs, scalar_loop->num_nodes + 1, NULL);
814 if (scalar_loop != loop)
816 /* If we copied from SCALAR_LOOP rather than LOOP, SSA_NAMEs from
817 SCALAR_LOOP will have current_def set to SSA_NAMEs in the new_loop,
818 but LOOP will not. slpeel_update_phi_nodes_for_guard{1,2} expects
819 the LOOP SSA_NAMEs (on the exit edge and edge from latch to
820 header) to have current_def set, so copy them over. */
821 slpeel_duplicate_current_defs_from_edges (single_exit (scalar_loop),
822 exit);
823 slpeel_duplicate_current_defs_from_edges (EDGE_SUCC (scalar_loop->latch,
825 EDGE_SUCC (loop->latch, 0));
828 if (at_exit) /* Add the loop copy at exit. */
830 if (scalar_loop != loop)
832 gphi_iterator gsi;
833 new_exit = redirect_edge_and_branch (new_exit, exit_dest);
835 for (gsi = gsi_start_phis (exit_dest); !gsi_end_p (gsi);
836 gsi_next (&gsi))
838 gphi *phi = gsi.phi ();
839 tree orig_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
840 location_t orig_locus
841 = gimple_phi_arg_location_from_edge (phi, e);
843 add_phi_arg (phi, orig_arg, new_exit, orig_locus);
846 redirect_edge_and_branch_force (e, new_preheader);
847 flush_pending_stmts (e);
848 set_immediate_dominator (CDI_DOMINATORS, new_preheader, e->src);
849 if (was_imm_dom)
850 set_immediate_dominator (CDI_DOMINATORS, exit_dest, new_exit->src);
852 /* And remove the non-necessary forwarder again. Keep the other
853 one so we have a proper pre-header for the loop at the exit edge. */
854 redirect_edge_pred (single_succ_edge (preheader),
855 single_pred (preheader));
856 delete_basic_block (preheader);
857 set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header,
858 loop_preheader_edge (scalar_loop)->src);
860 else /* Add the copy at entry. */
862 if (scalar_loop != loop)
864 /* Remove the non-necessary forwarder of scalar_loop again. */
865 redirect_edge_pred (single_succ_edge (preheader),
866 single_pred (preheader));
867 delete_basic_block (preheader);
868 set_immediate_dominator (CDI_DOMINATORS, scalar_loop->header,
869 loop_preheader_edge (scalar_loop)->src);
870 preheader = split_edge (loop_preheader_edge (loop));
871 entry_e = single_pred_edge (preheader);
874 redirect_edge_and_branch_force (entry_e, new_preheader);
875 flush_pending_stmts (entry_e);
876 set_immediate_dominator (CDI_DOMINATORS, new_preheader, entry_e->src);
878 redirect_edge_and_branch_force (new_exit, preheader);
879 flush_pending_stmts (new_exit);
880 set_immediate_dominator (CDI_DOMINATORS, preheader, new_exit->src);
882 /* And remove the non-necessary forwarder again. Keep the other
883 one so we have a proper pre-header for the loop at the exit edge. */
884 redirect_edge_pred (single_succ_edge (new_preheader),
885 single_pred (new_preheader));
886 delete_basic_block (new_preheader);
887 set_immediate_dominator (CDI_DOMINATORS, new_loop->header,
888 loop_preheader_edge (new_loop)->src);
891 for (unsigned i = 0; i < scalar_loop->num_nodes + 1; i++)
892 rename_variables_in_bb (new_bbs[i]);
894 if (scalar_loop != loop)
896 /* Update new_loop->header PHIs, so that on the preheader
897 edge they are the ones from loop rather than scalar_loop. */
898 gphi_iterator gsi_orig, gsi_new;
899 edge orig_e = loop_preheader_edge (loop);
900 edge new_e = loop_preheader_edge (new_loop);
902 for (gsi_orig = gsi_start_phis (loop->header),
903 gsi_new = gsi_start_phis (new_loop->header);
904 !gsi_end_p (gsi_orig) && !gsi_end_p (gsi_new);
905 gsi_next (&gsi_orig), gsi_next (&gsi_new))
907 gphi *orig_phi = gsi_orig.phi ();
908 gphi *new_phi = gsi_new.phi ();
909 tree orig_arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, orig_e);
910 location_t orig_locus
911 = gimple_phi_arg_location_from_edge (orig_phi, orig_e);
913 add_phi_arg (new_phi, orig_arg, new_e, orig_locus);
917 free (new_bbs);
918 free (bbs);
920 #ifdef ENABLE_CHECKING
921 verify_dominators (CDI_DOMINATORS);
922 #endif
924 return new_loop;
928 /* Given the condition statement COND, put it as the last statement
929 of GUARD_BB; EXIT_BB is the basic block to skip the loop;
930 Assumes that this is the single exit of the guarded loop.
931 Returns the skip edge, inserts new stmts on the COND_EXPR_STMT_LIST. */
933 static edge
934 slpeel_add_loop_guard (basic_block guard_bb, tree cond,
935 gimple_seq cond_expr_stmt_list,
936 basic_block exit_bb, basic_block dom_bb,
937 int probability)
939 gimple_stmt_iterator gsi;
940 edge new_e, enter_e;
941 gcond *cond_stmt;
942 gimple_seq gimplify_stmt_list = NULL;
944 enter_e = EDGE_SUCC (guard_bb, 0);
945 enter_e->flags &= ~EDGE_FALLTHRU;
946 enter_e->flags |= EDGE_FALSE_VALUE;
947 gsi = gsi_last_bb (guard_bb);
949 cond = force_gimple_operand_1 (cond, &gimplify_stmt_list, is_gimple_condexpr,
950 NULL_TREE);
951 if (gimplify_stmt_list)
952 gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list);
953 cond_stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE);
954 if (cond_expr_stmt_list)
955 gsi_insert_seq_after (&gsi, cond_expr_stmt_list, GSI_NEW_STMT);
957 gsi = gsi_last_bb (guard_bb);
958 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
960 /* Add new edge to connect guard block to the merge/loop-exit block. */
961 new_e = make_edge (guard_bb, exit_bb, EDGE_TRUE_VALUE);
963 new_e->count = guard_bb->count;
964 new_e->probability = probability;
965 new_e->count = apply_probability (enter_e->count, probability);
966 enter_e->count -= new_e->count;
967 enter_e->probability = inverse_probability (probability);
968 set_immediate_dominator (CDI_DOMINATORS, exit_bb, dom_bb);
969 return new_e;
973 /* This function verifies that the following restrictions apply to LOOP:
974 (1) it is innermost
975 (2) it consists of exactly 2 basic blocks - header, and an empty latch.
976 (3) it is single entry, single exit
977 (4) its exit condition is the last stmt in the header
978 (5) E is the entry/exit edge of LOOP.
981 bool
982 slpeel_can_duplicate_loop_p (const struct loop *loop, const_edge e)
984 edge exit_e = single_exit (loop);
985 edge entry_e = loop_preheader_edge (loop);
986 gcond *orig_cond = get_loop_exit_condition (loop);
987 gimple_stmt_iterator loop_exit_gsi = gsi_last_bb (exit_e->src);
989 if (loop->inner
990 /* All loops have an outer scope; the only case loop->outer is NULL is for
991 the function itself. */
992 || !loop_outer (loop)
993 || loop->num_nodes != 2
994 || !empty_block_p (loop->latch)
995 || !single_exit (loop)
996 /* Verify that new loop exit condition can be trivially modified. */
997 || (!orig_cond || orig_cond != gsi_stmt (loop_exit_gsi))
998 || (e != exit_e && e != entry_e))
999 return false;
1001 return true;
1004 #ifdef ENABLE_CHECKING
1005 static void
1006 slpeel_verify_cfg_after_peeling (struct loop *first_loop,
1007 struct loop *second_loop)
1009 basic_block loop1_exit_bb = single_exit (first_loop)->dest;
1010 basic_block loop2_entry_bb = loop_preheader_edge (second_loop)->src;
1011 basic_block loop1_entry_bb = loop_preheader_edge (first_loop)->src;
1013 /* A guard that controls whether the second_loop is to be executed or skipped
1014 is placed in first_loop->exit. first_loop->exit therefore has two
1015 successors - one is the preheader of second_loop, and the other is a bb
1016 after second_loop.
1018 gcc_assert (EDGE_COUNT (loop1_exit_bb->succs) == 2);
1020 /* 1. Verify that one of the successors of first_loop->exit is the preheader
1021 of second_loop. */
1023 /* The preheader of new_loop is expected to have two predecessors:
1024 first_loop->exit and the block that precedes first_loop. */
1026 gcc_assert (EDGE_COUNT (loop2_entry_bb->preds) == 2
1027 && ((EDGE_PRED (loop2_entry_bb, 0)->src == loop1_exit_bb
1028 && EDGE_PRED (loop2_entry_bb, 1)->src == loop1_entry_bb)
1029 || (EDGE_PRED (loop2_entry_bb, 1)->src == loop1_exit_bb
1030 && EDGE_PRED (loop2_entry_bb, 0)->src == loop1_entry_bb)));
1032 /* Verify that the other successor of first_loop->exit is after the
1033 second_loop. */
1034 /* TODO */
1036 #endif
1038 /* If the run time cost model check determines that vectorization is
1039 not profitable and hence scalar loop should be generated then set
1040 FIRST_NITERS to prologue peeled iterations. This will allow all the
1041 iterations to be executed in the prologue peeled scalar loop. */
1043 static void
1044 set_prologue_iterations (basic_block bb_before_first_loop,
1045 tree *first_niters,
1046 struct loop *loop,
1047 unsigned int th,
1048 int probability)
1050 edge e;
1051 basic_block cond_bb, then_bb;
1052 tree var, prologue_after_cost_adjust_name;
1053 gimple_stmt_iterator gsi;
1054 gphi *newphi;
1055 edge e_true, e_false, e_fallthru;
1056 gcond *cond_stmt;
1057 gimple_seq stmts = NULL;
1058 tree cost_pre_condition = NULL_TREE;
1059 tree scalar_loop_iters =
1060 unshare_expr (LOOP_VINFO_NITERS_UNCHANGED (loop_vec_info_for_loop (loop)));
1062 e = single_pred_edge (bb_before_first_loop);
1063 cond_bb = split_edge (e);
1065 e = single_pred_edge (bb_before_first_loop);
1066 then_bb = split_edge (e);
1067 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
1069 e_false = make_single_succ_edge (cond_bb, bb_before_first_loop,
1070 EDGE_FALSE_VALUE);
1071 set_immediate_dominator (CDI_DOMINATORS, bb_before_first_loop, cond_bb);
1073 e_true = EDGE_PRED (then_bb, 0);
1074 e_true->flags &= ~EDGE_FALLTHRU;
1075 e_true->flags |= EDGE_TRUE_VALUE;
1077 e_true->probability = probability;
1078 e_false->probability = inverse_probability (probability);
1079 e_true->count = apply_probability (cond_bb->count, probability);
1080 e_false->count = cond_bb->count - e_true->count;
1081 then_bb->frequency = EDGE_FREQUENCY (e_true);
1082 then_bb->count = e_true->count;
1084 e_fallthru = EDGE_SUCC (then_bb, 0);
1085 e_fallthru->count = then_bb->count;
1087 gsi = gsi_last_bb (cond_bb);
1088 cost_pre_condition =
1089 fold_build2 (LE_EXPR, boolean_type_node, scalar_loop_iters,
1090 build_int_cst (TREE_TYPE (scalar_loop_iters), th));
1091 cost_pre_condition =
1092 force_gimple_operand_gsi_1 (&gsi, cost_pre_condition, is_gimple_condexpr,
1093 NULL_TREE, false, GSI_CONTINUE_LINKING);
1094 cond_stmt = gimple_build_cond_from_tree (cost_pre_condition,
1095 NULL_TREE, NULL_TREE);
1096 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
1098 var = create_tmp_var (TREE_TYPE (scalar_loop_iters),
1099 "prologue_after_cost_adjust");
1100 prologue_after_cost_adjust_name =
1101 force_gimple_operand (scalar_loop_iters, &stmts, false, var);
1103 gsi = gsi_last_bb (then_bb);
1104 if (stmts)
1105 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
1107 newphi = create_phi_node (var, bb_before_first_loop);
1108 add_phi_arg (newphi, prologue_after_cost_adjust_name, e_fallthru,
1109 UNKNOWN_LOCATION);
1110 add_phi_arg (newphi, *first_niters, e_false, UNKNOWN_LOCATION);
1112 *first_niters = PHI_RESULT (newphi);
1115 /* Function slpeel_tree_peel_loop_to_edge.
1117 Peel the first (last) iterations of LOOP into a new prolog (epilog) loop
1118 that is placed on the entry (exit) edge E of LOOP. After this transformation
1119 we have two loops one after the other - first-loop iterates FIRST_NITERS
1120 times, and second-loop iterates the remainder NITERS - FIRST_NITERS times.
1121 If the cost model indicates that it is profitable to emit a scalar
1122 loop instead of the vector one, then the prolog (epilog) loop will iterate
1123 for the entire unchanged scalar iterations of the loop.
1125 Input:
1126 - LOOP: the loop to be peeled.
1127 - SCALAR_LOOP: if non-NULL, the alternate loop from which basic blocks
1128 should be copied.
1129 - E: the exit or entry edge of LOOP.
1130 If it is the entry edge, we peel the first iterations of LOOP. In this
1131 case first-loop is LOOP, and second-loop is the newly created loop.
1132 If it is the exit edge, we peel the last iterations of LOOP. In this
1133 case, first-loop is the newly created loop, and second-loop is LOOP.
1134 - NITERS: the number of iterations that LOOP iterates.
1135 - FIRST_NITERS: the number of iterations that the first-loop should iterate.
1136 - UPDATE_FIRST_LOOP_COUNT: specified whether this function is responsible
1137 for updating the loop bound of the first-loop to FIRST_NITERS. If it
1138 is false, the caller of this function may want to take care of this
1139 (this can be useful if we don't want new stmts added to first-loop).
1140 - TH: cost model profitability threshold of iterations for vectorization.
1141 - CHECK_PROFITABILITY: specify whether cost model check has not occurred
1142 during versioning and hence needs to occur during
1143 prologue generation or whether cost model check
1144 has not occurred during prologue generation and hence
1145 needs to occur during epilogue generation.
1146 - BOUND1 is the upper bound on number of iterations of the first loop (if known)
1147 - BOUND2 is the upper bound on number of iterations of the second loop (if known)
1150 Output:
1151 The function returns a pointer to the new loop-copy, or NULL if it failed
1152 to perform the transformation.
1154 The function generates two if-then-else guards: one before the first loop,
1155 and the other before the second loop:
1156 The first guard is:
1157 if (FIRST_NITERS == 0) then skip the first loop,
1158 and go directly to the second loop.
1159 The second guard is:
1160 if (FIRST_NITERS == NITERS) then skip the second loop.
1162 If the optional COND_EXPR and COND_EXPR_STMT_LIST arguments are given
1163 then the generated condition is combined with COND_EXPR and the
1164 statements in COND_EXPR_STMT_LIST are emitted together with it.
1166 FORNOW only simple loops are supported (see slpeel_can_duplicate_loop_p).
1167 FORNOW the resulting code will not be in loop-closed-ssa form.
1170 static struct loop *
1171 slpeel_tree_peel_loop_to_edge (struct loop *loop, struct loop *scalar_loop,
1172 edge e, tree *first_niters,
1173 tree niters, bool update_first_loop_count,
1174 unsigned int th, bool check_profitability,
1175 tree cond_expr, gimple_seq cond_expr_stmt_list,
1176 int bound1, int bound2)
1178 struct loop *new_loop = NULL, *first_loop, *second_loop;
1179 edge skip_e;
1180 tree pre_condition = NULL_TREE;
1181 basic_block bb_before_second_loop, bb_after_second_loop;
1182 basic_block bb_before_first_loop;
1183 basic_block bb_between_loops;
1184 basic_block new_exit_bb;
1185 gphi_iterator gsi;
1186 edge exit_e = single_exit (loop);
1187 source_location loop_loc;
1188 /* There are many aspects to how likely the first loop is going to be executed.
1189 Without histogram we can't really do good job. Simply set it to
1190 2/3, so the first loop is not reordered to the end of function and
1191 the hot path through stays short. */
1192 int first_guard_probability = 2 * REG_BR_PROB_BASE / 3;
1193 int second_guard_probability = 2 * REG_BR_PROB_BASE / 3;
1194 int probability_of_second_loop;
1196 if (!slpeel_can_duplicate_loop_p (loop, e))
1197 return NULL;
1199 /* We might have a queued need to update virtual SSA form. As we
1200 delete the update SSA machinery below after doing a regular
1201 incremental SSA update during loop copying make sure we don't
1202 lose that fact.
1203 ??? Needing to update virtual SSA form by renaming is unfortunate
1204 but not all of the vectorizer code inserting new loads / stores
1205 properly assigns virtual operands to those statements. */
1206 update_ssa (TODO_update_ssa_only_virtuals);
1208 /* If the loop has a virtual PHI, but exit bb doesn't, create a virtual PHI
1209 in the exit bb and rename all the uses after the loop. This simplifies
1210 the *guard[12] routines, which assume loop closed SSA form for all PHIs
1211 (but normally loop closed SSA form doesn't require virtual PHIs to be
1212 in the same form). Doing this early simplifies the checking what
1213 uses should be renamed. */
1214 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
1215 if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi))))
1217 gphi *phi = gsi.phi ();
1218 for (gsi = gsi_start_phis (exit_e->dest);
1219 !gsi_end_p (gsi); gsi_next (&gsi))
1220 if (virtual_operand_p (gimple_phi_result (gsi_stmt (gsi))))
1221 break;
1222 if (gsi_end_p (gsi))
1224 tree new_vop = copy_ssa_name (PHI_RESULT (phi));
1225 gphi *new_phi = create_phi_node (new_vop, exit_e->dest);
1226 tree vop = PHI_ARG_DEF_FROM_EDGE (phi, EDGE_SUCC (loop->latch, 0));
1227 imm_use_iterator imm_iter;
1228 gimple stmt;
1229 use_operand_p use_p;
1231 add_phi_arg (new_phi, vop, exit_e, UNKNOWN_LOCATION);
1232 gimple_phi_set_result (new_phi, new_vop);
1233 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, vop)
1234 if (stmt != new_phi && gimple_bb (stmt) != loop->header)
1235 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1236 SET_USE (use_p, new_vop);
1238 break;
1241 /* 1. Generate a copy of LOOP and put it on E (E is the entry/exit of LOOP).
1242 Resulting CFG would be:
1244 first_loop:
1245 do {
1246 } while ...
1248 second_loop:
1249 do {
1250 } while ...
1252 orig_exit_bb:
1255 if (!(new_loop = slpeel_tree_duplicate_loop_to_edge_cfg (loop, scalar_loop,
1256 e)))
1258 loop_loc = find_loop_location (loop);
1259 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loop_loc,
1260 "tree_duplicate_loop_to_edge_cfg failed.\n");
1261 return NULL;
1264 if (MAY_HAVE_DEBUG_STMTS)
1266 gcc_assert (!adjust_vec.exists ());
1267 adjust_vec.create (32);
1270 if (e == exit_e)
1272 /* NEW_LOOP was placed after LOOP. */
1273 first_loop = loop;
1274 second_loop = new_loop;
1276 else
1278 /* NEW_LOOP was placed before LOOP. */
1279 first_loop = new_loop;
1280 second_loop = loop;
1283 /* 2. Add the guard code in one of the following ways:
1285 2.a Add the guard that controls whether the first loop is executed.
1286 This occurs when this function is invoked for prologue or epilogue
1287 generation and when the cost model check can be done at compile time.
1289 Resulting CFG would be:
1291 bb_before_first_loop:
1292 if (FIRST_NITERS == 0) GOTO bb_before_second_loop
1293 GOTO first-loop
1295 first_loop:
1296 do {
1297 } while ...
1299 bb_before_second_loop:
1301 second_loop:
1302 do {
1303 } while ...
1305 orig_exit_bb:
1307 2.b Add the cost model check that allows the prologue
1308 to iterate for the entire unchanged scalar
1309 iterations of the loop in the event that the cost
1310 model indicates that the scalar loop is more
1311 profitable than the vector one. This occurs when
1312 this function is invoked for prologue generation
1313 and the cost model check needs to be done at run
1314 time.
1316 Resulting CFG after prologue peeling would be:
1318 if (scalar_loop_iterations <= th)
1319 FIRST_NITERS = scalar_loop_iterations
1321 bb_before_first_loop:
1322 if (FIRST_NITERS == 0) GOTO bb_before_second_loop
1323 GOTO first-loop
1325 first_loop:
1326 do {
1327 } while ...
1329 bb_before_second_loop:
1331 second_loop:
1332 do {
1333 } while ...
1335 orig_exit_bb:
1337 2.c Add the cost model check that allows the epilogue
1338 to iterate for the entire unchanged scalar
1339 iterations of the loop in the event that the cost
1340 model indicates that the scalar loop is more
1341 profitable than the vector one. This occurs when
1342 this function is invoked for epilogue generation
1343 and the cost model check needs to be done at run
1344 time. This check is combined with any pre-existing
1345 check in COND_EXPR to avoid versioning.
1347 Resulting CFG after prologue peeling would be:
1349 bb_before_first_loop:
1350 if ((scalar_loop_iterations <= th)
1352 FIRST_NITERS == 0) GOTO bb_before_second_loop
1353 GOTO first-loop
1355 first_loop:
1356 do {
1357 } while ...
1359 bb_before_second_loop:
1361 second_loop:
1362 do {
1363 } while ...
1365 orig_exit_bb:
1368 bb_before_first_loop = split_edge (loop_preheader_edge (first_loop));
1369 /* Loop copying insterted a forwarder block for us here. */
1370 bb_before_second_loop = single_exit (first_loop)->dest;
1372 probability_of_second_loop = (inverse_probability (first_guard_probability)
1373 + combine_probabilities (second_guard_probability,
1374 first_guard_probability));
1375 /* Theoretically preheader edge of first loop and exit edge should have
1376 same frequencies. Loop exit probablities are however easy to get wrong.
1377 It is safer to copy value from original loop entry. */
1378 bb_before_second_loop->frequency
1379 = combine_probabilities (bb_before_first_loop->frequency,
1380 probability_of_second_loop);
1381 bb_before_second_loop->count
1382 = apply_probability (bb_before_first_loop->count,
1383 probability_of_second_loop);
1384 single_succ_edge (bb_before_second_loop)->count
1385 = bb_before_second_loop->count;
1387 /* Epilogue peeling. */
1388 if (!update_first_loop_count)
1390 loop_vec_info loop_vinfo = loop_vec_info_for_loop (loop);
1391 tree scalar_loop_iters = LOOP_VINFO_NITERSM1 (loop_vinfo);
1392 unsigned limit = LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1;
1393 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
1394 limit = limit + 1;
1395 if (check_profitability
1396 && th > limit)
1397 limit = th;
1398 pre_condition =
1399 fold_build2 (LT_EXPR, boolean_type_node, scalar_loop_iters,
1400 build_int_cst (TREE_TYPE (scalar_loop_iters), limit));
1401 if (cond_expr)
1403 pre_condition =
1404 fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
1405 pre_condition,
1406 fold_build1 (TRUTH_NOT_EXPR, boolean_type_node,
1407 cond_expr));
1411 /* Prologue peeling. */
1412 else
1414 if (check_profitability)
1415 set_prologue_iterations (bb_before_first_loop, first_niters,
1416 loop, th, first_guard_probability);
1418 pre_condition =
1419 fold_build2 (LE_EXPR, boolean_type_node, *first_niters,
1420 build_int_cst (TREE_TYPE (*first_niters), 0));
1423 skip_e = slpeel_add_loop_guard (bb_before_first_loop, pre_condition,
1424 cond_expr_stmt_list,
1425 bb_before_second_loop, bb_before_first_loop,
1426 inverse_probability (first_guard_probability));
1427 scale_loop_profile (first_loop, first_guard_probability,
1428 check_profitability && (int)th > bound1 ? th : bound1);
1429 slpeel_update_phi_nodes_for_guard1 (skip_e, first_loop,
1430 first_loop == new_loop,
1431 &new_exit_bb);
1434 /* 3. Add the guard that controls whether the second loop is executed.
1435 Resulting CFG would be:
1437 bb_before_first_loop:
1438 if (FIRST_NITERS == 0) GOTO bb_before_second_loop (skip first loop)
1439 GOTO first-loop
1441 first_loop:
1442 do {
1443 } while ...
1445 bb_between_loops:
1446 if (FIRST_NITERS == NITERS) GOTO bb_after_second_loop (skip second loop)
1447 GOTO bb_before_second_loop
1449 bb_before_second_loop:
1451 second_loop:
1452 do {
1453 } while ...
1455 bb_after_second_loop:
1457 orig_exit_bb:
1460 bb_between_loops = new_exit_bb;
1461 bb_after_second_loop = split_edge (single_exit (second_loop));
1463 pre_condition =
1464 fold_build2 (EQ_EXPR, boolean_type_node, *first_niters, niters);
1465 skip_e = slpeel_add_loop_guard (bb_between_loops, pre_condition, NULL,
1466 bb_after_second_loop, bb_before_first_loop,
1467 inverse_probability (second_guard_probability));
1468 scale_loop_profile (second_loop, probability_of_second_loop, bound2);
1469 slpeel_update_phi_nodes_for_guard2 (skip_e, second_loop,
1470 second_loop == new_loop, &new_exit_bb);
1472 /* 4. Make first-loop iterate FIRST_NITERS times, if requested.
1474 if (update_first_loop_count)
1475 slpeel_make_loop_iterate_ntimes (first_loop, *first_niters);
1477 delete_update_ssa ();
1479 adjust_vec_debug_stmts ();
1481 return new_loop;
1484 /* Function vect_get_loop_location.
1486 Extract the location of the loop in the source code.
1487 If the loop is not well formed for vectorization, an estimated
1488 location is calculated.
1489 Return the loop location if succeed and NULL if not. */
1491 source_location
1492 find_loop_location (struct loop *loop)
1494 gimple stmt = NULL;
1495 basic_block bb;
1496 gimple_stmt_iterator si;
1498 if (!loop)
1499 return UNKNOWN_LOCATION;
1501 stmt = get_loop_exit_condition (loop);
1503 if (stmt
1504 && LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
1505 return gimple_location (stmt);
1507 /* If we got here the loop is probably not "well formed",
1508 try to estimate the loop location */
1510 if (!loop->header)
1511 return UNKNOWN_LOCATION;
1513 bb = loop->header;
1515 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1517 stmt = gsi_stmt (si);
1518 if (LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
1519 return gimple_location (stmt);
1522 return UNKNOWN_LOCATION;
1526 /* Function vect_can_advance_ivs_p
1528 In case the number of iterations that LOOP iterates is unknown at compile
1529 time, an epilog loop will be generated, and the loop induction variables
1530 (IVs) will be "advanced" to the value they are supposed to take just before
1531 the epilog loop. Here we check that the access function of the loop IVs
1532 and the expression that represents the loop bound are simple enough.
1533 These restrictions will be relaxed in the future. */
1535 bool
1536 vect_can_advance_ivs_p (loop_vec_info loop_vinfo)
1538 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1539 basic_block bb = loop->header;
1540 gimple phi;
1541 gphi_iterator gsi;
1543 /* Analyze phi functions of the loop header. */
1545 if (dump_enabled_p ())
1546 dump_printf_loc (MSG_NOTE, vect_location, "vect_can_advance_ivs_p:\n");
1547 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1549 tree evolution_part;
1551 phi = gsi.phi ();
1552 if (dump_enabled_p ())
1554 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
1555 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
1556 dump_printf (MSG_NOTE, "\n");
1559 /* Skip virtual phi's. The data dependences that are associated with
1560 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
1562 if (virtual_operand_p (PHI_RESULT (phi)))
1564 if (dump_enabled_p ())
1565 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1566 "virtual phi. skip.\n");
1567 continue;
1570 /* Skip reduction phis. */
1572 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (phi)) == vect_reduction_def)
1574 if (dump_enabled_p ())
1575 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1576 "reduc phi. skip.\n");
1577 continue;
1580 /* Analyze the evolution function. */
1582 evolution_part
1583 = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (vinfo_for_stmt (phi));
1584 if (evolution_part == NULL_TREE)
1586 if (dump_enabled_p ())
1587 dump_printf (MSG_MISSED_OPTIMIZATION,
1588 "No access function or evolution.\n");
1589 return false;
1592 /* FORNOW: We do not transform initial conditions of IVs
1593 which evolution functions are a polynomial of degree >= 2. */
1595 if (tree_is_chrec (evolution_part))
1596 return false;
1599 return true;
1603 /* Function vect_update_ivs_after_vectorizer.
1605 "Advance" the induction variables of LOOP to the value they should take
1606 after the execution of LOOP. This is currently necessary because the
1607 vectorizer does not handle induction variables that are used after the
1608 loop. Such a situation occurs when the last iterations of LOOP are
1609 peeled, because:
1610 1. We introduced new uses after LOOP for IVs that were not originally used
1611 after LOOP: the IVs of LOOP are now used by an epilog loop.
1612 2. LOOP is going to be vectorized; this means that it will iterate N/VF
1613 times, whereas the loop IVs should be bumped N times.
1615 Input:
1616 - LOOP - a loop that is going to be vectorized. The last few iterations
1617 of LOOP were peeled.
1618 - NITERS - the number of iterations that LOOP executes (before it is
1619 vectorized). i.e, the number of times the ivs should be bumped.
1620 - UPDATE_E - a successor edge of LOOP->exit that is on the (only) path
1621 coming out from LOOP on which there are uses of the LOOP ivs
1622 (this is the path from LOOP->exit to epilog_loop->preheader).
1624 The new definitions of the ivs are placed in LOOP->exit.
1625 The phi args associated with the edge UPDATE_E in the bb
1626 UPDATE_E->dest are updated accordingly.
1628 Assumption 1: Like the rest of the vectorizer, this function assumes
1629 a single loop exit that has a single predecessor.
1631 Assumption 2: The phi nodes in the LOOP header and in update_bb are
1632 organized in the same order.
1634 Assumption 3: The access function of the ivs is simple enough (see
1635 vect_can_advance_ivs_p). This assumption will be relaxed in the future.
1637 Assumption 4: Exactly one of the successors of LOOP exit-bb is on a path
1638 coming out of LOOP on which the ivs of LOOP are used (this is the path
1639 that leads to the epilog loop; other paths skip the epilog loop). This
1640 path starts with the edge UPDATE_E, and its destination (denoted update_bb)
1641 needs to have its phis updated.
1644 static void
1645 vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo, tree niters,
1646 edge update_e)
1648 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1649 basic_block exit_bb = single_exit (loop)->dest;
1650 gphi *phi, *phi1;
1651 gphi_iterator gsi, gsi1;
1652 basic_block update_bb = update_e->dest;
1654 gcc_checking_assert (vect_can_advance_ivs_p (loop_vinfo));
1656 /* Make sure there exists a single-predecessor exit bb: */
1657 gcc_assert (single_pred_p (exit_bb));
1659 for (gsi = gsi_start_phis (loop->header), gsi1 = gsi_start_phis (update_bb);
1660 !gsi_end_p (gsi) && !gsi_end_p (gsi1);
1661 gsi_next (&gsi), gsi_next (&gsi1))
1663 tree init_expr;
1664 tree step_expr, off;
1665 tree type;
1666 tree var, ni, ni_name;
1667 gimple_stmt_iterator last_gsi;
1668 stmt_vec_info stmt_info;
1670 phi = gsi.phi ();
1671 phi1 = gsi1.phi ();
1672 if (dump_enabled_p ())
1674 dump_printf_loc (MSG_NOTE, vect_location,
1675 "vect_update_ivs_after_vectorizer: phi: ");
1676 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
1677 dump_printf (MSG_NOTE, "\n");
1680 /* Skip virtual phi's. */
1681 if (virtual_operand_p (PHI_RESULT (phi)))
1683 if (dump_enabled_p ())
1684 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1685 "virtual phi. skip.\n");
1686 continue;
1689 /* Skip reduction phis. */
1690 stmt_info = vinfo_for_stmt (phi);
1691 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
1693 if (dump_enabled_p ())
1694 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1695 "reduc phi. skip.\n");
1696 continue;
1699 type = TREE_TYPE (gimple_phi_result (phi));
1700 step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info);
1701 step_expr = unshare_expr (step_expr);
1703 /* FORNOW: We do not support IVs whose evolution function is a polynomial
1704 of degree >= 2 or exponential. */
1705 gcc_assert (!tree_is_chrec (step_expr));
1707 init_expr = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1709 off = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
1710 fold_convert (TREE_TYPE (step_expr), niters),
1711 step_expr);
1712 if (POINTER_TYPE_P (type))
1713 ni = fold_build_pointer_plus (init_expr, off);
1714 else
1715 ni = fold_build2 (PLUS_EXPR, type,
1716 init_expr, fold_convert (type, off));
1718 var = create_tmp_var (type, "tmp");
1720 last_gsi = gsi_last_bb (exit_bb);
1721 ni_name = force_gimple_operand_gsi (&last_gsi, ni, false, var,
1722 true, GSI_SAME_STMT);
1724 /* Fix phi expressions in the successor bb. */
1725 adjust_phi_and_debug_stmts (phi1, update_e, ni_name);
1729 /* Function vect_do_peeling_for_loop_bound
1731 Peel the last iterations of the loop represented by LOOP_VINFO.
1732 The peeled iterations form a new epilog loop. Given that the loop now
1733 iterates NITERS times, the new epilog loop iterates
1734 NITERS % VECTORIZATION_FACTOR times.
1736 The original loop will later be made to iterate
1737 NITERS / VECTORIZATION_FACTOR times (this value is placed into RATIO).
1739 COND_EXPR and COND_EXPR_STMT_LIST are combined with a new generated
1740 test. */
1742 void
1743 vect_do_peeling_for_loop_bound (loop_vec_info loop_vinfo,
1744 tree ni_name, tree ratio_mult_vf_name,
1745 unsigned int th, bool check_profitability)
1747 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1748 struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
1749 struct loop *new_loop;
1750 edge update_e;
1751 basic_block preheader;
1752 int loop_num;
1753 int max_iter;
1754 tree cond_expr = NULL_TREE;
1755 gimple_seq cond_expr_stmt_list = NULL;
1757 if (dump_enabled_p ())
1758 dump_printf_loc (MSG_NOTE, vect_location,
1759 "=== vect_do_peeling_for_loop_bound ===\n");
1761 initialize_original_copy_tables ();
1763 loop_num = loop->num;
1765 new_loop
1766 = slpeel_tree_peel_loop_to_edge (loop, scalar_loop, single_exit (loop),
1767 &ratio_mult_vf_name, ni_name, false,
1768 th, check_profitability,
1769 cond_expr, cond_expr_stmt_list,
1770 0, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
1771 gcc_assert (new_loop);
1772 gcc_assert (loop_num == loop->num);
1773 #ifdef ENABLE_CHECKING
1774 slpeel_verify_cfg_after_peeling (loop, new_loop);
1775 #endif
1777 /* A guard that controls whether the new_loop is to be executed or skipped
1778 is placed in LOOP->exit. LOOP->exit therefore has two successors - one
1779 is the preheader of NEW_LOOP, where the IVs from LOOP are used. The other
1780 is a bb after NEW_LOOP, where these IVs are not used. Find the edge that
1781 is on the path where the LOOP IVs are used and need to be updated. */
1783 preheader = loop_preheader_edge (new_loop)->src;
1784 if (EDGE_PRED (preheader, 0)->src == single_exit (loop)->dest)
1785 update_e = EDGE_PRED (preheader, 0);
1786 else
1787 update_e = EDGE_PRED (preheader, 1);
1789 /* Update IVs of original loop as if they were advanced
1790 by ratio_mult_vf_name steps. */
1791 vect_update_ivs_after_vectorizer (loop_vinfo, ratio_mult_vf_name, update_e);
1793 /* For vectorization factor N, we need to copy last N-1 values in epilogue
1794 and this means N-2 loopback edge executions.
1796 PEELING_FOR_GAPS works by subtracting last iteration and thus the epilogue
1797 will execute at least LOOP_VINFO_VECT_FACTOR times. */
1798 max_iter = (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
1799 ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) * 2
1800 : LOOP_VINFO_VECT_FACTOR (loop_vinfo)) - 2;
1801 if (check_profitability)
1802 max_iter = MAX (max_iter, (int) th - 1);
1803 record_niter_bound (new_loop, max_iter, false, true);
1804 dump_printf (MSG_NOTE,
1805 "Setting upper bound of nb iterations for epilogue "
1806 "loop to %d\n", max_iter);
1808 /* After peeling we have to reset scalar evolution analyzer. */
1809 scev_reset ();
1811 free_original_copy_tables ();
1815 /* Function vect_gen_niters_for_prolog_loop
1817 Set the number of iterations for the loop represented by LOOP_VINFO
1818 to the minimum between LOOP_NITERS (the original iteration count of the loop)
1819 and the misalignment of DR - the data reference recorded in
1820 LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO). As a result, after the execution of
1821 this loop, the data reference DR will refer to an aligned location.
1823 The following computation is generated:
1825 If the misalignment of DR is known at compile time:
1826 addr_mis = int mis = DR_MISALIGNMENT (dr);
1827 Else, compute address misalignment in bytes:
1828 addr_mis = addr & (vectype_align - 1)
1830 prolog_niters = min (LOOP_NITERS, ((VF - addr_mis/elem_size)&(VF-1))/step)
1832 (elem_size = element type size; an element is the scalar element whose type
1833 is the inner type of the vectype)
1835 When the step of the data-ref in the loop is not 1 (as in interleaved data
1836 and SLP), the number of iterations of the prolog must be divided by the step
1837 (which is equal to the size of interleaved group).
1839 The above formulas assume that VF == number of elements in the vector. This
1840 may not hold when there are multiple-types in the loop.
1841 In this case, for some data-references in the loop the VF does not represent
1842 the number of elements that fit in the vector. Therefore, instead of VF we
1843 use TYPE_VECTOR_SUBPARTS. */
1845 static tree
1846 vect_gen_niters_for_prolog_loop (loop_vec_info loop_vinfo, tree loop_niters, int *bound)
1848 struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
1849 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1850 tree var;
1851 gimple_seq stmts;
1852 tree iters, iters_name;
1853 edge pe;
1854 basic_block new_bb;
1855 gimple dr_stmt = DR_STMT (dr);
1856 stmt_vec_info stmt_info = vinfo_for_stmt (dr_stmt);
1857 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1858 int vectype_align = TYPE_ALIGN (vectype) / BITS_PER_UNIT;
1859 tree niters_type = TREE_TYPE (loop_niters);
1860 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1862 pe = loop_preheader_edge (loop);
1864 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
1866 int npeel = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
1868 if (dump_enabled_p ())
1869 dump_printf_loc (MSG_NOTE, vect_location,
1870 "known peeling = %d.\n", npeel);
1872 iters = build_int_cst (niters_type, npeel);
1873 *bound = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
1875 else
1877 gimple_seq new_stmts = NULL;
1878 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
1879 tree offset = negative
1880 ? size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1) : size_zero_node;
1881 tree start_addr = vect_create_addr_base_for_vector_ref (dr_stmt,
1882 &new_stmts, offset, loop);
1883 tree type = unsigned_type_for (TREE_TYPE (start_addr));
1884 tree vectype_align_minus_1 = build_int_cst (type, vectype_align - 1);
1885 HOST_WIDE_INT elem_size =
1886 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1887 tree elem_size_log = build_int_cst (type, exact_log2 (elem_size));
1888 tree nelements_minus_1 = build_int_cst (type, nelements - 1);
1889 tree nelements_tree = build_int_cst (type, nelements);
1890 tree byte_misalign;
1891 tree elem_misalign;
1893 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmts);
1894 gcc_assert (!new_bb);
1896 /* Create: byte_misalign = addr & (vectype_align - 1) */
1897 byte_misalign =
1898 fold_build2 (BIT_AND_EXPR, type, fold_convert (type, start_addr),
1899 vectype_align_minus_1);
1901 /* Create: elem_misalign = byte_misalign / element_size */
1902 elem_misalign =
1903 fold_build2 (RSHIFT_EXPR, type, byte_misalign, elem_size_log);
1905 /* Create: (niters_type) (nelements - elem_misalign)&(nelements - 1) */
1906 if (negative)
1907 iters = fold_build2 (MINUS_EXPR, type, elem_misalign, nelements_tree);
1908 else
1909 iters = fold_build2 (MINUS_EXPR, type, nelements_tree, elem_misalign);
1910 iters = fold_build2 (BIT_AND_EXPR, type, iters, nelements_minus_1);
1911 iters = fold_convert (niters_type, iters);
1912 *bound = nelements;
1915 /* Create: prolog_loop_niters = min (iters, loop_niters) */
1916 /* If the loop bound is known at compile time we already verified that it is
1917 greater than vf; since the misalignment ('iters') is at most vf, there's
1918 no need to generate the MIN_EXPR in this case. */
1919 if (TREE_CODE (loop_niters) != INTEGER_CST)
1920 iters = fold_build2 (MIN_EXPR, niters_type, iters, loop_niters);
1922 if (dump_enabled_p ())
1924 dump_printf_loc (MSG_NOTE, vect_location,
1925 "niters for prolog loop: ");
1926 dump_generic_expr (MSG_NOTE, TDF_SLIM, iters);
1927 dump_printf (MSG_NOTE, "\n");
1930 var = create_tmp_var (niters_type, "prolog_loop_niters");
1931 stmts = NULL;
1932 iters_name = force_gimple_operand (iters, &stmts, false, var);
1934 /* Insert stmt on loop preheader edge. */
1935 if (stmts)
1937 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
1938 gcc_assert (!new_bb);
1941 return iters_name;
1945 /* Function vect_update_init_of_dr
1947 NITERS iterations were peeled from LOOP. DR represents a data reference
1948 in LOOP. This function updates the information recorded in DR to
1949 account for the fact that the first NITERS iterations had already been
1950 executed. Specifically, it updates the OFFSET field of DR. */
1952 static void
1953 vect_update_init_of_dr (struct data_reference *dr, tree niters)
1955 tree offset = DR_OFFSET (dr);
1957 niters = fold_build2 (MULT_EXPR, sizetype,
1958 fold_convert (sizetype, niters),
1959 fold_convert (sizetype, DR_STEP (dr)));
1960 offset = fold_build2 (PLUS_EXPR, sizetype,
1961 fold_convert (sizetype, offset), niters);
1962 DR_OFFSET (dr) = offset;
1966 /* Function vect_update_inits_of_drs
1968 NITERS iterations were peeled from the loop represented by LOOP_VINFO.
1969 This function updates the information recorded for the data references in
1970 the loop to account for the fact that the first NITERS iterations had
1971 already been executed. Specifically, it updates the initial_condition of
1972 the access_function of all the data_references in the loop. */
1974 static void
1975 vect_update_inits_of_drs (loop_vec_info loop_vinfo, tree niters)
1977 unsigned int i;
1978 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1979 struct data_reference *dr;
1981 if (dump_enabled_p ())
1982 dump_printf_loc (MSG_NOTE, vect_location,
1983 "=== vect_update_inits_of_dr ===\n");
1985 FOR_EACH_VEC_ELT (datarefs, i, dr)
1986 vect_update_init_of_dr (dr, niters);
1990 /* Function vect_do_peeling_for_alignment
1992 Peel the first 'niters' iterations of the loop represented by LOOP_VINFO.
1993 'niters' is set to the misalignment of one of the data references in the
1994 loop, thereby forcing it to refer to an aligned location at the beginning
1995 of the execution of this loop. The data reference for which we are
1996 peeling is recorded in LOOP_VINFO_UNALIGNED_DR. */
1998 void
1999 vect_do_peeling_for_alignment (loop_vec_info loop_vinfo, tree ni_name,
2000 unsigned int th, bool check_profitability)
2002 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2003 struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
2004 tree niters_of_prolog_loop;
2005 tree wide_prolog_niters;
2006 struct loop *new_loop;
2007 int max_iter;
2008 int bound = 0;
2010 if (dump_enabled_p ())
2011 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
2012 "loop peeled for vectorization to enhance"
2013 " alignment\n");
2015 initialize_original_copy_tables ();
2017 gimple_seq stmts = NULL;
2018 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2019 niters_of_prolog_loop = vect_gen_niters_for_prolog_loop (loop_vinfo,
2020 ni_name,
2021 &bound);
2023 /* Peel the prolog loop and iterate it niters_of_prolog_loop. */
2024 new_loop =
2025 slpeel_tree_peel_loop_to_edge (loop, scalar_loop,
2026 loop_preheader_edge (loop),
2027 &niters_of_prolog_loop, ni_name, true,
2028 th, check_profitability, NULL_TREE, NULL,
2029 bound, 0);
2031 gcc_assert (new_loop);
2032 #ifdef ENABLE_CHECKING
2033 slpeel_verify_cfg_after_peeling (new_loop, loop);
2034 #endif
2035 /* For vectorization factor N, we need to copy at most N-1 values
2036 for alignment and this means N-2 loopback edge executions. */
2037 max_iter = LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 2;
2038 if (check_profitability)
2039 max_iter = MAX (max_iter, (int) th - 1);
2040 record_niter_bound (new_loop, max_iter, false, true);
2041 dump_printf (MSG_NOTE,
2042 "Setting upper bound of nb iterations for prologue "
2043 "loop to %d\n", max_iter);
2045 /* Update number of times loop executes. */
2046 LOOP_VINFO_NITERS (loop_vinfo) = fold_build2 (MINUS_EXPR,
2047 TREE_TYPE (ni_name), ni_name, niters_of_prolog_loop);
2048 LOOP_VINFO_NITERSM1 (loop_vinfo) = fold_build2 (MINUS_EXPR,
2049 TREE_TYPE (ni_name),
2050 LOOP_VINFO_NITERSM1 (loop_vinfo), niters_of_prolog_loop);
2052 if (types_compatible_p (sizetype, TREE_TYPE (niters_of_prolog_loop)))
2053 wide_prolog_niters = niters_of_prolog_loop;
2054 else
2056 gimple_seq seq = NULL;
2057 edge pe = loop_preheader_edge (loop);
2058 tree wide_iters = fold_convert (sizetype, niters_of_prolog_loop);
2059 tree var = create_tmp_var (sizetype, "prolog_loop_adjusted_niters");
2060 wide_prolog_niters = force_gimple_operand (wide_iters, &seq, false,
2061 var);
2062 if (seq)
2064 /* Insert stmt on loop preheader edge. */
2065 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
2066 gcc_assert (!new_bb);
2070 /* Update the init conditions of the access functions of all data refs. */
2071 vect_update_inits_of_drs (loop_vinfo, wide_prolog_niters);
2073 /* After peeling we have to reset scalar evolution analyzer. */
2074 scev_reset ();
2076 free_original_copy_tables ();
2080 /* Function vect_create_cond_for_align_checks.
2082 Create a conditional expression that represents the alignment checks for
2083 all of data references (array element references) whose alignment must be
2084 checked at runtime.
2086 Input:
2087 COND_EXPR - input conditional expression. New conditions will be chained
2088 with logical AND operation.
2089 LOOP_VINFO - two fields of the loop information are used.
2090 LOOP_VINFO_PTR_MASK is the mask used to check the alignment.
2091 LOOP_VINFO_MAY_MISALIGN_STMTS contains the refs to be checked.
2093 Output:
2094 COND_EXPR_STMT_LIST - statements needed to construct the conditional
2095 expression.
2096 The returned value is the conditional expression to be used in the if
2097 statement that controls which version of the loop gets executed at runtime.
2099 The algorithm makes two assumptions:
2100 1) The number of bytes "n" in a vector is a power of 2.
2101 2) An address "a" is aligned if a%n is zero and that this
2102 test can be done as a&(n-1) == 0. For example, for 16
2103 byte vectors the test is a&0xf == 0. */
2105 static void
2106 vect_create_cond_for_align_checks (loop_vec_info loop_vinfo,
2107 tree *cond_expr,
2108 gimple_seq *cond_expr_stmt_list)
2110 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2111 vec<gimple> may_misalign_stmts
2112 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
2113 gimple ref_stmt;
2114 int mask = LOOP_VINFO_PTR_MASK (loop_vinfo);
2115 tree mask_cst;
2116 unsigned int i;
2117 tree int_ptrsize_type;
2118 char tmp_name[20];
2119 tree or_tmp_name = NULL_TREE;
2120 tree and_tmp_name;
2121 gimple and_stmt;
2122 tree ptrsize_zero;
2123 tree part_cond_expr;
2125 /* Check that mask is one less than a power of 2, i.e., mask is
2126 all zeros followed by all ones. */
2127 gcc_assert ((mask != 0) && ((mask & (mask+1)) == 0));
2129 int_ptrsize_type = signed_type_for (ptr_type_node);
2131 /* Create expression (mask & (dr_1 || ... || dr_n)) where dr_i is the address
2132 of the first vector of the i'th data reference. */
2134 FOR_EACH_VEC_ELT (may_misalign_stmts, i, ref_stmt)
2136 gimple_seq new_stmt_list = NULL;
2137 tree addr_base;
2138 tree addr_tmp_name;
2139 tree new_or_tmp_name;
2140 gimple addr_stmt, or_stmt;
2141 stmt_vec_info stmt_vinfo = vinfo_for_stmt (ref_stmt);
2142 tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
2143 bool negative = tree_int_cst_compare
2144 (DR_STEP (STMT_VINFO_DATA_REF (stmt_vinfo)), size_zero_node) < 0;
2145 tree offset = negative
2146 ? size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1) : NULL_TREE;
2148 /* create: addr_tmp = (int)(address_of_first_vector) */
2149 addr_base =
2150 vect_create_addr_base_for_vector_ref (ref_stmt, &new_stmt_list,
2151 offset, loop);
2152 if (new_stmt_list != NULL)
2153 gimple_seq_add_seq (cond_expr_stmt_list, new_stmt_list);
2155 sprintf (tmp_name, "addr2int%d", i);
2156 addr_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, tmp_name);
2157 addr_stmt = gimple_build_assign (addr_tmp_name, NOP_EXPR, addr_base);
2158 gimple_seq_add_stmt (cond_expr_stmt_list, addr_stmt);
2160 /* The addresses are OR together. */
2162 if (or_tmp_name != NULL_TREE)
2164 /* create: or_tmp = or_tmp | addr_tmp */
2165 sprintf (tmp_name, "orptrs%d", i);
2166 new_or_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, tmp_name);
2167 or_stmt = gimple_build_assign (new_or_tmp_name, BIT_IOR_EXPR,
2168 or_tmp_name, addr_tmp_name);
2169 gimple_seq_add_stmt (cond_expr_stmt_list, or_stmt);
2170 or_tmp_name = new_or_tmp_name;
2172 else
2173 or_tmp_name = addr_tmp_name;
2175 } /* end for i */
2177 mask_cst = build_int_cst (int_ptrsize_type, mask);
2179 /* create: and_tmp = or_tmp & mask */
2180 and_tmp_name = make_temp_ssa_name (int_ptrsize_type, NULL, "andmask");
2182 and_stmt = gimple_build_assign (and_tmp_name, BIT_AND_EXPR,
2183 or_tmp_name, mask_cst);
2184 gimple_seq_add_stmt (cond_expr_stmt_list, and_stmt);
2186 /* Make and_tmp the left operand of the conditional test against zero.
2187 if and_tmp has a nonzero bit then some address is unaligned. */
2188 ptrsize_zero = build_int_cst (int_ptrsize_type, 0);
2189 part_cond_expr = fold_build2 (EQ_EXPR, boolean_type_node,
2190 and_tmp_name, ptrsize_zero);
2191 if (*cond_expr)
2192 *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2193 *cond_expr, part_cond_expr);
2194 else
2195 *cond_expr = part_cond_expr;
2198 /* Function vect_create_cond_for_alias_checks.
2200 Create a conditional expression that represents the run-time checks for
2201 overlapping of address ranges represented by a list of data references
2202 relations passed as input.
2204 Input:
2205 COND_EXPR - input conditional expression. New conditions will be chained
2206 with logical AND operation. If it is NULL, then the function
2207 is used to return the number of alias checks.
2208 LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs
2209 to be checked.
2211 Output:
2212 COND_EXPR - conditional expression.
2214 The returned COND_EXPR is the conditional expression to be used in the if
2215 statement that controls which version of the loop gets executed at runtime.
2218 void
2219 vect_create_cond_for_alias_checks (loop_vec_info loop_vinfo, tree * cond_expr)
2221 vec<dr_with_seg_len_pair_t> comp_alias_ddrs =
2222 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2223 tree part_cond_expr;
2225 /* Create expression
2226 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2227 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2231 ((store_ptr_n + store_segment_length_n) <= load_ptr_n)
2232 || (load_ptr_n + load_segment_length_n) <= store_ptr_n)) */
2234 if (comp_alias_ddrs.is_empty ())
2235 return;
2237 for (size_t i = 0, s = comp_alias_ddrs.length (); i < s; ++i)
2239 const dr_with_seg_len& dr_a = comp_alias_ddrs[i].first;
2240 const dr_with_seg_len& dr_b = comp_alias_ddrs[i].second;
2241 tree segment_length_a = dr_a.seg_len;
2242 tree segment_length_b = dr_b.seg_len;
2244 tree addr_base_a
2245 = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_a.dr), dr_a.offset);
2246 tree addr_base_b
2247 = fold_build_pointer_plus (DR_BASE_ADDRESS (dr_b.dr), dr_b.offset);
2249 if (dump_enabled_p ())
2251 dump_printf_loc (MSG_NOTE, vect_location,
2252 "create runtime check for data references ");
2253 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_a.dr));
2254 dump_printf (MSG_NOTE, " and ");
2255 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_b.dr));
2256 dump_printf (MSG_NOTE, "\n");
2259 tree seg_a_min = addr_base_a;
2260 tree seg_a_max = fold_build_pointer_plus (addr_base_a, segment_length_a);
2261 /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
2262 bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
2263 [a, a+12) */
2264 if (tree_int_cst_compare (DR_STEP (dr_a.dr), size_zero_node) < 0)
2266 tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_a.dr)));
2267 seg_a_min = fold_build_pointer_plus (seg_a_max, unit_size);
2268 seg_a_max = fold_build_pointer_plus (addr_base_a, unit_size);
2271 tree seg_b_min = addr_base_b;
2272 tree seg_b_max = fold_build_pointer_plus (addr_base_b, segment_length_b);
2273 if (tree_int_cst_compare (DR_STEP (dr_b.dr), size_zero_node) < 0)
2275 tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_b.dr)));
2276 seg_b_min = fold_build_pointer_plus (seg_b_max, unit_size);
2277 seg_b_max = fold_build_pointer_plus (addr_base_b, unit_size);
2280 part_cond_expr =
2281 fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
2282 fold_build2 (LE_EXPR, boolean_type_node, seg_a_max, seg_b_min),
2283 fold_build2 (LE_EXPR, boolean_type_node, seg_b_max, seg_a_min));
2285 if (*cond_expr)
2286 *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2287 *cond_expr, part_cond_expr);
2288 else
2289 *cond_expr = part_cond_expr;
2292 if (dump_enabled_p ())
2293 dump_printf_loc (MSG_NOTE, vect_location,
2294 "created %u versioning for alias checks.\n",
2295 comp_alias_ddrs.length ());
2297 comp_alias_ddrs.release ();
2301 /* Function vect_loop_versioning.
2303 If the loop has data references that may or may not be aligned or/and
2304 has data reference relations whose independence was not proven then
2305 two versions of the loop need to be generated, one which is vectorized
2306 and one which isn't. A test is then generated to control which of the
2307 loops is executed. The test checks for the alignment of all of the
2308 data references that may or may not be aligned. An additional
2309 sequence of runtime tests is generated for each pairs of DDRs whose
2310 independence was not proven. The vectorized version of loop is
2311 executed only if both alias and alignment tests are passed.
2313 The test generated to check which version of loop is executed
2314 is modified to also check for profitability as indicated by the
2315 cost model initially.
2317 The versioning precondition(s) are placed in *COND_EXPR and
2318 *COND_EXPR_STMT_LIST. */
2320 void
2321 vect_loop_versioning (loop_vec_info loop_vinfo,
2322 unsigned int th, bool check_profitability)
2324 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2325 struct loop *scalar_loop = LOOP_VINFO_SCALAR_LOOP (loop_vinfo);
2326 basic_block condition_bb;
2327 gphi_iterator gsi;
2328 gimple_stmt_iterator cond_exp_gsi;
2329 basic_block merge_bb;
2330 basic_block new_exit_bb;
2331 edge new_exit_e, e;
2332 gphi *orig_phi, *new_phi;
2333 tree cond_expr = NULL_TREE;
2334 gimple_seq cond_expr_stmt_list = NULL;
2335 tree arg;
2336 unsigned prob = 4 * REG_BR_PROB_BASE / 5;
2337 gimple_seq gimplify_stmt_list = NULL;
2338 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2339 bool version_align = LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo);
2340 bool version_alias = LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo);
2342 if (check_profitability)
2344 cond_expr = fold_build2 (GT_EXPR, boolean_type_node, scalar_loop_iters,
2345 build_int_cst (TREE_TYPE (scalar_loop_iters), th));
2346 cond_expr = force_gimple_operand_1 (cond_expr, &cond_expr_stmt_list,
2347 is_gimple_condexpr, NULL_TREE);
2350 if (version_align)
2351 vect_create_cond_for_align_checks (loop_vinfo, &cond_expr,
2352 &cond_expr_stmt_list);
2354 if (version_alias)
2355 vect_create_cond_for_alias_checks (loop_vinfo, &cond_expr);
2357 cond_expr = force_gimple_operand_1 (cond_expr, &gimplify_stmt_list,
2358 is_gimple_condexpr, NULL_TREE);
2359 gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list);
2361 initialize_original_copy_tables ();
2362 if (scalar_loop)
2364 edge scalar_e;
2365 basic_block preheader, scalar_preheader;
2367 /* We don't want to scale SCALAR_LOOP's frequencies, we need to
2368 scale LOOP's frequencies instead. */
2369 loop_version (scalar_loop, cond_expr, &condition_bb,
2370 prob, REG_BR_PROB_BASE, REG_BR_PROB_BASE - prob, true);
2371 scale_loop_frequencies (loop, prob, REG_BR_PROB_BASE);
2372 /* CONDITION_BB was created above SCALAR_LOOP's preheader,
2373 while we need to move it above LOOP's preheader. */
2374 e = loop_preheader_edge (loop);
2375 scalar_e = loop_preheader_edge (scalar_loop);
2376 gcc_assert (empty_block_p (e->src)
2377 && single_pred_p (e->src));
2378 gcc_assert (empty_block_p (scalar_e->src)
2379 && single_pred_p (scalar_e->src));
2380 gcc_assert (single_pred_p (condition_bb));
2381 preheader = e->src;
2382 scalar_preheader = scalar_e->src;
2383 scalar_e = find_edge (condition_bb, scalar_preheader);
2384 e = single_pred_edge (preheader);
2385 redirect_edge_and_branch_force (single_pred_edge (condition_bb),
2386 scalar_preheader);
2387 redirect_edge_and_branch_force (scalar_e, preheader);
2388 redirect_edge_and_branch_force (e, condition_bb);
2389 set_immediate_dominator (CDI_DOMINATORS, condition_bb,
2390 single_pred (condition_bb));
2391 set_immediate_dominator (CDI_DOMINATORS, scalar_preheader,
2392 single_pred (scalar_preheader));
2393 set_immediate_dominator (CDI_DOMINATORS, preheader,
2394 condition_bb);
2396 else
2397 loop_version (loop, cond_expr, &condition_bb,
2398 prob, prob, REG_BR_PROB_BASE - prob, true);
2400 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
2401 && dump_enabled_p ())
2403 if (version_alias)
2404 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
2405 "loop versioned for vectorization because of "
2406 "possible aliasing\n");
2407 if (version_align)
2408 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
2409 "loop versioned for vectorization to enhance "
2410 "alignment\n");
2413 free_original_copy_tables ();
2415 /* Loop versioning violates an assumption we try to maintain during
2416 vectorization - that the loop exit block has a single predecessor.
2417 After versioning, the exit block of both loop versions is the same
2418 basic block (i.e. it has two predecessors). Just in order to simplify
2419 following transformations in the vectorizer, we fix this situation
2420 here by adding a new (empty) block on the exit-edge of the loop,
2421 with the proper loop-exit phis to maintain loop-closed-form.
2422 If loop versioning wasn't done from loop, but scalar_loop instead,
2423 merge_bb will have already just a single successor. */
2425 merge_bb = single_exit (loop)->dest;
2426 if (scalar_loop == NULL || EDGE_COUNT (merge_bb->preds) >= 2)
2428 gcc_assert (EDGE_COUNT (merge_bb->preds) >= 2);
2429 new_exit_bb = split_edge (single_exit (loop));
2430 new_exit_e = single_exit (loop);
2431 e = EDGE_SUCC (new_exit_bb, 0);
2433 for (gsi = gsi_start_phis (merge_bb); !gsi_end_p (gsi); gsi_next (&gsi))
2435 tree new_res;
2436 orig_phi = gsi.phi ();
2437 new_res = copy_ssa_name (PHI_RESULT (orig_phi));
2438 new_phi = create_phi_node (new_res, new_exit_bb);
2439 arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, e);
2440 add_phi_arg (new_phi, arg, new_exit_e,
2441 gimple_phi_arg_location_from_edge (orig_phi, e));
2442 adjust_phi_and_debug_stmts (orig_phi, e, PHI_RESULT (new_phi));
2446 /* End loop-exit-fixes after versioning. */
2448 if (cond_expr_stmt_list)
2450 cond_exp_gsi = gsi_last_bb (condition_bb);
2451 gsi_insert_seq_before (&cond_exp_gsi, cond_expr_stmt_list,
2452 GSI_SAME_STMT);
2454 update_ssa (TODO_update_ssa);