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