2 Copyright (C) 2003-2015 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
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
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
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
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:
44 loop_vect() loop_aware_slp() slp_vect()
47 tree-vect-loop.c tree-vect-slp.c
52 tree-vect-stmts.c tree-vect-data-refs.c
59 #include "coretypes.h"
69 #include "fold-const.h"
70 #include "stor-layout.h"
71 #include "tree-pretty-print.h"
73 #include "hard-reg-set.h"
76 #include "dominance.h"
78 #include "basic-block.h"
79 #include "tree-ssa-alias.h"
80 #include "internal-fn.h"
81 #include "gimple-expr.h"
84 #include "gimple-iterator.h"
85 #include "gimple-walk.h"
86 #include "gimple-ssa.h"
88 #include "plugin-api.h"
91 #include "tree-phinodes.h"
92 #include "ssa-iterators.h"
93 #include "tree-ssa-loop-manip.h"
96 #include "tree-vectorizer.h"
97 #include "tree-pass.h"
98 #include "tree-ssa-propagate.h"
100 #include "gimple-fold.h"
101 #include "tree-scalar-evolution.h"
104 /* Loop or bb location. */
105 source_location vect_location
;
107 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
108 vec
<vec_void_p
> stmt_vec_info_vec
;
110 /* For mapping simduid to vectorization factor. */
112 struct simduid_to_vf
: typed_free_remove
<simduid_to_vf
>
114 unsigned int simduid
;
117 /* hash_table support. */
118 typedef simduid_to_vf
*value_type
;
119 typedef simduid_to_vf
*compare_type
;
120 static inline hashval_t
hash (const simduid_to_vf
*);
121 static inline int equal (const simduid_to_vf
*, const simduid_to_vf
*);
125 simduid_to_vf::hash (const simduid_to_vf
*p
)
131 simduid_to_vf::equal (const simduid_to_vf
*p1
, const simduid_to_vf
*p2
)
133 return p1
->simduid
== p2
->simduid
;
136 /* This hash maps the OMP simd array to the corresponding simduid used
137 to index into it. Like thus,
139 _7 = GOMP_SIMD_LANE (simduid.0)
145 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
148 struct simd_array_to_simduid
: typed_free_remove
<simd_array_to_simduid
>
151 unsigned int simduid
;
153 /* hash_table support. */
154 typedef simd_array_to_simduid
*value_type
;
155 typedef simd_array_to_simduid
*compare_type
;
156 static inline hashval_t
hash (const simd_array_to_simduid
*);
157 static inline int equal (const simd_array_to_simduid
*,
158 const simd_array_to_simduid
*);
162 simd_array_to_simduid::hash (const simd_array_to_simduid
*p
)
164 return DECL_UID (p
->decl
);
168 simd_array_to_simduid::equal (const simd_array_to_simduid
*p1
,
169 const simd_array_to_simduid
*p2
)
171 return p1
->decl
== p2
->decl
;
174 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
175 into their corresponding constants. */
178 adjust_simduid_builtins (hash_table
<simduid_to_vf
> **htab
)
182 FOR_EACH_BB_FN (bb
, cfun
)
184 gimple_stmt_iterator i
;
186 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
189 enum internal_fn ifn
;
190 gimple stmt
= gsi_stmt (i
);
192 if (!is_gimple_call (stmt
)
193 || !gimple_call_internal_p (stmt
))
195 ifn
= gimple_call_internal_fn (stmt
);
198 case IFN_GOMP_SIMD_LANE
:
199 case IFN_GOMP_SIMD_VF
:
200 case IFN_GOMP_SIMD_LAST_LANE
:
205 tree arg
= gimple_call_arg (stmt
, 0);
206 gcc_assert (arg
!= NULL_TREE
);
207 gcc_assert (TREE_CODE (arg
) == SSA_NAME
);
208 simduid_to_vf
*p
= NULL
, data
;
209 data
.simduid
= DECL_UID (SSA_NAME_VAR (arg
));
211 p
= (*htab
)->find (&data
);
216 case IFN_GOMP_SIMD_VF
:
217 t
= build_int_cst (unsigned_type_node
, vf
);
219 case IFN_GOMP_SIMD_LANE
:
220 t
= build_int_cst (unsigned_type_node
, 0);
222 case IFN_GOMP_SIMD_LAST_LANE
:
223 t
= gimple_call_arg (stmt
, 1);
228 update_call_from_tree (&i
, t
);
233 /* Helper structure for note_simd_array_uses. */
235 struct note_simd_array_uses_struct
237 hash_table
<simd_array_to_simduid
> **htab
;
238 unsigned int simduid
;
241 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
244 note_simd_array_uses_cb (tree
*tp
, int *walk_subtrees
, void *data
)
246 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
247 struct note_simd_array_uses_struct
*ns
248 = (struct note_simd_array_uses_struct
*) wi
->info
;
253 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp
))
254 && DECL_CONTEXT (*tp
) == current_function_decl
)
256 simd_array_to_simduid data
;
258 *ns
->htab
= new hash_table
<simd_array_to_simduid
> (15);
260 data
.simduid
= ns
->simduid
;
261 simd_array_to_simduid
**slot
= (*ns
->htab
)->find_slot (&data
, INSERT
);
264 simd_array_to_simduid
*p
= XNEW (simd_array_to_simduid
);
268 else if ((*slot
)->simduid
!= ns
->simduid
)
269 (*slot
)->simduid
= -1U;
275 /* Find "omp simd array" temporaries and map them to corresponding
279 note_simd_array_uses (hash_table
<simd_array_to_simduid
> **htab
)
282 gimple_stmt_iterator gsi
;
283 struct walk_stmt_info wi
;
284 struct note_simd_array_uses_struct ns
;
286 memset (&wi
, 0, sizeof (wi
));
290 FOR_EACH_BB_FN (bb
, cfun
)
291 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
293 gimple stmt
= gsi_stmt (gsi
);
294 if (!is_gimple_call (stmt
) || !gimple_call_internal_p (stmt
))
296 switch (gimple_call_internal_fn (stmt
))
298 case IFN_GOMP_SIMD_LANE
:
299 case IFN_GOMP_SIMD_VF
:
300 case IFN_GOMP_SIMD_LAST_LANE
:
305 tree lhs
= gimple_call_lhs (stmt
);
306 if (lhs
== NULL_TREE
)
308 imm_use_iterator use_iter
;
310 ns
.simduid
= DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt
, 0)));
311 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, lhs
)
312 if (!is_gimple_debug (use_stmt
))
313 walk_gimple_op (use_stmt
, note_simd_array_uses_cb
, &wi
);
317 /* A helper function to free data refs. */
320 vect_destroy_datarefs (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
322 vec
<data_reference_p
> datarefs
;
323 struct data_reference
*dr
;
327 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
329 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
331 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
338 free_data_refs (datarefs
);
342 /* If LOOP has been versioned during ifcvt, return the internal call
346 vect_loop_vectorized_call (struct loop
*loop
)
348 basic_block bb
= loop_preheader_edge (loop
)->src
;
355 if (!single_pred_p (bb
))
357 bb
= single_pred (bb
);
360 if (g
&& gimple_code (g
) == GIMPLE_COND
)
362 gimple_stmt_iterator gsi
= gsi_for_stmt (g
);
364 if (!gsi_end_p (gsi
))
367 if (is_gimple_call (g
)
368 && gimple_call_internal_p (g
)
369 && gimple_call_internal_fn (g
) == IFN_LOOP_VECTORIZED
370 && (tree_to_shwi (gimple_call_arg (g
, 0)) == loop
->num
371 || tree_to_shwi (gimple_call_arg (g
, 1)) == loop
->num
))
378 /* Fold LOOP_VECTORIZED internal call G to VALUE and
379 update any immediate uses of it's LHS. */
382 fold_loop_vectorized_call (gimple g
, tree value
)
384 tree lhs
= gimple_call_lhs (g
);
386 imm_use_iterator iter
;
388 gimple_stmt_iterator gsi
= gsi_for_stmt (g
);
390 update_call_from_tree (&gsi
, value
);
391 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
393 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
394 SET_USE (use_p
, value
);
395 update_stmt (use_stmt
);
398 /* Set the uids of all the statements in basic blocks inside loop
399 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
400 call guarding the loop which has been if converted. */
402 set_uid_loop_bbs (loop_vec_info loop_vinfo
, gimple loop_vectorized_call
)
404 tree arg
= gimple_call_arg (loop_vectorized_call
, 1);
407 struct loop
*scalar_loop
= get_loop (cfun
, tree_to_shwi (arg
));
409 LOOP_VINFO_SCALAR_LOOP (loop_vinfo
) = scalar_loop
;
410 gcc_checking_assert (vect_loop_vectorized_call
411 (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
412 == loop_vectorized_call
);
413 bbs
= get_loop_body (scalar_loop
);
414 for (i
= 0; i
< scalar_loop
->num_nodes
; i
++)
416 basic_block bb
= bbs
[i
];
417 gimple_stmt_iterator gsi
;
418 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
420 gimple phi
= gsi_stmt (gsi
);
421 gimple_set_uid (phi
, 0);
423 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
425 gimple stmt
= gsi_stmt (gsi
);
426 gimple_set_uid (stmt
, 0);
432 /* Function vectorize_loops.
434 Entry point to loop vectorization phase. */
437 vectorize_loops (void)
440 unsigned int num_vectorized_loops
= 0;
441 unsigned int vect_loops_num
;
443 hash_table
<simduid_to_vf
> *simduid_to_vf_htab
= NULL
;
444 hash_table
<simd_array_to_simduid
> *simd_array_to_simduid_htab
= NULL
;
445 bool any_ifcvt_loops
= false;
448 vect_loops_num
= number_of_loops (cfun
);
450 /* Bail out if there are no loops. */
451 if (vect_loops_num
<= 1)
453 if (cfun
->has_simduid_loops
)
454 adjust_simduid_builtins (&simduid_to_vf_htab
);
458 if (cfun
->has_simduid_loops
)
459 note_simd_array_uses (&simd_array_to_simduid_htab
);
461 init_stmt_vec_info_vec ();
463 /* ----------- Analyze loops. ----------- */
465 /* If some loop was duplicated, it gets bigger number
466 than all previously defined loops. This fact allows us to run
467 only over initial loops skipping newly generated ones. */
468 FOR_EACH_LOOP (loop
, 0)
469 if (loop
->dont_vectorize
)
470 any_ifcvt_loops
= true;
471 else if ((flag_tree_loop_vectorize
472 && optimize_loop_nest_for_speed_p (loop
))
473 || loop
->force_vectorize
)
475 loop_vec_info loop_vinfo
;
476 vect_location
= find_loop_location (loop
);
477 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOCATION
478 && dump_enabled_p ())
479 dump_printf (MSG_NOTE
, "\nAnalyzing loop at %s:%d\n",
480 LOCATION_FILE (vect_location
),
481 LOCATION_LINE (vect_location
));
483 loop_vinfo
= vect_analyze_loop (loop
);
484 loop
->aux
= loop_vinfo
;
486 if (!loop_vinfo
|| !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
))
489 if (!dbg_cnt (vect_loop
))
492 gimple loop_vectorized_call
= vect_loop_vectorized_call (loop
);
493 if (loop_vectorized_call
)
494 set_uid_loop_bbs (loop_vinfo
, loop_vectorized_call
);
495 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOCATION
496 && dump_enabled_p ())
497 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
498 "loop vectorized\n");
499 vect_transform_loop (loop_vinfo
);
500 num_vectorized_loops
++;
501 /* Now that the loop has been vectorized, allow it to be unrolled
503 loop
->force_vectorize
= false;
507 simduid_to_vf
*simduid_to_vf_data
= XNEW (simduid_to_vf
);
508 if (!simduid_to_vf_htab
)
509 simduid_to_vf_htab
= new hash_table
<simduid_to_vf
> (15);
510 simduid_to_vf_data
->simduid
= DECL_UID (loop
->simduid
);
511 simduid_to_vf_data
->vf
= loop_vinfo
->vectorization_factor
;
512 *simduid_to_vf_htab
->find_slot (simduid_to_vf_data
, INSERT
)
513 = simduid_to_vf_data
;
516 if (loop_vectorized_call
)
518 fold_loop_vectorized_call (loop_vectorized_call
, boolean_true_node
);
519 ret
|= TODO_cleanup_cfg
;
523 vect_location
= UNKNOWN_LOCATION
;
525 statistics_counter_event (cfun
, "Vectorized loops", num_vectorized_loops
);
526 if (dump_enabled_p ()
527 || (num_vectorized_loops
> 0 && dump_enabled_p ()))
528 dump_printf_loc (MSG_NOTE
, vect_location
,
529 "vectorized %u loops in function.\n",
530 num_vectorized_loops
);
532 /* ----------- Finalize. ----------- */
535 for (i
= 1; i
< vect_loops_num
; i
++)
537 loop
= get_loop (cfun
, i
);
538 if (loop
&& loop
->dont_vectorize
)
540 gimple g
= vect_loop_vectorized_call (loop
);
543 fold_loop_vectorized_call (g
, boolean_false_node
);
544 ret
|= TODO_cleanup_cfg
;
549 for (i
= 1; i
< vect_loops_num
; i
++)
551 loop_vec_info loop_vinfo
;
553 loop
= get_loop (cfun
, i
);
556 loop_vinfo
= (loop_vec_info
) loop
->aux
;
557 destroy_loop_vec_info (loop_vinfo
, true);
561 free_stmt_vec_info_vec ();
563 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */
564 if (cfun
->has_simduid_loops
)
565 adjust_simduid_builtins (&simduid_to_vf_htab
);
567 /* Shrink any "omp array simd" temporary arrays to the
568 actual vectorization factors. */
569 if (simd_array_to_simduid_htab
)
571 for (hash_table
<simd_array_to_simduid
>::iterator iter
572 = simd_array_to_simduid_htab
->begin ();
573 iter
!= simd_array_to_simduid_htab
->end (); ++iter
)
574 if ((*iter
)->simduid
!= -1U)
576 tree decl
= (*iter
)->decl
;
578 if (simduid_to_vf_htab
)
580 simduid_to_vf
*p
= NULL
, data
;
581 data
.simduid
= (*iter
)->simduid
;
582 p
= simduid_to_vf_htab
->find (&data
);
587 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl
)), vf
);
588 TREE_TYPE (decl
) = atype
;
589 relayout_decl (decl
);
592 delete simd_array_to_simduid_htab
;
594 delete simduid_to_vf_htab
;
595 simduid_to_vf_htab
= NULL
;
597 if (num_vectorized_loops
> 0)
599 /* If we vectorized any loop only virtual SSA form needs to be updated.
600 ??? Also while we try hard to update loop-closed SSA form we fail
601 to properly do this in some corner-cases (see PR56286). */
602 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa_only_virtuals
);
603 return TODO_cleanup_cfg
;
610 /* Entry point to basic block SLP phase. */
614 const pass_data pass_data_slp_vectorize
=
616 GIMPLE_PASS
, /* type */
618 OPTGROUP_LOOP
| OPTGROUP_VEC
, /* optinfo_flags */
619 TV_TREE_SLP_VECTORIZATION
, /* tv_id */
620 ( PROP_ssa
| PROP_cfg
), /* properties_required */
621 0, /* properties_provided */
622 0, /* properties_destroyed */
623 0, /* todo_flags_start */
624 TODO_update_ssa
, /* todo_flags_finish */
627 class pass_slp_vectorize
: public gimple_opt_pass
630 pass_slp_vectorize (gcc::context
*ctxt
)
631 : gimple_opt_pass (pass_data_slp_vectorize
, ctxt
)
634 /* opt_pass methods: */
635 opt_pass
* clone () { return new pass_slp_vectorize (m_ctxt
); }
636 virtual bool gate (function
*) { return flag_tree_slp_vectorize
!= 0; }
637 virtual unsigned int execute (function
*);
639 }; // class pass_slp_vectorize
642 pass_slp_vectorize::execute (function
*fun
)
646 bool in_loop_pipeline
= scev_initialized_p ();
647 if (!in_loop_pipeline
)
649 loop_optimizer_init (LOOPS_NORMAL
);
653 init_stmt_vec_info_vec ();
655 FOR_EACH_BB_FN (bb
, fun
)
657 vect_location
= find_bb_location (bb
);
659 if (vect_slp_analyze_bb (bb
))
661 if (!dbg_cnt (vect_slp
))
664 vect_slp_transform_bb (bb
);
665 if (dump_enabled_p ())
666 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
667 "basic block vectorized\n");
671 free_stmt_vec_info_vec ();
673 if (!in_loop_pipeline
)
676 loop_optimizer_finalize ();
685 make_pass_slp_vectorize (gcc::context
*ctxt
)
687 return new pass_slp_vectorize (ctxt
);
691 /* Increase alignment of global arrays to improve vectorization potential.
693 - Consider also structs that have an array field.
694 - Use ipa analysis to prune arrays that can't be vectorized?
695 This should involve global alignment analysis and in the future also
699 increase_alignment (void)
703 vect_location
= UNKNOWN_LOCATION
;
705 /* Increase the alignment of all global arrays for vectorization. */
706 FOR_EACH_DEFINED_VARIABLE (vnode
)
708 tree vectype
, decl
= vnode
->decl
;
710 unsigned int alignment
;
712 t
= TREE_TYPE (decl
);
713 if (TREE_CODE (t
) != ARRAY_TYPE
)
715 vectype
= get_vectype_for_scalar_type (strip_array_types (t
));
718 alignment
= TYPE_ALIGN (vectype
);
719 if (DECL_ALIGN (decl
) >= alignment
)
722 if (vect_can_force_dr_alignment_p (decl
, alignment
))
724 vnode
->increase_alignment (TYPE_ALIGN (vectype
));
725 dump_printf (MSG_NOTE
, "Increasing alignment of decl: ");
726 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, decl
);
727 dump_printf (MSG_NOTE
, "\n");
736 const pass_data pass_data_ipa_increase_alignment
=
738 SIMPLE_IPA_PASS
, /* type */
739 "increase_alignment", /* name */
740 OPTGROUP_LOOP
| OPTGROUP_VEC
, /* optinfo_flags */
741 TV_IPA_OPT
, /* tv_id */
742 0, /* properties_required */
743 0, /* properties_provided */
744 0, /* properties_destroyed */
745 0, /* todo_flags_start */
746 0, /* todo_flags_finish */
749 class pass_ipa_increase_alignment
: public simple_ipa_opt_pass
752 pass_ipa_increase_alignment (gcc::context
*ctxt
)
753 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment
, ctxt
)
756 /* opt_pass methods: */
757 virtual bool gate (function
*)
759 return flag_section_anchors
&& flag_tree_loop_vectorize
;
762 virtual unsigned int execute (function
*) { return increase_alignment (); }
764 }; // class pass_ipa_increase_alignment
768 simple_ipa_opt_pass
*
769 make_pass_ipa_increase_alignment (gcc::context
*ctxt
)
771 return new pass_ipa_increase_alignment (ctxt
);