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