2013-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob741586067bfa053e9ab3701562b8fcfa3d44f99b
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2013 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 "basic-block.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "tree-eh.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-ssa-loop-ivopts.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-ssa-loop.h"
50 #include "dumpfile.h"
51 #include "cfgloop.h"
52 #include "tree-chrec.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-vectorizer.h"
55 #include "diagnostic-core.h"
56 #include "cgraph.h"
57 /* Need to include rtl.h, expr.h, etc. for optabs. */
58 #include "expr.h"
59 #include "optabs.h"
61 /* Return true if load- or store-lanes optab OPTAB is implemented for
62 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
64 static bool
65 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
66 tree vectype, unsigned HOST_WIDE_INT count)
68 enum machine_mode mode, array_mode;
69 bool limit_p;
71 mode = TYPE_MODE (vectype);
72 limit_p = !targetm.array_mode_supported_p (mode, count);
73 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
74 MODE_INT, limit_p);
76 if (array_mode == BLKmode)
78 if (dump_enabled_p ())
79 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
80 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
81 GET_MODE_NAME (mode), count);
82 return false;
85 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
87 if (dump_enabled_p ())
88 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
89 "cannot use %s<%s><%s>\n", name,
90 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
91 return false;
94 if (dump_enabled_p ())
95 dump_printf_loc (MSG_NOTE, vect_location,
96 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
97 GET_MODE_NAME (mode));
99 return true;
103 /* Return the smallest scalar part of STMT.
104 This is used to determine the vectype of the stmt. We generally set the
105 vectype according to the type of the result (lhs). For stmts whose
106 result-type is different than the type of the arguments (e.g., demotion,
107 promotion), vectype will be reset appropriately (later). Note that we have
108 to visit the smallest datatype in this function, because that determines the
109 VF. If the smallest datatype in the loop is present only as the rhs of a
110 promotion operation - we'd miss it.
111 Such a case, where a variable of this datatype does not appear in the lhs
112 anywhere in the loop, can only occur if it's an invariant: e.g.:
113 'int_x = (int) short_inv', which we'd expect to have been optimized away by
114 invariant motion. However, we cannot rely on invariant motion to always
115 take invariants out of the loop, and so in the case of promotion we also
116 have to check the rhs.
117 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
118 types. */
120 tree
121 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
122 HOST_WIDE_INT *rhs_size_unit)
124 tree scalar_type = gimple_expr_type (stmt);
125 HOST_WIDE_INT lhs, rhs;
127 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
129 if (is_gimple_assign (stmt)
130 && (gimple_assign_cast_p (stmt)
131 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
132 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
133 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
135 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
137 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
138 if (rhs < lhs)
139 scalar_type = rhs_type;
142 *lhs_size_unit = lhs;
143 *rhs_size_unit = rhs;
144 return scalar_type;
148 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
149 tested at run-time. Return TRUE if DDR was successfully inserted.
150 Return false if versioning is not supported. */
152 static bool
153 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
155 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
157 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
158 return false;
160 if (dump_enabled_p ())
162 dump_printf_loc (MSG_NOTE, vect_location,
163 "mark for run-time aliasing test between ");
164 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
165 dump_printf (MSG_NOTE, " and ");
166 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
167 dump_printf (MSG_NOTE, "\n");
170 if (optimize_loop_nest_for_size_p (loop))
172 if (dump_enabled_p ())
173 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
174 "versioning not supported when optimizing"
175 " for size.\n");
176 return false;
179 /* FORNOW: We don't support versioning with outer-loop vectorization. */
180 if (loop->inner)
182 if (dump_enabled_p ())
183 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
184 "versioning not yet supported for outer-loops.\n");
185 return false;
188 /* FORNOW: We don't support creating runtime alias tests for non-constant
189 step. */
190 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
191 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
193 if (dump_enabled_p ())
194 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
195 "versioning not yet supported for non-constant "
196 "step\n");
197 return false;
200 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
201 return true;
205 /* Function vect_analyze_data_ref_dependence.
207 Return TRUE if there (might) exist a dependence between a memory-reference
208 DRA and a memory-reference DRB. When versioning for alias may check a
209 dependence at run-time, return FALSE. Adjust *MAX_VF according to
210 the data dependence. */
212 static bool
213 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
214 loop_vec_info loop_vinfo, int *max_vf)
216 unsigned int i;
217 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
218 struct data_reference *dra = DDR_A (ddr);
219 struct data_reference *drb = DDR_B (ddr);
220 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
221 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
222 lambda_vector dist_v;
223 unsigned int loop_depth;
225 /* In loop analysis all data references should be vectorizable. */
226 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
227 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
228 gcc_unreachable ();
230 /* Independent data accesses. */
231 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
232 return false;
234 if (dra == drb
235 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
236 return false;
238 /* Unknown data dependence. */
239 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
241 /* If user asserted safelen consecutive iterations can be
242 executed concurrently, assume independence. */
243 if (loop->safelen >= 2)
245 if (loop->safelen < *max_vf)
246 *max_vf = loop->safelen;
247 return false;
250 if (STMT_VINFO_GATHER_P (stmtinfo_a)
251 || STMT_VINFO_GATHER_P (stmtinfo_b))
253 if (dump_enabled_p ())
255 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
256 "versioning for alias not supported for: "
257 "can't determine dependence between ");
258 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
259 DR_REF (dra));
260 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
261 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
262 DR_REF (drb));
263 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
265 return true;
268 if (dump_enabled_p ())
270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
271 "versioning for alias required: "
272 "can't determine dependence between ");
273 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
274 DR_REF (dra));
275 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
276 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
277 DR_REF (drb));
278 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
281 /* Add to list of ddrs that need to be tested at run-time. */
282 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
285 /* Known data dependence. */
286 if (DDR_NUM_DIST_VECTS (ddr) == 0)
288 /* If user asserted safelen consecutive iterations can be
289 executed concurrently, assume independence. */
290 if (loop->safelen >= 2)
292 if (loop->safelen < *max_vf)
293 *max_vf = loop->safelen;
294 return false;
297 if (STMT_VINFO_GATHER_P (stmtinfo_a)
298 || STMT_VINFO_GATHER_P (stmtinfo_b))
300 if (dump_enabled_p ())
302 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
303 "versioning for alias not supported for: "
304 "bad dist vector for ");
305 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
306 DR_REF (dra));
307 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
308 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
309 DR_REF (drb));
310 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
312 return true;
315 if (dump_enabled_p ())
317 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
318 "versioning for alias required: "
319 "bad dist vector for ");
320 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
321 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
322 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
323 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
325 /* Add to list of ddrs that need to be tested at run-time. */
326 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
329 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
330 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
332 int dist = dist_v[loop_depth];
334 if (dump_enabled_p ())
335 dump_printf_loc (MSG_NOTE, vect_location,
336 "dependence distance = %d.\n", dist);
338 if (dist == 0)
340 if (dump_enabled_p ())
342 dump_printf_loc (MSG_NOTE, vect_location,
343 "dependence distance == 0 between ");
344 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
345 dump_printf (MSG_NOTE, " and ");
346 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
347 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
350 /* When we perform grouped accesses and perform implicit CSE
351 by detecting equal accesses and doing disambiguation with
352 runtime alias tests like for
353 .. = a[i];
354 .. = a[i+1];
355 a[i] = ..;
356 a[i+1] = ..;
357 *p = ..;
358 .. = a[i];
359 .. = a[i+1];
360 where we will end up loading { a[i], a[i+1] } once, make
361 sure that inserting group loads before the first load and
362 stores after the last store will do the right thing. */
363 if ((STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
364 && GROUP_SAME_DR_STMT (stmtinfo_a))
365 || (STMT_VINFO_GROUPED_ACCESS (stmtinfo_b)
366 && GROUP_SAME_DR_STMT (stmtinfo_b)))
368 gimple earlier_stmt;
369 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
370 if (DR_IS_WRITE
371 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
373 if (dump_enabled_p ())
374 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
375 "READ_WRITE dependence in interleaving."
376 "\n");
377 return true;
381 continue;
384 if (dist > 0 && DDR_REVERSED_P (ddr))
386 /* If DDR_REVERSED_P the order of the data-refs in DDR was
387 reversed (to make distance vector positive), and the actual
388 distance is negative. */
389 if (dump_enabled_p ())
390 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
391 "dependence distance negative.\n");
392 continue;
395 if (abs (dist) >= 2
396 && abs (dist) < *max_vf)
398 /* The dependence distance requires reduction of the maximal
399 vectorization factor. */
400 *max_vf = abs (dist);
401 if (dump_enabled_p ())
402 dump_printf_loc (MSG_NOTE, vect_location,
403 "adjusting maximal vectorization factor to %i\n",
404 *max_vf);
407 if (abs (dist) >= *max_vf)
409 /* Dependence distance does not create dependence, as far as
410 vectorization is concerned, in this case. */
411 if (dump_enabled_p ())
412 dump_printf_loc (MSG_NOTE, vect_location,
413 "dependence distance >= VF.\n");
414 continue;
417 if (dump_enabled_p ())
419 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
420 "not vectorized, possible dependence "
421 "between data-refs ");
422 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
423 dump_printf (MSG_NOTE, " and ");
424 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
425 dump_printf (MSG_NOTE, "\n");
428 return true;
431 return false;
434 /* Function vect_analyze_data_ref_dependences.
436 Examine all the data references in the loop, and make sure there do not
437 exist any data dependences between them. Set *MAX_VF according to
438 the maximum vectorization factor the data dependences allow. */
440 bool
441 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
443 unsigned int i;
444 struct data_dependence_relation *ddr;
446 if (dump_enabled_p ())
447 dump_printf_loc (MSG_NOTE, vect_location,
448 "=== vect_analyze_data_ref_dependences ===\n");
450 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
451 &LOOP_VINFO_DDRS (loop_vinfo),
452 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
453 return false;
455 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
456 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
457 return false;
459 return true;
463 /* Function vect_slp_analyze_data_ref_dependence.
465 Return TRUE if there (might) exist a dependence between a memory-reference
466 DRA and a memory-reference DRB. When versioning for alias may check a
467 dependence at run-time, return FALSE. Adjust *MAX_VF according to
468 the data dependence. */
470 static bool
471 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
473 struct data_reference *dra = DDR_A (ddr);
474 struct data_reference *drb = DDR_B (ddr);
476 /* We need to check dependences of statements marked as unvectorizable
477 as well, they still can prohibit vectorization. */
479 /* Independent data accesses. */
480 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
481 return false;
483 if (dra == drb)
484 return false;
486 /* Read-read is OK. */
487 if (DR_IS_READ (dra) && DR_IS_READ (drb))
488 return false;
490 /* If dra and drb are part of the same interleaving chain consider
491 them independent. */
492 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
493 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
494 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
495 return false;
497 /* Unknown data dependence. */
498 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
500 if (dump_enabled_p ())
502 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
503 "can't determine dependence between ");
504 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
505 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
506 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
507 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
510 else if (dump_enabled_p ())
512 dump_printf_loc (MSG_NOTE, vect_location,
513 "determined dependence between ");
514 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
515 dump_printf (MSG_NOTE, " and ");
516 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
517 dump_printf (MSG_NOTE, "\n");
520 /* We do not vectorize basic blocks with write-write dependencies. */
521 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
522 return true;
524 /* If we have a read-write dependence check that the load is before the store.
525 When we vectorize basic blocks, vector load can be only before
526 corresponding scalar load, and vector store can be only after its
527 corresponding scalar store. So the order of the acceses is preserved in
528 case the load is before the store. */
529 gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
530 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
532 /* That only holds for load-store pairs taking part in vectorization. */
533 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
534 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
535 return false;
538 return true;
542 /* Function vect_analyze_data_ref_dependences.
544 Examine all the data references in the basic-block, and make sure there
545 do not exist any data dependences between them. Set *MAX_VF according to
546 the maximum vectorization factor the data dependences allow. */
548 bool
549 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
551 struct data_dependence_relation *ddr;
552 unsigned int i;
554 if (dump_enabled_p ())
555 dump_printf_loc (MSG_NOTE, vect_location,
556 "=== vect_slp_analyze_data_ref_dependences ===\n");
558 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
559 &BB_VINFO_DDRS (bb_vinfo),
560 vNULL, true))
561 return false;
563 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
564 if (vect_slp_analyze_data_ref_dependence (ddr))
565 return false;
567 return true;
571 /* Function vect_compute_data_ref_alignment
573 Compute the misalignment of the data reference DR.
575 Output:
576 1. If during the misalignment computation it is found that the data reference
577 cannot be vectorized then false is returned.
578 2. DR_MISALIGNMENT (DR) is defined.
580 FOR NOW: No analysis is actually performed. Misalignment is calculated
581 only for trivial cases. TODO. */
583 static bool
584 vect_compute_data_ref_alignment (struct data_reference *dr)
586 gimple stmt = DR_STMT (dr);
587 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
588 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
589 struct loop *loop = NULL;
590 tree ref = DR_REF (dr);
591 tree vectype;
592 tree base, base_addr;
593 bool base_aligned;
594 tree misalign;
595 tree aligned_to, alignment;
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_NOTE, vect_location,
599 "vect_compute_data_ref_alignment:\n");
601 if (loop_vinfo)
602 loop = LOOP_VINFO_LOOP (loop_vinfo);
604 /* Initialize misalignment to unknown. */
605 SET_DR_MISALIGNMENT (dr, -1);
607 /* Strided loads perform only component accesses, misalignment information
608 is irrelevant for them. */
609 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
610 return true;
612 misalign = DR_INIT (dr);
613 aligned_to = DR_ALIGNED_TO (dr);
614 base_addr = DR_BASE_ADDRESS (dr);
615 vectype = STMT_VINFO_VECTYPE (stmt_info);
617 /* In case the dataref is in an inner-loop of the loop that is being
618 vectorized (LOOP), we use the base and misalignment information
619 relative to the outer-loop (LOOP). This is ok only if the misalignment
620 stays the same throughout the execution of the inner-loop, which is why
621 we have to check that the stride of the dataref in the inner-loop evenly
622 divides by the vector size. */
623 if (loop && nested_in_vect_loop_p (loop, stmt))
625 tree step = DR_STEP (dr);
626 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
628 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
630 if (dump_enabled_p ())
631 dump_printf_loc (MSG_NOTE, vect_location,
632 "inner step divides the vector-size.\n");
633 misalign = STMT_VINFO_DR_INIT (stmt_info);
634 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
635 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
637 else
639 if (dump_enabled_p ())
640 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
641 "inner step doesn't divide the vector-size.\n");
642 misalign = NULL_TREE;
646 /* Similarly, if we're doing basic-block vectorization, we can only use
647 base and misalignment information relative to an innermost loop if the
648 misalignment stays the same throughout the execution of the loop.
649 As above, this is the case if the stride of the dataref evenly divides
650 by the vector size. */
651 if (!loop)
653 tree step = DR_STEP (dr);
654 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
656 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
658 if (dump_enabled_p ())
659 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
660 "SLP: step doesn't divide the vector-size.\n");
661 misalign = NULL_TREE;
665 base = build_fold_indirect_ref (base_addr);
666 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
668 if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
669 || !misalign)
671 if (dump_enabled_p ())
673 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
674 "Unknown alignment for access: ");
675 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, base);
676 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
678 return true;
681 if ((DECL_P (base)
682 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
683 alignment) >= 0)
684 || (TREE_CODE (base_addr) == SSA_NAME
685 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
686 TREE_TYPE (base_addr)))),
687 alignment) >= 0)
688 || (get_pointer_alignment (base_addr) >= TYPE_ALIGN (vectype)))
689 base_aligned = true;
690 else
691 base_aligned = false;
693 if (!base_aligned)
695 /* Do not change the alignment of global variables here if
696 flag_section_anchors is enabled as we already generated
697 RTL for other functions. Most global variables should
698 have been aligned during the IPA increase_alignment pass. */
699 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
700 || (TREE_STATIC (base) && flag_section_anchors))
702 if (dump_enabled_p ())
704 dump_printf_loc (MSG_NOTE, vect_location,
705 "can't force alignment of ref: ");
706 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
707 dump_printf (MSG_NOTE, "\n");
709 return true;
712 /* Force the alignment of the decl.
713 NOTE: This is the only change to the code we make during
714 the analysis phase, before deciding to vectorize the loop. */
715 if (dump_enabled_p ())
717 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
718 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
719 dump_printf (MSG_NOTE, "\n");
722 ((dataref_aux *)dr->aux)->base_decl = base;
723 ((dataref_aux *)dr->aux)->base_misaligned = true;
726 /* If this is a backward running DR then first access in the larger
727 vectype actually is N-1 elements before the address in the DR.
728 Adjust misalign accordingly. */
729 if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
731 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
732 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
733 otherwise we wouldn't be here. */
734 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
735 /* PLUS because DR_STEP was negative. */
736 misalign = size_binop (PLUS_EXPR, misalign, offset);
739 /* Modulo alignment. */
740 misalign = size_binop (FLOOR_MOD_EXPR, misalign, alignment);
742 if (!tree_fits_uhwi_p (misalign))
744 /* Negative or overflowed misalignment value. */
745 if (dump_enabled_p ())
746 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
747 "unexpected misalign value\n");
748 return false;
751 SET_DR_MISALIGNMENT (dr, tree_to_uhwi (misalign));
753 if (dump_enabled_p ())
755 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
756 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
757 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
758 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
761 return true;
765 /* Function vect_compute_data_refs_alignment
767 Compute the misalignment of data references in the loop.
768 Return FALSE if a data reference is found that cannot be vectorized. */
770 static bool
771 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
772 bb_vec_info bb_vinfo)
774 vec<data_reference_p> datarefs;
775 struct data_reference *dr;
776 unsigned int i;
778 if (loop_vinfo)
779 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
780 else
781 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
783 FOR_EACH_VEC_ELT (datarefs, i, dr)
784 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
785 && !vect_compute_data_ref_alignment (dr))
787 if (bb_vinfo)
789 /* Mark unsupported statement as unvectorizable. */
790 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
791 continue;
793 else
794 return false;
797 return true;
801 /* Function vect_update_misalignment_for_peel
803 DR - the data reference whose misalignment is to be adjusted.
804 DR_PEEL - the data reference whose misalignment is being made
805 zero in the vector loop by the peel.
806 NPEEL - the number of iterations in the peel loop if the misalignment
807 of DR_PEEL is known at compile time. */
809 static void
810 vect_update_misalignment_for_peel (struct data_reference *dr,
811 struct data_reference *dr_peel, int npeel)
813 unsigned int i;
814 vec<dr_p> same_align_drs;
815 struct data_reference *current_dr;
816 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
817 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
818 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
819 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
821 /* For interleaved data accesses the step in the loop must be multiplied by
822 the size of the interleaving group. */
823 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
824 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
825 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
826 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
828 /* It can be assumed that the data refs with the same alignment as dr_peel
829 are aligned in the vector loop. */
830 same_align_drs
831 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
832 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
834 if (current_dr != dr)
835 continue;
836 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
837 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
838 SET_DR_MISALIGNMENT (dr, 0);
839 return;
842 if (known_alignment_for_access_p (dr)
843 && known_alignment_for_access_p (dr_peel))
845 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
846 int misal = DR_MISALIGNMENT (dr);
847 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
848 misal += negative ? -npeel * dr_size : npeel * dr_size;
849 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
850 SET_DR_MISALIGNMENT (dr, misal);
851 return;
854 if (dump_enabled_p ())
855 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
856 SET_DR_MISALIGNMENT (dr, -1);
860 /* Function vect_verify_datarefs_alignment
862 Return TRUE if all data references in the loop can be
863 handled with respect to alignment. */
865 bool
866 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
868 vec<data_reference_p> datarefs;
869 struct data_reference *dr;
870 enum dr_alignment_support supportable_dr_alignment;
871 unsigned int i;
873 if (loop_vinfo)
874 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
875 else
876 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
878 FOR_EACH_VEC_ELT (datarefs, i, dr)
880 gimple stmt = DR_STMT (dr);
881 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
883 if (!STMT_VINFO_RELEVANT_P (stmt_info))
884 continue;
886 /* For interleaving, only the alignment of the first access matters.
887 Skip statements marked as not vectorizable. */
888 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
889 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
890 || !STMT_VINFO_VECTORIZABLE (stmt_info))
891 continue;
893 /* Strided loads perform only component accesses, alignment is
894 irrelevant for them. */
895 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
896 continue;
898 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
899 if (!supportable_dr_alignment)
901 if (dump_enabled_p ())
903 if (DR_IS_READ (dr))
904 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
905 "not vectorized: unsupported unaligned load.");
906 else
907 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
908 "not vectorized: unsupported unaligned "
909 "store.");
911 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
912 DR_REF (dr));
913 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
915 return false;
917 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
918 dump_printf_loc (MSG_NOTE, vect_location,
919 "Vectorizing an unaligned access.\n");
921 return true;
924 /* Given an memory reference EXP return whether its alignment is less
925 than its size. */
927 static bool
928 not_size_aligned (tree exp)
930 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
931 return true;
933 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
934 > get_object_alignment (exp));
937 /* Function vector_alignment_reachable_p
939 Return true if vector alignment for DR is reachable by peeling
940 a few loop iterations. Return false otherwise. */
942 static bool
943 vector_alignment_reachable_p (struct data_reference *dr)
945 gimple stmt = DR_STMT (dr);
946 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
947 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
949 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
951 /* For interleaved access we peel only if number of iterations in
952 the prolog loop ({VF - misalignment}), is a multiple of the
953 number of the interleaved accesses. */
954 int elem_size, mis_in_elements;
955 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
957 /* FORNOW: handle only known alignment. */
958 if (!known_alignment_for_access_p (dr))
959 return false;
961 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
962 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
964 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
965 return false;
968 /* If misalignment is known at the compile time then allow peeling
969 only if natural alignment is reachable through peeling. */
970 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
972 HOST_WIDE_INT elmsize =
973 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
974 if (dump_enabled_p ())
976 dump_printf_loc (MSG_NOTE, vect_location,
977 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
978 dump_printf (MSG_NOTE,
979 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
981 if (DR_MISALIGNMENT (dr) % elmsize)
983 if (dump_enabled_p ())
984 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
985 "data size does not divide the misalignment.\n");
986 return false;
990 if (!known_alignment_for_access_p (dr))
992 tree type = TREE_TYPE (DR_REF (dr));
993 bool is_packed = not_size_aligned (DR_REF (dr));
994 if (dump_enabled_p ())
995 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
996 "Unknown misalignment, is_packed = %d\n",is_packed);
997 if ((TYPE_USER_ALIGN (type) && !is_packed)
998 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
999 return true;
1000 else
1001 return false;
1004 return true;
1008 /* Calculate the cost of the memory access represented by DR. */
1010 static void
1011 vect_get_data_access_cost (struct data_reference *dr,
1012 unsigned int *inside_cost,
1013 unsigned int *outside_cost,
1014 stmt_vector_for_cost *body_cost_vec)
1016 gimple stmt = DR_STMT (dr);
1017 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1018 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1019 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1020 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1021 int ncopies = vf / nunits;
1023 if (DR_IS_READ (dr))
1024 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1025 NULL, body_cost_vec, false);
1026 else
1027 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1029 if (dump_enabled_p ())
1030 dump_printf_loc (MSG_NOTE, vect_location,
1031 "vect_get_data_access_cost: inside_cost = %d, "
1032 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1036 /* Insert DR into peeling hash table with NPEEL as key. */
1038 static void
1039 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1040 int npeel)
1042 struct _vect_peel_info elem, *slot;
1043 _vect_peel_info **new_slot;
1044 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1046 elem.npeel = npeel;
1047 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find (&elem);
1048 if (slot)
1049 slot->count++;
1050 else
1052 slot = XNEW (struct _vect_peel_info);
1053 slot->npeel = npeel;
1054 slot->dr = dr;
1055 slot->count = 1;
1056 new_slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find_slot (slot, INSERT);
1057 *new_slot = slot;
1060 if (!supportable_dr_alignment
1061 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1062 slot->count += VECT_MAX_COST;
1066 /* Traverse peeling hash table to find peeling option that aligns maximum
1067 number of data accesses. */
1070 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1071 _vect_peel_extended_info *max)
1073 vect_peel_info elem = *slot;
1075 if (elem->count > max->peel_info.count
1076 || (elem->count == max->peel_info.count
1077 && max->peel_info.npeel > elem->npeel))
1079 max->peel_info.npeel = elem->npeel;
1080 max->peel_info.count = elem->count;
1081 max->peel_info.dr = elem->dr;
1084 return 1;
1088 /* Traverse peeling hash table and calculate cost for each peeling option.
1089 Find the one with the lowest cost. */
1092 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1093 _vect_peel_extended_info *min)
1095 vect_peel_info elem = *slot;
1096 int save_misalignment, dummy;
1097 unsigned int inside_cost = 0, outside_cost = 0, i;
1098 gimple stmt = DR_STMT (elem->dr);
1099 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1100 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1101 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1102 struct data_reference *dr;
1103 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1104 int single_iter_cost;
1106 prologue_cost_vec.create (2);
1107 body_cost_vec.create (2);
1108 epilogue_cost_vec.create (2);
1110 FOR_EACH_VEC_ELT (datarefs, i, dr)
1112 stmt = DR_STMT (dr);
1113 stmt_info = vinfo_for_stmt (stmt);
1114 /* For interleaving, only the alignment of the first access
1115 matters. */
1116 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1117 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1118 continue;
1120 save_misalignment = DR_MISALIGNMENT (dr);
1121 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1122 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1123 &body_cost_vec);
1124 SET_DR_MISALIGNMENT (dr, save_misalignment);
1127 single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
1128 outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel,
1129 &dummy, single_iter_cost,
1130 &prologue_cost_vec,
1131 &epilogue_cost_vec);
1133 /* Prologue and epilogue costs are added to the target model later.
1134 These costs depend only on the scalar iteration cost, the
1135 number of peeling iterations finally chosen, and the number of
1136 misaligned statements. So discard the information found here. */
1137 prologue_cost_vec.release ();
1138 epilogue_cost_vec.release ();
1140 if (inside_cost < min->inside_cost
1141 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1143 min->inside_cost = inside_cost;
1144 min->outside_cost = outside_cost;
1145 min->body_cost_vec.release ();
1146 min->body_cost_vec = body_cost_vec;
1147 min->peel_info.dr = elem->dr;
1148 min->peel_info.npeel = elem->npeel;
1150 else
1151 body_cost_vec.release ();
1153 return 1;
1157 /* Choose best peeling option by traversing peeling hash table and either
1158 choosing an option with the lowest cost (if cost model is enabled) or the
1159 option that aligns as many accesses as possible. */
1161 static struct data_reference *
1162 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1163 unsigned int *npeel,
1164 stmt_vector_for_cost *body_cost_vec)
1166 struct _vect_peel_extended_info res;
1168 res.peel_info.dr = NULL;
1169 res.body_cost_vec = stmt_vector_for_cost ();
1171 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1173 res.inside_cost = INT_MAX;
1174 res.outside_cost = INT_MAX;
1175 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1176 .traverse <_vect_peel_extended_info *,
1177 vect_peeling_hash_get_lowest_cost> (&res);
1179 else
1181 res.peel_info.count = 0;
1182 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1183 .traverse <_vect_peel_extended_info *,
1184 vect_peeling_hash_get_most_frequent> (&res);
1187 *npeel = res.peel_info.npeel;
1188 *body_cost_vec = res.body_cost_vec;
1189 return res.peel_info.dr;
1193 /* Function vect_enhance_data_refs_alignment
1195 This pass will use loop versioning and loop peeling in order to enhance
1196 the alignment of data references in the loop.
1198 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1199 original loop is to be vectorized. Any other loops that are created by
1200 the transformations performed in this pass - are not supposed to be
1201 vectorized. This restriction will be relaxed.
1203 This pass will require a cost model to guide it whether to apply peeling
1204 or versioning or a combination of the two. For example, the scheme that
1205 intel uses when given a loop with several memory accesses, is as follows:
1206 choose one memory access ('p') which alignment you want to force by doing
1207 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1208 other accesses are not necessarily aligned, or (2) use loop versioning to
1209 generate one loop in which all accesses are aligned, and another loop in
1210 which only 'p' is necessarily aligned.
1212 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1213 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1214 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1216 Devising a cost model is the most critical aspect of this work. It will
1217 guide us on which access to peel for, whether to use loop versioning, how
1218 many versions to create, etc. The cost model will probably consist of
1219 generic considerations as well as target specific considerations (on
1220 powerpc for example, misaligned stores are more painful than misaligned
1221 loads).
1223 Here are the general steps involved in alignment enhancements:
1225 -- original loop, before alignment analysis:
1226 for (i=0; i<N; i++){
1227 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1228 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1231 -- After vect_compute_data_refs_alignment:
1232 for (i=0; i<N; i++){
1233 x = q[i]; # DR_MISALIGNMENT(q) = 3
1234 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1237 -- Possibility 1: we do loop versioning:
1238 if (p is aligned) {
1239 for (i=0; i<N; i++){ # loop 1A
1240 x = q[i]; # DR_MISALIGNMENT(q) = 3
1241 p[i] = y; # DR_MISALIGNMENT(p) = 0
1244 else {
1245 for (i=0; i<N; i++){ # loop 1B
1246 x = q[i]; # DR_MISALIGNMENT(q) = 3
1247 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1251 -- Possibility 2: we do loop peeling:
1252 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1253 x = q[i];
1254 p[i] = y;
1256 for (i = 3; i < N; i++){ # loop 2A
1257 x = q[i]; # DR_MISALIGNMENT(q) = 0
1258 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1261 -- Possibility 3: combination of loop peeling and versioning:
1262 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1263 x = q[i];
1264 p[i] = y;
1266 if (p is aligned) {
1267 for (i = 3; i<N; i++){ # loop 3A
1268 x = q[i]; # DR_MISALIGNMENT(q) = 0
1269 p[i] = y; # DR_MISALIGNMENT(p) = 0
1272 else {
1273 for (i = 3; i<N; i++){ # loop 3B
1274 x = q[i]; # DR_MISALIGNMENT(q) = 0
1275 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1279 These loops are later passed to loop_transform to be vectorized. The
1280 vectorizer will use the alignment information to guide the transformation
1281 (whether to generate regular loads/stores, or with special handling for
1282 misalignment). */
1284 bool
1285 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1287 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1288 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1289 enum dr_alignment_support supportable_dr_alignment;
1290 struct data_reference *dr0 = NULL, *first_store = NULL;
1291 struct data_reference *dr;
1292 unsigned int i, j;
1293 bool do_peeling = false;
1294 bool do_versioning = false;
1295 bool stat;
1296 gimple stmt;
1297 stmt_vec_info stmt_info;
1298 unsigned int npeel = 0;
1299 bool all_misalignments_unknown = true;
1300 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1301 unsigned possible_npeel_number = 1;
1302 tree vectype;
1303 unsigned int nelements, mis, same_align_drs_max = 0;
1304 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1306 if (dump_enabled_p ())
1307 dump_printf_loc (MSG_NOTE, vect_location,
1308 "=== vect_enhance_data_refs_alignment ===\n");
1310 /* While cost model enhancements are expected in the future, the high level
1311 view of the code at this time is as follows:
1313 A) If there is a misaligned access then see if peeling to align
1314 this access can make all data references satisfy
1315 vect_supportable_dr_alignment. If so, update data structures
1316 as needed and return true.
1318 B) If peeling wasn't possible and there is a data reference with an
1319 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1320 then see if loop versioning checks can be used to make all data
1321 references satisfy vect_supportable_dr_alignment. If so, update
1322 data structures as needed and return true.
1324 C) If neither peeling nor versioning were successful then return false if
1325 any data reference does not satisfy vect_supportable_dr_alignment.
1327 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1329 Note, Possibility 3 above (which is peeling and versioning together) is not
1330 being done at this time. */
1332 /* (1) Peeling to force alignment. */
1334 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1335 Considerations:
1336 + How many accesses will become aligned due to the peeling
1337 - How many accesses will become unaligned due to the peeling,
1338 and the cost of misaligned accesses.
1339 - The cost of peeling (the extra runtime checks, the increase
1340 in code size). */
1342 FOR_EACH_VEC_ELT (datarefs, i, dr)
1344 stmt = DR_STMT (dr);
1345 stmt_info = vinfo_for_stmt (stmt);
1347 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1348 continue;
1350 /* For interleaving, only the alignment of the first access
1351 matters. */
1352 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1353 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1354 continue;
1356 /* For invariant accesses there is nothing to enhance. */
1357 if (integer_zerop (DR_STEP (dr)))
1358 continue;
1360 /* Strided loads perform only component accesses, alignment is
1361 irrelevant for them. */
1362 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1363 continue;
1365 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1366 do_peeling = vector_alignment_reachable_p (dr);
1367 if (do_peeling)
1369 if (known_alignment_for_access_p (dr))
1371 unsigned int npeel_tmp;
1372 bool negative = tree_int_cst_compare (DR_STEP (dr),
1373 size_zero_node) < 0;
1375 /* Save info about DR in the hash table. */
1376 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo).is_created ())
1377 LOOP_VINFO_PEELING_HTAB (loop_vinfo).create (1);
1379 vectype = STMT_VINFO_VECTYPE (stmt_info);
1380 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1381 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1382 TREE_TYPE (DR_REF (dr))));
1383 npeel_tmp = (negative
1384 ? (mis - nelements) : (nelements - mis))
1385 & (nelements - 1);
1387 /* For multiple types, it is possible that the bigger type access
1388 will have more than one peeling option. E.g., a loop with two
1389 types: one of size (vector size / 4), and the other one of
1390 size (vector size / 8). Vectorization factor will 8. If both
1391 access are misaligned by 3, the first one needs one scalar
1392 iteration to be aligned, and the second one needs 5. But the
1393 the first one will be aligned also by peeling 5 scalar
1394 iterations, and in that case both accesses will be aligned.
1395 Hence, except for the immediate peeling amount, we also want
1396 to try to add full vector size, while we don't exceed
1397 vectorization factor.
1398 We do this automtically for cost model, since we calculate cost
1399 for every peeling option. */
1400 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1401 possible_npeel_number = vf /nelements;
1403 /* Handle the aligned case. We may decide to align some other
1404 access, making DR unaligned. */
1405 if (DR_MISALIGNMENT (dr) == 0)
1407 npeel_tmp = 0;
1408 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1409 possible_npeel_number++;
1412 for (j = 0; j < possible_npeel_number; j++)
1414 gcc_assert (npeel_tmp <= vf);
1415 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1416 npeel_tmp += nelements;
1419 all_misalignments_unknown = false;
1420 /* Data-ref that was chosen for the case that all the
1421 misalignments are unknown is not relevant anymore, since we
1422 have a data-ref with known alignment. */
1423 dr0 = NULL;
1425 else
1427 /* If we don't know any misalignment values, we prefer
1428 peeling for data-ref that has the maximum number of data-refs
1429 with the same alignment, unless the target prefers to align
1430 stores over load. */
1431 if (all_misalignments_unknown)
1433 unsigned same_align_drs
1434 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1435 if (!dr0
1436 || same_align_drs_max < same_align_drs)
1438 same_align_drs_max = same_align_drs;
1439 dr0 = dr;
1441 /* For data-refs with the same number of related
1442 accesses prefer the one where the misalign
1443 computation will be invariant in the outermost loop. */
1444 else if (same_align_drs_max == same_align_drs)
1446 struct loop *ivloop0, *ivloop;
1447 ivloop0 = outermost_invariant_loop_for_expr
1448 (loop, DR_BASE_ADDRESS (dr0));
1449 ivloop = outermost_invariant_loop_for_expr
1450 (loop, DR_BASE_ADDRESS (dr));
1451 if ((ivloop && !ivloop0)
1452 || (ivloop && ivloop0
1453 && flow_loop_nested_p (ivloop, ivloop0)))
1454 dr0 = dr;
1457 if (!first_store && DR_IS_WRITE (dr))
1458 first_store = dr;
1461 /* If there are both known and unknown misaligned accesses in the
1462 loop, we choose peeling amount according to the known
1463 accesses. */
1464 if (!supportable_dr_alignment)
1466 dr0 = dr;
1467 if (!first_store && DR_IS_WRITE (dr))
1468 first_store = dr;
1472 else
1474 if (!aligned_access_p (dr))
1476 if (dump_enabled_p ())
1477 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1478 "vector alignment may not be reachable\n");
1479 break;
1484 /* Check if we can possibly peel the loop. */
1485 if (!vect_can_advance_ivs_p (loop_vinfo)
1486 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1487 do_peeling = false;
1489 if (do_peeling && all_misalignments_unknown
1490 && vect_supportable_dr_alignment (dr0, false))
1493 /* Check if the target requires to prefer stores over loads, i.e., if
1494 misaligned stores are more expensive than misaligned loads (taking
1495 drs with same alignment into account). */
1496 if (first_store && DR_IS_READ (dr0))
1498 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1499 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1500 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1501 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1502 stmt_vector_for_cost dummy;
1503 dummy.create (2);
1505 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1506 &dummy);
1507 vect_get_data_access_cost (first_store, &store_inside_cost,
1508 &store_outside_cost, &dummy);
1510 dummy.release ();
1512 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1513 aligning the load DR0). */
1514 load_inside_penalty = store_inside_cost;
1515 load_outside_penalty = store_outside_cost;
1516 for (i = 0;
1517 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1518 DR_STMT (first_store))).iterate (i, &dr);
1519 i++)
1520 if (DR_IS_READ (dr))
1522 load_inside_penalty += load_inside_cost;
1523 load_outside_penalty += load_outside_cost;
1525 else
1527 load_inside_penalty += store_inside_cost;
1528 load_outside_penalty += store_outside_cost;
1531 /* Calculate the penalty for leaving DR0 unaligned (by
1532 aligning the FIRST_STORE). */
1533 store_inside_penalty = load_inside_cost;
1534 store_outside_penalty = load_outside_cost;
1535 for (i = 0;
1536 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1537 DR_STMT (dr0))).iterate (i, &dr);
1538 i++)
1539 if (DR_IS_READ (dr))
1541 store_inside_penalty += load_inside_cost;
1542 store_outside_penalty += load_outside_cost;
1544 else
1546 store_inside_penalty += store_inside_cost;
1547 store_outside_penalty += store_outside_cost;
1550 if (load_inside_penalty > store_inside_penalty
1551 || (load_inside_penalty == store_inside_penalty
1552 && load_outside_penalty > store_outside_penalty))
1553 dr0 = first_store;
1556 /* In case there are only loads with different unknown misalignments, use
1557 peeling only if it may help to align other accesses in the loop. */
1558 if (!first_store
1559 && !STMT_VINFO_SAME_ALIGN_REFS (
1560 vinfo_for_stmt (DR_STMT (dr0))).length ()
1561 && vect_supportable_dr_alignment (dr0, false)
1562 != dr_unaligned_supported)
1563 do_peeling = false;
1566 if (do_peeling && !dr0)
1568 /* Peeling is possible, but there is no data access that is not supported
1569 unless aligned. So we try to choose the best possible peeling. */
1571 /* We should get here only if there are drs with known misalignment. */
1572 gcc_assert (!all_misalignments_unknown);
1574 /* Choose the best peeling from the hash table. */
1575 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1576 &body_cost_vec);
1577 if (!dr0 || !npeel)
1578 do_peeling = false;
1581 if (do_peeling)
1583 stmt = DR_STMT (dr0);
1584 stmt_info = vinfo_for_stmt (stmt);
1585 vectype = STMT_VINFO_VECTYPE (stmt_info);
1586 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1588 if (known_alignment_for_access_p (dr0))
1590 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1591 size_zero_node) < 0;
1592 if (!npeel)
1594 /* Since it's known at compile time, compute the number of
1595 iterations in the peeled loop (the peeling factor) for use in
1596 updating DR_MISALIGNMENT values. The peeling factor is the
1597 vectorization factor minus the misalignment as an element
1598 count. */
1599 mis = DR_MISALIGNMENT (dr0);
1600 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1601 npeel = ((negative ? mis - nelements : nelements - mis)
1602 & (nelements - 1));
1605 /* For interleaved data access every iteration accesses all the
1606 members of the group, therefore we divide the number of iterations
1607 by the group size. */
1608 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1609 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1610 npeel /= GROUP_SIZE (stmt_info);
1612 if (dump_enabled_p ())
1613 dump_printf_loc (MSG_NOTE, vect_location,
1614 "Try peeling by %d\n", npeel);
1617 /* Ensure that all data refs can be vectorized after the peel. */
1618 FOR_EACH_VEC_ELT (datarefs, i, dr)
1620 int save_misalignment;
1622 if (dr == dr0)
1623 continue;
1625 stmt = DR_STMT (dr);
1626 stmt_info = vinfo_for_stmt (stmt);
1627 /* For interleaving, only the alignment of the first access
1628 matters. */
1629 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1630 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1631 continue;
1633 /* Strided loads perform only component accesses, alignment is
1634 irrelevant for them. */
1635 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1636 continue;
1638 save_misalignment = DR_MISALIGNMENT (dr);
1639 vect_update_misalignment_for_peel (dr, dr0, npeel);
1640 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1641 SET_DR_MISALIGNMENT (dr, save_misalignment);
1643 if (!supportable_dr_alignment)
1645 do_peeling = false;
1646 break;
1650 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1652 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1653 if (!stat)
1654 do_peeling = false;
1655 else
1657 body_cost_vec.release ();
1658 return stat;
1662 if (do_peeling)
1664 unsigned max_allowed_peel
1665 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1666 if (max_allowed_peel != (unsigned)-1)
1668 unsigned max_peel = npeel;
1669 if (max_peel == 0)
1671 gimple dr_stmt = DR_STMT (dr0);
1672 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1673 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1674 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1676 if (max_peel > max_allowed_peel)
1678 do_peeling = false;
1679 if (dump_enabled_p ())
1680 dump_printf_loc (MSG_NOTE, vect_location,
1681 "Disable peeling, max peels reached: %d\n", max_peel);
1686 if (do_peeling)
1688 stmt_info_for_cost *si;
1689 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
1691 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1692 If the misalignment of DR_i is identical to that of dr0 then set
1693 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1694 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1695 by the peeling factor times the element size of DR_i (MOD the
1696 vectorization factor times the size). Otherwise, the
1697 misalignment of DR_i must be set to unknown. */
1698 FOR_EACH_VEC_ELT (datarefs, i, dr)
1699 if (dr != dr0)
1700 vect_update_misalignment_for_peel (dr, dr0, npeel);
1702 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1703 if (npeel)
1704 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1705 else
1706 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1707 = DR_MISALIGNMENT (dr0);
1708 SET_DR_MISALIGNMENT (dr0, 0);
1709 if (dump_enabled_p ())
1711 dump_printf_loc (MSG_NOTE, vect_location,
1712 "Alignment of access forced using peeling.\n");
1713 dump_printf_loc (MSG_NOTE, vect_location,
1714 "Peeling for alignment will be applied.\n");
1716 /* We've delayed passing the inside-loop peeling costs to the
1717 target cost model until we were sure peeling would happen.
1718 Do so now. */
1719 if (body_cost_vec.exists ())
1721 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1723 struct _stmt_vec_info *stmt_info
1724 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1725 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1726 si->misalign, vect_body);
1728 body_cost_vec.release ();
1731 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1732 gcc_assert (stat);
1733 return stat;
1737 body_cost_vec.release ();
1739 /* (2) Versioning to force alignment. */
1741 /* Try versioning if:
1742 1) optimize loop for speed
1743 2) there is at least one unsupported misaligned data ref with an unknown
1744 misalignment, and
1745 3) all misaligned data refs with a known misalignment are supported, and
1746 4) the number of runtime alignment checks is within reason. */
1748 do_versioning =
1749 optimize_loop_nest_for_speed_p (loop)
1750 && (!loop->inner); /* FORNOW */
1752 if (do_versioning)
1754 FOR_EACH_VEC_ELT (datarefs, i, dr)
1756 stmt = DR_STMT (dr);
1757 stmt_info = vinfo_for_stmt (stmt);
1759 /* For interleaving, only the alignment of the first access
1760 matters. */
1761 if (aligned_access_p (dr)
1762 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1763 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1764 continue;
1766 /* Strided loads perform only component accesses, alignment is
1767 irrelevant for them. */
1768 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1769 continue;
1771 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1773 if (!supportable_dr_alignment)
1775 gimple stmt;
1776 int mask;
1777 tree vectype;
1779 if (known_alignment_for_access_p (dr)
1780 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1781 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1783 do_versioning = false;
1784 break;
1787 stmt = DR_STMT (dr);
1788 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1789 gcc_assert (vectype);
1791 /* The rightmost bits of an aligned address must be zeros.
1792 Construct the mask needed for this test. For example,
1793 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1794 mask must be 15 = 0xf. */
1795 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1797 /* FORNOW: use the same mask to test all potentially unaligned
1798 references in the loop. The vectorizer currently supports
1799 a single vector size, see the reference to
1800 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1801 vectorization factor is computed. */
1802 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1803 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1804 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1805 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1806 DR_STMT (dr));
1810 /* Versioning requires at least one misaligned data reference. */
1811 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1812 do_versioning = false;
1813 else if (!do_versioning)
1814 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1817 if (do_versioning)
1819 vec<gimple> may_misalign_stmts
1820 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1821 gimple stmt;
1823 /* It can now be assumed that the data references in the statements
1824 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1825 of the loop being vectorized. */
1826 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1828 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1829 dr = STMT_VINFO_DATA_REF (stmt_info);
1830 SET_DR_MISALIGNMENT (dr, 0);
1831 if (dump_enabled_p ())
1832 dump_printf_loc (MSG_NOTE, vect_location,
1833 "Alignment of access forced using versioning.\n");
1836 if (dump_enabled_p ())
1837 dump_printf_loc (MSG_NOTE, vect_location,
1838 "Versioning for alignment will be applied.\n");
1840 /* Peeling and versioning can't be done together at this time. */
1841 gcc_assert (! (do_peeling && do_versioning));
1843 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1844 gcc_assert (stat);
1845 return stat;
1848 /* This point is reached if neither peeling nor versioning is being done. */
1849 gcc_assert (! (do_peeling || do_versioning));
1851 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1852 return stat;
1856 /* Function vect_find_same_alignment_drs.
1858 Update group and alignment relations according to the chosen
1859 vectorization factor. */
1861 static void
1862 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1863 loop_vec_info loop_vinfo)
1865 unsigned int i;
1866 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1867 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1868 struct data_reference *dra = DDR_A (ddr);
1869 struct data_reference *drb = DDR_B (ddr);
1870 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1871 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1872 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1873 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1874 lambda_vector dist_v;
1875 unsigned int loop_depth;
1877 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1878 return;
1880 if (dra == drb)
1881 return;
1883 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1884 return;
1886 /* Loop-based vectorization and known data dependence. */
1887 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1888 return;
1890 /* Data-dependence analysis reports a distance vector of zero
1891 for data-references that overlap only in the first iteration
1892 but have different sign step (see PR45764).
1893 So as a sanity check require equal DR_STEP. */
1894 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1895 return;
1897 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1898 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1900 int dist = dist_v[loop_depth];
1902 if (dump_enabled_p ())
1903 dump_printf_loc (MSG_NOTE, vect_location,
1904 "dependence distance = %d.\n", dist);
1906 /* Same loop iteration. */
1907 if (dist == 0
1908 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1910 /* Two references with distance zero have the same alignment. */
1911 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1912 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1913 if (dump_enabled_p ())
1915 dump_printf_loc (MSG_NOTE, vect_location,
1916 "accesses have the same alignment.\n");
1917 dump_printf (MSG_NOTE,
1918 "dependence distance modulo vf == 0 between ");
1919 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1920 dump_printf (MSG_NOTE, " and ");
1921 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1922 dump_printf (MSG_NOTE, "\n");
1929 /* Function vect_analyze_data_refs_alignment
1931 Analyze the alignment of the data-references in the loop.
1932 Return FALSE if a data reference is found that cannot be vectorized. */
1934 bool
1935 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
1936 bb_vec_info bb_vinfo)
1938 if (dump_enabled_p ())
1939 dump_printf_loc (MSG_NOTE, vect_location,
1940 "=== vect_analyze_data_refs_alignment ===\n");
1942 /* Mark groups of data references with same alignment using
1943 data dependence information. */
1944 if (loop_vinfo)
1946 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
1947 struct data_dependence_relation *ddr;
1948 unsigned int i;
1950 FOR_EACH_VEC_ELT (ddrs, i, ddr)
1951 vect_find_same_alignment_drs (ddr, loop_vinfo);
1954 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
1956 if (dump_enabled_p ())
1957 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1958 "not vectorized: can't calculate alignment "
1959 "for data ref.\n");
1960 return false;
1963 return true;
1967 /* Analyze groups of accesses: check that DR belongs to a group of
1968 accesses of legal size, step, etc. Detect gaps, single element
1969 interleaving, and other special cases. Set grouped access info.
1970 Collect groups of strided stores for further use in SLP analysis. */
1972 static bool
1973 vect_analyze_group_access (struct data_reference *dr)
1975 tree step = DR_STEP (dr);
1976 tree scalar_type = TREE_TYPE (DR_REF (dr));
1977 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
1978 gimple stmt = DR_STMT (dr);
1979 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1980 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1981 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1982 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
1983 HOST_WIDE_INT groupsize, last_accessed_element = 1;
1984 bool slp_impossible = false;
1985 struct loop *loop = NULL;
1987 if (loop_vinfo)
1988 loop = LOOP_VINFO_LOOP (loop_vinfo);
1990 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
1991 size of the interleaving group (including gaps). */
1992 groupsize = absu_hwi (dr_step) / type_size;
1994 /* Not consecutive access is possible only if it is a part of interleaving. */
1995 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1997 /* Check if it this DR is a part of interleaving, and is a single
1998 element of the group that is accessed in the loop. */
2000 /* Gaps are supported only for loads. STEP must be a multiple of the type
2001 size. The size of the group must be a power of 2. */
2002 if (DR_IS_READ (dr)
2003 && (dr_step % type_size) == 0
2004 && groupsize > 0
2005 && exact_log2 (groupsize) != -1)
2007 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2008 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2009 if (dump_enabled_p ())
2011 dump_printf_loc (MSG_NOTE, vect_location,
2012 "Detected single element interleaving ");
2013 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2014 dump_printf (MSG_NOTE, " step ");
2015 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2016 dump_printf (MSG_NOTE, "\n");
2019 if (loop_vinfo)
2021 if (dump_enabled_p ())
2022 dump_printf_loc (MSG_NOTE, vect_location,
2023 "Data access with gaps requires scalar "
2024 "epilogue loop\n");
2025 if (loop->inner)
2027 if (dump_enabled_p ())
2028 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2029 "Peeling for outer loop is not"
2030 " supported\n");
2031 return false;
2034 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2037 return true;
2040 if (dump_enabled_p ())
2042 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2043 "not consecutive access ");
2044 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2045 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2048 if (bb_vinfo)
2050 /* Mark the statement as unvectorizable. */
2051 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2052 return true;
2055 return false;
2058 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2060 /* First stmt in the interleaving chain. Check the chain. */
2061 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2062 struct data_reference *data_ref = dr;
2063 unsigned int count = 1;
2064 tree prev_init = DR_INIT (data_ref);
2065 gimple prev = stmt;
2066 HOST_WIDE_INT diff, gaps = 0;
2067 unsigned HOST_WIDE_INT count_in_bytes;
2069 while (next)
2071 /* Skip same data-refs. In case that two or more stmts share
2072 data-ref (supported only for loads), we vectorize only the first
2073 stmt, and the rest get their vectorized loads from the first
2074 one. */
2075 if (!tree_int_cst_compare (DR_INIT (data_ref),
2076 DR_INIT (STMT_VINFO_DATA_REF (
2077 vinfo_for_stmt (next)))))
2079 if (DR_IS_WRITE (data_ref))
2081 if (dump_enabled_p ())
2082 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2083 "Two store stmts share the same dr.\n");
2084 return false;
2087 /* For load use the same data-ref load. */
2088 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2090 prev = next;
2091 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2092 continue;
2095 prev = next;
2096 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2098 /* All group members have the same STEP by construction. */
2099 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2101 /* Check that the distance between two accesses is equal to the type
2102 size. Otherwise, we have gaps. */
2103 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2104 - TREE_INT_CST_LOW (prev_init)) / type_size;
2105 if (diff != 1)
2107 /* FORNOW: SLP of accesses with gaps is not supported. */
2108 slp_impossible = true;
2109 if (DR_IS_WRITE (data_ref))
2111 if (dump_enabled_p ())
2112 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2113 "interleaved store with gaps\n");
2114 return false;
2117 gaps += diff - 1;
2120 last_accessed_element += diff;
2122 /* Store the gap from the previous member of the group. If there is no
2123 gap in the access, GROUP_GAP is always 1. */
2124 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2126 prev_init = DR_INIT (data_ref);
2127 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2128 /* Count the number of data-refs in the chain. */
2129 count++;
2132 /* COUNT is the number of accesses found, we multiply it by the size of
2133 the type to get COUNT_IN_BYTES. */
2134 count_in_bytes = type_size * count;
2136 /* Check that the size of the interleaving (including gaps) is not
2137 greater than STEP. */
2138 if (dr_step != 0
2139 && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
2141 if (dump_enabled_p ())
2143 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2144 "interleaving size is greater than step for ");
2145 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2146 DR_REF (dr));
2147 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2149 return false;
2152 /* Check that the size of the interleaving is equal to STEP for stores,
2153 i.e., that there are no gaps. */
2154 if (dr_step != 0
2155 && absu_hwi (dr_step) != count_in_bytes)
2157 if (DR_IS_READ (dr))
2159 slp_impossible = true;
2160 /* There is a gap after the last load in the group. This gap is a
2161 difference between the groupsize and the number of elements.
2162 When there is no gap, this difference should be 0. */
2163 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2165 else
2167 if (dump_enabled_p ())
2168 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2169 "interleaved store with gaps\n");
2170 return false;
2174 /* Check that STEP is a multiple of type size. */
2175 if (dr_step != 0
2176 && (dr_step % type_size) != 0)
2178 if (dump_enabled_p ())
2180 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2181 "step is not a multiple of type size: step ");
2182 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
2183 dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
2184 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2185 TYPE_SIZE_UNIT (scalar_type));
2186 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2188 return false;
2191 if (groupsize == 0)
2192 groupsize = count;
2194 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2195 if (dump_enabled_p ())
2196 dump_printf_loc (MSG_NOTE, vect_location,
2197 "Detected interleaving of size %d\n", (int)groupsize);
2199 /* SLP: create an SLP data structure for every interleaving group of
2200 stores for further analysis in vect_analyse_slp. */
2201 if (DR_IS_WRITE (dr) && !slp_impossible)
2203 if (loop_vinfo)
2204 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2205 if (bb_vinfo)
2206 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2209 /* There is a gap in the end of the group. */
2210 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2212 if (dump_enabled_p ())
2213 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2214 "Data access with gaps requires scalar "
2215 "epilogue loop\n");
2216 if (loop->inner)
2218 if (dump_enabled_p ())
2219 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2220 "Peeling for outer loop is not supported\n");
2221 return false;
2224 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2228 return true;
2232 /* Analyze the access pattern of the data-reference DR.
2233 In case of non-consecutive accesses call vect_analyze_group_access() to
2234 analyze groups of accesses. */
2236 static bool
2237 vect_analyze_data_ref_access (struct data_reference *dr)
2239 tree step = DR_STEP (dr);
2240 tree scalar_type = TREE_TYPE (DR_REF (dr));
2241 gimple stmt = DR_STMT (dr);
2242 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2243 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2244 struct loop *loop = NULL;
2246 if (loop_vinfo)
2247 loop = LOOP_VINFO_LOOP (loop_vinfo);
2249 if (loop_vinfo && !step)
2251 if (dump_enabled_p ())
2252 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2253 "bad data-ref access in loop\n");
2254 return false;
2257 /* Allow invariant loads in not nested loops. */
2258 if (loop_vinfo && integer_zerop (step))
2260 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2261 if (nested_in_vect_loop_p (loop, stmt))
2263 if (dump_enabled_p ())
2264 dump_printf_loc (MSG_NOTE, vect_location,
2265 "zero step in inner loop of nest\n");
2266 return false;
2268 return DR_IS_READ (dr);
2271 if (loop && nested_in_vect_loop_p (loop, stmt))
2273 /* Interleaved accesses are not yet supported within outer-loop
2274 vectorization for references in the inner-loop. */
2275 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2277 /* For the rest of the analysis we use the outer-loop step. */
2278 step = STMT_VINFO_DR_STEP (stmt_info);
2279 if (integer_zerop (step))
2281 if (dump_enabled_p ())
2282 dump_printf_loc (MSG_NOTE, vect_location,
2283 "zero step in outer loop.\n");
2284 if (DR_IS_READ (dr))
2285 return true;
2286 else
2287 return false;
2291 /* Consecutive? */
2292 if (TREE_CODE (step) == INTEGER_CST)
2294 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2295 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2296 || (dr_step < 0
2297 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2299 /* Mark that it is not interleaving. */
2300 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2301 return true;
2305 if (loop && nested_in_vect_loop_p (loop, stmt))
2307 if (dump_enabled_p ())
2308 dump_printf_loc (MSG_NOTE, vect_location,
2309 "grouped access in outer loop.\n");
2310 return false;
2313 /* Assume this is a DR handled by non-constant strided load case. */
2314 if (TREE_CODE (step) != INTEGER_CST)
2315 return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
2317 /* Not consecutive access - check if it's a part of interleaving group. */
2318 return vect_analyze_group_access (dr);
2323 /* A helper function used in the comparator function to sort data
2324 references. T1 and T2 are two data references to be compared.
2325 The function returns -1, 0, or 1. */
2327 static int
2328 compare_tree (tree t1, tree t2)
2330 int i, cmp;
2331 enum tree_code code;
2332 char tclass;
2334 if (t1 == t2)
2335 return 0;
2336 if (t1 == NULL)
2337 return -1;
2338 if (t2 == NULL)
2339 return 1;
2342 if (TREE_CODE (t1) != TREE_CODE (t2))
2343 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2345 code = TREE_CODE (t1);
2346 switch (code)
2348 /* For const values, we can just use hash values for comparisons. */
2349 case INTEGER_CST:
2350 case REAL_CST:
2351 case FIXED_CST:
2352 case STRING_CST:
2353 case COMPLEX_CST:
2354 case VECTOR_CST:
2356 hashval_t h1 = iterative_hash_expr (t1, 0);
2357 hashval_t h2 = iterative_hash_expr (t2, 0);
2358 if (h1 != h2)
2359 return h1 < h2 ? -1 : 1;
2360 break;
2363 case SSA_NAME:
2364 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2365 if (cmp != 0)
2366 return cmp;
2368 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2369 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2370 break;
2372 default:
2373 tclass = TREE_CODE_CLASS (code);
2375 /* For var-decl, we could compare their UIDs. */
2376 if (tclass == tcc_declaration)
2378 if (DECL_UID (t1) != DECL_UID (t2))
2379 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2380 break;
2383 /* For expressions with operands, compare their operands recursively. */
2384 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2386 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2387 if (cmp != 0)
2388 return cmp;
2392 return 0;
2396 /* Compare two data-references DRA and DRB to group them into chunks
2397 suitable for grouping. */
2399 static int
2400 dr_group_sort_cmp (const void *dra_, const void *drb_)
2402 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2403 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2404 int cmp;
2406 /* Stabilize sort. */
2407 if (dra == drb)
2408 return 0;
2410 /* Ordering of DRs according to base. */
2411 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2413 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2414 if (cmp != 0)
2415 return cmp;
2418 /* And according to DR_OFFSET. */
2419 if (!dr_equal_offsets_p (dra, drb))
2421 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2422 if (cmp != 0)
2423 return cmp;
2426 /* Put reads before writes. */
2427 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2428 return DR_IS_READ (dra) ? -1 : 1;
2430 /* Then sort after access size. */
2431 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2432 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2434 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2435 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2436 if (cmp != 0)
2437 return cmp;
2440 /* And after step. */
2441 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2443 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2444 if (cmp != 0)
2445 return cmp;
2448 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2449 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2450 if (cmp == 0)
2451 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2452 return cmp;
2455 /* Function vect_analyze_data_ref_accesses.
2457 Analyze the access pattern of all the data references in the loop.
2459 FORNOW: the only access pattern that is considered vectorizable is a
2460 simple step 1 (consecutive) access.
2462 FORNOW: handle only arrays and pointer accesses. */
2464 bool
2465 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2467 unsigned int i;
2468 vec<data_reference_p> datarefs;
2469 struct data_reference *dr;
2471 if (dump_enabled_p ())
2472 dump_printf_loc (MSG_NOTE, vect_location,
2473 "=== vect_analyze_data_ref_accesses ===\n");
2475 if (loop_vinfo)
2476 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2477 else
2478 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2480 if (datarefs.is_empty ())
2481 return true;
2483 /* Sort the array of datarefs to make building the interleaving chains
2484 linear. */
2485 qsort (datarefs.address (), datarefs.length (),
2486 sizeof (data_reference_p), dr_group_sort_cmp);
2488 /* Build the interleaving chains. */
2489 for (i = 0; i < datarefs.length () - 1;)
2491 data_reference_p dra = datarefs[i];
2492 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2493 stmt_vec_info lastinfo = NULL;
2494 for (i = i + 1; i < datarefs.length (); ++i)
2496 data_reference_p drb = datarefs[i];
2497 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2499 /* ??? Imperfect sorting (non-compatible types, non-modulo
2500 accesses, same accesses) can lead to a group to be artificially
2501 split here as we don't just skip over those. If it really
2502 matters we can push those to a worklist and re-iterate
2503 over them. The we can just skip ahead to the next DR here. */
2505 /* Check that the data-refs have same first location (except init)
2506 and they are both either store or load (not load and store). */
2507 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2508 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2509 DR_BASE_ADDRESS (drb), 0)
2510 || !dr_equal_offsets_p (dra, drb))
2511 break;
2513 /* Check that the data-refs have the same constant size and step. */
2514 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2515 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2516 if (!tree_fits_uhwi_p (sza)
2517 || !tree_fits_uhwi_p (szb)
2518 || !tree_int_cst_equal (sza, szb)
2519 || !tree_fits_shwi_p (DR_STEP (dra))
2520 || !tree_fits_shwi_p (DR_STEP (drb))
2521 || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
2522 break;
2524 /* Do not place the same access in the interleaving chain twice. */
2525 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2526 break;
2528 /* Check the types are compatible.
2529 ??? We don't distinguish this during sorting. */
2530 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2531 TREE_TYPE (DR_REF (drb))))
2532 break;
2534 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2535 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2536 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2537 gcc_assert (init_a < init_b);
2539 /* If init_b == init_a + the size of the type * k, we have an
2540 interleaving, and DRA is accessed before DRB. */
2541 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2542 if ((init_b - init_a) % type_size_a != 0)
2543 break;
2545 /* The step (if not zero) is greater than the difference between
2546 data-refs' inits. This splits groups into suitable sizes. */
2547 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2548 if (step != 0 && step <= (init_b - init_a))
2549 break;
2551 if (dump_enabled_p ())
2553 dump_printf_loc (MSG_NOTE, vect_location,
2554 "Detected interleaving ");
2555 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2556 dump_printf (MSG_NOTE, " and ");
2557 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2558 dump_printf (MSG_NOTE, "\n");
2561 /* Link the found element into the group list. */
2562 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2564 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2565 lastinfo = stmtinfo_a;
2567 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2568 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2569 lastinfo = stmtinfo_b;
2573 FOR_EACH_VEC_ELT (datarefs, i, dr)
2574 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2575 && !vect_analyze_data_ref_access (dr))
2577 if (dump_enabled_p ())
2578 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2579 "not vectorized: complicated access pattern.\n");
2581 if (bb_vinfo)
2583 /* Mark the statement as not vectorizable. */
2584 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2585 continue;
2587 else
2588 return false;
2591 return true;
2595 /* Operator == between two dr_with_seg_len objects.
2597 This equality operator is used to make sure two data refs
2598 are the same one so that we will consider to combine the
2599 aliasing checks of those two pairs of data dependent data
2600 refs. */
2602 static bool
2603 operator == (const dr_with_seg_len& d1,
2604 const dr_with_seg_len& d2)
2606 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2607 DR_BASE_ADDRESS (d2.dr), 0)
2608 && compare_tree (d1.offset, d2.offset) == 0
2609 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2612 /* Function comp_dr_with_seg_len_pair.
2614 Comparison function for sorting objects of dr_with_seg_len_pair_t
2615 so that we can combine aliasing checks in one scan. */
2617 static int
2618 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2620 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2621 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2623 const dr_with_seg_len &p11 = p1->first,
2624 &p12 = p1->second,
2625 &p21 = p2->first,
2626 &p22 = p2->second;
2628 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2629 if a and c have the same basic address snd step, and b and d have the same
2630 address and step. Therefore, if any a&c or b&d don't have the same address
2631 and step, we don't care the order of those two pairs after sorting. */
2632 int comp_res;
2634 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2635 DR_BASE_ADDRESS (p21.dr))) != 0)
2636 return comp_res;
2637 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2638 DR_BASE_ADDRESS (p22.dr))) != 0)
2639 return comp_res;
2640 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2641 return comp_res;
2642 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2643 return comp_res;
2644 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2645 return comp_res;
2646 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2647 return comp_res;
2649 return 0;
2652 template <class T> static void
2653 swap (T& a, T& b)
2655 T c (a);
2656 a = b;
2657 b = c;
2660 /* Function vect_vfa_segment_size.
2662 Create an expression that computes the size of segment
2663 that will be accessed for a data reference. The functions takes into
2664 account that realignment loads may access one more vector.
2666 Input:
2667 DR: The data reference.
2668 LENGTH_FACTOR: segment length to consider.
2670 Return an expression whose value is the size of segment which will be
2671 accessed by DR. */
2673 static tree
2674 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2676 tree segment_length;
2678 if (integer_zerop (DR_STEP (dr)))
2679 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2680 else
2681 segment_length = size_binop (MULT_EXPR,
2682 fold_convert (sizetype, DR_STEP (dr)),
2683 fold_convert (sizetype, length_factor));
2685 if (vect_supportable_dr_alignment (dr, false)
2686 == dr_explicit_realign_optimized)
2688 tree vector_size = TYPE_SIZE_UNIT
2689 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2691 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2693 return segment_length;
2696 /* Function vect_prune_runtime_alias_test_list.
2698 Prune a list of ddrs to be tested at run-time by versioning for alias.
2699 Merge several alias checks into one if possible.
2700 Return FALSE if resulting list of ddrs is longer then allowed by
2701 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2703 bool
2704 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2706 vec<ddr_p> may_alias_ddrs =
2707 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2708 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2709 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2710 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2711 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2713 ddr_p ddr;
2714 unsigned int i;
2715 tree length_factor;
2717 if (dump_enabled_p ())
2718 dump_printf_loc (MSG_NOTE, vect_location,
2719 "=== vect_prune_runtime_alias_test_list ===\n");
2721 if (may_alias_ddrs.is_empty ())
2722 return true;
2724 /* Basically, for each pair of dependent data refs store_ptr_0
2725 and load_ptr_0, we create an expression:
2727 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2728 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2730 for aliasing checks. However, in some cases we can decrease
2731 the number of checks by combining two checks into one. For
2732 example, suppose we have another pair of data refs store_ptr_0
2733 and load_ptr_1, and if the following condition is satisfied:
2735 load_ptr_0 < load_ptr_1 &&
2736 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2738 (this condition means, in each iteration of vectorized loop,
2739 the accessed memory of store_ptr_0 cannot be between the memory
2740 of load_ptr_0 and load_ptr_1.)
2742 we then can use only the following expression to finish the
2743 alising checks between store_ptr_0 & load_ptr_0 and
2744 store_ptr_0 & load_ptr_1:
2746 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2747 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2749 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2750 same basic address. */
2752 comp_alias_ddrs.create (may_alias_ddrs.length ());
2754 /* First, we collect all data ref pairs for aliasing checks. */
2755 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2757 struct data_reference *dr_a, *dr_b;
2758 gimple dr_group_first_a, dr_group_first_b;
2759 tree segment_length_a, segment_length_b;
2760 gimple stmt_a, stmt_b;
2762 dr_a = DDR_A (ddr);
2763 stmt_a = DR_STMT (DDR_A (ddr));
2764 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2765 if (dr_group_first_a)
2767 stmt_a = dr_group_first_a;
2768 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2771 dr_b = DDR_B (ddr);
2772 stmt_b = DR_STMT (DDR_B (ddr));
2773 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2774 if (dr_group_first_b)
2776 stmt_b = dr_group_first_b;
2777 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2780 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2781 length_factor = scalar_loop_iters;
2782 else
2783 length_factor = size_int (vect_factor);
2784 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2785 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2787 dr_with_seg_len_pair_t dr_with_seg_len_pair
2788 (dr_with_seg_len (dr_a, segment_length_a),
2789 dr_with_seg_len (dr_b, segment_length_b));
2791 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2792 swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2794 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2797 /* Second, we sort the collected data ref pairs so that we can scan
2798 them once to combine all possible aliasing checks. */
2799 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2801 /* Third, we scan the sorted dr pairs and check if we can combine
2802 alias checks of two neighbouring dr pairs. */
2803 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2805 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2806 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2807 *dr_b1 = &comp_alias_ddrs[i-1].second,
2808 *dr_a2 = &comp_alias_ddrs[i].first,
2809 *dr_b2 = &comp_alias_ddrs[i].second;
2811 /* Remove duplicate data ref pairs. */
2812 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2814 if (dump_enabled_p ())
2816 dump_printf_loc (MSG_NOTE, vect_location,
2817 "found equal ranges ");
2818 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2819 DR_REF (dr_a1->dr));
2820 dump_printf (MSG_NOTE, ", ");
2821 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2822 DR_REF (dr_b1->dr));
2823 dump_printf (MSG_NOTE, " and ");
2824 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2825 DR_REF (dr_a2->dr));
2826 dump_printf (MSG_NOTE, ", ");
2827 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2828 DR_REF (dr_b2->dr));
2829 dump_printf (MSG_NOTE, "\n");
2832 comp_alias_ddrs.ordered_remove (i--);
2833 continue;
2836 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2838 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2839 and DR_A1 and DR_A2 are two consecutive memrefs. */
2840 if (*dr_a1 == *dr_a2)
2842 swap (dr_a1, dr_b1);
2843 swap (dr_a2, dr_b2);
2846 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2847 DR_BASE_ADDRESS (dr_a2->dr),
2849 || !tree_fits_shwi_p (dr_a1->offset)
2850 || !tree_fits_shwi_p (dr_a2->offset))
2851 continue;
2853 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2854 - tree_to_shwi (dr_a1->offset));
2857 /* Now we check if the following condition is satisfied:
2859 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2861 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2862 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2863 have to make a best estimation. We can get the minimum value
2864 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2865 then either of the following two conditions can guarantee the
2866 one above:
2868 1: DIFF <= MIN_SEG_LEN_B
2869 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2873 HOST_WIDE_INT
2874 min_seg_len_b = (TREE_CODE (dr_b1->seg_len) == INTEGER_CST) ?
2875 TREE_INT_CST_LOW (dr_b1->seg_len) :
2876 vect_factor;
2878 if (diff <= min_seg_len_b
2879 || (TREE_CODE (dr_a1->seg_len) == INTEGER_CST
2880 && diff - (HOST_WIDE_INT) TREE_INT_CST_LOW (dr_a1->seg_len) <
2881 min_seg_len_b))
2883 dr_a1->seg_len = size_binop (PLUS_EXPR,
2884 dr_a2->seg_len, size_int (diff));
2885 comp_alias_ddrs.ordered_remove (i--);
2890 if ((int) comp_alias_ddrs.length () >
2891 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2893 if (dump_enabled_p ())
2895 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2896 "disable versioning for alias - max number of "
2897 "generated checks exceeded.\n");
2900 return false;
2903 return true;
2906 /* Check whether a non-affine read in stmt is suitable for gather load
2907 and if so, return a builtin decl for that operation. */
2909 tree
2910 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2911 tree *offp, int *scalep)
2913 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2914 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2915 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2916 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2917 tree offtype = NULL_TREE;
2918 tree decl, base, off;
2919 enum machine_mode pmode;
2920 int punsignedp, pvolatilep;
2922 /* The gather builtins need address of the form
2923 loop_invariant + vector * {1, 2, 4, 8}
2925 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
2926 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
2927 of loop invariants/SSA_NAMEs defined in the loop, with casts,
2928 multiplications and additions in it. To get a vector, we need
2929 a single SSA_NAME that will be defined in the loop and will
2930 contain everything that is not loop invariant and that can be
2931 vectorized. The following code attempts to find such a preexistng
2932 SSA_NAME OFF and put the loop invariants into a tree BASE
2933 that can be gimplified before the loop. */
2934 base = get_inner_reference (DR_REF (dr), &pbitsize, &pbitpos, &off,
2935 &pmode, &punsignedp, &pvolatilep, false);
2936 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
2938 if (TREE_CODE (base) == MEM_REF)
2940 if (!integer_zerop (TREE_OPERAND (base, 1)))
2942 if (off == NULL_TREE)
2944 double_int moff = mem_ref_offset (base);
2945 off = double_int_to_tree (sizetype, moff);
2947 else
2948 off = size_binop (PLUS_EXPR, off,
2949 fold_convert (sizetype, TREE_OPERAND (base, 1)));
2951 base = TREE_OPERAND (base, 0);
2953 else
2954 base = build_fold_addr_expr (base);
2956 if (off == NULL_TREE)
2957 off = size_zero_node;
2959 /* If base is not loop invariant, either off is 0, then we start with just
2960 the constant offset in the loop invariant BASE and continue with base
2961 as OFF, otherwise give up.
2962 We could handle that case by gimplifying the addition of base + off
2963 into some SSA_NAME and use that as off, but for now punt. */
2964 if (!expr_invariant_in_loop_p (loop, base))
2966 if (!integer_zerop (off))
2967 return NULL_TREE;
2968 off = base;
2969 base = size_int (pbitpos / BITS_PER_UNIT);
2971 /* Otherwise put base + constant offset into the loop invariant BASE
2972 and continue with OFF. */
2973 else
2975 base = fold_convert (sizetype, base);
2976 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
2979 /* OFF at this point may be either a SSA_NAME or some tree expression
2980 from get_inner_reference. Try to peel off loop invariants from it
2981 into BASE as long as possible. */
2982 STRIP_NOPS (off);
2983 while (offtype == NULL_TREE)
2985 enum tree_code code;
2986 tree op0, op1, add = NULL_TREE;
2988 if (TREE_CODE (off) == SSA_NAME)
2990 gimple def_stmt = SSA_NAME_DEF_STMT (off);
2992 if (expr_invariant_in_loop_p (loop, off))
2993 return NULL_TREE;
2995 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
2996 break;
2998 op0 = gimple_assign_rhs1 (def_stmt);
2999 code = gimple_assign_rhs_code (def_stmt);
3000 op1 = gimple_assign_rhs2 (def_stmt);
3002 else
3004 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3005 return NULL_TREE;
3006 code = TREE_CODE (off);
3007 extract_ops_from_tree (off, &code, &op0, &op1);
3009 switch (code)
3011 case POINTER_PLUS_EXPR:
3012 case PLUS_EXPR:
3013 if (expr_invariant_in_loop_p (loop, op0))
3015 add = op0;
3016 off = op1;
3017 do_add:
3018 add = fold_convert (sizetype, add);
3019 if (scale != 1)
3020 add = size_binop (MULT_EXPR, add, size_int (scale));
3021 base = size_binop (PLUS_EXPR, base, add);
3022 continue;
3024 if (expr_invariant_in_loop_p (loop, op1))
3026 add = op1;
3027 off = op0;
3028 goto do_add;
3030 break;
3031 case MINUS_EXPR:
3032 if (expr_invariant_in_loop_p (loop, op1))
3034 add = fold_convert (sizetype, op1);
3035 add = size_binop (MINUS_EXPR, size_zero_node, add);
3036 off = op0;
3037 goto do_add;
3039 break;
3040 case MULT_EXPR:
3041 if (scale == 1 && tree_fits_shwi_p (op1))
3043 scale = tree_to_shwi (op1);
3044 off = op0;
3045 continue;
3047 break;
3048 case SSA_NAME:
3049 off = op0;
3050 continue;
3051 CASE_CONVERT:
3052 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3053 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3054 break;
3055 if (TYPE_PRECISION (TREE_TYPE (op0))
3056 == TYPE_PRECISION (TREE_TYPE (off)))
3058 off = op0;
3059 continue;
3061 if (TYPE_PRECISION (TREE_TYPE (op0))
3062 < TYPE_PRECISION (TREE_TYPE (off)))
3064 off = op0;
3065 offtype = TREE_TYPE (off);
3066 STRIP_NOPS (off);
3067 continue;
3069 break;
3070 default:
3071 break;
3073 break;
3076 /* If at the end OFF still isn't a SSA_NAME or isn't
3077 defined in the loop, punt. */
3078 if (TREE_CODE (off) != SSA_NAME
3079 || expr_invariant_in_loop_p (loop, off))
3080 return NULL_TREE;
3082 if (offtype == NULL_TREE)
3083 offtype = TREE_TYPE (off);
3085 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3086 offtype, scale);
3087 if (decl == NULL_TREE)
3088 return NULL_TREE;
3090 if (basep)
3091 *basep = base;
3092 if (offp)
3093 *offp = off;
3094 if (scalep)
3095 *scalep = scale;
3096 return decl;
3099 /* Function vect_analyze_data_refs.
3101 Find all the data references in the loop or basic block.
3103 The general structure of the analysis of data refs in the vectorizer is as
3104 follows:
3105 1- vect_analyze_data_refs(loop/bb): call
3106 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3107 in the loop/bb and their dependences.
3108 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3109 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3110 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3114 bool
3115 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3116 bb_vec_info bb_vinfo,
3117 int *min_vf)
3119 struct loop *loop = NULL;
3120 basic_block bb = NULL;
3121 unsigned int i;
3122 vec<data_reference_p> datarefs;
3123 struct data_reference *dr;
3124 tree scalar_type;
3126 if (dump_enabled_p ())
3127 dump_printf_loc (MSG_NOTE, vect_location,
3128 "=== vect_analyze_data_refs ===\n");
3130 if (loop_vinfo)
3132 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3134 loop = LOOP_VINFO_LOOP (loop_vinfo);
3135 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3136 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3138 if (dump_enabled_p ())
3139 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3140 "not vectorized: loop contains function calls"
3141 " or data references that cannot be analyzed\n");
3142 return false;
3145 for (i = 0; i < loop->num_nodes; i++)
3147 gimple_stmt_iterator gsi;
3149 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3151 gimple stmt = gsi_stmt (gsi);
3152 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3154 if (is_gimple_call (stmt) && loop->safelen)
3156 tree fndecl = gimple_call_fndecl (stmt), op;
3157 if (fndecl != NULL_TREE)
3159 struct cgraph_node *node = cgraph_get_node (fndecl);
3160 if (node != NULL && node->simd_clones != NULL)
3162 unsigned int j, n = gimple_call_num_args (stmt);
3163 for (j = 0; j < n; j++)
3165 op = gimple_call_arg (stmt, j);
3166 if (DECL_P (op)
3167 || (REFERENCE_CLASS_P (op)
3168 && get_base_address (op)))
3169 break;
3171 op = gimple_call_lhs (stmt);
3172 /* Ignore #pragma omp declare simd functions
3173 if they don't have data references in the
3174 call stmt itself. */
3175 if (j == n
3176 && !(op
3177 && (DECL_P (op)
3178 || (REFERENCE_CLASS_P (op)
3179 && get_base_address (op)))))
3180 continue;
3184 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3185 if (dump_enabled_p ())
3186 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3187 "not vectorized: loop contains function "
3188 "calls or data references that cannot "
3189 "be analyzed\n");
3190 return false;
3195 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3197 else
3199 gimple_stmt_iterator gsi;
3201 bb = BB_VINFO_BB (bb_vinfo);
3202 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3204 gimple stmt = gsi_stmt (gsi);
3205 if (!find_data_references_in_stmt (NULL, stmt,
3206 &BB_VINFO_DATAREFS (bb_vinfo)))
3208 /* Mark the rest of the basic-block as unvectorizable. */
3209 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3211 stmt = gsi_stmt (gsi);
3212 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3214 break;
3218 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3221 /* Go through the data-refs, check that the analysis succeeded. Update
3222 pointer from stmt_vec_info struct to DR and vectype. */
3224 FOR_EACH_VEC_ELT (datarefs, i, dr)
3226 gimple stmt;
3227 stmt_vec_info stmt_info;
3228 tree base, offset, init;
3229 bool gather = false;
3230 bool simd_lane_access = false;
3231 int vf;
3233 again:
3234 if (!dr || !DR_REF (dr))
3236 if (dump_enabled_p ())
3237 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3238 "not vectorized: unhandled data-ref\n");
3239 return false;
3242 stmt = DR_STMT (dr);
3243 stmt_info = vinfo_for_stmt (stmt);
3245 /* Discard clobbers from the dataref vector. We will remove
3246 clobber stmts during vectorization. */
3247 if (gimple_clobber_p (stmt))
3249 if (i == datarefs.length () - 1)
3251 datarefs.pop ();
3252 break;
3254 datarefs[i] = datarefs.pop ();
3255 goto again;
3258 /* Check that analysis of the data-ref succeeded. */
3259 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3260 || !DR_STEP (dr))
3262 bool maybe_gather
3263 = DR_IS_READ (dr)
3264 && !TREE_THIS_VOLATILE (DR_REF (dr))
3265 && targetm.vectorize.builtin_gather != NULL;
3266 bool maybe_simd_lane_access
3267 = loop_vinfo && loop->simduid;
3269 /* If target supports vector gather loads, or if this might be
3270 a SIMD lane access, see if they can't be used. */
3271 if (loop_vinfo
3272 && (maybe_gather || maybe_simd_lane_access)
3273 && !nested_in_vect_loop_p (loop, stmt))
3275 struct data_reference *newdr
3276 = create_data_ref (NULL, loop_containing_stmt (stmt),
3277 DR_REF (dr), stmt, true);
3278 gcc_assert (newdr != NULL && DR_REF (newdr));
3279 if (DR_BASE_ADDRESS (newdr)
3280 && DR_OFFSET (newdr)
3281 && DR_INIT (newdr)
3282 && DR_STEP (newdr)
3283 && integer_zerop (DR_STEP (newdr)))
3285 if (maybe_simd_lane_access)
3287 tree off = DR_OFFSET (newdr);
3288 STRIP_NOPS (off);
3289 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3290 && TREE_CODE (off) == MULT_EXPR
3291 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3293 tree step = TREE_OPERAND (off, 1);
3294 off = TREE_OPERAND (off, 0);
3295 STRIP_NOPS (off);
3296 if (CONVERT_EXPR_P (off)
3297 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3298 0)))
3299 < TYPE_PRECISION (TREE_TYPE (off)))
3300 off = TREE_OPERAND (off, 0);
3301 if (TREE_CODE (off) == SSA_NAME)
3303 gimple def = SSA_NAME_DEF_STMT (off);
3304 tree reft = TREE_TYPE (DR_REF (newdr));
3305 if (gimple_call_internal_p (def)
3306 && gimple_call_internal_fn (def)
3307 == IFN_GOMP_SIMD_LANE)
3309 tree arg = gimple_call_arg (def, 0);
3310 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3311 arg = SSA_NAME_VAR (arg);
3312 if (arg == loop->simduid
3313 /* For now. */
3314 && tree_int_cst_equal
3315 (TYPE_SIZE_UNIT (reft),
3316 step))
3318 DR_OFFSET (newdr) = ssize_int (0);
3319 DR_STEP (newdr) = step;
3320 DR_ALIGNED_TO (newdr)
3321 = size_int (BIGGEST_ALIGNMENT);
3322 dr = newdr;
3323 simd_lane_access = true;
3329 if (!simd_lane_access && maybe_gather)
3331 dr = newdr;
3332 gather = true;
3335 if (!gather && !simd_lane_access)
3336 free_data_ref (newdr);
3339 if (!gather && !simd_lane_access)
3341 if (dump_enabled_p ())
3343 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3344 "not vectorized: data ref analysis "
3345 "failed ");
3346 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3347 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3350 if (bb_vinfo)
3351 break;
3353 return false;
3357 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3359 if (dump_enabled_p ())
3360 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3361 "not vectorized: base addr of dr is a "
3362 "constant\n");
3364 if (bb_vinfo)
3365 break;
3367 if (gather || simd_lane_access)
3368 free_data_ref (dr);
3369 return false;
3372 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3374 if (dump_enabled_p ())
3376 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3377 "not vectorized: volatile type ");
3378 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3379 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3382 if (bb_vinfo)
3383 break;
3385 return false;
3388 if (stmt_can_throw_internal (stmt))
3390 if (dump_enabled_p ())
3392 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3393 "not vectorized: statement can throw an "
3394 "exception ");
3395 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3396 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3399 if (bb_vinfo)
3400 break;
3402 if (gather || simd_lane_access)
3403 free_data_ref (dr);
3404 return false;
3407 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3408 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3410 if (dump_enabled_p ())
3412 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3413 "not vectorized: statement is bitfield "
3414 "access ");
3415 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3416 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3419 if (bb_vinfo)
3420 break;
3422 if (gather || simd_lane_access)
3423 free_data_ref (dr);
3424 return false;
3427 base = unshare_expr (DR_BASE_ADDRESS (dr));
3428 offset = unshare_expr (DR_OFFSET (dr));
3429 init = unshare_expr (DR_INIT (dr));
3431 if (is_gimple_call (stmt))
3433 if (dump_enabled_p ())
3435 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3436 "not vectorized: dr in a call ");
3437 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3438 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3441 if (bb_vinfo)
3442 break;
3444 if (gather || simd_lane_access)
3445 free_data_ref (dr);
3446 return false;
3449 /* Update DR field in stmt_vec_info struct. */
3451 /* If the dataref is in an inner-loop of the loop that is considered for
3452 for vectorization, we also want to analyze the access relative to
3453 the outer-loop (DR contains information only relative to the
3454 inner-most enclosing loop). We do that by building a reference to the
3455 first location accessed by the inner-loop, and analyze it relative to
3456 the outer-loop. */
3457 if (loop && nested_in_vect_loop_p (loop, stmt))
3459 tree outer_step, outer_base, outer_init;
3460 HOST_WIDE_INT pbitsize, pbitpos;
3461 tree poffset;
3462 enum machine_mode pmode;
3463 int punsignedp, pvolatilep;
3464 affine_iv base_iv, offset_iv;
3465 tree dinit;
3467 /* Build a reference to the first location accessed by the
3468 inner-loop: *(BASE+INIT). (The first location is actually
3469 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3470 tree inner_base = build_fold_indirect_ref
3471 (fold_build_pointer_plus (base, init));
3473 if (dump_enabled_p ())
3475 dump_printf_loc (MSG_NOTE, vect_location,
3476 "analyze in outer-loop: ");
3477 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3478 dump_printf (MSG_NOTE, "\n");
3481 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3482 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3483 gcc_assert (outer_base != NULL_TREE);
3485 if (pbitpos % BITS_PER_UNIT != 0)
3487 if (dump_enabled_p ())
3488 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3489 "failed: bit offset alignment.\n");
3490 return false;
3493 outer_base = build_fold_addr_expr (outer_base);
3494 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3495 &base_iv, false))
3497 if (dump_enabled_p ())
3498 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3499 "failed: evolution of base is not affine.\n");
3500 return false;
3503 if (offset)
3505 if (poffset)
3506 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3507 poffset);
3508 else
3509 poffset = offset;
3512 if (!poffset)
3514 offset_iv.base = ssize_int (0);
3515 offset_iv.step = ssize_int (0);
3517 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3518 &offset_iv, false))
3520 if (dump_enabled_p ())
3521 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3522 "evolution of offset is not affine.\n");
3523 return false;
3526 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3527 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3528 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3529 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3530 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3532 outer_step = size_binop (PLUS_EXPR,
3533 fold_convert (ssizetype, base_iv.step),
3534 fold_convert (ssizetype, offset_iv.step));
3536 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3537 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3538 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3539 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3540 STMT_VINFO_DR_OFFSET (stmt_info) =
3541 fold_convert (ssizetype, offset_iv.base);
3542 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3543 size_int (highest_pow2_factor (offset_iv.base));
3545 if (dump_enabled_p ())
3547 dump_printf_loc (MSG_NOTE, vect_location,
3548 "\touter base_address: ");
3549 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3550 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3551 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3552 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3553 STMT_VINFO_DR_OFFSET (stmt_info));
3554 dump_printf (MSG_NOTE,
3555 "\n\touter constant offset from base address: ");
3556 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3557 STMT_VINFO_DR_INIT (stmt_info));
3558 dump_printf (MSG_NOTE, "\n\touter step: ");
3559 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3560 STMT_VINFO_DR_STEP (stmt_info));
3561 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3562 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3563 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3564 dump_printf (MSG_NOTE, "\n");
3568 if (STMT_VINFO_DATA_REF (stmt_info))
3570 if (dump_enabled_p ())
3572 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3573 "not vectorized: more than one data ref "
3574 "in stmt: ");
3575 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3576 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3579 if (bb_vinfo)
3580 break;
3582 if (gather || simd_lane_access)
3583 free_data_ref (dr);
3584 return false;
3587 STMT_VINFO_DATA_REF (stmt_info) = dr;
3588 if (simd_lane_access)
3590 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3591 datarefs[i] = dr;
3594 /* Set vectype for STMT. */
3595 scalar_type = TREE_TYPE (DR_REF (dr));
3596 STMT_VINFO_VECTYPE (stmt_info) =
3597 get_vectype_for_scalar_type (scalar_type);
3598 if (!STMT_VINFO_VECTYPE (stmt_info))
3600 if (dump_enabled_p ())
3602 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3603 "not vectorized: no vectype for stmt: ");
3604 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3605 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3606 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3607 scalar_type);
3608 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3611 if (bb_vinfo)
3612 break;
3614 if (gather || simd_lane_access)
3616 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3617 free_data_ref (dr);
3619 return false;
3621 else
3623 if (dump_enabled_p ())
3625 dump_printf_loc (MSG_NOTE, vect_location,
3626 "got vectype for stmt: ");
3627 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3628 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3629 STMT_VINFO_VECTYPE (stmt_info));
3630 dump_printf (MSG_NOTE, "\n");
3634 /* Adjust the minimal vectorization factor according to the
3635 vector type. */
3636 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3637 if (vf > *min_vf)
3638 *min_vf = vf;
3640 if (gather)
3642 tree off;
3644 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3645 if (gather
3646 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3647 gather = false;
3648 if (!gather)
3650 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3651 free_data_ref (dr);
3652 if (dump_enabled_p ())
3654 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3655 "not vectorized: not suitable for gather "
3656 "load ");
3657 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3658 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3660 return false;
3663 datarefs[i] = dr;
3664 STMT_VINFO_GATHER_P (stmt_info) = true;
3666 else if (loop_vinfo
3667 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3669 if (nested_in_vect_loop_p (loop, stmt)
3670 || !DR_IS_READ (dr))
3672 if (dump_enabled_p ())
3674 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3675 "not vectorized: not suitable for strided "
3676 "load ");
3677 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3678 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3680 return false;
3682 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3686 /* If we stopped analysis at the first dataref we could not analyze
3687 when trying to vectorize a basic-block mark the rest of the datarefs
3688 as not vectorizable and truncate the vector of datarefs. That
3689 avoids spending useless time in analyzing their dependence. */
3690 if (i != datarefs.length ())
3692 gcc_assert (bb_vinfo != NULL);
3693 for (unsigned j = i; j < datarefs.length (); ++j)
3695 data_reference_p dr = datarefs[j];
3696 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3697 free_data_ref (dr);
3699 datarefs.truncate (i);
3702 return true;
3706 /* Function vect_get_new_vect_var.
3708 Returns a name for a new variable. The current naming scheme appends the
3709 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3710 the name of vectorizer generated variables, and appends that to NAME if
3711 provided. */
3713 tree
3714 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3716 const char *prefix;
3717 tree new_vect_var;
3719 switch (var_kind)
3721 case vect_simple_var:
3722 prefix = "vect";
3723 break;
3724 case vect_scalar_var:
3725 prefix = "stmp";
3726 break;
3727 case vect_pointer_var:
3728 prefix = "vectp";
3729 break;
3730 default:
3731 gcc_unreachable ();
3734 if (name)
3736 char* tmp = concat (prefix, "_", name, NULL);
3737 new_vect_var = create_tmp_reg (type, tmp);
3738 free (tmp);
3740 else
3741 new_vect_var = create_tmp_reg (type, prefix);
3743 return new_vect_var;
3747 /* Function vect_create_addr_base_for_vector_ref.
3749 Create an expression that computes the address of the first memory location
3750 that will be accessed for a data reference.
3752 Input:
3753 STMT: The statement containing the data reference.
3754 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3755 OFFSET: Optional. If supplied, it is be added to the initial address.
3756 LOOP: Specify relative to which loop-nest should the address be computed.
3757 For example, when the dataref is in an inner-loop nested in an
3758 outer-loop that is now being vectorized, LOOP can be either the
3759 outer-loop, or the inner-loop. The first memory location accessed
3760 by the following dataref ('in' points to short):
3762 for (i=0; i<N; i++)
3763 for (j=0; j<M; j++)
3764 s += in[i+j]
3766 is as follows:
3767 if LOOP=i_loop: &in (relative to i_loop)
3768 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3770 Output:
3771 1. Return an SSA_NAME whose value is the address of the memory location of
3772 the first vector of the data reference.
3773 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3774 these statement(s) which define the returned SSA_NAME.
3776 FORNOW: We are only handling array accesses with step 1. */
3778 tree
3779 vect_create_addr_base_for_vector_ref (gimple stmt,
3780 gimple_seq *new_stmt_list,
3781 tree offset,
3782 struct loop *loop)
3784 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3785 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3786 tree data_ref_base;
3787 const char *base_name;
3788 tree addr_base;
3789 tree dest;
3790 gimple_seq seq = NULL;
3791 tree base_offset;
3792 tree init;
3793 tree vect_ptr_type;
3794 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3795 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3797 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3799 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3801 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3803 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3804 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3805 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3807 else
3809 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3810 base_offset = unshare_expr (DR_OFFSET (dr));
3811 init = unshare_expr (DR_INIT (dr));
3814 if (loop_vinfo)
3815 base_name = get_name (data_ref_base);
3816 else
3818 base_offset = ssize_int (0);
3819 init = ssize_int (0);
3820 base_name = get_name (DR_REF (dr));
3823 /* Create base_offset */
3824 base_offset = size_binop (PLUS_EXPR,
3825 fold_convert (sizetype, base_offset),
3826 fold_convert (sizetype, init));
3828 if (offset)
3830 offset = fold_build2 (MULT_EXPR, sizetype,
3831 fold_convert (sizetype, offset), step);
3832 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3833 base_offset, offset);
3836 /* base + base_offset */
3837 if (loop_vinfo)
3838 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3839 else
3841 addr_base = build1 (ADDR_EXPR,
3842 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3843 unshare_expr (DR_REF (dr)));
3846 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3847 addr_base = fold_convert (vect_ptr_type, addr_base);
3848 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3849 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3850 gimple_seq_add_seq (new_stmt_list, seq);
3852 if (DR_PTR_INFO (dr)
3853 && TREE_CODE (addr_base) == SSA_NAME)
3855 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3856 if (offset)
3857 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3860 if (dump_enabled_p ())
3862 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3863 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3864 dump_printf (MSG_NOTE, "\n");
3867 return addr_base;
3871 /* Function vect_create_data_ref_ptr.
3873 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3874 location accessed in the loop by STMT, along with the def-use update
3875 chain to appropriately advance the pointer through the loop iterations.
3876 Also set aliasing information for the pointer. This pointer is used by
3877 the callers to this function to create a memory reference expression for
3878 vector load/store access.
3880 Input:
3881 1. STMT: a stmt that references memory. Expected to be of the form
3882 GIMPLE_ASSIGN <name, data-ref> or
3883 GIMPLE_ASSIGN <data-ref, name>.
3884 2. AGGR_TYPE: the type of the reference, which should be either a vector
3885 or an array.
3886 3. AT_LOOP: the loop where the vector memref is to be created.
3887 4. OFFSET (optional): an offset to be added to the initial address accessed
3888 by the data-ref in STMT.
3889 5. BSI: location where the new stmts are to be placed if there is no loop
3890 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
3891 pointing to the initial address.
3893 Output:
3894 1. Declare a new ptr to vector_type, and have it point to the base of the
3895 data reference (initial addressed accessed by the data reference).
3896 For example, for vector of type V8HI, the following code is generated:
3898 v8hi *ap;
3899 ap = (v8hi *)initial_address;
3901 if OFFSET is not supplied:
3902 initial_address = &a[init];
3903 if OFFSET is supplied:
3904 initial_address = &a[init + OFFSET];
3906 Return the initial_address in INITIAL_ADDRESS.
3908 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
3909 update the pointer in each iteration of the loop.
3911 Return the increment stmt that updates the pointer in PTR_INCR.
3913 3. Set INV_P to true if the access pattern of the data reference in the
3914 vectorized loop is invariant. Set it to false otherwise.
3916 4. Return the pointer. */
3918 tree
3919 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
3920 tree offset, tree *initial_address,
3921 gimple_stmt_iterator *gsi, gimple *ptr_incr,
3922 bool only_init, bool *inv_p)
3924 const char *base_name;
3925 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3926 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3927 struct loop *loop = NULL;
3928 bool nested_in_vect_loop = false;
3929 struct loop *containing_loop = NULL;
3930 tree aggr_ptr_type;
3931 tree aggr_ptr;
3932 tree new_temp;
3933 gimple vec_stmt;
3934 gimple_seq new_stmt_list = NULL;
3935 edge pe = NULL;
3936 basic_block new_bb;
3937 tree aggr_ptr_init;
3938 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3939 tree aptr;
3940 gimple_stmt_iterator incr_gsi;
3941 bool insert_after;
3942 tree indx_before_incr, indx_after_incr;
3943 gimple incr;
3944 tree step;
3945 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3947 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
3948 || TREE_CODE (aggr_type) == VECTOR_TYPE);
3950 if (loop_vinfo)
3952 loop = LOOP_VINFO_LOOP (loop_vinfo);
3953 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
3954 containing_loop = (gimple_bb (stmt))->loop_father;
3955 pe = loop_preheader_edge (loop);
3957 else
3959 gcc_assert (bb_vinfo);
3960 only_init = true;
3961 *ptr_incr = NULL;
3964 /* Check the step (evolution) of the load in LOOP, and record
3965 whether it's invariant. */
3966 if (nested_in_vect_loop)
3967 step = STMT_VINFO_DR_STEP (stmt_info);
3968 else
3969 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
3971 if (integer_zerop (step))
3972 *inv_p = true;
3973 else
3974 *inv_p = false;
3976 /* Create an expression for the first address accessed by this load
3977 in LOOP. */
3978 base_name = get_name (DR_BASE_ADDRESS (dr));
3980 if (dump_enabled_p ())
3982 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
3983 dump_printf_loc (MSG_NOTE, vect_location,
3984 "create %s-pointer variable to type: ",
3985 get_tree_code_name (TREE_CODE (aggr_type)));
3986 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
3987 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
3988 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
3989 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
3990 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
3991 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
3992 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
3993 else
3994 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
3995 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
3996 dump_printf (MSG_NOTE, "\n");
3999 /* (1) Create the new aggregate-pointer variable.
4000 Vector and array types inherit the alias set of their component
4001 type by default so we need to use a ref-all pointer if the data
4002 reference does not conflict with the created aggregated data
4003 reference because it is not addressable. */
4004 bool need_ref_all = false;
4005 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4006 get_alias_set (DR_REF (dr))))
4007 need_ref_all = true;
4008 /* Likewise for any of the data references in the stmt group. */
4009 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4011 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4014 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4015 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4016 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4017 get_alias_set (DR_REF (sdr))))
4019 need_ref_all = true;
4020 break;
4022 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4024 while (orig_stmt);
4026 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4027 need_ref_all);
4028 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4031 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4032 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4033 def-use update cycles for the pointer: one relative to the outer-loop
4034 (LOOP), which is what steps (3) and (4) below do. The other is relative
4035 to the inner-loop (which is the inner-most loop containing the dataref),
4036 and this is done be step (5) below.
4038 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4039 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4040 redundant. Steps (3),(4) create the following:
4042 vp0 = &base_addr;
4043 LOOP: vp1 = phi(vp0,vp2)
4046 vp2 = vp1 + step
4047 goto LOOP
4049 If there is an inner-loop nested in loop, then step (5) will also be
4050 applied, and an additional update in the inner-loop will be created:
4052 vp0 = &base_addr;
4053 LOOP: vp1 = phi(vp0,vp2)
4055 inner: vp3 = phi(vp1,vp4)
4056 vp4 = vp3 + inner_step
4057 if () goto inner
4059 vp2 = vp1 + step
4060 if () goto LOOP */
4062 /* (2) Calculate the initial address of the aggregate-pointer, and set
4063 the aggregate-pointer to point to it before the loop. */
4065 /* Create: (&(base[init_val+offset]) in the loop preheader. */
4067 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4068 offset, loop);
4069 if (new_stmt_list)
4071 if (pe)
4073 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4074 gcc_assert (!new_bb);
4076 else
4077 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4080 *initial_address = new_temp;
4082 /* Create: p = (aggr_type *) initial_base */
4083 if (TREE_CODE (new_temp) != SSA_NAME
4084 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4086 vec_stmt = gimple_build_assign (aggr_ptr,
4087 fold_convert (aggr_ptr_type, new_temp));
4088 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4089 /* Copy the points-to information if it exists. */
4090 if (DR_PTR_INFO (dr))
4091 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
4092 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4093 if (pe)
4095 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4096 gcc_assert (!new_bb);
4098 else
4099 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4101 else
4102 aggr_ptr_init = new_temp;
4104 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4105 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4106 inner-loop nested in LOOP (during outer-loop vectorization). */
4108 /* No update in loop is required. */
4109 if (only_init && (!loop_vinfo || at_loop == loop))
4110 aptr = aggr_ptr_init;
4111 else
4113 /* The step of the aggregate pointer is the type size. */
4114 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4115 /* One exception to the above is when the scalar step of the load in
4116 LOOP is zero. In this case the step here is also zero. */
4117 if (*inv_p)
4118 iv_step = size_zero_node;
4119 else if (tree_int_cst_sgn (step) == -1)
4120 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4122 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4124 create_iv (aggr_ptr_init,
4125 fold_convert (aggr_ptr_type, iv_step),
4126 aggr_ptr, loop, &incr_gsi, insert_after,
4127 &indx_before_incr, &indx_after_incr);
4128 incr = gsi_stmt (incr_gsi);
4129 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4131 /* Copy the points-to information if it exists. */
4132 if (DR_PTR_INFO (dr))
4134 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4135 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4137 if (ptr_incr)
4138 *ptr_incr = incr;
4140 aptr = indx_before_incr;
4143 if (!nested_in_vect_loop || only_init)
4144 return aptr;
4147 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4148 nested in LOOP, if exists. */
4150 gcc_assert (nested_in_vect_loop);
4151 if (!only_init)
4153 standard_iv_increment_position (containing_loop, &incr_gsi,
4154 &insert_after);
4155 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4156 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4157 &indx_after_incr);
4158 incr = gsi_stmt (incr_gsi);
4159 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4161 /* Copy the points-to information if it exists. */
4162 if (DR_PTR_INFO (dr))
4164 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4165 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4167 if (ptr_incr)
4168 *ptr_incr = incr;
4170 return indx_before_incr;
4172 else
4173 gcc_unreachable ();
4177 /* Function bump_vector_ptr
4179 Increment a pointer (to a vector type) by vector-size. If requested,
4180 i.e. if PTR-INCR is given, then also connect the new increment stmt
4181 to the existing def-use update-chain of the pointer, by modifying
4182 the PTR_INCR as illustrated below:
4184 The pointer def-use update-chain before this function:
4185 DATAREF_PTR = phi (p_0, p_2)
4186 ....
4187 PTR_INCR: p_2 = DATAREF_PTR + step
4189 The pointer def-use update-chain after this function:
4190 DATAREF_PTR = phi (p_0, p_2)
4191 ....
4192 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4193 ....
4194 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4196 Input:
4197 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4198 in the loop.
4199 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4200 the loop. The increment amount across iterations is expected
4201 to be vector_size.
4202 BSI - location where the new update stmt is to be placed.
4203 STMT - the original scalar memory-access stmt that is being vectorized.
4204 BUMP - optional. The offset by which to bump the pointer. If not given,
4205 the offset is assumed to be vector_size.
4207 Output: Return NEW_DATAREF_PTR as illustrated above.
4211 tree
4212 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4213 gimple stmt, tree bump)
4215 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4216 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4217 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4218 tree update = TYPE_SIZE_UNIT (vectype);
4219 gimple incr_stmt;
4220 ssa_op_iter iter;
4221 use_operand_p use_p;
4222 tree new_dataref_ptr;
4224 if (bump)
4225 update = bump;
4227 new_dataref_ptr = copy_ssa_name (dataref_ptr, NULL);
4228 incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, new_dataref_ptr,
4229 dataref_ptr, update);
4230 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4232 /* Copy the points-to information if it exists. */
4233 if (DR_PTR_INFO (dr))
4235 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4236 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4239 if (!ptr_incr)
4240 return new_dataref_ptr;
4242 /* Update the vector-pointer's cross-iteration increment. */
4243 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4245 tree use = USE_FROM_PTR (use_p);
4247 if (use == dataref_ptr)
4248 SET_USE (use_p, new_dataref_ptr);
4249 else
4250 gcc_assert (tree_int_cst_compare (use, update) == 0);
4253 return new_dataref_ptr;
4257 /* Function vect_create_destination_var.
4259 Create a new temporary of type VECTYPE. */
4261 tree
4262 vect_create_destination_var (tree scalar_dest, tree vectype)
4264 tree vec_dest;
4265 const char *name;
4266 char *new_name;
4267 tree type;
4268 enum vect_var_kind kind;
4270 kind = vectype ? vect_simple_var : vect_scalar_var;
4271 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4273 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4275 name = get_name (scalar_dest);
4276 if (name)
4277 asprintf (&new_name, "%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4278 else
4279 asprintf (&new_name, "_%u", SSA_NAME_VERSION (scalar_dest));
4280 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4281 free (new_name);
4283 return vec_dest;
4286 /* Function vect_grouped_store_supported.
4288 Returns TRUE if interleave high and interleave low permutations
4289 are supported, and FALSE otherwise. */
4291 bool
4292 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4294 enum machine_mode mode = TYPE_MODE (vectype);
4296 /* vect_permute_store_chain requires the group size to be a power of two. */
4297 if (exact_log2 (count) == -1)
4299 if (dump_enabled_p ())
4300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4301 "the size of the group of accesses"
4302 " is not a power of 2\n");
4303 return false;
4306 /* Check that the permutation is supported. */
4307 if (VECTOR_MODE_P (mode))
4309 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4310 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4311 for (i = 0; i < nelt / 2; i++)
4313 sel[i * 2] = i;
4314 sel[i * 2 + 1] = i + nelt;
4316 if (can_vec_perm_p (mode, false, sel))
4318 for (i = 0; i < nelt; i++)
4319 sel[i] += nelt / 2;
4320 if (can_vec_perm_p (mode, false, sel))
4321 return true;
4325 if (dump_enabled_p ())
4326 dump_printf (MSG_MISSED_OPTIMIZATION,
4327 "interleave op not supported by target.\n");
4328 return false;
4332 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4333 type VECTYPE. */
4335 bool
4336 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4338 return vect_lanes_optab_supported_p ("vec_store_lanes",
4339 vec_store_lanes_optab,
4340 vectype, count);
4344 /* Function vect_permute_store_chain.
4346 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4347 a power of 2, generate interleave_high/low stmts to reorder the data
4348 correctly for the stores. Return the final references for stores in
4349 RESULT_CHAIN.
4351 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4352 The input is 4 vectors each containing 8 elements. We assign a number to
4353 each element, the input sequence is:
4355 1st vec: 0 1 2 3 4 5 6 7
4356 2nd vec: 8 9 10 11 12 13 14 15
4357 3rd vec: 16 17 18 19 20 21 22 23
4358 4th vec: 24 25 26 27 28 29 30 31
4360 The output sequence should be:
4362 1st vec: 0 8 16 24 1 9 17 25
4363 2nd vec: 2 10 18 26 3 11 19 27
4364 3rd vec: 4 12 20 28 5 13 21 30
4365 4th vec: 6 14 22 30 7 15 23 31
4367 i.e., we interleave the contents of the four vectors in their order.
4369 We use interleave_high/low instructions to create such output. The input of
4370 each interleave_high/low operation is two vectors:
4371 1st vec 2nd vec
4372 0 1 2 3 4 5 6 7
4373 the even elements of the result vector are obtained left-to-right from the
4374 high/low elements of the first vector. The odd elements of the result are
4375 obtained left-to-right from the high/low elements of the second vector.
4376 The output of interleave_high will be: 0 4 1 5
4377 and of interleave_low: 2 6 3 7
4380 The permutation is done in log LENGTH stages. In each stage interleave_high
4381 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4382 where the first argument is taken from the first half of DR_CHAIN and the
4383 second argument from it's second half.
4384 In our example,
4386 I1: interleave_high (1st vec, 3rd vec)
4387 I2: interleave_low (1st vec, 3rd vec)
4388 I3: interleave_high (2nd vec, 4th vec)
4389 I4: interleave_low (2nd vec, 4th vec)
4391 The output for the first stage is:
4393 I1: 0 16 1 17 2 18 3 19
4394 I2: 4 20 5 21 6 22 7 23
4395 I3: 8 24 9 25 10 26 11 27
4396 I4: 12 28 13 29 14 30 15 31
4398 The output of the second stage, i.e. the final result is:
4400 I1: 0 8 16 24 1 9 17 25
4401 I2: 2 10 18 26 3 11 19 27
4402 I3: 4 12 20 28 5 13 21 30
4403 I4: 6 14 22 30 7 15 23 31. */
4405 void
4406 vect_permute_store_chain (vec<tree> dr_chain,
4407 unsigned int length,
4408 gimple stmt,
4409 gimple_stmt_iterator *gsi,
4410 vec<tree> *result_chain)
4412 tree vect1, vect2, high, low;
4413 gimple perm_stmt;
4414 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4415 tree perm_mask_low, perm_mask_high;
4416 unsigned int i, n;
4417 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4418 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4420 result_chain->quick_grow (length);
4421 memcpy (result_chain->address (), dr_chain.address (),
4422 length * sizeof (tree));
4424 for (i = 0, n = nelt / 2; i < n; i++)
4426 sel[i * 2] = i;
4427 sel[i * 2 + 1] = i + nelt;
4429 perm_mask_high = vect_gen_perm_mask (vectype, sel);
4430 gcc_assert (perm_mask_high != NULL);
4432 for (i = 0; i < nelt; i++)
4433 sel[i] += nelt / 2;
4434 perm_mask_low = vect_gen_perm_mask (vectype, sel);
4435 gcc_assert (perm_mask_low != NULL);
4437 for (i = 0, n = exact_log2 (length); i < n; i++)
4439 for (j = 0; j < length/2; j++)
4441 vect1 = dr_chain[j];
4442 vect2 = dr_chain[j+length/2];
4444 /* Create interleaving stmt:
4445 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1, ...}> */
4446 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4447 perm_stmt
4448 = gimple_build_assign_with_ops (VEC_PERM_EXPR, high,
4449 vect1, vect2, perm_mask_high);
4450 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4451 (*result_chain)[2*j] = high;
4453 /* Create interleaving stmt:
4454 low = VEC_PERM_EXPR <vect1, vect2, {nelt/2, nelt*3/2, nelt/2+1,
4455 nelt*3/2+1, ...}> */
4456 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4457 perm_stmt
4458 = gimple_build_assign_with_ops (VEC_PERM_EXPR, low,
4459 vect1, vect2, perm_mask_low);
4460 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4461 (*result_chain)[2*j+1] = low;
4463 memcpy (dr_chain.address (), result_chain->address (),
4464 length * sizeof (tree));
4468 /* Function vect_setup_realignment
4470 This function is called when vectorizing an unaligned load using
4471 the dr_explicit_realign[_optimized] scheme.
4472 This function generates the following code at the loop prolog:
4474 p = initial_addr;
4475 x msq_init = *(floor(p)); # prolog load
4476 realignment_token = call target_builtin;
4477 loop:
4478 x msq = phi (msq_init, ---)
4480 The stmts marked with x are generated only for the case of
4481 dr_explicit_realign_optimized.
4483 The code above sets up a new (vector) pointer, pointing to the first
4484 location accessed by STMT, and a "floor-aligned" load using that pointer.
4485 It also generates code to compute the "realignment-token" (if the relevant
4486 target hook was defined), and creates a phi-node at the loop-header bb
4487 whose arguments are the result of the prolog-load (created by this
4488 function) and the result of a load that takes place in the loop (to be
4489 created by the caller to this function).
4491 For the case of dr_explicit_realign_optimized:
4492 The caller to this function uses the phi-result (msq) to create the
4493 realignment code inside the loop, and sets up the missing phi argument,
4494 as follows:
4495 loop:
4496 msq = phi (msq_init, lsq)
4497 lsq = *(floor(p')); # load in loop
4498 result = realign_load (msq, lsq, realignment_token);
4500 For the case of dr_explicit_realign:
4501 loop:
4502 msq = *(floor(p)); # load in loop
4503 p' = p + (VS-1);
4504 lsq = *(floor(p')); # load in loop
4505 result = realign_load (msq, lsq, realignment_token);
4507 Input:
4508 STMT - (scalar) load stmt to be vectorized. This load accesses
4509 a memory location that may be unaligned.
4510 BSI - place where new code is to be inserted.
4511 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4512 is used.
4514 Output:
4515 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4516 target hook, if defined.
4517 Return value - the result of the loop-header phi node. */
4519 tree
4520 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4521 tree *realignment_token,
4522 enum dr_alignment_support alignment_support_scheme,
4523 tree init_addr,
4524 struct loop **at_loop)
4526 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4527 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4528 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4529 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4530 struct loop *loop = NULL;
4531 edge pe = NULL;
4532 tree scalar_dest = gimple_assign_lhs (stmt);
4533 tree vec_dest;
4534 gimple inc;
4535 tree ptr;
4536 tree data_ref;
4537 gimple new_stmt;
4538 basic_block new_bb;
4539 tree msq_init = NULL_TREE;
4540 tree new_temp;
4541 gimple phi_stmt;
4542 tree msq = NULL_TREE;
4543 gimple_seq stmts = NULL;
4544 bool inv_p;
4545 bool compute_in_loop = false;
4546 bool nested_in_vect_loop = false;
4547 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4548 struct loop *loop_for_initial_load = NULL;
4550 if (loop_vinfo)
4552 loop = LOOP_VINFO_LOOP (loop_vinfo);
4553 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4556 gcc_assert (alignment_support_scheme == dr_explicit_realign
4557 || alignment_support_scheme == dr_explicit_realign_optimized);
4559 /* We need to generate three things:
4560 1. the misalignment computation
4561 2. the extra vector load (for the optimized realignment scheme).
4562 3. the phi node for the two vectors from which the realignment is
4563 done (for the optimized realignment scheme). */
4565 /* 1. Determine where to generate the misalignment computation.
4567 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4568 calculation will be generated by this function, outside the loop (in the
4569 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4570 caller, inside the loop.
4572 Background: If the misalignment remains fixed throughout the iterations of
4573 the loop, then both realignment schemes are applicable, and also the
4574 misalignment computation can be done outside LOOP. This is because we are
4575 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4576 are a multiple of VS (the Vector Size), and therefore the misalignment in
4577 different vectorized LOOP iterations is always the same.
4578 The problem arises only if the memory access is in an inner-loop nested
4579 inside LOOP, which is now being vectorized using outer-loop vectorization.
4580 This is the only case when the misalignment of the memory access may not
4581 remain fixed throughout the iterations of the inner-loop (as explained in
4582 detail in vect_supportable_dr_alignment). In this case, not only is the
4583 optimized realignment scheme not applicable, but also the misalignment
4584 computation (and generation of the realignment token that is passed to
4585 REALIGN_LOAD) have to be done inside the loop.
4587 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4588 or not, which in turn determines if the misalignment is computed inside
4589 the inner-loop, or outside LOOP. */
4591 if (init_addr != NULL_TREE || !loop_vinfo)
4593 compute_in_loop = true;
4594 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4598 /* 2. Determine where to generate the extra vector load.
4600 For the optimized realignment scheme, instead of generating two vector
4601 loads in each iteration, we generate a single extra vector load in the
4602 preheader of the loop, and in each iteration reuse the result of the
4603 vector load from the previous iteration. In case the memory access is in
4604 an inner-loop nested inside LOOP, which is now being vectorized using
4605 outer-loop vectorization, we need to determine whether this initial vector
4606 load should be generated at the preheader of the inner-loop, or can be
4607 generated at the preheader of LOOP. If the memory access has no evolution
4608 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4609 to be generated inside LOOP (in the preheader of the inner-loop). */
4611 if (nested_in_vect_loop)
4613 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4614 bool invariant_in_outerloop =
4615 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4616 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4618 else
4619 loop_for_initial_load = loop;
4620 if (at_loop)
4621 *at_loop = loop_for_initial_load;
4623 if (loop_for_initial_load)
4624 pe = loop_preheader_edge (loop_for_initial_load);
4626 /* 3. For the case of the optimized realignment, create the first vector
4627 load at the loop preheader. */
4629 if (alignment_support_scheme == dr_explicit_realign_optimized)
4631 /* Create msq_init = *(floor(p1)) in the loop preheader */
4633 gcc_assert (!compute_in_loop);
4634 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4635 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4636 NULL_TREE, &init_addr, NULL, &inc,
4637 true, &inv_p);
4638 new_temp = copy_ssa_name (ptr, NULL);
4639 new_stmt = gimple_build_assign_with_ops
4640 (BIT_AND_EXPR, new_temp, ptr,
4641 build_int_cst (TREE_TYPE (ptr),
4642 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4643 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4644 gcc_assert (!new_bb);
4645 data_ref
4646 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4647 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4648 new_stmt = gimple_build_assign (vec_dest, data_ref);
4649 new_temp = make_ssa_name (vec_dest, new_stmt);
4650 gimple_assign_set_lhs (new_stmt, new_temp);
4651 if (pe)
4653 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4654 gcc_assert (!new_bb);
4656 else
4657 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4659 msq_init = gimple_assign_lhs (new_stmt);
4662 /* 4. Create realignment token using a target builtin, if available.
4663 It is done either inside the containing loop, or before LOOP (as
4664 determined above). */
4666 if (targetm.vectorize.builtin_mask_for_load)
4668 tree builtin_decl;
4670 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4671 if (!init_addr)
4673 /* Generate the INIT_ADDR computation outside LOOP. */
4674 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4675 NULL_TREE, loop);
4676 if (loop)
4678 pe = loop_preheader_edge (loop);
4679 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4680 gcc_assert (!new_bb);
4682 else
4683 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4686 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4687 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4688 vec_dest =
4689 vect_create_destination_var (scalar_dest,
4690 gimple_call_return_type (new_stmt));
4691 new_temp = make_ssa_name (vec_dest, new_stmt);
4692 gimple_call_set_lhs (new_stmt, new_temp);
4694 if (compute_in_loop)
4695 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4696 else
4698 /* Generate the misalignment computation outside LOOP. */
4699 pe = loop_preheader_edge (loop);
4700 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4701 gcc_assert (!new_bb);
4704 *realignment_token = gimple_call_lhs (new_stmt);
4706 /* The result of the CALL_EXPR to this builtin is determined from
4707 the value of the parameter and no global variables are touched
4708 which makes the builtin a "const" function. Requiring the
4709 builtin to have the "const" attribute makes it unnecessary
4710 to call mark_call_clobbered. */
4711 gcc_assert (TREE_READONLY (builtin_decl));
4714 if (alignment_support_scheme == dr_explicit_realign)
4715 return msq;
4717 gcc_assert (!compute_in_loop);
4718 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4721 /* 5. Create msq = phi <msq_init, lsq> in loop */
4723 pe = loop_preheader_edge (containing_loop);
4724 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4725 msq = make_ssa_name (vec_dest, NULL);
4726 phi_stmt = create_phi_node (msq, containing_loop->header);
4727 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4729 return msq;
4733 /* Function vect_grouped_load_supported.
4735 Returns TRUE if even and odd permutations are supported,
4736 and FALSE otherwise. */
4738 bool
4739 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4741 enum machine_mode mode = TYPE_MODE (vectype);
4743 /* vect_permute_load_chain requires the group size to be a power of two. */
4744 if (exact_log2 (count) == -1)
4746 if (dump_enabled_p ())
4747 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4748 "the size of the group of accesses"
4749 " is not a power of 2\n");
4750 return false;
4753 /* Check that the permutation is supported. */
4754 if (VECTOR_MODE_P (mode))
4756 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4757 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4759 for (i = 0; i < nelt; i++)
4760 sel[i] = i * 2;
4761 if (can_vec_perm_p (mode, false, sel))
4763 for (i = 0; i < nelt; i++)
4764 sel[i] = i * 2 + 1;
4765 if (can_vec_perm_p (mode, false, sel))
4766 return true;
4770 if (dump_enabled_p ())
4771 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4772 "extract even/odd not supported by target\n");
4773 return false;
4776 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
4777 type VECTYPE. */
4779 bool
4780 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4782 return vect_lanes_optab_supported_p ("vec_load_lanes",
4783 vec_load_lanes_optab,
4784 vectype, count);
4787 /* Function vect_permute_load_chain.
4789 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
4790 a power of 2, generate extract_even/odd stmts to reorder the input data
4791 correctly. Return the final references for loads in RESULT_CHAIN.
4793 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4794 The input is 4 vectors each containing 8 elements. We assign a number to each
4795 element, the input sequence is:
4797 1st vec: 0 1 2 3 4 5 6 7
4798 2nd vec: 8 9 10 11 12 13 14 15
4799 3rd vec: 16 17 18 19 20 21 22 23
4800 4th vec: 24 25 26 27 28 29 30 31
4802 The output sequence should be:
4804 1st vec: 0 4 8 12 16 20 24 28
4805 2nd vec: 1 5 9 13 17 21 25 29
4806 3rd vec: 2 6 10 14 18 22 26 30
4807 4th vec: 3 7 11 15 19 23 27 31
4809 i.e., the first output vector should contain the first elements of each
4810 interleaving group, etc.
4812 We use extract_even/odd instructions to create such output. The input of
4813 each extract_even/odd operation is two vectors
4814 1st vec 2nd vec
4815 0 1 2 3 4 5 6 7
4817 and the output is the vector of extracted even/odd elements. The output of
4818 extract_even will be: 0 2 4 6
4819 and of extract_odd: 1 3 5 7
4822 The permutation is done in log LENGTH stages. In each stage extract_even
4823 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
4824 their order. In our example,
4826 E1: extract_even (1st vec, 2nd vec)
4827 E2: extract_odd (1st vec, 2nd vec)
4828 E3: extract_even (3rd vec, 4th vec)
4829 E4: extract_odd (3rd vec, 4th vec)
4831 The output for the first stage will be:
4833 E1: 0 2 4 6 8 10 12 14
4834 E2: 1 3 5 7 9 11 13 15
4835 E3: 16 18 20 22 24 26 28 30
4836 E4: 17 19 21 23 25 27 29 31
4838 In order to proceed and create the correct sequence for the next stage (or
4839 for the correct output, if the second stage is the last one, as in our
4840 example), we first put the output of extract_even operation and then the
4841 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
4842 The input for the second stage is:
4844 1st vec (E1): 0 2 4 6 8 10 12 14
4845 2nd vec (E3): 16 18 20 22 24 26 28 30
4846 3rd vec (E2): 1 3 5 7 9 11 13 15
4847 4th vec (E4): 17 19 21 23 25 27 29 31
4849 The output of the second stage:
4851 E1: 0 4 8 12 16 20 24 28
4852 E2: 2 6 10 14 18 22 26 30
4853 E3: 1 5 9 13 17 21 25 29
4854 E4: 3 7 11 15 19 23 27 31
4856 And RESULT_CHAIN after reordering:
4858 1st vec (E1): 0 4 8 12 16 20 24 28
4859 2nd vec (E3): 1 5 9 13 17 21 25 29
4860 3rd vec (E2): 2 6 10 14 18 22 26 30
4861 4th vec (E4): 3 7 11 15 19 23 27 31. */
4863 static void
4864 vect_permute_load_chain (vec<tree> dr_chain,
4865 unsigned int length,
4866 gimple stmt,
4867 gimple_stmt_iterator *gsi,
4868 vec<tree> *result_chain)
4870 tree data_ref, first_vect, second_vect;
4871 tree perm_mask_even, perm_mask_odd;
4872 gimple perm_stmt;
4873 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4874 unsigned int i, j, log_length = exact_log2 (length);
4875 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
4876 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4878 result_chain->quick_grow (length);
4879 memcpy (result_chain->address (), dr_chain.address (),
4880 length * sizeof (tree));
4882 for (i = 0; i < nelt; ++i)
4883 sel[i] = i * 2;
4884 perm_mask_even = vect_gen_perm_mask (vectype, sel);
4885 gcc_assert (perm_mask_even != NULL);
4887 for (i = 0; i < nelt; ++i)
4888 sel[i] = i * 2 + 1;
4889 perm_mask_odd = vect_gen_perm_mask (vectype, sel);
4890 gcc_assert (perm_mask_odd != NULL);
4892 for (i = 0; i < log_length; i++)
4894 for (j = 0; j < length; j += 2)
4896 first_vect = dr_chain[j];
4897 second_vect = dr_chain[j+1];
4899 /* data_ref = permute_even (first_data_ref, second_data_ref); */
4900 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
4901 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4902 first_vect, second_vect,
4903 perm_mask_even);
4904 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4905 (*result_chain)[j/2] = data_ref;
4907 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
4908 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
4909 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4910 first_vect, second_vect,
4911 perm_mask_odd);
4912 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4913 (*result_chain)[j/2+length/2] = data_ref;
4915 memcpy (dr_chain.address (), result_chain->address (),
4916 length * sizeof (tree));
4921 /* Function vect_transform_grouped_load.
4923 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
4924 to perform their permutation and ascribe the result vectorized statements to
4925 the scalar statements.
4928 void
4929 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
4930 gimple_stmt_iterator *gsi)
4932 vec<tree> result_chain = vNULL;
4934 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
4935 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
4936 vectors, that are ready for vector computation. */
4937 result_chain.create (size);
4938 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
4939 vect_record_grouped_load_vectors (stmt, result_chain);
4940 result_chain.release ();
4943 /* RESULT_CHAIN contains the output of a group of grouped loads that were
4944 generated as part of the vectorization of STMT. Assign the statement
4945 for each vector to the associated scalar statement. */
4947 void
4948 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
4950 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
4951 gimple next_stmt, new_stmt;
4952 unsigned int i, gap_count;
4953 tree tmp_data_ref;
4955 /* Put a permuted data-ref in the VECTORIZED_STMT field.
4956 Since we scan the chain starting from it's first node, their order
4957 corresponds the order of data-refs in RESULT_CHAIN. */
4958 next_stmt = first_stmt;
4959 gap_count = 1;
4960 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
4962 if (!next_stmt)
4963 break;
4965 /* Skip the gaps. Loads created for the gaps will be removed by dead
4966 code elimination pass later. No need to check for the first stmt in
4967 the group, since it always exists.
4968 GROUP_GAP is the number of steps in elements from the previous
4969 access (if there is no gap GROUP_GAP is 1). We skip loads that
4970 correspond to the gaps. */
4971 if (next_stmt != first_stmt
4972 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
4974 gap_count++;
4975 continue;
4978 while (next_stmt)
4980 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
4981 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
4982 copies, and we put the new vector statement in the first available
4983 RELATED_STMT. */
4984 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
4985 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
4986 else
4988 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
4990 gimple prev_stmt =
4991 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
4992 gimple rel_stmt =
4993 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
4994 while (rel_stmt)
4996 prev_stmt = rel_stmt;
4997 rel_stmt =
4998 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5001 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5002 new_stmt;
5006 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5007 gap_count = 1;
5008 /* If NEXT_STMT accesses the same DR as the previous statement,
5009 put the same TMP_DATA_REF as its vectorized statement; otherwise
5010 get the next data-ref from RESULT_CHAIN. */
5011 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5012 break;
5017 /* Function vect_force_dr_alignment_p.
5019 Returns whether the alignment of a DECL can be forced to be aligned
5020 on ALIGNMENT bit boundary. */
5022 bool
5023 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5025 if (TREE_CODE (decl) != VAR_DECL)
5026 return false;
5028 /* We cannot change alignment of common or external symbols as another
5029 translation unit may contain a definition with lower alignment.
5030 The rules of common symbol linking mean that the definition
5031 will override the common symbol. The same is true for constant
5032 pool entries which may be shared and are not properly merged
5033 by LTO. */
5034 if (DECL_EXTERNAL (decl)
5035 || DECL_COMMON (decl)
5036 || DECL_IN_CONSTANT_POOL (decl))
5037 return false;
5039 if (TREE_ASM_WRITTEN (decl))
5040 return false;
5042 /* Do not override the alignment as specified by the ABI when the used
5043 attribute is set. */
5044 if (DECL_PRESERVE_P (decl))
5045 return false;
5047 /* Do not override explicit alignment set by the user when an explicit
5048 section name is also used. This is a common idiom used by many
5049 software projects. */
5050 if (DECL_SECTION_NAME (decl) != NULL_TREE
5051 && !DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
5052 return false;
5054 if (TREE_STATIC (decl))
5055 return (alignment <= MAX_OFILE_ALIGNMENT);
5056 else
5057 return (alignment <= MAX_STACK_ALIGNMENT);
5061 /* Return whether the data reference DR is supported with respect to its
5062 alignment.
5063 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5064 it is aligned, i.e., check if it is possible to vectorize it with different
5065 alignment. */
5067 enum dr_alignment_support
5068 vect_supportable_dr_alignment (struct data_reference *dr,
5069 bool check_aligned_accesses)
5071 gimple stmt = DR_STMT (dr);
5072 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5073 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5074 enum machine_mode mode = TYPE_MODE (vectype);
5075 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5076 struct loop *vect_loop = NULL;
5077 bool nested_in_vect_loop = false;
5079 if (aligned_access_p (dr) && !check_aligned_accesses)
5080 return dr_aligned;
5082 if (loop_vinfo)
5084 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5085 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5088 /* Possibly unaligned access. */
5090 /* We can choose between using the implicit realignment scheme (generating
5091 a misaligned_move stmt) and the explicit realignment scheme (generating
5092 aligned loads with a REALIGN_LOAD). There are two variants to the
5093 explicit realignment scheme: optimized, and unoptimized.
5094 We can optimize the realignment only if the step between consecutive
5095 vector loads is equal to the vector size. Since the vector memory
5096 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5097 is guaranteed that the misalignment amount remains the same throughout the
5098 execution of the vectorized loop. Therefore, we can create the
5099 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5100 at the loop preheader.
5102 However, in the case of outer-loop vectorization, when vectorizing a
5103 memory access in the inner-loop nested within the LOOP that is now being
5104 vectorized, while it is guaranteed that the misalignment of the
5105 vectorized memory access will remain the same in different outer-loop
5106 iterations, it is *not* guaranteed that is will remain the same throughout
5107 the execution of the inner-loop. This is because the inner-loop advances
5108 with the original scalar step (and not in steps of VS). If the inner-loop
5109 step happens to be a multiple of VS, then the misalignment remains fixed
5110 and we can use the optimized realignment scheme. For example:
5112 for (i=0; i<N; i++)
5113 for (j=0; j<M; j++)
5114 s += a[i+j];
5116 When vectorizing the i-loop in the above example, the step between
5117 consecutive vector loads is 1, and so the misalignment does not remain
5118 fixed across the execution of the inner-loop, and the realignment cannot
5119 be optimized (as illustrated in the following pseudo vectorized loop):
5121 for (i=0; i<N; i+=4)
5122 for (j=0; j<M; j++){
5123 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5124 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5125 // (assuming that we start from an aligned address).
5128 We therefore have to use the unoptimized realignment scheme:
5130 for (i=0; i<N; i+=4)
5131 for (j=k; j<M; j+=4)
5132 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5133 // that the misalignment of the initial address is
5134 // 0).
5136 The loop can then be vectorized as follows:
5138 for (k=0; k<4; k++){
5139 rt = get_realignment_token (&vp[k]);
5140 for (i=0; i<N; i+=4){
5141 v1 = vp[i+k];
5142 for (j=k; j<M; j+=4){
5143 v2 = vp[i+j+VS-1];
5144 va = REALIGN_LOAD <v1,v2,rt>;
5145 vs += va;
5146 v1 = v2;
5149 } */
5151 if (DR_IS_READ (dr))
5153 bool is_packed = false;
5154 tree type = (TREE_TYPE (DR_REF (dr)));
5156 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5157 && (!targetm.vectorize.builtin_mask_for_load
5158 || targetm.vectorize.builtin_mask_for_load ()))
5160 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5161 if ((nested_in_vect_loop
5162 && (TREE_INT_CST_LOW (DR_STEP (dr))
5163 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5164 || !loop_vinfo)
5165 return dr_explicit_realign;
5166 else
5167 return dr_explicit_realign_optimized;
5169 if (!known_alignment_for_access_p (dr))
5170 is_packed = not_size_aligned (DR_REF (dr));
5172 if ((TYPE_USER_ALIGN (type) && !is_packed)
5173 || targetm.vectorize.
5174 support_vector_misalignment (mode, type,
5175 DR_MISALIGNMENT (dr), is_packed))
5176 /* Can't software pipeline the loads, but can at least do them. */
5177 return dr_unaligned_supported;
5179 else
5181 bool is_packed = false;
5182 tree type = (TREE_TYPE (DR_REF (dr)));
5184 if (!known_alignment_for_access_p (dr))
5185 is_packed = not_size_aligned (DR_REF (dr));
5187 if ((TYPE_USER_ALIGN (type) && !is_packed)
5188 || targetm.vectorize.
5189 support_vector_misalignment (mode, type,
5190 DR_MISALIGNMENT (dr), is_packed))
5191 return dr_unaligned_supported;
5194 /* Unsupported. */
5195 return dr_unaligned_unsupported;