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