re PR target/66258 (compiling a stdarg function with arch +nofp generates an ICE)
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob5c3fa3d56d9e3f381fe472113788f1cf57d4c5e3
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 "tm.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "tm_p.h"
40 #include "target.h"
41 #include "predict.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "basic-block.h"
47 #include "gimple-pretty-print.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimplify.h"
55 #include "gimple-iterator.h"
56 #include "gimplify-me.h"
57 #include "gimple-ssa.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-ssa-loop-ivopts.h"
63 #include "tree-ssa-loop-manip.h"
64 #include "tree-ssa-loop.h"
65 #include "cfgloop.h"
66 #include "tree-chrec.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-vectorizer.h"
69 #include "diagnostic-core.h"
70 #include "hash-map.h"
71 #include "plugin-api.h"
72 #include "ipa-ref.h"
73 #include "cgraph.h"
74 /* Need to include rtl.h, expr.h, etc. for optabs. */
75 #include "hashtab.h"
76 #include "rtl.h"
77 #include "flags.h"
78 #include "statistics.h"
79 #include "real.h"
80 #include "fixed-value.h"
81 #include "insn-config.h"
82 #include "expmed.h"
83 #include "dojump.h"
84 #include "explow.h"
85 #include "calls.h"
86 #include "emit-rtl.h"
87 #include "varasm.h"
88 #include "stmt.h"
89 #include "expr.h"
90 #include "insn-codes.h"
91 #include "optabs.h"
92 #include "builtins.h"
94 /* Return true if load- or store-lanes optab OPTAB is implemented for
95 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
97 static bool
98 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
99 tree vectype, unsigned HOST_WIDE_INT count)
101 machine_mode mode, array_mode;
102 bool limit_p;
104 mode = TYPE_MODE (vectype);
105 limit_p = !targetm.array_mode_supported_p (mode, count);
106 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
107 MODE_INT, limit_p);
109 if (array_mode == BLKmode)
111 if (dump_enabled_p ())
112 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
113 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
114 GET_MODE_NAME (mode), count);
115 return false;
118 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
120 if (dump_enabled_p ())
121 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
122 "cannot use %s<%s><%s>\n", name,
123 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
124 return false;
127 if (dump_enabled_p ())
128 dump_printf_loc (MSG_NOTE, vect_location,
129 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
130 GET_MODE_NAME (mode));
132 return true;
136 /* Return the smallest scalar part of STMT.
137 This is used to determine the vectype of the stmt. We generally set the
138 vectype according to the type of the result (lhs). For stmts whose
139 result-type is different than the type of the arguments (e.g., demotion,
140 promotion), vectype will be reset appropriately (later). Note that we have
141 to visit the smallest datatype in this function, because that determines the
142 VF. If the smallest datatype in the loop is present only as the rhs of a
143 promotion operation - we'd miss it.
144 Such a case, where a variable of this datatype does not appear in the lhs
145 anywhere in the loop, can only occur if it's an invariant: e.g.:
146 'int_x = (int) short_inv', which we'd expect to have been optimized away by
147 invariant motion. However, we cannot rely on invariant motion to always
148 take invariants out of the loop, and so in the case of promotion we also
149 have to check the rhs.
150 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
151 types. */
153 tree
154 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
155 HOST_WIDE_INT *rhs_size_unit)
157 tree scalar_type = gimple_expr_type (stmt);
158 HOST_WIDE_INT lhs, rhs;
160 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
162 if (is_gimple_assign (stmt)
163 && (gimple_assign_cast_p (stmt)
164 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
165 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
166 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
168 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
170 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
171 if (rhs < lhs)
172 scalar_type = rhs_type;
175 *lhs_size_unit = lhs;
176 *rhs_size_unit = rhs;
177 return scalar_type;
181 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
182 tested at run-time. Return TRUE if DDR was successfully inserted.
183 Return false if versioning is not supported. */
185 static bool
186 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
188 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
190 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
191 return false;
193 if (dump_enabled_p ())
195 dump_printf_loc (MSG_NOTE, vect_location,
196 "mark for run-time aliasing test between ");
197 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
198 dump_printf (MSG_NOTE, " and ");
199 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
200 dump_printf (MSG_NOTE, "\n");
203 if (optimize_loop_nest_for_size_p (loop))
205 if (dump_enabled_p ())
206 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
207 "versioning not supported when optimizing"
208 " for size.\n");
209 return false;
212 /* FORNOW: We don't support versioning with outer-loop vectorization. */
213 if (loop->inner)
215 if (dump_enabled_p ())
216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
217 "versioning not yet supported for outer-loops.\n");
218 return false;
221 /* FORNOW: We don't support creating runtime alias tests for non-constant
222 step. */
223 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
224 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
226 if (dump_enabled_p ())
227 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
228 "versioning not yet supported for non-constant "
229 "step\n");
230 return false;
233 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
234 return true;
238 /* Function vect_analyze_data_ref_dependence.
240 Return TRUE if there (might) exist a dependence between a memory-reference
241 DRA and a memory-reference DRB. When versioning for alias may check a
242 dependence at run-time, return FALSE. Adjust *MAX_VF according to
243 the data dependence. */
245 static bool
246 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
247 loop_vec_info loop_vinfo, int *max_vf)
249 unsigned int i;
250 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
251 struct data_reference *dra = DDR_A (ddr);
252 struct data_reference *drb = DDR_B (ddr);
253 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
254 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
255 lambda_vector dist_v;
256 unsigned int loop_depth;
258 /* In loop analysis all data references should be vectorizable. */
259 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
260 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
261 gcc_unreachable ();
263 /* Independent data accesses. */
264 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
265 return false;
267 if (dra == drb
268 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
269 return false;
271 /* Even if we have an anti-dependence then, as the vectorized loop covers at
272 least two scalar iterations, there is always also a true dependence.
273 As the vectorizer does not re-order loads and stores we can ignore
274 the anti-dependence if TBAA can disambiguate both DRs similar to the
275 case with known negative distance anti-dependences (positive
276 distance anti-dependences would violate TBAA constraints). */
277 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
278 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
279 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
280 get_alias_set (DR_REF (drb))))
281 return false;
283 /* Unknown data dependence. */
284 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
286 /* If user asserted safelen consecutive iterations can be
287 executed concurrently, assume independence. */
288 if (loop->safelen >= 2)
290 if (loop->safelen < *max_vf)
291 *max_vf = loop->safelen;
292 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
293 return false;
296 if (STMT_VINFO_GATHER_P (stmtinfo_a)
297 || STMT_VINFO_GATHER_P (stmtinfo_b))
299 if (dump_enabled_p ())
301 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
302 "versioning for alias not supported for: "
303 "can't determine dependence between ");
304 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
305 DR_REF (dra));
306 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
307 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
308 DR_REF (drb));
309 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
311 return true;
314 if (dump_enabled_p ())
316 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
317 "versioning for alias required: "
318 "can't determine dependence between ");
319 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
320 DR_REF (dra));
321 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
322 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
323 DR_REF (drb));
324 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
327 /* Add to list of ddrs that need to be tested at run-time. */
328 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
331 /* Known data dependence. */
332 if (DDR_NUM_DIST_VECTS (ddr) == 0)
334 /* If user asserted safelen consecutive iterations can be
335 executed concurrently, assume independence. */
336 if (loop->safelen >= 2)
338 if (loop->safelen < *max_vf)
339 *max_vf = loop->safelen;
340 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
341 return false;
344 if (STMT_VINFO_GATHER_P (stmtinfo_a)
345 || STMT_VINFO_GATHER_P (stmtinfo_b))
347 if (dump_enabled_p ())
349 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
350 "versioning for alias not supported for: "
351 "bad dist vector for ");
352 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
353 DR_REF (dra));
354 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
355 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
356 DR_REF (drb));
357 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
359 return true;
362 if (dump_enabled_p ())
364 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
365 "versioning for alias required: "
366 "bad dist vector for ");
367 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
368 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
369 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
370 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
372 /* Add to list of ddrs that need to be tested at run-time. */
373 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
376 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
377 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
379 int dist = dist_v[loop_depth];
381 if (dump_enabled_p ())
382 dump_printf_loc (MSG_NOTE, vect_location,
383 "dependence distance = %d.\n", dist);
385 if (dist == 0)
387 if (dump_enabled_p ())
389 dump_printf_loc (MSG_NOTE, vect_location,
390 "dependence distance == 0 between ");
391 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
392 dump_printf (MSG_NOTE, " and ");
393 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
394 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
397 /* When we perform grouped accesses and perform implicit CSE
398 by detecting equal accesses and doing disambiguation with
399 runtime alias tests like for
400 .. = a[i];
401 .. = a[i+1];
402 a[i] = ..;
403 a[i+1] = ..;
404 *p = ..;
405 .. = a[i];
406 .. = a[i+1];
407 where we will end up loading { a[i], a[i+1] } once, make
408 sure that inserting group loads before the first load and
409 stores after the last store will do the right thing.
410 Similar for groups like
411 a[i] = ...;
412 ... = a[i];
413 a[i+1] = ...;
414 where loads from the group interleave with the store. */
415 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
416 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
418 gimple earlier_stmt;
419 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
420 if (DR_IS_WRITE
421 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
423 if (dump_enabled_p ())
424 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
425 "READ_WRITE dependence in interleaving."
426 "\n");
427 return true;
431 continue;
434 if (dist > 0 && DDR_REVERSED_P (ddr))
436 /* If DDR_REVERSED_P the order of the data-refs in DDR was
437 reversed (to make distance vector positive), and the actual
438 distance is negative. */
439 if (dump_enabled_p ())
440 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
441 "dependence distance negative.\n");
442 /* Record a negative dependence distance to later limit the
443 amount of stmt copying / unrolling we can perform.
444 Only need to handle read-after-write dependence. */
445 if (DR_IS_READ (drb)
446 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
447 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
448 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
449 continue;
452 if (abs (dist) >= 2
453 && abs (dist) < *max_vf)
455 /* The dependence distance requires reduction of the maximal
456 vectorization factor. */
457 *max_vf = abs (dist);
458 if (dump_enabled_p ())
459 dump_printf_loc (MSG_NOTE, vect_location,
460 "adjusting maximal vectorization factor to %i\n",
461 *max_vf);
464 if (abs (dist) >= *max_vf)
466 /* Dependence distance does not create dependence, as far as
467 vectorization is concerned, in this case. */
468 if (dump_enabled_p ())
469 dump_printf_loc (MSG_NOTE, vect_location,
470 "dependence distance >= VF.\n");
471 continue;
474 if (dump_enabled_p ())
476 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
477 "not vectorized, possible dependence "
478 "between data-refs ");
479 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
480 dump_printf (MSG_NOTE, " and ");
481 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
482 dump_printf (MSG_NOTE, "\n");
485 return true;
488 return false;
491 /* Function vect_analyze_data_ref_dependences.
493 Examine all the data references in the loop, and make sure there do not
494 exist any data dependences between them. Set *MAX_VF according to
495 the maximum vectorization factor the data dependences allow. */
497 bool
498 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
500 unsigned int i;
501 struct data_dependence_relation *ddr;
503 if (dump_enabled_p ())
504 dump_printf_loc (MSG_NOTE, vect_location,
505 "=== vect_analyze_data_ref_dependences ===\n");
507 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
508 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
509 &LOOP_VINFO_DDRS (loop_vinfo),
510 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
511 return false;
513 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
514 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
515 return false;
517 return true;
521 /* Function vect_slp_analyze_data_ref_dependence.
523 Return TRUE if there (might) exist a dependence between a memory-reference
524 DRA and a memory-reference DRB. When versioning for alias may check a
525 dependence at run-time, return FALSE. Adjust *MAX_VF according to
526 the data dependence. */
528 static bool
529 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
531 struct data_reference *dra = DDR_A (ddr);
532 struct data_reference *drb = DDR_B (ddr);
534 /* We need to check dependences of statements marked as unvectorizable
535 as well, they still can prohibit vectorization. */
537 /* Independent data accesses. */
538 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
539 return false;
541 if (dra == drb)
542 return false;
544 /* Read-read is OK. */
545 if (DR_IS_READ (dra) && DR_IS_READ (drb))
546 return false;
548 /* If dra and drb are part of the same interleaving chain consider
549 them independent. */
550 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
551 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
552 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
553 return false;
555 /* Unknown data dependence. */
556 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
558 if (dump_enabled_p ())
560 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
561 "can't determine dependence between ");
562 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
563 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
564 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
565 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
568 else if (dump_enabled_p ())
570 dump_printf_loc (MSG_NOTE, vect_location,
571 "determined dependence between ");
572 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
573 dump_printf (MSG_NOTE, " and ");
574 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
575 dump_printf (MSG_NOTE, "\n");
578 /* We do not vectorize basic blocks with write-write dependencies. */
579 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
580 return true;
582 /* If we have a read-write dependence check that the load is before the store.
583 When we vectorize basic blocks, vector load can be only before
584 corresponding scalar load, and vector store can be only after its
585 corresponding scalar store. So the order of the acceses is preserved in
586 case the load is before the store. */
587 gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
588 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
590 /* That only holds for load-store pairs taking part in vectorization. */
591 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
592 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
593 return false;
596 return true;
600 /* Function vect_analyze_data_ref_dependences.
602 Examine all the data references in the basic-block, and make sure there
603 do not exist any data dependences between them. Set *MAX_VF according to
604 the maximum vectorization factor the data dependences allow. */
606 bool
607 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
609 struct data_dependence_relation *ddr;
610 unsigned int i;
612 if (dump_enabled_p ())
613 dump_printf_loc (MSG_NOTE, vect_location,
614 "=== vect_slp_analyze_data_ref_dependences ===\n");
616 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
617 &BB_VINFO_DDRS (bb_vinfo),
618 vNULL, true))
619 return false;
621 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
622 if (vect_slp_analyze_data_ref_dependence (ddr))
623 return false;
625 return true;
629 /* Function vect_compute_data_ref_alignment
631 Compute the misalignment of the data reference DR.
633 Output:
634 1. If during the misalignment computation it is found that the data reference
635 cannot be vectorized then false is returned.
636 2. DR_MISALIGNMENT (DR) is defined.
638 FOR NOW: No analysis is actually performed. Misalignment is calculated
639 only for trivial cases. TODO. */
641 static bool
642 vect_compute_data_ref_alignment (struct data_reference *dr)
644 gimple stmt = DR_STMT (dr);
645 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
646 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
647 struct loop *loop = NULL;
648 tree ref = DR_REF (dr);
649 tree vectype;
650 tree base, base_addr;
651 bool base_aligned;
652 tree misalign = NULL_TREE;
653 tree aligned_to;
654 unsigned HOST_WIDE_INT alignment;
656 if (dump_enabled_p ())
657 dump_printf_loc (MSG_NOTE, vect_location,
658 "vect_compute_data_ref_alignment:\n");
660 if (loop_vinfo)
661 loop = LOOP_VINFO_LOOP (loop_vinfo);
663 /* Initialize misalignment to unknown. */
664 SET_DR_MISALIGNMENT (dr, -1);
666 /* Strided accesses perform only component accesses, misalignment information
667 is irrelevant for them. */
668 if (STMT_VINFO_STRIDED_P (stmt_info)
669 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
670 return true;
672 if (tree_fits_shwi_p (DR_STEP (dr)))
673 misalign = DR_INIT (dr);
674 aligned_to = DR_ALIGNED_TO (dr);
675 base_addr = DR_BASE_ADDRESS (dr);
676 vectype = STMT_VINFO_VECTYPE (stmt_info);
678 /* In case the dataref is in an inner-loop of the loop that is being
679 vectorized (LOOP), we use the base and misalignment information
680 relative to the outer-loop (LOOP). This is ok only if the misalignment
681 stays the same throughout the execution of the inner-loop, which is why
682 we have to check that the stride of the dataref in the inner-loop evenly
683 divides by the vector size. */
684 if (loop && nested_in_vect_loop_p (loop, stmt))
686 tree step = DR_STEP (dr);
688 if (tree_fits_shwi_p (step)
689 && tree_to_shwi (step) % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
691 if (dump_enabled_p ())
692 dump_printf_loc (MSG_NOTE, vect_location,
693 "inner step divides the vector-size.\n");
694 misalign = STMT_VINFO_DR_INIT (stmt_info);
695 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
696 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
698 else
700 if (dump_enabled_p ())
701 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
702 "inner step doesn't divide the vector-size.\n");
703 misalign = NULL_TREE;
707 /* Similarly, if we're doing basic-block vectorization, we can only use
708 base and misalignment information relative to an innermost loop if the
709 misalignment stays the same throughout the execution of the loop.
710 As above, this is the case if the stride of the dataref evenly divides
711 by the vector size. */
712 if (!loop)
714 tree step = DR_STEP (dr);
716 if (tree_fits_shwi_p (step)
717 && tree_to_shwi (step) % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
719 if (dump_enabled_p ())
720 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
721 "SLP: step doesn't divide the vector-size.\n");
722 misalign = NULL_TREE;
726 alignment = TYPE_ALIGN_UNIT (vectype);
728 if ((compare_tree_int (aligned_to, alignment) < 0)
729 || !misalign)
731 if (dump_enabled_p ())
733 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
734 "Unknown alignment for access: ");
735 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
736 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
738 return true;
741 /* To look at alignment of the base we have to preserve an inner MEM_REF
742 as that carries alignment information of the actual access. */
743 base = ref;
744 while (handled_component_p (base))
745 base = TREE_OPERAND (base, 0);
746 if (TREE_CODE (base) == MEM_REF)
747 base = build2 (MEM_REF, TREE_TYPE (base), base_addr,
748 build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)), 0));
750 if (get_object_alignment (base) >= TYPE_ALIGN (vectype))
751 base_aligned = true;
752 else
753 base_aligned = false;
755 if (!base_aligned)
757 /* Strip an inner MEM_REF to a bare decl if possible. */
758 if (TREE_CODE (base) == MEM_REF
759 && integer_zerop (TREE_OPERAND (base, 1))
760 && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
761 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
763 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
765 if (dump_enabled_p ())
767 dump_printf_loc (MSG_NOTE, vect_location,
768 "can't force alignment of ref: ");
769 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
770 dump_printf (MSG_NOTE, "\n");
772 return true;
775 /* Force the alignment of the decl.
776 NOTE: This is the only change to the code we make during
777 the analysis phase, before deciding to vectorize the loop. */
778 if (dump_enabled_p ())
780 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
781 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
782 dump_printf (MSG_NOTE, "\n");
785 ((dataref_aux *)dr->aux)->base_decl = base;
786 ((dataref_aux *)dr->aux)->base_misaligned = true;
789 /* If this is a backward running DR then first access in the larger
790 vectype actually is N-1 elements before the address in the DR.
791 Adjust misalign accordingly. */
792 if (tree_int_cst_sgn (DR_STEP (dr)) < 0)
794 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
795 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
796 otherwise we wouldn't be here. */
797 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
798 /* PLUS because DR_STEP was negative. */
799 misalign = size_binop (PLUS_EXPR, misalign, offset);
802 SET_DR_MISALIGNMENT (dr,
803 wi::mod_floor (misalign, alignment, SIGNED).to_uhwi ());
805 if (dump_enabled_p ())
807 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
808 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
809 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
810 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
813 return true;
817 /* Function vect_compute_data_refs_alignment
819 Compute the misalignment of data references in the loop.
820 Return FALSE if a data reference is found that cannot be vectorized. */
822 static bool
823 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
824 bb_vec_info bb_vinfo)
826 vec<data_reference_p> datarefs;
827 struct data_reference *dr;
828 unsigned int i;
830 if (loop_vinfo)
831 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
832 else
833 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
835 FOR_EACH_VEC_ELT (datarefs, i, dr)
836 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
837 && !vect_compute_data_ref_alignment (dr))
839 if (bb_vinfo)
841 /* Mark unsupported statement as unvectorizable. */
842 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
843 continue;
845 else
846 return false;
849 return true;
853 /* Function vect_update_misalignment_for_peel
855 DR - the data reference whose misalignment is to be adjusted.
856 DR_PEEL - the data reference whose misalignment is being made
857 zero in the vector loop by the peel.
858 NPEEL - the number of iterations in the peel loop if the misalignment
859 of DR_PEEL is known at compile time. */
861 static void
862 vect_update_misalignment_for_peel (struct data_reference *dr,
863 struct data_reference *dr_peel, int npeel)
865 unsigned int i;
866 vec<dr_p> same_align_drs;
867 struct data_reference *current_dr;
868 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
869 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
870 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
871 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
873 /* For interleaved data accesses the step in the loop must be multiplied by
874 the size of the interleaving group. */
875 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
876 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
877 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
878 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
880 /* It can be assumed that the data refs with the same alignment as dr_peel
881 are aligned in the vector loop. */
882 same_align_drs
883 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
884 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
886 if (current_dr != dr)
887 continue;
888 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
889 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
890 SET_DR_MISALIGNMENT (dr, 0);
891 return;
894 if (known_alignment_for_access_p (dr)
895 && known_alignment_for_access_p (dr_peel))
897 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
898 int misal = DR_MISALIGNMENT (dr);
899 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
900 misal += negative ? -npeel * dr_size : npeel * dr_size;
901 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
902 SET_DR_MISALIGNMENT (dr, misal);
903 return;
906 if (dump_enabled_p ())
907 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
908 SET_DR_MISALIGNMENT (dr, -1);
912 /* Function vect_verify_datarefs_alignment
914 Return TRUE if all data references in the loop can be
915 handled with respect to alignment. */
917 bool
918 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
920 vec<data_reference_p> datarefs;
921 struct data_reference *dr;
922 enum dr_alignment_support supportable_dr_alignment;
923 unsigned int i;
925 if (loop_vinfo)
926 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
927 else
928 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
930 FOR_EACH_VEC_ELT (datarefs, i, dr)
932 gimple stmt = DR_STMT (dr);
933 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
935 if (!STMT_VINFO_RELEVANT_P (stmt_info))
936 continue;
938 /* For interleaving, only the alignment of the first access matters.
939 Skip statements marked as not vectorizable. */
940 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
941 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
942 || !STMT_VINFO_VECTORIZABLE (stmt_info))
943 continue;
945 /* Strided accesses perform only component accesses, alignment is
946 irrelevant for them. */
947 if (STMT_VINFO_STRIDED_P (stmt_info)
948 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
949 continue;
951 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
952 if (!supportable_dr_alignment)
954 if (dump_enabled_p ())
956 if (DR_IS_READ (dr))
957 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
958 "not vectorized: unsupported unaligned load.");
959 else
960 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
961 "not vectorized: unsupported unaligned "
962 "store.");
964 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
965 DR_REF (dr));
966 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
968 return false;
970 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
971 dump_printf_loc (MSG_NOTE, vect_location,
972 "Vectorizing an unaligned access.\n");
974 return true;
977 /* Given an memory reference EXP return whether its alignment is less
978 than its size. */
980 static bool
981 not_size_aligned (tree exp)
983 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
984 return true;
986 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
987 > get_object_alignment (exp));
990 /* Function vector_alignment_reachable_p
992 Return true if vector alignment for DR is reachable by peeling
993 a few loop iterations. Return false otherwise. */
995 static bool
996 vector_alignment_reachable_p (struct data_reference *dr)
998 gimple stmt = DR_STMT (dr);
999 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1000 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1002 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1004 /* For interleaved access we peel only if number of iterations in
1005 the prolog loop ({VF - misalignment}), is a multiple of the
1006 number of the interleaved accesses. */
1007 int elem_size, mis_in_elements;
1008 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1010 /* FORNOW: handle only known alignment. */
1011 if (!known_alignment_for_access_p (dr))
1012 return false;
1014 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
1015 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1017 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
1018 return false;
1021 /* If misalignment is known at the compile time then allow peeling
1022 only if natural alignment is reachable through peeling. */
1023 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1025 HOST_WIDE_INT elmsize =
1026 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1027 if (dump_enabled_p ())
1029 dump_printf_loc (MSG_NOTE, vect_location,
1030 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1031 dump_printf (MSG_NOTE,
1032 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1034 if (DR_MISALIGNMENT (dr) % elmsize)
1036 if (dump_enabled_p ())
1037 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1038 "data size does not divide the misalignment.\n");
1039 return false;
1043 if (!known_alignment_for_access_p (dr))
1045 tree type = TREE_TYPE (DR_REF (dr));
1046 bool is_packed = not_size_aligned (DR_REF (dr));
1047 if (dump_enabled_p ())
1048 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1049 "Unknown misalignment, is_packed = %d\n",is_packed);
1050 if ((TYPE_USER_ALIGN (type) && !is_packed)
1051 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1052 return true;
1053 else
1054 return false;
1057 return true;
1061 /* Calculate the cost of the memory access represented by DR. */
1063 static void
1064 vect_get_data_access_cost (struct data_reference *dr,
1065 unsigned int *inside_cost,
1066 unsigned int *outside_cost,
1067 stmt_vector_for_cost *body_cost_vec)
1069 gimple stmt = DR_STMT (dr);
1070 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1071 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1072 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1073 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1074 int ncopies = vf / nunits;
1076 if (DR_IS_READ (dr))
1077 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1078 NULL, body_cost_vec, false);
1079 else
1080 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1082 if (dump_enabled_p ())
1083 dump_printf_loc (MSG_NOTE, vect_location,
1084 "vect_get_data_access_cost: inside_cost = %d, "
1085 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1089 /* Insert DR into peeling hash table with NPEEL as key. */
1091 static void
1092 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1093 int npeel)
1095 struct _vect_peel_info elem, *slot;
1096 _vect_peel_info **new_slot;
1097 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1099 elem.npeel = npeel;
1100 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find (&elem);
1101 if (slot)
1102 slot->count++;
1103 else
1105 slot = XNEW (struct _vect_peel_info);
1106 slot->npeel = npeel;
1107 slot->dr = dr;
1108 slot->count = 1;
1109 new_slot
1110 = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find_slot (slot, INSERT);
1111 *new_slot = slot;
1114 if (!supportable_dr_alignment
1115 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1116 slot->count += VECT_MAX_COST;
1120 /* Traverse peeling hash table to find peeling option that aligns maximum
1121 number of data accesses. */
1124 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1125 _vect_peel_extended_info *max)
1127 vect_peel_info elem = *slot;
1129 if (elem->count > max->peel_info.count
1130 || (elem->count == max->peel_info.count
1131 && max->peel_info.npeel > elem->npeel))
1133 max->peel_info.npeel = elem->npeel;
1134 max->peel_info.count = elem->count;
1135 max->peel_info.dr = elem->dr;
1138 return 1;
1142 /* Traverse peeling hash table and calculate cost for each peeling option.
1143 Find the one with the lowest cost. */
1146 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1147 _vect_peel_extended_info *min)
1149 vect_peel_info elem = *slot;
1150 int save_misalignment, dummy;
1151 unsigned int inside_cost = 0, outside_cost = 0, i;
1152 gimple stmt = DR_STMT (elem->dr);
1153 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1154 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1155 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1156 struct data_reference *dr;
1157 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1159 prologue_cost_vec.create (2);
1160 body_cost_vec.create (2);
1161 epilogue_cost_vec.create (2);
1163 FOR_EACH_VEC_ELT (datarefs, i, dr)
1165 stmt = DR_STMT (dr);
1166 stmt_info = vinfo_for_stmt (stmt);
1167 /* For interleaving, only the alignment of the first access
1168 matters. */
1169 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1170 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1171 continue;
1173 save_misalignment = DR_MISALIGNMENT (dr);
1174 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1175 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1176 &body_cost_vec);
1177 SET_DR_MISALIGNMENT (dr, save_misalignment);
1180 auto_vec<stmt_info_for_cost> scalar_cost_vec;
1181 vect_get_single_scalar_iteration_cost (loop_vinfo, &scalar_cost_vec);
1182 outside_cost += vect_get_known_peeling_cost
1183 (loop_vinfo, elem->npeel, &dummy,
1184 &scalar_cost_vec, &prologue_cost_vec, &epilogue_cost_vec);
1186 /* Prologue and epilogue costs are added to the target model later.
1187 These costs depend only on the scalar iteration cost, the
1188 number of peeling iterations finally chosen, and the number of
1189 misaligned statements. So discard the information found here. */
1190 prologue_cost_vec.release ();
1191 epilogue_cost_vec.release ();
1193 if (inside_cost < min->inside_cost
1194 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1196 min->inside_cost = inside_cost;
1197 min->outside_cost = outside_cost;
1198 min->body_cost_vec.release ();
1199 min->body_cost_vec = body_cost_vec;
1200 min->peel_info.dr = elem->dr;
1201 min->peel_info.npeel = elem->npeel;
1203 else
1204 body_cost_vec.release ();
1206 return 1;
1210 /* Choose best peeling option by traversing peeling hash table and either
1211 choosing an option with the lowest cost (if cost model is enabled) or the
1212 option that aligns as many accesses as possible. */
1214 static struct data_reference *
1215 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1216 unsigned int *npeel,
1217 stmt_vector_for_cost *body_cost_vec)
1219 struct _vect_peel_extended_info res;
1221 res.peel_info.dr = NULL;
1222 res.body_cost_vec = stmt_vector_for_cost ();
1224 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1226 res.inside_cost = INT_MAX;
1227 res.outside_cost = INT_MAX;
1228 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1229 ->traverse <_vect_peel_extended_info *,
1230 vect_peeling_hash_get_lowest_cost> (&res);
1232 else
1234 res.peel_info.count = 0;
1235 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1236 ->traverse <_vect_peel_extended_info *,
1237 vect_peeling_hash_get_most_frequent> (&res);
1240 *npeel = res.peel_info.npeel;
1241 *body_cost_vec = res.body_cost_vec;
1242 return res.peel_info.dr;
1246 /* Function vect_enhance_data_refs_alignment
1248 This pass will use loop versioning and loop peeling in order to enhance
1249 the alignment of data references in the loop.
1251 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1252 original loop is to be vectorized. Any other loops that are created by
1253 the transformations performed in this pass - are not supposed to be
1254 vectorized. This restriction will be relaxed.
1256 This pass will require a cost model to guide it whether to apply peeling
1257 or versioning or a combination of the two. For example, the scheme that
1258 intel uses when given a loop with several memory accesses, is as follows:
1259 choose one memory access ('p') which alignment you want to force by doing
1260 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1261 other accesses are not necessarily aligned, or (2) use loop versioning to
1262 generate one loop in which all accesses are aligned, and another loop in
1263 which only 'p' is necessarily aligned.
1265 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1266 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1267 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1269 Devising a cost model is the most critical aspect of this work. It will
1270 guide us on which access to peel for, whether to use loop versioning, how
1271 many versions to create, etc. The cost model will probably consist of
1272 generic considerations as well as target specific considerations (on
1273 powerpc for example, misaligned stores are more painful than misaligned
1274 loads).
1276 Here are the general steps involved in alignment enhancements:
1278 -- original loop, before alignment analysis:
1279 for (i=0; i<N; i++){
1280 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1281 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1284 -- After vect_compute_data_refs_alignment:
1285 for (i=0; i<N; i++){
1286 x = q[i]; # DR_MISALIGNMENT(q) = 3
1287 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1290 -- Possibility 1: we do loop versioning:
1291 if (p is aligned) {
1292 for (i=0; i<N; i++){ # loop 1A
1293 x = q[i]; # DR_MISALIGNMENT(q) = 3
1294 p[i] = y; # DR_MISALIGNMENT(p) = 0
1297 else {
1298 for (i=0; i<N; i++){ # loop 1B
1299 x = q[i]; # DR_MISALIGNMENT(q) = 3
1300 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1304 -- Possibility 2: we do loop peeling:
1305 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1306 x = q[i];
1307 p[i] = y;
1309 for (i = 3; i < N; i++){ # loop 2A
1310 x = q[i]; # DR_MISALIGNMENT(q) = 0
1311 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1314 -- Possibility 3: combination of loop peeling and versioning:
1315 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1316 x = q[i];
1317 p[i] = y;
1319 if (p is aligned) {
1320 for (i = 3; i<N; i++){ # loop 3A
1321 x = q[i]; # DR_MISALIGNMENT(q) = 0
1322 p[i] = y; # DR_MISALIGNMENT(p) = 0
1325 else {
1326 for (i = 3; i<N; i++){ # loop 3B
1327 x = q[i]; # DR_MISALIGNMENT(q) = 0
1328 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1332 These loops are later passed to loop_transform to be vectorized. The
1333 vectorizer will use the alignment information to guide the transformation
1334 (whether to generate regular loads/stores, or with special handling for
1335 misalignment). */
1337 bool
1338 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1340 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1341 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1342 enum dr_alignment_support supportable_dr_alignment;
1343 struct data_reference *dr0 = NULL, *first_store = NULL;
1344 struct data_reference *dr;
1345 unsigned int i, j;
1346 bool do_peeling = false;
1347 bool do_versioning = false;
1348 bool stat;
1349 gimple stmt;
1350 stmt_vec_info stmt_info;
1351 unsigned int npeel = 0;
1352 bool all_misalignments_unknown = true;
1353 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1354 unsigned possible_npeel_number = 1;
1355 tree vectype;
1356 unsigned int nelements, mis, same_align_drs_max = 0;
1357 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1359 if (dump_enabled_p ())
1360 dump_printf_loc (MSG_NOTE, vect_location,
1361 "=== vect_enhance_data_refs_alignment ===\n");
1363 /* While cost model enhancements are expected in the future, the high level
1364 view of the code at this time is as follows:
1366 A) If there is a misaligned access then see if peeling to align
1367 this access can make all data references satisfy
1368 vect_supportable_dr_alignment. If so, update data structures
1369 as needed and return true.
1371 B) If peeling wasn't possible and there is a data reference with an
1372 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1373 then see if loop versioning checks can be used to make all data
1374 references satisfy vect_supportable_dr_alignment. If so, update
1375 data structures as needed and return true.
1377 C) If neither peeling nor versioning were successful then return false if
1378 any data reference does not satisfy vect_supportable_dr_alignment.
1380 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1382 Note, Possibility 3 above (which is peeling and versioning together) is not
1383 being done at this time. */
1385 /* (1) Peeling to force alignment. */
1387 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1388 Considerations:
1389 + How many accesses will become aligned due to the peeling
1390 - How many accesses will become unaligned due to the peeling,
1391 and the cost of misaligned accesses.
1392 - The cost of peeling (the extra runtime checks, the increase
1393 in code size). */
1395 FOR_EACH_VEC_ELT (datarefs, i, dr)
1397 stmt = DR_STMT (dr);
1398 stmt_info = vinfo_for_stmt (stmt);
1400 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1401 continue;
1403 /* For interleaving, only the alignment of the first access
1404 matters. */
1405 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1406 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1407 continue;
1409 /* For invariant accesses there is nothing to enhance. */
1410 if (integer_zerop (DR_STEP (dr)))
1411 continue;
1413 /* Strided accesses perform only component accesses, alignment is
1414 irrelevant for them. */
1415 if (STMT_VINFO_STRIDED_P (stmt_info)
1416 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1417 continue;
1419 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1420 do_peeling = vector_alignment_reachable_p (dr);
1421 if (do_peeling)
1423 if (known_alignment_for_access_p (dr))
1425 unsigned int npeel_tmp;
1426 bool negative = tree_int_cst_compare (DR_STEP (dr),
1427 size_zero_node) < 0;
1429 /* Save info about DR in the hash table. */
1430 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo))
1431 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1432 = new hash_table<peel_info_hasher> (1);
1434 vectype = STMT_VINFO_VECTYPE (stmt_info);
1435 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1436 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1437 TREE_TYPE (DR_REF (dr))));
1438 npeel_tmp = (negative
1439 ? (mis - nelements) : (nelements - mis))
1440 & (nelements - 1);
1442 /* For multiple types, it is possible that the bigger type access
1443 will have more than one peeling option. E.g., a loop with two
1444 types: one of size (vector size / 4), and the other one of
1445 size (vector size / 8). Vectorization factor will 8. If both
1446 access are misaligned by 3, the first one needs one scalar
1447 iteration to be aligned, and the second one needs 5. But the
1448 the first one will be aligned also by peeling 5 scalar
1449 iterations, and in that case both accesses will be aligned.
1450 Hence, except for the immediate peeling amount, we also want
1451 to try to add full vector size, while we don't exceed
1452 vectorization factor.
1453 We do this automtically for cost model, since we calculate cost
1454 for every peeling option. */
1455 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1456 possible_npeel_number = vf /nelements;
1458 /* Handle the aligned case. We may decide to align some other
1459 access, making DR unaligned. */
1460 if (DR_MISALIGNMENT (dr) == 0)
1462 npeel_tmp = 0;
1463 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1464 possible_npeel_number++;
1467 for (j = 0; j < possible_npeel_number; j++)
1469 gcc_assert (npeel_tmp <= vf);
1470 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1471 npeel_tmp += nelements;
1474 all_misalignments_unknown = false;
1475 /* Data-ref that was chosen for the case that all the
1476 misalignments are unknown is not relevant anymore, since we
1477 have a data-ref with known alignment. */
1478 dr0 = NULL;
1480 else
1482 /* If we don't know any misalignment values, we prefer
1483 peeling for data-ref that has the maximum number of data-refs
1484 with the same alignment, unless the target prefers to align
1485 stores over load. */
1486 if (all_misalignments_unknown)
1488 unsigned same_align_drs
1489 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1490 if (!dr0
1491 || same_align_drs_max < same_align_drs)
1493 same_align_drs_max = same_align_drs;
1494 dr0 = dr;
1496 /* For data-refs with the same number of related
1497 accesses prefer the one where the misalign
1498 computation will be invariant in the outermost loop. */
1499 else if (same_align_drs_max == same_align_drs)
1501 struct loop *ivloop0, *ivloop;
1502 ivloop0 = outermost_invariant_loop_for_expr
1503 (loop, DR_BASE_ADDRESS (dr0));
1504 ivloop = outermost_invariant_loop_for_expr
1505 (loop, DR_BASE_ADDRESS (dr));
1506 if ((ivloop && !ivloop0)
1507 || (ivloop && ivloop0
1508 && flow_loop_nested_p (ivloop, ivloop0)))
1509 dr0 = dr;
1512 if (!first_store && DR_IS_WRITE (dr))
1513 first_store = dr;
1516 /* If there are both known and unknown misaligned accesses in the
1517 loop, we choose peeling amount according to the known
1518 accesses. */
1519 if (!supportable_dr_alignment)
1521 dr0 = dr;
1522 if (!first_store && DR_IS_WRITE (dr))
1523 first_store = dr;
1527 else
1529 if (!aligned_access_p (dr))
1531 if (dump_enabled_p ())
1532 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1533 "vector alignment may not be reachable\n");
1534 break;
1539 /* Check if we can possibly peel the loop. */
1540 if (!vect_can_advance_ivs_p (loop_vinfo)
1541 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1542 do_peeling = false;
1544 if (do_peeling
1545 && all_misalignments_unknown
1546 && vect_supportable_dr_alignment (dr0, false))
1548 /* Check if the target requires to prefer stores over loads, i.e., if
1549 misaligned stores are more expensive than misaligned loads (taking
1550 drs with same alignment into account). */
1551 if (first_store && DR_IS_READ (dr0))
1553 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1554 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1555 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1556 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1557 stmt_vector_for_cost dummy;
1558 dummy.create (2);
1560 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1561 &dummy);
1562 vect_get_data_access_cost (first_store, &store_inside_cost,
1563 &store_outside_cost, &dummy);
1565 dummy.release ();
1567 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1568 aligning the load DR0). */
1569 load_inside_penalty = store_inside_cost;
1570 load_outside_penalty = store_outside_cost;
1571 for (i = 0;
1572 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1573 DR_STMT (first_store))).iterate (i, &dr);
1574 i++)
1575 if (DR_IS_READ (dr))
1577 load_inside_penalty += load_inside_cost;
1578 load_outside_penalty += load_outside_cost;
1580 else
1582 load_inside_penalty += store_inside_cost;
1583 load_outside_penalty += store_outside_cost;
1586 /* Calculate the penalty for leaving DR0 unaligned (by
1587 aligning the FIRST_STORE). */
1588 store_inside_penalty = load_inside_cost;
1589 store_outside_penalty = load_outside_cost;
1590 for (i = 0;
1591 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1592 DR_STMT (dr0))).iterate (i, &dr);
1593 i++)
1594 if (DR_IS_READ (dr))
1596 store_inside_penalty += load_inside_cost;
1597 store_outside_penalty += load_outside_cost;
1599 else
1601 store_inside_penalty += store_inside_cost;
1602 store_outside_penalty += store_outside_cost;
1605 if (load_inside_penalty > store_inside_penalty
1606 || (load_inside_penalty == store_inside_penalty
1607 && load_outside_penalty > store_outside_penalty))
1608 dr0 = first_store;
1611 /* In case there are only loads with different unknown misalignments, use
1612 peeling only if it may help to align other accesses in the loop or
1613 if it may help improving load bandwith when we'd end up using
1614 unaligned loads. */
1615 tree dr0_vt = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr0)));
1616 if (!first_store
1617 && !STMT_VINFO_SAME_ALIGN_REFS (
1618 vinfo_for_stmt (DR_STMT (dr0))).length ()
1619 && (vect_supportable_dr_alignment (dr0, false)
1620 != dr_unaligned_supported
1621 || (builtin_vectorization_cost (vector_load, dr0_vt, 0)
1622 == builtin_vectorization_cost (unaligned_load, dr0_vt, -1))))
1623 do_peeling = false;
1626 if (do_peeling && !dr0)
1628 /* Peeling is possible, but there is no data access that is not supported
1629 unless aligned. So we try to choose the best possible peeling. */
1631 /* We should get here only if there are drs with known misalignment. */
1632 gcc_assert (!all_misalignments_unknown);
1634 /* Choose the best peeling from the hash table. */
1635 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1636 &body_cost_vec);
1637 if (!dr0 || !npeel)
1638 do_peeling = false;
1641 if (do_peeling)
1643 stmt = DR_STMT (dr0);
1644 stmt_info = vinfo_for_stmt (stmt);
1645 vectype = STMT_VINFO_VECTYPE (stmt_info);
1646 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1648 if (known_alignment_for_access_p (dr0))
1650 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1651 size_zero_node) < 0;
1652 if (!npeel)
1654 /* Since it's known at compile time, compute the number of
1655 iterations in the peeled loop (the peeling factor) for use in
1656 updating DR_MISALIGNMENT values. The peeling factor is the
1657 vectorization factor minus the misalignment as an element
1658 count. */
1659 mis = DR_MISALIGNMENT (dr0);
1660 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1661 npeel = ((negative ? mis - nelements : nelements - mis)
1662 & (nelements - 1));
1665 /* For interleaved data access every iteration accesses all the
1666 members of the group, therefore we divide the number of iterations
1667 by the group size. */
1668 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1669 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1670 npeel /= GROUP_SIZE (stmt_info);
1672 if (dump_enabled_p ())
1673 dump_printf_loc (MSG_NOTE, vect_location,
1674 "Try peeling by %d\n", npeel);
1677 /* Ensure that all data refs can be vectorized after the peel. */
1678 FOR_EACH_VEC_ELT (datarefs, i, dr)
1680 int save_misalignment;
1682 if (dr == dr0)
1683 continue;
1685 stmt = DR_STMT (dr);
1686 stmt_info = vinfo_for_stmt (stmt);
1687 /* For interleaving, only the alignment of the first access
1688 matters. */
1689 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1690 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1691 continue;
1693 /* Strided accesses perform only component accesses, alignment is
1694 irrelevant for them. */
1695 if (STMT_VINFO_STRIDED_P (stmt_info)
1696 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1697 continue;
1699 save_misalignment = DR_MISALIGNMENT (dr);
1700 vect_update_misalignment_for_peel (dr, dr0, npeel);
1701 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1702 SET_DR_MISALIGNMENT (dr, save_misalignment);
1704 if (!supportable_dr_alignment)
1706 do_peeling = false;
1707 break;
1711 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1713 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1714 if (!stat)
1715 do_peeling = false;
1716 else
1718 body_cost_vec.release ();
1719 return stat;
1723 /* Cost model #1 - honor --param vect-max-peeling-for-alignment. */
1724 if (do_peeling)
1726 unsigned max_allowed_peel
1727 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1728 if (max_allowed_peel != (unsigned)-1)
1730 unsigned max_peel = npeel;
1731 if (max_peel == 0)
1733 gimple dr_stmt = DR_STMT (dr0);
1734 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1735 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1736 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1738 if (max_peel > max_allowed_peel)
1740 do_peeling = false;
1741 if (dump_enabled_p ())
1742 dump_printf_loc (MSG_NOTE, vect_location,
1743 "Disable peeling, max peels reached: %d\n", max_peel);
1748 /* Cost model #2 - if peeling may result in a remaining loop not
1749 iterating enough to be vectorized then do not peel. */
1750 if (do_peeling
1751 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
1753 unsigned max_peel
1754 = npeel == 0 ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1 : npeel;
1755 if (LOOP_VINFO_INT_NITERS (loop_vinfo)
1756 < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + max_peel)
1757 do_peeling = false;
1760 if (do_peeling)
1762 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1763 If the misalignment of DR_i is identical to that of dr0 then set
1764 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1765 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1766 by the peeling factor times the element size of DR_i (MOD the
1767 vectorization factor times the size). Otherwise, the
1768 misalignment of DR_i must be set to unknown. */
1769 FOR_EACH_VEC_ELT (datarefs, i, dr)
1770 if (dr != dr0)
1771 vect_update_misalignment_for_peel (dr, dr0, npeel);
1773 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1774 if (npeel)
1775 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1776 else
1777 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1778 = DR_MISALIGNMENT (dr0);
1779 SET_DR_MISALIGNMENT (dr0, 0);
1780 if (dump_enabled_p ())
1782 dump_printf_loc (MSG_NOTE, vect_location,
1783 "Alignment of access forced using peeling.\n");
1784 dump_printf_loc (MSG_NOTE, vect_location,
1785 "Peeling for alignment will be applied.\n");
1787 /* The inside-loop cost will be accounted for in vectorizable_load
1788 and vectorizable_store correctly with adjusted alignments.
1789 Drop the body_cst_vec on the floor here. */
1790 body_cost_vec.release ();
1792 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1793 gcc_assert (stat);
1794 return stat;
1798 body_cost_vec.release ();
1800 /* (2) Versioning to force alignment. */
1802 /* Try versioning if:
1803 1) optimize loop for speed
1804 2) there is at least one unsupported misaligned data ref with an unknown
1805 misalignment, and
1806 3) all misaligned data refs with a known misalignment are supported, and
1807 4) the number of runtime alignment checks is within reason. */
1809 do_versioning =
1810 optimize_loop_nest_for_speed_p (loop)
1811 && (!loop->inner); /* FORNOW */
1813 if (do_versioning)
1815 FOR_EACH_VEC_ELT (datarefs, i, dr)
1817 stmt = DR_STMT (dr);
1818 stmt_info = vinfo_for_stmt (stmt);
1820 /* For interleaving, only the alignment of the first access
1821 matters. */
1822 if (aligned_access_p (dr)
1823 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1824 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1825 continue;
1827 if (STMT_VINFO_STRIDED_P (stmt_info))
1829 /* Strided loads perform only component accesses, alignment is
1830 irrelevant for them. */
1831 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1832 continue;
1833 do_versioning = false;
1834 break;
1837 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1839 if (!supportable_dr_alignment)
1841 gimple stmt;
1842 int mask;
1843 tree vectype;
1845 if (known_alignment_for_access_p (dr)
1846 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1847 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1849 do_versioning = false;
1850 break;
1853 stmt = DR_STMT (dr);
1854 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1855 gcc_assert (vectype);
1857 /* The rightmost bits of an aligned address must be zeros.
1858 Construct the mask needed for this test. For example,
1859 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1860 mask must be 15 = 0xf. */
1861 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1863 /* FORNOW: use the same mask to test all potentially unaligned
1864 references in the loop. The vectorizer currently supports
1865 a single vector size, see the reference to
1866 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1867 vectorization factor is computed. */
1868 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1869 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1870 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1871 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1872 DR_STMT (dr));
1876 /* Versioning requires at least one misaligned data reference. */
1877 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1878 do_versioning = false;
1879 else if (!do_versioning)
1880 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1883 if (do_versioning)
1885 vec<gimple> may_misalign_stmts
1886 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1887 gimple stmt;
1889 /* It can now be assumed that the data references in the statements
1890 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1891 of the loop being vectorized. */
1892 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1894 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1895 dr = STMT_VINFO_DATA_REF (stmt_info);
1896 SET_DR_MISALIGNMENT (dr, 0);
1897 if (dump_enabled_p ())
1898 dump_printf_loc (MSG_NOTE, vect_location,
1899 "Alignment of access forced using versioning.\n");
1902 if (dump_enabled_p ())
1903 dump_printf_loc (MSG_NOTE, vect_location,
1904 "Versioning for alignment will be applied.\n");
1906 /* Peeling and versioning can't be done together at this time. */
1907 gcc_assert (! (do_peeling && do_versioning));
1909 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1910 gcc_assert (stat);
1911 return stat;
1914 /* This point is reached if neither peeling nor versioning is being done. */
1915 gcc_assert (! (do_peeling || do_versioning));
1917 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1918 return stat;
1922 /* Function vect_find_same_alignment_drs.
1924 Update group and alignment relations according to the chosen
1925 vectorization factor. */
1927 static void
1928 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1929 loop_vec_info loop_vinfo)
1931 unsigned int i;
1932 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1933 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1934 struct data_reference *dra = DDR_A (ddr);
1935 struct data_reference *drb = DDR_B (ddr);
1936 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1937 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1938 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1939 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1940 lambda_vector dist_v;
1941 unsigned int loop_depth;
1943 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1944 return;
1946 if (dra == drb)
1947 return;
1949 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1950 return;
1952 /* Loop-based vectorization and known data dependence. */
1953 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1954 return;
1956 /* Data-dependence analysis reports a distance vector of zero
1957 for data-references that overlap only in the first iteration
1958 but have different sign step (see PR45764).
1959 So as a sanity check require equal DR_STEP. */
1960 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1961 return;
1963 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1964 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1966 int dist = dist_v[loop_depth];
1968 if (dump_enabled_p ())
1969 dump_printf_loc (MSG_NOTE, vect_location,
1970 "dependence distance = %d.\n", dist);
1972 /* Same loop iteration. */
1973 if (dist == 0
1974 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1976 /* Two references with distance zero have the same alignment. */
1977 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1978 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1979 if (dump_enabled_p ())
1981 dump_printf_loc (MSG_NOTE, vect_location,
1982 "accesses have the same alignment.\n");
1983 dump_printf (MSG_NOTE,
1984 "dependence distance modulo vf == 0 between ");
1985 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1986 dump_printf (MSG_NOTE, " and ");
1987 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1988 dump_printf (MSG_NOTE, "\n");
1995 /* Function vect_analyze_data_refs_alignment
1997 Analyze the alignment of the data-references in the loop.
1998 Return FALSE if a data reference is found that cannot be vectorized. */
2000 bool
2001 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
2002 bb_vec_info bb_vinfo)
2004 if (dump_enabled_p ())
2005 dump_printf_loc (MSG_NOTE, vect_location,
2006 "=== vect_analyze_data_refs_alignment ===\n");
2008 /* Mark groups of data references with same alignment using
2009 data dependence information. */
2010 if (loop_vinfo)
2012 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
2013 struct data_dependence_relation *ddr;
2014 unsigned int i;
2016 FOR_EACH_VEC_ELT (ddrs, i, ddr)
2017 vect_find_same_alignment_drs (ddr, loop_vinfo);
2020 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
2022 if (dump_enabled_p ())
2023 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2024 "not vectorized: can't calculate alignment "
2025 "for data ref.\n");
2026 return false;
2029 return true;
2033 /* Analyze groups of accesses: check that DR belongs to a group of
2034 accesses of legal size, step, etc. Detect gaps, single element
2035 interleaving, and other special cases. Set grouped access info.
2036 Collect groups of strided stores for further use in SLP analysis. */
2038 static bool
2039 vect_analyze_group_access (struct data_reference *dr)
2041 tree step = DR_STEP (dr);
2042 tree scalar_type = TREE_TYPE (DR_REF (dr));
2043 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2044 gimple stmt = DR_STMT (dr);
2045 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2046 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2047 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2048 HOST_WIDE_INT dr_step = -1;
2049 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2050 bool slp_impossible = false;
2051 struct loop *loop = NULL;
2053 if (loop_vinfo)
2054 loop = LOOP_VINFO_LOOP (loop_vinfo);
2056 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2057 size of the interleaving group (including gaps). */
2058 if (tree_fits_shwi_p (step))
2060 dr_step = tree_to_shwi (step);
2061 groupsize = absu_hwi (dr_step) / type_size;
2063 else
2064 groupsize = 0;
2066 /* Not consecutive access is possible only if it is a part of interleaving. */
2067 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2069 /* Check if it this DR is a part of interleaving, and is a single
2070 element of the group that is accessed in the loop. */
2072 /* Gaps are supported only for loads. STEP must be a multiple of the type
2073 size. The size of the group must be a power of 2. */
2074 if (DR_IS_READ (dr)
2075 && (dr_step % type_size) == 0
2076 && groupsize > 0
2077 && exact_log2 (groupsize) != -1)
2079 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2080 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2081 if (dump_enabled_p ())
2083 dump_printf_loc (MSG_NOTE, vect_location,
2084 "Detected single element interleaving ");
2085 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2086 dump_printf (MSG_NOTE, " step ");
2087 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2088 dump_printf (MSG_NOTE, "\n");
2091 if (loop_vinfo)
2093 if (dump_enabled_p ())
2094 dump_printf_loc (MSG_NOTE, vect_location,
2095 "Data access with gaps requires scalar "
2096 "epilogue loop\n");
2097 if (loop->inner)
2099 if (dump_enabled_p ())
2100 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2101 "Peeling for outer loop is not"
2102 " supported\n");
2103 return false;
2106 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2109 return true;
2112 if (dump_enabled_p ())
2114 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2115 "not consecutive access ");
2116 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2117 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2120 if (bb_vinfo)
2122 /* Mark the statement as unvectorizable. */
2123 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2124 return true;
2127 return false;
2130 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2132 /* First stmt in the interleaving chain. Check the chain. */
2133 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2134 struct data_reference *data_ref = dr;
2135 unsigned int count = 1;
2136 tree prev_init = DR_INIT (data_ref);
2137 gimple prev = stmt;
2138 HOST_WIDE_INT diff, gaps = 0;
2140 while (next)
2142 /* Skip same data-refs. In case that two or more stmts share
2143 data-ref (supported only for loads), we vectorize only the first
2144 stmt, and the rest get their vectorized loads from the first
2145 one. */
2146 if (!tree_int_cst_compare (DR_INIT (data_ref),
2147 DR_INIT (STMT_VINFO_DATA_REF (
2148 vinfo_for_stmt (next)))))
2150 if (DR_IS_WRITE (data_ref))
2152 if (dump_enabled_p ())
2153 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2154 "Two store stmts share the same dr.\n");
2155 return false;
2158 /* For load use the same data-ref load. */
2159 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2161 prev = next;
2162 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2163 continue;
2166 prev = next;
2167 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2169 /* All group members have the same STEP by construction. */
2170 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2172 /* Check that the distance between two accesses is equal to the type
2173 size. Otherwise, we have gaps. */
2174 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2175 - TREE_INT_CST_LOW (prev_init)) / type_size;
2176 if (diff != 1)
2178 /* FORNOW: SLP of accesses with gaps is not supported. */
2179 slp_impossible = true;
2180 if (DR_IS_WRITE (data_ref))
2182 if (dump_enabled_p ())
2183 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2184 "interleaved store with gaps\n");
2185 return false;
2188 gaps += diff - 1;
2191 last_accessed_element += diff;
2193 /* Store the gap from the previous member of the group. If there is no
2194 gap in the access, GROUP_GAP is always 1. */
2195 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2197 prev_init = DR_INIT (data_ref);
2198 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2199 /* Count the number of data-refs in the chain. */
2200 count++;
2203 if (groupsize == 0)
2204 groupsize = count + gaps;
2206 /* Check that the size of the interleaving is equal to count for stores,
2207 i.e., that there are no gaps. */
2208 if (groupsize != count)
2210 if (DR_IS_READ (dr))
2212 slp_impossible = true;
2213 /* There is a gap after the last load in the group. This gap is a
2214 difference between the groupsize and the number of elements.
2215 When there is no gap, this difference should be 0. */
2216 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2218 else
2220 if (dump_enabled_p ())
2221 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2222 "interleaved store with gaps\n");
2223 return false;
2227 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2228 if (dump_enabled_p ())
2229 dump_printf_loc (MSG_NOTE, vect_location,
2230 "Detected interleaving of size %d\n", (int)groupsize);
2232 /* SLP: create an SLP data structure for every interleaving group of
2233 stores for further analysis in vect_analyse_slp. */
2234 if (DR_IS_WRITE (dr) && !slp_impossible)
2236 if (loop_vinfo)
2237 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2238 if (bb_vinfo)
2239 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2242 /* There is a gap in the end of the group. */
2243 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2245 if (dump_enabled_p ())
2246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2247 "Data access with gaps requires scalar "
2248 "epilogue loop\n");
2249 if (loop->inner)
2251 if (dump_enabled_p ())
2252 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2253 "Peeling for outer loop is not supported\n");
2254 return false;
2257 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2261 return true;
2265 /* Analyze the access pattern of the data-reference DR.
2266 In case of non-consecutive accesses call vect_analyze_group_access() to
2267 analyze groups of accesses. */
2269 static bool
2270 vect_analyze_data_ref_access (struct data_reference *dr)
2272 tree step = DR_STEP (dr);
2273 tree scalar_type = TREE_TYPE (DR_REF (dr));
2274 gimple stmt = DR_STMT (dr);
2275 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2276 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2277 struct loop *loop = NULL;
2279 if (loop_vinfo)
2280 loop = LOOP_VINFO_LOOP (loop_vinfo);
2282 if (loop_vinfo && !step)
2284 if (dump_enabled_p ())
2285 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2286 "bad data-ref access in loop\n");
2287 return false;
2290 /* Allow loads with zero step in inner-loop vectorization. */
2291 if (loop_vinfo && integer_zerop (step))
2293 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2294 if (!nested_in_vect_loop_p (loop, stmt))
2295 return DR_IS_READ (dr);
2296 /* Allow references with zero step for outer loops marked
2297 with pragma omp simd only - it guarantees absence of
2298 loop-carried dependencies between inner loop iterations. */
2299 if (!loop->force_vectorize)
2301 if (dump_enabled_p ())
2302 dump_printf_loc (MSG_NOTE, vect_location,
2303 "zero step in inner loop of nest\n");
2304 return false;
2308 if (loop && nested_in_vect_loop_p (loop, stmt))
2310 /* Interleaved accesses are not yet supported within outer-loop
2311 vectorization for references in the inner-loop. */
2312 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2314 /* For the rest of the analysis we use the outer-loop step. */
2315 step = STMT_VINFO_DR_STEP (stmt_info);
2316 if (integer_zerop (step))
2318 if (dump_enabled_p ())
2319 dump_printf_loc (MSG_NOTE, vect_location,
2320 "zero step in outer loop.\n");
2321 if (DR_IS_READ (dr))
2322 return true;
2323 else
2324 return false;
2328 /* Consecutive? */
2329 if (TREE_CODE (step) == INTEGER_CST)
2331 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2332 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2333 || (dr_step < 0
2334 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2336 /* Mark that it is not interleaving. */
2337 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2338 return true;
2342 if (loop && nested_in_vect_loop_p (loop, stmt))
2344 if (dump_enabled_p ())
2345 dump_printf_loc (MSG_NOTE, vect_location,
2346 "grouped access in outer loop.\n");
2347 return false;
2351 /* Assume this is a DR handled by non-constant strided load case. */
2352 if (TREE_CODE (step) != INTEGER_CST)
2353 return (STMT_VINFO_STRIDED_P (stmt_info)
2354 && (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
2355 || vect_analyze_group_access (dr)));
2357 /* Not consecutive access - check if it's a part of interleaving group. */
2358 return vect_analyze_group_access (dr);
2363 /* A helper function used in the comparator function to sort data
2364 references. T1 and T2 are two data references to be compared.
2365 The function returns -1, 0, or 1. */
2367 static int
2368 compare_tree (tree t1, tree t2)
2370 int i, cmp;
2371 enum tree_code code;
2372 char tclass;
2374 if (t1 == t2)
2375 return 0;
2376 if (t1 == NULL)
2377 return -1;
2378 if (t2 == NULL)
2379 return 1;
2382 if (TREE_CODE (t1) != TREE_CODE (t2))
2383 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2385 code = TREE_CODE (t1);
2386 switch (code)
2388 /* For const values, we can just use hash values for comparisons. */
2389 case INTEGER_CST:
2390 case REAL_CST:
2391 case FIXED_CST:
2392 case STRING_CST:
2393 case COMPLEX_CST:
2394 case VECTOR_CST:
2396 hashval_t h1 = iterative_hash_expr (t1, 0);
2397 hashval_t h2 = iterative_hash_expr (t2, 0);
2398 if (h1 != h2)
2399 return h1 < h2 ? -1 : 1;
2400 break;
2403 case SSA_NAME:
2404 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2405 if (cmp != 0)
2406 return cmp;
2408 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2409 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2410 break;
2412 default:
2413 tclass = TREE_CODE_CLASS (code);
2415 /* For var-decl, we could compare their UIDs. */
2416 if (tclass == tcc_declaration)
2418 if (DECL_UID (t1) != DECL_UID (t2))
2419 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2420 break;
2423 /* For expressions with operands, compare their operands recursively. */
2424 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2426 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2427 if (cmp != 0)
2428 return cmp;
2432 return 0;
2436 /* Compare two data-references DRA and DRB to group them into chunks
2437 suitable for grouping. */
2439 static int
2440 dr_group_sort_cmp (const void *dra_, const void *drb_)
2442 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2443 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2444 int cmp;
2446 /* Stabilize sort. */
2447 if (dra == drb)
2448 return 0;
2450 /* Ordering of DRs according to base. */
2451 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2453 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2454 if (cmp != 0)
2455 return cmp;
2458 /* And according to DR_OFFSET. */
2459 if (!dr_equal_offsets_p (dra, drb))
2461 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2462 if (cmp != 0)
2463 return cmp;
2466 /* Put reads before writes. */
2467 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2468 return DR_IS_READ (dra) ? -1 : 1;
2470 /* Then sort after access size. */
2471 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2472 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2474 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2475 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2476 if (cmp != 0)
2477 return cmp;
2480 /* And after step. */
2481 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2483 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2484 if (cmp != 0)
2485 return cmp;
2488 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2489 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2490 if (cmp == 0)
2491 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2492 return cmp;
2495 /* Function vect_analyze_data_ref_accesses.
2497 Analyze the access pattern of all the data references in the loop.
2499 FORNOW: the only access pattern that is considered vectorizable is a
2500 simple step 1 (consecutive) access.
2502 FORNOW: handle only arrays and pointer accesses. */
2504 bool
2505 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2507 unsigned int i;
2508 vec<data_reference_p> datarefs;
2509 struct data_reference *dr;
2511 if (dump_enabled_p ())
2512 dump_printf_loc (MSG_NOTE, vect_location,
2513 "=== vect_analyze_data_ref_accesses ===\n");
2515 if (loop_vinfo)
2516 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2517 else
2518 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2520 if (datarefs.is_empty ())
2521 return true;
2523 /* Sort the array of datarefs to make building the interleaving chains
2524 linear. Don't modify the original vector's order, it is needed for
2525 determining what dependencies are reversed. */
2526 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2527 datarefs_copy.qsort (dr_group_sort_cmp);
2529 /* Build the interleaving chains. */
2530 for (i = 0; i < datarefs_copy.length () - 1;)
2532 data_reference_p dra = datarefs_copy[i];
2533 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2534 stmt_vec_info lastinfo = NULL;
2535 for (i = i + 1; i < datarefs_copy.length (); ++i)
2537 data_reference_p drb = datarefs_copy[i];
2538 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2540 /* ??? Imperfect sorting (non-compatible types, non-modulo
2541 accesses, same accesses) can lead to a group to be artificially
2542 split here as we don't just skip over those. If it really
2543 matters we can push those to a worklist and re-iterate
2544 over them. The we can just skip ahead to the next DR here. */
2546 /* Check that the data-refs have same first location (except init)
2547 and they are both either store or load (not load and store,
2548 not masked loads or stores). */
2549 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2550 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2551 DR_BASE_ADDRESS (drb), 0)
2552 || !dr_equal_offsets_p (dra, drb)
2553 || !gimple_assign_single_p (DR_STMT (dra))
2554 || !gimple_assign_single_p (DR_STMT (drb)))
2555 break;
2557 /* Check that the data-refs have the same constant size. */
2558 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2559 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2560 if (!tree_fits_uhwi_p (sza)
2561 || !tree_fits_uhwi_p (szb)
2562 || !tree_int_cst_equal (sza, szb))
2563 break;
2565 /* Check that the data-refs have the same step. */
2566 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2567 break;
2569 /* Do not place the same access in the interleaving chain twice. */
2570 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2571 break;
2573 /* Check the types are compatible.
2574 ??? We don't distinguish this during sorting. */
2575 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2576 TREE_TYPE (DR_REF (drb))))
2577 break;
2579 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2580 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2581 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2582 gcc_assert (init_a < init_b);
2584 /* If init_b == init_a + the size of the type * k, we have an
2585 interleaving, and DRA is accessed before DRB. */
2586 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2587 if ((init_b - init_a) % type_size_a != 0)
2588 break;
2590 /* If we have a store, the accesses are adjacent. This splits
2591 groups into chunks we support (we don't support vectorization
2592 of stores with gaps). */
2593 if (!DR_IS_READ (dra)
2594 && (init_b - (HOST_WIDE_INT) TREE_INT_CST_LOW
2595 (DR_INIT (datarefs_copy[i-1]))
2596 != type_size_a))
2597 break;
2599 /* If the step (if not zero or non-constant) is greater than the
2600 difference between data-refs' inits this splits groups into
2601 suitable sizes. */
2602 if (tree_fits_shwi_p (DR_STEP (dra)))
2604 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2605 if (step != 0 && step <= (init_b - init_a))
2606 break;
2609 if (dump_enabled_p ())
2611 dump_printf_loc (MSG_NOTE, vect_location,
2612 "Detected interleaving ");
2613 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2614 dump_printf (MSG_NOTE, " and ");
2615 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2616 dump_printf (MSG_NOTE, "\n");
2619 /* Link the found element into the group list. */
2620 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2622 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2623 lastinfo = stmtinfo_a;
2625 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2626 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2627 lastinfo = stmtinfo_b;
2631 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2632 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2633 && !vect_analyze_data_ref_access (dr))
2635 if (dump_enabled_p ())
2636 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2637 "not vectorized: complicated access pattern.\n");
2639 if (bb_vinfo)
2641 /* Mark the statement as not vectorizable. */
2642 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2643 continue;
2645 else
2647 datarefs_copy.release ();
2648 return false;
2652 datarefs_copy.release ();
2653 return true;
2657 /* Operator == between two dr_with_seg_len objects.
2659 This equality operator is used to make sure two data refs
2660 are the same one so that we will consider to combine the
2661 aliasing checks of those two pairs of data dependent data
2662 refs. */
2664 static bool
2665 operator == (const dr_with_seg_len& d1,
2666 const dr_with_seg_len& d2)
2668 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2669 DR_BASE_ADDRESS (d2.dr), 0)
2670 && compare_tree (d1.offset, d2.offset) == 0
2671 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2674 /* Function comp_dr_with_seg_len_pair.
2676 Comparison function for sorting objects of dr_with_seg_len_pair_t
2677 so that we can combine aliasing checks in one scan. */
2679 static int
2680 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2682 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2683 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2685 const dr_with_seg_len &p11 = p1->first,
2686 &p12 = p1->second,
2687 &p21 = p2->first,
2688 &p22 = p2->second;
2690 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2691 if a and c have the same basic address snd step, and b and d have the same
2692 address and step. Therefore, if any a&c or b&d don't have the same address
2693 and step, we don't care the order of those two pairs after sorting. */
2694 int comp_res;
2696 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2697 DR_BASE_ADDRESS (p21.dr))) != 0)
2698 return comp_res;
2699 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2700 DR_BASE_ADDRESS (p22.dr))) != 0)
2701 return comp_res;
2702 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2703 return comp_res;
2704 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2705 return comp_res;
2706 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2707 return comp_res;
2708 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2709 return comp_res;
2711 return 0;
2714 /* Function vect_vfa_segment_size.
2716 Create an expression that computes the size of segment
2717 that will be accessed for a data reference. The functions takes into
2718 account that realignment loads may access one more vector.
2720 Input:
2721 DR: The data reference.
2722 LENGTH_FACTOR: segment length to consider.
2724 Return an expression whose value is the size of segment which will be
2725 accessed by DR. */
2727 static tree
2728 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2730 tree segment_length;
2732 if (integer_zerop (DR_STEP (dr)))
2733 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2734 else
2735 segment_length = size_binop (MULT_EXPR,
2736 fold_convert (sizetype, DR_STEP (dr)),
2737 fold_convert (sizetype, length_factor));
2739 if (vect_supportable_dr_alignment (dr, false)
2740 == dr_explicit_realign_optimized)
2742 tree vector_size = TYPE_SIZE_UNIT
2743 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2745 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2747 return segment_length;
2750 /* Function vect_prune_runtime_alias_test_list.
2752 Prune a list of ddrs to be tested at run-time by versioning for alias.
2753 Merge several alias checks into one if possible.
2754 Return FALSE if resulting list of ddrs is longer then allowed by
2755 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2757 bool
2758 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2760 vec<ddr_p> may_alias_ddrs =
2761 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2762 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2763 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2764 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2765 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2767 ddr_p ddr;
2768 unsigned int i;
2769 tree length_factor;
2771 if (dump_enabled_p ())
2772 dump_printf_loc (MSG_NOTE, vect_location,
2773 "=== vect_prune_runtime_alias_test_list ===\n");
2775 if (may_alias_ddrs.is_empty ())
2776 return true;
2778 /* Basically, for each pair of dependent data refs store_ptr_0
2779 and load_ptr_0, we create an expression:
2781 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2782 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2784 for aliasing checks. However, in some cases we can decrease
2785 the number of checks by combining two checks into one. For
2786 example, suppose we have another pair of data refs store_ptr_0
2787 and load_ptr_1, and if the following condition is satisfied:
2789 load_ptr_0 < load_ptr_1 &&
2790 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2792 (this condition means, in each iteration of vectorized loop,
2793 the accessed memory of store_ptr_0 cannot be between the memory
2794 of load_ptr_0 and load_ptr_1.)
2796 we then can use only the following expression to finish the
2797 alising checks between store_ptr_0 & load_ptr_0 and
2798 store_ptr_0 & load_ptr_1:
2800 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2801 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2803 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2804 same basic address. */
2806 comp_alias_ddrs.create (may_alias_ddrs.length ());
2808 /* First, we collect all data ref pairs for aliasing checks. */
2809 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2811 struct data_reference *dr_a, *dr_b;
2812 gimple dr_group_first_a, dr_group_first_b;
2813 tree segment_length_a, segment_length_b;
2814 gimple stmt_a, stmt_b;
2816 dr_a = DDR_A (ddr);
2817 stmt_a = DR_STMT (DDR_A (ddr));
2818 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2819 if (dr_group_first_a)
2821 stmt_a = dr_group_first_a;
2822 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2825 dr_b = DDR_B (ddr);
2826 stmt_b = DR_STMT (DDR_B (ddr));
2827 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2828 if (dr_group_first_b)
2830 stmt_b = dr_group_first_b;
2831 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2834 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2835 length_factor = scalar_loop_iters;
2836 else
2837 length_factor = size_int (vect_factor);
2838 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2839 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2841 dr_with_seg_len_pair_t dr_with_seg_len_pair
2842 (dr_with_seg_len (dr_a, segment_length_a),
2843 dr_with_seg_len (dr_b, segment_length_b));
2845 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2846 std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2848 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2851 /* Second, we sort the collected data ref pairs so that we can scan
2852 them once to combine all possible aliasing checks. */
2853 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2855 /* Third, we scan the sorted dr pairs and check if we can combine
2856 alias checks of two neighbouring dr pairs. */
2857 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2859 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2860 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2861 *dr_b1 = &comp_alias_ddrs[i-1].second,
2862 *dr_a2 = &comp_alias_ddrs[i].first,
2863 *dr_b2 = &comp_alias_ddrs[i].second;
2865 /* Remove duplicate data ref pairs. */
2866 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2868 if (dump_enabled_p ())
2870 dump_printf_loc (MSG_NOTE, vect_location,
2871 "found equal ranges ");
2872 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2873 DR_REF (dr_a1->dr));
2874 dump_printf (MSG_NOTE, ", ");
2875 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2876 DR_REF (dr_b1->dr));
2877 dump_printf (MSG_NOTE, " and ");
2878 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2879 DR_REF (dr_a2->dr));
2880 dump_printf (MSG_NOTE, ", ");
2881 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2882 DR_REF (dr_b2->dr));
2883 dump_printf (MSG_NOTE, "\n");
2886 comp_alias_ddrs.ordered_remove (i--);
2887 continue;
2890 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2892 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2893 and DR_A1 and DR_A2 are two consecutive memrefs. */
2894 if (*dr_a1 == *dr_a2)
2896 std::swap (dr_a1, dr_b1);
2897 std::swap (dr_a2, dr_b2);
2900 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2901 DR_BASE_ADDRESS (dr_a2->dr),
2903 || !tree_fits_shwi_p (dr_a1->offset)
2904 || !tree_fits_shwi_p (dr_a2->offset))
2905 continue;
2907 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2908 - tree_to_shwi (dr_a1->offset));
2911 /* Now we check if the following condition is satisfied:
2913 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2915 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2916 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2917 have to make a best estimation. We can get the minimum value
2918 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2919 then either of the following two conditions can guarantee the
2920 one above:
2922 1: DIFF <= MIN_SEG_LEN_B
2923 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2927 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2928 ? tree_to_shwi (dr_b1->seg_len)
2929 : vect_factor);
2931 if (diff <= min_seg_len_b
2932 || (tree_fits_shwi_p (dr_a1->seg_len)
2933 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2935 if (dump_enabled_p ())
2937 dump_printf_loc (MSG_NOTE, vect_location,
2938 "merging ranges for ");
2939 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2940 DR_REF (dr_a1->dr));
2941 dump_printf (MSG_NOTE, ", ");
2942 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2943 DR_REF (dr_b1->dr));
2944 dump_printf (MSG_NOTE, " and ");
2945 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2946 DR_REF (dr_a2->dr));
2947 dump_printf (MSG_NOTE, ", ");
2948 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2949 DR_REF (dr_b2->dr));
2950 dump_printf (MSG_NOTE, "\n");
2953 dr_a1->seg_len = size_binop (PLUS_EXPR,
2954 dr_a2->seg_len, size_int (diff));
2955 comp_alias_ddrs.ordered_remove (i--);
2960 dump_printf_loc (MSG_NOTE, vect_location,
2961 "improved number of alias checks from %d to %d\n",
2962 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2963 if ((int) comp_alias_ddrs.length () >
2964 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2965 return false;
2967 return true;
2970 /* Check whether a non-affine read in stmt is suitable for gather load
2971 and if so, return a builtin decl for that operation. */
2973 tree
2974 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2975 tree *offp, int *scalep)
2977 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2978 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2979 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2980 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2981 tree offtype = NULL_TREE;
2982 tree decl, base, off;
2983 machine_mode pmode;
2984 int punsignedp, pvolatilep;
2986 base = DR_REF (dr);
2987 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
2988 see if we can use the def stmt of the address. */
2989 if (is_gimple_call (stmt)
2990 && gimple_call_internal_p (stmt)
2991 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2992 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
2993 && TREE_CODE (base) == MEM_REF
2994 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2995 && integer_zerop (TREE_OPERAND (base, 1))
2996 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
2998 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
2999 if (is_gimple_assign (def_stmt)
3000 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3001 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3004 /* The gather builtins need address of the form
3005 loop_invariant + vector * {1, 2, 4, 8}
3007 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3008 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3009 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3010 multiplications and additions in it. To get a vector, we need
3011 a single SSA_NAME that will be defined in the loop and will
3012 contain everything that is not loop invariant and that can be
3013 vectorized. The following code attempts to find such a preexistng
3014 SSA_NAME OFF and put the loop invariants into a tree BASE
3015 that can be gimplified before the loop. */
3016 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
3017 &pmode, &punsignedp, &pvolatilep, false);
3018 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
3020 if (TREE_CODE (base) == MEM_REF)
3022 if (!integer_zerop (TREE_OPERAND (base, 1)))
3024 if (off == NULL_TREE)
3026 offset_int moff = mem_ref_offset (base);
3027 off = wide_int_to_tree (sizetype, moff);
3029 else
3030 off = size_binop (PLUS_EXPR, off,
3031 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3033 base = TREE_OPERAND (base, 0);
3035 else
3036 base = build_fold_addr_expr (base);
3038 if (off == NULL_TREE)
3039 off = size_zero_node;
3041 /* If base is not loop invariant, either off is 0, then we start with just
3042 the constant offset in the loop invariant BASE and continue with base
3043 as OFF, otherwise give up.
3044 We could handle that case by gimplifying the addition of base + off
3045 into some SSA_NAME and use that as off, but for now punt. */
3046 if (!expr_invariant_in_loop_p (loop, base))
3048 if (!integer_zerop (off))
3049 return NULL_TREE;
3050 off = base;
3051 base = size_int (pbitpos / BITS_PER_UNIT);
3053 /* Otherwise put base + constant offset into the loop invariant BASE
3054 and continue with OFF. */
3055 else
3057 base = fold_convert (sizetype, base);
3058 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3061 /* OFF at this point may be either a SSA_NAME or some tree expression
3062 from get_inner_reference. Try to peel off loop invariants from it
3063 into BASE as long as possible. */
3064 STRIP_NOPS (off);
3065 while (offtype == NULL_TREE)
3067 enum tree_code code;
3068 tree op0, op1, add = NULL_TREE;
3070 if (TREE_CODE (off) == SSA_NAME)
3072 gimple def_stmt = SSA_NAME_DEF_STMT (off);
3074 if (expr_invariant_in_loop_p (loop, off))
3075 return NULL_TREE;
3077 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3078 break;
3080 op0 = gimple_assign_rhs1 (def_stmt);
3081 code = gimple_assign_rhs_code (def_stmt);
3082 op1 = gimple_assign_rhs2 (def_stmt);
3084 else
3086 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3087 return NULL_TREE;
3088 code = TREE_CODE (off);
3089 extract_ops_from_tree (off, &code, &op0, &op1);
3091 switch (code)
3093 case POINTER_PLUS_EXPR:
3094 case PLUS_EXPR:
3095 if (expr_invariant_in_loop_p (loop, op0))
3097 add = op0;
3098 off = op1;
3099 do_add:
3100 add = fold_convert (sizetype, add);
3101 if (scale != 1)
3102 add = size_binop (MULT_EXPR, add, size_int (scale));
3103 base = size_binop (PLUS_EXPR, base, add);
3104 continue;
3106 if (expr_invariant_in_loop_p (loop, op1))
3108 add = op1;
3109 off = op0;
3110 goto do_add;
3112 break;
3113 case MINUS_EXPR:
3114 if (expr_invariant_in_loop_p (loop, op1))
3116 add = fold_convert (sizetype, op1);
3117 add = size_binop (MINUS_EXPR, size_zero_node, add);
3118 off = op0;
3119 goto do_add;
3121 break;
3122 case MULT_EXPR:
3123 if (scale == 1 && tree_fits_shwi_p (op1))
3125 scale = tree_to_shwi (op1);
3126 off = op0;
3127 continue;
3129 break;
3130 case SSA_NAME:
3131 off = op0;
3132 continue;
3133 CASE_CONVERT:
3134 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3135 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3136 break;
3137 if (TYPE_PRECISION (TREE_TYPE (op0))
3138 == TYPE_PRECISION (TREE_TYPE (off)))
3140 off = op0;
3141 continue;
3143 if (TYPE_PRECISION (TREE_TYPE (op0))
3144 < TYPE_PRECISION (TREE_TYPE (off)))
3146 off = op0;
3147 offtype = TREE_TYPE (off);
3148 STRIP_NOPS (off);
3149 continue;
3151 break;
3152 default:
3153 break;
3155 break;
3158 /* If at the end OFF still isn't a SSA_NAME or isn't
3159 defined in the loop, punt. */
3160 if (TREE_CODE (off) != SSA_NAME
3161 || expr_invariant_in_loop_p (loop, off))
3162 return NULL_TREE;
3164 if (offtype == NULL_TREE)
3165 offtype = TREE_TYPE (off);
3167 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3168 offtype, scale);
3169 if (decl == NULL_TREE)
3170 return NULL_TREE;
3172 if (basep)
3173 *basep = base;
3174 if (offp)
3175 *offp = off;
3176 if (scalep)
3177 *scalep = scale;
3178 return decl;
3181 /* Function vect_analyze_data_refs.
3183 Find all the data references in the loop or basic block.
3185 The general structure of the analysis of data refs in the vectorizer is as
3186 follows:
3187 1- vect_analyze_data_refs(loop/bb): call
3188 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3189 in the loop/bb and their dependences.
3190 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3191 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3192 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3196 bool
3197 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3198 bb_vec_info bb_vinfo,
3199 int *min_vf, unsigned *n_stmts)
3201 struct loop *loop = NULL;
3202 basic_block bb = NULL;
3203 unsigned int i;
3204 vec<data_reference_p> datarefs;
3205 struct data_reference *dr;
3206 tree scalar_type;
3208 if (dump_enabled_p ())
3209 dump_printf_loc (MSG_NOTE, vect_location,
3210 "=== vect_analyze_data_refs ===\n");
3212 if (loop_vinfo)
3214 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3216 loop = LOOP_VINFO_LOOP (loop_vinfo);
3217 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3218 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3220 if (dump_enabled_p ())
3221 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3222 "not vectorized: loop contains function calls"
3223 " or data references that cannot be analyzed\n");
3224 return false;
3227 for (i = 0; i < loop->num_nodes; i++)
3229 gimple_stmt_iterator gsi;
3231 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3233 gimple stmt = gsi_stmt (gsi);
3234 if (is_gimple_debug (stmt))
3235 continue;
3236 ++*n_stmts;
3237 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3239 if (is_gimple_call (stmt) && loop->safelen)
3241 tree fndecl = gimple_call_fndecl (stmt), op;
3242 if (fndecl != NULL_TREE)
3244 struct cgraph_node *node = cgraph_node::get (fndecl);
3245 if (node != NULL && node->simd_clones != NULL)
3247 unsigned int j, n = gimple_call_num_args (stmt);
3248 for (j = 0; j < n; j++)
3250 op = gimple_call_arg (stmt, j);
3251 if (DECL_P (op)
3252 || (REFERENCE_CLASS_P (op)
3253 && get_base_address (op)))
3254 break;
3256 op = gimple_call_lhs (stmt);
3257 /* Ignore #pragma omp declare simd functions
3258 if they don't have data references in the
3259 call stmt itself. */
3260 if (j == n
3261 && !(op
3262 && (DECL_P (op)
3263 || (REFERENCE_CLASS_P (op)
3264 && get_base_address (op)))))
3265 continue;
3269 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3270 if (dump_enabled_p ())
3271 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3272 "not vectorized: loop contains function "
3273 "calls or data references that cannot "
3274 "be analyzed\n");
3275 return false;
3280 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3282 else
3284 gimple_stmt_iterator gsi;
3286 bb = BB_VINFO_BB (bb_vinfo);
3287 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3289 gimple stmt = gsi_stmt (gsi);
3290 if (is_gimple_debug (stmt))
3291 continue;
3292 ++*n_stmts;
3293 if (!find_data_references_in_stmt (NULL, stmt,
3294 &BB_VINFO_DATAREFS (bb_vinfo)))
3296 /* Mark the rest of the basic-block as unvectorizable. */
3297 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3299 stmt = gsi_stmt (gsi);
3300 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3302 break;
3306 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3309 /* Go through the data-refs, check that the analysis succeeded. Update
3310 pointer from stmt_vec_info struct to DR and vectype. */
3312 FOR_EACH_VEC_ELT (datarefs, i, dr)
3314 gimple stmt;
3315 stmt_vec_info stmt_info;
3316 tree base, offset, init;
3317 bool gather = false;
3318 bool simd_lane_access = false;
3319 int vf;
3321 again:
3322 if (!dr || !DR_REF (dr))
3324 if (dump_enabled_p ())
3325 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3326 "not vectorized: unhandled data-ref\n");
3327 return false;
3330 stmt = DR_STMT (dr);
3331 stmt_info = vinfo_for_stmt (stmt);
3333 /* Discard clobbers from the dataref vector. We will remove
3334 clobber stmts during vectorization. */
3335 if (gimple_clobber_p (stmt))
3337 free_data_ref (dr);
3338 if (i == datarefs.length () - 1)
3340 datarefs.pop ();
3341 break;
3343 datarefs.ordered_remove (i);
3344 dr = datarefs[i];
3345 goto again;
3348 /* Check that analysis of the data-ref succeeded. */
3349 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3350 || !DR_STEP (dr))
3352 bool maybe_gather
3353 = DR_IS_READ (dr)
3354 && !TREE_THIS_VOLATILE (DR_REF (dr))
3355 && targetm.vectorize.builtin_gather != NULL;
3356 bool maybe_simd_lane_access
3357 = loop_vinfo && loop->simduid;
3359 /* If target supports vector gather loads, or if this might be
3360 a SIMD lane access, see if they can't be used. */
3361 if (loop_vinfo
3362 && (maybe_gather || maybe_simd_lane_access)
3363 && !nested_in_vect_loop_p (loop, stmt))
3365 struct data_reference *newdr
3366 = create_data_ref (NULL, loop_containing_stmt (stmt),
3367 DR_REF (dr), stmt, true);
3368 gcc_assert (newdr != NULL && DR_REF (newdr));
3369 if (DR_BASE_ADDRESS (newdr)
3370 && DR_OFFSET (newdr)
3371 && DR_INIT (newdr)
3372 && DR_STEP (newdr)
3373 && integer_zerop (DR_STEP (newdr)))
3375 if (maybe_simd_lane_access)
3377 tree off = DR_OFFSET (newdr);
3378 STRIP_NOPS (off);
3379 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3380 && TREE_CODE (off) == MULT_EXPR
3381 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3383 tree step = TREE_OPERAND (off, 1);
3384 off = TREE_OPERAND (off, 0);
3385 STRIP_NOPS (off);
3386 if (CONVERT_EXPR_P (off)
3387 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3388 0)))
3389 < TYPE_PRECISION (TREE_TYPE (off)))
3390 off = TREE_OPERAND (off, 0);
3391 if (TREE_CODE (off) == SSA_NAME)
3393 gimple def = SSA_NAME_DEF_STMT (off);
3394 tree reft = TREE_TYPE (DR_REF (newdr));
3395 if (is_gimple_call (def)
3396 && gimple_call_internal_p (def)
3397 && (gimple_call_internal_fn (def)
3398 == IFN_GOMP_SIMD_LANE))
3400 tree arg = gimple_call_arg (def, 0);
3401 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3402 arg = SSA_NAME_VAR (arg);
3403 if (arg == loop->simduid
3404 /* For now. */
3405 && tree_int_cst_equal
3406 (TYPE_SIZE_UNIT (reft),
3407 step))
3409 DR_OFFSET (newdr) = ssize_int (0);
3410 DR_STEP (newdr) = step;
3411 DR_ALIGNED_TO (newdr)
3412 = size_int (BIGGEST_ALIGNMENT);
3413 dr = newdr;
3414 simd_lane_access = true;
3420 if (!simd_lane_access && maybe_gather)
3422 dr = newdr;
3423 gather = true;
3426 if (!gather && !simd_lane_access)
3427 free_data_ref (newdr);
3430 if (!gather && !simd_lane_access)
3432 if (dump_enabled_p ())
3434 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3435 "not vectorized: data ref analysis "
3436 "failed ");
3437 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3438 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3441 if (bb_vinfo)
3442 break;
3444 return false;
3448 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3450 if (dump_enabled_p ())
3451 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3452 "not vectorized: base addr of dr is a "
3453 "constant\n");
3455 if (bb_vinfo)
3456 break;
3458 if (gather || simd_lane_access)
3459 free_data_ref (dr);
3460 return false;
3463 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3465 if (dump_enabled_p ())
3467 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3468 "not vectorized: volatile type ");
3469 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3470 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3473 if (bb_vinfo)
3474 break;
3476 return false;
3479 if (stmt_can_throw_internal (stmt))
3481 if (dump_enabled_p ())
3483 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3484 "not vectorized: statement can throw an "
3485 "exception ");
3486 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3487 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3490 if (bb_vinfo)
3491 break;
3493 if (gather || simd_lane_access)
3494 free_data_ref (dr);
3495 return false;
3498 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3499 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3501 if (dump_enabled_p ())
3503 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3504 "not vectorized: statement is bitfield "
3505 "access ");
3506 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3507 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3510 if (bb_vinfo)
3511 break;
3513 if (gather || simd_lane_access)
3514 free_data_ref (dr);
3515 return false;
3518 base = unshare_expr (DR_BASE_ADDRESS (dr));
3519 offset = unshare_expr (DR_OFFSET (dr));
3520 init = unshare_expr (DR_INIT (dr));
3522 if (is_gimple_call (stmt)
3523 && (!gimple_call_internal_p (stmt)
3524 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3525 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3527 if (dump_enabled_p ())
3529 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3530 "not vectorized: dr in a call ");
3531 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3532 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3535 if (bb_vinfo)
3536 break;
3538 if (gather || simd_lane_access)
3539 free_data_ref (dr);
3540 return false;
3543 /* Update DR field in stmt_vec_info struct. */
3545 /* If the dataref is in an inner-loop of the loop that is considered for
3546 for vectorization, we also want to analyze the access relative to
3547 the outer-loop (DR contains information only relative to the
3548 inner-most enclosing loop). We do that by building a reference to the
3549 first location accessed by the inner-loop, and analyze it relative to
3550 the outer-loop. */
3551 if (loop && nested_in_vect_loop_p (loop, stmt))
3553 tree outer_step, outer_base, outer_init;
3554 HOST_WIDE_INT pbitsize, pbitpos;
3555 tree poffset;
3556 machine_mode pmode;
3557 int punsignedp, pvolatilep;
3558 affine_iv base_iv, offset_iv;
3559 tree dinit;
3561 /* Build a reference to the first location accessed by the
3562 inner-loop: *(BASE+INIT). (The first location is actually
3563 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3564 tree inner_base = build_fold_indirect_ref
3565 (fold_build_pointer_plus (base, init));
3567 if (dump_enabled_p ())
3569 dump_printf_loc (MSG_NOTE, vect_location,
3570 "analyze in outer-loop: ");
3571 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3572 dump_printf (MSG_NOTE, "\n");
3575 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3576 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3577 gcc_assert (outer_base != NULL_TREE);
3579 if (pbitpos % BITS_PER_UNIT != 0)
3581 if (dump_enabled_p ())
3582 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3583 "failed: bit offset alignment.\n");
3584 return false;
3587 outer_base = build_fold_addr_expr (outer_base);
3588 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3589 &base_iv, false))
3591 if (dump_enabled_p ())
3592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3593 "failed: evolution of base is not affine.\n");
3594 return false;
3597 if (offset)
3599 if (poffset)
3600 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3601 poffset);
3602 else
3603 poffset = offset;
3606 if (!poffset)
3608 offset_iv.base = ssize_int (0);
3609 offset_iv.step = ssize_int (0);
3611 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3612 &offset_iv, false))
3614 if (dump_enabled_p ())
3615 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3616 "evolution of offset is not affine.\n");
3617 return false;
3620 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3621 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3622 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3623 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3624 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3626 outer_step = size_binop (PLUS_EXPR,
3627 fold_convert (ssizetype, base_iv.step),
3628 fold_convert (ssizetype, offset_iv.step));
3630 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3631 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3632 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3633 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3634 STMT_VINFO_DR_OFFSET (stmt_info) =
3635 fold_convert (ssizetype, offset_iv.base);
3636 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3637 size_int (highest_pow2_factor (offset_iv.base));
3639 if (dump_enabled_p ())
3641 dump_printf_loc (MSG_NOTE, vect_location,
3642 "\touter base_address: ");
3643 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3644 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3645 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3646 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3647 STMT_VINFO_DR_OFFSET (stmt_info));
3648 dump_printf (MSG_NOTE,
3649 "\n\touter constant offset from base address: ");
3650 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3651 STMT_VINFO_DR_INIT (stmt_info));
3652 dump_printf (MSG_NOTE, "\n\touter step: ");
3653 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3654 STMT_VINFO_DR_STEP (stmt_info));
3655 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3656 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3657 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3658 dump_printf (MSG_NOTE, "\n");
3662 if (STMT_VINFO_DATA_REF (stmt_info))
3664 if (dump_enabled_p ())
3666 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3667 "not vectorized: more than one data ref "
3668 "in stmt: ");
3669 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3670 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3673 if (bb_vinfo)
3674 break;
3676 if (gather || simd_lane_access)
3677 free_data_ref (dr);
3678 return false;
3681 STMT_VINFO_DATA_REF (stmt_info) = dr;
3682 if (simd_lane_access)
3684 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3685 free_data_ref (datarefs[i]);
3686 datarefs[i] = dr;
3689 /* Set vectype for STMT. */
3690 scalar_type = TREE_TYPE (DR_REF (dr));
3691 STMT_VINFO_VECTYPE (stmt_info)
3692 = get_vectype_for_scalar_type (scalar_type);
3693 if (!STMT_VINFO_VECTYPE (stmt_info))
3695 if (dump_enabled_p ())
3697 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3698 "not vectorized: no vectype for stmt: ");
3699 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3700 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3701 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3702 scalar_type);
3703 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3706 if (bb_vinfo)
3707 break;
3709 if (gather || simd_lane_access)
3711 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3712 if (gather)
3713 free_data_ref (dr);
3715 return false;
3717 else
3719 if (dump_enabled_p ())
3721 dump_printf_loc (MSG_NOTE, vect_location,
3722 "got vectype for stmt: ");
3723 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3724 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3725 STMT_VINFO_VECTYPE (stmt_info));
3726 dump_printf (MSG_NOTE, "\n");
3730 /* Adjust the minimal vectorization factor according to the
3731 vector type. */
3732 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3733 if (vf > *min_vf)
3734 *min_vf = vf;
3736 if (gather)
3738 tree off;
3740 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3741 if (gather
3742 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3743 gather = false;
3744 if (!gather)
3746 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3747 free_data_ref (dr);
3748 if (dump_enabled_p ())
3750 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3751 "not vectorized: not suitable for gather "
3752 "load ");
3753 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3754 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3756 return false;
3759 datarefs[i] = dr;
3760 STMT_VINFO_GATHER_P (stmt_info) = true;
3762 else if (loop_vinfo
3763 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3765 if (nested_in_vect_loop_p (loop, stmt))
3767 if (dump_enabled_p ())
3769 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3770 "not vectorized: not suitable for strided "
3771 "load ");
3772 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3773 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3775 return false;
3777 STMT_VINFO_STRIDED_P (stmt_info) = true;
3781 /* If we stopped analysis at the first dataref we could not analyze
3782 when trying to vectorize a basic-block mark the rest of the datarefs
3783 as not vectorizable and truncate the vector of datarefs. That
3784 avoids spending useless time in analyzing their dependence. */
3785 if (i != datarefs.length ())
3787 gcc_assert (bb_vinfo != NULL);
3788 for (unsigned j = i; j < datarefs.length (); ++j)
3790 data_reference_p dr = datarefs[j];
3791 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3792 free_data_ref (dr);
3794 datarefs.truncate (i);
3797 return true;
3801 /* Function vect_get_new_vect_var.
3803 Returns a name for a new variable. The current naming scheme appends the
3804 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3805 the name of vectorizer generated variables, and appends that to NAME if
3806 provided. */
3808 tree
3809 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3811 const char *prefix;
3812 tree new_vect_var;
3814 switch (var_kind)
3816 case vect_simple_var:
3817 prefix = "vect";
3818 break;
3819 case vect_scalar_var:
3820 prefix = "stmp";
3821 break;
3822 case vect_pointer_var:
3823 prefix = "vectp";
3824 break;
3825 default:
3826 gcc_unreachable ();
3829 if (name)
3831 char* tmp = concat (prefix, "_", name, NULL);
3832 new_vect_var = create_tmp_reg (type, tmp);
3833 free (tmp);
3835 else
3836 new_vect_var = create_tmp_reg (type, prefix);
3838 return new_vect_var;
3841 /* Duplicate ptr info and set alignment/misaligment on NAME from DR. */
3843 static void
3844 vect_duplicate_ssa_name_ptr_info (tree name, data_reference *dr,
3845 stmt_vec_info stmt_info)
3847 duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr));
3848 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
3849 int misalign = DR_MISALIGNMENT (dr);
3850 if (misalign == -1)
3851 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
3852 else
3853 set_ptr_info_alignment (SSA_NAME_PTR_INFO (name), align, misalign);
3856 /* Function vect_create_addr_base_for_vector_ref.
3858 Create an expression that computes the address of the first memory location
3859 that will be accessed for a data reference.
3861 Input:
3862 STMT: The statement containing the data reference.
3863 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3864 OFFSET: Optional. If supplied, it is be added to the initial address.
3865 LOOP: Specify relative to which loop-nest should the address be computed.
3866 For example, when the dataref is in an inner-loop nested in an
3867 outer-loop that is now being vectorized, LOOP can be either the
3868 outer-loop, or the inner-loop. The first memory location accessed
3869 by the following dataref ('in' points to short):
3871 for (i=0; i<N; i++)
3872 for (j=0; j<M; j++)
3873 s += in[i+j]
3875 is as follows:
3876 if LOOP=i_loop: &in (relative to i_loop)
3877 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3878 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3879 initial address. Unlike OFFSET, which is number of elements to
3880 be added, BYTE_OFFSET is measured in bytes.
3882 Output:
3883 1. Return an SSA_NAME whose value is the address of the memory location of
3884 the first vector of the data reference.
3885 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3886 these statement(s) which define the returned SSA_NAME.
3888 FORNOW: We are only handling array accesses with step 1. */
3890 tree
3891 vect_create_addr_base_for_vector_ref (gimple stmt,
3892 gimple_seq *new_stmt_list,
3893 tree offset,
3894 struct loop *loop,
3895 tree byte_offset)
3897 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3898 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3899 tree data_ref_base;
3900 const char *base_name;
3901 tree addr_base;
3902 tree dest;
3903 gimple_seq seq = NULL;
3904 tree base_offset;
3905 tree init;
3906 tree vect_ptr_type;
3907 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3908 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3910 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3912 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3914 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3916 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3917 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3918 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3920 else
3922 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3923 base_offset = unshare_expr (DR_OFFSET (dr));
3924 init = unshare_expr (DR_INIT (dr));
3927 if (loop_vinfo)
3928 base_name = get_name (data_ref_base);
3929 else
3931 base_offset = ssize_int (0);
3932 init = ssize_int (0);
3933 base_name = get_name (DR_REF (dr));
3936 /* Create base_offset */
3937 base_offset = size_binop (PLUS_EXPR,
3938 fold_convert (sizetype, base_offset),
3939 fold_convert (sizetype, init));
3941 if (offset)
3943 offset = fold_build2 (MULT_EXPR, sizetype,
3944 fold_convert (sizetype, offset), step);
3945 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3946 base_offset, offset);
3948 if (byte_offset)
3950 byte_offset = fold_convert (sizetype, byte_offset);
3951 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3952 base_offset, byte_offset);
3955 /* base + base_offset */
3956 if (loop_vinfo)
3957 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3958 else
3960 addr_base = build1 (ADDR_EXPR,
3961 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3962 unshare_expr (DR_REF (dr)));
3965 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3966 addr_base = fold_convert (vect_ptr_type, addr_base);
3967 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3968 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3969 gimple_seq_add_seq (new_stmt_list, seq);
3971 if (DR_PTR_INFO (dr)
3972 && TREE_CODE (addr_base) == SSA_NAME)
3974 vect_duplicate_ssa_name_ptr_info (addr_base, dr, stmt_info);
3975 if (offset || byte_offset)
3976 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3979 if (dump_enabled_p ())
3981 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3982 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3983 dump_printf (MSG_NOTE, "\n");
3986 return addr_base;
3990 /* Function vect_create_data_ref_ptr.
3992 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3993 location accessed in the loop by STMT, along with the def-use update
3994 chain to appropriately advance the pointer through the loop iterations.
3995 Also set aliasing information for the pointer. This pointer is used by
3996 the callers to this function to create a memory reference expression for
3997 vector load/store access.
3999 Input:
4000 1. STMT: a stmt that references memory. Expected to be of the form
4001 GIMPLE_ASSIGN <name, data-ref> or
4002 GIMPLE_ASSIGN <data-ref, name>.
4003 2. AGGR_TYPE: the type of the reference, which should be either a vector
4004 or an array.
4005 3. AT_LOOP: the loop where the vector memref is to be created.
4006 4. OFFSET (optional): an offset to be added to the initial address accessed
4007 by the data-ref in STMT.
4008 5. BSI: location where the new stmts are to be placed if there is no loop
4009 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4010 pointing to the initial address.
4011 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4012 to the initial address accessed by the data-ref in STMT. This is
4013 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4014 in bytes.
4016 Output:
4017 1. Declare a new ptr to vector_type, and have it point to the base of the
4018 data reference (initial addressed accessed by the data reference).
4019 For example, for vector of type V8HI, the following code is generated:
4021 v8hi *ap;
4022 ap = (v8hi *)initial_address;
4024 if OFFSET is not supplied:
4025 initial_address = &a[init];
4026 if OFFSET is supplied:
4027 initial_address = &a[init + OFFSET];
4028 if BYTE_OFFSET is supplied:
4029 initial_address = &a[init] + BYTE_OFFSET;
4031 Return the initial_address in INITIAL_ADDRESS.
4033 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4034 update the pointer in each iteration of the loop.
4036 Return the increment stmt that updates the pointer in PTR_INCR.
4038 3. Set INV_P to true if the access pattern of the data reference in the
4039 vectorized loop is invariant. Set it to false otherwise.
4041 4. Return the pointer. */
4043 tree
4044 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
4045 tree offset, tree *initial_address,
4046 gimple_stmt_iterator *gsi, gimple *ptr_incr,
4047 bool only_init, bool *inv_p, tree byte_offset)
4049 const char *base_name;
4050 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4051 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4052 struct loop *loop = NULL;
4053 bool nested_in_vect_loop = false;
4054 struct loop *containing_loop = NULL;
4055 tree aggr_ptr_type;
4056 tree aggr_ptr;
4057 tree new_temp;
4058 gimple vec_stmt;
4059 gimple_seq new_stmt_list = NULL;
4060 edge pe = NULL;
4061 basic_block new_bb;
4062 tree aggr_ptr_init;
4063 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4064 tree aptr;
4065 gimple_stmt_iterator incr_gsi;
4066 bool insert_after;
4067 tree indx_before_incr, indx_after_incr;
4068 gimple incr;
4069 tree step;
4070 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4072 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4073 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4075 if (loop_vinfo)
4077 loop = LOOP_VINFO_LOOP (loop_vinfo);
4078 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4079 containing_loop = (gimple_bb (stmt))->loop_father;
4080 pe = loop_preheader_edge (loop);
4082 else
4084 gcc_assert (bb_vinfo);
4085 only_init = true;
4086 *ptr_incr = NULL;
4089 /* Check the step (evolution) of the load in LOOP, and record
4090 whether it's invariant. */
4091 if (nested_in_vect_loop)
4092 step = STMT_VINFO_DR_STEP (stmt_info);
4093 else
4094 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4096 if (integer_zerop (step))
4097 *inv_p = true;
4098 else
4099 *inv_p = false;
4101 /* Create an expression for the first address accessed by this load
4102 in LOOP. */
4103 base_name = get_name (DR_BASE_ADDRESS (dr));
4105 if (dump_enabled_p ())
4107 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4108 dump_printf_loc (MSG_NOTE, vect_location,
4109 "create %s-pointer variable to type: ",
4110 get_tree_code_name (TREE_CODE (aggr_type)));
4111 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4112 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4113 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4114 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4115 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4116 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4117 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4118 else
4119 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4120 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4121 dump_printf (MSG_NOTE, "\n");
4124 /* (1) Create the new aggregate-pointer variable.
4125 Vector and array types inherit the alias set of their component
4126 type by default so we need to use a ref-all pointer if the data
4127 reference does not conflict with the created aggregated data
4128 reference because it is not addressable. */
4129 bool need_ref_all = false;
4130 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4131 get_alias_set (DR_REF (dr))))
4132 need_ref_all = true;
4133 /* Likewise for any of the data references in the stmt group. */
4134 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4136 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4139 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4140 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4141 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4142 get_alias_set (DR_REF (sdr))))
4144 need_ref_all = true;
4145 break;
4147 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4149 while (orig_stmt);
4151 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4152 need_ref_all);
4153 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4156 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4157 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4158 def-use update cycles for the pointer: one relative to the outer-loop
4159 (LOOP), which is what steps (3) and (4) below do. The other is relative
4160 to the inner-loop (which is the inner-most loop containing the dataref),
4161 and this is done be step (5) below.
4163 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4164 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4165 redundant. Steps (3),(4) create the following:
4167 vp0 = &base_addr;
4168 LOOP: vp1 = phi(vp0,vp2)
4171 vp2 = vp1 + step
4172 goto LOOP
4174 If there is an inner-loop nested in loop, then step (5) will also be
4175 applied, and an additional update in the inner-loop will be created:
4177 vp0 = &base_addr;
4178 LOOP: vp1 = phi(vp0,vp2)
4180 inner: vp3 = phi(vp1,vp4)
4181 vp4 = vp3 + inner_step
4182 if () goto inner
4184 vp2 = vp1 + step
4185 if () goto LOOP */
4187 /* (2) Calculate the initial address of the aggregate-pointer, and set
4188 the aggregate-pointer to point to it before the loop. */
4190 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4192 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4193 offset, loop, byte_offset);
4194 if (new_stmt_list)
4196 if (pe)
4198 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4199 gcc_assert (!new_bb);
4201 else
4202 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4205 *initial_address = new_temp;
4207 /* Create: p = (aggr_type *) initial_base */
4208 if (TREE_CODE (new_temp) != SSA_NAME
4209 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4211 vec_stmt = gimple_build_assign (aggr_ptr,
4212 fold_convert (aggr_ptr_type, new_temp));
4213 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4214 /* Copy the points-to information if it exists. */
4215 if (DR_PTR_INFO (dr))
4216 vect_duplicate_ssa_name_ptr_info (aggr_ptr_init, dr, stmt_info);
4217 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4218 if (pe)
4220 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4221 gcc_assert (!new_bb);
4223 else
4224 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4226 else
4227 aggr_ptr_init = new_temp;
4229 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4230 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4231 inner-loop nested in LOOP (during outer-loop vectorization). */
4233 /* No update in loop is required. */
4234 if (only_init && (!loop_vinfo || at_loop == loop))
4235 aptr = aggr_ptr_init;
4236 else
4238 /* The step of the aggregate pointer is the type size. */
4239 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4240 /* One exception to the above is when the scalar step of the load in
4241 LOOP is zero. In this case the step here is also zero. */
4242 if (*inv_p)
4243 iv_step = size_zero_node;
4244 else if (tree_int_cst_sgn (step) == -1)
4245 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4247 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4249 create_iv (aggr_ptr_init,
4250 fold_convert (aggr_ptr_type, iv_step),
4251 aggr_ptr, loop, &incr_gsi, insert_after,
4252 &indx_before_incr, &indx_after_incr);
4253 incr = gsi_stmt (incr_gsi);
4254 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4256 /* Copy the points-to information if it exists. */
4257 if (DR_PTR_INFO (dr))
4259 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4260 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4262 if (ptr_incr)
4263 *ptr_incr = incr;
4265 aptr = indx_before_incr;
4268 if (!nested_in_vect_loop || only_init)
4269 return aptr;
4272 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4273 nested in LOOP, if exists. */
4275 gcc_assert (nested_in_vect_loop);
4276 if (!only_init)
4278 standard_iv_increment_position (containing_loop, &incr_gsi,
4279 &insert_after);
4280 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4281 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4282 &indx_after_incr);
4283 incr = gsi_stmt (incr_gsi);
4284 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4286 /* Copy the points-to information if it exists. */
4287 if (DR_PTR_INFO (dr))
4289 vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
4290 vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
4292 if (ptr_incr)
4293 *ptr_incr = incr;
4295 return indx_before_incr;
4297 else
4298 gcc_unreachable ();
4302 /* Function bump_vector_ptr
4304 Increment a pointer (to a vector type) by vector-size. If requested,
4305 i.e. if PTR-INCR is given, then also connect the new increment stmt
4306 to the existing def-use update-chain of the pointer, by modifying
4307 the PTR_INCR as illustrated below:
4309 The pointer def-use update-chain before this function:
4310 DATAREF_PTR = phi (p_0, p_2)
4311 ....
4312 PTR_INCR: p_2 = DATAREF_PTR + step
4314 The pointer def-use update-chain after this function:
4315 DATAREF_PTR = phi (p_0, p_2)
4316 ....
4317 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4318 ....
4319 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4321 Input:
4322 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4323 in the loop.
4324 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4325 the loop. The increment amount across iterations is expected
4326 to be vector_size.
4327 BSI - location where the new update stmt is to be placed.
4328 STMT - the original scalar memory-access stmt that is being vectorized.
4329 BUMP - optional. The offset by which to bump the pointer. If not given,
4330 the offset is assumed to be vector_size.
4332 Output: Return NEW_DATAREF_PTR as illustrated above.
4336 tree
4337 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4338 gimple stmt, tree bump)
4340 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4341 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4342 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4343 tree update = TYPE_SIZE_UNIT (vectype);
4344 gassign *incr_stmt;
4345 ssa_op_iter iter;
4346 use_operand_p use_p;
4347 tree new_dataref_ptr;
4349 if (bump)
4350 update = bump;
4352 new_dataref_ptr = copy_ssa_name (dataref_ptr);
4353 incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
4354 dataref_ptr, update);
4355 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4357 /* Copy the points-to information if it exists. */
4358 if (DR_PTR_INFO (dr))
4360 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4361 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4364 if (!ptr_incr)
4365 return new_dataref_ptr;
4367 /* Update the vector-pointer's cross-iteration increment. */
4368 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4370 tree use = USE_FROM_PTR (use_p);
4372 if (use == dataref_ptr)
4373 SET_USE (use_p, new_dataref_ptr);
4374 else
4375 gcc_assert (tree_int_cst_compare (use, update) == 0);
4378 return new_dataref_ptr;
4382 /* Function vect_create_destination_var.
4384 Create a new temporary of type VECTYPE. */
4386 tree
4387 vect_create_destination_var (tree scalar_dest, tree vectype)
4389 tree vec_dest;
4390 const char *name;
4391 char *new_name;
4392 tree type;
4393 enum vect_var_kind kind;
4395 kind = vectype ? vect_simple_var : vect_scalar_var;
4396 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4398 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4400 name = get_name (scalar_dest);
4401 if (name)
4402 new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4403 else
4404 new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
4405 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4406 free (new_name);
4408 return vec_dest;
4411 /* Function vect_grouped_store_supported.
4413 Returns TRUE if interleave high and interleave low permutations
4414 are supported, and FALSE otherwise. */
4416 bool
4417 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4419 machine_mode mode = TYPE_MODE (vectype);
4421 /* vect_permute_store_chain requires the group size to be equal to 3 or
4422 be a power of two. */
4423 if (count != 3 && exact_log2 (count) == -1)
4425 if (dump_enabled_p ())
4426 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4427 "the size of the group of accesses"
4428 " is not a power of 2 or not eqaul to 3\n");
4429 return false;
4432 /* Check that the permutation is supported. */
4433 if (VECTOR_MODE_P (mode))
4435 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4436 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4438 if (count == 3)
4440 unsigned int j0 = 0, j1 = 0, j2 = 0;
4441 unsigned int i, j;
4443 for (j = 0; j < 3; j++)
4445 int nelt0 = ((3 - j) * nelt) % 3;
4446 int nelt1 = ((3 - j) * nelt + 1) % 3;
4447 int nelt2 = ((3 - j) * nelt + 2) % 3;
4448 for (i = 0; i < nelt; i++)
4450 if (3 * i + nelt0 < nelt)
4451 sel[3 * i + nelt0] = j0++;
4452 if (3 * i + nelt1 < nelt)
4453 sel[3 * i + nelt1] = nelt + j1++;
4454 if (3 * i + nelt2 < nelt)
4455 sel[3 * i + nelt2] = 0;
4457 if (!can_vec_perm_p (mode, false, sel))
4459 if (dump_enabled_p ())
4460 dump_printf (MSG_MISSED_OPTIMIZATION,
4461 "permutaion op not supported by target.\n");
4462 return false;
4465 for (i = 0; i < nelt; i++)
4467 if (3 * i + nelt0 < nelt)
4468 sel[3 * i + nelt0] = 3 * i + nelt0;
4469 if (3 * i + nelt1 < nelt)
4470 sel[3 * i + nelt1] = 3 * i + nelt1;
4471 if (3 * i + nelt2 < nelt)
4472 sel[3 * i + nelt2] = nelt + j2++;
4474 if (!can_vec_perm_p (mode, false, sel))
4476 if (dump_enabled_p ())
4477 dump_printf (MSG_MISSED_OPTIMIZATION,
4478 "permutaion op not supported by target.\n");
4479 return false;
4482 return true;
4484 else
4486 /* If length is not equal to 3 then only power of 2 is supported. */
4487 gcc_assert (exact_log2 (count) != -1);
4489 for (i = 0; i < nelt / 2; i++)
4491 sel[i * 2] = i;
4492 sel[i * 2 + 1] = i + nelt;
4494 if (can_vec_perm_p (mode, false, sel))
4496 for (i = 0; i < nelt; i++)
4497 sel[i] += nelt / 2;
4498 if (can_vec_perm_p (mode, false, sel))
4499 return true;
4504 if (dump_enabled_p ())
4505 dump_printf (MSG_MISSED_OPTIMIZATION,
4506 "permutaion op not supported by target.\n");
4507 return false;
4511 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4512 type VECTYPE. */
4514 bool
4515 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4517 return vect_lanes_optab_supported_p ("vec_store_lanes",
4518 vec_store_lanes_optab,
4519 vectype, count);
4523 /* Function vect_permute_store_chain.
4525 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4526 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4527 the data correctly for the stores. Return the final references for stores
4528 in RESULT_CHAIN.
4530 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4531 The input is 4 vectors each containing 8 elements. We assign a number to
4532 each element, the input sequence is:
4534 1st vec: 0 1 2 3 4 5 6 7
4535 2nd vec: 8 9 10 11 12 13 14 15
4536 3rd vec: 16 17 18 19 20 21 22 23
4537 4th vec: 24 25 26 27 28 29 30 31
4539 The output sequence should be:
4541 1st vec: 0 8 16 24 1 9 17 25
4542 2nd vec: 2 10 18 26 3 11 19 27
4543 3rd vec: 4 12 20 28 5 13 21 30
4544 4th vec: 6 14 22 30 7 15 23 31
4546 i.e., we interleave the contents of the four vectors in their order.
4548 We use interleave_high/low instructions to create such output. The input of
4549 each interleave_high/low operation is two vectors:
4550 1st vec 2nd vec
4551 0 1 2 3 4 5 6 7
4552 the even elements of the result vector are obtained left-to-right from the
4553 high/low elements of the first vector. The odd elements of the result are
4554 obtained left-to-right from the high/low elements of the second vector.
4555 The output of interleave_high will be: 0 4 1 5
4556 and of interleave_low: 2 6 3 7
4559 The permutation is done in log LENGTH stages. In each stage interleave_high
4560 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4561 where the first argument is taken from the first half of DR_CHAIN and the
4562 second argument from it's second half.
4563 In our example,
4565 I1: interleave_high (1st vec, 3rd vec)
4566 I2: interleave_low (1st vec, 3rd vec)
4567 I3: interleave_high (2nd vec, 4th vec)
4568 I4: interleave_low (2nd vec, 4th vec)
4570 The output for the first stage is:
4572 I1: 0 16 1 17 2 18 3 19
4573 I2: 4 20 5 21 6 22 7 23
4574 I3: 8 24 9 25 10 26 11 27
4575 I4: 12 28 13 29 14 30 15 31
4577 The output of the second stage, i.e. the final result is:
4579 I1: 0 8 16 24 1 9 17 25
4580 I2: 2 10 18 26 3 11 19 27
4581 I3: 4 12 20 28 5 13 21 30
4582 I4: 6 14 22 30 7 15 23 31. */
4584 void
4585 vect_permute_store_chain (vec<tree> dr_chain,
4586 unsigned int length,
4587 gimple stmt,
4588 gimple_stmt_iterator *gsi,
4589 vec<tree> *result_chain)
4591 tree vect1, vect2, high, low;
4592 gimple perm_stmt;
4593 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4594 tree perm_mask_low, perm_mask_high;
4595 tree data_ref;
4596 tree perm3_mask_low, perm3_mask_high;
4597 unsigned int i, n, log_length = exact_log2 (length);
4598 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4599 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4601 result_chain->quick_grow (length);
4602 memcpy (result_chain->address (), dr_chain.address (),
4603 length * sizeof (tree));
4605 if (length == 3)
4607 unsigned int j0 = 0, j1 = 0, j2 = 0;
4609 for (j = 0; j < 3; j++)
4611 int nelt0 = ((3 - j) * nelt) % 3;
4612 int nelt1 = ((3 - j) * nelt + 1) % 3;
4613 int nelt2 = ((3 - j) * nelt + 2) % 3;
4615 for (i = 0; i < nelt; i++)
4617 if (3 * i + nelt0 < nelt)
4618 sel[3 * i + nelt0] = j0++;
4619 if (3 * i + nelt1 < nelt)
4620 sel[3 * i + nelt1] = nelt + j1++;
4621 if (3 * i + nelt2 < nelt)
4622 sel[3 * i + nelt2] = 0;
4624 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4626 for (i = 0; i < nelt; i++)
4628 if (3 * i + nelt0 < nelt)
4629 sel[3 * i + nelt0] = 3 * i + nelt0;
4630 if (3 * i + nelt1 < nelt)
4631 sel[3 * i + nelt1] = 3 * i + nelt1;
4632 if (3 * i + nelt2 < nelt)
4633 sel[3 * i + nelt2] = nelt + j2++;
4635 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4637 vect1 = dr_chain[0];
4638 vect2 = dr_chain[1];
4640 /* Create interleaving stmt:
4641 low = VEC_PERM_EXPR <vect1, vect2,
4642 {j, nelt, *, j + 1, nelt + j + 1, *,
4643 j + 2, nelt + j + 2, *, ...}> */
4644 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4645 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4646 vect2, perm3_mask_low);
4647 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4649 vect1 = data_ref;
4650 vect2 = dr_chain[2];
4651 /* Create interleaving stmt:
4652 low = VEC_PERM_EXPR <vect1, vect2,
4653 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4654 6, 7, nelt + j + 2, ...}> */
4655 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4656 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4657 vect2, perm3_mask_high);
4658 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4659 (*result_chain)[j] = data_ref;
4662 else
4664 /* If length is not equal to 3 then only power of 2 is supported. */
4665 gcc_assert (exact_log2 (length) != -1);
4667 for (i = 0, n = nelt / 2; i < n; i++)
4669 sel[i * 2] = i;
4670 sel[i * 2 + 1] = i + nelt;
4672 perm_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4674 for (i = 0; i < nelt; i++)
4675 sel[i] += nelt / 2;
4676 perm_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4678 for (i = 0, n = log_length; i < n; i++)
4680 for (j = 0; j < length/2; j++)
4682 vect1 = dr_chain[j];
4683 vect2 = dr_chain[j+length/2];
4685 /* Create interleaving stmt:
4686 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4687 ...}> */
4688 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4689 perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
4690 vect2, perm_mask_high);
4691 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4692 (*result_chain)[2*j] = high;
4694 /* Create interleaving stmt:
4695 low = VEC_PERM_EXPR <vect1, vect2,
4696 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4697 ...}> */
4698 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4699 perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
4700 vect2, perm_mask_low);
4701 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4702 (*result_chain)[2*j+1] = low;
4704 memcpy (dr_chain.address (), result_chain->address (),
4705 length * sizeof (tree));
4710 /* Function vect_setup_realignment
4712 This function is called when vectorizing an unaligned load using
4713 the dr_explicit_realign[_optimized] scheme.
4714 This function generates the following code at the loop prolog:
4716 p = initial_addr;
4717 x msq_init = *(floor(p)); # prolog load
4718 realignment_token = call target_builtin;
4719 loop:
4720 x msq = phi (msq_init, ---)
4722 The stmts marked with x are generated only for the case of
4723 dr_explicit_realign_optimized.
4725 The code above sets up a new (vector) pointer, pointing to the first
4726 location accessed by STMT, and a "floor-aligned" load using that pointer.
4727 It also generates code to compute the "realignment-token" (if the relevant
4728 target hook was defined), and creates a phi-node at the loop-header bb
4729 whose arguments are the result of the prolog-load (created by this
4730 function) and the result of a load that takes place in the loop (to be
4731 created by the caller to this function).
4733 For the case of dr_explicit_realign_optimized:
4734 The caller to this function uses the phi-result (msq) to create the
4735 realignment code inside the loop, and sets up the missing phi argument,
4736 as follows:
4737 loop:
4738 msq = phi (msq_init, lsq)
4739 lsq = *(floor(p')); # load in loop
4740 result = realign_load (msq, lsq, realignment_token);
4742 For the case of dr_explicit_realign:
4743 loop:
4744 msq = *(floor(p)); # load in loop
4745 p' = p + (VS-1);
4746 lsq = *(floor(p')); # load in loop
4747 result = realign_load (msq, lsq, realignment_token);
4749 Input:
4750 STMT - (scalar) load stmt to be vectorized. This load accesses
4751 a memory location that may be unaligned.
4752 BSI - place where new code is to be inserted.
4753 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4754 is used.
4756 Output:
4757 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4758 target hook, if defined.
4759 Return value - the result of the loop-header phi node. */
4761 tree
4762 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4763 tree *realignment_token,
4764 enum dr_alignment_support alignment_support_scheme,
4765 tree init_addr,
4766 struct loop **at_loop)
4768 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4769 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4770 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4771 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4772 struct loop *loop = NULL;
4773 edge pe = NULL;
4774 tree scalar_dest = gimple_assign_lhs (stmt);
4775 tree vec_dest;
4776 gimple inc;
4777 tree ptr;
4778 tree data_ref;
4779 basic_block new_bb;
4780 tree msq_init = NULL_TREE;
4781 tree new_temp;
4782 gphi *phi_stmt;
4783 tree msq = NULL_TREE;
4784 gimple_seq stmts = NULL;
4785 bool inv_p;
4786 bool compute_in_loop = false;
4787 bool nested_in_vect_loop = false;
4788 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4789 struct loop *loop_for_initial_load = NULL;
4791 if (loop_vinfo)
4793 loop = LOOP_VINFO_LOOP (loop_vinfo);
4794 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4797 gcc_assert (alignment_support_scheme == dr_explicit_realign
4798 || alignment_support_scheme == dr_explicit_realign_optimized);
4800 /* We need to generate three things:
4801 1. the misalignment computation
4802 2. the extra vector load (for the optimized realignment scheme).
4803 3. the phi node for the two vectors from which the realignment is
4804 done (for the optimized realignment scheme). */
4806 /* 1. Determine where to generate the misalignment computation.
4808 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4809 calculation will be generated by this function, outside the loop (in the
4810 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4811 caller, inside the loop.
4813 Background: If the misalignment remains fixed throughout the iterations of
4814 the loop, then both realignment schemes are applicable, and also the
4815 misalignment computation can be done outside LOOP. This is because we are
4816 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4817 are a multiple of VS (the Vector Size), and therefore the misalignment in
4818 different vectorized LOOP iterations is always the same.
4819 The problem arises only if the memory access is in an inner-loop nested
4820 inside LOOP, which is now being vectorized using outer-loop vectorization.
4821 This is the only case when the misalignment of the memory access may not
4822 remain fixed throughout the iterations of the inner-loop (as explained in
4823 detail in vect_supportable_dr_alignment). In this case, not only is the
4824 optimized realignment scheme not applicable, but also the misalignment
4825 computation (and generation of the realignment token that is passed to
4826 REALIGN_LOAD) have to be done inside the loop.
4828 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4829 or not, which in turn determines if the misalignment is computed inside
4830 the inner-loop, or outside LOOP. */
4832 if (init_addr != NULL_TREE || !loop_vinfo)
4834 compute_in_loop = true;
4835 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4839 /* 2. Determine where to generate the extra vector load.
4841 For the optimized realignment scheme, instead of generating two vector
4842 loads in each iteration, we generate a single extra vector load in the
4843 preheader of the loop, and in each iteration reuse the result of the
4844 vector load from the previous iteration. In case the memory access is in
4845 an inner-loop nested inside LOOP, which is now being vectorized using
4846 outer-loop vectorization, we need to determine whether this initial vector
4847 load should be generated at the preheader of the inner-loop, or can be
4848 generated at the preheader of LOOP. If the memory access has no evolution
4849 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4850 to be generated inside LOOP (in the preheader of the inner-loop). */
4852 if (nested_in_vect_loop)
4854 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4855 bool invariant_in_outerloop =
4856 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4857 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4859 else
4860 loop_for_initial_load = loop;
4861 if (at_loop)
4862 *at_loop = loop_for_initial_load;
4864 if (loop_for_initial_load)
4865 pe = loop_preheader_edge (loop_for_initial_load);
4867 /* 3. For the case of the optimized realignment, create the first vector
4868 load at the loop preheader. */
4870 if (alignment_support_scheme == dr_explicit_realign_optimized)
4872 /* Create msq_init = *(floor(p1)) in the loop preheader */
4873 gassign *new_stmt;
4875 gcc_assert (!compute_in_loop);
4876 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4877 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4878 NULL_TREE, &init_addr, NULL, &inc,
4879 true, &inv_p);
4880 new_temp = copy_ssa_name (ptr);
4881 new_stmt = gimple_build_assign
4882 (new_temp, BIT_AND_EXPR, ptr,
4883 build_int_cst (TREE_TYPE (ptr),
4884 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4885 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4886 gcc_assert (!new_bb);
4887 data_ref
4888 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4889 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4890 new_stmt = gimple_build_assign (vec_dest, data_ref);
4891 new_temp = make_ssa_name (vec_dest, new_stmt);
4892 gimple_assign_set_lhs (new_stmt, new_temp);
4893 if (pe)
4895 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4896 gcc_assert (!new_bb);
4898 else
4899 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4901 msq_init = gimple_assign_lhs (new_stmt);
4904 /* 4. Create realignment token using a target builtin, if available.
4905 It is done either inside the containing loop, or before LOOP (as
4906 determined above). */
4908 if (targetm.vectorize.builtin_mask_for_load)
4910 gcall *new_stmt;
4911 tree builtin_decl;
4913 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4914 if (!init_addr)
4916 /* Generate the INIT_ADDR computation outside LOOP. */
4917 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4918 NULL_TREE, loop);
4919 if (loop)
4921 pe = loop_preheader_edge (loop);
4922 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4923 gcc_assert (!new_bb);
4925 else
4926 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4929 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4930 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4931 vec_dest =
4932 vect_create_destination_var (scalar_dest,
4933 gimple_call_return_type (new_stmt));
4934 new_temp = make_ssa_name (vec_dest, new_stmt);
4935 gimple_call_set_lhs (new_stmt, new_temp);
4937 if (compute_in_loop)
4938 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4939 else
4941 /* Generate the misalignment computation outside LOOP. */
4942 pe = loop_preheader_edge (loop);
4943 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4944 gcc_assert (!new_bb);
4947 *realignment_token = gimple_call_lhs (new_stmt);
4949 /* The result of the CALL_EXPR to this builtin is determined from
4950 the value of the parameter and no global variables are touched
4951 which makes the builtin a "const" function. Requiring the
4952 builtin to have the "const" attribute makes it unnecessary
4953 to call mark_call_clobbered. */
4954 gcc_assert (TREE_READONLY (builtin_decl));
4957 if (alignment_support_scheme == dr_explicit_realign)
4958 return msq;
4960 gcc_assert (!compute_in_loop);
4961 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4964 /* 5. Create msq = phi <msq_init, lsq> in loop */
4966 pe = loop_preheader_edge (containing_loop);
4967 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4968 msq = make_ssa_name (vec_dest);
4969 phi_stmt = create_phi_node (msq, containing_loop->header);
4970 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4972 return msq;
4976 /* Function vect_grouped_load_supported.
4978 Returns TRUE if even and odd permutations are supported,
4979 and FALSE otherwise. */
4981 bool
4982 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4984 machine_mode mode = TYPE_MODE (vectype);
4986 /* vect_permute_load_chain requires the group size to be equal to 3 or
4987 be a power of two. */
4988 if (count != 3 && exact_log2 (count) == -1)
4990 if (dump_enabled_p ())
4991 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4992 "the size of the group of accesses"
4993 " is not a power of 2 or not equal to 3\n");
4994 return false;
4997 /* Check that the permutation is supported. */
4998 if (VECTOR_MODE_P (mode))
5000 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5001 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5003 if (count == 3)
5005 unsigned int k;
5006 for (k = 0; k < 3; k++)
5008 for (i = 0; i < nelt; i++)
5009 if (3 * i + k < 2 * nelt)
5010 sel[i] = 3 * i + k;
5011 else
5012 sel[i] = 0;
5013 if (!can_vec_perm_p (mode, false, sel))
5015 if (dump_enabled_p ())
5016 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5017 "shuffle of 3 loads is not supported by"
5018 " target\n");
5019 return false;
5021 for (i = 0, j = 0; i < nelt; i++)
5022 if (3 * i + k < 2 * nelt)
5023 sel[i] = i;
5024 else
5025 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5026 if (!can_vec_perm_p (mode, false, sel))
5028 if (dump_enabled_p ())
5029 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5030 "shuffle of 3 loads is not supported by"
5031 " target\n");
5032 return false;
5035 return true;
5037 else
5039 /* If length is not equal to 3 then only power of 2 is supported. */
5040 gcc_assert (exact_log2 (count) != -1);
5041 for (i = 0; i < nelt; i++)
5042 sel[i] = i * 2;
5043 if (can_vec_perm_p (mode, false, sel))
5045 for (i = 0; i < nelt; i++)
5046 sel[i] = i * 2 + 1;
5047 if (can_vec_perm_p (mode, false, sel))
5048 return true;
5053 if (dump_enabled_p ())
5054 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5055 "extract even/odd not supported by target\n");
5056 return false;
5059 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5060 type VECTYPE. */
5062 bool
5063 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5065 return vect_lanes_optab_supported_p ("vec_load_lanes",
5066 vec_load_lanes_optab,
5067 vectype, count);
5070 /* Function vect_permute_load_chain.
5072 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5073 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5074 the input data correctly. Return the final references for loads in
5075 RESULT_CHAIN.
5077 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5078 The input is 4 vectors each containing 8 elements. We assign a number to each
5079 element, the input sequence is:
5081 1st vec: 0 1 2 3 4 5 6 7
5082 2nd vec: 8 9 10 11 12 13 14 15
5083 3rd vec: 16 17 18 19 20 21 22 23
5084 4th vec: 24 25 26 27 28 29 30 31
5086 The output sequence should be:
5088 1st vec: 0 4 8 12 16 20 24 28
5089 2nd vec: 1 5 9 13 17 21 25 29
5090 3rd vec: 2 6 10 14 18 22 26 30
5091 4th vec: 3 7 11 15 19 23 27 31
5093 i.e., the first output vector should contain the first elements of each
5094 interleaving group, etc.
5096 We use extract_even/odd instructions to create such output. The input of
5097 each extract_even/odd operation is two vectors
5098 1st vec 2nd vec
5099 0 1 2 3 4 5 6 7
5101 and the output is the vector of extracted even/odd elements. The output of
5102 extract_even will be: 0 2 4 6
5103 and of extract_odd: 1 3 5 7
5106 The permutation is done in log LENGTH stages. In each stage extract_even
5107 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5108 their order. In our example,
5110 E1: extract_even (1st vec, 2nd vec)
5111 E2: extract_odd (1st vec, 2nd vec)
5112 E3: extract_even (3rd vec, 4th vec)
5113 E4: extract_odd (3rd vec, 4th vec)
5115 The output for the first stage will be:
5117 E1: 0 2 4 6 8 10 12 14
5118 E2: 1 3 5 7 9 11 13 15
5119 E3: 16 18 20 22 24 26 28 30
5120 E4: 17 19 21 23 25 27 29 31
5122 In order to proceed and create the correct sequence for the next stage (or
5123 for the correct output, if the second stage is the last one, as in our
5124 example), we first put the output of extract_even operation and then the
5125 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5126 The input for the second stage is:
5128 1st vec (E1): 0 2 4 6 8 10 12 14
5129 2nd vec (E3): 16 18 20 22 24 26 28 30
5130 3rd vec (E2): 1 3 5 7 9 11 13 15
5131 4th vec (E4): 17 19 21 23 25 27 29 31
5133 The output of the second stage:
5135 E1: 0 4 8 12 16 20 24 28
5136 E2: 2 6 10 14 18 22 26 30
5137 E3: 1 5 9 13 17 21 25 29
5138 E4: 3 7 11 15 19 23 27 31
5140 And RESULT_CHAIN after reordering:
5142 1st vec (E1): 0 4 8 12 16 20 24 28
5143 2nd vec (E3): 1 5 9 13 17 21 25 29
5144 3rd vec (E2): 2 6 10 14 18 22 26 30
5145 4th vec (E4): 3 7 11 15 19 23 27 31. */
5147 static void
5148 vect_permute_load_chain (vec<tree> dr_chain,
5149 unsigned int length,
5150 gimple stmt,
5151 gimple_stmt_iterator *gsi,
5152 vec<tree> *result_chain)
5154 tree data_ref, first_vect, second_vect;
5155 tree perm_mask_even, perm_mask_odd;
5156 tree perm3_mask_low, perm3_mask_high;
5157 gimple perm_stmt;
5158 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5159 unsigned int i, j, log_length = exact_log2 (length);
5160 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5161 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5163 result_chain->quick_grow (length);
5164 memcpy (result_chain->address (), dr_chain.address (),
5165 length * sizeof (tree));
5167 if (length == 3)
5169 unsigned int k;
5171 for (k = 0; k < 3; k++)
5173 for (i = 0; i < nelt; i++)
5174 if (3 * i + k < 2 * nelt)
5175 sel[i] = 3 * i + k;
5176 else
5177 sel[i] = 0;
5178 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
5180 for (i = 0, j = 0; i < nelt; i++)
5181 if (3 * i + k < 2 * nelt)
5182 sel[i] = i;
5183 else
5184 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5186 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
5188 first_vect = dr_chain[0];
5189 second_vect = dr_chain[1];
5191 /* Create interleaving stmt (low part of):
5192 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5193 ...}> */
5194 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5195 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5196 second_vect, perm3_mask_low);
5197 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5199 /* Create interleaving stmt (high part of):
5200 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5201 ...}> */
5202 first_vect = data_ref;
5203 second_vect = dr_chain[2];
5204 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5205 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5206 second_vect, perm3_mask_high);
5207 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5208 (*result_chain)[k] = data_ref;
5211 else
5213 /* If length is not equal to 3 then only power of 2 is supported. */
5214 gcc_assert (exact_log2 (length) != -1);
5216 for (i = 0; i < nelt; ++i)
5217 sel[i] = i * 2;
5218 perm_mask_even = vect_gen_perm_mask_checked (vectype, sel);
5220 for (i = 0; i < nelt; ++i)
5221 sel[i] = i * 2 + 1;
5222 perm_mask_odd = vect_gen_perm_mask_checked (vectype, sel);
5224 for (i = 0; i < log_length; i++)
5226 for (j = 0; j < length; j += 2)
5228 first_vect = dr_chain[j];
5229 second_vect = dr_chain[j+1];
5231 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5232 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5233 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5234 first_vect, second_vect,
5235 perm_mask_even);
5236 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5237 (*result_chain)[j/2] = data_ref;
5239 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5240 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5241 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5242 first_vect, second_vect,
5243 perm_mask_odd);
5244 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5245 (*result_chain)[j/2+length/2] = data_ref;
5247 memcpy (dr_chain.address (), result_chain->address (),
5248 length * sizeof (tree));
5253 /* Function vect_shift_permute_load_chain.
5255 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5256 sequence of stmts to reorder the input data accordingly.
5257 Return the final references for loads in RESULT_CHAIN.
5258 Return true if successed, false otherwise.
5260 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5261 The input is 3 vectors each containing 8 elements. We assign a
5262 number to each element, the input sequence is:
5264 1st vec: 0 1 2 3 4 5 6 7
5265 2nd vec: 8 9 10 11 12 13 14 15
5266 3rd vec: 16 17 18 19 20 21 22 23
5268 The output sequence should be:
5270 1st vec: 0 3 6 9 12 15 18 21
5271 2nd vec: 1 4 7 10 13 16 19 22
5272 3rd vec: 2 5 8 11 14 17 20 23
5274 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5276 First we shuffle all 3 vectors to get correct elements order:
5278 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5279 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5280 3rd vec: (16 19 22) (17 20 23) (18 21)
5282 Next we unite and shift vector 3 times:
5284 1st step:
5285 shift right by 6 the concatenation of:
5286 "1st vec" and "2nd vec"
5287 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5288 "2nd vec" and "3rd vec"
5289 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5290 "3rd vec" and "1st vec"
5291 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5292 | New vectors |
5294 So that now new vectors are:
5296 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5297 2nd vec: (10 13) (16 19 22) (17 20 23)
5298 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5300 2nd step:
5301 shift right by 5 the concatenation of:
5302 "1st vec" and "3rd vec"
5303 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5304 "2nd vec" and "1st vec"
5305 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5306 "3rd vec" and "2nd vec"
5307 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5308 | New vectors |
5310 So that now new vectors are:
5312 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5313 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5314 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5316 3rd step:
5317 shift right by 5 the concatenation of:
5318 "1st vec" and "1st vec"
5319 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5320 shift right by 3 the concatenation of:
5321 "2nd vec" and "2nd vec"
5322 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5323 | New vectors |
5325 So that now all vectors are READY:
5326 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5327 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5328 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5330 This algorithm is faster than one in vect_permute_load_chain if:
5331 1. "shift of a concatination" is faster than general permutation.
5332 This is usually so.
5333 2. The TARGET machine can't execute vector instructions in parallel.
5334 This is because each step of the algorithm depends on previous.
5335 The algorithm in vect_permute_load_chain is much more parallel.
5337 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5340 static bool
5341 vect_shift_permute_load_chain (vec<tree> dr_chain,
5342 unsigned int length,
5343 gimple stmt,
5344 gimple_stmt_iterator *gsi,
5345 vec<tree> *result_chain)
5347 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5348 tree perm2_mask1, perm2_mask2, perm3_mask;
5349 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5350 gimple perm_stmt;
5352 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5353 unsigned int i;
5354 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5355 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5356 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5357 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5359 result_chain->quick_grow (length);
5360 memcpy (result_chain->address (), dr_chain.address (),
5361 length * sizeof (tree));
5363 if (exact_log2 (length) != -1 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5365 unsigned int j, log_length = exact_log2 (length);
5366 for (i = 0; i < nelt / 2; ++i)
5367 sel[i] = i * 2;
5368 for (i = 0; i < nelt / 2; ++i)
5369 sel[nelt / 2 + i] = i * 2 + 1;
5370 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5372 if (dump_enabled_p ())
5373 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5374 "shuffle of 2 fields structure is not \
5375 supported by target\n");
5376 return false;
5378 perm2_mask1 = vect_gen_perm_mask_checked (vectype, sel);
5380 for (i = 0; i < nelt / 2; ++i)
5381 sel[i] = i * 2 + 1;
5382 for (i = 0; i < nelt / 2; ++i)
5383 sel[nelt / 2 + i] = i * 2;
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_mask2 = vect_gen_perm_mask_checked (vectype, sel);
5394 /* Generating permutation constant to shift all elements.
5395 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5396 for (i = 0; i < nelt; i++)
5397 sel[i] = nelt / 2 + i;
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 "shift permutation is not supported by target\n");
5403 return false;
5405 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5407 /* Generating permutation constant to select vector from 2.
5408 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5409 for (i = 0; i < nelt / 2; i++)
5410 sel[i] = i;
5411 for (i = nelt / 2; i < nelt; i++)
5412 sel[i] = nelt + i;
5413 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5415 if (dump_enabled_p ())
5416 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5417 "select is not supported by target\n");
5418 return false;
5420 select_mask = vect_gen_perm_mask_checked (vectype, sel);
5422 for (i = 0; i < log_length; i++)
5424 for (j = 0; j < length; j += 2)
5426 first_vect = dr_chain[j];
5427 second_vect = dr_chain[j + 1];
5429 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5430 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5431 first_vect, first_vect,
5432 perm2_mask1);
5433 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5434 vect[0] = data_ref;
5436 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5437 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5438 second_vect, second_vect,
5439 perm2_mask2);
5440 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5441 vect[1] = data_ref;
5443 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5444 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5445 vect[0], vect[1], shift1_mask);
5446 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5447 (*result_chain)[j/2 + length/2] = data_ref;
5449 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5450 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5451 vect[0], vect[1], select_mask);
5452 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5453 (*result_chain)[j/2] = data_ref;
5455 memcpy (dr_chain.address (), result_chain->address (),
5456 length * sizeof (tree));
5458 return true;
5460 if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
5462 unsigned int k = 0, l = 0;
5464 /* Generating permutation constant to get all elements in rigth order.
5465 For vector length 8 it is {0 3 6 1 4 7 2 5}. */
5466 for (i = 0; i < nelt; i++)
5468 if (3 * k + (l % 3) >= nelt)
5470 k = 0;
5471 l += (3 - (nelt % 3));
5473 sel[i] = 3 * k + (l % 3);
5474 k++;
5476 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5478 if (dump_enabled_p ())
5479 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5480 "shuffle of 3 fields structure is not \
5481 supported by target\n");
5482 return false;
5484 perm3_mask = vect_gen_perm_mask_checked (vectype, sel);
5486 /* Generating permutation constant to shift all elements.
5487 For vector length 8 it is {6 7 8 9 10 11 12 13}. */
5488 for (i = 0; i < nelt; i++)
5489 sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
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 "shift permutation is not supported by target\n");
5495 return false;
5497 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5499 /* Generating permutation constant to shift all elements.
5500 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5501 for (i = 0; i < nelt; i++)
5502 sel[i] = 2 * (nelt / 3) + 1 + i;
5503 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5505 if (dump_enabled_p ())
5506 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5507 "shift permutation is not supported by target\n");
5508 return false;
5510 shift2_mask = vect_gen_perm_mask_checked (vectype, sel);
5512 /* Generating permutation constant to shift all elements.
5513 For vector length 8 it is {3 4 5 6 7 8 9 10}. */
5514 for (i = 0; i < nelt; i++)
5515 sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
5516 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5518 if (dump_enabled_p ())
5519 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5520 "shift permutation is not supported by target\n");
5521 return false;
5523 shift3_mask = vect_gen_perm_mask_checked (vectype, sel);
5525 /* Generating permutation constant to shift all elements.
5526 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5527 for (i = 0; i < nelt; i++)
5528 sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
5529 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5531 if (dump_enabled_p ())
5532 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5533 "shift permutation is not supported by target\n");
5534 return false;
5536 shift4_mask = vect_gen_perm_mask_checked (vectype, sel);
5538 for (k = 0; k < 3; k++)
5540 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
5541 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5542 dr_chain[k], dr_chain[k],
5543 perm3_mask);
5544 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5545 vect[k] = data_ref;
5548 for (k = 0; k < 3; k++)
5550 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
5551 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5552 vect[k % 3], vect[(k + 1) % 3],
5553 shift1_mask);
5554 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5555 vect_shift[k] = data_ref;
5558 for (k = 0; k < 3; k++)
5560 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
5561 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5562 vect_shift[(4 - k) % 3],
5563 vect_shift[(3 - k) % 3],
5564 shift2_mask);
5565 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5566 vect[k] = data_ref;
5569 (*result_chain)[3 - (nelt % 3)] = vect[2];
5571 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
5572 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
5573 vect[0], shift3_mask);
5574 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5575 (*result_chain)[nelt % 3] = data_ref;
5577 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
5578 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
5579 vect[1], shift4_mask);
5580 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5581 (*result_chain)[0] = data_ref;
5582 return true;
5584 return false;
5587 /* Function vect_transform_grouped_load.
5589 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5590 to perform their permutation and ascribe the result vectorized statements to
5591 the scalar statements.
5594 void
5595 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
5596 gimple_stmt_iterator *gsi)
5598 machine_mode mode;
5599 vec<tree> result_chain = vNULL;
5601 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5602 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5603 vectors, that are ready for vector computation. */
5604 result_chain.create (size);
5606 /* If reassociation width for vector type is 2 or greater target machine can
5607 execute 2 or more vector instructions in parallel. Otherwise try to
5608 get chain for loads group using vect_shift_permute_load_chain. */
5609 mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
5610 if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
5611 || exact_log2 (size) != -1
5612 || !vect_shift_permute_load_chain (dr_chain, size, stmt,
5613 gsi, &result_chain))
5614 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5615 vect_record_grouped_load_vectors (stmt, result_chain);
5616 result_chain.release ();
5619 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5620 generated as part of the vectorization of STMT. Assign the statement
5621 for each vector to the associated scalar statement. */
5623 void
5624 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
5626 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5627 gimple next_stmt, new_stmt;
5628 unsigned int i, gap_count;
5629 tree tmp_data_ref;
5631 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5632 Since we scan the chain starting from it's first node, their order
5633 corresponds the order of data-refs in RESULT_CHAIN. */
5634 next_stmt = first_stmt;
5635 gap_count = 1;
5636 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5638 if (!next_stmt)
5639 break;
5641 /* Skip the gaps. Loads created for the gaps will be removed by dead
5642 code elimination pass later. No need to check for the first stmt in
5643 the group, since it always exists.
5644 GROUP_GAP is the number of steps in elements from the previous
5645 access (if there is no gap GROUP_GAP is 1). We skip loads that
5646 correspond to the gaps. */
5647 if (next_stmt != first_stmt
5648 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5650 gap_count++;
5651 continue;
5654 while (next_stmt)
5656 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5657 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5658 copies, and we put the new vector statement in the first available
5659 RELATED_STMT. */
5660 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5661 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5662 else
5664 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5666 gimple prev_stmt =
5667 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5668 gimple rel_stmt =
5669 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5670 while (rel_stmt)
5672 prev_stmt = rel_stmt;
5673 rel_stmt =
5674 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5677 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5678 new_stmt;
5682 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5683 gap_count = 1;
5684 /* If NEXT_STMT accesses the same DR as the previous statement,
5685 put the same TMP_DATA_REF as its vectorized statement; otherwise
5686 get the next data-ref from RESULT_CHAIN. */
5687 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5688 break;
5693 /* Function vect_force_dr_alignment_p.
5695 Returns whether the alignment of a DECL can be forced to be aligned
5696 on ALIGNMENT bit boundary. */
5698 bool
5699 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5701 if (TREE_CODE (decl) != VAR_DECL)
5702 return false;
5704 if (decl_in_symtab_p (decl)
5705 && !symtab_node::get (decl)->can_increase_alignment_p ())
5706 return false;
5708 if (TREE_STATIC (decl))
5709 return (alignment <= MAX_OFILE_ALIGNMENT);
5710 else
5711 return (alignment <= MAX_STACK_ALIGNMENT);
5715 /* Return whether the data reference DR is supported with respect to its
5716 alignment.
5717 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5718 it is aligned, i.e., check if it is possible to vectorize it with different
5719 alignment. */
5721 enum dr_alignment_support
5722 vect_supportable_dr_alignment (struct data_reference *dr,
5723 bool check_aligned_accesses)
5725 gimple stmt = DR_STMT (dr);
5726 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5727 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5728 machine_mode mode = TYPE_MODE (vectype);
5729 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5730 struct loop *vect_loop = NULL;
5731 bool nested_in_vect_loop = false;
5733 if (aligned_access_p (dr) && !check_aligned_accesses)
5734 return dr_aligned;
5736 /* For now assume all conditional loads/stores support unaligned
5737 access without any special code. */
5738 if (is_gimple_call (stmt)
5739 && gimple_call_internal_p (stmt)
5740 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
5741 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
5742 return dr_unaligned_supported;
5744 if (loop_vinfo)
5746 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5747 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5750 /* Possibly unaligned access. */
5752 /* We can choose between using the implicit realignment scheme (generating
5753 a misaligned_move stmt) and the explicit realignment scheme (generating
5754 aligned loads with a REALIGN_LOAD). There are two variants to the
5755 explicit realignment scheme: optimized, and unoptimized.
5756 We can optimize the realignment only if the step between consecutive
5757 vector loads is equal to the vector size. Since the vector memory
5758 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5759 is guaranteed that the misalignment amount remains the same throughout the
5760 execution of the vectorized loop. Therefore, we can create the
5761 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5762 at the loop preheader.
5764 However, in the case of outer-loop vectorization, when vectorizing a
5765 memory access in the inner-loop nested within the LOOP that is now being
5766 vectorized, while it is guaranteed that the misalignment of the
5767 vectorized memory access will remain the same in different outer-loop
5768 iterations, it is *not* guaranteed that is will remain the same throughout
5769 the execution of the inner-loop. This is because the inner-loop advances
5770 with the original scalar step (and not in steps of VS). If the inner-loop
5771 step happens to be a multiple of VS, then the misalignment remains fixed
5772 and we can use the optimized realignment scheme. For example:
5774 for (i=0; i<N; i++)
5775 for (j=0; j<M; j++)
5776 s += a[i+j];
5778 When vectorizing the i-loop in the above example, the step between
5779 consecutive vector loads is 1, and so the misalignment does not remain
5780 fixed across the execution of the inner-loop, and the realignment cannot
5781 be optimized (as illustrated in the following pseudo vectorized loop):
5783 for (i=0; i<N; i+=4)
5784 for (j=0; j<M; j++){
5785 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5786 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5787 // (assuming that we start from an aligned address).
5790 We therefore have to use the unoptimized realignment scheme:
5792 for (i=0; i<N; i+=4)
5793 for (j=k; j<M; j+=4)
5794 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5795 // that the misalignment of the initial address is
5796 // 0).
5798 The loop can then be vectorized as follows:
5800 for (k=0; k<4; k++){
5801 rt = get_realignment_token (&vp[k]);
5802 for (i=0; i<N; i+=4){
5803 v1 = vp[i+k];
5804 for (j=k; j<M; j+=4){
5805 v2 = vp[i+j+VS-1];
5806 va = REALIGN_LOAD <v1,v2,rt>;
5807 vs += va;
5808 v1 = v2;
5811 } */
5813 if (DR_IS_READ (dr))
5815 bool is_packed = false;
5816 tree type = (TREE_TYPE (DR_REF (dr)));
5818 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5819 && (!targetm.vectorize.builtin_mask_for_load
5820 || targetm.vectorize.builtin_mask_for_load ()))
5822 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5823 if ((nested_in_vect_loop
5824 && (TREE_INT_CST_LOW (DR_STEP (dr))
5825 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5826 || !loop_vinfo)
5827 return dr_explicit_realign;
5828 else
5829 return dr_explicit_realign_optimized;
5831 if (!known_alignment_for_access_p (dr))
5832 is_packed = not_size_aligned (DR_REF (dr));
5834 if ((TYPE_USER_ALIGN (type) && !is_packed)
5835 || targetm.vectorize.
5836 support_vector_misalignment (mode, type,
5837 DR_MISALIGNMENT (dr), is_packed))
5838 /* Can't software pipeline the loads, but can at least do them. */
5839 return dr_unaligned_supported;
5841 else
5843 bool is_packed = false;
5844 tree type = (TREE_TYPE (DR_REF (dr)));
5846 if (!known_alignment_for_access_p (dr))
5847 is_packed = not_size_aligned (DR_REF (dr));
5849 if ((TYPE_USER_ALIGN (type) && !is_packed)
5850 || targetm.vectorize.
5851 support_vector_misalignment (mode, type,
5852 DR_MISALIGNMENT (dr), is_packed))
5853 return dr_unaligned_supported;
5856 /* Unsupported. */
5857 return dr_unaligned_unsupported;