* configure.ac: If the compiler supports -Qunused-arguments, use
[official-gcc.git] / gcc / tree-vectorizer.c
blobc02653748bff82d9ab7ec80b89a0afd7ff6fedd1
1 /* Vectorizer
2 Copyright (C) 2003-2014 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 "dumpfile.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "stor-layout.h"
64 #include "tree-pretty-print.h"
65 #include "basic-block.h"
66 #include "tree-ssa-alias.h"
67 #include "internal-fn.h"
68 #include "gimple-expr.h"
69 #include "is-a.h"
70 #include "gimple.h"
71 #include "gimple-iterator.h"
72 #include "gimple-walk.h"
73 #include "gimple-ssa.h"
74 #include "cgraph.h"
75 #include "tree-phinodes.h"
76 #include "ssa-iterators.h"
77 #include "tree-ssa-loop-manip.h"
78 #include "tree-cfg.h"
79 #include "cfgloop.h"
80 #include "tree-vectorizer.h"
81 #include "tree-pass.h"
82 #include "tree-ssa-propagate.h"
83 #include "dbgcnt.h"
84 #include "gimple-fold.h"
85 #include "tree-scalar-evolution.h"
88 /* Loop or bb location. */
89 source_location vect_location;
91 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
92 vec<vec_void_p> stmt_vec_info_vec;
94 /* For mapping simduid to vectorization factor. */
96 struct simduid_to_vf : typed_free_remove<simduid_to_vf>
98 unsigned int simduid;
99 int vf;
101 /* hash_table support. */
102 typedef simduid_to_vf value_type;
103 typedef simduid_to_vf compare_type;
104 static inline hashval_t hash (const value_type *);
105 static inline int equal (const value_type *, const compare_type *);
108 inline hashval_t
109 simduid_to_vf::hash (const value_type *p)
111 return p->simduid;
114 inline int
115 simduid_to_vf::equal (const value_type *p1, const value_type *p2)
117 return p1->simduid == p2->simduid;
120 /* This hash maps the OMP simd array to the corresponding simduid used
121 to index into it. Like thus,
123 _7 = GOMP_SIMD_LANE (simduid.0)
126 D.1737[_7] = stuff;
129 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
130 simduid.0. */
132 struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
134 tree decl;
135 unsigned int simduid;
137 /* hash_table support. */
138 typedef simd_array_to_simduid value_type;
139 typedef simd_array_to_simduid compare_type;
140 static inline hashval_t hash (const value_type *);
141 static inline int equal (const value_type *, const compare_type *);
144 inline hashval_t
145 simd_array_to_simduid::hash (const value_type *p)
147 return DECL_UID (p->decl);
150 inline int
151 simd_array_to_simduid::equal (const value_type *p1, const value_type *p2)
153 return p1->decl == p2->decl;
156 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
157 into their corresponding constants. */
159 static void
160 adjust_simduid_builtins (hash_table<simduid_to_vf> **htab)
162 basic_block bb;
164 FOR_EACH_BB_FN (bb, cfun)
166 gimple_stmt_iterator i;
168 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
170 unsigned int vf = 1;
171 enum internal_fn ifn;
172 gimple stmt = gsi_stmt (i);
173 tree t;
174 if (!is_gimple_call (stmt)
175 || !gimple_call_internal_p (stmt))
176 continue;
177 ifn = gimple_call_internal_fn (stmt);
178 switch (ifn)
180 case IFN_GOMP_SIMD_LANE:
181 case IFN_GOMP_SIMD_VF:
182 case IFN_GOMP_SIMD_LAST_LANE:
183 break;
184 default:
185 continue;
187 tree arg = gimple_call_arg (stmt, 0);
188 gcc_assert (arg != NULL_TREE);
189 gcc_assert (TREE_CODE (arg) == SSA_NAME);
190 simduid_to_vf *p = NULL, data;
191 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
192 if (*htab)
193 p = (*htab)->find (&data);
194 if (p)
195 vf = p->vf;
196 switch (ifn)
198 case IFN_GOMP_SIMD_VF:
199 t = build_int_cst (unsigned_type_node, vf);
200 break;
201 case IFN_GOMP_SIMD_LANE:
202 t = build_int_cst (unsigned_type_node, 0);
203 break;
204 case IFN_GOMP_SIMD_LAST_LANE:
205 t = gimple_call_arg (stmt, 1);
206 break;
207 default:
208 gcc_unreachable ();
210 update_call_from_tree (&i, t);
215 /* Helper structure for note_simd_array_uses. */
217 struct note_simd_array_uses_struct
219 hash_table<simd_array_to_simduid> **htab;
220 unsigned int simduid;
223 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
225 static tree
226 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
228 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
229 struct note_simd_array_uses_struct *ns
230 = (struct note_simd_array_uses_struct *) wi->info;
232 if (TYPE_P (*tp))
233 *walk_subtrees = 0;
234 else if (VAR_P (*tp)
235 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
236 && DECL_CONTEXT (*tp) == current_function_decl)
238 simd_array_to_simduid data;
239 if (!*ns->htab)
240 *ns->htab = new hash_table<simd_array_to_simduid> (15);
241 data.decl = *tp;
242 data.simduid = ns->simduid;
243 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
244 if (*slot == NULL)
246 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
247 *p = data;
248 *slot = p;
250 else if ((*slot)->simduid != ns->simduid)
251 (*slot)->simduid = -1U;
252 *walk_subtrees = 0;
254 return NULL_TREE;
257 /* Find "omp simd array" temporaries and map them to corresponding
258 simduid. */
260 static void
261 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
263 basic_block bb;
264 gimple_stmt_iterator gsi;
265 struct walk_stmt_info wi;
266 struct note_simd_array_uses_struct ns;
268 memset (&wi, 0, sizeof (wi));
269 wi.info = &ns;
270 ns.htab = htab;
272 FOR_EACH_BB_FN (bb, cfun)
273 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
275 gimple stmt = gsi_stmt (gsi);
276 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
277 continue;
278 switch (gimple_call_internal_fn (stmt))
280 case IFN_GOMP_SIMD_LANE:
281 case IFN_GOMP_SIMD_VF:
282 case IFN_GOMP_SIMD_LAST_LANE:
283 break;
284 default:
285 continue;
287 tree lhs = gimple_call_lhs (stmt);
288 if (lhs == NULL_TREE)
289 continue;
290 imm_use_iterator use_iter;
291 gimple use_stmt;
292 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
293 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
294 if (!is_gimple_debug (use_stmt))
295 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
299 /* A helper function to free data refs. */
301 void
302 vect_destroy_datarefs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
304 vec<data_reference_p> datarefs;
305 struct data_reference *dr;
306 unsigned int i;
308 if (loop_vinfo)
309 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
310 else
311 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
313 FOR_EACH_VEC_ELT (datarefs, i, dr)
314 if (dr->aux)
316 free (dr->aux);
317 dr->aux = NULL;
320 free_data_refs (datarefs);
324 /* If LOOP has been versioned during ifcvt, return the internal call
325 guarding it. */
327 static gimple
328 vect_loop_vectorized_call (struct loop *loop)
330 basic_block bb = loop_preheader_edge (loop)->src;
331 gimple g;
334 g = last_stmt (bb);
335 if (g)
336 break;
337 if (!single_pred_p (bb))
338 break;
339 bb = single_pred (bb);
341 while (1);
342 if (g && gimple_code (g) == GIMPLE_COND)
344 gimple_stmt_iterator gsi = gsi_for_stmt (g);
345 gsi_prev (&gsi);
346 if (!gsi_end_p (gsi))
348 g = gsi_stmt (gsi);
349 if (is_gimple_call (g)
350 && gimple_call_internal_p (g)
351 && gimple_call_internal_fn (g) == IFN_LOOP_VECTORIZED
352 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
353 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
354 return g;
357 return NULL;
360 /* Fold LOOP_VECTORIZED internal call G to VALUE and
361 update any immediate uses of it's LHS. */
363 static void
364 fold_loop_vectorized_call (gimple g, tree value)
366 tree lhs = gimple_call_lhs (g);
367 use_operand_p use_p;
368 imm_use_iterator iter;
369 gimple use_stmt;
370 gimple_stmt_iterator gsi = gsi_for_stmt (g);
372 update_call_from_tree (&gsi, value);
373 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
375 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
376 SET_USE (use_p, value);
377 update_stmt (use_stmt);
381 /* Function vectorize_loops.
383 Entry point to loop vectorization phase. */
385 unsigned
386 vectorize_loops (void)
388 unsigned int i;
389 unsigned int num_vectorized_loops = 0;
390 unsigned int vect_loops_num;
391 struct loop *loop;
392 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
393 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
394 bool any_ifcvt_loops = false;
395 unsigned ret = 0;
397 vect_loops_num = number_of_loops (cfun);
399 /* Bail out if there are no loops. */
400 if (vect_loops_num <= 1)
402 if (cfun->has_simduid_loops)
403 adjust_simduid_builtins (&simduid_to_vf_htab);
404 return 0;
407 if (cfun->has_simduid_loops)
408 note_simd_array_uses (&simd_array_to_simduid_htab);
410 init_stmt_vec_info_vec ();
412 /* ----------- Analyze loops. ----------- */
414 /* If some loop was duplicated, it gets bigger number
415 than all previously defined loops. This fact allows us to run
416 only over initial loops skipping newly generated ones. */
417 FOR_EACH_LOOP (loop, 0)
418 if (loop->dont_vectorize)
419 any_ifcvt_loops = true;
420 else if ((flag_tree_loop_vectorize
421 && optimize_loop_nest_for_speed_p (loop))
422 || loop->force_vectorize)
424 loop_vec_info loop_vinfo;
425 vect_location = find_loop_location (loop);
426 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
427 && dump_enabled_p ())
428 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
429 LOCATION_FILE (vect_location),
430 LOCATION_LINE (vect_location));
432 loop_vinfo = vect_analyze_loop (loop);
433 loop->aux = loop_vinfo;
435 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
436 continue;
438 if (!dbg_cnt (vect_loop))
439 break;
441 gimple loop_vectorized_call = vect_loop_vectorized_call (loop);
442 if (loop_vectorized_call)
444 tree arg = gimple_call_arg (loop_vectorized_call, 1);
445 basic_block *bbs;
446 unsigned int i;
447 struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
449 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
450 gcc_checking_assert (vect_loop_vectorized_call
451 (LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
452 == loop_vectorized_call);
453 bbs = get_loop_body (scalar_loop);
454 for (i = 0; i < scalar_loop->num_nodes; i++)
456 basic_block bb = bbs[i];
457 gimple_stmt_iterator gsi;
458 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
459 gsi_next (&gsi))
461 gimple phi = gsi_stmt (gsi);
462 gimple_set_uid (phi, 0);
464 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
465 gsi_next (&gsi))
467 gimple stmt = gsi_stmt (gsi);
468 gimple_set_uid (stmt, 0);
471 free (bbs);
474 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
475 && dump_enabled_p ())
476 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
477 "loop vectorized\n");
478 vect_transform_loop (loop_vinfo);
479 num_vectorized_loops++;
480 /* Now that the loop has been vectorized, allow it to be unrolled
481 etc. */
482 loop->force_vectorize = false;
484 if (loop->simduid)
486 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
487 if (!simduid_to_vf_htab)
488 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
489 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
490 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
491 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
492 = simduid_to_vf_data;
495 if (loop_vectorized_call)
497 fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
498 ret |= TODO_cleanup_cfg;
502 vect_location = UNKNOWN_LOCATION;
504 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
505 if (dump_enabled_p ()
506 || (num_vectorized_loops > 0 && dump_enabled_p ()))
507 dump_printf_loc (MSG_NOTE, vect_location,
508 "vectorized %u loops in function.\n",
509 num_vectorized_loops);
511 /* ----------- Finalize. ----------- */
513 if (any_ifcvt_loops)
514 for (i = 1; i < vect_loops_num; i++)
516 loop = get_loop (cfun, i);
517 if (loop && loop->dont_vectorize)
519 gimple g = vect_loop_vectorized_call (loop);
520 if (g)
522 fold_loop_vectorized_call (g, boolean_false_node);
523 ret |= TODO_cleanup_cfg;
528 for (i = 1; i < vect_loops_num; i++)
530 loop_vec_info loop_vinfo;
532 loop = get_loop (cfun, i);
533 if (!loop)
534 continue;
535 loop_vinfo = (loop_vec_info) loop->aux;
536 destroy_loop_vec_info (loop_vinfo, true);
537 loop->aux = NULL;
540 free_stmt_vec_info_vec ();
542 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */
543 if (cfun->has_simduid_loops)
544 adjust_simduid_builtins (&simduid_to_vf_htab);
546 /* Shrink any "omp array simd" temporary arrays to the
547 actual vectorization factors. */
548 if (simd_array_to_simduid_htab)
550 for (hash_table<simd_array_to_simduid>::iterator iter
551 = simd_array_to_simduid_htab->begin ();
552 iter != simd_array_to_simduid_htab->end (); ++iter)
553 if ((*iter)->simduid != -1U)
555 tree decl = (*iter)->decl;
556 int vf = 1;
557 if (simduid_to_vf_htab)
559 simduid_to_vf *p = NULL, data;
560 data.simduid = (*iter)->simduid;
561 p = simduid_to_vf_htab->find (&data);
562 if (p)
563 vf = p->vf;
565 tree atype
566 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
567 TREE_TYPE (decl) = atype;
568 relayout_decl (decl);
571 delete simd_array_to_simduid_htab;
573 delete simduid_to_vf_htab;
574 simduid_to_vf_htab = NULL;
576 if (num_vectorized_loops > 0)
578 /* If we vectorized any loop only virtual SSA form needs to be updated.
579 ??? Also while we try hard to update loop-closed SSA form we fail
580 to properly do this in some corner-cases (see PR56286). */
581 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
582 return TODO_cleanup_cfg;
585 return ret;
589 /* Entry point to basic block SLP phase. */
591 namespace {
593 const pass_data pass_data_slp_vectorize =
595 GIMPLE_PASS, /* type */
596 "slp", /* name */
597 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
598 TV_TREE_SLP_VECTORIZATION, /* tv_id */
599 ( PROP_ssa | PROP_cfg ), /* properties_required */
600 0, /* properties_provided */
601 0, /* properties_destroyed */
602 0, /* todo_flags_start */
603 TODO_update_ssa, /* todo_flags_finish */
606 class pass_slp_vectorize : public gimple_opt_pass
608 public:
609 pass_slp_vectorize (gcc::context *ctxt)
610 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
613 /* opt_pass methods: */
614 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
615 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
616 virtual unsigned int execute (function *);
618 }; // class pass_slp_vectorize
620 unsigned int
621 pass_slp_vectorize::execute (function *fun)
623 basic_block bb;
625 bool in_loop_pipeline = scev_initialized_p ();
626 if (!in_loop_pipeline)
628 loop_optimizer_init (LOOPS_NORMAL);
629 scev_initialize ();
632 init_stmt_vec_info_vec ();
634 FOR_EACH_BB_FN (bb, fun)
636 vect_location = find_bb_location (bb);
638 if (vect_slp_analyze_bb (bb))
640 if (!dbg_cnt (vect_slp))
641 break;
643 vect_slp_transform_bb (bb);
644 if (dump_enabled_p ())
645 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
646 "basic block vectorized\n");
650 free_stmt_vec_info_vec ();
652 if (!in_loop_pipeline)
654 scev_finalize ();
655 loop_optimizer_finalize ();
658 return 0;
661 } // anon namespace
663 gimple_opt_pass *
664 make_pass_slp_vectorize (gcc::context *ctxt)
666 return new pass_slp_vectorize (ctxt);
670 /* Increase alignment of global arrays to improve vectorization potential.
671 TODO:
672 - Consider also structs that have an array field.
673 - Use ipa analysis to prune arrays that can't be vectorized?
674 This should involve global alignment analysis and in the future also
675 array padding. */
677 static unsigned int
678 increase_alignment (void)
680 varpool_node *vnode;
682 vect_location = UNKNOWN_LOCATION;
684 /* Increase the alignment of all global arrays for vectorization. */
685 FOR_EACH_DEFINED_VARIABLE (vnode)
687 tree vectype, decl = vnode->decl;
688 tree t;
689 unsigned int alignment;
691 t = TREE_TYPE (decl);
692 if (TREE_CODE (t) != ARRAY_TYPE)
693 continue;
694 vectype = get_vectype_for_scalar_type (strip_array_types (t));
695 if (!vectype)
696 continue;
697 alignment = TYPE_ALIGN (vectype);
698 if (DECL_ALIGN (decl) >= alignment)
699 continue;
701 if (vect_can_force_dr_alignment_p (decl, alignment))
703 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
704 DECL_USER_ALIGN (decl) = 1;
705 if (TREE_STATIC (decl))
707 tree target = symtab_node::get (decl)->ultimate_alias_target ()->decl;
708 DECL_ALIGN (target) = TYPE_ALIGN (vectype);
709 DECL_USER_ALIGN (target) = 1;
711 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
712 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
713 dump_printf (MSG_NOTE, "\n");
716 return 0;
720 namespace {
722 const pass_data pass_data_ipa_increase_alignment =
724 SIMPLE_IPA_PASS, /* type */
725 "increase_alignment", /* name */
726 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
727 TV_IPA_OPT, /* tv_id */
728 0, /* properties_required */
729 0, /* properties_provided */
730 0, /* properties_destroyed */
731 0, /* todo_flags_start */
732 0, /* todo_flags_finish */
735 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
737 public:
738 pass_ipa_increase_alignment (gcc::context *ctxt)
739 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
742 /* opt_pass methods: */
743 virtual bool gate (function *)
745 return flag_section_anchors && flag_tree_loop_vectorize;
748 virtual unsigned int execute (function *) { return increase_alignment (); }
750 }; // class pass_ipa_increase_alignment
752 } // anon namespace
754 simple_ipa_opt_pass *
755 make_pass_ipa_increase_alignment (gcc::context *ctxt)
757 return new pass_ipa_increase_alignment (ctxt);