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 371466 : 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 371466 : machine_mode mode, array_mode;
73 371466 : bool limit_p;
74 :
75 371466 : mode = TYPE_MODE (vectype);
76 371466 : if (!targetm.array_mode (mode, count).exists (&array_mode))
77 : {
78 742932 : poly_uint64 bits = count * GET_MODE_BITSIZE (mode);
79 371466 : limit_p = !targetm.array_mode_supported_p (mode, count);
80 371466 : if (!int_mode_for_size (bits, limit_p).exists (&array_mode))
81 : {
82 318614 : if (dump_enabled_p ())
83 12894 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
84 : "no array mode for %s[%wu]\n",
85 12894 : GET_MODE_NAME (mode), count);
86 318614 : return false;
87 : }
88 : }
89 :
90 52852 : enum insn_code icode;
91 52852 : if ((icode = convert_optab_handler (optab, array_mode, mode))
92 : == CODE_FOR_nothing)
93 : {
94 52852 : if (dump_enabled_p ())
95 4176 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
96 : "cannot use %s<%s><%s>\n", name,
97 4176 : GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
98 52852 : 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 624315 : simd_clone_call_p (gimple *stmt)
120 : {
121 700667 : gcall *call = dyn_cast <gcall *> (stmt);
122 78024 : if (!call)
123 : return NULL;
124 :
125 78024 : tree fndecl = NULL_TREE;
126 78024 : if (gimple_call_internal_p (call, IFN_MASK_CALL))
127 226 : fndecl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
128 : else
129 77798 : fndecl = gimple_call_fndecl (stmt);
130 :
131 78024 : if (fndecl == NULL_TREE)
132 : return NULL;
133 :
134 35380 : cgraph_node *node = cgraph_node::get (fndecl);
135 35380 : if (node && node->simd_clones != NULL)
136 : return node;
137 :
138 : return NULL;
139 : }
140 :
141 :
142 :
143 : /* Return the smallest scalar part of STMT_INFO.
144 : This is used to determine the vectype of the stmt. We generally set the
145 : vectype according to the type of the result (lhs). For stmts whose
146 : result-type is different than the type of the arguments (e.g., demotion,
147 : promotion), vectype will be reset appropriately (later). Note that we have
148 : to visit the smallest datatype in this function, because that determines the
149 : VF. If the smallest datatype in the loop is present only as the rhs of a
150 : promotion operation - we'd miss it.
151 : Such a case, where a variable of this datatype does not appear in the lhs
152 : anywhere in the loop, can only occur if it's an invariant: e.g.:
153 : 'int_x = (int) short_inv', which we'd expect to have been optimized away by
154 : invariant motion. However, we cannot rely on invariant motion to always
155 : take invariants out of the loop, and so in the case of promotion we also
156 : have to check the rhs.
157 : LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
158 : types. */
159 :
160 : tree
161 5131442 : vect_get_smallest_scalar_type (stmt_vec_info stmt_info, tree scalar_type)
162 : {
163 5131442 : 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 5131442 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (scalar_type)))
168 : return scalar_type;
169 :
170 5131442 : lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
171 :
172 5131442 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
173 5131442 : if (assign)
174 : {
175 4507127 : scalar_type = TREE_TYPE (gimple_assign_lhs (assign));
176 4507127 : if (gimple_assign_cast_p (assign)
177 4099545 : || gimple_assign_rhs_code (assign) == DOT_PROD_EXPR
178 4098903 : || gimple_assign_rhs_code (assign) == WIDEN_SUM_EXPR
179 4098903 : || gimple_assign_rhs_code (assign) == SAD_EXPR
180 4098794 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_EXPR
181 4094826 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_PLUS_EXPR
182 4094826 : || gimple_assign_rhs_code (assign) == WIDEN_MULT_MINUS_EXPR
183 4094826 : || gimple_assign_rhs_code (assign) == WIDEN_LSHIFT_EXPR
184 8601953 : || gimple_assign_rhs_code (assign) == FLOAT_EXPR)
185 : {
186 426874 : tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (assign));
187 :
188 426874 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
189 426874 : if (rhs < lhs)
190 5131442 : scalar_type = rhs_type;
191 : }
192 : }
193 624315 : else if (cgraph_node *node = simd_clone_call_p (stmt_info->stmt))
194 : {
195 1672 : auto clone = node->simd_clones->simdclone;
196 5096 : for (unsigned int i = 0; i < clone->nargs; ++i)
197 : {
198 3424 : if (clone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
199 : {
200 1971 : tree arg_scalar_type = TREE_TYPE (clone->args[i].vector_type);
201 1971 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (arg_scalar_type));
202 1971 : if (rhs < lhs)
203 : {
204 3424 : scalar_type = arg_scalar_type;
205 3424 : lhs = rhs;
206 : }
207 : }
208 : }
209 : }
210 622643 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
211 : {
212 76352 : unsigned int i = 0;
213 76352 : if (gimple_call_internal_p (call))
214 : {
215 40293 : internal_fn ifn = gimple_call_internal_fn (call);
216 40293 : if (internal_load_fn_p (ifn))
217 : /* For loads the LHS type does the trick. */
218 : i = ~0U;
219 35487 : else if (internal_store_fn_p (ifn))
220 : {
221 : /* For stores use the type of the stored value. */
222 2659 : i = internal_fn_stored_value_index (ifn);
223 2659 : scalar_type = TREE_TYPE (gimple_call_arg (call, i));
224 2659 : i = ~0U;
225 : }
226 32828 : else if (internal_fn_mask_index (ifn) == 0)
227 11080 : i = 1;
228 : }
229 76352 : if (i < gimple_call_num_args (call))
230 : {
231 66962 : tree rhs_type = TREE_TYPE (gimple_call_arg (call, i));
232 66962 : if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (rhs_type)))
233 : {
234 66962 : rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
235 66962 : if (rhs < lhs)
236 5131442 : 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 178231 : vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
251 : {
252 178231 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
253 :
254 178231 : 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 178177 : opt_result res
261 178177 : = runtime_alias_check_p (ddr, loop,
262 178177 : optimize_loop_nest_for_speed_p (loop));
263 178177 : if (!res)
264 143 : return res;
265 :
266 178034 : LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
267 178034 : 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 145730 : vect_preserves_scalar_order_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b)
293 : {
294 145730 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
295 145730 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
296 :
297 : /* Single statements are always kept in their original order. */
298 145730 : if (!STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
299 241958 : && !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 26222 : if ((DR_IS_READ (dr_info_a->dr) && integer_zerop (DR_STEP (dr_info_a->dr)))
305 81174 : || (DR_IS_READ (dr_info_b->dr) && integer_zerop (DR_STEP (dr_info_b->dr))))
306 1726 : 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 54461 : stmt_vec_info il_a = DR_GROUP_FIRST_ELEMENT (stmtinfo_a);
314 54461 : if (il_a)
315 : {
316 49011 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_a)))
317 213628 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
318 188243 : s = DR_GROUP_NEXT_ELEMENT (s))
319 188243 : il_a = get_later_stmt (il_a, s);
320 : else /* DR_IS_READ */
321 93800 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_a); s;
322 70174 : s = DR_GROUP_NEXT_ELEMENT (s))
323 70174 : if (get_later_stmt (il_a, s) == il_a)
324 2144 : il_a = s;
325 : }
326 : else
327 : il_a = stmtinfo_a;
328 54461 : stmt_vec_info il_b = DR_GROUP_FIRST_ELEMENT (stmtinfo_b);
329 54461 : if (il_b)
330 : {
331 47595 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmtinfo_b)))
332 274437 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
333 236145 : s = DR_GROUP_NEXT_ELEMENT (s))
334 236145 : il_b = get_later_stmt (il_b, s);
335 : else /* DR_IS_READ */
336 42785 : for (stmt_vec_info s = DR_GROUP_NEXT_ELEMENT (il_b); s;
337 33482 : s = DR_GROUP_NEXT_ELEMENT (s))
338 33482 : if (get_later_stmt (il_b, s) == il_b)
339 327 : il_b = s;
340 : }
341 : else
342 : il_b = stmtinfo_b;
343 54461 : bool a_after_b = (get_later_stmt (stmtinfo_a, stmtinfo_b) == stmtinfo_a);
344 54461 : 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 8472 : 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 8472 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
364 33906 : for (lambda_vector &dist_v : DDR_DIST_VECTS (ddr))
365 : {
366 16679 : int dist = dist_v[loop_depth];
367 16679 : 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 8221 : 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 8189 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
393 8189 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
394 8189 : return (!STMT_VINFO_GATHER_SCATTER_P (dr_info_a->stmt)
395 8189 : && !STMT_VINFO_GATHER_SCATTER_P (dr_info_b->stmt)
396 16386 : && 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 1502936 : vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
414 : loop_vec_info loop_vinfo,
415 : unsigned int *max_vf)
416 : {
417 1502936 : unsigned int i;
418 1502936 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
419 1502936 : struct data_reference *dra = DDR_A (ddr);
420 1502936 : struct data_reference *drb = DDR_B (ddr);
421 1502936 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (dra);
422 1502936 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (drb);
423 1502936 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
424 1502936 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
425 1502936 : lambda_vector dist_v;
426 1502936 : unsigned int loop_depth;
427 :
428 : /* If user asserted safelen consecutive iterations can be
429 : executed concurrently, assume independence. */
430 1691845 : auto apply_safelen = [&]()
431 : {
432 188909 : if (loop->safelen >= 2)
433 : {
434 7459 : if ((unsigned int) loop->safelen < *max_vf)
435 1894 : *max_vf = loop->safelen;
436 7459 : LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
437 7459 : return true;
438 : }
439 : return false;
440 1502936 : };
441 :
442 : /* In loop analysis all data references should be vectorizable. */
443 1502936 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
444 1502936 : || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
445 0 : gcc_unreachable ();
446 :
447 : /* Independent data accesses. */
448 1502936 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
449 1226413 : return opt_result::success ();
450 :
451 276523 : if (dra == drb
452 276523 : || (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 276523 : if (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
459 121100 : && (DR_GROUP_FIRST_ELEMENT (stmtinfo_a)
460 121100 : == DR_GROUP_FIRST_ELEMENT (stmtinfo_b))
461 296126 : && !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 133699 : if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
471 140339 : || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
472 426392 : && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
473 : get_alias_set (DR_REF (drb))))
474 6395 : return opt_result::success ();
475 :
476 267643 : if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
477 257531 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
478 : {
479 12801 : if (apply_safelen ())
480 1393 : return opt_result::success ();
481 :
482 11408 : return opt_result::failure_at
483 11408 : (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 254842 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
490 : {
491 175547 : if (apply_safelen ())
492 6066 : return opt_result::success ();
493 :
494 169481 : if (dump_enabled_p ())
495 7681 : 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 169481 : return vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
502 : }
503 :
504 : /* Known data dependence. */
505 79295 : 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 78734 : loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
520 :
521 78734 : if (DDR_COULD_BE_INDEPENDENT_P (ddr)
522 78734 : && vect_analyze_possibly_independent_ddr (ddr, loop_vinfo,
523 : loop_depth, max_vf))
524 8464 : return opt_result::success ();
525 :
526 134079 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
527 : {
528 70292 : int dist = dist_v[loop_depth];
529 :
530 70292 : if (dump_enabled_p ())
531 4722 : dump_printf_loc (MSG_NOTE, vect_location,
532 : "dependence distance = %d.\n", dist);
533 :
534 70292 : if (dist == 0)
535 : {
536 58465 : 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 58465 : 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 58465 : if (loop->safelen < 2)
565 : {
566 54592 : tree indicator = dr_zero_step_indicator (dra);
567 54592 : if (!indicator || integer_zerop (indicator))
568 0 : return opt_result::failure_at (stmtinfo_a->stmt,
569 : "access also has a zero step\n");
570 54592 : else if (TREE_CODE (indicator) != INTEGER_CST)
571 1504 : vect_check_nonzero_value (loop_vinfo, indicator);
572 : }
573 58465 : continue;
574 58465 : }
575 :
576 11827 : 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 3950 : 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 3950 : if (nested_in_vect_loop_p (loop, stmtinfo_a)
588 3938 : || 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 3941 : 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 3941 : continue;
606 3941 : }
607 :
608 7877 : unsigned int abs_dist = abs (dist);
609 7877 : if (abs_dist >= 2 && abs_dist < *max_vf)
610 : {
611 : /* The dependence distance requires reduction of the maximal
612 : vectorization factor. */
613 560 : *max_vf = abs_dist;
614 560 : 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 7877 : if (abs_dist >= *max_vf)
621 : {
622 : /* Dependence distance does not create dependence, as far as
623 : vectorization is concerned, in this case. */
624 1403 : if (dump_enabled_p ())
625 737 : dump_printf_loc (MSG_NOTE, vect_location,
626 : "dependence distance >= VF.\n");
627 1403 : continue;
628 : }
629 :
630 6474 : 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 63787 : 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 147736 : vect_analyze_early_break_dependences (loop_vec_info loop_vinfo)
681 : {
682 147736 : DUMP_VECT_SCOPE ("vect_analyze_early_break_dependences");
683 :
684 : /* List of all load data references found during traversal. */
685 147736 : auto_vec<data_reference *> bases;
686 147736 : basic_block dest_bb = NULL;
687 :
688 147736 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
689 147736 : class loop *loop_nest = loop_outer (loop);
690 :
691 147736 : if (dump_enabled_p ())
692 1586 : dump_printf_loc (MSG_NOTE, vect_location,
693 : "loop contains multiple exits, analyzing"
694 : " statement dependencies.\n");
695 :
696 147736 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
697 26103 : if (dump_enabled_p ())
698 286 : 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 147736 : if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
709 26103 : dest_bb = loop->latch;
710 : else
711 121633 : 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 147736 : 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 147736 : bool check_deps = false;
721 :
722 531118 : do
723 : {
724 339427 : 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 2536965 : while (!gsi_end_p (gsi))
729 : {
730 2203537 : gimple *stmt = gsi_stmt (gsi);
731 2203537 : gsi_prev (&gsi);
732 2203537 : if (is_gimple_debug (stmt))
733 1947315 : continue;
734 :
735 1156162 : stmt_vec_info orig_stmt_vinfo = loop_vinfo->lookup_stmt (stmt);
736 1156162 : stmt_vec_info stmt_vinfo
737 1156162 : = vect_stmt_to_vectorize (orig_stmt_vinfo);
738 1156162 : auto dr_ref = STMT_VINFO_DATA_REF (stmt_vinfo);
739 1156162 : if (!dr_ref)
740 : {
741 : /* Trapping statements after the last early exit are fine. */
742 893751 : if (check_deps)
743 : {
744 536583 : bool could_trap_p = false;
745 536583 : gimple *cur_stmt = STMT_VINFO_STMT (stmt_vinfo);
746 536583 : could_trap_p = gimple_could_trap_p (cur_stmt);
747 536583 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_vinfo))
748 : {
749 199139 : gimple_stmt_iterator gsi2;
750 199139 : auto stmt_seq
751 199139 : = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_vinfo);
752 199139 : for (gsi2 = gsi_start (stmt_seq);
753 401290 : !could_trap_p && !gsi_end_p (gsi2); gsi_next (&gsi2))
754 : {
755 202151 : cur_stmt = gsi_stmt (gsi2);
756 202151 : could_trap_p = gimple_could_trap_p (cur_stmt);
757 : }
758 : }
759 :
760 536583 : if (could_trap_p)
761 : {
762 5451 : if (dump_enabled_p ())
763 150 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
764 : "cannot vectorize as operation may trap.\n");
765 5451 : 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 888300 : continue;
773 888300 : }
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 262411 : if (!check_deps)
780 11640 : 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 250771 : if (DR_IS_READ (dr_ref))
788 : {
789 234882 : dr_set_safe_speculative_read_required (stmt_vinfo, true);
790 234882 : bool inbounds = ref_within_array_bound (stmt, DR_REF (dr_ref));
791 234882 : DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_vinfo)) = inbounds;
792 :
793 234882 : if (dump_enabled_p ())
794 2461 : dump_printf_loc (MSG_NOTE, vect_location,
795 : "marking DR (read) as possibly needing peeling "
796 : "for alignment at %G", stmt);
797 : }
798 :
799 250771 : if (DR_IS_READ (dr_ref))
800 234882 : bases.safe_push (dr_ref);
801 15889 : 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 49231 : 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 500446 : if (gimple_vdef (stmt))
843 : {
844 15341 : if (dump_enabled_p ())
845 282 : dump_printf_loc (MSG_NOTE, vect_location,
846 : "==> recording stmt %G", stmt);
847 :
848 15341 : LOOP_VINFO_EARLY_BRK_STORES (loop_vinfo).safe_push (stmt);
849 : }
850 719987 : else if (gimple_vuse (stmt))
851 : {
852 234882 : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).safe_insert (0, stmt);
853 234882 : if (dump_enabled_p ())
854 2461 : dump_printf_loc (MSG_NOTE, vect_location,
855 : "marked statement for vUSE update: %G", stmt);
856 : }
857 : }
858 :
859 333428 : if (!single_pred_p (bb))
860 : {
861 141737 : gcc_assert (bb == loop->header);
862 141737 : break;
863 : }
864 :
865 : /* If we possibly sink through a virtual PHI make sure to elide that. */
866 191691 : 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 191691 : check_deps = true;
871 191691 : bb = single_pred (bb);
872 191691 : }
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 141737 : 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 141737 : 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 141737 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo) = dest_bb;
892 : /* Check if loop has a side-effect (stores), force scalar epilogue. */
893 639058 : for (auto dr : LOOP_VINFO_DATAREFS (loop_vinfo))
894 240814 : if (DR_IS_WRITE (dr))
895 : {
896 13329 : LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo) = true;
897 13329 : break;
898 : }
899 :
900 141737 : 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 131018 : tree vuse = gimple_vuse (LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo).last ());
905 364627 : for (auto g : LOOP_VINFO_EARLY_BRK_VUSES (loop_vinfo))
906 : {
907 233609 : if (dump_enabled_p ())
908 2398 : dump_printf_loc (MSG_NOTE, vect_location,
909 : "will update use: %T, mem_ref: %G", vuse, g);
910 : }
911 : }
912 :
913 141737 : if (dump_enabled_p ())
914 1432 : dump_printf_loc (MSG_NOTE, vect_location,
915 : "recorded statements to be moved to BB %d\n",
916 1432 : LOOP_VINFO_EARLY_BRK_DEST_BB (loop_vinfo)->index);
917 :
918 141737 : return opt_result::success ();
919 147736 : }
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 395480 : vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo,
929 : unsigned int *max_vf)
930 : {
931 395480 : unsigned int i;
932 395480 : struct data_dependence_relation *ddr;
933 :
934 395480 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_dependences");
935 :
936 395480 : if (!LOOP_VINFO_DDRS (loop_vinfo).exists ())
937 : {
938 164096 : LOOP_VINFO_DDRS (loop_vinfo)
939 164096 : .create (LOOP_VINFO_DATAREFS (loop_vinfo).length ()
940 164096 : * LOOP_VINFO_DATAREFS (loop_vinfo).length ());
941 : /* We do not need read-read dependences. */
942 328192 : bool res = compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
943 : &LOOP_VINFO_DDRS (loop_vinfo),
944 164096 : LOOP_VINFO_LOOP_NEST (loop_vinfo),
945 : false);
946 164096 : gcc_assert (res);
947 : }
948 :
949 395480 : 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 395480 : if (LOOP_VINFO_EPILOGUE_P (loop_vinfo))
955 12582 : *max_vf = LOOP_VINFO_ORIG_MAX_VECT_FACTOR (loop_vinfo);
956 : else
957 1867754 : FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
958 : {
959 1502936 : opt_result res
960 1502936 : = vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf);
961 1502936 : if (!res)
962 18080 : 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 377400 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
968 147736 : return vect_analyze_early_break_dependences (loop_vinfo);
969 :
970 229664 : 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 6926609 : vect_slp_analyze_data_ref_dependence (vec_info *vinfo,
984 : struct data_dependence_relation *ddr)
985 : {
986 6926609 : struct data_reference *dra = DDR_A (ddr);
987 6926609 : struct data_reference *drb = DDR_B (ddr);
988 6926609 : dr_vec_info *dr_info_a = vinfo->lookup_dr (dra);
989 6926609 : 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 6926609 : if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
996 : return chrec_known;
997 :
998 1121628 : if (dra == drb)
999 : return chrec_known;
1000 :
1001 : /* Read-read is OK. */
1002 9086 : 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 9086 : if (STMT_VINFO_GROUPED_ACCESS (dr_info_a->stmt)
1008 9086 : && (DR_GROUP_FIRST_ELEMENT (dr_info_a->stmt)
1009 9086 : == DR_GROUP_FIRST_ELEMENT (dr_info_b->stmt)))
1010 : return chrec_known;
1011 :
1012 : /* Unknown data dependence. */
1013 9086 : if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1014 : {
1015 9086 : 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 9086 : 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 671652 : 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 671652 : stmt_vec_info last_access_info = vect_find_last_scalar_stmt_in_slp (node);
1038 671652 : gcc_assert (DR_IS_WRITE (STMT_VINFO_DATA_REF (last_access_info)));
1039 :
1040 3782594 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (node))
1041 : {
1042 1776335 : stmt_vec_info access_info
1043 1776335 : = vect_orig_stmt (stmt_vinfo);
1044 1776335 : if (access_info == last_access_info)
1045 663793 : continue;
1046 1112542 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1047 1112542 : ao_ref ref;
1048 1112542 : bool ref_initialized_p = false;
1049 1112542 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1050 10689310 : gsi_stmt (gsi) != last_access_info->stmt; gsi_next (&gsi))
1051 : {
1052 9585465 : gimple *stmt = gsi_stmt (gsi);
1053 16976069 : if (! gimple_vuse (stmt))
1054 2658507 : 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 6926958 : stmt_vec_info stmt_info = vinfo->lookup_stmt (stmt);
1060 6926958 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1061 6926958 : if (dr_b)
1062 : {
1063 6926395 : gcc_assert (!gimple_visited_p (stmt));
1064 :
1065 6926395 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1066 6926395 : dr_b, vNULL);
1067 6926395 : tree dep = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1068 6926395 : free_dependence_relation (ddr);
1069 6926395 : if (dep == chrec_known)
1070 6917345 : continue;
1071 9050 : if (dep != chrec_dont_know)
1072 8697 : 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 9613 : if (!ref_initialized_p)
1079 : {
1080 9352 : ao_ref_init (&ref, DR_REF (dr_a));
1081 9352 : ref_initialized_p = true;
1082 : }
1083 9613 : if (stmt_may_clobber_ref_p_1 (stmt, &ref, false)
1084 9613 : || ref_maybe_used_by_stmt_p (stmt, &ref, false))
1085 8697 : 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 157980 : 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 157980 : stmt_vec_info first_access_info = vect_find_first_scalar_stmt_in_slp (node);
1104 157980 : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (first_access_info)));
1105 :
1106 871450 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (node))
1107 : {
1108 397546 : if (! stmt_vinfo)
1109 164675 : continue;
1110 397546 : stmt_vec_info access_info
1111 397546 : = vect_orig_stmt (stmt_vinfo);
1112 397546 : if (access_info == first_access_info)
1113 164675 : continue;
1114 232871 : data_reference *dr_a = STMT_VINFO_DATA_REF (access_info);
1115 232871 : ao_ref ref;
1116 232871 : bool ref_initialized_p = false;
1117 232871 : hash_set<stmt_vec_info> grp_visited;
1118 232871 : for (gimple_stmt_iterator gsi = gsi_for_stmt (access_info->stmt);
1119 4583113 : gsi_stmt (gsi) != first_access_info->stmt; gsi_prev (&gsi))
1120 : {
1121 2175157 : gimple *stmt = gsi_stmt (gsi);
1122 3504319 : if (! gimple_vdef (stmt))
1123 2119565 : continue;
1124 :
1125 280740 : 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 280740 : if (gimple_visited_p (stmt))
1132 : {
1133 225148 : if (stmt_info != last_store_info)
1134 225146 : 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 36 : 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 114095 : 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 58503 : if (!ref_initialized_p)
1167 : {
1168 10954 : ao_ref_init (&ref, DR_REF (dr_a));
1169 10954 : ref_initialized_p = true;
1170 : }
1171 58503 : 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 210 : data_reference *dr_b = STMT_VINFO_DATA_REF (stmt_info);
1176 210 : if (!dr_b)
1177 : return false;
1178 210 : ddr_p ddr = initialize_data_dependence_relation (dr_a,
1179 210 : dr_b, vNULL);
1180 210 : tree dep
1181 210 : = vect_slp_analyze_data_ref_dependence (vinfo, ddr);
1182 210 : 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 210 : if (dep != chrec_known)
1187 : return false;
1188 : }
1189 : /* No dependence. */
1190 : return true;
1191 55592 : };
1192 55592 : 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 55124 : if (!grp_visited.add (DR_GROUP_FIRST_ELEMENT (stmt_info)))
1204 10760 : for (auto store_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
1205 129053 : store_info != NULL;
1206 118293 : store_info = DR_GROUP_NEXT_ELEMENT (store_info))
1207 118329 : if ((store_info == stmt_info
1208 107578 : || get_later_stmt (store_info, stmt_info) == stmt_info)
1209 165613 : && !check_hoist (store_info))
1210 : return false;
1211 : }
1212 : else
1213 : {
1214 468 : if (!check_hoist (stmt_info))
1215 : return false;
1216 : }
1217 : }
1218 232871 : }
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 808954 : vect_slp_analyze_instance_dependence (vec_info *vinfo, slp_instance instance)
1231 : {
1232 808954 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_dependence");
1233 :
1234 : /* The stores of this instance are at the root of the SLP tree. */
1235 808954 : slp_tree store = NULL;
1236 808954 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store)
1237 671652 : store = SLP_INSTANCE_TREE (instance);
1238 :
1239 : /* Verify we can sink stores to the vectorized stmt insert location. */
1240 671652 : stmt_vec_info last_store_info = NULL;
1241 671652 : if (store)
1242 : {
1243 671652 : if (! vect_slp_analyze_store_dependences (vinfo, store))
1244 : return false;
1245 :
1246 : /* Mark stores in this instance and remember the last one. */
1247 662955 : last_store_info = vect_find_last_scalar_stmt_in_slp (store);
1248 3755635 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (store))
1249 1766770 : gimple_set_visited (STMT_VINFO_STMT (stmt_vinfo), true);
1250 : }
1251 :
1252 800257 : bool res = true;
1253 :
1254 : /* Verify we can sink loads to the vectorized stmt insert location,
1255 : special-casing stores of this instance. */
1256 1210727 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1257 157980 : 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 800257 : if (store)
1268 3755635 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (store))
1269 1766770 : 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 800257 : if (res && store)
1275 : {
1276 662931 : stmt_vec_info store_info
1277 662931 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (store)[0]);
1278 662931 : class loop *store_loop = gimple_bb (store_info->stmt)->loop_father;
1279 662931 : if (! loop_outer (store_loop))
1280 565061 : return res;
1281 97870 : vec<loop_p> loop_nest;
1282 97870 : loop_nest.create (1);
1283 97870 : loop_nest.quick_push (store_loop);
1284 97870 : data_reference *drs = nullptr;
1285 181889 : for (slp_tree &load : SLP_INSTANCE_LOADS (instance))
1286 : {
1287 37177 : if (! STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (load)[0]))
1288 0 : continue;
1289 37177 : stmt_vec_info load_info
1290 37177 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (load)[0]);
1291 37177 : if (gimple_bb (load_info->stmt)->loop_father != store_loop)
1292 5149 : 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 32028 : if (! vect_stmt_dominates_stmt_p (STMT_VINFO_STMT (load_info),
1299 : STMT_VINFO_STMT (store_info)))
1300 11761 : continue;
1301 :
1302 20267 : if (! drs)
1303 : {
1304 19404 : drs = create_data_ref (loop_preheader_edge (store_loop),
1305 : store_loop,
1306 19404 : DR_REF (STMT_VINFO_DATA_REF (store_info)),
1307 : store_info->stmt, false, false);
1308 19404 : if (! DR_BASE_ADDRESS (drs)
1309 16379 : || TREE_CODE (DR_STEP (drs)) != INTEGER_CST)
1310 : break;
1311 : }
1312 16965 : data_reference *drl
1313 16965 : = create_data_ref (loop_preheader_edge (store_loop),
1314 : store_loop,
1315 16965 : 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 32240 : if (! DR_BASE_ADDRESS (drl)
1321 14836 : || ! operand_equal_p (DR_STEP (drs), DR_STEP (drl))
1322 8831 : || ! operand_equal_p (DR_BASE_ADDRESS (drs),
1323 8831 : DR_BASE_ADDRESS (drl))
1324 18665 : || ! operand_equal_p (DR_OFFSET (drs), DR_OFFSET (drl)))
1325 : {
1326 15275 : free_data_ref (drl);
1327 15275 : 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 832 : unsigned HOST_WIDE_INT dist
1343 832 : = absu_hwi (TREE_INT_CST_LOW (DR_INIT (drl)) + step
1344 832 : - TREE_INT_CST_LOW (DR_INIT (drs)));
1345 832 : poly_uint64 loadsz = tree_to_poly_uint64
1346 832 : (TYPE_SIZE_UNIT (SLP_TREE_VECTYPE (load)));
1347 832 : poly_uint64 storesz = tree_to_poly_uint64
1348 832 : (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 1664 : 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 97870 : if (drs)
1358 19404 : free_data_ref (drs);
1359 97870 : 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 6779053 : dr_misalignment (dr_vec_info *dr_info, tree vectype, poly_int64 offset)
1370 : {
1371 6779053 : 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 6779053 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
1375 : {
1376 2328634 : dr_vec_info *first_dr
1377 2328634 : = 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 2328634 : diff = (TREE_INT_CST_LOW (DR_INIT (dr_info->dr))
1382 2328634 : - TREE_INT_CST_LOW (DR_INIT (first_dr->dr)));
1383 2328634 : gcc_assert (diff >= 0);
1384 : dr_info = first_dr;
1385 : }
1386 :
1387 6779053 : int misalign = dr_info->misalignment;
1388 6779053 : gcc_assert (misalign != DR_MISALIGNMENT_UNINITIALIZED);
1389 6779053 : 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 4143380 : if (maybe_lt (dr_info->target_alignment * BITS_PER_UNIT,
1395 4143380 : 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 4143377 : 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 4143377 : if (known_eq (misalignment, 0))
1406 : return 0;
1407 :
1408 656293 : unsigned HOST_WIDE_INT target_alignment_c;
1409 656293 : if (!dr_info->target_alignment.is_constant (&target_alignment_c)
1410 656293 : || !known_misalignment (misalignment, target_alignment_c, &misalign))
1411 : return DR_MISALIGNMENT_UNKNOWN;
1412 656293 : return misalign;
1413 : }
1414 :
1415 : /* Record the base alignment guarantee given by DRB, which occurs
1416 : in STMT_INFO. */
1417 :
1418 : static void
1419 4720558 : vect_record_base_alignment (vec_info *vinfo, stmt_vec_info stmt_info,
1420 : innermost_loop_behavior *drb)
1421 : {
1422 4720558 : bool existed;
1423 4720558 : std::pair<stmt_vec_info, innermost_loop_behavior *> &entry
1424 4720558 : = vinfo->base_alignments.get_or_insert (drb->base_address, &existed);
1425 4720558 : if (!existed || entry.second->base_alignment < drb->base_alignment)
1426 : {
1427 1454780 : entry = std::make_pair (stmt_info, drb);
1428 1454780 : if (dump_enabled_p ())
1429 32789 : 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 4720558 : }
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 1053033 : vect_record_base_alignments (vec_info *vinfo)
1451 : {
1452 1053033 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1453 423583 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
1454 15516480 : for (data_reference *dr : vinfo->shared->datarefs)
1455 : {
1456 12457921 : dr_vec_info *dr_info = vinfo->lookup_dr (dr);
1457 12457921 : stmt_vec_info stmt_info = dr_info->stmt;
1458 12457921 : if (!DR_IS_CONDITIONAL_IN_STMT (dr)
1459 12448229 : && STMT_VINFO_VECTORIZABLE (stmt_info)
1460 4737631 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1461 : {
1462 4719021 : 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 13405081 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
1467 1537 : vect_record_base_alignment
1468 1537 : (vinfo, stmt_info, &STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info));
1469 : }
1470 : }
1471 1053033 : }
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 1635578 : vect_compute_data_ref_alignment (vec_info *vinfo, dr_vec_info *dr_info,
1486 : tree vectype)
1487 : {
1488 1635578 : stmt_vec_info stmt_info = dr_info->stmt;
1489 1635578 : vec_base_alignments *base_alignments = &vinfo->base_alignments;
1490 1635578 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1491 1635578 : class loop *loop = NULL;
1492 1635578 : tree ref = DR_REF (dr_info->dr);
1493 :
1494 1635578 : if (dump_enabled_p ())
1495 51658 : dump_printf_loc (MSG_NOTE, vect_location,
1496 : "vect_compute_data_ref_alignment:\n");
1497 :
1498 1635578 : if (loop_vinfo)
1499 843474 : loop = LOOP_VINFO_LOOP (loop_vinfo);
1500 :
1501 : /* Initialize misalignment to unknown. */
1502 1635578 : SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
1503 :
1504 1635578 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1505 : return;
1506 :
1507 1615254 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
1508 1615254 : bool step_preserves_misalignment_p;
1509 :
1510 1615254 : poly_uint64 vector_alignment
1511 1615254 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
1512 : BITS_PER_UNIT);
1513 :
1514 1615254 : if (loop_vinfo
1515 1615254 : && 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 320003 : auto align_factor_c
1522 320003 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1523 320003 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1524 90148 : align_factor_c *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
1525 :
1526 320003 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1527 320003 : poly_uint64 new_alignment = vf * align_factor_c;
1528 :
1529 640006 : if ((vf.is_constant () && pow2p_hwi (new_alignment.to_constant ()))
1530 : || (!vf.is_constant () && pow2p_hwi (align_factor_c)))
1531 : {
1532 256800 : if (dump_enabled_p ())
1533 : {
1534 2965 : dump_printf_loc (MSG_NOTE, vect_location,
1535 : "alignment increased due to early break to ");
1536 2965 : dump_dec (MSG_NOTE, new_alignment);
1537 2965 : dump_printf (MSG_NOTE, " bytes.\n");
1538 : }
1539 256800 : vector_alignment = new_alignment;
1540 : }
1541 : }
1542 :
1543 1615254 : 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 1615254 : if (loop_vinfo)
1550 : {
1551 823150 : loop_vec_info orig_loop_vinfo = LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo);
1552 823150 : if (orig_loop_vinfo
1553 32539 : && LOOP_VINFO_PEELING_FOR_ALIGNMENT (orig_loop_vinfo) != 0)
1554 : return;
1555 : }
1556 :
1557 1615037 : unsigned HOST_WIDE_INT vect_align_c;
1558 1615037 : if (!vector_alignment.is_constant (&vect_align_c))
1559 : return;
1560 :
1561 : /* No step for BB vectorization. */
1562 1615037 : if (!loop)
1563 : {
1564 792104 : 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 1678914 : while (lvinfo)
1578 : {
1579 855981 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (lvinfo);
1580 855981 : step_preserves_misalignment_p
1581 855981 : &= multiple_p (drb->step_alignment * vf, vect_align_c);
1582 855981 : lvinfo = LOOP_VINFO_ORIG_LOOP_INFO (lvinfo);
1583 : }
1584 :
1585 822933 : if (!step_preserves_misalignment_p && dump_enabled_p ())
1586 322 : 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 822933 : if (step_preserves_misalignment_p
1596 822933 : && nested_in_vect_loop_p (loop, stmt_info))
1597 : {
1598 1536 : step_preserves_misalignment_p
1599 1536 : = (DR_STEP_ALIGNMENT (dr_info->dr) % vect_align_c) == 0;
1600 :
1601 1536 : if (dump_enabled_p ())
1602 : {
1603 496 : 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 138 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1608 : "inner step doesn't divide the vector"
1609 : " alignment.\n");
1610 : }
1611 : }
1612 : }
1613 :
1614 1615037 : unsigned int base_alignment = drb->base_alignment;
1615 1615037 : 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 1615037 : std::pair<stmt_vec_info, innermost_loop_behavior *> *entry
1620 1615037 : = base_alignments->get (drb->base_address);
1621 1615037 : if (entry
1622 1610288 : && base_alignment < (*entry).second->base_alignment
1623 1618379 : && (loop_vinfo
1624 2432 : || (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt_info->stmt),
1625 2432 : gimple_bb (entry->first->stmt))
1626 2326 : && (gimple_bb (stmt_info->stmt) != gimple_bb (entry->first->stmt)
1627 2084 : || (entry->first->dr_aux.group <= dr_info->group)))))
1628 : {
1629 3219 : base_alignment = entry->second->base_alignment;
1630 3219 : base_misalignment = entry->second->base_misalignment;
1631 : }
1632 :
1633 1615037 : if (drb->offset_alignment < vect_align_c
1634 1547547 : || !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 1539166 : || TREE_CODE (drb->step) != INTEGER_CST)
1638 : {
1639 103714 : if (dump_enabled_p ())
1640 3631 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1641 : "Unknown alignment for access: %T\n", ref);
1642 103714 : return;
1643 : }
1644 :
1645 1511323 : if (base_alignment < vect_align_c)
1646 : {
1647 743802 : unsigned int max_alignment;
1648 743802 : tree base = get_base_for_alignment (drb->base_address, &max_alignment);
1649 743802 : if (max_alignment < vect_align_c
1650 741223 : || (loop_vinfo && LOOP_VINFO_EPILOGUE_P (loop_vinfo))
1651 1464348 : || !vect_can_force_dr_alignment_p (base,
1652 720546 : vect_align_c * BITS_PER_UNIT))
1653 : {
1654 545072 : if (dump_enabled_p ())
1655 14358 : dump_printf_loc (MSG_NOTE, vect_location,
1656 : "can't force alignment of ref: %T\n", ref);
1657 545072 : 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 198730 : if (dump_enabled_p ())
1664 7944 : dump_printf_loc (MSG_NOTE, vect_location,
1665 : "force alignment of %T\n", ref);
1666 :
1667 198730 : dr_info->base_decl = base;
1668 198730 : dr_info->base_misaligned = true;
1669 198730 : base_misalignment = 0;
1670 : }
1671 966251 : poly_int64 misalignment
1672 966251 : = base_misalignment + wi::to_poly_offset (drb->init).force_shwi ();
1673 :
1674 966251 : unsigned int const_misalignment;
1675 966251 : 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 966251 : SET_DR_MISALIGNMENT (dr_info, const_misalignment);
1684 :
1685 966251 : if (dump_enabled_p ())
1686 32344 : 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 1990734 : vect_dr_aligned_if_related_peeled_dr_is (dr_vec_info *dr_info,
1699 : dr_vec_info *dr_peel_info)
1700 : {
1701 1990734 : if (multiple_p (DR_TARGET_ALIGNMENT (dr_peel_info),
1702 1991518 : DR_TARGET_ALIGNMENT (dr_info)))
1703 : {
1704 1989950 : poly_offset_int diff
1705 1989950 : = (wi::to_poly_offset (DR_INIT (dr_peel_info->dr))
1706 1989950 : - wi::to_poly_offset (DR_INIT (dr_info->dr)));
1707 1989950 : if (known_eq (diff, 0)
1708 1989950 : || multiple_p (diff, DR_TARGET_ALIGNMENT (dr_info)))
1709 748865 : 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 201462 : vect_dr_aligned_if_peeled_dr_is (dr_vec_info *dr_info,
1719 : dr_vec_info *dr_peel_info)
1720 : {
1721 201462 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_info->dr),
1722 201462 : DR_BASE_ADDRESS (dr_peel_info->dr), 0)
1723 49040 : || !operand_equal_p (DR_OFFSET (dr_info->dr),
1724 49040 : DR_OFFSET (dr_peel_info->dr), 0)
1725 249600 : || !operand_equal_p (DR_STEP (dr_info->dr),
1726 48138 : DR_STEP (dr_peel_info->dr), 0))
1727 153722 : return false;
1728 :
1729 47740 : 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 22158 : vect_dr_misalign_for_aligned_access (dr_vec_info *dr_info)
1738 : {
1739 22158 : 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 2799 : 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 2799 : if (vect_dr_aligned_if_peeled_dr_is (dr_info, dr_peel_info))
1773 : {
1774 444 : SET_DR_MISALIGNMENT (dr_info,
1775 : vect_dr_misalign_for_aligned_access (dr_peel_info));
1776 444 : 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 1838038 : vect_relevant_for_alignment_p (dr_vec_info *dr_info)
1803 : {
1804 1838038 : stmt_vec_info stmt_info = dr_info->stmt;
1805 :
1806 1838038 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
1807 : return false;
1808 :
1809 : /* For interleaving, only the alignment of the first access matters. */
1810 1837116 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1811 2080679 : && 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 1729629 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1817 1729629 : || integer_zerop (DR_STEP (dr_info->dr)))
1818 54951 : return false;
1819 :
1820 : /* Strided accesses perform only component accesses, alignment is
1821 : irrelevant for them. */
1822 1674678 : if (STMT_VINFO_STRIDED_P (stmt_info)
1823 1674678 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1824 : 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 1612122 : not_size_aligned (tree exp)
1834 : {
1835 1612122 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
1836 : return true;
1837 :
1838 1612122 : return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
1839 1612122 : > 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 626835 : vector_alignment_reachable_p (dr_vec_info *dr_info, poly_uint64 vf)
1849 : {
1850 626835 : stmt_vec_info stmt_info = dr_info->stmt;
1851 626835 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1852 626835 : poly_uint64 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1853 1253670 : poly_uint64 vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
1854 626835 : unsigned elem_size = vector_element_size (vector_size, nelements);
1855 626835 : unsigned group_size = 1;
1856 :
1857 626835 : 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 87586 : if (!known_alignment_for_access_p (dr_info, vectype))
1865 626835 : return false;
1866 :
1867 51952 : unsigned mis_in_elements = dr_misalignment (dr_info, vectype) / elem_size;
1868 64424 : if (!multiple_p (nelements - mis_in_elements, DR_GROUP_SIZE (stmt_info)))
1869 : return false;
1870 :
1871 12472 : 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 551721 : if (!multiple_p (elem_size * group_size * vf, dr_target_alignment (dr_info)))
1877 154 : 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 551567 : if (known_alignment_for_access_p (dr_info, vectype)
1882 861674 : && !aligned_access_p (dr_info, vectype))
1883 : {
1884 16059 : HOST_WIDE_INT elmsize =
1885 16059 : int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1886 16059 : 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 16059 : 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 72 : return false;
1898 : }
1899 : }
1900 :
1901 551495 : if (!known_alignment_for_access_p (dr_info, vectype))
1902 : {
1903 241460 : tree type = TREE_TYPE (DR_REF (dr_info->dr));
1904 241460 : bool is_packed = not_size_aligned (DR_REF (dr_info->dr));
1905 241460 : if (dump_enabled_p ())
1906 16019 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1907 : "Unknown misalignment, %snaturally aligned\n",
1908 : is_packed ? "not " : "");
1909 241460 : 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 752207 : 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 752207 : stmt_vec_info stmt_info = dr_info->stmt;
1928 :
1929 752207 : if (DR_IS_READ (dr_info->dr))
1930 530186 : 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 222021 : vect_get_store_cost (vinfo,stmt_info, NULL, 1,
1936 : alignment_support_scheme, misalignment, inside_cost,
1937 : body_cost_vec);
1938 :
1939 752207 : if (dump_enabled_p ())
1940 29884 : 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 752207 : }
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 761653 : peel_info_hasher::hash (const _vect_peel_info *peel_info)
1972 : {
1973 761653 : return (hashval_t) peel_info->npeel;
1974 : }
1975 :
1976 : inline bool
1977 393530 : peel_info_hasher::equal (const _vect_peel_info *a, const _vect_peel_info *b)
1978 : {
1979 393530 : 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 368923 : 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 368923 : struct _vect_peel_info elem, *slot;
1991 368923 : _vect_peel_info **new_slot;
1992 :
1993 368923 : elem.npeel = npeel;
1994 368923 : slot = peeling_htab->find (&elem);
1995 368923 : if (slot)
1996 159340 : slot->count++;
1997 : else
1998 : {
1999 209583 : slot = XNEW (struct _vect_peel_info);
2000 209583 : slot->npeel = npeel;
2001 209583 : slot->dr_info = dr_info;
2002 209583 : slot->count = 1;
2003 209583 : new_slot = peeling_htab->find_slot (slot, INSERT);
2004 209583 : *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 368923 : if (!supportable_if_not_aligned
2010 368923 : && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2011 4656 : slot->count += VECT_MAX_COST;
2012 368923 : }
2013 :
2014 :
2015 : /* Traverse peeling hash table to find peeling option that aligns maximum
2016 : number of data accesses. */
2017 :
2018 : int
2019 35820 : vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
2020 : _vect_peel_extended_info *max)
2021 : {
2022 35820 : vect_peel_info elem = *slot;
2023 :
2024 35820 : if (elem->count > max->peel_info.count
2025 21754 : || (elem->count == max->peel_info.count
2026 17046 : && max->peel_info.npeel > elem->npeel))
2027 : {
2028 14082 : max->peel_info.npeel = elem->npeel;
2029 14082 : max->peel_info.count = elem->count;
2030 14082 : max->peel_info.dr_info = elem->dr_info;
2031 : }
2032 :
2033 35820 : 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 414687 : 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 414687 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2051 :
2052 414687 : bool dr0_alignment_known_p
2053 : = (dr0_info
2054 760507 : && known_alignment_for_access_p (dr0_info,
2055 345820 : STMT_VINFO_VECTYPE (dr0_info->stmt)));
2056 :
2057 2033841 : for (data_reference *dr : datarefs)
2058 : {
2059 789780 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2060 789780 : if (!vect_relevant_for_alignment_p (dr_info))
2061 37573 : continue;
2062 :
2063 752207 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2064 752207 : dr_alignment_support alignment_support_scheme;
2065 752207 : int misalignment;
2066 752207 : unsigned HOST_WIDE_INT alignment;
2067 :
2068 752207 : bool negative = tree_int_cst_compare (DR_STEP (dr_info->dr),
2069 752207 : size_zero_node) < 0;
2070 752207 : poly_int64 off = 0;
2071 752207 : if (negative)
2072 24197 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2073 24197 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2074 :
2075 752207 : if (npeel == 0)
2076 384341 : misalignment = dr_misalignment (dr_info, vectype, off);
2077 367866 : else if (dr_info == dr0_info
2078 367866 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2079 : misalignment = 0;
2080 127641 : else if (!dr0_alignment_known_p
2081 9713 : || !known_alignment_for_access_p (dr_info, vectype)
2082 137354 : || !DR_TARGET_ALIGNMENT (dr_info).is_constant (&alignment))
2083 : misalignment = DR_MISALIGNMENT_UNKNOWN;
2084 : else
2085 : {
2086 8704 : misalignment = dr_misalignment (dr_info, vectype, off);
2087 8704 : misalignment += npeel * TREE_INT_CST_LOW (DR_STEP (dr_info->dr));
2088 8704 : misalignment &= alignment - 1;
2089 : }
2090 752207 : alignment_support_scheme
2091 752207 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2092 : misalignment);
2093 :
2094 752207 : 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 414687 : }
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 154122 : vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
2106 : _vect_peel_extended_info *min)
2107 : {
2108 154122 : vect_peel_info elem = *slot;
2109 154122 : unsigned int inside_cost = 0, outside_cost = 0;
2110 154122 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (min->vinfo);
2111 154122 : stmt_vector_for_cost prologue_cost_vec, body_cost_vec;
2112 :
2113 154122 : prologue_cost_vec.create (2);
2114 154122 : body_cost_vec.create (2);
2115 :
2116 154122 : vect_get_peeling_costs_all_drs (loop_vinfo, elem->dr_info, &inside_cost,
2117 : &outside_cost, &body_cost_vec,
2118 154122 : &prologue_cost_vec, elem->npeel);
2119 :
2120 154122 : body_cost_vec.release ();
2121 154122 : prologue_cost_vec.release ();
2122 :
2123 154122 : outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel);
2124 :
2125 154122 : if (inside_cost < min->inside_cost
2126 2603 : || (inside_cost == min->inside_cost
2127 1255 : && outside_cost < min->outside_cost))
2128 : {
2129 151525 : min->inside_cost = inside_cost;
2130 151525 : min->outside_cost = outside_cost;
2131 151525 : min->peel_info.dr_info = elem->dr_info;
2132 151525 : min->peel_info.npeel = elem->npeel;
2133 151525 : min->peel_info.count = elem->count;
2134 : }
2135 :
2136 154122 : 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 163558 : vect_peeling_hash_choose_best_peeling (hash_table<peel_info_hasher> *peeling_htab,
2146 : loop_vec_info loop_vinfo)
2147 : {
2148 163558 : struct _vect_peel_extended_info res;
2149 :
2150 163558 : res.peel_info.dr_info = NULL;
2151 163558 : res.vinfo = loop_vinfo;
2152 :
2153 163558 : if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2154 : {
2155 149545 : res.inside_cost = INT_MAX;
2156 149545 : res.outside_cost = INT_MAX;
2157 149545 : peeling_htab->traverse <_vect_peel_extended_info *,
2158 303667 : vect_peeling_hash_get_lowest_cost> (&res);
2159 : }
2160 : else
2161 : {
2162 14013 : res.peel_info.count = 0;
2163 14013 : peeling_htab->traverse <_vect_peel_extended_info *,
2164 49833 : vect_peeling_hash_get_most_frequent> (&res);
2165 14013 : res.inside_cost = 0;
2166 14013 : res.outside_cost = 0;
2167 : }
2168 :
2169 163558 : 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 81853 : vect_peeling_supportable (loop_vec_info loop_vinfo, dr_vec_info *dr0_info,
2177 : unsigned npeel)
2178 : {
2179 81853 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2180 81853 : enum dr_alignment_support supportable_dr_alignment;
2181 :
2182 81853 : bool dr0_alignment_known_p
2183 163706 : = known_alignment_for_access_p (dr0_info,
2184 81853 : STMT_VINFO_VECTYPE (dr0_info->stmt));
2185 81853 : bool has_unsupported_dr_p = false;
2186 81853 : unsigned int dr0_step = tree_to_shwi (DR_STEP (dr0_info->dr));
2187 81853 : int known_unsupported_misalignment = DR_MISALIGNMENT_UNKNOWN;
2188 :
2189 : /* Check if each data ref can be vectorized after peeling. */
2190 349184 : for (data_reference *dr : datarefs)
2191 : {
2192 120279 : if (dr == dr0_info->dr)
2193 80901 : continue;
2194 :
2195 39378 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2196 39378 : if (!vect_relevant_for_alignment_p (dr_info)
2197 39378 : || vect_dr_aligned_if_peeled_dr_is (dr_info, dr0_info))
2198 6767 : continue;
2199 :
2200 32611 : tree vectype = STMT_VINFO_VECTYPE (dr_info->stmt);
2201 32611 : int misalignment;
2202 32611 : unsigned HOST_WIDE_INT alignment;
2203 32611 : if (!dr0_alignment_known_p
2204 2558 : || !known_alignment_for_access_p (dr_info, vectype)
2205 35169 : || !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 32611 : supportable_dr_alignment
2214 32611 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2215 : misalignment);
2216 32611 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2217 : {
2218 31186 : 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 31186 : if (tree_to_shwi (DR_STEP (dr)) != dr0_step)
2230 81853 : 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 26988 : int curr_dr_misalignment = dr_misalignment (dr_info, vectype);
2238 26988 : if (curr_dr_misalignment == 0)
2239 : return peeling_unsupported;
2240 14532 : 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 65199 : 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 4616827 : dr_align_group_sort_cmp (const void *dra_, const void *drb_)
2262 : {
2263 4616827 : data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2264 4616827 : data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2265 4616827 : int cmp;
2266 :
2267 : /* Stabilize sort. */
2268 4616827 : if (dra == drb)
2269 : return 0;
2270 :
2271 : /* Ordering of DRs according to base. */
2272 4616827 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
2273 : DR_BASE_ADDRESS (drb));
2274 4616827 : if (cmp != 0)
2275 : return cmp;
2276 :
2277 : /* And according to DR_OFFSET. */
2278 2033604 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2279 2033604 : if (cmp != 0)
2280 : return cmp;
2281 :
2282 : /* And after step. */
2283 2019481 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
2284 2019481 : if (cmp != 0)
2285 : return cmp;
2286 :
2287 : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2288 2014246 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
2289 2014246 : if (cmp == 0)
2290 237373 : 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 391287 : vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
2387 : {
2388 391287 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2389 391287 : dr_vec_info *first_store = NULL;
2390 391287 : dr_vec_info *dr0_info = NULL;
2391 391287 : struct data_reference *dr;
2392 391287 : unsigned int i;
2393 391287 : bool do_peeling = false;
2394 391287 : bool do_versioning = false;
2395 391287 : bool try_peeling_with_versioning = false;
2396 391287 : unsigned int npeel = 0;
2397 391287 : bool one_misalignment_known = false;
2398 391287 : bool one_misalignment_unknown = false;
2399 391287 : bool one_dr_unsupportable = false;
2400 391287 : dr_vec_info *unsupportable_dr_info = NULL;
2401 391287 : unsigned int dr0_same_align_drs = 0, first_store_same_align_drs = 0;
2402 391287 : hash_table<peel_info_hasher> peeling_htab (1);
2403 :
2404 391287 : DUMP_VECT_SCOPE ("vect_enhance_data_refs_alignment");
2405 :
2406 : /* Reset data so we can safely be called multiple times. */
2407 391287 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
2408 391287 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = 0;
2409 :
2410 391287 : if (LOOP_VINFO_DATAREFS (loop_vinfo).is_empty ())
2411 14264 : 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 377023 : auto_vec<data_reference_p> datarefs
2416 377023 : = LOOP_VINFO_DATAREFS (loop_vinfo).copy ();
2417 377023 : 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 754046 : auto_vec<unsigned> n_same_align_refs (datarefs.length ());
2422 377023 : n_same_align_refs.quick_grow_cleared (datarefs.length ());
2423 377023 : unsigned i0;
2424 773821 : for (i0 = 0; i0 < datarefs.length (); ++i0)
2425 390133 : if (DR_BASE_ADDRESS (datarefs[i0]))
2426 : break;
2427 2432814 : for (i = i0 + 1; i <= datarefs.length (); ++i)
2428 : {
2429 839384 : if (i == datarefs.length ()
2430 469026 : || !operand_equal_p (DR_BASE_ADDRESS (datarefs[i0]),
2431 469026 : DR_BASE_ADDRESS (datarefs[i]), 0)
2432 218901 : || !operand_equal_p (DR_OFFSET (datarefs[i0]),
2433 218901 : DR_OFFSET (datarefs[i]), 0)
2434 1056988 : || !operand_equal_p (DR_STEP (datarefs[i0]),
2435 217604 : 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 1461710 : for (unsigned j = i0; j < i; ++j)
2442 : {
2443 839384 : dr_vec_info *dr_infoj = loop_vinfo->lookup_dr (datarefs[j]);
2444 3621762 : for (unsigned k = i0; k < i; ++k)
2445 : {
2446 2782378 : if (k == j)
2447 839384 : continue;
2448 1942994 : dr_vec_info *dr_infok = loop_vinfo->lookup_dr (datarefs[k]);
2449 1942994 : if (vect_dr_aligned_if_related_peeled_dr_is (dr_infok,
2450 : dr_infoj))
2451 710010 : n_same_align_refs[j]++;
2452 : }
2453 : }
2454 : i0 = i;
2455 : }
2456 : }
2457 :
2458 : /* While cost model enhancements are expected in the future, the high level
2459 : view of the code at this time is as follows:
2460 :
2461 : A) If there is a misaligned access then see if doing peeling alone can
2462 : make all data references satisfy vect_supportable_dr_alignment. If so,
2463 : update data structures and return.
2464 :
2465 : B) If peeling alone wasn't possible and there is a data reference with an
2466 : unknown misalignment that does not satisfy vect_supportable_dr_alignment
2467 : then we may use either of the following two approaches.
2468 :
2469 : B1) Try peeling with versioning: Add a runtime loop versioning check to
2470 : see if all unsupportable data references are mutually aligned, which
2471 : means they will be uniformly aligned after a certain amount of loop
2472 : peeling. If peeling and versioning can be used together, set
2473 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to TRUE and return.
2474 :
2475 : B2) Try versioning alone: Add a runtime loop versioning check to see if
2476 : all unsupportable data references are already uniformly aligned
2477 : without loop peeling. If versioning can be applied alone, set
2478 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT_P to FALSE and return.
2479 :
2480 : Above B1 is more powerful and more likely to be adopted than B2. But B2
2481 : is still available and useful in some cases, for example, the cost model
2482 : does not allow much peeling.
2483 :
2484 : C) If none of above was successful then the alignment was not enhanced,
2485 : just return. */
2486 :
2487 : /* (1) Peeling to force alignment. */
2488 :
2489 : /* (1.1) Decide whether to perform peeling, how many iterations to peel, and
2490 : if vectorization may be supported by peeling with versioning.
2491 : Considerations:
2492 : - How many accesses will become aligned due to the peeling
2493 : - How many accesses will become unaligned due to the peeling,
2494 : and the cost of misaligned accesses.
2495 : - The cost of peeling (the extra runtime checks, the increase
2496 : in code size). */
2497 :
2498 377023 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2499 1067308 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2500 : {
2501 735808 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2502 735808 : if (!vect_relevant_for_alignment_p (dr_info))
2503 108973 : continue;
2504 :
2505 626835 : stmt_vec_info stmt_info = dr_info->stmt;
2506 626835 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2507 :
2508 : /* With variable VF, unsafe speculative read can be avoided for known
2509 : inbounds DRs as long as partial vectors are used. */
2510 626835 : if (!vf.is_constant ()
2511 : && dr_safe_speculative_read_required (stmt_info)
2512 : && DR_SCALAR_KNOWN_BOUNDS (dr_info))
2513 : {
2514 : dr_set_safe_speculative_read_required (stmt_info, false);
2515 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2516 : }
2517 :
2518 626835 : do_peeling = vector_alignment_reachable_p (dr_info, vf);
2519 626835 : if (do_peeling)
2520 : {
2521 549309 : if (known_alignment_for_access_p (dr_info, vectype))
2522 : {
2523 310035 : unsigned int npeel_tmp = 0;
2524 310035 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2525 310035 : size_zero_node) < 0;
2526 :
2527 : /* If known_alignment_for_access_p then we have set
2528 : DR_MISALIGNMENT which is only done if we know it at compiler
2529 : time, so it is safe to assume target alignment is constant.
2530 : */
2531 310035 : unsigned int target_align =
2532 310035 : DR_TARGET_ALIGNMENT (dr_info).to_constant ();
2533 310035 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (dr_info);
2534 310035 : poly_int64 off = 0;
2535 310035 : if (negative)
2536 2564 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2537 310035 : unsigned int mis = dr_misalignment (dr_info, vectype, off);
2538 310035 : mis = negative ? mis : -mis;
2539 310035 : if (mis != 0)
2540 15028 : npeel_tmp = (mis & (target_align - 1)) / dr_size;
2541 :
2542 : /* For multiple types, it is possible that the bigger type access
2543 : will have more than one peeling option. E.g., a loop with two
2544 : types: one of size (vector size / 4), and the other one of
2545 : size (vector size / 8). Vectorization factor will 8. If both
2546 : accesses are misaligned by 3, the first one needs one scalar
2547 : iteration to be aligned, and the second one needs 5. But the
2548 : first one will be aligned also by peeling 5 scalar
2549 : iterations, and in that case both accesses will be aligned.
2550 : Hence, except for the immediate peeling amount, we also want
2551 : to try to add full vector size, while we don't exceed
2552 : vectorization factor.
2553 : We do this automatically for cost model, since we calculate
2554 : cost for every peeling option. */
2555 310035 : poly_uint64 nscalars = npeel_tmp;
2556 310035 : if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2557 : {
2558 39812 : unsigned group_size = 1;
2559 39812 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2560 1917 : group_size = DR_GROUP_SIZE (stmt_info);
2561 39812 : nscalars = vf * group_size;
2562 : }
2563 :
2564 : /* Save info about DR in the hash table. Also include peeling
2565 : amounts according to the explanation above. Indicate
2566 : the alignment status when the ref is not aligned.
2567 : ??? Rather than using unknown alignment here we should
2568 : prune all entries from the peeling hashtable which cause
2569 : DRs to be not supported. */
2570 310035 : bool supportable_if_not_aligned
2571 : = vect_supportable_dr_alignment
2572 310035 : (loop_vinfo, dr_info, vectype, DR_MISALIGNMENT_UNKNOWN);
2573 678958 : while (known_le (npeel_tmp, nscalars))
2574 : {
2575 368923 : vect_peeling_hash_insert (&peeling_htab, loop_vinfo,
2576 : dr_info, npeel_tmp,
2577 : supportable_if_not_aligned);
2578 368923 : npeel_tmp += MAX (1, target_align / dr_size);
2579 : }
2580 :
2581 310035 : one_misalignment_known = true;
2582 : }
2583 : else
2584 : {
2585 : /* If we don't know any misalignment values, we prefer
2586 : peeling for data-ref that has the maximum number of data-refs
2587 : with the same alignment, unless the target prefers to align
2588 : stores over load. */
2589 239274 : unsigned same_align_drs = n_same_align_refs[i];
2590 239274 : if (!dr0_info
2591 239274 : || dr0_same_align_drs < same_align_drs)
2592 : {
2593 : dr0_same_align_drs = same_align_drs;
2594 : dr0_info = dr_info;
2595 : }
2596 : /* For data-refs with the same number of related
2597 : accesses prefer the one where the misalign
2598 : computation will be invariant in the outermost loop. */
2599 76584 : else if (dr0_same_align_drs == same_align_drs)
2600 : {
2601 75115 : class loop *ivloop0, *ivloop;
2602 75115 : ivloop0 = outermost_invariant_loop_for_expr
2603 75115 : (loop, DR_BASE_ADDRESS (dr0_info->dr));
2604 75115 : ivloop = outermost_invariant_loop_for_expr
2605 75115 : (loop, DR_BASE_ADDRESS (dr));
2606 75115 : if ((ivloop && !ivloop0)
2607 75115 : || (ivloop && ivloop0
2608 75107 : && flow_loop_nested_p (ivloop, ivloop0)))
2609 : dr0_info = dr_info;
2610 : }
2611 :
2612 239274 : one_misalignment_unknown = true;
2613 :
2614 : /* Check for data refs with unsupportable alignment that
2615 : can be peeled. */
2616 239274 : enum dr_alignment_support supportable_dr_alignment
2617 239274 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2618 : DR_MISALIGNMENT_UNKNOWN);
2619 239274 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2620 : {
2621 99568 : one_dr_unsupportable = true;
2622 99568 : unsupportable_dr_info = dr_info;
2623 : }
2624 :
2625 239274 : if (!first_store && DR_IS_WRITE (dr))
2626 : {
2627 51532 : first_store = dr_info;
2628 51532 : first_store_same_align_drs = same_align_drs;
2629 : }
2630 : }
2631 : }
2632 : else
2633 : {
2634 77526 : if (!aligned_access_p (dr_info, vectype))
2635 : {
2636 45523 : if (dump_enabled_p ())
2637 2088 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2638 : "vector alignment may not be reachable\n");
2639 : break;
2640 : }
2641 : }
2642 : }
2643 :
2644 : /* Check if we can possibly peel the loop. */
2645 377023 : if (!vect_can_advance_ivs_p (loop_vinfo)
2646 373413 : || !slpeel_can_duplicate_loop_p (loop, LOOP_VINFO_MAIN_EXIT (loop_vinfo),
2647 373413 : loop_preheader_edge (loop))
2648 373413 : || loop->inner
2649 : /* We don't currently maintain the LCSSA for prologue peeled inversed
2650 : loops. */
2651 748831 : || (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo)
2652 30133 : && !LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo)))
2653 : do_peeling = false;
2654 :
2655 377023 : struct _vect_peel_extended_info peel_for_known_alignment;
2656 377023 : struct _vect_peel_extended_info peel_for_unknown_alignment;
2657 377023 : struct _vect_peel_extended_info best_peel;
2658 :
2659 377023 : peel_for_unknown_alignment.inside_cost = INT_MAX;
2660 377023 : peel_for_unknown_alignment.outside_cost = INT_MAX;
2661 377023 : peel_for_unknown_alignment.peel_info.count = 0;
2662 :
2663 377023 : if (do_peeling
2664 377023 : && one_misalignment_unknown)
2665 : {
2666 : /* Check if the target requires to prefer stores over loads, i.e., if
2667 : misaligned stores are more expensive than misaligned loads (taking
2668 : drs with same alignment into account). */
2669 147084 : unsigned int load_inside_cost = 0;
2670 147084 : unsigned int load_outside_cost = 0;
2671 147084 : unsigned int store_inside_cost = 0;
2672 147084 : unsigned int store_outside_cost = 0;
2673 147084 : unsigned int estimated_npeels = vect_vf_for_cost (loop_vinfo) / 2;
2674 :
2675 147084 : stmt_vector_for_cost dummy;
2676 147084 : dummy.create (2);
2677 147084 : vect_get_peeling_costs_all_drs (loop_vinfo, dr0_info,
2678 : &load_inside_cost,
2679 : &load_outside_cost,
2680 : &dummy, &dummy, estimated_npeels);
2681 147084 : dummy.release ();
2682 :
2683 147084 : if (first_store)
2684 : {
2685 44614 : dummy.create (2);
2686 44614 : vect_get_peeling_costs_all_drs (loop_vinfo, first_store,
2687 : &store_inside_cost,
2688 : &store_outside_cost,
2689 : &dummy, &dummy,
2690 : estimated_npeels);
2691 44614 : dummy.release ();
2692 : }
2693 : else
2694 : {
2695 102470 : store_inside_cost = INT_MAX;
2696 102470 : store_outside_cost = INT_MAX;
2697 : }
2698 :
2699 147084 : if (load_inside_cost > store_inside_cost
2700 147084 : || (load_inside_cost == store_inside_cost
2701 44065 : && load_outside_cost > store_outside_cost))
2702 : {
2703 147084 : dr0_info = first_store;
2704 147084 : dr0_same_align_drs = first_store_same_align_drs;
2705 147084 : peel_for_unknown_alignment.inside_cost = store_inside_cost;
2706 147084 : peel_for_unknown_alignment.outside_cost = store_outside_cost;
2707 : }
2708 : else
2709 : {
2710 147084 : peel_for_unknown_alignment.inside_cost = load_inside_cost;
2711 147084 : peel_for_unknown_alignment.outside_cost = load_outside_cost;
2712 : }
2713 :
2714 147084 : peel_for_unknown_alignment.outside_cost
2715 147084 : += vect_get_known_peeling_cost (loop_vinfo, estimated_npeels);
2716 :
2717 147084 : peel_for_unknown_alignment.peel_info.count = dr0_same_align_drs + 1;
2718 : }
2719 :
2720 377023 : peel_for_unknown_alignment.peel_info.npeel = 0;
2721 377023 : peel_for_unknown_alignment.peel_info.dr_info = dr0_info;
2722 :
2723 377023 : best_peel = peel_for_unknown_alignment;
2724 :
2725 377023 : peel_for_known_alignment.inside_cost = INT_MAX;
2726 377023 : peel_for_known_alignment.outside_cost = INT_MAX;
2727 377023 : peel_for_known_alignment.peel_info.count = 0;
2728 377023 : peel_for_known_alignment.peel_info.dr_info = NULL;
2729 :
2730 377023 : if (do_peeling && one_misalignment_known)
2731 : {
2732 : /* Peeling is possible, but there is no data access that is not supported
2733 : unless aligned. So we try to choose the best possible peeling from
2734 : the hash table. */
2735 163558 : peel_for_known_alignment = vect_peeling_hash_choose_best_peeling
2736 163558 : (&peeling_htab, loop_vinfo);
2737 : }
2738 :
2739 : /* Compare costs of peeling for known and unknown alignment. */
2740 377023 : if (peel_for_known_alignment.peel_info.dr_info != NULL
2741 163558 : && peel_for_unknown_alignment.inside_cost
2742 : >= peel_for_known_alignment.inside_cost)
2743 : {
2744 149294 : best_peel = peel_for_known_alignment;
2745 :
2746 : /* If the best peeling for known alignment has NPEEL == 0, perform no
2747 : peeling at all except if there is an unsupportable dr that we can
2748 : align. */
2749 149294 : if (best_peel.peel_info.npeel == 0 && !one_dr_unsupportable)
2750 : do_peeling = false;
2751 : }
2752 :
2753 : /* If there is an unsupportable data ref, prefer this over all choices so far
2754 : since we'd have to discard a chosen peeling except when it accidentally
2755 : aligned the unsupportable data ref. */
2756 236321 : if (one_dr_unsupportable)
2757 : dr0_info = unsupportable_dr_info;
2758 295291 : else if (do_peeling)
2759 : {
2760 : /* Calculate the penalty for no peeling, i.e. leaving everything as-is.
2761 : TODO: Use nopeel_outside_cost or get rid of it? */
2762 68867 : unsigned nopeel_inside_cost = 0;
2763 68867 : unsigned nopeel_outside_cost = 0;
2764 :
2765 68867 : stmt_vector_for_cost dummy;
2766 68867 : dummy.create (2);
2767 68867 : vect_get_peeling_costs_all_drs (loop_vinfo, NULL, &nopeel_inside_cost,
2768 : &nopeel_outside_cost, &dummy, &dummy, 0);
2769 68867 : dummy.release ();
2770 :
2771 : /* Add epilogue costs. As we do not peel for alignment here, no prologue
2772 : costs will be recorded. */
2773 68867 : nopeel_outside_cost += vect_get_known_peeling_cost (loop_vinfo, 0);
2774 :
2775 68867 : npeel = best_peel.peel_info.npeel;
2776 68867 : dr0_info = best_peel.peel_info.dr_info;
2777 :
2778 : /* If no peeling is not more expensive than the best peeling we
2779 : have so far, don't perform any peeling. */
2780 68867 : if (nopeel_inside_cost <= best_peel.inside_cost)
2781 61459 : do_peeling = false;
2782 : }
2783 :
2784 150599 : if (do_peeling)
2785 : {
2786 81853 : stmt_vec_info stmt_info = dr0_info->stmt;
2787 81853 : if (known_alignment_for_access_p (dr0_info,
2788 : STMT_VINFO_VECTYPE (stmt_info)))
2789 : {
2790 7385 : bool negative = tree_int_cst_compare (DR_STEP (dr0_info->dr),
2791 7385 : size_zero_node) < 0;
2792 7385 : if (!npeel)
2793 : {
2794 : /* Since it's known at compile time, compute the number of
2795 : iterations in the peeled loop (the peeling factor) for use in
2796 : updating DR_MISALIGNMENT values. The peeling factor is the
2797 : vectorization factor minus the misalignment as an element
2798 : count. */
2799 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2800 0 : poly_int64 off = 0;
2801 0 : if (negative)
2802 0 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2803 0 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2804 0 : unsigned int mis
2805 0 : = dr_misalignment (dr0_info, vectype, off);
2806 0 : mis = negative ? mis : -mis;
2807 : /* If known_alignment_for_access_p then we have set
2808 : DR_MISALIGNMENT which is only done if we know it at compiler
2809 : time, so it is safe to assume target alignment is constant.
2810 : */
2811 0 : unsigned int target_align =
2812 0 : DR_TARGET_ALIGNMENT (dr0_info).to_constant ();
2813 0 : npeel = ((mis & (target_align - 1))
2814 0 : / vect_get_scalar_dr_size (dr0_info));
2815 : }
2816 :
2817 : /* For interleaved data access every iteration accesses all the
2818 : members of the group, therefore we divide the number of iterations
2819 : by the group size. */
2820 7385 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2821 281 : npeel /= DR_GROUP_SIZE (stmt_info);
2822 :
2823 7385 : if (dump_enabled_p ())
2824 284 : dump_printf_loc (MSG_NOTE, vect_location,
2825 : "Try peeling by %d\n", npeel);
2826 : }
2827 :
2828 : /* Check how peeling for alignment can support vectorization. Function
2829 : vect_peeling_supportable returns one of the three possible values:
2830 : - PEELING_KNOWN_SUPPORTED: indicates that we know all unsupported
2831 : datarefs can be aligned after peeling. We can use peeling alone.
2832 : - PEELING_MAYBE_SUPPORTED: indicates that peeling may be able to make
2833 : these datarefs aligned but we are not sure about it at compile time.
2834 : We will try peeling with versioning to add a runtime check to guard
2835 : the peeled loop.
2836 : - PEELING_UNSUPPORTED: indicates that peeling is almost impossible to
2837 : support vectorization. We will stop trying peeling. */
2838 81853 : switch (vect_peeling_supportable (loop_vinfo, dr0_info, npeel))
2839 : {
2840 : case peeling_known_supported:
2841 : break;
2842 13350 : case peeling_maybe_supported:
2843 13350 : try_peeling_with_versioning = true;
2844 13350 : break;
2845 16654 : case peeling_unsupported:
2846 16654 : do_peeling = false;
2847 16654 : break;
2848 : }
2849 :
2850 : /* Check if all datarefs are supportable and log. */
2851 81853 : if (do_peeling
2852 81853 : && npeel == 0
2853 81853 : && known_alignment_for_access_p (dr0_info,
2854 : STMT_VINFO_VECTYPE (stmt_info)))
2855 3 : return opt_result::success ();
2856 :
2857 : /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
2858 81850 : if (do_peeling)
2859 : {
2860 65196 : unsigned max_allowed_peel
2861 65196 : = param_vect_max_peeling_for_alignment;
2862 65196 : if (loop_cost_model (loop) <= VECT_COST_MODEL_CHEAP)
2863 : max_allowed_peel = 0;
2864 14652 : if (max_allowed_peel != (unsigned)-1)
2865 : {
2866 50565 : unsigned max_peel = npeel;
2867 50565 : if (max_peel == 0)
2868 : {
2869 47634 : poly_uint64 target_align = DR_TARGET_ALIGNMENT (dr0_info);
2870 47634 : unsigned HOST_WIDE_INT target_align_c;
2871 47634 : if (target_align.is_constant (&target_align_c))
2872 95268 : max_peel =
2873 47634 : target_align_c / vect_get_scalar_dr_size (dr0_info) - 1;
2874 : else
2875 : {
2876 : do_peeling = false;
2877 : if (dump_enabled_p ())
2878 : dump_printf_loc (MSG_NOTE, vect_location,
2879 : "Disable peeling, max peels set and vector"
2880 : " alignment unknown\n");
2881 : }
2882 : }
2883 50565 : if (max_peel > max_allowed_peel)
2884 : {
2885 50557 : do_peeling = false;
2886 50557 : if (dump_enabled_p ())
2887 53 : dump_printf_loc (MSG_NOTE, vect_location,
2888 : "Disable peeling, max peels reached: %d\n", max_peel);
2889 : }
2890 : }
2891 : }
2892 :
2893 : /* Cost model #2 - if peeling may result in a remaining loop not
2894 : iterating enough to be vectorized then do not peel. Since this
2895 : is a cost heuristic rather than a correctness decision, use the
2896 : most likely runtime value for variable vectorization factors. */
2897 53 : if (do_peeling
2898 14639 : && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2899 : {
2900 3203 : unsigned int assumed_vf = vect_vf_for_cost (loop_vinfo);
2901 3203 : unsigned int max_peel = npeel == 0 ? assumed_vf - 1 : npeel;
2902 3203 : if ((unsigned HOST_WIDE_INT) LOOP_VINFO_INT_NITERS (loop_vinfo)
2903 3203 : < assumed_vf + max_peel)
2904 : do_peeling = false;
2905 : }
2906 :
2907 : if (do_peeling)
2908 : {
2909 : /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
2910 : If the misalignment of DR_i is identical to that of dr0 then set
2911 : DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
2912 : dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
2913 : by the peeling factor times the element size of DR_i (MOD the
2914 : vectorization factor times the size). Otherwise, the
2915 : misalignment of DR_i must be set to unknown. */
2916 30821 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2917 16999 : if (dr != dr0_info->dr)
2918 : {
2919 3177 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2920 3177 : if (!vect_relevant_for_alignment_p (dr_info))
2921 378 : continue;
2922 :
2923 2799 : vect_update_misalignment_for_peel (dr_info, dr0_info, npeel);
2924 : }
2925 : }
2926 :
2927 81850 : if (do_peeling && !try_peeling_with_versioning)
2928 : {
2929 : /* Update data structures if peeling will be applied alone. */
2930 12746 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
2931 12746 : if (npeel)
2932 2114 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
2933 : else
2934 10632 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
2935 12746 : SET_DR_MISALIGNMENT (dr0_info,
2936 : vect_dr_misalign_for_aligned_access (dr0_info));
2937 12746 : if (dump_enabled_p ())
2938 : {
2939 351 : dump_printf_loc (MSG_NOTE, vect_location,
2940 : "Alignment of access forced using peeling.\n");
2941 351 : dump_printf_loc (MSG_NOTE, vect_location,
2942 : "Peeling for alignment will be applied.\n");
2943 : }
2944 :
2945 : /* The inside-loop cost will be accounted for in vectorizable_load
2946 : and vectorizable_store correctly with adjusted alignments.
2947 : Drop the body_cst_vec on the floor here. */
2948 12746 : return opt_result::success ();
2949 : }
2950 : }
2951 :
2952 : /* (2) Versioning to force alignment. */
2953 :
2954 : /* Try versioning if:
2955 : 1) optimize loop for speed and the cost-model is not cheap
2956 : 2) there is at least one unsupported misaligned data ref with an unknown
2957 : misalignment, and
2958 : 3) all misaligned data refs with a known misalignment are supported, and
2959 : 4) the number of runtime alignment checks is within reason. */
2960 :
2961 364274 : do_versioning
2962 364274 : = (optimize_loop_nest_for_speed_p (loop)
2963 363835 : && !loop->inner /* FORNOW */
2964 726504 : && loop_cost_model (loop) > VECT_COST_MODEL_CHEAP);
2965 :
2966 : if (do_versioning)
2967 : {
2968 358723 : FOR_EACH_VEC_ELT (datarefs, i, dr)
2969 : {
2970 269895 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
2971 269895 : if (!vect_relevant_for_alignment_p (dr_info))
2972 189457 : continue;
2973 :
2974 186049 : stmt_vec_info stmt_info = dr_info->stmt;
2975 186049 : if (STMT_VINFO_STRIDED_P (stmt_info))
2976 : {
2977 : do_versioning = false;
2978 5025 : break;
2979 : }
2980 :
2981 184969 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2982 184969 : bool negative = tree_int_cst_compare (DR_STEP (dr),
2983 184969 : size_zero_node) < 0;
2984 184969 : poly_int64 off = 0;
2985 184969 : if (negative)
2986 3381 : off = ((TYPE_VECTOR_SUBPARTS (vectype) - 1)
2987 3381 : * -TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
2988 184969 : int misalignment;
2989 184969 : if ((misalignment = dr_misalignment (dr_info, vectype, off)) == 0)
2990 105611 : continue;
2991 :
2992 79358 : enum dr_alignment_support supportable_dr_alignment
2993 79358 : = vect_supportable_dr_alignment (loop_vinfo, dr_info, vectype,
2994 : misalignment);
2995 79358 : if (supportable_dr_alignment == dr_unaligned_unsupported)
2996 : {
2997 15553 : if (misalignment != DR_MISALIGNMENT_UNKNOWN
2998 15553 : || (LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
2999 12132 : >= (unsigned) param_vect_max_version_for_alignment_checks))
3000 : {
3001 : do_versioning = false;
3002 5025 : break;
3003 : }
3004 :
3005 : /* Forcing alignment in the first iteration is no good if
3006 : we don't keep it across iterations. For now, just disable
3007 : versioning in this case.
3008 : ?? We could actually unroll the loop to achieve the required
3009 : overall step alignment, and forcing the alignment could be
3010 : done by doing some iterations of the non-vectorized loop. */
3011 11724 : if (!multiple_p (vf * DR_STEP_ALIGNMENT (dr),
3012 11724 : DR_TARGET_ALIGNMENT (dr_info)))
3013 : {
3014 : do_versioning = false;
3015 : break;
3016 : }
3017 :
3018 : /* Use "mask = DR_TARGET_ALIGNMENT - 1" to test rightmost address
3019 : bits for runtime alignment check. For example, for 16 bytes
3020 : target alignment the mask is 15 = 0xf. */
3021 11724 : poly_uint64 mask = DR_TARGET_ALIGNMENT (dr_info) - 1;
3022 :
3023 : /* FORNOW: use the same mask to test all potentially unaligned
3024 : references in the loop. */
3025 11724 : if (maybe_ne (LOOP_VINFO_PTR_MASK (loop_vinfo), 0U)
3026 11724 : && maybe_ne (LOOP_VINFO_PTR_MASK (loop_vinfo), mask))
3027 : {
3028 : do_versioning = false;
3029 : break;
3030 : }
3031 :
3032 11608 : LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
3033 11608 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (stmt_info);
3034 : }
3035 : }
3036 :
3037 : /* Versioning requires at least one misaligned data reference. */
3038 93853 : if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
3039 : do_versioning = false;
3040 5862 : else if (!do_versioning)
3041 540 : LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
3042 : }
3043 :
3044 : /* If we are trying peeling with versioning but versioning is disabled for
3045 : some reason, peeling should be turned off together. */
3046 364274 : if (try_peeling_with_versioning && !do_versioning)
3047 : do_peeling = false;
3048 :
3049 352100 : if (do_versioning)
3050 : {
3051 : const vec<stmt_vec_info> &may_misalign_stmts
3052 : = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
3053 : stmt_vec_info stmt_info;
3054 :
3055 : /* It can now be assumed that the data references in the statements
3056 : in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
3057 : of the loop being vectorized. */
3058 14290 : FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt_info)
3059 : {
3060 8968 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
3061 8968 : SET_DR_MISALIGNMENT (dr_info,
3062 : vect_dr_misalign_for_aligned_access (dr_info));
3063 8968 : if (dump_enabled_p ())
3064 146 : dump_printf_loc (MSG_NOTE, vect_location,
3065 : "Alignment of access forced using versioning.\n");
3066 : }
3067 :
3068 5322 : if (do_peeling)
3069 : {
3070 : /* This point is reached if peeling and versioning are used together
3071 : to ensure alignment. Update data structures to make sure the loop
3072 : is correctly peeled and a right runtime check is added for loop
3073 : versioning. */
3074 1076 : gcc_assert (try_peeling_with_versioning);
3075 1076 : LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0_info;
3076 1076 : LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = -1;
3077 1076 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = true;
3078 1076 : if (dump_enabled_p ())
3079 11 : dump_printf_loc (MSG_NOTE, vect_location,
3080 : "Both peeling and versioning will be applied.\n");
3081 : }
3082 : else
3083 : {
3084 : /* This point is reached if versioning is used alone. */
3085 4246 : LOOP_VINFO_ALLOW_MUTUAL_ALIGNMENT (loop_vinfo) = false;
3086 4246 : if (dump_enabled_p ())
3087 82 : dump_printf_loc (MSG_NOTE, vect_location,
3088 : "Versioning for alignment will be applied.\n");
3089 : }
3090 :
3091 5322 : return opt_result::success ();
3092 : }
3093 :
3094 : /* This point is reached if neither peeling nor versioning is being done. */
3095 358952 : gcc_assert (! (do_peeling || do_versioning));
3096 :
3097 358952 : return opt_result::success ();
3098 768310 : }
3099 :
3100 :
3101 : /* Function vect_analyze_data_refs_alignment
3102 :
3103 : Analyze the alignment of the data-references in the loop. */
3104 :
3105 : void
3106 423583 : vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
3107 : {
3108 423583 : DUMP_VECT_SCOPE ("vect_analyze_data_refs_alignment");
3109 :
3110 423583 : vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3111 423583 : struct data_reference *dr;
3112 423583 : unsigned int i;
3113 :
3114 423583 : vect_record_base_alignments (loop_vinfo);
3115 1805512 : FOR_EACH_VEC_ELT (datarefs, i, dr)
3116 : {
3117 972939 : dr_vec_info *dr_info = loop_vinfo->lookup_dr (dr);
3118 972939 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt))
3119 : {
3120 972939 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt)
3121 1263671 : && DR_GROUP_FIRST_ELEMENT (dr_info->stmt) != dr_info->stmt)
3122 129465 : continue;
3123 :
3124 843474 : vect_compute_data_ref_alignment (loop_vinfo, dr_info,
3125 : STMT_VINFO_VECTYPE (dr_info->stmt));
3126 : }
3127 : }
3128 423583 : }
3129 :
3130 :
3131 : /* Analyze alignment of DRs of stmts in NODE. */
3132 :
3133 : static bool
3134 832894 : vect_slp_analyze_node_alignment (vec_info *vinfo, slp_tree node)
3135 : {
3136 : /* Alignment is maintained in the first element of the group. */
3137 832894 : stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
3138 832894 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (first_stmt_info);
3139 832894 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
3140 832894 : tree vectype = SLP_TREE_VECTYPE (node);
3141 832894 : poly_uint64 vector_alignment
3142 832894 : = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
3143 : BITS_PER_UNIT);
3144 832894 : if (dr_info->misalignment == DR_MISALIGNMENT_UNINITIALIZED)
3145 792025 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3146 : /* Re-analyze alignment when we're facing a vectorization with a bigger
3147 : alignment requirement. */
3148 40869 : else if (known_lt (dr_info->target_alignment, vector_alignment))
3149 : {
3150 79 : poly_uint64 old_target_alignment = dr_info->target_alignment;
3151 79 : int old_misalignment = dr_info->misalignment;
3152 79 : vect_compute_data_ref_alignment (vinfo, dr_info, SLP_TREE_VECTYPE (node));
3153 : /* But keep knowledge about a smaller alignment. */
3154 79 : if (old_misalignment != DR_MISALIGNMENT_UNKNOWN
3155 38 : && dr_info->misalignment == DR_MISALIGNMENT_UNKNOWN)
3156 : {
3157 1 : dr_info->target_alignment = old_target_alignment;
3158 1 : dr_info->misalignment = old_misalignment;
3159 : }
3160 : }
3161 : /* When we ever face unordered target alignments the first one wins in terms
3162 : of analyzing and the other will become unknown in dr_misalignment. */
3163 832894 : return true;
3164 : }
3165 :
3166 : /* Function vect_slp_analyze_instance_alignment
3167 :
3168 : Analyze the alignment of the data-references in the SLP instance.
3169 : Return FALSE if a data reference is found that cannot be vectorized. */
3170 :
3171 : bool
3172 808954 : vect_slp_analyze_instance_alignment (vec_info *vinfo,
3173 : slp_instance instance)
3174 : {
3175 808954 : DUMP_VECT_SCOPE ("vect_slp_analyze_instance_alignment");
3176 :
3177 808954 : slp_tree node;
3178 808954 : unsigned i;
3179 970196 : FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, node)
3180 161242 : if (! vect_slp_analyze_node_alignment (vinfo, node))
3181 : return false;
3182 :
3183 808954 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
3184 808954 : && ! vect_slp_analyze_node_alignment
3185 671652 : (vinfo, SLP_INSTANCE_TREE (instance)))
3186 : return false;
3187 :
3188 : return true;
3189 : }
3190 :
3191 :
3192 : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3193 : accesses of legal size, step, etc. Detect gaps, single element
3194 : interleaving, and other special cases. Set grouped access info.
3195 : Collect groups of strided stores for further use in SLP analysis.
3196 : Worker for vect_analyze_group_access. */
3197 :
3198 : static bool
3199 12941092 : vect_analyze_group_access_1 (vec_info *vinfo, dr_vec_info *dr_info)
3200 : {
3201 12941092 : data_reference *dr = dr_info->dr;
3202 12941092 : tree step = DR_STEP (dr);
3203 12941092 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3204 12941092 : HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
3205 12941092 : stmt_vec_info stmt_info = dr_info->stmt;
3206 12941092 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3207 12941092 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3208 12941092 : HOST_WIDE_INT dr_step = -1;
3209 12941092 : HOST_WIDE_INT groupsize, last_accessed_element = 1;
3210 12941092 : bool slp_impossible = false;
3211 :
3212 : /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
3213 : size of the interleaving group (including gaps). */
3214 12941092 : if (tree_fits_shwi_p (step))
3215 : {
3216 12931518 : dr_step = tree_to_shwi (step);
3217 : /* Check that STEP is a multiple of type size. Otherwise there is
3218 : a non-element-sized gap at the end of the group which we
3219 : cannot represent in DR_GROUP_GAP or DR_GROUP_SIZE.
3220 : ??? As we can handle non-constant step fine here we should
3221 : simply remove uses of DR_GROUP_GAP between the last and first
3222 : element and instead rely on DR_STEP. DR_GROUP_SIZE then would
3223 : simply not include that gap. */
3224 12931518 : if ((dr_step % type_size) != 0)
3225 : {
3226 494 : if (dump_enabled_p ())
3227 27 : dump_printf_loc (MSG_NOTE, vect_location,
3228 : "Step %T is not a multiple of the element size"
3229 : " for %T\n",
3230 : step, DR_REF (dr));
3231 494 : return false;
3232 : }
3233 12931024 : groupsize = absu_hwi (dr_step) / type_size;
3234 : }
3235 : else
3236 : groupsize = 0;
3237 :
3238 : /* Not consecutive access is possible only if it is a part of interleaving. */
3239 12940598 : if (!DR_GROUP_FIRST_ELEMENT (stmt_info))
3240 : {
3241 : /* Check if it this DR is a part of interleaving, and is a single
3242 : element of the group that is accessed in the loop. */
3243 :
3244 : /* Gaps are supported only for loads. STEP must be a multiple of the type
3245 : size. */
3246 8714318 : if (DR_IS_READ (dr)
3247 5205821 : && (dr_step % type_size) == 0
3248 : && groupsize > 0
3249 : /* This could be UINT_MAX but as we are generating code in a very
3250 : inefficient way we have to cap earlier.
3251 : See PR91403 for example. */
3252 5205821 : && groupsize <= 4096)
3253 : {
3254 73284 : DR_GROUP_FIRST_ELEMENT (stmt_info) = stmt_info;
3255 73284 : DR_GROUP_SIZE (stmt_info) = groupsize;
3256 73284 : DR_GROUP_GAP (stmt_info) = groupsize - 1;
3257 73284 : if (dump_enabled_p ())
3258 1492 : dump_printf_loc (MSG_NOTE, vect_location,
3259 : "Detected single element interleaving %T"
3260 : " step %T\n",
3261 : DR_REF (dr), step);
3262 :
3263 73284 : return true;
3264 : }
3265 :
3266 8641034 : if (dump_enabled_p ())
3267 3080 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3268 : "not consecutive access %G", stmt_info->stmt);
3269 :
3270 8641034 : if (bb_vinfo)
3271 : {
3272 : /* Mark the statement as unvectorizable. */
3273 8621679 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
3274 8621679 : return true;
3275 : }
3276 :
3277 19355 : if (dump_enabled_p ())
3278 305 : dump_printf_loc (MSG_NOTE, vect_location, "using strided accesses\n");
3279 19355 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3280 19355 : return true;
3281 : }
3282 :
3283 4226280 : if (DR_GROUP_FIRST_ELEMENT (stmt_info) == stmt_info)
3284 : {
3285 : /* First stmt in the interleaving chain. Check the chain. */
3286 1536855 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3287 1536855 : struct data_reference *data_ref = dr;
3288 1536855 : unsigned int count = 1;
3289 1536855 : tree prev_init = DR_INIT (data_ref);
3290 1536855 : HOST_WIDE_INT diff, gaps = 0;
3291 :
3292 : /* By construction, all group members have INTEGER_CST DR_INITs. */
3293 4226289 : while (next)
3294 : {
3295 : /* We never have the same DR multiple times. */
3296 2689496 : gcc_assert (tree_int_cst_compare (DR_INIT (data_ref),
3297 : DR_INIT (STMT_VINFO_DATA_REF (next))) != 0);
3298 :
3299 2689496 : data_ref = STMT_VINFO_DATA_REF (next);
3300 :
3301 : /* All group members have the same STEP by construction. */
3302 2689496 : gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
3303 :
3304 : /* Check that the distance between two accesses is equal to the type
3305 : size. Otherwise, we have gaps. */
3306 2689496 : diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
3307 2689496 : - TREE_INT_CST_LOW (prev_init)) / type_size;
3308 2689496 : if (diff < 1 || diff > UINT_MAX)
3309 : {
3310 : /* For artificial testcases with array accesses with large
3311 : constant indices we can run into overflow issues which
3312 : can end up fooling the groupsize constraint below so
3313 : check the individual gaps (which are represented as
3314 : unsigned int) as well. */
3315 0 : if (dump_enabled_p ())
3316 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3317 : "interleaved access with gap larger "
3318 : "than representable\n");
3319 0 : return false;
3320 : }
3321 2689496 : if (diff != 1)
3322 : {
3323 : /* FORNOW: SLP of accesses with gaps is not supported. */
3324 105165 : slp_impossible = true;
3325 105165 : if (DR_IS_WRITE (data_ref))
3326 : {
3327 62 : if (dump_enabled_p ())
3328 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3329 : "interleaved store with gaps\n");
3330 62 : return false;
3331 : }
3332 :
3333 105103 : gaps += diff - 1;
3334 : }
3335 :
3336 2689434 : last_accessed_element += diff;
3337 :
3338 : /* Store the gap from the previous member of the group. If there is no
3339 : gap in the access, DR_GROUP_GAP is always 1. */
3340 2689434 : DR_GROUP_GAP (next) = diff;
3341 :
3342 2689434 : prev_init = DR_INIT (data_ref);
3343 2689434 : next = DR_GROUP_NEXT_ELEMENT (next);
3344 : /* Count the number of data-refs in the chain. */
3345 2689434 : count++;
3346 : }
3347 :
3348 1536793 : if (groupsize == 0)
3349 1465844 : groupsize = count + gaps;
3350 :
3351 : /* This could be UINT_MAX but as we are generating code in a very
3352 : inefficient way we have to cap earlier. See PR78699 for example. */
3353 1536793 : if (groupsize > 4096)
3354 : {
3355 1 : if (dump_enabled_p ())
3356 1 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3357 : "group is too large\n");
3358 1 : return false;
3359 : }
3360 :
3361 : /* Check that the size of the interleaving is equal to count for stores,
3362 : i.e., that there are no gaps. */
3363 1536792 : if (groupsize != count
3364 108512 : && !DR_IS_READ (dr))
3365 : {
3366 12016 : groupsize = count;
3367 12016 : STMT_VINFO_STRIDED_P (stmt_info) = true;
3368 : }
3369 :
3370 : /* If there is a gap after the last load in the group it is the
3371 : difference between the groupsize and the last accessed
3372 : element.
3373 : When there is no gap, this difference should be 0. */
3374 1536792 : DR_GROUP_GAP (stmt_info) = groupsize - last_accessed_element;
3375 :
3376 1536792 : DR_GROUP_SIZE (stmt_info) = groupsize;
3377 1536792 : if (dump_enabled_p ())
3378 : {
3379 8025 : dump_printf_loc (MSG_NOTE, vect_location,
3380 : "Detected interleaving ");
3381 8025 : if (DR_IS_READ (dr))
3382 4343 : dump_printf (MSG_NOTE, "load ");
3383 3682 : else if (STMT_VINFO_STRIDED_P (stmt_info))
3384 472 : dump_printf (MSG_NOTE, "strided store ");
3385 : else
3386 3210 : dump_printf (MSG_NOTE, "store ");
3387 8025 : dump_printf (MSG_NOTE, "of size %u\n",
3388 : (unsigned)groupsize);
3389 8025 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", stmt_info->stmt);
3390 8025 : next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3391 39529 : while (next)
3392 : {
3393 31504 : if (DR_GROUP_GAP (next) != 1)
3394 317 : dump_printf_loc (MSG_NOTE, vect_location,
3395 : "\t<gap of %d elements>\n",
3396 317 : DR_GROUP_GAP (next) - 1);
3397 31504 : dump_printf_loc (MSG_NOTE, vect_location, "\t%G", next->stmt);
3398 31504 : next = DR_GROUP_NEXT_ELEMENT (next);
3399 : }
3400 8025 : if (DR_GROUP_GAP (stmt_info) != 0)
3401 398 : dump_printf_loc (MSG_NOTE, vect_location,
3402 : "\t<gap of %d elements>\n",
3403 398 : DR_GROUP_GAP (stmt_info));
3404 : }
3405 :
3406 : /* SLP: create an SLP data structure for every interleaving group of
3407 : stores for further analysis in vect_analyse_slp. */
3408 1536792 : if (DR_IS_WRITE (dr) && !slp_impossible)
3409 : {
3410 936810 : if (loop_vinfo)
3411 30031 : LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt_info);
3412 936810 : if (bb_vinfo)
3413 906779 : BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt_info);
3414 : }
3415 : }
3416 :
3417 : return true;
3418 : }
3419 :
3420 : /* Analyze groups of accesses: check that DR_INFO belongs to a group of
3421 : accesses of legal size, step, etc. Detect gaps, single element
3422 : interleaving, and other special cases. Set grouped access info.
3423 : Collect groups of strided stores for further use in SLP analysis. */
3424 :
3425 : static bool
3426 12941092 : vect_analyze_group_access (vec_info *vinfo, dr_vec_info *dr_info)
3427 : {
3428 12941092 : if (!vect_analyze_group_access_1 (vinfo, dr_info))
3429 : {
3430 : /* Dissolve the group if present. */
3431 557 : stmt_vec_info stmt_info = DR_GROUP_FIRST_ELEMENT (dr_info->stmt);
3432 788 : while (stmt_info)
3433 : {
3434 231 : stmt_vec_info next = DR_GROUP_NEXT_ELEMENT (stmt_info);
3435 231 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3436 231 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3437 231 : stmt_info = next;
3438 : }
3439 : return false;
3440 : }
3441 : return true;
3442 : }
3443 :
3444 : /* Analyze the access pattern of the data-reference DR_INFO.
3445 : In case of non-consecutive accesses call vect_analyze_group_access() to
3446 : analyze groups of accesses. */
3447 :
3448 : static bool
3449 13708316 : vect_analyze_data_ref_access (vec_info *vinfo, dr_vec_info *dr_info)
3450 : {
3451 13708316 : data_reference *dr = dr_info->dr;
3452 13708316 : tree step = DR_STEP (dr);
3453 13708316 : tree scalar_type = TREE_TYPE (DR_REF (dr));
3454 13708316 : stmt_vec_info stmt_info = dr_info->stmt;
3455 13708316 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3456 13708316 : class loop *loop = NULL;
3457 :
3458 13708316 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
3459 : return true;
3460 :
3461 13606939 : if (loop_vinfo)
3462 982538 : loop = LOOP_VINFO_LOOP (loop_vinfo);
3463 :
3464 13606939 : if (loop_vinfo && !step)
3465 : {
3466 0 : if (dump_enabled_p ())
3467 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3468 : "bad data-ref access in loop\n");
3469 0 : return false;
3470 : }
3471 :
3472 : /* Allow loads with zero step in inner-loop vectorization. */
3473 13606939 : if (loop_vinfo && integer_zerop (step))
3474 : {
3475 14248 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3476 14248 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3477 14248 : if (!nested_in_vect_loop_p (loop, stmt_info))
3478 13987 : return DR_IS_READ (dr);
3479 : /* Allow references with zero step for outer loops marked
3480 : with pragma omp simd only - it guarantees absence of
3481 : loop-carried dependencies between inner loop iterations. */
3482 261 : if (loop->safelen < 2)
3483 : {
3484 225 : if (dump_enabled_p ())
3485 6 : dump_printf_loc (MSG_NOTE, vect_location,
3486 : "zero step in inner loop of nest\n");
3487 225 : return false;
3488 : }
3489 : }
3490 :
3491 13592691 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3492 : {
3493 : /* Interleaved accesses are not yet supported within outer-loop
3494 : vectorization for references in the inner-loop. */
3495 5850 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3496 5850 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3497 :
3498 : /* For the rest of the analysis we use the outer-loop step. */
3499 5850 : step = STMT_VINFO_DR_STEP (stmt_info);
3500 5850 : if (integer_zerop (step))
3501 : {
3502 1290 : if (dump_enabled_p ())
3503 241 : dump_printf_loc (MSG_NOTE, vect_location,
3504 : "zero step in outer loop.\n");
3505 1290 : return DR_IS_READ (dr);
3506 : }
3507 : }
3508 :
3509 : /* Consecutive? */
3510 13591437 : if (TREE_CODE (step) == INTEGER_CST)
3511 : {
3512 13552192 : HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
3513 13552192 : if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
3514 13552192 : || (dr_step < 0
3515 29302 : && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
3516 : {
3517 : /* Mark that it is not interleaving. */
3518 617767 : DR_GROUP_FIRST_ELEMENT (stmt_info) = NULL;
3519 617767 : DR_GROUP_NEXT_ELEMENT (stmt_info) = NULL;
3520 617767 : return true;
3521 : }
3522 : }
3523 :
3524 12973670 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
3525 : {
3526 3367 : if (dump_enabled_p ())
3527 163 : dump_printf_loc (MSG_NOTE, vect_location,
3528 : "grouped access in outer loop.\n");
3529 3367 : return false;
3530 : }
3531 :
3532 :
3533 : /* Assume this is a DR handled by non-constant strided load case. */
3534 12970303 : if (TREE_CODE (step) != INTEGER_CST)
3535 38785 : return (STMT_VINFO_STRIDED_P (stmt_info)
3536 38785 : && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
3537 9574 : || vect_analyze_group_access (vinfo, dr_info)));
3538 :
3539 : /* Not consecutive access - check if it's a part of interleaving group. */
3540 12931518 : return vect_analyze_group_access (vinfo, dr_info);
3541 : }
3542 :
3543 : /* Compare two data-references DRA and DRB to group them into chunks
3544 : suitable for grouping. */
3545 :
3546 : static int
3547 359538799 : dr_group_sort_cmp (const void *dra_, const void *drb_)
3548 : {
3549 359538799 : dr_vec_info *dra_info = *(dr_vec_info **)const_cast<void *>(dra_);
3550 359538799 : dr_vec_info *drb_info = *(dr_vec_info **)const_cast<void *>(drb_);
3551 359538799 : data_reference_p dra = dra_info->dr;
3552 359538799 : data_reference_p drb = drb_info->dr;
3553 359538799 : int cmp;
3554 :
3555 : /* Stabilize sort. */
3556 359538799 : if (dra == drb)
3557 : return 0;
3558 :
3559 : /* Different group IDs lead never belong to the same group. */
3560 359538799 : if (dra_info->group != drb_info->group)
3561 395109718 : return dra_info->group < drb_info->group ? -1 : 1;
3562 :
3563 : /* Ordering of DRs according to base. */
3564 99584455 : cmp = data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3565 : DR_BASE_ADDRESS (drb));
3566 99584455 : if (cmp != 0)
3567 : return cmp;
3568 :
3569 : /* And according to DR_OFFSET. */
3570 53935723 : cmp = data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
3571 53935723 : if (cmp != 0)
3572 : return cmp;
3573 :
3574 : /* Put reads before writes. */
3575 53584040 : if (DR_IS_READ (dra) != DR_IS_READ (drb))
3576 4349658 : return DR_IS_READ (dra) ? -1 : 1;
3577 :
3578 : /* Then sort after access size. */
3579 50625029 : cmp = data_ref_compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
3580 50625029 : TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
3581 50625029 : if (cmp != 0)
3582 : return cmp;
3583 :
3584 : /* And after step. */
3585 43695013 : cmp = data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb));
3586 43695013 : if (cmp != 0)
3587 : return cmp;
3588 :
3589 : /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
3590 43687571 : cmp = data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb));
3591 43687571 : if (cmp == 0)
3592 501951 : return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
3593 : return cmp;
3594 : }
3595 :
3596 : /* If OP is the result of a conversion, return the unconverted value,
3597 : otherwise return null. */
3598 :
3599 : static tree
3600 366 : strip_conversion (tree op)
3601 : {
3602 366 : if (TREE_CODE (op) != SSA_NAME)
3603 : return NULL_TREE;
3604 366 : gimple *stmt = SSA_NAME_DEF_STMT (op);
3605 366 : if (!is_gimple_assign (stmt)
3606 366 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
3607 : return NULL_TREE;
3608 186 : return gimple_assign_rhs1 (stmt);
3609 : }
3610 :
3611 : /* Return true if vectorizable_* routines can handle statements STMT1_INFO
3612 : and STMT2_INFO being in a single group. When ALLOW_SLP_P, masked loads can
3613 : be grouped in SLP mode. */
3614 :
3615 : static bool
3616 7189555 : can_group_stmts_p (stmt_vec_info stmt1_info, stmt_vec_info stmt2_info,
3617 : bool allow_slp_p)
3618 : {
3619 7189555 : if (gimple_assign_single_p (stmt1_info->stmt))
3620 7188085 : return gimple_assign_single_p (stmt2_info->stmt);
3621 :
3622 1470 : gcall *call1 = dyn_cast <gcall *> (stmt1_info->stmt);
3623 1470 : if (call1 && gimple_call_internal_p (call1))
3624 : {
3625 : /* Check for two masked loads or two masked stores. */
3626 1687 : gcall *call2 = dyn_cast <gcall *> (stmt2_info->stmt);
3627 1454 : if (!call2 || !gimple_call_internal_p (call2))
3628 : return false;
3629 1454 : internal_fn ifn = gimple_call_internal_fn (call1);
3630 1454 : if (ifn != IFN_MASK_LOAD && ifn != IFN_MASK_STORE)
3631 : return false;
3632 1454 : if (ifn != gimple_call_internal_fn (call2))
3633 : return false;
3634 :
3635 : /* Check that the masks are the same. Cope with casts of masks,
3636 : like those created by build_mask_conversion. */
3637 1454 : tree mask1 = gimple_call_arg (call1, 2);
3638 1454 : tree mask2 = gimple_call_arg (call2, 2);
3639 1454 : if (!operand_equal_p (mask1, mask2, 0) && !allow_slp_p)
3640 : {
3641 273 : mask1 = strip_conversion (mask1);
3642 273 : if (!mask1)
3643 : return false;
3644 93 : mask2 = strip_conversion (mask2);
3645 93 : if (!mask2)
3646 : return false;
3647 93 : if (!operand_equal_p (mask1, mask2, 0))
3648 : return false;
3649 : }
3650 1237 : return true;
3651 : }
3652 :
3653 : return false;
3654 : }
3655 :
3656 : /* Function vect_analyze_data_ref_accesses.
3657 :
3658 : Analyze the access pattern of all the data references in the loop.
3659 :
3660 : FORNOW: the only access pattern that is considered vectorizable is a
3661 : simple step 1 (consecutive) access.
3662 :
3663 : FORNOW: handle only arrays and pointer accesses. */
3664 :
3665 : opt_result
3666 2703302 : vect_analyze_data_ref_accesses (vec_info *vinfo,
3667 : vec<int> *dataref_groups)
3668 : {
3669 2703302 : unsigned int i;
3670 2703302 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
3671 :
3672 2703302 : DUMP_VECT_SCOPE ("vect_analyze_data_ref_accesses");
3673 :
3674 2703302 : if (datarefs.is_empty ())
3675 1082794 : return opt_result::success ();
3676 :
3677 : /* Sort the array of datarefs to make building the interleaving chains
3678 : linear. Don't modify the original vector's order, it is needed for
3679 : determining what dependencies are reversed. */
3680 1620508 : vec<dr_vec_info *> datarefs_copy;
3681 1620508 : datarefs_copy.create (datarefs.length ());
3682 17178730 : for (unsigned i = 0; i < datarefs.length (); i++)
3683 : {
3684 15558222 : dr_vec_info *dr_info = vinfo->lookup_dr (datarefs[i]);
3685 : /* If the caller computed DR grouping use that, otherwise group by
3686 : basic blocks. */
3687 15558222 : if (dataref_groups)
3688 14460182 : dr_info->group = (*dataref_groups)[i];
3689 : else
3690 1098040 : dr_info->group = gimple_bb (DR_STMT (datarefs[i]))->index;
3691 15558222 : datarefs_copy.quick_push (dr_info);
3692 : }
3693 1620508 : datarefs_copy.qsort (dr_group_sort_cmp);
3694 1620508 : hash_set<stmt_vec_info> to_fixup;
3695 :
3696 : /* Build the interleaving chains. */
3697 14699381 : for (i = 0; i < datarefs_copy.length () - 1;)
3698 : {
3699 11458365 : dr_vec_info *dr_info_a = datarefs_copy[i];
3700 11458365 : data_reference_p dra = dr_info_a->dr;
3701 11458365 : int dra_group_id = dr_info_a->group;
3702 11458365 : stmt_vec_info stmtinfo_a = dr_info_a->stmt;
3703 11458365 : stmt_vec_info lastinfo = NULL;
3704 11458365 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
3705 9762849 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a))
3706 : {
3707 1761126 : ++i;
3708 1761126 : continue;
3709 : }
3710 25499939 : for (i = i + 1; i < datarefs_copy.length (); ++i)
3711 : {
3712 12176588 : dr_vec_info *dr_info_b = datarefs_copy[i];
3713 12176588 : data_reference_p drb = dr_info_b->dr;
3714 12176588 : int drb_group_id = dr_info_b->group;
3715 12176588 : stmt_vec_info stmtinfo_b = dr_info_b->stmt;
3716 12176588 : if (!STMT_VINFO_VECTORIZABLE (stmtinfo_b)
3717 11854506 : || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
3718 : break;
3719 :
3720 : /* ??? Imperfect sorting (non-compatible types, non-modulo
3721 : accesses, same accesses) can lead to a group to be artificially
3722 : split here as we don't just skip over those. If it really
3723 : matters we can push those to a worklist and re-iterate
3724 : over them. The we can just skip ahead to the next DR here. */
3725 :
3726 : /* DRs in a different DR group should not be put into the same
3727 : interleaving group. */
3728 11850858 : if (dra_group_id != drb_group_id)
3729 : break;
3730 :
3731 : /* Check that the data-refs have same first location (except init)
3732 : and they are both either store or load (not load and store,
3733 : not masked loads or stores). */
3734 7477196 : if (DR_IS_READ (dra) != DR_IS_READ (drb)
3735 6143356 : || data_ref_compare_tree (DR_BASE_ADDRESS (dra),
3736 : DR_BASE_ADDRESS (drb)) != 0
3737 4485233 : || data_ref_compare_tree (DR_OFFSET (dra), DR_OFFSET (drb)) != 0
3738 11943194 : || !can_group_stmts_p (stmtinfo_a, stmtinfo_b, true))
3739 : break;
3740 :
3741 : /* Check that the data-refs have the same constant size. */
3742 4465973 : tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
3743 4465973 : tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
3744 4465973 : if (!tree_fits_uhwi_p (sza)
3745 4465973 : || !tree_fits_uhwi_p (szb)
3746 8931946 : || !tree_int_cst_equal (sza, szb))
3747 : break;
3748 :
3749 : /* Check that the data-refs have the same step. */
3750 4106089 : if (data_ref_compare_tree (DR_STEP (dra), DR_STEP (drb)) != 0)
3751 : break;
3752 :
3753 : /* Check the types are compatible.
3754 : ??? We don't distinguish this during sorting. */
3755 4105355 : if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
3756 4105355 : TREE_TYPE (DR_REF (drb))))
3757 : break;
3758 :
3759 : /* Check that the DR_INITs are compile-time constants. */
3760 2940561 : if (!tree_fits_shwi_p (DR_INIT (dra))
3761 2940561 : || !tree_fits_shwi_p (DR_INIT (drb)))
3762 : break;
3763 :
3764 : /* Different .GOMP_SIMD_LANE calls still give the same lane,
3765 : just hold extra information. */
3766 2940561 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_a)
3767 1240 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmtinfo_b)
3768 2941801 : && data_ref_compare_tree (DR_INIT (dra), DR_INIT (drb)) == 0)
3769 : break;
3770 :
3771 : /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
3772 2939321 : HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
3773 2939321 : HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
3774 2939321 : HOST_WIDE_INT init_prev
3775 2939321 : = TREE_INT_CST_LOW (DR_INIT (datarefs_copy[i-1]->dr));
3776 2939321 : gcc_assert (init_a <= init_b
3777 : && init_a <= init_prev
3778 : && init_prev <= init_b);
3779 :
3780 : /* Do not place the same access in the interleaving chain twice. */
3781 2939321 : if (init_b == init_prev)
3782 : {
3783 30421 : gcc_assert (gimple_uid (DR_STMT (datarefs_copy[i-1]->dr))
3784 : < gimple_uid (DR_STMT (drb)));
3785 : /* Simply link in duplicates and fix up the chain below. */
3786 : }
3787 : else
3788 : {
3789 : /* If init_b == init_a + the size of the type * k, we have an
3790 : interleaving, and DRA is accessed before DRB. */
3791 2908900 : unsigned HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
3792 2908900 : if (type_size_a == 0
3793 2908900 : || (((unsigned HOST_WIDE_INT)init_b - init_a)
3794 2908900 : % type_size_a != 0))
3795 : break;
3796 :
3797 : /* If we have a store, the accesses are adjacent. This splits
3798 : groups into chunks we support (we don't support vectorization
3799 : of stores with gaps). */
3800 2907172 : if (!DR_IS_READ (dra)
3801 1901963 : && (((unsigned HOST_WIDE_INT)init_b - init_prev)
3802 : != type_size_a))
3803 : break;
3804 :
3805 : /* For datarefs with big gap, it's better to split them into different
3806 : groups.
3807 : .i.e a[0], a[1], a[2], .. a[7], a[100], a[101],..., a[107] */
3808 2722425 : if ((unsigned HOST_WIDE_INT)(init_b - init_prev)
3809 : > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
3810 : break;
3811 :
3812 : /* If the step (if not zero or non-constant) is smaller than the
3813 : difference between data-refs' inits this splits groups into
3814 : suitable sizes. */
3815 2712728 : if (tree_fits_shwi_p (DR_STEP (dra)))
3816 : {
3817 2706420 : unsigned HOST_WIDE_INT step
3818 2706420 : = absu_hwi (tree_to_shwi (DR_STEP (dra)));
3819 2706420 : if (step != 0
3820 165619 : && step <= ((unsigned HOST_WIDE_INT)init_b - init_a))
3821 : break;
3822 : }
3823 : }
3824 :
3825 2723827 : if (dump_enabled_p ())
3826 32400 : dump_printf_loc (MSG_NOTE, vect_location,
3827 32400 : DR_IS_READ (dra)
3828 : ? "Detected interleaving load %T and %T\n"
3829 : : "Detected interleaving store %T and %T\n",
3830 : DR_REF (dra), DR_REF (drb));
3831 :
3832 : /* Link the found element into the group list. */
3833 2723827 : if (!DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3834 : {
3835 1514780 : DR_GROUP_FIRST_ELEMENT (stmtinfo_a) = stmtinfo_a;
3836 1514780 : lastinfo = stmtinfo_a;
3837 : }
3838 2723827 : DR_GROUP_FIRST_ELEMENT (stmtinfo_b) = stmtinfo_a;
3839 2723827 : DR_GROUP_NEXT_ELEMENT (lastinfo) = stmtinfo_b;
3840 2723827 : lastinfo = stmtinfo_b;
3841 :
3842 2723827 : if (! STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3843 : {
3844 2723557 : STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a)
3845 2723557 : = !can_group_stmts_p (stmtinfo_a, stmtinfo_b, false);
3846 :
3847 2723557 : if (dump_enabled_p () && STMT_VINFO_SLP_VECT_ONLY (stmtinfo_a))
3848 114 : dump_printf_loc (MSG_NOTE, vect_location,
3849 : "Load suitable for SLP vectorization only.\n");
3850 : }
3851 :
3852 2723827 : if (init_b == init_prev
3853 30421 : && !to_fixup.add (DR_GROUP_FIRST_ELEMENT (stmtinfo_a))
3854 2740816 : && dump_enabled_p ())
3855 201 : dump_printf_loc (MSG_NOTE, vect_location,
3856 : "Queuing group with duplicate access for fixup\n");
3857 : }
3858 : }
3859 :
3860 : /* Fixup groups with duplicate entries by splitting it. */
3861 1664624 : while (1)
3862 : {
3863 1664624 : hash_set<stmt_vec_info>::iterator it = to_fixup.begin ();
3864 1664624 : if (!(it != to_fixup.end ()))
3865 : break;
3866 44116 : stmt_vec_info grp = *it;
3867 44116 : to_fixup.remove (grp);
3868 :
3869 : /* Find the earliest duplicate group member. */
3870 44116 : unsigned first_duplicate = -1u;
3871 44116 : stmt_vec_info next, g = grp;
3872 279429 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3873 : {
3874 191197 : if (tree_int_cst_equal (DR_INIT (STMT_VINFO_DR_INFO (next)->dr),
3875 191197 : DR_INIT (STMT_VINFO_DR_INFO (g)->dr))
3876 191197 : && gimple_uid (STMT_VINFO_STMT (next)) < first_duplicate)
3877 : first_duplicate = gimple_uid (STMT_VINFO_STMT (next));
3878 : g = next;
3879 : }
3880 44116 : if (first_duplicate == -1U)
3881 16989 : continue;
3882 :
3883 : /* Then move all stmts after the first duplicate to a new group.
3884 : Note this is a heuristic but one with the property that *it
3885 : is fixed up completely. */
3886 27127 : g = grp;
3887 27127 : stmt_vec_info newgroup = NULL, ng = grp;
3888 242962 : while ((next = DR_GROUP_NEXT_ELEMENT (g)))
3889 : {
3890 188708 : if (gimple_uid (STMT_VINFO_STMT (next)) >= first_duplicate)
3891 : {
3892 182436 : DR_GROUP_NEXT_ELEMENT (g) = DR_GROUP_NEXT_ELEMENT (next);
3893 182436 : if (!newgroup)
3894 : {
3895 27127 : newgroup = next;
3896 27127 : STMT_VINFO_SLP_VECT_ONLY (newgroup)
3897 27127 : = STMT_VINFO_SLP_VECT_ONLY (grp);
3898 : }
3899 : else
3900 155309 : DR_GROUP_NEXT_ELEMENT (ng) = next;
3901 182436 : ng = next;
3902 182436 : DR_GROUP_FIRST_ELEMENT (ng) = newgroup;
3903 : }
3904 : else
3905 : g = DR_GROUP_NEXT_ELEMENT (g);
3906 : }
3907 27127 : DR_GROUP_NEXT_ELEMENT (ng) = NULL;
3908 :
3909 : /* Fixup the new group which still may contain duplicates. */
3910 27127 : to_fixup.add (newgroup);
3911 : }
3912 :
3913 1620508 : dr_vec_info *dr_info;
3914 17156620 : FOR_EACH_VEC_ELT (datarefs_copy, i, dr_info)
3915 : {
3916 15544097 : if (STMT_VINFO_VECTORIZABLE (dr_info->stmt)
3917 15544097 : && !vect_analyze_data_ref_access (vinfo, dr_info))
3918 : {
3919 8039 : if (dump_enabled_p ())
3920 292 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3921 : "not vectorized: complicated access pattern.\n");
3922 :
3923 8039 : if (is_a <bb_vec_info> (vinfo))
3924 : {
3925 : /* Mark the statement as not vectorizable. */
3926 54 : STMT_VINFO_VECTORIZABLE (dr_info->stmt) = false;
3927 54 : continue;
3928 : }
3929 : else
3930 : {
3931 7985 : datarefs_copy.release ();
3932 7985 : return opt_result::failure_at (dr_info->stmt->stmt,
3933 : "not vectorized:"
3934 : " complicated access pattern.\n");
3935 : }
3936 : }
3937 : }
3938 :
3939 1612523 : datarefs_copy.release ();
3940 1612523 : return opt_result::success ();
3941 1620508 : }
3942 :
3943 : /* Function vect_vfa_segment_size.
3944 :
3945 : Input:
3946 : DR_INFO: The data reference.
3947 : LENGTH_FACTOR: segment length to consider.
3948 :
3949 : Return a value suitable for the dr_with_seg_len::seg_len field.
3950 : This is the "distance travelled" by the pointer from the first
3951 : iteration in the segment to the last. Note that it does not include
3952 : the size of the access; in effect it only describes the first byte. */
3953 :
3954 : static tree
3955 148458 : vect_vfa_segment_size (dr_vec_info *dr_info, tree length_factor)
3956 : {
3957 148458 : length_factor = size_binop (MINUS_EXPR,
3958 : fold_convert (sizetype, length_factor),
3959 : size_one_node);
3960 148458 : return size_binop (MULT_EXPR, fold_convert (sizetype, DR_STEP (dr_info->dr)),
3961 : length_factor);
3962 : }
3963 :
3964 : /* Return a value that, when added to abs (vect_vfa_segment_size (DR_INFO)),
3965 : gives the worst-case number of bytes covered by the segment. */
3966 :
3967 : static unsigned HOST_WIDE_INT
3968 148940 : vect_vfa_access_size (vec_info *vinfo, dr_vec_info *dr_info)
3969 : {
3970 148940 : stmt_vec_info stmt_vinfo = dr_info->stmt;
3971 148940 : tree ref_type = TREE_TYPE (DR_REF (dr_info->dr));
3972 148940 : unsigned HOST_WIDE_INT ref_size = tree_to_uhwi (TYPE_SIZE_UNIT (ref_type));
3973 148940 : unsigned HOST_WIDE_INT access_size = ref_size;
3974 148940 : if (DR_GROUP_FIRST_ELEMENT (stmt_vinfo))
3975 : {
3976 42595 : gcc_assert (DR_GROUP_FIRST_ELEMENT (stmt_vinfo) == stmt_vinfo);
3977 42595 : access_size *= DR_GROUP_SIZE (stmt_vinfo) - DR_GROUP_GAP (stmt_vinfo);
3978 : }
3979 148940 : tree vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
3980 148940 : int misalignment;
3981 297880 : if (((misalignment = dr_misalignment (dr_info, vectype)), true)
3982 148940 : && (vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment)
3983 : == dr_explicit_realign_optimized))
3984 : {
3985 : /* We might access a full vector's worth. */
3986 0 : access_size += tree_to_uhwi (TYPE_SIZE_UNIT (vectype)) - ref_size;
3987 : }
3988 148940 : return access_size;
3989 : }
3990 :
3991 : /* Get the minimum alignment for all the scalar accesses that DR_INFO
3992 : describes. */
3993 :
3994 : static unsigned int
3995 148940 : vect_vfa_align (dr_vec_info *dr_info)
3996 : {
3997 0 : return dr_alignment (dr_info->dr);
3998 : }
3999 :
4000 : /* Function vect_no_alias_p.
4001 :
4002 : Given data references A and B with equal base and offset, see whether
4003 : the alias relation can be decided at compilation time. Return 1 if
4004 : it can and the references alias, 0 if it can and the references do
4005 : not alias, and -1 if we cannot decide at compile time. SEGMENT_LENGTH_A,
4006 : SEGMENT_LENGTH_B, ACCESS_SIZE_A and ACCESS_SIZE_B are the equivalent
4007 : of dr_with_seg_len::{seg_len,access_size} for A and B. */
4008 :
4009 : static int
4010 4532 : vect_compile_time_alias (dr_vec_info *a, dr_vec_info *b,
4011 : tree segment_length_a, tree segment_length_b,
4012 : unsigned HOST_WIDE_INT access_size_a,
4013 : unsigned HOST_WIDE_INT access_size_b)
4014 : {
4015 4532 : poly_offset_int offset_a = wi::to_poly_offset (DR_INIT (a->dr));
4016 4532 : poly_offset_int offset_b = wi::to_poly_offset (DR_INIT (b->dr));
4017 4532 : poly_uint64 const_length_a;
4018 4532 : poly_uint64 const_length_b;
4019 :
4020 : /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
4021 : bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
4022 : [a, a+12) */
4023 4532 : if (tree_int_cst_compare (DR_STEP (a->dr), size_zero_node) < 0)
4024 : {
4025 228 : const_length_a = (-wi::to_poly_wide (segment_length_a)).force_uhwi ();
4026 228 : offset_a -= const_length_a;
4027 : }
4028 : else
4029 4304 : const_length_a = tree_to_poly_uint64 (segment_length_a);
4030 4532 : if (tree_int_cst_compare (DR_STEP (b->dr), size_zero_node) < 0)
4031 : {
4032 386 : const_length_b = (-wi::to_poly_wide (segment_length_b)).force_uhwi ();
4033 386 : offset_b -= const_length_b;
4034 : }
4035 : else
4036 4146 : const_length_b = tree_to_poly_uint64 (segment_length_b);
4037 :
4038 4532 : const_length_a += access_size_a;
4039 4532 : const_length_b += access_size_b;
4040 :
4041 4532 : if (ranges_known_overlap_p (offset_a, const_length_a,
4042 : offset_b, const_length_b))
4043 : return 1;
4044 :
4045 536 : if (!ranges_maybe_overlap_p (offset_a, const_length_a,
4046 : offset_b, const_length_b))
4047 536 : return 0;
4048 :
4049 : return -1;
4050 : }
4051 :
4052 : /* Return true if the minimum nonzero dependence distance for loop LOOP_DEPTH
4053 : in DDR is >= VF. */
4054 :
4055 : static bool
4056 87406 : dependence_distance_ge_vf (data_dependence_relation *ddr,
4057 : unsigned int loop_depth, poly_uint64 vf)
4058 : {
4059 87406 : if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE
4060 92509 : || DDR_NUM_DIST_VECTS (ddr) == 0)
4061 : return false;
4062 :
4063 : /* If the dependence is exact, we should have limited the VF instead. */
4064 5138 : gcc_checking_assert (DDR_COULD_BE_INDEPENDENT_P (ddr));
4065 :
4066 : unsigned int i;
4067 : lambda_vector dist_v;
4068 10307 : FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
4069 : {
4070 10272 : HOST_WIDE_INT dist = dist_v[loop_depth];
4071 10272 : if (dist != 0
4072 5138 : && !(dist > 0 && DDR_REVERSED_P (ddr))
4073 15410 : && maybe_lt ((unsigned HOST_WIDE_INT) abs_hwi (dist), vf))
4074 : return false;
4075 : }
4076 :
4077 35 : if (dump_enabled_p ())
4078 2 : dump_printf_loc (MSG_NOTE, vect_location,
4079 : "dependence distance between %T and %T is >= VF\n",
4080 2 : DR_REF (DDR_A (ddr)), DR_REF (DDR_B (ddr)));
4081 :
4082 : return true;
4083 : }
4084 :
4085 : /* Dump LOWER_BOUND using flags DUMP_KIND. Dumps are known to be enabled. */
4086 :
4087 : static void
4088 429 : dump_lower_bound (dump_flags_t dump_kind, const vec_lower_bound &lower_bound)
4089 : {
4090 429 : dump_printf (dump_kind, "%s (%T) >= ",
4091 429 : lower_bound.unsigned_p ? "unsigned" : "abs",
4092 429 : lower_bound.expr);
4093 429 : dump_dec (dump_kind, lower_bound.min_value);
4094 429 : }
4095 :
4096 : /* Record that the vectorized loop requires the vec_lower_bound described
4097 : by EXPR, UNSIGNED_P and MIN_VALUE. */
4098 :
4099 : static void
4100 6644 : vect_check_lower_bound (loop_vec_info loop_vinfo, tree expr, bool unsigned_p,
4101 : poly_uint64 min_value)
4102 : {
4103 6644 : vec<vec_lower_bound> &lower_bounds
4104 : = LOOP_VINFO_LOWER_BOUNDS (loop_vinfo);
4105 7616 : for (unsigned int i = 0; i < lower_bounds.length (); ++i)
4106 5876 : if (operand_equal_p (lower_bounds[i].expr, expr, 0))
4107 : {
4108 4904 : unsigned_p &= lower_bounds[i].unsigned_p;
4109 4904 : min_value = upper_bound (lower_bounds[i].min_value, min_value);
4110 4904 : if (lower_bounds[i].unsigned_p != unsigned_p
4111 4904 : || maybe_lt (lower_bounds[i].min_value, min_value))
4112 : {
4113 798 : lower_bounds[i].unsigned_p = unsigned_p;
4114 798 : lower_bounds[i].min_value = min_value;
4115 798 : if (dump_enabled_p ())
4116 : {
4117 250 : dump_printf_loc (MSG_NOTE, vect_location,
4118 : "updating run-time check to ");
4119 250 : dump_lower_bound (MSG_NOTE, lower_bounds[i]);
4120 250 : dump_printf (MSG_NOTE, "\n");
4121 : }
4122 : }
4123 4904 : return;
4124 : }
4125 :
4126 1740 : vec_lower_bound lower_bound (expr, unsigned_p, min_value);
4127 1740 : if (dump_enabled_p ())
4128 : {
4129 179 : dump_printf_loc (MSG_NOTE, vect_location, "need a run-time check that ");
4130 179 : dump_lower_bound (MSG_NOTE, lower_bound);
4131 179 : dump_printf (MSG_NOTE, "\n");
4132 : }
4133 1740 : LOOP_VINFO_LOWER_BOUNDS (loop_vinfo).safe_push (lower_bound);
4134 : }
4135 :
4136 : /* Return true if it's unlikely that the step of the vectorized form of DR_INFO
4137 : will span fewer than GAP bytes. */
4138 :
4139 : static bool
4140 5348 : vect_small_gap_p (loop_vec_info loop_vinfo, dr_vec_info *dr_info,
4141 : poly_int64 gap)
4142 : {
4143 5348 : stmt_vec_info stmt_info = dr_info->stmt;
4144 5348 : HOST_WIDE_INT count
4145 5348 : = estimated_poly_value (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
4146 5348 : if (DR_GROUP_FIRST_ELEMENT (stmt_info))
4147 4588 : count *= DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
4148 5348 : return (estimated_poly_value (gap)
4149 5348 : <= count * vect_get_scalar_dr_size (dr_info));
4150 : }
4151 :
4152 : /* Return true if we know that there is no alias between DR_INFO_A and
4153 : DR_INFO_B when abs (DR_STEP (DR_INFO_A->dr)) >= N for some N.
4154 : When returning true, set *LOWER_BOUND_OUT to this N. */
4155 :
4156 : static bool
4157 19466 : vectorizable_with_step_bound_p (dr_vec_info *dr_info_a, dr_vec_info *dr_info_b,
4158 : poly_uint64 *lower_bound_out)
4159 : {
4160 : /* Check that there is a constant gap of known sign between DR_A
4161 : and DR_B. */
4162 19466 : data_reference *dr_a = dr_info_a->dr;
4163 19466 : data_reference *dr_b = dr_info_b->dr;
4164 19466 : poly_int64 init_a, init_b;
4165 19466 : if (!operand_equal_p (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b), 0)
4166 8828 : || !operand_equal_p (DR_OFFSET (dr_a), DR_OFFSET (dr_b), 0)
4167 8142 : || !operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0)
4168 8132 : || !poly_int_tree_p (DR_INIT (dr_a), &init_a)
4169 8132 : || !poly_int_tree_p (DR_INIT (dr_b), &init_b)
4170 19466 : || !ordered_p (init_a, init_b))
4171 11334 : return false;
4172 :
4173 : /* Sort DR_A and DR_B by the address they access. */
4174 8132 : if (maybe_lt (init_b, init_a))
4175 : {
4176 116 : std::swap (init_a, init_b);
4177 116 : std::swap (dr_info_a, dr_info_b);
4178 116 : std::swap (dr_a, dr_b);
4179 : }
4180 :
4181 : /* If the two accesses could be dependent within a scalar iteration,
4182 : make sure that we'd retain their order. */
4183 8132 : if (maybe_gt (init_a + vect_get_scalar_dr_size (dr_info_a), init_b)
4184 8132 : && !vect_preserves_scalar_order_p (dr_info_a, dr_info_b))
4185 : return false;
4186 :
4187 : /* There is no alias if abs (DR_STEP) is greater than or equal to
4188 : the bytes spanned by the combination of the two accesses. */
4189 8132 : *lower_bound_out = init_b + vect_get_scalar_dr_size (dr_info_b) - init_a;
4190 8132 : return true;
4191 : }
4192 :
4193 : /* Function vect_prune_runtime_alias_test_list.
4194 :
4195 : Prune a list of ddrs to be tested at run-time by versioning for alias.
4196 : Merge several alias checks into one if possible.
4197 : Return FALSE if resulting list of ddrs is longer then allowed by
4198 : PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
4199 :
4200 : opt_result
4201 423583 : vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
4202 : {
4203 423583 : typedef pair_hash <tree_operand_hash, tree_operand_hash> tree_pair_hash;
4204 423583 : hash_set <tree_pair_hash> compared_objects;
4205 :
4206 423583 : const vec<ddr_p> &may_alias_ddrs = LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
4207 423583 : vec<dr_with_seg_len_pair_t> &comp_alias_ddrs
4208 : = LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
4209 423583 : const vec<vec_object_pair> &check_unequal_addrs
4210 : = LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo);
4211 423583 : poly_uint64 vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4212 423583 : tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
4213 :
4214 423583 : ddr_p ddr;
4215 423583 : unsigned int i;
4216 423583 : tree length_factor;
4217 :
4218 423583 : DUMP_VECT_SCOPE ("vect_prune_runtime_alias_test_list");
4219 :
4220 : /* Step values are irrelevant for aliasing if the number of vector
4221 : iterations is equal to the number of scalar iterations (which can
4222 : happen for fully-SLP loops). */
4223 423583 : bool vf_one_p = known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U);
4224 :
4225 423583 : if (!vf_one_p)
4226 : {
4227 : /* Convert the checks for nonzero steps into bound tests. */
4228 : tree value;
4229 420315 : FOR_EACH_VEC_ELT (LOOP_VINFO_CHECK_NONZERO (loop_vinfo), i, value)
4230 1653 : vect_check_lower_bound (loop_vinfo, value, true, 1);
4231 : }
4232 :
4233 423583 : if (may_alias_ddrs.is_empty ())
4234 396261 : return opt_result::success ();
4235 :
4236 27322 : comp_alias_ddrs.create (may_alias_ddrs.length ());
4237 :
4238 27322 : unsigned int loop_depth
4239 27322 : = index_in_loop_nest (LOOP_VINFO_LOOP (loop_vinfo)->num,
4240 27322 : LOOP_VINFO_LOOP_NEST (loop_vinfo));
4241 :
4242 : /* First, we collect all data ref pairs for aliasing checks. */
4243 110720 : FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
4244 : {
4245 87406 : poly_uint64 lower_bound;
4246 87406 : tree segment_length_a, segment_length_b;
4247 87406 : unsigned HOST_WIDE_INT access_size_a, access_size_b;
4248 87406 : unsigned HOST_WIDE_INT align_a, align_b;
4249 :
4250 : /* Ignore the alias if the VF we chose ended up being no greater
4251 : than the dependence distance. */
4252 87406 : if (dependence_distance_ge_vf (ddr, loop_depth, vect_factor))
4253 13460 : continue;
4254 :
4255 87371 : if (DDR_OBJECT_A (ddr))
4256 : {
4257 106 : vec_object_pair new_pair (DDR_OBJECT_A (ddr), DDR_OBJECT_B (ddr));
4258 106 : if (!compared_objects.add (new_pair))
4259 : {
4260 22 : if (dump_enabled_p ())
4261 16 : dump_printf_loc (MSG_NOTE, vect_location,
4262 : "checking that %T and %T"
4263 : " have different addresses\n",
4264 : new_pair.first, new_pair.second);
4265 22 : LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo).safe_push (new_pair);
4266 : }
4267 106 : continue;
4268 106 : }
4269 :
4270 87265 : dr_vec_info *dr_info_a = loop_vinfo->lookup_dr (DDR_A (ddr));
4271 87265 : stmt_vec_info stmt_info_a = dr_info_a->stmt;
4272 :
4273 87265 : dr_vec_info *dr_info_b = loop_vinfo->lookup_dr (DDR_B (ddr));
4274 87265 : stmt_vec_info stmt_info_b = dr_info_b->stmt;
4275 :
4276 87265 : bool preserves_scalar_order_p
4277 87265 : = vect_preserves_scalar_order_p (dr_info_a, dr_info_b);
4278 87265 : bool ignore_step_p
4279 : = (vf_one_p
4280 87265 : && (preserves_scalar_order_p
4281 4037 : || operand_equal_p (DR_STEP (dr_info_a->dr),
4282 4037 : DR_STEP (dr_info_b->dr))));
4283 :
4284 : /* Skip the pair if inter-iteration dependencies are irrelevant
4285 : and intra-iteration dependencies are guaranteed to be honored. */
4286 15825 : if (ignore_step_p
4287 8033 : && (preserves_scalar_order_p
4288 3318 : || vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4289 : &lower_bound)))
4290 : {
4291 7792 : if (dump_enabled_p ())
4292 2528 : dump_printf_loc (MSG_NOTE, vect_location,
4293 : "no need for alias check between "
4294 : "%T and %T when VF is 1\n",
4295 2528 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4296 7792 : continue;
4297 : }
4298 :
4299 : /* See whether we can handle the alias using a bounds check on
4300 : the step, and whether that's likely to be the best approach.
4301 : (It might not be, for example, if the minimum step is much larger
4302 : than the number of bytes handled by one vector iteration.) */
4303 79473 : if (!ignore_step_p
4304 79232 : && TREE_CODE (DR_STEP (dr_info_a->dr)) != INTEGER_CST
4305 16148 : && vectorizable_with_step_bound_p (dr_info_a, dr_info_b,
4306 : &lower_bound)
4307 84528 : && (vect_small_gap_p (loop_vinfo, dr_info_a, lower_bound)
4308 293 : || vect_small_gap_p (loop_vinfo, dr_info_b, lower_bound)))
4309 : {
4310 4991 : bool unsigned_p = dr_known_forward_stride_p (dr_info_a->dr);
4311 4991 : if (dump_enabled_p ())
4312 : {
4313 3384 : dump_printf_loc (MSG_NOTE, vect_location, "no alias between "
4314 : "%T and %T when the step %T is outside ",
4315 : DR_REF (dr_info_a->dr),
4316 1692 : DR_REF (dr_info_b->dr),
4317 1692 : DR_STEP (dr_info_a->dr));
4318 1692 : if (unsigned_p)
4319 504 : dump_printf (MSG_NOTE, "[0");
4320 : else
4321 : {
4322 1188 : dump_printf (MSG_NOTE, "(");
4323 1188 : dump_dec (MSG_NOTE, poly_int64 (-lower_bound));
4324 : }
4325 1692 : dump_printf (MSG_NOTE, ", ");
4326 1692 : dump_dec (MSG_NOTE, lower_bound);
4327 1692 : dump_printf (MSG_NOTE, ")\n");
4328 : }
4329 4991 : vect_check_lower_bound (loop_vinfo, DR_STEP (dr_info_a->dr),
4330 : unsigned_p, lower_bound);
4331 4991 : continue;
4332 4991 : }
4333 :
4334 74482 : stmt_vec_info dr_group_first_a = DR_GROUP_FIRST_ELEMENT (stmt_info_a);
4335 74482 : if (dr_group_first_a)
4336 : {
4337 20982 : stmt_info_a = dr_group_first_a;
4338 20982 : dr_info_a = STMT_VINFO_DR_INFO (stmt_info_a);
4339 : }
4340 :
4341 74482 : stmt_vec_info dr_group_first_b = DR_GROUP_FIRST_ELEMENT (stmt_info_b);
4342 74482 : if (dr_group_first_b)
4343 : {
4344 21613 : stmt_info_b = dr_group_first_b;
4345 21613 : dr_info_b = STMT_VINFO_DR_INFO (stmt_info_b);
4346 : }
4347 :
4348 74482 : if (ignore_step_p)
4349 : {
4350 241 : segment_length_a = size_zero_node;
4351 241 : segment_length_b = size_zero_node;
4352 : }
4353 : else
4354 : {
4355 74241 : if (!operand_equal_p (DR_STEP (dr_info_a->dr),
4356 74241 : DR_STEP (dr_info_b->dr), 0))
4357 : {
4358 16536 : length_factor = scalar_loop_iters;
4359 16536 : if (TREE_CODE (length_factor) == SCEV_NOT_KNOWN)
4360 12 : return opt_result::failure_at (vect_location,
4361 : "Unsupported alias check on"
4362 : " uncounted loop\n");
4363 : }
4364 : else
4365 57705 : length_factor = size_int (vect_factor);
4366 74229 : segment_length_a = vect_vfa_segment_size (dr_info_a, length_factor);
4367 74229 : segment_length_b = vect_vfa_segment_size (dr_info_b, length_factor);
4368 : }
4369 74470 : access_size_a = vect_vfa_access_size (loop_vinfo, dr_info_a);
4370 74470 : access_size_b = vect_vfa_access_size (loop_vinfo, dr_info_b);
4371 74470 : align_a = vect_vfa_align (dr_info_a);
4372 74470 : align_b = vect_vfa_align (dr_info_b);
4373 :
4374 : /* See whether the alias is known at compilation time. */
4375 74470 : if (operand_equal_p (DR_BASE_ADDRESS (dr_info_a->dr),
4376 74470 : DR_BASE_ADDRESS (dr_info_b->dr), 0)
4377 6372 : && operand_equal_p (DR_OFFSET (dr_info_a->dr),
4378 6372 : DR_OFFSET (dr_info_b->dr), 0)
4379 4680 : && TREE_CODE (DR_STEP (dr_info_a->dr)) == INTEGER_CST
4380 4606 : && TREE_CODE (DR_STEP (dr_info_b->dr)) == INTEGER_CST
4381 4596 : && poly_int_tree_p (segment_length_a)
4382 79024 : && poly_int_tree_p (segment_length_b))
4383 : {
4384 4532 : int res = vect_compile_time_alias (dr_info_a, dr_info_b,
4385 : segment_length_a,
4386 : segment_length_b,
4387 : access_size_a,
4388 : access_size_b);
4389 4532 : if (res >= 0 && dump_enabled_p ())
4390 : {
4391 208 : dump_printf_loc (MSG_NOTE, vect_location,
4392 : "can tell at compile time that %T and %T",
4393 104 : DR_REF (dr_info_a->dr), DR_REF (dr_info_b->dr));
4394 104 : if (res == 0)
4395 57 : dump_printf (MSG_NOTE, " do not alias\n");
4396 : else
4397 47 : dump_printf (MSG_NOTE, " alias\n");
4398 : }
4399 :
4400 4532 : if (res == 0)
4401 536 : continue;
4402 :
4403 3996 : if (res == 1)
4404 3996 : return opt_result::failure_at (stmt_info_b->stmt,
4405 : "not vectorized:"
4406 : " compilation time alias: %G%G",
4407 : stmt_info_a->stmt,
4408 : stmt_info_b->stmt);
4409 : }
4410 :
4411 : /* dr_with_seg_len requires the alignment to apply to the segment length
4412 : and access size, not just the start address. The access size can be
4413 : smaller than the pointer alignment for grouped accesses and bitfield
4414 : references; see PR115192 and PR116125 respectively. */
4415 69938 : align_a = std::min (align_a, least_bit_hwi (access_size_a));
4416 69938 : align_b = std::min (align_b, least_bit_hwi (access_size_b));
4417 :
4418 69938 : dr_with_seg_len dr_a (dr_info_a->dr, segment_length_a,
4419 69938 : access_size_a, align_a);
4420 69938 : dr_with_seg_len dr_b (dr_info_b->dr, segment_length_b,
4421 69938 : access_size_b, align_b);
4422 : /* Canonicalize the order to be the one that's needed for accurate
4423 : RAW, WAR and WAW flags, in cases where the data references are
4424 : well-ordered. The order doesn't really matter otherwise,
4425 : but we might as well be consistent. */
4426 69938 : if (get_later_stmt (stmt_info_a, stmt_info_b) == stmt_info_a)
4427 5194 : std::swap (dr_a, dr_b);
4428 :
4429 69938 : dr_with_seg_len_pair_t dr_with_seg_len_pair
4430 : (dr_a, dr_b, (preserves_scalar_order_p
4431 : ? dr_with_seg_len_pair_t::WELL_ORDERED
4432 76785 : : dr_with_seg_len_pair_t::REORDERED));
4433 :
4434 69938 : comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
4435 : }
4436 :
4437 23314 : prune_runtime_alias_test_list (&comp_alias_ddrs, vect_factor);
4438 :
4439 46628 : unsigned int count = (comp_alias_ddrs.length ()
4440 23314 : + check_unequal_addrs.length ());
4441 :
4442 23314 : if (count
4443 23314 : && (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo))
4444 : == VECT_COST_MODEL_VERY_CHEAP))
4445 13466 : return opt_result::failure_at
4446 13466 : (vect_location, "would need a runtime alias check\n");
4447 :
4448 9848 : if (dump_enabled_p ())
4449 1954 : dump_printf_loc (MSG_NOTE, vect_location,
4450 : "improved number of alias checks from %d to %d\n",
4451 : may_alias_ddrs.length (), count);
4452 9848 : unsigned limit = param_vect_max_version_for_alias_checks;
4453 9848 : if (loop_cost_model (LOOP_VINFO_LOOP (loop_vinfo)) == VECT_COST_MODEL_CHEAP)
4454 936 : limit = param_vect_max_version_for_alias_checks * 6 / 10;
4455 9848 : if (count > limit)
4456 162 : return opt_result::failure_at
4457 162 : (vect_location,
4458 : "number of versioning for alias run-time tests exceeds %d "
4459 : "(--param vect-max-version-for-alias-checks)\n", limit);
4460 :
4461 9686 : return opt_result::success ();
4462 423583 : }
4463 :
4464 : /* Structure to hold information about a supported gather/scatter
4465 : configuration. */
4466 : struct gather_scatter_config
4467 : {
4468 : internal_fn ifn;
4469 : tree offset_vectype;
4470 : int scale;
4471 : vec<int> elsvals;
4472 : };
4473 :
4474 : /* Determine which gather/scatter IFN is supported for the given parameters.
4475 : IFN_MASK_GATHER_LOAD, IFN_GATHER_LOAD, and IFN_MASK_LEN_GATHER_LOAD
4476 : are mutually exclusive, so we only need to find one. Return the
4477 : supported IFN or IFN_LAST if none are supported. */
4478 :
4479 : static internal_fn
4480 1176004 : vect_gather_scatter_which_ifn (bool read_p, bool masked_p,
4481 : tree vectype, tree memory_type,
4482 : tree offset_vectype, int scale,
4483 : vec<int> *elsvals)
4484 : {
4485 : /* Work out which functions to try. */
4486 1176004 : internal_fn ifn, alt_ifn, alt_ifn2;
4487 1176004 : if (read_p)
4488 : {
4489 878102 : ifn = masked_p ? IFN_MASK_GATHER_LOAD : IFN_GATHER_LOAD;
4490 : alt_ifn = IFN_MASK_GATHER_LOAD;
4491 : alt_ifn2 = IFN_MASK_LEN_GATHER_LOAD;
4492 : }
4493 : else
4494 : {
4495 297902 : ifn = masked_p ? IFN_MASK_SCATTER_STORE : IFN_SCATTER_STORE;
4496 : alt_ifn = IFN_MASK_SCATTER_STORE;
4497 : alt_ifn2 = IFN_MASK_LEN_SCATTER_STORE;
4498 : }
4499 :
4500 1176004 : if (!offset_vectype)
4501 : return IFN_LAST;
4502 :
4503 1176004 : if (internal_gather_scatter_fn_supported_p (ifn, vectype, memory_type,
4504 : offset_vectype, scale, elsvals))
4505 : return ifn;
4506 1176004 : if (internal_gather_scatter_fn_supported_p (alt_ifn, vectype, memory_type,
4507 : offset_vectype, scale, elsvals))
4508 : return alt_ifn;
4509 1176004 : if (internal_gather_scatter_fn_supported_p (alt_ifn2, vectype, memory_type,
4510 : offset_vectype, scale, elsvals))
4511 : return alt_ifn2;
4512 :
4513 : return IFN_LAST;
4514 : }
4515 :
4516 : /* Collect all supported offset vector types for a gather load or scatter
4517 : store. READ_P is true for loads and false for stores. MASKED_P is true
4518 : if the load or store is conditional. VECTYPE is the data vector type.
4519 : MEMORY_TYPE is the type of the memory elements being loaded or stored,
4520 : and OFFSET_TYPE is the type of the offset.
4521 : SCALE is the amount by which the offset should be multiplied.
4522 :
4523 : Return a vector of all configurations the target supports (which can
4524 : be none). */
4525 :
4526 : static auto_vec<gather_scatter_config>
4527 83460 : vect_gather_scatter_get_configs (vec_info *vinfo, bool read_p, bool masked_p,
4528 : tree vectype, tree memory_type,
4529 : tree offset_type, int scale)
4530 : {
4531 83460 : auto_vec<gather_scatter_config> configs;
4532 :
4533 83460 : auto_vec<tree, 8> offset_types_to_try;
4534 :
4535 : /* Try all sizes from the offset type's precision up to POINTER_SIZE. */
4536 83460 : for (unsigned int bits = TYPE_PRECISION (offset_type);
4537 394985 : bits <= POINTER_SIZE;
4538 298301 : bits *= 2)
4539 : {
4540 : /* Signed variant. */
4541 298301 : offset_types_to_try.safe_push
4542 298301 : (build_nonstandard_integer_type (bits, 0));
4543 : /* Unsigned variant. */
4544 298301 : offset_types_to_try.safe_push
4545 298301 : (build_nonstandard_integer_type (bits, 1));
4546 : }
4547 :
4548 : /* Once we find which IFN works for one offset type, we know that it
4549 : will work for other offset types as well. Then we can perform
4550 : the checks for the remaining offset types with only that IFN.
4551 : However, we might need to try different offset types to find which
4552 : IFN is supported, since the check is offset-type-specific. */
4553 : internal_fn ifn = IFN_LAST;
4554 :
4555 : /* Try each offset type. */
4556 680062 : for (unsigned int i = 0; i < offset_types_to_try.length (); i++)
4557 : {
4558 596602 : tree offset_type = offset_types_to_try[i];
4559 596602 : tree offset_vectype = get_vectype_for_scalar_type (vinfo, offset_type);
4560 596602 : if (!offset_vectype)
4561 9910 : continue;
4562 :
4563 : /* Try multiple scale values. Start with exact match, then try
4564 : smaller common scales that a target might support . */
4565 586692 : int scales_to_try[] = {scale, 1, 2, 4, 8};
4566 :
4567 3520152 : for (unsigned int j = 0;
4568 3520152 : j < sizeof (scales_to_try) / sizeof (*scales_to_try);
4569 : j++)
4570 : {
4571 2933460 : int try_scale = scales_to_try[j];
4572 :
4573 : /* Skip scales >= requested scale (except for exact match). */
4574 2933460 : if (j > 0 && try_scale >= scale)
4575 1757456 : continue;
4576 :
4577 : /* Skip if requested scale is not a multiple of this scale. */
4578 1176148 : if (j > 0 && scale % try_scale != 0)
4579 144 : continue;
4580 :
4581 1176004 : vec<int> elsvals = vNULL;
4582 :
4583 : /* If we haven't determined which IFN is supported yet, try all three
4584 : to find which one the target supports. */
4585 1176004 : if (ifn == IFN_LAST)
4586 : {
4587 1176004 : ifn = vect_gather_scatter_which_ifn (read_p, masked_p,
4588 : vectype, memory_type,
4589 : offset_vectype, try_scale,
4590 : &elsvals);
4591 1176004 : if (ifn != IFN_LAST)
4592 : {
4593 : /* Found which IFN is supported. Save this configuration. */
4594 0 : gather_scatter_config config;
4595 0 : config.ifn = ifn;
4596 0 : config.offset_vectype = offset_vectype;
4597 0 : config.scale = try_scale;
4598 0 : config.elsvals = elsvals;
4599 0 : configs.safe_push (config);
4600 : }
4601 : }
4602 : else
4603 : {
4604 : /* We already know which IFN is supported, just check if this
4605 : offset type and scale work with it. */
4606 0 : if (internal_gather_scatter_fn_supported_p (ifn, vectype,
4607 : memory_type,
4608 : offset_vectype,
4609 : try_scale,
4610 : &elsvals))
4611 : {
4612 0 : gather_scatter_config config;
4613 0 : config.ifn = ifn;
4614 0 : config.offset_vectype = offset_vectype;
4615 0 : config.scale = try_scale;
4616 0 : config.elsvals = elsvals;
4617 0 : configs.safe_push (config);
4618 : }
4619 : }
4620 : }
4621 : }
4622 :
4623 83460 : return configs;
4624 83460 : }
4625 :
4626 : /* Check whether we can use an internal function for a gather load
4627 : or scatter store. READ_P is true for loads and false for stores.
4628 : MASKED_P is true if the load or store is conditional. MEMORY_TYPE is
4629 : the type of the memory elements being loaded or stored. OFFSET_TYPE
4630 : is the type of the offset that is being applied to the invariant
4631 : base address. If OFFSET_TYPE is scalar the function chooses an
4632 : appropriate vector type for it. SCALE is the amount by which the
4633 : offset should be multiplied *after* it has been converted to address width.
4634 : If the target does not support the requested SCALE, SUPPORTED_SCALE
4635 : will contain the scale that is actually supported
4636 : (which may be smaller, requiring additional multiplication).
4637 : Otherwise SUPPORTED_SCALE is 0.
4638 :
4639 : Return true if the function is supported, storing the function id in
4640 : *IFN_OUT and the vector type for the offset in *OFFSET_VECTYPE_OUT.
4641 : If we support an offset vector type with different signedness than
4642 : OFFSET_TYPE store it in SUPPORTED_OFFSET_VECTYPE.
4643 :
4644 : If we can use gather/scatter and ELSVALS is nonzero, store the possible
4645 : else values in ELSVALS. */
4646 :
4647 : bool
4648 83460 : vect_gather_scatter_fn_p (vec_info *vinfo, bool read_p, bool masked_p,
4649 : tree vectype, tree memory_type, tree offset_type,
4650 : int scale, int *supported_scale,
4651 : internal_fn *ifn_out,
4652 : tree *offset_vectype_out,
4653 : tree *supported_offset_vectype,
4654 : vec<int> *elsvals)
4655 : {
4656 83460 : *supported_offset_vectype = NULL_TREE;
4657 83460 : *supported_scale = 0;
4658 83460 : unsigned int memory_bits = tree_to_uhwi (TYPE_SIZE (memory_type));
4659 83460 : unsigned int element_bits = vector_element_bits (vectype);
4660 83460 : if (element_bits != memory_bits)
4661 : /* For now the vector elements must be the same width as the
4662 : memory elements. */
4663 : return false;
4664 :
4665 : /* Get the original offset vector type for comparison. */
4666 83460 : tree offset_vectype = VECTOR_TYPE_P (offset_type)
4667 83460 : ? offset_type : get_vectype_for_scalar_type (vinfo, offset_type);
4668 :
4669 : /* If there is no offset vectype, bail. */
4670 70726 : if (!offset_vectype)
4671 : return false;
4672 :
4673 83460 : offset_type = TREE_TYPE (offset_vectype);
4674 :
4675 : /* Get all supported configurations for this data vector type. */
4676 83460 : auto_vec<gather_scatter_config> configs
4677 : = vect_gather_scatter_get_configs (vinfo, read_p, masked_p, vectype,
4678 83460 : memory_type, offset_type, scale);
4679 :
4680 83460 : if (configs.is_empty ())
4681 : return false;
4682 :
4683 : /* Selection priority:
4684 : 1 - Exact scale match + offset type match
4685 : 2 - Exact scale match + sign-swapped offset
4686 : 3 - Smaller scale + offset type match
4687 : 4 - Smaller scale + sign-swapped offset
4688 : Within each category, prefer smaller offset types. */
4689 :
4690 : /* First pass: exact scale match with no conversion. */
4691 0 : for (unsigned int i = 0; i < configs.length (); i++)
4692 : {
4693 0 : if (configs[i].scale == scale
4694 0 : && TYPE_SIGN (configs[i].offset_vectype)
4695 0 : == TYPE_SIGN (offset_vectype))
4696 : {
4697 0 : *ifn_out = configs[i].ifn;
4698 0 : *offset_vectype_out = configs[i].offset_vectype;
4699 0 : if (elsvals)
4700 0 : *elsvals = configs[i].elsvals;
4701 0 : return true;
4702 : }
4703 : }
4704 :
4705 : /* No direct match. This means we try to find either
4706 : - a sign-swapped offset vectype or
4707 : - a different scale and 2x larger offset type
4708 : - a different scale and larger sign-swapped offset vectype. */
4709 0 : unsigned int offset_precision = TYPE_PRECISION (TREE_TYPE (offset_vectype));
4710 0 : unsigned int needed_precision
4711 0 : = TYPE_UNSIGNED (offset_vectype) ? offset_precision * 2 : POINTER_SIZE;
4712 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4713 :
4714 : /* Second pass: No direct match. This means we try to find a sign-swapped
4715 : offset vectype. */
4716 0 : enum tree_code tmp;
4717 0 : for (unsigned int i = 0; i < configs.length (); i++)
4718 : {
4719 0 : unsigned int precision
4720 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4721 0 : if (configs[i].scale == scale
4722 0 : && precision >= needed_precision
4723 0 : && (supportable_convert_operation (CONVERT_EXPR,
4724 0 : configs[i].offset_vectype,
4725 : offset_vectype, &tmp)
4726 0 : || (needed_precision == offset_precision
4727 0 : && tree_nop_conversion_p (configs[i].offset_vectype,
4728 : offset_vectype))))
4729 : {
4730 0 : *ifn_out = configs[i].ifn;
4731 0 : *offset_vectype_out = offset_vectype;
4732 0 : *supported_offset_vectype = configs[i].offset_vectype;
4733 0 : if (elsvals)
4734 0 : *elsvals = configs[i].elsvals;
4735 0 : return true;
4736 : }
4737 : }
4738 :
4739 : /* Third pass: Try a smaller scale with the same signedness. */
4740 0 : needed_precision = offset_precision * 2;
4741 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4742 :
4743 0 : for (unsigned int i = 0; i < configs.length (); i++)
4744 : {
4745 0 : unsigned int precision
4746 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4747 0 : if (configs[i].scale < scale
4748 0 : && TYPE_SIGN (configs[i].offset_vectype)
4749 0 : == TYPE_SIGN (offset_vectype)
4750 0 : && precision >= needed_precision)
4751 : {
4752 0 : *ifn_out = configs[i].ifn;
4753 0 : *offset_vectype_out = configs[i].offset_vectype;
4754 0 : *supported_scale = configs[i].scale;
4755 : /* Only set SUPPORTED_OFFSET_VECTYPE if this is a real
4756 : conversion. */
4757 0 : if (!useless_type_conversion_p (offset_vectype,
4758 0 : configs[i].offset_vectype))
4759 0 : *supported_offset_vectype = configs[i].offset_vectype;
4760 0 : if (elsvals)
4761 0 : *elsvals = configs[i].elsvals;
4762 0 : return true;
4763 : }
4764 : }
4765 :
4766 : /* Fourth pass: Try a smaller scale and sign-swapped offset vectype. */
4767 0 : needed_precision
4768 0 : = TYPE_UNSIGNED (offset_vectype) ? offset_precision * 2 : POINTER_SIZE;
4769 0 : needed_precision = std::min (needed_precision, (unsigned) POINTER_SIZE);
4770 :
4771 0 : for (unsigned int i = 0; i < configs.length (); i++)
4772 : {
4773 0 : unsigned int precision
4774 0 : = TYPE_PRECISION (TREE_TYPE (configs[i].offset_vectype));
4775 0 : if (configs[i].scale < scale
4776 0 : && precision >= needed_precision
4777 0 : && (supportable_convert_operation (CONVERT_EXPR,
4778 0 : configs[i].offset_vectype,
4779 : offset_vectype, &tmp)
4780 0 : || (needed_precision == offset_precision
4781 0 : && tree_nop_conversion_p (configs[i].offset_vectype,
4782 : offset_vectype))))
4783 : {
4784 0 : *ifn_out = configs[i].ifn;
4785 0 : *offset_vectype_out = offset_vectype;
4786 0 : *supported_offset_vectype = configs[i].offset_vectype;
4787 0 : *supported_scale = configs[i].scale;
4788 0 : if (elsvals)
4789 0 : *elsvals = configs[i].elsvals;
4790 0 : return true;
4791 : }
4792 : }
4793 :
4794 : return false;
4795 83460 : }
4796 :
4797 : /* STMT_INFO is a call to an internal gather load or scatter store function.
4798 : Describe the operation in INFO. */
4799 :
4800 : void
4801 0 : vect_describe_gather_scatter_call (stmt_vec_info stmt_info,
4802 : gather_scatter_info *info)
4803 : {
4804 0 : gcall *call = as_a <gcall *> (stmt_info->stmt);
4805 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4806 0 : data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4807 :
4808 0 : info->ifn = gimple_call_internal_fn (call);
4809 0 : info->decl = NULL_TREE;
4810 0 : info->base = gimple_call_arg (call, 0);
4811 0 : info->alias_ptr = gimple_call_arg
4812 0 : (call, internal_fn_alias_ptr_index (info->ifn));
4813 0 : info->offset = gimple_call_arg
4814 0 : (call, internal_fn_offset_index (info->ifn));
4815 0 : info->offset_vectype = NULL_TREE;
4816 0 : info->scale = TREE_INT_CST_LOW (gimple_call_arg
4817 : (call, internal_fn_scale_index (info->ifn)));
4818 0 : info->element_type = TREE_TYPE (vectype);
4819 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
4820 0 : }
4821 :
4822 : /* Return true if a non-affine read or write in STMT_INFO is suitable for a
4823 : gather load or scatter store with VECTYPE. Describe the operation in *INFO
4824 : if so. If it is suitable and ELSVALS is nonzero store the supported else
4825 : values in the vector it points to. */
4826 :
4827 : bool
4828 354464 : vect_check_gather_scatter (stmt_vec_info stmt_info, tree vectype,
4829 : loop_vec_info loop_vinfo,
4830 : gather_scatter_info *info, vec<int> *elsvals)
4831 : {
4832 354464 : HOST_WIDE_INT scale = 1;
4833 354464 : poly_int64 pbitpos, pbitsize;
4834 354464 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4835 354464 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4836 354464 : tree offtype = NULL_TREE;
4837 354464 : tree decl = NULL_TREE, base, off;
4838 354464 : tree memory_type = TREE_TYPE (DR_REF (dr));
4839 354464 : machine_mode pmode;
4840 354464 : int punsignedp, reversep, pvolatilep = 0;
4841 354464 : internal_fn ifn;
4842 354464 : tree offset_vectype;
4843 354464 : bool masked_p = false;
4844 :
4845 : /* See whether this is already a call to a gather/scatter internal function.
4846 : If not, see whether it's a masked load or store. */
4847 354464 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
4848 6272 : if (call && gimple_call_internal_p (call))
4849 : {
4850 6272 : ifn = gimple_call_internal_fn (call);
4851 6272 : if (internal_gather_scatter_fn_p (ifn))
4852 : {
4853 0 : vect_describe_gather_scatter_call (stmt_info, info);
4854 :
4855 : /* In pattern recog we simply used a ZERO else value that
4856 : we need to correct here. To that end just re-use the
4857 : (already successful) check if we support a gather IFN
4858 : and have it populate the else values. */
4859 0 : if (DR_IS_READ (dr) && internal_fn_mask_index (ifn) >= 0 && elsvals)
4860 0 : supports_vec_gather_load_p (TYPE_MODE (vectype), elsvals);
4861 0 : return true;
4862 : }
4863 6272 : masked_p = (ifn == IFN_MASK_LOAD || ifn == IFN_MASK_STORE);
4864 : }
4865 :
4866 : /* True if we should aim to use internal functions rather than
4867 : built-in functions. */
4868 354464 : bool use_ifn_p = (DR_IS_READ (dr)
4869 354464 : ? supports_vec_gather_load_p (TYPE_MODE (vectype),
4870 : elsvals)
4871 354464 : : supports_vec_scatter_store_p (TYPE_MODE (vectype)));
4872 :
4873 354464 : base = DR_REF (dr);
4874 : /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
4875 : see if we can use the def stmt of the address. */
4876 354464 : if (masked_p
4877 6272 : && TREE_CODE (base) == MEM_REF
4878 6272 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
4879 6272 : && integer_zerop (TREE_OPERAND (base, 1))
4880 360736 : && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
4881 : {
4882 6272 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
4883 6272 : if (is_gimple_assign (def_stmt)
4884 6272 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
4885 613 : base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
4886 : }
4887 :
4888 : /* The gather and scatter builtins need address of the form
4889 : loop_invariant + vector * {1, 2, 4, 8}
4890 : or
4891 : loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
4892 : Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
4893 : of loop invariants/SSA_NAMEs defined in the loop, with casts,
4894 : multiplications and additions in it. To get a vector, we need
4895 : a single SSA_NAME that will be defined in the loop and will
4896 : contain everything that is not loop invariant and that can be
4897 : vectorized. The following code attempts to find such a preexistng
4898 : SSA_NAME OFF and put the loop invariants into a tree BASE
4899 : that can be gimplified before the loop. */
4900 354464 : base = get_inner_reference (base, &pbitsize, &pbitpos, &off, &pmode,
4901 : &punsignedp, &reversep, &pvolatilep);
4902 354464 : if (reversep)
4903 : return false;
4904 :
4905 : /* PR 107346. Packed structs can have fields at offsets that are not
4906 : multiples of BITS_PER_UNIT. Do not use gather/scatters in such cases. */
4907 354464 : if (!multiple_p (pbitpos, BITS_PER_UNIT))
4908 : return false;
4909 :
4910 : /* We need to be able to form an address to the base which for example
4911 : isn't possible for hard registers. */
4912 354464 : if (may_be_nonaddressable_p (base))
4913 : return false;
4914 :
4915 354456 : poly_int64 pbytepos = exact_div (pbitpos, BITS_PER_UNIT);
4916 :
4917 354456 : if (TREE_CODE (base) == MEM_REF)
4918 : {
4919 287467 : if (!integer_zerop (TREE_OPERAND (base, 1)))
4920 : {
4921 34061 : if (off == NULL_TREE)
4922 33744 : off = wide_int_to_tree (sizetype, mem_ref_offset (base));
4923 : else
4924 317 : off = size_binop (PLUS_EXPR, off,
4925 : fold_convert (sizetype, TREE_OPERAND (base, 1)));
4926 : }
4927 287467 : base = TREE_OPERAND (base, 0);
4928 : }
4929 : else
4930 66989 : base = build_fold_addr_expr (base);
4931 :
4932 354456 : if (off == NULL_TREE)
4933 228639 : off = size_zero_node;
4934 :
4935 : /* BASE must be loop invariant. If it is not invariant, but OFF is, then we
4936 : * can fix that by swapping BASE and OFF. */
4937 354456 : if (!expr_invariant_in_loop_p (loop, base))
4938 : {
4939 262910 : if (!expr_invariant_in_loop_p (loop, off))
4940 : return false;
4941 :
4942 262641 : std::swap (base, off);
4943 : }
4944 :
4945 354187 : base = fold_convert (sizetype, base);
4946 354187 : base = size_binop (PLUS_EXPR, base, size_int (pbytepos));
4947 354187 : int tmp_scale;
4948 354187 : tree tmp_offset_vectype;
4949 :
4950 : /* OFF at this point may be either a SSA_NAME or some tree expression
4951 : from get_inner_reference. Try to peel off loop invariants from it
4952 : into BASE as long as possible. */
4953 354187 : STRIP_NOPS (off);
4954 927414 : while (offtype == NULL_TREE)
4955 : {
4956 804869 : enum tree_code code;
4957 804869 : tree op0, op1, add = NULL_TREE;
4958 :
4959 804869 : if (TREE_CODE (off) == SSA_NAME)
4960 : {
4961 617821 : gimple *def_stmt = SSA_NAME_DEF_STMT (off);
4962 :
4963 617821 : if (expr_invariant_in_loop_p (loop, off))
4964 0 : return false;
4965 :
4966 617821 : if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
4967 : break;
4968 :
4969 486130 : op0 = gimple_assign_rhs1 (def_stmt);
4970 486130 : code = gimple_assign_rhs_code (def_stmt);
4971 486130 : op1 = gimple_assign_rhs2 (def_stmt);
4972 : }
4973 : else
4974 : {
4975 187048 : if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
4976 : return false;
4977 187048 : code = TREE_CODE (off);
4978 187048 : extract_ops_from_tree (off, &code, &op0, &op1);
4979 : }
4980 673178 : switch (code)
4981 : {
4982 206391 : case POINTER_PLUS_EXPR:
4983 206391 : case PLUS_EXPR:
4984 206391 : if (expr_invariant_in_loop_p (loop, op0))
4985 : {
4986 136865 : add = op0;
4987 136865 : off = op1;
4988 190255 : do_add:
4989 190255 : add = fold_convert (sizetype, add);
4990 190255 : if (scale != 1)
4991 46606 : add = size_binop (MULT_EXPR, add, size_int (scale));
4992 190255 : base = size_binop (PLUS_EXPR, base, add);
4993 573227 : continue;
4994 : }
4995 69526 : if (expr_invariant_in_loop_p (loop, op1))
4996 : {
4997 53096 : add = op1;
4998 53096 : off = op0;
4999 53096 : goto do_add;
5000 : }
5001 : break;
5002 487 : case MINUS_EXPR:
5003 487 : if (expr_invariant_in_loop_p (loop, op1))
5004 : {
5005 294 : add = fold_convert (sizetype, op1);
5006 294 : add = size_binop (MINUS_EXPR, size_zero_node, add);
5007 294 : off = op0;
5008 294 : goto do_add;
5009 : }
5010 : break;
5011 204400 : case MULT_EXPR:
5012 204400 : if (scale == 1 && tree_fits_shwi_p (op1))
5013 : {
5014 171503 : int new_scale = tree_to_shwi (op1);
5015 : /* Only treat this as a scaling operation if the target
5016 : supports it for at least some offset type. */
5017 171503 : if (use_ifn_p
5018 0 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5019 : masked_p, vectype, memory_type,
5020 : signed_char_type_node,
5021 : new_scale, &tmp_scale,
5022 : &ifn,
5023 : &offset_vectype,
5024 : &tmp_offset_vectype,
5025 : elsvals)
5026 171503 : && !vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5027 : masked_p, vectype, memory_type,
5028 : unsigned_char_type_node,
5029 : new_scale, &tmp_scale,
5030 : &ifn,
5031 : &offset_vectype,
5032 : &tmp_offset_vectype,
5033 : elsvals))
5034 : break;
5035 171503 : scale = new_scale;
5036 171503 : off = op0;
5037 171503 : continue;
5038 171503 : }
5039 : break;
5040 0 : case SSA_NAME:
5041 0 : off = op0;
5042 0 : continue;
5043 217863 : CASE_CONVERT:
5044 435710 : if (!POINTER_TYPE_P (TREE_TYPE (op0))
5045 435710 : && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
5046 : break;
5047 :
5048 : /* Don't include the conversion if the target is happy with
5049 : the current offset type. */
5050 217863 : if (use_ifn_p
5051 0 : && TREE_CODE (off) == SSA_NAME
5052 0 : && !POINTER_TYPE_P (TREE_TYPE (off))
5053 217863 : && vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr),
5054 : masked_p, vectype, memory_type,
5055 0 : TREE_TYPE (off),
5056 : scale, &tmp_scale,
5057 : &ifn,
5058 : &offset_vectype,
5059 : &tmp_offset_vectype,
5060 : elsvals))
5061 : break;
5062 :
5063 217863 : if (TYPE_PRECISION (TREE_TYPE (op0))
5064 217863 : == TYPE_PRECISION (TREE_TYPE (off)))
5065 : {
5066 88924 : off = op0;
5067 88924 : continue;
5068 : }
5069 :
5070 : /* Include the conversion if it is widening and we're using
5071 : the IFN path or the target can handle the converted from
5072 : offset or the current size is not already the same as the
5073 : data vector element size. */
5074 128939 : if ((TYPE_PRECISION (TREE_TYPE (op0))
5075 128939 : < TYPE_PRECISION (TREE_TYPE (off)))
5076 128939 : && (use_ifn_p
5077 128148 : || (DR_IS_READ (dr)
5078 83012 : ? (targetm.vectorize.builtin_gather
5079 83012 : && targetm.vectorize.builtin_gather (vectype,
5080 83012 : TREE_TYPE (op0),
5081 : scale))
5082 45136 : : (targetm.vectorize.builtin_scatter
5083 45136 : && targetm.vectorize.builtin_scatter (vectype,
5084 45136 : TREE_TYPE (op0),
5085 : scale)))
5086 127052 : || !operand_equal_p (TYPE_SIZE (TREE_TYPE (off)),
5087 127052 : TYPE_SIZE (TREE_TYPE (vectype)), 0)))
5088 : {
5089 122545 : off = op0;
5090 122545 : offtype = TREE_TYPE (off);
5091 122545 : STRIP_NOPS (off);
5092 122545 : continue;
5093 : }
5094 : break;
5095 : default:
5096 : break;
5097 0 : }
5098 : break;
5099 : }
5100 :
5101 : /* If at the end OFF still isn't a SSA_NAME or isn't
5102 : defined in the loop, punt. */
5103 354187 : if (TREE_CODE (off) != SSA_NAME
5104 354187 : || expr_invariant_in_loop_p (loop, off))
5105 6649 : return false;
5106 :
5107 347538 : if (offtype == NULL_TREE)
5108 225347 : offtype = TREE_TYPE (off);
5109 :
5110 347538 : if (use_ifn_p)
5111 : {
5112 0 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
5113 : vectype, memory_type, offtype,
5114 : scale, &tmp_scale,
5115 : &ifn, &offset_vectype,
5116 : &tmp_offset_vectype,
5117 : elsvals))
5118 0 : ifn = IFN_LAST;
5119 : decl = NULL_TREE;
5120 : }
5121 : else
5122 : {
5123 347538 : if (DR_IS_READ (dr))
5124 : {
5125 261661 : if (targetm.vectorize.builtin_gather)
5126 261661 : decl = targetm.vectorize.builtin_gather (vectype, offtype, scale);
5127 : }
5128 : else
5129 : {
5130 85877 : if (targetm.vectorize.builtin_scatter)
5131 85877 : decl = targetm.vectorize.builtin_scatter (vectype, offtype, scale);
5132 : }
5133 347538 : ifn = IFN_LAST;
5134 : /* The offset vector type will be read from DECL when needed. */
5135 347538 : offset_vectype = NULL_TREE;
5136 : }
5137 :
5138 347538 : gcc_checking_assert (expr_invariant_in_loop_p (loop, base));
5139 347538 : gcc_checking_assert (!expr_invariant_in_loop_p (loop, off));
5140 :
5141 347538 : info->ifn = ifn;
5142 347538 : info->decl = decl;
5143 347538 : info->base = base;
5144 :
5145 695076 : info->alias_ptr = build_int_cst
5146 347538 : (reference_alias_ptr_type (DR_REF (dr)),
5147 347538 : get_object_alignment (DR_REF (dr)));
5148 :
5149 347538 : info->offset = off;
5150 347538 : info->offset_vectype = offset_vectype;
5151 347538 : info->scale = scale;
5152 347538 : info->element_type = TREE_TYPE (vectype);
5153 347538 : info->memory_type = memory_type;
5154 347538 : return true;
5155 : }
5156 :
5157 : /* Find the data references in STMT, analyze them with respect to LOOP and
5158 : append them to DATAREFS. Return false if datarefs in this stmt cannot
5159 : be handled. */
5160 :
5161 : opt_result
5162 33087764 : vect_find_stmt_data_reference (loop_p loop, gimple *stmt,
5163 : vec<data_reference_p> *datarefs,
5164 : vec<int> *dataref_groups, int group_id)
5165 : {
5166 : /* We can ignore clobbers for dataref analysis - they are removed during
5167 : loop vectorization and BB vectorization checks dependences with a
5168 : stmt walk. */
5169 33087764 : if (gimple_clobber_p (stmt))
5170 1171922 : return opt_result::success ();
5171 :
5172 59385034 : if (gimple_has_volatile_ops (stmt))
5173 320770 : return opt_result::failure_at (stmt, "not vectorized: volatile type: %G",
5174 : stmt);
5175 :
5176 31595072 : if (stmt_can_throw_internal (cfun, stmt))
5177 716568 : return opt_result::failure_at (stmt,
5178 : "not vectorized:"
5179 : " statement can throw an exception: %G",
5180 : stmt);
5181 :
5182 30878504 : auto_vec<data_reference_p, 2> refs;
5183 30878504 : opt_result res = find_data_references_in_stmt (loop, stmt, &refs);
5184 30878504 : if (!res)
5185 3753025 : return res;
5186 :
5187 27125479 : if (refs.is_empty ())
5188 15602321 : return opt_result::success ();
5189 :
5190 11523158 : if (refs.length () > 1)
5191 : {
5192 1251949 : while (!refs.is_empty ())
5193 834935 : free_data_ref (refs.pop ());
5194 417014 : return opt_result::failure_at (stmt,
5195 : "not vectorized: more than one "
5196 : "data ref in stmt: %G", stmt);
5197 : }
5198 :
5199 11106144 : data_reference_p dr = refs.pop ();
5200 11106144 : if (gcall *call = dyn_cast <gcall *> (stmt))
5201 25713 : if (!gimple_call_internal_p (call)
5202 25713 : || (gimple_call_internal_fn (call) != IFN_MASK_LOAD
5203 22744 : && gimple_call_internal_fn (call) != IFN_MASK_STORE))
5204 : {
5205 22296 : free_data_ref (dr);
5206 22296 : return opt_result::failure_at (stmt,
5207 : "not vectorized: dr in a call %G", stmt);
5208 : }
5209 :
5210 11083848 : if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
5211 11083848 : && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
5212 : {
5213 55254 : free_data_ref (dr);
5214 55254 : return opt_result::failure_at (stmt,
5215 : "not vectorized:"
5216 : " statement is an unsupported"
5217 : " bitfield access %G", stmt);
5218 : }
5219 :
5220 11028594 : if (DR_BASE_ADDRESS (dr)
5221 10939946 : && TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
5222 : {
5223 998 : free_data_ref (dr);
5224 998 : return opt_result::failure_at (stmt,
5225 : "not vectorized:"
5226 : " base addr of dr is a constant\n");
5227 : }
5228 :
5229 : /* Check whether this may be a SIMD lane access and adjust the
5230 : DR to make it easier for us to handle it. */
5231 11027596 : if (loop
5232 605892 : && loop->simduid
5233 10688 : && (!DR_BASE_ADDRESS (dr)
5234 2952 : || !DR_OFFSET (dr)
5235 2952 : || !DR_INIT (dr)
5236 2952 : || !DR_STEP (dr)))
5237 : {
5238 7736 : struct data_reference *newdr
5239 7736 : = create_data_ref (NULL, loop_containing_stmt (stmt), DR_REF (dr), stmt,
5240 7736 : DR_IS_READ (dr), DR_IS_CONDITIONAL_IN_STMT (dr));
5241 7736 : if (DR_BASE_ADDRESS (newdr)
5242 7736 : && DR_OFFSET (newdr)
5243 7736 : && DR_INIT (newdr)
5244 7736 : && DR_STEP (newdr)
5245 7736 : && TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
5246 15472 : && integer_zerop (DR_STEP (newdr)))
5247 : {
5248 7736 : tree base_address = DR_BASE_ADDRESS (newdr);
5249 7736 : tree off = DR_OFFSET (newdr);
5250 7736 : tree step = ssize_int (1);
5251 7736 : if (integer_zerop (off)
5252 7736 : && TREE_CODE (base_address) == POINTER_PLUS_EXPR)
5253 : {
5254 82 : off = TREE_OPERAND (base_address, 1);
5255 82 : base_address = TREE_OPERAND (base_address, 0);
5256 : }
5257 7736 : STRIP_NOPS (off);
5258 7736 : if (TREE_CODE (off) == MULT_EXPR
5259 7736 : && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
5260 : {
5261 7485 : step = TREE_OPERAND (off, 1);
5262 7485 : off = TREE_OPERAND (off, 0);
5263 7485 : STRIP_NOPS (off);
5264 : }
5265 539 : if (CONVERT_EXPR_P (off)
5266 7736 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off, 0)))
5267 7197 : < TYPE_PRECISION (TREE_TYPE (off))))
5268 7197 : off = TREE_OPERAND (off, 0);
5269 7736 : if (TREE_CODE (off) == SSA_NAME)
5270 : {
5271 7213 : gimple *def = SSA_NAME_DEF_STMT (off);
5272 : /* Look through widening conversion. */
5273 7213 : if (is_gimple_assign (def)
5274 7213 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5275 : {
5276 0 : tree rhs1 = gimple_assign_rhs1 (def);
5277 0 : if (TREE_CODE (rhs1) == SSA_NAME
5278 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5279 0 : && (TYPE_PRECISION (TREE_TYPE (off))
5280 0 : > TYPE_PRECISION (TREE_TYPE (rhs1))))
5281 0 : def = SSA_NAME_DEF_STMT (rhs1);
5282 : }
5283 7213 : if (is_gimple_call (def)
5284 7077 : && gimple_call_internal_p (def)
5285 14290 : && (gimple_call_internal_fn (def) == IFN_GOMP_SIMD_LANE))
5286 : {
5287 7077 : tree arg = gimple_call_arg (def, 0);
5288 7077 : tree reft = TREE_TYPE (DR_REF (newdr));
5289 7077 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
5290 7077 : arg = SSA_NAME_VAR (arg);
5291 7077 : if (arg == loop->simduid
5292 : /* For now. */
5293 7077 : && tree_int_cst_equal (TYPE_SIZE_UNIT (reft), step))
5294 : {
5295 7052 : DR_BASE_ADDRESS (newdr) = base_address;
5296 7052 : DR_OFFSET (newdr) = ssize_int (0);
5297 7052 : DR_STEP (newdr) = step;
5298 7052 : DR_OFFSET_ALIGNMENT (newdr) = BIGGEST_ALIGNMENT;
5299 7052 : DR_STEP_ALIGNMENT (newdr) = highest_pow2_factor (step);
5300 : /* Mark as simd-lane access. */
5301 7052 : tree arg2 = gimple_call_arg (def, 1);
5302 7052 : newdr->aux = (void *) (-1 - tree_to_uhwi (arg2));
5303 7052 : free_data_ref (dr);
5304 7052 : datarefs->safe_push (newdr);
5305 7052 : if (dataref_groups)
5306 0 : dataref_groups->safe_push (group_id);
5307 7052 : return opt_result::success ();
5308 : }
5309 : }
5310 : }
5311 : }
5312 684 : free_data_ref (newdr);
5313 : }
5314 :
5315 11020544 : datarefs->safe_push (dr);
5316 11020544 : if (dataref_groups)
5317 10421704 : dataref_groups->safe_push (group_id);
5318 11020544 : return opt_result::success ();
5319 30878504 : }
5320 :
5321 : /* Function vect_analyze_data_refs.
5322 :
5323 : Find all the data references in the loop or basic block.
5324 :
5325 : The general structure of the analysis of data refs in the vectorizer is as
5326 : follows:
5327 : 1- vect_analyze_data_refs(loop/bb): call
5328 : compute_data_dependences_for_loop/bb to find and analyze all data-refs
5329 : in the loop/bb and their dependences.
5330 : 2- vect_analyze_dependences(): apply dependence testing using ddrs.
5331 : 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
5332 : 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
5333 :
5334 : */
5335 :
5336 : opt_result
5337 2776383 : vect_analyze_data_refs (vec_info *vinfo, bool *fatal)
5338 : {
5339 2776383 : class loop *loop = NULL;
5340 2776383 : unsigned int i;
5341 2776383 : struct data_reference *dr;
5342 2776383 : tree scalar_type;
5343 :
5344 2776383 : DUMP_VECT_SCOPE ("vect_analyze_data_refs");
5345 :
5346 2776383 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5347 522291 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5348 :
5349 : /* Go through the data-refs, check that the analysis succeeded. Update
5350 : pointer from stmt_vec_info struct to DR and vectype. */
5351 :
5352 2776383 : vec<data_reference_p> datarefs = vinfo->shared->datarefs;
5353 18346986 : FOR_EACH_VEC_ELT (datarefs, i, dr)
5354 : {
5355 15643684 : enum { SG_NONE, GATHER, SCATTER } gatherscatter = SG_NONE;
5356 :
5357 15643684 : gcc_assert (DR_REF (dr));
5358 15643684 : stmt_vec_info stmt_info = vinfo->lookup_stmt (DR_STMT (dr));
5359 15643684 : gcc_assert (!stmt_info->dr_aux.dr);
5360 15643684 : stmt_info->dr_aux.dr = dr;
5361 15643684 : stmt_info->dr_aux.stmt = stmt_info;
5362 :
5363 : /* Check that analysis of the data-ref succeeded. */
5364 15643684 : if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
5365 15525185 : || !DR_STEP (dr))
5366 : {
5367 236998 : bool maybe_gather
5368 118499 : = DR_IS_READ (dr)
5369 118499 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5370 236998 : bool maybe_scatter
5371 : = DR_IS_WRITE (dr)
5372 118499 : && !TREE_THIS_VOLATILE (DR_REF (dr));
5373 :
5374 : /* If target supports vector gather loads or scatter stores,
5375 : see if they can't be used. */
5376 118499 : if (is_a <loop_vec_info> (vinfo)
5377 118499 : && !nested_in_vect_loop_p (loop, stmt_info))
5378 : {
5379 115101 : if (maybe_gather || maybe_scatter)
5380 : {
5381 115101 : if (maybe_gather)
5382 : gatherscatter = GATHER;
5383 : else
5384 22259 : gatherscatter = SCATTER;
5385 : }
5386 : }
5387 :
5388 22259 : if (gatherscatter == SG_NONE)
5389 : {
5390 3398 : if (dump_enabled_p ())
5391 5 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5392 : "not vectorized: data ref analysis "
5393 : "failed %G", stmt_info->stmt);
5394 3398 : if (is_a <bb_vec_info> (vinfo))
5395 : {
5396 : /* In BB vectorization the ref can still participate
5397 : in dependence analysis, we just can't vectorize it. */
5398 3048 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5399 3048 : continue;
5400 : }
5401 350 : return opt_result::failure_at (stmt_info->stmt,
5402 : "not vectorized:"
5403 : " data ref analysis failed: %G",
5404 : stmt_info->stmt);
5405 : }
5406 : }
5407 :
5408 : /* See if this was detected as SIMD lane access. */
5409 15640286 : if (dr->aux == (void *)-1
5410 15640286 : || dr->aux == (void *)-2
5411 15631399 : || dr->aux == (void *)-3
5412 15630559 : || dr->aux == (void *)-4)
5413 : {
5414 10527 : if (nested_in_vect_loop_p (loop, stmt_info))
5415 0 : return opt_result::failure_at (stmt_info->stmt,
5416 : "not vectorized:"
5417 : " data ref analysis failed: %G",
5418 : stmt_info->stmt);
5419 10527 : STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)
5420 10527 : = -(uintptr_t) dr->aux;
5421 : }
5422 :
5423 15640286 : tree base = get_base_address (DR_REF (dr));
5424 15640286 : if (base && VAR_P (base) && DECL_NONALIASED (base))
5425 : {
5426 9086 : if (dump_enabled_p ())
5427 186 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5428 : "not vectorized: base object not addressable "
5429 : "for stmt: %G", stmt_info->stmt);
5430 9086 : if (is_a <bb_vec_info> (vinfo))
5431 : {
5432 : /* In BB vectorization the ref can still participate
5433 : in dependence analysis, we just can't vectorize it. */
5434 9085 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5435 9085 : continue;
5436 : }
5437 1 : return opt_result::failure_at (stmt_info->stmt,
5438 : "not vectorized: base object not"
5439 : " addressable for stmt: %G",
5440 : stmt_info->stmt);
5441 : }
5442 :
5443 15631200 : if (is_a <loop_vec_info> (vinfo)
5444 1183151 : && DR_STEP (dr)
5445 16699250 : && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
5446 : {
5447 44859 : if (nested_in_vect_loop_p (loop, stmt_info))
5448 394 : return opt_result::failure_at (stmt_info->stmt,
5449 : "not vectorized: "
5450 : "not suitable for strided load %G",
5451 : stmt_info->stmt);
5452 44465 : STMT_VINFO_STRIDED_P (stmt_info) = true;
5453 : }
5454 :
5455 : /* Update DR field in stmt_vec_info struct. */
5456 :
5457 : /* If the dataref is in an inner-loop of the loop that is considered for
5458 : for vectorization, we also want to analyze the access relative to
5459 : the outer-loop (DR contains information only relative to the
5460 : inner-most enclosing loop). We do that by building a reference to the
5461 : first location accessed by the inner-loop, and analyze it relative to
5462 : the outer-loop. */
5463 15630806 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
5464 : {
5465 : /* Build a reference to the first location accessed by the
5466 : inner loop: *(BASE + INIT + OFFSET). By construction,
5467 : this address must be invariant in the inner loop, so we
5468 : can consider it as being used in the outer loop. */
5469 11949 : tree base = unshare_expr (DR_BASE_ADDRESS (dr));
5470 11949 : tree offset = unshare_expr (DR_OFFSET (dr));
5471 11949 : tree init = unshare_expr (DR_INIT (dr));
5472 11949 : tree init_offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset),
5473 : init, offset);
5474 11949 : tree init_addr = fold_build_pointer_plus (base, init_offset);
5475 11949 : tree init_ref = build_fold_indirect_ref (init_addr);
5476 :
5477 11949 : if (dump_enabled_p ())
5478 1228 : dump_printf_loc (MSG_NOTE, vect_location,
5479 : "analyze in outer loop: %T\n", init_ref);
5480 :
5481 11949 : opt_result res
5482 11949 : = dr_analyze_innermost (&STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info),
5483 11949 : init_ref, loop, stmt_info->stmt);
5484 11949 : if (!res)
5485 : /* dr_analyze_innermost already explained the failure. */
5486 166 : return res;
5487 :
5488 11783 : if (dump_enabled_p ())
5489 1224 : dump_printf_loc (MSG_NOTE, vect_location,
5490 : "\touter base_address: %T\n"
5491 : "\touter offset from base address: %T\n"
5492 : "\touter constant offset from base address: %T\n"
5493 : "\touter step: %T\n"
5494 : "\touter base alignment: %d\n\n"
5495 : "\touter base misalignment: %d\n"
5496 : "\touter offset alignment: %d\n"
5497 : "\touter step alignment: %d\n",
5498 : STMT_VINFO_DR_BASE_ADDRESS (stmt_info),
5499 : STMT_VINFO_DR_OFFSET (stmt_info),
5500 : STMT_VINFO_DR_INIT (stmt_info),
5501 : STMT_VINFO_DR_STEP (stmt_info),
5502 : STMT_VINFO_DR_BASE_ALIGNMENT (stmt_info),
5503 : STMT_VINFO_DR_BASE_MISALIGNMENT (stmt_info),
5504 : STMT_VINFO_DR_OFFSET_ALIGNMENT (stmt_info),
5505 : STMT_VINFO_DR_STEP_ALIGNMENT (stmt_info));
5506 : }
5507 :
5508 : /* Set vectype for STMT. */
5509 15630640 : scalar_type = TREE_TYPE (DR_REF (dr));
5510 15630640 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
5511 15630640 : if (!vectype)
5512 : {
5513 1888243 : if (dump_enabled_p ())
5514 : {
5515 2036 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5516 : "not vectorized: no vectype for stmt: %G",
5517 : stmt_info->stmt);
5518 2036 : dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
5519 2036 : dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
5520 : scalar_type);
5521 2036 : dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5522 : }
5523 :
5524 1888243 : if (is_a <bb_vec_info> (vinfo))
5525 : {
5526 : /* No vector type is fine, the ref can still participate
5527 : in dependence analysis, we just can't vectorize it. */
5528 1823648 : STMT_VINFO_VECTORIZABLE (stmt_info) = false;
5529 1823648 : continue;
5530 : }
5531 64595 : if (fatal)
5532 64595 : *fatal = false;
5533 64595 : return opt_result::failure_at (stmt_info->stmt,
5534 : "not vectorized:"
5535 : " no vectype for stmt: %G"
5536 : " scalar_type: %T\n",
5537 : stmt_info->stmt, scalar_type);
5538 : }
5539 : else
5540 : {
5541 13742397 : if (dump_enabled_p ())
5542 83286 : dump_printf_loc (MSG_NOTE, vect_location,
5543 : "got vectype for stmt: %G%T\n",
5544 : stmt_info->stmt, vectype);
5545 : }
5546 :
5547 : /* Leave the BB vectorizer to pick the vector type later, based on
5548 : the final dataref group size and SLP node size. */
5549 13742397 : if (is_a <loop_vec_info> (vinfo))
5550 1117996 : STMT_VINFO_VECTYPE (stmt_info) = vectype;
5551 :
5552 13742397 : if (gatherscatter != SG_NONE)
5553 : {
5554 109385 : gather_scatter_info gs_info;
5555 109385 : if (!vect_check_gather_scatter (stmt_info, vectype,
5556 : as_a <loop_vec_info> (vinfo),
5557 : &gs_info)
5558 214730 : || !get_vectype_for_scalar_type (vinfo,
5559 105345 : TREE_TYPE (gs_info.offset)))
5560 : {
5561 7575 : if (fatal)
5562 7575 : *fatal = false;
5563 7575 : return opt_result::failure_at
5564 7948 : (stmt_info->stmt,
5565 : (gatherscatter == GATHER)
5566 : ? "not vectorized: not suitable for gather load %G"
5567 : : "not vectorized: not suitable for scatter store %G",
5568 : stmt_info->stmt);
5569 : }
5570 101810 : STMT_VINFO_GATHER_SCATTER_P (stmt_info) = gatherscatter;
5571 : }
5572 : }
5573 :
5574 : /* We used to stop processing and prune the list here. Verify we no
5575 : longer need to. */
5576 4323810 : gcc_assert (i == datarefs.length ());
5577 :
5578 2703302 : return opt_result::success ();
5579 : }
5580 :
5581 :
5582 : /* Function vect_get_new_vect_var.
5583 :
5584 : Returns a name for a new variable. The current naming scheme appends the
5585 : prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
5586 : the name of vectorizer generated variables, and appends that to NAME if
5587 : provided. */
5588 :
5589 : tree
5590 1983500 : vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
5591 : {
5592 1983500 : const char *prefix;
5593 1983500 : tree new_vect_var;
5594 :
5595 1983500 : switch (var_kind)
5596 : {
5597 : case vect_simple_var:
5598 : prefix = "vect";
5599 : break;
5600 23081 : case vect_scalar_var:
5601 23081 : prefix = "stmp";
5602 23081 : break;
5603 20391 : case vect_mask_var:
5604 20391 : prefix = "mask";
5605 20391 : break;
5606 1429198 : case vect_pointer_var:
5607 1429198 : prefix = "vectp";
5608 1429198 : break;
5609 0 : default:
5610 0 : gcc_unreachable ();
5611 : }
5612 :
5613 1983500 : if (name)
5614 : {
5615 1115686 : char* tmp = concat (prefix, "_", name, NULL);
5616 1115686 : new_vect_var = create_tmp_reg (type, tmp);
5617 1115686 : free (tmp);
5618 : }
5619 : else
5620 867814 : new_vect_var = create_tmp_reg (type, prefix);
5621 :
5622 1983500 : return new_vect_var;
5623 : }
5624 :
5625 : /* Like vect_get_new_vect_var but return an SSA name. */
5626 :
5627 : tree
5628 7110 : vect_get_new_ssa_name (tree type, enum vect_var_kind var_kind, const char *name)
5629 : {
5630 7110 : const char *prefix;
5631 7110 : tree new_vect_var;
5632 :
5633 7110 : switch (var_kind)
5634 : {
5635 : case vect_simple_var:
5636 : prefix = "vect";
5637 : break;
5638 315 : case vect_scalar_var:
5639 315 : prefix = "stmp";
5640 315 : break;
5641 0 : case vect_pointer_var:
5642 0 : prefix = "vectp";
5643 0 : break;
5644 0 : default:
5645 0 : gcc_unreachable ();
5646 : }
5647 :
5648 7110 : if (name)
5649 : {
5650 6624 : char* tmp = concat (prefix, "_", name, NULL);
5651 6624 : new_vect_var = make_temp_ssa_name (type, NULL, tmp);
5652 6624 : free (tmp);
5653 : }
5654 : else
5655 486 : new_vect_var = make_temp_ssa_name (type, NULL, prefix);
5656 :
5657 7110 : return new_vect_var;
5658 : }
5659 :
5660 : /* Duplicate points-to info on NAME from DR_INFO. */
5661 :
5662 : static void
5663 435051 : vect_duplicate_ssa_name_ptr_info (tree name, dr_vec_info *dr_info)
5664 : {
5665 435051 : if (DR_PTR_INFO (dr_info->dr))
5666 : {
5667 292688 : duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr_info->dr));
5668 : /* DR_PTR_INFO is for a base SSA name, not including constant or
5669 : variable offsets in the ref so its alignment info does not apply. */
5670 292688 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
5671 : }
5672 142363 : else if (!SSA_NAME_PTR_INFO (name))
5673 : {
5674 142363 : tree base = get_base_address (dr_info->dr->ref);
5675 142363 : if (VAR_P (base)
5676 : || TREE_CODE (base) == PARM_DECL
5677 : || TREE_CODE (base) == RESULT_DECL)
5678 : {
5679 130507 : struct ptr_info_def *pi = get_ptr_info (name);
5680 130507 : pt_solution_set_var (&pi->pt, base);
5681 : }
5682 : }
5683 435051 : }
5684 :
5685 : /* Function vect_create_addr_base_for_vector_ref.
5686 :
5687 : Create an expression that computes the address of the first memory location
5688 : that will be accessed for a data reference.
5689 :
5690 : Input:
5691 : STMT_INFO: The statement containing the data reference.
5692 : NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
5693 : OFFSET: Optional. If supplied, it is be added to the initial address.
5694 : LOOP: Specify relative to which loop-nest should the address be computed.
5695 : For example, when the dataref is in an inner-loop nested in an
5696 : outer-loop that is now being vectorized, LOOP can be either the
5697 : outer-loop, or the inner-loop. The first memory location accessed
5698 : by the following dataref ('in' points to short):
5699 :
5700 : for (i=0; i<N; i++)
5701 : for (j=0; j<M; j++)
5702 : s += in[i+j]
5703 :
5704 : is as follows:
5705 : if LOOP=i_loop: &in (relative to i_loop)
5706 : if LOOP=j_loop: &in+i*2B (relative to j_loop)
5707 :
5708 : Output:
5709 : 1. Return an SSA_NAME whose value is the address of the memory location of
5710 : the first vector of the data reference.
5711 : 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
5712 : these statement(s) which define the returned SSA_NAME.
5713 :
5714 : FORNOW: We are only handling array accesses with step 1. */
5715 :
5716 : tree
5717 705750 : vect_create_addr_base_for_vector_ref (vec_info *vinfo, stmt_vec_info stmt_info,
5718 : gimple_seq *new_stmt_list,
5719 : tree offset)
5720 : {
5721 705750 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5722 705750 : struct data_reference *dr = dr_info->dr;
5723 705750 : const char *base_name;
5724 705750 : tree addr_base;
5725 705750 : tree dest;
5726 705750 : gimple_seq seq = NULL;
5727 705750 : tree vect_ptr_type;
5728 705750 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5729 705750 : innermost_loop_behavior *drb = vect_dr_behavior (vinfo, dr_info);
5730 :
5731 705750 : tree data_ref_base = unshare_expr (drb->base_address);
5732 705750 : tree vector_offset = NULL_TREE;
5733 705750 : if (loop_vinfo && dr_info->offset)
5734 17967 : vector_offset = unshare_expr (dr_info->offset);
5735 705750 : tree base_offset = unshare_expr (vector_offset
5736 : ? drb->offset
5737 687783 : : get_dr_vinfo_offset (vinfo, dr_info,
5738 : true));
5739 705750 : tree init = unshare_expr (drb->init);
5740 :
5741 705750 : if (loop_vinfo)
5742 129017 : base_name = get_name (data_ref_base);
5743 : else
5744 : {
5745 576733 : base_offset = ssize_int (0);
5746 576733 : init = ssize_int (0);
5747 576733 : base_name = get_name (DR_REF (dr));
5748 : }
5749 :
5750 : /* Create base_offset */
5751 705750 : base_offset = size_binop (PLUS_EXPR,
5752 : fold_convert (sizetype, base_offset),
5753 : fold_convert (sizetype, init));
5754 :
5755 705750 : if (offset)
5756 : {
5757 3109 : offset = fold_convert (sizetype, offset);
5758 3109 : base_offset = fold_build2 (PLUS_EXPR, sizetype,
5759 : base_offset, offset);
5760 : }
5761 :
5762 : /* base + base_offset */
5763 705750 : if (loop_vinfo)
5764 129017 : addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
5765 : else
5766 1153466 : addr_base = build1 (ADDR_EXPR,
5767 576733 : build_pointer_type (TREE_TYPE (DR_REF (dr))),
5768 : /* Strip zero offset components since we don't need
5769 : them and they can confuse late diagnostics if
5770 : we CSE them wrongly. See PR106904 for example. */
5771 : unshare_expr (strip_zero_offset_components
5772 : (DR_REF (dr))));
5773 :
5774 705750 : vect_ptr_type = build_pointer_type (TREE_TYPE (DR_REF (dr)));
5775 705750 : dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
5776 :
5777 : /* Keep vectorizer-added offsets separate from the original scalar access
5778 : address. Forming "base + scalar offset" first gives the target a better
5779 : chance of sharing it with other address calculations, such as the
5780 : misalignment check used for masked alignment peeling. */
5781 705750 : if (vector_offset)
5782 : {
5783 17967 : tree scalar_dest = vect_get_new_vect_var (vect_ptr_type,
5784 : vect_pointer_var, base_name);
5785 17967 : gimple_seq addr_seq = NULL;
5786 17967 : addr_base = force_gimple_operand (addr_base, &addr_seq, true,
5787 : scalar_dest);
5788 17967 : gimple_seq_add_seq (&seq, addr_seq);
5789 17967 : addr_base = fold_build_pointer_plus (addr_base,
5790 : fold_convert (sizetype,
5791 : vector_offset));
5792 : }
5793 :
5794 705750 : gimple_seq addr_seq = NULL;
5795 705750 : addr_base = force_gimple_operand (addr_base, &addr_seq, true, dest);
5796 705750 : gimple_seq_add_seq (&seq, addr_seq);
5797 705750 : gimple_seq_add_seq (new_stmt_list, seq);
5798 :
5799 705750 : if (TREE_CODE (addr_base) == SSA_NAME
5800 : /* We should only duplicate pointer info to newly created SSA names. */
5801 712484 : && SSA_NAME_VAR (addr_base) == dest)
5802 : {
5803 176899 : gcc_assert (!SSA_NAME_PTR_INFO (addr_base));
5804 176899 : vect_duplicate_ssa_name_ptr_info (addr_base, dr_info);
5805 : }
5806 :
5807 705750 : if (dump_enabled_p ())
5808 25365 : dump_printf_loc (MSG_NOTE, vect_location, "created %T\n", addr_base);
5809 :
5810 705750 : return addr_base;
5811 : }
5812 :
5813 :
5814 : /* Function vect_create_data_ref_ptr.
5815 :
5816 : Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
5817 : location accessed in the loop by STMT_INFO, along with the def-use update
5818 : chain to appropriately advance the pointer through the loop iterations.
5819 : Also set aliasing information for the pointer. This pointer is used by
5820 : the callers to this function to create a memory reference expression for
5821 : vector load/store access.
5822 :
5823 : Input:
5824 : 1. STMT_INFO: a stmt that references memory. Expected to be of the form
5825 : GIMPLE_ASSIGN <name, data-ref> or
5826 : GIMPLE_ASSIGN <data-ref, name>.
5827 : 2. AGGR_TYPE: the type of the reference, which should be either a vector
5828 : or an array.
5829 : 3. AT_LOOP: the loop where the vector memref is to be created.
5830 : 4. OFFSET (optional): a byte offset to be added to the initial address
5831 : accessed by the data-ref in STMT_INFO.
5832 : 5. BSI: location where the new stmts are to be placed if there is no loop
5833 : 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
5834 : pointing to the initial address.
5835 : 8. IV_STEP (optional, defaults to NULL): the amount that should be added
5836 : to the IV during each iteration of the loop. NULL says to move
5837 : by one copy of AGGR_TYPE up or down, depending on the step of the
5838 : data reference.
5839 :
5840 : Output:
5841 : 1. Declare a new ptr to vector_type, and have it point to the base of the
5842 : data reference (initial addressed accessed by the data reference).
5843 : For example, for vector of type V8HI, the following code is generated:
5844 :
5845 : v8hi *ap;
5846 : ap = (v8hi *)initial_address;
5847 :
5848 : if OFFSET is not supplied:
5849 : initial_address = &a[init];
5850 : if OFFSET is supplied:
5851 : initial_address = &a[init] + OFFSET;
5852 : if BYTE_OFFSET is supplied:
5853 : initial_address = &a[init] + BYTE_OFFSET;
5854 :
5855 : Return the initial_address in INITIAL_ADDRESS.
5856 :
5857 : 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
5858 : update the pointer in each iteration of the loop.
5859 :
5860 : Return the increment stmt that updates the pointer in PTR_INCR.
5861 :
5862 : 3. Return the pointer. */
5863 :
5864 : tree
5865 705481 : vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
5866 : tree aggr_type, class loop *at_loop, tree offset,
5867 : tree *initial_address, gimple_stmt_iterator *gsi,
5868 : gimple **ptr_incr, bool only_init,
5869 : tree iv_step)
5870 : {
5871 705481 : const char *base_name;
5872 705481 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5873 705481 : class loop *loop = NULL;
5874 705481 : bool nested_in_vect_loop = false;
5875 705481 : class loop *containing_loop = NULL;
5876 705481 : tree aggr_ptr_type;
5877 705481 : tree aggr_ptr;
5878 705481 : tree new_temp;
5879 705481 : gimple_seq new_stmt_list = NULL;
5880 705481 : edge pe = NULL;
5881 705481 : basic_block new_bb;
5882 705481 : tree aggr_ptr_init;
5883 705481 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
5884 705481 : struct data_reference *dr = dr_info->dr;
5885 705481 : tree aptr;
5886 705481 : gimple_stmt_iterator incr_gsi;
5887 705481 : bool insert_after;
5888 705481 : tree indx_before_incr, indx_after_incr;
5889 705481 : gimple *incr;
5890 705481 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5891 :
5892 705481 : gcc_assert (iv_step != NULL_TREE
5893 : || TREE_CODE (aggr_type) == ARRAY_TYPE
5894 : || TREE_CODE (aggr_type) == VECTOR_TYPE);
5895 :
5896 705481 : if (loop_vinfo)
5897 : {
5898 128748 : loop = LOOP_VINFO_LOOP (loop_vinfo);
5899 128748 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
5900 128748 : containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
5901 128748 : pe = loop_preheader_edge (loop);
5902 : }
5903 : else
5904 : {
5905 576733 : gcc_assert (bb_vinfo);
5906 576733 : only_init = true;
5907 576733 : if (ptr_incr)
5908 0 : *ptr_incr = NULL;
5909 : }
5910 :
5911 : /* Create an expression for the first address accessed by this load
5912 : in LOOP. */
5913 705481 : base_name = get_name (DR_BASE_ADDRESS (dr));
5914 :
5915 705481 : if (dump_enabled_p ())
5916 : {
5917 25261 : tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
5918 25261 : dump_printf_loc (MSG_NOTE, vect_location,
5919 : "create %s-pointer variable to type: %T",
5920 25261 : get_tree_code_name (TREE_CODE (aggr_type)),
5921 : aggr_type);
5922 25261 : if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
5923 13555 : dump_printf (MSG_NOTE, " vectorizing an array ref: ");
5924 11706 : else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
5925 0 : dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
5926 11706 : else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
5927 1644 : dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
5928 : else
5929 10062 : dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
5930 25261 : dump_printf (MSG_NOTE, "%T\n", DR_BASE_OBJECT (dr));
5931 : }
5932 :
5933 : /* (1) Create the new aggregate-pointer variable.
5934 : Vector and array types inherit the alias set of their component
5935 : type by default so we need to use a ref-all pointer if the data
5936 : reference does not conflict with the created aggregated data
5937 : reference because it is not addressable. */
5938 705481 : bool need_ref_all = false;
5939 705481 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5940 : get_alias_set (DR_REF (dr))))
5941 : need_ref_all = true;
5942 : /* Likewise for any of the data references in the stmt group. */
5943 599137 : else if (DR_GROUP_SIZE (stmt_info) > 1)
5944 : {
5945 484889 : stmt_vec_info sinfo = DR_GROUP_FIRST_ELEMENT (stmt_info);
5946 1355307 : do
5947 : {
5948 1355307 : struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
5949 1355307 : if (!alias_sets_conflict_p (get_alias_set (aggr_type),
5950 : get_alias_set (DR_REF (sdr))))
5951 : {
5952 : need_ref_all = true;
5953 : break;
5954 : }
5955 1329852 : sinfo = DR_GROUP_NEXT_ELEMENT (sinfo);
5956 : }
5957 1329852 : while (sinfo);
5958 : }
5959 705481 : aggr_ptr_type = build_pointer_type_for_mode (aggr_type, VOIDmode,
5960 : need_ref_all);
5961 705481 : aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
5962 :
5963 :
5964 : /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
5965 : vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
5966 : def-use update cycles for the pointer: one relative to the outer-loop
5967 : (LOOP), which is what steps (3) and (4) below do. The other is relative
5968 : to the inner-loop (which is the inner-most loop containing the dataref),
5969 : and this is done be step (5) below.
5970 :
5971 : When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
5972 : inner-most loop, and so steps (3),(4) work the same, and step (5) is
5973 : redundant. Steps (3),(4) create the following:
5974 :
5975 : vp0 = &base_addr;
5976 : LOOP: vp1 = phi(vp0,vp2)
5977 : ...
5978 : ...
5979 : vp2 = vp1 + step
5980 : goto LOOP
5981 :
5982 : If there is an inner-loop nested in loop, then step (5) will also be
5983 : applied, and an additional update in the inner-loop will be created:
5984 :
5985 : vp0 = &base_addr;
5986 : LOOP: vp1 = phi(vp0,vp2)
5987 : ...
5988 : inner: vp3 = phi(vp1,vp4)
5989 : vp4 = vp3 + inner_step
5990 : if () goto inner
5991 : ...
5992 : vp2 = vp1 + step
5993 : if () goto LOOP */
5994 :
5995 : /* (2) Calculate the initial address of the aggregate-pointer, and set
5996 : the aggregate-pointer to point to it before the loop. */
5997 :
5998 : /* Create: (&(base[init_val]+offset) in the loop preheader. */
5999 :
6000 705481 : new_temp = vect_create_addr_base_for_vector_ref (vinfo,
6001 : stmt_info, &new_stmt_list,
6002 : offset);
6003 705481 : if (new_stmt_list)
6004 : {
6005 176778 : if (pe)
6006 : {
6007 54717 : new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
6008 54717 : gcc_assert (!new_bb);
6009 : }
6010 : else
6011 122061 : gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
6012 : }
6013 :
6014 705481 : *initial_address = new_temp;
6015 705481 : aggr_ptr_init = new_temp;
6016 :
6017 : /* (3) Handle the updating of the aggregate-pointer inside the loop.
6018 : This is needed when ONLY_INIT is false, and also when AT_LOOP is the
6019 : inner-loop nested in LOOP (during outer-loop vectorization). */
6020 :
6021 : /* No update in loop is required. */
6022 705481 : if (only_init && (!loop_vinfo || at_loop == loop))
6023 : aptr = aggr_ptr_init;
6024 : else
6025 : {
6026 : /* Accesses to invariant addresses should be handled specially
6027 : by the caller. */
6028 128740 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
6029 128740 : gcc_assert (!integer_zerop (step));
6030 :
6031 128740 : if (iv_step == NULL_TREE)
6032 : {
6033 : /* The step of the aggregate pointer is the type size,
6034 : negated for downward accesses. */
6035 0 : iv_step = TYPE_SIZE_UNIT (aggr_type);
6036 0 : if (tree_int_cst_sgn (step) == -1)
6037 0 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
6038 : }
6039 :
6040 128740 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6041 :
6042 128740 : create_iv (aggr_ptr_init, PLUS_EXPR,
6043 : iv_step, aggr_ptr, loop, &incr_gsi, insert_after,
6044 : &indx_before_incr, &indx_after_incr,
6045 : !loop_vinfo
6046 128740 : || LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
6047 128740 : incr = gsi_stmt (incr_gsi);
6048 :
6049 : /* Copy the points-to information if it exists. */
6050 128740 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
6051 128740 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
6052 128740 : if (ptr_incr)
6053 0 : *ptr_incr = incr;
6054 :
6055 128740 : aptr = indx_before_incr;
6056 : }
6057 :
6058 705481 : if (!nested_in_vect_loop || only_init)
6059 : return aptr;
6060 :
6061 :
6062 : /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
6063 : nested in LOOP, if exists. */
6064 :
6065 336 : gcc_assert (nested_in_vect_loop);
6066 336 : if (!only_init)
6067 : {
6068 336 : standard_iv_increment_position (containing_loop, &incr_gsi,
6069 : &insert_after);
6070 336 : create_iv (aptr, PLUS_EXPR, DR_STEP (dr),
6071 : aggr_ptr, containing_loop, &incr_gsi, insert_after,
6072 : &indx_before_incr, &indx_after_incr);
6073 336 : incr = gsi_stmt (incr_gsi);
6074 :
6075 : /* Copy the points-to information if it exists. */
6076 336 : vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr_info);
6077 336 : vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr_info);
6078 336 : if (ptr_incr)
6079 0 : *ptr_incr = incr;
6080 :
6081 336 : return indx_before_incr;
6082 : }
6083 : else
6084 : gcc_unreachable ();
6085 : }
6086 :
6087 :
6088 : /* Function bump_vector_ptr
6089 :
6090 : Increment DATAREF_PTR by UPDATE.
6091 :
6092 : Input:
6093 : DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
6094 : in the loop.
6095 : GSI - location where the new update stmt is to be placed.
6096 : STMT_INFO - the original scalar memory-access stmt that is being vectorized.
6097 : UPDATE - The offset by which to bump the pointer.
6098 :
6099 : Output: Return NEW_DATAREF_PTR as illustrated above.
6100 :
6101 : */
6102 :
6103 : tree
6104 244623 : bump_vector_ptr (vec_info *vinfo,
6105 : tree dataref_ptr, gimple_stmt_iterator *gsi,
6106 : stmt_vec_info stmt_info, tree update)
6107 : {
6108 244623 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
6109 244623 : gimple *incr_stmt;
6110 244623 : tree new_dataref_ptr;
6111 :
6112 244623 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
6113 113882 : new_dataref_ptr = copy_ssa_name (dataref_ptr);
6114 130741 : else if (is_gimple_min_invariant (dataref_ptr))
6115 : /* When possible avoid emitting a separate increment stmt that will
6116 : force the addressed object addressable. */
6117 261482 : return build1 (ADDR_EXPR, TREE_TYPE (dataref_ptr),
6118 130741 : fold_build2 (MEM_REF,
6119 : TREE_TYPE (TREE_TYPE (dataref_ptr)),
6120 : dataref_ptr,
6121 130741 : fold_convert (ptr_type_node, update)));
6122 : else
6123 0 : new_dataref_ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
6124 113882 : incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
6125 : dataref_ptr, update);
6126 113882 : vect_finish_stmt_generation (vinfo, stmt_info, incr_stmt, gsi);
6127 : /* Fold the increment, avoiding excessive chains use-def chains of
6128 : those, leading to compile-time issues for passes until the next
6129 : forwprop pass which would do this as well. */
6130 113882 : gimple_stmt_iterator fold_gsi = gsi_for_stmt (incr_stmt);
6131 113882 : if (fold_stmt (&fold_gsi, follow_all_ssa_edges))
6132 : {
6133 74052 : incr_stmt = gsi_stmt (fold_gsi);
6134 74052 : update_stmt (incr_stmt);
6135 : }
6136 :
6137 : /* Copy the points-to information if it exists. */
6138 113882 : duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
6139 :
6140 113882 : return new_dataref_ptr;
6141 : }
6142 :
6143 :
6144 : /* Copy memory reference info such as base/clique from the SRC reference
6145 : to the DEST MEM_REF. */
6146 :
6147 : void
6148 960373 : vect_copy_ref_info (tree dest, tree src)
6149 : {
6150 960373 : if (TREE_CODE (dest) != MEM_REF)
6151 : return;
6152 :
6153 : tree src_base = src;
6154 1930080 : while (handled_component_p (src_base))
6155 974366 : src_base = TREE_OPERAND (src_base, 0);
6156 955714 : if (TREE_CODE (src_base) != MEM_REF
6157 955714 : && TREE_CODE (src_base) != TARGET_MEM_REF)
6158 : return;
6159 :
6160 523092 : MR_DEPENDENCE_CLIQUE (dest) = MR_DEPENDENCE_CLIQUE (src_base);
6161 523092 : MR_DEPENDENCE_BASE (dest) = MR_DEPENDENCE_BASE (src_base);
6162 : }
6163 :
6164 :
6165 : /* Function vect_create_destination_var.
6166 :
6167 : Create a new temporary of type VECTYPE. */
6168 :
6169 : tree
6170 537359 : vect_create_destination_var (tree scalar_dest, tree vectype)
6171 : {
6172 537359 : tree vec_dest;
6173 537359 : const char *name;
6174 537359 : char *new_name;
6175 537359 : tree type;
6176 537359 : enum vect_var_kind kind;
6177 :
6178 537359 : kind = vectype
6179 1051637 : ? VECTOR_BOOLEAN_TYPE_P (vectype)
6180 514278 : ? vect_mask_var
6181 : : vect_simple_var
6182 : : vect_scalar_var;
6183 23081 : type = vectype ? vectype : TREE_TYPE (scalar_dest);
6184 :
6185 537359 : gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
6186 :
6187 537359 : name = get_name (scalar_dest);
6188 537359 : if (name)
6189 192150 : new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
6190 : else
6191 345209 : new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
6192 537359 : vec_dest = vect_get_new_vect_var (type, kind, new_name);
6193 537359 : free (new_name);
6194 :
6195 537359 : return vec_dest;
6196 : }
6197 :
6198 : /* Function vect_grouped_store_supported.
6199 :
6200 : Returns TRUE if interleave high and interleave low permutations
6201 : are supported, and FALSE otherwise. */
6202 :
6203 : bool
6204 2736 : vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
6205 : {
6206 2736 : machine_mode mode = TYPE_MODE (vectype);
6207 :
6208 : /* vect_permute_store_chain requires the group size to be equal to 3 or
6209 : be a power of two. */
6210 2736 : if (count != 3 && exact_log2 (count) == -1)
6211 : {
6212 549 : if (dump_enabled_p ())
6213 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6214 : "the size of the group of accesses"
6215 : " is not a power of 2 or not equal to 3\n");
6216 549 : return false;
6217 : }
6218 :
6219 : /* Check that the permutation is supported. */
6220 2187 : if (VECTOR_MODE_P (mode))
6221 : {
6222 2187 : unsigned int i;
6223 2187 : if (count == 3)
6224 : {
6225 954 : unsigned int j0 = 0, j1 = 0, j2 = 0;
6226 954 : unsigned int i, j;
6227 :
6228 954 : unsigned int nelt;
6229 1908 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6230 : {
6231 : if (dump_enabled_p ())
6232 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6233 : "cannot handle groups of 3 stores for"
6234 : " variable-length vectors\n");
6235 : return false;
6236 : }
6237 :
6238 954 : vec_perm_builder sel (nelt, nelt, 1);
6239 954 : sel.quick_grow (nelt);
6240 954 : vec_perm_indices indices;
6241 3591 : for (j = 0; j < 3; j++)
6242 : {
6243 2712 : int nelt0 = ((3 - j) * nelt) % 3;
6244 2712 : int nelt1 = ((3 - j) * nelt + 1) % 3;
6245 2712 : int nelt2 = ((3 - j) * nelt + 2) % 3;
6246 9612 : for (i = 0; i < nelt; i++)
6247 : {
6248 6900 : if (3 * i + nelt0 < nelt)
6249 2338 : sel[3 * i + nelt0] = j0++;
6250 6900 : if (3 * i + nelt1 < nelt)
6251 2299 : sel[3 * i + nelt1] = nelt + j1++;
6252 6900 : if (3 * i + nelt2 < nelt)
6253 2263 : sel[3 * i + nelt2] = 0;
6254 : }
6255 2712 : indices.new_vector (sel, 2, nelt);
6256 2712 : if (!can_vec_perm_const_p (mode, mode, indices))
6257 : {
6258 66 : if (dump_enabled_p ())
6259 37 : dump_printf (MSG_MISSED_OPTIMIZATION,
6260 : "permutation op not supported by target.\n");
6261 66 : return false;
6262 : }
6263 :
6264 8970 : for (i = 0; i < nelt; i++)
6265 : {
6266 6324 : if (3 * i + nelt0 < nelt)
6267 2114 : sel[3 * i + nelt0] = 3 * i + nelt0;
6268 6324 : if (3 * i + nelt1 < nelt)
6269 2105 : sel[3 * i + nelt1] = 3 * i + nelt1;
6270 6324 : if (3 * i + nelt2 < nelt)
6271 2105 : sel[3 * i + nelt2] = nelt + j2++;
6272 : }
6273 2646 : indices.new_vector (sel, 2, nelt);
6274 2646 : if (!can_vec_perm_const_p (mode, mode, indices))
6275 : {
6276 9 : if (dump_enabled_p ())
6277 9 : dump_printf (MSG_MISSED_OPTIMIZATION,
6278 : "permutation op not supported by target.\n");
6279 9 : return false;
6280 : }
6281 : }
6282 : return true;
6283 954 : }
6284 : else
6285 : {
6286 : /* If length is not equal to 3 then only power of 2 is supported. */
6287 1233 : gcc_assert (pow2p_hwi (count));
6288 2466 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6289 :
6290 : /* The encoding has 2 interleaved stepped patterns. */
6291 2466 : if(!multiple_p (nelt, 2))
6292 1187 : return false;
6293 1233 : vec_perm_builder sel (nelt, 2, 3);
6294 1233 : sel.quick_grow (6);
6295 6165 : for (i = 0; i < 3; i++)
6296 : {
6297 3699 : sel[i * 2] = i;
6298 3699 : sel[i * 2 + 1] = i + nelt;
6299 : }
6300 1233 : vec_perm_indices indices (sel, 2, nelt);
6301 1233 : if (can_vec_perm_const_p (mode, mode, indices))
6302 : {
6303 8309 : for (i = 0; i < 6; i++)
6304 7122 : sel[i] += exact_div (nelt, 2);
6305 1187 : indices.new_vector (sel, 2, nelt);
6306 1187 : if (can_vec_perm_const_p (mode, mode, indices))
6307 1187 : return true;
6308 : }
6309 1233 : }
6310 : }
6311 :
6312 46 : if (dump_enabled_p ())
6313 3 : dump_printf (MSG_MISSED_OPTIMIZATION,
6314 : "permutation op not supported by target.\n");
6315 : return false;
6316 : }
6317 :
6318 : /* Return FN if vec_{mask_,mask_len_}store_lanes is available for COUNT vectors
6319 : of type VECTYPE. MASKED_P says whether the masked form is needed. */
6320 :
6321 : internal_fn
6322 41108 : vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6323 : bool masked_p)
6324 : {
6325 41108 : if (vect_lanes_optab_supported_p ("vec_mask_len_store_lanes",
6326 : vec_mask_len_store_lanes_optab, vectype,
6327 : count))
6328 : return IFN_MASK_LEN_STORE_LANES;
6329 41108 : else if (masked_p)
6330 : {
6331 111 : if (vect_lanes_optab_supported_p ("vec_mask_store_lanes",
6332 : vec_mask_store_lanes_optab, vectype,
6333 : count))
6334 : return IFN_MASK_STORE_LANES;
6335 : }
6336 : else
6337 : {
6338 40997 : if (vect_lanes_optab_supported_p ("vec_store_lanes",
6339 : vec_store_lanes_optab, vectype, count))
6340 : return IFN_STORE_LANES;
6341 : }
6342 : return IFN_LAST;
6343 : }
6344 :
6345 :
6346 : /* Function vect_setup_realignment
6347 :
6348 : This function is called when vectorizing an unaligned load using
6349 : the dr_explicit_realign[_optimized] scheme.
6350 : This function generates the following code at the loop prolog:
6351 :
6352 : p = initial_addr;
6353 : x msq_init = *(floor(p)); # prolog load
6354 : realignment_token = call target_builtin;
6355 : loop:
6356 : x msq = phi (msq_init, ---)
6357 :
6358 : The stmts marked with x are generated only for the case of
6359 : dr_explicit_realign_optimized.
6360 :
6361 : The code above sets up a new (vector) pointer, pointing to the first
6362 : location accessed by STMT_INFO, and a "floor-aligned" load using that
6363 : pointer. It also generates code to compute the "realignment-token"
6364 : (if the relevant target hook was defined), and creates a phi-node at the
6365 : loop-header bb whose arguments are the result of the prolog-load (created
6366 : by this function) and the result of a load that takes place in the loop
6367 : (to be created by the caller to this function).
6368 :
6369 : For the case of dr_explicit_realign_optimized:
6370 : The caller to this function uses the phi-result (msq) to create the
6371 : realignment code inside the loop, and sets up the missing phi argument,
6372 : as follows:
6373 : loop:
6374 : msq = phi (msq_init, lsq)
6375 : lsq = *(floor(p')); # load in loop
6376 : result = realign_load (msq, lsq, realignment_token);
6377 :
6378 : For the case of dr_explicit_realign:
6379 : loop:
6380 : msq = *(floor(p)); # load in loop
6381 : p' = p + (VS-1);
6382 : lsq = *(floor(p')); # load in loop
6383 : result = realign_load (msq, lsq, realignment_token);
6384 :
6385 : Input:
6386 : STMT_INFO - (scalar) load stmt to be vectorized. This load accesses
6387 : a memory location that may be unaligned.
6388 : BSI - place where new code is to be inserted.
6389 : ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
6390 : is used.
6391 :
6392 : Output:
6393 : REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
6394 : target hook, if defined.
6395 : Return value - the result of the loop-header phi node. */
6396 :
6397 : tree
6398 0 : vect_setup_realignment (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
6399 : gimple_stmt_iterator *gsi, tree *realignment_token,
6400 : enum dr_alignment_support alignment_support_scheme,
6401 : tree init_addr,
6402 : class loop **at_loop)
6403 : {
6404 0 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6405 0 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
6406 0 : struct data_reference *dr = dr_info->dr;
6407 0 : class loop *loop = NULL;
6408 0 : edge pe = NULL;
6409 0 : tree scalar_dest = gimple_assign_lhs (stmt_info->stmt);
6410 0 : tree vec_dest;
6411 0 : gimple *inc;
6412 0 : tree ptr;
6413 0 : tree data_ref;
6414 0 : basic_block new_bb;
6415 0 : tree msq_init = NULL_TREE;
6416 0 : tree new_temp;
6417 0 : gphi *phi_stmt;
6418 0 : tree msq = NULL_TREE;
6419 0 : gimple_seq stmts = NULL;
6420 0 : bool compute_in_loop = false;
6421 0 : bool nested_in_vect_loop = false;
6422 0 : class loop *containing_loop = (gimple_bb (stmt_info->stmt))->loop_father;
6423 0 : class loop *loop_for_initial_load = NULL;
6424 :
6425 0 : if (loop_vinfo)
6426 : {
6427 0 : loop = LOOP_VINFO_LOOP (loop_vinfo);
6428 0 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
6429 : }
6430 :
6431 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign
6432 : || alignment_support_scheme == dr_explicit_realign_optimized);
6433 :
6434 : /* We need to generate three things:
6435 : 1. the misalignment computation
6436 : 2. the extra vector load (for the optimized realignment scheme).
6437 : 3. the phi node for the two vectors from which the realignment is
6438 : done (for the optimized realignment scheme). */
6439 :
6440 : /* 1. Determine where to generate the misalignment computation.
6441 :
6442 : If INIT_ADDR is NULL_TREE, this indicates that the misalignment
6443 : calculation will be generated by this function, outside the loop (in the
6444 : preheader). Otherwise, INIT_ADDR had already been computed for us by the
6445 : caller, inside the loop.
6446 :
6447 : Background: If the misalignment remains fixed throughout the iterations of
6448 : the loop, then both realignment schemes are applicable, and also the
6449 : misalignment computation can be done outside LOOP. This is because we are
6450 : vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
6451 : are a multiple of VS (the Vector Size), and therefore the misalignment in
6452 : different vectorized LOOP iterations is always the same.
6453 : The problem arises only if the memory access is in an inner-loop nested
6454 : inside LOOP, which is now being vectorized using outer-loop vectorization.
6455 : This is the only case when the misalignment of the memory access may not
6456 : remain fixed throughout the iterations of the inner-loop (as explained in
6457 : detail in vect_supportable_dr_alignment). In this case, not only is the
6458 : optimized realignment scheme not applicable, but also the misalignment
6459 : computation (and generation of the realignment token that is passed to
6460 : REALIGN_LOAD) have to be done inside the loop.
6461 :
6462 : In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
6463 : or not, which in turn determines if the misalignment is computed inside
6464 : the inner-loop, or outside LOOP. */
6465 :
6466 0 : if (init_addr != NULL_TREE || !loop_vinfo)
6467 : {
6468 0 : compute_in_loop = true;
6469 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign);
6470 : }
6471 :
6472 :
6473 : /* 2. Determine where to generate the extra vector load.
6474 :
6475 : For the optimized realignment scheme, instead of generating two vector
6476 : loads in each iteration, we generate a single extra vector load in the
6477 : preheader of the loop, and in each iteration reuse the result of the
6478 : vector load from the previous iteration. In case the memory access is in
6479 : an inner-loop nested inside LOOP, which is now being vectorized using
6480 : outer-loop vectorization, we need to determine whether this initial vector
6481 : load should be generated at the preheader of the inner-loop, or can be
6482 : generated at the preheader of LOOP. If the memory access has no evolution
6483 : in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
6484 : to be generated inside LOOP (in the preheader of the inner-loop). */
6485 :
6486 0 : if (nested_in_vect_loop)
6487 : {
6488 0 : tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
6489 0 : bool invariant_in_outerloop =
6490 0 : (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
6491 0 : loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
6492 : }
6493 : else
6494 : loop_for_initial_load = loop;
6495 0 : if (at_loop)
6496 0 : *at_loop = loop_for_initial_load;
6497 :
6498 0 : tree vuse = NULL_TREE;
6499 0 : if (loop_for_initial_load)
6500 : {
6501 0 : pe = loop_preheader_edge (loop_for_initial_load);
6502 0 : if (gphi *vphi = get_virtual_phi (loop_for_initial_load->header))
6503 0 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
6504 : }
6505 0 : if (!vuse)
6506 0 : vuse = gimple_vuse (gsi_stmt (*gsi));
6507 :
6508 : /* 3. For the case of the optimized realignment, create the first vector
6509 : load at the loop preheader. */
6510 :
6511 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
6512 : {
6513 : /* Create msq_init = *(floor(p1)) in the loop preheader */
6514 0 : gassign *new_stmt;
6515 :
6516 0 : gcc_assert (!compute_in_loop);
6517 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6518 0 : ptr = vect_create_data_ref_ptr (vinfo, stmt_info, vectype,
6519 : loop_for_initial_load, NULL_TREE,
6520 : &init_addr, NULL, &inc, true);
6521 0 : if (TREE_CODE (ptr) == SSA_NAME)
6522 0 : new_temp = copy_ssa_name (ptr);
6523 : else
6524 0 : new_temp = make_ssa_name (TREE_TYPE (ptr));
6525 0 : poly_uint64 align = DR_TARGET_ALIGNMENT (dr_info);
6526 0 : tree type = TREE_TYPE (ptr);
6527 0 : new_stmt = gimple_build_assign
6528 0 : (new_temp, BIT_AND_EXPR, ptr,
6529 0 : fold_build2 (MINUS_EXPR, type,
6530 : build_int_cst (type, 0),
6531 : build_int_cst (type, align)));
6532 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6533 0 : gcc_assert (!new_bb);
6534 0 : data_ref
6535 0 : = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
6536 : build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
6537 0 : vect_copy_ref_info (data_ref, DR_REF (dr));
6538 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
6539 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6540 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6541 0 : gimple_set_vuse (new_stmt, vuse);
6542 0 : if (pe)
6543 : {
6544 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6545 0 : gcc_assert (!new_bb);
6546 : }
6547 : else
6548 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6549 :
6550 0 : msq_init = gimple_assign_lhs (new_stmt);
6551 : }
6552 :
6553 : /* 4. Create realignment token using a target builtin, if available.
6554 : It is done either inside the containing loop, or before LOOP (as
6555 : determined above). */
6556 :
6557 0 : if (targetm.vectorize.builtin_mask_for_load)
6558 : {
6559 0 : gcall *new_stmt;
6560 0 : tree builtin_decl;
6561 :
6562 : /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
6563 0 : if (!init_addr)
6564 : {
6565 : /* Generate the INIT_ADDR computation outside LOOP. */
6566 0 : init_addr = vect_create_addr_base_for_vector_ref (vinfo,
6567 : stmt_info, &stmts,
6568 : NULL_TREE);
6569 0 : if (loop)
6570 : {
6571 0 : pe = loop_preheader_edge (loop);
6572 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6573 0 : gcc_assert (!new_bb);
6574 : }
6575 : else
6576 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6577 : }
6578 :
6579 0 : builtin_decl = targetm.vectorize.builtin_mask_for_load ();
6580 0 : new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
6581 0 : vec_dest =
6582 0 : vect_create_destination_var (scalar_dest,
6583 : gimple_call_return_type (new_stmt));
6584 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6585 0 : gimple_call_set_lhs (new_stmt, new_temp);
6586 :
6587 0 : if (compute_in_loop)
6588 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6589 : else
6590 : {
6591 : /* Generate the misalignment computation outside LOOP. */
6592 0 : pe = loop_preheader_edge (loop);
6593 0 : new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
6594 0 : gcc_assert (!new_bb);
6595 : }
6596 :
6597 0 : *realignment_token = gimple_call_lhs (new_stmt);
6598 :
6599 : /* The result of the CALL_EXPR to this builtin is determined from
6600 : the value of the parameter and no global variables are touched
6601 : which makes the builtin a "const" function. Requiring the
6602 : builtin to have the "const" attribute makes it unnecessary
6603 : to call mark_call_clobbered. */
6604 0 : gcc_assert (TREE_READONLY (builtin_decl));
6605 : }
6606 :
6607 0 : if (alignment_support_scheme == dr_explicit_realign)
6608 : return msq;
6609 :
6610 0 : gcc_assert (!compute_in_loop);
6611 0 : gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
6612 :
6613 :
6614 : /* 5. Create msq = phi <msq_init, lsq> in loop */
6615 :
6616 0 : pe = loop_preheader_edge (containing_loop);
6617 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6618 0 : msq = make_ssa_name (vec_dest);
6619 0 : phi_stmt = create_phi_node (msq, containing_loop->header);
6620 0 : add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
6621 :
6622 0 : return msq;
6623 : }
6624 :
6625 :
6626 : /* Function vect_grouped_load_supported.
6627 :
6628 : COUNT is the size of the load group (the number of statements plus the
6629 : number of gaps). SINGLE_ELEMENT_P is true if there is actually
6630 : only one statement, with a gap of COUNT - 1.
6631 :
6632 : Returns true if a suitable permute exists. */
6633 :
6634 : bool
6635 1965 : vect_grouped_load_supported (tree vectype, bool single_element_p,
6636 : unsigned HOST_WIDE_INT count)
6637 : {
6638 1965 : machine_mode mode = TYPE_MODE (vectype);
6639 :
6640 : /* If this is single-element interleaving with an element distance
6641 : that leaves unused vector loads around punt - we at least create
6642 : very sub-optimal code in that case (and blow up memory,
6643 : see PR65518). */
6644 1965 : if (single_element_p && maybe_gt (count, TYPE_VECTOR_SUBPARTS (vectype)))
6645 : {
6646 42 : if (dump_enabled_p ())
6647 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6648 : "single-element interleaving not supported "
6649 : "for not adjacent vector loads\n");
6650 42 : return false;
6651 : }
6652 :
6653 : /* vect_permute_load_chain requires the group size to be equal to 3 or
6654 : be a power of two. */
6655 1923 : if (count != 3 && exact_log2 (count) == -1)
6656 : {
6657 226 : if (dump_enabled_p ())
6658 14 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6659 : "the size of the group of accesses"
6660 : " is not a power of 2 or not equal to 3\n");
6661 226 : return false;
6662 : }
6663 :
6664 : /* Check that the permutation is supported. */
6665 1697 : if (VECTOR_MODE_P (mode))
6666 : {
6667 1697 : unsigned int i, j;
6668 1697 : if (count == 3)
6669 : {
6670 843 : unsigned int nelt;
6671 1686 : if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6672 : {
6673 : if (dump_enabled_p ())
6674 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6675 : "cannot handle groups of 3 loads for"
6676 : " variable-length vectors\n");
6677 : return false;
6678 : }
6679 :
6680 843 : vec_perm_builder sel (nelt, nelt, 1);
6681 843 : sel.quick_grow (nelt);
6682 843 : vec_perm_indices indices;
6683 843 : unsigned int k;
6684 3336 : for (k = 0; k < 3; k++)
6685 : {
6686 8921 : for (i = 0; i < nelt; i++)
6687 6416 : if (3 * i + k < 2 * nelt)
6688 4283 : sel[i] = 3 * i + k;
6689 : else
6690 2133 : sel[i] = 0;
6691 2505 : indices.new_vector (sel, 2, nelt);
6692 2505 : if (!can_vec_perm_const_p (mode, mode, indices))
6693 : {
6694 12 : if (dump_enabled_p ())
6695 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6696 : "shuffle of 3 loads is not supported by"
6697 : " target\n");
6698 12 : return false;
6699 : }
6700 8757 : for (i = 0, j = 0; i < nelt; i++)
6701 6264 : if (3 * i + k < 2 * nelt)
6702 4176 : sel[i] = i;
6703 : else
6704 2088 : sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
6705 2493 : indices.new_vector (sel, 2, nelt);
6706 2493 : if (!can_vec_perm_const_p (mode, mode, indices))
6707 : {
6708 0 : if (dump_enabled_p ())
6709 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6710 : "shuffle of 3 loads is not supported by"
6711 : " target\n");
6712 0 : return false;
6713 : }
6714 : }
6715 : return true;
6716 843 : }
6717 : else
6718 : {
6719 : /* If length is not equal to 3 then only power of 2 is supported. */
6720 854 : gcc_assert (pow2p_hwi (count));
6721 1708 : poly_uint64 nelt = GET_MODE_NUNITS (mode);
6722 :
6723 : /* The encoding has a single stepped pattern. */
6724 854 : vec_perm_builder sel (nelt, 1, 3);
6725 854 : sel.quick_grow (3);
6726 4270 : for (i = 0; i < 3; i++)
6727 2562 : sel[i] = i * 2;
6728 854 : vec_perm_indices indices (sel, 2, nelt);
6729 854 : if (can_vec_perm_const_p (mode, mode, indices))
6730 : {
6731 3404 : for (i = 0; i < 3; i++)
6732 2553 : sel[i] = i * 2 + 1;
6733 851 : indices.new_vector (sel, 2, nelt);
6734 851 : if (can_vec_perm_const_p (mode, mode, indices))
6735 851 : return true;
6736 : }
6737 854 : }
6738 : }
6739 :
6740 3 : if (dump_enabled_p ())
6741 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6742 : "extract even/odd not supported by target\n");
6743 : return false;
6744 : }
6745 :
6746 : /* Return FN if vec_{masked_,mask_len_}load_lanes is available for COUNT vectors
6747 : of type VECTYPE. MASKED_P says whether the masked form is needed.
6748 : If it is available and ELSVALS is nonzero store the possible else values
6749 : in the vector it points to. */
6750 :
6751 : internal_fn
6752 144625 : vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count,
6753 : bool masked_p, vec<int> *elsvals)
6754 : {
6755 144625 : if (vect_lanes_optab_supported_p ("vec_mask_len_load_lanes",
6756 : vec_mask_len_load_lanes_optab, vectype,
6757 : count, elsvals))
6758 : return IFN_MASK_LEN_LOAD_LANES;
6759 144625 : else if (masked_p)
6760 : {
6761 30 : if (vect_lanes_optab_supported_p ("vec_mask_load_lanes",
6762 : vec_mask_load_lanes_optab, vectype,
6763 : count, elsvals))
6764 : return IFN_MASK_LOAD_LANES;
6765 : }
6766 : else
6767 : {
6768 144595 : if (vect_lanes_optab_supported_p ("vec_load_lanes", vec_load_lanes_optab,
6769 : vectype, count, elsvals))
6770 : return IFN_LOAD_LANES;
6771 : }
6772 : return IFN_LAST;
6773 : }
6774 :
6775 : /* Function vect_force_dr_alignment_p.
6776 :
6777 : Returns whether the alignment of a DECL can be forced to be aligned
6778 : on ALIGNMENT bit boundary. */
6779 :
6780 : bool
6781 720546 : vect_can_force_dr_alignment_p (const_tree decl, poly_uint64 alignment)
6782 : {
6783 720546 : if (!VAR_P (decl))
6784 : return false;
6785 :
6786 212759 : if (decl_in_symtab_p (decl)
6787 212759 : && (!symtab_node::get (decl)
6788 22838 : || !symtab_node::get (decl)->can_increase_alignment_p ()))
6789 14029 : return false;
6790 :
6791 198730 : if (TREE_STATIC (decl))
6792 8809 : return (known_le (alignment,
6793 8809 : (unsigned HOST_WIDE_INT) MAX_OFILE_ALIGNMENT));
6794 : else
6795 189921 : return (known_le (alignment, (unsigned HOST_WIDE_INT) MAX_STACK_ALIGNMENT));
6796 : }
6797 :
6798 : /* Return whether the data reference DR_INFO is supported with respect to its
6799 : alignment.
6800 : If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
6801 : it is aligned, i.e., check if it is possible to vectorize it with different
6802 : alignment. If IS_GATHER_SCATTER is true we are dealing with a
6803 : gather/scatter. */
6804 :
6805 : enum dr_alignment_support
6806 2906416 : vect_supportable_dr_alignment (vec_info *vinfo, dr_vec_info *dr_info,
6807 : tree vectype, int misalignment,
6808 : bool is_gather_scatter)
6809 : {
6810 2906416 : data_reference *dr = dr_info->dr;
6811 2906416 : stmt_vec_info stmt_info = dr_info->stmt;
6812 2906416 : machine_mode mode = TYPE_MODE (vectype);
6813 2906416 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6814 2906416 : class loop *vect_loop = NULL;
6815 2906416 : bool nested_in_vect_loop = false;
6816 :
6817 2906416 : if (misalignment == 0)
6818 : return dr_aligned;
6819 1781178 : else if (dr_safe_speculative_read_required (stmt_info))
6820 : return dr_unaligned_unsupported;
6821 :
6822 1370662 : if (loop_vinfo)
6823 : {
6824 960011 : vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
6825 960011 : nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt_info);
6826 : }
6827 :
6828 : /* Possibly unaligned access. */
6829 :
6830 : /* We can choose between using the implicit realignment scheme (generating
6831 : a misaligned_move stmt) and the explicit realignment scheme (generating
6832 : aligned loads with a REALIGN_LOAD). There are two variants to the
6833 : explicit realignment scheme: optimized, and unoptimized.
6834 : We can optimize the realignment only if the step between consecutive
6835 : vector loads is equal to the vector size. Since the vector memory
6836 : accesses advance in steps of VS (Vector Size) in the vectorized loop, it
6837 : is guaranteed that the misalignment amount remains the same throughout the
6838 : execution of the vectorized loop. Therefore, we can create the
6839 : "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
6840 : at the loop preheader.
6841 :
6842 : However, in the case of outer-loop vectorization, when vectorizing a
6843 : memory access in the inner-loop nested within the LOOP that is now being
6844 : vectorized, while it is guaranteed that the misalignment of the
6845 : vectorized memory access will remain the same in different outer-loop
6846 : iterations, it is *not* guaranteed that is will remain the same throughout
6847 : the execution of the inner-loop. This is because the inner-loop advances
6848 : with the original scalar step (and not in steps of VS). If the inner-loop
6849 : step happens to be a multiple of VS, then the misalignment remains fixed
6850 : and we can use the optimized realignment scheme. For example:
6851 :
6852 : for (i=0; i<N; i++)
6853 : for (j=0; j<M; j++)
6854 : s += a[i+j];
6855 :
6856 : When vectorizing the i-loop in the above example, the step between
6857 : consecutive vector loads is 1, and so the misalignment does not remain
6858 : fixed across the execution of the inner-loop, and the realignment cannot
6859 : be optimized (as illustrated in the following pseudo vectorized loop):
6860 :
6861 : for (i=0; i<N; i+=4)
6862 : for (j=0; j<M; j++){
6863 : vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
6864 : // when j is {0,1,2,3,4,5,6,7,...} respectively.
6865 : // (assuming that we start from an aligned address).
6866 : }
6867 :
6868 : We therefore have to use the unoptimized realignment scheme:
6869 :
6870 : for (i=0; i<N; i+=4)
6871 : for (j=k; j<M; j+=4)
6872 : vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
6873 : // that the misalignment of the initial address is
6874 : // 0).
6875 :
6876 : The loop can then be vectorized as follows:
6877 :
6878 : for (k=0; k<4; k++){
6879 : rt = get_realignment_token (&vp[k]);
6880 : for (i=0; i<N; i+=4){
6881 : v1 = vp[i+k];
6882 : for (j=k; j<M; j+=4){
6883 : v2 = vp[i+j+VS-1];
6884 : va = REALIGN_LOAD <v1,v2,rt>;
6885 : vs += va;
6886 : v1 = v2;
6887 : }
6888 : }
6889 : } */
6890 :
6891 1370662 : if (DR_IS_READ (dr) && !is_gather_scatter)
6892 : {
6893 614945 : if (can_implement_p (vec_realign_load_optab, mode)
6894 614945 : && (!targetm.vectorize.builtin_mask_for_load
6895 0 : || targetm.vectorize.builtin_mask_for_load ()))
6896 : {
6897 : /* If we are doing SLP then the accesses need not have the
6898 : same alignment, instead it depends on the SLP group size. */
6899 0 : if (loop_vinfo
6900 0 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
6901 0 : && !multiple_p (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6902 0 : * (DR_GROUP_SIZE
6903 0 : (DR_GROUP_FIRST_ELEMENT (stmt_info))),
6904 0 : TYPE_VECTOR_SUBPARTS (vectype)))
6905 : ;
6906 0 : else if (!loop_vinfo
6907 0 : || (nested_in_vect_loop
6908 0 : && maybe_ne (TREE_INT_CST_LOW (DR_STEP (dr)),
6909 0 : GET_MODE_SIZE (TYPE_MODE (vectype)))))
6910 0 : return dr_explicit_realign;
6911 : else
6912 0 : return dr_explicit_realign_optimized;
6913 : }
6914 : }
6915 :
6916 1370662 : bool is_packed = not_size_aligned (DR_REF (dr));
6917 1370662 : if (misalignment == DR_MISALIGNMENT_UNKNOWN
6918 1370662 : && is_gather_scatter)
6919 3192 : misalignment = (get_object_alignment (DR_REF (dr))
6920 3192 : % (GET_MODE_BITSIZE (GET_MODE_INNER (mode))))
6921 3192 : / BITS_PER_UNIT;
6922 1370662 : if (targetm.vectorize.support_vector_misalignment (mode, misalignment,
6923 : is_packed,
6924 : is_gather_scatter))
6925 : return dr_unaligned_supported;
6926 :
6927 : /* Unsupported. */
6928 : return dr_unaligned_unsupported;
6929 : }
|