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