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