OSDN Git Service

PR tree-optimization/36630
[pf3gnuchains/gcc-fork.git] / gcc / tree-vect-transform.c
1 /* Transformation Utilities for Loop Vectorization.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3    Contributed by Dorit Naishlos <dorit@il.ibm.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "cfgloop.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "params.h"
38 #include "recog.h"
39 #include "tree-data-ref.h"
40 #include "tree-chrec.h"
41 #include "tree-scalar-evolution.h"
42 #include "tree-vectorizer.h"
43 #include "langhooks.h"
44 #include "tree-pass.h"
45 #include "toplev.h"
46 #include "real.h"
47
48 /* Utility functions for the code transformation.  */
49 static bool vect_transform_stmt (gimple, gimple_stmt_iterator *, bool *,
50                                  slp_tree, slp_instance);
51 static tree vect_create_destination_var (tree, tree);
52 static tree vect_create_data_ref_ptr 
53   (gimple, struct loop*, tree, tree *, gimple *, bool, bool *);
54 static tree vect_create_addr_base_for_vector_ref 
55   (gimple, gimple_seq *, tree, struct loop *);
56 static tree vect_get_new_vect_var (tree, enum vect_var_kind, const char *);
57 static tree vect_get_vec_def_for_operand (tree, gimple, tree *);
58 static tree vect_init_vector (gimple, tree, tree, gimple_stmt_iterator *);
59 static void vect_finish_stmt_generation 
60   (gimple stmt, gimple vec_stmt, gimple_stmt_iterator *);
61 static bool vect_is_simple_cond (tree, loop_vec_info); 
62 static void vect_create_epilog_for_reduction 
63   (tree, gimple, int, enum tree_code, gimple);
64 static tree get_initial_def_for_reduction (gimple, tree, tree *);
65
66 /* Utility function dealing with loop peeling (not peeling itself).  */
67 static void vect_generate_tmps_on_preheader 
68   (loop_vec_info, tree *, tree *, tree *);
69 static tree vect_build_loop_niters (loop_vec_info);
70 static void vect_update_ivs_after_vectorizer (loop_vec_info, tree, edge); 
71 static tree vect_gen_niters_for_prolog_loop (loop_vec_info, tree);
72 static void vect_update_init_of_dr (struct data_reference *, tree niters);
73 static void vect_update_inits_of_drs (loop_vec_info, tree);
74 static int vect_min_worthwhile_factor (enum tree_code);
75
76
77 static int
78 cost_for_stmt (gimple stmt)
79 {
80   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
81
82   switch (STMT_VINFO_TYPE (stmt_info))
83   {
84   case load_vec_info_type:
85     return TARG_SCALAR_LOAD_COST;
86   case store_vec_info_type:
87     return TARG_SCALAR_STORE_COST;
88   case op_vec_info_type:
89   case condition_vec_info_type:
90   case assignment_vec_info_type:
91   case reduc_vec_info_type:
92   case induc_vec_info_type:
93   case type_promotion_vec_info_type:
94   case type_demotion_vec_info_type:
95   case type_conversion_vec_info_type:
96   case call_vec_info_type:
97     return TARG_SCALAR_STMT_COST;
98   case undef_vec_info_type:
99   default:
100     gcc_unreachable ();
101   }
102 }
103
104
105 /* Function vect_estimate_min_profitable_iters
106
107    Return the number of iterations required for the vector version of the
108    loop to be profitable relative to the cost of the scalar version of the
109    loop.
110
111    TODO: Take profile info into account before making vectorization
112    decisions, if available.  */
113
114 int
115 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo)
116 {
117   int i;
118   int min_profitable_iters;
119   int peel_iters_prologue;
120   int peel_iters_epilogue;
121   int vec_inside_cost = 0;
122   int vec_outside_cost = 0;
123   int scalar_single_iter_cost = 0;
124   int scalar_outside_cost = 0;
125   bool runtime_test = false;
126   int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
127   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
128   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
129   int nbbs = loop->num_nodes;
130   int byte_misalign = LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo);
131   int peel_guard_costs = 0;
132   int innerloop_iters = 0, factor;
133   VEC (slp_instance, heap) *slp_instances;
134   slp_instance instance;
135
136   /* Cost model disabled.  */
137   if (!flag_vect_cost_model)
138     {
139       if (vect_print_dump_info (REPORT_COST))
140         fprintf (vect_dump, "cost model disabled.");      
141       return 0;
142     }
143
144   /* If the number of iterations is unknown, or the
145      peeling-for-misalignment amount is unknown, we will have to generate
146      a runtime test to test the loop count against the threshold.    */
147   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
148       || (byte_misalign < 0))
149     runtime_test = true;
150
151   /* Requires loop versioning tests to handle misalignment.  */
152
153   if (VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo)))
154     {
155       /*  FIXME: Make cost depend on complexity of individual check.  */
156       vec_outside_cost +=
157         VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo));
158       if (vect_print_dump_info (REPORT_COST))
159         fprintf (vect_dump, "cost model: Adding cost of checks for loop "
160                  "versioning to treat misalignment.\n");
161     }
162
163   if (VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
164     {
165       /*  FIXME: Make cost depend on complexity of individual check.  */
166       vec_outside_cost +=
167         VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo));
168       if (vect_print_dump_info (REPORT_COST))
169         fprintf (vect_dump, "cost model: Adding cost of checks for loop "
170                  "versioning aliasing.\n");
171     }
172
173   if (VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
174       || VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
175     {
176       vec_outside_cost += TARG_COND_TAKEN_BRANCH_COST;
177     }
178
179   /* Count statements in scalar loop.  Using this as scalar cost for a single
180      iteration for now.
181
182      TODO: Add outer loop support.
183
184      TODO: Consider assigning different costs to different scalar
185      statements.  */
186
187   /* FORNOW.  */
188   if (loop->inner)
189     innerloop_iters = 50; /* FIXME */
190
191   for (i = 0; i < nbbs; i++)
192     {
193       gimple_stmt_iterator si;
194       basic_block bb = bbs[i];
195
196       if (bb->loop_father == loop->inner)
197         factor = innerloop_iters;
198       else
199         factor = 1;
200
201       for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
202         {
203           gimple stmt = gsi_stmt (si);
204           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
205           /* Skip stmts that are not vectorized inside the loop.  */
206           if (!STMT_VINFO_RELEVANT_P (stmt_info)
207               && (!STMT_VINFO_LIVE_P (stmt_info)
208                   || STMT_VINFO_DEF_TYPE (stmt_info) != vect_reduction_def))
209             continue;
210           scalar_single_iter_cost += cost_for_stmt (stmt) * factor;
211           vec_inside_cost += STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info) * factor;
212           /* FIXME: for stmts in the inner-loop in outer-loop vectorization,
213              some of the "outside" costs are generated inside the outer-loop.  */
214           vec_outside_cost += STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info);
215         }
216     }
217
218   /* Add additional cost for the peeled instructions in prologue and epilogue
219      loop.
220
221      FORNOW: If we don't know the value of peel_iters for prologue or epilogue
222      at compile-time - we assume it's vf/2 (the worst would be vf-1).
223
224      TODO: Build an expression that represents peel_iters for prologue and
225      epilogue to be used in a run-time test.  */
226
227   if (byte_misalign < 0)
228     {
229       peel_iters_prologue = vf/2;
230       if (vect_print_dump_info (REPORT_COST))
231         fprintf (vect_dump, "cost model: "
232                  "prologue peel iters set to vf/2.");
233
234       /* If peeling for alignment is unknown, loop bound of main loop becomes
235          unknown.  */
236       peel_iters_epilogue = vf/2;
237       if (vect_print_dump_info (REPORT_COST))
238         fprintf (vect_dump, "cost model: "
239                  "epilogue peel iters set to vf/2 because "
240                  "peeling for alignment is unknown .");
241
242       /* If peeled iterations are unknown, count a taken branch and a not taken
243          branch per peeled loop. Even if scalar loop iterations are known, 
244          vector iterations are not known since peeled prologue iterations are
245          not known. Hence guards remain the same.  */
246       peel_guard_costs +=  2 * (TARG_COND_TAKEN_BRANCH_COST
247                                + TARG_COND_NOT_TAKEN_BRANCH_COST);
248
249     }
250   else 
251     {
252       if (byte_misalign)
253         {
254           struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
255           int element_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
256           tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr)));
257           int nelements = TYPE_VECTOR_SUBPARTS (vectype);
258
259           peel_iters_prologue = nelements - (byte_misalign / element_size);
260         }
261       else
262         peel_iters_prologue = 0;
263
264       if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
265         {
266           peel_iters_epilogue = vf/2;
267           if (vect_print_dump_info (REPORT_COST))
268             fprintf (vect_dump, "cost model: "
269                      "epilogue peel iters set to vf/2 because "
270                      "loop iterations are unknown .");
271
272           /* If peeled iterations are known but number of scalar loop
273              iterations are unknown, count a taken branch per peeled loop.  */
274           peel_guard_costs +=  2 * TARG_COND_TAKEN_BRANCH_COST;
275
276         }
277       else      
278         {
279           int niters = LOOP_VINFO_INT_NITERS (loop_vinfo);
280           peel_iters_prologue = niters < peel_iters_prologue ? 
281                                         niters : peel_iters_prologue;
282           peel_iters_epilogue = (niters - peel_iters_prologue) % vf;
283         }
284     }
285
286   vec_outside_cost += (peel_iters_prologue * scalar_single_iter_cost)
287                       + (peel_iters_epilogue * scalar_single_iter_cost)
288                       + peel_guard_costs;
289
290   /* FORNOW: The scalar outside cost is incremented in one of the
291      following ways:
292
293      1. The vectorizer checks for alignment and aliasing and generates
294      a condition that allows dynamic vectorization.  A cost model
295      check is ANDED with the versioning condition.  Hence scalar code
296      path now has the added cost of the versioning check.
297
298        if (cost > th & versioning_check)
299          jmp to vector code
300
301      Hence run-time scalar is incremented by not-taken branch cost.
302
303      2. The vectorizer then checks if a prologue is required.  If the
304      cost model check was not done before during versioning, it has to
305      be done before the prologue check.
306
307        if (cost <= th)
308          prologue = scalar_iters
309        if (prologue == 0)
310          jmp to vector code
311        else
312          execute prologue
313        if (prologue == num_iters)
314          go to exit
315
316      Hence the run-time scalar cost is incremented by a taken branch,
317      plus a not-taken branch, plus a taken branch cost.
318
319      3. The vectorizer then checks if an epilogue is required.  If the
320      cost model check was not done before during prologue check, it
321      has to be done with the epilogue check.
322
323        if (prologue == 0)
324          jmp to vector code
325        else
326          execute prologue
327        if (prologue == num_iters)
328          go to exit
329        vector code:
330          if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
331            jmp to epilogue
332
333      Hence the run-time scalar cost should be incremented by 2 taken
334      branches.
335
336      TODO: The back end may reorder the BBS's differently and reverse
337      conditions/branch directions.  Change the estimates below to
338      something more reasonable.  */
339
340   if (runtime_test)
341     {
342       /* Cost model check occurs at versioning.  */
343       if (VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
344           || VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
345         scalar_outside_cost += TARG_COND_NOT_TAKEN_BRANCH_COST;
346       else
347         {
348           /* Cost model occurs at prologue generation.  */
349           if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
350             scalar_outside_cost += 2 * TARG_COND_TAKEN_BRANCH_COST
351               + TARG_COND_NOT_TAKEN_BRANCH_COST;
352           /* Cost model check occurs at epilogue generation.  */
353           else
354             scalar_outside_cost += 2 * TARG_COND_TAKEN_BRANCH_COST;
355         }
356     }
357
358   /* Add SLP costs.  */
359   slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
360   for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); i++)
361     {
362       vec_outside_cost += SLP_INSTANCE_OUTSIDE_OF_LOOP_COST (instance);
363       vec_inside_cost += SLP_INSTANCE_INSIDE_OF_LOOP_COST (instance);
364     }
365
366   /* Calculate number of iterations required to make the vector version 
367      profitable, relative to the loop bodies only. The following condition
368      must hold true: 
369      SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
370      where
371      SIC = scalar iteration cost, VIC = vector iteration cost,
372      VOC = vector outside cost, VF = vectorization factor,
373      PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
374      SOC = scalar outside cost for run time cost model check.  */
375
376   if ((scalar_single_iter_cost * vf) > vec_inside_cost)
377     {
378       if (vec_outside_cost <= 0)
379         min_profitable_iters = 1;
380       else
381         {
382           min_profitable_iters = ((vec_outside_cost - scalar_outside_cost) * vf
383                                   - vec_inside_cost * peel_iters_prologue
384                                   - vec_inside_cost * peel_iters_epilogue)
385                                  / ((scalar_single_iter_cost * vf)
386                                     - vec_inside_cost);
387
388           if ((scalar_single_iter_cost * vf * min_profitable_iters)
389               <= ((vec_inside_cost * min_profitable_iters)
390                   + ((vec_outside_cost - scalar_outside_cost) * vf)))
391             min_profitable_iters++;
392         }
393     }
394   /* vector version will never be profitable.  */
395   else
396     {
397       if (vect_print_dump_info (REPORT_COST))
398         fprintf (vect_dump, "cost model: vector iteration cost = %d "
399                  "is divisible by scalar iteration cost = %d by a factor "
400                  "greater than or equal to the vectorization factor = %d .",
401                  vec_inside_cost, scalar_single_iter_cost, vf);
402       return -1;
403     }
404
405   if (vect_print_dump_info (REPORT_COST))
406     {
407       fprintf (vect_dump, "Cost model analysis: \n");
408       fprintf (vect_dump, "  Vector inside of loop cost: %d\n",
409                vec_inside_cost);
410       fprintf (vect_dump, "  Vector outside of loop cost: %d\n",
411                vec_outside_cost);
412       fprintf (vect_dump, "  Scalar iteration cost: %d\n",
413                scalar_single_iter_cost);
414       fprintf (vect_dump, "  Scalar outside cost: %d\n", scalar_outside_cost);
415       fprintf (vect_dump, "  prologue iterations: %d\n",
416                peel_iters_prologue);
417       fprintf (vect_dump, "  epilogue iterations: %d\n",
418                peel_iters_epilogue);
419       fprintf (vect_dump, "  Calculated minimum iters for profitability: %d\n",
420                min_profitable_iters);
421     }
422
423   min_profitable_iters = 
424         min_profitable_iters < vf ? vf : min_profitable_iters;
425
426   /* Because the condition we create is:
427      if (niters <= min_profitable_iters)
428        then skip the vectorized loop.  */
429   min_profitable_iters--;
430
431   if (vect_print_dump_info (REPORT_COST))
432     fprintf (vect_dump, "  Profitability threshold = %d\n",
433              min_profitable_iters);
434     
435   return min_profitable_iters;
436 }
437
438
439 /* TODO: Close dependency between vect_model_*_cost and vectorizable_* 
440    functions. Design better to avoid maintenance issues.  */
441     
442 /* Function vect_model_reduction_cost.  
443
444    Models cost for a reduction operation, including the vector ops 
445    generated within the strip-mine loop, the initial definition before
446    the loop, and the epilogue code that must be generated.  */
447
448 static bool 
449 vect_model_reduction_cost (stmt_vec_info stmt_info, enum tree_code reduc_code,
450                            int ncopies)
451 {
452   int outer_cost = 0;
453   enum tree_code code;
454   optab optab;
455   tree vectype;
456   gimple stmt, orig_stmt;
457   tree reduction_op;
458   enum machine_mode mode;
459   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
460   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
461
462
463   /* Cost of reduction op inside loop.  */
464   STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info) += ncopies * TARG_VEC_STMT_COST;
465
466   stmt = STMT_VINFO_STMT (stmt_info);
467
468   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
469     {
470     case GIMPLE_SINGLE_RHS:
471       gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt)) == ternary_op);
472       reduction_op = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
473       break;
474     case GIMPLE_UNARY_RHS:
475       reduction_op = gimple_assign_rhs1 (stmt);
476       break;
477     case GIMPLE_BINARY_RHS:
478       reduction_op = gimple_assign_rhs2 (stmt);
479       break;
480     default:
481       gcc_unreachable ();
482     }
483
484   vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
485   if (!vectype)
486     {
487       if (vect_print_dump_info (REPORT_COST))
488         {
489           fprintf (vect_dump, "unsupported data-type ");
490           print_generic_expr (vect_dump, TREE_TYPE (reduction_op), TDF_SLIM);
491         }
492       return false;
493    }
494   
495   mode = TYPE_MODE (vectype);
496   orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
497
498   if (!orig_stmt) 
499     orig_stmt = STMT_VINFO_STMT (stmt_info);
500
501   code = gimple_assign_rhs_code (orig_stmt);
502
503   /* Add in cost for initial definition.  */
504   outer_cost += TARG_SCALAR_TO_VEC_COST;
505
506   /* Determine cost of epilogue code.
507
508      We have a reduction operator that will reduce the vector in one statement.
509      Also requires scalar extract.  */
510
511   if (!nested_in_vect_loop_p (loop, orig_stmt))
512     {
513       if (reduc_code < NUM_TREE_CODES) 
514         outer_cost += TARG_VEC_STMT_COST + TARG_VEC_TO_SCALAR_COST;
515       else 
516         {
517           int vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
518           tree bitsize =
519             TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt)));
520           int element_bitsize = tree_low_cst (bitsize, 1);
521           int nelements = vec_size_in_bits / element_bitsize;
522
523           optab = optab_for_tree_code (code, vectype, optab_default);
524
525           /* We have a whole vector shift available.  */
526           if (VECTOR_MODE_P (mode)
527               && optab_handler (optab, mode)->insn_code != CODE_FOR_nothing
528               && optab_handler (vec_shr_optab, mode)->insn_code != CODE_FOR_nothing)
529             /* Final reduction via vector shifts and the reduction operator. Also
530                requires scalar extract.  */
531             outer_cost += ((exact_log2(nelements) * 2) * TARG_VEC_STMT_COST
532                                 + TARG_VEC_TO_SCALAR_COST); 
533           else
534             /* Use extracts and reduction op for final reduction.  For N elements,
535                we have N extracts and N-1 reduction ops.  */
536             outer_cost += ((nelements + nelements - 1) * TARG_VEC_STMT_COST);
537         }
538     }
539
540   STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info) = outer_cost;
541
542   if (vect_print_dump_info (REPORT_COST))
543     fprintf (vect_dump, "vect_model_reduction_cost: inside_cost = %d, "
544              "outside_cost = %d .", STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info),
545              STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info));
546
547   return true;
548 }
549
550
551 /* Function vect_model_induction_cost.
552
553    Models cost for induction operations.  */
554
555 static void
556 vect_model_induction_cost (stmt_vec_info stmt_info, int ncopies)
557 {
558   /* loop cost for vec_loop.  */
559   STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info) = ncopies * TARG_VEC_STMT_COST;
560   /* prologue cost for vec_init and vec_step.  */
561   STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info) = 2 * TARG_SCALAR_TO_VEC_COST;
562   
563   if (vect_print_dump_info (REPORT_COST))
564     fprintf (vect_dump, "vect_model_induction_cost: inside_cost = %d, "
565              "outside_cost = %d .", STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info),
566              STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info));
567 }
568
569
570 /* Function vect_model_simple_cost.  
571
572    Models cost for simple operations, i.e. those that only emit ncopies of a 
573    single op.  Right now, this does not account for multiple insns that could
574    be generated for the single vector op.  We will handle that shortly.  */
575
576 void
577 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies, 
578                         enum vect_def_type *dt, slp_tree slp_node)
579 {
580   int i;
581   int inside_cost = 0, outside_cost = 0;
582
583   /* The SLP costs were already calculated during SLP tree build.  */
584   if (PURE_SLP_STMT (stmt_info))
585     return;
586
587   inside_cost = ncopies * TARG_VEC_STMT_COST;
588
589   /* FORNOW: Assuming maximum 2 args per stmts.  */
590   for (i = 0; i < 2; i++)
591     {
592       if (dt[i] == vect_constant_def || dt[i] == vect_invariant_def)
593         outside_cost += TARG_SCALAR_TO_VEC_COST; 
594     }
595   
596   if (vect_print_dump_info (REPORT_COST))
597     fprintf (vect_dump, "vect_model_simple_cost: inside_cost = %d, "
598              "outside_cost = %d .", inside_cost, outside_cost);
599
600   /* Set the costs either in STMT_INFO or SLP_NODE (if exists).  */
601   stmt_vinfo_set_inside_of_loop_cost (stmt_info, slp_node, inside_cost);
602   stmt_vinfo_set_outside_of_loop_cost (stmt_info, slp_node, outside_cost);
603 }
604
605
606 /* Function vect_cost_strided_group_size 
607  
608    For strided load or store, return the group_size only if it is the first
609    load or store of a group, else return 1.  This ensures that group size is
610    only returned once per group.  */
611
612 static int
613 vect_cost_strided_group_size (stmt_vec_info stmt_info)
614 {
615   gimple first_stmt = DR_GROUP_FIRST_DR (stmt_info);
616
617   if (first_stmt == STMT_VINFO_STMT (stmt_info))
618     return DR_GROUP_SIZE (stmt_info);
619
620   return 1;
621 }
622
623
624 /* Function vect_model_store_cost
625
626    Models cost for stores.  In the case of strided accesses, one access
627    has the overhead of the strided access attributed to it.  */
628
629 void
630 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies, 
631                        enum vect_def_type dt, slp_tree slp_node)
632 {
633   int group_size;
634   int inside_cost = 0, outside_cost = 0;
635
636   /* The SLP costs were already calculated during SLP tree build.  */
637   if (PURE_SLP_STMT (stmt_info))
638     return;
639
640   if (dt == vect_constant_def || dt == vect_invariant_def)
641     outside_cost = TARG_SCALAR_TO_VEC_COST;
642
643   /* Strided access?  */
644   if (DR_GROUP_FIRST_DR (stmt_info) && !slp_node) 
645     group_size = vect_cost_strided_group_size (stmt_info);
646   /* Not a strided access.  */
647   else
648     group_size = 1;
649
650   /* Is this an access in a group of stores, which provide strided access?  
651      If so, add in the cost of the permutes.  */
652   if (group_size > 1) 
653     {
654       /* Uses a high and low interleave operation for each needed permute.  */
655       inside_cost = ncopies * exact_log2(group_size) * group_size 
656              * TARG_VEC_STMT_COST;
657
658       if (vect_print_dump_info (REPORT_COST))
659         fprintf (vect_dump, "vect_model_store_cost: strided group_size = %d .",
660                  group_size);
661
662     }
663
664   /* Costs of the stores.  */
665   inside_cost += ncopies * TARG_VEC_STORE_COST;
666
667   if (vect_print_dump_info (REPORT_COST))
668     fprintf (vect_dump, "vect_model_store_cost: inside_cost = %d, "
669              "outside_cost = %d .", inside_cost, outside_cost);
670
671   /* Set the costs either in STMT_INFO or SLP_NODE (if exists).  */
672   stmt_vinfo_set_inside_of_loop_cost (stmt_info, slp_node, inside_cost);
673   stmt_vinfo_set_outside_of_loop_cost (stmt_info, slp_node, outside_cost);
674 }
675
676
677 /* Function vect_model_load_cost
678
679    Models cost for loads.  In the case of strided accesses, the last access
680    has the overhead of the strided access attributed to it.  Since unaligned
681    accesses are supported for loads, we also account for the costs of the 
682    access scheme chosen.  */
683
684 void
685 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies, slp_tree slp_node)
686                  
687 {
688   int group_size;
689   int alignment_support_cheme;
690   gimple first_stmt;
691   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
692   int inside_cost = 0, outside_cost = 0;
693
694   /* The SLP costs were already calculated during SLP tree build.  */
695   if (PURE_SLP_STMT (stmt_info))
696     return;
697
698   /* Strided accesses?  */
699   first_stmt = DR_GROUP_FIRST_DR (stmt_info);
700   if (first_stmt && !slp_node)
701     {
702       group_size = vect_cost_strided_group_size (stmt_info);
703       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
704     }
705   /* Not a strided access.  */
706   else
707     {
708       group_size = 1;
709       first_dr = dr;
710     }
711
712   alignment_support_cheme = vect_supportable_dr_alignment (first_dr);
713
714   /* Is this an access in a group of loads providing strided access?  
715      If so, add in the cost of the permutes.  */
716   if (group_size > 1) 
717     {
718       /* Uses an even and odd extract operations for each needed permute.  */
719       inside_cost = ncopies * exact_log2(group_size) * group_size
720         * TARG_VEC_STMT_COST;
721
722       if (vect_print_dump_info (REPORT_COST))
723         fprintf (vect_dump, "vect_model_load_cost: strided group_size = %d .",
724                  group_size);
725
726     }
727
728   /* The loads themselves.  */
729   switch (alignment_support_cheme)
730     {
731     case dr_aligned:
732       {
733         inside_cost += ncopies * TARG_VEC_LOAD_COST;
734
735         if (vect_print_dump_info (REPORT_COST))
736           fprintf (vect_dump, "vect_model_load_cost: aligned.");
737
738         break;
739       }
740     case dr_unaligned_supported:
741       {
742         /* Here, we assign an additional cost for the unaligned load.  */
743         inside_cost += ncopies * TARG_VEC_UNALIGNED_LOAD_COST;
744
745         if (vect_print_dump_info (REPORT_COST))
746           fprintf (vect_dump, "vect_model_load_cost: unaligned supported by "
747                    "hardware.");
748
749         break;
750       }
751     case dr_explicit_realign:
752       {
753         inside_cost += ncopies * (2*TARG_VEC_LOAD_COST + TARG_VEC_STMT_COST);
754
755         /* FIXME: If the misalignment remains fixed across the iterations of
756            the containing loop, the following cost should be added to the
757            outside costs.  */
758         if (targetm.vectorize.builtin_mask_for_load)
759           inside_cost += TARG_VEC_STMT_COST;
760
761         break;
762       }
763     case dr_explicit_realign_optimized:
764       {
765         if (vect_print_dump_info (REPORT_COST))
766           fprintf (vect_dump, "vect_model_load_cost: unaligned software "
767                    "pipelined.");
768
769         /* Unaligned software pipeline has a load of an address, an initial
770            load, and possibly a mask operation to "prime" the loop. However,
771            if this is an access in a group of loads, which provide strided
772            access, then the above cost should only be considered for one
773            access in the group. Inside the loop, there is a load op
774            and a realignment op.  */
775
776         if ((!DR_GROUP_FIRST_DR (stmt_info)) || group_size > 1 || slp_node)
777           {
778             outside_cost = 2*TARG_VEC_STMT_COST;
779             if (targetm.vectorize.builtin_mask_for_load)
780               outside_cost += TARG_VEC_STMT_COST;
781           }
782
783         inside_cost += ncopies * (TARG_VEC_LOAD_COST + TARG_VEC_STMT_COST);
784
785         break;
786       }
787
788     default:
789       gcc_unreachable ();
790     }
791   
792   if (vect_print_dump_info (REPORT_COST))
793     fprintf (vect_dump, "vect_model_load_cost: inside_cost = %d, "
794              "outside_cost = %d .", inside_cost, outside_cost);
795
796   /* Set the costs either in STMT_INFO or SLP_NODE (if exists).  */
797   stmt_vinfo_set_inside_of_loop_cost (stmt_info, slp_node, inside_cost);
798   stmt_vinfo_set_outside_of_loop_cost (stmt_info, slp_node, outside_cost);
799 }
800
801
802 /* Function vect_get_new_vect_var.
803
804    Returns a name for a new variable. The current naming scheme appends the 
805    prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to 
806    the name of vectorizer generated variables, and appends that to NAME if 
807    provided.  */
808
809 static tree
810 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
811 {
812   const char *prefix;
813   tree new_vect_var;
814
815   switch (var_kind)
816   {
817   case vect_simple_var:
818     prefix = "vect_";
819     break;
820   case vect_scalar_var:
821     prefix = "stmp_";
822     break;
823   case vect_pointer_var:
824     prefix = "vect_p";
825     break;
826   default:
827     gcc_unreachable ();
828   }
829
830   if (name)
831     {
832       char* tmp = concat (prefix, name, NULL);
833       new_vect_var = create_tmp_var (type, tmp);
834       free (tmp);
835     }
836   else
837     new_vect_var = create_tmp_var (type, prefix);
838
839   /* Mark vector typed variable as a gimple register variable.  */
840   if (TREE_CODE (type) == VECTOR_TYPE)
841     DECL_GIMPLE_REG_P (new_vect_var) = true;
842
843   return new_vect_var;
844 }
845
846
847 /* Function vect_create_addr_base_for_vector_ref.
848
849    Create an expression that computes the address of the first memory location
850    that will be accessed for a data reference.
851
852    Input:
853    STMT: The statement containing the data reference.
854    NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
855    OFFSET: Optional. If supplied, it is be added to the initial address.
856    LOOP:    Specify relative to which loop-nest should the address be computed.
857             For example, when the dataref is in an inner-loop nested in an
858             outer-loop that is now being vectorized, LOOP can be either the
859             outer-loop, or the inner-loop. The first memory location accessed
860             by the following dataref ('in' points to short):
861
862                 for (i=0; i<N; i++)
863                    for (j=0; j<M; j++)
864                      s += in[i+j]
865
866             is as follows:
867             if LOOP=i_loop:     &in             (relative to i_loop)
868             if LOOP=j_loop:     &in+i*2B        (relative to j_loop)
869
870    Output:
871    1. Return an SSA_NAME whose value is the address of the memory location of 
872       the first vector of the data reference.
873    2. If new_stmt_list is not NULL_TREE after return then the caller must insert
874       these statement(s) which define the returned SSA_NAME.
875
876    FORNOW: We are only handling array accesses with step 1.  */
877
878 static tree
879 vect_create_addr_base_for_vector_ref (gimple stmt,
880                                       gimple_seq *new_stmt_list,
881                                       tree offset,
882                                       struct loop *loop)
883 {
884   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
885   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
886   struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
887   tree data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
888   tree base_name;
889   tree data_ref_base_var;
890   tree vec_stmt;
891   tree addr_base, addr_expr;
892   tree dest;
893   gimple_seq seq = NULL;
894   tree base_offset = unshare_expr (DR_OFFSET (dr));
895   tree init = unshare_expr (DR_INIT (dr));
896   tree vect_ptr_type, addr_expr2;
897   tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
898
899   gcc_assert (loop);
900   if (loop != containing_loop)
901     {
902       loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
903       struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
904
905       gcc_assert (nested_in_vect_loop_p (loop, stmt));
906
907       data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
908       base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
909       init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
910     }
911
912   /* Create data_ref_base */
913   base_name = build_fold_indirect_ref (data_ref_base);
914   data_ref_base_var = create_tmp_var (TREE_TYPE (data_ref_base), "batmp");
915   add_referenced_var (data_ref_base_var);
916   data_ref_base = force_gimple_operand (data_ref_base, &seq, true,
917                                         data_ref_base_var);
918   gimple_seq_add_seq (new_stmt_list, seq);
919
920   /* Create base_offset */
921   base_offset = size_binop (PLUS_EXPR, base_offset, init);
922   base_offset = fold_convert (sizetype, base_offset);
923   dest = create_tmp_var (TREE_TYPE (base_offset), "base_off");
924   add_referenced_var (dest);
925   base_offset = force_gimple_operand (base_offset, &seq, true, dest);
926   gimple_seq_add_seq (new_stmt_list, seq);
927
928   if (offset)
929     {
930       tree tmp = create_tmp_var (sizetype, "offset");
931
932       add_referenced_var (tmp);
933       offset = fold_build2 (MULT_EXPR, TREE_TYPE (offset), offset, step);
934       base_offset = fold_build2 (PLUS_EXPR, TREE_TYPE (base_offset),
935                                  base_offset, offset);
936       base_offset = force_gimple_operand (base_offset, &seq, false, tmp);
937       gimple_seq_add_seq (new_stmt_list, seq);
938     }
939
940   /* base + base_offset */
941   addr_base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (data_ref_base), 
942                            data_ref_base, base_offset);
943
944   vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
945
946   /* addr_expr = addr_base */
947   addr_expr = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var,
948                                      get_name (base_name));
949   add_referenced_var (addr_expr);
950   vec_stmt = fold_convert (vect_ptr_type, addr_base);
951   addr_expr2 = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var,
952                                      get_name (base_name));
953   add_referenced_var (addr_expr2);
954   vec_stmt = force_gimple_operand (vec_stmt, &seq, false, addr_expr2);
955   gimple_seq_add_seq (new_stmt_list, seq);
956
957   if (vect_print_dump_info (REPORT_DETAILS))
958     {
959       fprintf (vect_dump, "created ");
960       print_generic_expr (vect_dump, vec_stmt, TDF_SLIM);
961     }
962   return vec_stmt;
963 }
964
965
966 /* Function vect_create_data_ref_ptr.
967
968    Create a new pointer to vector type (vp), that points to the first location
969    accessed in the loop by STMT, along with the def-use update chain to 
970    appropriately advance the pointer through the loop iterations. Also set
971    aliasing information for the pointer.  This vector pointer is used by the
972    callers to this function to create a memory reference expression for vector
973    load/store access.
974
975    Input:
976    1. STMT: a stmt that references memory. Expected to be of the form
977          GIMPLE_ASSIGN <name, data-ref> or
978          GIMPLE_ASSIGN <data-ref, name>.
979    2. AT_LOOP: the loop where the vector memref is to be created.
980    3. OFFSET (optional): an offset to be added to the initial address accessed
981         by the data-ref in STMT.
982    4. ONLY_INIT: indicate if vp is to be updated in the loop, or remain
983         pointing to the initial address.
984
985    Output:
986    1. Declare a new ptr to vector_type, and have it point to the base of the
987       data reference (initial addressed accessed by the data reference).
988       For example, for vector of type V8HI, the following code is generated:
989
990       v8hi *vp;
991       vp = (v8hi *)initial_address;
992
993       if OFFSET is not supplied:
994          initial_address = &a[init];
995       if OFFSET is supplied:
996          initial_address = &a[init + OFFSET];
997
998       Return the initial_address in INITIAL_ADDRESS.
999
1000    2. If ONLY_INIT is true, just return the initial pointer.  Otherwise, also
1001       update the pointer in each iteration of the loop.  
1002
1003       Return the increment stmt that updates the pointer in PTR_INCR.
1004
1005    3. Set INV_P to true if the access pattern of the data reference in the 
1006       vectorized loop is invariant. Set it to false otherwise.
1007
1008    4. Return the pointer.  */
1009
1010 static tree
1011 vect_create_data_ref_ptr (gimple stmt, struct loop *at_loop,
1012                           tree offset, tree *initial_address, gimple *ptr_incr,
1013                           bool only_init, bool *inv_p)
1014 {
1015   tree base_name;
1016   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1017   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1018   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1019   bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
1020   struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
1021   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1022   tree vect_ptr_type;
1023   tree vect_ptr;
1024   tree tag;
1025   tree new_temp;
1026   gimple vec_stmt;
1027   gimple_seq new_stmt_list = NULL;
1028   edge pe;
1029   basic_block new_bb;
1030   tree vect_ptr_init;
1031   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1032   tree vptr;
1033   gimple_stmt_iterator incr_gsi;
1034   bool insert_after;
1035   tree indx_before_incr, indx_after_incr;
1036   gimple incr;
1037   tree step;
1038
1039   /* Check the step (evolution) of the load in LOOP, and record
1040      whether it's invariant.  */
1041   if (nested_in_vect_loop)
1042     step = STMT_VINFO_DR_STEP (stmt_info);
1043   else
1044     step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
1045     
1046   if (tree_int_cst_compare (step, size_zero_node) == 0)
1047     *inv_p = true;
1048   else
1049     *inv_p = false;
1050
1051   /* Create an expression for the first address accessed by this load
1052      in LOOP.  */ 
1053   base_name = build_fold_indirect_ref (unshare_expr (DR_BASE_ADDRESS (dr)));
1054
1055   if (vect_print_dump_info (REPORT_DETAILS))
1056     {
1057       tree data_ref_base = base_name;
1058       fprintf (vect_dump, "create vector-pointer variable to type: ");
1059       print_generic_expr (vect_dump, vectype, TDF_SLIM);
1060       if (TREE_CODE (data_ref_base) == VAR_DECL)
1061         fprintf (vect_dump, "  vectorizing a one dimensional array ref: ");
1062       else if (TREE_CODE (data_ref_base) == ARRAY_REF)
1063         fprintf (vect_dump, "  vectorizing a multidimensional array ref: ");
1064       else if (TREE_CODE (data_ref_base) == COMPONENT_REF)
1065         fprintf (vect_dump, "  vectorizing a record based array ref: ");
1066       else if (TREE_CODE (data_ref_base) == SSA_NAME)
1067         fprintf (vect_dump, "  vectorizing a pointer ref: ");
1068       print_generic_expr (vect_dump, base_name, TDF_SLIM);
1069     }
1070
1071   /** (1) Create the new vector-pointer variable:  **/
1072   vect_ptr_type = build_pointer_type (vectype);
1073
1074   vect_ptr = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var,
1075                                     get_name (base_name));
1076   add_referenced_var (vect_ptr);
1077
1078   /** (2) Add aliasing information to the new vector-pointer:
1079           (The points-to info (DR_PTR_INFO) may be defined later.)  **/
1080   
1081   tag = DR_SYMBOL_TAG (dr);
1082   gcc_assert (tag);
1083
1084   /* If tag is a variable (and NOT_A_TAG) than a new symbol memory
1085      tag must be created with tag added to its may alias list.  */
1086   if (!MTAG_P (tag))
1087     new_type_alias (vect_ptr, tag, DR_REF (dr));
1088   else
1089     set_symbol_mem_tag (vect_ptr, tag);
1090
1091   /** Note: If the dataref is in an inner-loop nested in LOOP, and we are
1092       vectorizing LOOP (i.e. outer-loop vectorization), we need to create two
1093       def-use update cycles for the pointer: One relative to the outer-loop
1094       (LOOP), which is what steps (3) and (4) below do. The other is relative
1095       to the inner-loop (which is the inner-most loop containing the dataref),
1096       and this is done be step (5) below. 
1097
1098       When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
1099       inner-most loop, and so steps (3),(4) work the same, and step (5) is
1100       redundant.  Steps (3),(4) create the following:
1101
1102         vp0 = &base_addr;
1103         LOOP:   vp1 = phi(vp0,vp2)
1104                 ...  
1105                 ...
1106                 vp2 = vp1 + step
1107                 goto LOOP
1108                         
1109       If there is an inner-loop nested in loop, then step (5) will also be
1110       applied, and an additional update in the inner-loop will be created:
1111
1112         vp0 = &base_addr;
1113         LOOP:   vp1 = phi(vp0,vp2)
1114                 ...
1115         inner:     vp3 = phi(vp1,vp4)
1116                    vp4 = vp3 + inner_step
1117                    if () goto inner
1118                 ...
1119                 vp2 = vp1 + step
1120                 if () goto LOOP   */
1121
1122   /** (3) Calculate the initial address the vector-pointer, and set
1123           the vector-pointer to point to it before the loop:  **/
1124
1125   /* Create: (&(base[init_val+offset]) in the loop preheader.  */
1126
1127   new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
1128                                                    offset, loop);
1129   pe = loop_preheader_edge (loop);
1130   if (new_stmt_list)
1131     {
1132       new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
1133       gcc_assert (!new_bb);
1134     }
1135
1136   *initial_address = new_temp;
1137
1138   /* Create: p = (vectype *) initial_base  */
1139   vec_stmt = gimple_build_assign (vect_ptr,
1140                                   fold_convert (vect_ptr_type, new_temp));
1141   vect_ptr_init = make_ssa_name (vect_ptr, vec_stmt);
1142   gimple_assign_set_lhs (vec_stmt, vect_ptr_init);
1143   new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
1144   gcc_assert (!new_bb);
1145
1146
1147   /** (4) Handle the updating of the vector-pointer inside the loop.
1148           This is needed when ONLY_INIT is false, and also when AT_LOOP
1149           is the inner-loop nested in LOOP (during outer-loop vectorization).
1150    **/
1151
1152   if (only_init && at_loop == loop) /* No update in loop is required.  */
1153     {
1154       /* Copy the points-to information if it exists. */
1155       if (DR_PTR_INFO (dr))
1156         duplicate_ssa_name_ptr_info (vect_ptr_init, DR_PTR_INFO (dr));
1157       vptr = vect_ptr_init;
1158     }
1159   else
1160     {
1161       /* The step of the vector pointer is the Vector Size.  */
1162       tree step = TYPE_SIZE_UNIT (vectype);
1163       /* One exception to the above is when the scalar step of the load in 
1164          LOOP is zero. In this case the step here is also zero.  */
1165       if (*inv_p)
1166         step = size_zero_node;
1167
1168       standard_iv_increment_position (loop, &incr_gsi, &insert_after);
1169
1170       create_iv (vect_ptr_init,
1171                  fold_convert (vect_ptr_type, step),
1172                  NULL_TREE, loop, &incr_gsi, insert_after,
1173                  &indx_before_incr, &indx_after_incr);
1174       incr = gsi_stmt (incr_gsi);
1175       set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
1176
1177       /* Copy the points-to information if it exists. */
1178       if (DR_PTR_INFO (dr))
1179         {
1180           duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
1181           duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
1182         }
1183       merge_alias_info (vect_ptr_init, indx_before_incr);
1184       merge_alias_info (vect_ptr_init, indx_after_incr);
1185       if (ptr_incr)
1186         *ptr_incr = incr;
1187
1188       vptr = indx_before_incr;
1189     }
1190
1191   if (!nested_in_vect_loop || only_init)
1192     return vptr;
1193
1194
1195   /** (5) Handle the updating of the vector-pointer inside the inner-loop
1196           nested in LOOP, if exists: **/
1197
1198   gcc_assert (nested_in_vect_loop);
1199   if (!only_init)
1200     {
1201       standard_iv_increment_position (containing_loop, &incr_gsi,
1202                                       &insert_after);
1203       create_iv (vptr, fold_convert (vect_ptr_type, DR_STEP (dr)), NULL_TREE, 
1204                  containing_loop, &incr_gsi, insert_after, &indx_before_incr,
1205                  &indx_after_incr);
1206       incr = gsi_stmt (incr_gsi);
1207       set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
1208
1209       /* Copy the points-to information if it exists. */
1210       if (DR_PTR_INFO (dr))
1211         {
1212           duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
1213           duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
1214         }
1215       merge_alias_info (vect_ptr_init, indx_before_incr);
1216       merge_alias_info (vect_ptr_init, indx_after_incr);
1217       if (ptr_incr)
1218         *ptr_incr = incr;
1219
1220       return indx_before_incr; 
1221     }
1222   else
1223     gcc_unreachable ();
1224 }
1225
1226
1227 /* Function bump_vector_ptr
1228
1229    Increment a pointer (to a vector type) by vector-size. If requested,
1230    i.e. if PTR-INCR is given, then also connect the new increment stmt 
1231    to the existing def-use update-chain of the pointer, by modifying
1232    the PTR_INCR as illustrated below:
1233
1234    The pointer def-use update-chain before this function:
1235                         DATAREF_PTR = phi (p_0, p_2)
1236                         ....
1237         PTR_INCR:       p_2 = DATAREF_PTR + step 
1238
1239    The pointer def-use update-chain after this function:
1240                         DATAREF_PTR = phi (p_0, p_2)
1241                         ....
1242                         NEW_DATAREF_PTR = DATAREF_PTR + BUMP
1243                         ....
1244         PTR_INCR:       p_2 = NEW_DATAREF_PTR + step
1245
1246    Input:
1247    DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated 
1248                  in the loop.
1249    PTR_INCR - optional. The stmt that updates the pointer in each iteration of 
1250               the loop.  The increment amount across iterations is expected
1251               to be vector_size.      
1252    BSI - location where the new update stmt is to be placed.
1253    STMT - the original scalar memory-access stmt that is being vectorized.
1254    BUMP - optional. The offset by which to bump the pointer. If not given,
1255           the offset is assumed to be vector_size.
1256
1257    Output: Return NEW_DATAREF_PTR as illustrated above.
1258    
1259 */
1260
1261 static tree
1262 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
1263                  gimple stmt, tree bump)
1264 {
1265   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1266   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1267   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1268   tree ptr_var = SSA_NAME_VAR (dataref_ptr);
1269   tree update = TYPE_SIZE_UNIT (vectype);
1270   gimple incr_stmt;
1271   ssa_op_iter iter;
1272   use_operand_p use_p;
1273   tree new_dataref_ptr;
1274
1275   if (bump)
1276     update = bump;
1277     
1278   incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, ptr_var,
1279                                             dataref_ptr, update);
1280   new_dataref_ptr = make_ssa_name (ptr_var, incr_stmt);
1281   gimple_assign_set_lhs (incr_stmt, new_dataref_ptr);
1282   vect_finish_stmt_generation (stmt, incr_stmt, gsi);
1283
1284   /* Copy the points-to information if it exists. */
1285   if (DR_PTR_INFO (dr))
1286     duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
1287   merge_alias_info (new_dataref_ptr, dataref_ptr);
1288
1289   if (!ptr_incr)
1290     return new_dataref_ptr;
1291
1292   /* Update the vector-pointer's cross-iteration increment.  */
1293   FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
1294     {
1295       tree use = USE_FROM_PTR (use_p);
1296
1297       if (use == dataref_ptr)
1298         SET_USE (use_p, new_dataref_ptr);
1299       else
1300         gcc_assert (tree_int_cst_compare (use, update) == 0);
1301     }
1302
1303   return new_dataref_ptr;
1304 }
1305
1306
1307 /* Function vect_create_destination_var.
1308
1309    Create a new temporary of type VECTYPE.  */
1310
1311 static tree
1312 vect_create_destination_var (tree scalar_dest, tree vectype)
1313 {
1314   tree vec_dest;
1315   const char *new_name;
1316   tree type;
1317   enum vect_var_kind kind;
1318
1319   kind = vectype ? vect_simple_var : vect_scalar_var;
1320   type = vectype ? vectype : TREE_TYPE (scalar_dest);
1321
1322   gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
1323
1324   new_name = get_name (scalar_dest);
1325   if (!new_name)
1326     new_name = "var_";
1327   vec_dest = vect_get_new_vect_var (type, kind, new_name);
1328   add_referenced_var (vec_dest);
1329
1330   return vec_dest;
1331 }
1332
1333
1334 /* Function vect_init_vector.
1335
1336    Insert a new stmt (INIT_STMT) that initializes a new vector variable with
1337    the vector elements of VECTOR_VAR. Place the initialization at BSI if it
1338    is not NULL. Otherwise, place the initialization at the loop preheader.
1339    Return the DEF of INIT_STMT. 
1340    It will be used in the vectorization of STMT.  */
1341
1342 static tree
1343 vect_init_vector (gimple stmt, tree vector_var, tree vector_type,
1344                   gimple_stmt_iterator *gsi)
1345 {
1346   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1347   tree new_var;
1348   gimple init_stmt;
1349   tree vec_oprnd;
1350   edge pe;
1351   tree new_temp;
1352   basic_block new_bb;
1353  
1354   new_var = vect_get_new_vect_var (vector_type, vect_simple_var, "cst_");
1355   add_referenced_var (new_var); 
1356   init_stmt = gimple_build_assign  (new_var, vector_var);
1357   new_temp = make_ssa_name (new_var, init_stmt);
1358   gimple_assign_set_lhs (init_stmt, new_temp);
1359
1360   if (gsi)
1361     vect_finish_stmt_generation (stmt, init_stmt, gsi);
1362   else
1363     {
1364       loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1365       struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1366
1367       if (nested_in_vect_loop_p (loop, stmt))
1368         loop = loop->inner;
1369       pe = loop_preheader_edge (loop);
1370       new_bb = gsi_insert_on_edge_immediate (pe, init_stmt);
1371       gcc_assert (!new_bb);
1372     }
1373
1374   if (vect_print_dump_info (REPORT_DETAILS))
1375     {
1376       fprintf (vect_dump, "created new init_stmt: ");
1377       print_gimple_stmt (vect_dump, init_stmt, 0, TDF_SLIM);
1378     }
1379
1380   vec_oprnd = gimple_assign_lhs (init_stmt);
1381   return vec_oprnd;
1382 }
1383
1384
1385 /* For constant and loop invariant defs of SLP_NODE this function returns 
1386    (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.  
1387    OP_NUM determines if we gather defs for operand 0 or operand 1 of the scalar
1388    stmts. NUMBER_OF_VECTORS is the number of vector defs to create.  */
1389
1390 static void
1391 vect_get_constant_vectors (slp_tree slp_node, VEC(tree,heap) **vec_oprnds,
1392                            unsigned int op_num, unsigned int number_of_vectors)
1393 {
1394   VEC (gimple, heap) *stmts = SLP_TREE_SCALAR_STMTS (slp_node);
1395   gimple stmt = VEC_index (gimple, stmts, 0);
1396   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1397   tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1398   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1399   tree vec_cst;
1400   tree t = NULL_TREE;
1401   int j, number_of_places_left_in_vector;
1402   tree vector_type;
1403   tree op, vop;
1404   int group_size = VEC_length (gimple, stmts);
1405   unsigned int vec_num, i;
1406   int number_of_copies = 1;
1407   bool is_store = false;
1408   VEC (tree, heap) *voprnds = VEC_alloc (tree, heap, number_of_vectors);
1409   bool constant_p;
1410
1411   if (STMT_VINFO_DATA_REF (stmt_vinfo))
1412     is_store = true;
1413
1414   /* NUMBER_OF_COPIES is the number of times we need to use the same values in
1415      created vectors. It is greater than 1 if unrolling is performed. 
1416
1417      For example, we have two scalar operands, s1 and s2 (e.g., group of
1418      strided accesses of size two), while NUNITS is four (i.e., four scalars
1419      of this type can be packed in a vector). The output vector will contain
1420      two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
1421      will be 2).
1422
1423      If GROUP_SIZE > NUNITS, the scalars will be split into several vectors 
1424      containing the operands.
1425
1426      For example, NUNITS is four as before, and the group size is 8
1427      (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
1428      {s5, s6, s7, s8}.  */
1429     
1430   number_of_copies = least_common_multiple (nunits, group_size) / group_size;
1431
1432   number_of_places_left_in_vector = nunits;
1433   constant_p = true;
1434   for (j = 0; j < number_of_copies; j++)
1435     {
1436       for (i = group_size - 1; VEC_iterate (gimple, stmts, i, stmt); i--)
1437         {
1438           if (is_store)
1439             op = gimple_assign_rhs1 (stmt);
1440           else
1441             op = gimple_op (stmt, op_num + 1);
1442           if (!CONSTANT_CLASS_P (op))
1443             constant_p = false;
1444
1445           /* Create 'vect_ = {op0,op1,...,opn}'.  */
1446           t = tree_cons (NULL_TREE, op, t);
1447
1448           number_of_places_left_in_vector--;
1449
1450           if (number_of_places_left_in_vector == 0)
1451             {
1452               number_of_places_left_in_vector = nunits;
1453
1454               vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1455               gcc_assert (vector_type);
1456               if (constant_p)
1457                 vec_cst = build_vector (vector_type, t);
1458               else
1459                 vec_cst = build_constructor_from_list (vector_type, t);
1460               constant_p = true;
1461               VEC_quick_push (tree, voprnds,
1462                               vect_init_vector (stmt, vec_cst, vector_type,
1463                                                 NULL));
1464               t = NULL_TREE;
1465             }
1466         }
1467     }
1468
1469   /* Since the vectors are created in the reverse order, we should invert 
1470      them.  */
1471   vec_num = VEC_length (tree, voprnds);
1472   for (j = vec_num - 1; j >= 0; j--)
1473     {
1474       vop = VEC_index (tree, voprnds, j);
1475       VEC_quick_push (tree, *vec_oprnds, vop);
1476     }
1477
1478   VEC_free (tree, heap, voprnds);
1479
1480   /* In case that VF is greater than the unrolling factor needed for the SLP
1481      group of stmts, NUMBER_OF_VECTORS to be created is greater than 
1482      NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have 
1483      to replicate the vectors.  */
1484   while (number_of_vectors > VEC_length (tree, *vec_oprnds))
1485     {
1486       for (i = 0; VEC_iterate (tree, *vec_oprnds, i, vop) && i < vec_num; i++)
1487         VEC_quick_push (tree, *vec_oprnds, vop);
1488     }
1489 }
1490
1491
1492 /* Get vectorized definitions from SLP_NODE that contains corresponding
1493    vectorized def-stmts.  */
1494
1495 static void
1496 vect_get_slp_vect_defs (slp_tree slp_node, VEC (tree,heap) **vec_oprnds)
1497 {
1498   tree vec_oprnd;
1499   gimple vec_def_stmt;
1500   unsigned int i;
1501
1502   gcc_assert (SLP_TREE_VEC_STMTS (slp_node));
1503
1504   for (i = 0;
1505        VEC_iterate (gimple, SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt);
1506        i++)
1507     {
1508       gcc_assert (vec_def_stmt);
1509       vec_oprnd = gimple_get_lhs (vec_def_stmt);
1510       VEC_quick_push (tree, *vec_oprnds, vec_oprnd);
1511     }
1512 }
1513
1514
1515 /* Get vectorized definitions for SLP_NODE. 
1516    If the scalar definitions are loop invariants or constants, collect them and 
1517    call vect_get_constant_vectors() to create vector stmts.
1518    Otherwise, the def-stmts must be already vectorized and the vectorized stmts
1519    must be stored in the LEFT/RIGHT node of SLP_NODE, and we call
1520    vect_get_slp_vect_defs() to retrieve them.  
1521    If VEC_OPRNDS1 is NULL, don't get vector defs for the second operand (from
1522    the right node. This is used when the second operand must remain scalar.  */ 
1523  
1524 static void
1525 vect_get_slp_defs (slp_tree slp_node, VEC (tree,heap) **vec_oprnds0,
1526                    VEC (tree,heap) **vec_oprnds1)
1527 {
1528   gimple first_stmt;
1529   enum tree_code code;
1530   int number_of_vects;
1531   HOST_WIDE_INT lhs_size_unit, rhs_size_unit; 
1532
1533   first_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0);
1534   /* The number of vector defs is determined by the number of vector statements
1535      in the node from which we get those statements.  */
1536   if (SLP_TREE_LEFT (slp_node)) 
1537     number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_LEFT (slp_node));
1538   else
1539     {
1540       number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
1541       /* Number of vector stmts was calculated according to LHS in
1542          vect_schedule_slp_instance(), fix it by replacing LHS with RHS, if
1543          necessary. See vect_get_smallest_scalar_type() for details.  */
1544       vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
1545                                      &rhs_size_unit);
1546       if (rhs_size_unit != lhs_size_unit)
1547         {
1548           number_of_vects *= rhs_size_unit;
1549           number_of_vects /= lhs_size_unit;
1550         }
1551     }
1552
1553   /* Allocate memory for vectorized defs.  */
1554   *vec_oprnds0 = VEC_alloc (tree, heap, number_of_vects);
1555
1556   /* SLP_NODE corresponds either to a group of stores or to a group of
1557      unary/binary operations. We don't call this function for loads.  */
1558   if (SLP_TREE_LEFT (slp_node))
1559     /* The defs are already vectorized.  */
1560     vect_get_slp_vect_defs (SLP_TREE_LEFT (slp_node), vec_oprnds0);
1561   else
1562     /* Build vectors from scalar defs.  */
1563     vect_get_constant_vectors (slp_node, vec_oprnds0, 0, number_of_vects);
1564
1565   if (STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt)))
1566     /* Since we don't call this function with loads, this is a group of
1567        stores.  */
1568     return;
1569
1570   code = gimple_assign_rhs_code (first_stmt);
1571   if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS || !vec_oprnds1)
1572     return;
1573
1574   /* The number of vector defs is determined by the number of vector statements
1575      in the node from which we get those statements.  */
1576   if (SLP_TREE_RIGHT (slp_node))
1577     number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_RIGHT (slp_node));
1578   else
1579     number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
1580
1581   *vec_oprnds1 = VEC_alloc (tree, heap, number_of_vects);
1582
1583   if (SLP_TREE_RIGHT (slp_node))
1584     /* The defs are already vectorized.  */
1585     vect_get_slp_vect_defs (SLP_TREE_RIGHT (slp_node), vec_oprnds1);
1586   else
1587     /* Build vectors from scalar defs.  */
1588     vect_get_constant_vectors (slp_node, vec_oprnds1, 1, number_of_vects);
1589 }
1590
1591
1592 /* Function get_initial_def_for_induction
1593
1594    Input:
1595    STMT - a stmt that performs an induction operation in the loop.
1596    IV_PHI - the initial value of the induction variable
1597
1598    Output:
1599    Return a vector variable, initialized with the first VF values of
1600    the induction variable. E.g., for an iv with IV_PHI='X' and
1601    evolution S, for a vector of 4 units, we want to return: 
1602    [X, X + S, X + 2*S, X + 3*S].  */
1603
1604 static tree
1605 get_initial_def_for_induction (gimple iv_phi)
1606 {
1607   stmt_vec_info stmt_vinfo = vinfo_for_stmt (iv_phi);
1608   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1609   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1610   tree scalar_type = TREE_TYPE (gimple_phi_result (iv_phi));
1611   tree vectype; 
1612   int nunits;
1613   edge pe = loop_preheader_edge (loop);
1614   struct loop *iv_loop;
1615   basic_block new_bb;
1616   tree vec, vec_init, vec_step, t;
1617   tree access_fn;
1618   tree new_var;
1619   tree new_name;
1620   gimple init_stmt, induction_phi, new_stmt;
1621   tree induc_def, vec_def, vec_dest;
1622   tree init_expr, step_expr;
1623   int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1624   int i;
1625   bool ok;
1626   int ncopies;
1627   tree expr;
1628   stmt_vec_info phi_info = vinfo_for_stmt (iv_phi);
1629   bool nested_in_vect_loop = false;
1630   gimple_seq stmts = NULL;
1631   imm_use_iterator imm_iter;
1632   use_operand_p use_p;
1633   gimple exit_phi;
1634   edge latch_e;
1635   tree loop_arg;
1636   gimple_stmt_iterator si;
1637   basic_block bb = gimple_bb (iv_phi);
1638
1639   vectype = get_vectype_for_scalar_type (scalar_type);
1640   gcc_assert (vectype);
1641   nunits = TYPE_VECTOR_SUBPARTS (vectype);
1642   ncopies = vf / nunits;
1643
1644   gcc_assert (phi_info);
1645   gcc_assert (ncopies >= 1);
1646
1647   /* Find the first insertion point in the BB.  */
1648   si = gsi_after_labels (bb);
1649
1650   if (INTEGRAL_TYPE_P (scalar_type) || POINTER_TYPE_P (scalar_type))
1651     step_expr = build_int_cst (scalar_type, 0);
1652   else
1653     step_expr = build_real (scalar_type, dconst0);
1654
1655   /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop?  */
1656   if (nested_in_vect_loop_p (loop, iv_phi))
1657     {
1658       nested_in_vect_loop = true;
1659       iv_loop = loop->inner;
1660     }
1661   else
1662     iv_loop = loop;
1663   gcc_assert (iv_loop == (gimple_bb (iv_phi))->loop_father);
1664
1665   latch_e = loop_latch_edge (iv_loop);
1666   loop_arg = PHI_ARG_DEF_FROM_EDGE (iv_phi, latch_e);
1667
1668   access_fn = analyze_scalar_evolution (iv_loop, PHI_RESULT (iv_phi));
1669   gcc_assert (access_fn);
1670   ok = vect_is_simple_iv_evolution (iv_loop->num, access_fn,
1671                                   &init_expr, &step_expr);
1672   gcc_assert (ok);
1673   pe = loop_preheader_edge (iv_loop);
1674
1675   /* Create the vector that holds the initial_value of the induction.  */
1676   if (nested_in_vect_loop)
1677     {
1678       /* iv_loop is nested in the loop to be vectorized.  init_expr had already
1679          been created during vectorization of previous stmts; We obtain it from
1680          the STMT_VINFO_VEC_STMT of the defining stmt. */
1681       tree iv_def = PHI_ARG_DEF_FROM_EDGE (iv_phi, loop_preheader_edge (iv_loop));
1682       vec_init = vect_get_vec_def_for_operand (iv_def, iv_phi, NULL);
1683     }
1684   else
1685     {
1686       /* iv_loop is the loop to be vectorized. Create:
1687          vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr)  */
1688       new_var = vect_get_new_vect_var (scalar_type, vect_scalar_var, "var_");
1689       add_referenced_var (new_var);
1690
1691       new_name = force_gimple_operand (init_expr, &stmts, false, new_var);
1692       if (stmts)
1693         {
1694           new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
1695           gcc_assert (!new_bb);
1696         }
1697
1698       t = NULL_TREE;
1699       t = tree_cons (NULL_TREE, init_expr, t);
1700       for (i = 1; i < nunits; i++)
1701         {
1702           /* Create: new_name_i = new_name + step_expr  */
1703           enum tree_code code = POINTER_TYPE_P (scalar_type)
1704                                 ? POINTER_PLUS_EXPR : PLUS_EXPR;
1705           init_stmt = gimple_build_assign_with_ops (code, new_var,
1706                                                     new_name, step_expr);
1707           new_name = make_ssa_name (new_var, init_stmt);
1708           gimple_assign_set_lhs (init_stmt, new_name);
1709
1710           new_bb = gsi_insert_on_edge_immediate (pe, init_stmt);
1711           gcc_assert (!new_bb);
1712
1713           if (vect_print_dump_info (REPORT_DETAILS))
1714             {
1715               fprintf (vect_dump, "created new init_stmt: ");
1716               print_gimple_stmt (vect_dump, init_stmt, 0, TDF_SLIM);
1717             }
1718           t = tree_cons (NULL_TREE, new_name, t);
1719         }
1720       /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1]  */
1721       vec = build_constructor_from_list (vectype, nreverse (t));
1722       vec_init = vect_init_vector (iv_phi, vec, vectype, NULL);
1723     }
1724
1725
1726   /* Create the vector that holds the step of the induction.  */
1727   if (nested_in_vect_loop)
1728     /* iv_loop is nested in the loop to be vectorized. Generate:
1729        vec_step = [S, S, S, S]  */
1730     new_name = step_expr;
1731   else
1732     {
1733       /* iv_loop is the loop to be vectorized. Generate:
1734           vec_step = [VF*S, VF*S, VF*S, VF*S]  */
1735       expr = build_int_cst (scalar_type, vf);
1736       new_name = fold_build2 (MULT_EXPR, scalar_type, expr, step_expr);
1737     }
1738
1739   t = NULL_TREE;
1740   for (i = 0; i < nunits; i++)
1741     t = tree_cons (NULL_TREE, unshare_expr (new_name), t);
1742   gcc_assert (CONSTANT_CLASS_P (new_name));
1743   vec = build_vector (vectype, t);
1744   vec_step = vect_init_vector (iv_phi, vec, vectype, NULL);
1745
1746
1747   /* Create the following def-use cycle:
1748      loop prolog:
1749          vec_init = ...
1750          vec_step = ...
1751      loop:
1752          vec_iv = PHI <vec_init, vec_loop>
1753          ...
1754          STMT
1755          ...
1756          vec_loop = vec_iv + vec_step;  */
1757
1758   /* Create the induction-phi that defines the induction-operand.  */
1759   vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, "vec_iv_");
1760   add_referenced_var (vec_dest);
1761   induction_phi = create_phi_node (vec_dest, iv_loop->header);
1762   set_vinfo_for_stmt (induction_phi,
1763                       new_stmt_vec_info (induction_phi, loop_vinfo));
1764   induc_def = PHI_RESULT (induction_phi);
1765
1766   /* Create the iv update inside the loop  */
1767   new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, vec_dest,
1768                                            induc_def, vec_step);
1769   vec_def = make_ssa_name (vec_dest, new_stmt);
1770   gimple_assign_set_lhs (new_stmt, vec_def);
1771   gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
1772   set_vinfo_for_stmt (new_stmt, new_stmt_vec_info (new_stmt, loop_vinfo));
1773
1774   /* Set the arguments of the phi node:  */
1775   add_phi_arg (induction_phi, vec_init, pe);
1776   add_phi_arg (induction_phi, vec_def, loop_latch_edge (iv_loop));
1777
1778
1779   /* In case that vectorization factor (VF) is bigger than the number
1780      of elements that we can fit in a vectype (nunits), we have to generate
1781      more than one vector stmt - i.e - we need to "unroll" the
1782      vector stmt by a factor VF/nunits.  For more details see documentation
1783      in vectorizable_operation.  */
1784   
1785   if (ncopies > 1)
1786     {
1787       stmt_vec_info prev_stmt_vinfo;
1788       /* FORNOW. This restriction should be relaxed.  */
1789       gcc_assert (!nested_in_vect_loop);
1790
1791       /* Create the vector that holds the step of the induction.  */
1792       expr = build_int_cst (scalar_type, nunits);
1793       new_name = fold_build2 (MULT_EXPR, scalar_type, expr, step_expr);
1794       t = NULL_TREE;
1795       for (i = 0; i < nunits; i++)
1796         t = tree_cons (NULL_TREE, unshare_expr (new_name), t);
1797       gcc_assert (CONSTANT_CLASS_P (new_name));
1798       vec = build_vector (vectype, t);
1799       vec_step = vect_init_vector (iv_phi, vec, vectype, NULL);
1800
1801       vec_def = induc_def;
1802       prev_stmt_vinfo = vinfo_for_stmt (induction_phi);
1803       for (i = 1; i < ncopies; i++)
1804         {
1805           /* vec_i = vec_prev + vec_step  */
1806           new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, vec_dest,
1807                                                    vec_def, vec_step);
1808           vec_def = make_ssa_name (vec_dest, new_stmt);
1809           gimple_assign_set_lhs (new_stmt, vec_def);
1810
1811           gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
1812           set_vinfo_for_stmt (new_stmt,
1813                               new_stmt_vec_info (new_stmt, loop_vinfo));
1814           STMT_VINFO_RELATED_STMT (prev_stmt_vinfo) = new_stmt;
1815           prev_stmt_vinfo = vinfo_for_stmt (new_stmt); 
1816         }
1817     }
1818
1819   if (nested_in_vect_loop)
1820     {
1821       /* Find the loop-closed exit-phi of the induction, and record
1822          the final vector of induction results:  */
1823       exit_phi = NULL;
1824       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
1825         {
1826           if (!flow_bb_inside_loop_p (iv_loop, gimple_bb (USE_STMT (use_p))))
1827             {
1828               exit_phi = USE_STMT (use_p);
1829               break;
1830             }
1831         }
1832       if (exit_phi) 
1833         {
1834           stmt_vec_info stmt_vinfo = vinfo_for_stmt (exit_phi);
1835           /* FORNOW. Currently not supporting the case that an inner-loop induction
1836              is not used in the outer-loop (i.e. only outside the outer-loop).  */
1837           gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo)
1838                       && !STMT_VINFO_LIVE_P (stmt_vinfo));
1839
1840           STMT_VINFO_VEC_STMT (stmt_vinfo) = new_stmt;
1841           if (vect_print_dump_info (REPORT_DETAILS))
1842             {
1843               fprintf (vect_dump, "vector of inductions after inner-loop:");
1844               print_gimple_stmt (vect_dump, new_stmt, 0, TDF_SLIM);
1845             }
1846         }
1847     }
1848
1849
1850   if (vect_print_dump_info (REPORT_DETAILS))
1851     {
1852       fprintf (vect_dump, "transform induction: created def-use cycle: ");
1853       print_gimple_stmt (vect_dump, induction_phi, 0, TDF_SLIM);
1854       fprintf (vect_dump, "\n");
1855       print_gimple_stmt (vect_dump, SSA_NAME_DEF_STMT (vec_def), 0, TDF_SLIM);
1856     }
1857
1858   STMT_VINFO_VEC_STMT (phi_info) = induction_phi;
1859   return induc_def;
1860 }
1861
1862
1863 /* Function vect_get_vec_def_for_operand.
1864
1865    OP is an operand in STMT. This function returns a (vector) def that will be
1866    used in the vectorized stmt for STMT.
1867
1868    In the case that OP is an SSA_NAME which is defined in the loop, then
1869    STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1870
1871    In case OP is an invariant or constant, a new stmt that creates a vector def
1872    needs to be introduced.  */
1873
1874 static tree
1875 vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
1876 {
1877   tree vec_oprnd;
1878   gimple vec_stmt;
1879   gimple def_stmt;
1880   stmt_vec_info def_stmt_info = NULL;
1881   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1882   tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1883   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1884   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1885   tree vec_inv;
1886   tree vec_cst;
1887   tree t = NULL_TREE;
1888   tree def;
1889   int i;
1890   enum vect_def_type dt;
1891   bool is_simple_use;
1892   tree vector_type;
1893
1894   if (vect_print_dump_info (REPORT_DETAILS))
1895     {
1896       fprintf (vect_dump, "vect_get_vec_def_for_operand: ");
1897       print_generic_expr (vect_dump, op, TDF_SLIM);
1898     }
1899
1900   is_simple_use = vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt);
1901   gcc_assert (is_simple_use);
1902   if (vect_print_dump_info (REPORT_DETAILS))
1903     {
1904       if (def)
1905         {
1906           fprintf (vect_dump, "def =  ");
1907           print_generic_expr (vect_dump, def, TDF_SLIM);
1908         }
1909       if (def_stmt)
1910         {
1911           fprintf (vect_dump, "  def_stmt =  ");
1912           print_gimple_stmt (vect_dump, def_stmt, 0, TDF_SLIM);
1913         }
1914     }
1915
1916   switch (dt)
1917     {
1918     /* Case 1: operand is a constant.  */
1919     case vect_constant_def:
1920       {
1921         if (scalar_def) 
1922           *scalar_def = op;
1923
1924         /* Create 'vect_cst_ = {cst,cst,...,cst}'  */
1925         if (vect_print_dump_info (REPORT_DETAILS))
1926           fprintf (vect_dump, "Create vector_cst. nunits = %d", nunits);
1927
1928         for (i = nunits - 1; i >= 0; --i)
1929           {
1930             t = tree_cons (NULL_TREE, op, t);
1931           }
1932         vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1933         gcc_assert (vector_type);
1934         vec_cst = build_vector (vector_type, t);
1935
1936         return vect_init_vector (stmt, vec_cst, vector_type, NULL);
1937       }
1938
1939     /* Case 2: operand is defined outside the loop - loop invariant.  */
1940     case vect_invariant_def:
1941       {
1942         if (scalar_def) 
1943           *scalar_def = def;
1944
1945         /* Create 'vec_inv = {inv,inv,..,inv}'  */
1946         if (vect_print_dump_info (REPORT_DETAILS))
1947           fprintf (vect_dump, "Create vector_inv.");
1948
1949         for (i = nunits - 1; i >= 0; --i)
1950           {
1951             t = tree_cons (NULL_TREE, def, t);
1952           }
1953
1954         /* FIXME: use build_constructor directly.  */
1955         vector_type = get_vectype_for_scalar_type (TREE_TYPE (def));
1956         gcc_assert (vector_type);
1957         vec_inv = build_constructor_from_list (vector_type, t);
1958         return vect_init_vector (stmt, vec_inv, vector_type, NULL);
1959       }
1960
1961     /* Case 3: operand is defined inside the loop.  */
1962     case vect_loop_def:
1963       {
1964         if (scalar_def) 
1965           *scalar_def = NULL/* FIXME tuples: def_stmt*/;
1966
1967         /* Get the def from the vectorized stmt.  */
1968         def_stmt_info = vinfo_for_stmt (def_stmt);
1969         vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1970         gcc_assert (vec_stmt);
1971         if (gimple_code (vec_stmt) == GIMPLE_PHI)
1972           vec_oprnd = PHI_RESULT (vec_stmt);
1973         else if (is_gimple_call (vec_stmt))
1974           vec_oprnd = gimple_call_lhs (vec_stmt);
1975         else
1976           vec_oprnd = gimple_assign_lhs (vec_stmt);
1977         return vec_oprnd;
1978       }
1979
1980     /* Case 4: operand is defined by a loop header phi - reduction  */
1981     case vect_reduction_def:
1982       {
1983         struct loop *loop;
1984
1985         gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1986         loop = (gimple_bb (def_stmt))->loop_father; 
1987
1988         /* Get the def before the loop  */
1989         op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
1990         return get_initial_def_for_reduction (stmt, op, scalar_def);
1991      }
1992
1993     /* Case 5: operand is defined by loop-header phi - induction.  */
1994     case vect_induction_def:
1995       {
1996         gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1997
1998         /* Get the def from the vectorized stmt.  */
1999         def_stmt_info = vinfo_for_stmt (def_stmt);
2000         vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
2001         gcc_assert (vec_stmt && gimple_code (vec_stmt) == GIMPLE_PHI);
2002         vec_oprnd = PHI_RESULT (vec_stmt);
2003         return vec_oprnd;
2004       }
2005
2006     default:
2007       gcc_unreachable ();
2008     }
2009 }
2010
2011
2012 /* Function vect_get_vec_def_for_stmt_copy
2013
2014    Return a vector-def for an operand. This function is used when the 
2015    vectorized stmt to be created (by the caller to this function) is a "copy" 
2016    created in case the vectorized result cannot fit in one vector, and several 
2017    copies of the vector-stmt are required. In this case the vector-def is 
2018    retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
2019    of the stmt that defines VEC_OPRND. 
2020    DT is the type of the vector def VEC_OPRND.
2021
2022    Context:
2023         In case the vectorization factor (VF) is bigger than the number
2024    of elements that can fit in a vectype (nunits), we have to generate
2025    more than one vector stmt to vectorize the scalar stmt. This situation
2026    arises when there are multiple data-types operated upon in the loop; the 
2027    smallest data-type determines the VF, and as a result, when vectorizing
2028    stmts operating on wider types we need to create 'VF/nunits' "copies" of the
2029    vector stmt (each computing a vector of 'nunits' results, and together
2030    computing 'VF' results in each iteration).  This function is called when 
2031    vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
2032    which VF=16 and nunits=4, so the number of copies required is 4):
2033
2034    scalar stmt:         vectorized into:        STMT_VINFO_RELATED_STMT
2035  
2036    S1: x = load         VS1.0:  vx.0 = memref0      VS1.1
2037                         VS1.1:  vx.1 = memref1      VS1.2
2038                         VS1.2:  vx.2 = memref2      VS1.3
2039                         VS1.3:  vx.3 = memref3 
2040
2041    S2: z = x + ...      VSnew.0:  vz0 = vx.0 + ...  VSnew.1
2042                         VSnew.1:  vz1 = vx.1 + ...  VSnew.2
2043                         VSnew.2:  vz2 = vx.2 + ...  VSnew.3
2044                         VSnew.3:  vz3 = vx.3 + ...
2045
2046    The vectorization of S1 is explained in vectorizable_load.
2047    The vectorization of S2:
2048         To create the first vector-stmt out of the 4 copies - VSnew.0 - 
2049    the function 'vect_get_vec_def_for_operand' is called to 
2050    get the relevant vector-def for each operand of S2. For operand x it
2051    returns  the vector-def 'vx.0'.
2052
2053         To create the remaining copies of the vector-stmt (VSnew.j), this 
2054    function is called to get the relevant vector-def for each operand.  It is 
2055    obtained from the respective VS1.j stmt, which is recorded in the 
2056    STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
2057
2058         For example, to obtain the vector-def 'vx.1' in order to create the 
2059    vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'. 
2060    Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the 
2061    STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
2062    and return its def ('vx.1').
2063    Overall, to create the above sequence this function will be called 3 times:
2064         vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
2065         vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
2066         vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2);  */
2067
2068 static tree
2069 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
2070 {
2071   gimple vec_stmt_for_operand;
2072   stmt_vec_info def_stmt_info;
2073
2074   /* Do nothing; can reuse same def.  */
2075   if (dt == vect_invariant_def || dt == vect_constant_def )
2076     return vec_oprnd;
2077
2078   vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
2079   def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
2080   gcc_assert (def_stmt_info);
2081   vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
2082   gcc_assert (vec_stmt_for_operand);
2083   vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
2084   if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
2085     vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
2086   else
2087     vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
2088   return vec_oprnd;
2089 }
2090
2091
2092 /* Get vectorized definitions for the operands to create a copy of an original
2093    stmt. See vect_get_vec_def_for_stmt_copy() for details.  */
2094
2095 static void
2096 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt, 
2097                                  VEC(tree,heap) **vec_oprnds0, 
2098                                  VEC(tree,heap) **vec_oprnds1)
2099 {
2100   tree vec_oprnd = VEC_pop (tree, *vec_oprnds0);
2101
2102   vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
2103   VEC_quick_push (tree, *vec_oprnds0, vec_oprnd);
2104
2105   if (vec_oprnds1 && *vec_oprnds1)
2106     {
2107       vec_oprnd = VEC_pop (tree, *vec_oprnds1);
2108       vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
2109       VEC_quick_push (tree, *vec_oprnds1, vec_oprnd);
2110     }
2111 }
2112
2113
2114 /* Get vectorized definitions for OP0 and OP1, or SLP_NODE if it is not NULL.  */
2115
2116 static void
2117 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
2118                    VEC(tree,heap) **vec_oprnds0, VEC(tree,heap) **vec_oprnds1,
2119                    slp_tree slp_node)
2120 {
2121   if (slp_node)
2122     vect_get_slp_defs (slp_node, vec_oprnds0, vec_oprnds1);
2123   else
2124     {
2125       tree vec_oprnd;
2126
2127       *vec_oprnds0 = VEC_alloc (tree, heap, 1); 
2128       vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);      
2129       VEC_quick_push (tree, *vec_oprnds0, vec_oprnd);
2130
2131       if (op1)
2132         {
2133           *vec_oprnds1 = VEC_alloc (tree, heap, 1);     
2134           vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);      
2135           VEC_quick_push (tree, *vec_oprnds1, vec_oprnd);
2136         }
2137     }
2138 }
2139
2140
2141 /* Function vect_finish_stmt_generation.
2142
2143    Insert a new stmt.  */
2144
2145 static void
2146 vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
2147                              gimple_stmt_iterator *gsi)
2148 {
2149   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2150   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2151
2152   gcc_assert (stmt == gsi_stmt (*gsi));
2153   gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
2154
2155   gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
2156
2157   set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo));
2158
2159   if (vect_print_dump_info (REPORT_DETAILS))
2160     {
2161       fprintf (vect_dump, "add new stmt: ");
2162       print_gimple_stmt (vect_dump, vec_stmt, 0, TDF_SLIM);
2163     }
2164
2165   /* Make sure gsi points to the stmt that is being vectorized.  */
2166   gcc_assert (stmt == gsi_stmt (*gsi));
2167
2168   gimple_set_location (vec_stmt, gimple_location (stmt));
2169 }
2170
2171
2172 /* Function get_initial_def_for_reduction
2173
2174    Input:
2175    STMT - a stmt that performs a reduction operation in the loop.
2176    INIT_VAL - the initial value of the reduction variable
2177
2178    Output:
2179    ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
2180         of the reduction (used for adjusting the epilog - see below).
2181    Return a vector variable, initialized according to the operation that STMT
2182         performs. This vector will be used as the initial value of the
2183         vector of partial results.
2184
2185    Option1 (adjust in epilog): Initialize the vector as follows:
2186      add:         [0,0,...,0,0]
2187      mult:        [1,1,...,1,1]
2188      min/max:     [init_val,init_val,..,init_val,init_val]
2189      bit and/or:  [init_val,init_val,..,init_val,init_val]
2190    and when necessary (e.g. add/mult case) let the caller know
2191    that it needs to adjust the result by init_val.
2192
2193    Option2: Initialize the vector as follows:
2194      add:         [0,0,...,0,init_val]
2195      mult:        [1,1,...,1,init_val]
2196      min/max:     [init_val,init_val,...,init_val]
2197      bit and/or:  [init_val,init_val,...,init_val]
2198    and no adjustments are needed.
2199
2200    For example, for the following code:
2201
2202    s = init_val;
2203    for (i=0;i<n;i++)
2204      s = s + a[i];
2205
2206    STMT is 's = s + a[i]', and the reduction variable is 's'.
2207    For a vector of 4 units, we want to return either [0,0,0,init_val],
2208    or [0,0,0,0] and let the caller know that it needs to adjust
2209    the result at the end by 'init_val'.
2210
2211    FORNOW, we are using the 'adjust in epilog' scheme, because this way the
2212    initialization vector is simpler (same element in all entries).
2213    A cost model should help decide between these two schemes.  */
2214
2215 static tree
2216 get_initial_def_for_reduction (gimple stmt, tree init_val, tree *adjustment_def)
2217 {
2218   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2219   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
2220   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2221   tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
2222   int nunits =  TYPE_VECTOR_SUBPARTS (vectype);
2223   enum tree_code code = gimple_assign_rhs_code (stmt);
2224   tree type = TREE_TYPE (init_val);
2225   tree vecdef;
2226   tree def_for_init;
2227   tree init_def;
2228   tree t = NULL_TREE;
2229   int i;
2230   tree vector_type;
2231   bool nested_in_vect_loop = false; 
2232
2233   gcc_assert (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type));
2234   if (nested_in_vect_loop_p (loop, stmt))
2235     nested_in_vect_loop = true;
2236   else
2237     gcc_assert (loop == (gimple_bb (stmt))->loop_father);
2238
2239   vecdef = vect_get_vec_def_for_operand (init_val, stmt, NULL);
2240
2241   switch (code)
2242   {
2243   case WIDEN_SUM_EXPR:
2244   case DOT_PROD_EXPR:
2245   case PLUS_EXPR:
2246     if (nested_in_vect_loop)
2247       *adjustment_def = vecdef;
2248     else
2249       *adjustment_def = init_val;
2250     /* Create a vector of zeros for init_def.  */
2251     if (SCALAR_FLOAT_TYPE_P (type))
2252       def_for_init = build_real (type, dconst0);
2253     else
2254       def_for_init = build_int_cst (type, 0);
2255     for (i = nunits - 1; i >= 0; --i)
2256       t = tree_cons (NULL_TREE, def_for_init, t);
2257     vector_type = get_vectype_for_scalar_type (TREE_TYPE (def_for_init));
2258     gcc_assert (vector_type);
2259     init_def = build_vector (vector_type, t);
2260     break;
2261
2262   case MIN_EXPR:
2263   case MAX_EXPR:
2264     *adjustment_def = NULL_TREE;
2265     init_def = vecdef;
2266     break;
2267
2268   default:
2269     gcc_unreachable ();
2270   }
2271
2272   return init_def;
2273 }
2274
2275
2276 /* Function vect_create_epilog_for_reduction
2277     
2278    Create code at the loop-epilog to finalize the result of a reduction
2279    computation. 
2280   
2281    VECT_DEF is a vector of partial results. 
2282    REDUC_CODE is the tree-code for the epilog reduction.
2283    NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
2284      number of elements that we can fit in a vectype (nunits). In this case
2285      we have to generate more than one vector stmt - i.e - we need to "unroll"
2286      the vector stmt by a factor VF/nunits.  For more details see documentation
2287      in vectorizable_operation.
2288    STMT is the scalar reduction stmt that is being vectorized.
2289    REDUCTION_PHI is the phi-node that carries the reduction computation.
2290
2291    This function:
2292    1. Creates the reduction def-use cycle: sets the arguments for 
2293       REDUCTION_PHI:
2294       The loop-entry argument is the vectorized initial-value of the reduction.
2295       The loop-latch argument is VECT_DEF - the vector of partial sums.
2296    2. "Reduces" the vector of partial results VECT_DEF into a single result,
2297       by applying the operation specified by REDUC_CODE if available, or by 
2298       other means (whole-vector shifts or a scalar loop).
2299       The function also creates a new phi node at the loop exit to preserve 
2300       loop-closed form, as illustrated below.
2301   
2302      The flow at the entry to this function:
2303     
2304         loop:
2305           vec_def = phi <null, null>            # REDUCTION_PHI
2306           VECT_DEF = vector_stmt                # vectorized form of STMT
2307           s_loop = scalar_stmt                  # (scalar) STMT
2308         loop_exit:
2309           s_out0 = phi <s_loop>                 # (scalar) EXIT_PHI
2310           use <s_out0>
2311           use <s_out0>
2312
2313      The above is transformed by this function into:
2314
2315         loop:
2316           vec_def = phi <vec_init, VECT_DEF>    # REDUCTION_PHI
2317           VECT_DEF = vector_stmt                # vectorized form of STMT
2318           s_loop = scalar_stmt                  # (scalar) STMT 
2319         loop_exit:
2320           s_out0 = phi <s_loop>                 # (scalar) EXIT_PHI
2321           v_out1 = phi <VECT_DEF>               # NEW_EXIT_PHI
2322           v_out2 = reduce <v_out1>
2323           s_out3 = extract_field <v_out2, 0>
2324           s_out4 = adjust_result <s_out3>
2325           use <s_out4>
2326           use <s_out4>
2327 */
2328
2329 static void
2330 vect_create_epilog_for_reduction (tree vect_def, gimple stmt,
2331                                   int ncopies,
2332                                   enum tree_code reduc_code,
2333                                   gimple reduction_phi)
2334 {
2335   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2336   stmt_vec_info prev_phi_info;
2337   tree vectype;
2338   enum machine_mode mode;
2339   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2340   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2341   basic_block exit_bb;
2342   tree scalar_dest;
2343   tree scalar_type;
2344   gimple new_phi = NULL, phi;
2345   gimple_stmt_iterator exit_gsi;
2346   tree vec_dest;
2347   tree new_temp = NULL_TREE;
2348   tree new_name;
2349   gimple epilog_stmt = NULL;
2350   tree new_scalar_dest, new_dest;
2351   gimple exit_phi;
2352   tree bitsize, bitpos, bytesize; 
2353   enum tree_code code = gimple_assign_rhs_code (stmt);
2354   tree adjustment_def;
2355   tree vec_initial_def, def;
2356   tree orig_name;
2357   imm_use_iterator imm_iter;
2358   use_operand_p use_p;
2359   bool extract_scalar_result = false;
2360   tree reduction_op, expr;
2361   gimple orig_stmt;
2362   gimple use_stmt;
2363   bool nested_in_vect_loop = false;
2364   VEC(gimple,heap) *phis = NULL;
2365   enum vect_def_type dt = vect_unknown_def_type;
2366   int j, i;
2367   
2368   if (nested_in_vect_loop_p (loop, stmt))
2369     {
2370       loop = loop->inner;
2371       nested_in_vect_loop = true;
2372     }
2373   
2374   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2375     {
2376     case GIMPLE_SINGLE_RHS:
2377       gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt)) == ternary_op);
2378       reduction_op = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2379       break;
2380     case GIMPLE_UNARY_RHS:
2381       reduction_op = gimple_assign_rhs1 (stmt);
2382       break;
2383     case GIMPLE_BINARY_RHS:
2384       reduction_op = gimple_assign_rhs2 (stmt);
2385       break;
2386     default:
2387       gcc_unreachable ();
2388     }
2389
2390   vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
2391   gcc_assert (vectype);
2392   mode = TYPE_MODE (vectype);
2393
2394   /*** 1. Create the reduction def-use cycle  ***/
2395   
2396   /* For the case of reduction, vect_get_vec_def_for_operand returns
2397      the scalar def before the loop, that defines the initial value
2398      of the reduction variable.  */
2399   vec_initial_def = vect_get_vec_def_for_operand (reduction_op, stmt,
2400                                                   &adjustment_def);
2401
2402   phi = reduction_phi;
2403   def = vect_def;
2404   for (j = 0; j < ncopies; j++)
2405     {
2406       /* 1.1 set the loop-entry arg of the reduction-phi:  */
2407       add_phi_arg (phi, vec_initial_def, loop_preheader_edge (loop));
2408
2409       /* 1.2 set the loop-latch arg for the reduction-phi:  */
2410       if (j > 0)
2411         def = vect_get_vec_def_for_stmt_copy (dt, def);
2412       add_phi_arg (phi, def, loop_latch_edge (loop));
2413
2414       if (vect_print_dump_info (REPORT_DETAILS))
2415         {
2416           fprintf (vect_dump, "transform reduction: created def-use cycle: ");
2417           print_gimple_stmt (vect_dump, phi, 0, TDF_SLIM);
2418           fprintf (vect_dump, "\n");
2419           print_gimple_stmt (vect_dump, SSA_NAME_DEF_STMT (def), 0, TDF_SLIM);
2420         }
2421
2422       phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
2423     }
2424
2425   /*** 2. Create epilog code
2426           The reduction epilog code operates across the elements of the vector
2427           of partial results computed by the vectorized loop.
2428           The reduction epilog code consists of:
2429           step 1: compute the scalar result in a vector (v_out2)
2430           step 2: extract the scalar result (s_out3) from the vector (v_out2)
2431           step 3: adjust the scalar result (s_out3) if needed.
2432
2433           Step 1 can be accomplished using one the following three schemes:
2434           (scheme 1) using reduc_code, if available.
2435           (scheme 2) using whole-vector shifts, if available.
2436           (scheme 3) using a scalar loop. In this case steps 1+2 above are 
2437                      combined.
2438                 
2439           The overall epilog code looks like this:
2440
2441           s_out0 = phi <s_loop>         # original EXIT_PHI
2442           v_out1 = phi <VECT_DEF>       # NEW_EXIT_PHI
2443           v_out2 = reduce <v_out1>              # step 1
2444           s_out3 = extract_field <v_out2, 0>    # step 2
2445           s_out4 = adjust_result <s_out3>       # step 3
2446
2447           (step 3 is optional, and steps 1 and 2 may be combined).
2448           Lastly, the uses of s_out0 are replaced by s_out4.
2449
2450           ***/
2451
2452   /* 2.1 Create new loop-exit-phi to preserve loop-closed form:
2453         v_out1 = phi <v_loop>  */
2454
2455   exit_bb = single_exit (loop)->dest;
2456   def = vect_def;
2457   prev_phi_info = NULL;
2458   for (j = 0; j < ncopies; j++)
2459     {
2460       phi = create_phi_node (SSA_NAME_VAR (vect_def), exit_bb);
2461       set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, loop_vinfo));
2462       if (j == 0)
2463         new_phi = phi;
2464       else
2465         {
2466           def = vect_get_vec_def_for_stmt_copy (dt, def);
2467           STMT_VINFO_RELATED_STMT (prev_phi_info) = phi;
2468         }
2469       SET_PHI_ARG_DEF (phi, single_exit (loop)->dest_idx, def);
2470       prev_phi_info = vinfo_for_stmt (phi);
2471     }
2472   exit_gsi = gsi_after_labels (exit_bb);
2473
2474   /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3 
2475          (i.e. when reduc_code is not available) and in the final adjustment
2476          code (if needed).  Also get the original scalar reduction variable as
2477          defined in the loop.  In case STMT is a "pattern-stmt" (i.e. - it 
2478          represents a reduction pattern), the tree-code and scalar-def are 
2479          taken from the original stmt that the pattern-stmt (STMT) replaces.  
2480          Otherwise (it is a regular reduction) - the tree-code and scalar-def
2481          are taken from STMT.  */ 
2482
2483   orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
2484   if (!orig_stmt)
2485     {
2486       /* Regular reduction  */
2487       orig_stmt = stmt;
2488     }
2489   else
2490     {
2491       /* Reduction pattern  */
2492       stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt);
2493       gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
2494       gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
2495     }
2496   code = gimple_assign_rhs_code (orig_stmt);
2497   scalar_dest = gimple_assign_lhs (orig_stmt);
2498   scalar_type = TREE_TYPE (scalar_dest);
2499   new_scalar_dest = vect_create_destination_var (scalar_dest, NULL);
2500   bitsize = TYPE_SIZE (scalar_type);
2501   bytesize = TYPE_SIZE_UNIT (scalar_type);
2502
2503
2504   /* In case this is a reduction in an inner-loop while vectorizing an outer
2505      loop - we don't need to extract a single scalar result at the end of the
2506      inner-loop.  The final vector of partial results will be used in the
2507      vectorized outer-loop, or reduced to a scalar result at the end of the
2508      outer-loop.  */
2509   if (nested_in_vect_loop)
2510     goto vect_finalize_reduction;
2511
2512   /* FORNOW */
2513   gcc_assert (ncopies == 1);
2514
2515   /* 2.3 Create the reduction code, using one of the three schemes described
2516          above.  */
2517
2518   if (reduc_code < NUM_TREE_CODES)
2519     {
2520       tree tmp;
2521
2522       /*** Case 1:  Create:
2523            v_out2 = reduc_expr <v_out1>  */
2524
2525       if (vect_print_dump_info (REPORT_DETAILS))
2526         fprintf (vect_dump, "Reduce using direct vector reduction.");
2527
2528       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2529       tmp = build1 (reduc_code, vectype,  PHI_RESULT (new_phi));
2530       epilog_stmt = gimple_build_assign (vec_dest, tmp);
2531       new_temp = make_ssa_name (vec_dest, epilog_stmt);
2532       gimple_assign_set_lhs (epilog_stmt, new_temp);
2533       gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2534
2535       extract_scalar_result = true;
2536     }
2537   else
2538     {
2539       enum tree_code shift_code = 0;
2540       bool have_whole_vector_shift = true;
2541       int bit_offset;
2542       int element_bitsize = tree_low_cst (bitsize, 1);
2543       int vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
2544       tree vec_temp;
2545
2546       if (optab_handler (vec_shr_optab, mode)->insn_code != CODE_FOR_nothing)
2547         shift_code = VEC_RSHIFT_EXPR;
2548       else
2549         have_whole_vector_shift = false;
2550
2551       /* Regardless of whether we have a whole vector shift, if we're
2552          emulating the operation via tree-vect-generic, we don't want
2553          to use it.  Only the first round of the reduction is likely
2554          to still be profitable via emulation.  */
2555       /* ??? It might be better to emit a reduction tree code here, so that
2556          tree-vect-generic can expand the first round via bit tricks.  */
2557       if (!VECTOR_MODE_P (mode))
2558         have_whole_vector_shift = false;
2559       else
2560         {
2561           optab optab = optab_for_tree_code (code, vectype, optab_default);
2562           if (optab_handler (optab, mode)->insn_code == CODE_FOR_nothing)
2563             have_whole_vector_shift = false;
2564         }
2565
2566       if (have_whole_vector_shift)
2567         {
2568           /*** Case 2: Create:
2569              for (offset = VS/2; offset >= element_size; offset/=2)
2570                 {
2571                   Create:  va' = vec_shift <va, offset>
2572                   Create:  va = vop <va, va'>
2573                 }  */
2574
2575           if (vect_print_dump_info (REPORT_DETAILS))
2576             fprintf (vect_dump, "Reduce using vector shifts");
2577
2578           vec_dest = vect_create_destination_var (scalar_dest, vectype);
2579           new_temp = PHI_RESULT (new_phi);
2580
2581           for (bit_offset = vec_size_in_bits/2;
2582                bit_offset >= element_bitsize;
2583                bit_offset /= 2)
2584             {
2585               tree bitpos = size_int (bit_offset);
2586               epilog_stmt = gimple_build_assign_with_ops (shift_code, vec_dest,
2587                                                           new_temp, bitpos);
2588               new_name = make_ssa_name (vec_dest, epilog_stmt);
2589               gimple_assign_set_lhs (epilog_stmt, new_name);
2590               gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2591
2592               epilog_stmt = gimple_build_assign_with_ops (code, vec_dest,
2593                                                           new_name, new_temp);
2594               new_temp = make_ssa_name (vec_dest, epilog_stmt);
2595               gimple_assign_set_lhs (epilog_stmt, new_temp);
2596               gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2597             }
2598
2599           extract_scalar_result = true;
2600         }
2601       else
2602         {
2603           tree rhs;
2604
2605           /*** Case 3: Create:  
2606              s = extract_field <v_out2, 0>
2607              for (offset = element_size; 
2608                   offset < vector_size; 
2609                   offset += element_size;)
2610                {
2611                  Create:  s' = extract_field <v_out2, offset>
2612                  Create:  s = op <s, s'>
2613                }  */
2614
2615           if (vect_print_dump_info (REPORT_DETAILS))
2616             fprintf (vect_dump, "Reduce using scalar code. ");
2617
2618           vec_temp = PHI_RESULT (new_phi);
2619           vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
2620           rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp, bitsize,
2621                          bitsize_zero_node);
2622           epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
2623           new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
2624           gimple_assign_set_lhs (epilog_stmt, new_temp);
2625           gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2626               
2627           for (bit_offset = element_bitsize;
2628                bit_offset < vec_size_in_bits;
2629                bit_offset += element_bitsize)
2630             { 
2631               tree bitpos = bitsize_int (bit_offset);
2632               tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp, bitsize,
2633                                  bitpos);
2634                 
2635               epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
2636               new_name = make_ssa_name (new_scalar_dest, epilog_stmt);
2637               gimple_assign_set_lhs (epilog_stmt, new_name);
2638               gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2639
2640               epilog_stmt = gimple_build_assign_with_ops (code,
2641                                                           new_scalar_dest,
2642                                                           new_name, new_temp);
2643               new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
2644               gimple_assign_set_lhs (epilog_stmt, new_temp);
2645               gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2646             }
2647
2648           extract_scalar_result = false;
2649         }
2650     }
2651
2652   /* 2.4  Extract the final scalar result.  Create:
2653          s_out3 = extract_field <v_out2, bitpos>  */
2654   
2655   if (extract_scalar_result)
2656     {
2657       tree rhs;
2658
2659       gcc_assert (!nested_in_vect_loop);
2660       if (vect_print_dump_info (REPORT_DETAILS))
2661         fprintf (vect_dump, "extract scalar result");
2662
2663       if (BYTES_BIG_ENDIAN)
2664         bitpos = size_binop (MULT_EXPR,
2665                        bitsize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1),
2666                        TYPE_SIZE (scalar_type));
2667       else
2668         bitpos = bitsize_zero_node;
2669
2670       rhs = build3 (BIT_FIELD_REF, scalar_type, new_temp, bitsize, bitpos);
2671       epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
2672       new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
2673       gimple_assign_set_lhs (epilog_stmt, new_temp);
2674       gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2675     }
2676
2677 vect_finalize_reduction:
2678
2679   /* 2.5 Adjust the final result by the initial value of the reduction
2680          variable. (When such adjustment is not needed, then
2681          'adjustment_def' is zero).  For example, if code is PLUS we create:
2682          new_temp = loop_exit_def + adjustment_def  */
2683
2684   if (adjustment_def)
2685     {
2686       if (nested_in_vect_loop)
2687         {
2688           gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) == VECTOR_TYPE);
2689           expr = build2 (code, vectype, PHI_RESULT (new_phi), adjustment_def);
2690           new_dest = vect_create_destination_var (scalar_dest, vectype);
2691         }
2692       else
2693         {
2694           gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) != VECTOR_TYPE);
2695           expr = build2 (code, scalar_type, new_temp, adjustment_def);
2696           new_dest = vect_create_destination_var (scalar_dest, scalar_type);
2697         }
2698       epilog_stmt = gimple_build_assign (new_dest, expr);
2699       new_temp = make_ssa_name (new_dest, epilog_stmt);
2700       gimple_assign_set_lhs (epilog_stmt, new_temp);
2701       SSA_NAME_DEF_STMT (new_temp) = epilog_stmt;
2702       gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
2703     }
2704
2705
2706   /* 2.6  Handle the loop-exit phi  */
2707
2708   /* Replace uses of s_out0 with uses of s_out3:
2709      Find the loop-closed-use at the loop exit of the original scalar result.
2710      (The reduction result is expected to have two immediate uses - one at the 
2711      latch block, and one at the loop exit).  */
2712   phis = VEC_alloc (gimple, heap, 10);
2713   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
2714     {
2715       if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
2716         {
2717           exit_phi = USE_STMT (use_p);
2718           VEC_quick_push (gimple, phis, exit_phi);
2719         }
2720     }
2721   /* We expect to have found an exit_phi because of loop-closed-ssa form.  */
2722   gcc_assert (!VEC_empty (gimple, phis));
2723
2724   for (i = 0; VEC_iterate (gimple, phis, i, exit_phi); i++)
2725     {
2726       if (nested_in_vect_loop)
2727         {
2728           stmt_vec_info stmt_vinfo = vinfo_for_stmt (exit_phi);
2729
2730           /* FORNOW. Currently not supporting the case that an inner-loop
2731              reduction is not used in the outer-loop (but only outside the
2732              outer-loop).  */
2733           gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo) 
2734                       && !STMT_VINFO_LIVE_P (stmt_vinfo));
2735
2736           epilog_stmt = adjustment_def ? epilog_stmt : new_phi;
2737           STMT_VINFO_VEC_STMT (stmt_vinfo) = epilog_stmt;
2738           set_vinfo_for_stmt (epilog_stmt, 
2739                               new_stmt_vec_info (epilog_stmt, loop_vinfo));
2740           if (adjustment_def)
2741             STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt)) =
2742                 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi));
2743           continue;
2744         }
2745
2746       /* Replace the uses:  */
2747       orig_name = PHI_RESULT (exit_phi);
2748       FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
2749         FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
2750           SET_USE (use_p, new_temp);
2751     }
2752   VEC_free (gimple, heap, phis);
2753
2754
2755
2756 /* Function vectorizable_reduction.
2757
2758    Check if STMT performs a reduction operation that can be vectorized.
2759    If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2760    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2761    Return FALSE if not a vectorizable STMT, TRUE otherwise.
2762
2763    This function also handles reduction idioms (patterns) that have been 
2764    recognized in advance during vect_pattern_recog. In this case, STMT may be
2765    of this form:
2766      X = pattern_expr (arg0, arg1, ..., X)
2767    and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
2768    sequence that had been detected and replaced by the pattern-stmt (STMT).
2769   
2770    In some cases of reduction patterns, the type of the reduction variable X is
2771    different than the type of the other arguments of STMT.
2772    In such cases, the vectype that is used when transforming STMT into a vector
2773    stmt is different than the vectype that is used to determine the
2774    vectorization factor, because it consists of a different number of elements 
2775    than the actual number of elements that are being operated upon in parallel.
2776
2777    For example, consider an accumulation of shorts into an int accumulator.
2778    On some targets it's possible to vectorize this pattern operating on 8
2779    shorts at a time (hence, the vectype for purposes of determining the
2780    vectorization factor should be V8HI); on the other hand, the vectype that
2781    is used to create the vector form is actually V4SI (the type of the result).
2782
2783    Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
2784    indicates what is the actual level of parallelism (V8HI in the example), so
2785    that the right vectorization factor would be derived. This vectype
2786    corresponds to the type of arguments to the reduction stmt, and should *NOT*
2787    be used to create the vectorized stmt. The right vectype for the vectorized
2788    stmt is obtained from the type of the result X:
2789         get_vectype_for_scalar_type (TREE_TYPE (X))
2790
2791    This means that, contrary to "regular" reductions (or "regular" stmts in
2792    general), the following equation:
2793       STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
2794    does *NOT* necessarily hold for reduction patterns.  */
2795
2796 bool
2797 vectorizable_reduction (gimple stmt, gimple_stmt_iterator *gsi,
2798                         gimple *vec_stmt)
2799 {
2800   tree vec_dest;
2801   tree scalar_dest;
2802   tree loop_vec_def0 = NULL_TREE, loop_vec_def1 = NULL_TREE;
2803   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2804   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2805   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2806   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2807   enum tree_code code, orig_code, epilog_reduc_code = 0;
2808   enum machine_mode vec_mode;
2809   int op_type;
2810   optab optab, reduc_optab;
2811   tree new_temp = NULL_TREE;
2812   tree def;
2813   gimple def_stmt;
2814   enum vect_def_type dt;
2815   gimple new_phi = NULL;
2816   tree scalar_type;
2817   bool is_simple_use;
2818   gimple orig_stmt;
2819   stmt_vec_info orig_stmt_info;
2820   tree expr = NULL_TREE;
2821   int i;
2822   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
2823   int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2824   int epilog_copies;
2825   stmt_vec_info prev_stmt_info, prev_phi_info;
2826   gimple first_phi = NULL;
2827   bool single_defuse_cycle = false;
2828   tree reduc_def;
2829   gimple new_stmt = NULL;
2830   int j;
2831   tree ops[3];
2832
2833   if (nested_in_vect_loop_p (loop, stmt))
2834     loop = loop->inner;
2835
2836   gcc_assert (ncopies >= 1);
2837
2838   /* FORNOW: SLP not supported.  */
2839   if (STMT_SLP_TYPE (stmt_info))
2840     return false;
2841
2842   /* 1. Is vectorizable reduction?  */
2843
2844   /* Not supportable if the reduction variable is used in the loop.  */
2845   if (STMT_VINFO_RELEVANT (stmt_info) > vect_used_in_outer)
2846     return false;
2847
2848   /* Reductions that are not used even in an enclosing outer-loop,
2849      are expected to be "live" (used out of the loop).  */
2850   if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_loop
2851       && !STMT_VINFO_LIVE_P (stmt_info))
2852     return false;
2853
2854   /* Make sure it was already recognized as a reduction computation.  */
2855   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_reduction_def)
2856     return false;
2857
2858   /* 2. Has this been recognized as a reduction pattern? 
2859
2860      Check if STMT represents a pattern that has been recognized
2861      in earlier analysis stages.  For stmts that represent a pattern,
2862      the STMT_VINFO_RELATED_STMT field records the last stmt in
2863      the original sequence that constitutes the pattern.  */
2864
2865   orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
2866   if (orig_stmt)
2867     {
2868       orig_stmt_info = vinfo_for_stmt (orig_stmt);
2869       gcc_assert (STMT_VINFO_RELATED_STMT (orig_stmt_info) == stmt);
2870       gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info));
2871       gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info));
2872     }
2873  
2874   /* 3. Check the operands of the operation. The first operands are defined
2875         inside the loop body. The last operand is the reduction variable,
2876         which is defined by the loop-header-phi.  */
2877
2878   gcc_assert (is_gimple_assign (stmt));
2879
2880   /* Flatten RHS */
2881   switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2882     {
2883     case GIMPLE_SINGLE_RHS:
2884       op_type = TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt));
2885       if (op_type == ternary_op)
2886         {
2887           tree rhs = gimple_assign_rhs1 (stmt);
2888           ops[0] = TREE_OPERAND (rhs, 0);
2889           ops[1] = TREE_OPERAND (rhs, 1);
2890           ops[2] = TREE_OPERAND (rhs, 2);
2891           code = TREE_CODE (rhs);
2892         }
2893       else
2894         return false;
2895       break;
2896
2897     case GIMPLE_BINARY_RHS:
2898       code = gimple_assign_rhs_code (stmt);
2899       op_type = TREE_CODE_LENGTH (code);
2900       gcc_assert (op_type == binary_op);
2901       ops[0] = gimple_assign_rhs1 (stmt);
2902       ops[1] = gimple_assign_rhs2 (stmt);
2903       break;
2904
2905     case GIMPLE_UNARY_RHS:
2906       return false;
2907
2908     default:
2909       gcc_unreachable ();
2910     }
2911
2912   scalar_dest = gimple_assign_lhs (stmt);
2913   scalar_type = TREE_TYPE (scalar_dest);
2914   if (!POINTER_TYPE_P (scalar_type) && !INTEGRAL_TYPE_P (scalar_type) 
2915       && !SCALAR_FLOAT_TYPE_P (scalar_type))
2916     return false;
2917
2918   /* All uses but the last are expected to be defined in the loop.
2919      The last use is the reduction variable.  */
2920   for (i = 0; i < op_type-1; i++)
2921     {
2922       is_simple_use = vect_is_simple_use (ops[i], loop_vinfo, &def_stmt,
2923                                           &def, &dt);
2924       gcc_assert (is_simple_use);
2925       if (dt != vect_loop_def
2926           && dt != vect_invariant_def
2927           && dt != vect_constant_def
2928           && dt != vect_induction_def)
2929         return false;
2930     }
2931
2932   is_simple_use = vect_is_simple_use (ops[i], loop_vinfo, &def_stmt, &def, &dt);
2933   gcc_assert (is_simple_use);
2934   gcc_assert (dt == vect_reduction_def);
2935   gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
2936   if (orig_stmt) 
2937     gcc_assert (orig_stmt == vect_is_simple_reduction (loop_vinfo, def_stmt));
2938   else
2939     gcc_assert (stmt == vect_is_simple_reduction (loop_vinfo, def_stmt));
2940   
2941   if (STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
2942     return false;
2943
2944   /* 4. Supportable by target?  */
2945
2946   /* 4.1. check support for the operation in the loop  */
2947   optab = optab_for_tree_code (code, vectype, optab_default);
2948   if (!optab)
2949     {
2950       if (vect_print_dump_info (REPORT_DETAILS))
2951         fprintf (vect_dump, "no optab.");
2952       return false;
2953     }
2954   vec_mode = TYPE_MODE (vectype);
2955   if (optab_handler (optab, vec_mode)->insn_code == CODE_FOR_nothing)
2956     {
2957       if (vect_print_dump_info (REPORT_DETAILS))
2958         fprintf (vect_dump, "op not supported by target.");
2959       if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
2960           || LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2961              < vect_min_worthwhile_factor (code))
2962         return false;
2963       if (vect_print_dump_info (REPORT_DETAILS))
2964         fprintf (vect_dump, "proceeding using word mode.");
2965     }
2966
2967   /* Worthwhile without SIMD support?  */
2968   if (!VECTOR_MODE_P (TYPE_MODE (vectype))
2969       && LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2970          < vect_min_worthwhile_factor (code))
2971     {
2972       if (vect_print_dump_info (REPORT_DETAILS))
2973         fprintf (vect_dump, "not worthwhile without SIMD support.");
2974       return false;
2975     }
2976
2977   /* 4.2. Check support for the epilog operation.
2978
2979           If STMT represents a reduction pattern, then the type of the
2980           reduction variable may be different than the type of the rest
2981           of the arguments.  For example, consider the case of accumulation
2982           of shorts into an int accumulator; The original code:
2983                         S1: int_a = (int) short_a;
2984           orig_stmt->   S2: int_acc = plus <int_a ,int_acc>;
2985
2986           was replaced with:
2987                         STMT: int_acc = widen_sum <short_a, int_acc>
2988
2989           This means that:
2990           1. The tree-code that is used to create the vector operation in the 
2991              epilog code (that reduces the partial results) is not the 
2992              tree-code of STMT, but is rather the tree-code of the original 
2993              stmt from the pattern that STMT is replacing. I.e, in the example 
2994              above we want to use 'widen_sum' in the loop, but 'plus' in the 
2995              epilog.
2996           2. The type (mode) we use to check available target support
2997              for the vector operation to be created in the *epilog*, is 
2998              determined by the type of the reduction variable (in the example 
2999              above we'd check this: plus_optab[vect_int_mode]).
3000              However the type (mode) we use to check available target support
3001              for the vector operation to be created *inside the loop*, is
3002              determined by the type of the other arguments to STMT (in the
3003              example we'd check this: widen_sum_optab[vect_short_mode]).
3004   
3005           This is contrary to "regular" reductions, in which the types of all 
3006           the arguments are the same as the type of the reduction variable. 
3007           For "regular" reductions we can therefore use the same vector type 
3008           (and also the same tree-code) when generating the epilog code and
3009           when generating the code inside the loop.  */
3010
3011   if (orig_stmt)
3012     {
3013       /* This is a reduction pattern: get the vectype from the type of the
3014          reduction variable, and get the tree-code from orig_stmt.  */
3015       orig_code = gimple_assign_rhs_code (orig_stmt);
3016       vectype = get_vectype_for_scalar_type (TREE_TYPE (def));
3017       if (!vectype)
3018         {
3019           if (vect_print_dump_info (REPORT_DETAILS))
3020             {
3021               fprintf (vect_dump, "unsupported data-type ");
3022               print_generic_expr (vect_dump, TREE_TYPE (def), TDF_SLIM);
3023             }
3024           return false;
3025         }
3026
3027       vec_mode = TYPE_MODE (vectype);
3028     }
3029   else
3030     {
3031       /* Regular reduction: use the same vectype and tree-code as used for
3032          the vector code inside the loop can be used for the epilog code. */
3033       orig_code = code;
3034     }
3035
3036   if (!reduction_code_for_scalar_code (orig_code, &epilog_reduc_code))
3037     return false;
3038   reduc_optab = optab_for_tree_code (epilog_reduc_code, vectype, optab_default);
3039   if (!reduc_optab)
3040     {
3041       if (vect_print_dump_info (REPORT_DETAILS))
3042         fprintf (vect_dump, "no optab for reduction.");
3043       epilog_reduc_code = NUM_TREE_CODES;
3044     }
3045   if (optab_handler (reduc_optab, vec_mode)->insn_code == CODE_FOR_nothing)
3046     {
3047       if (vect_print_dump_info (REPORT_DETAILS))
3048         fprintf (vect_dump, "reduc op not supported by target.");
3049       epilog_reduc_code = NUM_TREE_CODES;
3050     }
3051  
3052   if (!vec_stmt) /* transformation not required.  */
3053     {
3054       STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3055       if (!vect_model_reduction_cost (stmt_info, epilog_reduc_code, ncopies))
3056         return false;
3057       return true;
3058     }
3059
3060   /** Transform.  **/
3061
3062   if (vect_print_dump_info (REPORT_DETAILS))
3063     fprintf (vect_dump, "transform reduction.");
3064
3065   /* Create the destination vector  */
3066   vec_dest = vect_create_destination_var (scalar_dest, vectype);
3067
3068   /* In case the vectorization factor (VF) is bigger than the number
3069      of elements that we can fit in a vectype (nunits), we have to generate
3070      more than one vector stmt - i.e - we need to "unroll" the
3071      vector stmt by a factor VF/nunits.  For more details see documentation
3072      in vectorizable_operation.  */
3073
3074   /* If the reduction is used in an outer loop we need to generate
3075      VF intermediate results, like so (e.g. for ncopies=2):
3076         r0 = phi (init, r0)
3077         r1 = phi (init, r1)
3078         r0 = x0 + r0;
3079         r1 = x1 + r1;
3080     (i.e. we generate VF results in 2 registers).
3081     In this case we have a separate def-use cycle for each copy, and therefore
3082     for each copy we get the vector def for the reduction variable from the
3083     respective phi node created for this copy.
3084
3085     Otherwise (the reduction is unused in the loop nest), we can combine
3086     together intermediate results, like so (e.g. for ncopies=2):
3087         r = phi (init, r)
3088         r = x0 + r;
3089         r = x1 + r;
3090    (i.e. we generate VF/2 results in a single register).
3091    In this case for each copy we get the vector def for the reduction variable
3092    from the vectorized reduction operation generated in the previous iteration.
3093   */
3094
3095   if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_loop)
3096     {
3097       single_defuse_cycle = true;
3098       epilog_copies = 1;
3099     }
3100   else
3101     epilog_copies = ncopies;
3102
3103   prev_stmt_info = NULL;
3104   prev_phi_info = NULL;
3105   for (j = 0; j < ncopies; j++)
3106     {
3107       if (j == 0 || !single_defuse_cycle)
3108         {
3109           /* Create the reduction-phi that defines the reduction-operand.  */
3110           new_phi = create_phi_node (vec_dest, loop->header);
3111           set_vinfo_for_stmt (new_phi, new_stmt_vec_info (new_phi, loop_vinfo));
3112         }
3113
3114       /* Handle uses.  */
3115       if (j == 0)
3116         {
3117           loop_vec_def0 = vect_get_vec_def_for_operand (ops[0], stmt, NULL);
3118           if (op_type == ternary_op)
3119             {
3120               loop_vec_def1 = vect_get_vec_def_for_operand (ops[1], stmt, NULL);
3121             }
3122
3123           /* Get the vector def for the reduction variable from the phi node */
3124           reduc_def = PHI_RESULT (new_phi);
3125           first_phi = new_phi;
3126         }
3127       else
3128         {
3129           enum vect_def_type dt = vect_unknown_def_type; /* Dummy */
3130           loop_vec_def0 = vect_get_vec_def_for_stmt_copy (dt, loop_vec_def0);
3131           if (op_type == ternary_op)
3132             loop_vec_def1 = vect_get_vec_def_for_stmt_copy (dt, loop_vec_def1);
3133
3134           if (single_defuse_cycle)
3135             reduc_def = gimple_assign_lhs (new_stmt);
3136           else
3137             reduc_def = PHI_RESULT (new_phi);
3138
3139           STMT_VINFO_RELATED_STMT (prev_phi_info) = new_phi;
3140         }
3141
3142       /* Arguments are ready. create the new vector stmt.  */
3143       if (op_type == binary_op)
3144         expr = build2 (code, vectype, loop_vec_def0, reduc_def);
3145       else
3146         expr = build3 (code, vectype, loop_vec_def0, loop_vec_def1, 
3147                        reduc_def);
3148       new_stmt = gimple_build_assign (vec_dest, expr);
3149       new_temp = make_ssa_name (vec_dest, new_stmt);
3150       gimple_assign_set_lhs (new_stmt, new_temp);
3151       vect_finish_stmt_generation (stmt, new_stmt, gsi);
3152
3153       if (j == 0)
3154         STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3155       else
3156         STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3157       prev_stmt_info = vinfo_for_stmt (new_stmt);
3158       prev_phi_info = vinfo_for_stmt (new_phi);
3159     }
3160
3161   /* Finalize the reduction-phi (set its arguments) and create the
3162      epilog reduction code.  */
3163   if (!single_defuse_cycle)
3164     new_temp = gimple_assign_lhs (*vec_stmt);
3165   vect_create_epilog_for_reduction (new_temp, stmt, epilog_copies,
3166                                     epilog_reduc_code, first_phi);
3167   return true;
3168 }
3169
3170 /* Checks if CALL can be vectorized in type VECTYPE.  Returns
3171    a function declaration if the target has a vectorized version
3172    of the function, or NULL_TREE if the function cannot be vectorized.  */
3173
3174 tree
3175 vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
3176 {
3177   tree fndecl = gimple_call_fndecl (call);
3178   enum built_in_function code;
3179
3180   /* We only handle functions that do not read or clobber memory -- i.e.
3181      const or novops ones.  */
3182   if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
3183     return NULL_TREE;
3184
3185   if (!fndecl
3186       || TREE_CODE (fndecl) != FUNCTION_DECL
3187       || !DECL_BUILT_IN (fndecl))
3188     return NULL_TREE;
3189
3190   code = DECL_FUNCTION_CODE (fndecl);
3191   return targetm.vectorize.builtin_vectorized_function (code, vectype_out,
3192                                                         vectype_in);
3193 }
3194
3195 /* Function vectorizable_call.
3196
3197    Check if STMT performs a function call that can be vectorized. 
3198    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
3199    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3200    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
3201
3202 bool
3203 vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt)
3204 {
3205   tree vec_dest;
3206   tree scalar_dest;
3207   tree op, type;
3208   tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3209   stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
3210   tree vectype_out, vectype_in;
3211   int nunits_in;
3212   int nunits_out;
3213   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3214   tree fndecl, new_temp, def, rhs_type, lhs_type;
3215   gimple def_stmt;
3216   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3217   gimple new_stmt;
3218   int ncopies, j;
3219   VEC(tree, heap) *vargs = NULL;
3220   enum { NARROW, NONE, WIDEN } modifier;
3221   size_t i, nargs;
3222
3223   if (!STMT_VINFO_RELEVANT_P (stmt_info))
3224     return false;
3225
3226   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
3227     return false;
3228
3229   /* FORNOW: SLP not supported.  */
3230   if (STMT_SLP_TYPE (stmt_info))
3231     return false;
3232
3233   /* Is STMT a vectorizable call?   */
3234   if (!is_gimple_call (stmt))
3235     return false;
3236
3237   if (TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3238     return false;
3239
3240   /* Process function arguments.  */
3241   rhs_type = NULL_TREE;
3242   nargs = gimple_call_num_args (stmt);
3243
3244   /* Bail out if the function has more than two arguments, we
3245      do not have interesting builtin functions to vectorize with
3246      more than two arguments.  No arguments is also not good.  */
3247   if (nargs == 0 || nargs > 2)
3248     return false;
3249
3250   for (i = 0; i < nargs; i++)
3251     {
3252       op = gimple_call_arg (stmt, i);
3253
3254       /* We can only handle calls with arguments of the same type.  */
3255       if (rhs_type
3256           && rhs_type != TREE_TYPE (op))
3257         {
3258           if (vect_print_dump_info (REPORT_DETAILS))
3259             fprintf (vect_dump, "argument types differ.");
3260           return false;
3261         }
3262       rhs_type = TREE_TYPE (op);
3263
3264       if (!vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt[i]))
3265         {
3266           if (vect_print_dump_info (REPORT_DETAILS))
3267             fprintf (vect_dump, "use not simple.");
3268           return false;
3269         }
3270     }
3271
3272   vectype_in = get_vectype_for_scalar_type (rhs_type);
3273   if (!vectype_in)
3274     return false;
3275   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3276
3277   lhs_type = TREE_TYPE (gimple_call_lhs (stmt));
3278   vectype_out = get_vectype_for_scalar_type (lhs_type);
3279   if (!vectype_out)
3280     return false;
3281   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3282
3283   /* FORNOW */
3284   if (nunits_in == nunits_out / 2)
3285     modifier = NARROW;
3286   else if (nunits_out == nunits_in)
3287     modifier = NONE;
3288   else if (nunits_out == nunits_in / 2)
3289     modifier = WIDEN;
3290   else
3291     return false;
3292
3293   /* For now, we only vectorize functions if a target specific builtin
3294      is available.  TODO -- in some cases, it might be profitable to
3295      insert the calls for pieces of the vector, in order to be able
3296      to vectorize other operations in the loop.  */
3297   fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
3298   if (fndecl == NULL_TREE)
3299     {
3300       if (vect_print_dump_info (REPORT_DETAILS))
3301         fprintf (vect_dump, "function is not vectorizable.");
3302
3303       return false;
3304     }
3305
3306   gcc_assert (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS));
3307
3308   if (modifier == NARROW)
3309     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3310   else
3311     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3312
3313   /* Sanity check: make sure that at least one copy of the vectorized stmt
3314      needs to be generated.  */
3315   gcc_assert (ncopies >= 1);
3316
3317   if (!vec_stmt) /* transformation not required.  */
3318     {
3319       STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
3320       if (vect_print_dump_info (REPORT_DETAILS))
3321         fprintf (vect_dump, "=== vectorizable_call ===");
3322       vect_model_simple_cost (stmt_info, ncopies, dt, NULL);
3323       return true;
3324     }
3325
3326   /** Transform.  **/
3327
3328   if (vect_print_dump_info (REPORT_DETAILS))
3329     fprintf (vect_dump, "transform operation.");
3330
3331   /* Handle def.  */
3332   scalar_dest = gimple_call_lhs (stmt);
3333   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3334
3335   prev_stmt_info = NULL;
3336   switch (modifier)
3337     {
3338     case NONE:
3339       for (j = 0; j < ncopies; ++j)
3340         {
3341           /* Build argument list for the vectorized call.  */
3342           if (j == 0)
3343             vargs = VEC_alloc (tree, heap, nargs);
3344           else
3345             VEC_truncate (tree, vargs, 0);
3346
3347           for (i = 0; i < nargs; i++)
3348             {
3349               op = gimple_call_arg (stmt, i);
3350               if (j == 0)
3351                 vec_oprnd0
3352                   = vect_get_vec_def_for_operand (op, stmt, NULL);
3353               else
3354                 vec_oprnd0
3355                   = vect_get_vec_def_for_stmt_copy (dt[nargs], vec_oprnd0);
3356
3357               VEC_quick_push (tree, vargs, vec_oprnd0);
3358             }
3359
3360           new_stmt = gimple_build_call_vec (fndecl, vargs);
3361           new_temp = make_ssa_name (vec_dest, new_stmt);
3362           gimple_call_set_lhs (new_stmt, new_temp);
3363
3364           vect_finish_stmt_generation (stmt, new_stmt, gsi);
3365
3366           if (j == 0)
3367             STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3368           else
3369             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3370
3371           prev_stmt_info = vinfo_for_stmt (new_stmt);
3372         }
3373
3374       break;
3375
3376     case NARROW:
3377       for (j = 0; j < ncopies; ++j)
3378         {
3379           /* Build argument list for the vectorized call.  */
3380           if (j == 0)
3381             vargs = VEC_alloc (tree, heap, nargs * 2);
3382           else
3383             VEC_truncate (tree, vargs, 0);
3384
3385           for (i = 0; i < nargs; i++)
3386             {
3387               op = gimple_call_arg (stmt, i);
3388               if (j == 0)
3389                 {
3390                   vec_oprnd0
3391                     = vect_get_vec_def_for_operand (op, stmt, NULL);
3392                   vec_oprnd1
3393                     = vect_get_vec_def_for_stmt_copy (dt[nargs], vec_oprnd0);
3394                 }
3395               else
3396                 {
3397                   vec_oprnd0
3398                     = vect_get_vec_def_for_stmt_copy (dt[nargs], vec_oprnd1);
3399                   vec_oprnd1
3400                     = vect_get_vec_def_for_stmt_copy (dt[nargs], vec_oprnd0);
3401                 }
3402
3403               VEC_quick_push (tree, vargs, vec_oprnd0);
3404               VEC_quick_push (tree, vargs, vec_oprnd1);
3405             }
3406
3407           new_stmt = gimple_build_call_vec (fndecl, vargs);
3408           new_temp = make_ssa_name (vec_dest, new_stmt);
3409           gimple_call_set_lhs (new_stmt, new_temp);
3410
3411           vect_finish_stmt_generation (stmt, new_stmt, gsi);
3412
3413           if (j == 0)
3414             STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3415           else
3416             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3417
3418           prev_stmt_info = vinfo_for_stmt (new_stmt);
3419         }
3420
3421       *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3422
3423       break;
3424
3425     case WIDEN:
3426       /* No current target implements this case.  */
3427       return false;
3428     }
3429
3430   VEC_free (tree, heap, vargs);
3431
3432   /* The call in STMT might prevent it from being removed in dce.
3433      We however cannot remove it here, due to the way the ssa name
3434      it defines is mapped to the new definition.  So just replace
3435      rhs of the statement with something harmless.  */
3436
3437   type = TREE_TYPE (scalar_dest);
3438   new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
3439                                   fold_convert (type, integer_zero_node));
3440   set_vinfo_for_stmt (new_stmt, stmt_info);
3441   set_vinfo_for_stmt (stmt, NULL);
3442   STMT_VINFO_STMT (stmt_info) = new_stmt;
3443   gsi_replace (gsi, new_stmt, false);
3444   SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3445
3446   return true;
3447 }
3448
3449
3450 /* Function vect_gen_widened_results_half
3451
3452    Create a vector stmt whose code, type, number of arguments, and result
3453    variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are 
3454    VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
3455    In the case that CODE is a CALL_EXPR, this means that a call to DECL
3456    needs to be created (DECL is a function-decl of a target-builtin).
3457    STMT is the original scalar stmt that we are vectorizing.  */
3458
3459 static gimple
3460 vect_gen_widened_results_half (enum tree_code code,
3461                                tree decl,
3462                                tree vec_oprnd0, tree vec_oprnd1, int op_type,
3463                                tree vec_dest, gimple_stmt_iterator *gsi,
3464                                gimple stmt)
3465
3466   gimple new_stmt;
3467   tree new_temp; 
3468   tree sym; 
3469   ssa_op_iter iter;
3470  
3471   /* Generate half of the widened result:  */ 
3472   if (code == CALL_EXPR) 
3473     {  
3474       /* Target specific support  */ 
3475       if (op_type == binary_op)
3476         new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3477       else
3478         new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3479       new_temp = make_ssa_name (vec_dest, new_stmt);
3480       gimple_call_set_lhs (new_stmt, new_temp);
3481     } 
3482   else 
3483     {
3484       /* Generic support */ 
3485       gcc_assert (op_type == TREE_CODE_LENGTH (code)); 
3486       if (op_type != binary_op)
3487         vec_oprnd1 = NULL;
3488       new_stmt = gimple_build_assign_with_ops (code, vec_dest, vec_oprnd0,
3489                                                vec_oprnd1);
3490       new_temp = make_ssa_name (vec_dest, new_stmt);
3491       gimple_assign_set_lhs (new_stmt, new_temp);
3492     } 
3493   vect_finish_stmt_generation (stmt, new_stmt, gsi);
3494
3495   if (code == CALL_EXPR)
3496     {
3497       FOR_EACH_SSA_TREE_OPERAND (sym, new_stmt, iter, SSA_OP_ALL_VIRTUALS)
3498         {
3499           if (TREE_CODE (sym) == SSA_NAME)
3500             sym = SSA_NAME_VAR (sym);
3501           mark_sym_for_renaming (sym);
3502         }
3503     }
3504
3505   return new_stmt;
3506 }
3507
3508
3509 /* Check if STMT performs a conversion operation, that can be vectorized. 
3510    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
3511    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3512    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
3513
3514 bool
3515 vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
3516                          gimple *vec_stmt, slp_tree slp_node)
3517 {
3518   tree vec_dest;
3519   tree scalar_dest;
3520   tree op0;
3521   tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3522   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3523   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3524   enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
3525   tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3526   tree new_temp;
3527   tree def;
3528   gimple def_stmt;
3529   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3530   gimple new_stmt = NULL;
3531   stmt_vec_info prev_stmt_info;
3532   int nunits_in;
3533   int nunits_out;
3534   tree vectype_out, vectype_in;
3535   int ncopies, j;
3536   tree expr;
3537   tree rhs_type, lhs_type;
3538   tree builtin_decl;
3539   enum { NARROW, NONE, WIDEN } modifier;
3540   int i;
3541   VEC(tree,heap) *vec_oprnds0 = NULL;
3542   tree vop0;
3543   tree integral_type;
3544   VEC(tree,heap) *dummy = NULL;
3545   int dummy_int;
3546
3547   /* Is STMT a vectorizable conversion?   */
3548
3549   if (!STMT_VINFO_RELEVANT_P (stmt_info))
3550     return false;
3551
3552   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
3553     return false;
3554
3555   if (!is_gimple_assign (stmt))
3556     return false;
3557
3558   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3559     return false;
3560
3561   code = gimple_assign_rhs_code (stmt);
3562   if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3563     return false;
3564
3565   /* Check types of lhs and rhs.  */
3566   op0 = gimple_assign_rhs1 (stmt);
3567   rhs_type = TREE_TYPE (op0);
3568   vectype_in = get_vectype_for_scalar_type (rhs_type);
3569   if (!vectype_in)
3570     return false;
3571   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3572
3573   scalar_dest = gimple_assign_lhs (stmt);
3574   lhs_type = TREE_TYPE (scalar_dest);
3575   vectype_out = get_vectype_for_scalar_type (lhs_type);
3576   if (!vectype_out)
3577     return false;
3578   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3579
3580   /* FORNOW */
3581   if (nunits_in == nunits_out / 2)
3582     modifier = NARROW;
3583   else if (nunits_out == nunits_in)
3584     modifier = NONE;
3585   else if (nunits_out == nunits_in / 2)
3586     modifier = WIDEN;
3587   else
3588     return false;
3589
3590   if (modifier == NONE)
3591     gcc_assert (STMT_VINFO_VECTYPE (stmt_info) == vectype_out);
3592
3593   /* Bail out if the types are both integral or non-integral.  */
3594   if ((INTEGRAL_TYPE_P (rhs_type) && INTEGRAL_TYPE_P (lhs_type))
3595       || (!INTEGRAL_TYPE_P (rhs_type) && !INTEGRAL_TYPE_P (lhs_type)))
3596     return false;
3597
3598   integral_type = INTEGRAL_TYPE_P (rhs_type) ? vectype_in : vectype_out;
3599
3600   if (modifier == NARROW)
3601     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3602   else
3603     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3604
3605   /* FORNOW: SLP with multiple types is not supported. The SLP analysis verifies
3606      this, so we can safely override NCOPIES with 1 here.  */
3607   if (slp_node)
3608     ncopies = 1;
3609   
3610   /* Sanity check: make sure that at least one copy of the vectorized stmt
3611      needs to be generated.  */
3612   gcc_assert (ncopies >= 1);
3613
3614   /* Check the operands of the operation.  */
3615   if (!vect_is_simple_use (op0, loop_vinfo, &def_stmt, &def, &dt[0]))
3616     {
3617       if (vect_print_dump_info (REPORT_DETAILS))
3618         fprintf (vect_dump, "use not simple.");
3619       return false;
3620     }
3621
3622   /* Supportable by target?  */
3623   if ((modifier == NONE
3624        && !targetm.vectorize.builtin_conversion (code, integral_type))
3625       || (modifier == WIDEN
3626           && !supportable_widening_operation (code, stmt, vectype_in,
3627                                               &decl1, &decl2,
3628                                               &code1, &code2,
3629                                               &dummy_int, &dummy))
3630       || (modifier == NARROW
3631           && !supportable_narrowing_operation (code, stmt, vectype_in,
3632                                                &code1, &dummy_int, &dummy)))
3633     {
3634       if (vect_print_dump_info (REPORT_DETAILS))
3635         fprintf (vect_dump, "conversion not supported by target.");
3636       return false;
3637     }
3638
3639   if (modifier != NONE)
3640     {
3641       STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
3642       /* FORNOW: SLP not supported.  */
3643       if (STMT_SLP_TYPE (stmt_info))
3644         return false;      
3645     }
3646
3647   if (!vec_stmt)                /* transformation not required.  */
3648     {
3649       STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
3650       return true;
3651     }
3652
3653   /** Transform.  **/
3654   if (vect_print_dump_info (REPORT_DETAILS))
3655     fprintf (vect_dump, "transform conversion.");
3656
3657   /* Handle def.  */
3658   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3659
3660   if (modifier == NONE && !slp_node)
3661     vec_oprnds0 = VEC_alloc (tree, heap, 1);
3662
3663   prev_stmt_info = NULL;
3664   switch (modifier)
3665     {
3666     case NONE:
3667       for (j = 0; j < ncopies; j++)
3668         {
3669           tree sym;
3670           ssa_op_iter iter;
3671
3672           if (j == 0)
3673             vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node); 
3674           else
3675             vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3676
3677           builtin_decl =
3678             targetm.vectorize.builtin_conversion (code, integral_type);
3679           for (i = 0; VEC_iterate (tree, vec_oprnds0, i, vop0); i++)
3680             { 
3681               /* Arguments are ready. create the new vector stmt.  */
3682               new_stmt = gimple_build_call (builtin_decl, 1, vop0);
3683               new_temp = make_ssa_name (vec_dest, new_stmt);
3684               gimple_call_set_lhs (new_stmt, new_temp);
3685               vect_finish_stmt_generation (stmt, new_stmt, gsi);
3686               FOR_EACH_SSA_TREE_OPERAND (sym, new_stmt, iter, 
3687                                          SSA_OP_ALL_VIRTUALS)
3688                 {
3689                   if (TREE_CODE (sym) == SSA_NAME)
3690                     sym = SSA_NAME_VAR (sym);
3691                   mark_sym_for_renaming (sym);
3692                 }
3693               if (slp_node)
3694                 VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
3695             }
3696
3697           if (j == 0)
3698             STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3699           else
3700             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3701           prev_stmt_info = vinfo_for_stmt (new_stmt);
3702         }
3703       break;
3704
3705     case WIDEN:
3706       /* In case the vectorization factor (VF) is bigger than the number
3707          of elements that we can fit in a vectype (nunits), we have to
3708          generate more than one vector stmt - i.e - we need to "unroll"
3709          the vector stmt by a factor VF/nunits.  */
3710       for (j = 0; j < ncopies; j++)
3711         {
3712           if (j == 0)
3713             vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
3714           else
3715             vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3716
3717           STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
3718
3719           /* Generate first half of the widened result:  */
3720           new_stmt
3721             = vect_gen_widened_results_half (code1, decl1, 
3722                                              vec_oprnd0, vec_oprnd1,
3723                                              unary_op, vec_dest, gsi, stmt);
3724           if (j == 0)
3725             STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3726           else
3727             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3728           prev_stmt_info = vinfo_for_stmt (new_stmt);
3729
3730           /* Generate second half of the widened result:  */
3731           new_stmt
3732             = vect_gen_widened_results_half (code2, decl2,
3733                                              vec_oprnd0, vec_oprnd1,
3734                                              unary_op, vec_dest, gsi, stmt);
3735           STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3736           prev_stmt_info = vinfo_for_stmt (new_stmt);
3737         }
3738       break;
3739
3740     case NARROW:
3741       /* In case the vectorization factor (VF) is bigger than the number
3742          of elements that we can fit in a vectype (nunits), we have to
3743          generate more than one vector stmt - i.e - we need to "unroll"
3744          the vector stmt by a factor VF/nunits.  */
3745       for (j = 0; j < ncopies; j++)
3746         {
3747           /* Handle uses.  */
3748           if (j == 0)
3749             {
3750               vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
3751               vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3752             }
3753           else
3754             {
3755               vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd1);
3756               vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3757             }
3758
3759           /* Arguments are ready. Create the new vector stmt.  */
3760           expr = build2 (code1, vectype_out, vec_oprnd0, vec_oprnd1);
3761           new_stmt = gimple_build_assign_with_ops (code1, vec_dest, vec_oprnd0,
3762                                                    vec_oprnd1);
3763           new_temp = make_ssa_name (vec_dest, new_stmt);
3764           gimple_assign_set_lhs (new_stmt, new_temp);
3765           vect_finish_stmt_generation (stmt, new_stmt, gsi);
3766
3767           if (j == 0)
3768             STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3769           else
3770             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3771
3772           prev_stmt_info = vinfo_for_stmt (new_stmt);
3773         }
3774
3775       *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3776     }
3777
3778   if (vec_oprnds0)
3779     VEC_free (tree, heap, vec_oprnds0); 
3780
3781   return true;
3782 }
3783
3784
3785 /* Function vectorizable_assignment.
3786
3787    Check if STMT performs an assignment (copy) that can be vectorized. 
3788    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
3789    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3790    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
3791
3792 bool
3793 vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
3794                          gimple *vec_stmt, slp_tree slp_node)
3795 {
3796   tree vec_dest;
3797   tree scalar_dest;
3798   tree op;
3799   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3800   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3801   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3802   tree new_temp;
3803   tree def;
3804   gimple def_stmt;
3805   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3806   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
3807   int ncopies;
3808   int i;
3809   VEC(tree,heap) *vec_oprnds = NULL;
3810   tree vop;
3811
3812   /* Multiple types in SLP are handled by creating the appropriate number of
3813      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3814      case of SLP.  */
3815   if (slp_node)
3816     ncopies = 1;
3817   else
3818     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3819
3820   gcc_assert (ncopies >= 1);
3821   if (ncopies > 1)
3822     return false; /* FORNOW */
3823
3824   if (!STMT_VINFO_RELEVANT_P (stmt_info))
3825     return false;
3826
3827   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
3828     return false;
3829
3830   /* Is vectorizable assignment?  */
3831   if (!is_gimple_assign (stmt))
3832     return false;
3833
3834   scalar_dest = gimple_assign_lhs (stmt);
3835   if (TREE_CODE (scalar_dest) != SSA_NAME)
3836     return false;
3837
3838   if (gimple_assign_single_p (stmt)
3839       || gimple_assign_rhs_code (stmt) == PAREN_EXPR)
3840     op = gimple_assign_rhs1 (stmt);
3841   else
3842     return false;
3843
3844   if (!vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt[0]))
3845     {
3846       if (vect_print_dump_info (REPORT_DETAILS))
3847         fprintf (vect_dump, "use not simple.");
3848       return false;
3849     }
3850
3851   if (!vec_stmt) /* transformation not required.  */
3852     {
3853       STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
3854       if (vect_print_dump_info (REPORT_DETAILS))
3855         fprintf (vect_dump, "=== vectorizable_assignment ===");
3856       vect_model_simple_cost (stmt_info, ncopies, dt, NULL);
3857       return true;
3858     }
3859
3860   /** Transform.  **/
3861   if (vect_print_dump_info (REPORT_DETAILS))
3862     fprintf (vect_dump, "transform assignment.");
3863
3864   /* Handle def.  */
3865   vec_dest = vect_create_destination_var (scalar_dest, vectype);
3866
3867   /* Handle use.  */
3868   vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node);
3869
3870   /* Arguments are ready. create the new vector stmt.  */
3871   for (i = 0; VEC_iterate (tree, vec_oprnds, i, vop); i++)
3872     {
3873       *vec_stmt = gimple_build_assign (vec_dest, vop);
3874       new_temp = make_ssa_name (vec_dest, *vec_stmt);
3875       gimple_assign_set_lhs (*vec_stmt, new_temp);
3876       vect_finish_stmt_generation (stmt, *vec_stmt, gsi);
3877       STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt;
3878
3879       if (slp_node)
3880         VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), *vec_stmt);
3881    }
3882   
3883   VEC_free (tree, heap, vec_oprnds);       
3884   return true;
3885 }
3886
3887
3888 /* Function vect_min_worthwhile_factor.
3889
3890    For a loop where we could vectorize the operation indicated by CODE,
3891    return the minimum vectorization factor that makes it worthwhile
3892    to use generic vectors.  */
3893 static int
3894 vect_min_worthwhile_factor (enum tree_code code)
3895 {
3896   switch (code)
3897     {
3898     case PLUS_EXPR:
3899     case MINUS_EXPR:
3900     case NEGATE_EXPR:
3901       return 4;
3902
3903     case BIT_AND_EXPR:
3904     case BIT_IOR_EXPR:
3905     case BIT_XOR_EXPR:
3906     case BIT_NOT_EXPR:
3907       return 2;
3908
3909     default:
3910       return INT_MAX;
3911     }
3912 }
3913
3914
3915 /* Function vectorizable_induction
3916
3917    Check if PHI performs an induction computation that can be vectorized.
3918    If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
3919    phi to replace it, put it in VEC_STMT, and add it to the same basic block.
3920    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
3921
3922 bool
3923 vectorizable_induction (gimple phi, gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
3924                         gimple *vec_stmt)
3925 {
3926   stmt_vec_info stmt_info = vinfo_for_stmt (phi);
3927   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3928   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3929   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3930   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
3931   int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3932   tree vec_def;
3933
3934   gcc_assert (ncopies >= 1);
3935   /* FORNOW. This restriction should be relaxed.  */
3936   if (nested_in_vect_loop_p (loop, phi) && ncopies > 1)
3937     {
3938       if (vect_print_dump_info (REPORT_DETAILS))
3939         fprintf (vect_dump, "multiple types in nested loop.");
3940       return false;
3941     }
3942
3943   if (!STMT_VINFO_RELEVANT_P (stmt_info))
3944     return false;
3945
3946   /* FORNOW: SLP not supported.  */
3947   if (STMT_SLP_TYPE (stmt_info))
3948     return false;
3949
3950   gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def);
3951
3952   if (gimple_code (phi) != GIMPLE_PHI)
3953     return false;
3954
3955   if (!vec_stmt) /* transformation not required.  */
3956     {
3957       STMT_VINFO_TYPE (stmt_info) = induc_vec_info_type;
3958       if (vect_print_dump_info (REPORT_DETAILS))
3959         fprintf (vect_dump, "=== vectorizable_induction ===");
3960       vect_model_induction_cost (stmt_info, ncopies);
3961       return true;
3962     }
3963
3964   /** Transform.  **/
3965
3966   if (vect_print_dump_info (REPORT_DETAILS))
3967     fprintf (vect_dump, "transform induction phi.");
3968
3969   vec_def = get_initial_def_for_induction (phi);
3970   *vec_stmt = SSA_NAME_DEF_STMT (vec_def);
3971   return true;
3972 }
3973
3974
3975 /* Function vectorizable_operation.
3976
3977    Check if STMT performs a binary or unary operation that can be vectorized. 
3978    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
3979    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3980    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
3981
3982 bool
3983 vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
3984                         gimple *vec_stmt, slp_tree slp_node)
3985 {
3986   tree vec_dest;
3987   tree scalar_dest;
3988   tree op0, op1 = NULL;
3989   tree vec_oprnd1 = NULL_TREE;
3990   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3991   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3992   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3993   enum tree_code code;
3994   enum machine_mode vec_mode;
3995   tree new_temp;
3996   int op_type;
3997   optab optab;
3998   int icode;
3999   enum machine_mode optab_op2_mode;
4000   tree def;
4001   gimple def_stmt;
4002   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4003   gimple new_stmt = NULL;
4004   stmt_vec_info prev_stmt_info;
4005   int nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4006   int nunits_out;
4007   tree vectype_out;
4008   int ncopies;
4009   int j, i;
4010   VEC(tree,heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL;
4011   tree vop0, vop1;
4012   unsigned int k;
4013   bool shift_p = false;
4014   bool scalar_shift_arg = false;
4015
4016   /* Multiple types in SLP are handled by creating the appropriate number of
4017      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4018      case of SLP.  */
4019   if (slp_node)
4020     ncopies = 1;
4021   else
4022     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4023
4024   gcc_assert (ncopies >= 1);
4025
4026   if (!STMT_VINFO_RELEVANT_P (stmt_info))
4027     return false;
4028
4029   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
4030     return false;
4031
4032   /* Is STMT a vectorizable binary/unary operation?   */
4033   if (!is_gimple_assign (stmt))
4034     return false;
4035
4036   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4037     return false;
4038
4039   scalar_dest = gimple_assign_lhs (stmt);
4040   vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
4041   if (!vectype_out)
4042     return false;
4043   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4044   if (nunits_out != nunits_in)
4045     return false;
4046
4047   code = gimple_assign_rhs_code (stmt);
4048
4049   /* For pointer addition, we should use the normal plus for
4050      the vector addition.  */
4051   if (code == POINTER_PLUS_EXPR)
4052     code = PLUS_EXPR;
4053
4054   /* Support only unary or binary operations.  */
4055   op_type = TREE_CODE_LENGTH (code);
4056   if (op_type != unary_op && op_type != binary_op)
4057     {
4058       if (vect_print_dump_info (REPORT_DETAILS))
4059         fprintf (vect_dump, "num. args = %d (not unary/binary op).", op_type);
4060       return false;
4061     }
4062
4063   op0 = gimple_assign_rhs1 (stmt);
4064   if (!vect_is_simple_use (op0, loop_vinfo, &def_stmt, &def, &dt[0]))
4065     {
4066       if (vect_print_dump_info (REPORT_DETAILS))
4067         fprintf (vect_dump, "use not simple.");
4068       return false;
4069     }
4070
4071   if (op_type == binary_op)
4072     {
4073       op1 = gimple_assign_rhs2 (stmt);
4074       if (!vect_is_simple_use (op1, loop_vinfo, &def_stmt, &def, &dt[1]))
4075         {
4076           if (vect_print_dump_info (REPORT_DETAILS))
4077             fprintf (vect_dump, "use not simple.");
4078           return false;
4079         }
4080     }
4081
4082   /* If this is a shift/rotate, determine whether the shift amount is a vector,
4083      or scalar.  If the shift/rotate amount is a vector, use the vector/vector
4084      shift optabs.  */
4085   if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4086       || code == RROTATE_EXPR)
4087     {
4088       shift_p = true;
4089
4090       /* vector shifted by vector */
4091       if (dt[1] == vect_loop_def)
4092         {
4093           optab = optab_for_tree_code (code, vectype, optab_vector);
4094           if (vect_print_dump_info (REPORT_DETAILS))
4095             fprintf (vect_dump, "vector/vector shift/rotate found.");
4096         }
4097
4098       /* See if the machine has a vector shifted by scalar insn and if not
4099          then see if it has a vector shifted by vector insn */
4100       else if (dt[1] == vect_constant_def || dt[1] == vect_invariant_def)
4101         {
4102           optab = optab_for_tree_code (code, vectype, optab_scalar);
4103           if (optab
4104               && (optab_handler (optab, TYPE_MODE (vectype))->insn_code
4105                   != CODE_FOR_nothing))
4106             {
4107               scalar_shift_arg = true;
4108               if (vect_print_dump_info (REPORT_DETAILS))
4109                 fprintf (vect_dump, "vector/scalar shift/rotate found.");
4110             }
4111           else
4112             {
4113               optab = optab_for_tree_code (code, vectype, optab_vector);
4114               if (vect_print_dump_info (REPORT_DETAILS)
4115                   && optab
4116                   && (optab_handler (optab, TYPE_MODE (vectype))->insn_code
4117                       != CODE_FOR_nothing))
4118                 fprintf (vect_dump, "vector/vector shift/rotate found.");
4119             }
4120         }
4121
4122       else
4123         {
4124           if (vect_print_dump_info (REPORT_DETAILS))
4125             fprintf (vect_dump, "operand mode requires invariant argument.");
4126           return false;
4127         }
4128     }
4129   else
4130     optab = optab_for_tree_code (code, vectype, optab_default);
4131
4132   /* Supportable by target?  */
4133   if (!optab)
4134     {
4135       if (vect_print_dump_info (REPORT_DETAILS))
4136         fprintf (vect_dump, "no optab.");
4137       return false;
4138     }
4139   vec_mode = TYPE_MODE (vectype);
4140   icode = (int) optab_handler (optab, vec_mode)->insn_code;
4141   if (icode == CODE_FOR_nothing)
4142     {
4143       if (vect_print_dump_info (REPORT_DETAILS))
4144         fprintf (vect_dump, "op not supported by target.");
4145       /* Check only during analysis.  */
4146       if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4147           || (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
4148               < vect_min_worthwhile_factor (code)
4149               && !vec_stmt))
4150         return false;
4151       if (vect_print_dump_info (REPORT_DETAILS))
4152         fprintf (vect_dump, "proceeding using word mode.");
4153     }
4154
4155   /* Worthwhile without SIMD support? Check only during analysis.  */
4156   if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4157       && LOOP_VINFO_VECT_FACTOR (loop_vinfo)
4158          < vect_min_worthwhile_factor (code)
4159       && !vec_stmt)
4160     {
4161       if (vect_print_dump_info (REPORT_DETAILS))
4162         fprintf (vect_dump, "not worthwhile without SIMD support.");
4163       return false;
4164     }
4165
4166   if (!vec_stmt) /* transformation not required.  */
4167     {
4168       STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
4169       if (vect_print_dump_info (REPORT_DETAILS))
4170         fprintf (vect_dump, "=== vectorizable_operation ===");
4171       vect_model_simple_cost (stmt_info, ncopies, dt, NULL);
4172       return true;
4173     }
4174
4175   /** Transform.  **/
4176
4177   if (vect_print_dump_info (REPORT_DETAILS))
4178     fprintf (vect_dump, "transform binary/unary operation.");
4179
4180   /* Handle def.  */
4181   vec_dest = vect_create_destination_var (scalar_dest, vectype);
4182
4183   /* Allocate VECs for vector operands. In case of SLP, vector operands are 
4184      created in the previous stages of the recursion, so no allocation is
4185      needed, except for the case of shift with scalar shift argument. In that
4186      case we store the scalar operand in VEC_OPRNDS1 for every vector stmt to
4187      be created to vectorize the SLP group, i.e., SLP_NODE->VEC_STMTS_SIZE.
4188      In case of loop-based vectorization we allocate VECs of size 1. We 
4189      allocate VEC_OPRNDS1 only in case of binary operation.  */ 
4190   if (!slp_node)
4191     {
4192       vec_oprnds0 = VEC_alloc (tree, heap, 1);
4193       if (op_type == binary_op)
4194         vec_oprnds1 = VEC_alloc (tree, heap, 1);
4195     }
4196   else if (scalar_shift_arg)
4197     vec_oprnds1 = VEC_alloc (tree, heap, slp_node->vec_stmts_size);  
4198
4199   /* In case the vectorization factor (VF) is bigger than the number
4200      of elements that we can fit in a vectype (nunits), we have to generate
4201      more than one vector stmt - i.e - we need to "unroll" the
4202      vector stmt by a factor VF/nunits. In doing so, we record a pointer
4203      from one copy of the vector stmt to the next, in the field
4204      STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4205      stages to find the correct vector defs to be used when vectorizing
4206      stmts that use the defs of the current stmt. The example below illustrates
4207      the vectorization process when VF=16 and nunits=4 (i.e - we need to create
4208      4 vectorized stmts):
4209
4210      before vectorization:
4211                                 RELATED_STMT    VEC_STMT
4212         S1:     x = memref      -               -
4213         S2:     z = x + 1       -               -
4214
4215      step 1: vectorize stmt S1 (done in vectorizable_load. See more details
4216              there):
4217                                 RELATED_STMT    VEC_STMT
4218         VS1_0:  vx0 = memref0   VS1_1           -
4219         VS1_1:  vx1 = memref1   VS1_2           -
4220         VS1_2:  vx2 = memref2   VS1_3           -
4221         VS1_3:  vx3 = memref3   -               -
4222         S1:     x = load        -               VS1_0
4223         S2:     z = x + 1       -               -
4224
4225      step2: vectorize stmt S2 (done here):
4226         To vectorize stmt S2 we first need to find the relevant vector
4227         def for the first operand 'x'. This is, as usual, obtained from
4228         the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
4229         that defines 'x' (S1). This way we find the stmt VS1_0, and the
4230         relevant vector def 'vx0'. Having found 'vx0' we can generate
4231         the vector stmt VS2_0, and as usual, record it in the
4232         STMT_VINFO_VEC_STMT of stmt S2.
4233         When creating the second copy (VS2_1), we obtain the relevant vector
4234         def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
4235         stmt VS1_0. This way we find the stmt VS1_1 and the relevant
4236         vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
4237         pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
4238         Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
4239         chain of stmts and pointers:
4240                                 RELATED_STMT    VEC_STMT
4241         VS1_0:  vx0 = memref0   VS1_1           -
4242         VS1_1:  vx1 = memref1   VS1_2           -
4243         VS1_2:  vx2 = memref2   VS1_3           -
4244         VS1_3:  vx3 = memref3   -               -
4245         S1:     x = load        -               VS1_0
4246         VS2_0:  vz0 = vx0 + v1  VS2_1           -
4247         VS2_1:  vz1 = vx1 + v1  VS2_2           -
4248         VS2_2:  vz2 = vx2 + v1  VS2_3           -
4249         VS2_3:  vz3 = vx3 + v1  -               -
4250         S2:     z = x + 1       -               VS2_0  */
4251
4252   prev_stmt_info = NULL;
4253   for (j = 0; j < ncopies; j++)
4254     {
4255       /* Handle uses.  */
4256       if (j == 0)
4257         {
4258           if (op_type == binary_op && scalar_shift_arg)
4259             {
4260               /* Vector shl and shr insn patterns can be defined with scalar 
4261                  operand 2 (shift operand). In this case, use constant or loop 
4262                  invariant op1 directly, without extending it to vector mode 
4263                  first.  */
4264               optab_op2_mode = insn_data[icode].operand[2].mode;
4265               if (!VECTOR_MODE_P (optab_op2_mode))
4266                 {
4267                   if (vect_print_dump_info (REPORT_DETAILS))
4268                     fprintf (vect_dump, "operand 1 using scalar mode.");
4269                   vec_oprnd1 = op1;
4270                   VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
4271                   if (slp_node)
4272                     {
4273                       /* Store vec_oprnd1 for every vector stmt to be created
4274                          for SLP_NODE. We check during the analysis that all the
4275                          shift arguments are the same.  
4276                          TODO: Allow different constants for different vector 
4277                          stmts generated for an SLP instance.  */          
4278                       for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4279                         VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
4280                     }
4281                 }
4282             }
4283          
4284           /* vec_oprnd1 is available if operand 1 should be of a scalar-type 
4285              (a special case for certain kind of vector shifts); otherwise, 
4286              operand 1 should be of a vector type (the usual case).  */
4287           if (op_type == binary_op && !vec_oprnd1)
4288             vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1, 
4289                                slp_node);
4290           else
4291             vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL, 
4292                                slp_node);
4293         }
4294       else
4295         vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4296
4297       /* Arguments are ready. Create the new vector stmt.  */
4298       for (i = 0; VEC_iterate (tree, vec_oprnds0, i, vop0); i++)
4299         {
4300           vop1 = ((op_type == binary_op)
4301                   ? VEC_index (tree, vec_oprnds1, i) : NULL);
4302           new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
4303           new_temp = make_ssa_name (vec_dest, new_stmt);
4304           gimple_assign_set_lhs (new_stmt, new_temp);
4305           vect_finish_stmt_generation (stmt, new_stmt, gsi);
4306           if (slp_node)
4307             VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
4308         }
4309
4310       if (slp_node)
4311         continue;
4312
4313       if (j == 0)
4314         STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4315       else
4316         STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4317       prev_stmt_info = vinfo_for_stmt (new_stmt);
4318     }
4319
4320   VEC_free (tree, heap, vec_oprnds0);
4321   if (vec_oprnds1)
4322     VEC_free (tree, heap, vec_oprnds1);
4323
4324   return true;
4325 }
4326
4327
4328 /* Get vectorized definitions for loop-based vectorization. For the first
4329    operand we call vect_get_vec_def_for_operand() (with OPRND containing 
4330    scalar operand), and for the rest we get a copy with 
4331    vect_get_vec_def_for_stmt_copy() using the previous vector definition
4332    (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
4333    The vectors are collected into VEC_OPRNDS.  */
4334
4335 static void
4336 vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt, 
4337                           VEC (tree, heap) **vec_oprnds, int multi_step_cvt)
4338 {
4339   tree vec_oprnd;
4340
4341   /* Get first vector operand.  */
4342   /* All the vector operands except the very first one (that is scalar oprnd)
4343      are stmt copies.  */
4344   if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)  
4345     vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
4346   else
4347     vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
4348
4349   VEC_quick_push (tree, *vec_oprnds, vec_oprnd);
4350
4351   /* Get second vector operand.  */
4352   vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
4353   VEC_quick_push (tree, *vec_oprnds, vec_oprnd);
4354     
4355   *oprnd = vec_oprnd;
4356
4357   /* For conversion in multiple steps, continue to get operands 
4358      recursively.  */
4359   if (multi_step_cvt)
4360     vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds,  multi_step_cvt - 1); 
4361 }
4362
4363
4364 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
4365    For multi-step conversions store the resulting vectors and call the function 
4366    recursively.  */
4367
4368 static void
4369 vect_create_vectorized_demotion_stmts (VEC (tree, heap) **vec_oprnds,
4370                                        int multi_step_cvt, gimple stmt,
4371                                        VEC (tree, heap) *vec_dsts,
4372                                        gimple_stmt_iterator *gsi,
4373                                        slp_tree slp_node, enum tree_code code,
4374                                        stmt_vec_info *prev_stmt_info)
4375 {
4376   unsigned int i;
4377   tree vop0, vop1, new_tmp, vec_dest;
4378   gimple new_stmt;
4379   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4380
4381   vec_dest = VEC_pop (tree, vec_dsts); 
4382
4383   for (i = 0; i < VEC_length (tree, *vec_oprnds); i += 2)
4384     {
4385       /* Create demotion operation.  */
4386       vop0 = VEC_index (tree, *vec_oprnds, i);
4387       vop1 = VEC_index (tree, *vec_oprnds, i + 1);
4388       new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
4389       new_tmp = make_ssa_name (vec_dest, new_stmt);
4390       gimple_assign_set_lhs (new_stmt, new_tmp);
4391       vect_finish_stmt_generation (stmt, new_stmt, gsi);
4392
4393       if (multi_step_cvt)
4394         /* Store the resulting vector for next recursive call.  */
4395         VEC_replace (tree, *vec_oprnds, i/2, new_tmp);      
4396       else
4397         {
4398           /* This is the last step of the conversion sequence. Store the 
4399              vectors in SLP_NODE or in vector info of the scalar statement
4400              (or in STMT_VINFO_RELATED_STMT chain).  */
4401           if (slp_node)
4402             VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
4403           else
4404             {
4405               if (!*prev_stmt_info)
4406                 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
4407               else
4408                 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
4409
4410               *prev_stmt_info = vinfo_for_stmt (new_stmt);
4411             }
4412         }
4413     }
4414
4415   /* For multi-step demotion operations we first generate demotion operations
4416      from the source type to the intermediate types, and then combine the 
4417      results (stored in VEC_OPRNDS) in demotion operation to the destination
4418      type.  */
4419   if (multi_step_cvt)
4420     {
4421       /* At each level of recursion we have have of the operands we had at the
4422          previous level.  */
4423       VEC_truncate (tree, *vec_oprnds, (i+1)/2);
4424       vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1, 
4425                                              stmt, vec_dsts, gsi, slp_node,
4426                                              code, prev_stmt_info);
4427     }
4428 }
4429
4430
4431 /* Function vectorizable_type_demotion
4432
4433    Check if STMT performs a binary or unary operation that involves
4434    type demotion, and if it can be vectorized.
4435    If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4436    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4437    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
4438
4439 bool
4440 vectorizable_type_demotion (gimple stmt, gimple_stmt_iterator *gsi,
4441                             gimple *vec_stmt, slp_tree slp_node)
4442 {
4443   tree vec_dest;
4444   tree scalar_dest;
4445   tree op0;
4446   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4447   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4448   enum tree_code code, code1 = ERROR_MARK;
4449   tree def;
4450   gimple def_stmt;
4451   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4452   stmt_vec_info prev_stmt_info;
4453   int nunits_in;
4454   int nunits_out;
4455   tree vectype_out;
4456   int ncopies;
4457   int j, i;
4458   tree vectype_in;
4459   int multi_step_cvt = 0;
4460   VEC (tree, heap) *vec_oprnds0 = NULL;
4461   VEC (tree, heap) *vec_dsts = NULL, *interm_types = NULL, *tmp_vec_dsts = NULL;
4462   tree last_oprnd, intermediate_type;
4463
4464   if (!STMT_VINFO_RELEVANT_P (stmt_info))
4465     return false;
4466
4467   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
4468     return false;
4469
4470   /* Is STMT a vectorizable type-demotion operation?  */
4471   if (!is_gimple_assign (stmt))
4472     return false;
4473
4474   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4475     return false;
4476
4477   code = gimple_assign_rhs_code (stmt);
4478   if (!CONVERT_EXPR_CODE_P (code))
4479     return false;
4480
4481   op0 = gimple_assign_rhs1 (stmt);
4482   vectype_in = get_vectype_for_scalar_type (TREE_TYPE (op0));
4483   if (!vectype_in)
4484     return false;
4485   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
4486
4487   scalar_dest = gimple_assign_lhs (stmt);
4488   vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
4489   if (!vectype_out)
4490     return false;
4491   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4492   if (nunits_in >= nunits_out)
4493     return false;
4494
4495   /* Multiple types in SLP are handled by creating the appropriate number of
4496      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4497      case of SLP.  */
4498   if (slp_node)
4499     ncopies = 1;
4500   else
4501     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
4502
4503   gcc_assert (ncopies >= 1);
4504
4505   if (! ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4506           && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
4507          || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (scalar_dest))
4508              && SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0))
4509              && CONVERT_EXPR_CODE_P (code))))
4510     return false;
4511
4512   /* Check the operands of the operation.  */
4513   if (!vect_is_simple_use (op0, loop_vinfo, &def_stmt, &def, &dt[0]))
4514     {
4515       if (vect_print_dump_info (REPORT_DETAILS))
4516         fprintf (vect_dump, "use not simple.");
4517       return false;
4518     }
4519
4520   /* Supportable by target?  */
4521   if (!supportable_narrowing_operation (code, stmt, vectype_in, &code1,
4522                                         &multi_step_cvt, &interm_types))
4523     return false;
4524
4525   STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
4526
4527   if (!vec_stmt) /* transformation not required.  */
4528     {
4529       STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
4530       if (vect_print_dump_info (REPORT_DETAILS))
4531         fprintf (vect_dump, "=== vectorizable_demotion ===");
4532       vect_model_simple_cost (stmt_info, ncopies, dt, NULL);
4533       return true;
4534     }
4535
4536   /** Transform.  **/
4537   if (vect_print_dump_info (REPORT_DETAILS))
4538     fprintf (vect_dump, "transform type demotion operation. ncopies = %d.",
4539              ncopies);
4540
4541   /* In case of multi-step demotion, we first generate demotion operations to 
4542      the intermediate types, and then from that types to the final one. 
4543      We create vector destinations for the intermediate type (TYPES) received
4544      from supportable_narrowing_operation, and store them in the correct order 
4545      for future use in vect_create_vectorized_demotion_stmts().  */
4546   if (multi_step_cvt)
4547     vec_dsts = VEC_alloc (tree, heap, multi_step_cvt + 1);
4548   else
4549     vec_dsts = VEC_alloc (tree, heap, 1);
4550  
4551   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
4552   VEC_quick_push (tree, vec_dsts, vec_dest);
4553
4554   if (multi_step_cvt)
4555     {
4556       for (i = VEC_length (tree, interm_types) - 1; 
4557            VEC_iterate (tree, interm_types, i, intermediate_type); i--)
4558         {
4559           vec_dest = vect_create_destination_var (scalar_dest, 
4560                                                   intermediate_type);
4561           VEC_quick_push (tree, vec_dsts, vec_dest);
4562         }
4563     }
4564
4565   /* In case the vectorization factor (VF) is bigger than the number
4566      of elements that we can fit in a vectype (nunits), we have to generate
4567      more than one vector stmt - i.e - we need to "unroll" the
4568      vector stmt by a factor VF/nunits.   */
4569   last_oprnd = op0;
4570   prev_stmt_info = NULL;
4571   for (j = 0; j < ncopies; j++)
4572     {
4573       /* Handle uses.  */
4574       if (slp_node)
4575         vect_get_slp_defs (slp_node, &vec_oprnds0, NULL); 
4576       else
4577         {
4578           VEC_free (tree, heap, vec_oprnds0);
4579           vec_oprnds0 = VEC_alloc (tree, heap,
4580                         (multi_step_cvt ? vect_pow2 (multi_step_cvt) * 2 : 2));
4581           vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,  
4582                                     vect_pow2 (multi_step_cvt) - 1);
4583         }
4584
4585       /* Arguments are ready. Create the new vector stmts.  */
4586       tmp_vec_dsts = VEC_copy (tree, heap, vec_dsts);
4587       vect_create_vectorized_demotion_stmts (&vec_oprnds0,  
4588                                              multi_step_cvt, stmt, tmp_vec_dsts,
4589                                              gsi, slp_node, code1, 
4590                                              &prev_stmt_info);
4591     }
4592
4593   VEC_free (tree, heap, vec_oprnds0);
4594   VEC_free (tree, heap, vec_dsts);
4595   VEC_free (tree, heap, tmp_vec_dsts);
4596   VEC_free (tree, heap, interm_types);
4597
4598   *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4599   return true;
4600 }
4601
4602
4603 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
4604    and VEC_OPRNDS1 (for binary operations). For multi-step conversions store 
4605    the resulting vectors and call the function recursively.  */
4606
4607 static void
4608 vect_create_vectorized_promotion_stmts (VEC (tree, heap) **vec_oprnds0,
4609                                         VEC (tree, heap) **vec_oprnds1,
4610                                         int multi_step_cvt, gimple stmt,
4611                                         VEC (tree, heap) *vec_dsts,
4612                                         gimple_stmt_iterator *gsi,
4613                                         slp_tree slp_node, enum tree_code code1,
4614                                         enum tree_code code2, tree decl1, 
4615                                         tree decl2, int op_type,
4616                                         stmt_vec_info *prev_stmt_info)
4617 {
4618   int i;
4619   tree vop0, vop1, new_tmp1, new_tmp2, vec_dest;
4620   gimple new_stmt1, new_stmt2;
4621   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4622   VEC (tree, heap) *vec_tmp;
4623
4624   vec_dest = VEC_pop (tree, vec_dsts);
4625   vec_tmp = VEC_alloc (tree, heap, VEC_length (tree, *vec_oprnds0) * 2);
4626
4627   for (i = 0; VEC_iterate (tree, *vec_oprnds0, i, vop0); i++)
4628     {
4629       if (op_type == binary_op)
4630         vop1 = VEC_index (tree, *vec_oprnds1, i);
4631       else
4632         vop1 = NULL_TREE;
4633
4634       /* Generate the two halves of promotion operation.  */
4635       new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,  
4636                                                  op_type, vec_dest, gsi, stmt);
4637       new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
4638                                                  op_type, vec_dest, gsi, stmt);
4639       if (is_gimple_call (new_stmt1))
4640         {
4641           new_tmp1 = gimple_call_lhs (new_stmt1);
4642           new_tmp2 = gimple_call_lhs (new_stmt2);
4643         }
4644       else
4645         {
4646           new_tmp1 = gimple_assign_lhs (new_stmt1);
4647           new_tmp2 = gimple_assign_lhs (new_stmt2);
4648         }
4649
4650       if (multi_step_cvt)
4651         {
4652           /* Store the results for the recursive call.  */
4653           VEC_quick_push (tree, vec_tmp, new_tmp1);
4654           VEC_quick_push (tree, vec_tmp, new_tmp2);
4655         }
4656       else
4657         {
4658           /* Last step of promotion sequience - store the results.  */
4659           if (slp_node)
4660             {
4661               VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt1);
4662               VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt2);
4663             }
4664           else
4665             {
4666               if (!*prev_stmt_info)
4667                 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt1;
4668               else
4669                 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt1;
4670
4671               *prev_stmt_info = vinfo_for_stmt (new_stmt1);
4672               STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt2;
4673               *prev_stmt_info = vinfo_for_stmt (new_stmt2);
4674             }
4675         }
4676     }
4677
4678   if (multi_step_cvt)
4679     {
4680       /* For multi-step promotion operation we first generate we call the 
4681          function recurcively for every stage. We start from the input type,
4682          create promotion operations to the intermediate types, and then
4683          create promotions to the output type.  */
4684       *vec_oprnds0 = VEC_copy (tree, heap, vec_tmp);
4685       VEC_free (tree, heap, vec_tmp);
4686       vect_create_vectorized_promotion_stmts (vec_oprnds0, vec_oprnds1,
4687                                               multi_step_cvt - 1, stmt,
4688                                               vec_dsts, gsi, slp_node, code1,
4689                                               code2, decl2, decl2, op_type,
4690                                               prev_stmt_info);
4691     }
4692 }
4693
4694
4695 /* Function vectorizable_type_promotion
4696
4697    Check if STMT performs a binary or unary operation that involves
4698    type promotion, and if it can be vectorized.
4699    If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4700    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4701    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
4702
4703 bool
4704 vectorizable_type_promotion (gimple stmt, gimple_stmt_iterator *gsi,
4705                              gimple *vec_stmt, slp_tree slp_node)
4706 {
4707   tree vec_dest;
4708   tree scalar_dest;
4709   tree op0, op1 = NULL;
4710   tree vec_oprnd0=NULL, vec_oprnd1=NULL;
4711   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4712   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4713   enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
4714   tree decl1 = NULL_TREE, decl2 = NULL_TREE;
4715   int op_type; 
4716   tree def;
4717   gimple def_stmt;
4718   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4719   stmt_vec_info prev_stmt_info;
4720   int nunits_in;
4721   int nunits_out;
4722   tree vectype_out;
4723   int ncopies;
4724   int j, i;
4725   tree vectype_in;
4726   tree intermediate_type = NULL_TREE;
4727   int multi_step_cvt = 0;
4728   VEC (tree, heap) *vec_oprnds0 = NULL, *vec_oprnds1 = NULL;
4729   VEC (tree, heap) *vec_dsts = NULL, *interm_types = NULL, *tmp_vec_dsts = NULL;
4730   
4731   if (!STMT_VINFO_RELEVANT_P (stmt_info))
4732     return false;
4733
4734   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
4735     return false;
4736
4737   /* Is STMT a vectorizable type-promotion operation?  */
4738   if (!is_gimple_assign (stmt))
4739     return false;
4740
4741   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4742     return false;
4743
4744   code = gimple_assign_rhs_code (stmt);
4745   if (!CONVERT_EXPR_CODE_P (code)
4746       && code != WIDEN_MULT_EXPR)
4747     return false;
4748
4749   op0 = gimple_assign_rhs1 (stmt);
4750   vectype_in = get_vectype_for_scalar_type (TREE_TYPE (op0));
4751   if (!vectype_in)
4752     return false;
4753   nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
4754
4755   scalar_dest = gimple_assign_lhs (stmt);
4756   vectype_out = get_vectype_for_scalar_type (TREE_TYPE (scalar_dest));
4757   if (!vectype_out)
4758     return false;
4759   nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4760   if (nunits_in <= nunits_out)
4761     return false;
4762
4763   /* Multiple types in SLP are handled by creating the appropriate number of
4764      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4765      case of SLP.  */
4766   if (slp_node)
4767     ncopies = 1;
4768   else
4769     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4770
4771   gcc_assert (ncopies >= 1);
4772
4773   if (! ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4774           && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
4775          || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (scalar_dest))
4776              && SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0))
4777              && CONVERT_EXPR_CODE_P (code))))
4778     return false;
4779
4780   /* Check the operands of the operation.  */
4781   if (!vect_is_simple_use (op0, loop_vinfo, &def_stmt, &def, &dt[0]))
4782     {
4783       if (vect_print_dump_info (REPORT_DETAILS))
4784         fprintf (vect_dump, "use not simple.");
4785       return false;
4786     }
4787
4788   op_type = TREE_CODE_LENGTH (code);
4789   if (op_type == binary_op)
4790     {
4791       op1 = gimple_assign_rhs2 (stmt);
4792       if (!vect_is_simple_use (op1, loop_vinfo, &def_stmt, &def, &dt[1]))
4793         {
4794           if (vect_print_dump_info (REPORT_DETAILS))
4795             fprintf (vect_dump, "use not simple.");
4796           return false;
4797         }
4798     }
4799
4800   /* Supportable by target?  */
4801   if (!supportable_widening_operation (code, stmt, vectype_in,
4802                                        &decl1, &decl2, &code1, &code2,
4803                                        &multi_step_cvt, &interm_types))
4804     return false;
4805
4806   /* Binary widening operation can only be supported directly by the
4807      architecture.  */
4808   gcc_assert (!(multi_step_cvt && op_type == binary_op));
4809
4810   STMT_VINFO_VECTYPE (stmt_info) = vectype_in;
4811
4812   if (!vec_stmt) /* transformation not required.  */
4813     {
4814       STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
4815       if (vect_print_dump_info (REPORT_DETAILS))
4816         fprintf (vect_dump, "=== vectorizable_promotion ===");
4817       vect_model_simple_cost (stmt_info, 2*ncopies, dt, NULL);
4818       return true;
4819     }
4820
4821   /** Transform.  **/
4822
4823   if (vect_print_dump_info (REPORT_DETAILS))
4824     fprintf (vect_dump, "transform type promotion operation. ncopies = %d.",
4825                         ncopies);
4826
4827   /* Handle def.  */
4828   /* In case of multi-step promotion, we first generate promotion operations 
4829      to the intermediate types, and then from that types to the final one.
4830      We store vector destination in VEC_DSTS in the correct order for 
4831      recursive creation of promotion operations in 
4832      vect_create_vectorized_promotion_stmts(). Vector destinations are created
4833      according to TYPES recieved from supportable_widening_operation().   */
4834   if (multi_step_cvt)
4835     vec_dsts = VEC_alloc (tree, heap, multi_step_cvt + 1);
4836   else
4837     vec_dsts = VEC_alloc (tree, heap, 1);
4838
4839   vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
4840   VEC_quick_push (tree, vec_dsts, vec_dest);
4841
4842   if (multi_step_cvt)
4843     {
4844       for (i = VEC_length (tree, interm_types) - 1;
4845            VEC_iterate (tree, interm_types, i, intermediate_type); i--)
4846         {
4847           vec_dest = vect_create_destination_var (scalar_dest,
4848                                                   intermediate_type);
4849           VEC_quick_push (tree, vec_dsts, vec_dest);
4850         }
4851     }
4852   
4853   if (!slp_node)
4854     {
4855       vec_oprnds0 = VEC_alloc (tree, heap, 
4856                             (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
4857       if (op_type == binary_op)
4858         vec_oprnds1 = VEC_alloc (tree, heap, 1);
4859     }
4860
4861   /* In case the vectorization factor (VF) is bigger than the number
4862      of elements that we can fit in a vectype (nunits), we have to generate
4863      more than one vector stmt - i.e - we need to "unroll" the
4864      vector stmt by a factor VF/nunits.   */
4865
4866   prev_stmt_info = NULL;
4867   for (j = 0; j < ncopies; j++)
4868     {
4869       /* Handle uses.  */
4870       if (j == 0)
4871         {
4872           if (slp_node)
4873               vect_get_slp_defs (slp_node, &vec_oprnds0, &vec_oprnds1);
4874           else
4875             {
4876               vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
4877               VEC_quick_push (tree, vec_oprnds0, vec_oprnd0);
4878               if (op_type == binary_op)
4879                 {
4880                   vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt, NULL);
4881                   VEC_quick_push (tree, vec_oprnds1, vec_oprnd1);
4882                 }
4883             }
4884         }
4885       else
4886         {
4887           vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
4888           VEC_replace (tree, vec_oprnds0, 0, vec_oprnd0);
4889           if (op_type == binary_op)
4890             {
4891               vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd1);
4892               VEC_replace (tree, vec_oprnds1, 0, vec_oprnd1);
4893             }
4894         }
4895
4896       /* Arguments are ready. Create the new vector stmts.  */
4897       tmp_vec_dsts = VEC_copy (tree, heap, vec_dsts);
4898       vect_create_vectorized_promotion_stmts (&vec_oprnds0, &vec_oprnds1,
4899                                               multi_step_cvt, stmt, 
4900                                               tmp_vec_dsts,
4901                                               gsi, slp_node, code1, code2,
4902                                               decl1, decl2, op_type,
4903                                               &prev_stmt_info);
4904     }
4905
4906   VEC_free (tree, heap, vec_dsts);
4907   VEC_free (tree, heap, tmp_vec_dsts);
4908   VEC_free (tree, heap, interm_types);
4909   VEC_free (tree, heap, vec_oprnds0);
4910   VEC_free (tree, heap, vec_oprnds1);
4911
4912   *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4913   return true;
4914 }
4915
4916
4917 /* Function vect_strided_store_supported.
4918
4919    Returns TRUE is INTERLEAVE_HIGH and INTERLEAVE_LOW operations are supported,
4920    and FALSE otherwise.  */
4921
4922 static bool
4923 vect_strided_store_supported (tree vectype)
4924 {
4925   optab interleave_high_optab, interleave_low_optab;
4926   int mode;
4927
4928   mode = (int) TYPE_MODE (vectype);
4929       
4930   /* Check that the operation is supported.  */
4931   interleave_high_optab = optab_for_tree_code (VEC_INTERLEAVE_HIGH_EXPR, 
4932                                                vectype, optab_default);
4933   interleave_low_optab = optab_for_tree_code (VEC_INTERLEAVE_LOW_EXPR, 
4934                                               vectype, optab_default);
4935   if (!interleave_high_optab || !interleave_low_optab)
4936     {
4937       if (vect_print_dump_info (REPORT_DETAILS))
4938         fprintf (vect_dump, "no optab for interleave.");
4939       return false;
4940     }
4941
4942   if (optab_handler (interleave_high_optab, mode)->insn_code 
4943       == CODE_FOR_nothing
4944       || optab_handler (interleave_low_optab, mode)->insn_code 
4945       == CODE_FOR_nothing)
4946     {
4947       if (vect_print_dump_info (REPORT_DETAILS))
4948         fprintf (vect_dump, "interleave op not supported by target.");
4949       return false;
4950     }
4951
4952   return true;
4953 }
4954
4955
4956 /* Function vect_permute_store_chain.
4957
4958    Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4959    a power of 2, generate interleave_high/low stmts to reorder the data 
4960    correctly for the stores. Return the final references for stores in
4961    RESULT_CHAIN.
4962
4963    E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4964    The input is 4 vectors each containing 8 elements. We assign a number to each
4965    element, the input sequence is:
4966
4967    1st vec:   0  1  2  3  4  5  6  7
4968    2nd vec:   8  9 10 11 12 13 14 15
4969    3rd vec:  16 17 18 19 20 21 22 23 
4970    4th vec:  24 25 26 27 28 29 30 31
4971
4972    The output sequence should be:
4973
4974    1st vec:  0  8 16 24  1  9 17 25
4975    2nd vec:  2 10 18 26  3 11 19 27
4976    3rd vec:  4 12 20 28  5 13 21 30
4977    4th vec:  6 14 22 30  7 15 23 31
4978
4979    i.e., we interleave the contents of the four vectors in their order.
4980
4981    We use interleave_high/low instructions to create such output. The input of 
4982    each interleave_high/low operation is two vectors:
4983    1st vec    2nd vec 
4984    0 1 2 3    4 5 6 7 
4985    the even elements of the result vector are obtained left-to-right from the 
4986    high/low elements of the first vector. The odd elements of the result are 
4987    obtained left-to-right from the high/low elements of the second vector.
4988    The output of interleave_high will be:   0 4 1 5
4989    and of interleave_low:                   2 6 3 7
4990
4991    
4992    The permutation is done in log LENGTH stages. In each stage interleave_high
4993    and interleave_low stmts are created for each pair of vectors in DR_CHAIN, 
4994    where the first argument is taken from the first half of DR_CHAIN and the 
4995    second argument from it's second half. 
4996    In our example, 
4997
4998    I1: interleave_high (1st vec, 3rd vec)
4999    I2: interleave_low (1st vec, 3rd vec)
5000    I3: interleave_high (2nd vec, 4th vec)
5001    I4: interleave_low (2nd vec, 4th vec)
5002
5003    The output for the first stage is:
5004
5005    I1:  0 16  1 17  2 18  3 19
5006    I2:  4 20  5 21  6 22  7 23
5007    I3:  8 24  9 25 10 26 11 27
5008    I4: 12 28 13 29 14 30 15 31
5009
5010    The output of the second stage, i.e. the final result is:
5011
5012    I1:  0  8 16 24  1  9 17 25
5013    I2:  2 10 18 26  3 11 19 27
5014    I3:  4 12 20 28  5 13 21 30
5015    I4:  6 14 22 30  7 15 23 31.  */
5016  
5017 static bool
5018 vect_permute_store_chain (VEC(tree,heap) *dr_chain, 
5019                           unsigned int length, 
5020                           gimple stmt,
5021                           gimple_stmt_iterator *gsi,
5022                           VEC(tree,heap) **result_chain)
5023 {
5024   tree perm_dest, vect1, vect2, high, low;
5025   gimple perm_stmt;
5026   tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5027   tree scalar_dest;
5028   int i;
5029   unsigned int j;
5030   enum tree_code high_code, low_code;
5031   
5032   scalar_dest = gimple_assign_lhs (stmt);
5033
5034   /* Check that the operation is supported.  */
5035   if (!vect_strided_store_supported (vectype))
5036     return false;
5037
5038   *result_chain = VEC_copy (tree, heap, dr_chain);
5039
5040   for (i = 0; i < exact_log2 (length); i++)
5041     {
5042       for (j = 0; j < length/2; j++)
5043         {
5044           vect1 = VEC_index (tree, dr_chain, j);
5045           vect2 = VEC_index (tree, dr_chain, j+length/2);
5046
5047           /* Create interleaving stmt:
5048              in the case of big endian: 
5049                                 high = interleave_high (vect1, vect2) 
5050              and in the case of little endian: 
5051                                 high = interleave_low (vect1, vect2).  */
5052           perm_dest = create_tmp_var (vectype, "vect_inter_high");
5053           DECL_GIMPLE_REG_P (perm_dest) = 1;
5054           add_referenced_var (perm_dest);
5055           if (BYTES_BIG_ENDIAN)
5056             {
5057               high_code = VEC_INTERLEAVE_HIGH_EXPR;
5058               low_code = VEC_INTERLEAVE_LOW_EXPR;
5059             }
5060           else
5061             {
5062               low_code = VEC_INTERLEAVE_HIGH_EXPR;
5063               high_code = VEC_INTERLEAVE_LOW_EXPR;
5064             }
5065           perm_stmt = gimple_build_assign_with_ops (high_code, perm_dest,
5066                                                     vect1, vect2);
5067           high = make_ssa_name (perm_dest, perm_stmt);
5068           gimple_assign_set_lhs (perm_stmt, high);
5069           vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5070           VEC_replace (tree, *result_chain, 2*j, high);
5071
5072           /* Create interleaving stmt:
5073              in the case of big endian:
5074                                low  = interleave_low (vect1, vect2) 
5075              and in the case of little endian:
5076                                low  = interleave_high (vect1, vect2).  */     
5077           perm_dest = create_tmp_var (vectype, "vect_inter_low");
5078           DECL_GIMPLE_REG_P (perm_dest) = 1;
5079           add_referenced_var (perm_dest);
5080           perm_stmt = gimple_build_assign_with_ops (low_code, perm_dest,
5081                                                     vect1, vect2);
5082           low = make_ssa_name (perm_dest, perm_stmt);
5083           gimple_assign_set_lhs (perm_stmt, low);
5084           vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5085           VEC_replace (tree, *result_chain, 2*j+1, low);
5086         }
5087       dr_chain = VEC_copy (tree, heap, *result_chain);
5088     }
5089   return true;
5090 }
5091
5092
5093 /* Function vectorizable_store.
5094
5095    Check if STMT defines a non scalar data-ref (array/pointer/structure) that 
5096    can be vectorized. 
5097    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
5098    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5099    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
5100
5101 bool
5102 vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
5103                     slp_tree slp_node)
5104 {
5105   tree scalar_dest;
5106   tree data_ref;
5107   tree op;
5108   tree vec_oprnd = NULL_TREE;
5109   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5110   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5111   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5112   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5113   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5114   enum machine_mode vec_mode;
5115   tree dummy;
5116   enum dr_alignment_support alignment_support_scheme;
5117   tree def;
5118   gimple def_stmt;
5119   enum vect_def_type dt;
5120   stmt_vec_info prev_stmt_info = NULL;
5121   tree dataref_ptr = NULL_TREE;
5122   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5123   int ncopies;
5124   int j;
5125   gimple next_stmt, first_stmt = NULL;
5126   bool strided_store = false;
5127   unsigned int group_size, i;
5128   VEC(tree,heap) *dr_chain = NULL, *oprnds = NULL, *result_chain = NULL;
5129   bool inv_p;
5130   VEC(tree,heap) *vec_oprnds = NULL;
5131   bool slp = (slp_node != NULL);
5132   stmt_vec_info first_stmt_vinfo;
5133   unsigned int vec_num;
5134
5135   /* Multiple types in SLP are handled by creating the appropriate number of
5136      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5137      case of SLP.  */
5138   if (slp)
5139     ncopies = 1;
5140   else
5141     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5142
5143   gcc_assert (ncopies >= 1);
5144
5145   /* FORNOW. This restriction should be relaxed.  */
5146   if (nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
5147     {
5148       if (vect_print_dump_info (REPORT_DETAILS))
5149         fprintf (vect_dump, "multiple types in nested loop.");
5150       return false;
5151     }
5152
5153   if (!STMT_VINFO_RELEVANT_P (stmt_info))
5154     return false;
5155
5156   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
5157     return false;
5158
5159   /* Is vectorizable store? */
5160
5161   if (!is_gimple_assign (stmt))
5162     return false;
5163
5164   scalar_dest = gimple_assign_lhs (stmt);
5165   if (TREE_CODE (scalar_dest) != ARRAY_REF
5166       && TREE_CODE (scalar_dest) != INDIRECT_REF
5167       && !STMT_VINFO_STRIDED_ACCESS (stmt_info))
5168     return false;
5169
5170   gcc_assert (gimple_assign_single_p (stmt));
5171   op = gimple_assign_rhs1 (stmt);
5172   if (!vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt))
5173     {
5174       if (vect_print_dump_info (REPORT_DETAILS))
5175         fprintf (vect_dump, "use not simple.");
5176       return false;
5177     }
5178
5179   /* If accesses through a pointer to vectype do not alias the original
5180      memory reference we have a problem.  */
5181   if (get_alias_set (vectype) != get_alias_set (TREE_TYPE (scalar_dest))
5182       && !alias_set_subset_of (get_alias_set (vectype), 
5183                                get_alias_set (TREE_TYPE (scalar_dest))))
5184     {
5185       if (vect_print_dump_info (REPORT_DETAILS))
5186         fprintf (vect_dump, "vector type does not alias scalar type");
5187       return false;
5188     }
5189
5190   if (!useless_type_conversion_p (TREE_TYPE (op), TREE_TYPE (scalar_dest)))
5191     {      
5192       if (vect_print_dump_info (REPORT_DETAILS))
5193         fprintf (vect_dump, "operands of different types");
5194       return false;
5195     }
5196
5197   vec_mode = TYPE_MODE (vectype);
5198   /* FORNOW. In some cases can vectorize even if data-type not supported
5199      (e.g. - array initialization with 0).  */
5200   if (optab_handler (mov_optab, (int)vec_mode)->insn_code == CODE_FOR_nothing)
5201     return false;
5202
5203   if (!STMT_VINFO_DATA_REF (stmt_info))
5204     return false;
5205
5206   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
5207     {
5208       strided_store = true;
5209       first_stmt = DR_GROUP_FIRST_DR (stmt_info);
5210       if (!vect_strided_store_supported (vectype)
5211           && !PURE_SLP_STMT (stmt_info) && !slp)
5212         return false;
5213      
5214       if (first_stmt == stmt)
5215         {
5216           /* STMT is the leader of the group. Check the operands of all the
5217              stmts of the group.  */
5218           next_stmt = DR_GROUP_NEXT_DR (stmt_info);
5219           while (next_stmt)
5220             {
5221               gcc_assert (gimple_assign_single_p (next_stmt));
5222               op = gimple_assign_rhs1 (next_stmt);
5223               if (!vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt))
5224                 {
5225                   if (vect_print_dump_info (REPORT_DETAILS))
5226                     fprintf (vect_dump, "use not simple.");
5227                   return false;
5228                 }
5229               next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
5230             }
5231         }
5232     }
5233
5234   if (!vec_stmt) /* transformation not required.  */
5235     {
5236       STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
5237       vect_model_store_cost (stmt_info, ncopies, dt, NULL);
5238       return true;
5239     }
5240
5241   /** Transform.  **/
5242
5243   if (strided_store)
5244     {
5245       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5246       group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
5247
5248       DR_GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
5249
5250       /* FORNOW */
5251       gcc_assert (!nested_in_vect_loop_p (loop, stmt));
5252
5253       /* We vectorize all the stmts of the interleaving group when we
5254          reach the last stmt in the group.  */
5255       if (DR_GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt)) 
5256           < DR_GROUP_SIZE (vinfo_for_stmt (first_stmt))
5257           && !slp)
5258         {
5259           *vec_stmt = NULL;
5260           return true;
5261         }
5262
5263       if (slp)
5264         strided_store = false;
5265
5266       /* VEC_NUM is the number of vect stmts to be created for this group.  */
5267       if (slp)
5268         vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5269       else
5270         vec_num = group_size;
5271     }
5272   else 
5273     {
5274       first_stmt = stmt;
5275       first_dr = dr;
5276       group_size = vec_num = 1;
5277       first_stmt_vinfo = stmt_info;
5278     }
5279   
5280   if (vect_print_dump_info (REPORT_DETAILS))
5281     fprintf (vect_dump, "transform store. ncopies = %d",ncopies);
5282
5283   dr_chain = VEC_alloc (tree, heap, group_size);
5284   oprnds = VEC_alloc (tree, heap, group_size);
5285
5286   alignment_support_scheme = vect_supportable_dr_alignment (first_dr);
5287   gcc_assert (alignment_support_scheme);
5288   gcc_assert (alignment_support_scheme == dr_aligned);  /* FORNOW */
5289
5290   /* In case the vectorization factor (VF) is bigger than the number
5291      of elements that we can fit in a vectype (nunits), we have to generate
5292      more than one vector stmt - i.e - we need to "unroll" the
5293      vector stmt by a factor VF/nunits.  For more details see documentation in 
5294      vect_get_vec_def_for_copy_stmt.  */
5295
5296   /* In case of interleaving (non-unit strided access):
5297
5298         S1:  &base + 2 = x2
5299         S2:  &base = x0
5300         S3:  &base + 1 = x1
5301         S4:  &base + 3 = x3
5302
5303      We create vectorized stores starting from base address (the access of the
5304      first stmt in the chain (S2 in the above example), when the last store stmt
5305      of the chain (S4) is reached:
5306
5307         VS1: &base = vx2
5308         VS2: &base + vec_size*1 = vx0
5309         VS3: &base + vec_size*2 = vx1
5310         VS4: &base + vec_size*3 = vx3
5311
5312      Then permutation statements are generated:
5313
5314         VS5: vx5 = VEC_INTERLEAVE_HIGH_EXPR < vx0, vx3 >
5315         VS6: vx6 = VEC_INTERLEAVE_LOW_EXPR < vx0, vx3 >
5316         ...
5317         
5318      And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5319      (the order of the data-refs in the output of vect_permute_store_chain
5320      corresponds to the order of scalar stmts in the interleaving chain - see
5321      the documentation of vect_permute_store_chain()).
5322
5323      In case of both multiple types and interleaving, above vector stores and
5324      permutation stmts are created for every copy. The result vector stmts are
5325      put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
5326      STMT_VINFO_RELATED_STMT for the next copies.     
5327   */
5328
5329   prev_stmt_info = NULL;
5330   for (j = 0; j < ncopies; j++)
5331     {
5332       gimple new_stmt;
5333       gimple ptr_incr;
5334
5335       if (j == 0)
5336         {
5337           if (slp)
5338             {
5339               /* Get vectorized arguments for SLP_NODE.  */
5340               vect_get_slp_defs (slp_node, &vec_oprnds, NULL);
5341
5342               vec_oprnd = VEC_index (tree, vec_oprnds, 0);
5343             }
5344           else
5345             {
5346               /* For interleaved stores we collect vectorized defs for all the 
5347                  stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then 
5348                  used as an input to vect_permute_store_chain(), and OPRNDS as 
5349                  an input to vect_get_vec_def_for_stmt_copy() for the next copy.
5350
5351                  If the store is not strided, GROUP_SIZE is 1, and DR_CHAIN and
5352                  OPRNDS are of size 1.  */
5353               next_stmt = first_stmt;     
5354               for (i = 0; i < group_size; i++)
5355                 {
5356                   /* Since gaps are not supported for interleaved stores, 
5357                      GROUP_SIZE is the exact number of stmts in the chain. 
5358                      Therefore, NEXT_STMT can't be NULL_TREE.  In case that 
5359                      there is no interleaving, GROUP_SIZE is 1, and only one 
5360                      iteration of the loop will be executed.  */
5361                   gcc_assert (next_stmt);
5362                   gcc_assert (gimple_assign_single_p (next_stmt));
5363                   op = gimple_assign_rhs1 (next_stmt);
5364
5365                   vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt, 
5366                                                             NULL);
5367                   VEC_quick_push(tree, dr_chain, vec_oprnd); 
5368                   VEC_quick_push(tree, oprnds, vec_oprnd); 
5369                   next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
5370                 }
5371             }
5372
5373           dataref_ptr = vect_create_data_ref_ptr (first_stmt, NULL, NULL_TREE, 
5374                                                   &dummy, &ptr_incr, false, 
5375                                                   &inv_p);
5376           gcc_assert (!inv_p);
5377         }
5378       else 
5379         {
5380           /* For interleaved stores we created vectorized defs for all the 
5381              defs stored in OPRNDS in the previous iteration (previous copy). 
5382              DR_CHAIN is then used as an input to vect_permute_store_chain(), 
5383              and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
5384              next copy.
5385              If the store is not strided, GROUP_SIZE is 1, and DR_CHAIN and
5386              OPRNDS are of size 1.  */
5387           for (i = 0; i < group_size; i++)
5388             {
5389               op = VEC_index (tree, oprnds, i);
5390               vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt);
5391               vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op); 
5392               VEC_replace(tree, dr_chain, i, vec_oprnd);
5393               VEC_replace(tree, oprnds, i, vec_oprnd);
5394             }
5395           dataref_ptr = 
5396                 bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt, NULL_TREE);
5397         }
5398
5399       if (strided_store)
5400         {
5401           result_chain = VEC_alloc (tree, heap, group_size);     
5402           /* Permute.  */
5403           if (!vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
5404                                          &result_chain))
5405             return false;
5406         }
5407
5408       next_stmt = first_stmt;
5409       for (i = 0; i < vec_num; i++)
5410         {
5411           if (i > 0)
5412             /* Bump the vector pointer.  */
5413             dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5414                                            NULL_TREE);
5415
5416           if (slp)
5417             vec_oprnd = VEC_index (tree, vec_oprnds, i);
5418           else if (strided_store)
5419             /* For strided stores vectorized defs are interleaved in 
5420                vect_permute_store_chain().  */
5421             vec_oprnd = VEC_index (tree, result_chain, i);
5422
5423           data_ref = build_fold_indirect_ref (dataref_ptr);
5424           /* Arguments are ready. Create the new vector stmt.  */
5425           new_stmt = gimple_build_assign (data_ref, vec_oprnd);
5426           vect_finish_stmt_generation (stmt, new_stmt, gsi);
5427           mark_symbols_for_renaming (new_stmt);
5428
5429           if (slp)
5430             continue;
5431           
5432           if (j == 0)
5433             STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt =  new_stmt;
5434           else
5435             STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5436
5437           prev_stmt_info = vinfo_for_stmt (new_stmt);
5438           next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
5439           if (!next_stmt)
5440             break;
5441         }
5442     }
5443
5444   VEC_free (tree, heap, dr_chain);  
5445   VEC_free (tree, heap, oprnds);  
5446   if (result_chain)
5447     VEC_free (tree, heap, result_chain);  
5448
5449   return true;
5450 }
5451
5452
5453 /* Function vect_setup_realignment
5454   
5455    This function is called when vectorizing an unaligned load using
5456    the dr_explicit_realign[_optimized] scheme.
5457    This function generates the following code at the loop prolog:
5458
5459       p = initial_addr;
5460    x  msq_init = *(floor(p));   # prolog load
5461       realignment_token = call target_builtin; 
5462     loop:
5463    x  msq = phi (msq_init, ---)
5464
5465    The stmts marked with x are generated only for the case of 
5466    dr_explicit_realign_optimized.
5467
5468    The code above sets up a new (vector) pointer, pointing to the first 
5469    location accessed by STMT, and a "floor-aligned" load using that pointer.
5470    It also generates code to compute the "realignment-token" (if the relevant
5471    target hook was defined), and creates a phi-node at the loop-header bb
5472    whose arguments are the result of the prolog-load (created by this
5473    function) and the result of a load that takes place in the loop (to be
5474    created by the caller to this function).
5475
5476    For the case of dr_explicit_realign_optimized:
5477    The caller to this function uses the phi-result (msq) to create the 
5478    realignment code inside the loop, and sets up the missing phi argument,
5479    as follows:
5480     loop: 
5481       msq = phi (msq_init, lsq)
5482       lsq = *(floor(p'));        # load in loop
5483       result = realign_load (msq, lsq, realignment_token);
5484
5485    For the case of dr_explicit_realign:
5486     loop:
5487       msq = *(floor(p));        # load in loop
5488       p' = p + (VS-1);
5489       lsq = *(floor(p'));       # load in loop
5490       result = realign_load (msq, lsq, realignment_token);
5491
5492    Input:
5493    STMT - (scalar) load stmt to be vectorized. This load accesses
5494           a memory location that may be unaligned.
5495    BSI - place where new code is to be inserted.
5496    ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
5497                               is used.  
5498    
5499    Output:
5500    REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
5501                        target hook, if defined.
5502    Return value - the result of the loop-header phi node.  */
5503
5504 static tree
5505 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
5506                         tree *realignment_token,
5507                         enum dr_alignment_support alignment_support_scheme,
5508                         tree init_addr,
5509                         struct loop **at_loop)
5510 {
5511   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5512   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5513   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5514   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5515   edge pe;
5516   tree scalar_dest = gimple_assign_lhs (stmt);
5517   tree vec_dest;
5518   gimple inc;
5519   tree ptr;
5520   tree data_ref;
5521   gimple new_stmt;
5522   basic_block new_bb;
5523   tree msq_init = NULL_TREE;
5524   tree new_temp;
5525   gimple phi_stmt;
5526   tree msq = NULL_TREE;
5527   gimple_seq stmts = NULL;
5528   bool inv_p;
5529   bool compute_in_loop = false;
5530   bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
5531   struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
5532   struct loop *loop_for_initial_load;
5533
5534   gcc_assert (alignment_support_scheme == dr_explicit_realign
5535               || alignment_support_scheme == dr_explicit_realign_optimized);
5536
5537   /* We need to generate three things:
5538      1. the misalignment computation
5539      2. the extra vector load (for the optimized realignment scheme).
5540      3. the phi node for the two vectors from which the realignment is
5541       done (for the optimized realignment scheme).
5542    */
5543
5544   /* 1. Determine where to generate the misalignment computation.
5545
5546      If INIT_ADDR is NULL_TREE, this indicates that the misalignment
5547      calculation will be generated by this function, outside the loop (in the
5548      preheader).  Otherwise, INIT_ADDR had already been computed for us by the
5549      caller, inside the loop.
5550
5551      Background: If the misalignment remains fixed throughout the iterations of
5552      the loop, then both realignment schemes are applicable, and also the
5553      misalignment computation can be done outside LOOP.  This is because we are
5554      vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
5555      are a multiple of VS (the Vector Size), and therefore the misalignment in
5556      different vectorized LOOP iterations is always the same.
5557      The problem arises only if the memory access is in an inner-loop nested
5558      inside LOOP, which is now being vectorized using outer-loop vectorization.
5559      This is the only case when the misalignment of the memory access may not
5560      remain fixed throughout the iterations of the inner-loop (as explained in
5561      detail in vect_supportable_dr_alignment).  In this case, not only is the
5562      optimized realignment scheme not applicable, but also the misalignment
5563      computation (and generation of the realignment token that is passed to
5564      REALIGN_LOAD) have to be done inside the loop.
5565
5566      In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
5567      or not, which in turn determines if the misalignment is computed inside
5568      the inner-loop, or outside LOOP.  */
5569
5570   if (init_addr != NULL_TREE)
5571     {
5572       compute_in_loop = true;
5573       gcc_assert (alignment_support_scheme == dr_explicit_realign);
5574     }
5575
5576
5577   /* 2. Determine where to generate the extra vector load.
5578
5579      For the optimized realignment scheme, instead of generating two vector
5580      loads in each iteration, we generate a single extra vector load in the
5581      preheader of the loop, and in each iteration reuse the result of the
5582      vector load from the previous iteration.  In case the memory access is in
5583      an inner-loop nested inside LOOP, which is now being vectorized using
5584      outer-loop vectorization, we need to determine whether this initial vector
5585      load should be generated at the preheader of the inner-loop, or can be
5586      generated at the preheader of LOOP.  If the memory access has no evolution
5587      in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
5588      to be generated inside LOOP (in the preheader of the inner-loop).  */
5589
5590   if (nested_in_vect_loop)
5591     {
5592       tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
5593       bool invariant_in_outerloop =
5594             (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
5595       loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
5596     }
5597   else
5598     loop_for_initial_load = loop;
5599   if (at_loop)
5600     *at_loop = loop_for_initial_load;
5601
5602   /* 3. For the case of the optimized realignment, create the first vector
5603       load at the loop preheader.  */
5604
5605   if (alignment_support_scheme == dr_explicit_realign_optimized)
5606     {
5607       /* Create msq_init = *(floor(p1)) in the loop preheader  */
5608
5609       gcc_assert (!compute_in_loop);
5610       pe = loop_preheader_edge (loop_for_initial_load);
5611       vec_dest = vect_create_destination_var (scalar_dest, vectype);
5612       ptr = vect_create_data_ref_ptr (stmt, loop_for_initial_load, NULL_TREE,
5613                                       &init_addr, &inc, true, &inv_p);
5614       data_ref = build1 (ALIGN_INDIRECT_REF, vectype, ptr);
5615       new_stmt = gimple_build_assign (vec_dest, data_ref);
5616       new_temp = make_ssa_name (vec_dest, new_stmt);
5617       gimple_assign_set_lhs (new_stmt, new_temp);
5618       mark_symbols_for_renaming (new_stmt);
5619       new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
5620       gcc_assert (!new_bb);
5621       msq_init = gimple_assign_lhs (new_stmt);
5622     }
5623
5624   /* 4. Create realignment token using a target builtin, if available.
5625       It is done either inside the containing loop, or before LOOP (as
5626       determined above).  */
5627
5628   if (targetm.vectorize.builtin_mask_for_load)
5629     {
5630       tree builtin_decl;
5631
5632       /* Compute INIT_ADDR - the initial addressed accessed by this memref.  */
5633       if (compute_in_loop)
5634         gcc_assert (init_addr); /* already computed by the caller.  */
5635       else
5636         {
5637           /* Generate the INIT_ADDR computation outside LOOP.  */
5638           init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
5639                                                         NULL_TREE, loop);
5640           pe = loop_preheader_edge (loop);
5641           new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
5642           gcc_assert (!new_bb);
5643         }
5644
5645       builtin_decl = targetm.vectorize.builtin_mask_for_load ();
5646       new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
5647       vec_dest =
5648         vect_create_destination_var (scalar_dest,
5649                                      gimple_call_return_type (new_stmt));
5650       new_temp = make_ssa_name (vec_dest, new_stmt);
5651       gimple_call_set_lhs (new_stmt, new_temp);
5652
5653       if (compute_in_loop)
5654         gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5655       else
5656         {
5657           /* Generate the misalignment computation outside LOOP.  */
5658           pe = loop_preheader_edge (loop);
5659           new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
5660           gcc_assert (!new_bb);
5661         }
5662
5663       *realignment_token = gimple_call_lhs (new_stmt);
5664
5665       /* The result of the CALL_EXPR to this builtin is determined from
5666          the value of the parameter and no global variables are touched
5667          which makes the builtin a "const" function.  Requiring the
5668          builtin to have the "const" attribute makes it unnecessary
5669          to call mark_call_clobbered.  */
5670       gcc_assert (TREE_READONLY (builtin_decl));
5671     }
5672
5673   if (alignment_support_scheme == dr_explicit_realign)
5674     return msq;
5675
5676   gcc_assert (!compute_in_loop);
5677   gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
5678
5679
5680   /* 5. Create msq = phi <msq_init, lsq> in loop  */
5681
5682   pe = loop_preheader_edge (containing_loop);
5683   vec_dest = vect_create_destination_var (scalar_dest, vectype);
5684   msq = make_ssa_name (vec_dest, NULL);
5685   phi_stmt = create_phi_node (msq, containing_loop->header);
5686   SSA_NAME_DEF_STMT (msq) = phi_stmt;
5687   add_phi_arg (phi_stmt, msq_init, pe);
5688
5689   return msq;
5690 }
5691
5692
5693 /* Function vect_strided_load_supported.
5694
5695    Returns TRUE is EXTRACT_EVEN and EXTRACT_ODD operations are supported,
5696    and FALSE otherwise.  */
5697
5698 static bool
5699 vect_strided_load_supported (tree vectype)
5700 {
5701   optab perm_even_optab, perm_odd_optab;
5702   int mode;
5703
5704   mode = (int) TYPE_MODE (vectype);
5705
5706   perm_even_optab = optab_for_tree_code (VEC_EXTRACT_EVEN_EXPR, vectype,
5707                                          optab_default);
5708   if (!perm_even_optab)
5709     {
5710       if (vect_print_dump_info (REPORT_DETAILS))
5711         fprintf (vect_dump, "no optab for perm_even.");
5712       return false;
5713     }
5714
5715   if (optab_handler (perm_even_optab, mode)->insn_code == CODE_FOR_nothing)
5716     {
5717       if (vect_print_dump_info (REPORT_DETAILS))
5718         fprintf (vect_dump, "perm_even op not supported by target.");
5719       return false;
5720     }
5721
5722   perm_odd_optab = optab_for_tree_code (VEC_EXTRACT_ODD_EXPR, vectype,
5723                                         optab_default);
5724   if (!perm_odd_optab)
5725     {
5726       if (vect_print_dump_info (REPORT_DETAILS))
5727         fprintf (vect_dump, "no optab for perm_odd.");
5728       return false;
5729     }
5730
5731   if (optab_handler (perm_odd_optab, mode)->insn_code == CODE_FOR_nothing)
5732     {
5733       if (vect_print_dump_info (REPORT_DETAILS))
5734         fprintf (vect_dump, "perm_odd op not supported by target.");
5735       return false;
5736     }
5737   return true;
5738 }
5739
5740
5741 /* Function vect_permute_load_chain.
5742
5743    Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5744    a power of 2, generate extract_even/odd stmts to reorder the input data 
5745    correctly. Return the final references for loads in RESULT_CHAIN.
5746
5747    E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5748    The input is 4 vectors each containing 8 elements. We assign a number to each
5749    element, the input sequence is:
5750
5751    1st vec:   0  1  2  3  4  5  6  7
5752    2nd vec:   8  9 10 11 12 13 14 15
5753    3rd vec:  16 17 18 19 20 21 22 23 
5754    4th vec:  24 25 26 27 28 29 30 31
5755
5756    The output sequence should be:
5757
5758    1st vec:  0 4  8 12 16 20 24 28
5759    2nd vec:  1 5  9 13 17 21 25 29
5760    3rd vec:  2 6 10 14 18 22 26 30 
5761    4th vec:  3 7 11 15 19 23 27 31
5762
5763    i.e., the first output vector should contain the first elements of each
5764    interleaving group, etc.
5765
5766    We use extract_even/odd instructions to create such output. The input of each
5767    extract_even/odd operation is two vectors
5768    1st vec    2nd vec 
5769    0 1 2 3    4 5 6 7 
5770
5771    and the output is the vector of extracted even/odd elements. The output of 
5772    extract_even will be:   0 2 4 6
5773    and of extract_odd:     1 3 5 7
5774
5775    
5776    The permutation is done in log LENGTH stages. In each stage extract_even and
5777    extract_odd stmts are created for each pair of vectors in DR_CHAIN in their 
5778    order. In our example, 
5779
5780    E1: extract_even (1st vec, 2nd vec)
5781    E2: extract_odd (1st vec, 2nd vec)
5782    E3: extract_even (3rd vec, 4th vec)
5783    E4: extract_odd (3rd vec, 4th vec)
5784
5785    The output for the first stage will be:
5786
5787    E1:  0  2  4  6  8 10 12 14
5788    E2:  1  3  5  7  9 11 13 15
5789    E3: 16 18 20 22 24 26 28 30 
5790    E4: 17 19 21 23 25 27 29 31
5791
5792    In order to proceed and create the correct sequence for the next stage (or
5793    for the correct output, if the second stage is the last one, as in our 
5794    example), we first put the output of extract_even operation and then the 
5795    output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5796    The input for the second stage is:
5797
5798    1st vec (E1):  0  2  4  6  8 10 12 14
5799    2nd vec (E3): 16 18 20 22 24 26 28 30  
5800    3rd vec (E2):  1  3  5  7  9 11 13 15    
5801    4th vec (E4): 17 19 21 23 25 27 29 31
5802
5803    The output of the second stage:
5804
5805    E1: 0 4  8 12 16 20 24 28
5806    E2: 2 6 10 14 18 22 26 30
5807    E3: 1 5  9 13 17 21 25 29
5808    E4: 3 7 11 15 19 23 27 31
5809
5810    And RESULT_CHAIN after reordering:
5811
5812    1st vec (E1):  0 4  8 12 16 20 24 28
5813    2nd vec (E3):  1 5  9 13 17 21 25 29
5814    3rd vec (E2):  2 6 10 14 18 22 26 30 
5815    4th vec (E4):  3 7 11 15 19 23 27 31.  */
5816
5817 static bool
5818 vect_permute_load_chain (VEC(tree,heap) *dr_chain, 
5819                          unsigned int length, 
5820                          gimple stmt,
5821                          gimple_stmt_iterator *gsi,
5822                          VEC(tree,heap) **result_chain)
5823 {
5824   tree perm_dest, data_ref, first_vect, second_vect;
5825   gimple perm_stmt;
5826   tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5827   int i;
5828   unsigned int j;
5829
5830   /* Check that the operation is supported.  */
5831   if (!vect_strided_load_supported (vectype))
5832     return false;
5833
5834   *result_chain = VEC_copy (tree, heap, dr_chain);
5835   for (i = 0; i < exact_log2 (length); i++)
5836     {
5837       for (j = 0; j < length; j +=2)
5838         {
5839           first_vect = VEC_index (tree, dr_chain, j);
5840           second_vect = VEC_index (tree, dr_chain, j+1);
5841
5842           /* data_ref = permute_even (first_data_ref, second_data_ref);  */
5843           perm_dest = create_tmp_var (vectype, "vect_perm_even");
5844           DECL_GIMPLE_REG_P (perm_dest) = 1;
5845           add_referenced_var (perm_dest);
5846
5847           perm_stmt = gimple_build_assign_with_ops (VEC_EXTRACT_EVEN_EXPR,
5848                                                     perm_dest, first_vect,
5849                                                     second_vect);
5850
5851           data_ref = make_ssa_name (perm_dest, perm_stmt);
5852           gimple_assign_set_lhs (perm_stmt, data_ref);
5853           vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5854           mark_symbols_for_renaming (perm_stmt);
5855
5856           VEC_replace (tree, *result_chain, j/2, data_ref);           
5857               
5858           /* data_ref = permute_odd (first_data_ref, second_data_ref);  */
5859           perm_dest = create_tmp_var (vectype, "vect_perm_odd");
5860           DECL_GIMPLE_REG_P (perm_dest) = 1;
5861           add_referenced_var (perm_dest);
5862
5863           perm_stmt = gimple_build_assign_with_ops (VEC_EXTRACT_ODD_EXPR,
5864                                                     perm_dest, first_vect,
5865                                                     second_vect);
5866           data_ref = make_ssa_name (perm_dest, perm_stmt);
5867           gimple_assign_set_lhs (perm_stmt, data_ref);
5868           vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5869           mark_symbols_for_renaming (perm_stmt);
5870
5871           VEC_replace (tree, *result_chain, j/2+length/2, data_ref);
5872         }
5873       dr_chain = VEC_copy (tree, heap, *result_chain);
5874     }
5875   return true;
5876 }
5877
5878
5879 /* Function vect_transform_strided_load.
5880
5881    Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5882    to perform their permutation and ascribe the result vectorized statements to
5883    the scalar statements.
5884 */
5885
5886 static bool
5887 vect_transform_strided_load (gimple stmt, VEC(tree,heap) *dr_chain, int size,
5888                              gimple_stmt_iterator *gsi)
5889 {
5890   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5891   gimple first_stmt = DR_GROUP_FIRST_DR (stmt_info);
5892   gimple next_stmt, new_stmt;
5893   VEC(tree,heap) *result_chain = NULL;
5894   unsigned int i, gap_count;
5895   tree tmp_data_ref;
5896
5897   /* DR_CHAIN contains input data-refs that are a part of the interleaving. 
5898      RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted 
5899      vectors, that are ready for vector computation.  */
5900   result_chain = VEC_alloc (tree, heap, size);
5901   /* Permute.  */
5902   if (!vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain))
5903     return false;
5904
5905   /* Put a permuted data-ref in the VECTORIZED_STMT field.  
5906      Since we scan the chain starting from it's first node, their order 
5907      corresponds the order of data-refs in RESULT_CHAIN.  */
5908   next_stmt = first_stmt;
5909   gap_count = 1;
5910   for (i = 0; VEC_iterate (tree, result_chain, i, tmp_data_ref); i++)
5911     {
5912       if (!next_stmt)
5913         break;
5914
5915       /* Skip the gaps. Loads created for the gaps will be removed by dead
5916        code elimination pass later. No need to check for the first stmt in
5917        the group, since it always exists.
5918        DR_GROUP_GAP is the number of steps in elements from the previous
5919        access (if there is no gap DR_GROUP_GAP is 1). We skip loads that
5920        correspond to the gaps.
5921       */
5922       if (next_stmt != first_stmt 
5923           && gap_count < DR_GROUP_GAP (vinfo_for_stmt (next_stmt)))
5924       {
5925         gap_count++;
5926         continue;
5927       }
5928
5929       while (next_stmt)
5930         {
5931           new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5932           /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5933              copies, and we put the new vector statement in the first available
5934              RELATED_STMT.  */
5935           if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5936             STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5937           else
5938             {
5939               gimple prev_stmt =
5940                 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5941               gimple rel_stmt =
5942                 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5943               while (rel_stmt)
5944                 {
5945                   prev_stmt = rel_stmt;
5946                   rel_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5947                 }
5948               STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) = new_stmt;
5949             }
5950           next_stmt = DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt));
5951           gap_count = 1;
5952           /* If NEXT_STMT accesses the same DR as the previous statement,
5953              put the same TMP_DATA_REF as its vectorized statement; otherwise
5954              get the next data-ref from RESULT_CHAIN.  */
5955           if (!next_stmt || !DR_GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5956             break;
5957         }
5958     }
5959
5960   VEC_free (tree, heap, result_chain);
5961   return true;
5962 }
5963
5964
5965 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by 
5966    building a vector of type MASK_TYPE from it) and two input vectors placed in
5967    DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
5968    shifting by STRIDE elements of DR_CHAIN for every copy.
5969    (STRIDE is the number of vectorized stmts for NODE divided by the number of
5970    copies).  
5971    VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
5972    the created stmts must be inserted.  */
5973
5974 static inline void
5975 vect_create_mask_and_perm (gimple stmt, gimple next_scalar_stmt, 
5976                            int *mask_array, int mask_nunits, 
5977                            tree mask_element_type, tree mask_type,
5978                            int first_vec_indx, int second_vec_indx, 
5979                            gimple_stmt_iterator *gsi, slp_tree node, 
5980                            tree builtin_decl, tree vectype, 
5981                            VEC(tree,heap) *dr_chain,
5982                            int ncopies, int vect_stmts_counter)
5983 {
5984   tree t = NULL_TREE, mask_vec, mask, perm_dest;
5985   gimple perm_stmt = NULL;
5986   stmt_vec_info next_stmt_info;
5987   int i, group_size, stride, dr_chain_size;
5988   tree first_vec, second_vec, data_ref;
5989   tree sym;
5990   ssa_op_iter iter;
5991   VEC (tree, heap) *params = NULL;
5992
5993   /* Create a vector mask.  */
5994   for (i = mask_nunits - 1; i >= 0; --i)
5995     t = tree_cons (NULL_TREE, build_int_cst (mask_element_type, mask_array[i]),
5996                    t);
5997
5998   mask_vec = build_vector (mask_type, t);
5999   mask = vect_init_vector (stmt, mask_vec, mask_type, NULL);
6000
6001   group_size = VEC_length (gimple, SLP_TREE_SCALAR_STMTS (node));
6002   stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
6003   dr_chain_size = VEC_length (tree, dr_chain); 
6004
6005   /* Initialize the vect stmts of NODE to properly insert the generated 
6006      stmts later.  */
6007   for (i = VEC_length (gimple, SLP_TREE_VEC_STMTS (node)); 
6008        i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
6009     VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (node), NULL);
6010
6011   perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
6012   for (i = 0; i < ncopies; i++)
6013     {
6014       first_vec = VEC_index (tree, dr_chain, first_vec_indx);
6015       second_vec = VEC_index (tree, dr_chain, second_vec_indx);
6016
6017       /* Build argument list for the vectorized call.  */
6018       VEC_free (tree, heap, params);
6019       params = VEC_alloc (tree, heap, 3);
6020       VEC_quick_push (tree, params, first_vec);
6021       VEC_quick_push (tree, params, second_vec);
6022       VEC_quick_push (tree, params, mask);
6023
6024       /* Generate the permute statement.  */
6025       perm_stmt = gimple_build_call_vec (builtin_decl, params);
6026       data_ref = make_ssa_name (perm_dest, perm_stmt);
6027       gimple_call_set_lhs (perm_stmt, data_ref);
6028       vect_finish_stmt_generation (stmt, perm_stmt, gsi);
6029       FOR_EACH_SSA_TREE_OPERAND (sym, perm_stmt, iter, SSA_OP_ALL_VIRTUALS)
6030         {
6031           if (TREE_CODE (sym) == SSA_NAME)
6032             sym = SSA_NAME_VAR (sym);
6033           mark_sym_for_renaming (sym);
6034         }
6035
6036       /* Store the vector statement in NODE.  */ 
6037       VEC_replace (gimple, SLP_TREE_VEC_STMTS (node), 
6038                    stride * i + vect_stmts_counter, perm_stmt);
6039
6040       first_vec_indx += stride;
6041       second_vec_indx += stride;
6042     }
6043
6044   /* Mark the scalar stmt as vectorized.  */
6045   next_stmt_info = vinfo_for_stmt (next_scalar_stmt);
6046   STMT_VINFO_VEC_STMT (next_stmt_info) = perm_stmt;
6047 }
6048
6049
6050 /* Given FIRST_MASK_ELEMENT - the mask element in element representation, 
6051    return in CURRENT_MASK_ELEMENT its equivalent in target specific
6052    representation. Check that the mask is valid and return FALSE if not. 
6053    Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
6054    the next vector, i.e., the current first vector is not needed.  */
6055    
6056 static bool
6057 vect_get_mask_element (gimple stmt, int first_mask_element, int m, 
6058                        int mask_nunits, bool only_one_vec, int index,
6059                        int *mask, int *current_mask_element, 
6060                        bool *need_next_vector)
6061 {
6062   int i;
6063   static int number_of_mask_fixes = 1;
6064   static bool mask_fixed = false;
6065   static bool needs_first_vector = false;
6066
6067   /* Convert to target specific representation.  */
6068   *current_mask_element = first_mask_element + m;
6069   /* Adjust the value in case it's a mask for second and third vectors.  */
6070   *current_mask_element -= mask_nunits * (number_of_mask_fixes - 1);
6071
6072   if (*current_mask_element < mask_nunits)
6073     needs_first_vector = true;
6074
6075   /* We have only one input vector to permute but the mask accesses values in
6076      the next vector as well.  */
6077   if (only_one_vec && *current_mask_element >= mask_nunits)
6078     {
6079       if (vect_print_dump_info (REPORT_DETAILS))
6080         {
6081           fprintf (vect_dump, "permutation requires at least two vectors ");
6082           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
6083         }
6084
6085       return false;
6086     }
6087
6088   /* The mask requires the next vector.  */
6089   if (*current_mask_element >= mask_nunits * 2)
6090     {
6091       if (needs_first_vector || mask_fixed)
6092         {
6093           /* We either need the first vector too or have already moved to the
6094              next vector. In both cases, this permutation needs three   
6095              vectors.  */
6096           if (vect_print_dump_info (REPORT_DETAILS))
6097             {
6098               fprintf (vect_dump, "permutation requires at "
6099                                   "least three vectors ");
6100               print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
6101             }
6102
6103           return false;
6104         }
6105
6106       /* We move to the next vector, dropping the first one and working with
6107          the second and the third - we need to adjust the values of the mask
6108          accordingly.  */
6109       *current_mask_element -= mask_nunits * number_of_mask_fixes;
6110
6111       for (i = 0; i < index; i++)
6112         mask[i] -= mask_nunits * number_of_mask_fixes;
6113
6114       (number_of_mask_fixes)++;
6115       mask_fixed = true;
6116     }
6117
6118   *need_next_vector = mask_fixed;
6119
6120   /* This was the last element of this mask. Start a new one.  */
6121   if (index == mask_nunits - 1)
6122     {
6123       number_of_mask_fixes = 1;
6124       mask_fixed = false;
6125       needs_first_vector = false;
6126     }
6127
6128   return true;
6129 }
6130
6131
6132 /* Generate vector permute statements from a list of loads in DR_CHAIN.
6133    If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
6134    permute statements for SLP_NODE_INSTANCE.  */
6135 bool
6136 vect_transform_slp_perm_load (gimple stmt, VEC (tree, heap) *dr_chain,
6137                               gimple_stmt_iterator *gsi, int vf,
6138                               slp_instance slp_node_instance, bool analyze_only)
6139 {
6140   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6141   tree mask_element_type = NULL_TREE, mask_type;
6142   int i, j, k, m, scale, mask_nunits, nunits, vec_index = 0, scalar_index;
6143   slp_tree node;
6144   tree vectype = STMT_VINFO_VECTYPE (stmt_info), builtin_decl;
6145   gimple next_scalar_stmt;
6146   int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
6147   int first_mask_element;
6148   int index, unroll_factor, *mask, current_mask_element, ncopies;
6149   bool only_one_vec = false, need_next_vector = false;
6150   int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
6151
6152   if (!targetm.vectorize.builtin_vec_perm)
6153     {
6154       if (vect_print_dump_info (REPORT_DETAILS))
6155         {
6156           fprintf (vect_dump, "no builtin for vect permute for ");
6157           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
6158         }
6159
6160        return false;
6161     }
6162
6163   builtin_decl = targetm.vectorize.builtin_vec_perm (vectype,
6164                                                      &mask_element_type);
6165   if (!builtin_decl || !mask_element_type)
6166     {
6167       if (vect_print_dump_info (REPORT_DETAILS))
6168         {
6169           fprintf (vect_dump, "no builtin for vect permute for ");
6170           print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
6171         }
6172
6173        return false;
6174     }
6175
6176   mask_type = get_vectype_for_scalar_type (mask_element_type);
6177   mask_nunits = TYPE_VECTOR_SUBPARTS (mask_type);
6178   mask = (int *) xmalloc (sizeof (int) * mask_nunits);
6179   nunits = TYPE_VECTOR_SUBPARTS (vectype);
6180   scale = mask_nunits / nunits;
6181   unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
6182
6183   /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
6184      unrolling factor.  */
6185   orig_vec_stmts_num = group_size * 
6186                 SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance) / nunits;
6187   if (orig_vec_stmts_num == 1)
6188     only_one_vec = true;
6189
6190   /* Number of copies is determined by the final vectorization factor 
6191      relatively to SLP_NODE_INSTANCE unrolling factor.  */
6192   ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance); 
6193
6194   /* Generate permutation masks for every NODE. Number of masks for each NODE 
6195      is equal to GROUP_SIZE.  
6196      E.g., we have a group of three nodes with three loads from the same 
6197      location in each node, and the vector size is 4. I.e., we have a 
6198      a0b0c0a1b1c1... sequence and we need to create the following vectors: 
6199      for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
6200      for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
6201      ...
6202
6203      The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9} (in target
6204      scpecific type, e.g., in bytes for Altivec.
6205      The last mask is illegal since we assume two operands for permute 
6206      operation, and the mask element values can't be outside that range. Hence,
6207      the last mask must be converted into {2,5,5,5}.
6208      For the first two permutations we need the first and the second input 
6209      vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
6210      we need the second and the third vectors: {b1,c1,a2,b2} and 
6211      {c2,a3,b3,c3}.  */
6212
6213   for (i = 0;
6214        VEC_iterate (slp_tree, SLP_INSTANCE_LOADS (slp_node_instance),
6215                     i, node);
6216        i++)
6217     {
6218       scalar_index = 0;
6219       index = 0;
6220       vect_stmts_counter = 0;
6221       vec_index = 0;
6222       first_vec_index = vec_index++;
6223       if (only_one_vec)
6224         second_vec_index = first_vec_index;
6225       else
6226         second_vec_index =  vec_index++;
6227
6228       for (j = 0; j < unroll_factor; j++)
6229         {
6230           for (k = 0; k < group_size; k++)
6231             {
6232               first_mask_element = (i + j * group_size) * scale;
6233               for (m = 0; m < scale; m++)
6234                 {
6235                   if (!vect_get_mask_element (stmt, first_mask_element, m, 
6236                                    mask_nunits, only_one_vec, index, mask,
6237                                    &current_mask_element, &need_next_vector))
6238                     return false;
6239
6240                   mask[index++] = current_mask_element;
6241                 } 
6242
6243               if (index == mask_nunits)
6244                 {
6245                   index = 0;
6246                   if (!analyze_only)
6247                     {
6248                       if (need_next_vector)
6249                         {
6250                           first_vec_index = second_vec_index;
6251                           second_vec_index = vec_index;
6252                         }
6253
6254                       next_scalar_stmt = VEC_index (gimple,
6255                                 SLP_TREE_SCALAR_STMTS (node), scalar_index++);
6256
6257                       vect_create_mask_and_perm (stmt, next_scalar_stmt,
6258                                mask, mask_nunits, mask_element_type, mask_type, 
6259                                first_vec_index, second_vec_index, gsi, node, 
6260                                builtin_decl, vectype, dr_chain, ncopies, 
6261                                vect_stmts_counter++);
6262                     }
6263                 } 
6264             } 
6265         } 
6266     } 
6267
6268   free (mask);
6269   return true;
6270 }
6271
6272 /* vectorizable_load.
6273
6274    Check if STMT reads a non scalar data-ref (array/pointer/structure) that 
6275    can be vectorized. 
6276    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
6277    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
6278    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
6279
6280 bool
6281 vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
6282                    slp_tree slp_node, slp_instance slp_node_instance)
6283 {
6284   tree scalar_dest;
6285   tree vec_dest = NULL;
6286   tree data_ref = NULL;
6287   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6288   stmt_vec_info prev_stmt_info; 
6289   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6290   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
6291   struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
6292   bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
6293   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
6294   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6295   tree new_temp;
6296   int mode;
6297   gimple new_stmt = NULL;
6298   tree dummy;
6299   enum dr_alignment_support alignment_support_scheme;
6300   tree dataref_ptr = NULL_TREE;
6301   gimple ptr_incr;
6302   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6303   int ncopies;
6304   int i, j, group_size;
6305   tree msq = NULL_TREE, lsq;
6306   tree offset = NULL_TREE;
6307   tree realignment_token = NULL_TREE;
6308   gimple phi = NULL;
6309   VEC(tree,heap) *dr_chain = NULL;
6310   bool strided_load = false;
6311   gimple first_stmt;
6312   tree scalar_type;
6313   bool inv_p;
6314   bool compute_in_loop = false;
6315   struct loop *at_loop;
6316   int vec_num;
6317   bool slp = (slp_node != NULL);
6318   bool slp_perm = false;
6319   enum tree_code code;
6320
6321   /* Multiple types in SLP are handled by creating the appropriate number of
6322      vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6323      case of SLP.  */
6324   if (slp)
6325     ncopies = 1;
6326   else
6327     ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6328
6329   gcc_assert (ncopies >= 1);
6330
6331   /* FORNOW. This restriction should be relaxed.  */
6332   if (nested_in_vect_loop && ncopies > 1)
6333     {
6334       if (vect_print_dump_info (REPORT_DETAILS))
6335         fprintf (vect_dump, "multiple types in nested loop.");
6336       return false;
6337     }
6338
6339   if (slp && SLP_INSTANCE_LOAD_PERMUTATION (slp_node_instance))
6340     slp_perm = true;
6341  
6342   if (!STMT_VINFO_RELEVANT_P (stmt_info))
6343     return false;
6344
6345   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
6346     return false;
6347
6348   /* Is vectorizable load? */
6349   if (!is_gimple_assign (stmt))
6350     return false;
6351
6352   scalar_dest = gimple_assign_lhs (stmt);
6353   if (TREE_CODE (scalar_dest) != SSA_NAME)
6354     return false;
6355
6356   code = gimple_assign_rhs_code (stmt);
6357   if (code != ARRAY_REF
6358       && code != INDIRECT_REF
6359       && !STMT_VINFO_STRIDED_ACCESS (stmt_info))
6360     return false;
6361
6362   if (!STMT_VINFO_DATA_REF (stmt_info))
6363     return false;
6364
6365   scalar_type = TREE_TYPE (DR_REF (dr));
6366   mode = (int) TYPE_MODE (vectype);
6367
6368   /* FORNOW. In some cases can vectorize even if data-type not supported
6369     (e.g. - data copies).  */
6370   if (optab_handler (mov_optab, mode)->insn_code == CODE_FOR_nothing)
6371     {
6372       if (vect_print_dump_info (REPORT_DETAILS))
6373         fprintf (vect_dump, "Aligned load, but unsupported type.");
6374       return false;
6375     }
6376
6377   /* If accesses through a pointer to vectype do not alias the original
6378      memory reference we have a problem.  */
6379   if (get_alias_set (vectype) != get_alias_set (scalar_type)
6380       && !alias_set_subset_of (get_alias_set (vectype),
6381                                get_alias_set (scalar_type)))
6382     {
6383       if (vect_print_dump_info (REPORT_DETAILS))
6384         fprintf (vect_dump, "vector type does not alias scalar type");
6385       return false;
6386     }
6387
6388   /* Check if the load is a part of an interleaving chain.  */
6389   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
6390     {
6391       strided_load = true;
6392       /* FORNOW */
6393       gcc_assert (! nested_in_vect_loop);
6394
6395       /* Check if interleaving is supported.  */
6396       if (!vect_strided_load_supported (vectype)
6397           && !PURE_SLP_STMT (stmt_info) && !slp)
6398         return false;
6399     }
6400
6401   if (!vec_stmt) /* transformation not required.  */
6402     {
6403       STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
6404       vect_model_load_cost (stmt_info, ncopies, NULL);
6405       return true;
6406     }
6407
6408   if (vect_print_dump_info (REPORT_DETAILS))
6409     fprintf (vect_dump, "transform load.");
6410
6411   /** Transform.  **/
6412
6413   if (strided_load)
6414     {
6415       first_stmt = DR_GROUP_FIRST_DR (stmt_info);
6416       /* Check if the chain of loads is already vectorized.  */
6417       if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt)))
6418         {
6419           *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6420           return true;
6421         }
6422       first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
6423       group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
6424
6425       /* VEC_NUM is the number of vect stmts to be created for this group.  */
6426       if (slp)
6427         {
6428           strided_load = false;
6429           vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6430         }
6431       else
6432         vec_num = group_size;
6433
6434       dr_chain = VEC_alloc (tree, heap, vec_num);
6435     }
6436   else
6437     {
6438       first_stmt = stmt;
6439       first_dr = dr;
6440       group_size = vec_num = 1;
6441     }
6442
6443   alignment_support_scheme = vect_supportable_dr_alignment (first_dr);
6444   gcc_assert (alignment_support_scheme);
6445
6446   /* In case the vectorization factor (VF) is bigger than the number
6447      of elements that we can fit in a vectype (nunits), we have to generate
6448      more than one vector stmt - i.e - we need to "unroll" the
6449      vector stmt by a factor VF/nunits. In doing so, we record a pointer
6450      from one copy of the vector stmt to the next, in the field
6451      STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
6452      stages to find the correct vector defs to be used when vectorizing
6453      stmts that use the defs of the current stmt. The example below illustrates
6454      the vectorization process when VF=16 and nunits=4 (i.e - we need to create
6455      4 vectorized stmts):
6456
6457      before vectorization:
6458                                 RELATED_STMT    VEC_STMT
6459         S1:     x = memref      -               -
6460         S2:     z = x + 1       -               -
6461
6462      step 1: vectorize stmt S1:
6463         We first create the vector stmt VS1_0, and, as usual, record a
6464         pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
6465         Next, we create the vector stmt VS1_1, and record a pointer to
6466         it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
6467         Similarly, for VS1_2 and VS1_3. This is the resulting chain of
6468         stmts and pointers:
6469                                 RELATED_STMT    VEC_STMT
6470         VS1_0:  vx0 = memref0   VS1_1           -
6471         VS1_1:  vx1 = memref1   VS1_2           -
6472         VS1_2:  vx2 = memref2   VS1_3           -
6473         VS1_3:  vx3 = memref3   -               -
6474         S1:     x = load        -               VS1_0
6475         S2:     z = x + 1       -               -
6476
6477      See in documentation in vect_get_vec_def_for_stmt_copy for how the 
6478      information we recorded in RELATED_STMT field is used to vectorize 
6479      stmt S2.  */
6480
6481   /* In case of interleaving (non-unit strided access):
6482
6483      S1:  x2 = &base + 2
6484      S2:  x0 = &base
6485      S3:  x1 = &base + 1
6486      S4:  x3 = &base + 3
6487
6488      Vectorized loads are created in the order of memory accesses 
6489      starting from the access of the first stmt of the chain:
6490
6491      VS1: vx0 = &base
6492      VS2: vx1 = &base + vec_size*1
6493      VS3: vx3 = &base + vec_size*2
6494      VS4: vx4 = &base + vec_size*3
6495
6496      Then permutation statements are generated:
6497
6498      VS5: vx5 = VEC_EXTRACT_EVEN_EXPR < vx0, vx1 >
6499      VS6: vx6 = VEC_EXTRACT_ODD_EXPR < vx0, vx1 >
6500        ...
6501
6502      And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6503      (the order of the data-refs in the output of vect_permute_load_chain
6504      corresponds to the order of scalar stmts in the interleaving chain - see
6505      the documentation of vect_permute_load_chain()).
6506      The generation of permutation stmts and recording them in
6507      STMT_VINFO_VEC_STMT is done in vect_transform_strided_load().
6508
6509      In case of both multiple types and interleaving, the vector loads and 
6510      permutation stmts above are created for every copy. The result vector stmts
6511      are put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
6512      STMT_VINFO_RELATED_STMT for the next copies.  */
6513
6514   /* If the data reference is aligned (dr_aligned) or potentially unaligned
6515      on a target that supports unaligned accesses (dr_unaligned_supported)
6516      we generate the following code:
6517          p = initial_addr;
6518          indx = 0;
6519          loop {
6520            p = p + indx * vectype_size;
6521            vec_dest = *(p);
6522            indx = indx + 1;
6523          }
6524
6525      Otherwise, the data reference is potentially unaligned on a target that
6526      does not support unaligned accesses (dr_explicit_realign_optimized) - 
6527      then generate the following code, in which the data in each iteration is
6528      obtained by two vector loads, one from the previous iteration, and one
6529      from the current iteration:
6530          p1 = initial_addr;
6531          msq_init = *(floor(p1))
6532          p2 = initial_addr + VS - 1;
6533          realignment_token = call target_builtin;
6534          indx = 0;
6535          loop {
6536            p2 = p2 + indx * vectype_size
6537            lsq = *(floor(p2))
6538            vec_dest = realign_load (msq, lsq, realignment_token)
6539            indx = indx + 1;
6540            msq = lsq;
6541          }   */
6542
6543   /* If the misalignment remains the same throughout the execution of the
6544      loop, we can create the init_addr and permutation mask at the loop
6545      preheader. Otherwise, it needs to be created inside the loop.
6546      This can only occur when vectorizing memory accesses in the inner-loop
6547      nested within an outer-loop that is being vectorized.  */
6548
6549   if (nested_in_vect_loop_p (loop, stmt)
6550       && (TREE_INT_CST_LOW (DR_STEP (dr))
6551           % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
6552     {
6553       gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
6554       compute_in_loop = true;
6555     }
6556
6557   if ((alignment_support_scheme == dr_explicit_realign_optimized
6558        || alignment_support_scheme == dr_explicit_realign)
6559       && !compute_in_loop)
6560     {
6561       msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
6562                                     alignment_support_scheme, NULL_TREE,
6563                                     &at_loop);
6564       if (alignment_support_scheme == dr_explicit_realign_optimized)
6565         {
6566           phi = SSA_NAME_DEF_STMT (msq);
6567           offset = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
6568         }
6569     }
6570   else
6571     at_loop = loop;
6572
6573   prev_stmt_info = NULL;
6574   for (j = 0; j < ncopies; j++)
6575     { 
6576       /* 1. Create the vector pointer update chain.  */
6577       if (j == 0)
6578         dataref_ptr = vect_create_data_ref_ptr (first_stmt,
6579                                                 at_loop, offset, 
6580                                                 &dummy, &ptr_incr, false, 
6581                                                 &inv_p);
6582       else
6583         dataref_ptr = 
6584                 bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt, NULL_TREE);
6585
6586       for (i = 0; i < vec_num; i++)
6587         {
6588           if (i > 0)
6589             dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
6590                                            NULL_TREE);
6591
6592           /* 2. Create the vector-load in the loop.  */
6593           switch (alignment_support_scheme)
6594             {
6595             case dr_aligned:
6596               gcc_assert (aligned_access_p (first_dr));
6597               data_ref = build_fold_indirect_ref (dataref_ptr);
6598               break;
6599             case dr_unaligned_supported:
6600               {
6601                 int mis = DR_MISALIGNMENT (first_dr);
6602                 tree tmis = (mis == -1 ? size_zero_node : size_int (mis));
6603
6604                 tmis = size_binop (MULT_EXPR, tmis, size_int(BITS_PER_UNIT));
6605                 data_ref =
6606                   build2 (MISALIGNED_INDIRECT_REF, vectype, dataref_ptr, tmis);
6607                 break;
6608               }
6609             case dr_explicit_realign:
6610               {
6611                 tree ptr, bump;
6612                 tree vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
6613
6614                 if (compute_in_loop)
6615                   msq = vect_setup_realignment (first_stmt, gsi,
6616                                                 &realignment_token,
6617                                                 dr_explicit_realign, 
6618                                                 dataref_ptr, NULL);
6619
6620                 data_ref = build1 (ALIGN_INDIRECT_REF, vectype, dataref_ptr);
6621                 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6622                 new_stmt = gimple_build_assign (vec_dest, data_ref);
6623                 new_temp = make_ssa_name (vec_dest, new_stmt);
6624                 gimple_assign_set_lhs (new_stmt, new_temp);
6625                 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6626                 copy_virtual_operands (new_stmt, stmt);
6627                 mark_symbols_for_renaming (new_stmt);
6628                 msq = new_temp;
6629
6630                 bump = size_binop (MULT_EXPR, vs_minus_1,
6631                                    TYPE_SIZE_UNIT (scalar_type));
6632                 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
6633                 data_ref = build1 (ALIGN_INDIRECT_REF, vectype, ptr);
6634                 break;
6635               }
6636             case dr_explicit_realign_optimized:
6637               data_ref = build1 (ALIGN_INDIRECT_REF, vectype, dataref_ptr);
6638               break;
6639             default:
6640               gcc_unreachable ();
6641             }
6642           vec_dest = vect_create_destination_var (scalar_dest, vectype);
6643           new_stmt = gimple_build_assign (vec_dest, data_ref);
6644           new_temp = make_ssa_name (vec_dest, new_stmt);
6645           gimple_assign_set_lhs (new_stmt, new_temp);
6646           vect_finish_stmt_generation (stmt, new_stmt, gsi);
6647           mark_symbols_for_renaming (new_stmt);
6648
6649           /* 3. Handle explicit realignment if necessary/supported. Create in
6650                 loop: vec_dest = realign_load (msq, lsq, realignment_token)  */
6651           if (alignment_support_scheme == dr_explicit_realign_optimized
6652               || alignment_support_scheme == dr_explicit_realign)
6653             {
6654               tree tmp;
6655
6656               lsq = gimple_assign_lhs (new_stmt);
6657               if (!realignment_token)
6658                 realignment_token = dataref_ptr;
6659               vec_dest = vect_create_destination_var (scalar_dest, vectype);
6660               tmp = build3 (REALIGN_LOAD_EXPR, vectype, msq, lsq,
6661                             realignment_token);
6662               new_stmt = gimple_build_assign (vec_dest, tmp);
6663               new_temp = make_ssa_name (vec_dest, new_stmt);
6664               gimple_assign_set_lhs (new_stmt, new_temp);
6665               vect_finish_stmt_generation (stmt, new_stmt, gsi);
6666
6667               if (alignment_support_scheme == dr_explicit_realign_optimized)
6668                 {
6669                   gcc_assert (phi);
6670                   if (i == vec_num - 1 && j == ncopies - 1)
6671                     add_phi_arg (phi, lsq, loop_latch_edge (containing_loop));
6672                   msq = lsq;
6673                 }
6674             }
6675
6676           /* 4. Handle invariant-load.  */
6677           if (inv_p)
6678             {
6679               gcc_assert (!strided_load);
6680               gcc_assert (nested_in_vect_loop_p (loop, stmt));
6681               if (j == 0)
6682                 {
6683                   int k;
6684                   tree t = NULL_TREE;
6685                   tree vec_inv, bitpos, bitsize = TYPE_SIZE (scalar_type);
6686
6687                   /* CHECKME: bitpos depends on endianess?  */
6688                   bitpos = bitsize_zero_node;
6689                   vec_inv = build3 (BIT_FIELD_REF, scalar_type, new_temp, 
6690                                     bitsize, bitpos);
6691                   vec_dest = 
6692                         vect_create_destination_var (scalar_dest, NULL_TREE);
6693                   new_stmt = gimple_build_assign (vec_dest, vec_inv);
6694                   new_temp = make_ssa_name (vec_dest, new_stmt);
6695                   gimple_assign_set_lhs (new_stmt, new_temp);
6696                   vect_finish_stmt_generation (stmt, new_stmt, gsi);
6697
6698                   for (k = nunits - 1; k >= 0; --k)
6699                     t = tree_cons (NULL_TREE, new_temp, t);
6700                   /* FIXME: use build_constructor directly.  */
6701                   vec_inv = build_constructor_from_list (vectype, t);
6702                   new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
6703                   new_stmt = SSA_NAME_DEF_STMT (new_temp);
6704                 }
6705               else
6706                 gcc_unreachable (); /* FORNOW. */
6707             }
6708
6709           /* Collect vector loads and later create their permutation in
6710              vect_transform_strided_load ().  */
6711           if (strided_load || slp_perm)
6712             VEC_quick_push (tree, dr_chain, new_temp);
6713
6714          /* Store vector loads in the corresponding SLP_NODE.  */
6715           if (slp && !slp_perm)
6716             VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (slp_node), new_stmt);
6717         }
6718
6719       if (slp && !slp_perm)
6720         continue;
6721
6722       if (slp_perm)
6723         {
6724           if (!vect_transform_slp_perm_load (stmt, dr_chain, gsi,
6725                                    LOOP_VINFO_VECT_FACTOR (loop_vinfo),
6726                                              slp_node_instance, false))
6727             {
6728               VEC_free (tree, heap, dr_chain);
6729               return false;
6730             }
6731         }
6732       else
6733         {
6734           if (strided_load)
6735             {
6736               if (!vect_transform_strided_load (stmt, dr_chain, group_size, gsi))
6737                 return false;     
6738
6739               *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6740               VEC_free (tree, heap, dr_chain);
6741               dr_chain = VEC_alloc (tree, heap, group_size);
6742             }
6743           else
6744             {
6745               if (j == 0)
6746                 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6747               else
6748                 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6749               prev_stmt_info = vinfo_for_stmt (new_stmt);
6750             }
6751         }
6752     }
6753
6754   if (dr_chain)
6755     VEC_free (tree, heap, dr_chain);
6756
6757   return true;
6758 }
6759
6760
6761 /* Function vectorizable_live_operation.
6762
6763    STMT computes a value that is used outside the loop. Check if 
6764    it can be supported.  */
6765
6766 bool
6767 vectorizable_live_operation (gimple stmt,
6768                              gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
6769                              gimple *vec_stmt ATTRIBUTE_UNUSED)
6770 {
6771   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6772   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6773   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
6774   int i;
6775   int op_type;
6776   tree op;
6777   tree def;
6778   gimple def_stmt;
6779   enum vect_def_type dt; 
6780   enum tree_code code;
6781   enum gimple_rhs_class rhs_class;
6782
6783   gcc_assert (STMT_VINFO_LIVE_P (stmt_info));
6784
6785   if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
6786     return false;
6787
6788   if (!is_gimple_assign (stmt))
6789     return false;
6790
6791   if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6792     return false;
6793
6794   /* FORNOW. CHECKME. */
6795   if (nested_in_vect_loop_p (loop, stmt))
6796     return false;
6797
6798   code = gimple_assign_rhs_code (stmt);
6799   op_type = TREE_CODE_LENGTH (code);
6800   rhs_class = get_gimple_rhs_class (code);
6801   gcc_assert (rhs_class != GIMPLE_UNARY_RHS || op_type == unary_op);
6802   gcc_assert (rhs_class != GIMPLE_BINARY_RHS || op_type == binary_op);
6803
6804   /* FORNOW: support only if all uses are invariant. This means
6805      that the scalar operations can remain in place, unvectorized.
6806      The original last scalar value that they compute will be used.  */
6807
6808   for (i = 0; i < op_type; i++)
6809     {
6810       if (rhs_class == GIMPLE_SINGLE_RHS)
6811         op = TREE_OPERAND (gimple_op (stmt, 1), i);
6812       else
6813         op = gimple_op (stmt, i + 1);
6814       if (op && !vect_is_simple_use (op, loop_vinfo, &def_stmt, &def, &dt))
6815         {
6816           if (vect_print_dump_info (REPORT_DETAILS))
6817             fprintf (vect_dump, "use not simple.");
6818           return false;
6819         }
6820
6821       if (dt != vect_invariant_def && dt != vect_constant_def)
6822         return false;
6823     }
6824
6825   /* No transformation is required for the cases we currently support.  */
6826   return true;
6827 }
6828
6829
6830 /* Function vect_is_simple_cond.
6831   
6832    Input:
6833    LOOP - the loop that is being vectorized.
6834    COND - Condition that is checked for simple use.
6835
6836    Returns whether a COND can be vectorized.  Checks whether
6837    condition operands are supportable using vec_is_simple_use.  */
6838
6839 static bool
6840 vect_is_simple_cond (tree cond, loop_vec_info loop_vinfo)
6841 {
6842   tree lhs, rhs;
6843   tree def;
6844   enum vect_def_type dt;
6845
6846   if (!COMPARISON_CLASS_P (cond))
6847     return false;
6848
6849   lhs = TREE_OPERAND (cond, 0);
6850   rhs = TREE_OPERAND (cond, 1);
6851
6852   if (TREE_CODE (lhs) == SSA_NAME)
6853     {
6854       gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
6855       if (!vect_is_simple_use (lhs, loop_vinfo, &lhs_def_stmt, &def, &dt))
6856         return false;
6857     }
6858   else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
6859            && TREE_CODE (lhs) != FIXED_CST)
6860     return false;
6861
6862   if (TREE_CODE (rhs) == SSA_NAME)
6863     {
6864       gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
6865       if (!vect_is_simple_use (rhs, loop_vinfo, &rhs_def_stmt, &def, &dt))
6866         return false;
6867     }
6868   else if (TREE_CODE (rhs) != INTEGER_CST  && TREE_CODE (rhs) != REAL_CST
6869            && TREE_CODE (rhs) != FIXED_CST)
6870     return false;
6871
6872   return true;
6873 }
6874
6875 /* vectorizable_condition.
6876
6877    Check if STMT is conditional modify expression that can be vectorized. 
6878    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
6879    stmt using VEC_COND_EXPR  to replace it, put it in VEC_STMT, and insert it 
6880    at BSI.
6881
6882    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
6883
6884 bool
6885 vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
6886                         gimple *vec_stmt)
6887 {
6888   tree scalar_dest = NULL_TREE;
6889   tree vec_dest = NULL_TREE;
6890   tree op = NULL_TREE;
6891   tree cond_expr, then_clause, else_clause;
6892   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6893   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6894   tree vec_cond_lhs, vec_cond_rhs, vec_then_clause, vec_else_clause;
6895   tree vec_compare, vec_cond_expr;
6896   tree new_temp;
6897   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6898   enum machine_mode vec_mode;
6899   tree def;
6900   enum vect_def_type dt;
6901   int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6902   int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6903   enum tree_code code;
6904
6905   gcc_assert (ncopies >= 1);
6906   if (ncopies > 1)
6907     return false; /* FORNOW */
6908
6909   if (!STMT_VINFO_RELEVANT_P (stmt_info))
6910     return false;
6911
6912   if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_loop_def)
6913     return false;
6914
6915   /* FORNOW: SLP not supported.  */
6916   if (STMT_SLP_TYPE (stmt_info))
6917     return false;
6918
6919   /* FORNOW: not yet supported.  */
6920   if (STMT_VINFO_LIVE_P (stmt_info))
6921     {
6922       if (vect_print_dump_info (REPORT_DETAILS))
6923         fprintf (vect_dump, "value used after loop.");
6924       return false;
6925     }
6926
6927   /* Is vectorizable conditional operation?  */
6928   if (!is_gimple_assign (stmt))
6929     return false;
6930
6931   code = gimple_assign_rhs_code (stmt);
6932
6933   if (code != COND_EXPR)
6934     return false;
6935
6936   gcc_assert (gimple_assign_single_p (stmt));
6937   op = gimple_assign_rhs1 (stmt);
6938   cond_expr = TREE_OPERAND (op, 0);
6939   then_clause = TREE_OPERAND (op, 1);
6940   else_clause = TREE_OPERAND (op, 2);
6941
6942   if (!vect_is_simple_cond (cond_expr, loop_vinfo))
6943     return false;
6944
6945   /* We do not handle two different vector types for the condition
6946      and the values.  */
6947   if (TREE_TYPE (TREE_OPERAND (cond_expr, 0)) != TREE_TYPE (vectype))
6948     return false;
6949
6950   if (TREE_CODE (then_clause) == SSA_NAME)
6951     {
6952       gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
6953       if (!vect_is_simple_use (then_clause, loop_vinfo, 
6954                                &then_def_stmt, &def, &dt))
6955         return false;
6956     }
6957   else if (TREE_CODE (then_clause) != INTEGER_CST 
6958            && TREE_CODE (then_clause) != REAL_CST
6959            && TREE_CODE (then_clause) != FIXED_CST)
6960     return false;
6961
6962   if (TREE_CODE (else_clause) == SSA_NAME)
6963     {
6964       gimple else_def_stmt = SSA_NAME_DEF_STMT (else_clause);
6965       if (!vect_is_simple_use (else_clause, loop_vinfo, 
6966                                &else_def_stmt, &def, &dt))
6967         return false;
6968     }
6969   else if (TREE_CODE (else_clause) != INTEGER_CST 
6970            && TREE_CODE (else_clause) != REAL_CST
6971            && TREE_CODE (else_clause) != FIXED_CST)
6972     return false;
6973
6974
6975   vec_mode = TYPE_MODE (vectype);
6976
6977   if (!vec_stmt) 
6978     {
6979       STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
6980       return expand_vec_cond_expr_p (op, vec_mode);
6981     }
6982
6983   /* Transform */
6984
6985   /* Handle def.  */
6986   scalar_dest = gimple_assign_lhs (stmt);
6987   vec_dest = vect_create_destination_var (scalar_dest, vectype);
6988
6989   /* Handle cond expr.  */
6990   vec_cond_lhs = 
6991     vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0), stmt, NULL);
6992   vec_cond_rhs = 
6993     vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1), stmt, NULL);
6994   vec_then_clause = vect_get_vec_def_for_operand (then_clause, stmt, NULL);
6995   vec_else_clause = vect_get_vec_def_for_operand (else_clause, stmt, NULL);
6996
6997   /* Arguments are ready. Create the new vector stmt.  */
6998   vec_compare = build2 (TREE_CODE (cond_expr), vectype, 
6999                         vec_cond_lhs, vec_cond_rhs);
7000   vec_cond_expr = build3 (VEC_COND_EXPR, vectype, 
7001                           vec_compare, vec_then_clause, vec_else_clause);
7002
7003   *vec_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
7004   new_temp = make_ssa_name (vec_dest, *vec_stmt);
7005   gimple_assign_set_lhs (*vec_stmt, new_temp);
7006   vect_finish_stmt_generation (stmt, *vec_stmt, gsi);
7007   
7008   return true;
7009 }
7010
7011
7012 /* Function vect_transform_stmt.
7013
7014    Create a vectorized stmt to replace STMT, and insert it at BSI.  */
7015
7016 static bool
7017 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
7018                      bool *strided_store, slp_tree slp_node, 
7019                      slp_instance slp_node_instance)
7020 {
7021   bool is_store = false;
7022   gimple vec_stmt = NULL;
7023   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7024   gimple orig_stmt_in_pattern;
7025   bool done;
7026
7027   switch (STMT_VINFO_TYPE (stmt_info))
7028     {
7029     case type_demotion_vec_info_type:
7030       done = vectorizable_type_demotion (stmt, gsi, &vec_stmt, slp_node);
7031       gcc_assert (done);
7032       break;
7033
7034     case type_promotion_vec_info_type:
7035       done = vectorizable_type_promotion (stmt, gsi, &vec_stmt, slp_node);
7036       gcc_assert (done);
7037       break;
7038
7039     case type_conversion_vec_info_type:
7040       done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
7041       gcc_assert (done);
7042       break;
7043
7044     case induc_vec_info_type:
7045       gcc_assert (!slp_node);
7046       done = vectorizable_induction (stmt, gsi, &vec_stmt);
7047       gcc_assert (done);
7048       break;
7049
7050     case op_vec_info_type:
7051       done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
7052       gcc_assert (done);
7053       break;
7054
7055     case assignment_vec_info_type:
7056       done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
7057       gcc_assert (done);
7058       break;
7059
7060     case load_vec_info_type:
7061       done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node, 
7062                                 slp_node_instance);
7063       gcc_assert (done);
7064       break;
7065
7066     case store_vec_info_type:
7067       done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
7068       gcc_assert (done);
7069       if (STMT_VINFO_STRIDED_ACCESS (stmt_info) && !slp_node)
7070         {
7071           /* In case of interleaving, the whole chain is vectorized when the
7072              last store in the chain is reached. Store stmts before the last
7073              one are skipped, and there vec_stmt_info shouldn't be freed
7074              meanwhile.  */
7075           *strided_store = true;
7076           if (STMT_VINFO_VEC_STMT (stmt_info))
7077             is_store = true;
7078           }
7079       else
7080         is_store = true;
7081       break;
7082
7083     case condition_vec_info_type:
7084       gcc_assert (!slp_node);
7085       done = vectorizable_condition (stmt, gsi, &vec_stmt);
7086       gcc_assert (done);
7087       break;
7088
7089     case call_vec_info_type:
7090       gcc_assert (!slp_node);
7091       done = vectorizable_call (stmt, gsi, &vec_stmt);
7092       break;
7093
7094     case reduc_vec_info_type:
7095       gcc_assert (!slp_node);
7096       done = vectorizable_reduction (stmt, gsi, &vec_stmt);
7097       gcc_assert (done);
7098       break;
7099
7100     default:
7101       if (!STMT_VINFO_LIVE_P (stmt_info))
7102         {
7103           if (vect_print_dump_info (REPORT_DETAILS))
7104             fprintf (vect_dump, "stmt not supported.");
7105           gcc_unreachable ();
7106         }
7107     }
7108
7109   if (STMT_VINFO_LIVE_P (stmt_info)
7110       && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
7111     {
7112       done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
7113       gcc_assert (done);
7114     }
7115
7116   if (vec_stmt)
7117     {
7118       STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
7119       orig_stmt_in_pattern = STMT_VINFO_RELATED_STMT (stmt_info);
7120       if (orig_stmt_in_pattern)
7121         {
7122           stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt_in_pattern);
7123           /* STMT was inserted by the vectorizer to replace a computation idiom.
7124              ORIG_STMT_IN_PATTERN is a stmt in the original sequence that 
7125              computed this idiom.  We need to record a pointer to VEC_STMT in 
7126              the stmt_info of ORIG_STMT_IN_PATTERN.  See more details in the 
7127              documentation of vect_pattern_recog.  */
7128           if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
7129             {
7130               gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
7131               STMT_VINFO_VEC_STMT (stmt_vinfo) = vec_stmt;
7132             }
7133         }
7134     }
7135
7136   return is_store; 
7137 }
7138
7139
7140 /* This function builds ni_name = number of iterations loop executes
7141    on the loop preheader.  */
7142
7143 static tree
7144 vect_build_loop_niters (loop_vec_info loop_vinfo)
7145 {
7146   tree ni_name, var;
7147   gimple_seq stmts = NULL;
7148   edge pe;
7149   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7150   tree ni = unshare_expr (LOOP_VINFO_NITERS (loop_vinfo));
7151
7152   var = create_tmp_var (TREE_TYPE (ni), "niters");
7153   add_referenced_var (var);
7154   ni_name = force_gimple_operand (ni, &stmts, false, var);
7155
7156   pe = loop_preheader_edge (loop);
7157   if (stmts)
7158     {
7159       basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
7160       gcc_assert (!new_bb);
7161     }
7162       
7163   return ni_name;
7164 }
7165
7166
7167 /* This function generates the following statements:
7168
7169  ni_name = number of iterations loop executes
7170  ratio = ni_name / vf
7171  ratio_mult_vf_name = ratio * vf
7172
7173  and places them at the loop preheader edge.  */
7174
7175 static void 
7176 vect_generate_tmps_on_preheader (loop_vec_info loop_vinfo, 
7177                                  tree *ni_name_ptr,
7178                                  tree *ratio_mult_vf_name_ptr, 
7179                                  tree *ratio_name_ptr)
7180 {
7181
7182   edge pe;
7183   basic_block new_bb;
7184   gimple_seq stmts;
7185   tree ni_name;
7186   tree var;
7187   tree ratio_name;
7188   tree ratio_mult_vf_name;
7189   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7190   tree ni = LOOP_VINFO_NITERS (loop_vinfo);
7191   int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
7192   tree log_vf;
7193
7194   pe = loop_preheader_edge (loop);
7195
7196   /* Generate temporary variable that contains 
7197      number of iterations loop executes.  */
7198
7199   ni_name = vect_build_loop_niters (loop_vinfo);
7200   log_vf = build_int_cst (TREE_TYPE (ni), exact_log2 (vf));
7201
7202   /* Create: ratio = ni >> log2(vf) */
7203
7204   ratio_name = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ni_name), ni_name, log_vf);
7205   if (!is_gimple_val (ratio_name))
7206     {
7207       var = create_tmp_var (TREE_TYPE (ni), "bnd");
7208       add_referenced_var (var);
7209
7210       stmts = NULL;
7211       ratio_name = force_gimple_operand (ratio_name, &stmts, true, var);
7212       pe = loop_preheader_edge (loop);
7213       new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
7214       gcc_assert (!new_bb);
7215     }
7216        
7217   /* Create: ratio_mult_vf = ratio << log2 (vf).  */
7218
7219   ratio_mult_vf_name = fold_build2 (LSHIFT_EXPR, TREE_TYPE (ratio_name),
7220                                     ratio_name, log_vf);
7221   if (!is_gimple_val (ratio_mult_vf_name))
7222     {
7223       var = create_tmp_var (TREE_TYPE (ni), "ratio_mult_vf");
7224       add_referenced_var (var);
7225
7226       stmts = NULL;
7227       ratio_mult_vf_name = force_gimple_operand (ratio_mult_vf_name, &stmts,
7228                                                  true, var);
7229       pe = loop_preheader_edge (loop);
7230       new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
7231       gcc_assert (!new_bb);
7232     }
7233
7234   *ni_name_ptr = ni_name;
7235   *ratio_mult_vf_name_ptr = ratio_mult_vf_name;
7236   *ratio_name_ptr = ratio_name;
7237     
7238   return;  
7239 }
7240
7241
7242 /*   Function vect_update_ivs_after_vectorizer.
7243
7244      "Advance" the induction variables of LOOP to the value they should take
7245      after the execution of LOOP.  This is currently necessary because the
7246      vectorizer does not handle induction variables that are used after the
7247      loop.  Such a situation occurs when the last iterations of LOOP are
7248      peeled, because:
7249      1. We introduced new uses after LOOP for IVs that were not originally used
7250         after LOOP: the IVs of LOOP are now used by an epilog loop.
7251      2. LOOP is going to be vectorized; this means that it will iterate N/VF
7252         times, whereas the loop IVs should be bumped N times.
7253
7254      Input:
7255      - LOOP - a loop that is going to be vectorized. The last few iterations
7256               of LOOP were peeled.
7257      - NITERS - the number of iterations that LOOP executes (before it is
7258                 vectorized). i.e, the number of times the ivs should be bumped.
7259      - UPDATE_E - a successor edge of LOOP->exit that is on the (only) path
7260                   coming out from LOOP on which there are uses of the LOOP ivs
7261                   (this is the path from LOOP->exit to epilog_loop->preheader).
7262
7263                   The new definitions of the ivs are placed in LOOP->exit.
7264                   The phi args associated with the edge UPDATE_E in the bb
7265                   UPDATE_E->dest are updated accordingly.
7266
7267      Assumption 1: Like the rest of the vectorizer, this function assumes
7268      a single loop exit that has a single predecessor.
7269
7270      Assumption 2: The phi nodes in the LOOP header and in update_bb are
7271      organized in the same order.
7272
7273      Assumption 3: The access function of the ivs is simple enough (see
7274      vect_can_advance_ivs_p).  This assumption will be relaxed in the future.
7275
7276      Assumption 4: Exactly one of the successors of LOOP exit-bb is on a path
7277      coming out of LOOP on which the ivs of LOOP are used (this is the path 
7278      that leads to the epilog loop; other paths skip the epilog loop).  This
7279      path starts with the edge UPDATE_E, and its destination (denoted update_bb)
7280      needs to have its phis updated.
7281  */
7282
7283 static void
7284 vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo, tree niters, 
7285                                   edge update_e)
7286 {
7287   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7288   basic_block exit_bb = single_exit (loop)->dest;
7289   gimple phi, phi1;
7290   gimple_stmt_iterator gsi, gsi1;
7291   basic_block update_bb = update_e->dest;
7292
7293   /* gcc_assert (vect_can_advance_ivs_p (loop_vinfo)); */
7294
7295   /* Make sure there exists a single-predecessor exit bb:  */
7296   gcc_assert (single_pred_p (exit_bb));
7297
7298   for (gsi = gsi_start_phis (loop->header), gsi1 = gsi_start_phis (update_bb);
7299        !gsi_end_p (gsi) && !gsi_end_p (gsi1);
7300        gsi_next (&gsi), gsi_next (&gsi1))
7301     {
7302       tree access_fn = NULL;
7303       tree evolution_part;
7304       tree init_expr;
7305       tree step_expr;
7306       tree var, ni, ni_name;
7307       gimple_stmt_iterator last_gsi;
7308
7309       phi = gsi_stmt (gsi);
7310       phi1 = gsi_stmt (gsi1);
7311       if (vect_print_dump_info (REPORT_DETAILS))
7312         {
7313           fprintf (vect_dump, "vect_update_ivs_after_vectorizer: phi: ");
7314           print_gimple_stmt (vect_dump, phi, 0, TDF_SLIM);
7315         }
7316
7317       /* Skip virtual phi's.  */
7318       if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
7319         {
7320           if (vect_print_dump_info (REPORT_DETAILS))
7321             fprintf (vect_dump, "virtual phi. skip.");
7322           continue;
7323         }
7324
7325       /* Skip reduction phis.  */
7326       if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (phi)) == vect_reduction_def)
7327         { 
7328           if (vect_print_dump_info (REPORT_DETAILS))
7329             fprintf (vect_dump, "reduc phi. skip.");
7330           continue;
7331         } 
7332
7333       access_fn = analyze_scalar_evolution (loop, PHI_RESULT (phi)); 
7334       gcc_assert (access_fn);
7335       STRIP_NOPS (access_fn);
7336       evolution_part =
7337          unshare_expr (evolution_part_in_loop_num (access_fn, loop->num));
7338       gcc_assert (evolution_part != NULL_TREE);
7339       
7340       /* FORNOW: We do not support IVs whose evolution function is a polynomial
7341          of degree >= 2 or exponential.  */
7342       gcc_assert (!tree_is_chrec (evolution_part));
7343
7344       step_expr = evolution_part;
7345       init_expr = unshare_expr (initial_condition_in_loop_num (access_fn, 
7346                                                                loop->num));
7347
7348       if (POINTER_TYPE_P (TREE_TYPE (init_expr)))
7349         ni = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (init_expr), 
7350                           init_expr, 
7351                           fold_convert (sizetype, 
7352                                         fold_build2 (MULT_EXPR, TREE_TYPE (niters),
7353                                                      niters, step_expr)));
7354       else
7355         ni = fold_build2 (PLUS_EXPR, TREE_TYPE (init_expr),
7356                           fold_build2 (MULT_EXPR, TREE_TYPE (init_expr),
7357                                        fold_convert (TREE_TYPE (init_expr),
7358                                                      niters),
7359                                        step_expr),
7360                           init_expr);
7361
7362
7363
7364       var = create_tmp_var (TREE_TYPE (init_expr), "tmp");
7365       add_referenced_var (var);
7366
7367       last_gsi = gsi_last_bb (exit_bb);
7368       ni_name = force_gimple_operand_gsi (&last_gsi, ni, false, var,
7369                                           true, GSI_SAME_STMT);
7370       
7371       /* Fix phi expressions in the successor bb.  */
7372       SET_PHI_ARG_DEF (phi1, update_e->dest_idx, ni_name);
7373     }
7374 }
7375
7376 /* Return the more conservative threshold between the
7377    min_profitable_iters returned by the cost model and the user
7378    specified threshold, if provided.  */
7379
7380 static unsigned int
7381 conservative_cost_threshold (loop_vec_info loop_vinfo,
7382                              int min_profitable_iters)
7383 {
7384   unsigned int th;
7385   int min_scalar_loop_bound;
7386
7387   min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
7388                             * LOOP_VINFO_VECT_FACTOR (loop_vinfo)) - 1);
7389
7390   /* Use the cost model only if it is more conservative than user specified
7391      threshold.  */
7392   th = (unsigned) min_scalar_loop_bound;
7393   if (min_profitable_iters
7394       && (!min_scalar_loop_bound
7395           || min_profitable_iters > min_scalar_loop_bound))
7396     th = (unsigned) min_profitable_iters;
7397
7398   if (th && vect_print_dump_info (REPORT_COST))
7399     fprintf (vect_dump, "Vectorization may not be profitable.");
7400
7401   return th;
7402 }
7403
7404 /* Function vect_do_peeling_for_loop_bound
7405
7406    Peel the last iterations of the loop represented by LOOP_VINFO.
7407    The peeled iterations form a new epilog loop.  Given that the loop now 
7408    iterates NITERS times, the new epilog loop iterates
7409    NITERS % VECTORIZATION_FACTOR times.
7410    
7411    The original loop will later be made to iterate 
7412    NITERS / VECTORIZATION_FACTOR times (this value is placed into RATIO).  */
7413
7414 static void 
7415 vect_do_peeling_for_loop_bound (loop_vec_info loop_vinfo, tree *ratio)
7416 {
7417   tree ni_name, ratio_mult_vf_name;
7418   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7419   struct loop *new_loop;
7420   edge update_e;
7421   basic_block preheader;
7422   int loop_num;
7423   bool check_profitability = false;
7424   unsigned int th = 0;
7425   int min_profitable_iters;
7426
7427   if (vect_print_dump_info (REPORT_DETAILS))
7428     fprintf (vect_dump, "=== vect_do_peeling_for_loop_bound ===");
7429
7430   initialize_original_copy_tables ();
7431
7432   /* Generate the following variables on the preheader of original loop:
7433          
7434      ni_name = number of iteration the original loop executes
7435      ratio = ni_name / vf
7436      ratio_mult_vf_name = ratio * vf  */
7437   vect_generate_tmps_on_preheader (loop_vinfo, &ni_name,
7438                                    &ratio_mult_vf_name, ratio);
7439
7440   loop_num  = loop->num; 
7441
7442   /* If cost model check not done during versioning and 
7443      peeling for alignment.  */
7444   if (!VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
7445       && !VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo))
7446       && !LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
7447     {
7448       check_profitability = true;
7449
7450       /* Get profitability threshold for vectorized loop.  */
7451       min_profitable_iters = LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo);
7452
7453       th = conservative_cost_threshold (loop_vinfo, 
7454                                         min_profitable_iters);
7455     }
7456
7457   new_loop = slpeel_tree_peel_loop_to_edge (loop, single_exit (loop),
7458                                             ratio_mult_vf_name, ni_name, false,
7459                                             th, check_profitability);
7460   gcc_assert (new_loop);
7461   gcc_assert (loop_num == loop->num);
7462 #ifdef ENABLE_CHECKING
7463   slpeel_verify_cfg_after_peeling (loop, new_loop);
7464 #endif
7465
7466   /* A guard that controls whether the new_loop is to be executed or skipped
7467      is placed in LOOP->exit.  LOOP->exit therefore has two successors - one
7468      is the preheader of NEW_LOOP, where the IVs from LOOP are used.  The other
7469      is a bb after NEW_LOOP, where these IVs are not used.  Find the edge that
7470      is on the path where the LOOP IVs are used and need to be updated.  */
7471
7472   preheader = loop_preheader_edge (new_loop)->src;
7473   if (EDGE_PRED (preheader, 0)->src == single_exit (loop)->dest)
7474     update_e = EDGE_PRED (preheader, 0);
7475   else
7476     update_e = EDGE_PRED (preheader, 1);
7477
7478   /* Update IVs of original loop as if they were advanced 
7479      by ratio_mult_vf_name steps.  */
7480   vect_update_ivs_after_vectorizer (loop_vinfo, ratio_mult_vf_name, update_e); 
7481
7482   /* After peeling we have to reset scalar evolution analyzer.  */
7483   scev_reset ();
7484
7485   free_original_copy_tables ();
7486 }
7487
7488
7489 /* Function vect_gen_niters_for_prolog_loop
7490
7491    Set the number of iterations for the loop represented by LOOP_VINFO
7492    to the minimum between LOOP_NITERS (the original iteration count of the loop)
7493    and the misalignment of DR - the data reference recorded in
7494    LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO).  As a result, after the execution of 
7495    this loop, the data reference DR will refer to an aligned location.
7496
7497    The following computation is generated:
7498
7499    If the misalignment of DR is known at compile time:
7500      addr_mis = int mis = DR_MISALIGNMENT (dr);
7501    Else, compute address misalignment in bytes:
7502      addr_mis = addr & (vectype_size - 1)
7503
7504    prolog_niters = min (LOOP_NITERS, ((VF - addr_mis/elem_size)&(VF-1))/step)
7505
7506    (elem_size = element type size; an element is the scalar element whose type
7507    is the inner type of the vectype)
7508
7509    When the step of the data-ref in the loop is not 1 (as in interleaved data
7510    and SLP), the number of iterations of the prolog must be divided by the step
7511    (which is equal to the size of interleaved group).
7512
7513    The above formulas assume that VF == number of elements in the vector. This
7514    may not hold when there are multiple-types in the loop.
7515    In this case, for some data-references in the loop the VF does not represent
7516    the number of elements that fit in the vector.  Therefore, instead of VF we
7517    use TYPE_VECTOR_SUBPARTS.  */
7518
7519 static tree 
7520 vect_gen_niters_for_prolog_loop (loop_vec_info loop_vinfo, tree loop_niters)
7521 {
7522   struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
7523   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7524   tree var;
7525   gimple_seq stmts;
7526   tree iters, iters_name;
7527   edge pe;
7528   basic_block new_bb;
7529   gimple dr_stmt = DR_STMT (dr);
7530   stmt_vec_info stmt_info = vinfo_for_stmt (dr_stmt);
7531   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7532   int vectype_align = TYPE_ALIGN (vectype) / BITS_PER_UNIT;
7533   tree niters_type = TREE_TYPE (loop_niters);
7534   int step = 1;
7535   int element_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
7536   int nelements = TYPE_VECTOR_SUBPARTS (vectype);
7537
7538   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
7539     step = DR_GROUP_SIZE (vinfo_for_stmt (DR_GROUP_FIRST_DR (stmt_info)));
7540
7541   pe = loop_preheader_edge (loop); 
7542
7543   if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
7544     {
7545       int byte_misalign = LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo);
7546       int elem_misalign = byte_misalign / element_size;
7547
7548       if (vect_print_dump_info (REPORT_DETAILS))
7549         fprintf (vect_dump, "known alignment = %d.", byte_misalign);
7550
7551       iters = build_int_cst (niters_type,
7552                      (((nelements - elem_misalign) & (nelements - 1)) / step));
7553     }
7554   else
7555     {
7556       gimple_seq new_stmts = NULL;
7557       tree start_addr = vect_create_addr_base_for_vector_ref (dr_stmt, 
7558                                                 &new_stmts, NULL_TREE, loop);
7559       tree ptr_type = TREE_TYPE (start_addr);
7560       tree size = TYPE_SIZE (ptr_type);
7561       tree type = lang_hooks.types.type_for_size (tree_low_cst (size, 1), 1);
7562       tree vectype_size_minus_1 = build_int_cst (type, vectype_align - 1);
7563       tree elem_size_log =
7564         build_int_cst (type, exact_log2 (vectype_align/nelements));
7565       tree nelements_minus_1 = build_int_cst (type, nelements - 1);
7566       tree nelements_tree = build_int_cst (type, nelements);
7567       tree byte_misalign;
7568       tree elem_misalign;
7569
7570       new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmts);
7571       gcc_assert (!new_bb);
7572   
7573       /* Create:  byte_misalign = addr & (vectype_size - 1)  */
7574       byte_misalign = 
7575         fold_build2 (BIT_AND_EXPR, type, fold_convert (type, start_addr), vectype_size_minus_1);
7576   
7577       /* Create:  elem_misalign = byte_misalign / element_size  */
7578       elem_misalign =
7579         fold_build2 (RSHIFT_EXPR, type, byte_misalign, elem_size_log);
7580
7581       /* Create:  (niters_type) (nelements - elem_misalign)&(nelements - 1)  */
7582       iters = fold_build2 (MINUS_EXPR, type, nelements_tree, elem_misalign);
7583       iters = fold_build2 (BIT_AND_EXPR, type, iters, nelements_minus_1);
7584       iters = fold_convert (niters_type, iters);
7585     }
7586
7587   /* Create:  prolog_loop_niters = min (iters, loop_niters) */
7588   /* If the loop bound is known at compile time we already verified that it is
7589      greater than vf; since the misalignment ('iters') is at most vf, there's
7590      no need to generate the MIN_EXPR in this case.  */
7591   if (TREE_CODE (loop_niters) != INTEGER_CST)
7592     iters = fold_build2 (MIN_EXPR, niters_type, iters, loop_niters);
7593
7594   if (vect_print_dump_info (REPORT_DETAILS))
7595     {
7596       fprintf (vect_dump, "niters for prolog loop: ");
7597       print_generic_expr (vect_dump, iters, TDF_SLIM);
7598     }
7599
7600   var = create_tmp_var (niters_type, "prolog_loop_niters");
7601   add_referenced_var (var);
7602   stmts = NULL;
7603   iters_name = force_gimple_operand (iters, &stmts, false, var);
7604
7605   /* Insert stmt on loop preheader edge.  */
7606   if (stmts)
7607     {
7608       basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
7609       gcc_assert (!new_bb);
7610     }
7611
7612   return iters_name; 
7613 }
7614
7615
7616 /* Function vect_update_init_of_dr
7617
7618    NITERS iterations were peeled from LOOP.  DR represents a data reference
7619    in LOOP.  This function updates the information recorded in DR to
7620    account for the fact that the first NITERS iterations had already been 
7621    executed.  Specifically, it updates the OFFSET field of DR.  */
7622
7623 static void
7624 vect_update_init_of_dr (struct data_reference *dr, tree niters)
7625 {
7626   tree offset = DR_OFFSET (dr);
7627       
7628   niters = fold_build2 (MULT_EXPR, TREE_TYPE (niters), niters, DR_STEP (dr));
7629   offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, niters);
7630   DR_OFFSET (dr) = offset;
7631 }
7632
7633
7634 /* Function vect_update_inits_of_drs
7635
7636    NITERS iterations were peeled from the loop represented by LOOP_VINFO.  
7637    This function updates the information recorded for the data references in 
7638    the loop to account for the fact that the first NITERS iterations had 
7639    already been executed.  Specifically, it updates the initial_condition of
7640    the access_function of all the data_references in the loop.  */
7641
7642 static void
7643 vect_update_inits_of_drs (loop_vec_info loop_vinfo, tree niters)
7644 {
7645   unsigned int i;
7646   VEC (data_reference_p, heap) *datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
7647   struct data_reference *dr;
7648
7649   if (vect_print_dump_info (REPORT_DETAILS))
7650     fprintf (vect_dump, "=== vect_update_inits_of_dr ===");
7651
7652   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
7653     vect_update_init_of_dr (dr, niters);
7654 }
7655
7656
7657 /* Function vect_do_peeling_for_alignment
7658
7659    Peel the first 'niters' iterations of the loop represented by LOOP_VINFO.
7660    'niters' is set to the misalignment of one of the data references in the
7661    loop, thereby forcing it to refer to an aligned location at the beginning
7662    of the execution of this loop.  The data reference for which we are
7663    peeling is recorded in LOOP_VINFO_UNALIGNED_DR.  */
7664
7665 static void
7666 vect_do_peeling_for_alignment (loop_vec_info loop_vinfo)
7667 {
7668   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7669   tree niters_of_prolog_loop, ni_name;
7670   tree n_iters;
7671   struct loop *new_loop;
7672   bool check_profitability = false;
7673   unsigned int th = 0;
7674   int min_profitable_iters;
7675
7676   if (vect_print_dump_info (REPORT_DETAILS))
7677     fprintf (vect_dump, "=== vect_do_peeling_for_alignment ===");
7678
7679   initialize_original_copy_tables ();
7680
7681   ni_name = vect_build_loop_niters (loop_vinfo);
7682   niters_of_prolog_loop = vect_gen_niters_for_prolog_loop (loop_vinfo, ni_name);
7683   
7684
7685   /* If cost model check not done during versioning.  */
7686   if (!VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
7687       && !VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
7688     {
7689       check_profitability = true;
7690
7691       /* Get profitability threshold for vectorized loop.  */
7692       min_profitable_iters = LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo);
7693
7694       th = conservative_cost_threshold (loop_vinfo, 
7695                                         min_profitable_iters);
7696     }
7697
7698   /* Peel the prolog loop and iterate it niters_of_prolog_loop.  */
7699   new_loop =
7700     slpeel_tree_peel_loop_to_edge (loop, loop_preheader_edge (loop),
7701                                    niters_of_prolog_loop, ni_name, true,
7702                                    th, check_profitability);
7703
7704   gcc_assert (new_loop);
7705 #ifdef ENABLE_CHECKING
7706   slpeel_verify_cfg_after_peeling (new_loop, loop);
7707 #endif
7708
7709   /* Update number of times loop executes.  */
7710   n_iters = LOOP_VINFO_NITERS (loop_vinfo);
7711   LOOP_VINFO_NITERS (loop_vinfo) = fold_build2 (MINUS_EXPR,
7712                 TREE_TYPE (n_iters), n_iters, niters_of_prolog_loop);
7713
7714   /* Update the init conditions of the access functions of all data refs.  */
7715   vect_update_inits_of_drs (loop_vinfo, niters_of_prolog_loop);
7716
7717   /* After peeling we have to reset scalar evolution analyzer.  */
7718   scev_reset ();
7719
7720   free_original_copy_tables ();
7721 }
7722
7723
7724 /* Function vect_create_cond_for_align_checks.
7725
7726    Create a conditional expression that represents the alignment checks for
7727    all of data references (array element references) whose alignment must be
7728    checked at runtime.
7729
7730    Input:
7731    COND_EXPR  - input conditional expression.  New conditions will be chained
7732                 with logical AND operation.
7733    LOOP_VINFO - two fields of the loop information are used.
7734                 LOOP_VINFO_PTR_MASK is the mask used to check the alignment.
7735                 LOOP_VINFO_MAY_MISALIGN_STMTS contains the refs to be checked.
7736
7737    Output:
7738    COND_EXPR_STMT_LIST - statements needed to construct the conditional
7739                          expression.
7740    The returned value is the conditional expression to be used in the if
7741    statement that controls which version of the loop gets executed at runtime.
7742
7743    The algorithm makes two assumptions:
7744      1) The number of bytes "n" in a vector is a power of 2.
7745      2) An address "a" is aligned if a%n is zero and that this
7746         test can be done as a&(n-1) == 0.  For example, for 16
7747         byte vectors the test is a&0xf == 0.  */
7748
7749 static void
7750 vect_create_cond_for_align_checks (loop_vec_info loop_vinfo,
7751                                    tree *cond_expr,
7752                                    gimple_seq *cond_expr_stmt_list)
7753 {
7754   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7755   VEC(gimple,heap) *may_misalign_stmts
7756     = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
7757   gimple ref_stmt;
7758   int mask = LOOP_VINFO_PTR_MASK (loop_vinfo);
7759   tree mask_cst;
7760   unsigned int i;
7761   tree psize;
7762   tree int_ptrsize_type;
7763   char tmp_name[20];
7764   tree or_tmp_name = NULL_TREE;
7765   tree and_tmp, and_tmp_name;
7766   gimple and_stmt;
7767   tree ptrsize_zero;
7768   tree part_cond_expr;
7769
7770   /* Check that mask is one less than a power of 2, i.e., mask is
7771      all zeros followed by all ones.  */
7772   gcc_assert ((mask != 0) && ((mask & (mask+1)) == 0));
7773
7774   /* CHECKME: what is the best integer or unsigned type to use to hold a
7775      cast from a pointer value?  */
7776   psize = TYPE_SIZE (ptr_type_node);
7777   int_ptrsize_type
7778     = lang_hooks.types.type_for_size (tree_low_cst (psize, 1), 0);
7779
7780   /* Create expression (mask & (dr_1 || ... || dr_n)) where dr_i is the address
7781      of the first vector of the i'th data reference. */
7782
7783   for (i = 0; VEC_iterate (gimple, may_misalign_stmts, i, ref_stmt); i++)
7784     {
7785       gimple_seq new_stmt_list = NULL;
7786       tree addr_base;
7787       tree addr_tmp, addr_tmp_name;
7788       tree or_tmp, new_or_tmp_name;
7789       gimple addr_stmt, or_stmt;
7790
7791       /* create: addr_tmp = (int)(address_of_first_vector) */
7792       addr_base =
7793         vect_create_addr_base_for_vector_ref (ref_stmt, &new_stmt_list,
7794                                               NULL_TREE, loop);
7795       if (new_stmt_list != NULL)
7796         gimple_seq_add_seq (cond_expr_stmt_list, new_stmt_list);
7797
7798       sprintf (tmp_name, "%s%d", "addr2int", i);
7799       addr_tmp = create_tmp_var (int_ptrsize_type, tmp_name);
7800       add_referenced_var (addr_tmp);
7801       addr_tmp_name = make_ssa_name (addr_tmp, NULL);
7802       addr_stmt = gimple_build_assign_with_ops (NOP_EXPR, addr_tmp_name,
7803                                                 addr_base, NULL_TREE);
7804       SSA_NAME_DEF_STMT (addr_tmp_name) = addr_stmt;
7805       gimple_seq_add_stmt (cond_expr_stmt_list, addr_stmt);
7806
7807       /* The addresses are OR together.  */
7808
7809       if (or_tmp_name != NULL_TREE)
7810         {
7811           /* create: or_tmp = or_tmp | addr_tmp */
7812           sprintf (tmp_name, "%s%d", "orptrs", i);
7813           or_tmp = create_tmp_var (int_ptrsize_type, tmp_name);
7814           add_referenced_var (or_tmp);
7815           new_or_tmp_name = make_ssa_name (or_tmp, NULL);
7816           or_stmt = gimple_build_assign_with_ops (BIT_IOR_EXPR,
7817                                                   new_or_tmp_name,
7818                                                   or_tmp_name, addr_tmp_name);
7819           SSA_NAME_DEF_STMT (new_or_tmp_name) = or_stmt;
7820           gimple_seq_add_stmt (cond_expr_stmt_list, or_stmt);
7821           or_tmp_name = new_or_tmp_name;
7822         }
7823       else
7824         or_tmp_name = addr_tmp_name;
7825
7826     } /* end for i */
7827
7828   mask_cst = build_int_cst (int_ptrsize_type, mask);
7829
7830   /* create: and_tmp = or_tmp & mask  */
7831   and_tmp = create_tmp_var (int_ptrsize_type, "andmask" );
7832   add_referenced_var (and_tmp);
7833   and_tmp_name = make_ssa_name (and_tmp, NULL);
7834
7835   and_stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, and_tmp_name,
7836                                            or_tmp_name, mask_cst);
7837   SSA_NAME_DEF_STMT (and_tmp_name) = and_stmt;
7838   gimple_seq_add_stmt (cond_expr_stmt_list, and_stmt);
7839
7840   /* Make and_tmp the left operand of the conditional test against zero.
7841      if and_tmp has a nonzero bit then some address is unaligned.  */
7842   ptrsize_zero = build_int_cst (int_ptrsize_type, 0);
7843   part_cond_expr = fold_build2 (EQ_EXPR, boolean_type_node,
7844                                 and_tmp_name, ptrsize_zero);
7845   if (*cond_expr)
7846     *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
7847                               *cond_expr, part_cond_expr);
7848   else
7849     *cond_expr = part_cond_expr;
7850 }
7851
7852 /* Function vect_vfa_segment_size.
7853
7854    Create an expression that computes the size of segment
7855    that will be accessed for a data reference.  The functions takes into
7856    account that realignment loads may access one more vector.
7857
7858    Input:
7859      DR: The data reference.
7860      VECT_FACTOR: vectorization factor.
7861
7862    Return an expression whose value is the size of segment which will be
7863    accessed by DR.  */
7864
7865 static tree
7866 vect_vfa_segment_size (struct data_reference *dr, tree vect_factor)
7867 {
7868   tree segment_length = fold_build2 (MULT_EXPR, integer_type_node,
7869                                      DR_STEP (dr), vect_factor);
7870
7871   if (vect_supportable_dr_alignment (dr) == dr_explicit_realign_optimized)
7872     {
7873       tree vector_size = TYPE_SIZE_UNIT
7874                           (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
7875
7876       segment_length = fold_build2 (PLUS_EXPR, integer_type_node,
7877                                     segment_length, vector_size);
7878     }
7879   return fold_convert (sizetype, segment_length);
7880 }
7881
7882 /* Function vect_create_cond_for_alias_checks.
7883
7884    Create a conditional expression that represents the run-time checks for
7885    overlapping of address ranges represented by a list of data references
7886    relations passed as input.
7887
7888    Input:
7889    COND_EXPR  - input conditional expression.  New conditions will be chained
7890                 with logical AND operation.
7891    LOOP_VINFO - field LOOP_VINFO_MAY_ALIAS_STMTS contains the list of ddrs
7892                 to be checked.
7893
7894    Output:
7895    COND_EXPR - conditional expression.
7896    COND_EXPR_STMT_LIST - statements needed to construct the conditional
7897                          expression.
7898
7899
7900    The returned value is the conditional expression to be used in the if
7901    statement that controls which version of the loop gets executed at runtime.
7902 */
7903
7904 static void
7905 vect_create_cond_for_alias_checks (loop_vec_info loop_vinfo,
7906                                    tree * cond_expr,
7907                                    gimple_seq * cond_expr_stmt_list)
7908 {
7909   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7910   VEC (ddr_p, heap) * may_alias_ddrs =
7911     LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
7912   tree vect_factor =
7913     build_int_cst (integer_type_node, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
7914
7915   ddr_p ddr;
7916   unsigned int i;
7917   tree part_cond_expr;
7918
7919   /* Create expression
7920      ((store_ptr_0 + store_segment_length_0) < load_ptr_0)
7921      || (load_ptr_0 + load_segment_length_0) < store_ptr_0))
7922      &&         
7923      ...
7924      &&
7925      ((store_ptr_n + store_segment_length_n) < load_ptr_n)
7926      || (load_ptr_n + load_segment_length_n) < store_ptr_n))  */
7927
7928   if (VEC_empty (ddr_p, may_alias_ddrs))
7929     return;
7930
7931   for (i = 0; VEC_iterate (ddr_p, may_alias_ddrs, i, ddr); i++)
7932     {
7933       struct data_reference *dr_a, *dr_b;
7934       gimple dr_group_first_a, dr_group_first_b;
7935       tree addr_base_a, addr_base_b;
7936       tree segment_length_a, segment_length_b;
7937       gimple stmt_a, stmt_b;
7938
7939       dr_a = DDR_A (ddr);
7940       stmt_a = DR_STMT (DDR_A (ddr));
7941       dr_group_first_a = DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_a));
7942       if (dr_group_first_a)
7943         {
7944           stmt_a = dr_group_first_a;
7945           dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
7946         }
7947
7948       dr_b = DDR_B (ddr);
7949       stmt_b = DR_STMT (DDR_B (ddr));
7950       dr_group_first_b = DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_b));
7951       if (dr_group_first_b)
7952         {
7953           stmt_b = dr_group_first_b;
7954           dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
7955         }
7956
7957       addr_base_a =
7958         vect_create_addr_base_for_vector_ref (stmt_a, cond_expr_stmt_list,
7959                                               NULL_TREE, loop);
7960       addr_base_b =
7961         vect_create_addr_base_for_vector_ref (stmt_b, cond_expr_stmt_list,
7962                                               NULL_TREE, loop);
7963
7964       segment_length_a = vect_vfa_segment_size (dr_a, vect_factor);
7965       segment_length_b = vect_vfa_segment_size (dr_b, vect_factor);
7966
7967       if (vect_print_dump_info (REPORT_DR_DETAILS))
7968         {
7969           fprintf (vect_dump,
7970                    "create runtime check for data references ");
7971           print_generic_expr (vect_dump, DR_REF (dr_a), TDF_SLIM);
7972           fprintf (vect_dump, " and ");
7973           print_generic_expr (vect_dump, DR_REF (dr_b), TDF_SLIM);
7974         }
7975
7976
7977       part_cond_expr = 
7978         fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
7979           fold_build2 (LT_EXPR, boolean_type_node,
7980             fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr_base_a),
7981               addr_base_a,
7982               segment_length_a),
7983             addr_base_b),
7984           fold_build2 (LT_EXPR, boolean_type_node,
7985             fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr_base_b),
7986               addr_base_b,
7987               segment_length_b),
7988             addr_base_a));
7989       
7990       if (*cond_expr)
7991         *cond_expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
7992                                   *cond_expr, part_cond_expr);
7993       else
7994         *cond_expr = part_cond_expr;
7995     }
7996     if (vect_print_dump_info (REPORT_VECTORIZED_LOOPS))
7997       fprintf (vect_dump, "created %u versioning for alias checks.\n",
7998                VEC_length (ddr_p, may_alias_ddrs));
7999
8000 }
8001
8002 /* Function vect_loop_versioning.
8003  
8004    If the loop has data references that may or may not be aligned or/and
8005    has data reference relations whose independence was not proven then
8006    two versions of the loop need to be generated, one which is vectorized
8007    and one which isn't.  A test is then generated to control which of the
8008    loops is executed.  The test checks for the alignment of all of the
8009    data references that may or may not be aligned.  An additional
8010    sequence of runtime tests is generated for each pairs of DDRs whose
8011    independence was not proven.  The vectorized version of loop is 
8012    executed only if both alias and alignment tests are passed.  
8013   
8014    The test generated to check which version of loop is executed
8015    is modified to also check for profitability as indicated by the 
8016    cost model initially.  */
8017
8018 static void
8019 vect_loop_versioning (loop_vec_info loop_vinfo)
8020 {
8021   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
8022   struct loop *nloop;
8023   tree cond_expr = NULL_TREE;
8024   gimple_seq cond_expr_stmt_list = NULL;
8025   basic_block condition_bb;
8026   gimple_stmt_iterator gsi, cond_exp_gsi;
8027   basic_block merge_bb;
8028   basic_block new_exit_bb;
8029   edge new_exit_e, e;
8030   gimple orig_phi, new_phi;
8031   tree arg;
8032   unsigned prob = 4 * REG_BR_PROB_BASE / 5;
8033   gimple_seq gimplify_stmt_list = NULL;
8034   tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
8035   int min_profitable_iters = 0;
8036   unsigned int th;
8037
8038   /* Get profitability threshold for vectorized loop.  */
8039   min_profitable_iters = LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo);
8040
8041   th = conservative_cost_threshold (loop_vinfo,
8042                                     min_profitable_iters);
8043
8044   cond_expr =
8045     build2 (GT_EXPR, boolean_type_node, scalar_loop_iters, 
8046             build_int_cst (TREE_TYPE (scalar_loop_iters), th));
8047
8048   cond_expr = force_gimple_operand (cond_expr, &cond_expr_stmt_list,
8049                                     false, NULL_TREE);
8050
8051   if (VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo)))
8052       vect_create_cond_for_align_checks (loop_vinfo, &cond_expr,
8053                                          &cond_expr_stmt_list);
8054
8055   if (VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
8056     vect_create_cond_for_alias_checks (loop_vinfo, &cond_expr, 
8057                                        &cond_expr_stmt_list);
8058
8059   cond_expr =
8060     fold_build2 (NE_EXPR, boolean_type_node, cond_expr, integer_zero_node);
8061   cond_expr =
8062     force_gimple_operand (cond_expr, &gimplify_stmt_list, true, NULL_TREE);
8063   gimple_seq_add_seq (&cond_expr_stmt_list, gimplify_stmt_list);
8064
8065   initialize_original_copy_tables ();
8066   nloop = loop_version (loop, cond_expr, &condition_bb,
8067                         prob, prob, REG_BR_PROB_BASE - prob, true);
8068   free_original_copy_tables();
8069
8070   /* Loop versioning violates an assumption we try to maintain during 
8071      vectorization - that the loop exit block has a single predecessor.
8072      After versioning, the exit block of both loop versions is the same
8073      basic block (i.e. it has two predecessors). Just in order to simplify
8074      following transformations in the vectorizer, we fix this situation
8075      here by adding a new (empty) block on the exit-edge of the loop,
8076      with the proper loop-exit phis to maintain loop-closed-form.  */
8077   
8078   merge_bb = single_exit (loop)->dest;
8079   gcc_assert (EDGE_COUNT (merge_bb->preds) == 2);
8080   new_exit_bb = split_edge (single_exit (loop));
8081   new_exit_e = single_exit (loop);
8082   e = EDGE_SUCC (new_exit_bb, 0);
8083
8084   for (gsi = gsi_start_phis (merge_bb); !gsi_end_p (gsi); gsi_next (&gsi))
8085     {
8086       orig_phi = gsi_stmt (gsi);
8087       new_phi = create_phi_node (SSA_NAME_VAR (PHI_RESULT (orig_phi)),
8088                                   new_exit_bb);
8089       arg = PHI_ARG_DEF_FROM_EDGE (orig_phi, e);
8090       add_phi_arg (new_phi, arg, new_exit_e);
8091       SET_PHI_ARG_DEF (orig_phi, e->dest_idx, PHI_RESULT (new_phi));
8092     } 
8093
8094   /* End loop-exit-fixes after versioning.  */
8095
8096   update_ssa (TODO_update_ssa);
8097   if (cond_expr_stmt_list)
8098     {
8099       cond_exp_gsi = gsi_last_bb (condition_bb);
8100       gsi_insert_seq_before (&cond_exp_gsi, cond_expr_stmt_list, GSI_SAME_STMT);
8101     }
8102 }
8103
8104 /* Remove a group of stores (for SLP or interleaving), free their 
8105    stmt_vec_info.  */
8106
8107 static void
8108 vect_remove_stores (gimple first_stmt)
8109 {
8110   gimple next = first_stmt;
8111   gimple tmp;
8112   gimple_stmt_iterator next_si;
8113
8114   while (next)
8115     {
8116       /* Free the attached stmt_vec_info and remove the stmt.  */
8117       next_si = gsi_for_stmt (next);
8118       gsi_remove (&next_si, true);
8119       tmp = DR_GROUP_NEXT_DR (vinfo_for_stmt (next));
8120       free_stmt_vec_info (next);
8121       next = tmp;
8122     }
8123 }
8124
8125
8126 /* Vectorize SLP instance tree in postorder.  */
8127
8128 static bool
8129 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
8130                             unsigned int vectorization_factor)
8131 {
8132   gimple stmt;
8133   bool strided_store, is_store;
8134   gimple_stmt_iterator si;
8135   stmt_vec_info stmt_info;
8136   unsigned int vec_stmts_size, nunits, group_size;
8137   tree vectype;
8138   int i;
8139   slp_tree loads_node;
8140
8141   if (!node)
8142     return false;
8143
8144   vect_schedule_slp_instance (SLP_TREE_LEFT (node), instance,
8145                               vectorization_factor);
8146   vect_schedule_slp_instance (SLP_TREE_RIGHT (node), instance,
8147                               vectorization_factor);
8148   
8149   stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
8150   stmt_info = vinfo_for_stmt (stmt);
8151   /* VECTYPE is the type of the destination.  */
8152   vectype = get_vectype_for_scalar_type (TREE_TYPE (gimple_assign_lhs (stmt)));
8153   nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
8154   group_size = SLP_INSTANCE_GROUP_SIZE (instance);
8155
8156   /* For each SLP instance calculate number of vector stmts to be created
8157      for the scalar stmts in each node of the SLP tree. Number of vector
8158      elements in one vector iteration is the number of scalar elements in
8159      one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
8160      size.  */
8161   vec_stmts_size = (vectorization_factor * group_size) / nunits;
8162
8163   /* In case of load permutation we have to allocate vectorized statements for
8164      all the nodes that participate in that permutation.  */
8165   if (SLP_INSTANCE_LOAD_PERMUTATION (instance))
8166     {
8167       for (i = 0;
8168            VEC_iterate (slp_tree, SLP_INSTANCE_LOADS (instance), i, loads_node);
8169            i++)
8170         {
8171           if (!SLP_TREE_VEC_STMTS (loads_node))
8172             {
8173               SLP_TREE_VEC_STMTS (loads_node) = VEC_alloc (gimple, heap,
8174                                                            vec_stmts_size);
8175               SLP_TREE_NUMBER_OF_VEC_STMTS (loads_node) = vec_stmts_size;
8176             }
8177         }
8178     }
8179
8180   if (!SLP_TREE_VEC_STMTS (node))
8181     {
8182       SLP_TREE_VEC_STMTS (node) = VEC_alloc (gimple, heap, vec_stmts_size);
8183       SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
8184     }
8185
8186   if (vect_print_dump_info (REPORT_DETAILS))
8187     {
8188       fprintf (vect_dump, "------>vectorizing SLP node starting from: ");
8189       print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
8190     }   
8191
8192   si = gsi_for_stmt (stmt);
8193   is_store = vect_transform_stmt (stmt, &si, &strided_store, node, instance);
8194   if (is_store)
8195     {
8196       if (DR_GROUP_FIRST_DR (stmt_info))
8197         /* If IS_STORE is TRUE, the vectorization of the
8198            interleaving chain was completed - free all the stores in
8199            the chain.  */
8200         vect_remove_stores (DR_GROUP_FIRST_DR (stmt_info));
8201       else
8202         /* FORNOW: SLP originates only from strided stores.  */
8203         gcc_unreachable ();
8204
8205       return true;
8206     }
8207
8208   /* FORNOW: SLP originates only from strided stores.  */
8209   return false;
8210 }
8211
8212
8213 static bool
8214 vect_schedule_slp (loop_vec_info loop_vinfo)
8215 {
8216   VEC (slp_instance, heap) *slp_instances = 
8217     LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
8218   slp_instance instance;
8219   unsigned int i;
8220   bool is_store = false;
8221
8222   for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); i++)
8223     {
8224       /* Schedule the tree of INSTANCE.  */
8225       is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
8226                                           instance,
8227                                           LOOP_VINFO_VECT_FACTOR (loop_vinfo));
8228                           
8229       if (vect_print_dump_info (REPORT_VECTORIZED_LOOPS)
8230           || vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
8231         fprintf (vect_dump, "vectorizing stmts using SLP.");
8232     }
8233
8234   return is_store;
8235 }
8236
8237 /* Function vect_transform_loop.
8238
8239    The analysis phase has determined that the loop is vectorizable.
8240    Vectorize the loop - created vectorized stmts to replace the scalar
8241    stmts in the loop, and update the loop exit condition.  */
8242
8243 void
8244 vect_transform_loop (loop_vec_info loop_vinfo)
8245 {
8246   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
8247   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
8248   int nbbs = loop->num_nodes;
8249   gimple_stmt_iterator si;
8250   int i;
8251   tree ratio = NULL;
8252   int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8253   bool strided_store;
8254   bool slp_scheduled = false;
8255   unsigned int nunits;
8256
8257   if (vect_print_dump_info (REPORT_DETAILS))
8258     fprintf (vect_dump, "=== vec_transform_loop ===");
8259
8260   if (VEC_length (gimple, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
8261       || VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)))
8262     vect_loop_versioning (loop_vinfo);
8263
8264   /* CHECKME: we wouldn't need this if we called update_ssa once
8265      for all loops.  */
8266   bitmap_zero (vect_memsyms_to_rename);
8267
8268   /* Peel the loop if there are data refs with unknown alignment.
8269      Only one data ref with unknown store is allowed.  */
8270
8271   if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
8272     vect_do_peeling_for_alignment (loop_vinfo);
8273   
8274   /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
8275      compile time constant), or it is a constant that doesn't divide by the
8276      vectorization factor, then an epilog loop needs to be created.
8277      We therefore duplicate the loop: the original loop will be vectorized,
8278      and will compute the first (n/VF) iterations. The second copy of the loop
8279      will remain scalar and will compute the remaining (n%VF) iterations.
8280      (VF is the vectorization factor).  */
8281
8282   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
8283       || (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
8284           && LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0))
8285     vect_do_peeling_for_loop_bound (loop_vinfo, &ratio);
8286   else
8287     ratio = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo)),
8288                 LOOP_VINFO_INT_NITERS (loop_vinfo) / vectorization_factor);
8289
8290   /* 1) Make sure the loop header has exactly two entries
8291      2) Make sure we have a preheader basic block.  */
8292
8293   gcc_assert (EDGE_COUNT (loop->header->preds) == 2);
8294
8295   split_edge (loop_preheader_edge (loop));
8296
8297   /* FORNOW: the vectorizer supports only loops which body consist
8298      of one basic block (header + empty latch). When the vectorizer will 
8299      support more involved loop forms, the order by which the BBs are 
8300      traversed need to be reconsidered.  */
8301
8302   for (i = 0; i < nbbs; i++)
8303     {
8304       basic_block bb = bbs[i];
8305       stmt_vec_info stmt_info;
8306       gimple phi;
8307
8308       for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
8309         {
8310           phi = gsi_stmt (si);
8311           if (vect_print_dump_info (REPORT_DETAILS))
8312             {
8313               fprintf (vect_dump, "------>vectorizing phi: ");
8314               print_gimple_stmt (vect_dump, phi, 0, TDF_SLIM);
8315             }
8316           stmt_info = vinfo_for_stmt (phi);
8317           if (!stmt_info)
8318             continue;
8319
8320           if (!STMT_VINFO_RELEVANT_P (stmt_info)
8321               && !STMT_VINFO_LIVE_P (stmt_info))
8322             continue;
8323
8324           if ((TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info))
8325                 != (unsigned HOST_WIDE_INT) vectorization_factor)
8326               && vect_print_dump_info (REPORT_DETAILS))
8327             fprintf (vect_dump, "multiple-types.");
8328
8329           if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
8330             {
8331               if (vect_print_dump_info (REPORT_DETAILS))
8332                 fprintf (vect_dump, "transform phi.");
8333               vect_transform_stmt (phi, NULL, NULL, NULL, NULL);
8334             }
8335         }
8336
8337       for (si = gsi_start_bb (bb); !gsi_end_p (si);)
8338         {
8339           gimple stmt = gsi_stmt (si);
8340           bool is_store;
8341
8342           if (vect_print_dump_info (REPORT_DETAILS))
8343             {
8344               fprintf (vect_dump, "------>vectorizing statement: ");
8345               print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
8346             }   
8347
8348           stmt_info = vinfo_for_stmt (stmt);
8349
8350           /* vector stmts created in the outer-loop during vectorization of
8351              stmts in an inner-loop may not have a stmt_info, and do not
8352              need to be vectorized.  */
8353           if (!stmt_info)
8354             {
8355               gsi_next (&si);
8356               continue;
8357             }
8358
8359           if (!STMT_VINFO_RELEVANT_P (stmt_info)
8360               && !STMT_VINFO_LIVE_P (stmt_info))
8361             {
8362               gsi_next (&si);
8363               continue;
8364             }
8365
8366           gcc_assert (STMT_VINFO_VECTYPE (stmt_info));
8367           nunits =
8368             (unsigned int) TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
8369           if (!STMT_SLP_TYPE (stmt_info)
8370               && nunits != (unsigned int) vectorization_factor
8371               && vect_print_dump_info (REPORT_DETAILS))
8372             /* For SLP VF is set according to unrolling factor, and not to
8373                vector size, hence for SLP this print is not valid.  */
8374             fprintf (vect_dump, "multiple-types.");
8375
8376           /* SLP. Schedule all the SLP instances when the first SLP stmt is
8377              reached.  */
8378           if (STMT_SLP_TYPE (stmt_info))
8379             {
8380               if (!slp_scheduled)
8381                 {
8382                   slp_scheduled = true;
8383
8384                   if (vect_print_dump_info (REPORT_DETAILS))
8385                     fprintf (vect_dump, "=== scheduling SLP instances ===");
8386
8387                   is_store = vect_schedule_slp (loop_vinfo);
8388
8389                   /* IS_STORE is true if STMT is a store. Stores cannot be of
8390                      hybrid SLP type. They are removed in
8391                      vect_schedule_slp_instance and their vinfo is destroyed. */
8392                   if (is_store)
8393                     {
8394                       gsi_next (&si);
8395                       continue;
8396                     }
8397                 }
8398
8399               /* Hybrid SLP stmts must be vectorized in addition to SLP.  */
8400               if (PURE_SLP_STMT (stmt_info))
8401                 {
8402                   gsi_next (&si);
8403                   continue;
8404                 }
8405             }
8406           
8407           /* -------- vectorize statement ------------ */
8408           if (vect_print_dump_info (REPORT_DETAILS))
8409             fprintf (vect_dump, "transform statement.");
8410
8411           strided_store = false;
8412           is_store = vect_transform_stmt (stmt, &si, &strided_store, NULL, NULL);
8413           if (is_store)
8414             {
8415               if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
8416                 {
8417                   /* Interleaving. If IS_STORE is TRUE, the vectorization of the
8418                      interleaving chain was completed - free all the stores in
8419                      the chain.  */
8420                   vect_remove_stores (DR_GROUP_FIRST_DR (stmt_info));
8421                   gsi_remove (&si, true);
8422                   continue;
8423                 }
8424               else
8425                 {
8426                   /* Free the attached stmt_vec_info and remove the stmt.  */
8427                   free_stmt_vec_info (stmt);
8428                   gsi_remove (&si, true);
8429                   continue;
8430                 }
8431             }
8432           gsi_next (&si);
8433         }                       /* stmts in BB */
8434     }                           /* BBs in loop */
8435
8436   slpeel_make_loop_iterate_ntimes (loop, ratio);
8437
8438   mark_set_for_renaming (vect_memsyms_to_rename);
8439
8440   /* The memory tags and pointers in vectorized statements need to
8441      have their SSA forms updated.  FIXME, why can't this be delayed
8442      until all the loops have been transformed?  */
8443   update_ssa (TODO_update_ssa);
8444
8445   if (vect_print_dump_info (REPORT_VECTORIZED_LOOPS))
8446     fprintf (vect_dump, "LOOP VECTORIZED.");
8447   if (loop->inner && vect_print_dump_info (REPORT_VECTORIZED_LOOPS))
8448     fprintf (vect_dump, "OUTER LOOP VECTORIZED.");
8449 }