re PR lto/45375 ([meta-bug] Issues with building Mozilla (i.e. Firefox) with LTO)
[official-gcc.git] / gcc / tree-vect-data-refs.c
blob52d6a869c4e6dfbc15b1c5cf1afa070d5efd270f
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "tm_p.h"
40 #include "target.h"
41 #include "predict.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "basic-block.h"
47 #include "gimple-pretty-print.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimplify.h"
55 #include "gimple-iterator.h"
56 #include "gimplify-me.h"
57 #include "gimple-ssa.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-ssa-loop-ivopts.h"
63 #include "tree-ssa-loop-manip.h"
64 #include "tree-ssa-loop.h"
65 #include "cfgloop.h"
66 #include "tree-chrec.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-vectorizer.h"
69 #include "diagnostic-core.h"
70 #include "hash-map.h"
71 #include "plugin-api.h"
72 #include "ipa-ref.h"
73 #include "cgraph.h"
74 /* Need to include rtl.h, expr.h, etc. for optabs. */
75 #include "hashtab.h"
76 #include "rtl.h"
77 #include "flags.h"
78 #include "statistics.h"
79 #include "real.h"
80 #include "fixed-value.h"
81 #include "insn-config.h"
82 #include "expmed.h"
83 #include "dojump.h"
84 #include "explow.h"
85 #include "calls.h"
86 #include "emit-rtl.h"
87 #include "varasm.h"
88 #include "stmt.h"
89 #include "expr.h"
90 #include "insn-codes.h"
91 #include "optabs.h"
92 #include "builtins.h"
94 /* Return true if load- or store-lanes optab OPTAB is implemented for
95 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
97 static bool
98 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
99 tree vectype, unsigned HOST_WIDE_INT count)
101 machine_mode mode, array_mode;
102 bool limit_p;
104 mode = TYPE_MODE (vectype);
105 limit_p = !targetm.array_mode_supported_p (mode, count);
106 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
107 MODE_INT, limit_p);
109 if (array_mode == BLKmode)
111 if (dump_enabled_p ())
112 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
113 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
114 GET_MODE_NAME (mode), count);
115 return false;
118 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
120 if (dump_enabled_p ())
121 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
122 "cannot use %s<%s><%s>\n", name,
123 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
124 return false;
127 if (dump_enabled_p ())
128 dump_printf_loc (MSG_NOTE, vect_location,
129 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
130 GET_MODE_NAME (mode));
132 return true;
136 /* Return the smallest scalar part of STMT.
137 This is used to determine the vectype of the stmt. We generally set the
138 vectype according to the type of the result (lhs). For stmts whose
139 result-type is different than the type of the arguments (e.g., demotion,
140 promotion), vectype will be reset appropriately (later). Note that we have
141 to visit the smallest datatype in this function, because that determines the
142 VF. If the smallest datatype in the loop is present only as the rhs of a
143 promotion operation - we'd miss it.
144 Such a case, where a variable of this datatype does not appear in the lhs
145 anywhere in the loop, can only occur if it's an invariant: e.g.:
146 'int_x = (int) short_inv', which we'd expect to have been optimized away by
147 invariant motion. However, we cannot rely on invariant motion to always
148 take invariants out of the loop, and so in the case of promotion we also
149 have to check the rhs.
150 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
151 types. */
153 tree
154 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
155 HOST_WIDE_INT *rhs_size_unit)
157 tree scalar_type = gimple_expr_type (stmt);
158 HOST_WIDE_INT lhs, rhs;
160 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
162 if (is_gimple_assign (stmt)
163 && (gimple_assign_cast_p (stmt)
164 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
165 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
166 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
168 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
170 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
171 if (rhs < lhs)
172 scalar_type = rhs_type;
175 *lhs_size_unit = lhs;
176 *rhs_size_unit = rhs;
177 return scalar_type;
181 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
182 tested at run-time. Return TRUE if DDR was successfully inserted.
183 Return false if versioning is not supported. */
185 static bool
186 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
188 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
190 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
191 return false;
193 if (dump_enabled_p ())
195 dump_printf_loc (MSG_NOTE, vect_location,
196 "mark for run-time aliasing test between ");
197 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
198 dump_printf (MSG_NOTE, " and ");
199 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
200 dump_printf (MSG_NOTE, "\n");
203 if (optimize_loop_nest_for_size_p (loop))
205 if (dump_enabled_p ())
206 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
207 "versioning not supported when optimizing"
208 " for size.\n");
209 return false;
212 /* FORNOW: We don't support versioning with outer-loop vectorization. */
213 if (loop->inner)
215 if (dump_enabled_p ())
216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
217 "versioning not yet supported for outer-loops.\n");
218 return false;
221 /* FORNOW: We don't support creating runtime alias tests for non-constant
222 step. */
223 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
224 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
226 if (dump_enabled_p ())
227 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
228 "versioning not yet supported for non-constant "
229 "step\n");
230 return false;
233 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
234 return true;
238 /* Function vect_analyze_data_ref_dependence.
240 Return TRUE if there (might) exist a dependence between a memory-reference
241 DRA and a memory-reference DRB. When versioning for alias may check a
242 dependence at run-time, return FALSE. Adjust *MAX_VF according to
243 the data dependence. */
245 static bool
246 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
247 loop_vec_info loop_vinfo, int *max_vf)
249 unsigned int i;
250 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
251 struct data_reference *dra = DDR_A (ddr);
252 struct data_reference *drb = DDR_B (ddr);
253 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
254 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
255 lambda_vector dist_v;
256 unsigned int loop_depth;
258 /* In loop analysis all data references should be vectorizable. */
259 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
260 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
261 gcc_unreachable ();
263 /* Independent data accesses. */
264 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
265 return false;
267 if (dra == drb
268 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
269 return false;
271 /* Even if we have an anti-dependence then, as the vectorized loop covers at
272 least two scalar iterations, there is always also a true dependence.
273 As the vectorizer does not re-order loads and stores we can ignore
274 the anti-dependence if TBAA can disambiguate both DRs similar to the
275 case with known negative distance anti-dependences (positive
276 distance anti-dependences would violate TBAA constraints). */
277 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
278 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
279 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
280 get_alias_set (DR_REF (drb))))
281 return false;
283 /* Unknown data dependence. */
284 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
286 /* If user asserted safelen consecutive iterations can be
287 executed concurrently, assume independence. */
288 if (loop->safelen >= 2)
290 if (loop->safelen < *max_vf)
291 *max_vf = loop->safelen;
292 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
293 return false;
296 if (STMT_VINFO_GATHER_P (stmtinfo_a)
297 || STMT_VINFO_GATHER_P (stmtinfo_b))
299 if (dump_enabled_p ())
301 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
302 "versioning for alias not supported for: "
303 "can't determine dependence between ");
304 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
305 DR_REF (dra));
306 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
307 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
308 DR_REF (drb));
309 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
311 return true;
314 if (dump_enabled_p ())
316 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
317 "versioning for alias required: "
318 "can't determine dependence between ");
319 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
320 DR_REF (dra));
321 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
322 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
323 DR_REF (drb));
324 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
327 /* Add to list of ddrs that need to be tested at run-time. */
328 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
331 /* Known data dependence. */
332 if (DDR_NUM_DIST_VECTS (ddr) == 0)
334 /* If user asserted safelen consecutive iterations can be
335 executed concurrently, assume independence. */
336 if (loop->safelen >= 2)
338 if (loop->safelen < *max_vf)
339 *max_vf = loop->safelen;
340 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
341 return false;
344 if (STMT_VINFO_GATHER_P (stmtinfo_a)
345 || STMT_VINFO_GATHER_P (stmtinfo_b))
347 if (dump_enabled_p ())
349 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
350 "versioning for alias not supported for: "
351 "bad dist vector for ");
352 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
353 DR_REF (dra));
354 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
355 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
356 DR_REF (drb));
357 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
359 return true;
362 if (dump_enabled_p ())
364 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
365 "versioning for alias required: "
366 "bad dist vector for ");
367 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
368 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
369 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
370 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
372 /* Add to list of ddrs that need to be tested at run-time. */
373 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
376 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
377 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
379 int dist = dist_v[loop_depth];
381 if (dump_enabled_p ())
382 dump_printf_loc (MSG_NOTE, vect_location,
383 "dependence distance = %d.\n", dist);
385 if (dist == 0)
387 if (dump_enabled_p ())
389 dump_printf_loc (MSG_NOTE, vect_location,
390 "dependence distance == 0 between ");
391 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
392 dump_printf (MSG_NOTE, " and ");
393 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
394 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
397 /* When we perform grouped accesses and perform implicit CSE
398 by detecting equal accesses and doing disambiguation with
399 runtime alias tests like for
400 .. = a[i];
401 .. = a[i+1];
402 a[i] = ..;
403 a[i+1] = ..;
404 *p = ..;
405 .. = a[i];
406 .. = a[i+1];
407 where we will end up loading { a[i], a[i+1] } once, make
408 sure that inserting group loads before the first load and
409 stores after the last store will do the right thing.
410 Similar for groups like
411 a[i] = ...;
412 ... = a[i];
413 a[i+1] = ...;
414 where loads from the group interleave with the store. */
415 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
416 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
418 gimple earlier_stmt;
419 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
420 if (DR_IS_WRITE
421 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
423 if (dump_enabled_p ())
424 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
425 "READ_WRITE dependence in interleaving."
426 "\n");
427 return true;
431 continue;
434 if (dist > 0 && DDR_REVERSED_P (ddr))
436 /* If DDR_REVERSED_P the order of the data-refs in DDR was
437 reversed (to make distance vector positive), and the actual
438 distance is negative. */
439 if (dump_enabled_p ())
440 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
441 "dependence distance negative.\n");
442 /* Record a negative dependence distance to later limit the
443 amount of stmt copying / unrolling we can perform.
444 Only need to handle read-after-write dependence. */
445 if (DR_IS_READ (drb)
446 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
447 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
448 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
449 continue;
452 if (abs (dist) >= 2
453 && abs (dist) < *max_vf)
455 /* The dependence distance requires reduction of the maximal
456 vectorization factor. */
457 *max_vf = abs (dist);
458 if (dump_enabled_p ())
459 dump_printf_loc (MSG_NOTE, vect_location,
460 "adjusting maximal vectorization factor to %i\n",
461 *max_vf);
464 if (abs (dist) >= *max_vf)
466 /* Dependence distance does not create dependence, as far as
467 vectorization is concerned, in this case. */
468 if (dump_enabled_p ())
469 dump_printf_loc (MSG_NOTE, vect_location,
470 "dependence distance >= VF.\n");
471 continue;
474 if (dump_enabled_p ())
476 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
477 "not vectorized, possible dependence "
478 "between data-refs ");
479 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
480 dump_printf (MSG_NOTE, " and ");
481 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
482 dump_printf (MSG_NOTE, "\n");
485 return true;
488 return false;
491 /* Function vect_analyze_data_ref_dependences.
493 Examine all the data references in the loop, and make sure there do not
494 exist any data dependences between them. Set *MAX_VF according to
495 the maximum vectorization factor the data dependences allow. */
497 bool
498 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
500 unsigned int i;
501 struct data_dependence_relation *ddr;
503 if (dump_enabled_p ())
504 dump_printf_loc (MSG_NOTE, vect_location,
505 "=== vect_analyze_data_ref_dependences ===\n");
507 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
508 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
509 &LOOP_VINFO_DDRS (loop_vinfo),
510 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
511 return false;
513 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
514 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
515 return false;
517 return true;
521 /* Function vect_slp_analyze_data_ref_dependence.
523 Return TRUE if there (might) exist a dependence between a memory-reference
524 DRA and a memory-reference DRB. When versioning for alias may check a
525 dependence at run-time, return FALSE. Adjust *MAX_VF according to
526 the data dependence. */
528 static bool
529 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
531 struct data_reference *dra = DDR_A (ddr);
532 struct data_reference *drb = DDR_B (ddr);
534 /* We need to check dependences of statements marked as unvectorizable
535 as well, they still can prohibit vectorization. */
537 /* Independent data accesses. */
538 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
539 return false;
541 if (dra == drb)
542 return false;
544 /* Read-read is OK. */
545 if (DR_IS_READ (dra) && DR_IS_READ (drb))
546 return false;
548 /* If dra and drb are part of the same interleaving chain consider
549 them independent. */
550 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
551 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
552 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
553 return false;
555 /* Unknown data dependence. */
556 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
558 if (dump_enabled_p ())
560 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
561 "can't determine dependence between ");
562 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
563 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
564 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
565 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
568 else if (dump_enabled_p ())
570 dump_printf_loc (MSG_NOTE, vect_location,
571 "determined dependence between ");
572 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
573 dump_printf (MSG_NOTE, " and ");
574 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
575 dump_printf (MSG_NOTE, "\n");
578 /* We do not vectorize basic blocks with write-write dependencies. */
579 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
580 return true;
582 /* If we have a read-write dependence check that the load is before the store.
583 When we vectorize basic blocks, vector load can be only before
584 corresponding scalar load, and vector store can be only after its
585 corresponding scalar store. So the order of the acceses is preserved in
586 case the load is before the store. */
587 gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
588 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
590 /* That only holds for load-store pairs taking part in vectorization. */
591 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
592 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
593 return false;
596 return true;
600 /* Function vect_analyze_data_ref_dependences.
602 Examine all the data references in the basic-block, and make sure there
603 do not exist any data dependences between them. Set *MAX_VF according to
604 the maximum vectorization factor the data dependences allow. */
606 bool
607 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
609 struct data_dependence_relation *ddr;
610 unsigned int i;
612 if (dump_enabled_p ())
613 dump_printf_loc (MSG_NOTE, vect_location,
614 "=== vect_slp_analyze_data_ref_dependences ===\n");
616 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
617 &BB_VINFO_DDRS (bb_vinfo),
618 vNULL, true))
619 return false;
621 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
622 if (vect_slp_analyze_data_ref_dependence (ddr))
623 return false;
625 return true;
629 /* Function vect_compute_data_ref_alignment
631 Compute the misalignment of the data reference DR.
633 Output:
634 1. If during the misalignment computation it is found that the data reference
635 cannot be vectorized then false is returned.
636 2. DR_MISALIGNMENT (DR) is defined.
638 FOR NOW: No analysis is actually performed. Misalignment is calculated
639 only for trivial cases. TODO. */
641 static bool
642 vect_compute_data_ref_alignment (struct data_reference *dr)
644 gimple stmt = DR_STMT (dr);
645 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
646 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
647 struct loop *loop = NULL;
648 tree ref = DR_REF (dr);
649 tree vectype;
650 tree base, base_addr;
651 bool base_aligned;
652 tree misalign;
653 tree aligned_to, alignment;
655 if (dump_enabled_p ())
656 dump_printf_loc (MSG_NOTE, vect_location,
657 "vect_compute_data_ref_alignment:\n");
659 if (loop_vinfo)
660 loop = LOOP_VINFO_LOOP (loop_vinfo);
662 /* Initialize misalignment to unknown. */
663 SET_DR_MISALIGNMENT (dr, -1);
665 /* Strided loads perform only component accesses, misalignment information
666 is irrelevant for them. */
667 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
668 return true;
670 misalign = DR_INIT (dr);
671 aligned_to = DR_ALIGNED_TO (dr);
672 base_addr = DR_BASE_ADDRESS (dr);
673 vectype = STMT_VINFO_VECTYPE (stmt_info);
675 /* In case the dataref is in an inner-loop of the loop that is being
676 vectorized (LOOP), we use the base and misalignment information
677 relative to the outer-loop (LOOP). This is ok only if the misalignment
678 stays the same throughout the execution of the inner-loop, which is why
679 we have to check that the stride of the dataref in the inner-loop evenly
680 divides by the vector size. */
681 if (loop && nested_in_vect_loop_p (loop, stmt))
683 tree step = DR_STEP (dr);
684 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
686 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
688 if (dump_enabled_p ())
689 dump_printf_loc (MSG_NOTE, vect_location,
690 "inner step divides the vector-size.\n");
691 misalign = STMT_VINFO_DR_INIT (stmt_info);
692 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
693 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
695 else
697 if (dump_enabled_p ())
698 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
699 "inner step doesn't divide the vector-size.\n");
700 misalign = NULL_TREE;
704 /* Similarly, if we're doing basic-block vectorization, we can only use
705 base and misalignment information relative to an innermost loop if the
706 misalignment stays the same throughout the execution of the loop.
707 As above, this is the case if the stride of the dataref evenly divides
708 by the vector size. */
709 if (!loop)
711 tree step = DR_STEP (dr);
712 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
714 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
716 if (dump_enabled_p ())
717 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
718 "SLP: step doesn't divide the vector-size.\n");
719 misalign = NULL_TREE;
723 base = build_fold_indirect_ref (base_addr);
724 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
726 if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
727 || !misalign)
729 if (dump_enabled_p ())
731 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
732 "Unknown alignment for access: ");
733 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, base);
734 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
736 return true;
739 if ((DECL_P (base)
740 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
741 alignment) >= 0)
742 || (TREE_CODE (base_addr) == SSA_NAME
743 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
744 TREE_TYPE (base_addr)))),
745 alignment) >= 0)
746 || (get_pointer_alignment (base_addr) >= TYPE_ALIGN (vectype)))
747 base_aligned = true;
748 else
749 base_aligned = false;
751 if (!base_aligned)
753 /* Do not change the alignment of global variables here if
754 flag_section_anchors is enabled as we already generated
755 RTL for other functions. Most global variables should
756 have been aligned during the IPA increase_alignment pass. */
757 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
758 || (TREE_STATIC (base) && flag_section_anchors))
760 if (dump_enabled_p ())
762 dump_printf_loc (MSG_NOTE, vect_location,
763 "can't force alignment of ref: ");
764 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
765 dump_printf (MSG_NOTE, "\n");
767 return true;
770 /* Force the alignment of the decl.
771 NOTE: This is the only change to the code we make during
772 the analysis phase, before deciding to vectorize the loop. */
773 if (dump_enabled_p ())
775 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
776 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
777 dump_printf (MSG_NOTE, "\n");
780 ((dataref_aux *)dr->aux)->base_decl = base;
781 ((dataref_aux *)dr->aux)->base_misaligned = true;
784 /* If this is a backward running DR then first access in the larger
785 vectype actually is N-1 elements before the address in the DR.
786 Adjust misalign accordingly. */
787 if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
789 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
790 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
791 otherwise we wouldn't be here. */
792 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
793 /* PLUS because DR_STEP was negative. */
794 misalign = size_binop (PLUS_EXPR, misalign, offset);
797 /* Modulo alignment. */
798 misalign = size_binop (FLOOR_MOD_EXPR, misalign, alignment);
800 if (!tree_fits_uhwi_p (misalign))
802 /* Negative or overflowed misalignment value. */
803 if (dump_enabled_p ())
804 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
805 "unexpected misalign value\n");
806 return false;
809 SET_DR_MISALIGNMENT (dr, tree_to_uhwi (misalign));
811 if (dump_enabled_p ())
813 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
814 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
815 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
816 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
819 return true;
823 /* Function vect_compute_data_refs_alignment
825 Compute the misalignment of data references in the loop.
826 Return FALSE if a data reference is found that cannot be vectorized. */
828 static bool
829 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
830 bb_vec_info bb_vinfo)
832 vec<data_reference_p> datarefs;
833 struct data_reference *dr;
834 unsigned int i;
836 if (loop_vinfo)
837 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
838 else
839 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
841 FOR_EACH_VEC_ELT (datarefs, i, dr)
842 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
843 && !vect_compute_data_ref_alignment (dr))
845 if (bb_vinfo)
847 /* Mark unsupported statement as unvectorizable. */
848 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
849 continue;
851 else
852 return false;
855 return true;
859 /* Function vect_update_misalignment_for_peel
861 DR - the data reference whose misalignment is to be adjusted.
862 DR_PEEL - the data reference whose misalignment is being made
863 zero in the vector loop by the peel.
864 NPEEL - the number of iterations in the peel loop if the misalignment
865 of DR_PEEL is known at compile time. */
867 static void
868 vect_update_misalignment_for_peel (struct data_reference *dr,
869 struct data_reference *dr_peel, int npeel)
871 unsigned int i;
872 vec<dr_p> same_align_drs;
873 struct data_reference *current_dr;
874 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
875 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
876 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
877 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
879 /* For interleaved data accesses the step in the loop must be multiplied by
880 the size of the interleaving group. */
881 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
882 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
883 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
884 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
886 /* It can be assumed that the data refs with the same alignment as dr_peel
887 are aligned in the vector loop. */
888 same_align_drs
889 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
890 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
892 if (current_dr != dr)
893 continue;
894 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
895 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
896 SET_DR_MISALIGNMENT (dr, 0);
897 return;
900 if (known_alignment_for_access_p (dr)
901 && known_alignment_for_access_p (dr_peel))
903 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
904 int misal = DR_MISALIGNMENT (dr);
905 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
906 misal += negative ? -npeel * dr_size : npeel * dr_size;
907 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
908 SET_DR_MISALIGNMENT (dr, misal);
909 return;
912 if (dump_enabled_p ())
913 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
914 SET_DR_MISALIGNMENT (dr, -1);
918 /* Function vect_verify_datarefs_alignment
920 Return TRUE if all data references in the loop can be
921 handled with respect to alignment. */
923 bool
924 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
926 vec<data_reference_p> datarefs;
927 struct data_reference *dr;
928 enum dr_alignment_support supportable_dr_alignment;
929 unsigned int i;
931 if (loop_vinfo)
932 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
933 else
934 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
936 FOR_EACH_VEC_ELT (datarefs, i, dr)
938 gimple stmt = DR_STMT (dr);
939 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
941 if (!STMT_VINFO_RELEVANT_P (stmt_info))
942 continue;
944 /* For interleaving, only the alignment of the first access matters.
945 Skip statements marked as not vectorizable. */
946 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
947 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
948 || !STMT_VINFO_VECTORIZABLE (stmt_info))
949 continue;
951 /* Strided loads perform only component accesses, alignment is
952 irrelevant for them. */
953 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
954 continue;
956 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
957 if (!supportable_dr_alignment)
959 if (dump_enabled_p ())
961 if (DR_IS_READ (dr))
962 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
963 "not vectorized: unsupported unaligned load.");
964 else
965 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
966 "not vectorized: unsupported unaligned "
967 "store.");
969 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
970 DR_REF (dr));
971 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
973 return false;
975 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
976 dump_printf_loc (MSG_NOTE, vect_location,
977 "Vectorizing an unaligned access.\n");
979 return true;
982 /* Given an memory reference EXP return whether its alignment is less
983 than its size. */
985 static bool
986 not_size_aligned (tree exp)
988 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
989 return true;
991 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
992 > get_object_alignment (exp));
995 /* Function vector_alignment_reachable_p
997 Return true if vector alignment for DR is reachable by peeling
998 a few loop iterations. Return false otherwise. */
1000 static bool
1001 vector_alignment_reachable_p (struct data_reference *dr)
1003 gimple stmt = DR_STMT (dr);
1004 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1005 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1007 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1009 /* For interleaved access we peel only if number of iterations in
1010 the prolog loop ({VF - misalignment}), is a multiple of the
1011 number of the interleaved accesses. */
1012 int elem_size, mis_in_elements;
1013 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1015 /* FORNOW: handle only known alignment. */
1016 if (!known_alignment_for_access_p (dr))
1017 return false;
1019 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
1020 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1022 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
1023 return false;
1026 /* If misalignment is known at the compile time then allow peeling
1027 only if natural alignment is reachable through peeling. */
1028 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1030 HOST_WIDE_INT elmsize =
1031 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1032 if (dump_enabled_p ())
1034 dump_printf_loc (MSG_NOTE, vect_location,
1035 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1036 dump_printf (MSG_NOTE,
1037 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1039 if (DR_MISALIGNMENT (dr) % elmsize)
1041 if (dump_enabled_p ())
1042 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1043 "data size does not divide the misalignment.\n");
1044 return false;
1048 if (!known_alignment_for_access_p (dr))
1050 tree type = TREE_TYPE (DR_REF (dr));
1051 bool is_packed = not_size_aligned (DR_REF (dr));
1052 if (dump_enabled_p ())
1053 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1054 "Unknown misalignment, is_packed = %d\n",is_packed);
1055 if ((TYPE_USER_ALIGN (type) && !is_packed)
1056 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1057 return true;
1058 else
1059 return false;
1062 return true;
1066 /* Calculate the cost of the memory access represented by DR. */
1068 static void
1069 vect_get_data_access_cost (struct data_reference *dr,
1070 unsigned int *inside_cost,
1071 unsigned int *outside_cost,
1072 stmt_vector_for_cost *body_cost_vec)
1074 gimple stmt = DR_STMT (dr);
1075 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1076 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1077 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1078 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1079 int ncopies = vf / nunits;
1081 if (DR_IS_READ (dr))
1082 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1083 NULL, body_cost_vec, false);
1084 else
1085 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1087 if (dump_enabled_p ())
1088 dump_printf_loc (MSG_NOTE, vect_location,
1089 "vect_get_data_access_cost: inside_cost = %d, "
1090 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1094 /* Insert DR into peeling hash table with NPEEL as key. */
1096 static void
1097 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1098 int npeel)
1100 struct _vect_peel_info elem, *slot;
1101 _vect_peel_info **new_slot;
1102 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1104 elem.npeel = npeel;
1105 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find (&elem);
1106 if (slot)
1107 slot->count++;
1108 else
1110 slot = XNEW (struct _vect_peel_info);
1111 slot->npeel = npeel;
1112 slot->dr = dr;
1113 slot->count = 1;
1114 new_slot
1115 = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find_slot (slot, INSERT);
1116 *new_slot = slot;
1119 if (!supportable_dr_alignment
1120 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1121 slot->count += VECT_MAX_COST;
1125 /* Traverse peeling hash table to find peeling option that aligns maximum
1126 number of data accesses. */
1129 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1130 _vect_peel_extended_info *max)
1132 vect_peel_info elem = *slot;
1134 if (elem->count > max->peel_info.count
1135 || (elem->count == max->peel_info.count
1136 && max->peel_info.npeel > elem->npeel))
1138 max->peel_info.npeel = elem->npeel;
1139 max->peel_info.count = elem->count;
1140 max->peel_info.dr = elem->dr;
1143 return 1;
1147 /* Traverse peeling hash table and calculate cost for each peeling option.
1148 Find the one with the lowest cost. */
1151 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1152 _vect_peel_extended_info *min)
1154 vect_peel_info elem = *slot;
1155 int save_misalignment, dummy;
1156 unsigned int inside_cost = 0, outside_cost = 0, i;
1157 gimple stmt = DR_STMT (elem->dr);
1158 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1159 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1160 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1161 struct data_reference *dr;
1162 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1163 int single_iter_cost;
1165 prologue_cost_vec.create (2);
1166 body_cost_vec.create (2);
1167 epilogue_cost_vec.create (2);
1169 FOR_EACH_VEC_ELT (datarefs, i, dr)
1171 stmt = DR_STMT (dr);
1172 stmt_info = vinfo_for_stmt (stmt);
1173 /* For interleaving, only the alignment of the first access
1174 matters. */
1175 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1176 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1177 continue;
1179 save_misalignment = DR_MISALIGNMENT (dr);
1180 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1181 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1182 &body_cost_vec);
1183 SET_DR_MISALIGNMENT (dr, save_misalignment);
1186 single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
1187 outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel,
1188 &dummy, single_iter_cost,
1189 &prologue_cost_vec,
1190 &epilogue_cost_vec);
1192 /* Prologue and epilogue costs are added to the target model later.
1193 These costs depend only on the scalar iteration cost, the
1194 number of peeling iterations finally chosen, and the number of
1195 misaligned statements. So discard the information found here. */
1196 prologue_cost_vec.release ();
1197 epilogue_cost_vec.release ();
1199 if (inside_cost < min->inside_cost
1200 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1202 min->inside_cost = inside_cost;
1203 min->outside_cost = outside_cost;
1204 min->body_cost_vec.release ();
1205 min->body_cost_vec = body_cost_vec;
1206 min->peel_info.dr = elem->dr;
1207 min->peel_info.npeel = elem->npeel;
1209 else
1210 body_cost_vec.release ();
1212 return 1;
1216 /* Choose best peeling option by traversing peeling hash table and either
1217 choosing an option with the lowest cost (if cost model is enabled) or the
1218 option that aligns as many accesses as possible. */
1220 static struct data_reference *
1221 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1222 unsigned int *npeel,
1223 stmt_vector_for_cost *body_cost_vec)
1225 struct _vect_peel_extended_info res;
1227 res.peel_info.dr = NULL;
1228 res.body_cost_vec = stmt_vector_for_cost ();
1230 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1232 res.inside_cost = INT_MAX;
1233 res.outside_cost = INT_MAX;
1234 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1235 ->traverse <_vect_peel_extended_info *,
1236 vect_peeling_hash_get_lowest_cost> (&res);
1238 else
1240 res.peel_info.count = 0;
1241 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1242 ->traverse <_vect_peel_extended_info *,
1243 vect_peeling_hash_get_most_frequent> (&res);
1246 *npeel = res.peel_info.npeel;
1247 *body_cost_vec = res.body_cost_vec;
1248 return res.peel_info.dr;
1252 /* Function vect_enhance_data_refs_alignment
1254 This pass will use loop versioning and loop peeling in order to enhance
1255 the alignment of data references in the loop.
1257 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1258 original loop is to be vectorized. Any other loops that are created by
1259 the transformations performed in this pass - are not supposed to be
1260 vectorized. This restriction will be relaxed.
1262 This pass will require a cost model to guide it whether to apply peeling
1263 or versioning or a combination of the two. For example, the scheme that
1264 intel uses when given a loop with several memory accesses, is as follows:
1265 choose one memory access ('p') which alignment you want to force by doing
1266 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1267 other accesses are not necessarily aligned, or (2) use loop versioning to
1268 generate one loop in which all accesses are aligned, and another loop in
1269 which only 'p' is necessarily aligned.
1271 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1272 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1273 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1275 Devising a cost model is the most critical aspect of this work. It will
1276 guide us on which access to peel for, whether to use loop versioning, how
1277 many versions to create, etc. The cost model will probably consist of
1278 generic considerations as well as target specific considerations (on
1279 powerpc for example, misaligned stores are more painful than misaligned
1280 loads).
1282 Here are the general steps involved in alignment enhancements:
1284 -- original loop, before alignment analysis:
1285 for (i=0; i<N; i++){
1286 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1287 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1290 -- After vect_compute_data_refs_alignment:
1291 for (i=0; i<N; i++){
1292 x = q[i]; # DR_MISALIGNMENT(q) = 3
1293 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1296 -- Possibility 1: we do loop versioning:
1297 if (p is aligned) {
1298 for (i=0; i<N; i++){ # loop 1A
1299 x = q[i]; # DR_MISALIGNMENT(q) = 3
1300 p[i] = y; # DR_MISALIGNMENT(p) = 0
1303 else {
1304 for (i=0; i<N; i++){ # loop 1B
1305 x = q[i]; # DR_MISALIGNMENT(q) = 3
1306 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1310 -- Possibility 2: we do loop peeling:
1311 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1312 x = q[i];
1313 p[i] = y;
1315 for (i = 3; i < N; i++){ # loop 2A
1316 x = q[i]; # DR_MISALIGNMENT(q) = 0
1317 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1320 -- Possibility 3: combination of loop peeling and versioning:
1321 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1322 x = q[i];
1323 p[i] = y;
1325 if (p is aligned) {
1326 for (i = 3; i<N; i++){ # loop 3A
1327 x = q[i]; # DR_MISALIGNMENT(q) = 0
1328 p[i] = y; # DR_MISALIGNMENT(p) = 0
1331 else {
1332 for (i = 3; i<N; i++){ # loop 3B
1333 x = q[i]; # DR_MISALIGNMENT(q) = 0
1334 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1338 These loops are later passed to loop_transform to be vectorized. The
1339 vectorizer will use the alignment information to guide the transformation
1340 (whether to generate regular loads/stores, or with special handling for
1341 misalignment). */
1343 bool
1344 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1346 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1347 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1348 enum dr_alignment_support supportable_dr_alignment;
1349 struct data_reference *dr0 = NULL, *first_store = NULL;
1350 struct data_reference *dr;
1351 unsigned int i, j;
1352 bool do_peeling = false;
1353 bool do_versioning = false;
1354 bool stat;
1355 gimple stmt;
1356 stmt_vec_info stmt_info;
1357 unsigned int npeel = 0;
1358 bool all_misalignments_unknown = true;
1359 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1360 unsigned possible_npeel_number = 1;
1361 tree vectype;
1362 unsigned int nelements, mis, same_align_drs_max = 0;
1363 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1365 if (dump_enabled_p ())
1366 dump_printf_loc (MSG_NOTE, vect_location,
1367 "=== vect_enhance_data_refs_alignment ===\n");
1369 /* While cost model enhancements are expected in the future, the high level
1370 view of the code at this time is as follows:
1372 A) If there is a misaligned access then see if peeling to align
1373 this access can make all data references satisfy
1374 vect_supportable_dr_alignment. If so, update data structures
1375 as needed and return true.
1377 B) If peeling wasn't possible and there is a data reference with an
1378 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1379 then see if loop versioning checks can be used to make all data
1380 references satisfy vect_supportable_dr_alignment. If so, update
1381 data structures as needed and return true.
1383 C) If neither peeling nor versioning were successful then return false if
1384 any data reference does not satisfy vect_supportable_dr_alignment.
1386 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1388 Note, Possibility 3 above (which is peeling and versioning together) is not
1389 being done at this time. */
1391 /* (1) Peeling to force alignment. */
1393 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1394 Considerations:
1395 + How many accesses will become aligned due to the peeling
1396 - How many accesses will become unaligned due to the peeling,
1397 and the cost of misaligned accesses.
1398 - The cost of peeling (the extra runtime checks, the increase
1399 in code size). */
1401 FOR_EACH_VEC_ELT (datarefs, i, dr)
1403 stmt = DR_STMT (dr);
1404 stmt_info = vinfo_for_stmt (stmt);
1406 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1407 continue;
1409 /* For interleaving, only the alignment of the first access
1410 matters. */
1411 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1412 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1413 continue;
1415 /* For invariant accesses there is nothing to enhance. */
1416 if (integer_zerop (DR_STEP (dr)))
1417 continue;
1419 /* Strided loads perform only component accesses, alignment is
1420 irrelevant for them. */
1421 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1422 continue;
1424 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1425 do_peeling = vector_alignment_reachable_p (dr);
1426 if (do_peeling)
1428 if (known_alignment_for_access_p (dr))
1430 unsigned int npeel_tmp;
1431 bool negative = tree_int_cst_compare (DR_STEP (dr),
1432 size_zero_node) < 0;
1434 /* Save info about DR in the hash table. */
1435 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo))
1436 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1437 = new hash_table<peel_info_hasher> (1);
1439 vectype = STMT_VINFO_VECTYPE (stmt_info);
1440 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1441 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1442 TREE_TYPE (DR_REF (dr))));
1443 npeel_tmp = (negative
1444 ? (mis - nelements) : (nelements - mis))
1445 & (nelements - 1);
1447 /* For multiple types, it is possible that the bigger type access
1448 will have more than one peeling option. E.g., a loop with two
1449 types: one of size (vector size / 4), and the other one of
1450 size (vector size / 8). Vectorization factor will 8. If both
1451 access are misaligned by 3, the first one needs one scalar
1452 iteration to be aligned, and the second one needs 5. But the
1453 the first one will be aligned also by peeling 5 scalar
1454 iterations, and in that case both accesses will be aligned.
1455 Hence, except for the immediate peeling amount, we also want
1456 to try to add full vector size, while we don't exceed
1457 vectorization factor.
1458 We do this automtically for cost model, since we calculate cost
1459 for every peeling option. */
1460 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1461 possible_npeel_number = vf /nelements;
1463 /* Handle the aligned case. We may decide to align some other
1464 access, making DR unaligned. */
1465 if (DR_MISALIGNMENT (dr) == 0)
1467 npeel_tmp = 0;
1468 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1469 possible_npeel_number++;
1472 for (j = 0; j < possible_npeel_number; j++)
1474 gcc_assert (npeel_tmp <= vf);
1475 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1476 npeel_tmp += nelements;
1479 all_misalignments_unknown = false;
1480 /* Data-ref that was chosen for the case that all the
1481 misalignments are unknown is not relevant anymore, since we
1482 have a data-ref with known alignment. */
1483 dr0 = NULL;
1485 else
1487 /* If we don't know any misalignment values, we prefer
1488 peeling for data-ref that has the maximum number of data-refs
1489 with the same alignment, unless the target prefers to align
1490 stores over load. */
1491 if (all_misalignments_unknown)
1493 unsigned same_align_drs
1494 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1495 if (!dr0
1496 || same_align_drs_max < same_align_drs)
1498 same_align_drs_max = same_align_drs;
1499 dr0 = dr;
1501 /* For data-refs with the same number of related
1502 accesses prefer the one where the misalign
1503 computation will be invariant in the outermost loop. */
1504 else if (same_align_drs_max == same_align_drs)
1506 struct loop *ivloop0, *ivloop;
1507 ivloop0 = outermost_invariant_loop_for_expr
1508 (loop, DR_BASE_ADDRESS (dr0));
1509 ivloop = outermost_invariant_loop_for_expr
1510 (loop, DR_BASE_ADDRESS (dr));
1511 if ((ivloop && !ivloop0)
1512 || (ivloop && ivloop0
1513 && flow_loop_nested_p (ivloop, ivloop0)))
1514 dr0 = dr;
1517 if (!first_store && DR_IS_WRITE (dr))
1518 first_store = dr;
1521 /* If there are both known and unknown misaligned accesses in the
1522 loop, we choose peeling amount according to the known
1523 accesses. */
1524 if (!supportable_dr_alignment)
1526 dr0 = dr;
1527 if (!first_store && DR_IS_WRITE (dr))
1528 first_store = dr;
1532 else
1534 if (!aligned_access_p (dr))
1536 if (dump_enabled_p ())
1537 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1538 "vector alignment may not be reachable\n");
1539 break;
1544 /* Check if we can possibly peel the loop. */
1545 if (!vect_can_advance_ivs_p (loop_vinfo)
1546 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1547 do_peeling = false;
1549 /* If we don't know how many times the peeling loop will run
1550 assume it will run VF-1 times and disable peeling if the remaining
1551 iters are less than the vectorization factor. */
1552 if (do_peeling
1553 && all_misalignments_unknown
1554 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1555 && (LOOP_VINFO_INT_NITERS (loop_vinfo)
1556 < 2 * (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1))
1557 do_peeling = false;
1559 if (do_peeling
1560 && all_misalignments_unknown
1561 && vect_supportable_dr_alignment (dr0, false))
1563 /* Check if the target requires to prefer stores over loads, i.e., if
1564 misaligned stores are more expensive than misaligned loads (taking
1565 drs with same alignment into account). */
1566 if (first_store && DR_IS_READ (dr0))
1568 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1569 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1570 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1571 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1572 stmt_vector_for_cost dummy;
1573 dummy.create (2);
1575 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1576 &dummy);
1577 vect_get_data_access_cost (first_store, &store_inside_cost,
1578 &store_outside_cost, &dummy);
1580 dummy.release ();
1582 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1583 aligning the load DR0). */
1584 load_inside_penalty = store_inside_cost;
1585 load_outside_penalty = store_outside_cost;
1586 for (i = 0;
1587 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1588 DR_STMT (first_store))).iterate (i, &dr);
1589 i++)
1590 if (DR_IS_READ (dr))
1592 load_inside_penalty += load_inside_cost;
1593 load_outside_penalty += load_outside_cost;
1595 else
1597 load_inside_penalty += store_inside_cost;
1598 load_outside_penalty += store_outside_cost;
1601 /* Calculate the penalty for leaving DR0 unaligned (by
1602 aligning the FIRST_STORE). */
1603 store_inside_penalty = load_inside_cost;
1604 store_outside_penalty = load_outside_cost;
1605 for (i = 0;
1606 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1607 DR_STMT (dr0))).iterate (i, &dr);
1608 i++)
1609 if (DR_IS_READ (dr))
1611 store_inside_penalty += load_inside_cost;
1612 store_outside_penalty += load_outside_cost;
1614 else
1616 store_inside_penalty += store_inside_cost;
1617 store_outside_penalty += store_outside_cost;
1620 if (load_inside_penalty > store_inside_penalty
1621 || (load_inside_penalty == store_inside_penalty
1622 && load_outside_penalty > store_outside_penalty))
1623 dr0 = first_store;
1626 /* In case there are only loads with different unknown misalignments, use
1627 peeling only if it may help to align other accesses in the loop. */
1628 if (!first_store
1629 && !STMT_VINFO_SAME_ALIGN_REFS (
1630 vinfo_for_stmt (DR_STMT (dr0))).length ()
1631 && vect_supportable_dr_alignment (dr0, false)
1632 != dr_unaligned_supported)
1633 do_peeling = false;
1636 if (do_peeling && !dr0)
1638 /* Peeling is possible, but there is no data access that is not supported
1639 unless aligned. So we try to choose the best possible peeling. */
1641 /* We should get here only if there are drs with known misalignment. */
1642 gcc_assert (!all_misalignments_unknown);
1644 /* Choose the best peeling from the hash table. */
1645 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1646 &body_cost_vec);
1647 if (!dr0 || !npeel)
1648 do_peeling = false;
1650 /* If peeling by npeel will result in a remaining loop not iterating
1651 enough to be vectorized then do not peel. */
1652 if (do_peeling
1653 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1654 && (LOOP_VINFO_INT_NITERS (loop_vinfo)
1655 < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + npeel))
1656 do_peeling = false;
1659 if (do_peeling)
1661 stmt = DR_STMT (dr0);
1662 stmt_info = vinfo_for_stmt (stmt);
1663 vectype = STMT_VINFO_VECTYPE (stmt_info);
1664 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1666 if (known_alignment_for_access_p (dr0))
1668 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1669 size_zero_node) < 0;
1670 if (!npeel)
1672 /* Since it's known at compile time, compute the number of
1673 iterations in the peeled loop (the peeling factor) for use in
1674 updating DR_MISALIGNMENT values. The peeling factor is the
1675 vectorization factor minus the misalignment as an element
1676 count. */
1677 mis = DR_MISALIGNMENT (dr0);
1678 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1679 npeel = ((negative ? mis - nelements : nelements - mis)
1680 & (nelements - 1));
1683 /* For interleaved data access every iteration accesses all the
1684 members of the group, therefore we divide the number of iterations
1685 by the group size. */
1686 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1687 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1688 npeel /= GROUP_SIZE (stmt_info);
1690 if (dump_enabled_p ())
1691 dump_printf_loc (MSG_NOTE, vect_location,
1692 "Try peeling by %d\n", npeel);
1695 /* Ensure that all data refs can be vectorized after the peel. */
1696 FOR_EACH_VEC_ELT (datarefs, i, dr)
1698 int save_misalignment;
1700 if (dr == dr0)
1701 continue;
1703 stmt = DR_STMT (dr);
1704 stmt_info = vinfo_for_stmt (stmt);
1705 /* For interleaving, only the alignment of the first access
1706 matters. */
1707 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1708 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1709 continue;
1711 /* Strided loads perform only component accesses, alignment is
1712 irrelevant for them. */
1713 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1714 continue;
1716 save_misalignment = DR_MISALIGNMENT (dr);
1717 vect_update_misalignment_for_peel (dr, dr0, npeel);
1718 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1719 SET_DR_MISALIGNMENT (dr, save_misalignment);
1721 if (!supportable_dr_alignment)
1723 do_peeling = false;
1724 break;
1728 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1730 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1731 if (!stat)
1732 do_peeling = false;
1733 else
1735 body_cost_vec.release ();
1736 return stat;
1740 if (do_peeling)
1742 unsigned max_allowed_peel
1743 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1744 if (max_allowed_peel != (unsigned)-1)
1746 unsigned max_peel = npeel;
1747 if (max_peel == 0)
1749 gimple dr_stmt = DR_STMT (dr0);
1750 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1751 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1752 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1754 if (max_peel > max_allowed_peel)
1756 do_peeling = false;
1757 if (dump_enabled_p ())
1758 dump_printf_loc (MSG_NOTE, vect_location,
1759 "Disable peeling, max peels reached: %d\n", max_peel);
1764 if (do_peeling)
1766 stmt_info_for_cost *si;
1767 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
1769 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1770 If the misalignment of DR_i is identical to that of dr0 then set
1771 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1772 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1773 by the peeling factor times the element size of DR_i (MOD the
1774 vectorization factor times the size). Otherwise, the
1775 misalignment of DR_i must be set to unknown. */
1776 FOR_EACH_VEC_ELT (datarefs, i, dr)
1777 if (dr != dr0)
1778 vect_update_misalignment_for_peel (dr, dr0, npeel);
1780 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1781 if (npeel)
1782 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1783 else
1784 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1785 = DR_MISALIGNMENT (dr0);
1786 SET_DR_MISALIGNMENT (dr0, 0);
1787 if (dump_enabled_p ())
1789 dump_printf_loc (MSG_NOTE, vect_location,
1790 "Alignment of access forced using peeling.\n");
1791 dump_printf_loc (MSG_NOTE, vect_location,
1792 "Peeling for alignment will be applied.\n");
1794 /* We've delayed passing the inside-loop peeling costs to the
1795 target cost model until we were sure peeling would happen.
1796 Do so now. */
1797 if (body_cost_vec.exists ())
1799 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1801 struct _stmt_vec_info *stmt_info
1802 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1803 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1804 si->misalign, vect_body);
1806 body_cost_vec.release ();
1809 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1810 gcc_assert (stat);
1811 return stat;
1815 body_cost_vec.release ();
1817 /* (2) Versioning to force alignment. */
1819 /* Try versioning if:
1820 1) optimize loop for speed
1821 2) there is at least one unsupported misaligned data ref with an unknown
1822 misalignment, and
1823 3) all misaligned data refs with a known misalignment are supported, and
1824 4) the number of runtime alignment checks is within reason. */
1826 do_versioning =
1827 optimize_loop_nest_for_speed_p (loop)
1828 && (!loop->inner); /* FORNOW */
1830 if (do_versioning)
1832 FOR_EACH_VEC_ELT (datarefs, i, dr)
1834 stmt = DR_STMT (dr);
1835 stmt_info = vinfo_for_stmt (stmt);
1837 /* For interleaving, only the alignment of the first access
1838 matters. */
1839 if (aligned_access_p (dr)
1840 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1841 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1842 continue;
1844 /* Strided loads perform only component accesses, alignment is
1845 irrelevant for them. */
1846 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1847 continue;
1849 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1851 if (!supportable_dr_alignment)
1853 gimple stmt;
1854 int mask;
1855 tree vectype;
1857 if (known_alignment_for_access_p (dr)
1858 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1859 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1861 do_versioning = false;
1862 break;
1865 stmt = DR_STMT (dr);
1866 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1867 gcc_assert (vectype);
1869 /* The rightmost bits of an aligned address must be zeros.
1870 Construct the mask needed for this test. For example,
1871 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1872 mask must be 15 = 0xf. */
1873 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1875 /* FORNOW: use the same mask to test all potentially unaligned
1876 references in the loop. The vectorizer currently supports
1877 a single vector size, see the reference to
1878 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1879 vectorization factor is computed. */
1880 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1881 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1882 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1883 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1884 DR_STMT (dr));
1888 /* Versioning requires at least one misaligned data reference. */
1889 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1890 do_versioning = false;
1891 else if (!do_versioning)
1892 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1895 if (do_versioning)
1897 vec<gimple> may_misalign_stmts
1898 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1899 gimple stmt;
1901 /* It can now be assumed that the data references in the statements
1902 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1903 of the loop being vectorized. */
1904 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1906 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1907 dr = STMT_VINFO_DATA_REF (stmt_info);
1908 SET_DR_MISALIGNMENT (dr, 0);
1909 if (dump_enabled_p ())
1910 dump_printf_loc (MSG_NOTE, vect_location,
1911 "Alignment of access forced using versioning.\n");
1914 if (dump_enabled_p ())
1915 dump_printf_loc (MSG_NOTE, vect_location,
1916 "Versioning for alignment will be applied.\n");
1918 /* Peeling and versioning can't be done together at this time. */
1919 gcc_assert (! (do_peeling && do_versioning));
1921 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1922 gcc_assert (stat);
1923 return stat;
1926 /* This point is reached if neither peeling nor versioning is being done. */
1927 gcc_assert (! (do_peeling || do_versioning));
1929 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1930 return stat;
1934 /* Function vect_find_same_alignment_drs.
1936 Update group and alignment relations according to the chosen
1937 vectorization factor. */
1939 static void
1940 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1941 loop_vec_info loop_vinfo)
1943 unsigned int i;
1944 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1945 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1946 struct data_reference *dra = DDR_A (ddr);
1947 struct data_reference *drb = DDR_B (ddr);
1948 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1949 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1950 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1951 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1952 lambda_vector dist_v;
1953 unsigned int loop_depth;
1955 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1956 return;
1958 if (dra == drb)
1959 return;
1961 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1962 return;
1964 /* Loop-based vectorization and known data dependence. */
1965 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1966 return;
1968 /* Data-dependence analysis reports a distance vector of zero
1969 for data-references that overlap only in the first iteration
1970 but have different sign step (see PR45764).
1971 So as a sanity check require equal DR_STEP. */
1972 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1973 return;
1975 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1976 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1978 int dist = dist_v[loop_depth];
1980 if (dump_enabled_p ())
1981 dump_printf_loc (MSG_NOTE, vect_location,
1982 "dependence distance = %d.\n", dist);
1984 /* Same loop iteration. */
1985 if (dist == 0
1986 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1988 /* Two references with distance zero have the same alignment. */
1989 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1990 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1991 if (dump_enabled_p ())
1993 dump_printf_loc (MSG_NOTE, vect_location,
1994 "accesses have the same alignment.\n");
1995 dump_printf (MSG_NOTE,
1996 "dependence distance modulo vf == 0 between ");
1997 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1998 dump_printf (MSG_NOTE, " and ");
1999 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2000 dump_printf (MSG_NOTE, "\n");
2007 /* Function vect_analyze_data_refs_alignment
2009 Analyze the alignment of the data-references in the loop.
2010 Return FALSE if a data reference is found that cannot be vectorized. */
2012 bool
2013 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
2014 bb_vec_info bb_vinfo)
2016 if (dump_enabled_p ())
2017 dump_printf_loc (MSG_NOTE, vect_location,
2018 "=== vect_analyze_data_refs_alignment ===\n");
2020 /* Mark groups of data references with same alignment using
2021 data dependence information. */
2022 if (loop_vinfo)
2024 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
2025 struct data_dependence_relation *ddr;
2026 unsigned int i;
2028 FOR_EACH_VEC_ELT (ddrs, i, ddr)
2029 vect_find_same_alignment_drs (ddr, loop_vinfo);
2032 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
2034 if (dump_enabled_p ())
2035 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2036 "not vectorized: can't calculate alignment "
2037 "for data ref.\n");
2038 return false;
2041 return true;
2045 /* Analyze groups of accesses: check that DR belongs to a group of
2046 accesses of legal size, step, etc. Detect gaps, single element
2047 interleaving, and other special cases. Set grouped access info.
2048 Collect groups of strided stores for further use in SLP analysis. */
2050 static bool
2051 vect_analyze_group_access (struct data_reference *dr)
2053 tree step = DR_STEP (dr);
2054 tree scalar_type = TREE_TYPE (DR_REF (dr));
2055 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2056 gimple stmt = DR_STMT (dr);
2057 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2058 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2059 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2060 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2061 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2062 bool slp_impossible = false;
2063 struct loop *loop = NULL;
2065 if (loop_vinfo)
2066 loop = LOOP_VINFO_LOOP (loop_vinfo);
2068 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2069 size of the interleaving group (including gaps). */
2070 groupsize = absu_hwi (dr_step) / type_size;
2072 /* Not consecutive access is possible only if it is a part of interleaving. */
2073 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2075 /* Check if it this DR is a part of interleaving, and is a single
2076 element of the group that is accessed in the loop. */
2078 /* Gaps are supported only for loads. STEP must be a multiple of the type
2079 size. The size of the group must be a power of 2. */
2080 if (DR_IS_READ (dr)
2081 && (dr_step % type_size) == 0
2082 && groupsize > 0
2083 && exact_log2 (groupsize) != -1)
2085 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2086 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2087 if (dump_enabled_p ())
2089 dump_printf_loc (MSG_NOTE, vect_location,
2090 "Detected single element interleaving ");
2091 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2092 dump_printf (MSG_NOTE, " step ");
2093 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2094 dump_printf (MSG_NOTE, "\n");
2097 if (loop_vinfo)
2099 if (dump_enabled_p ())
2100 dump_printf_loc (MSG_NOTE, vect_location,
2101 "Data access with gaps requires scalar "
2102 "epilogue loop\n");
2103 if (loop->inner)
2105 if (dump_enabled_p ())
2106 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2107 "Peeling for outer loop is not"
2108 " supported\n");
2109 return false;
2112 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2115 return true;
2118 if (dump_enabled_p ())
2120 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2121 "not consecutive access ");
2122 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2123 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2126 if (bb_vinfo)
2128 /* Mark the statement as unvectorizable. */
2129 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2130 return true;
2133 return false;
2136 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2138 /* First stmt in the interleaving chain. Check the chain. */
2139 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2140 struct data_reference *data_ref = dr;
2141 unsigned int count = 1;
2142 tree prev_init = DR_INIT (data_ref);
2143 gimple prev = stmt;
2144 HOST_WIDE_INT diff, gaps = 0;
2145 unsigned HOST_WIDE_INT count_in_bytes;
2147 while (next)
2149 /* Skip same data-refs. In case that two or more stmts share
2150 data-ref (supported only for loads), we vectorize only the first
2151 stmt, and the rest get their vectorized loads from the first
2152 one. */
2153 if (!tree_int_cst_compare (DR_INIT (data_ref),
2154 DR_INIT (STMT_VINFO_DATA_REF (
2155 vinfo_for_stmt (next)))))
2157 if (DR_IS_WRITE (data_ref))
2159 if (dump_enabled_p ())
2160 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2161 "Two store stmts share the same dr.\n");
2162 return false;
2165 /* For load use the same data-ref load. */
2166 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2168 prev = next;
2169 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2170 continue;
2173 prev = next;
2174 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2176 /* All group members have the same STEP by construction. */
2177 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2179 /* Check that the distance between two accesses is equal to the type
2180 size. Otherwise, we have gaps. */
2181 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2182 - TREE_INT_CST_LOW (prev_init)) / type_size;
2183 if (diff != 1)
2185 /* FORNOW: SLP of accesses with gaps is not supported. */
2186 slp_impossible = true;
2187 if (DR_IS_WRITE (data_ref))
2189 if (dump_enabled_p ())
2190 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2191 "interleaved store with gaps\n");
2192 return false;
2195 gaps += diff - 1;
2198 last_accessed_element += diff;
2200 /* Store the gap from the previous member of the group. If there is no
2201 gap in the access, GROUP_GAP is always 1. */
2202 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2204 prev_init = DR_INIT (data_ref);
2205 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2206 /* Count the number of data-refs in the chain. */
2207 count++;
2210 /* COUNT is the number of accesses found, we multiply it by the size of
2211 the type to get COUNT_IN_BYTES. */
2212 count_in_bytes = type_size * count;
2214 /* Check that the size of the interleaving (including gaps) is not
2215 greater than STEP. */
2216 if (dr_step != 0
2217 && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
2219 if (dump_enabled_p ())
2221 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2222 "interleaving size is greater than step for ");
2223 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2224 DR_REF (dr));
2225 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2227 return false;
2230 /* Check that the size of the interleaving is equal to STEP for stores,
2231 i.e., that there are no gaps. */
2232 if (dr_step != 0
2233 && absu_hwi (dr_step) != count_in_bytes)
2235 if (DR_IS_READ (dr))
2237 slp_impossible = true;
2238 /* There is a gap after the last load in the group. This gap is a
2239 difference between the groupsize and the number of elements.
2240 When there is no gap, this difference should be 0. */
2241 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2243 else
2245 if (dump_enabled_p ())
2246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2247 "interleaved store with gaps\n");
2248 return false;
2252 /* Check that STEP is a multiple of type size. */
2253 if (dr_step != 0
2254 && (dr_step % type_size) != 0)
2256 if (dump_enabled_p ())
2258 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2259 "step is not a multiple of type size: step ");
2260 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
2261 dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
2262 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2263 TYPE_SIZE_UNIT (scalar_type));
2264 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2266 return false;
2269 if (groupsize == 0)
2270 groupsize = count;
2272 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2273 if (dump_enabled_p ())
2274 dump_printf_loc (MSG_NOTE, vect_location,
2275 "Detected interleaving of size %d\n", (int)groupsize);
2277 /* SLP: create an SLP data structure for every interleaving group of
2278 stores for further analysis in vect_analyse_slp. */
2279 if (DR_IS_WRITE (dr) && !slp_impossible)
2281 if (loop_vinfo)
2282 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2283 if (bb_vinfo)
2284 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2287 /* There is a gap in the end of the group. */
2288 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2290 if (dump_enabled_p ())
2291 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2292 "Data access with gaps requires scalar "
2293 "epilogue loop\n");
2294 if (loop->inner)
2296 if (dump_enabled_p ())
2297 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2298 "Peeling for outer loop is not supported\n");
2299 return false;
2302 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2306 return true;
2310 /* Analyze the access pattern of the data-reference DR.
2311 In case of non-consecutive accesses call vect_analyze_group_access() to
2312 analyze groups of accesses. */
2314 static bool
2315 vect_analyze_data_ref_access (struct data_reference *dr)
2317 tree step = DR_STEP (dr);
2318 tree scalar_type = TREE_TYPE (DR_REF (dr));
2319 gimple stmt = DR_STMT (dr);
2320 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2321 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2322 struct loop *loop = NULL;
2324 if (loop_vinfo)
2325 loop = LOOP_VINFO_LOOP (loop_vinfo);
2327 if (loop_vinfo && !step)
2329 if (dump_enabled_p ())
2330 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2331 "bad data-ref access in loop\n");
2332 return false;
2335 /* Allow invariant loads in not nested loops. */
2336 if (loop_vinfo && integer_zerop (step))
2338 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2339 if (nested_in_vect_loop_p (loop, stmt))
2341 if (dump_enabled_p ())
2342 dump_printf_loc (MSG_NOTE, vect_location,
2343 "zero step in inner loop of nest\n");
2344 return false;
2346 return DR_IS_READ (dr);
2349 if (loop && nested_in_vect_loop_p (loop, stmt))
2351 /* Interleaved accesses are not yet supported within outer-loop
2352 vectorization for references in the inner-loop. */
2353 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2355 /* For the rest of the analysis we use the outer-loop step. */
2356 step = STMT_VINFO_DR_STEP (stmt_info);
2357 if (integer_zerop (step))
2359 if (dump_enabled_p ())
2360 dump_printf_loc (MSG_NOTE, vect_location,
2361 "zero step in outer loop.\n");
2362 if (DR_IS_READ (dr))
2363 return true;
2364 else
2365 return false;
2369 /* Consecutive? */
2370 if (TREE_CODE (step) == INTEGER_CST)
2372 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2373 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2374 || (dr_step < 0
2375 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2377 /* Mark that it is not interleaving. */
2378 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2379 return true;
2383 if (loop && nested_in_vect_loop_p (loop, stmt))
2385 if (dump_enabled_p ())
2386 dump_printf_loc (MSG_NOTE, vect_location,
2387 "grouped access in outer loop.\n");
2388 return false;
2391 /* Assume this is a DR handled by non-constant strided load case. */
2392 if (TREE_CODE (step) != INTEGER_CST)
2393 return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
2395 /* Not consecutive access - check if it's a part of interleaving group. */
2396 return vect_analyze_group_access (dr);
2401 /* A helper function used in the comparator function to sort data
2402 references. T1 and T2 are two data references to be compared.
2403 The function returns -1, 0, or 1. */
2405 static int
2406 compare_tree (tree t1, tree t2)
2408 int i, cmp;
2409 enum tree_code code;
2410 char tclass;
2412 if (t1 == t2)
2413 return 0;
2414 if (t1 == NULL)
2415 return -1;
2416 if (t2 == NULL)
2417 return 1;
2420 if (TREE_CODE (t1) != TREE_CODE (t2))
2421 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2423 code = TREE_CODE (t1);
2424 switch (code)
2426 /* For const values, we can just use hash values for comparisons. */
2427 case INTEGER_CST:
2428 case REAL_CST:
2429 case FIXED_CST:
2430 case STRING_CST:
2431 case COMPLEX_CST:
2432 case VECTOR_CST:
2434 hashval_t h1 = iterative_hash_expr (t1, 0);
2435 hashval_t h2 = iterative_hash_expr (t2, 0);
2436 if (h1 != h2)
2437 return h1 < h2 ? -1 : 1;
2438 break;
2441 case SSA_NAME:
2442 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2443 if (cmp != 0)
2444 return cmp;
2446 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2447 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2448 break;
2450 default:
2451 tclass = TREE_CODE_CLASS (code);
2453 /* For var-decl, we could compare their UIDs. */
2454 if (tclass == tcc_declaration)
2456 if (DECL_UID (t1) != DECL_UID (t2))
2457 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2458 break;
2461 /* For expressions with operands, compare their operands recursively. */
2462 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2464 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2465 if (cmp != 0)
2466 return cmp;
2470 return 0;
2474 /* Compare two data-references DRA and DRB to group them into chunks
2475 suitable for grouping. */
2477 static int
2478 dr_group_sort_cmp (const void *dra_, const void *drb_)
2480 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2481 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2482 int cmp;
2484 /* Stabilize sort. */
2485 if (dra == drb)
2486 return 0;
2488 /* Ordering of DRs according to base. */
2489 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2491 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2492 if (cmp != 0)
2493 return cmp;
2496 /* And according to DR_OFFSET. */
2497 if (!dr_equal_offsets_p (dra, drb))
2499 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2500 if (cmp != 0)
2501 return cmp;
2504 /* Put reads before writes. */
2505 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2506 return DR_IS_READ (dra) ? -1 : 1;
2508 /* Then sort after access size. */
2509 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2510 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2512 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2513 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2514 if (cmp != 0)
2515 return cmp;
2518 /* And after step. */
2519 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2521 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2522 if (cmp != 0)
2523 return cmp;
2526 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2527 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2528 if (cmp == 0)
2529 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2530 return cmp;
2533 /* Function vect_analyze_data_ref_accesses.
2535 Analyze the access pattern of all the data references in the loop.
2537 FORNOW: the only access pattern that is considered vectorizable is a
2538 simple step 1 (consecutive) access.
2540 FORNOW: handle only arrays and pointer accesses. */
2542 bool
2543 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2545 unsigned int i;
2546 vec<data_reference_p> datarefs;
2547 struct data_reference *dr;
2549 if (dump_enabled_p ())
2550 dump_printf_loc (MSG_NOTE, vect_location,
2551 "=== vect_analyze_data_ref_accesses ===\n");
2553 if (loop_vinfo)
2554 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2555 else
2556 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2558 if (datarefs.is_empty ())
2559 return true;
2561 /* Sort the array of datarefs to make building the interleaving chains
2562 linear. Don't modify the original vector's order, it is needed for
2563 determining what dependencies are reversed. */
2564 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2565 datarefs_copy.qsort (dr_group_sort_cmp);
2567 /* Build the interleaving chains. */
2568 for (i = 0; i < datarefs_copy.length () - 1;)
2570 data_reference_p dra = datarefs_copy[i];
2571 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2572 stmt_vec_info lastinfo = NULL;
2573 for (i = i + 1; i < datarefs_copy.length (); ++i)
2575 data_reference_p drb = datarefs_copy[i];
2576 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2578 /* ??? Imperfect sorting (non-compatible types, non-modulo
2579 accesses, same accesses) can lead to a group to be artificially
2580 split here as we don't just skip over those. If it really
2581 matters we can push those to a worklist and re-iterate
2582 over them. The we can just skip ahead to the next DR here. */
2584 /* Check that the data-refs have same first location (except init)
2585 and they are both either store or load (not load and store,
2586 not masked loads or stores). */
2587 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2588 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2589 DR_BASE_ADDRESS (drb), 0)
2590 || !dr_equal_offsets_p (dra, drb)
2591 || !gimple_assign_single_p (DR_STMT (dra))
2592 || !gimple_assign_single_p (DR_STMT (drb)))
2593 break;
2595 /* Check that the data-refs have the same constant size and step. */
2596 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2597 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2598 if (!tree_fits_uhwi_p (sza)
2599 || !tree_fits_uhwi_p (szb)
2600 || !tree_int_cst_equal (sza, szb)
2601 || !tree_fits_shwi_p (DR_STEP (dra))
2602 || !tree_fits_shwi_p (DR_STEP (drb))
2603 || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
2604 break;
2606 /* Do not place the same access in the interleaving chain twice. */
2607 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2608 break;
2610 /* Check the types are compatible.
2611 ??? We don't distinguish this during sorting. */
2612 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2613 TREE_TYPE (DR_REF (drb))))
2614 break;
2616 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2617 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2618 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2619 gcc_assert (init_a < init_b);
2621 /* If init_b == init_a + the size of the type * k, we have an
2622 interleaving, and DRA is accessed before DRB. */
2623 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2624 if ((init_b - init_a) % type_size_a != 0)
2625 break;
2627 /* The step (if not zero) is greater than the difference between
2628 data-refs' inits. This splits groups into suitable sizes. */
2629 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2630 if (step != 0 && step <= (init_b - init_a))
2631 break;
2633 if (dump_enabled_p ())
2635 dump_printf_loc (MSG_NOTE, vect_location,
2636 "Detected interleaving ");
2637 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2638 dump_printf (MSG_NOTE, " and ");
2639 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2640 dump_printf (MSG_NOTE, "\n");
2643 /* Link the found element into the group list. */
2644 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2646 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2647 lastinfo = stmtinfo_a;
2649 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2650 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2651 lastinfo = stmtinfo_b;
2655 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2656 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2657 && !vect_analyze_data_ref_access (dr))
2659 if (dump_enabled_p ())
2660 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2661 "not vectorized: complicated access pattern.\n");
2663 if (bb_vinfo)
2665 /* Mark the statement as not vectorizable. */
2666 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2667 continue;
2669 else
2671 datarefs_copy.release ();
2672 return false;
2676 datarefs_copy.release ();
2677 return true;
2681 /* Operator == between two dr_with_seg_len objects.
2683 This equality operator is used to make sure two data refs
2684 are the same one so that we will consider to combine the
2685 aliasing checks of those two pairs of data dependent data
2686 refs. */
2688 static bool
2689 operator == (const dr_with_seg_len& d1,
2690 const dr_with_seg_len& d2)
2692 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2693 DR_BASE_ADDRESS (d2.dr), 0)
2694 && compare_tree (d1.offset, d2.offset) == 0
2695 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2698 /* Function comp_dr_with_seg_len_pair.
2700 Comparison function for sorting objects of dr_with_seg_len_pair_t
2701 so that we can combine aliasing checks in one scan. */
2703 static int
2704 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2706 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2707 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2709 const dr_with_seg_len &p11 = p1->first,
2710 &p12 = p1->second,
2711 &p21 = p2->first,
2712 &p22 = p2->second;
2714 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2715 if a and c have the same basic address snd step, and b and d have the same
2716 address and step. Therefore, if any a&c or b&d don't have the same address
2717 and step, we don't care the order of those two pairs after sorting. */
2718 int comp_res;
2720 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2721 DR_BASE_ADDRESS (p21.dr))) != 0)
2722 return comp_res;
2723 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2724 DR_BASE_ADDRESS (p22.dr))) != 0)
2725 return comp_res;
2726 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2727 return comp_res;
2728 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2729 return comp_res;
2730 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2731 return comp_res;
2732 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2733 return comp_res;
2735 return 0;
2738 /* Function vect_vfa_segment_size.
2740 Create an expression that computes the size of segment
2741 that will be accessed for a data reference. The functions takes into
2742 account that realignment loads may access one more vector.
2744 Input:
2745 DR: The data reference.
2746 LENGTH_FACTOR: segment length to consider.
2748 Return an expression whose value is the size of segment which will be
2749 accessed by DR. */
2751 static tree
2752 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2754 tree segment_length;
2756 if (integer_zerop (DR_STEP (dr)))
2757 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2758 else
2759 segment_length = size_binop (MULT_EXPR,
2760 fold_convert (sizetype, DR_STEP (dr)),
2761 fold_convert (sizetype, length_factor));
2763 if (vect_supportable_dr_alignment (dr, false)
2764 == dr_explicit_realign_optimized)
2766 tree vector_size = TYPE_SIZE_UNIT
2767 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2769 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2771 return segment_length;
2774 /* Function vect_prune_runtime_alias_test_list.
2776 Prune a list of ddrs to be tested at run-time by versioning for alias.
2777 Merge several alias checks into one if possible.
2778 Return FALSE if resulting list of ddrs is longer then allowed by
2779 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2781 bool
2782 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2784 vec<ddr_p> may_alias_ddrs =
2785 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2786 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2787 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2788 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2789 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2791 ddr_p ddr;
2792 unsigned int i;
2793 tree length_factor;
2795 if (dump_enabled_p ())
2796 dump_printf_loc (MSG_NOTE, vect_location,
2797 "=== vect_prune_runtime_alias_test_list ===\n");
2799 if (may_alias_ddrs.is_empty ())
2800 return true;
2802 /* Basically, for each pair of dependent data refs store_ptr_0
2803 and load_ptr_0, we create an expression:
2805 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2806 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2808 for aliasing checks. However, in some cases we can decrease
2809 the number of checks by combining two checks into one. For
2810 example, suppose we have another pair of data refs store_ptr_0
2811 and load_ptr_1, and if the following condition is satisfied:
2813 load_ptr_0 < load_ptr_1 &&
2814 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2816 (this condition means, in each iteration of vectorized loop,
2817 the accessed memory of store_ptr_0 cannot be between the memory
2818 of load_ptr_0 and load_ptr_1.)
2820 we then can use only the following expression to finish the
2821 alising checks between store_ptr_0 & load_ptr_0 and
2822 store_ptr_0 & load_ptr_1:
2824 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2825 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2827 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2828 same basic address. */
2830 comp_alias_ddrs.create (may_alias_ddrs.length ());
2832 /* First, we collect all data ref pairs for aliasing checks. */
2833 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2835 struct data_reference *dr_a, *dr_b;
2836 gimple dr_group_first_a, dr_group_first_b;
2837 tree segment_length_a, segment_length_b;
2838 gimple stmt_a, stmt_b;
2840 dr_a = DDR_A (ddr);
2841 stmt_a = DR_STMT (DDR_A (ddr));
2842 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2843 if (dr_group_first_a)
2845 stmt_a = dr_group_first_a;
2846 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2849 dr_b = DDR_B (ddr);
2850 stmt_b = DR_STMT (DDR_B (ddr));
2851 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2852 if (dr_group_first_b)
2854 stmt_b = dr_group_first_b;
2855 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2858 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2859 length_factor = scalar_loop_iters;
2860 else
2861 length_factor = size_int (vect_factor);
2862 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2863 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2865 dr_with_seg_len_pair_t dr_with_seg_len_pair
2866 (dr_with_seg_len (dr_a, segment_length_a),
2867 dr_with_seg_len (dr_b, segment_length_b));
2869 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2870 std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2872 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2875 /* Second, we sort the collected data ref pairs so that we can scan
2876 them once to combine all possible aliasing checks. */
2877 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2879 /* Third, we scan the sorted dr pairs and check if we can combine
2880 alias checks of two neighbouring dr pairs. */
2881 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2883 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2884 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2885 *dr_b1 = &comp_alias_ddrs[i-1].second,
2886 *dr_a2 = &comp_alias_ddrs[i].first,
2887 *dr_b2 = &comp_alias_ddrs[i].second;
2889 /* Remove duplicate data ref pairs. */
2890 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2892 if (dump_enabled_p ())
2894 dump_printf_loc (MSG_NOTE, vect_location,
2895 "found equal ranges ");
2896 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2897 DR_REF (dr_a1->dr));
2898 dump_printf (MSG_NOTE, ", ");
2899 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2900 DR_REF (dr_b1->dr));
2901 dump_printf (MSG_NOTE, " and ");
2902 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2903 DR_REF (dr_a2->dr));
2904 dump_printf (MSG_NOTE, ", ");
2905 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2906 DR_REF (dr_b2->dr));
2907 dump_printf (MSG_NOTE, "\n");
2910 comp_alias_ddrs.ordered_remove (i--);
2911 continue;
2914 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2916 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2917 and DR_A1 and DR_A2 are two consecutive memrefs. */
2918 if (*dr_a1 == *dr_a2)
2920 std::swap (dr_a1, dr_b1);
2921 std::swap (dr_a2, dr_b2);
2924 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2925 DR_BASE_ADDRESS (dr_a2->dr),
2927 || !tree_fits_shwi_p (dr_a1->offset)
2928 || !tree_fits_shwi_p (dr_a2->offset))
2929 continue;
2931 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2932 - tree_to_shwi (dr_a1->offset));
2935 /* Now we check if the following condition is satisfied:
2937 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2939 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2940 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2941 have to make a best estimation. We can get the minimum value
2942 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2943 then either of the following two conditions can guarantee the
2944 one above:
2946 1: DIFF <= MIN_SEG_LEN_B
2947 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2951 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2952 ? tree_to_shwi (dr_b1->seg_len)
2953 : vect_factor);
2955 if (diff <= min_seg_len_b
2956 || (tree_fits_shwi_p (dr_a1->seg_len)
2957 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2959 if (dump_enabled_p ())
2961 dump_printf_loc (MSG_NOTE, vect_location,
2962 "merging ranges for ");
2963 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2964 DR_REF (dr_a1->dr));
2965 dump_printf (MSG_NOTE, ", ");
2966 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2967 DR_REF (dr_b1->dr));
2968 dump_printf (MSG_NOTE, " and ");
2969 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2970 DR_REF (dr_a2->dr));
2971 dump_printf (MSG_NOTE, ", ");
2972 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2973 DR_REF (dr_b2->dr));
2974 dump_printf (MSG_NOTE, "\n");
2977 dr_a1->seg_len = size_binop (PLUS_EXPR,
2978 dr_a2->seg_len, size_int (diff));
2979 comp_alias_ddrs.ordered_remove (i--);
2984 dump_printf_loc (MSG_NOTE, vect_location,
2985 "improved number of alias checks from %d to %d\n",
2986 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2987 if ((int) comp_alias_ddrs.length () >
2988 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2989 return false;
2991 return true;
2994 /* Check whether a non-affine read in stmt is suitable for gather load
2995 and if so, return a builtin decl for that operation. */
2997 tree
2998 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2999 tree *offp, int *scalep)
3001 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
3002 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3003 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3004 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3005 tree offtype = NULL_TREE;
3006 tree decl, base, off;
3007 machine_mode pmode;
3008 int punsignedp, pvolatilep;
3010 base = DR_REF (dr);
3011 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
3012 see if we can use the def stmt of the address. */
3013 if (is_gimple_call (stmt)
3014 && gimple_call_internal_p (stmt)
3015 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
3016 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
3017 && TREE_CODE (base) == MEM_REF
3018 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3019 && integer_zerop (TREE_OPERAND (base, 1))
3020 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
3022 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
3023 if (is_gimple_assign (def_stmt)
3024 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3025 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
3028 /* The gather builtins need address of the form
3029 loop_invariant + vector * {1, 2, 4, 8}
3031 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
3032 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
3033 of loop invariants/SSA_NAMEs defined in the loop, with casts,
3034 multiplications and additions in it. To get a vector, we need
3035 a single SSA_NAME that will be defined in the loop and will
3036 contain everything that is not loop invariant and that can be
3037 vectorized. The following code attempts to find such a preexistng
3038 SSA_NAME OFF and put the loop invariants into a tree BASE
3039 that can be gimplified before the loop. */
3040 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
3041 &pmode, &punsignedp, &pvolatilep, false);
3042 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
3044 if (TREE_CODE (base) == MEM_REF)
3046 if (!integer_zerop (TREE_OPERAND (base, 1)))
3048 if (off == NULL_TREE)
3050 offset_int moff = mem_ref_offset (base);
3051 off = wide_int_to_tree (sizetype, moff);
3053 else
3054 off = size_binop (PLUS_EXPR, off,
3055 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3057 base = TREE_OPERAND (base, 0);
3059 else
3060 base = build_fold_addr_expr (base);
3062 if (off == NULL_TREE)
3063 off = size_zero_node;
3065 /* If base is not loop invariant, either off is 0, then we start with just
3066 the constant offset in the loop invariant BASE and continue with base
3067 as OFF, otherwise give up.
3068 We could handle that case by gimplifying the addition of base + off
3069 into some SSA_NAME and use that as off, but for now punt. */
3070 if (!expr_invariant_in_loop_p (loop, base))
3072 if (!integer_zerop (off))
3073 return NULL_TREE;
3074 off = base;
3075 base = size_int (pbitpos / BITS_PER_UNIT);
3077 /* Otherwise put base + constant offset into the loop invariant BASE
3078 and continue with OFF. */
3079 else
3081 base = fold_convert (sizetype, base);
3082 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3085 /* OFF at this point may be either a SSA_NAME or some tree expression
3086 from get_inner_reference. Try to peel off loop invariants from it
3087 into BASE as long as possible. */
3088 STRIP_NOPS (off);
3089 while (offtype == NULL_TREE)
3091 enum tree_code code;
3092 tree op0, op1, add = NULL_TREE;
3094 if (TREE_CODE (off) == SSA_NAME)
3096 gimple def_stmt = SSA_NAME_DEF_STMT (off);
3098 if (expr_invariant_in_loop_p (loop, off))
3099 return NULL_TREE;
3101 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3102 break;
3104 op0 = gimple_assign_rhs1 (def_stmt);
3105 code = gimple_assign_rhs_code (def_stmt);
3106 op1 = gimple_assign_rhs2 (def_stmt);
3108 else
3110 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3111 return NULL_TREE;
3112 code = TREE_CODE (off);
3113 extract_ops_from_tree (off, &code, &op0, &op1);
3115 switch (code)
3117 case POINTER_PLUS_EXPR:
3118 case PLUS_EXPR:
3119 if (expr_invariant_in_loop_p (loop, op0))
3121 add = op0;
3122 off = op1;
3123 do_add:
3124 add = fold_convert (sizetype, add);
3125 if (scale != 1)
3126 add = size_binop (MULT_EXPR, add, size_int (scale));
3127 base = size_binop (PLUS_EXPR, base, add);
3128 continue;
3130 if (expr_invariant_in_loop_p (loop, op1))
3132 add = op1;
3133 off = op0;
3134 goto do_add;
3136 break;
3137 case MINUS_EXPR:
3138 if (expr_invariant_in_loop_p (loop, op1))
3140 add = fold_convert (sizetype, op1);
3141 add = size_binop (MINUS_EXPR, size_zero_node, add);
3142 off = op0;
3143 goto do_add;
3145 break;
3146 case MULT_EXPR:
3147 if (scale == 1 && tree_fits_shwi_p (op1))
3149 scale = tree_to_shwi (op1);
3150 off = op0;
3151 continue;
3153 break;
3154 case SSA_NAME:
3155 off = op0;
3156 continue;
3157 CASE_CONVERT:
3158 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3159 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3160 break;
3161 if (TYPE_PRECISION (TREE_TYPE (op0))
3162 == TYPE_PRECISION (TREE_TYPE (off)))
3164 off = op0;
3165 continue;
3167 if (TYPE_PRECISION (TREE_TYPE (op0))
3168 < TYPE_PRECISION (TREE_TYPE (off)))
3170 off = op0;
3171 offtype = TREE_TYPE (off);
3172 STRIP_NOPS (off);
3173 continue;
3175 break;
3176 default:
3177 break;
3179 break;
3182 /* If at the end OFF still isn't a SSA_NAME or isn't
3183 defined in the loop, punt. */
3184 if (TREE_CODE (off) != SSA_NAME
3185 || expr_invariant_in_loop_p (loop, off))
3186 return NULL_TREE;
3188 if (offtype == NULL_TREE)
3189 offtype = TREE_TYPE (off);
3191 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3192 offtype, scale);
3193 if (decl == NULL_TREE)
3194 return NULL_TREE;
3196 if (basep)
3197 *basep = base;
3198 if (offp)
3199 *offp = off;
3200 if (scalep)
3201 *scalep = scale;
3202 return decl;
3205 /* Function vect_analyze_data_refs.
3207 Find all the data references in the loop or basic block.
3209 The general structure of the analysis of data refs in the vectorizer is as
3210 follows:
3211 1- vect_analyze_data_refs(loop/bb): call
3212 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3213 in the loop/bb and their dependences.
3214 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3215 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3216 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3220 bool
3221 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3222 bb_vec_info bb_vinfo,
3223 int *min_vf, unsigned *n_stmts)
3225 struct loop *loop = NULL;
3226 basic_block bb = NULL;
3227 unsigned int i;
3228 vec<data_reference_p> datarefs;
3229 struct data_reference *dr;
3230 tree scalar_type;
3232 if (dump_enabled_p ())
3233 dump_printf_loc (MSG_NOTE, vect_location,
3234 "=== vect_analyze_data_refs ===\n");
3236 if (loop_vinfo)
3238 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3240 loop = LOOP_VINFO_LOOP (loop_vinfo);
3241 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3242 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3244 if (dump_enabled_p ())
3245 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3246 "not vectorized: loop contains function calls"
3247 " or data references that cannot be analyzed\n");
3248 return false;
3251 for (i = 0; i < loop->num_nodes; i++)
3253 gimple_stmt_iterator gsi;
3255 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3257 gimple stmt = gsi_stmt (gsi);
3258 if (is_gimple_debug (stmt))
3259 continue;
3260 ++*n_stmts;
3261 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3263 if (is_gimple_call (stmt) && loop->safelen)
3265 tree fndecl = gimple_call_fndecl (stmt), op;
3266 if (fndecl != NULL_TREE)
3268 struct cgraph_node *node = cgraph_node::get (fndecl);
3269 if (node != NULL && node->simd_clones != NULL)
3271 unsigned int j, n = gimple_call_num_args (stmt);
3272 for (j = 0; j < n; j++)
3274 op = gimple_call_arg (stmt, j);
3275 if (DECL_P (op)
3276 || (REFERENCE_CLASS_P (op)
3277 && get_base_address (op)))
3278 break;
3280 op = gimple_call_lhs (stmt);
3281 /* Ignore #pragma omp declare simd functions
3282 if they don't have data references in the
3283 call stmt itself. */
3284 if (j == n
3285 && !(op
3286 && (DECL_P (op)
3287 || (REFERENCE_CLASS_P (op)
3288 && get_base_address (op)))))
3289 continue;
3293 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3294 if (dump_enabled_p ())
3295 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3296 "not vectorized: loop contains function "
3297 "calls or data references that cannot "
3298 "be analyzed\n");
3299 return false;
3304 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3306 else
3308 gimple_stmt_iterator gsi;
3310 bb = BB_VINFO_BB (bb_vinfo);
3311 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3313 gimple stmt = gsi_stmt (gsi);
3314 if (is_gimple_debug (stmt))
3315 continue;
3316 ++*n_stmts;
3317 if (!find_data_references_in_stmt (NULL, stmt,
3318 &BB_VINFO_DATAREFS (bb_vinfo)))
3320 /* Mark the rest of the basic-block as unvectorizable. */
3321 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3323 stmt = gsi_stmt (gsi);
3324 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3326 break;
3330 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3333 /* Go through the data-refs, check that the analysis succeeded. Update
3334 pointer from stmt_vec_info struct to DR and vectype. */
3336 FOR_EACH_VEC_ELT (datarefs, i, dr)
3338 gimple stmt;
3339 stmt_vec_info stmt_info;
3340 tree base, offset, init;
3341 bool gather = false;
3342 bool simd_lane_access = false;
3343 int vf;
3345 again:
3346 if (!dr || !DR_REF (dr))
3348 if (dump_enabled_p ())
3349 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3350 "not vectorized: unhandled data-ref\n");
3351 return false;
3354 stmt = DR_STMT (dr);
3355 stmt_info = vinfo_for_stmt (stmt);
3357 /* Discard clobbers from the dataref vector. We will remove
3358 clobber stmts during vectorization. */
3359 if (gimple_clobber_p (stmt))
3361 free_data_ref (dr);
3362 if (i == datarefs.length () - 1)
3364 datarefs.pop ();
3365 break;
3367 datarefs.ordered_remove (i);
3368 dr = datarefs[i];
3369 goto again;
3372 /* Check that analysis of the data-ref succeeded. */
3373 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3374 || !DR_STEP (dr))
3376 bool maybe_gather
3377 = DR_IS_READ (dr)
3378 && !TREE_THIS_VOLATILE (DR_REF (dr))
3379 && targetm.vectorize.builtin_gather != NULL;
3380 bool maybe_simd_lane_access
3381 = loop_vinfo && loop->simduid;
3383 /* If target supports vector gather loads, or if this might be
3384 a SIMD lane access, see if they can't be used. */
3385 if (loop_vinfo
3386 && (maybe_gather || maybe_simd_lane_access)
3387 && !nested_in_vect_loop_p (loop, stmt))
3389 struct data_reference *newdr
3390 = create_data_ref (NULL, loop_containing_stmt (stmt),
3391 DR_REF (dr), stmt, true);
3392 gcc_assert (newdr != NULL && DR_REF (newdr));
3393 if (DR_BASE_ADDRESS (newdr)
3394 && DR_OFFSET (newdr)
3395 && DR_INIT (newdr)
3396 && DR_STEP (newdr)
3397 && integer_zerop (DR_STEP (newdr)))
3399 if (maybe_simd_lane_access)
3401 tree off = DR_OFFSET (newdr);
3402 STRIP_NOPS (off);
3403 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3404 && TREE_CODE (off) == MULT_EXPR
3405 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3407 tree step = TREE_OPERAND (off, 1);
3408 off = TREE_OPERAND (off, 0);
3409 STRIP_NOPS (off);
3410 if (CONVERT_EXPR_P (off)
3411 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3412 0)))
3413 < TYPE_PRECISION (TREE_TYPE (off)))
3414 off = TREE_OPERAND (off, 0);
3415 if (TREE_CODE (off) == SSA_NAME)
3417 gimple def = SSA_NAME_DEF_STMT (off);
3418 tree reft = TREE_TYPE (DR_REF (newdr));
3419 if (is_gimple_call (def)
3420 && gimple_call_internal_p (def)
3421 && (gimple_call_internal_fn (def)
3422 == IFN_GOMP_SIMD_LANE))
3424 tree arg = gimple_call_arg (def, 0);
3425 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3426 arg = SSA_NAME_VAR (arg);
3427 if (arg == loop->simduid
3428 /* For now. */
3429 && tree_int_cst_equal
3430 (TYPE_SIZE_UNIT (reft),
3431 step))
3433 DR_OFFSET (newdr) = ssize_int (0);
3434 DR_STEP (newdr) = step;
3435 DR_ALIGNED_TO (newdr)
3436 = size_int (BIGGEST_ALIGNMENT);
3437 dr = newdr;
3438 simd_lane_access = true;
3444 if (!simd_lane_access && maybe_gather)
3446 dr = newdr;
3447 gather = true;
3450 if (!gather && !simd_lane_access)
3451 free_data_ref (newdr);
3454 if (!gather && !simd_lane_access)
3456 if (dump_enabled_p ())
3458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3459 "not vectorized: data ref analysis "
3460 "failed ");
3461 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3462 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3465 if (bb_vinfo)
3466 break;
3468 return false;
3472 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3474 if (dump_enabled_p ())
3475 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3476 "not vectorized: base addr of dr is a "
3477 "constant\n");
3479 if (bb_vinfo)
3480 break;
3482 if (gather || simd_lane_access)
3483 free_data_ref (dr);
3484 return false;
3487 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3489 if (dump_enabled_p ())
3491 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3492 "not vectorized: volatile type ");
3493 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3494 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3497 if (bb_vinfo)
3498 break;
3500 return false;
3503 if (stmt_can_throw_internal (stmt))
3505 if (dump_enabled_p ())
3507 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3508 "not vectorized: statement can throw an "
3509 "exception ");
3510 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3511 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3514 if (bb_vinfo)
3515 break;
3517 if (gather || simd_lane_access)
3518 free_data_ref (dr);
3519 return false;
3522 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3523 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3525 if (dump_enabled_p ())
3527 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3528 "not vectorized: statement is bitfield "
3529 "access ");
3530 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3531 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3534 if (bb_vinfo)
3535 break;
3537 if (gather || simd_lane_access)
3538 free_data_ref (dr);
3539 return false;
3542 base = unshare_expr (DR_BASE_ADDRESS (dr));
3543 offset = unshare_expr (DR_OFFSET (dr));
3544 init = unshare_expr (DR_INIT (dr));
3546 if (is_gimple_call (stmt)
3547 && (!gimple_call_internal_p (stmt)
3548 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3549 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3551 if (dump_enabled_p ())
3553 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3554 "not vectorized: dr in a call ");
3555 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3556 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3559 if (bb_vinfo)
3560 break;
3562 if (gather || simd_lane_access)
3563 free_data_ref (dr);
3564 return false;
3567 /* Update DR field in stmt_vec_info struct. */
3569 /* If the dataref is in an inner-loop of the loop that is considered for
3570 for vectorization, we also want to analyze the access relative to
3571 the outer-loop (DR contains information only relative to the
3572 inner-most enclosing loop). We do that by building a reference to the
3573 first location accessed by the inner-loop, and analyze it relative to
3574 the outer-loop. */
3575 if (loop && nested_in_vect_loop_p (loop, stmt))
3577 tree outer_step, outer_base, outer_init;
3578 HOST_WIDE_INT pbitsize, pbitpos;
3579 tree poffset;
3580 machine_mode pmode;
3581 int punsignedp, pvolatilep;
3582 affine_iv base_iv, offset_iv;
3583 tree dinit;
3585 /* Build a reference to the first location accessed by the
3586 inner-loop: *(BASE+INIT). (The first location is actually
3587 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3588 tree inner_base = build_fold_indirect_ref
3589 (fold_build_pointer_plus (base, init));
3591 if (dump_enabled_p ())
3593 dump_printf_loc (MSG_NOTE, vect_location,
3594 "analyze in outer-loop: ");
3595 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3596 dump_printf (MSG_NOTE, "\n");
3599 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3600 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3601 gcc_assert (outer_base != NULL_TREE);
3603 if (pbitpos % BITS_PER_UNIT != 0)
3605 if (dump_enabled_p ())
3606 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3607 "failed: bit offset alignment.\n");
3608 return false;
3611 outer_base = build_fold_addr_expr (outer_base);
3612 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3613 &base_iv, false))
3615 if (dump_enabled_p ())
3616 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3617 "failed: evolution of base is not affine.\n");
3618 return false;
3621 if (offset)
3623 if (poffset)
3624 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3625 poffset);
3626 else
3627 poffset = offset;
3630 if (!poffset)
3632 offset_iv.base = ssize_int (0);
3633 offset_iv.step = ssize_int (0);
3635 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3636 &offset_iv, false))
3638 if (dump_enabled_p ())
3639 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3640 "evolution of offset is not affine.\n");
3641 return false;
3644 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3645 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3646 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3647 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3648 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3650 outer_step = size_binop (PLUS_EXPR,
3651 fold_convert (ssizetype, base_iv.step),
3652 fold_convert (ssizetype, offset_iv.step));
3654 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3655 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3656 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3657 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3658 STMT_VINFO_DR_OFFSET (stmt_info) =
3659 fold_convert (ssizetype, offset_iv.base);
3660 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3661 size_int (highest_pow2_factor (offset_iv.base));
3663 if (dump_enabled_p ())
3665 dump_printf_loc (MSG_NOTE, vect_location,
3666 "\touter base_address: ");
3667 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3668 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3669 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3670 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3671 STMT_VINFO_DR_OFFSET (stmt_info));
3672 dump_printf (MSG_NOTE,
3673 "\n\touter constant offset from base address: ");
3674 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3675 STMT_VINFO_DR_INIT (stmt_info));
3676 dump_printf (MSG_NOTE, "\n\touter step: ");
3677 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3678 STMT_VINFO_DR_STEP (stmt_info));
3679 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3680 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3681 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3682 dump_printf (MSG_NOTE, "\n");
3686 if (STMT_VINFO_DATA_REF (stmt_info))
3688 if (dump_enabled_p ())
3690 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3691 "not vectorized: more than one data ref "
3692 "in stmt: ");
3693 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3694 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3697 if (bb_vinfo)
3698 break;
3700 if (gather || simd_lane_access)
3701 free_data_ref (dr);
3702 return false;
3705 STMT_VINFO_DATA_REF (stmt_info) = dr;
3706 if (simd_lane_access)
3708 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3709 free_data_ref (datarefs[i]);
3710 datarefs[i] = dr;
3713 /* Set vectype for STMT. */
3714 scalar_type = TREE_TYPE (DR_REF (dr));
3715 STMT_VINFO_VECTYPE (stmt_info)
3716 = get_vectype_for_scalar_type (scalar_type);
3717 if (!STMT_VINFO_VECTYPE (stmt_info))
3719 if (dump_enabled_p ())
3721 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3722 "not vectorized: no vectype for stmt: ");
3723 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3724 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3725 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3726 scalar_type);
3727 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3730 if (bb_vinfo)
3731 break;
3733 if (gather || simd_lane_access)
3735 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3736 if (gather)
3737 free_data_ref (dr);
3739 return false;
3741 else
3743 if (dump_enabled_p ())
3745 dump_printf_loc (MSG_NOTE, vect_location,
3746 "got vectype for stmt: ");
3747 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3748 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3749 STMT_VINFO_VECTYPE (stmt_info));
3750 dump_printf (MSG_NOTE, "\n");
3754 /* Adjust the minimal vectorization factor according to the
3755 vector type. */
3756 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3757 if (vf > *min_vf)
3758 *min_vf = vf;
3760 if (gather)
3762 tree off;
3764 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3765 if (gather
3766 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3767 gather = false;
3768 if (!gather)
3770 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3771 free_data_ref (dr);
3772 if (dump_enabled_p ())
3774 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3775 "not vectorized: not suitable for gather "
3776 "load ");
3777 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3778 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3780 return false;
3783 datarefs[i] = dr;
3784 STMT_VINFO_GATHER_P (stmt_info) = true;
3786 else if (loop_vinfo
3787 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3789 if (nested_in_vect_loop_p (loop, stmt)
3790 || !DR_IS_READ (dr))
3792 if (dump_enabled_p ())
3794 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3795 "not vectorized: not suitable for strided "
3796 "load ");
3797 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3798 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3800 return false;
3802 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3806 /* If we stopped analysis at the first dataref we could not analyze
3807 when trying to vectorize a basic-block mark the rest of the datarefs
3808 as not vectorizable and truncate the vector of datarefs. That
3809 avoids spending useless time in analyzing their dependence. */
3810 if (i != datarefs.length ())
3812 gcc_assert (bb_vinfo != NULL);
3813 for (unsigned j = i; j < datarefs.length (); ++j)
3815 data_reference_p dr = datarefs[j];
3816 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3817 free_data_ref (dr);
3819 datarefs.truncate (i);
3822 return true;
3826 /* Function vect_get_new_vect_var.
3828 Returns a name for a new variable. The current naming scheme appends the
3829 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3830 the name of vectorizer generated variables, and appends that to NAME if
3831 provided. */
3833 tree
3834 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3836 const char *prefix;
3837 tree new_vect_var;
3839 switch (var_kind)
3841 case vect_simple_var:
3842 prefix = "vect";
3843 break;
3844 case vect_scalar_var:
3845 prefix = "stmp";
3846 break;
3847 case vect_pointer_var:
3848 prefix = "vectp";
3849 break;
3850 default:
3851 gcc_unreachable ();
3854 if (name)
3856 char* tmp = concat (prefix, "_", name, NULL);
3857 new_vect_var = create_tmp_reg (type, tmp);
3858 free (tmp);
3860 else
3861 new_vect_var = create_tmp_reg (type, prefix);
3863 return new_vect_var;
3867 /* Function vect_create_addr_base_for_vector_ref.
3869 Create an expression that computes the address of the first memory location
3870 that will be accessed for a data reference.
3872 Input:
3873 STMT: The statement containing the data reference.
3874 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3875 OFFSET: Optional. If supplied, it is be added to the initial address.
3876 LOOP: Specify relative to which loop-nest should the address be computed.
3877 For example, when the dataref is in an inner-loop nested in an
3878 outer-loop that is now being vectorized, LOOP can be either the
3879 outer-loop, or the inner-loop. The first memory location accessed
3880 by the following dataref ('in' points to short):
3882 for (i=0; i<N; i++)
3883 for (j=0; j<M; j++)
3884 s += in[i+j]
3886 is as follows:
3887 if LOOP=i_loop: &in (relative to i_loop)
3888 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3889 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3890 initial address. Unlike OFFSET, which is number of elements to
3891 be added, BYTE_OFFSET is measured in bytes.
3893 Output:
3894 1. Return an SSA_NAME whose value is the address of the memory location of
3895 the first vector of the data reference.
3896 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3897 these statement(s) which define the returned SSA_NAME.
3899 FORNOW: We are only handling array accesses with step 1. */
3901 tree
3902 vect_create_addr_base_for_vector_ref (gimple stmt,
3903 gimple_seq *new_stmt_list,
3904 tree offset,
3905 struct loop *loop,
3906 tree byte_offset)
3908 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3909 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3910 tree data_ref_base;
3911 const char *base_name;
3912 tree addr_base;
3913 tree dest;
3914 gimple_seq seq = NULL;
3915 tree base_offset;
3916 tree init;
3917 tree vect_ptr_type;
3918 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3919 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3921 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3923 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3925 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3927 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3928 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3929 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3931 else
3933 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3934 base_offset = unshare_expr (DR_OFFSET (dr));
3935 init = unshare_expr (DR_INIT (dr));
3938 if (loop_vinfo)
3939 base_name = get_name (data_ref_base);
3940 else
3942 base_offset = ssize_int (0);
3943 init = ssize_int (0);
3944 base_name = get_name (DR_REF (dr));
3947 /* Create base_offset */
3948 base_offset = size_binop (PLUS_EXPR,
3949 fold_convert (sizetype, base_offset),
3950 fold_convert (sizetype, init));
3952 if (offset)
3954 offset = fold_build2 (MULT_EXPR, sizetype,
3955 fold_convert (sizetype, offset), step);
3956 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3957 base_offset, offset);
3959 if (byte_offset)
3961 byte_offset = fold_convert (sizetype, byte_offset);
3962 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3963 base_offset, byte_offset);
3966 /* base + base_offset */
3967 if (loop_vinfo)
3968 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3969 else
3971 addr_base = build1 (ADDR_EXPR,
3972 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3973 unshare_expr (DR_REF (dr)));
3976 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3977 addr_base = fold_convert (vect_ptr_type, addr_base);
3978 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3979 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3980 gimple_seq_add_seq (new_stmt_list, seq);
3982 if (DR_PTR_INFO (dr)
3983 && TREE_CODE (addr_base) == SSA_NAME)
3985 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3986 unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
3987 int misalign = DR_MISALIGNMENT (dr);
3988 if (offset || byte_offset || (misalign == -1))
3989 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3990 else
3991 set_ptr_info_alignment (SSA_NAME_PTR_INFO (addr_base), align, misalign);
3994 if (dump_enabled_p ())
3996 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3997 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3998 dump_printf (MSG_NOTE, "\n");
4001 return addr_base;
4005 /* Function vect_create_data_ref_ptr.
4007 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
4008 location accessed in the loop by STMT, along with the def-use update
4009 chain to appropriately advance the pointer through the loop iterations.
4010 Also set aliasing information for the pointer. This pointer is used by
4011 the callers to this function to create a memory reference expression for
4012 vector load/store access.
4014 Input:
4015 1. STMT: a stmt that references memory. Expected to be of the form
4016 GIMPLE_ASSIGN <name, data-ref> or
4017 GIMPLE_ASSIGN <data-ref, name>.
4018 2. AGGR_TYPE: the type of the reference, which should be either a vector
4019 or an array.
4020 3. AT_LOOP: the loop where the vector memref is to be created.
4021 4. OFFSET (optional): an offset to be added to the initial address accessed
4022 by the data-ref in STMT.
4023 5. BSI: location where the new stmts are to be placed if there is no loop
4024 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
4025 pointing to the initial address.
4026 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
4027 to the initial address accessed by the data-ref in STMT. This is
4028 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
4029 in bytes.
4031 Output:
4032 1. Declare a new ptr to vector_type, and have it point to the base of the
4033 data reference (initial addressed accessed by the data reference).
4034 For example, for vector of type V8HI, the following code is generated:
4036 v8hi *ap;
4037 ap = (v8hi *)initial_address;
4039 if OFFSET is not supplied:
4040 initial_address = &a[init];
4041 if OFFSET is supplied:
4042 initial_address = &a[init + OFFSET];
4043 if BYTE_OFFSET is supplied:
4044 initial_address = &a[init] + BYTE_OFFSET;
4046 Return the initial_address in INITIAL_ADDRESS.
4048 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4049 update the pointer in each iteration of the loop.
4051 Return the increment stmt that updates the pointer in PTR_INCR.
4053 3. Set INV_P to true if the access pattern of the data reference in the
4054 vectorized loop is invariant. Set it to false otherwise.
4056 4. Return the pointer. */
4058 tree
4059 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
4060 tree offset, tree *initial_address,
4061 gimple_stmt_iterator *gsi, gimple *ptr_incr,
4062 bool only_init, bool *inv_p, tree byte_offset)
4064 const char *base_name;
4065 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4066 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4067 struct loop *loop = NULL;
4068 bool nested_in_vect_loop = false;
4069 struct loop *containing_loop = NULL;
4070 tree aggr_ptr_type;
4071 tree aggr_ptr;
4072 tree new_temp;
4073 gimple vec_stmt;
4074 gimple_seq new_stmt_list = NULL;
4075 edge pe = NULL;
4076 basic_block new_bb;
4077 tree aggr_ptr_init;
4078 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4079 tree aptr;
4080 gimple_stmt_iterator incr_gsi;
4081 bool insert_after;
4082 tree indx_before_incr, indx_after_incr;
4083 gimple incr;
4084 tree step;
4085 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4087 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4088 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4090 if (loop_vinfo)
4092 loop = LOOP_VINFO_LOOP (loop_vinfo);
4093 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4094 containing_loop = (gimple_bb (stmt))->loop_father;
4095 pe = loop_preheader_edge (loop);
4097 else
4099 gcc_assert (bb_vinfo);
4100 only_init = true;
4101 *ptr_incr = NULL;
4104 /* Check the step (evolution) of the load in LOOP, and record
4105 whether it's invariant. */
4106 if (nested_in_vect_loop)
4107 step = STMT_VINFO_DR_STEP (stmt_info);
4108 else
4109 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4111 if (integer_zerop (step))
4112 *inv_p = true;
4113 else
4114 *inv_p = false;
4116 /* Create an expression for the first address accessed by this load
4117 in LOOP. */
4118 base_name = get_name (DR_BASE_ADDRESS (dr));
4120 if (dump_enabled_p ())
4122 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4123 dump_printf_loc (MSG_NOTE, vect_location,
4124 "create %s-pointer variable to type: ",
4125 get_tree_code_name (TREE_CODE (aggr_type)));
4126 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4127 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4128 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4129 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4130 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4131 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4132 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4133 else
4134 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4135 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4136 dump_printf (MSG_NOTE, "\n");
4139 /* (1) Create the new aggregate-pointer variable.
4140 Vector and array types inherit the alias set of their component
4141 type by default so we need to use a ref-all pointer if the data
4142 reference does not conflict with the created aggregated data
4143 reference because it is not addressable. */
4144 bool need_ref_all = false;
4145 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4146 get_alias_set (DR_REF (dr))))
4147 need_ref_all = true;
4148 /* Likewise for any of the data references in the stmt group. */
4149 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4151 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4154 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4155 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4156 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4157 get_alias_set (DR_REF (sdr))))
4159 need_ref_all = true;
4160 break;
4162 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4164 while (orig_stmt);
4166 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4167 need_ref_all);
4168 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4171 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4172 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4173 def-use update cycles for the pointer: one relative to the outer-loop
4174 (LOOP), which is what steps (3) and (4) below do. The other is relative
4175 to the inner-loop (which is the inner-most loop containing the dataref),
4176 and this is done be step (5) below.
4178 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4179 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4180 redundant. Steps (3),(4) create the following:
4182 vp0 = &base_addr;
4183 LOOP: vp1 = phi(vp0,vp2)
4186 vp2 = vp1 + step
4187 goto LOOP
4189 If there is an inner-loop nested in loop, then step (5) will also be
4190 applied, and an additional update in the inner-loop will be created:
4192 vp0 = &base_addr;
4193 LOOP: vp1 = phi(vp0,vp2)
4195 inner: vp3 = phi(vp1,vp4)
4196 vp4 = vp3 + inner_step
4197 if () goto inner
4199 vp2 = vp1 + step
4200 if () goto LOOP */
4202 /* (2) Calculate the initial address of the aggregate-pointer, and set
4203 the aggregate-pointer to point to it before the loop. */
4205 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4207 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4208 offset, loop, byte_offset);
4209 if (new_stmt_list)
4211 if (pe)
4213 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4214 gcc_assert (!new_bb);
4216 else
4217 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4220 *initial_address = new_temp;
4222 /* Create: p = (aggr_type *) initial_base */
4223 if (TREE_CODE (new_temp) != SSA_NAME
4224 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4226 vec_stmt = gimple_build_assign (aggr_ptr,
4227 fold_convert (aggr_ptr_type, new_temp));
4228 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4229 /* Copy the points-to information if it exists. */
4230 if (DR_PTR_INFO (dr))
4231 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
4232 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4233 if (pe)
4235 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4236 gcc_assert (!new_bb);
4238 else
4239 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4241 else
4242 aggr_ptr_init = new_temp;
4244 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4245 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4246 inner-loop nested in LOOP (during outer-loop vectorization). */
4248 /* No update in loop is required. */
4249 if (only_init && (!loop_vinfo || at_loop == loop))
4250 aptr = aggr_ptr_init;
4251 else
4253 /* The step of the aggregate pointer is the type size. */
4254 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4255 /* One exception to the above is when the scalar step of the load in
4256 LOOP is zero. In this case the step here is also zero. */
4257 if (*inv_p)
4258 iv_step = size_zero_node;
4259 else if (tree_int_cst_sgn (step) == -1)
4260 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4262 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4264 create_iv (aggr_ptr_init,
4265 fold_convert (aggr_ptr_type, iv_step),
4266 aggr_ptr, loop, &incr_gsi, insert_after,
4267 &indx_before_incr, &indx_after_incr);
4268 incr = gsi_stmt (incr_gsi);
4269 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4271 /* Copy the points-to information if it exists. */
4272 if (DR_PTR_INFO (dr))
4274 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4275 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4277 if (ptr_incr)
4278 *ptr_incr = incr;
4280 aptr = indx_before_incr;
4283 if (!nested_in_vect_loop || only_init)
4284 return aptr;
4287 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4288 nested in LOOP, if exists. */
4290 gcc_assert (nested_in_vect_loop);
4291 if (!only_init)
4293 standard_iv_increment_position (containing_loop, &incr_gsi,
4294 &insert_after);
4295 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4296 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4297 &indx_after_incr);
4298 incr = gsi_stmt (incr_gsi);
4299 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4301 /* Copy the points-to information if it exists. */
4302 if (DR_PTR_INFO (dr))
4304 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4305 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4307 if (ptr_incr)
4308 *ptr_incr = incr;
4310 return indx_before_incr;
4312 else
4313 gcc_unreachable ();
4317 /* Function bump_vector_ptr
4319 Increment a pointer (to a vector type) by vector-size. If requested,
4320 i.e. if PTR-INCR is given, then also connect the new increment stmt
4321 to the existing def-use update-chain of the pointer, by modifying
4322 the PTR_INCR as illustrated below:
4324 The pointer def-use update-chain before this function:
4325 DATAREF_PTR = phi (p_0, p_2)
4326 ....
4327 PTR_INCR: p_2 = DATAREF_PTR + step
4329 The pointer def-use update-chain after this function:
4330 DATAREF_PTR = phi (p_0, p_2)
4331 ....
4332 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4333 ....
4334 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4336 Input:
4337 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4338 in the loop.
4339 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4340 the loop. The increment amount across iterations is expected
4341 to be vector_size.
4342 BSI - location where the new update stmt is to be placed.
4343 STMT - the original scalar memory-access stmt that is being vectorized.
4344 BUMP - optional. The offset by which to bump the pointer. If not given,
4345 the offset is assumed to be vector_size.
4347 Output: Return NEW_DATAREF_PTR as illustrated above.
4351 tree
4352 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4353 gimple stmt, tree bump)
4355 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4356 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4357 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4358 tree update = TYPE_SIZE_UNIT (vectype);
4359 gassign *incr_stmt;
4360 ssa_op_iter iter;
4361 use_operand_p use_p;
4362 tree new_dataref_ptr;
4364 if (bump)
4365 update = bump;
4367 new_dataref_ptr = copy_ssa_name (dataref_ptr);
4368 incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
4369 dataref_ptr, update);
4370 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4372 /* Copy the points-to information if it exists. */
4373 if (DR_PTR_INFO (dr))
4375 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4376 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4379 if (!ptr_incr)
4380 return new_dataref_ptr;
4382 /* Update the vector-pointer's cross-iteration increment. */
4383 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4385 tree use = USE_FROM_PTR (use_p);
4387 if (use == dataref_ptr)
4388 SET_USE (use_p, new_dataref_ptr);
4389 else
4390 gcc_assert (tree_int_cst_compare (use, update) == 0);
4393 return new_dataref_ptr;
4397 /* Function vect_create_destination_var.
4399 Create a new temporary of type VECTYPE. */
4401 tree
4402 vect_create_destination_var (tree scalar_dest, tree vectype)
4404 tree vec_dest;
4405 const char *name;
4406 char *new_name;
4407 tree type;
4408 enum vect_var_kind kind;
4410 kind = vectype ? vect_simple_var : vect_scalar_var;
4411 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4413 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4415 name = get_name (scalar_dest);
4416 if (name)
4417 new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4418 else
4419 new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
4420 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4421 free (new_name);
4423 return vec_dest;
4426 /* Function vect_grouped_store_supported.
4428 Returns TRUE if interleave high and interleave low permutations
4429 are supported, and FALSE otherwise. */
4431 bool
4432 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4434 machine_mode mode = TYPE_MODE (vectype);
4436 /* vect_permute_store_chain requires the group size to be equal to 3 or
4437 be a power of two. */
4438 if (count != 3 && exact_log2 (count) == -1)
4440 if (dump_enabled_p ())
4441 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4442 "the size of the group of accesses"
4443 " is not a power of 2 or not eqaul to 3\n");
4444 return false;
4447 /* Check that the permutation is supported. */
4448 if (VECTOR_MODE_P (mode))
4450 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4451 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4453 if (count == 3)
4455 unsigned int j0 = 0, j1 = 0, j2 = 0;
4456 unsigned int i, j;
4458 for (j = 0; j < 3; j++)
4460 int nelt0 = ((3 - j) * nelt) % 3;
4461 int nelt1 = ((3 - j) * nelt + 1) % 3;
4462 int nelt2 = ((3 - j) * nelt + 2) % 3;
4463 for (i = 0; i < nelt; i++)
4465 if (3 * i + nelt0 < nelt)
4466 sel[3 * i + nelt0] = j0++;
4467 if (3 * i + nelt1 < nelt)
4468 sel[3 * i + nelt1] = nelt + j1++;
4469 if (3 * i + nelt2 < nelt)
4470 sel[3 * i + nelt2] = 0;
4472 if (!can_vec_perm_p (mode, false, sel))
4474 if (dump_enabled_p ())
4475 dump_printf (MSG_MISSED_OPTIMIZATION,
4476 "permutaion op not supported by target.\n");
4477 return false;
4480 for (i = 0; i < nelt; i++)
4482 if (3 * i + nelt0 < nelt)
4483 sel[3 * i + nelt0] = 3 * i + nelt0;
4484 if (3 * i + nelt1 < nelt)
4485 sel[3 * i + nelt1] = 3 * i + nelt1;
4486 if (3 * i + nelt2 < nelt)
4487 sel[3 * i + nelt2] = nelt + j2++;
4489 if (!can_vec_perm_p (mode, false, sel))
4491 if (dump_enabled_p ())
4492 dump_printf (MSG_MISSED_OPTIMIZATION,
4493 "permutaion op not supported by target.\n");
4494 return false;
4497 return true;
4499 else
4501 /* If length is not equal to 3 then only power of 2 is supported. */
4502 gcc_assert (exact_log2 (count) != -1);
4504 for (i = 0; i < nelt / 2; i++)
4506 sel[i * 2] = i;
4507 sel[i * 2 + 1] = i + nelt;
4509 if (can_vec_perm_p (mode, false, sel))
4511 for (i = 0; i < nelt; i++)
4512 sel[i] += nelt / 2;
4513 if (can_vec_perm_p (mode, false, sel))
4514 return true;
4519 if (dump_enabled_p ())
4520 dump_printf (MSG_MISSED_OPTIMIZATION,
4521 "permutaion op not supported by target.\n");
4522 return false;
4526 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4527 type VECTYPE. */
4529 bool
4530 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4532 return vect_lanes_optab_supported_p ("vec_store_lanes",
4533 vec_store_lanes_optab,
4534 vectype, count);
4538 /* Function vect_permute_store_chain.
4540 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4541 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4542 the data correctly for the stores. Return the final references for stores
4543 in RESULT_CHAIN.
4545 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4546 The input is 4 vectors each containing 8 elements. We assign a number to
4547 each element, the input sequence is:
4549 1st vec: 0 1 2 3 4 5 6 7
4550 2nd vec: 8 9 10 11 12 13 14 15
4551 3rd vec: 16 17 18 19 20 21 22 23
4552 4th vec: 24 25 26 27 28 29 30 31
4554 The output sequence should be:
4556 1st vec: 0 8 16 24 1 9 17 25
4557 2nd vec: 2 10 18 26 3 11 19 27
4558 3rd vec: 4 12 20 28 5 13 21 30
4559 4th vec: 6 14 22 30 7 15 23 31
4561 i.e., we interleave the contents of the four vectors in their order.
4563 We use interleave_high/low instructions to create such output. The input of
4564 each interleave_high/low operation is two vectors:
4565 1st vec 2nd vec
4566 0 1 2 3 4 5 6 7
4567 the even elements of the result vector are obtained left-to-right from the
4568 high/low elements of the first vector. The odd elements of the result are
4569 obtained left-to-right from the high/low elements of the second vector.
4570 The output of interleave_high will be: 0 4 1 5
4571 and of interleave_low: 2 6 3 7
4574 The permutation is done in log LENGTH stages. In each stage interleave_high
4575 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4576 where the first argument is taken from the first half of DR_CHAIN and the
4577 second argument from it's second half.
4578 In our example,
4580 I1: interleave_high (1st vec, 3rd vec)
4581 I2: interleave_low (1st vec, 3rd vec)
4582 I3: interleave_high (2nd vec, 4th vec)
4583 I4: interleave_low (2nd vec, 4th vec)
4585 The output for the first stage is:
4587 I1: 0 16 1 17 2 18 3 19
4588 I2: 4 20 5 21 6 22 7 23
4589 I3: 8 24 9 25 10 26 11 27
4590 I4: 12 28 13 29 14 30 15 31
4592 The output of the second stage, i.e. the final result is:
4594 I1: 0 8 16 24 1 9 17 25
4595 I2: 2 10 18 26 3 11 19 27
4596 I3: 4 12 20 28 5 13 21 30
4597 I4: 6 14 22 30 7 15 23 31. */
4599 void
4600 vect_permute_store_chain (vec<tree> dr_chain,
4601 unsigned int length,
4602 gimple stmt,
4603 gimple_stmt_iterator *gsi,
4604 vec<tree> *result_chain)
4606 tree vect1, vect2, high, low;
4607 gimple perm_stmt;
4608 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4609 tree perm_mask_low, perm_mask_high;
4610 tree data_ref;
4611 tree perm3_mask_low, perm3_mask_high;
4612 unsigned int i, n, log_length = exact_log2 (length);
4613 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4614 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4616 result_chain->quick_grow (length);
4617 memcpy (result_chain->address (), dr_chain.address (),
4618 length * sizeof (tree));
4620 if (length == 3)
4622 unsigned int j0 = 0, j1 = 0, j2 = 0;
4624 for (j = 0; j < 3; j++)
4626 int nelt0 = ((3 - j) * nelt) % 3;
4627 int nelt1 = ((3 - j) * nelt + 1) % 3;
4628 int nelt2 = ((3 - j) * nelt + 2) % 3;
4630 for (i = 0; i < nelt; i++)
4632 if (3 * i + nelt0 < nelt)
4633 sel[3 * i + nelt0] = j0++;
4634 if (3 * i + nelt1 < nelt)
4635 sel[3 * i + nelt1] = nelt + j1++;
4636 if (3 * i + nelt2 < nelt)
4637 sel[3 * i + nelt2] = 0;
4639 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4641 for (i = 0; i < nelt; i++)
4643 if (3 * i + nelt0 < nelt)
4644 sel[3 * i + nelt0] = 3 * i + nelt0;
4645 if (3 * i + nelt1 < nelt)
4646 sel[3 * i + nelt1] = 3 * i + nelt1;
4647 if (3 * i + nelt2 < nelt)
4648 sel[3 * i + nelt2] = nelt + j2++;
4650 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4652 vect1 = dr_chain[0];
4653 vect2 = dr_chain[1];
4655 /* Create interleaving stmt:
4656 low = VEC_PERM_EXPR <vect1, vect2,
4657 {j, nelt, *, j + 1, nelt + j + 1, *,
4658 j + 2, nelt + j + 2, *, ...}> */
4659 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4660 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4661 vect2, perm3_mask_low);
4662 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4664 vect1 = data_ref;
4665 vect2 = dr_chain[2];
4666 /* Create interleaving stmt:
4667 low = VEC_PERM_EXPR <vect1, vect2,
4668 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4669 6, 7, nelt + j + 2, ...}> */
4670 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4671 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
4672 vect2, perm3_mask_high);
4673 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4674 (*result_chain)[j] = data_ref;
4677 else
4679 /* If length is not equal to 3 then only power of 2 is supported. */
4680 gcc_assert (exact_log2 (length) != -1);
4682 for (i = 0, n = nelt / 2; i < n; i++)
4684 sel[i * 2] = i;
4685 sel[i * 2 + 1] = i + nelt;
4687 perm_mask_high = vect_gen_perm_mask_checked (vectype, sel);
4689 for (i = 0; i < nelt; i++)
4690 sel[i] += nelt / 2;
4691 perm_mask_low = vect_gen_perm_mask_checked (vectype, sel);
4693 for (i = 0, n = log_length; i < n; i++)
4695 for (j = 0; j < length/2; j++)
4697 vect1 = dr_chain[j];
4698 vect2 = dr_chain[j+length/2];
4700 /* Create interleaving stmt:
4701 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4702 ...}> */
4703 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4704 perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
4705 vect2, perm_mask_high);
4706 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4707 (*result_chain)[2*j] = high;
4709 /* Create interleaving stmt:
4710 low = VEC_PERM_EXPR <vect1, vect2,
4711 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4712 ...}> */
4713 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4714 perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
4715 vect2, perm_mask_low);
4716 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4717 (*result_chain)[2*j+1] = low;
4719 memcpy (dr_chain.address (), result_chain->address (),
4720 length * sizeof (tree));
4725 /* Function vect_setup_realignment
4727 This function is called when vectorizing an unaligned load using
4728 the dr_explicit_realign[_optimized] scheme.
4729 This function generates the following code at the loop prolog:
4731 p = initial_addr;
4732 x msq_init = *(floor(p)); # prolog load
4733 realignment_token = call target_builtin;
4734 loop:
4735 x msq = phi (msq_init, ---)
4737 The stmts marked with x are generated only for the case of
4738 dr_explicit_realign_optimized.
4740 The code above sets up a new (vector) pointer, pointing to the first
4741 location accessed by STMT, and a "floor-aligned" load using that pointer.
4742 It also generates code to compute the "realignment-token" (if the relevant
4743 target hook was defined), and creates a phi-node at the loop-header bb
4744 whose arguments are the result of the prolog-load (created by this
4745 function) and the result of a load that takes place in the loop (to be
4746 created by the caller to this function).
4748 For the case of dr_explicit_realign_optimized:
4749 The caller to this function uses the phi-result (msq) to create the
4750 realignment code inside the loop, and sets up the missing phi argument,
4751 as follows:
4752 loop:
4753 msq = phi (msq_init, lsq)
4754 lsq = *(floor(p')); # load in loop
4755 result = realign_load (msq, lsq, realignment_token);
4757 For the case of dr_explicit_realign:
4758 loop:
4759 msq = *(floor(p)); # load in loop
4760 p' = p + (VS-1);
4761 lsq = *(floor(p')); # load in loop
4762 result = realign_load (msq, lsq, realignment_token);
4764 Input:
4765 STMT - (scalar) load stmt to be vectorized. This load accesses
4766 a memory location that may be unaligned.
4767 BSI - place where new code is to be inserted.
4768 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4769 is used.
4771 Output:
4772 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4773 target hook, if defined.
4774 Return value - the result of the loop-header phi node. */
4776 tree
4777 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4778 tree *realignment_token,
4779 enum dr_alignment_support alignment_support_scheme,
4780 tree init_addr,
4781 struct loop **at_loop)
4783 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4784 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4785 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4786 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4787 struct loop *loop = NULL;
4788 edge pe = NULL;
4789 tree scalar_dest = gimple_assign_lhs (stmt);
4790 tree vec_dest;
4791 gimple inc;
4792 tree ptr;
4793 tree data_ref;
4794 basic_block new_bb;
4795 tree msq_init = NULL_TREE;
4796 tree new_temp;
4797 gphi *phi_stmt;
4798 tree msq = NULL_TREE;
4799 gimple_seq stmts = NULL;
4800 bool inv_p;
4801 bool compute_in_loop = false;
4802 bool nested_in_vect_loop = false;
4803 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4804 struct loop *loop_for_initial_load = NULL;
4806 if (loop_vinfo)
4808 loop = LOOP_VINFO_LOOP (loop_vinfo);
4809 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4812 gcc_assert (alignment_support_scheme == dr_explicit_realign
4813 || alignment_support_scheme == dr_explicit_realign_optimized);
4815 /* We need to generate three things:
4816 1. the misalignment computation
4817 2. the extra vector load (for the optimized realignment scheme).
4818 3. the phi node for the two vectors from which the realignment is
4819 done (for the optimized realignment scheme). */
4821 /* 1. Determine where to generate the misalignment computation.
4823 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4824 calculation will be generated by this function, outside the loop (in the
4825 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4826 caller, inside the loop.
4828 Background: If the misalignment remains fixed throughout the iterations of
4829 the loop, then both realignment schemes are applicable, and also the
4830 misalignment computation can be done outside LOOP. This is because we are
4831 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4832 are a multiple of VS (the Vector Size), and therefore the misalignment in
4833 different vectorized LOOP iterations is always the same.
4834 The problem arises only if the memory access is in an inner-loop nested
4835 inside LOOP, which is now being vectorized using outer-loop vectorization.
4836 This is the only case when the misalignment of the memory access may not
4837 remain fixed throughout the iterations of the inner-loop (as explained in
4838 detail in vect_supportable_dr_alignment). In this case, not only is the
4839 optimized realignment scheme not applicable, but also the misalignment
4840 computation (and generation of the realignment token that is passed to
4841 REALIGN_LOAD) have to be done inside the loop.
4843 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4844 or not, which in turn determines if the misalignment is computed inside
4845 the inner-loop, or outside LOOP. */
4847 if (init_addr != NULL_TREE || !loop_vinfo)
4849 compute_in_loop = true;
4850 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4854 /* 2. Determine where to generate the extra vector load.
4856 For the optimized realignment scheme, instead of generating two vector
4857 loads in each iteration, we generate a single extra vector load in the
4858 preheader of the loop, and in each iteration reuse the result of the
4859 vector load from the previous iteration. In case the memory access is in
4860 an inner-loop nested inside LOOP, which is now being vectorized using
4861 outer-loop vectorization, we need to determine whether this initial vector
4862 load should be generated at the preheader of the inner-loop, or can be
4863 generated at the preheader of LOOP. If the memory access has no evolution
4864 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4865 to be generated inside LOOP (in the preheader of the inner-loop). */
4867 if (nested_in_vect_loop)
4869 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4870 bool invariant_in_outerloop =
4871 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4872 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4874 else
4875 loop_for_initial_load = loop;
4876 if (at_loop)
4877 *at_loop = loop_for_initial_load;
4879 if (loop_for_initial_load)
4880 pe = loop_preheader_edge (loop_for_initial_load);
4882 /* 3. For the case of the optimized realignment, create the first vector
4883 load at the loop preheader. */
4885 if (alignment_support_scheme == dr_explicit_realign_optimized)
4887 /* Create msq_init = *(floor(p1)) in the loop preheader */
4888 gassign *new_stmt;
4890 gcc_assert (!compute_in_loop);
4891 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4892 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4893 NULL_TREE, &init_addr, NULL, &inc,
4894 true, &inv_p);
4895 new_temp = copy_ssa_name (ptr);
4896 new_stmt = gimple_build_assign
4897 (new_temp, BIT_AND_EXPR, ptr,
4898 build_int_cst (TREE_TYPE (ptr),
4899 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4900 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4901 gcc_assert (!new_bb);
4902 data_ref
4903 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4904 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4905 new_stmt = gimple_build_assign (vec_dest, data_ref);
4906 new_temp = make_ssa_name (vec_dest, new_stmt);
4907 gimple_assign_set_lhs (new_stmt, new_temp);
4908 if (pe)
4910 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4911 gcc_assert (!new_bb);
4913 else
4914 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4916 msq_init = gimple_assign_lhs (new_stmt);
4919 /* 4. Create realignment token using a target builtin, if available.
4920 It is done either inside the containing loop, or before LOOP (as
4921 determined above). */
4923 if (targetm.vectorize.builtin_mask_for_load)
4925 gcall *new_stmt;
4926 tree builtin_decl;
4928 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4929 if (!init_addr)
4931 /* Generate the INIT_ADDR computation outside LOOP. */
4932 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4933 NULL_TREE, loop);
4934 if (loop)
4936 pe = loop_preheader_edge (loop);
4937 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4938 gcc_assert (!new_bb);
4940 else
4941 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4944 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4945 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4946 vec_dest =
4947 vect_create_destination_var (scalar_dest,
4948 gimple_call_return_type (new_stmt));
4949 new_temp = make_ssa_name (vec_dest, new_stmt);
4950 gimple_call_set_lhs (new_stmt, new_temp);
4952 if (compute_in_loop)
4953 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4954 else
4956 /* Generate the misalignment computation outside LOOP. */
4957 pe = loop_preheader_edge (loop);
4958 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4959 gcc_assert (!new_bb);
4962 *realignment_token = gimple_call_lhs (new_stmt);
4964 /* The result of the CALL_EXPR to this builtin is determined from
4965 the value of the parameter and no global variables are touched
4966 which makes the builtin a "const" function. Requiring the
4967 builtin to have the "const" attribute makes it unnecessary
4968 to call mark_call_clobbered. */
4969 gcc_assert (TREE_READONLY (builtin_decl));
4972 if (alignment_support_scheme == dr_explicit_realign)
4973 return msq;
4975 gcc_assert (!compute_in_loop);
4976 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4979 /* 5. Create msq = phi <msq_init, lsq> in loop */
4981 pe = loop_preheader_edge (containing_loop);
4982 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4983 msq = make_ssa_name (vec_dest);
4984 phi_stmt = create_phi_node (msq, containing_loop->header);
4985 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4987 return msq;
4991 /* Function vect_grouped_load_supported.
4993 Returns TRUE if even and odd permutations are supported,
4994 and FALSE otherwise. */
4996 bool
4997 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4999 machine_mode mode = TYPE_MODE (vectype);
5001 /* vect_permute_load_chain requires the group size to be equal to 3 or
5002 be a power of two. */
5003 if (count != 3 && exact_log2 (count) == -1)
5005 if (dump_enabled_p ())
5006 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5007 "the size of the group of accesses"
5008 " is not a power of 2 or not equal to 3\n");
5009 return false;
5012 /* Check that the permutation is supported. */
5013 if (VECTOR_MODE_P (mode))
5015 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
5016 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5018 if (count == 3)
5020 unsigned int k;
5021 for (k = 0; k < 3; k++)
5023 for (i = 0; i < nelt; i++)
5024 if (3 * i + k < 2 * nelt)
5025 sel[i] = 3 * i + k;
5026 else
5027 sel[i] = 0;
5028 if (!can_vec_perm_p (mode, false, sel))
5030 if (dump_enabled_p ())
5031 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5032 "shuffle of 3 loads is not supported by"
5033 " target\n");
5034 return false;
5036 for (i = 0, j = 0; i < nelt; i++)
5037 if (3 * i + k < 2 * nelt)
5038 sel[i] = i;
5039 else
5040 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5041 if (!can_vec_perm_p (mode, false, sel))
5043 if (dump_enabled_p ())
5044 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5045 "shuffle of 3 loads is not supported by"
5046 " target\n");
5047 return false;
5050 return true;
5052 else
5054 /* If length is not equal to 3 then only power of 2 is supported. */
5055 gcc_assert (exact_log2 (count) != -1);
5056 for (i = 0; i < nelt; i++)
5057 sel[i] = i * 2;
5058 if (can_vec_perm_p (mode, false, sel))
5060 for (i = 0; i < nelt; i++)
5061 sel[i] = i * 2 + 1;
5062 if (can_vec_perm_p (mode, false, sel))
5063 return true;
5068 if (dump_enabled_p ())
5069 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5070 "extract even/odd not supported by target\n");
5071 return false;
5074 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5075 type VECTYPE. */
5077 bool
5078 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5080 return vect_lanes_optab_supported_p ("vec_load_lanes",
5081 vec_load_lanes_optab,
5082 vectype, count);
5085 /* Function vect_permute_load_chain.
5087 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5088 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5089 the input data correctly. Return the final references for loads in
5090 RESULT_CHAIN.
5092 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5093 The input is 4 vectors each containing 8 elements. We assign a number to each
5094 element, the input sequence is:
5096 1st vec: 0 1 2 3 4 5 6 7
5097 2nd vec: 8 9 10 11 12 13 14 15
5098 3rd vec: 16 17 18 19 20 21 22 23
5099 4th vec: 24 25 26 27 28 29 30 31
5101 The output sequence should be:
5103 1st vec: 0 4 8 12 16 20 24 28
5104 2nd vec: 1 5 9 13 17 21 25 29
5105 3rd vec: 2 6 10 14 18 22 26 30
5106 4th vec: 3 7 11 15 19 23 27 31
5108 i.e., the first output vector should contain the first elements of each
5109 interleaving group, etc.
5111 We use extract_even/odd instructions to create such output. The input of
5112 each extract_even/odd operation is two vectors
5113 1st vec 2nd vec
5114 0 1 2 3 4 5 6 7
5116 and the output is the vector of extracted even/odd elements. The output of
5117 extract_even will be: 0 2 4 6
5118 and of extract_odd: 1 3 5 7
5121 The permutation is done in log LENGTH stages. In each stage extract_even
5122 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5123 their order. In our example,
5125 E1: extract_even (1st vec, 2nd vec)
5126 E2: extract_odd (1st vec, 2nd vec)
5127 E3: extract_even (3rd vec, 4th vec)
5128 E4: extract_odd (3rd vec, 4th vec)
5130 The output for the first stage will be:
5132 E1: 0 2 4 6 8 10 12 14
5133 E2: 1 3 5 7 9 11 13 15
5134 E3: 16 18 20 22 24 26 28 30
5135 E4: 17 19 21 23 25 27 29 31
5137 In order to proceed and create the correct sequence for the next stage (or
5138 for the correct output, if the second stage is the last one, as in our
5139 example), we first put the output of extract_even operation and then the
5140 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5141 The input for the second stage is:
5143 1st vec (E1): 0 2 4 6 8 10 12 14
5144 2nd vec (E3): 16 18 20 22 24 26 28 30
5145 3rd vec (E2): 1 3 5 7 9 11 13 15
5146 4th vec (E4): 17 19 21 23 25 27 29 31
5148 The output of the second stage:
5150 E1: 0 4 8 12 16 20 24 28
5151 E2: 2 6 10 14 18 22 26 30
5152 E3: 1 5 9 13 17 21 25 29
5153 E4: 3 7 11 15 19 23 27 31
5155 And RESULT_CHAIN after reordering:
5157 1st vec (E1): 0 4 8 12 16 20 24 28
5158 2nd vec (E3): 1 5 9 13 17 21 25 29
5159 3rd vec (E2): 2 6 10 14 18 22 26 30
5160 4th vec (E4): 3 7 11 15 19 23 27 31. */
5162 static void
5163 vect_permute_load_chain (vec<tree> dr_chain,
5164 unsigned int length,
5165 gimple stmt,
5166 gimple_stmt_iterator *gsi,
5167 vec<tree> *result_chain)
5169 tree data_ref, first_vect, second_vect;
5170 tree perm_mask_even, perm_mask_odd;
5171 tree perm3_mask_low, perm3_mask_high;
5172 gimple perm_stmt;
5173 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5174 unsigned int i, j, log_length = exact_log2 (length);
5175 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5176 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5178 result_chain->quick_grow (length);
5179 memcpy (result_chain->address (), dr_chain.address (),
5180 length * sizeof (tree));
5182 if (length == 3)
5184 unsigned int k;
5186 for (k = 0; k < 3; k++)
5188 for (i = 0; i < nelt; i++)
5189 if (3 * i + k < 2 * nelt)
5190 sel[i] = 3 * i + k;
5191 else
5192 sel[i] = 0;
5193 perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
5195 for (i = 0, j = 0; i < nelt; i++)
5196 if (3 * i + k < 2 * nelt)
5197 sel[i] = i;
5198 else
5199 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5201 perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
5203 first_vect = dr_chain[0];
5204 second_vect = dr_chain[1];
5206 /* Create interleaving stmt (low part of):
5207 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5208 ...}> */
5209 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
5210 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5211 second_vect, perm3_mask_low);
5212 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5214 /* Create interleaving stmt (high part of):
5215 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5216 ...}> */
5217 first_vect = data_ref;
5218 second_vect = dr_chain[2];
5219 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
5220 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
5221 second_vect, perm3_mask_high);
5222 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5223 (*result_chain)[k] = data_ref;
5226 else
5228 /* If length is not equal to 3 then only power of 2 is supported. */
5229 gcc_assert (exact_log2 (length) != -1);
5231 for (i = 0; i < nelt; ++i)
5232 sel[i] = i * 2;
5233 perm_mask_even = vect_gen_perm_mask_checked (vectype, sel);
5235 for (i = 0; i < nelt; ++i)
5236 sel[i] = i * 2 + 1;
5237 perm_mask_odd = vect_gen_perm_mask_checked (vectype, sel);
5239 for (i = 0; i < log_length; i++)
5241 for (j = 0; j < length; j += 2)
5243 first_vect = dr_chain[j];
5244 second_vect = dr_chain[j+1];
5246 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5247 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5248 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5249 first_vect, second_vect,
5250 perm_mask_even);
5251 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5252 (*result_chain)[j/2] = data_ref;
5254 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5255 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5256 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5257 first_vect, second_vect,
5258 perm_mask_odd);
5259 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5260 (*result_chain)[j/2+length/2] = data_ref;
5262 memcpy (dr_chain.address (), result_chain->address (),
5263 length * sizeof (tree));
5268 /* Function vect_shift_permute_load_chain.
5270 Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
5271 sequence of stmts to reorder the input data accordingly.
5272 Return the final references for loads in RESULT_CHAIN.
5273 Return true if successed, false otherwise.
5275 E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
5276 The input is 3 vectors each containing 8 elements. We assign a
5277 number to each element, the input sequence is:
5279 1st vec: 0 1 2 3 4 5 6 7
5280 2nd vec: 8 9 10 11 12 13 14 15
5281 3rd vec: 16 17 18 19 20 21 22 23
5283 The output sequence should be:
5285 1st vec: 0 3 6 9 12 15 18 21
5286 2nd vec: 1 4 7 10 13 16 19 22
5287 3rd vec: 2 5 8 11 14 17 20 23
5289 We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
5291 First we shuffle all 3 vectors to get correct elements order:
5293 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
5294 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
5295 3rd vec: (16 19 22) (17 20 23) (18 21)
5297 Next we unite and shift vector 3 times:
5299 1st step:
5300 shift right by 6 the concatenation of:
5301 "1st vec" and "2nd vec"
5302 ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
5303 "2nd vec" and "3rd vec"
5304 ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
5305 "3rd vec" and "1st vec"
5306 (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
5307 | New vectors |
5309 So that now new vectors are:
5311 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
5312 2nd vec: (10 13) (16 19 22) (17 20 23)
5313 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
5315 2nd step:
5316 shift right by 5 the concatenation of:
5317 "1st vec" and "3rd vec"
5318 ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
5319 "2nd vec" and "1st vec"
5320 (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
5321 "3rd vec" and "2nd vec"
5322 (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
5323 | New vectors |
5325 So that now new vectors are:
5327 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
5328 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
5329 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
5331 3rd step:
5332 shift right by 5 the concatenation of:
5333 "1st vec" and "1st vec"
5334 ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
5335 shift right by 3 the concatenation of:
5336 "2nd vec" and "2nd vec"
5337 (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
5338 | New vectors |
5340 So that now all vectors are READY:
5341 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
5342 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
5343 3rd vec: ( 1 4 7) (10 13) (16 19 22)
5345 This algorithm is faster than one in vect_permute_load_chain if:
5346 1. "shift of a concatination" is faster than general permutation.
5347 This is usually so.
5348 2. The TARGET machine can't execute vector instructions in parallel.
5349 This is because each step of the algorithm depends on previous.
5350 The algorithm in vect_permute_load_chain is much more parallel.
5352 The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
5355 static bool
5356 vect_shift_permute_load_chain (vec<tree> dr_chain,
5357 unsigned int length,
5358 gimple stmt,
5359 gimple_stmt_iterator *gsi,
5360 vec<tree> *result_chain)
5362 tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
5363 tree perm2_mask1, perm2_mask2, perm3_mask;
5364 tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
5365 gimple perm_stmt;
5367 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5368 unsigned int i;
5369 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5370 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5371 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5372 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5374 result_chain->quick_grow (length);
5375 memcpy (result_chain->address (), dr_chain.address (),
5376 length * sizeof (tree));
5378 if (exact_log2 (length) != -1 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
5380 unsigned int j, log_length = exact_log2 (length);
5381 for (i = 0; i < nelt / 2; ++i)
5382 sel[i] = i * 2;
5383 for (i = 0; i < nelt / 2; ++i)
5384 sel[nelt / 2 + i] = i * 2 + 1;
5385 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5387 if (dump_enabled_p ())
5388 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5389 "shuffle of 2 fields structure is not \
5390 supported by target\n");
5391 return false;
5393 perm2_mask1 = vect_gen_perm_mask_checked (vectype, sel);
5395 for (i = 0; i < nelt / 2; ++i)
5396 sel[i] = i * 2 + 1;
5397 for (i = 0; i < nelt / 2; ++i)
5398 sel[nelt / 2 + i] = i * 2;
5399 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5401 if (dump_enabled_p ())
5402 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5403 "shuffle of 2 fields structure is not \
5404 supported by target\n");
5405 return false;
5407 perm2_mask2 = vect_gen_perm_mask_checked (vectype, sel);
5409 /* Generating permutation constant to shift all elements.
5410 For vector length 8 it is {4 5 6 7 8 9 10 11}. */
5411 for (i = 0; i < nelt; i++)
5412 sel[i] = nelt / 2 + i;
5413 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5415 if (dump_enabled_p ())
5416 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5417 "shift permutation is not supported by target\n");
5418 return false;
5420 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5422 /* Generating permutation constant to select vector from 2.
5423 For vector length 8 it is {0 1 2 3 12 13 14 15}. */
5424 for (i = 0; i < nelt / 2; i++)
5425 sel[i] = i;
5426 for (i = nelt / 2; i < nelt; i++)
5427 sel[i] = nelt + i;
5428 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5430 if (dump_enabled_p ())
5431 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5432 "select is not supported by target\n");
5433 return false;
5435 select_mask = vect_gen_perm_mask_checked (vectype, sel);
5437 for (i = 0; i < log_length; i++)
5439 for (j = 0; j < length; j += 2)
5441 first_vect = dr_chain[j];
5442 second_vect = dr_chain[j + 1];
5444 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5445 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5446 first_vect, first_vect,
5447 perm2_mask1);
5448 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5449 vect[0] = data_ref;
5451 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
5452 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5453 second_vect, second_vect,
5454 perm2_mask2);
5455 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5456 vect[1] = data_ref;
5458 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
5459 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5460 vect[0], vect[1], shift1_mask);
5461 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5462 (*result_chain)[j/2 + length/2] = data_ref;
5464 data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
5465 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5466 vect[0], vect[1], select_mask);
5467 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5468 (*result_chain)[j/2] = data_ref;
5470 memcpy (dr_chain.address (), result_chain->address (),
5471 length * sizeof (tree));
5473 return true;
5475 if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
5477 unsigned int k = 0, l = 0;
5479 /* Generating permutation constant to get all elements in rigth order.
5480 For vector length 8 it is {0 3 6 1 4 7 2 5}. */
5481 for (i = 0; i < nelt; i++)
5483 if (3 * k + (l % 3) >= nelt)
5485 k = 0;
5486 l += (3 - (nelt % 3));
5488 sel[i] = 3 * k + (l % 3);
5489 k++;
5491 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5493 if (dump_enabled_p ())
5494 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5495 "shuffle of 3 fields structure is not \
5496 supported by target\n");
5497 return false;
5499 perm3_mask = vect_gen_perm_mask_checked (vectype, sel);
5501 /* Generating permutation constant to shift all elements.
5502 For vector length 8 it is {6 7 8 9 10 11 12 13}. */
5503 for (i = 0; i < nelt; i++)
5504 sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
5505 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5507 if (dump_enabled_p ())
5508 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5509 "shift permutation is not supported by target\n");
5510 return false;
5512 shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
5514 /* Generating permutation constant to shift all elements.
5515 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5516 for (i = 0; i < nelt; i++)
5517 sel[i] = 2 * (nelt / 3) + 1 + i;
5518 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5520 if (dump_enabled_p ())
5521 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5522 "shift permutation is not supported by target\n");
5523 return false;
5525 shift2_mask = vect_gen_perm_mask_checked (vectype, sel);
5527 /* Generating permutation constant to shift all elements.
5528 For vector length 8 it is {3 4 5 6 7 8 9 10}. */
5529 for (i = 0; i < nelt; i++)
5530 sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
5531 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5533 if (dump_enabled_p ())
5534 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5535 "shift permutation is not supported by target\n");
5536 return false;
5538 shift3_mask = vect_gen_perm_mask_checked (vectype, sel);
5540 /* Generating permutation constant to shift all elements.
5541 For vector length 8 it is {5 6 7 8 9 10 11 12}. */
5542 for (i = 0; i < nelt; i++)
5543 sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
5544 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5546 if (dump_enabled_p ())
5547 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5548 "shift permutation is not supported by target\n");
5549 return false;
5551 shift4_mask = vect_gen_perm_mask_checked (vectype, sel);
5553 for (k = 0; k < 3; k++)
5555 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
5556 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5557 dr_chain[k], dr_chain[k],
5558 perm3_mask);
5559 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5560 vect[k] = data_ref;
5563 for (k = 0; k < 3; k++)
5565 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
5566 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5567 vect[k % 3], vect[(k + 1) % 3],
5568 shift1_mask);
5569 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5570 vect_shift[k] = data_ref;
5573 for (k = 0; k < 3; k++)
5575 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
5576 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
5577 vect_shift[(4 - k) % 3],
5578 vect_shift[(3 - k) % 3],
5579 shift2_mask);
5580 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5581 vect[k] = data_ref;
5584 (*result_chain)[3 - (nelt % 3)] = vect[2];
5586 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
5587 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
5588 vect[0], shift3_mask);
5589 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5590 (*result_chain)[nelt % 3] = data_ref;
5592 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
5593 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
5594 vect[1], shift4_mask);
5595 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5596 (*result_chain)[0] = data_ref;
5597 return true;
5599 return false;
5602 /* Function vect_transform_grouped_load.
5604 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5605 to perform their permutation and ascribe the result vectorized statements to
5606 the scalar statements.
5609 void
5610 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
5611 gimple_stmt_iterator *gsi)
5613 machine_mode mode;
5614 vec<tree> result_chain = vNULL;
5616 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5617 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5618 vectors, that are ready for vector computation. */
5619 result_chain.create (size);
5621 /* If reassociation width for vector type is 2 or greater target machine can
5622 execute 2 or more vector instructions in parallel. Otherwise try to
5623 get chain for loads group using vect_shift_permute_load_chain. */
5624 mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
5625 if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
5626 || exact_log2 (size) != -1
5627 || !vect_shift_permute_load_chain (dr_chain, size, stmt,
5628 gsi, &result_chain))
5629 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5630 vect_record_grouped_load_vectors (stmt, result_chain);
5631 result_chain.release ();
5634 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5635 generated as part of the vectorization of STMT. Assign the statement
5636 for each vector to the associated scalar statement. */
5638 void
5639 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
5641 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5642 gimple next_stmt, new_stmt;
5643 unsigned int i, gap_count;
5644 tree tmp_data_ref;
5646 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5647 Since we scan the chain starting from it's first node, their order
5648 corresponds the order of data-refs in RESULT_CHAIN. */
5649 next_stmt = first_stmt;
5650 gap_count = 1;
5651 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5653 if (!next_stmt)
5654 break;
5656 /* Skip the gaps. Loads created for the gaps will be removed by dead
5657 code elimination pass later. No need to check for the first stmt in
5658 the group, since it always exists.
5659 GROUP_GAP is the number of steps in elements from the previous
5660 access (if there is no gap GROUP_GAP is 1). We skip loads that
5661 correspond to the gaps. */
5662 if (next_stmt != first_stmt
5663 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5665 gap_count++;
5666 continue;
5669 while (next_stmt)
5671 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5672 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5673 copies, and we put the new vector statement in the first available
5674 RELATED_STMT. */
5675 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5676 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5677 else
5679 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5681 gimple prev_stmt =
5682 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5683 gimple rel_stmt =
5684 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5685 while (rel_stmt)
5687 prev_stmt = rel_stmt;
5688 rel_stmt =
5689 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5692 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5693 new_stmt;
5697 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5698 gap_count = 1;
5699 /* If NEXT_STMT accesses the same DR as the previous statement,
5700 put the same TMP_DATA_REF as its vectorized statement; otherwise
5701 get the next data-ref from RESULT_CHAIN. */
5702 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5703 break;
5708 /* Function vect_force_dr_alignment_p.
5710 Returns whether the alignment of a DECL can be forced to be aligned
5711 on ALIGNMENT bit boundary. */
5713 bool
5714 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5716 if (TREE_CODE (decl) != VAR_DECL)
5717 return false;
5719 /* With -fno-toplevel-reorder we may have already output the constant. */
5720 if (TREE_ASM_WRITTEN (decl))
5721 return false;
5723 /* Constant pool entries may be shared and not properly merged by LTO. */
5724 if (DECL_IN_CONSTANT_POOL (decl))
5725 return false;
5727 if (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
5729 symtab_node *snode;
5731 /* We cannot change alignment of symbols that may bind to symbols
5732 in other translation unit that may contain a definition with lower
5733 alignment. */
5734 if (!decl_binds_to_current_def_p (decl))
5735 return false;
5737 /* When compiling partition, be sure the symbol is not output by other
5738 partition. */
5739 snode = symtab_node::get (decl);
5740 if (flag_ltrans
5741 && (snode->in_other_partition
5742 || snode->get_partitioning_class () == SYMBOL_DUPLICATE))
5743 return false;
5746 /* Do not override the alignment as specified by the ABI when the used
5747 attribute is set. */
5748 if (DECL_PRESERVE_P (decl))
5749 return false;
5751 /* Do not override explicit alignment set by the user when an explicit
5752 section name is also used. This is a common idiom used by many
5753 software projects. */
5754 if (TREE_STATIC (decl)
5755 && DECL_SECTION_NAME (decl) != NULL
5756 && !symtab_node::get (decl)->implicit_section)
5757 return false;
5759 /* If symbol is an alias, we need to check that target is OK. */
5760 if (TREE_STATIC (decl))
5762 tree target = symtab_node::get (decl)->ultimate_alias_target ()->decl;
5763 if (target != decl)
5765 if (DECL_PRESERVE_P (target))
5766 return false;
5767 decl = target;
5771 if (TREE_STATIC (decl))
5772 return (alignment <= MAX_OFILE_ALIGNMENT);
5773 else
5774 return (alignment <= MAX_STACK_ALIGNMENT);
5778 /* Return whether the data reference DR is supported with respect to its
5779 alignment.
5780 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5781 it is aligned, i.e., check if it is possible to vectorize it with different
5782 alignment. */
5784 enum dr_alignment_support
5785 vect_supportable_dr_alignment (struct data_reference *dr,
5786 bool check_aligned_accesses)
5788 gimple stmt = DR_STMT (dr);
5789 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5790 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5791 machine_mode mode = TYPE_MODE (vectype);
5792 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5793 struct loop *vect_loop = NULL;
5794 bool nested_in_vect_loop = false;
5796 if (aligned_access_p (dr) && !check_aligned_accesses)
5797 return dr_aligned;
5799 /* For now assume all conditional loads/stores support unaligned
5800 access without any special code. */
5801 if (is_gimple_call (stmt)
5802 && gimple_call_internal_p (stmt)
5803 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
5804 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
5805 return dr_unaligned_supported;
5807 if (loop_vinfo)
5809 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5810 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5813 /* Possibly unaligned access. */
5815 /* We can choose between using the implicit realignment scheme (generating
5816 a misaligned_move stmt) and the explicit realignment scheme (generating
5817 aligned loads with a REALIGN_LOAD). There are two variants to the
5818 explicit realignment scheme: optimized, and unoptimized.
5819 We can optimize the realignment only if the step between consecutive
5820 vector loads is equal to the vector size. Since the vector memory
5821 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5822 is guaranteed that the misalignment amount remains the same throughout the
5823 execution of the vectorized loop. Therefore, we can create the
5824 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5825 at the loop preheader.
5827 However, in the case of outer-loop vectorization, when vectorizing a
5828 memory access in the inner-loop nested within the LOOP that is now being
5829 vectorized, while it is guaranteed that the misalignment of the
5830 vectorized memory access will remain the same in different outer-loop
5831 iterations, it is *not* guaranteed that is will remain the same throughout
5832 the execution of the inner-loop. This is because the inner-loop advances
5833 with the original scalar step (and not in steps of VS). If the inner-loop
5834 step happens to be a multiple of VS, then the misalignment remains fixed
5835 and we can use the optimized realignment scheme. For example:
5837 for (i=0; i<N; i++)
5838 for (j=0; j<M; j++)
5839 s += a[i+j];
5841 When vectorizing the i-loop in the above example, the step between
5842 consecutive vector loads is 1, and so the misalignment does not remain
5843 fixed across the execution of the inner-loop, and the realignment cannot
5844 be optimized (as illustrated in the following pseudo vectorized loop):
5846 for (i=0; i<N; i+=4)
5847 for (j=0; j<M; j++){
5848 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5849 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5850 // (assuming that we start from an aligned address).
5853 We therefore have to use the unoptimized realignment scheme:
5855 for (i=0; i<N; i+=4)
5856 for (j=k; j<M; j+=4)
5857 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5858 // that the misalignment of the initial address is
5859 // 0).
5861 The loop can then be vectorized as follows:
5863 for (k=0; k<4; k++){
5864 rt = get_realignment_token (&vp[k]);
5865 for (i=0; i<N; i+=4){
5866 v1 = vp[i+k];
5867 for (j=k; j<M; j+=4){
5868 v2 = vp[i+j+VS-1];
5869 va = REALIGN_LOAD <v1,v2,rt>;
5870 vs += va;
5871 v1 = v2;
5874 } */
5876 if (DR_IS_READ (dr))
5878 bool is_packed = false;
5879 tree type = (TREE_TYPE (DR_REF (dr)));
5881 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5882 && (!targetm.vectorize.builtin_mask_for_load
5883 || targetm.vectorize.builtin_mask_for_load ()))
5885 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5886 if ((nested_in_vect_loop
5887 && (TREE_INT_CST_LOW (DR_STEP (dr))
5888 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5889 || !loop_vinfo)
5890 return dr_explicit_realign;
5891 else
5892 return dr_explicit_realign_optimized;
5894 if (!known_alignment_for_access_p (dr))
5895 is_packed = not_size_aligned (DR_REF (dr));
5897 if ((TYPE_USER_ALIGN (type) && !is_packed)
5898 || targetm.vectorize.
5899 support_vector_misalignment (mode, type,
5900 DR_MISALIGNMENT (dr), is_packed))
5901 /* Can't software pipeline the loads, but can at least do them. */
5902 return dr_unaligned_supported;
5904 else
5906 bool is_packed = false;
5907 tree type = (TREE_TYPE (DR_REF (dr)));
5909 if (!known_alignment_for_access_p (dr))
5910 is_packed = not_size_aligned (DR_REF (dr));
5912 if ((TYPE_USER_ALIGN (type) && !is_packed)
5913 || targetm.vectorize.
5914 support_vector_misalignment (mode, type,
5915 DR_MISALIGNMENT (dr), is_packed))
5916 return dr_unaligned_supported;
5919 /* Unsupported. */
5920 return dr_unaligned_unsupported;