libgo: add misc/cgo files
[official-gcc.git] / gcc / tree-vectorizer.c
blob0d62c829ef5875f7ce6732d82e64601e79b2b130
1 /* Vectorizer
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 /* Loop and basic block vectorizer.
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
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)
41 Here's a poor attempt at illustrating that:
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
57 #include "config.h"
58 #include "system.h"
59 #include "coretypes.h"
60 #include "backend.h"
61 #include "tree.h"
62 #include "gimple.h"
63 #include "predict.h"
64 #include "tree-pass.h"
65 #include "ssa.h"
66 #include "cgraph.h"
67 #include "fold-const.h"
68 #include "stor-layout.h"
69 #include "gimple-iterator.h"
70 #include "gimple-walk.h"
71 #include "tree-ssa-loop-manip.h"
72 #include "tree-ssa-loop-niter.h"
73 #include "tree-cfg.h"
74 #include "cfgloop.h"
75 #include "tree-vectorizer.h"
76 #include "tree-ssa-propagate.h"
77 #include "dbgcnt.h"
78 #include "tree-scalar-evolution.h"
81 /* Loop or bb location. */
82 source_location vect_location;
84 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
85 vec<stmt_vec_info> stmt_vec_info_vec;
87 /* For mapping simduid to vectorization factor. */
89 struct simduid_to_vf : free_ptr_hash<simduid_to_vf>
91 unsigned int simduid;
92 int vf;
94 /* hash_table support. */
95 static inline hashval_t hash (const simduid_to_vf *);
96 static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
99 inline hashval_t
100 simduid_to_vf::hash (const simduid_to_vf *p)
102 return p->simduid;
105 inline int
106 simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
108 return p1->simduid == p2->simduid;
111 /* This hash maps the OMP simd array to the corresponding simduid used
112 to index into it. Like thus,
114 _7 = GOMP_SIMD_LANE (simduid.0)
117 D.1737[_7] = stuff;
120 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
121 simduid.0. */
123 struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
125 tree decl;
126 unsigned int simduid;
128 /* hash_table support. */
129 static inline hashval_t hash (const simd_array_to_simduid *);
130 static inline int equal (const simd_array_to_simduid *,
131 const simd_array_to_simduid *);
134 inline hashval_t
135 simd_array_to_simduid::hash (const simd_array_to_simduid *p)
137 return DECL_UID (p->decl);
140 inline int
141 simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
142 const simd_array_to_simduid *p2)
144 return p1->decl == p2->decl;
147 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LAST_LANE,
148 into their corresponding constants and remove
149 IFN_GOMP_SIMD_ORDERED_{START,END}. */
151 static void
152 adjust_simduid_builtins (hash_table<simduid_to_vf> *htab)
154 basic_block bb;
156 FOR_EACH_BB_FN (bb, cfun)
158 gimple_stmt_iterator i;
160 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
162 unsigned int vf = 1;
163 enum internal_fn ifn;
164 gimple *stmt = gsi_stmt (i);
165 tree t;
166 if (!is_gimple_call (stmt)
167 || !gimple_call_internal_p (stmt))
169 gsi_next (&i);
170 continue;
172 ifn = gimple_call_internal_fn (stmt);
173 switch (ifn)
175 case IFN_GOMP_SIMD_LANE:
176 case IFN_GOMP_SIMD_VF:
177 case IFN_GOMP_SIMD_LAST_LANE:
178 break;
179 case IFN_GOMP_SIMD_ORDERED_START:
180 case IFN_GOMP_SIMD_ORDERED_END:
181 if (integer_onep (gimple_call_arg (stmt, 0)))
183 enum built_in_function bcode
184 = (ifn == IFN_GOMP_SIMD_ORDERED_START
185 ? BUILT_IN_GOMP_ORDERED_START
186 : BUILT_IN_GOMP_ORDERED_END);
187 gimple *g
188 = gimple_build_call (builtin_decl_explicit (bcode), 0);
189 tree vdef = gimple_vdef (stmt);
190 gimple_set_vdef (g, vdef);
191 SSA_NAME_DEF_STMT (vdef) = g;
192 gimple_set_vuse (g, gimple_vuse (stmt));
193 gsi_replace (&i, g, true);
194 continue;
196 gsi_remove (&i, true);
197 unlink_stmt_vdef (stmt);
198 continue;
199 default:
200 gsi_next (&i);
201 continue;
203 tree arg = gimple_call_arg (stmt, 0);
204 gcc_assert (arg != NULL_TREE);
205 gcc_assert (TREE_CODE (arg) == SSA_NAME);
206 simduid_to_vf *p = NULL, data;
207 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
208 /* Need to nullify loop safelen field since it's value is not
209 valid after transformation. */
210 if (bb->loop_father && bb->loop_father->safelen > 0)
211 bb->loop_father->safelen = 0;
212 if (htab)
214 p = htab->find (&data);
215 if (p)
216 vf = p->vf;
218 switch (ifn)
220 case IFN_GOMP_SIMD_VF:
221 t = build_int_cst (unsigned_type_node, vf);
222 break;
223 case IFN_GOMP_SIMD_LANE:
224 t = build_int_cst (unsigned_type_node, 0);
225 break;
226 case IFN_GOMP_SIMD_LAST_LANE:
227 t = gimple_call_arg (stmt, 1);
228 break;
229 default:
230 gcc_unreachable ();
232 tree lhs = gimple_call_lhs (stmt);
233 if (lhs)
234 replace_uses_by (lhs, t);
235 release_defs (stmt);
236 gsi_remove (&i, true);
241 /* Helper structure for note_simd_array_uses. */
243 struct note_simd_array_uses_struct
245 hash_table<simd_array_to_simduid> **htab;
246 unsigned int simduid;
249 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
251 static tree
252 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
254 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
255 struct note_simd_array_uses_struct *ns
256 = (struct note_simd_array_uses_struct *) wi->info;
258 if (TYPE_P (*tp))
259 *walk_subtrees = 0;
260 else if (VAR_P (*tp)
261 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
262 && DECL_CONTEXT (*tp) == current_function_decl)
264 simd_array_to_simduid data;
265 if (!*ns->htab)
266 *ns->htab = new hash_table<simd_array_to_simduid> (15);
267 data.decl = *tp;
268 data.simduid = ns->simduid;
269 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
270 if (*slot == NULL)
272 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
273 *p = data;
274 *slot = p;
276 else if ((*slot)->simduid != ns->simduid)
277 (*slot)->simduid = -1U;
278 *walk_subtrees = 0;
280 return NULL_TREE;
283 /* Find "omp simd array" temporaries and map them to corresponding
284 simduid. */
286 static void
287 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
289 basic_block bb;
290 gimple_stmt_iterator gsi;
291 struct walk_stmt_info wi;
292 struct note_simd_array_uses_struct ns;
294 memset (&wi, 0, sizeof (wi));
295 wi.info = &ns;
296 ns.htab = htab;
298 FOR_EACH_BB_FN (bb, cfun)
299 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
301 gimple *stmt = gsi_stmt (gsi);
302 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
303 continue;
304 switch (gimple_call_internal_fn (stmt))
306 case IFN_GOMP_SIMD_LANE:
307 case IFN_GOMP_SIMD_VF:
308 case IFN_GOMP_SIMD_LAST_LANE:
309 break;
310 default:
311 continue;
313 tree lhs = gimple_call_lhs (stmt);
314 if (lhs == NULL_TREE)
315 continue;
316 imm_use_iterator use_iter;
317 gimple *use_stmt;
318 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
319 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
320 if (!is_gimple_debug (use_stmt))
321 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
325 /* Shrink arrays with "omp simd array" attribute to the corresponding
326 vectorization factor. */
328 static void
329 shrink_simd_arrays
330 (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
331 hash_table<simduid_to_vf> *simduid_to_vf_htab)
333 for (hash_table<simd_array_to_simduid>::iterator iter
334 = simd_array_to_simduid_htab->begin ();
335 iter != simd_array_to_simduid_htab->end (); ++iter)
336 if ((*iter)->simduid != -1U)
338 tree decl = (*iter)->decl;
339 int vf = 1;
340 if (simduid_to_vf_htab)
342 simduid_to_vf *p = NULL, data;
343 data.simduid = (*iter)->simduid;
344 p = simduid_to_vf_htab->find (&data);
345 if (p)
346 vf = p->vf;
348 tree atype
349 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
350 TREE_TYPE (decl) = atype;
351 relayout_decl (decl);
354 delete simd_array_to_simduid_htab;
357 /* A helper function to free data refs. */
359 void
360 vect_destroy_datarefs (vec_info *vinfo)
362 struct data_reference *dr;
363 unsigned int i;
365 FOR_EACH_VEC_ELT (vinfo->datarefs, i, dr)
366 if (dr->aux)
368 free (dr->aux);
369 dr->aux = NULL;
372 free_data_refs (vinfo->datarefs);
375 /* A helper function to free scev and LOOP niter information, as well as
376 clear loop constraint LOOP_C_FINITE. */
378 void
379 vect_free_loop_info_assumptions (struct loop *loop)
381 scev_reset_htab ();
382 /* We need to explicitly reset upper bound information since they are
383 used even after free_numbers_of_iterations_estimates. */
384 loop->any_upper_bound = false;
385 loop->any_likely_upper_bound = false;
386 free_numbers_of_iterations_estimates (loop);
387 loop_constraint_clear (loop, LOOP_C_FINITE);
390 /* Return whether STMT is inside the region we try to vectorize. */
392 bool
393 vect_stmt_in_region_p (vec_info *vinfo, gimple *stmt)
395 if (!gimple_bb (stmt))
396 return false;
398 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
400 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
401 if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
402 return false;
404 else
406 bb_vec_info bb_vinfo = as_a <bb_vec_info> (vinfo);
407 if (gimple_bb (stmt) != BB_VINFO_BB (bb_vinfo)
408 || gimple_uid (stmt) == -1U
409 || gimple_code (stmt) == GIMPLE_PHI)
410 return false;
413 return true;
417 /* If LOOP has been versioned during ifcvt, return the internal call
418 guarding it. */
420 static gimple *
421 vect_loop_vectorized_call (struct loop *loop)
423 basic_block bb = loop_preheader_edge (loop)->src;
424 gimple *g;
427 g = last_stmt (bb);
428 if (g)
429 break;
430 if (!single_pred_p (bb))
431 break;
432 bb = single_pred (bb);
434 while (1);
435 if (g && gimple_code (g) == GIMPLE_COND)
437 gimple_stmt_iterator gsi = gsi_for_stmt (g);
438 gsi_prev (&gsi);
439 if (!gsi_end_p (gsi))
441 g = gsi_stmt (gsi);
442 if (gimple_call_internal_p (g, IFN_LOOP_VECTORIZED)
443 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
444 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
445 return g;
448 return NULL;
451 /* Fold LOOP_VECTORIZED internal call G to VALUE and
452 update any immediate uses of it's LHS. */
454 static void
455 fold_loop_vectorized_call (gimple *g, tree value)
457 tree lhs = gimple_call_lhs (g);
458 use_operand_p use_p;
459 imm_use_iterator iter;
460 gimple *use_stmt;
461 gimple_stmt_iterator gsi = gsi_for_stmt (g);
463 update_call_from_tree (&gsi, value);
464 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
466 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
467 SET_USE (use_p, value);
468 update_stmt (use_stmt);
472 /* Set the uids of all the statements in basic blocks inside loop
473 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
474 call guarding the loop which has been if converted. */
475 static void
476 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
478 tree arg = gimple_call_arg (loop_vectorized_call, 1);
479 basic_block *bbs;
480 unsigned int i;
481 struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
483 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
484 gcc_checking_assert (vect_loop_vectorized_call (scalar_loop)
485 == loop_vectorized_call);
486 /* If we are going to vectorize outer loop, prevent vectorization
487 of the inner loop in the scalar loop - either the scalar loop is
488 thrown away, so it is a wasted work, or is used only for
489 a few iterations. */
490 if (scalar_loop->inner)
492 gimple *g = vect_loop_vectorized_call (scalar_loop->inner);
493 if (g)
495 arg = gimple_call_arg (g, 0);
496 get_loop (cfun, tree_to_shwi (arg))->dont_vectorize = true;
497 fold_loop_vectorized_call (g, boolean_false_node);
500 bbs = get_loop_body (scalar_loop);
501 for (i = 0; i < scalar_loop->num_nodes; i++)
503 basic_block bb = bbs[i];
504 gimple_stmt_iterator gsi;
505 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
507 gimple *phi = gsi_stmt (gsi);
508 gimple_set_uid (phi, 0);
510 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
512 gimple *stmt = gsi_stmt (gsi);
513 gimple_set_uid (stmt, 0);
516 free (bbs);
519 /* Function vectorize_loops.
521 Entry point to loop vectorization phase. */
523 unsigned
524 vectorize_loops (void)
526 unsigned int i;
527 unsigned int num_vectorized_loops = 0;
528 unsigned int vect_loops_num;
529 struct loop *loop;
530 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
531 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
532 bool any_ifcvt_loops = false;
533 unsigned ret = 0;
534 struct loop *new_loop;
536 vect_loops_num = number_of_loops (cfun);
538 /* Bail out if there are no loops. */
539 if (vect_loops_num <= 1)
540 return 0;
542 if (cfun->has_simduid_loops)
543 note_simd_array_uses (&simd_array_to_simduid_htab);
545 init_stmt_vec_info_vec ();
547 /* ----------- Analyze loops. ----------- */
549 /* If some loop was duplicated, it gets bigger number
550 than all previously defined loops. This fact allows us to run
551 only over initial loops skipping newly generated ones. */
552 FOR_EACH_LOOP (loop, 0)
553 if (loop->dont_vectorize)
555 any_ifcvt_loops = true;
556 /* If-conversion sometimes versions both the outer loop
557 (for the case when outer loop vectorization might be
558 desirable) as well as the inner loop in the scalar version
559 of the loop. So we have:
560 if (LOOP_VECTORIZED (1, 3))
562 loop1
563 loop2
565 else
566 loop3 (copy of loop1)
567 if (LOOP_VECTORIZED (4, 5))
568 loop4 (copy of loop2)
569 else
570 loop5 (copy of loop4)
571 If FOR_EACH_LOOP gives us loop3 first (which has
572 dont_vectorize set), make sure to process loop1 before loop4;
573 so that we can prevent vectorization of loop4 if loop1
574 is successfully vectorized. */
575 if (loop->inner)
577 gimple *loop_vectorized_call
578 = vect_loop_vectorized_call (loop);
579 if (loop_vectorized_call
580 && vect_loop_vectorized_call (loop->inner))
582 tree arg = gimple_call_arg (loop_vectorized_call, 0);
583 struct loop *vector_loop
584 = get_loop (cfun, tree_to_shwi (arg));
585 if (vector_loop && vector_loop != loop)
587 loop = vector_loop;
588 /* Make sure we don't vectorize it twice. */
589 loop->dont_vectorize = true;
590 goto try_vectorize;
595 else
597 loop_vec_info loop_vinfo, orig_loop_vinfo;
598 gimple *loop_vectorized_call;
599 try_vectorize:
600 if (!((flag_tree_loop_vectorize
601 && optimize_loop_nest_for_speed_p (loop))
602 || loop->force_vectorize))
603 continue;
604 orig_loop_vinfo = NULL;
605 loop_vectorized_call = vect_loop_vectorized_call (loop);
606 vectorize_epilogue:
607 vect_location = find_loop_location (loop);
608 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
609 && dump_enabled_p ())
610 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
611 LOCATION_FILE (vect_location),
612 LOCATION_LINE (vect_location));
614 loop_vinfo = vect_analyze_loop (loop, orig_loop_vinfo);
615 loop->aux = loop_vinfo;
617 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
619 /* Free existing information if loop is analyzed with some
620 assumptions. */
621 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
622 vect_free_loop_info_assumptions (loop);
624 /* If we applied if-conversion then try to vectorize the
625 BB of innermost loops.
626 ??? Ideally BB vectorization would learn to vectorize
627 control flow by applying if-conversion on-the-fly, the
628 following retains the if-converted loop body even when
629 only non-if-converted parts took part in BB vectorization. */
630 if (flag_tree_slp_vectorize != 0
631 && loop_vectorized_call
632 && ! loop->inner)
634 basic_block bb = loop->header;
635 bool has_mask_load_store = false;
636 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
637 !gsi_end_p (gsi); gsi_next (&gsi))
639 gimple *stmt = gsi_stmt (gsi);
640 if (is_gimple_call (stmt)
641 && gimple_call_internal_p (stmt)
642 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
643 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
645 has_mask_load_store = true;
646 break;
648 gimple_set_uid (stmt, -1);
649 gimple_set_visited (stmt, false);
651 if (! has_mask_load_store && vect_slp_bb (bb))
653 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
654 "basic block vectorized\n");
655 fold_loop_vectorized_call (loop_vectorized_call,
656 boolean_true_node);
657 loop_vectorized_call = NULL;
658 ret |= TODO_cleanup_cfg;
661 /* If outer loop vectorization fails for LOOP_VECTORIZED guarded
662 loop, don't vectorize its inner loop; we'll attempt to
663 vectorize LOOP_VECTORIZED guarded inner loop of the scalar
664 loop version. */
665 if (loop_vectorized_call && loop->inner)
666 loop->inner->dont_vectorize = true;
667 continue;
670 if (!dbg_cnt (vect_loop))
672 /* We may miss some if-converted loops due to
673 debug counter. Set any_ifcvt_loops to visit
674 them at finalization. */
675 any_ifcvt_loops = true;
676 /* Free existing information if loop is analyzed with some
677 assumptions. */
678 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
679 vect_free_loop_info_assumptions (loop);
681 break;
684 if (loop_vectorized_call)
685 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
686 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
687 && dump_enabled_p ())
688 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
689 "loop vectorized\n");
690 new_loop = vect_transform_loop (loop_vinfo);
691 num_vectorized_loops++;
692 /* Now that the loop has been vectorized, allow it to be unrolled
693 etc. */
694 loop->force_vectorize = false;
696 if (loop->simduid)
698 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
699 if (!simduid_to_vf_htab)
700 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
701 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
702 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
703 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
704 = simduid_to_vf_data;
707 if (loop_vectorized_call)
709 fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
710 loop_vectorized_call = NULL;
711 ret |= TODO_cleanup_cfg;
714 if (new_loop)
716 /* Epilogue of vectorized loop must be vectorized too. */
717 vect_loops_num = number_of_loops (cfun);
718 loop = new_loop;
719 orig_loop_vinfo = loop_vinfo; /* To pass vect_analyze_loop. */
720 goto vectorize_epilogue;
724 vect_location = UNKNOWN_LOCATION;
726 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
727 if (dump_enabled_p ()
728 || (num_vectorized_loops > 0 && dump_enabled_p ()))
729 dump_printf_loc (MSG_NOTE, vect_location,
730 "vectorized %u loops in function.\n",
731 num_vectorized_loops);
733 /* ----------- Finalize. ----------- */
735 if (any_ifcvt_loops)
736 for (i = 1; i < vect_loops_num; i++)
738 loop = get_loop (cfun, i);
739 if (loop && loop->dont_vectorize)
741 gimple *g = vect_loop_vectorized_call (loop);
742 if (g)
744 fold_loop_vectorized_call (g, boolean_false_node);
745 ret |= TODO_cleanup_cfg;
750 for (i = 1; i < vect_loops_num; i++)
752 loop_vec_info loop_vinfo;
753 bool has_mask_store;
755 loop = get_loop (cfun, i);
756 if (!loop)
757 continue;
758 loop_vinfo = (loop_vec_info) loop->aux;
759 has_mask_store = false;
760 if (loop_vinfo)
761 has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
762 destroy_loop_vec_info (loop_vinfo, true);
763 if (has_mask_store)
764 optimize_mask_stores (loop);
765 loop->aux = NULL;
768 free_stmt_vec_info_vec ();
770 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
771 if (cfun->has_simduid_loops)
772 adjust_simduid_builtins (simduid_to_vf_htab);
774 /* Shrink any "omp array simd" temporary arrays to the
775 actual vectorization factors. */
776 if (simd_array_to_simduid_htab)
777 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
778 delete simduid_to_vf_htab;
779 cfun->has_simduid_loops = false;
781 if (num_vectorized_loops > 0)
783 /* If we vectorized any loop only virtual SSA form needs to be updated.
784 ??? Also while we try hard to update loop-closed SSA form we fail
785 to properly do this in some corner-cases (see PR56286). */
786 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
787 return TODO_cleanup_cfg;
790 return ret;
794 /* Entry point to the simduid cleanup pass. */
796 namespace {
798 const pass_data pass_data_simduid_cleanup =
800 GIMPLE_PASS, /* type */
801 "simduid", /* name */
802 OPTGROUP_NONE, /* optinfo_flags */
803 TV_NONE, /* tv_id */
804 ( PROP_ssa | PROP_cfg ), /* properties_required */
805 0, /* properties_provided */
806 0, /* properties_destroyed */
807 0, /* todo_flags_start */
808 0, /* todo_flags_finish */
811 class pass_simduid_cleanup : public gimple_opt_pass
813 public:
814 pass_simduid_cleanup (gcc::context *ctxt)
815 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
818 /* opt_pass methods: */
819 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
820 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
821 virtual unsigned int execute (function *);
823 }; // class pass_simduid_cleanup
825 unsigned int
826 pass_simduid_cleanup::execute (function *fun)
828 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
830 note_simd_array_uses (&simd_array_to_simduid_htab);
832 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
833 adjust_simduid_builtins (NULL);
835 /* Shrink any "omp array simd" temporary arrays to the
836 actual vectorization factors. */
837 if (simd_array_to_simduid_htab)
838 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
839 fun->has_simduid_loops = false;
840 return 0;
843 } // anon namespace
845 gimple_opt_pass *
846 make_pass_simduid_cleanup (gcc::context *ctxt)
848 return new pass_simduid_cleanup (ctxt);
852 /* Entry point to basic block SLP phase. */
854 namespace {
856 const pass_data pass_data_slp_vectorize =
858 GIMPLE_PASS, /* type */
859 "slp", /* name */
860 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
861 TV_TREE_SLP_VECTORIZATION, /* tv_id */
862 ( PROP_ssa | PROP_cfg ), /* properties_required */
863 0, /* properties_provided */
864 0, /* properties_destroyed */
865 0, /* todo_flags_start */
866 TODO_update_ssa, /* todo_flags_finish */
869 class pass_slp_vectorize : public gimple_opt_pass
871 public:
872 pass_slp_vectorize (gcc::context *ctxt)
873 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
876 /* opt_pass methods: */
877 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
878 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
879 virtual unsigned int execute (function *);
881 }; // class pass_slp_vectorize
883 unsigned int
884 pass_slp_vectorize::execute (function *fun)
886 basic_block bb;
888 bool in_loop_pipeline = scev_initialized_p ();
889 if (!in_loop_pipeline)
891 loop_optimizer_init (LOOPS_NORMAL);
892 scev_initialize ();
895 /* Mark all stmts as not belonging to the current region and unvisited. */
896 FOR_EACH_BB_FN (bb, fun)
898 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
899 gsi_next (&gsi))
901 gimple *stmt = gsi_stmt (gsi);
902 gimple_set_uid (stmt, -1);
903 gimple_set_visited (stmt, false);
907 init_stmt_vec_info_vec ();
909 FOR_EACH_BB_FN (bb, fun)
911 if (vect_slp_bb (bb))
912 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
913 "basic block vectorized\n");
916 free_stmt_vec_info_vec ();
918 if (!in_loop_pipeline)
920 scev_finalize ();
921 loop_optimizer_finalize ();
924 return 0;
927 } // anon namespace
929 gimple_opt_pass *
930 make_pass_slp_vectorize (gcc::context *ctxt)
932 return new pass_slp_vectorize (ctxt);
936 /* Increase alignment of global arrays to improve vectorization potential.
937 TODO:
938 - Consider also structs that have an array field.
939 - Use ipa analysis to prune arrays that can't be vectorized?
940 This should involve global alignment analysis and in the future also
941 array padding. */
943 static unsigned get_vec_alignment_for_type (tree);
944 static hash_map<tree, unsigned> *type_align_map;
946 /* Return alignment of array's vector type corresponding to scalar type.
947 0 if no vector type exists. */
948 static unsigned
949 get_vec_alignment_for_array_type (tree type)
951 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
953 tree vectype = get_vectype_for_scalar_type (strip_array_types (type));
954 if (!vectype
955 || !TYPE_SIZE (type)
956 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
957 || tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (vectype)))
958 return 0;
960 return TYPE_ALIGN (vectype);
963 /* Return alignment of field having maximum alignment of vector type
964 corresponding to it's scalar type. For now, we only consider fields whose
965 offset is a multiple of it's vector alignment.
966 0 if no suitable field is found. */
967 static unsigned
968 get_vec_alignment_for_record_type (tree type)
970 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
972 unsigned max_align = 0, alignment;
973 HOST_WIDE_INT offset;
974 tree offset_tree;
976 if (TYPE_PACKED (type))
977 return 0;
979 unsigned *slot = type_align_map->get (type);
980 if (slot)
981 return *slot;
983 for (tree field = first_field (type);
984 field != NULL_TREE;
985 field = DECL_CHAIN (field))
987 /* Skip if not FIELD_DECL or if alignment is set by user. */
988 if (TREE_CODE (field) != FIELD_DECL
989 || DECL_USER_ALIGN (field)
990 || DECL_ARTIFICIAL (field))
991 continue;
993 /* We don't need to process the type further if offset is variable,
994 since the offsets of remaining members will also be variable. */
995 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
996 || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
997 break;
999 /* Similarly stop processing the type if offset_tree
1000 does not fit in unsigned HOST_WIDE_INT. */
1001 offset_tree = bit_position (field);
1002 if (!tree_fits_uhwi_p (offset_tree))
1003 break;
1005 offset = tree_to_uhwi (offset_tree);
1006 alignment = get_vec_alignment_for_type (TREE_TYPE (field));
1008 /* Get maximum alignment of vectorized field/array among those members
1009 whose offset is multiple of the vector alignment. */
1010 if (alignment
1011 && (offset % alignment == 0)
1012 && (alignment > max_align))
1013 max_align = alignment;
1016 type_align_map->put (type, max_align);
1017 return max_align;
1020 /* Return alignment of vector type corresponding to decl's scalar type
1021 or 0 if it doesn't exist or the vector alignment is lesser than
1022 decl's alignment. */
1023 static unsigned
1024 get_vec_alignment_for_type (tree type)
1026 if (type == NULL_TREE)
1027 return 0;
1029 gcc_assert (TYPE_P (type));
1031 static unsigned alignment = 0;
1032 switch (TREE_CODE (type))
1034 case ARRAY_TYPE:
1035 alignment = get_vec_alignment_for_array_type (type);
1036 break;
1037 case RECORD_TYPE:
1038 alignment = get_vec_alignment_for_record_type (type);
1039 break;
1040 default:
1041 alignment = 0;
1042 break;
1045 return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
1048 /* Entry point to increase_alignment pass. */
1049 static unsigned int
1050 increase_alignment (void)
1052 varpool_node *vnode;
1054 vect_location = UNKNOWN_LOCATION;
1055 type_align_map = new hash_map<tree, unsigned>;
1057 /* Increase the alignment of all global arrays for vectorization. */
1058 FOR_EACH_DEFINED_VARIABLE (vnode)
1060 tree decl = vnode->decl;
1061 unsigned int alignment;
1063 if ((decl_in_symtab_p (decl)
1064 && !symtab_node::get (decl)->can_increase_alignment_p ())
1065 || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
1066 continue;
1068 alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
1069 if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
1071 vnode->increase_alignment (alignment);
1072 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
1073 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
1074 dump_printf (MSG_NOTE, "\n");
1078 delete type_align_map;
1079 return 0;
1083 namespace {
1085 const pass_data pass_data_ipa_increase_alignment =
1087 SIMPLE_IPA_PASS, /* type */
1088 "increase_alignment", /* name */
1089 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1090 TV_IPA_OPT, /* tv_id */
1091 0, /* properties_required */
1092 0, /* properties_provided */
1093 0, /* properties_destroyed */
1094 0, /* todo_flags_start */
1095 0, /* todo_flags_finish */
1098 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
1100 public:
1101 pass_ipa_increase_alignment (gcc::context *ctxt)
1102 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
1105 /* opt_pass methods: */
1106 virtual bool gate (function *)
1108 return flag_section_anchors && flag_tree_loop_vectorize;
1111 virtual unsigned int execute (function *) { return increase_alignment (); }
1113 }; // class pass_ipa_increase_alignment
1115 } // anon namespace
1117 simple_ipa_opt_pass *
1118 make_pass_ipa_increase_alignment (gcc::context *ctxt)
1120 return new pass_ipa_increase_alignment (ctxt);