2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob3befa38788d6167510cdbd46377601c8c479dc5f
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2015 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 "dumpfile.h"
26 #include "backend.h"
27 #include "predict.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "rtl.h"
31 #include "ssa.h"
32 #include "alias.h"
33 #include "fold-const.h"
34 #include "stor-layout.h"
35 #include "tm_p.h"
36 #include "target.h"
37 #include "gimple-pretty-print.h"
38 #include "internal-fn.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-chrec.h"
48 #include "tree-scalar-evolution.h"
49 #include "tree-vectorizer.h"
50 #include "diagnostic-core.h"
51 #include "cgraph.h"
52 #include "expr.h"
53 #include "insn-codes.h"
54 #include "optabs-tree.h"
55 #include "builtins.h"
56 #include "params.h"
58 /* Return true if load- or store-lanes optab OPTAB is implemented for
59 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
61 static bool
62 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
63 tree vectype, unsigned HOST_WIDE_INT count)
65 machine_mode mode, array_mode;
66 bool limit_p;
68 mode = TYPE_MODE (vectype);
69 limit_p = !targetm.array_mode_supported_p (mode, count);
70 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
71 MODE_INT, limit_p);
73 if (array_mode == BLKmode)
75 if (dump_enabled_p ())
76 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
77 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
78 GET_MODE_NAME (mode), count);
79 return false;
82 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
84 if (dump_enabled_p ())
85 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
86 "cannot use %s<%s><%s>\n", name,
87 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
88 return false;
91 if (dump_enabled_p ())
92 dump_printf_loc (MSG_NOTE, vect_location,
93 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
94 GET_MODE_NAME (mode));
96 return true;
100 /* Return the smallest scalar part of STMT.
101 This is used to determine the vectype of the stmt. We generally set the
102 vectype according to the type of the result (lhs). For stmts whose
103 result-type is different than the type of the arguments (e.g., demotion,
104 promotion), vectype will be reset appropriately (later). Note that we have
105 to visit the smallest datatype in this function, because that determines the
106 VF. If the smallest datatype in the loop is present only as the rhs of a
107 promotion operation - we'd miss it.
108 Such a case, where a variable of this datatype does not appear in the lhs
109 anywhere in the loop, can only occur if it's an invariant: e.g.:
110 'int_x = (int) short_inv', which we'd expect to have been optimized away by
111 invariant motion. However, we cannot rely on invariant motion to always
112 take invariants out of the loop, and so in the case of promotion we also
113 have to check the rhs.
114 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
115 types. */
117 tree
118 vect_get_smallest_scalar_type (gimple *stmt, HOST_WIDE_INT *lhs_size_unit,
119 HOST_WIDE_INT *rhs_size_unit)
121 tree scalar_type = gimple_expr_type (stmt);
122 HOST_WIDE_INT lhs, rhs;
124 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
126 if (is_gimple_assign (stmt)
127 && (gimple_assign_cast_p (stmt)
128 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
129 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
130 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
132 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
134 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
135 if (rhs < lhs)
136 scalar_type = rhs_type;
139 *lhs_size_unit = lhs;
140 *rhs_size_unit = rhs;
141 return scalar_type;
145 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
146 tested at run-time. Return TRUE if DDR was successfully inserted.
147 Return false if versioning is not supported. */
149 static bool
150 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
152 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
154 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
155 return false;
157 if (dump_enabled_p ())
159 dump_printf_loc (MSG_NOTE, vect_location,
160 "mark for run-time aliasing test between ");
161 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
162 dump_printf (MSG_NOTE, " and ");
163 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
164 dump_printf (MSG_NOTE, "\n");
167 if (optimize_loop_nest_for_size_p (loop))
169 if (dump_enabled_p ())
170 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
171 "versioning not supported when optimizing"
172 " for size.\n");
173 return false;
176 /* FORNOW: We don't support versioning with outer-loop vectorization. */
177 if (loop->inner)
179 if (dump_enabled_p ())
180 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
181 "versioning not yet supported for outer-loops.\n");
182 return false;
185 /* FORNOW: We don't support creating runtime alias tests for non-constant
186 step. */
187 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
188 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
190 if (dump_enabled_p ())
191 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
192 "versioning not yet supported for non-constant "
193 "step\n");
194 return false;
197 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
198 return true;
202 /* Function vect_analyze_data_ref_dependence.
204 Return TRUE if there (might) exist a dependence between a memory-reference
205 DRA and a memory-reference DRB. When versioning for alias may check a
206 dependence at run-time, return FALSE. Adjust *MAX_VF according to
207 the data dependence. */
209 static bool
210 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
211 loop_vec_info loop_vinfo, int *max_vf)
213 unsigned int i;
214 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
215 struct data_reference *dra = DDR_A (ddr);
216 struct data_reference *drb = DDR_B (ddr);
217 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
218 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
219 lambda_vector dist_v;
220 unsigned int loop_depth;
222 /* In loop analysis all data references should be vectorizable. */
223 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
224 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
225 gcc_unreachable ();
227 /* Independent data accesses. */
228 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
229 return false;
231 if (dra == drb
232 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
233 return false;
235 /* Even if we have an anti-dependence then, as the vectorized loop covers at
236 least two scalar iterations, there is always also a true dependence.
237 As the vectorizer does not re-order loads and stores we can ignore
238 the anti-dependence if TBAA can disambiguate both DRs similar to the
239 case with known negative distance anti-dependences (positive
240 distance anti-dependences would violate TBAA constraints). */
241 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
242 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
243 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
244 get_alias_set (DR_REF (drb))))
245 return false;
247 /* Unknown data dependence. */
248 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
250 /* If user asserted safelen consecutive iterations can be
251 executed concurrently, assume independence. */
252 if (loop->safelen >= 2)
254 if (loop->safelen < *max_vf)
255 *max_vf = loop->safelen;
256 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
257 return false;
260 if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
261 || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
263 if (dump_enabled_p ())
265 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
266 "versioning for alias not supported for: "
267 "can't determine dependence between ");
268 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
269 DR_REF (dra));
270 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
271 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
272 DR_REF (drb));
273 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
275 return true;
278 if (dump_enabled_p ())
280 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
281 "versioning for alias required: "
282 "can't determine dependence between ");
283 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
284 DR_REF (dra));
285 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
286 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
287 DR_REF (drb));
288 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
291 /* Add to list of ddrs that need to be tested at run-time. */
292 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
295 /* Known data dependence. */
296 if (DDR_NUM_DIST_VECTS (ddr) == 0)
298 /* If user asserted safelen consecutive iterations can be
299 executed concurrently, assume independence. */
300 if (loop->safelen >= 2)
302 if (loop->safelen < *max_vf)
303 *max_vf = loop->safelen;
304 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
305 return false;
308 if (STMT_VINFO_GATHER_SCATTER_P (stmtinfo_a)
309 || STMT_VINFO_GATHER_SCATTER_P (stmtinfo_b))
311 if (dump_enabled_p ())
313 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
314 "versioning for alias not supported for: "
315 "bad dist vector for ");
316 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
317 DR_REF (dra));
318 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
319 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
320 DR_REF (drb));
321 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
323 return true;
326 if (dump_enabled_p ())
328 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
329 "versioning for alias required: "
330 "bad dist vector for ");
331 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
332 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
333 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
334 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
336 /* Add to list of ddrs that need to be tested at run-time. */
337 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
340 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
341 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
343 int dist = dist_v[loop_depth];
345 if (dump_enabled_p ())
346 dump_printf_loc (MSG_NOTE, vect_location,
347 "dependence distance = %d.\n", dist);
349 if (dist == 0)
351 if (dump_enabled_p ())
353 dump_printf_loc (MSG_NOTE, vect_location,
354 "dependence distance == 0 between ");
355 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
356 dump_printf (MSG_NOTE, " and ");
357 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
358 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
361 /* When we perform grouped accesses and perform implicit CSE
362 by detecting equal accesses and doing disambiguation with
363 runtime alias tests like for
364 .. = a[i];
365 .. = a[i+1];
366 a[i] = ..;
367 a[i+1] = ..;
368 *p = ..;
369 .. = a[i];
370 .. = a[i+1];
371 where we will end up loading { a[i], a[i+1] } once, make
372 sure that inserting group loads before the first load and
373 stores after the last store will do the right thing.
374 Similar for groups like
375 a[i] = ...;
376 ... = a[i];
377 a[i+1] = ...;
378 where loads from the group interleave with the store. */
379 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
380 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
382 gimple *earlier_stmt;
383 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
384 if (DR_IS_WRITE
385 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
387 if (dump_enabled_p ())
388 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
389 "READ_WRITE dependence in interleaving."
390 "\n");
391 return true;
395 continue;
398 if (dist > 0 && DDR_REVERSED_P (ddr))
400 /* If DDR_REVERSED_P the order of the data-refs in DDR was
401 reversed (to make distance vector positive), and the actual
402 distance is negative. */
403 if (dump_enabled_p ())
404 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
405 "dependence distance negative.\n");
406 /* Record a negative dependence distance to later limit the
407 amount of stmt copying / unrolling we can perform.
408 Only need to handle read-after-write dependence. */
409 if (DR_IS_READ (drb)
410 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
411 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
412 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
413 continue;
416 if (abs (dist) >= 2
417 && abs (dist) < *max_vf)
419 /* The dependence distance requires reduction of the maximal
420 vectorization factor. */
421 *max_vf = abs (dist);
422 if (dump_enabled_p ())
423 dump_printf_loc (MSG_NOTE, vect_location,
424 "adjusting maximal vectorization factor to %i\n",
425 *max_vf);
428 if (abs (dist) >= *max_vf)
430 /* Dependence distance does not create dependence, as far as
431 vectorization is concerned, in this case. */
432 if (dump_enabled_p ())
433 dump_printf_loc (MSG_NOTE, vect_location,
434 "dependence distance >= VF.\n");
435 continue;
438 if (dump_enabled_p ())
440 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
441 "not vectorized, possible dependence "
442 "between data-refs ");
443 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
444 dump_printf (MSG_NOTE, " and ");
445 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
446 dump_printf (MSG_NOTE, "\n");
449 return true;
452 return false;
455 /* Function vect_analyze_data_ref_dependences.
457 Examine all the data references in the loop, and make sure there do not
458 exist any data dependences between them. Set *MAX_VF according to
459 the maximum vectorization factor the data dependences allow. */
461 bool
462 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
464 unsigned int i;
465 struct data_dependence_relation *ddr;
467 if (dump_enabled_p ())
468 dump_printf_loc (MSG_NOTE, vect_location,
469 "=== vect_analyze_data_ref_dependences ===\n");
471 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
472 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
473 &LOOP_VINFO_DDRS (loop_vinfo),
474 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
475 return false;
477 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
478 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
479 return false;
481 return true;
485 /* Function vect_slp_analyze_data_ref_dependence.
487 Return TRUE if there (might) exist a dependence between a memory-reference
488 DRA and a memory-reference DRB. When versioning for alias may check a
489 dependence at run-time, return FALSE. Adjust *MAX_VF according to
490 the data dependence. */
492 static bool
493 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
495 struct data_reference *dra = DDR_A (ddr);
496 struct data_reference *drb = DDR_B (ddr);
498 /* We need to check dependences of statements marked as unvectorizable
499 as well, they still can prohibit vectorization. */
501 /* Independent data accesses. */
502 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
503 return false;
505 if (dra == drb)
506 return false;
508 /* Read-read is OK. */
509 if (DR_IS_READ (dra) && DR_IS_READ (drb))
510 return false;
512 /* If dra and drb are part of the same interleaving chain consider
513 them independent. */
514 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
515 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
516 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
517 return false;
519 /* Unknown data dependence. */
520 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
522 if (dump_enabled_p ())
524 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
525 "can't determine dependence between ");
526 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
527 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
528 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
529 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
532 else if (dump_enabled_p ())
534 dump_printf_loc (MSG_NOTE, vect_location,
535 "determined dependence between ");
536 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
537 dump_printf (MSG_NOTE, " and ");
538 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
539 dump_printf (MSG_NOTE, "\n");
542 /* We do not vectorize basic blocks with write-write dependencies. */
543 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
544 return true;
546 /* If we have a read-write dependence check that the load is before the store.
547 When we vectorize basic blocks, vector load can be only before
548 corresponding scalar load, and vector store can be only after its
549 corresponding scalar store. So the order of the acceses is preserved in
550 case the load is before the store. */
551 gimple *earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
552 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
554 /* That only holds for load-store pairs taking part in vectorization. */
555 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
556 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
557 return false;
560 return true;
564 /* Function vect_analyze_data_ref_dependences.
566 Examine all the data references in the basic-block, and make sure there
567 do not exist any data dependences between them. Set *MAX_VF according to
568 the maximum vectorization factor the data dependences allow. */
570 bool
571 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
573 struct data_dependence_relation *ddr;
574 unsigned int i;
576 if (dump_enabled_p ())
577 dump_printf_loc (MSG_NOTE, vect_location,
578 "=== vect_slp_analyze_data_ref_dependences ===\n");
580 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
581 &BB_VINFO_DDRS (bb_vinfo),
582 vNULL, true))
583 return false;
585 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
586 if (vect_slp_analyze_data_ref_dependence (ddr))
587 return false;
589 return true;
593 /* Function vect_compute_data_ref_alignment
595 Compute the misalignment of the data reference DR.
597 Output:
598 1. If during the misalignment computation it is found that the data reference
599 cannot be vectorized then false is returned.
600 2. DR_MISALIGNMENT (DR) is defined.
602 FOR NOW: No analysis is actually performed. Misalignment is calculated
603 only for trivial cases. TODO. */
605 static bool
606 vect_compute_data_ref_alignment (struct data_reference *dr)
608 gimple *stmt = DR_STMT (dr);
609 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
610 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
611 struct loop *loop = NULL;
612 tree ref = DR_REF (dr);
613 tree vectype;
614 tree base, base_addr;
615 tree misalign = NULL_TREE;
616 tree aligned_to;
617 unsigned HOST_WIDE_INT alignment;
619 if (dump_enabled_p ())
620 dump_printf_loc (MSG_NOTE, vect_location,
621 "vect_compute_data_ref_alignment:\n");
623 if (loop_vinfo)
624 loop = LOOP_VINFO_LOOP (loop_vinfo);
626 /* Initialize misalignment to unknown. */
627 SET_DR_MISALIGNMENT (dr, -1);
629 /* Strided accesses perform only component accesses, misalignment information
630 is irrelevant for them. */
631 if (STMT_VINFO_STRIDED_P (stmt_info)
632 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
633 return true;
635 if (tree_fits_shwi_p (DR_STEP (dr)))
636 misalign = DR_INIT (dr);
637 aligned_to = DR_ALIGNED_TO (dr);
638 base_addr = DR_BASE_ADDRESS (dr);
639 vectype = STMT_VINFO_VECTYPE (stmt_info);
641 /* In case the dataref is in an inner-loop of the loop that is being
642 vectorized (LOOP), we use the base and misalignment information
643 relative to the outer-loop (LOOP). This is ok only if the misalignment
644 stays the same throughout the execution of the inner-loop, which is why
645 we have to check that the stride of the dataref in the inner-loop evenly
646 divides by the vector size. */
647 if (loop && nested_in_vect_loop_p (loop, stmt))
649 tree step = DR_STEP (dr);
651 if (tree_fits_shwi_p (step)
652 && tree_to_shwi (step) % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
654 if (dump_enabled_p ())
655 dump_printf_loc (MSG_NOTE, vect_location,
656 "inner step divides the vector-size.\n");
657 misalign = STMT_VINFO_DR_INIT (stmt_info);
658 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
659 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
661 else
663 if (dump_enabled_p ())
664 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
665 "inner step doesn't divide the vector-size.\n");
666 misalign = NULL_TREE;
670 /* Similarly we can only use base and misalignment information relative to
671 an innermost loop if the misalignment stays the same throughout the
672 execution of the loop. As above, this is the case if the stride of
673 the dataref evenly divides by the vector size. */
674 else
676 tree step = DR_STEP (dr);
677 unsigned vf = loop ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
679 if (tree_fits_shwi_p (step)
680 && ((tree_to_shwi (step) * vf)
681 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
683 if (dump_enabled_p ())
684 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
685 "step doesn't divide the vector-size.\n");
686 misalign = NULL_TREE;
690 /* To look at alignment of the base we have to preserve an inner MEM_REF
691 as that carries alignment information of the actual access. */
692 base = ref;
693 while (handled_component_p (base))
694 base = TREE_OPERAND (base, 0);
695 if (TREE_CODE (base) == MEM_REF)
696 base = build2 (MEM_REF, TREE_TYPE (base), base_addr,
697 build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)), 0));
698 unsigned int base_alignment = get_object_alignment (base);
700 if (base_alignment >= TYPE_ALIGN (TREE_TYPE (vectype)))
701 DR_VECT_AUX (dr)->base_element_aligned = true;
703 alignment = TYPE_ALIGN_UNIT (vectype);
705 if ((compare_tree_int (aligned_to, alignment) < 0)
706 || !misalign)
708 if (dump_enabled_p ())
710 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
711 "Unknown alignment for access: ");
712 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
713 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
715 return true;
718 if (base_alignment < TYPE_ALIGN (vectype))
720 /* Strip an inner MEM_REF to a bare decl if possible. */
721 if (TREE_CODE (base) == MEM_REF
722 && integer_zerop (TREE_OPERAND (base, 1))
723 && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
724 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
726 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
728 if (dump_enabled_p ())
730 dump_printf_loc (MSG_NOTE, vect_location,
731 "can't force alignment of ref: ");
732 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
733 dump_printf (MSG_NOTE, "\n");
735 return true;
738 /* Force the alignment of the decl.
739 NOTE: This is the only change to the code we make during
740 the analysis phase, before deciding to vectorize the loop. */
741 if (dump_enabled_p ())
743 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
744 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
745 dump_printf (MSG_NOTE, "\n");
748 DR_VECT_AUX (dr)->base_decl = base;
749 DR_VECT_AUX (dr)->base_misaligned = true;
750 DR_VECT_AUX (dr)->base_element_aligned = true;
753 /* If this is a backward running DR then first access in the larger
754 vectype actually is N-1 elements before the address in the DR.
755 Adjust misalign accordingly. */
756 if (tree_int_cst_sgn (DR_STEP (dr)) < 0)
758 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
759 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
760 otherwise we wouldn't be here. */
761 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
762 /* PLUS because DR_STEP was negative. */
763 misalign = size_binop (PLUS_EXPR, misalign, offset);
766 SET_DR_MISALIGNMENT (dr,
767 wi::mod_floor (misalign, alignment, SIGNED).to_uhwi ());
769 if (dump_enabled_p ())
771 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
772 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
773 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
774 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
777 return true;
781 /* Function vect_compute_data_refs_alignment
783 Compute the misalignment of data references in the loop.
784 Return FALSE if a data reference is found that cannot be vectorized. */
786 static bool
787 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
788 bb_vec_info bb_vinfo)
790 vec<data_reference_p> datarefs;
791 struct data_reference *dr;
792 unsigned int i;
794 if (loop_vinfo)
795 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
796 else
797 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
799 FOR_EACH_VEC_ELT (datarefs, i, dr)
800 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
801 && !vect_compute_data_ref_alignment (dr))
803 if (bb_vinfo)
805 /* Mark unsupported statement as unvectorizable. */
806 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
807 continue;
809 else
810 return false;
813 return true;
817 /* Function vect_update_misalignment_for_peel
819 DR - the data reference whose misalignment is to be adjusted.
820 DR_PEEL - the data reference whose misalignment is being made
821 zero in the vector loop by the peel.
822 NPEEL - the number of iterations in the peel loop if the misalignment
823 of DR_PEEL is known at compile time. */
825 static void
826 vect_update_misalignment_for_peel (struct data_reference *dr,
827 struct data_reference *dr_peel, int npeel)
829 unsigned int i;
830 vec<dr_p> same_align_drs;
831 struct data_reference *current_dr;
832 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
833 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
834 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
835 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
837 /* For interleaved data accesses the step in the loop must be multiplied by
838 the size of the interleaving group. */
839 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
840 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
841 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
842 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
844 /* It can be assumed that the data refs with the same alignment as dr_peel
845 are aligned in the vector loop. */
846 same_align_drs
847 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
848 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
850 if (current_dr != dr)
851 continue;
852 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
853 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
854 SET_DR_MISALIGNMENT (dr, 0);
855 return;
858 if (known_alignment_for_access_p (dr)
859 && known_alignment_for_access_p (dr_peel))
861 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
862 int misal = DR_MISALIGNMENT (dr);
863 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
864 misal += negative ? -npeel * dr_size : npeel * dr_size;
865 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
866 SET_DR_MISALIGNMENT (dr, misal);
867 return;
870 if (dump_enabled_p ())
871 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
872 SET_DR_MISALIGNMENT (dr, -1);
876 /* Function vect_verify_datarefs_alignment
878 Return TRUE if all data references in the loop can be
879 handled with respect to alignment. */
881 bool
882 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
884 vec<data_reference_p> datarefs;
885 struct data_reference *dr;
886 enum dr_alignment_support supportable_dr_alignment;
887 unsigned int i;
889 if (loop_vinfo)
890 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
891 else
892 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
894 FOR_EACH_VEC_ELT (datarefs, i, dr)
896 gimple *stmt = DR_STMT (dr);
897 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
899 if (!STMT_VINFO_RELEVANT_P (stmt_info))
900 continue;
902 /* For interleaving, only the alignment of the first access matters.
903 Skip statements marked as not vectorizable. */
904 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
905 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
906 || !STMT_VINFO_VECTORIZABLE (stmt_info))
907 continue;
909 /* Strided accesses perform only component accesses, alignment is
910 irrelevant for them. */
911 if (STMT_VINFO_STRIDED_P (stmt_info)
912 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
913 continue;
915 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
916 if (!supportable_dr_alignment)
918 if (dump_enabled_p ())
920 if (DR_IS_READ (dr))
921 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
922 "not vectorized: unsupported unaligned load.");
923 else
924 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
925 "not vectorized: unsupported unaligned "
926 "store.");
928 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
929 DR_REF (dr));
930 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
932 return false;
934 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
935 dump_printf_loc (MSG_NOTE, vect_location,
936 "Vectorizing an unaligned access.\n");
938 return true;
941 /* Given an memory reference EXP return whether its alignment is less
942 than its size. */
944 static bool
945 not_size_aligned (tree exp)
947 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
948 return true;
950 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
951 > get_object_alignment (exp));
954 /* Function vector_alignment_reachable_p
956 Return true if vector alignment for DR is reachable by peeling
957 a few loop iterations. Return false otherwise. */
959 static bool
960 vector_alignment_reachable_p (struct data_reference *dr)
962 gimple *stmt = DR_STMT (dr);
963 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
964 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
966 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
968 /* For interleaved access we peel only if number of iterations in
969 the prolog loop ({VF - misalignment}), is a multiple of the
970 number of the interleaved accesses. */
971 int elem_size, mis_in_elements;
972 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
974 /* FORNOW: handle only known alignment. */
975 if (!known_alignment_for_access_p (dr))
976 return false;
978 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
979 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
981 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
982 return false;
985 /* If misalignment is known at the compile time then allow peeling
986 only if natural alignment is reachable through peeling. */
987 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
989 HOST_WIDE_INT elmsize =
990 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
991 if (dump_enabled_p ())
993 dump_printf_loc (MSG_NOTE, vect_location,
994 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
995 dump_printf (MSG_NOTE,
996 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
998 if (DR_MISALIGNMENT (dr) % elmsize)
1000 if (dump_enabled_p ())
1001 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1002 "data size does not divide the misalignment.\n");
1003 return false;
1007 if (!known_alignment_for_access_p (dr))
1009 tree type = TREE_TYPE (DR_REF (dr));
1010 bool is_packed = not_size_aligned (DR_REF (dr));
1011 if (dump_enabled_p ())
1012 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1013 "Unknown misalignment, is_packed = %d\n",is_packed);
1014 if ((TYPE_USER_ALIGN (type) && !is_packed)
1015 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1016 return true;
1017 else
1018 return false;
1021 return true;
1025 /* Calculate the cost of the memory access represented by DR. */
1027 static void
1028 vect_get_data_access_cost (struct data_reference *dr,
1029 unsigned int *inside_cost,
1030 unsigned int *outside_cost,
1031 stmt_vector_for_cost *body_cost_vec)
1033 gimple *stmt = DR_STMT (dr);
1034 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1035 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1036 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1037 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1038 int ncopies = vf / nunits;
1040 if (DR_IS_READ (dr))
1041 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1042 NULL, body_cost_vec, false);
1043 else
1044 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1046 if (dump_enabled_p ())
1047 dump_printf_loc (MSG_NOTE, vect_location,
1048 "vect_get_data_access_cost: inside_cost = %d, "
1049 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1053 /* Insert DR into peeling hash table with NPEEL as key. */
1055 static void
1056 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1057 int npeel)
1059 struct _vect_peel_info elem, *slot;
1060 _vect_peel_info **new_slot;
1061 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1063 elem.npeel = npeel;
1064 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find (&elem);
1065 if (slot)
1066 slot->count++;
1067 else
1069 slot = XNEW (struct _vect_peel_info);
1070 slot->npeel = npeel;
1071 slot->dr = dr;
1072 slot->count = 1;
1073 new_slot
1074 = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find_slot (slot, INSERT);
1075 *new_slot = slot;
1078 if (!supportable_dr_alignment
1079 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1080 slot->count += VECT_MAX_COST;
1084 /* Traverse peeling hash table to find peeling option that aligns maximum
1085 number of data accesses. */
1088 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1089 _vect_peel_extended_info *max)
1091 vect_peel_info elem = *slot;
1093 if (elem->count > max->peel_info.count
1094 || (elem->count == max->peel_info.count
1095 && max->peel_info.npeel > elem->npeel))
1097 max->peel_info.npeel = elem->npeel;
1098 max->peel_info.count = elem->count;
1099 max->peel_info.dr = elem->dr;
1102 return 1;
1106 /* Traverse peeling hash table and calculate cost for each peeling option.
1107 Find the one with the lowest cost. */
1110 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1111 _vect_peel_extended_info *min)
1113 vect_peel_info elem = *slot;
1114 int save_misalignment, dummy;
1115 unsigned int inside_cost = 0, outside_cost = 0, i;
1116 gimple *stmt = DR_STMT (elem->dr);
1117 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1118 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1119 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1120 struct data_reference *dr;
1121 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1123 prologue_cost_vec.create (2);
1124 body_cost_vec.create (2);
1125 epilogue_cost_vec.create (2);
1127 FOR_EACH_VEC_ELT (datarefs, i, dr)
1129 stmt = DR_STMT (dr);
1130 stmt_info = vinfo_for_stmt (stmt);
1131 /* For interleaving, only the alignment of the first access
1132 matters. */
1133 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1134 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1135 continue;
1137 save_misalignment = DR_MISALIGNMENT (dr);
1138 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1139 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1140 &body_cost_vec);
1141 SET_DR_MISALIGNMENT (dr, save_misalignment);
1144 outside_cost += vect_get_known_peeling_cost
1145 (loop_vinfo, elem->npeel, &dummy,
1146 &LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
1147 &prologue_cost_vec, &epilogue_cost_vec);
1149 /* Prologue and epilogue costs are added to the target model later.
1150 These costs depend only on the scalar iteration cost, the
1151 number of peeling iterations finally chosen, and the number of
1152 misaligned statements. So discard the information found here. */
1153 prologue_cost_vec.release ();
1154 epilogue_cost_vec.release ();
1156 if (inside_cost < min->inside_cost
1157 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1159 min->inside_cost = inside_cost;
1160 min->outside_cost = outside_cost;
1161 min->body_cost_vec.release ();
1162 min->body_cost_vec = body_cost_vec;
1163 min->peel_info.dr = elem->dr;
1164 min->peel_info.npeel = elem->npeel;
1166 else
1167 body_cost_vec.release ();
1169 return 1;
1173 /* Choose best peeling option by traversing peeling hash table and either
1174 choosing an option with the lowest cost (if cost model is enabled) or the
1175 option that aligns as many accesses as possible. */
1177 static struct data_reference *
1178 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1179 unsigned int *npeel,
1180 stmt_vector_for_cost *body_cost_vec)
1182 struct _vect_peel_extended_info res;
1184 res.peel_info.dr = NULL;
1185 res.body_cost_vec = stmt_vector_for_cost ();
1187 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1189 res.inside_cost = INT_MAX;
1190 res.outside_cost = INT_MAX;
1191 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1192 ->traverse <_vect_peel_extended_info *,
1193 vect_peeling_hash_get_lowest_cost> (&res);
1195 else
1197 res.peel_info.count = 0;
1198 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1199 ->traverse <_vect_peel_extended_info *,
1200 vect_peeling_hash_get_most_frequent> (&res);
1203 *npeel = res.peel_info.npeel;
1204 *body_cost_vec = res.body_cost_vec;
1205 return res.peel_info.dr;
1209 /* Function vect_enhance_data_refs_alignment
1211 This pass will use loop versioning and loop peeling in order to enhance
1212 the alignment of data references in the loop.
1214 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1215 original loop is to be vectorized. Any other loops that are created by
1216 the transformations performed in this pass - are not supposed to be
1217 vectorized. This restriction will be relaxed.
1219 This pass will require a cost model to guide it whether to apply peeling
1220 or versioning or a combination of the two. For example, the scheme that
1221 intel uses when given a loop with several memory accesses, is as follows:
1222 choose one memory access ('p') which alignment you want to force by doing
1223 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1224 other accesses are not necessarily aligned, or (2) use loop versioning to
1225 generate one loop in which all accesses are aligned, and another loop in
1226 which only 'p' is necessarily aligned.
1228 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1229 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1230 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1232 Devising a cost model is the most critical aspect of this work. It will
1233 guide us on which access to peel for, whether to use loop versioning, how
1234 many versions to create, etc. The cost model will probably consist of
1235 generic considerations as well as target specific considerations (on
1236 powerpc for example, misaligned stores are more painful than misaligned
1237 loads).
1239 Here are the general steps involved in alignment enhancements:
1241 -- original loop, before alignment analysis:
1242 for (i=0; i<N; i++){
1243 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1244 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1247 -- After vect_compute_data_refs_alignment:
1248 for (i=0; i<N; i++){
1249 x = q[i]; # DR_MISALIGNMENT(q) = 3
1250 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1253 -- Possibility 1: we do loop versioning:
1254 if (p is aligned) {
1255 for (i=0; i<N; i++){ # loop 1A
1256 x = q[i]; # DR_MISALIGNMENT(q) = 3
1257 p[i] = y; # DR_MISALIGNMENT(p) = 0
1260 else {
1261 for (i=0; i<N; i++){ # loop 1B
1262 x = q[i]; # DR_MISALIGNMENT(q) = 3
1263 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1267 -- Possibility 2: we do loop peeling:
1268 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1269 x = q[i];
1270 p[i] = y;
1272 for (i = 3; i < N; i++){ # loop 2A
1273 x = q[i]; # DR_MISALIGNMENT(q) = 0
1274 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1277 -- Possibility 3: combination of loop peeling and versioning:
1278 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1279 x = q[i];
1280 p[i] = y;
1282 if (p is aligned) {
1283 for (i = 3; i<N; i++){ # loop 3A
1284 x = q[i]; # DR_MISALIGNMENT(q) = 0
1285 p[i] = y; # DR_MISALIGNMENT(p) = 0
1288 else {
1289 for (i = 3; i<N; i++){ # loop 3B
1290 x = q[i]; # DR_MISALIGNMENT(q) = 0
1291 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1295 These loops are later passed to loop_transform to be vectorized. The
1296 vectorizer will use the alignment information to guide the transformation
1297 (whether to generate regular loads/stores, or with special handling for
1298 misalignment). */
1300 bool
1301 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1303 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1304 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1305 enum dr_alignment_support supportable_dr_alignment;
1306 struct data_reference *dr0 = NULL, *first_store = NULL;
1307 struct data_reference *dr;
1308 unsigned int i, j;
1309 bool do_peeling = false;
1310 bool do_versioning = false;
1311 bool stat;
1312 gimple *stmt;
1313 stmt_vec_info stmt_info;
1314 unsigned int npeel = 0;
1315 bool all_misalignments_unknown = true;
1316 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1317 unsigned possible_npeel_number = 1;
1318 tree vectype;
1319 unsigned int nelements, mis, same_align_drs_max = 0;
1320 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1322 if (dump_enabled_p ())
1323 dump_printf_loc (MSG_NOTE, vect_location,
1324 "=== vect_enhance_data_refs_alignment ===\n");
1326 /* While cost model enhancements are expected in the future, the high level
1327 view of the code at this time is as follows:
1329 A) If there is a misaligned access then see if peeling to align
1330 this access can make all data references satisfy
1331 vect_supportable_dr_alignment. If so, update data structures
1332 as needed and return true.
1334 B) If peeling wasn't possible and there is a data reference with an
1335 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1336 then see if loop versioning checks can be used to make all data
1337 references satisfy vect_supportable_dr_alignment. If so, update
1338 data structures as needed and return true.
1340 C) If neither peeling nor versioning were successful then return false if
1341 any data reference does not satisfy vect_supportable_dr_alignment.
1343 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1345 Note, Possibility 3 above (which is peeling and versioning together) is not
1346 being done at this time. */
1348 /* (1) Peeling to force alignment. */
1350 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1351 Considerations:
1352 + How many accesses will become aligned due to the peeling
1353 - How many accesses will become unaligned due to the peeling,
1354 and the cost of misaligned accesses.
1355 - The cost of peeling (the extra runtime checks, the increase
1356 in code size). */
1358 FOR_EACH_VEC_ELT (datarefs, i, dr)
1360 stmt = DR_STMT (dr);
1361 stmt_info = vinfo_for_stmt (stmt);
1363 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1364 continue;
1366 /* For interleaving, only the alignment of the first access
1367 matters. */
1368 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1369 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1370 continue;
1372 /* For invariant accesses there is nothing to enhance. */
1373 if (integer_zerop (DR_STEP (dr)))
1374 continue;
1376 /* Strided accesses perform only component accesses, alignment is
1377 irrelevant for them. */
1378 if (STMT_VINFO_STRIDED_P (stmt_info)
1379 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1380 continue;
1382 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1383 do_peeling = vector_alignment_reachable_p (dr);
1384 if (do_peeling)
1386 if (known_alignment_for_access_p (dr))
1388 unsigned int npeel_tmp;
1389 bool negative = tree_int_cst_compare (DR_STEP (dr),
1390 size_zero_node) < 0;
1392 /* Save info about DR in the hash table. */
1393 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo))
1394 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1395 = new hash_table<peel_info_hasher> (1);
1397 vectype = STMT_VINFO_VECTYPE (stmt_info);
1398 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1399 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1400 TREE_TYPE (DR_REF (dr))));
1401 npeel_tmp = (negative
1402 ? (mis - nelements) : (nelements - mis))
1403 & (nelements - 1);
1405 /* For multiple types, it is possible that the bigger type access
1406 will have more than one peeling option. E.g., a loop with two
1407 types: one of size (vector size / 4), and the other one of
1408 size (vector size / 8). Vectorization factor will 8. If both
1409 access are misaligned by 3, the first one needs one scalar
1410 iteration to be aligned, and the second one needs 5. But the
1411 the first one will be aligned also by peeling 5 scalar
1412 iterations, and in that case both accesses will be aligned.
1413 Hence, except for the immediate peeling amount, we also want
1414 to try to add full vector size, while we don't exceed
1415 vectorization factor.
1416 We do this automtically for cost model, since we calculate cost
1417 for every peeling option. */
1418 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1420 if (STMT_SLP_TYPE (stmt_info))
1421 possible_npeel_number
1422 = (vf * GROUP_SIZE (stmt_info)) / nelements;
1423 else
1424 possible_npeel_number = vf / nelements;
1427 /* Handle the aligned case. We may decide to align some other
1428 access, making DR unaligned. */
1429 if (DR_MISALIGNMENT (dr) == 0)
1431 npeel_tmp = 0;
1432 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1433 possible_npeel_number++;
1436 for (j = 0; j < possible_npeel_number; j++)
1438 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1439 npeel_tmp += nelements;
1442 all_misalignments_unknown = false;
1443 /* Data-ref that was chosen for the case that all the
1444 misalignments are unknown is not relevant anymore, since we
1445 have a data-ref with known alignment. */
1446 dr0 = NULL;
1448 else
1450 /* If we don't know any misalignment values, we prefer
1451 peeling for data-ref that has the maximum number of data-refs
1452 with the same alignment, unless the target prefers to align
1453 stores over load. */
1454 if (all_misalignments_unknown)
1456 unsigned same_align_drs
1457 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1458 if (!dr0
1459 || same_align_drs_max < same_align_drs)
1461 same_align_drs_max = same_align_drs;
1462 dr0 = dr;
1464 /* For data-refs with the same number of related
1465 accesses prefer the one where the misalign
1466 computation will be invariant in the outermost loop. */
1467 else if (same_align_drs_max == same_align_drs)
1469 struct loop *ivloop0, *ivloop;
1470 ivloop0 = outermost_invariant_loop_for_expr
1471 (loop, DR_BASE_ADDRESS (dr0));
1472 ivloop = outermost_invariant_loop_for_expr
1473 (loop, DR_BASE_ADDRESS (dr));
1474 if ((ivloop && !ivloop0)
1475 || (ivloop && ivloop0
1476 && flow_loop_nested_p (ivloop, ivloop0)))
1477 dr0 = dr;
1480 if (!first_store && DR_IS_WRITE (dr))
1481 first_store = dr;
1484 /* If there are both known and unknown misaligned accesses in the
1485 loop, we choose peeling amount according to the known
1486 accesses. */
1487 if (!supportable_dr_alignment)
1489 dr0 = dr;
1490 if (!first_store && DR_IS_WRITE (dr))
1491 first_store = dr;
1495 else
1497 if (!aligned_access_p (dr))
1499 if (dump_enabled_p ())
1500 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1501 "vector alignment may not be reachable\n");
1502 break;
1507 /* Check if we can possibly peel the loop. */
1508 if (!vect_can_advance_ivs_p (loop_vinfo)
1509 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop))
1510 || loop->inner)
1511 do_peeling = false;
1513 if (do_peeling
1514 && all_misalignments_unknown
1515 && vect_supportable_dr_alignment (dr0, false))
1517 /* Check if the target requires to prefer stores over loads, i.e., if
1518 misaligned stores are more expensive than misaligned loads (taking
1519 drs with same alignment into account). */
1520 if (first_store && DR_IS_READ (dr0))
1522 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1523 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1524 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1525 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1526 stmt_vector_for_cost dummy;
1527 dummy.create (2);
1529 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1530 &dummy);
1531 vect_get_data_access_cost (first_store, &store_inside_cost,
1532 &store_outside_cost, &dummy);
1534 dummy.release ();
1536 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1537 aligning the load DR0). */
1538 load_inside_penalty = store_inside_cost;
1539 load_outside_penalty = store_outside_cost;
1540 for (i = 0;
1541 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1542 DR_STMT (first_store))).iterate (i, &dr);
1543 i++)
1544 if (DR_IS_READ (dr))
1546 load_inside_penalty += load_inside_cost;
1547 load_outside_penalty += load_outside_cost;
1549 else
1551 load_inside_penalty += store_inside_cost;
1552 load_outside_penalty += store_outside_cost;
1555 /* Calculate the penalty for leaving DR0 unaligned (by
1556 aligning the FIRST_STORE). */
1557 store_inside_penalty = load_inside_cost;
1558 store_outside_penalty = load_outside_cost;
1559 for (i = 0;
1560 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1561 DR_STMT (dr0))).iterate (i, &dr);
1562 i++)
1563 if (DR_IS_READ (dr))
1565 store_inside_penalty += load_inside_cost;
1566 store_outside_penalty += load_outside_cost;
1568 else
1570 store_inside_penalty += store_inside_cost;
1571 store_outside_penalty += store_outside_cost;
1574 if (load_inside_penalty > store_inside_penalty
1575 || (load_inside_penalty == store_inside_penalty
1576 && load_outside_penalty > store_outside_penalty))
1577 dr0 = first_store;
1580 /* In case there are only loads with different unknown misalignments, use
1581 peeling only if it may help to align other accesses in the loop or
1582 if it may help improving load bandwith when we'd end up using
1583 unaligned loads. */
1584 tree dr0_vt = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr0)));
1585 if (!first_store
1586 && !STMT_VINFO_SAME_ALIGN_REFS (
1587 vinfo_for_stmt (DR_STMT (dr0))).length ()
1588 && (vect_supportable_dr_alignment (dr0, false)
1589 != dr_unaligned_supported
1590 || (builtin_vectorization_cost (vector_load, dr0_vt, 0)
1591 == builtin_vectorization_cost (unaligned_load, dr0_vt, -1))))
1592 do_peeling = false;
1595 if (do_peeling && !dr0)
1597 /* Peeling is possible, but there is no data access that is not supported
1598 unless aligned. So we try to choose the best possible peeling. */
1600 /* We should get here only if there are drs with known misalignment. */
1601 gcc_assert (!all_misalignments_unknown);
1603 /* Choose the best peeling from the hash table. */
1604 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1605 &body_cost_vec);
1606 if (!dr0 || !npeel)
1607 do_peeling = false;
1610 if (do_peeling)
1612 stmt = DR_STMT (dr0);
1613 stmt_info = vinfo_for_stmt (stmt);
1614 vectype = STMT_VINFO_VECTYPE (stmt_info);
1615 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1617 if (known_alignment_for_access_p (dr0))
1619 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1620 size_zero_node) < 0;
1621 if (!npeel)
1623 /* Since it's known at compile time, compute the number of
1624 iterations in the peeled loop (the peeling factor) for use in
1625 updating DR_MISALIGNMENT values. The peeling factor is the
1626 vectorization factor minus the misalignment as an element
1627 count. */
1628 mis = DR_MISALIGNMENT (dr0);
1629 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1630 npeel = ((negative ? mis - nelements : nelements - mis)
1631 & (nelements - 1));
1634 /* For interleaved data access every iteration accesses all the
1635 members of the group, therefore we divide the number of iterations
1636 by the group size. */
1637 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1638 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1639 npeel /= GROUP_SIZE (stmt_info);
1641 if (dump_enabled_p ())
1642 dump_printf_loc (MSG_NOTE, vect_location,
1643 "Try peeling by %d\n", npeel);
1646 /* Ensure that all data refs can be vectorized after the peel. */
1647 FOR_EACH_VEC_ELT (datarefs, i, dr)
1649 int save_misalignment;
1651 if (dr == dr0)
1652 continue;
1654 stmt = DR_STMT (dr);
1655 stmt_info = vinfo_for_stmt (stmt);
1656 /* For interleaving, only the alignment of the first access
1657 matters. */
1658 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1659 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1660 continue;
1662 /* Strided accesses perform only component accesses, alignment is
1663 irrelevant for them. */
1664 if (STMT_VINFO_STRIDED_P (stmt_info)
1665 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1666 continue;
1668 save_misalignment = DR_MISALIGNMENT (dr);
1669 vect_update_misalignment_for_peel (dr, dr0, npeel);
1670 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1671 SET_DR_MISALIGNMENT (dr, save_misalignment);
1673 if (!supportable_dr_alignment)
1675 do_peeling = false;
1676 break;
1680 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1682 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1683 if (!stat)
1684 do_peeling = false;
1685 else
1687 body_cost_vec.release ();
1688 return stat;
1692 /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
1693 if (do_peeling)
1695 unsigned max_allowed_peel
1696 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1697 if (max_allowed_peel != (unsigned)-1)
1699 unsigned max_peel = npeel;
1700 if (max_peel == 0)
1702 gimple *dr_stmt = DR_STMT (dr0);
1703 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1704 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1705 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1707 if (max_peel > max_allowed_peel)
1709 do_peeling = false;
1710 if (dump_enabled_p ())
1711 dump_printf_loc (MSG_NOTE, vect_location,
1712 "Disable peeling, max peels reached: %d\n", max_peel);
1717 /* Cost model #2 - if peeling may result in a remaining loop not
1718 iterating enough to be vectorized then do not peel. */
1719 if (do_peeling
1720 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
1722 unsigned max_peel
1723 = npeel == 0 ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1 : npeel;
1724 if (LOOP_VINFO_INT_NITERS (loop_vinfo)
1725 < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + max_peel)
1726 do_peeling = false;
1729 if (do_peeling)
1731 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1732 If the misalignment of DR_i is identical to that of dr0 then set
1733 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1734 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1735 by the peeling factor times the element size of DR_i (MOD the
1736 vectorization factor times the size). Otherwise, the
1737 misalignment of DR_i must be set to unknown. */
1738 FOR_EACH_VEC_ELT (datarefs, i, dr)
1739 if (dr != dr0)
1740 vect_update_misalignment_for_peel (dr, dr0, npeel);
1742 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1743 if (npeel)
1744 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1745 else
1746 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1747 = DR_MISALIGNMENT (dr0);
1748 SET_DR_MISALIGNMENT (dr0, 0);
1749 if (dump_enabled_p ())
1751 dump_printf_loc (MSG_NOTE, vect_location,
1752 "Alignment of access forced using peeling.\n");
1753 dump_printf_loc (MSG_NOTE, vect_location,
1754 "Peeling for alignment will be applied.\n");
1756 /* The inside-loop cost will be accounted for in vectorizable_load
1757 and vectorizable_store correctly with adjusted alignments.
1758 Drop the body_cst_vec on the floor here. */
1759 body_cost_vec.release ();
1761 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1762 gcc_assert (stat);
1763 return stat;
1767 body_cost_vec.release ();
1769 /* (2) Versioning to force alignment. */
1771 /* Try versioning if:
1772 1) optimize loop for speed
1773 2) there is at least one unsupported misaligned data ref with an unknown
1774 misalignment, and
1775 3) all misaligned data refs with a known misalignment are supported, and
1776 4) the number of runtime alignment checks is within reason. */
1778 do_versioning =
1779 optimize_loop_nest_for_speed_p (loop)
1780 && (!loop->inner); /* FORNOW */
1782 if (do_versioning)
1784 FOR_EACH_VEC_ELT (datarefs, i, dr)
1786 stmt = DR_STMT (dr);
1787 stmt_info = vinfo_for_stmt (stmt);
1789 /* For interleaving, only the alignment of the first access
1790 matters. */
1791 if (aligned_access_p (dr)
1792 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1793 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1794 continue;
1796 if (STMT_VINFO_STRIDED_P (stmt_info))
1798 /* Strided loads perform only component accesses, alignment is
1799 irrelevant for them. */
1800 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1801 continue;
1802 do_versioning = false;
1803 break;
1806 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1808 if (!supportable_dr_alignment)
1810 gimple *stmt;
1811 int mask;
1812 tree vectype;
1814 if (known_alignment_for_access_p (dr)
1815 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1816 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1818 do_versioning = false;
1819 break;
1822 stmt = DR_STMT (dr);
1823 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1824 gcc_assert (vectype);
1826 /* The rightmost bits of an aligned address must be zeros.
1827 Construct the mask needed for this test. For example,
1828 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1829 mask must be 15 = 0xf. */
1830 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1832 /* FORNOW: use the same mask to test all potentially unaligned
1833 references in the loop. The vectorizer currently supports
1834 a single vector size, see the reference to
1835 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1836 vectorization factor is computed. */
1837 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1838 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1839 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1840 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1841 DR_STMT (dr));
1845 /* Versioning requires at least one misaligned data reference. */
1846 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1847 do_versioning = false;
1848 else if (!do_versioning)
1849 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1852 if (do_versioning)
1854 vec<gimple *> may_misalign_stmts
1855 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1856 gimple *stmt;
1858 /* It can now be assumed that the data references in the statements
1859 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1860 of the loop being vectorized. */
1861 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1863 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1864 dr = STMT_VINFO_DATA_REF (stmt_info);
1865 SET_DR_MISALIGNMENT (dr, 0);
1866 if (dump_enabled_p ())
1867 dump_printf_loc (MSG_NOTE, vect_location,
1868 "Alignment of access forced using versioning.\n");
1871 if (dump_enabled_p ())
1872 dump_printf_loc (MSG_NOTE, vect_location,
1873 "Versioning for alignment will be applied.\n");
1875 /* Peeling and versioning can't be done together at this time. */
1876 gcc_assert (! (do_peeling && do_versioning));
1878 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1879 gcc_assert (stat);
1880 return stat;
1883 /* This point is reached if neither peeling nor versioning is being done. */
1884 gcc_assert (! (do_peeling || do_versioning));
1886 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1887 return stat;
1891 /* Function vect_find_same_alignment_drs.
1893 Update group and alignment relations according to the chosen
1894 vectorization factor. */
1896 static void
1897 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1898 loop_vec_info loop_vinfo)
1900 unsigned int i;
1901 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1902 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1903 struct data_reference *dra = DDR_A (ddr);
1904 struct data_reference *drb = DDR_B (ddr);
1905 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1906 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1907 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1908 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1909 lambda_vector dist_v;
1910 unsigned int loop_depth;
1912 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1913 return;
1915 if (dra == drb)
1916 return;
1918 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1919 return;
1921 /* Loop-based vectorization and known data dependence. */
1922 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1923 return;
1925 /* Data-dependence analysis reports a distance vector of zero
1926 for data-references that overlap only in the first iteration
1927 but have different sign step (see PR45764).
1928 So as a sanity check require equal DR_STEP. */
1929 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1930 return;
1932 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1933 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1935 int dist = dist_v[loop_depth];
1937 if (dump_enabled_p ())
1938 dump_printf_loc (MSG_NOTE, vect_location,
1939 "dependence distance = %d.\n", dist);
1941 /* Same loop iteration. */
1942 if (dist == 0
1943 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1945 /* Two references with distance zero have the same alignment. */
1946 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1947 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1948 if (dump_enabled_p ())
1950 dump_printf_loc (MSG_NOTE, vect_location,
1951 "accesses have the same alignment.\n");
1952 dump_printf (MSG_NOTE,
1953 "dependence distance modulo vf == 0 between ");
1954 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1955 dump_printf (MSG_NOTE, " and ");
1956 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1957 dump_printf (MSG_NOTE, "\n");
1964 /* Function vect_analyze_data_refs_alignment
1966 Analyze the alignment of the data-references in the loop.
1967 Return FALSE if a data reference is found that cannot be vectorized. */
1969 bool
1970 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
1971 bb_vec_info bb_vinfo)
1973 if (dump_enabled_p ())
1974 dump_printf_loc (MSG_NOTE, vect_location,
1975 "=== vect_analyze_data_refs_alignment ===\n");
1977 /* Mark groups of data references with same alignment using
1978 data dependence information. */
1979 if (loop_vinfo)
1981 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
1982 struct data_dependence_relation *ddr;
1983 unsigned int i;
1985 FOR_EACH_VEC_ELT (ddrs, i, ddr)
1986 vect_find_same_alignment_drs (ddr, loop_vinfo);
1989 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
1991 if (dump_enabled_p ())
1992 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1993 "not vectorized: can't calculate alignment "
1994 "for data ref.\n");
1995 return false;
1998 return true;
2002 /* Analyze groups of accesses: check that DR belongs to a group of
2003 accesses of legal size, step, etc. Detect gaps, single element
2004 interleaving, and other special cases. Set grouped access info.
2005 Collect groups of strided stores for further use in SLP analysis.
2006 Worker for vect_analyze_group_access. */
2008 static bool
2009 vect_analyze_group_access_1 (struct data_reference *dr)
2011 tree step = DR_STEP (dr);
2012 tree scalar_type = TREE_TYPE (DR_REF (dr));
2013 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2014 gimple *stmt = DR_STMT (dr);
2015 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2016 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2017 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2018 HOST_WIDE_INT dr_step = -1;
2019 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2020 bool slp_impossible = false;
2021 struct loop *loop = NULL;
2023 if (loop_vinfo)
2024 loop = LOOP_VINFO_LOOP (loop_vinfo);
2026 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2027 size of the interleaving group (including gaps). */
2028 if (tree_fits_shwi_p (step))
2030 dr_step = tree_to_shwi (step);
2031 groupsize = absu_hwi (dr_step) / type_size;
2033 else
2034 groupsize = 0;
2036 /* Not consecutive access is possible only if it is a part of interleaving. */
2037 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2039 /* Check if it this DR is a part of interleaving, and is a single
2040 element of the group that is accessed in the loop. */
2042 /* Gaps are supported only for loads. STEP must be a multiple of the type
2043 size. The size of the group must be a power of 2. */
2044 if (DR_IS_READ (dr)
2045 && (dr_step % type_size) == 0
2046 && groupsize > 0
2047 && exact_log2 (groupsize) != -1)
2049 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2050 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2051 if (dump_enabled_p ())
2053 dump_printf_loc (MSG_NOTE, vect_location,
2054 "Detected single element interleaving ");
2055 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2056 dump_printf (MSG_NOTE, " step ");
2057 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2058 dump_printf (MSG_NOTE, "\n");
2061 if (loop_vinfo)
2063 if (dump_enabled_p ())
2064 dump_printf_loc (MSG_NOTE, vect_location,
2065 "Data access with gaps requires scalar "
2066 "epilogue loop\n");
2067 if (loop->inner)
2069 if (dump_enabled_p ())
2070 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2071 "Peeling for outer loop is not"
2072 " supported\n");
2073 return false;
2076 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2079 return true;
2082 if (dump_enabled_p ())
2084 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2085 "not consecutive access ");
2086 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2087 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2090 if (bb_vinfo)
2092 /* Mark the statement as unvectorizable. */
2093 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2094 return true;
2097 return false;
2100 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2102 /* First stmt in the interleaving chain. Check the chain. */
2103 gimple *next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2104 struct data_reference *data_ref = dr;
2105 unsigned int count = 1;
2106 tree prev_init = DR_INIT (data_ref);
2107 gimple *prev = stmt;
2108 HOST_WIDE_INT diff, gaps = 0;
2110 while (next)
2112 /* Skip same data-refs. In case that two or more stmts share
2113 data-ref (supported only for loads), we vectorize only the first
2114 stmt, and the rest get their vectorized loads from the first
2115 one. */
2116 if (!tree_int_cst_compare (DR_INIT (data_ref),
2117 DR_INIT (STMT_VINFO_DATA_REF (
2118 vinfo_for_stmt (next)))))
2120 if (DR_IS_WRITE (data_ref))
2122 if (dump_enabled_p ())
2123 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2124 "Two store stmts share the same dr.\n");
2125 return false;
2128 /* For load use the same data-ref load. */
2129 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2131 prev = next;
2132 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2133 continue;
2136 prev = next;
2137 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2139 /* All group members have the same STEP by construction. */
2140 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2142 /* Check that the distance between two accesses is equal to the type
2143 size. Otherwise, we have gaps. */
2144 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2145 - TREE_INT_CST_LOW (prev_init)) / type_size;
2146 if (diff != 1)
2148 /* FORNOW: SLP of accesses with gaps is not supported. */
2149 slp_impossible = true;
2150 if (DR_IS_WRITE (data_ref))
2152 if (dump_enabled_p ())
2153 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2154 "interleaved store with gaps\n");
2155 return false;
2158 gaps += diff - 1;
2161 last_accessed_element += diff;
2163 /* Store the gap from the previous member of the group. If there is no
2164 gap in the access, GROUP_GAP is always 1. */
2165 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2167 prev_init = DR_INIT (data_ref);
2168 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2169 /* Count the number of data-refs in the chain. */
2170 count++;
2173 if (groupsize == 0)
2174 groupsize = count + gaps;
2176 if (groupsize > UINT_MAX)
2178 if (dump_enabled_p ())
2179 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2180 "group is too large\n");
2181 return false;
2184 /* Check that the size of the interleaving is equal to count for stores,
2185 i.e., that there are no gaps. */
2186 if (groupsize != count
2187 && !DR_IS_READ (dr))
2189 if (dump_enabled_p ())
2190 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2191 "interleaved store with gaps\n");
2192 return false;
2195 /* If there is a gap after the last load in the group it is the
2196 difference between the groupsize and the last accessed
2197 element.
2198 When there is no gap, this difference should be 0. */
2199 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - last_accessed_element;
2201 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2202 if (dump_enabled_p ())
2204 dump_printf_loc (MSG_NOTE, vect_location,
2205 "Detected interleaving ");
2206 if (DR_IS_READ (dr))
2207 dump_printf (MSG_NOTE, "load ");
2208 else
2209 dump_printf (MSG_NOTE, "store ");
2210 dump_printf (MSG_NOTE, "of size %u starting with ",
2211 (unsigned)groupsize);
2212 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2213 if (GROUP_GAP (vinfo_for_stmt (stmt)) != 0)
2214 dump_printf_loc (MSG_NOTE, vect_location,
2215 "There is a gap of %u elements after the group\n",
2216 GROUP_GAP (vinfo_for_stmt (stmt)));
2219 /* SLP: create an SLP data structure for every interleaving group of
2220 stores for further analysis in vect_analyse_slp. */
2221 if (DR_IS_WRITE (dr) && !slp_impossible)
2223 if (loop_vinfo)
2224 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2225 if (bb_vinfo)
2226 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2229 /* If there is a gap in the end of the group or the group size cannot
2230 be made a multiple of the vector element count then we access excess
2231 elements in the last iteration and thus need to peel that off. */
2232 if (loop_vinfo
2233 && (groupsize - last_accessed_element > 0
2234 || exact_log2 (groupsize) == -1))
2237 if (dump_enabled_p ())
2238 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2239 "Data access with gaps requires scalar "
2240 "epilogue loop\n");
2241 if (loop->inner)
2243 if (dump_enabled_p ())
2244 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2245 "Peeling for outer loop is not supported\n");
2246 return false;
2249 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2253 return true;
2256 /* Analyze groups of accesses: check that DR belongs to a group of
2257 accesses of legal size, step, etc. Detect gaps, single element
2258 interleaving, and other special cases. Set grouped access info.
2259 Collect groups of strided stores for further use in SLP analysis. */
2261 static bool
2262 vect_analyze_group_access (struct data_reference *dr)
2264 if (!vect_analyze_group_access_1 (dr))
2266 /* Dissolve the group if present. */
2267 gimple *next;
2268 gimple *stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dr)));
2269 while (stmt)
2271 stmt_vec_info vinfo = vinfo_for_stmt (stmt);
2272 next = GROUP_NEXT_ELEMENT (vinfo);
2273 GROUP_FIRST_ELEMENT (vinfo) = NULL;
2274 GROUP_NEXT_ELEMENT (vinfo) = NULL;
2275 stmt = next;
2277 return false;
2279 return true;
2282 /* Analyze the access pattern of the data-reference DR.
2283 In case of non-consecutive accesses call vect_analyze_group_access() to
2284 analyze groups of accesses. */
2286 static bool
2287 vect_analyze_data_ref_access (struct data_reference *dr)
2289 tree step = DR_STEP (dr);
2290 tree scalar_type = TREE_TYPE (DR_REF (dr));
2291 gimple *stmt = DR_STMT (dr);
2292 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2293 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2294 struct loop *loop = NULL;
2296 if (loop_vinfo)
2297 loop = LOOP_VINFO_LOOP (loop_vinfo);
2299 if (loop_vinfo && !step)
2301 if (dump_enabled_p ())
2302 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2303 "bad data-ref access in loop\n");
2304 return false;
2307 /* Allow loads with zero step in inner-loop vectorization. */
2308 if (loop_vinfo && integer_zerop (step))
2310 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2311 if (!nested_in_vect_loop_p (loop, stmt))
2312 return DR_IS_READ (dr);
2313 /* Allow references with zero step for outer loops marked
2314 with pragma omp simd only - it guarantees absence of
2315 loop-carried dependencies between inner loop iterations. */
2316 if (!loop->force_vectorize)
2318 if (dump_enabled_p ())
2319 dump_printf_loc (MSG_NOTE, vect_location,
2320 "zero step in inner loop of nest\n");
2321 return false;
2325 if (loop && nested_in_vect_loop_p (loop, stmt))
2327 /* Interleaved accesses are not yet supported within outer-loop
2328 vectorization for references in the inner-loop. */
2329 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2331 /* For the rest of the analysis we use the outer-loop step. */
2332 step = STMT_VINFO_DR_STEP (stmt_info);
2333 if (integer_zerop (step))
2335 if (dump_enabled_p ())
2336 dump_printf_loc (MSG_NOTE, vect_location,
2337 "zero step in outer loop.\n");
2338 return DR_IS_READ (dr);
2342 /* Consecutive? */
2343 if (TREE_CODE (step) == INTEGER_CST)
2345 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2346 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2347 || (dr_step < 0
2348 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2350 /* Mark that it is not interleaving. */
2351 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2352 return true;
2356 if (loop && nested_in_vect_loop_p (loop, stmt))
2358 if (dump_enabled_p ())
2359 dump_printf_loc (MSG_NOTE, vect_location,
2360 "grouped access in outer loop.\n");
2361 return false;
2365 /* Assume this is a DR handled by non-constant strided load case. */
2366 if (TREE_CODE (step) != INTEGER_CST)
2367 return (STMT_VINFO_STRIDED_P (stmt_info)
2368 && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
2369 || vect_analyze_group_access (dr)));
2371 /* Not consecutive access - check if it's a part of interleaving group. */
2372 return vect_analyze_group_access (dr);
2377 /* A helper function used in the comparator function to sort data
2378 references. T1 and T2 are two data references to be compared.
2379 The function returns -1, 0, or 1. */
2381 static int
2382 compare_tree (tree t1, tree t2)
2384 int i, cmp;
2385 enum tree_code code;
2386 char tclass;
2388 if (t1 == t2)
2389 return 0;
2390 if (t1 == NULL)
2391 return -1;
2392 if (t2 == NULL)
2393 return 1;
2396 if (TREE_CODE (t1) != TREE_CODE (t2))
2397 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2399 code = TREE_CODE (t1);
2400 switch (code)
2402 /* For const values, we can just use hash values for comparisons. */
2403 case INTEGER_CST:
2404 case REAL_CST:
2405 case FIXED_CST:
2406 case STRING_CST:
2407 case COMPLEX_CST:
2408 case VECTOR_CST:
2410 hashval_t h1 = iterative_hash_expr (t1, 0);
2411 hashval_t h2 = iterative_hash_expr (t2, 0);
2412 if (h1 != h2)
2413 return h1 < h2 ? -1 : 1;
2414 break;
2417 case SSA_NAME:
2418 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2419 if (cmp != 0)
2420 return cmp;
2422 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2423 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2424 break;
2426 default:
2427 tclass = TREE_CODE_CLASS (code);
2429 /* For var-decl, we could compare their UIDs. */
2430 if (tclass == tcc_declaration)
2432 if (DECL_UID (t1) != DECL_UID (t2))
2433 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2434 break;
2437 /* For expressions with operands, compare their operands recursively. */
2438 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2440 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2441 if (cmp != 0)
2442 return cmp;
2446 return 0;
2450 /* Compare two data-references DRA and DRB to group them into chunks
2451 suitable for grouping. */
2453 static int
2454 dr_group_sort_cmp (const void *dra_, const void *drb_)
2456 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2457 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2458 int cmp;
2460 /* Stabilize sort. */
2461 if (dra == drb)
2462 return 0;
2464 /* Ordering of DRs according to base. */
2465 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2467 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2468 if (cmp != 0)
2469 return cmp;
2472 /* And according to DR_OFFSET. */
2473 if (!dr_equal_offsets_p (dra, drb))
2475 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2476 if (cmp != 0)
2477 return cmp;
2480 /* Put reads before writes. */
2481 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2482 return DR_IS_READ (dra) ? -1 : 1;
2484 /* Then sort after access size. */
2485 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2486 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2488 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2489 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2490 if (cmp != 0)
2491 return cmp;
2494 /* And after step. */
2495 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2497 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2498 if (cmp != 0)
2499 return cmp;
2502 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2503 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2504 if (cmp == 0)
2505 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2506 return cmp;
2509 /* Function vect_analyze_data_ref_accesses.
2511 Analyze the access pattern of all the data references in the loop.
2513 FORNOW: the only access pattern that is considered vectorizable is a
2514 simple step 1 (consecutive) access.
2516 FORNOW: handle only arrays and pointer accesses. */
2518 bool
2519 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2521 unsigned int i;
2522 vec<data_reference_p> datarefs;
2523 struct data_reference *dr;
2525 if (dump_enabled_p ())
2526 dump_printf_loc (MSG_NOTE, vect_location,
2527 "=== vect_analyze_data_ref_accesses ===\n");
2529 if (loop_vinfo)
2530 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2531 else
2532 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2534 if (datarefs.is_empty ())
2535 return true;
2537 /* Sort the array of datarefs to make building the interleaving chains
2538 linear. Don't modify the original vector's order, it is needed for
2539 determining what dependencies are reversed. */
2540 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2541 datarefs_copy.qsort (dr_group_sort_cmp);
2543 /* Build the interleaving chains. */
2544 for (i = 0; i < datarefs_copy.length () - 1;)
2546 data_reference_p dra = datarefs_copy[i];
2547 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2548 stmt_vec_info lastinfo = NULL;
2549 for (i = i + 1; i < datarefs_copy.length (); ++i)
2551 data_reference_p drb = datarefs_copy[i];
2552 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2554 /* ??? Imperfect sorting (non-compatible types, non-modulo
2555 accesses, same accesses) can lead to a group to be artificially
2556 split here as we don't just skip over those. If it really
2557 matters we can push those to a worklist and re-iterate
2558 over them. The we can just skip ahead to the next DR here. */
2560 /* Check that the data-refs have same first location (except init)
2561 and they are both either store or load (not load and store,
2562 not masked loads or stores). */
2563 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2564 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2565 DR_BASE_ADDRESS (drb), 0)
2566 || !dr_equal_offsets_p (dra, drb)
2567 || !gimple_assign_single_p (DR_STMT (dra))
2568 || !gimple_assign_single_p (DR_STMT (drb)))
2569 break;
2571 /* Check that the data-refs have the same constant size. */
2572 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2573 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2574 if (!tree_fits_uhwi_p (sza)
2575 || !tree_fits_uhwi_p (szb)
2576 || !tree_int_cst_equal (sza, szb))
2577 break;
2579 /* Check that the data-refs have the same step. */
2580 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2581 break;
2583 /* Do not place the same access in the interleaving chain twice. */
2584 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2585 break;
2587 /* Check the types are compatible.
2588 ??? We don't distinguish this during sorting. */
2589 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2590 TREE_TYPE (DR_REF (drb))))
2591 break;
2593 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2594 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2595 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2596 gcc_assert (init_a < init_b);
2598 /* If init_b == init_a + the size of the type * k, we have an
2599 interleaving, and DRA is accessed before DRB. */
2600 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2601 if ((init_b - init_a) % type_size_a != 0)
2602 break;
2604 /* If we have a store, the accesses are adjacent. This splits
2605 groups into chunks we support (we don't support vectorization
2606 of stores with gaps). */
2607 if (!DR_IS_READ (dra)
2608 && (init_b - (HOST_WIDE_INT) TREE_INT_CST_LOW
2609 (DR_INIT (datarefs_copy[i-1]))
2610 != type_size_a))
2611 break;
2613 /* If the step (if not zero or non-constant) is greater than the
2614 difference between data-refs' inits this splits groups into
2615 suitable sizes. */
2616 if (tree_fits_shwi_p (DR_STEP (dra)))
2618 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2619 if (step != 0 && step <= (init_b - init_a))
2620 break;
2623 if (dump_enabled_p ())
2625 dump_printf_loc (MSG_NOTE, vect_location,
2626 "Detected interleaving ");
2627 if (DR_IS_READ (dra))
2628 dump_printf (MSG_NOTE, "load ");
2629 else
2630 dump_printf (MSG_NOTE, "store ");
2631 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2632 dump_printf (MSG_NOTE, " and ");
2633 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2634 dump_printf (MSG_NOTE, "\n");
2637 /* Link the found element into the group list. */
2638 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2640 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2641 lastinfo = stmtinfo_a;
2643 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2644 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2645 lastinfo = stmtinfo_b;
2649 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2650 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2651 && !vect_analyze_data_ref_access (dr))
2653 if (dump_enabled_p ())
2654 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2655 "not vectorized: complicated access pattern.\n");
2657 if (bb_vinfo)
2659 /* Mark the statement as not vectorizable. */
2660 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2661 continue;
2663 else
2665 datarefs_copy.release ();
2666 return false;
2670 datarefs_copy.release ();
2671 return true;
2675 /* Operator == between two dr_with_seg_len objects.
2677 This equality operator is used to make sure two data refs
2678 are the same one so that we will consider to combine the
2679 aliasing checks of those two pairs of data dependent data
2680 refs. */
2682 static bool
2683 operator == (const dr_with_seg_len& d1,
2684 const dr_with_seg_len& d2)
2686 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2687 DR_BASE_ADDRESS (d2.dr), 0)
2688 && compare_tree (d1.offset, d2.offset) == 0
2689 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2692 /* Function comp_dr_with_seg_len_pair.
2694 Comparison function for sorting objects of dr_with_seg_len_pair_t
2695 so that we can combine aliasing checks in one scan. */
2697 static int
2698 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2700 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2701 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2703 const dr_with_seg_len &p11 = p1->first,
2704 &p12 = p1->second,
2705 &p21 = p2->first,
2706 &p22 = p2->second;
2708 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2709 if a and c have the same basic address snd step, and b and d have the same
2710 address and step. Therefore, if any a&c or b&d don't have the same address
2711 and step, we don't care the order of those two pairs after sorting. */
2712 int comp_res;
2714 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2715 DR_BASE_ADDRESS (p21.dr))) != 0)
2716 return comp_res;
2717 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2718 DR_BASE_ADDRESS (p22.dr))) != 0)
2719 return comp_res;
2720 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2721 return comp_res;
2722 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2723 return comp_res;
2724 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2725 return comp_res;
2726 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2727 return comp_res;
2729 return 0;
2732 /* Function vect_vfa_segment_size.
2734 Create an expression that computes the size of segment
2735 that will be accessed for a data reference. The functions takes into
2736 account that realignment loads may access one more vector.
2738 Input:
2739 DR: The data reference.
2740 LENGTH_FACTOR: segment length to consider.
2742 Return an expression whose value is the size of segment which will be
2743 accessed by DR. */
2745 static tree
2746 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2748 tree segment_length;
2750 if (integer_zerop (DR_STEP (dr)))
2751 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2752 else
2753 segment_length = size_binop (MULT_EXPR,
2754 fold_convert (sizetype, DR_STEP (dr)),
2755 fold_convert (sizetype, length_factor));
2757 if (vect_supportable_dr_alignment (dr, false)
2758 == dr_explicit_realign_optimized)
2760 tree vector_size = TYPE_SIZE_UNIT
2761 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2763 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2765 return segment_length;
2768 /* Function vect_prune_runtime_alias_test_list.
2770 Prune a list of ddrs to be tested at run-time by versioning for alias.
2771 Merge several alias checks into one if possible.
2772 Return FALSE if resulting list of ddrs is longer then allowed by
2773 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2775 bool
2776 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2778 vec<ddr_p> may_alias_ddrs =
2779 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2780 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2781 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2782 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2783 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2785 ddr_p ddr;
2786 unsigned int i;
2787 tree length_factor;
2789 if (dump_enabled_p ())
2790 dump_printf_loc (MSG_NOTE, vect_location,
2791 "=== vect_prune_runtime_alias_test_list ===\n");
2793 if (may_alias_ddrs.is_empty ())
2794 return true;
2796 /* Basically, for each pair of dependent data refs store_ptr_0
2797 and load_ptr_0, we create an expression:
2799 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2800 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2802 for aliasing checks. However, in some cases we can decrease
2803 the number of checks by combining two checks into one. For
2804 example, suppose we have another pair of data refs store_ptr_0
2805 and load_ptr_1, and if the following condition is satisfied:
2807 load_ptr_0 < load_ptr_1 &&
2808 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2810 (this condition means, in each iteration of vectorized loop,
2811 the accessed memory of store_ptr_0 cannot be between the memory
2812 of load_ptr_0 and load_ptr_1.)
2814 we then can use only the following expression to finish the
2815 alising checks between store_ptr_0 & load_ptr_0 and
2816 store_ptr_0 & load_ptr_1:
2818 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2819 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2821 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2822 same basic address. */
2824 comp_alias_ddrs.create (may_alias_ddrs.length ());
2826 /* First, we collect all data ref pairs for aliasing checks. */
2827 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2829 struct data_reference *dr_a, *dr_b;
2830 gimple *dr_group_first_a, *dr_group_first_b;
2831 tree segment_length_a, segment_length_b;
2832 gimple *stmt_a, *stmt_b;
2834 dr_a = DDR_A (ddr);
2835 stmt_a = DR_STMT (DDR_A (ddr));
2836 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2837 if (dr_group_first_a)
2839 stmt_a = dr_group_first_a;
2840 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2843 dr_b = DDR_B (ddr);
2844 stmt_b = DR_STMT (DDR_B (ddr));
2845 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2846 if (dr_group_first_b)
2848 stmt_b = dr_group_first_b;
2849 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2852 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2853 length_factor = scalar_loop_iters;
2854 else
2855 length_factor = size_int (vect_factor);
2856 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2857 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2859 dr_with_seg_len_pair_t dr_with_seg_len_pair
2860 (dr_with_seg_len (dr_a, segment_length_a),
2861 dr_with_seg_len (dr_b, segment_length_b));
2863 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2864 std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2866 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2869 /* Second, we sort the collected data ref pairs so that we can scan
2870 them once to combine all possible aliasing checks. */
2871 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2873 /* Third, we scan the sorted dr pairs and check if we can combine
2874 alias checks of two neighbouring dr pairs. */
2875 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2877 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2878 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2879 *dr_b1 = &comp_alias_ddrs[i-1].second,
2880 *dr_a2 = &comp_alias_ddrs[i].first,
2881 *dr_b2 = &comp_alias_ddrs[i].second;
2883 /* Remove duplicate data ref pairs. */
2884 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2886 if (dump_enabled_p ())
2888 dump_printf_loc (MSG_NOTE, vect_location,
2889 "found equal ranges ");
2890 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2891 DR_REF (dr_a1->dr));
2892 dump_printf (MSG_NOTE, ", ");
2893 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2894 DR_REF (dr_b1->dr));
2895 dump_printf (MSG_NOTE, " and ");
2896 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2897 DR_REF (dr_a2->dr));
2898 dump_printf (MSG_NOTE, ", ");
2899 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2900 DR_REF (dr_b2->dr));
2901 dump_printf (MSG_NOTE, "\n");
2904 comp_alias_ddrs.ordered_remove (i--);
2905 continue;
2908 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2910 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2911 and DR_A1 and DR_A2 are two consecutive memrefs. */
2912 if (*dr_a1 == *dr_a2)
2914 std::swap (dr_a1, dr_b1);
2915 std::swap (dr_a2, dr_b2);
2918 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2919 DR_BASE_ADDRESS (dr_a2->dr),
2921 || !tree_fits_shwi_p (dr_a1->offset)
2922 || !tree_fits_shwi_p (dr_a2->offset))
2923 continue;
2925 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2926 - tree_to_shwi (dr_a1->offset));
2929 /* Now we check if the following condition is satisfied:
2931 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2933 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2934 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2935 have to make a best estimation. We can get the minimum value
2936 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2937 then either of the following two conditions can guarantee the
2938 one above:
2940 1: DIFF <= MIN_SEG_LEN_B
2941 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2945 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2946 ? tree_to_shwi (dr_b1->seg_len)
2947 : vect_factor);
2949 if (diff <= min_seg_len_b
2950 || (tree_fits_shwi_p (dr_a1->seg_len)
2951 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2953 if (dump_enabled_p ())
2955 dump_printf_loc (MSG_NOTE, vect_location,
2956 "merging ranges for ");
2957 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2958 DR_REF (dr_a1->dr));
2959 dump_printf (MSG_NOTE, ", ");
2960 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2961 DR_REF (dr_b1->dr));
2962 dump_printf (MSG_NOTE, " and ");
2963 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2964 DR_REF (dr_a2->dr));
2965 dump_printf (MSG_NOTE, ", ");
2966 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2967 DR_REF (dr_b2->dr));
2968 dump_printf (MSG_NOTE, "\n");
2971 dr_a1->seg_len = size_binop (PLUS_EXPR,
2972 dr_a2->seg_len, size_int (diff));
2973 comp_alias_ddrs.ordered_remove (i--);
2978 dump_printf_loc (MSG_NOTE, vect_location,
2979 "improved number of alias checks from %d to %d\n",
2980 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2981 if ((int) comp_alias_ddrs.length () >
2982 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2983 return false;
2985 return true;
2988 /* Check whether a non-affine read or write in stmt is suitable for gather load
2989 or scatter store and if so, return a builtin decl for that operation. */
2991 tree
2992 vect_check_gather_scatter (gimple *stmt, loop_vec_info loop_vinfo, tree *basep,
2993 tree *offp, int *scalep)
2995 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2996 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2997 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2998 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2999 tree offtype = NULL_TREE;
3000 tree decl, base, off;
3001 machine_mode pmode;
3002 int punsignedp, pvolatilep;
3004 base = DR_REF (dr);
3005 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
3006 see if we can use the def stmt of the address. */
3007 if (is_gimple_call (stmt)
3008 && gimple_call_internal_p (stmt)
3009 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
3010 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
3011 && TREE_CODE (base) == MEM_REF
3012 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3013 && integer_zerop (TREE_OPERAND (base, 1))
3014 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
3016 gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
3017 if (is_gimple_assign (def_stmt)
3018 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3019 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3022 /* The gather and scatter builtins need address of the form
3023 loop_invariant + vector * {1, 2, 4, 8}
3025 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3026 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3027 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3028 multiplications and additions in it. To get a vector, we need
3029 a single SSA_NAME that will be defined in the loop and will
3030 contain everything that is not loop invariant and that can be
3031 vectorized. The following code attempts to find such a preexistng
3032 SSA_NAME OFF and put the loop invariants into a tree BASE
3033 that can be gimplified before the loop. */
3034 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
3035 &pmode, &punsignedp, &pvolatilep, false);
3036 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
3038 if (TREE_CODE (base) == MEM_REF)
3040 if (!integer_zerop (TREE_OPERAND (base, 1)))
3042 if (off == NULL_TREE)
3044 offset_int moff = mem_ref_offset (base);
3045 off = wide_int_to_tree (sizetype, moff);
3047 else
3048 off = size_binop (PLUS_EXPR, off,
3049 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3051 base = TREE_OPERAND (base, 0);
3053 else
3054 base = build_fold_addr_expr (base);
3056 if (off == NULL_TREE)
3057 off = size_zero_node;
3059 /* If base is not loop invariant, either off is 0, then we start with just
3060 the constant offset in the loop invariant BASE and continue with base
3061 as OFF, otherwise give up.
3062 We could handle that case by gimplifying the addition of base + off
3063 into some SSA_NAME and use that as off, but for now punt. */
3064 if (!expr_invariant_in_loop_p (loop, base))
3066 if (!integer_zerop (off))
3067 return NULL_TREE;
3068 off = base;
3069 base = size_int (pbitpos / BITS_PER_UNIT);
3071 /* Otherwise put base + constant offset into the loop invariant BASE
3072 and continue with OFF. */
3073 else
3075 base = fold_convert (sizetype, base);
3076 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3079 /* OFF at this point may be either a SSA_NAME or some tree expression
3080 from get_inner_reference. Try to peel off loop invariants from it
3081 into BASE as long as possible. */
3082 STRIP_NOPS (off);
3083 while (offtype == NULL_TREE)
3085 enum tree_code code;
3086 tree op0, op1, add = NULL_TREE;
3088 if (TREE_CODE (off) == SSA_NAME)
3090 gimple *def_stmt = SSA_NAME_DEF_STMT (off);
3092 if (expr_invariant_in_loop_p (loop, off))
3093 return NULL_TREE;
3095 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3096 break;
3098 op0 = gimple_assign_rhs1 (def_stmt);
3099 code = gimple_assign_rhs_code (def_stmt);
3100 op1 = gimple_assign_rhs2 (def_stmt);
3102 else
3104 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3105 return NULL_TREE;
3106 code = TREE_CODE (off);
3107 extract_ops_from_tree (off, &code, &op0, &op1);
3109 switch (code)
3111 case POINTER_PLUS_EXPR:
3112 case PLUS_EXPR:
3113 if (expr_invariant_in_loop_p (loop, op0))
3115 add = op0;
3116 off = op1;
3117 do_add:
3118 add = fold_convert (sizetype, add);
3119 if (scale != 1)
3120 add = size_binop (MULT_EXPR, add, size_int (scale));
3121 base = size_binop (PLUS_EXPR, base, add);
3122 continue;
3124 if (expr_invariant_in_loop_p (loop, op1))
3126 add = op1;
3127 off = op0;
3128 goto do_add;
3130 break;
3131 case MINUS_EXPR:
3132 if (expr_invariant_in_loop_p (loop, op1))
3134 add = fold_convert (sizetype, op1);
3135 add = size_binop (MINUS_EXPR, size_zero_node, add);
3136 off = op0;
3137 goto do_add;
3139 break;
3140 case MULT_EXPR:
3141 if (scale == 1 && tree_fits_shwi_p (op1))
3143 scale = tree_to_shwi (op1);
3144 off = op0;
3145 continue;
3147 break;
3148 case SSA_NAME:
3149 off = op0;
3150 continue;
3151 CASE_CONVERT:
3152 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3153 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3154 break;
3155 if (TYPE_PRECISION (TREE_TYPE (op0))
3156 == TYPE_PRECISION (TREE_TYPE (off)))
3158 off = op0;
3159 continue;
3161 if (TYPE_PRECISION (TREE_TYPE (op0))
3162 < TYPE_PRECISION (TREE_TYPE (off)))
3164 off = op0;
3165 offtype = TREE_TYPE (off);
3166 STRIP_NOPS (off);
3167 continue;
3169 break;
3170 default:
3171 break;
3173 break;
3176 /* If at the end OFF still isn't a SSA_NAME or isn't
3177 defined in the loop, punt. */
3178 if (TREE_CODE (off) != SSA_NAME
3179 || expr_invariant_in_loop_p (loop, off))
3180 return NULL_TREE;
3182 if (offtype == NULL_TREE)
3183 offtype = TREE_TYPE (off);
3185 if (DR_IS_READ (dr))
3186 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3187 offtype, scale);
3188 else
3189 decl = targetm.vectorize.builtin_scatter (STMT_VINFO_VECTYPE (stmt_info),
3190 offtype, scale);
3192 if (decl == NULL_TREE)
3193 return NULL_TREE;
3195 if (basep)
3196 *basep = base;
3197 if (offp)
3198 *offp = off;
3199 if (scalep)
3200 *scalep = scale;
3201 return decl;
3204 /* Function vect_analyze_data_refs.
3206 Find all the data references in the loop or basic block.
3208 The general structure of the analysis of data refs in the vectorizer is as
3209 follows:
3210 1- vect_analyze_data_refs(loop/bb): call
3211 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3212 in the loop/bb and their dependences.
3213 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3214 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3215 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3219 bool
3220 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3221 bb_vec_info bb_vinfo,
3222 int *min_vf, unsigned *n_stmts)
3224 struct loop *loop = NULL;
3225 basic_block bb = NULL;
3226 unsigned int i;
3227 vec<data_reference_p> datarefs;
3228 struct data_reference *dr;
3229 tree scalar_type;
3231 if (dump_enabled_p ())
3232 dump_printf_loc (MSG_NOTE, vect_location,
3233 "=== vect_analyze_data_refs ===\n");
3235 if (loop_vinfo)
3237 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3239 loop = LOOP_VINFO_LOOP (loop_vinfo);
3240 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3241 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3243 if (dump_enabled_p ())
3244 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3245 "not vectorized: loop contains function calls"
3246 " or data references that cannot be analyzed\n");
3247 return false;
3250 for (i = 0; i < loop->num_nodes; i++)
3252 gimple_stmt_iterator gsi;
3254 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3256 gimple *stmt = gsi_stmt (gsi);
3257 if (is_gimple_debug (stmt))
3258 continue;
3259 ++*n_stmts;
3260 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3262 if (is_gimple_call (stmt) && loop->safelen)
3264 tree fndecl = gimple_call_fndecl (stmt), op;
3265 if (fndecl != NULL_TREE)
3267 struct cgraph_node *node = cgraph_node::get (fndecl);
3268 if (node != NULL && node->simd_clones != NULL)
3270 unsigned int j, n = gimple_call_num_args (stmt);
3271 for (j = 0; j < n; j++)
3273 op = gimple_call_arg (stmt, j);
3274 if (DECL_P (op)
3275 || (REFERENCE_CLASS_P (op)
3276 && get_base_address (op)))
3277 break;
3279 op = gimple_call_lhs (stmt);
3280 /* Ignore #pragma omp declare simd functions
3281 if they don't have data references in the
3282 call stmt itself. */
3283 if (j == n
3284 && !(op
3285 && (DECL_P (op)
3286 || (REFERENCE_CLASS_P (op)
3287 && get_base_address (op)))))
3288 continue;
3292 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3293 if (dump_enabled_p ())
3294 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3295 "not vectorized: loop contains function "
3296 "calls or data references that cannot "
3297 "be analyzed\n");
3298 return false;
3303 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3305 else
3307 gimple_stmt_iterator gsi;
3309 bb = BB_VINFO_BB (bb_vinfo);
3310 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3312 gimple *stmt = gsi_stmt (gsi);
3313 if (is_gimple_debug (stmt))
3314 continue;
3315 ++*n_stmts;
3316 if (!find_data_references_in_stmt (NULL, stmt,
3317 &BB_VINFO_DATAREFS (bb_vinfo)))
3319 /* Mark the rest of the basic-block as unvectorizable. */
3320 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3322 stmt = gsi_stmt (gsi);
3323 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3325 break;
3329 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3332 /* Go through the data-refs, check that the analysis succeeded. Update
3333 pointer from stmt_vec_info struct to DR and vectype. */
3335 FOR_EACH_VEC_ELT (datarefs, i, dr)
3337 gimple *stmt;
3338 stmt_vec_info stmt_info;
3339 tree base, offset, init;
3340 enum { SG_NONE, GATHER, SCATTER } gatherscatter = SG_NONE;
3341 bool simd_lane_access = false;
3342 int vf;
3344 again:
3345 if (!dr || !DR_REF (dr))
3347 if (dump_enabled_p ())
3348 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3349 "not vectorized: unhandled data-ref\n");
3350 return false;
3353 stmt = DR_STMT (dr);
3354 stmt_info = vinfo_for_stmt (stmt);
3356 /* Discard clobbers from the dataref vector. We will remove
3357 clobber stmts during vectorization. */
3358 if (gimple_clobber_p (stmt))
3360 free_data_ref (dr);
3361 if (i == datarefs.length () - 1)
3363 datarefs.pop ();
3364 break;
3366 datarefs.ordered_remove (i);
3367 dr = datarefs[i];
3368 goto again;
3371 /* Check that analysis of the data-ref succeeded. */
3372 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3373 || !DR_STEP (dr))
3375 bool maybe_gather
3376 = DR_IS_READ (dr)
3377 && !TREE_THIS_VOLATILE (DR_REF (dr))
3378 && targetm.vectorize.builtin_gather != NULL;
3379 bool maybe_scatter
3380 = DR_IS_WRITE (dr)
3381 && !TREE_THIS_VOLATILE (DR_REF (dr))
3382 && targetm.vectorize.builtin_scatter != NULL;
3383 bool maybe_simd_lane_access
3384 = loop_vinfo && loop->simduid;
3386 /* If target supports vector gather loads or scatter stores, or if
3387 this might be a SIMD lane access, see if they can't be used. */
3388 if (loop_vinfo
3389 && (maybe_gather || maybe_scatter || maybe_simd_lane_access)
3390 && !nested_in_vect_loop_p (loop, stmt))
3392 struct data_reference *newdr
3393 = create_data_ref (NULL, loop_containing_stmt (stmt),
3394 DR_REF (dr), stmt, maybe_scatter ? false : true);
3395 gcc_assert (newdr != NULL && DR_REF (newdr));
3396 if (DR_BASE_ADDRESS (newdr)
3397 && DR_OFFSET (newdr)
3398 && DR_INIT (newdr)
3399 && DR_STEP (newdr)
3400 && integer_zerop (DR_STEP (newdr)))
3402 if (maybe_simd_lane_access)
3404 tree off = DR_OFFSET (newdr);
3405 STRIP_NOPS (off);
3406 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3407 && TREE_CODE (off) == MULT_EXPR
3408 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3410 tree step = TREE_OPERAND (off, 1);
3411 off = TREE_OPERAND (off, 0);
3412 STRIP_NOPS (off);
3413 if (CONVERT_EXPR_P (off)
3414 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3415 0)))
3416 < TYPE_PRECISION (TREE_TYPE (off)))
3417 off = TREE_OPERAND (off, 0);
3418 if (TREE_CODE (off) == SSA_NAME)
3420 gimple *def = SSA_NAME_DEF_STMT (off);
3421 tree reft = TREE_TYPE (DR_REF (newdr));
3422 if (is_gimple_call (def)
3423 && gimple_call_internal_p (def)
3424 && (gimple_call_internal_fn (def)
3425 == IFN_GOMP_SIMD_LANE))
3427 tree arg = gimple_call_arg (def, 0);
3428 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3429 arg = SSA_NAME_VAR (arg);
3430 if (arg == loop->simduid
3431 /* For now. */
3432 && tree_int_cst_equal
3433 (TYPE_SIZE_UNIT (reft),
3434 step))
3436 DR_OFFSET (newdr) = ssize_int (0);
3437 DR_STEP (newdr) = step;
3438 DR_ALIGNED_TO (newdr)
3439 = size_int (BIGGEST_ALIGNMENT);
3440 dr = newdr;
3441 simd_lane_access = true;
3447 if (!simd_lane_access && (maybe_gather || maybe_scatter))
3449 dr = newdr;
3450 if (maybe_gather)
3451 gatherscatter = GATHER;
3452 else
3453 gatherscatter = SCATTER;
3456 if (gatherscatter == SG_NONE && !simd_lane_access)
3457 free_data_ref (newdr);
3460 if (gatherscatter == SG_NONE && !simd_lane_access)
3462 if (dump_enabled_p ())
3464 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3465 "not vectorized: data ref analysis "
3466 "failed ");
3467 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3468 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3471 if (bb_vinfo)
3472 break;
3474 return false;
3478 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3480 if (dump_enabled_p ())
3481 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3482 "not vectorized: base addr of dr is a "
3483 "constant\n");
3485 if (bb_vinfo)
3486 break;
3488 if (gatherscatter != SG_NONE || simd_lane_access)
3489 free_data_ref (dr);
3490 return false;
3493 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3495 if (dump_enabled_p ())
3497 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3498 "not vectorized: volatile type ");
3499 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3500 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3503 if (bb_vinfo)
3504 break;
3506 return false;
3509 if (stmt_can_throw_internal (stmt))
3511 if (dump_enabled_p ())
3513 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3514 "not vectorized: statement can throw an "
3515 "exception ");
3516 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3517 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3520 if (bb_vinfo)
3521 break;
3523 if (gatherscatter != SG_NONE || simd_lane_access)
3524 free_data_ref (dr);
3525 return false;
3528 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3529 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3531 if (dump_enabled_p ())
3533 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3534 "not vectorized: statement is bitfield "
3535 "access ");
3536 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3537 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3540 if (bb_vinfo)
3541 break;
3543 if (gatherscatter != SG_NONE || simd_lane_access)
3544 free_data_ref (dr);
3545 return false;
3548 base = unshare_expr (DR_BASE_ADDRESS (dr));
3549 offset = unshare_expr (DR_OFFSET (dr));
3550 init = unshare_expr (DR_INIT (dr));
3552 if (is_gimple_call (stmt)
3553 && (!gimple_call_internal_p (stmt)
3554 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3555 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3557 if (dump_enabled_p ())
3559 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3560 "not vectorized: dr in a call ");
3561 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3562 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3565 if (bb_vinfo)
3566 break;
3568 if (gatherscatter != SG_NONE || simd_lane_access)
3569 free_data_ref (dr);
3570 return false;
3573 /* Update DR field in stmt_vec_info struct. */
3575 /* If the dataref is in an inner-loop of the loop that is considered for
3576 for vectorization, we also want to analyze the access relative to
3577 the outer-loop (DR contains information only relative to the
3578 inner-most enclosing loop). We do that by building a reference to the
3579 first location accessed by the inner-loop, and analyze it relative to
3580 the outer-loop. */
3581 if (loop && nested_in_vect_loop_p (loop, stmt))
3583 tree outer_step, outer_base, outer_init;
3584 HOST_WIDE_INT pbitsize, pbitpos;
3585 tree poffset;
3586 machine_mode pmode;
3587 int punsignedp, pvolatilep;
3588 affine_iv base_iv, offset_iv;
3589 tree dinit;
3591 /* Build a reference to the first location accessed by the
3592 inner-loop: *(BASE+INIT). (The first location is actually
3593 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3594 tree inner_base = build_fold_indirect_ref
3595 (fold_build_pointer_plus (base, init));
3597 if (dump_enabled_p ())
3599 dump_printf_loc (MSG_NOTE, vect_location,
3600 "analyze in outer-loop: ");
3601 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3602 dump_printf (MSG_NOTE, "\n");
3605 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3606 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3607 gcc_assert (outer_base != NULL_TREE);
3609 if (pbitpos % BITS_PER_UNIT != 0)
3611 if (dump_enabled_p ())
3612 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3613 "failed: bit offset alignment.\n");
3614 return false;
3617 outer_base = build_fold_addr_expr (outer_base);
3618 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3619 &base_iv, false))
3621 if (dump_enabled_p ())
3622 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3623 "failed: evolution of base is not affine.\n");
3624 return false;
3627 if (offset)
3629 if (poffset)
3630 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3631 poffset);
3632 else
3633 poffset = offset;
3636 if (!poffset)
3638 offset_iv.base = ssize_int (0);
3639 offset_iv.step = ssize_int (0);
3641 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3642 &offset_iv, false))
3644 if (dump_enabled_p ())
3645 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3646 "evolution of offset is not affine.\n");
3647 return false;
3650 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3651 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3652 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3653 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3654 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3656 outer_step = size_binop (PLUS_EXPR,
3657 fold_convert (ssizetype, base_iv.step),
3658 fold_convert (ssizetype, offset_iv.step));
3660 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3661 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3662 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3663 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3664 STMT_VINFO_DR_OFFSET (stmt_info) =
3665 fold_convert (ssizetype, offset_iv.base);
3666 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3667 size_int (highest_pow2_factor (offset_iv.base));
3669 if (dump_enabled_p ())
3671 dump_printf_loc (MSG_NOTE, vect_location,
3672 "\touter base_address: ");
3673 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3674 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3675 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3676 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3677 STMT_VINFO_DR_OFFSET (stmt_info));
3678 dump_printf (MSG_NOTE,
3679 "\n\touter constant offset from base address: ");
3680 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3681 STMT_VINFO_DR_INIT (stmt_info));
3682 dump_printf (MSG_NOTE, "\n\touter step: ");
3683 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3684 STMT_VINFO_DR_STEP (stmt_info));
3685 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3686 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3687 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3688 dump_printf (MSG_NOTE, "\n");
3692 if (STMT_VINFO_DATA_REF (stmt_info))
3694 if (dump_enabled_p ())
3696 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3697 "not vectorized: more than one data ref "
3698 "in stmt: ");
3699 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3700 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3703 if (bb_vinfo)
3704 break;
3706 if (gatherscatter != SG_NONE || simd_lane_access)
3707 free_data_ref (dr);
3708 return false;
3711 STMT_VINFO_DATA_REF (stmt_info) = dr;
3712 if (simd_lane_access)
3714 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3715 free_data_ref (datarefs[i]);
3716 datarefs[i] = dr;
3719 /* Set vectype for STMT. */
3720 scalar_type = TREE_TYPE (DR_REF (dr));
3721 STMT_VINFO_VECTYPE (stmt_info)
3722 = get_vectype_for_scalar_type (scalar_type);
3723 if (!STMT_VINFO_VECTYPE (stmt_info))
3725 if (dump_enabled_p ())
3727 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3728 "not vectorized: no vectype for stmt: ");
3729 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3730 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3731 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3732 scalar_type);
3733 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3736 if (bb_vinfo)
3737 break;
3739 if (gatherscatter != SG_NONE || simd_lane_access)
3741 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3742 if (gatherscatter != SG_NONE)
3743 free_data_ref (dr);
3745 return false;
3747 else
3749 if (dump_enabled_p ())
3751 dump_printf_loc (MSG_NOTE, vect_location,
3752 "got vectype for stmt: ");
3753 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3754 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3755 STMT_VINFO_VECTYPE (stmt_info));
3756 dump_printf (MSG_NOTE, "\n");
3760 /* Adjust the minimal vectorization factor according to the
3761 vector type. */
3762 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3763 if (vf > *min_vf)
3764 *min_vf = vf;
3766 if (gatherscatter != SG_NONE)
3768 tree off;
3769 if (!vect_check_gather_scatter (stmt, loop_vinfo, NULL, &off, NULL)
3770 || get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3772 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3773 free_data_ref (dr);
3774 if (dump_enabled_p ())
3776 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3777 (gatherscatter == GATHER) ?
3778 "not vectorized: not suitable for gather "
3779 "load " :
3780 "not vectorized: not suitable for scatter "
3781 "store ");
3782 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3783 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3785 return false;
3788 datarefs[i] = dr;
3789 STMT_VINFO_GATHER_SCATTER_P (stmt_info) = gatherscatter;
3792 else if (loop_vinfo
3793 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3795 if (nested_in_vect_loop_p (loop, stmt))
3797 if (dump_enabled_p ())
3799 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3800 "not vectorized: not suitable for strided "
3801 "load ");
3802 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3803 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3805 return false;
3807 STMT_VINFO_STRIDED_P (stmt_info) = true;
3811 /* If we stopped analysis at the first dataref we could not analyze
3812 when trying to vectorize a basic-block mark the rest of the datarefs
3813 as not vectorizable and truncate the vector of datarefs. That
3814 avoids spending useless time in analyzing their dependence. */
3815 if (i != datarefs.length ())
3817 gcc_assert (bb_vinfo != NULL);
3818 for (unsigned j = i; j < datarefs.length (); ++j)
3820 data_reference_p dr = datarefs[j];
3821 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3822 free_data_ref (dr);
3824 datarefs.truncate (i);
3827 return true;
3831 /* Function vect_get_new_vect_var.
3833 Returns a name for a new variable. The current naming scheme appends the
3834 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3835 the name of vectorizer generated variables, and appends that to NAME if
3836 provided. */
3838 tree
3839 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3841 const char *prefix;
3842 tree new_vect_var;
3844 switch (var_kind)
3846 case vect_simple_var:
3847 prefix = "vect";
3848 break;
3849 case vect_scalar_var:
3850 prefix = "stmp";
3851 break;
3852 case vect_pointer_var:
3853 prefix = "vectp";
3854 break;
3855 default:
3856 gcc_unreachable ();
3859 if (name)
3861 char* tmp = concat (prefix, "_", name, NULL);
3862 new_vect_var = create_tmp_reg (type, tmp);
3863 free (tmp);
3865 else
3866 new_vect_var = create_tmp_reg (type, prefix);
3868 return new_vect_var;
3871 /* Duplicate ptr info and set alignment/misaligment on NAME from DR. */
3873 static void
3874 vect_duplicate_ssa_name_ptr_info (tree name, data_reference *dr,
3875 stmt_vec_info stmt_info)
3877 duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr));
3878 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
3879 int misalign = DR_MISALIGNMENT (dr);
3880 if (misalign == -1)
3881 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
3882 else
3883 set_ptr_info_alignment (SSA_NAME_PTR_INFO (name), align, misalign);
3886 /* Function vect_create_addr_base_for_vector_ref.
3888 Create an expression that computes the address of the first memory location
3889 that will be accessed for a data reference.
3891 Input:
3892 STMT: The statement containing the data reference.
3893 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3894 OFFSET: Optional. If supplied, it is be added to the initial address.
3895 LOOP: Specify relative to which loop-nest should the address be computed.
3896 For example, when the dataref is in an inner-loop nested in an
3897 outer-loop that is now being vectorized, LOOP can be either the
3898 outer-loop, or the inner-loop. The first memory location accessed
3899 by the following dataref ('in' points to short):
3901 for (i=0; i<N; i++)
3902 for (j=0; j<M; j++)
3903 s += in[i+j]
3905 is as follows:
3906 if LOOP=i_loop: &in (relative to i_loop)
3907 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3908 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3909 initial address. Unlike OFFSET, which is number of elements to
3910 be added, BYTE_OFFSET is measured in bytes.
3912 Output:
3913 1. Return an SSA_NAME whose value is the address of the memory location of
3914 the first vector of the data reference.
3915 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3916 these statement(s) which define the returned SSA_NAME.
3918 FORNOW: We are only handling array accesses with step 1. */
3920 tree
3921 vect_create_addr_base_for_vector_ref (gimple *stmt,
3922 gimple_seq *new_stmt_list,
3923 tree offset,
3924 struct loop *loop,
3925 tree byte_offset)
3927 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3928 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3929 tree data_ref_base;
3930 const char *base_name;
3931 tree addr_base;
3932 tree dest;
3933 gimple_seq seq = NULL;
3934 tree base_offset;
3935 tree init;
3936 tree vect_ptr_type;
3937 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3938 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3940 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3942 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3944 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3946 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3947 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3948 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3950 else
3952 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3953 base_offset = unshare_expr (DR_OFFSET (dr));
3954 init = unshare_expr (DR_INIT (dr));
3957 if (loop_vinfo)
3958 base_name = get_name (data_ref_base);
3959 else
3961 base_offset = ssize_int (0);
3962 init = ssize_int (0);
3963 base_name = get_name (DR_REF (dr));
3966 /* Create base_offset */
3967 base_offset = size_binop (PLUS_EXPR,
3968 fold_convert (sizetype, base_offset),
3969 fold_convert (sizetype, init));
3971 if (offset)
3973 offset = fold_build2 (MULT_EXPR, sizetype,
3974 fold_convert (sizetype, offset), step);
3975 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3976 base_offset, offset);
3978 if (byte_offset)
3980 byte_offset = fold_convert (sizetype, byte_offset);
3981 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3982 base_offset, byte_offset);
3985 /* base + base_offset */
3986 if (loop_vinfo)
3987 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3988 else
3990 addr_base = build1 (ADDR_EXPR,
3991 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3992 unshare_expr (DR_REF (dr)));
3995 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3996 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3997 addr_base = force_gimple_operand (addr_base, &seq, true, dest);
3998 gimple_seq_add_seq (new_stmt_list, seq);
4000 if (DR_PTR_INFO (dr)
4001 && TREE_CODE (addr_base) == SSA_NAME
4002 && !SSA_NAME_PTR_INFO (addr_base))
4004 vect_duplicate_ssa_name_ptr_info (addr_base, dr, stmt_info);
4005 if (offset || byte_offset)
4006 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
4009 if (dump_enabled_p ())
4011 dump_printf_loc (MSG_NOTE, vect_location, "created ");
4012 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
4013 dump_printf (MSG_NOTE, "\n");
4016 return addr_base;
4020 /* Function vect_create_data_ref_ptr.
4022 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
4023 location accessed in the loop by STMT, along with the def-use update
4024 chain to appropriately advance the pointer through the loop iterations.
4025 Also set aliasing information for the pointer. This pointer is used by
4026 the callers to this function to create a memory reference expression for
4027 vector load/store access.
4029 Input:
4030 1. STMT: a stmt that references memory. Expected to be of the form
4031 GIMPLE_ASSIGN <name, data-ref> or
4032 GIMPLE_ASSIGN <data-ref, name>.
4033 2. AGGR_TYPE: the type of the reference, which should be either a vector
4034 or an array.
4035 3. AT_LOOP: the loop where the vector memref is to be created.
4036 4. OFFSET (optional): an offset to be added to the initial address accessed
4037 by the data-ref in STMT.
4038 5. BSI: location where the new stmts are to be placed if there is no loop
4039 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4040 pointing to the initial address.
4041 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4042 to the initial address accessed by the data-ref in STMT. This is
4043 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4044 in bytes.
4046 Output:
4047 1. Declare a new ptr to vector_type, and have it point to the base of the
4048 data reference (initial addressed accessed by the data reference).
4049 For example, for vector of type V8HI, the following code is generated:
4051 v8hi *ap;
4052 ap = (v8hi *)initial_address;
4054 if OFFSET is not supplied:
4055 initial_address = &a[init];
4056 if OFFSET is supplied:
4057 initial_address = &a[init + OFFSET];
4058 if BYTE_OFFSET is supplied:
4059 initial_address = &a[init] + BYTE_OFFSET;
4061 Return the initial_address in INITIAL_ADDRESS.
4063 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4064 update the pointer in each iteration of the loop.
4066 Return the increment stmt that updates the pointer in PTR_INCR.
4068 3. Set INV_P to true if the access pattern of the data reference in the
4069 vectorized loop is invariant. Set it to false otherwise.
4071 4. Return the pointer. */
4073 tree
4074 vect_create_data_ref_ptr (gimple *stmt, tree aggr_type, struct loop *at_loop,
4075 tree offset, tree *initial_address,
4076 gimple_stmt_iterator *gsi, gimple **ptr_incr,
4077 bool only_init, bool *inv_p, tree byte_offset)
4079 const char *base_name;
4080 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4081 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4082 struct loop *loop = NULL;
4083 bool nested_in_vect_loop = false;
4084 struct loop *containing_loop = NULL;
4085 tree aggr_ptr_type;
4086 tree aggr_ptr;
4087 tree new_temp;
4088 gimple_seq new_stmt_list = NULL;
4089 edge pe = NULL;
4090 basic_block new_bb;
4091 tree aggr_ptr_init;
4092 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4093 tree aptr;
4094 gimple_stmt_iterator incr_gsi;
4095 bool insert_after;
4096 tree indx_before_incr, indx_after_incr;
4097 gimple *incr;
4098 tree step;
4099 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4101 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4102 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4104 if (loop_vinfo)
4106 loop = LOOP_VINFO_LOOP (loop_vinfo);
4107 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4108 containing_loop = (gimple_bb (stmt))->loop_father;
4109 pe = loop_preheader_edge (loop);
4111 else
4113 gcc_assert (bb_vinfo);
4114 only_init = true;
4115 *ptr_incr = NULL;
4118 /* Check the step (evolution) of the load in LOOP, and record
4119 whether it's invariant. */
4120 if (nested_in_vect_loop)
4121 step = STMT_VINFO_DR_STEP (stmt_info);
4122 else
4123 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4125 if (integer_zerop (step))
4126 *inv_p = true;
4127 else
4128 *inv_p = false;
4130 /* Create an expression for the first address accessed by this load
4131 in LOOP. */
4132 base_name = get_name (DR_BASE_ADDRESS (dr));
4134 if (dump_enabled_p ())
4136 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4137 dump_printf_loc (MSG_NOTE, vect_location,
4138 "create %s-pointer variable to type: ",
4139 get_tree_code_name (TREE_CODE (aggr_type)));
4140 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4141 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4142 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4143 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4144 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4145 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4146 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4147 else
4148 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4149 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4150 dump_printf (MSG_NOTE, "\n");
4153 /* (1) Create the new aggregate-pointer variable.
4154 Vector and array types inherit the alias set of their component
4155 type by default so we need to use a ref-all pointer if the data
4156 reference does not conflict with the created aggregated data
4157 reference because it is not addressable. */
4158 bool need_ref_all = false;
4159 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4160 get_alias_set (DR_REF (dr))))
4161 need_ref_all = true;
4162 /* Likewise for any of the data references in the stmt group. */
4163 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4165 gimple *orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4168 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4169 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4170 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4171 get_alias_set (DR_REF (sdr))))
4173 need_ref_all = true;
4174 break;
4176 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4178 while (orig_stmt);
4180 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4181 need_ref_all);
4182 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4185 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4186 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4187 def-use update cycles for the pointer: one relative to the outer-loop
4188 (LOOP), which is what steps (3) and (4) below do. The other is relative
4189 to the inner-loop (which is the inner-most loop containing the dataref),
4190 and this is done be step (5) below.
4192 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4193 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4194 redundant. Steps (3),(4) create the following:
4196 vp0 = &base_addr;
4197 LOOP: vp1 = phi(vp0,vp2)
4200 vp2 = vp1 + step
4201 goto LOOP
4203 If there is an inner-loop nested in loop, then step (5) will also be
4204 applied, and an additional update in the inner-loop will be created:
4206 vp0 = &base_addr;
4207 LOOP: vp1 = phi(vp0,vp2)
4209 inner: vp3 = phi(vp1,vp4)
4210 vp4 = vp3 + inner_step
4211 if () goto inner
4213 vp2 = vp1 + step
4214 if () goto LOOP */
4216 /* (2) Calculate the initial address of the aggregate-pointer, and set
4217 the aggregate-pointer to point to it before the loop. */
4219 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4221 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4222 offset, loop, byte_offset);
4223 if (new_stmt_list)
4225 if (pe)
4227 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4228 gcc_assert (!new_bb);
4230 else
4231 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4234 *initial_address = new_temp;
4235 aggr_ptr_init = new_temp;
4237 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4238 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4239 inner-loop nested in LOOP (during outer-loop vectorization). */
4241 /* No update in loop is required. */
4242 if (only_init && (!loop_vinfo || at_loop == loop))
4243 aptr = aggr_ptr_init;
4244 else
4246 /* The step of the aggregate pointer is the type size. */
4247 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4248 /* One exception to the above is when the scalar step of the load in
4249 LOOP is zero. In this case the step here is also zero. */
4250 if (*inv_p)
4251 iv_step = size_zero_node;
4252 else if (tree_int_cst_sgn (step) == -1)
4253 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4255 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4257 create_iv (aggr_ptr_init,
4258 fold_convert (aggr_ptr_type, iv_step),
4259 aggr_ptr, loop, &incr_gsi, insert_after,
4260 &indx_before_incr, &indx_after_incr);
4261 incr = gsi_stmt (incr_gsi);
4262 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4264 /* Copy the points-to information if it exists. */
4265 if (DR_PTR_INFO (dr))
4267 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4268 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4270 if (ptr_incr)
4271 *ptr_incr = incr;
4273 aptr = indx_before_incr;
4276 if (!nested_in_vect_loop || only_init)
4277 return aptr;
4280 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4281 nested in LOOP, if exists. */
4283 gcc_assert (nested_in_vect_loop);
4284 if (!only_init)
4286 standard_iv_increment_position (containing_loop, &incr_gsi,
4287 &insert_after);
4288 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4289 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4290 &indx_after_incr);
4291 incr = gsi_stmt (incr_gsi);
4292 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4294 /* Copy the points-to information if it exists. */
4295 if (DR_PTR_INFO (dr))
4297 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4298 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4300 if (ptr_incr)
4301 *ptr_incr = incr;
4303 return indx_before_incr;
4305 else
4306 gcc_unreachable ();
4310 /* Function bump_vector_ptr
4312 Increment a pointer (to a vector type) by vector-size. If requested,
4313 i.e. if PTR-INCR is given, then also connect the new increment stmt
4314 to the existing def-use update-chain of the pointer, by modifying
4315 the PTR_INCR as illustrated below:
4317 The pointer def-use update-chain before this function:
4318 DATAREF_PTR = phi (p_0, p_2)
4319 ....
4320 PTR_INCR: p_2 = DATAREF_PTR + step
4322 The pointer def-use update-chain after this function:
4323 DATAREF_PTR = phi (p_0, p_2)
4324 ....
4325 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4326 ....
4327 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4329 Input:
4330 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4331 in the loop.
4332 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4333 the loop. The increment amount across iterations is expected
4334 to be vector_size.
4335 BSI - location where the new update stmt is to be placed.
4336 STMT - the original scalar memory-access stmt that is being vectorized.
4337 BUMP - optional. The offset by which to bump the pointer. If not given,
4338 the offset is assumed to be vector_size.
4340 Output: Return NEW_DATAREF_PTR as illustrated above.
4344 tree
4345 bump_vector_ptr (tree dataref_ptr, gimple *ptr_incr, gimple_stmt_iterator *gsi,
4346 gimple *stmt, tree bump)
4348 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4349 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4350 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4351 tree update = TYPE_SIZE_UNIT (vectype);
4352 gassign *incr_stmt;
4353 ssa_op_iter iter;
4354 use_operand_p use_p;
4355 tree new_dataref_ptr;
4357 if (bump)
4358 update = bump;
4360 if (TREE_CODE (dataref_ptr) == SSA_NAME)
4361 new_dataref_ptr = copy_ssa_name (dataref_ptr);
4362 else
4363 new_dataref_ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
4364 incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
4365 dataref_ptr, update);
4366 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4368 /* Copy the points-to information if it exists. */
4369 if (DR_PTR_INFO (dr))
4371 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4372 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4375 if (!ptr_incr)
4376 return new_dataref_ptr;
4378 /* Update the vector-pointer's cross-iteration increment. */
4379 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4381 tree use = USE_FROM_PTR (use_p);
4383 if (use == dataref_ptr)
4384 SET_USE (use_p, new_dataref_ptr);
4385 else
4386 gcc_assert (tree_int_cst_compare (use, update) == 0);
4389 return new_dataref_ptr;
4393 /* Function vect_create_destination_var.
4395 Create a new temporary of type VECTYPE. */
4397 tree
4398 vect_create_destination_var (tree scalar_dest, tree vectype)
4400 tree vec_dest;
4401 const char *name;
4402 char *new_name;
4403 tree type;
4404 enum vect_var_kind kind;
4406 kind = vectype ? vect_simple_var : vect_scalar_var;
4407 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4409 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4411 name = get_name (scalar_dest);
4412 if (name)
4413 new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4414 else
4415 new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
4416 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4417 free (new_name);
4419 return vec_dest;
4422 /* Function vect_grouped_store_supported.
4424 Returns TRUE if interleave high and interleave low permutations
4425 are supported, and FALSE otherwise. */
4427 bool
4428 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4430 machine_mode mode = TYPE_MODE (vectype);
4432 /* vect_permute_store_chain requires the group size to be equal to 3 or
4433 be a power of two. */
4434 if (count != 3 && exact_log2 (count) == -1)
4436 if (dump_enabled_p ())
4437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4438 "the size of the group of accesses"
4439 " is not a power of 2 or not eqaul to 3\n");
4440 return false;
4443 /* Check that the permutation is supported. */
4444 if (VECTOR_MODE_P (mode))
4446 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4447 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4449 if (count == 3)
4451 unsigned int j0 = 0, j1 = 0, j2 = 0;
4452 unsigned int i, j;
4454 for (j = 0; j < 3; j++)
4456 int nelt0 = ((3 - j) * nelt) % 3;
4457 int nelt1 = ((3 - j) * nelt + 1) % 3;
4458 int nelt2 = ((3 - j) * nelt + 2) % 3;
4459 for (i = 0; i < nelt; i++)
4461 if (3 * i + nelt0 < nelt)
4462 sel[3 * i + nelt0] = j0++;
4463 if (3 * i + nelt1 < nelt)
4464 sel[3 * i + nelt1] = nelt + j1++;
4465 if (3 * i + nelt2 < nelt)
4466 sel[3 * i + nelt2] = 0;
4468 if (!can_vec_perm_p (mode, false, sel))
4470 if (dump_enabled_p ())
4471 dump_printf (MSG_MISSED_OPTIMIZATION,
4472 "permutaion op not supported by target.\n");
4473 return false;
4476 for (i = 0; i < nelt; i++)
4478 if (3 * i + nelt0 < nelt)
4479 sel[3 * i + nelt0] = 3 * i + nelt0;
4480 if (3 * i + nelt1 < nelt)
4481 sel[3 * i + nelt1] = 3 * i + nelt1;
4482 if (3 * i + nelt2 < nelt)
4483 sel[3 * i + nelt2] = nelt + j2++;
4485 if (!can_vec_perm_p (mode, false, sel))
4487 if (dump_enabled_p ())
4488 dump_printf (MSG_MISSED_OPTIMIZATION,
4489 "permutaion op not supported by target.\n");
4490 return false;
4493 return true;
4495 else
4497 /* If length is not equal to 3 then only power of 2 is supported. */
4498 gcc_assert (exact_log2 (count) != -1);
4500 for (i = 0; i < nelt / 2; i++)
4502 sel[i * 2] = i;
4503 sel[i * 2 + 1] = i + nelt;
4505 if (can_vec_perm_p (mode, false, sel))
4507 for (i = 0; i < nelt; i++)
4508 sel[i] += nelt / 2;
4509 if (can_vec_perm_p (mode, false, sel))
4510 return true;
4515 if (dump_enabled_p ())
4516 dump_printf (MSG_MISSED_OPTIMIZATION,
4517 "permutaion op not supported by target.\n");
4518 return false;
4522 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4523 type VECTYPE. */
4525 bool
4526 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4528 return vect_lanes_optab_supported_p ("vec_store_lanes",
4529 vec_store_lanes_optab,
4530 vectype, count);
4534 /* Function vect_permute_store_chain.
4536 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4537 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4538 the data correctly for the stores. Return the final references for stores
4539 in RESULT_CHAIN.
4541 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4542 The input is 4 vectors each containing 8 elements. We assign a number to
4543 each element, the input sequence is:
4545 1st vec: 0 1 2 3 4 5 6 7
4546 2nd vec: 8 9 10 11 12 13 14 15
4547 3rd vec: 16 17 18 19 20 21 22 23
4548 4th vec: 24 25 26 27 28 29 30 31
4550 The output sequence should be:
4552 1st vec: 0 8 16 24 1 9 17 25
4553 2nd vec: 2 10 18 26 3 11 19 27
4554 3rd vec: 4 12 20 28 5 13 21 30
4555 4th vec: 6 14 22 30 7 15 23 31
4557 i.e., we interleave the contents of the four vectors in their order.
4559 We use interleave_high/low instructions to create such output. The input of
4560 each interleave_high/low operation is two vectors:
4561 1st vec 2nd vec
4562 0 1 2 3 4 5 6 7
4563 the even elements of the result vector are obtained left-to-right from the
4564 high/low elements of the first vector. The odd elements of the result are
4565 obtained left-to-right from the high/low elements of the second vector.
4566 The output of interleave_high will be: 0 4 1 5
4567 and of interleave_low: 2 6 3 7
4570 The permutation is done in log LENGTH stages. In each stage interleave_high
4571 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4572 where the first argument is taken from the first half of DR_CHAIN and the
4573 second argument from it's second half.
4574 In our example,
4576 I1: interleave_high (1st vec, 3rd vec)
4577 I2: interleave_low (1st vec, 3rd vec)
4578 I3: interleave_high (2nd vec, 4th vec)
4579 I4: interleave_low (2nd vec, 4th vec)
4581 The output for the first stage is:
4583 I1: 0 16 1 17 2 18 3 19
4584 I2: 4 20 5 21 6 22 7 23
4585 I3: 8 24 9 25 10 26 11 27
4586 I4: 12 28 13 29 14 30 15 31
4588 The output of the second stage, i.e. the final result is:
4590 I1: 0 8 16 24 1 9 17 25
4591 I2: 2 10 18 26 3 11 19 27
4592 I3: 4 12 20 28 5 13 21 30
4593 I4: 6 14 22 30 7 15 23 31. */
4595 void
4596 vect_permute_store_chain (vec<tree> dr_chain,
4597 unsigned int length,
4598 gimple *stmt,
4599 gimple_stmt_iterator *gsi,
4600 vec<tree> *result_chain)
4602 tree vect1, vect2, high, low;
4603 gimple *perm_stmt;
4604 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4605 tree perm_mask_low, perm_mask_high;
4606 tree data_ref;
4607 tree perm3_mask_low, perm3_mask_high;
4608 unsigned int i, n, log_length = exact_log2 (length);
4609 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4610 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4612 result_chain->quick_grow (length);
4613 memcpy (result_chain->address (), dr_chain.address (),
4614 length * sizeof (tree));
4616 if (length == 3)
4618 unsigned int j0 = 0, j1 = 0, j2 = 0;
4620 for (j = 0; j < 3; j++)
4622 int nelt0 = ((3 - j) * nelt) % 3;
4623 int nelt1 = ((3 - j) * nelt + 1) % 3;
4624 int nelt2 = ((3 - j) * nelt + 2) % 3;
4626 for (i = 0; i < nelt; i++)
4628 if (3 * i + nelt0 < nelt)
4629 sel[3 * i + nelt0] = j0++;
4630 if (3 * i + nelt1 < nelt)
4631 sel[3 * i + nelt1] = nelt + j1++;
4632 if (3 * i + nelt2 < nelt)
4633 sel[3 * i + nelt2] = 0;
4635 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4637 for (i = 0; i < nelt; i++)
4639 if (3 * i + nelt0 < nelt)
4640 sel[3 * i + nelt0] = 3 * i + nelt0;
4641 if (3 * i + nelt1 < nelt)
4642 sel[3 * i + nelt1] = 3 * i + nelt1;
4643 if (3 * i + nelt2 < nelt)
4644 sel[3 * i + nelt2] = nelt + j2++;
4646 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4648 vect1 = dr_chain[0];
4649 vect2 = dr_chain[1];
4651 /* Create interleaving stmt:
4652 low = VEC_PERM_EXPR <vect1, vect2,
4653 {j, nelt, *, j + 1, nelt + j + 1, *,
4654 j + 2, nelt + j + 2, *, ...}> */
4655 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4656 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4657 vect2, perm3_mask_low);
4658 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4660 vect1 = data_ref;
4661 vect2 = dr_chain[2];
4662 /* Create interleaving stmt:
4663 low = VEC_PERM_EXPR <vect1, vect2,
4664 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4665 6, 7, nelt + j + 2, ...}> */
4666 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4667 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4668 vect2, perm3_mask_high);
4669 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4670 (*result_chain)[j] = data_ref;
4673 else
4675 /* If length is not equal to 3 then only power of 2 is supported. */
4676 gcc_assert (exact_log2 (length) != -1);
4678 for (i = 0, n = nelt / 2; i < n; i++)
4680 sel[i * 2] = i;
4681 sel[i * 2 + 1] = i + nelt;
4683 perm_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4685 for (i = 0; i < nelt; i++)
4686 sel[i] += nelt / 2;
4687 perm_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4689 for (i = 0, n = log_length; i < n; i++)
4691 for (j = 0; j < length/2; j++)
4693 vect1 = dr_chain[j];
4694 vect2 = dr_chain[j+length/2];
4696 /* Create interleaving stmt:
4697 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4698 ...}> */
4699 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4700 perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
4701 vect2, perm_mask_high);
4702 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4703 (*result_chain)[2*j] = high;
4705 /* Create interleaving stmt:
4706 low = VEC_PERM_EXPR <vect1, vect2,
4707 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4708 ...}> */
4709 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4710 perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
4711 vect2, perm_mask_low);
4712 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4713 (*result_chain)[2*j+1] = low;
4715 memcpy (dr_chain.address (), result_chain->address (),
4716 length * sizeof (tree));
4721 /* Function vect_setup_realignment
4723 This function is called when vectorizing an unaligned load using
4724 the dr_explicit_realign[_optimized] scheme.
4725 This function generates the following code at the loop prolog:
4727 p = initial_addr;
4728 x msq_init = *(floor(p)); # prolog load
4729 realignment_token = call target_builtin;
4730 loop:
4731 x msq = phi (msq_init, ---)
4733 The stmts marked with x are generated only for the case of
4734 dr_explicit_realign_optimized.
4736 The code above sets up a new (vector) pointer, pointing to the first
4737 location accessed by STMT, and a "floor-aligned" load using that pointer.
4738 It also generates code to compute the "realignment-token" (if the relevant
4739 target hook was defined), and creates a phi-node at the loop-header bb
4740 whose arguments are the result of the prolog-load (created by this
4741 function) and the result of a load that takes place in the loop (to be
4742 created by the caller to this function).
4744 For the case of dr_explicit_realign_optimized:
4745 The caller to this function uses the phi-result (msq) to create the
4746 realignment code inside the loop, and sets up the missing phi argument,
4747 as follows:
4748 loop:
4749 msq = phi (msq_init, lsq)
4750 lsq = *(floor(p')); # load in loop
4751 result = realign_load (msq, lsq, realignment_token);
4753 For the case of dr_explicit_realign:
4754 loop:
4755 msq = *(floor(p)); # load in loop
4756 p' = p + (VS-1);
4757 lsq = *(floor(p')); # load in loop
4758 result = realign_load (msq, lsq, realignment_token);
4760 Input:
4761 STMT - (scalar) load stmt to be vectorized. This load accesses
4762 a memory location that may be unaligned.
4763 BSI - place where new code is to be inserted.
4764 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4765 is used.
4767 Output:
4768 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4769 target hook, if defined.
4770 Return value - the result of the loop-header phi node. */
4772 tree
4773 vect_setup_realignment (gimple *stmt, gimple_stmt_iterator *gsi,
4774 tree *realignment_token,
4775 enum dr_alignment_support alignment_support_scheme,
4776 tree init_addr,
4777 struct loop **at_loop)
4779 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4780 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4781 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4782 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4783 struct loop *loop = NULL;
4784 edge pe = NULL;
4785 tree scalar_dest = gimple_assign_lhs (stmt);
4786 tree vec_dest;
4787 gimple *inc;
4788 tree ptr;
4789 tree data_ref;
4790 basic_block new_bb;
4791 tree msq_init = NULL_TREE;
4792 tree new_temp;
4793 gphi *phi_stmt;
4794 tree msq = NULL_TREE;
4795 gimple_seq stmts = NULL;
4796 bool inv_p;
4797 bool compute_in_loop = false;
4798 bool nested_in_vect_loop = false;
4799 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4800 struct loop *loop_for_initial_load = NULL;
4802 if (loop_vinfo)
4804 loop = LOOP_VINFO_LOOP (loop_vinfo);
4805 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4808 gcc_assert (alignment_support_scheme == dr_explicit_realign
4809 || alignment_support_scheme == dr_explicit_realign_optimized);
4811 /* We need to generate three things:
4812 1. the misalignment computation
4813 2. the extra vector load (for the optimized realignment scheme).
4814 3. the phi node for the two vectors from which the realignment is
4815 done (for the optimized realignment scheme). */
4817 /* 1. Determine where to generate the misalignment computation.
4819 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4820 calculation will be generated by this function, outside the loop (in the
4821 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4822 caller, inside the loop.
4824 Background: If the misalignment remains fixed throughout the iterations of
4825 the loop, then both realignment schemes are applicable, and also the
4826 misalignment computation can be done outside LOOP. This is because we are
4827 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4828 are a multiple of VS (the Vector Size), and therefore the misalignment in
4829 different vectorized LOOP iterations is always the same.
4830 The problem arises only if the memory access is in an inner-loop nested
4831 inside LOOP, which is now being vectorized using outer-loop vectorization.
4832 This is the only case when the misalignment of the memory access may not
4833 remain fixed throughout the iterations of the inner-loop (as explained in
4834 detail in vect_supportable_dr_alignment). In this case, not only is the
4835 optimized realignment scheme not applicable, but also the misalignment
4836 computation (and generation of the realignment token that is passed to
4837 REALIGN_LOAD) have to be done inside the loop.
4839 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4840 or not, which in turn determines if the misalignment is computed inside
4841 the inner-loop, or outside LOOP. */
4843 if (init_addr != NULL_TREE || !loop_vinfo)
4845 compute_in_loop = true;
4846 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4850 /* 2. Determine where to generate the extra vector load.
4852 For the optimized realignment scheme, instead of generating two vector
4853 loads in each iteration, we generate a single extra vector load in the
4854 preheader of the loop, and in each iteration reuse the result of the
4855 vector load from the previous iteration. In case the memory access is in
4856 an inner-loop nested inside LOOP, which is now being vectorized using
4857 outer-loop vectorization, we need to determine whether this initial vector
4858 load should be generated at the preheader of the inner-loop, or can be
4859 generated at the preheader of LOOP. If the memory access has no evolution
4860 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4861 to be generated inside LOOP (in the preheader of the inner-loop). */
4863 if (nested_in_vect_loop)
4865 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4866 bool invariant_in_outerloop =
4867 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4868 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4870 else
4871 loop_for_initial_load = loop;
4872 if (at_loop)
4873 *at_loop = loop_for_initial_load;
4875 if (loop_for_initial_load)
4876 pe = loop_preheader_edge (loop_for_initial_load);
4878 /* 3. For the case of the optimized realignment, create the first vector
4879 load at the loop preheader. */
4881 if (alignment_support_scheme == dr_explicit_realign_optimized)
4883 /* Create msq_init = *(floor(p1)) in the loop preheader */
4884 gassign *new_stmt;
4886 gcc_assert (!compute_in_loop);
4887 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4888 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4889 NULL_TREE, &init_addr, NULL, &inc,
4890 true, &inv_p);
4891 if (TREE_CODE (ptr) == SSA_NAME)
4892 new_temp = copy_ssa_name (ptr);
4893 else
4894 new_temp = make_ssa_name (TREE_TYPE (ptr));
4895 new_stmt = gimple_build_assign
4896 (new_temp, BIT_AND_EXPR, ptr,
4897 build_int_cst (TREE_TYPE (ptr),
4898 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4899 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4900 gcc_assert (!new_bb);
4901 data_ref
4902 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4903 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4904 new_stmt = gimple_build_assign (vec_dest, data_ref);
4905 new_temp = make_ssa_name (vec_dest, new_stmt);
4906 gimple_assign_set_lhs (new_stmt, new_temp);
4907 if (pe)
4909 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4910 gcc_assert (!new_bb);
4912 else
4913 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4915 msq_init = gimple_assign_lhs (new_stmt);
4918 /* 4. Create realignment token using a target builtin, if available.
4919 It is done either inside the containing loop, or before LOOP (as
4920 determined above). */
4922 if (targetm.vectorize.builtin_mask_for_load)
4924 gcall *new_stmt;
4925 tree builtin_decl;
4927 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4928 if (!init_addr)
4930 /* Generate the INIT_ADDR computation outside LOOP. */
4931 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4932 NULL_TREE, loop);
4933 if (loop)
4935 pe = loop_preheader_edge (loop);
4936 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4937 gcc_assert (!new_bb);
4939 else
4940 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4943 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4944 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4945 vec_dest =
4946 vect_create_destination_var (scalar_dest,
4947 gimple_call_return_type (new_stmt));
4948 new_temp = make_ssa_name (vec_dest, new_stmt);
4949 gimple_call_set_lhs (new_stmt, new_temp);
4951 if (compute_in_loop)
4952 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4953 else
4955 /* Generate the misalignment computation outside LOOP. */
4956 pe = loop_preheader_edge (loop);
4957 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4958 gcc_assert (!new_bb);
4961 *realignment_token = gimple_call_lhs (new_stmt);
4963 /* The result of the CALL_EXPR to this builtin is determined from
4964 the value of the parameter and no global variables are touched
4965 which makes the builtin a "const" function. Requiring the
4966 builtin to have the "const" attribute makes it unnecessary
4967 to call mark_call_clobbered. */
4968 gcc_assert (TREE_READONLY (builtin_decl));
4971 if (alignment_support_scheme == dr_explicit_realign)
4972 return msq;
4974 gcc_assert (!compute_in_loop);
4975 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4978 /* 5. Create msq = phi <msq_init, lsq> in loop */
4980 pe = loop_preheader_edge (containing_loop);
4981 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4982 msq = make_ssa_name (vec_dest);
4983 phi_stmt = create_phi_node (msq, containing_loop->header);
4984 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4986 return msq;
4990 /* Function vect_grouped_load_supported.
4992 Returns TRUE if even and odd permutations are supported,
4993 and FALSE otherwise. */
4995 bool
4996 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4998 machine_mode mode = TYPE_MODE (vectype);
5000 /* vect_permute_load_chain requires the group size to be equal to 3 or
5001 be a power of two. */
5002 if (count != 3 && exact_log2 (count) == -1)
5004 if (dump_enabled_p ())
5005 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5006 "the size of the group of accesses"
5007 " is not a power of 2 or not equal to 3\n");
5008 return false;
5011 /* Check that the permutation is supported. */
5012 if (VECTOR_MODE_P (mode))
5014 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5015 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5017 if (count == 3)
5019 unsigned int k;
5020 for (k = 0; k < 3; k++)
5022 for (i = 0; i < nelt; i++)
5023 if (3 * i + k < 2 * nelt)
5024 sel[i] = 3 * i + k;
5025 else
5026 sel[i] = 0;
5027 if (!can_vec_perm_p (mode, false, sel))
5029 if (dump_enabled_p ())
5030 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5031 "shuffle of 3 loads is not supported by"
5032 " target\n");
5033 return false;
5035 for (i = 0, j = 0; i < nelt; i++)
5036 if (3 * i + k < 2 * nelt)
5037 sel[i] = i;
5038 else
5039 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5040 if (!can_vec_perm_p (mode, false, sel))
5042 if (dump_enabled_p ())
5043 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5044 "shuffle of 3 loads is not supported by"
5045 " target\n");
5046 return false;
5049 return true;
5051 else
5053 /* If length is not equal to 3 then only power of 2 is supported. */
5054 gcc_assert (exact_log2 (count) != -1);
5055 for (i = 0; i < nelt; i++)
5056 sel[i] = i * 2;
5057 if (can_vec_perm_p (mode, false, sel))
5059 for (i = 0; i < nelt; i++)
5060 sel[i] = i * 2 + 1;
5061 if (can_vec_perm_p (mode, false, sel))
5062 return true;
5067 if (dump_enabled_p ())
5068 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5069 "extract even/odd not supported by target\n");
5070 return false;
5073 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5074 type VECTYPE. */
5076 bool
5077 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5079 return vect_lanes_optab_supported_p ("vec_load_lanes",
5080 vec_load_lanes_optab,
5081 vectype, count);
5084 /* Function vect_permute_load_chain.
5086 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5087 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5088 the input data correctly. Return the final references for loads in
5089 RESULT_CHAIN.
5091 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5092 The input is 4 vectors each containing 8 elements. We assign a number to each
5093 element, the input sequence is:
5095 1st vec: 0 1 2 3 4 5 6 7
5096 2nd vec: 8 9 10 11 12 13 14 15
5097 3rd vec: 16 17 18 19 20 21 22 23
5098 4th vec: 24 25 26 27 28 29 30 31
5100 The output sequence should be:
5102 1st vec: 0 4 8 12 16 20 24 28
5103 2nd vec: 1 5 9 13 17 21 25 29
5104 3rd vec: 2 6 10 14 18 22 26 30
5105 4th vec: 3 7 11 15 19 23 27 31
5107 i.e., the first output vector should contain the first elements of each
5108 interleaving group, etc.
5110 We use extract_even/odd instructions to create such output. The input of
5111 each extract_even/odd operation is two vectors
5112 1st vec 2nd vec
5113 0 1 2 3 4 5 6 7
5115 and the output is the vector of extracted even/odd elements. The output of
5116 extract_even will be: 0 2 4 6
5117 and of extract_odd: 1 3 5 7
5120 The permutation is done in log LENGTH stages. In each stage extract_even
5121 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5122 their order. In our example,
5124 E1: extract_even (1st vec, 2nd vec)
5125 E2: extract_odd (1st vec, 2nd vec)
5126 E3: extract_even (3rd vec, 4th vec)
5127 E4: extract_odd (3rd vec, 4th vec)
5129 The output for the first stage will be:
5131 E1: 0 2 4 6 8 10 12 14
5132 E2: 1 3 5 7 9 11 13 15
5133 E3: 16 18 20 22 24 26 28 30
5134 E4: 17 19 21 23 25 27 29 31
5136 In order to proceed and create the correct sequence for the next stage (or
5137 for the correct output, if the second stage is the last one, as in our
5138 example), we first put the output of extract_even operation and then the
5139 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5140 The input for the second stage is:
5142 1st vec (E1): 0 2 4 6 8 10 12 14
5143 2nd vec (E3): 16 18 20 22 24 26 28 30
5144 3rd vec (E2): 1 3 5 7 9 11 13 15
5145 4th vec (E4): 17 19 21 23 25 27 29 31
5147 The output of the second stage:
5149 E1: 0 4 8 12 16 20 24 28
5150 E2: 2 6 10 14 18 22 26 30
5151 E3: 1 5 9 13 17 21 25 29
5152 E4: 3 7 11 15 19 23 27 31
5154 And RESULT_CHAIN after reordering:
5156 1st vec (E1): 0 4 8 12 16 20 24 28
5157 2nd vec (E3): 1 5 9 13 17 21 25 29
5158 3rd vec (E2): 2 6 10 14 18 22 26 30
5159 4th vec (E4): 3 7 11 15 19 23 27 31. */
5161 static void
5162 vect_permute_load_chain (vec<tree> dr_chain,
5163 unsigned int length,
5164 gimple *stmt,
5165 gimple_stmt_iterator *gsi,
5166 vec<tree> *result_chain)
5168 tree data_ref, first_vect, second_vect;
5169 tree perm_mask_even, perm_mask_odd;
5170 tree perm3_mask_low, perm3_mask_high;
5171 gimple *perm_stmt;
5172 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5173 unsigned int i, j, log_length = exact_log2 (length);
5174 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5175 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5177 result_chain->quick_grow (length);
5178 memcpy (result_chain->address (), dr_chain.address (),
5179 length * sizeof (tree));
5181 if (length == 3)
5183 unsigned int k;
5185 for (k = 0; k < 3; k++)
5187 for (i = 0; i < nelt; i++)
5188 if (3 * i + k < 2 * nelt)
5189 sel[i] = 3 * i + k;
5190 else
5191 sel[i] = 0;
5192 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
5194 for (i = 0, j = 0; i < nelt; i++)
5195 if (3 * i + k < 2 * nelt)
5196 sel[i] = i;
5197 else
5198 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5200 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
5202 first_vect = dr_chain[0];
5203 second_vect = dr_chain[1];
5205 /* Create interleaving stmt (low part of):
5206 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5207 ...}> */
5208 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5209 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5210 second_vect, perm3_mask_low);
5211 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5213 /* Create interleaving stmt (high part of):
5214 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5215 ...}> */
5216 first_vect = data_ref;
5217 second_vect = dr_chain[2];
5218 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5219 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5220 second_vect, perm3_mask_high);
5221 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5222 (*result_chain)[k] = data_ref;
5225 else
5227 /* If length is not equal to 3 then only power of 2 is supported. */
5228 gcc_assert (exact_log2 (length) != -1);
5230 for (i = 0; i < nelt; ++i)
5231 sel[i] = i * 2;
5232 perm_mask_even = vect_gen_perm_mask_checked (vectype, sel);
5234 for (i = 0; i < nelt; ++i)
5235 sel[i] = i * 2 + 1;
5236 perm_mask_odd = vect_gen_perm_mask_checked (vectype, sel);
5238 for (i = 0; i < log_length; i++)
5240 for (j = 0; j < length; j += 2)
5242 first_vect = dr_chain[j];
5243 second_vect = dr_chain[j+1];
5245 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5246 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5247 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5248 first_vect, second_vect,
5249 perm_mask_even);
5250 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5251 (*result_chain)[j/2] = data_ref;
5253 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5254 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5255 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5256 first_vect, second_vect,
5257 perm_mask_odd);
5258 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5259 (*result_chain)[j/2+length/2] = data_ref;
5261 memcpy (dr_chain.address (), result_chain->address (),
5262 length * sizeof (tree));
5267 /* Function vect_shift_permute_load_chain.
5269 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5270 sequence of stmts to reorder the input data accordingly.
5271 Return the final references for loads in RESULT_CHAIN.
5272 Return true if successed, false otherwise.
5274 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5275 The input is 3 vectors each containing 8 elements. We assign a
5276 number to each element, the input sequence is:
5278 1st vec: 0 1 2 3 4 5 6 7
5279 2nd vec: 8 9 10 11 12 13 14 15
5280 3rd vec: 16 17 18 19 20 21 22 23
5282 The output sequence should be:
5284 1st vec: 0 3 6 9 12 15 18 21
5285 2nd vec: 1 4 7 10 13 16 19 22
5286 3rd vec: 2 5 8 11 14 17 20 23
5288 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5290 First we shuffle all 3 vectors to get correct elements order:
5292 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5293 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5294 3rd vec: (16 19 22) (17 20 23) (18 21)
5296 Next we unite and shift vector 3 times:
5298 1st step:
5299 shift right by 6 the concatenation of:
5300 "1st vec" and "2nd vec"
5301 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5302 "2nd vec" and "3rd vec"
5303 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5304 "3rd vec" and "1st vec"
5305 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5306 | New vectors |
5308 So that now new vectors are:
5310 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5311 2nd vec: (10 13) (16 19 22) (17 20 23)
5312 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5314 2nd step:
5315 shift right by 5 the concatenation of:
5316 "1st vec" and "3rd vec"
5317 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5318 "2nd vec" and "1st vec"
5319 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5320 "3rd vec" and "2nd vec"
5321 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5322 | New vectors |
5324 So that now new vectors are:
5326 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5327 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5328 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5330 3rd step:
5331 shift right by 5 the concatenation of:
5332 "1st vec" and "1st vec"
5333 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5334 shift right by 3 the concatenation of:
5335 "2nd vec" and "2nd vec"
5336 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5337 | New vectors |
5339 So that now all vectors are READY:
5340 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5341 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5342 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5344 This algorithm is faster than one in vect_permute_load_chain if:
5345 1. "shift of a concatination" is faster than general permutation.
5346 This is usually so.
5347 2. The TARGET machine can't execute vector instructions in parallel.
5348 This is because each step of the algorithm depends on previous.
5349 The algorithm in vect_permute_load_chain is much more parallel.
5351 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5354 static bool
5355 vect_shift_permute_load_chain (vec<tree> dr_chain,
5356 unsigned int length,
5357 gimple *stmt,
5358 gimple_stmt_iterator *gsi,
5359 vec<tree> *result_chain)
5361 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5362 tree perm2_mask1, perm2_mask2, perm3_mask;
5363 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5364 gimple *perm_stmt;
5366 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5367 unsigned int i;
5368 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5369 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5370 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5371 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5373 result_chain->quick_grow (length);
5374 memcpy (result_chain->address (), dr_chain.address (),
5375 length * sizeof (tree));
5377 if (exact_log2 (length) != -1 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5379 unsigned int j, log_length = exact_log2 (length);
5380 for (i = 0; i < nelt / 2; ++i)
5381 sel[i] = i * 2;
5382 for (i = 0; i < nelt / 2; ++i)
5383 sel[nelt / 2 + i] = i * 2 + 1;
5384 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5386 if (dump_enabled_p ())
5387 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5388 "shuffle of 2 fields structure is not \
5389 supported by target\n");
5390 return false;
5392 perm2_mask1 = vect_gen_perm_mask_checked (vectype, sel);
5394 for (i = 0; i < nelt / 2; ++i)
5395 sel[i] = i * 2 + 1;
5396 for (i = 0; i < nelt / 2; ++i)
5397 sel[nelt / 2 + i] = i * 2;
5398 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5400 if (dump_enabled_p ())
5401 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5402 "shuffle of 2 fields structure is not \
5403 supported by target\n");
5404 return false;
5406 perm2_mask2 = vect_gen_perm_mask_checked (vectype, sel);
5408 /* Generating permutation constant to shift all elements.
5409 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5410 for (i = 0; i < nelt; i++)
5411 sel[i] = nelt / 2 + i;
5412 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5414 if (dump_enabled_p ())
5415 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5416 "shift permutation is not supported by target\n");
5417 return false;
5419 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5421 /* Generating permutation constant to select vector from 2.
5422 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5423 for (i = 0; i < nelt / 2; i++)
5424 sel[i] = i;
5425 for (i = nelt / 2; i < nelt; i++)
5426 sel[i] = nelt + i;
5427 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5429 if (dump_enabled_p ())
5430 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5431 "select is not supported by target\n");
5432 return false;
5434 select_mask = vect_gen_perm_mask_checked (vectype, sel);
5436 for (i = 0; i < log_length; i++)
5438 for (j = 0; j < length; j += 2)
5440 first_vect = dr_chain[j];
5441 second_vect = dr_chain[j + 1];
5443 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5444 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5445 first_vect, first_vect,
5446 perm2_mask1);
5447 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5448 vect[0] = data_ref;
5450 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5451 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5452 second_vect, second_vect,
5453 perm2_mask2);
5454 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5455 vect[1] = data_ref;
5457 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5458 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5459 vect[0], vect[1], shift1_mask);
5460 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5461 (*result_chain)[j/2 + length/2] = data_ref;
5463 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5464 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5465 vect[0], vect[1], select_mask);
5466 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5467 (*result_chain)[j/2] = data_ref;
5469 memcpy (dr_chain.address (), result_chain->address (),
5470 length * sizeof (tree));
5472 return true;
5474 if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
5476 unsigned int k = 0, l = 0;
5478 /* Generating permutation constant to get all elements in rigth order.
5479 For vector length 8 it is {0 3 6 1 4 7 2 5}. */
5480 for (i = 0; i < nelt; i++)
5482 if (3 * k + (l % 3) >= nelt)
5484 k = 0;
5485 l += (3 - (nelt % 3));
5487 sel[i] = 3 * k + (l % 3);
5488 k++;
5490 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5492 if (dump_enabled_p ())
5493 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5494 "shuffle of 3 fields structure is not \
5495 supported by target\n");
5496 return false;
5498 perm3_mask = vect_gen_perm_mask_checked (vectype, sel);
5500 /* Generating permutation constant to shift all elements.
5501 For vector length 8 it is {6 7 8 9 10 11 12 13}. */
5502 for (i = 0; i < nelt; i++)
5503 sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
5504 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5506 if (dump_enabled_p ())
5507 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5508 "shift permutation is not supported by target\n");
5509 return false;
5511 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5513 /* Generating permutation constant to shift all elements.
5514 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5515 for (i = 0; i < nelt; i++)
5516 sel[i] = 2 * (nelt / 3) + 1 + i;
5517 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5519 if (dump_enabled_p ())
5520 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5521 "shift permutation is not supported by target\n");
5522 return false;
5524 shift2_mask = vect_gen_perm_mask_checked (vectype, sel);
5526 /* Generating permutation constant to shift all elements.
5527 For vector length 8 it is {3 4 5 6 7 8 9 10}. */
5528 for (i = 0; i < nelt; i++)
5529 sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
5530 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5532 if (dump_enabled_p ())
5533 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5534 "shift permutation is not supported by target\n");
5535 return false;
5537 shift3_mask = vect_gen_perm_mask_checked (vectype, sel);
5539 /* Generating permutation constant to shift all elements.
5540 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5541 for (i = 0; i < nelt; i++)
5542 sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
5543 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5545 if (dump_enabled_p ())
5546 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5547 "shift permutation is not supported by target\n");
5548 return false;
5550 shift4_mask = vect_gen_perm_mask_checked (vectype, sel);
5552 for (k = 0; k < 3; k++)
5554 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
5555 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5556 dr_chain[k], dr_chain[k],
5557 perm3_mask);
5558 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5559 vect[k] = data_ref;
5562 for (k = 0; k < 3; k++)
5564 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
5565 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5566 vect[k % 3], vect[(k + 1) % 3],
5567 shift1_mask);
5568 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5569 vect_shift[k] = data_ref;
5572 for (k = 0; k < 3; k++)
5574 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
5575 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5576 vect_shift[(4 - k) % 3],
5577 vect_shift[(3 - k) % 3],
5578 shift2_mask);
5579 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5580 vect[k] = data_ref;
5583 (*result_chain)[3 - (nelt % 3)] = vect[2];
5585 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
5586 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
5587 vect[0], shift3_mask);
5588 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5589 (*result_chain)[nelt % 3] = data_ref;
5591 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
5592 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
5593 vect[1], shift4_mask);
5594 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5595 (*result_chain)[0] = data_ref;
5596 return true;
5598 return false;
5601 /* Function vect_transform_grouped_load.
5603 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5604 to perform their permutation and ascribe the result vectorized statements to
5605 the scalar statements.
5608 void
5609 vect_transform_grouped_load (gimple *stmt, vec<tree> dr_chain, int size,
5610 gimple_stmt_iterator *gsi)
5612 machine_mode mode;
5613 vec<tree> result_chain = vNULL;
5615 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5616 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5617 vectors, that are ready for vector computation. */
5618 result_chain.create (size);
5620 /* If reassociation width for vector type is 2 or greater target machine can
5621 execute 2 or more vector instructions in parallel. Otherwise try to
5622 get chain for loads group using vect_shift_permute_load_chain. */
5623 mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
5624 if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
5625 || exact_log2 (size) != -1
5626 || !vect_shift_permute_load_chain (dr_chain, size, stmt,
5627 gsi, &result_chain))
5628 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5629 vect_record_grouped_load_vectors (stmt, result_chain);
5630 result_chain.release ();
5633 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5634 generated as part of the vectorization of STMT. Assign the statement
5635 for each vector to the associated scalar statement. */
5637 void
5638 vect_record_grouped_load_vectors (gimple *stmt, vec<tree> result_chain)
5640 gimple *first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5641 gimple *next_stmt, *new_stmt;
5642 unsigned int i, gap_count;
5643 tree tmp_data_ref;
5645 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5646 Since we scan the chain starting from it's first node, their order
5647 corresponds the order of data-refs in RESULT_CHAIN. */
5648 next_stmt = first_stmt;
5649 gap_count = 1;
5650 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5652 if (!next_stmt)
5653 break;
5655 /* Skip the gaps. Loads created for the gaps will be removed by dead
5656 code elimination pass later. No need to check for the first stmt in
5657 the group, since it always exists.
5658 GROUP_GAP is the number of steps in elements from the previous
5659 access (if there is no gap GROUP_GAP is 1). We skip loads that
5660 correspond to the gaps. */
5661 if (next_stmt != first_stmt
5662 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5664 gap_count++;
5665 continue;
5668 while (next_stmt)
5670 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5671 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5672 copies, and we put the new vector statement in the first available
5673 RELATED_STMT. */
5674 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5675 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5676 else
5678 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5680 gimple *prev_stmt =
5681 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5682 gimple *rel_stmt =
5683 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5684 while (rel_stmt)
5686 prev_stmt = rel_stmt;
5687 rel_stmt =
5688 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5691 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5692 new_stmt;
5696 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5697 gap_count = 1;
5698 /* If NEXT_STMT accesses the same DR as the previous statement,
5699 put the same TMP_DATA_REF as its vectorized statement; otherwise
5700 get the next data-ref from RESULT_CHAIN. */
5701 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5702 break;
5707 /* Function vect_force_dr_alignment_p.
5709 Returns whether the alignment of a DECL can be forced to be aligned
5710 on ALIGNMENT bit boundary. */
5712 bool
5713 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5715 if (TREE_CODE (decl) != VAR_DECL)
5716 return false;
5718 if (decl_in_symtab_p (decl)
5719 && !symtab_node::get (decl)->can_increase_alignment_p ())
5720 return false;
5722 if (TREE_STATIC (decl))
5723 return (alignment <= MAX_OFILE_ALIGNMENT);
5724 else
5725 return (alignment <= MAX_STACK_ALIGNMENT);
5729 /* Return whether the data reference DR is supported with respect to its
5730 alignment.
5731 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5732 it is aligned, i.e., check if it is possible to vectorize it with different
5733 alignment. */
5735 enum dr_alignment_support
5736 vect_supportable_dr_alignment (struct data_reference *dr,
5737 bool check_aligned_accesses)
5739 gimple *stmt = DR_STMT (dr);
5740 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5741 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5742 machine_mode mode = TYPE_MODE (vectype);
5743 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5744 struct loop *vect_loop = NULL;
5745 bool nested_in_vect_loop = false;
5747 if (aligned_access_p (dr) && !check_aligned_accesses)
5748 return dr_aligned;
5750 /* For now assume all conditional loads/stores support unaligned
5751 access without any special code. */
5752 if (is_gimple_call (stmt)
5753 && gimple_call_internal_p (stmt)
5754 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
5755 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
5756 return dr_unaligned_supported;
5758 if (loop_vinfo)
5760 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5761 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5764 /* Possibly unaligned access. */
5766 /* We can choose between using the implicit realignment scheme (generating
5767 a misaligned_move stmt) and the explicit realignment scheme (generating
5768 aligned loads with a REALIGN_LOAD). There are two variants to the
5769 explicit realignment scheme: optimized, and unoptimized.
5770 We can optimize the realignment only if the step between consecutive
5771 vector loads is equal to the vector size. Since the vector memory
5772 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5773 is guaranteed that the misalignment amount remains the same throughout the
5774 execution of the vectorized loop. Therefore, we can create the
5775 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5776 at the loop preheader.
5778 However, in the case of outer-loop vectorization, when vectorizing a
5779 memory access in the inner-loop nested within the LOOP that is now being
5780 vectorized, while it is guaranteed that the misalignment of the
5781 vectorized memory access will remain the same in different outer-loop
5782 iterations, it is *not* guaranteed that is will remain the same throughout
5783 the execution of the inner-loop. This is because the inner-loop advances
5784 with the original scalar step (and not in steps of VS). If the inner-loop
5785 step happens to be a multiple of VS, then the misalignment remains fixed
5786 and we can use the optimized realignment scheme. For example:
5788 for (i=0; i<N; i++)
5789 for (j=0; j<M; j++)
5790 s += a[i+j];
5792 When vectorizing the i-loop in the above example, the step between
5793 consecutive vector loads is 1, and so the misalignment does not remain
5794 fixed across the execution of the inner-loop, and the realignment cannot
5795 be optimized (as illustrated in the following pseudo vectorized loop):
5797 for (i=0; i<N; i+=4)
5798 for (j=0; j<M; j++){
5799 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5800 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5801 // (assuming that we start from an aligned address).
5804 We therefore have to use the unoptimized realignment scheme:
5806 for (i=0; i<N; i+=4)
5807 for (j=k; j<M; j+=4)
5808 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5809 // that the misalignment of the initial address is
5810 // 0).
5812 The loop can then be vectorized as follows:
5814 for (k=0; k<4; k++){
5815 rt = get_realignment_token (&vp[k]);
5816 for (i=0; i<N; i+=4){
5817 v1 = vp[i+k];
5818 for (j=k; j<M; j+=4){
5819 v2 = vp[i+j+VS-1];
5820 va = REALIGN_LOAD <v1,v2,rt>;
5821 vs += va;
5822 v1 = v2;
5825 } */
5827 if (DR_IS_READ (dr))
5829 bool is_packed = false;
5830 tree type = (TREE_TYPE (DR_REF (dr)));
5832 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5833 && (!targetm.vectorize.builtin_mask_for_load
5834 || targetm.vectorize.builtin_mask_for_load ()))
5836 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5837 if ((nested_in_vect_loop
5838 && (TREE_INT_CST_LOW (DR_STEP (dr))
5839 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5840 || !loop_vinfo)
5841 return dr_explicit_realign;
5842 else
5843 return dr_explicit_realign_optimized;
5845 if (!known_alignment_for_access_p (dr))
5846 is_packed = not_size_aligned (DR_REF (dr));
5848 if ((TYPE_USER_ALIGN (type) && !is_packed)
5849 || targetm.vectorize.
5850 support_vector_misalignment (mode, type,
5851 DR_MISALIGNMENT (dr), is_packed))
5852 /* Can't software pipeline the loads, but can at least do them. */
5853 return dr_unaligned_supported;
5855 else
5857 bool is_packed = false;
5858 tree type = (TREE_TYPE (DR_REF (dr)));
5860 if (!known_alignment_for_access_p (dr))
5861 is_packed = not_size_aligned (DR_REF (dr));
5863 if ((TYPE_USER_ALIGN (type) && !is_packed)
5864 || targetm.vectorize.
5865 support_vector_misalignment (mode, type,
5866 DR_MISALIGNMENT (dr), is_packed))
5867 return dr_unaligned_supported;
5870 /* Unsupported. */
5871 return dr_unaligned_unsupported;