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