tree-ssa.h: New.
[platform/upstream/gcc.git] / gcc / tree-vectorizer.c
1 /* Vectorizer
2    Copyright (C) 2003-2013 Free Software Foundation, Inc.
3    Contributed by Dorit Naishlos <dorit@il.ibm.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* Loop and basic block vectorizer.
22
23   This file contains drivers for the three vectorizers:
24   (1) loop vectorizer (inter-iteration parallelism),
25   (2) loop-aware SLP (intra-iteration parallelism) (invoked by the loop
26       vectorizer)
27   (3) BB vectorizer (out-of-loops), aka SLP
28
29   The rest of the vectorizer's code is organized as follows:
30   - tree-vect-loop.c - loop specific parts such as reductions, etc. These are
31     used by drivers (1) and (2).
32   - tree-vect-loop-manip.c - vectorizer's loop control-flow utilities, used by
33     drivers (1) and (2).
34   - tree-vect-slp.c - BB vectorization specific analysis and transformation,
35     used by drivers (2) and (3).
36   - tree-vect-stmts.c - statements analysis and transformation (used by all).
37   - tree-vect-data-refs.c - vectorizer specific data-refs analysis and
38     manipulations (used by all).
39   - tree-vect-patterns.c - vectorizable code patterns detector (used by all)
40
41   Here's a poor attempt at illustrating that:
42
43      tree-vectorizer.c:
44      loop_vect()  loop_aware_slp()  slp_vect()
45           |        /           \          /
46           |       /             \        /
47           tree-vect-loop.c  tree-vect-slp.c
48                 | \      \  /      /   |
49                 |  \      \/      /    |
50                 |   \     /\     /     |
51                 |    \   /  \   /      |
52          tree-vect-stmts.c  tree-vect-data-refs.c
53                        \      /
54                     tree-vect-patterns.c
55 */
56
57 #include "config.h"
58 #include "system.h"
59 #include "coretypes.h"
60 #include "dumpfile.h"
61 #include "tm.h"
62 #include "ggc.h"
63 #include "tree.h"
64 #include "tree-pretty-print.h"
65 #include "tree-ssa.h"
66 #include "cfgloop.h"
67 #include "tree-vectorizer.h"
68 #include "tree-pass.h"
69 #include "hash-table.h"
70 #include "tree-ssa-propagate.h"
71 #include "dbgcnt.h"
72
73 /* Loop or bb location.  */
74 LOC vect_location;
75
76 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
77 vec<vec_void_p> stmt_vec_info_vec;
78 \f
79 /* For mapping simduid to vectorization factor.  */
80
81 struct simduid_to_vf : typed_free_remove<simduid_to_vf>
82 {
83   unsigned int simduid;
84   int vf;
85
86   /* hash_table support.  */
87   typedef simduid_to_vf value_type;
88   typedef simduid_to_vf compare_type;
89   static inline hashval_t hash (const value_type *);
90   static inline int equal (const value_type *, const compare_type *);
91 };
92
93 inline hashval_t
94 simduid_to_vf::hash (const value_type *p)
95 {
96   return p->simduid;
97 }
98
99 inline int
100 simduid_to_vf::equal (const value_type *p1, const value_type *p2)
101 {
102   return p1->simduid == p2->simduid;
103 }
104
105 /* This hash maps the OMP simd array to the corresponding simduid used
106    to index into it.  Like thus,
107
108         _7 = GOMP_SIMD_LANE (simduid.0)
109         ...
110         ...
111         D.1737[_7] = stuff;
112
113
114    This hash maps from the simduid.0 to OMP simd array (D.1737[]).  */
115
116 struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
117 {
118   tree decl;
119   unsigned int simduid;
120
121   /* hash_table support.  */
122   typedef simd_array_to_simduid value_type;
123   typedef simd_array_to_simduid compare_type;
124   static inline hashval_t hash (const value_type *);
125   static inline int equal (const value_type *, const compare_type *);
126 };
127
128 inline hashval_t
129 simd_array_to_simduid::hash (const value_type *p)
130 {
131   return DECL_UID (p->decl);
132 }
133
134 inline int
135 simd_array_to_simduid::equal (const value_type *p1, const value_type *p2)
136 {
137   return p1->decl == p2->decl;
138 }
139
140 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
141    into their corresponding constants.  */
142
143 static void
144 adjust_simduid_builtins (hash_table <simduid_to_vf> &htab)
145 {
146   basic_block bb;
147
148   FOR_EACH_BB (bb)
149     {
150       gimple_stmt_iterator i;
151
152       for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
153         {
154           unsigned int vf = 1;
155           enum internal_fn ifn;
156           gimple stmt = gsi_stmt (i);
157           tree t;
158           if (!is_gimple_call (stmt)
159               || !gimple_call_internal_p (stmt))
160             continue;
161           ifn = gimple_call_internal_fn (stmt);
162           switch (ifn)
163             {
164             case IFN_GOMP_SIMD_LANE:
165             case IFN_GOMP_SIMD_VF:
166             case IFN_GOMP_SIMD_LAST_LANE:
167               break;
168             default:
169               continue;
170             }
171           tree arg = gimple_call_arg (stmt, 0);
172           gcc_assert (arg != NULL_TREE);
173           gcc_assert (TREE_CODE (arg) == SSA_NAME);
174           simduid_to_vf *p = NULL, data;
175           data.simduid = DECL_UID (SSA_NAME_VAR (arg));
176           if (htab.is_created ())
177             p = htab.find (&data);
178           if (p)
179             vf = p->vf;
180           switch (ifn)
181             {
182             case IFN_GOMP_SIMD_VF:
183               t = build_int_cst (unsigned_type_node, vf);
184               break;
185             case IFN_GOMP_SIMD_LANE:
186               t = build_int_cst (unsigned_type_node, 0);
187               break;
188             case IFN_GOMP_SIMD_LAST_LANE:
189               t = gimple_call_arg (stmt, 1);
190               break;
191             default:
192               gcc_unreachable ();
193             }
194           update_call_from_tree (&i, t);
195         }
196     }
197 }
198
199 /* Helper structure for note_simd_array_uses.  */
200
201 struct note_simd_array_uses_struct
202 {
203   hash_table <simd_array_to_simduid> *htab;
204   unsigned int simduid;
205 };
206
207 /* Callback for note_simd_array_uses, called through walk_gimple_op.  */
208
209 static tree
210 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
211 {
212   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
213   struct note_simd_array_uses_struct *ns
214     = (struct note_simd_array_uses_struct *) wi->info;
215
216   if (TYPE_P (*tp))
217     *walk_subtrees = 0;
218   else if (VAR_P (*tp)
219            && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
220            && DECL_CONTEXT (*tp) == current_function_decl)
221     {
222       simd_array_to_simduid data;
223       if (!ns->htab->is_created ())
224         ns->htab->create (15);
225       data.decl = *tp;
226       data.simduid = ns->simduid;
227       simd_array_to_simduid **slot = ns->htab->find_slot (&data, INSERT);
228       if (*slot == NULL)
229         {
230           simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
231           *p = data;
232           *slot = p;
233         }
234       else if ((*slot)->simduid != ns->simduid)
235         (*slot)->simduid = -1U;
236       *walk_subtrees = 0;
237     }
238   return NULL_TREE;
239 }
240
241 /* Find "omp simd array" temporaries and map them to corresponding
242    simduid.  */
243
244 static void
245 note_simd_array_uses (hash_table <simd_array_to_simduid> *htab)
246 {
247   basic_block bb;
248   gimple_stmt_iterator gsi;
249   struct walk_stmt_info wi;
250   struct note_simd_array_uses_struct ns;
251
252   memset (&wi, 0, sizeof (wi));
253   wi.info = &ns;
254   ns.htab = htab;
255
256   FOR_EACH_BB (bb)
257     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
258       {
259         gimple stmt = gsi_stmt (gsi);
260         if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
261           continue;
262         switch (gimple_call_internal_fn (stmt))
263           {
264           case IFN_GOMP_SIMD_LANE:
265           case IFN_GOMP_SIMD_VF:
266           case IFN_GOMP_SIMD_LAST_LANE:
267             break;
268           default:
269             continue;
270           }
271         tree lhs = gimple_call_lhs (stmt);
272         if (lhs == NULL_TREE)
273           continue;
274         imm_use_iterator use_iter;
275         gimple use_stmt;
276         ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
277         FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
278           if (!is_gimple_debug (use_stmt))
279             walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
280       }
281 }
282 \f
283 /* A helper function to free data refs.  */
284
285 void
286 vect_destroy_datarefs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
287 {
288   vec<data_reference_p> datarefs;
289   struct data_reference *dr;
290   unsigned int i;
291
292  if (loop_vinfo)
293     datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
294   else
295     datarefs = BB_VINFO_DATAREFS (bb_vinfo);
296
297   FOR_EACH_VEC_ELT (datarefs, i, dr)
298     if (dr->aux)
299       {
300         free (dr->aux);
301         dr->aux = NULL;
302       }
303
304   free_data_refs (datarefs);
305 }
306
307
308 /* Function vectorize_loops.
309
310    Entry point to loop vectorization phase.  */
311
312 unsigned
313 vectorize_loops (void)
314 {
315   unsigned int i;
316   unsigned int num_vectorized_loops = 0;
317   unsigned int vect_loops_num;
318   loop_iterator li;
319   struct loop *loop;
320   hash_table <simduid_to_vf> simduid_to_vf_htab;
321   hash_table <simd_array_to_simduid> simd_array_to_simduid_htab;
322
323   vect_loops_num = number_of_loops (cfun);
324
325   /* Bail out if there are no loops.  */
326   if (vect_loops_num <= 1)
327     {
328       if (cfun->has_simduid_loops)
329         adjust_simduid_builtins (simduid_to_vf_htab);
330       return 0;
331     }
332
333   if (cfun->has_simduid_loops)
334     note_simd_array_uses (&simd_array_to_simduid_htab);
335
336   init_stmt_vec_info_vec ();
337
338   /*  ----------- Analyze loops. -----------  */
339
340   /* If some loop was duplicated, it gets bigger number
341      than all previously defined loops.  This fact allows us to run
342      only over initial loops skipping newly generated ones.  */
343   FOR_EACH_LOOP (li, loop, 0)
344     if ((flag_tree_vectorize && optimize_loop_nest_for_speed_p (loop))
345         || loop->force_vect)
346       {
347         loop_vec_info loop_vinfo;
348         vect_location = find_loop_location (loop);
349         if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
350             && dump_enabled_p ())
351           dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
352                        LOC_FILE (vect_location), LOC_LINE (vect_location));
353
354         loop_vinfo = vect_analyze_loop (loop);
355         loop->aux = loop_vinfo;
356
357         if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
358           continue;
359
360         if (!dbg_cnt (vect_loop))
361           break;
362
363         if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
364             && dump_enabled_p ())
365           dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
366                            "loop vectorized\n");
367         vect_transform_loop (loop_vinfo);
368         num_vectorized_loops++;
369         /* Now that the loop has been vectorized, allow it to be unrolled
370            etc.  */
371         loop->force_vect = false;
372
373         if (loop->simduid)
374           {
375             simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
376             if (!simduid_to_vf_htab.is_created ())
377               simduid_to_vf_htab.create (15);
378             simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
379             simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
380             *simduid_to_vf_htab.find_slot (simduid_to_vf_data, INSERT)
381               = simduid_to_vf_data;
382           }
383       }
384
385   vect_location = UNKNOWN_LOC;
386
387   statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
388   if (dump_enabled_p ()
389       || (num_vectorized_loops > 0 && dump_enabled_p ()))
390     dump_printf_loc (MSG_NOTE, vect_location,
391                      "vectorized %u loops in function.\n",
392                      num_vectorized_loops);
393
394   /*  ----------- Finalize. -----------  */
395
396   for (i = 1; i < vect_loops_num; i++)
397     {
398       loop_vec_info loop_vinfo;
399
400       loop = get_loop (cfun, i);
401       if (!loop)
402         continue;
403       loop_vinfo = (loop_vec_info) loop->aux;
404       destroy_loop_vec_info (loop_vinfo, true);
405       loop->aux = NULL;
406     }
407
408   free_stmt_vec_info_vec ();
409
410   /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins.  */
411   if (cfun->has_simduid_loops)
412     adjust_simduid_builtins (simduid_to_vf_htab);
413
414   /* Shrink any "omp array simd" temporary arrays to the
415      actual vectorization factors.  */
416   if (simd_array_to_simduid_htab.is_created ())
417     {
418       for (hash_table <simd_array_to_simduid>::iterator iter
419            = simd_array_to_simduid_htab.begin ();
420            iter != simd_array_to_simduid_htab.end (); ++iter)
421         if ((*iter).simduid != -1U)
422           {
423             tree decl = (*iter).decl;
424             int vf = 1;
425             if (simduid_to_vf_htab.is_created ())
426               {
427                 simduid_to_vf *p = NULL, data;
428                 data.simduid = (*iter).simduid;
429                 p = simduid_to_vf_htab.find (&data);
430                 if (p)
431                   vf = p->vf;
432               }
433             tree atype
434               = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
435             TREE_TYPE (decl) = atype;
436             relayout_decl (decl);
437           }
438
439       simd_array_to_simduid_htab.dispose ();
440     }
441   if (simduid_to_vf_htab.is_created ())
442     simduid_to_vf_htab.dispose ();
443
444   if (num_vectorized_loops > 0)
445     {
446       /* If we vectorized any loop only virtual SSA form needs to be updated.
447          ???  Also while we try hard to update loop-closed SSA form we fail
448          to properly do this in some corner-cases (see PR56286).  */
449       rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
450       return TODO_cleanup_cfg;
451     }
452
453   return 0;
454 }
455
456
457 /*  Entry point to basic block SLP phase.  */
458
459 static unsigned int
460 execute_vect_slp (void)
461 {
462   basic_block bb;
463
464   init_stmt_vec_info_vec ();
465
466   FOR_EACH_BB (bb)
467     {
468       vect_location = find_bb_location (bb);
469
470       if (vect_slp_analyze_bb (bb))
471         {
472           if (!dbg_cnt (vect_slp))
473             break;
474
475           vect_slp_transform_bb (bb);
476           if (dump_enabled_p ())
477             dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
478                              "basic block vectorized\n");
479         }
480     }
481
482   free_stmt_vec_info_vec ();
483   return 0;
484 }
485
486 static bool
487 gate_vect_slp (void)
488 {
489   /* Apply SLP either if the vectorizer is on and the user didn't specify
490      whether to run SLP or not, or if the SLP flag was set by the user.  */
491   return ((flag_tree_vectorize != 0 && flag_tree_slp_vectorize != 0)
492           || flag_tree_slp_vectorize == 1);
493 }
494
495 namespace {
496
497 const pass_data pass_data_slp_vectorize =
498 {
499   GIMPLE_PASS, /* type */
500   "slp", /* name */
501   OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
502   true, /* has_gate */
503   true, /* has_execute */
504   TV_TREE_SLP_VECTORIZATION, /* tv_id */
505   ( PROP_ssa | PROP_cfg ), /* properties_required */
506   0, /* properties_provided */
507   0, /* properties_destroyed */
508   0, /* todo_flags_start */
509   ( TODO_verify_ssa | TODO_update_ssa
510     | TODO_verify_stmts ), /* todo_flags_finish */
511 };
512
513 class pass_slp_vectorize : public gimple_opt_pass
514 {
515 public:
516   pass_slp_vectorize(gcc::context *ctxt)
517     : gimple_opt_pass(pass_data_slp_vectorize, ctxt)
518   {}
519
520   /* opt_pass methods: */
521   bool gate () { return gate_vect_slp (); }
522   unsigned int execute () { return execute_vect_slp (); }
523
524 }; // class pass_slp_vectorize
525
526 } // anon namespace
527
528 gimple_opt_pass *
529 make_pass_slp_vectorize (gcc::context *ctxt)
530 {
531   return new pass_slp_vectorize (ctxt);
532 }
533
534
535 /* Increase alignment of global arrays to improve vectorization potential.
536    TODO:
537    - Consider also structs that have an array field.
538    - Use ipa analysis to prune arrays that can't be vectorized?
539      This should involve global alignment analysis and in the future also
540      array padding.  */
541
542 static unsigned int
543 increase_alignment (void)
544 {
545   struct varpool_node *vnode;
546
547   vect_location = UNKNOWN_LOC;
548
549   /* Increase the alignment of all global arrays for vectorization.  */
550   FOR_EACH_DEFINED_VARIABLE (vnode)
551     {
552       tree vectype, decl = vnode->symbol.decl;
553       tree t;
554       unsigned int alignment;
555
556       t = TREE_TYPE(decl);
557       if (TREE_CODE (t) != ARRAY_TYPE)
558         continue;
559       vectype = get_vectype_for_scalar_type (strip_array_types (t));
560       if (!vectype)
561         continue;
562       alignment = TYPE_ALIGN (vectype);
563       if (DECL_ALIGN (decl) >= alignment)
564         continue;
565
566       if (vect_can_force_dr_alignment_p (decl, alignment))
567         {
568           DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
569           DECL_USER_ALIGN (decl) = 1;
570           dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
571           dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
572           dump_printf (MSG_NOTE, "\n");
573         }
574     }
575   return 0;
576 }
577
578
579 static bool
580 gate_increase_alignment (void)
581 {
582   return flag_section_anchors && flag_tree_vectorize;
583 }
584
585
586 namespace {
587
588 const pass_data pass_data_ipa_increase_alignment =
589 {
590   SIMPLE_IPA_PASS, /* type */
591   "increase_alignment", /* name */
592   OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
593   true, /* has_gate */
594   true, /* has_execute */
595   TV_IPA_OPT, /* tv_id */
596   0, /* properties_required */
597   0, /* properties_provided */
598   0, /* properties_destroyed */
599   0, /* todo_flags_start */
600   0, /* todo_flags_finish */
601 };
602
603 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
604 {
605 public:
606   pass_ipa_increase_alignment(gcc::context *ctxt)
607     : simple_ipa_opt_pass(pass_data_ipa_increase_alignment, ctxt)
608   {}
609
610   /* opt_pass methods: */
611   bool gate () { return gate_increase_alignment (); }
612   unsigned int execute () { return increase_alignment (); }
613
614 }; // class pass_ipa_increase_alignment
615
616 } // anon namespace
617
618 simple_ipa_opt_pass *
619 make_pass_ipa_increase_alignment (gcc::context *ctxt)
620 {
621   return new pass_ipa_increase_alignment (ctxt);
622 }