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"
65 #include "hard-reg-set.h"
68 #include "fold-const.h"
69 #include "stor-layout.h"
70 #include "tree-pretty-print.h"
71 #include "internal-fn.h"
72 #include "gimple-iterator.h"
73 #include "gimple-walk.h"
75 #include "tree-ssa-loop-manip.h"
78 #include "tree-vectorizer.h"
79 #include "tree-pass.h"
80 #include "tree-ssa-propagate.h"
82 #include "gimple-fold.h"
83 #include "tree-scalar-evolution.h"
86 /* Loop or bb location. */
87 source_location vect_location
;
89 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
90 vec
<vec_void_p
> stmt_vec_info_vec
;
92 /* For mapping simduid to vectorization factor. */
94 struct simduid_to_vf
: free_ptr_hash
<simduid_to_vf
>
99 /* hash_table support. */
100 static inline hashval_t
hash (const simduid_to_vf
*);
101 static inline int equal (const simduid_to_vf
*, const simduid_to_vf
*);
105 simduid_to_vf::hash (const simduid_to_vf
*p
)
111 simduid_to_vf::equal (const simduid_to_vf
*p1
, const simduid_to_vf
*p2
)
113 return p1
->simduid
== p2
->simduid
;
116 /* This hash maps the OMP simd array to the corresponding simduid used
117 to index into it. Like thus,
119 _7 = GOMP_SIMD_LANE (simduid.0)
125 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
128 struct simd_array_to_simduid
: free_ptr_hash
<simd_array_to_simduid
>
131 unsigned int simduid
;
133 /* hash_table support. */
134 static inline hashval_t
hash (const simd_array_to_simduid
*);
135 static inline int equal (const simd_array_to_simduid
*,
136 const simd_array_to_simduid
*);
140 simd_array_to_simduid::hash (const simd_array_to_simduid
*p
)
142 return DECL_UID (p
->decl
);
146 simd_array_to_simduid::equal (const simd_array_to_simduid
*p1
,
147 const simd_array_to_simduid
*p2
)
149 return p1
->decl
== p2
->decl
;
152 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE
153 into their corresponding constants. */
156 adjust_simduid_builtins (hash_table
<simduid_to_vf
> *htab
)
160 FOR_EACH_BB_FN (bb
, cfun
)
162 gimple_stmt_iterator i
;
164 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
167 enum internal_fn ifn
;
168 gimple stmt
= gsi_stmt (i
);
170 if (!is_gimple_call (stmt
)
171 || !gimple_call_internal_p (stmt
))
173 ifn
= gimple_call_internal_fn (stmt
);
176 case IFN_GOMP_SIMD_LANE
:
177 case IFN_GOMP_SIMD_VF
:
178 case IFN_GOMP_SIMD_LAST_LANE
:
183 tree arg
= gimple_call_arg (stmt
, 0);
184 gcc_assert (arg
!= NULL_TREE
);
185 gcc_assert (TREE_CODE (arg
) == SSA_NAME
);
186 simduid_to_vf
*p
= NULL
, data
;
187 data
.simduid
= DECL_UID (SSA_NAME_VAR (arg
));
190 p
= htab
->find (&data
);
196 case IFN_GOMP_SIMD_VF
:
197 t
= build_int_cst (unsigned_type_node
, vf
);
199 case IFN_GOMP_SIMD_LANE
:
200 t
= build_int_cst (unsigned_type_node
, 0);
202 case IFN_GOMP_SIMD_LAST_LANE
:
203 t
= gimple_call_arg (stmt
, 1);
208 update_call_from_tree (&i
, t
);
213 /* Helper structure for note_simd_array_uses. */
215 struct note_simd_array_uses_struct
217 hash_table
<simd_array_to_simduid
> **htab
;
218 unsigned int simduid
;
221 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
224 note_simd_array_uses_cb (tree
*tp
, int *walk_subtrees
, void *data
)
226 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
227 struct note_simd_array_uses_struct
*ns
228 = (struct note_simd_array_uses_struct
*) wi
->info
;
233 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp
))
234 && DECL_CONTEXT (*tp
) == current_function_decl
)
236 simd_array_to_simduid data
;
238 *ns
->htab
= new hash_table
<simd_array_to_simduid
> (15);
240 data
.simduid
= ns
->simduid
;
241 simd_array_to_simduid
**slot
= (*ns
->htab
)->find_slot (&data
, INSERT
);
244 simd_array_to_simduid
*p
= XNEW (simd_array_to_simduid
);
248 else if ((*slot
)->simduid
!= ns
->simduid
)
249 (*slot
)->simduid
= -1U;
255 /* Find "omp simd array" temporaries and map them to corresponding
259 note_simd_array_uses (hash_table
<simd_array_to_simduid
> **htab
)
262 gimple_stmt_iterator gsi
;
263 struct walk_stmt_info wi
;
264 struct note_simd_array_uses_struct ns
;
266 memset (&wi
, 0, sizeof (wi
));
270 FOR_EACH_BB_FN (bb
, cfun
)
271 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
273 gimple stmt
= gsi_stmt (gsi
);
274 if (!is_gimple_call (stmt
) || !gimple_call_internal_p (stmt
))
276 switch (gimple_call_internal_fn (stmt
))
278 case IFN_GOMP_SIMD_LANE
:
279 case IFN_GOMP_SIMD_VF
:
280 case IFN_GOMP_SIMD_LAST_LANE
:
285 tree lhs
= gimple_call_lhs (stmt
);
286 if (lhs
== NULL_TREE
)
288 imm_use_iterator use_iter
;
290 ns
.simduid
= DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt
, 0)));
291 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, lhs
)
292 if (!is_gimple_debug (use_stmt
))
293 walk_gimple_op (use_stmt
, note_simd_array_uses_cb
, &wi
);
297 /* Shrink arrays with "omp simd array" attribute to the corresponding
298 vectorization factor. */
302 (hash_table
<simd_array_to_simduid
> *simd_array_to_simduid_htab
,
303 hash_table
<simduid_to_vf
> *simduid_to_vf_htab
)
305 for (hash_table
<simd_array_to_simduid
>::iterator iter
306 = simd_array_to_simduid_htab
->begin ();
307 iter
!= simd_array_to_simduid_htab
->end (); ++iter
)
308 if ((*iter
)->simduid
!= -1U)
310 tree decl
= (*iter
)->decl
;
312 if (simduid_to_vf_htab
)
314 simduid_to_vf
*p
= NULL
, data
;
315 data
.simduid
= (*iter
)->simduid
;
316 p
= simduid_to_vf_htab
->find (&data
);
321 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl
)), vf
);
322 TREE_TYPE (decl
) = atype
;
323 relayout_decl (decl
);
326 delete simd_array_to_simduid_htab
;
329 /* A helper function to free data refs. */
332 vect_destroy_datarefs (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
334 vec
<data_reference_p
> datarefs
;
335 struct data_reference
*dr
;
339 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
341 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
343 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
350 free_data_refs (datarefs
);
354 /* If LOOP has been versioned during ifcvt, return the internal call
358 vect_loop_vectorized_call (struct loop
*loop
)
360 basic_block bb
= loop_preheader_edge (loop
)->src
;
367 if (!single_pred_p (bb
))
369 bb
= single_pred (bb
);
372 if (g
&& gimple_code (g
) == GIMPLE_COND
)
374 gimple_stmt_iterator gsi
= gsi_for_stmt (g
);
376 if (!gsi_end_p (gsi
))
379 if (is_gimple_call (g
)
380 && gimple_call_internal_p (g
)
381 && gimple_call_internal_fn (g
) == IFN_LOOP_VECTORIZED
382 && (tree_to_shwi (gimple_call_arg (g
, 0)) == loop
->num
383 || tree_to_shwi (gimple_call_arg (g
, 1)) == loop
->num
))
390 /* Fold LOOP_VECTORIZED internal call G to VALUE and
391 update any immediate uses of it's LHS. */
394 fold_loop_vectorized_call (gimple g
, tree value
)
396 tree lhs
= gimple_call_lhs (g
);
398 imm_use_iterator iter
;
400 gimple_stmt_iterator gsi
= gsi_for_stmt (g
);
402 update_call_from_tree (&gsi
, value
);
403 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
405 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
406 SET_USE (use_p
, value
);
407 update_stmt (use_stmt
);
410 /* Set the uids of all the statements in basic blocks inside loop
411 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
412 call guarding the loop which has been if converted. */
414 set_uid_loop_bbs (loop_vec_info loop_vinfo
, gimple loop_vectorized_call
)
416 tree arg
= gimple_call_arg (loop_vectorized_call
, 1);
419 struct loop
*scalar_loop
= get_loop (cfun
, tree_to_shwi (arg
));
421 LOOP_VINFO_SCALAR_LOOP (loop_vinfo
) = scalar_loop
;
422 gcc_checking_assert (vect_loop_vectorized_call
423 (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
424 == loop_vectorized_call
);
425 bbs
= get_loop_body (scalar_loop
);
426 for (i
= 0; i
< scalar_loop
->num_nodes
; i
++)
428 basic_block bb
= bbs
[i
];
429 gimple_stmt_iterator gsi
;
430 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
432 gimple phi
= gsi_stmt (gsi
);
433 gimple_set_uid (phi
, 0);
435 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
437 gimple stmt
= gsi_stmt (gsi
);
438 gimple_set_uid (stmt
, 0);
444 /* Function vectorize_loops.
446 Entry point to loop vectorization phase. */
449 vectorize_loops (void)
452 unsigned int num_vectorized_loops
= 0;
453 unsigned int vect_loops_num
;
455 hash_table
<simduid_to_vf
> *simduid_to_vf_htab
= NULL
;
456 hash_table
<simd_array_to_simduid
> *simd_array_to_simduid_htab
= NULL
;
457 bool any_ifcvt_loops
= false;
460 vect_loops_num
= number_of_loops (cfun
);
462 /* Bail out if there are no loops. */
463 if (vect_loops_num
<= 1)
466 if (cfun
->has_simduid_loops
)
467 note_simd_array_uses (&simd_array_to_simduid_htab
);
469 init_stmt_vec_info_vec ();
471 /* ----------- Analyze loops. ----------- */
473 /* If some loop was duplicated, it gets bigger number
474 than all previously defined loops. This fact allows us to run
475 only over initial loops skipping newly generated ones. */
476 FOR_EACH_LOOP (loop
, 0)
477 if (loop
->dont_vectorize
)
478 any_ifcvt_loops
= true;
479 else if ((flag_tree_loop_vectorize
480 && optimize_loop_nest_for_speed_p (loop
))
481 || loop
->force_vectorize
)
483 loop_vec_info loop_vinfo
;
484 vect_location
= find_loop_location (loop
);
485 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOCATION
486 && dump_enabled_p ())
487 dump_printf (MSG_NOTE
, "\nAnalyzing loop at %s:%d\n",
488 LOCATION_FILE (vect_location
),
489 LOCATION_LINE (vect_location
));
491 loop_vinfo
= vect_analyze_loop (loop
);
492 loop
->aux
= loop_vinfo
;
494 if (!loop_vinfo
|| !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
))
497 if (!dbg_cnt (vect_loop
))
500 gimple loop_vectorized_call
= vect_loop_vectorized_call (loop
);
501 if (loop_vectorized_call
)
502 set_uid_loop_bbs (loop_vinfo
, loop_vectorized_call
);
503 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOCATION
504 && dump_enabled_p ())
505 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
506 "loop vectorized\n");
507 vect_transform_loop (loop_vinfo
);
508 num_vectorized_loops
++;
509 /* Now that the loop has been vectorized, allow it to be unrolled
511 loop
->force_vectorize
= false;
515 simduid_to_vf
*simduid_to_vf_data
= XNEW (simduid_to_vf
);
516 if (!simduid_to_vf_htab
)
517 simduid_to_vf_htab
= new hash_table
<simduid_to_vf
> (15);
518 simduid_to_vf_data
->simduid
= DECL_UID (loop
->simduid
);
519 simduid_to_vf_data
->vf
= loop_vinfo
->vectorization_factor
;
520 *simduid_to_vf_htab
->find_slot (simduid_to_vf_data
, INSERT
)
521 = simduid_to_vf_data
;
524 if (loop_vectorized_call
)
526 fold_loop_vectorized_call (loop_vectorized_call
, boolean_true_node
);
527 ret
|= TODO_cleanup_cfg
;
531 vect_location
= UNKNOWN_LOCATION
;
533 statistics_counter_event (cfun
, "Vectorized loops", num_vectorized_loops
);
534 if (dump_enabled_p ()
535 || (num_vectorized_loops
> 0 && dump_enabled_p ()))
536 dump_printf_loc (MSG_NOTE
, vect_location
,
537 "vectorized %u loops in function.\n",
538 num_vectorized_loops
);
540 /* ----------- Finalize. ----------- */
543 for (i
= 1; i
< vect_loops_num
; i
++)
545 loop
= get_loop (cfun
, i
);
546 if (loop
&& loop
->dont_vectorize
)
548 gimple g
= vect_loop_vectorized_call (loop
);
551 fold_loop_vectorized_call (g
, boolean_false_node
);
552 ret
|= TODO_cleanup_cfg
;
557 for (i
= 1; i
< vect_loops_num
; i
++)
559 loop_vec_info loop_vinfo
;
561 loop
= get_loop (cfun
, i
);
564 loop_vinfo
= (loop_vec_info
) loop
->aux
;
565 destroy_loop_vec_info (loop_vinfo
, true);
569 free_stmt_vec_info_vec ();
571 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */
572 if (cfun
->has_simduid_loops
)
573 adjust_simduid_builtins (simduid_to_vf_htab
);
575 /* Shrink any "omp array simd" temporary arrays to the
576 actual vectorization factors. */
577 if (simd_array_to_simduid_htab
)
578 shrink_simd_arrays (simd_array_to_simduid_htab
, simduid_to_vf_htab
);
579 delete simduid_to_vf_htab
;
580 cfun
->has_simduid_loops
= false;
582 if (num_vectorized_loops
> 0)
584 /* If we vectorized any loop only virtual SSA form needs to be updated.
585 ??? Also while we try hard to update loop-closed SSA form we fail
586 to properly do this in some corner-cases (see PR56286). */
587 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa_only_virtuals
);
588 return TODO_cleanup_cfg
;
595 /* Entry point to the simduid cleanup pass. */
599 const pass_data pass_data_simduid_cleanup
=
601 GIMPLE_PASS
, /* type */
602 "simduid", /* name */
603 OPTGROUP_NONE
, /* optinfo_flags */
605 ( PROP_ssa
| PROP_cfg
), /* properties_required */
606 0, /* properties_provided */
607 0, /* properties_destroyed */
608 0, /* todo_flags_start */
609 0, /* todo_flags_finish */
612 class pass_simduid_cleanup
: public gimple_opt_pass
615 pass_simduid_cleanup (gcc::context
*ctxt
)
616 : gimple_opt_pass (pass_data_simduid_cleanup
, ctxt
)
619 /* opt_pass methods: */
620 opt_pass
* clone () { return new pass_simduid_cleanup (m_ctxt
); }
621 virtual bool gate (function
*fun
) { return fun
->has_simduid_loops
; }
622 virtual unsigned int execute (function
*);
624 }; // class pass_simduid_cleanup
627 pass_simduid_cleanup::execute (function
*fun
)
629 hash_table
<simd_array_to_simduid
> *simd_array_to_simduid_htab
= NULL
;
631 note_simd_array_uses (&simd_array_to_simduid_htab
);
633 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */
634 adjust_simduid_builtins (NULL
);
636 /* Shrink any "omp array simd" temporary arrays to the
637 actual vectorization factors. */
638 if (simd_array_to_simduid_htab
)
639 shrink_simd_arrays (simd_array_to_simduid_htab
, NULL
);
640 fun
->has_simduid_loops
= false;
647 make_pass_simduid_cleanup (gcc::context
*ctxt
)
649 return new pass_simduid_cleanup (ctxt
);
653 /* Entry point to basic block SLP phase. */
657 const pass_data pass_data_slp_vectorize
=
659 GIMPLE_PASS
, /* type */
661 OPTGROUP_LOOP
| OPTGROUP_VEC
, /* optinfo_flags */
662 TV_TREE_SLP_VECTORIZATION
, /* tv_id */
663 ( PROP_ssa
| PROP_cfg
), /* properties_required */
664 0, /* properties_provided */
665 0, /* properties_destroyed */
666 0, /* todo_flags_start */
667 TODO_update_ssa
, /* todo_flags_finish */
670 class pass_slp_vectorize
: public gimple_opt_pass
673 pass_slp_vectorize (gcc::context
*ctxt
)
674 : gimple_opt_pass (pass_data_slp_vectorize
, ctxt
)
677 /* opt_pass methods: */
678 opt_pass
* clone () { return new pass_slp_vectorize (m_ctxt
); }
679 virtual bool gate (function
*) { return flag_tree_slp_vectorize
!= 0; }
680 virtual unsigned int execute (function
*);
682 }; // class pass_slp_vectorize
685 pass_slp_vectorize::execute (function
*fun
)
689 bool in_loop_pipeline
= scev_initialized_p ();
690 if (!in_loop_pipeline
)
692 loop_optimizer_init (LOOPS_NORMAL
);
696 init_stmt_vec_info_vec ();
698 FOR_EACH_BB_FN (bb
, fun
)
700 vect_location
= find_bb_location (bb
);
702 if (vect_slp_analyze_bb (bb
))
704 if (!dbg_cnt (vect_slp
))
707 vect_slp_transform_bb (bb
);
708 if (dump_enabled_p ())
709 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
710 "basic block vectorized\n");
714 free_stmt_vec_info_vec ();
716 if (!in_loop_pipeline
)
719 loop_optimizer_finalize ();
728 make_pass_slp_vectorize (gcc::context
*ctxt
)
730 return new pass_slp_vectorize (ctxt
);
734 /* Increase alignment of global arrays to improve vectorization potential.
736 - Consider also structs that have an array field.
737 - Use ipa analysis to prune arrays that can't be vectorized?
738 This should involve global alignment analysis and in the future also
742 increase_alignment (void)
746 vect_location
= UNKNOWN_LOCATION
;
748 /* Increase the alignment of all global arrays for vectorization. */
749 FOR_EACH_DEFINED_VARIABLE (vnode
)
751 tree vectype
, decl
= vnode
->decl
;
753 unsigned int alignment
;
755 t
= TREE_TYPE (decl
);
756 if (TREE_CODE (t
) != ARRAY_TYPE
)
758 vectype
= get_vectype_for_scalar_type (strip_array_types (t
));
761 alignment
= TYPE_ALIGN (vectype
);
762 if (DECL_ALIGN (decl
) >= alignment
)
765 if (vect_can_force_dr_alignment_p (decl
, alignment
))
767 vnode
->increase_alignment (TYPE_ALIGN (vectype
));
768 dump_printf (MSG_NOTE
, "Increasing alignment of decl: ");
769 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, decl
);
770 dump_printf (MSG_NOTE
, "\n");
779 const pass_data pass_data_ipa_increase_alignment
=
781 SIMPLE_IPA_PASS
, /* type */
782 "increase_alignment", /* name */
783 OPTGROUP_LOOP
| OPTGROUP_VEC
, /* optinfo_flags */
784 TV_IPA_OPT
, /* tv_id */
785 0, /* properties_required */
786 0, /* properties_provided */
787 0, /* properties_destroyed */
788 0, /* todo_flags_start */
789 0, /* todo_flags_finish */
792 class pass_ipa_increase_alignment
: public simple_ipa_opt_pass
795 pass_ipa_increase_alignment (gcc::context
*ctxt
)
796 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment
, ctxt
)
799 /* opt_pass methods: */
800 virtual bool gate (function
*)
802 return flag_section_anchors
&& flag_tree_loop_vectorize
;
805 virtual unsigned int execute (function
*) { return increase_alignment (); }
807 }; // class pass_ipa_increase_alignment
811 simple_ipa_opt_pass
*
812 make_pass_ipa_increase_alignment (gcc::context
*ctxt
)
814 return new pass_ipa_increase_alignment (ctxt
);