Branch data Line data Source code
1 : : /* Data References Analysis and Manipulation Utilities for Vectorization.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 : : and Ira Rosen <irar@il.ibm.com>
5 : :
6 : : This file is part of GCC.
7 : :
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
11 : : version.
12 : :
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
16 : : for more details.
17 : :
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/>. */
21 : :
22 : : #define INCLUDE_ALGORITHM
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "backend.h"
27 : : #include "target.h"
28 : : #include "rtl.h"
29 : : #include "tree.h"
30 : : #include "gimple.h"
31 : : #include "predict.h"
32 : : #include "memmodel.h"
33 : : #include "tm_p.h"
34 : : #include "ssa.h"
35 : : #include "optabs-tree.h"
36 : : #include "cgraph.h"
37 : : #include "dumpfile.h"
38 : : #include "pretty-print.h"
39 : : #include "alias.h"
40 : : #include "fold-const.h"
41 : : #include "stor-layout.h"
42 : : #include "tree-eh.h"
43 : : #include "gimplify.h"
44 : : #include "gimple-iterator.h"
45 : : #include "gimplify-me.h"
46 : : #include "tree-ssa-loop-ivopts.h"
47 : : #include "tree-ssa-loop-manip.h"
48 : : #include "tree-ssa-loop.h"
49 : : #include "cfgloop.h"
50 : : #include "tree-scalar-evolution.h"
51 : : #include "tree-vectorizer.h"
52 : : #include "expr.h"
53 : : #include "builtins.h"
54 : : #include "tree-cfg.h"
55 : : #include "tree-hash-traits.h"
56 : : #include "vec-perm-indices.h"
57 : : #include "internal-fn.h"
58 : : #include "gimple-fold.h"
59 : : #include "optabs-query.h"
60 : :
61 : : /* Return true if load- or store-lanes optab OPTAB is implemented for
62 : : COUNT vectors of type VECTYPE. NAME is the name of OPTAB.
63 : :
64 : : If it is implemented and ELSVALS is nonzero store the possible else
65 : : values in the vector it points to. */
66 : :
67 : : static bool
68 : 242476 : vect_lanes_optab_supported_p (const char *name, convert_optab optab,
69 : : tree vectype, unsigned HOST_WIDE_INT count,
70 : : vec<int> *elsvals = nullptr)
71 : : {
72 : 242476 : machine_mode mode, array_mode;
73 : 242476 : bool limit_p;
74 : :
75 : 242476 : mode = TYPE_MODE (vectype);
76 : 242476 : if (!targetm.array_mode (mode, count).exists (&array_mode))
77 : : {
78 : 484952 : poly_uint64 bits = count * GET_MODE_BITSIZE (mode);
79 : 242476 : limit_p = !targetm.array_mode_supported_p (mode, count);
80 : 242476 : if (!int_mode_for_size (bits, limit_p).exists (&array_mode))
81 : : {
82 : 218834 : if (dump_enabled_p ())
83 : 11888 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
84 : : "no array mode for %s[%wu]\n",
85 : 11888 : GET_MODE_NAME (mode), count);
86 : 218834 : return false;
87 : : }
88 : : }
89 : :
90 : 23642 : enum insn_code icode;
91 : 23642 : if ((icode = convert_optab_handler (optab, array_mode, mode))
92 : : == CODE_FOR_nothing)
93 : : {
94 : 23642 : if (dump_enabled_p ())
95 : 3838 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
96 : : "cannot use %s<%s><%s>\n", name,
97 : 3838 : GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
98 : 23642 : return false;
99 : : }
100 : :
101 : 0 : if (dump_enabled_p ())
102 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
103 : 0 : "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
104 : 0 : GET_MODE_NAME (mode));
105 : :
106 : 0 : if (elsvals)
107 : 0 : get_supported_else_vals (icode,
108 : 0 : internal_fn_else_index (IFN_MASK_LEN_LOAD_LANES),
109 : : *elsvals);
110 : :
111 : : return true;
112 : : }
113 : :
114 : : /* Helper function to identify a simd clone call. If this is a call to a
115 : : function with simd clones then return the corresponding cgraph_node,
116 : : otherwise return NULL. */
117 : :
118 : : static cgraph_node*
119 : 800235 : simd_clone_call_p (gimple *stmt)
120 : : {
121 : 882339 : gcall *call = dyn_cast <gcall *> (stmt);
122 : 84329 : if (!call)
123 : : return NULL;
124 : :
125 : 84329 : tree fndecl = NULL_TREE;
126 : 84329 : if (gimple_call_internal_p (call, IFN_MASK_CALL))
127 : 287 : fndecl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
128 : : else
129 : 84042 : fndecl = gimple_call_fndecl (stmt);
130 : :
131 : 84329 : if (fndecl == NULL_TREE)
132 : : return NULL;
133 : :
134 : 39414 : cgraph_node *node = cgraph_node::get (fndecl);
135 : 39414 : if (node && node->simd_clones != NULL)
136 : : return node;
137 : :
138 : : return NULL;
139 : : }
140 : :
141 : :
142 : :
143 : : /* Return the smallest scalar part of STMT_INFO.
144 : : This is used to determine the vectype of the stmt. We generally set the
145 : : vectype according to the type of the result (lhs). For stmts whose
146 : : result-type is different than the type of the arguments (e.g., demotion,
147 : : promotion), vectype will be reset appropriately (later). Note that we have
148 : : to visit the smallest datatype in this function, because that determines the
149 : : VF. If the smallest datatype in the loop is present only as the rhs of a
150 : : promotion operation - we'd miss it.
151 : : Such a case, where a variable of this datatype does not appear in the lhs
152 : : anywhere in the loop, can only occur if it's an invariant: e.g.:
153 : : 'int_x = (int) short_inv', which we'd expect to have been optimized away by
154 : : invariant motion. However, we cannot rely on invariant motion to always
155 : : take invariants out of the loop, and so in the case of promotion we also
156 : : have to check the rhs.
157 : : LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
158 : : types. */
159 : :
160 : : tree
161 : 6202462 : vect_get_smallest_scalar_type (stmt_vec_info stmt_info, tree scalar_type)
162 : : {
163 : 6202462 : HOST_WIDE_INT lhs, rhs;
164 : :
165 : : /* During the analysis phase, this function is called on arbitrary
166 : : statements that might not have scalar results. */
167 : 6202462 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (scalar_type)))
168 : : return scalar_type;
169 : :
170 : 6202462 : lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
171 : :
172 : 6202462 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
173 : 6202462 : if (assign)
174 : : {
175 : 5402227 : scalar_type = TREE_TYPE (gimple_assign_lhs (assign));
176 : 5402227 : if (gimple_assign_cast_p (assign)
177 : 4924515 : || gimple_assign_rhs_code (assign) == DOT_PROD_EXPR
178 : 4923873 : || gimple_assign_rhs_code (assign) == WIDEN_SUM_EXPR
179 : 4923873 : || gimple_assign_rhs_code (assign) == SAD_EXPR
180 : 4923562 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_EXPR
181 : 4918433 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_PLUS_EXPR
182 : 4918433 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_MINUS_EXPR
183 : 4918433 : || gimple_assign_rhs_code (assign) == WIDEN_LSHIFT_EXPR
184 : 10320660 : || gimple_assign_rhs_code (assign) == FLOAT_EXPR)
185 : : {
186 : 503840 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (assign));
187 : :
188 : 503840 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
189 : 503840 : if (rhs < lhs)
190 : 6202462 : scalar_type = rhs_type;
191 : : }
192 : : }
193 : 800235 : else if (cgraph_node *node = simd_clone_call_p (stmt_info->stmt))
194 : : {
195 : 2225 : auto clone = node->simd_clones->simdclone;
196 : 7134 : for (unsigned int i = 0; i < clone->nargs; ++i)
197 : : {
198 : 4909 : if (clone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
199 : : {
200 : 2879 : tree arg_scalar_type = TREE_TYPE (clone->args[i].vector_type);
201 : 2879 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (arg_scalar_type));
202 : 2879 : if (rhs < lhs)
203 : : {
204 : 4909 : scalar_type = arg_scalar_type;
205 : 4909 : lhs = rhs;
206 : : }
207 : : }
208 : : }
209 : : }
210 : 798010 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
211 : : {
212 : 82104 : unsigned int i = 0;
213 : 82104 : if (gimple_call_internal_p (call))
214 : : {
215 : 42524 : internal_fn ifn = gimple_call_internal_fn (call);
216 : 42524 : if (internal_load_fn_p (ifn))
217 : : /* For loads the LHS type does the trick. */
218 : : i = ~0U;
219 : 36561 : else if (internal_store_fn_p (ifn))
220 : : {
221 : : /* For stores use the tyep of the stored value. */
222 : 2697 : i = internal_fn_stored_value_index (ifn);
223 : 2697 : scalar_type = TREE_TYPE (gimple_call_arg (call, i));
224 : 2697 : i = ~0U;
225 : : }
226 : 33864 : else if (internal_fn_mask_index (ifn) == 0)
227 : 9087 : i = 1;
228 : : }
229 : 82104 : if (i < gimple_call_num_args (call))
230 : : {
231 : 68828 : tree rhs_type = TREE_TYPE (gimple_call_arg (call, i));
232 : 68828 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (rhs_type)))
233 : : {
234 : 68828 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
235 : 68828 : if (rhs < lhs)
236 : 6202462 : scalar_type = rhs_type;
237 : : }
238 : : }
239 : : }
240 : :
241 : : return scalar_type;
242 : : }
243 : :
244 : :
245 : : /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
246 : : tested at run-time. Return TRUE if DDR was successfully inserted.
247 : : Return false if versioning is not supported. */
248 : :
249 : : static opt_result
250 : 131551 : vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
251 : : {
252 : 131551 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
253 : :
254 : 131551 : if ((unsigned) param_vect_max_version_for_alias_checks == 0)
255 : 54 : return opt_result::failure_at (vect_location,
256 : : "will not create alias checks, as"
257 : : " --param vect-max-version-for-alias-checks"
258 : : " == 0\n");
259 : :
260 : 131497 : opt_result res
261 : 131497 : = runtime_alias_check_p (ddr, loop,
262 : 131497 : optimize_loop_nest_for_speed_p (loop));
263 : 131497 : if (!res)
264 : 125 : return res;
265 : :
266 : 131372 : LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
267 : 131372 : return opt_result::success ();
268 : : }
269 : :
270 : : /* Record that loop LOOP_VINFO needs to check that VALUE is nonzero. */
271 : :
272 : : static void
273 : 1214 : vect_check_nonzero_value (loop_vec_info loop_vinfo, tree value)
274 : : {
275 : 1214 : const vec<tree> &checks = LOOP_VINFO_CHECK_NONZERO (loop_vinfo);
276 : 1865 : for (unsigned int i = 0; i < checks.length(); ++i)
277 : 657 : if (checks[i] == value)
278 : : return;
279 : :
280 : 1208 : if (dump_enabled_p ())
281 : 428 : dump_printf_loc (MSG_NOTE, vect_location,
282 : : "need run-time check that %T is nonzero\n",
283 : : value);
284 : 1208 : LOOP_VINFO_CHECK_NONZERO (loop_vinfo).safe_push (value);
285 : : }
286 : :
287 : : /* Return true if we know that the order of vectorized DR_INFO_A and
288 : : vectorized DR_INFO_B will be the same as the order of DR_INFO_A and
289 : : DR_INFO_B. At least one of the accesses is a write. */
290 : :
291 : : static bool
292 : 108931 : vect_preserves_scalar_order_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b)
293 : : {
294 : 108931 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
295 : 108931 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
296 : :
297 : : /* Single statements are always kept in their original order. */
298 : 108931 : if (!STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
299 : 176234 : && !STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
300 : : return true;
301 : :
302 : : /* If there is a loop invariant read involved we might vectorize it in
303 : : the prologue, breaking scalar oder with respect to the in-loop store. */
304 : 21621 : if ((DR_IS_READ (dr_info_a->dr) && integer_zerop (DR_STEP (dr_info_a->dr)))
305 : 67110 : || (DR_IS_READ (dr_info_b->dr) && integer_zerop (DR_STEP (dr_info_b->dr))))
306 : 1305 : return false;
307 : :
308 : : /* STMT_A and STMT_B belong to overlapping groups. All loads are
309 : : emitted at the position of the first scalar load.
310 : : Stores in a group are emitted at the position of the last scalar store.
311 : : Compute that position and check whether the resulting order matches
312 : : the current one. */
313 : 45177 : stmt_vec_info il_a = DR_GROUP_FIRST_ELEMENT (stmtinfo_a);
314 : 45177 : if (il_a)
315 : : {
316 : 41316 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_a)))
317 : 159824 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
318 : 138526 : s = DR_GROUP_NEXT_ELEMENT (s))
319 : 138526 : il_a = get_later_stmt (il_a, s);
320 : : else /* DR_IS_READ */
321 : 79759 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
322 : 59741 : s = DR_GROUP_NEXT_ELEMENT (s))
323 : 59741 : if (get_later_stmt (il_a, s) == il_a)
324 : 1854 : il_a = s;
325 : : }
326 : : else
327 : : il_a = stmtinfo_a;
328 : 45177 : stmt_vec_info il_b = DR_GROUP_FIRST_ELEMENT (stmtinfo_b);
329 : 45177 : if (il_b)
330 : : {
331 : 40269 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_b)))
332 : 209440 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
333 : 177610 : s = DR_GROUP_NEXT_ELEMENT (s))
334 : 177610 : il_b = get_later_stmt (il_b, s);
335 : : else /* DR_IS_READ */
336 : 39067 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
337 : 30628 : s = DR_GROUP_NEXT_ELEMENT (s))
338 : 30628 : if (get_later_stmt (il_b, s) == il_b)
339 : 207 : il_b = s;
340 : : }
341 : : else
342 : : il_b = stmtinfo_b;
343 : 45177 : bool a_after_b = (get_later_stmt (stmtinfo_a, stmtinfo_b) == stmtinfo_a);
344 : 45177 : return (get_later_stmt (il_a, il_b) == il_a) == a_after_b;
345 : : }
346 : :
347 : : /* A subroutine of vect_analyze_data_ref_dependence. Handle
348 : : DDR_COULD_BE_INDEPENDENT_P ddr DDR that has a known set of dependence
349 : : distances. These distances are conservatively correct but they don't
350 : : reflect a guaranteed dependence.
351 : :
352 : : Return true if this function does all the work necessary to avoid
353 : : an alias or false if the caller should use the dependence distances
354 : : to limit the vectorization factor in the usual way. LOOP_DEPTH is
355 : : the depth of the loop described by LOOP_VINFO and the other arguments
356 : : are as for vect_analyze_data_ref_dependence. */
357 : :
358 : : static bool
359 : 7771 : vect_analyze_possibly_independent_ddr (data_dependence_relation *ddr,
360 : : loop_vec_info loop_vinfo,
361 : : int loop_depth, unsigned int *max_vf)
362 : : {
363 : 7771 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
364 : 31102 : for (lambda_vector &dist_v : DDR_DIST_VECTS (ddr))
365 : : {
366 : 15392 : int dist = dist_v[loop_depth];
367 : 15392 : if (dist != 0 && !(dist > 0 && DDR_REVERSED_P (ddr)))
368 : : {
369 : : /* If the user asserted safelen >= DIST consecutive iterations
370 : : can be executed concurrently, assume independence.
371 : :
372 : : ??? An alternative would be to add the alias check even
373 : : in this case, and vectorize the fallback loop with the
374 : : maximum VF set to safelen. However, if the user has
375 : : explicitly given a length, it's less likely that that
376 : : would be a win. */
377 : 7635 : if (loop->safelen >= 2 && abs_hwi (dist) <= loop->safelen)
378 : : {
379 : 32 : if ((unsigned int) loop->safelen < *max_vf)
380 : 2 : *max_vf = loop->safelen;
381 : 32 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
382 : 32 : continue;
383 : : }
384 : :
385 : : /* For dependence distances of 2 or more, we have the option
386 : : of limiting VF or checking for an alias at runtime.
387 : : Prefer to check at runtime if we can, to avoid limiting
388 : : the VF unnecessarily when the bases are in fact independent.
389 : :
390 : : Note that the alias checks will be removed if the VF ends up
391 : : being small enough. */
392 : 7603 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
393 : 7603 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
394 : 7603 : return (!STMT_VINFO_GATHER_SCATTER_P (dr_info_a->stmt)
395 : 7603 : && !STMT_VINFO_GATHER_SCATTER_P (dr_info_b->stmt)
396 : 15214 : && vect_mark_for_runtime_alias_test (ddr, loop_vinfo));
397 : : }
398 : : }
399 : : return true;
400 : : }
401 : :
402 : :
403 : : /* Function vect_analyze_data_ref_dependence.
404 : :
405 : : FIXME: I needed to change the sense of the returned flag.
406 : :
407 : : Return FALSE if there (might) exist a dependence between a memory-reference
408 : : DRA and a memory-reference DRB. When versioning for alias may check a
409 : : dependence at run-time, return TRUE. Adjust *MAX_VF according to
410 : : the data dependence. */
411 : :
412 : : static opt_result
413 : 969654 : vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
414 : : loop_vec_info loop_vinfo,
415 : : unsigned int *max_vf)
416 : : {
417 : 969654 : unsigned int i;
418 : 969654 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
419 : 969654 : struct data_reference *dra = DDR_A (ddr);
420 : 969654 : struct data_reference *drb = DDR_B (ddr);
421 : 969654 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (dra);
422 : 969654 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (drb);
423 : 969654 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
424 : 969654 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
425 : 969654 : lambda_vector dist_v;
426 : 969654 : unsigned int loop_depth;
427 : :
428 : : /* If user asserted safelen consecutive iterations can be
429 : : executed concurrently, assume independence. */
430 : 1108840 : auto apply_safelen = [&]()
431 : : {
432 : 139186 : if (loop->safelen >= 2)
433 : : {
434 : 7454 : if ((unsigned int) loop->safelen < *max_vf)
435 : 1893 : *max_vf = loop->safelen;
436 : 7454 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
437 : 7454 : return true;
438 : : }
439 : : return false;
440 : 969654 : };
441 : :
442 : : /* In loop analysis all data references should be vectorizable. */
443 : 969654 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
444 : 969654 : || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
445 : 0 : gcc_unreachable ();
446 : :
447 : : /* Independent data accesses. */
448 : 969654 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
449 : 767828 : return opt_result::success ();
450 : :
451 : 201826 : if (dra == drb
452 : 201826 : || (DR_IS_READ (dra) && DR_IS_READ (drb)))
453 : 0 : return opt_result::success ();
454 : :
455 : : /* We do not have to consider dependences between accesses that belong
456 : : to the same group, unless the stride could be smaller than the
457 : : group size. */
458 : 201826 : if (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
459 : 90624 : && (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
460 : 90624 : == DR_GROUP_FIRST_ELEMENT (stmtinfo_b))
461 : 215239 : && !STMT_VINFO_STRIDED_P (stmtinfo_a))
462 : 1847 : return opt_result::success ();
463 : :
464 : : /* Even if we have an anti-dependence then, as the vectorized loop covers at
465 : : least two scalar iterations, there is always also a true dependence.
466 : : As the vectorizer does not re-order loads and stores we can ignore
467 : : the anti-dependence if TBAA can disambiguate both DRs similar to the
468 : : case with known negative distance anti-dependences (positive
469 : : distance anti-dependences would violate TBAA constraints). */
470 : 97010 : if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
471 : 102969 : || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
472 : 311299 : && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
473 : : get_alias_set (DR_REF (drb))))
474 : 5041 : return opt_result::success ();
475 : :
476 : 194938 : if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
477 : 187627 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
478 : : {
479 : 9176 : if (apply_safelen ())
480 : 1392 : return opt_result::success ();
481 : :
482 : 7784 : return opt_result::failure_at
483 : 7784 : (stmtinfo_a->stmt,
484 : : "possible alias involving gather/scatter between %T and %T\n",
485 : : DR_REF (dra), DR_REF (drb));
486 : : }
487 : :
488 : : /* Unknown data dependence. */
489 : 185762 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
490 : : {
491 : 129511 : if (apply_safelen ())
492 : 6062 : return opt_result::success ();
493 : :
494 : 123449 : if (dump_enabled_p ())
495 : 7024 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmtinfo_a->stmt,
496 : : "versioning for alias required: "
497 : : "can't determine dependence between %T and %T\n",
498 : : DR_REF (dra), DR_REF (drb));
499 : :
500 : : /* Add to list of ddrs that need to be tested at run-time. */
501 : 123449 : return vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
502 : : }
503 : :
504 : : /* Known data dependence. */
505 : 56251 : if (DDR_NUM_DIST_VECTS (ddr) == 0)
506 : : {
507 : 499 : if (apply_safelen ())
508 : 0 : return opt_result::success ();
509 : :
510 : 499 : if (dump_enabled_p ())
511 : 114 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmtinfo_a->stmt,
512 : : "versioning for alias required: "
513 : : "bad dist vector for %T and %T\n",
514 : : DR_REF (dra), DR_REF (drb));
515 : : /* Add to list of ddrs that need to be tested at run-time. */
516 : 499 : return vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
517 : : }
518 : :
519 : 55752 : loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
520 : :
521 : 55752 : if (DDR_COULD_BE_INDEPENDENT_P (ddr)
522 : 55752 : && vect_analyze_possibly_independent_ddr (ddr, loop_vinfo,
523 : : loop_depth, max_vf))
524 : 7763 : return opt_result::success ();
525 : :
526 : 90063 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
527 : : {
528 : 48009 : int dist = dist_v[loop_depth];
529 : :
530 : 48009 : if (dump_enabled_p ())
531 : 3665 : dump_printf_loc (MSG_NOTE, vect_location,
532 : : "dependence distance = %d.\n", dist);
533 : :
534 : 48009 : if (dist == 0)
535 : : {
536 : 38466 : if (dump_enabled_p ())
537 : 3257 : dump_printf_loc (MSG_NOTE, vect_location,
538 : : "dependence distance == 0 between %T and %T\n",
539 : : DR_REF (dra), DR_REF (drb));
540 : :
541 : : /* When we perform grouped accesses and perform implicit CSE
542 : : by detecting equal accesses and doing disambiguation with
543 : : runtime alias tests like for
544 : : .. = a[i];
545 : : .. = a[i+1];
546 : : a[i] = ..;
547 : : a[i+1] = ..;
548 : : *p = ..;
549 : : .. = a[i];
550 : : .. = a[i+1];
551 : : where we will end up loading { a[i], a[i+1] } once, make
552 : : sure that inserting group loads before the first load and
553 : : stores after the last store will do the right thing.
554 : : Similar for groups like
555 : : a[i] = ...;
556 : : ... = a[i];
557 : : a[i+1] = ...;
558 : : where loads from the group interleave with the store. */
559 : 38466 : if (!vect_preserves_scalar_order_p (dr_info_a, dr_info_b))
560 : 0 : return opt_result::failure_at (stmtinfo_a->stmt,
561 : : "READ_WRITE dependence"
562 : : " in interleaving.\n");
563 : :
564 : 38466 : if (loop->safelen < 2)
565 : : {
566 : 34732 : tree indicator = dr_zero_step_indicator (dra);
567 : 34732 : if (!indicator || integer_zerop (indicator))
568 : 0 : return opt_result::failure_at (stmtinfo_a->stmt,
569 : : "access also has a zero step\n");
570 : 34732 : else if (TREE_CODE (indicator) != INTEGER_CST)
571 : 1214 : vect_check_nonzero_value (loop_vinfo, indicator);
572 : : }
573 : 38466 : continue;
574 : 38466 : }
575 : :
576 : 9543 : if (dist > 0 && DDR_REVERSED_P (ddr))
577 : : {
578 : : /* If DDR_REVERSED_P the order of the data-refs in DDR was
579 : : reversed (to make distance vector positive), and the actual
580 : : distance is negative. */
581 : 3195 : if (dump_enabled_p ())
582 : 105 : dump_printf_loc (MSG_NOTE, vect_location,
583 : : "dependence distance negative.\n");
584 : : /* When doing outer loop vectorization, we need to check if there is
585 : : a backward dependence at the inner loop level if the dependence
586 : : at the outer loop is reversed. See PR81740. */
587 : 3195 : if (nested_in_vect_loop_p (loop, stmtinfo_a)
588 : 3183 : || nested_in_vect_loop_p (loop, stmtinfo_b))
589 : : {
590 : 12 : unsigned inner_depth = index_in_loop_nest (loop->inner->num,
591 : 12 : DDR_LOOP_NEST (ddr));
592 : 12 : if (dist_v[inner_depth] < 0)
593 : 9 : return opt_result::failure_at (stmtinfo_a->stmt,
594 : : "not vectorized, dependence "
595 : : "between data-refs %T and %T\n",
596 : : DR_REF (dra), DR_REF (drb));
597 : : }
598 : : /* Record a negative dependence distance to later limit the
599 : : amount of stmt copying / unrolling we can perform.
600 : : Only need to handle read-after-write dependence. */
601 : 3186 : if (DR_IS_READ (drb)
602 : 78 : && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
603 : 12 : || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
604 : 78 : STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
605 : 3186 : continue;
606 : 3186 : }
607 : :
608 : 6348 : unsigned int abs_dist = abs (dist);
609 : 6348 : if (abs_dist >= 2 && abs_dist < *max_vf)
610 : : {
611 : : /* The dependence distance requires reduction of the maximal
612 : : vectorization factor. */
613 : 313 : *max_vf = abs_dist;
614 : 313 : if (dump_enabled_p ())
615 : 30 : dump_printf_loc (MSG_NOTE, vect_location,
616 : : "adjusting maximal vectorization factor to %i\n",
617 : : *max_vf);
618 : : }
619 : :
620 : 6348 : if (abs_dist >= *max_vf)
621 : : {
622 : : /* Dependence distance does not create dependence, as far as
623 : : vectorization is concerned, in this case. */
624 : 422 : if (dump_enabled_p ())
625 : 57 : dump_printf_loc (MSG_NOTE, vect_location,
626 : : "dependence distance >= VF.\n");
627 : 422 : continue;
628 : : }
629 : :
630 : 5926 : return opt_result::failure_at (stmtinfo_a->stmt,
631 : : "not vectorized, possible dependence "
632 : : "between data-refs %T and %T\n",
633 : : DR_REF (dra), DR_REF (drb));
634 : : }
635 : :
636 : 42054 : return opt_result::success ();
637 : : }
638 : :
639 : : /* Function vect_analyze_early_break_dependences.
640 : :
641 : : Examine all the data references in the loop and make sure that if we have
642 : : multiple exits that we are able to safely move stores such that they become
643 : : safe for vectorization. The function also calculates the place where to move
644 : : the instructions to and computes what the new vUSE chain should be.
645 : :
646 : : This works in tandem with the CFG that will be produced by
647 : : slpeel_tree_duplicate_loop_to_edge_cfg later on.
648 : :
649 : : This function tries to validate whether an early break vectorization
650 : : is possible for the current instruction sequence. Returns True i
651 : : possible, otherwise False.
652 : :
653 : : Requirements:
654 : : - Any memory access must be to a fixed size buffer.
655 : : - There must not be any loads and stores to the same object.
656 : : - Multiple loads are allowed as long as they don't alias.
657 : :
658 : : NOTE:
659 : : This implementation is very conservative. Any overlapping loads/stores
660 : : that take place before the early break statement gets rejected aside from
661 : : WAR dependencies.
662 : :
663 : : i.e.:
664 : :
665 : : a[i] = 8
666 : : c = a[i]
667 : : if (b[i])
668 : : ...
669 : :
670 : : is not allowed, but
671 : :
672 : : c = a[i]
673 : : a[i] = 8
674 : : if (b[i])
675 : : ...
676 : :
677 : : is which is the common case. */
678 : :
679 : : static opt_result
680 : 113907 : vect_analyze_early_break_dependences (loop_vec_info loop_vinfo)
681 : : {
682 : 113907 : DUMP_VECT_SCOPE ("vect_analyze_early_break_dependences");
683 : :
684 : : /* List of all load data references found during traversal. */
685 : 113907 : auto_vec<data_reference *> bases;
686 : 113907 : basic_block dest_bb = NULL;
687 : :
688 : 113907 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
689 : 113907 : class loop *loop_nest = loop_outer (loop);
690 : :
691 : 113907 : if (dump_enabled_p ())
692 : 1336 : dump_printf_loc (MSG_NOTE, vect_location,
693 : : "loop contains multiple exits, analyzing"
694 : : " statement dependencies.\n");
695 : :
696 : 113907 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
697 : 5240 : if (dump_enabled_p ())
698 : 159 : dump_printf_loc (MSG_NOTE, vect_location,
699 : : "alternate exit has been chosen as main exit.\n");
700 : :
701 : : /* Since we don't support general control flow, the location we'll move the
702 : : side-effects to is always the latch connected exit. When we support
703 : : general control flow we can do better but for now this is fine. Move
704 : : side-effects to the in-loop destination of the last early exit. For the
705 : : PEELED case we move the side-effects to the latch block as this is
706 : : guaranteed to be the last block to be executed when a vector iteration
707 : : finished. */
708 : 113907 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
709 : 5240 : dest_bb = loop->latch;
710 : : else
711 : 108667 : dest_bb = single_pred (loop->latch);
712 : :
713 : : /* We start looking from dest_bb, for the non-PEELED case we don't want to
714 : : move any stores already present, but we do want to read and validate the
715 : : loads. */
716 : 113907 : basic_block bb = dest_bb;
717 : :
718 : : /* We move stores across all loads to the beginning of dest_bb, so
719 : : the first block processed below doesn't need dependence checking. */
720 : 113907 : bool check_deps = false;
721 : :
722 : 422661 : do
723 : : {
724 : 268284 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
725 : :
726 : : /* Now analyze all the remaining statements and try to determine which
727 : : instructions are allowed/needed to be moved. */
728 : 2056098 : while (!gsi_end_p (gsi))
729 : : {
730 : 1787961 : gimple *stmt = gsi_stmt (gsi);
731 : 1787961 : gsi_prev (&gsi);
732 : 1787961 : if (is_gimple_debug (stmt))
733 : 1583544 : continue;
734 : :
735 : 953366 : stmt_vec_info stmt_vinfo
736 : 953366 : = vect_stmt_to_vectorize (loop_vinfo->lookup_stmt (stmt));
737 : 953366 : auto dr_ref = STMT_VINFO_DATA_REF (stmt_vinfo);
738 : 953366 : if (!dr_ref)
739 : 737653 : continue;
740 : :
741 : : /* We know everything below dest_bb is safe since we know we
742 : : had a full vector iteration when reaching it. Either by
743 : : the loop entry / IV exit test being last or because this
744 : : is the loop latch itself. */
745 : 215713 : if (!check_deps)
746 : 11296 : continue;
747 : :
748 : : /* Check if vector accesses to the object will be within bounds.
749 : : must be a constant or assume loop will be versioned or niters
750 : : bounded by VF so accesses are within range. We only need to check
751 : : the reads since writes are moved to a safe place where if we get
752 : : there we know they are safe to perform. */
753 : 204417 : if (DR_IS_READ (dr_ref))
754 : : {
755 : 190880 : dr_set_safe_speculative_read_required (stmt_vinfo, true);
756 : 190880 : bool inbounds = ref_within_array_bound (stmt, DR_REF (dr_ref));
757 : 190880 : DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_vinfo)) = inbounds;
758 : :
759 : 190880 : if (dump_enabled_p ())
760 : 2196 : dump_printf_loc (MSG_NOTE, vect_location,
761 : : "marking DR (read) as possibly needing peeling "
762 : : "for alignment at %G", stmt);
763 : : }
764 : :
765 : 204417 : if (DR_IS_READ (dr_ref))
766 : 190880 : bases.safe_push (dr_ref);
767 : 13537 : else if (DR_IS_WRITE (dr_ref))
768 : : {
769 : : /* We are moving writes down in the CFG. To be sure that this
770 : : is valid after vectorization we have to check all the loads
771 : : we are sinking the stores past to see if any of them may
772 : : alias or are the same object.
773 : :
774 : : Same objects will not be an issue because unless the store
775 : : is marked volatile the value can be forwarded. If the
776 : : store is marked volatile we don't vectorize the loop
777 : : anyway.
778 : :
779 : : That leaves the check for aliasing. We don't really need
780 : : to care about the stores aliasing with each other since the
781 : : stores are moved in order so the effects are still observed
782 : : correctly. This leaves the check for WAR dependencies
783 : : which we would be introducing here if the DR can alias.
784 : : The check is quadratic in loads/stores but I have not found
785 : : a better API to do this. I believe all loads and stores
786 : : must be checked. We also must check them when we
787 : : encountered the store, since we don't care about loads past
788 : : the store. */
789 : :
790 : 45199 : for (auto dr_read : bases)
791 : 14775 : if (dr_may_alias_p (dr_ref, dr_read, loop_nest))
792 : : {
793 : 147 : if (dump_enabled_p ())
794 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
795 : : vect_location,
796 : : "early breaks not supported: "
797 : : "overlapping loads and stores "
798 : : "found before the break "
799 : : "statement.\n");
800 : :
801 : 147 : return opt_result::failure_at (stmt,
802 : : "can't safely apply code motion to dependencies"
803 : : " to vectorize the early exit. %G may alias with"
804 : : " %G\n", stmt, dr_read->stmt);
805 : : }
806 : : }
807 : :
808 : 408540 : if (gimple_vdef (stmt))
809 : : {
810 : 13390 : if (dump_enabled_p ())
811 : 251 : dump_printf_loc (MSG_NOTE, vect_location,
812 : : "==> recording stmt %G", stmt);
813 : :
814 : 13390 : LOOP_VINFO_EARLY_BRK_STORES (loop_vinfo).safe_push (stmt);
815 : : }
816 : 586030 : else if (gimple_vuse (stmt))
817 : : {
818 : 190880 : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).safe_insert (0, stmt);
819 : 190880 : if (dump_enabled_p ())
820 : 2196 : dump_printf_loc (MSG_NOTE, vect_location,
821 : : "marked statement for vUSE update: %G", stmt);
822 : : }
823 : : }
824 : :
825 : 268137 : if (!single_pred_p (bb))
826 : : {
827 : 113760 : gcc_assert (bb == loop->header);
828 : 113760 : break;
829 : : }
830 : :
831 : : /* If we possibly sink through a virtual PHI make sure to elide that. */
832 : 154377 : if (gphi *vphi = get_virtual_phi (bb))
833 : 41 : LOOP_VINFO_EARLY_BRK_STORES (loop_vinfo).safe_push (vphi);
834 : :
835 : : /* All earlier blocks need dependence checking. */
836 : 154377 : check_deps = true;
837 : 154377 : bb = single_pred (bb);
838 : 154377 : }
839 : : while (1);
840 : :
841 : : /* We don't allow outer -> inner loop transitions which should have been
842 : : trapped already during loop form analysis. */
843 : 113760 : gcc_assert (dest_bb->loop_father == loop);
844 : :
845 : : /* Check that the destination block we picked has only one pred. To relax this we
846 : : have to take special care when moving the statements. We don't currently support
847 : : such control flow however this check is there to simplify how we handle
848 : : labels that may be present anywhere in the IL. This check is to ensure that the
849 : : labels aren't significant for the CFG. */
850 : 113760 : if (!single_pred (dest_bb))
851 : 0 : return opt_result::failure_at (vect_location,
852 : : "chosen loop exit block (BB %d) does not have a "
853 : : "single predecessor which is currently not "
854 : : "supported for early break vectorization.\n",
855 : : dest_bb->index);
856 : :
857 : 113760 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo) = dest_bb;
858 : :
859 : 113760 : if (!LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).is_empty ())
860 : : {
861 : : /* All uses shall be updated to that of the first load. Entries are
862 : : stored in reverse order. */
863 : 105209 : tree vuse = gimple_vuse (LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).last ());
864 : 295940 : for (auto g : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo))
865 : : {
866 : 190731 : if (dump_enabled_p ())
867 : 2196 : dump_printf_loc (MSG_NOTE, vect_location,
868 : : "will update use: %T, mem_ref: %G", vuse, g);
869 : : }
870 : : }
871 : :
872 : 113760 : if (dump_enabled_p ())
873 : 1336 : dump_printf_loc (MSG_NOTE, vect_location,
874 : : "recorded statements to be moved to BB %d\n",
875 : 1336 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo)->index);
876 : :
877 : 113760 : return opt_result::success ();
878 : 113907 : }
879 : :
880 : : /* Function vect_analyze_data_ref_dependences.
881 : :
882 : : Examine all the data references in the loop, and make sure there do not
883 : : exist any data dependences between them. Set *MAX_VF according to
884 : : the maximum vectorization factor the data dependences allow. */
885 : :
886 : : opt_result
887 : 291423 : vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo,
888 : : unsigned int *max_vf)
889 : : {
890 : 291423 : unsigned int i;
891 : 291423 : struct data_dependence_relation *ddr;
892 : :
893 : 291423 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_dependences");
894 : :
895 : 291423 : if (!LOOP_VINFO_DDRS (loop_vinfo).exists ())
896 : : {
897 : 145780 : LOOP_VINFO_DDRS (loop_vinfo)
898 : 145780 : .create (LOOP_VINFO_DATAREFS (loop_vinfo).length ()
899 : 145780 : * LOOP_VINFO_DATAREFS (loop_vinfo).length ());
900 : : /* We do not need read-read dependences. */
901 : 291560 : bool res = compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
902 : : &LOOP_VINFO_DDRS (loop_vinfo),
903 : 145780 : LOOP_VINFO_LOOP_NEST (loop_vinfo),
904 : : false);
905 : 145780 : gcc_assert (res);
906 : : }
907 : :
908 : 291423 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
909 : :
910 : : /* For epilogues we either have no aliases or alias versioning
911 : : was applied to original loop. Therefore we may just get max_vf
912 : : using VF of original loop. */
913 : 291423 : if (LOOP_VINFO_EPILOGUE_P (loop_vinfo))
914 : 14012 : *max_vf = LOOP_VINFO_ORIG_MAX_VECT_FACTOR (loop_vinfo);
915 : : else
916 : 1233175 : FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
917 : : {
918 : 969654 : opt_result res
919 : 969654 : = vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf);
920 : 969654 : if (!res)
921 : 13890 : return res;
922 : : }
923 : :
924 : : /* If we have early break statements in the loop, check to see if they
925 : : are of a form we can vectorizer. */
926 : 277533 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
927 : 113907 : return vect_analyze_early_break_dependences (loop_vinfo);
928 : :
929 : 163626 : return opt_result::success ();
930 : : }
931 : :
932 : :
933 : : /* Function vect_slp_analyze_data_ref_dependence.
934 : :
935 : : Return TRUE if there (might) exist a dependence between a memory-reference
936 : : DRA and a memory-reference DRB for VINFO. When versioning for alias
937 : : may check a dependence at run-time, return FALSE. Adjust *MAX_VF
938 : : according to the data dependence. */
939 : :
940 : : static bool
941 : 6543007 : vect_slp_analyze_data_ref_dependence (vec_info *vinfo,
942 : : struct data_dependence_relation *ddr)
943 : : {
944 : 6543007 : struct data_reference *dra = DDR_A (ddr);
945 : 6543007 : struct data_reference *drb = DDR_B (ddr);
946 : 6543007 : dr_vec_info *dr_info_a = vinfo->lookup_dr (dra);
947 : 6543007 : dr_vec_info *dr_info_b = vinfo->lookup_dr (drb);
948 : :
949 : : /* We need to check dependences of statements marked as unvectorizable
950 : : as well, they still can prohibit vectorization. */
951 : :
952 : : /* Independent data accesses. */
953 : 6543007 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
954 : : return false;
955 : :
956 : 1183883 : if (dra == drb)
957 : : return false;
958 : :
959 : : /* Read-read is OK. */
960 : 9316 : if (DR_IS_READ (dra) && DR_IS_READ (drb))
961 : : return false;
962 : :
963 : : /* If dra and drb are part of the same interleaving chain consider
964 : : them independent. */
965 : 9316 : if (STMT_VINFO_GROUPED_ACCESS (dr_info_a->stmt)
966 : 9316 : && (DR_GROUP_FIRST_ELEMENT (dr_info_a->stmt)
967 : 9316 : == DR_GROUP_FIRST_ELEMENT (dr_info_b->stmt)))
968 : : return false;
969 : :
970 : : /* Unknown data dependence. */
971 : 9316 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
972 : : {
973 : 9316 : if (dump_enabled_p ())
974 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
975 : : "can't determine dependence between %T and %T\n",
976 : : DR_REF (dra), DR_REF (drb));
977 : : }
978 : 0 : else if (dump_enabled_p ())
979 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
980 : : "determined dependence between %T and %T\n",
981 : : DR_REF (dra), DR_REF (drb));
982 : :
983 : : return true;
984 : : }
985 : :
986 : :
987 : : /* Analyze dependences involved in the transform of a store SLP NODE. */
988 : :
989 : : static bool
990 : 733323 : vect_slp_analyze_store_dependences (vec_info *vinfo, slp_tree node)
991 : : {
992 : : /* This walks over all stmts involved in the SLP store done
993 : : in NODE verifying we can sink them up to the last stmt in the
994 : : group. */
995 : 733323 : stmt_vec_info last_access_info = vect_find_last_scalar_stmt_in_slp (node);
996 : 733323 : gcc_assert (DR_IS_WRITE (STMT_VINFO_DATA_REF (last_access_info)));
997 : :
998 : 2623339 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (node).length (); ++k)
999 : : {
1000 : 1899342 : stmt_vec_info access_info
1001 : 1899342 : = vect_orig_stmt (SLP_TREE_SCALAR_STMTS (node)[k]);
1002 : 1899342 : if (access_info == last_access_info)
1003 : 724775 : continue;
1004 : 1174567 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1005 : 1174567 : ao_ref ref;
1006 : 1174567 : bool ref_initialized_p = false;
1007 : 1174567 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1008 : 10708120 : gsi_stmt (gsi) != last_access_info->stmt; gsi_next (&gsi))
1009 : : {
1010 : 9542879 : gimple *stmt = gsi_stmt (gsi);
1011 : 16717010 : if (! gimple_vuse (stmt))
1012 : 2915558 : continue;
1013 : :
1014 : : /* If we couldn't record a (single) data reference for this
1015 : : stmt we have to resort to the alias oracle. */
1016 : 6627321 : stmt_vec_info stmt_info = vinfo->lookup_stmt (stmt);
1017 : 6627321 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1018 : 6627321 : if (!dr_b)
1019 : : {
1020 : : /* We are moving a store - this means
1021 : : we cannot use TBAA for disambiguation. */
1022 : 84592 : if (!ref_initialized_p)
1023 : 84592 : ao_ref_init (&ref, DR_REF (dr_a));
1024 : 84592 : if (stmt_may_clobber_ref_p_1 (stmt, &ref, false)
1025 : 84592 : || ref_maybe_used_by_stmt_p (stmt, &ref, false))
1026 : 9326 : return false;
1027 : 84543 : continue;
1028 : : }
1029 : :
1030 : 6542729 : gcc_assert (!gimple_visited_p (stmt));
1031 : :
1032 : 6542729 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1033 : 6542729 : dr_b, vNULL);
1034 : 6542729 : bool dependent = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1035 : 6542729 : free_dependence_relation (ddr);
1036 : 6542729 : if (dependent)
1037 : : return false;
1038 : : }
1039 : : }
1040 : : return true;
1041 : : }
1042 : :
1043 : : /* Analyze dependences involved in the transform of a load SLP NODE. STORES
1044 : : contain the vector of scalar stores of this instance if we are
1045 : : disambiguating the loads. */
1046 : :
1047 : : static bool
1048 : 162388 : vect_slp_analyze_load_dependences (vec_info *vinfo, slp_tree node,
1049 : : vec<stmt_vec_info> stores,
1050 : : stmt_vec_info last_store_info)
1051 : : {
1052 : : /* This walks over all stmts involved in the SLP load done
1053 : : in NODE verifying we can hoist them up to the first stmt in the
1054 : : group. */
1055 : 162388 : stmt_vec_info first_access_info = vect_find_first_scalar_stmt_in_slp (node);
1056 : 162388 : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (first_access_info)));
1057 : :
1058 : 565936 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (node).length (); ++k)
1059 : : {
1060 : 403615 : if (! SLP_TREE_SCALAR_STMTS (node)[k])
1061 : 168370 : continue;
1062 : 403615 : stmt_vec_info access_info
1063 : 403615 : = vect_orig_stmt (SLP_TREE_SCALAR_STMTS (node)[k]);
1064 : 403615 : if (access_info == first_access_info)
1065 : 168370 : continue;
1066 : 235245 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1067 : 235245 : ao_ref ref;
1068 : 235245 : bool ref_initialized_p = false;
1069 : 235245 : hash_set<stmt_vec_info> grp_visited;
1070 : 235245 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1071 : 4997639 : gsi_stmt (gsi) != first_access_info->stmt; gsi_prev (&gsi))
1072 : : {
1073 : 2381264 : gimple *stmt = gsi_stmt (gsi);
1074 : 3849555 : if (! gimple_vdef (stmt))
1075 : 2294075 : continue;
1076 : :
1077 : 352601 : stmt_vec_info stmt_info = vinfo->lookup_stmt (stmt);
1078 : :
1079 : : /* If we run into a store of this same instance (we've just
1080 : : marked those) then delay dependence checking until we run
1081 : : into the last store because this is where it will have
1082 : : been sunk to (and we verified that we can do that already). */
1083 : 352601 : if (gimple_visited_p (stmt))
1084 : : {
1085 : 265412 : if (stmt_info != last_store_info)
1086 : 265410 : continue;
1087 : :
1088 : 10 : for (stmt_vec_info &store_info : stores)
1089 : : {
1090 : 4 : data_reference *store_dr = STMT_VINFO_DATA_REF (store_info);
1091 : 4 : ddr_p ddr = initialize_data_dependence_relation
1092 : 4 : (dr_a, store_dr, vNULL);
1093 : 4 : bool dependent
1094 : 4 : = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1095 : 4 : free_dependence_relation (ddr);
1096 : 4 : if (dependent)
1097 : 67 : return false;
1098 : : }
1099 : 2 : continue;
1100 : 2 : }
1101 : :
1102 : 178094 : auto check_hoist = [&] (stmt_vec_info stmt_info) -> bool
1103 : : {
1104 : : /* We are hoisting a load - this means we can use TBAA for
1105 : : disambiguation. */
1106 : 90905 : if (!ref_initialized_p)
1107 : 90905 : ao_ref_init (&ref, DR_REF (dr_a));
1108 : 90905 : if (stmt_may_clobber_ref_p_1 (stmt_info->stmt, &ref, true))
1109 : : {
1110 : : /* If we couldn't record a (single) data reference for this
1111 : : stmt we have to give up now. */
1112 : 302 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1113 : 302 : if (!dr_b)
1114 : : return false;
1115 : 274 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1116 : 274 : dr_b, vNULL);
1117 : 274 : bool dependent
1118 : 274 : = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1119 : 274 : free_dependence_relation (ddr);
1120 : 274 : if (dependent)
1121 : : return false;
1122 : : }
1123 : : /* No dependence. */
1124 : : return true;
1125 : 87189 : };
1126 : 87189 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1127 : : {
1128 : : /* When we run into a store group we have to honor
1129 : : that earlier stores might be moved here. We don't
1130 : : know exactly which and where to since we lack a
1131 : : back-mapping from DR to SLP node, so assume all
1132 : : earlier stores are sunk here. It's enough to
1133 : : consider the last stmt of a group for this.
1134 : : ??? Both this and the fact that we disregard that
1135 : : the conflicting instance might be removed later
1136 : : is overly conservative. */
1137 : 67835 : if (!grp_visited.add (DR_GROUP_FIRST_ELEMENT (stmt_info)))
1138 : 12882 : for (auto store_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
1139 : 159093 : store_info != NULL;
1140 : 146211 : store_info = DR_GROUP_NEXT_ELEMENT (store_info))
1141 : 146250 : if ((store_info == stmt_info
1142 : 133377 : || get_later_stmt (store_info, stmt_info) == stmt_info)
1143 : 204928 : && !check_hoist (store_info))
1144 : : return false;
1145 : : }
1146 : : else
1147 : : {
1148 : 19354 : if (!check_hoist (stmt_info))
1149 : : return false;
1150 : : }
1151 : : }
1152 : 235245 : }
1153 : : return true;
1154 : : }
1155 : :
1156 : :
1157 : : /* Function vect_analyze_data_ref_dependences.
1158 : :
1159 : : Examine all the data references in the basic-block, and make sure there
1160 : : do not exist any data dependences between them. Set *MAX_VF according to
1161 : : the maximum vectorization factor the data dependences allow. */
1162 : :
1163 : : bool
1164 : 873001 : vect_slp_analyze_instance_dependence (vec_info *vinfo, slp_instance instance)
1165 : : {
1166 : 873001 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_dependence");
1167 : :
1168 : : /* The stores of this instance are at the root of the SLP tree. */
1169 : 873001 : slp_tree store = NULL;
1170 : 873001 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store)
1171 : 733323 : store = SLP_INSTANCE_TREE (instance);
1172 : :
1173 : : /* Verify we can sink stores to the vectorized stmt insert location. */
1174 : 733323 : stmt_vec_info last_store_info = NULL;
1175 : 733323 : if (store)
1176 : : {
1177 : 733323 : if (! vect_slp_analyze_store_dependences (vinfo, store))
1178 : : return false;
1179 : :
1180 : : /* Mark stores in this instance and remember the last one. */
1181 : 723997 : last_store_info = vect_find_last_scalar_stmt_in_slp (store);
1182 : 2613204 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (store).length (); ++k)
1183 : 1889207 : gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k]->stmt, true);
1184 : : }
1185 : :
1186 : 863675 : bool res = true;
1187 : :
1188 : : /* Verify we can sink loads to the vectorized stmt insert location,
1189 : : special-casing stores of this instance. */
1190 : 1281770 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1191 : 162388 : if (! vect_slp_analyze_load_dependences (vinfo, load,
1192 : : store
1193 : : ? SLP_TREE_SCALAR_STMTS (store)
1194 : : : vNULL, last_store_info))
1195 : : {
1196 : : res = false;
1197 : : break;
1198 : : }
1199 : :
1200 : : /* Unset the visited flag. */
1201 : 863675 : if (store)
1202 : 2613204 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (store).length (); ++k)
1203 : 1889207 : gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k]->stmt, false);
1204 : :
1205 : : /* If this is a SLP instance with a store check if there's a dependent
1206 : : load that cannot be forwarded from a previous iteration of a loop
1207 : : both are in. This is to avoid situations like that in PR115777. */
1208 : 863675 : if (res && store)
1209 : : {
1210 : 723946 : stmt_vec_info store_info
1211 : 723946 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (store)[0]);
1212 : 723946 : class loop *store_loop = gimple_bb (store_info->stmt)->loop_father;
1213 : 723946 : if (! loop_outer (store_loop))
1214 : 616018 : return res;
1215 : 107928 : vec<loop_p> loop_nest;
1216 : 107928 : loop_nest.create (1);
1217 : 107928 : loop_nest.quick_push (store_loop);
1218 : 107928 : data_reference *drs = nullptr;
1219 : 183879 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1220 : : {
1221 : 33554 : if (! STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (load)[0]))
1222 : 0 : continue;
1223 : 33554 : stmt_vec_info load_info
1224 : 33554 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (load)[0]);
1225 : 33554 : if (gimple_bb (load_info->stmt)->loop_father != store_loop)
1226 : 5738 : continue;
1227 : :
1228 : : /* For now concern ourselves with write-after-read as we also
1229 : : only look for re-use of the store within the same SLP instance.
1230 : : We can still get a RAW here when the instance contais a PHI
1231 : : with a backedge though, thus this test. */
1232 : 27816 : if (! vect_stmt_dominates_stmt_p (STMT_VINFO_STMT (load_info),
1233 : : STMT_VINFO_STMT (store_info)))
1234 : 10194 : continue;
1235 : :
1236 : 17622 : if (! drs)
1237 : : {
1238 : 16464 : drs = create_data_ref (loop_preheader_edge (store_loop),
1239 : : store_loop,
1240 : 16464 : DR_REF (STMT_VINFO_DATA_REF (store_info)),
1241 : : store_info->stmt, false, false);
1242 : 16464 : if (! DR_BASE_ADDRESS (drs)
1243 : 13168 : || TREE_CODE (DR_STEP (drs)) != INTEGER_CST)
1244 : : break;
1245 : : }
1246 : 14025 : data_reference *drl
1247 : 14025 : = create_data_ref (loop_preheader_edge (store_loop),
1248 : : store_loop,
1249 : 14025 : DR_REF (STMT_VINFO_DATA_REF (load_info)),
1250 : : load_info->stmt, true, false);
1251 : :
1252 : : /* See whether the DRs have a known constant distance throughout
1253 : : the containing loop iteration. */
1254 : 26493 : if (! DR_BASE_ADDRESS (drl)
1255 : 11553 : || ! operand_equal_p (DR_STEP (drs), DR_STEP (drl))
1256 : 8484 : || ! operand_equal_p (DR_BASE_ADDRESS (drs),
1257 : 8484 : DR_BASE_ADDRESS (drl))
1258 : 15586 : || ! operand_equal_p (DR_OFFSET (drs), DR_OFFSET (drl)))
1259 : : {
1260 : 12468 : free_data_ref (drl);
1261 : 12468 : continue;
1262 : : }
1263 : :
1264 : : /* If the next iteration load overlaps with a non-power-of-two offset
1265 : : we are surely failing any STLF attempt. */
1266 : 1557 : HOST_WIDE_INT step = TREE_INT_CST_LOW (DR_STEP (drl));
1267 : 1557 : unsigned HOST_WIDE_INT sizes
1268 : 1557 : = (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drs))))
1269 : 1557 : * DR_GROUP_SIZE (store_info));
1270 : 1557 : unsigned HOST_WIDE_INT sizel
1271 : 1557 : = (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drl))))
1272 : 1557 : * DR_GROUP_SIZE (load_info));
1273 : 1557 : if (ranges_overlap_p (TREE_INT_CST_LOW (DR_INIT (drl)) + step, sizel,
1274 : 1557 : TREE_INT_CST_LOW (DR_INIT (drs)), sizes))
1275 : : {
1276 : 869 : unsigned HOST_WIDE_INT dist
1277 : 869 : = absu_hwi (TREE_INT_CST_LOW (DR_INIT (drl)) + step
1278 : 869 : - TREE_INT_CST_LOW (DR_INIT (drs)));
1279 : 869 : poly_uint64 loadsz = tree_to_poly_uint64
1280 : 869 : (TYPE_SIZE_UNIT (SLP_TREE_VECTYPE (load)));
1281 : 869 : poly_uint64 storesz = tree_to_poly_uint64
1282 : 869 : (TYPE_SIZE_UNIT (SLP_TREE_VECTYPE (store)));
1283 : : /* When the overlap aligns with vector sizes used for the loads
1284 : : and the vector stores are larger or equal to the loads
1285 : : forwarding should work. */
1286 : 1738 : if (maybe_gt (loadsz, storesz) || ! multiple_p (dist, loadsz))
1287 : 74 : load->avoid_stlf_fail = true;
1288 : : }
1289 : 1557 : free_data_ref (drl);
1290 : : }
1291 : 107928 : if (drs)
1292 : 16464 : free_data_ref (drs);
1293 : 107928 : loop_nest.release ();
1294 : : }
1295 : :
1296 : : return res;
1297 : : }
1298 : :
1299 : : /* Return the misalignment of DR_INFO accessed in VECTYPE with OFFSET
1300 : : applied. */
1301 : :
1302 : : int
1303 : 7054202 : dr_misalignment (dr_vec_info *dr_info, tree vectype, poly_int64 offset)
1304 : : {
1305 : 7054202 : HOST_WIDE_INT diff = 0;
1306 : : /* Alignment is only analyzed for the first element of a DR group,
1307 : : use that but adjust misalignment by the offset of the access. */
1308 : 7054202 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
1309 : : {
1310 : 3438822 : dr_vec_info *first_dr
1311 : 3438822 : = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
1312 : : /* vect_analyze_data_ref_accesses guarantees that DR_INIT are
1313 : : INTEGER_CSTs and the first element in the group has the lowest
1314 : : address. */
1315 : 3438822 : diff = (TREE_INT_CST_LOW (DR_INIT (dr_info->dr))
1316 : 3438822 : - TREE_INT_CST_LOW (DR_INIT (first_dr->dr)));
1317 : 3438822 : gcc_assert (diff >= 0);
1318 : : dr_info = first_dr;
1319 : : }
1320 : :
1321 : 7054202 : int misalign = dr_info->misalignment;
1322 : 7054202 : gcc_assert (misalign != DR_MISALIGNMENT_UNINITIALIZED);
1323 : 7054202 : if (misalign == DR_MISALIGNMENT_UNKNOWN)
1324 : : return misalign;
1325 : :
1326 : : /* If the access is only aligned for a vector type with smaller alignment
1327 : : requirement the access has unknown misalignment. */
1328 : 4711988 : if (maybe_lt (dr_info->target_alignment * BITS_PER_UNIT,
1329 : 4711988 : targetm.vectorize.preferred_vector_alignment (vectype)))
1330 : : return DR_MISALIGNMENT_UNKNOWN;
1331 : :
1332 : : /* Apply the offset from the DR group start and the externally supplied
1333 : : offset which can for example result from a negative stride access. */
1334 : 4711983 : poly_int64 misalignment = misalign + diff + offset;
1335 : :
1336 : : /* Below we reject compile-time non-constant target alignments, but if
1337 : : our misalignment is zero, then we are known to already be aligned
1338 : : w.r.t. any such possible target alignment. */
1339 : 4711983 : if (known_eq (misalignment, 0))
1340 : : return 0;
1341 : :
1342 : 949389 : unsigned HOST_WIDE_INT target_alignment_c;
1343 : 949389 : if (!dr_info->target_alignment.is_constant (&target_alignment_c)
1344 : 949389 : || !known_misalignment (misalignment, target_alignment_c, &misalign))
1345 : : return DR_MISALIGNMENT_UNKNOWN;
1346 : 949389 : return misalign;
1347 : : }
1348 : :
1349 : : /* Record the base alignment guarantee given by DRB, which occurs
1350 : : in STMT_INFO. */
1351 : :
1352 : : static void
1353 : 4437229 : vect_record_base_alignment (vec_info *vinfo, stmt_vec_info stmt_info,
1354 : : innermost_loop_behavior *drb)
1355 : : {
1356 : 4437229 : bool existed;
1357 : 4437229 : std::pair<stmt_vec_info, innermost_loop_behavior *> &entry
1358 : 4437229 : = vinfo->base_alignments.get_or_insert (drb->base_address, &existed);
1359 : 4437229 : if (!existed || entry.second->base_alignment < drb->base_alignment)
1360 : : {
1361 : 1340629 : entry = std::make_pair (stmt_info, drb);
1362 : 1340629 : if (dump_enabled_p ())
1363 : 31521 : dump_printf_loc (MSG_NOTE, vect_location,
1364 : : "recording new base alignment for %T\n"
1365 : : " alignment: %d\n"
1366 : : " misalignment: %d\n"
1367 : : " based on: %G",
1368 : : drb->base_address,
1369 : : drb->base_alignment,
1370 : : drb->base_misalignment,
1371 : : stmt_info->stmt);
1372 : : }
1373 : 4437229 : }
1374 : :
1375 : : /* If the region we're going to vectorize is reached, all unconditional
1376 : : data references occur at least once. We can therefore pool the base
1377 : : alignment guarantees from each unconditional reference. Do this by
1378 : : going through all the data references in VINFO and checking whether
1379 : : the containing statement makes the reference unconditionally. If so,
1380 : : record the alignment of the base address in VINFO so that it can be
1381 : : used for all other references with the same base. */
1382 : :
1383 : : void
1384 : 979747 : vect_record_base_alignments (vec_info *vinfo)
1385 : : {
1386 : 979747 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1387 : 350060 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
1388 : 14613458 : for (data_reference *dr : vinfo->shared->datarefs)
1389 : : {
1390 : 11771263 : dr_vec_info *dr_info = vinfo->lookup_dr (dr);
1391 : 11771263 : stmt_vec_info stmt_info = dr_info->stmt;
1392 : 11771263 : if (!DR_IS_CONDITIONAL_IN_STMT (dr)
1393 : 11764709 : && STMT_VINFO_VECTORIZABLE (stmt_info)
1394 : 4450620 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1395 : : {
1396 : 4435736 : vect_record_base_alignment (vinfo, stmt_info, &DR_INNERMOST (dr));
1397 : :
1398 : : /* If DR is nested in the loop that is being vectorized, we can also
1399 : : record the alignment of the base wrt the outer loop. */
1400 : 12544431 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
1401 : 1493 : vect_record_base_alignment
1402 : 1493 : (vinfo, stmt_info, &STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info));
1403 : : }
1404 : : }
1405 : 979747 : }
1406 : :
1407 : : /* Function vect_compute_data_ref_alignment
1408 : :
1409 : : Compute the misalignment of the data reference DR_INFO when vectorizing
1410 : : with VECTYPE.
1411 : :
1412 : : Output:
1413 : : 1. initialized misalignment info for DR_INFO
1414 : :
1415 : : FOR NOW: No analysis is actually performed. Misalignment is calculated
1416 : : only for trivial cases. TODO. */
1417 : :
1418 : : static void
1419 : 1548741 : vect_compute_data_ref_alignment (vec_info *vinfo, dr_vec_info *dr_info,
1420 : : tree vectype)
1421 : : {
1422 : 1548741 : stmt_vec_info stmt_info = dr_info->stmt;
1423 : 1548741 : vec_base_alignments *base_alignments = &vinfo->base_alignments;
1424 : 1548741 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1425 : 1548741 : class loop *loop = NULL;
1426 : 1548741 : tree ref = DR_REF (dr_info->dr);
1427 : :
1428 : 1548741 : if (dump_enabled_p ())
1429 : 50529 : dump_printf_loc (MSG_NOTE, vect_location,
1430 : : "vect_compute_data_ref_alignment:\n");
1431 : :
1432 : 1548741 : if (loop_vinfo)
1433 : 687035 : loop = LOOP_VINFO_LOOP (loop_vinfo);
1434 : :
1435 : : /* Initialize misalignment to unknown. */
1436 : 1548741 : SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
1437 : :
1438 : 1548741 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1439 : : return;
1440 : :
1441 : 1532165 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
1442 : 1532165 : bool step_preserves_misalignment_p;
1443 : :
1444 : 1532165 : poly_uint64 vector_alignment
1445 : 1532165 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
1446 : : BITS_PER_UNIT);
1447 : :
1448 : 1532165 : if (loop_vinfo
1449 : 1532165 : && dr_safe_speculative_read_required (stmt_info))
1450 : : {
1451 : 261249 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1452 : 261249 : auto vectype_size
1453 : 261249 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1454 : 261249 : poly_uint64 new_alignment = vf * vectype_size;
1455 : : /* If we have a grouped access we require that the alignment be N * elem. */
1456 : 261249 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1457 : 61850 : new_alignment *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
1458 : :
1459 : 261249 : unsigned HOST_WIDE_INT target_alignment;
1460 : 261249 : if (new_alignment.is_constant (&target_alignment)
1461 : 522498 : && pow2p_hwi (target_alignment))
1462 : : {
1463 : 226829 : if (dump_enabled_p ())
1464 : : {
1465 : 3178 : dump_printf_loc (MSG_NOTE, vect_location,
1466 : : "alignment increased due to early break to ");
1467 : 3178 : dump_dec (MSG_NOTE, new_alignment);
1468 : 3178 : dump_printf (MSG_NOTE, " bytes.\n");
1469 : : }
1470 : : vector_alignment = target_alignment;
1471 : : }
1472 : : }
1473 : :
1474 : 1532165 : SET_DR_TARGET_ALIGNMENT (dr_info, vector_alignment);
1475 : :
1476 : : /* If the main loop has peeled for alignment we have no way of knowing
1477 : : whether the data accesses in the epilogues are aligned. We can't at
1478 : : compile time answer the question whether we have entered the main loop or
1479 : : not. Fixes PR 92351. */
1480 : 1532165 : if (loop_vinfo)
1481 : : {
1482 : 670459 : loop_vec_info orig_loop_vinfo = LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo);
1483 : 670459 : if (orig_loop_vinfo
1484 : 30831 : && LOOP_VINFO_PEELING_FOR_ALIGNMENT (orig_loop_vinfo) != 0)
1485 : : return;
1486 : : }
1487 : :
1488 : 1531942 : unsigned HOST_WIDE_INT vect_align_c;
1489 : 1531942 : if (!vector_alignment.is_constant (&vect_align_c))
1490 : : return;
1491 : :
1492 : : /* No step for BB vectorization. */
1493 : 1531942 : if (!loop)
1494 : : {
1495 : 861706 : gcc_assert (integer_zerop (drb->step));
1496 : : step_preserves_misalignment_p = true;
1497 : : }
1498 : :
1499 : : else
1500 : : {
1501 : : /* We can only use base and misalignment information relative to
1502 : : an innermost loop if the misalignment stays the same throughout the
1503 : : execution of the loop. As above, this is the case if the stride of
1504 : : the dataref evenly divides by the alignment. Make sure to check
1505 : : previous epilogues and the main loop. */
1506 : : step_preserves_misalignment_p = true;
1507 : : auto lvinfo = loop_vinfo;
1508 : 1371738 : while (lvinfo)
1509 : : {
1510 : 701502 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (lvinfo);
1511 : 701502 : step_preserves_misalignment_p
1512 : 701502 : &= multiple_p (drb->step_alignment * vf, vect_align_c);
1513 : 701502 : lvinfo = LOOP_VINFO_ORIG_LOOP_INFO (lvinfo);
1514 : : }
1515 : :
1516 : 670236 : if (!step_preserves_misalignment_p && dump_enabled_p ())
1517 : 271 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1518 : : "step doesn't divide the vector alignment.\n");
1519 : :
1520 : : /* In case the dataref is in an inner-loop of the loop that is being
1521 : : vectorized (LOOP), we use the base and misalignment information
1522 : : relative to the outer-loop (LOOP). This is ok only if the
1523 : : misalignment stays the same throughout the execution of the
1524 : : inner-loop, which is why we have to check that the stride of the
1525 : : dataref in the inner-loop evenly divides by the vector alignment. */
1526 : 670236 : if (step_preserves_misalignment_p
1527 : 670236 : && nested_in_vect_loop_p (loop, stmt_info))
1528 : : {
1529 : 1492 : step_preserves_misalignment_p
1530 : 1492 : = (DR_STEP_ALIGNMENT (dr_info->dr) % vect_align_c) == 0;
1531 : :
1532 : 1492 : if (dump_enabled_p ())
1533 : : {
1534 : 517 : if (step_preserves_misalignment_p)
1535 : 371 : dump_printf_loc (MSG_NOTE, vect_location,
1536 : : "inner step divides the vector alignment.\n");
1537 : : else
1538 : 146 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1539 : : "inner step doesn't divide the vector"
1540 : : " alignment.\n");
1541 : : }
1542 : : }
1543 : : }
1544 : :
1545 : 1531942 : unsigned int base_alignment = drb->base_alignment;
1546 : 1531942 : unsigned int base_misalignment = drb->base_misalignment;
1547 : :
1548 : : /* Calculate the maximum of the pooled base address alignment and the
1549 : : alignment that we can compute for DR itself. */
1550 : 1531942 : std::pair<stmt_vec_info, innermost_loop_behavior *> *entry
1551 : 1531942 : = base_alignments->get (drb->base_address);
1552 : 1531942 : if (entry
1553 : 1528638 : && base_alignment < (*entry).second->base_alignment
1554 : 1533959 : && (loop_vinfo
1555 : 1367 : || (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt_info->stmt),
1556 : 1367 : gimple_bb (entry->first->stmt))
1557 : 1096 : && (gimple_bb (stmt_info->stmt) != gimple_bb (entry->first->stmt)
1558 : 807 : || (entry->first->dr_aux.group <= dr_info->group)))))
1559 : : {
1560 : 1729 : base_alignment = entry->second->base_alignment;
1561 : 1729 : base_misalignment = entry->second->base_misalignment;
1562 : : }
1563 : :
1564 : 1531942 : if (drb->offset_alignment < vect_align_c
1565 : 1463329 : || !step_preserves_misalignment_p
1566 : : /* We need to know whether the step wrt the vectorized loop is
1567 : : negative when computing the starting misalignment below. */
1568 : 1453571 : || TREE_CODE (drb->step) != INTEGER_CST)
1569 : : {
1570 : 106074 : if (dump_enabled_p ())
1571 : 3446 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1572 : : "Unknown alignment for access: %T\n", ref);
1573 : 106074 : return;
1574 : : }
1575 : :
1576 : 1425868 : if (base_alignment < vect_align_c)
1577 : : {
1578 : 649931 : unsigned int max_alignment;
1579 : 649931 : tree base = get_base_for_alignment (drb->base_address, &max_alignment);
1580 : 649931 : if (max_alignment < vect_align_c
1581 : 648011 : || (loop_vinfo && LOOP_VINFO_EPILOGUE_P (loop_vinfo))
1582 : 1280358 : || !vect_can_force_dr_alignment_p (base,
1583 : 630427 : vect_align_c * BITS_PER_UNIT))
1584 : : {
1585 : 452003 : if (dump_enabled_p ())
1586 : 13246 : dump_printf_loc (MSG_NOTE, vect_location,
1587 : : "can't force alignment of ref: %T\n", ref);
1588 : 452003 : return;
1589 : : }
1590 : :
1591 : : /* Force the alignment of the decl.
1592 : : NOTE: This is the only change to the code we make during
1593 : : the analysis phase, before deciding to vectorize the loop. */
1594 : 197928 : if (dump_enabled_p ())
1595 : 8003 : dump_printf_loc (MSG_NOTE, vect_location,
1596 : : "force alignment of %T\n", ref);
1597 : :
1598 : 197928 : dr_info->base_decl = base;
1599 : 197928 : dr_info->base_misaligned = true;
1600 : 197928 : base_misalignment = 0;
1601 : : }
1602 : 973865 : poly_int64 misalignment
1603 : 973865 : = base_misalignment + wi::to_poly_offset (drb->init).force_shwi ();
1604 : :
1605 : 973865 : unsigned int const_misalignment;
1606 : 973865 : if (!known_misalignment (misalignment, vect_align_c, &const_misalignment))
1607 : : {
1608 : : if (dump_enabled_p ())
1609 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1610 : : "Non-constant misalignment for access: %T\n", ref);
1611 : : return;
1612 : : }
1613 : :
1614 : 973865 : SET_DR_MISALIGNMENT (dr_info, const_misalignment);
1615 : :
1616 : 973865 : if (dump_enabled_p ())
1617 : 32621 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1618 : : "misalign = %d bytes of ref %T\n",
1619 : : const_misalignment, ref);
1620 : :
1621 : : return;
1622 : : }
1623 : :
1624 : : /* Return whether DR_INFO, which is related to DR_PEEL_INFO in
1625 : : that it only differs in DR_INIT, is aligned if DR_PEEL_INFO
1626 : : is made aligned via peeling. */
1627 : :
1628 : : static bool
1629 : 1597328 : vect_dr_aligned_if_related_peeled_dr_is (dr_vec_info *dr_info,
1630 : : dr_vec_info *dr_peel_info)
1631 : : {
1632 : 1597328 : if (multiple_p (DR_TARGET_ALIGNMENT (dr_peel_info),
1633 : 1598405 : DR_TARGET_ALIGNMENT (dr_info)))
1634 : : {
1635 : 1596251 : poly_offset_int diff
1636 : 1596251 : = (wi::to_poly_offset (DR_INIT (dr_peel_info->dr))
1637 : 1596251 : - wi::to_poly_offset (DR_INIT (dr_info->dr)));
1638 : 1596251 : if (known_eq (diff, 0)
1639 : 1596251 : || multiple_p (diff, DR_TARGET_ALIGNMENT (dr_info)))
1640 : 552410 : return true;
1641 : : }
1642 : : return false;
1643 : : }
1644 : :
1645 : : /* Return whether DR_INFO is aligned if DR_PEEL_INFO is made
1646 : : aligned via peeling. */
1647 : :
1648 : : static bool
1649 : 160401 : vect_dr_aligned_if_peeled_dr_is (dr_vec_info *dr_info,
1650 : : dr_vec_info *dr_peel_info)
1651 : : {
1652 : 160401 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_info->dr),
1653 : 160401 : DR_BASE_ADDRESS (dr_peel_info->dr), 0)
1654 : 48311 : || !operand_equal_p (DR_OFFSET (dr_info->dr),
1655 : 48311 : DR_OFFSET (dr_peel_info->dr), 0)
1656 : 207884 : || !operand_equal_p (DR_STEP (dr_info->dr),
1657 : 47483 : DR_STEP (dr_peel_info->dr), 0))
1658 : 113283 : return false;
1659 : :
1660 : 47118 : return vect_dr_aligned_if_related_peeled_dr_is (dr_info, dr_peel_info);
1661 : : }
1662 : :
1663 : : /* Compute the value for dr_info->misalign so that the access appears
1664 : : aligned. This is used by peeling to compensate for dr_misalignment
1665 : : applying the offset for negative step. */
1666 : :
1667 : : int
1668 : 16630 : vect_dr_misalign_for_aligned_access (dr_vec_info *dr_info)
1669 : : {
1670 : 16630 : if (tree_int_cst_sgn (DR_STEP (dr_info->dr)) >= 0)
1671 : : return 0;
1672 : :
1673 : 160 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
1674 : 160 : poly_int64 misalignment
1675 : 160 : = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
1676 : 160 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1677 : :
1678 : 160 : unsigned HOST_WIDE_INT target_alignment_c;
1679 : 160 : int misalign;
1680 : 160 : if (!dr_info->target_alignment.is_constant (&target_alignment_c)
1681 : 160 : || !known_misalignment (misalignment, target_alignment_c, &misalign))
1682 : : return DR_MISALIGNMENT_UNKNOWN;
1683 : 160 : return misalign;
1684 : : }
1685 : :
1686 : : /* Function vect_update_misalignment_for_peel.
1687 : : Sets DR_INFO's misalignment
1688 : : - to 0 if it has the same alignment as DR_PEEL_INFO,
1689 : : - to the misalignment computed using NPEEL if DR_INFO's salignment is known,
1690 : : - to -1 (unknown) otherwise.
1691 : :
1692 : : DR_INFO - the data reference whose misalignment is to be adjusted.
1693 : : DR_PEEL_INFO - the data reference whose misalignment is being made
1694 : : zero in the vector loop by the peel.
1695 : : NPEEL - the number of iterations in the peel loop if the misalignment
1696 : : of DR_PEEL_INFO is known at compile time. */
1697 : :
1698 : : static void
1699 : 2415 : vect_update_misalignment_for_peel (dr_vec_info *dr_info,
1700 : : dr_vec_info *dr_peel_info, int npeel)
1701 : : {
1702 : : /* If dr_info is aligned of dr_peel_info is, then mark it so. */
1703 : 2415 : if (vect_dr_aligned_if_peeled_dr_is (dr_info, dr_peel_info))
1704 : : {
1705 : 456 : SET_DR_MISALIGNMENT (dr_info,
1706 : : vect_dr_misalign_for_aligned_access (dr_peel_info));
1707 : 456 : return;
1708 : : }
1709 : :
1710 : 1959 : unsigned HOST_WIDE_INT alignment;
1711 : 1959 : if (DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment)
1712 : 1959 : && known_alignment_for_access_p (dr_info,
1713 : 1959 : STMT_VINFO_VECTYPE (dr_info->stmt))
1714 : 197 : && known_alignment_for_access_p (dr_peel_info,
1715 : 197 : STMT_VINFO_VECTYPE (dr_peel_info->stmt)))
1716 : : {
1717 : 185 : int misal = dr_info->misalignment;
1718 : 185 : misal += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
1719 : 185 : misal &= alignment - 1;
1720 : 185 : set_dr_misalignment (dr_info, misal);
1721 : 185 : return;
1722 : : }
1723 : :
1724 : 1774 : if (dump_enabled_p ())
1725 : 29 : dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment " \
1726 : : "to unknown (-1).\n");
1727 : 1774 : SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
1728 : : }
1729 : :
1730 : : /* Return true if alignment is relevant for DR_INFO. */
1731 : :
1732 : : static bool
1733 : 1459855 : vect_relevant_for_alignment_p (dr_vec_info *dr_info)
1734 : : {
1735 : 1459855 : stmt_vec_info stmt_info = dr_info->stmt;
1736 : :
1737 : 1459855 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
1738 : : return false;
1739 : :
1740 : : /* For interleaving, only the alignment of the first access matters. */
1741 : 1458709 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1742 : 1659529 : && DR_GROUP_FIRST_ELEMENT (stmt_info) != stmt_info)
1743 : : return false;
1744 : :
1745 : : /* Scatter-gather and invariant accesses continue to address individual
1746 : : scalars, so vector-level alignment is irrelevant. */
1747 : 1369523 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1748 : 1369523 : || integer_zerop (DR_STEP (dr_info->dr)))
1749 : 43851 : return false;
1750 : :
1751 : : /* Strided accesses perform only component accesses, alignment is
1752 : : irrelevant for them. */
1753 : 1325672 : if (STMT_VINFO_STRIDED_P (stmt_info)
1754 : 1325672 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1755 : : return false;
1756 : :
1757 : : return true;
1758 : : }
1759 : :
1760 : : /* Given an memory reference EXP return whether its alignment is less
1761 : : than its size. */
1762 : :
1763 : : static bool
1764 : 1147915 : not_size_aligned (tree exp)
1765 : : {
1766 : 1147915 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
1767 : : return true;
1768 : :
1769 : 1147915 : return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
1770 : 1147915 : > get_object_alignment (exp));
1771 : : }
1772 : :
1773 : : /* Function vector_alignment_reachable_p
1774 : :
1775 : : Return true if vector alignment for DR_INFO is reachable by peeling
1776 : : a few loop iterations. Return false otherwise. */
1777 : :
1778 : : static bool
1779 : 492878 : vector_alignment_reachable_p (dr_vec_info *dr_info, poly_uint64 vf)
1780 : : {
1781 : 492878 : stmt_vec_info stmt_info = dr_info->stmt;
1782 : 492878 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1783 : 492878 : poly_uint64 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1784 : 985756 : poly_uint64 vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
1785 : 492878 : unsigned elem_size = vector_element_size (vector_size, nelements);
1786 : 492878 : unsigned group_size = 1;
1787 : :
1788 : 492878 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1789 : : {
1790 : : /* For interleaved access we peel only if number of iterations in
1791 : : the prolog loop ({VF - misalignment}), is a multiple of the
1792 : : number of the interleaved accesses. */
1793 : :
1794 : : /* FORNOW: handle only known alignment. */
1795 : 64245 : if (!known_alignment_for_access_p (dr_info, vectype))
1796 : 492878 : return false;
1797 : :
1798 : 35347 : unsigned mis_in_elements = dr_misalignment (dr_info, vectype) / elem_size;
1799 : 46610 : if (!multiple_p (nelements - mis_in_elements, DR_GROUP_SIZE (stmt_info)))
1800 : : return false;
1801 : :
1802 : 11263 : group_size = DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
1803 : : }
1804 : :
1805 : : /* If the vectorization factor does not guarantee DR advancement of
1806 : : a multiple of the target alignment no peeling will help. */
1807 : 439896 : if (!multiple_p (elem_size * group_size * vf, dr_target_alignment (dr_info)))
1808 : 76 : return false;
1809 : :
1810 : : /* If misalignment is known at the compile time then allow peeling
1811 : : only if natural alignment is reachable through peeling. */
1812 : 439820 : if (known_alignment_for_access_p (dr_info, vectype)
1813 : 692923 : && !aligned_access_p (dr_info, vectype))
1814 : : {
1815 : 12252 : HOST_WIDE_INT elmsize =
1816 : 12252 : int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1817 : 12252 : if (dump_enabled_p ())
1818 : : {
1819 : 787 : dump_printf_loc (MSG_NOTE, vect_location,
1820 : : "data size = %wd. misalignment = %d.\n", elmsize,
1821 : : dr_misalignment (dr_info, vectype));
1822 : : }
1823 : 12252 : if (dr_misalignment (dr_info, vectype) % elmsize)
1824 : : {
1825 : 53 : if (dump_enabled_p ())
1826 : 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1827 : : "data size does not divide the misalignment.\n");
1828 : 53 : return false;
1829 : : }
1830 : : }
1831 : :
1832 : 439767 : if (!known_alignment_for_access_p (dr_info, vectype))
1833 : : {
1834 : 186717 : tree type = TREE_TYPE (DR_REF (dr_info->dr));
1835 : 186717 : bool is_packed = not_size_aligned (DR_REF (dr_info->dr));
1836 : 186717 : if (dump_enabled_p ())
1837 : 13915 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1838 : : "Unknown misalignment, %snaturally aligned\n",
1839 : : is_packed ? "not " : "");
1840 : 186717 : return targetm.vectorize.vector_alignment_reachable (type, is_packed);
1841 : : }
1842 : :
1843 : : return true;
1844 : : }
1845 : :
1846 : :
1847 : : /* Calculate the cost of the memory access represented by DR_INFO. */
1848 : :
1849 : : static void
1850 : 568031 : vect_get_data_access_cost (vec_info *vinfo, dr_vec_info *dr_info,
1851 : : dr_alignment_support alignment_support_scheme,
1852 : : int misalignment,
1853 : : unsigned int *inside_cost,
1854 : : unsigned int *outside_cost,
1855 : : stmt_vector_for_cost *body_cost_vec,
1856 : : stmt_vector_for_cost *prologue_cost_vec)
1857 : : {
1858 : 568031 : stmt_vec_info stmt_info = dr_info->stmt;
1859 : 568031 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1860 : 568031 : int ncopies;
1861 : :
1862 : 568031 : if (PURE_SLP_STMT (stmt_info))
1863 : : ncopies = 1;
1864 : : else
1865 : 15730 : ncopies = vect_get_num_copies (loop_vinfo, STMT_VINFO_VECTYPE (stmt_info));
1866 : :
1867 : 568031 : if (DR_IS_READ (dr_info->dr))
1868 : 405291 : vect_get_load_cost (vinfo, stmt_info, NULL, ncopies,
1869 : : alignment_support_scheme, misalignment, true,
1870 : : inside_cost, outside_cost, prologue_cost_vec,
1871 : : body_cost_vec, false);
1872 : : else
1873 : 162740 : vect_get_store_cost (vinfo,stmt_info, NULL, ncopies,
1874 : : alignment_support_scheme, misalignment, inside_cost,
1875 : : body_cost_vec);
1876 : :
1877 : 568031 : if (dump_enabled_p ())
1878 : 26485 : dump_printf_loc (MSG_NOTE, vect_location,
1879 : : "vect_get_data_access_cost: inside_cost = %d, "
1880 : : "outside_cost = %d.\n", *inside_cost, *outside_cost);
1881 : 568031 : }
1882 : :
1883 : :
1884 : : typedef struct _vect_peel_info
1885 : : {
1886 : : dr_vec_info *dr_info;
1887 : : int npeel;
1888 : : unsigned int count;
1889 : : } *vect_peel_info;
1890 : :
1891 : : typedef struct _vect_peel_extended_info
1892 : : {
1893 : : vec_info *vinfo;
1894 : : struct _vect_peel_info peel_info;
1895 : : unsigned int inside_cost;
1896 : : unsigned int outside_cost;
1897 : : } *vect_peel_extended_info;
1898 : :
1899 : :
1900 : : /* Peeling hashtable helpers. */
1901 : :
1902 : : struct peel_info_hasher : free_ptr_hash <_vect_peel_info>
1903 : : {
1904 : : static inline hashval_t hash (const _vect_peel_info *);
1905 : : static inline bool equal (const _vect_peel_info *, const _vect_peel_info *);
1906 : : };
1907 : :
1908 : : inline hashval_t
1909 : 706427 : peel_info_hasher::hash (const _vect_peel_info *peel_info)
1910 : : {
1911 : 706427 : return (hashval_t) peel_info->npeel;
1912 : : }
1913 : :
1914 : : inline bool
1915 : 390628 : peel_info_hasher::equal (const _vect_peel_info *a, const _vect_peel_info *b)
1916 : : {
1917 : 390628 : return (a->npeel == b->npeel);
1918 : : }
1919 : :
1920 : :
1921 : : /* Insert DR_INFO into peeling hash table with NPEEL as key. */
1922 : :
1923 : : static void
1924 : 316615 : vect_peeling_hash_insert (hash_table<peel_info_hasher> *peeling_htab,
1925 : : loop_vec_info loop_vinfo, dr_vec_info *dr_info,
1926 : : int npeel, bool supportable_if_not_aligned)
1927 : : {
1928 : 316615 : struct _vect_peel_info elem, *slot;
1929 : 316615 : _vect_peel_info **new_slot;
1930 : :
1931 : 316615 : elem.npeel = npeel;
1932 : 316615 : slot = peeling_htab->find (&elem);
1933 : 316615 : if (slot)
1934 : 135346 : slot->count++;
1935 : : else
1936 : : {
1937 : 181269 : slot = XNEW (struct _vect_peel_info);
1938 : 181269 : slot->npeel = npeel;
1939 : 181269 : slot->dr_info = dr_info;
1940 : 181269 : slot->count = 1;
1941 : 181269 : new_slot = peeling_htab->find_slot (slot, INSERT);
1942 : 181269 : *new_slot = slot;
1943 : : }
1944 : :
1945 : : /* If this DR is not supported with unknown misalignment then bias
1946 : : this slot when the cost model is disabled. */
1947 : 316615 : if (!supportable_if_not_aligned
1948 : 316615 : && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1949 : 4880 : slot->count += VECT_MAX_COST;
1950 : 316615 : }
1951 : :
1952 : :
1953 : : /* Traverse peeling hash table to find peeling option that aligns maximum
1954 : : number of data accesses. */
1955 : :
1956 : : int
1957 : 38153 : vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1958 : : _vect_peel_extended_info *max)
1959 : : {
1960 : 38153 : vect_peel_info elem = *slot;
1961 : :
1962 : 38153 : if (elem->count > max->peel_info.count
1963 : 23542 : || (elem->count == max->peel_info.count
1964 : 18619 : && max->peel_info.npeel > elem->npeel))
1965 : : {
1966 : 14625 : max->peel_info.npeel = elem->npeel;
1967 : 14625 : max->peel_info.count = elem->count;
1968 : 14625 : max->peel_info.dr_info = elem->dr_info;
1969 : : }
1970 : :
1971 : 38153 : return 1;
1972 : : }
1973 : :
1974 : : /* Get the costs of peeling NPEEL iterations for LOOP_VINFO, checking
1975 : : data access costs for all data refs. If UNKNOWN_MISALIGNMENT is true,
1976 : : npeel is computed at runtime but DR0_INFO's misalignment will be zero
1977 : : after peeling. */
1978 : :
1979 : : static void
1980 : 317530 : vect_get_peeling_costs_all_drs (loop_vec_info loop_vinfo,
1981 : : dr_vec_info *dr0_info,
1982 : : unsigned int *inside_cost,
1983 : : unsigned int *outside_cost,
1984 : : stmt_vector_for_cost *body_cost_vec,
1985 : : stmt_vector_for_cost *prologue_cost_vec,
1986 : : unsigned int npeel)
1987 : : {
1988 : 317530 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1989 : :
1990 : 317530 : bool dr0_alignment_known_p
1991 : : = (dr0_info
1992 : 583790 : && known_alignment_for_access_p (dr0_info,
1993 : 266260 : STMT_VINFO_VECTYPE (dr0_info->stmt)));
1994 : :
1995 : 1555624 : for (data_reference *dr : datarefs)
1996 : : {
1997 : 603034 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
1998 : 603034 : if (!vect_relevant_for_alignment_p (dr_info))
1999 : 35003 : continue;
2000 : :
2001 : 568031 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2002 : 568031 : dr_alignment_support alignment_support_scheme;
2003 : 568031 : int misalignment;
2004 : 568031 : unsigned HOST_WIDE_INT alignment;
2005 : :
2006 : 568031 : bool negative = tree_int_cst_compare (DR_STEP (dr_info->dr),
2007 : 568031 : size_zero_node) < 0;
2008 : 568031 : poly_int64 off = 0;
2009 : 568031 : if (negative)
2010 : 20158 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2011 : 20158 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2012 : :
2013 : 568031 : if (npeel == 0)
2014 : 289449 : misalignment = dr_misalignment (dr_info, vectype, off);
2015 : 278582 : else if (dr_info == dr0_info
2016 : 278582 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2017 : : misalignment = 0;
2018 : 90041 : else if (!dr0_alignment_known_p
2019 : 7000 : || !known_alignment_for_access_p (dr_info, vectype)
2020 : 97041 : || !DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment))
2021 : : misalignment = DR_MISALIGNMENT_UNKNOWN;
2022 : : else
2023 : : {
2024 : 6014 : misalignment = dr_misalignment (dr_info, vectype, off);
2025 : 6014 : misalignment += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
2026 : 6014 : misalignment &= alignment - 1;
2027 : : }
2028 : 568031 : alignment_support_scheme
2029 : 568031 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2030 : : misalignment);
2031 : :
2032 : 568031 : vect_get_data_access_cost (loop_vinfo, dr_info,
2033 : : alignment_support_scheme, misalignment,
2034 : : inside_cost, outside_cost,
2035 : : body_cost_vec, prologue_cost_vec);
2036 : : }
2037 : 317530 : }
2038 : :
2039 : : /* Traverse peeling hash table and calculate cost for each peeling option.
2040 : : Find the one with the lowest cost. */
2041 : :
2042 : : int
2043 : 123059 : vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
2044 : : _vect_peel_extended_info *min)
2045 : : {
2046 : 123059 : vect_peel_info elem = *slot;
2047 : 123059 : int dummy;
2048 : 123059 : unsigned int inside_cost = 0, outside_cost = 0;
2049 : 123059 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (min->vinfo);
2050 : 123059 : stmt_vector_for_cost prologue_cost_vec, body_cost_vec,
2051 : : epilogue_cost_vec;
2052 : :
2053 : 123059 : prologue_cost_vec.create (2);
2054 : 123059 : body_cost_vec.create (2);
2055 : 123059 : epilogue_cost_vec.create (2);
2056 : :
2057 : 123059 : vect_get_peeling_costs_all_drs (loop_vinfo, elem->dr_info, &inside_cost,
2058 : : &outside_cost, &body_cost_vec,
2059 : 123059 : &prologue_cost_vec, elem->npeel);
2060 : :
2061 : 123059 : body_cost_vec.release ();
2062 : :
2063 : 246118 : outside_cost += vect_get_known_peeling_cost
2064 : 123059 : (loop_vinfo, elem->npeel, &dummy,
2065 : : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2066 : : &prologue_cost_vec, &epilogue_cost_vec);
2067 : :
2068 : : /* Prologue and epilogue costs are added to the target model later.
2069 : : These costs depend only on the scalar iteration cost, the
2070 : : number of peeling iterations finally chosen, and the number of
2071 : : misaligned statements. So discard the information found here. */
2072 : 123059 : prologue_cost_vec.release ();
2073 : 123059 : epilogue_cost_vec.release ();
2074 : :
2075 : 123059 : if (inside_cost < min->inside_cost
2076 : 1200 : || (inside_cost == min->inside_cost
2077 : 986 : && outside_cost < min->outside_cost))
2078 : : {
2079 : 121865 : min->inside_cost = inside_cost;
2080 : 121865 : min->outside_cost = outside_cost;
2081 : 121865 : min->peel_info.dr_info = elem->dr_info;
2082 : 121865 : min->peel_info.npeel = elem->npeel;
2083 : 121865 : min->peel_info.count = elem->count;
2084 : : }
2085 : :
2086 : 123059 : return 1;
2087 : : }
2088 : :
2089 : :
2090 : : /* Choose best peeling option by traversing peeling hash table and either
2091 : : choosing an option with the lowest cost (if cost model is enabled) or the
2092 : : option that aligns as many accesses as possible. */
2093 : :
2094 : : static struct _vect_peel_extended_info
2095 : 134987 : vect_peeling_hash_choose_best_peeling (hash_table<peel_info_hasher> *peeling_htab,
2096 : : loop_vec_info loop_vinfo)
2097 : : {
2098 : 134987 : struct _vect_peel_extended_info res;
2099 : :
2100 : 134987 : res.peel_info.dr_info = NULL;
2101 : 134987 : res.vinfo = loop_vinfo;
2102 : :
2103 : 134987 : if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2104 : : {
2105 : 120429 : res.inside_cost = INT_MAX;
2106 : 120429 : res.outside_cost = INT_MAX;
2107 : 120429 : peeling_htab->traverse <_vect_peel_extended_info *,
2108 : 243488 : vect_peeling_hash_get_lowest_cost> (&res);
2109 : : }
2110 : : else
2111 : : {
2112 : 14558 : res.peel_info.count = 0;
2113 : 14558 : peeling_htab->traverse <_vect_peel_extended_info *,
2114 : 52711 : vect_peeling_hash_get_most_frequent> (&res);
2115 : 14558 : res.inside_cost = 0;
2116 : 14558 : res.outside_cost = 0;
2117 : : }
2118 : :
2119 : 134987 : return res;
2120 : : }
2121 : :
2122 : : /* Return if vectorization is definitely, possibly, or unlikely to be
2123 : : supportable after loop peeling. */
2124 : :
2125 : : static enum peeling_support
2126 : 60795 : vect_peeling_supportable (loop_vec_info loop_vinfo, dr_vec_info *dr0_info,
2127 : : unsigned npeel)
2128 : : {
2129 : 60795 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2130 : 60795 : enum dr_alignment_support supportable_dr_alignment;
2131 : :
2132 : 60795 : bool dr0_alignment_known_p
2133 : 121590 : = known_alignment_for_access_p (dr0_info,
2134 : 60795 : STMT_VINFO_VECTYPE (dr0_info->stmt));
2135 : 60795 : bool has_unsupported_dr_p = false;
2136 : 60795 : unsigned int dr0_step = tree_to_shwi (DR_STEP (dr0_info->dr));
2137 : 60795 : int known_unsupported_misalignment = DR_MISALIGNMENT_UNKNOWN;
2138 : :
2139 : : /* Check if each data ref can be vectorized after peeling. */
2140 : 262080 : for (data_reference *dr : datarefs)
2141 : : {
2142 : 94044 : if (dr == dr0_info->dr)
2143 : 59894 : continue;
2144 : :
2145 : 34150 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2146 : 34150 : if (!vect_relevant_for_alignment_p (dr_info)
2147 : 34150 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2148 : 5136 : continue;
2149 : :
2150 : 29014 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2151 : 29014 : int misalignment;
2152 : 29014 : unsigned HOST_WIDE_INT alignment;
2153 : 29014 : if (!dr0_alignment_known_p
2154 : 1829 : || !known_alignment_for_access_p (dr_info, vectype)
2155 : 30843 : || !DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment))
2156 : : misalignment = DR_MISALIGNMENT_UNKNOWN;
2157 : : else
2158 : : {
2159 : 1815 : misalignment = dr_misalignment (dr_info, vectype);
2160 : 1815 : misalignment += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
2161 : 1815 : misalignment &= alignment - 1;
2162 : : }
2163 : 29014 : supportable_dr_alignment
2164 : 29014 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2165 : : misalignment);
2166 : 29014 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2167 : : {
2168 : 28150 : has_unsupported_dr_p = true;
2169 : :
2170 : : /* If unaligned unsupported DRs exist, we do following checks to see
2171 : : if they can be mutually aligned to support vectorization. If yes,
2172 : : we can try peeling and create a runtime (mutual alignment) check
2173 : : to guard the peeled loop. If no, return PEELING_UNSUPPORTED. */
2174 : :
2175 : : /* 1) If unaligned unsupported DRs have different alignment steps, the
2176 : : probability of DRs being mutually aligned is very low, and it's
2177 : : quite complex to check mutual alignment at runtime. We return
2178 : : PEELING_UNSUPPORTED in this case. */
2179 : 28150 : if (tree_to_shwi (DR_STEP (dr)) != dr0_step)
2180 : 60795 : return peeling_unsupported;
2181 : :
2182 : : /* 2) Based on above same alignment step condition, if one known
2183 : : misaligned DR has zero misalignment, or different misalignment
2184 : : amount from another known misaligned DR, peeling is unable to
2185 : : help make all these DRs aligned together. We won't try peeling
2186 : : with versioning anymore. */
2187 : 23838 : int curr_dr_misalignment = dr_misalignment (dr_info, vectype);
2188 : 23838 : if (curr_dr_misalignment == 0)
2189 : : return peeling_unsupported;
2190 : 13801 : if (known_unsupported_misalignment != DR_MISALIGNMENT_UNKNOWN)
2191 : : {
2192 : 8 : if (curr_dr_misalignment != DR_MISALIGNMENT_UNKNOWN
2193 : 8 : && curr_dr_misalignment != known_unsupported_misalignment)
2194 : : return peeling_unsupported;
2195 : : }
2196 : : else
2197 : : known_unsupported_misalignment = curr_dr_misalignment;
2198 : : }
2199 : : }
2200 : :
2201 : : /* Vectorization is known to be supportable with peeling alone when there is
2202 : : no unsupported DR. */
2203 : 46446 : return has_unsupported_dr_p ? peeling_maybe_supported
2204 : : : peeling_known_supported;
2205 : : }
2206 : :
2207 : : /* Compare two data-references DRA and DRB to group them into chunks
2208 : : with related alignment. */
2209 : :
2210 : : static int
2211 : 3673933 : dr_align_group_sort_cmp (const void *dra_, const void *drb_)
2212 : : {
2213 : 3673933 : data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2214 : 3673933 : data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2215 : 3673933 : int cmp;
2216 : :
2217 : : /* Stabilize sort. */
2218 : 3673933 : if (dra == drb)
2219 : : return 0;
2220 : :
2221 : : /* Ordering of DRs according to base. */
2222 : 3673933 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
2223 : : DR_BASE_ADDRESS (drb));
2224 : 3673933 : if (cmp != 0)
2225 : : return cmp;
2226 : :
2227 : : /* And according to DR_OFFSET. */
2228 : 1697106 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2229 : 1697106 : if (cmp != 0)
2230 : : return cmp;
2231 : :
2232 : : /* And after step. */
2233 : 1683709 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
2234 : 1683709 : if (cmp != 0)
2235 : : return cmp;
2236 : :
2237 : : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2238 : 1679190 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
2239 : 1679190 : if (cmp == 0)
2240 : 212116 : return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2241 : : return cmp;
2242 : : }
2243 : :
2244 : : /* Function vect_enhance_data_refs_alignment
2245 : :
2246 : : This pass will use loop versioning and loop peeling in order to enhance
2247 : : the alignment of data references in the loop.
2248 : :
2249 : : FOR NOW: we assume that whatever versioning/peeling takes place, only the
2250 : : original loop is to be vectorized. Any other loops that are created by
2251 : : the transformations performed in this pass - are not supposed to be
2252 : : vectorized. This restriction will be relaxed.
2253 : :
2254 : : This pass will require a cost model to guide it whether to apply peeling
2255 : : or versioning or a combination of the two. For example, the scheme that
2256 : : intel uses when given a loop with several memory accesses, is as follows:
2257 : : choose one memory access ('p') which alignment you want to force by doing
2258 : : peeling. Then, either (1) generate a loop in which 'p' is aligned and all
2259 : : other accesses are not necessarily aligned, or (2) use loop versioning to
2260 : : generate one loop in which all accesses are aligned, and another loop in
2261 : : which only 'p' is necessarily aligned.
2262 : :
2263 : : ("Automatic Intra-Register Vectorization for the Intel Architecture",
2264 : : Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
2265 : : Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
2266 : :
2267 : : Devising a cost model is the most critical aspect of this work. It will
2268 : : guide us on which access to peel for, whether to use loop versioning, how
2269 : : many versions to create, etc. The cost model will probably consist of
2270 : : generic considerations as well as target specific considerations (on
2271 : : powerpc for example, misaligned stores are more painful than misaligned
2272 : : loads).
2273 : :
2274 : : Here are the general steps involved in alignment enhancements:
2275 : :
2276 : : -- original loop, before alignment analysis:
2277 : : for (i=0; i<N; i++){
2278 : : x = q[i]; # DR_MISALIGNMENT(q) = unknown
2279 : : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2280 : : }
2281 : :
2282 : : -- After vect_compute_data_refs_alignment:
2283 : : for (i=0; i<N; i++){
2284 : : x = q[i]; # DR_MISALIGNMENT(q) = 3
2285 : : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2286 : : }
2287 : :
2288 : : -- Possibility 1: we do loop versioning:
2289 : : if (p is aligned) {
2290 : : for (i=0; i<N; i++){ # loop 1A
2291 : : x = q[i]; # DR_MISALIGNMENT(q) = 3
2292 : : p[i] = y; # DR_MISALIGNMENT(p) = 0
2293 : : }
2294 : : }
2295 : : else {
2296 : : for (i=0; i<N; i++){ # loop 1B
2297 : : x = q[i]; # DR_MISALIGNMENT(q) = 3
2298 : : p[i] = y; # DR_MISALIGNMENT(p) = unaligned
2299 : : }
2300 : : }
2301 : :
2302 : : -- Possibility 2: we do loop peeling:
2303 : : for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
2304 : : x = q[i];
2305 : : p[i] = y;
2306 : : }
2307 : : for (i = 3; i < N; i++){ # loop 2A
2308 : : x = q[i]; # DR_MISALIGNMENT(q) = 0
2309 : : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2310 : : }
2311 : :
2312 : : -- Possibility 3: combination of loop peeling and versioning:
2313 : : if (p & q are mutually aligned) {
2314 : : for (i=0; i<3; i++){ # (peeled loop iterations).
2315 : : x = q[i];
2316 : : p[i] = y;
2317 : : }
2318 : : for (i=3; i<N; i++){ # loop 3A
2319 : : x = q[i]; # DR_MISALIGNMENT(q) = 0
2320 : : p[i] = y; # DR_MISALIGNMENT(p) = 0
2321 : : }
2322 : : }
2323 : : else {
2324 : : for (i=0; i<N; i++){ # (scalar loop, not to be vectorized).
2325 : : x = q[i]; # DR_MISALIGNMENT(q) = 3
2326 : : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2327 : : }
2328 : : }
2329 : :
2330 : : These loops are later passed to loop_transform to be vectorized. The
2331 : : vectorizer will use the alignment information to guide the transformation
2332 : : (whether to generate regular loads/stores, or with special handling for
2333 : : misalignment). */
2334 : :
2335 : : opt_result
2336 : 319388 : vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
2337 : : {
2338 : 319388 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2339 : 319388 : dr_vec_info *first_store = NULL;
2340 : 319388 : dr_vec_info *dr0_info = NULL;
2341 : 319388 : struct data_reference *dr;
2342 : 319388 : unsigned int i;
2343 : 319388 : bool do_peeling = false;
2344 : 319388 : bool do_versioning = false;
2345 : 319388 : bool try_peeling_with_versioning = false;
2346 : 319388 : unsigned int npeel = 0;
2347 : 319388 : bool one_misalignment_known = false;
2348 : 319388 : bool one_misalignment_unknown = false;
2349 : 319388 : bool one_dr_unsupportable = false;
2350 : 319388 : dr_vec_info *unsupportable_dr_info = NULL;
2351 : 319388 : unsigned int dr0_same_align_drs = 0, first_store_same_align_drs = 0;
2352 : 319388 : hash_table<peel_info_hasher> peeling_htab (1);
2353 : :
2354 : 319388 : DUMP_VECT_SCOPE ("vect_enhance_data_refs_alignment");
2355 : :
2356 : : /* Reset data so we can safely be called multiple times. */
2357 : 319388 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
2358 : 319388 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = 0;
2359 : :
2360 : 319388 : if (LOOP_VINFO_DATAREFS (loop_vinfo).is_empty ())
2361 : 13277 : return opt_result::success ();
2362 : :
2363 : : /* Sort the vector of datarefs so DRs that have the same or dependent
2364 : : alignment are next to each other. */
2365 : 306111 : auto_vec<data_reference_p> datarefs
2366 : 306111 : = LOOP_VINFO_DATAREFS (loop_vinfo).copy ();
2367 : 306111 : datarefs.qsort (dr_align_group_sort_cmp);
2368 : :
2369 : : /* Compute the number of DRs that become aligned when we peel
2370 : : a dataref so it becomes aligned. */
2371 : 612222 : auto_vec<unsigned> n_same_align_refs (datarefs.length ());
2372 : 306111 : n_same_align_refs.quick_grow_cleared (datarefs.length ());
2373 : 306111 : unsigned i0;
2374 : 628102 : for (i0 = 0; i0 < datarefs.length (); ++i0)
2375 : 318760 : if (DR_BASE_ADDRESS (datarefs[i0]))
2376 : : break;
2377 : 1965422 : for (i = i0 + 1; i <= datarefs.length (); ++i)
2378 : : {
2379 : 676600 : if (i == datarefs.length ()
2380 : 373720 : || !operand_equal_p (DR_BASE_ADDRESS (datarefs[i0]),
2381 : 373720 : DR_BASE_ADDRESS (datarefs[i]), 0)
2382 : 176043 : || !operand_equal_p (DR_OFFSET (datarefs[i0]),
2383 : 176043 : DR_OFFSET (datarefs[i]), 0)
2384 : 851425 : || !operand_equal_p (DR_STEP (datarefs[i0]),
2385 : 174825 : DR_STEP (datarefs[i]), 0))
2386 : : {
2387 : : /* The subgroup [i0, i-1] now only differs in DR_INIT and
2388 : : possibly DR_TARGET_ALIGNMENT. Still the whole subgroup
2389 : : will get known misalignment if we align one of the refs
2390 : : with the largest DR_TARGET_ALIGNMENT. */
2391 : 1178888 : for (unsigned j = i0; j < i; ++j)
2392 : : {
2393 : 676600 : dr_vec_info *dr_infoj = loop_vinfo->lookup_dr (datarefs[j]);
2394 : 2903410 : for (unsigned k = i0; k < i; ++k)
2395 : : {
2396 : 2226810 : if (k == j)
2397 : 676600 : continue;
2398 : 1550210 : dr_vec_info *dr_infok = loop_vinfo->lookup_dr (datarefs[k]);
2399 : 1550210 : if (vect_dr_aligned_if_related_peeled_dr_is (dr_infok,
2400 : : dr_infoj))
2401 : 513023 : n_same_align_refs[j]++;
2402 : : }
2403 : : }
2404 : : i0 = i;
2405 : : }
2406 : : }
2407 : :
2408 : : /* While cost model enhancements are expected in the future, the high level
2409 : : view of the code at this time is as follows:
2410 : :
2411 : : A) If there is a misaligned access then see if doing peeling alone can
2412 : : make all data references satisfy vect_supportable_dr_alignment. If so,
2413 : : update data structures and return.
2414 : :
2415 : : B) If peeling alone wasn't possible and there is a data reference with an
2416 : : unknown misalignment that does not satisfy vect_supportable_dr_alignment
2417 : : then we may use either of the following two approaches.
2418 : :
2419 : : B1) Try peeling with versioning: Add a runtime loop versioning check to
2420 : : see if all unsupportable data references are mutually aligned, which
2421 : : means they will be uniformly aligned after a certain amount of loop
2422 : : peeling. If peeling and versioning can be used together, set
2423 : : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to TRUE and return.
2424 : :
2425 : : B2) Try versioning alone: Add a runtime loop versioning check to see if
2426 : : all unsupportable data references are already uniformly aligned
2427 : : without loop peeling. If versioning can be applied alone, set
2428 : : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to FALSE and return.
2429 : :
2430 : : Above B1 is more powerful and more likely to be adopted than B2. But B2
2431 : : is still available and useful in some cases, for example, the cost model
2432 : : does not allow much peeling.
2433 : :
2434 : : C) If none of above was successful then the alignment was not enhanced,
2435 : : just return. */
2436 : :
2437 : : /* (1) Peeling to force alignment. */
2438 : :
2439 : : /* (1.1) Decide whether to perform peeling, how many iterations to peel, and
2440 : : if vectorization may be supported by peeling with versioning.
2441 : : Considerations:
2442 : : - How many accesses will become aligned due to the peeling
2443 : : - How many accesses will become unaligned due to the peeling,
2444 : : and the cost of misaligned accesses.
2445 : : - The cost of peeling (the extra runtime checks, the increase
2446 : : in code size). */
2447 : :
2448 : 859081 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2449 : : {
2450 : 591828 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2451 : 591828 : if (!vect_relevant_for_alignment_p (dr_info))
2452 : 98950 : continue;
2453 : :
2454 : 492878 : stmt_vec_info stmt_info = dr_info->stmt;
2455 : 492878 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2456 : 492878 : do_peeling
2457 : 492878 : = vector_alignment_reachable_p (dr_info,
2458 : : LOOP_VINFO_VECT_FACTOR (loop_vinfo));
2459 : 492878 : if (do_peeling)
2460 : : {
2461 : 437802 : if (known_alignment_for_access_p (dr_info, vectype))
2462 : : {
2463 : 253050 : unsigned int npeel_tmp = 0;
2464 : 253050 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2465 : 253050 : size_zero_node) < 0;
2466 : :
2467 : : /* If known_alignment_for_access_p then we have set
2468 : : DR_MISALIGNMENT which is only done if we know it at compiler
2469 : : time, so it is safe to assume target alignment is constant.
2470 : : */
2471 : 253050 : unsigned int target_align =
2472 : 253050 : DR_TARGET_ALIGNMENT (dr_info).to_constant ();
2473 : 253050 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (dr_info);
2474 : 253050 : poly_int64 off = 0;
2475 : 253050 : if (negative)
2476 : 2156 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2477 : 253050 : unsigned int mis = dr_misalignment (dr_info, vectype, off);
2478 : 253050 : mis = negative ? mis : -mis;
2479 : 253050 : if (mis != 0)
2480 : 11415 : npeel_tmp = (mis & (target_align - 1)) / dr_size;
2481 : :
2482 : : /* For multiple types, it is possible that the bigger type access
2483 : : will have more than one peeling option. E.g., a loop with two
2484 : : types: one of size (vector size / 4), and the other one of
2485 : : size (vector size / 8). Vectorization factor will 8. If both
2486 : : accesses are misaligned by 3, the first one needs one scalar
2487 : : iteration to be aligned, and the second one needs 5. But the
2488 : : first one will be aligned also by peeling 5 scalar
2489 : : iterations, and in that case both accesses will be aligned.
2490 : : Hence, except for the immediate peeling amount, we also want
2491 : : to try to add full vector size, while we don't exceed
2492 : : vectorization factor.
2493 : : We do this automatically for cost model, since we calculate
2494 : : cost for every peeling option. */
2495 : 253050 : poly_uint64 nscalars = npeel_tmp;
2496 : 253050 : if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2497 : : {
2498 : 41492 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2499 : 41492 : unsigned group_size = 1;
2500 : 41492 : if (STMT_SLP_TYPE (stmt_info)
2501 : 41492 : && STMT_VINFO_GROUPED_ACCESS (stmt_info))
2502 : 1899 : group_size = DR_GROUP_SIZE (stmt_info);
2503 : 41492 : nscalars = vf * group_size;
2504 : : }
2505 : :
2506 : : /* Save info about DR in the hash table. Also include peeling
2507 : : amounts according to the explanation above. Indicate
2508 : : the alignment status when the ref is not aligned.
2509 : : ??? Rather than using unknown alignment here we should
2510 : : prune all entries from the peeling hashtable which cause
2511 : : DRs to be not supported. */
2512 : 253050 : bool supportable_if_not_aligned
2513 : : = vect_supportable_dr_alignment
2514 : 253050 : (loop_vinfo, dr_info, vectype, DR_MISALIGNMENT_UNKNOWN);
2515 : 569665 : while (known_le (npeel_tmp, nscalars))
2516 : : {
2517 : 316615 : vect_peeling_hash_insert (&peeling_htab, loop_vinfo,
2518 : : dr_info, npeel_tmp,
2519 : : supportable_if_not_aligned);
2520 : 316615 : npeel_tmp += MAX (1, target_align / dr_size);
2521 : : }
2522 : :
2523 : 253050 : one_misalignment_known = true;
2524 : : }
2525 : : else
2526 : : {
2527 : : /* If we don't know any misalignment values, we prefer
2528 : : peeling for data-ref that has the maximum number of data-refs
2529 : : with the same alignment, unless the target prefers to align
2530 : : stores over load. */
2531 : 184752 : unsigned same_align_drs = n_same_align_refs[i];
2532 : 184752 : if (!dr0_info
2533 : 184752 : || dr0_same_align_drs < same_align_drs)
2534 : : {
2535 : : dr0_same_align_drs = same_align_drs;
2536 : : dr0_info = dr_info;
2537 : : }
2538 : : /* For data-refs with the same number of related
2539 : : accesses prefer the one where the misalign
2540 : : computation will be invariant in the outermost loop. */
2541 : 58961 : else if (dr0_same_align_drs == same_align_drs)
2542 : : {
2543 : 58000 : class loop *ivloop0, *ivloop;
2544 : 58000 : ivloop0 = outermost_invariant_loop_for_expr
2545 : 58000 : (loop, DR_BASE_ADDRESS (dr0_info->dr));
2546 : 58000 : ivloop = outermost_invariant_loop_for_expr
2547 : 58000 : (loop, DR_BASE_ADDRESS (dr));
2548 : 58000 : if ((ivloop && !ivloop0)
2549 : 58000 : || (ivloop && ivloop0
2550 : 57994 : && flow_loop_nested_p (ivloop, ivloop0)))
2551 : : dr0_info = dr_info;
2552 : : }
2553 : :
2554 : 184752 : one_misalignment_unknown = true;
2555 : :
2556 : : /* Check for data refs with unsupportable alignment that
2557 : : can be peeled. */
2558 : 184752 : enum dr_alignment_support supportable_dr_alignment
2559 : 184752 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2560 : : DR_MISALIGNMENT_UNKNOWN);
2561 : 184752 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2562 : : {
2563 : 77008 : one_dr_unsupportable = true;
2564 : 77008 : unsupportable_dr_info = dr_info;
2565 : : }
2566 : :
2567 : 184752 : if (!first_store && DR_IS_WRITE (dr))
2568 : : {
2569 : 45278 : first_store = dr_info;
2570 : 45278 : first_store_same_align_drs = same_align_drs;
2571 : : }
2572 : : }
2573 : : }
2574 : : else
2575 : : {
2576 : 55076 : if (!aligned_access_p (dr_info, vectype))
2577 : : {
2578 : 38858 : if (dump_enabled_p ())
2579 : 1997 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2580 : : "vector alignment may not be reachable\n");
2581 : : break;
2582 : : }
2583 : : }
2584 : : }
2585 : :
2586 : : /* Check if we can possibly peel the loop. */
2587 : 306111 : if (!vect_can_advance_ivs_p (loop_vinfo)
2588 : 297728 : || !slpeel_can_duplicate_loop_p (loop, LOOP_VINFO_IV_EXIT (loop_vinfo),
2589 : 297728 : loop_preheader_edge (loop))
2590 : 297728 : || loop->inner
2591 : : /* We don't currently maintaing the LCSSA for prologue peeled inversed
2592 : : loops. */
2593 : 602219 : || LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
2594 : : do_peeling = false;
2595 : :
2596 : 306111 : struct _vect_peel_extended_info peel_for_known_alignment;
2597 : 306111 : struct _vect_peel_extended_info peel_for_unknown_alignment;
2598 : 306111 : struct _vect_peel_extended_info best_peel;
2599 : :
2600 : 306111 : peel_for_unknown_alignment.inside_cost = INT_MAX;
2601 : 306111 : peel_for_unknown_alignment.outside_cost = INT_MAX;
2602 : 306111 : peel_for_unknown_alignment.peel_info.count = 0;
2603 : :
2604 : 306111 : if (do_peeling
2605 : 306111 : && one_misalignment_unknown)
2606 : : {
2607 : : /* Check if the target requires to prefer stores over loads, i.e., if
2608 : : misaligned stores are more expensive than misaligned loads (taking
2609 : : drs with same alignment into account). */
2610 : 108072 : unsigned int load_inside_cost = 0;
2611 : 108072 : unsigned int load_outside_cost = 0;
2612 : 108072 : unsigned int store_inside_cost = 0;
2613 : 108072 : unsigned int store_outside_cost = 0;
2614 : 108072 : unsigned int estimated_npeels = vect_vf_for_cost (loop_vinfo) / 2;
2615 : :
2616 : 108072 : stmt_vector_for_cost dummy;
2617 : 108072 : dummy.create (2);
2618 : 108072 : vect_get_peeling_costs_all_drs (loop_vinfo, dr0_info,
2619 : : &load_inside_cost,
2620 : : &load_outside_cost,
2621 : : &dummy, &dummy, estimated_npeels);
2622 : 108072 : dummy.release ();
2623 : :
2624 : 108072 : if (first_store)
2625 : : {
2626 : 35129 : dummy.create (2);
2627 : 35129 : vect_get_peeling_costs_all_drs (loop_vinfo, first_store,
2628 : : &store_inside_cost,
2629 : : &store_outside_cost,
2630 : : &dummy, &dummy,
2631 : : estimated_npeels);
2632 : 35129 : dummy.release ();
2633 : : }
2634 : : else
2635 : : {
2636 : 72943 : store_inside_cost = INT_MAX;
2637 : 72943 : store_outside_cost = INT_MAX;
2638 : : }
2639 : :
2640 : 108072 : if (load_inside_cost > store_inside_cost
2641 : 108072 : || (load_inside_cost == store_inside_cost
2642 : 34848 : && load_outside_cost > store_outside_cost))
2643 : : {
2644 : 108072 : dr0_info = first_store;
2645 : 108072 : dr0_same_align_drs = first_store_same_align_drs;
2646 : 108072 : peel_for_unknown_alignment.inside_cost = store_inside_cost;
2647 : 108072 : peel_for_unknown_alignment.outside_cost = store_outside_cost;
2648 : : }
2649 : : else
2650 : : {
2651 : 108072 : peel_for_unknown_alignment.inside_cost = load_inside_cost;
2652 : 108072 : peel_for_unknown_alignment.outside_cost = load_outside_cost;
2653 : : }
2654 : :
2655 : 108072 : stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2656 : 108072 : prologue_cost_vec.create (2);
2657 : 108072 : epilogue_cost_vec.create (2);
2658 : :
2659 : 108072 : int dummy2;
2660 : 216144 : peel_for_unknown_alignment.outside_cost += vect_get_known_peeling_cost
2661 : 108072 : (loop_vinfo, estimated_npeels, &dummy2,
2662 : : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2663 : : &prologue_cost_vec, &epilogue_cost_vec);
2664 : :
2665 : 108072 : prologue_cost_vec.release ();
2666 : 108072 : epilogue_cost_vec.release ();
2667 : :
2668 : 108072 : peel_for_unknown_alignment.peel_info.count = dr0_same_align_drs + 1;
2669 : : }
2670 : :
2671 : 306111 : peel_for_unknown_alignment.peel_info.npeel = 0;
2672 : 306111 : peel_for_unknown_alignment.peel_info.dr_info = dr0_info;
2673 : :
2674 : 306111 : best_peel = peel_for_unknown_alignment;
2675 : :
2676 : 306111 : peel_for_known_alignment.inside_cost = INT_MAX;
2677 : 306111 : peel_for_known_alignment.outside_cost = INT_MAX;
2678 : 306111 : peel_for_known_alignment.peel_info.count = 0;
2679 : 306111 : peel_for_known_alignment.peel_info.dr_info = NULL;
2680 : :
2681 : 306111 : if (do_peeling && one_misalignment_known)
2682 : : {
2683 : : /* Peeling is possible, but there is no data access that is not supported
2684 : : unless aligned. So we try to choose the best possible peeling from
2685 : : the hash table. */
2686 : 134987 : peel_for_known_alignment = vect_peeling_hash_choose_best_peeling
2687 : 134987 : (&peeling_htab, loop_vinfo);
2688 : : }
2689 : :
2690 : : /* Compare costs of peeling for known and unknown alignment. */
2691 : 306111 : if (peel_for_known_alignment.peel_info.dr_info != NULL
2692 : 134987 : && peel_for_unknown_alignment.inside_cost
2693 : : >= peel_for_known_alignment.inside_cost)
2694 : : {
2695 : 122264 : best_peel = peel_for_known_alignment;
2696 : :
2697 : : /* If the best peeling for known alignment has NPEEL == 0, perform no
2698 : : peeling at all except if there is an unsupportable dr that we can
2699 : : align. */
2700 : 122264 : if (best_peel.peel_info.npeel == 0 && !one_dr_unsupportable)
2701 : : do_peeling = false;
2702 : : }
2703 : :
2704 : : /* If there is an unsupportable data ref, prefer this over all choices so far
2705 : : since we'd have to discard a chosen peeling except when it accidentally
2706 : : aligned the unsupportable data ref. */
2707 : 190200 : if (one_dr_unsupportable)
2708 : : dr0_info = unsupportable_dr_info;
2709 : 244055 : else if (do_peeling)
2710 : : {
2711 : : /* Calculate the penalty for no peeling, i.e. leaving everything as-is.
2712 : : TODO: Use nopeel_outside_cost or get rid of it? */
2713 : 51270 : unsigned nopeel_inside_cost = 0;
2714 : 51270 : unsigned nopeel_outside_cost = 0;
2715 : :
2716 : 51270 : stmt_vector_for_cost dummy;
2717 : 51270 : dummy.create (2);
2718 : 51270 : vect_get_peeling_costs_all_drs (loop_vinfo, NULL, &nopeel_inside_cost,
2719 : : &nopeel_outside_cost, &dummy, &dummy, 0);
2720 : 51270 : dummy.release ();
2721 : :
2722 : : /* Add epilogue costs. As we do not peel for alignment here, no prologue
2723 : : costs will be recorded. */
2724 : 51270 : stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2725 : 51270 : prologue_cost_vec.create (2);
2726 : 51270 : epilogue_cost_vec.create (2);
2727 : :
2728 : 51270 : int dummy2;
2729 : 102540 : nopeel_outside_cost += vect_get_known_peeling_cost
2730 : 51270 : (loop_vinfo, 0, &dummy2,
2731 : : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2732 : : &prologue_cost_vec, &epilogue_cost_vec);
2733 : :
2734 : 51270 : prologue_cost_vec.release ();
2735 : 51270 : epilogue_cost_vec.release ();
2736 : :
2737 : 51270 : npeel = best_peel.peel_info.npeel;
2738 : 51270 : dr0_info = best_peel.peel_info.dr_info;
2739 : :
2740 : : /* If no peeling is not more expensive than the best peeling we
2741 : : have so far, don't perform any peeling. */
2742 : 51270 : if (nopeel_inside_cost <= best_peel.inside_cost)
2743 : 45898 : do_peeling = false;
2744 : : }
2745 : :
2746 : 113326 : if (do_peeling)
2747 : : {
2748 : 60795 : stmt_vec_info stmt_info = dr0_info->stmt;
2749 : 60795 : if (known_alignment_for_access_p (dr0_info,
2750 : : STMT_VINFO_VECTYPE (stmt_info)))
2751 : : {
2752 : 5356 : bool negative = tree_int_cst_compare (DR_STEP (dr0_info->dr),
2753 : 5356 : size_zero_node) < 0;
2754 : 5356 : if (!npeel)
2755 : : {
2756 : : /* Since it's known at compile time, compute the number of
2757 : : iterations in the peeled loop (the peeling factor) for use in
2758 : : updating DR_MISALIGNMENT values. The peeling factor is the
2759 : : vectorization factor minus the misalignment as an element
2760 : : count. */
2761 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2762 : 0 : poly_int64 off = 0;
2763 : 0 : if (negative)
2764 : 0 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2765 : 0 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2766 : 0 : unsigned int mis
2767 : 0 : = dr_misalignment (dr0_info, vectype, off);
2768 : 0 : mis = negative ? mis : -mis;
2769 : : /* If known_alignment_for_access_p then we have set
2770 : : DR_MISALIGNMENT which is only done if we know it at compiler
2771 : : time, so it is safe to assume target alignment is constant.
2772 : : */
2773 : 0 : unsigned int target_align =
2774 : 0 : DR_TARGET_ALIGNMENT (dr0_info).to_constant ();
2775 : 0 : npeel = ((mis & (target_align - 1))
2776 : 0 : / vect_get_scalar_dr_size (dr0_info));
2777 : : }
2778 : :
2779 : : /* For interleaved data access every iteration accesses all the
2780 : : members of the group, therefore we divide the number of iterations
2781 : : by the group size. */
2782 : 5356 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2783 : 223 : npeel /= DR_GROUP_SIZE (stmt_info);
2784 : :
2785 : 5356 : if (dump_enabled_p ())
2786 : 264 : dump_printf_loc (MSG_NOTE, vect_location,
2787 : : "Try peeling by %d\n", npeel);
2788 : : }
2789 : :
2790 : : /* Check how peeling for alignment can support vectorization. Function
2791 : : vect_peeling_supportable returns one of the three possible values:
2792 : : - PEELING_KNOWN_SUPPORTED: indicates that we know all unsupported
2793 : : datarefs can be aligned after peeling. We can use peeling alone.
2794 : : - PEELING_MAYBE_SUPPORTED: indicates that peeling may be able to make
2795 : : these datarefs aligned but we are not sure about it at compile time.
2796 : : We will try peeling with versioning to add a runtime check to guard
2797 : : the peeled loop.
2798 : : - PEELING_UNSUPPORTED: indicates that peeling is almost impossible to
2799 : : support vectorization. We will stop trying peeling. */
2800 : 60795 : switch (vect_peeling_supportable (loop_vinfo, dr0_info, npeel))
2801 : : {
2802 : : case peeling_known_supported:
2803 : : break;
2804 : 12731 : case peeling_maybe_supported:
2805 : 12731 : try_peeling_with_versioning = true;
2806 : 12731 : break;
2807 : 14349 : case peeling_unsupported:
2808 : 14349 : do_peeling = false;
2809 : 14349 : break;
2810 : : }
2811 : :
2812 : : /* Check if all datarefs are supportable and log. */
2813 : 60795 : if (do_peeling
2814 : 60795 : && npeel == 0
2815 : 60795 : && known_alignment_for_access_p (dr0_info,
2816 : : STMT_VINFO_VECTYPE (stmt_info)))
2817 : 3 : return opt_result::success ();
2818 : :
2819 : : /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
2820 : 60792 : if (do_peeling)
2821 : : {
2822 : 46443 : unsigned max_allowed_peel
2823 : 46443 : = param_vect_max_peeling_for_alignment;
2824 : 46443 : if (loop_cost_model (loop) <= VECT_COST_MODEL_CHEAP)
2825 : : max_allowed_peel = 0;
2826 : 8109 : if (max_allowed_peel != (unsigned)-1)
2827 : : {
2828 : 38337 : unsigned max_peel = npeel;
2829 : 38337 : if (max_peel == 0)
2830 : : {
2831 : 35756 : poly_uint64 target_align = DR_TARGET_ALIGNMENT (dr0_info);
2832 : 35756 : unsigned HOST_WIDE_INT target_align_c;
2833 : 35756 : if (target_align.is_constant (&target_align_c))
2834 : 71512 : max_peel =
2835 : 35756 : target_align_c / vect_get_scalar_dr_size (dr0_info) - 1;
2836 : : else
2837 : : {
2838 : : do_peeling = false;
2839 : : if (dump_enabled_p ())
2840 : : dump_printf_loc (MSG_NOTE, vect_location,
2841 : : "Disable peeling, max peels set and vector"
2842 : : " alignment unknown\n");
2843 : : }
2844 : : }
2845 : 38337 : if (max_peel > max_allowed_peel)
2846 : : {
2847 : 38337 : do_peeling = false;
2848 : 38337 : if (dump_enabled_p ())
2849 : 51 : dump_printf_loc (MSG_NOTE, vect_location,
2850 : : "Disable peeling, max peels reached: %d\n", max_peel);
2851 : : }
2852 : : }
2853 : : }
2854 : :
2855 : : /* Cost model #2 - if peeling may result in a remaining loop not
2856 : : iterating enough to be vectorized then do not peel. Since this
2857 : : is a cost heuristic rather than a correctness decision, use the
2858 : : most likely runtime value for variable vectorization factors. */
2859 : 51 : if (do_peeling
2860 : 8106 : && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2861 : : {
2862 : 3370 : unsigned int assumed_vf = vect_vf_for_cost (loop_vinfo);
2863 : 3370 : unsigned int max_peel = npeel == 0 ? assumed_vf - 1 : npeel;
2864 : 3370 : if ((unsigned HOST_WIDE_INT) LOOP_VINFO_INT_NITERS (loop_vinfo)
2865 : 3370 : < assumed_vf + max_peel)
2866 : : do_peeling = false;
2867 : : }
2868 : :
2869 : : if (do_peeling)
2870 : : {
2871 : : /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
2872 : : If the misalignment of DR_i is identical to that of dr0 then set
2873 : : DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
2874 : : dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
2875 : : by the peeling factor times the element size of DR_i (MOD the
2876 : : vectorization factor times the size). Otherwise, the
2877 : : misalignment of DR_i must be set to unknown. */
2878 : 17703 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2879 : 10225 : if (dr != dr0_info->dr)
2880 : : {
2881 : 2747 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2882 : 2747 : if (!vect_relevant_for_alignment_p (dr_info))
2883 : 332 : continue;
2884 : :
2885 : 2415 : vect_update_misalignment_for_peel (dr_info, dr0_info, npeel);
2886 : : }
2887 : : }
2888 : :
2889 : 60792 : if (do_peeling && !try_peeling_with_versioning)
2890 : : {
2891 : : /* Update data structures if peeling will be applied alone. */
2892 : 6197 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
2893 : 6197 : if (npeel)
2894 : 1173 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
2895 : : else
2896 : 5024 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
2897 : 6197 : SET_DR_MISALIGNMENT (dr0_info,
2898 : : vect_dr_misalign_for_aligned_access (dr0_info));
2899 : 6197 : if (dump_enabled_p ())
2900 : : {
2901 : 281 : dump_printf_loc (MSG_NOTE, vect_location,
2902 : : "Alignment of access forced using peeling.\n");
2903 : 281 : dump_printf_loc (MSG_NOTE, vect_location,
2904 : : "Peeling for alignment will be applied.\n");
2905 : : }
2906 : :
2907 : : /* The inside-loop cost will be accounted for in vectorizable_load
2908 : : and vectorizable_store correctly with adjusted alignments.
2909 : : Drop the body_cst_vec on the floor here. */
2910 : 6197 : return opt_result::success ();
2911 : : }
2912 : : }
2913 : :
2914 : : /* (2) Versioning to force alignment. */
2915 : :
2916 : : /* Try versioning if:
2917 : : 1) optimize loop for speed and the cost-model is not cheap
2918 : : 2) there is at least one unsupported misaligned data ref with an unknown
2919 : : misalignment, and
2920 : : 3) all misaligned data refs with a known misalignment are supported, and
2921 : : 4) the number of runtime alignment checks is within reason. */
2922 : :
2923 : 299911 : do_versioning
2924 : 299911 : = (optimize_loop_nest_for_speed_p (loop)
2925 : 299468 : && !loop->inner /* FORNOW */
2926 : 597759 : && loop_cost_model (loop) > VECT_COST_MODEL_CHEAP);
2927 : :
2928 : : if (do_versioning)
2929 : : {
2930 : 301188 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2931 : : {
2932 : 228096 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2933 : 228096 : if (!vect_relevant_for_alignment_p (dr_info))
2934 : 166788 : continue;
2935 : :
2936 : 157736 : stmt_vec_info stmt_info = dr_info->stmt;
2937 : 157736 : if (STMT_VINFO_STRIDED_P (stmt_info))
2938 : : {
2939 : : do_versioning = false;
2940 : 5273 : break;
2941 : : }
2942 : :
2943 : 156899 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2944 : 156899 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2945 : 156899 : size_zero_node) < 0;
2946 : 156899 : poly_int64 off = 0;
2947 : 156899 : if (negative)
2948 : 2848 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2949 : 2848 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2950 : 156899 : int misalignment;
2951 : 156899 : if ((misalignment = dr_misalignment (dr_info, vectype, off)) == 0)
2952 : 96428 : continue;
2953 : :
2954 : 60471 : enum dr_alignment_support supportable_dr_alignment
2955 : 60471 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2956 : : misalignment);
2957 : 60471 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2958 : : {
2959 : 17765 : if (misalignment != DR_MISALIGNMENT_UNKNOWN
2960 : 17765 : || (LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
2961 : 13875 : >= (unsigned) param_vect_max_version_for_alignment_checks))
2962 : : {
2963 : : do_versioning = false;
2964 : 5273 : break;
2965 : : }
2966 : :
2967 : : /* At present we don't support versioning for alignment
2968 : : with variable VF, since there's no guarantee that the
2969 : : VF is a power of two. We could relax this if we added
2970 : : a way of enforcing a power-of-two size. */
2971 : 13329 : unsigned HOST_WIDE_INT size;
2972 : 26658 : if (!GET_MODE_SIZE (TYPE_MODE (vectype)).is_constant (&size))
2973 : : {
2974 : : do_versioning = false;
2975 : : break;
2976 : : }
2977 : :
2978 : : /* Forcing alignment in the first iteration is no good if
2979 : : we don't keep it across iterations. For now, just disable
2980 : : versioning in this case.
2981 : : ?? We could actually unroll the loop to achieve the required
2982 : : overall step alignment, and forcing the alignment could be
2983 : : done by doing some iterations of the non-vectorized loop. */
2984 : 13329 : if (!multiple_p (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2985 : 13329 : * DR_STEP_ALIGNMENT (dr),
2986 : 13329 : DR_TARGET_ALIGNMENT (dr_info)))
2987 : : {
2988 : : do_versioning = false;
2989 : : break;
2990 : : }
2991 : :
2992 : : /* The rightmost bits of an aligned address must be zeros.
2993 : : Construct the mask needed for this test. For example,
2994 : : GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
2995 : : mask must be 15 = 0xf. */
2996 : 13329 : int mask = size - 1;
2997 : :
2998 : : /* FORNOW: use the same mask to test all potentially unaligned
2999 : : references in the loop. */
3000 : 13329 : if (LOOP_VINFO_PTR_MASK (loop_vinfo)
3001 : 10053 : && LOOP_VINFO_PTR_MASK (loop_vinfo) != mask)
3002 : : {
3003 : : do_versioning = false;
3004 : : break;
3005 : : }
3006 : :
3007 : 13329 : LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
3008 : 13329 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (stmt_info);
3009 : : }
3010 : : }
3011 : :
3012 : : /* Versioning requires at least one misaligned data reference. */
3013 : 78365 : if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
3014 : : do_versioning = false;
3015 : 6515 : else if (!do_versioning)
3016 : 570 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
3017 : : }
3018 : :
3019 : : /* If we are trying peeling with versioning but versioning is disabled for
3020 : : some reason, peeling should be turned off together. */
3021 : 299911 : if (try_peeling_with_versioning && !do_versioning)
3022 : : do_peeling = false;
3023 : :
3024 : 288553 : if (do_versioning)
3025 : : {
3026 : : const vec<stmt_vec_info> &may_misalign_stmts
3027 : : = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
3028 : : stmt_vec_info stmt_info;
3029 : :
3030 : : /* It can now be assumed that the data references in the statements
3031 : : in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
3032 : : of the loop being vectorized. */
3033 : 15922 : FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt_info)
3034 : : {
3035 : 9977 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
3036 : 9977 : SET_DR_MISALIGNMENT (dr_info,
3037 : : vect_dr_misalign_for_aligned_access (dr_info));
3038 : 9977 : if (dump_enabled_p ())
3039 : 145 : dump_printf_loc (MSG_NOTE, vect_location,
3040 : : "Alignment of access forced using versioning.\n");
3041 : : }
3042 : :
3043 : 5945 : if (do_peeling)
3044 : : {
3045 : : /* This point is reached if peeling and versioning are used together
3046 : : to ensure alignment. Update data structures to make sure the loop
3047 : : is correctly peeled and a right runtime check is added for loop
3048 : : versioning. */
3049 : 1281 : gcc_assert (try_peeling_with_versioning);
3050 : 1281 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
3051 : 1281 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
3052 : 1281 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = true;
3053 : 1281 : if (dump_enabled_p ())
3054 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
3055 : : "Both peeling and versioning will be applied.\n");
3056 : : }
3057 : : else
3058 : : {
3059 : : /* This point is reached if versioning is used alone. */
3060 : 4664 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = false;
3061 : 4664 : if (dump_enabled_p ())
3062 : 80 : dump_printf_loc (MSG_NOTE, vect_location,
3063 : : "Versioning for alignment will be applied.\n");
3064 : : }
3065 : :
3066 : 5945 : return opt_result::success ();
3067 : : }
3068 : :
3069 : : /* This point is reached if neither peeling nor versioning is being done. */
3070 : 293966 : gcc_assert (! (do_peeling || do_versioning));
3071 : :
3072 : 293966 : return opt_result::success ();
3073 : 625499 : }
3074 : :
3075 : :
3076 : : /* Function vect_analyze_data_refs_alignment
3077 : :
3078 : : Analyze the alignment of the data-references in the loop.
3079 : : Return FALSE if a data reference is found that cannot be vectorized. */
3080 : :
3081 : : opt_result
3082 : 350060 : vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
3083 : : {
3084 : 350060 : DUMP_VECT_SCOPE ("vect_analyze_data_refs_alignment");
3085 : :
3086 : 350060 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3087 : 350060 : struct data_reference *dr;
3088 : 350060 : unsigned int i;
3089 : :
3090 : 350060 : vect_record_base_alignments (loop_vinfo);
3091 : 1143415 : FOR_EACH_VEC_ELT (datarefs, i, dr)
3092 : : {
3093 : 793355 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
3094 : 793355 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt))
3095 : : {
3096 : 793355 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt)
3097 : 1021271 : && DR_GROUP_FIRST_ELEMENT (dr_info->stmt) != dr_info->stmt)
3098 : 106320 : continue;
3099 : :
3100 : 687035 : vect_compute_data_ref_alignment (loop_vinfo, dr_info,
3101 : : STMT_VINFO_VECTYPE (dr_info->stmt));
3102 : : }
3103 : : }
3104 : :
3105 : 350060 : return opt_result::success ();
3106 : : }
3107 : :
3108 : :
3109 : : /* Analyze alignment of DRs of stmts in NODE. */
3110 : :
3111 : : static bool
3112 : 899574 : vect_slp_analyze_node_alignment (vec_info *vinfo, slp_tree node)
3113 : : {
3114 : : /* Alignment is maintained in the first element of the group. */
3115 : 899574 : stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
3116 : 899574 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (first_stmt_info);
3117 : 899574 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
3118 : 899574 : tree vectype = SLP_TREE_VECTYPE (node);
3119 : 899574 : poly_uint64 vector_alignment
3120 : 899574 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
3121 : : BITS_PER_UNIT);
3122 : 899574 : if (dr_info->misalignment == DR_MISALIGNMENT_UNINITIALIZED)
3123 : 861623 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3124 : : /* Re-analyze alignment when we're facing a vectorization with a bigger
3125 : : alignment requirement. */
3126 : 37951 : else if (known_lt (dr_info->target_alignment, vector_alignment))
3127 : : {
3128 : 83 : poly_uint64 old_target_alignment = dr_info->target_alignment;
3129 : 83 : int old_misalignment = dr_info->misalignment;
3130 : 83 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3131 : : /* But keep knowledge about a smaller alignment. */
3132 : 83 : if (old_misalignment != DR_MISALIGNMENT_UNKNOWN
3133 : 50 : && dr_info->misalignment == DR_MISALIGNMENT_UNKNOWN)
3134 : : {
3135 : 1 : dr_info->target_alignment = old_target_alignment;
3136 : 1 : dr_info->misalignment = old_misalignment;
3137 : : }
3138 : : }
3139 : : /* When we ever face unordered target alignments the first one wins in terms
3140 : : of analyzing and the other will become unknown in dr_misalignment. */
3141 : 899574 : return true;
3142 : : }
3143 : :
3144 : : /* Function vect_slp_analyze_instance_alignment
3145 : :
3146 : : Analyze the alignment of the data-references in the SLP instance.
3147 : : Return FALSE if a data reference is found that cannot be vectorized. */
3148 : :
3149 : : bool
3150 : 873001 : vect_slp_analyze_instance_alignment (vec_info *vinfo,
3151 : : slp_instance instance)
3152 : : {
3153 : 873001 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_alignment");
3154 : :
3155 : 873001 : slp_tree node;
3156 : 873001 : unsigned i;
3157 : 1039252 : FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, node)
3158 : 166251 : if (! vect_slp_analyze_node_alignment (vinfo, node))
3159 : : return false;
3160 : :
3161 : 873001 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
3162 : 873001 : && ! vect_slp_analyze_node_alignment
3163 : 733323 : (vinfo, SLP_INSTANCE_TREE (instance)))
3164 : : return false;
3165 : :
3166 : : return true;
3167 : : }
3168 : :
3169 : :
3170 : : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3171 : : accesses of legal size, step, etc. Detect gaps, single element
3172 : : interleaving, and other special cases. Set grouped access info.
3173 : : Collect groups of strided stores for further use in SLP analysis.
3174 : : Worker for vect_analyze_group_access. */
3175 : :
3176 : : static bool
3177 : 12502945 : vect_analyze_group_access_1 (vec_info *vinfo, dr_vec_info *dr_info)
3178 : : {
3179 : 12502945 : data_reference *dr = dr_info->dr;
3180 : 12502945 : tree step = DR_STEP (dr);
3181 : 12502945 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3182 : 12502945 : HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
3183 : 12502945 : stmt_vec_info stmt_info = dr_info->stmt;
3184 : 12502945 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3185 : 12502945 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3186 : 12502945 : HOST_WIDE_INT dr_step = -1;
3187 : 12502945 : HOST_WIDE_INT groupsize, last_accessed_element = 1;
3188 : 12502945 : bool slp_impossible = false;
3189 : :
3190 : : /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
3191 : : size of the interleaving group (including gaps). */
3192 : 12502945 : if (tree_fits_shwi_p (step))
3193 : : {
3194 : 12495726 : dr_step = tree_to_shwi (step);
3195 : : /* Check that STEP is a multiple of type size. Otherwise there is
3196 : : a non-element-sized gap at the end of the group which we
3197 : : cannot represent in DR_GROUP_GAP or DR_GROUP_SIZE.
3198 : : ??? As we can handle non-constant step fine here we should
3199 : : simply remove uses of DR_GROUP_GAP between the last and first
3200 : : element and instead rely on DR_STEP. DR_GROUP_SIZE then would
3201 : : simply not include that gap. */
3202 : 12495726 : if ((dr_step % type_size) != 0)
3203 : : {
3204 : 71 : if (dump_enabled_p ())
3205 : 27 : dump_printf_loc (MSG_NOTE, vect_location,
3206 : : "Step %T is not a multiple of the element size"
3207 : : " for %T\n",
3208 : : step, DR_REF (dr));
3209 : 71 : return false;
3210 : : }
3211 : 12495655 : groupsize = absu_hwi (dr_step) / type_size;
3212 : : }
3213 : : else
3214 : : groupsize = 0;
3215 : :
3216 : : /* Not consecutive access is possible only if it is a part of interleaving. */
3217 : 12502874 : if (!DR_GROUP_FIRST_ELEMENT (stmt_info))
3218 : : {
3219 : : /* Check if it this DR is a part of interleaving, and is a single
3220 : : element of the group that is accessed in the loop. */
3221 : :
3222 : : /* Gaps are supported only for loads. STEP must be a multiple of the type
3223 : : size. */
3224 : 8431116 : if (DR_IS_READ (dr)
3225 : 5044208 : && (dr_step % type_size) == 0
3226 : : && groupsize > 0
3227 : : /* This could be UINT_MAX but as we are generating code in a very
3228 : : inefficient way we have to cap earlier.
3229 : : See PR91403 for example. */
3230 : 5044208 : && groupsize <= 4096)
3231 : : {
3232 : 53709 : DR_GROUP_FIRST_ELEMENT (stmt_info) = stmt_info;
3233 : 53709 : DR_GROUP_SIZE (stmt_info) = groupsize;
3234 : 53709 : DR_GROUP_GAP (stmt_info) = groupsize - 1;
3235 : 53709 : if (dump_enabled_p ())
3236 : 1218 : dump_printf_loc (MSG_NOTE, vect_location,
3237 : : "Detected single element interleaving %T"
3238 : : " step %T\n",
3239 : : DR_REF (dr), step);
3240 : :
3241 : 53709 : return true;
3242 : : }
3243 : :
3244 : 8377407 : if (dump_enabled_p ())
3245 : 3068 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3246 : : "not consecutive access %G", stmt_info->stmt);
3247 : :
3248 : 8377407 : if (bb_vinfo)
3249 : : {
3250 : : /* Mark the statement as unvectorizable. */
3251 : 8360768 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
3252 : 8360768 : return true;
3253 : : }
3254 : :
3255 : 16639 : if (dump_enabled_p ())
3256 : 285 : dump_printf_loc (MSG_NOTE, vect_location, "using strided accesses\n");
3257 : 16639 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3258 : 16639 : return true;
3259 : : }
3260 : :
3261 : 4071758 : if (DR_GROUP_FIRST_ELEMENT (stmt_info) == stmt_info)
3262 : : {
3263 : : /* First stmt in the interleaving chain. Check the chain. */
3264 : 1488557 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3265 : 1488557 : struct data_reference *data_ref = dr;
3266 : 1488557 : unsigned int count = 1;
3267 : 1488557 : tree prev_init = DR_INIT (data_ref);
3268 : 1488557 : HOST_WIDE_INT diff, gaps = 0;
3269 : :
3270 : : /* By construction, all group members have INTEGER_CST DR_INITs. */
3271 : 4071767 : while (next)
3272 : : {
3273 : : /* We never have the same DR multiple times. */
3274 : 2583272 : gcc_assert (tree_int_cst_compare (DR_INIT (data_ref),
3275 : : DR_INIT (STMT_VINFO_DATA_REF (next))) != 0);
3276 : :
3277 : 2583272 : data_ref = STMT_VINFO_DATA_REF (next);
3278 : :
3279 : : /* All group members have the same STEP by construction. */
3280 : 2583272 : gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
3281 : :
3282 : : /* Check that the distance between two accesses is equal to the type
3283 : : size. Otherwise, we have gaps. */
3284 : 2583272 : diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
3285 : 2583272 : - TREE_INT_CST_LOW (prev_init)) / type_size;
3286 : 2583272 : if (diff < 1 || diff > UINT_MAX)
3287 : : {
3288 : : /* For artificial testcases with array accesses with large
3289 : : constant indices we can run into overflow issues which
3290 : : can end up fooling the groupsize constraint below so
3291 : : check the individual gaps (which are represented as
3292 : : unsigned int) as well. */
3293 : 0 : if (dump_enabled_p ())
3294 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3295 : : "interleaved access with gap larger "
3296 : : "than representable\n");
3297 : 0 : return false;
3298 : : }
3299 : 2583272 : if (diff != 1)
3300 : : {
3301 : : /* FORNOW: SLP of accesses with gaps is not supported. */
3302 : 102768 : slp_impossible = true;
3303 : 102768 : if (DR_IS_WRITE (data_ref))
3304 : : {
3305 : 62 : if (dump_enabled_p ())
3306 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3307 : : "interleaved store with gaps\n");
3308 : 62 : return false;
3309 : : }
3310 : :
3311 : 102706 : gaps += diff - 1;
3312 : : }
3313 : :
3314 : 2583210 : last_accessed_element += diff;
3315 : :
3316 : : /* Store the gap from the previous member of the group. If there is no
3317 : : gap in the access, DR_GROUP_GAP is always 1. */
3318 : 2583210 : DR_GROUP_GAP (next) = diff;
3319 : :
3320 : 2583210 : prev_init = DR_INIT (data_ref);
3321 : 2583210 : next = DR_GROUP_NEXT_ELEMENT (next);
3322 : : /* Count the number of data-refs in the chain. */
3323 : 2583210 : count++;
3324 : : }
3325 : :
3326 : 1488495 : if (groupsize == 0)
3327 : 1434358 : groupsize = count + gaps;
3328 : :
3329 : : /* This could be UINT_MAX but as we are generating code in a very
3330 : : inefficient way we have to cap earlier. See PR78699 for example. */
3331 : 1488495 : if (groupsize > 4096)
3332 : : {
3333 : 1 : if (dump_enabled_p ())
3334 : 1 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3335 : : "group is too large\n");
3336 : 1 : return false;
3337 : : }
3338 : :
3339 : : /* Check that the size of the interleaving is equal to count for stores,
3340 : : i.e., that there are no gaps. */
3341 : 1488494 : if (groupsize != count
3342 : 103790 : && !DR_IS_READ (dr))
3343 : : {
3344 : 8884 : groupsize = count;
3345 : 8884 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3346 : : }
3347 : :
3348 : : /* If there is a gap after the last load in the group it is the
3349 : : difference between the groupsize and the last accessed
3350 : : element.
3351 : : When there is no gap, this difference should be 0. */
3352 : 1488494 : DR_GROUP_GAP (stmt_info) = groupsize - last_accessed_element;
3353 : :
3354 : 1488494 : DR_GROUP_SIZE (stmt_info) = groupsize;
3355 : 1488494 : if (dump_enabled_p ())
3356 : : {
3357 : 7450 : dump_printf_loc (MSG_NOTE, vect_location,
3358 : : "Detected interleaving ");
3359 : 7450 : if (DR_IS_READ (dr))
3360 : 3963 : dump_printf (MSG_NOTE, "load ");
3361 : 3487 : else if (STMT_VINFO_STRIDED_P (stmt_info))
3362 : 472 : dump_printf (MSG_NOTE, "strided store ");
3363 : : else
3364 : 3015 : dump_printf (MSG_NOTE, "store ");
3365 : 7450 : dump_printf (MSG_NOTE, "of size %u\n",
3366 : : (unsigned)groupsize);
3367 : 7450 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", stmt_info->stmt);
3368 : 7450 : next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3369 : 36408 : while (next)
3370 : : {
3371 : 28958 : if (DR_GROUP_GAP (next) != 1)
3372 : 254 : dump_printf_loc (MSG_NOTE, vect_location,
3373 : : "\t<gap of %d elements>\n",
3374 : 254 : DR_GROUP_GAP (next) - 1);
3375 : 28958 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", next->stmt);
3376 : 28958 : next = DR_GROUP_NEXT_ELEMENT (next);
3377 : : }
3378 : 7450 : if (DR_GROUP_GAP (stmt_info) != 0)
3379 : 309 : dump_printf_loc (MSG_NOTE, vect_location,
3380 : : "\t<gap of %d elements>\n",
3381 : 309 : DR_GROUP_GAP (stmt_info));
3382 : : }
3383 : :
3384 : : /* SLP: create an SLP data structure for every interleaving group of
3385 : : stores for further analysis in vect_analyse_slp. */
3386 : 1488494 : if (DR_IS_WRITE (dr) && !slp_impossible)
3387 : : {
3388 : 904290 : if (loop_vinfo)
3389 : 21916 : LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt_info);
3390 : 904290 : if (bb_vinfo)
3391 : 882374 : BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt_info);
3392 : : }
3393 : : }
3394 : :
3395 : : return true;
3396 : : }
3397 : :
3398 : : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3399 : : accesses of legal size, step, etc. Detect gaps, single element
3400 : : interleaving, and other special cases. Set grouped access info.
3401 : : Collect groups of strided stores for further use in SLP analysis. */
3402 : :
3403 : : static bool
3404 : 12502945 : vect_analyze_group_access (vec_info *vinfo, dr_vec_info *dr_info)
3405 : : {
3406 : 12502945 : if (!vect_analyze_group_access_1 (vinfo, dr_info))
3407 : : {
3408 : : /* Dissolve the group if present. */
3409 : 134 : stmt_vec_info stmt_info = DR_GROUP_FIRST_ELEMENT (dr_info->stmt);
3410 : 365 : while (stmt_info)
3411 : : {
3412 : 231 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3413 : 231 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3414 : 231 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3415 : 231 : stmt_info = next;
3416 : : }
3417 : : return false;
3418 : : }
3419 : : return true;
3420 : : }
3421 : :
3422 : : /* Analyze the access pattern of the data-reference DR_INFO.
3423 : : In case of non-consecutive accesses call vect_analyze_group_access() to
3424 : : analyze groups of accesses. */
3425 : :
3426 : : static bool
3427 : 13010798 : vect_analyze_data_ref_access (vec_info *vinfo, dr_vec_info *dr_info)
3428 : : {
3429 : 13010798 : data_reference *dr = dr_info->dr;
3430 : 13010798 : tree step = DR_STEP (dr);
3431 : 13010798 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3432 : 13010798 : stmt_vec_info stmt_info = dr_info->stmt;
3433 : 13010798 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3434 : 13010798 : class loop *loop = NULL;
3435 : :
3436 : 13010798 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
3437 : : return true;
3438 : :
3439 : 12978486 : if (loop_vinfo)
3440 : 715221 : loop = LOOP_VINFO_LOOP (loop_vinfo);
3441 : :
3442 : 12978486 : if (loop_vinfo && !step)
3443 : : {
3444 : 0 : if (dump_enabled_p ())
3445 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3446 : : "bad data-ref access in loop\n");
3447 : 0 : return false;
3448 : : }
3449 : :
3450 : : /* Allow loads with zero step in inner-loop vectorization. */
3451 : 12978486 : if (loop_vinfo && integer_zerop (step))
3452 : : {
3453 : 12721 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3454 : 12721 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3455 : 12721 : if (!nested_in_vect_loop_p (loop, stmt_info))
3456 : 12454 : return DR_IS_READ (dr);
3457 : : /* Allow references with zero step for outer loops marked
3458 : : with pragma omp simd only - it guarantees absence of
3459 : : loop-carried dependencies between inner loop iterations. */
3460 : 267 : if (loop->safelen < 2)
3461 : : {
3462 : 231 : if (dump_enabled_p ())
3463 : 5 : dump_printf_loc (MSG_NOTE, vect_location,
3464 : : "zero step in inner loop of nest\n");
3465 : 231 : return false;
3466 : : }
3467 : : }
3468 : :
3469 : 12965765 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3470 : : {
3471 : : /* Interleaved accesses are not yet supported within outer-loop
3472 : : vectorization for references in the inner-loop. */
3473 : 5196 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3474 : 5196 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3475 : :
3476 : : /* For the rest of the analysis we use the outer-loop step. */
3477 : 5196 : step = STMT_VINFO_DR_STEP (stmt_info);
3478 : 5196 : if (integer_zerop (step))
3479 : : {
3480 : 1195 : if (dump_enabled_p ())
3481 : 228 : dump_printf_loc (MSG_NOTE, vect_location,
3482 : : "zero step in outer loop.\n");
3483 : 1195 : return DR_IS_READ (dr);
3484 : : }
3485 : : }
3486 : :
3487 : : /* Consecutive? */
3488 : 12964606 : if (TREE_CODE (step) == INTEGER_CST)
3489 : : {
3490 : 12929789 : HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
3491 : 12929789 : if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
3492 : 12929789 : || (dr_step < 0
3493 : 22021 : && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
3494 : : {
3495 : : /* Mark that it is not interleaving. */
3496 : 431582 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3497 : 431582 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3498 : 431582 : return true;
3499 : : }
3500 : : }
3501 : :
3502 : 12533024 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3503 : : {
3504 : 2892 : if (dump_enabled_p ())
3505 : 144 : dump_printf_loc (MSG_NOTE, vect_location,
3506 : : "grouped access in outer loop.\n");
3507 : 2892 : return false;
3508 : : }
3509 : :
3510 : :
3511 : : /* Assume this is a DR handled by non-constant strided load case. */
3512 : 12530132 : if (TREE_CODE (step) != INTEGER_CST)
3513 : 34406 : return (STMT_VINFO_STRIDED_P (stmt_info)
3514 : 34406 : && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
3515 : 7219 : || vect_analyze_group_access (vinfo, dr_info)));
3516 : :
3517 : : /* Not consecutive access - check if it's a part of interleaving group. */
3518 : 12495726 : return vect_analyze_group_access (vinfo, dr_info);
3519 : : }
3520 : :
3521 : : /* Compare two data-references DRA and DRB to group them into chunks
3522 : : suitable for grouping. */
3523 : :
3524 : : static int
3525 : 338580000 : dr_group_sort_cmp (const void *dra_, const void *drb_)
3526 : : {
3527 : 338580000 : dr_vec_info *dra_info = *(dr_vec_info **)const_cast<void *>(dra_);
3528 : 338580000 : dr_vec_info *drb_info = *(dr_vec_info **)const_cast<void *>(drb_);
3529 : 338580000 : data_reference_p dra = dra_info->dr;
3530 : 338580000 : data_reference_p drb = drb_info->dr;
3531 : 338580000 : int cmp;
3532 : :
3533 : : /* Stabilize sort. */
3534 : 338580000 : if (dra == drb)
3535 : : return 0;
3536 : :
3537 : : /* Different group IDs lead never belong to the same group. */
3538 : 338580000 : if (dra_info->group != drb_info->group)
3539 : 370630578 : return dra_info->group < drb_info->group ? -1 : 1;
3540 : :
3541 : : /* Ordering of DRs according to base. */
3542 : 94790787 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3543 : : DR_BASE_ADDRESS (drb));
3544 : 94790787 : if (cmp != 0)
3545 : : return cmp;
3546 : :
3547 : : /* And according to DR_OFFSET. */
3548 : 51123644 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
3549 : 51123644 : if (cmp != 0)
3550 : : return cmp;
3551 : :
3552 : : /* Put reads before writes. */
3553 : 50781122 : if (DR_IS_READ (dra) != DR_IS_READ (drb))
3554 : 4177363 : return DR_IS_READ (dra) ? -1 : 1;
3555 : :
3556 : : /* Then sort after access size. */
3557 : 47915522 : cmp = data_ref_compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
3558 : 47915522 : TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
3559 : 47915522 : if (cmp != 0)
3560 : : return cmp;
3561 : :
3562 : : /* And after step. */
3563 : 41556350 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
3564 : 41556350 : if (cmp != 0)
3565 : : return cmp;
3566 : :
3567 : : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
3568 : 41550067 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
3569 : 41550067 : if (cmp == 0)
3570 : 323141 : return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
3571 : : return cmp;
3572 : : }
3573 : :
3574 : : /* If OP is the result of a conversion, return the unconverted value,
3575 : : otherwise return null. */
3576 : :
3577 : : static tree
3578 : 179 : strip_conversion (tree op)
3579 : : {
3580 : 179 : if (TREE_CODE (op) != SSA_NAME)
3581 : : return NULL_TREE;
3582 : 179 : gimple *stmt = SSA_NAME_DEF_STMT (op);
3583 : 179 : if (!is_gimple_assign (stmt)
3584 : 179 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
3585 : : return NULL_TREE;
3586 : 68 : return gimple_assign_rhs1 (stmt);
3587 : : }
3588 : :
3589 : : /* Return true if vectorizable_* routines can handle statements STMT1_INFO
3590 : : and STMT2_INFO being in a single group. When ALLOW_SLP_P, masked loads can
3591 : : be grouped in SLP mode. */
3592 : :
3593 : : static bool
3594 : 6904051 : can_group_stmts_p (stmt_vec_info stmt1_info, stmt_vec_info stmt2_info,
3595 : : bool allow_slp_p)
3596 : : {
3597 : 6904051 : if (gimple_assign_single_p (stmt1_info->stmt))
3598 : 6903621 : return gimple_assign_single_p (stmt2_info->stmt);
3599 : :
3600 : 430 : gcall *call1 = dyn_cast <gcall *> (stmt1_info->stmt);
3601 : 430 : if (call1 && gimple_call_internal_p (call1))
3602 : : {
3603 : : /* Check for two masked loads or two masked stores. */
3604 : 571 : gcall *call2 = dyn_cast <gcall *> (stmt2_info->stmt);
3605 : 419 : if (!call2 || !gimple_call_internal_p (call2))
3606 : : return false;
3607 : 419 : internal_fn ifn = gimple_call_internal_fn (call1);
3608 : 419 : if (ifn != IFN_MASK_LOAD && ifn != IFN_MASK_STORE)
3609 : : return false;
3610 : 419 : if (ifn != gimple_call_internal_fn (call2))
3611 : : return false;
3612 : :
3613 : : /* Check that the masks are the same. Cope with casts of masks,
3614 : : like those created by build_mask_conversion. */
3615 : 419 : tree mask1 = gimple_call_arg (call1, 2);
3616 : 419 : tree mask2 = gimple_call_arg (call2, 2);
3617 : 419 : if (!operand_equal_p (mask1, mask2, 0) && !allow_slp_p)
3618 : : {
3619 : 145 : mask1 = strip_conversion (mask1);
3620 : 145 : if (!mask1)
3621 : : return false;
3622 : 34 : mask2 = strip_conversion (mask2);
3623 : 34 : if (!mask2)
3624 : : return false;
3625 : 34 : if (!operand_equal_p (mask1, mask2, 0))
3626 : : return false;
3627 : : }
3628 : 278 : return true;
3629 : : }
3630 : :
3631 : : return false;
3632 : : }
3633 : :
3634 : : /* Function vect_analyze_data_ref_accesses.
3635 : :
3636 : : Analyze the access pattern of all the data references in the loop.
3637 : :
3638 : : FORNOW: the only access pattern that is considered vectorizable is a
3639 : : simple step 1 (consecutive) access.
3640 : :
3641 : : FORNOW: handle only arrays and pointer accesses. */
3642 : :
3643 : : opt_result
3644 : 2701884 : vect_analyze_data_ref_accesses (vec_info *vinfo,
3645 : : vec<int> *dataref_groups)
3646 : : {
3647 : 2701884 : unsigned int i;
3648 : 2701884 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
3649 : :
3650 : 2701884 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_accesses");
3651 : :
3652 : 2701884 : if (datarefs.is_empty ())
3653 : 1192314 : return opt_result::success ();
3654 : :
3655 : : /* Sort the array of datarefs to make building the interleaving chains
3656 : : linear. Don't modify the original vector's order, it is needed for
3657 : : determining what dependencies are reversed. */
3658 : 1509570 : vec<dr_vec_info *> datarefs_copy;
3659 : 1509570 : datarefs_copy.create (datarefs.length ());
3660 : 16228551 : for (unsigned i = 0; i < datarefs.length (); i++)
3661 : : {
3662 : 14718981 : dr_vec_info *dr_info = vinfo->lookup_dr (datarefs[i]);
3663 : : /* If the caller computed DR grouping use that, otherwise group by
3664 : : basic blocks. */
3665 : 14718981 : if (dataref_groups)
3666 : 13959580 : dr_info->group = (*dataref_groups)[i];
3667 : : else
3668 : 759401 : dr_info->group = gimple_bb (DR_STMT (datarefs[i]))->index;
3669 : 14718981 : datarefs_copy.quick_push (dr_info);
3670 : : }
3671 : 1509570 : datarefs_copy.qsort (dr_group_sort_cmp);
3672 : 1509570 : hash_set<stmt_vec_info> to_fixup;
3673 : :
3674 : : /* Build the interleaving chains. */
3675 : 13863526 : for (i = 0; i < datarefs_copy.length () - 1;)
3676 : : {
3677 : 10844386 : dr_vec_info *dr_info_a = datarefs_copy[i];
3678 : 10844386 : data_reference_p dra = dr_info_a->dr;
3679 : 10844386 : int dra_group_id = dr_info_a->group;
3680 : 10844386 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
3681 : 10844386 : stmt_vec_info lastinfo = NULL;
3682 : 10844386 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
3683 : 9280370 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a))
3684 : : {
3685 : 1590158 : ++i;
3686 : 1590158 : continue;
3687 : : }
3688 : 24224217 : for (i = i + 1; i < datarefs_copy.length (); ++i)
3689 : : {
3690 : 11619253 : dr_vec_info *dr_info_b = datarefs_copy[i];
3691 : 11619253 : data_reference_p drb = dr_info_b->dr;
3692 : 11619253 : int drb_group_id = dr_info_b->group;
3693 : 11619253 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
3694 : 11619253 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_b)
3695 : 11317361 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
3696 : : break;
3697 : :
3698 : : /* ??? Imperfect sorting (non-compatible types, non-modulo
3699 : : accesses, same accesses) can lead to a group to be artificially
3700 : : split here as we don't just skip over those. If it really
3701 : : matters we can push those to a worklist and re-iterate
3702 : : over them. The we can just skip ahead to the next DR here. */
3703 : :
3704 : : /* DRs in a different DR group should not be put into the same
3705 : : interleaving group. */
3706 : 11314409 : if (dra_group_id != drb_group_id)
3707 : : break;
3708 : :
3709 : : /* Check that the data-refs have same first location (except init)
3710 : : and they are both either store or load (not load and store,
3711 : : not masked loads or stores). */
3712 : 7144277 : if (DR_IS_READ (dra) != DR_IS_READ (drb)
3713 : 5875689 : || data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3714 : : DR_BASE_ADDRESS (drb)) != 0
3715 : 4309290 : || data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb)) != 0
3716 : 11432325 : || !can_group_stmts_p (stmtinfo_a, stmtinfo_b, true))
3717 : : break;
3718 : :
3719 : : /* Check that the data-refs have the same constant size. */
3720 : 4288031 : tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
3721 : 4288031 : tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
3722 : 4288031 : if (!tree_fits_uhwi_p (sza)
3723 : 4288031 : || !tree_fits_uhwi_p (szb)
3724 : 8576062 : || !tree_int_cst_equal (sza, szb))
3725 : : break;
3726 : :
3727 : : /* Check that the data-refs have the same step. */
3728 : 3947696 : if (data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb)) != 0)
3729 : : break;
3730 : :
3731 : : /* Check the types are compatible.
3732 : : ??? We don't distinguish this during sorting. */
3733 : 3947066 : if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
3734 : 3947066 : TREE_TYPE (DR_REF (drb))))
3735 : : break;
3736 : :
3737 : : /* Check that the DR_INITs are compile-time constants. */
3738 : 2817458 : if (!tree_fits_shwi_p (DR_INIT (dra))
3739 : 2817458 : || !tree_fits_shwi_p (DR_INIT (drb)))
3740 : : break;
3741 : :
3742 : : /* Different .GOMP_SIMD_LANE calls still give the same lane,
3743 : : just hold extra information. */
3744 : 2817458 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_a)
3745 : 1240 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_b)
3746 : 2818698 : && data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb)) == 0)
3747 : : break;
3748 : :
3749 : : /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
3750 : 2816218 : HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
3751 : 2816218 : HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
3752 : 2816218 : HOST_WIDE_INT init_prev
3753 : 2816218 : = TREE_INT_CST_LOW (DR_INIT (datarefs_copy[i-1]->dr));
3754 : 2816218 : gcc_assert (init_a <= init_b
3755 : : && init_a <= init_prev
3756 : : && init_prev <= init_b);
3757 : :
3758 : : /* Do not place the same access in the interleaving chain twice. */
3759 : 2816218 : if (init_b == init_prev)
3760 : : {
3761 : 28822 : gcc_assert (gimple_uid (DR_STMT (datarefs_copy[i-1]->dr))
3762 : : < gimple_uid (DR_STMT (drb)));
3763 : : /* Simply link in duplicates and fix up the chain below. */
3764 : : }
3765 : : else
3766 : : {
3767 : : /* If init_b == init_a + the size of the type * k, we have an
3768 : : interleaving, and DRA is accessed before DRB. */
3769 : 2787396 : unsigned HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
3770 : 2787396 : if (type_size_a == 0
3771 : 2787396 : || (((unsigned HOST_WIDE_INT)init_b - init_a)
3772 : 2787396 : % type_size_a != 0))
3773 : : break;
3774 : :
3775 : : /* If we have a store, the accesses are adjacent. This splits
3776 : : groups into chunks we support (we don't support vectorization
3777 : : of stores with gaps). */
3778 : 2786337 : if (!DR_IS_READ (dra)
3779 : 1825691 : && (((unsigned HOST_WIDE_INT)init_b - init_prev)
3780 : : != type_size_a))
3781 : : break;
3782 : :
3783 : : /* For datarefs with big gap, it's better to split them into different
3784 : : groups.
3785 : : .i.e a[0], a[1], a[2], .. a[7], a[100], a[101],..., a[107] */
3786 : 2609222 : if ((unsigned HOST_WIDE_INT)(init_b - init_prev)
3787 : : > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
3788 : : break;
3789 : :
3790 : : /* If the step (if not zero or non-constant) is smaller than the
3791 : : difference between data-refs' inits this splits groups into
3792 : : suitable sizes. */
3793 : 2600981 : if (tree_fits_shwi_p (DR_STEP (dra)))
3794 : : {
3795 : 2596423 : unsigned HOST_WIDE_INT step
3796 : 2596423 : = absu_hwi (tree_to_shwi (DR_STEP (dra)));
3797 : 2596423 : if (step != 0
3798 : 125241 : && step <= ((unsigned HOST_WIDE_INT)init_b - init_a))
3799 : : break;
3800 : : }
3801 : : }
3802 : :
3803 : 2616033 : if (dump_enabled_p ())
3804 : 29642 : dump_printf_loc (MSG_NOTE, vect_location,
3805 : 29642 : DR_IS_READ (dra)
3806 : : ? "Detected interleaving load %T and %T\n"
3807 : : : "Detected interleaving store %T and %T\n",
3808 : : DR_REF (dra), DR_REF (drb));
3809 : :
3810 : : /* Link the found element into the group list. */
3811 : 2616033 : if (!DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3812 : : {
3813 : 1466615 : DR_GROUP_FIRST_ELEMENT (stmtinfo_a) = stmtinfo_a;
3814 : 1466615 : lastinfo = stmtinfo_a;
3815 : : }
3816 : 2616033 : DR_GROUP_FIRST_ELEMENT (stmtinfo_b) = stmtinfo_a;
3817 : 2616033 : DR_GROUP_NEXT_ELEMENT (lastinfo) = stmtinfo_b;
3818 : 2616033 : lastinfo = stmtinfo_b;
3819 : :
3820 : 2616033 : if (! STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3821 : : {
3822 : 2616003 : STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a)
3823 : 2616003 : = !can_group_stmts_p (stmtinfo_a, stmtinfo_b, false);
3824 : :
3825 : 2616003 : if (dump_enabled_p () && STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3826 : 72 : dump_printf_loc (MSG_NOTE, vect_location,
3827 : : "Load suitable for SLP vectorization only.\n");
3828 : : }
3829 : :
3830 : 2616033 : if (init_b == init_prev
3831 : 28822 : && !to_fixup.add (DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3832 : 2634351 : && dump_enabled_p ())
3833 : 217 : dump_printf_loc (MSG_NOTE, vect_location,
3834 : : "Queuing group with duplicate access for fixup\n");
3835 : : }
3836 : : }
3837 : :
3838 : : /* Fixup groups with duplicate entries by splitting it. */
3839 : 1553934 : while (1)
3840 : : {
3841 : 1553934 : hash_set<stmt_vec_info>::iterator it = to_fixup.begin ();
3842 : 1553934 : if (!(it != to_fixup.end ()))
3843 : : break;
3844 : 44364 : stmt_vec_info grp = *it;
3845 : 44364 : to_fixup.remove (grp);
3846 : :
3847 : : /* Find the earliest duplicate group member. */
3848 : 44364 : unsigned first_duplicate = -1u;
3849 : 44364 : stmt_vec_info next, g = grp;
3850 : 212098 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3851 : : {
3852 : 123370 : if (tree_int_cst_equal (DR_INIT (STMT_VINFO_DR_INFO (next)->dr),
3853 : 123370 : DR_INIT (STMT_VINFO_DR_INFO (g)->dr))
3854 : 123370 : && gimple_uid (STMT_VINFO_STMT (next)) < first_duplicate)
3855 : : first_duplicate = gimple_uid (STMT_VINFO_STMT (next));
3856 : : g = next;
3857 : : }
3858 : 44364 : if (first_duplicate == -1U)
3859 : 18318 : continue;
3860 : :
3861 : : /* Then move all stmts after the first duplicate to a new group.
3862 : : Note this is a heuristic but one with the property that *it
3863 : : is fixed up completely. */
3864 : 26046 : g = grp;
3865 : 26046 : stmt_vec_info newgroup = NULL, ng = grp;
3866 : 173645 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3867 : : {
3868 : 121553 : if (gimple_uid (STMT_VINFO_STMT (next)) >= first_duplicate)
3869 : : {
3870 : 115862 : DR_GROUP_NEXT_ELEMENT (g) = DR_GROUP_NEXT_ELEMENT (next);
3871 : 115862 : if (!newgroup)
3872 : : {
3873 : 26046 : newgroup = next;
3874 : 26046 : STMT_VINFO_SLP_VECT_ONLY (newgroup)
3875 : 26046 : = STMT_VINFO_SLP_VECT_ONLY (grp);
3876 : : }
3877 : : else
3878 : 89816 : DR_GROUP_NEXT_ELEMENT (ng) = next;
3879 : 115862 : ng = next;
3880 : 115862 : DR_GROUP_FIRST_ELEMENT (ng) = newgroup;
3881 : : }
3882 : : else
3883 : : g = DR_GROUP_NEXT_ELEMENT (g);
3884 : : }
3885 : 26046 : DR_GROUP_NEXT_ELEMENT (ng) = NULL;
3886 : :
3887 : : /* Fixup the new group which still may contain duplicates. */
3888 : 26046 : to_fixup.add (newgroup);
3889 : : }
3890 : :
3891 : 1509570 : dr_vec_info *dr_info;
3892 : 16210507 : FOR_EACH_VEC_ELT (datarefs_copy, i, dr_info)
3893 : : {
3894 : 14707113 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt)
3895 : 14707113 : && !vect_analyze_data_ref_access (vinfo, dr_info))
3896 : : {
3897 : 6230 : if (dump_enabled_p ())
3898 : 260 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3899 : : "not vectorized: complicated access pattern.\n");
3900 : :
3901 : 6230 : if (is_a <bb_vec_info> (vinfo))
3902 : : {
3903 : : /* Mark the statement as not vectorizable. */
3904 : 54 : STMT_VINFO_VECTORIZABLE (dr_info->stmt) = false;
3905 : 54 : continue;
3906 : : }
3907 : : else
3908 : : {
3909 : 6176 : datarefs_copy.release ();
3910 : 6176 : return opt_result::failure_at (dr_info->stmt->stmt,
3911 : : "not vectorized:"
3912 : : " complicated access pattern.\n");
3913 : : }
3914 : : }
3915 : : }
3916 : :
3917 : 1503394 : datarefs_copy.release ();
3918 : 1503394 : return opt_result::success ();
3919 : 1509570 : }
3920 : :
3921 : : /* Function vect_vfa_segment_size.
3922 : :
3923 : : Input:
3924 : : DR_INFO: The data reference.
3925 : : LENGTH_FACTOR: segment length to consider.
3926 : :
3927 : : Return a value suitable for the dr_with_seg_len::seg_len field.
3928 : : This is the "distance travelled" by the pointer from the first
3929 : : iteration in the segment to the last. Note that it does not include
3930 : : the size of the access; in effect it only describes the first byte. */
3931 : :
3932 : : static tree
3933 : 119804 : vect_vfa_segment_size (dr_vec_info *dr_info, tree length_factor)
3934 : : {
3935 : 119804 : length_factor = size_binop (MINUS_EXPR,
3936 : : fold_convert (sizetype, length_factor),
3937 : : size_one_node);
3938 : 119804 : return size_binop (MULT_EXPR, fold_convert (sizetype, DR_STEP (dr_info->dr)),
3939 : : length_factor);
3940 : : }
3941 : :
3942 : : /* Return a value that, when added to abs (vect_vfa_segment_size (DR_INFO)),
3943 : : gives the worst-case number of bytes covered by the segment. */
3944 : :
3945 : : static unsigned HOST_WIDE_INT
3946 : 120286 : vect_vfa_access_size (vec_info *vinfo, dr_vec_info *dr_info)
3947 : : {
3948 : 120286 : stmt_vec_info stmt_vinfo = dr_info->stmt;
3949 : 120286 : tree ref_type = TREE_TYPE (DR_REF (dr_info->dr));
3950 : 120286 : unsigned HOST_WIDE_INT ref_size = tree_to_uhwi (TYPE_SIZE_UNIT (ref_type));
3951 : 120286 : unsigned HOST_WIDE_INT access_size = ref_size;
3952 : 120286 : if (DR_GROUP_FIRST_ELEMENT (stmt_vinfo))
3953 : : {
3954 : 38342 : gcc_assert (DR_GROUP_FIRST_ELEMENT (stmt_vinfo) == stmt_vinfo);
3955 : 38342 : access_size *= DR_GROUP_SIZE (stmt_vinfo) - DR_GROUP_GAP (stmt_vinfo);
3956 : : }
3957 : 120286 : tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
3958 : 120286 : int misalignment;
3959 : 120286 : if (STMT_VINFO_VEC_STMTS (stmt_vinfo).exists ()
3960 : 120286 : && ((misalignment = dr_misalignment (dr_info, vectype)), true)
3961 : 120286 : && (vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment)
3962 : : == dr_explicit_realign_optimized))
3963 : : {
3964 : : /* We might access a full vector's worth. */
3965 : 0 : access_size += tree_to_uhwi (TYPE_SIZE_UNIT (vectype)) - ref_size;
3966 : : }
3967 : 120286 : return access_size;
3968 : : }
3969 : :
3970 : : /* Get the minimum alignment for all the scalar accesses that DR_INFO
3971 : : describes. */
3972 : :
3973 : : static unsigned int
3974 : 120286 : vect_vfa_align (dr_vec_info *dr_info)
3975 : : {
3976 : 0 : return dr_alignment (dr_info->dr);
3977 : : }
3978 : :
3979 : : /* Function vect_no_alias_p.
3980 : :
3981 : : Given data references A and B with equal base and offset, see whether
3982 : : the alias relation can be decided at compilation time. Return 1 if
3983 : : it can and the references alias, 0 if it can and the references do
3984 : : not alias, and -1 if we cannot decide at compile time. SEGMENT_LENGTH_A,
3985 : : SEGMENT_LENGTH_B, ACCESS_SIZE_A and ACCESS_SIZE_B are the equivalent
3986 : : of dr_with_seg_len::{seg_len,access_size} for A and B. */
3987 : :
3988 : : static int
3989 : 3656 : vect_compile_time_alias (dr_vec_info *a, dr_vec_info *b,
3990 : : tree segment_length_a, tree segment_length_b,
3991 : : unsigned HOST_WIDE_INT access_size_a,
3992 : : unsigned HOST_WIDE_INT access_size_b)
3993 : : {
3994 : 3656 : poly_offset_int offset_a = wi::to_poly_offset (DR_INIT (a->dr));
3995 : 3656 : poly_offset_int offset_b = wi::to_poly_offset (DR_INIT (b->dr));
3996 : 3656 : poly_uint64 const_length_a;
3997 : 3656 : poly_uint64 const_length_b;
3998 : :
3999 : : /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
4000 : : bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
4001 : : [a, a+12) */
4002 : 3656 : if (tree_int_cst_compare (DR_STEP (a->dr), size_zero_node) < 0)
4003 : : {
4004 : 224 : const_length_a = (-wi::to_poly_wide (segment_length_a)).force_uhwi ();
4005 : 224 : offset_a -= const_length_a;
4006 : : }
4007 : : else
4008 : 3432 : const_length_a = tree_to_poly_uint64 (segment_length_a);
4009 : 3656 : if (tree_int_cst_compare (DR_STEP (b->dr), size_zero_node) < 0)
4010 : : {
4011 : 378 : const_length_b = (-wi::to_poly_wide (segment_length_b)).force_uhwi ();
4012 : 378 : offset_b -= const_length_b;
4013 : : }
4014 : : else
4015 : 3278 : const_length_b = tree_to_poly_uint64 (segment_length_b);
4016 : :
4017 : 3656 : const_length_a += access_size_a;
4018 : 3656 : const_length_b += access_size_b;
4019 : :
4020 : 3656 : if (ranges_known_overlap_p (offset_a, const_length_a,
4021 : : offset_b, const_length_b))
4022 : : return 1;
4023 : :
4024 : 461 : if (!ranges_maybe_overlap_p (offset_a, const_length_a,
4025 : : offset_b, const_length_b))
4026 : 461 : return 0;
4027 : :
4028 : : return -1;
4029 : : }
4030 : :
4031 : : /* Return true if the minimum nonzero dependence distance for loop LOOP_DEPTH
4032 : : in DDR is >= VF. */
4033 : :
4034 : : static bool
4035 : 70550 : dependence_distance_ge_vf (data_dependence_relation *ddr,
4036 : : unsigned int loop_depth, poly_uint64 vf)
4037 : : {
4038 : 70550 : if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE
4039 : 75247 : || DDR_NUM_DIST_VECTS (ddr) == 0)
4040 : : return false;
4041 : :
4042 : : /* If the dependence is exact, we should have limited the VF instead. */
4043 : 4736 : gcc_checking_assert (DDR_COULD_BE_INDEPENDENT_P (ddr));
4044 : :
4045 : : unsigned int i;
4046 : : lambda_vector dist_v;
4047 : 9507 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
4048 : : {
4049 : 9468 : HOST_WIDE_INT dist = dist_v[loop_depth];
4050 : 9468 : if (dist != 0
4051 : 4736 : && !(dist > 0 && DDR_REVERSED_P (ddr))
4052 : 14204 : && maybe_lt ((unsigned HOST_WIDE_INT) abs_hwi (dist), vf))
4053 : : return false;
4054 : : }
4055 : :
4056 : 39 : if (dump_enabled_p ())
4057 : 2 : dump_printf_loc (MSG_NOTE, vect_location,
4058 : : "dependence distance between %T and %T is >= VF\n",
4059 : 2 : DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
4060 : :
4061 : : return true;
4062 : : }
4063 : :
4064 : : /* Dump LOWER_BOUND using flags DUMP_KIND. Dumps are known to be enabled. */
4065 : :
4066 : : static void
4067 : 439 : dump_lower_bound (dump_flags_t dump_kind, const vec_lower_bound &lower_bound)
4068 : : {
4069 : 439 : dump_printf (dump_kind, "%s (%T) >= ",
4070 : 439 : lower_bound.unsigned_p ? "unsigned" : "abs",
4071 : 439 : lower_bound.expr);
4072 : 439 : dump_dec (dump_kind, lower_bound.min_value);
4073 : 439 : }
4074 : :
4075 : : /* Record that the vectorized loop requires the vec_lower_bound described
4076 : : by EXPR, UNSIGNED_P and MIN_VALUE. */
4077 : :
4078 : : static void
4079 : 6053 : vect_check_lower_bound (loop_vec_info loop_vinfo, tree expr, bool unsigned_p,
4080 : : poly_uint64 min_value)
4081 : : {
4082 : 6053 : vec<vec_lower_bound> &lower_bounds
4083 : : = LOOP_VINFO_LOWER_BOUNDS (loop_vinfo);
4084 : 7025 : for (unsigned int i = 0; i < lower_bounds.length (); ++i)
4085 : 5581 : if (operand_equal_p (lower_bounds[i].expr, expr, 0))
4086 : : {
4087 : 4609 : unsigned_p &= lower_bounds[i].unsigned_p;
4088 : 4609 : min_value = upper_bound (lower_bounds[i].min_value, min_value);
4089 : 4609 : if (lower_bounds[i].unsigned_p != unsigned_p
4090 : 4609 : || maybe_lt (lower_bounds[i].min_value, min_value))
4091 : : {
4092 : 732 : lower_bounds[i].unsigned_p = unsigned_p;
4093 : 732 : lower_bounds[i].min_value = min_value;
4094 : 732 : if (dump_enabled_p ())
4095 : : {
4096 : 246 : dump_printf_loc (MSG_NOTE, vect_location,
4097 : : "updating run-time check to ");
4098 : 246 : dump_lower_bound (MSG_NOTE, lower_bounds[i]);
4099 : 246 : dump_printf (MSG_NOTE, "\n");
4100 : : }
4101 : : }
4102 : 4609 : return;
4103 : : }
4104 : :
4105 : 1444 : vec_lower_bound lower_bound (expr, unsigned_p, min_value);
4106 : 1444 : if (dump_enabled_p ())
4107 : : {
4108 : 193 : dump_printf_loc (MSG_NOTE, vect_location, "need a run-time check that ");
4109 : 193 : dump_lower_bound (MSG_NOTE, lower_bound);
4110 : 193 : dump_printf (MSG_NOTE, "\n");
4111 : : }
4112 : 1444 : LOOP_VINFO_LOWER_BOUNDS (loop_vinfo).safe_push (lower_bound);
4113 : : }
4114 : :
4115 : : /* Return true if it's unlikely that the step of the vectorized form of DR_INFO
4116 : : will span fewer than GAP bytes. */
4117 : :
4118 : : static bool
4119 : 4835 : vect_small_gap_p (loop_vec_info loop_vinfo, dr_vec_info *dr_info,
4120 : : poly_int64 gap)
4121 : : {
4122 : 4835 : stmt_vec_info stmt_info = dr_info->stmt;
4123 : 4835 : HOST_WIDE_INT count
4124 : 4835 : = estimated_poly_value (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
4125 : 4835 : if (DR_GROUP_FIRST_ELEMENT (stmt_info))
4126 : 4123 : count *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
4127 : 4835 : return (estimated_poly_value (gap)
4128 : 4835 : <= count * vect_get_scalar_dr_size (dr_info));
4129 : : }
4130 : :
4131 : : /* Return true if we know that there is no alias between DR_INFO_A and
4132 : : DR_INFO_B when abs (DR_STEP (DR_INFO_A->dr)) >= N for some N.
4133 : : When returning true, set *LOWER_BOUND_OUT to this N. */
4134 : :
4135 : : static bool
4136 : 18380 : vectorizable_with_step_bound_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b,
4137 : : poly_uint64 *lower_bound_out)
4138 : : {
4139 : : /* Check that there is a constant gap of known sign between DR_A
4140 : : and DR_B. */
4141 : 18380 : data_reference *dr_a = dr_info_a->dr;
4142 : 18380 : data_reference *dr_b = dr_info_b->dr;
4143 : 18380 : poly_int64 init_a, init_b;
4144 : 18380 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b), 0)
4145 : 7913 : || !operand_equal_p (DR_OFFSET (dr_a), DR_OFFSET (dr_b), 0)
4146 : 6893 : || !operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0)
4147 : 6883 : || !poly_int_tree_p (DR_INIT (dr_a), &init_a)
4148 : 6883 : || !poly_int_tree_p (DR_INIT (dr_b), &init_b)
4149 : 18380 : || !ordered_p (init_a, init_b))
4150 : 11497 : return false;
4151 : :
4152 : : /* Sort DR_A and DR_B by the address they access. */
4153 : 6883 : if (maybe_lt (init_b, init_a))
4154 : : {
4155 : 128 : std::swap (init_a, init_b);
4156 : 128 : std::swap (dr_info_a, dr_info_b);
4157 : 128 : std::swap (dr_a, dr_b);
4158 : : }
4159 : :
4160 : : /* If the two accesses could be dependent within a scalar iteration,
4161 : : make sure that we'd retain their order. */
4162 : 6883 : if (maybe_gt (init_a + vect_get_scalar_dr_size (dr_info_a), init_b)
4163 : 6883 : && !vect_preserves_scalar_order_p (dr_info_a, dr_info_b))
4164 : : return false;
4165 : :
4166 : : /* There is no alias if abs (DR_STEP) is greater than or equal to
4167 : : the bytes spanned by the combination of the two accesses. */
4168 : 6883 : *lower_bound_out = init_b + vect_get_scalar_dr_size (dr_info_b) - init_a;
4169 : 6883 : return true;
4170 : : }
4171 : :
4172 : : /* Function vect_prune_runtime_alias_test_list.
4173 : :
4174 : : Prune a list of ddrs to be tested at run-time by versioning for alias.
4175 : : Merge several alias checks into one if possible.
4176 : : Return FALSE if resulting list of ddrs is longer then allowed by
4177 : : PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
4178 : :
4179 : : opt_result
4180 : 350060 : vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
4181 : : {
4182 : 350060 : typedef pair_hash <tree_operand_hash, tree_operand_hash> tree_pair_hash;
4183 : 350060 : hash_set <tree_pair_hash> compared_objects;
4184 : :
4185 : 350060 : const vec<ddr_p> &may_alias_ddrs = LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
4186 : 350060 : vec<dr_with_seg_len_pair_t> &comp_alias_ddrs
4187 : : = LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
4188 : 350060 : const vec<vec_object_pair> &check_unequal_addrs
4189 : : = LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo);
4190 : 350060 : poly_uint64 vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4191 : 350060 : tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
4192 : :
4193 : 350060 : ddr_p ddr;
4194 : 350060 : unsigned int i;
4195 : 350060 : tree length_factor;
4196 : :
4197 : 350060 : DUMP_VECT_SCOPE ("vect_prune_runtime_alias_test_list");
4198 : :
4199 : : /* Step values are irrelevant for aliasing if the number of vector
4200 : : iterations is equal to the number of scalar iterations (which can
4201 : : happen for fully-SLP loops). */
4202 : 350060 : bool vf_one_p = known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U);
4203 : :
4204 : 350060 : if (!vf_one_p)
4205 : : {
4206 : : /* Convert the checks for nonzero steps into bound tests. */
4207 : : tree value;
4208 : 347975 : FOR_EACH_VEC_ELT (LOOP_VINFO_CHECK_NONZERO (loop_vinfo), i, value)
4209 : 1527 : vect_check_lower_bound (loop_vinfo, value, true, 1);
4210 : : }
4211 : :
4212 : 350060 : if (may_alias_ddrs.is_empty ())
4213 : 328385 : return opt_result::success ();
4214 : :
4215 : 21675 : comp_alias_ddrs.create (may_alias_ddrs.length ());
4216 : :
4217 : 21675 : unsigned int loop_depth
4218 : 21675 : = index_in_loop_nest (LOOP_VINFO_LOOP (loop_vinfo)->num,
4219 : 21675 : LOOP_VINFO_LOOP_NEST (loop_vinfo));
4220 : :
4221 : : /* First, we collect all data ref pairs for aliasing checks. */
4222 : 89030 : FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
4223 : : {
4224 : 70550 : poly_uint64 lower_bound;
4225 : 70550 : tree segment_length_a, segment_length_b;
4226 : 70550 : unsigned HOST_WIDE_INT access_size_a, access_size_b;
4227 : 70550 : unsigned HOST_WIDE_INT align_a, align_b;
4228 : :
4229 : : /* Ignore the alias if the VF we chose ended up being no greater
4230 : : than the dependence distance. */
4231 : 70550 : if (dependence_distance_ge_vf (ddr, loop_depth, vect_factor))
4232 : 10868 : continue;
4233 : :
4234 : 70511 : if (DDR_OBJECT_A (ddr))
4235 : : {
4236 : 46 : vec_object_pair new_pair (DDR_OBJECT_A (ddr), DDR_OBJECT_B (ddr));
4237 : 46 : if (!compared_objects.add (new_pair))
4238 : : {
4239 : 14 : if (dump_enabled_p ())
4240 : 8 : dump_printf_loc (MSG_NOTE, vect_location,
4241 : : "checking that %T and %T"
4242 : : " have different addresses\n",
4243 : : new_pair.first, new_pair.second);
4244 : 14 : LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo).safe_push (new_pair);
4245 : : }
4246 : 46 : continue;
4247 : 46 : }
4248 : :
4249 : 70465 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
4250 : 70465 : stmt_vec_info stmt_info_a = dr_info_a->stmt;
4251 : :
4252 : 70465 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
4253 : 70465 : stmt_vec_info stmt_info_b = dr_info_b->stmt;
4254 : :
4255 : 70465 : bool preserves_scalar_order_p
4256 : 70465 : = vect_preserves_scalar_order_p (dr_info_a, dr_info_b);
4257 : 70465 : bool ignore_step_p
4258 : : = (vf_one_p
4259 : 70465 : && (preserves_scalar_order_p
4260 : 2820 : || operand_equal_p (DR_STEP (dr_info_a->dr),
4261 : 2820 : DR_STEP (dr_info_b->dr))));
4262 : :
4263 : : /* Skip the pair if inter-iteration dependencies are irrelevant
4264 : : and intra-iteration dependencies are guaranteed to be honored. */
4265 : 11833 : if (ignore_step_p
4266 : 6037 : && (preserves_scalar_order_p
4267 : 2558 : || vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4268 : : &lower_bound)))
4269 : : {
4270 : 5796 : if (dump_enabled_p ())
4271 : 2352 : dump_printf_loc (MSG_NOTE, vect_location,
4272 : : "no need for alias check between "
4273 : : "%T and %T when VF is 1\n",
4274 : 2352 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4275 : 5796 : continue;
4276 : : }
4277 : :
4278 : : /* See whether we can handle the alias using a bounds check on
4279 : : the step, and whether that's likely to be the best approach.
4280 : : (It might not be, for example, if the minimum step is much larger
4281 : : than the number of bytes handled by one vector iteration.) */
4282 : 64669 : if (!ignore_step_p
4283 : 64428 : && TREE_CODE (DR_STEP (dr_info_a->dr)) != INTEGER_CST
4284 : 15822 : && vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4285 : : &lower_bound)
4286 : 69235 : && (vect_small_gap_p (loop_vinfo, dr_info_a, lower_bound)
4287 : 269 : || vect_small_gap_p (loop_vinfo, dr_info_b, lower_bound)))
4288 : : {
4289 : 4526 : bool unsigned_p = dr_known_forward_stride_p (dr_info_a->dr);
4290 : 4526 : if (dump_enabled_p ())
4291 : : {
4292 : 3312 : dump_printf_loc (MSG_NOTE, vect_location, "no alias between "
4293 : : "%T and %T when the step %T is outside ",
4294 : : DR_REF (dr_info_a->dr),
4295 : 1656 : DR_REF (dr_info_b->dr),
4296 : 1656 : DR_STEP (dr_info_a->dr));
4297 : 1656 : if (unsigned_p)
4298 : 504 : dump_printf (MSG_NOTE, "[0");
4299 : : else
4300 : : {
4301 : 1152 : dump_printf (MSG_NOTE, "(");
4302 : 1152 : dump_dec (MSG_NOTE, poly_int64 (-lower_bound));
4303 : : }
4304 : 1656 : dump_printf (MSG_NOTE, ", ");
4305 : 1656 : dump_dec (MSG_NOTE, lower_bound);
4306 : 1656 : dump_printf (MSG_NOTE, ")\n");
4307 : : }
4308 : 4526 : vect_check_lower_bound (loop_vinfo, DR_STEP (dr_info_a->dr),
4309 : : unsigned_p, lower_bound);
4310 : 4526 : continue;
4311 : 4526 : }
4312 : :
4313 : 60143 : stmt_vec_info dr_group_first_a = DR_GROUP_FIRST_ELEMENT (stmt_info_a);
4314 : 60143 : if (dr_group_first_a)
4315 : : {
4316 : 18776 : stmt_info_a = dr_group_first_a;
4317 : 18776 : dr_info_a = STMT_VINFO_DR_INFO (stmt_info_a);
4318 : : }
4319 : :
4320 : 60143 : stmt_vec_info dr_group_first_b = DR_GROUP_FIRST_ELEMENT (stmt_info_b);
4321 : 60143 : if (dr_group_first_b)
4322 : : {
4323 : 19566 : stmt_info_b = dr_group_first_b;
4324 : 19566 : dr_info_b = STMT_VINFO_DR_INFO (stmt_info_b);
4325 : : }
4326 : :
4327 : 60143 : if (ignore_step_p)
4328 : : {
4329 : 241 : segment_length_a = size_zero_node;
4330 : 241 : segment_length_b = size_zero_node;
4331 : : }
4332 : : else
4333 : : {
4334 : 59902 : if (!operand_equal_p (DR_STEP (dr_info_a->dr),
4335 : 59902 : DR_STEP (dr_info_b->dr), 0))
4336 : : length_factor = scalar_loop_iters;
4337 : : else
4338 : 47203 : length_factor = size_int (vect_factor);
4339 : 59902 : segment_length_a = vect_vfa_segment_size (dr_info_a, length_factor);
4340 : 59902 : segment_length_b = vect_vfa_segment_size (dr_info_b, length_factor);
4341 : : }
4342 : 60143 : access_size_a = vect_vfa_access_size (loop_vinfo, dr_info_a);
4343 : 60143 : access_size_b = vect_vfa_access_size (loop_vinfo, dr_info_b);
4344 : 60143 : align_a = vect_vfa_align (dr_info_a);
4345 : 60143 : align_b = vect_vfa_align (dr_info_b);
4346 : :
4347 : : /* See whether the alias is known at compilation time. */
4348 : 60143 : if (operand_equal_p (DR_BASE_ADDRESS (dr_info_a->dr),
4349 : 60143 : DR_BASE_ADDRESS (dr_info_b->dr), 0)
4350 : 5603 : && operand_equal_p (DR_OFFSET (dr_info_a->dr),
4351 : 5603 : DR_OFFSET (dr_info_b->dr), 0)
4352 : 3776 : && TREE_CODE (DR_STEP (dr_info_a->dr)) == INTEGER_CST
4353 : 3726 : && TREE_CODE (DR_STEP (dr_info_b->dr)) == INTEGER_CST
4354 : 3716 : && poly_int_tree_p (segment_length_a)
4355 : 63810 : && poly_int_tree_p (segment_length_b))
4356 : : {
4357 : 3656 : int res = vect_compile_time_alias (dr_info_a, dr_info_b,
4358 : : segment_length_a,
4359 : : segment_length_b,
4360 : : access_size_a,
4361 : : access_size_b);
4362 : 3656 : if (res >= 0 && dump_enabled_p ())
4363 : : {
4364 : 232 : dump_printf_loc (MSG_NOTE, vect_location,
4365 : : "can tell at compile time that %T and %T",
4366 : 116 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4367 : 116 : if (res == 0)
4368 : 57 : dump_printf (MSG_NOTE, " do not alias\n");
4369 : : else
4370 : 59 : dump_printf (MSG_NOTE, " alias\n");
4371 : : }
4372 : :
4373 : 3656 : if (res == 0)
4374 : 461 : continue;
4375 : :
4376 : 3195 : if (res == 1)
4377 : 3195 : return opt_result::failure_at (stmt_info_b->stmt,
4378 : : "not vectorized:"
4379 : : " compilation time alias: %G%G",
4380 : : stmt_info_a->stmt,
4381 : : stmt_info_b->stmt);
4382 : : }
4383 : :
4384 : : /* dr_with_seg_len requires the alignment to apply to the segment length
4385 : : and access size, not just the start address. The access size can be
4386 : : smaller than the pointer alignment for grouped accesses and bitfield
4387 : : references; see PR115192 and PR116125 respectively. */
4388 : 56487 : align_a = std::min (align_a, least_bit_hwi (access_size_a));
4389 : 56487 : align_b = std::min (align_b, least_bit_hwi (access_size_b));
4390 : :
4391 : 56487 : dr_with_seg_len dr_a (dr_info_a->dr, segment_length_a,
4392 : 56487 : access_size_a, align_a);
4393 : 56487 : dr_with_seg_len dr_b (dr_info_b->dr, segment_length_b,
4394 : 56487 : access_size_b, align_b);
4395 : : /* Canonicalize the order to be the one that's needed for accurate
4396 : : RAW, WAR and WAW flags, in cases where the data references are
4397 : : well-ordered. The order doesn't really matter otherwise,
4398 : : but we might as well be consistent. */
4399 : 56487 : if (get_later_stmt (stmt_info_a, stmt_info_b) == stmt_info_a)
4400 : 4419 : std::swap (dr_a, dr_b);
4401 : :
4402 : 56487 : dr_with_seg_len_pair_t dr_with_seg_len_pair
4403 : : (dr_a, dr_b, (preserves_scalar_order_p
4404 : : ? dr_with_seg_len_pair_t::WELL_ORDERED
4405 : 62272 : : dr_with_seg_len_pair_t::REORDERED));
4406 : :
4407 : 56487 : comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
4408 : : }
4409 : :
4410 : 18480 : prune_runtime_alias_test_list (&comp_alias_ddrs, vect_factor);
4411 : :
4412 : 36960 : unsigned int count = (comp_alias_ddrs.length ()
4413 : 18480 : + check_unequal_addrs.length ());
4414 : :
4415 : 18480 : if (count
4416 : 18480 : && (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo))
4417 : : == VECT_COST_MODEL_VERY_CHEAP))
4418 : 12727 : return opt_result::failure_at
4419 : 12727 : (vect_location, "would need a runtime alias check\n");
4420 : :
4421 : 5753 : if (dump_enabled_p ())
4422 : 1918 : dump_printf_loc (MSG_NOTE, vect_location,
4423 : : "improved number of alias checks from %d to %d\n",
4424 : : may_alias_ddrs.length (), count);
4425 : 5753 : unsigned limit = param_vect_max_version_for_alias_checks;
4426 : 5753 : if (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo)) == VECT_COST_MODEL_CHEAP)
4427 : 766 : limit = param_vect_max_version_for_alias_checks * 6 / 10;
4428 : 5753 : if (count > limit)
4429 : 166 : return opt_result::failure_at
4430 : 166 : (vect_location,
4431 : : "number of versioning for alias run-time tests exceeds %d "
4432 : : "(--param vect-max-version-for-alias-checks)\n", limit);
4433 : :
4434 : 5587 : return opt_result::success ();
4435 : 350060 : }
4436 : :
4437 : : /* Check whether we can use an internal function for a gather load
4438 : : or scatter store. READ_P is true for loads and false for stores.
4439 : : MASKED_P is true if the load or store is conditional. MEMORY_TYPE is
4440 : : the type of the memory elements being loaded or stored. OFFSET_TYPE
4441 : : is the type of the offset that is being applied to the invariant
4442 : : base address. SCALE is the amount by which the offset should
4443 : : be multiplied *after* it has been converted to address width.
4444 : :
4445 : : Return true if the function is supported, storing the function id in
4446 : : *IFN_OUT and the vector type for the offset in *OFFSET_VECTYPE_OUT.
4447 : :
4448 : : If we can use gather and store the possible else values in ELSVALS. */
4449 : :
4450 : : bool
4451 : 60894 : vect_gather_scatter_fn_p (vec_info *vinfo, bool read_p, bool masked_p,
4452 : : tree vectype, tree memory_type, tree offset_type,
4453 : : int scale, internal_fn *ifn_out,
4454 : : tree *offset_vectype_out, vec<int> *elsvals)
4455 : : {
4456 : 60894 : unsigned int memory_bits = tree_to_uhwi (TYPE_SIZE (memory_type));
4457 : 60894 : unsigned int element_bits = vector_element_bits (vectype);
4458 : 60894 : if (element_bits != memory_bits)
4459 : : /* For now the vector elements must be the same width as the
4460 : : memory elements. */
4461 : : return false;
4462 : :
4463 : : /* Work out which function we need. */
4464 : 60894 : internal_fn ifn, alt_ifn, alt_ifn2;
4465 : 60894 : if (read_p)
4466 : : {
4467 : 37828 : ifn = masked_p ? IFN_MASK_GATHER_LOAD : IFN_GATHER_LOAD;
4468 : : alt_ifn = IFN_MASK_GATHER_LOAD;
4469 : : /* When target supports MASK_LEN_GATHER_LOAD, we always
4470 : : use MASK_LEN_GATHER_LOAD regardless whether len and
4471 : : mask are valid or not. */
4472 : : alt_ifn2 = IFN_MASK_LEN_GATHER_LOAD;
4473 : : }
4474 : : else
4475 : : {
4476 : 23066 : ifn = masked_p ? IFN_MASK_SCATTER_STORE : IFN_SCATTER_STORE;
4477 : 60894 : alt_ifn = IFN_MASK_SCATTER_STORE;
4478 : : /* When target supports MASK_LEN_SCATTER_STORE, we always
4479 : : use MASK_LEN_SCATTER_STORE regardless whether len and
4480 : : mask are valid or not. */
4481 : 60894 : alt_ifn2 = IFN_MASK_LEN_SCATTER_STORE;
4482 : : }
4483 : :
4484 : 472678 : for (;;)
4485 : : {
4486 : 266786 : tree offset_vectype = get_vectype_for_scalar_type (vinfo, offset_type);
4487 : 266786 : if (!offset_vectype)
4488 : : return false;
4489 : :
4490 : : /* Test whether the target supports this combination. */
4491 : 262832 : if (internal_gather_scatter_fn_supported_p (ifn, vectype, memory_type,
4492 : : offset_vectype, scale,
4493 : : elsvals))
4494 : : {
4495 : 0 : *ifn_out = ifn;
4496 : 0 : *offset_vectype_out = offset_vectype;
4497 : 0 : return true;
4498 : : }
4499 : 262832 : else if (!masked_p
4500 : 262832 : && internal_gather_scatter_fn_supported_p (alt_ifn, vectype,
4501 : : memory_type,
4502 : : offset_vectype,
4503 : : scale, elsvals))
4504 : : {
4505 : 0 : *ifn_out = alt_ifn;
4506 : 0 : *offset_vectype_out = offset_vectype;
4507 : 0 : return true;
4508 : : }
4509 : 262832 : else if (internal_gather_scatter_fn_supported_p (alt_ifn2, vectype,
4510 : : memory_type,
4511 : : offset_vectype, scale,
4512 : : elsvals))
4513 : : {
4514 : 0 : *ifn_out = alt_ifn2;
4515 : 0 : *offset_vectype_out = offset_vectype;
4516 : 0 : return true;
4517 : : }
4518 : :
4519 : 262832 : if (TYPE_PRECISION (offset_type) >= POINTER_SIZE
4520 : 262832 : && TYPE_PRECISION (offset_type) >= element_bits)
4521 : : return false;
4522 : :
4523 : 205892 : offset_type = build_nonstandard_integer_type
4524 : 205892 : (TYPE_PRECISION (offset_type) * 2, TYPE_UNSIGNED (offset_type));
4525 : 205892 : }
4526 : : }
4527 : :
4528 : : /* STMT_INFO is a call to an internal gather load or scatter store function.
4529 : : Describe the operation in INFO. */
4530 : :
4531 : : static void
4532 : 0 : vect_describe_gather_scatter_call (stmt_vec_info stmt_info,
4533 : : gather_scatter_info *info)
4534 : : {
4535 : 0 : gcall *call = as_a <gcall *> (stmt_info->stmt);
4536 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4537 : 0 : data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4538 : :
4539 : 0 : info->ifn = gimple_call_internal_fn (call);
4540 : 0 : info->decl = NULL_TREE;
4541 : 0 : info->base = gimple_call_arg (call, 0);
4542 : 0 : info->offset = gimple_call_arg (call, 1);
4543 : 0 : info->offset_dt = vect_unknown_def_type;
4544 : 0 : info->offset_vectype = NULL_TREE;
4545 : 0 : info->scale = TREE_INT_CST_LOW (gimple_call_arg (call, 2));
4546 : 0 : info->element_type = TREE_TYPE (vectype);
4547 : 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
4548 : 0 : }
4549 : :
4550 : : /* Return true if a non-affine read or write in STMT_INFO is suitable for a
4551 : : gather load or scatter store. Describe the operation in *INFO if so.
4552 : : If it is suitable and ELSVALS is nonzero store the supported else values
4553 : : in the vector it points to. */
4554 : :
4555 : : bool
4556 : 187236 : vect_check_gather_scatter (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
4557 : : gather_scatter_info *info, vec<int> *elsvals)
4558 : : {
4559 : 187236 : HOST_WIDE_INT scale = 1;
4560 : 187236 : poly_int64 pbitpos, pbitsize;
4561 : 187236 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4562 : 187236 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4563 : 187236 : tree offtype = NULL_TREE;
4564 : 187236 : tree decl = NULL_TREE, base, off;
4565 : 187236 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4566 : 187236 : tree memory_type = TREE_TYPE (DR_REF (dr));
4567 : 187236 : machine_mode pmode;
4568 : 187236 : int punsignedp, reversep, pvolatilep = 0;
4569 : 187236 : internal_fn ifn;
4570 : 187236 : tree offset_vectype;
4571 : 187236 : bool masked_p = false;
4572 : :
4573 : : /* See whether this is already a call to a gather/scatter internal function.
4574 : : If not, see whether it's a masked load or store. */
4575 : 187236 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
4576 : 6864 : if (call && gimple_call_internal_p (call))
4577 : : {
4578 : 6864 : ifn = gimple_call_internal_fn (call);
4579 : 6864 : if (internal_gather_scatter_fn_p (ifn))
4580 : : {
4581 : 0 : vect_describe_gather_scatter_call (stmt_info, info);
4582 : :
4583 : : /* In pattern recog we simply used a ZERO else value that
4584 : : we need to correct here. To that end just re-use the
4585 : : (already succesful) check if we support a gather IFN
4586 : : and have it populate the else values. */
4587 : 0 : if (DR_IS_READ (dr) && internal_fn_mask_index (ifn) >= 0 && elsvals)
4588 : 0 : supports_vec_gather_load_p (TYPE_MODE (vectype), elsvals);
4589 : 0 : return true;
4590 : : }
4591 : 6864 : masked_p = (ifn == IFN_MASK_LOAD || ifn == IFN_MASK_STORE);
4592 : : }
4593 : :
4594 : : /* ??? For epilogues we adjust DR_REF to make the following stmt-based
4595 : : analysis work, but this adjustment doesn't work for epilogues of
4596 : : epilogues during transform, so disable gather/scatter in that case. */
4597 : 187236 : if (LOOP_VINFO_EPILOGUE_P (loop_vinfo)
4598 : 5105 : && LOOP_VINFO_EPILOGUE_P (LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo)))
4599 : : return false;
4600 : :
4601 : : /* True if we should aim to use internal functions rather than
4602 : : built-in functions. */
4603 : 187212 : bool use_ifn_p = (DR_IS_READ (dr)
4604 : 187212 : ? supports_vec_gather_load_p (TYPE_MODE (vectype),
4605 : : elsvals)
4606 : 187212 : : supports_vec_scatter_store_p (TYPE_MODE (vectype)));
4607 : :
4608 : 187212 : base = DR_REF (dr);
4609 : : /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
4610 : : see if we can use the def stmt of the address. */
4611 : 187212 : if (masked_p
4612 : 6864 : && TREE_CODE (base) == MEM_REF
4613 : 6864 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
4614 : 6864 : && integer_zerop (TREE_OPERAND (base, 1))
4615 : 194076 : && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
4616 : : {
4617 : 6864 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
4618 : 6864 : if (is_gimple_assign (def_stmt)
4619 : 6864 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
4620 : 741 : base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
4621 : : }
4622 : :
4623 : : /* The gather and scatter builtins need address of the form
4624 : : loop_invariant + vector * {1, 2, 4, 8}
4625 : : or
4626 : : loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
4627 : : Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
4628 : : of loop invariants/SSA_NAMEs defined in the loop, with casts,
4629 : : multiplications and additions in it. To get a vector, we need
4630 : : a single SSA_NAME that will be defined in the loop and will
4631 : : contain everything that is not loop invariant and that can be
4632 : : vectorized. The following code attempts to find such a preexistng
4633 : : SSA_NAME OFF and put the loop invariants into a tree BASE
4634 : : that can be gimplified before the loop. */
4635 : 187212 : base = get_inner_reference (base, &pbitsize, &pbitpos, &off, &pmode,
4636 : : &punsignedp, &reversep, &pvolatilep);
4637 : 187212 : if (reversep)
4638 : : return false;
4639 : :
4640 : : /* PR 107346. Packed structs can have fields at offsets that are not
4641 : : multiples of BITS_PER_UNIT. Do not use gather/scatters in such cases. */
4642 : 187212 : if (!multiple_p (pbitpos, BITS_PER_UNIT))
4643 : : return false;
4644 : :
4645 : : /* We need to be able to form an address to the base which for example
4646 : : isn't possible for hard registers. */
4647 : 187212 : if (may_be_nonaddressable_p (base))
4648 : : return false;
4649 : :
4650 : 187204 : poly_int64 pbytepos = exact_div (pbitpos, BITS_PER_UNIT);
4651 : :
4652 : 187204 : if (TREE_CODE (base) == MEM_REF)
4653 : : {
4654 : 128200 : if (!integer_zerop (TREE_OPERAND (base, 1)))
4655 : : {
4656 : 3386 : if (off == NULL_TREE)
4657 : 3202 : off = wide_int_to_tree (sizetype, mem_ref_offset (base));
4658 : : else
4659 : 184 : off = size_binop (PLUS_EXPR, off,
4660 : : fold_convert (sizetype, TREE_OPERAND (base, 1)));
4661 : : }
4662 : 128200 : base = TREE_OPERAND (base, 0);
4663 : : }
4664 : : else
4665 : 59004 : base = build_fold_addr_expr (base);
4666 : :
4667 : 187204 : if (off == NULL_TREE)
4668 : 108030 : off = size_zero_node;
4669 : :
4670 : : /* If base is not loop invariant, either off is 0, then we start with just
4671 : : the constant offset in the loop invariant BASE and continue with base
4672 : : as OFF, otherwise give up.
4673 : : We could handle that case by gimplifying the addition of base + off
4674 : : into some SSA_NAME and use that as off, but for now punt. */
4675 : 187204 : if (!expr_invariant_in_loop_p (loop, base))
4676 : : {
4677 : 111761 : if (!integer_zerop (off))
4678 : : return false;
4679 : 108026 : off = base;
4680 : 108026 : base = size_int (pbytepos);
4681 : : }
4682 : : /* Otherwise put base + constant offset into the loop invariant BASE
4683 : : and continue with OFF. */
4684 : : else
4685 : : {
4686 : 75443 : base = fold_convert (sizetype, base);
4687 : 75443 : base = size_binop (PLUS_EXPR, base, size_int (pbytepos));
4688 : : }
4689 : :
4690 : : /* OFF at this point may be either a SSA_NAME or some tree expression
4691 : : from get_inner_reference. Try to peel off loop invariants from it
4692 : : into BASE as long as possible. */
4693 : 183469 : STRIP_NOPS (off);
4694 : 604514 : while (offtype == NULL_TREE)
4695 : : {
4696 : 528894 : enum tree_code code;
4697 : 528894 : tree op0, op1, add = NULL_TREE;
4698 : :
4699 : 528894 : if (TREE_CODE (off) == SSA_NAME)
4700 : : {
4701 : 363660 : gimple *def_stmt = SSA_NAME_DEF_STMT (off);
4702 : :
4703 : 363660 : if (expr_invariant_in_loop_p (loop, off))
4704 : 16 : return false;
4705 : :
4706 : 363644 : if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
4707 : : break;
4708 : :
4709 : 336600 : op0 = gimple_assign_rhs1 (def_stmt);
4710 : 336600 : code = gimple_assign_rhs_code (def_stmt);
4711 : 336600 : op1 = gimple_assign_rhs2 (def_stmt);
4712 : : }
4713 : : else
4714 : : {
4715 : 165234 : if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
4716 : : return false;
4717 : 165234 : code = TREE_CODE (off);
4718 : 165234 : extract_ops_from_tree (off, &code, &op0, &op1);
4719 : : }
4720 : 501834 : switch (code)
4721 : : {
4722 : 146176 : case POINTER_PLUS_EXPR:
4723 : 146176 : case PLUS_EXPR:
4724 : 146176 : if (expr_invariant_in_loop_p (loop, op0))
4725 : : {
4726 : 88779 : add = op0;
4727 : 88779 : off = op1;
4728 : 135031 : do_add:
4729 : 135031 : add = fold_convert (sizetype, add);
4730 : 135031 : if (scale != 1)
4731 : 49125 : add = size_binop (MULT_EXPR, add, size_int (scale));
4732 : 135031 : base = size_binop (PLUS_EXPR, base, add);
4733 : 421045 : continue;
4734 : : }
4735 : 57397 : if (expr_invariant_in_loop_p (loop, op1))
4736 : : {
4737 : 45948 : add = op1;
4738 : 45948 : off = op0;
4739 : 45948 : goto do_add;
4740 : : }
4741 : : break;
4742 : 472 : case MINUS_EXPR:
4743 : 472 : if (expr_invariant_in_loop_p (loop, op1))
4744 : : {
4745 : 304 : add = fold_convert (sizetype, op1);
4746 : 304 : add = size_binop (MINUS_EXPR, size_zero_node, add);
4747 : 304 : off = op0;
4748 : 304 : goto do_add;
4749 : : }
4750 : : break;
4751 : 164807 : case MULT_EXPR:
4752 : 164807 : if (scale == 1 && tree_fits_shwi_p (op1))
4753 : : {
4754 : 132046 : int new_scale = tree_to_shwi (op1);
4755 : : /* Only treat this as a scaling operation if the target
4756 : : supports it for at least some offset type. */
4757 : 132046 : if (use_ifn_p
4758 : 0 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
4759 : : masked_p, vectype, memory_type,
4760 : : signed_char_type_node,
4761 : : new_scale, &ifn,
4762 : : &offset_vectype,
4763 : : elsvals)
4764 : 132046 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
4765 : : masked_p, vectype, memory_type,
4766 : : unsigned_char_type_node,
4767 : : new_scale, &ifn,
4768 : : &offset_vectype,
4769 : : elsvals))
4770 : : break;
4771 : 132046 : scale = new_scale;
4772 : 132046 : off = op0;
4773 : 132046 : continue;
4774 : 132046 : }
4775 : : break;
4776 : 0 : case SSA_NAME:
4777 : 0 : off = op0;
4778 : 0 : continue;
4779 : 158705 : CASE_CONVERT:
4780 : 317410 : if (!POINTER_TYPE_P (TREE_TYPE (op0))
4781 : 317410 : && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
4782 : : break;
4783 : :
4784 : : /* Don't include the conversion if the target is happy with
4785 : : the current offset type. */
4786 : 158705 : if (use_ifn_p
4787 : 0 : && TREE_CODE (off) == SSA_NAME
4788 : 0 : && !POINTER_TYPE_P (TREE_TYPE (off))
4789 : 158705 : && vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
4790 : : masked_p, vectype, memory_type,
4791 : 0 : TREE_TYPE (off), scale, &ifn,
4792 : : &offset_vectype, elsvals))
4793 : : break;
4794 : :
4795 : 158705 : if (TYPE_PRECISION (TREE_TYPE (op0))
4796 : 158705 : == TYPE_PRECISION (TREE_TYPE (off)))
4797 : : {
4798 : 78348 : off = op0;
4799 : 78348 : continue;
4800 : : }
4801 : :
4802 : : /* Include the conversion if it is widening and we're using
4803 : : the IFN path or the target can handle the converted from
4804 : : offset or the current size is not already the same as the
4805 : : data vector element size. */
4806 : 80357 : if ((TYPE_PRECISION (TREE_TYPE (op0))
4807 : 80357 : < TYPE_PRECISION (TREE_TYPE (off)))
4808 : 80357 : && (use_ifn_p
4809 : 80307 : || (DR_IS_READ (dr)
4810 : 49454 : ? (targetm.vectorize.builtin_gather
4811 : 49454 : && targetm.vectorize.builtin_gather (vectype,
4812 : 49454 : TREE_TYPE (op0),
4813 : : scale))
4814 : 30853 : : (targetm.vectorize.builtin_scatter
4815 : 30853 : && targetm.vectorize.builtin_scatter (vectype,
4816 : 30853 : TREE_TYPE (op0),
4817 : : scale)))
4818 : 79361 : || !operand_equal_p (TYPE_SIZE (TREE_TYPE (off)),
4819 : 79361 : TYPE_SIZE (TREE_TYPE (vectype)), 0)))
4820 : : {
4821 : 75620 : off = op0;
4822 : 75620 : offtype = TREE_TYPE (off);
4823 : 75620 : STRIP_NOPS (off);
4824 : 75620 : continue;
4825 : : }
4826 : : break;
4827 : : default:
4828 : : break;
4829 : 0 : }
4830 : : break;
4831 : : }
4832 : :
4833 : : /* If at the end OFF still isn't a SSA_NAME or isn't
4834 : : defined in the loop, punt. */
4835 : 183453 : if (TREE_CODE (off) != SSA_NAME
4836 : 183453 : || expr_invariant_in_loop_p (loop, off))
4837 : 5294 : return false;
4838 : :
4839 : 178159 : if (offtype == NULL_TREE)
4840 : 102761 : offtype = TREE_TYPE (off);
4841 : :
4842 : 178159 : if (use_ifn_p)
4843 : : {
4844 : 0 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
4845 : : vectype, memory_type, offtype, scale,
4846 : : &ifn, &offset_vectype, elsvals))
4847 : 0 : ifn = IFN_LAST;
4848 : : decl = NULL_TREE;
4849 : : }
4850 : : else
4851 : : {
4852 : 178159 : if (DR_IS_READ (dr))
4853 : : {
4854 : 113696 : if (targetm.vectorize.builtin_gather)
4855 : 113696 : decl = targetm.vectorize.builtin_gather (vectype, offtype, scale);
4856 : : }
4857 : : else
4858 : : {
4859 : 64463 : if (targetm.vectorize.builtin_scatter)
4860 : 64463 : decl = targetm.vectorize.builtin_scatter (vectype, offtype, scale);
4861 : : }
4862 : 178159 : ifn = IFN_LAST;
4863 : : /* The offset vector type will be read from DECL when needed. */
4864 : 178159 : offset_vectype = NULL_TREE;
4865 : : }
4866 : :
4867 : 178159 : info->ifn = ifn;
4868 : 178159 : info->decl = decl;
4869 : 178159 : info->base = base;
4870 : 178159 : info->offset = off;
4871 : 178159 : info->offset_dt = vect_unknown_def_type;
4872 : 178159 : info->offset_vectype = offset_vectype;
4873 : 178159 : info->scale = scale;
4874 : 178159 : info->element_type = TREE_TYPE (vectype);
4875 : 178159 : info->memory_type = memory_type;
4876 : 178159 : return true;
4877 : : }
4878 : :
4879 : : /* Find the data references in STMT, analyze them with respect to LOOP and
4880 : : append them to DATAREFS. Return false if datarefs in this stmt cannot
4881 : : be handled. */
4882 : :
4883 : : opt_result
4884 : 31897638 : vect_find_stmt_data_reference (loop_p loop, gimple *stmt,
4885 : : vec<data_reference_p> *datarefs,
4886 : : vec<int> *dataref_groups, int group_id)
4887 : : {
4888 : : /* We can ignore clobbers for dataref analysis - they are removed during
4889 : : loop vectorization and BB vectorization checks dependences with a
4890 : : stmt walk. */
4891 : 31897638 : if (gimple_clobber_p (stmt))
4892 : 1365618 : return opt_result::success ();
4893 : :
4894 : 56813469 : if (gimple_has_volatile_ops (stmt))
4895 : 317669 : return opt_result::failure_at (stmt, "not vectorized: volatile type: %G",
4896 : : stmt);
4897 : :
4898 : 30214351 : if (stmt_can_throw_internal (cfun, stmt))
4899 : 790906 : return opt_result::failure_at (stmt,
4900 : : "not vectorized:"
4901 : : " statement can throw an exception: %G",
4902 : : stmt);
4903 : :
4904 : 29423445 : auto_vec<data_reference_p, 2> refs;
4905 : 29423445 : opt_result res = find_data_references_in_stmt (loop, stmt, &refs);
4906 : 29423445 : if (!res)
4907 : 3540585 : return res;
4908 : :
4909 : 25882860 : if (refs.is_empty ())
4910 : 14683107 : return opt_result::success ();
4911 : :
4912 : 11199753 : if (refs.length () > 1)
4913 : : {
4914 : 1462018 : while (!refs.is_empty ())
4915 : 974981 : free_data_ref (refs.pop ());
4916 : 487037 : return opt_result::failure_at (stmt,
4917 : : "not vectorized: more than one "
4918 : : "data ref in stmt: %G", stmt);
4919 : : }
4920 : :
4921 : 10712716 : data_reference_p dr = refs.pop ();
4922 : 10712716 : if (gcall *call = dyn_cast <gcall *> (stmt))
4923 : 4503 : if (!gimple_call_internal_p (call)
4924 : 4503 : || (gimple_call_internal_fn (call) != IFN_MASK_LOAD
4925 : 1260 : && gimple_call_internal_fn (call) != IFN_MASK_STORE))
4926 : : {
4927 : 1672 : free_data_ref (dr);
4928 : 1672 : return opt_result::failure_at (stmt,
4929 : : "not vectorized: dr in a call %G", stmt);
4930 : : }
4931 : :
4932 : 10711044 : if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
4933 : 10711044 : && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
4934 : : {
4935 : 82613 : free_data_ref (dr);
4936 : 82613 : return opt_result::failure_at (stmt,
4937 : : "not vectorized:"
4938 : : " statement is an unsupported"
4939 : : " bitfield access %G", stmt);
4940 : : }
4941 : :
4942 : 10628431 : if (DR_BASE_ADDRESS (dr)
4943 : 10592644 : && TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
4944 : : {
4945 : 955 : free_data_ref (dr);
4946 : 955 : return opt_result::failure_at (stmt,
4947 : : "not vectorized:"
4948 : : " base addr of dr is a constant\n");
4949 : : }
4950 : :
4951 : : /* Check whether this may be a SIMD lane access and adjust the
4952 : : DR to make it easier for us to handle it. */
4953 : 10627476 : if (loop
4954 : 473441 : && loop->simduid
4955 : 10702 : && (!DR_BASE_ADDRESS (dr)
4956 : 2955 : || !DR_OFFSET (dr)
4957 : 2955 : || !DR_INIT (dr)
4958 : 2955 : || !DR_STEP (dr)))
4959 : : {
4960 : 7747 : struct data_reference *newdr
4961 : 7747 : = create_data_ref (NULL, loop_containing_stmt (stmt), DR_REF (dr), stmt,
4962 : 7747 : DR_IS_READ (dr), DR_IS_CONDITIONAL_IN_STMT (dr));
4963 : 7747 : if (DR_BASE_ADDRESS (newdr)
4964 : 7747 : && DR_OFFSET (newdr)
4965 : 7747 : && DR_INIT (newdr)
4966 : 7747 : && DR_STEP (newdr)
4967 : 7747 : && TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
4968 : 15494 : && integer_zerop (DR_STEP (newdr)))
4969 : : {
4970 : 7747 : tree base_address = DR_BASE_ADDRESS (newdr);
4971 : 7747 : tree off = DR_OFFSET (newdr);
4972 : 7747 : tree step = ssize_int (1);
4973 : 7747 : if (integer_zerop (off)
4974 : 7747 : && TREE_CODE (base_address) == POINTER_PLUS_EXPR)
4975 : : {
4976 : 89 : off = TREE_OPERAND (base_address, 1);
4977 : 89 : base_address = TREE_OPERAND (base_address, 0);
4978 : : }
4979 : 7747 : STRIP_NOPS (off);
4980 : 7747 : if (TREE_CODE (off) == MULT_EXPR
4981 : 7747 : && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
4982 : : {
4983 : 7497 : step = TREE_OPERAND (off, 1);
4984 : 7497 : off = TREE_OPERAND (off, 0);
4985 : 7497 : STRIP_NOPS (off);
4986 : : }
4987 : 544 : if (CONVERT_EXPR_P (off)
4988 : 7747 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off, 0)))
4989 : 7203 : < TYPE_PRECISION (TREE_TYPE (off))))
4990 : 7203 : off = TREE_OPERAND (off, 0);
4991 : 7747 : if (TREE_CODE (off) == SSA_NAME)
4992 : : {
4993 : 7219 : gimple *def = SSA_NAME_DEF_STMT (off);
4994 : : /* Look through widening conversion. */
4995 : 7219 : if (is_gimple_assign (def)
4996 : 7219 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
4997 : : {
4998 : 0 : tree rhs1 = gimple_assign_rhs1 (def);
4999 : 0 : if (TREE_CODE (rhs1) == SSA_NAME
5000 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5001 : 0 : && (TYPE_PRECISION (TREE_TYPE (off))
5002 : 0 : > TYPE_PRECISION (TREE_TYPE (rhs1))))
5003 : 0 : def = SSA_NAME_DEF_STMT (rhs1);
5004 : : }
5005 : 7219 : if (is_gimple_call (def)
5006 : 7080 : && gimple_call_internal_p (def)
5007 : 14299 : && (gimple_call_internal_fn (def) == IFN_GOMP_SIMD_LANE))
5008 : : {
5009 : 7080 : tree arg = gimple_call_arg (def, 0);
5010 : 7080 : tree reft = TREE_TYPE (DR_REF (newdr));
5011 : 7080 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
5012 : 7080 : arg = SSA_NAME_VAR (arg);
5013 : 7080 : if (arg == loop->simduid
5014 : : /* For now. */
5015 : 7080 : && tree_int_cst_equal (TYPE_SIZE_UNIT (reft), step))
5016 : : {
5017 : 7055 : DR_BASE_ADDRESS (newdr) = base_address;
5018 : 7055 : DR_OFFSET (newdr) = ssize_int (0);
5019 : 7055 : DR_STEP (newdr) = step;
5020 : 7055 : DR_OFFSET_ALIGNMENT (newdr) = BIGGEST_ALIGNMENT;
5021 : 7055 : DR_STEP_ALIGNMENT (newdr) = highest_pow2_factor (step);
5022 : : /* Mark as simd-lane access. */
5023 : 7055 : tree arg2 = gimple_call_arg (def, 1);
5024 : 7055 : newdr->aux = (void *) (-1 - tree_to_uhwi (arg2));
5025 : 7055 : free_data_ref (dr);
5026 : 7055 : datarefs->safe_push (newdr);
5027 : 7055 : if (dataref_groups)
5028 : 0 : dataref_groups->safe_push (group_id);
5029 : 7055 : return opt_result::success ();
5030 : : }
5031 : : }
5032 : : }
5033 : : }
5034 : 692 : free_data_ref (newdr);
5035 : : }
5036 : :
5037 : 10620421 : datarefs->safe_push (dr);
5038 : 10620421 : if (dataref_groups)
5039 : 10154035 : dataref_groups->safe_push (group_id);
5040 : 10620421 : return opt_result::success ();
5041 : 29423445 : }
5042 : :
5043 : : /* Function vect_analyze_data_refs.
5044 : :
5045 : : Find all the data references in the loop or basic block.
5046 : :
5047 : : The general structure of the analysis of data refs in the vectorizer is as
5048 : : follows:
5049 : : 1- vect_analyze_data_refs(loop/bb): call
5050 : : compute_data_dependences_for_loop/bb to find and analyze all data-refs
5051 : : in the loop/bb and their dependences.
5052 : : 2- vect_analyze_dependences(): apply dependence testing using ddrs.
5053 : : 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
5054 : : 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
5055 : :
5056 : : */
5057 : :
5058 : : opt_result
5059 : 2752854 : vect_analyze_data_refs (vec_info *vinfo, poly_uint64 *min_vf, bool *fatal)
5060 : : {
5061 : 2752854 : class loop *loop = NULL;
5062 : 2752854 : unsigned int i;
5063 : 2752854 : struct data_reference *dr;
5064 : 2752854 : tree scalar_type;
5065 : :
5066 : 2752854 : DUMP_VECT_SCOPE ("vect_analyze_data_refs");
5067 : :
5068 : 2752854 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5069 : 361290 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5070 : :
5071 : : /* Go through the data-refs, check that the analysis succeeded. Update
5072 : : pointer from stmt_vec_info struct to DR and vectype. */
5073 : :
5074 : 2752854 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
5075 : 17480926 : FOR_EACH_VEC_ELT (datarefs, i, dr)
5076 : : {
5077 : 14779042 : enum { SG_NONE, GATHER, SCATTER } gatherscatter = SG_NONE;
5078 : 14779042 : poly_uint64 vf;
5079 : :
5080 : 14779042 : gcc_assert (DR_REF (dr));
5081 : 14779042 : stmt_vec_info stmt_info = vinfo->lookup_stmt (DR_STMT (dr));
5082 : 14779042 : gcc_assert (!stmt_info->dr_aux.dr);
5083 : 14779042 : stmt_info->dr_aux.dr = dr;
5084 : 14779042 : stmt_info->dr_aux.stmt = stmt_info;
5085 : :
5086 : : /* Check that analysis of the data-ref succeeded. */
5087 : 14779042 : if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
5088 : 14734413 : || !DR_STEP (dr))
5089 : : {
5090 : 89258 : bool maybe_gather
5091 : 44629 : = DR_IS_READ (dr)
5092 : 44629 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5093 : 89258 : bool maybe_scatter
5094 : : = DR_IS_WRITE (dr)
5095 : 44629 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5096 : :
5097 : : /* If target supports vector gather loads or scatter stores,
5098 : : see if they can't be used. */
5099 : 44629 : if (is_a <loop_vec_info> (vinfo)
5100 : 44629 : && !nested_in_vect_loop_p (loop, stmt_info))
5101 : : {
5102 : 40990 : if (maybe_gather || maybe_scatter)
5103 : : {
5104 : 40990 : if (maybe_gather)
5105 : : gatherscatter = GATHER;
5106 : : else
5107 : 12516 : gatherscatter = SCATTER;
5108 : : }
5109 : : }
5110 : :
5111 : 12516 : if (gatherscatter == SG_NONE)
5112 : : {
5113 : 3639 : if (dump_enabled_p ())
5114 : 5 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5115 : : "not vectorized: data ref analysis "
5116 : : "failed %G", stmt_info->stmt);
5117 : 3639 : if (is_a <bb_vec_info> (vinfo))
5118 : : {
5119 : : /* In BB vectorization the ref can still participate
5120 : : in dependence analysis, we just can't vectorize it. */
5121 : 3025 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5122 : 1696315 : continue;
5123 : : }
5124 : 614 : return opt_result::failure_at (stmt_info->stmt,
5125 : : "not vectorized:"
5126 : : " data ref analysis failed: %G",
5127 : : stmt_info->stmt);
5128 : : }
5129 : : }
5130 : :
5131 : : /* See if this was detected as SIMD lane access. */
5132 : 14775403 : if (dr->aux == (void *)-1
5133 : 14775403 : || dr->aux == (void *)-2
5134 : 14766464 : || dr->aux == (void *)-3
5135 : 14765624 : || dr->aux == (void *)-4)
5136 : : {
5137 : 10579 : if (nested_in_vect_loop_p (loop, stmt_info))
5138 : 0 : return opt_result::failure_at (stmt_info->stmt,
5139 : : "not vectorized:"
5140 : : " data ref analysis failed: %G",
5141 : : stmt_info->stmt);
5142 : 10579 : STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)
5143 : 10579 : = -(uintptr_t) dr->aux;
5144 : : }
5145 : :
5146 : 14775403 : tree base = get_base_address (DR_REF (dr));
5147 : 14775403 : if (base && VAR_P (base) && DECL_NONALIASED (base))
5148 : : {
5149 : 8060 : if (dump_enabled_p ())
5150 : 186 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5151 : : "not vectorized: base object not addressable "
5152 : : "for stmt: %G", stmt_info->stmt);
5153 : 8060 : if (is_a <bb_vec_info> (vinfo))
5154 : : {
5155 : : /* In BB vectorization the ref can still participate
5156 : : in dependence analysis, we just can't vectorize it. */
5157 : 8060 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5158 : 8060 : continue;
5159 : : }
5160 : 0 : return opt_result::failure_at (stmt_info->stmt,
5161 : : "not vectorized: base object not"
5162 : : " addressable for stmt: %G",
5163 : : stmt_info->stmt);
5164 : : }
5165 : :
5166 : 14767343 : if (is_a <loop_vec_info> (vinfo)
5167 : 818848 : && DR_STEP (dr)
5168 : 15545201 : && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
5169 : : {
5170 : 39726 : if (nested_in_vect_loop_p (loop, stmt_info))
5171 : 373 : return opt_result::failure_at (stmt_info->stmt,
5172 : : "not vectorized: "
5173 : : "not suitable for strided load %G",
5174 : : stmt_info->stmt);
5175 : 39353 : STMT_VINFO_STRIDED_P (stmt_info) = true;
5176 : : }
5177 : :
5178 : : /* Update DR field in stmt_vec_info struct. */
5179 : :
5180 : : /* If the dataref is in an inner-loop of the loop that is considered for
5181 : : for vectorization, we also want to analyze the access relative to
5182 : : the outer-loop (DR contains information only relative to the
5183 : : inner-most enclosing loop). We do that by building a reference to the
5184 : : first location accessed by the inner-loop, and analyze it relative to
5185 : : the outer-loop. */
5186 : 14766970 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
5187 : : {
5188 : : /* Build a reference to the first location accessed by the
5189 : : inner loop: *(BASE + INIT + OFFSET). By construction,
5190 : : this address must be invariant in the inner loop, so we
5191 : : can consider it as being used in the outer loop. */
5192 : 11134 : tree base = unshare_expr (DR_BASE_ADDRESS (dr));
5193 : 11134 : tree offset = unshare_expr (DR_OFFSET (dr));
5194 : 11134 : tree init = unshare_expr (DR_INIT (dr));
5195 : 11134 : tree init_offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset),
5196 : : init, offset);
5197 : 11134 : tree init_addr = fold_build_pointer_plus (base, init_offset);
5198 : 11134 : tree init_ref = build_fold_indirect_ref (init_addr);
5199 : :
5200 : 11134 : if (dump_enabled_p ())
5201 : 1155 : dump_printf_loc (MSG_NOTE, vect_location,
5202 : : "analyze in outer loop: %T\n", init_ref);
5203 : :
5204 : 11134 : opt_result res
5205 : 11134 : = dr_analyze_innermost (&STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info),
5206 : 11134 : init_ref, loop, stmt_info->stmt);
5207 : 11134 : if (!res)
5208 : : /* dr_analyze_innermost already explained the failure. */
5209 : 156 : return res;
5210 : :
5211 : 10978 : if (dump_enabled_p ())
5212 : 1155 : dump_printf_loc (MSG_NOTE, vect_location,
5213 : : "\touter base_address: %T\n"
5214 : : "\touter offset from base address: %T\n"
5215 : : "\touter constant offset from base address: %T\n"
5216 : : "\touter step: %T\n"
5217 : : "\touter base alignment: %d\n\n"
5218 : : "\touter base misalignment: %d\n"
5219 : : "\touter offset alignment: %d\n"
5220 : : "\touter step alignment: %d\n",
5221 : : STMT_VINFO_DR_BASE_ADDRESS (stmt_info),
5222 : : STMT_VINFO_DR_OFFSET (stmt_info),
5223 : : STMT_VINFO_DR_INIT (stmt_info),
5224 : : STMT_VINFO_DR_STEP (stmt_info),
5225 : : STMT_VINFO_DR_BASE_ALIGNMENT (stmt_info),
5226 : : STMT_VINFO_DR_BASE_MISALIGNMENT (stmt_info),
5227 : : STMT_VINFO_DR_OFFSET_ALIGNMENT (stmt_info),
5228 : : STMT_VINFO_DR_STEP_ALIGNMENT (stmt_info));
5229 : : }
5230 : :
5231 : : /* Set vectype for STMT. */
5232 : 14766814 : scalar_type = TREE_TYPE (DR_REF (dr));
5233 : 14766814 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
5234 : 14766814 : if (!vectype)
5235 : : {
5236 : 1729395 : if (dump_enabled_p ())
5237 : : {
5238 : 1800 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5239 : : "not vectorized: no vectype for stmt: %G",
5240 : : stmt_info->stmt);
5241 : 1800 : dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
5242 : 1800 : dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
5243 : : scalar_type);
5244 : 1800 : dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5245 : : }
5246 : :
5247 : 1729395 : if (is_a <bb_vec_info> (vinfo))
5248 : : {
5249 : : /* No vector type is fine, the ref can still participate
5250 : : in dependence analysis, we just can't vectorize it. */
5251 : 1685230 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5252 : 1685230 : continue;
5253 : : }
5254 : 44165 : if (fatal)
5255 : 44165 : *fatal = false;
5256 : 44165 : return opt_result::failure_at (stmt_info->stmt,
5257 : : "not vectorized:"
5258 : : " no vectype for stmt: %G"
5259 : : " scalar_type: %T\n",
5260 : : stmt_info->stmt, scalar_type);
5261 : : }
5262 : : else
5263 : : {
5264 : 13037419 : if (dump_enabled_p ())
5265 : 76448 : dump_printf_loc (MSG_NOTE, vect_location,
5266 : : "got vectype for stmt: %G%T\n",
5267 : : stmt_info->stmt, vectype);
5268 : : }
5269 : :
5270 : : /* Adjust the minimal vectorization factor according to the
5271 : : vector type. */
5272 : 13037419 : vf = TYPE_VECTOR_SUBPARTS (vectype);
5273 : 13037419 : *min_vf = upper_bound (*min_vf, vf);
5274 : :
5275 : : /* Leave the BB vectorizer to pick the vector type later, based on
5276 : : the final dataref group size and SLP node size. */
5277 : 13037419 : if (is_a <loop_vec_info> (vinfo))
5278 : 774154 : STMT_VINFO_VECTYPE (stmt_info) = vectype;
5279 : :
5280 : 13037419 : if (gatherscatter != SG_NONE)
5281 : : {
5282 : 38527 : gather_scatter_info gs_info;
5283 : 38527 : if (!vect_check_gather_scatter (stmt_info,
5284 : : as_a <loop_vec_info> (vinfo),
5285 : : &gs_info)
5286 : 72090 : || !get_vectype_for_scalar_type (vinfo,
5287 : 33563 : TREE_TYPE (gs_info.offset)))
5288 : : {
5289 : 5662 : if (fatal)
5290 : 5662 : *fatal = false;
5291 : 5662 : return opt_result::failure_at
5292 : 6324 : (stmt_info->stmt,
5293 : : (gatherscatter == GATHER)
5294 : : ? "not vectorized: not suitable for gather load %G"
5295 : : : "not vectorized: not suitable for scatter store %G",
5296 : : stmt_info->stmt);
5297 : : }
5298 : 32865 : STMT_VINFO_GATHER_SCATTER_P (stmt_info) = gatherscatter;
5299 : : }
5300 : : }
5301 : :
5302 : : /* We used to stop processing and prune the list here. Verify we no
5303 : : longer need to. */
5304 : 4211454 : gcc_assert (i == datarefs.length ());
5305 : :
5306 : 2701884 : return opt_result::success ();
5307 : : }
5308 : :
5309 : :
5310 : : /* Function vect_get_new_vect_var.
5311 : :
5312 : : Returns a name for a new variable. The current naming scheme appends the
5313 : : prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
5314 : : the name of vectorizer generated variables, and appends that to NAME if
5315 : : provided. */
5316 : :
5317 : : tree
5318 : 1828649 : vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
5319 : : {
5320 : 1828649 : const char *prefix;
5321 : 1828649 : tree new_vect_var;
5322 : :
5323 : 1828649 : switch (var_kind)
5324 : : {
5325 : : case vect_simple_var:
5326 : : prefix = "vect";
5327 : : break;
5328 : 22030 : case vect_scalar_var:
5329 : 22030 : prefix = "stmp";
5330 : 22030 : break;
5331 : 13635 : case vect_mask_var:
5332 : 13635 : prefix = "mask";
5333 : 13635 : break;
5334 : 1361978 : case vect_pointer_var:
5335 : 1361978 : prefix = "vectp";
5336 : 1361978 : break;
5337 : 0 : default:
5338 : 0 : gcc_unreachable ();
5339 : : }
5340 : :
5341 : 1828649 : if (name)
5342 : : {
5343 : 1002793 : char* tmp = concat (prefix, "_", name, NULL);
5344 : 1002793 : new_vect_var = create_tmp_reg (type, tmp);
5345 : 1002793 : free (tmp);
5346 : : }
5347 : : else
5348 : 825856 : new_vect_var = create_tmp_reg (type, prefix);
5349 : :
5350 : 1828649 : return new_vect_var;
5351 : : }
5352 : :
5353 : : /* Like vect_get_new_vect_var but return an SSA name. */
5354 : :
5355 : : tree
5356 : 6611 : vect_get_new_ssa_name (tree type, enum vect_var_kind var_kind, const char *name)
5357 : : {
5358 : 6611 : const char *prefix;
5359 : 6611 : tree new_vect_var;
5360 : :
5361 : 6611 : switch (var_kind)
5362 : : {
5363 : : case vect_simple_var:
5364 : : prefix = "vect";
5365 : : break;
5366 : 313 : case vect_scalar_var:
5367 : 313 : prefix = "stmp";
5368 : 313 : break;
5369 : 0 : case vect_pointer_var:
5370 : 0 : prefix = "vectp";
5371 : 0 : break;
5372 : 0 : default:
5373 : 0 : gcc_unreachable ();
5374 : : }
5375 : :
5376 : 6611 : if (name)
5377 : : {
5378 : 6125 : char* tmp = concat (prefix, "_", name, NULL);
5379 : 6125 : new_vect_var = make_temp_ssa_name (type, NULL, tmp);
5380 : 6125 : free (tmp);
5381 : : }
5382 : : else
5383 : 486 : new_vect_var = make_temp_ssa_name (type, NULL, prefix);
5384 : :
5385 : 6611 : return new_vect_var;
5386 : : }
5387 : :
5388 : : /* Duplicate points-to info on NAME from DR_INFO. */
5389 : :
5390 : : static void
5391 : 270308 : vect_duplicate_ssa_name_ptr_info (tree name, dr_vec_info *dr_info)
5392 : : {
5393 : 270308 : duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr_info->dr));
5394 : : /* DR_PTR_INFO is for a base SSA name, not including constant or
5395 : : variable offsets in the ref so its alignment info does not apply. */
5396 : 270308 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
5397 : 270308 : }
5398 : :
5399 : : /* Function vect_create_addr_base_for_vector_ref.
5400 : :
5401 : : Create an expression that computes the address of the first memory location
5402 : : that will be accessed for a data reference.
5403 : :
5404 : : Input:
5405 : : STMT_INFO: The statement containing the data reference.
5406 : : NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
5407 : : OFFSET: Optional. If supplied, it is be added to the initial address.
5408 : : LOOP: Specify relative to which loop-nest should the address be computed.
5409 : : For example, when the dataref is in an inner-loop nested in an
5410 : : outer-loop that is now being vectorized, LOOP can be either the
5411 : : outer-loop, or the inner-loop. The first memory location accessed
5412 : : by the following dataref ('in' points to short):
5413 : :
5414 : : for (i=0; i<N; i++)
5415 : : for (j=0; j<M; j++)
5416 : : s += in[i+j]
5417 : :
5418 : : is as follows:
5419 : : if LOOP=i_loop: &in (relative to i_loop)
5420 : : if LOOP=j_loop: &in+i*2B (relative to j_loop)
5421 : :
5422 : : Output:
5423 : : 1. Return an SSA_NAME whose value is the address of the memory location of
5424 : : the first vector of the data reference.
5425 : : 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
5426 : : these statement(s) which define the returned SSA_NAME.
5427 : :
5428 : : FORNOW: We are only handling array accesses with step 1. */
5429 : :
5430 : : tree
5431 : 681095 : vect_create_addr_base_for_vector_ref (vec_info *vinfo, stmt_vec_info stmt_info,
5432 : : gimple_seq *new_stmt_list,
5433 : : tree offset)
5434 : : {
5435 : 681095 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5436 : 681095 : struct data_reference *dr = dr_info->dr;
5437 : 681095 : const char *base_name;
5438 : 681095 : tree addr_base;
5439 : 681095 : tree dest;
5440 : 681095 : gimple_seq seq = NULL;
5441 : 681095 : tree vect_ptr_type;
5442 : 681095 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5443 : 681095 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
5444 : :
5445 : 681095 : tree data_ref_base = unshare_expr (drb->base_address);
5446 : 681095 : tree base_offset = unshare_expr (get_dr_vinfo_offset (vinfo, dr_info, true));
5447 : 681095 : tree init = unshare_expr (drb->init);
5448 : :
5449 : 681095 : if (loop_vinfo)
5450 : 119347 : base_name = get_name (data_ref_base);
5451 : : else
5452 : : {
5453 : 561748 : base_offset = ssize_int (0);
5454 : 561748 : init = ssize_int (0);
5455 : 561748 : base_name = get_name (DR_REF (dr));
5456 : : }
5457 : :
5458 : : /* Create base_offset */
5459 : 681095 : base_offset = size_binop (PLUS_EXPR,
5460 : : fold_convert (sizetype, base_offset),
5461 : : fold_convert (sizetype, init));
5462 : :
5463 : 681095 : if (offset)
5464 : : {
5465 : 3020 : offset = fold_convert (sizetype, offset);
5466 : 3020 : base_offset = fold_build2 (PLUS_EXPR, sizetype,
5467 : : base_offset, offset);
5468 : : }
5469 : :
5470 : : /* base + base_offset */
5471 : 681095 : if (loop_vinfo)
5472 : 119347 : addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
5473 : : else
5474 : 1123496 : addr_base = build1 (ADDR_EXPR,
5475 : 561748 : build_pointer_type (TREE_TYPE (DR_REF (dr))),
5476 : : /* Strip zero offset components since we don't need
5477 : : them and they can confuse late diagnostics if
5478 : : we CSE them wrongly. See PR106904 for example. */
5479 : : unshare_expr (strip_zero_offset_components
5480 : : (DR_REF (dr))));
5481 : :
5482 : 681095 : vect_ptr_type = build_pointer_type (TREE_TYPE (DR_REF (dr)));
5483 : 681095 : dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
5484 : 681095 : addr_base = force_gimple_operand (addr_base, &seq, true, dest);
5485 : 681095 : gimple_seq_add_seq (new_stmt_list, seq);
5486 : :
5487 : 681095 : if (DR_PTR_INFO (dr)
5488 : 167675 : && TREE_CODE (addr_base) == SSA_NAME
5489 : : /* We should only duplicate pointer info to newly created SSA names. */
5490 : 848148 : && SSA_NAME_VAR (addr_base) == dest)
5491 : : {
5492 : 140398 : gcc_assert (!SSA_NAME_PTR_INFO (addr_base));
5493 : 140398 : vect_duplicate_ssa_name_ptr_info (addr_base, dr_info);
5494 : : }
5495 : :
5496 : 681095 : if (dump_enabled_p ())
5497 : 23777 : dump_printf_loc (MSG_NOTE, vect_location, "created %T\n", addr_base);
5498 : :
5499 : 681095 : return addr_base;
5500 : : }
5501 : :
5502 : :
5503 : : /* Function vect_create_data_ref_ptr.
5504 : :
5505 : : Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
5506 : : location accessed in the loop by STMT_INFO, along with the def-use update
5507 : : chain to appropriately advance the pointer through the loop iterations.
5508 : : Also set aliasing information for the pointer. This pointer is used by
5509 : : the callers to this function to create a memory reference expression for
5510 : : vector load/store access.
5511 : :
5512 : : Input:
5513 : : 1. STMT_INFO: a stmt that references memory. Expected to be of the form
5514 : : GIMPLE_ASSIGN <name, data-ref> or
5515 : : GIMPLE_ASSIGN <data-ref, name>.
5516 : : 2. AGGR_TYPE: the type of the reference, which should be either a vector
5517 : : or an array.
5518 : : 3. AT_LOOP: the loop where the vector memref is to be created.
5519 : : 4. OFFSET (optional): a byte offset to be added to the initial address
5520 : : accessed by the data-ref in STMT_INFO.
5521 : : 5. BSI: location where the new stmts are to be placed if there is no loop
5522 : : 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
5523 : : pointing to the initial address.
5524 : : 8. IV_STEP (optional, defaults to NULL): the amount that should be added
5525 : : to the IV during each iteration of the loop. NULL says to move
5526 : : by one copy of AGGR_TYPE up or down, depending on the step of the
5527 : : data reference.
5528 : :
5529 : : Output:
5530 : : 1. Declare a new ptr to vector_type, and have it point to the base of the
5531 : : data reference (initial addressed accessed by the data reference).
5532 : : For example, for vector of type V8HI, the following code is generated:
5533 : :
5534 : : v8hi *ap;
5535 : : ap = (v8hi *)initial_address;
5536 : :
5537 : : if OFFSET is not supplied:
5538 : : initial_address = &a[init];
5539 : : if OFFSET is supplied:
5540 : : initial_address = &a[init] + OFFSET;
5541 : : if BYTE_OFFSET is supplied:
5542 : : initial_address = &a[init] + BYTE_OFFSET;
5543 : :
5544 : : Return the initial_address in INITIAL_ADDRESS.
5545 : :
5546 : : 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
5547 : : update the pointer in each iteration of the loop.
5548 : :
5549 : : Return the increment stmt that updates the pointer in PTR_INCR.
5550 : :
5551 : : 3. Return the pointer. */
5552 : :
5553 : : tree
5554 : 680883 : vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
5555 : : tree aggr_type, class loop *at_loop, tree offset,
5556 : : tree *initial_address, gimple_stmt_iterator *gsi,
5557 : : gimple **ptr_incr, bool only_init,
5558 : : tree iv_step)
5559 : : {
5560 : 680883 : const char *base_name;
5561 : 680883 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5562 : 680883 : class loop *loop = NULL;
5563 : 680883 : bool nested_in_vect_loop = false;
5564 : 680883 : class loop *containing_loop = NULL;
5565 : 680883 : tree aggr_ptr_type;
5566 : 680883 : tree aggr_ptr;
5567 : 680883 : tree new_temp;
5568 : 680883 : gimple_seq new_stmt_list = NULL;
5569 : 680883 : edge pe = NULL;
5570 : 680883 : basic_block new_bb;
5571 : 680883 : tree aggr_ptr_init;
5572 : 680883 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5573 : 680883 : struct data_reference *dr = dr_info->dr;
5574 : 680883 : tree aptr;
5575 : 680883 : gimple_stmt_iterator incr_gsi;
5576 : 680883 : bool insert_after;
5577 : 680883 : tree indx_before_incr, indx_after_incr;
5578 : 680883 : gimple *incr;
5579 : 680883 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5580 : :
5581 : 680883 : gcc_assert (iv_step != NULL_TREE
5582 : : || TREE_CODE (aggr_type) == ARRAY_TYPE
5583 : : || TREE_CODE (aggr_type) == VECTOR_TYPE);
5584 : :
5585 : 680883 : if (loop_vinfo)
5586 : : {
5587 : 119135 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5588 : 119135 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
5589 : 119135 : containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
5590 : 119135 : pe = loop_preheader_edge (loop);
5591 : : }
5592 : : else
5593 : : {
5594 : 561748 : gcc_assert (bb_vinfo);
5595 : 561748 : only_init = true;
5596 : 561748 : *ptr_incr = NULL;
5597 : : }
5598 : :
5599 : : /* Create an expression for the first address accessed by this load
5600 : : in LOOP. */
5601 : 680883 : base_name = get_name (DR_BASE_ADDRESS (dr));
5602 : :
5603 : 680883 : if (dump_enabled_p ())
5604 : : {
5605 : 23745 : tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
5606 : 23745 : dump_printf_loc (MSG_NOTE, vect_location,
5607 : : "create %s-pointer variable to type: %T",
5608 : 23745 : get_tree_code_name (TREE_CODE (aggr_type)),
5609 : : aggr_type);
5610 : 23745 : if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
5611 : 13018 : dump_printf (MSG_NOTE, " vectorizing an array ref: ");
5612 : 10727 : else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
5613 : 0 : dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
5614 : 10727 : else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
5615 : 1550 : dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
5616 : : else
5617 : 9177 : dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
5618 : 23745 : dump_printf (MSG_NOTE, "%T\n", DR_BASE_OBJECT (dr));
5619 : : }
5620 : :
5621 : : /* (1) Create the new aggregate-pointer variable.
5622 : : Vector and array types inherit the alias set of their component
5623 : : type by default so we need to use a ref-all pointer if the data
5624 : : reference does not conflict with the created aggregated data
5625 : : reference because it is not addressable. */
5626 : 680883 : bool need_ref_all = false;
5627 : 680883 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5628 : : get_alias_set (DR_REF (dr))))
5629 : : need_ref_all = true;
5630 : : /* Likewise for any of the data references in the stmt group. */
5631 : 566498 : else if (DR_GROUP_SIZE (stmt_info) > 1)
5632 : : {
5633 : 461071 : stmt_vec_info sinfo = DR_GROUP_FIRST_ELEMENT (stmt_info);
5634 : 1268593 : do
5635 : : {
5636 : 1268593 : struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
5637 : 1268593 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5638 : : get_alias_set (DR_REF (sdr))))
5639 : : {
5640 : : need_ref_all = true;
5641 : : break;
5642 : : }
5643 : 1266737 : sinfo = DR_GROUP_NEXT_ELEMENT (sinfo);
5644 : : }
5645 : 1266737 : while (sinfo);
5646 : : }
5647 : 680883 : aggr_ptr_type = build_pointer_type_for_mode (aggr_type, VOIDmode,
5648 : : need_ref_all);
5649 : 680883 : aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
5650 : :
5651 : :
5652 : : /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
5653 : : vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
5654 : : def-use update cycles for the pointer: one relative to the outer-loop
5655 : : (LOOP), which is what steps (3) and (4) below do. The other is relative
5656 : : to the inner-loop (which is the inner-most loop containing the dataref),
5657 : : and this is done be step (5) below.
5658 : :
5659 : : When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
5660 : : inner-most loop, and so steps (3),(4) work the same, and step (5) is
5661 : : redundant. Steps (3),(4) create the following:
5662 : :
5663 : : vp0 = &base_addr;
5664 : : LOOP: vp1 = phi(vp0,vp2)
5665 : : ...
5666 : : ...
5667 : : vp2 = vp1 + step
5668 : : goto LOOP
5669 : :
5670 : : If there is an inner-loop nested in loop, then step (5) will also be
5671 : : applied, and an additional update in the inner-loop will be created:
5672 : :
5673 : : vp0 = &base_addr;
5674 : : LOOP: vp1 = phi(vp0,vp2)
5675 : : ...
5676 : : inner: vp3 = phi(vp1,vp4)
5677 : : vp4 = vp3 + inner_step
5678 : : if () goto inner
5679 : : ...
5680 : : vp2 = vp1 + step
5681 : : if () goto LOOP */
5682 : :
5683 : : /* (2) Calculate the initial address of the aggregate-pointer, and set
5684 : : the aggregate-pointer to point to it before the loop. */
5685 : :
5686 : : /* Create: (&(base[init_val]+offset) in the loop preheader. */
5687 : :
5688 : 680883 : new_temp = vect_create_addr_base_for_vector_ref (vinfo,
5689 : : stmt_info, &new_stmt_list,
5690 : : offset);
5691 : 680883 : if (new_stmt_list)
5692 : : {
5693 : 162036 : if (pe)
5694 : : {
5695 : 52275 : new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
5696 : 52275 : gcc_assert (!new_bb);
5697 : : }
5698 : : else
5699 : 109761 : gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
5700 : : }
5701 : :
5702 : 680883 : *initial_address = new_temp;
5703 : 680883 : aggr_ptr_init = new_temp;
5704 : :
5705 : : /* (3) Handle the updating of the aggregate-pointer inside the loop.
5706 : : This is needed when ONLY_INIT is false, and also when AT_LOOP is the
5707 : : inner-loop nested in LOOP (during outer-loop vectorization). */
5708 : :
5709 : : /* No update in loop is required. */
5710 : 680883 : if (only_init && (!loop_vinfo || at_loop == loop))
5711 : : aptr = aggr_ptr_init;
5712 : : else
5713 : : {
5714 : : /* Accesses to invariant addresses should be handled specially
5715 : : by the caller. */
5716 : 119127 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
5717 : 119127 : gcc_assert (!integer_zerop (step));
5718 : :
5719 : 119127 : if (iv_step == NULL_TREE)
5720 : : {
5721 : : /* The step of the aggregate pointer is the type size,
5722 : : negated for downward accesses. */
5723 : 0 : iv_step = TYPE_SIZE_UNIT (aggr_type);
5724 : 0 : if (tree_int_cst_sgn (step) == -1)
5725 : 0 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
5726 : : }
5727 : :
5728 : 119127 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5729 : :
5730 : 119127 : create_iv (aggr_ptr_init, PLUS_EXPR,
5731 : : fold_convert (aggr_ptr_type, iv_step),
5732 : : aggr_ptr, loop, &incr_gsi, insert_after,
5733 : : &indx_before_incr, &indx_after_incr);
5734 : 119127 : incr = gsi_stmt (incr_gsi);
5735 : :
5736 : : /* Copy the points-to information if it exists. */
5737 : 119127 : if (DR_PTR_INFO (dr))
5738 : : {
5739 : 64880 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
5740 : 64880 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
5741 : : }
5742 : 119127 : if (ptr_incr)
5743 : 119127 : *ptr_incr = incr;
5744 : :
5745 : 119127 : aptr = indx_before_incr;
5746 : : }
5747 : :
5748 : 680883 : if (!nested_in_vect_loop || only_init)
5749 : : return aptr;
5750 : :
5751 : :
5752 : : /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
5753 : : nested in LOOP, if exists. */
5754 : :
5755 : 339 : gcc_assert (nested_in_vect_loop);
5756 : 339 : if (!only_init)
5757 : : {
5758 : 339 : standard_iv_increment_position (containing_loop, &incr_gsi,
5759 : : &insert_after);
5760 : 339 : create_iv (aptr, PLUS_EXPR, fold_convert (aggr_ptr_type, DR_STEP (dr)),
5761 : : aggr_ptr, containing_loop, &incr_gsi, insert_after,
5762 : : &indx_before_incr, &indx_after_incr);
5763 : 339 : incr = gsi_stmt (incr_gsi);
5764 : :
5765 : : /* Copy the points-to information if it exists. */
5766 : 339 : if (DR_PTR_INFO (dr))
5767 : : {
5768 : 75 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
5769 : 75 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
5770 : : }
5771 : 339 : if (ptr_incr)
5772 : 339 : *ptr_incr = incr;
5773 : :
5774 : 339 : return indx_before_incr;
5775 : : }
5776 : : else
5777 : : gcc_unreachable ();
5778 : : }
5779 : :
5780 : :
5781 : : /* Function bump_vector_ptr
5782 : :
5783 : : Increment a pointer (to a vector type) by vector-size. If requested,
5784 : : i.e. if PTR-INCR is given, then also connect the new increment stmt
5785 : : to the existing def-use update-chain of the pointer, by modifying
5786 : : the PTR_INCR as illustrated below:
5787 : :
5788 : : The pointer def-use update-chain before this function:
5789 : : DATAREF_PTR = phi (p_0, p_2)
5790 : : ....
5791 : : PTR_INCR: p_2 = DATAREF_PTR + step
5792 : :
5793 : : The pointer def-use update-chain after this function:
5794 : : DATAREF_PTR = phi (p_0, p_2)
5795 : : ....
5796 : : NEW_DATAREF_PTR = DATAREF_PTR + BUMP
5797 : : ....
5798 : : PTR_INCR: p_2 = NEW_DATAREF_PTR + step
5799 : :
5800 : : Input:
5801 : : DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
5802 : : in the loop.
5803 : : PTR_INCR - optional. The stmt that updates the pointer in each iteration of
5804 : : the loop. The increment amount across iterations is expected
5805 : : to be vector_size.
5806 : : BSI - location where the new update stmt is to be placed.
5807 : : STMT_INFO - the original scalar memory-access stmt that is being vectorized.
5808 : : BUMP - optional. The offset by which to bump the pointer. If not given,
5809 : : the offset is assumed to be vector_size.
5810 : :
5811 : : Output: Return NEW_DATAREF_PTR as illustrated above.
5812 : :
5813 : : */
5814 : :
5815 : : tree
5816 : 197340 : bump_vector_ptr (vec_info *vinfo,
5817 : : tree dataref_ptr, gimple *ptr_incr, gimple_stmt_iterator *gsi,
5818 : : stmt_vec_info stmt_info, tree bump)
5819 : : {
5820 : 197340 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
5821 : 197340 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5822 : 197340 : tree update = TYPE_SIZE_UNIT (vectype);
5823 : 197340 : gimple *incr_stmt;
5824 : 197340 : ssa_op_iter iter;
5825 : 197340 : use_operand_p use_p;
5826 : 197340 : tree new_dataref_ptr;
5827 : :
5828 : 197340 : if (bump)
5829 : 197340 : update = bump;
5830 : :
5831 : 197340 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
5832 : 78551 : new_dataref_ptr = copy_ssa_name (dataref_ptr);
5833 : 118789 : else if (is_gimple_min_invariant (dataref_ptr))
5834 : : /* When possible avoid emitting a separate increment stmt that will
5835 : : force the addressed object addressable. */
5836 : 237578 : return build1 (ADDR_EXPR, TREE_TYPE (dataref_ptr),
5837 : 118789 : fold_build2 (MEM_REF,
5838 : : TREE_TYPE (TREE_TYPE (dataref_ptr)),
5839 : : dataref_ptr,
5840 : 118789 : fold_convert (ptr_type_node, update)));
5841 : : else
5842 : 0 : new_dataref_ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
5843 : 78551 : incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
5844 : : dataref_ptr, update);
5845 : 78551 : vect_finish_stmt_generation (vinfo, stmt_info, incr_stmt, gsi);
5846 : : /* Fold the increment, avoiding excessive chains use-def chains of
5847 : : those, leading to compile-time issues for passes until the next
5848 : : forwprop pass which would do this as well. */
5849 : 78551 : gimple_stmt_iterator fold_gsi = gsi_for_stmt (incr_stmt);
5850 : 78551 : if (fold_stmt (&fold_gsi, follow_all_ssa_edges))
5851 : : {
5852 : 42922 : incr_stmt = gsi_stmt (fold_gsi);
5853 : 42922 : update_stmt (incr_stmt);
5854 : : }
5855 : :
5856 : : /* Copy the points-to information if it exists. */
5857 : 78551 : if (DR_PTR_INFO (dr))
5858 : : {
5859 : 55719 : duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
5860 : 55719 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
5861 : : }
5862 : :
5863 : 78551 : if (!ptr_incr)
5864 : : return new_dataref_ptr;
5865 : :
5866 : : /* Update the vector-pointer's cross-iteration increment. */
5867 : 79766 : FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
5868 : : {
5869 : 39883 : tree use = USE_FROM_PTR (use_p);
5870 : :
5871 : 39883 : if (use == dataref_ptr)
5872 : 39883 : SET_USE (use_p, new_dataref_ptr);
5873 : : else
5874 : 0 : gcc_assert (operand_equal_p (use, update, 0));
5875 : : }
5876 : :
5877 : : return new_dataref_ptr;
5878 : : }
5879 : :
5880 : :
5881 : : /* Copy memory reference info such as base/clique from the SRC reference
5882 : : to the DEST MEM_REF. */
5883 : :
5884 : : void
5885 : 890259 : vect_copy_ref_info (tree dest, tree src)
5886 : : {
5887 : 890259 : if (TREE_CODE (dest) != MEM_REF)
5888 : : return;
5889 : :
5890 : : tree src_base = src;
5891 : 1838928 : while (handled_component_p (src_base))
5892 : 949194 : src_base = TREE_OPERAND (src_base, 0);
5893 : 889734 : if (TREE_CODE (src_base) != MEM_REF
5894 : 889734 : && TREE_CODE (src_base) != TARGET_MEM_REF)
5895 : : return;
5896 : :
5897 : 466427 : MR_DEPENDENCE_CLIQUE (dest) = MR_DEPENDENCE_CLIQUE (src_base);
5898 : 466427 : MR_DEPENDENCE_BASE (dest) = MR_DEPENDENCE_BASE (src_base);
5899 : : }
5900 : :
5901 : :
5902 : : /* Function vect_create_destination_var.
5903 : :
5904 : : Create a new temporary of type VECTYPE. */
5905 : :
5906 : : tree
5907 : 450160 : vect_create_destination_var (tree scalar_dest, tree vectype)
5908 : : {
5909 : 450160 : tree vec_dest;
5910 : 450160 : const char *name;
5911 : 450160 : char *new_name;
5912 : 450160 : tree type;
5913 : 450160 : enum vect_var_kind kind;
5914 : :
5915 : 450160 : kind = vectype
5916 : 878290 : ? VECTOR_BOOLEAN_TYPE_P (vectype)
5917 : 428130 : ? vect_mask_var
5918 : : : vect_simple_var
5919 : : : vect_scalar_var;
5920 : 22030 : type = vectype ? vectype : TREE_TYPE (scalar_dest);
5921 : :
5922 : 450160 : gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
5923 : :
5924 : 450160 : name = get_name (scalar_dest);
5925 : 450160 : if (name)
5926 : 163960 : new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
5927 : : else
5928 : 286200 : new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
5929 : 450160 : vec_dest = vect_get_new_vect_var (type, kind, new_name);
5930 : 450160 : free (new_name);
5931 : :
5932 : 450160 : return vec_dest;
5933 : : }
5934 : :
5935 : : /* Function vect_grouped_store_supported.
5936 : :
5937 : : Returns TRUE if interleave high and interleave low permutations
5938 : : are supported, and FALSE otherwise. */
5939 : :
5940 : : bool
5941 : 2650 : vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
5942 : : {
5943 : 2650 : machine_mode mode = TYPE_MODE (vectype);
5944 : :
5945 : : /* vect_permute_store_chain requires the group size to be equal to 3 or
5946 : : be a power of two. */
5947 : 2650 : if (count != 3 && exact_log2 (count) == -1)
5948 : : {
5949 : 563 : if (dump_enabled_p ())
5950 : 9 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5951 : : "the size of the group of accesses"
5952 : : " is not a power of 2 or not eqaul to 3\n");
5953 : 563 : return false;
5954 : : }
5955 : :
5956 : : /* Check that the permutation is supported. */
5957 : 2087 : if (VECTOR_MODE_P (mode))
5958 : : {
5959 : 2087 : unsigned int i;
5960 : 2087 : if (count == 3)
5961 : : {
5962 : 958 : unsigned int j0 = 0, j1 = 0, j2 = 0;
5963 : 958 : unsigned int i, j;
5964 : :
5965 : 958 : unsigned int nelt;
5966 : 1916 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
5967 : : {
5968 : : if (dump_enabled_p ())
5969 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5970 : : "cannot handle groups of 3 stores for"
5971 : : " variable-length vectors\n");
5972 : : return false;
5973 : : }
5974 : :
5975 : 958 : vec_perm_builder sel (nelt, nelt, 1);
5976 : 958 : sel.quick_grow (nelt);
5977 : 958 : vec_perm_indices indices;
5978 : 3607 : for (j = 0; j < 3; j++)
5979 : : {
5980 : 2724 : int nelt0 = ((3 - j) * nelt) % 3;
5981 : 2724 : int nelt1 = ((3 - j) * nelt + 1) % 3;
5982 : 2724 : int nelt2 = ((3 - j) * nelt + 2) % 3;
5983 : 9684 : for (i = 0; i < nelt; i++)
5984 : : {
5985 : 6960 : if (3 * i + nelt0 < nelt)
5986 : 2358 : sel[3 * i + nelt0] = j0++;
5987 : 6960 : if (3 * i + nelt1 < nelt)
5988 : 2319 : sel[3 * i + nelt1] = nelt + j1++;
5989 : 6960 : if (3 * i + nelt2 < nelt)
5990 : 2283 : sel[3 * i + nelt2] = 0;
5991 : : }
5992 : 2724 : indices.new_vector (sel, 2, nelt);
5993 : 2724 : if (!can_vec_perm_const_p (mode, mode, indices))
5994 : : {
5995 : 66 : if (dump_enabled_p ())
5996 : 37 : dump_printf (MSG_MISSED_OPTIMIZATION,
5997 : : "permutation op not supported by target.\n");
5998 : 66 : return false;
5999 : : }
6000 : :
6001 : 9042 : for (i = 0; i < nelt; i++)
6002 : : {
6003 : 6384 : if (3 * i + nelt0 < nelt)
6004 : 2134 : sel[3 * i + nelt0] = 3 * i + nelt0;
6005 : 6384 : if (3 * i + nelt1 < nelt)
6006 : 2125 : sel[3 * i + nelt1] = 3 * i + nelt1;
6007 : 6384 : if (3 * i + nelt2 < nelt)
6008 : 2125 : sel[3 * i + nelt2] = nelt + j2++;
6009 : : }
6010 : 2658 : indices.new_vector (sel, 2, nelt);
6011 : 2658 : if (!can_vec_perm_const_p (mode, mode, indices))
6012 : : {
6013 : 9 : if (dump_enabled_p ())
6014 : 9 : dump_printf (MSG_MISSED_OPTIMIZATION,
6015 : : "permutation op not supported by target.\n");
6016 : 9 : return false;
6017 : : }
6018 : : }
6019 : : return true;
6020 : 958 : }
6021 : : else
6022 : : {
6023 : : /* If length is not equal to 3 then only power of 2 is supported. */
6024 : 1129 : gcc_assert (pow2p_hwi (count));
6025 : 2258 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6026 : :
6027 : : /* The encoding has 2 interleaved stepped patterns. */
6028 : 2258 : if(!multiple_p (nelt, 2))
6029 : 1081 : return false;
6030 : 1129 : vec_perm_builder sel (nelt, 2, 3);
6031 : 1129 : sel.quick_grow (6);
6032 : 5645 : for (i = 0; i < 3; i++)
6033 : : {
6034 : 3387 : sel[i * 2] = i;
6035 : 3387 : sel[i * 2 + 1] = i + nelt;
6036 : : }
6037 : 1129 : vec_perm_indices indices (sel, 2, nelt);
6038 : 1129 : if (can_vec_perm_const_p (mode, mode, indices))
6039 : : {
6040 : 7567 : for (i = 0; i < 6; i++)
6041 : 6486 : sel[i] += exact_div (nelt, 2);
6042 : 1081 : indices.new_vector (sel, 2, nelt);
6043 : 1081 : if (can_vec_perm_const_p (mode, mode, indices))
6044 : 1081 : return true;
6045 : : }
6046 : 1129 : }
6047 : : }
6048 : :
6049 : 48 : if (dump_enabled_p ())
6050 : 3 : dump_printf (MSG_MISSED_OPTIMIZATION,
6051 : : "permutation op not supported by target.\n");
6052 : : return false;
6053 : : }
6054 : :
6055 : : /* Return FN if vec_{mask_,mask_len_}store_lanes is available for COUNT vectors
6056 : : of type VECTYPE. MASKED_P says whether the masked form is needed. */
6057 : :
6058 : : internal_fn
6059 : 28570 : vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6060 : : bool masked_p)
6061 : : {
6062 : 28570 : if (vect_lanes_optab_supported_p ("vec_mask_len_store_lanes",
6063 : : vec_mask_len_store_lanes_optab, vectype,
6064 : : count))
6065 : : return IFN_MASK_LEN_STORE_LANES;
6066 : 28570 : else if (masked_p)
6067 : : {
6068 : 160 : if (vect_lanes_optab_supported_p ("vec_mask_store_lanes",
6069 : : vec_mask_store_lanes_optab, vectype,
6070 : : count))
6071 : : return IFN_MASK_STORE_LANES;
6072 : : }
6073 : : else
6074 : : {
6075 : 28410 : if (vect_lanes_optab_supported_p ("vec_store_lanes",
6076 : : vec_store_lanes_optab, vectype, count))
6077 : : return IFN_STORE_LANES;
6078 : : }
6079 : : return IFN_LAST;
6080 : : }
6081 : :
6082 : :
6083 : : /* Function vect_permute_store_chain.
6084 : :
6085 : : Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
6086 : : a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
6087 : : the data correctly for the stores. Return the final references for stores
6088 : : in RESULT_CHAIN.
6089 : :
6090 : : E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
6091 : : The input is 4 vectors each containing 8 elements. We assign a number to
6092 : : each element, the input sequence is:
6093 : :
6094 : : 1st vec: 0 1 2 3 4 5 6 7
6095 : : 2nd vec: 8 9 10 11 12 13 14 15
6096 : : 3rd vec: 16 17 18 19 20 21 22 23
6097 : : 4th vec: 24 25 26 27 28 29 30 31
6098 : :
6099 : : The output sequence should be:
6100 : :
6101 : : 1st vec: 0 8 16 24 1 9 17 25
6102 : : 2nd vec: 2 10 18 26 3 11 19 27
6103 : : 3rd vec: 4 12 20 28 5 13 21 30
6104 : : 4th vec: 6 14 22 30 7 15 23 31
6105 : :
6106 : : i.e., we interleave the contents of the four vectors in their order.
6107 : :
6108 : : We use interleave_high/low instructions to create such output. The input of
6109 : : each interleave_high/low operation is two vectors:
6110 : : 1st vec 2nd vec
6111 : : 0 1 2 3 4 5 6 7
6112 : : the even elements of the result vector are obtained left-to-right from the
6113 : : high/low elements of the first vector. The odd elements of the result are
6114 : : obtained left-to-right from the high/low elements of the second vector.
6115 : : The output of interleave_high will be: 0 4 1 5
6116 : : and of interleave_low: 2 6 3 7
6117 : :
6118 : :
6119 : : The permutation is done in log LENGTH stages. In each stage interleave_high
6120 : : and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
6121 : : where the first argument is taken from the first half of DR_CHAIN and the
6122 : : second argument from it's second half.
6123 : : In our example,
6124 : :
6125 : : I1: interleave_high (1st vec, 3rd vec)
6126 : : I2: interleave_low (1st vec, 3rd vec)
6127 : : I3: interleave_high (2nd vec, 4th vec)
6128 : : I4: interleave_low (2nd vec, 4th vec)
6129 : :
6130 : : The output for the first stage is:
6131 : :
6132 : : I1: 0 16 1 17 2 18 3 19
6133 : : I2: 4 20 5 21 6 22 7 23
6134 : : I3: 8 24 9 25 10 26 11 27
6135 : : I4: 12 28 13 29 14 30 15 31
6136 : :
6137 : : The output of the second stage, i.e. the final result is:
6138 : :
6139 : : I1: 0 8 16 24 1 9 17 25
6140 : : I2: 2 10 18 26 3 11 19 27
6141 : : I3: 4 12 20 28 5 13 21 30
6142 : : I4: 6 14 22 30 7 15 23 31. */
6143 : :
6144 : : void
6145 : 0 : vect_permute_store_chain (vec_info *vinfo, vec<tree> &dr_chain,
6146 : : unsigned int length,
6147 : : stmt_vec_info stmt_info,
6148 : : gimple_stmt_iterator *gsi,
6149 : : vec<tree> *result_chain)
6150 : : {
6151 : 0 : tree vect1, vect2, high, low;
6152 : 0 : gimple *perm_stmt;
6153 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6154 : 0 : tree perm_mask_low, perm_mask_high;
6155 : 0 : tree data_ref;
6156 : 0 : tree perm3_mask_low, perm3_mask_high;
6157 : 0 : unsigned int i, j, n, log_length = exact_log2 (length);
6158 : :
6159 : 0 : result_chain->quick_grow (length);
6160 : 0 : memcpy (result_chain->address (), dr_chain.address (),
6161 : : length * sizeof (tree));
6162 : :
6163 : 0 : if (length == 3)
6164 : : {
6165 : : /* vect_grouped_store_supported ensures that this is constant. */
6166 : 0 : unsigned int nelt = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
6167 : 0 : unsigned int j0 = 0, j1 = 0, j2 = 0;
6168 : :
6169 : 0 : vec_perm_builder sel (nelt, nelt, 1);
6170 : 0 : sel.quick_grow (nelt);
6171 : 0 : vec_perm_indices indices;
6172 : 0 : for (j = 0; j < 3; j++)
6173 : : {
6174 : 0 : int nelt0 = ((3 - j) * nelt) % 3;
6175 : 0 : int nelt1 = ((3 - j) * nelt + 1) % 3;
6176 : 0 : int nelt2 = ((3 - j) * nelt + 2) % 3;
6177 : :
6178 : 0 : for (i = 0; i < nelt; i++)
6179 : : {
6180 : 0 : if (3 * i + nelt0 < nelt)
6181 : 0 : sel[3 * i + nelt0] = j0++;
6182 : 0 : if (3 * i + nelt1 < nelt)
6183 : 0 : sel[3 * i + nelt1] = nelt + j1++;
6184 : 0 : if (3 * i + nelt2 < nelt)
6185 : 0 : sel[3 * i + nelt2] = 0;
6186 : : }
6187 : 0 : indices.new_vector (sel, 2, nelt);
6188 : 0 : perm3_mask_low = vect_gen_perm_mask_checked (vectype, indices);
6189 : :
6190 : 0 : for (i = 0; i < nelt; i++)
6191 : : {
6192 : 0 : if (3 * i + nelt0 < nelt)
6193 : 0 : sel[3 * i + nelt0] = 3 * i + nelt0;
6194 : 0 : if (3 * i + nelt1 < nelt)
6195 : 0 : sel[3 * i + nelt1] = 3 * i + nelt1;
6196 : 0 : if (3 * i + nelt2 < nelt)
6197 : 0 : sel[3 * i + nelt2] = nelt + j2++;
6198 : : }
6199 : 0 : indices.new_vector (sel, 2, nelt);
6200 : 0 : perm3_mask_high = vect_gen_perm_mask_checked (vectype, indices);
6201 : :
6202 : 0 : vect1 = dr_chain[0];
6203 : 0 : vect2 = dr_chain[1];
6204 : :
6205 : : /* Create interleaving stmt:
6206 : : low = VEC_PERM_EXPR <vect1, vect2,
6207 : : {j, nelt, *, j + 1, nelt + j + 1, *,
6208 : : j + 2, nelt + j + 2, *, ...}> */
6209 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
6210 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
6211 : : vect2, perm3_mask_low);
6212 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6213 : :
6214 : 0 : vect1 = data_ref;
6215 : 0 : vect2 = dr_chain[2];
6216 : : /* Create interleaving stmt:
6217 : : low = VEC_PERM_EXPR <vect1, vect2,
6218 : : {0, 1, nelt + j, 3, 4, nelt + j + 1,
6219 : : 6, 7, nelt + j + 2, ...}> */
6220 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
6221 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
6222 : : vect2, perm3_mask_high);
6223 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6224 : 0 : (*result_chain)[j] = data_ref;
6225 : : }
6226 : 0 : }
6227 : : else
6228 : : {
6229 : : /* If length is not equal to 3 then only power of 2 is supported. */
6230 : 0 : gcc_assert (pow2p_hwi (length));
6231 : :
6232 : : /* The encoding has 2 interleaved stepped patterns. */
6233 : 0 : poly_uint64 nelt = TYPE_VECTOR_SUBPARTS (vectype);
6234 : 0 : vec_perm_builder sel (nelt, 2, 3);
6235 : 0 : sel.quick_grow (6);
6236 : 0 : for (i = 0; i < 3; i++)
6237 : : {
6238 : 0 : sel[i * 2] = i;
6239 : 0 : sel[i * 2 + 1] = i + nelt;
6240 : : }
6241 : 0 : vec_perm_indices indices (sel, 2, nelt);
6242 : 0 : perm_mask_high = vect_gen_perm_mask_checked (vectype, indices);
6243 : :
6244 : 0 : for (i = 0; i < 6; i++)
6245 : 0 : sel[i] += exact_div (nelt, 2);
6246 : 0 : indices.new_vector (sel, 2, nelt);
6247 : 0 : perm_mask_low = vect_gen_perm_mask_checked (vectype, indices);
6248 : :
6249 : 0 : for (i = 0, n = log_length; i < n; i++)
6250 : : {
6251 : 0 : for (j = 0; j < length/2; j++)
6252 : : {
6253 : 0 : vect1 = dr_chain[j];
6254 : 0 : vect2 = dr_chain[j+length/2];
6255 : :
6256 : : /* Create interleaving stmt:
6257 : : high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
6258 : : ...}> */
6259 : 0 : high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
6260 : 0 : perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
6261 : : vect2, perm_mask_high);
6262 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6263 : 0 : (*result_chain)[2*j] = high;
6264 : :
6265 : : /* Create interleaving stmt:
6266 : : low = VEC_PERM_EXPR <vect1, vect2,
6267 : : {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
6268 : : ...}> */
6269 : 0 : low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
6270 : 0 : perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
6271 : : vect2, perm_mask_low);
6272 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6273 : 0 : (*result_chain)[2*j+1] = low;
6274 : : }
6275 : 0 : memcpy (dr_chain.address (), result_chain->address (),
6276 : : length * sizeof (tree));
6277 : : }
6278 : 0 : }
6279 : 0 : }
6280 : :
6281 : : /* Function vect_setup_realignment
6282 : :
6283 : : This function is called when vectorizing an unaligned load using
6284 : : the dr_explicit_realign[_optimized] scheme.
6285 : : This function generates the following code at the loop prolog:
6286 : :
6287 : : p = initial_addr;
6288 : : x msq_init = *(floor(p)); # prolog load
6289 : : realignment_token = call target_builtin;
6290 : : loop:
6291 : : x msq = phi (msq_init, ---)
6292 : :
6293 : : The stmts marked with x are generated only for the case of
6294 : : dr_explicit_realign_optimized.
6295 : :
6296 : : The code above sets up a new (vector) pointer, pointing to the first
6297 : : location accessed by STMT_INFO, and a "floor-aligned" load using that
6298 : : pointer. It also generates code to compute the "realignment-token"
6299 : : (if the relevant target hook was defined), and creates a phi-node at the
6300 : : loop-header bb whose arguments are the result of the prolog-load (created
6301 : : by this function) and the result of a load that takes place in the loop
6302 : : (to be created by the caller to this function).
6303 : :
6304 : : For the case of dr_explicit_realign_optimized:
6305 : : The caller to this function uses the phi-result (msq) to create the
6306 : : realignment code inside the loop, and sets up the missing phi argument,
6307 : : as follows:
6308 : : loop:
6309 : : msq = phi (msq_init, lsq)
6310 : : lsq = *(floor(p')); # load in loop
6311 : : result = realign_load (msq, lsq, realignment_token);
6312 : :
6313 : : For the case of dr_explicit_realign:
6314 : : loop:
6315 : : msq = *(floor(p)); # load in loop
6316 : : p' = p + (VS-1);
6317 : : lsq = *(floor(p')); # load in loop
6318 : : result = realign_load (msq, lsq, realignment_token);
6319 : :
6320 : : Input:
6321 : : STMT_INFO - (scalar) load stmt to be vectorized. This load accesses
6322 : : a memory location that may be unaligned.
6323 : : BSI - place where new code is to be inserted.
6324 : : ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
6325 : : is used.
6326 : :
6327 : : Output:
6328 : : REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
6329 : : target hook, if defined.
6330 : : Return value - the result of the loop-header phi node. */
6331 : :
6332 : : tree
6333 : 0 : vect_setup_realignment (vec_info *vinfo, stmt_vec_info stmt_info,
6334 : : gimple_stmt_iterator *gsi, tree *realignment_token,
6335 : : enum dr_alignment_support alignment_support_scheme,
6336 : : tree init_addr,
6337 : : class loop **at_loop)
6338 : : {
6339 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6340 : 0 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6341 : 0 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
6342 : 0 : struct data_reference *dr = dr_info->dr;
6343 : 0 : class loop *loop = NULL;
6344 : 0 : edge pe = NULL;
6345 : 0 : tree scalar_dest = gimple_assign_lhs (stmt_info->stmt);
6346 : 0 : tree vec_dest;
6347 : 0 : gimple *inc;
6348 : 0 : tree ptr;
6349 : 0 : tree data_ref;
6350 : 0 : basic_block new_bb;
6351 : 0 : tree msq_init = NULL_TREE;
6352 : 0 : tree new_temp;
6353 : 0 : gphi *phi_stmt;
6354 : 0 : tree msq = NULL_TREE;
6355 : 0 : gimple_seq stmts = NULL;
6356 : 0 : bool compute_in_loop = false;
6357 : 0 : bool nested_in_vect_loop = false;
6358 : 0 : class loop *containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
6359 : 0 : class loop *loop_for_initial_load = NULL;
6360 : :
6361 : 0 : if (loop_vinfo)
6362 : : {
6363 : 0 : loop = LOOP_VINFO_LOOP (loop_vinfo);
6364 : 0 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
6365 : : }
6366 : :
6367 : 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign
6368 : : || alignment_support_scheme == dr_explicit_realign_optimized);
6369 : :
6370 : : /* We need to generate three things:
6371 : : 1. the misalignment computation
6372 : : 2. the extra vector load (for the optimized realignment scheme).
6373 : : 3. the phi node for the two vectors from which the realignment is
6374 : : done (for the optimized realignment scheme). */
6375 : :
6376 : : /* 1. Determine where to generate the misalignment computation.
6377 : :
6378 : : If INIT_ADDR is NULL_TREE, this indicates that the misalignment
6379 : : calculation will be generated by this function, outside the loop (in the
6380 : : preheader). Otherwise, INIT_ADDR had already been computed for us by the
6381 : : caller, inside the loop.
6382 : :
6383 : : Background: If the misalignment remains fixed throughout the iterations of
6384 : : the loop, then both realignment schemes are applicable, and also the
6385 : : misalignment computation can be done outside LOOP. This is because we are
6386 : : vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
6387 : : are a multiple of VS (the Vector Size), and therefore the misalignment in
6388 : : different vectorized LOOP iterations is always the same.
6389 : : The problem arises only if the memory access is in an inner-loop nested
6390 : : inside LOOP, which is now being vectorized using outer-loop vectorization.
6391 : : This is the only case when the misalignment of the memory access may not
6392 : : remain fixed throughout the iterations of the inner-loop (as explained in
6393 : : detail in vect_supportable_dr_alignment). In this case, not only is the
6394 : : optimized realignment scheme not applicable, but also the misalignment
6395 : : computation (and generation of the realignment token that is passed to
6396 : : REALIGN_LOAD) have to be done inside the loop.
6397 : :
6398 : : In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
6399 : : or not, which in turn determines if the misalignment is computed inside
6400 : : the inner-loop, or outside LOOP. */
6401 : :
6402 : 0 : if (init_addr != NULL_TREE || !loop_vinfo)
6403 : : {
6404 : 0 : compute_in_loop = true;
6405 : 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign);
6406 : : }
6407 : :
6408 : :
6409 : : /* 2. Determine where to generate the extra vector load.
6410 : :
6411 : : For the optimized realignment scheme, instead of generating two vector
6412 : : loads in each iteration, we generate a single extra vector load in the
6413 : : preheader of the loop, and in each iteration reuse the result of the
6414 : : vector load from the previous iteration. In case the memory access is in
6415 : : an inner-loop nested inside LOOP, which is now being vectorized using
6416 : : outer-loop vectorization, we need to determine whether this initial vector
6417 : : load should be generated at the preheader of the inner-loop, or can be
6418 : : generated at the preheader of LOOP. If the memory access has no evolution
6419 : : in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
6420 : : to be generated inside LOOP (in the preheader of the inner-loop). */
6421 : :
6422 : 0 : if (nested_in_vect_loop)
6423 : : {
6424 : 0 : tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
6425 : 0 : bool invariant_in_outerloop =
6426 : 0 : (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
6427 : 0 : loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
6428 : : }
6429 : : else
6430 : : loop_for_initial_load = loop;
6431 : 0 : if (at_loop)
6432 : 0 : *at_loop = loop_for_initial_load;
6433 : :
6434 : 0 : tree vuse = NULL_TREE;
6435 : 0 : if (loop_for_initial_load)
6436 : : {
6437 : 0 : pe = loop_preheader_edge (loop_for_initial_load);
6438 : 0 : if (gphi *vphi = get_virtual_phi (loop_for_initial_load->header))
6439 : 0 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
6440 : : }
6441 : 0 : if (!vuse)
6442 : 0 : vuse = gimple_vuse (gsi_stmt (*gsi));
6443 : :
6444 : : /* 3. For the case of the optimized realignment, create the first vector
6445 : : load at the loop preheader. */
6446 : :
6447 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
6448 : : {
6449 : : /* Create msq_init = *(floor(p1)) in the loop preheader */
6450 : 0 : gassign *new_stmt;
6451 : :
6452 : 0 : gcc_assert (!compute_in_loop);
6453 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6454 : 0 : ptr = vect_create_data_ref_ptr (vinfo, stmt_info, vectype,
6455 : : loop_for_initial_load, NULL_TREE,
6456 : : &init_addr, NULL, &inc, true);
6457 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
6458 : 0 : new_temp = copy_ssa_name (ptr);
6459 : : else
6460 : 0 : new_temp = make_ssa_name (TREE_TYPE (ptr));
6461 : 0 : poly_uint64 align = DR_TARGET_ALIGNMENT (dr_info);
6462 : 0 : tree type = TREE_TYPE (ptr);
6463 : 0 : new_stmt = gimple_build_assign
6464 : 0 : (new_temp, BIT_AND_EXPR, ptr,
6465 : 0 : fold_build2 (MINUS_EXPR, type,
6466 : : build_int_cst (type, 0),
6467 : : build_int_cst (type, align)));
6468 : 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6469 : 0 : gcc_assert (!new_bb);
6470 : 0 : data_ref
6471 : 0 : = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
6472 : : build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
6473 : 0 : vect_copy_ref_info (data_ref, DR_REF (dr));
6474 : 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
6475 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6476 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6477 : 0 : gimple_set_vuse (new_stmt, vuse);
6478 : 0 : if (pe)
6479 : : {
6480 : 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6481 : 0 : gcc_assert (!new_bb);
6482 : : }
6483 : : else
6484 : 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6485 : :
6486 : 0 : msq_init = gimple_assign_lhs (new_stmt);
6487 : : }
6488 : :
6489 : : /* 4. Create realignment token using a target builtin, if available.
6490 : : It is done either inside the containing loop, or before LOOP (as
6491 : : determined above). */
6492 : :
6493 : 0 : if (targetm.vectorize.builtin_mask_for_load)
6494 : : {
6495 : 0 : gcall *new_stmt;
6496 : 0 : tree builtin_decl;
6497 : :
6498 : : /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
6499 : 0 : if (!init_addr)
6500 : : {
6501 : : /* Generate the INIT_ADDR computation outside LOOP. */
6502 : 0 : init_addr = vect_create_addr_base_for_vector_ref (vinfo,
6503 : : stmt_info, &stmts,
6504 : : NULL_TREE);
6505 : 0 : if (loop)
6506 : : {
6507 : 0 : pe = loop_preheader_edge (loop);
6508 : 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6509 : 0 : gcc_assert (!new_bb);
6510 : : }
6511 : : else
6512 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6513 : : }
6514 : :
6515 : 0 : builtin_decl = targetm.vectorize.builtin_mask_for_load ();
6516 : 0 : new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
6517 : 0 : vec_dest =
6518 : 0 : vect_create_destination_var (scalar_dest,
6519 : : gimple_call_return_type (new_stmt));
6520 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6521 : 0 : gimple_call_set_lhs (new_stmt, new_temp);
6522 : :
6523 : 0 : if (compute_in_loop)
6524 : 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6525 : : else
6526 : : {
6527 : : /* Generate the misalignment computation outside LOOP. */
6528 : 0 : pe = loop_preheader_edge (loop);
6529 : 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6530 : 0 : gcc_assert (!new_bb);
6531 : : }
6532 : :
6533 : 0 : *realignment_token = gimple_call_lhs (new_stmt);
6534 : :
6535 : : /* The result of the CALL_EXPR to this builtin is determined from
6536 : : the value of the parameter and no global variables are touched
6537 : : which makes the builtin a "const" function. Requiring the
6538 : : builtin to have the "const" attribute makes it unnecessary
6539 : : to call mark_call_clobbered. */
6540 : 0 : gcc_assert (TREE_READONLY (builtin_decl));
6541 : : }
6542 : :
6543 : 0 : if (alignment_support_scheme == dr_explicit_realign)
6544 : : return msq;
6545 : :
6546 : 0 : gcc_assert (!compute_in_loop);
6547 : 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
6548 : :
6549 : :
6550 : : /* 5. Create msq = phi <msq_init, lsq> in loop */
6551 : :
6552 : 0 : pe = loop_preheader_edge (containing_loop);
6553 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6554 : 0 : msq = make_ssa_name (vec_dest);
6555 : 0 : phi_stmt = create_phi_node (msq, containing_loop->header);
6556 : 0 : add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
6557 : :
6558 : 0 : return msq;
6559 : : }
6560 : :
6561 : :
6562 : : /* Function vect_grouped_load_supported.
6563 : :
6564 : : COUNT is the size of the load group (the number of statements plus the
6565 : : number of gaps). SINGLE_ELEMENT_P is true if there is actually
6566 : : only one statement, with a gap of COUNT - 1.
6567 : :
6568 : : Returns true if a suitable permute exists. */
6569 : :
6570 : : bool
6571 : 1972 : vect_grouped_load_supported (tree vectype, bool single_element_p,
6572 : : unsigned HOST_WIDE_INT count)
6573 : : {
6574 : 1972 : machine_mode mode = TYPE_MODE (vectype);
6575 : :
6576 : : /* If this is single-element interleaving with an element distance
6577 : : that leaves unused vector loads around punt - we at least create
6578 : : very sub-optimal code in that case (and blow up memory,
6579 : : see PR65518). */
6580 : 1972 : if (single_element_p && maybe_gt (count, TYPE_VECTOR_SUBPARTS (vectype)))
6581 : : {
6582 : 41 : if (dump_enabled_p ())
6583 : 13 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6584 : : "single-element interleaving not supported "
6585 : : "for not adjacent vector loads\n");
6586 : 41 : return false;
6587 : : }
6588 : :
6589 : : /* vect_permute_load_chain requires the group size to be equal to 3 or
6590 : : be a power of two. */
6591 : 1931 : if (count != 3 && exact_log2 (count) == -1)
6592 : : {
6593 : 437 : if (dump_enabled_p ())
6594 : 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6595 : : "the size of the group of accesses"
6596 : : " is not a power of 2 or not equal to 3\n");
6597 : 437 : return false;
6598 : : }
6599 : :
6600 : : /* Check that the permutation is supported. */
6601 : 1494 : if (VECTOR_MODE_P (mode))
6602 : : {
6603 : 1494 : unsigned int i, j;
6604 : 1494 : if (count == 3)
6605 : : {
6606 : 716 : unsigned int nelt;
6607 : 1432 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6608 : : {
6609 : : if (dump_enabled_p ())
6610 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6611 : : "cannot handle groups of 3 loads for"
6612 : : " variable-length vectors\n");
6613 : : return false;
6614 : : }
6615 : :
6616 : 716 : vec_perm_builder sel (nelt, nelt, 1);
6617 : 716 : sel.quick_grow (nelt);
6618 : 716 : vec_perm_indices indices;
6619 : 716 : unsigned int k;
6620 : 2816 : for (k = 0; k < 3; k++)
6621 : : {
6622 : 7525 : for (i = 0; i < nelt; i++)
6623 : 5409 : if (3 * i + k < 2 * nelt)
6624 : 3613 : sel[i] = 3 * i + k;
6625 : : else
6626 : 1796 : sel[i] = 0;
6627 : 2116 : indices.new_vector (sel, 2, nelt);
6628 : 2116 : if (!can_vec_perm_const_p (mode, mode, indices))
6629 : : {
6630 : 16 : if (dump_enabled_p ())
6631 : 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6632 : : "shuffle of 3 loads is not supported by"
6633 : : " target\n");
6634 : 16 : return false;
6635 : : }
6636 : 7344 : for (i = 0, j = 0; i < nelt; i++)
6637 : 5244 : if (3 * i + k < 2 * nelt)
6638 : 3496 : sel[i] = i;
6639 : : else
6640 : 1748 : sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
6641 : 2100 : indices.new_vector (sel, 2, nelt);
6642 : 2100 : if (!can_vec_perm_const_p (mode, mode, indices))
6643 : : {
6644 : 0 : if (dump_enabled_p ())
6645 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6646 : : "shuffle of 3 loads is not supported by"
6647 : : " target\n");
6648 : 0 : return false;
6649 : : }
6650 : : }
6651 : : return true;
6652 : 716 : }
6653 : : else
6654 : : {
6655 : : /* If length is not equal to 3 then only power of 2 is supported. */
6656 : 778 : gcc_assert (pow2p_hwi (count));
6657 : 1556 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6658 : :
6659 : : /* The encoding has a single stepped pattern. */
6660 : 778 : vec_perm_builder sel (nelt, 1, 3);
6661 : 778 : sel.quick_grow (3);
6662 : 3890 : for (i = 0; i < 3; i++)
6663 : 2334 : sel[i] = i * 2;
6664 : 778 : vec_perm_indices indices (sel, 2, nelt);
6665 : 778 : if (can_vec_perm_const_p (mode, mode, indices))
6666 : : {
6667 : 3092 : for (i = 0; i < 3; i++)
6668 : 2319 : sel[i] = i * 2 + 1;
6669 : 773 : indices.new_vector (sel, 2, nelt);
6670 : 773 : if (can_vec_perm_const_p (mode, mode, indices))
6671 : 773 : return true;
6672 : : }
6673 : 778 : }
6674 : : }
6675 : :
6676 : 5 : if (dump_enabled_p ())
6677 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6678 : : "extract even/odd not supported by target\n");
6679 : : return false;
6680 : : }
6681 : :
6682 : : /* Return FN if vec_{masked_,mask_len_}load_lanes is available for COUNT vectors
6683 : : of type VECTYPE. MASKED_P says whether the masked form is needed.
6684 : : If it is available and ELSVALS is nonzero store the possible else values
6685 : : in the vector it points to. */
6686 : :
6687 : : internal_fn
6688 : 92668 : vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6689 : : bool masked_p, vec<int> *elsvals)
6690 : : {
6691 : 92668 : if (vect_lanes_optab_supported_p ("vec_mask_len_load_lanes",
6692 : : vec_mask_len_load_lanes_optab, vectype,
6693 : : count, elsvals))
6694 : : return IFN_MASK_LEN_LOAD_LANES;
6695 : 92668 : else if (masked_p)
6696 : : {
6697 : 0 : if (vect_lanes_optab_supported_p ("vec_mask_load_lanes",
6698 : : vec_mask_load_lanes_optab, vectype,
6699 : : count, elsvals))
6700 : : return IFN_MASK_LOAD_LANES;
6701 : : }
6702 : : else
6703 : : {
6704 : 92668 : if (vect_lanes_optab_supported_p ("vec_load_lanes", vec_load_lanes_optab,
6705 : : vectype, count, elsvals))
6706 : : return IFN_LOAD_LANES;
6707 : : }
6708 : : return IFN_LAST;
6709 : : }
6710 : :
6711 : : /* Function vect_permute_load_chain.
6712 : :
6713 : : Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
6714 : : a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
6715 : : the input data correctly. Return the final references for loads in
6716 : : RESULT_CHAIN.
6717 : :
6718 : : E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
6719 : : The input is 4 vectors each containing 8 elements. We assign a number to each
6720 : : element, the input sequence is:
6721 : :
6722 : : 1st vec: 0 1 2 3 4 5 6 7
6723 : : 2nd vec: 8 9 10 11 12 13 14 15
6724 : : 3rd vec: 16 17 18 19 20 21 22 23
6725 : : 4th vec: 24 25 26 27 28 29 30 31
6726 : :
6727 : : The output sequence should be:
6728 : :
6729 : : 1st vec: 0 4 8 12 16 20 24 28
6730 : : 2nd vec: 1 5 9 13 17 21 25 29
6731 : : 3rd vec: 2 6 10 14 18 22 26 30
6732 : : 4th vec: 3 7 11 15 19 23 27 31
6733 : :
6734 : : i.e., the first output vector should contain the first elements of each
6735 : : interleaving group, etc.
6736 : :
6737 : : We use extract_even/odd instructions to create such output. The input of
6738 : : each extract_even/odd operation is two vectors
6739 : : 1st vec 2nd vec
6740 : : 0 1 2 3 4 5 6 7
6741 : :
6742 : : and the output is the vector of extracted even/odd elements. The output of
6743 : : extract_even will be: 0 2 4 6
6744 : : and of extract_odd: 1 3 5 7
6745 : :
6746 : :
6747 : : The permutation is done in log LENGTH stages. In each stage extract_even
6748 : : and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
6749 : : their order. In our example,
6750 : :
6751 : : E1: extract_even (1st vec, 2nd vec)
6752 : : E2: extract_odd (1st vec, 2nd vec)
6753 : : E3: extract_even (3rd vec, 4th vec)
6754 : : E4: extract_odd (3rd vec, 4th vec)
6755 : :
6756 : : The output for the first stage will be:
6757 : :
6758 : : E1: 0 2 4 6 8 10 12 14
6759 : : E2: 1 3 5 7 9 11 13 15
6760 : : E3: 16 18 20 22 24 26 28 30
6761 : : E4: 17 19 21 23 25 27 29 31
6762 : :
6763 : : In order to proceed and create the correct sequence for the next stage (or
6764 : : for the correct output, if the second stage is the last one, as in our
6765 : : example), we first put the output of extract_even operation and then the
6766 : : output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
6767 : : The input for the second stage is:
6768 : :
6769 : : 1st vec (E1): 0 2 4 6 8 10 12 14
6770 : : 2nd vec (E3): 16 18 20 22 24 26 28 30
6771 : : 3rd vec (E2): 1 3 5 7 9 11 13 15
6772 : : 4th vec (E4): 17 19 21 23 25 27 29 31
6773 : :
6774 : : The output of the second stage:
6775 : :
6776 : : E1: 0 4 8 12 16 20 24 28
6777 : : E2: 2 6 10 14 18 22 26 30
6778 : : E3: 1 5 9 13 17 21 25 29
6779 : : E4: 3 7 11 15 19 23 27 31
6780 : :
6781 : : And RESULT_CHAIN after reordering:
6782 : :
6783 : : 1st vec (E1): 0 4 8 12 16 20 24 28
6784 : : 2nd vec (E3): 1 5 9 13 17 21 25 29
6785 : : 3rd vec (E2): 2 6 10 14 18 22 26 30
6786 : : 4th vec (E4): 3 7 11 15 19 23 27 31. */
6787 : :
6788 : : static void
6789 : 0 : vect_permute_load_chain (vec_info *vinfo, vec<tree> dr_chain,
6790 : : unsigned int length,
6791 : : stmt_vec_info stmt_info,
6792 : : gimple_stmt_iterator *gsi,
6793 : : vec<tree> *result_chain)
6794 : : {
6795 : 0 : tree data_ref, first_vect, second_vect;
6796 : 0 : tree perm_mask_even, perm_mask_odd;
6797 : 0 : tree perm3_mask_low, perm3_mask_high;
6798 : 0 : gimple *perm_stmt;
6799 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6800 : 0 : unsigned int i, j, log_length = exact_log2 (length);
6801 : :
6802 : 0 : result_chain->quick_grow (length);
6803 : 0 : memcpy (result_chain->address (), dr_chain.address (),
6804 : : length * sizeof (tree));
6805 : :
6806 : 0 : if (length == 3)
6807 : : {
6808 : : /* vect_grouped_load_supported ensures that this is constant. */
6809 : 0 : unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
6810 : 0 : unsigned int k;
6811 : :
6812 : 0 : vec_perm_builder sel (nelt, nelt, 1);
6813 : 0 : sel.quick_grow (nelt);
6814 : 0 : vec_perm_indices indices;
6815 : 0 : for (k = 0; k < 3; k++)
6816 : : {
6817 : 0 : for (i = 0; i < nelt; i++)
6818 : 0 : if (3 * i + k < 2 * nelt)
6819 : 0 : sel[i] = 3 * i + k;
6820 : : else
6821 : 0 : sel[i] = 0;
6822 : 0 : indices.new_vector (sel, 2, nelt);
6823 : 0 : perm3_mask_low = vect_gen_perm_mask_checked (vectype, indices);
6824 : :
6825 : 0 : for (i = 0, j = 0; i < nelt; i++)
6826 : 0 : if (3 * i + k < 2 * nelt)
6827 : 0 : sel[i] = i;
6828 : : else
6829 : 0 : sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
6830 : 0 : indices.new_vector (sel, 2, nelt);
6831 : 0 : perm3_mask_high = vect_gen_perm_mask_checked (vectype, indices);
6832 : :
6833 : 0 : first_vect = dr_chain[0];
6834 : 0 : second_vect = dr_chain[1];
6835 : :
6836 : : /* Create interleaving stmt (low part of):
6837 : : low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
6838 : : ...}> */
6839 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
6840 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
6841 : : second_vect, perm3_mask_low);
6842 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6843 : :
6844 : : /* Create interleaving stmt (high part of):
6845 : : high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
6846 : : ...}> */
6847 : 0 : first_vect = data_ref;
6848 : 0 : second_vect = dr_chain[2];
6849 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
6850 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
6851 : : second_vect, perm3_mask_high);
6852 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6853 : 0 : (*result_chain)[k] = data_ref;
6854 : : }
6855 : 0 : }
6856 : : else
6857 : : {
6858 : : /* If length is not equal to 3 then only power of 2 is supported. */
6859 : 0 : gcc_assert (pow2p_hwi (length));
6860 : :
6861 : : /* The encoding has a single stepped pattern. */
6862 : 0 : poly_uint64 nelt = TYPE_VECTOR_SUBPARTS (vectype);
6863 : 0 : vec_perm_builder sel (nelt, 1, 3);
6864 : 0 : sel.quick_grow (3);
6865 : 0 : for (i = 0; i < 3; ++i)
6866 : 0 : sel[i] = i * 2;
6867 : 0 : vec_perm_indices indices (sel, 2, nelt);
6868 : 0 : perm_mask_even = vect_gen_perm_mask_checked (vectype, indices);
6869 : :
6870 : 0 : for (i = 0; i < 3; ++i)
6871 : 0 : sel[i] = i * 2 + 1;
6872 : 0 : indices.new_vector (sel, 2, nelt);
6873 : 0 : perm_mask_odd = vect_gen_perm_mask_checked (vectype, indices);
6874 : :
6875 : 0 : for (i = 0; i < log_length; i++)
6876 : : {
6877 : 0 : for (j = 0; j < length; j += 2)
6878 : : {
6879 : 0 : first_vect = dr_chain[j];
6880 : 0 : second_vect = dr_chain[j+1];
6881 : :
6882 : : /* data_ref = permute_even (first_data_ref, second_data_ref); */
6883 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
6884 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
6885 : : first_vect, second_vect,
6886 : : perm_mask_even);
6887 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6888 : 0 : (*result_chain)[j/2] = data_ref;
6889 : :
6890 : : /* data_ref = permute_odd (first_data_ref, second_data_ref); */
6891 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
6892 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
6893 : : first_vect, second_vect,
6894 : : perm_mask_odd);
6895 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
6896 : 0 : (*result_chain)[j/2+length/2] = data_ref;
6897 : : }
6898 : 0 : memcpy (dr_chain.address (), result_chain->address (),
6899 : : length * sizeof (tree));
6900 : : }
6901 : 0 : }
6902 : 0 : }
6903 : :
6904 : : /* Function vect_shift_permute_load_chain.
6905 : :
6906 : : Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
6907 : : sequence of stmts to reorder the input data accordingly.
6908 : : Return the final references for loads in RESULT_CHAIN.
6909 : : Return true if successed, false otherwise.
6910 : :
6911 : : E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
6912 : : The input is 3 vectors each containing 8 elements. We assign a
6913 : : number to each element, the input sequence is:
6914 : :
6915 : : 1st vec: 0 1 2 3 4 5 6 7
6916 : : 2nd vec: 8 9 10 11 12 13 14 15
6917 : : 3rd vec: 16 17 18 19 20 21 22 23
6918 : :
6919 : : The output sequence should be:
6920 : :
6921 : : 1st vec: 0 3 6 9 12 15 18 21
6922 : : 2nd vec: 1 4 7 10 13 16 19 22
6923 : : 3rd vec: 2 5 8 11 14 17 20 23
6924 : :
6925 : : We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
6926 : :
6927 : : First we shuffle all 3 vectors to get correct elements order:
6928 : :
6929 : : 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
6930 : : 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
6931 : : 3rd vec: (16 19 22) (17 20 23) (18 21)
6932 : :
6933 : : Next we unite and shift vector 3 times:
6934 : :
6935 : : 1st step:
6936 : : shift right by 6 the concatenation of:
6937 : : "1st vec" and "2nd vec"
6938 : : ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
6939 : : "2nd vec" and "3rd vec"
6940 : : ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
6941 : : "3rd vec" and "1st vec"
6942 : : (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
6943 : : | New vectors |
6944 : :
6945 : : So that now new vectors are:
6946 : :
6947 : : 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
6948 : : 2nd vec: (10 13) (16 19 22) (17 20 23)
6949 : : 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
6950 : :
6951 : : 2nd step:
6952 : : shift right by 5 the concatenation of:
6953 : : "1st vec" and "3rd vec"
6954 : : ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
6955 : : "2nd vec" and "1st vec"
6956 : : (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
6957 : : "3rd vec" and "2nd vec"
6958 : : (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
6959 : : | New vectors |
6960 : :
6961 : : So that now new vectors are:
6962 : :
6963 : : 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
6964 : : 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
6965 : : 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
6966 : :
6967 : : 3rd step:
6968 : : shift right by 5 the concatenation of:
6969 : : "1st vec" and "1st vec"
6970 : : ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
6971 : : shift right by 3 the concatenation of:
6972 : : "2nd vec" and "2nd vec"
6973 : : (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
6974 : : | New vectors |
6975 : :
6976 : : So that now all vectors are READY:
6977 : : 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
6978 : : 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
6979 : : 3rd vec: ( 1 4 7) (10 13) (16 19 22)
6980 : :
6981 : : This algorithm is faster than one in vect_permute_load_chain if:
6982 : : 1. "shift of a concatination" is faster than general permutation.
6983 : : This is usually so.
6984 : : 2. The TARGET machine can't execute vector instructions in parallel.
6985 : : This is because each step of the algorithm depends on previous.
6986 : : The algorithm in vect_permute_load_chain is much more parallel.
6987 : :
6988 : : The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
6989 : : */
6990 : :
6991 : : static bool
6992 : 0 : vect_shift_permute_load_chain (vec_info *vinfo, vec<tree> dr_chain,
6993 : : unsigned int length,
6994 : : stmt_vec_info stmt_info,
6995 : : gimple_stmt_iterator *gsi,
6996 : : vec<tree> *result_chain)
6997 : : {
6998 : 0 : tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
6999 : 0 : tree perm2_mask1, perm2_mask2, perm3_mask;
7000 : 0 : tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
7001 : 0 : gimple *perm_stmt;
7002 : :
7003 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7004 : 0 : machine_mode vmode = TYPE_MODE (vectype);
7005 : 0 : unsigned int i;
7006 : 0 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7007 : :
7008 : 0 : unsigned HOST_WIDE_INT nelt, vf;
7009 : 0 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nelt)
7010 : 0 : || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&vf))
7011 : : /* Not supported for variable-length vectors. */
7012 : : return false;
7013 : :
7014 : 0 : vec_perm_builder sel (nelt, nelt, 1);
7015 : 0 : sel.quick_grow (nelt);
7016 : :
7017 : 0 : result_chain->quick_grow (length);
7018 : 0 : memcpy (result_chain->address (), dr_chain.address (),
7019 : 0 : length * sizeof (tree));
7020 : :
7021 : 0 : if (pow2p_hwi (length) && vf > 4)
7022 : : {
7023 : 0 : unsigned int j, log_length = exact_log2 (length);
7024 : 0 : for (i = 0; i < nelt / 2; ++i)
7025 : 0 : sel[i] = i * 2;
7026 : 0 : for (i = 0; i < nelt / 2; ++i)
7027 : 0 : sel[nelt / 2 + i] = i * 2 + 1;
7028 : 0 : vec_perm_indices indices (sel, 2, nelt);
7029 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7030 : : {
7031 : 0 : if (dump_enabled_p ())
7032 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7033 : : "shuffle of 2 fields structure is not \
7034 : : supported by target\n");
7035 : 0 : return false;
7036 : : }
7037 : 0 : perm2_mask1 = vect_gen_perm_mask_checked (vectype, indices);
7038 : :
7039 : 0 : for (i = 0; i < nelt / 2; ++i)
7040 : 0 : sel[i] = i * 2 + 1;
7041 : 0 : for (i = 0; i < nelt / 2; ++i)
7042 : 0 : sel[nelt / 2 + i] = i * 2;
7043 : 0 : indices.new_vector (sel, 2, nelt);
7044 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7045 : : {
7046 : 0 : if (dump_enabled_p ())
7047 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7048 : : "shuffle of 2 fields structure is not \
7049 : : supported by target\n");
7050 : 0 : return false;
7051 : : }
7052 : 0 : perm2_mask2 = vect_gen_perm_mask_checked (vectype, indices);
7053 : :
7054 : : /* Generating permutation constant to shift all elements.
7055 : : For vector length 8 it is {4 5 6 7 8 9 10 11}. */
7056 : 0 : for (i = 0; i < nelt; i++)
7057 : 0 : sel[i] = nelt / 2 + i;
7058 : 0 : indices.new_vector (sel, 2, nelt);
7059 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7060 : : {
7061 : 0 : if (dump_enabled_p ())
7062 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7063 : : "shift permutation is not supported by target\n");
7064 : 0 : return false;
7065 : : }
7066 : 0 : shift1_mask = vect_gen_perm_mask_checked (vectype, indices);
7067 : :
7068 : : /* Generating permutation constant to select vector from 2.
7069 : : For vector length 8 it is {0 1 2 3 12 13 14 15}. */
7070 : 0 : for (i = 0; i < nelt / 2; i++)
7071 : 0 : sel[i] = i;
7072 : 0 : for (i = nelt / 2; i < nelt; i++)
7073 : 0 : sel[i] = nelt + i;
7074 : 0 : indices.new_vector (sel, 2, nelt);
7075 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7076 : : {
7077 : 0 : if (dump_enabled_p ())
7078 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7079 : : "select is not supported by target\n");
7080 : 0 : return false;
7081 : : }
7082 : 0 : select_mask = vect_gen_perm_mask_checked (vectype, indices);
7083 : :
7084 : 0 : for (i = 0; i < log_length; i++)
7085 : : {
7086 : 0 : for (j = 0; j < length; j += 2)
7087 : : {
7088 : 0 : first_vect = dr_chain[j];
7089 : 0 : second_vect = dr_chain[j + 1];
7090 : :
7091 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
7092 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7093 : : first_vect, first_vect,
7094 : : perm2_mask1);
7095 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7096 : 0 : vect[0] = data_ref;
7097 : :
7098 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
7099 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7100 : : second_vect, second_vect,
7101 : : perm2_mask2);
7102 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7103 : 0 : vect[1] = data_ref;
7104 : :
7105 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
7106 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7107 : : vect[0], vect[1], shift1_mask);
7108 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7109 : 0 : (*result_chain)[j/2 + length/2] = data_ref;
7110 : :
7111 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
7112 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7113 : : vect[0], vect[1], select_mask);
7114 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7115 : 0 : (*result_chain)[j/2] = data_ref;
7116 : : }
7117 : 0 : memcpy (dr_chain.address (), result_chain->address (),
7118 : : length * sizeof (tree));
7119 : : }
7120 : : return true;
7121 : 0 : }
7122 : 0 : if (length == 3 && vf > 2)
7123 : : {
7124 : : unsigned int k = 0, l = 0;
7125 : :
7126 : : /* Generating permutation constant to get all elements in rigth order.
7127 : : For vector length 8 it is {0 3 6 1 4 7 2 5}. */
7128 : 0 : for (i = 0; i < nelt; i++)
7129 : : {
7130 : 0 : if (3 * k + (l % 3) >= nelt)
7131 : : {
7132 : 0 : k = 0;
7133 : 0 : l += (3 - (nelt % 3));
7134 : : }
7135 : 0 : sel[i] = 3 * k + (l % 3);
7136 : 0 : k++;
7137 : : }
7138 : 0 : vec_perm_indices indices (sel, 2, nelt);
7139 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7140 : : {
7141 : 0 : if (dump_enabled_p ())
7142 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7143 : : "shuffle of 3 fields structure is not \
7144 : : supported by target\n");
7145 : 0 : return false;
7146 : : }
7147 : 0 : perm3_mask = vect_gen_perm_mask_checked (vectype, indices);
7148 : :
7149 : : /* Generating permutation constant to shift all elements.
7150 : : For vector length 8 it is {6 7 8 9 10 11 12 13}. */
7151 : 0 : for (i = 0; i < nelt; i++)
7152 : 0 : sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
7153 : 0 : indices.new_vector (sel, 2, nelt);
7154 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7155 : : {
7156 : 0 : if (dump_enabled_p ())
7157 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7158 : : "shift permutation is not supported by target\n");
7159 : 0 : return false;
7160 : : }
7161 : 0 : shift1_mask = vect_gen_perm_mask_checked (vectype, indices);
7162 : :
7163 : : /* Generating permutation constant to shift all elements.
7164 : : For vector length 8 it is {5 6 7 8 9 10 11 12}. */
7165 : 0 : for (i = 0; i < nelt; i++)
7166 : 0 : sel[i] = 2 * (nelt / 3) + 1 + i;
7167 : 0 : indices.new_vector (sel, 2, nelt);
7168 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7169 : : {
7170 : 0 : if (dump_enabled_p ())
7171 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7172 : : "shift permutation is not supported by target\n");
7173 : 0 : return false;
7174 : : }
7175 : 0 : shift2_mask = vect_gen_perm_mask_checked (vectype, indices);
7176 : :
7177 : : /* Generating permutation constant to shift all elements.
7178 : : For vector length 8 it is {3 4 5 6 7 8 9 10}. */
7179 : 0 : for (i = 0; i < nelt; i++)
7180 : 0 : sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
7181 : 0 : indices.new_vector (sel, 2, nelt);
7182 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7183 : : {
7184 : 0 : if (dump_enabled_p ())
7185 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7186 : : "shift permutation is not supported by target\n");
7187 : 0 : return false;
7188 : : }
7189 : 0 : shift3_mask = vect_gen_perm_mask_checked (vectype, indices);
7190 : :
7191 : : /* Generating permutation constant to shift all elements.
7192 : : For vector length 8 it is {5 6 7 8 9 10 11 12}. */
7193 : 0 : for (i = 0; i < nelt; i++)
7194 : 0 : sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
7195 : 0 : indices.new_vector (sel, 2, nelt);
7196 : 0 : if (!can_vec_perm_const_p (vmode, vmode, indices))
7197 : : {
7198 : 0 : if (dump_enabled_p ())
7199 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7200 : : "shift permutation is not supported by target\n");
7201 : 0 : return false;
7202 : : }
7203 : 0 : shift4_mask = vect_gen_perm_mask_checked (vectype, indices);
7204 : :
7205 : 0 : for (k = 0; k < 3; k++)
7206 : : {
7207 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
7208 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7209 : 0 : dr_chain[k], dr_chain[k],
7210 : : perm3_mask);
7211 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7212 : 0 : vect[k] = data_ref;
7213 : : }
7214 : :
7215 : 0 : for (k = 0; k < 3; k++)
7216 : : {
7217 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
7218 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7219 : 0 : vect[k % 3], vect[(k + 1) % 3],
7220 : : shift1_mask);
7221 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7222 : 0 : vect_shift[k] = data_ref;
7223 : : }
7224 : :
7225 : 0 : for (k = 0; k < 3; k++)
7226 : : {
7227 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
7228 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
7229 : 0 : vect_shift[(4 - k) % 3],
7230 : 0 : vect_shift[(3 - k) % 3],
7231 : : shift2_mask);
7232 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7233 : 0 : vect[k] = data_ref;
7234 : : }
7235 : :
7236 : 0 : (*result_chain)[3 - (nelt % 3)] = vect[2];
7237 : :
7238 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
7239 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
7240 : : vect[0], shift3_mask);
7241 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7242 : 0 : (*result_chain)[nelt % 3] = data_ref;
7243 : :
7244 : 0 : data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
7245 : 0 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
7246 : : vect[1], shift4_mask);
7247 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
7248 : 0 : (*result_chain)[0] = data_ref;
7249 : 0 : return true;
7250 : 0 : }
7251 : : return false;
7252 : 0 : }
7253 : :
7254 : : /* Function vect_transform_grouped_load.
7255 : :
7256 : : Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
7257 : : to perform their permutation and ascribe the result vectorized statements to
7258 : : the scalar statements.
7259 : : */
7260 : :
7261 : : void
7262 : 0 : vect_transform_grouped_load (vec_info *vinfo, stmt_vec_info stmt_info,
7263 : : vec<tree> dr_chain,
7264 : : int size, gimple_stmt_iterator *gsi)
7265 : : {
7266 : 0 : machine_mode mode;
7267 : 0 : vec<tree> result_chain = vNULL;
7268 : :
7269 : : /* DR_CHAIN contains input data-refs that are a part of the interleaving.
7270 : : RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
7271 : : vectors, that are ready for vector computation. */
7272 : 0 : result_chain.create (size);
7273 : :
7274 : : /* If reassociation width for vector type is 2 or greater target machine can
7275 : : execute 2 or more vector instructions in parallel. Otherwise try to
7276 : : get chain for loads group using vect_shift_permute_load_chain. */
7277 : 0 : mode = TYPE_MODE (STMT_VINFO_VECTYPE (stmt_info));
7278 : 0 : if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
7279 : 0 : || pow2p_hwi (size)
7280 : 0 : || !vect_shift_permute_load_chain (vinfo, dr_chain, size, stmt_info,
7281 : : gsi, &result_chain))
7282 : 0 : vect_permute_load_chain (vinfo, dr_chain,
7283 : : size, stmt_info, gsi, &result_chain);
7284 : 0 : vect_record_grouped_load_vectors (vinfo, stmt_info, result_chain);
7285 : 0 : result_chain.release ();
7286 : 0 : }
7287 : :
7288 : : /* RESULT_CHAIN contains the output of a group of grouped loads that were
7289 : : generated as part of the vectorization of STMT_INFO. Assign the statement
7290 : : for each vector to the associated scalar statement. */
7291 : :
7292 : : void
7293 : 0 : vect_record_grouped_load_vectors (vec_info *, stmt_vec_info stmt_info,
7294 : : vec<tree> result_chain)
7295 : : {
7296 : 0 : stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
7297 : 0 : unsigned int i, gap_count;
7298 : 0 : tree tmp_data_ref;
7299 : :
7300 : : /* Put a permuted data-ref in the VECTORIZED_STMT field.
7301 : : Since we scan the chain starting from it's first node, their order
7302 : : corresponds the order of data-refs in RESULT_CHAIN. */
7303 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
7304 : 0 : gap_count = 1;
7305 : 0 : FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
7306 : : {
7307 : 0 : if (!next_stmt_info)
7308 : : break;
7309 : :
7310 : : /* Skip the gaps. Loads created for the gaps will be removed by dead
7311 : : code elimination pass later. No need to check for the first stmt in
7312 : : the group, since it always exists.
7313 : : DR_GROUP_GAP is the number of steps in elements from the previous
7314 : : access (if there is no gap DR_GROUP_GAP is 1). We skip loads that
7315 : : correspond to the gaps. */
7316 : 0 : if (next_stmt_info != first_stmt_info
7317 : 0 : && gap_count < DR_GROUP_GAP (next_stmt_info))
7318 : : {
7319 : 0 : gap_count++;
7320 : 0 : continue;
7321 : : }
7322 : :
7323 : : /* ??? The following needs cleanup after the removal of
7324 : : DR_GROUP_SAME_DR_STMT. */
7325 : 0 : if (next_stmt_info)
7326 : : {
7327 : 0 : gimple *new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
7328 : : /* We assume that if VEC_STMT is not NULL, this is a case of multiple
7329 : : copies, and we put the new vector statement last. */
7330 : 0 : STMT_VINFO_VEC_STMTS (next_stmt_info).safe_push (new_stmt);
7331 : :
7332 : 0 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7333 : 0 : gap_count = 1;
7334 : : }
7335 : : }
7336 : 0 : }
7337 : :
7338 : : /* Function vect_force_dr_alignment_p.
7339 : :
7340 : : Returns whether the alignment of a DECL can be forced to be aligned
7341 : : on ALIGNMENT bit boundary. */
7342 : :
7343 : : bool
7344 : 630427 : vect_can_force_dr_alignment_p (const_tree decl, poly_uint64 alignment)
7345 : : {
7346 : 630427 : if (!VAR_P (decl))
7347 : : return false;
7348 : :
7349 : 209823 : if (decl_in_symtab_p (decl)
7350 : 209823 : && (!symtab_node::get (decl)
7351 : 19963 : || !symtab_node::get (decl)->can_increase_alignment_p ()))
7352 : 11895 : return false;
7353 : :
7354 : 197928 : if (TREE_STATIC (decl))
7355 : 8068 : return (known_le (alignment,
7356 : 8068 : (unsigned HOST_WIDE_INT) MAX_OFILE_ALIGNMENT));
7357 : : else
7358 : 189860 : return (known_le (alignment, (unsigned HOST_WIDE_INT) MAX_STACK_ALIGNMENT));
7359 : : }
7360 : :
7361 : : /* Return whether the data reference DR_INFO is supported with respect to its
7362 : : alignment.
7363 : : If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
7364 : : it is aligned, i.e., check if it is possible to vectorize it with different
7365 : : alignment. */
7366 : :
7367 : : enum dr_alignment_support
7368 : 3071758 : vect_supportable_dr_alignment (vec_info *vinfo, dr_vec_info *dr_info,
7369 : : tree vectype, int misalignment)
7370 : : {
7371 : 3071758 : data_reference *dr = dr_info->dr;
7372 : 3071758 : stmt_vec_info stmt_info = dr_info->stmt;
7373 : 3071758 : machine_mode mode = TYPE_MODE (vectype);
7374 : 3071758 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7375 : 3071758 : class loop *vect_loop = NULL;
7376 : 3071758 : bool nested_in_vect_loop = false;
7377 : :
7378 : 3071758 : if (misalignment == 0)
7379 : : return dr_aligned;
7380 : 1699292 : else if (dr_safe_speculative_read_required (stmt_info))
7381 : : return dr_unaligned_unsupported;
7382 : :
7383 : : /* For now assume all conditional loads/stores support unaligned
7384 : : access without any special code. */
7385 : 1352290 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
7386 : 7035 : if (gimple_call_internal_p (stmt)
7387 : 7035 : && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
7388 : 2462 : || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
7389 : : return dr_unaligned_supported;
7390 : :
7391 : 1345255 : if (loop_vinfo)
7392 : : {
7393 : 647832 : vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
7394 : 647832 : nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt_info);
7395 : : }
7396 : :
7397 : : /* Possibly unaligned access. */
7398 : :
7399 : : /* We can choose between using the implicit realignment scheme (generating
7400 : : a misaligned_move stmt) and the explicit realignment scheme (generating
7401 : : aligned loads with a REALIGN_LOAD). There are two variants to the
7402 : : explicit realignment scheme: optimized, and unoptimized.
7403 : : We can optimize the realignment only if the step between consecutive
7404 : : vector loads is equal to the vector size. Since the vector memory
7405 : : accesses advance in steps of VS (Vector Size) in the vectorized loop, it
7406 : : is guaranteed that the misalignment amount remains the same throughout the
7407 : : execution of the vectorized loop. Therefore, we can create the
7408 : : "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
7409 : : at the loop preheader.
7410 : :
7411 : : However, in the case of outer-loop vectorization, when vectorizing a
7412 : : memory access in the inner-loop nested within the LOOP that is now being
7413 : : vectorized, while it is guaranteed that the misalignment of the
7414 : : vectorized memory access will remain the same in different outer-loop
7415 : : iterations, it is *not* guaranteed that is will remain the same throughout
7416 : : the execution of the inner-loop. This is because the inner-loop advances
7417 : : with the original scalar step (and not in steps of VS). If the inner-loop
7418 : : step happens to be a multiple of VS, then the misalignment remains fixed
7419 : : and we can use the optimized realignment scheme. For example:
7420 : :
7421 : : for (i=0; i<N; i++)
7422 : : for (j=0; j<M; j++)
7423 : : s += a[i+j];
7424 : :
7425 : : When vectorizing the i-loop in the above example, the step between
7426 : : consecutive vector loads is 1, and so the misalignment does not remain
7427 : : fixed across the execution of the inner-loop, and the realignment cannot
7428 : : be optimized (as illustrated in the following pseudo vectorized loop):
7429 : :
7430 : : for (i=0; i<N; i+=4)
7431 : : for (j=0; j<M; j++){
7432 : : vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
7433 : : // when j is {0,1,2,3,4,5,6,7,...} respectively.
7434 : : // (assuming that we start from an aligned address).
7435 : : }
7436 : :
7437 : : We therefore have to use the unoptimized realignment scheme:
7438 : :
7439 : : for (i=0; i<N; i+=4)
7440 : : for (j=k; j<M; j+=4)
7441 : : vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
7442 : : // that the misalignment of the initial address is
7443 : : // 0).
7444 : :
7445 : : The loop can then be vectorized as follows:
7446 : :
7447 : : for (k=0; k<4; k++){
7448 : : rt = get_realignment_token (&vp[k]);
7449 : : for (i=0; i<N; i+=4){
7450 : : v1 = vp[i+k];
7451 : : for (j=k; j<M; j+=4){
7452 : : v2 = vp[i+j+VS-1];
7453 : : va = REALIGN_LOAD <v1,v2,rt>;
7454 : : vs += va;
7455 : : v1 = v2;
7456 : : }
7457 : : }
7458 : : } */
7459 : :
7460 : 1345255 : if (DR_IS_READ (dr))
7461 : : {
7462 : 503023 : if (can_implement_p (vec_realign_load_optab, mode)
7463 : 503023 : && (!targetm.vectorize.builtin_mask_for_load
7464 : 0 : || targetm.vectorize.builtin_mask_for_load ()))
7465 : : {
7466 : : /* If we are doing SLP then the accesses need not have the
7467 : : same alignment, instead it depends on the SLP group size. */
7468 : 0 : if (loop_vinfo
7469 : 0 : && STMT_SLP_TYPE (stmt_info)
7470 : 0 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
7471 : 0 : && !multiple_p (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
7472 : 0 : * (DR_GROUP_SIZE
7473 : 0 : (DR_GROUP_FIRST_ELEMENT (stmt_info))),
7474 : 0 : TYPE_VECTOR_SUBPARTS (vectype)))
7475 : : ;
7476 : 0 : else if (!loop_vinfo
7477 : 0 : || (nested_in_vect_loop
7478 : 0 : && maybe_ne (TREE_INT_CST_LOW (DR_STEP (dr)),
7479 : 0 : GET_MODE_SIZE (TYPE_MODE (vectype)))))
7480 : 0 : return dr_explicit_realign;
7481 : : else
7482 : 0 : return dr_explicit_realign_optimized;
7483 : : }
7484 : : }
7485 : :
7486 : 1345255 : bool is_packed = false;
7487 : 1345255 : tree type = TREE_TYPE (DR_REF (dr));
7488 : 1345255 : if (misalignment == DR_MISALIGNMENT_UNKNOWN)
7489 : 961198 : is_packed = not_size_aligned (DR_REF (dr));
7490 : 1345255 : if (targetm.vectorize.support_vector_misalignment (mode, type, misalignment,
7491 : : is_packed))
7492 : : return dr_unaligned_supported;
7493 : :
7494 : : /* Unsupported. */
7495 : : return dr_unaligned_unsupported;
7496 : : }
|