gcc/
[official-gcc.git] / gcc / tree-vect-data-refs.c
blobd07885fd1438fac6119f7b437bf5e4daea2b2b08
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 /* Function vect_vfa_segment_size.
2723 Create an expression that computes the size of segment
2724 that will be accessed for a data reference. The functions takes into
2725 account that realignment loads may access one more vector.
2727 Input:
2728 DR: The data reference.
2729 LENGTH_FACTOR: segment length to consider.
2731 Return an expression whose value is the size of segment which will be
2732 accessed by DR. */
2734 static tree
2735 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2737 tree segment_length;
2739 if (integer_zerop (DR_STEP (dr)))
2740 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2741 else
2742 segment_length = size_binop (MULT_EXPR,
2743 fold_convert (sizetype, DR_STEP (dr)),
2744 fold_convert (sizetype, length_factor));
2746 if (vect_supportable_dr_alignment (dr, false)
2747 == dr_explicit_realign_optimized)
2749 tree vector_size = TYPE_SIZE_UNIT
2750 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2752 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2754 return segment_length;
2757 /* Function vect_prune_runtime_alias_test_list.
2759 Prune a list of ddrs to be tested at run-time by versioning for alias.
2760 Merge several alias checks into one if possible.
2761 Return FALSE if resulting list of ddrs is longer then allowed by
2762 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2764 bool
2765 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2767 vec<ddr_p> may_alias_ddrs =
2768 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2769 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2770 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2771 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2772 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2774 ddr_p ddr;
2775 unsigned int i;
2776 tree length_factor;
2778 if (dump_enabled_p ())
2779 dump_printf_loc (MSG_NOTE, vect_location,
2780 "=== vect_prune_runtime_alias_test_list ===\n");
2782 if (may_alias_ddrs.is_empty ())
2783 return true;
2785 /* Basically, for each pair of dependent data refs store_ptr_0
2786 and load_ptr_0, we create an expression:
2788 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2789 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2791 for aliasing checks. However, in some cases we can decrease
2792 the number of checks by combining two checks into one. For
2793 example, suppose we have another pair of data refs store_ptr_0
2794 and load_ptr_1, and if the following condition is satisfied:
2796 load_ptr_0 < load_ptr_1 &&
2797 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2799 (this condition means, in each iteration of vectorized loop,
2800 the accessed memory of store_ptr_0 cannot be between the memory
2801 of load_ptr_0 and load_ptr_1.)
2803 we then can use only the following expression to finish the
2804 alising checks between store_ptr_0 & load_ptr_0 and
2805 store_ptr_0 & load_ptr_1:
2807 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2808 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2810 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2811 same basic address. */
2813 comp_alias_ddrs.create (may_alias_ddrs.length ());
2815 /* First, we collect all data ref pairs for aliasing checks. */
2816 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2818 struct data_reference *dr_a, *dr_b;
2819 gimple dr_group_first_a, dr_group_first_b;
2820 tree segment_length_a, segment_length_b;
2821 gimple stmt_a, stmt_b;
2823 dr_a = DDR_A (ddr);
2824 stmt_a = DR_STMT (DDR_A (ddr));
2825 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2826 if (dr_group_first_a)
2828 stmt_a = dr_group_first_a;
2829 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2832 dr_b = DDR_B (ddr);
2833 stmt_b = DR_STMT (DDR_B (ddr));
2834 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2835 if (dr_group_first_b)
2837 stmt_b = dr_group_first_b;
2838 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2841 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2842 length_factor = scalar_loop_iters;
2843 else
2844 length_factor = size_int (vect_factor);
2845 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2846 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2848 dr_with_seg_len_pair_t dr_with_seg_len_pair
2849 (dr_with_seg_len (dr_a, segment_length_a),
2850 dr_with_seg_len (dr_b, segment_length_b));
2852 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2853 std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2855 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2858 /* Second, we sort the collected data ref pairs so that we can scan
2859 them once to combine all possible aliasing checks. */
2860 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2862 /* Third, we scan the sorted dr pairs and check if we can combine
2863 alias checks of two neighbouring dr pairs. */
2864 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2866 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2867 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2868 *dr_b1 = &comp_alias_ddrs[i-1].second,
2869 *dr_a2 = &comp_alias_ddrs[i].first,
2870 *dr_b2 = &comp_alias_ddrs[i].second;
2872 /* Remove duplicate data ref pairs. */
2873 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2875 if (dump_enabled_p ())
2877 dump_printf_loc (MSG_NOTE, vect_location,
2878 "found equal ranges ");
2879 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2880 DR_REF (dr_a1->dr));
2881 dump_printf (MSG_NOTE, ", ");
2882 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2883 DR_REF (dr_b1->dr));
2884 dump_printf (MSG_NOTE, " and ");
2885 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2886 DR_REF (dr_a2->dr));
2887 dump_printf (MSG_NOTE, ", ");
2888 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2889 DR_REF (dr_b2->dr));
2890 dump_printf (MSG_NOTE, "\n");
2893 comp_alias_ddrs.ordered_remove (i--);
2894 continue;
2897 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2899 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2900 and DR_A1 and DR_A2 are two consecutive memrefs. */
2901 if (*dr_a1 == *dr_a2)
2903 std::swap (dr_a1, dr_b1);
2904 std::swap (dr_a2, dr_b2);
2907 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2908 DR_BASE_ADDRESS (dr_a2->dr),
2910 || !tree_fits_shwi_p (dr_a1->offset)
2911 || !tree_fits_shwi_p (dr_a2->offset))
2912 continue;
2914 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2915 - tree_to_shwi (dr_a1->offset));
2918 /* Now we check if the following condition is satisfied:
2920 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2922 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2923 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2924 have to make a best estimation. We can get the minimum value
2925 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2926 then either of the following two conditions can guarantee the
2927 one above:
2929 1: DIFF <= MIN_SEG_LEN_B
2930 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2934 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2935 ? tree_to_shwi (dr_b1->seg_len)
2936 : vect_factor);
2938 if (diff <= min_seg_len_b
2939 || (tree_fits_shwi_p (dr_a1->seg_len)
2940 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2942 if (dump_enabled_p ())
2944 dump_printf_loc (MSG_NOTE, vect_location,
2945 "merging ranges for ");
2946 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2947 DR_REF (dr_a1->dr));
2948 dump_printf (MSG_NOTE, ", ");
2949 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2950 DR_REF (dr_b1->dr));
2951 dump_printf (MSG_NOTE, " and ");
2952 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2953 DR_REF (dr_a2->dr));
2954 dump_printf (MSG_NOTE, ", ");
2955 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2956 DR_REF (dr_b2->dr));
2957 dump_printf (MSG_NOTE, "\n");
2960 dr_a1->seg_len = size_binop (PLUS_EXPR,
2961 dr_a2->seg_len, size_int (diff));
2962 comp_alias_ddrs.ordered_remove (i--);
2967 dump_printf_loc (MSG_NOTE, vect_location,
2968 "improved number of alias checks from %d to %d\n",
2969 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2970 if ((int) comp_alias_ddrs.length () >
2971 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2972 return false;
2974 return true;
2977 /* Check whether a non-affine read in stmt is suitable for gather load
2978 and if so, return a builtin decl for that operation. */
2980 tree
2981 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2982 tree *offp, int *scalep)
2984 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2985 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2986 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2987 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2988 tree offtype = NULL_TREE;
2989 tree decl, base, off;
2990 machine_mode pmode;
2991 int punsignedp, pvolatilep;
2993 base = DR_REF (dr);
2994 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
2995 see if we can use the def stmt of the address. */
2996 if (is_gimple_call (stmt)
2997 && gimple_call_internal_p (stmt)
2998 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2999 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
3000 && TREE_CODE (base) == MEM_REF
3001 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3002 && integer_zerop (TREE_OPERAND (base, 1))
3003 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
3005 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
3006 if (is_gimple_assign (def_stmt)
3007 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3008 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3011 /* The gather builtins need address of the form
3012 loop_invariant + vector * {1, 2, 4, 8}
3014 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3015 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3016 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3017 multiplications and additions in it. To get a vector, we need
3018 a single SSA_NAME that will be defined in the loop and will
3019 contain everything that is not loop invariant and that can be
3020 vectorized. The following code attempts to find such a preexistng
3021 SSA_NAME OFF and put the loop invariants into a tree BASE
3022 that can be gimplified before the loop. */
3023 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
3024 &pmode, &punsignedp, &pvolatilep, false);
3025 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
3027 if (TREE_CODE (base) == MEM_REF)
3029 if (!integer_zerop (TREE_OPERAND (base, 1)))
3031 if (off == NULL_TREE)
3033 offset_int moff = mem_ref_offset (base);
3034 off = wide_int_to_tree (sizetype, moff);
3036 else
3037 off = size_binop (PLUS_EXPR, off,
3038 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3040 base = TREE_OPERAND (base, 0);
3042 else
3043 base = build_fold_addr_expr (base);
3045 if (off == NULL_TREE)
3046 off = size_zero_node;
3048 /* If base is not loop invariant, either off is 0, then we start with just
3049 the constant offset in the loop invariant BASE and continue with base
3050 as OFF, otherwise give up.
3051 We could handle that case by gimplifying the addition of base + off
3052 into some SSA_NAME and use that as off, but for now punt. */
3053 if (!expr_invariant_in_loop_p (loop, base))
3055 if (!integer_zerop (off))
3056 return NULL_TREE;
3057 off = base;
3058 base = size_int (pbitpos / BITS_PER_UNIT);
3060 /* Otherwise put base + constant offset into the loop invariant BASE
3061 and continue with OFF. */
3062 else
3064 base = fold_convert (sizetype, base);
3065 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3068 /* OFF at this point may be either a SSA_NAME or some tree expression
3069 from get_inner_reference. Try to peel off loop invariants from it
3070 into BASE as long as possible. */
3071 STRIP_NOPS (off);
3072 while (offtype == NULL_TREE)
3074 enum tree_code code;
3075 tree op0, op1, add = NULL_TREE;
3077 if (TREE_CODE (off) == SSA_NAME)
3079 gimple def_stmt = SSA_NAME_DEF_STMT (off);
3081 if (expr_invariant_in_loop_p (loop, off))
3082 return NULL_TREE;
3084 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3085 break;
3087 op0 = gimple_assign_rhs1 (def_stmt);
3088 code = gimple_assign_rhs_code (def_stmt);
3089 op1 = gimple_assign_rhs2 (def_stmt);
3091 else
3093 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3094 return NULL_TREE;
3095 code = TREE_CODE (off);
3096 extract_ops_from_tree (off, &code, &op0, &op1);
3098 switch (code)
3100 case POINTER_PLUS_EXPR:
3101 case PLUS_EXPR:
3102 if (expr_invariant_in_loop_p (loop, op0))
3104 add = op0;
3105 off = op1;
3106 do_add:
3107 add = fold_convert (sizetype, add);
3108 if (scale != 1)
3109 add = size_binop (MULT_EXPR, add, size_int (scale));
3110 base = size_binop (PLUS_EXPR, base, add);
3111 continue;
3113 if (expr_invariant_in_loop_p (loop, op1))
3115 add = op1;
3116 off = op0;
3117 goto do_add;
3119 break;
3120 case MINUS_EXPR:
3121 if (expr_invariant_in_loop_p (loop, op1))
3123 add = fold_convert (sizetype, op1);
3124 add = size_binop (MINUS_EXPR, size_zero_node, add);
3125 off = op0;
3126 goto do_add;
3128 break;
3129 case MULT_EXPR:
3130 if (scale == 1 && tree_fits_shwi_p (op1))
3132 scale = tree_to_shwi (op1);
3133 off = op0;
3134 continue;
3136 break;
3137 case SSA_NAME:
3138 off = op0;
3139 continue;
3140 CASE_CONVERT:
3141 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3142 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3143 break;
3144 if (TYPE_PRECISION (TREE_TYPE (op0))
3145 == TYPE_PRECISION (TREE_TYPE (off)))
3147 off = op0;
3148 continue;
3150 if (TYPE_PRECISION (TREE_TYPE (op0))
3151 < TYPE_PRECISION (TREE_TYPE (off)))
3153 off = op0;
3154 offtype = TREE_TYPE (off);
3155 STRIP_NOPS (off);
3156 continue;
3158 break;
3159 default:
3160 break;
3162 break;
3165 /* If at the end OFF still isn't a SSA_NAME or isn't
3166 defined in the loop, punt. */
3167 if (TREE_CODE (off) != SSA_NAME
3168 || expr_invariant_in_loop_p (loop, off))
3169 return NULL_TREE;
3171 if (offtype == NULL_TREE)
3172 offtype = TREE_TYPE (off);
3174 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3175 offtype, scale);
3176 if (decl == NULL_TREE)
3177 return NULL_TREE;
3179 if (basep)
3180 *basep = base;
3181 if (offp)
3182 *offp = off;
3183 if (scalep)
3184 *scalep = scale;
3185 return decl;
3188 /* Function vect_analyze_data_refs.
3190 Find all the data references in the loop or basic block.
3192 The general structure of the analysis of data refs in the vectorizer is as
3193 follows:
3194 1- vect_analyze_data_refs(loop/bb): call
3195 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3196 in the loop/bb and their dependences.
3197 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3198 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3199 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3203 bool
3204 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3205 bb_vec_info bb_vinfo,
3206 int *min_vf, unsigned *n_stmts)
3208 struct loop *loop = NULL;
3209 basic_block bb = NULL;
3210 unsigned int i;
3211 vec<data_reference_p> datarefs;
3212 struct data_reference *dr;
3213 tree scalar_type;
3215 if (dump_enabled_p ())
3216 dump_printf_loc (MSG_NOTE, vect_location,
3217 "=== vect_analyze_data_refs ===\n");
3219 if (loop_vinfo)
3221 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3223 loop = LOOP_VINFO_LOOP (loop_vinfo);
3224 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3225 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3227 if (dump_enabled_p ())
3228 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3229 "not vectorized: loop contains function calls"
3230 " or data references that cannot be analyzed\n");
3231 return false;
3234 for (i = 0; i < loop->num_nodes; i++)
3236 gimple_stmt_iterator gsi;
3238 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3240 gimple stmt = gsi_stmt (gsi);
3241 if (is_gimple_debug (stmt))
3242 continue;
3243 ++*n_stmts;
3244 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3246 if (is_gimple_call (stmt) && loop->safelen)
3248 tree fndecl = gimple_call_fndecl (stmt), op;
3249 if (fndecl != NULL_TREE)
3251 struct cgraph_node *node = cgraph_node::get (fndecl);
3252 if (node != NULL && node->simd_clones != NULL)
3254 unsigned int j, n = gimple_call_num_args (stmt);
3255 for (j = 0; j < n; j++)
3257 op = gimple_call_arg (stmt, j);
3258 if (DECL_P (op)
3259 || (REFERENCE_CLASS_P (op)
3260 && get_base_address (op)))
3261 break;
3263 op = gimple_call_lhs (stmt);
3264 /* Ignore #pragma omp declare simd functions
3265 if they don't have data references in the
3266 call stmt itself. */
3267 if (j == n
3268 && !(op
3269 && (DECL_P (op)
3270 || (REFERENCE_CLASS_P (op)
3271 && get_base_address (op)))))
3272 continue;
3276 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3277 if (dump_enabled_p ())
3278 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3279 "not vectorized: loop contains function "
3280 "calls or data references that cannot "
3281 "be analyzed\n");
3282 return false;
3287 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3289 else
3291 gimple_stmt_iterator gsi;
3293 bb = BB_VINFO_BB (bb_vinfo);
3294 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3296 gimple stmt = gsi_stmt (gsi);
3297 if (is_gimple_debug (stmt))
3298 continue;
3299 ++*n_stmts;
3300 if (!find_data_references_in_stmt (NULL, stmt,
3301 &BB_VINFO_DATAREFS (bb_vinfo)))
3303 /* Mark the rest of the basic-block as unvectorizable. */
3304 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3306 stmt = gsi_stmt (gsi);
3307 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3309 break;
3313 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3316 /* Go through the data-refs, check that the analysis succeeded. Update
3317 pointer from stmt_vec_info struct to DR and vectype. */
3319 FOR_EACH_VEC_ELT (datarefs, i, dr)
3321 gimple stmt;
3322 stmt_vec_info stmt_info;
3323 tree base, offset, init;
3324 bool gather = false;
3325 bool simd_lane_access = false;
3326 int vf;
3328 again:
3329 if (!dr || !DR_REF (dr))
3331 if (dump_enabled_p ())
3332 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3333 "not vectorized: unhandled data-ref\n");
3334 return false;
3337 stmt = DR_STMT (dr);
3338 stmt_info = vinfo_for_stmt (stmt);
3340 /* Discard clobbers from the dataref vector. We will remove
3341 clobber stmts during vectorization. */
3342 if (gimple_clobber_p (stmt))
3344 free_data_ref (dr);
3345 if (i == datarefs.length () - 1)
3347 datarefs.pop ();
3348 break;
3350 datarefs.ordered_remove (i);
3351 dr = datarefs[i];
3352 goto again;
3355 /* Check that analysis of the data-ref succeeded. */
3356 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3357 || !DR_STEP (dr))
3359 bool maybe_gather
3360 = DR_IS_READ (dr)
3361 && !TREE_THIS_VOLATILE (DR_REF (dr))
3362 && targetm.vectorize.builtin_gather != NULL;
3363 bool maybe_simd_lane_access
3364 = loop_vinfo && loop->simduid;
3366 /* If target supports vector gather loads, or if this might be
3367 a SIMD lane access, see if they can't be used. */
3368 if (loop_vinfo
3369 && (maybe_gather || maybe_simd_lane_access)
3370 && !nested_in_vect_loop_p (loop, stmt))
3372 struct data_reference *newdr
3373 = create_data_ref (NULL, loop_containing_stmt (stmt),
3374 DR_REF (dr), stmt, true);
3375 gcc_assert (newdr != NULL && DR_REF (newdr));
3376 if (DR_BASE_ADDRESS (newdr)
3377 && DR_OFFSET (newdr)
3378 && DR_INIT (newdr)
3379 && DR_STEP (newdr)
3380 && integer_zerop (DR_STEP (newdr)))
3382 if (maybe_simd_lane_access)
3384 tree off = DR_OFFSET (newdr);
3385 STRIP_NOPS (off);
3386 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3387 && TREE_CODE (off) == MULT_EXPR
3388 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3390 tree step = TREE_OPERAND (off, 1);
3391 off = TREE_OPERAND (off, 0);
3392 STRIP_NOPS (off);
3393 if (CONVERT_EXPR_P (off)
3394 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3395 0)))
3396 < TYPE_PRECISION (TREE_TYPE (off)))
3397 off = TREE_OPERAND (off, 0);
3398 if (TREE_CODE (off) == SSA_NAME)
3400 gimple def = SSA_NAME_DEF_STMT (off);
3401 tree reft = TREE_TYPE (DR_REF (newdr));
3402 if (is_gimple_call (def)
3403 && gimple_call_internal_p (def)
3404 && (gimple_call_internal_fn (def)
3405 == IFN_GOMP_SIMD_LANE))
3407 tree arg = gimple_call_arg (def, 0);
3408 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3409 arg = SSA_NAME_VAR (arg);
3410 if (arg == loop->simduid
3411 /* For now. */
3412 && tree_int_cst_equal
3413 (TYPE_SIZE_UNIT (reft),
3414 step))
3416 DR_OFFSET (newdr) = ssize_int (0);
3417 DR_STEP (newdr) = step;
3418 DR_ALIGNED_TO (newdr)
3419 = size_int (BIGGEST_ALIGNMENT);
3420 dr = newdr;
3421 simd_lane_access = true;
3427 if (!simd_lane_access && maybe_gather)
3429 dr = newdr;
3430 gather = true;
3433 if (!gather && !simd_lane_access)
3434 free_data_ref (newdr);
3437 if (!gather && !simd_lane_access)
3439 if (dump_enabled_p ())
3441 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3442 "not vectorized: data ref analysis "
3443 "failed ");
3444 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3445 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3448 if (bb_vinfo)
3449 break;
3451 return false;
3455 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3457 if (dump_enabled_p ())
3458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3459 "not vectorized: base addr of dr is a "
3460 "constant\n");
3462 if (bb_vinfo)
3463 break;
3465 if (gather || simd_lane_access)
3466 free_data_ref (dr);
3467 return false;
3470 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3472 if (dump_enabled_p ())
3474 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3475 "not vectorized: volatile type ");
3476 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3477 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3480 if (bb_vinfo)
3481 break;
3483 return false;
3486 if (stmt_can_throw_internal (stmt))
3488 if (dump_enabled_p ())
3490 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3491 "not vectorized: statement can throw an "
3492 "exception ");
3493 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3494 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3497 if (bb_vinfo)
3498 break;
3500 if (gather || simd_lane_access)
3501 free_data_ref (dr);
3502 return false;
3505 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3506 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3508 if (dump_enabled_p ())
3510 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3511 "not vectorized: statement is bitfield "
3512 "access ");
3513 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3514 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3517 if (bb_vinfo)
3518 break;
3520 if (gather || simd_lane_access)
3521 free_data_ref (dr);
3522 return false;
3525 base = unshare_expr (DR_BASE_ADDRESS (dr));
3526 offset = unshare_expr (DR_OFFSET (dr));
3527 init = unshare_expr (DR_INIT (dr));
3529 if (is_gimple_call (stmt)
3530 && (!gimple_call_internal_p (stmt)
3531 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3532 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3534 if (dump_enabled_p ())
3536 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3537 "not vectorized: dr in a call ");
3538 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3539 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3542 if (bb_vinfo)
3543 break;
3545 if (gather || simd_lane_access)
3546 free_data_ref (dr);
3547 return false;
3550 /* Update DR field in stmt_vec_info struct. */
3552 /* If the dataref is in an inner-loop of the loop that is considered for
3553 for vectorization, we also want to analyze the access relative to
3554 the outer-loop (DR contains information only relative to the
3555 inner-most enclosing loop). We do that by building a reference to the
3556 first location accessed by the inner-loop, and analyze it relative to
3557 the outer-loop. */
3558 if (loop && nested_in_vect_loop_p (loop, stmt))
3560 tree outer_step, outer_base, outer_init;
3561 HOST_WIDE_INT pbitsize, pbitpos;
3562 tree poffset;
3563 machine_mode pmode;
3564 int punsignedp, pvolatilep;
3565 affine_iv base_iv, offset_iv;
3566 tree dinit;
3568 /* Build a reference to the first location accessed by the
3569 inner-loop: *(BASE+INIT). (The first location is actually
3570 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3571 tree inner_base = build_fold_indirect_ref
3572 (fold_build_pointer_plus (base, init));
3574 if (dump_enabled_p ())
3576 dump_printf_loc (MSG_NOTE, vect_location,
3577 "analyze in outer-loop: ");
3578 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3579 dump_printf (MSG_NOTE, "\n");
3582 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3583 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3584 gcc_assert (outer_base != NULL_TREE);
3586 if (pbitpos % BITS_PER_UNIT != 0)
3588 if (dump_enabled_p ())
3589 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3590 "failed: bit offset alignment.\n");
3591 return false;
3594 outer_base = build_fold_addr_expr (outer_base);
3595 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3596 &base_iv, false))
3598 if (dump_enabled_p ())
3599 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3600 "failed: evolution of base is not affine.\n");
3601 return false;
3604 if (offset)
3606 if (poffset)
3607 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3608 poffset);
3609 else
3610 poffset = offset;
3613 if (!poffset)
3615 offset_iv.base = ssize_int (0);
3616 offset_iv.step = ssize_int (0);
3618 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3619 &offset_iv, false))
3621 if (dump_enabled_p ())
3622 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3623 "evolution of offset is not affine.\n");
3624 return false;
3627 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3628 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3629 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3630 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3631 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3633 outer_step = size_binop (PLUS_EXPR,
3634 fold_convert (ssizetype, base_iv.step),
3635 fold_convert (ssizetype, offset_iv.step));
3637 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3638 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3639 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3640 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3641 STMT_VINFO_DR_OFFSET (stmt_info) =
3642 fold_convert (ssizetype, offset_iv.base);
3643 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3644 size_int (highest_pow2_factor (offset_iv.base));
3646 if (dump_enabled_p ())
3648 dump_printf_loc (MSG_NOTE, vect_location,
3649 "\touter base_address: ");
3650 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3651 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3652 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3653 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3654 STMT_VINFO_DR_OFFSET (stmt_info));
3655 dump_printf (MSG_NOTE,
3656 "\n\touter constant offset from base address: ");
3657 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3658 STMT_VINFO_DR_INIT (stmt_info));
3659 dump_printf (MSG_NOTE, "\n\touter step: ");
3660 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3661 STMT_VINFO_DR_STEP (stmt_info));
3662 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3663 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3664 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3665 dump_printf (MSG_NOTE, "\n");
3669 if (STMT_VINFO_DATA_REF (stmt_info))
3671 if (dump_enabled_p ())
3673 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3674 "not vectorized: more than one data ref "
3675 "in stmt: ");
3676 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3677 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3680 if (bb_vinfo)
3681 break;
3683 if (gather || simd_lane_access)
3684 free_data_ref (dr);
3685 return false;
3688 STMT_VINFO_DATA_REF (stmt_info) = dr;
3689 if (simd_lane_access)
3691 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3692 free_data_ref (datarefs[i]);
3693 datarefs[i] = dr;
3696 /* Set vectype for STMT. */
3697 scalar_type = TREE_TYPE (DR_REF (dr));
3698 STMT_VINFO_VECTYPE (stmt_info)
3699 = get_vectype_for_scalar_type (scalar_type);
3700 if (!STMT_VINFO_VECTYPE (stmt_info))
3702 if (dump_enabled_p ())
3704 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3705 "not vectorized: no vectype for stmt: ");
3706 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3707 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3708 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3709 scalar_type);
3710 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3713 if (bb_vinfo)
3714 break;
3716 if (gather || simd_lane_access)
3718 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3719 if (gather)
3720 free_data_ref (dr);
3722 return false;
3724 else
3726 if (dump_enabled_p ())
3728 dump_printf_loc (MSG_NOTE, vect_location,
3729 "got vectype for stmt: ");
3730 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3731 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3732 STMT_VINFO_VECTYPE (stmt_info));
3733 dump_printf (MSG_NOTE, "\n");
3737 /* Adjust the minimal vectorization factor according to the
3738 vector type. */
3739 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3740 if (vf > *min_vf)
3741 *min_vf = vf;
3743 if (gather)
3745 tree off;
3747 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3748 if (gather
3749 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3750 gather = false;
3751 if (!gather)
3753 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3754 free_data_ref (dr);
3755 if (dump_enabled_p ())
3757 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3758 "not vectorized: not suitable for gather "
3759 "load ");
3760 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3761 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3763 return false;
3766 datarefs[i] = dr;
3767 STMT_VINFO_GATHER_P (stmt_info) = true;
3769 else if (loop_vinfo
3770 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3772 if (nested_in_vect_loop_p (loop, stmt)
3773 || !DR_IS_READ (dr))
3775 if (dump_enabled_p ())
3777 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3778 "not vectorized: not suitable for strided "
3779 "load ");
3780 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3781 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3783 return false;
3785 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3789 /* If we stopped analysis at the first dataref we could not analyze
3790 when trying to vectorize a basic-block mark the rest of the datarefs
3791 as not vectorizable and truncate the vector of datarefs. That
3792 avoids spending useless time in analyzing their dependence. */
3793 if (i != datarefs.length ())
3795 gcc_assert (bb_vinfo != NULL);
3796 for (unsigned j = i; j < datarefs.length (); ++j)
3798 data_reference_p dr = datarefs[j];
3799 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3800 free_data_ref (dr);
3802 datarefs.truncate (i);
3805 return true;
3809 /* Function vect_get_new_vect_var.
3811 Returns a name for a new variable. The current naming scheme appends the
3812 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3813 the name of vectorizer generated variables, and appends that to NAME if
3814 provided. */
3816 tree
3817 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3819 const char *prefix;
3820 tree new_vect_var;
3822 switch (var_kind)
3824 case vect_simple_var:
3825 prefix = "vect";
3826 break;
3827 case vect_scalar_var:
3828 prefix = "stmp";
3829 break;
3830 case vect_pointer_var:
3831 prefix = "vectp";
3832 break;
3833 default:
3834 gcc_unreachable ();
3837 if (name)
3839 char* tmp = concat (prefix, "_", name, NULL);
3840 new_vect_var = create_tmp_reg (type, tmp);
3841 free (tmp);
3843 else
3844 new_vect_var = create_tmp_reg (type, prefix);
3846 return new_vect_var;
3850 /* Function vect_create_addr_base_for_vector_ref.
3852 Create an expression that computes the address of the first memory location
3853 that will be accessed for a data reference.
3855 Input:
3856 STMT: The statement containing the data reference.
3857 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3858 OFFSET: Optional. If supplied, it is be added to the initial address.
3859 LOOP: Specify relative to which loop-nest should the address be computed.
3860 For example, when the dataref is in an inner-loop nested in an
3861 outer-loop that is now being vectorized, LOOP can be either the
3862 outer-loop, or the inner-loop. The first memory location accessed
3863 by the following dataref ('in' points to short):
3865 for (i=0; i<N; i++)
3866 for (j=0; j<M; j++)
3867 s += in[i+j]
3869 is as follows:
3870 if LOOP=i_loop: &in (relative to i_loop)
3871 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3872 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3873 initial address. Unlike OFFSET, which is number of elements to
3874 be added, BYTE_OFFSET is measured in bytes.
3876 Output:
3877 1. Return an SSA_NAME whose value is the address of the memory location of
3878 the first vector of the data reference.
3879 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3880 these statement(s) which define the returned SSA_NAME.
3882 FORNOW: We are only handling array accesses with step 1. */
3884 tree
3885 vect_create_addr_base_for_vector_ref (gimple stmt,
3886 gimple_seq *new_stmt_list,
3887 tree offset,
3888 struct loop *loop,
3889 tree byte_offset)
3891 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3892 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3893 tree data_ref_base;
3894 const char *base_name;
3895 tree addr_base;
3896 tree dest;
3897 gimple_seq seq = NULL;
3898 tree base_offset;
3899 tree init;
3900 tree vect_ptr_type;
3901 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3902 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3904 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3906 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3908 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3910 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3911 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3912 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3914 else
3916 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3917 base_offset = unshare_expr (DR_OFFSET (dr));
3918 init = unshare_expr (DR_INIT (dr));
3921 if (loop_vinfo)
3922 base_name = get_name (data_ref_base);
3923 else
3925 base_offset = ssize_int (0);
3926 init = ssize_int (0);
3927 base_name = get_name (DR_REF (dr));
3930 /* Create base_offset */
3931 base_offset = size_binop (PLUS_EXPR,
3932 fold_convert (sizetype, base_offset),
3933 fold_convert (sizetype, init));
3935 if (offset)
3937 offset = fold_build2 (MULT_EXPR, sizetype,
3938 fold_convert (sizetype, offset), step);
3939 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3940 base_offset, offset);
3942 if (byte_offset)
3944 byte_offset = fold_convert (sizetype, byte_offset);
3945 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3946 base_offset, byte_offset);
3949 /* base + base_offset */
3950 if (loop_vinfo)
3951 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3952 else
3954 addr_base = build1 (ADDR_EXPR,
3955 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3956 unshare_expr (DR_REF (dr)));
3959 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3960 addr_base = fold_convert (vect_ptr_type, addr_base);
3961 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3962 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3963 gimple_seq_add_seq (new_stmt_list, seq);
3965 if (DR_PTR_INFO (dr)
3966 && TREE_CODE (addr_base) == SSA_NAME)
3968 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3969 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
3970 int misalign = DR_MISALIGNMENT (dr);
3971 if (offset || byte_offset || (misalign == -1))
3972 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3973 else
3974 set_ptr_info_alignment (SSA_NAME_PTR_INFO (addr_base), align, misalign);
3977 if (dump_enabled_p ())
3979 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3980 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3981 dump_printf (MSG_NOTE, "\n");
3984 return addr_base;
3988 /* Function vect_create_data_ref_ptr.
3990 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3991 location accessed in the loop by STMT, along with the def-use update
3992 chain to appropriately advance the pointer through the loop iterations.
3993 Also set aliasing information for the pointer. This pointer is used by
3994 the callers to this function to create a memory reference expression for
3995 vector load/store access.
3997 Input:
3998 1. STMT: a stmt that references memory. Expected to be of the form
3999 GIMPLE_ASSIGN <name, data-ref> or
4000 GIMPLE_ASSIGN <data-ref, name>.
4001 2. AGGR_TYPE: the type of the reference, which should be either a vector
4002 or an array.
4003 3. AT_LOOP: the loop where the vector memref is to be created.
4004 4. OFFSET (optional): an offset to be added to the initial address accessed
4005 by the data-ref in STMT.
4006 5. BSI: location where the new stmts are to be placed if there is no loop
4007 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4008 pointing to the initial address.
4009 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4010 to the initial address accessed by the data-ref in STMT. This is
4011 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4012 in bytes.
4014 Output:
4015 1. Declare a new ptr to vector_type, and have it point to the base of the
4016 data reference (initial addressed accessed by the data reference).
4017 For example, for vector of type V8HI, the following code is generated:
4019 v8hi *ap;
4020 ap = (v8hi *)initial_address;
4022 if OFFSET is not supplied:
4023 initial_address = &a[init];
4024 if OFFSET is supplied:
4025 initial_address = &a[init + OFFSET];
4026 if BYTE_OFFSET is supplied:
4027 initial_address = &a[init] + BYTE_OFFSET;
4029 Return the initial_address in INITIAL_ADDRESS.
4031 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4032 update the pointer in each iteration of the loop.
4034 Return the increment stmt that updates the pointer in PTR_INCR.
4036 3. Set INV_P to true if the access pattern of the data reference in the
4037 vectorized loop is invariant. Set it to false otherwise.
4039 4. Return the pointer. */
4041 tree
4042 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
4043 tree offset, tree *initial_address,
4044 gimple_stmt_iterator *gsi, gimple *ptr_incr,
4045 bool only_init, bool *inv_p, tree byte_offset)
4047 const char *base_name;
4048 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4049 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4050 struct loop *loop = NULL;
4051 bool nested_in_vect_loop = false;
4052 struct loop *containing_loop = NULL;
4053 tree aggr_ptr_type;
4054 tree aggr_ptr;
4055 tree new_temp;
4056 gimple vec_stmt;
4057 gimple_seq new_stmt_list = NULL;
4058 edge pe = NULL;
4059 basic_block new_bb;
4060 tree aggr_ptr_init;
4061 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4062 tree aptr;
4063 gimple_stmt_iterator incr_gsi;
4064 bool insert_after;
4065 tree indx_before_incr, indx_after_incr;
4066 gimple incr;
4067 tree step;
4068 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4070 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4071 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4073 if (loop_vinfo)
4075 loop = LOOP_VINFO_LOOP (loop_vinfo);
4076 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4077 containing_loop = (gimple_bb (stmt))->loop_father;
4078 pe = loop_preheader_edge (loop);
4080 else
4082 gcc_assert (bb_vinfo);
4083 only_init = true;
4084 *ptr_incr = NULL;
4087 /* Check the step (evolution) of the load in LOOP, and record
4088 whether it's invariant. */
4089 if (nested_in_vect_loop)
4090 step = STMT_VINFO_DR_STEP (stmt_info);
4091 else
4092 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4094 if (integer_zerop (step))
4095 *inv_p = true;
4096 else
4097 *inv_p = false;
4099 /* Create an expression for the first address accessed by this load
4100 in LOOP. */
4101 base_name = get_name (DR_BASE_ADDRESS (dr));
4103 if (dump_enabled_p ())
4105 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4106 dump_printf_loc (MSG_NOTE, vect_location,
4107 "create %s-pointer variable to type: ",
4108 get_tree_code_name (TREE_CODE (aggr_type)));
4109 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4110 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4111 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4112 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4113 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4114 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4115 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4116 else
4117 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4118 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4119 dump_printf (MSG_NOTE, "\n");
4122 /* (1) Create the new aggregate-pointer variable.
4123 Vector and array types inherit the alias set of their component
4124 type by default so we need to use a ref-all pointer if the data
4125 reference does not conflict with the created aggregated data
4126 reference because it is not addressable. */
4127 bool need_ref_all = false;
4128 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4129 get_alias_set (DR_REF (dr))))
4130 need_ref_all = true;
4131 /* Likewise for any of the data references in the stmt group. */
4132 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4134 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4137 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4138 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4139 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4140 get_alias_set (DR_REF (sdr))))
4142 need_ref_all = true;
4143 break;
4145 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4147 while (orig_stmt);
4149 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4150 need_ref_all);
4151 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4154 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4155 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4156 def-use update cycles for the pointer: one relative to the outer-loop
4157 (LOOP), which is what steps (3) and (4) below do. The other is relative
4158 to the inner-loop (which is the inner-most loop containing the dataref),
4159 and this is done be step (5) below.
4161 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4162 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4163 redundant. Steps (3),(4) create the following:
4165 vp0 = &base_addr;
4166 LOOP: vp1 = phi(vp0,vp2)
4169 vp2 = vp1 + step
4170 goto LOOP
4172 If there is an inner-loop nested in loop, then step (5) will also be
4173 applied, and an additional update in the inner-loop will be created:
4175 vp0 = &base_addr;
4176 LOOP: vp1 = phi(vp0,vp2)
4178 inner: vp3 = phi(vp1,vp4)
4179 vp4 = vp3 + inner_step
4180 if () goto inner
4182 vp2 = vp1 + step
4183 if () goto LOOP */
4185 /* (2) Calculate the initial address of the aggregate-pointer, and set
4186 the aggregate-pointer to point to it before the loop. */
4188 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4190 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4191 offset, loop, byte_offset);
4192 if (new_stmt_list)
4194 if (pe)
4196 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4197 gcc_assert (!new_bb);
4199 else
4200 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4203 *initial_address = new_temp;
4205 /* Create: p = (aggr_type *) initial_base */
4206 if (TREE_CODE (new_temp) != SSA_NAME
4207 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4209 vec_stmt = gimple_build_assign (aggr_ptr,
4210 fold_convert (aggr_ptr_type, new_temp));
4211 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4212 /* Copy the points-to information if it exists. */
4213 if (DR_PTR_INFO (dr))
4214 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
4215 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4216 if (pe)
4218 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4219 gcc_assert (!new_bb);
4221 else
4222 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4224 else
4225 aggr_ptr_init = new_temp;
4227 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4228 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4229 inner-loop nested in LOOP (during outer-loop vectorization). */
4231 /* No update in loop is required. */
4232 if (only_init && (!loop_vinfo || at_loop == loop))
4233 aptr = aggr_ptr_init;
4234 else
4236 /* The step of the aggregate pointer is the type size. */
4237 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4238 /* One exception to the above is when the scalar step of the load in
4239 LOOP is zero. In this case the step here is also zero. */
4240 if (*inv_p)
4241 iv_step = size_zero_node;
4242 else if (tree_int_cst_sgn (step) == -1)
4243 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4245 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4247 create_iv (aggr_ptr_init,
4248 fold_convert (aggr_ptr_type, iv_step),
4249 aggr_ptr, loop, &incr_gsi, insert_after,
4250 &indx_before_incr, &indx_after_incr);
4251 incr = gsi_stmt (incr_gsi);
4252 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4254 /* Copy the points-to information if it exists. */
4255 if (DR_PTR_INFO (dr))
4257 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4258 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4260 if (ptr_incr)
4261 *ptr_incr = incr;
4263 aptr = indx_before_incr;
4266 if (!nested_in_vect_loop || only_init)
4267 return aptr;
4270 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4271 nested in LOOP, if exists. */
4273 gcc_assert (nested_in_vect_loop);
4274 if (!only_init)
4276 standard_iv_increment_position (containing_loop, &incr_gsi,
4277 &insert_after);
4278 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4279 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4280 &indx_after_incr);
4281 incr = gsi_stmt (incr_gsi);
4282 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4284 /* Copy the points-to information if it exists. */
4285 if (DR_PTR_INFO (dr))
4287 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4288 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4290 if (ptr_incr)
4291 *ptr_incr = incr;
4293 return indx_before_incr;
4295 else
4296 gcc_unreachable ();
4300 /* Function bump_vector_ptr
4302 Increment a pointer (to a vector type) by vector-size. If requested,
4303 i.e. if PTR-INCR is given, then also connect the new increment stmt
4304 to the existing def-use update-chain of the pointer, by modifying
4305 the PTR_INCR as illustrated below:
4307 The pointer def-use update-chain before this function:
4308 DATAREF_PTR = phi (p_0, p_2)
4309 ....
4310 PTR_INCR: p_2 = DATAREF_PTR + step
4312 The pointer def-use update-chain after this function:
4313 DATAREF_PTR = phi (p_0, p_2)
4314 ....
4315 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4316 ....
4317 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4319 Input:
4320 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4321 in the loop.
4322 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4323 the loop. The increment amount across iterations is expected
4324 to be vector_size.
4325 BSI - location where the new update stmt is to be placed.
4326 STMT - the original scalar memory-access stmt that is being vectorized.
4327 BUMP - optional. The offset by which to bump the pointer. If not given,
4328 the offset is assumed to be vector_size.
4330 Output: Return NEW_DATAREF_PTR as illustrated above.
4334 tree
4335 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4336 gimple stmt, tree bump)
4338 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4339 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4340 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4341 tree update = TYPE_SIZE_UNIT (vectype);
4342 gimple incr_stmt;
4343 ssa_op_iter iter;
4344 use_operand_p use_p;
4345 tree new_dataref_ptr;
4347 if (bump)
4348 update = bump;
4350 new_dataref_ptr = copy_ssa_name (dataref_ptr, NULL);
4351 incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, new_dataref_ptr,
4352 dataref_ptr, update);
4353 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4355 /* Copy the points-to information if it exists. */
4356 if (DR_PTR_INFO (dr))
4358 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4359 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4362 if (!ptr_incr)
4363 return new_dataref_ptr;
4365 /* Update the vector-pointer's cross-iteration increment. */
4366 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4368 tree use = USE_FROM_PTR (use_p);
4370 if (use == dataref_ptr)
4371 SET_USE (use_p, new_dataref_ptr);
4372 else
4373 gcc_assert (tree_int_cst_compare (use, update) == 0);
4376 return new_dataref_ptr;
4380 /* Function vect_create_destination_var.
4382 Create a new temporary of type VECTYPE. */
4384 tree
4385 vect_create_destination_var (tree scalar_dest, tree vectype)
4387 tree vec_dest;
4388 const char *name;
4389 char *new_name;
4390 tree type;
4391 enum vect_var_kind kind;
4393 kind = vectype ? vect_simple_var : vect_scalar_var;
4394 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4396 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4398 name = get_name (scalar_dest);
4399 if (name)
4400 asprintf (&new_name, "%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4401 else
4402 asprintf (&new_name, "_%u", SSA_NAME_VERSION (scalar_dest));
4403 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4404 free (new_name);
4406 return vec_dest;
4409 /* Function vect_grouped_store_supported.
4411 Returns TRUE if interleave high and interleave low permutations
4412 are supported, and FALSE otherwise. */
4414 bool
4415 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4417 machine_mode mode = TYPE_MODE (vectype);
4419 /* vect_permute_store_chain requires the group size to be equal to 3 or
4420 be a power of two. */
4421 if (count != 3 && exact_log2 (count) == -1)
4423 if (dump_enabled_p ())
4424 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4425 "the size of the group of accesses"
4426 " is not a power of 2 or not eqaul to 3\n");
4427 return false;
4430 /* Check that the permutation is supported. */
4431 if (VECTOR_MODE_P (mode))
4433 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4434 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4436 if (count == 3)
4438 unsigned int j0 = 0, j1 = 0, j2 = 0;
4439 unsigned int i, j;
4441 for (j = 0; j < 3; j++)
4443 int nelt0 = ((3 - j) * nelt) % 3;
4444 int nelt1 = ((3 - j) * nelt + 1) % 3;
4445 int nelt2 = ((3 - j) * nelt + 2) % 3;
4446 for (i = 0; i < nelt; i++)
4448 if (3 * i + nelt0 < nelt)
4449 sel[3 * i + nelt0] = j0++;
4450 if (3 * i + nelt1 < nelt)
4451 sel[3 * i + nelt1] = nelt + j1++;
4452 if (3 * i + nelt2 < nelt)
4453 sel[3 * i + nelt2] = 0;
4455 if (!can_vec_perm_p (mode, false, sel))
4457 if (dump_enabled_p ())
4458 dump_printf (MSG_MISSED_OPTIMIZATION,
4459 "permutaion op not supported by target.\n");
4460 return false;
4463 for (i = 0; i < nelt; i++)
4465 if (3 * i + nelt0 < nelt)
4466 sel[3 * i + nelt0] = 3 * i + nelt0;
4467 if (3 * i + nelt1 < nelt)
4468 sel[3 * i + nelt1] = 3 * i + nelt1;
4469 if (3 * i + nelt2 < nelt)
4470 sel[3 * i + nelt2] = nelt + j2++;
4472 if (!can_vec_perm_p (mode, false, sel))
4474 if (dump_enabled_p ())
4475 dump_printf (MSG_MISSED_OPTIMIZATION,
4476 "permutaion op not supported by target.\n");
4477 return false;
4480 return true;
4482 else
4484 /* If length is not equal to 3 then only power of 2 is supported. */
4485 gcc_assert (exact_log2 (count) != -1);
4487 for (i = 0; i < nelt / 2; i++)
4489 sel[i * 2] = i;
4490 sel[i * 2 + 1] = i + nelt;
4492 if (can_vec_perm_p (mode, false, sel))
4494 for (i = 0; i < nelt; i++)
4495 sel[i] += nelt / 2;
4496 if (can_vec_perm_p (mode, false, sel))
4497 return true;
4502 if (dump_enabled_p ())
4503 dump_printf (MSG_MISSED_OPTIMIZATION,
4504 "permutaion op not supported by target.\n");
4505 return false;
4509 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4510 type VECTYPE. */
4512 bool
4513 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4515 return vect_lanes_optab_supported_p ("vec_store_lanes",
4516 vec_store_lanes_optab,
4517 vectype, count);
4521 /* Function vect_permute_store_chain.
4523 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4524 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4525 the data correctly for the stores. Return the final references for stores
4526 in RESULT_CHAIN.
4528 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4529 The input is 4 vectors each containing 8 elements. We assign a number to
4530 each element, the input sequence is:
4532 1st vec: 0 1 2 3 4 5 6 7
4533 2nd vec: 8 9 10 11 12 13 14 15
4534 3rd vec: 16 17 18 19 20 21 22 23
4535 4th vec: 24 25 26 27 28 29 30 31
4537 The output sequence should be:
4539 1st vec: 0 8 16 24 1 9 17 25
4540 2nd vec: 2 10 18 26 3 11 19 27
4541 3rd vec: 4 12 20 28 5 13 21 30
4542 4th vec: 6 14 22 30 7 15 23 31
4544 i.e., we interleave the contents of the four vectors in their order.
4546 We use interleave_high/low instructions to create such output. The input of
4547 each interleave_high/low operation is two vectors:
4548 1st vec 2nd vec
4549 0 1 2 3 4 5 6 7
4550 the even elements of the result vector are obtained left-to-right from the
4551 high/low elements of the first vector. The odd elements of the result are
4552 obtained left-to-right from the high/low elements of the second vector.
4553 The output of interleave_high will be: 0 4 1 5
4554 and of interleave_low: 2 6 3 7
4557 The permutation is done in log LENGTH stages. In each stage interleave_high
4558 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4559 where the first argument is taken from the first half of DR_CHAIN and the
4560 second argument from it's second half.
4561 In our example,
4563 I1: interleave_high (1st vec, 3rd vec)
4564 I2: interleave_low (1st vec, 3rd vec)
4565 I3: interleave_high (2nd vec, 4th vec)
4566 I4: interleave_low (2nd vec, 4th vec)
4568 The output for the first stage is:
4570 I1: 0 16 1 17 2 18 3 19
4571 I2: 4 20 5 21 6 22 7 23
4572 I3: 8 24 9 25 10 26 11 27
4573 I4: 12 28 13 29 14 30 15 31
4575 The output of the second stage, i.e. the final result is:
4577 I1: 0 8 16 24 1 9 17 25
4578 I2: 2 10 18 26 3 11 19 27
4579 I3: 4 12 20 28 5 13 21 30
4580 I4: 6 14 22 30 7 15 23 31. */
4582 void
4583 vect_permute_store_chain (vec<tree> dr_chain,
4584 unsigned int length,
4585 gimple stmt,
4586 gimple_stmt_iterator *gsi,
4587 vec<tree> *result_chain)
4589 tree vect1, vect2, high, low;
4590 gimple perm_stmt;
4591 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4592 tree perm_mask_low, perm_mask_high;
4593 tree data_ref;
4594 tree perm3_mask_low, perm3_mask_high;
4595 unsigned int i, n, log_length = exact_log2 (length);
4596 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4597 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4599 result_chain->quick_grow (length);
4600 memcpy (result_chain->address (), dr_chain.address (),
4601 length * sizeof (tree));
4603 if (length == 3)
4605 unsigned int j0 = 0, j1 = 0, j2 = 0;
4607 for (j = 0; j < 3; j++)
4609 int nelt0 = ((3 - j) * nelt) % 3;
4610 int nelt1 = ((3 - j) * nelt + 1) % 3;
4611 int nelt2 = ((3 - j) * nelt + 2) % 3;
4613 for (i = 0; i < nelt; i++)
4615 if (3 * i + nelt0 < nelt)
4616 sel[3 * i + nelt0] = j0++;
4617 if (3 * i + nelt1 < nelt)
4618 sel[3 * i + nelt1] = nelt + j1++;
4619 if (3 * i + nelt2 < nelt)
4620 sel[3 * i + nelt2] = 0;
4622 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
4623 gcc_assert (perm3_mask_low != NULL);
4625 for (i = 0; i < nelt; i++)
4627 if (3 * i + nelt0 < nelt)
4628 sel[3 * i + nelt0] = 3 * i + nelt0;
4629 if (3 * i + nelt1 < nelt)
4630 sel[3 * i + nelt1] = 3 * i + nelt1;
4631 if (3 * i + nelt2 < nelt)
4632 sel[3 * i + nelt2] = nelt + j2++;
4634 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
4635 gcc_assert (perm3_mask_high != NULL);
4637 vect1 = dr_chain[0];
4638 vect2 = dr_chain[1];
4640 /* Create interleaving stmt:
4641 low = VEC_PERM_EXPR <vect1, vect2,
4642 {j, nelt, *, j + 1, nelt + j + 1, *,
4643 j + 2, nelt + j + 2, *, ...}> */
4644 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4645 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4646 vect1, vect2,
4647 perm3_mask_low);
4648 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4650 vect1 = data_ref;
4651 vect2 = dr_chain[2];
4652 /* Create interleaving stmt:
4653 low = VEC_PERM_EXPR <vect1, vect2,
4654 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4655 6, 7, nelt + j + 2, ...}> */
4656 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4657 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4658 vect1, vect2,
4659 perm3_mask_high);
4660 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4661 (*result_chain)[j] = data_ref;
4664 else
4666 /* If length is not equal to 3 then only power of 2 is supported. */
4667 gcc_assert (exact_log2 (length) != -1);
4669 for (i = 0, n = nelt / 2; i < n; i++)
4671 sel[i * 2] = i;
4672 sel[i * 2 + 1] = i + nelt;
4674 perm_mask_high = vect_gen_perm_mask (vectype, sel);
4675 gcc_assert (perm_mask_high != NULL);
4677 for (i = 0; i < nelt; i++)
4678 sel[i] += nelt / 2;
4679 perm_mask_low = vect_gen_perm_mask (vectype, sel);
4680 gcc_assert (perm_mask_low != NULL);
4682 for (i = 0, n = log_length; i < n; i++)
4684 for (j = 0; j < length/2; j++)
4686 vect1 = dr_chain[j];
4687 vect2 = dr_chain[j+length/2];
4689 /* Create interleaving stmt:
4690 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4691 ...}> */
4692 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4693 perm_stmt
4694 = gimple_build_assign_with_ops (VEC_PERM_EXPR, high,
4695 vect1, vect2, perm_mask_high);
4696 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4697 (*result_chain)[2*j] = high;
4699 /* Create interleaving stmt:
4700 low = VEC_PERM_EXPR <vect1, vect2,
4701 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4702 ...}> */
4703 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4704 perm_stmt
4705 = gimple_build_assign_with_ops (VEC_PERM_EXPR, low,
4706 vect1, vect2, perm_mask_low);
4707 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4708 (*result_chain)[2*j+1] = low;
4710 memcpy (dr_chain.address (), result_chain->address (),
4711 length * sizeof (tree));
4716 /* Function vect_setup_realignment
4718 This function is called when vectorizing an unaligned load using
4719 the dr_explicit_realign[_optimized] scheme.
4720 This function generates the following code at the loop prolog:
4722 p = initial_addr;
4723 x msq_init = *(floor(p)); # prolog load
4724 realignment_token = call target_builtin;
4725 loop:
4726 x msq = phi (msq_init, ---)
4728 The stmts marked with x are generated only for the case of
4729 dr_explicit_realign_optimized.
4731 The code above sets up a new (vector) pointer, pointing to the first
4732 location accessed by STMT, and a "floor-aligned" load using that pointer.
4733 It also generates code to compute the "realignment-token" (if the relevant
4734 target hook was defined), and creates a phi-node at the loop-header bb
4735 whose arguments are the result of the prolog-load (created by this
4736 function) and the result of a load that takes place in the loop (to be
4737 created by the caller to this function).
4739 For the case of dr_explicit_realign_optimized:
4740 The caller to this function uses the phi-result (msq) to create the
4741 realignment code inside the loop, and sets up the missing phi argument,
4742 as follows:
4743 loop:
4744 msq = phi (msq_init, lsq)
4745 lsq = *(floor(p')); # load in loop
4746 result = realign_load (msq, lsq, realignment_token);
4748 For the case of dr_explicit_realign:
4749 loop:
4750 msq = *(floor(p)); # load in loop
4751 p' = p + (VS-1);
4752 lsq = *(floor(p')); # load in loop
4753 result = realign_load (msq, lsq, realignment_token);
4755 Input:
4756 STMT - (scalar) load stmt to be vectorized. This load accesses
4757 a memory location that may be unaligned.
4758 BSI - place where new code is to be inserted.
4759 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4760 is used.
4762 Output:
4763 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4764 target hook, if defined.
4765 Return value - the result of the loop-header phi node. */
4767 tree
4768 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4769 tree *realignment_token,
4770 enum dr_alignment_support alignment_support_scheme,
4771 tree init_addr,
4772 struct loop **at_loop)
4774 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4775 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4776 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4777 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4778 struct loop *loop = NULL;
4779 edge pe = NULL;
4780 tree scalar_dest = gimple_assign_lhs (stmt);
4781 tree vec_dest;
4782 gimple inc;
4783 tree ptr;
4784 tree data_ref;
4785 gimple new_stmt;
4786 basic_block new_bb;
4787 tree msq_init = NULL_TREE;
4788 tree new_temp;
4789 gimple phi_stmt;
4790 tree msq = NULL_TREE;
4791 gimple_seq stmts = NULL;
4792 bool inv_p;
4793 bool compute_in_loop = false;
4794 bool nested_in_vect_loop = false;
4795 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4796 struct loop *loop_for_initial_load = NULL;
4798 if (loop_vinfo)
4800 loop = LOOP_VINFO_LOOP (loop_vinfo);
4801 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4804 gcc_assert (alignment_support_scheme == dr_explicit_realign
4805 || alignment_support_scheme == dr_explicit_realign_optimized);
4807 /* We need to generate three things:
4808 1. the misalignment computation
4809 2. the extra vector load (for the optimized realignment scheme).
4810 3. the phi node for the two vectors from which the realignment is
4811 done (for the optimized realignment scheme). */
4813 /* 1. Determine where to generate the misalignment computation.
4815 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4816 calculation will be generated by this function, outside the loop (in the
4817 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4818 caller, inside the loop.
4820 Background: If the misalignment remains fixed throughout the iterations of
4821 the loop, then both realignment schemes are applicable, and also the
4822 misalignment computation can be done outside LOOP. This is because we are
4823 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4824 are a multiple of VS (the Vector Size), and therefore the misalignment in
4825 different vectorized LOOP iterations is always the same.
4826 The problem arises only if the memory access is in an inner-loop nested
4827 inside LOOP, which is now being vectorized using outer-loop vectorization.
4828 This is the only case when the misalignment of the memory access may not
4829 remain fixed throughout the iterations of the inner-loop (as explained in
4830 detail in vect_supportable_dr_alignment). In this case, not only is the
4831 optimized realignment scheme not applicable, but also the misalignment
4832 computation (and generation of the realignment token that is passed to
4833 REALIGN_LOAD) have to be done inside the loop.
4835 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4836 or not, which in turn determines if the misalignment is computed inside
4837 the inner-loop, or outside LOOP. */
4839 if (init_addr != NULL_TREE || !loop_vinfo)
4841 compute_in_loop = true;
4842 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4846 /* 2. Determine where to generate the extra vector load.
4848 For the optimized realignment scheme, instead of generating two vector
4849 loads in each iteration, we generate a single extra vector load in the
4850 preheader of the loop, and in each iteration reuse the result of the
4851 vector load from the previous iteration. In case the memory access is in
4852 an inner-loop nested inside LOOP, which is now being vectorized using
4853 outer-loop vectorization, we need to determine whether this initial vector
4854 load should be generated at the preheader of the inner-loop, or can be
4855 generated at the preheader of LOOP. If the memory access has no evolution
4856 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4857 to be generated inside LOOP (in the preheader of the inner-loop). */
4859 if (nested_in_vect_loop)
4861 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4862 bool invariant_in_outerloop =
4863 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4864 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4866 else
4867 loop_for_initial_load = loop;
4868 if (at_loop)
4869 *at_loop = loop_for_initial_load;
4871 if (loop_for_initial_load)
4872 pe = loop_preheader_edge (loop_for_initial_load);
4874 /* 3. For the case of the optimized realignment, create the first vector
4875 load at the loop preheader. */
4877 if (alignment_support_scheme == dr_explicit_realign_optimized)
4879 /* Create msq_init = *(floor(p1)) in the loop preheader */
4881 gcc_assert (!compute_in_loop);
4882 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4883 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4884 NULL_TREE, &init_addr, NULL, &inc,
4885 true, &inv_p);
4886 new_temp = copy_ssa_name (ptr, NULL);
4887 new_stmt = gimple_build_assign_with_ops
4888 (BIT_AND_EXPR, new_temp, ptr,
4889 build_int_cst (TREE_TYPE (ptr),
4890 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4891 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4892 gcc_assert (!new_bb);
4893 data_ref
4894 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4895 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4896 new_stmt = gimple_build_assign (vec_dest, data_ref);
4897 new_temp = make_ssa_name (vec_dest, new_stmt);
4898 gimple_assign_set_lhs (new_stmt, new_temp);
4899 if (pe)
4901 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4902 gcc_assert (!new_bb);
4904 else
4905 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4907 msq_init = gimple_assign_lhs (new_stmt);
4910 /* 4. Create realignment token using a target builtin, if available.
4911 It is done either inside the containing loop, or before LOOP (as
4912 determined above). */
4914 if (targetm.vectorize.builtin_mask_for_load)
4916 tree builtin_decl;
4918 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4919 if (!init_addr)
4921 /* Generate the INIT_ADDR computation outside LOOP. */
4922 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4923 NULL_TREE, loop);
4924 if (loop)
4926 pe = loop_preheader_edge (loop);
4927 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4928 gcc_assert (!new_bb);
4930 else
4931 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4934 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4935 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4936 vec_dest =
4937 vect_create_destination_var (scalar_dest,
4938 gimple_call_return_type (new_stmt));
4939 new_temp = make_ssa_name (vec_dest, new_stmt);
4940 gimple_call_set_lhs (new_stmt, new_temp);
4942 if (compute_in_loop)
4943 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4944 else
4946 /* Generate the misalignment computation outside LOOP. */
4947 pe = loop_preheader_edge (loop);
4948 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4949 gcc_assert (!new_bb);
4952 *realignment_token = gimple_call_lhs (new_stmt);
4954 /* The result of the CALL_EXPR to this builtin is determined from
4955 the value of the parameter and no global variables are touched
4956 which makes the builtin a "const" function. Requiring the
4957 builtin to have the "const" attribute makes it unnecessary
4958 to call mark_call_clobbered. */
4959 gcc_assert (TREE_READONLY (builtin_decl));
4962 if (alignment_support_scheme == dr_explicit_realign)
4963 return msq;
4965 gcc_assert (!compute_in_loop);
4966 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4969 /* 5. Create msq = phi <msq_init, lsq> in loop */
4971 pe = loop_preheader_edge (containing_loop);
4972 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4973 msq = make_ssa_name (vec_dest, NULL);
4974 phi_stmt = create_phi_node (msq, containing_loop->header);
4975 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4977 return msq;
4981 /* Function vect_grouped_load_supported.
4983 Returns TRUE if even and odd permutations are supported,
4984 and FALSE otherwise. */
4986 bool
4987 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4989 machine_mode mode = TYPE_MODE (vectype);
4991 /* vect_permute_load_chain requires the group size to be equal to 3 or
4992 be a power of two. */
4993 if (count != 3 && exact_log2 (count) == -1)
4995 if (dump_enabled_p ())
4996 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4997 "the size of the group of accesses"
4998 " is not a power of 2 or not equal to 3\n");
4999 return false;
5002 /* Check that the permutation is supported. */
5003 if (VECTOR_MODE_P (mode))
5005 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5006 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5008 if (count == 3)
5010 unsigned int k;
5011 for (k = 0; k < 3; k++)
5013 for (i = 0; i < nelt; i++)
5014 if (3 * i + k < 2 * nelt)
5015 sel[i] = 3 * i + k;
5016 else
5017 sel[i] = 0;
5018 if (!can_vec_perm_p (mode, false, sel))
5020 if (dump_enabled_p ())
5021 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5022 "shuffle of 3 loads is not supported by"
5023 " target\n");
5024 return false;
5026 for (i = 0, j = 0; i < nelt; i++)
5027 if (3 * i + k < 2 * nelt)
5028 sel[i] = i;
5029 else
5030 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5031 if (!can_vec_perm_p (mode, false, sel))
5033 if (dump_enabled_p ())
5034 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5035 "shuffle of 3 loads is not supported by"
5036 " target\n");
5037 return false;
5040 return true;
5042 else
5044 /* If length is not equal to 3 then only power of 2 is supported. */
5045 gcc_assert (exact_log2 (count) != -1);
5046 for (i = 0; i < nelt; i++)
5047 sel[i] = i * 2;
5048 if (can_vec_perm_p (mode, false, sel))
5050 for (i = 0; i < nelt; i++)
5051 sel[i] = i * 2 + 1;
5052 if (can_vec_perm_p (mode, false, sel))
5053 return true;
5058 if (dump_enabled_p ())
5059 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5060 "extract even/odd not supported by target\n");
5061 return false;
5064 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5065 type VECTYPE. */
5067 bool
5068 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5070 return vect_lanes_optab_supported_p ("vec_load_lanes",
5071 vec_load_lanes_optab,
5072 vectype, count);
5075 /* Function vect_permute_load_chain.
5077 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5078 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5079 the input data correctly. Return the final references for loads in
5080 RESULT_CHAIN.
5082 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5083 The input is 4 vectors each containing 8 elements. We assign a number to each
5084 element, the input sequence is:
5086 1st vec: 0 1 2 3 4 5 6 7
5087 2nd vec: 8 9 10 11 12 13 14 15
5088 3rd vec: 16 17 18 19 20 21 22 23
5089 4th vec: 24 25 26 27 28 29 30 31
5091 The output sequence should be:
5093 1st vec: 0 4 8 12 16 20 24 28
5094 2nd vec: 1 5 9 13 17 21 25 29
5095 3rd vec: 2 6 10 14 18 22 26 30
5096 4th vec: 3 7 11 15 19 23 27 31
5098 i.e., the first output vector should contain the first elements of each
5099 interleaving group, etc.
5101 We use extract_even/odd instructions to create such output. The input of
5102 each extract_even/odd operation is two vectors
5103 1st vec 2nd vec
5104 0 1 2 3 4 5 6 7
5106 and the output is the vector of extracted even/odd elements. The output of
5107 extract_even will be: 0 2 4 6
5108 and of extract_odd: 1 3 5 7
5111 The permutation is done in log LENGTH stages. In each stage extract_even
5112 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5113 their order. In our example,
5115 E1: extract_even (1st vec, 2nd vec)
5116 E2: extract_odd (1st vec, 2nd vec)
5117 E3: extract_even (3rd vec, 4th vec)
5118 E4: extract_odd (3rd vec, 4th vec)
5120 The output for the first stage will be:
5122 E1: 0 2 4 6 8 10 12 14
5123 E2: 1 3 5 7 9 11 13 15
5124 E3: 16 18 20 22 24 26 28 30
5125 E4: 17 19 21 23 25 27 29 31
5127 In order to proceed and create the correct sequence for the next stage (or
5128 for the correct output, if the second stage is the last one, as in our
5129 example), we first put the output of extract_even operation and then the
5130 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5131 The input for the second stage is:
5133 1st vec (E1): 0 2 4 6 8 10 12 14
5134 2nd vec (E3): 16 18 20 22 24 26 28 30
5135 3rd vec (E2): 1 3 5 7 9 11 13 15
5136 4th vec (E4): 17 19 21 23 25 27 29 31
5138 The output of the second stage:
5140 E1: 0 4 8 12 16 20 24 28
5141 E2: 2 6 10 14 18 22 26 30
5142 E3: 1 5 9 13 17 21 25 29
5143 E4: 3 7 11 15 19 23 27 31
5145 And RESULT_CHAIN after reordering:
5147 1st vec (E1): 0 4 8 12 16 20 24 28
5148 2nd vec (E3): 1 5 9 13 17 21 25 29
5149 3rd vec (E2): 2 6 10 14 18 22 26 30
5150 4th vec (E4): 3 7 11 15 19 23 27 31. */
5152 static void
5153 vect_permute_load_chain (vec<tree> dr_chain,
5154 unsigned int length,
5155 gimple stmt,
5156 gimple_stmt_iterator *gsi,
5157 vec<tree> *result_chain)
5159 tree data_ref, first_vect, second_vect;
5160 tree perm_mask_even, perm_mask_odd;
5161 tree perm3_mask_low, perm3_mask_high;
5162 gimple perm_stmt;
5163 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5164 unsigned int i, j, log_length = exact_log2 (length);
5165 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5166 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5168 result_chain->quick_grow (length);
5169 memcpy (result_chain->address (), dr_chain.address (),
5170 length * sizeof (tree));
5172 if (length == 3)
5174 unsigned int k;
5176 for (k = 0; k < 3; k++)
5178 for (i = 0; i < nelt; i++)
5179 if (3 * i + k < 2 * nelt)
5180 sel[i] = 3 * i + k;
5181 else
5182 sel[i] = 0;
5183 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
5184 gcc_assert (perm3_mask_low != NULL);
5186 for (i = 0, j = 0; i < nelt; i++)
5187 if (3 * i + k < 2 * nelt)
5188 sel[i] = i;
5189 else
5190 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5192 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
5193 gcc_assert (perm3_mask_high != NULL);
5195 first_vect = dr_chain[0];
5196 second_vect = dr_chain[1];
5198 /* Create interleaving stmt (low part of):
5199 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5200 ...}> */
5201 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5202 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5203 first_vect, second_vect,
5204 perm3_mask_low);
5205 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5207 /* Create interleaving stmt (high part of):
5208 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5209 ...}> */
5210 first_vect = data_ref;
5211 second_vect = dr_chain[2];
5212 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5213 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5214 first_vect, second_vect,
5215 perm3_mask_high);
5216 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5217 (*result_chain)[k] = data_ref;
5220 else
5222 /* If length is not equal to 3 then only power of 2 is supported. */
5223 gcc_assert (exact_log2 (length) != -1);
5225 for (i = 0; i < nelt; ++i)
5226 sel[i] = i * 2;
5227 perm_mask_even = vect_gen_perm_mask (vectype, sel);
5228 gcc_assert (perm_mask_even != NULL);
5230 for (i = 0; i < nelt; ++i)
5231 sel[i] = i * 2 + 1;
5232 perm_mask_odd = vect_gen_perm_mask (vectype, sel);
5233 gcc_assert (perm_mask_odd != NULL);
5235 for (i = 0; i < log_length; i++)
5237 for (j = 0; j < length; j += 2)
5239 first_vect = dr_chain[j];
5240 second_vect = dr_chain[j+1];
5242 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5243 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5244 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5245 first_vect, second_vect,
5246 perm_mask_even);
5247 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5248 (*result_chain)[j/2] = data_ref;
5250 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5251 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5252 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5253 first_vect, second_vect,
5254 perm_mask_odd);
5255 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5256 (*result_chain)[j/2+length/2] = data_ref;
5258 memcpy (dr_chain.address (), result_chain->address (),
5259 length * sizeof (tree));
5264 /* Function vect_shift_permute_load_chain.
5266 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5267 sequence of stmts to reorder the input data accordingly.
5268 Return the final references for loads in RESULT_CHAIN.
5269 Return true if successed, false otherwise.
5271 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5272 The input is 3 vectors each containing 8 elements. We assign a
5273 number to each element, the input sequence is:
5275 1st vec: 0 1 2 3 4 5 6 7
5276 2nd vec: 8 9 10 11 12 13 14 15
5277 3rd vec: 16 17 18 19 20 21 22 23
5279 The output sequence should be:
5281 1st vec: 0 3 6 9 12 15 18 21
5282 2nd vec: 1 4 7 10 13 16 19 22
5283 3rd vec: 2 5 8 11 14 17 20 23
5285 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5287 First we shuffle all 3 vectors to get correct elements order:
5289 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5290 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5291 3rd vec: (16 19 22) (17 20 23) (18 21)
5293 Next we unite and shift vector 3 times:
5295 1st step:
5296 shift right by 6 the concatenation of:
5297 "1st vec" and "2nd vec"
5298 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5299 "2nd vec" and "3rd vec"
5300 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5301 "3rd vec" and "1st vec"
5302 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5303 | New vectors |
5305 So that now new vectors are:
5307 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5308 2nd vec: (10 13) (16 19 22) (17 20 23)
5309 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5311 2nd step:
5312 shift right by 5 the concatenation of:
5313 "1st vec" and "3rd vec"
5314 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5315 "2nd vec" and "1st vec"
5316 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5317 "3rd vec" and "2nd vec"
5318 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5319 | New vectors |
5321 So that now new vectors are:
5323 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5324 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5325 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5327 3rd step:
5328 shift right by 5 the concatenation of:
5329 "1st vec" and "1st vec"
5330 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5331 shift right by 3 the concatenation of:
5332 "2nd vec" and "2nd vec"
5333 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5334 | New vectors |
5336 So that now all vectors are READY:
5337 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5338 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5339 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5341 This algorithm is faster than one in vect_permute_load_chain if:
5342 1. "shift of a concatination" is faster than general permutation.
5343 This is usually so.
5344 2. The TARGET machine can't execute vector instructions in parallel.
5345 This is because each step of the algorithm depends on previous.
5346 The algorithm in vect_permute_load_chain is much more parallel.
5348 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5351 static bool
5352 vect_shift_permute_load_chain (vec<tree> dr_chain,
5353 unsigned int length,
5354 gimple stmt,
5355 gimple_stmt_iterator *gsi,
5356 vec<tree> *result_chain)
5358 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5359 tree perm2_mask1, perm2_mask2, perm3_mask;
5360 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5361 gimple perm_stmt;
5363 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5364 unsigned int i;
5365 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5366 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5367 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5368 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5370 result_chain->quick_grow (length);
5371 memcpy (result_chain->address (), dr_chain.address (),
5372 length * sizeof (tree));
5374 if (exact_log2 (length) != -1 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5376 unsigned int j, log_length = exact_log2 (length);
5377 for (i = 0; i < nelt / 2; ++i)
5378 sel[i] = i * 2;
5379 for (i = 0; i < nelt / 2; ++i)
5380 sel[nelt / 2 + i] = i * 2 + 1;
5381 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5383 if (dump_enabled_p ())
5384 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5385 "shuffle of 2 fields structure is not \
5386 supported by target\n");
5387 return false;
5389 perm2_mask1 = vect_gen_perm_mask (vectype, sel);
5390 gcc_assert (perm2_mask1 != NULL);
5392 for (i = 0; i < nelt / 2; ++i)
5393 sel[i] = i * 2 + 1;
5394 for (i = 0; i < nelt / 2; ++i)
5395 sel[nelt / 2 + i] = i * 2;
5396 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5398 if (dump_enabled_p ())
5399 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5400 "shuffle of 2 fields structure is not \
5401 supported by target\n");
5402 return false;
5404 perm2_mask2 = vect_gen_perm_mask (vectype, sel);
5405 gcc_assert (perm2_mask2 != NULL);
5407 /* Generating permutation constant to shift all elements.
5408 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5409 for (i = 0; i < nelt; i++)
5410 sel[i] = nelt / 2 + i;
5411 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5413 if (dump_enabled_p ())
5414 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5415 "shift permutation is not supported by target\n");
5416 return false;
5418 shift1_mask = vect_gen_perm_mask (vectype, sel);
5419 gcc_assert (shift1_mask != NULL);
5421 /* Generating permutation constant to select vector from 2.
5422 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5423 for (i = 0; i < nelt / 2; i++)
5424 sel[i] = i;
5425 for (i = nelt / 2; i < nelt; i++)
5426 sel[i] = nelt + i;
5427 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5429 if (dump_enabled_p ())
5430 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5431 "select is not supported by target\n");
5432 return false;
5434 select_mask = vect_gen_perm_mask (vectype, sel);
5435 gcc_assert (select_mask != NULL);
5437 for (i = 0; i < log_length; i++)
5439 for (j = 0; j < length; j += 2)
5441 first_vect = dr_chain[j];
5442 second_vect = dr_chain[j + 1];
5444 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5445 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5446 first_vect, first_vect,
5447 perm2_mask1);
5448 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5449 vect[0] = data_ref;
5451 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5452 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5453 second_vect, second_vect,
5454 perm2_mask2);
5455 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5456 vect[1] = data_ref;
5458 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5459 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5460 vect[0], vect[1],
5461 shift1_mask);
5462 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5463 (*result_chain)[j/2 + length/2] = data_ref;
5465 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5466 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5467 vect[0], vect[1],
5468 select_mask);
5469 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5470 (*result_chain)[j/2] = data_ref;
5472 memcpy (dr_chain.address (), result_chain->address (),
5473 length * sizeof (tree));
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;