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
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"
64 #include "tree-pretty-print.h"
65 #include "tree-flow.h"
67 #include "tree-vectorizer.h"
68 #include "tree-pass.h"
70 /* Loop or bb location. */
73 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
74 vec
<vec_void_p
> stmt_vec_info_vec
;
77 /* Function vectorize_loops.
79 Entry point to loop vectorization phase. */
82 vectorize_loops (void)
85 unsigned int num_vectorized_loops
= 0;
86 unsigned int vect_loops_num
;
90 vect_loops_num
= number_of_loops (cfun
);
92 /* Bail out if there are no loops. */
93 if (vect_loops_num
<= 1)
96 init_stmt_vec_info_vec ();
98 /* ----------- Analyze loops. ----------- */
100 /* If some loop was duplicated, it gets bigger number
101 than all previously defined loops. This fact allows us to run
102 only over initial loops skipping newly generated ones. */
103 FOR_EACH_LOOP (li
, loop
, 0)
104 if (optimize_loop_nest_for_speed_p (loop
))
106 loop_vec_info loop_vinfo
;
107 vect_location
= find_loop_location (loop
);
108 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOC
109 && dump_enabled_p ())
110 dump_printf (MSG_NOTE
, "\nAnalyzing loop at %s:%d\n",
111 LOC_FILE (vect_location
), LOC_LINE (vect_location
));
113 loop_vinfo
= vect_analyze_loop (loop
);
114 loop
->aux
= loop_vinfo
;
116 if (!loop_vinfo
|| !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
))
119 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOC
120 && dump_enabled_p ())
121 dump_printf (MSG_NOTE
, "\n\nVectorizing loop at %s:%d\n",
122 LOC_FILE (vect_location
), LOC_LINE (vect_location
));
123 vect_transform_loop (loop_vinfo
);
124 num_vectorized_loops
++;
127 vect_location
= UNKNOWN_LOC
;
129 statistics_counter_event (cfun
, "Vectorized loops", num_vectorized_loops
);
130 if (dump_enabled_p ()
131 || (num_vectorized_loops
> 0 && dump_enabled_p ()))
132 dump_printf_loc (MSG_NOTE
, vect_location
,
133 "vectorized %u loops in function.\n",
134 num_vectorized_loops
);
136 /* ----------- Finalize. ----------- */
138 for (i
= 1; i
< vect_loops_num
; i
++)
140 loop_vec_info loop_vinfo
;
142 loop
= get_loop (cfun
, i
);
145 loop_vinfo
= (loop_vec_info
) loop
->aux
;
146 destroy_loop_vec_info (loop_vinfo
, true);
150 free_stmt_vec_info_vec ();
152 if (num_vectorized_loops
> 0)
154 /* If we vectorized any loop only virtual SSA form needs to be updated.
155 ??? Also while we try hard to update loop-closed SSA form we fail
156 to properly do this in some corner-cases (see PR56286). */
157 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa_only_virtuals
);
158 return TODO_cleanup_cfg
;
165 /* Entry point to basic block SLP phase. */
168 execute_vect_slp (void)
172 init_stmt_vec_info_vec ();
176 vect_location
= find_bb_location (bb
);
178 if (vect_slp_analyze_bb (bb
))
180 vect_slp_transform_bb (bb
);
181 if (dump_enabled_p ())
182 dump_printf_loc (MSG_NOTE
, vect_location
,
183 "basic block vectorized using SLP\n");
187 free_stmt_vec_info_vec ();
194 /* Apply SLP either if the vectorizer is on and the user didn't specify
195 whether to run SLP or not, or if the SLP flag was set by the user. */
196 return ((flag_tree_vectorize
!= 0 && flag_tree_slp_vectorize
!= 0)
197 || flag_tree_slp_vectorize
== 1);
200 struct gimple_opt_pass pass_slp_vectorize
=
206 | OPTGROUP_VEC
, /* optinfo_flags */
207 gate_vect_slp
, /* gate */
208 execute_vect_slp
, /* execute */
211 0, /* static_pass_number */
212 TV_TREE_SLP_VECTORIZATION
, /* tv_id */
213 PROP_ssa
| PROP_cfg
, /* properties_required */
214 0, /* properties_provided */
215 0, /* properties_destroyed */
216 0, /* todo_flags_start */
219 | TODO_verify_stmts
/* todo_flags_finish */
224 /* Increase alignment of global arrays to improve vectorization potential.
226 - Consider also structs that have an array field.
227 - Use ipa analysis to prune arrays that can't be vectorized?
228 This should involve global alignment analysis and in the future also
232 increase_alignment (void)
234 struct varpool_node
*vnode
;
236 vect_location
= UNKNOWN_LOC
;
238 /* Increase the alignment of all global arrays for vectorization. */
239 FOR_EACH_DEFINED_VARIABLE (vnode
)
241 tree vectype
, decl
= vnode
->symbol
.decl
;
243 unsigned int alignment
;
246 if (TREE_CODE (t
) != ARRAY_TYPE
)
248 vectype
= get_vectype_for_scalar_type (strip_array_types (t
));
251 alignment
= TYPE_ALIGN (vectype
);
252 if (DECL_ALIGN (decl
) >= alignment
)
255 if (vect_can_force_dr_alignment_p (decl
, alignment
))
257 DECL_ALIGN (decl
) = TYPE_ALIGN (vectype
);
258 DECL_USER_ALIGN (decl
) = 1;
259 dump_printf (MSG_NOTE
, "Increasing alignment of decl: ");
260 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, decl
);
261 dump_printf (MSG_NOTE
, "\n");
269 gate_increase_alignment (void)
271 return flag_section_anchors
&& flag_tree_vectorize
;
275 struct simple_ipa_opt_pass pass_ipa_increase_alignment
=
279 "increase_alignment", /* name */
281 | OPTGROUP_VEC
, /* optinfo_flags */
282 gate_increase_alignment
, /* gate */
283 increase_alignment
, /* execute */
286 0, /* static_pass_number */
287 TV_IPA_OPT
, /* tv_id */
288 0, /* properties_required */
289 0, /* properties_provided */
290 0, /* properties_destroyed */
291 0, /* todo_flags_start */
292 0 /* todo_flags_finish */