1 /* Vectorizer
2    Copyright (C) 2003-2014 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 "tree.h"
63 #include "stor-layout.h"
64 #include "tree-pretty-print.h"
65 #include "basic-block.h"
66 #include "tree-ssa-alias.h"
67 #include "internal-fn.h"
68 #include "gimple-expr.h"
69 #include "is-a.h"
70 #include "gimple.h"
71 #include "gimple-iterator.h"
72 #include "gimple-walk.h"
73 #include "gimple-ssa.h"
74 #include "cgraph.h"
75 #include "tree-phinodes.h"
76 #include "ssa-iterators.h"
77 #include "tree-ssa-loop-manip.h"
78 #include "tree-cfg.h"
79 #include "cfgloop.h"
80 #include "tree-vectorizer.h"
81 #include "tree-pass.h"
82 #include "tree-ssa-propagate.h"
83 #include "dbgcnt.h"
84 #include "gimple-fold.h"
85 
86 /* Loop or bb location.  */
87 source_location vect_location;
88 
89 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
90 vec<vec_void_p> stmt_vec_info_vec;
91 
92 /* For mapping simduid to vectorization factor.  */
93 
94 struct simduid_to_vf : typed_free_remove<simduid_to_vf>
95 {
96   unsigned int simduid;
97   int vf;
98 
99   /* hash_table support.  */
100   typedef simduid_to_vf value_type;
101   typedef simduid_to_vf compare_type;
102   static inline hashval_t hash (const value_type *);
103   static inline int equal (const value_type *, const compare_type *);
104 };
105 
106 inline hashval_t
hash(const value_type * p)107 simduid_to_vf::hash (const value_type *p)
108 {
109   return p->simduid;
110 }
111 
112 inline int
equal(const value_type * p1,const value_type * p2)113 simduid_to_vf::equal (const value_type *p1, const value_type *p2)
114 {
115   return p1->simduid == p2->simduid;
116 }
117 
118 /* This hash maps the OMP simd array to the corresponding simduid used
119    to index into it.  Like thus,
120 
121         _7 = GOMP_SIMD_LANE (simduid.0)
122         ...
123         ...
124         D.1737[_7] = stuff;
125 
126 
127    This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
128    simduid.0.  */
129 
130 struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
131 {
132   tree decl;
133   unsigned int simduid;
134 
135   /* hash_table support.  */
136   typedef simd_array_to_simduid value_type;
137   typedef simd_array_to_simduid compare_type;
138   static inline hashval_t hash (const value_type *);
139   static inline int equal (const value_type *, const compare_type *);
140 };
141 
142 inline hashval_t
hash(const value_type * p)143 simd_array_to_simduid::hash (const value_type *p)
144 {
145   return DECL_UID (p->decl);
146 }
147 
148 inline int
equal(const value_type * p1,const value_type * p2)149 simd_array_to_simduid::equal (const value_type *p1, const value_type *p2)
150 {
151   return p1->decl == p2->decl;
152 }
153 
154 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
155    into their corresponding constants.  */
156 
157 static void
adjust_simduid_builtins(hash_table<simduid_to_vf> & htab)158 adjust_simduid_builtins (hash_table <simduid_to_vf> &htab)
159 {
160   basic_block bb;
161 
162   FOR_EACH_BB_FN (bb, cfun)
163     {
164       gimple_stmt_iterator i;
165 
166       for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
167 	{
168 	  unsigned int vf = 1;
169 	  enum internal_fn ifn;
170 	  gimple stmt = gsi_stmt (i);
171 	  tree t;
172 	  if (!is_gimple_call (stmt)
173 	      || !gimple_call_internal_p (stmt))
174 	    continue;
175 	  ifn = gimple_call_internal_fn (stmt);
176 	  switch (ifn)
177 	    {
178 	    case IFN_GOMP_SIMD_LANE:
179 	    case IFN_GOMP_SIMD_VF:
180 	    case IFN_GOMP_SIMD_LAST_LANE:
181 	      break;
182 	    default:
183 	      continue;
184 	    }
185 	  tree arg = gimple_call_arg (stmt, 0);
186 	  gcc_assert (arg != NULL_TREE);
187 	  gcc_assert (TREE_CODE (arg) == SSA_NAME);
188 	  simduid_to_vf *p = NULL, data;
189 	  data.simduid = DECL_UID (SSA_NAME_VAR (arg));
190 	  if (htab.is_created ())
191 	    p = htab.find (&data);
192 	  if (p)
193 	    vf = p->vf;
194 	  switch (ifn)
195 	    {
196 	    case IFN_GOMP_SIMD_VF:
197 	      t = build_int_cst (unsigned_type_node, vf);
198 	      break;
199 	    case IFN_GOMP_SIMD_LANE:
200 	      t = build_int_cst (unsigned_type_node, 0);
201 	      break;
202 	    case IFN_GOMP_SIMD_LAST_LANE:
203 	      t = gimple_call_arg (stmt, 1);
204 	      break;
205 	    default:
206 	      gcc_unreachable ();
207 	    }
208 	  update_call_from_tree (&i, t);
209 	}
210     }
211 }
212 
213 /* Helper structure for note_simd_array_uses.  */
214 
215 struct note_simd_array_uses_struct
216 {
217   hash_table <simd_array_to_simduid> *htab;
218   unsigned int simduid;
219 };
220 
221 /* Callback for note_simd_array_uses, called through walk_gimple_op.  */
222 
223 static tree
note_simd_array_uses_cb(tree * tp,int * walk_subtrees,void * data)224 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
225 {
226   struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
227   struct note_simd_array_uses_struct *ns
228     = (struct note_simd_array_uses_struct *) wi->info;
229 
230   if (TYPE_P (*tp))
231     *walk_subtrees = 0;
232   else if (VAR_P (*tp)
233 	   && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
234 	   && DECL_CONTEXT (*tp) == current_function_decl)
235     {
236       simd_array_to_simduid data;
237       if (!ns->htab->is_created ())
238 	ns->htab->create (15);
239       data.decl = *tp;
240       data.simduid = ns->simduid;
241       simd_array_to_simduid **slot = ns->htab->find_slot (&data, INSERT);
242       if (*slot == NULL)
243 	{
244 	  simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
245 	  *p = data;
246 	  *slot = p;
247 	}
248       else if ((*slot)->simduid != ns->simduid)
249 	(*slot)->simduid = -1U;
250       *walk_subtrees = 0;
251     }
252   return NULL_TREE;
253 }
254 
255 /* Find "omp simd array" temporaries and map them to corresponding
256    simduid.  */
257 
258 static void
note_simd_array_uses(hash_table<simd_array_to_simduid> * htab)259 note_simd_array_uses (hash_table <simd_array_to_simduid> *htab)
260 {
261   basic_block bb;
262   gimple_stmt_iterator gsi;
263   struct walk_stmt_info wi;
264   struct note_simd_array_uses_struct ns;
265 
266   memset (&wi, 0, sizeof (wi));
267   wi.info = &ns;
268   ns.htab = htab;
269 
270   FOR_EACH_BB_FN (bb, cfun)
271     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
272       {
273 	gimple stmt = gsi_stmt (gsi);
274 	if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
275 	  continue;
276 	switch (gimple_call_internal_fn (stmt))
277 	  {
278 	  case IFN_GOMP_SIMD_LANE:
279 	  case IFN_GOMP_SIMD_VF:
280 	  case IFN_GOMP_SIMD_LAST_LANE:
281 	    break;
282 	  default:
283 	    continue;
284 	  }
285 	tree lhs = gimple_call_lhs (stmt);
286 	if (lhs == NULL_TREE)
287 	  continue;
288 	imm_use_iterator use_iter;
289 	gimple use_stmt;
290 	ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
291 	FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
292 	  if (!is_gimple_debug (use_stmt))
293 	    walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
294       }
295 }
296 
297 /* A helper function to free data refs.  */
298 
299 void
vect_destroy_datarefs(loop_vec_info loop_vinfo,bb_vec_info bb_vinfo)300 vect_destroy_datarefs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
301 {
302   vec<data_reference_p> datarefs;
303   struct data_reference *dr;
304   unsigned int i;
305 
306  if (loop_vinfo)
307     datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
308   else
309     datarefs = BB_VINFO_DATAREFS (bb_vinfo);
310 
311   FOR_EACH_VEC_ELT (datarefs, i, dr)
312     if (dr->aux)
313       {
314         free (dr->aux);
315         dr->aux = NULL;
316       }
317 
318   free_data_refs (datarefs);
319 }
320 
321 
322 /* If LOOP has been versioned during ifcvt, return the internal call
323    guarding it.  */
324 
325 static gimple
vect_loop_vectorized_call(struct loop * loop)326 vect_loop_vectorized_call (struct loop *loop)
327 {
328   basic_block bb = loop_preheader_edge (loop)->src;
329   gimple g;
330   do
331     {
332       g = last_stmt (bb);
333       if (g)
334 	break;
335       if (!single_pred_p (bb))
336 	break;
337       bb = single_pred (bb);
338     }
339   while (1);
340   if (g && gimple_code (g) == GIMPLE_COND)
341     {
342       gimple_stmt_iterator gsi = gsi_for_stmt (g);
343       gsi_prev (&gsi);
344       if (!gsi_end_p (gsi))
345 	{
346 	  g = gsi_stmt (gsi);
347 	  if (is_gimple_call (g)
348 	      && gimple_call_internal_p (g)
349 	      && gimple_call_internal_fn (g) == IFN_LOOP_VECTORIZED
350 	      && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
351 		  || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
352 	    return g;
353 	}
354     }
355   return NULL;
356 }
357 
358 /* Fold LOOP_VECTORIZED internal call G to VALUE and
359    update any immediate uses of it's LHS.  */
360 
361 static void
fold_loop_vectorized_call(gimple g,tree value)362 fold_loop_vectorized_call (gimple g, tree value)
363 {
364   tree lhs = gimple_call_lhs (g);
365   use_operand_p use_p;
366   imm_use_iterator iter;
367   gimple use_stmt;
368   gimple_stmt_iterator gsi = gsi_for_stmt (g);
369 
370   update_call_from_tree (&gsi, value);
371   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
372     {
373       FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
374 	SET_USE (use_p, value);
375       update_stmt (use_stmt);
376     }
377 }
378 
379 /* Function vectorize_loops.
380 
381    Entry point to loop vectorization phase.  */
382 
383 unsigned
vectorize_loops(void)384 vectorize_loops (void)
385 {
386   unsigned int i;
387   unsigned int num_vectorized_loops = 0;
388   unsigned int vect_loops_num;
389   struct loop *loop;
390   hash_table <simduid_to_vf> simduid_to_vf_htab;
391   hash_table <simd_array_to_simduid> simd_array_to_simduid_htab;
392   bool any_ifcvt_loops = false;
393   unsigned ret = 0;
394 
395   vect_loops_num = number_of_loops (cfun);
396 
397   /* Bail out if there are no loops.  */
398   if (vect_loops_num <= 1)
399     {
400       if (cfun->has_simduid_loops)
401 	adjust_simduid_builtins (simduid_to_vf_htab);
402       return 0;
403     }
404 
405   if (cfun->has_simduid_loops)
406     note_simd_array_uses (&simd_array_to_simduid_htab);
407 
408   init_stmt_vec_info_vec ();
409 
410   /*  ----------- Analyze loops. -----------  */
411 
412   /* If some loop was duplicated, it gets bigger number
413      than all previously defined loops.  This fact allows us to run
414      only over initial loops skipping newly generated ones.  */
415   FOR_EACH_LOOP (loop, 0)
416     if (loop->dont_vectorize)
417       any_ifcvt_loops = true;
418     else if ((flag_tree_loop_vectorize
419 	      && optimize_loop_nest_for_speed_p (loop))
420 	     || loop->force_vect)
421       {
422 	loop_vec_info loop_vinfo;
423 	vect_location = find_loop_location (loop);
424         if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
425 	    && dump_enabled_p ())
426 	  dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
427                        LOCATION_FILE (vect_location),
428 		       LOCATION_LINE (vect_location));
429 
430 	loop_vinfo = vect_analyze_loop (loop);
431 	loop->aux = loop_vinfo;
432 
433 	if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
434 	  continue;
435 
436         if (!dbg_cnt (vect_loop))
437 	  break;
438 
439 	gimple loop_vectorized_call = vect_loop_vectorized_call (loop);
440 	if (loop_vectorized_call)
441 	  {
442 	    tree arg = gimple_call_arg (loop_vectorized_call, 1);
443 	    basic_block *bbs;
444 	    unsigned int i;
445 	    struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
446 
447 	    LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
448 	    gcc_checking_assert (vect_loop_vectorized_call
449 					(LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
450 				 == loop_vectorized_call);
451 	    bbs = get_loop_body (scalar_loop);
452 	    for (i = 0; i < scalar_loop->num_nodes; i++)
453 	      {
454 		basic_block bb = bbs[i];
455 		gimple_stmt_iterator gsi;
456 		for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
457 		     gsi_next (&gsi))
458 		  {
459 		    gimple phi = gsi_stmt (gsi);
460 		    gimple_set_uid (phi, 0);
461 		  }
462 		for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
463 		     gsi_next (&gsi))
464 		  {
465 		    gimple stmt = gsi_stmt (gsi);
466 		    gimple_set_uid (stmt, 0);
467 		  }
468 	      }
469 	    free (bbs);
470 	  }
471 
472         if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
473 	    && dump_enabled_p ())
474           dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
475                            "loop vectorized\n");
476 	vect_transform_loop (loop_vinfo);
477 	num_vectorized_loops++;
478 	/* Now that the loop has been vectorized, allow it to be unrolled
479 	   etc.  */
480 	loop->force_vect = false;
481 
482 	if (loop->simduid)
483 	  {
484 	    simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
485 	    if (!simduid_to_vf_htab.is_created ())
486 	      simduid_to_vf_htab.create (15);
487 	    simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
488 	    simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
489 	    *simduid_to_vf_htab.find_slot (simduid_to_vf_data, INSERT)
490 	      = simduid_to_vf_data;
491 	  }
492 
493 	if (loop_vectorized_call)
494 	  {
495 	    fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
496 	    ret |= TODO_cleanup_cfg;
497 	  }
498       }
499 
500   vect_location = UNKNOWN_LOCATION;
501 
502   statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
503   if (dump_enabled_p ()
504       || (num_vectorized_loops > 0 && dump_enabled_p ()))
505     dump_printf_loc (MSG_NOTE, vect_location,
506                      "vectorized %u loops in function.\n",
507                      num_vectorized_loops);
508 
509   /*  ----------- Finalize. -----------  */
510 
511   if (any_ifcvt_loops)
512     for (i = 1; i < vect_loops_num; i++)
513       {
514 	loop = get_loop (cfun, i);
515 	if (loop && loop->dont_vectorize)
516 	  {
517 	    gimple g = vect_loop_vectorized_call (loop);
518 	    if (g)
519 	      {
520 		fold_loop_vectorized_call (g, boolean_false_node);
521 		ret |= TODO_cleanup_cfg;
522 	      }
523 	  }
524       }
525 
526   for (i = 1; i < vect_loops_num; i++)
527     {
528       loop_vec_info loop_vinfo;
529 
530       loop = get_loop (cfun, i);
531       if (!loop)
532 	continue;
533       loop_vinfo = (loop_vec_info) loop->aux;
534       destroy_loop_vec_info (loop_vinfo, true);
535       loop->aux = NULL;
536     }
537 
538   free_stmt_vec_info_vec ();
539 
540   /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins.  */
541   if (cfun->has_simduid_loops)
542     adjust_simduid_builtins (simduid_to_vf_htab);
543 
544   /* Shrink any "omp array simd" temporary arrays to the
545      actual vectorization factors.  */
546   if (simd_array_to_simduid_htab.is_created ())
547     {
548       for (hash_table <simd_array_to_simduid>::iterator iter
549 	   = simd_array_to_simduid_htab.begin ();
550 	   iter != simd_array_to_simduid_htab.end (); ++iter)
551 	if ((*iter).simduid != -1U)
552 	  {
553 	    tree decl = (*iter).decl;
554 	    int vf = 1;
555 	    if (simduid_to_vf_htab.is_created ())
556 	      {
557 		simduid_to_vf *p = NULL, data;
558 		data.simduid = (*iter).simduid;
559 		p = simduid_to_vf_htab.find (&data);
560 		if (p)
561 		  vf = p->vf;
562 	      }
563 	    tree atype
564 	      = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
565 	    TREE_TYPE (decl) = atype;
566 	    relayout_decl (decl);
567 	  }
568 
569       simd_array_to_simduid_htab.dispose ();
570     }
571   if (simduid_to_vf_htab.is_created ())
572     simduid_to_vf_htab.dispose ();
573 
574   if (num_vectorized_loops > 0)
575     {
576       /* If we vectorized any loop only virtual SSA form needs to be updated.
577 	 ???  Also while we try hard to update loop-closed SSA form we fail
578 	 to properly do this in some corner-cases (see PR56286).  */
579       rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
580       return TODO_cleanup_cfg;
581     }
582 
583   return ret;
584 }
585 
586 
587 /*  Entry point to basic block SLP phase.  */
588 
589 static unsigned int
execute_vect_slp(void)590 execute_vect_slp (void)
591 {
592   basic_block bb;
593 
594   init_stmt_vec_info_vec ();
595 
596   FOR_EACH_BB_FN (bb, cfun)
597     {
598       vect_location = find_bb_location (bb);
599 
600       if (vect_slp_analyze_bb (bb))
601         {
602           if (!dbg_cnt (vect_slp))
603             break;
604 
605           vect_slp_transform_bb (bb);
606           if (dump_enabled_p ())
607             dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
608 			     "basic block vectorized\n");
609         }
610     }
611 
612   free_stmt_vec_info_vec ();
613   return 0;
614 }
615 
616 static bool
gate_vect_slp(void)617 gate_vect_slp (void)
618 {
619   return flag_tree_slp_vectorize != 0;
620 }
621 
622 namespace {
623 
624 const pass_data pass_data_slp_vectorize =
625 {
626   GIMPLE_PASS, /* type */
627   "slp", /* name */
628   OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
629   true, /* has_gate */
630   true, /* has_execute */
631   TV_TREE_SLP_VECTORIZATION, /* tv_id */
632   ( PROP_ssa | PROP_cfg ), /* properties_required */
633   0, /* properties_provided */
634   0, /* properties_destroyed */
635   0, /* todo_flags_start */
636   ( TODO_verify_ssa | TODO_update_ssa
637     | TODO_verify_stmts ), /* todo_flags_finish */
638 };
639 
640 class pass_slp_vectorize : public gimple_opt_pass
641 {
642 public:
pass_slp_vectorize(gcc::context * ctxt)643   pass_slp_vectorize (gcc::context *ctxt)
644     : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
645   {}
646 
647   /* opt_pass methods: */
gate()648   bool gate () { return gate_vect_slp (); }
execute()649   unsigned int execute () { return execute_vect_slp (); }
650 
651 }; // class pass_slp_vectorize
652 
653 } // anon namespace
654 
655 gimple_opt_pass *
make_pass_slp_vectorize(gcc::context * ctxt)656 make_pass_slp_vectorize (gcc::context *ctxt)
657 {
658   return new pass_slp_vectorize (ctxt);
659 }
660 
661 
662 /* Increase alignment of global arrays to improve vectorization potential.
663    TODO:
664    - Consider also structs that have an array field.
665    - Use ipa analysis to prune arrays that can't be vectorized?
666      This should involve global alignment analysis and in the future also
667      array padding.  */
668 
669 static unsigned int
increase_alignment(void)670 increase_alignment (void)
671 {
672   varpool_node *vnode;
673 
674   vect_location = UNKNOWN_LOCATION;
675 
676   /* Increase the alignment of all global arrays for vectorization.  */
677   FOR_EACH_DEFINED_VARIABLE (vnode)
678     {
679       tree vectype, decl = vnode->decl;
680       tree t;
681       unsigned int alignment;
682 
683       t = TREE_TYPE (decl);
684       if (TREE_CODE (t) != ARRAY_TYPE)
685         continue;
686       vectype = get_vectype_for_scalar_type (strip_array_types (t));
687       if (!vectype)
688         continue;
689       alignment = TYPE_ALIGN (vectype);
690       if (DECL_ALIGN (decl) >= alignment)
691         continue;
692 
693       if (vect_can_force_dr_alignment_p (decl, alignment))
694         {
695           DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
696           DECL_USER_ALIGN (decl) = 1;
697           dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
698           dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
699           dump_printf (MSG_NOTE, "\n");
700         }
701     }
702   return 0;
703 }
704 
705 
706 static bool
gate_increase_alignment(void)707 gate_increase_alignment (void)
708 {
709   return flag_section_anchors && flag_tree_loop_vectorize;
710 }
711 
712 
713 namespace {
714 
715 const pass_data pass_data_ipa_increase_alignment =
716 {
717   SIMPLE_IPA_PASS, /* type */
718   "increase_alignment", /* name */
719   OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
720   true, /* has_gate */
721   true, /* has_execute */
722   TV_IPA_OPT, /* tv_id */
723   0, /* properties_required */
724   0, /* properties_provided */
725   0, /* properties_destroyed */
726   0, /* todo_flags_start */
727   0, /* todo_flags_finish */
728 };
729 
730 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
731 {
732 public:
pass_ipa_increase_alignment(gcc::context * ctxt)733   pass_ipa_increase_alignment (gcc::context *ctxt)
734     : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
735   {}
736 
737   /* opt_pass methods: */
gate()738   bool gate () { return gate_increase_alignment (); }
execute()739   unsigned int execute () { return increase_alignment (); }
740 
741 }; // class pass_ipa_increase_alignment
742 
743 } // anon namespace
744 
745 simple_ipa_opt_pass *
make_pass_ipa_increase_alignment(gcc::context * ctxt)746 make_pass_ipa_increase_alignment (gcc::context *ctxt)
747 {
748   return new pass_ipa_increase_alignment (ctxt);
749 }
750