* tree-ssa.c (target_for_debug_bind, verify_phi_args,
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob6cf26d9c367d6152f2c5cd8c598908d0af0005ca
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "predict.h"
31 #include "tm_p.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "cgraph.h"
35 #include "dumpfile.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "tree-eh.h"
40 #include "gimplify.h"
41 #include "gimple-iterator.h"
42 #include "gimplify-me.h"
43 #include "tree-ssa-loop-ivopts.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "tree-ssa-loop.h"
46 #include "cfgloop.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-vectorizer.h"
49 #include "expr.h"
50 #include "builtins.h"
51 #include "params.h"
53 /* Return true if load- or store-lanes optab OPTAB is implemented for
54 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
56 static bool
57 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
58 tree vectype, unsigned HOST_WIDE_INT count)
60 machine_mode mode, array_mode;
61 bool limit_p;
63 mode = TYPE_MODE (vectype);
64 limit_p = !targetm.array_mode_supported_p (mode, count);
65 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
66 MODE_INT, limit_p);
68 if (array_mode == BLKmode)
70 if (dump_enabled_p ())
71 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
72 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
73 GET_MODE_NAME (mode), count);
74 return false;
77 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
79 if (dump_enabled_p ())
80 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
81 "cannot use %s<%s><%s>\n", name,
82 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
83 return false;
86 if (dump_enabled_p ())
87 dump_printf_loc (MSG_NOTE, vect_location,
88 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
89 GET_MODE_NAME (mode));
91 return true;
95 /* Return the smallest scalar part of STMT.
96 This is used to determine the vectype of the stmt. We generally set the
97 vectype according to the type of the result (lhs). For stmts whose
98 result-type is different than the type of the arguments (e.g., demotion,
99 promotion), vectype will be reset appropriately (later). Note that we have
100 to visit the smallest datatype in this function, because that determines the
101 VF. If the smallest datatype in the loop is present only as the rhs of a
102 promotion operation - we'd miss it.
103 Such a case, where a variable of this datatype does not appear in the lhs
104 anywhere in the loop, can only occur if it's an invariant: e.g.:
105 'int_x = (int) short_inv', which we'd expect to have been optimized away by
106 invariant motion. However, we cannot rely on invariant motion to always
107 take invariants out of the loop, and so in the case of promotion we also
108 have to check the rhs.
109 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
110 types. */
112 tree
113 vect_get_smallest_scalar_type (gimple *stmt, HOST_WIDE_INT *lhs_size_unit,
114 HOST_WIDE_INT *rhs_size_unit)
116 tree scalar_type = gimple_expr_type (stmt);
117 HOST_WIDE_INT lhs, rhs;
119 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
121 if (is_gimple_assign (stmt)
122 && (gimple_assign_cast_p (stmt)
123 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
124 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
125 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
127 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
129 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
130 if (rhs < lhs)
131 scalar_type = rhs_type;
134 *lhs_size_unit = lhs;
135 *rhs_size_unit = rhs;
136 return scalar_type;
140 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
141 tested at run-time. Return TRUE if DDR was successfully inserted.
142 Return false if versioning is not supported. */
144 static bool
145 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
147 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
149 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
150 return false;
152 if (dump_enabled_p ())
154 dump_printf_loc (MSG_NOTE, vect_location,
155 "mark for run-time aliasing test between ");
156 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
157 dump_printf (MSG_NOTE, " and ");
158 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
159 dump_printf (MSG_NOTE, "\n");
162 if (optimize_loop_nest_for_size_p (loop))
164 if (dump_enabled_p ())
165 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
166 "versioning not supported when optimizing"
167 " for size.\n");
168 return false;
171 /* FORNOW: We don't support versioning with outer-loop vectorization. */
172 if (loop->inner)
174 if (dump_enabled_p ())
175 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
176 "versioning not yet supported for outer-loops.\n");
177 return false;
180 /* FORNOW: We don't support creating runtime alias tests for non-constant
181 step. */
182 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
183 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
185 if (dump_enabled_p ())
186 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
187 "versioning not yet supported for non-constant "
188 "step\n");
189 return false;
192 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
193 return true;
197 /* Function vect_analyze_data_ref_dependence.
199 Return TRUE if there (might) exist a dependence between a memory-reference
200 DRA and a memory-reference DRB. When versioning for alias may check a
201 dependence at run-time, return FALSE. Adjust *MAX_VF according to
202 the data dependence. */
204 static bool
205 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
206 loop_vec_info loop_vinfo, int *max_vf)
208 unsigned int i;
209 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
210 struct data_reference *dra = DDR_A (ddr);
211 struct data_reference *drb = DDR_B (ddr);
212 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
213 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
214 lambda_vector dist_v;
215 unsigned int loop_depth;
217 /* In loop analysis all data references should be vectorizable. */
218 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
219 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
220 gcc_unreachable ();
222 /* Independent data accesses. */
223 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
224 return false;
226 if (dra == drb
227 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
228 return false;
230 /* We do not have to consider dependences between accesses that belong
231 to the same group. */
232 if (GROUP_FIRST_ELEMENT (stmtinfo_a)
233 && GROUP_FIRST_ELEMENT (stmtinfo_a) == GROUP_FIRST_ELEMENT (stmtinfo_b))
234 return false;
236 /* Even if we have an anti-dependence then, as the vectorized loop covers at
237 least two scalar iterations, there is always also a true dependence.
238 As the vectorizer does not re-order loads and stores we can ignore
239 the anti-dependence if TBAA can disambiguate both DRs similar to the
240 case with known negative distance anti-dependences (positive
241 distance anti-dependences would violate TBAA constraints). */
242 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
243 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
244 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
245 get_alias_set (DR_REF (drb))))
246 return false;
248 /* Unknown data dependence. */
249 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
251 /* If user asserted safelen consecutive iterations can be
252 executed concurrently, assume independence. */
253 if (loop->safelen >= 2)
255 if (loop->safelen < *max_vf)
256 *max_vf = loop->safelen;
257 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
258 return false;
261 if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
262 || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
264 if (dump_enabled_p ())
266 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
267 "versioning for alias not supported for: "
268 "can't determine dependence between ");
269 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
270 DR_REF (dra));
271 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
272 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
273 DR_REF (drb));
274 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
276 return true;
279 if (dump_enabled_p ())
281 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
282 "versioning for alias required: "
283 "can't determine dependence between ");
284 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
285 DR_REF (dra));
286 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
287 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
288 DR_REF (drb));
289 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
292 /* Add to list of ddrs that need to be tested at run-time. */
293 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
296 /* Known data dependence. */
297 if (DDR_NUM_DIST_VECTS (ddr) == 0)
299 /* If user asserted safelen consecutive iterations can be
300 executed concurrently, assume independence. */
301 if (loop->safelen >= 2)
303 if (loop->safelen < *max_vf)
304 *max_vf = loop->safelen;
305 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
306 return false;
309 if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
310 || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
312 if (dump_enabled_p ())
314 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
315 "versioning for alias not supported for: "
316 "bad dist vector for ");
317 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
318 DR_REF (dra));
319 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
320 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
321 DR_REF (drb));
322 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
324 return true;
327 if (dump_enabled_p ())
329 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
330 "versioning for alias required: "
331 "bad dist vector for ");
332 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
333 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
334 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
335 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
337 /* Add to list of ddrs that need to be tested at run-time. */
338 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
341 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
342 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
344 int dist = dist_v[loop_depth];
346 if (dump_enabled_p ())
347 dump_printf_loc (MSG_NOTE, vect_location,
348 "dependence distance = %d.\n", dist);
350 if (dist == 0)
352 if (dump_enabled_p ())
354 dump_printf_loc (MSG_NOTE, vect_location,
355 "dependence distance == 0 between ");
356 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
357 dump_printf (MSG_NOTE, " and ");
358 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
359 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
362 /* When we perform grouped accesses and perform implicit CSE
363 by detecting equal accesses and doing disambiguation with
364 runtime alias tests like for
365 .. = a[i];
366 .. = a[i+1];
367 a[i] = ..;
368 a[i+1] = ..;
369 *p = ..;
370 .. = a[i];
371 .. = a[i+1];
372 where we will end up loading { a[i], a[i+1] } once, make
373 sure that inserting group loads before the first load and
374 stores after the last store will do the right thing.
375 Similar for groups like
376 a[i] = ...;
377 ... = a[i];
378 a[i+1] = ...;
379 where loads from the group interleave with the store. */
380 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
381 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
383 gimple *earlier_stmt;
384 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
385 if (DR_IS_WRITE
386 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
388 if (dump_enabled_p ())
389 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
390 "READ_WRITE dependence in interleaving."
391 "\n");
392 return true;
396 continue;
399 if (dist > 0 && DDR_REVERSED_P (ddr))
401 /* If DDR_REVERSED_P the order of the data-refs in DDR was
402 reversed (to make distance vector positive), and the actual
403 distance is negative. */
404 if (dump_enabled_p ())
405 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
406 "dependence distance negative.\n");
407 /* Record a negative dependence distance to later limit the
408 amount of stmt copying / unrolling we can perform.
409 Only need to handle read-after-write dependence. */
410 if (DR_IS_READ (drb)
411 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
412 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
413 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
414 continue;
417 if (abs (dist) >= 2
418 && abs (dist) < *max_vf)
420 /* The dependence distance requires reduction of the maximal
421 vectorization factor. */
422 *max_vf = abs (dist);
423 if (dump_enabled_p ())
424 dump_printf_loc (MSG_NOTE, vect_location,
425 "adjusting maximal vectorization factor to %i\n",
426 *max_vf);
429 if (abs (dist) >= *max_vf)
431 /* Dependence distance does not create dependence, as far as
432 vectorization is concerned, in this case. */
433 if (dump_enabled_p ())
434 dump_printf_loc (MSG_NOTE, vect_location,
435 "dependence distance >= VF.\n");
436 continue;
439 if (dump_enabled_p ())
441 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
442 "not vectorized, possible dependence "
443 "between data-refs ");
444 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
445 dump_printf (MSG_NOTE, " and ");
446 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
447 dump_printf (MSG_NOTE, "\n");
450 return true;
453 return false;
456 /* Function vect_analyze_data_ref_dependences.
458 Examine all the data references in the loop, and make sure there do not
459 exist any data dependences between them. Set *MAX_VF according to
460 the maximum vectorization factor the data dependences allow. */
462 bool
463 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
465 unsigned int i;
466 struct data_dependence_relation *ddr;
468 if (dump_enabled_p ())
469 dump_printf_loc (MSG_NOTE, vect_location,
470 "=== vect_analyze_data_ref_dependences ===\n");
472 LOOP_VINFO_DDRS (loop_vinfo)
473 .create (LOOP_VINFO_DATAREFS (loop_vinfo).length ()
474 * LOOP_VINFO_DATAREFS (loop_vinfo).length ());
475 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
476 /* We need read-read dependences to compute STMT_VINFO_SAME_ALIGN_REFS. */
477 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
478 &LOOP_VINFO_DDRS (loop_vinfo),
479 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
480 return false;
482 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
483 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
484 return false;
486 return true;
490 /* Function vect_slp_analyze_data_ref_dependence.
492 Return TRUE if there (might) exist a dependence between a memory-reference
493 DRA and a memory-reference DRB. When versioning for alias may check a
494 dependence at run-time, return FALSE. Adjust *MAX_VF according to
495 the data dependence. */
497 static bool
498 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
500 struct data_reference *dra = DDR_A (ddr);
501 struct data_reference *drb = DDR_B (ddr);
503 /* We need to check dependences of statements marked as unvectorizable
504 as well, they still can prohibit vectorization. */
506 /* Independent data accesses. */
507 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
508 return false;
510 if (dra == drb)
511 return false;
513 /* Read-read is OK. */
514 if (DR_IS_READ (dra) && DR_IS_READ (drb))
515 return false;
517 /* If dra and drb are part of the same interleaving chain consider
518 them independent. */
519 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
520 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
521 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
522 return false;
524 /* Unknown data dependence. */
525 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
527 if (dump_enabled_p ())
529 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
530 "can't determine dependence between ");
531 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
532 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
533 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
534 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
537 else if (dump_enabled_p ())
539 dump_printf_loc (MSG_NOTE, vect_location,
540 "determined dependence between ");
541 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
542 dump_printf (MSG_NOTE, " and ");
543 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
544 dump_printf (MSG_NOTE, "\n");
547 return true;
551 /* Analyze dependences involved in the transform of SLP NODE. STORES
552 contain the vector of scalar stores of this instance if we are
553 disambiguating the loads. */
555 static bool
556 vect_slp_analyze_node_dependences (slp_instance instance, slp_tree node,
557 vec<gimple *> stores, gimple *last_store)
559 /* This walks over all stmts involved in the SLP load/store done
560 in NODE verifying we can sink them up to the last stmt in the
561 group. */
562 gimple *last_access = vect_find_last_scalar_stmt_in_slp (node);
563 for (unsigned k = 0; k < SLP_INSTANCE_GROUP_SIZE (instance); ++k)
565 gimple *access = SLP_TREE_SCALAR_STMTS (node)[k];
566 if (access == last_access)
567 continue;
568 data_reference *dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (access));
569 for (gimple_stmt_iterator gsi = gsi_for_stmt (access);
570 gsi_stmt (gsi) != last_access; gsi_next (&gsi))
572 gimple *stmt = gsi_stmt (gsi);
573 if (! gimple_vuse (stmt)
574 || (DR_IS_READ (dr_a) && ! gimple_vdef (stmt)))
575 continue;
577 /* If we couldn't record a (single) data reference for this
578 stmt we have to give up. */
579 /* ??? Here and below if dependence analysis fails we can resort
580 to the alias oracle which can handle more kinds of stmts. */
581 data_reference *dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
582 if (!dr_b)
583 return false;
585 /* If we run into a store of this same instance (we've just
586 marked those) then delay dependence checking until we run
587 into the last store because this is where it will have
588 been sunk to (and we verify if we can do that as well). */
589 if (gimple_visited_p (stmt))
591 if (stmt != last_store)
592 continue;
593 unsigned i;
594 gimple *store;
595 FOR_EACH_VEC_ELT (stores, i, store)
597 data_reference *store_dr
598 = STMT_VINFO_DATA_REF (vinfo_for_stmt (store));
599 ddr_p ddr = initialize_data_dependence_relation
600 (dr_a, store_dr, vNULL);
601 if (vect_slp_analyze_data_ref_dependence (ddr))
603 free_dependence_relation (ddr);
604 return false;
606 free_dependence_relation (ddr);
610 ddr_p ddr = initialize_data_dependence_relation (dr_a, dr_b, vNULL);
611 if (vect_slp_analyze_data_ref_dependence (ddr))
613 free_dependence_relation (ddr);
614 return false;
616 free_dependence_relation (ddr);
619 return true;
623 /* Function vect_analyze_data_ref_dependences.
625 Examine all the data references in the basic-block, and make sure there
626 do not exist any data dependences between them. Set *MAX_VF according to
627 the maximum vectorization factor the data dependences allow. */
629 bool
630 vect_slp_analyze_instance_dependence (slp_instance instance)
632 if (dump_enabled_p ())
633 dump_printf_loc (MSG_NOTE, vect_location,
634 "=== vect_slp_analyze_instance_dependence ===\n");
636 /* The stores of this instance are at the root of the SLP tree. */
637 slp_tree store = SLP_INSTANCE_TREE (instance);
638 if (! STMT_VINFO_DATA_REF (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (store)[0])))
639 store = NULL;
641 /* Verify we can sink stores to the vectorized stmt insert location. */
642 gimple *last_store = NULL;
643 if (store)
645 if (! vect_slp_analyze_node_dependences (instance, store, vNULL, NULL))
646 return false;
648 /* Mark stores in this instance and remember the last one. */
649 last_store = vect_find_last_scalar_stmt_in_slp (store);
650 for (unsigned k = 0; k < SLP_INSTANCE_GROUP_SIZE (instance); ++k)
651 gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k], true);
654 bool res = true;
656 /* Verify we can sink loads to the vectorized stmt insert location,
657 special-casing stores of this instance. */
658 slp_tree load;
659 unsigned int i;
660 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, load)
661 if (! vect_slp_analyze_node_dependences (instance, load,
662 store
663 ? SLP_TREE_SCALAR_STMTS (store)
664 : vNULL, last_store))
666 res = false;
667 break;
670 /* Unset the visited flag. */
671 if (store)
672 for (unsigned k = 0; k < SLP_INSTANCE_GROUP_SIZE (instance); ++k)
673 gimple_set_visited (SLP_TREE_SCALAR_STMTS (store)[k], false);
675 return res;
678 /* Function vect_compute_data_ref_alignment
680 Compute the misalignment of the data reference DR.
682 Output:
683 1. If during the misalignment computation it is found that the data reference
684 cannot be vectorized then false is returned.
685 2. DR_MISALIGNMENT (DR) is defined.
687 FOR NOW: No analysis is actually performed. Misalignment is calculated
688 only for trivial cases. TODO. */
690 bool
691 vect_compute_data_ref_alignment (struct data_reference *dr)
693 gimple *stmt = DR_STMT (dr);
694 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
695 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
696 struct loop *loop = NULL;
697 tree ref = DR_REF (dr);
698 tree vectype;
699 tree base, base_addr;
700 tree misalign = NULL_TREE;
701 tree aligned_to;
702 tree step;
703 unsigned HOST_WIDE_INT alignment;
705 if (dump_enabled_p ())
706 dump_printf_loc (MSG_NOTE, vect_location,
707 "vect_compute_data_ref_alignment:\n");
709 if (loop_vinfo)
710 loop = LOOP_VINFO_LOOP (loop_vinfo);
712 /* Initialize misalignment to unknown. */
713 SET_DR_MISALIGNMENT (dr, -1);
715 if (tree_fits_shwi_p (DR_STEP (dr)))
716 misalign = DR_INIT (dr);
717 aligned_to = DR_ALIGNED_TO (dr);
718 base_addr = DR_BASE_ADDRESS (dr);
719 vectype = STMT_VINFO_VECTYPE (stmt_info);
721 /* In case the dataref is in an inner-loop of the loop that is being
722 vectorized (LOOP), we use the base and misalignment information
723 relative to the outer-loop (LOOP). This is ok only if the misalignment
724 stays the same throughout the execution of the inner-loop, which is why
725 we have to check that the stride of the dataref in the inner-loop evenly
726 divides by the vector size. */
727 if (loop && nested_in_vect_loop_p (loop, stmt))
729 tree step = DR_STEP (dr);
731 if (tree_fits_shwi_p (step)
732 && tree_to_shwi (step) % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
734 if (dump_enabled_p ())
735 dump_printf_loc (MSG_NOTE, vect_location,
736 "inner step divides the vector-size.\n");
737 misalign = STMT_VINFO_DR_INIT (stmt_info);
738 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
739 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
741 else
743 if (dump_enabled_p ())
744 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
745 "inner step doesn't divide the vector-size.\n");
746 misalign = NULL_TREE;
750 /* Similarly we can only use base and misalignment information relative to
751 an innermost loop if the misalignment stays the same throughout the
752 execution of the loop. As above, this is the case if the stride of
753 the dataref evenly divides by the vector size. */
754 else
756 tree step = DR_STEP (dr);
757 unsigned vf = loop ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
759 if (tree_fits_shwi_p (step)
760 && ((tree_to_shwi (step) * vf)
761 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
763 if (dump_enabled_p ())
764 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
765 "step doesn't divide the vector-size.\n");
766 misalign = NULL_TREE;
770 /* To look at alignment of the base we have to preserve an inner MEM_REF
771 as that carries alignment information of the actual access. */
772 base = ref;
773 while (handled_component_p (base))
774 base = TREE_OPERAND (base, 0);
775 if (TREE_CODE (base) == MEM_REF)
776 base = build2 (MEM_REF, TREE_TYPE (base), base_addr,
777 build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)), 0));
778 unsigned int base_alignment = get_object_alignment (base);
780 if (base_alignment >= TYPE_ALIGN (TREE_TYPE (vectype)))
781 DR_VECT_AUX (dr)->base_element_aligned = true;
783 alignment = TYPE_ALIGN_UNIT (vectype);
785 if ((compare_tree_int (aligned_to, alignment) < 0)
786 || !misalign)
788 if (dump_enabled_p ())
790 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
791 "Unknown alignment for access: ");
792 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
793 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
795 return true;
798 if (base_alignment < TYPE_ALIGN (vectype))
800 /* Strip an inner MEM_REF to a bare decl if possible. */
801 if (TREE_CODE (base) == MEM_REF
802 && integer_zerop (TREE_OPERAND (base, 1))
803 && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
804 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
806 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
808 if (dump_enabled_p ())
810 dump_printf_loc (MSG_NOTE, vect_location,
811 "can't force alignment of ref: ");
812 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
813 dump_printf (MSG_NOTE, "\n");
815 return true;
818 /* Force the alignment of the decl.
819 NOTE: This is the only change to the code we make during
820 the analysis phase, before deciding to vectorize the loop. */
821 if (dump_enabled_p ())
823 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
824 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
825 dump_printf (MSG_NOTE, "\n");
828 DR_VECT_AUX (dr)->base_decl = base;
829 DR_VECT_AUX (dr)->base_misaligned = true;
830 DR_VECT_AUX (dr)->base_element_aligned = true;
833 if (loop && nested_in_vect_loop_p (loop, stmt))
834 step = STMT_VINFO_DR_STEP (stmt_info);
835 else
836 step = DR_STEP (dr);
837 /* If this is a backward running DR then first access in the larger
838 vectype actually is N-1 elements before the address in the DR.
839 Adjust misalign accordingly. */
840 if (tree_int_cst_sgn (step) < 0)
842 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
843 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
844 otherwise we wouldn't be here. */
845 offset = fold_build2 (MULT_EXPR, ssizetype, offset, step);
846 /* PLUS because STEP was negative. */
847 misalign = size_binop (PLUS_EXPR, misalign, offset);
850 SET_DR_MISALIGNMENT (dr,
851 wi::mod_floor (misalign, alignment, SIGNED).to_uhwi ());
853 if (dump_enabled_p ())
855 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
856 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
857 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
858 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
861 return true;
865 /* Function vect_update_misalignment_for_peel
867 DR - the data reference whose misalignment is to be adjusted.
868 DR_PEEL - the data reference whose misalignment is being made
869 zero in the vector loop by the peel.
870 NPEEL - the number of iterations in the peel loop if the misalignment
871 of DR_PEEL is known at compile time. */
873 static void
874 vect_update_misalignment_for_peel (struct data_reference *dr,
875 struct data_reference *dr_peel, int npeel)
877 unsigned int i;
878 vec<dr_p> same_align_drs;
879 struct data_reference *current_dr;
880 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
881 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
882 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
883 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
885 /* For interleaved data accesses the step in the loop must be multiplied by
886 the size of the interleaving group. */
887 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
888 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
889 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
890 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
892 /* It can be assumed that the data refs with the same alignment as dr_peel
893 are aligned in the vector loop. */
894 same_align_drs
895 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
896 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
898 if (current_dr != dr)
899 continue;
900 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
901 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
902 SET_DR_MISALIGNMENT (dr, 0);
903 return;
906 if (known_alignment_for_access_p (dr)
907 && known_alignment_for_access_p (dr_peel))
909 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
910 int misal = DR_MISALIGNMENT (dr);
911 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
912 misal += negative ? -npeel * dr_size : npeel * dr_size;
913 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
914 SET_DR_MISALIGNMENT (dr, misal);
915 return;
918 if (dump_enabled_p ())
919 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
920 SET_DR_MISALIGNMENT (dr, -1);
924 /* Function verify_data_ref_alignment
926 Return TRUE if DR can be handled with respect to alignment. */
928 static bool
929 verify_data_ref_alignment (data_reference_p dr)
931 enum dr_alignment_support supportable_dr_alignment
932 = vect_supportable_dr_alignment (dr, false);
933 if (!supportable_dr_alignment)
935 if (dump_enabled_p ())
937 if (DR_IS_READ (dr))
938 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
939 "not vectorized: unsupported unaligned load.");
940 else
941 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
942 "not vectorized: unsupported unaligned "
943 "store.");
945 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
946 DR_REF (dr));
947 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
949 return false;
952 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
953 dump_printf_loc (MSG_NOTE, vect_location,
954 "Vectorizing an unaligned access.\n");
956 return true;
959 /* Function vect_verify_datarefs_alignment
961 Return TRUE if all data references in the loop can be
962 handled with respect to alignment. */
964 bool
965 vect_verify_datarefs_alignment (loop_vec_info vinfo)
967 vec<data_reference_p> datarefs = vinfo->datarefs;
968 struct data_reference *dr;
969 unsigned int i;
971 FOR_EACH_VEC_ELT (datarefs, i, dr)
973 gimple *stmt = DR_STMT (dr);
974 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
976 if (!STMT_VINFO_RELEVANT_P (stmt_info))
977 continue;
979 /* For interleaving, only the alignment of the first access matters. */
980 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
981 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
982 continue;
984 /* Strided accesses perform only component accesses, alignment is
985 irrelevant for them. */
986 if (STMT_VINFO_STRIDED_P (stmt_info)
987 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
988 continue;
990 if (! verify_data_ref_alignment (dr))
991 return false;
994 return true;
997 /* Given an memory reference EXP return whether its alignment is less
998 than its size. */
1000 static bool
1001 not_size_aligned (tree exp)
1003 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
1004 return true;
1006 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
1007 > get_object_alignment (exp));
1010 /* Function vector_alignment_reachable_p
1012 Return true if vector alignment for DR is reachable by peeling
1013 a few loop iterations. Return false otherwise. */
1015 static bool
1016 vector_alignment_reachable_p (struct data_reference *dr)
1018 gimple *stmt = DR_STMT (dr);
1019 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1020 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1022 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1024 /* For interleaved access we peel only if number of iterations in
1025 the prolog loop ({VF - misalignment}), is a multiple of the
1026 number of the interleaved accesses. */
1027 int elem_size, mis_in_elements;
1028 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1030 /* FORNOW: handle only known alignment. */
1031 if (!known_alignment_for_access_p (dr))
1032 return false;
1034 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
1035 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1037 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
1038 return false;
1041 /* If misalignment is known at the compile time then allow peeling
1042 only if natural alignment is reachable through peeling. */
1043 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1045 HOST_WIDE_INT elmsize =
1046 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1047 if (dump_enabled_p ())
1049 dump_printf_loc (MSG_NOTE, vect_location,
1050 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1051 dump_printf (MSG_NOTE,
1052 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1054 if (DR_MISALIGNMENT (dr) % elmsize)
1056 if (dump_enabled_p ())
1057 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1058 "data size does not divide the misalignment.\n");
1059 return false;
1063 if (!known_alignment_for_access_p (dr))
1065 tree type = TREE_TYPE (DR_REF (dr));
1066 bool is_packed = not_size_aligned (DR_REF (dr));
1067 if (dump_enabled_p ())
1068 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1069 "Unknown misalignment, is_packed = %d\n",is_packed);
1070 if ((TYPE_USER_ALIGN (type) && !is_packed)
1071 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1072 return true;
1073 else
1074 return false;
1077 return true;
1081 /* Calculate the cost of the memory access represented by DR. */
1083 static void
1084 vect_get_data_access_cost (struct data_reference *dr,
1085 unsigned int *inside_cost,
1086 unsigned int *outside_cost,
1087 stmt_vector_for_cost *body_cost_vec)
1089 gimple *stmt = DR_STMT (dr);
1090 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1091 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1092 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1093 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1094 int ncopies = vf / nunits;
1096 if (DR_IS_READ (dr))
1097 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1098 NULL, body_cost_vec, false);
1099 else
1100 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1102 if (dump_enabled_p ())
1103 dump_printf_loc (MSG_NOTE, vect_location,
1104 "vect_get_data_access_cost: inside_cost = %d, "
1105 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1109 typedef struct _vect_peel_info
1111 int npeel;
1112 struct data_reference *dr;
1113 unsigned int count;
1114 } *vect_peel_info;
1116 typedef struct _vect_peel_extended_info
1118 struct _vect_peel_info peel_info;
1119 unsigned int inside_cost;
1120 unsigned int outside_cost;
1121 stmt_vector_for_cost body_cost_vec;
1122 } *vect_peel_extended_info;
1125 /* Peeling hashtable helpers. */
1127 struct peel_info_hasher : free_ptr_hash <_vect_peel_info>
1129 static inline hashval_t hash (const _vect_peel_info *);
1130 static inline bool equal (const _vect_peel_info *, const _vect_peel_info *);
1133 inline hashval_t
1134 peel_info_hasher::hash (const _vect_peel_info *peel_info)
1136 return (hashval_t) peel_info->npeel;
1139 inline bool
1140 peel_info_hasher::equal (const _vect_peel_info *a, const _vect_peel_info *b)
1142 return (a->npeel == b->npeel);
1146 /* Insert DR into peeling hash table with NPEEL as key. */
1148 static void
1149 vect_peeling_hash_insert (hash_table<peel_info_hasher> *peeling_htab,
1150 loop_vec_info loop_vinfo, struct data_reference *dr,
1151 int npeel)
1153 struct _vect_peel_info elem, *slot;
1154 _vect_peel_info **new_slot;
1155 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1157 elem.npeel = npeel;
1158 slot = peeling_htab->find (&elem);
1159 if (slot)
1160 slot->count++;
1161 else
1163 slot = XNEW (struct _vect_peel_info);
1164 slot->npeel = npeel;
1165 slot->dr = dr;
1166 slot->count = 1;
1167 new_slot = peeling_htab->find_slot (slot, INSERT);
1168 *new_slot = slot;
1171 if (!supportable_dr_alignment
1172 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1173 slot->count += VECT_MAX_COST;
1177 /* Traverse peeling hash table to find peeling option that aligns maximum
1178 number of data accesses. */
1181 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1182 _vect_peel_extended_info *max)
1184 vect_peel_info elem = *slot;
1186 if (elem->count > max->peel_info.count
1187 || (elem->count == max->peel_info.count
1188 && max->peel_info.npeel > elem->npeel))
1190 max->peel_info.npeel = elem->npeel;
1191 max->peel_info.count = elem->count;
1192 max->peel_info.dr = elem->dr;
1195 return 1;
1199 /* Traverse peeling hash table and calculate cost for each peeling option.
1200 Find the one with the lowest cost. */
1203 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1204 _vect_peel_extended_info *min)
1206 vect_peel_info elem = *slot;
1207 int save_misalignment, dummy;
1208 unsigned int inside_cost = 0, outside_cost = 0, i;
1209 gimple *stmt = DR_STMT (elem->dr);
1210 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1211 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1212 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1213 struct data_reference *dr;
1214 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1216 prologue_cost_vec.create (2);
1217 body_cost_vec.create (2);
1218 epilogue_cost_vec.create (2);
1220 FOR_EACH_VEC_ELT (datarefs, i, dr)
1222 stmt = DR_STMT (dr);
1223 stmt_info = vinfo_for_stmt (stmt);
1224 /* For interleaving, only the alignment of the first access
1225 matters. */
1226 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1227 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1228 continue;
1230 /* Strided accesses perform only component accesses, alignment is
1231 irrelevant for them. */
1232 if (STMT_VINFO_STRIDED_P (stmt_info)
1233 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1234 continue;
1236 save_misalignment = DR_MISALIGNMENT (dr);
1237 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1238 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1239 &body_cost_vec);
1240 SET_DR_MISALIGNMENT (dr, save_misalignment);
1243 outside_cost += vect_get_known_peeling_cost
1244 (loop_vinfo, elem->npeel, &dummy,
1245 &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
1246 &prologue_cost_vec, &epilogue_cost_vec);
1248 /* Prologue and epilogue costs are added to the target model later.
1249 These costs depend only on the scalar iteration cost, the
1250 number of peeling iterations finally chosen, and the number of
1251 misaligned statements. So discard the information found here. */
1252 prologue_cost_vec.release ();
1253 epilogue_cost_vec.release ();
1255 if (inside_cost < min->inside_cost
1256 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1258 min->inside_cost = inside_cost;
1259 min->outside_cost = outside_cost;
1260 min->body_cost_vec.release ();
1261 min->body_cost_vec = body_cost_vec;
1262 min->peel_info.dr = elem->dr;
1263 min->peel_info.npeel = elem->npeel;
1265 else
1266 body_cost_vec.release ();
1268 return 1;
1272 /* Choose best peeling option by traversing peeling hash table and either
1273 choosing an option with the lowest cost (if cost model is enabled) or the
1274 option that aligns as many accesses as possible. */
1276 static struct data_reference *
1277 vect_peeling_hash_choose_best_peeling (hash_table<peel_info_hasher> *peeling_htab,
1278 loop_vec_info loop_vinfo,
1279 unsigned int *npeel,
1280 stmt_vector_for_cost *body_cost_vec)
1282 struct _vect_peel_extended_info res;
1284 res.peel_info.dr = NULL;
1285 res.body_cost_vec = stmt_vector_for_cost ();
1287 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1289 res.inside_cost = INT_MAX;
1290 res.outside_cost = INT_MAX;
1291 peeling_htab->traverse <_vect_peel_extended_info *,
1292 vect_peeling_hash_get_lowest_cost> (&res);
1294 else
1296 res.peel_info.count = 0;
1297 peeling_htab->traverse <_vect_peel_extended_info *,
1298 vect_peeling_hash_get_most_frequent> (&res);
1301 *npeel = res.peel_info.npeel;
1302 *body_cost_vec = res.body_cost_vec;
1303 return res.peel_info.dr;
1307 /* Function vect_enhance_data_refs_alignment
1309 This pass will use loop versioning and loop peeling in order to enhance
1310 the alignment of data references in the loop.
1312 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1313 original loop is to be vectorized. Any other loops that are created by
1314 the transformations performed in this pass - are not supposed to be
1315 vectorized. This restriction will be relaxed.
1317 This pass will require a cost model to guide it whether to apply peeling
1318 or versioning or a combination of the two. For example, the scheme that
1319 intel uses when given a loop with several memory accesses, is as follows:
1320 choose one memory access ('p') which alignment you want to force by doing
1321 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1322 other accesses are not necessarily aligned, or (2) use loop versioning to
1323 generate one loop in which all accesses are aligned, and another loop in
1324 which only 'p' is necessarily aligned.
1326 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1327 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1328 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1330 Devising a cost model is the most critical aspect of this work. It will
1331 guide us on which access to peel for, whether to use loop versioning, how
1332 many versions to create, etc. The cost model will probably consist of
1333 generic considerations as well as target specific considerations (on
1334 powerpc for example, misaligned stores are more painful than misaligned
1335 loads).
1337 Here are the general steps involved in alignment enhancements:
1339 -- original loop, before alignment analysis:
1340 for (i=0; i<N; i++){
1341 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1342 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1345 -- After vect_compute_data_refs_alignment:
1346 for (i=0; i<N; i++){
1347 x = q[i]; # DR_MISALIGNMENT(q) = 3
1348 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1351 -- Possibility 1: we do loop versioning:
1352 if (p is aligned) {
1353 for (i=0; i<N; i++){ # loop 1A
1354 x = q[i]; # DR_MISALIGNMENT(q) = 3
1355 p[i] = y; # DR_MISALIGNMENT(p) = 0
1358 else {
1359 for (i=0; i<N; i++){ # loop 1B
1360 x = q[i]; # DR_MISALIGNMENT(q) = 3
1361 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1365 -- Possibility 2: we do loop peeling:
1366 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1367 x = q[i];
1368 p[i] = y;
1370 for (i = 3; i < N; i++){ # loop 2A
1371 x = q[i]; # DR_MISALIGNMENT(q) = 0
1372 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1375 -- Possibility 3: combination of loop peeling and versioning:
1376 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1377 x = q[i];
1378 p[i] = y;
1380 if (p is aligned) {
1381 for (i = 3; i<N; i++){ # loop 3A
1382 x = q[i]; # DR_MISALIGNMENT(q) = 0
1383 p[i] = y; # DR_MISALIGNMENT(p) = 0
1386 else {
1387 for (i = 3; i<N; i++){ # loop 3B
1388 x = q[i]; # DR_MISALIGNMENT(q) = 0
1389 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1393 These loops are later passed to loop_transform to be vectorized. The
1394 vectorizer will use the alignment information to guide the transformation
1395 (whether to generate regular loads/stores, or with special handling for
1396 misalignment). */
1398 bool
1399 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1401 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1402 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1403 enum dr_alignment_support supportable_dr_alignment;
1404 struct data_reference *dr0 = NULL, *first_store = NULL;
1405 struct data_reference *dr;
1406 unsigned int i, j;
1407 bool do_peeling = false;
1408 bool do_versioning = false;
1409 bool stat;
1410 gimple *stmt;
1411 stmt_vec_info stmt_info;
1412 unsigned int npeel = 0;
1413 bool all_misalignments_unknown = true;
1414 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1415 unsigned possible_npeel_number = 1;
1416 tree vectype;
1417 unsigned int nelements, mis, same_align_drs_max = 0;
1418 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1419 hash_table<peel_info_hasher> peeling_htab (1);
1421 if (dump_enabled_p ())
1422 dump_printf_loc (MSG_NOTE, vect_location,
1423 "=== vect_enhance_data_refs_alignment ===\n");
1425 /* Reset data so we can safely be called multiple times. */
1426 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1427 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = 0;
1429 /* While cost model enhancements are expected in the future, the high level
1430 view of the code at this time is as follows:
1432 A) If there is a misaligned access then see if peeling to align
1433 this access can make all data references satisfy
1434 vect_supportable_dr_alignment. If so, update data structures
1435 as needed and return true.
1437 B) If peeling wasn't possible and there is a data reference with an
1438 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1439 then see if loop versioning checks can be used to make all data
1440 references satisfy vect_supportable_dr_alignment. If so, update
1441 data structures as needed and return true.
1443 C) If neither peeling nor versioning were successful then return false if
1444 any data reference does not satisfy vect_supportable_dr_alignment.
1446 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1448 Note, Possibility 3 above (which is peeling and versioning together) is not
1449 being done at this time. */
1451 /* (1) Peeling to force alignment. */
1453 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1454 Considerations:
1455 + How many accesses will become aligned due to the peeling
1456 - How many accesses will become unaligned due to the peeling,
1457 and the cost of misaligned accesses.
1458 - The cost of peeling (the extra runtime checks, the increase
1459 in code size). */
1461 FOR_EACH_VEC_ELT (datarefs, i, dr)
1463 stmt = DR_STMT (dr);
1464 stmt_info = vinfo_for_stmt (stmt);
1466 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1467 continue;
1469 /* For interleaving, only the alignment of the first access
1470 matters. */
1471 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1472 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1473 continue;
1475 /* For invariant accesses there is nothing to enhance. */
1476 if (integer_zerop (DR_STEP (dr)))
1477 continue;
1479 /* Strided accesses perform only component accesses, alignment is
1480 irrelevant for them. */
1481 if (STMT_VINFO_STRIDED_P (stmt_info)
1482 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1483 continue;
1485 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1486 do_peeling = vector_alignment_reachable_p (dr);
1487 if (do_peeling)
1489 if (known_alignment_for_access_p (dr))
1491 unsigned int npeel_tmp;
1492 bool negative = tree_int_cst_compare (DR_STEP (dr),
1493 size_zero_node) < 0;
1495 /* Save info about DR in the hash table. */
1496 vectype = STMT_VINFO_VECTYPE (stmt_info);
1497 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1498 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1499 TREE_TYPE (DR_REF (dr))));
1500 npeel_tmp = (negative
1501 ? (mis - nelements) : (nelements - mis))
1502 & (nelements - 1);
1504 /* For multiple types, it is possible that the bigger type access
1505 will have more than one peeling option. E.g., a loop with two
1506 types: one of size (vector size / 4), and the other one of
1507 size (vector size / 8). Vectorization factor will 8. If both
1508 access are misaligned by 3, the first one needs one scalar
1509 iteration to be aligned, and the second one needs 5. But the
1510 first one will be aligned also by peeling 5 scalar
1511 iterations, and in that case both accesses will be aligned.
1512 Hence, except for the immediate peeling amount, we also want
1513 to try to add full vector size, while we don't exceed
1514 vectorization factor.
1515 We do this automtically for cost model, since we calculate cost
1516 for every peeling option. */
1517 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1519 if (STMT_SLP_TYPE (stmt_info))
1520 possible_npeel_number
1521 = (vf * GROUP_SIZE (stmt_info)) / nelements;
1522 else
1523 possible_npeel_number = vf / nelements;
1526 /* Handle the aligned case. We may decide to align some other
1527 access, making DR unaligned. */
1528 if (DR_MISALIGNMENT (dr) == 0)
1530 npeel_tmp = 0;
1531 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1532 possible_npeel_number++;
1535 for (j = 0; j < possible_npeel_number; j++)
1537 vect_peeling_hash_insert (&peeling_htab, loop_vinfo,
1538 dr, npeel_tmp);
1539 npeel_tmp += nelements;
1542 all_misalignments_unknown = false;
1543 /* Data-ref that was chosen for the case that all the
1544 misalignments are unknown is not relevant anymore, since we
1545 have a data-ref with known alignment. */
1546 dr0 = NULL;
1548 else
1550 /* If we don't know any misalignment values, we prefer
1551 peeling for data-ref that has the maximum number of data-refs
1552 with the same alignment, unless the target prefers to align
1553 stores over load. */
1554 if (all_misalignments_unknown)
1556 unsigned same_align_drs
1557 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1558 if (!dr0
1559 || same_align_drs_max < same_align_drs)
1561 same_align_drs_max = same_align_drs;
1562 dr0 = dr;
1564 /* For data-refs with the same number of related
1565 accesses prefer the one where the misalign
1566 computation will be invariant in the outermost loop. */
1567 else if (same_align_drs_max == same_align_drs)
1569 struct loop *ivloop0, *ivloop;
1570 ivloop0 = outermost_invariant_loop_for_expr
1571 (loop, DR_BASE_ADDRESS (dr0));
1572 ivloop = outermost_invariant_loop_for_expr
1573 (loop, DR_BASE_ADDRESS (dr));
1574 if ((ivloop && !ivloop0)
1575 || (ivloop && ivloop0
1576 && flow_loop_nested_p (ivloop, ivloop0)))
1577 dr0 = dr;
1580 if (!first_store && DR_IS_WRITE (dr))
1581 first_store = dr;
1584 /* If there are both known and unknown misaligned accesses in the
1585 loop, we choose peeling amount according to the known
1586 accesses. */
1587 if (!supportable_dr_alignment)
1589 dr0 = dr;
1590 if (!first_store && DR_IS_WRITE (dr))
1591 first_store = dr;
1595 else
1597 if (!aligned_access_p (dr))
1599 if (dump_enabled_p ())
1600 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1601 "vector alignment may not be reachable\n");
1602 break;
1607 /* Check if we can possibly peel the loop. */
1608 if (!vect_can_advance_ivs_p (loop_vinfo)
1609 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop))
1610 || loop->inner)
1611 do_peeling = false;
1613 if (do_peeling
1614 && all_misalignments_unknown
1615 && vect_supportable_dr_alignment (dr0, false))
1617 /* Check if the target requires to prefer stores over loads, i.e., if
1618 misaligned stores are more expensive than misaligned loads (taking
1619 drs with same alignment into account). */
1620 if (first_store && DR_IS_READ (dr0))
1622 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1623 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1624 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1625 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1626 stmt_vector_for_cost dummy;
1627 dummy.create (2);
1629 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1630 &dummy);
1631 vect_get_data_access_cost (first_store, &store_inside_cost,
1632 &store_outside_cost, &dummy);
1634 dummy.release ();
1636 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1637 aligning the load DR0). */
1638 load_inside_penalty = store_inside_cost;
1639 load_outside_penalty = store_outside_cost;
1640 for (i = 0;
1641 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1642 DR_STMT (first_store))).iterate (i, &dr);
1643 i++)
1644 if (DR_IS_READ (dr))
1646 load_inside_penalty += load_inside_cost;
1647 load_outside_penalty += load_outside_cost;
1649 else
1651 load_inside_penalty += store_inside_cost;
1652 load_outside_penalty += store_outside_cost;
1655 /* Calculate the penalty for leaving DR0 unaligned (by
1656 aligning the FIRST_STORE). */
1657 store_inside_penalty = load_inside_cost;
1658 store_outside_penalty = load_outside_cost;
1659 for (i = 0;
1660 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1661 DR_STMT (dr0))).iterate (i, &dr);
1662 i++)
1663 if (DR_IS_READ (dr))
1665 store_inside_penalty += load_inside_cost;
1666 store_outside_penalty += load_outside_cost;
1668 else
1670 store_inside_penalty += store_inside_cost;
1671 store_outside_penalty += store_outside_cost;
1674 if (load_inside_penalty > store_inside_penalty
1675 || (load_inside_penalty == store_inside_penalty
1676 && load_outside_penalty > store_outside_penalty))
1677 dr0 = first_store;
1680 /* In case there are only loads with different unknown misalignments, use
1681 peeling only if it may help to align other accesses in the loop or
1682 if it may help improving load bandwith when we'd end up using
1683 unaligned loads. */
1684 tree dr0_vt = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr0)));
1685 if (!first_store
1686 && !STMT_VINFO_SAME_ALIGN_REFS (
1687 vinfo_for_stmt (DR_STMT (dr0))).length ()
1688 && (vect_supportable_dr_alignment (dr0, false)
1689 != dr_unaligned_supported
1690 || (builtin_vectorization_cost (vector_load, dr0_vt, 0)
1691 == builtin_vectorization_cost (unaligned_load, dr0_vt, -1))))
1692 do_peeling = false;
1695 if (do_peeling && !dr0)
1697 /* Peeling is possible, but there is no data access that is not supported
1698 unless aligned. So we try to choose the best possible peeling. */
1700 /* We should get here only if there are drs with known misalignment. */
1701 gcc_assert (!all_misalignments_unknown);
1703 /* Choose the best peeling from the hash table. */
1704 dr0 = vect_peeling_hash_choose_best_peeling (&peeling_htab,
1705 loop_vinfo, &npeel,
1706 &body_cost_vec);
1707 if (!dr0 || !npeel)
1708 do_peeling = false;
1711 if (do_peeling)
1713 stmt = DR_STMT (dr0);
1714 stmt_info = vinfo_for_stmt (stmt);
1715 vectype = STMT_VINFO_VECTYPE (stmt_info);
1716 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1718 if (known_alignment_for_access_p (dr0))
1720 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1721 size_zero_node) < 0;
1722 if (!npeel)
1724 /* Since it's known at compile time, compute the number of
1725 iterations in the peeled loop (the peeling factor) for use in
1726 updating DR_MISALIGNMENT values. The peeling factor is the
1727 vectorization factor minus the misalignment as an element
1728 count. */
1729 mis = DR_MISALIGNMENT (dr0);
1730 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1731 npeel = ((negative ? mis - nelements : nelements - mis)
1732 & (nelements - 1));
1735 /* For interleaved data access every iteration accesses all the
1736 members of the group, therefore we divide the number of iterations
1737 by the group size. */
1738 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1739 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1740 npeel /= GROUP_SIZE (stmt_info);
1742 if (dump_enabled_p ())
1743 dump_printf_loc (MSG_NOTE, vect_location,
1744 "Try peeling by %d\n", npeel);
1747 /* Ensure that all data refs can be vectorized after the peel. */
1748 FOR_EACH_VEC_ELT (datarefs, i, dr)
1750 int save_misalignment;
1752 if (dr == dr0)
1753 continue;
1755 stmt = DR_STMT (dr);
1756 stmt_info = vinfo_for_stmt (stmt);
1757 /* For interleaving, only the alignment of the first access
1758 matters. */
1759 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1760 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1761 continue;
1763 /* Strided accesses perform only component accesses, alignment is
1764 irrelevant for them. */
1765 if (STMT_VINFO_STRIDED_P (stmt_info)
1766 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1767 continue;
1769 save_misalignment = DR_MISALIGNMENT (dr);
1770 vect_update_misalignment_for_peel (dr, dr0, npeel);
1771 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1772 SET_DR_MISALIGNMENT (dr, save_misalignment);
1774 if (!supportable_dr_alignment)
1776 do_peeling = false;
1777 break;
1781 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1783 stat = vect_verify_datarefs_alignment (loop_vinfo);
1784 if (!stat)
1785 do_peeling = false;
1786 else
1788 body_cost_vec.release ();
1789 return stat;
1793 /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
1794 if (do_peeling)
1796 unsigned max_allowed_peel
1797 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1798 if (max_allowed_peel != (unsigned)-1)
1800 unsigned max_peel = npeel;
1801 if (max_peel == 0)
1803 gimple *dr_stmt = DR_STMT (dr0);
1804 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1805 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1806 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1808 if (max_peel > max_allowed_peel)
1810 do_peeling = false;
1811 if (dump_enabled_p ())
1812 dump_printf_loc (MSG_NOTE, vect_location,
1813 "Disable peeling, max peels reached: %d\n", max_peel);
1818 /* Cost model #2 - if peeling may result in a remaining loop not
1819 iterating enough to be vectorized then do not peel. */
1820 if (do_peeling
1821 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
1823 unsigned max_peel
1824 = npeel == 0 ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1 : npeel;
1825 if (LOOP_VINFO_INT_NITERS (loop_vinfo)
1826 < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + max_peel)
1827 do_peeling = false;
1830 if (do_peeling)
1832 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1833 If the misalignment of DR_i is identical to that of dr0 then set
1834 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1835 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1836 by the peeling factor times the element size of DR_i (MOD the
1837 vectorization factor times the size). Otherwise, the
1838 misalignment of DR_i must be set to unknown. */
1839 FOR_EACH_VEC_ELT (datarefs, i, dr)
1840 if (dr != dr0)
1842 /* Strided accesses perform only component accesses, alignment
1843 is irrelevant for them. */
1844 stmt_info = vinfo_for_stmt (DR_STMT (dr));
1845 if (STMT_VINFO_STRIDED_P (stmt_info)
1846 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1847 continue;
1849 vect_update_misalignment_for_peel (dr, dr0, npeel);
1852 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1853 if (npeel)
1854 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1855 else
1856 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1857 = DR_MISALIGNMENT (dr0);
1858 SET_DR_MISALIGNMENT (dr0, 0);
1859 if (dump_enabled_p ())
1861 dump_printf_loc (MSG_NOTE, vect_location,
1862 "Alignment of access forced using peeling.\n");
1863 dump_printf_loc (MSG_NOTE, vect_location,
1864 "Peeling for alignment will be applied.\n");
1866 /* The inside-loop cost will be accounted for in vectorizable_load
1867 and vectorizable_store correctly with adjusted alignments.
1868 Drop the body_cst_vec on the floor here. */
1869 body_cost_vec.release ();
1871 stat = vect_verify_datarefs_alignment (loop_vinfo);
1872 gcc_assert (stat);
1873 return stat;
1877 body_cost_vec.release ();
1879 /* (2) Versioning to force alignment. */
1881 /* Try versioning if:
1882 1) optimize loop for speed
1883 2) there is at least one unsupported misaligned data ref with an unknown
1884 misalignment, and
1885 3) all misaligned data refs with a known misalignment are supported, and
1886 4) the number of runtime alignment checks is within reason. */
1888 do_versioning =
1889 optimize_loop_nest_for_speed_p (loop)
1890 && (!loop->inner); /* FORNOW */
1892 if (do_versioning)
1894 FOR_EACH_VEC_ELT (datarefs, i, dr)
1896 stmt = DR_STMT (dr);
1897 stmt_info = vinfo_for_stmt (stmt);
1899 /* For interleaving, only the alignment of the first access
1900 matters. */
1901 if (aligned_access_p (dr)
1902 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1903 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1904 continue;
1906 if (STMT_VINFO_STRIDED_P (stmt_info))
1908 /* Strided loads perform only component accesses, alignment is
1909 irrelevant for them. */
1910 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1911 continue;
1912 do_versioning = false;
1913 break;
1916 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1918 if (!supportable_dr_alignment)
1920 gimple *stmt;
1921 int mask;
1922 tree vectype;
1924 if (known_alignment_for_access_p (dr)
1925 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1926 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1928 do_versioning = false;
1929 break;
1932 stmt = DR_STMT (dr);
1933 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1934 gcc_assert (vectype);
1936 /* The rightmost bits of an aligned address must be zeros.
1937 Construct the mask needed for this test. For example,
1938 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1939 mask must be 15 = 0xf. */
1940 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1942 /* FORNOW: use the same mask to test all potentially unaligned
1943 references in the loop. The vectorizer currently supports
1944 a single vector size, see the reference to
1945 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1946 vectorization factor is computed. */
1947 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1948 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1949 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1950 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1951 DR_STMT (dr));
1955 /* Versioning requires at least one misaligned data reference. */
1956 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1957 do_versioning = false;
1958 else if (!do_versioning)
1959 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1962 if (do_versioning)
1964 vec<gimple *> may_misalign_stmts
1965 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1966 gimple *stmt;
1968 /* It can now be assumed that the data references in the statements
1969 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1970 of the loop being vectorized. */
1971 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1973 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1974 dr = STMT_VINFO_DATA_REF (stmt_info);
1975 SET_DR_MISALIGNMENT (dr, 0);
1976 if (dump_enabled_p ())
1977 dump_printf_loc (MSG_NOTE, vect_location,
1978 "Alignment of access forced using versioning.\n");
1981 if (dump_enabled_p ())
1982 dump_printf_loc (MSG_NOTE, vect_location,
1983 "Versioning for alignment will be applied.\n");
1985 /* Peeling and versioning can't be done together at this time. */
1986 gcc_assert (! (do_peeling && do_versioning));
1988 stat = vect_verify_datarefs_alignment (loop_vinfo);
1989 gcc_assert (stat);
1990 return stat;
1993 /* This point is reached if neither peeling nor versioning is being done. */
1994 gcc_assert (! (do_peeling || do_versioning));
1996 stat = vect_verify_datarefs_alignment (loop_vinfo);
1997 return stat;
2001 /* Function vect_find_same_alignment_drs.
2003 Update group and alignment relations according to the chosen
2004 vectorization factor. */
2006 static void
2007 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
2008 loop_vec_info loop_vinfo)
2010 unsigned int i;
2011 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2012 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2013 struct data_reference *dra = DDR_A (ddr);
2014 struct data_reference *drb = DDR_B (ddr);
2015 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2016 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2017 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
2018 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
2019 lambda_vector dist_v;
2020 unsigned int loop_depth;
2022 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
2023 return;
2025 if (dra == drb)
2026 return;
2028 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
2029 return;
2031 /* Loop-based vectorization and known data dependence. */
2032 if (DDR_NUM_DIST_VECTS (ddr) == 0)
2033 return;
2035 /* Data-dependence analysis reports a distance vector of zero
2036 for data-references that overlap only in the first iteration
2037 but have different sign step (see PR45764).
2038 So as a sanity check require equal DR_STEP. */
2039 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2040 return;
2042 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
2043 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
2045 int dist = dist_v[loop_depth];
2047 if (dump_enabled_p ())
2048 dump_printf_loc (MSG_NOTE, vect_location,
2049 "dependence distance = %d.\n", dist);
2051 /* Same loop iteration. */
2052 if (dist == 0
2053 || (dist % vectorization_factor == 0 && dra_size == drb_size))
2055 /* Two references with distance zero have the same alignment. */
2056 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
2057 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
2058 if (dump_enabled_p ())
2060 dump_printf_loc (MSG_NOTE, vect_location,
2061 "accesses have the same alignment.\n");
2062 dump_printf (MSG_NOTE,
2063 "dependence distance modulo vf == 0 between ");
2064 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2065 dump_printf (MSG_NOTE, " and ");
2066 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2067 dump_printf (MSG_NOTE, "\n");
2074 /* Function vect_analyze_data_refs_alignment
2076 Analyze the alignment of the data-references in the loop.
2077 Return FALSE if a data reference is found that cannot be vectorized. */
2079 bool
2080 vect_analyze_data_refs_alignment (loop_vec_info vinfo)
2082 if (dump_enabled_p ())
2083 dump_printf_loc (MSG_NOTE, vect_location,
2084 "=== vect_analyze_data_refs_alignment ===\n");
2086 /* Mark groups of data references with same alignment using
2087 data dependence information. */
2088 vec<ddr_p> ddrs = vinfo->ddrs;
2089 struct data_dependence_relation *ddr;
2090 unsigned int i;
2092 FOR_EACH_VEC_ELT (ddrs, i, ddr)
2093 vect_find_same_alignment_drs (ddr, vinfo);
2095 vec<data_reference_p> datarefs = vinfo->datarefs;
2096 struct data_reference *dr;
2098 FOR_EACH_VEC_ELT (datarefs, i, dr)
2100 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
2101 if (STMT_VINFO_VECTORIZABLE (stmt_info)
2102 && !vect_compute_data_ref_alignment (dr))
2104 /* Strided accesses perform only component accesses, misalignment
2105 information is irrelevant for them. */
2106 if (STMT_VINFO_STRIDED_P (stmt_info)
2107 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
2108 continue;
2110 if (dump_enabled_p ())
2111 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2112 "not vectorized: can't calculate alignment "
2113 "for data ref.\n");
2115 return false;
2119 return true;
2123 /* Analyze alignment of DRs of stmts in NODE. */
2125 static bool
2126 vect_slp_analyze_and_verify_node_alignment (slp_tree node)
2128 /* We vectorize from the first scalar stmt in the node unless
2129 the node is permuted in which case we start from the first
2130 element in the group. */
2131 gimple *first_stmt = SLP_TREE_SCALAR_STMTS (node)[0];
2132 data_reference_p first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
2133 if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
2134 first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt));
2136 data_reference_p dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
2137 if (! vect_compute_data_ref_alignment (dr)
2138 /* For creating the data-ref pointer we need alignment of the
2139 first element anyway. */
2140 || (dr != first_dr
2141 && ! vect_compute_data_ref_alignment (first_dr))
2142 || ! verify_data_ref_alignment (dr))
2144 if (dump_enabled_p ())
2145 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2146 "not vectorized: bad data alignment in basic "
2147 "block.\n");
2148 return false;
2151 return true;
2154 /* Function vect_slp_analyze_instance_alignment
2156 Analyze the alignment of the data-references in the SLP instance.
2157 Return FALSE if a data reference is found that cannot be vectorized. */
2159 bool
2160 vect_slp_analyze_and_verify_instance_alignment (slp_instance instance)
2162 if (dump_enabled_p ())
2163 dump_printf_loc (MSG_NOTE, vect_location,
2164 "=== vect_slp_analyze_and_verify_instance_alignment ===\n");
2166 slp_tree node;
2167 unsigned i;
2168 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, node)
2169 if (! vect_slp_analyze_and_verify_node_alignment (node))
2170 return false;
2172 node = SLP_INSTANCE_TREE (instance);
2173 if (STMT_VINFO_DATA_REF (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]))
2174 && ! vect_slp_analyze_and_verify_node_alignment
2175 (SLP_INSTANCE_TREE (instance)))
2176 return false;
2178 return true;
2182 /* Analyze groups of accesses: check that DR belongs to a group of
2183 accesses of legal size, step, etc. Detect gaps, single element
2184 interleaving, and other special cases. Set grouped access info.
2185 Collect groups of strided stores for further use in SLP analysis.
2186 Worker for vect_analyze_group_access. */
2188 static bool
2189 vect_analyze_group_access_1 (struct data_reference *dr)
2191 tree step = DR_STEP (dr);
2192 tree scalar_type = TREE_TYPE (DR_REF (dr));
2193 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2194 gimple *stmt = DR_STMT (dr);
2195 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2196 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2197 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2198 HOST_WIDE_INT dr_step = -1;
2199 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2200 bool slp_impossible = false;
2202 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2203 size of the interleaving group (including gaps). */
2204 if (tree_fits_shwi_p (step))
2206 dr_step = tree_to_shwi (step);
2207 /* Check that STEP is a multiple of type size. Otherwise there is
2208 a non-element-sized gap at the end of the group which we
2209 cannot represent in GROUP_GAP or GROUP_SIZE.
2210 ??? As we can handle non-constant step fine here we should
2211 simply remove uses of GROUP_GAP between the last and first
2212 element and instead rely on DR_STEP. GROUP_SIZE then would
2213 simply not include that gap. */
2214 if ((dr_step % type_size) != 0)
2216 if (dump_enabled_p ())
2218 dump_printf_loc (MSG_NOTE, vect_location,
2219 "Step ");
2220 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2221 dump_printf (MSG_NOTE,
2222 " is not a multiple of the element size for ");
2223 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2224 dump_printf (MSG_NOTE, "\n");
2226 return false;
2228 groupsize = absu_hwi (dr_step) / type_size;
2230 else
2231 groupsize = 0;
2233 /* Not consecutive access is possible only if it is a part of interleaving. */
2234 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2236 /* Check if it this DR is a part of interleaving, and is a single
2237 element of the group that is accessed in the loop. */
2239 /* Gaps are supported only for loads. STEP must be a multiple of the type
2240 size. The size of the group must be a power of 2. */
2241 if (DR_IS_READ (dr)
2242 && (dr_step % type_size) == 0
2243 && groupsize > 0
2244 && pow2p_hwi (groupsize))
2246 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2247 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2248 GROUP_GAP (stmt_info) = groupsize - 1;
2249 if (dump_enabled_p ())
2251 dump_printf_loc (MSG_NOTE, vect_location,
2252 "Detected single element interleaving ");
2253 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2254 dump_printf (MSG_NOTE, " step ");
2255 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2256 dump_printf (MSG_NOTE, "\n");
2259 return true;
2262 if (dump_enabled_p ())
2264 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2265 "not consecutive access ");
2266 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2269 if (bb_vinfo)
2271 /* Mark the statement as unvectorizable. */
2272 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2273 return true;
2276 dump_printf_loc (MSG_NOTE, vect_location, "using strided accesses\n");
2277 STMT_VINFO_STRIDED_P (stmt_info) = true;
2278 return true;
2281 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2283 /* First stmt in the interleaving chain. Check the chain. */
2284 gimple *next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2285 struct data_reference *data_ref = dr;
2286 unsigned int count = 1;
2287 tree prev_init = DR_INIT (data_ref);
2288 gimple *prev = stmt;
2289 HOST_WIDE_INT diff, gaps = 0;
2291 while (next)
2293 /* Skip same data-refs. In case that two or more stmts share
2294 data-ref (supported only for loads), we vectorize only the first
2295 stmt, and the rest get their vectorized loads from the first
2296 one. */
2297 if (!tree_int_cst_compare (DR_INIT (data_ref),
2298 DR_INIT (STMT_VINFO_DATA_REF (
2299 vinfo_for_stmt (next)))))
2301 if (DR_IS_WRITE (data_ref))
2303 if (dump_enabled_p ())
2304 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2305 "Two store stmts share the same dr.\n");
2306 return false;
2309 if (dump_enabled_p ())
2310 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2311 "Two or more load stmts share the same dr.\n");
2313 /* For load use the same data-ref load. */
2314 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2316 prev = next;
2317 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2318 continue;
2321 prev = next;
2322 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2324 /* All group members have the same STEP by construction. */
2325 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2327 /* Check that the distance between two accesses is equal to the type
2328 size. Otherwise, we have gaps. */
2329 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2330 - TREE_INT_CST_LOW (prev_init)) / type_size;
2331 if (diff != 1)
2333 /* FORNOW: SLP of accesses with gaps is not supported. */
2334 slp_impossible = true;
2335 if (DR_IS_WRITE (data_ref))
2337 if (dump_enabled_p ())
2338 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2339 "interleaved store with gaps\n");
2340 return false;
2343 gaps += diff - 1;
2346 last_accessed_element += diff;
2348 /* Store the gap from the previous member of the group. If there is no
2349 gap in the access, GROUP_GAP is always 1. */
2350 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2352 prev_init = DR_INIT (data_ref);
2353 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2354 /* Count the number of data-refs in the chain. */
2355 count++;
2358 if (groupsize == 0)
2359 groupsize = count + gaps;
2361 if (groupsize > UINT_MAX)
2363 if (dump_enabled_p ())
2364 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2365 "group is too large\n");
2366 return false;
2369 /* Check that the size of the interleaving is equal to count for stores,
2370 i.e., that there are no gaps. */
2371 if (groupsize != count
2372 && !DR_IS_READ (dr))
2374 if (dump_enabled_p ())
2375 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2376 "interleaved store with gaps\n");
2377 return false;
2380 /* If there is a gap after the last load in the group it is the
2381 difference between the groupsize and the last accessed
2382 element.
2383 When there is no gap, this difference should be 0. */
2384 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - last_accessed_element;
2386 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2387 if (dump_enabled_p ())
2389 dump_printf_loc (MSG_NOTE, vect_location,
2390 "Detected interleaving ");
2391 if (DR_IS_READ (dr))
2392 dump_printf (MSG_NOTE, "load ");
2393 else
2394 dump_printf (MSG_NOTE, "store ");
2395 dump_printf (MSG_NOTE, "of size %u starting with ",
2396 (unsigned)groupsize);
2397 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2398 if (GROUP_GAP (vinfo_for_stmt (stmt)) != 0)
2399 dump_printf_loc (MSG_NOTE, vect_location,
2400 "There is a gap of %u elements after the group\n",
2401 GROUP_GAP (vinfo_for_stmt (stmt)));
2404 /* SLP: create an SLP data structure for every interleaving group of
2405 stores for further analysis in vect_analyse_slp. */
2406 if (DR_IS_WRITE (dr) && !slp_impossible)
2408 if (loop_vinfo)
2409 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2410 if (bb_vinfo)
2411 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2415 return true;
2418 /* Analyze groups of accesses: check that DR belongs to a group of
2419 accesses of legal size, step, etc. Detect gaps, single element
2420 interleaving, and other special cases. Set grouped access info.
2421 Collect groups of strided stores for further use in SLP analysis. */
2423 static bool
2424 vect_analyze_group_access (struct data_reference *dr)
2426 if (!vect_analyze_group_access_1 (dr))
2428 /* Dissolve the group if present. */
2429 gimple *next;
2430 gimple *stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dr)));
2431 while (stmt)
2433 stmt_vec_info vinfo = vinfo_for_stmt (stmt);
2434 next = GROUP_NEXT_ELEMENT (vinfo);
2435 GROUP_FIRST_ELEMENT (vinfo) = NULL;
2436 GROUP_NEXT_ELEMENT (vinfo) = NULL;
2437 stmt = next;
2439 return false;
2441 return true;
2444 /* Analyze the access pattern of the data-reference DR.
2445 In case of non-consecutive accesses call vect_analyze_group_access() to
2446 analyze groups of accesses. */
2448 static bool
2449 vect_analyze_data_ref_access (struct data_reference *dr)
2451 tree step = DR_STEP (dr);
2452 tree scalar_type = TREE_TYPE (DR_REF (dr));
2453 gimple *stmt = DR_STMT (dr);
2454 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2455 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2456 struct loop *loop = NULL;
2458 if (loop_vinfo)
2459 loop = LOOP_VINFO_LOOP (loop_vinfo);
2461 if (loop_vinfo && !step)
2463 if (dump_enabled_p ())
2464 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2465 "bad data-ref access in loop\n");
2466 return false;
2469 /* Allow loads with zero step in inner-loop vectorization. */
2470 if (loop_vinfo && integer_zerop (step))
2472 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2473 if (!nested_in_vect_loop_p (loop, stmt))
2474 return DR_IS_READ (dr);
2475 /* Allow references with zero step for outer loops marked
2476 with pragma omp simd only - it guarantees absence of
2477 loop-carried dependencies between inner loop iterations. */
2478 if (!loop->force_vectorize)
2480 if (dump_enabled_p ())
2481 dump_printf_loc (MSG_NOTE, vect_location,
2482 "zero step in inner loop of nest\n");
2483 return false;
2487 if (loop && nested_in_vect_loop_p (loop, stmt))
2489 /* Interleaved accesses are not yet supported within outer-loop
2490 vectorization for references in the inner-loop. */
2491 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2493 /* For the rest of the analysis we use the outer-loop step. */
2494 step = STMT_VINFO_DR_STEP (stmt_info);
2495 if (integer_zerop (step))
2497 if (dump_enabled_p ())
2498 dump_printf_loc (MSG_NOTE, vect_location,
2499 "zero step in outer loop.\n");
2500 return DR_IS_READ (dr);
2504 /* Consecutive? */
2505 if (TREE_CODE (step) == INTEGER_CST)
2507 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2508 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2509 || (dr_step < 0
2510 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2512 /* Mark that it is not interleaving. */
2513 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2514 return true;
2518 if (loop && nested_in_vect_loop_p (loop, stmt))
2520 if (dump_enabled_p ())
2521 dump_printf_loc (MSG_NOTE, vect_location,
2522 "grouped access in outer loop.\n");
2523 return false;
2527 /* Assume this is a DR handled by non-constant strided load case. */
2528 if (TREE_CODE (step) != INTEGER_CST)
2529 return (STMT_VINFO_STRIDED_P (stmt_info)
2530 && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
2531 || vect_analyze_group_access (dr)));
2533 /* Not consecutive access - check if it's a part of interleaving group. */
2534 return vect_analyze_group_access (dr);
2539 /* A helper function used in the comparator function to sort data
2540 references. T1 and T2 are two data references to be compared.
2541 The function returns -1, 0, or 1. */
2543 static int
2544 compare_tree (tree t1, tree t2)
2546 int i, cmp;
2547 enum tree_code code;
2548 char tclass;
2550 if (t1 == t2)
2551 return 0;
2552 if (t1 == NULL)
2553 return -1;
2554 if (t2 == NULL)
2555 return 1;
2557 STRIP_NOPS (t1);
2558 STRIP_NOPS (t2);
2560 if (TREE_CODE (t1) != TREE_CODE (t2))
2561 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2563 code = TREE_CODE (t1);
2564 switch (code)
2566 /* For const values, we can just use hash values for comparisons. */
2567 case INTEGER_CST:
2568 case REAL_CST:
2569 case FIXED_CST:
2570 case STRING_CST:
2571 case COMPLEX_CST:
2572 case VECTOR_CST:
2574 hashval_t h1 = iterative_hash_expr (t1, 0);
2575 hashval_t h2 = iterative_hash_expr (t2, 0);
2576 if (h1 != h2)
2577 return h1 < h2 ? -1 : 1;
2578 break;
2581 case SSA_NAME:
2582 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2583 if (cmp != 0)
2584 return cmp;
2586 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2587 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2588 break;
2590 default:
2591 tclass = TREE_CODE_CLASS (code);
2593 /* For var-decl, we could compare their UIDs. */
2594 if (tclass == tcc_declaration)
2596 if (DECL_UID (t1) != DECL_UID (t2))
2597 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2598 break;
2601 /* For expressions with operands, compare their operands recursively. */
2602 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2604 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2605 if (cmp != 0)
2606 return cmp;
2610 return 0;
2614 /* Compare two data-references DRA and DRB to group them into chunks
2615 suitable for grouping. */
2617 static int
2618 dr_group_sort_cmp (const void *dra_, const void *drb_)
2620 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2621 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2622 int cmp;
2624 /* Stabilize sort. */
2625 if (dra == drb)
2626 return 0;
2628 /* DRs in different loops never belong to the same group. */
2629 loop_p loopa = gimple_bb (DR_STMT (dra))->loop_father;
2630 loop_p loopb = gimple_bb (DR_STMT (drb))->loop_father;
2631 if (loopa != loopb)
2632 return loopa->num < loopb->num ? -1 : 1;
2634 /* Ordering of DRs according to base. */
2635 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2637 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2638 if (cmp != 0)
2639 return cmp;
2642 /* And according to DR_OFFSET. */
2643 if (!dr_equal_offsets_p (dra, drb))
2645 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2646 if (cmp != 0)
2647 return cmp;
2650 /* Put reads before writes. */
2651 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2652 return DR_IS_READ (dra) ? -1 : 1;
2654 /* Then sort after access size. */
2655 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2656 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2658 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2659 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2660 if (cmp != 0)
2661 return cmp;
2664 /* And after step. */
2665 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2667 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2668 if (cmp != 0)
2669 return cmp;
2672 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2673 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2674 if (cmp == 0)
2675 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2676 return cmp;
2679 /* Function vect_analyze_data_ref_accesses.
2681 Analyze the access pattern of all the data references in the loop.
2683 FORNOW: the only access pattern that is considered vectorizable is a
2684 simple step 1 (consecutive) access.
2686 FORNOW: handle only arrays and pointer accesses. */
2688 bool
2689 vect_analyze_data_ref_accesses (vec_info *vinfo)
2691 unsigned int i;
2692 vec<data_reference_p> datarefs = vinfo->datarefs;
2693 struct data_reference *dr;
2695 if (dump_enabled_p ())
2696 dump_printf_loc (MSG_NOTE, vect_location,
2697 "=== vect_analyze_data_ref_accesses ===\n");
2699 if (datarefs.is_empty ())
2700 return true;
2702 /* Sort the array of datarefs to make building the interleaving chains
2703 linear. Don't modify the original vector's order, it is needed for
2704 determining what dependencies are reversed. */
2705 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2706 datarefs_copy.qsort (dr_group_sort_cmp);
2708 /* Build the interleaving chains. */
2709 for (i = 0; i < datarefs_copy.length () - 1;)
2711 data_reference_p dra = datarefs_copy[i];
2712 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2713 stmt_vec_info lastinfo = NULL;
2714 if (! STMT_VINFO_VECTORIZABLE (stmtinfo_a))
2716 ++i;
2717 continue;
2719 for (i = i + 1; i < datarefs_copy.length (); ++i)
2721 data_reference_p drb = datarefs_copy[i];
2722 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2723 if (! STMT_VINFO_VECTORIZABLE (stmtinfo_b))
2724 break;
2726 /* ??? Imperfect sorting (non-compatible types, non-modulo
2727 accesses, same accesses) can lead to a group to be artificially
2728 split here as we don't just skip over those. If it really
2729 matters we can push those to a worklist and re-iterate
2730 over them. The we can just skip ahead to the next DR here. */
2732 /* DRs in a different loop should not be put into the same
2733 interleaving group. */
2734 if (gimple_bb (DR_STMT (dra))->loop_father
2735 != gimple_bb (DR_STMT (drb))->loop_father)
2736 break;
2738 /* Check that the data-refs have same first location (except init)
2739 and they are both either store or load (not load and store,
2740 not masked loads or stores). */
2741 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2742 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2743 DR_BASE_ADDRESS (drb), 0)
2744 || !dr_equal_offsets_p (dra, drb)
2745 || !gimple_assign_single_p (DR_STMT (dra))
2746 || !gimple_assign_single_p (DR_STMT (drb)))
2747 break;
2749 /* Check that the data-refs have the same constant size. */
2750 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2751 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2752 if (!tree_fits_uhwi_p (sza)
2753 || !tree_fits_uhwi_p (szb)
2754 || !tree_int_cst_equal (sza, szb))
2755 break;
2757 /* Check that the data-refs have the same step. */
2758 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2759 break;
2761 /* Do not place the same access in the interleaving chain twice. */
2762 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2763 break;
2765 /* Check the types are compatible.
2766 ??? We don't distinguish this during sorting. */
2767 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2768 TREE_TYPE (DR_REF (drb))))
2769 break;
2771 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2772 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2773 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2774 gcc_assert (init_a <= init_b);
2776 /* If init_b == init_a + the size of the type * k, we have an
2777 interleaving, and DRA is accessed before DRB. */
2778 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2779 if (type_size_a == 0
2780 || (init_b - init_a) % type_size_a != 0)
2781 break;
2783 /* If we have a store, the accesses are adjacent. This splits
2784 groups into chunks we support (we don't support vectorization
2785 of stores with gaps). */
2786 if (!DR_IS_READ (dra)
2787 && (init_b - (HOST_WIDE_INT) TREE_INT_CST_LOW
2788 (DR_INIT (datarefs_copy[i-1]))
2789 != type_size_a))
2790 break;
2792 /* If the step (if not zero or non-constant) is greater than the
2793 difference between data-refs' inits this splits groups into
2794 suitable sizes. */
2795 if (tree_fits_shwi_p (DR_STEP (dra)))
2797 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2798 if (step != 0 && step <= (init_b - init_a))
2799 break;
2802 if (dump_enabled_p ())
2804 dump_printf_loc (MSG_NOTE, vect_location,
2805 "Detected interleaving ");
2806 if (DR_IS_READ (dra))
2807 dump_printf (MSG_NOTE, "load ");
2808 else
2809 dump_printf (MSG_NOTE, "store ");
2810 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2811 dump_printf (MSG_NOTE, " and ");
2812 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2813 dump_printf (MSG_NOTE, "\n");
2816 /* Link the found element into the group list. */
2817 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2819 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2820 lastinfo = stmtinfo_a;
2822 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2823 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2824 lastinfo = stmtinfo_b;
2828 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2829 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2830 && !vect_analyze_data_ref_access (dr))
2832 if (dump_enabled_p ())
2833 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2834 "not vectorized: complicated access pattern.\n");
2836 if (is_a <bb_vec_info> (vinfo))
2838 /* Mark the statement as not vectorizable. */
2839 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2840 continue;
2842 else
2844 datarefs_copy.release ();
2845 return false;
2849 datarefs_copy.release ();
2850 return true;
2854 /* Operator == between two dr_with_seg_len objects.
2856 This equality operator is used to make sure two data refs
2857 are the same one so that we will consider to combine the
2858 aliasing checks of those two pairs of data dependent data
2859 refs. */
2861 static bool
2862 operator == (const dr_with_seg_len& d1,
2863 const dr_with_seg_len& d2)
2865 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2866 DR_BASE_ADDRESS (d2.dr), 0)
2867 && compare_tree (DR_OFFSET (d1.dr), DR_OFFSET (d2.dr)) == 0
2868 && compare_tree (DR_INIT (d1.dr), DR_INIT (d2.dr)) == 0
2869 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2872 /* Function comp_dr_with_seg_len_pair.
2874 Comparison function for sorting objects of dr_with_seg_len_pair_t
2875 so that we can combine aliasing checks in one scan. */
2877 static int
2878 comp_dr_with_seg_len_pair (const void *pa_, const void *pb_)
2880 const dr_with_seg_len_pair_t* pa = (const dr_with_seg_len_pair_t *) pa_;
2881 const dr_with_seg_len_pair_t* pb = (const dr_with_seg_len_pair_t *) pb_;
2882 const dr_with_seg_len &a1 = pa->first, &a2 = pa->second;
2883 const dr_with_seg_len &b1 = pb->first, &b2 = pb->second;
2885 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2886 if a and c have the same basic address snd step, and b and d have the same
2887 address and step. Therefore, if any a&c or b&d don't have the same address
2888 and step, we don't care the order of those two pairs after sorting. */
2889 int comp_res;
2891 if ((comp_res = compare_tree (DR_BASE_ADDRESS (a1.dr),
2892 DR_BASE_ADDRESS (b1.dr))) != 0)
2893 return comp_res;
2894 if ((comp_res = compare_tree (DR_BASE_ADDRESS (a2.dr),
2895 DR_BASE_ADDRESS (b2.dr))) != 0)
2896 return comp_res;
2897 if ((comp_res = compare_tree (DR_STEP (a1.dr), DR_STEP (b1.dr))) != 0)
2898 return comp_res;
2899 if ((comp_res = compare_tree (DR_STEP (a2.dr), DR_STEP (b2.dr))) != 0)
2900 return comp_res;
2901 if ((comp_res = compare_tree (DR_OFFSET (a1.dr), DR_OFFSET (b1.dr))) != 0)
2902 return comp_res;
2903 if ((comp_res = compare_tree (DR_INIT (a1.dr), DR_INIT (b1.dr))) != 0)
2904 return comp_res;
2905 if ((comp_res = compare_tree (DR_OFFSET (a2.dr), DR_OFFSET (b2.dr))) != 0)
2906 return comp_res;
2907 if ((comp_res = compare_tree (DR_INIT (a2.dr), DR_INIT (b2.dr))) != 0)
2908 return comp_res;
2910 return 0;
2913 /* Function vect_vfa_segment_size.
2915 Create an expression that computes the size of segment
2916 that will be accessed for a data reference. The functions takes into
2917 account that realignment loads may access one more vector.
2919 Input:
2920 DR: The data reference.
2921 LENGTH_FACTOR: segment length to consider.
2923 Return an expression whose value is the size of segment which will be
2924 accessed by DR. */
2926 static tree
2927 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2929 tree segment_length;
2931 if (integer_zerop (DR_STEP (dr)))
2932 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2933 else
2934 segment_length = size_binop (MULT_EXPR,
2935 fold_convert (sizetype, DR_STEP (dr)),
2936 fold_convert (sizetype, length_factor));
2938 if (vect_supportable_dr_alignment (dr, false)
2939 == dr_explicit_realign_optimized)
2941 tree vector_size = TYPE_SIZE_UNIT
2942 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2944 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2946 return segment_length;
2949 /* Function vect_no_alias_p.
2951 Given data references A and B with equal base and offset, the alias
2952 relation can be decided at compilation time, return TRUE if they do
2953 not alias to each other; return FALSE otherwise. SEGMENT_LENGTH_A
2954 and SEGMENT_LENGTH_B are the memory lengths accessed by A and B
2955 respectively. */
2957 static bool
2958 vect_no_alias_p (struct data_reference *a, struct data_reference *b,
2959 tree segment_length_a, tree segment_length_b)
2961 gcc_assert (TREE_CODE (DR_INIT (a)) == INTEGER_CST
2962 && TREE_CODE (DR_INIT (b)) == INTEGER_CST);
2963 if (tree_int_cst_equal (DR_INIT (a), DR_INIT (b)))
2964 return false;
2966 tree seg_a_min = DR_INIT (a);
2967 tree seg_a_max = fold_build2 (PLUS_EXPR, TREE_TYPE (seg_a_min),
2968 seg_a_min, segment_length_a);
2969 /* For negative step, we need to adjust address range by TYPE_SIZE_UNIT
2970 bytes, e.g., int a[3] -> a[1] range is [a+4, a+16) instead of
2971 [a, a+12) */
2972 if (tree_int_cst_compare (DR_STEP (a), size_zero_node) < 0)
2974 tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (a)));
2975 seg_a_min = fold_build2 (PLUS_EXPR, TREE_TYPE (seg_a_max),
2976 seg_a_max, unit_size);
2977 seg_a_max = fold_build2 (PLUS_EXPR, TREE_TYPE (DR_INIT (a)),
2978 DR_INIT (a), unit_size);
2980 tree seg_b_min = DR_INIT (b);
2981 tree seg_b_max = fold_build2 (PLUS_EXPR, TREE_TYPE (seg_b_min),
2982 seg_b_min, segment_length_b);
2983 if (tree_int_cst_compare (DR_STEP (b), size_zero_node) < 0)
2985 tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (b)));
2986 seg_b_min = fold_build2 (PLUS_EXPR, TREE_TYPE (seg_b_max),
2987 seg_b_max, unit_size);
2988 seg_b_max = fold_build2 (PLUS_EXPR, TREE_TYPE (DR_INIT (b)),
2989 DR_INIT (b), unit_size);
2992 if (tree_int_cst_le (seg_a_max, seg_b_min)
2993 || tree_int_cst_le (seg_b_max, seg_a_min))
2994 return true;
2996 return false;
2999 /* Function vect_prune_runtime_alias_test_list.
3001 Prune a list of ddrs to be tested at run-time by versioning for alias.
3002 Merge several alias checks into one if possible.
3003 Return FALSE if resulting list of ddrs is longer then allowed by
3004 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
3006 bool
3007 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
3009 vec<ddr_p> may_alias_ddrs =
3010 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
3011 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
3012 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
3013 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3014 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
3016 ddr_p ddr;
3017 unsigned int i;
3018 tree length_factor;
3020 if (dump_enabled_p ())
3021 dump_printf_loc (MSG_NOTE, vect_location,
3022 "=== vect_prune_runtime_alias_test_list ===\n");
3024 if (may_alias_ddrs.is_empty ())
3025 return true;
3027 /* Basically, for each pair of dependent data refs store_ptr_0
3028 and load_ptr_0, we create an expression:
3030 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
3031 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
3033 for aliasing checks. However, in some cases we can decrease
3034 the number of checks by combining two checks into one. For
3035 example, suppose we have another pair of data refs store_ptr_0
3036 and load_ptr_1, and if the following condition is satisfied:
3038 load_ptr_0 < load_ptr_1 &&
3039 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
3041 (this condition means, in each iteration of vectorized loop,
3042 the accessed memory of store_ptr_0 cannot be between the memory
3043 of load_ptr_0 and load_ptr_1.)
3045 we then can use only the following expression to finish the
3046 alising checks between store_ptr_0 & load_ptr_0 and
3047 store_ptr_0 & load_ptr_1:
3049 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
3050 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
3052 Note that we only consider that load_ptr_0 and load_ptr_1 have the
3053 same basic address. */
3055 comp_alias_ddrs.create (may_alias_ddrs.length ());
3057 /* First, we collect all data ref pairs for aliasing checks. */
3058 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
3060 int comp_res;
3061 struct data_reference *dr_a, *dr_b;
3062 gimple *dr_group_first_a, *dr_group_first_b;
3063 tree segment_length_a, segment_length_b;
3064 gimple *stmt_a, *stmt_b;
3066 dr_a = DDR_A (ddr);
3067 stmt_a = DR_STMT (DDR_A (ddr));
3068 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
3069 if (dr_group_first_a)
3071 stmt_a = dr_group_first_a;
3072 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
3075 dr_b = DDR_B (ddr);
3076 stmt_b = DR_STMT (DDR_B (ddr));
3077 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
3078 if (dr_group_first_b)
3080 stmt_b = dr_group_first_b;
3081 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
3084 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
3085 length_factor = scalar_loop_iters;
3086 else
3087 length_factor = size_int (vect_factor);
3088 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
3089 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
3091 comp_res = compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b));
3092 if (comp_res == 0)
3093 comp_res = compare_tree (DR_OFFSET (dr_a), DR_OFFSET (dr_b));
3095 /* Alias is known at compilation time. */
3096 if (comp_res == 0
3097 && TREE_CODE (DR_STEP (dr_a)) == INTEGER_CST
3098 && TREE_CODE (DR_STEP (dr_b)) == INTEGER_CST
3099 && TREE_CODE (segment_length_a) == INTEGER_CST
3100 && TREE_CODE (segment_length_b) == INTEGER_CST)
3102 if (vect_no_alias_p (dr_a, dr_b, segment_length_a, segment_length_b))
3103 continue;
3105 if (dump_enabled_p ())
3106 dump_printf_loc (MSG_NOTE, vect_location,
3107 "not vectorized: compilation time alias.\n");
3109 return false;
3112 dr_with_seg_len_pair_t dr_with_seg_len_pair
3113 (dr_with_seg_len (dr_a, segment_length_a),
3114 dr_with_seg_len (dr_b, segment_length_b));
3116 /* Canonicalize pairs by sorting the two DR members. */
3117 if (comp_res > 0)
3118 std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
3120 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
3123 /* Second, we sort the collected data ref pairs so that we can scan
3124 them once to combine all possible aliasing checks. */
3125 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
3127 /* Third, we scan the sorted dr pairs and check if we can combine
3128 alias checks of two neighboring dr pairs. */
3129 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
3131 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
3132 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
3133 *dr_b1 = &comp_alias_ddrs[i-1].second,
3134 *dr_a2 = &comp_alias_ddrs[i].first,
3135 *dr_b2 = &comp_alias_ddrs[i].second;
3137 /* Remove duplicate data ref pairs. */
3138 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
3140 if (dump_enabled_p ())
3142 dump_printf_loc (MSG_NOTE, vect_location,
3143 "found equal ranges ");
3144 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3145 DR_REF (dr_a1->dr));
3146 dump_printf (MSG_NOTE, ", ");
3147 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3148 DR_REF (dr_b1->dr));
3149 dump_printf (MSG_NOTE, " and ");
3150 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3151 DR_REF (dr_a2->dr));
3152 dump_printf (MSG_NOTE, ", ");
3153 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3154 DR_REF (dr_b2->dr));
3155 dump_printf (MSG_NOTE, "\n");
3158 comp_alias_ddrs.ordered_remove (i--);
3159 continue;
3162 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
3164 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
3165 and DR_A1 and DR_A2 are two consecutive memrefs. */
3166 if (*dr_a1 == *dr_a2)
3168 std::swap (dr_a1, dr_b1);
3169 std::swap (dr_a2, dr_b2);
3172 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
3173 DR_BASE_ADDRESS (dr_a2->dr), 0)
3174 || !operand_equal_p (DR_OFFSET (dr_a1->dr),
3175 DR_OFFSET (dr_a2->dr), 0)
3176 || !tree_fits_shwi_p (DR_INIT (dr_a1->dr))
3177 || !tree_fits_shwi_p (DR_INIT (dr_a2->dr)))
3178 continue;
3180 /* Make sure dr_a1 starts left of dr_a2. */
3181 if (tree_int_cst_lt (DR_INIT (dr_a2->dr), DR_INIT (dr_a1->dr)))
3182 std::swap (*dr_a1, *dr_a2);
3184 bool do_remove = false;
3185 unsigned HOST_WIDE_INT diff
3186 = (tree_to_shwi (DR_INIT (dr_a2->dr))
3187 - tree_to_shwi (DR_INIT (dr_a1->dr)));
3189 /* If the left segment does not extend beyond the start of the
3190 right segment the new segment length is that of the right
3191 plus the segment distance. */
3192 if (tree_fits_uhwi_p (dr_a1->seg_len)
3193 && compare_tree_int (dr_a1->seg_len, diff) <= 0)
3195 dr_a1->seg_len = size_binop (PLUS_EXPR, dr_a2->seg_len,
3196 size_int (diff));
3197 do_remove = true;
3199 /* Generally the new segment length is the maximum of the
3200 left segment size and the right segment size plus the distance.
3201 ??? We can also build tree MAX_EXPR here but it's not clear this
3202 is profitable. */
3203 else if (tree_fits_uhwi_p (dr_a1->seg_len)
3204 && tree_fits_uhwi_p (dr_a2->seg_len))
3206 unsigned HOST_WIDE_INT seg_len_a1 = tree_to_uhwi (dr_a1->seg_len);
3207 unsigned HOST_WIDE_INT seg_len_a2 = tree_to_uhwi (dr_a2->seg_len);
3208 dr_a1->seg_len = size_int (MAX (seg_len_a1, diff + seg_len_a2));
3209 do_remove = true;
3211 /* Now we check if the following condition is satisfied:
3213 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
3215 where DIFF = DR_A2_INIT - DR_A1_INIT. However,
3216 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
3217 have to make a best estimation. We can get the minimum value
3218 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
3219 then either of the following two conditions can guarantee the
3220 one above:
3222 1: DIFF <= MIN_SEG_LEN_B
3223 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B */
3224 else
3226 unsigned HOST_WIDE_INT min_seg_len_b
3227 = (tree_fits_uhwi_p (dr_b1->seg_len)
3228 ? tree_to_uhwi (dr_b1->seg_len)
3229 : vect_factor);
3231 if (diff <= min_seg_len_b
3232 || (tree_fits_uhwi_p (dr_a1->seg_len)
3233 && diff - tree_to_uhwi (dr_a1->seg_len) < min_seg_len_b))
3235 dr_a1->seg_len = size_binop (PLUS_EXPR,
3236 dr_a2->seg_len, size_int (diff));
3237 do_remove = true;
3241 if (do_remove)
3243 if (dump_enabled_p ())
3245 dump_printf_loc (MSG_NOTE, vect_location,
3246 "merging ranges for ");
3247 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_a1->dr));
3248 dump_printf (MSG_NOTE, ", ");
3249 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_b1->dr));
3250 dump_printf (MSG_NOTE, " and ");
3251 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_a2->dr));
3252 dump_printf (MSG_NOTE, ", ");
3253 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr_b2->dr));
3254 dump_printf (MSG_NOTE, "\n");
3256 comp_alias_ddrs.ordered_remove (i--);
3261 dump_printf_loc (MSG_NOTE, vect_location,
3262 "improved number of alias checks from %d to %d\n",
3263 may_alias_ddrs.length (), comp_alias_ddrs.length ());
3264 if ((int) comp_alias_ddrs.length () >
3265 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
3267 if (dump_enabled_p ())
3268 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3269 "number of versioning for alias "
3270 "run-time tests exceeds %d "
3271 "(--param vect-max-version-for-alias-checks)\n",
3272 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS));
3273 return false;
3276 /* All alias checks have been resolved at compilation time. */
3277 if (!comp_alias_ddrs.length ())
3278 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).truncate (0);
3280 return true;
3283 /* Return true if a non-affine read or write in STMT is suitable for a
3284 gather load or scatter store. Describe the operation in *INFO if so. */
3286 bool
3287 vect_check_gather_scatter (gimple *stmt, loop_vec_info loop_vinfo,
3288 gather_scatter_info *info)
3290 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
3291 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3292 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3293 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3294 tree offtype = NULL_TREE;
3295 tree decl, base, off;
3296 machine_mode pmode;
3297 int punsignedp, reversep, pvolatilep = 0;
3299 base = DR_REF (dr);
3300 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
3301 see if we can use the def stmt of the address. */
3302 if (is_gimple_call (stmt)
3303 && gimple_call_internal_p (stmt)
3304 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
3305 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
3306 && TREE_CODE (base) == MEM_REF
3307 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3308 && integer_zerop (TREE_OPERAND (base, 1))
3309 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
3311 gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
3312 if (is_gimple_assign (def_stmt)
3313 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3314 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3317 /* The gather and scatter builtins need address of the form
3318 loop_invariant + vector * {1, 2, 4, 8}
3320 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3321 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3322 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3323 multiplications and additions in it. To get a vector, we need
3324 a single SSA_NAME that will be defined in the loop and will
3325 contain everything that is not loop invariant and that can be
3326 vectorized. The following code attempts to find such a preexistng
3327 SSA_NAME OFF and put the loop invariants into a tree BASE
3328 that can be gimplified before the loop. */
3329 base = get_inner_reference (base, &pbitsize, &pbitpos, &off, &pmode,
3330 &punsignedp, &reversep, &pvolatilep);
3331 gcc_assert (base && (pbitpos % BITS_PER_UNIT) == 0 && !reversep);
3333 if (TREE_CODE (base) == MEM_REF)
3335 if (!integer_zerop (TREE_OPERAND (base, 1)))
3337 if (off == NULL_TREE)
3339 offset_int moff = mem_ref_offset (base);
3340 off = wide_int_to_tree (sizetype, moff);
3342 else
3343 off = size_binop (PLUS_EXPR, off,
3344 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3346 base = TREE_OPERAND (base, 0);
3348 else
3349 base = build_fold_addr_expr (base);
3351 if (off == NULL_TREE)
3352 off = size_zero_node;
3354 /* If base is not loop invariant, either off is 0, then we start with just
3355 the constant offset in the loop invariant BASE and continue with base
3356 as OFF, otherwise give up.
3357 We could handle that case by gimplifying the addition of base + off
3358 into some SSA_NAME and use that as off, but for now punt. */
3359 if (!expr_invariant_in_loop_p (loop, base))
3361 if (!integer_zerop (off))
3362 return false;
3363 off = base;
3364 base = size_int (pbitpos / BITS_PER_UNIT);
3366 /* Otherwise put base + constant offset into the loop invariant BASE
3367 and continue with OFF. */
3368 else
3370 base = fold_convert (sizetype, base);
3371 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3374 /* OFF at this point may be either a SSA_NAME or some tree expression
3375 from get_inner_reference. Try to peel off loop invariants from it
3376 into BASE as long as possible. */
3377 STRIP_NOPS (off);
3378 while (offtype == NULL_TREE)
3380 enum tree_code code;
3381 tree op0, op1, add = NULL_TREE;
3383 if (TREE_CODE (off) == SSA_NAME)
3385 gimple *def_stmt = SSA_NAME_DEF_STMT (off);
3387 if (expr_invariant_in_loop_p (loop, off))
3388 return false;
3390 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3391 break;
3393 op0 = gimple_assign_rhs1 (def_stmt);
3394 code = gimple_assign_rhs_code (def_stmt);
3395 op1 = gimple_assign_rhs2 (def_stmt);
3397 else
3399 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3400 return false;
3401 code = TREE_CODE (off);
3402 extract_ops_from_tree (off, &code, &op0, &op1);
3404 switch (code)
3406 case POINTER_PLUS_EXPR:
3407 case PLUS_EXPR:
3408 if (expr_invariant_in_loop_p (loop, op0))
3410 add = op0;
3411 off = op1;
3412 do_add:
3413 add = fold_convert (sizetype, add);
3414 if (scale != 1)
3415 add = size_binop (MULT_EXPR, add, size_int (scale));
3416 base = size_binop (PLUS_EXPR, base, add);
3417 continue;
3419 if (expr_invariant_in_loop_p (loop, op1))
3421 add = op1;
3422 off = op0;
3423 goto do_add;
3425 break;
3426 case MINUS_EXPR:
3427 if (expr_invariant_in_loop_p (loop, op1))
3429 add = fold_convert (sizetype, op1);
3430 add = size_binop (MINUS_EXPR, size_zero_node, add);
3431 off = op0;
3432 goto do_add;
3434 break;
3435 case MULT_EXPR:
3436 if (scale == 1 && tree_fits_shwi_p (op1))
3438 scale = tree_to_shwi (op1);
3439 off = op0;
3440 continue;
3442 break;
3443 case SSA_NAME:
3444 off = op0;
3445 continue;
3446 CASE_CONVERT:
3447 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3448 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3449 break;
3450 if (TYPE_PRECISION (TREE_TYPE (op0))
3451 == TYPE_PRECISION (TREE_TYPE (off)))
3453 off = op0;
3454 continue;
3456 if (TYPE_PRECISION (TREE_TYPE (op0))
3457 < TYPE_PRECISION (TREE_TYPE (off)))
3459 off = op0;
3460 offtype = TREE_TYPE (off);
3461 STRIP_NOPS (off);
3462 continue;
3464 break;
3465 default:
3466 break;
3468 break;
3471 /* If at the end OFF still isn't a SSA_NAME or isn't
3472 defined in the loop, punt. */
3473 if (TREE_CODE (off) != SSA_NAME
3474 || expr_invariant_in_loop_p (loop, off))
3475 return false;
3477 if (offtype == NULL_TREE)
3478 offtype = TREE_TYPE (off);
3480 if (DR_IS_READ (dr))
3481 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3482 offtype, scale);
3483 else
3484 decl = targetm.vectorize.builtin_scatter (STMT_VINFO_VECTYPE (stmt_info),
3485 offtype, scale);
3487 if (decl == NULL_TREE)
3488 return false;
3490 info->decl = decl;
3491 info->base = base;
3492 info->offset = off;
3493 info->offset_dt = vect_unknown_def_type;
3494 info->offset_vectype = NULL_TREE;
3495 info->scale = scale;
3496 return true;
3499 /* Function vect_analyze_data_refs.
3501 Find all the data references in the loop or basic block.
3503 The general structure of the analysis of data refs in the vectorizer is as
3504 follows:
3505 1- vect_analyze_data_refs(loop/bb): call
3506 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3507 in the loop/bb and their dependences.
3508 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3509 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3510 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3514 bool
3515 vect_analyze_data_refs (vec_info *vinfo, int *min_vf)
3517 struct loop *loop = NULL;
3518 unsigned int i;
3519 struct data_reference *dr;
3520 tree scalar_type;
3522 if (dump_enabled_p ())
3523 dump_printf_loc (MSG_NOTE, vect_location,
3524 "=== vect_analyze_data_refs ===\n");
3526 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
3527 loop = LOOP_VINFO_LOOP (loop_vinfo);
3529 /* Go through the data-refs, check that the analysis succeeded. Update
3530 pointer from stmt_vec_info struct to DR and vectype. */
3532 vec<data_reference_p> datarefs = vinfo->datarefs;
3533 FOR_EACH_VEC_ELT (datarefs, i, dr)
3535 gimple *stmt;
3536 stmt_vec_info stmt_info;
3537 tree base, offset, init;
3538 enum { SG_NONE, GATHER, SCATTER } gatherscatter = SG_NONE;
3539 bool simd_lane_access = false;
3540 int vf;
3542 again:
3543 if (!dr || !DR_REF (dr))
3545 if (dump_enabled_p ())
3546 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3547 "not vectorized: unhandled data-ref\n");
3548 return false;
3551 stmt = DR_STMT (dr);
3552 stmt_info = vinfo_for_stmt (stmt);
3554 /* Discard clobbers from the dataref vector. We will remove
3555 clobber stmts during vectorization. */
3556 if (gimple_clobber_p (stmt))
3558 free_data_ref (dr);
3559 if (i == datarefs.length () - 1)
3561 datarefs.pop ();
3562 break;
3564 datarefs.ordered_remove (i);
3565 dr = datarefs[i];
3566 goto again;
3569 /* Check that analysis of the data-ref succeeded. */
3570 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3571 || !DR_STEP (dr))
3573 bool maybe_gather
3574 = DR_IS_READ (dr)
3575 && !TREE_THIS_VOLATILE (DR_REF (dr))
3576 && targetm.vectorize.builtin_gather != NULL;
3577 bool maybe_scatter
3578 = DR_IS_WRITE (dr)
3579 && !TREE_THIS_VOLATILE (DR_REF (dr))
3580 && targetm.vectorize.builtin_scatter != NULL;
3581 bool maybe_simd_lane_access
3582 = is_a <loop_vec_info> (vinfo) && loop->simduid;
3584 /* If target supports vector gather loads or scatter stores, or if
3585 this might be a SIMD lane access, see if they can't be used. */
3586 if (is_a <loop_vec_info> (vinfo)
3587 && (maybe_gather || maybe_scatter || maybe_simd_lane_access)
3588 && !nested_in_vect_loop_p (loop, stmt))
3590 struct data_reference *newdr
3591 = create_data_ref (NULL, loop_containing_stmt (stmt),
3592 DR_REF (dr), stmt, maybe_scatter ? false : true);
3593 gcc_assert (newdr != NULL && DR_REF (newdr));
3594 if (DR_BASE_ADDRESS (newdr)
3595 && DR_OFFSET (newdr)
3596 && DR_INIT (newdr)
3597 && DR_STEP (newdr)
3598 && integer_zerop (DR_STEP (newdr)))
3600 if (maybe_simd_lane_access)
3602 tree off = DR_OFFSET (newdr);
3603 STRIP_NOPS (off);
3604 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3605 && TREE_CODE (off) == MULT_EXPR
3606 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3608 tree step = TREE_OPERAND (off, 1);
3609 off = TREE_OPERAND (off, 0);
3610 STRIP_NOPS (off);
3611 if (CONVERT_EXPR_P (off)
3612 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3613 0)))
3614 < TYPE_PRECISION (TREE_TYPE (off)))
3615 off = TREE_OPERAND (off, 0);
3616 if (TREE_CODE (off) == SSA_NAME)
3618 gimple *def = SSA_NAME_DEF_STMT (off);
3619 tree reft = TREE_TYPE (DR_REF (newdr));
3620 if (is_gimple_call (def)
3621 && gimple_call_internal_p (def)
3622 && (gimple_call_internal_fn (def)
3623 == IFN_GOMP_SIMD_LANE))
3625 tree arg = gimple_call_arg (def, 0);
3626 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3627 arg = SSA_NAME_VAR (arg);
3628 if (arg == loop->simduid
3629 /* For now. */
3630 && tree_int_cst_equal
3631 (TYPE_SIZE_UNIT (reft),
3632 step))
3634 DR_OFFSET (newdr) = ssize_int (0);
3635 DR_STEP (newdr) = step;
3636 DR_ALIGNED_TO (newdr)
3637 = size_int (BIGGEST_ALIGNMENT);
3638 dr = newdr;
3639 simd_lane_access = true;
3645 if (!simd_lane_access && (maybe_gather || maybe_scatter))
3647 dr = newdr;
3648 if (maybe_gather)
3649 gatherscatter = GATHER;
3650 else
3651 gatherscatter = SCATTER;
3654 if (gatherscatter == SG_NONE && !simd_lane_access)
3655 free_data_ref (newdr);
3658 if (gatherscatter == SG_NONE && !simd_lane_access)
3660 if (dump_enabled_p ())
3662 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3663 "not vectorized: data ref analysis "
3664 "failed ");
3665 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3668 if (is_a <bb_vec_info> (vinfo))
3669 break;
3671 return false;
3675 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3677 if (dump_enabled_p ())
3678 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3679 "not vectorized: base addr of dr is a "
3680 "constant\n");
3682 if (is_a <bb_vec_info> (vinfo))
3683 break;
3685 if (gatherscatter != SG_NONE || simd_lane_access)
3686 free_data_ref (dr);
3687 return false;
3690 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3692 if (dump_enabled_p ())
3694 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3695 "not vectorized: volatile type ");
3696 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3699 if (is_a <bb_vec_info> (vinfo))
3700 break;
3702 return false;
3705 if (stmt_can_throw_internal (stmt))
3707 if (dump_enabled_p ())
3709 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3710 "not vectorized: statement can throw an "
3711 "exception ");
3712 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3715 if (is_a <bb_vec_info> (vinfo))
3716 break;
3718 if (gatherscatter != SG_NONE || simd_lane_access)
3719 free_data_ref (dr);
3720 return false;
3723 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3724 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3726 if (dump_enabled_p ())
3728 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3729 "not vectorized: statement is bitfield "
3730 "access ");
3731 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3734 if (is_a <bb_vec_info> (vinfo))
3735 break;
3737 if (gatherscatter != SG_NONE || simd_lane_access)
3738 free_data_ref (dr);
3739 return false;
3742 base = unshare_expr (DR_BASE_ADDRESS (dr));
3743 offset = unshare_expr (DR_OFFSET (dr));
3744 init = unshare_expr (DR_INIT (dr));
3746 if (is_gimple_call (stmt)
3747 && (!gimple_call_internal_p (stmt)
3748 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3749 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3751 if (dump_enabled_p ())
3753 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3754 "not vectorized: dr in a call ");
3755 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3758 if (is_a <bb_vec_info> (vinfo))
3759 break;
3761 if (gatherscatter != SG_NONE || simd_lane_access)
3762 free_data_ref (dr);
3763 return false;
3766 /* Update DR field in stmt_vec_info struct. */
3768 /* If the dataref is in an inner-loop of the loop that is considered for
3769 for vectorization, we also want to analyze the access relative to
3770 the outer-loop (DR contains information only relative to the
3771 inner-most enclosing loop). We do that by building a reference to the
3772 first location accessed by the inner-loop, and analyze it relative to
3773 the outer-loop. */
3774 if (loop && nested_in_vect_loop_p (loop, stmt))
3776 tree outer_step, outer_base, outer_init;
3777 HOST_WIDE_INT pbitsize, pbitpos;
3778 tree poffset;
3779 machine_mode pmode;
3780 int punsignedp, preversep, pvolatilep;
3781 affine_iv base_iv, offset_iv;
3782 tree dinit;
3784 /* Build a reference to the first location accessed by the
3785 inner-loop: *(BASE+INIT). (The first location is actually
3786 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3787 tree inner_base = build_fold_indirect_ref
3788 (fold_build_pointer_plus (base, init));
3790 if (dump_enabled_p ())
3792 dump_printf_loc (MSG_NOTE, vect_location,
3793 "analyze in outer-loop: ");
3794 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3795 dump_printf (MSG_NOTE, "\n");
3798 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3799 &poffset, &pmode, &punsignedp,
3800 &preversep, &pvolatilep);
3801 gcc_assert (outer_base != NULL_TREE);
3803 if (pbitpos % BITS_PER_UNIT != 0)
3805 if (dump_enabled_p ())
3806 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3807 "failed: bit offset alignment.\n");
3808 return false;
3811 if (preversep)
3813 if (dump_enabled_p ())
3814 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3815 "failed: reverse storage order.\n");
3816 return false;
3819 outer_base = build_fold_addr_expr (outer_base);
3820 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3821 &base_iv, false))
3823 if (dump_enabled_p ())
3824 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3825 "failed: evolution of base is not affine.\n");
3826 return false;
3829 if (offset)
3831 if (poffset)
3832 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3833 poffset);
3834 else
3835 poffset = offset;
3838 if (!poffset)
3840 offset_iv.base = ssize_int (0);
3841 offset_iv.step = ssize_int (0);
3843 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3844 &offset_iv, false))
3846 if (dump_enabled_p ())
3847 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3848 "evolution of offset is not affine.\n");
3849 return false;
3852 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3853 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3854 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3855 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3856 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3858 outer_step = size_binop (PLUS_EXPR,
3859 fold_convert (ssizetype, base_iv.step),
3860 fold_convert (ssizetype, offset_iv.step));
3862 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3863 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3864 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3865 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3866 STMT_VINFO_DR_OFFSET (stmt_info) =
3867 fold_convert (ssizetype, offset_iv.base);
3868 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3869 size_int (highest_pow2_factor (offset_iv.base));
3871 if (dump_enabled_p ())
3873 dump_printf_loc (MSG_NOTE, vect_location,
3874 "\touter base_address: ");
3875 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3876 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3877 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3878 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3879 STMT_VINFO_DR_OFFSET (stmt_info));
3880 dump_printf (MSG_NOTE,
3881 "\n\touter constant offset from base address: ");
3882 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3883 STMT_VINFO_DR_INIT (stmt_info));
3884 dump_printf (MSG_NOTE, "\n\touter step: ");
3885 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3886 STMT_VINFO_DR_STEP (stmt_info));
3887 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3888 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3889 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3890 dump_printf (MSG_NOTE, "\n");
3894 if (STMT_VINFO_DATA_REF (stmt_info))
3896 if (dump_enabled_p ())
3898 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3899 "not vectorized: more than one data ref "
3900 "in stmt: ");
3901 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3904 if (is_a <bb_vec_info> (vinfo))
3905 break;
3907 if (gatherscatter != SG_NONE || simd_lane_access)
3908 free_data_ref (dr);
3909 return false;
3912 STMT_VINFO_DATA_REF (stmt_info) = dr;
3913 if (simd_lane_access)
3915 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3916 free_data_ref (datarefs[i]);
3917 datarefs[i] = dr;
3920 /* Set vectype for STMT. */
3921 scalar_type = TREE_TYPE (DR_REF (dr));
3922 STMT_VINFO_VECTYPE (stmt_info)
3923 = get_vectype_for_scalar_type (scalar_type);
3924 if (!STMT_VINFO_VECTYPE (stmt_info))
3926 if (dump_enabled_p ())
3928 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3929 "not vectorized: no vectype for stmt: ");
3930 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3931 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3932 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3933 scalar_type);
3934 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3937 if (is_a <bb_vec_info> (vinfo))
3939 /* No vector type is fine, the ref can still participate
3940 in dependence analysis, we just can't vectorize it. */
3941 STMT_VINFO_VECTORIZABLE (stmt_info) = false;
3942 continue;
3945 if (gatherscatter != SG_NONE || simd_lane_access)
3947 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3948 if (gatherscatter != SG_NONE)
3949 free_data_ref (dr);
3951 return false;
3953 else
3955 if (dump_enabled_p ())
3957 dump_printf_loc (MSG_NOTE, vect_location,
3958 "got vectype for stmt: ");
3959 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3960 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3961 STMT_VINFO_VECTYPE (stmt_info));
3962 dump_printf (MSG_NOTE, "\n");
3966 /* Adjust the minimal vectorization factor according to the
3967 vector type. */
3968 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3969 if (vf > *min_vf)
3970 *min_vf = vf;
3972 if (gatherscatter != SG_NONE)
3974 gather_scatter_info gs_info;
3975 if (!vect_check_gather_scatter (stmt, as_a <loop_vec_info> (vinfo),
3976 &gs_info)
3977 || !get_vectype_for_scalar_type (TREE_TYPE (gs_info.offset)))
3979 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3980 free_data_ref (dr);
3981 if (dump_enabled_p ())
3983 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3984 (gatherscatter == GATHER) ?
3985 "not vectorized: not suitable for gather "
3986 "load " :
3987 "not vectorized: not suitable for scatter "
3988 "store ");
3989 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3991 return false;
3994 free_data_ref (datarefs[i]);
3995 datarefs[i] = dr;
3996 STMT_VINFO_GATHER_SCATTER_P (stmt_info) = gatherscatter;
3999 else if (is_a <loop_vec_info> (vinfo)
4000 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
4002 if (nested_in_vect_loop_p (loop, stmt))
4004 if (dump_enabled_p ())
4006 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4007 "not vectorized: not suitable for strided "
4008 "load ");
4009 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
4011 return false;
4013 STMT_VINFO_STRIDED_P (stmt_info) = true;
4017 /* If we stopped analysis at the first dataref we could not analyze
4018 when trying to vectorize a basic-block mark the rest of the datarefs
4019 as not vectorizable and truncate the vector of datarefs. That
4020 avoids spending useless time in analyzing their dependence. */
4021 if (i != datarefs.length ())
4023 gcc_assert (is_a <bb_vec_info> (vinfo));
4024 for (unsigned j = i; j < datarefs.length (); ++j)
4026 data_reference_p dr = datarefs[j];
4027 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
4028 free_data_ref (dr);
4030 datarefs.truncate (i);
4033 return true;
4037 /* Function vect_get_new_vect_var.
4039 Returns a name for a new variable. The current naming scheme appends the
4040 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
4041 the name of vectorizer generated variables, and appends that to NAME if
4042 provided. */
4044 tree
4045 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
4047 const char *prefix;
4048 tree new_vect_var;
4050 switch (var_kind)
4052 case vect_simple_var:
4053 prefix = "vect";
4054 break;
4055 case vect_scalar_var:
4056 prefix = "stmp";
4057 break;
4058 case vect_mask_var:
4059 prefix = "mask";
4060 break;
4061 case vect_pointer_var:
4062 prefix = "vectp";
4063 break;
4064 default:
4065 gcc_unreachable ();
4068 if (name)
4070 char* tmp = concat (prefix, "_", name, NULL);
4071 new_vect_var = create_tmp_reg (type, tmp);
4072 free (tmp);
4074 else
4075 new_vect_var = create_tmp_reg (type, prefix);
4077 return new_vect_var;
4080 /* Like vect_get_new_vect_var but return an SSA name. */
4082 tree
4083 vect_get_new_ssa_name (tree type, enum vect_var_kind var_kind, const char *name)
4085 const char *prefix;
4086 tree new_vect_var;
4088 switch (var_kind)
4090 case vect_simple_var:
4091 prefix = "vect";
4092 break;
4093 case vect_scalar_var:
4094 prefix = "stmp";
4095 break;
4096 case vect_pointer_var:
4097 prefix = "vectp";
4098 break;
4099 default:
4100 gcc_unreachable ();
4103 if (name)
4105 char* tmp = concat (prefix, "_", name, NULL);
4106 new_vect_var = make_temp_ssa_name (type, NULL, tmp);
4107 free (tmp);
4109 else
4110 new_vect_var = make_temp_ssa_name (type, NULL, prefix);
4112 return new_vect_var;
4115 /* Duplicate ptr info and set alignment/misaligment on NAME from DR. */
4117 static void
4118 vect_duplicate_ssa_name_ptr_info (tree name, data_reference *dr,
4119 stmt_vec_info stmt_info)
4121 duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr));
4122 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
4123 int misalign = DR_MISALIGNMENT (dr);
4124 if (misalign == -1)
4125 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
4126 else
4127 set_ptr_info_alignment (SSA_NAME_PTR_INFO (name), align, misalign);
4130 /* Function vect_create_addr_base_for_vector_ref.
4132 Create an expression that computes the address of the first memory location
4133 that will be accessed for a data reference.
4135 Input:
4136 STMT: The statement containing the data reference.
4137 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
4138 OFFSET: Optional. If supplied, it is be added to the initial address.
4139 LOOP: Specify relative to which loop-nest should the address be computed.
4140 For example, when the dataref is in an inner-loop nested in an
4141 outer-loop that is now being vectorized, LOOP can be either the
4142 outer-loop, or the inner-loop. The first memory location accessed
4143 by the following dataref ('in' points to short):
4145 for (i=0; i<N; i++)
4146 for (j=0; j<M; j++)
4147 s += in[i+j]
4149 is as follows:
4150 if LOOP=i_loop: &in (relative to i_loop)
4151 if LOOP=j_loop: &in+i*2B (relative to j_loop)
4152 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
4153 initial address. Unlike OFFSET, which is number of elements to
4154 be added, BYTE_OFFSET is measured in bytes.
4156 Output:
4157 1. Return an SSA_NAME whose value is the address of the memory location of
4158 the first vector of the data reference.
4159 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
4160 these statement(s) which define the returned SSA_NAME.
4162 FORNOW: We are only handling array accesses with step 1. */
4164 tree
4165 vect_create_addr_base_for_vector_ref (gimple *stmt,
4166 gimple_seq *new_stmt_list,
4167 tree offset,
4168 struct loop *loop,
4169 tree byte_offset)
4171 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4172 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4173 tree data_ref_base;
4174 const char *base_name;
4175 tree addr_base;
4176 tree dest;
4177 gimple_seq seq = NULL;
4178 tree base_offset;
4179 tree init;
4180 tree vect_ptr_type;
4181 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
4182 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4184 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
4186 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
4188 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
4190 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
4191 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
4192 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
4194 else
4196 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
4197 base_offset = unshare_expr (DR_OFFSET (dr));
4198 init = unshare_expr (DR_INIT (dr));
4201 if (loop_vinfo)
4202 base_name = get_name (data_ref_base);
4203 else
4205 base_offset = ssize_int (0);
4206 init = ssize_int (0);
4207 base_name = get_name (DR_REF (dr));
4210 /* Create base_offset */
4211 base_offset = size_binop (PLUS_EXPR,
4212 fold_convert (sizetype, base_offset),
4213 fold_convert (sizetype, init));
4215 if (offset)
4217 offset = fold_build2 (MULT_EXPR, sizetype,
4218 fold_convert (sizetype, offset), step);
4219 base_offset = fold_build2 (PLUS_EXPR, sizetype,
4220 base_offset, offset);
4222 if (byte_offset)
4224 byte_offset = fold_convert (sizetype, byte_offset);
4225 base_offset = fold_build2 (PLUS_EXPR, sizetype,
4226 base_offset, byte_offset);
4229 /* base + base_offset */
4230 if (loop_vinfo)
4231 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
4232 else
4234 addr_base = build1 (ADDR_EXPR,
4235 build_pointer_type (TREE_TYPE (DR_REF (dr))),
4236 unshare_expr (DR_REF (dr)));
4239 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
4240 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
4241 addr_base = force_gimple_operand (addr_base, &seq, true, dest);
4242 gimple_seq_add_seq (new_stmt_list, seq);
4244 if (DR_PTR_INFO (dr)
4245 && TREE_CODE (addr_base) == SSA_NAME
4246 && !SSA_NAME_PTR_INFO (addr_base))
4248 vect_duplicate_ssa_name_ptr_info (addr_base, dr, stmt_info);
4249 if (offset || byte_offset)
4250 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
4253 if (dump_enabled_p ())
4255 dump_printf_loc (MSG_NOTE, vect_location, "created ");
4256 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
4257 dump_printf (MSG_NOTE, "\n");
4260 return addr_base;
4264 /* Function vect_create_data_ref_ptr.
4266 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
4267 location accessed in the loop by STMT, along with the def-use update
4268 chain to appropriately advance the pointer through the loop iterations.
4269 Also set aliasing information for the pointer. This pointer is used by
4270 the callers to this function to create a memory reference expression for
4271 vector load/store access.
4273 Input:
4274 1. STMT: a stmt that references memory. Expected to be of the form
4275 GIMPLE_ASSIGN <name, data-ref> or
4276 GIMPLE_ASSIGN <data-ref, name>.
4277 2. AGGR_TYPE: the type of the reference, which should be either a vector
4278 or an array.
4279 3. AT_LOOP: the loop where the vector memref is to be created.
4280 4. OFFSET (optional): an offset to be added to the initial address accessed
4281 by the data-ref in STMT.
4282 5. BSI: location where the new stmts are to be placed if there is no loop
4283 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4284 pointing to the initial address.
4285 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4286 to the initial address accessed by the data-ref in STMT. This is
4287 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4288 in bytes.
4290 Output:
4291 1. Declare a new ptr to vector_type, and have it point to the base of the
4292 data reference (initial addressed accessed by the data reference).
4293 For example, for vector of type V8HI, the following code is generated:
4295 v8hi *ap;
4296 ap = (v8hi *)initial_address;
4298 if OFFSET is not supplied:
4299 initial_address = &a[init];
4300 if OFFSET is supplied:
4301 initial_address = &a[init + OFFSET];
4302 if BYTE_OFFSET is supplied:
4303 initial_address = &a[init] + BYTE_OFFSET;
4305 Return the initial_address in INITIAL_ADDRESS.
4307 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4308 update the pointer in each iteration of the loop.
4310 Return the increment stmt that updates the pointer in PTR_INCR.
4312 3. Set INV_P to true if the access pattern of the data reference in the
4313 vectorized loop is invariant. Set it to false otherwise.
4315 4. Return the pointer. */
4317 tree
4318 vect_create_data_ref_ptr (gimple *stmt, tree aggr_type, struct loop *at_loop,
4319 tree offset, tree *initial_address,
4320 gimple_stmt_iterator *gsi, gimple **ptr_incr,
4321 bool only_init, bool *inv_p, tree byte_offset)
4323 const char *base_name;
4324 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4325 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4326 struct loop *loop = NULL;
4327 bool nested_in_vect_loop = false;
4328 struct loop *containing_loop = NULL;
4329 tree aggr_ptr_type;
4330 tree aggr_ptr;
4331 tree new_temp;
4332 gimple_seq new_stmt_list = NULL;
4333 edge pe = NULL;
4334 basic_block new_bb;
4335 tree aggr_ptr_init;
4336 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4337 tree aptr;
4338 gimple_stmt_iterator incr_gsi;
4339 bool insert_after;
4340 tree indx_before_incr, indx_after_incr;
4341 gimple *incr;
4342 tree step;
4343 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4345 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4346 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4348 if (loop_vinfo)
4350 loop = LOOP_VINFO_LOOP (loop_vinfo);
4351 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4352 containing_loop = (gimple_bb (stmt))->loop_father;
4353 pe = loop_preheader_edge (loop);
4355 else
4357 gcc_assert (bb_vinfo);
4358 only_init = true;
4359 *ptr_incr = NULL;
4362 /* Check the step (evolution) of the load in LOOP, and record
4363 whether it's invariant. */
4364 if (nested_in_vect_loop)
4365 step = STMT_VINFO_DR_STEP (stmt_info);
4366 else
4367 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4369 if (integer_zerop (step))
4370 *inv_p = true;
4371 else
4372 *inv_p = false;
4374 /* Create an expression for the first address accessed by this load
4375 in LOOP. */
4376 base_name = get_name (DR_BASE_ADDRESS (dr));
4378 if (dump_enabled_p ())
4380 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4381 dump_printf_loc (MSG_NOTE, vect_location,
4382 "create %s-pointer variable to type: ",
4383 get_tree_code_name (TREE_CODE (aggr_type)));
4384 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4385 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4386 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4387 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4388 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4389 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4390 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4391 else
4392 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4393 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4394 dump_printf (MSG_NOTE, "\n");
4397 /* (1) Create the new aggregate-pointer variable.
4398 Vector and array types inherit the alias set of their component
4399 type by default so we need to use a ref-all pointer if the data
4400 reference does not conflict with the created aggregated data
4401 reference because it is not addressable. */
4402 bool need_ref_all = false;
4403 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4404 get_alias_set (DR_REF (dr))))
4405 need_ref_all = true;
4406 /* Likewise for any of the data references in the stmt group. */
4407 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4409 gimple *orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4412 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4413 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4414 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4415 get_alias_set (DR_REF (sdr))))
4417 need_ref_all = true;
4418 break;
4420 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4422 while (orig_stmt);
4424 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4425 need_ref_all);
4426 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4429 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4430 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4431 def-use update cycles for the pointer: one relative to the outer-loop
4432 (LOOP), which is what steps (3) and (4) below do. The other is relative
4433 to the inner-loop (which is the inner-most loop containing the dataref),
4434 and this is done be step (5) below.
4436 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4437 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4438 redundant. Steps (3),(4) create the following:
4440 vp0 = &base_addr;
4441 LOOP: vp1 = phi(vp0,vp2)
4444 vp2 = vp1 + step
4445 goto LOOP
4447 If there is an inner-loop nested in loop, then step (5) will also be
4448 applied, and an additional update in the inner-loop will be created:
4450 vp0 = &base_addr;
4451 LOOP: vp1 = phi(vp0,vp2)
4453 inner: vp3 = phi(vp1,vp4)
4454 vp4 = vp3 + inner_step
4455 if () goto inner
4457 vp2 = vp1 + step
4458 if () goto LOOP */
4460 /* (2) Calculate the initial address of the aggregate-pointer, and set
4461 the aggregate-pointer to point to it before the loop. */
4463 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4465 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4466 offset, loop, byte_offset);
4467 if (new_stmt_list)
4469 if (pe)
4471 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4472 gcc_assert (!new_bb);
4474 else
4475 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4478 *initial_address = new_temp;
4479 aggr_ptr_init = new_temp;
4481 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4482 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4483 inner-loop nested in LOOP (during outer-loop vectorization). */
4485 /* No update in loop is required. */
4486 if (only_init && (!loop_vinfo || at_loop == loop))
4487 aptr = aggr_ptr_init;
4488 else
4490 /* The step of the aggregate pointer is the type size. */
4491 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4492 /* One exception to the above is when the scalar step of the load in
4493 LOOP is zero. In this case the step here is also zero. */
4494 if (*inv_p)
4495 iv_step = size_zero_node;
4496 else if (tree_int_cst_sgn (step) == -1)
4497 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4499 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4501 create_iv (aggr_ptr_init,
4502 fold_convert (aggr_ptr_type, iv_step),
4503 aggr_ptr, loop, &incr_gsi, insert_after,
4504 &indx_before_incr, &indx_after_incr);
4505 incr = gsi_stmt (incr_gsi);
4506 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
4508 /* Copy the points-to information if it exists. */
4509 if (DR_PTR_INFO (dr))
4511 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4512 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4514 if (ptr_incr)
4515 *ptr_incr = incr;
4517 aptr = indx_before_incr;
4520 if (!nested_in_vect_loop || only_init)
4521 return aptr;
4524 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4525 nested in LOOP, if exists. */
4527 gcc_assert (nested_in_vect_loop);
4528 if (!only_init)
4530 standard_iv_increment_position (containing_loop, &incr_gsi,
4531 &insert_after);
4532 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4533 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4534 &indx_after_incr);
4535 incr = gsi_stmt (incr_gsi);
4536 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
4538 /* Copy the points-to information if it exists. */
4539 if (DR_PTR_INFO (dr))
4541 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4542 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4544 if (ptr_incr)
4545 *ptr_incr = incr;
4547 return indx_before_incr;
4549 else
4550 gcc_unreachable ();
4554 /* Function bump_vector_ptr
4556 Increment a pointer (to a vector type) by vector-size. If requested,
4557 i.e. if PTR-INCR is given, then also connect the new increment stmt
4558 to the existing def-use update-chain of the pointer, by modifying
4559 the PTR_INCR as illustrated below:
4561 The pointer def-use update-chain before this function:
4562 DATAREF_PTR = phi (p_0, p_2)
4563 ....
4564 PTR_INCR: p_2 = DATAREF_PTR + step
4566 The pointer def-use update-chain after this function:
4567 DATAREF_PTR = phi (p_0, p_2)
4568 ....
4569 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4570 ....
4571 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4573 Input:
4574 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4575 in the loop.
4576 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4577 the loop. The increment amount across iterations is expected
4578 to be vector_size.
4579 BSI - location where the new update stmt is to be placed.
4580 STMT - the original scalar memory-access stmt that is being vectorized.
4581 BUMP - optional. The offset by which to bump the pointer. If not given,
4582 the offset is assumed to be vector_size.
4584 Output: Return NEW_DATAREF_PTR as illustrated above.
4588 tree
4589 bump_vector_ptr (tree dataref_ptr, gimple *ptr_incr, gimple_stmt_iterator *gsi,
4590 gimple *stmt, tree bump)
4592 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4593 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4594 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4595 tree update = TYPE_SIZE_UNIT (vectype);
4596 gassign *incr_stmt;
4597 ssa_op_iter iter;
4598 use_operand_p use_p;
4599 tree new_dataref_ptr;
4601 if (bump)
4602 update = bump;
4604 if (TREE_CODE (dataref_ptr) == SSA_NAME)
4605 new_dataref_ptr = copy_ssa_name (dataref_ptr);
4606 else
4607 new_dataref_ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
4608 incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
4609 dataref_ptr, update);
4610 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4612 /* Copy the points-to information if it exists. */
4613 if (DR_PTR_INFO (dr))
4615 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4616 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4619 if (!ptr_incr)
4620 return new_dataref_ptr;
4622 /* Update the vector-pointer's cross-iteration increment. */
4623 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4625 tree use = USE_FROM_PTR (use_p);
4627 if (use == dataref_ptr)
4628 SET_USE (use_p, new_dataref_ptr);
4629 else
4630 gcc_assert (tree_int_cst_compare (use, update) == 0);
4633 return new_dataref_ptr;
4637 /* Function vect_create_destination_var.
4639 Create a new temporary of type VECTYPE. */
4641 tree
4642 vect_create_destination_var (tree scalar_dest, tree vectype)
4644 tree vec_dest;
4645 const char *name;
4646 char *new_name;
4647 tree type;
4648 enum vect_var_kind kind;
4650 kind = vectype
4651 ? VECTOR_BOOLEAN_TYPE_P (vectype)
4652 ? vect_mask_var
4653 : vect_simple_var
4654 : vect_scalar_var;
4655 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4657 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4659 name = get_name (scalar_dest);
4660 if (name)
4661 new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4662 else
4663 new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
4664 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4665 free (new_name);
4667 return vec_dest;
4670 /* Function vect_grouped_store_supported.
4672 Returns TRUE if interleave high and interleave low permutations
4673 are supported, and FALSE otherwise. */
4675 bool
4676 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4678 machine_mode mode = TYPE_MODE (vectype);
4680 /* vect_permute_store_chain requires the group size to be equal to 3 or
4681 be a power of two. */
4682 if (count != 3 && exact_log2 (count) == -1)
4684 if (dump_enabled_p ())
4685 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4686 "the size of the group of accesses"
4687 " is not a power of 2 or not eqaul to 3\n");
4688 return false;
4691 /* Check that the permutation is supported. */
4692 if (VECTOR_MODE_P (mode))
4694 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4695 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4697 if (count == 3)
4699 unsigned int j0 = 0, j1 = 0, j2 = 0;
4700 unsigned int i, j;
4702 for (j = 0; j < 3; j++)
4704 int nelt0 = ((3 - j) * nelt) % 3;
4705 int nelt1 = ((3 - j) * nelt + 1) % 3;
4706 int nelt2 = ((3 - j) * nelt + 2) % 3;
4707 for (i = 0; i < nelt; i++)
4709 if (3 * i + nelt0 < nelt)
4710 sel[3 * i + nelt0] = j0++;
4711 if (3 * i + nelt1 < nelt)
4712 sel[3 * i + nelt1] = nelt + j1++;
4713 if (3 * i + nelt2 < nelt)
4714 sel[3 * i + nelt2] = 0;
4716 if (!can_vec_perm_p (mode, false, sel))
4718 if (dump_enabled_p ())
4719 dump_printf (MSG_MISSED_OPTIMIZATION,
4720 "permutaion op not supported by target.\n");
4721 return false;
4724 for (i = 0; i < nelt; i++)
4726 if (3 * i + nelt0 < nelt)
4727 sel[3 * i + nelt0] = 3 * i + nelt0;
4728 if (3 * i + nelt1 < nelt)
4729 sel[3 * i + nelt1] = 3 * i + nelt1;
4730 if (3 * i + nelt2 < nelt)
4731 sel[3 * i + nelt2] = nelt + j2++;
4733 if (!can_vec_perm_p (mode, false, sel))
4735 if (dump_enabled_p ())
4736 dump_printf (MSG_MISSED_OPTIMIZATION,
4737 "permutaion op not supported by target.\n");
4738 return false;
4741 return true;
4743 else
4745 /* If length is not equal to 3 then only power of 2 is supported. */
4746 gcc_assert (pow2p_hwi (count));
4748 for (i = 0; i < nelt / 2; i++)
4750 sel[i * 2] = i;
4751 sel[i * 2 + 1] = i + nelt;
4753 if (can_vec_perm_p (mode, false, sel))
4755 for (i = 0; i < nelt; i++)
4756 sel[i] += nelt / 2;
4757 if (can_vec_perm_p (mode, false, sel))
4758 return true;
4763 if (dump_enabled_p ())
4764 dump_printf (MSG_MISSED_OPTIMIZATION,
4765 "permutaion op not supported by target.\n");
4766 return false;
4770 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4771 type VECTYPE. */
4773 bool
4774 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4776 return vect_lanes_optab_supported_p ("vec_store_lanes",
4777 vec_store_lanes_optab,
4778 vectype, count);
4782 /* Function vect_permute_store_chain.
4784 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4785 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4786 the data correctly for the stores. Return the final references for stores
4787 in RESULT_CHAIN.
4789 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4790 The input is 4 vectors each containing 8 elements. We assign a number to
4791 each element, the input sequence is:
4793 1st vec: 0 1 2 3 4 5 6 7
4794 2nd vec: 8 9 10 11 12 13 14 15
4795 3rd vec: 16 17 18 19 20 21 22 23
4796 4th vec: 24 25 26 27 28 29 30 31
4798 The output sequence should be:
4800 1st vec: 0 8 16 24 1 9 17 25
4801 2nd vec: 2 10 18 26 3 11 19 27
4802 3rd vec: 4 12 20 28 5 13 21 30
4803 4th vec: 6 14 22 30 7 15 23 31
4805 i.e., we interleave the contents of the four vectors in their order.
4807 We use interleave_high/low instructions to create such output. The input of
4808 each interleave_high/low operation is two vectors:
4809 1st vec 2nd vec
4810 0 1 2 3 4 5 6 7
4811 the even elements of the result vector are obtained left-to-right from the
4812 high/low elements of the first vector. The odd elements of the result are
4813 obtained left-to-right from the high/low elements of the second vector.
4814 The output of interleave_high will be: 0 4 1 5
4815 and of interleave_low: 2 6 3 7
4818 The permutation is done in log LENGTH stages. In each stage interleave_high
4819 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4820 where the first argument is taken from the first half of DR_CHAIN and the
4821 second argument from it's second half.
4822 In our example,
4824 I1: interleave_high (1st vec, 3rd vec)
4825 I2: interleave_low (1st vec, 3rd vec)
4826 I3: interleave_high (2nd vec, 4th vec)
4827 I4: interleave_low (2nd vec, 4th vec)
4829 The output for the first stage is:
4831 I1: 0 16 1 17 2 18 3 19
4832 I2: 4 20 5 21 6 22 7 23
4833 I3: 8 24 9 25 10 26 11 27
4834 I4: 12 28 13 29 14 30 15 31
4836 The output of the second stage, i.e. the final result is:
4838 I1: 0 8 16 24 1 9 17 25
4839 I2: 2 10 18 26 3 11 19 27
4840 I3: 4 12 20 28 5 13 21 30
4841 I4: 6 14 22 30 7 15 23 31. */
4843 void
4844 vect_permute_store_chain (vec<tree> dr_chain,
4845 unsigned int length,
4846 gimple *stmt,
4847 gimple_stmt_iterator *gsi,
4848 vec<tree> *result_chain)
4850 tree vect1, vect2, high, low;
4851 gimple *perm_stmt;
4852 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4853 tree perm_mask_low, perm_mask_high;
4854 tree data_ref;
4855 tree perm3_mask_low, perm3_mask_high;
4856 unsigned int i, n, log_length = exact_log2 (length);
4857 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4858 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4860 result_chain->quick_grow (length);
4861 memcpy (result_chain->address (), dr_chain.address (),
4862 length * sizeof (tree));
4864 if (length == 3)
4866 unsigned int j0 = 0, j1 = 0, j2 = 0;
4868 for (j = 0; j < 3; j++)
4870 int nelt0 = ((3 - j) * nelt) % 3;
4871 int nelt1 = ((3 - j) * nelt + 1) % 3;
4872 int nelt2 = ((3 - j) * nelt + 2) % 3;
4874 for (i = 0; i < nelt; i++)
4876 if (3 * i + nelt0 < nelt)
4877 sel[3 * i + nelt0] = j0++;
4878 if (3 * i + nelt1 < nelt)
4879 sel[3 * i + nelt1] = nelt + j1++;
4880 if (3 * i + nelt2 < nelt)
4881 sel[3 * i + nelt2] = 0;
4883 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4885 for (i = 0; i < nelt; i++)
4887 if (3 * i + nelt0 < nelt)
4888 sel[3 * i + nelt0] = 3 * i + nelt0;
4889 if (3 * i + nelt1 < nelt)
4890 sel[3 * i + nelt1] = 3 * i + nelt1;
4891 if (3 * i + nelt2 < nelt)
4892 sel[3 * i + nelt2] = nelt + j2++;
4894 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4896 vect1 = dr_chain[0];
4897 vect2 = dr_chain[1];
4899 /* Create interleaving stmt:
4900 low = VEC_PERM_EXPR <vect1, vect2,
4901 {j, nelt, *, j + 1, nelt + j + 1, *,
4902 j + 2, nelt + j + 2, *, ...}> */
4903 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4904 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4905 vect2, perm3_mask_low);
4906 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4908 vect1 = data_ref;
4909 vect2 = dr_chain[2];
4910 /* Create interleaving stmt:
4911 low = VEC_PERM_EXPR <vect1, vect2,
4912 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4913 6, 7, nelt + j + 2, ...}> */
4914 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4915 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4916 vect2, perm3_mask_high);
4917 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4918 (*result_chain)[j] = data_ref;
4921 else
4923 /* If length is not equal to 3 then only power of 2 is supported. */
4924 gcc_assert (pow2p_hwi (length));
4926 for (i = 0, n = nelt / 2; i < n; i++)
4928 sel[i * 2] = i;
4929 sel[i * 2 + 1] = i + nelt;
4931 perm_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4933 for (i = 0; i < nelt; i++)
4934 sel[i] += nelt / 2;
4935 perm_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4937 for (i = 0, n = log_length; i < n; i++)
4939 for (j = 0; j < length/2; j++)
4941 vect1 = dr_chain[j];
4942 vect2 = dr_chain[j+length/2];
4944 /* Create interleaving stmt:
4945 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4946 ...}> */
4947 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4948 perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
4949 vect2, perm_mask_high);
4950 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4951 (*result_chain)[2*j] = high;
4953 /* Create interleaving stmt:
4954 low = VEC_PERM_EXPR <vect1, vect2,
4955 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4956 ...}> */
4957 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4958 perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
4959 vect2, perm_mask_low);
4960 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4961 (*result_chain)[2*j+1] = low;
4963 memcpy (dr_chain.address (), result_chain->address (),
4964 length * sizeof (tree));
4969 /* Function vect_setup_realignment
4971 This function is called when vectorizing an unaligned load using
4972 the dr_explicit_realign[_optimized] scheme.
4973 This function generates the following code at the loop prolog:
4975 p = initial_addr;
4976 x msq_init = *(floor(p)); # prolog load
4977 realignment_token = call target_builtin;
4978 loop:
4979 x msq = phi (msq_init, ---)
4981 The stmts marked with x are generated only for the case of
4982 dr_explicit_realign_optimized.
4984 The code above sets up a new (vector) pointer, pointing to the first
4985 location accessed by STMT, and a "floor-aligned" load using that pointer.
4986 It also generates code to compute the "realignment-token" (if the relevant
4987 target hook was defined), and creates a phi-node at the loop-header bb
4988 whose arguments are the result of the prolog-load (created by this
4989 function) and the result of a load that takes place in the loop (to be
4990 created by the caller to this function).
4992 For the case of dr_explicit_realign_optimized:
4993 The caller to this function uses the phi-result (msq) to create the
4994 realignment code inside the loop, and sets up the missing phi argument,
4995 as follows:
4996 loop:
4997 msq = phi (msq_init, lsq)
4998 lsq = *(floor(p')); # load in loop
4999 result = realign_load (msq, lsq, realignment_token);
5001 For the case of dr_explicit_realign:
5002 loop:
5003 msq = *(floor(p)); # load in loop
5004 p' = p + (VS-1);
5005 lsq = *(floor(p')); # load in loop
5006 result = realign_load (msq, lsq, realignment_token);
5008 Input:
5009 STMT - (scalar) load stmt to be vectorized. This load accesses
5010 a memory location that may be unaligned.
5011 BSI - place where new code is to be inserted.
5012 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
5013 is used.
5015 Output:
5016 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
5017 target hook, if defined.
5018 Return value - the result of the loop-header phi node. */
5020 tree
5021 vect_setup_realignment (gimple *stmt, gimple_stmt_iterator *gsi,
5022 tree *realignment_token,
5023 enum dr_alignment_support alignment_support_scheme,
5024 tree init_addr,
5025 struct loop **at_loop)
5027 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5028 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5029 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5030 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
5031 struct loop *loop = NULL;
5032 edge pe = NULL;
5033 tree scalar_dest = gimple_assign_lhs (stmt);
5034 tree vec_dest;
5035 gimple *inc;
5036 tree ptr;
5037 tree data_ref;
5038 basic_block new_bb;
5039 tree msq_init = NULL_TREE;
5040 tree new_temp;
5041 gphi *phi_stmt;
5042 tree msq = NULL_TREE;
5043 gimple_seq stmts = NULL;
5044 bool inv_p;
5045 bool compute_in_loop = false;
5046 bool nested_in_vect_loop = false;
5047 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
5048 struct loop *loop_for_initial_load = NULL;
5050 if (loop_vinfo)
5052 loop = LOOP_VINFO_LOOP (loop_vinfo);
5053 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
5056 gcc_assert (alignment_support_scheme == dr_explicit_realign
5057 || alignment_support_scheme == dr_explicit_realign_optimized);
5059 /* We need to generate three things:
5060 1. the misalignment computation
5061 2. the extra vector load (for the optimized realignment scheme).
5062 3. the phi node for the two vectors from which the realignment is
5063 done (for the optimized realignment scheme). */
5065 /* 1. Determine where to generate the misalignment computation.
5067 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
5068 calculation will be generated by this function, outside the loop (in the
5069 preheader). Otherwise, INIT_ADDR had already been computed for us by the
5070 caller, inside the loop.
5072 Background: If the misalignment remains fixed throughout the iterations of
5073 the loop, then both realignment schemes are applicable, and also the
5074 misalignment computation can be done outside LOOP. This is because we are
5075 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
5076 are a multiple of VS (the Vector Size), and therefore the misalignment in
5077 different vectorized LOOP iterations is always the same.
5078 The problem arises only if the memory access is in an inner-loop nested
5079 inside LOOP, which is now being vectorized using outer-loop vectorization.
5080 This is the only case when the misalignment of the memory access may not
5081 remain fixed throughout the iterations of the inner-loop (as explained in
5082 detail in vect_supportable_dr_alignment). In this case, not only is the
5083 optimized realignment scheme not applicable, but also the misalignment
5084 computation (and generation of the realignment token that is passed to
5085 REALIGN_LOAD) have to be done inside the loop.
5087 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
5088 or not, which in turn determines if the misalignment is computed inside
5089 the inner-loop, or outside LOOP. */
5091 if (init_addr != NULL_TREE || !loop_vinfo)
5093 compute_in_loop = true;
5094 gcc_assert (alignment_support_scheme == dr_explicit_realign);
5098 /* 2. Determine where to generate the extra vector load.
5100 For the optimized realignment scheme, instead of generating two vector
5101 loads in each iteration, we generate a single extra vector load in the
5102 preheader of the loop, and in each iteration reuse the result of the
5103 vector load from the previous iteration. In case the memory access is in
5104 an inner-loop nested inside LOOP, which is now being vectorized using
5105 outer-loop vectorization, we need to determine whether this initial vector
5106 load should be generated at the preheader of the inner-loop, or can be
5107 generated at the preheader of LOOP. If the memory access has no evolution
5108 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
5109 to be generated inside LOOP (in the preheader of the inner-loop). */
5111 if (nested_in_vect_loop)
5113 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
5114 bool invariant_in_outerloop =
5115 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
5116 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
5118 else
5119 loop_for_initial_load = loop;
5120 if (at_loop)
5121 *at_loop = loop_for_initial_load;
5123 if (loop_for_initial_load)
5124 pe = loop_preheader_edge (loop_for_initial_load);
5126 /* 3. For the case of the optimized realignment, create the first vector
5127 load at the loop preheader. */
5129 if (alignment_support_scheme == dr_explicit_realign_optimized)
5131 /* Create msq_init = *(floor(p1)) in the loop preheader */
5132 gassign *new_stmt;
5134 gcc_assert (!compute_in_loop);
5135 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5136 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
5137 NULL_TREE, &init_addr, NULL, &inc,
5138 true, &inv_p);
5139 if (TREE_CODE (ptr) == SSA_NAME)
5140 new_temp = copy_ssa_name (ptr);
5141 else
5142 new_temp = make_ssa_name (TREE_TYPE (ptr));
5143 new_stmt = gimple_build_assign
5144 (new_temp, BIT_AND_EXPR, ptr,
5145 build_int_cst (TREE_TYPE (ptr),
5146 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5147 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
5148 gcc_assert (!new_bb);
5149 data_ref
5150 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
5151 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
5152 new_stmt = gimple_build_assign (vec_dest, data_ref);
5153 new_temp = make_ssa_name (vec_dest, new_stmt);
5154 gimple_assign_set_lhs (new_stmt, new_temp);
5155 if (pe)
5157 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
5158 gcc_assert (!new_bb);
5160 else
5161 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5163 msq_init = gimple_assign_lhs (new_stmt);
5166 /* 4. Create realignment token using a target builtin, if available.
5167 It is done either inside the containing loop, or before LOOP (as
5168 determined above). */
5170 if (targetm.vectorize.builtin_mask_for_load)
5172 gcall *new_stmt;
5173 tree builtin_decl;
5175 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
5176 if (!init_addr)
5178 /* Generate the INIT_ADDR computation outside LOOP. */
5179 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
5180 NULL_TREE, loop);
5181 if (loop)
5183 pe = loop_preheader_edge (loop);
5184 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
5185 gcc_assert (!new_bb);
5187 else
5188 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
5191 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
5192 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
5193 vec_dest =
5194 vect_create_destination_var (scalar_dest,
5195 gimple_call_return_type (new_stmt));
5196 new_temp = make_ssa_name (vec_dest, new_stmt);
5197 gimple_call_set_lhs (new_stmt, new_temp);
5199 if (compute_in_loop)
5200 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5201 else
5203 /* Generate the misalignment computation outside LOOP. */
5204 pe = loop_preheader_edge (loop);
5205 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
5206 gcc_assert (!new_bb);
5209 *realignment_token = gimple_call_lhs (new_stmt);
5211 /* The result of the CALL_EXPR to this builtin is determined from
5212 the value of the parameter and no global variables are touched
5213 which makes the builtin a "const" function. Requiring the
5214 builtin to have the "const" attribute makes it unnecessary
5215 to call mark_call_clobbered. */
5216 gcc_assert (TREE_READONLY (builtin_decl));
5219 if (alignment_support_scheme == dr_explicit_realign)
5220 return msq;
5222 gcc_assert (!compute_in_loop);
5223 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
5226 /* 5. Create msq = phi <msq_init, lsq> in loop */
5228 pe = loop_preheader_edge (containing_loop);
5229 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5230 msq = make_ssa_name (vec_dest);
5231 phi_stmt = create_phi_node (msq, containing_loop->header);
5232 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
5234 return msq;
5238 /* Function vect_grouped_load_supported.
5240 COUNT is the size of the load group (the number of statements plus the
5241 number of gaps). SINGLE_ELEMENT_P is true if there is actually
5242 only one statement, with a gap of COUNT - 1.
5244 Returns true if a suitable permute exists. */
5246 bool
5247 vect_grouped_load_supported (tree vectype, bool single_element_p,
5248 unsigned HOST_WIDE_INT count)
5250 machine_mode mode = TYPE_MODE (vectype);
5252 /* If this is single-element interleaving with an element distance
5253 that leaves unused vector loads around punt - we at least create
5254 very sub-optimal code in that case (and blow up memory,
5255 see PR65518). */
5256 if (single_element_p && count > TYPE_VECTOR_SUBPARTS (vectype))
5258 if (dump_enabled_p ())
5259 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5260 "single-element interleaving not supported "
5261 "for not adjacent vector loads\n");
5262 return false;
5265 /* vect_permute_load_chain requires the group size to be equal to 3 or
5266 be a power of two. */
5267 if (count != 3 && exact_log2 (count) == -1)
5269 if (dump_enabled_p ())
5270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5271 "the size of the group of accesses"
5272 " is not a power of 2 or not equal to 3\n");
5273 return false;
5276 /* Check that the permutation is supported. */
5277 if (VECTOR_MODE_P (mode))
5279 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5280 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5282 if (count == 3)
5284 unsigned int k;
5285 for (k = 0; k < 3; k++)
5287 for (i = 0; i < nelt; i++)
5288 if (3 * i + k < 2 * nelt)
5289 sel[i] = 3 * i + k;
5290 else
5291 sel[i] = 0;
5292 if (!can_vec_perm_p (mode, false, sel))
5294 if (dump_enabled_p ())
5295 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5296 "shuffle of 3 loads is not supported by"
5297 " target\n");
5298 return false;
5300 for (i = 0, j = 0; i < nelt; i++)
5301 if (3 * i + k < 2 * nelt)
5302 sel[i] = i;
5303 else
5304 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5305 if (!can_vec_perm_p (mode, false, sel))
5307 if (dump_enabled_p ())
5308 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5309 "shuffle of 3 loads is not supported by"
5310 " target\n");
5311 return false;
5314 return true;
5316 else
5318 /* If length is not equal to 3 then only power of 2 is supported. */
5319 gcc_assert (pow2p_hwi (count));
5320 for (i = 0; i < nelt; i++)
5321 sel[i] = i * 2;
5322 if (can_vec_perm_p (mode, false, sel))
5324 for (i = 0; i < nelt; i++)
5325 sel[i] = i * 2 + 1;
5326 if (can_vec_perm_p (mode, false, sel))
5327 return true;
5332 if (dump_enabled_p ())
5333 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5334 "extract even/odd not supported by target\n");
5335 return false;
5338 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5339 type VECTYPE. */
5341 bool
5342 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5344 return vect_lanes_optab_supported_p ("vec_load_lanes",
5345 vec_load_lanes_optab,
5346 vectype, count);
5349 /* Function vect_permute_load_chain.
5351 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5352 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5353 the input data correctly. Return the final references for loads in
5354 RESULT_CHAIN.
5356 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5357 The input is 4 vectors each containing 8 elements. We assign a number to each
5358 element, the input sequence is:
5360 1st vec: 0 1 2 3 4 5 6 7
5361 2nd vec: 8 9 10 11 12 13 14 15
5362 3rd vec: 16 17 18 19 20 21 22 23
5363 4th vec: 24 25 26 27 28 29 30 31
5365 The output sequence should be:
5367 1st vec: 0 4 8 12 16 20 24 28
5368 2nd vec: 1 5 9 13 17 21 25 29
5369 3rd vec: 2 6 10 14 18 22 26 30
5370 4th vec: 3 7 11 15 19 23 27 31
5372 i.e., the first output vector should contain the first elements of each
5373 interleaving group, etc.
5375 We use extract_even/odd instructions to create such output. The input of
5376 each extract_even/odd operation is two vectors
5377 1st vec 2nd vec
5378 0 1 2 3 4 5 6 7
5380 and the output is the vector of extracted even/odd elements. The output of
5381 extract_even will be: 0 2 4 6
5382 and of extract_odd: 1 3 5 7
5385 The permutation is done in log LENGTH stages. In each stage extract_even
5386 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5387 their order. In our example,
5389 E1: extract_even (1st vec, 2nd vec)
5390 E2: extract_odd (1st vec, 2nd vec)
5391 E3: extract_even (3rd vec, 4th vec)
5392 E4: extract_odd (3rd vec, 4th vec)
5394 The output for the first stage will be:
5396 E1: 0 2 4 6 8 10 12 14
5397 E2: 1 3 5 7 9 11 13 15
5398 E3: 16 18 20 22 24 26 28 30
5399 E4: 17 19 21 23 25 27 29 31
5401 In order to proceed and create the correct sequence for the next stage (or
5402 for the correct output, if the second stage is the last one, as in our
5403 example), we first put the output of extract_even operation and then the
5404 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5405 The input for the second stage is:
5407 1st vec (E1): 0 2 4 6 8 10 12 14
5408 2nd vec (E3): 16 18 20 22 24 26 28 30
5409 3rd vec (E2): 1 3 5 7 9 11 13 15
5410 4th vec (E4): 17 19 21 23 25 27 29 31
5412 The output of the second stage:
5414 E1: 0 4 8 12 16 20 24 28
5415 E2: 2 6 10 14 18 22 26 30
5416 E3: 1 5 9 13 17 21 25 29
5417 E4: 3 7 11 15 19 23 27 31
5419 And RESULT_CHAIN after reordering:
5421 1st vec (E1): 0 4 8 12 16 20 24 28
5422 2nd vec (E3): 1 5 9 13 17 21 25 29
5423 3rd vec (E2): 2 6 10 14 18 22 26 30
5424 4th vec (E4): 3 7 11 15 19 23 27 31. */
5426 static void
5427 vect_permute_load_chain (vec<tree> dr_chain,
5428 unsigned int length,
5429 gimple *stmt,
5430 gimple_stmt_iterator *gsi,
5431 vec<tree> *result_chain)
5433 tree data_ref, first_vect, second_vect;
5434 tree perm_mask_even, perm_mask_odd;
5435 tree perm3_mask_low, perm3_mask_high;
5436 gimple *perm_stmt;
5437 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5438 unsigned int i, j, log_length = exact_log2 (length);
5439 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5440 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5442 result_chain->quick_grow (length);
5443 memcpy (result_chain->address (), dr_chain.address (),
5444 length * sizeof (tree));
5446 if (length == 3)
5448 unsigned int k;
5450 for (k = 0; k < 3; k++)
5452 for (i = 0; i < nelt; i++)
5453 if (3 * i + k < 2 * nelt)
5454 sel[i] = 3 * i + k;
5455 else
5456 sel[i] = 0;
5457 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
5459 for (i = 0, j = 0; i < nelt; i++)
5460 if (3 * i + k < 2 * nelt)
5461 sel[i] = i;
5462 else
5463 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5465 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
5467 first_vect = dr_chain[0];
5468 second_vect = dr_chain[1];
5470 /* Create interleaving stmt (low part of):
5471 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5472 ...}> */
5473 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5474 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5475 second_vect, perm3_mask_low);
5476 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5478 /* Create interleaving stmt (high part of):
5479 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5480 ...}> */
5481 first_vect = data_ref;
5482 second_vect = dr_chain[2];
5483 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5484 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5485 second_vect, perm3_mask_high);
5486 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5487 (*result_chain)[k] = data_ref;
5490 else
5492 /* If length is not equal to 3 then only power of 2 is supported. */
5493 gcc_assert (pow2p_hwi (length));
5495 for (i = 0; i < nelt; ++i)
5496 sel[i] = i * 2;
5497 perm_mask_even = vect_gen_perm_mask_checked (vectype, sel);
5499 for (i = 0; i < nelt; ++i)
5500 sel[i] = i * 2 + 1;
5501 perm_mask_odd = vect_gen_perm_mask_checked (vectype, sel);
5503 for (i = 0; i < log_length; i++)
5505 for (j = 0; j < length; j += 2)
5507 first_vect = dr_chain[j];
5508 second_vect = dr_chain[j+1];
5510 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5511 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5512 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5513 first_vect, second_vect,
5514 perm_mask_even);
5515 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5516 (*result_chain)[j/2] = data_ref;
5518 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5519 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5520 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5521 first_vect, second_vect,
5522 perm_mask_odd);
5523 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5524 (*result_chain)[j/2+length/2] = data_ref;
5526 memcpy (dr_chain.address (), result_chain->address (),
5527 length * sizeof (tree));
5532 /* Function vect_shift_permute_load_chain.
5534 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5535 sequence of stmts to reorder the input data accordingly.
5536 Return the final references for loads in RESULT_CHAIN.
5537 Return true if successed, false otherwise.
5539 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5540 The input is 3 vectors each containing 8 elements. We assign a
5541 number to each element, the input sequence is:
5543 1st vec: 0 1 2 3 4 5 6 7
5544 2nd vec: 8 9 10 11 12 13 14 15
5545 3rd vec: 16 17 18 19 20 21 22 23
5547 The output sequence should be:
5549 1st vec: 0 3 6 9 12 15 18 21
5550 2nd vec: 1 4 7 10 13 16 19 22
5551 3rd vec: 2 5 8 11 14 17 20 23
5553 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5555 First we shuffle all 3 vectors to get correct elements order:
5557 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5558 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5559 3rd vec: (16 19 22) (17 20 23) (18 21)
5561 Next we unite and shift vector 3 times:
5563 1st step:
5564 shift right by 6 the concatenation of:
5565 "1st vec" and "2nd vec"
5566 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5567 "2nd vec" and "3rd vec"
5568 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5569 "3rd vec" and "1st vec"
5570 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5571 | New vectors |
5573 So that now new vectors are:
5575 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5576 2nd vec: (10 13) (16 19 22) (17 20 23)
5577 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5579 2nd step:
5580 shift right by 5 the concatenation of:
5581 "1st vec" and "3rd vec"
5582 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5583 "2nd vec" and "1st vec"
5584 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5585 "3rd vec" and "2nd vec"
5586 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5587 | New vectors |
5589 So that now new vectors are:
5591 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5592 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5593 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5595 3rd step:
5596 shift right by 5 the concatenation of:
5597 "1st vec" and "1st vec"
5598 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5599 shift right by 3 the concatenation of:
5600 "2nd vec" and "2nd vec"
5601 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5602 | New vectors |
5604 So that now all vectors are READY:
5605 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5606 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5607 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5609 This algorithm is faster than one in vect_permute_load_chain if:
5610 1. "shift of a concatination" is faster than general permutation.
5611 This is usually so.
5612 2. The TARGET machine can't execute vector instructions in parallel.
5613 This is because each step of the algorithm depends on previous.
5614 The algorithm in vect_permute_load_chain is much more parallel.
5616 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5619 static bool
5620 vect_shift_permute_load_chain (vec<tree> dr_chain,
5621 unsigned int length,
5622 gimple *stmt,
5623 gimple_stmt_iterator *gsi,
5624 vec<tree> *result_chain)
5626 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5627 tree perm2_mask1, perm2_mask2, perm3_mask;
5628 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5629 gimple *perm_stmt;
5631 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5632 unsigned int i;
5633 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5634 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5635 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5636 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5638 result_chain->quick_grow (length);
5639 memcpy (result_chain->address (), dr_chain.address (),
5640 length * sizeof (tree));
5642 if (pow2p_hwi (length) && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5644 unsigned int j, log_length = exact_log2 (length);
5645 for (i = 0; i < nelt / 2; ++i)
5646 sel[i] = i * 2;
5647 for (i = 0; i < nelt / 2; ++i)
5648 sel[nelt / 2 + i] = i * 2 + 1;
5649 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5651 if (dump_enabled_p ())
5652 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5653 "shuffle of 2 fields structure is not \
5654 supported by target\n");
5655 return false;
5657 perm2_mask1 = vect_gen_perm_mask_checked (vectype, sel);
5659 for (i = 0; i < nelt / 2; ++i)
5660 sel[i] = i * 2 + 1;
5661 for (i = 0; i < nelt / 2; ++i)
5662 sel[nelt / 2 + i] = i * 2;
5663 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5665 if (dump_enabled_p ())
5666 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5667 "shuffle of 2 fields structure is not \
5668 supported by target\n");
5669 return false;
5671 perm2_mask2 = vect_gen_perm_mask_checked (vectype, sel);
5673 /* Generating permutation constant to shift all elements.
5674 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5675 for (i = 0; i < nelt; i++)
5676 sel[i] = nelt / 2 + i;
5677 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5679 if (dump_enabled_p ())
5680 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5681 "shift permutation is not supported by target\n");
5682 return false;
5684 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5686 /* Generating permutation constant to select vector from 2.
5687 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5688 for (i = 0; i < nelt / 2; i++)
5689 sel[i] = i;
5690 for (i = nelt / 2; i < nelt; i++)
5691 sel[i] = nelt + i;
5692 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5694 if (dump_enabled_p ())
5695 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5696 "select is not supported by target\n");
5697 return false;
5699 select_mask = vect_gen_perm_mask_checked (vectype, sel);
5701 for (i = 0; i < log_length; i++)
5703 for (j = 0; j < length; j += 2)
5705 first_vect = dr_chain[j];
5706 second_vect = dr_chain[j + 1];
5708 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5709 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5710 first_vect, first_vect,
5711 perm2_mask1);
5712 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5713 vect[0] = data_ref;
5715 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5716 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5717 second_vect, second_vect,
5718 perm2_mask2);
5719 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5720 vect[1] = data_ref;
5722 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5723 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5724 vect[0], vect[1], shift1_mask);
5725 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5726 (*result_chain)[j/2 + length/2] = data_ref;
5728 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5729 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5730 vect[0], vect[1], select_mask);
5731 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5732 (*result_chain)[j/2] = data_ref;
5734 memcpy (dr_chain.address (), result_chain->address (),
5735 length * sizeof (tree));
5737 return true;
5739 if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
5741 unsigned int k = 0, l = 0;
5743 /* Generating permutation constant to get all elements in rigth order.
5744 For vector length 8 it is {0 3 6 1 4 7 2 5}. */
5745 for (i = 0; i < nelt; i++)
5747 if (3 * k + (l % 3) >= nelt)
5749 k = 0;
5750 l += (3 - (nelt % 3));
5752 sel[i] = 3 * k + (l % 3);
5753 k++;
5755 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5757 if (dump_enabled_p ())
5758 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5759 "shuffle of 3 fields structure is not \
5760 supported by target\n");
5761 return false;
5763 perm3_mask = vect_gen_perm_mask_checked (vectype, sel);
5765 /* Generating permutation constant to shift all elements.
5766 For vector length 8 it is {6 7 8 9 10 11 12 13}. */
5767 for (i = 0; i < nelt; i++)
5768 sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
5769 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5771 if (dump_enabled_p ())
5772 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5773 "shift permutation is not supported by target\n");
5774 return false;
5776 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5778 /* Generating permutation constant to shift all elements.
5779 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5780 for (i = 0; i < nelt; i++)
5781 sel[i] = 2 * (nelt / 3) + 1 + i;
5782 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5784 if (dump_enabled_p ())
5785 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5786 "shift permutation is not supported by target\n");
5787 return false;
5789 shift2_mask = vect_gen_perm_mask_checked (vectype, sel);
5791 /* Generating permutation constant to shift all elements.
5792 For vector length 8 it is {3 4 5 6 7 8 9 10}. */
5793 for (i = 0; i < nelt; i++)
5794 sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
5795 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5797 if (dump_enabled_p ())
5798 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5799 "shift permutation is not supported by target\n");
5800 return false;
5802 shift3_mask = vect_gen_perm_mask_checked (vectype, sel);
5804 /* Generating permutation constant to shift all elements.
5805 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5806 for (i = 0; i < nelt; i++)
5807 sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
5808 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5810 if (dump_enabled_p ())
5811 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5812 "shift permutation is not supported by target\n");
5813 return false;
5815 shift4_mask = vect_gen_perm_mask_checked (vectype, sel);
5817 for (k = 0; k < 3; k++)
5819 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
5820 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5821 dr_chain[k], dr_chain[k],
5822 perm3_mask);
5823 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5824 vect[k] = data_ref;
5827 for (k = 0; k < 3; k++)
5829 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
5830 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5831 vect[k % 3], vect[(k + 1) % 3],
5832 shift1_mask);
5833 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5834 vect_shift[k] = data_ref;
5837 for (k = 0; k < 3; k++)
5839 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
5840 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5841 vect_shift[(4 - k) % 3],
5842 vect_shift[(3 - k) % 3],
5843 shift2_mask);
5844 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5845 vect[k] = data_ref;
5848 (*result_chain)[3 - (nelt % 3)] = vect[2];
5850 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
5851 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
5852 vect[0], shift3_mask);
5853 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5854 (*result_chain)[nelt % 3] = data_ref;
5856 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
5857 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
5858 vect[1], shift4_mask);
5859 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5860 (*result_chain)[0] = data_ref;
5861 return true;
5863 return false;
5866 /* Function vect_transform_grouped_load.
5868 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5869 to perform their permutation and ascribe the result vectorized statements to
5870 the scalar statements.
5873 void
5874 vect_transform_grouped_load (gimple *stmt, vec<tree> dr_chain, int size,
5875 gimple_stmt_iterator *gsi)
5877 machine_mode mode;
5878 vec<tree> result_chain = vNULL;
5880 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5881 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5882 vectors, that are ready for vector computation. */
5883 result_chain.create (size);
5885 /* If reassociation width for vector type is 2 or greater target machine can
5886 execute 2 or more vector instructions in parallel. Otherwise try to
5887 get chain for loads group using vect_shift_permute_load_chain. */
5888 mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
5889 if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
5890 || pow2p_hwi (size)
5891 || !vect_shift_permute_load_chain (dr_chain, size, stmt,
5892 gsi, &result_chain))
5893 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5894 vect_record_grouped_load_vectors (stmt, result_chain);
5895 result_chain.release ();
5898 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5899 generated as part of the vectorization of STMT. Assign the statement
5900 for each vector to the associated scalar statement. */
5902 void
5903 vect_record_grouped_load_vectors (gimple *stmt, vec<tree> result_chain)
5905 gimple *first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5906 gimple *next_stmt, *new_stmt;
5907 unsigned int i, gap_count;
5908 tree tmp_data_ref;
5910 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5911 Since we scan the chain starting from it's first node, their order
5912 corresponds the order of data-refs in RESULT_CHAIN. */
5913 next_stmt = first_stmt;
5914 gap_count = 1;
5915 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5917 if (!next_stmt)
5918 break;
5920 /* Skip the gaps. Loads created for the gaps will be removed by dead
5921 code elimination pass later. No need to check for the first stmt in
5922 the group, since it always exists.
5923 GROUP_GAP is the number of steps in elements from the previous
5924 access (if there is no gap GROUP_GAP is 1). We skip loads that
5925 correspond to the gaps. */
5926 if (next_stmt != first_stmt
5927 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5929 gap_count++;
5930 continue;
5933 while (next_stmt)
5935 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5936 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5937 copies, and we put the new vector statement in the first available
5938 RELATED_STMT. */
5939 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5940 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5941 else
5943 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5945 gimple *prev_stmt =
5946 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5947 gimple *rel_stmt =
5948 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5949 while (rel_stmt)
5951 prev_stmt = rel_stmt;
5952 rel_stmt =
5953 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5956 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5957 new_stmt;
5961 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5962 gap_count = 1;
5963 /* If NEXT_STMT accesses the same DR as the previous statement,
5964 put the same TMP_DATA_REF as its vectorized statement; otherwise
5965 get the next data-ref from RESULT_CHAIN. */
5966 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5967 break;
5972 /* Function vect_force_dr_alignment_p.
5974 Returns whether the alignment of a DECL can be forced to be aligned
5975 on ALIGNMENT bit boundary. */
5977 bool
5978 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5980 if (!VAR_P (decl))
5981 return false;
5983 if (decl_in_symtab_p (decl)
5984 && !symtab_node::get (decl)->can_increase_alignment_p ())
5985 return false;
5987 if (TREE_STATIC (decl))
5988 return (alignment <= MAX_OFILE_ALIGNMENT);
5989 else
5990 return (alignment <= MAX_STACK_ALIGNMENT);
5994 /* Return whether the data reference DR is supported with respect to its
5995 alignment.
5996 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5997 it is aligned, i.e., check if it is possible to vectorize it with different
5998 alignment. */
6000 enum dr_alignment_support
6001 vect_supportable_dr_alignment (struct data_reference *dr,
6002 bool check_aligned_accesses)
6004 gimple *stmt = DR_STMT (dr);
6005 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6006 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6007 machine_mode mode = TYPE_MODE (vectype);
6008 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6009 struct loop *vect_loop = NULL;
6010 bool nested_in_vect_loop = false;
6012 if (aligned_access_p (dr) && !check_aligned_accesses)
6013 return dr_aligned;
6015 /* For now assume all conditional loads/stores support unaligned
6016 access without any special code. */
6017 if (is_gimple_call (stmt)
6018 && gimple_call_internal_p (stmt)
6019 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
6020 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
6021 return dr_unaligned_supported;
6023 if (loop_vinfo)
6025 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
6026 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
6029 /* Possibly unaligned access. */
6031 /* We can choose between using the implicit realignment scheme (generating
6032 a misaligned_move stmt) and the explicit realignment scheme (generating
6033 aligned loads with a REALIGN_LOAD). There are two variants to the
6034 explicit realignment scheme: optimized, and unoptimized.
6035 We can optimize the realignment only if the step between consecutive
6036 vector loads is equal to the vector size. Since the vector memory
6037 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
6038 is guaranteed that the misalignment amount remains the same throughout the
6039 execution of the vectorized loop. Therefore, we can create the
6040 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
6041 at the loop preheader.
6043 However, in the case of outer-loop vectorization, when vectorizing a
6044 memory access in the inner-loop nested within the LOOP that is now being
6045 vectorized, while it is guaranteed that the misalignment of the
6046 vectorized memory access will remain the same in different outer-loop
6047 iterations, it is *not* guaranteed that is will remain the same throughout
6048 the execution of the inner-loop. This is because the inner-loop advances
6049 with the original scalar step (and not in steps of VS). If the inner-loop
6050 step happens to be a multiple of VS, then the misalignment remains fixed
6051 and we can use the optimized realignment scheme. For example:
6053 for (i=0; i<N; i++)
6054 for (j=0; j<M; j++)
6055 s += a[i+j];
6057 When vectorizing the i-loop in the above example, the step between
6058 consecutive vector loads is 1, and so the misalignment does not remain
6059 fixed across the execution of the inner-loop, and the realignment cannot
6060 be optimized (as illustrated in the following pseudo vectorized loop):
6062 for (i=0; i<N; i+=4)
6063 for (j=0; j<M; j++){
6064 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
6065 // when j is {0,1,2,3,4,5,6,7,...} respectively.
6066 // (assuming that we start from an aligned address).
6069 We therefore have to use the unoptimized realignment scheme:
6071 for (i=0; i<N; i+=4)
6072 for (j=k; j<M; j+=4)
6073 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
6074 // that the misalignment of the initial address is
6075 // 0).
6077 The loop can then be vectorized as follows:
6079 for (k=0; k<4; k++){
6080 rt = get_realignment_token (&vp[k]);
6081 for (i=0; i<N; i+=4){
6082 v1 = vp[i+k];
6083 for (j=k; j<M; j+=4){
6084 v2 = vp[i+j+VS-1];
6085 va = REALIGN_LOAD <v1,v2,rt>;
6086 vs += va;
6087 v1 = v2;
6090 } */
6092 if (DR_IS_READ (dr))
6094 bool is_packed = false;
6095 tree type = (TREE_TYPE (DR_REF (dr)));
6097 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
6098 && (!targetm.vectorize.builtin_mask_for_load
6099 || targetm.vectorize.builtin_mask_for_load ()))
6101 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6103 /* If we are doing SLP then the accesses need not have the
6104 same alignment, instead it depends on the SLP group size. */
6105 if (loop_vinfo
6106 && STMT_SLP_TYPE (stmt_info)
6107 && (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6108 * GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)))
6109 % TYPE_VECTOR_SUBPARTS (vectype) != 0))
6111 else if (!loop_vinfo
6112 || (nested_in_vect_loop
6113 && (TREE_INT_CST_LOW (DR_STEP (dr))
6114 != GET_MODE_SIZE (TYPE_MODE (vectype)))))
6115 return dr_explicit_realign;
6116 else
6117 return dr_explicit_realign_optimized;
6119 if (!known_alignment_for_access_p (dr))
6120 is_packed = not_size_aligned (DR_REF (dr));
6122 if ((TYPE_USER_ALIGN (type) && !is_packed)
6123 || targetm.vectorize.
6124 support_vector_misalignment (mode, type,
6125 DR_MISALIGNMENT (dr), is_packed))
6126 /* Can't software pipeline the loads, but can at least do them. */
6127 return dr_unaligned_supported;
6129 else
6131 bool is_packed = false;
6132 tree type = (TREE_TYPE (DR_REF (dr)));
6134 if (!known_alignment_for_access_p (dr))
6135 is_packed = not_size_aligned (DR_REF (dr));
6137 if ((TYPE_USER_ALIGN (type) && !is_packed)
6138 || targetm.vectorize.
6139 support_vector_misalignment (mode, type,
6140 DR_MISALIGNMENT (dr), is_packed))
6141 return dr_unaligned_supported;
6144 /* Unsupported. */
6145 return dr_unaligned_unsupported;