OSDN Git Service

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