PR target/81369
[official-gcc.git] / gcc / tree-vectorizer.c
blob33a1f580474cb93ccc4f2f43631bf7a90623c2ad
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 internal call G like IFN_LOOP_VECTORIZED/IFN_LOOP_DIST_ALIAS
452 to VALUE and update any immediate uses of it's LHS. */
454 static void
455 fold_loop_internal_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 /* If LOOP has been versioned during loop distribution, return the gurading
473 internal call. */
475 static gimple *
476 vect_loop_dist_alias_call (struct loop *loop)
478 basic_block bb;
479 basic_block entry;
480 struct loop *outer, *orig;
481 gimple_stmt_iterator gsi;
482 gimple *g;
484 if (loop->orig_loop_num == 0)
485 return NULL;
487 orig = get_loop (cfun, loop->orig_loop_num);
488 if (orig == NULL)
490 /* The original loop is somehow destroyed. Clear the information. */
491 loop->orig_loop_num = 0;
492 return NULL;
495 if (loop != orig)
496 bb = nearest_common_dominator (CDI_DOMINATORS, loop->header, orig->header);
497 else
498 bb = loop_preheader_edge (loop)->src;
500 outer = bb->loop_father;
501 entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
503 /* Look upward in dominance tree. */
504 for (; bb != entry && flow_bb_inside_loop_p (outer, bb);
505 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
507 g = last_stmt (bb);
508 if (g == NULL || gimple_code (g) != GIMPLE_COND)
509 continue;
511 gsi = gsi_for_stmt (g);
512 gsi_prev (&gsi);
513 if (gsi_end_p (gsi))
514 continue;
516 g = gsi_stmt (gsi);
517 /* The guarding internal function call must have the same distribution
518 alias id. */
519 if (gimple_call_internal_p (g, IFN_LOOP_DIST_ALIAS)
520 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->orig_loop_num))
521 return g;
523 return NULL;
526 /* Set the uids of all the statements in basic blocks inside loop
527 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
528 call guarding the loop which has been if converted. */
529 static void
530 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
532 tree arg = gimple_call_arg (loop_vectorized_call, 1);
533 basic_block *bbs;
534 unsigned int i;
535 struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
537 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
538 gcc_checking_assert (vect_loop_vectorized_call (scalar_loop)
539 == loop_vectorized_call);
540 /* If we are going to vectorize outer loop, prevent vectorization
541 of the inner loop in the scalar loop - either the scalar loop is
542 thrown away, so it is a wasted work, or is used only for
543 a few iterations. */
544 if (scalar_loop->inner)
546 gimple *g = vect_loop_vectorized_call (scalar_loop->inner);
547 if (g)
549 arg = gimple_call_arg (g, 0);
550 get_loop (cfun, tree_to_shwi (arg))->dont_vectorize = true;
551 fold_loop_internal_call (g, boolean_false_node);
554 bbs = get_loop_body (scalar_loop);
555 for (i = 0; i < scalar_loop->num_nodes; i++)
557 basic_block bb = bbs[i];
558 gimple_stmt_iterator gsi;
559 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
561 gimple *phi = gsi_stmt (gsi);
562 gimple_set_uid (phi, 0);
564 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
566 gimple *stmt = gsi_stmt (gsi);
567 gimple_set_uid (stmt, 0);
570 free (bbs);
573 /* Function vectorize_loops.
575 Entry point to loop vectorization phase. */
577 unsigned
578 vectorize_loops (void)
580 unsigned int i;
581 unsigned int num_vectorized_loops = 0;
582 unsigned int vect_loops_num;
583 struct loop *loop;
584 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
585 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
586 bool any_ifcvt_loops = false;
587 unsigned ret = 0;
588 struct loop *new_loop;
590 vect_loops_num = number_of_loops (cfun);
592 /* Bail out if there are no loops. */
593 if (vect_loops_num <= 1)
594 return 0;
596 if (cfun->has_simduid_loops)
597 note_simd_array_uses (&simd_array_to_simduid_htab);
599 init_stmt_vec_info_vec ();
601 /* ----------- Analyze loops. ----------- */
603 /* If some loop was duplicated, it gets bigger number
604 than all previously defined loops. This fact allows us to run
605 only over initial loops skipping newly generated ones. */
606 FOR_EACH_LOOP (loop, 0)
607 if (loop->dont_vectorize)
609 any_ifcvt_loops = true;
610 /* If-conversion sometimes versions both the outer loop
611 (for the case when outer loop vectorization might be
612 desirable) as well as the inner loop in the scalar version
613 of the loop. So we have:
614 if (LOOP_VECTORIZED (1, 3))
616 loop1
617 loop2
619 else
620 loop3 (copy of loop1)
621 if (LOOP_VECTORIZED (4, 5))
622 loop4 (copy of loop2)
623 else
624 loop5 (copy of loop4)
625 If FOR_EACH_LOOP gives us loop3 first (which has
626 dont_vectorize set), make sure to process loop1 before loop4;
627 so that we can prevent vectorization of loop4 if loop1
628 is successfully vectorized. */
629 if (loop->inner)
631 gimple *loop_vectorized_call
632 = vect_loop_vectorized_call (loop);
633 if (loop_vectorized_call
634 && vect_loop_vectorized_call (loop->inner))
636 tree arg = gimple_call_arg (loop_vectorized_call, 0);
637 struct loop *vector_loop
638 = get_loop (cfun, tree_to_shwi (arg));
639 if (vector_loop && vector_loop != loop)
641 loop = vector_loop;
642 /* Make sure we don't vectorize it twice. */
643 loop->dont_vectorize = true;
644 goto try_vectorize;
649 else
651 loop_vec_info loop_vinfo, orig_loop_vinfo;
652 gimple *loop_vectorized_call, *loop_dist_alias_call;
653 try_vectorize:
654 if (!((flag_tree_loop_vectorize
655 && optimize_loop_nest_for_speed_p (loop))
656 || loop->force_vectorize))
657 continue;
658 orig_loop_vinfo = NULL;
659 loop_vectorized_call = vect_loop_vectorized_call (loop);
660 loop_dist_alias_call = vect_loop_dist_alias_call (loop);
661 vectorize_epilogue:
662 vect_location = find_loop_location (loop);
663 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
664 && dump_enabled_p ())
665 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
666 LOCATION_FILE (vect_location),
667 LOCATION_LINE (vect_location));
669 loop_vinfo = vect_analyze_loop (loop, orig_loop_vinfo);
670 loop->aux = loop_vinfo;
672 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
674 /* Free existing information if loop is analyzed with some
675 assumptions. */
676 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
677 vect_free_loop_info_assumptions (loop);
679 /* If we applied if-conversion then try to vectorize the
680 BB of innermost loops.
681 ??? Ideally BB vectorization would learn to vectorize
682 control flow by applying if-conversion on-the-fly, the
683 following retains the if-converted loop body even when
684 only non-if-converted parts took part in BB vectorization. */
685 if (flag_tree_slp_vectorize != 0
686 && loop_vectorized_call
687 && ! loop->inner)
689 basic_block bb = loop->header;
690 bool has_mask_load_store = false;
691 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
692 !gsi_end_p (gsi); gsi_next (&gsi))
694 gimple *stmt = gsi_stmt (gsi);
695 if (is_gimple_call (stmt)
696 && gimple_call_internal_p (stmt)
697 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
698 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
700 has_mask_load_store = true;
701 break;
703 gimple_set_uid (stmt, -1);
704 gimple_set_visited (stmt, false);
706 if (! has_mask_load_store && vect_slp_bb (bb))
708 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
709 "basic block vectorized\n");
710 fold_loop_internal_call (loop_vectorized_call,
711 boolean_true_node);
712 loop_vectorized_call = NULL;
713 ret |= TODO_cleanup_cfg;
716 /* If outer loop vectorization fails for LOOP_VECTORIZED guarded
717 loop, don't vectorize its inner loop; we'll attempt to
718 vectorize LOOP_VECTORIZED guarded inner loop of the scalar
719 loop version. */
720 if (loop_vectorized_call && loop->inner)
721 loop->inner->dont_vectorize = true;
722 continue;
725 if (!dbg_cnt (vect_loop))
727 /* We may miss some if-converted loops due to
728 debug counter. Set any_ifcvt_loops to visit
729 them at finalization. */
730 any_ifcvt_loops = true;
731 /* Free existing information if loop is analyzed with some
732 assumptions. */
733 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
734 vect_free_loop_info_assumptions (loop);
736 break;
739 if (loop_vectorized_call)
740 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
741 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
742 && dump_enabled_p ())
743 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
744 "loop vectorized\n");
745 new_loop = vect_transform_loop (loop_vinfo);
746 num_vectorized_loops++;
747 /* Now that the loop has been vectorized, allow it to be unrolled
748 etc. */
749 loop->force_vectorize = false;
751 if (loop->simduid)
753 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
754 if (!simduid_to_vf_htab)
755 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
756 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
757 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
758 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
759 = simduid_to_vf_data;
762 if (loop_vectorized_call)
764 fold_loop_internal_call (loop_vectorized_call, boolean_true_node);
765 loop_vectorized_call = NULL;
766 ret |= TODO_cleanup_cfg;
768 if (loop_dist_alias_call)
770 tree value = gimple_call_arg (loop_dist_alias_call, 1);
771 fold_loop_internal_call (loop_dist_alias_call, value);
772 loop_dist_alias_call = NULL;
773 ret |= TODO_cleanup_cfg;
776 if (new_loop)
778 /* Epilogue of vectorized loop must be vectorized too. */
779 vect_loops_num = number_of_loops (cfun);
780 loop = new_loop;
781 orig_loop_vinfo = loop_vinfo; /* To pass vect_analyze_loop. */
782 goto vectorize_epilogue;
786 vect_location = UNKNOWN_LOCATION;
788 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
789 if (dump_enabled_p ()
790 || (num_vectorized_loops > 0 && dump_enabled_p ()))
791 dump_printf_loc (MSG_NOTE, vect_location,
792 "vectorized %u loops in function.\n",
793 num_vectorized_loops);
795 /* ----------- Finalize. ----------- */
797 if (any_ifcvt_loops)
798 for (i = 1; i < vect_loops_num; i++)
800 loop = get_loop (cfun, i);
801 if (loop && loop->dont_vectorize)
803 gimple *g = vect_loop_vectorized_call (loop);
804 if (g)
806 fold_loop_internal_call (g, boolean_false_node);
807 ret |= TODO_cleanup_cfg;
808 g = NULL;
810 else
811 g = vect_loop_dist_alias_call (loop);
813 if (g)
815 fold_loop_internal_call (g, boolean_false_node);
816 ret |= TODO_cleanup_cfg;
821 for (i = 1; i < vect_loops_num; i++)
823 loop_vec_info loop_vinfo;
824 bool has_mask_store;
826 loop = get_loop (cfun, i);
827 if (!loop)
828 continue;
829 loop_vinfo = (loop_vec_info) loop->aux;
830 has_mask_store = false;
831 if (loop_vinfo)
832 has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
833 destroy_loop_vec_info (loop_vinfo, true);
834 if (has_mask_store)
835 optimize_mask_stores (loop);
836 loop->aux = NULL;
839 free_stmt_vec_info_vec ();
841 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
842 if (cfun->has_simduid_loops)
843 adjust_simduid_builtins (simduid_to_vf_htab);
845 /* Shrink any "omp array simd" temporary arrays to the
846 actual vectorization factors. */
847 if (simd_array_to_simduid_htab)
848 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
849 delete simduid_to_vf_htab;
850 cfun->has_simduid_loops = false;
852 if (num_vectorized_loops > 0)
854 /* If we vectorized any loop only virtual SSA form needs to be updated.
855 ??? Also while we try hard to update loop-closed SSA form we fail
856 to properly do this in some corner-cases (see PR56286). */
857 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
858 return TODO_cleanup_cfg;
861 return ret;
865 /* Entry point to the simduid cleanup pass. */
867 namespace {
869 const pass_data pass_data_simduid_cleanup =
871 GIMPLE_PASS, /* type */
872 "simduid", /* name */
873 OPTGROUP_NONE, /* optinfo_flags */
874 TV_NONE, /* tv_id */
875 ( PROP_ssa | PROP_cfg ), /* properties_required */
876 0, /* properties_provided */
877 0, /* properties_destroyed */
878 0, /* todo_flags_start */
879 0, /* todo_flags_finish */
882 class pass_simduid_cleanup : public gimple_opt_pass
884 public:
885 pass_simduid_cleanup (gcc::context *ctxt)
886 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
889 /* opt_pass methods: */
890 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
891 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
892 virtual unsigned int execute (function *);
894 }; // class pass_simduid_cleanup
896 unsigned int
897 pass_simduid_cleanup::execute (function *fun)
899 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
901 note_simd_array_uses (&simd_array_to_simduid_htab);
903 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
904 adjust_simduid_builtins (NULL);
906 /* Shrink any "omp array simd" temporary arrays to the
907 actual vectorization factors. */
908 if (simd_array_to_simduid_htab)
909 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
910 fun->has_simduid_loops = false;
911 return 0;
914 } // anon namespace
916 gimple_opt_pass *
917 make_pass_simduid_cleanup (gcc::context *ctxt)
919 return new pass_simduid_cleanup (ctxt);
923 /* Entry point to basic block SLP phase. */
925 namespace {
927 const pass_data pass_data_slp_vectorize =
929 GIMPLE_PASS, /* type */
930 "slp", /* name */
931 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
932 TV_TREE_SLP_VECTORIZATION, /* tv_id */
933 ( PROP_ssa | PROP_cfg ), /* properties_required */
934 0, /* properties_provided */
935 0, /* properties_destroyed */
936 0, /* todo_flags_start */
937 TODO_update_ssa, /* todo_flags_finish */
940 class pass_slp_vectorize : public gimple_opt_pass
942 public:
943 pass_slp_vectorize (gcc::context *ctxt)
944 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
947 /* opt_pass methods: */
948 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
949 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
950 virtual unsigned int execute (function *);
952 }; // class pass_slp_vectorize
954 unsigned int
955 pass_slp_vectorize::execute (function *fun)
957 basic_block bb;
959 bool in_loop_pipeline = scev_initialized_p ();
960 if (!in_loop_pipeline)
962 loop_optimizer_init (LOOPS_NORMAL);
963 scev_initialize ();
966 /* Mark all stmts as not belonging to the current region and unvisited. */
967 FOR_EACH_BB_FN (bb, fun)
969 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
970 gsi_next (&gsi))
972 gimple *stmt = gsi_stmt (gsi);
973 gimple_set_uid (stmt, -1);
974 gimple_set_visited (stmt, false);
978 init_stmt_vec_info_vec ();
980 FOR_EACH_BB_FN (bb, fun)
982 if (vect_slp_bb (bb))
983 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
984 "basic block vectorized\n");
987 free_stmt_vec_info_vec ();
989 if (!in_loop_pipeline)
991 scev_finalize ();
992 loop_optimizer_finalize ();
995 return 0;
998 } // anon namespace
1000 gimple_opt_pass *
1001 make_pass_slp_vectorize (gcc::context *ctxt)
1003 return new pass_slp_vectorize (ctxt);
1007 /* Increase alignment of global arrays to improve vectorization potential.
1008 TODO:
1009 - Consider also structs that have an array field.
1010 - Use ipa analysis to prune arrays that can't be vectorized?
1011 This should involve global alignment analysis and in the future also
1012 array padding. */
1014 static unsigned get_vec_alignment_for_type (tree);
1015 static hash_map<tree, unsigned> *type_align_map;
1017 /* Return alignment of array's vector type corresponding to scalar type.
1018 0 if no vector type exists. */
1019 static unsigned
1020 get_vec_alignment_for_array_type (tree type)
1022 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1024 tree vectype = get_vectype_for_scalar_type (strip_array_types (type));
1025 if (!vectype
1026 || !TYPE_SIZE (type)
1027 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1028 || tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (vectype)))
1029 return 0;
1031 return TYPE_ALIGN (vectype);
1034 /* Return alignment of field having maximum alignment of vector type
1035 corresponding to it's scalar type. For now, we only consider fields whose
1036 offset is a multiple of it's vector alignment.
1037 0 if no suitable field is found. */
1038 static unsigned
1039 get_vec_alignment_for_record_type (tree type)
1041 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1043 unsigned max_align = 0, alignment;
1044 HOST_WIDE_INT offset;
1045 tree offset_tree;
1047 if (TYPE_PACKED (type))
1048 return 0;
1050 unsigned *slot = type_align_map->get (type);
1051 if (slot)
1052 return *slot;
1054 for (tree field = first_field (type);
1055 field != NULL_TREE;
1056 field = DECL_CHAIN (field))
1058 /* Skip if not FIELD_DECL or if alignment is set by user. */
1059 if (TREE_CODE (field) != FIELD_DECL
1060 || DECL_USER_ALIGN (field)
1061 || DECL_ARTIFICIAL (field))
1062 continue;
1064 /* We don't need to process the type further if offset is variable,
1065 since the offsets of remaining members will also be variable. */
1066 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
1067 || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
1068 break;
1070 /* Similarly stop processing the type if offset_tree
1071 does not fit in unsigned HOST_WIDE_INT. */
1072 offset_tree = bit_position (field);
1073 if (!tree_fits_uhwi_p (offset_tree))
1074 break;
1076 offset = tree_to_uhwi (offset_tree);
1077 alignment = get_vec_alignment_for_type (TREE_TYPE (field));
1079 /* Get maximum alignment of vectorized field/array among those members
1080 whose offset is multiple of the vector alignment. */
1081 if (alignment
1082 && (offset % alignment == 0)
1083 && (alignment > max_align))
1084 max_align = alignment;
1087 type_align_map->put (type, max_align);
1088 return max_align;
1091 /* Return alignment of vector type corresponding to decl's scalar type
1092 or 0 if it doesn't exist or the vector alignment is lesser than
1093 decl's alignment. */
1094 static unsigned
1095 get_vec_alignment_for_type (tree type)
1097 if (type == NULL_TREE)
1098 return 0;
1100 gcc_assert (TYPE_P (type));
1102 static unsigned alignment = 0;
1103 switch (TREE_CODE (type))
1105 case ARRAY_TYPE:
1106 alignment = get_vec_alignment_for_array_type (type);
1107 break;
1108 case RECORD_TYPE:
1109 alignment = get_vec_alignment_for_record_type (type);
1110 break;
1111 default:
1112 alignment = 0;
1113 break;
1116 return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
1119 /* Entry point to increase_alignment pass. */
1120 static unsigned int
1121 increase_alignment (void)
1123 varpool_node *vnode;
1125 vect_location = UNKNOWN_LOCATION;
1126 type_align_map = new hash_map<tree, unsigned>;
1128 /* Increase the alignment of all global arrays for vectorization. */
1129 FOR_EACH_DEFINED_VARIABLE (vnode)
1131 tree decl = vnode->decl;
1132 unsigned int alignment;
1134 if ((decl_in_symtab_p (decl)
1135 && !symtab_node::get (decl)->can_increase_alignment_p ())
1136 || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
1137 continue;
1139 alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
1140 if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
1142 vnode->increase_alignment (alignment);
1143 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
1144 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
1145 dump_printf (MSG_NOTE, "\n");
1149 delete type_align_map;
1150 return 0;
1154 namespace {
1156 const pass_data pass_data_ipa_increase_alignment =
1158 SIMPLE_IPA_PASS, /* type */
1159 "increase_alignment", /* name */
1160 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1161 TV_IPA_OPT, /* tv_id */
1162 0, /* properties_required */
1163 0, /* properties_provided */
1164 0, /* properties_destroyed */
1165 0, /* todo_flags_start */
1166 0, /* todo_flags_finish */
1169 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
1171 public:
1172 pass_ipa_increase_alignment (gcc::context *ctxt)
1173 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
1176 /* opt_pass methods: */
1177 virtual bool gate (function *)
1179 return flag_section_anchors && flag_tree_loop_vectorize;
1182 virtual unsigned int execute (function *) { return increase_alignment (); }
1184 }; // class pass_ipa_increase_alignment
1186 } // anon namespace
1188 simple_ipa_opt_pass *
1189 make_pass_ipa_increase_alignment (gcc::context *ctxt)
1191 return new pass_ipa_increase_alignment (ctxt);