OSDN Git Service

PR tree-optimization/33860
[pf3gnuchains/gcc-fork.git] / gcc / tree-vect-analyze.c
1 /* Analysis Utilities for Loop Vectorization.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 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 "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "tree-dump.h"
32 #include "timevar.h"
33 #include "cfgloop.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "params.h"
37 #include "tree-chrec.h"
38 #include "tree-data-ref.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-vectorizer.h"
41 #include "toplev.h"
42 #include "recog.h"
43
44 /* Main analysis functions.  */
45 static loop_vec_info vect_analyze_loop_form (struct loop *);
46 static bool vect_analyze_data_refs (loop_vec_info);
47 static bool vect_mark_stmts_to_be_vectorized (loop_vec_info);
48 static void vect_analyze_scalar_cycles (loop_vec_info);
49 static bool vect_analyze_data_ref_accesses (loop_vec_info);
50 static bool vect_analyze_data_ref_dependences (loop_vec_info);
51 static bool vect_analyze_data_refs_alignment (loop_vec_info);
52 static bool vect_compute_data_refs_alignment (loop_vec_info);
53 static bool vect_enhance_data_refs_alignment (loop_vec_info);
54 static bool vect_analyze_operations (loop_vec_info);
55 static bool vect_determine_vectorization_factor (loop_vec_info);
56
57 /* Utility functions for the analyses.  */
58 static bool exist_non_indexing_operands_for_use_p (tree, tree);
59 static tree vect_get_loop_niters (struct loop *, tree *);
60 static bool vect_analyze_data_ref_dependence
61   (struct data_dependence_relation *, loop_vec_info);
62 static bool vect_compute_data_ref_alignment (struct data_reference *); 
63 static bool vect_analyze_data_ref_access (struct data_reference *);
64 static bool vect_can_advance_ivs_p (loop_vec_info);
65 static void vect_update_misalignment_for_peel
66   (struct data_reference *, struct data_reference *, int npeel);
67
68 /* Function vect_determine_vectorization_factor
69
70    Determine the vectorization factor (VF). VF is the number of data elements
71    that are operated upon in parallel in a single iteration of the vectorized
72    loop. For example, when vectorizing a loop that operates on 4byte elements,
73    on a target with vector size (VS) 16byte, the VF is set to 4, since 4
74    elements can fit in a single vector register.
75
76    We currently support vectorization of loops in which all types operated upon
77    are of the same size. Therefore this function currently sets VF according to
78    the size of the types operated upon, and fails if there are multiple sizes
79    in the loop.
80
81    VF is also the factor by which the loop iterations are strip-mined, e.g.:
82    original loop:
83         for (i=0; i<N; i++){
84           a[i] = b[i] + c[i];
85         }
86
87    vectorized loop:
88         for (i=0; i<N; i+=VF){
89           a[i:VF] = b[i:VF] + c[i:VF];
90         }
91 */
92
93 static bool
94 vect_determine_vectorization_factor (loop_vec_info loop_vinfo)
95 {
96   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
97   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
98   int nbbs = loop->num_nodes;
99   block_stmt_iterator si;
100   unsigned int vectorization_factor = 0;
101   tree scalar_type;
102   tree phi;
103   tree vectype;
104   unsigned int nunits;
105   stmt_vec_info stmt_info;
106   int i;
107
108   if (vect_print_dump_info (REPORT_DETAILS))
109     fprintf (vect_dump, "=== vect_determine_vectorization_factor ===");
110
111   for (i = 0; i < nbbs; i++)
112     {
113       basic_block bb = bbs[i];
114
115       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
116         {
117           stmt_info = vinfo_for_stmt (phi);
118           if (vect_print_dump_info (REPORT_DETAILS))
119             {
120               fprintf (vect_dump, "==> examining phi: ");
121               print_generic_expr (vect_dump, phi, TDF_SLIM);
122             }
123
124           gcc_assert (stmt_info);
125
126           if (STMT_VINFO_RELEVANT_P (stmt_info))
127             {
128               gcc_assert (!STMT_VINFO_VECTYPE (stmt_info));
129               scalar_type = TREE_TYPE (PHI_RESULT (phi));
130
131               if (vect_print_dump_info (REPORT_DETAILS))
132                 {
133                   fprintf (vect_dump, "get vectype for scalar type:  ");
134                   print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
135                 }
136
137               vectype = get_vectype_for_scalar_type (scalar_type);
138               if (!vectype)
139                 {
140                   if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
141                     {
142                       fprintf (vect_dump,
143                                "not vectorized: unsupported data-type ");
144                       print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
145                     }
146                   return false;
147                 }
148               STMT_VINFO_VECTYPE (stmt_info) = vectype;
149
150               if (vect_print_dump_info (REPORT_DETAILS))
151                 {
152                   fprintf (vect_dump, "vectype: ");
153                   print_generic_expr (vect_dump, vectype, TDF_SLIM);
154                 }
155
156               nunits = TYPE_VECTOR_SUBPARTS (vectype);
157               if (vect_print_dump_info (REPORT_DETAILS))
158                 fprintf (vect_dump, "nunits = %d", nunits);
159
160               if (!vectorization_factor
161                   || (nunits > vectorization_factor))
162                 vectorization_factor = nunits;
163             }
164         }
165
166       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
167         {
168           tree stmt = bsi_stmt (si);
169           stmt_info = vinfo_for_stmt (stmt);
170
171           if (vect_print_dump_info (REPORT_DETAILS))
172             {
173               fprintf (vect_dump, "==> examining statement: ");
174               print_generic_expr (vect_dump, stmt, TDF_SLIM);
175             }
176
177           gcc_assert (stmt_info);
178
179           /* skip stmts which do not need to be vectorized.  */
180           if (!STMT_VINFO_RELEVANT_P (stmt_info)
181               && !STMT_VINFO_LIVE_P (stmt_info))
182             {
183               if (vect_print_dump_info (REPORT_DETAILS))
184                 fprintf (vect_dump, "skip.");
185               continue;
186             }
187
188           if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
189             {
190               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
191                 {
192                   fprintf (vect_dump, "not vectorized: irregular stmt.");
193                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
194                 }
195               return false;
196             }
197
198           if (!GIMPLE_STMT_P (stmt)
199               && VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (stmt))))
200             {
201               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
202                 {
203                   fprintf (vect_dump, "not vectorized: vector stmt in loop:");
204                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
205                 }
206               return false;
207             }
208
209           if (STMT_VINFO_VECTYPE (stmt_info))
210             {
211               /* The only case when a vectype had been already set is for stmts 
212                  that contain a dataref, or for "pattern-stmts" (stmts generated
213                  by the vectorizer to represent/replace a certain idiom).  */
214               gcc_assert (STMT_VINFO_DATA_REF (stmt_info) 
215                           || is_pattern_stmt_p (stmt_info));
216               vectype = STMT_VINFO_VECTYPE (stmt_info);
217             }
218           else
219             {
220               tree operation;
221
222               gcc_assert (! STMT_VINFO_DATA_REF (stmt_info)
223                           && !is_pattern_stmt_p (stmt_info));
224
225               /* We generally set the vectype according to the type of the 
226                  result (lhs).
227                  For stmts whose result-type is different than the type of the
228                  arguments (e.g. demotion, promotion), vectype will be reset 
229                  appropriately (later).  Note that we have to visit the smallest 
230                  datatype in this function, because that determines the VF.  
231                  If the smallest datatype in the loop is present only as the 
232                  rhs of a promotion operation - we'd miss it here.
233                  Such a case, where a variable of this datatype does not appear 
234                  in the lhs anywhere in the loop, can only occur if it's an
235                  invariant: e.g.: 'int_x = (int) short_inv', which we'd expect
236                  to have been optimized away by invariant motion. However, we 
237                  cannot rely on invariant motion to always take invariants out
238                  of the loop, and so in the case of promotion we also have to 
239                  check the rhs.  */
240               scalar_type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0));
241
242               operation = GIMPLE_STMT_OPERAND (stmt, 1);
243               if (TREE_CODE (operation) == NOP_EXPR
244                   || TREE_CODE (operation) == CONVERT_EXPR
245                   || TREE_CODE (operation) == WIDEN_MULT_EXPR
246                   || TREE_CODE (operation) == FLOAT_EXPR)
247                 {
248                   tree rhs_type = TREE_TYPE (TREE_OPERAND (operation, 0));
249                   if (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type)) < 
250                       TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type)))
251                     scalar_type = rhs_type;
252                 }
253
254               if (vect_print_dump_info (REPORT_DETAILS))
255                 {
256                   fprintf (vect_dump, "get vectype for scalar type:  ");
257                   print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
258                 }
259
260               vectype = get_vectype_for_scalar_type (scalar_type);
261               if (!vectype)
262                 {
263                   if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
264                     {
265                       fprintf (vect_dump, 
266                                "not vectorized: unsupported data-type ");
267                       print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
268                     }
269                   return false;
270                 }
271               STMT_VINFO_VECTYPE (stmt_info) = vectype;
272             }
273
274           if (vect_print_dump_info (REPORT_DETAILS))
275             {
276               fprintf (vect_dump, "vectype: ");
277               print_generic_expr (vect_dump, vectype, TDF_SLIM);
278             }
279
280           nunits = TYPE_VECTOR_SUBPARTS (vectype);
281           if (vect_print_dump_info (REPORT_DETAILS))
282             fprintf (vect_dump, "nunits = %d", nunits);
283
284           if (!vectorization_factor
285               || (nunits > vectorization_factor))
286             vectorization_factor = nunits;
287
288         }
289     }
290
291   /* TODO: Analyze cost. Decide if worth while to vectorize.  */
292   if (vect_print_dump_info (REPORT_DETAILS))
293     fprintf (vect_dump, "vectorization factor = %d", vectorization_factor);
294   if (vectorization_factor <= 1)
295     {
296       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
297         fprintf (vect_dump, "not vectorized: unsupported data-type");
298       return false;
299     }
300   LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
301
302   return true;
303 }
304
305
306 /* SLP costs are calculated according to SLP instance unrolling factor (i.e., 
307    the number of created vector stmts depends on the unrolling factor). However,
308    the actual number of vector stmts for every SLP node depends on VF which is
309    set later in vect_analyze_operations(). Hence, SLP costs should be updated.
310    In this function we assume that the inside costs calculated in 
311    vect_model_xxx_cost are linear in ncopies.  */
312
313 static void
314 vect_update_slp_costs_according_to_vf (loop_vec_info loop_vinfo)
315 {
316   unsigned int i, vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
317   VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
318   slp_instance instance;
319
320   if (vect_print_dump_info (REPORT_SLP))
321     fprintf (vect_dump, "=== vect_update_slp_costs_according_to_vf ===");
322
323   for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); i++)
324     /* We assume that costs are linear in ncopies.  */
325     SLP_INSTANCE_INSIDE_OF_LOOP_COST (instance) *= vf 
326       / SLP_INSTANCE_UNROLLING_FACTOR (instance);         
327 }
328
329
330 /* Function vect_analyze_operations.
331
332    Scan the loop stmts and make sure they are all vectorizable.  */
333
334 static bool
335 vect_analyze_operations (loop_vec_info loop_vinfo)
336 {
337   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
338   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
339   int nbbs = loop->num_nodes;
340   block_stmt_iterator si;
341   unsigned int vectorization_factor = 0;
342   int i;
343   bool ok;
344   tree phi;
345   stmt_vec_info stmt_info;
346   bool need_to_vectorize = false;
347   int min_profitable_iters;
348   int min_scalar_loop_bound;
349   unsigned int th;
350   bool only_slp_in_loop = true;
351
352   if (vect_print_dump_info (REPORT_DETAILS))
353     fprintf (vect_dump, "=== vect_analyze_operations ===");
354
355   gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
356   vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
357
358   for (i = 0; i < nbbs; i++)
359     {
360       basic_block bb = bbs[i];
361
362       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
363         {
364           ok = true;
365
366           stmt_info = vinfo_for_stmt (phi);
367           if (vect_print_dump_info (REPORT_DETAILS))
368             {
369               fprintf (vect_dump, "examining phi: ");
370               print_generic_expr (vect_dump, phi, TDF_SLIM);
371             }
372
373           if (! is_loop_header_bb_p (bb))
374             {
375               /* inner-loop loop-closed exit phi in outer-loop vectorization
376                  (i.e. a phi in the tail of the outer-loop). 
377                  FORNOW: we currently don't support the case that these phis
378                  are not used in the outerloop, cause this case requires
379                  to actually do something here.  */
380               if (!STMT_VINFO_RELEVANT_P (stmt_info) 
381                   || STMT_VINFO_LIVE_P (stmt_info))
382                 {
383                   if (vect_print_dump_info (REPORT_DETAILS))
384                     fprintf (vect_dump, 
385                              "Unsupported loop-closed phi in outer-loop.");
386                   return false;
387                 }
388               continue;
389             }
390
391           gcc_assert (stmt_info);
392
393           if (STMT_VINFO_LIVE_P (stmt_info))
394             {
395               /* FORNOW: not yet supported.  */
396               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
397                 fprintf (vect_dump, "not vectorized: value used after loop.");
398               return false;
399             }
400
401           if (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_loop
402               && STMT_VINFO_DEF_TYPE (stmt_info) != vect_induction_def)
403             {
404               /* A scalar-dependence cycle that we don't support.  */
405               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
406                 fprintf (vect_dump, "not vectorized: scalar dependence cycle.");
407               return false;
408             }
409
410           if (STMT_VINFO_RELEVANT_P (stmt_info))
411             {
412               need_to_vectorize = true;
413               if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
414                 ok = vectorizable_induction (phi, NULL, NULL);
415             }
416
417           if (!ok)
418             {
419               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
420                 {
421                   fprintf (vect_dump,
422                            "not vectorized: relevant phi not supported: ");
423                   print_generic_expr (vect_dump, phi, TDF_SLIM);
424                 }
425               return false;
426             }
427         }
428
429       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
430         {
431           tree stmt = bsi_stmt (si);
432           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
433           enum vect_def_type relevance = STMT_VINFO_RELEVANT (stmt_info);
434
435           if (vect_print_dump_info (REPORT_DETAILS))
436             {
437               fprintf (vect_dump, "==> examining statement: ");
438               print_generic_expr (vect_dump, stmt, TDF_SLIM);
439             }
440
441           gcc_assert (stmt_info);
442
443           /* skip stmts which do not need to be vectorized.
444              this is expected to include:
445              - the COND_EXPR which is the loop exit condition
446              - any LABEL_EXPRs in the loop
447              - computations that are used only for array indexing or loop
448              control  */
449
450           if (!STMT_VINFO_RELEVANT_P (stmt_info)
451               && !STMT_VINFO_LIVE_P (stmt_info))
452             {
453               if (vect_print_dump_info (REPORT_DETAILS))
454                 fprintf (vect_dump, "irrelevant.");
455               continue;
456             }
457
458           switch (STMT_VINFO_DEF_TYPE (stmt_info))
459             {
460             case vect_loop_def:
461               break;
462         
463             case vect_reduction_def:
464               gcc_assert (relevance == vect_used_in_outer
465                           || relevance == vect_used_in_outer_by_reduction
466                           || relevance == vect_unused_in_loop);
467               break;    
468
469             case vect_induction_def:
470             case vect_constant_def:
471             case vect_invariant_def:
472             case vect_unknown_def_type:
473             default:
474               gcc_unreachable ();       
475             }
476
477           if (STMT_VINFO_RELEVANT_P (stmt_info))
478             {
479               gcc_assert (GIMPLE_STMT_P (stmt)
480                           || !VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (stmt))));
481               gcc_assert (STMT_VINFO_VECTYPE (stmt_info));
482               need_to_vectorize = true;
483             }
484
485           ok = true;
486           if (STMT_VINFO_RELEVANT_P (stmt_info)
487               || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
488             ok = (vectorizable_type_promotion (stmt, NULL, NULL)
489                 || vectorizable_type_demotion (stmt, NULL, NULL)
490                 || vectorizable_conversion (stmt, NULL, NULL, NULL)
491                 || vectorizable_operation (stmt, NULL, NULL, NULL)
492                 || vectorizable_assignment (stmt, NULL, NULL, NULL)
493                 || vectorizable_load (stmt, NULL, NULL, NULL)
494                 || vectorizable_call (stmt, NULL, NULL)
495                 || vectorizable_store (stmt, NULL, NULL, NULL)
496                 || vectorizable_condition (stmt, NULL, NULL)
497                 || vectorizable_reduction (stmt, NULL, NULL));
498
499           if (!ok)
500             {
501               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
502                 {
503                   fprintf (vect_dump, "not vectorized: relevant stmt not ");
504                   fprintf (vect_dump, "supported: ");
505                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
506                 }
507               return false;
508             }
509
510           /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
511              need extra handling, except for vectorizable reductions.  */
512           if (STMT_VINFO_LIVE_P (stmt_info)
513               && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type) 
514             ok = vectorizable_live_operation (stmt, NULL, NULL);
515
516           if (!ok)
517             {
518               if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
519                 {
520                   fprintf (vect_dump, "not vectorized: live stmt not ");
521                   fprintf (vect_dump, "supported: ");
522                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
523                 }
524               return false;
525             }   
526
527           if (!PURE_SLP_STMT (stmt_info))
528             {
529               /* STMT needs loop-based vectorization.  */
530               only_slp_in_loop = false;
531
532               /* Groups of strided accesses whose size is not a power of 2 are 
533                  not vectorizable yet using loop-vectorization. Therefore, if 
534                  this stmt feeds non-SLP-able stmts (i.e., this stmt has to be 
535                  both SLPed and loop-based vectorzed), the loop cannot be 
536                  vectorized.  */
537               if (STMT_VINFO_STRIDED_ACCESS (stmt_info)
538                   && exact_log2 (DR_GROUP_SIZE (vinfo_for_stmt (
539                                   DR_GROUP_FIRST_DR (stmt_info)))) == -1)
540                 {
541                   if (vect_print_dump_info (REPORT_DETAILS))
542                     {
543                       fprintf (vect_dump, "not vectorized: the size of group "
544                                "of strided accesses is not a power of 2");
545                       print_generic_expr (vect_dump, stmt, TDF_SLIM);
546                     }
547                   return false;
548                 }
549             }
550         } /* stmts in bb */
551     } /* bbs */
552
553   /* All operations in the loop are either irrelevant (deal with loop
554      control, or dead), or only used outside the loop and can be moved
555      out of the loop (e.g. invariants, inductions).  The loop can be 
556      optimized away by scalar optimizations.  We're better off not 
557      touching this loop.  */
558   if (!need_to_vectorize)
559     {
560       if (vect_print_dump_info (REPORT_DETAILS))
561         fprintf (vect_dump, 
562                  "All the computation can be taken out of the loop.");
563       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
564         fprintf (vect_dump, 
565                  "not vectorized: redundant loop. no profit to vectorize.");
566       return false;
567     }
568
569   /* If all the stmts in the loop can be SLPed, we perform only SLP, and
570      vectorization factor of the loop is the unrolling factor required by the
571      SLP instances.  If that unrolling factor is 1, we say, that we perform
572      pure SLP on loop - cross iteration parallelism is not exploited.  */
573   if (only_slp_in_loop)
574     vectorization_factor = LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo);
575   else
576     vectorization_factor = least_common_multiple (vectorization_factor,
577                                 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo));
578   
579   LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
580
581   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
582       && vect_print_dump_info (REPORT_DETAILS))
583     fprintf (vect_dump,
584         "vectorization_factor = %d, niters = " HOST_WIDE_INT_PRINT_DEC,
585         vectorization_factor, LOOP_VINFO_INT_NITERS (loop_vinfo));
586
587   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
588       && (LOOP_VINFO_INT_NITERS (loop_vinfo) < vectorization_factor))
589     {
590       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
591         fprintf (vect_dump, "not vectorized: iteration count too small.");
592       if (vect_print_dump_info (REPORT_DETAILS))
593         fprintf (vect_dump,"not vectorized: iteration count smaller than "
594                  "vectorization factor.");
595       return false;
596     }
597
598   /* Analyze cost. Decide if worth while to vectorize.  */
599
600   /* Once VF is set, SLP costs should be updated since the number of created
601      vector stmts depends on VF.  */
602   vect_update_slp_costs_according_to_vf (loop_vinfo);
603
604   min_profitable_iters = vect_estimate_min_profitable_iters (loop_vinfo);
605   LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo) = min_profitable_iters;
606   if (min_profitable_iters < 0)
607     {
608       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
609         fprintf (vect_dump, "not vectorized: vectorization not profitable.");
610       if (vect_print_dump_info (REPORT_DETAILS))
611         fprintf (vect_dump, "not vectorized: vector version will never be "
612                  "profitable.");
613       return false;
614     }
615
616   min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
617                             * vectorization_factor) - 1);
618
619   /* Use the cost model only if it is more conservative than user specified
620      threshold.  */
621
622   th = (unsigned) min_scalar_loop_bound;
623   if (min_profitable_iters 
624       && (!min_scalar_loop_bound
625           || min_profitable_iters > min_scalar_loop_bound))
626     th = (unsigned) min_profitable_iters;
627
628   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
629       && LOOP_VINFO_INT_NITERS (loop_vinfo) <= th)
630     {
631       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))           
632         fprintf (vect_dump, "not vectorized: vectorization not "
633                  "profitable.");
634       if (vect_print_dump_info (REPORT_DETAILS))              
635         fprintf (vect_dump, "not vectorized: iteration count smaller than "
636                  "user specified loop bound parameter or minimum "
637                  "profitable iterations (whichever is more conservative).");
638       return false;
639     }  
640
641   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
642       || LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0
643       || LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
644     {
645       if (vect_print_dump_info (REPORT_DETAILS))
646         fprintf (vect_dump, "epilog loop required.");
647       if (!vect_can_advance_ivs_p (loop_vinfo))
648         {
649           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
650             fprintf (vect_dump,
651                      "not vectorized: can't create epilog loop 1.");
652           return false;
653         }
654       if (!slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
655         {
656           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
657             fprintf (vect_dump,
658                      "not vectorized: can't create epilog loop 2.");
659           return false;
660         }
661     }
662
663   return true;
664 }
665
666
667 /* Function exist_non_indexing_operands_for_use_p 
668
669    USE is one of the uses attached to STMT. Check if USE is 
670    used in STMT for anything other than indexing an array.  */
671
672 static bool
673 exist_non_indexing_operands_for_use_p (tree use, tree stmt)
674 {
675   tree operand;
676   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
677  
678   /* USE corresponds to some operand in STMT. If there is no data
679      reference in STMT, then any operand that corresponds to USE
680      is not indexing an array.  */
681   if (!STMT_VINFO_DATA_REF (stmt_info))
682     return true;
683  
684   /* STMT has a data_ref. FORNOW this means that its of one of
685      the following forms:
686      -1- ARRAY_REF = var
687      -2- var = ARRAY_REF
688      (This should have been verified in analyze_data_refs).
689
690      'var' in the second case corresponds to a def, not a use,
691      so USE cannot correspond to any operands that are not used 
692      for array indexing.
693
694      Therefore, all we need to check is if STMT falls into the
695      first case, and whether var corresponds to USE.  */
696  
697   if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
698     return false;
699
700   operand = GIMPLE_STMT_OPERAND (stmt, 1);
701
702   if (TREE_CODE (operand) != SSA_NAME)
703     return false;
704
705   if (operand == use)
706     return true;
707
708   return false;
709 }
710
711
712 /* Function vect_analyze_scalar_cycles_1.
713
714    Examine the cross iteration def-use cycles of scalar variables
715    in LOOP. LOOP_VINFO represents the loop that is noe being
716    considered for vectorization (can be LOOP, or an outer-loop
717    enclosing LOOP).  */
718
719 static void
720 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, struct loop *loop)
721 {
722   tree phi;
723   basic_block bb = loop->header;
724   tree dumy;
725   VEC(tree,heap) *worklist = VEC_alloc (tree, heap, 64);
726
727   if (vect_print_dump_info (REPORT_DETAILS))
728     fprintf (vect_dump, "=== vect_analyze_scalar_cycles ===");
729
730   /* First - identify all inductions.  */
731   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
732     {
733       tree access_fn = NULL;
734       tree def = PHI_RESULT (phi);
735       stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
736
737       if (vect_print_dump_info (REPORT_DETAILS))
738         {
739           fprintf (vect_dump, "Analyze phi: ");
740           print_generic_expr (vect_dump, phi, TDF_SLIM);
741         }
742
743       /* Skip virtual phi's. The data dependences that are associated with
744          virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.  */
745       if (!is_gimple_reg (SSA_NAME_VAR (def)))
746         continue;
747
748       STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_unknown_def_type;
749
750       /* Analyze the evolution function.  */
751       access_fn = analyze_scalar_evolution (loop, def);
752       if (access_fn && vect_print_dump_info (REPORT_DETAILS))
753         {
754           fprintf (vect_dump, "Access function of PHI: ");
755           print_generic_expr (vect_dump, access_fn, TDF_SLIM);
756         }
757
758       if (!access_fn
759           || !vect_is_simple_iv_evolution (loop->num, access_fn, &dumy, &dumy)) 
760         {
761           VEC_safe_push (tree, heap, worklist, phi);      
762           continue;
763         }
764
765       if (vect_print_dump_info (REPORT_DETAILS))
766         fprintf (vect_dump, "Detected induction.");
767       STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_induction_def;
768     }
769
770
771   /* Second - identify all reductions.  */
772   while (VEC_length (tree, worklist) > 0)
773     {
774       tree phi = VEC_pop (tree, worklist);
775       tree def = PHI_RESULT (phi);
776       stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
777       tree reduc_stmt;
778
779       if (vect_print_dump_info (REPORT_DETAILS))
780         { 
781           fprintf (vect_dump, "Analyze phi: ");
782           print_generic_expr (vect_dump, phi, TDF_SLIM);
783         }
784
785       gcc_assert (is_gimple_reg (SSA_NAME_VAR (def)));
786       gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_unknown_def_type);
787
788       reduc_stmt = vect_is_simple_reduction (loop_vinfo, phi);
789       if (reduc_stmt)
790         {
791           if (vect_print_dump_info (REPORT_DETAILS))
792             fprintf (vect_dump, "Detected reduction.");
793           STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_reduction_def;
794           STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
795                                                         vect_reduction_def;
796         }
797       else
798         if (vect_print_dump_info (REPORT_DETAILS))
799           fprintf (vect_dump, "Unknown def-use cycle pattern.");
800     }
801
802   VEC_free (tree, heap, worklist);
803   return;
804 }
805
806
807 /* Function vect_analyze_scalar_cycles.
808
809    Examine the cross iteration def-use cycles of scalar variables, by
810    analyzing the loop-header PHIs of scalar variables; Classify each 
811    cycle as one of the following: invariant, induction, reduction, unknown.
812    We do that for the loop represented by LOOP_VINFO, and also to its
813    inner-loop, if exists.
814    Examples for scalar cycles:
815
816    Example1: reduction:
817
818               loop1:
819               for (i=0; i<N; i++)
820                  sum += a[i];
821
822    Example2: induction:
823
824               loop2:
825               for (i=0; i<N; i++)
826                  a[i] = i;  */
827
828 static void
829 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
830 {
831   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
832
833   vect_analyze_scalar_cycles_1 (loop_vinfo, loop);
834
835   /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
836      Reductions in such inner-loop therefore have different properties than
837      the reductions in the nest that gets vectorized:
838      1. When vectorized, they are executed in the same order as in the original
839         scalar loop, so we can't change the order of computation when
840         vectorizing them.
841      2. FIXME: Inner-loop reductions can be used in the inner-loop, so the 
842         current checks are too strict.  */
843
844   if (loop->inner)
845     vect_analyze_scalar_cycles_1 (loop_vinfo, loop->inner);
846 }
847
848
849 /* Function vect_insert_into_interleaving_chain.
850
851    Insert DRA into the interleaving chain of DRB according to DRA's INIT.  */
852
853 static void
854 vect_insert_into_interleaving_chain (struct data_reference *dra,
855                                      struct data_reference *drb)
856 {
857   tree prev, next, next_init;
858   stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra)); 
859   stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
860
861   prev = DR_GROUP_FIRST_DR (stmtinfo_b);
862   next = DR_GROUP_NEXT_DR (vinfo_for_stmt (prev));                
863   while (next)
864     {
865       next_init = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (next)));
866       if (tree_int_cst_compare (next_init, DR_INIT (dra)) > 0)
867         {
868           /* Insert here.  */
869           DR_GROUP_NEXT_DR (vinfo_for_stmt (prev)) = DR_STMT (dra);
870           DR_GROUP_NEXT_DR (stmtinfo_a) = next;
871           return;
872         }
873       prev = next;
874       next = DR_GROUP_NEXT_DR (vinfo_for_stmt (prev));
875     }
876
877   /* We got to the end of the list. Insert here.  */
878   DR_GROUP_NEXT_DR (vinfo_for_stmt (prev)) = DR_STMT (dra);
879   DR_GROUP_NEXT_DR (stmtinfo_a) = NULL_TREE;
880 }
881
882
883 /* Function vect_update_interleaving_chain.
884    
885    For two data-refs DRA and DRB that are a part of a chain interleaved data 
886    accesses, update the interleaving chain. DRB's INIT is smaller than DRA's.
887
888    There are four possible cases:
889    1. New stmts - both DRA and DRB are not a part of any chain:
890       FIRST_DR = DRB
891       NEXT_DR (DRB) = DRA
892    2. DRB is a part of a chain and DRA is not:
893       no need to update FIRST_DR
894       no need to insert DRB
895       insert DRA according to init
896    3. DRA is a part of a chain and DRB is not:
897       if (init of FIRST_DR > init of DRB)
898           FIRST_DR = DRB
899           NEXT(FIRST_DR) = previous FIRST_DR
900       else
901           insert DRB according to its init
902    4. both DRA and DRB are in some interleaving chains:
903       choose the chain with the smallest init of FIRST_DR
904       insert the nodes of the second chain into the first one.  */
905
906 static void
907 vect_update_interleaving_chain (struct data_reference *drb,
908                                 struct data_reference *dra)
909 {
910   stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra)); 
911   stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
912   tree next_init, init_dra_chain, init_drb_chain, first_a, first_b;
913   tree node, prev, next, node_init, first_stmt;
914
915   /* 1. New stmts - both DRA and DRB are not a part of any chain.   */
916   if (!DR_GROUP_FIRST_DR (stmtinfo_a) && !DR_GROUP_FIRST_DR (stmtinfo_b))
917     {
918       DR_GROUP_FIRST_DR (stmtinfo_a) = DR_STMT (drb);
919       DR_GROUP_FIRST_DR (stmtinfo_b) = DR_STMT (drb);
920       DR_GROUP_NEXT_DR (stmtinfo_b) = DR_STMT (dra);
921       return;
922     }
923
924   /* 2. DRB is a part of a chain and DRA is not.  */
925   if (!DR_GROUP_FIRST_DR (stmtinfo_a) && DR_GROUP_FIRST_DR (stmtinfo_b))
926     {
927       DR_GROUP_FIRST_DR (stmtinfo_a) = DR_GROUP_FIRST_DR (stmtinfo_b);
928       /* Insert DRA into the chain of DRB.  */
929       vect_insert_into_interleaving_chain (dra, drb);
930       return;
931     }
932
933   /* 3. DRA is a part of a chain and DRB is not.  */  
934   if (DR_GROUP_FIRST_DR (stmtinfo_a) && !DR_GROUP_FIRST_DR (stmtinfo_b))
935     {
936       tree old_first_stmt = DR_GROUP_FIRST_DR (stmtinfo_a);
937       tree init_old = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (
938                                                               old_first_stmt)));
939       tree tmp;
940
941       if (tree_int_cst_compare (init_old, DR_INIT (drb)) > 0)
942         {
943           /* DRB's init is smaller than the init of the stmt previously marked 
944              as the first stmt of the interleaving chain of DRA. Therefore, we 
945              update FIRST_STMT and put DRB in the head of the list.  */
946           DR_GROUP_FIRST_DR (stmtinfo_b) = DR_STMT (drb);
947           DR_GROUP_NEXT_DR (stmtinfo_b) = old_first_stmt;
948                 
949           /* Update all the stmts in the list to point to the new FIRST_STMT.  */
950           tmp = old_first_stmt;
951           while (tmp)
952             {
953               DR_GROUP_FIRST_DR (vinfo_for_stmt (tmp)) = DR_STMT (drb);
954               tmp = DR_GROUP_NEXT_DR (vinfo_for_stmt (tmp));
955             }
956         }
957       else
958         {
959           /* Insert DRB in the list of DRA.  */
960           vect_insert_into_interleaving_chain (drb, dra);
961           DR_GROUP_FIRST_DR (stmtinfo_b) = DR_GROUP_FIRST_DR (stmtinfo_a);            
962         }
963       return;
964     }
965   
966   /* 4. both DRA and DRB are in some interleaving chains.  */
967   first_a = DR_GROUP_FIRST_DR (stmtinfo_a);
968   first_b = DR_GROUP_FIRST_DR (stmtinfo_b);
969   if (first_a == first_b)
970     return;
971   init_dra_chain = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (first_a)));
972   init_drb_chain = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (first_b)));
973
974   if (tree_int_cst_compare (init_dra_chain, init_drb_chain) > 0)
975     {
976       /* Insert the nodes of DRA chain into the DRB chain.  
977          After inserting a node, continue from this node of the DRB chain (don't
978          start from the beginning.  */
979       node = DR_GROUP_FIRST_DR (stmtinfo_a);
980       prev = DR_GROUP_FIRST_DR (stmtinfo_b);      
981       first_stmt = first_b;
982     }
983   else
984     {
985       /* Insert the nodes of DRB chain into the DRA chain.  
986          After inserting a node, continue from this node of the DRA chain (don't
987          start from the beginning.  */
988       node = DR_GROUP_FIRST_DR (stmtinfo_b);
989       prev = DR_GROUP_FIRST_DR (stmtinfo_a);      
990       first_stmt = first_a;
991     }
992   
993   while (node)
994     {
995       node_init = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (node)));
996       next = DR_GROUP_NEXT_DR (vinfo_for_stmt (prev));            
997       while (next)
998         {         
999           next_init = DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (next)));
1000           if (tree_int_cst_compare (next_init, node_init) > 0)
1001             {
1002               /* Insert here.  */
1003               DR_GROUP_NEXT_DR (vinfo_for_stmt (prev)) = node;
1004               DR_GROUP_NEXT_DR (vinfo_for_stmt (node)) = next;
1005               prev = node;
1006               break;
1007             }
1008           prev = next;
1009           next = DR_GROUP_NEXT_DR (vinfo_for_stmt (prev));
1010         }
1011       if (!next)
1012         {
1013           /* We got to the end of the list. Insert here.  */
1014           DR_GROUP_NEXT_DR (vinfo_for_stmt (prev)) = node;
1015           DR_GROUP_NEXT_DR (vinfo_for_stmt (node)) = NULL_TREE;
1016           prev = node;
1017         }                       
1018       DR_GROUP_FIRST_DR (vinfo_for_stmt (node)) = first_stmt;
1019       node = DR_GROUP_NEXT_DR (vinfo_for_stmt (node));         
1020     }
1021 }
1022
1023
1024 /* Function vect_equal_offsets.
1025
1026    Check if OFFSET1 and OFFSET2 are identical expressions.  */
1027
1028 static bool
1029 vect_equal_offsets (tree offset1, tree offset2)
1030 {
1031   bool res0, res1;
1032
1033   STRIP_NOPS (offset1);
1034   STRIP_NOPS (offset2);
1035
1036   if (offset1 == offset2)
1037     return true;
1038
1039   if (TREE_CODE (offset1) != TREE_CODE (offset2)
1040       || !BINARY_CLASS_P (offset1)
1041       || !BINARY_CLASS_P (offset2))    
1042     return false;
1043   
1044   res0 = vect_equal_offsets (TREE_OPERAND (offset1, 0), 
1045                              TREE_OPERAND (offset2, 0));
1046   res1 = vect_equal_offsets (TREE_OPERAND (offset1, 1), 
1047                              TREE_OPERAND (offset2, 1));
1048
1049   return (res0 && res1);
1050 }
1051
1052
1053 /* Function vect_check_interleaving.
1054
1055    Check if DRA and DRB are a part of interleaving. In case they are, insert
1056    DRA and DRB in an interleaving chain.  */
1057
1058 static void
1059 vect_check_interleaving (struct data_reference *dra,
1060                          struct data_reference *drb)
1061 {
1062   HOST_WIDE_INT type_size_a, type_size_b, diff_mod_size, step, init_a, init_b;
1063
1064   /* Check that the data-refs have same first location (except init) and they
1065      are both either store or load (not load and store).  */
1066   if ((DR_BASE_ADDRESS (dra) != DR_BASE_ADDRESS (drb)
1067        && (TREE_CODE (DR_BASE_ADDRESS (dra)) != ADDR_EXPR 
1068            || TREE_CODE (DR_BASE_ADDRESS (drb)) != ADDR_EXPR
1069            || TREE_OPERAND (DR_BASE_ADDRESS (dra), 0) 
1070            != TREE_OPERAND (DR_BASE_ADDRESS (drb),0)))
1071       || !vect_equal_offsets (DR_OFFSET (dra), DR_OFFSET (drb))
1072       || !tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) 
1073       || DR_IS_READ (dra) != DR_IS_READ (drb))
1074     return;
1075
1076   /* Check:
1077      1. data-refs are of the same type
1078      2. their steps are equal
1079      3. the step is greater than the difference between data-refs' inits  */
1080   type_size_a = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))));
1081   type_size_b = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
1082
1083   if (type_size_a != type_size_b
1084       || tree_int_cst_compare (DR_STEP (dra), DR_STEP (drb)))
1085     return;
1086
1087   init_a = TREE_INT_CST_LOW (DR_INIT (dra));
1088   init_b = TREE_INT_CST_LOW (DR_INIT (drb));
1089   step = TREE_INT_CST_LOW (DR_STEP (dra));
1090
1091   if (init_a > init_b)
1092     {
1093       /* If init_a == init_b + the size of the type * k, we have an interleaving, 
1094          and DRB is accessed before DRA.  */
1095       diff_mod_size = (init_a - init_b) % type_size_a;
1096
1097       if ((init_a - init_b) > step)
1098          return; 
1099
1100       if (diff_mod_size == 0)
1101         {
1102           vect_update_interleaving_chain (drb, dra);      
1103           if (vect_print_dump_info (REPORT_DR_DETAILS))
1104             {
1105               fprintf (vect_dump, "Detected interleaving ");
1106               print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1107               fprintf (vect_dump, " and ");
1108               print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1109             }
1110           return;
1111         } 
1112     }
1113   else 
1114     {
1115       /* If init_b == init_a + the size of the type * k, we have an 
1116          interleaving, and DRA is accessed before DRB.  */
1117       diff_mod_size = (init_b - init_a) % type_size_a;
1118
1119       if ((init_b - init_a) > step)
1120          return;
1121
1122       if (diff_mod_size == 0)
1123         {
1124           vect_update_interleaving_chain (dra, drb);      
1125           if (vect_print_dump_info (REPORT_DR_DETAILS))
1126             {
1127               fprintf (vect_dump, "Detected interleaving ");
1128               print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1129               fprintf (vect_dump, " and ");
1130               print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1131             }
1132           return;
1133         } 
1134     }
1135 }
1136
1137 /* Check if data references pointed by DR_I and DR_J are same or
1138    belong to same interleaving group.  Return FALSE if drs are
1139    different, otherwise return TRUE.  */
1140
1141 static bool
1142 vect_same_range_drs (data_reference_p dr_i, data_reference_p dr_j)
1143 {
1144   tree stmt_i = DR_STMT (dr_i);
1145   tree stmt_j = DR_STMT (dr_j);
1146
1147   if (operand_equal_p (DR_REF (dr_i), DR_REF (dr_j), 0)
1148       || (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_i))
1149             && DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_j))
1150             && (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_i))
1151                 == DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_j)))))
1152     return true;
1153   else
1154     return false;
1155 }
1156
1157 /* If address ranges represented by DDR_I and DDR_J are equal,
1158    return TRUE, otherwise return FALSE.  */
1159
1160 static bool
1161 vect_vfa_range_equal (ddr_p ddr_i, ddr_p ddr_j)
1162 {
1163   if ((vect_same_range_drs (DDR_A (ddr_i), DDR_A (ddr_j))
1164        && vect_same_range_drs (DDR_B (ddr_i), DDR_B (ddr_j)))
1165       || (vect_same_range_drs (DDR_A (ddr_i), DDR_B (ddr_j))
1166           && vect_same_range_drs (DDR_B (ddr_i), DDR_A (ddr_j))))
1167     return true;
1168   else
1169     return false;
1170 }
1171
1172 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
1173    tested at run-time.  Return TRUE if DDR was successfully inserted.
1174    Return false if versioning is not supported.  */
1175
1176 static bool
1177 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
1178 {
1179   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1180
1181   if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
1182     return false;
1183
1184   if (vect_print_dump_info (REPORT_DR_DETAILS))
1185     {
1186       fprintf (vect_dump, "mark for run-time aliasing test between ");
1187       print_generic_expr (vect_dump, DR_REF (DDR_A (ddr)), TDF_SLIM);
1188       fprintf (vect_dump, " and ");
1189       print_generic_expr (vect_dump, DR_REF (DDR_B (ddr)), TDF_SLIM);
1190     }
1191
1192   if (optimize_size)
1193     {
1194       if (vect_print_dump_info (REPORT_DR_DETAILS))
1195         fprintf (vect_dump, "versioning not supported when optimizing for size.");
1196       return false;
1197     }
1198
1199   /* FORNOW: We don't support versioning with outer-loop vectorization.  */
1200   if (loop->inner)
1201     {
1202       if (vect_print_dump_info (REPORT_DR_DETAILS))
1203         fprintf (vect_dump, "versioning not yet supported for outer-loops.");
1204       return false;
1205     }
1206
1207   VEC_safe_push (ddr_p, heap, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo), ddr);
1208   return true;
1209 }
1210
1211 /* Function vect_analyze_data_ref_dependence.
1212
1213    Return TRUE if there (might) exist a dependence between a memory-reference
1214    DRA and a memory-reference DRB.  When versioning for alias may check a
1215    dependence at run-time, return FALSE.  */
1216       
1217 static bool
1218 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
1219                                   loop_vec_info loop_vinfo)
1220 {
1221   unsigned int i;
1222   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1223   int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1224   struct data_reference *dra = DDR_A (ddr);
1225   struct data_reference *drb = DDR_B (ddr);
1226   stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra)); 
1227   stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1228   int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1229   int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1230   lambda_vector dist_v;
1231   unsigned int loop_depth;
1232          
1233   if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1234     {
1235       /* Independent data accesses.  */
1236       vect_check_interleaving (dra, drb);
1237       return false;
1238     }
1239
1240   if ((DR_IS_READ (dra) && DR_IS_READ (drb)) || dra == drb)
1241     return false;
1242   
1243   if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1244     {
1245       if (vect_print_dump_info (REPORT_DR_DETAILS))
1246         {
1247           fprintf (vect_dump,
1248                    "versioning for alias required: can't determine dependence between ");
1249           print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1250           fprintf (vect_dump, " and ");
1251           print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1252         }
1253       /* Add to list of ddrs that need to be tested at run-time.  */
1254       return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
1255     }
1256
1257   if (DDR_NUM_DIST_VECTS (ddr) == 0)
1258     {
1259       if (vect_print_dump_info (REPORT_DR_DETAILS))
1260         {
1261           fprintf (vect_dump, "versioning for alias required: bad dist vector for ");
1262           print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1263           fprintf (vect_dump, " and ");
1264           print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1265         }
1266       /* Add to list of ddrs that need to be tested at run-time.  */
1267       return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
1268     }    
1269
1270   loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1271   for (i = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), i, dist_v); i++)
1272     {
1273       int dist = dist_v[loop_depth];
1274
1275       if (vect_print_dump_info (REPORT_DR_DETAILS))
1276         fprintf (vect_dump, "dependence distance  = %d.", dist);
1277
1278       /* Same loop iteration.  */
1279       if (dist % vectorization_factor == 0 && dra_size == drb_size)
1280         {
1281           /* Two references with distance zero have the same alignment.  */
1282           VEC_safe_push (dr_p, heap, STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a), drb);
1283           VEC_safe_push (dr_p, heap, STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b), dra);
1284           if (vect_print_dump_info (REPORT_ALIGNMENT))
1285             fprintf (vect_dump, "accesses have the same alignment.");
1286           if (vect_print_dump_info (REPORT_DR_DETAILS))
1287             {
1288               fprintf (vect_dump, "dependence distance modulo vf == 0 between ");
1289               print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1290               fprintf (vect_dump, " and ");
1291               print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1292             }
1293
1294           /* For interleaving, mark that there is a read-write dependency if
1295              necessary. We check before that one of the data-refs is store.  */ 
1296           if (DR_IS_READ (dra))
1297             DR_GROUP_READ_WRITE_DEPENDENCE (stmtinfo_a) = true;
1298           else
1299             {
1300               if (DR_IS_READ (drb))
1301                 DR_GROUP_READ_WRITE_DEPENDENCE (stmtinfo_b) = true;
1302             }
1303           
1304           continue;
1305         }
1306
1307       if (abs (dist) >= vectorization_factor 
1308           || (dist > 0 && DDR_REVERSED_P (ddr)))
1309         {
1310           /* Dependence distance does not create dependence, as far as 
1311              vectorization is concerned, in this case. If DDR_REVERSED_P the 
1312              order of the data-refs in DDR was reversed (to make distance
1313              vector positive), and the actual distance is negative.  */
1314           if (vect_print_dump_info (REPORT_DR_DETAILS))
1315             fprintf (vect_dump, "dependence distance >= VF or negative.");
1316           continue;
1317         }
1318
1319       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
1320         {
1321           fprintf (vect_dump,
1322                    "not vectorized, possible dependence "
1323                    "between data-refs ");
1324           print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
1325           fprintf (vect_dump, " and ");
1326           print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
1327         }
1328
1329       return true;
1330     }
1331
1332   return false;
1333 }
1334
1335 /* Function vect_analyze_data_ref_dependences.
1336           
1337    Examine all the data references in the loop, and make sure there do not
1338    exist any data dependences between them.  */
1339          
1340 static bool
1341 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo)
1342 {
1343   unsigned int i;
1344   VEC (ddr_p, heap) * ddrs = LOOP_VINFO_DDRS (loop_vinfo);
1345   struct data_dependence_relation *ddr;
1346
1347   if (vect_print_dump_info (REPORT_DETAILS)) 
1348     fprintf (vect_dump, "=== vect_analyze_dependences ===");
1349      
1350   for (i = 0; VEC_iterate (ddr_p, ddrs, i, ddr); i++)
1351     if (vect_analyze_data_ref_dependence (ddr, loop_vinfo))
1352       return false;
1353
1354   return true;
1355 }
1356
1357
1358 /* Function vect_compute_data_ref_alignment
1359
1360    Compute the misalignment of the data reference DR.
1361
1362    Output:
1363    1. If during the misalignment computation it is found that the data reference
1364       cannot be vectorized then false is returned.
1365    2. DR_MISALIGNMENT (DR) is defined.
1366
1367    FOR NOW: No analysis is actually performed. Misalignment is calculated
1368    only for trivial cases. TODO.  */
1369
1370 static bool
1371 vect_compute_data_ref_alignment (struct data_reference *dr)
1372 {
1373   tree stmt = DR_STMT (dr);
1374   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);  
1375   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1376   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1377   tree ref = DR_REF (dr);
1378   tree vectype;
1379   tree base, base_addr;
1380   bool base_aligned;
1381   tree misalign;
1382   tree aligned_to, alignment;
1383    
1384   if (vect_print_dump_info (REPORT_DETAILS))
1385     fprintf (vect_dump, "vect_compute_data_ref_alignment:");
1386
1387   /* Initialize misalignment to unknown.  */
1388   SET_DR_MISALIGNMENT (dr, -1);
1389
1390   misalign = DR_INIT (dr);
1391   aligned_to = DR_ALIGNED_TO (dr);
1392   base_addr = DR_BASE_ADDRESS (dr);
1393
1394   /* In case the dataref is in an inner-loop of the loop that is being
1395      vectorized (LOOP), we use the base and misalignment information
1396      relative to the outer-loop (LOOP). This is ok only if the misalignment
1397      stays the same throughout the execution of the inner-loop, which is why
1398      we have to check that the stride of the dataref in the inner-loop evenly
1399      divides by the vector size.  */
1400   if (nested_in_vect_loop_p (loop, stmt))
1401     {
1402       tree step = DR_STEP (dr);
1403       HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
1404     
1405       if (dr_step % UNITS_PER_SIMD_WORD == 0)
1406         {
1407           if (vect_print_dump_info (REPORT_ALIGNMENT))
1408             fprintf (vect_dump, "inner step divides the vector-size.");
1409           misalign = STMT_VINFO_DR_INIT (stmt_info);
1410           aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
1411           base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
1412         }
1413       else
1414         {
1415           if (vect_print_dump_info (REPORT_ALIGNMENT))
1416             fprintf (vect_dump, "inner step doesn't divide the vector-size.");
1417           misalign = NULL_TREE;
1418         }
1419     }
1420
1421   base = build_fold_indirect_ref (base_addr);
1422   vectype = STMT_VINFO_VECTYPE (stmt_info);
1423   alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
1424
1425   if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
1426       || !misalign)
1427     {
1428       if (vect_print_dump_info (REPORT_ALIGNMENT))
1429         {
1430           fprintf (vect_dump, "Unknown alignment for access: ");
1431           print_generic_expr (vect_dump, base, TDF_SLIM);
1432         }
1433       return true;
1434     }
1435
1436   if ((DECL_P (base) 
1437        && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
1438                                 alignment) >= 0)
1439       || (TREE_CODE (base_addr) == SSA_NAME
1440           && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
1441                                                       TREE_TYPE (base_addr)))),
1442                                    alignment) >= 0))
1443     base_aligned = true;
1444   else
1445     base_aligned = false;   
1446
1447   if (!base_aligned) 
1448     {
1449       /* Do not change the alignment of global variables if 
1450          flag_section_anchors is enabled.  */
1451       if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
1452           || (TREE_STATIC (base) && flag_section_anchors))
1453         {
1454           if (vect_print_dump_info (REPORT_DETAILS))
1455             {
1456               fprintf (vect_dump, "can't force alignment of ref: ");
1457               print_generic_expr (vect_dump, ref, TDF_SLIM);
1458             }
1459           return true;
1460         }
1461       
1462       /* Force the alignment of the decl.
1463          NOTE: This is the only change to the code we make during
1464          the analysis phase, before deciding to vectorize the loop.  */
1465       if (vect_print_dump_info (REPORT_DETAILS))
1466         fprintf (vect_dump, "force alignment");
1467       DECL_ALIGN (base) = TYPE_ALIGN (vectype);
1468       DECL_USER_ALIGN (base) = 1;
1469     }
1470
1471   /* At this point we assume that the base is aligned.  */
1472   gcc_assert (base_aligned
1473               || (TREE_CODE (base) == VAR_DECL 
1474                   && DECL_ALIGN (base) >= TYPE_ALIGN (vectype)));
1475
1476   /* Modulo alignment.  */
1477   misalign = size_binop (TRUNC_MOD_EXPR, misalign, alignment);
1478
1479   if (!host_integerp (misalign, 1))
1480     {
1481       /* Negative or overflowed misalignment value.  */
1482       if (vect_print_dump_info (REPORT_DETAILS))
1483         fprintf (vect_dump, "unexpected misalign value");
1484       return false;
1485     }
1486
1487   SET_DR_MISALIGNMENT (dr, TREE_INT_CST_LOW (misalign));
1488
1489   if (vect_print_dump_info (REPORT_DETAILS))
1490     {
1491       fprintf (vect_dump, "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
1492       print_generic_expr (vect_dump, ref, TDF_SLIM);
1493     }
1494
1495   return true;
1496 }
1497
1498
1499 /* Function vect_compute_data_refs_alignment
1500
1501    Compute the misalignment of data references in the loop.
1502    Return FALSE if a data reference is found that cannot be vectorized.  */
1503
1504 static bool
1505 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo)
1506 {
1507   VEC (data_reference_p, heap) *datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1508   struct data_reference *dr;
1509   unsigned int i;
1510
1511   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1512     if (!vect_compute_data_ref_alignment (dr))
1513       return false;
1514
1515   return true;
1516 }
1517
1518
1519 /* Function vect_update_misalignment_for_peel
1520
1521    DR - the data reference whose misalignment is to be adjusted.
1522    DR_PEEL - the data reference whose misalignment is being made
1523              zero in the vector loop by the peel.
1524    NPEEL - the number of iterations in the peel loop if the misalignment
1525            of DR_PEEL is known at compile time.  */
1526
1527 static void
1528 vect_update_misalignment_for_peel (struct data_reference *dr,
1529                                    struct data_reference *dr_peel, int npeel)
1530 {
1531   unsigned int i;
1532   VEC(dr_p,heap) *same_align_drs;
1533   struct data_reference *current_dr;
1534   int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
1535   int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
1536   stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
1537   stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
1538
1539  /* For interleaved data accesses the step in the loop must be multiplied by
1540      the size of the interleaving group.  */
1541   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
1542     dr_size *= DR_GROUP_SIZE (vinfo_for_stmt (DR_GROUP_FIRST_DR (stmt_info)));
1543   if (STMT_VINFO_STRIDED_ACCESS (peel_stmt_info))
1544     dr_peel_size *= DR_GROUP_SIZE (peel_stmt_info);
1545
1546   /* It can be assumed that the data refs with the same alignment as dr_peel
1547      are aligned in the vector loop.  */
1548   same_align_drs
1549     = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
1550   for (i = 0; VEC_iterate (dr_p, same_align_drs, i, current_dr); i++)
1551     {
1552       if (current_dr != dr)
1553         continue;
1554       gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
1555                   DR_MISALIGNMENT (dr_peel) / dr_peel_size);
1556       SET_DR_MISALIGNMENT (dr, 0);
1557       return;
1558     }
1559
1560   if (known_alignment_for_access_p (dr)
1561       && known_alignment_for_access_p (dr_peel))
1562     {
1563       int misal = DR_MISALIGNMENT (dr);
1564       misal += npeel * dr_size;
1565       misal %= UNITS_PER_SIMD_WORD;
1566       SET_DR_MISALIGNMENT (dr, misal);
1567       return;
1568     }
1569
1570   if (vect_print_dump_info (REPORT_DETAILS))
1571     fprintf (vect_dump, "Setting misalignment to -1.");
1572   SET_DR_MISALIGNMENT (dr, -1);
1573 }
1574
1575
1576 /* Function vect_verify_datarefs_alignment
1577
1578    Return TRUE if all data references in the loop can be
1579    handled with respect to alignment.  */
1580
1581 static bool
1582 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo)
1583 {
1584   VEC (data_reference_p, heap) *datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1585   struct data_reference *dr;
1586   enum dr_alignment_support supportable_dr_alignment;
1587   unsigned int i;
1588
1589   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1590     {
1591       tree stmt = DR_STMT (dr);
1592       stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1593
1594       /* For interleaving, only the alignment of the first access matters.  */
1595       if (STMT_VINFO_STRIDED_ACCESS (stmt_info)
1596           && DR_GROUP_FIRST_DR (stmt_info) != stmt)
1597         continue;
1598
1599       supportable_dr_alignment = vect_supportable_dr_alignment (dr);
1600       if (!supportable_dr_alignment)
1601         {
1602           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
1603             {
1604               if (DR_IS_READ (dr))
1605                 fprintf (vect_dump, 
1606                          "not vectorized: unsupported unaligned load.");
1607               else
1608                 fprintf (vect_dump, 
1609                          "not vectorized: unsupported unaligned store.");
1610             }
1611           return false;
1612         }
1613       if (supportable_dr_alignment != dr_aligned
1614           && vect_print_dump_info (REPORT_ALIGNMENT))
1615         fprintf (vect_dump, "Vectorizing an unaligned access.");
1616     }
1617   return true;
1618 }
1619
1620
1621 /* Function vector_alignment_reachable_p
1622
1623    Return true if vector alignment for DR is reachable by peeling
1624    a few loop iterations.  Return false otherwise.  */
1625
1626 static bool
1627 vector_alignment_reachable_p (struct data_reference *dr)
1628 {
1629   tree stmt = DR_STMT (dr);
1630   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1631   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1632
1633   if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
1634     {
1635       /* For interleaved access we peel only if number of iterations in
1636          the prolog loop ({VF - misalignment}), is a multiple of the
1637          number of the interleaved accesses.  */
1638       int elem_size, mis_in_elements;
1639       int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1640
1641       /* FORNOW: handle only known alignment.  */
1642       if (!known_alignment_for_access_p (dr))
1643         return false;
1644
1645       elem_size = UNITS_PER_SIMD_WORD / nelements;
1646       mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1647
1648       if ((nelements - mis_in_elements) % DR_GROUP_SIZE (stmt_info))
1649         return false;
1650     }
1651
1652   /* If misalignment is known at the compile time then allow peeling
1653      only if natural alignment is reachable through peeling.  */
1654   if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1655     {
1656       HOST_WIDE_INT elmsize = 
1657                 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1658       if (vect_print_dump_info (REPORT_DETAILS))
1659         {
1660           fprintf (vect_dump, "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1661           fprintf (vect_dump, ". misalignment = %d. ", DR_MISALIGNMENT (dr));
1662         }
1663       if (DR_MISALIGNMENT (dr) % elmsize)
1664         {
1665           if (vect_print_dump_info (REPORT_DETAILS))
1666             fprintf (vect_dump, "data size does not divide the misalignment.\n");
1667           return false;
1668         }
1669     }
1670
1671   if (!known_alignment_for_access_p (dr))
1672     {
1673       tree type = (TREE_TYPE (DR_REF (dr)));
1674       tree ba = DR_BASE_OBJECT (dr);
1675       bool is_packed = false;
1676
1677       if (ba)
1678         is_packed = contains_packed_reference (ba);
1679
1680       if (vect_print_dump_info (REPORT_DETAILS))
1681         fprintf (vect_dump, "Unknown misalignment, is_packed = %d",is_packed);
1682       if (targetm.vectorize.vector_alignment_reachable (type, is_packed))
1683         return true;
1684       else
1685         return false;
1686     }
1687
1688   return true;
1689 }
1690
1691 /* Function vect_enhance_data_refs_alignment
1692
1693    This pass will use loop versioning and loop peeling in order to enhance
1694    the alignment of data references in the loop.
1695
1696    FOR NOW: we assume that whatever versioning/peeling takes place, only the
1697    original loop is to be vectorized; Any other loops that are created by
1698    the transformations performed in this pass - are not supposed to be
1699    vectorized. This restriction will be relaxed.
1700
1701    This pass will require a cost model to guide it whether to apply peeling
1702    or versioning or a combination of the two. For example, the scheme that
1703    intel uses when given a loop with several memory accesses, is as follows:
1704    choose one memory access ('p') which alignment you want to force by doing
1705    peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1706    other accesses are not necessarily aligned, or (2) use loop versioning to
1707    generate one loop in which all accesses are aligned, and another loop in
1708    which only 'p' is necessarily aligned.
1709
1710    ("Automatic Intra-Register Vectorization for the Intel Architecture",
1711    Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1712    Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1713
1714    Devising a cost model is the most critical aspect of this work. It will
1715    guide us on which access to peel for, whether to use loop versioning, how
1716    many versions to create, etc. The cost model will probably consist of
1717    generic considerations as well as target specific considerations (on
1718    powerpc for example, misaligned stores are more painful than misaligned
1719    loads).
1720
1721    Here are the general steps involved in alignment enhancements:
1722
1723      -- original loop, before alignment analysis:
1724         for (i=0; i<N; i++){
1725           x = q[i];                     # DR_MISALIGNMENT(q) = unknown
1726           p[i] = y;                     # DR_MISALIGNMENT(p) = unknown
1727         }
1728
1729      -- After vect_compute_data_refs_alignment:
1730         for (i=0; i<N; i++){
1731           x = q[i];                     # DR_MISALIGNMENT(q) = 3
1732           p[i] = y;                     # DR_MISALIGNMENT(p) = unknown
1733         }
1734
1735      -- Possibility 1: we do loop versioning:
1736      if (p is aligned) {
1737         for (i=0; i<N; i++){    # loop 1A
1738           x = q[i];                     # DR_MISALIGNMENT(q) = 3
1739           p[i] = y;                     # DR_MISALIGNMENT(p) = 0
1740         }
1741      }
1742      else {
1743         for (i=0; i<N; i++){    # loop 1B
1744           x = q[i];                     # DR_MISALIGNMENT(q) = 3
1745           p[i] = y;                     # DR_MISALIGNMENT(p) = unaligned
1746         }
1747      }
1748
1749      -- Possibility 2: we do loop peeling:
1750      for (i = 0; i < 3; i++){   # (scalar loop, not to be vectorized).
1751         x = q[i];
1752         p[i] = y;
1753      }
1754      for (i = 3; i < N; i++){   # loop 2A
1755         x = q[i];                       # DR_MISALIGNMENT(q) = 0
1756         p[i] = y;                       # DR_MISALIGNMENT(p) = unknown
1757      }
1758
1759      -- Possibility 3: combination of loop peeling and versioning:
1760      for (i = 0; i < 3; i++){   # (scalar loop, not to be vectorized).
1761         x = q[i];
1762         p[i] = y;
1763      }
1764      if (p is aligned) {
1765         for (i = 3; i<N; i++){  # loop 3A
1766           x = q[i];                     # DR_MISALIGNMENT(q) = 0
1767           p[i] = y;                     # DR_MISALIGNMENT(p) = 0
1768         }
1769      }
1770      else {
1771         for (i = 3; i<N; i++){  # loop 3B
1772           x = q[i];                     # DR_MISALIGNMENT(q) = 0
1773           p[i] = y;                     # DR_MISALIGNMENT(p) = unaligned
1774         }
1775      }
1776
1777      These loops are later passed to loop_transform to be vectorized. The
1778      vectorizer will use the alignment information to guide the transformation
1779      (whether to generate regular loads/stores, or with special handling for
1780      misalignment).  */
1781
1782 static bool
1783 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1784 {
1785   VEC (data_reference_p, heap) *datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1786   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1787   enum dr_alignment_support supportable_dr_alignment;
1788   struct data_reference *dr0 = NULL;
1789   struct data_reference *dr;
1790   unsigned int i;
1791   bool do_peeling = false;
1792   bool do_versioning = false;
1793   bool stat;
1794   tree stmt;
1795   stmt_vec_info stmt_info;
1796   int vect_versioning_for_alias_required;
1797
1798   if (vect_print_dump_info (REPORT_DETAILS))
1799     fprintf (vect_dump, "=== vect_enhance_data_refs_alignment ===");
1800
1801   /* While cost model enhancements are expected in the future, the high level
1802      view of the code at this time is as follows:
1803
1804      A) If there is a misaligned write then see if peeling to align this write
1805         can make all data references satisfy vect_supportable_dr_alignment.
1806         If so, update data structures as needed and return true.  Note that
1807         at this time vect_supportable_dr_alignment is known to return false
1808         for a misaligned write.
1809
1810      B) If peeling wasn't possible and there is a data reference with an
1811         unknown misalignment that does not satisfy vect_supportable_dr_alignment
1812         then see if loop versioning checks can be used to make all data
1813         references satisfy vect_supportable_dr_alignment.  If so, update
1814         data structures as needed and return true.
1815
1816      C) If neither peeling nor versioning were successful then return false if
1817         any data reference does not satisfy vect_supportable_dr_alignment.
1818
1819      D) Return true (all data references satisfy vect_supportable_dr_alignment).
1820
1821      Note, Possibility 3 above (which is peeling and versioning together) is not
1822      being done at this time.  */
1823
1824   /* (1) Peeling to force alignment.  */
1825
1826   /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1827      Considerations:
1828      + How many accesses will become aligned due to the peeling
1829      - How many accesses will become unaligned due to the peeling,
1830        and the cost of misaligned accesses.
1831      - The cost of peeling (the extra runtime checks, the increase 
1832        in code size).
1833
1834      The scheme we use FORNOW: peel to force the alignment of the first
1835      misaligned store in the loop.
1836      Rationale: misaligned stores are not yet supported.
1837
1838      TODO: Use a cost model.  */
1839
1840   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1841     {
1842       stmt = DR_STMT (dr);
1843       stmt_info = vinfo_for_stmt (stmt);
1844
1845       /* For interleaving, only the alignment of the first access
1846          matters.  */
1847       if (STMT_VINFO_STRIDED_ACCESS (stmt_info)
1848           && DR_GROUP_FIRST_DR (stmt_info) != stmt)
1849         continue;
1850
1851       if (!DR_IS_READ (dr) && !aligned_access_p (dr))
1852         {
1853           do_peeling = vector_alignment_reachable_p (dr);
1854           if (do_peeling)
1855             dr0 = dr;
1856           if (!do_peeling && vect_print_dump_info (REPORT_DETAILS))
1857             fprintf (vect_dump, "vector alignment may not be reachable");
1858           break;
1859         }
1860     }
1861
1862   vect_versioning_for_alias_required =
1863     (VEC_length (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo)) > 0);
1864
1865   /* Temporarily, if versioning for alias is required, we disable peeling
1866      until we support peeling and versioning.  Often peeling for alignment
1867      will require peeling for loop-bound, which in turn requires that we
1868      know how to adjust the loop ivs after the loop.  */
1869   if (vect_versioning_for_alias_required
1870        || !vect_can_advance_ivs_p (loop_vinfo)
1871       || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1872     do_peeling = false;
1873
1874   if (do_peeling)
1875     {
1876       int mis;
1877       int npeel = 0;
1878       tree stmt = DR_STMT (dr0);
1879       stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1880       tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1881       int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1882
1883       if (known_alignment_for_access_p (dr0))
1884         {
1885           /* Since it's known at compile time, compute the number of iterations
1886              in the peeled loop (the peeling factor) for use in updating
1887              DR_MISALIGNMENT values.  The peeling factor is the vectorization
1888              factor minus the misalignment as an element count.  */
1889           mis = DR_MISALIGNMENT (dr0);
1890           mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1891           npeel = nelements - mis;
1892
1893           /* For interleaved data access every iteration accesses all the 
1894              members of the group, therefore we divide the number of iterations
1895              by the group size.  */
1896           stmt_info = vinfo_for_stmt (DR_STMT (dr0));     
1897           if (STMT_VINFO_STRIDED_ACCESS (stmt_info))
1898             npeel /= DR_GROUP_SIZE (stmt_info);
1899
1900           if (vect_print_dump_info (REPORT_DETAILS))
1901             fprintf (vect_dump, "Try peeling by %d", npeel);
1902         }
1903
1904       /* Ensure that all data refs can be vectorized after the peel.  */
1905       for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1906         {
1907           int save_misalignment;
1908
1909           if (dr == dr0)
1910             continue;
1911
1912           stmt = DR_STMT (dr);
1913           stmt_info = vinfo_for_stmt (stmt);
1914           /* For interleaving, only the alignment of the first access
1915             matters.  */
1916           if (STMT_VINFO_STRIDED_ACCESS (stmt_info)
1917               && DR_GROUP_FIRST_DR (stmt_info) != stmt)
1918             continue;
1919
1920           save_misalignment = DR_MISALIGNMENT (dr);
1921           vect_update_misalignment_for_peel (dr, dr0, npeel);
1922           supportable_dr_alignment = vect_supportable_dr_alignment (dr);
1923           SET_DR_MISALIGNMENT (dr, save_misalignment);
1924           
1925           if (!supportable_dr_alignment)
1926             {
1927               do_peeling = false;
1928               break;
1929             }
1930         }
1931
1932       if (do_peeling)
1933         {
1934           /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1935              If the misalignment of DR_i is identical to that of dr0 then set
1936              DR_MISALIGNMENT (DR_i) to zero.  If the misalignment of DR_i and
1937              dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1938              by the peeling factor times the element size of DR_i (MOD the
1939              vectorization factor times the size).  Otherwise, the
1940              misalignment of DR_i must be set to unknown.  */
1941           for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1942             if (dr != dr0)
1943               vect_update_misalignment_for_peel (dr, dr0, npeel);
1944
1945           LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1946           LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) = DR_MISALIGNMENT (dr0);
1947           SET_DR_MISALIGNMENT (dr0, 0);
1948           if (vect_print_dump_info (REPORT_ALIGNMENT))
1949             fprintf (vect_dump, "Alignment of access forced using peeling.");
1950
1951           if (vect_print_dump_info (REPORT_DETAILS))
1952             fprintf (vect_dump, "Peeling for alignment will be applied.");
1953
1954           stat = vect_verify_datarefs_alignment (loop_vinfo);
1955           gcc_assert (stat);
1956           return stat;
1957         }
1958     }
1959
1960
1961   /* (2) Versioning to force alignment.  */
1962
1963   /* Try versioning if:
1964      1) flag_tree_vect_loop_version is TRUE
1965      2) optimize_size is FALSE
1966      3) there is at least one unsupported misaligned data ref with an unknown
1967         misalignment, and
1968      4) all misaligned data refs with a known misalignment are supported, and
1969      5) the number of runtime alignment checks is within reason.  */
1970
1971   do_versioning = 
1972         flag_tree_vect_loop_version 
1973         && (!optimize_size)
1974         && (!loop->inner); /* FORNOW */
1975
1976   if (do_versioning)
1977     {
1978       for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
1979         {
1980           stmt = DR_STMT (dr);
1981           stmt_info = vinfo_for_stmt (stmt);
1982
1983           /* For interleaving, only the alignment of the first access
1984              matters.  */
1985           if (aligned_access_p (dr)
1986               || (STMT_VINFO_STRIDED_ACCESS (stmt_info)
1987                   && DR_GROUP_FIRST_DR (stmt_info) != stmt))
1988             continue;
1989
1990           supportable_dr_alignment = vect_supportable_dr_alignment (dr);
1991
1992           if (!supportable_dr_alignment)
1993             {
1994               tree stmt;
1995               int mask;
1996               tree vectype;
1997
1998               if (known_alignment_for_access_p (dr)
1999                   || VEC_length (tree,
2000                                  LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo))
2001                      >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
2002                 {
2003                   do_versioning = false;
2004                   break;
2005                 }
2006
2007               stmt = DR_STMT (dr);
2008               vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
2009               gcc_assert (vectype);
2010   
2011               /* The rightmost bits of an aligned address must be zeros.
2012                  Construct the mask needed for this test.  For example,
2013                  GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
2014                  mask must be 15 = 0xf. */
2015               mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
2016
2017               /* FORNOW: use the same mask to test all potentially unaligned
2018                  references in the loop.  The vectorizer currently supports
2019                  a single vector size, see the reference to
2020                  GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
2021                  vectorization factor is computed.  */
2022               gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
2023                           || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
2024               LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
2025               VEC_safe_push (tree, heap,
2026                              LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo),
2027                              DR_STMT (dr));
2028             }
2029         }
2030       
2031       /* Versioning requires at least one misaligned data reference.  */
2032       if (VEC_length (tree, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo)) == 0)
2033         do_versioning = false;
2034       else if (!do_versioning)
2035         VEC_truncate (tree, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo), 0);
2036     }
2037
2038   if (do_versioning)
2039     {
2040       VEC(tree,heap) *may_misalign_stmts
2041         = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
2042       tree stmt;
2043
2044       /* It can now be assumed that the data references in the statements
2045          in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
2046          of the loop being vectorized.  */
2047       for (i = 0; VEC_iterate (tree, may_misalign_stmts, i, stmt); i++)
2048         {
2049           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2050           dr = STMT_VINFO_DATA_REF (stmt_info);
2051           SET_DR_MISALIGNMENT (dr, 0);
2052           if (vect_print_dump_info (REPORT_ALIGNMENT))
2053             fprintf (vect_dump, "Alignment of access forced using versioning.");
2054         }
2055
2056       if (vect_print_dump_info (REPORT_DETAILS))
2057         fprintf (vect_dump, "Versioning for alignment will be applied.");
2058
2059       /* Peeling and versioning can't be done together at this time.  */
2060       gcc_assert (! (do_peeling && do_versioning));
2061
2062       stat = vect_verify_datarefs_alignment (loop_vinfo);
2063       gcc_assert (stat);
2064       return stat;
2065     }
2066
2067   /* This point is reached if neither peeling nor versioning is being done.  */
2068   gcc_assert (! (do_peeling || do_versioning));
2069
2070   stat = vect_verify_datarefs_alignment (loop_vinfo);
2071   return stat;
2072 }
2073
2074
2075 /* Function vect_analyze_data_refs_alignment
2076
2077    Analyze the alignment of the data-references in the loop.
2078    Return FALSE if a data reference is found that cannot be vectorized.  */
2079
2080 static bool
2081 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
2082 {
2083   if (vect_print_dump_info (REPORT_DETAILS))
2084     fprintf (vect_dump, "=== vect_analyze_data_refs_alignment ===");
2085
2086   if (!vect_compute_data_refs_alignment (loop_vinfo))
2087     {
2088       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
2089         fprintf (vect_dump, 
2090                  "not vectorized: can't calculate alignment for data ref.");
2091       return false;
2092     }
2093
2094   return true;
2095 }
2096
2097
2098 /* Analyze groups of strided accesses: check that DR belongs to a group of
2099    strided accesses of legal size, step, etc. Detect gaps, single element
2100    interleaving, and other special cases. Set strided access info.
2101    Collect groups of strided stores for further use in SLP analysis.  */
2102
2103 static bool
2104 vect_analyze_group_access (struct data_reference *dr)
2105 {
2106   tree step = DR_STEP (dr);
2107   tree scalar_type = TREE_TYPE (DR_REF (dr));
2108   HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2109   tree stmt = DR_STMT (dr);
2110   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2111   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2112   HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2113   HOST_WIDE_INT stride;
2114   bool slp_impossible = false;
2115
2116   /* For interleaving, STRIDE is STEP counted in elements, i.e., the size of the 
2117      interleaving group (including gaps).  */
2118   stride = dr_step / type_size; 
2119
2120   /* Not consecutive access is possible only if it is a part of interleaving.  */
2121   if (!DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)))
2122     {
2123       /* Check if it this DR is a part of interleaving, and is a single
2124          element of the group that is accessed in the loop.  */
2125       
2126       /* Gaps are supported only for loads. STEP must be a multiple of the type
2127          size.  The size of the group must be a power of 2.  */
2128       if (DR_IS_READ (dr)
2129           && (dr_step % type_size) == 0
2130           && stride > 0
2131           && exact_log2 (stride) != -1)
2132         {
2133           DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)) = stmt;
2134           DR_GROUP_SIZE (vinfo_for_stmt (stmt)) = stride;
2135           if (vect_print_dump_info (REPORT_DR_DETAILS))
2136             {
2137               fprintf (vect_dump, "Detected single element interleaving %d ",
2138                        DR_GROUP_SIZE (vinfo_for_stmt (stmt)));
2139               print_generic_expr (vect_dump, DR_REF (dr), TDF_SLIM);
2140               fprintf (vect_dump, " step ");
2141               print_generic_expr (vect_dump, step, TDF_SLIM);
2142             }
2143           return true;
2144         }
2145       if (vect_print_dump_info (REPORT_DETAILS))
2146         fprintf (vect_dump, "not consecutive access");
2147       return false;
2148     }
2149
2150   if (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)) == stmt)
2151     {
2152       /* First stmt in the interleaving chain. Check the chain.  */
2153       tree next = DR_GROUP_NEXT_DR (vinfo_for_stmt (stmt));
2154       struct data_reference *data_ref = dr;
2155       unsigned int count = 1;
2156       tree next_step;
2157       tree prev_init = DR_INIT (data_ref);
2158       tree prev = stmt;
2159       HOST_WIDE_INT diff, count_in_bytes;
2160
2161       while (next)
2162         {
2163           /* Skip same data-refs. In case that two or more stmts share data-ref
2164              (supported only for loads), we vectorize only the first stmt, and
2165              the rest get their vectorized loads from the first one.  */
2166           if (!tree_int_cst_compare (DR_INIT (data_ref),
2167                                      DR_INIT (STMT_VINFO_DATA_REF (
2168                                                    vinfo_for_stmt (next)))))
2169             {
2170               if (!DR_IS_READ (data_ref))
2171                 {
2172                   if (vect_print_dump_info (REPORT_DETAILS))
2173                     fprintf (vect_dump, "Two store stmts share the same dr.");
2174                   return false;
2175                 }
2176
2177               /* Check that there is no load-store dependencies for this loads
2178                  to prevent a case of load-store-load to the same location.  */
2179               if (DR_GROUP_READ_WRITE_DEPENDENCE (vinfo_for_stmt (next))
2180                   || DR_GROUP_READ_WRITE_DEPENDENCE (vinfo_for_stmt (prev)))
2181                 {
2182                   if (vect_print_dump_info (REPORT_DETAILS))
2183                     fprintf (vect_dump,
2184                              "READ_WRITE dependence in interleaving.");
2185                   return false;
2186                 }
2187
2188               /* For load use the same data-ref load.  */
2189               DR_GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2190
2191               prev = next;
2192               next = DR_GROUP_NEXT_DR (vinfo_for_stmt (next));
2193               continue;
2194             }
2195           prev = next;
2196
2197           /* Check that all the accesses have the same STEP.  */
2198           next_step = DR_STEP (STMT_VINFO_DATA_REF (vinfo_for_stmt (next)));
2199           if (tree_int_cst_compare (step, next_step))
2200             {
2201               if (vect_print_dump_info (REPORT_DETAILS))
2202                 fprintf (vect_dump, "not consecutive access in interleaving");
2203               return false;
2204             }
2205
2206           data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2207           /* Check that the distance between two accesses is equal to the type
2208              size. Otherwise, we have gaps.  */
2209           diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2210                   - TREE_INT_CST_LOW (prev_init)) / type_size;
2211           if (diff != 1)
2212             {
2213               /* FORNOW: SLP of accesses with gaps is not supported.  */
2214               slp_impossible = true;
2215               if (!DR_IS_READ (data_ref))
2216                 {
2217                   if (vect_print_dump_info (REPORT_DETAILS))
2218                     fprintf (vect_dump, "interleaved store with gaps");
2219                   return false;
2220                 }
2221             }
2222
2223           /* Store the gap from the previous member of the group. If there is no
2224              gap in the access, DR_GROUP_GAP is always 1.  */
2225           DR_GROUP_GAP (vinfo_for_stmt (next)) = diff;
2226
2227           prev_init = DR_INIT (data_ref);
2228           next = DR_GROUP_NEXT_DR (vinfo_for_stmt (next));
2229           /* Count the number of data-refs in the chain.  */
2230           count++;
2231         }
2232
2233       /* COUNT is the number of accesses found, we multiply it by the size of
2234          the type to get COUNT_IN_BYTES.  */
2235       count_in_bytes = type_size * count;
2236
2237       /* Check that the size of the interleaving is not greater than STEP.  */
2238       if (dr_step < count_in_bytes)
2239         {
2240           if (vect_print_dump_info (REPORT_DETAILS))
2241             {
2242               fprintf (vect_dump, "interleaving size is greater than step for ");
2243               print_generic_expr (vect_dump, DR_REF (dr), TDF_SLIM);
2244             }
2245           return false;
2246         }
2247
2248       /* Check that the size of the interleaving is equal to STEP for stores,
2249          i.e., that there are no gaps.  */
2250       if (!DR_IS_READ (dr) && dr_step != count_in_bytes)
2251         {
2252           if (vect_print_dump_info (REPORT_DETAILS))
2253             fprintf (vect_dump, "interleaved store with gaps");
2254           return false;
2255         }
2256
2257       /* Check that STEP is a multiple of type size.  */
2258       if ((dr_step % type_size) != 0)
2259         {
2260           if (vect_print_dump_info (REPORT_DETAILS))
2261             {
2262               fprintf (vect_dump, "step is not a multiple of type size: step ");
2263               print_generic_expr (vect_dump, step, TDF_SLIM);
2264               fprintf (vect_dump, " size ");
2265               print_generic_expr (vect_dump, TYPE_SIZE_UNIT (scalar_type),
2266                                   TDF_SLIM);
2267             }
2268           return false;
2269         }
2270
2271       /* FORNOW: we handle only interleaving that is a power of 2.  
2272          We don't fail here if it may be still possible to vectorize the
2273          group using SLP. If not, the size of the group will be checked in
2274          vect_analyze_operations, and the vectorization will fail.  */
2275       if (exact_log2 (stride) == -1)
2276         {
2277           if (vect_print_dump_info (REPORT_DETAILS))
2278             fprintf (vect_dump, "interleaving is not a power of 2");
2279
2280           if (slp_impossible)
2281             return false;
2282         }
2283       DR_GROUP_SIZE (vinfo_for_stmt (stmt)) = stride;
2284       if (vect_print_dump_info (REPORT_DETAILS))
2285         fprintf (vect_dump, "Detected interleaving of size %d", (int)stride);
2286
2287       /* SLP: create an SLP data structure for every interleaving group of 
2288          stores for further analysis in vect_analyse_slp.  */
2289       if (!DR_IS_READ (dr) && !slp_impossible)
2290         VEC_safe_push (tree, heap, LOOP_VINFO_STRIDED_STORES (loop_vinfo), stmt);
2291     }
2292
2293   return true;
2294 }
2295
2296
2297 /* Analyze the access pattern of the data-reference DR.
2298    In case of non-consecutive accesses call vect_analyze_group_access() to
2299    analyze groups of strided accesses.  */
2300
2301 static bool
2302 vect_analyze_data_ref_access (struct data_reference *dr)
2303 {
2304   tree step = DR_STEP (dr);
2305   tree scalar_type = TREE_TYPE (DR_REF (dr));
2306   tree stmt = DR_STMT (dr);
2307   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2308   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2309   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2310   HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2311
2312   if (!step)
2313     {
2314       if (vect_print_dump_info (REPORT_DETAILS))
2315         fprintf (vect_dump, "bad data-ref access");
2316       return false;
2317     }
2318
2319   /* Don't allow invariant accesses.  */
2320   if (dr_step == 0)
2321     return false; 
2322
2323   if (nested_in_vect_loop_p (loop, stmt))
2324     {
2325       /* Interleaved accesses are not yet supported within outer-loop
2326         vectorization for references in the inner-loop.  */
2327       DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)) = NULL_TREE;
2328
2329       /* For the rest of the analysis we use the outer-loop step.  */
2330       step = STMT_VINFO_DR_STEP (stmt_info);
2331       dr_step = TREE_INT_CST_LOW (step);
2332       
2333       if (dr_step == 0)
2334         {
2335           if (vect_print_dump_info (REPORT_ALIGNMENT))
2336             fprintf (vect_dump, "zero step in outer loop.");
2337           if (DR_IS_READ (dr))
2338             return true; 
2339           else
2340             return false;
2341         }
2342     }
2343
2344   /* Consecutive?  */
2345   if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type)))
2346     {
2347       /* Mark that it is not interleaving.  */
2348       DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)) = NULL_TREE;
2349       return true;
2350     }
2351
2352   if (nested_in_vect_loop_p (loop, stmt))
2353     {
2354       if (vect_print_dump_info (REPORT_ALIGNMENT))
2355         fprintf (vect_dump, "strided access in outer loop.");
2356       return false;
2357     }
2358
2359   /* Not consecutive access - check if it's a part of interleaving group.  */
2360   return vect_analyze_group_access (dr);
2361 }
2362
2363
2364 /* Function vect_analyze_data_ref_accesses.
2365
2366    Analyze the access pattern of all the data references in the loop.
2367
2368    FORNOW: the only access pattern that is considered vectorizable is a
2369            simple step 1 (consecutive) access.
2370
2371    FORNOW: handle only arrays and pointer accesses.  */
2372
2373 static bool
2374 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo)
2375 {
2376   unsigned int i;
2377   VEC (data_reference_p, heap) *datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2378   struct data_reference *dr;
2379
2380   if (vect_print_dump_info (REPORT_DETAILS))
2381     fprintf (vect_dump, "=== vect_analyze_data_ref_accesses ===");
2382
2383   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
2384     if (!vect_analyze_data_ref_access (dr))
2385       {
2386         if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
2387           fprintf (vect_dump, "not vectorized: complicated access pattern.");
2388         return false;
2389       }
2390
2391   return true;
2392 }
2393
2394 /* Function vect_prune_runtime_alias_test_list.
2395
2396    Prune a list of ddrs to be tested at run-time by versioning for alias.
2397    Return FALSE if resulting list of ddrs is longer then allowed by
2398    PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE.  */
2399
2400 static bool
2401 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2402 {
2403   VEC (ddr_p, heap) * ddrs =
2404     LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2405   unsigned i, j;
2406
2407   if (vect_print_dump_info (REPORT_DETAILS))
2408     fprintf (vect_dump, "=== vect_prune_runtime_alias_test_list ===");
2409
2410   for (i = 0; i < VEC_length (ddr_p, ddrs); )
2411     {
2412       bool found;
2413       ddr_p ddr_i;
2414
2415       ddr_i = VEC_index (ddr_p, ddrs, i);
2416       found = false;
2417
2418       for (j = 0; j < i; j++)
2419         {
2420           ddr_p ddr_j = VEC_index (ddr_p, ddrs, j);
2421
2422           if (vect_vfa_range_equal (ddr_i, ddr_j))
2423             {
2424               if (vect_print_dump_info (REPORT_DR_DETAILS))
2425                 {
2426                   fprintf (vect_dump, "found equal ranges ");
2427                   print_generic_expr (vect_dump, DR_REF (DDR_A (ddr_i)), TDF_SLIM);
2428                   fprintf (vect_dump, ", ");
2429                   print_generic_expr (vect_dump, DR_REF (DDR_B (ddr_i)), TDF_SLIM);
2430                   fprintf (vect_dump, " and ");
2431                   print_generic_expr (vect_dump, DR_REF (DDR_A (ddr_j)), TDF_SLIM);
2432                   fprintf (vect_dump, ", ");
2433                   print_generic_expr (vect_dump, DR_REF (DDR_B (ddr_j)), TDF_SLIM);
2434                 }
2435               found = true;
2436               break;
2437             }
2438         }
2439       
2440       if (found)
2441       {
2442         VEC_ordered_remove (ddr_p, ddrs, i);
2443         continue;
2444       }
2445       i++;
2446     }
2447
2448   if (VEC_length (ddr_p, ddrs) >
2449        (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2450     {
2451       if (vect_print_dump_info (REPORT_DR_DETAILS))
2452         {
2453           fprintf (vect_dump,
2454                    "disable versioning for alias - max number of generated "
2455                    "checks exceeded.");
2456         }
2457
2458       VEC_truncate (ddr_p, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo), 0);
2459
2460       return false;
2461     }
2462
2463   return true;
2464 }
2465
2466 /* Recursively free the memory allocated for the SLP tree rooted at NODE.  */
2467
2468 void
2469 vect_free_slp_tree (slp_tree node)
2470 {
2471   if (!node)
2472     return;
2473
2474   if (SLP_TREE_LEFT (node))
2475     vect_free_slp_tree (SLP_TREE_LEFT (node));
2476    
2477   if (SLP_TREE_RIGHT (node))
2478     vect_free_slp_tree (SLP_TREE_RIGHT (node));
2479    
2480   VEC_free (tree, heap, SLP_TREE_SCALAR_STMTS (node));
2481   
2482   if (SLP_TREE_VEC_STMTS (node))
2483     VEC_free (tree, heap, SLP_TREE_VEC_STMTS (node));
2484
2485   free (node);
2486 }
2487
2488
2489 /* Get the defs for the RHS (collect them in DEF_STMTS0/1), check that they are 
2490    of a legal type and that they match the defs of the first stmt of the SLP 
2491    group (stored in FIRST_STMT_...).  */
2492
2493 static bool
2494 vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, slp_tree slp_node,
2495                              tree rhs, VEC (tree, heap) **def_stmts0,
2496                              VEC (tree, heap) **def_stmts1,
2497                              enum vect_def_type *first_stmt_dt0,
2498                              enum vect_def_type *first_stmt_dt1,
2499                              tree *first_stmt_def0_type, 
2500                              tree *first_stmt_def1_type,
2501                              tree *first_stmt_const_oprnd,
2502                              int ncopies_for_cost)
2503 {
2504   tree oprnd;
2505   enum operation_type op_type = TREE_OPERAND_LENGTH (rhs);
2506   unsigned int i, number_of_oprnds = op_type;
2507   tree def, def_stmt;
2508   enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
2509   stmt_vec_info stmt_info = 
2510     vinfo_for_stmt (VEC_index (tree, SLP_TREE_SCALAR_STMTS (slp_node), 0));
2511
2512   /* Store.  */
2513   if (!op_type)
2514     number_of_oprnds = 1;
2515   else
2516     gcc_assert (op_type == unary_op || op_type == binary_op);
2517
2518   for (i = 0; i < number_of_oprnds; i++)
2519     {
2520       if (op_type)
2521         oprnd = TREE_OPERAND (rhs, i);
2522       else
2523         oprnd = rhs;
2524
2525       if (!vect_is_simple_use (oprnd, loop_vinfo, &def_stmt, &def, &dt[i])
2526           || (!def_stmt && dt[i] != vect_constant_def))
2527         {
2528           if (vect_print_dump_info (REPORT_SLP)) 
2529             {
2530               fprintf (vect_dump, "Build SLP failed: can't find def for ");
2531               print_generic_expr (vect_dump, oprnd, TDF_SLIM);
2532             }
2533
2534           return false;
2535         }
2536
2537       if (!*first_stmt_dt0)
2538         {
2539           /* op0 of the first stmt of the group - store its info.  */
2540           *first_stmt_dt0 = dt[i];
2541           if (def)
2542             *first_stmt_def0_type = TREE_TYPE (def);
2543           else
2544             *first_stmt_const_oprnd = oprnd;
2545
2546           /* Analyze costs (for the first stmt of the group only).  */
2547           if (op_type)
2548             /* Not memory operation (we don't call this functions for loads).  */
2549             vect_model_simple_cost (stmt_info, ncopies_for_cost, dt, slp_node);
2550           else
2551             /* Store.  */
2552             vect_model_store_cost (stmt_info, ncopies_for_cost, dt[0], slp_node);
2553         }
2554       
2555       else
2556         {
2557           if (!*first_stmt_dt1 && i == 1)
2558             {
2559               /* op1 of the first stmt of the group - store its info.  */
2560               *first_stmt_dt1 = dt[i];
2561               if (def)
2562                 *first_stmt_def1_type = TREE_TYPE (def);
2563               else
2564                 {
2565                   /* We assume that the stmt contains only one constant 
2566                      operand. We fail otherwise, to be on the safe side.  */
2567                   if (*first_stmt_const_oprnd)
2568                     {
2569                       if (vect_print_dump_info (REPORT_SLP)) 
2570                         fprintf (vect_dump, "Build SLP failed: two constant "
2571                                  "oprnds in stmt");                 
2572                       return false;
2573                     }
2574                   *first_stmt_const_oprnd = oprnd;
2575                 }
2576             }
2577           else
2578             {
2579               /* Not first stmt of the group, check that the def-stmt/s match 
2580                  the def-stmt/s of the first stmt.  */
2581               if ((i == 0 
2582                    && (*first_stmt_dt0 != dt[i]
2583                        || (*first_stmt_def0_type && def
2584                            && *first_stmt_def0_type != TREE_TYPE (def))))
2585                   || (i == 1 
2586                       && (*first_stmt_dt1 != dt[i]
2587                           || (*first_stmt_def1_type && def
2588                               && *first_stmt_def1_type != TREE_TYPE (def))))              
2589                   || (!def 
2590                       && TREE_TYPE (*first_stmt_const_oprnd) 
2591                       != TREE_TYPE (oprnd)))
2592                 { 
2593                   if (vect_print_dump_info (REPORT_SLP)) 
2594                     fprintf (vect_dump, "Build SLP failed: different types ");
2595                   
2596                   return false;
2597                 }
2598             }
2599         }
2600
2601       /* Check the types of the definitions.  */
2602       switch (dt[i])
2603         {
2604         case vect_constant_def:
2605         case vect_invariant_def:
2606           break;
2607           
2608         case vect_loop_def:
2609           if (i == 0)
2610             VEC_safe_push (tree, heap, *def_stmts0, def_stmt);
2611           else
2612             VEC_safe_push (tree, heap, *def_stmts1, def_stmt);
2613           break;
2614
2615         default:
2616           /* FORNOW: Not supported.  */
2617           if (vect_print_dump_info (REPORT_SLP)) 
2618             {
2619               fprintf (vect_dump, "Build SLP failed: illegal type of def ");
2620               print_generic_expr (vect_dump, def, TDF_SLIM);
2621             }
2622
2623           return false;
2624         }
2625     }
2626
2627   return true;
2628 }
2629
2630
2631 /* Recursively build an SLP tree starting from NODE.
2632    Fail (and return FALSE) if def-stmts are not isomorphic, require data 
2633    permutation or are of unsupported types of operation. Otherwise, return 
2634    TRUE.
2635    SLP_IMPOSSIBLE is TRUE if it is impossible to SLP in the loop, for example
2636    in the case of multiple types for now.  */
2637
2638 static bool
2639 vect_build_slp_tree (loop_vec_info loop_vinfo, slp_tree *node, 
2640                      unsigned int group_size, bool *slp_impossible,
2641                      int *inside_cost, int *outside_cost,
2642                      int ncopies_for_cost)
2643 {
2644   VEC (tree, heap) *def_stmts0 = VEC_alloc (tree, heap, group_size);
2645   VEC (tree, heap) *def_stmts1 =  VEC_alloc (tree, heap, group_size);
2646   unsigned int i;
2647   VEC (tree, heap) *stmts = SLP_TREE_SCALAR_STMTS (*node);
2648   tree stmt = VEC_index (tree, stmts, 0);
2649   enum vect_def_type first_stmt_dt0 = 0, first_stmt_dt1 = 0;
2650   enum tree_code first_stmt_code = 0;
2651   tree first_stmt_def1_type = NULL_TREE, first_stmt_def0_type = NULL_TREE;
2652   tree lhs, rhs, prev_stmt = NULL_TREE;
2653   bool stop_recursion = false, need_same_oprnds = false;
2654   tree vectype, scalar_type, first_op1 = NULL_TREE;
2655   unsigned int vectorization_factor = 0, ncopies;
2656   optab optab;
2657   int icode;
2658   enum machine_mode optab_op2_mode;
2659   enum machine_mode vec_mode;
2660   tree first_stmt_const_oprnd = NULL_TREE;
2661   struct data_reference *first_dr;
2662  
2663   /* For every stmt in NODE find its def stmt/s.  */
2664   for (i = 0; VEC_iterate (tree, stmts, i, stmt); i++)
2665     {
2666       if (vect_print_dump_info (REPORT_SLP)) 
2667         {
2668           fprintf (vect_dump, "Build SLP for ");
2669           print_generic_expr (vect_dump, stmt, TDF_SLIM);
2670         }
2671
2672       if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
2673         {
2674           if (vect_print_dump_info (REPORT_SLP)) 
2675             {
2676               fprintf (vect_dump, "Build SLP failed: not MODIFY_STMT ");
2677               print_generic_expr (vect_dump, stmt, TDF_SLIM);
2678             }
2679           
2680           return false;
2681         }
2682
2683       scalar_type = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0));
2684       vectype = get_vectype_for_scalar_type (scalar_type);
2685       gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
2686       vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2687       ncopies = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
2688       if (ncopies > 1)
2689         {
2690           /* FORNOW.  */
2691           if (vect_print_dump_info (REPORT_SLP)) 
2692             fprintf (vect_dump, "SLP failed - multiple types ");
2693           
2694           *slp_impossible = true;
2695           return false;
2696         }
2697
2698       lhs = GIMPLE_STMT_OPERAND (stmt, 0);
2699       rhs = GIMPLE_STMT_OPERAND (stmt, 1);
2700
2701       /* Check the operation.  */
2702       if (i == 0)
2703         {
2704           first_stmt_code = TREE_CODE (rhs);
2705
2706           /* Shift arguments should be equal in all the packed stmts for a 
2707              vector shift with scalar shift operand.  */
2708           if (TREE_CODE (rhs) == LSHIFT_EXPR || TREE_CODE (rhs) == RSHIFT_EXPR)
2709             {
2710               vec_mode = TYPE_MODE (vectype);
2711               optab = optab_for_tree_code (TREE_CODE (rhs), vectype);
2712               if (!optab)
2713                 {
2714                   if (vect_print_dump_info (REPORT_SLP))
2715                     fprintf (vect_dump, "Build SLP failed: no optab.");
2716                   return false;
2717                 }
2718               icode = (int) optab->handlers[(int) vec_mode].insn_code;
2719               if (icode == CODE_FOR_nothing)
2720                 {
2721                   if (vect_print_dump_info (REPORT_SLP))
2722                     fprintf (vect_dump,
2723                              "Build SLP failed: op not supported by target.");
2724                   return false;
2725                 }
2726               optab_op2_mode = insn_data[icode].operand[2].mode;
2727               if (!VECTOR_MODE_P (optab_op2_mode))
2728                 {
2729                   need_same_oprnds = true;
2730                   first_op1 = TREE_OPERAND (rhs, 1);
2731                 }
2732             }
2733         }
2734       else
2735         {
2736           if (first_stmt_code != TREE_CODE (rhs))
2737             {
2738               if (vect_print_dump_info (REPORT_SLP)) 
2739                 {
2740                   fprintf (vect_dump, 
2741                            "Build SLP failed: different operation in stmt ");
2742                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
2743                 }
2744               
2745               return false;
2746             }
2747           
2748           if (need_same_oprnds 
2749               && !operand_equal_p (first_op1, TREE_OPERAND (rhs, 1), 0))
2750             {
2751               if (vect_print_dump_info (REPORT_SLP)) 
2752                 {
2753                   fprintf (vect_dump, 
2754                            "Build SLP failed: different shift arguments in ");
2755                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
2756                 }
2757               
2758               return false;
2759             }
2760         }
2761
2762       /* Strided store or load.  */
2763       if (STMT_VINFO_STRIDED_ACCESS (vinfo_for_stmt (stmt)))
2764         {
2765           if (REFERENCE_CLASS_P (lhs))
2766             {
2767               /* Store.  */
2768               if (!vect_get_and_check_slp_defs (loop_vinfo, *node, rhs, 
2769                                                 &def_stmts0, &def_stmts1, 
2770                                                 &first_stmt_dt0, 
2771                                                 &first_stmt_dt1, 
2772                                                 &first_stmt_def0_type, 
2773                                                 &first_stmt_def1_type,
2774                                                 &first_stmt_const_oprnd,
2775                                                 ncopies_for_cost))
2776                 return false;
2777             }
2778             else
2779               {
2780                 /* Load.  */
2781                 if (i == 0)
2782                   {
2783                     /* First stmt of the SLP group should be the first load of 
2784                        the interleaving loop if data permutation is not 
2785                        allowed.  */
2786                     if  (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt)) != stmt) 
2787                       {
2788                         /* FORNOW: data permutations are not supported.  */
2789                         if (vect_print_dump_info (REPORT_SLP)) 
2790                           {
2791                             fprintf (vect_dump, "Build SLP failed: strided "
2792                                      " loads need permutation ");
2793                             print_generic_expr (vect_dump, stmt, TDF_SLIM);
2794                           }
2795
2796                         return false;
2797                       }
2798
2799                     first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
2800                     if (vect_supportable_dr_alignment (first_dr)
2801                         == dr_unaligned_unsupported)
2802                       {
2803                         if (vect_print_dump_info (REPORT_SLP)) 
2804                           {
2805                             fprintf (vect_dump, "Build SLP failed: unsupported "
2806                                      " unaligned load ");
2807                             print_generic_expr (vect_dump, stmt, TDF_SLIM);
2808                           }
2809
2810                         return false;
2811                       }
2812
2813                     /* Analyze costs (for the first stmt in the group).  */
2814                     vect_model_load_cost (vinfo_for_stmt (stmt), 
2815                                           ncopies_for_cost, *node);
2816                   }
2817                 else
2818                   {
2819                     if (DR_GROUP_NEXT_DR (vinfo_for_stmt (prev_stmt)) != stmt)
2820                       {
2821                         /* FORNOW: data permutations are not supported.  */
2822                         if (vect_print_dump_info (REPORT_SLP)) 
2823                           {
2824                             fprintf (vect_dump, "Build SLP failed: strided "
2825                                      " loads need permutation ");
2826                             print_generic_expr (vect_dump, stmt, TDF_SLIM);
2827                           }
2828                         return false;
2829                       }
2830                   }
2831
2832                 prev_stmt = stmt;
2833
2834                 /* We stop the tree when we reach a group of loads.  */
2835                 stop_recursion = true;
2836                 continue;
2837               }
2838         } /* Strided access.  */
2839       else
2840         {
2841           if (REFERENCE_CLASS_P (rhs))
2842             {
2843               /* Not strided load. */
2844               if (vect_print_dump_info (REPORT_SLP)) 
2845                 {
2846                   fprintf (vect_dump, "Build SLP failed: not strided load ");
2847                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
2848                 }
2849
2850               /* FORNOW: Not strided loads are not supported.  */
2851               return false;
2852             }
2853
2854           /* Not memory operation.  */
2855           if (!BINARY_CLASS_P (rhs) && !UNARY_CLASS_P (rhs))
2856             {
2857               if (vect_print_dump_info (REPORT_SLP)) 
2858                 {
2859                   fprintf (vect_dump, "Build SLP failed: operation");
2860                   fprintf (vect_dump, " unsupported ");
2861                   print_generic_expr (vect_dump, stmt, TDF_SLIM);
2862                 }
2863
2864               return false;
2865             }
2866
2867           /* Find the def-stmts.  */ 
2868           if (!vect_get_and_check_slp_defs (loop_vinfo, *node, rhs, &def_stmts0, 
2869                                             &def_stmts1, &first_stmt_dt0, 
2870                                             &first_stmt_dt1, 
2871                                             &first_stmt_def0_type, 
2872                                             &first_stmt_def1_type,
2873                                             &first_stmt_const_oprnd,
2874                                             ncopies_for_cost))
2875             return false;
2876         }
2877     }
2878
2879   /* Add the costs of the node to the overall instance costs.  */
2880   *inside_cost += SLP_TREE_INSIDE_OF_LOOP_COST (*node); 
2881   *outside_cost += SLP_TREE_OUTSIDE_OF_LOOP_COST (*node);
2882
2883   /* Strided loads were reached - stop the recursion.  */
2884   if (stop_recursion)
2885     return true;
2886
2887   /* Create SLP_TREE nodes for the definition node/s.  */ 
2888   if (first_stmt_dt0 == vect_loop_def)
2889     {
2890       slp_tree left_node = XNEW (struct _slp_tree);
2891       SLP_TREE_SCALAR_STMTS (left_node) = def_stmts0;
2892       SLP_TREE_VEC_STMTS (left_node) = NULL;
2893       SLP_TREE_LEFT (left_node) = NULL;
2894       SLP_TREE_RIGHT (left_node) = NULL;
2895       SLP_TREE_OUTSIDE_OF_LOOP_COST (left_node) = 0;
2896       SLP_TREE_INSIDE_OF_LOOP_COST (left_node) = 0;
2897       if (!vect_build_slp_tree (loop_vinfo, &left_node, group_size, 
2898                                 slp_impossible, inside_cost, outside_cost,
2899                                 ncopies_for_cost))
2900         return false;
2901       
2902       SLP_TREE_LEFT (*node) = left_node;
2903     }
2904
2905   if (first_stmt_dt1 == vect_loop_def)
2906     {
2907       slp_tree right_node = XNEW (struct _slp_tree);
2908       SLP_TREE_SCALAR_STMTS (right_node) = def_stmts1;
2909       SLP_TREE_VEC_STMTS (right_node) = NULL;
2910       SLP_TREE_LEFT (right_node) = NULL;
2911       SLP_TREE_RIGHT (right_node) = NULL;
2912       SLP_TREE_OUTSIDE_OF_LOOP_COST (right_node) = 0;
2913       SLP_TREE_INSIDE_OF_LOOP_COST (right_node) = 0;
2914       if (!vect_build_slp_tree (loop_vinfo, &right_node, group_size,
2915                                 slp_impossible, inside_cost, outside_cost,
2916                                 ncopies_for_cost))
2917         return false;
2918       
2919       SLP_TREE_RIGHT (*node) = right_node;
2920     }
2921
2922   return true;
2923 }
2924
2925
2926 static void
2927 vect_print_slp_tree (slp_tree node)
2928 {
2929   int i;
2930   tree stmt;
2931
2932   if (!node)
2933     return;
2934
2935   fprintf (vect_dump, "node ");
2936   for (i = 0; VEC_iterate (tree, SLP_TREE_SCALAR_STMTS (node), i, stmt); i++)
2937     {
2938       fprintf (vect_dump, "\n\tstmt %d ", i);
2939       print_generic_expr (vect_dump, stmt, TDF_SLIM);  
2940     }
2941   fprintf (vect_dump, "\n");
2942
2943   vect_print_slp_tree (SLP_TREE_LEFT (node));
2944   vect_print_slp_tree (SLP_TREE_RIGHT (node));
2945 }
2946
2947
2948 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID). 
2949    If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index 
2950    J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the 
2951    stmts in NODE are to be marked.  */
2952
2953 static void
2954 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
2955 {
2956   int i;
2957   tree stmt;
2958
2959   if (!node)
2960     return;
2961
2962   for (i = 0; VEC_iterate (tree, SLP_TREE_SCALAR_STMTS (node), i, stmt); i++)
2963     if (j < 0 || i == j)
2964       STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
2965
2966   vect_mark_slp_stmts (SLP_TREE_LEFT (node), mark, j);
2967   vect_mark_slp_stmts (SLP_TREE_RIGHT (node), mark, j);
2968 }
2969
2970
2971 /* Analyze an SLP instance starting from a group of strided stores. Call
2972    vect_build_slp_tree to build a tree of packed stmts if possible. 
2973    Return FALSE if it's impossible to SLP any stmt in the loop.  */
2974
2975 static bool
2976 vect_analyze_slp_instance (loop_vec_info loop_vinfo, tree stmt)
2977 {
2978   slp_instance new_instance;
2979   slp_tree node = XNEW (struct _slp_tree);
2980   unsigned int group_size = DR_GROUP_SIZE (vinfo_for_stmt (stmt));
2981   unsigned int unrolling_factor = 1, nunits;
2982   tree vectype, scalar_type, next;
2983   unsigned int vectorization_factor = 0, ncopies;
2984   bool slp_impossible = false; 
2985   int inside_cost = 0, outside_cost = 0, ncopies_for_cost;
2986
2987   /* FORNOW: multiple types are not supported.  */
2988   scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))));
2989   vectype = get_vectype_for_scalar_type (scalar_type);
2990   nunits = TYPE_VECTOR_SUBPARTS (vectype);
2991   vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2992   ncopies = vectorization_factor / nunits;
2993   if (ncopies > 1)
2994     {
2995       if (vect_print_dump_info (REPORT_SLP)) 
2996           fprintf (vect_dump, "SLP failed - multiple types ");
2997
2998       return false;
2999     }
3000
3001   /* Create a node (a root of the SLP tree) for the packed strided stores.  */ 
3002   SLP_TREE_SCALAR_STMTS (node) = VEC_alloc (tree, heap, group_size);
3003   next = stmt;
3004   /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS.  */
3005   while (next)
3006     {
3007       VEC_safe_push (tree, heap, SLP_TREE_SCALAR_STMTS (node), next);
3008       next = DR_GROUP_NEXT_DR (vinfo_for_stmt (next));
3009     }
3010
3011   SLP_TREE_VEC_STMTS (node) = NULL;
3012   SLP_TREE_NUMBER_OF_VEC_STMTS (node) = 0;
3013   SLP_TREE_LEFT (node) = NULL;
3014   SLP_TREE_RIGHT (node) = NULL;
3015   SLP_TREE_OUTSIDE_OF_LOOP_COST (node) = 0;
3016   SLP_TREE_INSIDE_OF_LOOP_COST (node) = 0;
3017
3018   /* Calculate the unrolling factor.  */
3019   unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
3020         
3021   /* Calculate the number of vector stmts to create based on the unrolling
3022      factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
3023      GROUP_SIZE / NUNITS otherwise.  */
3024   ncopies_for_cost = unrolling_factor * group_size / nunits;
3025
3026   /* Build the tree for the SLP instance.  */
3027   if (vect_build_slp_tree (loop_vinfo, &node, group_size, &slp_impossible,
3028                            &inside_cost, &outside_cost, ncopies_for_cost))
3029     {
3030       /* Create a new SLP instance.  */  
3031       new_instance = XNEW (struct _slp_instance);
3032       SLP_INSTANCE_TREE (new_instance) = node;
3033       SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
3034       SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
3035       SLP_INSTANCE_OUTSIDE_OF_LOOP_COST (new_instance) = outside_cost;
3036       SLP_INSTANCE_INSIDE_OF_LOOP_COST (new_instance) = inside_cost;
3037       VEC_safe_push (slp_instance, heap, LOOP_VINFO_SLP_INSTANCES (loop_vinfo), 
3038                      new_instance);
3039       if (vect_print_dump_info (REPORT_SLP))
3040         vect_print_slp_tree (node);
3041
3042       return true;
3043     }
3044
3045   /* Failed to SLP.  */
3046   /* Free the allocated memory.  */
3047   vect_free_slp_tree (node);
3048
3049   if (slp_impossible)
3050     return false;
3051
3052   /* SLP failed for this instance, but it is still possible to SLP other stmts 
3053      in the loop.  */
3054   return true;
3055 }
3056
3057
3058 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
3059    trees of packed scalar stmts if SLP is possible.  */
3060
3061 static bool
3062 vect_analyze_slp (loop_vec_info loop_vinfo)
3063 {
3064   unsigned int i;
3065   VEC (tree, heap) *strided_stores = LOOP_VINFO_STRIDED_STORES (loop_vinfo);
3066   tree store;
3067
3068   if (vect_print_dump_info (REPORT_SLP))
3069     fprintf (vect_dump, "=== vect_analyze_slp ===");
3070
3071   for (i = 0; VEC_iterate (tree, strided_stores, i, store); i++)
3072     if (!vect_analyze_slp_instance (loop_vinfo, store))
3073       {
3074         /* SLP failed. No instance can be SLPed in the loop.  */
3075         if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))   
3076           fprintf (vect_dump, "SLP failed.");
3077
3078         return false;
3079       }
3080
3081   return true;
3082 }
3083
3084
3085 /* For each possible SLP instance decide whether to SLP it and calculate overall
3086    unrolling factor needed to SLP the loop.  */
3087
3088 static void
3089 vect_make_slp_decision (loop_vec_info loop_vinfo)
3090 {
3091   unsigned int i, unrolling_factor = 1;
3092   VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3093   slp_instance instance;
3094   int decided_to_slp = 0;
3095
3096   if (vect_print_dump_info (REPORT_SLP))
3097     fprintf (vect_dump, "=== vect_make_slp_decision ===");
3098
3099   for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); i++)
3100     {
3101       /* FORNOW: SLP if you can.  */
3102       if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
3103         unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
3104
3105       /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we 
3106          call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and 
3107          loop-based vectorization. Such stmts will be marked as HYBRID.  */
3108       vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
3109       decided_to_slp++;
3110     }
3111
3112   LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
3113
3114   if (decided_to_slp && vect_print_dump_info (REPORT_SLP)) 
3115     fprintf (vect_dump, "Decided to SLP %d instances. Unrolling factor %d", 
3116              decided_to_slp, unrolling_factor);
3117 }
3118
3119
3120 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
3121    can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID.  */
3122
3123 static void
3124 vect_detect_hybrid_slp_stmts (slp_tree node)
3125 {
3126   int i;
3127   tree stmt;
3128   imm_use_iterator imm_iter;
3129   tree use_stmt;
3130
3131   if (!node)
3132     return;
3133
3134   for (i = 0; VEC_iterate (tree, SLP_TREE_SCALAR_STMTS (node), i, stmt); i++)
3135     if (PURE_SLP_STMT (vinfo_for_stmt (stmt))
3136         && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
3137       FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, GIMPLE_STMT_OPERAND (stmt, 0))
3138         if (vinfo_for_stmt (use_stmt)
3139             && !STMT_SLP_TYPE (vinfo_for_stmt (use_stmt)))
3140           vect_mark_slp_stmts (node, hybrid, i);
3141
3142   vect_detect_hybrid_slp_stmts (SLP_TREE_LEFT (node));
3143   vect_detect_hybrid_slp_stmts (SLP_TREE_RIGHT (node));
3144 }
3145
3146
3147 /* Find stmts that must be both vectorized and SLPed.  */
3148
3149 static void
3150 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
3151 {
3152   unsigned int i;
3153   VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3154   slp_instance instance;
3155
3156   if (vect_print_dump_info (REPORT_SLP))
3157     fprintf (vect_dump, "=== vect_detect_hybrid_slp ===");
3158
3159   for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); i++)
3160     vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance));
3161 }
3162
3163
3164 /* Function vect_analyze_data_refs.
3165
3166   Find all the data references in the loop.
3167
3168    The general structure of the analysis of data refs in the vectorizer is as
3169    follows:
3170    1- vect_analyze_data_refs(loop): call compute_data_dependences_for_loop to
3171       find and analyze all data-refs in the loop and their dependences.
3172    2- vect_analyze_dependences(): apply dependence testing using ddrs.
3173    3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3174    4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3175
3176 */
3177
3178 static bool
3179 vect_analyze_data_refs (loop_vec_info loop_vinfo)  
3180 {
3181   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3182   unsigned int i;
3183   VEC (data_reference_p, heap) *datarefs;
3184   struct data_reference *dr;
3185   tree scalar_type;
3186
3187   if (vect_print_dump_info (REPORT_DETAILS))
3188     fprintf (vect_dump, "=== vect_analyze_data_refs ===\n");
3189
3190   compute_data_dependences_for_loop (loop, true,
3191                                      &LOOP_VINFO_DATAREFS (loop_vinfo),
3192                                      &LOOP_VINFO_DDRS (loop_vinfo));
3193
3194   /* Go through the data-refs, check that the analysis succeeded. Update pointer
3195      from stmt_vec_info struct to DR and vectype.  */
3196   datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3197
3198   for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
3199     {
3200       tree stmt;
3201       stmt_vec_info stmt_info;
3202       basic_block bb;
3203       tree base, offset, init;  
3204    
3205       if (!dr || !DR_REF (dr))
3206         {
3207           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3208             fprintf (vect_dump, "not vectorized: unhandled data-ref ");
3209           return false;
3210         }
3211
3212       stmt = DR_STMT (dr);
3213       stmt_info = vinfo_for_stmt (stmt);
3214
3215       /* Check that analysis of the data-ref succeeded.  */
3216       if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3217           || !DR_STEP (dr))
3218         {
3219           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3220             {
3221               fprintf (vect_dump, "not vectorized: data ref analysis failed ");
3222               print_generic_expr (vect_dump, stmt, TDF_SLIM);
3223             }
3224           return false;
3225         }
3226
3227       if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3228         {
3229           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3230             fprintf (vect_dump, "not vectorized: base addr of dr is a "
3231                      "constant");
3232           return false;
3233         }
3234
3235       if (!DR_SYMBOL_TAG (dr))
3236         {
3237           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3238             {
3239               fprintf (vect_dump, "not vectorized: no memory tag for ");
3240               print_generic_expr (vect_dump, DR_REF (dr), TDF_SLIM);
3241             }
3242           return false;
3243         }
3244
3245       base = unshare_expr (DR_BASE_ADDRESS (dr));
3246       offset = unshare_expr (DR_OFFSET (dr));
3247       init = unshare_expr (DR_INIT (dr));
3248         
3249       /* Update DR field in stmt_vec_info struct.  */
3250       bb = bb_for_stmt (stmt);
3251
3252       /* If the dataref is in an inner-loop of the loop that is considered for
3253          for vectorization, we also want to analyze the access relative to
3254          the outer-loop (DR contains information only relative to the 
3255          inner-most enclosing loop).  We do that by building a reference to the
3256          first location accessed by the inner-loop, and analyze it relative to
3257          the outer-loop.  */    
3258       if (nested_in_vect_loop_p (loop, stmt)) 
3259         {
3260           tree outer_step, outer_base, outer_init;
3261           HOST_WIDE_INT pbitsize, pbitpos;
3262           tree poffset;
3263           enum machine_mode pmode;
3264           int punsignedp, pvolatilep;
3265           affine_iv base_iv, offset_iv;
3266           tree dinit;
3267
3268           /* Build a reference to the first location accessed by the 
3269              inner-loop: *(BASE+INIT). (The first location is actually
3270              BASE+INIT+OFFSET, but we add OFFSET separately later.  */
3271           tree inner_base = build_fold_indirect_ref 
3272                                 (fold_build2 (PLUS_EXPR, 
3273                                               TREE_TYPE (base), base, init));
3274
3275           if (vect_print_dump_info (REPORT_DETAILS))
3276             {
3277               fprintf (dump_file, "analyze in outer-loop: ");
3278               print_generic_expr (dump_file, inner_base, TDF_SLIM);
3279             }
3280
3281           outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos, 
3282                           &poffset, &pmode, &punsignedp, &pvolatilep, false);
3283           gcc_assert (outer_base != NULL_TREE);
3284
3285           if (pbitpos % BITS_PER_UNIT != 0)
3286             {
3287               if (vect_print_dump_info (REPORT_DETAILS))
3288                 fprintf (dump_file, "failed: bit offset alignment.\n");
3289               return false;
3290             }
3291
3292           outer_base = build_fold_addr_expr (outer_base);
3293           if (!simple_iv (loop, stmt, outer_base, &base_iv, false))
3294             {
3295               if (vect_print_dump_info (REPORT_DETAILS))
3296                 fprintf (dump_file, "failed: evolution of base is not affine.\n");
3297               return false;
3298             }
3299
3300           if (offset)
3301             {
3302               if (poffset)
3303                 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, poffset);
3304               else
3305                 poffset = offset;
3306             }
3307
3308           if (!poffset)
3309             {
3310               offset_iv.base = ssize_int (0);
3311               offset_iv.step = ssize_int (0);
3312             }
3313           else if (!simple_iv (loop, stmt, poffset, &offset_iv, false))
3314             {
3315               if (vect_print_dump_info (REPORT_DETAILS))
3316                 fprintf (dump_file, "evolution of offset is not affine.\n");
3317               return false;
3318             }
3319
3320           outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3321           split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3322           outer_init =  size_binop (PLUS_EXPR, outer_init, dinit);
3323           split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3324           outer_init =  size_binop (PLUS_EXPR, outer_init, dinit);
3325
3326           outer_step = size_binop (PLUS_EXPR,
3327                                 fold_convert (ssizetype, base_iv.step),
3328                                 fold_convert (ssizetype, offset_iv.step));
3329
3330           STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3331           /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3332           STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base; 
3333           STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3334           STMT_VINFO_DR_OFFSET (stmt_info) = 
3335                                 fold_convert (ssizetype, offset_iv.base);
3336           STMT_VINFO_DR_ALIGNED_TO (stmt_info) = 
3337                                 size_int (highest_pow2_factor (offset_iv.base));
3338
3339           if (dump_file && (dump_flags & TDF_DETAILS))
3340             {
3341               fprintf (dump_file, "\touter base_address: ");
3342               print_generic_expr (dump_file, STMT_VINFO_DR_BASE_ADDRESS (stmt_info), TDF_SLIM);
3343               fprintf (dump_file, "\n\touter offset from base address: ");
3344               print_generic_expr (dump_file, STMT_VINFO_DR_OFFSET (stmt_info), TDF_SLIM);
3345               fprintf (dump_file, "\n\touter constant offset from base address: ");
3346               print_generic_expr (dump_file, STMT_VINFO_DR_INIT (stmt_info), TDF_SLIM);
3347               fprintf (dump_file, "\n\touter step: ");
3348               print_generic_expr (dump_file, STMT_VINFO_DR_STEP (stmt_info), TDF_SLIM);
3349               fprintf (dump_file, "\n\touter aligned to: ");
3350               print_generic_expr (dump_file, STMT_VINFO_DR_ALIGNED_TO (stmt_info), TDF_SLIM);
3351             }
3352         }
3353
3354       if (STMT_VINFO_DATA_REF (stmt_info))
3355         {
3356           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3357             {
3358               fprintf (vect_dump,
3359                        "not vectorized: more than one data ref in stmt: ");
3360               print_generic_expr (vect_dump, stmt, TDF_SLIM);
3361             }
3362           return false;
3363         }
3364       STMT_VINFO_DATA_REF (stmt_info) = dr;
3365      
3366       /* Set vectype for STMT.  */
3367       scalar_type = TREE_TYPE (DR_REF (dr));
3368       STMT_VINFO_VECTYPE (stmt_info) =
3369                 get_vectype_for_scalar_type (scalar_type);
3370       if (!STMT_VINFO_VECTYPE (stmt_info)) 
3371         {
3372           if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3373             {
3374               fprintf (vect_dump,
3375                        "not vectorized: no vectype for stmt: ");
3376               print_generic_expr (vect_dump, stmt, TDF_SLIM);
3377               fprintf (vect_dump, " scalar_type: ");
3378               print_generic_expr (vect_dump, scalar_type, TDF_DETAILS);
3379             }
3380           return false;
3381         }
3382     }
3383       
3384   return true;
3385 }
3386
3387
3388 /* Utility functions used by vect_mark_stmts_to_be_vectorized.  */
3389
3390 /* Function vect_mark_relevant.
3391
3392    Mark STMT as "relevant for vectorization" and add it to WORKLIST.  */
3393
3394 static void
3395 vect_mark_relevant (VEC(tree,heap) **worklist, tree stmt,
3396                     enum vect_relevant relevant, bool live_p)
3397 {
3398   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3399   enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
3400   bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
3401
3402   if (vect_print_dump_info (REPORT_DETAILS))
3403     fprintf (vect_dump, "mark relevant %d, live %d.", relevant, live_p);
3404
3405   if (STMT_VINFO_IN_PATTERN_P (stmt_info))
3406     {
3407       tree pattern_stmt;
3408
3409       /* This is the last stmt in a sequence that was detected as a 
3410          pattern that can potentially be vectorized.  Don't mark the stmt
3411          as relevant/live because it's not going to be vectorized.
3412          Instead mark the pattern-stmt that replaces it.  */
3413
3414       pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
3415
3416       if (vect_print_dump_info (REPORT_DETAILS))
3417         fprintf (vect_dump, "last stmt in pattern. don't mark relevant/live.");
3418       stmt_info = vinfo_for_stmt (pattern_stmt);
3419       gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
3420       save_relevant = STMT_VINFO_RELEVANT (stmt_info);
3421       save_live_p = STMT_VINFO_LIVE_P (stmt_info);
3422       stmt = pattern_stmt;
3423     }
3424
3425   STMT_VINFO_LIVE_P (stmt_info) |= live_p;
3426   if (relevant > STMT_VINFO_RELEVANT (stmt_info))
3427     STMT_VINFO_RELEVANT (stmt_info) = relevant;
3428
3429   if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
3430       && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
3431     {
3432       if (vect_print_dump_info (REPORT_DETAILS))
3433         fprintf (vect_dump, "already marked relevant/live.");
3434       return;
3435     }
3436
3437   VEC_safe_push (tree, heap, *worklist, stmt);
3438 }
3439
3440
3441 /* Function vect_stmt_relevant_p.
3442
3443    Return true if STMT in loop that is represented by LOOP_VINFO is
3444    "relevant for vectorization".
3445
3446    A stmt is considered "relevant for vectorization" if:
3447    - it has uses outside the loop.
3448    - it has vdefs (it alters memory).
3449    - control stmts in the loop (except for the exit condition).
3450
3451    CHECKME: what other side effects would the vectorizer allow?  */
3452
3453 static bool
3454 vect_stmt_relevant_p (tree stmt, loop_vec_info loop_vinfo,
3455                       enum vect_relevant *relevant, bool *live_p)
3456 {
3457   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3458   ssa_op_iter op_iter;
3459   imm_use_iterator imm_iter;
3460   use_operand_p use_p;
3461   def_operand_p def_p;
3462
3463   *relevant = vect_unused_in_loop;
3464   *live_p = false;
3465
3466   /* cond stmt other than loop exit cond.  */
3467   if (is_ctrl_stmt (stmt) 
3468       && STMT_VINFO_TYPE (vinfo_for_stmt (stmt)) != loop_exit_ctrl_vec_info_type) 
3469     *relevant = vect_used_in_loop;
3470
3471   /* changing memory.  */
3472   if (TREE_CODE (stmt) != PHI_NODE)
3473     if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
3474       {
3475         if (vect_print_dump_info (REPORT_DETAILS))
3476           fprintf (vect_dump, "vec_stmt_relevant_p: stmt has vdefs.");
3477         *relevant = vect_used_in_loop;
3478       }
3479
3480   /* uses outside the loop.  */
3481   FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
3482     {
3483       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
3484         {
3485           basic_block bb = bb_for_stmt (USE_STMT (use_p));
3486           if (!flow_bb_inside_loop_p (loop, bb))
3487             {
3488               if (vect_print_dump_info (REPORT_DETAILS))
3489                 fprintf (vect_dump, "vec_stmt_relevant_p: used out of loop.");
3490
3491               /* We expect all such uses to be in the loop exit phis
3492                  (because of loop closed form)   */
3493               gcc_assert (TREE_CODE (USE_STMT (use_p)) == PHI_NODE);
3494               gcc_assert (bb == single_exit (loop)->dest);
3495
3496               *live_p = true;
3497             }
3498         }
3499     }
3500
3501   return (*live_p || *relevant);
3502 }
3503
3504
3505 /* 
3506    Function process_use.
3507
3508    Inputs:
3509    - a USE in STMT in a loop represented by LOOP_VINFO
3510    - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt 
3511      that defined USE. This is dont by calling mark_relevant and passing it
3512      the WORKLIST (to add DEF_STMT to the WORKlist in case itis relevant). 
3513
3514    Outputs:
3515    Generally, LIVE_P and RELEVANT are used to define the liveness and
3516    relevance info of the DEF_STMT of this USE:
3517        STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
3518        STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
3519    Exceptions:
3520    - case 1: If USE is used only for address computations (e.g. array indexing),
3521    which does not need to be directly vectorized, then the liveness/relevance 
3522    of the respective DEF_STMT is left unchanged.
3523    - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we 
3524    skip DEF_STMT cause it had already been processed.  
3525    - case 3: If DEF_STMT and STMT are in different nests, then  "relevant" will
3526    be modified accordingly.
3527
3528    Return true if everything is as expected. Return false otherwise.  */
3529
3530 static bool
3531 process_use (tree stmt, tree use, loop_vec_info loop_vinfo, bool live_p, 
3532              enum vect_relevant relevant, VEC(tree,heap) **worklist)
3533 {
3534   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3535   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3536   stmt_vec_info dstmt_vinfo;
3537   basic_block bb, def_bb;
3538   tree def, def_stmt;
3539   enum vect_def_type dt;
3540
3541   /* case 1: we are only interested in uses that need to be vectorized.  Uses 
3542      that are used for address computation are not considered relevant.  */
3543   if (!exist_non_indexing_operands_for_use_p (use, stmt))
3544      return true;
3545
3546   if (!vect_is_simple_use (use, loop_vinfo, &def_stmt, &def, &dt))
3547     { 
3548       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
3549         fprintf (vect_dump, "not vectorized: unsupported use in stmt.");
3550       return false;
3551     }
3552
3553   if (!def_stmt || IS_EMPTY_STMT (def_stmt))
3554     return true;
3555
3556   def_bb = bb_for_stmt (def_stmt);
3557   if (!flow_bb_inside_loop_p (loop, def_bb))
3558     {
3559       if (vect_print_dump_info (REPORT_DETAILS))
3560         fprintf (vect_dump, "def_stmt is out of loop.");
3561       return true;
3562     }
3563
3564   /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT). 
3565      DEF_STMT must have already been processed, because this should be the 
3566      only way that STMT, which is a reduction-phi, was put in the worklist, 
3567      as there should be no other uses for DEF_STMT in the loop.  So we just 
3568      check that everything is as expected, and we are done.  */
3569   dstmt_vinfo = vinfo_for_stmt (def_stmt);
3570   bb = bb_for_stmt (stmt);
3571   if (TREE_CODE (stmt) == PHI_NODE
3572       && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
3573       && TREE_CODE (def_stmt) != PHI_NODE
3574       && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
3575       && bb->loop_father == def_bb->loop_father)
3576     {
3577       if (vect_print_dump_info (REPORT_DETAILS))
3578         fprintf (vect_dump, "reduc-stmt defining reduc-phi in the same nest.");
3579       if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
3580         dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
3581       gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
3582       gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo) 
3583                   || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_loop);
3584       return true;
3585     }
3586
3587   /* case 3a: outer-loop stmt defining an inner-loop stmt:
3588         outer-loop-header-bb:
3589                 d = def_stmt
3590         inner-loop:
3591                 stmt # use (d)
3592         outer-loop-tail-bb:
3593                 ...               */
3594   if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
3595     {
3596       if (vect_print_dump_info (REPORT_DETAILS))
3597         fprintf (vect_dump, "outer-loop def-stmt defining inner-loop stmt.");
3598       switch (relevant)
3599         {
3600         case vect_unused_in_loop:
3601           relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def) ?
3602                         vect_used_by_reduction : vect_unused_in_loop;
3603           break;
3604         case vect_used_in_outer_by_reduction:
3605           relevant = vect_used_by_reduction;
3606           break;
3607         case vect_used_in_outer:
3608           relevant = vect_used_in_loop;
3609           break;
3610         case vect_used_by_reduction: 
3611         case vect_used_in_loop:
3612           break;
3613
3614         default:
3615           gcc_unreachable ();
3616         }   
3617     }
3618
3619   /* case 3b: inner-loop stmt defining an outer-loop stmt:
3620         outer-loop-header-bb:
3621                 ...
3622         inner-loop:
3623                 d = def_stmt
3624         outer-loop-tail-bb:
3625                 stmt # use (d)          */
3626   else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
3627     {
3628       if (vect_print_dump_info (REPORT_DETAILS))
3629         fprintf (vect_dump, "inner-loop def-stmt defining outer-loop stmt.");
3630       switch (relevant)
3631         {
3632         case vect_unused_in_loop:
3633           relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def) ?
3634                         vect_used_in_outer_by_reduction : vect_unused_in_loop;
3635           break;
3636
3637         case vect_used_in_outer_by_reduction:
3638         case vect_used_in_outer:
3639           break;
3640
3641         case vect_used_by_reduction:
3642           relevant = vect_used_in_outer_by_reduction;
3643           break;
3644
3645         case vect_used_in_loop:
3646           relevant = vect_used_in_outer;
3647           break;
3648
3649         default:
3650           gcc_unreachable ();
3651         }
3652     }
3653
3654   vect_mark_relevant (worklist, def_stmt, relevant, live_p);
3655   return true;
3656 }
3657
3658
3659 /* Function vect_mark_stmts_to_be_vectorized.
3660
3661    Not all stmts in the loop need to be vectorized. For example:
3662
3663      for i...
3664        for j...
3665    1.    T0 = i + j
3666    2.    T1 = a[T0]
3667
3668    3.    j = j + 1
3669
3670    Stmt 1 and 3 do not need to be vectorized, because loop control and
3671    addressing of vectorized data-refs are handled differently.
3672
3673    This pass detects such stmts.  */
3674
3675 static bool
3676 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
3677 {
3678   VEC(tree,heap) *worklist;
3679   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3680   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3681   unsigned int nbbs = loop->num_nodes;
3682   block_stmt_iterator si;
3683   tree stmt;
3684   stmt_ann_t ann;
3685   unsigned int i;
3686   stmt_vec_info stmt_vinfo;
3687   basic_block bb;
3688   tree phi;
3689   bool live_p;
3690   enum vect_relevant relevant;
3691
3692   if (vect_print_dump_info (REPORT_DETAILS))
3693     fprintf (vect_dump, "=== vect_mark_stmts_to_be_vectorized ===");
3694
3695   worklist = VEC_alloc (tree, heap, 64);
3696
3697   /* 1. Init worklist.  */
3698   for (i = 0; i < nbbs; i++)
3699     {
3700       bb = bbs[i];
3701       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3702         { 
3703           if (vect_print_dump_info (REPORT_DETAILS))
3704             {
3705               fprintf (vect_dump, "init: phi relevant? ");
3706               print_generic_expr (vect_dump, phi, TDF_SLIM);
3707             }
3708
3709           if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
3710             vect_mark_relevant (&worklist, phi, relevant, live_p);
3711         }
3712       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3713         {
3714           stmt = bsi_stmt (si);
3715           if (vect_print_dump_info (REPORT_DETAILS))
3716             {
3717               fprintf (vect_dump, "init: stmt relevant? ");
3718               print_generic_expr (vect_dump, stmt, TDF_SLIM);
3719             } 
3720
3721           if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
3722             vect_mark_relevant (&worklist, stmt, relevant, live_p);
3723         }
3724     }
3725
3726   /* 2. Process_worklist */
3727   while (VEC_length (tree, worklist) > 0)
3728     {
3729       use_operand_p use_p;
3730       ssa_op_iter iter;
3731
3732       stmt = VEC_pop (tree, worklist);
3733       if (vect_print_dump_info (REPORT_DETAILS))
3734         {
3735           fprintf (vect_dump, "worklist: examine stmt: ");
3736           print_generic_expr (vect_dump, stmt, TDF_SLIM);
3737         }
3738
3739       /* Examine the USEs of STMT. For each USE, mark the stmt that defines it 
3740          (DEF_STMT) as relevant/irrelevant and live/dead according to the 
3741          liveness and relevance properties of STMT.  */
3742       ann = stmt_ann (stmt);
3743       stmt_vinfo = vinfo_for_stmt (stmt);
3744       relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
3745       live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
3746
3747       /* Generally, the liveness and relevance properties of STMT are
3748          propagated as is to the DEF_STMTs of its USEs:
3749           live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
3750           relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
3751
3752          One exception is when STMT has been identified as defining a reduction
3753          variable; in this case we set the liveness/relevance as follows:
3754            live_p = false
3755            relevant = vect_used_by_reduction
3756          This is because we distinguish between two kinds of relevant stmts -
3757          those that are used by a reduction computation, and those that are 
3758          (also) used by a regular computation. This allows us later on to 
3759          identify stmts that are used solely by a reduction, and therefore the 
3760          order of the results that they produce does not have to be kept.
3761
3762          Reduction phis are expected to be used by a reduction stmt, or by
3763          in an outer loop;  Other reduction stmts are expected to be
3764          in the loop, and possibly used by a stmt in an outer loop. 
3765          Here are the expected values of "relevant" for reduction phis/stmts:
3766
3767          relevance:                             phi     stmt
3768          vect_unused_in_loop                            ok
3769          vect_used_in_outer_by_reduction        ok      ok
3770          vect_used_in_outer                     ok      ok
3771          vect_used_by_reduction                 ok
3772          vect_used_in_loop                                                */
3773
3774       if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def)
3775         {
3776           enum vect_relevant tmp_relevant = relevant;
3777           switch (tmp_relevant)
3778             {
3779             case vect_unused_in_loop:
3780               gcc_assert (TREE_CODE (stmt) != PHI_NODE);
3781               relevant = vect_used_by_reduction;
3782               break;
3783
3784             case vect_used_in_outer_by_reduction:
3785             case vect_used_in_outer:
3786               gcc_assert (TREE_CODE (stmt) != WIDEN_SUM_EXPR
3787                           && TREE_CODE (stmt) != DOT_PROD_EXPR);
3788               break;
3789
3790             case vect_used_by_reduction:
3791               if (TREE_CODE (stmt) == PHI_NODE)
3792                 break;
3793               /* fall through */
3794             case vect_used_in_loop:
3795             default:
3796               if (vect_print_dump_info (REPORT_DETAILS))
3797                 fprintf (vect_dump, "unsupported use of reduction.");
3798               VEC_free (tree, heap, worklist);
3799               return false;
3800             }
3801           live_p = false;       
3802         }
3803
3804       FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3805         {
3806           tree op = USE_FROM_PTR (use_p);
3807           if (!process_use (stmt, op, loop_vinfo, live_p, relevant, &worklist))
3808             {
3809               VEC_free (tree, heap, worklist);
3810               return false;
3811             }
3812         }
3813     } /* while worklist */
3814
3815   VEC_free (tree, heap, worklist);
3816   return true;
3817 }
3818
3819
3820 /* Function vect_can_advance_ivs_p
3821
3822    In case the number of iterations that LOOP iterates is unknown at compile 
3823    time, an epilog loop will be generated, and the loop induction variables 
3824    (IVs) will be "advanced" to the value they are supposed to take just before 
3825    the epilog loop.  Here we check that the access function of the loop IVs
3826    and the expression that represents the loop bound are simple enough.
3827    These restrictions will be relaxed in the future.  */
3828
3829 static bool 
3830 vect_can_advance_ivs_p (loop_vec_info loop_vinfo)
3831 {
3832   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3833   basic_block bb = loop->header;
3834   tree phi;
3835
3836   /* Analyze phi functions of the loop header.  */
3837
3838   if (vect_print_dump_info (REPORT_DETAILS))
3839     fprintf (vect_dump, "vect_can_advance_ivs_p:");
3840
3841   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3842     {
3843       tree access_fn = NULL;
3844       tree evolution_part;
3845
3846       if (vect_print_dump_info (REPORT_DETAILS))
3847         {
3848           fprintf (vect_dump, "Analyze phi: ");
3849           print_generic_expr (vect_dump, phi, TDF_SLIM);
3850         }
3851
3852       /* Skip virtual phi's. The data dependences that are associated with
3853          virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.  */
3854
3855       if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
3856         {
3857           if (vect_print_dump_info (REPORT_DETAILS))
3858             fprintf (vect_dump, "virtual phi. skip.");
3859           continue;
3860         }
3861
3862       /* Skip reduction phis.  */
3863
3864       if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (phi)) == vect_reduction_def)
3865         {
3866           if (vect_print_dump_info (REPORT_DETAILS))
3867             fprintf (vect_dump, "reduc phi. skip.");
3868           continue;
3869         }
3870
3871       /* Analyze the evolution function.  */
3872
3873       access_fn = instantiate_parameters
3874         (loop, analyze_scalar_evolution (loop, PHI_RESULT (phi)));
3875
3876       if (!access_fn)
3877         {
3878           if (vect_print_dump_info (REPORT_DETAILS))
3879             fprintf (vect_dump, "No Access function.");
3880           return false;
3881         }
3882
3883       if (vect_print_dump_info (REPORT_DETAILS))
3884         {
3885           fprintf (vect_dump, "Access function of PHI: ");
3886           print_generic_expr (vect_dump, access_fn, TDF_SLIM);
3887         }
3888
3889       evolution_part = evolution_part_in_loop_num (access_fn, loop->num);
3890       
3891       if (evolution_part == NULL_TREE)
3892         {
3893           if (vect_print_dump_info (REPORT_DETAILS))
3894             fprintf (vect_dump, "No evolution.");
3895           return false;
3896         }
3897   
3898       /* FORNOW: We do not transform initial conditions of IVs 
3899          which evolution functions are a polynomial of degree >= 2.  */
3900
3901       if (tree_is_chrec (evolution_part))
3902         return false;  
3903     }
3904
3905   return true;
3906 }
3907
3908
3909 /* Function vect_get_loop_niters.
3910
3911    Determine how many iterations the loop is executed.
3912    If an expression that represents the number of iterations
3913    can be constructed, place it in NUMBER_OF_ITERATIONS.
3914    Return the loop exit condition.  */
3915
3916 static tree
3917 vect_get_loop_niters (struct loop *loop, tree *number_of_iterations)
3918 {
3919   tree niters;
3920
3921   if (vect_print_dump_info (REPORT_DETAILS))
3922     fprintf (vect_dump, "=== get_loop_niters ===");
3923
3924   niters = number_of_exit_cond_executions (loop);
3925
3926   if (niters != NULL_TREE
3927       && niters != chrec_dont_know)
3928     {
3929       *number_of_iterations = niters;
3930
3931       if (vect_print_dump_info (REPORT_DETAILS))
3932         {
3933           fprintf (vect_dump, "==> get_loop_niters:" );
3934           print_generic_expr (vect_dump, *number_of_iterations, TDF_SLIM);
3935         }
3936     }
3937
3938   return get_loop_exit_condition (loop);
3939 }
3940
3941
3942 /* Function vect_analyze_loop_1.
3943
3944    Apply a set of analyses on LOOP, and create a loop_vec_info struct
3945    for it. The different analyses will record information in the
3946    loop_vec_info struct.  This is a subset of the analyses applied in
3947    vect_analyze_loop, to be applied on an inner-loop nested in the loop
3948    that is now considered for (outer-loop) vectorization.  */
3949
3950 static loop_vec_info
3951 vect_analyze_loop_1 (struct loop *loop)
3952 {
3953   loop_vec_info loop_vinfo;
3954
3955   if (vect_print_dump_info (REPORT_DETAILS))
3956     fprintf (vect_dump, "===== analyze_loop_nest_1 =====");
3957
3958   /* Check the CFG characteristics of the loop (nesting, entry/exit, etc.  */
3959
3960   loop_vinfo = vect_analyze_loop_form (loop);
3961   if (!loop_vinfo)
3962     {
3963       if (vect_print_dump_info (REPORT_DETAILS))
3964         fprintf (vect_dump, "bad inner-loop form.");
3965       return NULL;
3966     }
3967
3968   return loop_vinfo;
3969 }
3970
3971
3972 /* Function vect_analyze_loop_form.
3973
3974    Verify that certain CFG restrictions hold, including:
3975    - the loop has a pre-header
3976    - the loop has a single entry and exit
3977    - the loop exit condition is simple enough, and the number of iterations
3978      can be analyzed (a countable loop).  */
3979
3980 static loop_vec_info
3981 vect_analyze_loop_form (struct loop *loop)
3982 {
3983   loop_vec_info loop_vinfo;
3984   tree loop_cond;
3985   tree number_of_iterations = NULL;
3986   loop_vec_info inner_loop_vinfo = NULL;
3987
3988   if (vect_print_dump_info (REPORT_DETAILS))
3989     fprintf (vect_dump, "=== vect_analyze_loop_form ===");
3990
3991   /* Different restrictions apply when we are considering an inner-most loop,
3992      vs. an outer (nested) loop.  
3993      (FORNOW. May want to relax some of these restrictions in the future).  */
3994
3995   if (!loop->inner)
3996     {
3997       /* Inner-most loop.  We currently require that the number of BBs is 
3998          exactly 2 (the header and latch).  Vectorizable inner-most loops 
3999          look like this:
4000
4001                         (pre-header)
4002                            |
4003                           header <--------+
4004                            | |            |
4005                            | +--> latch --+
4006                            |
4007                         (exit-bb)  */
4008
4009       if (loop->num_nodes != 2)
4010         {
4011           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4012             fprintf (vect_dump, "not vectorized: too many BBs in loop.");
4013           return NULL;
4014         }
4015
4016       if (empty_block_p (loop->header))
4017     {
4018           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4019             fprintf (vect_dump, "not vectorized: empty loop.");
4020       return NULL;
4021     }
4022     }
4023   else
4024     {
4025       struct loop *innerloop = loop->inner;
4026       edge backedge, entryedge;
4027
4028       /* Nested loop. We currently require that the loop is doubly-nested,
4029          contains a single inner loop, and the number of BBs is exactly 5. 
4030          Vectorizable outer-loops look like this:
4031
4032                         (pre-header)
4033                            |
4034                           header <---+
4035                            |         |
4036                           inner-loop |
4037                            |         |
4038                           tail ------+
4039                            | 
4040                         (exit-bb)
4041
4042          The inner-loop has the properties expected of inner-most loops
4043          as described above.  */
4044
4045       if ((loop->inner)->inner || (loop->inner)->next)
4046         {
4047           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4048             fprintf (vect_dump, "not vectorized: multiple nested loops.");
4049           return NULL;
4050         }
4051
4052       /* Analyze the inner-loop.  */
4053       inner_loop_vinfo = vect_analyze_loop_1 (loop->inner);
4054       if (!inner_loop_vinfo)
4055         {
4056           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4057             fprintf (vect_dump, "not vectorized: Bad inner loop.");
4058           return NULL;
4059         }
4060
4061       if (!expr_invariant_in_loop_p (loop,
4062                                         LOOP_VINFO_NITERS (inner_loop_vinfo)))
4063         {
4064           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4065             fprintf (vect_dump,
4066                      "not vectorized: inner-loop count not invariant.");
4067           destroy_loop_vec_info (inner_loop_vinfo, true);
4068           return NULL;
4069         }
4070
4071       if (loop->num_nodes != 5) 
4072         {
4073           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4074             fprintf (vect_dump, "not vectorized: too many BBs in loop.");
4075           destroy_loop_vec_info (inner_loop_vinfo, true);
4076           return NULL;
4077         }
4078
4079       gcc_assert (EDGE_COUNT (innerloop->header->preds) == 2);
4080       backedge = EDGE_PRED (innerloop->header, 1);        
4081       entryedge = EDGE_PRED (innerloop->header, 0);
4082       if (EDGE_PRED (innerloop->header, 0)->src == innerloop->latch)
4083         {
4084           backedge = EDGE_PRED (innerloop->header, 0);
4085           entryedge = EDGE_PRED (innerloop->header, 1); 
4086         }
4087         
4088       if (entryedge->src != loop->header
4089           || !single_exit (innerloop)
4090           || single_exit (innerloop)->dest !=  EDGE_PRED (loop->latch, 0)->src)
4091         {
4092           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4093             fprintf (vect_dump, "not vectorized: unsupported outerloop form.");
4094           destroy_loop_vec_info (inner_loop_vinfo, true);
4095           return NULL;
4096         }
4097
4098       if (vect_print_dump_info (REPORT_DETAILS))
4099         fprintf (vect_dump, "Considering outer-loop vectorization.");
4100     }
4101   
4102   if (!single_exit (loop) 
4103       || EDGE_COUNT (loop->header->preds) != 2)
4104     {
4105       if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4106         {
4107           if (!single_exit (loop))
4108             fprintf (vect_dump, "not vectorized: multiple exits.");
4109           else if (EDGE_COUNT (loop->header->preds) != 2)
4110             fprintf (vect_dump, "not vectorized: too many incoming edges.");
4111         }
4112       if (inner_loop_vinfo)
4113         destroy_loop_vec_info (inner_loop_vinfo, true);
4114       return NULL;
4115     }
4116
4117   /* We assume that the loop exit condition is at the end of the loop. i.e,
4118      that the loop is represented as a do-while (with a proper if-guard
4119      before the loop if needed), where the loop header contains all the
4120      executable statements, and the latch is empty.  */
4121   if (!empty_block_p (loop->latch)
4122         || phi_nodes (loop->latch))
4123     {
4124       if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4125         fprintf (vect_dump, "not vectorized: unexpected loop form.");
4126       if (inner_loop_vinfo)
4127         destroy_loop_vec_info (inner_loop_vinfo, true);
4128       return NULL;
4129     }
4130
4131   /* Make sure there exists a single-predecessor exit bb:  */
4132   if (!single_pred_p (single_exit (loop)->dest))
4133     {
4134       edge e = single_exit (loop);
4135       if (!(e->flags & EDGE_ABNORMAL))
4136         {
4137           split_loop_exit_edge (e);
4138           if (vect_print_dump_info (REPORT_DETAILS))
4139             fprintf (vect_dump, "split exit edge.");
4140         }
4141       else
4142         {
4143           if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4144             fprintf (vect_dump, "not vectorized: abnormal loop exit edge.");
4145           if (inner_loop_vinfo)
4146             destroy_loop_vec_info (inner_loop_vinfo, true);
4147           return NULL;
4148         }
4149     }
4150
4151   loop_cond = vect_get_loop_niters (loop, &number_of_iterations);
4152   if (!loop_cond)
4153     {
4154       if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4155         fprintf (vect_dump, "not vectorized: complicated exit condition.");
4156       if (inner_loop_vinfo)
4157         destroy_loop_vec_info (inner_loop_vinfo, true);
4158       return NULL;
4159     }
4160   
4161   if (!number_of_iterations) 
4162     {
4163       if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4164         fprintf (vect_dump, 
4165                  "not vectorized: number of iterations cannot be computed.");
4166       if (inner_loop_vinfo)
4167         destroy_loop_vec_info (inner_loop_vinfo, true);
4168       return NULL;
4169     }
4170
4171   if (chrec_contains_undetermined (number_of_iterations))
4172     {
4173       if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS))
4174         fprintf (vect_dump, "Infinite number of iterations.");
4175       if (inner_loop_vinfo)
4176         destroy_loop_vec_info (inner_loop_vinfo, true);
4177       return NULL;
4178     }
4179
4180   if (!NITERS_KNOWN_P (number_of_iterations))
4181     {
4182       if (vect_print_dump_info (REPORT_DETAILS))
4183         {
4184           fprintf (vect_dump, "Symbolic number of iterations is ");
4185           print_generic_expr (vect_dump, number_of_iterations, TDF_DETAILS);
4186         }
4187     }
4188   else if (TREE_INT_CST_LOW (number_of_iterations) == 0)
4189     {
4190       if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS))
4191         fprintf (vect_dump, "not vectorized: number of iterations = 0.");
4192       if (inner_loop_vinfo)
4193         destroy_loop_vec_info (inner_loop_vinfo, false);
4194       return NULL;
4195     }
4196
4197   loop_vinfo = new_loop_vec_info (loop);
4198   LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
4199
4200   STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond)) = loop_exit_ctrl_vec_info_type;
4201
4202   /* CHECKME: May want to keep it around it in the future.  */
4203   if (inner_loop_vinfo)
4204     destroy_loop_vec_info (inner_loop_vinfo, false);
4205
4206   gcc_assert (!loop->aux);
4207   loop->aux = loop_vinfo;
4208   return loop_vinfo;
4209 }
4210
4211
4212 /* Function vect_analyze_loop.
4213
4214    Apply a set of analyses on LOOP, and create a loop_vec_info struct
4215    for it. The different analyses will record information in the
4216    loop_vec_info struct.  */
4217 loop_vec_info
4218 vect_analyze_loop (struct loop *loop)
4219 {
4220   bool ok;
4221   loop_vec_info loop_vinfo;
4222
4223   if (vect_print_dump_info (REPORT_DETAILS))
4224     fprintf (vect_dump, "===== analyze_loop_nest =====");
4225
4226   if (loop_outer (loop) 
4227       && loop_vec_info_for_loop (loop_outer (loop))
4228       && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop))))
4229     {
4230       if (vect_print_dump_info (REPORT_DETAILS))
4231         fprintf (vect_dump, "outer-loop already vectorized.");
4232       return NULL;
4233     }
4234
4235   /* Check the CFG characteristics of the loop (nesting, entry/exit, etc.  */
4236
4237   loop_vinfo = vect_analyze_loop_form (loop);
4238   if (!loop_vinfo)
4239     {
4240       if (vect_print_dump_info (REPORT_DETAILS))
4241         fprintf (vect_dump, "bad loop form.");
4242       return NULL;
4243     }
4244
4245   /* Find all data references in the loop (which correspond to vdefs/vuses)
4246      and analyze their evolution in the loop.
4247
4248      FORNOW: Handle only simple, array references, which
4249      alignment can be forced, and aligned pointer-references.  */
4250
4251   ok = vect_analyze_data_refs (loop_vinfo);
4252   if (!ok)
4253     {
4254       if (vect_print_dump_info (REPORT_DETAILS))
4255         fprintf (vect_dump, "bad data references.");
4256       destroy_loop_vec_info (loop_vinfo, true);
4257       return NULL;
4258     }
4259
4260   /* Classify all cross-iteration scalar data-flow cycles.
4261      Cross-iteration cycles caused by virtual phis are analyzed separately.  */
4262
4263   vect_analyze_scalar_cycles (loop_vinfo);
4264
4265   vect_pattern_recog (loop_vinfo);
4266
4267   /* Data-flow analysis to detect stmts that do not need to be vectorized.  */
4268
4269   ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
4270   if (!ok)
4271     {
4272       if (vect_print_dump_info (REPORT_DETAILS))
4273         fprintf (vect_dump, "unexpected pattern.");
4274       destroy_loop_vec_info (loop_vinfo, true);
4275       return NULL;
4276     }
4277
4278   /* Analyze the alignment of the data-refs in the loop.
4279      Fail if a data reference is found that cannot be vectorized.  */
4280
4281   ok = vect_analyze_data_refs_alignment (loop_vinfo);
4282   if (!ok)
4283     {
4284       if (vect_print_dump_info (REPORT_DETAILS))
4285         fprintf (vect_dump, "bad data alignment.");
4286       destroy_loop_vec_info (loop_vinfo, true);
4287       return NULL;
4288     }
4289
4290   ok = vect_determine_vectorization_factor (loop_vinfo);
4291   if (!ok)
4292     {
4293       if (vect_print_dump_info (REPORT_DETAILS))
4294         fprintf (vect_dump, "can't determine vectorization factor.");
4295       destroy_loop_vec_info (loop_vinfo, true);
4296       return NULL;
4297     }
4298
4299   /* Analyze data dependences between the data-refs in the loop. 
4300      FORNOW: fail at the first data dependence that we encounter.  */
4301
4302   ok = vect_analyze_data_ref_dependences (loop_vinfo);
4303   if (!ok)
4304     {
4305       if (vect_print_dump_info (REPORT_DETAILS))
4306         fprintf (vect_dump, "bad data dependence.");
4307       destroy_loop_vec_info (loop_vinfo, true);
4308       return NULL;
4309     }
4310
4311   /* Analyze the access patterns of the data-refs in the loop (consecutive,
4312      complex, etc.). FORNOW: Only handle consecutive access pattern.  */
4313
4314   ok = vect_analyze_data_ref_accesses (loop_vinfo);
4315   if (!ok)
4316     {
4317       if (vect_print_dump_info (REPORT_DETAILS))
4318         fprintf (vect_dump, "bad data access.");
4319       destroy_loop_vec_info (loop_vinfo, true);
4320       return NULL;
4321     }
4322
4323   /* Prune the list of ddrs to be tested at run-time by versioning for alias.
4324      It is important to call pruning after vect_analyze_data_ref_accesses,
4325      since we use grouping information gathered by interleaving analysis.  */
4326   ok = vect_prune_runtime_alias_test_list (loop_vinfo);
4327   if (!ok)
4328     {
4329       if (vect_print_dump_info (REPORT_DETAILS))
4330         fprintf (vect_dump, "too long list of versioning for alias "
4331                             "run-time tests.");
4332       destroy_loop_vec_info (loop_vinfo, true);
4333       return NULL;
4334     }
4335
4336   /* Check the SLP opportunities in the loop, analyze and build SLP trees.  */
4337   ok = vect_analyze_slp (loop_vinfo);
4338   if (ok)
4339     {
4340       /* Decide which possible SLP instances to SLP.  */
4341       vect_make_slp_decision (loop_vinfo);
4342
4343       /* Find stmts that need to be both vectorized and SLPed.  */
4344       vect_detect_hybrid_slp (loop_vinfo);
4345     }
4346
4347   /* This pass will decide on using loop versioning and/or loop peeling in
4348      order to enhance the alignment of data references in the loop.  */
4349
4350   ok = vect_enhance_data_refs_alignment (loop_vinfo);
4351   if (!ok)
4352     {
4353       if (vect_print_dump_info (REPORT_DETAILS))
4354         fprintf (vect_dump, "bad data alignment.");
4355       destroy_loop_vec_info (loop_vinfo, true);
4356       return NULL;
4357     }
4358
4359   /* Scan all the operations in the loop and make sure they are
4360      vectorizable.  */
4361
4362   ok = vect_analyze_operations (loop_vinfo);
4363   if (!ok)
4364     {
4365       if (vect_print_dump_info (REPORT_DETAILS))
4366         fprintf (vect_dump, "bad operation or unsupported loop bound.");
4367       destroy_loop_vec_info (loop_vinfo, true);
4368       return NULL;
4369     }
4370
4371   LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;
4372
4373   return loop_vinfo;
4374 }