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