OSDN Git Service

* tree-cfg.c, tree-if-conv.c, tree-into-ssa.c,
[pf3gnuchains/gcc-fork.git] / gcc / tree-vectorizer.c
1 /* Loop Vectorization
2    Copyright (C) 2003, 2004 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 2, 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 COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /* Loop Vectorization Pass.
23
24    This pass tries to vectorize loops. This first implementation focuses on
25    simple inner-most loops, with no conditional control flow, and a set of
26    simple operations which vector form can be expressed using existing
27    tree codes (PLUS, MULT etc).
28
29    For example, the vectorizer transforms the following simple loop:
30
31         short a[N]; short b[N]; short c[N]; int i;
32
33         for (i=0; i<N; i++){
34           a[i] = b[i] + c[i];
35         }
36
37    as if it was manually vectorized by rewriting the source code into:
38
39         typedef int __attribute__((mode(V8HI))) v8hi;
40         short a[N];  short b[N]; short c[N];   int i;
41         v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
42         v8hi va, vb, vc;
43
44         for (i=0; i<N/8; i++){
45           vb = pb[i];
46           vc = pc[i];
47           va = vb + vc;
48           pa[i] = va;
49         }
50
51         The main entry to this pass is vectorize_loops(), in which
52    the vectorizer applies a set of analyses on a given set of loops,
53    followed by the actual vectorization transformation for the loops that
54    had successfully passed the analysis phase.
55
56         Throughout this pass we make a distinction between two types of
57    data: scalars (which are represented by SSA_NAMES), and memory references
58    ("data-refs"). These two types of data require different handling both 
59    during analysis and transformation. The types of data-refs that the 
60    vectorizer currently supports are ARRAY_REFS which base is an array DECL 
61    (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
62    accesses are required to have a  simple (consecutive) access pattern.
63
64    Analysis phase:
65    ===============
66         The driver for the analysis phase is vect_analyze_loop_nest().
67    It applies a set of analyses, some of which rely on the scalar evolution 
68    analyzer (scev) developed by Sebastian Pop.
69
70         During the analysis phase the vectorizer records some information
71    per stmt in a "stmt_vec_info" struct which is attached to each stmt in the 
72    loop, as well as general information about the loop as a whole, which is
73    recorded in a "loop_vec_info" struct attached to each loop.
74
75    Transformation phase:
76    =====================
77         The loop transformation phase scans all the stmts in the loop, and
78    creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
79    the loop that needs to be vectorized. It insert the vector code sequence
80    just before the scalar stmt S, and records a pointer to the vector code
81    in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct 
82    attached to S). This pointer will be used for the vectorization of following
83    stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
84    otherwise, we rely on dead code elimination for removing it.
85
86         For example, say stmt S1 was vectorized into stmt VS1:
87
88    VS1: vb = px[i];
89    S1:  b = x[i];    STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
90    S2:  a = b;
91
92    To vectorize stmt S2, the vectorizer first finds the stmt that defines
93    the operand 'b' (S1), and gets the relevant vector def 'vb' from the
94    vector stmt VS1 pointed by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
95    resulting sequence would be:
96
97    VS1: vb = px[i];
98    S1:  b = x[i];       STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
99    VS2: va = vb;
100    S2:  a = b;          STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
101
102         Operands that are not SSA_NAMEs, are data-refs that appear in 
103    load/store operations (like 'x[i]' in S1), and are handled differently.
104
105    Target modeling:
106    =================
107         Currently the only target specific information that is used is the
108    size of the vector (in bytes) - "UNITS_PER_SIMD_WORD". Targets that can 
109    support different sizes of vectors, for now will need to specify one value 
110    for "UNITS_PER_SIMD_WORD". More flexibility will be added in the future.
111
112         Since we only vectorize operations which vector form can be
113    expressed using existing tree codes, to verify that an operation is
114    supported, the vectorizer checks the relevant optab at the relevant
115    machine_mode (e.g, add_optab->handlers[(int) V8HImode].insn_code). If
116    the value found is CODE_FOR_nothing, then there's no target support, and
117    we can't vectorize the stmt.
118
119    For additional information on this project see:
120    http://gcc.gnu.org/projects/tree-ssa/vectorization.html
121 */
122
123 #include "config.h"
124 #include "system.h"
125 #include "coretypes.h"
126 #include "tm.h"
127 #include "errors.h"
128 #include "ggc.h"
129 #include "tree.h"
130 #include "target.h"
131
132 #include "rtl.h"
133 #include "basic-block.h"
134 #include "diagnostic.h"
135 #include "tree-flow.h"
136 #include "tree-dump.h"
137 #include "timevar.h"
138 #include "cfgloop.h"
139 #include "cfglayout.h"
140 #include "expr.h"
141 #include "optabs.h"
142 #include "toplev.h"
143 #include "tree-chrec.h"
144 #include "tree-data-ref.h"
145 #include "tree-scalar-evolution.h"
146 #include "tree-vectorizer.h"
147 #include "tree-pass.h"
148
149 /* Main analysis functions.  */
150 static loop_vec_info vect_analyze_loop (struct loop *);
151 static loop_vec_info vect_analyze_loop_form (struct loop *);
152 static bool vect_analyze_data_refs (loop_vec_info);
153 static bool vect_mark_stmts_to_be_vectorized (loop_vec_info);
154 static bool vect_analyze_scalar_cycles (loop_vec_info);
155 static bool vect_analyze_data_ref_accesses (loop_vec_info);
156 static bool vect_analyze_data_refs_alignment (loop_vec_info);
157 static bool vect_compute_data_refs_alignment (loop_vec_info);
158 static bool vect_analyze_operations (loop_vec_info);
159
160 /* Main code transformation functions.  */
161 static void vect_transform_loop (loop_vec_info, struct loops *);
162 static void vect_transform_loop_bound (loop_vec_info, tree niters);
163 static bool vect_transform_stmt (tree, block_stmt_iterator *);
164 static bool vectorizable_load (tree, block_stmt_iterator *, tree *);
165 static bool vectorizable_store (tree, block_stmt_iterator *, tree *);
166 static bool vectorizable_operation (tree, block_stmt_iterator *, tree *);
167 static bool vectorizable_assignment (tree, block_stmt_iterator *, tree *);
168 static enum dr_alignment_support vect_supportable_dr_alignment
169   (struct data_reference *);
170 static void vect_align_data_ref (tree);
171 static void vect_enhance_data_refs_alignment (loop_vec_info);
172
173 /* Utility functions for the analyses.  */
174 static bool vect_is_simple_use (tree , struct loop *, tree *);
175 static bool exist_non_indexing_operands_for_use_p (tree, tree);
176 static bool vect_is_simple_iv_evolution (unsigned, tree, tree *, tree *, bool);
177 static void vect_mark_relevant (varray_type, tree);
178 static bool vect_stmt_relevant_p (tree, loop_vec_info);
179 static tree vect_get_loop_niters (struct loop *, tree *);
180 static bool vect_compute_data_ref_alignment 
181   (struct data_reference *, loop_vec_info);
182 static bool vect_analyze_data_ref_access (struct data_reference *);
183 static bool vect_get_first_index (tree, tree *);
184 static bool vect_can_force_dr_alignment_p (tree, unsigned int);
185 static struct data_reference * vect_analyze_pointer_ref_access 
186   (tree, tree, bool);
187 static bool vect_analyze_loop_with_symbolic_num_of_iters (tree niters, 
188                                                           struct loop *loop);
189 static tree vect_get_base_and_bit_offset
190   (struct data_reference *, tree, tree, loop_vec_info, tree *, bool*);
191 static struct data_reference * vect_analyze_pointer_ref_access
192   (tree, tree, bool);
193 static tree vect_compute_array_base_alignment (tree, tree, tree *, tree *);
194 static tree vect_compute_array_ref_alignment
195   (struct data_reference *, loop_vec_info, tree, tree *);
196 static tree vect_get_ptr_offset (tree, tree, tree *);
197 static tree vect_get_symbl_and_dr
198   (tree, tree, bool, loop_vec_info, struct data_reference **);
199
200 /* Utility functions for the code transformation.  */
201 static tree vect_create_destination_var (tree, tree);
202 static tree vect_create_data_ref_ptr 
203   (tree, block_stmt_iterator *, tree, tree *, bool); 
204 static tree vect_create_index_for_vector_ref 
205   (struct loop *, block_stmt_iterator *);
206 static tree vect_create_addr_base_for_vector_ref (tree, tree *, tree);
207 static tree get_vectype_for_scalar_type (tree);
208 static tree vect_get_new_vect_var (tree, enum vect_var_kind, const char *);
209 static tree vect_get_vec_def_for_operand (tree, tree);
210 static tree vect_init_vector (tree, tree);
211 static tree vect_build_symbol_bound (tree, int, struct loop *);
212 static void vect_finish_stmt_generation 
213   (tree stmt, tree vec_stmt, block_stmt_iterator *bsi);
214
215 static void vect_generate_tmps_on_preheader (loop_vec_info, 
216                                              tree *, tree *, 
217                                              tree *);
218 static tree vect_build_loop_niters (loop_vec_info);
219 static void vect_update_ivs_after_vectorizer (struct loop *, tree); 
220
221 /* Loop transformations prior to vectorization.  */
222
223 /* Loop transformations entry point function. 
224    It can be used outside of the vectorizer 
225    in case the loop to be manipulated answers conditions specified
226    in function documentation.  */
227 struct loop *tree_duplicate_loop_to_edge (struct loop *, 
228                                           struct loops *, edge, 
229                                           tree, tree, bool);
230
231 static void allocate_new_names (bitmap);
232 static void rename_use_op (use_operand_p);
233 static void rename_def_op (def_operand_p, tree);
234 static void rename_variables_in_bb (basic_block);
235 static void free_new_names (bitmap);
236 static void rename_variables_in_loop (struct loop *);
237 static void copy_phi_nodes (struct loop *, struct loop *, bool);
238 static void update_phis_for_duplicate_loop (struct loop *,
239                                             struct loop *, 
240                                             bool after);
241 static void update_phi_nodes_for_guard (edge, struct loop *);  
242 static void make_loop_iterate_ntimes (struct loop *, tree, tree, tree);
243 static struct loop *tree_duplicate_loop_to_edge_cfg (struct loop *, 
244                                                      struct loops *, 
245                                                      edge);
246 static edge add_loop_guard (basic_block, tree, basic_block);
247 static bool verify_loop_for_duplication (struct loop *, bool, edge);
248
249 /* Utilities dealing with loop peeling (not peeling itself).  */
250 static tree vect_gen_niters_for_prolog_loop (loop_vec_info, tree);
251 static void vect_update_niters_after_peeling (loop_vec_info, tree);
252 static void vect_update_inits_of_dr (struct data_reference *, struct loop *, 
253                                      tree niters);
254 static void vect_update_inits_of_drs (loop_vec_info, tree);
255 static void vect_do_peeling_for_alignment (loop_vec_info, struct loops *);
256
257 /* Utilities for creation and deletion of vec_info structs.  */
258 loop_vec_info new_loop_vec_info (struct loop *loop);
259 void destroy_loop_vec_info (loop_vec_info);
260 stmt_vec_info new_stmt_vec_info (tree stmt, struct loop *loop);
261
262 static bool vect_debug_stats (struct loop *loop);
263 static bool vect_debug_details (struct loop *loop);
264
265 \f
266 /* Utilities to support loop peeling for vectorization purposes.  */
267
268
269 /* For each definition in DEFINITIONS this function allocates 
270    new ssa name.  */
271
272 static void
273 allocate_new_names (bitmap definitions)
274 {
275   unsigned ver;
276   bitmap_iterator bi;
277
278   EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
279     {
280       tree def = ssa_name (ver);
281       tree *new_name_ptr = xmalloc (sizeof (tree));
282
283       bool abnormal = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def);
284
285       *new_name_ptr = duplicate_ssa_name (def, SSA_NAME_DEF_STMT (def));
286       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (*new_name_ptr) = abnormal;
287
288       SSA_NAME_AUX (def) = new_name_ptr;
289     }
290 }
291
292
293 /* Renames the use *OP_P.  */
294
295 static void
296 rename_use_op (use_operand_p op_p)
297 {
298   tree *new_name_ptr;
299
300   if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
301     return;
302
303   new_name_ptr = SSA_NAME_AUX (USE_FROM_PTR (op_p));
304
305   /* Something defined outside of the loop.  */
306   if (!new_name_ptr)
307     return;
308
309   /* An ordinary ssa name defined in the loop.  */
310
311   SET_USE (op_p, *new_name_ptr);
312 }
313
314
315 /* Renames the def *OP_P in statement STMT.  */
316
317 static void
318 rename_def_op (def_operand_p op_p, tree stmt)
319 {
320   tree *new_name_ptr;
321
322   if (TREE_CODE (DEF_FROM_PTR (op_p)) != SSA_NAME)
323     return;
324
325   new_name_ptr = SSA_NAME_AUX (DEF_FROM_PTR (op_p));
326
327   /* Something defined outside of the loop.  */
328   if (!new_name_ptr)
329     return;
330
331   /* An ordinary ssa name defined in the loop.  */
332
333   SET_DEF (op_p, *new_name_ptr);
334   SSA_NAME_DEF_STMT (DEF_FROM_PTR (op_p)) = stmt;
335 }
336
337
338 /* Renames the variables in basic block BB.  */
339
340 static void
341 rename_variables_in_bb (basic_block bb)
342 {
343   tree phi;
344   block_stmt_iterator bsi;
345   tree stmt;
346   stmt_ann_t ann;
347   use_optype uses;
348   vuse_optype vuses;
349   def_optype defs;
350   v_may_def_optype v_may_defs;
351   v_must_def_optype v_must_defs;
352   unsigned i;
353   edge e;
354   edge_iterator ei;
355
356   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
357     rename_def_op (PHI_RESULT_PTR (phi), phi);
358
359   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
360     {
361       stmt = bsi_stmt (bsi);
362       get_stmt_operands (stmt);
363       ann = stmt_ann (stmt);
364
365       uses = USE_OPS (ann);
366       for (i = 0; i < NUM_USES (uses); i++)
367         rename_use_op (USE_OP_PTR (uses, i));
368
369       defs = DEF_OPS (ann);
370       for (i = 0; i < NUM_DEFS (defs); i++)
371         rename_def_op (DEF_OP_PTR (defs, i), stmt);
372
373       vuses = VUSE_OPS (ann);
374       for (i = 0; i < NUM_VUSES (vuses); i++)
375         rename_use_op (VUSE_OP_PTR (vuses, i));
376
377       v_may_defs = V_MAY_DEF_OPS (ann);
378       for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
379         {
380           rename_use_op (V_MAY_DEF_OP_PTR (v_may_defs, i));
381           rename_def_op (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt);
382         }
383
384       v_must_defs = V_MUST_DEF_OPS (ann);
385       for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
386         {
387           rename_use_op (V_MUST_DEF_KILL_PTR (v_must_defs, i));
388           rename_def_op (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt);
389         }
390     }
391
392   FOR_EACH_EDGE (e, ei, bb->succs)
393     for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
394       rename_use_op (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e));
395 }
396
397
398 /* Releases the structures holding the new ssa names.  */
399
400 static void
401 free_new_names (bitmap definitions)
402 {
403   unsigned ver;
404   bitmap_iterator bi;
405
406   EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
407     {
408       tree def = ssa_name (ver);
409
410       if (SSA_NAME_AUX (def))
411         {
412           free (SSA_NAME_AUX (def));
413           SSA_NAME_AUX (def) = NULL;
414         }
415     }
416 }
417
418
419 /* Renames variables in new generated LOOP.  */
420
421 static void
422 rename_variables_in_loop (struct loop *loop)
423 {
424   unsigned i;
425   basic_block *bbs;
426
427   bbs = get_loop_body (loop);
428
429   for (i = 0; i < loop->num_nodes; i++)
430     rename_variables_in_bb (bbs[i]);
431
432   free (bbs);
433 }
434
435
436 /* This function copies phis from LOOP header to
437    NEW_LOOP header. AFTER is as
438    in update_phis_for_duplicate_loop function.  */
439
440 static void
441 copy_phi_nodes (struct loop *loop, struct loop *new_loop,
442                       bool after)
443 {
444   tree phi, new_phi, def;
445   edge new_e;
446   edge e = (after ? loop_latch_edge (loop) : loop_preheader_edge (loop));
447
448   /* Second add arguments to newly created phi nodes.  */
449   for (phi = phi_nodes (loop->header),
450          new_phi = phi_nodes (new_loop->header);
451        phi;
452        phi = PHI_CHAIN (phi),
453          new_phi = PHI_CHAIN (new_phi))
454     {
455       new_e = loop_preheader_edge (new_loop);
456       def = PHI_ARG_DEF_FROM_EDGE (phi, e);
457       add_phi_arg (&new_phi, def, new_e);
458     }
459 }
460
461
462 /* Update the PHI nodes of the NEW_LOOP. AFTER is true if the NEW_LOOP
463    executes after LOOP, and false if it executes before it.  */
464
465 static void
466 update_phis_for_duplicate_loop (struct loop *loop,
467                                 struct loop *new_loop, bool after)
468 {
469   edge old_latch;
470   tree *new_name_ptr, new_ssa_name;
471   tree phi_new, phi_old, def;
472   edge orig_entry_e = loop_preheader_edge (loop);
473
474   /* Copy phis from loop->header to new_loop->header.  */
475   copy_phi_nodes (loop, new_loop, after);
476
477   old_latch = loop_latch_edge (loop);
478
479   /* Update PHI args for the new loop latch edge, and
480      the old loop preheader edge, we know that the PHI nodes
481      are ordered appropriately in copy_phi_nodes.  */
482   for (phi_new = phi_nodes (new_loop->header),
483        phi_old = phi_nodes (loop->header);
484        phi_new && phi_old;
485        phi_new = TREE_CHAIN (phi_new), phi_old = TREE_CHAIN (phi_old))
486     {
487       def = PHI_ARG_DEF_FROM_EDGE (phi_old, old_latch);
488
489       if (TREE_CODE (def) != SSA_NAME)
490         continue;
491
492       new_name_ptr = SSA_NAME_AUX (def);
493
494       /* Something defined outside of the loop.  */
495       if (!new_name_ptr)
496         continue;
497
498       /* An ordinary ssa name defined in the loop.  */
499       new_ssa_name = *new_name_ptr;
500
501       add_phi_arg (&phi_new, new_ssa_name, loop_latch_edge(new_loop));
502
503       /* Update PHI args for the original loop pre-header edge.  */
504       if (! after)
505         SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi_old, orig_entry_e),
506                  new_ssa_name);
507     }
508 }
509
510
511 /* Update PHI nodes for a guard of the LOOP.
512
513    LOOP is supposed to have a preheader bb at which a guard condition is 
514    located.  The true edge of this condition skips the LOOP and ends
515    at the destination of the (unique) LOOP exit.  The loop exit bb is supposed 
516    to be an empty bb (created by this transformation) with one successor.
517
518    This function creates phi nodes at the LOOP exit bb.  These phis need to be
519    created as a result of adding true edge coming from guard.
520
521    FORNOW: Only phis which have corresponding phi nodes at the header of the
522    LOOP are created.  Here we use the assumption that after the LOOP there
523    are no uses of defs generated in LOOP.
524
525    After the phis creation, the function updates the values of phi nodes at
526    the LOOP exit successor bb:
527
528    Original loop:
529
530    bb0: loop preheader
531         goto bb1
532    bb1: loop header
533         if (exit_cond) goto bb3 else goto bb2
534    bb2: loop latch
535         goto bb1
536    bb3:
537
538
539    After guard creation (the loop before this function):
540
541    bb0: loop preheader
542         if (guard_condition) goto bb4 else goto bb1
543    bb1: loop header
544         if (exit_cond) goto bb4 else goto bb2
545    bb2: loop latch
546         goto bb1
547    bb4: loop exit       
548         (new empty bb)
549         goto bb3
550    bb3:
551
552    This function updates the phi nodes in bb4 and in bb3, to account for the 
553    new edge from bb0 to bb4.  */
554
555 static void
556 update_phi_nodes_for_guard (edge guard_true_edge, struct loop * loop)
557 {
558   tree phi, phi1;
559
560   for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
561       {
562         tree new_phi;
563         tree phi_arg;
564
565         /* Generate new phi node.  */
566         new_phi = create_phi_node (SSA_NAME_VAR (PHI_RESULT (phi)),
567                                    loop->exit_edges[0]->dest);
568
569         /* Add argument coming from guard true edge.  */
570         phi_arg = PHI_ARG_DEF_FROM_EDGE (phi, loop->entry_edges[0]);
571         add_phi_arg (&new_phi, phi_arg, guard_true_edge);
572
573         /* Add argument coming from loop exit edge.  */
574         phi_arg = PHI_ARG_DEF_FROM_EDGE (phi, EDGE_SUCC (loop->latch, 0));                                 
575         add_phi_arg (&new_phi, phi_arg, loop->exit_edges[0]);
576       
577         /* Update all phi nodes at the loop exit successor.  */
578         for (phi1 = phi_nodes (EDGE_SUCC (loop->exit_edges[0]->dest, 0)->dest); 
579              phi1; 
580              phi1 = TREE_CHAIN (phi1))
581           {
582             tree old_arg = PHI_ARG_DEF_FROM_EDGE (phi1, 
583                                   EDGE_SUCC (loop->exit_edges[0]->dest, 0));
584             if (old_arg == phi_arg)
585               { 
586                 edge e = EDGE_SUCC (loop->exit_edges[0]->dest, 0);
587
588                 SET_PHI_ARG_DEF (phi1, 
589                                  phi_arg_from_edge (phi1, e),
590                                  PHI_RESULT (new_phi)); 
591               }
592           }
593       }       
594 }
595
596
597 /* Make the LOOP iterate NITERS times. This is done by adding a new IV
598    that starts at zero, increases by one and its limit is NITERS.  */
599
600 static void
601 make_loop_iterate_ntimes (struct loop *loop, tree niters,
602                           tree begin_label, tree exit_label)
603 {
604   tree indx_before_incr, indx_after_incr, cond_stmt, cond;
605   tree orig_cond;
606   edge exit_edge = loop->exit_edges[0];
607   block_stmt_iterator loop_exit_bsi = bsi_last (exit_edge->src);
608
609   /* Flow loop scan does not update loop->single_exit field.  */
610   loop->single_exit = loop->exit_edges[0];
611   orig_cond = get_loop_exit_condition (loop);
612   gcc_assert (orig_cond);
613   create_iv (integer_zero_node, integer_one_node, NULL_TREE, loop,
614              &loop_exit_bsi, false, &indx_before_incr, &indx_after_incr);
615   
616   /* CREATE_IV uses BSI_INSERT with TSI_NEW_STMT, so we want to get
617      back to the exit condition statement.  */
618   bsi_next (&loop_exit_bsi);
619   gcc_assert (bsi_stmt (loop_exit_bsi) == orig_cond);
620
621
622   if (exit_edge->flags & EDGE_TRUE_VALUE) /* 'then' edge exits the loop.  */
623     cond = build2 (GE_EXPR, boolean_type_node, indx_after_incr, niters);
624   else /* 'then' edge loops back.  */
625     cond = build2 (LT_EXPR, boolean_type_node, indx_after_incr, niters);
626
627   begin_label = build1 (GOTO_EXPR, void_type_node, begin_label);
628   exit_label = build1 (GOTO_EXPR, void_type_node, exit_label);
629   cond_stmt = build (COND_EXPR, TREE_TYPE (orig_cond), cond,
630                      begin_label, exit_label);
631   bsi_insert_before (&loop_exit_bsi, cond_stmt, BSI_SAME_STMT);
632
633   /* Remove old loop exit test:  */
634   bsi_remove (&loop_exit_bsi);
635
636   if (vect_debug_stats (loop) || vect_debug_details (loop))
637     print_generic_expr (dump_file, cond_stmt, TDF_SLIM);
638 }
639
640
641 /* Given LOOP this function generates a new copy of it and puts it 
642    on E which is either the entry or exit of LOOP.  */
643
644 static struct loop *
645 tree_duplicate_loop_to_edge_cfg (struct loop *loop, struct loops *loops, 
646                                  edge e)
647 {
648   struct loop *new_loop;
649   basic_block *new_bbs, *bbs;
650   bool at_exit;
651   bool was_imm_dom;
652   basic_block exit_dest; 
653   tree phi, phi_arg;
654
655   at_exit = (e == loop->exit_edges[0]); 
656   if (!at_exit && e != loop_preheader_edge (loop))
657     {
658       if (dump_file && (dump_flags & TDF_DETAILS))
659           fprintf (dump_file,
660                    "Edge is not an entry nor an exit edge.\n");
661       return NULL;
662     }
663
664   bbs = get_loop_body (loop);
665
666   /* Check whether duplication is possible.  */
667   if (!can_copy_bbs_p (bbs, loop->num_nodes))
668     {
669       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
670           fprintf (dump_file,
671                    "Cannot copy basic blocks.\n");
672       free (bbs);
673       return NULL;
674     }
675
676   /* Generate new loop structure.  */
677   new_loop = duplicate_loop (loops, loop, loop->outer);
678   if (!new_loop)
679     {
680       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
681           fprintf (dump_file,
682                    "The duplicate_loop returns NULL.\n");
683       free (bbs);
684       return NULL;
685     }
686
687   exit_dest = loop->exit_edges[0]->dest;
688   was_imm_dom = (get_immediate_dominator (CDI_DOMINATORS, 
689                                           exit_dest) == loop->header ? 
690                  true : false);
691
692   new_bbs = xmalloc (sizeof (basic_block) * loop->num_nodes);
693
694   copy_bbs (bbs, loop->num_nodes, new_bbs, NULL, 0, NULL, NULL);
695
696   /* Duplicating phi args at exit bbs as coming 
697      also from exit of duplicated loop.  */
698   for (phi = phi_nodes (exit_dest); phi; phi = PHI_CHAIN (phi))
699     {
700       phi_arg = PHI_ARG_DEF_FROM_EDGE (phi, loop->exit_edges[0]);
701       if (phi_arg)
702         {
703           edge new_loop_exit_edge;
704
705           if (EDGE_SUCC (new_loop->header, 0)->dest == new_loop->latch)
706             new_loop_exit_edge = EDGE_SUCC (new_loop->header, 1);
707           else
708             new_loop_exit_edge = EDGE_SUCC (new_loop->header, 0);
709   
710           add_phi_arg (&phi, phi_arg, new_loop_exit_edge);      
711         }
712     }    
713    
714   if (at_exit) /* Add the loop copy at exit.  */
715     {
716       redirect_edge_and_branch_force (e, new_loop->header);
717       set_immediate_dominator (CDI_DOMINATORS, new_loop->header, e->src);
718       if (was_imm_dom)
719         set_immediate_dominator (CDI_DOMINATORS, exit_dest, new_loop->header);
720     }
721   else /* Add the copy at entry.  */
722     {
723       edge new_exit_e;
724       edge entry_e = loop_preheader_edge (loop);
725       basic_block preheader = entry_e->src;
726            
727       if (!flow_bb_inside_loop_p (new_loop, 
728                                   EDGE_SUCC (new_loop->header, 0)->dest))
729         new_exit_e = EDGE_SUCC (new_loop->header, 0);
730       else
731         new_exit_e = EDGE_SUCC (new_loop->header, 1); 
732
733       redirect_edge_and_branch_force (new_exit_e, loop->header);
734       set_immediate_dominator (CDI_DOMINATORS, loop->header,
735                                new_exit_e->src);
736
737       /* We have to add phi args to the loop->header here as coming 
738          from new_exit_e edge.  */
739       for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
740         {
741           phi_arg = PHI_ARG_DEF_FROM_EDGE (phi, entry_e);
742           if (phi_arg)
743             add_phi_arg (&phi, phi_arg, new_exit_e);    
744         }    
745
746       redirect_edge_and_branch_force (entry_e, new_loop->header);
747       set_immediate_dominator (CDI_DOMINATORS, new_loop->header, preheader);
748     }
749
750   flow_loop_scan (new_loop, LOOP_ALL);
751   flow_loop_scan (loop, LOOP_ALL);  
752   free (new_bbs);
753   free (bbs);
754
755   return new_loop;
756 }
757
758
759 /* Given the condition statement COND, put it as the last statement
760    of GUARD_BB; EXIT_BB is the basic block to skip the loop;
761    Assumes that this is the single exit of the guarded loop.  
762    Returns the skip edge.  */
763
764 static edge
765 add_loop_guard (basic_block guard_bb, tree cond, basic_block exit_bb)
766 {
767   block_stmt_iterator bsi;
768   edge new_e, enter_e;
769   tree cond_stmt, then_label, else_label;
770
771   enter_e = EDGE_SUCC (guard_bb, 0);
772   enter_e->flags &= ~EDGE_FALLTHRU;
773   enter_e->flags |= EDGE_FALSE_VALUE;
774   bsi = bsi_last (guard_bb);
775
776   then_label = build1 (GOTO_EXPR, void_type_node,
777                        tree_block_label (exit_bb));
778   else_label = build1 (GOTO_EXPR, void_type_node,
779                        tree_block_label (enter_e->dest));
780   cond_stmt = build (COND_EXPR, void_type_node, cond,
781                      then_label, else_label);
782   bsi_insert_after (&bsi, cond_stmt, BSI_NEW_STMT);
783   /* Add new edge to connect entry block to the second loop.  */
784   new_e = make_edge (guard_bb, exit_bb, EDGE_TRUE_VALUE);
785   set_immediate_dominator (CDI_DOMINATORS, exit_bb, guard_bb);
786   return new_e;
787 }
788
789
790 /* This function verifies that certain restrictions apply to LOOP.  */
791
792 static bool
793 verify_loop_for_duplication (struct loop *loop,
794                              bool update_first_loop_count, edge e)
795 {
796   edge exit_e = loop->exit_edges [0];
797   edge entry_e = loop_preheader_edge (loop);
798
799   /* We duplicate only innermost loops.  */
800   if (loop->inner)
801     {
802       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
803           fprintf (dump_file,
804                    "Loop duplication failed. Loop is not innermost.\n");
805       return false;
806     }
807
808   /* Only loops with 1 exit.  */
809   if (loop->num_exits != 1)
810     {
811       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
812           fprintf (dump_file,
813                    "More than one exit from loop.\n");
814       return false;
815     }
816
817   /* Only loops with 1 entry.  */
818   if (loop->num_entries != 1)
819     {
820       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
821           fprintf (dump_file,
822                    "More than one exit from loop.\n");
823       return false;
824     }
825
826   /* All loops has outers, the only case loop->outer is NULL is for
827      the function itself.  */
828   if (!loop->outer)
829     {
830       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
831           fprintf (dump_file,
832                    "Loop is outer-most loop.\n");
833       return false;
834     }
835   
836   /* Verify that new IV can be created and loop condition 
837      can be changed to make first loop iterate first_niters times.  */
838   if (!update_first_loop_count)
839     {
840       tree orig_cond = get_loop_exit_condition (loop);
841       block_stmt_iterator loop_exit_bsi = bsi_last (exit_e->src);
842       
843       if (!orig_cond)
844         {
845           if (vect_debug_stats (loop) || vect_debug_details (loop))     
846             fprintf (dump_file,
847                      "Loop has no exit condition.\n");
848           return false;
849         }
850       if (orig_cond != bsi_stmt (loop_exit_bsi))
851         {
852           if (vect_debug_stats (loop) || vect_debug_details (loop))     
853             fprintf (dump_file,
854                      "Loop exit condition is not loop header last stmt.\n");
855           return false;
856         }
857     }
858
859   /* Make sure E is either an entry or an exit edge.  */
860   if (e != exit_e && e != entry_e)
861     {
862       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
863         fprintf (dump_file,
864                  "E is not loop entry or exit edge.\n");
865       return false;
866     }
867
868   return true;
869 }
870
871
872 /* Given LOOP this function duplicates it to the edge E. 
873
874    This transformation takes place before the loop is vectorized. 
875    For now, there are two main cases when it's used 
876    by the vectorizer: to support loops with unknown loop bounds 
877    (or loop bounds indivisible by vectorization factor) and to force the 
878    alignment of data references in the loop. In the first case, LOOP is 
879    duplicated to the exit edge, producing epilog loop. In the second case, LOOP 
880    is duplicated to the preheader edge thus generating prolog loop. In both 
881    cases, the original loop will be vectorized after the transformation. 
882
883    The edge E is supposed to be either preheader edge of the LOOP or  
884    its exit edge. If preheader edge is specified, the LOOP copy 
885    will precede the original one. Otherwise the copy will be located 
886    at the exit of the LOOP.
887    
888    FIRST_NITERS (SSA_NAME) parameter specifies how many times to iterate 
889    the first loop. If UPDATE_FIRST_LOOP_COUNT parameter is false, the first 
890    loop will be iterated FIRST_NITERS times by introducing additional 
891    induction variable and replacing loop exit condition. If 
892    UPDATE_FIRST_LOOP_COUNT is true no change to the first loop is made and 
893    the caller to tree_duplicate_loop_to_edge is responsible for updating 
894    the first loop count.
895    
896    NITERS (also SSA_NAME) parameter defines the number of iteration the
897    original loop iterated. The function generates two if-then guards: 
898    one prior to the first loop and the other prior to the second loop. 
899    The first guard will be:
900
901    if (FIRST_NITERS == 0) then skip the first loop
902    
903    The second guard will be:
904
905    if (FIRST_NITERS == NITERS) then skip the second loop
906
907    Thus the equivalence to the original code is guaranteed by correct values 
908    of NITERS and FIRST_NITERS and generation of if-then loop guards.
909
910    For now this function supports only loop forms that are candidate for 
911    vectorization. Such types are the following: 
912
913    (1) only innermost loops 
914    (2) loops built from 2 basic blocks 
915    (3) loops with one entry and one exit
916    (4) loops without function calls
917    (5) loops without defs that are used after the loop 
918
919    (1), (3) are checked in this function; (2) - in function 
920    vect_analyze_loop_form; (4) - in function vect_analyze_data_refs;
921    (5) is checked as part of the function vect_mark_stmts_to_be_vectorized, 
922    when excluding induction/reduction support.   
923
924    The function returns NULL in case one of these checks or 
925    transformations failed.  */
926    
927 struct loop*
928 tree_duplicate_loop_to_edge (struct loop *loop, struct loops *loops, 
929                              edge e, tree first_niters, 
930                              tree niters, bool update_first_loop_count)
931 {
932   struct loop *new_loop = NULL, *first_loop, *second_loop;
933   edge skip_e;
934   tree pre_condition;
935   bitmap definitions;
936   basic_block first_exit_bb, second_exit_bb;
937   basic_block pre_header_bb;
938   edge exit_e = loop->exit_edges [0];
939
940   gcc_assert (!any_marked_for_rewrite_p ());
941
942   if (!verify_loop_for_duplication (loop, update_first_loop_count, e))
943       return NULL;
944
945   /* We have to initialize cfg_hooks. Then, when calling 
946    cfg_hooks->split_edge, the function tree_split_edge 
947    is actually called and, when calling cfg_hooks->duplicate_block, 
948    the function tree_duplicate_bb is called.  */
949   tree_register_cfg_hooks ();
950
951   /* 1. Generate a copy of LOOP and put it on E (entry or exit).  */
952   if (!(new_loop = tree_duplicate_loop_to_edge_cfg (loop, loops, e)))
953     {
954       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
955         fprintf (dump_file,
956                  "The tree_duplicate_loop_to_edge_cfg failed.\n");
957       return NULL;
958     }
959
960   definitions = marked_ssa_names ();
961   allocate_new_names (definitions);
962   update_phis_for_duplicate_loop (loop, new_loop, e == exit_e);
963   /* Here, using assumption (5), we do not propagate new names further 
964      than on phis of the exit from the second loop.  */
965   rename_variables_in_loop (new_loop);
966   free_new_names (definitions);
967
968   if (e == exit_e)
969     {
970       first_loop = loop;
971       second_loop = new_loop;
972     }
973   else 
974     {
975       first_loop = new_loop;
976       second_loop = loop;
977     }
978
979   /* 2. Generate bb between the loops.  */
980   first_exit_bb = split_edge (first_loop->exit_edges[0]);
981   add_bb_to_loop (first_exit_bb, first_loop->outer);
982
983   /* We need to update here first loop exit edge 
984      and second loop preheader edge.  */
985   flow_loop_scan (first_loop, LOOP_ALL);
986   flow_loop_scan (second_loop, LOOP_ALL);  
987
988   /* 3. Make first loop iterate FIRST_NITERS times, if needed.  */
989   if (!update_first_loop_count)
990     {
991       tree first_loop_latch_lbl = tree_block_label (first_loop->latch);
992       tree first_loop_exit_lbl = tree_block_label (first_exit_bb);
993
994       make_loop_iterate_ntimes (first_loop, first_niters,
995                                 first_loop_latch_lbl,
996                                 first_loop_exit_lbl);
997     }
998   
999   /* 4. Add the guard before first loop:
1000
1001      if FIRST_NITERS == 0 
1002        skip first loop
1003      else 
1004        enter first loop  */
1005
1006   /* 4a. Generate bb before first loop.  */
1007   pre_header_bb = split_edge (loop_preheader_edge (first_loop));
1008   add_bb_to_loop (pre_header_bb, first_loop->outer);
1009
1010   /* First loop preheader edge is changed.  */
1011   flow_loop_scan (first_loop, LOOP_ALL);
1012
1013   /* 4b. Generate guard condition.  */
1014   pre_condition = build (LE_EXPR, boolean_type_node,
1015                            first_niters, integer_zero_node);
1016
1017   /* 4c. Add condition at the end of preheader bb.  */
1018   skip_e = add_loop_guard (pre_header_bb, pre_condition, first_exit_bb);
1019
1020   /* 4d. Update phis at first loop exit and propagate changes 
1021      to the phis of second loop.  */
1022   update_phi_nodes_for_guard (skip_e, first_loop);
1023
1024   /* 5. Add the guard before second loop:
1025
1026      if FIRST_NITERS == NITERS SKIP
1027        skip second loop
1028      else 
1029        enter second loop  */
1030
1031   /* 5a. Generate empty bb at the exit from the second loop.  */
1032   second_exit_bb = split_edge (second_loop->exit_edges[0]);
1033   add_bb_to_loop (second_exit_bb, second_loop->outer);
1034
1035   /* Second loop preheader edge is changed.  */
1036   flow_loop_scan (second_loop, LOOP_ALL);
1037
1038   /* 5b. Generate guard condition.  */
1039   pre_condition = build (EQ_EXPR, boolean_type_node,
1040                            first_niters, niters);
1041
1042   /* 5c. Add condition at the end of preheader bb.  */
1043   skip_e = add_loop_guard (first_exit_bb, pre_condition, second_exit_bb);
1044   update_phi_nodes_for_guard (skip_e, second_loop);
1045
1046   BITMAP_XFREE (definitions);
1047   unmark_all_for_rewrite ();
1048   
1049   return new_loop;
1050 }
1051
1052
1053 \f
1054 /* Here the proper Vectorizer starts.  */
1055
1056 /* Function new_stmt_vec_info.
1057
1058    Create and initialize a new stmt_vec_info struct for STMT.  */
1059
1060 stmt_vec_info
1061 new_stmt_vec_info (tree stmt, struct loop *loop)
1062 {
1063   stmt_vec_info res;
1064   res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
1065
1066   STMT_VINFO_TYPE (res) = undef_vec_info_type;
1067   STMT_VINFO_STMT (res) = stmt;
1068   STMT_VINFO_LOOP (res) = loop;
1069   STMT_VINFO_RELEVANT_P (res) = 0;
1070   STMT_VINFO_VECTYPE (res) = NULL;
1071   STMT_VINFO_VEC_STMT (res) = NULL;
1072   STMT_VINFO_DATA_REF (res) = NULL;
1073   STMT_VINFO_MEMTAG (res) = NULL;
1074   STMT_VINFO_VECT_DR_BASE (res) = NULL;
1075
1076   return res;
1077 }
1078
1079
1080 /* Function new_loop_vec_info.
1081
1082    Create and initialize a new loop_vec_info struct for LOOP, as well as
1083    stmt_vec_info structs for all the stmts in LOOP.  */
1084
1085 loop_vec_info
1086 new_loop_vec_info (struct loop *loop)
1087 {
1088   loop_vec_info res;
1089   basic_block *bbs;
1090   block_stmt_iterator si;
1091   unsigned int i;
1092
1093   res = (loop_vec_info) xcalloc (1, sizeof (struct _loop_vec_info));
1094
1095   bbs = get_loop_body (loop);
1096
1097   /* Create stmt_info for all stmts in the loop.  */
1098   for (i = 0; i < loop->num_nodes; i++)
1099     {
1100       basic_block bb = bbs[i];
1101       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
1102         {
1103           tree stmt = bsi_stmt (si);
1104           stmt_ann_t ann;
1105
1106           get_stmt_operands (stmt);
1107           ann = stmt_ann (stmt);
1108           set_stmt_info (ann, new_stmt_vec_info (stmt, loop));
1109         }
1110     }
1111
1112   LOOP_VINFO_LOOP (res) = loop;
1113   LOOP_VINFO_BBS (res) = bbs;
1114   LOOP_VINFO_EXIT_COND (res) = NULL;
1115   LOOP_VINFO_NITERS (res) = NULL;
1116   LOOP_VINFO_VECTORIZABLE_P (res) = 0;
1117   LOOP_DO_PEELING_FOR_ALIGNMENT (res) = false;
1118   LOOP_VINFO_VECT_FACTOR (res) = 0;
1119   VARRAY_GENERIC_PTR_INIT (LOOP_VINFO_DATAREF_WRITES (res), 20,
1120                            "loop_write_datarefs");
1121   VARRAY_GENERIC_PTR_INIT (LOOP_VINFO_DATAREF_READS (res), 20,
1122                            "loop_read_datarefs");
1123   LOOP_VINFO_UNALIGNED_DR (res) = NULL;
1124
1125   return res;
1126 }
1127
1128
1129 /* Function destroy_loop_vec_info.
1130  
1131    Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the 
1132    stmts in the loop.  */
1133
1134 void
1135 destroy_loop_vec_info (loop_vec_info loop_vinfo)
1136 {
1137   struct loop *loop;
1138   basic_block *bbs;
1139   int nbbs;
1140   block_stmt_iterator si;
1141   int j;
1142
1143   if (!loop_vinfo)
1144     return;
1145
1146   loop = LOOP_VINFO_LOOP (loop_vinfo);
1147
1148   bbs = LOOP_VINFO_BBS (loop_vinfo);
1149   nbbs = loop->num_nodes;
1150
1151   for (j = 0; j < nbbs; j++)
1152     {
1153       basic_block bb = bbs[j];
1154       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
1155         {
1156           tree stmt = bsi_stmt (si);
1157           stmt_ann_t ann = stmt_ann (stmt);
1158           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1159           free (stmt_info);
1160           set_stmt_info (ann, NULL);
1161         }
1162     }
1163
1164   free (LOOP_VINFO_BBS (loop_vinfo));
1165   varray_clear (LOOP_VINFO_DATAREF_WRITES (loop_vinfo));
1166   varray_clear (LOOP_VINFO_DATAREF_READS (loop_vinfo));
1167
1168   free (loop_vinfo);
1169 }
1170
1171
1172 /* Function debug_loop_stats.
1173
1174    For vectorization statistics dumps.  */
1175
1176 static bool
1177 vect_debug_stats (struct loop *loop)
1178 {
1179   basic_block bb;
1180   block_stmt_iterator si;
1181   tree node = NULL_TREE;
1182
1183   if (!dump_file || !(dump_flags & TDF_STATS))
1184     return false;
1185
1186   if (!loop)
1187     {
1188       fprintf (dump_file, "\n");
1189       return true;
1190     }
1191
1192   if (!loop->header)
1193     return false;
1194
1195   bb = loop->header;
1196
1197   for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
1198     {
1199       node = bsi_stmt (si);
1200       if (node && EXPR_P (node) && EXPR_LOCUS (node))
1201         break;
1202     }
1203
1204   if (node && EXPR_P (node) && EXPR_LOCUS (node) 
1205       && EXPR_FILENAME (node) && EXPR_LINENO (node))
1206     {
1207       fprintf (dump_file, "\nloop at %s:%d: ", 
1208         EXPR_FILENAME (node), EXPR_LINENO (node));
1209       return true;
1210     }
1211
1212   return false;
1213 }
1214
1215
1216 /* Function debug_loop_details.
1217
1218    For vectorization debug dumps.  */
1219
1220 static bool
1221 vect_debug_details (struct loop *loop)
1222 {
1223    basic_block bb;
1224    block_stmt_iterator si;
1225    tree node = NULL_TREE;
1226
1227   if (!dump_file || !(dump_flags & TDF_DETAILS))
1228     return false;
1229
1230   if (!loop)
1231     {
1232       fprintf (dump_file, "\n");
1233       return true;
1234     }
1235
1236   if (!loop->header)
1237     return false;
1238
1239   bb = loop->header;
1240
1241   for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
1242     {
1243       node = bsi_stmt (si);
1244       if (node && EXPR_P (node) && EXPR_LOCUS (node))
1245         break;
1246     }
1247
1248   if (node && EXPR_P (node) && EXPR_LOCUS (node)
1249       && EXPR_FILENAME (node) && EXPR_LINENO (node))
1250     {
1251       fprintf (dump_file, "\nloop at %s:%d: ", 
1252                EXPR_FILENAME (node), EXPR_LINENO (node));
1253       return true;
1254     }
1255
1256   return false;
1257 }
1258
1259
1260 /* Function vect_get_ptr_offset
1261
1262    Compute the OFFSET modulo vector-type alignment of pointer REF in bits.  */
1263
1264 static tree 
1265 vect_get_ptr_offset (tree ref ATTRIBUTE_UNUSED, 
1266                      tree vectype ATTRIBUTE_UNUSED, 
1267                      tree *offset ATTRIBUTE_UNUSED)
1268 {
1269   /* TODO: Use alignment information.  */
1270   return NULL_TREE; 
1271 }
1272
1273
1274 /* Function vect_get_base_and_bit_offset
1275
1276    Return the BASE of the data reference EXPR.
1277    If VECTYPE is given, also compute the OFFSET from BASE in bits.
1278    E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset in 
1279    bits of 'a.b[i] + 4B' from a.
1280
1281    Input:
1282    EXPR - the memory reference that is being analyzed
1283    DR - the data_reference struct of the _original_ memory reference
1284         (Note: DR_REF (DR) is not necessarily EXPR)
1285    VECTYPE - the type that defines the alignment (i.e, we compute
1286              alignment relative to TYPE_ALIGN(VECTYPE))
1287    
1288    Output:
1289    BASE (returned value) - the base of the data reference EXPR.
1290                            E.g, if EXPR is a.b[k].c[i][j] the returned
1291                            base is a.
1292    OFFSET - offset of EXPR from BASE in bits
1293    BASE_ALIGNED_P - indicates if BASE is aligned
1294  
1295    If something unexpected is encountered (an unsupported form of data-ref),
1296    or if VECTYPE is given but OFFSET cannot be determined:
1297    then NULL_TREE is returned.  */
1298
1299 static tree 
1300 vect_get_base_and_bit_offset (struct data_reference *dr, 
1301                               tree expr, 
1302                               tree vectype, 
1303                               loop_vec_info loop_vinfo,
1304                               tree *offset,
1305                               bool *base_aligned_p)
1306 {
1307   tree this_offset = size_zero_node;
1308   tree base = NULL_TREE;
1309   tree next_ref;
1310   tree oprnd0, oprnd1;
1311   struct data_reference *array_dr;
1312   enum tree_code code = TREE_CODE (expr);
1313
1314   *base_aligned_p = false;
1315
1316   switch (code)
1317     {
1318     /* These cases end the recursion:  */
1319     case VAR_DECL:
1320       *offset = size_zero_node;
1321       if (vectype && DECL_ALIGN (expr) >= TYPE_ALIGN (vectype))
1322         *base_aligned_p = true;
1323       return expr;
1324
1325     case SSA_NAME:
1326       if (!vectype)
1327         return expr;
1328
1329       if (TREE_CODE (TREE_TYPE (expr)) != POINTER_TYPE)
1330         return NULL_TREE;
1331       
1332       if (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (expr))) < TYPE_ALIGN (vectype)) 
1333         {
1334           base = vect_get_ptr_offset (expr, vectype, offset);
1335           if (base)
1336             *base_aligned_p = true;
1337         }
1338       else
1339         {         
1340           *base_aligned_p = true;
1341           *offset = size_zero_node;
1342           base = expr;
1343         }
1344       return base;
1345       
1346     case INTEGER_CST:      
1347       *offset = int_const_binop (MULT_EXPR, expr,     
1348                                  build_int_cst (NULL_TREE, BITS_PER_UNIT), 1);
1349       return expr;
1350
1351     /* These cases continue the recursion:  */
1352     case COMPONENT_REF:
1353       oprnd0 = TREE_OPERAND (expr, 0);
1354       oprnd1 = TREE_OPERAND (expr, 1);
1355
1356       this_offset = bit_position (oprnd1);
1357       if (vectype && !host_integerp (this_offset, 1))
1358         return NULL_TREE;
1359       next_ref = oprnd0;
1360       break;
1361
1362     case ADDR_EXPR:
1363       oprnd0 = TREE_OPERAND (expr, 0);
1364       next_ref = oprnd0;
1365       break;
1366
1367     case INDIRECT_REF:
1368       oprnd0 = TREE_OPERAND (expr, 0);
1369       next_ref = oprnd0;
1370       break;
1371     
1372     case ARRAY_REF:
1373       if (DR_REF (dr) != expr)
1374         /* Build array data_reference struct if the existing DR_REF 
1375            doesn't match EXPR. This happens, for example, when the 
1376            EXPR is *T and T is initialized to &arr[indx]. The DR struct
1377            contains information on the access of T, not of arr. In order
1378            to continue  the analysis, we create a new DR struct that
1379            describes the access of arr.  
1380         */
1381         array_dr = analyze_array (DR_STMT (dr), expr, DR_IS_READ (dr));
1382       else
1383         array_dr = dr;
1384           
1385       next_ref = vect_compute_array_ref_alignment (array_dr, loop_vinfo,  
1386                                                    vectype, &this_offset);
1387       if (!next_ref)
1388         return NULL_TREE;
1389
1390       if (vectype &&
1391           TYPE_ALIGN (TREE_TYPE (TREE_TYPE (next_ref))) >= TYPE_ALIGN (vectype))
1392         {
1393           *offset = this_offset;
1394           *base_aligned_p = true;
1395           return next_ref;
1396         }
1397       break;
1398
1399     case PLUS_EXPR:
1400     case MINUS_EXPR:
1401       /* In case we have a PLUS_EXPR of the form
1402          (oprnd0 + oprnd1), we assume that only oprnd0 determines the base. 
1403          This is verified in  vect_get_symbl_and_dr.  */ 
1404       oprnd0 = TREE_OPERAND (expr, 0);
1405       oprnd1 = TREE_OPERAND (expr, 1);
1406
1407       base = vect_get_base_and_bit_offset 
1408         (dr, oprnd1, vectype, loop_vinfo, &this_offset, base_aligned_p);  
1409       if (vectype && !base) 
1410         return NULL_TREE;
1411
1412       next_ref = oprnd0;
1413       break;
1414
1415     default:
1416       return NULL_TREE;
1417     }
1418
1419   base = vect_get_base_and_bit_offset (dr, next_ref, vectype, 
1420                                        loop_vinfo, offset, base_aligned_p);  
1421
1422   if (vectype && base)
1423     {
1424       *offset = int_const_binop (PLUS_EXPR, *offset, this_offset, 1);
1425       if (!host_integerp (*offset, 1) || TREE_OVERFLOW (*offset))
1426         return NULL_TREE;
1427
1428       if (vect_debug_details (NULL))
1429         {
1430           print_generic_expr (dump_file, expr, TDF_SLIM);
1431           fprintf (dump_file, " --> total offset for ref: ");
1432           print_generic_expr (dump_file, *offset, TDF_SLIM);
1433         }
1434     }    
1435   return base;
1436 }
1437
1438
1439 /* Function vect_force_dr_alignment_p.
1440
1441    Returns whether the alignment of a DECL can be forced to be aligned
1442    on ALIGNMENT bit boundary.  */
1443
1444 static bool 
1445 vect_can_force_dr_alignment_p (tree decl, unsigned int alignment)
1446 {
1447   if (TREE_CODE (decl) != VAR_DECL)
1448     return false;
1449
1450   if (DECL_EXTERNAL (decl))
1451     return false;
1452
1453   if (TREE_STATIC (decl))
1454     return (alignment <= MAX_OFILE_ALIGNMENT);
1455   else
1456     /* This is not 100% correct.  The absolute correct stack alignment
1457        is STACK_BOUNDARY.  We're supposed to hope, but not assume, that
1458        PREFERRED_STACK_BOUNDARY is honored by all translation units.
1459        However, until someone implements forced stack alignment, SSE
1460        isn't really usable without this.  */  
1461     return (alignment <= PREFERRED_STACK_BOUNDARY); 
1462 }
1463
1464
1465 /* Function vect_get_new_vect_var.
1466
1467    Returns a name for a new variable. The current naming scheme appends the 
1468    prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to 
1469    the name of vectorizer generated variables, and appends that to NAME if 
1470    provided.  */
1471
1472 static tree
1473 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
1474 {
1475   const char *prefix;
1476   int prefix_len;
1477   tree new_vect_var;
1478
1479   if (var_kind == vect_simple_var)
1480     prefix = "vect_"; 
1481   else
1482     prefix = "vect_p";
1483
1484   prefix_len = strlen (prefix);
1485
1486   if (name)
1487     new_vect_var = create_tmp_var (type, concat (prefix, name, NULL));
1488   else
1489     new_vect_var = create_tmp_var (type, prefix);
1490
1491   return new_vect_var;
1492 }
1493
1494
1495 /* Function vect_create_index_for_vector_ref.
1496
1497    Create (and return) an index variable, along with it's update chain in the
1498    loop. This variable will be used to access a memory location in a vector
1499    operation.
1500
1501    Input:
1502    LOOP: The loop being vectorized.
1503    BSI: The block_stmt_iterator where STMT is. Any new stmts created by this
1504         function can be added here, or in the loop pre-header.
1505
1506    Output:
1507    Return an index that will be used to index a vector array.  It is expected
1508    that a pointer to the first vector will be used as the base address for the
1509    indexed reference.
1510
1511    FORNOW: we are not trying to be efficient, just creating a new index each
1512    time from scratch.  At this time all vector references could use the same
1513    index.
1514
1515    TODO: create only one index to be used by all vector references.  Record
1516    the index in the LOOP_VINFO the first time this procedure is called and
1517    return it on subsequent calls.  The increment of this index must be placed
1518    just before the conditional expression that ends the single block loop.  */
1519
1520 static tree
1521 vect_create_index_for_vector_ref (struct loop *loop, block_stmt_iterator *bsi)
1522 {
1523   tree init, step;
1524   tree indx_before_incr, indx_after_incr;
1525
1526   /* It is assumed that the base pointer used for vectorized access contains
1527      the address of the first vector.  Therefore the index used for vectorized
1528      access must be initialized to zero and incremented by 1.  */
1529
1530   init = integer_zero_node;
1531   step = integer_one_node;
1532
1533   /* Assuming that bsi_insert is used with BSI_NEW_STMT  */
1534   create_iv (init, step, NULL_TREE, loop, bsi, false,
1535         &indx_before_incr, &indx_after_incr);
1536
1537   return indx_before_incr;
1538 }
1539
1540
1541 /* Function vect_create_addr_base_for_vector_ref.
1542
1543    Create an expression that computes the address of the first memory location
1544    that will be accessed for a data reference.
1545
1546    Input:
1547    STMT: The statement containing the data reference.
1548    NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
1549    OFFSET: Optional. If supplied, it is be added to the initial address.
1550
1551    Output:
1552    1. Return an SSA_NAME whose value is the address of the memory location of 
1553       the first vector of the data reference.
1554    2. If new_stmt_list is not NULL_TREE after return then the caller must insert
1555       these statement(s) which define the returned SSA_NAME.
1556
1557    FORNOW: We are only handling array accesses with step 1.  */
1558
1559 static tree
1560 vect_create_addr_base_for_vector_ref (tree stmt,
1561                                       tree *new_stmt_list,
1562                                       tree offset)
1563 {
1564   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1565   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
1566   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1567   tree data_ref_base = unshare_expr (STMT_VINFO_VECT_DR_BASE (stmt_info));
1568   tree base_name = unshare_expr (DR_BASE_NAME (dr));
1569   tree ref = DR_REF (dr);
1570   tree data_ref_base_type = TREE_TYPE (data_ref_base);
1571   tree scalar_type = TREE_TYPE (ref);
1572   tree scalar_ptr_type = build_pointer_type (scalar_type);
1573   tree access_fn;
1574   tree init_val, step, init_oval;
1575   bool ok;
1576   bool is_ptr_ref, is_array_ref, is_addr_expr;
1577   tree array_base;
1578   tree vec_stmt;
1579   tree new_temp;
1580   tree array_ref;
1581   tree addr_base, addr_expr;
1582   tree dest, new_stmt;
1583
1584   /* Only the access function of the last index is relevant (i_n in
1585      a[i_1][i_2]...[i_n]), the others correspond to loop invariants.  */
1586   access_fn = DR_ACCESS_FN (dr, 0);
1587   ok = vect_is_simple_iv_evolution (loop->num, access_fn, &init_oval, &step, 
1588                                     true);
1589   if (!ok)
1590     init_oval = integer_zero_node;
1591
1592   is_ptr_ref = TREE_CODE (data_ref_base_type) == POINTER_TYPE
1593                && TREE_CODE (data_ref_base) == SSA_NAME;
1594   is_array_ref = TREE_CODE (data_ref_base_type) == ARRAY_TYPE;
1595   is_addr_expr = TREE_CODE (data_ref_base) == ADDR_EXPR
1596                  || TREE_CODE (data_ref_base) == PLUS_EXPR
1597                  || TREE_CODE (data_ref_base) == MINUS_EXPR;
1598   gcc_assert (is_ptr_ref || is_array_ref || is_addr_expr);
1599
1600   /** Create: &(base[init_val])
1601
1602       if data_ref_base is an ARRAY_TYPE:
1603          base = data_ref_base
1604
1605       if data_ref_base is the SSA_NAME of a POINTER_TYPE:
1606          base = *((scalar_array *) data_ref_base)
1607    **/
1608
1609   if (is_array_ref)
1610     array_base = data_ref_base;
1611   else /* is_ptr_ref  or is_addr_expr */
1612     {
1613       /* array_ptr = (scalar_array_ptr_type *) data_ref_base;  */
1614       tree scalar_array_type = build_array_type (scalar_type, 0);
1615       tree scalar_array_ptr_type = build_pointer_type (scalar_array_type);
1616       tree array_ptr = create_tmp_var (scalar_array_ptr_type, "array_ptr");
1617       add_referenced_tmp_var (array_ptr);
1618
1619       dest = create_tmp_var (TREE_TYPE (data_ref_base), "dataref");
1620       add_referenced_tmp_var (dest);
1621       data_ref_base = 
1622         force_gimple_operand (data_ref_base, &new_stmt, false, dest);  
1623       append_to_statement_list_force (new_stmt, new_stmt_list);
1624
1625       vec_stmt = fold_convert (scalar_array_ptr_type, data_ref_base);
1626       vec_stmt = build2 (MODIFY_EXPR, void_type_node, array_ptr, vec_stmt);
1627       new_temp = make_ssa_name (array_ptr, vec_stmt);
1628       TREE_OPERAND (vec_stmt, 0) = new_temp;
1629       append_to_statement_list_force (vec_stmt, new_stmt_list);
1630
1631       /* (*array_ptr)  */
1632       array_base = build_fold_indirect_ref (new_temp);
1633     }
1634
1635   dest = create_tmp_var (TREE_TYPE (init_oval), "newinit");
1636   add_referenced_tmp_var (dest);
1637   init_val = force_gimple_operand (init_oval, &new_stmt, false, dest);  
1638   append_to_statement_list_force (new_stmt, new_stmt_list);
1639
1640   if (offset)
1641     {
1642       tree tmp = create_tmp_var (TREE_TYPE (init_val), "offset");
1643       add_referenced_tmp_var (tmp);
1644       vec_stmt = build2 (PLUS_EXPR, TREE_TYPE (init_val), init_val, offset);
1645       vec_stmt = build2 (MODIFY_EXPR, TREE_TYPE (init_val), tmp, vec_stmt);
1646       init_val = make_ssa_name (tmp, vec_stmt);
1647       TREE_OPERAND (vec_stmt, 0) = init_val;
1648       append_to_statement_list_force (vec_stmt, new_stmt_list);
1649     }
1650
1651   array_ref = build4 (ARRAY_REF, scalar_type, array_base, init_val, 
1652                       NULL_TREE, NULL_TREE);
1653   addr_base = build_fold_addr_expr (array_ref);
1654
1655   /* addr_expr = addr_base */
1656   addr_expr = vect_get_new_vect_var (scalar_ptr_type, vect_pointer_var,
1657                                      get_name (base_name));
1658   add_referenced_tmp_var (addr_expr);
1659   vec_stmt = build2 (MODIFY_EXPR, void_type_node, addr_expr, addr_base);
1660   new_temp = make_ssa_name (addr_expr, vec_stmt);
1661   TREE_OPERAND (vec_stmt, 0) = new_temp;
1662   append_to_statement_list_force (vec_stmt, new_stmt_list);
1663
1664   return new_temp;
1665 }
1666
1667
1668 /* Function get_vectype_for_scalar_type.
1669
1670    Returns the vector type corresponding to SCALAR_TYPE as supported
1671    by the target.  */
1672
1673 static tree
1674 get_vectype_for_scalar_type (tree scalar_type)
1675 {
1676   enum machine_mode inner_mode = TYPE_MODE (scalar_type);
1677   int nbytes = GET_MODE_SIZE (inner_mode);
1678   int nunits;
1679   tree vectype;
1680
1681   if (nbytes == 0)
1682     return NULL_TREE;
1683
1684   /* FORNOW: Only a single vector size per target (UNITS_PER_SIMD_WORD)
1685      is expected.  */
1686   nunits = UNITS_PER_SIMD_WORD / nbytes;
1687
1688   vectype = build_vector_type (scalar_type, nunits);
1689   if (vect_debug_details (NULL))
1690     {
1691       fprintf (dump_file, "get vectype with %d units of type ", nunits);
1692       print_generic_expr (dump_file, scalar_type, TDF_SLIM);
1693     }
1694
1695   if (!vectype)
1696     return NULL_TREE;
1697
1698   if (vect_debug_details (NULL))
1699     {
1700       fprintf (dump_file, "vectype: ");
1701       print_generic_expr (dump_file, vectype, TDF_SLIM);
1702     }
1703
1704   if (!VECTOR_MODE_P (TYPE_MODE (vectype)))
1705     {
1706       /* TODO: tree-complex.c sometimes can parallelize operations
1707          on generic vectors.  We can vectorize the loop in that case,
1708          but then we should re-run the lowering pass.  */
1709       if (vect_debug_details (NULL))
1710         fprintf (dump_file, "mode not supported by target.");
1711       return NULL_TREE;
1712     }
1713
1714   return vectype;
1715 }
1716
1717
1718 /* Function vect_align_data_ref.
1719
1720    Handle mislignment of a memory accesses.
1721
1722    FORNOW: Can't handle misaligned accesses. 
1723    Make sure that the dataref is aligned.  */
1724
1725 static void
1726 vect_align_data_ref (tree stmt)
1727 {
1728   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1729   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1730
1731   /* FORNOW: can't handle misaligned accesses; 
1732              all accesses expected to be aligned.  */
1733   gcc_assert (aligned_access_p (dr));
1734 }
1735
1736
1737 /* Function vect_create_data_ref_ptr.
1738
1739    Create a memory reference expression for vector access, to be used in a
1740    vector load/store stmt. The reference is based on a new pointer to vector
1741    type (vp).
1742
1743    Input:
1744    1. STMT: a stmt that references memory. Expected to be of the form
1745          MODIFY_EXPR <name, data-ref> or MODIFY_EXPR <data-ref, name>.
1746    2. BSI: block_stmt_iterator where new stmts can be added.
1747    3. OFFSET (optional): an offset to be added to the initial address accessed
1748         by the data-ref in STMT.
1749    4. ONLY_INIT: indicate if vp is to be updated in the loop, or remain
1750         pointing to the initial address.
1751
1752    Output:
1753    1. Declare a new ptr to vector_type, and have it point to the base of the
1754       data reference (initial addressed accessed by the data reference).
1755       For example, for vector of type V8HI, the following code is generated:
1756
1757       v8hi *vp;
1758       vp = (v8hi *)initial_address;
1759
1760       if OFFSET is not supplied:
1761          initial_address = &a[init];
1762       if OFFSET is supplied:
1763          initial_address = &a[init + OFFSET];
1764
1765       Return the initial_address in INITIAL_ADDRESS.
1766
1767    2. Create a data-reference in the loop based on the new vector pointer vp,
1768       and using a new index variable 'idx' as follows:
1769
1770       vp' = vp + update
1771
1772       where if ONLY_INIT is true:
1773          update = zero
1774       and otherwise
1775          update = idx + vector_type_size
1776
1777       Return the pointer vp'.
1778
1779
1780    FORNOW: handle only aligned and consecutive accesses.  */
1781
1782 static tree
1783 vect_create_data_ref_ptr (tree stmt, block_stmt_iterator *bsi, tree offset,
1784                           tree *initial_address, bool only_init)
1785 {
1786   tree base_name;
1787   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1788   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1789   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
1790   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1791   tree vect_ptr_type;
1792   tree vect_ptr;
1793   tree tag;
1794   v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
1795   v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
1796   vuse_optype vuses = STMT_VUSE_OPS (stmt);
1797   int nvuses, nv_may_defs, nv_must_defs;
1798   int i;
1799   tree new_temp;
1800   tree vec_stmt;
1801   tree new_stmt_list = NULL_TREE;
1802   tree idx;
1803   edge pe = loop_preheader_edge (loop);
1804   basic_block new_bb;
1805   tree vect_ptr_init;
1806   tree vectype_size;
1807   tree ptr_update;
1808   tree data_ref_ptr;
1809
1810   base_name = unshare_expr (DR_BASE_NAME (dr));
1811   if (vect_debug_details (NULL))
1812     {
1813       tree data_ref_base = base_name;
1814       fprintf (dump_file, "create array_ref of type: ");
1815       print_generic_expr (dump_file, vectype, TDF_SLIM);
1816       if (TREE_CODE (data_ref_base) == VAR_DECL)
1817         fprintf (dump_file, "vectorizing a one dimensional array ref: ");
1818       else if (TREE_CODE (data_ref_base) == ARRAY_REF)
1819         fprintf (dump_file, "vectorizing a multidimensional array ref: ");
1820       else if (TREE_CODE (data_ref_base) == COMPONENT_REF)
1821         fprintf (dump_file, "vectorizing a record based array ref: ");
1822       else if (TREE_CODE (data_ref_base) == SSA_NAME)
1823         fprintf (dump_file, "vectorizing a pointer ref: ");
1824       print_generic_expr (dump_file, base_name, TDF_SLIM);
1825     }
1826
1827   /** (1) Create the new vector-pointer variable:  **/
1828
1829   vect_ptr_type = build_pointer_type (vectype);
1830   vect_ptr = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var,
1831                                     get_name (base_name));
1832   add_referenced_tmp_var (vect_ptr);
1833   
1834   
1835   /** (2) Handle aliasing information of the new vector-pointer:  **/
1836   
1837   tag = STMT_VINFO_MEMTAG (stmt_info);
1838   gcc_assert (tag);
1839   get_var_ann (vect_ptr)->type_mem_tag = tag;
1840   
1841   /* Mark for renaming all aliased variables
1842      (i.e, the may-aliases of the type-mem-tag).  */
1843   nvuses = NUM_VUSES (vuses);
1844   nv_may_defs = NUM_V_MAY_DEFS (v_may_defs);
1845   nv_must_defs = NUM_V_MUST_DEFS (v_must_defs);
1846   for (i = 0; i < nvuses; i++)
1847     {
1848       tree use = VUSE_OP (vuses, i);
1849       if (TREE_CODE (use) == SSA_NAME)
1850         bitmap_set_bit (vars_to_rename, var_ann (SSA_NAME_VAR (use))->uid);
1851     }
1852   for (i = 0; i < nv_may_defs; i++)
1853     {
1854       tree def = V_MAY_DEF_RESULT (v_may_defs, i);
1855       if (TREE_CODE (def) == SSA_NAME)
1856         bitmap_set_bit (vars_to_rename, var_ann (SSA_NAME_VAR (def))->uid);
1857     }
1858   for (i = 0; i < nv_must_defs; i++)
1859     {
1860       tree def = V_MUST_DEF_RESULT (v_must_defs, i);
1861       if (TREE_CODE (def) == SSA_NAME)
1862         bitmap_set_bit (vars_to_rename, var_ann (SSA_NAME_VAR (def))->uid);
1863     }
1864
1865
1866   /** (3) Calculate the initial address the vector-pointer, and set
1867           the vector-pointer to point to it before the loop:  **/
1868
1869   /* Create: (&(base[init_val+offset]) in the loop preheader.  */
1870   new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
1871                                                    offset);
1872   pe = loop_preheader_edge (loop);
1873   new_bb = bsi_insert_on_edge_immediate (pe, new_stmt_list);
1874   gcc_assert (!new_bb);
1875   *initial_address = new_temp;
1876
1877   /* Create: p = (vectype *) initial_base  */
1878   vec_stmt = fold_convert (vect_ptr_type, new_temp);
1879   vec_stmt = build2 (MODIFY_EXPR, void_type_node, vect_ptr, vec_stmt);
1880   new_temp = make_ssa_name (vect_ptr, vec_stmt);
1881   TREE_OPERAND (vec_stmt, 0) = new_temp;
1882   new_bb = bsi_insert_on_edge_immediate (pe, vec_stmt);
1883   gcc_assert (!new_bb);
1884   vect_ptr_init = TREE_OPERAND (vec_stmt, 0);
1885
1886
1887   /** (4) Handle the updating of the vector-pointer inside the loop: **/
1888
1889   if (only_init) /* No update in loop is required.  */
1890     return vect_ptr_init;
1891
1892   idx = vect_create_index_for_vector_ref (loop, bsi);
1893
1894   /* Create: update = idx * vectype_size  */
1895   ptr_update = create_tmp_var (integer_type_node, "update");
1896   add_referenced_tmp_var (ptr_update);
1897   vectype_size = build_int_cst (integer_type_node,
1898                                 GET_MODE_SIZE (TYPE_MODE (vectype)));
1899   vec_stmt = build2 (MULT_EXPR, integer_type_node, idx, vectype_size);
1900   vec_stmt = build2 (MODIFY_EXPR, void_type_node, ptr_update, vec_stmt);
1901   new_temp = make_ssa_name (ptr_update, vec_stmt);
1902   TREE_OPERAND (vec_stmt, 0) = new_temp;
1903   bsi_insert_before (bsi, vec_stmt, BSI_SAME_STMT);
1904
1905   /* Create: data_ref_ptr = vect_ptr_init + update  */
1906   vec_stmt = build2 (PLUS_EXPR, vect_ptr_type, vect_ptr_init, new_temp);
1907   vec_stmt = build2 (MODIFY_EXPR, void_type_node, vect_ptr, vec_stmt);
1908   new_temp = make_ssa_name (vect_ptr, vec_stmt);
1909   TREE_OPERAND (vec_stmt, 0) = new_temp;
1910   bsi_insert_before (bsi, vec_stmt, BSI_SAME_STMT);
1911   data_ref_ptr = TREE_OPERAND (vec_stmt, 0);
1912
1913   return data_ref_ptr;
1914 }
1915
1916
1917 /* Function vect_create_destination_var.
1918
1919    Create a new temporary of type VECTYPE.  */
1920
1921 static tree
1922 vect_create_destination_var (tree scalar_dest, tree vectype)
1923 {
1924   tree vec_dest;
1925   const char *new_name;
1926
1927   gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
1928
1929   new_name = get_name (scalar_dest);
1930   if (!new_name)
1931     new_name = "var_";
1932   vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, new_name);
1933   add_referenced_tmp_var (vec_dest);
1934
1935   return vec_dest;
1936 }
1937
1938
1939 /* Function vect_init_vector.
1940
1941    Insert a new stmt (INIT_STMT) that initializes a new vector variable with
1942    the vector elements of VECTOR_VAR. Return the DEF of INIT_STMT. It will be
1943    used in the vectorization of STMT.  */
1944
1945 static tree
1946 vect_init_vector (tree stmt, tree vector_var)
1947 {
1948   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1949   struct loop *loop = STMT_VINFO_LOOP (stmt_vinfo);
1950   tree new_var;
1951   tree init_stmt;
1952   tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo); 
1953   tree vec_oprnd;
1954   edge pe;
1955   tree new_temp;
1956   basic_block new_bb;
1957  
1958   new_var = vect_get_new_vect_var (vectype, vect_simple_var, "cst_");
1959   add_referenced_tmp_var (new_var); 
1960  
1961   init_stmt = build2 (MODIFY_EXPR, vectype, new_var, vector_var);
1962   new_temp = make_ssa_name (new_var, init_stmt);
1963   TREE_OPERAND (init_stmt, 0) = new_temp;
1964
1965   pe = loop_preheader_edge (loop);
1966   new_bb = bsi_insert_on_edge_immediate (pe, init_stmt);
1967   gcc_assert (!new_bb);
1968
1969   if (vect_debug_details (NULL))
1970     {
1971       fprintf (dump_file, "created new init_stmt: ");
1972       print_generic_expr (dump_file, init_stmt, TDF_SLIM);
1973     }
1974
1975   vec_oprnd = TREE_OPERAND (init_stmt, 0);
1976   return vec_oprnd;
1977 }
1978
1979
1980 /* Function vect_get_vec_def_for_operand.
1981
1982    OP is an operand in STMT. This function returns a (vector) def that will be
1983    used in the vectorized stmt for STMT.
1984
1985    In the case that OP is an SSA_NAME which is defined in the loop, then
1986    STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1987
1988    In case OP is an invariant or constant, a new stmt that creates a vector def
1989    needs to be introduced.  */
1990
1991 static tree
1992 vect_get_vec_def_for_operand (tree op, tree stmt)
1993 {
1994   tree vec_oprnd;
1995   tree vec_stmt;
1996   tree def_stmt;
1997   stmt_vec_info def_stmt_info = NULL;
1998   stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1999   tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
2000   int nunits = GET_MODE_NUNITS (TYPE_MODE (vectype));
2001   struct loop *loop = STMT_VINFO_LOOP (stmt_vinfo);
2002   basic_block bb;
2003   tree vec_inv;
2004   tree t = NULL_TREE;
2005   tree def;
2006   int i;
2007
2008   if (vect_debug_details (NULL))
2009     {
2010       fprintf (dump_file, "vect_get_vec_def_for_operand: ");
2011       print_generic_expr (dump_file, op, TDF_SLIM);
2012     }
2013
2014   /** ===> Case 1: operand is a constant.  **/
2015
2016   if (TREE_CODE (op) == INTEGER_CST || TREE_CODE (op) == REAL_CST)
2017     {
2018       /* Create 'vect_cst_ = {cst,cst,...,cst}'  */
2019
2020       tree vec_cst;
2021
2022       /* Build a tree with vector elements.  */
2023       if (vect_debug_details (NULL))
2024         fprintf (dump_file, "Create vector_cst. nunits = %d", nunits);
2025
2026       for (i = nunits - 1; i >= 0; --i)
2027         {
2028           t = tree_cons (NULL_TREE, op, t);
2029         }
2030       vec_cst = build_vector (vectype, t);
2031       return vect_init_vector (stmt, vec_cst);
2032     }
2033
2034   gcc_assert (TREE_CODE (op) == SSA_NAME);
2035  
2036   /** ===> Case 2: operand is an SSA_NAME - find the stmt that defines it.  **/
2037
2038   def_stmt = SSA_NAME_DEF_STMT (op);
2039   def_stmt_info = vinfo_for_stmt (def_stmt);
2040
2041   if (vect_debug_details (NULL))
2042     {
2043       fprintf (dump_file, "vect_get_vec_def_for_operand: def_stmt: ");
2044       print_generic_expr (dump_file, def_stmt, TDF_SLIM);
2045     }
2046
2047
2048   /** ==> Case 2.1: operand is defined inside the loop.  **/
2049
2050   if (def_stmt_info)
2051     {
2052       /* Get the def from the vectorized stmt.  */
2053
2054       vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
2055       gcc_assert (vec_stmt);
2056       vec_oprnd = TREE_OPERAND (vec_stmt, 0);
2057       return vec_oprnd;
2058     }
2059
2060
2061   /** ==> Case 2.2: operand is defined by the loop-header phi-node - 
2062                     it is a reduction/induction.  **/
2063
2064   bb = bb_for_stmt (def_stmt);
2065   if (TREE_CODE (def_stmt) == PHI_NODE && flow_bb_inside_loop_p (loop, bb))
2066     {
2067       if (vect_debug_details (NULL))
2068         fprintf (dump_file, "reduction/induction - unsupported.");
2069       internal_error ("no support for reduction/induction"); /* FORNOW */
2070     }
2071
2072
2073   /** ==> Case 2.3: operand is defined outside the loop - 
2074                     it is a loop invariant.  */
2075
2076   switch (TREE_CODE (def_stmt))
2077     {
2078     case PHI_NODE:
2079       def = PHI_RESULT (def_stmt);
2080       break;
2081     case MODIFY_EXPR:
2082       def = TREE_OPERAND (def_stmt, 0);
2083       break;
2084     case NOP_EXPR:
2085       def = TREE_OPERAND (def_stmt, 0);
2086       gcc_assert (IS_EMPTY_STMT (def_stmt));
2087       def = op;
2088       break;
2089     default:
2090       if (vect_debug_details (NULL))
2091         {
2092           fprintf (dump_file, "unsupported defining stmt: ");
2093           print_generic_expr (dump_file, def_stmt, TDF_SLIM);
2094         }
2095       internal_error ("unsupported defining stmt");
2096     }
2097
2098   /* Build a tree with vector elements. Create 'vec_inv = {inv,inv,..,inv}'  */
2099
2100   if (vect_debug_details (NULL))
2101     fprintf (dump_file, "Create vector_inv.");
2102
2103   for (i = nunits - 1; i >= 0; --i)
2104     {
2105       t = tree_cons (NULL_TREE, def, t);
2106     }
2107
2108   vec_inv = build_constructor (vectype, t);
2109   return vect_init_vector (stmt, vec_inv);
2110 }
2111
2112
2113 /* Function vect_finish_stmt_generation.
2114
2115    Insert a new stmt.  */
2116
2117 static void
2118 vect_finish_stmt_generation (tree stmt, tree vec_stmt, block_stmt_iterator *bsi)
2119 {
2120   bsi_insert_before (bsi, vec_stmt, BSI_SAME_STMT);
2121
2122   if (vect_debug_details (NULL))
2123     {
2124       fprintf (dump_file, "add new stmt: ");
2125       print_generic_expr (dump_file, vec_stmt, TDF_SLIM);
2126     }
2127
2128   /* Make sure bsi points to the stmt that is being vectorized.  */
2129
2130   /* Assumption: any stmts created for the vectorization of stmt S were
2131      inserted before S. BSI is expected to point to S or some new stmt before S.  */
2132
2133   while (stmt != bsi_stmt (*bsi) && !bsi_end_p (*bsi))
2134     bsi_next (bsi);
2135   gcc_assert (stmt == bsi_stmt (*bsi));
2136 }
2137
2138
2139 /* Function vectorizable_assignment.
2140
2141    Check if STMT performs an assignment (copy) that can be vectorized. 
2142    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
2143    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2144    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
2145
2146 static bool
2147 vectorizable_assignment (tree stmt, block_stmt_iterator *bsi, tree *vec_stmt)
2148 {
2149   tree vec_dest;
2150   tree scalar_dest;
2151   tree op;
2152   tree vec_oprnd;
2153   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2154   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2155   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
2156   tree new_temp;
2157
2158   /* Is vectorizable assignment?  */
2159
2160   if (TREE_CODE (stmt) != MODIFY_EXPR)
2161     return false;
2162
2163   scalar_dest = TREE_OPERAND (stmt, 0);
2164   if (TREE_CODE (scalar_dest) != SSA_NAME)
2165     return false;
2166
2167   op = TREE_OPERAND (stmt, 1);
2168   if (!vect_is_simple_use (op, loop, NULL))
2169     {
2170       if (vect_debug_details (NULL))
2171         fprintf (dump_file, "use not simple.");
2172       return false;
2173     }
2174
2175   if (!vec_stmt) /* transformation not required.  */
2176     {
2177       STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
2178       return true;
2179     }
2180
2181   /** Trasform.  **/
2182   if (vect_debug_details (NULL))
2183     fprintf (dump_file, "transform assignment.");
2184
2185   /* Handle def.  */
2186   vec_dest = vect_create_destination_var (scalar_dest, vectype);
2187
2188   /* Handle use.  */
2189   op = TREE_OPERAND (stmt, 1);
2190   vec_oprnd = vect_get_vec_def_for_operand (op, stmt);
2191
2192   /* Arguments are ready. create the new vector stmt.  */
2193   *vec_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, vec_oprnd);
2194   new_temp = make_ssa_name (vec_dest, *vec_stmt);
2195   TREE_OPERAND (*vec_stmt, 0) = new_temp;
2196   vect_finish_stmt_generation (stmt, *vec_stmt, bsi);
2197   
2198   return true;
2199 }
2200
2201
2202 /* Function vectorizable_operation.
2203
2204    Check if STMT performs a binary or unary operation that can be vectorized. 
2205    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
2206    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2207    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
2208
2209 static bool
2210 vectorizable_operation (tree stmt, block_stmt_iterator *bsi, tree *vec_stmt)
2211 {
2212   tree vec_dest;
2213   tree scalar_dest;
2214   tree operation;
2215   tree op0, op1 = NULL;
2216   tree vec_oprnd0, vec_oprnd1=NULL;
2217   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2218   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2219   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
2220   int i;
2221   enum tree_code code;
2222   enum machine_mode vec_mode;
2223   tree new_temp;
2224   int op_type;
2225   tree op;
2226   optab optab;
2227
2228   /* Is STMT a vectorizable binary/unary operation?   */
2229   if (TREE_CODE (stmt) != MODIFY_EXPR)
2230     return false;
2231
2232   if (TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
2233     return false;
2234
2235   operation = TREE_OPERAND (stmt, 1);
2236   code = TREE_CODE (operation);
2237   optab = optab_for_tree_code (code, vectype);
2238
2239   /* Support only unary or binary operations.  */
2240   op_type = TREE_CODE_LENGTH (code);
2241   if (op_type != unary_op && op_type != binary_op)
2242     {
2243       if (vect_debug_details (NULL))
2244         fprintf (dump_file, "num. args = %d (not unary/binary op).", op_type);
2245       return false;
2246     }
2247
2248   for (i = 0; i < op_type; i++)
2249     {
2250       op = TREE_OPERAND (operation, i);
2251       if (!vect_is_simple_use (op, loop, NULL))
2252         {
2253           if (vect_debug_details (NULL))
2254             fprintf (dump_file, "use not simple.");
2255           return false;
2256         }       
2257     } 
2258
2259   /* Supportable by target?  */
2260   if (!optab)
2261     {
2262       if (vect_debug_details (NULL))
2263         fprintf (dump_file, "no optab.");
2264       return false;
2265     }
2266   vec_mode = TYPE_MODE (vectype);
2267   if (optab->handlers[(int) vec_mode].insn_code == CODE_FOR_nothing)
2268     {
2269       if (vect_debug_details (NULL))
2270         fprintf (dump_file, "op not supported by target.");
2271       return false;
2272     }
2273
2274   if (!vec_stmt) /* transformation not required.  */
2275     {
2276       STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
2277       return true;
2278     }
2279
2280   /** Transform.  **/
2281
2282   if (vect_debug_details (NULL))
2283     fprintf (dump_file, "transform binary/unary operation.");
2284
2285   /* Handle def.  */
2286   scalar_dest = TREE_OPERAND (stmt, 0);
2287   vec_dest = vect_create_destination_var (scalar_dest, vectype);
2288
2289   /* Handle uses.  */
2290   op0 = TREE_OPERAND (operation, 0);
2291   vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt);
2292
2293   if (op_type == binary_op)
2294     {
2295       op1 = TREE_OPERAND (operation, 1);
2296       vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt); 
2297     }
2298
2299   /* Arguments are ready. create the new vector stmt.  */
2300
2301   if (op_type == binary_op)
2302     *vec_stmt = build2 (MODIFY_EXPR, vectype, vec_dest,
2303                 build2 (code, vectype, vec_oprnd0, vec_oprnd1));
2304   else
2305     *vec_stmt = build2 (MODIFY_EXPR, vectype, vec_dest,
2306                 build1 (code, vectype, vec_oprnd0));
2307   new_temp = make_ssa_name (vec_dest, *vec_stmt);
2308   TREE_OPERAND (*vec_stmt, 0) = new_temp;
2309   vect_finish_stmt_generation (stmt, *vec_stmt, bsi);
2310
2311   return true;
2312 }
2313
2314
2315 /* Function vectorizable_store.
2316
2317    Check if STMT defines a non scalar data-ref (array/pointer/structure) that 
2318    can be vectorized. 
2319    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
2320    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2321    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
2322
2323 static bool
2324 vectorizable_store (tree stmt, block_stmt_iterator *bsi, tree *vec_stmt)
2325 {
2326   tree scalar_dest;
2327   tree data_ref;
2328   tree op;
2329   tree vec_oprnd1;
2330   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2331   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2332   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2333   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
2334   enum machine_mode vec_mode;
2335   tree dummy;
2336   enum dr_alignment_support alignment_support_cheme;
2337
2338   /* Is vectorizable store? */
2339
2340   if (TREE_CODE (stmt) != MODIFY_EXPR)
2341     return false;
2342
2343   scalar_dest = TREE_OPERAND (stmt, 0);
2344   if (TREE_CODE (scalar_dest) != ARRAY_REF
2345       && TREE_CODE (scalar_dest) != INDIRECT_REF)
2346     return false;
2347
2348   op = TREE_OPERAND (stmt, 1);
2349   if (!vect_is_simple_use (op, loop, NULL))
2350     {
2351       if (vect_debug_details (NULL))
2352         fprintf (dump_file, "use not simple.");
2353       return false;
2354     }
2355
2356   vec_mode = TYPE_MODE (vectype);
2357   /* FORNOW. In some cases can vectorize even if data-type not supported
2358      (e.g. - array initialization with 0).  */
2359   if (mov_optab->handlers[(int)vec_mode].insn_code == CODE_FOR_nothing)
2360     return false;
2361
2362   if (!STMT_VINFO_DATA_REF (stmt_info))
2363     return false;
2364
2365
2366   if (!vec_stmt) /* transformation not required.  */
2367     {
2368       STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
2369       return true;
2370     }
2371
2372   /** Trasform.  **/
2373
2374   if (vect_debug_details (NULL))
2375     fprintf (dump_file, "transform store");
2376
2377   alignment_support_cheme = vect_supportable_dr_alignment (dr);
2378   gcc_assert (alignment_support_cheme);
2379   gcc_assert (alignment_support_cheme = dr_aligned);  /* FORNOW */
2380
2381   /* Handle use - get the vectorized def from the defining stmt.  */
2382   vec_oprnd1 = vect_get_vec_def_for_operand (op, stmt);
2383
2384   /* Handle def.  */
2385   /* FORNOW: make sure the data reference is aligned.  */
2386   vect_align_data_ref (stmt);
2387   data_ref = vect_create_data_ref_ptr (stmt, bsi, NULL_TREE, &dummy, false);
2388   data_ref = build_fold_indirect_ref (data_ref);
2389
2390   /* Arguments are ready. create the new vector stmt.  */
2391   *vec_stmt = build2 (MODIFY_EXPR, vectype, data_ref, vec_oprnd1);
2392   vect_finish_stmt_generation (stmt, *vec_stmt, bsi);
2393
2394   return true;
2395 }
2396
2397
2398 /* vectorizable_load.
2399
2400    Check if STMT reads a non scalar data-ref (array/pointer/structure) that 
2401    can be vectorized. 
2402    If VEC_STMT is also passed, vectorize the STMT: create a vectorized 
2403    stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2404    Return FALSE if not a vectorizable STMT, TRUE otherwise.  */
2405
2406 static bool
2407 vectorizable_load (tree stmt, block_stmt_iterator *bsi, tree *vec_stmt)
2408 {
2409   tree scalar_dest;
2410   tree vec_dest = NULL;
2411   tree data_ref = NULL;
2412   tree op;
2413   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2414   struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2415   tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2416   tree new_temp;
2417   int mode;
2418   tree init_addr;
2419   tree new_stmt;
2420   tree dummy;
2421   basic_block new_bb;
2422   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
2423   edge pe = loop_preheader_edge (loop);
2424   enum dr_alignment_support alignment_support_cheme;
2425
2426   /* Is vectorizable load? */
2427
2428   if (TREE_CODE (stmt) != MODIFY_EXPR)
2429     return false;
2430
2431   scalar_dest = TREE_OPERAND (stmt, 0);
2432   if (TREE_CODE (scalar_dest) != SSA_NAME)
2433     return false;
2434
2435   op = TREE_OPERAND (stmt, 1);
2436   if (TREE_CODE (op) != ARRAY_REF && TREE_CODE (op) != INDIRECT_REF)
2437     return false;
2438
2439   if (!STMT_VINFO_DATA_REF (stmt_info))
2440     return false;
2441
2442   mode = (int) TYPE_MODE (vectype);
2443
2444   /* FORNOW. In some cases can vectorize even if data-type not supported
2445     (e.g. - data copies).  */
2446   if (mov_optab->handlers[mode].insn_code == CODE_FOR_nothing)
2447     {
2448       if (vect_debug_details (loop))
2449         fprintf (dump_file, "Aligned load, but unsupported type.");
2450       return false;
2451     }
2452
2453   if (!vec_stmt) /* transformation not required.  */
2454     {
2455       STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
2456       return true;
2457     }
2458
2459   /** Trasform.  **/
2460
2461   if (vect_debug_details (NULL))
2462     fprintf (dump_file, "transform load.");
2463
2464   alignment_support_cheme = vect_supportable_dr_alignment (dr);
2465   gcc_assert (alignment_support_cheme);
2466
2467   if (alignment_support_cheme == dr_aligned
2468       || alignment_support_cheme == dr_unaligned_supported)
2469     {
2470       /* Create:
2471          p = initial_addr;
2472          indx = 0;
2473          loop {
2474            vec_dest = *(p);
2475            indx = indx + 1;
2476          }
2477       */
2478
2479       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2480       data_ref = vect_create_data_ref_ptr (stmt, bsi, NULL_TREE, &dummy, false);
2481       if (aligned_access_p (dr))
2482         data_ref = build_fold_indirect_ref (data_ref);
2483       else
2484         {
2485           int mis = DR_MISALIGNMENT (dr);
2486           tree tmis = (mis == -1 ?
2487                        integer_zero_node : 
2488                        build_int_cst (integer_type_node, mis));
2489           tmis = int_const_binop (MULT_EXPR, tmis, 
2490                         build_int_cst (integer_type_node, BITS_PER_UNIT), 1);
2491           data_ref = build2 (MISALIGNED_INDIRECT_REF, vectype, data_ref, tmis);
2492         }
2493       new_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, data_ref);
2494       new_temp = make_ssa_name (vec_dest, new_stmt);
2495       TREE_OPERAND (new_stmt, 0) = new_temp;
2496       vect_finish_stmt_generation (stmt, new_stmt, bsi);
2497     }
2498   else if (alignment_support_cheme == dr_unaligned_software_pipeline)
2499     {
2500       /* Create:
2501          p1 = initial_addr;
2502          msq_init = *(floor(p1))
2503          p2 = initial_addr + VS - 1;
2504          magic = have_builtin ? builtin_result : initial_address;
2505          indx = 0;
2506          loop {
2507            p2' = p2 + indx * vectype_size
2508            lsq = *(floor(p2'))
2509            vec_dest = realign_load (msq, lsq, magic)
2510            indx = indx + 1;
2511            msq = lsq;
2512          }
2513       */
2514
2515       tree offset;
2516       tree magic;
2517       tree phi_stmt;
2518       tree msq_init;
2519       tree msq, lsq;
2520       tree dataref_ptr;
2521       tree params;
2522
2523       /* <1> Create msq_init = *(floor(p1)) in the loop preheader  */
2524       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2525       data_ref = vect_create_data_ref_ptr (stmt, bsi, NULL_TREE, 
2526                                            &init_addr, true);
2527       data_ref = build1 (ALIGN_INDIRECT_REF, vectype, data_ref);
2528       new_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, data_ref);
2529       new_temp = make_ssa_name (vec_dest, new_stmt);
2530       TREE_OPERAND (new_stmt, 0) = new_temp;
2531       new_bb = bsi_insert_on_edge_immediate (pe, new_stmt);
2532       gcc_assert (!new_bb);
2533       msq_init = TREE_OPERAND (new_stmt, 0);
2534
2535
2536       /* <2> Create lsq = *(floor(p2')) in the loop  */ 
2537       offset = build_int_cst (integer_type_node, 
2538                               GET_MODE_NUNITS (TYPE_MODE (vectype)));
2539       offset = int_const_binop (MINUS_EXPR, offset, integer_one_node, 1);
2540       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2541       dataref_ptr = vect_create_data_ref_ptr (stmt, bsi, offset, &dummy, false);
2542       data_ref = build1 (ALIGN_INDIRECT_REF, vectype, dataref_ptr);
2543       new_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, data_ref);
2544       new_temp = make_ssa_name (vec_dest, new_stmt);
2545       TREE_OPERAND (new_stmt, 0) = new_temp;
2546       vect_finish_stmt_generation (stmt, new_stmt, bsi);
2547       lsq = TREE_OPERAND (new_stmt, 0);
2548
2549
2550       /* <3> */
2551       if (targetm.vectorize.builtin_mask_for_load)
2552         {
2553           /* Create permutation mask, if required, in loop preheader.  */
2554           tree builtin_decl;
2555           params = build_tree_list (NULL_TREE, init_addr);
2556           vec_dest = vect_create_destination_var (scalar_dest, vectype);
2557           builtin_decl = targetm.vectorize.builtin_mask_for_load ();
2558           new_stmt = build_function_call_expr (builtin_decl, params);
2559           new_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, new_stmt);
2560           new_temp = make_ssa_name (vec_dest, new_stmt);
2561           TREE_OPERAND (new_stmt, 0) = new_temp;
2562           new_bb = bsi_insert_on_edge_immediate (pe, new_stmt);
2563           gcc_assert (!new_bb);
2564           magic = TREE_OPERAND (new_stmt, 0);
2565         }
2566       else
2567         {
2568           /* Use current address instead of init_addr for reduced reg pressure.
2569            */
2570           magic = dataref_ptr;
2571         }
2572
2573
2574       /* <4> Create msq = phi <msq_init, lsq> in loop  */ 
2575       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2576       msq = make_ssa_name (vec_dest, NULL_TREE);
2577       phi_stmt = create_phi_node (msq, loop->header); /* CHECKME */
2578       SSA_NAME_DEF_STMT (msq) = phi_stmt;
2579       add_phi_arg (&phi_stmt, msq_init, loop_preheader_edge (loop));
2580       add_phi_arg (&phi_stmt, lsq, loop_latch_edge (loop));
2581
2582
2583       /* <5> Create <vec_dest = realign_load (msq, lsq, magic)> in loop  */
2584       vec_dest = vect_create_destination_var (scalar_dest, vectype);
2585       new_stmt = build3 (REALIGN_LOAD_EXPR, vectype, msq, lsq, magic);
2586       new_stmt = build2 (MODIFY_EXPR, vectype, vec_dest, new_stmt);
2587       new_temp = make_ssa_name (vec_dest, new_stmt); 
2588       TREE_OPERAND (new_stmt, 0) = new_temp;
2589       vect_finish_stmt_generation (stmt, new_stmt, bsi);
2590     }
2591   else
2592     gcc_unreachable ();
2593
2594   *vec_stmt = new_stmt;
2595   return true;
2596 }
2597
2598
2599 /* Function vect_supportable_dr_alignment
2600
2601    Return whether the data reference DR is supported with respect to its
2602    alignment.  */
2603
2604 static enum dr_alignment_support
2605 vect_supportable_dr_alignment (struct data_reference *dr)
2606 {
2607   tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr)));
2608   enum machine_mode mode = (int) TYPE_MODE (vectype);
2609
2610   if (aligned_access_p (dr))
2611     return dr_aligned;
2612
2613   /* Possibly unaligned access.  */
2614   
2615   if (DR_IS_READ (dr))
2616     {
2617       if (vec_realign_load_optab->handlers[mode].insn_code != CODE_FOR_nothing
2618           && (!targetm.vectorize.builtin_mask_for_load
2619               || targetm.vectorize.builtin_mask_for_load ()))
2620         return dr_unaligned_software_pipeline;
2621
2622       if (targetm.vectorize.misaligned_mem_ok (mode))
2623         /* Can't software pipeline the loads.  */
2624         return dr_unaligned_supported;
2625     }
2626
2627   /* Unsupported.  */
2628   return dr_unaligned_unsupported;
2629 }
2630
2631
2632 /* Function vect_transform_stmt.
2633
2634    Create a vectorized stmt to replace STMT, and insert it at BSI.  */
2635
2636 static bool
2637 vect_transform_stmt (tree stmt, block_stmt_iterator *bsi)
2638 {
2639   bool is_store = false;
2640   tree vec_stmt = NULL_TREE;
2641   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2642   bool done;
2643
2644   switch (STMT_VINFO_TYPE (stmt_info))
2645     {
2646     case op_vec_info_type:
2647       done = vectorizable_operation (stmt, bsi, &vec_stmt);
2648       gcc_assert (done);
2649       break;
2650
2651     case assignment_vec_info_type:
2652       done = vectorizable_assignment (stmt, bsi, &vec_stmt);
2653       gcc_assert (done);
2654       break;
2655
2656     case load_vec_info_type:
2657       done = vectorizable_load (stmt, bsi, &vec_stmt);
2658       gcc_assert (done);
2659       break;
2660
2661     case store_vec_info_type:
2662       done = vectorizable_store (stmt, bsi, &vec_stmt);
2663       gcc_assert (done);
2664       is_store = true;
2665       break;
2666     default:
2667       if (vect_debug_details (NULL))
2668         fprintf (dump_file, "stmt not supported.");
2669       gcc_unreachable ();
2670     }
2671
2672   STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
2673
2674   return is_store;
2675 }
2676
2677
2678 /* This function builds ni_name = number of iterations loop executes
2679    on the loop preheader.  */
2680
2681 static tree
2682 vect_build_loop_niters (loop_vec_info loop_vinfo)
2683 {
2684   tree ni_name, stmt, var;
2685   edge pe;
2686   basic_block new_bb;
2687   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2688   tree ni = unshare_expr (LOOP_VINFO_NITERS(loop_vinfo));
2689
2690   var = create_tmp_var (TREE_TYPE (ni), "niters");
2691   add_referenced_tmp_var (var);
2692   if (TREE_CODE (ni) == INTEGER_CST)
2693     {
2694       /* This case is generated when treating a known loop bound 
2695          indivisible by VF. Here we cannot use force_gimple_operand.  */
2696       stmt = build (MODIFY_EXPR, void_type_node, var, ni);
2697       ni_name = make_ssa_name (var, stmt);
2698       TREE_OPERAND (stmt, 0) = ni_name;
2699     }
2700   else
2701     ni_name = force_gimple_operand (ni, &stmt, false, var);
2702
2703   pe = loop_preheader_edge (loop);
2704   new_bb = bsi_insert_on_edge_immediate (pe, stmt);
2705   if (new_bb)
2706     add_bb_to_loop (new_bb, EDGE_PRED (new_bb, 0)->src->loop_father);
2707       
2708   return ni_name;
2709 }
2710
2711
2712 /* This function generates the following statements:
2713
2714  ni_name = number of iterations loop executes
2715  ratio = ni_name / vf
2716  ratio_mult_vf_name = ratio * vf
2717
2718  and places them at the loop preheader edge.  */
2719
2720 static void 
2721 vect_generate_tmps_on_preheader (loop_vec_info loop_vinfo, tree *ni_name_p,
2722                                  tree *ratio_mult_vf_name_p, tree *ratio_p)
2723 {
2724
2725   edge pe;
2726   basic_block new_bb;
2727   tree stmt, ni_name;
2728   tree ratio;
2729   tree ratio_mult_vf_name, ratio_mult_vf;
2730   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2731   tree ni = LOOP_VINFO_NITERS(loop_vinfo);
2732   
2733   int vf, i;
2734
2735   /* Generate temporary variable that contains 
2736      number of iterations loop executes.  */
2737
2738   ni_name = vect_build_loop_niters (loop_vinfo);
2739
2740   /* ratio = ni / vf.
2741      vf is power of 2; then if ratio =  = n >> log2 (vf).  */
2742   vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2743   ratio = vect_build_symbol_bound (ni_name, vf, loop);
2744        
2745   /* Update initial conditions of loop copy.  */
2746        
2747   /* ratio_mult_vf = ratio * vf;  
2748      then if ratio_mult_vf = ratio << log2 (vf).  */
2749
2750   i = exact_log2 (vf);
2751   ratio_mult_vf = create_tmp_var (TREE_TYPE (ni), "ratio_mult_vf");
2752   add_referenced_tmp_var (ratio_mult_vf);
2753
2754   ratio_mult_vf_name = make_ssa_name (ratio_mult_vf, NULL_TREE);
2755
2756   stmt = build2 (MODIFY_EXPR, void_type_node, ratio_mult_vf_name,
2757                 build2 (LSHIFT_EXPR, TREE_TYPE (ratio),
2758                        ratio, build_int_cst (unsigned_type_node,
2759                                              i)));
2760
2761   SSA_NAME_DEF_STMT (ratio_mult_vf_name) = stmt;
2762
2763   pe = loop_preheader_edge (loop);
2764   new_bb = bsi_insert_on_edge_immediate (pe, stmt);
2765   if (new_bb)
2766     add_bb_to_loop (new_bb, EDGE_PRED (new_bb, 0)->src->loop_father);
2767
2768   *ni_name_p = ni_name;
2769   *ratio_mult_vf_name_p = ratio_mult_vf_name;
2770   *ratio_p = ratio;
2771     
2772   return;  
2773 }
2774
2775
2776 /* This function generates stmt 
2777    
2778    tmp = n / vf;
2779
2780    and attaches it to preheader of LOOP.  */
2781
2782 static tree 
2783 vect_build_symbol_bound (tree n, int vf, struct loop * loop)
2784 {
2785   tree var, stmt, var_name;
2786   edge pe;
2787   basic_block new_bb;
2788   int i;
2789
2790   /* create temporary variable */
2791   var = create_tmp_var (TREE_TYPE (n), "bnd");
2792   add_referenced_tmp_var (var);
2793
2794   var_name = make_ssa_name (var, NULL_TREE);
2795
2796   /* vf is power of 2; then n/vf = n >> log2 (vf).  */
2797
2798   i = exact_log2 (vf);
2799   stmt = build2 (MODIFY_EXPR, void_type_node, var_name,
2800                 build2 (RSHIFT_EXPR, TREE_TYPE (n),
2801                        n, build_int_cst (unsigned_type_node,i)));
2802
2803   SSA_NAME_DEF_STMT (var_name) = stmt;
2804
2805   pe = loop_preheader_edge (loop);
2806   new_bb = bsi_insert_on_edge_immediate (pe, stmt);
2807   if (new_bb)
2808     add_bb_to_loop (new_bb, EDGE_PRED (new_bb, 0)->src->loop_father);
2809   else  
2810     if (vect_debug_details (NULL))
2811       fprintf (dump_file, "New bb on preheader edge was not generated.");
2812
2813   return var_name;
2814 }
2815
2816
2817 /* Function vect_transform_loop_bound.
2818
2819    Create a new exit condition for the loop.  */
2820
2821 static void
2822 vect_transform_loop_bound (loop_vec_info loop_vinfo, tree niters)
2823 {
2824   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2825   edge exit_edge = loop->single_exit;
2826   block_stmt_iterator loop_exit_bsi = bsi_last (exit_edge->src);
2827   tree indx_before_incr, indx_after_incr;
2828   tree orig_cond_expr;
2829   HOST_WIDE_INT old_N = 0;
2830   int vf;
2831   tree cond_stmt;
2832   tree new_loop_bound;
2833   bool symbol_niters;
2834   tree cond;
2835   tree lb_type;
2836
2837   symbol_niters = !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo);
2838
2839   if (!symbol_niters)
2840     old_N = LOOP_VINFO_INT_NITERS (loop_vinfo);
2841
2842   vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2843
2844   orig_cond_expr = LOOP_VINFO_EXIT_COND (loop_vinfo);
2845 #ifdef ENABLE_CHECKING
2846   gcc_assert (orig_cond_expr);
2847 #endif
2848   gcc_assert (orig_cond_expr == bsi_stmt (loop_exit_bsi));
2849
2850   create_iv (integer_zero_node, integer_one_node, NULL_TREE, loop, 
2851              &loop_exit_bsi, false, &indx_before_incr, &indx_after_incr);
2852
2853   /* bsi_insert is using BSI_NEW_STMT. We need to bump it back 
2854      to point to the exit condition.  */
2855   bsi_next (&loop_exit_bsi);
2856   gcc_assert (bsi_stmt (loop_exit_bsi) == orig_cond_expr);
2857
2858   /* new loop exit test:  */
2859   lb_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (orig_cond_expr, 0), 1));
2860   if (!symbol_niters)
2861     new_loop_bound = fold_convert (lb_type, 
2862                                    build_int_cst (unsigned_type_node, 
2863                                                   old_N/vf));
2864   else
2865     new_loop_bound = niters;
2866
2867   if (exit_edge->flags & EDGE_TRUE_VALUE) /* 'then' edge exits the loop.  */
2868     cond = build2 (GE_EXPR, boolean_type_node, 
2869                    indx_after_incr, new_loop_bound);
2870   else /* 'then' edge loops back.  */
2871     cond = build2 (LT_EXPR, boolean_type_node, 
2872                    indx_after_incr, new_loop_bound);
2873
2874   cond_stmt = build3 (COND_EXPR, TREE_TYPE (orig_cond_expr), cond,
2875         TREE_OPERAND (orig_cond_expr, 1), TREE_OPERAND (orig_cond_expr, 2));
2876
2877   bsi_insert_before (&loop_exit_bsi, cond_stmt, BSI_SAME_STMT);   
2878
2879   /* remove old loop exit test:  */
2880   bsi_remove (&loop_exit_bsi);
2881
2882   if (vect_debug_details (NULL))
2883     print_generic_expr (dump_file, cond_stmt, TDF_SLIM);
2884 }
2885
2886
2887 /*   Advance IVs of the loop (to be vectorized later) to correct position.
2888
2889      When loop is vectorized, its IVs are not always advanced
2890      correctly since vectorization changes the loop count. It's ok
2891      in case epilog loop was not produced after original one before 
2892      vectorization process (the vectorizer checks that there is no uses 
2893      of IVs after the loop). However, in case the epilog loop was peeled, 
2894      IVs from original loop are used in epilog loop and should be 
2895      advanced correctly.
2896
2897      Here we use access functions of IVs and number of
2898      iteration loop executes in order to bring IVs to correct position.
2899
2900      Function also update phis of basic block at the exit
2901      from the loop.  */
2902
2903 static void
2904 vect_update_ivs_after_vectorizer (struct loop *loop, tree niters)
2905 {
2906   edge exit = loop->exit_edges[0];
2907   tree phi;
2908   edge latch = loop_latch_edge (loop);
2909
2910   /* Generate basic block at the exit from the loop.  */
2911   basic_block new_bb = split_edge (exit);
2912   add_bb_to_loop (new_bb, EDGE_SUCC (new_bb, 0)->dest->loop_father);
2913   
2914   loop->exit_edges[0] = EDGE_PRED (new_bb, 0);
2915   
2916   for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi))
2917     {
2918       tree access_fn = NULL;
2919       tree evolution_part;
2920       tree init_expr;
2921       tree step_expr;
2922       tree var, stmt, ni, ni_name;
2923       int i, j, num_elem1, num_elem2;
2924       tree phi1;
2925       block_stmt_iterator last_bsi;
2926
2927       /* Skip virtual phi's. The data dependences that are associated with
2928          virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.  */
2929
2930       if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
2931         {
2932           if (vect_debug_details (NULL))
2933             fprintf (dump_file, "virtual phi. skip.");
2934           continue;
2935         }
2936
2937       access_fn = analyze_scalar_evolution (loop, PHI_RESULT (phi)); 
2938
2939       evolution_part = evolution_part_in_loop_num (access_fn, loop->num);
2940       
2941       /* FORNOW: We do not transform initial conditions of IVs 
2942          which evolution functions are a polynomial of degree >= 2 or
2943          exponential.  */
2944
2945       step_expr = evolution_part;
2946       init_expr = initial_condition (access_fn);
2947
2948       ni = build2 (PLUS_EXPR, TREE_TYPE (init_expr),
2949                   build2 (MULT_EXPR, TREE_TYPE (niters),
2950                        niters, step_expr), init_expr);
2951
2952       var = create_tmp_var (TREE_TYPE (init_expr), "tmp");
2953       add_referenced_tmp_var (var);
2954
2955       ni_name = force_gimple_operand (ni, &stmt, false, var);
2956       
2957       /* Insert stmt into new_bb.  */
2958       last_bsi = bsi_last (new_bb);
2959       bsi_insert_after (&last_bsi, stmt, BSI_NEW_STMT);   
2960
2961       /* Fix phi expressions in duplicated loop.  */
2962       num_elem1 = PHI_NUM_ARGS (phi);
2963       for (i = 0; i < num_elem1; i++)
2964         if (PHI_ARG_EDGE (phi, i) == latch)
2965           {
2966             tree def = PHI_ARG_DEF (phi, i);
2967
2968             for (phi1 = phi_nodes (EDGE_SUCC (new_bb, 0)->dest); phi1; 
2969                  phi1 = TREE_CHAIN (phi1))
2970               {
2971                 num_elem2 = PHI_NUM_ARGS (phi1);
2972                 for (j = 0; j < num_elem2; j++)
2973                   if (PHI_ARG_DEF (phi1, j) == def)
2974                     {
2975                       SET_PHI_ARG_DEF (phi1, j, ni_name);
2976                       PHI_ARG_EDGE (phi1, j) = EDGE_SUCC (new_bb, 0);                 
2977                       break;
2978                     }               
2979               }
2980             break;
2981           }
2982     }
2983         
2984 }
2985
2986
2987 /* This function is the main driver of transformation 
2988    to be done for loop before vectorizing it in case of 
2989    unknown loop bound.  */
2990
2991 static void 
2992 vect_transform_for_unknown_loop_bound (loop_vec_info loop_vinfo, tree * ratio,
2993                                        struct loops *loops)
2994 {
2995
2996   tree ni_name, ratio_mult_vf_name;
2997 #ifdef ENABLE_CHECKING
2998   int loop_num;
2999 #endif
3000   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3001   struct loop *new_loop;
3002
3003   if (vect_debug_details (NULL))
3004     fprintf (dump_file, "\n<<vect_transtorm_for_unknown_loop_bound>>\n");
3005
3006   /* Generate the following variables on the preheader of original loop:
3007          
3008      ni_name = number of iteration the original loop executes
3009      ratio = ni_name / vf
3010      ratio_mult_vf_name = ratio * vf  */
3011   vect_generate_tmps_on_preheader (loop_vinfo, &ni_name,
3012                                    &ratio_mult_vf_name, ratio);
3013
3014   /* Update loop info.  */
3015   loop->pre_header = loop_preheader_edge (loop)->src;
3016   loop->pre_header_edges[0] = loop_preheader_edge (loop);
3017
3018 #ifdef ENABLE_CHECKING
3019   loop_num  = loop->num; 
3020 #endif
3021   new_loop = tree_duplicate_loop_to_edge (loop, loops, loop->exit_edges[0],
3022                                           ratio_mult_vf_name, ni_name, true); 
3023 #ifdef ENABLE_CHECKING
3024   gcc_assert (new_loop);
3025   gcc_assert (loop_num == loop->num);
3026 #endif
3027
3028   /* Update IVs of original loop as if they were advanced 
3029      by ratio_mult_vf_name steps.  */
3030
3031 #ifdef ENABLE_CHECKING
3032   /* Check existence of intermediate bb.  */
3033   gcc_assert (loop->exit_edges[0]->dest == new_loop->pre_header);
3034 #endif
3035   vect_update_ivs_after_vectorizer (loop, ratio_mult_vf_name); 
3036
3037   return;
3038
3039 }
3040
3041
3042 /* Function vect_gen_niters_for_prolog_loop
3043
3044    Set the number of iterations for the loop represented by LOOP_VINFO
3045    to the minimum between NITERS (the original iteration count of the loop)
3046    and the misalignment of DR - the first data reference recorded in
3047    LOOP_VINFO_UNALIGNED_DR (LOOP_VINFO).  As a result, after the execution of 
3048    this loop, the data reference DR will refer to an aligned location.  */
3049
3050 static tree 
3051 vect_gen_niters_for_prolog_loop (loop_vec_info loop_vinfo, tree niters)
3052 {
3053   struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
3054   int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3055   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3056   tree var, stmt;
3057   tree iters, iters_name;
3058   edge pe;
3059   basic_block new_bb;
3060   tree dr_stmt = DR_STMT (dr);
3061   stmt_vec_info stmt_info = vinfo_for_stmt (dr_stmt);
3062   tree start_addr, byte_miss_align, elem_miss_align;
3063   int vec_type_align = 
3064     GET_MODE_ALIGNMENT (TYPE_MODE (STMT_VINFO_VECTYPE (stmt_info))) 
3065                                                         / BITS_PER_UNIT;
3066   tree tmp1, tmp2;
3067   tree new_stmt_list = NULL_TREE;
3068
3069   start_addr = vect_create_addr_base_for_vector_ref (dr_stmt,
3070                                                      &new_stmt_list, NULL_TREE);
3071
3072   pe = loop_preheader_edge (loop); 
3073   new_bb = bsi_insert_on_edge_immediate (pe, new_stmt_list); 
3074   if (new_bb)
3075     add_bb_to_loop (new_bb, EDGE_PRED (new_bb, 0)->src->loop_father);
3076
3077   byte_miss_align = 
3078         build (BIT_AND_EXPR, integer_type_node, start_addr, 
3079                   build (MINUS_EXPR, integer_type_node, 
3080                          build_int_cst (unsigned_type_node,
3081                                         vec_type_align), integer_one_node));
3082   tmp1 = build_int_cst (unsigned_type_node, vec_type_align/vf);
3083   elem_miss_align = build (FLOOR_DIV_EXPR, integer_type_node, 
3084                            byte_miss_align, tmp1); 
3085   
3086   tmp2 = 
3087         build (BIT_AND_EXPR, integer_type_node,
3088           build (MINUS_EXPR, integer_type_node, 
3089                 build_int_cst (unsigned_type_node, vf), elem_miss_align),
3090           build (MINUS_EXPR, integer_type_node, 
3091                 build_int_cst (unsigned_type_node, vf), integer_one_node)); 
3092
3093   iters = build2 (MIN_EXPR, TREE_TYPE (tmp2), tmp2, niters);
3094   var = create_tmp_var (TREE_TYPE (iters), "iters");
3095   add_referenced_tmp_var (var);
3096   iters_name = force_gimple_operand (iters, &stmt, false, var);
3097
3098   /* Insert stmt on loop preheader edge.  */
3099   pe = loop_preheader_edge (loop);
3100   new_bb = bsi_insert_on_edge_immediate (pe, stmt);
3101   if (new_bb)
3102     add_bb_to_loop (new_bb, EDGE_PRED (new_bb, 0)->src->loop_father);
3103
3104   return iters_name; 
3105 }
3106
3107
3108 /* Function vect_update_niters_after_peeling
3109
3110    NITERS iterations were peeled from the loop represented by LOOP_VINFO. 
3111    The new number of iterations is therefore original_niters - NITERS.
3112    Record the new number of iterations in LOOP_VINFO.  */
3113
3114 static void
3115 vect_update_niters_after_peeling (loop_vec_info loop_vinfo, tree niters)
3116 {
3117   tree n_iters = LOOP_VINFO_NITERS (loop_vinfo);
3118   LOOP_VINFO_NITERS (loop_vinfo) = 
3119     build (MINUS_EXPR, integer_type_node, n_iters, niters);      
3120 }
3121
3122
3123 /* Function vect_update_inits_of_dr
3124
3125    NITERS iterations were peeled from LOOP.  DR represents a data reference
3126    in LOOP.  This function updates the information recorded in DR to
3127    account for the fact that the first NITERS iterations had already been 
3128    executed.  Specifically, it updates the initial_condition of the 
3129    access_function of DR.  */
3130
3131 static void
3132 vect_update_inits_of_dr (struct data_reference *dr, struct loop *loop, 
3133                          tree niters)
3134 {
3135   tree access_fn = DR_ACCESS_FN (dr, 0);
3136   tree init, init_new, step;
3137       
3138   step = evolution_part_in_loop_num (access_fn, loop->num);
3139   init = initial_condition (access_fn);
3140       
3141   init_new = build (PLUS_EXPR, TREE_TYPE (init),
3142                   build (MULT_EXPR, TREE_TYPE (niters),
3143                          niters, step), init);
3144   DR_ACCESS_FN (dr, 0) = chrec_replace_initial_condition (access_fn, init_new);
3145   
3146   return;
3147 }
3148
3149
3150 /* Function vect_update_inits_of_drs
3151
3152    NITERS iterations were peeled from the loop represented by LOOP_VINFO.  
3153    This function updates the information recorded for the data references in 
3154    the loop to account for the fact that the first NITERS iterations had 
3155    already been executed.  Specifically, it updates the initial_condition of the
3156    access_function of all the data_references in the loop.  */
3157
3158 static void
3159 vect_update_inits_of_drs (loop_vec_info loop_vinfo, tree niters)
3160 {
3161   unsigned int i;
3162   varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
3163   varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
3164   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3165
3166   if (dump_file && (dump_flags & TDF_DETAILS))
3167     fprintf (dump_file, "\n<<vect_update_inits_of_dr>>\n");
3168
3169   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
3170     {
3171       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
3172       vect_update_inits_of_dr (dr, loop, niters);
3173     }
3174
3175   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
3176     {
3177       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
3178       vect_update_inits_of_dr (dr, loop, niters);
3179     }
3180 }
3181
3182
3183 /* Function vect_do_peeling_for_alignment
3184
3185    Peel the first 'niters' iterations of the loop represented by LOOP_VINFO.
3186    'niters' is set to the misalignment of one of the data references in the
3187    loop, thereby forcing it to refer to an aligned location at the beginning
3188    of the execution of this loop.  The data reference for which we are
3189    peeling is recorded in LOOP_VINFO_UNALIGNED_DR.  */
3190
3191 static void
3192 vect_do_peeling_for_alignment (loop_vec_info loop_vinfo, struct loops *loops)
3193 {
3194   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3195   tree niters_of_prolog_loop, ni_name;
3196
3197   if (vect_debug_details (NULL))
3198     fprintf (dump_file, "\n<<vect_do_peeling_for_alignment>>\n");
3199
3200   ni_name = vect_build_loop_niters (loop_vinfo);
3201   niters_of_prolog_loop = vect_gen_niters_for_prolog_loop (loop_vinfo, ni_name);
3202   
3203
3204   /* Peel the prolog loop and iterate it niters_of_prolog_loop.  */
3205   tree_duplicate_loop_to_edge (loop, loops, loop_preheader_edge(loop), 
3206                                   niters_of_prolog_loop, ni_name, false); 
3207
3208   /* Update number of times loop executes.  */
3209   vect_update_niters_after_peeling (loop_vinfo, niters_of_prolog_loop);
3210
3211   /* Update all inits of access functions of all data refs.  */
3212   vect_update_inits_of_drs (loop_vinfo, niters_of_prolog_loop);
3213
3214   /* After peeling we have to reset scalar evolution analyzer.  */
3215   scev_reset ();
3216
3217   return;
3218 }
3219
3220
3221 /* Function vect_transform_loop.
3222
3223    The analysis phase has determined that the loop is vectorizable.
3224    Vectorize the loop - created vectorized stmts to replace the scalar
3225    stmts in the loop, and update the loop exit condition.  */
3226
3227 static void
3228 vect_transform_loop (loop_vec_info loop_vinfo, 
3229                      struct loops *loops ATTRIBUTE_UNUSED)
3230 {
3231   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3232   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3233   int nbbs = loop->num_nodes;
3234   block_stmt_iterator si;
3235   int i;
3236   tree ratio = NULL;
3237   int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3238
3239   if (vect_debug_details (NULL))
3240     fprintf (dump_file, "\n<<vec_transform_loop>>\n");
3241
3242   
3243   /* Peel the loop if there are data refs with unknown alignment.
3244      Only one data ref with unknown store is allowed.  */
3245
3246   
3247   if (LOOP_DO_PEELING_FOR_ALIGNMENT (loop_vinfo))
3248     vect_do_peeling_for_alignment (loop_vinfo, loops);
3249   
3250   /* If the loop has a symbolic number of iterations 'n' 
3251      (i.e. it's not a compile time constant), 
3252      then an epilog loop needs to be created. We therefore duplicate 
3253      the initial loop. The original loop will be vectorized, and will compute
3254      the first (n/VF) iterations. The second copy of the loop will remain 
3255      serial and will compute the remaining (n%VF) iterations.
3256      (VF is the vectorization factor).  */
3257
3258   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
3259     vect_transform_for_unknown_loop_bound (loop_vinfo, &ratio, loops);
3260
3261   /* FORNOW: we'll treat the case where niters is constant and 
3262      
3263                         niters % vf != 0
3264
3265      in the way similar to one with symbolic niters. 
3266      For this we'll generate variable which value is equal to niters.  */
3267
3268   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) 
3269       && (LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0))
3270     vect_transform_for_unknown_loop_bound (loop_vinfo, &ratio, loops);
3271
3272
3273   /* 1) Make sure the loop header has exactly two entries
3274      2) Make sure we have a preheader basic block.  */
3275
3276   gcc_assert (EDGE_COUNT (loop->header->preds) == 2);
3277
3278   loop_split_edge_with (loop_preheader_edge (loop), NULL);
3279
3280
3281   /* FORNOW: the vectorizer supports only loops which body consist
3282      of one basic block (header + empty latch). When the vectorizer will 
3283      support more involved loop forms, the order by which the BBs are 
3284      traversed need to be reconsidered.  */
3285
3286   for (i = 0; i < nbbs; i++)
3287     {
3288       basic_block bb = bbs[i];
3289
3290       for (si = bsi_start (bb); !bsi_end_p (si);)
3291         {
3292           tree stmt = bsi_stmt (si);
3293           stmt_vec_info stmt_info;
3294           bool is_store;
3295
3296           if (vect_debug_details (NULL))
3297             {
3298               fprintf (dump_file, "------>vectorizing statement: ");
3299               print_generic_expr (dump_file, stmt, TDF_SLIM);
3300             }   
3301           stmt_info = vinfo_for_stmt (stmt);
3302           gcc_assert (stmt_info);
3303           if (!STMT_VINFO_RELEVANT_P (stmt_info))
3304             {
3305               bsi_next (&si);
3306               continue;
3307             }
3308 #ifdef ENABLE_CHECKING
3309           /* FORNOW: Verify that all stmts operate on the same number of
3310                      units and no inner unrolling is necessary.  */
3311           gcc_assert 
3312                 (GET_MODE_NUNITS (TYPE_MODE (STMT_VINFO_VECTYPE (stmt_info)))
3313                  == vectorization_factor);
3314 #endif
3315           /* -------- vectorize statement ------------ */
3316           if (vect_debug_details (NULL))
3317             fprintf (dump_file, "transform statement.");
3318
3319           is_store = vect_transform_stmt (stmt, &si);
3320           if (is_store)
3321             {
3322               /* free the attached stmt_vec_info and remove the stmt.  */
3323               stmt_ann_t ann = stmt_ann (stmt);
3324               free (stmt_info);
3325               set_stmt_info (ann, NULL);
3326               bsi_remove (&si);
3327               continue;
3328             }
3329
3330           bsi_next (&si);
3331         }                       /* stmts in BB */
3332     }                           /* BBs in loop */
3333
3334   vect_transform_loop_bound (loop_vinfo, ratio);
3335
3336   if (vect_debug_details (loop))
3337     fprintf (dump_file,"Success! loop vectorized.");
3338   if (vect_debug_stats (loop))
3339     fprintf (dump_file, "LOOP VECTORIZED.");
3340 }
3341
3342
3343 /* Function vect_is_simple_use.
3344
3345    Input:
3346    LOOP - the loop that is being vectorized.
3347    OPERAND - operand of a stmt in LOOP.
3348    DEF - the defining stmt in case OPERAND is an SSA_NAME.
3349
3350    Returns whether a stmt with OPERAND can be vectorized.
3351    Supportable operands are constants, loop invariants, and operands that are
3352    defined by the current iteration of the loop. Unsupportable operands are 
3353    those that are defined by a previous iteration of the loop (as is the case
3354    in reduction/induction computations).  */
3355
3356 static bool
3357 vect_is_simple_use (tree operand, struct loop *loop, tree *def)
3358
3359   tree def_stmt;
3360   basic_block bb;
3361
3362   if (def)
3363     *def = NULL_TREE;
3364
3365   if (TREE_CODE (operand) == INTEGER_CST || TREE_CODE (operand) == REAL_CST)
3366     return true;
3367
3368   if (TREE_CODE (operand) != SSA_NAME)
3369     return false;
3370
3371   def_stmt = SSA_NAME_DEF_STMT (operand);
3372   if (def_stmt == NULL_TREE )
3373     {
3374       if (vect_debug_details (NULL))
3375         fprintf (dump_file, "no def_stmt.");
3376       return false;
3377     }
3378
3379   /* empty stmt is expected only in case of a function argument.
3380      (Otherwise - we expect a phi_node or a modify_expr).  */
3381   if (IS_EMPTY_STMT (def_stmt))
3382     {
3383       tree arg = TREE_OPERAND (def_stmt, 0);
3384       if (TREE_CODE (arg) == INTEGER_CST || TREE_CODE (arg) == REAL_CST)
3385         return true;
3386       if (vect_debug_details (NULL))
3387         {
3388           fprintf (dump_file, "Unexpected empty stmt: ");
3389           print_generic_expr (dump_file, def_stmt, TDF_SLIM);
3390         }
3391       return false;  
3392     }
3393
3394   /* phi_node inside the loop indicates an induction/reduction pattern.
3395      This is not supported yet.  */
3396   bb = bb_for_stmt (def_stmt);
3397   if (TREE_CODE (def_stmt) == PHI_NODE && flow_bb_inside_loop_p (loop, bb))
3398     {
3399       if (vect_debug_details (NULL))
3400         fprintf (dump_file, "reduction/induction - unsupported.");
3401       return false; /* FORNOW: not supported yet.  */
3402     }
3403
3404   /* Expecting a modify_expr or a phi_node.  */
3405   if (TREE_CODE (def_stmt) == MODIFY_EXPR
3406       || TREE_CODE (def_stmt) == PHI_NODE)
3407     {
3408       if (def)
3409         *def = def_stmt;        
3410       return true;
3411     }
3412
3413   return false;
3414 }
3415
3416
3417 /* Function vect_analyze_operations.
3418
3419    Scan the loop stmts and make sure they are all vectorizable.  */
3420
3421 static bool
3422 vect_analyze_operations (loop_vec_info loop_vinfo)
3423 {
3424   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3425   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3426   int nbbs = loop->num_nodes;
3427   block_stmt_iterator si;
3428   int vectorization_factor = 0;
3429   int i;
3430   bool ok;
3431   tree scalar_type;
3432
3433   if (vect_debug_details (NULL))
3434     fprintf (dump_file, "\n<<vect_analyze_operations>>\n");
3435
3436   for (i = 0; i < nbbs; i++)
3437     {
3438       basic_block bb = bbs[i];
3439
3440       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3441         {
3442           tree stmt = bsi_stmt (si);
3443           int nunits;
3444           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3445           tree vectype;
3446
3447           if (vect_debug_details (NULL))
3448             {
3449               fprintf (dump_file, "==> examining statement: ");
3450               print_generic_expr (dump_file, stmt, TDF_SLIM);
3451             }
3452
3453           gcc_assert (stmt_info);
3454
3455           /* skip stmts which do not need to be vectorized.
3456              this is expected to include:
3457              - the COND_EXPR which is the loop exit condition
3458              - any LABEL_EXPRs in the loop
3459              - computations that are used only for array indexing or loop
3460              control  */
3461
3462           if (!STMT_VINFO_RELEVANT_P (stmt_info))
3463             {
3464               if (vect_debug_details (NULL))
3465                 fprintf (dump_file, "irrelevant.");
3466               continue;
3467             }
3468
3469           if (VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (stmt))))
3470             {
3471               if (vect_debug_stats (loop) || vect_debug_details (loop))
3472                 {
3473                   fprintf (dump_file, "not vectorized: vector stmt in loop:");
3474                   print_generic_expr (dump_file, stmt, TDF_SLIM);
3475                 }
3476               return false;
3477             }
3478
3479           if (STMT_VINFO_DATA_REF (stmt_info))
3480             scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));    
3481           else if (TREE_CODE (stmt) == MODIFY_EXPR)
3482             scalar_type = TREE_TYPE (TREE_OPERAND (stmt, 0));
3483           else
3484             scalar_type = TREE_TYPE (stmt);
3485
3486           if (vect_debug_details (NULL))
3487             {
3488               fprintf (dump_file, "get vectype for scalar type:  ");
3489               print_generic_expr (dump_file, scalar_type, TDF_SLIM);
3490             }
3491
3492           vectype = get_vectype_for_scalar_type (scalar_type);
3493           if (!vectype)
3494             {
3495               if (vect_debug_stats (loop) || vect_debug_details (loop))
3496                 {
3497                   fprintf (dump_file, "not vectorized: unsupported data-type ");
3498                   print_generic_expr (dump_file, scalar_type, TDF_SLIM);
3499                 }
3500               return false;
3501             }
3502
3503           if (vect_debug_details (NULL))
3504             {
3505               fprintf (dump_file, "vectype: ");
3506               print_generic_expr (dump_file, vectype, TDF_SLIM);
3507             }
3508           STMT_VINFO_VECTYPE (stmt_info) = vectype;
3509
3510           ok = (vectorizable_operation (stmt, NULL, NULL)
3511                 || vectorizable_assignment (stmt, NULL, NULL)
3512                 || vectorizable_load (stmt, NULL, NULL)
3513                 || vectorizable_store (stmt, NULL, NULL));
3514
3515           if (!ok)
3516             {
3517               if (vect_debug_stats (loop) || vect_debug_details (loop))
3518                 {
3519                   fprintf (dump_file, "not vectorized: stmt not supported: ");
3520                   print_generic_expr (dump_file, stmt, TDF_SLIM);
3521                 }
3522               return false;
3523             }
3524
3525           nunits = GET_MODE_NUNITS (TYPE_MODE (vectype));
3526           if (vect_debug_details (NULL))
3527             fprintf (dump_file, "nunits = %d", nunits);
3528
3529           if (vectorization_factor)
3530             {
3531               /* FORNOW: don't allow mixed units.
3532                  This restriction will be relaxed in the future.  */
3533               if (nunits != vectorization_factor)
3534                 {
3535                   if (vect_debug_stats (loop) || vect_debug_details (loop))
3536                     fprintf (dump_file, "not vectorized: mixed data-types");
3537                   return false;
3538                 }
3539             }
3540           else
3541             vectorization_factor = nunits;
3542
3543 #ifdef ENABLE_CHECKING
3544           gcc_assert (GET_MODE_SIZE (TYPE_MODE (scalar_type))
3545                         * vectorization_factor == UNITS_PER_SIMD_WORD);
3546 #endif
3547         }
3548     }
3549
3550   /* TODO: Analyze cost. Decide if worth while to vectorize.  */
3551
3552   if (vectorization_factor <= 1)
3553     {
3554       if (vect_debug_stats (loop) || vect_debug_details (loop))
3555         fprintf (dump_file, "not vectorized: unsupported data-type");
3556       return false;
3557     }
3558   LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
3559
3560   
3561   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) 
3562       && vect_debug_details (NULL))
3563     fprintf (dump_file, 
3564         "vectorization_factor = %d, niters = " HOST_WIDE_INT_PRINT_DEC,
3565         vectorization_factor, LOOP_VINFO_INT_NITERS (loop_vinfo));
3566
3567   if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
3568       && LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0)
3569     {
3570       /* In this case we have to generate epilog loop, that 
3571          can be done only for loops with one entry edge.  */
3572       if (LOOP_VINFO_LOOP (loop_vinfo)->num_entries != 1
3573           || !(LOOP_VINFO_LOOP (loop_vinfo)->pre_header))
3574         {
3575           if (vect_debug_stats (loop) || vect_debug_details (loop))
3576             fprintf (dump_file, "not vectorized: more than one entry.");
3577           return false;
3578         }
3579     }
3580   
3581   return true;
3582 }
3583
3584
3585 /* Function exist_non_indexing_operands_for_use_p 
3586
3587    USE is one of the uses attached to STMT. Check if USE is 
3588    used in STMT for anything other than indexing an array.  */
3589
3590 static bool
3591 exist_non_indexing_operands_for_use_p (tree use, tree stmt)
3592 {
3593   tree operand;
3594   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3595  
3596   /* USE corresponds to some operand in STMT. If there is no data
3597      reference in STMT, then any operand that corresponds to USE
3598      is not indexing an array.  */
3599   if (!STMT_VINFO_DATA_REF (stmt_info))
3600     return true;
3601  
3602   /* STMT has a data_ref. FORNOW this means that its of one of
3603      the following forms:
3604      -1- ARRAY_REF = var
3605      -2- var = ARRAY_REF
3606      (This should have been verified in analyze_data_refs).
3607
3608      'var' in the second case corresponds to a def, not a use,
3609      so USE cannot correspond to any operands that are not used 
3610      for array indexing.
3611
3612      Therefore, all we need to check is if STMT falls into the
3613      first case, and whether var corresponds to USE.  */
3614  
3615   if (TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME)
3616     return false;
3617
3618   operand = TREE_OPERAND (stmt, 1);
3619
3620   if (TREE_CODE (operand) != SSA_NAME)
3621     return false;
3622
3623   if (operand == use)
3624     return true;
3625
3626   return false;
3627 }
3628
3629
3630 /* Function vect_is_simple_iv_evolution.
3631
3632    FORNOW: A simple evolution of an induction variables in the loop is
3633    considered a polynomial evolution with constant step.  */
3634
3635 static bool
3636 vect_is_simple_iv_evolution (unsigned loop_nb, tree access_fn, tree * init, 
3637                                 tree * step, bool strict)
3638 {
3639   tree init_expr;
3640   tree step_expr;
3641   
3642   tree evolution_part = evolution_part_in_loop_num (access_fn, loop_nb);
3643
3644   /* When there is no evolution in this loop, the evolution function
3645      is not "simple".  */  
3646   if (evolution_part == NULL_TREE)
3647     return false;
3648   
3649   /* When the evolution is a polynomial of degree >= 2
3650      the evolution function is not "simple".  */
3651   if (tree_is_chrec (evolution_part))
3652     return false;
3653   
3654   step_expr = evolution_part;
3655   init_expr = unshare_expr (initial_condition (access_fn));
3656
3657   if (vect_debug_details (NULL))
3658     {
3659       fprintf (dump_file, "step: ");
3660       print_generic_expr (dump_file, step_expr, TDF_SLIM);
3661       fprintf (dump_file, ",  init: ");
3662       print_generic_expr (dump_file, init_expr, TDF_SLIM);
3663     }
3664
3665   *init = init_expr;
3666   *step = step_expr;
3667
3668   if (TREE_CODE (step_expr) != INTEGER_CST)
3669     {
3670       if (vect_debug_details (NULL))
3671         fprintf (dump_file, "step unknown.");
3672       return false;
3673     }
3674
3675   if (strict)
3676     if (!integer_onep (step_expr))
3677       {
3678         if (vect_debug_details (NULL))
3679           print_generic_expr (dump_file, step_expr, TDF_SLIM);
3680         return false;
3681       }
3682
3683   return true;
3684 }
3685
3686
3687 /* Function vect_analyze_scalar_cycles.
3688
3689    Examine the cross iteration def-use cycles of scalar variables, by
3690    analyzing the loop (scalar) PHIs; verify that the cross iteration def-use
3691    cycles that they represent do not impede vectorization.
3692
3693    FORNOW: Reduction as in the following loop, is not supported yet:
3694               loop1:
3695               for (i=0; i<N; i++)
3696                  sum += a[i];
3697            The cross-iteration cycle corresponding to variable 'sum' will be
3698            considered too complicated and will impede vectorization.
3699
3700    FORNOW: Induction as in the following loop, is not supported yet:
3701               loop2:
3702               for (i=0; i<N; i++)
3703                  a[i] = i;
3704
3705            However, the following loop *is* vectorizable:
3706               loop3:
3707               for (i=0; i<N; i++)
3708                  a[i] = b[i];
3709
3710            In both loops there exists a def-use cycle for the variable i:
3711               loop: i_2 = PHI (i_0, i_1)
3712                     a[i_2] = ...;
3713                     i_1 = i_2 + 1;
3714                     GOTO loop;
3715
3716            The evolution of the above cycle is considered simple enough,
3717            however, we also check that the cycle does not need to be
3718            vectorized, i.e - we check that the variable that this cycle
3719            defines is only used for array indexing or in stmts that do not
3720            need to be vectorized. This is not the case in loop2, but it
3721            *is* the case in loop3.  */
3722
3723 static bool
3724 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
3725 {
3726   tree phi;
3727   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3728   basic_block bb = loop->header;
3729   tree dummy;
3730
3731   if (vect_debug_details (NULL))
3732     fprintf (dump_file, "\n<<vect_analyze_scalar_cycles>>\n");
3733
3734   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3735     {
3736       tree access_fn = NULL;
3737
3738       if (vect_debug_details (NULL))
3739         {
3740           fprintf (dump_file, "Analyze phi: ");
3741           print_generic_expr (dump_file, phi, TDF_SLIM);
3742         }
3743
3744       /* Skip virtual phi's. The data dependences that are associated with
3745          virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.  */
3746
3747       if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
3748         {
3749           if (vect_debug_details (NULL))
3750             fprintf (dump_file, "virtual phi. skip.");
3751           continue;
3752         }
3753
3754       /* Analyze the evolution function.  */
3755
3756       /* FORNOW: The only scalar cross-iteration cycles that we allow are
3757          those of loop induction variables; This property is verified here.
3758
3759          Furthermore, if that induction variable is used in an operation
3760          that needs to be vectorized (i.e, is not solely used to index
3761          arrays and check the exit condition) - we do not support its
3762          vectorization yet. This property is verified in vect_is_simple_use,
3763          during vect_analyze_operations.  */
3764
3765       access_fn = /* instantiate_parameters
3766                      (loop,*/
3767          analyze_scalar_evolution (loop, PHI_RESULT (phi));
3768
3769       if (!access_fn)
3770         {
3771           if (vect_debug_stats (loop) || vect_debug_details (loop))
3772             fprintf (dump_file, "not vectorized: unsupported scalar cycle.");
3773           return false;
3774         }
3775
3776       if (vect_debug_details (NULL))
3777         {
3778            fprintf (dump_file, "Access function of PHI: ");
3779            print_generic_expr (dump_file, access_fn, TDF_SLIM);
3780         }
3781
3782       if (!vect_is_simple_iv_evolution (loop->num, access_fn, &dummy, 
3783                                         &dummy, false))
3784         {
3785           if (vect_debug_stats (loop) || vect_debug_details (loop))
3786             fprintf (dump_file, "not vectorized: unsupported scalar cycle.");
3787           return false;
3788         }
3789     }
3790
3791   return true;
3792 }
3793
3794
3795 /* Function vect_analyze_data_ref_dependence.
3796
3797    Return TRUE if there (might) exist a dependence between a memory-reference
3798    DRA and a memory-reference DRB.  */
3799
3800 static bool
3801 vect_analyze_data_ref_dependence (struct data_reference *dra,
3802                                   struct data_reference *drb, 
3803                                   struct loop *loop)
3804 {
3805   bool differ_p; 
3806   struct data_dependence_relation *ddr;
3807   
3808   if (!array_base_name_differ_p (dra, drb, &differ_p))
3809     {
3810       if (vect_debug_stats (loop) || vect_debug_details (loop))   
3811         {
3812           fprintf (dump_file,
3813                 "not vectorized: can't determine dependence between: ");
3814           print_generic_expr (dump_file, DR_REF (dra), TDF_SLIM);
3815           fprintf (dump_file, " and ");
3816           print_generic_expr (dump_file, DR_REF (drb), TDF_SLIM);
3817         }
3818       return true;
3819     }
3820
3821   if (differ_p)
3822     return false;
3823
3824   ddr = initialize_data_dependence_relation (dra, drb);
3825   compute_affine_dependence (ddr);
3826
3827   if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3828     return false;
3829   
3830   if (vect_debug_stats (loop) || vect_debug_details (loop))
3831     {
3832       fprintf (dump_file,
3833         "not vectorized: possible dependence between data-refs ");
3834       print_generic_expr (dump_file, DR_REF (dra), TDF_SLIM);
3835       fprintf (dump_file, " and ");
3836       print_generic_expr (dump_file, DR_REF (drb), TDF_SLIM);
3837     }
3838
3839   return true;
3840 }
3841
3842
3843 /* Function vect_analyze_data_ref_dependences.
3844
3845    Examine all the data references in the loop, and make sure there do not
3846    exist any data dependences between them.
3847
3848    TODO: dependences which distance is greater than the vectorization factor
3849          can be ignored.  */
3850
3851 static bool
3852 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo)
3853 {
3854   unsigned int i, j;
3855   varray_type loop_write_refs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
3856   varray_type loop_read_refs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
3857   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3858
3859   /* Examine store-store (output) dependences.  */
3860
3861   if (vect_debug_details (NULL))
3862     fprintf (dump_file, "\n<<vect_analyze_dependences>>\n");
3863
3864   if (vect_debug_details (NULL))
3865     fprintf (dump_file, "compare all store-store pairs.");
3866
3867   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_refs); i++)
3868     {
3869       for (j = i + 1; j < VARRAY_ACTIVE_SIZE (loop_write_refs); j++)
3870         {
3871           struct data_reference *dra =
3872             VARRAY_GENERIC_PTR (loop_write_refs, i);
3873           struct data_reference *drb =
3874             VARRAY_GENERIC_PTR (loop_write_refs, j);
3875           if (vect_analyze_data_ref_dependence (dra, drb, loop))
3876             return false;
3877         }
3878     }
3879
3880   /* Examine load-store (true/anti) dependences.  */
3881
3882   if (vect_debug_details (NULL))
3883     fprintf (dump_file, "compare all load-store pairs.");
3884
3885   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_refs); i++)
3886     {
3887       for (j = 0; j < VARRAY_ACTIVE_SIZE (loop_write_refs); j++)
3888         {
3889           struct data_reference *dra = VARRAY_GENERIC_PTR (loop_read_refs, i);
3890           struct data_reference *drb =
3891             VARRAY_GENERIC_PTR (loop_write_refs, j);
3892           if (vect_analyze_data_ref_dependence (dra, drb, loop))
3893             return false;
3894         }
3895     }
3896
3897   return true;
3898 }
3899
3900
3901 /* Function vect_get_first_index.
3902
3903    REF is a data reference.  
3904    If it is an ARRAY_REF: if its lower bound is simple enough, 
3905    put it in ARRAY_FIRST_INDEX and return TRUE; otherwise - return FALSE.
3906    If it is not an ARRAY_REF: REF has no "first index";
3907    ARRAY_FIRST_INDEX in zero, and the function returns TRUE.  */
3908
3909 static bool
3910 vect_get_first_index (tree ref, tree *array_first_index)
3911 {
3912   tree array_start;
3913
3914   if (TREE_CODE (ref) != ARRAY_REF)
3915     *array_first_index = size_zero_node;
3916   else
3917     {
3918       array_start = array_ref_low_bound (ref);
3919       if (!host_integerp (array_start,0))
3920         {
3921           if (vect_debug_details (NULL))
3922             {
3923               fprintf (dump_file, "array min val not simple integer cst.");
3924               print_generic_expr (dump_file, array_start, TDF_DETAILS);
3925             }
3926           return false;
3927         }
3928       *array_first_index = array_start;
3929     }
3930
3931   return true;
3932 }
3933
3934
3935 /* Function vect_compute_array_base_alignment.
3936    A utility function of vect_compute_array_ref_alignment.
3937
3938    Compute the misalignment of ARRAY in bits.
3939
3940    Input:
3941    ARRAY - an array_ref (possibly multidimensional) of type ARRAY_TYPE.
3942    VECTYPE - we are interested in the misalignment modulo the size of vectype.
3943              if NULL: don't compute misalignment, just return the base of ARRAY.
3944    PREV_DIMENSIONS - initialized to one.
3945    MISALIGNMENT - the computed misalignment in bits.
3946
3947    Output:
3948    If VECTYPE is not NULL:
3949      Return NULL_TREE if the misalignment cannot be computed. Otherwise, return 
3950      the base of the array, and put the computed misalignment in MISALIGNMENT. 
3951    If VECTYPE is NULL:
3952      Return the base of the array.
3953
3954    For a[idx_N]...[idx_2][idx_1][idx_0], the address of 
3955    a[idx_N]...[idx_2][idx_1] is 
3956    {&a + idx_1 * dim_0 + idx_2 * dim_0 * dim_1 + ...  
3957     ... + idx_N * dim_0 * ... * dim_N-1}. 
3958    (The misalignment of &a is not checked here).
3959    Note, that every term contains dim_0, therefore, if dim_0 is a 
3960    multiple of NUNITS, the whole sum is a multiple of NUNITS.
3961    Otherwise, if idx_1 is constant, and dim_1 is a multiple of
3962    NUINTS, we can say that the misalignment of the sum is equal to
3963    the misalignment of {idx_1 * dim_0}.  If idx_1 is not constant,
3964    we can't determine this array misalignment, and we return
3965    false. 
3966    We proceed recursively in this manner, accumulating total misalignment
3967    and the multiplication of previous dimensions for correct misalignment
3968    calculation.  */
3969
3970 static tree
3971 vect_compute_array_base_alignment (tree array,
3972                                    tree vectype,
3973                                    tree *prev_dimensions,
3974                                    tree *misalignment)
3975 {
3976   tree index;
3977   tree domain;
3978   tree dimension_size;
3979   tree mis;
3980   tree bits_per_vectype;
3981   tree bits_per_vectype_unit;
3982
3983   /* The 'stop condition' of the recursion.  */
3984   if (TREE_CODE (array) != ARRAY_REF)
3985     return array;
3986   
3987   if (!vectype)
3988     /* Just get the base decl.  */
3989     return vect_compute_array_base_alignment 
3990                 (TREE_OPERAND (array, 0), NULL, NULL, NULL);
3991
3992   if (!host_integerp (*misalignment, 1) || TREE_OVERFLOW (*misalignment) || 
3993       !host_integerp (*prev_dimensions, 1) || TREE_OVERFLOW (*prev_dimensions))
3994     return NULL_TREE;
3995
3996   domain = TYPE_DOMAIN (TREE_TYPE (array));
3997   dimension_size = 
3998         int_const_binop (PLUS_EXPR,
3999                 int_const_binop (MINUS_EXPR, TYPE_MAX_VALUE (domain), 
4000                                              TYPE_MIN_VALUE (domain), 1),
4001                 size_one_node, 1);
4002
4003   /* Check if the dimension size is a multiple of NUNITS, the remaining sum
4004      is a multiple of NUNITS: 
4005
4006      dimension_size % GET_MODE_NUNITS (TYPE_MODE (vectype)) == 0 ?
4007    */
4008   mis = int_const_binop (TRUNC_MOD_EXPR, dimension_size,
4009          build_int_cst (NULL_TREE, GET_MODE_NUNITS (TYPE_MODE (vectype))), 1);
4010   if (integer_zerop (mis))
4011     /* This array is aligned. Continue just in order to get the base decl.  */
4012     return vect_compute_array_base_alignment 
4013                 (TREE_OPERAND (array, 0), NULL, NULL, NULL);
4014
4015   index = TREE_OPERAND (array, 1);
4016   if (!host_integerp (index, 1))
4017     /* The current index is not constant.  */
4018     return NULL_TREE;
4019    
4020   index = int_const_binop (MINUS_EXPR, index, TYPE_MIN_VALUE (domain), 0);
4021
4022   bits_per_vectype = fold_convert (unsigned_type_node, 
4023     build_int_cst (NULL_TREE, BITS_PER_UNIT * 
4024                  GET_MODE_SIZE (TYPE_MODE (vectype))));
4025   bits_per_vectype_unit =  fold_convert (unsigned_type_node,
4026     build_int_cst (NULL_TREE, BITS_PER_UNIT * 
4027                  GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (vectype)))));
4028   
4029   /* Add {idx_i * dim_i-1 * ... * dim_0 } to the misalignment computed
4030      earlier:
4031
4032      *misalignment = 
4033        (*misalignment + index_val * dimension_size * *prev_dimensions) 
4034                                                         % vectype_nunits;
4035    */
4036
4037   mis = int_const_binop (MULT_EXPR, index, dimension_size, 1);
4038   mis = int_const_binop (MULT_EXPR, mis, *prev_dimensions, 1);
4039   mis = int_const_binop (MULT_EXPR, mis, bits_per_vectype_unit, 1);
4040   mis = int_const_binop (PLUS_EXPR, *misalignment, mis, 1);
4041   *misalignment = int_const_binop (TRUNC_MOD_EXPR, mis, bits_per_vectype, 1);
4042
4043
4044   *prev_dimensions = int_const_binop (MULT_EXPR, 
4045                                 *prev_dimensions, dimension_size, 1);
4046
4047   return vect_compute_array_base_alignment (TREE_OPERAND (array, 0), vectype,
4048                                             prev_dimensions,
4049                                             misalignment);
4050 }
4051
4052  
4053 /* Function vect_compute_data_ref_alignment
4054
4055    Compute the misalignment of the data reference DR.
4056
4057    Output:
4058    1. If during the misalignment computation it is found that the data reference
4059       cannot be vectorized then false is returned.
4060    2. DR_MISALIGNMENT (DR) is defined.
4061
4062    FOR NOW: No analysis is actually performed. Misalignment is calculated
4063    only for trivial cases. TODO.  */
4064
4065 static bool
4066 vect_compute_data_ref_alignment (struct data_reference *dr, 
4067                                  loop_vec_info loop_vinfo)
4068 {
4069   tree stmt = DR_STMT (dr);
4070   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);  
4071   tree ref = DR_REF (dr);
4072   tree vectype;
4073   tree scalar_type;
4074   tree offset = size_zero_node;
4075   tree base, bit_offset, alignment;
4076   tree unit_bits = fold_convert (unsigned_type_node, 
4077                                  build_int_cst (NULL_TREE, BITS_PER_UNIT));
4078   tree dr_base;
4079   bool base_aligned_p;
4080    
4081   if (vect_debug_details (NULL))
4082     fprintf (dump_file, "vect_compute_data_ref_alignment:");
4083
4084   /* Initialize misalignment to unknown.  */
4085   DR_MISALIGNMENT (dr) = -1;
4086
4087   scalar_type = TREE_TYPE (ref);
4088   vectype = get_vectype_for_scalar_type (scalar_type);
4089   if (!vectype)
4090     {
4091       if (vect_debug_details (NULL))
4092         {
4093           fprintf (dump_file, "no vectype for stmt: ");
4094           print_generic_expr (dump_file, stmt, TDF_SLIM);
4095           fprintf (dump_file, " scalar_type: ");
4096           print_generic_expr (dump_file, scalar_type, TDF_DETAILS);
4097         }
4098       /* It is not possible to vectorize this data reference.  */
4099       return false;
4100     }
4101   STMT_VINFO_VECTYPE (stmt_info) = vectype;
4102   gcc_assert (TREE_CODE (ref) == ARRAY_REF || TREE_CODE (ref) == INDIRECT_REF);
4103   
4104   if (TREE_CODE (ref) == ARRAY_REF)
4105     dr_base = ref;
4106   else
4107     dr_base = STMT_VINFO_VECT_DR_BASE (stmt_info);
4108
4109   base = vect_get_base_and_bit_offset (dr, dr_base, vectype, 
4110                           loop_vinfo, &bit_offset, &base_aligned_p);
4111   if (!base)
4112     {
4113       if (vect_debug_details (NULL)) 
4114         {
4115           fprintf (dump_file, "Unknown alignment for access: ");
4116           print_generic_expr (dump_file, 
4117                               STMT_VINFO_VECT_DR_BASE (stmt_info), TDF_SLIM);
4118         }
4119       return true;
4120     }
4121
4122   if (!base_aligned_p) 
4123     {
4124       if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
4125         {
4126           if (vect_debug_details (NULL))
4127             {
4128               fprintf (dump_file, "can't force alignment of ref: ");
4129               print_generic_expr (dump_file, ref, TDF_SLIM);
4130             }
4131           return true;
4132         }
4133       
4134       /* Force the alignment of the decl.
4135          NOTE: This is the only change to the code we make during
4136          the analysis phase, before deciding to vectorize the loop.  */
4137       if (vect_debug_details (NULL))
4138         fprintf (dump_file, "force alignment");
4139       DECL_ALIGN (base) = TYPE_ALIGN (vectype);
4140       DECL_USER_ALIGN (base) = TYPE_ALIGN (vectype);
4141     }
4142
4143   /* At this point we assume that the base is aligned, and the offset from it
4144      (including index, if relevant) has been computed and is in BIT_OFFSET.  */
4145   gcc_assert (base_aligned_p 
4146               || (TREE_CODE (base) == VAR_DECL 
4147                   && DECL_ALIGN (base) >= TYPE_ALIGN (vectype)));
4148
4149   /* Convert into bytes.  */
4150   offset = int_const_binop (TRUNC_DIV_EXPR, bit_offset, unit_bits, 1);
4151   /* Check that there is no remainder in bits.  */
4152   bit_offset = int_const_binop (TRUNC_MOD_EXPR, bit_offset, unit_bits, 1);
4153   if (!integer_zerop (bit_offset))
4154     {
4155       if (vect_debug_details (NULL))
4156         {
4157           fprintf (dump_file, "bit offset alignment: ");
4158           print_generic_expr (dump_file, bit_offset, TDF_SLIM);
4159         }
4160       return false;
4161     }
4162   
4163   /* Alignment required, in bytes:  */
4164   alignment = fold_convert (unsigned_type_node,
4165             build_int_cst (NULL_TREE, TYPE_ALIGN (vectype)/BITS_PER_UNIT));
4166
4167   /* Modulo alignment.  */
4168   offset = int_const_binop (TRUNC_MOD_EXPR, offset, alignment, 0);
4169   if (!host_integerp (offset, 1) || TREE_OVERFLOW (offset))
4170     {
4171       if (vect_debug_details (NULL))
4172         fprintf (dump_file, "unexpected misalign value");
4173       return false;
4174     }
4175
4176   DR_MISALIGNMENT (dr) = tree_low_cst (offset, 1);
4177
4178   if (vect_debug_details (NULL))
4179     fprintf (dump_file, "misalign = %d", DR_MISALIGNMENT (dr));
4180
4181   return true;
4182 }
4183
4184
4185 /* Function vect_compute_array_ref_alignment
4186
4187    Compute the alignment of an array-ref.
4188    The alignment we compute here is relative to 
4189    TYPE_ALIGN(VECTYPE) boundary.  
4190
4191    Output:
4192    OFFSET - the alignment in bits
4193    Return value - the base of the array-ref. E.g, 
4194                   if the array-ref is a.b[k].c[i][j] the returned
4195                   base is a.b[k].c
4196 */
4197
4198 static tree
4199 vect_compute_array_ref_alignment (struct data_reference *dr,
4200                                   loop_vec_info loop_vinfo,
4201                                   tree vectype,
4202                                   tree *offset)
4203 {
4204   tree array_first_index = size_zero_node;
4205   tree init;
4206   tree ref = DR_REF (dr);
4207   tree scalar_type = TREE_TYPE (ref);
4208   tree oprnd0 = TREE_OPERAND (ref, 0);
4209   tree dims = size_one_node;  
4210   tree misalign = size_zero_node;
4211   tree next_ref, this_offset = size_zero_node;
4212   tree nunits;
4213   tree nbits;
4214
4215   if (TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
4216     /* The reference is an array without its last index.  */
4217     next_ref = vect_compute_array_base_alignment (ref, vectype, &dims, 
4218                                                   &misalign);
4219   else
4220     next_ref = vect_compute_array_base_alignment (oprnd0, vectype, &dims, 
4221                                                   &misalign);
4222   if (!vectype)
4223     /* Alignment is not requested. Just return the base.  */
4224     return next_ref;
4225
4226   /* Compute alignment.  */
4227   if (!host_integerp (misalign, 1) || TREE_OVERFLOW (misalign) || !next_ref)
4228     return NULL_TREE;
4229   this_offset = misalign;
4230
4231   /* Check the first index accessed.  */
4232   if (!vect_get_first_index (ref, &array_first_index))
4233     {
4234       if (vect_debug_details (NULL))
4235         fprintf (dump_file, "no first_index for array.");
4236       return NULL_TREE;
4237     }
4238
4239   /* Check the index of the array_ref.  */
4240   init = initial_condition_in_loop_num (DR_ACCESS_FN (dr, 0), 
4241                                         LOOP_VINFO_LOOP (loop_vinfo)->num);
4242
4243   /* FORNOW: In order to simplify the handling of alignment, we make sure
4244      that the first location at which the array is accessed ('init') is on an
4245      'NUNITS' boundary, since we are assuming here that 'array base' is aligned. 
4246      This is too conservative, since we require that
4247      both {'array_base' is a multiple of NUNITS} && {'init' is a multiple of
4248      NUNITS}, instead of just {('array_base' + 'init') is a multiple of NUNITS}.
4249      This should be relaxed in the future.  */
4250
4251   if (!init || !host_integerp (init, 0))
4252     {
4253       if (vect_debug_details (NULL))
4254         fprintf (dump_file, "non constant init. ");
4255       return NULL_TREE;
4256     }
4257
4258   /* bytes per scalar element: */
4259   nunits = fold_convert (unsigned_type_node,
4260         build_int_cst (NULL_TREE, GET_MODE_SIZE (TYPE_MODE (scalar_type))));
4261   nbits = int_const_binop (MULT_EXPR, nunits,     
4262                            build_int_cst (NULL_TREE, BITS_PER_UNIT), 1);
4263
4264   /* misalign = offset + (init-array_first_index)*nunits*bits_in_byte */
4265   misalign = int_const_binop (MINUS_EXPR, init, array_first_index, 0);
4266   misalign = int_const_binop (MULT_EXPR, misalign, nbits, 0);
4267   misalign = int_const_binop (PLUS_EXPR, misalign, this_offset, 0);
4268
4269   /* TODO: allow negative misalign values.  */
4270   if (!host_integerp (misalign, 1) || TREE_OVERFLOW (misalign))
4271     {
4272       if (vect_debug_details (NULL))
4273         fprintf (dump_file, "unexpected misalign value");
4274       return NULL_TREE;
4275     }
4276   *offset = misalign;
4277   return next_ref;
4278 }
4279
4280
4281 /* Function vect_compute_data_refs_alignment
4282
4283    Compute the misalignment of data references in the loop.
4284    This pass may take place at function granularity instead of at loop
4285    granularity.
4286
4287    FOR NOW: No analysis is actually performed. Misalignment is calculated
4288    only for trivial cases. TODO.  */
4289
4290 static bool
4291 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo)
4292 {
4293   varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
4294   varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
4295   unsigned int i;
4296
4297   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
4298     {
4299       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
4300       if (!vect_compute_data_ref_alignment (dr, loop_vinfo))
4301         return false;
4302     }
4303
4304   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
4305     {
4306       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
4307       if (!vect_compute_data_ref_alignment (dr, loop_vinfo))
4308         return false;
4309     }
4310
4311   return true;
4312 }
4313
4314
4315 /* Function vect_enhance_data_refs_alignment
4316
4317    This pass will use loop versioning and loop peeling in order to enhance
4318    the alignment of data references in the loop.
4319
4320    FOR NOW: we assume that whatever versioning/peeling takes place, only the
4321    original loop is to be vectorized; Any other loops that are created by
4322    the transformations performed in this pass - are not supposed to be
4323    vectorized. This restriction will be relaxed.
4324
4325    FOR NOW: No transformation is actually performed. TODO.  */
4326
4327 static void
4328 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
4329 {
4330   varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
4331   varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
4332   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4333   unsigned int i;
4334
4335   /*
4336      This pass will require a cost model to guide it whether to apply peeling 
4337      or versioning or a combination of the two. For example, the scheme that
4338      intel uses when given a loop with several memory accesses, is as follows:
4339      choose one memory access ('p') which alignment you want to force by doing 
4340      peeling. Then, either (1) generate a loop in which 'p' is aligned and all 
4341      other accesses are not necessarily aligned, or (2) use loop versioning to 
4342      generate one loop in which all accesses are aligned, and another loop in 
4343      which only 'p' is necessarily aligned. 
4344
4345      ("Automatic Intra-Register Vectorization for the Intel Architecture",
4346       Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
4347       Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)     
4348
4349      Devising a cost model is the most critical aspect of this work. It will 
4350      guide us on which access to peel for, whether to use loop versioning, how 
4351      many versions to create, etc. The cost model will probably consist of 
4352      generic considerations as well as target specific considerations (on 
4353      powerpc for example, misaligned stores are more painful than misaligned 
4354      loads). 
4355
4356      Here is the general steps involved in alignment enhancements:
4357     
4358      -- original loop, before alignment analysis:
4359         for (i=0; i<N; i++){
4360           x = q[i];                     # DR_MISALIGNMENT(q) = unknown
4361           p[i] = y;                     # DR_MISALIGNMENT(p) = unknown
4362         }
4363
4364      -- After vect_compute_data_refs_alignment:
4365         for (i=0; i<N; i++){
4366           x = q[i];                     # DR_MISALIGNMENT(q) = 3
4367           p[i] = y;                     # DR_MISALIGNMENT(p) = unknown
4368         }
4369
4370      -- Possibility 1: we do loop versioning:
4371      if (p is aligned) {
4372         for (i=0; i<N; i++){    # loop 1A
4373           x = q[i];                     # DR_MISALIGNMENT(q) = 3
4374           p[i] = y;                     # DR_MISALIGNMENT(p) = 0
4375         }
4376      } 
4377      else {
4378         for (i=0; i<N; i++){    # loop 1B
4379           x = q[i];                     # DR_MISALIGNMENT(q) = 3
4380           p[i] = y;                     # DR_MISALIGNMENT(p) = unaligned
4381         }
4382      }
4383    
4384      -- Possibility 2: we do loop peeling:
4385      for (i = 0; i < 3; i++){   # (scalar loop, not to be vectorized).
4386         x = q[i];
4387         p[i] = y;
4388      }
4389      for (i = 3; i < N; i++){   # loop 2A
4390         x = q[i];                       # DR_MISALIGNMENT(q) = 0
4391         p[i] = y;                       # DR_MISALIGNMENT(p) = unknown
4392      }
4393
4394      -- Possibility 3: combination of loop peeling and versioning:
4395      for (i = 0; i < 3; i++){   # (scalar loop, not to be vectorized).
4396         x = q[i];
4397         p[i] = y;
4398      }
4399      if (p is aligned) {
4400         for (i = 3; i<N; i++){  # loop 3A
4401           x = q[i];                     # DR_MISALIGNMENT(q) = 0
4402           p[i] = y;                     # DR_MISALIGNMENT(p) = 0
4403         }
4404      } 
4405      else {
4406         for (i = 3; i<N; i++){  # loop 3B
4407           x = q[i];                     # DR_MISALIGNMENT(q) = 0
4408           p[i] = y;                     # DR_MISALIGNMENT(p) = unaligned
4409         }
4410      }
4411
4412      These loops are later passed to loop_transform to be vectorized. The 
4413      vectorizer will use the alignment information to guide the transformation 
4414      (whether to generate regular loads/stores, or with special handling for 
4415      misalignment). 
4416    */
4417
4418   /* (1) Peeling to force alignment.  */
4419
4420   /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
4421      Considerations:
4422      + How many accesses will become aligned due to the peeling
4423      - How many accesses will become unaligned due to the peeling,
4424        and the cost of misaligned accesses.
4425      - The cost of peeling (the extra runtime checks, the increase 
4426        in code size).
4427
4428      The scheme we use FORNOW: peel to force the alignment of the first
4429      misaligned store in the loop.
4430      Rationale: misaligned store are not yet supported.
4431
4432      TODO: Use a better cost model.  */
4433
4434   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
4435     {
4436       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
4437       if (!aligned_access_p (dr))
4438         {
4439           LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr;
4440           LOOP_DO_PEELING_FOR_ALIGNMENT (loop_vinfo) = true;
4441           break;
4442         }
4443     }
4444
4445   if (!LOOP_VINFO_UNALIGNED_DR (loop_vinfo))
4446     {
4447       if (vect_debug_details (loop))
4448         fprintf (dump_file, "Peeling for alignment will not be applied.");
4449       return;
4450     }
4451   else
4452     if (vect_debug_details (loop))
4453       fprintf (dump_file, "Peeling for alignment will be applied.");
4454
4455
4456   /* (1.2) Update the alignment info according to the peeling factor.
4457            If the misalignment of the DR we peel for is M, then the
4458            peeling factor is VF - M, and the misalignment of each access DR_i
4459            in the loop is DR_MISALIGNMENT (DR_i) + VF - M.
4460            If the misalignment of the DR we peel for is unknown, then the 
4461            misalignment of each access DR_i in the loop is also unknown.
4462
4463            FORNOW: set the misalignment of the accesses to unknown even
4464                    if the peeling factor is known at compile time.
4465
4466            TODO: - if the peeling factor is known at compile time, use that
4467                    when updating the misalignment info of the loop DRs.
4468                  - consider accesses that are known to have the same 
4469                    alignment, even if that alignment is unknown.  */
4470    
4471   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
4472     {
4473       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
4474       if (dr == LOOP_VINFO_UNALIGNED_DR (loop_vinfo))
4475         DR_MISALIGNMENT (dr) = 0;
4476       else
4477         DR_MISALIGNMENT (dr) = -1;
4478     }
4479   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
4480     {
4481       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
4482       if (dr == LOOP_VINFO_UNALIGNED_DR (loop_vinfo))
4483         DR_MISALIGNMENT (dr) = 0;
4484       else
4485         DR_MISALIGNMENT (dr) = -1;
4486     }
4487 }
4488
4489
4490 /* Function vect_analyze_data_refs_alignment
4491
4492    Analyze the alignment of the data-references in the loop.
4493    FOR NOW: Until support for misliagned accesses is in place, only if all
4494    accesses are aligned can the loop be vectorized. This restriction will be 
4495    relaxed.  */ 
4496
4497 static bool
4498 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
4499 {
4500   varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
4501   varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
4502   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4503   enum dr_alignment_support supportable_dr_alignment;
4504   unsigned int i;
4505
4506   if (vect_debug_details (NULL))
4507     fprintf (dump_file, "\n<<vect_analyze_data_refs_alignment>>\n");
4508
4509
4510   /* This pass may take place at function granularity instead of at loop
4511      granularity.  */
4512
4513   if (!vect_compute_data_refs_alignment (loop_vinfo))
4514     {
4515       if (vect_debug_details (loop) || vect_debug_stats (loop))
4516         fprintf (dump_file, 
4517                  "not vectorized: can't calculate alignment for data ref.");
4518       return false;
4519     }
4520
4521
4522   /* This pass will decide on using loop versioning and/or loop peeling in 
4523      order to enhance the alignment of data references in the loop.  */
4524
4525   vect_enhance_data_refs_alignment (loop_vinfo);
4526
4527
4528   /* Finally, check that all the data references in the loop can be
4529      handled with respect to their alignment.  */
4530
4531   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
4532     {
4533       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
4534       supportable_dr_alignment = vect_supportable_dr_alignment (dr);
4535       if (!supportable_dr_alignment)
4536         {
4537           if (vect_debug_details (loop) || vect_debug_stats (loop))
4538             fprintf (dump_file, "not vectorized: unsupported unaligned load.");
4539           return false;
4540         }
4541     }
4542   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
4543     {
4544       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
4545       supportable_dr_alignment = vect_supportable_dr_alignment (dr);
4546       if (!supportable_dr_alignment)
4547         {
4548           if (vect_debug_details (loop) || vect_debug_stats (loop))
4549             fprintf (dump_file, "not vectorized: unsupported unaligned store.");
4550           return false;
4551         }
4552     }
4553
4554   return true;
4555 }
4556
4557
4558 /* Function vect_analyze_data_ref_access.
4559
4560    Analyze the access pattern of the data-reference DR. For now, a data access
4561    has to consecutive and aligned to be considered vectorizable.  */
4562
4563 static bool
4564 vect_analyze_data_ref_access (struct data_reference *dr)
4565 {
4566   varray_type access_fns = DR_ACCESS_FNS (dr);
4567   tree access_fn;
4568   tree init, step;
4569   unsigned int dimensions, i;
4570
4571   /* Check that in case of multidimensional array ref A[i1][i2]..[iN],
4572      i1, i2, ..., iN-1 are loop invariant (to make sure that the memory
4573      access is contiguous).  */
4574   dimensions = VARRAY_ACTIVE_SIZE (access_fns);
4575
4576   for (i = 1; i < dimensions; i++) /* Not including the last dimension.  */
4577     {
4578       access_fn = DR_ACCESS_FN (dr, i);
4579
4580       if (evolution_part_in_loop_num (access_fn, 
4581                                       loop_containing_stmt (DR_STMT (dr))->num))
4582         {
4583           /* Evolution part is not NULL in this loop (it is neither constant 
4584              nor invariant).  */
4585           if (vect_debug_details (NULL))
4586             {
4587               fprintf (dump_file, 
4588                        "not vectorized: complicated multidim. array access.");
4589               print_generic_expr (dump_file, access_fn, TDF_SLIM);
4590             }
4591           return false;
4592         }
4593     }
4594   
4595   access_fn = DR_ACCESS_FN (dr, 0); /*  The last dimension access function.  */
4596   if (!evolution_function_is_constant_p (access_fn)
4597       && !vect_is_simple_iv_evolution (loop_containing_stmt (DR_STMT (dr))->num,
4598                                        access_fn, &init, &step, true))
4599     {
4600       if (vect_debug_details (NULL))
4601         {
4602           fprintf (dump_file, "not vectorized: complicated access function.");
4603           print_generic_expr (dump_file, access_fn, TDF_SLIM);
4604         }
4605       return false;
4606     }
4607   
4608   return true;
4609 }
4610
4611
4612 /* Function vect_analyze_data_ref_accesses.
4613
4614    Analyze the access pattern of all the data references in the loop.
4615
4616    FORNOW: the only access pattern that is considered vectorizable is a
4617            simple step 1 (consecutive) access.
4618
4619    FORNOW: handle only arrays and pointer accesses.  */
4620
4621 static bool
4622 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo)
4623 {
4624   unsigned int i;
4625   varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
4626   varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
4627
4628   if (vect_debug_details (NULL))
4629     fprintf (dump_file, "\n<<vect_analyze_data_ref_accesses>>\n");
4630
4631   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
4632     {
4633       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
4634       bool ok = vect_analyze_data_ref_access (dr);
4635       if (!ok)
4636         {
4637           if (vect_debug_stats (LOOP_VINFO_LOOP (loop_vinfo))
4638               || vect_debug_details (LOOP_VINFO_LOOP (loop_vinfo)))
4639             fprintf (dump_file, "not vectorized: complicated access pattern.");
4640           return false;
4641         }
4642     }
4643
4644   for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
4645     {
4646       struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
4647       bool ok = vect_analyze_data_ref_access (dr);
4648       if (!ok)
4649         {
4650           if (vect_debug_stats (LOOP_VINFO_LOOP (loop_vinfo))
4651               || vect_debug_details (LOOP_VINFO_LOOP (loop_vinfo))) 
4652             fprintf (dump_file, "not vectorized: complicated access pattern.");
4653           return false;
4654         }
4655     }
4656
4657   return true;
4658 }
4659
4660
4661 /* Function vect_analyze_pointer_ref_access.
4662
4663    Input:
4664    STMT - a stmt that contains a data-ref
4665    MEMREF - a data-ref in STMT, which is an INDIRECT_REF.
4666
4667    If the data-ref access is vectorizable, return a data_reference structure
4668    that represents it (DR). Otherwise - return NULL.  */
4669
4670 static struct data_reference *
4671 vect_analyze_pointer_ref_access (tree memref, tree stmt, bool is_read)
4672 {
4673   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4674   struct loop *loop = STMT_VINFO_LOOP (stmt_info);
4675   tree access_fn = analyze_scalar_evolution (loop, TREE_OPERAND (memref, 0));
4676   tree init, step;      
4677   int step_val;
4678   tree reftype, innertype;
4679   enum machine_mode innermode;
4680   tree indx_access_fn; 
4681   int loopnum = loop->num;
4682   struct data_reference *dr;
4683
4684   if (!access_fn)
4685     {
4686       if (vect_debug_stats (loop) || vect_debug_details (loop))
4687         fprintf (dump_file, "not vectorized: complicated pointer access.");     
4688       return NULL;
4689     }
4690
4691   if (vect_debug_details (NULL))
4692     {
4693       fprintf (dump_file, "Access function of ptr: ");
4694       print_generic_expr (dump_file, access_fn, TDF_SLIM);
4695     }
4696
4697   if (!vect_is_simple_iv_evolution (loopnum, access_fn, &init, &step, false))
4698     {
4699       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
4700         fprintf (dump_file, "not vectorized: pointer access is not simple.");   
4701       return NULL;
4702     }
4703                 
4704   STRIP_NOPS (init);
4705
4706   if (!host_integerp (step,0))
4707     {
4708       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
4709         fprintf (dump_file, 
4710                 "not vectorized: non constant step for pointer access.");       
4711       return NULL;
4712     }
4713
4714   step_val = TREE_INT_CST_LOW (step);
4715
4716   reftype = TREE_TYPE (TREE_OPERAND (memref, 0));
4717   if (TREE_CODE (reftype) != POINTER_TYPE) 
4718     {
4719       if (vect_debug_stats (loop) || vect_debug_details (loop))
4720         fprintf (dump_file, "not vectorized: unexpected pointer access form."); 
4721       return NULL;
4722     }
4723
4724   reftype = TREE_TYPE (init);
4725   if (TREE_CODE (reftype) != POINTER_TYPE) 
4726     {
4727       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
4728         fprintf (dump_file, "not vectorized: unexpected pointer access form.");
4729       return NULL;
4730     }
4731
4732   innertype = TREE_TYPE (reftype);
4733   innermode = TYPE_MODE (innertype);
4734   if (GET_MODE_SIZE (innermode) != step_val) 
4735     {
4736       /* FORNOW: support only consecutive access */
4737       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
4738         fprintf (dump_file, "not vectorized: non consecutive access."); 
4739       return NULL;
4740     }
4741
4742   indx_access_fn = 
4743         build_polynomial_chrec (loopnum, integer_zero_node, integer_one_node);
4744   if (vect_debug_details (NULL)) 
4745     {
4746       fprintf (dump_file, "Access function of ptr indx: ");
4747       print_generic_expr (dump_file, indx_access_fn, TDF_SLIM);
4748     }
4749   dr = init_data_ref (stmt, memref, init, indx_access_fn, is_read);
4750   return dr;
4751 }
4752
4753
4754 /* Function vect_get_symbl_and_dr.  
4755
4756    The function returns SYMBL - the relevant variable for
4757    memory tag (for aliasing purposes). 
4758    Also data reference structure DR is created.  
4759
4760    Input:
4761    MEMREF - data reference in STMT
4762    IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
4763    
4764    Output:
4765    DR - data_reference struct for MEMREF
4766    return value - the relevant variable for memory tag (for aliasing purposes).
4767
4768 */ 
4769
4770 static tree
4771 vect_get_symbl_and_dr (tree memref, tree stmt, bool is_read, 
4772                        loop_vec_info loop_vinfo, struct data_reference **dr)
4773 {
4774   tree symbl, oprnd0, oprnd1;
4775   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4776   tree offset;
4777   tree array_base, base;
4778   struct data_reference *new_dr;
4779   bool base_aligned_p;
4780
4781   *dr = NULL;
4782   switch (TREE_CODE (memref))
4783     {
4784     case INDIRECT_REF:
4785       new_dr = vect_analyze_pointer_ref_access (memref, stmt, is_read);
4786       if (! new_dr)
4787         return NULL_TREE; 
4788       *dr = new_dr;
4789       symbl = DR_BASE_NAME (new_dr);
4790       STMT_VINFO_VECT_DR_BASE (stmt_info) = symbl;
4791
4792       switch (TREE_CODE (symbl))
4793         {
4794         case PLUS_EXPR:
4795         case MINUS_EXPR:
4796           oprnd0 = TREE_OPERAND (symbl, 0);
4797           oprnd1 = TREE_OPERAND (symbl, 1);
4798
4799           STRIP_NOPS(oprnd1);
4800           /* Only {address_base + offset} expressions are supported,  
4801              where address_base can be POINTER_TYPE or ARRAY_TYPE and 
4802              offset can be anything but POINTER_TYPE or ARRAY_TYPE.  
4803              TODO: swap operands if {offset + address_base}.  */
4804           if ((TREE_CODE (TREE_TYPE (oprnd1)) == POINTER_TYPE 
4805                && TREE_CODE (oprnd1) != INTEGER_CST)
4806               || TREE_CODE (TREE_TYPE (oprnd1)) == ARRAY_TYPE)
4807             return NULL_TREE;
4808
4809           if (TREE_CODE (TREE_TYPE (oprnd0)) == POINTER_TYPE)
4810             symbl = oprnd0;
4811           else
4812             symbl = vect_get_symbl_and_dr (oprnd0, stmt, is_read, 
4813                                            loop_vinfo, &new_dr); 
4814
4815         case SSA_NAME:
4816         case ADDR_EXPR:
4817           /* symbl remains unchanged.  */
4818           break;
4819
4820         default:
4821           if (vect_debug_details (NULL))
4822             {
4823               fprintf (dump_file, "unhandled data ref: ");
4824               print_generic_expr (dump_file, memref, TDF_SLIM);
4825               fprintf (dump_file, " (symbl ");
4826               print_generic_expr (dump_file, symbl, TDF_SLIM);
4827               fprintf (dump_file, ") in stmt  ");
4828               print_generic_expr (dump_file, stmt, TDF_SLIM);
4829             }
4830           return NULL_TREE;     
4831         }
4832       break;
4833
4834     case ARRAY_REF:
4835       offset = size_zero_node;
4836
4837       /* Store the array base in the stmt info. 
4838          For one dimensional array ref a[i], the base is a,
4839          for multidimensional a[i1][i2]..[iN], the base is 
4840          a[i1][i2]..[iN-1].  */
4841       array_base = TREE_OPERAND (memref, 0);
4842       STMT_VINFO_VECT_DR_BASE (stmt_info) = array_base;      
4843
4844       new_dr = analyze_array (stmt, memref, is_read);
4845       *dr = new_dr;
4846
4847       /* Find the relevant symbol for aliasing purposes.  */    
4848       base = DR_BASE_NAME (new_dr);
4849       switch (TREE_CODE (base)) 
4850         {
4851         case VAR_DECL:
4852           symbl = base;
4853           break;
4854
4855         case INDIRECT_REF:
4856           symbl = TREE_OPERAND (base, 0); 
4857           break;
4858
4859         case COMPONENT_REF:
4860           /* Could have recorded more accurate information - 
4861              i.e, the actual FIELD_DECL that is being referenced -
4862              but later passes expect VAR_DECL as the nmt.  */   
4863           symbl = vect_get_base_and_bit_offset (new_dr, base, NULL_TREE, 
4864                                         loop_vinfo, &offset, &base_aligned_p);
4865           if (symbl)
4866             break;
4867           /* fall through */    
4868         default:
4869           if (vect_debug_details (NULL))
4870             {
4871               fprintf (dump_file, "unhandled struct/class field access ");
4872               print_generic_expr (dump_file, stmt, TDF_SLIM);
4873             }
4874           return NULL_TREE;
4875         }
4876       break;
4877
4878     default:
4879       if (vect_debug_details (NULL))
4880         {
4881           fprintf (dump_file, "unhandled data ref: ");
4882           print_generic_expr (dump_file, memref, TDF_SLIM);
4883           fprintf (dump_file, " in stmt  ");
4884           print_generic_expr (dump_file, stmt, TDF_SLIM);
4885         }
4886       return NULL_TREE;
4887     }
4888   return symbl;
4889 }
4890
4891
4892 /* Function vect_analyze_data_refs.
4893
4894    Find all the data references in the loop.
4895
4896    FORNOW: Handle aligned INDIRECT_REFs and ARRAY_REFs 
4897            which base is really an array (not a pointer) and which alignment 
4898            can be forced. This restriction will be relaxed.  */
4899
4900 static bool
4901 vect_analyze_data_refs (loop_vec_info loop_vinfo)
4902 {
4903   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4904   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
4905   int nbbs = loop->num_nodes;
4906   block_stmt_iterator si;
4907   int j;
4908   struct data_reference *dr;
4909   tree tag;
4910   tree address_base;
4911   bool base_aligned_p;
4912   tree offset;
4913
4914   if (vect_debug_details (NULL))
4915     fprintf (dump_file, "\n<<vect_analyze_data_refs>>\n");
4916
4917   for (j = 0; j < nbbs; j++)
4918     {
4919       basic_block bb = bbs[j];
4920       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4921         {
4922           bool is_read = false;
4923           tree stmt = bsi_stmt (si);
4924           stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4925           v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
4926           v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
4927           vuse_optype vuses = STMT_VUSE_OPS (stmt);
4928           varray_type *datarefs = NULL;
4929           int nvuses, nv_may_defs, nv_must_defs;
4930           tree memref = NULL;
4931           tree symbl;
4932
4933           /* Assumption: there exists a data-ref in stmt, if and only if 
4934              it has vuses/vdefs.  */
4935
4936           if (!vuses && !v_may_defs && !v_must_defs)
4937             continue;
4938
4939           nvuses = NUM_VUSES (vuses);
4940           nv_may_defs = NUM_V_MAY_DEFS (v_may_defs);
4941           nv_must_defs = NUM_V_MUST_DEFS (v_must_defs);
4942
4943           if (nvuses && (nv_may_defs || nv_must_defs))
4944             {
4945               if (vect_debug_details (NULL))
4946                 {
4947                   fprintf (dump_file, "unexpected vdefs and vuses in stmt: ");
4948                   print_generic_expr (dump_file, stmt, TDF_SLIM);
4949                 }
4950               return false;
4951             }
4952
4953           if (TREE_CODE (stmt) != MODIFY_EXPR)
4954             {
4955               if (vect_debug_details (NULL))
4956                 {
4957                   fprintf (dump_file, "unexpected vops in stmt: ");
4958                   print_generic_expr (dump_file, stmt, TDF_SLIM);
4959                 }
4960               return false;
4961             }
4962
4963           if (vuses)
4964             {
4965               memref = TREE_OPERAND (stmt, 1);
4966               datarefs = &(LOOP_VINFO_DATAREF_READS (loop_vinfo));
4967               is_read = true;
4968             } 
4969           else /* vdefs */
4970             {
4971               memref = TREE_OPERAND (stmt, 0);
4972               datarefs = &(LOOP_VINFO_DATAREF_WRITES (loop_vinfo));
4973               is_read = false;
4974             }
4975
4976           /* Analyze MEMREF. If it is of a supported form, build data_reference
4977              struct for it (DR) and find the relevant symbol for aliasing 
4978              purposes.  */
4979           symbl = vect_get_symbl_and_dr (memref, stmt, is_read, loop_vinfo, 
4980                                          &dr);
4981           if (!symbl)
4982             {
4983               if (vect_debug_stats (loop) || vect_debug_details (loop))
4984                 {
4985                   fprintf (dump_file, "not vectorized: unhandled data ref: "); 
4986                   print_generic_expr (dump_file, stmt, TDF_SLIM);
4987                 }
4988               return false;
4989             }
4990
4991           /* Find and record the memtag assigned to this data-ref.  */
4992            switch (TREE_CODE (symbl))
4993             {
4994             case VAR_DECL:
4995               STMT_VINFO_MEMTAG (stmt_info) = symbl;
4996               break;
4997               
4998             case SSA_NAME:
4999               symbl = SSA_NAME_VAR (symbl);
5000               tag = get_var_ann (symbl)->type_mem_tag;
5001               if (!tag)
5002                 {
5003                   tree ptr = TREE_OPERAND (memref, 0);
5004                   if (TREE_CODE (ptr) == SSA_NAME)
5005                     tag = get_var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
5006                 }
5007               if (!tag)
5008                 {
5009                   if (vect_debug_stats (loop) || vect_debug_details (loop))
5010                     fprintf (dump_file, "not vectorized: no memtag for ref.");
5011                   return false;
5012                 }
5013               STMT_VINFO_MEMTAG (stmt_info) = tag;
5014               break;
5015
5016             case ADDR_EXPR:
5017               address_base = TREE_OPERAND (symbl, 0);
5018
5019               switch (TREE_CODE (address_base))
5020                 {
5021                 case ARRAY_REF:
5022                   dr = analyze_array (stmt, TREE_OPERAND (symbl, 0), 
5023                                       DR_IS_READ(dr));
5024                   STMT_VINFO_MEMTAG (stmt_info) = 
5025                      vect_get_base_and_bit_offset (dr, DR_BASE_NAME (dr), NULL_TREE,
5026                                                    loop_vinfo, &offset, 
5027                                                    &base_aligned_p);
5028                   break;
5029                   
5030                 case VAR_DECL: 
5031                   STMT_VINFO_MEMTAG (stmt_info) = address_base;
5032                   break;
5033
5034                 default:
5035                   if (vect_debug_stats (loop) || vect_debug_details (loop))
5036                     {
5037                       fprintf (dump_file, 
5038                                "not vectorized: unhandled address expr: ");
5039                       print_generic_expr (dump_file, stmt, TDF_SLIM);
5040                     }
5041                   return false;
5042                 }
5043               break;
5044               
5045             default:
5046               if (vect_debug_stats (loop) || vect_debug_details (loop))
5047                 {
5048                   fprintf (dump_file, "not vectorized: unsupported data-ref: ");
5049                   print_generic_expr (dump_file, memref, TDF_SLIM);
5050                 }
5051               return false;
5052             }
5053
5054           VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
5055           STMT_VINFO_DATA_REF (stmt_info) = dr;
5056         }
5057     }
5058
5059   return true;
5060 }
5061
5062
5063 /* Utility functions used by vect_mark_stmts_to_be_vectorized.  */
5064
5065 /* Function vect_mark_relevant.
5066
5067    Mark STMT as "relevant for vectorization" and add it to WORKLIST.  */
5068
5069 static void
5070 vect_mark_relevant (varray_type worklist, tree stmt)
5071 {
5072   stmt_vec_info stmt_info;
5073
5074   if (vect_debug_details (NULL))
5075     fprintf (dump_file, "mark relevant.");
5076
5077   if (TREE_CODE (stmt) == PHI_NODE)
5078     {
5079       VARRAY_PUSH_TREE (worklist, stmt);
5080       return;
5081     }
5082
5083   stmt_info = vinfo_for_stmt (stmt);
5084
5085   if (!stmt_info)
5086     {
5087       if (vect_debug_details (NULL))
5088         {
5089           fprintf (dump_file, "mark relevant: no stmt info!!.");
5090           print_generic_expr (dump_file, stmt, TDF_SLIM);
5091         }
5092       return;
5093     }
5094
5095   if (STMT_VINFO_RELEVANT_P (stmt_info))
5096     {
5097       if (vect_debug_details (NULL))
5098         fprintf (dump_file, "already marked relevant.");
5099       return;
5100     }
5101
5102   STMT_VINFO_RELEVANT_P (stmt_info) = 1;
5103   VARRAY_PUSH_TREE (worklist, stmt);
5104 }
5105
5106
5107 /* Function vect_stmt_relevant_p.
5108
5109    Return true if STMT in loop that is represented by LOOP_VINFO is
5110    "relevant for vectorization".
5111
5112    A stmt is considered "relevant for vectorization" if:
5113    - it has uses outside the loop.
5114    - it has vdefs (it alters memory).
5115    - control stmts in the loop (except for the exit condition).
5116
5117    CHECKME: what other side effects would the vectorizer allow?  */
5118
5119 static bool
5120 vect_stmt_relevant_p (tree stmt, loop_vec_info loop_vinfo)
5121 {
5122   v_may_def_optype v_may_defs;
5123   v_must_def_optype v_must_defs;
5124   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5125   int i;
5126   dataflow_t df;
5127   int num_uses;
5128
5129   /* cond stmt other than loop exit cond.  */
5130   if (is_ctrl_stmt (stmt) && (stmt != LOOP_VINFO_EXIT_COND (loop_vinfo)))
5131     return true;
5132
5133   /* changing memory.  */
5134   v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
5135   v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
5136   if (v_may_defs || v_must_defs)
5137     {
5138       if (vect_debug_details (NULL))
5139         fprintf (dump_file, "vec_stmt_relevant_p: stmt has vdefs.");
5140       return true;
5141     }
5142
5143   /* uses outside the loop.  */
5144   df = get_immediate_uses (stmt);
5145   num_uses = num_immediate_uses (df);
5146   for (i = 0; i < num_uses; i++)
5147     {
5148       tree use = immediate_use (df, i);
5149       basic_block bb = bb_for_stmt (use);
5150       if (!flow_bb_inside_loop_p (loop, bb))
5151         {
5152           if (vect_debug_details (NULL))
5153             fprintf (dump_file, "vec_stmt_relevant_p: used out of loop.");
5154           return true;
5155         }
5156     }
5157
5158   return false;
5159 }
5160
5161
5162 /* Function vect_mark_stmts_to_be_vectorized.
5163
5164    Not all stmts in the loop need to be vectorized. For example:
5165
5166      for i...
5167        for j...
5168    1.    T0 = i + j
5169    2.    T1 = a[T0]
5170
5171    3.    j = j + 1
5172
5173    Stmt 1 and 3 do not need to be vectorized, because loop control and
5174    addressing of vectorized data-refs are handled differently.
5175
5176    This pass detects such stmts.  */
5177
5178 static bool
5179 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
5180 {
5181   varray_type worklist;
5182   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5183   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
5184   unsigned int nbbs = loop->num_nodes;
5185   block_stmt_iterator si;
5186   tree stmt;
5187   stmt_ann_t ann;
5188   unsigned int i;
5189   int j;
5190   use_optype use_ops;
5191   stmt_vec_info stmt_info;
5192
5193   if (vect_debug_details (NULL))
5194     fprintf (dump_file, "\n<<vect_mark_stmts_to_be_vectorized>>\n");
5195
5196   VARRAY_TREE_INIT (worklist, 64, "work list");
5197
5198   /* 1. Init worklist.  */
5199
5200   for (i = 0; i < nbbs; i++)
5201     {
5202       basic_block bb = bbs[i];
5203       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
5204         {
5205           stmt = bsi_stmt (si);
5206
5207           if (vect_debug_details (NULL))
5208             {
5209               fprintf (dump_file, "init: stmt relevant? ");
5210               print_generic_expr (dump_file, stmt, TDF_SLIM);
5211             } 
5212
5213           stmt_info = vinfo_for_stmt (stmt);
5214           STMT_VINFO_RELEVANT_P (stmt_info) = 0;
5215
5216           if (vect_stmt_relevant_p (stmt, loop_vinfo))
5217             vect_mark_relevant (worklist, stmt);
5218         }
5219     }
5220
5221
5222   /* 2. Process_worklist */
5223
5224   while (VARRAY_ACTIVE_SIZE (worklist) > 0)
5225     {
5226       stmt = VARRAY_TOP_TREE (worklist);
5227       VARRAY_POP (worklist);
5228
5229       if (vect_debug_details (NULL))
5230         {
5231           fprintf (dump_file, "worklist: examine stmt: ");
5232           print_generic_expr (dump_file, stmt, TDF_SLIM);
5233         }
5234
5235       /* Examine the USES in this statement. Mark all the statements which
5236          feed this statement's uses as "relevant", unless the USE is used as
5237          an array index.  */
5238
5239       if (TREE_CODE (stmt) == PHI_NODE)
5240         {
5241           /* follow the def-use chain inside the loop.  */
5242           for (j = 0; j < PHI_NUM_ARGS (stmt); j++)
5243             {
5244               tree arg = PHI_ARG_DEF (stmt, j);
5245               tree def_stmt = NULL_TREE;
5246               basic_block bb;
5247               if (!vect_is_simple_use (arg, loop, &def_stmt))
5248                 {
5249                   if (vect_debug_details (NULL))        
5250                     fprintf (dump_file, "worklist: unsupported use.");
5251                   varray_clear (worklist);
5252                   return false;
5253                 }
5254               if (!def_stmt)
5255                 continue;
5256
5257               if (vect_debug_details (NULL))
5258                 {
5259                   fprintf (dump_file, "worklist: def_stmt: ");
5260                   print_generic_expr (dump_file, def_stmt, TDF_SLIM);
5261                 }
5262
5263               bb = bb_for_stmt (def_stmt);
5264               if (flow_bb_inside_loop_p (loop, bb))
5265                 vect_mark_relevant (worklist, def_stmt);
5266             }
5267         } 
5268
5269       ann = stmt_ann (stmt);
5270       use_ops = USE_OPS (ann);
5271
5272       for (i = 0; i < NUM_USES (use_ops); i++)
5273         {
5274           tree use = USE_OP (use_ops, i);
5275
5276           /* We are only interested in uses that need to be vectorized. Uses 
5277              that are used for address computation are not considered relevant.
5278            */
5279           if (exist_non_indexing_operands_for_use_p (use, stmt))
5280             {
5281               tree def_stmt = NULL_TREE;
5282               basic_block bb;
5283               if (!vect_is_simple_use (use, loop, &def_stmt))
5284                 {
5285                   if (vect_debug_details (NULL))        
5286                     fprintf (dump_file, "worklist: unsupported use.");
5287                   varray_clear (worklist);
5288                   return false;
5289                 }
5290
5291               if (!def_stmt)
5292                 continue;
5293
5294               if (vect_debug_details (NULL))
5295                 {
5296                   fprintf (dump_file, "worklist: examine use %d: ", i);
5297                   print_generic_expr (dump_file, use, TDF_SLIM);
5298                 }
5299
5300               bb = bb_for_stmt (def_stmt);
5301               if (flow_bb_inside_loop_p (loop, bb))
5302                 vect_mark_relevant (worklist, def_stmt);
5303             }
5304         }
5305     }                           /* while worklist */
5306
5307   varray_clear (worklist);
5308   return true;
5309 }
5310
5311
5312 /* Function vect_analyze_loop_with_symbolic_num_of_iters.
5313
5314    In case the number of iterations that LOOP iterates in unknown at compile 
5315    time, an epilog loop will be generated, and the loop induction variables 
5316    (IVs) will be "advanced" to the value they are supposed to take just before 
5317    the epilog loop. Here we check that the access function of the loop IVs
5318    and the expression that represents the loop bound are simple enough.
5319    These restrictions will be relaxed in the future.  */
5320
5321 static bool 
5322 vect_analyze_loop_with_symbolic_num_of_iters (tree niters, 
5323                                               struct loop *loop)
5324 {
5325   basic_block bb = loop->header;
5326   tree phi;
5327
5328   if (vect_debug_details (NULL))
5329     fprintf (dump_file, 
5330              "\n<<vect_analyze_loop_with_symbolic_num_of_iters>>\n");
5331   
5332   if (chrec_contains_undetermined (niters))
5333     {
5334       if (vect_debug_details (NULL))
5335         fprintf (dump_file, "Infinite number of iterations.");
5336       return false;
5337     }
5338
5339   if (!niters)
5340     {
5341       if (vect_debug_details (NULL))
5342         fprintf (dump_file, "niters is NULL pointer.");
5343       return false;
5344     }
5345
5346   if (vect_debug_details (NULL))
5347     {
5348       fprintf (dump_file, "Symbolic number of iterations is ");
5349       print_generic_expr (dump_file, niters, TDF_DETAILS);
5350     }
5351    
5352   /* Analyze phi functions of the loop header.  */
5353
5354   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5355     {
5356       tree access_fn = NULL;
5357       tree evolution_part;
5358
5359       if (vect_debug_details (NULL))
5360         {
5361           fprintf (dump_file, "Analyze phi: ");
5362           print_generic_expr (dump_file, phi, TDF_SLIM);
5363         }
5364
5365       /* Skip virtual phi's. The data dependences that are associated with
5366          virtual defs/uses (i.e., memory accesses) are analyzed elsewhere.  */
5367
5368       if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
5369         {
5370           if (vect_debug_details (NULL))
5371             fprintf (dump_file, "virtual phi. skip.");
5372           continue;
5373         }
5374
5375       /* Analyze the evolution function.  */
5376
5377       access_fn = instantiate_parameters
5378         (loop, analyze_scalar_evolution (loop, PHI_RESULT (phi)));
5379
5380       if (!access_fn)
5381         {
5382           if (vect_debug_details (NULL))
5383             fprintf (dump_file, "No Access function.");
5384           return false;
5385         }
5386
5387       if (vect_debug_details (NULL))
5388         {
5389           fprintf (dump_file, "Access function of PHI: ");
5390           print_generic_expr (dump_file, access_fn, TDF_SLIM);
5391         }
5392
5393       evolution_part = evolution_part_in_loop_num (access_fn, loop->num);
5394       
5395       if (evolution_part == NULL_TREE)
5396         return false;
5397   
5398       /* FORNOW: We do not transform initial conditions of IVs 
5399          which evolution functions are a polynomial of degree >= 2.  */
5400
5401       if (tree_is_chrec (evolution_part))
5402         return false;  
5403     }
5404
5405   return  true;
5406 }
5407
5408
5409 /* Function vect_get_loop_niters.
5410
5411    Determine how many iterations the loop is executed.  */
5412
5413 static tree
5414 vect_get_loop_niters (struct loop *loop, tree *number_of_iterations)
5415 {
5416   tree niters;
5417
5418   if (vect_debug_details (NULL))
5419     fprintf (dump_file, "\n<<get_loop_niters>>\n");
5420
5421   niters = number_of_iterations_in_loop (loop);
5422
5423   if (niters != NULL_TREE
5424       && niters != chrec_dont_know)
5425     {
5426       *number_of_iterations = niters;
5427
5428       if (vect_debug_details (NULL))
5429         {
5430           fprintf (dump_file, "==> get_loop_niters:" );
5431           print_generic_expr (dump_file, *number_of_iterations, TDF_SLIM);
5432         }
5433     }
5434
5435   return get_loop_exit_condition (loop);
5436 }
5437
5438
5439 /* Function vect_analyze_loop_form.
5440
5441    Verify the following restrictions (some may be relaxed in the future):
5442    - it's an inner-most loop
5443    - number of BBs = 2 (which are the loop header and the latch)
5444    - the loop has a pre-header
5445    - the loop has a single entry and exit
5446    - the loop exit condition is simple enough, and the number of iterations
5447      can be analyzed (a countable loop).  */
5448
5449 static loop_vec_info
5450 vect_analyze_loop_form (struct loop *loop)
5451 {
5452   loop_vec_info loop_vinfo;
5453   tree loop_cond;
5454   tree number_of_iterations = NULL;
5455
5456   if (vect_debug_details (loop))
5457     fprintf (dump_file, "\n<<vect_analyze_loop_form>>\n");
5458
5459   if (loop->inner
5460       || !loop->single_exit
5461       || loop->num_nodes != 2)
5462     {
5463       if (vect_debug_stats (loop) || vect_debug_details (loop)) 
5464         {
5465           fprintf (dump_file, "not vectorized: bad loop form. ");
5466           if (loop->inner)
5467             fprintf (dump_file, "nested loop.");
5468           else if (!loop->single_exit)
5469             fprintf (dump_file, "multiple exits.");
5470           else if (loop->num_nodes != 2)
5471             fprintf (dump_file, "too many BBs in loop.");
5472         }
5473
5474       return NULL;
5475     }
5476
5477   /* We assume that the loop exit condition is at the end of the loop. i.e,
5478      that the loop is represented as a do-while (with a proper if-guard
5479      before the loop if needed), where the loop header contains all the
5480      executable statements, and the latch is empty.  */
5481   if (!empty_block_p (loop->latch))
5482     {
5483       if (vect_debug_stats (loop) || vect_debug_details (loop))
5484         fprintf (dump_file, "not vectorized: unexpectd loop form.");
5485       return NULL;
5486     }
5487
5488   if (empty_block_p (loop->header))
5489     {
5490       if (vect_debug_stats (loop) || vect_debug_details (loop))
5491         fprintf (dump_file, "not vectorized: empty loop.");
5492       return NULL;
5493     }
5494
5495   loop_cond = vect_get_loop_niters (loop, &number_of_iterations);
5496   if (!loop_cond)
5497     {
5498       if (vect_debug_stats (loop) || vect_debug_details (loop))
5499         fprintf (dump_file, "not vectorized: complicated exit condition.");
5500       return NULL;
5501     }
5502   
5503   if (!number_of_iterations) 
5504     {
5505       if (vect_debug_stats (loop) || vect_debug_details (loop))
5506         fprintf (dump_file, 
5507                  "not vectorized: number of iterations cannot be computed.");
5508       return NULL;
5509     }
5510
5511   loop_vinfo = new_loop_vec_info (loop);
5512   LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
5513   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5514     {
5515       if (vect_debug_stats (loop) || vect_debug_details (loop))
5516         fprintf (dump_file, "loop bound unknown.");
5517
5518       /* Unknown loop bound.  */
5519       if (!vect_analyze_loop_with_symbolic_num_of_iters 
5520                                         (number_of_iterations, loop))
5521         {
5522           if (vect_debug_stats (loop) || vect_debug_details (loop))
5523             fprintf (dump_file, 
5524                      "not vectorized: can't determine loop bound.");
5525           return NULL;
5526         }
5527       else
5528         {
5529           /* We need only one loop entry for unknown loop bound support.  */
5530           if (loop->num_entries != 1 || !loop->pre_header)
5531             {         
5532               if (vect_debug_stats (loop) || vect_debug_details (loop))
5533                 fprintf (dump_file, 
5534                          "not vectorized: more than one loop entry.");
5535               return NULL;
5536             }
5537         }
5538     }
5539   else
5540   if (LOOP_VINFO_INT_NITERS (loop_vinfo) == 0)
5541     {
5542       if (vect_debug_stats (loop) || vect_debug_details (loop))
5543         fprintf (dump_file, "not vectorized: number of iterations = 0.");
5544       return NULL;
5545     }
5546
5547   LOOP_VINFO_EXIT_COND (loop_vinfo) = loop_cond;
5548
5549   return loop_vinfo;
5550 }
5551
5552
5553 /* Function vect_analyze_loop.
5554
5555    Apply a set of analyses on LOOP, and create a loop_vec_info struct
5556    for it. The different analyses will record information in the
5557    loop_vec_info struct.  */
5558
5559 static loop_vec_info
5560 vect_analyze_loop (struct loop *loop)
5561 {
5562   bool ok;
5563   loop_vec_info loop_vinfo;
5564
5565   if (vect_debug_details (NULL))
5566     fprintf (dump_file, "\n<<<<<<< analyze_loop_nest >>>>>>>\n");
5567
5568   /* Check the CFG characteristics of the loop (nesting, entry/exit, etc.  */
5569
5570   loop_vinfo = vect_analyze_loop_form (loop);
5571   if (!loop_vinfo)
5572     {
5573       if (vect_debug_details (loop))
5574         fprintf (dump_file, "bad loop form.");
5575       return NULL;
5576     }
5577
5578   /* Find all data references in the loop (which correspond to vdefs/vuses)
5579      and analyze their evolution in the loop.
5580
5581      FORNOW: Handle only simple, array references, which
5582      alignment can be forced, and aligned pointer-references.  */
5583
5584   ok = vect_analyze_data_refs (loop_vinfo);
5585   if (!ok)
5586     {
5587       if (vect_debug_details (loop))
5588         fprintf (dump_file, "bad data references.");
5589       destroy_loop_vec_info (loop_vinfo);
5590       return NULL;
5591     }
5592
5593   /* Data-flow analysis to detect stmts that do not need to be vectorized.  */
5594
5595   ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
5596   if (!ok)
5597     {
5598       if (vect_debug_details (loop))
5599         fprintf (dump_file, "unexpected pattern.");
5600       if (vect_debug_details (loop))
5601         fprintf (dump_file, "not vectorized: unexpected pattern.");
5602       destroy_loop_vec_info (loop_vinfo);
5603       return NULL;
5604     }
5605
5606   /* Check that all cross-iteration scalar data-flow cycles are OK.
5607      Cross-iteration cycles caused by virtual phis are analyzed separately.  */
5608
5609   ok = vect_analyze_scalar_cycles (loop_vinfo);
5610   if (!ok)
5611     {
5612       if (vect_debug_details (loop))
5613         fprintf (dump_file, "bad scalar cycle.");
5614       destroy_loop_vec_info (loop_vinfo);
5615       return NULL;
5616     }
5617
5618   /* Analyze data dependences between the data-refs in the loop. 
5619      FORNOW: fail at the first data dependence that we encounter.  */
5620
5621   ok = vect_analyze_data_ref_dependences (loop_vinfo);
5622   if (!ok)
5623     {
5624       if (vect_debug_details (loop))
5625         fprintf (dump_file, "bad data dependence.");
5626       destroy_loop_vec_info (loop_vinfo);
5627       return NULL;
5628     }
5629
5630   /* Analyze the access patterns of the data-refs in the loop (consecutive,
5631      complex, etc.). FORNOW: Only handle consecutive access pattern.  */
5632
5633   ok = vect_analyze_data_ref_accesses (loop_vinfo);
5634   if (!ok)
5635     {
5636       if (vect_debug_details (loop))
5637         fprintf (dump_file, "bad data access.");
5638       destroy_loop_vec_info (loop_vinfo);
5639       return NULL;
5640     }
5641
5642   /* Analyze the alignment of the data-refs in the loop.
5643      FORNOW: Only aligned accesses are handled.  */
5644
5645   ok = vect_analyze_data_refs_alignment (loop_vinfo);
5646   if (!ok)
5647     {
5648       if (vect_debug_details (loop))
5649         fprintf (dump_file, "bad data alignment.");
5650       destroy_loop_vec_info (loop_vinfo);
5651       return NULL;
5652     }
5653
5654   /* Scan all the operations in the loop and make sure they are
5655      vectorizable.  */
5656
5657   ok = vect_analyze_operations (loop_vinfo);
5658   if (!ok)
5659     {
5660       if (vect_debug_details (loop))
5661         fprintf (dump_file, "bad operation or unsupported loop bound.");
5662       destroy_loop_vec_info (loop_vinfo);
5663       return NULL;
5664     }
5665
5666   LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;
5667
5668   return loop_vinfo;
5669 }
5670
5671
5672 /* Function need_imm_uses_for.
5673
5674    Return whether we ought to include information for 'var'
5675    when calculating immediate uses.  For this pass we only want use
5676    information for non-virtual variables.  */
5677
5678 static bool
5679 need_imm_uses_for (tree var)
5680 {
5681   return is_gimple_reg (var);
5682 }
5683
5684
5685 /* Function vectorize_loops.
5686    
5687    Entry Point to loop vectorization phase.  */
5688
5689 void
5690 vectorize_loops (struct loops *loops)
5691 {
5692   unsigned int i, loops_num;
5693   unsigned int num_vectorized_loops = 0;
5694
5695   /* Does the target support SIMD?  */
5696   /* FORNOW: until more sophisticated machine modelling is in place.  */
5697   if (!UNITS_PER_SIMD_WORD)
5698     {
5699       if (vect_debug_details (NULL))
5700         fprintf (dump_file, "vectorizer: target vector size is not defined.");
5701       return;
5702     }
5703
5704   compute_immediate_uses (TDFA_USE_OPS, need_imm_uses_for);
5705
5706   /*  ----------- Analyze loops. -----------  */
5707
5708   /* If some loop was duplicated, it gets bigger number 
5709      than all previously defined loops. This fact allows us to run 
5710      only over initial loops skipping newly generated ones.  */
5711   loops_num = loops->num;
5712   for (i = 1; i < loops_num; i++)
5713     {
5714       loop_vec_info loop_vinfo;
5715       struct loop *loop = loops->parray[i];
5716
5717       if (!loop)
5718         continue;
5719
5720       loop_vinfo = vect_analyze_loop (loop);
5721       loop->aux = loop_vinfo;
5722
5723       if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
5724         continue;
5725
5726       vect_transform_loop (loop_vinfo, loops); 
5727       num_vectorized_loops++;
5728     }
5729
5730   if (vect_debug_stats (NULL) || vect_debug_details (NULL))
5731     fprintf (dump_file, "\nvectorized %u loops in function.\n",
5732              num_vectorized_loops);
5733
5734   /*  ----------- Finalize. -----------  */
5735
5736   free_df ();
5737   for (i = 1; i < loops_num; i++)
5738     {
5739       struct loop *loop = loops->parray[i];
5740       loop_vec_info loop_vinfo;
5741
5742       if (!loop)
5743         continue;
5744       loop_vinfo = loop->aux;
5745       destroy_loop_vec_info (loop_vinfo);
5746       loop->aux = NULL;
5747     }
5748
5749   rewrite_into_ssa (false);
5750   if (!bitmap_empty_p (vars_to_rename))
5751     {
5752       /* The rewrite of ssa names may cause violation of loop closed ssa
5753          form invariants.  TODO -- avoid these rewrites completely.
5754          Information in virtual phi nodes is sufficient for it.  */
5755       rewrite_into_loop_closed_ssa (); 
5756     }
5757   rewrite_into_loop_closed_ssa (); 
5758   bitmap_clear (vars_to_rename);
5759 }