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