* c-c++-common/ubsan/float-cast-overflow-6.c: Add i?86-*-* target.
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob0bc03561cb00f45793ee60d6921bc664f7b3af18
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2014 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 "tree.h"
28 #include "stor-layout.h"
29 #include "tm_p.h"
30 #include "target.h"
31 #include "predict.h"
32 #include "vec.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "dominance.h"
40 #include "cfg.h"
41 #include "basic-block.h"
42 #include "gimple-pretty-print.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "tree-eh.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimplify.h"
50 #include "gimple-iterator.h"
51 #include "gimplify-me.h"
52 #include "gimple-ssa.h"
53 #include "tree-phinodes.h"
54 #include "ssa-iterators.h"
55 #include "stringpool.h"
56 #include "tree-ssanames.h"
57 #include "tree-ssa-loop-ivopts.h"
58 #include "tree-ssa-loop-manip.h"
59 #include "tree-ssa-loop.h"
60 #include "dumpfile.h"
61 #include "cfgloop.h"
62 #include "tree-chrec.h"
63 #include "tree-scalar-evolution.h"
64 #include "tree-vectorizer.h"
65 #include "diagnostic-core.h"
66 #include "hash-map.h"
67 #include "plugin-api.h"
68 #include "ipa-ref.h"
69 #include "cgraph.h"
70 /* Need to include rtl.h, expr.h, etc. for optabs. */
71 #include "expr.h"
72 #include "insn-codes.h"
73 #include "optabs.h"
74 #include "builtins.h"
75 #include "varasm.h"
77 /* Return true if load- or store-lanes optab OPTAB is implemented for
78 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
80 static bool
81 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
82 tree vectype, unsigned HOST_WIDE_INT count)
84 machine_mode mode, array_mode;
85 bool limit_p;
87 mode = TYPE_MODE (vectype);
88 limit_p = !targetm.array_mode_supported_p (mode, count);
89 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
90 MODE_INT, limit_p);
92 if (array_mode == BLKmode)
94 if (dump_enabled_p ())
95 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
96 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
97 GET_MODE_NAME (mode), count);
98 return false;
101 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
103 if (dump_enabled_p ())
104 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
105 "cannot use %s<%s><%s>\n", name,
106 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
107 return false;
110 if (dump_enabled_p ())
111 dump_printf_loc (MSG_NOTE, vect_location,
112 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
113 GET_MODE_NAME (mode));
115 return true;
119 /* Return the smallest scalar part of STMT.
120 This is used to determine the vectype of the stmt. We generally set the
121 vectype according to the type of the result (lhs). For stmts whose
122 result-type is different than the type of the arguments (e.g., demotion,
123 promotion), vectype will be reset appropriately (later). Note that we have
124 to visit the smallest datatype in this function, because that determines the
125 VF. If the smallest datatype in the loop is present only as the rhs of a
126 promotion operation - we'd miss it.
127 Such a case, where a variable of this datatype does not appear in the lhs
128 anywhere in the loop, can only occur if it's an invariant: e.g.:
129 'int_x = (int) short_inv', which we'd expect to have been optimized away by
130 invariant motion. However, we cannot rely on invariant motion to always
131 take invariants out of the loop, and so in the case of promotion we also
132 have to check the rhs.
133 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
134 types. */
136 tree
137 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
138 HOST_WIDE_INT *rhs_size_unit)
140 tree scalar_type = gimple_expr_type (stmt);
141 HOST_WIDE_INT lhs, rhs;
143 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
145 if (is_gimple_assign (stmt)
146 && (gimple_assign_cast_p (stmt)
147 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
148 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
149 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
151 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
153 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
154 if (rhs < lhs)
155 scalar_type = rhs_type;
158 *lhs_size_unit = lhs;
159 *rhs_size_unit = rhs;
160 return scalar_type;
164 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
165 tested at run-time. Return TRUE if DDR was successfully inserted.
166 Return false if versioning is not supported. */
168 static bool
169 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
171 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
173 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
174 return false;
176 if (dump_enabled_p ())
178 dump_printf_loc (MSG_NOTE, vect_location,
179 "mark for run-time aliasing test between ");
180 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
181 dump_printf (MSG_NOTE, " and ");
182 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
183 dump_printf (MSG_NOTE, "\n");
186 if (optimize_loop_nest_for_size_p (loop))
188 if (dump_enabled_p ())
189 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
190 "versioning not supported when optimizing"
191 " for size.\n");
192 return false;
195 /* FORNOW: We don't support versioning with outer-loop vectorization. */
196 if (loop->inner)
198 if (dump_enabled_p ())
199 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
200 "versioning not yet supported for outer-loops.\n");
201 return false;
204 /* FORNOW: We don't support creating runtime alias tests for non-constant
205 step. */
206 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
207 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
209 if (dump_enabled_p ())
210 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
211 "versioning not yet supported for non-constant "
212 "step\n");
213 return false;
216 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
217 return true;
221 /* Function vect_analyze_data_ref_dependence.
223 Return TRUE if there (might) exist a dependence between a memory-reference
224 DRA and a memory-reference DRB. When versioning for alias may check a
225 dependence at run-time, return FALSE. Adjust *MAX_VF according to
226 the data dependence. */
228 static bool
229 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
230 loop_vec_info loop_vinfo, int *max_vf)
232 unsigned int i;
233 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
234 struct data_reference *dra = DDR_A (ddr);
235 struct data_reference *drb = DDR_B (ddr);
236 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
237 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
238 lambda_vector dist_v;
239 unsigned int loop_depth;
241 /* In loop analysis all data references should be vectorizable. */
242 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
243 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
244 gcc_unreachable ();
246 /* Independent data accesses. */
247 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
248 return false;
250 if (dra == drb
251 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
252 return false;
254 /* Even if we have an anti-dependence then, as the vectorized loop covers at
255 least two scalar iterations, there is always also a true dependence.
256 As the vectorizer does not re-order loads and stores we can ignore
257 the anti-dependence if TBAA can disambiguate both DRs similar to the
258 case with known negative distance anti-dependences (positive
259 distance anti-dependences would violate TBAA constraints). */
260 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
261 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
262 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
263 get_alias_set (DR_REF (drb))))
264 return false;
266 /* Unknown data dependence. */
267 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
269 /* If user asserted safelen consecutive iterations can be
270 executed concurrently, assume independence. */
271 if (loop->safelen >= 2)
273 if (loop->safelen < *max_vf)
274 *max_vf = loop->safelen;
275 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
276 return false;
279 if (STMT_VINFO_GATHER_P (stmtinfo_a)
280 || STMT_VINFO_GATHER_P (stmtinfo_b))
282 if (dump_enabled_p ())
284 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
285 "versioning for alias not supported for: "
286 "can't determine dependence between ");
287 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
288 DR_REF (dra));
289 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
290 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
291 DR_REF (drb));
292 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
294 return true;
297 if (dump_enabled_p ())
299 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
300 "versioning for alias required: "
301 "can't determine dependence between ");
302 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
303 DR_REF (dra));
304 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
305 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
306 DR_REF (drb));
307 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
310 /* Add to list of ddrs that need to be tested at run-time. */
311 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
314 /* Known data dependence. */
315 if (DDR_NUM_DIST_VECTS (ddr) == 0)
317 /* If user asserted safelen consecutive iterations can be
318 executed concurrently, assume independence. */
319 if (loop->safelen >= 2)
321 if (loop->safelen < *max_vf)
322 *max_vf = loop->safelen;
323 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
324 return false;
327 if (STMT_VINFO_GATHER_P (stmtinfo_a)
328 || STMT_VINFO_GATHER_P (stmtinfo_b))
330 if (dump_enabled_p ())
332 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
333 "versioning for alias not supported for: "
334 "bad dist vector for ");
335 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
336 DR_REF (dra));
337 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
338 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
339 DR_REF (drb));
340 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
342 return true;
345 if (dump_enabled_p ())
347 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
348 "versioning for alias required: "
349 "bad dist vector for ");
350 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
351 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
352 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
353 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
355 /* Add to list of ddrs that need to be tested at run-time. */
356 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
359 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
360 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
362 int dist = dist_v[loop_depth];
364 if (dump_enabled_p ())
365 dump_printf_loc (MSG_NOTE, vect_location,
366 "dependence distance = %d.\n", dist);
368 if (dist == 0)
370 if (dump_enabled_p ())
372 dump_printf_loc (MSG_NOTE, vect_location,
373 "dependence distance == 0 between ");
374 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
375 dump_printf (MSG_NOTE, " and ");
376 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
377 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
380 /* When we perform grouped accesses and perform implicit CSE
381 by detecting equal accesses and doing disambiguation with
382 runtime alias tests like for
383 .. = a[i];
384 .. = a[i+1];
385 a[i] = ..;
386 a[i+1] = ..;
387 *p = ..;
388 .. = a[i];
389 .. = a[i+1];
390 where we will end up loading { a[i], a[i+1] } once, make
391 sure that inserting group loads before the first load and
392 stores after the last store will do the right thing.
393 Similar for groups like
394 a[i] = ...;
395 ... = a[i];
396 a[i+1] = ...;
397 where loads from the group interleave with the store. */
398 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
399 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
401 gimple earlier_stmt;
402 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
403 if (DR_IS_WRITE
404 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
406 if (dump_enabled_p ())
407 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
408 "READ_WRITE dependence in interleaving."
409 "\n");
410 return true;
414 continue;
417 if (dist > 0 && DDR_REVERSED_P (ddr))
419 /* If DDR_REVERSED_P the order of the data-refs in DDR was
420 reversed (to make distance vector positive), and the actual
421 distance is negative. */
422 if (dump_enabled_p ())
423 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
424 "dependence distance negative.\n");
425 /* Record a negative dependence distance to later limit the
426 amount of stmt copying / unrolling we can perform.
427 Only need to handle read-after-write dependence. */
428 if (DR_IS_READ (drb)
429 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
430 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
431 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
432 continue;
435 if (abs (dist) >= 2
436 && abs (dist) < *max_vf)
438 /* The dependence distance requires reduction of the maximal
439 vectorization factor. */
440 *max_vf = abs (dist);
441 if (dump_enabled_p ())
442 dump_printf_loc (MSG_NOTE, vect_location,
443 "adjusting maximal vectorization factor to %i\n",
444 *max_vf);
447 if (abs (dist) >= *max_vf)
449 /* Dependence distance does not create dependence, as far as
450 vectorization is concerned, in this case. */
451 if (dump_enabled_p ())
452 dump_printf_loc (MSG_NOTE, vect_location,
453 "dependence distance >= VF.\n");
454 continue;
457 if (dump_enabled_p ())
459 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
460 "not vectorized, possible dependence "
461 "between data-refs ");
462 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
463 dump_printf (MSG_NOTE, " and ");
464 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
465 dump_printf (MSG_NOTE, "\n");
468 return true;
471 return false;
474 /* Function vect_analyze_data_ref_dependences.
476 Examine all the data references in the loop, and make sure there do not
477 exist any data dependences between them. Set *MAX_VF according to
478 the maximum vectorization factor the data dependences allow. */
480 bool
481 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
483 unsigned int i;
484 struct data_dependence_relation *ddr;
486 if (dump_enabled_p ())
487 dump_printf_loc (MSG_NOTE, vect_location,
488 "=== vect_analyze_data_ref_dependences ===\n");
490 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
491 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
492 &LOOP_VINFO_DDRS (loop_vinfo),
493 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
494 return false;
496 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
497 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
498 return false;
500 return true;
504 /* Function vect_slp_analyze_data_ref_dependence.
506 Return TRUE if there (might) exist a dependence between a memory-reference
507 DRA and a memory-reference DRB. When versioning for alias may check a
508 dependence at run-time, return FALSE. Adjust *MAX_VF according to
509 the data dependence. */
511 static bool
512 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
514 struct data_reference *dra = DDR_A (ddr);
515 struct data_reference *drb = DDR_B (ddr);
517 /* We need to check dependences of statements marked as unvectorizable
518 as well, they still can prohibit vectorization. */
520 /* Independent data accesses. */
521 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
522 return false;
524 if (dra == drb)
525 return false;
527 /* Read-read is OK. */
528 if (DR_IS_READ (dra) && DR_IS_READ (drb))
529 return false;
531 /* If dra and drb are part of the same interleaving chain consider
532 them independent. */
533 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
534 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
535 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
536 return false;
538 /* Unknown data dependence. */
539 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
541 if (dump_enabled_p ())
543 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
544 "can't determine dependence between ");
545 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
546 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
547 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
548 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
551 else if (dump_enabled_p ())
553 dump_printf_loc (MSG_NOTE, vect_location,
554 "determined dependence between ");
555 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
556 dump_printf (MSG_NOTE, " and ");
557 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
558 dump_printf (MSG_NOTE, "\n");
561 /* We do not vectorize basic blocks with write-write dependencies. */
562 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
563 return true;
565 /* If we have a read-write dependence check that the load is before the store.
566 When we vectorize basic blocks, vector load can be only before
567 corresponding scalar load, and vector store can be only after its
568 corresponding scalar store. So the order of the acceses is preserved in
569 case the load is before the store. */
570 gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
571 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
573 /* That only holds for load-store pairs taking part in vectorization. */
574 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
575 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
576 return false;
579 return true;
583 /* Function vect_analyze_data_ref_dependences.
585 Examine all the data references in the basic-block, and make sure there
586 do not exist any data dependences between them. Set *MAX_VF according to
587 the maximum vectorization factor the data dependences allow. */
589 bool
590 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
592 struct data_dependence_relation *ddr;
593 unsigned int i;
595 if (dump_enabled_p ())
596 dump_printf_loc (MSG_NOTE, vect_location,
597 "=== vect_slp_analyze_data_ref_dependences ===\n");
599 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
600 &BB_VINFO_DDRS (bb_vinfo),
601 vNULL, true))
602 return false;
604 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
605 if (vect_slp_analyze_data_ref_dependence (ddr))
606 return false;
608 return true;
612 /* Function vect_compute_data_ref_alignment
614 Compute the misalignment of the data reference DR.
616 Output:
617 1. If during the misalignment computation it is found that the data reference
618 cannot be vectorized then false is returned.
619 2. DR_MISALIGNMENT (DR) is defined.
621 FOR NOW: No analysis is actually performed. Misalignment is calculated
622 only for trivial cases. TODO. */
624 static bool
625 vect_compute_data_ref_alignment (struct data_reference *dr)
627 gimple stmt = DR_STMT (dr);
628 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
629 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
630 struct loop *loop = NULL;
631 tree ref = DR_REF (dr);
632 tree vectype;
633 tree base, base_addr;
634 bool base_aligned;
635 tree misalign;
636 tree aligned_to, alignment;
638 if (dump_enabled_p ())
639 dump_printf_loc (MSG_NOTE, vect_location,
640 "vect_compute_data_ref_alignment:\n");
642 if (loop_vinfo)
643 loop = LOOP_VINFO_LOOP (loop_vinfo);
645 /* Initialize misalignment to unknown. */
646 SET_DR_MISALIGNMENT (dr, -1);
648 /* Strided loads perform only component accesses, misalignment information
649 is irrelevant for them. */
650 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
651 return true;
653 misalign = DR_INIT (dr);
654 aligned_to = DR_ALIGNED_TO (dr);
655 base_addr = DR_BASE_ADDRESS (dr);
656 vectype = STMT_VINFO_VECTYPE (stmt_info);
658 /* In case the dataref is in an inner-loop of the loop that is being
659 vectorized (LOOP), we use the base and misalignment information
660 relative to the outer-loop (LOOP). This is ok only if the misalignment
661 stays the same throughout the execution of the inner-loop, which is why
662 we have to check that the stride of the dataref in the inner-loop evenly
663 divides by the vector size. */
664 if (loop && nested_in_vect_loop_p (loop, stmt))
666 tree step = DR_STEP (dr);
667 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
669 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
671 if (dump_enabled_p ())
672 dump_printf_loc (MSG_NOTE, vect_location,
673 "inner step divides the vector-size.\n");
674 misalign = STMT_VINFO_DR_INIT (stmt_info);
675 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
676 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
678 else
680 if (dump_enabled_p ())
681 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
682 "inner step doesn't divide the vector-size.\n");
683 misalign = NULL_TREE;
687 /* Similarly, if we're doing basic-block vectorization, we can only use
688 base and misalignment information relative to an innermost loop if the
689 misalignment stays the same throughout the execution of the loop.
690 As above, this is the case if the stride of the dataref evenly divides
691 by the vector size. */
692 if (!loop)
694 tree step = DR_STEP (dr);
695 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
697 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
699 if (dump_enabled_p ())
700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
701 "SLP: step doesn't divide the vector-size.\n");
702 misalign = NULL_TREE;
706 base = build_fold_indirect_ref (base_addr);
707 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
709 if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
710 || !misalign)
712 if (dump_enabled_p ())
714 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
715 "Unknown alignment for access: ");
716 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, base);
717 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
719 return true;
722 if ((DECL_P (base)
723 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
724 alignment) >= 0)
725 || (TREE_CODE (base_addr) == SSA_NAME
726 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
727 TREE_TYPE (base_addr)))),
728 alignment) >= 0)
729 || (get_pointer_alignment (base_addr) >= TYPE_ALIGN (vectype)))
730 base_aligned = true;
731 else
732 base_aligned = false;
734 if (!base_aligned)
736 /* Do not change the alignment of global variables here if
737 flag_section_anchors is enabled as we already generated
738 RTL for other functions. Most global variables should
739 have been aligned during the IPA increase_alignment pass. */
740 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
741 || (TREE_STATIC (base) && flag_section_anchors))
743 if (dump_enabled_p ())
745 dump_printf_loc (MSG_NOTE, vect_location,
746 "can't force alignment of ref: ");
747 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
748 dump_printf (MSG_NOTE, "\n");
750 return true;
753 /* Force the alignment of the decl.
754 NOTE: This is the only change to the code we make during
755 the analysis phase, before deciding to vectorize the loop. */
756 if (dump_enabled_p ())
758 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
759 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
760 dump_printf (MSG_NOTE, "\n");
763 ((dataref_aux *)dr->aux)->base_decl = base;
764 ((dataref_aux *)dr->aux)->base_misaligned = true;
767 /* If this is a backward running DR then first access in the larger
768 vectype actually is N-1 elements before the address in the DR.
769 Adjust misalign accordingly. */
770 if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
772 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
773 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
774 otherwise we wouldn't be here. */
775 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
776 /* PLUS because DR_STEP was negative. */
777 misalign = size_binop (PLUS_EXPR, misalign, offset);
780 /* Modulo alignment. */
781 misalign = size_binop (FLOOR_MOD_EXPR, misalign, alignment);
783 if (!tree_fits_uhwi_p (misalign))
785 /* Negative or overflowed misalignment value. */
786 if (dump_enabled_p ())
787 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
788 "unexpected misalign value\n");
789 return false;
792 SET_DR_MISALIGNMENT (dr, tree_to_uhwi (misalign));
794 if (dump_enabled_p ())
796 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
797 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
798 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
799 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
802 return true;
806 /* Function vect_compute_data_refs_alignment
808 Compute the misalignment of data references in the loop.
809 Return FALSE if a data reference is found that cannot be vectorized. */
811 static bool
812 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
813 bb_vec_info bb_vinfo)
815 vec<data_reference_p> datarefs;
816 struct data_reference *dr;
817 unsigned int i;
819 if (loop_vinfo)
820 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
821 else
822 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
824 FOR_EACH_VEC_ELT (datarefs, i, dr)
825 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
826 && !vect_compute_data_ref_alignment (dr))
828 if (bb_vinfo)
830 /* Mark unsupported statement as unvectorizable. */
831 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
832 continue;
834 else
835 return false;
838 return true;
842 /* Function vect_update_misalignment_for_peel
844 DR - the data reference whose misalignment is to be adjusted.
845 DR_PEEL - the data reference whose misalignment is being made
846 zero in the vector loop by the peel.
847 NPEEL - the number of iterations in the peel loop if the misalignment
848 of DR_PEEL is known at compile time. */
850 static void
851 vect_update_misalignment_for_peel (struct data_reference *dr,
852 struct data_reference *dr_peel, int npeel)
854 unsigned int i;
855 vec<dr_p> same_align_drs;
856 struct data_reference *current_dr;
857 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
858 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
859 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
860 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
862 /* For interleaved data accesses the step in the loop must be multiplied by
863 the size of the interleaving group. */
864 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
865 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
866 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
867 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
869 /* It can be assumed that the data refs with the same alignment as dr_peel
870 are aligned in the vector loop. */
871 same_align_drs
872 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
873 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
875 if (current_dr != dr)
876 continue;
877 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
878 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
879 SET_DR_MISALIGNMENT (dr, 0);
880 return;
883 if (known_alignment_for_access_p (dr)
884 && known_alignment_for_access_p (dr_peel))
886 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
887 int misal = DR_MISALIGNMENT (dr);
888 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
889 misal += negative ? -npeel * dr_size : npeel * dr_size;
890 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
891 SET_DR_MISALIGNMENT (dr, misal);
892 return;
895 if (dump_enabled_p ())
896 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
897 SET_DR_MISALIGNMENT (dr, -1);
901 /* Function vect_verify_datarefs_alignment
903 Return TRUE if all data references in the loop can be
904 handled with respect to alignment. */
906 bool
907 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
909 vec<data_reference_p> datarefs;
910 struct data_reference *dr;
911 enum dr_alignment_support supportable_dr_alignment;
912 unsigned int i;
914 if (loop_vinfo)
915 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
916 else
917 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
919 FOR_EACH_VEC_ELT (datarefs, i, dr)
921 gimple stmt = DR_STMT (dr);
922 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
924 if (!STMT_VINFO_RELEVANT_P (stmt_info))
925 continue;
927 /* For interleaving, only the alignment of the first access matters.
928 Skip statements marked as not vectorizable. */
929 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
930 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
931 || !STMT_VINFO_VECTORIZABLE (stmt_info))
932 continue;
934 /* Strided loads perform only component accesses, alignment is
935 irrelevant for them. */
936 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
937 continue;
939 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
940 if (!supportable_dr_alignment)
942 if (dump_enabled_p ())
944 if (DR_IS_READ (dr))
945 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
946 "not vectorized: unsupported unaligned load.");
947 else
948 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
949 "not vectorized: unsupported unaligned "
950 "store.");
952 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
953 DR_REF (dr));
954 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
956 return false;
958 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
959 dump_printf_loc (MSG_NOTE, vect_location,
960 "Vectorizing an unaligned access.\n");
962 return true;
965 /* Given an memory reference EXP return whether its alignment is less
966 than its size. */
968 static bool
969 not_size_aligned (tree exp)
971 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
972 return true;
974 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
975 > get_object_alignment (exp));
978 /* Function vector_alignment_reachable_p
980 Return true if vector alignment for DR is reachable by peeling
981 a few loop iterations. Return false otherwise. */
983 static bool
984 vector_alignment_reachable_p (struct data_reference *dr)
986 gimple stmt = DR_STMT (dr);
987 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
988 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
990 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
992 /* For interleaved access we peel only if number of iterations in
993 the prolog loop ({VF - misalignment}), is a multiple of the
994 number of the interleaved accesses. */
995 int elem_size, mis_in_elements;
996 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
998 /* FORNOW: handle only known alignment. */
999 if (!known_alignment_for_access_p (dr))
1000 return false;
1002 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
1003 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1005 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
1006 return false;
1009 /* If misalignment is known at the compile time then allow peeling
1010 only if natural alignment is reachable through peeling. */
1011 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1013 HOST_WIDE_INT elmsize =
1014 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1015 if (dump_enabled_p ())
1017 dump_printf_loc (MSG_NOTE, vect_location,
1018 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1019 dump_printf (MSG_NOTE,
1020 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1022 if (DR_MISALIGNMENT (dr) % elmsize)
1024 if (dump_enabled_p ())
1025 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1026 "data size does not divide the misalignment.\n");
1027 return false;
1031 if (!known_alignment_for_access_p (dr))
1033 tree type = TREE_TYPE (DR_REF (dr));
1034 bool is_packed = not_size_aligned (DR_REF (dr));
1035 if (dump_enabled_p ())
1036 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1037 "Unknown misalignment, is_packed = %d\n",is_packed);
1038 if ((TYPE_USER_ALIGN (type) && !is_packed)
1039 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1040 return true;
1041 else
1042 return false;
1045 return true;
1049 /* Calculate the cost of the memory access represented by DR. */
1051 static void
1052 vect_get_data_access_cost (struct data_reference *dr,
1053 unsigned int *inside_cost,
1054 unsigned int *outside_cost,
1055 stmt_vector_for_cost *body_cost_vec)
1057 gimple stmt = DR_STMT (dr);
1058 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1059 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1060 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1061 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1062 int ncopies = vf / nunits;
1064 if (DR_IS_READ (dr))
1065 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1066 NULL, body_cost_vec, false);
1067 else
1068 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1070 if (dump_enabled_p ())
1071 dump_printf_loc (MSG_NOTE, vect_location,
1072 "vect_get_data_access_cost: inside_cost = %d, "
1073 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1077 /* Insert DR into peeling hash table with NPEEL as key. */
1079 static void
1080 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1081 int npeel)
1083 struct _vect_peel_info elem, *slot;
1084 _vect_peel_info **new_slot;
1085 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1087 elem.npeel = npeel;
1088 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find (&elem);
1089 if (slot)
1090 slot->count++;
1091 else
1093 slot = XNEW (struct _vect_peel_info);
1094 slot->npeel = npeel;
1095 slot->dr = dr;
1096 slot->count = 1;
1097 new_slot
1098 = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find_slot (slot, INSERT);
1099 *new_slot = slot;
1102 if (!supportable_dr_alignment
1103 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1104 slot->count += VECT_MAX_COST;
1108 /* Traverse peeling hash table to find peeling option that aligns maximum
1109 number of data accesses. */
1112 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1113 _vect_peel_extended_info *max)
1115 vect_peel_info elem = *slot;
1117 if (elem->count > max->peel_info.count
1118 || (elem->count == max->peel_info.count
1119 && max->peel_info.npeel > elem->npeel))
1121 max->peel_info.npeel = elem->npeel;
1122 max->peel_info.count = elem->count;
1123 max->peel_info.dr = elem->dr;
1126 return 1;
1130 /* Traverse peeling hash table and calculate cost for each peeling option.
1131 Find the one with the lowest cost. */
1134 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1135 _vect_peel_extended_info *min)
1137 vect_peel_info elem = *slot;
1138 int save_misalignment, dummy;
1139 unsigned int inside_cost = 0, outside_cost = 0, i;
1140 gimple stmt = DR_STMT (elem->dr);
1141 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1142 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1143 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1144 struct data_reference *dr;
1145 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1146 int single_iter_cost;
1148 prologue_cost_vec.create (2);
1149 body_cost_vec.create (2);
1150 epilogue_cost_vec.create (2);
1152 FOR_EACH_VEC_ELT (datarefs, i, dr)
1154 stmt = DR_STMT (dr);
1155 stmt_info = vinfo_for_stmt (stmt);
1156 /* For interleaving, only the alignment of the first access
1157 matters. */
1158 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1159 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1160 continue;
1162 save_misalignment = DR_MISALIGNMENT (dr);
1163 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1164 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1165 &body_cost_vec);
1166 SET_DR_MISALIGNMENT (dr, save_misalignment);
1169 single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
1170 outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel,
1171 &dummy, single_iter_cost,
1172 &prologue_cost_vec,
1173 &epilogue_cost_vec);
1175 /* Prologue and epilogue costs are added to the target model later.
1176 These costs depend only on the scalar iteration cost, the
1177 number of peeling iterations finally chosen, and the number of
1178 misaligned statements. So discard the information found here. */
1179 prologue_cost_vec.release ();
1180 epilogue_cost_vec.release ();
1182 if (inside_cost < min->inside_cost
1183 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1185 min->inside_cost = inside_cost;
1186 min->outside_cost = outside_cost;
1187 min->body_cost_vec.release ();
1188 min->body_cost_vec = body_cost_vec;
1189 min->peel_info.dr = elem->dr;
1190 min->peel_info.npeel = elem->npeel;
1192 else
1193 body_cost_vec.release ();
1195 return 1;
1199 /* Choose best peeling option by traversing peeling hash table and either
1200 choosing an option with the lowest cost (if cost model is enabled) or the
1201 option that aligns as many accesses as possible. */
1203 static struct data_reference *
1204 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1205 unsigned int *npeel,
1206 stmt_vector_for_cost *body_cost_vec)
1208 struct _vect_peel_extended_info res;
1210 res.peel_info.dr = NULL;
1211 res.body_cost_vec = stmt_vector_for_cost ();
1213 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1215 res.inside_cost = INT_MAX;
1216 res.outside_cost = INT_MAX;
1217 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1218 ->traverse <_vect_peel_extended_info *,
1219 vect_peeling_hash_get_lowest_cost> (&res);
1221 else
1223 res.peel_info.count = 0;
1224 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1225 ->traverse <_vect_peel_extended_info *,
1226 vect_peeling_hash_get_most_frequent> (&res);
1229 *npeel = res.peel_info.npeel;
1230 *body_cost_vec = res.body_cost_vec;
1231 return res.peel_info.dr;
1235 /* Function vect_enhance_data_refs_alignment
1237 This pass will use loop versioning and loop peeling in order to enhance
1238 the alignment of data references in the loop.
1240 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1241 original loop is to be vectorized. Any other loops that are created by
1242 the transformations performed in this pass - are not supposed to be
1243 vectorized. This restriction will be relaxed.
1245 This pass will require a cost model to guide it whether to apply peeling
1246 or versioning or a combination of the two. For example, the scheme that
1247 intel uses when given a loop with several memory accesses, is as follows:
1248 choose one memory access ('p') which alignment you want to force by doing
1249 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1250 other accesses are not necessarily aligned, or (2) use loop versioning to
1251 generate one loop in which all accesses are aligned, and another loop in
1252 which only 'p' is necessarily aligned.
1254 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1255 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1256 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1258 Devising a cost model is the most critical aspect of this work. It will
1259 guide us on which access to peel for, whether to use loop versioning, how
1260 many versions to create, etc. The cost model will probably consist of
1261 generic considerations as well as target specific considerations (on
1262 powerpc for example, misaligned stores are more painful than misaligned
1263 loads).
1265 Here are the general steps involved in alignment enhancements:
1267 -- original loop, before alignment analysis:
1268 for (i=0; i<N; i++){
1269 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1270 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1273 -- After vect_compute_data_refs_alignment:
1274 for (i=0; i<N; i++){
1275 x = q[i]; # DR_MISALIGNMENT(q) = 3
1276 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1279 -- Possibility 1: we do loop versioning:
1280 if (p is aligned) {
1281 for (i=0; i<N; i++){ # loop 1A
1282 x = q[i]; # DR_MISALIGNMENT(q) = 3
1283 p[i] = y; # DR_MISALIGNMENT(p) = 0
1286 else {
1287 for (i=0; i<N; i++){ # loop 1B
1288 x = q[i]; # DR_MISALIGNMENT(q) = 3
1289 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1293 -- Possibility 2: we do loop peeling:
1294 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1295 x = q[i];
1296 p[i] = y;
1298 for (i = 3; i < N; i++){ # loop 2A
1299 x = q[i]; # DR_MISALIGNMENT(q) = 0
1300 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1303 -- Possibility 3: combination of loop peeling and versioning:
1304 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1305 x = q[i];
1306 p[i] = y;
1308 if (p is aligned) {
1309 for (i = 3; i<N; i++){ # loop 3A
1310 x = q[i]; # DR_MISALIGNMENT(q) = 0
1311 p[i] = y; # DR_MISALIGNMENT(p) = 0
1314 else {
1315 for (i = 3; i<N; i++){ # loop 3B
1316 x = q[i]; # DR_MISALIGNMENT(q) = 0
1317 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1321 These loops are later passed to loop_transform to be vectorized. The
1322 vectorizer will use the alignment information to guide the transformation
1323 (whether to generate regular loads/stores, or with special handling for
1324 misalignment). */
1326 bool
1327 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1329 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1330 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1331 enum dr_alignment_support supportable_dr_alignment;
1332 struct data_reference *dr0 = NULL, *first_store = NULL;
1333 struct data_reference *dr;
1334 unsigned int i, j;
1335 bool do_peeling = false;
1336 bool do_versioning = false;
1337 bool stat;
1338 gimple stmt;
1339 stmt_vec_info stmt_info;
1340 unsigned int npeel = 0;
1341 bool all_misalignments_unknown = true;
1342 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1343 unsigned possible_npeel_number = 1;
1344 tree vectype;
1345 unsigned int nelements, mis, same_align_drs_max = 0;
1346 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1348 if (dump_enabled_p ())
1349 dump_printf_loc (MSG_NOTE, vect_location,
1350 "=== vect_enhance_data_refs_alignment ===\n");
1352 /* While cost model enhancements are expected in the future, the high level
1353 view of the code at this time is as follows:
1355 A) If there is a misaligned access then see if peeling to align
1356 this access can make all data references satisfy
1357 vect_supportable_dr_alignment. If so, update data structures
1358 as needed and return true.
1360 B) If peeling wasn't possible and there is a data reference with an
1361 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1362 then see if loop versioning checks can be used to make all data
1363 references satisfy vect_supportable_dr_alignment. If so, update
1364 data structures as needed and return true.
1366 C) If neither peeling nor versioning were successful then return false if
1367 any data reference does not satisfy vect_supportable_dr_alignment.
1369 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1371 Note, Possibility 3 above (which is peeling and versioning together) is not
1372 being done at this time. */
1374 /* (1) Peeling to force alignment. */
1376 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1377 Considerations:
1378 + How many accesses will become aligned due to the peeling
1379 - How many accesses will become unaligned due to the peeling,
1380 and the cost of misaligned accesses.
1381 - The cost of peeling (the extra runtime checks, the increase
1382 in code size). */
1384 FOR_EACH_VEC_ELT (datarefs, i, dr)
1386 stmt = DR_STMT (dr);
1387 stmt_info = vinfo_for_stmt (stmt);
1389 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1390 continue;
1392 /* For interleaving, only the alignment of the first access
1393 matters. */
1394 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1395 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1396 continue;
1398 /* For invariant accesses there is nothing to enhance. */
1399 if (integer_zerop (DR_STEP (dr)))
1400 continue;
1402 /* Strided loads perform only component accesses, alignment is
1403 irrelevant for them. */
1404 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1405 continue;
1407 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1408 do_peeling = vector_alignment_reachable_p (dr);
1409 if (do_peeling)
1411 if (known_alignment_for_access_p (dr))
1413 unsigned int npeel_tmp;
1414 bool negative = tree_int_cst_compare (DR_STEP (dr),
1415 size_zero_node) < 0;
1417 /* Save info about DR in the hash table. */
1418 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo))
1419 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1420 = new hash_table<peel_info_hasher> (1);
1422 vectype = STMT_VINFO_VECTYPE (stmt_info);
1423 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1424 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1425 TREE_TYPE (DR_REF (dr))));
1426 npeel_tmp = (negative
1427 ? (mis - nelements) : (nelements - mis))
1428 & (nelements - 1);
1430 /* For multiple types, it is possible that the bigger type access
1431 will have more than one peeling option. E.g., a loop with two
1432 types: one of size (vector size / 4), and the other one of
1433 size (vector size / 8). Vectorization factor will 8. If both
1434 access are misaligned by 3, the first one needs one scalar
1435 iteration to be aligned, and the second one needs 5. But the
1436 the first one will be aligned also by peeling 5 scalar
1437 iterations, and in that case both accesses will be aligned.
1438 Hence, except for the immediate peeling amount, we also want
1439 to try to add full vector size, while we don't exceed
1440 vectorization factor.
1441 We do this automtically for cost model, since we calculate cost
1442 for every peeling option. */
1443 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1444 possible_npeel_number = vf /nelements;
1446 /* Handle the aligned case. We may decide to align some other
1447 access, making DR unaligned. */
1448 if (DR_MISALIGNMENT (dr) == 0)
1450 npeel_tmp = 0;
1451 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1452 possible_npeel_number++;
1455 for (j = 0; j < possible_npeel_number; j++)
1457 gcc_assert (npeel_tmp <= vf);
1458 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1459 npeel_tmp += nelements;
1462 all_misalignments_unknown = false;
1463 /* Data-ref that was chosen for the case that all the
1464 misalignments are unknown is not relevant anymore, since we
1465 have a data-ref with known alignment. */
1466 dr0 = NULL;
1468 else
1470 /* If we don't know any misalignment values, we prefer
1471 peeling for data-ref that has the maximum number of data-refs
1472 with the same alignment, unless the target prefers to align
1473 stores over load. */
1474 if (all_misalignments_unknown)
1476 unsigned same_align_drs
1477 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1478 if (!dr0
1479 || same_align_drs_max < same_align_drs)
1481 same_align_drs_max = same_align_drs;
1482 dr0 = dr;
1484 /* For data-refs with the same number of related
1485 accesses prefer the one where the misalign
1486 computation will be invariant in the outermost loop. */
1487 else if (same_align_drs_max == same_align_drs)
1489 struct loop *ivloop0, *ivloop;
1490 ivloop0 = outermost_invariant_loop_for_expr
1491 (loop, DR_BASE_ADDRESS (dr0));
1492 ivloop = outermost_invariant_loop_for_expr
1493 (loop, DR_BASE_ADDRESS (dr));
1494 if ((ivloop && !ivloop0)
1495 || (ivloop && ivloop0
1496 && flow_loop_nested_p (ivloop, ivloop0)))
1497 dr0 = dr;
1500 if (!first_store && DR_IS_WRITE (dr))
1501 first_store = dr;
1504 /* If there are both known and unknown misaligned accesses in the
1505 loop, we choose peeling amount according to the known
1506 accesses. */
1507 if (!supportable_dr_alignment)
1509 dr0 = dr;
1510 if (!first_store && DR_IS_WRITE (dr))
1511 first_store = dr;
1515 else
1517 if (!aligned_access_p (dr))
1519 if (dump_enabled_p ())
1520 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1521 "vector alignment may not be reachable\n");
1522 break;
1527 /* Check if we can possibly peel the loop. */
1528 if (!vect_can_advance_ivs_p (loop_vinfo)
1529 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1530 do_peeling = false;
1532 /* If we don't know how many times the peeling loop will run
1533 assume it will run VF-1 times and disable peeling if the remaining
1534 iters are less than the vectorization factor. */
1535 if (do_peeling
1536 && all_misalignments_unknown
1537 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1538 && (LOOP_VINFO_INT_NITERS (loop_vinfo)
1539 < 2 * (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1))
1540 do_peeling = false;
1542 if (do_peeling
1543 && all_misalignments_unknown
1544 && vect_supportable_dr_alignment (dr0, false))
1546 /* Check if the target requires to prefer stores over loads, i.e., if
1547 misaligned stores are more expensive than misaligned loads (taking
1548 drs with same alignment into account). */
1549 if (first_store && DR_IS_READ (dr0))
1551 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1552 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1553 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1554 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1555 stmt_vector_for_cost dummy;
1556 dummy.create (2);
1558 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1559 &dummy);
1560 vect_get_data_access_cost (first_store, &store_inside_cost,
1561 &store_outside_cost, &dummy);
1563 dummy.release ();
1565 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1566 aligning the load DR0). */
1567 load_inside_penalty = store_inside_cost;
1568 load_outside_penalty = store_outside_cost;
1569 for (i = 0;
1570 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1571 DR_STMT (first_store))).iterate (i, &dr);
1572 i++)
1573 if (DR_IS_READ (dr))
1575 load_inside_penalty += load_inside_cost;
1576 load_outside_penalty += load_outside_cost;
1578 else
1580 load_inside_penalty += store_inside_cost;
1581 load_outside_penalty += store_outside_cost;
1584 /* Calculate the penalty for leaving DR0 unaligned (by
1585 aligning the FIRST_STORE). */
1586 store_inside_penalty = load_inside_cost;
1587 store_outside_penalty = load_outside_cost;
1588 for (i = 0;
1589 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1590 DR_STMT (dr0))).iterate (i, &dr);
1591 i++)
1592 if (DR_IS_READ (dr))
1594 store_inside_penalty += load_inside_cost;
1595 store_outside_penalty += load_outside_cost;
1597 else
1599 store_inside_penalty += store_inside_cost;
1600 store_outside_penalty += store_outside_cost;
1603 if (load_inside_penalty > store_inside_penalty
1604 || (load_inside_penalty == store_inside_penalty
1605 && load_outside_penalty > store_outside_penalty))
1606 dr0 = first_store;
1609 /* In case there are only loads with different unknown misalignments, use
1610 peeling only if it may help to align other accesses in the loop. */
1611 if (!first_store
1612 && !STMT_VINFO_SAME_ALIGN_REFS (
1613 vinfo_for_stmt (DR_STMT (dr0))).length ()
1614 && vect_supportable_dr_alignment (dr0, false)
1615 != dr_unaligned_supported)
1616 do_peeling = false;
1619 if (do_peeling && !dr0)
1621 /* Peeling is possible, but there is no data access that is not supported
1622 unless aligned. So we try to choose the best possible peeling. */
1624 /* We should get here only if there are drs with known misalignment. */
1625 gcc_assert (!all_misalignments_unknown);
1627 /* Choose the best peeling from the hash table. */
1628 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1629 &body_cost_vec);
1630 if (!dr0 || !npeel)
1631 do_peeling = false;
1633 /* If peeling by npeel will result in a remaining loop not iterating
1634 enough to be vectorized then do not peel. */
1635 if (do_peeling
1636 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1637 && (LOOP_VINFO_INT_NITERS (loop_vinfo)
1638 < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + npeel))
1639 do_peeling = false;
1642 if (do_peeling)
1644 stmt = DR_STMT (dr0);
1645 stmt_info = vinfo_for_stmt (stmt);
1646 vectype = STMT_VINFO_VECTYPE (stmt_info);
1647 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1649 if (known_alignment_for_access_p (dr0))
1651 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1652 size_zero_node) < 0;
1653 if (!npeel)
1655 /* Since it's known at compile time, compute the number of
1656 iterations in the peeled loop (the peeling factor) for use in
1657 updating DR_MISALIGNMENT values. The peeling factor is the
1658 vectorization factor minus the misalignment as an element
1659 count. */
1660 mis = DR_MISALIGNMENT (dr0);
1661 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1662 npeel = ((negative ? mis - nelements : nelements - mis)
1663 & (nelements - 1));
1666 /* For interleaved data access every iteration accesses all the
1667 members of the group, therefore we divide the number of iterations
1668 by the group size. */
1669 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1670 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1671 npeel /= GROUP_SIZE (stmt_info);
1673 if (dump_enabled_p ())
1674 dump_printf_loc (MSG_NOTE, vect_location,
1675 "Try peeling by %d\n", npeel);
1678 /* Ensure that all data refs can be vectorized after the peel. */
1679 FOR_EACH_VEC_ELT (datarefs, i, dr)
1681 int save_misalignment;
1683 if (dr == dr0)
1684 continue;
1686 stmt = DR_STMT (dr);
1687 stmt_info = vinfo_for_stmt (stmt);
1688 /* For interleaving, only the alignment of the first access
1689 matters. */
1690 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1691 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1692 continue;
1694 /* Strided loads perform only component accesses, alignment is
1695 irrelevant for them. */
1696 if (STMT_VINFO_STRIDE_LOAD_P (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 if (do_peeling)
1725 unsigned max_allowed_peel
1726 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1727 if (max_allowed_peel != (unsigned)-1)
1729 unsigned max_peel = npeel;
1730 if (max_peel == 0)
1732 gimple dr_stmt = DR_STMT (dr0);
1733 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1734 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1735 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1737 if (max_peel > max_allowed_peel)
1739 do_peeling = false;
1740 if (dump_enabled_p ())
1741 dump_printf_loc (MSG_NOTE, vect_location,
1742 "Disable peeling, max peels reached: %d\n", max_peel);
1747 if (do_peeling)
1749 stmt_info_for_cost *si;
1750 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
1752 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1753 If the misalignment of DR_i is identical to that of dr0 then set
1754 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1755 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1756 by the peeling factor times the element size of DR_i (MOD the
1757 vectorization factor times the size). Otherwise, the
1758 misalignment of DR_i must be set to unknown. */
1759 FOR_EACH_VEC_ELT (datarefs, i, dr)
1760 if (dr != dr0)
1761 vect_update_misalignment_for_peel (dr, dr0, npeel);
1763 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1764 if (npeel)
1765 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1766 else
1767 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1768 = DR_MISALIGNMENT (dr0);
1769 SET_DR_MISALIGNMENT (dr0, 0);
1770 if (dump_enabled_p ())
1772 dump_printf_loc (MSG_NOTE, vect_location,
1773 "Alignment of access forced using peeling.\n");
1774 dump_printf_loc (MSG_NOTE, vect_location,
1775 "Peeling for alignment will be applied.\n");
1777 /* We've delayed passing the inside-loop peeling costs to the
1778 target cost model until we were sure peeling would happen.
1779 Do so now. */
1780 if (body_cost_vec.exists ())
1782 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1784 struct _stmt_vec_info *stmt_info
1785 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1786 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1787 si->misalign, vect_body);
1789 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 /* Strided loads perform only component accesses, alignment is
1828 irrelevant for them. */
1829 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1830 continue;
1832 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1834 if (!supportable_dr_alignment)
1836 gimple stmt;
1837 int mask;
1838 tree vectype;
1840 if (known_alignment_for_access_p (dr)
1841 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1842 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1844 do_versioning = false;
1845 break;
1848 stmt = DR_STMT (dr);
1849 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1850 gcc_assert (vectype);
1852 /* The rightmost bits of an aligned address must be zeros.
1853 Construct the mask needed for this test. For example,
1854 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1855 mask must be 15 = 0xf. */
1856 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1858 /* FORNOW: use the same mask to test all potentially unaligned
1859 references in the loop. The vectorizer currently supports
1860 a single vector size, see the reference to
1861 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1862 vectorization factor is computed. */
1863 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1864 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1865 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1866 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1867 DR_STMT (dr));
1871 /* Versioning requires at least one misaligned data reference. */
1872 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1873 do_versioning = false;
1874 else if (!do_versioning)
1875 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1878 if (do_versioning)
1880 vec<gimple> may_misalign_stmts
1881 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1882 gimple stmt;
1884 /* It can now be assumed that the data references in the statements
1885 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1886 of the loop being vectorized. */
1887 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1889 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1890 dr = STMT_VINFO_DATA_REF (stmt_info);
1891 SET_DR_MISALIGNMENT (dr, 0);
1892 if (dump_enabled_p ())
1893 dump_printf_loc (MSG_NOTE, vect_location,
1894 "Alignment of access forced using versioning.\n");
1897 if (dump_enabled_p ())
1898 dump_printf_loc (MSG_NOTE, vect_location,
1899 "Versioning for alignment will be applied.\n");
1901 /* Peeling and versioning can't be done together at this time. */
1902 gcc_assert (! (do_peeling && do_versioning));
1904 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1905 gcc_assert (stat);
1906 return stat;
1909 /* This point is reached if neither peeling nor versioning is being done. */
1910 gcc_assert (! (do_peeling || do_versioning));
1912 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1913 return stat;
1917 /* Function vect_find_same_alignment_drs.
1919 Update group and alignment relations according to the chosen
1920 vectorization factor. */
1922 static void
1923 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1924 loop_vec_info loop_vinfo)
1926 unsigned int i;
1927 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1928 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1929 struct data_reference *dra = DDR_A (ddr);
1930 struct data_reference *drb = DDR_B (ddr);
1931 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1932 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1933 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1934 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1935 lambda_vector dist_v;
1936 unsigned int loop_depth;
1938 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1939 return;
1941 if (dra == drb)
1942 return;
1944 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1945 return;
1947 /* Loop-based vectorization and known data dependence. */
1948 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1949 return;
1951 /* Data-dependence analysis reports a distance vector of zero
1952 for data-references that overlap only in the first iteration
1953 but have different sign step (see PR45764).
1954 So as a sanity check require equal DR_STEP. */
1955 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1956 return;
1958 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1959 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1961 int dist = dist_v[loop_depth];
1963 if (dump_enabled_p ())
1964 dump_printf_loc (MSG_NOTE, vect_location,
1965 "dependence distance = %d.\n", dist);
1967 /* Same loop iteration. */
1968 if (dist == 0
1969 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1971 /* Two references with distance zero have the same alignment. */
1972 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1973 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1974 if (dump_enabled_p ())
1976 dump_printf_loc (MSG_NOTE, vect_location,
1977 "accesses have the same alignment.\n");
1978 dump_printf (MSG_NOTE,
1979 "dependence distance modulo vf == 0 between ");
1980 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1981 dump_printf (MSG_NOTE, " and ");
1982 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1983 dump_printf (MSG_NOTE, "\n");
1990 /* Function vect_analyze_data_refs_alignment
1992 Analyze the alignment of the data-references in the loop.
1993 Return FALSE if a data reference is found that cannot be vectorized. */
1995 bool
1996 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
1997 bb_vec_info bb_vinfo)
1999 if (dump_enabled_p ())
2000 dump_printf_loc (MSG_NOTE, vect_location,
2001 "=== vect_analyze_data_refs_alignment ===\n");
2003 /* Mark groups of data references with same alignment using
2004 data dependence information. */
2005 if (loop_vinfo)
2007 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
2008 struct data_dependence_relation *ddr;
2009 unsigned int i;
2011 FOR_EACH_VEC_ELT (ddrs, i, ddr)
2012 vect_find_same_alignment_drs (ddr, loop_vinfo);
2015 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
2017 if (dump_enabled_p ())
2018 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2019 "not vectorized: can't calculate alignment "
2020 "for data ref.\n");
2021 return false;
2024 return true;
2028 /* Analyze groups of accesses: check that DR belongs to a group of
2029 accesses of legal size, step, etc. Detect gaps, single element
2030 interleaving, and other special cases. Set grouped access info.
2031 Collect groups of strided stores for further use in SLP analysis. */
2033 static bool
2034 vect_analyze_group_access (struct data_reference *dr)
2036 tree step = DR_STEP (dr);
2037 tree scalar_type = TREE_TYPE (DR_REF (dr));
2038 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2039 gimple stmt = DR_STMT (dr);
2040 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2041 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2042 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2043 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2044 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2045 bool slp_impossible = false;
2046 struct loop *loop = NULL;
2048 if (loop_vinfo)
2049 loop = LOOP_VINFO_LOOP (loop_vinfo);
2051 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2052 size of the interleaving group (including gaps). */
2053 groupsize = absu_hwi (dr_step) / type_size;
2055 /* Not consecutive access is possible only if it is a part of interleaving. */
2056 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2058 /* Check if it this DR is a part of interleaving, and is a single
2059 element of the group that is accessed in the loop. */
2061 /* Gaps are supported only for loads. STEP must be a multiple of the type
2062 size. The size of the group must be a power of 2. */
2063 if (DR_IS_READ (dr)
2064 && (dr_step % type_size) == 0
2065 && groupsize > 0
2066 && exact_log2 (groupsize) != -1)
2068 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2069 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2070 if (dump_enabled_p ())
2072 dump_printf_loc (MSG_NOTE, vect_location,
2073 "Detected single element interleaving ");
2074 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2075 dump_printf (MSG_NOTE, " step ");
2076 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2077 dump_printf (MSG_NOTE, "\n");
2080 if (loop_vinfo)
2082 if (dump_enabled_p ())
2083 dump_printf_loc (MSG_NOTE, vect_location,
2084 "Data access with gaps requires scalar "
2085 "epilogue loop\n");
2086 if (loop->inner)
2088 if (dump_enabled_p ())
2089 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2090 "Peeling for outer loop is not"
2091 " supported\n");
2092 return false;
2095 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2098 return true;
2101 if (dump_enabled_p ())
2103 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2104 "not consecutive access ");
2105 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2106 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2109 if (bb_vinfo)
2111 /* Mark the statement as unvectorizable. */
2112 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2113 return true;
2116 return false;
2119 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2121 /* First stmt in the interleaving chain. Check the chain. */
2122 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2123 struct data_reference *data_ref = dr;
2124 unsigned int count = 1;
2125 tree prev_init = DR_INIT (data_ref);
2126 gimple prev = stmt;
2127 HOST_WIDE_INT diff, gaps = 0;
2128 unsigned HOST_WIDE_INT count_in_bytes;
2130 while (next)
2132 /* Skip same data-refs. In case that two or more stmts share
2133 data-ref (supported only for loads), we vectorize only the first
2134 stmt, and the rest get their vectorized loads from the first
2135 one. */
2136 if (!tree_int_cst_compare (DR_INIT (data_ref),
2137 DR_INIT (STMT_VINFO_DATA_REF (
2138 vinfo_for_stmt (next)))))
2140 if (DR_IS_WRITE (data_ref))
2142 if (dump_enabled_p ())
2143 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2144 "Two store stmts share the same dr.\n");
2145 return false;
2148 /* For load use the same data-ref load. */
2149 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2151 prev = next;
2152 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2153 continue;
2156 prev = next;
2157 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2159 /* All group members have the same STEP by construction. */
2160 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2162 /* Check that the distance between two accesses is equal to the type
2163 size. Otherwise, we have gaps. */
2164 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2165 - TREE_INT_CST_LOW (prev_init)) / type_size;
2166 if (diff != 1)
2168 /* FORNOW: SLP of accesses with gaps is not supported. */
2169 slp_impossible = true;
2170 if (DR_IS_WRITE (data_ref))
2172 if (dump_enabled_p ())
2173 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2174 "interleaved store with gaps\n");
2175 return false;
2178 gaps += diff - 1;
2181 last_accessed_element += diff;
2183 /* Store the gap from the previous member of the group. If there is no
2184 gap in the access, GROUP_GAP is always 1. */
2185 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2187 prev_init = DR_INIT (data_ref);
2188 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2189 /* Count the number of data-refs in the chain. */
2190 count++;
2193 /* COUNT is the number of accesses found, we multiply it by the size of
2194 the type to get COUNT_IN_BYTES. */
2195 count_in_bytes = type_size * count;
2197 /* Check that the size of the interleaving (including gaps) is not
2198 greater than STEP. */
2199 if (dr_step != 0
2200 && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
2202 if (dump_enabled_p ())
2204 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2205 "interleaving size is greater than step for ");
2206 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2207 DR_REF (dr));
2208 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2210 return false;
2213 /* Check that the size of the interleaving is equal to STEP for stores,
2214 i.e., that there are no gaps. */
2215 if (dr_step != 0
2216 && absu_hwi (dr_step) != count_in_bytes)
2218 if (DR_IS_READ (dr))
2220 slp_impossible = true;
2221 /* There is a gap after the last load in the group. This gap is a
2222 difference between the groupsize and the number of elements.
2223 When there is no gap, this difference should be 0. */
2224 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2226 else
2228 if (dump_enabled_p ())
2229 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2230 "interleaved store with gaps\n");
2231 return false;
2235 /* Check that STEP is a multiple of type size. */
2236 if (dr_step != 0
2237 && (dr_step % type_size) != 0)
2239 if (dump_enabled_p ())
2241 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2242 "step is not a multiple of type size: step ");
2243 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
2244 dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
2245 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2246 TYPE_SIZE_UNIT (scalar_type));
2247 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2249 return false;
2252 if (groupsize == 0)
2253 groupsize = count;
2255 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2256 if (dump_enabled_p ())
2257 dump_printf_loc (MSG_NOTE, vect_location,
2258 "Detected interleaving of size %d\n", (int)groupsize);
2260 /* SLP: create an SLP data structure for every interleaving group of
2261 stores for further analysis in vect_analyse_slp. */
2262 if (DR_IS_WRITE (dr) && !slp_impossible)
2264 if (loop_vinfo)
2265 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2266 if (bb_vinfo)
2267 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2270 /* There is a gap in the end of the group. */
2271 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2273 if (dump_enabled_p ())
2274 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2275 "Data access with gaps requires scalar "
2276 "epilogue loop\n");
2277 if (loop->inner)
2279 if (dump_enabled_p ())
2280 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2281 "Peeling for outer loop is not supported\n");
2282 return false;
2285 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2289 return true;
2293 /* Analyze the access pattern of the data-reference DR.
2294 In case of non-consecutive accesses call vect_analyze_group_access() to
2295 analyze groups of accesses. */
2297 static bool
2298 vect_analyze_data_ref_access (struct data_reference *dr)
2300 tree step = DR_STEP (dr);
2301 tree scalar_type = TREE_TYPE (DR_REF (dr));
2302 gimple stmt = DR_STMT (dr);
2303 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2304 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2305 struct loop *loop = NULL;
2307 if (loop_vinfo)
2308 loop = LOOP_VINFO_LOOP (loop_vinfo);
2310 if (loop_vinfo && !step)
2312 if (dump_enabled_p ())
2313 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2314 "bad data-ref access in loop\n");
2315 return false;
2318 /* Allow invariant loads in not nested loops. */
2319 if (loop_vinfo && integer_zerop (step))
2321 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2322 if (nested_in_vect_loop_p (loop, stmt))
2324 if (dump_enabled_p ())
2325 dump_printf_loc (MSG_NOTE, vect_location,
2326 "zero step in inner loop of nest\n");
2327 return false;
2329 return DR_IS_READ (dr);
2332 if (loop && nested_in_vect_loop_p (loop, stmt))
2334 /* Interleaved accesses are not yet supported within outer-loop
2335 vectorization for references in the inner-loop. */
2336 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2338 /* For the rest of the analysis we use the outer-loop step. */
2339 step = STMT_VINFO_DR_STEP (stmt_info);
2340 if (integer_zerop (step))
2342 if (dump_enabled_p ())
2343 dump_printf_loc (MSG_NOTE, vect_location,
2344 "zero step in outer loop.\n");
2345 if (DR_IS_READ (dr))
2346 return true;
2347 else
2348 return false;
2352 /* Consecutive? */
2353 if (TREE_CODE (step) == INTEGER_CST)
2355 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2356 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2357 || (dr_step < 0
2358 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2360 /* Mark that it is not interleaving. */
2361 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2362 return true;
2366 if (loop && nested_in_vect_loop_p (loop, stmt))
2368 if (dump_enabled_p ())
2369 dump_printf_loc (MSG_NOTE, vect_location,
2370 "grouped access in outer loop.\n");
2371 return false;
2374 /* Assume this is a DR handled by non-constant strided load case. */
2375 if (TREE_CODE (step) != INTEGER_CST)
2376 return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
2378 /* Not consecutive access - check if it's a part of interleaving group. */
2379 return vect_analyze_group_access (dr);
2384 /* A helper function used in the comparator function to sort data
2385 references. T1 and T2 are two data references to be compared.
2386 The function returns -1, 0, or 1. */
2388 static int
2389 compare_tree (tree t1, tree t2)
2391 int i, cmp;
2392 enum tree_code code;
2393 char tclass;
2395 if (t1 == t2)
2396 return 0;
2397 if (t1 == NULL)
2398 return -1;
2399 if (t2 == NULL)
2400 return 1;
2403 if (TREE_CODE (t1) != TREE_CODE (t2))
2404 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2406 code = TREE_CODE (t1);
2407 switch (code)
2409 /* For const values, we can just use hash values for comparisons. */
2410 case INTEGER_CST:
2411 case REAL_CST:
2412 case FIXED_CST:
2413 case STRING_CST:
2414 case COMPLEX_CST:
2415 case VECTOR_CST:
2417 hashval_t h1 = iterative_hash_expr (t1, 0);
2418 hashval_t h2 = iterative_hash_expr (t2, 0);
2419 if (h1 != h2)
2420 return h1 < h2 ? -1 : 1;
2421 break;
2424 case SSA_NAME:
2425 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2426 if (cmp != 0)
2427 return cmp;
2429 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2430 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2431 break;
2433 default:
2434 tclass = TREE_CODE_CLASS (code);
2436 /* For var-decl, we could compare their UIDs. */
2437 if (tclass == tcc_declaration)
2439 if (DECL_UID (t1) != DECL_UID (t2))
2440 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2441 break;
2444 /* For expressions with operands, compare their operands recursively. */
2445 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2447 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2448 if (cmp != 0)
2449 return cmp;
2453 return 0;
2457 /* Compare two data-references DRA and DRB to group them into chunks
2458 suitable for grouping. */
2460 static int
2461 dr_group_sort_cmp (const void *dra_, const void *drb_)
2463 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2464 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2465 int cmp;
2467 /* Stabilize sort. */
2468 if (dra == drb)
2469 return 0;
2471 /* Ordering of DRs according to base. */
2472 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2474 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2475 if (cmp != 0)
2476 return cmp;
2479 /* And according to DR_OFFSET. */
2480 if (!dr_equal_offsets_p (dra, drb))
2482 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2483 if (cmp != 0)
2484 return cmp;
2487 /* Put reads before writes. */
2488 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2489 return DR_IS_READ (dra) ? -1 : 1;
2491 /* Then sort after access size. */
2492 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2493 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2495 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2496 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2497 if (cmp != 0)
2498 return cmp;
2501 /* And after step. */
2502 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2504 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2505 if (cmp != 0)
2506 return cmp;
2509 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2510 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2511 if (cmp == 0)
2512 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2513 return cmp;
2516 /* Function vect_analyze_data_ref_accesses.
2518 Analyze the access pattern of all the data references in the loop.
2520 FORNOW: the only access pattern that is considered vectorizable is a
2521 simple step 1 (consecutive) access.
2523 FORNOW: handle only arrays and pointer accesses. */
2525 bool
2526 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2528 unsigned int i;
2529 vec<data_reference_p> datarefs;
2530 struct data_reference *dr;
2532 if (dump_enabled_p ())
2533 dump_printf_loc (MSG_NOTE, vect_location,
2534 "=== vect_analyze_data_ref_accesses ===\n");
2536 if (loop_vinfo)
2537 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2538 else
2539 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2541 if (datarefs.is_empty ())
2542 return true;
2544 /* Sort the array of datarefs to make building the interleaving chains
2545 linear. Don't modify the original vector's order, it is needed for
2546 determining what dependencies are reversed. */
2547 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2548 datarefs_copy.qsort (dr_group_sort_cmp);
2550 /* Build the interleaving chains. */
2551 for (i = 0; i < datarefs_copy.length () - 1;)
2553 data_reference_p dra = datarefs_copy[i];
2554 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2555 stmt_vec_info lastinfo = NULL;
2556 for (i = i + 1; i < datarefs_copy.length (); ++i)
2558 data_reference_p drb = datarefs_copy[i];
2559 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2561 /* ??? Imperfect sorting (non-compatible types, non-modulo
2562 accesses, same accesses) can lead to a group to be artificially
2563 split here as we don't just skip over those. If it really
2564 matters we can push those to a worklist and re-iterate
2565 over them. The we can just skip ahead to the next DR here. */
2567 /* Check that the data-refs have same first location (except init)
2568 and they are both either store or load (not load and store,
2569 not masked loads or stores). */
2570 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2571 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2572 DR_BASE_ADDRESS (drb), 0)
2573 || !dr_equal_offsets_p (dra, drb)
2574 || !gimple_assign_single_p (DR_STMT (dra))
2575 || !gimple_assign_single_p (DR_STMT (drb)))
2576 break;
2578 /* Check that the data-refs have the same constant size and step. */
2579 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2580 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2581 if (!tree_fits_uhwi_p (sza)
2582 || !tree_fits_uhwi_p (szb)
2583 || !tree_int_cst_equal (sza, szb)
2584 || !tree_fits_shwi_p (DR_STEP (dra))
2585 || !tree_fits_shwi_p (DR_STEP (drb))
2586 || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
2587 break;
2589 /* Do not place the same access in the interleaving chain twice. */
2590 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2591 break;
2593 /* Check the types are compatible.
2594 ??? We don't distinguish this during sorting. */
2595 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2596 TREE_TYPE (DR_REF (drb))))
2597 break;
2599 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2600 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2601 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2602 gcc_assert (init_a < init_b);
2604 /* If init_b == init_a + the size of the type * k, we have an
2605 interleaving, and DRA is accessed before DRB. */
2606 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2607 if ((init_b - init_a) % type_size_a != 0)
2608 break;
2610 /* The step (if not zero) is greater than the difference between
2611 data-refs' inits. This splits groups into suitable sizes. */
2612 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2613 if (step != 0 && step <= (init_b - init_a))
2614 break;
2616 if (dump_enabled_p ())
2618 dump_printf_loc (MSG_NOTE, vect_location,
2619 "Detected interleaving ");
2620 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2621 dump_printf (MSG_NOTE, " and ");
2622 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2623 dump_printf (MSG_NOTE, "\n");
2626 /* Link the found element into the group list. */
2627 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2629 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2630 lastinfo = stmtinfo_a;
2632 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2633 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2634 lastinfo = stmtinfo_b;
2638 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2639 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2640 && !vect_analyze_data_ref_access (dr))
2642 if (dump_enabled_p ())
2643 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2644 "not vectorized: complicated access pattern.\n");
2646 if (bb_vinfo)
2648 /* Mark the statement as not vectorizable. */
2649 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2650 continue;
2652 else
2654 datarefs_copy.release ();
2655 return false;
2659 datarefs_copy.release ();
2660 return true;
2664 /* Operator == between two dr_with_seg_len objects.
2666 This equality operator is used to make sure two data refs
2667 are the same one so that we will consider to combine the
2668 aliasing checks of those two pairs of data dependent data
2669 refs. */
2671 static bool
2672 operator == (const dr_with_seg_len& d1,
2673 const dr_with_seg_len& d2)
2675 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2676 DR_BASE_ADDRESS (d2.dr), 0)
2677 && compare_tree (d1.offset, d2.offset) == 0
2678 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2681 /* Function comp_dr_with_seg_len_pair.
2683 Comparison function for sorting objects of dr_with_seg_len_pair_t
2684 so that we can combine aliasing checks in one scan. */
2686 static int
2687 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2689 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2690 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2692 const dr_with_seg_len &p11 = p1->first,
2693 &p12 = p1->second,
2694 &p21 = p2->first,
2695 &p22 = p2->second;
2697 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2698 if a and c have the same basic address snd step, and b and d have the same
2699 address and step. Therefore, if any a&c or b&d don't have the same address
2700 and step, we don't care the order of those two pairs after sorting. */
2701 int comp_res;
2703 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2704 DR_BASE_ADDRESS (p21.dr))) != 0)
2705 return comp_res;
2706 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2707 DR_BASE_ADDRESS (p22.dr))) != 0)
2708 return comp_res;
2709 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2710 return comp_res;
2711 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2712 return comp_res;
2713 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2714 return comp_res;
2715 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2716 return comp_res;
2718 return 0;
2721 template <class T> static void
2722 swap (T& a, T& b)
2724 T c (a);
2725 a = b;
2726 b = c;
2729 /* Function vect_vfa_segment_size.
2731 Create an expression that computes the size of segment
2732 that will be accessed for a data reference. The functions takes into
2733 account that realignment loads may access one more vector.
2735 Input:
2736 DR: The data reference.
2737 LENGTH_FACTOR: segment length to consider.
2739 Return an expression whose value is the size of segment which will be
2740 accessed by DR. */
2742 static tree
2743 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2745 tree segment_length;
2747 if (integer_zerop (DR_STEP (dr)))
2748 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2749 else
2750 segment_length = size_binop (MULT_EXPR,
2751 fold_convert (sizetype, DR_STEP (dr)),
2752 fold_convert (sizetype, length_factor));
2754 if (vect_supportable_dr_alignment (dr, false)
2755 == dr_explicit_realign_optimized)
2757 tree vector_size = TYPE_SIZE_UNIT
2758 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2760 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2762 return segment_length;
2765 /* Function vect_prune_runtime_alias_test_list.
2767 Prune a list of ddrs to be tested at run-time by versioning for alias.
2768 Merge several alias checks into one if possible.
2769 Return FALSE if resulting list of ddrs is longer then allowed by
2770 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2772 bool
2773 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2775 vec<ddr_p> may_alias_ddrs =
2776 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2777 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2778 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2779 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2780 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2782 ddr_p ddr;
2783 unsigned int i;
2784 tree length_factor;
2786 if (dump_enabled_p ())
2787 dump_printf_loc (MSG_NOTE, vect_location,
2788 "=== vect_prune_runtime_alias_test_list ===\n");
2790 if (may_alias_ddrs.is_empty ())
2791 return true;
2793 /* Basically, for each pair of dependent data refs store_ptr_0
2794 and load_ptr_0, we create an expression:
2796 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2797 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2799 for aliasing checks. However, in some cases we can decrease
2800 the number of checks by combining two checks into one. For
2801 example, suppose we have another pair of data refs store_ptr_0
2802 and load_ptr_1, and if the following condition is satisfied:
2804 load_ptr_0 < load_ptr_1 &&
2805 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2807 (this condition means, in each iteration of vectorized loop,
2808 the accessed memory of store_ptr_0 cannot be between the memory
2809 of load_ptr_0 and load_ptr_1.)
2811 we then can use only the following expression to finish the
2812 alising checks between store_ptr_0 & load_ptr_0 and
2813 store_ptr_0 & load_ptr_1:
2815 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2816 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2818 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2819 same basic address. */
2821 comp_alias_ddrs.create (may_alias_ddrs.length ());
2823 /* First, we collect all data ref pairs for aliasing checks. */
2824 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2826 struct data_reference *dr_a, *dr_b;
2827 gimple dr_group_first_a, dr_group_first_b;
2828 tree segment_length_a, segment_length_b;
2829 gimple stmt_a, stmt_b;
2831 dr_a = DDR_A (ddr);
2832 stmt_a = DR_STMT (DDR_A (ddr));
2833 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2834 if (dr_group_first_a)
2836 stmt_a = dr_group_first_a;
2837 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2840 dr_b = DDR_B (ddr);
2841 stmt_b = DR_STMT (DDR_B (ddr));
2842 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2843 if (dr_group_first_b)
2845 stmt_b = dr_group_first_b;
2846 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2849 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2850 length_factor = scalar_loop_iters;
2851 else
2852 length_factor = size_int (vect_factor);
2853 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2854 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2856 dr_with_seg_len_pair_t dr_with_seg_len_pair
2857 (dr_with_seg_len (dr_a, segment_length_a),
2858 dr_with_seg_len (dr_b, segment_length_b));
2860 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2861 swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2863 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2866 /* Second, we sort the collected data ref pairs so that we can scan
2867 them once to combine all possible aliasing checks. */
2868 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2870 /* Third, we scan the sorted dr pairs and check if we can combine
2871 alias checks of two neighbouring dr pairs. */
2872 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2874 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2875 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2876 *dr_b1 = &comp_alias_ddrs[i-1].second,
2877 *dr_a2 = &comp_alias_ddrs[i].first,
2878 *dr_b2 = &comp_alias_ddrs[i].second;
2880 /* Remove duplicate data ref pairs. */
2881 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2883 if (dump_enabled_p ())
2885 dump_printf_loc (MSG_NOTE, vect_location,
2886 "found equal ranges ");
2887 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2888 DR_REF (dr_a1->dr));
2889 dump_printf (MSG_NOTE, ", ");
2890 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2891 DR_REF (dr_b1->dr));
2892 dump_printf (MSG_NOTE, " and ");
2893 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2894 DR_REF (dr_a2->dr));
2895 dump_printf (MSG_NOTE, ", ");
2896 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2897 DR_REF (dr_b2->dr));
2898 dump_printf (MSG_NOTE, "\n");
2901 comp_alias_ddrs.ordered_remove (i--);
2902 continue;
2905 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2907 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2908 and DR_A1 and DR_A2 are two consecutive memrefs. */
2909 if (*dr_a1 == *dr_a2)
2911 swap (dr_a1, dr_b1);
2912 swap (dr_a2, dr_b2);
2915 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2916 DR_BASE_ADDRESS (dr_a2->dr),
2918 || !tree_fits_shwi_p (dr_a1->offset)
2919 || !tree_fits_shwi_p (dr_a2->offset))
2920 continue;
2922 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2923 - tree_to_shwi (dr_a1->offset));
2926 /* Now we check if the following condition is satisfied:
2928 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2930 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2931 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2932 have to make a best estimation. We can get the minimum value
2933 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2934 then either of the following two conditions can guarantee the
2935 one above:
2937 1: DIFF <= MIN_SEG_LEN_B
2938 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2942 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2943 ? tree_to_shwi (dr_b1->seg_len)
2944 : vect_factor);
2946 if (diff <= min_seg_len_b
2947 || (tree_fits_shwi_p (dr_a1->seg_len)
2948 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2950 if (dump_enabled_p ())
2952 dump_printf_loc (MSG_NOTE, vect_location,
2953 "merging ranges for ");
2954 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2955 DR_REF (dr_a1->dr));
2956 dump_printf (MSG_NOTE, ", ");
2957 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2958 DR_REF (dr_b1->dr));
2959 dump_printf (MSG_NOTE, " and ");
2960 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2961 DR_REF (dr_a2->dr));
2962 dump_printf (MSG_NOTE, ", ");
2963 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2964 DR_REF (dr_b2->dr));
2965 dump_printf (MSG_NOTE, "\n");
2968 dr_a1->seg_len = size_binop (PLUS_EXPR,
2969 dr_a2->seg_len, size_int (diff));
2970 comp_alias_ddrs.ordered_remove (i--);
2975 dump_printf_loc (MSG_NOTE, vect_location,
2976 "improved number of alias checks from %d to %d\n",
2977 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2978 if ((int) comp_alias_ddrs.length () >
2979 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2980 return false;
2982 return true;
2985 /* Check whether a non-affine read in stmt is suitable for gather load
2986 and if so, return a builtin decl for that operation. */
2988 tree
2989 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2990 tree *offp, int *scalep)
2992 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2993 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2994 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2995 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2996 tree offtype = NULL_TREE;
2997 tree decl, base, off;
2998 machine_mode pmode;
2999 int punsignedp, pvolatilep;
3001 base = DR_REF (dr);
3002 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
3003 see if we can use the def stmt of the address. */
3004 if (is_gimple_call (stmt)
3005 && gimple_call_internal_p (stmt)
3006 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
3007 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
3008 && TREE_CODE (base) == MEM_REF
3009 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3010 && integer_zerop (TREE_OPERAND (base, 1))
3011 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
3013 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
3014 if (is_gimple_assign (def_stmt)
3015 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3016 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3019 /* The gather builtins need address of the form
3020 loop_invariant + vector * {1, 2, 4, 8}
3022 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3023 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3024 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3025 multiplications and additions in it. To get a vector, we need
3026 a single SSA_NAME that will be defined in the loop and will
3027 contain everything that is not loop invariant and that can be
3028 vectorized. The following code attempts to find such a preexistng
3029 SSA_NAME OFF and put the loop invariants into a tree BASE
3030 that can be gimplified before the loop. */
3031 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
3032 &pmode, &punsignedp, &pvolatilep, false);
3033 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
3035 if (TREE_CODE (base) == MEM_REF)
3037 if (!integer_zerop (TREE_OPERAND (base, 1)))
3039 if (off == NULL_TREE)
3041 offset_int moff = mem_ref_offset (base);
3042 off = wide_int_to_tree (sizetype, moff);
3044 else
3045 off = size_binop (PLUS_EXPR, off,
3046 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3048 base = TREE_OPERAND (base, 0);
3050 else
3051 base = build_fold_addr_expr (base);
3053 if (off == NULL_TREE)
3054 off = size_zero_node;
3056 /* If base is not loop invariant, either off is 0, then we start with just
3057 the constant offset in the loop invariant BASE and continue with base
3058 as OFF, otherwise give up.
3059 We could handle that case by gimplifying the addition of base + off
3060 into some SSA_NAME and use that as off, but for now punt. */
3061 if (!expr_invariant_in_loop_p (loop, base))
3063 if (!integer_zerop (off))
3064 return NULL_TREE;
3065 off = base;
3066 base = size_int (pbitpos / BITS_PER_UNIT);
3068 /* Otherwise put base + constant offset into the loop invariant BASE
3069 and continue with OFF. */
3070 else
3072 base = fold_convert (sizetype, base);
3073 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3076 /* OFF at this point may be either a SSA_NAME or some tree expression
3077 from get_inner_reference. Try to peel off loop invariants from it
3078 into BASE as long as possible. */
3079 STRIP_NOPS (off);
3080 while (offtype == NULL_TREE)
3082 enum tree_code code;
3083 tree op0, op1, add = NULL_TREE;
3085 if (TREE_CODE (off) == SSA_NAME)
3087 gimple def_stmt = SSA_NAME_DEF_STMT (off);
3089 if (expr_invariant_in_loop_p (loop, off))
3090 return NULL_TREE;
3092 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3093 break;
3095 op0 = gimple_assign_rhs1 (def_stmt);
3096 code = gimple_assign_rhs_code (def_stmt);
3097 op1 = gimple_assign_rhs2 (def_stmt);
3099 else
3101 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3102 return NULL_TREE;
3103 code = TREE_CODE (off);
3104 extract_ops_from_tree (off, &code, &op0, &op1);
3106 switch (code)
3108 case POINTER_PLUS_EXPR:
3109 case PLUS_EXPR:
3110 if (expr_invariant_in_loop_p (loop, op0))
3112 add = op0;
3113 off = op1;
3114 do_add:
3115 add = fold_convert (sizetype, add);
3116 if (scale != 1)
3117 add = size_binop (MULT_EXPR, add, size_int (scale));
3118 base = size_binop (PLUS_EXPR, base, add);
3119 continue;
3121 if (expr_invariant_in_loop_p (loop, op1))
3123 add = op1;
3124 off = op0;
3125 goto do_add;
3127 break;
3128 case MINUS_EXPR:
3129 if (expr_invariant_in_loop_p (loop, op1))
3131 add = fold_convert (sizetype, op1);
3132 add = size_binop (MINUS_EXPR, size_zero_node, add);
3133 off = op0;
3134 goto do_add;
3136 break;
3137 case MULT_EXPR:
3138 if (scale == 1 && tree_fits_shwi_p (op1))
3140 scale = tree_to_shwi (op1);
3141 off = op0;
3142 continue;
3144 break;
3145 case SSA_NAME:
3146 off = op0;
3147 continue;
3148 CASE_CONVERT:
3149 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3150 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3151 break;
3152 if (TYPE_PRECISION (TREE_TYPE (op0))
3153 == TYPE_PRECISION (TREE_TYPE (off)))
3155 off = op0;
3156 continue;
3158 if (TYPE_PRECISION (TREE_TYPE (op0))
3159 < TYPE_PRECISION (TREE_TYPE (off)))
3161 off = op0;
3162 offtype = TREE_TYPE (off);
3163 STRIP_NOPS (off);
3164 continue;
3166 break;
3167 default:
3168 break;
3170 break;
3173 /* If at the end OFF still isn't a SSA_NAME or isn't
3174 defined in the loop, punt. */
3175 if (TREE_CODE (off) != SSA_NAME
3176 || expr_invariant_in_loop_p (loop, off))
3177 return NULL_TREE;
3179 if (offtype == NULL_TREE)
3180 offtype = TREE_TYPE (off);
3182 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3183 offtype, scale);
3184 if (decl == NULL_TREE)
3185 return NULL_TREE;
3187 if (basep)
3188 *basep = base;
3189 if (offp)
3190 *offp = off;
3191 if (scalep)
3192 *scalep = scale;
3193 return decl;
3196 /* Function vect_analyze_data_refs.
3198 Find all the data references in the loop or basic block.
3200 The general structure of the analysis of data refs in the vectorizer is as
3201 follows:
3202 1- vect_analyze_data_refs(loop/bb): call
3203 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3204 in the loop/bb and their dependences.
3205 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3206 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3207 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3211 bool
3212 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3213 bb_vec_info bb_vinfo,
3214 int *min_vf, unsigned *n_stmts)
3216 struct loop *loop = NULL;
3217 basic_block bb = NULL;
3218 unsigned int i;
3219 vec<data_reference_p> datarefs;
3220 struct data_reference *dr;
3221 tree scalar_type;
3223 if (dump_enabled_p ())
3224 dump_printf_loc (MSG_NOTE, vect_location,
3225 "=== vect_analyze_data_refs ===\n");
3227 if (loop_vinfo)
3229 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3231 loop = LOOP_VINFO_LOOP (loop_vinfo);
3232 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3233 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3235 if (dump_enabled_p ())
3236 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3237 "not vectorized: loop contains function calls"
3238 " or data references that cannot be analyzed\n");
3239 return false;
3242 for (i = 0; i < loop->num_nodes; i++)
3244 gimple_stmt_iterator gsi;
3246 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3248 gimple stmt = gsi_stmt (gsi);
3249 if (is_gimple_debug (stmt))
3250 continue;
3251 ++*n_stmts;
3252 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3254 if (is_gimple_call (stmt) && loop->safelen)
3256 tree fndecl = gimple_call_fndecl (stmt), op;
3257 if (fndecl != NULL_TREE)
3259 struct cgraph_node *node = cgraph_node::get (fndecl);
3260 if (node != NULL && node->simd_clones != NULL)
3262 unsigned int j, n = gimple_call_num_args (stmt);
3263 for (j = 0; j < n; j++)
3265 op = gimple_call_arg (stmt, j);
3266 if (DECL_P (op)
3267 || (REFERENCE_CLASS_P (op)
3268 && get_base_address (op)))
3269 break;
3271 op = gimple_call_lhs (stmt);
3272 /* Ignore #pragma omp declare simd functions
3273 if they don't have data references in the
3274 call stmt itself. */
3275 if (j == n
3276 && !(op
3277 && (DECL_P (op)
3278 || (REFERENCE_CLASS_P (op)
3279 && get_base_address (op)))))
3280 continue;
3284 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3285 if (dump_enabled_p ())
3286 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3287 "not vectorized: loop contains function "
3288 "calls or data references that cannot "
3289 "be analyzed\n");
3290 return false;
3295 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3297 else
3299 gimple_stmt_iterator gsi;
3301 bb = BB_VINFO_BB (bb_vinfo);
3302 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3304 gimple stmt = gsi_stmt (gsi);
3305 if (is_gimple_debug (stmt))
3306 continue;
3307 ++*n_stmts;
3308 if (!find_data_references_in_stmt (NULL, stmt,
3309 &BB_VINFO_DATAREFS (bb_vinfo)))
3311 /* Mark the rest of the basic-block as unvectorizable. */
3312 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3314 stmt = gsi_stmt (gsi);
3315 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3317 break;
3321 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3324 /* Go through the data-refs, check that the analysis succeeded. Update
3325 pointer from stmt_vec_info struct to DR and vectype. */
3327 FOR_EACH_VEC_ELT (datarefs, i, dr)
3329 gimple stmt;
3330 stmt_vec_info stmt_info;
3331 tree base, offset, init;
3332 bool gather = false;
3333 bool simd_lane_access = false;
3334 int vf;
3336 again:
3337 if (!dr || !DR_REF (dr))
3339 if (dump_enabled_p ())
3340 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3341 "not vectorized: unhandled data-ref\n");
3342 return false;
3345 stmt = DR_STMT (dr);
3346 stmt_info = vinfo_for_stmt (stmt);
3348 /* Discard clobbers from the dataref vector. We will remove
3349 clobber stmts during vectorization. */
3350 if (gimple_clobber_p (stmt))
3352 free_data_ref (dr);
3353 if (i == datarefs.length () - 1)
3355 datarefs.pop ();
3356 break;
3358 datarefs.ordered_remove (i);
3359 dr = datarefs[i];
3360 goto again;
3363 /* Check that analysis of the data-ref succeeded. */
3364 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3365 || !DR_STEP (dr))
3367 bool maybe_gather
3368 = DR_IS_READ (dr)
3369 && !TREE_THIS_VOLATILE (DR_REF (dr))
3370 && targetm.vectorize.builtin_gather != NULL;
3371 bool maybe_simd_lane_access
3372 = loop_vinfo && loop->simduid;
3374 /* If target supports vector gather loads, or if this might be
3375 a SIMD lane access, see if they can't be used. */
3376 if (loop_vinfo
3377 && (maybe_gather || maybe_simd_lane_access)
3378 && !nested_in_vect_loop_p (loop, stmt))
3380 struct data_reference *newdr
3381 = create_data_ref (NULL, loop_containing_stmt (stmt),
3382 DR_REF (dr), stmt, true);
3383 gcc_assert (newdr != NULL && DR_REF (newdr));
3384 if (DR_BASE_ADDRESS (newdr)
3385 && DR_OFFSET (newdr)
3386 && DR_INIT (newdr)
3387 && DR_STEP (newdr)
3388 && integer_zerop (DR_STEP (newdr)))
3390 if (maybe_simd_lane_access)
3392 tree off = DR_OFFSET (newdr);
3393 STRIP_NOPS (off);
3394 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3395 && TREE_CODE (off) == MULT_EXPR
3396 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3398 tree step = TREE_OPERAND (off, 1);
3399 off = TREE_OPERAND (off, 0);
3400 STRIP_NOPS (off);
3401 if (CONVERT_EXPR_P (off)
3402 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3403 0)))
3404 < TYPE_PRECISION (TREE_TYPE (off)))
3405 off = TREE_OPERAND (off, 0);
3406 if (TREE_CODE (off) == SSA_NAME)
3408 gimple def = SSA_NAME_DEF_STMT (off);
3409 tree reft = TREE_TYPE (DR_REF (newdr));
3410 if (is_gimple_call (def)
3411 && gimple_call_internal_p (def)
3412 && (gimple_call_internal_fn (def)
3413 == IFN_GOMP_SIMD_LANE))
3415 tree arg = gimple_call_arg (def, 0);
3416 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3417 arg = SSA_NAME_VAR (arg);
3418 if (arg == loop->simduid
3419 /* For now. */
3420 && tree_int_cst_equal
3421 (TYPE_SIZE_UNIT (reft),
3422 step))
3424 DR_OFFSET (newdr) = ssize_int (0);
3425 DR_STEP (newdr) = step;
3426 DR_ALIGNED_TO (newdr)
3427 = size_int (BIGGEST_ALIGNMENT);
3428 dr = newdr;
3429 simd_lane_access = true;
3435 if (!simd_lane_access && maybe_gather)
3437 dr = newdr;
3438 gather = true;
3441 if (!gather && !simd_lane_access)
3442 free_data_ref (newdr);
3445 if (!gather && !simd_lane_access)
3447 if (dump_enabled_p ())
3449 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3450 "not vectorized: data ref analysis "
3451 "failed ");
3452 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3453 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3456 if (bb_vinfo)
3457 break;
3459 return false;
3463 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3465 if (dump_enabled_p ())
3466 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3467 "not vectorized: base addr of dr is a "
3468 "constant\n");
3470 if (bb_vinfo)
3471 break;
3473 if (gather || simd_lane_access)
3474 free_data_ref (dr);
3475 return false;
3478 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3480 if (dump_enabled_p ())
3482 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3483 "not vectorized: volatile type ");
3484 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3485 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3488 if (bb_vinfo)
3489 break;
3491 return false;
3494 if (stmt_can_throw_internal (stmt))
3496 if (dump_enabled_p ())
3498 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3499 "not vectorized: statement can throw an "
3500 "exception ");
3501 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3502 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3505 if (bb_vinfo)
3506 break;
3508 if (gather || simd_lane_access)
3509 free_data_ref (dr);
3510 return false;
3513 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3514 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3516 if (dump_enabled_p ())
3518 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3519 "not vectorized: statement is bitfield "
3520 "access ");
3521 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3522 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3525 if (bb_vinfo)
3526 break;
3528 if (gather || simd_lane_access)
3529 free_data_ref (dr);
3530 return false;
3533 base = unshare_expr (DR_BASE_ADDRESS (dr));
3534 offset = unshare_expr (DR_OFFSET (dr));
3535 init = unshare_expr (DR_INIT (dr));
3537 if (is_gimple_call (stmt)
3538 && (!gimple_call_internal_p (stmt)
3539 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3540 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3542 if (dump_enabled_p ())
3544 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3545 "not vectorized: dr in a call ");
3546 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3547 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3550 if (bb_vinfo)
3551 break;
3553 if (gather || simd_lane_access)
3554 free_data_ref (dr);
3555 return false;
3558 /* Update DR field in stmt_vec_info struct. */
3560 /* If the dataref is in an inner-loop of the loop that is considered for
3561 for vectorization, we also want to analyze the access relative to
3562 the outer-loop (DR contains information only relative to the
3563 inner-most enclosing loop). We do that by building a reference to the
3564 first location accessed by the inner-loop, and analyze it relative to
3565 the outer-loop. */
3566 if (loop && nested_in_vect_loop_p (loop, stmt))
3568 tree outer_step, outer_base, outer_init;
3569 HOST_WIDE_INT pbitsize, pbitpos;
3570 tree poffset;
3571 machine_mode pmode;
3572 int punsignedp, pvolatilep;
3573 affine_iv base_iv, offset_iv;
3574 tree dinit;
3576 /* Build a reference to the first location accessed by the
3577 inner-loop: *(BASE+INIT). (The first location is actually
3578 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3579 tree inner_base = build_fold_indirect_ref
3580 (fold_build_pointer_plus (base, init));
3582 if (dump_enabled_p ())
3584 dump_printf_loc (MSG_NOTE, vect_location,
3585 "analyze in outer-loop: ");
3586 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3587 dump_printf (MSG_NOTE, "\n");
3590 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3591 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3592 gcc_assert (outer_base != NULL_TREE);
3594 if (pbitpos % BITS_PER_UNIT != 0)
3596 if (dump_enabled_p ())
3597 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3598 "failed: bit offset alignment.\n");
3599 return false;
3602 outer_base = build_fold_addr_expr (outer_base);
3603 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3604 &base_iv, false))
3606 if (dump_enabled_p ())
3607 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3608 "failed: evolution of base is not affine.\n");
3609 return false;
3612 if (offset)
3614 if (poffset)
3615 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3616 poffset);
3617 else
3618 poffset = offset;
3621 if (!poffset)
3623 offset_iv.base = ssize_int (0);
3624 offset_iv.step = ssize_int (0);
3626 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3627 &offset_iv, false))
3629 if (dump_enabled_p ())
3630 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3631 "evolution of offset is not affine.\n");
3632 return false;
3635 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3636 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3637 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3638 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3639 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3641 outer_step = size_binop (PLUS_EXPR,
3642 fold_convert (ssizetype, base_iv.step),
3643 fold_convert (ssizetype, offset_iv.step));
3645 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3646 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3647 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3648 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3649 STMT_VINFO_DR_OFFSET (stmt_info) =
3650 fold_convert (ssizetype, offset_iv.base);
3651 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3652 size_int (highest_pow2_factor (offset_iv.base));
3654 if (dump_enabled_p ())
3656 dump_printf_loc (MSG_NOTE, vect_location,
3657 "\touter base_address: ");
3658 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3659 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3660 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3661 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3662 STMT_VINFO_DR_OFFSET (stmt_info));
3663 dump_printf (MSG_NOTE,
3664 "\n\touter constant offset from base address: ");
3665 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3666 STMT_VINFO_DR_INIT (stmt_info));
3667 dump_printf (MSG_NOTE, "\n\touter step: ");
3668 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3669 STMT_VINFO_DR_STEP (stmt_info));
3670 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3671 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3672 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3673 dump_printf (MSG_NOTE, "\n");
3677 if (STMT_VINFO_DATA_REF (stmt_info))
3679 if (dump_enabled_p ())
3681 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3682 "not vectorized: more than one data ref "
3683 "in stmt: ");
3684 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3685 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3688 if (bb_vinfo)
3689 break;
3691 if (gather || simd_lane_access)
3692 free_data_ref (dr);
3693 return false;
3696 STMT_VINFO_DATA_REF (stmt_info) = dr;
3697 if (simd_lane_access)
3699 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3700 free_data_ref (datarefs[i]);
3701 datarefs[i] = dr;
3704 /* Set vectype for STMT. */
3705 scalar_type = TREE_TYPE (DR_REF (dr));
3706 STMT_VINFO_VECTYPE (stmt_info)
3707 = get_vectype_for_scalar_type (scalar_type);
3708 if (!STMT_VINFO_VECTYPE (stmt_info))
3710 if (dump_enabled_p ())
3712 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3713 "not vectorized: no vectype for stmt: ");
3714 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3715 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3716 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3717 scalar_type);
3718 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3721 if (bb_vinfo)
3722 break;
3724 if (gather || simd_lane_access)
3726 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3727 if (gather)
3728 free_data_ref (dr);
3730 return false;
3732 else
3734 if (dump_enabled_p ())
3736 dump_printf_loc (MSG_NOTE, vect_location,
3737 "got vectype for stmt: ");
3738 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3739 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3740 STMT_VINFO_VECTYPE (stmt_info));
3741 dump_printf (MSG_NOTE, "\n");
3745 /* Adjust the minimal vectorization factor according to the
3746 vector type. */
3747 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3748 if (vf > *min_vf)
3749 *min_vf = vf;
3751 if (gather)
3753 tree off;
3755 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3756 if (gather
3757 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3758 gather = false;
3759 if (!gather)
3761 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3762 free_data_ref (dr);
3763 if (dump_enabled_p ())
3765 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3766 "not vectorized: not suitable for gather "
3767 "load ");
3768 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3769 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3771 return false;
3774 datarefs[i] = dr;
3775 STMT_VINFO_GATHER_P (stmt_info) = true;
3777 else if (loop_vinfo
3778 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3780 if (nested_in_vect_loop_p (loop, stmt)
3781 || !DR_IS_READ (dr))
3783 if (dump_enabled_p ())
3785 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3786 "not vectorized: not suitable for strided "
3787 "load ");
3788 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3789 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3791 return false;
3793 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3797 /* If we stopped analysis at the first dataref we could not analyze
3798 when trying to vectorize a basic-block mark the rest of the datarefs
3799 as not vectorizable and truncate the vector of datarefs. That
3800 avoids spending useless time in analyzing their dependence. */
3801 if (i != datarefs.length ())
3803 gcc_assert (bb_vinfo != NULL);
3804 for (unsigned j = i; j < datarefs.length (); ++j)
3806 data_reference_p dr = datarefs[j];
3807 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3808 free_data_ref (dr);
3810 datarefs.truncate (i);
3813 return true;
3817 /* Function vect_get_new_vect_var.
3819 Returns a name for a new variable. The current naming scheme appends the
3820 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3821 the name of vectorizer generated variables, and appends that to NAME if
3822 provided. */
3824 tree
3825 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3827 const char *prefix;
3828 tree new_vect_var;
3830 switch (var_kind)
3832 case vect_simple_var:
3833 prefix = "vect";
3834 break;
3835 case vect_scalar_var:
3836 prefix = "stmp";
3837 break;
3838 case vect_pointer_var:
3839 prefix = "vectp";
3840 break;
3841 default:
3842 gcc_unreachable ();
3845 if (name)
3847 char* tmp = concat (prefix, "_", name, NULL);
3848 new_vect_var = create_tmp_reg (type, tmp);
3849 free (tmp);
3851 else
3852 new_vect_var = create_tmp_reg (type, prefix);
3854 return new_vect_var;
3858 /* Function vect_create_addr_base_for_vector_ref.
3860 Create an expression that computes the address of the first memory location
3861 that will be accessed for a data reference.
3863 Input:
3864 STMT: The statement containing the data reference.
3865 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3866 OFFSET: Optional. If supplied, it is be added to the initial address.
3867 LOOP: Specify relative to which loop-nest should the address be computed.
3868 For example, when the dataref is in an inner-loop nested in an
3869 outer-loop that is now being vectorized, LOOP can be either the
3870 outer-loop, or the inner-loop. The first memory location accessed
3871 by the following dataref ('in' points to short):
3873 for (i=0; i<N; i++)
3874 for (j=0; j<M; j++)
3875 s += in[i+j]
3877 is as follows:
3878 if LOOP=i_loop: &in (relative to i_loop)
3879 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3880 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3881 initial address. Unlike OFFSET, which is number of elements to
3882 be added, BYTE_OFFSET is measured in bytes.
3884 Output:
3885 1. Return an SSA_NAME whose value is the address of the memory location of
3886 the first vector of the data reference.
3887 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3888 these statement(s) which define the returned SSA_NAME.
3890 FORNOW: We are only handling array accesses with step 1. */
3892 tree
3893 vect_create_addr_base_for_vector_ref (gimple stmt,
3894 gimple_seq *new_stmt_list,
3895 tree offset,
3896 struct loop *loop,
3897 tree byte_offset)
3899 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3900 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3901 tree data_ref_base;
3902 const char *base_name;
3903 tree addr_base;
3904 tree dest;
3905 gimple_seq seq = NULL;
3906 tree base_offset;
3907 tree init;
3908 tree vect_ptr_type;
3909 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3910 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3912 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3914 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3916 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3918 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3919 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3920 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3922 else
3924 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3925 base_offset = unshare_expr (DR_OFFSET (dr));
3926 init = unshare_expr (DR_INIT (dr));
3929 if (loop_vinfo)
3930 base_name = get_name (data_ref_base);
3931 else
3933 base_offset = ssize_int (0);
3934 init = ssize_int (0);
3935 base_name = get_name (DR_REF (dr));
3938 /* Create base_offset */
3939 base_offset = size_binop (PLUS_EXPR,
3940 fold_convert (sizetype, base_offset),
3941 fold_convert (sizetype, init));
3943 if (offset)
3945 offset = fold_build2 (MULT_EXPR, sizetype,
3946 fold_convert (sizetype, offset), step);
3947 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3948 base_offset, offset);
3950 if (byte_offset)
3952 byte_offset = fold_convert (sizetype, byte_offset);
3953 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3954 base_offset, byte_offset);
3957 /* base + base_offset */
3958 if (loop_vinfo)
3959 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3960 else
3962 addr_base = build1 (ADDR_EXPR,
3963 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3964 unshare_expr (DR_REF (dr)));
3967 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3968 addr_base = fold_convert (vect_ptr_type, addr_base);
3969 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3970 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3971 gimple_seq_add_seq (new_stmt_list, seq);
3973 if (DR_PTR_INFO (dr)
3974 && TREE_CODE (addr_base) == SSA_NAME)
3976 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3977 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
3978 int misalign = DR_MISALIGNMENT (dr);
3979 if (offset || byte_offset || (misalign == -1))
3980 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3981 else
3982 set_ptr_info_alignment (SSA_NAME_PTR_INFO (addr_base), align, misalign);
3985 if (dump_enabled_p ())
3987 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3988 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3989 dump_printf (MSG_NOTE, "\n");
3992 return addr_base;
3996 /* Function vect_create_data_ref_ptr.
3998 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3999 location accessed in the loop by STMT, along with the def-use update
4000 chain to appropriately advance the pointer through the loop iterations.
4001 Also set aliasing information for the pointer. This pointer is used by
4002 the callers to this function to create a memory reference expression for
4003 vector load/store access.
4005 Input:
4006 1. STMT: a stmt that references memory. Expected to be of the form
4007 GIMPLE_ASSIGN <name, data-ref> or
4008 GIMPLE_ASSIGN <data-ref, name>.
4009 2. AGGR_TYPE: the type of the reference, which should be either a vector
4010 or an array.
4011 3. AT_LOOP: the loop where the vector memref is to be created.
4012 4. OFFSET (optional): an offset to be added to the initial address accessed
4013 by the data-ref in STMT.
4014 5. BSI: location where the new stmts are to be placed if there is no loop
4015 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4016 pointing to the initial address.
4017 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4018 to the initial address accessed by the data-ref in STMT. This is
4019 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4020 in bytes.
4022 Output:
4023 1. Declare a new ptr to vector_type, and have it point to the base of the
4024 data reference (initial addressed accessed by the data reference).
4025 For example, for vector of type V8HI, the following code is generated:
4027 v8hi *ap;
4028 ap = (v8hi *)initial_address;
4030 if OFFSET is not supplied:
4031 initial_address = &a[init];
4032 if OFFSET is supplied:
4033 initial_address = &a[init + OFFSET];
4034 if BYTE_OFFSET is supplied:
4035 initial_address = &a[init] + BYTE_OFFSET;
4037 Return the initial_address in INITIAL_ADDRESS.
4039 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4040 update the pointer in each iteration of the loop.
4042 Return the increment stmt that updates the pointer in PTR_INCR.
4044 3. Set INV_P to true if the access pattern of the data reference in the
4045 vectorized loop is invariant. Set it to false otherwise.
4047 4. Return the pointer. */
4049 tree
4050 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
4051 tree offset, tree *initial_address,
4052 gimple_stmt_iterator *gsi, gimple *ptr_incr,
4053 bool only_init, bool *inv_p, tree byte_offset)
4055 const char *base_name;
4056 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4057 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4058 struct loop *loop = NULL;
4059 bool nested_in_vect_loop = false;
4060 struct loop *containing_loop = NULL;
4061 tree aggr_ptr_type;
4062 tree aggr_ptr;
4063 tree new_temp;
4064 gimple vec_stmt;
4065 gimple_seq new_stmt_list = NULL;
4066 edge pe = NULL;
4067 basic_block new_bb;
4068 tree aggr_ptr_init;
4069 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4070 tree aptr;
4071 gimple_stmt_iterator incr_gsi;
4072 bool insert_after;
4073 tree indx_before_incr, indx_after_incr;
4074 gimple incr;
4075 tree step;
4076 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4078 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4079 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4081 if (loop_vinfo)
4083 loop = LOOP_VINFO_LOOP (loop_vinfo);
4084 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4085 containing_loop = (gimple_bb (stmt))->loop_father;
4086 pe = loop_preheader_edge (loop);
4088 else
4090 gcc_assert (bb_vinfo);
4091 only_init = true;
4092 *ptr_incr = NULL;
4095 /* Check the step (evolution) of the load in LOOP, and record
4096 whether it's invariant. */
4097 if (nested_in_vect_loop)
4098 step = STMT_VINFO_DR_STEP (stmt_info);
4099 else
4100 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4102 if (integer_zerop (step))
4103 *inv_p = true;
4104 else
4105 *inv_p = false;
4107 /* Create an expression for the first address accessed by this load
4108 in LOOP. */
4109 base_name = get_name (DR_BASE_ADDRESS (dr));
4111 if (dump_enabled_p ())
4113 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4114 dump_printf_loc (MSG_NOTE, vect_location,
4115 "create %s-pointer variable to type: ",
4116 get_tree_code_name (TREE_CODE (aggr_type)));
4117 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4118 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4119 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4120 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4121 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4122 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4123 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4124 else
4125 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4126 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4127 dump_printf (MSG_NOTE, "\n");
4130 /* (1) Create the new aggregate-pointer variable.
4131 Vector and array types inherit the alias set of their component
4132 type by default so we need to use a ref-all pointer if the data
4133 reference does not conflict with the created aggregated data
4134 reference because it is not addressable. */
4135 bool need_ref_all = false;
4136 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4137 get_alias_set (DR_REF (dr))))
4138 need_ref_all = true;
4139 /* Likewise for any of the data references in the stmt group. */
4140 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4142 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4145 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4146 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4147 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4148 get_alias_set (DR_REF (sdr))))
4150 need_ref_all = true;
4151 break;
4153 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4155 while (orig_stmt);
4157 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4158 need_ref_all);
4159 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4162 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4163 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4164 def-use update cycles for the pointer: one relative to the outer-loop
4165 (LOOP), which is what steps (3) and (4) below do. The other is relative
4166 to the inner-loop (which is the inner-most loop containing the dataref),
4167 and this is done be step (5) below.
4169 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4170 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4171 redundant. Steps (3),(4) create the following:
4173 vp0 = &base_addr;
4174 LOOP: vp1 = phi(vp0,vp2)
4177 vp2 = vp1 + step
4178 goto LOOP
4180 If there is an inner-loop nested in loop, then step (5) will also be
4181 applied, and an additional update in the inner-loop will be created:
4183 vp0 = &base_addr;
4184 LOOP: vp1 = phi(vp0,vp2)
4186 inner: vp3 = phi(vp1,vp4)
4187 vp4 = vp3 + inner_step
4188 if () goto inner
4190 vp2 = vp1 + step
4191 if () goto LOOP */
4193 /* (2) Calculate the initial address of the aggregate-pointer, and set
4194 the aggregate-pointer to point to it before the loop. */
4196 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4198 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4199 offset, loop, byte_offset);
4200 if (new_stmt_list)
4202 if (pe)
4204 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4205 gcc_assert (!new_bb);
4207 else
4208 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4211 *initial_address = new_temp;
4213 /* Create: p = (aggr_type *) initial_base */
4214 if (TREE_CODE (new_temp) != SSA_NAME
4215 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4217 vec_stmt = gimple_build_assign (aggr_ptr,
4218 fold_convert (aggr_ptr_type, new_temp));
4219 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4220 /* Copy the points-to information if it exists. */
4221 if (DR_PTR_INFO (dr))
4222 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
4223 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4224 if (pe)
4226 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4227 gcc_assert (!new_bb);
4229 else
4230 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4232 else
4233 aggr_ptr_init = new_temp;
4235 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4236 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4237 inner-loop nested in LOOP (during outer-loop vectorization). */
4239 /* No update in loop is required. */
4240 if (only_init && (!loop_vinfo || at_loop == loop))
4241 aptr = aggr_ptr_init;
4242 else
4244 /* The step of the aggregate pointer is the type size. */
4245 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4246 /* One exception to the above is when the scalar step of the load in
4247 LOOP is zero. In this case the step here is also zero. */
4248 if (*inv_p)
4249 iv_step = size_zero_node;
4250 else if (tree_int_cst_sgn (step) == -1)
4251 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4253 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4255 create_iv (aggr_ptr_init,
4256 fold_convert (aggr_ptr_type, iv_step),
4257 aggr_ptr, loop, &incr_gsi, insert_after,
4258 &indx_before_incr, &indx_after_incr);
4259 incr = gsi_stmt (incr_gsi);
4260 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4262 /* Copy the points-to information if it exists. */
4263 if (DR_PTR_INFO (dr))
4265 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4266 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4268 if (ptr_incr)
4269 *ptr_incr = incr;
4271 aptr = indx_before_incr;
4274 if (!nested_in_vect_loop || only_init)
4275 return aptr;
4278 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4279 nested in LOOP, if exists. */
4281 gcc_assert (nested_in_vect_loop);
4282 if (!only_init)
4284 standard_iv_increment_position (containing_loop, &incr_gsi,
4285 &insert_after);
4286 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4287 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4288 &indx_after_incr);
4289 incr = gsi_stmt (incr_gsi);
4290 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4292 /* Copy the points-to information if it exists. */
4293 if (DR_PTR_INFO (dr))
4295 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4296 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4298 if (ptr_incr)
4299 *ptr_incr = incr;
4301 return indx_before_incr;
4303 else
4304 gcc_unreachable ();
4308 /* Function bump_vector_ptr
4310 Increment a pointer (to a vector type) by vector-size. If requested,
4311 i.e. if PTR-INCR is given, then also connect the new increment stmt
4312 to the existing def-use update-chain of the pointer, by modifying
4313 the PTR_INCR as illustrated below:
4315 The pointer def-use update-chain before this function:
4316 DATAREF_PTR = phi (p_0, p_2)
4317 ....
4318 PTR_INCR: p_2 = DATAREF_PTR + step
4320 The pointer def-use update-chain after this function:
4321 DATAREF_PTR = phi (p_0, p_2)
4322 ....
4323 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4324 ....
4325 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4327 Input:
4328 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4329 in the loop.
4330 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4331 the loop. The increment amount across iterations is expected
4332 to be vector_size.
4333 BSI - location where the new update stmt is to be placed.
4334 STMT - the original scalar memory-access stmt that is being vectorized.
4335 BUMP - optional. The offset by which to bump the pointer. If not given,
4336 the offset is assumed to be vector_size.
4338 Output: Return NEW_DATAREF_PTR as illustrated above.
4342 tree
4343 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4344 gimple stmt, tree bump)
4346 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4347 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4348 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4349 tree update = TYPE_SIZE_UNIT (vectype);
4350 gimple incr_stmt;
4351 ssa_op_iter iter;
4352 use_operand_p use_p;
4353 tree new_dataref_ptr;
4355 if (bump)
4356 update = bump;
4358 new_dataref_ptr = copy_ssa_name (dataref_ptr, NULL);
4359 incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, new_dataref_ptr,
4360 dataref_ptr, update);
4361 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4363 /* Copy the points-to information if it exists. */
4364 if (DR_PTR_INFO (dr))
4366 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4367 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4370 if (!ptr_incr)
4371 return new_dataref_ptr;
4373 /* Update the vector-pointer's cross-iteration increment. */
4374 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4376 tree use = USE_FROM_PTR (use_p);
4378 if (use == dataref_ptr)
4379 SET_USE (use_p, new_dataref_ptr);
4380 else
4381 gcc_assert (tree_int_cst_compare (use, update) == 0);
4384 return new_dataref_ptr;
4388 /* Function vect_create_destination_var.
4390 Create a new temporary of type VECTYPE. */
4392 tree
4393 vect_create_destination_var (tree scalar_dest, tree vectype)
4395 tree vec_dest;
4396 const char *name;
4397 char *new_name;
4398 tree type;
4399 enum vect_var_kind kind;
4401 kind = vectype ? vect_simple_var : vect_scalar_var;
4402 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4404 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4406 name = get_name (scalar_dest);
4407 if (name)
4408 asprintf (&new_name, "%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4409 else
4410 asprintf (&new_name, "_%u", SSA_NAME_VERSION (scalar_dest));
4411 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4412 free (new_name);
4414 return vec_dest;
4417 /* Function vect_grouped_store_supported.
4419 Returns TRUE if interleave high and interleave low permutations
4420 are supported, and FALSE otherwise. */
4422 bool
4423 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4425 machine_mode mode = TYPE_MODE (vectype);
4427 /* vect_permute_store_chain requires the group size to be equal to 3 or
4428 be a power of two. */
4429 if (count != 3 && exact_log2 (count) == -1)
4431 if (dump_enabled_p ())
4432 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4433 "the size of the group of accesses"
4434 " is not a power of 2 or not eqaul to 3\n");
4435 return false;
4438 /* Check that the permutation is supported. */
4439 if (VECTOR_MODE_P (mode))
4441 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4442 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4444 if (count == 3)
4446 unsigned int j0 = 0, j1 = 0, j2 = 0;
4447 unsigned int i, j;
4449 for (j = 0; j < 3; j++)
4451 int nelt0 = ((3 - j) * nelt) % 3;
4452 int nelt1 = ((3 - j) * nelt + 1) % 3;
4453 int nelt2 = ((3 - j) * nelt + 2) % 3;
4454 for (i = 0; i < nelt; i++)
4456 if (3 * i + nelt0 < nelt)
4457 sel[3 * i + nelt0] = j0++;
4458 if (3 * i + nelt1 < nelt)
4459 sel[3 * i + nelt1] = nelt + j1++;
4460 if (3 * i + nelt2 < nelt)
4461 sel[3 * i + nelt2] = 0;
4463 if (!can_vec_perm_p (mode, false, sel))
4465 if (dump_enabled_p ())
4466 dump_printf (MSG_MISSED_OPTIMIZATION,
4467 "permutaion op not supported by target.\n");
4468 return false;
4471 for (i = 0; i < nelt; i++)
4473 if (3 * i + nelt0 < nelt)
4474 sel[3 * i + nelt0] = 3 * i + nelt0;
4475 if (3 * i + nelt1 < nelt)
4476 sel[3 * i + nelt1] = 3 * i + nelt1;
4477 if (3 * i + nelt2 < nelt)
4478 sel[3 * i + nelt2] = nelt + j2++;
4480 if (!can_vec_perm_p (mode, false, sel))
4482 if (dump_enabled_p ())
4483 dump_printf (MSG_MISSED_OPTIMIZATION,
4484 "permutaion op not supported by target.\n");
4485 return false;
4488 return true;
4490 else
4492 /* If length is not equal to 3 then only power of 2 is supported. */
4493 gcc_assert (exact_log2 (count) != -1);
4495 for (i = 0; i < nelt / 2; i++)
4497 sel[i * 2] = i;
4498 sel[i * 2 + 1] = i + nelt;
4500 if (can_vec_perm_p (mode, false, sel))
4502 for (i = 0; i < nelt; i++)
4503 sel[i] += nelt / 2;
4504 if (can_vec_perm_p (mode, false, sel))
4505 return true;
4510 if (dump_enabled_p ())
4511 dump_printf (MSG_MISSED_OPTIMIZATION,
4512 "permutaion op not supported by target.\n");
4513 return false;
4517 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4518 type VECTYPE. */
4520 bool
4521 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4523 return vect_lanes_optab_supported_p ("vec_store_lanes",
4524 vec_store_lanes_optab,
4525 vectype, count);
4529 /* Function vect_permute_store_chain.
4531 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4532 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4533 the data correctly for the stores. Return the final references for stores
4534 in RESULT_CHAIN.
4536 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4537 The input is 4 vectors each containing 8 elements. We assign a number to
4538 each element, the input sequence is:
4540 1st vec: 0 1 2 3 4 5 6 7
4541 2nd vec: 8 9 10 11 12 13 14 15
4542 3rd vec: 16 17 18 19 20 21 22 23
4543 4th vec: 24 25 26 27 28 29 30 31
4545 The output sequence should be:
4547 1st vec: 0 8 16 24 1 9 17 25
4548 2nd vec: 2 10 18 26 3 11 19 27
4549 3rd vec: 4 12 20 28 5 13 21 30
4550 4th vec: 6 14 22 30 7 15 23 31
4552 i.e., we interleave the contents of the four vectors in their order.
4554 We use interleave_high/low instructions to create such output. The input of
4555 each interleave_high/low operation is two vectors:
4556 1st vec 2nd vec
4557 0 1 2 3 4 5 6 7
4558 the even elements of the result vector are obtained left-to-right from the
4559 high/low elements of the first vector. The odd elements of the result are
4560 obtained left-to-right from the high/low elements of the second vector.
4561 The output of interleave_high will be: 0 4 1 5
4562 and of interleave_low: 2 6 3 7
4565 The permutation is done in log LENGTH stages. In each stage interleave_high
4566 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4567 where the first argument is taken from the first half of DR_CHAIN and the
4568 second argument from it's second half.
4569 In our example,
4571 I1: interleave_high (1st vec, 3rd vec)
4572 I2: interleave_low (1st vec, 3rd vec)
4573 I3: interleave_high (2nd vec, 4th vec)
4574 I4: interleave_low (2nd vec, 4th vec)
4576 The output for the first stage is:
4578 I1: 0 16 1 17 2 18 3 19
4579 I2: 4 20 5 21 6 22 7 23
4580 I3: 8 24 9 25 10 26 11 27
4581 I4: 12 28 13 29 14 30 15 31
4583 The output of the second stage, i.e. the final result is:
4585 I1: 0 8 16 24 1 9 17 25
4586 I2: 2 10 18 26 3 11 19 27
4587 I3: 4 12 20 28 5 13 21 30
4588 I4: 6 14 22 30 7 15 23 31. */
4590 void
4591 vect_permute_store_chain (vec<tree> dr_chain,
4592 unsigned int length,
4593 gimple stmt,
4594 gimple_stmt_iterator *gsi,
4595 vec<tree> *result_chain)
4597 tree vect1, vect2, high, low;
4598 gimple perm_stmt;
4599 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4600 tree perm_mask_low, perm_mask_high;
4601 tree data_ref;
4602 tree perm3_mask_low, perm3_mask_high;
4603 unsigned int i, n, log_length = exact_log2 (length);
4604 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4605 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4607 result_chain->quick_grow (length);
4608 memcpy (result_chain->address (), dr_chain.address (),
4609 length * sizeof (tree));
4611 if (length == 3)
4613 unsigned int j0 = 0, j1 = 0, j2 = 0;
4615 for (j = 0; j < 3; j++)
4617 int nelt0 = ((3 - j) * nelt) % 3;
4618 int nelt1 = ((3 - j) * nelt + 1) % 3;
4619 int nelt2 = ((3 - j) * nelt + 2) % 3;
4621 for (i = 0; i < nelt; i++)
4623 if (3 * i + nelt0 < nelt)
4624 sel[3 * i + nelt0] = j0++;
4625 if (3 * i + nelt1 < nelt)
4626 sel[3 * i + nelt1] = nelt + j1++;
4627 if (3 * i + nelt2 < nelt)
4628 sel[3 * i + nelt2] = 0;
4630 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
4631 gcc_assert (perm3_mask_low != NULL);
4633 for (i = 0; i < nelt; i++)
4635 if (3 * i + nelt0 < nelt)
4636 sel[3 * i + nelt0] = 3 * i + nelt0;
4637 if (3 * i + nelt1 < nelt)
4638 sel[3 * i + nelt1] = 3 * i + nelt1;
4639 if (3 * i + nelt2 < nelt)
4640 sel[3 * i + nelt2] = nelt + j2++;
4642 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
4643 gcc_assert (perm3_mask_high != NULL);
4645 vect1 = dr_chain[0];
4646 vect2 = dr_chain[1];
4648 /* Create interleaving stmt:
4649 low = VEC_PERM_EXPR <vect1, vect2,
4650 {j, nelt, *, j + 1, nelt + j + 1, *,
4651 j + 2, nelt + j + 2, *, ...}> */
4652 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4653 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4654 vect1, vect2,
4655 perm3_mask_low);
4656 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4658 vect1 = data_ref;
4659 vect2 = dr_chain[2];
4660 /* Create interleaving stmt:
4661 low = VEC_PERM_EXPR <vect1, vect2,
4662 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4663 6, 7, nelt + j + 2, ...}> */
4664 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4665 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4666 vect1, vect2,
4667 perm3_mask_high);
4668 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4669 (*result_chain)[j] = data_ref;
4672 else
4674 /* If length is not equal to 3 then only power of 2 is supported. */
4675 gcc_assert (exact_log2 (length) != -1);
4677 for (i = 0, n = nelt / 2; i < n; i++)
4679 sel[i * 2] = i;
4680 sel[i * 2 + 1] = i + nelt;
4682 perm_mask_high = vect_gen_perm_mask (vectype, sel);
4683 gcc_assert (perm_mask_high != NULL);
4685 for (i = 0; i < nelt; i++)
4686 sel[i] += nelt / 2;
4687 perm_mask_low = vect_gen_perm_mask (vectype, sel);
4688 gcc_assert (perm_mask_low != NULL);
4690 for (i = 0, n = log_length; i < n; i++)
4692 for (j = 0; j < length/2; j++)
4694 vect1 = dr_chain[j];
4695 vect2 = dr_chain[j+length/2];
4697 /* Create interleaving stmt:
4698 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4699 ...}> */
4700 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4701 perm_stmt
4702 = gimple_build_assign_with_ops (VEC_PERM_EXPR, high,
4703 vect1, vect2, perm_mask_high);
4704 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4705 (*result_chain)[2*j] = high;
4707 /* Create interleaving stmt:
4708 low = VEC_PERM_EXPR <vect1, vect2,
4709 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4710 ...}> */
4711 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4712 perm_stmt
4713 = gimple_build_assign_with_ops (VEC_PERM_EXPR, low,
4714 vect1, vect2, perm_mask_low);
4715 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4716 (*result_chain)[2*j+1] = low;
4718 memcpy (dr_chain.address (), result_chain->address (),
4719 length * sizeof (tree));
4724 /* Function vect_setup_realignment
4726 This function is called when vectorizing an unaligned load using
4727 the dr_explicit_realign[_optimized] scheme.
4728 This function generates the following code at the loop prolog:
4730 p = initial_addr;
4731 x msq_init = *(floor(p)); # prolog load
4732 realignment_token = call target_builtin;
4733 loop:
4734 x msq = phi (msq_init, ---)
4736 The stmts marked with x are generated only for the case of
4737 dr_explicit_realign_optimized.
4739 The code above sets up a new (vector) pointer, pointing to the first
4740 location accessed by STMT, and a "floor-aligned" load using that pointer.
4741 It also generates code to compute the "realignment-token" (if the relevant
4742 target hook was defined), and creates a phi-node at the loop-header bb
4743 whose arguments are the result of the prolog-load (created by this
4744 function) and the result of a load that takes place in the loop (to be
4745 created by the caller to this function).
4747 For the case of dr_explicit_realign_optimized:
4748 The caller to this function uses the phi-result (msq) to create the
4749 realignment code inside the loop, and sets up the missing phi argument,
4750 as follows:
4751 loop:
4752 msq = phi (msq_init, lsq)
4753 lsq = *(floor(p')); # load in loop
4754 result = realign_load (msq, lsq, realignment_token);
4756 For the case of dr_explicit_realign:
4757 loop:
4758 msq = *(floor(p)); # load in loop
4759 p' = p + (VS-1);
4760 lsq = *(floor(p')); # load in loop
4761 result = realign_load (msq, lsq, realignment_token);
4763 Input:
4764 STMT - (scalar) load stmt to be vectorized. This load accesses
4765 a memory location that may be unaligned.
4766 BSI - place where new code is to be inserted.
4767 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4768 is used.
4770 Output:
4771 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4772 target hook, if defined.
4773 Return value - the result of the loop-header phi node. */
4775 tree
4776 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4777 tree *realignment_token,
4778 enum dr_alignment_support alignment_support_scheme,
4779 tree init_addr,
4780 struct loop **at_loop)
4782 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4783 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4784 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4785 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4786 struct loop *loop = NULL;
4787 edge pe = NULL;
4788 tree scalar_dest = gimple_assign_lhs (stmt);
4789 tree vec_dest;
4790 gimple inc;
4791 tree ptr;
4792 tree data_ref;
4793 gimple new_stmt;
4794 basic_block new_bb;
4795 tree msq_init = NULL_TREE;
4796 tree new_temp;
4797 gimple phi_stmt;
4798 tree msq = NULL_TREE;
4799 gimple_seq stmts = NULL;
4800 bool inv_p;
4801 bool compute_in_loop = false;
4802 bool nested_in_vect_loop = false;
4803 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4804 struct loop *loop_for_initial_load = NULL;
4806 if (loop_vinfo)
4808 loop = LOOP_VINFO_LOOP (loop_vinfo);
4809 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4812 gcc_assert (alignment_support_scheme == dr_explicit_realign
4813 || alignment_support_scheme == dr_explicit_realign_optimized);
4815 /* We need to generate three things:
4816 1. the misalignment computation
4817 2. the extra vector load (for the optimized realignment scheme).
4818 3. the phi node for the two vectors from which the realignment is
4819 done (for the optimized realignment scheme). */
4821 /* 1. Determine where to generate the misalignment computation.
4823 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4824 calculation will be generated by this function, outside the loop (in the
4825 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4826 caller, inside the loop.
4828 Background: If the misalignment remains fixed throughout the iterations of
4829 the loop, then both realignment schemes are applicable, and also the
4830 misalignment computation can be done outside LOOP. This is because we are
4831 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4832 are a multiple of VS (the Vector Size), and therefore the misalignment in
4833 different vectorized LOOP iterations is always the same.
4834 The problem arises only if the memory access is in an inner-loop nested
4835 inside LOOP, which is now being vectorized using outer-loop vectorization.
4836 This is the only case when the misalignment of the memory access may not
4837 remain fixed throughout the iterations of the inner-loop (as explained in
4838 detail in vect_supportable_dr_alignment). In this case, not only is the
4839 optimized realignment scheme not applicable, but also the misalignment
4840 computation (and generation of the realignment token that is passed to
4841 REALIGN_LOAD) have to be done inside the loop.
4843 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4844 or not, which in turn determines if the misalignment is computed inside
4845 the inner-loop, or outside LOOP. */
4847 if (init_addr != NULL_TREE || !loop_vinfo)
4849 compute_in_loop = true;
4850 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4854 /* 2. Determine where to generate the extra vector load.
4856 For the optimized realignment scheme, instead of generating two vector
4857 loads in each iteration, we generate a single extra vector load in the
4858 preheader of the loop, and in each iteration reuse the result of the
4859 vector load from the previous iteration. In case the memory access is in
4860 an inner-loop nested inside LOOP, which is now being vectorized using
4861 outer-loop vectorization, we need to determine whether this initial vector
4862 load should be generated at the preheader of the inner-loop, or can be
4863 generated at the preheader of LOOP. If the memory access has no evolution
4864 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4865 to be generated inside LOOP (in the preheader of the inner-loop). */
4867 if (nested_in_vect_loop)
4869 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4870 bool invariant_in_outerloop =
4871 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4872 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4874 else
4875 loop_for_initial_load = loop;
4876 if (at_loop)
4877 *at_loop = loop_for_initial_load;
4879 if (loop_for_initial_load)
4880 pe = loop_preheader_edge (loop_for_initial_load);
4882 /* 3. For the case of the optimized realignment, create the first vector
4883 load at the loop preheader. */
4885 if (alignment_support_scheme == dr_explicit_realign_optimized)
4887 /* Create msq_init = *(floor(p1)) in the loop preheader */
4889 gcc_assert (!compute_in_loop);
4890 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4891 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4892 NULL_TREE, &init_addr, NULL, &inc,
4893 true, &inv_p);
4894 new_temp = copy_ssa_name (ptr, NULL);
4895 new_stmt = gimple_build_assign_with_ops
4896 (BIT_AND_EXPR, new_temp, ptr,
4897 build_int_cst (TREE_TYPE (ptr),
4898 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4899 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4900 gcc_assert (!new_bb);
4901 data_ref
4902 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4903 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4904 new_stmt = gimple_build_assign (vec_dest, data_ref);
4905 new_temp = make_ssa_name (vec_dest, new_stmt);
4906 gimple_assign_set_lhs (new_stmt, new_temp);
4907 if (pe)
4909 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4910 gcc_assert (!new_bb);
4912 else
4913 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4915 msq_init = gimple_assign_lhs (new_stmt);
4918 /* 4. Create realignment token using a target builtin, if available.
4919 It is done either inside the containing loop, or before LOOP (as
4920 determined above). */
4922 if (targetm.vectorize.builtin_mask_for_load)
4924 tree builtin_decl;
4926 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4927 if (!init_addr)
4929 /* Generate the INIT_ADDR computation outside LOOP. */
4930 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4931 NULL_TREE, loop);
4932 if (loop)
4934 pe = loop_preheader_edge (loop);
4935 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4936 gcc_assert (!new_bb);
4938 else
4939 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4942 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4943 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4944 vec_dest =
4945 vect_create_destination_var (scalar_dest,
4946 gimple_call_return_type (new_stmt));
4947 new_temp = make_ssa_name (vec_dest, new_stmt);
4948 gimple_call_set_lhs (new_stmt, new_temp);
4950 if (compute_in_loop)
4951 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4952 else
4954 /* Generate the misalignment computation outside LOOP. */
4955 pe = loop_preheader_edge (loop);
4956 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4957 gcc_assert (!new_bb);
4960 *realignment_token = gimple_call_lhs (new_stmt);
4962 /* The result of the CALL_EXPR to this builtin is determined from
4963 the value of the parameter and no global variables are touched
4964 which makes the builtin a "const" function. Requiring the
4965 builtin to have the "const" attribute makes it unnecessary
4966 to call mark_call_clobbered. */
4967 gcc_assert (TREE_READONLY (builtin_decl));
4970 if (alignment_support_scheme == dr_explicit_realign)
4971 return msq;
4973 gcc_assert (!compute_in_loop);
4974 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4977 /* 5. Create msq = phi <msq_init, lsq> in loop */
4979 pe = loop_preheader_edge (containing_loop);
4980 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4981 msq = make_ssa_name (vec_dest, NULL);
4982 phi_stmt = create_phi_node (msq, containing_loop->header);
4983 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4985 return msq;
4989 /* Function vect_grouped_load_supported.
4991 Returns TRUE if even and odd permutations are supported,
4992 and FALSE otherwise. */
4994 bool
4995 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4997 machine_mode mode = TYPE_MODE (vectype);
4999 /* vect_permute_load_chain requires the group size to be equal to 3 or
5000 be a power of two. */
5001 if (count != 3 && exact_log2 (count) == -1)
5003 if (dump_enabled_p ())
5004 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5005 "the size of the group of accesses"
5006 " is not a power of 2 or not equal to 3\n");
5007 return false;
5010 /* Check that the permutation is supported. */
5011 if (VECTOR_MODE_P (mode))
5013 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5014 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5016 if (count == 3)
5018 unsigned int k;
5019 for (k = 0; k < 3; k++)
5021 for (i = 0; i < nelt; i++)
5022 if (3 * i + k < 2 * nelt)
5023 sel[i] = 3 * i + k;
5024 else
5025 sel[i] = 0;
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;
5034 for (i = 0, j = 0; i < nelt; i++)
5035 if (3 * i + k < 2 * nelt)
5036 sel[i] = i;
5037 else
5038 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5039 if (!can_vec_perm_p (mode, false, sel))
5041 if (dump_enabled_p ())
5042 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5043 "shuffle of 3 loads is not supported by"
5044 " target\n");
5045 return false;
5048 return true;
5050 else
5052 /* If length is not equal to 3 then only power of 2 is supported. */
5053 gcc_assert (exact_log2 (count) != -1);
5054 for (i = 0; i < nelt; i++)
5055 sel[i] = i * 2;
5056 if (can_vec_perm_p (mode, false, sel))
5058 for (i = 0; i < nelt; i++)
5059 sel[i] = i * 2 + 1;
5060 if (can_vec_perm_p (mode, false, sel))
5061 return true;
5066 if (dump_enabled_p ())
5067 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5068 "extract even/odd not supported by target\n");
5069 return false;
5072 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5073 type VECTYPE. */
5075 bool
5076 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5078 return vect_lanes_optab_supported_p ("vec_load_lanes",
5079 vec_load_lanes_optab,
5080 vectype, count);
5083 /* Function vect_permute_load_chain.
5085 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5086 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5087 the input data correctly. Return the final references for loads in
5088 RESULT_CHAIN.
5090 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5091 The input is 4 vectors each containing 8 elements. We assign a number to each
5092 element, the input sequence is:
5094 1st vec: 0 1 2 3 4 5 6 7
5095 2nd vec: 8 9 10 11 12 13 14 15
5096 3rd vec: 16 17 18 19 20 21 22 23
5097 4th vec: 24 25 26 27 28 29 30 31
5099 The output sequence should be:
5101 1st vec: 0 4 8 12 16 20 24 28
5102 2nd vec: 1 5 9 13 17 21 25 29
5103 3rd vec: 2 6 10 14 18 22 26 30
5104 4th vec: 3 7 11 15 19 23 27 31
5106 i.e., the first output vector should contain the first elements of each
5107 interleaving group, etc.
5109 We use extract_even/odd instructions to create such output. The input of
5110 each extract_even/odd operation is two vectors
5111 1st vec 2nd vec
5112 0 1 2 3 4 5 6 7
5114 and the output is the vector of extracted even/odd elements. The output of
5115 extract_even will be: 0 2 4 6
5116 and of extract_odd: 1 3 5 7
5119 The permutation is done in log LENGTH stages. In each stage extract_even
5120 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5121 their order. In our example,
5123 E1: extract_even (1st vec, 2nd vec)
5124 E2: extract_odd (1st vec, 2nd vec)
5125 E3: extract_even (3rd vec, 4th vec)
5126 E4: extract_odd (3rd vec, 4th vec)
5128 The output for the first stage will be:
5130 E1: 0 2 4 6 8 10 12 14
5131 E2: 1 3 5 7 9 11 13 15
5132 E3: 16 18 20 22 24 26 28 30
5133 E4: 17 19 21 23 25 27 29 31
5135 In order to proceed and create the correct sequence for the next stage (or
5136 for the correct output, if the second stage is the last one, as in our
5137 example), we first put the output of extract_even operation and then the
5138 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5139 The input for the second stage is:
5141 1st vec (E1): 0 2 4 6 8 10 12 14
5142 2nd vec (E3): 16 18 20 22 24 26 28 30
5143 3rd vec (E2): 1 3 5 7 9 11 13 15
5144 4th vec (E4): 17 19 21 23 25 27 29 31
5146 The output of the second stage:
5148 E1: 0 4 8 12 16 20 24 28
5149 E2: 2 6 10 14 18 22 26 30
5150 E3: 1 5 9 13 17 21 25 29
5151 E4: 3 7 11 15 19 23 27 31
5153 And RESULT_CHAIN after reordering:
5155 1st vec (E1): 0 4 8 12 16 20 24 28
5156 2nd vec (E3): 1 5 9 13 17 21 25 29
5157 3rd vec (E2): 2 6 10 14 18 22 26 30
5158 4th vec (E4): 3 7 11 15 19 23 27 31. */
5160 static void
5161 vect_permute_load_chain (vec<tree> dr_chain,
5162 unsigned int length,
5163 gimple stmt,
5164 gimple_stmt_iterator *gsi,
5165 vec<tree> *result_chain)
5167 tree data_ref, first_vect, second_vect;
5168 tree perm_mask_even, perm_mask_odd;
5169 tree perm3_mask_low, perm3_mask_high;
5170 gimple perm_stmt;
5171 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5172 unsigned int i, j, log_length = exact_log2 (length);
5173 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5174 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5176 result_chain->quick_grow (length);
5177 memcpy (result_chain->address (), dr_chain.address (),
5178 length * sizeof (tree));
5180 if (length == 3)
5182 unsigned int k;
5184 for (k = 0; k < 3; k++)
5186 for (i = 0; i < nelt; i++)
5187 if (3 * i + k < 2 * nelt)
5188 sel[i] = 3 * i + k;
5189 else
5190 sel[i] = 0;
5191 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
5192 gcc_assert (perm3_mask_low != NULL);
5194 for (i = 0, j = 0; i < nelt; i++)
5195 if (3 * i + k < 2 * nelt)
5196 sel[i] = i;
5197 else
5198 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5200 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
5201 gcc_assert (perm3_mask_high != NULL);
5203 first_vect = dr_chain[0];
5204 second_vect = dr_chain[1];
5206 /* Create interleaving stmt (low part of):
5207 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5208 ...}> */
5209 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5210 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5211 first_vect, second_vect,
5212 perm3_mask_low);
5213 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5215 /* Create interleaving stmt (high part of):
5216 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5217 ...}> */
5218 first_vect = data_ref;
5219 second_vect = dr_chain[2];
5220 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5221 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5222 first_vect, second_vect,
5223 perm3_mask_high);
5224 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5225 (*result_chain)[k] = data_ref;
5228 else
5230 /* If length is not equal to 3 then only power of 2 is supported. */
5231 gcc_assert (exact_log2 (length) != -1);
5233 for (i = 0; i < nelt; ++i)
5234 sel[i] = i * 2;
5235 perm_mask_even = vect_gen_perm_mask (vectype, sel);
5236 gcc_assert (perm_mask_even != NULL);
5238 for (i = 0; i < nelt; ++i)
5239 sel[i] = i * 2 + 1;
5240 perm_mask_odd = vect_gen_perm_mask (vectype, sel);
5241 gcc_assert (perm_mask_odd != NULL);
5243 for (i = 0; i < log_length; i++)
5245 for (j = 0; j < length; j += 2)
5247 first_vect = dr_chain[j];
5248 second_vect = dr_chain[j+1];
5250 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5251 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5252 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5253 first_vect, second_vect,
5254 perm_mask_even);
5255 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5256 (*result_chain)[j/2] = data_ref;
5258 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5259 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5260 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5261 first_vect, second_vect,
5262 perm_mask_odd);
5263 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5264 (*result_chain)[j/2+length/2] = data_ref;
5266 memcpy (dr_chain.address (), result_chain->address (),
5267 length * sizeof (tree));
5272 /* Function vect_shift_permute_load_chain.
5274 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5275 sequence of stmts to reorder the input data accordingly.
5276 Return the final references for loads in RESULT_CHAIN.
5277 Return true if successed, false otherwise.
5279 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5280 The input is 3 vectors each containing 8 elements. We assign a
5281 number to each element, the input sequence is:
5283 1st vec: 0 1 2 3 4 5 6 7
5284 2nd vec: 8 9 10 11 12 13 14 15
5285 3rd vec: 16 17 18 19 20 21 22 23
5287 The output sequence should be:
5289 1st vec: 0 3 6 9 12 15 18 21
5290 2nd vec: 1 4 7 10 13 16 19 22
5291 3rd vec: 2 5 8 11 14 17 20 23
5293 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5295 First we shuffle all 3 vectors to get correct elements order:
5297 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5298 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5299 3rd vec: (16 19 22) (17 20 23) (18 21)
5301 Next we unite and shift vector 3 times:
5303 1st step:
5304 shift right by 6 the concatenation of:
5305 "1st vec" and "2nd vec"
5306 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5307 "2nd vec" and "3rd vec"
5308 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5309 "3rd vec" and "1st vec"
5310 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5311 | New vectors |
5313 So that now new vectors are:
5315 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5316 2nd vec: (10 13) (16 19 22) (17 20 23)
5317 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5319 2nd step:
5320 shift right by 5 the concatenation of:
5321 "1st vec" and "3rd vec"
5322 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5323 "2nd vec" and "1st vec"
5324 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5325 "3rd vec" and "2nd vec"
5326 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5327 | New vectors |
5329 So that now new vectors are:
5331 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5332 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5333 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5335 3rd step:
5336 shift right by 5 the concatenation of:
5337 "1st vec" and "1st vec"
5338 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5339 shift right by 3 the concatenation of:
5340 "2nd vec" and "2nd vec"
5341 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5342 | New vectors |
5344 So that now all vectors are READY:
5345 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5346 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5347 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5349 This algorithm is faster than one in vect_permute_load_chain if:
5350 1. "shift of a concatination" is faster than general permutation.
5351 This is usually so.
5352 2. The TARGET machine can't execute vector instructions in parallel.
5353 This is because each step of the algorithm depends on previous.
5354 The algorithm in vect_permute_load_chain is much more parallel.
5356 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5359 static bool
5360 vect_shift_permute_load_chain (vec<tree> dr_chain,
5361 unsigned int length,
5362 gimple stmt,
5363 gimple_stmt_iterator *gsi,
5364 vec<tree> *result_chain)
5366 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5367 tree perm2_mask1, perm2_mask2, perm3_mask;
5368 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5369 gimple perm_stmt;
5371 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5372 unsigned int i;
5373 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5374 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5375 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5376 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5378 result_chain->quick_grow (length);
5379 memcpy (result_chain->address (), dr_chain.address (),
5380 length * sizeof (tree));
5382 if (length == 2 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5384 for (i = 0; i < nelt / 2; ++i)
5385 sel[i] = i * 2;
5386 for (i = 0; i < nelt / 2; ++i)
5387 sel[nelt / 2 + i] = i * 2 + 1;
5388 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5390 if (dump_enabled_p ())
5391 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5392 "shuffle of 2 fields structure is not \
5393 supported by target\n");
5394 return false;
5396 perm2_mask1 = vect_gen_perm_mask (vectype, sel);
5397 gcc_assert (perm2_mask1 != NULL);
5399 for (i = 0; i < nelt / 2; ++i)
5400 sel[i] = i * 2 + 1;
5401 for (i = 0; i < nelt / 2; ++i)
5402 sel[nelt / 2 + i] = i * 2;
5403 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5405 if (dump_enabled_p ())
5406 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5407 "shuffle of 2 fields structure is not \
5408 supported by target\n");
5409 return false;
5411 perm2_mask2 = vect_gen_perm_mask (vectype, sel);
5412 gcc_assert (perm2_mask2 != NULL);
5414 /* Generating permutation constant to shift all elements.
5415 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5416 for (i = 0; i < nelt; i++)
5417 sel[i] = nelt / 2 + i;
5418 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5420 if (dump_enabled_p ())
5421 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5422 "shift permutation is not supported by target\n");
5423 return false;
5425 shift1_mask = vect_gen_perm_mask (vectype, sel);
5426 gcc_assert (shift1_mask != NULL);
5428 /* Generating permutation constant to select vector from 2.
5429 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5430 for (i = 0; i < nelt / 2; i++)
5431 sel[i] = i;
5432 for (i = nelt / 2; i < nelt; i++)
5433 sel[i] = nelt + i;
5434 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5436 if (dump_enabled_p ())
5437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5438 "select is not supported by target\n");
5439 return false;
5441 select_mask = vect_gen_perm_mask (vectype, sel);
5442 gcc_assert (select_mask != NULL);
5444 first_vect = dr_chain[0];
5445 second_vect = dr_chain[1];
5447 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5448 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5449 first_vect, first_vect,
5450 perm2_mask1);
5451 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5452 vect[0] = data_ref;
5454 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5455 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5456 second_vect, second_vect,
5457 perm2_mask2);
5458 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5459 vect[1] = data_ref;
5461 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5462 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5463 vect[0], vect[1],
5464 shift1_mask);
5465 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5466 (*result_chain)[1] = data_ref;
5468 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5469 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5470 vect[0], vect[1],
5471 select_mask);
5472 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5473 (*result_chain)[0] = data_ref;
5475 return true;
5477 if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
5479 unsigned int k = 0, l = 0;
5481 /* Generating permutation constant to get all elements in rigth order.
5482 For vector length 8 it is {0 3 6 1 4 7 2 5}. */
5483 for (i = 0; i < nelt; i++)
5485 if (3 * k + (l % 3) >= nelt)
5487 k = 0;
5488 l += (3 - (nelt % 3));
5490 sel[i] = 3 * k + (l % 3);
5491 k++;
5493 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5495 if (dump_enabled_p ())
5496 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5497 "shuffle of 3 fields structure is not \
5498 supported by target\n");
5499 return false;
5501 perm3_mask = vect_gen_perm_mask (vectype, sel);
5502 gcc_assert (perm3_mask != NULL);
5504 /* Generating permutation constant to shift all elements.
5505 For vector length 8 it is {6 7 8 9 10 11 12 13}. */
5506 for (i = 0; i < nelt; i++)
5507 sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
5508 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5510 if (dump_enabled_p ())
5511 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5512 "shift permutation is not supported by target\n");
5513 return false;
5515 shift1_mask = vect_gen_perm_mask (vectype, sel);
5516 gcc_assert (shift1_mask != NULL);
5518 /* Generating permutation constant to shift all elements.
5519 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5520 for (i = 0; i < nelt; i++)
5521 sel[i] = 2 * (nelt / 3) + 1 + i;
5522 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5524 if (dump_enabled_p ())
5525 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5526 "shift permutation is not supported by target\n");
5527 return false;
5529 shift2_mask = vect_gen_perm_mask (vectype, sel);
5530 gcc_assert (shift2_mask != NULL);
5532 /* Generating permutation constant to shift all elements.
5533 For vector length 8 it is {3 4 5 6 7 8 9 10}. */
5534 for (i = 0; i < nelt; i++)
5535 sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
5536 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5538 if (dump_enabled_p ())
5539 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5540 "shift permutation is not supported by target\n");
5541 return false;
5543 shift3_mask = vect_gen_perm_mask (vectype, sel);
5544 gcc_assert (shift3_mask != NULL);
5546 /* Generating permutation constant to shift all elements.
5547 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5548 for (i = 0; i < nelt; i++)
5549 sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
5550 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5552 if (dump_enabled_p ())
5553 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5554 "shift permutation is not supported by target\n");
5555 return false;
5557 shift4_mask = vect_gen_perm_mask (vectype, sel);
5558 gcc_assert (shift4_mask != NULL);
5560 for (k = 0; k < 3; k++)
5562 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
5563 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5564 dr_chain[k], dr_chain[k],
5565 perm3_mask);
5566 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5567 vect[k] = data_ref;
5570 for (k = 0; k < 3; k++)
5572 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
5573 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5574 vect[k % 3],
5575 vect[(k + 1) % 3],
5576 shift1_mask);
5577 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5578 vect_shift[k] = data_ref;
5581 for (k = 0; k < 3; k++)
5583 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
5584 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5585 vect_shift[(4 - k) % 3],
5586 vect_shift[(3 - k) % 3],
5587 shift2_mask);
5588 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5589 vect[k] = data_ref;
5592 (*result_chain)[3 - (nelt % 3)] = vect[2];
5594 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
5595 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5596 vect[0], vect[0],
5597 shift3_mask);
5598 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5599 (*result_chain)[nelt % 3] = data_ref;
5601 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
5602 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5603 vect[1], vect[1],
5604 shift4_mask);
5605 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5606 (*result_chain)[0] = data_ref;
5607 return true;
5609 return false;
5612 /* Function vect_transform_grouped_load.
5614 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5615 to perform their permutation and ascribe the result vectorized statements to
5616 the scalar statements.
5619 void
5620 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
5621 gimple_stmt_iterator *gsi)
5623 machine_mode mode;
5624 vec<tree> result_chain = vNULL;
5626 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5627 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5628 vectors, that are ready for vector computation. */
5629 result_chain.create (size);
5631 /* If reassociation width for vector type is 2 or greater target machine can
5632 execute 2 or more vector instructions in parallel. Otherwise try to
5633 get chain for loads group using vect_shift_permute_load_chain. */
5634 mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
5635 if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
5636 || !vect_shift_permute_load_chain (dr_chain, size, stmt,
5637 gsi, &result_chain))
5638 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5639 vect_record_grouped_load_vectors (stmt, result_chain);
5640 result_chain.release ();
5643 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5644 generated as part of the vectorization of STMT. Assign the statement
5645 for each vector to the associated scalar statement. */
5647 void
5648 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
5650 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5651 gimple next_stmt, new_stmt;
5652 unsigned int i, gap_count;
5653 tree tmp_data_ref;
5655 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5656 Since we scan the chain starting from it's first node, their order
5657 corresponds the order of data-refs in RESULT_CHAIN. */
5658 next_stmt = first_stmt;
5659 gap_count = 1;
5660 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5662 if (!next_stmt)
5663 break;
5665 /* Skip the gaps. Loads created for the gaps will be removed by dead
5666 code elimination pass later. No need to check for the first stmt in
5667 the group, since it always exists.
5668 GROUP_GAP is the number of steps in elements from the previous
5669 access (if there is no gap GROUP_GAP is 1). We skip loads that
5670 correspond to the gaps. */
5671 if (next_stmt != first_stmt
5672 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5674 gap_count++;
5675 continue;
5678 while (next_stmt)
5680 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5681 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5682 copies, and we put the new vector statement in the first available
5683 RELATED_STMT. */
5684 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5685 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5686 else
5688 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5690 gimple prev_stmt =
5691 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5692 gimple rel_stmt =
5693 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5694 while (rel_stmt)
5696 prev_stmt = rel_stmt;
5697 rel_stmt =
5698 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5701 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5702 new_stmt;
5706 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5707 gap_count = 1;
5708 /* If NEXT_STMT accesses the same DR as the previous statement,
5709 put the same TMP_DATA_REF as its vectorized statement; otherwise
5710 get the next data-ref from RESULT_CHAIN. */
5711 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5712 break;
5717 /* Function vect_force_dr_alignment_p.
5719 Returns whether the alignment of a DECL can be forced to be aligned
5720 on ALIGNMENT bit boundary. */
5722 bool
5723 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5725 if (TREE_CODE (decl) != VAR_DECL)
5726 return false;
5728 /* With -fno-toplevel-reorder we may have already output the constant. */
5729 if (TREE_ASM_WRITTEN (decl))
5730 return false;
5732 /* Constant pool entries may be shared and not properly merged by LTO. */
5733 if (DECL_IN_CONSTANT_POOL (decl))
5734 return false;
5736 if (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
5738 symtab_node *snode;
5740 /* We cannot change alignment of symbols that may bind to symbols
5741 in other translation unit that may contain a definition with lower
5742 alignment. */
5743 if (!decl_binds_to_current_def_p (decl))
5744 return false;
5746 /* When compiling partition, be sure the symbol is not output by other
5747 partition. */
5748 snode = symtab_node::get (decl);
5749 if (flag_ltrans
5750 && (snode->in_other_partition
5751 || snode->get_partitioning_class () == SYMBOL_DUPLICATE))
5752 return false;
5755 /* Do not override the alignment as specified by the ABI when the used
5756 attribute is set. */
5757 if (DECL_PRESERVE_P (decl))
5758 return false;
5760 /* Do not override explicit alignment set by the user when an explicit
5761 section name is also used. This is a common idiom used by many
5762 software projects. */
5763 if (TREE_STATIC (decl)
5764 && DECL_SECTION_NAME (decl) != NULL
5765 && !symtab_node::get (decl)->implicit_section)
5766 return false;
5768 /* If symbol is an alias, we need to check that target is OK. */
5769 if (TREE_STATIC (decl))
5771 tree target = symtab_node::get (decl)->ultimate_alias_target ()->decl;
5772 if (target != decl)
5774 if (DECL_PRESERVE_P (target))
5775 return false;
5776 decl = target;
5780 if (TREE_STATIC (decl))
5781 return (alignment <= MAX_OFILE_ALIGNMENT);
5782 else
5783 return (alignment <= MAX_STACK_ALIGNMENT);
5787 /* Return whether the data reference DR is supported with respect to its
5788 alignment.
5789 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5790 it is aligned, i.e., check if it is possible to vectorize it with different
5791 alignment. */
5793 enum dr_alignment_support
5794 vect_supportable_dr_alignment (struct data_reference *dr,
5795 bool check_aligned_accesses)
5797 gimple stmt = DR_STMT (dr);
5798 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5799 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5800 machine_mode mode = TYPE_MODE (vectype);
5801 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5802 struct loop *vect_loop = NULL;
5803 bool nested_in_vect_loop = false;
5805 if (aligned_access_p (dr) && !check_aligned_accesses)
5806 return dr_aligned;
5808 /* For now assume all conditional loads/stores support unaligned
5809 access without any special code. */
5810 if (is_gimple_call (stmt)
5811 && gimple_call_internal_p (stmt)
5812 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
5813 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
5814 return dr_unaligned_supported;
5816 if (loop_vinfo)
5818 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5819 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5822 /* Possibly unaligned access. */
5824 /* We can choose between using the implicit realignment scheme (generating
5825 a misaligned_move stmt) and the explicit realignment scheme (generating
5826 aligned loads with a REALIGN_LOAD). There are two variants to the
5827 explicit realignment scheme: optimized, and unoptimized.
5828 We can optimize the realignment only if the step between consecutive
5829 vector loads is equal to the vector size. Since the vector memory
5830 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5831 is guaranteed that the misalignment amount remains the same throughout the
5832 execution of the vectorized loop. Therefore, we can create the
5833 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5834 at the loop preheader.
5836 However, in the case of outer-loop vectorization, when vectorizing a
5837 memory access in the inner-loop nested within the LOOP that is now being
5838 vectorized, while it is guaranteed that the misalignment of the
5839 vectorized memory access will remain the same in different outer-loop
5840 iterations, it is *not* guaranteed that is will remain the same throughout
5841 the execution of the inner-loop. This is because the inner-loop advances
5842 with the original scalar step (and not in steps of VS). If the inner-loop
5843 step happens to be a multiple of VS, then the misalignment remains fixed
5844 and we can use the optimized realignment scheme. For example:
5846 for (i=0; i<N; i++)
5847 for (j=0; j<M; j++)
5848 s += a[i+j];
5850 When vectorizing the i-loop in the above example, the step between
5851 consecutive vector loads is 1, and so the misalignment does not remain
5852 fixed across the execution of the inner-loop, and the realignment cannot
5853 be optimized (as illustrated in the following pseudo vectorized loop):
5855 for (i=0; i<N; i+=4)
5856 for (j=0; j<M; j++){
5857 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5858 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5859 // (assuming that we start from an aligned address).
5862 We therefore have to use the unoptimized realignment scheme:
5864 for (i=0; i<N; i+=4)
5865 for (j=k; j<M; j+=4)
5866 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5867 // that the misalignment of the initial address is
5868 // 0).
5870 The loop can then be vectorized as follows:
5872 for (k=0; k<4; k++){
5873 rt = get_realignment_token (&vp[k]);
5874 for (i=0; i<N; i+=4){
5875 v1 = vp[i+k];
5876 for (j=k; j<M; j+=4){
5877 v2 = vp[i+j+VS-1];
5878 va = REALIGN_LOAD <v1,v2,rt>;
5879 vs += va;
5880 v1 = v2;
5883 } */
5885 if (DR_IS_READ (dr))
5887 bool is_packed = false;
5888 tree type = (TREE_TYPE (DR_REF (dr)));
5890 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5891 && (!targetm.vectorize.builtin_mask_for_load
5892 || targetm.vectorize.builtin_mask_for_load ()))
5894 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5895 if ((nested_in_vect_loop
5896 && (TREE_INT_CST_LOW (DR_STEP (dr))
5897 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5898 || !loop_vinfo)
5899 return dr_explicit_realign;
5900 else
5901 return dr_explicit_realign_optimized;
5903 if (!known_alignment_for_access_p (dr))
5904 is_packed = not_size_aligned (DR_REF (dr));
5906 if ((TYPE_USER_ALIGN (type) && !is_packed)
5907 || targetm.vectorize.
5908 support_vector_misalignment (mode, type,
5909 DR_MISALIGNMENT (dr), is_packed))
5910 /* Can't software pipeline the loads, but can at least do them. */
5911 return dr_unaligned_supported;
5913 else
5915 bool is_packed = false;
5916 tree type = (TREE_TYPE (DR_REF (dr)));
5918 if (!known_alignment_for_access_p (dr))
5919 is_packed = not_size_aligned (DR_REF (dr));
5921 if ((TYPE_USER_ALIGN (type) && !is_packed)
5922 || targetm.vectorize.
5923 support_vector_misalignment (mode, type,
5924 DR_MISALIGNMENT (dr), is_packed))
5925 return dr_unaligned_supported;
5928 /* Unsupported. */
5929 return dr_unaligned_unsupported;