Line data Source code
1 : /* Data References Analysis and Manipulation Utilities for Vectorization.
2 : Copyright (C) 2003-2026 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 343688 : 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 343688 : machine_mode mode, array_mode;
73 343688 : bool limit_p;
74 :
75 343688 : mode = TYPE_MODE (vectype);
76 343688 : if (!targetm.array_mode (mode, count).exists (&array_mode))
77 : {
78 687376 : poly_uint64 bits = count * GET_MODE_BITSIZE (mode);
79 343688 : limit_p = !targetm.array_mode_supported_p (mode, count);
80 343688 : if (!int_mode_for_size (bits, limit_p).exists (&array_mode))
81 : {
82 304400 : if (dump_enabled_p ())
83 12814 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
84 : "no array mode for %s[%wu]\n",
85 12814 : GET_MODE_NAME (mode), count);
86 304400 : return false;
87 : }
88 : }
89 :
90 39288 : enum insn_code icode;
91 39288 : if ((icode = convert_optab_handler (optab, array_mode, mode))
92 : == CODE_FOR_nothing)
93 : {
94 39288 : if (dump_enabled_p ())
95 4104 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
96 : "cannot use %s<%s><%s>\n", name,
97 4104 : GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
98 39288 : 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 561894 : simd_clone_call_p (gimple *stmt)
120 : {
121 630467 : gcall *call = dyn_cast <gcall *> (stmt);
122 70164 : if (!call)
123 : return NULL;
124 :
125 70164 : tree fndecl = NULL_TREE;
126 70164 : if (gimple_call_internal_p (call, IFN_MASK_CALL))
127 226 : fndecl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
128 : else
129 69938 : fndecl = gimple_call_fndecl (stmt);
130 :
131 70164 : if (fndecl == NULL_TREE)
132 : return NULL;
133 :
134 35732 : cgraph_node *node = cgraph_node::get (fndecl);
135 35732 : 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 4576843 : vect_get_smallest_scalar_type (stmt_vec_info stmt_info, tree scalar_type)
162 : {
163 4576843 : 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 4576843 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (scalar_type)))
168 : return scalar_type;
169 :
170 4576843 : lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
171 :
172 4576843 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
173 4576843 : if (assign)
174 : {
175 4014949 : scalar_type = TREE_TYPE (gimple_assign_lhs (assign));
176 4014949 : if (gimple_assign_cast_p (assign)
177 3652516 : || gimple_assign_rhs_code (assign) == DOT_PROD_EXPR
178 3652122 : || gimple_assign_rhs_code (assign) == WIDEN_SUM_EXPR
179 3652122 : || gimple_assign_rhs_code (assign) == SAD_EXPR
180 3652039 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_EXPR
181 3648280 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_PLUS_EXPR
182 3648280 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_MINUS_EXPR
183 3648280 : || gimple_assign_rhs_code (assign) == WIDEN_LSHIFT_EXPR
184 7663229 : || gimple_assign_rhs_code (assign) == FLOAT_EXPR)
185 : {
186 376206 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (assign));
187 :
188 376206 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
189 376206 : if (rhs < lhs)
190 4576843 : scalar_type = rhs_type;
191 : }
192 : }
193 561894 : else if (cgraph_node *node = simd_clone_call_p (stmt_info->stmt))
194 : {
195 1591 : auto clone = node->simd_clones->simdclone;
196 4871 : for (unsigned int i = 0; i < clone->nargs; ++i)
197 : {
198 3280 : if (clone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
199 : {
200 1876 : tree arg_scalar_type = TREE_TYPE (clone->args[i].vector_type);
201 1876 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (arg_scalar_type));
202 1876 : if (rhs < lhs)
203 : {
204 3280 : scalar_type = arg_scalar_type;
205 3280 : lhs = rhs;
206 : }
207 : }
208 : }
209 : }
210 560303 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
211 : {
212 68573 : unsigned int i = 0;
213 68573 : if (gimple_call_internal_p (call))
214 : {
215 32058 : internal_fn ifn = gimple_call_internal_fn (call);
216 32058 : if (internal_load_fn_p (ifn))
217 : /* For loads the LHS type does the trick. */
218 : i = ~0U;
219 28426 : else if (internal_store_fn_p (ifn))
220 : {
221 : /* For stores use the tyep of the stored value. */
222 1523 : i = internal_fn_stored_value_index (ifn);
223 1523 : scalar_type = TREE_TYPE (gimple_call_arg (call, i));
224 1523 : i = ~0U;
225 : }
226 26903 : else if (internal_fn_mask_index (ifn) == 0)
227 5406 : i = 1;
228 : }
229 68573 : if (i < gimple_call_num_args (call))
230 : {
231 58841 : tree rhs_type = TREE_TYPE (gimple_call_arg (call, i));
232 58841 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (rhs_type)))
233 : {
234 58841 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
235 58841 : if (rhs < lhs)
236 4576843 : 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 147686 : vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
251 : {
252 147686 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
253 :
254 147686 : 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 147632 : opt_result res
261 147632 : = runtime_alias_check_p (ddr, loop,
262 147632 : optimize_loop_nest_for_speed_p (loop));
263 147632 : if (!res)
264 143 : return res;
265 :
266 147489 : LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
267 147489 : return opt_result::success ();
268 : }
269 :
270 : /* Record that loop LOOP_VINFO needs to check that VALUE is nonzero. */
271 :
272 : static void
273 1348 : vect_check_nonzero_value (loop_vec_info loop_vinfo, tree value)
274 : {
275 1348 : const vec<tree> &checks = LOOP_VINFO_CHECK_NONZERO (loop_vinfo);
276 2071 : for (unsigned int i = 0; i < checks.length(); ++i)
277 723 : if (checks[i] == value)
278 : return;
279 :
280 1348 : if (dump_enabled_p ())
281 432 : dump_printf_loc (MSG_NOTE, vect_location,
282 : "need run-time check that %T is nonzero\n",
283 : value);
284 1348 : 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 108185 : vect_preserves_scalar_order_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b)
293 : {
294 108185 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
295 108185 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
296 :
297 : /* Single statements are always kept in their original order. */
298 108185 : if (!STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
299 175594 : && !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 21220 : if ((DR_IS_READ (dr_info_a->dr) && integer_zerop (DR_STEP (dr_info_a->dr)))
305 66352 : || (DR_IS_READ (dr_info_b->dr) && integer_zerop (DR_STEP (dr_info_b->dr))))
306 1244 : 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 44763 : stmt_vec_info il_a = DR_GROUP_FIRST_ELEMENT (stmtinfo_a);
314 44763 : if (il_a)
315 : {
316 40407 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_a)))
317 161562 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
318 140867 : s = DR_GROUP_NEXT_ELEMENT (s))
319 140867 : il_a = get_later_stmt (il_a, s);
320 : else /* DR_IS_READ */
321 78051 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
322 58339 : s = DR_GROUP_NEXT_ELEMENT (s))
323 58339 : if (get_later_stmt (il_a, s) == il_a)
324 1550 : il_a = s;
325 : }
326 : else
327 : il_a = stmtinfo_a;
328 44763 : stmt_vec_info il_b = DR_GROUP_FIRST_ELEMENT (stmtinfo_b);
329 44763 : if (il_b)
330 : {
331 39204 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_b)))
332 211515 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
333 180251 : s = DR_GROUP_NEXT_ELEMENT (s))
334 180251 : il_b = get_later_stmt (il_b, s);
335 : else /* DR_IS_READ */
336 37374 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
337 29434 : s = DR_GROUP_NEXT_ELEMENT (s))
338 29434 : if (get_later_stmt (il_b, s) == il_b)
339 153 : il_b = s;
340 : }
341 : else
342 : il_b = stmtinfo_b;
343 44763 : bool a_after_b = (get_later_stmt (stmtinfo_a, stmtinfo_b) == stmtinfo_a);
344 44763 : 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 7974 : 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 7974 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
364 31914 : for (lambda_vector &dist_v : DDR_DIST_VECTS (ddr))
365 : {
366 15800 : int dist = dist_v[loop_depth];
367 15800 : 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 7840 : 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 7808 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
393 7808 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
394 7808 : return (!STMT_VINFO_GATHER_SCATTER_P (dr_info_a->stmt)
395 7808 : && !STMT_VINFO_GATHER_SCATTER_P (dr_info_b->stmt)
396 15624 : && 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 981502 : vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
414 : loop_vec_info loop_vinfo,
415 : unsigned int *max_vf)
416 : {
417 981502 : unsigned int i;
418 981502 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
419 981502 : struct data_reference *dra = DDR_A (ddr);
420 981502 : struct data_reference *drb = DDR_B (ddr);
421 981502 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (dra);
422 981502 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (drb);
423 981502 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
424 981502 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
425 981502 : lambda_vector dist_v;
426 981502 : unsigned int loop_depth;
427 :
428 : /* If user asserted safelen consecutive iterations can be
429 : executed concurrently, assume independence. */
430 1142243 : auto apply_safelen = [&]()
431 : {
432 160741 : if (loop->safelen >= 2)
433 : {
434 7444 : if ((unsigned int) loop->safelen < *max_vf)
435 1896 : *max_vf = loop->safelen;
436 7444 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
437 7444 : return true;
438 : }
439 : return false;
440 981502 : };
441 :
442 : /* In loop analysis all data references should be vectorizable. */
443 981502 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
444 981502 : || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
445 0 : gcc_unreachable ();
446 :
447 : /* Independent data accesses. */
448 981502 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
449 757918 : return opt_result::success ();
450 :
451 223584 : if (dra == drb
452 223584 : || (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 223584 : if (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
459 103338 : && (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
460 103338 : == DR_GROUP_FIRST_ELEMENT (stmtinfo_b))
461 239853 : && !STMT_VINFO_STRIDED_P (stmtinfo_a))
462 2301 : 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 100367 : if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
471 120916 : || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
472 336515 : && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
473 : get_alias_set (DR_REF (drb))))
474 5533 : return opt_result::success ();
475 :
476 215750 : if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
477 203596 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
478 : {
479 14815 : if (apply_safelen ())
480 1396 : return opt_result::success ();
481 :
482 13419 : return opt_result::failure_at
483 13419 : (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 200935 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
490 : {
491 145391 : if (apply_safelen ())
492 6048 : return opt_result::success ();
493 :
494 139343 : if (dump_enabled_p ())
495 7447 : 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 139343 : return vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
502 : }
503 :
504 : /* Known data dependence. */
505 55544 : if (DDR_NUM_DIST_VECTS (ddr) == 0)
506 : {
507 535 : if (apply_safelen ())
508 0 : return opt_result::success ();
509 :
510 535 : if (dump_enabled_p ())
511 132 : 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 535 : return vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
517 : }
518 :
519 55009 : loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
520 :
521 55009 : if (DDR_COULD_BE_INDEPENDENT_P (ddr)
522 55009 : && vect_analyze_possibly_independent_ddr (ddr, loop_vinfo,
523 : loop_depth, max_vf))
524 7966 : return opt_result::success ();
525 :
526 87745 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
527 : {
528 47063 : int dist = dist_v[loop_depth];
529 :
530 47063 : if (dump_enabled_p ())
531 4268 : dump_printf_loc (MSG_NOTE, vect_location,
532 : "dependence distance = %d.\n", dist);
533 :
534 47063 : if (dist == 0)
535 : {
536 36040 : if (dump_enabled_p ())
537 3474 : 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 36040 : 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 36040 : if (loop->safelen < 2)
565 : {
566 32286 : tree indicator = dr_zero_step_indicator (dra);
567 32286 : if (!indicator || integer_zerop (indicator))
568 0 : return opt_result::failure_at (stmtinfo_a->stmt,
569 : "access also has a zero step\n");
570 32286 : else if (TREE_CODE (indicator) != INTEGER_CST)
571 1348 : vect_check_nonzero_value (loop_vinfo, indicator);
572 : }
573 36040 : continue;
574 36040 : }
575 :
576 11023 : 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 3696 : 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 3696 : if (nested_in_vect_loop_p (loop, stmtinfo_a)
588 3684 : || 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 3687 : if (DR_IS_READ (drb)
602 76 : && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
603 12 : || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
604 76 : STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
605 3687 : continue;
606 3687 : }
607 :
608 7327 : unsigned int abs_dist = abs (dist);
609 7327 : if (abs_dist >= 2 && abs_dist < *max_vf)
610 : {
611 : /* The dependence distance requires reduction of the maximal
612 : vectorization factor. */
613 486 : *max_vf = abs_dist;
614 486 : 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 7327 : if (abs_dist >= *max_vf)
621 : {
622 : /* Dependence distance does not create dependence, as far as
623 : vectorization is concerned, in this case. */
624 975 : if (dump_enabled_p ())
625 437 : dump_printf_loc (MSG_NOTE, vect_location,
626 : "dependence distance >= VF.\n");
627 975 : continue;
628 : }
629 :
630 6352 : 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 40682 : 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 138755 : vect_analyze_early_break_dependences (loop_vec_info loop_vinfo)
681 : {
682 138755 : DUMP_VECT_SCOPE ("vect_analyze_early_break_dependences");
683 :
684 : /* List of all load data references found during traversal. */
685 138755 : auto_vec<data_reference *> bases;
686 138755 : basic_block dest_bb = NULL;
687 :
688 138755 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
689 138755 : class loop *loop_nest = loop_outer (loop);
690 :
691 138755 : if (dump_enabled_p ())
692 1525 : dump_printf_loc (MSG_NOTE, vect_location,
693 : "loop contains multiple exits, analyzing"
694 : " statement dependencies.\n");
695 :
696 138755 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
697 25105 : if (dump_enabled_p ())
698 280 : 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 138755 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
709 25105 : dest_bb = loop->latch;
710 : else
711 113650 : 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 138755 : 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 138755 : bool check_deps = false;
721 :
722 503223 : do
723 : {
724 320989 : 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 2392601 : while (!gsi_end_p (gsi))
729 : {
730 2077189 : gimple *stmt = gsi_stmt (gsi);
731 2077189 : gsi_prev (&gsi);
732 2077189 : if (is_gimple_debug (stmt))
733 1834918 : continue;
734 :
735 1090759 : stmt_vec_info orig_stmt_vinfo = loop_vinfo->lookup_stmt (stmt);
736 1090759 : stmt_vec_info stmt_vinfo
737 1090759 : = vect_stmt_to_vectorize (orig_stmt_vinfo);
738 1090759 : auto dr_ref = STMT_VINFO_DATA_REF (stmt_vinfo);
739 1090759 : if (!dr_ref)
740 : {
741 : /* Trapping statements after the last early exit are fine. */
742 842541 : if (check_deps)
743 : {
744 511691 : bool could_trap_p = false;
745 511691 : gimple *cur_stmt = STMT_VINFO_STMT (stmt_vinfo);
746 511691 : could_trap_p = gimple_could_trap_p (cur_stmt);
747 511691 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_vinfo))
748 : {
749 188793 : gimple_stmt_iterator gsi2;
750 188793 : auto stmt_seq
751 188793 : = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_vinfo);
752 188793 : for (gsi2 = gsi_start (stmt_seq);
753 380995 : !could_trap_p && !gsi_end_p (gsi2); gsi_next (&gsi2))
754 : {
755 192202 : cur_stmt = gsi_stmt (gsi2);
756 192202 : could_trap_p = gimple_could_trap_p (cur_stmt);
757 : }
758 : }
759 :
760 511691 : if (could_trap_p)
761 : {
762 5029 : if (dump_enabled_p ())
763 144 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
764 : "cannot vectorize as operation may trap.\n");
765 5029 : return opt_result::failure_at (cur_stmt,
766 : "can't safely apply code motion to dependencies"
767 : " to vectorize the early exit. %G may trap.\n",
768 : cur_stmt);
769 : }
770 : }
771 :
772 837512 : continue;
773 837512 : }
774 :
775 : /* We know everything below dest_bb is safe since we know we
776 : had a full vector iteration when reaching it. Either by
777 : the loop entry / IV exit test being last or because this
778 : is the loop latch itself. */
779 248218 : if (!check_deps)
780 10976 : continue;
781 :
782 : /* Check if vector accesses to the object will be within bounds.
783 : must be a constant or assume loop will be versioned or niters
784 : bounded by VF so accesses are within range. We only need to check
785 : the reads since writes are moved to a safe place where if we get
786 : there we know they are safe to perform. */
787 237242 : if (DR_IS_READ (dr_ref))
788 : {
789 221516 : dr_set_safe_speculative_read_required (stmt_vinfo, true);
790 221516 : bool inbounds = ref_within_array_bound (stmt, DR_REF (dr_ref));
791 221516 : DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_vinfo)) = inbounds;
792 :
793 221516 : if (dump_enabled_p ())
794 2369 : dump_printf_loc (MSG_NOTE, vect_location,
795 : "marking DR (read) as possibly needing peeling "
796 : "for alignment at %G", stmt);
797 : }
798 :
799 237242 : if (DR_IS_READ (dr_ref))
800 221516 : bases.safe_push (dr_ref);
801 15726 : else if (DR_IS_WRITE (dr_ref))
802 : {
803 : /* We are moving writes down in the CFG. To be sure that this
804 : is valid after vectorization we have to check all the loads
805 : we are sinking the stores past to see if any of them may
806 : alias or are the same object.
807 :
808 : Same objects will not be an issue because unless the store
809 : is marked volatile the value can be forwarded. If the
810 : store is marked volatile we don't vectorize the loop
811 : anyway.
812 :
813 : That leaves the check for aliasing. We don't really need
814 : to care about the stores aliasing with each other since the
815 : stores are moved in order so the effects are still observed
816 : correctly. This leaves the check for WAR dependencies
817 : which we would be introducing here if the DR can alias.
818 : The check is quadratic in loads/stores but I have not found
819 : a better API to do this. I believe all loads and stores
820 : must be checked. We also must check them when we
821 : encountered the store, since we don't care about loads past
822 : the store. */
823 :
824 48822 : for (auto dr_read : bases)
825 15408 : if (dr_may_alias_p (dr_ref, dr_read, loop_nest))
826 : {
827 548 : if (dump_enabled_p ())
828 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
829 : vect_location,
830 : "early breaks not supported: "
831 : "overlapping loads and stores "
832 : "found before the break "
833 : "statement.\n");
834 :
835 548 : return opt_result::failure_at (stmt,
836 : "can't safely apply code motion to dependencies"
837 : " to vectorize the early exit. %G may alias with"
838 : " %G\n", stmt, dr_read->stmt);
839 : }
840 : }
841 :
842 473388 : if (gimple_vdef (stmt))
843 : {
844 15178 : if (dump_enabled_p ())
845 280 : dump_printf_loc (MSG_NOTE, vect_location,
846 : "==> recording stmt %G", stmt);
847 :
848 15178 : LOOP_VINFO_EARLY_BRK_STORES (loop_vinfo).safe_push (stmt);
849 : }
850 679726 : else if (gimple_vuse (stmt))
851 : {
852 221516 : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).safe_insert (0, stmt);
853 221516 : if (dump_enabled_p ())
854 2369 : dump_printf_loc (MSG_NOTE, vect_location,
855 : "marked statement for vUSE update: %G", stmt);
856 : }
857 : }
858 :
859 315412 : if (!single_pred_p (bb))
860 : {
861 133178 : gcc_assert (bb == loop->header);
862 133178 : break;
863 : }
864 :
865 : /* If we possibly sink through a virtual PHI make sure to elide that. */
866 182234 : if (gphi *vphi = get_virtual_phi (bb))
867 107 : LOOP_VINFO_EARLY_BRK_STORES (loop_vinfo).safe_push (vphi);
868 :
869 : /* All earlier blocks need dependence checking. */
870 182234 : check_deps = true;
871 182234 : bb = single_pred (bb);
872 182234 : }
873 : while (1);
874 :
875 : /* We don't allow outer -> inner loop transitions which should have been
876 : trapped already during loop form analysis. */
877 133178 : gcc_assert (dest_bb->loop_father == loop);
878 :
879 : /* Check that the destination block we picked has only one pred. To relax this we
880 : have to take special care when moving the statements. We don't currently support
881 : such control flow however this check is there to simplify how we handle
882 : labels that may be present anywhere in the IL. This check is to ensure that the
883 : labels aren't significant for the CFG. */
884 133178 : if (!single_pred (dest_bb))
885 0 : return opt_result::failure_at (vect_location,
886 : "chosen loop exit block (BB %d) does not have a "
887 : "single predecessor which is currently not "
888 : "supported for early break vectorization.\n",
889 : dest_bb->index);
890 :
891 133178 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo) = dest_bb;
892 :
893 133178 : if (!LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).is_empty ())
894 : {
895 : /* All uses shall be updated to that of the first load. Entries are
896 : stored in reverse order. */
897 122540 : tree vuse = gimple_vuse (LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).last ());
898 342802 : for (auto g : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo))
899 : {
900 220262 : if (dump_enabled_p ())
901 2306 : dump_printf_loc (MSG_NOTE, vect_location,
902 : "will update use: %T, mem_ref: %G", vuse, g);
903 : }
904 : }
905 :
906 133178 : if (dump_enabled_p ())
907 1377 : dump_printf_loc (MSG_NOTE, vect_location,
908 : "recorded statements to be moved to BB %d\n",
909 1377 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo)->index);
910 :
911 133178 : return opt_result::success ();
912 138755 : }
913 :
914 : /* Function vect_analyze_data_ref_dependences.
915 :
916 : Examine all the data references in the loop, and make sure there do not
917 : exist any data dependences between them. Set *MAX_VF according to
918 : the maximum vectorization factor the data dependences allow. */
919 :
920 : opt_result
921 323509 : vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo,
922 : unsigned int *max_vf)
923 : {
924 323509 : unsigned int i;
925 323509 : struct data_dependence_relation *ddr;
926 :
927 323509 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_dependences");
928 :
929 323509 : if (!LOOP_VINFO_DDRS (loop_vinfo).exists ())
930 : {
931 161125 : LOOP_VINFO_DDRS (loop_vinfo)
932 161125 : .create (LOOP_VINFO_DATAREFS (loop_vinfo).length ()
933 161125 : * LOOP_VINFO_DATAREFS (loop_vinfo).length ());
934 : /* We do not need read-read dependences. */
935 322250 : bool res = compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
936 : &LOOP_VINFO_DDRS (loop_vinfo),
937 161125 : LOOP_VINFO_LOOP_NEST (loop_vinfo),
938 : false);
939 161125 : gcc_assert (res);
940 : }
941 :
942 323509 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
943 :
944 : /* For epilogues we either have no aliases or alias versioning
945 : was applied to original loop. Therefore we may just get max_vf
946 : using VF of original loop. */
947 323509 : if (LOOP_VINFO_EPILOGUE_P (loop_vinfo))
948 15076 : *max_vf = LOOP_VINFO_ORIG_MAX_VECT_FACTOR (loop_vinfo);
949 : else
950 1269966 : FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
951 : {
952 981502 : opt_result res
953 981502 : = vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf);
954 981502 : if (!res)
955 19969 : return res;
956 : }
957 :
958 : /* If we have early break statements in the loop, check to see if they
959 : are of a form we can vectorizer. */
960 303540 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
961 138755 : return vect_analyze_early_break_dependences (loop_vinfo);
962 :
963 164785 : return opt_result::success ();
964 : }
965 :
966 :
967 : /* Function vect_slp_analyze_data_ref_dependence.
968 :
969 : Return TRUE if there (might) exist a dependence between a memory-reference
970 : DRA and a memory-reference DRB for VINFO. When versioning for alias
971 : may check a dependence at run-time, return FALSE. Adjust *MAX_VF
972 : according to the data dependence. */
973 :
974 : static bool
975 6867885 : vect_slp_analyze_data_ref_dependence (vec_info *vinfo,
976 : struct data_dependence_relation *ddr)
977 : {
978 6867885 : struct data_reference *dra = DDR_A (ddr);
979 6867885 : struct data_reference *drb = DDR_B (ddr);
980 6867885 : dr_vec_info *dr_info_a = vinfo->lookup_dr (dra);
981 6867885 : dr_vec_info *dr_info_b = vinfo->lookup_dr (drb);
982 :
983 : /* We need to check dependences of statements marked as unvectorizable
984 : as well, they still can prohibit vectorization. */
985 :
986 : /* Independent data accesses. */
987 6867885 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
988 : return false;
989 :
990 1096405 : if (dra == drb)
991 : return false;
992 :
993 : /* Read-read is OK. */
994 8315 : if (DR_IS_READ (dra) && DR_IS_READ (drb))
995 : return false;
996 :
997 : /* If dra and drb are part of the same interleaving chain consider
998 : them independent. */
999 8315 : if (STMT_VINFO_GROUPED_ACCESS (dr_info_a->stmt)
1000 8315 : && (DR_GROUP_FIRST_ELEMENT (dr_info_a->stmt)
1001 8315 : == DR_GROUP_FIRST_ELEMENT (dr_info_b->stmt)))
1002 : return false;
1003 :
1004 : /* Unknown data dependence. */
1005 8315 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1006 : {
1007 8315 : if (dump_enabled_p ())
1008 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1009 : "can't determine dependence between %T and %T\n",
1010 : DR_REF (dra), DR_REF (drb));
1011 : }
1012 0 : else if (dump_enabled_p ())
1013 0 : dump_printf_loc (MSG_NOTE, vect_location,
1014 : "determined dependence between %T and %T\n",
1015 : DR_REF (dra), DR_REF (drb));
1016 :
1017 : return true;
1018 : }
1019 :
1020 :
1021 : /* Analyze dependences involved in the transform of a store SLP NODE. */
1022 :
1023 : static bool
1024 656830 : vect_slp_analyze_store_dependences (vec_info *vinfo, slp_tree node)
1025 : {
1026 : /* This walks over all stmts involved in the SLP store done
1027 : in NODE verifying we can sink them up to the last stmt in the
1028 : group. */
1029 656830 : stmt_vec_info last_access_info = vect_find_last_scalar_stmt_in_slp (node);
1030 656830 : gcc_assert (DR_IS_WRITE (STMT_VINFO_DATA_REF (last_access_info)));
1031 :
1032 2385812 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (node).length (); ++k)
1033 : {
1034 1737267 : stmt_vec_info access_info
1035 1737267 : = vect_orig_stmt (SLP_TREE_SCALAR_STMTS (node)[k]);
1036 1737267 : if (access_info == last_access_info)
1037 649177 : continue;
1038 1088090 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1039 1088090 : ao_ref ref;
1040 1088090 : bool ref_initialized_p = false;
1041 1088090 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1042 10585002 : gsi_stmt (gsi) != last_access_info->stmt; gsi_next (&gsi))
1043 : {
1044 9505197 : gimple *stmt = gsi_stmt (gsi);
1045 16861903 : if (! gimple_vuse (stmt))
1046 2636984 : continue;
1047 :
1048 : /* If we couldn't record a (single) data reference for this
1049 : stmt we have to resort to the alias oracle. */
1050 6868213 : stmt_vec_info stmt_info = vinfo->lookup_stmt (stmt);
1051 6868213 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1052 6868213 : if (!dr_b)
1053 : {
1054 : /* We are moving a store - this means
1055 : we cannot use TBAA for disambiguation. */
1056 546 : if (!ref_initialized_p)
1057 546 : ao_ref_init (&ref, DR_REF (dr_a));
1058 546 : if (stmt_may_clobber_ref_p_1 (stmt, &ref, false)
1059 546 : || ref_maybe_used_by_stmt_p (stmt, &ref, false))
1060 8285 : return false;
1061 542 : continue;
1062 : }
1063 :
1064 6867667 : gcc_assert (!gimple_visited_p (stmt));
1065 :
1066 6867667 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1067 6867667 : dr_b, vNULL);
1068 6867667 : bool dependent = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1069 6867667 : free_dependence_relation (ddr);
1070 6867667 : if (dependent)
1071 : return false;
1072 : }
1073 : }
1074 : return true;
1075 : }
1076 :
1077 : /* Analyze dependences involved in the transform of a load SLP NODE. STORES
1078 : contain the vector of scalar stores of this instance if we are
1079 : disambiguating the loads. */
1080 :
1081 : static bool
1082 154582 : vect_slp_analyze_load_dependences (vec_info *vinfo, slp_tree node,
1083 : vec<stmt_vec_info> stores,
1084 : stmt_vec_info last_store_info)
1085 : {
1086 : /* This walks over all stmts involved in the SLP load done
1087 : in NODE verifying we can hoist them up to the first stmt in the
1088 : group. */
1089 154582 : stmt_vec_info first_access_info = vect_find_first_scalar_stmt_in_slp (node);
1090 154582 : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (first_access_info)));
1091 :
1092 542316 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (node).length (); ++k)
1093 : {
1094 387768 : if (! SLP_TREE_SCALAR_STMTS (node)[k])
1095 161730 : continue;
1096 387768 : stmt_vec_info access_info
1097 387768 : = vect_orig_stmt (SLP_TREE_SCALAR_STMTS (node)[k]);
1098 387768 : if (access_info == first_access_info)
1099 161730 : continue;
1100 226038 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1101 226038 : ao_ref ref;
1102 226038 : bool ref_initialized_p = false;
1103 226038 : hash_set<stmt_vec_info> grp_visited;
1104 226038 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1105 4500548 : gsi_stmt (gsi) != first_access_info->stmt; gsi_prev (&gsi))
1106 : {
1107 2137289 : gimple *stmt = gsi_stmt (gsi);
1108 3472781 : if (! gimple_vdef (stmt))
1109 2081649 : continue;
1110 :
1111 279471 : stmt_vec_info stmt_info = vinfo->lookup_stmt (stmt);
1112 :
1113 : /* If we run into a store of this same instance (we've just
1114 : marked those) then delay dependence checking until we run
1115 : into the last store because this is where it will have
1116 : been sunk to (and we verified that we can do that already). */
1117 279471 : if (gimple_visited_p (stmt))
1118 : {
1119 223831 : if (stmt_info != last_store_info)
1120 223829 : continue;
1121 :
1122 10 : for (stmt_vec_info &store_info : stores)
1123 : {
1124 4 : data_reference *store_dr = STMT_VINFO_DATA_REF (store_info);
1125 4 : ddr_p ddr = initialize_data_dependence_relation
1126 4 : (dr_a, store_dr, vNULL);
1127 4 : bool dependent
1128 4 : = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1129 4 : free_dependence_relation (ddr);
1130 4 : if (dependent)
1131 34 : return false;
1132 : }
1133 2 : continue;
1134 2 : }
1135 :
1136 114181 : auto check_hoist = [&] (stmt_vec_info stmt_info) -> bool
1137 : {
1138 : /* We are hoisting a load - this means we can use TBAA for
1139 : disambiguation. */
1140 58541 : if (!ref_initialized_p)
1141 58541 : ao_ref_init (&ref, DR_REF (dr_a));
1142 58541 : if (stmt_may_clobber_ref_p_1 (stmt_info->stmt, &ref, true))
1143 : {
1144 : /* If we couldn't record a (single) data reference for this
1145 : stmt we have to give up now. */
1146 214 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1147 214 : if (!dr_b)
1148 : return false;
1149 214 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1150 214 : dr_b, vNULL);
1151 214 : bool dependent
1152 214 : = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1153 214 : free_dependence_relation (ddr);
1154 214 : if (dependent)
1155 : return false;
1156 : }
1157 : /* No dependence. */
1158 : return true;
1159 55640 : };
1160 55640 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1161 : {
1162 : /* When we run into a store group we have to honor
1163 : that earlier stores might be moved here. We don't
1164 : know exactly which and where to since we lack a
1165 : back-mapping from DR to SLP node, so assume all
1166 : earlier stores are sunk here. It's enough to
1167 : consider the last stmt of a group for this.
1168 : ??? Both this and the fact that we disregard that
1169 : the conflicting instance might be removed later
1170 : is overly conservative. */
1171 55188 : if (!grp_visited.add (DR_GROUP_FIRST_ELEMENT (stmt_info)))
1172 10675 : for (auto store_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
1173 129067 : store_info != NULL;
1174 118392 : store_info = DR_GROUP_NEXT_ELEMENT (store_info))
1175 118426 : if ((store_info == stmt_info
1176 107760 : || get_later_stmt (store_info, stmt_info) == stmt_info)
1177 165849 : && !check_hoist (store_info))
1178 : return false;
1179 : }
1180 : else
1181 : {
1182 452 : if (!check_hoist (stmt_info))
1183 : return false;
1184 : }
1185 : }
1186 226038 : }
1187 : return true;
1188 : }
1189 :
1190 :
1191 : /* Function vect_analyze_data_ref_dependences.
1192 :
1193 : Examine all the data references in the basic-block, and make sure there
1194 : do not exist any data dependences between them. Set *MAX_VF according to
1195 : the maximum vectorization factor the data dependences allow. */
1196 :
1197 : bool
1198 783997 : vect_slp_analyze_instance_dependence (vec_info *vinfo, slp_instance instance)
1199 : {
1200 783997 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_dependence");
1201 :
1202 : /* The stores of this instance are at the root of the SLP tree. */
1203 783997 : slp_tree store = NULL;
1204 783997 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store)
1205 656830 : store = SLP_INSTANCE_TREE (instance);
1206 :
1207 : /* Verify we can sink stores to the vectorized stmt insert location. */
1208 656830 : stmt_vec_info last_store_info = NULL;
1209 656830 : if (store)
1210 : {
1211 656830 : if (! vect_slp_analyze_store_dependences (vinfo, store))
1212 : return false;
1213 :
1214 : /* Mark stores in this instance and remember the last one. */
1215 648545 : last_store_info = vect_find_last_scalar_stmt_in_slp (store);
1216 2376865 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (store).length (); ++k)
1217 1728320 : gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k]->stmt, true);
1218 : }
1219 :
1220 775712 : bool res = true;
1221 :
1222 : /* Verify we can sink loads to the vectorized stmt insert location,
1223 : special-casing stores of this instance. */
1224 1172530 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1225 154582 : if (! vect_slp_analyze_load_dependences (vinfo, load,
1226 : store
1227 : ? SLP_TREE_SCALAR_STMTS (store)
1228 : : vNULL, last_store_info))
1229 : {
1230 : res = false;
1231 : break;
1232 : }
1233 :
1234 : /* Unset the visited flag. */
1235 775712 : if (store)
1236 2376865 : for (unsigned k = 0; k < SLP_TREE_SCALAR_STMTS (store).length (); ++k)
1237 1728320 : gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k]->stmt, false);
1238 :
1239 : /* If this is a SLP instance with a store check if there's a dependent
1240 : load that cannot be forwarded from a previous iteration of a loop
1241 : both are in. This is to avoid situations like that in PR115777. */
1242 775712 : if (res && store)
1243 : {
1244 648521 : stmt_vec_info store_info
1245 648521 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (store)[0]);
1246 648521 : class loop *store_loop = gimple_bb (store_info->stmt)->loop_father;
1247 648521 : if (! loop_outer (store_loop))
1248 552722 : return res;
1249 95799 : vec<loop_p> loop_nest;
1250 95799 : loop_nest.create (1);
1251 95799 : loop_nest.quick_push (store_loop);
1252 95799 : data_reference *drs = nullptr;
1253 178950 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1254 : {
1255 36907 : if (! STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (load)[0]))
1256 0 : continue;
1257 36907 : stmt_vec_info load_info
1258 36907 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (load)[0]);
1259 36907 : if (gimple_bb (load_info->stmt)->loop_father != store_loop)
1260 5171 : continue;
1261 :
1262 : /* For now concern ourselves with write-after-read as we also
1263 : only look for re-use of the store within the same SLP instance.
1264 : We can still get a RAW here when the instance contais a PHI
1265 : with a backedge though, thus this test. */
1266 31736 : if (! vect_stmt_dominates_stmt_p (STMT_VINFO_STMT (load_info),
1267 : STMT_VINFO_STMT (store_info)))
1268 11852 : continue;
1269 :
1270 19884 : if (! drs)
1271 : {
1272 19026 : drs = create_data_ref (loop_preheader_edge (store_loop),
1273 : store_loop,
1274 19026 : DR_REF (STMT_VINFO_DATA_REF (store_info)),
1275 : store_info->stmt, false, false);
1276 19026 : if (! DR_BASE_ADDRESS (drs)
1277 16165 : || TREE_CODE (DR_STEP (drs)) != INTEGER_CST)
1278 : break;
1279 : }
1280 16714 : data_reference *drl
1281 16714 : = create_data_ref (loop_preheader_edge (store_loop),
1282 : store_loop,
1283 16714 : DR_REF (STMT_VINFO_DATA_REF (load_info)),
1284 : load_info->stmt, true, false);
1285 :
1286 : /* See whether the DRs have a known constant distance throughout
1287 : the containing loop iteration. */
1288 31709 : if (! DR_BASE_ADDRESS (drl)
1289 14596 : || ! operand_equal_p (DR_STEP (drs), DR_STEP (drl))
1290 8613 : || ! operand_equal_p (DR_BASE_ADDRESS (drs),
1291 8613 : DR_BASE_ADDRESS (drl))
1292 18437 : || ! operand_equal_p (DR_OFFSET (drs), DR_OFFSET (drl)))
1293 : {
1294 14995 : free_data_ref (drl);
1295 14995 : continue;
1296 : }
1297 :
1298 : /* If the next iteration load overlaps with a non-power-of-two offset
1299 : we are surely failing any STLF attempt. */
1300 1719 : HOST_WIDE_INT step = TREE_INT_CST_LOW (DR_STEP (drl));
1301 1719 : unsigned HOST_WIDE_INT sizes
1302 1719 : = (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drs))))
1303 1719 : * DR_GROUP_SIZE (store_info));
1304 1719 : unsigned HOST_WIDE_INT sizel
1305 1719 : = (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drl))))
1306 1719 : * DR_GROUP_SIZE (load_info));
1307 1719 : if (ranges_overlap_p (TREE_INT_CST_LOW (DR_INIT (drl)) + step, sizel,
1308 1719 : TREE_INT_CST_LOW (DR_INIT (drs)), sizes))
1309 : {
1310 835 : unsigned HOST_WIDE_INT dist
1311 835 : = absu_hwi (TREE_INT_CST_LOW (DR_INIT (drl)) + step
1312 835 : - TREE_INT_CST_LOW (DR_INIT (drs)));
1313 835 : poly_uint64 loadsz = tree_to_poly_uint64
1314 835 : (TYPE_SIZE_UNIT (SLP_TREE_VECTYPE (load)));
1315 835 : poly_uint64 storesz = tree_to_poly_uint64
1316 835 : (TYPE_SIZE_UNIT (SLP_TREE_VECTYPE (store)));
1317 : /* When the overlap aligns with vector sizes used for the loads
1318 : and the vector stores are larger or equal to the loads
1319 : forwarding should work. */
1320 1670 : if (maybe_gt (loadsz, storesz) || ! multiple_p (dist, loadsz))
1321 74 : load->avoid_stlf_fail = true;
1322 : }
1323 1719 : free_data_ref (drl);
1324 : }
1325 95799 : if (drs)
1326 19026 : free_data_ref (drs);
1327 95799 : loop_nest.release ();
1328 : }
1329 :
1330 : return res;
1331 : }
1332 :
1333 : /* Return the misalignment of DR_INFO accessed in VECTYPE with OFFSET
1334 : applied. */
1335 :
1336 : int
1337 5697692 : dr_misalignment (dr_vec_info *dr_info, tree vectype, poly_int64 offset)
1338 : {
1339 5697692 : HOST_WIDE_INT diff = 0;
1340 : /* Alignment is only analyzed for the first element of a DR group,
1341 : use that but adjust misalignment by the offset of the access. */
1342 5697692 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
1343 : {
1344 2243714 : dr_vec_info *first_dr
1345 2243714 : = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
1346 : /* vect_analyze_data_ref_accesses guarantees that DR_INIT are
1347 : INTEGER_CSTs and the first element in the group has the lowest
1348 : address. */
1349 2243714 : diff = (TREE_INT_CST_LOW (DR_INIT (dr_info->dr))
1350 2243714 : - TREE_INT_CST_LOW (DR_INIT (first_dr->dr)));
1351 2243714 : gcc_assert (diff >= 0);
1352 : dr_info = first_dr;
1353 : }
1354 :
1355 5697692 : int misalign = dr_info->misalignment;
1356 5697692 : gcc_assert (misalign != DR_MISALIGNMENT_UNINITIALIZED);
1357 5697692 : if (misalign == DR_MISALIGNMENT_UNKNOWN)
1358 : return misalign;
1359 :
1360 : /* If the access is only aligned for a vector type with smaller alignment
1361 : requirement the access has unknown misalignment. */
1362 3452397 : if (maybe_lt (dr_info->target_alignment * BITS_PER_UNIT,
1363 3452397 : targetm.vectorize.preferred_vector_alignment (vectype)))
1364 : return DR_MISALIGNMENT_UNKNOWN;
1365 :
1366 : /* Apply the offset from the DR group start and the externally supplied
1367 : offset which can for example result from a negative stride access. */
1368 3452394 : poly_int64 misalignment = misalign + diff + offset;
1369 :
1370 : /* Below we reject compile-time non-constant target alignments, but if
1371 : our misalignment is zero, then we are known to already be aligned
1372 : w.r.t. any such possible target alignment. */
1373 3452394 : if (known_eq (misalignment, 0))
1374 : return 0;
1375 :
1376 622047 : unsigned HOST_WIDE_INT target_alignment_c;
1377 622047 : if (!dr_info->target_alignment.is_constant (&target_alignment_c)
1378 622047 : || !known_misalignment (misalignment, target_alignment_c, &misalign))
1379 : return DR_MISALIGNMENT_UNKNOWN;
1380 622047 : return misalign;
1381 : }
1382 :
1383 : /* Record the base alignment guarantee given by DRB, which occurs
1384 : in STMT_INFO. */
1385 :
1386 : static void
1387 4443024 : vect_record_base_alignment (vec_info *vinfo, stmt_vec_info stmt_info,
1388 : innermost_loop_behavior *drb)
1389 : {
1390 4443024 : bool existed;
1391 4443024 : std::pair<stmt_vec_info, innermost_loop_behavior *> &entry
1392 4443024 : = vinfo->base_alignments.get_or_insert (drb->base_address, &existed);
1393 4443024 : if (!existed || entry.second->base_alignment < drb->base_alignment)
1394 : {
1395 1332853 : entry = std::make_pair (stmt_info, drb);
1396 1332853 : if (dump_enabled_p ())
1397 31642 : dump_printf_loc (MSG_NOTE, vect_location,
1398 : "recording new base alignment for %T\n"
1399 : " alignment: %d\n"
1400 : " misalignment: %d\n"
1401 : " based on: %G",
1402 : drb->base_address,
1403 : drb->base_alignment,
1404 : drb->base_misalignment,
1405 : stmt_info->stmt);
1406 : }
1407 4443024 : }
1408 :
1409 : /* If the region we're going to vectorize is reached, all unconditional
1410 : data references occur at least once. We can therefore pool the base
1411 : alignment guarantees from each unconditional reference. Do this by
1412 : going through all the data references in VINFO and checking whether
1413 : the containing statement makes the reference unconditionally. If so,
1414 : record the alignment of the base address in VINFO so that it can be
1415 : used for all other references with the same base. */
1416 :
1417 : void
1418 968307 : vect_record_base_alignments (vec_info *vinfo)
1419 : {
1420 968307 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1421 359218 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
1422 14757094 : for (data_reference *dr : vinfo->shared->datarefs)
1423 : {
1424 11947621 : dr_vec_info *dr_info = vinfo->lookup_dr (dr);
1425 11947621 : stmt_vec_info stmt_info = dr_info->stmt;
1426 11947621 : if (!DR_IS_CONDITIONAL_IN_STMT (dr)
1427 11940051 : && STMT_VINFO_VECTORIZABLE (stmt_info)
1428 4459690 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1429 : {
1430 4441610 : vect_record_base_alignment (vinfo, stmt_info, &DR_INNERMOST (dr));
1431 :
1432 : /* If DR is nested in the loop that is being vectorized, we can also
1433 : record the alignment of the base wrt the outer loop. */
1434 12743495 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
1435 1414 : vect_record_base_alignment
1436 1414 : (vinfo, stmt_info, &STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info));
1437 : }
1438 : }
1439 968307 : }
1440 :
1441 : /* Function vect_compute_data_ref_alignment
1442 :
1443 : Compute the misalignment of the data reference DR_INFO when vectorizing
1444 : with VECTYPE.
1445 :
1446 : Output:
1447 : 1. initialized misalignment info for DR_INFO
1448 :
1449 : FOR NOW: No analysis is actually performed. Misalignment is calculated
1450 : only for trivial cases. TODO. */
1451 :
1452 : static void
1453 1483934 : vect_compute_data_ref_alignment (vec_info *vinfo, dr_vec_info *dr_info,
1454 : tree vectype)
1455 : {
1456 1483934 : stmt_vec_info stmt_info = dr_info->stmt;
1457 1483934 : vec_base_alignments *base_alignments = &vinfo->base_alignments;
1458 1483934 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1459 1483934 : class loop *loop = NULL;
1460 1483934 : tree ref = DR_REF (dr_info->dr);
1461 :
1462 1483934 : if (dump_enabled_p ())
1463 49884 : dump_printf_loc (MSG_NOTE, vect_location,
1464 : "vect_compute_data_ref_alignment:\n");
1465 :
1466 1483934 : if (loop_vinfo)
1467 709881 : loop = LOOP_VINFO_LOOP (loop_vinfo);
1468 :
1469 : /* Initialize misalignment to unknown. */
1470 1483934 : SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
1471 :
1472 1483934 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1473 : return;
1474 :
1475 1464306 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
1476 1464306 : bool step_preserves_misalignment_p;
1477 :
1478 1464306 : poly_uint64 vector_alignment
1479 1464306 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
1480 : BITS_PER_UNIT);
1481 :
1482 1464306 : if (loop_vinfo
1483 1464306 : && dr_safe_speculative_read_required (stmt_info))
1484 : {
1485 : /* The required target alignment must be a power-of-2 value and is
1486 : computed as the product of vector element size, VF and group size.
1487 : We compute the constant part first as VF may be a variable. For
1488 : variable VF, the power-of-2 check of VF is deferred to runtime. */
1489 300246 : auto align_factor_c
1490 300246 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1491 300246 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1492 89830 : align_factor_c *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
1493 :
1494 300246 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1495 300246 : poly_uint64 new_alignment = vf * align_factor_c;
1496 :
1497 600492 : if ((vf.is_constant () && pow2p_hwi (new_alignment.to_constant ()))
1498 : || (!vf.is_constant () && pow2p_hwi (align_factor_c)))
1499 : {
1500 237017 : if (dump_enabled_p ())
1501 : {
1502 2864 : dump_printf_loc (MSG_NOTE, vect_location,
1503 : "alignment increased due to early break to ");
1504 2864 : dump_dec (MSG_NOTE, new_alignment);
1505 2864 : dump_printf (MSG_NOTE, " bytes.\n");
1506 : }
1507 237017 : vector_alignment = new_alignment;
1508 : }
1509 : }
1510 :
1511 1464306 : SET_DR_TARGET_ALIGNMENT (dr_info, vector_alignment);
1512 :
1513 : /* If the main loop has peeled for alignment we have no way of knowing
1514 : whether the data accesses in the epilogues are aligned. We can't at
1515 : compile time answer the question whether we have entered the main loop or
1516 : not. Fixes PR 92351. */
1517 1464306 : if (loop_vinfo)
1518 : {
1519 690253 : loop_vec_info orig_loop_vinfo = LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo);
1520 690253 : if (orig_loop_vinfo
1521 32481 : && LOOP_VINFO_PEELING_FOR_ALIGNMENT (orig_loop_vinfo) != 0)
1522 : return;
1523 : }
1524 :
1525 1464089 : unsigned HOST_WIDE_INT vect_align_c;
1526 1464089 : if (!vector_alignment.is_constant (&vect_align_c))
1527 : return;
1528 :
1529 : /* No step for BB vectorization. */
1530 1464089 : if (!loop)
1531 : {
1532 774053 : gcc_assert (integer_zerop (drb->step));
1533 : step_preserves_misalignment_p = true;
1534 : }
1535 :
1536 : else
1537 : {
1538 : /* We can only use base and misalignment information relative to
1539 : an innermost loop if the misalignment stays the same throughout the
1540 : execution of the loop. As above, this is the case if the stride of
1541 : the dataref evenly divides by the alignment. Make sure to check
1542 : previous epilogues and the main loop. */
1543 : step_preserves_misalignment_p = true;
1544 : auto lvinfo = loop_vinfo;
1545 1413065 : while (lvinfo)
1546 : {
1547 723029 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (lvinfo);
1548 723029 : step_preserves_misalignment_p
1549 723029 : &= multiple_p (drb->step_alignment * vf, vect_align_c);
1550 723029 : lvinfo = LOOP_VINFO_ORIG_LOOP_INFO (lvinfo);
1551 : }
1552 :
1553 690036 : if (!step_preserves_misalignment_p && dump_enabled_p ())
1554 315 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1555 : "step doesn't divide the vector alignment.\n");
1556 :
1557 : /* In case the dataref is in an inner-loop of the loop that is being
1558 : vectorized (LOOP), we use the base and misalignment information
1559 : relative to the outer-loop (LOOP). This is ok only if the
1560 : misalignment stays the same throughout the execution of the
1561 : inner-loop, which is why we have to check that the stride of the
1562 : dataref in the inner-loop evenly divides by the vector alignment. */
1563 690036 : if (step_preserves_misalignment_p
1564 690036 : && nested_in_vect_loop_p (loop, stmt_info))
1565 : {
1566 1413 : step_preserves_misalignment_p
1567 1413 : = (DR_STEP_ALIGNMENT (dr_info->dr) % vect_align_c) == 0;
1568 :
1569 1413 : if (dump_enabled_p ())
1570 : {
1571 498 : if (step_preserves_misalignment_p)
1572 358 : dump_printf_loc (MSG_NOTE, vect_location,
1573 : "inner step divides the vector alignment.\n");
1574 : else
1575 140 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1576 : "inner step doesn't divide the vector"
1577 : " alignment.\n");
1578 : }
1579 : }
1580 : }
1581 :
1582 1464089 : unsigned int base_alignment = drb->base_alignment;
1583 1464089 : unsigned int base_misalignment = drb->base_misalignment;
1584 :
1585 : /* Calculate the maximum of the pooled base address alignment and the
1586 : alignment that we can compute for DR itself. */
1587 1464089 : std::pair<stmt_vec_info, innermost_loop_behavior *> *entry
1588 1464089 : = base_alignments->get (drb->base_address);
1589 1464089 : if (entry
1590 1461178 : && base_alignment < (*entry).second->base_alignment
1591 1467086 : && (loop_vinfo
1592 2311 : || (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt_info->stmt),
1593 2311 : gimple_bb (entry->first->stmt))
1594 2144 : && (gimple_bb (stmt_info->stmt) != gimple_bb (entry->first->stmt)
1595 1909 : || (entry->first->dr_aux.group <= dr_info->group)))))
1596 : {
1597 2813 : base_alignment = entry->second->base_alignment;
1598 2813 : base_misalignment = entry->second->base_misalignment;
1599 : }
1600 :
1601 1464089 : if (drb->offset_alignment < vect_align_c
1602 1399034 : || !step_preserves_misalignment_p
1603 : /* We need to know whether the step wrt the vectorized loop is
1604 : negative when computing the starting misalignment below. */
1605 1390850 : || TREE_CODE (drb->step) != INTEGER_CST)
1606 : {
1607 100778 : if (dump_enabled_p ())
1608 3706 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1609 : "Unknown alignment for access: %T\n", ref);
1610 100778 : return;
1611 : }
1612 :
1613 1363311 : if (base_alignment < vect_align_c)
1614 : {
1615 684878 : unsigned int max_alignment;
1616 684878 : tree base = get_base_for_alignment (drb->base_address, &max_alignment);
1617 684878 : if (max_alignment < vect_align_c
1618 682479 : || (loop_vinfo && LOOP_VINFO_EPILOGUE_P (loop_vinfo))
1619 1348213 : || !vect_can_force_dr_alignment_p (base,
1620 663335 : vect_align_c * BITS_PER_UNIT))
1621 : {
1622 488828 : if (dump_enabled_p ())
1623 13531 : dump_printf_loc (MSG_NOTE, vect_location,
1624 : "can't force alignment of ref: %T\n", ref);
1625 488828 : return;
1626 : }
1627 :
1628 : /* Force the alignment of the decl.
1629 : NOTE: This is the only change to the code we make during
1630 : the analysis phase, before deciding to vectorize the loop. */
1631 196050 : if (dump_enabled_p ())
1632 7924 : dump_printf_loc (MSG_NOTE, vect_location,
1633 : "force alignment of %T\n", ref);
1634 :
1635 196050 : dr_info->base_decl = base;
1636 196050 : dr_info->base_misaligned = true;
1637 196050 : base_misalignment = 0;
1638 : }
1639 874483 : poly_int64 misalignment
1640 874483 : = base_misalignment + wi::to_poly_offset (drb->init).force_shwi ();
1641 :
1642 874483 : unsigned int const_misalignment;
1643 874483 : if (!known_misalignment (misalignment, vect_align_c, &const_misalignment))
1644 : {
1645 : if (dump_enabled_p ())
1646 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1647 : "Non-constant misalignment for access: %T\n", ref);
1648 : return;
1649 : }
1650 :
1651 874483 : SET_DR_MISALIGNMENT (dr_info, const_misalignment);
1652 :
1653 874483 : if (dump_enabled_p ())
1654 31467 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1655 : "misalign = %d bytes of ref %T\n",
1656 : const_misalignment, ref);
1657 :
1658 : return;
1659 : }
1660 :
1661 : /* Return whether DR_INFO, which is related to DR_PEEL_INFO in
1662 : that it only differs in DR_INIT, is aligned if DR_PEEL_INFO
1663 : is made aligned via peeling. */
1664 :
1665 : static bool
1666 1450921 : vect_dr_aligned_if_related_peeled_dr_is (dr_vec_info *dr_info,
1667 : dr_vec_info *dr_peel_info)
1668 : {
1669 1450921 : if (multiple_p (DR_TARGET_ALIGNMENT (dr_peel_info),
1670 1451665 : DR_TARGET_ALIGNMENT (dr_info)))
1671 : {
1672 1450177 : poly_offset_int diff
1673 1450177 : = (wi::to_poly_offset (DR_INIT (dr_peel_info->dr))
1674 1450177 : - wi::to_poly_offset (DR_INIT (dr_info->dr)));
1675 1450177 : if (known_eq (diff, 0)
1676 1450177 : || multiple_p (diff, DR_TARGET_ALIGNMENT (dr_info)))
1677 481181 : return true;
1678 : }
1679 : return false;
1680 : }
1681 :
1682 : /* Return whether DR_INFO is aligned if DR_PEEL_INFO is made
1683 : aligned via peeling. */
1684 :
1685 : static bool
1686 155635 : vect_dr_aligned_if_peeled_dr_is (dr_vec_info *dr_info,
1687 : dr_vec_info *dr_peel_info)
1688 : {
1689 155635 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_info->dr),
1690 155635 : DR_BASE_ADDRESS (dr_peel_info->dr), 0)
1691 36023 : || !operand_equal_p (DR_OFFSET (dr_info->dr),
1692 36023 : DR_OFFSET (dr_peel_info->dr), 0)
1693 190787 : || !operand_equal_p (DR_STEP (dr_info->dr),
1694 35152 : DR_STEP (dr_peel_info->dr), 0))
1695 120854 : return false;
1696 :
1697 34781 : return vect_dr_aligned_if_related_peeled_dr_is (dr_info, dr_peel_info);
1698 : }
1699 :
1700 : /* Compute the value for dr_info->misalign so that the access appears
1701 : aligned. This is used by peeling to compensate for dr_misalignment
1702 : applying the offset for negative step. */
1703 :
1704 : int
1705 20327 : vect_dr_misalign_for_aligned_access (dr_vec_info *dr_info)
1706 : {
1707 20327 : if (tree_int_cst_sgn (DR_STEP (dr_info->dr)) >= 0)
1708 : return 0;
1709 :
1710 198 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
1711 198 : poly_int64 misalignment
1712 198 : = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
1713 198 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1714 :
1715 198 : unsigned HOST_WIDE_INT target_alignment_c;
1716 198 : int misalign;
1717 198 : if (!dr_info->target_alignment.is_constant (&target_alignment_c)
1718 198 : || !known_misalignment (misalignment, target_alignment_c, &misalign))
1719 : return DR_MISALIGNMENT_UNKNOWN;
1720 198 : return misalign;
1721 : }
1722 :
1723 : /* Function vect_update_misalignment_for_peel.
1724 : Sets DR_INFO's misalignment
1725 : - to 0 if it has the same alignment as DR_PEEL_INFO,
1726 : - to the misalignment computed using NPEEL if DR_INFO's salignment is known,
1727 : - to -1 (unknown) otherwise.
1728 :
1729 : DR_INFO - the data reference whose misalignment is to be adjusted.
1730 : DR_PEEL_INFO - the data reference whose misalignment is being made
1731 : zero in the vector loop by the peel.
1732 : NPEEL - the number of iterations in the peel loop if the misalignment
1733 : of DR_PEEL_INFO is known at compile time. */
1734 :
1735 : static void
1736 2741 : vect_update_misalignment_for_peel (dr_vec_info *dr_info,
1737 : dr_vec_info *dr_peel_info, int npeel)
1738 : {
1739 : /* If dr_info is aligned of dr_peel_info is, then mark it so. */
1740 2741 : if (vect_dr_aligned_if_peeled_dr_is (dr_info, dr_peel_info))
1741 : {
1742 444 : SET_DR_MISALIGNMENT (dr_info,
1743 : vect_dr_misalign_for_aligned_access (dr_peel_info));
1744 444 : return;
1745 : }
1746 :
1747 2297 : unsigned HOST_WIDE_INT alignment;
1748 2297 : if (DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment)
1749 2297 : && known_alignment_for_access_p (dr_info,
1750 2297 : STMT_VINFO_VECTYPE (dr_info->stmt))
1751 234 : && known_alignment_for_access_p (dr_peel_info,
1752 234 : STMT_VINFO_VECTYPE (dr_peel_info->stmt)))
1753 : {
1754 186 : int misal = dr_info->misalignment;
1755 186 : misal += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
1756 186 : misal &= alignment - 1;
1757 186 : set_dr_misalignment (dr_info, misal);
1758 186 : return;
1759 : }
1760 :
1761 2111 : if (dump_enabled_p ())
1762 34 : dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment " \
1763 : "to unknown (-1).\n");
1764 2111 : SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
1765 : }
1766 :
1767 : /* Return true if alignment is relevant for DR_INFO. */
1768 :
1769 : static bool
1770 1436482 : vect_relevant_for_alignment_p (dr_vec_info *dr_info)
1771 : {
1772 1436482 : stmt_vec_info stmt_info = dr_info->stmt;
1773 :
1774 1436482 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
1775 : return false;
1776 :
1777 : /* For interleaving, only the alignment of the first access matters. */
1778 1435432 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1779 1644648 : && DR_GROUP_FIRST_ELEMENT (stmt_info) != stmt_info)
1780 : return false;
1781 :
1782 : /* Scatter-gather and invariant accesses continue to address individual
1783 : scalars, so vector-level alignment is irrelevant. */
1784 1352939 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1785 1352939 : || integer_zerop (DR_STEP (dr_info->dr)))
1786 51747 : return false;
1787 :
1788 : /* Strided accesses perform only component accesses, alignment is
1789 : irrelevant for them. */
1790 1301192 : if (STMT_VINFO_STRIDED_P (stmt_info)
1791 1301192 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1792 : return false;
1793 :
1794 : return true;
1795 : }
1796 :
1797 : /* Given an memory reference EXP return whether its alignment is less
1798 : than its size. */
1799 :
1800 : static bool
1801 1291306 : not_size_aligned (tree exp)
1802 : {
1803 1291306 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
1804 : return true;
1805 :
1806 1291306 : return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
1807 1291306 : > get_object_alignment (exp));
1808 : }
1809 :
1810 : /* Function vector_alignment_reachable_p
1811 :
1812 : Return true if vector alignment for DR_INFO is reachable by peeling
1813 : a few loop iterations. Return false otherwise. */
1814 :
1815 : static bool
1816 502666 : vector_alignment_reachable_p (dr_vec_info *dr_info, poly_uint64 vf)
1817 : {
1818 502666 : stmt_vec_info stmt_info = dr_info->stmt;
1819 502666 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1820 502666 : poly_uint64 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1821 1005332 : poly_uint64 vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
1822 502666 : unsigned elem_size = vector_element_size (vector_size, nelements);
1823 502666 : unsigned group_size = 1;
1824 :
1825 502666 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1826 : {
1827 : /* For interleaved access we peel only if number of iterations in
1828 : the prolog loop ({VF - misalignment}), is a multiple of the
1829 : number of the interleaved accesses. */
1830 :
1831 : /* FORNOW: handle only known alignment. */
1832 82580 : if (!known_alignment_for_access_p (dr_info, vectype))
1833 502666 : return false;
1834 :
1835 48935 : unsigned mis_in_elements = dr_misalignment (dr_info, vectype) / elem_size;
1836 59930 : if (!multiple_p (nelements - mis_in_elements, DR_GROUP_SIZE (stmt_info)))
1837 : return false;
1838 :
1839 10995 : group_size = DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
1840 : }
1841 :
1842 : /* If the vectorization factor does not guarantee DR advancement of
1843 : a multiple of the target alignment no peeling will help. */
1844 431081 : if (!multiple_p (elem_size * group_size * vf, dr_target_alignment (dr_info)))
1845 122 : return false;
1846 :
1847 : /* If misalignment is known at the compile time then allow peeling
1848 : only if natural alignment is reachable through peeling. */
1849 430959 : if (known_alignment_for_access_p (dr_info, vectype)
1850 666745 : && !aligned_access_p (dr_info, vectype))
1851 : {
1852 13389 : HOST_WIDE_INT elmsize =
1853 13389 : int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1854 13389 : if (dump_enabled_p ())
1855 : {
1856 750 : dump_printf_loc (MSG_NOTE, vect_location,
1857 : "data size = %wd. misalignment = %d.\n", elmsize,
1858 : dr_misalignment (dr_info, vectype));
1859 : }
1860 13389 : if (dr_misalignment (dr_info, vectype) % elmsize)
1861 : {
1862 53 : if (dump_enabled_p ())
1863 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1864 : "data size does not divide the misalignment.\n");
1865 53 : return false;
1866 : }
1867 : }
1868 :
1869 430906 : if (!known_alignment_for_access_p (dr_info, vectype))
1870 : {
1871 195173 : tree type = TREE_TYPE (DR_REF (dr_info->dr));
1872 195173 : bool is_packed = not_size_aligned (DR_REF (dr_info->dr));
1873 195173 : if (dump_enabled_p ())
1874 14797 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1875 : "Unknown misalignment, %snaturally aligned\n",
1876 : is_packed ? "not " : "");
1877 195173 : return targetm.vectorize.vector_alignment_reachable (type, is_packed);
1878 : }
1879 :
1880 : return true;
1881 : }
1882 :
1883 :
1884 : /* Calculate the cost of the memory access represented by DR_INFO. */
1885 :
1886 : static void
1887 542904 : vect_get_data_access_cost (vec_info *vinfo, dr_vec_info *dr_info,
1888 : dr_alignment_support alignment_support_scheme,
1889 : int misalignment,
1890 : unsigned int *inside_cost,
1891 : unsigned int *outside_cost,
1892 : stmt_vector_for_cost *body_cost_vec,
1893 : stmt_vector_for_cost *prologue_cost_vec)
1894 : {
1895 542904 : stmt_vec_info stmt_info = dr_info->stmt;
1896 :
1897 542904 : if (DR_IS_READ (dr_info->dr))
1898 396635 : vect_get_load_cost (vinfo, stmt_info, NULL, 1,
1899 : alignment_support_scheme, misalignment, true,
1900 : inside_cost, outside_cost, prologue_cost_vec,
1901 : body_cost_vec, false);
1902 : else
1903 146269 : vect_get_store_cost (vinfo,stmt_info, NULL, 1,
1904 : alignment_support_scheme, misalignment, inside_cost,
1905 : body_cost_vec);
1906 :
1907 542904 : if (dump_enabled_p ())
1908 27482 : dump_printf_loc (MSG_NOTE, vect_location,
1909 : "vect_get_data_access_cost: inside_cost = %d, "
1910 : "outside_cost = %d.\n", *inside_cost, *outside_cost);
1911 542904 : }
1912 :
1913 :
1914 : typedef struct _vect_peel_info
1915 : {
1916 : dr_vec_info *dr_info;
1917 : int npeel;
1918 : unsigned int count;
1919 : } *vect_peel_info;
1920 :
1921 : typedef struct _vect_peel_extended_info
1922 : {
1923 : vec_info *vinfo;
1924 : struct _vect_peel_info peel_info;
1925 : unsigned int inside_cost;
1926 : unsigned int outside_cost;
1927 : } *vect_peel_extended_info;
1928 :
1929 :
1930 : /* Peeling hashtable helpers. */
1931 :
1932 : struct peel_info_hasher : free_ptr_hash <_vect_peel_info>
1933 : {
1934 : static inline hashval_t hash (const _vect_peel_info *);
1935 : static inline bool equal (const _vect_peel_info *, const _vect_peel_info *);
1936 : };
1937 :
1938 : inline hashval_t
1939 647310 : peel_info_hasher::hash (const _vect_peel_info *peel_info)
1940 : {
1941 647310 : return (hashval_t) peel_info->npeel;
1942 : }
1943 :
1944 : inline bool
1945 353618 : peel_info_hasher::equal (const _vect_peel_info *a, const _vect_peel_info *b)
1946 : {
1947 353618 : return (a->npeel == b->npeel);
1948 : }
1949 :
1950 :
1951 : /* Insert DR_INFO into peeling hash table with NPEEL as key. */
1952 :
1953 : static void
1954 294350 : vect_peeling_hash_insert (hash_table<peel_info_hasher> *peeling_htab,
1955 : loop_vec_info loop_vinfo, dr_vec_info *dr_info,
1956 : int npeel, bool supportable_if_not_aligned)
1957 : {
1958 294350 : struct _vect_peel_info elem, *slot;
1959 294350 : _vect_peel_info **new_slot;
1960 :
1961 294350 : elem.npeel = npeel;
1962 294350 : slot = peeling_htab->find (&elem);
1963 294350 : if (slot)
1964 124685 : slot->count++;
1965 : else
1966 : {
1967 169665 : slot = XNEW (struct _vect_peel_info);
1968 169665 : slot->npeel = npeel;
1969 169665 : slot->dr_info = dr_info;
1970 169665 : slot->count = 1;
1971 169665 : new_slot = peeling_htab->find_slot (slot, INSERT);
1972 169665 : *new_slot = slot;
1973 : }
1974 :
1975 : /* If this DR is not supported with unknown misalignment then bias
1976 : this slot when the cost model is disabled. */
1977 294350 : if (!supportable_if_not_aligned
1978 294350 : && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1979 4584 : slot->count += VECT_MAX_COST;
1980 294350 : }
1981 :
1982 :
1983 : /* Traverse peeling hash table to find peeling option that aligns maximum
1984 : number of data accesses. */
1985 :
1986 : int
1987 35621 : vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1988 : _vect_peel_extended_info *max)
1989 : {
1990 35621 : vect_peel_info elem = *slot;
1991 :
1992 35621 : if (elem->count > max->peel_info.count
1993 21645 : || (elem->count == max->peel_info.count
1994 16938 : && max->peel_info.npeel > elem->npeel))
1995 : {
1996 13992 : max->peel_info.npeel = elem->npeel;
1997 13992 : max->peel_info.count = elem->count;
1998 13992 : max->peel_info.dr_info = elem->dr_info;
1999 : }
2000 :
2001 35621 : return 1;
2002 : }
2003 :
2004 : /* Get the costs of peeling NPEEL iterations for LOOP_VINFO, checking
2005 : data access costs for all data refs. If UNKNOWN_MISALIGNMENT is true,
2006 : npeel is computed at runtime but DR0_INFO's misalignment will be zero
2007 : after peeling. */
2008 :
2009 : static void
2010 310906 : vect_get_peeling_costs_all_drs (loop_vec_info loop_vinfo,
2011 : dr_vec_info *dr0_info,
2012 : unsigned int *inside_cost,
2013 : unsigned int *outside_cost,
2014 : stmt_vector_for_cost *body_cost_vec,
2015 : stmt_vector_for_cost *prologue_cost_vec,
2016 : unsigned int npeel)
2017 : {
2018 310906 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2019 :
2020 310906 : bool dr0_alignment_known_p
2021 : = (dr0_info
2022 574835 : && known_alignment_for_access_p (dr0_info,
2023 263929 : STMT_VINFO_VECTYPE (dr0_info->stmt)));
2024 :
2025 1509483 : for (data_reference *dr : datarefs)
2026 : {
2027 576765 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2028 576765 : if (!vect_relevant_for_alignment_p (dr_info))
2029 33861 : continue;
2030 :
2031 542904 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2032 542904 : dr_alignment_support alignment_support_scheme;
2033 542904 : int misalignment;
2034 542904 : unsigned HOST_WIDE_INT alignment;
2035 :
2036 542904 : bool negative = tree_int_cst_compare (DR_STEP (dr_info->dr),
2037 542904 : size_zero_node) < 0;
2038 542904 : poly_int64 off = 0;
2039 542904 : if (negative)
2040 21844 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2041 21844 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2042 :
2043 542904 : if (npeel == 0)
2044 264474 : misalignment = dr_misalignment (dr_info, vectype, off);
2045 278430 : else if (dr_info == dr0_info
2046 278430 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2047 : misalignment = 0;
2048 95303 : else if (!dr0_alignment_known_p
2049 7431 : || !known_alignment_for_access_p (dr_info, vectype)
2050 102734 : || !DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment))
2051 : misalignment = DR_MISALIGNMENT_UNKNOWN;
2052 : else
2053 : {
2054 6435 : misalignment = dr_misalignment (dr_info, vectype, off);
2055 6435 : misalignment += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
2056 6435 : misalignment &= alignment - 1;
2057 : }
2058 542904 : alignment_support_scheme
2059 542904 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2060 : misalignment);
2061 :
2062 542904 : vect_get_data_access_cost (loop_vinfo, dr_info,
2063 : alignment_support_scheme, misalignment,
2064 : inside_cost, outside_cost,
2065 : body_cost_vec, prologue_cost_vec);
2066 : }
2067 310906 : }
2068 :
2069 : /* Traverse peeling hash table and calculate cost for each peeling option.
2070 : Find the one with the lowest cost. */
2071 :
2072 : int
2073 114741 : vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
2074 : _vect_peel_extended_info *min)
2075 : {
2076 114741 : vect_peel_info elem = *slot;
2077 114741 : int dummy;
2078 114741 : unsigned int inside_cost = 0, outside_cost = 0;
2079 114741 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (min->vinfo);
2080 114741 : stmt_vector_for_cost prologue_cost_vec, body_cost_vec,
2081 : epilogue_cost_vec;
2082 :
2083 114741 : prologue_cost_vec.create (2);
2084 114741 : body_cost_vec.create (2);
2085 114741 : epilogue_cost_vec.create (2);
2086 :
2087 114741 : vect_get_peeling_costs_all_drs (loop_vinfo, elem->dr_info, &inside_cost,
2088 : &outside_cost, &body_cost_vec,
2089 114741 : &prologue_cost_vec, elem->npeel);
2090 :
2091 114741 : body_cost_vec.release ();
2092 :
2093 229482 : outside_cost += vect_get_known_peeling_cost
2094 114741 : (loop_vinfo, elem->npeel, &dummy,
2095 : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2096 : &prologue_cost_vec, &epilogue_cost_vec);
2097 :
2098 : /* Prologue and epilogue costs are added to the target model later.
2099 : These costs depend only on the scalar iteration cost, the
2100 : number of peeling iterations finally chosen, and the number of
2101 : misaligned statements. So discard the information found here. */
2102 114741 : prologue_cost_vec.release ();
2103 114741 : epilogue_cost_vec.release ();
2104 :
2105 114741 : if (inside_cost < min->inside_cost
2106 1437 : || (inside_cost == min->inside_cost
2107 1063 : && outside_cost < min->outside_cost))
2108 : {
2109 113310 : min->inside_cost = inside_cost;
2110 113310 : min->outside_cost = outside_cost;
2111 113310 : min->peel_info.dr_info = elem->dr_info;
2112 113310 : min->peel_info.npeel = elem->npeel;
2113 113310 : min->peel_info.count = elem->count;
2114 : }
2115 :
2116 114741 : return 1;
2117 : }
2118 :
2119 :
2120 : /* Choose best peeling option by traversing peeling hash table and either
2121 : choosing an option with the lowest cost (if cost model is enabled) or the
2122 : option that aligns as many accesses as possible. */
2123 :
2124 : static struct _vect_peel_extended_info
2125 125923 : vect_peeling_hash_choose_best_peeling (hash_table<peel_info_hasher> *peeling_htab,
2126 : loop_vec_info loop_vinfo)
2127 : {
2128 125923 : struct _vect_peel_extended_info res;
2129 :
2130 125923 : res.peel_info.dr_info = NULL;
2131 125923 : res.vinfo = loop_vinfo;
2132 :
2133 125923 : if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2134 : {
2135 112000 : res.inside_cost = INT_MAX;
2136 112000 : res.outside_cost = INT_MAX;
2137 112000 : peeling_htab->traverse <_vect_peel_extended_info *,
2138 226741 : vect_peeling_hash_get_lowest_cost> (&res);
2139 : }
2140 : else
2141 : {
2142 13923 : res.peel_info.count = 0;
2143 13923 : peeling_htab->traverse <_vect_peel_extended_info *,
2144 49544 : vect_peeling_hash_get_most_frequent> (&res);
2145 13923 : res.inside_cost = 0;
2146 13923 : res.outside_cost = 0;
2147 : }
2148 :
2149 125923 : return res;
2150 : }
2151 :
2152 : /* Return if vectorization is definitely, possibly, or unlikely to be
2153 : supportable after loop peeling. */
2154 :
2155 : static enum peeling_support
2156 77607 : vect_peeling_supportable (loop_vec_info loop_vinfo, dr_vec_info *dr0_info,
2157 : unsigned npeel)
2158 : {
2159 77607 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2160 77607 : enum dr_alignment_support supportable_dr_alignment;
2161 :
2162 77607 : bool dr0_alignment_known_p
2163 155214 : = known_alignment_for_access_p (dr0_info,
2164 77607 : STMT_VINFO_VECTYPE (dr0_info->stmt));
2165 77607 : bool has_unsupported_dr_p = false;
2166 77607 : unsigned int dr0_step = tree_to_shwi (DR_STEP (dr0_info->dr));
2167 77607 : int known_unsupported_misalignment = DR_MISALIGNMENT_UNKNOWN;
2168 :
2169 : /* Check if each data ref can be vectorized after peeling. */
2170 331913 : for (data_reference *dr : datarefs)
2171 : {
2172 114827 : if (dr == dr0_info->dr)
2173 76666 : continue;
2174 :
2175 38161 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2176 38161 : if (!vect_relevant_for_alignment_p (dr_info)
2177 38161 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2178 6671 : continue;
2179 :
2180 31490 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2181 31490 : int misalignment;
2182 31490 : unsigned HOST_WIDE_INT alignment;
2183 31490 : if (!dr0_alignment_known_p
2184 1838 : || !known_alignment_for_access_p (dr_info, vectype)
2185 33328 : || !DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment))
2186 : misalignment = DR_MISALIGNMENT_UNKNOWN;
2187 : else
2188 : {
2189 1824 : misalignment = dr_misalignment (dr_info, vectype);
2190 1824 : misalignment += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
2191 1824 : misalignment &= alignment - 1;
2192 : }
2193 31490 : supportable_dr_alignment
2194 31490 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2195 : misalignment);
2196 31490 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2197 : {
2198 30053 : has_unsupported_dr_p = true;
2199 :
2200 : /* If unaligned unsupported DRs exist, we do following checks to see
2201 : if they can be mutually aligned to support vectorization. If yes,
2202 : we can try peeling and create a runtime (mutual alignment) check
2203 : to guard the peeled loop. If no, return PEELING_UNSUPPORTED. */
2204 :
2205 : /* 1) If unaligned unsupported DRs have different alignment steps, the
2206 : probability of DRs being mutually aligned is very low, and it's
2207 : quite complex to check mutual alignment at runtime. We return
2208 : PEELING_UNSUPPORTED in this case. */
2209 30053 : if (tree_to_shwi (DR_STEP (dr)) != dr0_step)
2210 77607 : return peeling_unsupported;
2211 :
2212 : /* 2) Based on above same alignment step condition, if one known
2213 : misaligned DR has zero misalignment, or different misalignment
2214 : amount from another known misaligned DR, peeling is unable to
2215 : help make all these DRs aligned together. We won't try peeling
2216 : with versioning anymore. */
2217 25813 : int curr_dr_misalignment = dr_misalignment (dr_info, vectype);
2218 25813 : if (curr_dr_misalignment == 0)
2219 : return peeling_unsupported;
2220 14318 : if (known_unsupported_misalignment != DR_MISALIGNMENT_UNKNOWN)
2221 : {
2222 8 : if (curr_dr_misalignment != DR_MISALIGNMENT_UNKNOWN
2223 8 : && curr_dr_misalignment != known_unsupported_misalignment)
2224 : return peeling_unsupported;
2225 : }
2226 : else
2227 : known_unsupported_misalignment = curr_dr_misalignment;
2228 : }
2229 : }
2230 :
2231 : /* Vectorization is known to be supportable with peeling alone when there is
2232 : no unsupported DR. */
2233 61872 : return has_unsupported_dr_p ? peeling_maybe_supported
2234 : : peeling_known_supported;
2235 : }
2236 :
2237 : /* Compare two data-references DRA and DRB to group them into chunks
2238 : with related alignment. */
2239 :
2240 : static int
2241 3687540 : dr_align_group_sort_cmp (const void *dra_, const void *drb_)
2242 : {
2243 3687540 : data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2244 3687540 : data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2245 3687540 : int cmp;
2246 :
2247 : /* Stabilize sort. */
2248 3687540 : if (dra == drb)
2249 : return 0;
2250 :
2251 : /* Ordering of DRs according to base. */
2252 3687540 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
2253 : DR_BASE_ADDRESS (drb));
2254 3687540 : if (cmp != 0)
2255 : return cmp;
2256 :
2257 : /* And according to DR_OFFSET. */
2258 1622424 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2259 1622424 : if (cmp != 0)
2260 : return cmp;
2261 :
2262 : /* And after step. */
2263 1610626 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
2264 1610626 : if (cmp != 0)
2265 : return cmp;
2266 :
2267 : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2268 1606127 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
2269 1606127 : if (cmp == 0)
2270 174156 : return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2271 : return cmp;
2272 : }
2273 :
2274 : /* Function vect_enhance_data_refs_alignment
2275 :
2276 : This pass will use loop versioning and loop peeling in order to enhance
2277 : the alignment of data references in the loop.
2278 :
2279 : FOR NOW: we assume that whatever versioning/peeling takes place, only the
2280 : original loop is to be vectorized. Any other loops that are created by
2281 : the transformations performed in this pass - are not supposed to be
2282 : vectorized. This restriction will be relaxed.
2283 :
2284 : This pass will require a cost model to guide it whether to apply peeling
2285 : or versioning or a combination of the two. For example, the scheme that
2286 : intel uses when given a loop with several memory accesses, is as follows:
2287 : choose one memory access ('p') which alignment you want to force by doing
2288 : peeling. Then, either (1) generate a loop in which 'p' is aligned and all
2289 : other accesses are not necessarily aligned, or (2) use loop versioning to
2290 : generate one loop in which all accesses are aligned, and another loop in
2291 : which only 'p' is necessarily aligned.
2292 :
2293 : ("Automatic Intra-Register Vectorization for the Intel Architecture",
2294 : Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
2295 : Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
2296 :
2297 : Devising a cost model is the most critical aspect of this work. It will
2298 : guide us on which access to peel for, whether to use loop versioning, how
2299 : many versions to create, etc. The cost model will probably consist of
2300 : generic considerations as well as target specific considerations (on
2301 : powerpc for example, misaligned stores are more painful than misaligned
2302 : loads).
2303 :
2304 : Here are the general steps involved in alignment enhancements:
2305 :
2306 : -- original loop, before alignment analysis:
2307 : for (i=0; i<N; i++){
2308 : x = q[i]; # DR_MISALIGNMENT(q) = unknown
2309 : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2310 : }
2311 :
2312 : -- After vect_compute_data_refs_alignment:
2313 : for (i=0; i<N; i++){
2314 : x = q[i]; # DR_MISALIGNMENT(q) = 3
2315 : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2316 : }
2317 :
2318 : -- Possibility 1: we do loop versioning:
2319 : if (p is aligned) {
2320 : for (i=0; i<N; i++){ # loop 1A
2321 : x = q[i]; # DR_MISALIGNMENT(q) = 3
2322 : p[i] = y; # DR_MISALIGNMENT(p) = 0
2323 : }
2324 : }
2325 : else {
2326 : for (i=0; i<N; i++){ # loop 1B
2327 : x = q[i]; # DR_MISALIGNMENT(q) = 3
2328 : p[i] = y; # DR_MISALIGNMENT(p) = unaligned
2329 : }
2330 : }
2331 :
2332 : -- Possibility 2: we do loop peeling:
2333 : for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
2334 : x = q[i];
2335 : p[i] = y;
2336 : }
2337 : for (i = 3; i < N; i++){ # loop 2A
2338 : x = q[i]; # DR_MISALIGNMENT(q) = 0
2339 : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2340 : }
2341 :
2342 : -- Possibility 3: combination of loop peeling and versioning:
2343 : if (p & q are mutually aligned) {
2344 : for (i=0; i<3; i++){ # (peeled loop iterations).
2345 : x = q[i];
2346 : p[i] = y;
2347 : }
2348 : for (i=3; i<N; i++){ # loop 3A
2349 : x = q[i]; # DR_MISALIGNMENT(q) = 0
2350 : p[i] = y; # DR_MISALIGNMENT(p) = 0
2351 : }
2352 : }
2353 : else {
2354 : for (i=0; i<N; i++){ # (scalar loop, not to be vectorized).
2355 : x = q[i]; # DR_MISALIGNMENT(q) = 3
2356 : p[i] = y; # DR_MISALIGNMENT(p) = unknown
2357 : }
2358 : }
2359 :
2360 : These loops are later passed to loop_transform to be vectorized. The
2361 : vectorizer will use the alignment information to guide the transformation
2362 : (whether to generate regular loads/stores, or with special handling for
2363 : misalignment). */
2364 :
2365 : opt_result
2366 327039 : vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
2367 : {
2368 327039 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2369 327039 : dr_vec_info *first_store = NULL;
2370 327039 : dr_vec_info *dr0_info = NULL;
2371 327039 : struct data_reference *dr;
2372 327039 : unsigned int i;
2373 327039 : bool do_peeling = false;
2374 327039 : bool do_versioning = false;
2375 327039 : bool try_peeling_with_versioning = false;
2376 327039 : unsigned int npeel = 0;
2377 327039 : bool one_misalignment_known = false;
2378 327039 : bool one_misalignment_unknown = false;
2379 327039 : bool one_dr_unsupportable = false;
2380 327039 : dr_vec_info *unsupportable_dr_info = NULL;
2381 327039 : unsigned int dr0_same_align_drs = 0, first_store_same_align_drs = 0;
2382 327039 : hash_table<peel_info_hasher> peeling_htab (1);
2383 :
2384 327039 : DUMP_VECT_SCOPE ("vect_enhance_data_refs_alignment");
2385 :
2386 : /* Reset data so we can safely be called multiple times. */
2387 327039 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
2388 327039 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = 0;
2389 :
2390 327039 : if (LOOP_VINFO_DATAREFS (loop_vinfo).is_empty ())
2391 13294 : return opt_result::success ();
2392 :
2393 : /* Sort the vector of datarefs so DRs that have the same or dependent
2394 : alignment are next to each other. */
2395 313745 : auto_vec<data_reference_p> datarefs
2396 313745 : = LOOP_VINFO_DATAREFS (loop_vinfo).copy ();
2397 313745 : datarefs.qsort (dr_align_group_sort_cmp);
2398 :
2399 : /* Compute the number of DRs that become aligned when we peel
2400 : a dataref so it becomes aligned. */
2401 627490 : auto_vec<unsigned> n_same_align_refs (datarefs.length ());
2402 313745 : n_same_align_refs.quick_grow_cleared (datarefs.length ());
2403 313745 : unsigned i0;
2404 646474 : for (i0 = 0; i0 < datarefs.length (); ++i0)
2405 326156 : if (DR_BASE_ADDRESS (datarefs[i0]))
2406 : break;
2407 2007786 : for (i = i0 + 1; i <= datarefs.length (); ++i)
2408 : {
2409 690148 : if (i == datarefs.length ()
2410 382976 : || !operand_equal_p (DR_BASE_ADDRESS (datarefs[i0]),
2411 382976 : DR_BASE_ADDRESS (datarefs[i]), 0)
2412 184823 : || !operand_equal_p (DR_OFFSET (datarefs[i0]),
2413 184823 : DR_OFFSET (datarefs[i]), 0)
2414 873813 : || !operand_equal_p (DR_STEP (datarefs[i0]),
2415 183665 : DR_STEP (datarefs[i]), 0))
2416 : {
2417 : /* The subgroup [i0, i-1] now only differs in DR_INIT and
2418 : possibly DR_TARGET_ALIGNMENT. Still the whole subgroup
2419 : will get known misalignment if we align one of the refs
2420 : with the largest DR_TARGET_ALIGNMENT. */
2421 1197113 : for (unsigned j = i0; j < i; ++j)
2422 : {
2423 690148 : dr_vec_info *dr_infoj = loop_vinfo->lookup_dr (datarefs[j]);
2424 2796436 : for (unsigned k = i0; k < i; ++k)
2425 : {
2426 2106288 : if (k == j)
2427 690148 : continue;
2428 1416140 : dr_vec_info *dr_infok = loop_vinfo->lookup_dr (datarefs[k]);
2429 1416140 : if (vect_dr_aligned_if_related_peeled_dr_is (dr_infok,
2430 : dr_infoj))
2431 454636 : n_same_align_refs[j]++;
2432 : }
2433 : }
2434 : i0 = i;
2435 : }
2436 : }
2437 :
2438 : /* While cost model enhancements are expected in the future, the high level
2439 : view of the code at this time is as follows:
2440 :
2441 : A) If there is a misaligned access then see if doing peeling alone can
2442 : make all data references satisfy vect_supportable_dr_alignment. If so,
2443 : update data structures and return.
2444 :
2445 : B) If peeling alone wasn't possible and there is a data reference with an
2446 : unknown misalignment that does not satisfy vect_supportable_dr_alignment
2447 : then we may use either of the following two approaches.
2448 :
2449 : B1) Try peeling with versioning: Add a runtime loop versioning check to
2450 : see if all unsupportable data references are mutually aligned, which
2451 : means they will be uniformly aligned after a certain amount of loop
2452 : peeling. If peeling and versioning can be used together, set
2453 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to TRUE and return.
2454 :
2455 : B2) Try versioning alone: Add a runtime loop versioning check to see if
2456 : all unsupportable data references are already uniformly aligned
2457 : without loop peeling. If versioning can be applied alone, set
2458 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to FALSE and return.
2459 :
2460 : Above B1 is more powerful and more likely to be adopted than B2. But B2
2461 : is still available and useful in some cases, for example, the cost model
2462 : does not allow much peeling.
2463 :
2464 : C) If none of above was successful then the alignment was not enhanced,
2465 : just return. */
2466 :
2467 : /* (1) Peeling to force alignment. */
2468 :
2469 : /* (1.1) Decide whether to perform peeling, how many iterations to peel, and
2470 : if vectorization may be supported by peeling with versioning.
2471 : Considerations:
2472 : - How many accesses will become aligned due to the peeling
2473 : - How many accesses will become unaligned due to the peeling,
2474 : and the cost of misaligned accesses.
2475 : - The cost of peeling (the extra runtime checks, the increase
2476 : in code size). */
2477 :
2478 313745 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2479 869024 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2480 : {
2481 598434 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2482 598434 : if (!vect_relevant_for_alignment_p (dr_info))
2483 95768 : continue;
2484 :
2485 502666 : stmt_vec_info stmt_info = dr_info->stmt;
2486 502666 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2487 :
2488 : /* With variable VF, unsafe speculative read can be avoided for known
2489 : inbounds DRs as long as partial vectors are used. */
2490 502666 : if (!vf.is_constant ()
2491 : && dr_safe_speculative_read_required (stmt_info)
2492 : && DR_SCALAR_KNOWN_BOUNDS (dr_info))
2493 : {
2494 : dr_set_safe_speculative_read_required (stmt_info, false);
2495 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2496 : }
2497 :
2498 502666 : do_peeling = vector_alignment_reachable_p (dr_info, vf);
2499 502666 : if (do_peeling)
2500 : {
2501 428835 : if (known_alignment_for_access_p (dr_info, vectype))
2502 : {
2503 235733 : unsigned int npeel_tmp = 0;
2504 235733 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2505 235733 : size_zero_node) < 0;
2506 :
2507 : /* If known_alignment_for_access_p then we have set
2508 : DR_MISALIGNMENT which is only done if we know it at compiler
2509 : time, so it is safe to assume target alignment is constant.
2510 : */
2511 235733 : unsigned int target_align =
2512 235733 : DR_TARGET_ALIGNMENT (dr_info).to_constant ();
2513 235733 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (dr_info);
2514 235733 : poly_int64 off = 0;
2515 235733 : if (negative)
2516 2378 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2517 235733 : unsigned int mis = dr_misalignment (dr_info, vectype, off);
2518 235733 : mis = negative ? mis : -mis;
2519 235733 : if (mis != 0)
2520 12375 : npeel_tmp = (mis & (target_align - 1)) / dr_size;
2521 :
2522 : /* For multiple types, it is possible that the bigger type access
2523 : will have more than one peeling option. E.g., a loop with two
2524 : types: one of size (vector size / 4), and the other one of
2525 : size (vector size / 8). Vectorization factor will 8. If both
2526 : accesses are misaligned by 3, the first one needs one scalar
2527 : iteration to be aligned, and the second one needs 5. But the
2528 : first one will be aligned also by peeling 5 scalar
2529 : iterations, and in that case both accesses will be aligned.
2530 : Hence, except for the immediate peeling amount, we also want
2531 : to try to add full vector size, while we don't exceed
2532 : vectorization factor.
2533 : We do this automatically for cost model, since we calculate
2534 : cost for every peeling option. */
2535 235733 : poly_uint64 nscalars = npeel_tmp;
2536 235733 : if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2537 : {
2538 39621 : unsigned group_size = 1;
2539 39621 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2540 1900 : group_size = DR_GROUP_SIZE (stmt_info);
2541 39621 : nscalars = vf * group_size;
2542 : }
2543 :
2544 : /* Save info about DR in the hash table. Also include peeling
2545 : amounts according to the explanation above. Indicate
2546 : the alignment status when the ref is not aligned.
2547 : ??? Rather than using unknown alignment here we should
2548 : prune all entries from the peeling hashtable which cause
2549 : DRs to be not supported. */
2550 235733 : bool supportable_if_not_aligned
2551 : = vect_supportable_dr_alignment
2552 235733 : (loop_vinfo, dr_info, vectype, DR_MISALIGNMENT_UNKNOWN);
2553 530083 : while (known_le (npeel_tmp, nscalars))
2554 : {
2555 294350 : vect_peeling_hash_insert (&peeling_htab, loop_vinfo,
2556 : dr_info, npeel_tmp,
2557 : supportable_if_not_aligned);
2558 294350 : npeel_tmp += MAX (1, target_align / dr_size);
2559 : }
2560 :
2561 235733 : one_misalignment_known = true;
2562 : }
2563 : else
2564 : {
2565 : /* If we don't know any misalignment values, we prefer
2566 : peeling for data-ref that has the maximum number of data-refs
2567 : with the same alignment, unless the target prefers to align
2568 : stores over load. */
2569 193102 : unsigned same_align_drs = n_same_align_refs[i];
2570 193102 : if (!dr0_info
2571 193102 : || dr0_same_align_drs < same_align_drs)
2572 : {
2573 : dr0_same_align_drs = same_align_drs;
2574 : dr0_info = dr_info;
2575 : }
2576 : /* For data-refs with the same number of related
2577 : accesses prefer the one where the misalign
2578 : computation will be invariant in the outermost loop. */
2579 57134 : else if (dr0_same_align_drs == same_align_drs)
2580 : {
2581 56178 : class loop *ivloop0, *ivloop;
2582 56178 : ivloop0 = outermost_invariant_loop_for_expr
2583 56178 : (loop, DR_BASE_ADDRESS (dr0_info->dr));
2584 56178 : ivloop = outermost_invariant_loop_for_expr
2585 56178 : (loop, DR_BASE_ADDRESS (dr));
2586 56178 : if ((ivloop && !ivloop0)
2587 56178 : || (ivloop && ivloop0
2588 56172 : && flow_loop_nested_p (ivloop, ivloop0)))
2589 : dr0_info = dr_info;
2590 : }
2591 :
2592 193102 : one_misalignment_unknown = true;
2593 :
2594 : /* Check for data refs with unsupportable alignment that
2595 : can be peeled. */
2596 193102 : enum dr_alignment_support supportable_dr_alignment
2597 193102 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2598 : DR_MISALIGNMENT_UNKNOWN);
2599 193102 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2600 : {
2601 96075 : one_dr_unsupportable = true;
2602 96075 : unsupportable_dr_info = dr_info;
2603 : }
2604 :
2605 193102 : if (!first_store && DR_IS_WRITE (dr))
2606 : {
2607 39488 : first_store = dr_info;
2608 39488 : first_store_same_align_drs = same_align_drs;
2609 : }
2610 : }
2611 : }
2612 : else
2613 : {
2614 73831 : if (!aligned_access_p (dr_info, vectype))
2615 : {
2616 43155 : if (dump_enabled_p ())
2617 2058 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2618 : "vector alignment may not be reachable\n");
2619 : break;
2620 : }
2621 : }
2622 : }
2623 :
2624 : /* Check if we can possibly peel the loop. */
2625 313745 : if (!vect_can_advance_ivs_p (loop_vinfo)
2626 308117 : || !slpeel_can_duplicate_loop_p (loop, LOOP_VINFO_MAIN_EXIT (loop_vinfo),
2627 308117 : loop_preheader_edge (loop))
2628 308117 : || loop->inner
2629 : /* We don't currently maintaing the LCSSA for prologue peeled inversed
2630 : loops. */
2631 620407 : || (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)
2632 28513 : && !LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo)))
2633 : do_peeling = false;
2634 :
2635 313745 : struct _vect_peel_extended_info peel_for_known_alignment;
2636 313745 : struct _vect_peel_extended_info peel_for_unknown_alignment;
2637 313745 : struct _vect_peel_extended_info best_peel;
2638 :
2639 313745 : peel_for_unknown_alignment.inside_cost = INT_MAX;
2640 313745 : peel_for_unknown_alignment.outside_cost = INT_MAX;
2641 313745 : peel_for_unknown_alignment.peel_info.count = 0;
2642 :
2643 313745 : if (do_peeling
2644 313745 : && one_misalignment_unknown)
2645 : {
2646 : /* Check if the target requires to prefer stores over loads, i.e., if
2647 : misaligned stores are more expensive than misaligned loads (taking
2648 : drs with same alignment into account). */
2649 118655 : unsigned int load_inside_cost = 0;
2650 118655 : unsigned int load_outside_cost = 0;
2651 118655 : unsigned int store_inside_cost = 0;
2652 118655 : unsigned int store_outside_cost = 0;
2653 118655 : unsigned int estimated_npeels = vect_vf_for_cost (loop_vinfo) / 2;
2654 :
2655 118655 : stmt_vector_for_cost dummy;
2656 118655 : dummy.create (2);
2657 118655 : vect_get_peeling_costs_all_drs (loop_vinfo, dr0_info,
2658 : &load_inside_cost,
2659 : &load_outside_cost,
2660 : &dummy, &dummy, estimated_npeels);
2661 118655 : dummy.release ();
2662 :
2663 118655 : if (first_store)
2664 : {
2665 30533 : dummy.create (2);
2666 30533 : vect_get_peeling_costs_all_drs (loop_vinfo, first_store,
2667 : &store_inside_cost,
2668 : &store_outside_cost,
2669 : &dummy, &dummy,
2670 : estimated_npeels);
2671 30533 : dummy.release ();
2672 : }
2673 : else
2674 : {
2675 88122 : store_inside_cost = INT_MAX;
2676 88122 : store_outside_cost = INT_MAX;
2677 : }
2678 :
2679 118655 : if (load_inside_cost > store_inside_cost
2680 118655 : || (load_inside_cost == store_inside_cost
2681 29964 : && load_outside_cost > store_outside_cost))
2682 : {
2683 118655 : dr0_info = first_store;
2684 118655 : dr0_same_align_drs = first_store_same_align_drs;
2685 118655 : peel_for_unknown_alignment.inside_cost = store_inside_cost;
2686 118655 : peel_for_unknown_alignment.outside_cost = store_outside_cost;
2687 : }
2688 : else
2689 : {
2690 118655 : peel_for_unknown_alignment.inside_cost = load_inside_cost;
2691 118655 : peel_for_unknown_alignment.outside_cost = load_outside_cost;
2692 : }
2693 :
2694 118655 : stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2695 118655 : prologue_cost_vec.create (2);
2696 118655 : epilogue_cost_vec.create (2);
2697 :
2698 118655 : int dummy2;
2699 237310 : peel_for_unknown_alignment.outside_cost += vect_get_known_peeling_cost
2700 118655 : (loop_vinfo, estimated_npeels, &dummy2,
2701 : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2702 : &prologue_cost_vec, &epilogue_cost_vec);
2703 :
2704 118655 : prologue_cost_vec.release ();
2705 118655 : epilogue_cost_vec.release ();
2706 :
2707 118655 : peel_for_unknown_alignment.peel_info.count = dr0_same_align_drs + 1;
2708 : }
2709 :
2710 313745 : peel_for_unknown_alignment.peel_info.npeel = 0;
2711 313745 : peel_for_unknown_alignment.peel_info.dr_info = dr0_info;
2712 :
2713 313745 : best_peel = peel_for_unknown_alignment;
2714 :
2715 313745 : peel_for_known_alignment.inside_cost = INT_MAX;
2716 313745 : peel_for_known_alignment.outside_cost = INT_MAX;
2717 313745 : peel_for_known_alignment.peel_info.count = 0;
2718 313745 : peel_for_known_alignment.peel_info.dr_info = NULL;
2719 :
2720 313745 : if (do_peeling && one_misalignment_known)
2721 : {
2722 : /* Peeling is possible, but there is no data access that is not supported
2723 : unless aligned. So we try to choose the best possible peeling from
2724 : the hash table. */
2725 125923 : peel_for_known_alignment = vect_peeling_hash_choose_best_peeling
2726 125923 : (&peeling_htab, loop_vinfo);
2727 : }
2728 :
2729 : /* Compare costs of peeling for known and unknown alignment. */
2730 313745 : if (peel_for_known_alignment.peel_info.dr_info != NULL
2731 125923 : && peel_for_unknown_alignment.inside_cost
2732 : >= peel_for_known_alignment.inside_cost)
2733 : {
2734 111826 : best_peel = peel_for_known_alignment;
2735 :
2736 : /* If the best peeling for known alignment has NPEEL == 0, perform no
2737 : peeling at all except if there is an unsupportable dr that we can
2738 : align. */
2739 111826 : if (best_peel.peel_info.npeel == 0 && !one_dr_unsupportable)
2740 : do_peeling = false;
2741 : }
2742 :
2743 : /* If there is an unsupportable data ref, prefer this over all choices so far
2744 : since we'd have to discard a chosen peeling except when it accidentally
2745 : aligned the unsupportable data ref. */
2746 209348 : if (one_dr_unsupportable)
2747 : dr0_info = unsupportable_dr_info;
2748 235213 : else if (do_peeling)
2749 : {
2750 : /* Calculate the penalty for no peeling, i.e. leaving everything as-is.
2751 : TODO: Use nopeel_outside_cost or get rid of it? */
2752 46977 : unsigned nopeel_inside_cost = 0;
2753 46977 : unsigned nopeel_outside_cost = 0;
2754 :
2755 46977 : stmt_vector_for_cost dummy;
2756 46977 : dummy.create (2);
2757 46977 : vect_get_peeling_costs_all_drs (loop_vinfo, NULL, &nopeel_inside_cost,
2758 : &nopeel_outside_cost, &dummy, &dummy, 0);
2759 46977 : dummy.release ();
2760 :
2761 : /* Add epilogue costs. As we do not peel for alignment here, no prologue
2762 : costs will be recorded. */
2763 46977 : stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2764 46977 : prologue_cost_vec.create (2);
2765 46977 : epilogue_cost_vec.create (2);
2766 :
2767 46977 : int dummy2;
2768 93954 : nopeel_outside_cost += vect_get_known_peeling_cost
2769 46977 : (loop_vinfo, 0, &dummy2,
2770 : &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
2771 : &prologue_cost_vec, &epilogue_cost_vec);
2772 :
2773 46977 : prologue_cost_vec.release ();
2774 46977 : epilogue_cost_vec.release ();
2775 :
2776 46977 : npeel = best_peel.peel_info.npeel;
2777 46977 : dr0_info = best_peel.peel_info.dr_info;
2778 :
2779 : /* If no peeling is not more expensive than the best peeling we
2780 : have so far, don't perform any peeling. */
2781 46977 : if (nopeel_inside_cost <= best_peel.inside_cost)
2782 40519 : do_peeling = false;
2783 : }
2784 :
2785 125509 : if (do_peeling)
2786 : {
2787 77607 : stmt_vec_info stmt_info = dr0_info->stmt;
2788 77607 : if (known_alignment_for_access_p (dr0_info,
2789 : STMT_VINFO_VECTYPE (stmt_info)))
2790 : {
2791 6442 : bool negative = tree_int_cst_compare (DR_STEP (dr0_info->dr),
2792 6442 : size_zero_node) < 0;
2793 6442 : if (!npeel)
2794 : {
2795 : /* Since it's known at compile time, compute the number of
2796 : iterations in the peeled loop (the peeling factor) for use in
2797 : updating DR_MISALIGNMENT values. The peeling factor is the
2798 : vectorization factor minus the misalignment as an element
2799 : count. */
2800 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2801 0 : poly_int64 off = 0;
2802 0 : if (negative)
2803 0 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2804 0 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2805 0 : unsigned int mis
2806 0 : = dr_misalignment (dr0_info, vectype, off);
2807 0 : mis = negative ? mis : -mis;
2808 : /* If known_alignment_for_access_p then we have set
2809 : DR_MISALIGNMENT which is only done if we know it at compiler
2810 : time, so it is safe to assume target alignment is constant.
2811 : */
2812 0 : unsigned int target_align =
2813 0 : DR_TARGET_ALIGNMENT (dr0_info).to_constant ();
2814 0 : npeel = ((mis & (target_align - 1))
2815 0 : / vect_get_scalar_dr_size (dr0_info));
2816 : }
2817 :
2818 : /* For interleaved data access every iteration accesses all the
2819 : members of the group, therefore we divide the number of iterations
2820 : by the group size. */
2821 6442 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2822 281 : npeel /= DR_GROUP_SIZE (stmt_info);
2823 :
2824 6442 : if (dump_enabled_p ())
2825 280 : dump_printf_loc (MSG_NOTE, vect_location,
2826 : "Try peeling by %d\n", npeel);
2827 : }
2828 :
2829 : /* Check how peeling for alignment can support vectorization. Function
2830 : vect_peeling_supportable returns one of the three possible values:
2831 : - PEELING_KNOWN_SUPPORTED: indicates that we know all unsupported
2832 : datarefs can be aligned after peeling. We can use peeling alone.
2833 : - PEELING_MAYBE_SUPPORTED: indicates that peeling may be able to make
2834 : these datarefs aligned but we are not sure about it at compile time.
2835 : We will try peeling with versioning to add a runtime check to guard
2836 : the peeled loop.
2837 : - PEELING_UNSUPPORTED: indicates that peeling is almost impossible to
2838 : support vectorization. We will stop trying peeling. */
2839 77607 : switch (vect_peeling_supportable (loop_vinfo, dr0_info, npeel))
2840 : {
2841 : case peeling_known_supported:
2842 : break;
2843 13124 : case peeling_maybe_supported:
2844 13124 : try_peeling_with_versioning = true;
2845 13124 : break;
2846 15735 : case peeling_unsupported:
2847 15735 : do_peeling = false;
2848 15735 : break;
2849 : }
2850 :
2851 : /* Check if all datarefs are supportable and log. */
2852 77607 : if (do_peeling
2853 77607 : && npeel == 0
2854 77607 : && known_alignment_for_access_p (dr0_info,
2855 : STMT_VINFO_VECTYPE (stmt_info)))
2856 3 : return opt_result::success ();
2857 :
2858 : /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
2859 77604 : if (do_peeling)
2860 : {
2861 61869 : unsigned max_allowed_peel
2862 61869 : = param_vect_max_peeling_for_alignment;
2863 61869 : if (loop_cost_model (loop) <= VECT_COST_MODEL_CHEAP)
2864 : max_allowed_peel = 0;
2865 13181 : if (max_allowed_peel != (unsigned)-1)
2866 : {
2867 48709 : unsigned max_peel = npeel;
2868 48709 : if (max_peel == 0)
2869 : {
2870 45988 : poly_uint64 target_align = DR_TARGET_ALIGNMENT (dr0_info);
2871 45988 : unsigned HOST_WIDE_INT target_align_c;
2872 45988 : if (target_align.is_constant (&target_align_c))
2873 91976 : max_peel =
2874 45988 : target_align_c / vect_get_scalar_dr_size (dr0_info) - 1;
2875 : else
2876 : {
2877 : do_peeling = false;
2878 : if (dump_enabled_p ())
2879 : dump_printf_loc (MSG_NOTE, vect_location,
2880 : "Disable peeling, max peels set and vector"
2881 : " alignment unknown\n");
2882 : }
2883 : }
2884 48709 : if (max_peel > max_allowed_peel)
2885 : {
2886 48701 : do_peeling = false;
2887 48701 : if (dump_enabled_p ())
2888 53 : dump_printf_loc (MSG_NOTE, vect_location,
2889 : "Disable peeling, max peels reached: %d\n", max_peel);
2890 : }
2891 : }
2892 : }
2893 :
2894 : /* Cost model #2 - if peeling may result in a remaining loop not
2895 : iterating enough to be vectorized then do not peel. Since this
2896 : is a cost heuristic rather than a correctness decision, use the
2897 : most likely runtime value for variable vectorization factors. */
2898 53 : if (do_peeling
2899 13168 : && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2900 : {
2901 3165 : unsigned int assumed_vf = vect_vf_for_cost (loop_vinfo);
2902 3165 : unsigned int max_peel = npeel == 0 ? assumed_vf - 1 : npeel;
2903 3165 : if ((unsigned HOST_WIDE_INT) LOOP_VINFO_INT_NITERS (loop_vinfo)
2904 3165 : < assumed_vf + max_peel)
2905 : do_peeling = false;
2906 : }
2907 :
2908 : if (do_peeling)
2909 : {
2910 : /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
2911 : If the misalignment of DR_i is identical to that of dr0 then set
2912 : DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
2913 : dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
2914 : by the peeling factor times the element size of DR_i (MOD the
2915 : vectorization factor times the size). Otherwise, the
2916 : misalignment of DR_i must be set to unknown. */
2917 27810 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2918 15451 : if (dr != dr0_info->dr)
2919 : {
2920 3092 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2921 3092 : if (!vect_relevant_for_alignment_p (dr_info))
2922 351 : continue;
2923 :
2924 2741 : vect_update_misalignment_for_peel (dr_info, dr0_info, npeel);
2925 : }
2926 : }
2927 :
2928 77604 : if (do_peeling && !try_peeling_with_versioning)
2929 : {
2930 : /* Update data structures if peeling will be applied alone. */
2931 11353 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
2932 11353 : if (npeel)
2933 2085 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
2934 : else
2935 9268 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
2936 11353 : SET_DR_MISALIGNMENT (dr0_info,
2937 : vect_dr_misalign_for_aligned_access (dr0_info));
2938 11353 : if (dump_enabled_p ())
2939 : {
2940 338 : dump_printf_loc (MSG_NOTE, vect_location,
2941 : "Alignment of access forced using peeling.\n");
2942 338 : dump_printf_loc (MSG_NOTE, vect_location,
2943 : "Peeling for alignment will be applied.\n");
2944 : }
2945 :
2946 : /* The inside-loop cost will be accounted for in vectorizable_load
2947 : and vectorizable_store correctly with adjusted alignments.
2948 : Drop the body_cst_vec on the floor here. */
2949 11353 : return opt_result::success ();
2950 : }
2951 : }
2952 :
2953 : /* (2) Versioning to force alignment. */
2954 :
2955 : /* Try versioning if:
2956 : 1) optimize loop for speed and the cost-model is not cheap
2957 : 2) there is at least one unsupported misaligned data ref with an unknown
2958 : misalignment, and
2959 : 3) all misaligned data refs with a known misalignment are supported, and
2960 : 4) the number of runtime alignment checks is within reason. */
2961 :
2962 302389 : do_versioning
2963 302389 : = (optimize_loop_nest_for_speed_p (loop)
2964 301961 : && !loop->inner /* FORNOW */
2965 602895 : && loop_cost_model (loop) > VECT_COST_MODEL_CHEAP);
2966 :
2967 : if (do_versioning)
2968 : {
2969 293373 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2970 : {
2971 220030 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2972 220030 : if (!vect_relevant_for_alignment_p (dr_info))
2973 158604 : continue;
2974 :
2975 149925 : stmt_vec_info stmt_info = dr_info->stmt;
2976 149925 : if (STMT_VINFO_STRIDED_P (stmt_info))
2977 : {
2978 : do_versioning = false;
2979 4823 : break;
2980 : }
2981 :
2982 149031 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2983 149031 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2984 149031 : size_zero_node) < 0;
2985 149031 : poly_int64 off = 0;
2986 149031 : if (negative)
2987 3056 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2988 3056 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2989 149031 : int misalignment;
2990 149031 : if ((misalignment = dr_misalignment (dr_info, vectype, off)) == 0)
2991 88499 : continue;
2992 :
2993 60532 : enum dr_alignment_support supportable_dr_alignment
2994 60532 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2995 : misalignment);
2996 60532 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2997 : {
2998 15099 : if (misalignment != DR_MISALIGNMENT_UNKNOWN
2999 15099 : || (LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
3000 11694 : >= (unsigned) param_vect_max_version_for_alignment_checks))
3001 : {
3002 : do_versioning = false;
3003 4823 : break;
3004 : }
3005 :
3006 : /* Forcing alignment in the first iteration is no good if
3007 : we don't keep it across iterations. For now, just disable
3008 : versioning in this case.
3009 : ?? We could actually unroll the loop to achieve the required
3010 : overall step alignment, and forcing the alignment could be
3011 : done by doing some iterations of the non-vectorized loop. */
3012 11286 : if (!multiple_p (vf * DR_STEP_ALIGNMENT (dr),
3013 11286 : DR_TARGET_ALIGNMENT (dr_info)))
3014 : {
3015 : do_versioning = false;
3016 : break;
3017 : }
3018 :
3019 : /* Use "mask = DR_TARGET_ALIGNMENT - 1" to test rightmost address
3020 : bits for runtime alignment check. For example, for 16 bytes
3021 : target alignment the mask is 15 = 0xf. */
3022 11286 : poly_uint64 mask = DR_TARGET_ALIGNMENT (dr_info) - 1;
3023 :
3024 : /* FORNOW: use the same mask to test all potentially unaligned
3025 : references in the loop. */
3026 11286 : if (maybe_ne (LOOP_VINFO_PTR_MASK (loop_vinfo), 0U)
3027 11286 : && maybe_ne (LOOP_VINFO_PTR_MASK (loop_vinfo), mask))
3028 : {
3029 : do_versioning = false;
3030 : break;
3031 : }
3032 :
3033 11170 : LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
3034 11170 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (stmt_info);
3035 : }
3036 : }
3037 :
3038 : /* Versioning requires at least one misaligned data reference. */
3039 78166 : if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
3040 : do_versioning = false;
3041 5495 : else if (!do_versioning)
3042 540 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
3043 : }
3044 :
3045 : /* If we are trying peeling with versioning but versioning is disabled for
3046 : some reason, peeling should be turned off together. */
3047 302389 : if (try_peeling_with_versioning && !do_versioning)
3048 : do_peeling = false;
3049 :
3050 290363 : if (do_versioning)
3051 : {
3052 : const vec<stmt_vec_info> &may_misalign_stmts
3053 : = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
3054 : stmt_vec_info stmt_info;
3055 :
3056 : /* It can now be assumed that the data references in the statements
3057 : in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
3058 : of the loop being vectorized. */
3059 13485 : FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt_info)
3060 : {
3061 8530 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
3062 8530 : SET_DR_MISALIGNMENT (dr_info,
3063 : vect_dr_misalign_for_aligned_access (dr_info));
3064 8530 : if (dump_enabled_p ())
3065 140 : dump_printf_loc (MSG_NOTE, vect_location,
3066 : "Alignment of access forced using versioning.\n");
3067 : }
3068 :
3069 4955 : if (do_peeling)
3070 : {
3071 : /* This point is reached if peeling and versioning are used together
3072 : to ensure alignment. Update data structures to make sure the loop
3073 : is correctly peeled and a right runtime check is added for loop
3074 : versioning. */
3075 1006 : gcc_assert (try_peeling_with_versioning);
3076 1006 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
3077 1006 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
3078 1006 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = true;
3079 1006 : if (dump_enabled_p ())
3080 9 : dump_printf_loc (MSG_NOTE, vect_location,
3081 : "Both peeling and versioning will be applied.\n");
3082 : }
3083 : else
3084 : {
3085 : /* This point is reached if versioning is used alone. */
3086 3949 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = false;
3087 3949 : if (dump_enabled_p ())
3088 81 : dump_printf_loc (MSG_NOTE, vect_location,
3089 : "Versioning for alignment will be applied.\n");
3090 : }
3091 :
3092 4955 : return opt_result::success ();
3093 : }
3094 :
3095 : /* This point is reached if neither peeling nor versioning is being done. */
3096 297434 : gcc_assert (! (do_peeling || do_versioning));
3097 :
3098 297434 : return opt_result::success ();
3099 640784 : }
3100 :
3101 :
3102 : /* Function vect_analyze_data_refs_alignment
3103 :
3104 : Analyze the alignment of the data-references in the loop. */
3105 :
3106 : void
3107 359218 : vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
3108 : {
3109 359218 : DUMP_VECT_SCOPE ("vect_analyze_data_refs_alignment");
3110 :
3111 359218 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3112 359218 : struct data_reference *dr;
3113 359218 : unsigned int i;
3114 :
3115 359218 : vect_record_base_alignments (loop_vinfo);
3116 1523217 : FOR_EACH_VEC_ELT (datarefs, i, dr)
3117 : {
3118 818861 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
3119 818861 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt))
3120 : {
3121 818861 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt)
3122 1079542 : && DR_GROUP_FIRST_ELEMENT (dr_info->stmt) != dr_info->stmt)
3123 108980 : continue;
3124 :
3125 709881 : vect_compute_data_ref_alignment (loop_vinfo, dr_info,
3126 : STMT_VINFO_VECTYPE (dr_info->stmt));
3127 : }
3128 : }
3129 359218 : }
3130 :
3131 :
3132 : /* Analyze alignment of DRs of stmts in NODE. */
3133 :
3134 : static bool
3135 814777 : vect_slp_analyze_node_alignment (vec_info *vinfo, slp_tree node)
3136 : {
3137 : /* Alignment is maintained in the first element of the group. */
3138 814777 : stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
3139 814777 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (first_stmt_info);
3140 814777 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
3141 814777 : tree vectype = SLP_TREE_VECTYPE (node);
3142 814777 : poly_uint64 vector_alignment
3143 814777 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
3144 : BITS_PER_UNIT);
3145 814777 : if (dr_info->misalignment == DR_MISALIGNMENT_UNINITIALIZED)
3146 773986 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3147 : /* Re-analyze alignment when we're facing a vectorization with a bigger
3148 : alignment requirement. */
3149 40791 : else if (known_lt (dr_info->target_alignment, vector_alignment))
3150 : {
3151 67 : poly_uint64 old_target_alignment = dr_info->target_alignment;
3152 67 : int old_misalignment = dr_info->misalignment;
3153 67 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3154 : /* But keep knowledge about a smaller alignment. */
3155 67 : if (old_misalignment != DR_MISALIGNMENT_UNKNOWN
3156 38 : && dr_info->misalignment == DR_MISALIGNMENT_UNKNOWN)
3157 : {
3158 1 : dr_info->target_alignment = old_target_alignment;
3159 1 : dr_info->misalignment = old_misalignment;
3160 : }
3161 : }
3162 : /* When we ever face unordered target alignments the first one wins in terms
3163 : of analyzing and the other will become unknown in dr_misalignment. */
3164 814777 : return true;
3165 : }
3166 :
3167 : /* Function vect_slp_analyze_instance_alignment
3168 :
3169 : Analyze the alignment of the data-references in the SLP instance.
3170 : Return FALSE if a data reference is found that cannot be vectorized. */
3171 :
3172 : bool
3173 783997 : vect_slp_analyze_instance_alignment (vec_info *vinfo,
3174 : slp_instance instance)
3175 : {
3176 783997 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_alignment");
3177 :
3178 783997 : slp_tree node;
3179 783997 : unsigned i;
3180 941944 : FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, node)
3181 157947 : if (! vect_slp_analyze_node_alignment (vinfo, node))
3182 : return false;
3183 :
3184 783997 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
3185 783997 : && ! vect_slp_analyze_node_alignment
3186 656830 : (vinfo, SLP_INSTANCE_TREE (instance)))
3187 : return false;
3188 :
3189 : return true;
3190 : }
3191 :
3192 :
3193 : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3194 : accesses of legal size, step, etc. Detect gaps, single element
3195 : interleaving, and other special cases. Set grouped access info.
3196 : Collect groups of strided stores for further use in SLP analysis.
3197 : Worker for vect_analyze_group_access. */
3198 :
3199 : static bool
3200 12534314 : vect_analyze_group_access_1 (vec_info *vinfo, dr_vec_info *dr_info)
3201 : {
3202 12534314 : data_reference *dr = dr_info->dr;
3203 12534314 : tree step = DR_STEP (dr);
3204 12534314 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3205 12534314 : HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
3206 12534314 : stmt_vec_info stmt_info = dr_info->stmt;
3207 12534314 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3208 12534314 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3209 12534314 : HOST_WIDE_INT dr_step = -1;
3210 12534314 : HOST_WIDE_INT groupsize, last_accessed_element = 1;
3211 12534314 : bool slp_impossible = false;
3212 :
3213 : /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
3214 : size of the interleaving group (including gaps). */
3215 12534314 : if (tree_fits_shwi_p (step))
3216 : {
3217 12526220 : dr_step = tree_to_shwi (step);
3218 : /* Check that STEP is a multiple of type size. Otherwise there is
3219 : a non-element-sized gap at the end of the group which we
3220 : cannot represent in DR_GROUP_GAP or DR_GROUP_SIZE.
3221 : ??? As we can handle non-constant step fine here we should
3222 : simply remove uses of DR_GROUP_GAP between the last and first
3223 : element and instead rely on DR_STEP. DR_GROUP_SIZE then would
3224 : simply not include that gap. */
3225 12526220 : if ((dr_step % type_size) != 0)
3226 : {
3227 498 : if (dump_enabled_p ())
3228 27 : dump_printf_loc (MSG_NOTE, vect_location,
3229 : "Step %T is not a multiple of the element size"
3230 : " for %T\n",
3231 : step, DR_REF (dr));
3232 498 : return false;
3233 : }
3234 12525722 : groupsize = absu_hwi (dr_step) / type_size;
3235 : }
3236 : else
3237 : groupsize = 0;
3238 :
3239 : /* Not consecutive access is possible only if it is a part of interleaving. */
3240 12533816 : if (!DR_GROUP_FIRST_ELEMENT (stmt_info))
3241 : {
3242 : /* Check if it this DR is a part of interleaving, and is a single
3243 : element of the group that is accessed in the loop. */
3244 :
3245 : /* Gaps are supported only for loads. STEP must be a multiple of the type
3246 : size. */
3247 8482898 : if (DR_IS_READ (dr)
3248 5066823 : && (dr_step % type_size) == 0
3249 : && groupsize > 0
3250 : /* This could be UINT_MAX but as we are generating code in a very
3251 : inefficient way we have to cap earlier.
3252 : See PR91403 for example. */
3253 5066823 : && groupsize <= 4096)
3254 : {
3255 71717 : DR_GROUP_FIRST_ELEMENT (stmt_info) = stmt_info;
3256 71717 : DR_GROUP_SIZE (stmt_info) = groupsize;
3257 71717 : DR_GROUP_GAP (stmt_info) = groupsize - 1;
3258 71717 : if (dump_enabled_p ())
3259 1465 : dump_printf_loc (MSG_NOTE, vect_location,
3260 : "Detected single element interleaving %T"
3261 : " step %T\n",
3262 : DR_REF (dr), step);
3263 :
3264 71717 : return true;
3265 : }
3266 :
3267 8411181 : if (dump_enabled_p ())
3268 3129 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3269 : "not consecutive access %G", stmt_info->stmt);
3270 :
3271 8411181 : if (bb_vinfo)
3272 : {
3273 : /* Mark the statement as unvectorizable. */
3274 8393043 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
3275 8393043 : return true;
3276 : }
3277 :
3278 18138 : if (dump_enabled_p ())
3279 305 : dump_printf_loc (MSG_NOTE, vect_location, "using strided accesses\n");
3280 18138 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3281 18138 : return true;
3282 : }
3283 :
3284 4050918 : if (DR_GROUP_FIRST_ELEMENT (stmt_info) == stmt_info)
3285 : {
3286 : /* First stmt in the interleaving chain. Check the chain. */
3287 1475451 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3288 1475451 : struct data_reference *data_ref = dr;
3289 1475451 : unsigned int count = 1;
3290 1475451 : tree prev_init = DR_INIT (data_ref);
3291 1475451 : HOST_WIDE_INT diff, gaps = 0;
3292 :
3293 : /* By construction, all group members have INTEGER_CST DR_INITs. */
3294 4050927 : while (next)
3295 : {
3296 : /* We never have the same DR multiple times. */
3297 2575538 : gcc_assert (tree_int_cst_compare (DR_INIT (data_ref),
3298 : DR_INIT (STMT_VINFO_DATA_REF (next))) != 0);
3299 :
3300 2575538 : data_ref = STMT_VINFO_DATA_REF (next);
3301 :
3302 : /* All group members have the same STEP by construction. */
3303 2575538 : gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
3304 :
3305 : /* Check that the distance between two accesses is equal to the type
3306 : size. Otherwise, we have gaps. */
3307 2575538 : diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
3308 2575538 : - TREE_INT_CST_LOW (prev_init)) / type_size;
3309 2575538 : if (diff < 1 || diff > UINT_MAX)
3310 : {
3311 : /* For artificial testcases with array accesses with large
3312 : constant indices we can run into overflow issues which
3313 : can end up fooling the groupsize constraint below so
3314 : check the individual gaps (which are represented as
3315 : unsigned int) as well. */
3316 0 : if (dump_enabled_p ())
3317 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3318 : "interleaved access with gap larger "
3319 : "than representable\n");
3320 0 : return false;
3321 : }
3322 2575538 : if (diff != 1)
3323 : {
3324 : /* FORNOW: SLP of accesses with gaps is not supported. */
3325 100852 : slp_impossible = true;
3326 100852 : if (DR_IS_WRITE (data_ref))
3327 : {
3328 62 : if (dump_enabled_p ())
3329 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3330 : "interleaved store with gaps\n");
3331 62 : return false;
3332 : }
3333 :
3334 100790 : gaps += diff - 1;
3335 : }
3336 :
3337 2575476 : last_accessed_element += diff;
3338 :
3339 : /* Store the gap from the previous member of the group. If there is no
3340 : gap in the access, DR_GROUP_GAP is always 1. */
3341 2575476 : DR_GROUP_GAP (next) = diff;
3342 :
3343 2575476 : prev_init = DR_INIT (data_ref);
3344 2575476 : next = DR_GROUP_NEXT_ELEMENT (next);
3345 : /* Count the number of data-refs in the chain. */
3346 2575476 : count++;
3347 : }
3348 :
3349 1475389 : if (groupsize == 0)
3350 1417611 : groupsize = count + gaps;
3351 :
3352 : /* This could be UINT_MAX but as we are generating code in a very
3353 : inefficient way we have to cap earlier. See PR78699 for example. */
3354 1475389 : if (groupsize > 4096)
3355 : {
3356 1 : if (dump_enabled_p ())
3357 1 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3358 : "group is too large\n");
3359 1 : return false;
3360 : }
3361 :
3362 : /* Check that the size of the interleaving is equal to count for stores,
3363 : i.e., that there are no gaps. */
3364 1475388 : if (groupsize != count
3365 104607 : && !DR_IS_READ (dr))
3366 : {
3367 10669 : groupsize = count;
3368 10669 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3369 : }
3370 :
3371 : /* If there is a gap after the last load in the group it is the
3372 : difference between the groupsize and the last accessed
3373 : element.
3374 : When there is no gap, this difference should be 0. */
3375 1475388 : DR_GROUP_GAP (stmt_info) = groupsize - last_accessed_element;
3376 :
3377 1475388 : DR_GROUP_SIZE (stmt_info) = groupsize;
3378 1475388 : if (dump_enabled_p ())
3379 : {
3380 7917 : dump_printf_loc (MSG_NOTE, vect_location,
3381 : "Detected interleaving ");
3382 7917 : if (DR_IS_READ (dr))
3383 4263 : dump_printf (MSG_NOTE, "load ");
3384 3654 : else if (STMT_VINFO_STRIDED_P (stmt_info))
3385 490 : dump_printf (MSG_NOTE, "strided store ");
3386 : else
3387 3164 : dump_printf (MSG_NOTE, "store ");
3388 7917 : dump_printf (MSG_NOTE, "of size %u\n",
3389 : (unsigned)groupsize);
3390 7917 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", stmt_info->stmt);
3391 7917 : next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3392 38772 : while (next)
3393 : {
3394 30855 : if (DR_GROUP_GAP (next) != 1)
3395 271 : dump_printf_loc (MSG_NOTE, vect_location,
3396 : "\t<gap of %d elements>\n",
3397 271 : DR_GROUP_GAP (next) - 1);
3398 30855 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", next->stmt);
3399 30855 : next = DR_GROUP_NEXT_ELEMENT (next);
3400 : }
3401 7917 : if (DR_GROUP_GAP (stmt_info) != 0)
3402 383 : dump_printf_loc (MSG_NOTE, vect_location,
3403 : "\t<gap of %d elements>\n",
3404 383 : DR_GROUP_GAP (stmt_info));
3405 : }
3406 :
3407 : /* SLP: create an SLP data structure for every interleaving group of
3408 : stores for further analysis in vect_analyse_slp. */
3409 1475388 : if (DR_IS_WRITE (dr) && !slp_impossible)
3410 : {
3411 907920 : if (loop_vinfo)
3412 23862 : LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt_info);
3413 907920 : if (bb_vinfo)
3414 884058 : BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt_info);
3415 : }
3416 : }
3417 :
3418 : return true;
3419 : }
3420 :
3421 : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3422 : accesses of legal size, step, etc. Detect gaps, single element
3423 : interleaving, and other special cases. Set grouped access info.
3424 : Collect groups of strided stores for further use in SLP analysis. */
3425 :
3426 : static bool
3427 12534314 : vect_analyze_group_access (vec_info *vinfo, dr_vec_info *dr_info)
3428 : {
3429 12534314 : if (!vect_analyze_group_access_1 (vinfo, dr_info))
3430 : {
3431 : /* Dissolve the group if present. */
3432 561 : stmt_vec_info stmt_info = DR_GROUP_FIRST_ELEMENT (dr_info->stmt);
3433 792 : while (stmt_info)
3434 : {
3435 231 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3436 231 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3437 231 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3438 231 : stmt_info = next;
3439 : }
3440 : return false;
3441 : }
3442 : return true;
3443 : }
3444 :
3445 : /* Analyze the access pattern of the data-reference DR_INFO.
3446 : In case of non-consecutive accesses call vect_analyze_group_access() to
3447 : analyze groups of accesses. */
3448 :
3449 : static bool
3450 13167286 : vect_analyze_data_ref_access (vec_info *vinfo, dr_vec_info *dr_info)
3451 : {
3452 13167286 : data_reference *dr = dr_info->dr;
3453 13167286 : tree step = DR_STEP (dr);
3454 13167286 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3455 13167286 : stmt_vec_info stmt_info = dr_info->stmt;
3456 13167286 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3457 13167286 : class loop *loop = NULL;
3458 :
3459 13167286 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
3460 : return true;
3461 :
3462 13060625 : if (loop_vinfo)
3463 793907 : loop = LOOP_VINFO_LOOP (loop_vinfo);
3464 :
3465 13060625 : if (loop_vinfo && !step)
3466 : {
3467 0 : if (dump_enabled_p ())
3468 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3469 : "bad data-ref access in loop\n");
3470 0 : return false;
3471 : }
3472 :
3473 : /* Allow loads with zero step in inner-loop vectorization. */
3474 13060625 : if (loop_vinfo && integer_zerop (step))
3475 : {
3476 13716 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3477 13716 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3478 13716 : if (!nested_in_vect_loop_p (loop, stmt_info))
3479 13455 : return DR_IS_READ (dr);
3480 : /* Allow references with zero step for outer loops marked
3481 : with pragma omp simd only - it guarantees absence of
3482 : loop-carried dependencies between inner loop iterations. */
3483 261 : if (loop->safelen < 2)
3484 : {
3485 225 : if (dump_enabled_p ())
3486 6 : dump_printf_loc (MSG_NOTE, vect_location,
3487 : "zero step in inner loop of nest\n");
3488 225 : return false;
3489 : }
3490 : }
3491 :
3492 13046909 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3493 : {
3494 : /* Interleaved accesses are not yet supported within outer-loop
3495 : vectorization for references in the inner-loop. */
3496 5661 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3497 5661 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3498 :
3499 : /* For the rest of the analysis we use the outer-loop step. */
3500 5661 : step = STMT_VINFO_DR_STEP (stmt_info);
3501 5661 : if (integer_zerop (step))
3502 : {
3503 1232 : if (dump_enabled_p ())
3504 237 : dump_printf_loc (MSG_NOTE, vect_location,
3505 : "zero step in outer loop.\n");
3506 1232 : return DR_IS_READ (dr);
3507 : }
3508 : }
3509 :
3510 : /* Consecutive? */
3511 13045713 : if (TREE_CODE (step) == INTEGER_CST)
3512 : {
3513 13008959 : HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
3514 13008959 : if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
3515 13008959 : || (dr_step < 0
3516 27214 : && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
3517 : {
3518 : /* Mark that it is not interleaving. */
3519 479864 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3520 479864 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3521 479864 : return true;
3522 : }
3523 : }
3524 :
3525 12565849 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3526 : {
3527 3334 : if (dump_enabled_p ())
3528 163 : dump_printf_loc (MSG_NOTE, vect_location,
3529 : "grouped access in outer loop.\n");
3530 3334 : return false;
3531 : }
3532 :
3533 :
3534 : /* Assume this is a DR handled by non-constant strided load case. */
3535 12562515 : if (TREE_CODE (step) != INTEGER_CST)
3536 36295 : return (STMT_VINFO_STRIDED_P (stmt_info)
3537 36295 : && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
3538 8094 : || vect_analyze_group_access (vinfo, dr_info)));
3539 :
3540 : /* Not consecutive access - check if it's a part of interleaving group. */
3541 12526220 : return vect_analyze_group_access (vinfo, dr_info);
3542 : }
3543 :
3544 : /* Compare two data-references DRA and DRB to group them into chunks
3545 : suitable for grouping. */
3546 :
3547 : static int
3548 347413857 : dr_group_sort_cmp (const void *dra_, const void *drb_)
3549 : {
3550 347413857 : dr_vec_info *dra_info = *(dr_vec_info **)const_cast<void *>(dra_);
3551 347413857 : dr_vec_info *drb_info = *(dr_vec_info **)const_cast<void *>(drb_);
3552 347413857 : data_reference_p dra = dra_info->dr;
3553 347413857 : data_reference_p drb = drb_info->dr;
3554 347413857 : int cmp;
3555 :
3556 : /* Stabilize sort. */
3557 347413857 : if (dra == drb)
3558 : return 0;
3559 :
3560 : /* Different group IDs lead never belong to the same group. */
3561 347413857 : if (dra_info->group != drb_info->group)
3562 381749341 : return dra_info->group < drb_info->group ? -1 : 1;
3563 :
3564 : /* Ordering of DRs according to base. */
3565 96254918 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3566 : DR_BASE_ADDRESS (drb));
3567 96254918 : if (cmp != 0)
3568 : return cmp;
3569 :
3570 : /* And according to DR_OFFSET. */
3571 52124775 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
3572 52124775 : if (cmp != 0)
3573 : return cmp;
3574 :
3575 : /* Put reads before writes. */
3576 51759730 : if (DR_IS_READ (dra) != DR_IS_READ (drb))
3577 4148772 : return DR_IS_READ (dra) ? -1 : 1;
3578 :
3579 : /* Then sort after access size. */
3580 48936948 : cmp = data_ref_compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
3581 48936948 : TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
3582 48936948 : if (cmp != 0)
3583 : return cmp;
3584 :
3585 : /* And after step. */
3586 42240058 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
3587 42240058 : if (cmp != 0)
3588 : return cmp;
3589 :
3590 : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
3591 42233914 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
3592 42233914 : if (cmp == 0)
3593 503675 : return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
3594 : return cmp;
3595 : }
3596 :
3597 : /* If OP is the result of a conversion, return the unconverted value,
3598 : otherwise return null. */
3599 :
3600 : static tree
3601 359 : strip_conversion (tree op)
3602 : {
3603 359 : if (TREE_CODE (op) != SSA_NAME)
3604 : return NULL_TREE;
3605 359 : gimple *stmt = SSA_NAME_DEF_STMT (op);
3606 359 : if (!is_gimple_assign (stmt)
3607 359 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
3608 : return NULL_TREE;
3609 182 : return gimple_assign_rhs1 (stmt);
3610 : }
3611 :
3612 : /* Return true if vectorizable_* routines can handle statements STMT1_INFO
3613 : and STMT2_INFO being in a single group. When ALLOW_SLP_P, masked loads can
3614 : be grouped in SLP mode. */
3615 :
3616 : static bool
3617 6912197 : can_group_stmts_p (stmt_vec_info stmt1_info, stmt_vec_info stmt2_info,
3618 : bool allow_slp_p)
3619 : {
3620 6912197 : if (gimple_assign_single_p (stmt1_info->stmt))
3621 6910594 : return gimple_assign_single_p (stmt2_info->stmt);
3622 :
3623 1603 : gcall *call1 = dyn_cast <gcall *> (stmt1_info->stmt);
3624 1603 : if (call1 && gimple_call_internal_p (call1))
3625 : {
3626 : /* Check for two masked loads or two masked stores. */
3627 1815 : gcall *call2 = dyn_cast <gcall *> (stmt2_info->stmt);
3628 1592 : if (!call2 || !gimple_call_internal_p (call2))
3629 : return false;
3630 1592 : internal_fn ifn = gimple_call_internal_fn (call1);
3631 1592 : if (ifn != IFN_MASK_LOAD && ifn != IFN_MASK_STORE)
3632 : return false;
3633 1592 : if (ifn != gimple_call_internal_fn (call2))
3634 : return false;
3635 :
3636 : /* Check that the masks are the same. Cope with casts of masks,
3637 : like those created by build_mask_conversion. */
3638 1592 : tree mask1 = gimple_call_arg (call1, 2);
3639 1592 : tree mask2 = gimple_call_arg (call2, 2);
3640 1592 : if (!operand_equal_p (mask1, mask2, 0) && !allow_slp_p)
3641 : {
3642 268 : mask1 = strip_conversion (mask1);
3643 268 : if (!mask1)
3644 : return false;
3645 91 : mask2 = strip_conversion (mask2);
3646 91 : if (!mask2)
3647 : return false;
3648 91 : if (!operand_equal_p (mask1, mask2, 0))
3649 : return false;
3650 : }
3651 1380 : return true;
3652 : }
3653 :
3654 : return false;
3655 : }
3656 :
3657 : /* Function vect_analyze_data_ref_accesses.
3658 :
3659 : Analyze the access pattern of all the data references in the loop.
3660 :
3661 : FORNOW: the only access pattern that is considered vectorizable is a
3662 : simple step 1 (consecutive) access.
3663 :
3664 : FORNOW: handle only arrays and pointer accesses. */
3665 :
3666 : opt_result
3667 2561479 : vect_analyze_data_ref_accesses (vec_info *vinfo,
3668 : vec<int> *dataref_groups)
3669 : {
3670 2561479 : unsigned int i;
3671 2561479 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
3672 :
3673 2561479 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_accesses");
3674 :
3675 2561479 : if (datarefs.is_empty ())
3676 1049311 : return opt_result::success ();
3677 :
3678 : /* Sort the array of datarefs to make building the interleaving chains
3679 : linear. Don't modify the original vector's order, it is needed for
3680 : determining what dependencies are reversed. */
3681 1512168 : vec<dr_vec_info *> datarefs_copy;
3682 1512168 : datarefs_copy.create (datarefs.length ());
3683 16446878 : for (unsigned i = 0; i < datarefs.length (); i++)
3684 : {
3685 14934710 : dr_vec_info *dr_info = vinfo->lookup_dr (datarefs[i]);
3686 : /* If the caller computed DR grouping use that, otherwise group by
3687 : basic blocks. */
3688 14934710 : if (dataref_groups)
3689 14020275 : dr_info->group = (*dataref_groups)[i];
3690 : else
3691 914435 : dr_info->group = gimple_bb (DR_STMT (datarefs[i]))->index;
3692 14934710 : datarefs_copy.quick_push (dr_info);
3693 : }
3694 1512168 : datarefs_copy.qsort (dr_group_sort_cmp);
3695 1512168 : hash_set<stmt_vec_info> to_fixup;
3696 :
3697 : /* Build the interleaving chains. */
3698 14071036 : for (i = 0; i < datarefs_copy.length () - 1;)
3699 : {
3700 11046700 : dr_vec_info *dr_info_a = datarefs_copy[i];
3701 11046700 : data_reference_p dra = dr_info_a->dr;
3702 11046700 : int dra_group_id = dr_info_a->group;
3703 11046700 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
3704 11046700 : stmt_vec_info lastinfo = NULL;
3705 11046700 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
3706 9429341 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a))
3707 : {
3708 1686207 : ++i;
3709 1686207 : continue;
3710 : }
3711 24529165 : for (i = i + 1; i < datarefs_copy.length (); ++i)
3712 : {
3713 11736335 : dr_vec_info *dr_info_b = datarefs_copy[i];
3714 11736335 : data_reference_p drb = dr_info_b->dr;
3715 11736335 : int drb_group_id = dr_info_b->group;
3716 11736335 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
3717 11736335 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_b)
3718 11425909 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
3719 : break;
3720 :
3721 : /* ??? Imperfect sorting (non-compatible types, non-modulo
3722 : accesses, same accesses) can lead to a group to be artificially
3723 : split here as we don't just skip over those. If it really
3724 : matters we can push those to a worklist and re-iterate
3725 : over them. The we can just skip ahead to the next DR here. */
3726 :
3727 : /* DRs in a different DR group should not be put into the same
3728 : interleaving group. */
3729 11422304 : if (dra_group_id != drb_group_id)
3730 : break;
3731 :
3732 : /* Check that the data-refs have same first location (except init)
3733 : and they are both either store or load (not load and store,
3734 : not masked loads or stores). */
3735 7178962 : if (DR_IS_READ (dra) != DR_IS_READ (drb)
3736 5908046 : || data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3737 : DR_BASE_ADDRESS (drb)) != 0
3738 4323780 : || data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb)) != 0
3739 11481732 : || !can_group_stmts_p (stmtinfo_a, stmtinfo_b, true))
3740 : break;
3741 :
3742 : /* Check that the data-refs have the same constant size. */
3743 4302753 : tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
3744 4302753 : tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
3745 4302753 : if (!tree_fits_uhwi_p (sza)
3746 4302753 : || !tree_fits_uhwi_p (szb)
3747 8605506 : || !tree_int_cst_equal (sza, szb))
3748 : break;
3749 :
3750 : /* Check that the data-refs have the same step. */
3751 3955738 : if (data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb)) != 0)
3752 : break;
3753 :
3754 : /* Check the types are compatible.
3755 : ??? We don't distinguish this during sorting. */
3756 3955127 : if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
3757 3955127 : TREE_TYPE (DR_REF (drb))))
3758 : break;
3759 :
3760 : /* Check that the DR_INITs are compile-time constants. */
3761 2817796 : if (!tree_fits_shwi_p (DR_INIT (dra))
3762 2817796 : || !tree_fits_shwi_p (DR_INIT (drb)))
3763 : break;
3764 :
3765 : /* Different .GOMP_SIMD_LANE calls still give the same lane,
3766 : just hold extra information. */
3767 2817796 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_a)
3768 1240 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_b)
3769 2819036 : && data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb)) == 0)
3770 : break;
3771 :
3772 : /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
3773 2816556 : HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
3774 2816556 : HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
3775 2816556 : HOST_WIDE_INT init_prev
3776 2816556 : = TREE_INT_CST_LOW (DR_INIT (datarefs_copy[i-1]->dr));
3777 2816556 : gcc_assert (init_a <= init_b
3778 : && init_a <= init_prev
3779 : && init_prev <= init_b);
3780 :
3781 : /* Do not place the same access in the interleaving chain twice. */
3782 2816556 : if (init_b == init_prev)
3783 : {
3784 30180 : gcc_assert (gimple_uid (DR_STMT (datarefs_copy[i-1]->dr))
3785 : < gimple_uid (DR_STMT (drb)));
3786 : /* Simply link in duplicates and fix up the chain below. */
3787 : }
3788 : else
3789 : {
3790 : /* If init_b == init_a + the size of the type * k, we have an
3791 : interleaving, and DRA is accessed before DRB. */
3792 2786376 : unsigned HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
3793 2786376 : if (type_size_a == 0
3794 2786376 : || (((unsigned HOST_WIDE_INT)init_b - init_a)
3795 2786376 : % type_size_a != 0))
3796 : break;
3797 :
3798 : /* If we have a store, the accesses are adjacent. This splits
3799 : groups into chunks we support (we don't support vectorization
3800 : of stores with gaps). */
3801 2784568 : if (!DR_IS_READ (dra)
3802 1831976 : && (((unsigned HOST_WIDE_INT)init_b - init_prev)
3803 : != type_size_a))
3804 : break;
3805 :
3806 : /* For datarefs with big gap, it's better to split them into different
3807 : groups.
3808 : .i.e a[0], a[1], a[2], .. a[7], a[100], a[101],..., a[107] */
3809 2607358 : if ((unsigned HOST_WIDE_INT)(init_b - init_prev)
3810 : > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
3811 : break;
3812 :
3813 : /* If the step (if not zero or non-constant) is smaller than the
3814 : difference between data-refs' inits this splits groups into
3815 : suitable sizes. */
3816 2597319 : if (tree_fits_shwi_p (DR_STEP (dra)))
3817 : {
3818 2592113 : unsigned HOST_WIDE_INT step
3819 2592113 : = absu_hwi (tree_to_shwi (DR_STEP (dra)));
3820 2592113 : if (step != 0
3821 132675 : && step <= ((unsigned HOST_WIDE_INT)init_b - init_a))
3822 : break;
3823 : }
3824 : }
3825 :
3826 2609804 : if (dump_enabled_p ())
3827 31683 : dump_printf_loc (MSG_NOTE, vect_location,
3828 31683 : DR_IS_READ (dra)
3829 : ? "Detected interleaving load %T and %T\n"
3830 : : "Detected interleaving store %T and %T\n",
3831 : DR_REF (dra), DR_REF (drb));
3832 :
3833 : /* Link the found element into the group list. */
3834 2609804 : if (!DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3835 : {
3836 1452632 : DR_GROUP_FIRST_ELEMENT (stmtinfo_a) = stmtinfo_a;
3837 1452632 : lastinfo = stmtinfo_a;
3838 : }
3839 2609804 : DR_GROUP_FIRST_ELEMENT (stmtinfo_b) = stmtinfo_a;
3840 2609804 : DR_GROUP_NEXT_ELEMENT (lastinfo) = stmtinfo_b;
3841 2609804 : lastinfo = stmtinfo_b;
3842 :
3843 2609804 : if (! STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3844 : {
3845 2609427 : STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a)
3846 2609427 : = !can_group_stmts_p (stmtinfo_a, stmtinfo_b, false);
3847 :
3848 2609427 : if (dump_enabled_p () && STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3849 126 : dump_printf_loc (MSG_NOTE, vect_location,
3850 : "Load suitable for SLP vectorization only.\n");
3851 : }
3852 :
3853 2609804 : if (init_b == init_prev
3854 30180 : && !to_fixup.add (DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3855 2627588 : && dump_enabled_p ())
3856 218 : dump_printf_loc (MSG_NOTE, vect_location,
3857 : "Queuing group with duplicate access for fixup\n");
3858 : }
3859 : }
3860 :
3861 : /* Fixup groups with duplicate entries by splitting it. */
3862 1557326 : while (1)
3863 : {
3864 1557326 : hash_set<stmt_vec_info>::iterator it = to_fixup.begin ();
3865 1557326 : if (!(it != to_fixup.end ()))
3866 : break;
3867 45158 : stmt_vec_info grp = *it;
3868 45158 : to_fixup.remove (grp);
3869 :
3870 : /* Find the earliest duplicate group member. */
3871 45158 : unsigned first_duplicate = -1u;
3872 45158 : stmt_vec_info next, g = grp;
3873 276975 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3874 : {
3875 186659 : if (tree_int_cst_equal (DR_INIT (STMT_VINFO_DR_INFO (next)->dr),
3876 186659 : DR_INIT (STMT_VINFO_DR_INFO (g)->dr))
3877 186659 : && gimple_uid (STMT_VINFO_STMT (next)) < first_duplicate)
3878 : first_duplicate = gimple_uid (STMT_VINFO_STMT (next));
3879 : g = next;
3880 : }
3881 45158 : if (first_duplicate == -1U)
3882 17784 : continue;
3883 :
3884 : /* Then move all stmts after the first duplicate to a new group.
3885 : Note this is a heuristic but one with the property that *it
3886 : is fixed up completely. */
3887 27374 : g = grp;
3888 27374 : stmt_vec_info newgroup = NULL, ng = grp;
3889 239182 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3890 : {
3891 184434 : if (gimple_uid (STMT_VINFO_STMT (next)) >= first_duplicate)
3892 : {
3893 178753 : DR_GROUP_NEXT_ELEMENT (g) = DR_GROUP_NEXT_ELEMENT (next);
3894 178753 : if (!newgroup)
3895 : {
3896 27374 : newgroup = next;
3897 27374 : STMT_VINFO_SLP_VECT_ONLY (newgroup)
3898 27374 : = STMT_VINFO_SLP_VECT_ONLY (grp);
3899 : }
3900 : else
3901 151379 : DR_GROUP_NEXT_ELEMENT (ng) = next;
3902 178753 : ng = next;
3903 178753 : DR_GROUP_FIRST_ELEMENT (ng) = newgroup;
3904 : }
3905 : else
3906 : g = DR_GROUP_NEXT_ELEMENT (g);
3907 : }
3908 27374 : DR_GROUP_NEXT_ELEMENT (ng) = NULL;
3909 :
3910 : /* Fixup the new group which still may contain duplicates. */
3911 27374 : to_fixup.add (newgroup);
3912 : }
3913 :
3914 1512168 : dr_vec_info *dr_info;
3915 16425082 : FOR_EACH_VEC_ELT (datarefs_copy, i, dr_info)
3916 : {
3917 14920843 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt)
3918 14920843 : && !vect_analyze_data_ref_access (vinfo, dr_info))
3919 : {
3920 7983 : if (dump_enabled_p ())
3921 291 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3922 : "not vectorized: complicated access pattern.\n");
3923 :
3924 7983 : if (is_a <bb_vec_info> (vinfo))
3925 : {
3926 : /* Mark the statement as not vectorizable. */
3927 54 : STMT_VINFO_VECTORIZABLE (dr_info->stmt) = false;
3928 54 : continue;
3929 : }
3930 : else
3931 : {
3932 7929 : datarefs_copy.release ();
3933 7929 : return opt_result::failure_at (dr_info->stmt->stmt,
3934 : "not vectorized:"
3935 : " complicated access pattern.\n");
3936 : }
3937 : }
3938 : }
3939 :
3940 1504239 : datarefs_copy.release ();
3941 1504239 : return opt_result::success ();
3942 1512168 : }
3943 :
3944 : /* Function vect_vfa_segment_size.
3945 :
3946 : Input:
3947 : DR_INFO: The data reference.
3948 : LENGTH_FACTOR: segment length to consider.
3949 :
3950 : Return a value suitable for the dr_with_seg_len::seg_len field.
3951 : This is the "distance travelled" by the pointer from the first
3952 : iteration in the segment to the last. Note that it does not include
3953 : the size of the access; in effect it only describes the first byte. */
3954 :
3955 : static tree
3956 123232 : vect_vfa_segment_size (dr_vec_info *dr_info, tree length_factor)
3957 : {
3958 123232 : length_factor = size_binop (MINUS_EXPR,
3959 : fold_convert (sizetype, length_factor),
3960 : size_one_node);
3961 123232 : return size_binop (MULT_EXPR, fold_convert (sizetype, DR_STEP (dr_info->dr)),
3962 : length_factor);
3963 : }
3964 :
3965 : /* Return a value that, when added to abs (vect_vfa_segment_size (DR_INFO)),
3966 : gives the worst-case number of bytes covered by the segment. */
3967 :
3968 : static unsigned HOST_WIDE_INT
3969 123714 : vect_vfa_access_size (vec_info *vinfo, dr_vec_info *dr_info)
3970 : {
3971 123714 : stmt_vec_info stmt_vinfo = dr_info->stmt;
3972 123714 : tree ref_type = TREE_TYPE (DR_REF (dr_info->dr));
3973 123714 : unsigned HOST_WIDE_INT ref_size = tree_to_uhwi (TYPE_SIZE_UNIT (ref_type));
3974 123714 : unsigned HOST_WIDE_INT access_size = ref_size;
3975 123714 : if (DR_GROUP_FIRST_ELEMENT (stmt_vinfo))
3976 : {
3977 38363 : gcc_assert (DR_GROUP_FIRST_ELEMENT (stmt_vinfo) == stmt_vinfo);
3978 38363 : access_size *= DR_GROUP_SIZE (stmt_vinfo) - DR_GROUP_GAP (stmt_vinfo);
3979 : }
3980 123714 : tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
3981 123714 : int misalignment;
3982 247428 : if (((misalignment = dr_misalignment (dr_info, vectype)), true)
3983 123714 : && (vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment)
3984 : == dr_explicit_realign_optimized))
3985 : {
3986 : /* We might access a full vector's worth. */
3987 0 : access_size += tree_to_uhwi (TYPE_SIZE_UNIT (vectype)) - ref_size;
3988 : }
3989 123714 : return access_size;
3990 : }
3991 :
3992 : /* Get the minimum alignment for all the scalar accesses that DR_INFO
3993 : describes. */
3994 :
3995 : static unsigned int
3996 123714 : vect_vfa_align (dr_vec_info *dr_info)
3997 : {
3998 0 : return dr_alignment (dr_info->dr);
3999 : }
4000 :
4001 : /* Function vect_no_alias_p.
4002 :
4003 : Given data references A and B with equal base and offset, see whether
4004 : the alias relation can be decided at compilation time. Return 1 if
4005 : it can and the references alias, 0 if it can and the references do
4006 : not alias, and -1 if we cannot decide at compile time. SEGMENT_LENGTH_A,
4007 : SEGMENT_LENGTH_B, ACCESS_SIZE_A and ACCESS_SIZE_B are the equivalent
4008 : of dr_with_seg_len::{seg_len,access_size} for A and B. */
4009 :
4010 : static int
4011 4195 : vect_compile_time_alias (dr_vec_info *a, dr_vec_info *b,
4012 : tree segment_length_a, tree segment_length_b,
4013 : unsigned HOST_WIDE_INT access_size_a,
4014 : unsigned HOST_WIDE_INT access_size_b)
4015 : {
4016 4195 : poly_offset_int offset_a = wi::to_poly_offset (DR_INIT (a->dr));
4017 4195 : poly_offset_int offset_b = wi::to_poly_offset (DR_INIT (b->dr));
4018 4195 : poly_uint64 const_length_a;
4019 4195 : poly_uint64 const_length_b;
4020 :
4021 : /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
4022 : bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
4023 : [a, a+12) */
4024 4195 : if (tree_int_cst_compare (DR_STEP (a->dr), size_zero_node) < 0)
4025 : {
4026 243 : const_length_a = (-wi::to_poly_wide (segment_length_a)).force_uhwi ();
4027 243 : offset_a -= const_length_a;
4028 : }
4029 : else
4030 3952 : const_length_a = tree_to_poly_uint64 (segment_length_a);
4031 4195 : if (tree_int_cst_compare (DR_STEP (b->dr), size_zero_node) < 0)
4032 : {
4033 395 : const_length_b = (-wi::to_poly_wide (segment_length_b)).force_uhwi ();
4034 395 : offset_b -= const_length_b;
4035 : }
4036 : else
4037 3800 : const_length_b = tree_to_poly_uint64 (segment_length_b);
4038 :
4039 4195 : const_length_a += access_size_a;
4040 4195 : const_length_b += access_size_b;
4041 :
4042 4195 : if (ranges_known_overlap_p (offset_a, const_length_a,
4043 : offset_b, const_length_b))
4044 : return 1;
4045 :
4046 459 : if (!ranges_maybe_overlap_p (offset_a, const_length_a,
4047 : offset_b, const_length_b))
4048 459 : return 0;
4049 :
4050 : return -1;
4051 : }
4052 :
4053 : /* Return true if the minimum nonzero dependence distance for loop LOOP_DEPTH
4054 : in DDR is >= VF. */
4055 :
4056 : static bool
4057 72252 : dependence_distance_ge_vf (data_dependence_relation *ddr,
4058 : unsigned int loop_depth, poly_uint64 vf)
4059 : {
4060 72252 : if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE
4061 77072 : || DDR_NUM_DIST_VECTS (ddr) == 0)
4062 : return false;
4063 :
4064 : /* If the dependence is exact, we should have limited the VF instead. */
4065 4851 : gcc_checking_assert (DDR_COULD_BE_INDEPENDENT_P (ddr));
4066 :
4067 : unsigned int i;
4068 : lambda_vector dist_v;
4069 9729 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
4070 : {
4071 9698 : HOST_WIDE_INT dist = dist_v[loop_depth];
4072 9698 : if (dist != 0
4073 4851 : && !(dist > 0 && DDR_REVERSED_P (ddr))
4074 14549 : && maybe_lt ((unsigned HOST_WIDE_INT) abs_hwi (dist), vf))
4075 : return false;
4076 : }
4077 :
4078 31 : if (dump_enabled_p ())
4079 2 : dump_printf_loc (MSG_NOTE, vect_location,
4080 : "dependence distance between %T and %T is >= VF\n",
4081 2 : DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
4082 :
4083 : return true;
4084 : }
4085 :
4086 : /* Dump LOWER_BOUND using flags DUMP_KIND. Dumps are known to be enabled. */
4087 :
4088 : static void
4089 437 : dump_lower_bound (dump_flags_t dump_kind, const vec_lower_bound &lower_bound)
4090 : {
4091 437 : dump_printf (dump_kind, "%s (%T) >= ",
4092 437 : lower_bound.unsigned_p ? "unsigned" : "abs",
4093 437 : lower_bound.expr);
4094 437 : dump_dec (dump_kind, lower_bound.min_value);
4095 437 : }
4096 :
4097 : /* Record that the vectorized loop requires the vec_lower_bound described
4098 : by EXPR, UNSIGNED_P and MIN_VALUE. */
4099 :
4100 : static void
4101 6441 : vect_check_lower_bound (loop_vec_info loop_vinfo, tree expr, bool unsigned_p,
4102 : poly_uint64 min_value)
4103 : {
4104 6441 : vec<vec_lower_bound> &lower_bounds
4105 : = LOOP_VINFO_LOWER_BOUNDS (loop_vinfo);
4106 7413 : for (unsigned int i = 0; i < lower_bounds.length (); ++i)
4107 5831 : if (operand_equal_p (lower_bounds[i].expr, expr, 0))
4108 : {
4109 4859 : unsigned_p &= lower_bounds[i].unsigned_p;
4110 4859 : min_value = upper_bound (lower_bounds[i].min_value, min_value);
4111 4859 : if (lower_bounds[i].unsigned_p != unsigned_p
4112 4859 : || maybe_lt (lower_bounds[i].min_value, min_value))
4113 : {
4114 782 : lower_bounds[i].unsigned_p = unsigned_p;
4115 782 : lower_bounds[i].min_value = min_value;
4116 782 : if (dump_enabled_p ())
4117 : {
4118 250 : dump_printf_loc (MSG_NOTE, vect_location,
4119 : "updating run-time check to ");
4120 250 : dump_lower_bound (MSG_NOTE, lower_bounds[i]);
4121 250 : dump_printf (MSG_NOTE, "\n");
4122 : }
4123 : }
4124 4859 : return;
4125 : }
4126 :
4127 1582 : vec_lower_bound lower_bound (expr, unsigned_p, min_value);
4128 1582 : if (dump_enabled_p ())
4129 : {
4130 187 : dump_printf_loc (MSG_NOTE, vect_location, "need a run-time check that ");
4131 187 : dump_lower_bound (MSG_NOTE, lower_bound);
4132 187 : dump_printf (MSG_NOTE, "\n");
4133 : }
4134 1582 : LOOP_VINFO_LOWER_BOUNDS (loop_vinfo).safe_push (lower_bound);
4135 : }
4136 :
4137 : /* Return true if it's unlikely that the step of the vectorized form of DR_INFO
4138 : will span fewer than GAP bytes. */
4139 :
4140 : static bool
4141 5271 : vect_small_gap_p (loop_vec_info loop_vinfo, dr_vec_info *dr_info,
4142 : poly_int64 gap)
4143 : {
4144 5271 : stmt_vec_info stmt_info = dr_info->stmt;
4145 5271 : HOST_WIDE_INT count
4146 5271 : = estimated_poly_value (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
4147 5271 : if (DR_GROUP_FIRST_ELEMENT (stmt_info))
4148 4511 : count *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
4149 5271 : return (estimated_poly_value (gap)
4150 5271 : <= count * vect_get_scalar_dr_size (dr_info));
4151 : }
4152 :
4153 : /* Return true if we know that there is no alias between DR_INFO_A and
4154 : DR_INFO_B when abs (DR_STEP (DR_INFO_A->dr)) >= N for some N.
4155 : When returning true, set *LOWER_BOUND_OUT to this N. */
4156 :
4157 : static bool
4158 18449 : vectorizable_with_step_bound_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b,
4159 : poly_uint64 *lower_bound_out)
4160 : {
4161 : /* Check that there is a constant gap of known sign between DR_A
4162 : and DR_B. */
4163 18449 : data_reference *dr_a = dr_info_a->dr;
4164 18449 : data_reference *dr_b = dr_info_b->dr;
4165 18449 : poly_int64 init_a, init_b;
4166 18449 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b), 0)
4167 8055 : || !operand_equal_p (DR_OFFSET (dr_a), DR_OFFSET (dr_b), 0)
4168 7369 : || !operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0)
4169 7359 : || !poly_int_tree_p (DR_INIT (dr_a), &init_a)
4170 7359 : || !poly_int_tree_p (DR_INIT (dr_b), &init_b)
4171 18449 : || !ordered_p (init_a, init_b))
4172 11090 : return false;
4173 :
4174 : /* Sort DR_A and DR_B by the address they access. */
4175 7359 : if (maybe_lt (init_b, init_a))
4176 : {
4177 116 : std::swap (init_a, init_b);
4178 116 : std::swap (dr_info_a, dr_info_b);
4179 116 : std::swap (dr_a, dr_b);
4180 : }
4181 :
4182 : /* If the two accesses could be dependent within a scalar iteration,
4183 : make sure that we'd retain their order. */
4184 7359 : if (maybe_gt (init_a + vect_get_scalar_dr_size (dr_info_a), init_b)
4185 7359 : && !vect_preserves_scalar_order_p (dr_info_a, dr_info_b))
4186 : return false;
4187 :
4188 : /* There is no alias if abs (DR_STEP) is greater than or equal to
4189 : the bytes spanned by the combination of the two accesses. */
4190 7359 : *lower_bound_out = init_b + vect_get_scalar_dr_size (dr_info_b) - init_a;
4191 7359 : return true;
4192 : }
4193 :
4194 : /* Function vect_prune_runtime_alias_test_list.
4195 :
4196 : Prune a list of ddrs to be tested at run-time by versioning for alias.
4197 : Merge several alias checks into one if possible.
4198 : Return FALSE if resulting list of ddrs is longer then allowed by
4199 : PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
4200 :
4201 : opt_result
4202 359218 : vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
4203 : {
4204 359218 : typedef pair_hash <tree_operand_hash, tree_operand_hash> tree_pair_hash;
4205 359218 : hash_set <tree_pair_hash> compared_objects;
4206 :
4207 359218 : const vec<ddr_p> &may_alias_ddrs = LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
4208 359218 : vec<dr_with_seg_len_pair_t> &comp_alias_ddrs
4209 : = LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
4210 359218 : const vec<vec_object_pair> &check_unequal_addrs
4211 : = LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo);
4212 359218 : poly_uint64 vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4213 359218 : tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
4214 :
4215 359218 : ddr_p ddr;
4216 359218 : unsigned int i;
4217 359218 : tree length_factor;
4218 :
4219 359218 : DUMP_VECT_SCOPE ("vect_prune_runtime_alias_test_list");
4220 :
4221 : /* Step values are irrelevant for aliasing if the number of vector
4222 : iterations is equal to the number of scalar iterations (which can
4223 : happen for fully-SLP loops). */
4224 359218 : bool vf_one_p = known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U);
4225 :
4226 359218 : if (!vf_one_p)
4227 : {
4228 : /* Convert the checks for nonzero steps into bound tests. */
4229 : tree value;
4230 356855 : FOR_EACH_VEC_ELT (LOOP_VINFO_CHECK_NONZERO (loop_vinfo), i, value)
4231 1527 : vect_check_lower_bound (loop_vinfo, value, true, 1);
4232 : }
4233 :
4234 359218 : if (may_alias_ddrs.is_empty ())
4235 335691 : return opt_result::success ();
4236 :
4237 23527 : comp_alias_ddrs.create (may_alias_ddrs.length ());
4238 :
4239 23527 : unsigned int loop_depth
4240 23527 : = index_in_loop_nest (LOOP_VINFO_LOOP (loop_vinfo)->num,
4241 23527 : LOOP_VINFO_LOOP_NEST (loop_vinfo));
4242 :
4243 : /* First, we collect all data ref pairs for aliasing checks. */
4244 92031 : FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
4245 : {
4246 72252 : poly_uint64 lower_bound;
4247 72252 : tree segment_length_a, segment_length_b;
4248 72252 : unsigned HOST_WIDE_INT access_size_a, access_size_b;
4249 72252 : unsigned HOST_WIDE_INT align_a, align_b;
4250 :
4251 : /* Ignore the alias if the VF we chose ended up being no greater
4252 : than the dependence distance. */
4253 72252 : if (dependence_distance_ge_vf (ddr, loop_depth, vect_factor))
4254 10842 : continue;
4255 :
4256 72221 : if (DDR_OBJECT_A (ddr))
4257 : {
4258 76 : vec_object_pair new_pair (DDR_OBJECT_A (ddr), DDR_OBJECT_B (ddr));
4259 76 : if (!compared_objects.add (new_pair))
4260 : {
4261 18 : if (dump_enabled_p ())
4262 12 : dump_printf_loc (MSG_NOTE, vect_location,
4263 : "checking that %T and %T"
4264 : " have different addresses\n",
4265 : new_pair.first, new_pair.second);
4266 18 : LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo).safe_push (new_pair);
4267 : }
4268 76 : continue;
4269 76 : }
4270 :
4271 72145 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
4272 72145 : stmt_vec_info stmt_info_a = dr_info_a->stmt;
4273 :
4274 72145 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
4275 72145 : stmt_vec_info stmt_info_b = dr_info_b->stmt;
4276 :
4277 72145 : bool preserves_scalar_order_p
4278 72145 : = vect_preserves_scalar_order_p (dr_info_a, dr_info_b);
4279 72145 : bool ignore_step_p
4280 : = (vf_one_p
4281 72145 : && (preserves_scalar_order_p
4282 2967 : || operand_equal_p (DR_STEP (dr_info_a->dr),
4283 2967 : DR_STEP (dr_info_b->dr))));
4284 :
4285 : /* Skip the pair if inter-iteration dependencies are irrelevant
4286 : and intra-iteration dependencies are guaranteed to be honored. */
4287 10965 : if (ignore_step_p
4288 5603 : && (preserves_scalar_order_p
4289 2622 : || vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4290 : &lower_bound)))
4291 : {
4292 5362 : if (dump_enabled_p ())
4293 2496 : dump_printf_loc (MSG_NOTE, vect_location,
4294 : "no need for alias check between "
4295 : "%T and %T when VF is 1\n",
4296 2496 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4297 5362 : continue;
4298 : }
4299 :
4300 : /* See whether we can handle the alias using a bounds check on
4301 : the step, and whether that's likely to be the best approach.
4302 : (It might not be, for example, if the minimum step is much larger
4303 : than the number of bytes handled by one vector iteration.) */
4304 66783 : if (!ignore_step_p
4305 66542 : && TREE_CODE (DR_STEP (dr_info_a->dr)) != INTEGER_CST
4306 15827 : && vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4307 : &lower_bound)
4308 71761 : && (vect_small_gap_p (loop_vinfo, dr_info_a, lower_bound)
4309 293 : || vect_small_gap_p (loop_vinfo, dr_info_b, lower_bound)))
4310 : {
4311 4914 : bool unsigned_p = dr_known_forward_stride_p (dr_info_a->dr);
4312 4914 : if (dump_enabled_p ())
4313 : {
4314 3384 : dump_printf_loc (MSG_NOTE, vect_location, "no alias between "
4315 : "%T and %T when the step %T is outside ",
4316 : DR_REF (dr_info_a->dr),
4317 1692 : DR_REF (dr_info_b->dr),
4318 1692 : DR_STEP (dr_info_a->dr));
4319 1692 : if (unsigned_p)
4320 504 : dump_printf (MSG_NOTE, "[0");
4321 : else
4322 : {
4323 1188 : dump_printf (MSG_NOTE, "(");
4324 1188 : dump_dec (MSG_NOTE, poly_int64 (-lower_bound));
4325 : }
4326 1692 : dump_printf (MSG_NOTE, ", ");
4327 1692 : dump_dec (MSG_NOTE, lower_bound);
4328 1692 : dump_printf (MSG_NOTE, ")\n");
4329 : }
4330 4914 : vect_check_lower_bound (loop_vinfo, DR_STEP (dr_info_a->dr),
4331 : unsigned_p, lower_bound);
4332 4914 : continue;
4333 4914 : }
4334 :
4335 61869 : stmt_vec_info dr_group_first_a = DR_GROUP_FIRST_ELEMENT (stmt_info_a);
4336 61869 : if (dr_group_first_a)
4337 : {
4338 18911 : stmt_info_a = dr_group_first_a;
4339 18911 : dr_info_a = STMT_VINFO_DR_INFO (stmt_info_a);
4340 : }
4341 :
4342 61869 : stmt_vec_info dr_group_first_b = DR_GROUP_FIRST_ELEMENT (stmt_info_b);
4343 61869 : if (dr_group_first_b)
4344 : {
4345 19452 : stmt_info_b = dr_group_first_b;
4346 19452 : dr_info_b = STMT_VINFO_DR_INFO (stmt_info_b);
4347 : }
4348 :
4349 61869 : if (ignore_step_p)
4350 : {
4351 241 : segment_length_a = size_zero_node;
4352 241 : segment_length_b = size_zero_node;
4353 : }
4354 : else
4355 : {
4356 61628 : if (!operand_equal_p (DR_STEP (dr_info_a->dr),
4357 61628 : DR_STEP (dr_info_b->dr), 0))
4358 : {
4359 12875 : length_factor = scalar_loop_iters;
4360 12875 : if (TREE_CODE (length_factor) == SCEV_NOT_KNOWN)
4361 12 : return opt_result::failure_at (vect_location,
4362 : "Unsupported alias check on"
4363 : " uncounted loop\n");
4364 : }
4365 : else
4366 48753 : length_factor = size_int (vect_factor);
4367 61616 : segment_length_a = vect_vfa_segment_size (dr_info_a, length_factor);
4368 61616 : segment_length_b = vect_vfa_segment_size (dr_info_b, length_factor);
4369 : }
4370 61857 : access_size_a = vect_vfa_access_size (loop_vinfo, dr_info_a);
4371 61857 : access_size_b = vect_vfa_access_size (loop_vinfo, dr_info_b);
4372 61857 : align_a = vect_vfa_align (dr_info_a);
4373 61857 : align_b = vect_vfa_align (dr_info_b);
4374 :
4375 : /* See whether the alias is known at compilation time. */
4376 61857 : if (operand_equal_p (DR_BASE_ADDRESS (dr_info_a->dr),
4377 61857 : DR_BASE_ADDRESS (dr_info_b->dr), 0)
4378 5964 : && operand_equal_p (DR_OFFSET (dr_info_a->dr),
4379 5964 : DR_OFFSET (dr_info_b->dr), 0)
4380 4327 : && TREE_CODE (DR_STEP (dr_info_a->dr)) == INTEGER_CST
4381 4253 : && TREE_CODE (DR_STEP (dr_info_b->dr)) == INTEGER_CST
4382 4243 : && poly_int_tree_p (segment_length_a)
4383 66063 : && poly_int_tree_p (segment_length_b))
4384 : {
4385 4195 : int res = vect_compile_time_alias (dr_info_a, dr_info_b,
4386 : segment_length_a,
4387 : segment_length_b,
4388 : access_size_a,
4389 : access_size_b);
4390 4195 : if (res >= 0 && dump_enabled_p ())
4391 : {
4392 208 : dump_printf_loc (MSG_NOTE, vect_location,
4393 : "can tell at compile time that %T and %T",
4394 104 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4395 104 : if (res == 0)
4396 57 : dump_printf (MSG_NOTE, " do not alias\n");
4397 : else
4398 47 : dump_printf (MSG_NOTE, " alias\n");
4399 : }
4400 :
4401 4195 : if (res == 0)
4402 459 : continue;
4403 :
4404 3736 : if (res == 1)
4405 3736 : return opt_result::failure_at (stmt_info_b->stmt,
4406 : "not vectorized:"
4407 : " compilation time alias: %G%G",
4408 : stmt_info_a->stmt,
4409 : stmt_info_b->stmt);
4410 : }
4411 :
4412 : /* dr_with_seg_len requires the alignment to apply to the segment length
4413 : and access size, not just the start address. The access size can be
4414 : smaller than the pointer alignment for grouped accesses and bitfield
4415 : references; see PR115192 and PR116125 respectively. */
4416 57662 : align_a = std::min (align_a, least_bit_hwi (access_size_a));
4417 57662 : align_b = std::min (align_b, least_bit_hwi (access_size_b));
4418 :
4419 57662 : dr_with_seg_len dr_a (dr_info_a->dr, segment_length_a,
4420 57662 : access_size_a, align_a);
4421 57662 : dr_with_seg_len dr_b (dr_info_b->dr, segment_length_b,
4422 57662 : access_size_b, align_b);
4423 : /* Canonicalize the order to be the one that's needed for accurate
4424 : RAW, WAR and WAW flags, in cases where the data references are
4425 : well-ordered. The order doesn't really matter otherwise,
4426 : but we might as well be consistent. */
4427 57662 : if (get_later_stmt (stmt_info_a, stmt_info_b) == stmt_info_a)
4428 4312 : std::swap (dr_a, dr_b);
4429 :
4430 57662 : dr_with_seg_len_pair_t dr_with_seg_len_pair
4431 : (dr_a, dr_b, (preserves_scalar_order_p
4432 : ? dr_with_seg_len_pair_t::WELL_ORDERED
4433 63213 : : dr_with_seg_len_pair_t::REORDERED));
4434 :
4435 57662 : comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
4436 : }
4437 :
4438 19779 : prune_runtime_alias_test_list (&comp_alias_ddrs, vect_factor);
4439 :
4440 39558 : unsigned int count = (comp_alias_ddrs.length ()
4441 19779 : + check_unequal_addrs.length ());
4442 :
4443 19779 : if (count
4444 19779 : && (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo))
4445 : == VECT_COST_MODEL_VERY_CHEAP))
4446 13158 : return opt_result::failure_at
4447 13158 : (vect_location, "would need a runtime alias check\n");
4448 :
4449 6621 : if (dump_enabled_p ())
4450 1894 : dump_printf_loc (MSG_NOTE, vect_location,
4451 : "improved number of alias checks from %d to %d\n",
4452 : may_alias_ddrs.length (), count);
4453 6621 : unsigned limit = param_vect_max_version_for_alias_checks;
4454 6621 : if (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo)) == VECT_COST_MODEL_CHEAP)
4455 757 : limit = param_vect_max_version_for_alias_checks * 6 / 10;
4456 6621 : if (count > limit)
4457 162 : return opt_result::failure_at
4458 162 : (vect_location,
4459 : "number of versioning for alias run-time tests exceeds %d "
4460 : "(--param vect-max-version-for-alias-checks)\n", limit);
4461 :
4462 6459 : return opt_result::success ();
4463 359218 : }
4464 :
4465 : /* Structure to hold information about a supported gather/scatter
4466 : configuration. */
4467 : struct gather_scatter_config
4468 : {
4469 : internal_fn ifn;
4470 : tree offset_vectype;
4471 : int scale;
4472 : vec<int> elsvals;
4473 : };
4474 :
4475 : /* Determine which gather/scatter IFN is supported for the given parameters.
4476 : IFN_MASK_GATHER_LOAD, IFN_GATHER_LOAD, and IFN_MASK_LEN_GATHER_LOAD
4477 : are mutually exclusive, so we only need to find one. Return the
4478 : supported IFN or IFN_LAST if none are supported. */
4479 :
4480 : static internal_fn
4481 1154738 : vect_gather_scatter_which_ifn (bool read_p, bool masked_p,
4482 : tree vectype, tree memory_type,
4483 : tree offset_vectype, int scale,
4484 : vec<int> *elsvals)
4485 : {
4486 : /* Work out which functions to try. */
4487 1154738 : internal_fn ifn, alt_ifn, alt_ifn2;
4488 1154738 : if (read_p)
4489 : {
4490 870932 : ifn = masked_p ? IFN_MASK_GATHER_LOAD : IFN_GATHER_LOAD;
4491 : alt_ifn = IFN_MASK_GATHER_LOAD;
4492 : alt_ifn2 = IFN_MASK_LEN_GATHER_LOAD;
4493 : }
4494 : else
4495 : {
4496 283806 : ifn = masked_p ? IFN_MASK_SCATTER_STORE : IFN_SCATTER_STORE;
4497 : alt_ifn = IFN_MASK_SCATTER_STORE;
4498 : alt_ifn2 = IFN_MASK_LEN_SCATTER_STORE;
4499 : }
4500 :
4501 1154738 : if (!offset_vectype)
4502 : return IFN_LAST;
4503 :
4504 1154738 : if (internal_gather_scatter_fn_supported_p (ifn, vectype, memory_type,
4505 : offset_vectype, scale, elsvals))
4506 : return ifn;
4507 1154738 : if (internal_gather_scatter_fn_supported_p (alt_ifn, vectype, memory_type,
4508 : offset_vectype, scale, elsvals))
4509 : return alt_ifn;
4510 1154738 : if (internal_gather_scatter_fn_supported_p (alt_ifn2, vectype, memory_type,
4511 : offset_vectype, scale, elsvals))
4512 : return alt_ifn2;
4513 :
4514 : return IFN_LAST;
4515 : }
4516 :
4517 : /* Collect all supported offset vector types for a gather load or scatter
4518 : store. READ_P is true for loads and false for stores. MASKED_P is true
4519 : if the load or store is conditional. VECTYPE is the data vector type.
4520 : MEMORY_TYPE is the type of the memory elements being loaded or stored,
4521 : and OFFSET_TYPE is the type of the offset.
4522 : SCALE is the amount by which the offset should be multiplied.
4523 :
4524 : Return a vector of all configurations the target supports (which can
4525 : be none). */
4526 :
4527 : static auto_vec<gather_scatter_config>
4528 82336 : vect_gather_scatter_get_configs (vec_info *vinfo, bool read_p, bool masked_p,
4529 : tree vectype, tree memory_type,
4530 : tree offset_type, int scale)
4531 : {
4532 82336 : auto_vec<gather_scatter_config> configs;
4533 :
4534 82336 : auto_vec<tree, 8> offset_types_to_try;
4535 :
4536 : /* Try all sizes from the offset type's precision up to POINTER_SIZE. */
4537 82336 : for (unsigned int bits = TYPE_PRECISION (offset_type);
4538 388457 : bits <= POINTER_SIZE;
4539 292739 : bits *= 2)
4540 : {
4541 : /* Signed variant. */
4542 292739 : offset_types_to_try.safe_push
4543 292739 : (build_nonstandard_integer_type (bits, 0));
4544 : /* Unsigned variant. */
4545 292739 : offset_types_to_try.safe_push
4546 292739 : (build_nonstandard_integer_type (bits, 1));
4547 : }
4548 :
4549 : /* Once we find which IFN works for one offset type, we know that it
4550 : will work for other offset types as well. Then we can perform
4551 : the checks for the remaining offset types with only that IFN.
4552 : However, we might need to try different offset types to find which
4553 : IFN is supported, since the check is offset-type-specific. */
4554 : internal_fn ifn = IFN_LAST;
4555 :
4556 : /* Try each offset type. */
4557 667814 : for (unsigned int i = 0; i < offset_types_to_try.length (); i++)
4558 : {
4559 585478 : tree offset_type = offset_types_to_try[i];
4560 585478 : tree offset_vectype = get_vectype_for_scalar_type (vinfo, offset_type);
4561 585478 : if (!offset_vectype)
4562 9346 : continue;
4563 :
4564 : /* Try multiple scale values. Start with exact match, then try
4565 : smaller common scales that a target might support . */
4566 576132 : int scales_to_try[] = {scale, 1, 2, 4, 8};
4567 :
4568 3456792 : for (unsigned int j = 0;
4569 3456792 : j < sizeof (scales_to_try) / sizeof (*scales_to_try);
4570 : j++)
4571 : {
4572 2880660 : int try_scale = scales_to_try[j];
4573 :
4574 : /* Skip scales >= requested scale (except for exact match). */
4575 2880660 : if (j > 0 && try_scale >= scale)
4576 1725922 : continue;
4577 :
4578 : /* Skip if requested scale is not a multiple of this scale. */
4579 1154882 : if (j > 0 && scale % try_scale != 0)
4580 144 : continue;
4581 :
4582 1154738 : vec<int> elsvals = vNULL;
4583 :
4584 : /* If we haven't determined which IFN is supported yet, try all three
4585 : to find which one the target supports. */
4586 1154738 : if (ifn == IFN_LAST)
4587 : {
4588 1154738 : ifn = vect_gather_scatter_which_ifn (read_p, masked_p,
4589 : vectype, memory_type,
4590 : offset_vectype, try_scale,
4591 : &elsvals);
4592 1154738 : if (ifn != IFN_LAST)
4593 : {
4594 : /* Found which IFN is supported. Save this configuration. */
4595 0 : gather_scatter_config config;
4596 0 : config.ifn = ifn;
4597 0 : config.offset_vectype = offset_vectype;
4598 0 : config.scale = try_scale;
4599 0 : config.elsvals = elsvals;
4600 0 : configs.safe_push (config);
4601 : }
4602 : }
4603 : else
4604 : {
4605 : /* We already know which IFN is supported, just check if this
4606 : offset type and scale work with it. */
4607 0 : if (internal_gather_scatter_fn_supported_p (ifn, vectype,
4608 : memory_type,
4609 : offset_vectype,
4610 : try_scale,
4611 : &elsvals))
4612 : {
4613 0 : gather_scatter_config config;
4614 0 : config.ifn = ifn;
4615 0 : config.offset_vectype = offset_vectype;
4616 0 : config.scale = try_scale;
4617 0 : config.elsvals = elsvals;
4618 0 : configs.safe_push (config);
4619 : }
4620 : }
4621 : }
4622 : }
4623 :
4624 82336 : return configs;
4625 82336 : }
4626 :
4627 : /* Check whether we can use an internal function for a gather load
4628 : or scatter store. READ_P is true for loads and false for stores.
4629 : MASKED_P is true if the load or store is conditional. MEMORY_TYPE is
4630 : the type of the memory elements being loaded or stored. OFFSET_TYPE
4631 : is the type of the offset that is being applied to the invariant
4632 : base address. If OFFSET_TYPE is scalar the function chooses an
4633 : appropriate vector type for it. SCALE is the amount by which the
4634 : offset should be multiplied *after* it has been converted to address width.
4635 : If the target does not support the requested SCALE, SUPPORTED_SCALE
4636 : will contain the scale that is actually supported
4637 : (which may be smaller, requiring additional multiplication).
4638 : Otherwise SUPPORTED_SCALE is 0.
4639 :
4640 : Return true if the function is supported, storing the function id in
4641 : *IFN_OUT and the vector type for the offset in *OFFSET_VECTYPE_OUT.
4642 : If we support an offset vector type with different signedness than
4643 : OFFSET_TYPE store it in SUPPORTED_OFFSET_VECTYPE.
4644 :
4645 : If we can use gather/scatter and ELSVALS is nonzero, store the possible
4646 : else values in ELSVALS. */
4647 :
4648 : bool
4649 82336 : vect_gather_scatter_fn_p (vec_info *vinfo, bool read_p, bool masked_p,
4650 : tree vectype, tree memory_type, tree offset_type,
4651 : int scale, int *supported_scale,
4652 : internal_fn *ifn_out,
4653 : tree *offset_vectype_out,
4654 : tree *supported_offset_vectype,
4655 : vec<int> *elsvals)
4656 : {
4657 82336 : *supported_offset_vectype = NULL_TREE;
4658 82336 : *supported_scale = 0;
4659 82336 : unsigned int memory_bits = tree_to_uhwi (TYPE_SIZE (memory_type));
4660 82336 : unsigned int element_bits = vector_element_bits (vectype);
4661 82336 : if (element_bits != memory_bits)
4662 : /* For now the vector elements must be the same width as the
4663 : memory elements. */
4664 : return false;
4665 :
4666 : /* Get the original offset vector type for comparison. */
4667 82336 : tree offset_vectype = VECTOR_TYPE_P (offset_type)
4668 82336 : ? offset_type : get_vectype_for_scalar_type (vinfo, offset_type);
4669 :
4670 : /* If there is no offset vectype, bail. */
4671 69466 : if (!offset_vectype)
4672 : return false;
4673 :
4674 82336 : offset_type = TREE_TYPE (offset_vectype);
4675 :
4676 : /* Get all supported configurations for this data vector type. */
4677 82336 : auto_vec<gather_scatter_config> configs
4678 : = vect_gather_scatter_get_configs (vinfo, read_p, masked_p, vectype,
4679 82336 : memory_type, offset_type, scale);
4680 :
4681 82336 : if (configs.is_empty ())
4682 : return false;
4683 :
4684 : /* Selection priority:
4685 : 1 - Exact scale match + offset type match
4686 : 2 - Exact scale match + sign-swapped offset
4687 : 3 - Smaller scale + offset type match
4688 : 4 - Smaller scale + sign-swapped offset
4689 : Within each category, prefer smaller offset types. */
4690 :
4691 : /* First pass: exact scale match with no conversion. */
4692 0 : for (unsigned int i = 0; i < configs.length (); i++)
4693 : {
4694 0 : if (configs[i].scale == scale
4695 0 : && TYPE_SIGN (configs[i].offset_vectype)
4696 0 : == TYPE_SIGN (offset_vectype))
4697 : {
4698 0 : *ifn_out = configs[i].ifn;
4699 0 : *offset_vectype_out = configs[i].offset_vectype;
4700 0 : if (elsvals)
4701 0 : *elsvals = configs[i].elsvals;
4702 0 : return true;
4703 : }
4704 : }
4705 :
4706 : /* No direct match. This means we try to find either
4707 : - a sign-swapped offset vectype or
4708 : - a different scale and 2x larger offset type
4709 : - a different scale and larger sign-swapped offset vectype. */
4710 0 : unsigned int offset_precision = TYPE_PRECISION (TREE_TYPE (offset_vectype));
4711 0 : unsigned int needed_precision
4712 0 : = TYPE_UNSIGNED (offset_vectype) ? offset_precision * 2 : POINTER_SIZE;
4713 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4714 :
4715 : /* Second pass: No direct match. This means we try to find a sign-swapped
4716 : offset vectype. */
4717 0 : enum tree_code tmp;
4718 0 : for (unsigned int i = 0; i < configs.length (); i++)
4719 : {
4720 0 : unsigned int precision
4721 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4722 0 : if (configs[i].scale == scale
4723 0 : && precision >= needed_precision
4724 0 : && (supportable_convert_operation (CONVERT_EXPR,
4725 0 : configs[i].offset_vectype,
4726 : offset_vectype, &tmp)
4727 0 : || (needed_precision == offset_precision
4728 0 : && tree_nop_conversion_p (configs[i].offset_vectype,
4729 : offset_vectype))))
4730 : {
4731 0 : *ifn_out = configs[i].ifn;
4732 0 : *offset_vectype_out = offset_vectype;
4733 0 : *supported_offset_vectype = configs[i].offset_vectype;
4734 0 : if (elsvals)
4735 0 : *elsvals = configs[i].elsvals;
4736 0 : return true;
4737 : }
4738 : }
4739 :
4740 : /* Third pass: Try a smaller scale with the same signedness. */
4741 0 : needed_precision = offset_precision * 2;
4742 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4743 :
4744 0 : for (unsigned int i = 0; i < configs.length (); i++)
4745 : {
4746 0 : unsigned int precision
4747 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4748 0 : if (configs[i].scale < scale
4749 0 : && TYPE_SIGN (configs[i].offset_vectype)
4750 0 : == TYPE_SIGN (offset_vectype)
4751 0 : && precision >= needed_precision)
4752 : {
4753 0 : *ifn_out = configs[i].ifn;
4754 0 : *offset_vectype_out = configs[i].offset_vectype;
4755 0 : *supported_scale = configs[i].scale;
4756 0 : if (elsvals)
4757 0 : *elsvals = configs[i].elsvals;
4758 0 : return true;
4759 : }
4760 : }
4761 :
4762 : /* Fourth pass: Try a smaller scale and sign-swapped offset vectype. */
4763 0 : needed_precision
4764 0 : = TYPE_UNSIGNED (offset_vectype) ? offset_precision * 2 : POINTER_SIZE;
4765 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4766 :
4767 0 : for (unsigned int i = 0; i < configs.length (); i++)
4768 : {
4769 0 : unsigned int precision
4770 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4771 0 : if (configs[i].scale < scale
4772 0 : && precision >= needed_precision
4773 0 : && (supportable_convert_operation (CONVERT_EXPR,
4774 0 : configs[i].offset_vectype,
4775 : offset_vectype, &tmp)
4776 0 : || (needed_precision == offset_precision
4777 0 : && tree_nop_conversion_p (configs[i].offset_vectype,
4778 : offset_vectype))))
4779 : {
4780 0 : *ifn_out = configs[i].ifn;
4781 0 : *offset_vectype_out = offset_vectype;
4782 0 : *supported_offset_vectype = configs[i].offset_vectype;
4783 0 : *supported_scale = configs[i].scale;
4784 0 : if (elsvals)
4785 0 : *elsvals = configs[i].elsvals;
4786 0 : return true;
4787 : }
4788 : }
4789 :
4790 : return false;
4791 82336 : }
4792 :
4793 : /* STMT_INFO is a call to an internal gather load or scatter store function.
4794 : Describe the operation in INFO. */
4795 :
4796 : void
4797 0 : vect_describe_gather_scatter_call (stmt_vec_info stmt_info,
4798 : gather_scatter_info *info)
4799 : {
4800 0 : gcall *call = as_a <gcall *> (stmt_info->stmt);
4801 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4802 0 : data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4803 :
4804 0 : info->ifn = gimple_call_internal_fn (call);
4805 0 : info->decl = NULL_TREE;
4806 0 : info->base = gimple_call_arg (call, 0);
4807 0 : info->alias_ptr = gimple_call_arg
4808 0 : (call, internal_fn_alias_ptr_index (info->ifn));
4809 0 : info->offset = gimple_call_arg
4810 0 : (call, internal_fn_offset_index (info->ifn));
4811 0 : info->offset_vectype = NULL_TREE;
4812 0 : info->scale = TREE_INT_CST_LOW (gimple_call_arg
4813 : (call, internal_fn_scale_index (info->ifn)));
4814 0 : info->element_type = TREE_TYPE (vectype);
4815 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
4816 0 : }
4817 :
4818 : /* Return true if a non-affine read or write in STMT_INFO is suitable for a
4819 : gather load or scatter store with VECTYPE. Describe the operation in *INFO
4820 : if so. If it is suitable and ELSVALS is nonzero store the supported else
4821 : values in the vector it points to. */
4822 :
4823 : bool
4824 368394 : vect_check_gather_scatter (stmt_vec_info stmt_info, tree vectype,
4825 : loop_vec_info loop_vinfo,
4826 : gather_scatter_info *info, vec<int> *elsvals)
4827 : {
4828 368394 : HOST_WIDE_INT scale = 1;
4829 368394 : poly_int64 pbitpos, pbitsize;
4830 368394 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4831 368394 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4832 368394 : tree offtype = NULL_TREE;
4833 368394 : tree decl = NULL_TREE, base, off;
4834 368394 : tree memory_type = TREE_TYPE (DR_REF (dr));
4835 368394 : machine_mode pmode;
4836 368394 : int punsignedp, reversep, pvolatilep = 0;
4837 368394 : internal_fn ifn;
4838 368394 : tree offset_vectype;
4839 368394 : bool masked_p = false;
4840 :
4841 : /* See whether this is already a call to a gather/scatter internal function.
4842 : If not, see whether it's a masked load or store. */
4843 368394 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
4844 5663 : if (call && gimple_call_internal_p (call))
4845 : {
4846 5663 : ifn = gimple_call_internal_fn (call);
4847 5663 : if (internal_gather_scatter_fn_p (ifn))
4848 : {
4849 0 : vect_describe_gather_scatter_call (stmt_info, info);
4850 :
4851 : /* In pattern recog we simply used a ZERO else value that
4852 : we need to correct here. To that end just re-use the
4853 : (already succesful) check if we support a gather IFN
4854 : and have it populate the else values. */
4855 0 : if (DR_IS_READ (dr) && internal_fn_mask_index (ifn) >= 0 && elsvals)
4856 0 : supports_vec_gather_load_p (TYPE_MODE (vectype), elsvals);
4857 0 : return true;
4858 : }
4859 5663 : masked_p = (ifn == IFN_MASK_LOAD || ifn == IFN_MASK_STORE);
4860 : }
4861 :
4862 : /* True if we should aim to use internal functions rather than
4863 : built-in functions. */
4864 368394 : bool use_ifn_p = (DR_IS_READ (dr)
4865 368394 : ? supports_vec_gather_load_p (TYPE_MODE (vectype),
4866 : elsvals)
4867 368394 : : supports_vec_scatter_store_p (TYPE_MODE (vectype)));
4868 :
4869 368394 : base = DR_REF (dr);
4870 : /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
4871 : see if we can use the def stmt of the address. */
4872 368394 : if (masked_p
4873 5663 : && TREE_CODE (base) == MEM_REF
4874 5663 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
4875 5663 : && integer_zerop (TREE_OPERAND (base, 1))
4876 374057 : && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
4877 : {
4878 5663 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
4879 5663 : if (is_gimple_assign (def_stmt)
4880 5663 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
4881 556 : base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
4882 : }
4883 :
4884 : /* The gather and scatter builtins need address of the form
4885 : loop_invariant + vector * {1, 2, 4, 8}
4886 : or
4887 : loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
4888 : Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
4889 : of loop invariants/SSA_NAMEs defined in the loop, with casts,
4890 : multiplications and additions in it. To get a vector, we need
4891 : a single SSA_NAME that will be defined in the loop and will
4892 : contain everything that is not loop invariant and that can be
4893 : vectorized. The following code attempts to find such a preexistng
4894 : SSA_NAME OFF and put the loop invariants into a tree BASE
4895 : that can be gimplified before the loop. */
4896 368394 : base = get_inner_reference (base, &pbitsize, &pbitpos, &off, &pmode,
4897 : &punsignedp, &reversep, &pvolatilep);
4898 368394 : if (reversep)
4899 : return false;
4900 :
4901 : /* PR 107346. Packed structs can have fields at offsets that are not
4902 : multiples of BITS_PER_UNIT. Do not use gather/scatters in such cases. */
4903 368394 : if (!multiple_p (pbitpos, BITS_PER_UNIT))
4904 : return false;
4905 :
4906 : /* We need to be able to form an address to the base which for example
4907 : isn't possible for hard registers. */
4908 368394 : if (may_be_nonaddressable_p (base))
4909 : return false;
4910 :
4911 368386 : poly_int64 pbytepos = exact_div (pbitpos, BITS_PER_UNIT);
4912 :
4913 368386 : if (TREE_CODE (base) == MEM_REF)
4914 : {
4915 297012 : if (!integer_zerop (TREE_OPERAND (base, 1)))
4916 : {
4917 33697 : if (off == NULL_TREE)
4918 33380 : off = wide_int_to_tree (sizetype, mem_ref_offset (base));
4919 : else
4920 317 : off = size_binop (PLUS_EXPR, off,
4921 : fold_convert (sizetype, TREE_OPERAND (base, 1)));
4922 : }
4923 297012 : base = TREE_OPERAND (base, 0);
4924 : }
4925 : else
4926 71374 : base = build_fold_addr_expr (base);
4927 :
4928 368386 : if (off == NULL_TREE)
4929 238682 : off = size_zero_node;
4930 :
4931 : /* BASE must be loop invariant. If it is not invariant, but OFF is, then we
4932 : * can fix that by swapping BASE and OFF. */
4933 368386 : if (!expr_invariant_in_loop_p (loop, base))
4934 : {
4935 272563 : if (!expr_invariant_in_loop_p (loop, off))
4936 : return false;
4937 :
4938 272288 : std::swap (base, off);
4939 : }
4940 :
4941 368111 : base = fold_convert (sizetype, base);
4942 368111 : base = size_binop (PLUS_EXPR, base, size_int (pbytepos));
4943 368111 : int tmp_scale;
4944 368111 : tree tmp_offset_vectype;
4945 :
4946 : /* OFF at this point may be either a SSA_NAME or some tree expression
4947 : from get_inner_reference. Try to peel off loop invariants from it
4948 : into BASE as long as possible. */
4949 368111 : STRIP_NOPS (off);
4950 959630 : while (offtype == NULL_TREE)
4951 : {
4952 822892 : enum tree_code code;
4953 822892 : tree op0, op1, add = NULL_TREE;
4954 :
4955 822892 : if (TREE_CODE (off) == SSA_NAME)
4956 : {
4957 636078 : gimple *def_stmt = SSA_NAME_DEF_STMT (off);
4958 :
4959 636078 : if (expr_invariant_in_loop_p (loop, off))
4960 0 : return false;
4961 :
4962 636078 : if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
4963 : break;
4964 :
4965 505546 : op0 = gimple_assign_rhs1 (def_stmt);
4966 505546 : code = gimple_assign_rhs_code (def_stmt);
4967 505546 : op1 = gimple_assign_rhs2 (def_stmt);
4968 : }
4969 : else
4970 : {
4971 186814 : if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
4972 : return false;
4973 186814 : code = TREE_CODE (off);
4974 186814 : extract_ops_from_tree (off, &code, &op0, &op1);
4975 : }
4976 692360 : switch (code)
4977 : {
4978 215885 : case POINTER_PLUS_EXPR:
4979 215885 : case PLUS_EXPR:
4980 215885 : if (expr_invariant_in_loop_p (loop, op0))
4981 : {
4982 147296 : add = op0;
4983 147296 : off = op1;
4984 199711 : do_add:
4985 199711 : add = fold_convert (sizetype, add);
4986 199711 : if (scale != 1)
4987 45458 : add = size_binop (MULT_EXPR, add, size_int (scale));
4988 199711 : base = size_binop (PLUS_EXPR, base, add);
4989 591519 : continue;
4990 : }
4991 68589 : if (expr_invariant_in_loop_p (loop, op1))
4992 : {
4993 52221 : add = op1;
4994 52221 : off = op0;
4995 52221 : goto do_add;
4996 : }
4997 : break;
4998 390 : case MINUS_EXPR:
4999 390 : if (expr_invariant_in_loop_p (loop, op1))
5000 : {
5001 194 : add = fold_convert (sizetype, op1);
5002 194 : add = size_binop (MINUS_EXPR, size_zero_node, add);
5003 194 : off = op0;
5004 194 : goto do_add;
5005 : }
5006 : break;
5007 200505 : case MULT_EXPR:
5008 200505 : if (scale == 1 && tree_fits_shwi_p (op1))
5009 : {
5010 168319 : int new_scale = tree_to_shwi (op1);
5011 : /* Only treat this as a scaling operation if the target
5012 : supports it for at least some offset type. */
5013 168319 : if (use_ifn_p
5014 0 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5015 : masked_p, vectype, memory_type,
5016 : signed_char_type_node,
5017 : new_scale, &tmp_scale,
5018 : &ifn,
5019 : &offset_vectype,
5020 : &tmp_offset_vectype,
5021 : elsvals)
5022 168319 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5023 : masked_p, vectype, memory_type,
5024 : unsigned_char_type_node,
5025 : new_scale, &tmp_scale,
5026 : &ifn,
5027 : &offset_vectype,
5028 : &tmp_offset_vectype,
5029 : elsvals))
5030 : break;
5031 168319 : scale = new_scale;
5032 168319 : off = op0;
5033 168319 : continue;
5034 168319 : }
5035 : break;
5036 0 : case SSA_NAME:
5037 0 : off = op0;
5038 0 : continue;
5039 229841 : CASE_CONVERT:
5040 459666 : if (!POINTER_TYPE_P (TREE_TYPE (op0))
5041 459666 : && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
5042 : break;
5043 :
5044 : /* Don't include the conversion if the target is happy with
5045 : the current offset type. */
5046 229841 : if (use_ifn_p
5047 0 : && TREE_CODE (off) == SSA_NAME
5048 0 : && !POINTER_TYPE_P (TREE_TYPE (off))
5049 229841 : && vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5050 : masked_p, vectype, memory_type,
5051 0 : TREE_TYPE (off),
5052 : scale, &tmp_scale,
5053 : &ifn,
5054 : &offset_vectype,
5055 : &tmp_offset_vectype,
5056 : elsvals))
5057 : break;
5058 :
5059 229841 : if (TYPE_PRECISION (TREE_TYPE (op0))
5060 229841 : == TYPE_PRECISION (TREE_TYPE (off)))
5061 : {
5062 86751 : off = op0;
5063 86751 : continue;
5064 : }
5065 :
5066 : /* Include the conversion if it is widening and we're using
5067 : the IFN path or the target can handle the converted from
5068 : offset or the current size is not already the same as the
5069 : data vector element size. */
5070 143090 : if ((TYPE_PRECISION (TREE_TYPE (op0))
5071 143090 : < TYPE_PRECISION (TREE_TYPE (off)))
5072 143090 : && (use_ifn_p
5073 141832 : || (DR_IS_READ (dr)
5074 83912 : ? (targetm.vectorize.builtin_gather
5075 83912 : && targetm.vectorize.builtin_gather (vectype,
5076 83912 : TREE_TYPE (op0),
5077 : scale))
5078 57920 : : (targetm.vectorize.builtin_scatter
5079 57920 : && targetm.vectorize.builtin_scatter (vectype,
5080 57920 : TREE_TYPE (op0),
5081 : scale)))
5082 141144 : || !operand_equal_p (TYPE_SIZE (TREE_TYPE (off)),
5083 141144 : TYPE_SIZE (TREE_TYPE (vectype)), 0)))
5084 : {
5085 136738 : off = op0;
5086 136738 : offtype = TREE_TYPE (off);
5087 136738 : STRIP_NOPS (off);
5088 136738 : continue;
5089 : }
5090 : break;
5091 : default:
5092 : break;
5093 0 : }
5094 : break;
5095 : }
5096 :
5097 : /* If at the end OFF still isn't a SSA_NAME or isn't
5098 : defined in the loop, punt. */
5099 368111 : if (TREE_CODE (off) != SSA_NAME
5100 368111 : || expr_invariant_in_loop_p (loop, off))
5101 6844 : return false;
5102 :
5103 361267 : if (offtype == NULL_TREE)
5104 224883 : offtype = TREE_TYPE (off);
5105 :
5106 361267 : if (use_ifn_p)
5107 : {
5108 0 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
5109 : vectype, memory_type, offtype,
5110 : scale, &tmp_scale,
5111 : &ifn, &offset_vectype,
5112 : &tmp_offset_vectype,
5113 : elsvals))
5114 0 : ifn = IFN_LAST;
5115 : decl = NULL_TREE;
5116 : }
5117 : else
5118 : {
5119 361267 : if (DR_IS_READ (dr))
5120 : {
5121 263372 : if (targetm.vectorize.builtin_gather)
5122 263372 : decl = targetm.vectorize.builtin_gather (vectype, offtype, scale);
5123 : }
5124 : else
5125 : {
5126 97895 : if (targetm.vectorize.builtin_scatter)
5127 97895 : decl = targetm.vectorize.builtin_scatter (vectype, offtype, scale);
5128 : }
5129 361267 : ifn = IFN_LAST;
5130 : /* The offset vector type will be read from DECL when needed. */
5131 361267 : offset_vectype = NULL_TREE;
5132 : }
5133 :
5134 361267 : gcc_checking_assert (expr_invariant_in_loop_p (loop, base));
5135 361267 : gcc_checking_assert (!expr_invariant_in_loop_p (loop, off));
5136 :
5137 361267 : info->ifn = ifn;
5138 361267 : info->decl = decl;
5139 361267 : info->base = base;
5140 :
5141 722534 : info->alias_ptr = build_int_cst
5142 361267 : (reference_alias_ptr_type (DR_REF (dr)),
5143 361267 : get_object_alignment (DR_REF (dr)));
5144 :
5145 361267 : info->offset = off;
5146 361267 : info->offset_vectype = offset_vectype;
5147 361267 : info->scale = scale;
5148 361267 : info->element_type = TREE_TYPE (vectype);
5149 361267 : info->memory_type = memory_type;
5150 361267 : return true;
5151 : }
5152 :
5153 : /* Find the data references in STMT, analyze them with respect to LOOP and
5154 : append them to DATAREFS. Return false if datarefs in this stmt cannot
5155 : be handled. */
5156 :
5157 : opt_result
5158 32220719 : vect_find_stmt_data_reference (loop_p loop, gimple *stmt,
5159 : vec<data_reference_p> *datarefs,
5160 : vec<int> *dataref_groups, int group_id)
5161 : {
5162 : /* We can ignore clobbers for dataref analysis - they are removed during
5163 : loop vectorization and BB vectorization checks dependences with a
5164 : stmt walk. */
5165 32220719 : if (gimple_clobber_p (stmt))
5166 1090781 : return opt_result::success ();
5167 :
5168 57896286 : if (gimple_has_volatile_ops (stmt))
5169 320381 : return opt_result::failure_at (stmt, "not vectorized: volatile type: %G",
5170 : stmt);
5171 :
5172 30809557 : if (stmt_can_throw_internal (cfun, stmt))
5173 680984 : return opt_result::failure_at (stmt,
5174 : "not vectorized:"
5175 : " statement can throw an exception: %G",
5176 : stmt);
5177 :
5178 30128573 : auto_vec<data_reference_p, 2> refs;
5179 30128573 : opt_result res = find_data_references_in_stmt (loop, stmt, &refs);
5180 30128573 : if (!res)
5181 3649848 : return res;
5182 :
5183 26478725 : if (refs.is_empty ())
5184 15230635 : return opt_result::success ();
5185 :
5186 11248090 : if (refs.length () > 1)
5187 : {
5188 1247719 : while (!refs.is_empty ())
5189 832115 : free_data_ref (refs.pop ());
5190 415604 : return opt_result::failure_at (stmt,
5191 : "not vectorized: more than one "
5192 : "data ref in stmt: %G", stmt);
5193 : }
5194 :
5195 10832486 : data_reference_p dr = refs.pop ();
5196 10832486 : if (gcall *call = dyn_cast <gcall *> (stmt))
5197 20363 : if (!gimple_call_internal_p (call)
5198 20363 : || (gimple_call_internal_fn (call) != IFN_MASK_LOAD
5199 17285 : && gimple_call_internal_fn (call) != IFN_MASK_STORE))
5200 : {
5201 16789 : free_data_ref (dr);
5202 16789 : return opt_result::failure_at (stmt,
5203 : "not vectorized: dr in a call %G", stmt);
5204 : }
5205 :
5206 10815697 : if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
5207 10815697 : && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
5208 : {
5209 65191 : free_data_ref (dr);
5210 65191 : return opt_result::failure_at (stmt,
5211 : "not vectorized:"
5212 : " statement is an unsupported"
5213 : " bitfield access %G", stmt);
5214 : }
5215 :
5216 10750506 : if (DR_BASE_ADDRESS (dr)
5217 10657990 : && TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
5218 : {
5219 979 : free_data_ref (dr);
5220 979 : return opt_result::failure_at (stmt,
5221 : "not vectorized:"
5222 : " base addr of dr is a constant\n");
5223 : }
5224 :
5225 : /* Check whether this may be a SIMD lane access and adjust the
5226 : DR to make it easier for us to handle it. */
5227 10749527 : if (loop
5228 603639 : && loop->simduid
5229 10711 : && (!DR_BASE_ADDRESS (dr)
5230 2960 : || !DR_OFFSET (dr)
5231 2960 : || !DR_INIT (dr)
5232 2960 : || !DR_STEP (dr)))
5233 : {
5234 7751 : struct data_reference *newdr
5235 7751 : = create_data_ref (NULL, loop_containing_stmt (stmt), DR_REF (dr), stmt,
5236 7751 : DR_IS_READ (dr), DR_IS_CONDITIONAL_IN_STMT (dr));
5237 7751 : if (DR_BASE_ADDRESS (newdr)
5238 7751 : && DR_OFFSET (newdr)
5239 7751 : && DR_INIT (newdr)
5240 7751 : && DR_STEP (newdr)
5241 7751 : && TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
5242 15502 : && integer_zerop (DR_STEP (newdr)))
5243 : {
5244 7751 : tree base_address = DR_BASE_ADDRESS (newdr);
5245 7751 : tree off = DR_OFFSET (newdr);
5246 7751 : tree step = ssize_int (1);
5247 7751 : if (integer_zerop (off)
5248 7751 : && TREE_CODE (base_address) == POINTER_PLUS_EXPR)
5249 : {
5250 82 : off = TREE_OPERAND (base_address, 1);
5251 82 : base_address = TREE_OPERAND (base_address, 0);
5252 : }
5253 7751 : STRIP_NOPS (off);
5254 7751 : if (TREE_CODE (off) == MULT_EXPR
5255 7751 : && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
5256 : {
5257 7500 : step = TREE_OPERAND (off, 1);
5258 7500 : off = TREE_OPERAND (off, 0);
5259 7500 : STRIP_NOPS (off);
5260 : }
5261 541 : if (CONVERT_EXPR_P (off)
5262 7751 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off, 0)))
5263 7210 : < TYPE_PRECISION (TREE_TYPE (off))))
5264 7210 : off = TREE_OPERAND (off, 0);
5265 7751 : if (TREE_CODE (off) == SSA_NAME)
5266 : {
5267 7226 : gimple *def = SSA_NAME_DEF_STMT (off);
5268 : /* Look through widening conversion. */
5269 7226 : if (is_gimple_assign (def)
5270 7226 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5271 : {
5272 0 : tree rhs1 = gimple_assign_rhs1 (def);
5273 0 : if (TREE_CODE (rhs1) == SSA_NAME
5274 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5275 0 : && (TYPE_PRECISION (TREE_TYPE (off))
5276 0 : > TYPE_PRECISION (TREE_TYPE (rhs1))))
5277 0 : def = SSA_NAME_DEF_STMT (rhs1);
5278 : }
5279 7226 : if (is_gimple_call (def)
5280 7090 : && gimple_call_internal_p (def)
5281 14316 : && (gimple_call_internal_fn (def) == IFN_GOMP_SIMD_LANE))
5282 : {
5283 7090 : tree arg = gimple_call_arg (def, 0);
5284 7090 : tree reft = TREE_TYPE (DR_REF (newdr));
5285 7090 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
5286 7090 : arg = SSA_NAME_VAR (arg);
5287 7090 : if (arg == loop->simduid
5288 : /* For now. */
5289 7090 : && tree_int_cst_equal (TYPE_SIZE_UNIT (reft), step))
5290 : {
5291 7065 : DR_BASE_ADDRESS (newdr) = base_address;
5292 7065 : DR_OFFSET (newdr) = ssize_int (0);
5293 7065 : DR_STEP (newdr) = step;
5294 7065 : DR_OFFSET_ALIGNMENT (newdr) = BIGGEST_ALIGNMENT;
5295 7065 : DR_STEP_ALIGNMENT (newdr) = highest_pow2_factor (step);
5296 : /* Mark as simd-lane access. */
5297 7065 : tree arg2 = gimple_call_arg (def, 1);
5298 7065 : newdr->aux = (void *) (-1 - tree_to_uhwi (arg2));
5299 7065 : free_data_ref (dr);
5300 7065 : datarefs->safe_push (newdr);
5301 7065 : if (dataref_groups)
5302 0 : dataref_groups->safe_push (group_id);
5303 7065 : return opt_result::success ();
5304 : }
5305 : }
5306 : }
5307 : }
5308 686 : free_data_ref (newdr);
5309 : }
5310 :
5311 10742462 : datarefs->safe_push (dr);
5312 10742462 : if (dataref_groups)
5313 10145888 : dataref_groups->safe_push (group_id);
5314 10742462 : return opt_result::success ();
5315 30128573 : }
5316 :
5317 : /* Function vect_analyze_data_refs.
5318 :
5319 : Find all the data references in the loop or basic block.
5320 :
5321 : The general structure of the analysis of data refs in the vectorizer is as
5322 : follows:
5323 : 1- vect_analyze_data_refs(loop/bb): call
5324 : compute_data_dependences_for_loop/bb to find and analyze all data-refs
5325 : in the loop/bb and their dependences.
5326 : 2- vect_analyze_dependences(): apply dependence testing using ddrs.
5327 : 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
5328 : 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
5329 :
5330 : */
5331 :
5332 : opt_result
5333 2620072 : vect_analyze_data_refs (vec_info *vinfo, bool *fatal)
5334 : {
5335 2620072 : class loop *loop = NULL;
5336 2620072 : unsigned int i;
5337 2620072 : struct data_reference *dr;
5338 2620072 : tree scalar_type;
5339 :
5340 2620072 : DUMP_VECT_SCOPE ("vect_analyze_data_refs");
5341 :
5342 2620072 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5343 436248 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5344 :
5345 : /* Go through the data-refs, check that the analysis succeeded. Update
5346 : pointer from stmt_vec_info struct to DR and vectype. */
5347 :
5348 2620072 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
5349 17565974 : FOR_EACH_VEC_ELT (datarefs, i, dr)
5350 : {
5351 15004495 : enum { SG_NONE, GATHER, SCATTER } gatherscatter = SG_NONE;
5352 :
5353 15004495 : gcc_assert (DR_REF (dr));
5354 15004495 : stmt_vec_info stmt_info = vinfo->lookup_stmt (DR_STMT (dr));
5355 15004495 : gcc_assert (!stmt_info->dr_aux.dr);
5356 15004495 : stmt_info->dr_aux.dr = dr;
5357 15004495 : stmt_info->dr_aux.stmt = stmt_info;
5358 :
5359 : /* Check that analysis of the data-ref succeeded. */
5360 15004495 : if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
5361 14880256 : || !DR_STEP (dr))
5362 : {
5363 248478 : bool maybe_gather
5364 124239 : = DR_IS_READ (dr)
5365 124239 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5366 248478 : bool maybe_scatter
5367 : = DR_IS_WRITE (dr)
5368 124239 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5369 :
5370 : /* If target supports vector gather loads or scatter stores,
5371 : see if they can't be used. */
5372 124239 : if (is_a <loop_vec_info> (vinfo)
5373 124239 : && !nested_in_vect_loop_p (loop, stmt_info))
5374 : {
5375 120868 : if (maybe_gather || maybe_scatter)
5376 : {
5377 120868 : if (maybe_gather)
5378 : gatherscatter = GATHER;
5379 : else
5380 26597 : gatherscatter = SCATTER;
5381 : }
5382 : }
5383 :
5384 26597 : if (gatherscatter == SG_NONE)
5385 : {
5386 3371 : if (dump_enabled_p ())
5387 5 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5388 : "not vectorized: data ref analysis "
5389 : "failed %G", stmt_info->stmt);
5390 3371 : if (is_a <bb_vec_info> (vinfo))
5391 : {
5392 : /* In BB vectorization the ref can still participate
5393 : in dependence analysis, we just can't vectorize it. */
5394 3021 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5395 3021 : continue;
5396 : }
5397 350 : return opt_result::failure_at (stmt_info->stmt,
5398 : "not vectorized:"
5399 : " data ref analysis failed: %G",
5400 : stmt_info->stmt);
5401 : }
5402 : }
5403 :
5404 : /* See if this was detected as SIMD lane access. */
5405 15001124 : if (dr->aux == (void *)-1
5406 15001124 : || dr->aux == (void *)-2
5407 14992222 : || dr->aux == (void *)-3
5408 14991382 : || dr->aux == (void *)-4)
5409 : {
5410 10542 : if (nested_in_vect_loop_p (loop, stmt_info))
5411 0 : return opt_result::failure_at (stmt_info->stmt,
5412 : "not vectorized:"
5413 : " data ref analysis failed: %G",
5414 : stmt_info->stmt);
5415 10542 : STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)
5416 10542 : = -(uintptr_t) dr->aux;
5417 : }
5418 :
5419 15001124 : tree base = get_base_address (DR_REF (dr));
5420 15001124 : if (base && VAR_P (base) && DECL_NONALIASED (base))
5421 : {
5422 8730 : if (dump_enabled_p ())
5423 186 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5424 : "not vectorized: base object not addressable "
5425 : "for stmt: %G", stmt_info->stmt);
5426 8730 : if (is_a <bb_vec_info> (vinfo))
5427 : {
5428 : /* In BB vectorization the ref can still participate
5429 : in dependence analysis, we just can't vectorize it. */
5430 8730 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5431 8730 : continue;
5432 : }
5433 0 : return opt_result::failure_at (stmt_info->stmt,
5434 : "not vectorized: base object not"
5435 : " addressable for stmt: %G",
5436 : stmt_info->stmt);
5437 : }
5438 :
5439 14992394 : if (is_a <loop_vec_info> (vinfo)
5440 983870 : && DR_STEP (dr)
5441 15855396 : && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
5442 : {
5443 41694 : if (nested_in_vect_loop_p (loop, stmt_info))
5444 372 : return opt_result::failure_at (stmt_info->stmt,
5445 : "not vectorized: "
5446 : "not suitable for strided load %G",
5447 : stmt_info->stmt);
5448 41322 : STMT_VINFO_STRIDED_P (stmt_info) = true;
5449 : }
5450 :
5451 : /* Update DR field in stmt_vec_info struct. */
5452 :
5453 : /* If the dataref is in an inner-loop of the loop that is considered for
5454 : for vectorization, we also want to analyze the access relative to
5455 : the outer-loop (DR contains information only relative to the
5456 : inner-most enclosing loop). We do that by building a reference to the
5457 : first location accessed by the inner-loop, and analyze it relative to
5458 : the outer-loop. */
5459 14992022 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
5460 : {
5461 : /* Build a reference to the first location accessed by the
5462 : inner loop: *(BASE + INIT + OFFSET). By construction,
5463 : this address must be invariant in the inner loop, so we
5464 : can consider it as being used in the outer loop. */
5465 11717 : tree base = unshare_expr (DR_BASE_ADDRESS (dr));
5466 11717 : tree offset = unshare_expr (DR_OFFSET (dr));
5467 11717 : tree init = unshare_expr (DR_INIT (dr));
5468 11717 : tree init_offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset),
5469 : init, offset);
5470 11717 : tree init_addr = fold_build_pointer_plus (base, init_offset);
5471 11717 : tree init_ref = build_fold_indirect_ref (init_addr);
5472 :
5473 11717 : if (dump_enabled_p ())
5474 1221 : dump_printf_loc (MSG_NOTE, vect_location,
5475 : "analyze in outer loop: %T\n", init_ref);
5476 :
5477 11717 : opt_result res
5478 11717 : = dr_analyze_innermost (&STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info),
5479 11717 : init_ref, loop, stmt_info->stmt);
5480 11717 : if (!res)
5481 : /* dr_analyze_innermost already explained the failure. */
5482 161 : return res;
5483 :
5484 11556 : if (dump_enabled_p ())
5485 1217 : dump_printf_loc (MSG_NOTE, vect_location,
5486 : "\touter base_address: %T\n"
5487 : "\touter offset from base address: %T\n"
5488 : "\touter constant offset from base address: %T\n"
5489 : "\touter step: %T\n"
5490 : "\touter base alignment: %d\n\n"
5491 : "\touter base misalignment: %d\n"
5492 : "\touter offset alignment: %d\n"
5493 : "\touter step alignment: %d\n",
5494 : STMT_VINFO_DR_BASE_ADDRESS (stmt_info),
5495 : STMT_VINFO_DR_OFFSET (stmt_info),
5496 : STMT_VINFO_DR_INIT (stmt_info),
5497 : STMT_VINFO_DR_STEP (stmt_info),
5498 : STMT_VINFO_DR_BASE_ALIGNMENT (stmt_info),
5499 : STMT_VINFO_DR_BASE_MISALIGNMENT (stmt_info),
5500 : STMT_VINFO_DR_OFFSET_ALIGNMENT (stmt_info),
5501 : STMT_VINFO_DR_STEP_ALIGNMENT (stmt_info));
5502 : }
5503 :
5504 : /* Set vectype for STMT. */
5505 14991861 : scalar_type = TREE_TYPE (DR_REF (dr));
5506 14991861 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
5507 14991861 : if (!vectype)
5508 : {
5509 1791359 : if (dump_enabled_p ())
5510 : {
5511 1847 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5512 : "not vectorized: no vectype for stmt: %G",
5513 : stmt_info->stmt);
5514 1847 : dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
5515 1847 : dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
5516 : scalar_type);
5517 1847 : dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5518 : }
5519 :
5520 1791359 : if (is_a <bb_vec_info> (vinfo))
5521 : {
5522 : /* No vector type is fine, the ref can still participate
5523 : in dependence analysis, we just can't vectorize it. */
5524 1741806 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5525 1741806 : continue;
5526 : }
5527 49553 : if (fatal)
5528 49553 : *fatal = false;
5529 49553 : return opt_result::failure_at (stmt_info->stmt,
5530 : "not vectorized:"
5531 : " no vectype for stmt: %G"
5532 : " scalar_type: %T\n",
5533 : stmt_info->stmt, scalar_type);
5534 : }
5535 : else
5536 : {
5537 13200502 : if (dump_enabled_p ())
5538 80750 : dump_printf_loc (MSG_NOTE, vect_location,
5539 : "got vectype for stmt: %G%T\n",
5540 : stmt_info->stmt, vectype);
5541 : }
5542 :
5543 : /* Leave the BB vectorizer to pick the vector type later, based on
5544 : the final dataref group size and SLP node size. */
5545 13200502 : if (is_a <loop_vec_info> (vinfo))
5546 933784 : STMT_VINFO_VECTYPE (stmt_info) = vectype;
5547 :
5548 13200502 : if (gatherscatter != SG_NONE)
5549 : {
5550 115238 : gather_scatter_info gs_info;
5551 115238 : if (!vect_check_gather_scatter (stmt_info, vectype,
5552 : as_a <loop_vec_info> (vinfo),
5553 : &gs_info)
5554 225976 : || !get_vectype_for_scalar_type (vinfo,
5555 110738 : TREE_TYPE (gs_info.offset)))
5556 : {
5557 8157 : if (fatal)
5558 8157 : *fatal = false;
5559 8157 : return opt_result::failure_at
5560 8511 : (stmt_info->stmt,
5561 : (gatherscatter == GATHER)
5562 : ? "not vectorized: not suitable for gather load %G"
5563 : : "not vectorized: not suitable for scatter store %G",
5564 : stmt_info->stmt);
5565 : }
5566 107081 : STMT_VINFO_GATHER_SCATTER_P (stmt_info) = gatherscatter;
5567 : }
5568 : }
5569 :
5570 : /* We used to stop processing and prune the list here. Verify we no
5571 : longer need to. */
5572 4073647 : gcc_assert (i == datarefs.length ());
5573 :
5574 2561479 : return opt_result::success ();
5575 : }
5576 :
5577 :
5578 : /* Function vect_get_new_vect_var.
5579 :
5580 : Returns a name for a new variable. The current naming scheme appends the
5581 : prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
5582 : the name of vectorizer generated variables, and appends that to NAME if
5583 : provided. */
5584 :
5585 : tree
5586 1936238 : vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
5587 : {
5588 1936238 : const char *prefix;
5589 1936238 : tree new_vect_var;
5590 :
5591 1936238 : switch (var_kind)
5592 : {
5593 : case vect_simple_var:
5594 : prefix = "vect";
5595 : break;
5596 23045 : case vect_scalar_var:
5597 23045 : prefix = "stmp";
5598 23045 : break;
5599 19884 : case vect_mask_var:
5600 19884 : prefix = "mask";
5601 19884 : break;
5602 1388526 : case vect_pointer_var:
5603 1388526 : prefix = "vectp";
5604 1388526 : break;
5605 0 : default:
5606 0 : gcc_unreachable ();
5607 : }
5608 :
5609 1936238 : if (name)
5610 : {
5611 1093860 : char* tmp = concat (prefix, "_", name, NULL);
5612 1093860 : new_vect_var = create_tmp_reg (type, tmp);
5613 1093860 : free (tmp);
5614 : }
5615 : else
5616 842378 : new_vect_var = create_tmp_reg (type, prefix);
5617 :
5618 1936238 : return new_vect_var;
5619 : }
5620 :
5621 : /* Like vect_get_new_vect_var but return an SSA name. */
5622 :
5623 : tree
5624 6827 : vect_get_new_ssa_name (tree type, enum vect_var_kind var_kind, const char *name)
5625 : {
5626 6827 : const char *prefix;
5627 6827 : tree new_vect_var;
5628 :
5629 6827 : switch (var_kind)
5630 : {
5631 : case vect_simple_var:
5632 : prefix = "vect";
5633 : break;
5634 312 : case vect_scalar_var:
5635 312 : prefix = "stmp";
5636 312 : break;
5637 0 : case vect_pointer_var:
5638 0 : prefix = "vectp";
5639 0 : break;
5640 0 : default:
5641 0 : gcc_unreachable ();
5642 : }
5643 :
5644 6827 : if (name)
5645 : {
5646 6350 : char* tmp = concat (prefix, "_", name, NULL);
5647 6350 : new_vect_var = make_temp_ssa_name (type, NULL, tmp);
5648 6350 : free (tmp);
5649 : }
5650 : else
5651 477 : new_vect_var = make_temp_ssa_name (type, NULL, prefix);
5652 :
5653 6827 : return new_vect_var;
5654 : }
5655 :
5656 : /* Duplicate points-to info on NAME from DR_INFO. */
5657 :
5658 : static void
5659 291204 : vect_duplicate_ssa_name_ptr_info (tree name, dr_vec_info *dr_info)
5660 : {
5661 291204 : duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr_info->dr));
5662 : /* DR_PTR_INFO is for a base SSA name, not including constant or
5663 : variable offsets in the ref so its alignment info does not apply. */
5664 291204 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
5665 291204 : }
5666 :
5667 : /* Function vect_create_addr_base_for_vector_ref.
5668 :
5669 : Create an expression that computes the address of the first memory location
5670 : that will be accessed for a data reference.
5671 :
5672 : Input:
5673 : STMT_INFO: The statement containing the data reference.
5674 : NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
5675 : OFFSET: Optional. If supplied, it is be added to the initial address.
5676 : LOOP: Specify relative to which loop-nest should the address be computed.
5677 : For example, when the dataref is in an inner-loop nested in an
5678 : outer-loop that is now being vectorized, LOOP can be either the
5679 : outer-loop, or the inner-loop. The first memory location accessed
5680 : by the following dataref ('in' points to short):
5681 :
5682 : for (i=0; i<N; i++)
5683 : for (j=0; j<M; j++)
5684 : s += in[i+j]
5685 :
5686 : is as follows:
5687 : if LOOP=i_loop: &in (relative to i_loop)
5688 : if LOOP=j_loop: &in+i*2B (relative to j_loop)
5689 :
5690 : Output:
5691 : 1. Return an SSA_NAME whose value is the address of the memory location of
5692 : the first vector of the data reference.
5693 : 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
5694 : these statement(s) which define the returned SSA_NAME.
5695 :
5696 : FORNOW: We are only handling array accesses with step 1. */
5697 :
5698 : tree
5699 694399 : vect_create_addr_base_for_vector_ref (vec_info *vinfo, stmt_vec_info stmt_info,
5700 : gimple_seq *new_stmt_list,
5701 : tree offset)
5702 : {
5703 694399 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5704 694399 : struct data_reference *dr = dr_info->dr;
5705 694399 : const char *base_name;
5706 694399 : tree addr_base;
5707 694399 : tree dest;
5708 694399 : gimple_seq seq = NULL;
5709 694399 : tree vect_ptr_type;
5710 694399 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5711 694399 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
5712 :
5713 694399 : tree data_ref_base = unshare_expr (drb->base_address);
5714 694399 : tree base_offset = unshare_expr (get_dr_vinfo_offset (vinfo, dr_info, true));
5715 694399 : tree init = unshare_expr (drb->init);
5716 :
5717 694399 : if (loop_vinfo)
5718 128677 : base_name = get_name (data_ref_base);
5719 : else
5720 : {
5721 565722 : base_offset = ssize_int (0);
5722 565722 : init = ssize_int (0);
5723 565722 : base_name = get_name (DR_REF (dr));
5724 : }
5725 :
5726 : /* Create base_offset */
5727 694399 : base_offset = size_binop (PLUS_EXPR,
5728 : fold_convert (sizetype, base_offset),
5729 : fold_convert (sizetype, init));
5730 :
5731 694399 : if (offset)
5732 : {
5733 3100 : offset = fold_convert (sizetype, offset);
5734 3100 : base_offset = fold_build2 (PLUS_EXPR, sizetype,
5735 : base_offset, offset);
5736 : }
5737 :
5738 : /* base + base_offset */
5739 694399 : if (loop_vinfo)
5740 128677 : addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
5741 : else
5742 1131444 : addr_base = build1 (ADDR_EXPR,
5743 565722 : build_pointer_type (TREE_TYPE (DR_REF (dr))),
5744 : /* Strip zero offset components since we don't need
5745 : them and they can confuse late diagnostics if
5746 : we CSE them wrongly. See PR106904 for example. */
5747 : unshare_expr (strip_zero_offset_components
5748 : (DR_REF (dr))));
5749 :
5750 694399 : vect_ptr_type = build_pointer_type (TREE_TYPE (DR_REF (dr)));
5751 694399 : dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
5752 694399 : addr_base = force_gimple_operand (addr_base, &seq, true, dest);
5753 694399 : gimple_seq_add_seq (new_stmt_list, seq);
5754 :
5755 694399 : if (DR_PTR_INFO (dr)
5756 182155 : && TREE_CODE (addr_base) == SSA_NAME
5757 : /* We should only duplicate pointer info to newly created SSA names. */
5758 875769 : && SSA_NAME_VAR (addr_base) == dest)
5759 : {
5760 152274 : gcc_assert (!SSA_NAME_PTR_INFO (addr_base));
5761 152274 : vect_duplicate_ssa_name_ptr_info (addr_base, dr_info);
5762 : }
5763 :
5764 694399 : if (dump_enabled_p ())
5765 25210 : dump_printf_loc (MSG_NOTE, vect_location, "created %T\n", addr_base);
5766 :
5767 694399 : return addr_base;
5768 : }
5769 :
5770 :
5771 : /* Function vect_create_data_ref_ptr.
5772 :
5773 : Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
5774 : location accessed in the loop by STMT_INFO, along with the def-use update
5775 : chain to appropriately advance the pointer through the loop iterations.
5776 : Also set aliasing information for the pointer. This pointer is used by
5777 : the callers to this function to create a memory reference expression for
5778 : vector load/store access.
5779 :
5780 : Input:
5781 : 1. STMT_INFO: a stmt that references memory. Expected to be of the form
5782 : GIMPLE_ASSIGN <name, data-ref> or
5783 : GIMPLE_ASSIGN <data-ref, name>.
5784 : 2. AGGR_TYPE: the type of the reference, which should be either a vector
5785 : or an array.
5786 : 3. AT_LOOP: the loop where the vector memref is to be created.
5787 : 4. OFFSET (optional): a byte offset to be added to the initial address
5788 : accessed by the data-ref in STMT_INFO.
5789 : 5. BSI: location where the new stmts are to be placed if there is no loop
5790 : 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
5791 : pointing to the initial address.
5792 : 8. IV_STEP (optional, defaults to NULL): the amount that should be added
5793 : to the IV during each iteration of the loop. NULL says to move
5794 : by one copy of AGGR_TYPE up or down, depending on the step of the
5795 : data reference.
5796 :
5797 : Output:
5798 : 1. Declare a new ptr to vector_type, and have it point to the base of the
5799 : data reference (initial addressed accessed by the data reference).
5800 : For example, for vector of type V8HI, the following code is generated:
5801 :
5802 : v8hi *ap;
5803 : ap = (v8hi *)initial_address;
5804 :
5805 : if OFFSET is not supplied:
5806 : initial_address = &a[init];
5807 : if OFFSET is supplied:
5808 : initial_address = &a[init] + OFFSET;
5809 : if BYTE_OFFSET is supplied:
5810 : initial_address = &a[init] + BYTE_OFFSET;
5811 :
5812 : Return the initial_address in INITIAL_ADDRESS.
5813 :
5814 : 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
5815 : update the pointer in each iteration of the loop.
5816 :
5817 : Return the increment stmt that updates the pointer in PTR_INCR.
5818 :
5819 : 3. Return the pointer. */
5820 :
5821 : tree
5822 694127 : vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
5823 : tree aggr_type, class loop *at_loop, tree offset,
5824 : tree *initial_address, gimple_stmt_iterator *gsi,
5825 : gimple **ptr_incr, bool only_init,
5826 : tree iv_step)
5827 : {
5828 694127 : const char *base_name;
5829 694127 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5830 694127 : class loop *loop = NULL;
5831 694127 : bool nested_in_vect_loop = false;
5832 694127 : class loop *containing_loop = NULL;
5833 694127 : tree aggr_ptr_type;
5834 694127 : tree aggr_ptr;
5835 694127 : tree new_temp;
5836 694127 : gimple_seq new_stmt_list = NULL;
5837 694127 : edge pe = NULL;
5838 694127 : basic_block new_bb;
5839 694127 : tree aggr_ptr_init;
5840 694127 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5841 694127 : struct data_reference *dr = dr_info->dr;
5842 694127 : tree aptr;
5843 694127 : gimple_stmt_iterator incr_gsi;
5844 694127 : bool insert_after;
5845 694127 : tree indx_before_incr, indx_after_incr;
5846 694127 : gimple *incr;
5847 694127 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5848 :
5849 694127 : gcc_assert (iv_step != NULL_TREE
5850 : || TREE_CODE (aggr_type) == ARRAY_TYPE
5851 : || TREE_CODE (aggr_type) == VECTOR_TYPE);
5852 :
5853 694127 : if (loop_vinfo)
5854 : {
5855 128405 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5856 128405 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
5857 128405 : containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
5858 128405 : pe = loop_preheader_edge (loop);
5859 : }
5860 : else
5861 : {
5862 565722 : gcc_assert (bb_vinfo);
5863 565722 : only_init = true;
5864 565722 : *ptr_incr = NULL;
5865 : }
5866 :
5867 : /* Create an expression for the first address accessed by this load
5868 : in LOOP. */
5869 694127 : base_name = get_name (DR_BASE_ADDRESS (dr));
5870 :
5871 694127 : if (dump_enabled_p ())
5872 : {
5873 25115 : tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
5874 25115 : dump_printf_loc (MSG_NOTE, vect_location,
5875 : "create %s-pointer variable to type: %T",
5876 25115 : get_tree_code_name (TREE_CODE (aggr_type)),
5877 : aggr_type);
5878 25115 : if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
5879 13461 : dump_printf (MSG_NOTE, " vectorizing an array ref: ");
5880 11654 : else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
5881 0 : dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
5882 11654 : else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
5883 1638 : dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
5884 : else
5885 10016 : dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
5886 25115 : dump_printf (MSG_NOTE, "%T\n", DR_BASE_OBJECT (dr));
5887 : }
5888 :
5889 : /* (1) Create the new aggregate-pointer variable.
5890 : Vector and array types inherit the alias set of their component
5891 : type by default so we need to use a ref-all pointer if the data
5892 : reference does not conflict with the created aggregated data
5893 : reference because it is not addressable. */
5894 694127 : bool need_ref_all = false;
5895 694127 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5896 : get_alias_set (DR_REF (dr))))
5897 : need_ref_all = true;
5898 : /* Likewise for any of the data references in the stmt group. */
5899 592870 : else if (DR_GROUP_SIZE (stmt_info) > 1)
5900 : {
5901 479243 : stmt_vec_info sinfo = DR_GROUP_FIRST_ELEMENT (stmt_info);
5902 1337017 : do
5903 : {
5904 1337017 : struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
5905 1337017 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5906 : get_alias_set (DR_REF (sdr))))
5907 : {
5908 : need_ref_all = true;
5909 : break;
5910 : }
5911 1336042 : sinfo = DR_GROUP_NEXT_ELEMENT (sinfo);
5912 : }
5913 1336042 : while (sinfo);
5914 : }
5915 694127 : aggr_ptr_type = build_pointer_type_for_mode (aggr_type, VOIDmode,
5916 : need_ref_all);
5917 694127 : aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
5918 :
5919 :
5920 : /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
5921 : vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
5922 : def-use update cycles for the pointer: one relative to the outer-loop
5923 : (LOOP), which is what steps (3) and (4) below do. The other is relative
5924 : to the inner-loop (which is the inner-most loop containing the dataref),
5925 : and this is done be step (5) below.
5926 :
5927 : When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
5928 : inner-most loop, and so steps (3),(4) work the same, and step (5) is
5929 : redundant. Steps (3),(4) create the following:
5930 :
5931 : vp0 = &base_addr;
5932 : LOOP: vp1 = phi(vp0,vp2)
5933 : ...
5934 : ...
5935 : vp2 = vp1 + step
5936 : goto LOOP
5937 :
5938 : If there is an inner-loop nested in loop, then step (5) will also be
5939 : applied, and an additional update in the inner-loop will be created:
5940 :
5941 : vp0 = &base_addr;
5942 : LOOP: vp1 = phi(vp0,vp2)
5943 : ...
5944 : inner: vp3 = phi(vp1,vp4)
5945 : vp4 = vp3 + inner_step
5946 : if () goto inner
5947 : ...
5948 : vp2 = vp1 + step
5949 : if () goto LOOP */
5950 :
5951 : /* (2) Calculate the initial address of the aggregate-pointer, and set
5952 : the aggregate-pointer to point to it before the loop. */
5953 :
5954 : /* Create: (&(base[init_val]+offset) in the loop preheader. */
5955 :
5956 694127 : new_temp = vect_create_addr_base_for_vector_ref (vinfo,
5957 : stmt_info, &new_stmt_list,
5958 : offset);
5959 694127 : if (new_stmt_list)
5960 : {
5961 174960 : if (pe)
5962 : {
5963 54878 : new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
5964 54878 : gcc_assert (!new_bb);
5965 : }
5966 : else
5967 120082 : gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
5968 : }
5969 :
5970 694127 : *initial_address = new_temp;
5971 694127 : aggr_ptr_init = new_temp;
5972 :
5973 : /* (3) Handle the updating of the aggregate-pointer inside the loop.
5974 : This is needed when ONLY_INIT is false, and also when AT_LOOP is the
5975 : inner-loop nested in LOOP (during outer-loop vectorization). */
5976 :
5977 : /* No update in loop is required. */
5978 694127 : if (only_init && (!loop_vinfo || at_loop == loop))
5979 : aptr = aggr_ptr_init;
5980 : else
5981 : {
5982 : /* Accesses to invariant addresses should be handled specially
5983 : by the caller. */
5984 128397 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
5985 128397 : gcc_assert (!integer_zerop (step));
5986 :
5987 128397 : if (iv_step == NULL_TREE)
5988 : {
5989 : /* The step of the aggregate pointer is the type size,
5990 : negated for downward accesses. */
5991 0 : iv_step = TYPE_SIZE_UNIT (aggr_type);
5992 0 : if (tree_int_cst_sgn (step) == -1)
5993 0 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
5994 : }
5995 :
5996 128397 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5997 :
5998 128397 : create_iv (aggr_ptr_init, PLUS_EXPR,
5999 : iv_step, aggr_ptr, loop, &incr_gsi, insert_after,
6000 : &indx_before_incr, &indx_after_incr);
6001 128397 : incr = gsi_stmt (incr_gsi);
6002 :
6003 : /* Copy the points-to information if it exists. */
6004 128397 : if (DR_PTR_INFO (dr))
6005 : {
6006 69390 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
6007 69390 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
6008 : }
6009 128397 : if (ptr_incr)
6010 128397 : *ptr_incr = incr;
6011 :
6012 128397 : aptr = indx_before_incr;
6013 : }
6014 :
6015 694127 : if (!nested_in_vect_loop || only_init)
6016 : return aptr;
6017 :
6018 :
6019 : /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
6020 : nested in LOOP, if exists. */
6021 :
6022 336 : gcc_assert (nested_in_vect_loop);
6023 336 : if (!only_init)
6024 : {
6025 336 : standard_iv_increment_position (containing_loop, &incr_gsi,
6026 : &insert_after);
6027 336 : create_iv (aptr, PLUS_EXPR, DR_STEP (dr),
6028 : aggr_ptr, containing_loop, &incr_gsi, insert_after,
6029 : &indx_before_incr, &indx_after_incr);
6030 336 : incr = gsi_stmt (incr_gsi);
6031 :
6032 : /* Copy the points-to information if it exists. */
6033 336 : if (DR_PTR_INFO (dr))
6034 : {
6035 75 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
6036 75 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
6037 : }
6038 336 : if (ptr_incr)
6039 336 : *ptr_incr = incr;
6040 :
6041 336 : return indx_before_incr;
6042 : }
6043 : else
6044 : gcc_unreachable ();
6045 : }
6046 :
6047 :
6048 : /* Function bump_vector_ptr
6049 :
6050 : Increment a pointer (to a vector type) by vector-size. If requested,
6051 : i.e. if PTR-INCR is given, then also connect the new increment stmt
6052 : to the existing def-use update-chain of the pointer, by modifying
6053 : the PTR_INCR as illustrated below:
6054 :
6055 : The pointer def-use update-chain before this function:
6056 : DATAREF_PTR = phi (p_0, p_2)
6057 : ....
6058 : PTR_INCR: p_2 = DATAREF_PTR + step
6059 :
6060 : The pointer def-use update-chain after this function:
6061 : DATAREF_PTR = phi (p_0, p_2)
6062 : ....
6063 : NEW_DATAREF_PTR = DATAREF_PTR + BUMP
6064 : ....
6065 : PTR_INCR: p_2 = NEW_DATAREF_PTR + step
6066 :
6067 : Input:
6068 : DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
6069 : in the loop.
6070 : PTR_INCR - optional. The stmt that updates the pointer in each iteration of
6071 : the loop. The increment amount across iterations is expected
6072 : to be vector_size.
6073 : BSI - location where the new update stmt is to be placed.
6074 : STMT_INFO - the original scalar memory-access stmt that is being vectorized.
6075 : UPDATE - The offset by which to bump the pointer.
6076 :
6077 : Output: Return NEW_DATAREF_PTR as illustrated above.
6078 :
6079 : */
6080 :
6081 : tree
6082 237943 : bump_vector_ptr (vec_info *vinfo,
6083 : tree dataref_ptr, gimple *ptr_incr, gimple_stmt_iterator *gsi,
6084 : stmt_vec_info stmt_info, tree update)
6085 : {
6086 237943 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
6087 237943 : gimple *incr_stmt;
6088 237943 : ssa_op_iter iter;
6089 237943 : use_operand_p use_p;
6090 237943 : tree new_dataref_ptr;
6091 :
6092 237943 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
6093 111843 : new_dataref_ptr = copy_ssa_name (dataref_ptr);
6094 126100 : else if (is_gimple_min_invariant (dataref_ptr))
6095 : /* When possible avoid emitting a separate increment stmt that will
6096 : force the addressed object addressable. */
6097 252200 : return build1 (ADDR_EXPR, TREE_TYPE (dataref_ptr),
6098 126100 : fold_build2 (MEM_REF,
6099 : TREE_TYPE (TREE_TYPE (dataref_ptr)),
6100 : dataref_ptr,
6101 126100 : fold_convert (ptr_type_node, update)));
6102 : else
6103 0 : new_dataref_ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
6104 111843 : incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
6105 : dataref_ptr, update);
6106 111843 : vect_finish_stmt_generation (vinfo, stmt_info, incr_stmt, gsi);
6107 : /* Fold the increment, avoiding excessive chains use-def chains of
6108 : those, leading to compile-time issues for passes until the next
6109 : forwprop pass which would do this as well. */
6110 111843 : gimple_stmt_iterator fold_gsi = gsi_for_stmt (incr_stmt);
6111 111843 : if (fold_stmt (&fold_gsi, follow_all_ssa_edges))
6112 : {
6113 72375 : incr_stmt = gsi_stmt (fold_gsi);
6114 72375 : update_stmt (incr_stmt);
6115 : }
6116 :
6117 : /* Copy the points-to information if it exists. */
6118 111843 : if (DR_PTR_INFO (dr))
6119 : {
6120 73085 : duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
6121 73085 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
6122 : }
6123 :
6124 111843 : if (!ptr_incr)
6125 : return new_dataref_ptr;
6126 :
6127 : /* Update the vector-pointer's cross-iteration increment. */
6128 114516 : FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
6129 : {
6130 57258 : tree use = USE_FROM_PTR (use_p);
6131 :
6132 57258 : if (use == dataref_ptr)
6133 57258 : SET_USE (use_p, new_dataref_ptr);
6134 : else
6135 0 : gcc_assert (operand_equal_p (use, update, 0));
6136 : }
6137 :
6138 : return new_dataref_ptr;
6139 : }
6140 :
6141 :
6142 : /* Copy memory reference info such as base/clique from the SRC reference
6143 : to the DEST MEM_REF. */
6144 :
6145 : void
6146 944549 : vect_copy_ref_info (tree dest, tree src)
6147 : {
6148 944549 : if (TREE_CODE (dest) != MEM_REF)
6149 : return;
6150 :
6151 : tree src_base = src;
6152 1894984 : while (handled_component_p (src_base))
6153 955106 : src_base = TREE_OPERAND (src_base, 0);
6154 939878 : if (TREE_CODE (src_base) != MEM_REF
6155 939878 : && TREE_CODE (src_base) != TARGET_MEM_REF)
6156 : return;
6157 :
6158 512891 : MR_DEPENDENCE_CLIQUE (dest) = MR_DEPENDENCE_CLIQUE (src_base);
6159 512891 : MR_DEPENDENCE_BASE (dest) = MR_DEPENDENCE_BASE (src_base);
6160 : }
6161 :
6162 :
6163 : /* Function vect_create_destination_var.
6164 :
6165 : Create a new temporary of type VECTYPE. */
6166 :
6167 : tree
6168 530923 : vect_create_destination_var (tree scalar_dest, tree vectype)
6169 : {
6170 530923 : tree vec_dest;
6171 530923 : const char *name;
6172 530923 : char *new_name;
6173 530923 : tree type;
6174 530923 : enum vect_var_kind kind;
6175 :
6176 530923 : kind = vectype
6177 1038801 : ? VECTOR_BOOLEAN_TYPE_P (vectype)
6178 507878 : ? vect_mask_var
6179 : : vect_simple_var
6180 : : vect_scalar_var;
6181 23045 : type = vectype ? vectype : TREE_TYPE (scalar_dest);
6182 :
6183 530923 : gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
6184 :
6185 530923 : name = get_name (scalar_dest);
6186 530923 : if (name)
6187 187571 : new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
6188 : else
6189 343352 : new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
6190 530923 : vec_dest = vect_get_new_vect_var (type, kind, new_name);
6191 530923 : free (new_name);
6192 :
6193 530923 : return vec_dest;
6194 : }
6195 :
6196 : /* Function vect_grouped_store_supported.
6197 :
6198 : Returns TRUE if interleave high and interleave low permutations
6199 : are supported, and FALSE otherwise. */
6200 :
6201 : bool
6202 2507 : vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
6203 : {
6204 2507 : machine_mode mode = TYPE_MODE (vectype);
6205 :
6206 : /* vect_permute_store_chain requires the group size to be equal to 3 or
6207 : be a power of two. */
6208 2507 : if (count != 3 && exact_log2 (count) == -1)
6209 : {
6210 552 : if (dump_enabled_p ())
6211 11 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6212 : "the size of the group of accesses"
6213 : " is not a power of 2 or not eqaul to 3\n");
6214 552 : return false;
6215 : }
6216 :
6217 : /* Check that the permutation is supported. */
6218 1955 : if (VECTOR_MODE_P (mode))
6219 : {
6220 1955 : unsigned int i;
6221 1955 : if (count == 3)
6222 : {
6223 921 : unsigned int j0 = 0, j1 = 0, j2 = 0;
6224 921 : unsigned int i, j;
6225 :
6226 921 : unsigned int nelt;
6227 1842 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6228 : {
6229 : if (dump_enabled_p ())
6230 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6231 : "cannot handle groups of 3 stores for"
6232 : " variable-length vectors\n");
6233 : return false;
6234 : }
6235 :
6236 921 : vec_perm_builder sel (nelt, nelt, 1);
6237 921 : sel.quick_grow (nelt);
6238 921 : vec_perm_indices indices;
6239 3459 : for (j = 0; j < 3; j++)
6240 : {
6241 2613 : int nelt0 = ((3 - j) * nelt) % 3;
6242 2613 : int nelt1 = ((3 - j) * nelt + 1) % 3;
6243 2613 : int nelt2 = ((3 - j) * nelt + 2) % 3;
6244 9291 : for (i = 0; i < nelt; i++)
6245 : {
6246 6678 : if (3 * i + nelt0 < nelt)
6247 2264 : sel[3 * i + nelt0] = j0++;
6248 6678 : if (3 * i + nelt1 < nelt)
6249 2225 : sel[3 * i + nelt1] = nelt + j1++;
6250 6678 : if (3 * i + nelt2 < nelt)
6251 2189 : sel[3 * i + nelt2] = 0;
6252 : }
6253 2613 : indices.new_vector (sel, 2, nelt);
6254 2613 : if (!can_vec_perm_const_p (mode, mode, indices))
6255 : {
6256 66 : if (dump_enabled_p ())
6257 37 : dump_printf (MSG_MISSED_OPTIMIZATION,
6258 : "permutation op not supported by target.\n");
6259 66 : return false;
6260 : }
6261 :
6262 8649 : for (i = 0; i < nelt; i++)
6263 : {
6264 6102 : if (3 * i + nelt0 < nelt)
6265 2040 : sel[3 * i + nelt0] = 3 * i + nelt0;
6266 6102 : if (3 * i + nelt1 < nelt)
6267 2031 : sel[3 * i + nelt1] = 3 * i + nelt1;
6268 6102 : if (3 * i + nelt2 < nelt)
6269 2031 : sel[3 * i + nelt2] = nelt + j2++;
6270 : }
6271 2547 : indices.new_vector (sel, 2, nelt);
6272 2547 : if (!can_vec_perm_const_p (mode, mode, indices))
6273 : {
6274 9 : if (dump_enabled_p ())
6275 9 : dump_printf (MSG_MISSED_OPTIMIZATION,
6276 : "permutation op not supported by target.\n");
6277 9 : return false;
6278 : }
6279 : }
6280 : return true;
6281 921 : }
6282 : else
6283 : {
6284 : /* If length is not equal to 3 then only power of 2 is supported. */
6285 1034 : gcc_assert (pow2p_hwi (count));
6286 2068 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6287 :
6288 : /* The encoding has 2 interleaved stepped patterns. */
6289 2068 : if(!multiple_p (nelt, 2))
6290 986 : return false;
6291 1034 : vec_perm_builder sel (nelt, 2, 3);
6292 1034 : sel.quick_grow (6);
6293 5170 : for (i = 0; i < 3; i++)
6294 : {
6295 3102 : sel[i * 2] = i;
6296 3102 : sel[i * 2 + 1] = i + nelt;
6297 : }
6298 1034 : vec_perm_indices indices (sel, 2, nelt);
6299 1034 : if (can_vec_perm_const_p (mode, mode, indices))
6300 : {
6301 6902 : for (i = 0; i < 6; i++)
6302 5916 : sel[i] += exact_div (nelt, 2);
6303 986 : indices.new_vector (sel, 2, nelt);
6304 986 : if (can_vec_perm_const_p (mode, mode, indices))
6305 986 : return true;
6306 : }
6307 1034 : }
6308 : }
6309 :
6310 48 : if (dump_enabled_p ())
6311 3 : dump_printf (MSG_MISSED_OPTIMIZATION,
6312 : "permutation op not supported by target.\n");
6313 : return false;
6314 : }
6315 :
6316 : /* Return FN if vec_{mask_,mask_len_}store_lanes is available for COUNT vectors
6317 : of type VECTYPE. MASKED_P says whether the masked form is needed. */
6318 :
6319 : internal_fn
6320 32536 : vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6321 : bool masked_p)
6322 : {
6323 32536 : if (vect_lanes_optab_supported_p ("vec_mask_len_store_lanes",
6324 : vec_mask_len_store_lanes_optab, vectype,
6325 : count))
6326 : return IFN_MASK_LEN_STORE_LANES;
6327 32536 : else if (masked_p)
6328 : {
6329 159 : if (vect_lanes_optab_supported_p ("vec_mask_store_lanes",
6330 : vec_mask_store_lanes_optab, vectype,
6331 : count))
6332 : return IFN_MASK_STORE_LANES;
6333 : }
6334 : else
6335 : {
6336 32377 : if (vect_lanes_optab_supported_p ("vec_store_lanes",
6337 : vec_store_lanes_optab, vectype, count))
6338 : return IFN_STORE_LANES;
6339 : }
6340 : return IFN_LAST;
6341 : }
6342 :
6343 :
6344 : /* Function vect_setup_realignment
6345 :
6346 : This function is called when vectorizing an unaligned load using
6347 : the dr_explicit_realign[_optimized] scheme.
6348 : This function generates the following code at the loop prolog:
6349 :
6350 : p = initial_addr;
6351 : x msq_init = *(floor(p)); # prolog load
6352 : realignment_token = call target_builtin;
6353 : loop:
6354 : x msq = phi (msq_init, ---)
6355 :
6356 : The stmts marked with x are generated only for the case of
6357 : dr_explicit_realign_optimized.
6358 :
6359 : The code above sets up a new (vector) pointer, pointing to the first
6360 : location accessed by STMT_INFO, and a "floor-aligned" load using that
6361 : pointer. It also generates code to compute the "realignment-token"
6362 : (if the relevant target hook was defined), and creates a phi-node at the
6363 : loop-header bb whose arguments are the result of the prolog-load (created
6364 : by this function) and the result of a load that takes place in the loop
6365 : (to be created by the caller to this function).
6366 :
6367 : For the case of dr_explicit_realign_optimized:
6368 : The caller to this function uses the phi-result (msq) to create the
6369 : realignment code inside the loop, and sets up the missing phi argument,
6370 : as follows:
6371 : loop:
6372 : msq = phi (msq_init, lsq)
6373 : lsq = *(floor(p')); # load in loop
6374 : result = realign_load (msq, lsq, realignment_token);
6375 :
6376 : For the case of dr_explicit_realign:
6377 : loop:
6378 : msq = *(floor(p)); # load in loop
6379 : p' = p + (VS-1);
6380 : lsq = *(floor(p')); # load in loop
6381 : result = realign_load (msq, lsq, realignment_token);
6382 :
6383 : Input:
6384 : STMT_INFO - (scalar) load stmt to be vectorized. This load accesses
6385 : a memory location that may be unaligned.
6386 : BSI - place where new code is to be inserted.
6387 : ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
6388 : is used.
6389 :
6390 : Output:
6391 : REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
6392 : target hook, if defined.
6393 : Return value - the result of the loop-header phi node. */
6394 :
6395 : tree
6396 0 : vect_setup_realignment (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
6397 : gimple_stmt_iterator *gsi, tree *realignment_token,
6398 : enum dr_alignment_support alignment_support_scheme,
6399 : tree init_addr,
6400 : class loop **at_loop)
6401 : {
6402 0 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6403 0 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
6404 0 : struct data_reference *dr = dr_info->dr;
6405 0 : class loop *loop = NULL;
6406 0 : edge pe = NULL;
6407 0 : tree scalar_dest = gimple_assign_lhs (stmt_info->stmt);
6408 0 : tree vec_dest;
6409 0 : gimple *inc;
6410 0 : tree ptr;
6411 0 : tree data_ref;
6412 0 : basic_block new_bb;
6413 0 : tree msq_init = NULL_TREE;
6414 0 : tree new_temp;
6415 0 : gphi *phi_stmt;
6416 0 : tree msq = NULL_TREE;
6417 0 : gimple_seq stmts = NULL;
6418 0 : bool compute_in_loop = false;
6419 0 : bool nested_in_vect_loop = false;
6420 0 : class loop *containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
6421 0 : class loop *loop_for_initial_load = NULL;
6422 :
6423 0 : if (loop_vinfo)
6424 : {
6425 0 : loop = LOOP_VINFO_LOOP (loop_vinfo);
6426 0 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
6427 : }
6428 :
6429 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign
6430 : || alignment_support_scheme == dr_explicit_realign_optimized);
6431 :
6432 : /* We need to generate three things:
6433 : 1. the misalignment computation
6434 : 2. the extra vector load (for the optimized realignment scheme).
6435 : 3. the phi node for the two vectors from which the realignment is
6436 : done (for the optimized realignment scheme). */
6437 :
6438 : /* 1. Determine where to generate the misalignment computation.
6439 :
6440 : If INIT_ADDR is NULL_TREE, this indicates that the misalignment
6441 : calculation will be generated by this function, outside the loop (in the
6442 : preheader). Otherwise, INIT_ADDR had already been computed for us by the
6443 : caller, inside the loop.
6444 :
6445 : Background: If the misalignment remains fixed throughout the iterations of
6446 : the loop, then both realignment schemes are applicable, and also the
6447 : misalignment computation can be done outside LOOP. This is because we are
6448 : vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
6449 : are a multiple of VS (the Vector Size), and therefore the misalignment in
6450 : different vectorized LOOP iterations is always the same.
6451 : The problem arises only if the memory access is in an inner-loop nested
6452 : inside LOOP, which is now being vectorized using outer-loop vectorization.
6453 : This is the only case when the misalignment of the memory access may not
6454 : remain fixed throughout the iterations of the inner-loop (as explained in
6455 : detail in vect_supportable_dr_alignment). In this case, not only is the
6456 : optimized realignment scheme not applicable, but also the misalignment
6457 : computation (and generation of the realignment token that is passed to
6458 : REALIGN_LOAD) have to be done inside the loop.
6459 :
6460 : In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
6461 : or not, which in turn determines if the misalignment is computed inside
6462 : the inner-loop, or outside LOOP. */
6463 :
6464 0 : if (init_addr != NULL_TREE || !loop_vinfo)
6465 : {
6466 0 : compute_in_loop = true;
6467 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign);
6468 : }
6469 :
6470 :
6471 : /* 2. Determine where to generate the extra vector load.
6472 :
6473 : For the optimized realignment scheme, instead of generating two vector
6474 : loads in each iteration, we generate a single extra vector load in the
6475 : preheader of the loop, and in each iteration reuse the result of the
6476 : vector load from the previous iteration. In case the memory access is in
6477 : an inner-loop nested inside LOOP, which is now being vectorized using
6478 : outer-loop vectorization, we need to determine whether this initial vector
6479 : load should be generated at the preheader of the inner-loop, or can be
6480 : generated at the preheader of LOOP. If the memory access has no evolution
6481 : in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
6482 : to be generated inside LOOP (in the preheader of the inner-loop). */
6483 :
6484 0 : if (nested_in_vect_loop)
6485 : {
6486 0 : tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
6487 0 : bool invariant_in_outerloop =
6488 0 : (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
6489 0 : loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
6490 : }
6491 : else
6492 : loop_for_initial_load = loop;
6493 0 : if (at_loop)
6494 0 : *at_loop = loop_for_initial_load;
6495 :
6496 0 : tree vuse = NULL_TREE;
6497 0 : if (loop_for_initial_load)
6498 : {
6499 0 : pe = loop_preheader_edge (loop_for_initial_load);
6500 0 : if (gphi *vphi = get_virtual_phi (loop_for_initial_load->header))
6501 0 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
6502 : }
6503 0 : if (!vuse)
6504 0 : vuse = gimple_vuse (gsi_stmt (*gsi));
6505 :
6506 : /* 3. For the case of the optimized realignment, create the first vector
6507 : load at the loop preheader. */
6508 :
6509 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
6510 : {
6511 : /* Create msq_init = *(floor(p1)) in the loop preheader */
6512 0 : gassign *new_stmt;
6513 :
6514 0 : gcc_assert (!compute_in_loop);
6515 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6516 0 : ptr = vect_create_data_ref_ptr (vinfo, stmt_info, vectype,
6517 : loop_for_initial_load, NULL_TREE,
6518 : &init_addr, NULL, &inc, true);
6519 0 : if (TREE_CODE (ptr) == SSA_NAME)
6520 0 : new_temp = copy_ssa_name (ptr);
6521 : else
6522 0 : new_temp = make_ssa_name (TREE_TYPE (ptr));
6523 0 : poly_uint64 align = DR_TARGET_ALIGNMENT (dr_info);
6524 0 : tree type = TREE_TYPE (ptr);
6525 0 : new_stmt = gimple_build_assign
6526 0 : (new_temp, BIT_AND_EXPR, ptr,
6527 0 : fold_build2 (MINUS_EXPR, type,
6528 : build_int_cst (type, 0),
6529 : build_int_cst (type, align)));
6530 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6531 0 : gcc_assert (!new_bb);
6532 0 : data_ref
6533 0 : = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
6534 : build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
6535 0 : vect_copy_ref_info (data_ref, DR_REF (dr));
6536 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
6537 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6538 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6539 0 : gimple_set_vuse (new_stmt, vuse);
6540 0 : if (pe)
6541 : {
6542 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6543 0 : gcc_assert (!new_bb);
6544 : }
6545 : else
6546 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6547 :
6548 0 : msq_init = gimple_assign_lhs (new_stmt);
6549 : }
6550 :
6551 : /* 4. Create realignment token using a target builtin, if available.
6552 : It is done either inside the containing loop, or before LOOP (as
6553 : determined above). */
6554 :
6555 0 : if (targetm.vectorize.builtin_mask_for_load)
6556 : {
6557 0 : gcall *new_stmt;
6558 0 : tree builtin_decl;
6559 :
6560 : /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
6561 0 : if (!init_addr)
6562 : {
6563 : /* Generate the INIT_ADDR computation outside LOOP. */
6564 0 : init_addr = vect_create_addr_base_for_vector_ref (vinfo,
6565 : stmt_info, &stmts,
6566 : NULL_TREE);
6567 0 : if (loop)
6568 : {
6569 0 : pe = loop_preheader_edge (loop);
6570 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6571 0 : gcc_assert (!new_bb);
6572 : }
6573 : else
6574 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6575 : }
6576 :
6577 0 : builtin_decl = targetm.vectorize.builtin_mask_for_load ();
6578 0 : new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
6579 0 : vec_dest =
6580 0 : vect_create_destination_var (scalar_dest,
6581 : gimple_call_return_type (new_stmt));
6582 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6583 0 : gimple_call_set_lhs (new_stmt, new_temp);
6584 :
6585 0 : if (compute_in_loop)
6586 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6587 : else
6588 : {
6589 : /* Generate the misalignment computation outside LOOP. */
6590 0 : pe = loop_preheader_edge (loop);
6591 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6592 0 : gcc_assert (!new_bb);
6593 : }
6594 :
6595 0 : *realignment_token = gimple_call_lhs (new_stmt);
6596 :
6597 : /* The result of the CALL_EXPR to this builtin is determined from
6598 : the value of the parameter and no global variables are touched
6599 : which makes the builtin a "const" function. Requiring the
6600 : builtin to have the "const" attribute makes it unnecessary
6601 : to call mark_call_clobbered. */
6602 0 : gcc_assert (TREE_READONLY (builtin_decl));
6603 : }
6604 :
6605 0 : if (alignment_support_scheme == dr_explicit_realign)
6606 : return msq;
6607 :
6608 0 : gcc_assert (!compute_in_loop);
6609 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
6610 :
6611 :
6612 : /* 5. Create msq = phi <msq_init, lsq> in loop */
6613 :
6614 0 : pe = loop_preheader_edge (containing_loop);
6615 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6616 0 : msq = make_ssa_name (vec_dest);
6617 0 : phi_stmt = create_phi_node (msq, containing_loop->header);
6618 0 : add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
6619 :
6620 0 : return msq;
6621 : }
6622 :
6623 :
6624 : /* Function vect_grouped_load_supported.
6625 :
6626 : COUNT is the size of the load group (the number of statements plus the
6627 : number of gaps). SINGLE_ELEMENT_P is true if there is actually
6628 : only one statement, with a gap of COUNT - 1.
6629 :
6630 : Returns true if a suitable permute exists. */
6631 :
6632 : bool
6633 1681 : vect_grouped_load_supported (tree vectype, bool single_element_p,
6634 : unsigned HOST_WIDE_INT count)
6635 : {
6636 1681 : machine_mode mode = TYPE_MODE (vectype);
6637 :
6638 : /* If this is single-element interleaving with an element distance
6639 : that leaves unused vector loads around punt - we at least create
6640 : very sub-optimal code in that case (and blow up memory,
6641 : see PR65518). */
6642 1681 : if (single_element_p && maybe_gt (count, TYPE_VECTOR_SUBPARTS (vectype)))
6643 : {
6644 23 : if (dump_enabled_p ())
6645 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6646 : "single-element interleaving not supported "
6647 : "for not adjacent vector loads\n");
6648 23 : return false;
6649 : }
6650 :
6651 : /* vect_permute_load_chain requires the group size to be equal to 3 or
6652 : be a power of two. */
6653 1658 : if (count != 3 && exact_log2 (count) == -1)
6654 : {
6655 222 : if (dump_enabled_p ())
6656 14 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6657 : "the size of the group of accesses"
6658 : " is not a power of 2 or not equal to 3\n");
6659 222 : return false;
6660 : }
6661 :
6662 : /* Check that the permutation is supported. */
6663 1436 : if (VECTOR_MODE_P (mode))
6664 : {
6665 1436 : unsigned int i, j;
6666 1436 : if (count == 3)
6667 : {
6668 695 : unsigned int nelt;
6669 1390 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6670 : {
6671 : if (dump_enabled_p ())
6672 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6673 : "cannot handle groups of 3 loads for"
6674 : " variable-length vectors\n");
6675 : return false;
6676 : }
6677 :
6678 695 : vec_perm_builder sel (nelt, nelt, 1);
6679 695 : sel.quick_grow (nelt);
6680 695 : vec_perm_indices indices;
6681 695 : unsigned int k;
6682 2744 : for (k = 0; k < 3; k++)
6683 : {
6684 7385 : for (i = 0; i < nelt; i++)
6685 5324 : if (3 * i + k < 2 * nelt)
6686 3555 : sel[i] = 3 * i + k;
6687 : else
6688 1769 : sel[i] = 0;
6689 2061 : indices.new_vector (sel, 2, nelt);
6690 2061 : if (!can_vec_perm_const_p (mode, mode, indices))
6691 : {
6692 12 : if (dump_enabled_p ())
6693 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6694 : "shuffle of 3 loads is not supported by"
6695 : " target\n");
6696 12 : return false;
6697 : }
6698 7221 : for (i = 0, j = 0; i < nelt; i++)
6699 5172 : if (3 * i + k < 2 * nelt)
6700 3448 : sel[i] = i;
6701 : else
6702 1724 : sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
6703 2049 : indices.new_vector (sel, 2, nelt);
6704 2049 : if (!can_vec_perm_const_p (mode, mode, indices))
6705 : {
6706 0 : if (dump_enabled_p ())
6707 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6708 : "shuffle of 3 loads is not supported by"
6709 : " target\n");
6710 0 : return false;
6711 : }
6712 : }
6713 : return true;
6714 695 : }
6715 : else
6716 : {
6717 : /* If length is not equal to 3 then only power of 2 is supported. */
6718 741 : gcc_assert (pow2p_hwi (count));
6719 1482 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6720 :
6721 : /* The encoding has a single stepped pattern. */
6722 741 : vec_perm_builder sel (nelt, 1, 3);
6723 741 : sel.quick_grow (3);
6724 3705 : for (i = 0; i < 3; i++)
6725 2223 : sel[i] = i * 2;
6726 741 : vec_perm_indices indices (sel, 2, nelt);
6727 741 : if (can_vec_perm_const_p (mode, mode, indices))
6728 : {
6729 2956 : for (i = 0; i < 3; i++)
6730 2217 : sel[i] = i * 2 + 1;
6731 739 : indices.new_vector (sel, 2, nelt);
6732 739 : if (can_vec_perm_const_p (mode, mode, indices))
6733 739 : return true;
6734 : }
6735 741 : }
6736 : }
6737 :
6738 2 : if (dump_enabled_p ())
6739 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6740 : "extract even/odd not supported by target\n");
6741 : return false;
6742 : }
6743 :
6744 : /* Return FN if vec_{masked_,mask_len_}load_lanes is available for COUNT vectors
6745 : of type VECTYPE. MASKED_P says whether the masked form is needed.
6746 : If it is available and ELSVALS is nonzero store the possible else values
6747 : in the vector it points to. */
6748 :
6749 : internal_fn
6750 139308 : vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6751 : bool masked_p, vec<int> *elsvals)
6752 : {
6753 139308 : if (vect_lanes_optab_supported_p ("vec_mask_len_load_lanes",
6754 : vec_mask_len_load_lanes_optab, vectype,
6755 : count, elsvals))
6756 : return IFN_MASK_LEN_LOAD_LANES;
6757 139308 : else if (masked_p)
6758 : {
6759 30 : if (vect_lanes_optab_supported_p ("vec_mask_load_lanes",
6760 : vec_mask_load_lanes_optab, vectype,
6761 : count, elsvals))
6762 : return IFN_MASK_LOAD_LANES;
6763 : }
6764 : else
6765 : {
6766 139278 : if (vect_lanes_optab_supported_p ("vec_load_lanes", vec_load_lanes_optab,
6767 : vectype, count, elsvals))
6768 : return IFN_LOAD_LANES;
6769 : }
6770 : return IFN_LAST;
6771 : }
6772 :
6773 : /* Function vect_force_dr_alignment_p.
6774 :
6775 : Returns whether the alignment of a DECL can be forced to be aligned
6776 : on ALIGNMENT bit boundary. */
6777 :
6778 : bool
6779 663335 : vect_can_force_dr_alignment_p (const_tree decl, poly_uint64 alignment)
6780 : {
6781 663335 : if (!VAR_P (decl))
6782 : return false;
6783 :
6784 208968 : if (decl_in_symtab_p (decl)
6785 208968 : && (!symtab_node::get (decl)
6786 21443 : || !symtab_node::get (decl)->can_increase_alignment_p ()))
6787 12918 : return false;
6788 :
6789 196050 : if (TREE_STATIC (decl))
6790 8525 : return (known_le (alignment,
6791 8525 : (unsigned HOST_WIDE_INT) MAX_OFILE_ALIGNMENT));
6792 : else
6793 187525 : return (known_le (alignment, (unsigned HOST_WIDE_INT) MAX_STACK_ALIGNMENT));
6794 : }
6795 :
6796 : /* Return whether the data reference DR_INFO is supported with respect to its
6797 : alignment.
6798 : If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
6799 : it is aligned, i.e., check if it is possible to vectorize it with different
6800 : alignment. If IS_GATHER_SCATTER is true we are dealing with a
6801 : gather/scatter. */
6802 :
6803 : enum dr_alignment_support
6804 2395267 : vect_supportable_dr_alignment (vec_info *vinfo, dr_vec_info *dr_info,
6805 : tree vectype, int misalignment,
6806 : bool is_gather_scatter)
6807 : {
6808 2395267 : data_reference *dr = dr_info->dr;
6809 2395267 : stmt_vec_info stmt_info = dr_info->stmt;
6810 2395267 : machine_mode mode = TYPE_MODE (vectype);
6811 2395267 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6812 2395267 : class loop *vect_loop = NULL;
6813 2395267 : bool nested_in_vect_loop = false;
6814 :
6815 2395267 : if (misalignment == 0)
6816 : return dr_aligned;
6817 1477526 : else if (dr_safe_speculative_read_required (stmt_info))
6818 : return dr_unaligned_unsupported;
6819 :
6820 1096133 : if (loop_vinfo)
6821 : {
6822 688874 : vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
6823 688874 : nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt_info);
6824 : }
6825 :
6826 : /* Possibly unaligned access. */
6827 :
6828 : /* We can choose between using the implicit realignment scheme (generating
6829 : a misaligned_move stmt) and the explicit realignment scheme (generating
6830 : aligned loads with a REALIGN_LOAD). There are two variants to the
6831 : explicit realignment scheme: optimized, and unoptimized.
6832 : We can optimize the realignment only if the step between consecutive
6833 : vector loads is equal to the vector size. Since the vector memory
6834 : accesses advance in steps of VS (Vector Size) in the vectorized loop, it
6835 : is guaranteed that the misalignment amount remains the same throughout the
6836 : execution of the vectorized loop. Therefore, we can create the
6837 : "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
6838 : at the loop preheader.
6839 :
6840 : However, in the case of outer-loop vectorization, when vectorizing a
6841 : memory access in the inner-loop nested within the LOOP that is now being
6842 : vectorized, while it is guaranteed that the misalignment of the
6843 : vectorized memory access will remain the same in different outer-loop
6844 : iterations, it is *not* guaranteed that is will remain the same throughout
6845 : the execution of the inner-loop. This is because the inner-loop advances
6846 : with the original scalar step (and not in steps of VS). If the inner-loop
6847 : step happens to be a multiple of VS, then the misalignment remains fixed
6848 : and we can use the optimized realignment scheme. For example:
6849 :
6850 : for (i=0; i<N; i++)
6851 : for (j=0; j<M; j++)
6852 : s += a[i+j];
6853 :
6854 : When vectorizing the i-loop in the above example, the step between
6855 : consecutive vector loads is 1, and so the misalignment does not remain
6856 : fixed across the execution of the inner-loop, and the realignment cannot
6857 : be optimized (as illustrated in the following pseudo vectorized loop):
6858 :
6859 : for (i=0; i<N; i+=4)
6860 : for (j=0; j<M; j++){
6861 : vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
6862 : // when j is {0,1,2,3,4,5,6,7,...} respectively.
6863 : // (assuming that we start from an aligned address).
6864 : }
6865 :
6866 : We therefore have to use the unoptimized realignment scheme:
6867 :
6868 : for (i=0; i<N; i+=4)
6869 : for (j=k; j<M; j+=4)
6870 : vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
6871 : // that the misalignment of the initial address is
6872 : // 0).
6873 :
6874 : The loop can then be vectorized as follows:
6875 :
6876 : for (k=0; k<4; k++){
6877 : rt = get_realignment_token (&vp[k]);
6878 : for (i=0; i<N; i+=4){
6879 : v1 = vp[i+k];
6880 : for (j=k; j<M; j+=4){
6881 : v2 = vp[i+j+VS-1];
6882 : va = REALIGN_LOAD <v1,v2,rt>;
6883 : vs += va;
6884 : v1 = v2;
6885 : }
6886 : }
6887 : } */
6888 :
6889 1096133 : if (DR_IS_READ (dr) && !is_gather_scatter)
6890 : {
6891 446852 : if (can_implement_p (vec_realign_load_optab, mode)
6892 446852 : && (!targetm.vectorize.builtin_mask_for_load
6893 0 : || targetm.vectorize.builtin_mask_for_load ()))
6894 : {
6895 : /* If we are doing SLP then the accesses need not have the
6896 : same alignment, instead it depends on the SLP group size. */
6897 0 : if (loop_vinfo
6898 0 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
6899 0 : && !multiple_p (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6900 0 : * (DR_GROUP_SIZE
6901 0 : (DR_GROUP_FIRST_ELEMENT (stmt_info))),
6902 0 : TYPE_VECTOR_SUBPARTS (vectype)))
6903 : ;
6904 0 : else if (!loop_vinfo
6905 0 : || (nested_in_vect_loop
6906 0 : && maybe_ne (TREE_INT_CST_LOW (DR_STEP (dr)),
6907 0 : GET_MODE_SIZE (TYPE_MODE (vectype)))))
6908 0 : return dr_explicit_realign;
6909 : else
6910 0 : return dr_explicit_realign_optimized;
6911 : }
6912 : }
6913 :
6914 1096133 : bool is_packed = not_size_aligned (DR_REF (dr));
6915 1096133 : if (misalignment == DR_MISALIGNMENT_UNKNOWN
6916 1096133 : && is_gather_scatter)
6917 3069 : misalignment = (get_object_alignment (DR_REF (dr))
6918 3069 : % (GET_MODE_BITSIZE (GET_MODE_INNER (mode))))
6919 3069 : / BITS_PER_UNIT;
6920 1096133 : if (targetm.vectorize.support_vector_misalignment (mode, misalignment,
6921 : is_packed,
6922 : is_gather_scatter))
6923 : return dr_unaligned_supported;
6924 :
6925 : /* Unsupported. */
6926 : return dr_unaligned_unsupported;
6927 : }
|