PR middle-end/59175
[official-gcc.git] / gcc / tree-vectorizer.c
blob9c2cf5d1ce213553605f20668b8f9fe692a6be31
1 /* Vectorizer
2 Copyright (C) 2003-2013 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 "ggc.h"
63 #include "tree.h"
64 #include "tree-pretty-print.h"
65 #include "gimple.h"
66 #include "gimple-iterator.h"
67 #include "gimple-walk.h"
68 #include "gimple-ssa.h"
69 #include "cgraph.h"
70 #include "tree-phinodes.h"
71 #include "ssa-iterators.h"
72 #include "tree-ssa-loop-manip.h"
73 #include "cfgloop.h"
74 #include "tree-vectorizer.h"
75 #include "tree-pass.h"
76 #include "hash-table.h"
77 #include "tree-ssa-propagate.h"
78 #include "dbgcnt.h"
80 /* Loop or bb location. */
81 LOC vect_location;
83 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
84 vec<vec_void_p> stmt_vec_info_vec;
86 /* For mapping simduid to vectorization factor. */
88 struct simduid_to_vf : typed_free_remove<simduid_to_vf>
90 unsigned int simduid;
91 int vf;
93 /* hash_table support. */
94 typedef simduid_to_vf value_type;
95 typedef simduid_to_vf compare_type;
96 static inline hashval_t hash (const value_type *);
97 static inline int equal (const value_type *, const compare_type *);
100 inline hashval_t
101 simduid_to_vf::hash (const value_type *p)
103 return p->simduid;
106 inline int
107 simduid_to_vf::equal (const value_type *p1, const value_type *p2)
109 return p1->simduid == p2->simduid;
112 /* This hash maps the OMP simd array to the corresponding simduid used
113 to index into it. Like thus,
115 _7 = GOMP_SIMD_LANE (simduid.0)
118 D.1737[_7] = stuff;
121 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
122 simduid.0. */
124 struct simd_array_to_simduid : typed_free_remove<simd_array_to_simduid>
126 tree decl;
127 unsigned int simduid;
129 /* hash_table support. */
130 typedef simd_array_to_simduid value_type;
131 typedef simd_array_to_simduid compare_type;
132 static inline hashval_t hash (const value_type *);
133 static inline int equal (const value_type *, const compare_type *);
136 inline hashval_t
137 simd_array_to_simduid::hash (const value_type *p)
139 return DECL_UID (p->decl);
142 inline int
143 simd_array_to_simduid::equal (const value_type *p1, const value_type *p2)
145 return p1->decl == p2->decl;
148 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
149 into their corresponding constants. */
151 static void
152 adjust_simduid_builtins (hash_table <simduid_to_vf> &htab)
154 basic_block bb;
156 FOR_EACH_BB (bb)
158 gimple_stmt_iterator i;
160 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&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))
168 continue;
169 ifn = gimple_call_internal_fn (stmt);
170 switch (ifn)
172 case IFN_GOMP_SIMD_LANE:
173 case IFN_GOMP_SIMD_VF:
174 case IFN_GOMP_SIMD_LAST_LANE:
175 break;
176 default:
177 continue;
179 tree arg = gimple_call_arg (stmt, 0);
180 gcc_assert (arg != NULL_TREE);
181 gcc_assert (TREE_CODE (arg) == SSA_NAME);
182 simduid_to_vf *p = NULL, data;
183 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
184 if (htab.is_created ())
185 p = htab.find (&data);
186 if (p)
187 vf = p->vf;
188 switch (ifn)
190 case IFN_GOMP_SIMD_VF:
191 t = build_int_cst (unsigned_type_node, vf);
192 break;
193 case IFN_GOMP_SIMD_LANE:
194 t = build_int_cst (unsigned_type_node, 0);
195 break;
196 case IFN_GOMP_SIMD_LAST_LANE:
197 t = gimple_call_arg (stmt, 1);
198 break;
199 default:
200 gcc_unreachable ();
202 update_call_from_tree (&i, t);
207 /* Helper structure for note_simd_array_uses. */
209 struct note_simd_array_uses_struct
211 hash_table <simd_array_to_simduid> *htab;
212 unsigned int simduid;
215 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
217 static tree
218 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
220 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
221 struct note_simd_array_uses_struct *ns
222 = (struct note_simd_array_uses_struct *) wi->info;
224 if (TYPE_P (*tp))
225 *walk_subtrees = 0;
226 else if (VAR_P (*tp)
227 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
228 && DECL_CONTEXT (*tp) == current_function_decl)
230 simd_array_to_simduid data;
231 if (!ns->htab->is_created ())
232 ns->htab->create (15);
233 data.decl = *tp;
234 data.simduid = ns->simduid;
235 simd_array_to_simduid **slot = ns->htab->find_slot (&data, INSERT);
236 if (*slot == NULL)
238 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
239 *p = data;
240 *slot = p;
242 else if ((*slot)->simduid != ns->simduid)
243 (*slot)->simduid = -1U;
244 *walk_subtrees = 0;
246 return NULL_TREE;
249 /* Find "omp simd array" temporaries and map them to corresponding
250 simduid. */
252 static void
253 note_simd_array_uses (hash_table <simd_array_to_simduid> *htab)
255 basic_block bb;
256 gimple_stmt_iterator gsi;
257 struct walk_stmt_info wi;
258 struct note_simd_array_uses_struct ns;
260 memset (&wi, 0, sizeof (wi));
261 wi.info = &ns;
262 ns.htab = htab;
264 FOR_EACH_BB (bb)
265 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
267 gimple stmt = gsi_stmt (gsi);
268 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
269 continue;
270 switch (gimple_call_internal_fn (stmt))
272 case IFN_GOMP_SIMD_LANE:
273 case IFN_GOMP_SIMD_VF:
274 case IFN_GOMP_SIMD_LAST_LANE:
275 break;
276 default:
277 continue;
279 tree lhs = gimple_call_lhs (stmt);
280 if (lhs == NULL_TREE)
281 continue;
282 imm_use_iterator use_iter;
283 gimple use_stmt;
284 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
285 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
286 if (!is_gimple_debug (use_stmt))
287 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
291 /* A helper function to free data refs. */
293 void
294 vect_destroy_datarefs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
296 vec<data_reference_p> datarefs;
297 struct data_reference *dr;
298 unsigned int i;
300 if (loop_vinfo)
301 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
302 else
303 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
305 FOR_EACH_VEC_ELT (datarefs, i, dr)
306 if (dr->aux)
308 free (dr->aux);
309 dr->aux = NULL;
312 free_data_refs (datarefs);
316 /* Function vectorize_loops.
318 Entry point to loop vectorization phase. */
320 unsigned
321 vectorize_loops (void)
323 unsigned int i;
324 unsigned int num_vectorized_loops = 0;
325 unsigned int vect_loops_num;
326 loop_iterator li;
327 struct loop *loop;
328 hash_table <simduid_to_vf> simduid_to_vf_htab;
329 hash_table <simd_array_to_simduid> simd_array_to_simduid_htab;
331 vect_loops_num = number_of_loops (cfun);
333 /* Bail out if there are no loops. */
334 if (vect_loops_num <= 1)
336 if (cfun->has_simduid_loops)
337 adjust_simduid_builtins (simduid_to_vf_htab);
338 return 0;
341 if (cfun->has_simduid_loops)
342 note_simd_array_uses (&simd_array_to_simduid_htab);
344 init_stmt_vec_info_vec ();
346 /* ----------- Analyze loops. ----------- */
348 /* If some loop was duplicated, it gets bigger number
349 than all previously defined loops. This fact allows us to run
350 only over initial loops skipping newly generated ones. */
351 FOR_EACH_LOOP (li, loop, 0)
352 if ((flag_tree_loop_vectorize && optimize_loop_nest_for_speed_p (loop))
353 || loop->force_vect)
355 loop_vec_info loop_vinfo;
356 vect_location = find_loop_location (loop);
357 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
358 && dump_enabled_p ())
359 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
360 LOC_FILE (vect_location), LOC_LINE (vect_location));
362 loop_vinfo = vect_analyze_loop (loop);
363 loop->aux = loop_vinfo;
365 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
366 continue;
368 if (!dbg_cnt (vect_loop))
369 break;
371 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
372 && dump_enabled_p ())
373 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
374 "loop vectorized\n");
375 vect_transform_loop (loop_vinfo);
376 num_vectorized_loops++;
377 /* Now that the loop has been vectorized, allow it to be unrolled
378 etc. */
379 loop->force_vect = false;
381 if (loop->simduid)
383 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
384 if (!simduid_to_vf_htab.is_created ())
385 simduid_to_vf_htab.create (15);
386 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
387 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
388 *simduid_to_vf_htab.find_slot (simduid_to_vf_data, INSERT)
389 = simduid_to_vf_data;
393 vect_location = UNKNOWN_LOC;
395 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
396 if (dump_enabled_p ()
397 || (num_vectorized_loops > 0 && dump_enabled_p ()))
398 dump_printf_loc (MSG_NOTE, vect_location,
399 "vectorized %u loops in function.\n",
400 num_vectorized_loops);
402 /* ----------- Finalize. ----------- */
404 for (i = 1; i < vect_loops_num; i++)
406 loop_vec_info loop_vinfo;
408 loop = get_loop (cfun, i);
409 if (!loop)
410 continue;
411 loop_vinfo = (loop_vec_info) loop->aux;
412 destroy_loop_vec_info (loop_vinfo, true);
413 loop->aux = NULL;
416 free_stmt_vec_info_vec ();
418 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */
419 if (cfun->has_simduid_loops)
420 adjust_simduid_builtins (simduid_to_vf_htab);
422 /* Shrink any "omp array simd" temporary arrays to the
423 actual vectorization factors. */
424 if (simd_array_to_simduid_htab.is_created ())
426 for (hash_table <simd_array_to_simduid>::iterator iter
427 = simd_array_to_simduid_htab.begin ();
428 iter != simd_array_to_simduid_htab.end (); ++iter)
429 if ((*iter).simduid != -1U)
431 tree decl = (*iter).decl;
432 int vf = 1;
433 if (simduid_to_vf_htab.is_created ())
435 simduid_to_vf *p = NULL, data;
436 data.simduid = (*iter).simduid;
437 p = simduid_to_vf_htab.find (&data);
438 if (p)
439 vf = p->vf;
441 tree atype
442 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
443 TREE_TYPE (decl) = atype;
444 relayout_decl (decl);
447 simd_array_to_simduid_htab.dispose ();
449 if (simduid_to_vf_htab.is_created ())
450 simduid_to_vf_htab.dispose ();
452 if (num_vectorized_loops > 0)
454 /* If we vectorized any loop only virtual SSA form needs to be updated.
455 ??? Also while we try hard to update loop-closed SSA form we fail
456 to properly do this in some corner-cases (see PR56286). */
457 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
458 return TODO_cleanup_cfg;
461 return 0;
465 /* Entry point to basic block SLP phase. */
467 static unsigned int
468 execute_vect_slp (void)
470 basic_block bb;
472 init_stmt_vec_info_vec ();
474 FOR_EACH_BB (bb)
476 vect_location = find_bb_location (bb);
478 if (vect_slp_analyze_bb (bb))
480 if (!dbg_cnt (vect_slp))
481 break;
483 vect_slp_transform_bb (bb);
484 if (dump_enabled_p ())
485 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
486 "basic block vectorized\n");
490 free_stmt_vec_info_vec ();
491 return 0;
494 static bool
495 gate_vect_slp (void)
497 return flag_tree_slp_vectorize != 0;
500 namespace {
502 const pass_data pass_data_slp_vectorize =
504 GIMPLE_PASS, /* type */
505 "slp", /* name */
506 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
507 true, /* has_gate */
508 true, /* has_execute */
509 TV_TREE_SLP_VECTORIZATION, /* tv_id */
510 ( PROP_ssa | PROP_cfg ), /* properties_required */
511 0, /* properties_provided */
512 0, /* properties_destroyed */
513 0, /* todo_flags_start */
514 ( TODO_verify_ssa | TODO_update_ssa
515 | TODO_verify_stmts ), /* todo_flags_finish */
518 class pass_slp_vectorize : public gimple_opt_pass
520 public:
521 pass_slp_vectorize (gcc::context *ctxt)
522 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
525 /* opt_pass methods: */
526 bool gate () { return gate_vect_slp (); }
527 unsigned int execute () { return execute_vect_slp (); }
529 }; // class pass_slp_vectorize
531 } // anon namespace
533 gimple_opt_pass *
534 make_pass_slp_vectorize (gcc::context *ctxt)
536 return new pass_slp_vectorize (ctxt);
540 /* Increase alignment of global arrays to improve vectorization potential.
541 TODO:
542 - Consider also structs that have an array field.
543 - Use ipa analysis to prune arrays that can't be vectorized?
544 This should involve global alignment analysis and in the future also
545 array padding. */
547 static unsigned int
548 increase_alignment (void)
550 struct varpool_node *vnode;
552 vect_location = UNKNOWN_LOC;
554 /* Increase the alignment of all global arrays for vectorization. */
555 FOR_EACH_DEFINED_VARIABLE (vnode)
557 tree vectype, decl = vnode->decl;
558 tree t;
559 unsigned int alignment;
561 t = TREE_TYPE (decl);
562 if (TREE_CODE (t) != ARRAY_TYPE)
563 continue;
564 vectype = get_vectype_for_scalar_type (strip_array_types (t));
565 if (!vectype)
566 continue;
567 alignment = TYPE_ALIGN (vectype);
568 if (DECL_ALIGN (decl) >= alignment)
569 continue;
571 if (vect_can_force_dr_alignment_p (decl, alignment))
573 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
574 DECL_USER_ALIGN (decl) = 1;
575 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
576 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
577 dump_printf (MSG_NOTE, "\n");
580 return 0;
584 static bool
585 gate_increase_alignment (void)
587 return flag_section_anchors && flag_tree_loop_vectorize;
591 namespace {
593 const pass_data pass_data_ipa_increase_alignment =
595 SIMPLE_IPA_PASS, /* type */
596 "increase_alignment", /* name */
597 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
598 true, /* has_gate */
599 true, /* has_execute */
600 TV_IPA_OPT, /* tv_id */
601 0, /* properties_required */
602 0, /* properties_provided */
603 0, /* properties_destroyed */
604 0, /* todo_flags_start */
605 0, /* todo_flags_finish */
608 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
610 public:
611 pass_ipa_increase_alignment (gcc::context *ctxt)
612 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
615 /* opt_pass methods: */
616 bool gate () { return gate_increase_alignment (); }
617 unsigned int execute () { return increase_alignment (); }
619 }; // class pass_ipa_increase_alignment
621 } // anon namespace
623 simple_ipa_opt_pass *
624 make_pass_ipa_increase_alignment (gcc::context *ctxt)
626 return new pass_ipa_increase_alignment (ctxt);