2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Dorit Naishlos <dorit@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Loop and basic block vectorizer.
24 This file contains drivers for the three vectorizers:
25 (1) loop vectorizer (inter-iteration parallelism),
26 (2) loop-aware SLP (intra-iteration parallelism) (invoked by the loop
28 (3) BB vectorizer (out-of-loops), aka SLP
30 The rest of the vectorizer's code is organized as follows:
31 - tree-vect-loop.c - loop specific parts such as reductions, etc. These are
32 used by drivers (1) and (2).
33 - tree-vect-loop-manip.c - vectorizer's loop control-flow utilities, used by
35 - tree-vect-slp.c - BB vectorization specific analysis and transformation,
36 used by drivers (2) and (3).
37 - tree-vect-stmts.c - statements analysis and transformation (used by all).
38 - tree-vect-data-refs.c - vectorizer specific data-refs analysis and
39 manipulations (used by all).
40 - tree-vect-patterns.c - vectorizable code patterns detector (used by all)
42 Here's a poor attempt at illustrating that:
45 loop_vect() loop_aware_slp() slp_vect()
48 tree-vect-loop.c tree-vect-slp.c
53 tree-vect-stmts.c tree-vect-data-refs.c
60 #include "coretypes.h"
65 #include "tree-pretty-print.h"
66 #include "tree-flow.h"
68 #include "tree-vectorizer.h"
69 #include "tree-pass.h"
71 /* Loop or bb location. */
74 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
75 VEC(vec_void_p
,heap
) *stmt_vec_info_vec
;
78 /* Function vectorize_loops.
80 Entry point to loop vectorization phase. */
83 vectorize_loops (void)
86 unsigned int num_vectorized_loops
= 0;
87 unsigned int vect_loops_num
;
91 vect_loops_num
= number_of_loops ();
93 /* Bail out if there are no loops. */
94 if (vect_loops_num
<= 1)
97 init_stmt_vec_info_vec ();
99 /* ----------- Analyze loops. ----------- */
101 /* If some loop was duplicated, it gets bigger number
102 than all previously defined loops. This fact allows us to run
103 only over initial loops skipping newly generated ones. */
104 FOR_EACH_LOOP (li
, loop
, 0)
105 if (optimize_loop_nest_for_speed_p (loop
))
107 loop_vec_info loop_vinfo
;
108 vect_location
= find_loop_location (loop
);
109 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOC
110 && dump_kind_p (MSG_ALL
))
111 dump_printf (MSG_ALL
, "\nAnalyzing loop at %s:%d\n",
112 LOC_FILE (vect_location
), LOC_LINE (vect_location
));
114 loop_vinfo
= vect_analyze_loop (loop
);
115 loop
->aux
= loop_vinfo
;
117 if (!loop_vinfo
|| !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
))
120 if (LOCATION_LOCUS (vect_location
) != UNKNOWN_LOC
121 && dump_kind_p (MSG_ALL
))
122 dump_printf (MSG_ALL
, "\n\nVectorizing loop at %s:%d\n",
123 LOC_FILE (vect_location
), LOC_LINE (vect_location
));
124 vect_transform_loop (loop_vinfo
);
125 num_vectorized_loops
++;
128 vect_location
= UNKNOWN_LOC
;
130 statistics_counter_event (cfun
, "Vectorized loops", num_vectorized_loops
);
131 if (dump_kind_p (MSG_ALL
)
132 || (num_vectorized_loops
> 0 && dump_kind_p (MSG_ALL
)))
133 dump_printf_loc (MSG_ALL
, vect_location
,
134 "vectorized %u loops in function.\n",
135 num_vectorized_loops
);
137 /* ----------- Finalize. ----------- */
139 for (i
= 1; i
< vect_loops_num
; i
++)
141 loop_vec_info loop_vinfo
;
146 loop_vinfo
= (loop_vec_info
) loop
->aux
;
147 destroy_loop_vec_info (loop_vinfo
, true);
151 free_stmt_vec_info_vec ();
153 return num_vectorized_loops
> 0 ? TODO_cleanup_cfg
: 0;
157 /* Entry point to basic block SLP phase. */
160 execute_vect_slp (void)
164 init_stmt_vec_info_vec ();
168 vect_location
= find_bb_location (bb
);
170 if (vect_slp_analyze_bb (bb
))
172 vect_slp_transform_bb (bb
);
173 if (dump_kind_p (MSG_OPTIMIZED_LOCATIONS
))
174 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
175 "basic block vectorized using SLP\n");
179 free_stmt_vec_info_vec ();
186 /* Apply SLP either if the vectorizer is on and the user didn't specify
187 whether to run SLP or not, or if the SLP flag was set by the user. */
188 return ((flag_tree_vectorize
!= 0 && flag_tree_slp_vectorize
!= 0)
189 || flag_tree_slp_vectorize
== 1);
192 struct gimple_opt_pass pass_slp_vectorize
=
197 gate_vect_slp
, /* gate */
198 execute_vect_slp
, /* execute */
201 0, /* static_pass_number */
202 TV_TREE_SLP_VECTORIZATION
, /* tv_id */
203 PROP_ssa
| PROP_cfg
, /* properties_required */
204 0, /* properties_provided */
205 0, /* properties_destroyed */
206 0, /* todo_flags_start */
210 | TODO_verify_stmts
/* todo_flags_finish */
215 /* Increase alignment of global arrays to improve vectorization potential.
217 - Consider also structs that have an array field.
218 - Use ipa analysis to prune arrays that can't be vectorized?
219 This should involve global alignment analysis and in the future also
223 increase_alignment (void)
225 struct varpool_node
*vnode
;
227 /* Increase the alignment of all global arrays for vectorization. */
228 FOR_EACH_DEFINED_VARIABLE (vnode
)
230 tree vectype
, decl
= vnode
->symbol
.decl
;
232 unsigned int alignment
;
235 if (TREE_CODE (t
) != ARRAY_TYPE
)
237 vectype
= get_vectype_for_scalar_type (strip_array_types (t
));
240 alignment
= TYPE_ALIGN (vectype
);
241 if (DECL_ALIGN (decl
) >= alignment
)
244 if (vect_can_force_dr_alignment_p (decl
, alignment
))
246 DECL_ALIGN (decl
) = TYPE_ALIGN (vectype
);
247 DECL_USER_ALIGN (decl
) = 1;
248 dump_printf (MSG_NOTE
, "Increasing alignment of decl: ");
249 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, decl
);
250 dump_printf (MSG_NOTE
, "\n");
258 gate_increase_alignment (void)
260 return flag_section_anchors
&& flag_tree_vectorize
;
264 struct simple_ipa_opt_pass pass_ipa_increase_alignment
=
268 "increase_alignment", /* name */
269 gate_increase_alignment
, /* gate */
270 increase_alignment
, /* execute */
273 0, /* static_pass_number */
274 TV_IPA_OPT
, /* tv_id */
275 0, /* properties_required */
276 0, /* properties_provided */
277 0, /* properties_destroyed */
278 0, /* todo_flags_start */
279 0 /* todo_flags_finish */