Fix dot dump bug
[official-gcc.git] / gcc / tree-vect-data-refs.c
blobb2b629c03c666052877fd0e52a44f9cf105f7e65
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "tm_p.h"
30 #include "target.h"
31 #include "basic-block.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "tree-eh.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-ssa-loop-ivopts.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-ssa-loop.h"
50 #include "dumpfile.h"
51 #include "cfgloop.h"
52 #include "tree-chrec.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-vectorizer.h"
55 #include "diagnostic-core.h"
56 #include "cgraph.h"
57 /* Need to include rtl.h, expr.h, etc. for optabs. */
58 #include "expr.h"
59 #include "optabs.h"
60 #include "builtins.h"
61 #include "varasm.h"
63 /* Return true if load- or store-lanes optab OPTAB is implemented for
64 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
66 static bool
67 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
68 tree vectype, unsigned HOST_WIDE_INT count)
70 enum machine_mode mode, array_mode;
71 bool limit_p;
73 mode = TYPE_MODE (vectype);
74 limit_p = !targetm.array_mode_supported_p (mode, count);
75 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
76 MODE_INT, limit_p);
78 if (array_mode == BLKmode)
80 if (dump_enabled_p ())
81 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
82 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
83 GET_MODE_NAME (mode), count);
84 return false;
87 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
89 if (dump_enabled_p ())
90 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
91 "cannot use %s<%s><%s>\n", name,
92 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
93 return false;
96 if (dump_enabled_p ())
97 dump_printf_loc (MSG_NOTE, vect_location,
98 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
99 GET_MODE_NAME (mode));
101 return true;
105 /* Return the smallest scalar part of STMT.
106 This is used to determine the vectype of the stmt. We generally set the
107 vectype according to the type of the result (lhs). For stmts whose
108 result-type is different than the type of the arguments (e.g., demotion,
109 promotion), vectype will be reset appropriately (later). Note that we have
110 to visit the smallest datatype in this function, because that determines the
111 VF. If the smallest datatype in the loop is present only as the rhs of a
112 promotion operation - we'd miss it.
113 Such a case, where a variable of this datatype does not appear in the lhs
114 anywhere in the loop, can only occur if it's an invariant: e.g.:
115 'int_x = (int) short_inv', which we'd expect to have been optimized away by
116 invariant motion. However, we cannot rely on invariant motion to always
117 take invariants out of the loop, and so in the case of promotion we also
118 have to check the rhs.
119 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
120 types. */
122 tree
123 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
124 HOST_WIDE_INT *rhs_size_unit)
126 tree scalar_type = gimple_expr_type (stmt);
127 HOST_WIDE_INT lhs, rhs;
129 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
131 if (is_gimple_assign (stmt)
132 && (gimple_assign_cast_p (stmt)
133 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
134 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
135 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
137 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
139 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
140 if (rhs < lhs)
141 scalar_type = rhs_type;
144 *lhs_size_unit = lhs;
145 *rhs_size_unit = rhs;
146 return scalar_type;
150 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
151 tested at run-time. Return TRUE if DDR was successfully inserted.
152 Return false if versioning is not supported. */
154 static bool
155 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
157 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
159 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
160 return false;
162 if (dump_enabled_p ())
164 dump_printf_loc (MSG_NOTE, vect_location,
165 "mark for run-time aliasing test between ");
166 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
167 dump_printf (MSG_NOTE, " and ");
168 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
169 dump_printf (MSG_NOTE, "\n");
172 if (optimize_loop_nest_for_size_p (loop))
174 if (dump_enabled_p ())
175 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
176 "versioning not supported when optimizing"
177 " for size.\n");
178 return false;
181 /* FORNOW: We don't support versioning with outer-loop vectorization. */
182 if (loop->inner)
184 if (dump_enabled_p ())
185 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
186 "versioning not yet supported for outer-loops.\n");
187 return false;
190 /* FORNOW: We don't support creating runtime alias tests for non-constant
191 step. */
192 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
193 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
195 if (dump_enabled_p ())
196 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
197 "versioning not yet supported for non-constant "
198 "step\n");
199 return false;
202 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
203 return true;
207 /* Function vect_analyze_data_ref_dependence.
209 Return TRUE if there (might) exist a dependence between a memory-reference
210 DRA and a memory-reference DRB. When versioning for alias may check a
211 dependence at run-time, return FALSE. Adjust *MAX_VF according to
212 the data dependence. */
214 static bool
215 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
216 loop_vec_info loop_vinfo, int *max_vf)
218 unsigned int i;
219 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
220 struct data_reference *dra = DDR_A (ddr);
221 struct data_reference *drb = DDR_B (ddr);
222 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
223 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
224 lambda_vector dist_v;
225 unsigned int loop_depth;
227 /* In loop analysis all data references should be vectorizable. */
228 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
229 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
230 gcc_unreachable ();
232 /* Independent data accesses. */
233 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
234 return false;
236 if (dra == drb
237 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
238 return false;
240 /* Even if we have an anti-dependence then, as the vectorized loop covers at
241 least two scalar iterations, there is always also a true dependence.
242 As the vectorizer does not re-order loads and stores we can ignore
243 the anti-dependence if TBAA can disambiguate both DRs similar to the
244 case with known negative distance anti-dependences (positive
245 distance anti-dependences would violate TBAA constraints). */
246 if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
247 || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
248 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
249 get_alias_set (DR_REF (drb))))
250 return false;
252 /* Unknown data dependence. */
253 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
255 /* If user asserted safelen consecutive iterations can be
256 executed concurrently, assume independence. */
257 if (loop->safelen >= 2)
259 if (loop->safelen < *max_vf)
260 *max_vf = loop->safelen;
261 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
262 return false;
265 if (STMT_VINFO_GATHER_P (stmtinfo_a)
266 || STMT_VINFO_GATHER_P (stmtinfo_b))
268 if (dump_enabled_p ())
270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
271 "versioning for alias not supported for: "
272 "can't determine dependence between ");
273 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
274 DR_REF (dra));
275 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
276 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
277 DR_REF (drb));
278 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
280 return true;
283 if (dump_enabled_p ())
285 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
286 "versioning for alias required: "
287 "can't determine dependence between ");
288 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
289 DR_REF (dra));
290 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
291 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
292 DR_REF (drb));
293 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
296 /* Add to list of ddrs that need to be tested at run-time. */
297 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
300 /* Known data dependence. */
301 if (DDR_NUM_DIST_VECTS (ddr) == 0)
303 /* If user asserted safelen consecutive iterations can be
304 executed concurrently, assume independence. */
305 if (loop->safelen >= 2)
307 if (loop->safelen < *max_vf)
308 *max_vf = loop->safelen;
309 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
310 return false;
313 if (STMT_VINFO_GATHER_P (stmtinfo_a)
314 || STMT_VINFO_GATHER_P (stmtinfo_b))
316 if (dump_enabled_p ())
318 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
319 "versioning for alias not supported for: "
320 "bad dist vector for ");
321 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
322 DR_REF (dra));
323 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
324 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
325 DR_REF (drb));
326 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
328 return true;
331 if (dump_enabled_p ())
333 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
334 "versioning for alias required: "
335 "bad dist vector for ");
336 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
337 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
338 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
339 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
341 /* Add to list of ddrs that need to be tested at run-time. */
342 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
345 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
346 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
348 int dist = dist_v[loop_depth];
350 if (dump_enabled_p ())
351 dump_printf_loc (MSG_NOTE, vect_location,
352 "dependence distance = %d.\n", dist);
354 if (dist == 0)
356 if (dump_enabled_p ())
358 dump_printf_loc (MSG_NOTE, vect_location,
359 "dependence distance == 0 between ");
360 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
361 dump_printf (MSG_NOTE, " and ");
362 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
363 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
366 /* When we perform grouped accesses and perform implicit CSE
367 by detecting equal accesses and doing disambiguation with
368 runtime alias tests like for
369 .. = a[i];
370 .. = a[i+1];
371 a[i] = ..;
372 a[i+1] = ..;
373 *p = ..;
374 .. = a[i];
375 .. = a[i+1];
376 where we will end up loading { a[i], a[i+1] } once, make
377 sure that inserting group loads before the first load and
378 stores after the last store will do the right thing. */
379 if ((STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
380 && GROUP_SAME_DR_STMT (stmtinfo_a))
381 || (STMT_VINFO_GROUPED_ACCESS (stmtinfo_b)
382 && GROUP_SAME_DR_STMT (stmtinfo_b)))
384 gimple earlier_stmt;
385 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
386 if (DR_IS_WRITE
387 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
389 if (dump_enabled_p ())
390 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
391 "READ_WRITE dependence in interleaving."
392 "\n");
393 return true;
397 continue;
400 if (dist > 0 && DDR_REVERSED_P (ddr))
402 /* If DDR_REVERSED_P the order of the data-refs in DDR was
403 reversed (to make distance vector positive), and the actual
404 distance is negative. */
405 if (dump_enabled_p ())
406 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
407 "dependence distance negative.\n");
408 /* Record a negative dependence distance to later limit the
409 amount of stmt copying / unrolling we can perform.
410 Only need to handle read-after-write dependence. */
411 if (DR_IS_READ (drb)
412 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
413 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
414 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
415 continue;
418 if (abs (dist) >= 2
419 && abs (dist) < *max_vf)
421 /* The dependence distance requires reduction of the maximal
422 vectorization factor. */
423 *max_vf = abs (dist);
424 if (dump_enabled_p ())
425 dump_printf_loc (MSG_NOTE, vect_location,
426 "adjusting maximal vectorization factor to %i\n",
427 *max_vf);
430 if (abs (dist) >= *max_vf)
432 /* Dependence distance does not create dependence, as far as
433 vectorization is concerned, in this case. */
434 if (dump_enabled_p ())
435 dump_printf_loc (MSG_NOTE, vect_location,
436 "dependence distance >= VF.\n");
437 continue;
440 if (dump_enabled_p ())
442 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
443 "not vectorized, possible dependence "
444 "between data-refs ");
445 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
446 dump_printf (MSG_NOTE, " and ");
447 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
448 dump_printf (MSG_NOTE, "\n");
451 return true;
454 return false;
457 /* Function vect_analyze_data_ref_dependences.
459 Examine all the data references in the loop, and make sure there do not
460 exist any data dependences between them. Set *MAX_VF according to
461 the maximum vectorization factor the data dependences allow. */
463 bool
464 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
466 unsigned int i;
467 struct data_dependence_relation *ddr;
469 if (dump_enabled_p ())
470 dump_printf_loc (MSG_NOTE, vect_location,
471 "=== vect_analyze_data_ref_dependences ===\n");
473 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
474 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
475 &LOOP_VINFO_DDRS (loop_vinfo),
476 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
477 return false;
479 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
480 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
481 return false;
483 return true;
487 /* Function vect_slp_analyze_data_ref_dependence.
489 Return TRUE if there (might) exist a dependence between a memory-reference
490 DRA and a memory-reference DRB. When versioning for alias may check a
491 dependence at run-time, return FALSE. Adjust *MAX_VF according to
492 the data dependence. */
494 static bool
495 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
497 struct data_reference *dra = DDR_A (ddr);
498 struct data_reference *drb = DDR_B (ddr);
500 /* We need to check dependences of statements marked as unvectorizable
501 as well, they still can prohibit vectorization. */
503 /* Independent data accesses. */
504 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
505 return false;
507 if (dra == drb)
508 return false;
510 /* Read-read is OK. */
511 if (DR_IS_READ (dra) && DR_IS_READ (drb))
512 return false;
514 /* If dra and drb are part of the same interleaving chain consider
515 them independent. */
516 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
517 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
518 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
519 return false;
521 /* Unknown data dependence. */
522 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
524 if (dump_enabled_p ())
526 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
527 "can't determine dependence between ");
528 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
529 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
530 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
531 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
534 else if (dump_enabled_p ())
536 dump_printf_loc (MSG_NOTE, vect_location,
537 "determined dependence between ");
538 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
539 dump_printf (MSG_NOTE, " and ");
540 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
541 dump_printf (MSG_NOTE, "\n");
544 /* We do not vectorize basic blocks with write-write dependencies. */
545 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
546 return true;
548 /* If we have a read-write dependence check that the load is before the store.
549 When we vectorize basic blocks, vector load can be only before
550 corresponding scalar load, and vector store can be only after its
551 corresponding scalar store. So the order of the acceses is preserved in
552 case the load is before the store. */
553 gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
554 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
556 /* That only holds for load-store pairs taking part in vectorization. */
557 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
558 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
559 return false;
562 return true;
566 /* Function vect_analyze_data_ref_dependences.
568 Examine all the data references in the basic-block, and make sure there
569 do not exist any data dependences between them. Set *MAX_VF according to
570 the maximum vectorization factor the data dependences allow. */
572 bool
573 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
575 struct data_dependence_relation *ddr;
576 unsigned int i;
578 if (dump_enabled_p ())
579 dump_printf_loc (MSG_NOTE, vect_location,
580 "=== vect_slp_analyze_data_ref_dependences ===\n");
582 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
583 &BB_VINFO_DDRS (bb_vinfo),
584 vNULL, true))
585 return false;
587 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
588 if (vect_slp_analyze_data_ref_dependence (ddr))
589 return false;
591 return true;
595 /* Function vect_compute_data_ref_alignment
597 Compute the misalignment of the data reference DR.
599 Output:
600 1. If during the misalignment computation it is found that the data reference
601 cannot be vectorized then false is returned.
602 2. DR_MISALIGNMENT (DR) is defined.
604 FOR NOW: No analysis is actually performed. Misalignment is calculated
605 only for trivial cases. TODO. */
607 static bool
608 vect_compute_data_ref_alignment (struct data_reference *dr)
610 gimple stmt = DR_STMT (dr);
611 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
612 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
613 struct loop *loop = NULL;
614 tree ref = DR_REF (dr);
615 tree vectype;
616 tree base, base_addr;
617 bool base_aligned;
618 tree misalign;
619 tree aligned_to, alignment;
621 if (dump_enabled_p ())
622 dump_printf_loc (MSG_NOTE, vect_location,
623 "vect_compute_data_ref_alignment:\n");
625 if (loop_vinfo)
626 loop = LOOP_VINFO_LOOP (loop_vinfo);
628 /* Initialize misalignment to unknown. */
629 SET_DR_MISALIGNMENT (dr, -1);
631 /* Strided loads perform only component accesses, misalignment information
632 is irrelevant for them. */
633 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
634 return true;
636 misalign = DR_INIT (dr);
637 aligned_to = DR_ALIGNED_TO (dr);
638 base_addr = DR_BASE_ADDRESS (dr);
639 vectype = STMT_VINFO_VECTYPE (stmt_info);
641 /* In case the dataref is in an inner-loop of the loop that is being
642 vectorized (LOOP), we use the base and misalignment information
643 relative to the outer-loop (LOOP). This is ok only if the misalignment
644 stays the same throughout the execution of the inner-loop, which is why
645 we have to check that the stride of the dataref in the inner-loop evenly
646 divides by the vector size. */
647 if (loop && nested_in_vect_loop_p (loop, stmt))
649 tree step = DR_STEP (dr);
650 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
652 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
654 if (dump_enabled_p ())
655 dump_printf_loc (MSG_NOTE, vect_location,
656 "inner step divides the vector-size.\n");
657 misalign = STMT_VINFO_DR_INIT (stmt_info);
658 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
659 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
661 else
663 if (dump_enabled_p ())
664 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
665 "inner step doesn't divide the vector-size.\n");
666 misalign = NULL_TREE;
670 /* Similarly, if we're doing basic-block vectorization, we can only use
671 base and misalignment information relative to an innermost loop if the
672 misalignment stays the same throughout the execution of the loop.
673 As above, this is the case if the stride of the dataref evenly divides
674 by the vector size. */
675 if (!loop)
677 tree step = DR_STEP (dr);
678 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
680 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
682 if (dump_enabled_p ())
683 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
684 "SLP: step doesn't divide the vector-size.\n");
685 misalign = NULL_TREE;
689 base = build_fold_indirect_ref (base_addr);
690 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
692 if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
693 || !misalign)
695 if (dump_enabled_p ())
697 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
698 "Unknown alignment for access: ");
699 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, base);
700 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
702 return true;
705 if ((DECL_P (base)
706 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
707 alignment) >= 0)
708 || (TREE_CODE (base_addr) == SSA_NAME
709 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
710 TREE_TYPE (base_addr)))),
711 alignment) >= 0)
712 || (get_pointer_alignment (base_addr) >= TYPE_ALIGN (vectype)))
713 base_aligned = true;
714 else
715 base_aligned = false;
717 if (!base_aligned)
719 /* Do not change the alignment of global variables here if
720 flag_section_anchors is enabled as we already generated
721 RTL for other functions. Most global variables should
722 have been aligned during the IPA increase_alignment pass. */
723 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
724 || (TREE_STATIC (base) && flag_section_anchors))
726 if (dump_enabled_p ())
728 dump_printf_loc (MSG_NOTE, vect_location,
729 "can't force alignment of ref: ");
730 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
731 dump_printf (MSG_NOTE, "\n");
733 return true;
736 /* Force the alignment of the decl.
737 NOTE: This is the only change to the code we make during
738 the analysis phase, before deciding to vectorize the loop. */
739 if (dump_enabled_p ())
741 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
742 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
743 dump_printf (MSG_NOTE, "\n");
746 ((dataref_aux *)dr->aux)->base_decl = base;
747 ((dataref_aux *)dr->aux)->base_misaligned = true;
750 /* If this is a backward running DR then first access in the larger
751 vectype actually is N-1 elements before the address in the DR.
752 Adjust misalign accordingly. */
753 if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
755 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
756 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
757 otherwise we wouldn't be here. */
758 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
759 /* PLUS because DR_STEP was negative. */
760 misalign = size_binop (PLUS_EXPR, misalign, offset);
763 /* Modulo alignment. */
764 misalign = size_binop (FLOOR_MOD_EXPR, misalign, alignment);
766 if (!tree_fits_uhwi_p (misalign))
768 /* Negative or overflowed misalignment value. */
769 if (dump_enabled_p ())
770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
771 "unexpected misalign value\n");
772 return false;
775 SET_DR_MISALIGNMENT (dr, tree_to_uhwi (misalign));
777 if (dump_enabled_p ())
779 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
780 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
781 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
782 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
785 return true;
789 /* Function vect_compute_data_refs_alignment
791 Compute the misalignment of data references in the loop.
792 Return FALSE if a data reference is found that cannot be vectorized. */
794 static bool
795 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
796 bb_vec_info bb_vinfo)
798 vec<data_reference_p> datarefs;
799 struct data_reference *dr;
800 unsigned int i;
802 if (loop_vinfo)
803 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
804 else
805 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
807 FOR_EACH_VEC_ELT (datarefs, i, dr)
808 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
809 && !vect_compute_data_ref_alignment (dr))
811 if (bb_vinfo)
813 /* Mark unsupported statement as unvectorizable. */
814 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
815 continue;
817 else
818 return false;
821 return true;
825 /* Function vect_update_misalignment_for_peel
827 DR - the data reference whose misalignment is to be adjusted.
828 DR_PEEL - the data reference whose misalignment is being made
829 zero in the vector loop by the peel.
830 NPEEL - the number of iterations in the peel loop if the misalignment
831 of DR_PEEL is known at compile time. */
833 static void
834 vect_update_misalignment_for_peel (struct data_reference *dr,
835 struct data_reference *dr_peel, int npeel)
837 unsigned int i;
838 vec<dr_p> same_align_drs;
839 struct data_reference *current_dr;
840 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
841 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
842 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
843 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
845 /* For interleaved data accesses the step in the loop must be multiplied by
846 the size of the interleaving group. */
847 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
848 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
849 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
850 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
852 /* It can be assumed that the data refs with the same alignment as dr_peel
853 are aligned in the vector loop. */
854 same_align_drs
855 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
856 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
858 if (current_dr != dr)
859 continue;
860 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
861 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
862 SET_DR_MISALIGNMENT (dr, 0);
863 return;
866 if (known_alignment_for_access_p (dr)
867 && known_alignment_for_access_p (dr_peel))
869 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
870 int misal = DR_MISALIGNMENT (dr);
871 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
872 misal += negative ? -npeel * dr_size : npeel * dr_size;
873 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
874 SET_DR_MISALIGNMENT (dr, misal);
875 return;
878 if (dump_enabled_p ())
879 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
880 SET_DR_MISALIGNMENT (dr, -1);
884 /* Function vect_verify_datarefs_alignment
886 Return TRUE if all data references in the loop can be
887 handled with respect to alignment. */
889 bool
890 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
892 vec<data_reference_p> datarefs;
893 struct data_reference *dr;
894 enum dr_alignment_support supportable_dr_alignment;
895 unsigned int i;
897 if (loop_vinfo)
898 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
899 else
900 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
902 FOR_EACH_VEC_ELT (datarefs, i, dr)
904 gimple stmt = DR_STMT (dr);
905 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
907 if (!STMT_VINFO_RELEVANT_P (stmt_info))
908 continue;
910 /* For interleaving, only the alignment of the first access matters.
911 Skip statements marked as not vectorizable. */
912 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
913 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
914 || !STMT_VINFO_VECTORIZABLE (stmt_info))
915 continue;
917 /* Strided loads perform only component accesses, alignment is
918 irrelevant for them. */
919 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
920 continue;
922 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
923 if (!supportable_dr_alignment)
925 if (dump_enabled_p ())
927 if (DR_IS_READ (dr))
928 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
929 "not vectorized: unsupported unaligned load.");
930 else
931 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
932 "not vectorized: unsupported unaligned "
933 "store.");
935 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
936 DR_REF (dr));
937 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
939 return false;
941 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
942 dump_printf_loc (MSG_NOTE, vect_location,
943 "Vectorizing an unaligned access.\n");
945 return true;
948 /* Given an memory reference EXP return whether its alignment is less
949 than its size. */
951 static bool
952 not_size_aligned (tree exp)
954 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
955 return true;
957 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
958 > get_object_alignment (exp));
961 /* Function vector_alignment_reachable_p
963 Return true if vector alignment for DR is reachable by peeling
964 a few loop iterations. Return false otherwise. */
966 static bool
967 vector_alignment_reachable_p (struct data_reference *dr)
969 gimple stmt = DR_STMT (dr);
970 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
971 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
973 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
975 /* For interleaved access we peel only if number of iterations in
976 the prolog loop ({VF - misalignment}), is a multiple of the
977 number of the interleaved accesses. */
978 int elem_size, mis_in_elements;
979 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
981 /* FORNOW: handle only known alignment. */
982 if (!known_alignment_for_access_p (dr))
983 return false;
985 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
986 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
988 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
989 return false;
992 /* If misalignment is known at the compile time then allow peeling
993 only if natural alignment is reachable through peeling. */
994 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
996 HOST_WIDE_INT elmsize =
997 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
998 if (dump_enabled_p ())
1000 dump_printf_loc (MSG_NOTE, vect_location,
1001 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1002 dump_printf (MSG_NOTE,
1003 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1005 if (DR_MISALIGNMENT (dr) % elmsize)
1007 if (dump_enabled_p ())
1008 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1009 "data size does not divide the misalignment.\n");
1010 return false;
1014 if (!known_alignment_for_access_p (dr))
1016 tree type = TREE_TYPE (DR_REF (dr));
1017 bool is_packed = not_size_aligned (DR_REF (dr));
1018 if (dump_enabled_p ())
1019 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1020 "Unknown misalignment, is_packed = %d\n",is_packed);
1021 if ((TYPE_USER_ALIGN (type) && !is_packed)
1022 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1023 return true;
1024 else
1025 return false;
1028 return true;
1032 /* Calculate the cost of the memory access represented by DR. */
1034 static void
1035 vect_get_data_access_cost (struct data_reference *dr,
1036 unsigned int *inside_cost,
1037 unsigned int *outside_cost,
1038 stmt_vector_for_cost *body_cost_vec)
1040 gimple stmt = DR_STMT (dr);
1041 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1042 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1043 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1044 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1045 int ncopies = vf / nunits;
1047 if (DR_IS_READ (dr))
1048 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1049 NULL, body_cost_vec, false);
1050 else
1051 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1053 if (dump_enabled_p ())
1054 dump_printf_loc (MSG_NOTE, vect_location,
1055 "vect_get_data_access_cost: inside_cost = %d, "
1056 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1060 /* Insert DR into peeling hash table with NPEEL as key. */
1062 static void
1063 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1064 int npeel)
1066 struct _vect_peel_info elem, *slot;
1067 _vect_peel_info **new_slot;
1068 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1070 elem.npeel = npeel;
1071 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find (&elem);
1072 if (slot)
1073 slot->count++;
1074 else
1076 slot = XNEW (struct _vect_peel_info);
1077 slot->npeel = npeel;
1078 slot->dr = dr;
1079 slot->count = 1;
1080 new_slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find_slot (slot, INSERT);
1081 *new_slot = slot;
1084 if (!supportable_dr_alignment
1085 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1086 slot->count += VECT_MAX_COST;
1090 /* Traverse peeling hash table to find peeling option that aligns maximum
1091 number of data accesses. */
1094 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1095 _vect_peel_extended_info *max)
1097 vect_peel_info elem = *slot;
1099 if (elem->count > max->peel_info.count
1100 || (elem->count == max->peel_info.count
1101 && max->peel_info.npeel > elem->npeel))
1103 max->peel_info.npeel = elem->npeel;
1104 max->peel_info.count = elem->count;
1105 max->peel_info.dr = elem->dr;
1108 return 1;
1112 /* Traverse peeling hash table and calculate cost for each peeling option.
1113 Find the one with the lowest cost. */
1116 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1117 _vect_peel_extended_info *min)
1119 vect_peel_info elem = *slot;
1120 int save_misalignment, dummy;
1121 unsigned int inside_cost = 0, outside_cost = 0, i;
1122 gimple stmt = DR_STMT (elem->dr);
1123 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1124 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1125 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1126 struct data_reference *dr;
1127 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1128 int single_iter_cost;
1130 prologue_cost_vec.create (2);
1131 body_cost_vec.create (2);
1132 epilogue_cost_vec.create (2);
1134 FOR_EACH_VEC_ELT (datarefs, i, dr)
1136 stmt = DR_STMT (dr);
1137 stmt_info = vinfo_for_stmt (stmt);
1138 /* For interleaving, only the alignment of the first access
1139 matters. */
1140 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1141 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1142 continue;
1144 save_misalignment = DR_MISALIGNMENT (dr);
1145 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1146 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1147 &body_cost_vec);
1148 SET_DR_MISALIGNMENT (dr, save_misalignment);
1151 single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
1152 outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel,
1153 &dummy, single_iter_cost,
1154 &prologue_cost_vec,
1155 &epilogue_cost_vec);
1157 /* Prologue and epilogue costs are added to the target model later.
1158 These costs depend only on the scalar iteration cost, the
1159 number of peeling iterations finally chosen, and the number of
1160 misaligned statements. So discard the information found here. */
1161 prologue_cost_vec.release ();
1162 epilogue_cost_vec.release ();
1164 if (inside_cost < min->inside_cost
1165 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1167 min->inside_cost = inside_cost;
1168 min->outside_cost = outside_cost;
1169 min->body_cost_vec.release ();
1170 min->body_cost_vec = body_cost_vec;
1171 min->peel_info.dr = elem->dr;
1172 min->peel_info.npeel = elem->npeel;
1174 else
1175 body_cost_vec.release ();
1177 return 1;
1181 /* Choose best peeling option by traversing peeling hash table and either
1182 choosing an option with the lowest cost (if cost model is enabled) or the
1183 option that aligns as many accesses as possible. */
1185 static struct data_reference *
1186 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1187 unsigned int *npeel,
1188 stmt_vector_for_cost *body_cost_vec)
1190 struct _vect_peel_extended_info res;
1192 res.peel_info.dr = NULL;
1193 res.body_cost_vec = stmt_vector_for_cost ();
1195 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1197 res.inside_cost = INT_MAX;
1198 res.outside_cost = INT_MAX;
1199 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1200 .traverse <_vect_peel_extended_info *,
1201 vect_peeling_hash_get_lowest_cost> (&res);
1203 else
1205 res.peel_info.count = 0;
1206 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1207 .traverse <_vect_peel_extended_info *,
1208 vect_peeling_hash_get_most_frequent> (&res);
1211 *npeel = res.peel_info.npeel;
1212 *body_cost_vec = res.body_cost_vec;
1213 return res.peel_info.dr;
1217 /* Function vect_enhance_data_refs_alignment
1219 This pass will use loop versioning and loop peeling in order to enhance
1220 the alignment of data references in the loop.
1222 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1223 original loop is to be vectorized. Any other loops that are created by
1224 the transformations performed in this pass - are not supposed to be
1225 vectorized. This restriction will be relaxed.
1227 This pass will require a cost model to guide it whether to apply peeling
1228 or versioning or a combination of the two. For example, the scheme that
1229 intel uses when given a loop with several memory accesses, is as follows:
1230 choose one memory access ('p') which alignment you want to force by doing
1231 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1232 other accesses are not necessarily aligned, or (2) use loop versioning to
1233 generate one loop in which all accesses are aligned, and another loop in
1234 which only 'p' is necessarily aligned.
1236 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1237 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1238 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1240 Devising a cost model is the most critical aspect of this work. It will
1241 guide us on which access to peel for, whether to use loop versioning, how
1242 many versions to create, etc. The cost model will probably consist of
1243 generic considerations as well as target specific considerations (on
1244 powerpc for example, misaligned stores are more painful than misaligned
1245 loads).
1247 Here are the general steps involved in alignment enhancements:
1249 -- original loop, before alignment analysis:
1250 for (i=0; i<N; i++){
1251 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1252 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1255 -- After vect_compute_data_refs_alignment:
1256 for (i=0; i<N; i++){
1257 x = q[i]; # DR_MISALIGNMENT(q) = 3
1258 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1261 -- Possibility 1: we do loop versioning:
1262 if (p is aligned) {
1263 for (i=0; i<N; i++){ # loop 1A
1264 x = q[i]; # DR_MISALIGNMENT(q) = 3
1265 p[i] = y; # DR_MISALIGNMENT(p) = 0
1268 else {
1269 for (i=0; i<N; i++){ # loop 1B
1270 x = q[i]; # DR_MISALIGNMENT(q) = 3
1271 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1275 -- Possibility 2: we do loop peeling:
1276 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1277 x = q[i];
1278 p[i] = y;
1280 for (i = 3; i < N; i++){ # loop 2A
1281 x = q[i]; # DR_MISALIGNMENT(q) = 0
1282 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1285 -- Possibility 3: combination of loop peeling and versioning:
1286 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1287 x = q[i];
1288 p[i] = y;
1290 if (p is aligned) {
1291 for (i = 3; i<N; i++){ # loop 3A
1292 x = q[i]; # DR_MISALIGNMENT(q) = 0
1293 p[i] = y; # DR_MISALIGNMENT(p) = 0
1296 else {
1297 for (i = 3; i<N; i++){ # loop 3B
1298 x = q[i]; # DR_MISALIGNMENT(q) = 0
1299 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1303 These loops are later passed to loop_transform to be vectorized. The
1304 vectorizer will use the alignment information to guide the transformation
1305 (whether to generate regular loads/stores, or with special handling for
1306 misalignment). */
1308 bool
1309 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1311 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1312 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1313 enum dr_alignment_support supportable_dr_alignment;
1314 struct data_reference *dr0 = NULL, *first_store = NULL;
1315 struct data_reference *dr;
1316 unsigned int i, j;
1317 bool do_peeling = false;
1318 bool do_versioning = false;
1319 bool stat;
1320 gimple stmt;
1321 stmt_vec_info stmt_info;
1322 unsigned int npeel = 0;
1323 bool all_misalignments_unknown = true;
1324 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1325 unsigned possible_npeel_number = 1;
1326 tree vectype;
1327 unsigned int nelements, mis, same_align_drs_max = 0;
1328 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
1330 if (dump_enabled_p ())
1331 dump_printf_loc (MSG_NOTE, vect_location,
1332 "=== vect_enhance_data_refs_alignment ===\n");
1334 /* While cost model enhancements are expected in the future, the high level
1335 view of the code at this time is as follows:
1337 A) If there is a misaligned access then see if peeling to align
1338 this access can make all data references satisfy
1339 vect_supportable_dr_alignment. If so, update data structures
1340 as needed and return true.
1342 B) If peeling wasn't possible and there is a data reference with an
1343 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1344 then see if loop versioning checks can be used to make all data
1345 references satisfy vect_supportable_dr_alignment. If so, update
1346 data structures as needed and return true.
1348 C) If neither peeling nor versioning were successful then return false if
1349 any data reference does not satisfy vect_supportable_dr_alignment.
1351 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1353 Note, Possibility 3 above (which is peeling and versioning together) is not
1354 being done at this time. */
1356 /* (1) Peeling to force alignment. */
1358 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1359 Considerations:
1360 + How many accesses will become aligned due to the peeling
1361 - How many accesses will become unaligned due to the peeling,
1362 and the cost of misaligned accesses.
1363 - The cost of peeling (the extra runtime checks, the increase
1364 in code size). */
1366 FOR_EACH_VEC_ELT (datarefs, i, dr)
1368 stmt = DR_STMT (dr);
1369 stmt_info = vinfo_for_stmt (stmt);
1371 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1372 continue;
1374 /* For interleaving, only the alignment of the first access
1375 matters. */
1376 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1377 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1378 continue;
1380 /* For invariant accesses there is nothing to enhance. */
1381 if (integer_zerop (DR_STEP (dr)))
1382 continue;
1384 /* Strided loads perform only component accesses, alignment is
1385 irrelevant for them. */
1386 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1387 continue;
1389 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1390 do_peeling = vector_alignment_reachable_p (dr);
1391 if (do_peeling)
1393 if (known_alignment_for_access_p (dr))
1395 unsigned int npeel_tmp;
1396 bool negative = tree_int_cst_compare (DR_STEP (dr),
1397 size_zero_node) < 0;
1399 /* Save info about DR in the hash table. */
1400 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo).is_created ())
1401 LOOP_VINFO_PEELING_HTAB (loop_vinfo).create (1);
1403 vectype = STMT_VINFO_VECTYPE (stmt_info);
1404 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1405 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1406 TREE_TYPE (DR_REF (dr))));
1407 npeel_tmp = (negative
1408 ? (mis - nelements) : (nelements - mis))
1409 & (nelements - 1);
1411 /* For multiple types, it is possible that the bigger type access
1412 will have more than one peeling option. E.g., a loop with two
1413 types: one of size (vector size / 4), and the other one of
1414 size (vector size / 8). Vectorization factor will 8. If both
1415 access are misaligned by 3, the first one needs one scalar
1416 iteration to be aligned, and the second one needs 5. But the
1417 the first one will be aligned also by peeling 5 scalar
1418 iterations, and in that case both accesses will be aligned.
1419 Hence, except for the immediate peeling amount, we also want
1420 to try to add full vector size, while we don't exceed
1421 vectorization factor.
1422 We do this automtically for cost model, since we calculate cost
1423 for every peeling option. */
1424 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1425 possible_npeel_number = vf /nelements;
1427 /* Handle the aligned case. We may decide to align some other
1428 access, making DR unaligned. */
1429 if (DR_MISALIGNMENT (dr) == 0)
1431 npeel_tmp = 0;
1432 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
1433 possible_npeel_number++;
1436 for (j = 0; j < possible_npeel_number; j++)
1438 gcc_assert (npeel_tmp <= vf);
1439 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1440 npeel_tmp += nelements;
1443 all_misalignments_unknown = false;
1444 /* Data-ref that was chosen for the case that all the
1445 misalignments are unknown is not relevant anymore, since we
1446 have a data-ref with known alignment. */
1447 dr0 = NULL;
1449 else
1451 /* If we don't know any misalignment values, we prefer
1452 peeling for data-ref that has the maximum number of data-refs
1453 with the same alignment, unless the target prefers to align
1454 stores over load. */
1455 if (all_misalignments_unknown)
1457 unsigned same_align_drs
1458 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1459 if (!dr0
1460 || same_align_drs_max < same_align_drs)
1462 same_align_drs_max = same_align_drs;
1463 dr0 = dr;
1465 /* For data-refs with the same number of related
1466 accesses prefer the one where the misalign
1467 computation will be invariant in the outermost loop. */
1468 else if (same_align_drs_max == same_align_drs)
1470 struct loop *ivloop0, *ivloop;
1471 ivloop0 = outermost_invariant_loop_for_expr
1472 (loop, DR_BASE_ADDRESS (dr0));
1473 ivloop = outermost_invariant_loop_for_expr
1474 (loop, DR_BASE_ADDRESS (dr));
1475 if ((ivloop && !ivloop0)
1476 || (ivloop && ivloop0
1477 && flow_loop_nested_p (ivloop, ivloop0)))
1478 dr0 = dr;
1481 if (!first_store && DR_IS_WRITE (dr))
1482 first_store = dr;
1485 /* If there are both known and unknown misaligned accesses in the
1486 loop, we choose peeling amount according to the known
1487 accesses. */
1488 if (!supportable_dr_alignment)
1490 dr0 = dr;
1491 if (!first_store && DR_IS_WRITE (dr))
1492 first_store = dr;
1496 else
1498 if (!aligned_access_p (dr))
1500 if (dump_enabled_p ())
1501 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1502 "vector alignment may not be reachable\n");
1503 break;
1508 /* Check if we can possibly peel the loop. */
1509 if (!vect_can_advance_ivs_p (loop_vinfo)
1510 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1511 do_peeling = false;
1513 if (do_peeling && all_misalignments_unknown
1514 && vect_supportable_dr_alignment (dr0, false))
1517 /* Check if the target requires to prefer stores over loads, i.e., if
1518 misaligned stores are more expensive than misaligned loads (taking
1519 drs with same alignment into account). */
1520 if (first_store && DR_IS_READ (dr0))
1522 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1523 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1524 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1525 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1526 stmt_vector_for_cost dummy;
1527 dummy.create (2);
1529 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1530 &dummy);
1531 vect_get_data_access_cost (first_store, &store_inside_cost,
1532 &store_outside_cost, &dummy);
1534 dummy.release ();
1536 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1537 aligning the load DR0). */
1538 load_inside_penalty = store_inside_cost;
1539 load_outside_penalty = store_outside_cost;
1540 for (i = 0;
1541 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1542 DR_STMT (first_store))).iterate (i, &dr);
1543 i++)
1544 if (DR_IS_READ (dr))
1546 load_inside_penalty += load_inside_cost;
1547 load_outside_penalty += load_outside_cost;
1549 else
1551 load_inside_penalty += store_inside_cost;
1552 load_outside_penalty += store_outside_cost;
1555 /* Calculate the penalty for leaving DR0 unaligned (by
1556 aligning the FIRST_STORE). */
1557 store_inside_penalty = load_inside_cost;
1558 store_outside_penalty = load_outside_cost;
1559 for (i = 0;
1560 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1561 DR_STMT (dr0))).iterate (i, &dr);
1562 i++)
1563 if (DR_IS_READ (dr))
1565 store_inside_penalty += load_inside_cost;
1566 store_outside_penalty += load_outside_cost;
1568 else
1570 store_inside_penalty += store_inside_cost;
1571 store_outside_penalty += store_outside_cost;
1574 if (load_inside_penalty > store_inside_penalty
1575 || (load_inside_penalty == store_inside_penalty
1576 && load_outside_penalty > store_outside_penalty))
1577 dr0 = first_store;
1580 /* In case there are only loads with different unknown misalignments, use
1581 peeling only if it may help to align other accesses in the loop. */
1582 if (!first_store
1583 && !STMT_VINFO_SAME_ALIGN_REFS (
1584 vinfo_for_stmt (DR_STMT (dr0))).length ()
1585 && vect_supportable_dr_alignment (dr0, false)
1586 != dr_unaligned_supported)
1587 do_peeling = false;
1590 if (do_peeling && !dr0)
1592 /* Peeling is possible, but there is no data access that is not supported
1593 unless aligned. So we try to choose the best possible peeling. */
1595 /* We should get here only if there are drs with known misalignment. */
1596 gcc_assert (!all_misalignments_unknown);
1598 /* Choose the best peeling from the hash table. */
1599 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1600 &body_cost_vec);
1601 if (!dr0 || !npeel)
1602 do_peeling = false;
1605 if (do_peeling)
1607 stmt = DR_STMT (dr0);
1608 stmt_info = vinfo_for_stmt (stmt);
1609 vectype = STMT_VINFO_VECTYPE (stmt_info);
1610 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1612 if (known_alignment_for_access_p (dr0))
1614 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1615 size_zero_node) < 0;
1616 if (!npeel)
1618 /* Since it's known at compile time, compute the number of
1619 iterations in the peeled loop (the peeling factor) for use in
1620 updating DR_MISALIGNMENT values. The peeling factor is the
1621 vectorization factor minus the misalignment as an element
1622 count. */
1623 mis = DR_MISALIGNMENT (dr0);
1624 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1625 npeel = ((negative ? mis - nelements : nelements - mis)
1626 & (nelements - 1));
1629 /* For interleaved data access every iteration accesses all the
1630 members of the group, therefore we divide the number of iterations
1631 by the group size. */
1632 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1633 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1634 npeel /= GROUP_SIZE (stmt_info);
1636 if (dump_enabled_p ())
1637 dump_printf_loc (MSG_NOTE, vect_location,
1638 "Try peeling by %d\n", npeel);
1641 /* Ensure that all data refs can be vectorized after the peel. */
1642 FOR_EACH_VEC_ELT (datarefs, i, dr)
1644 int save_misalignment;
1646 if (dr == dr0)
1647 continue;
1649 stmt = DR_STMT (dr);
1650 stmt_info = vinfo_for_stmt (stmt);
1651 /* For interleaving, only the alignment of the first access
1652 matters. */
1653 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1654 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1655 continue;
1657 /* Strided loads perform only component accesses, alignment is
1658 irrelevant for them. */
1659 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1660 continue;
1662 save_misalignment = DR_MISALIGNMENT (dr);
1663 vect_update_misalignment_for_peel (dr, dr0, npeel);
1664 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1665 SET_DR_MISALIGNMENT (dr, save_misalignment);
1667 if (!supportable_dr_alignment)
1669 do_peeling = false;
1670 break;
1674 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1676 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1677 if (!stat)
1678 do_peeling = false;
1679 else
1681 body_cost_vec.release ();
1682 return stat;
1686 if (do_peeling)
1688 unsigned max_allowed_peel
1689 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1690 if (max_allowed_peel != (unsigned)-1)
1692 unsigned max_peel = npeel;
1693 if (max_peel == 0)
1695 gimple dr_stmt = DR_STMT (dr0);
1696 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1697 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1698 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1700 if (max_peel > max_allowed_peel)
1702 do_peeling = false;
1703 if (dump_enabled_p ())
1704 dump_printf_loc (MSG_NOTE, vect_location,
1705 "Disable peeling, max peels reached: %d\n", max_peel);
1710 if (do_peeling)
1712 stmt_info_for_cost *si;
1713 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
1715 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1716 If the misalignment of DR_i is identical to that of dr0 then set
1717 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1718 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1719 by the peeling factor times the element size of DR_i (MOD the
1720 vectorization factor times the size). Otherwise, the
1721 misalignment of DR_i must be set to unknown. */
1722 FOR_EACH_VEC_ELT (datarefs, i, dr)
1723 if (dr != dr0)
1724 vect_update_misalignment_for_peel (dr, dr0, npeel);
1726 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1727 if (npeel)
1728 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1729 else
1730 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1731 = DR_MISALIGNMENT (dr0);
1732 SET_DR_MISALIGNMENT (dr0, 0);
1733 if (dump_enabled_p ())
1735 dump_printf_loc (MSG_NOTE, vect_location,
1736 "Alignment of access forced using peeling.\n");
1737 dump_printf_loc (MSG_NOTE, vect_location,
1738 "Peeling for alignment will be applied.\n");
1740 /* We've delayed passing the inside-loop peeling costs to the
1741 target cost model until we were sure peeling would happen.
1742 Do so now. */
1743 if (body_cost_vec.exists ())
1745 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1747 struct _stmt_vec_info *stmt_info
1748 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1749 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1750 si->misalign, vect_body);
1752 body_cost_vec.release ();
1755 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1756 gcc_assert (stat);
1757 return stat;
1761 body_cost_vec.release ();
1763 /* (2) Versioning to force alignment. */
1765 /* Try versioning if:
1766 1) optimize loop for speed
1767 2) there is at least one unsupported misaligned data ref with an unknown
1768 misalignment, and
1769 3) all misaligned data refs with a known misalignment are supported, and
1770 4) the number of runtime alignment checks is within reason. */
1772 do_versioning =
1773 optimize_loop_nest_for_speed_p (loop)
1774 && (!loop->inner); /* FORNOW */
1776 if (do_versioning)
1778 FOR_EACH_VEC_ELT (datarefs, i, dr)
1780 stmt = DR_STMT (dr);
1781 stmt_info = vinfo_for_stmt (stmt);
1783 /* For interleaving, only the alignment of the first access
1784 matters. */
1785 if (aligned_access_p (dr)
1786 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1787 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1788 continue;
1790 /* Strided loads perform only component accesses, alignment is
1791 irrelevant for them. */
1792 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1793 continue;
1795 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1797 if (!supportable_dr_alignment)
1799 gimple stmt;
1800 int mask;
1801 tree vectype;
1803 if (known_alignment_for_access_p (dr)
1804 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1805 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1807 do_versioning = false;
1808 break;
1811 stmt = DR_STMT (dr);
1812 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1813 gcc_assert (vectype);
1815 /* The rightmost bits of an aligned address must be zeros.
1816 Construct the mask needed for this test. For example,
1817 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1818 mask must be 15 = 0xf. */
1819 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1821 /* FORNOW: use the same mask to test all potentially unaligned
1822 references in the loop. The vectorizer currently supports
1823 a single vector size, see the reference to
1824 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1825 vectorization factor is computed. */
1826 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1827 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1828 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1829 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1830 DR_STMT (dr));
1834 /* Versioning requires at least one misaligned data reference. */
1835 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1836 do_versioning = false;
1837 else if (!do_versioning)
1838 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1841 if (do_versioning)
1843 vec<gimple> may_misalign_stmts
1844 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1845 gimple stmt;
1847 /* It can now be assumed that the data references in the statements
1848 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1849 of the loop being vectorized. */
1850 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1852 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1853 dr = STMT_VINFO_DATA_REF (stmt_info);
1854 SET_DR_MISALIGNMENT (dr, 0);
1855 if (dump_enabled_p ())
1856 dump_printf_loc (MSG_NOTE, vect_location,
1857 "Alignment of access forced using versioning.\n");
1860 if (dump_enabled_p ())
1861 dump_printf_loc (MSG_NOTE, vect_location,
1862 "Versioning for alignment will be applied.\n");
1864 /* Peeling and versioning can't be done together at this time. */
1865 gcc_assert (! (do_peeling && do_versioning));
1867 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1868 gcc_assert (stat);
1869 return stat;
1872 /* This point is reached if neither peeling nor versioning is being done. */
1873 gcc_assert (! (do_peeling || do_versioning));
1875 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1876 return stat;
1880 /* Function vect_find_same_alignment_drs.
1882 Update group and alignment relations according to the chosen
1883 vectorization factor. */
1885 static void
1886 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1887 loop_vec_info loop_vinfo)
1889 unsigned int i;
1890 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1891 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1892 struct data_reference *dra = DDR_A (ddr);
1893 struct data_reference *drb = DDR_B (ddr);
1894 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1895 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1896 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1897 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1898 lambda_vector dist_v;
1899 unsigned int loop_depth;
1901 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1902 return;
1904 if (dra == drb)
1905 return;
1907 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1908 return;
1910 /* Loop-based vectorization and known data dependence. */
1911 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1912 return;
1914 /* Data-dependence analysis reports a distance vector of zero
1915 for data-references that overlap only in the first iteration
1916 but have different sign step (see PR45764).
1917 So as a sanity check require equal DR_STEP. */
1918 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1919 return;
1921 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1922 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1924 int dist = dist_v[loop_depth];
1926 if (dump_enabled_p ())
1927 dump_printf_loc (MSG_NOTE, vect_location,
1928 "dependence distance = %d.\n", dist);
1930 /* Same loop iteration. */
1931 if (dist == 0
1932 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1934 /* Two references with distance zero have the same alignment. */
1935 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1936 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1937 if (dump_enabled_p ())
1939 dump_printf_loc (MSG_NOTE, vect_location,
1940 "accesses have the same alignment.\n");
1941 dump_printf (MSG_NOTE,
1942 "dependence distance modulo vf == 0 between ");
1943 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1944 dump_printf (MSG_NOTE, " and ");
1945 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1946 dump_printf (MSG_NOTE, "\n");
1953 /* Function vect_analyze_data_refs_alignment
1955 Analyze the alignment of the data-references in the loop.
1956 Return FALSE if a data reference is found that cannot be vectorized. */
1958 bool
1959 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
1960 bb_vec_info bb_vinfo)
1962 if (dump_enabled_p ())
1963 dump_printf_loc (MSG_NOTE, vect_location,
1964 "=== vect_analyze_data_refs_alignment ===\n");
1966 /* Mark groups of data references with same alignment using
1967 data dependence information. */
1968 if (loop_vinfo)
1970 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
1971 struct data_dependence_relation *ddr;
1972 unsigned int i;
1974 FOR_EACH_VEC_ELT (ddrs, i, ddr)
1975 vect_find_same_alignment_drs (ddr, loop_vinfo);
1978 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
1980 if (dump_enabled_p ())
1981 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1982 "not vectorized: can't calculate alignment "
1983 "for data ref.\n");
1984 return false;
1987 return true;
1991 /* Analyze groups of accesses: check that DR belongs to a group of
1992 accesses of legal size, step, etc. Detect gaps, single element
1993 interleaving, and other special cases. Set grouped access info.
1994 Collect groups of strided stores for further use in SLP analysis. */
1996 static bool
1997 vect_analyze_group_access (struct data_reference *dr)
1999 tree step = DR_STEP (dr);
2000 tree scalar_type = TREE_TYPE (DR_REF (dr));
2001 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2002 gimple stmt = DR_STMT (dr);
2003 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2004 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2005 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2006 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2007 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2008 bool slp_impossible = false;
2009 struct loop *loop = NULL;
2011 if (loop_vinfo)
2012 loop = LOOP_VINFO_LOOP (loop_vinfo);
2014 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2015 size of the interleaving group (including gaps). */
2016 groupsize = absu_hwi (dr_step) / type_size;
2018 /* Not consecutive access is possible only if it is a part of interleaving. */
2019 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2021 /* Check if it this DR is a part of interleaving, and is a single
2022 element of the group that is accessed in the loop. */
2024 /* Gaps are supported only for loads. STEP must be a multiple of the type
2025 size. The size of the group must be a power of 2. */
2026 if (DR_IS_READ (dr)
2027 && (dr_step % type_size) == 0
2028 && groupsize > 0
2029 && exact_log2 (groupsize) != -1)
2031 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2032 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2033 if (dump_enabled_p ())
2035 dump_printf_loc (MSG_NOTE, vect_location,
2036 "Detected single element interleaving ");
2037 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2038 dump_printf (MSG_NOTE, " step ");
2039 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2040 dump_printf (MSG_NOTE, "\n");
2043 if (loop_vinfo)
2045 if (dump_enabled_p ())
2046 dump_printf_loc (MSG_NOTE, vect_location,
2047 "Data access with gaps requires scalar "
2048 "epilogue loop\n");
2049 if (loop->inner)
2051 if (dump_enabled_p ())
2052 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2053 "Peeling for outer loop is not"
2054 " supported\n");
2055 return false;
2058 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2061 return true;
2064 if (dump_enabled_p ())
2066 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2067 "not consecutive access ");
2068 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2069 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2072 if (bb_vinfo)
2074 /* Mark the statement as unvectorizable. */
2075 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2076 return true;
2079 return false;
2082 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2084 /* First stmt in the interleaving chain. Check the chain. */
2085 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2086 struct data_reference *data_ref = dr;
2087 unsigned int count = 1;
2088 tree prev_init = DR_INIT (data_ref);
2089 gimple prev = stmt;
2090 HOST_WIDE_INT diff, gaps = 0;
2091 unsigned HOST_WIDE_INT count_in_bytes;
2093 while (next)
2095 /* Skip same data-refs. In case that two or more stmts share
2096 data-ref (supported only for loads), we vectorize only the first
2097 stmt, and the rest get their vectorized loads from the first
2098 one. */
2099 if (!tree_int_cst_compare (DR_INIT (data_ref),
2100 DR_INIT (STMT_VINFO_DATA_REF (
2101 vinfo_for_stmt (next)))))
2103 if (DR_IS_WRITE (data_ref))
2105 if (dump_enabled_p ())
2106 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2107 "Two store stmts share the same dr.\n");
2108 return false;
2111 /* For load use the same data-ref load. */
2112 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2114 prev = next;
2115 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2116 continue;
2119 prev = next;
2120 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2122 /* All group members have the same STEP by construction. */
2123 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2125 /* Check that the distance between two accesses is equal to the type
2126 size. Otherwise, we have gaps. */
2127 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2128 - TREE_INT_CST_LOW (prev_init)) / type_size;
2129 if (diff != 1)
2131 /* FORNOW: SLP of accesses with gaps is not supported. */
2132 slp_impossible = true;
2133 if (DR_IS_WRITE (data_ref))
2135 if (dump_enabled_p ())
2136 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2137 "interleaved store with gaps\n");
2138 return false;
2141 gaps += diff - 1;
2144 last_accessed_element += diff;
2146 /* Store the gap from the previous member of the group. If there is no
2147 gap in the access, GROUP_GAP is always 1. */
2148 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2150 prev_init = DR_INIT (data_ref);
2151 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2152 /* Count the number of data-refs in the chain. */
2153 count++;
2156 /* COUNT is the number of accesses found, we multiply it by the size of
2157 the type to get COUNT_IN_BYTES. */
2158 count_in_bytes = type_size * count;
2160 /* Check that the size of the interleaving (including gaps) is not
2161 greater than STEP. */
2162 if (dr_step != 0
2163 && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
2165 if (dump_enabled_p ())
2167 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2168 "interleaving size is greater than step for ");
2169 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2170 DR_REF (dr));
2171 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2173 return false;
2176 /* Check that the size of the interleaving is equal to STEP for stores,
2177 i.e., that there are no gaps. */
2178 if (dr_step != 0
2179 && absu_hwi (dr_step) != count_in_bytes)
2181 if (DR_IS_READ (dr))
2183 slp_impossible = true;
2184 /* There is a gap after the last load in the group. This gap is a
2185 difference between the groupsize and the number of elements.
2186 When there is no gap, this difference should be 0. */
2187 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2189 else
2191 if (dump_enabled_p ())
2192 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2193 "interleaved store with gaps\n");
2194 return false;
2198 /* Check that STEP is a multiple of type size. */
2199 if (dr_step != 0
2200 && (dr_step % type_size) != 0)
2202 if (dump_enabled_p ())
2204 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2205 "step is not a multiple of type size: step ");
2206 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
2207 dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
2208 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2209 TYPE_SIZE_UNIT (scalar_type));
2210 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2212 return false;
2215 if (groupsize == 0)
2216 groupsize = count;
2218 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2219 if (dump_enabled_p ())
2220 dump_printf_loc (MSG_NOTE, vect_location,
2221 "Detected interleaving of size %d\n", (int)groupsize);
2223 /* SLP: create an SLP data structure for every interleaving group of
2224 stores for further analysis in vect_analyse_slp. */
2225 if (DR_IS_WRITE (dr) && !slp_impossible)
2227 if (loop_vinfo)
2228 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2229 if (bb_vinfo)
2230 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2233 /* There is a gap in the end of the group. */
2234 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2236 if (dump_enabled_p ())
2237 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2238 "Data access with gaps requires scalar "
2239 "epilogue loop\n");
2240 if (loop->inner)
2242 if (dump_enabled_p ())
2243 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2244 "Peeling for outer loop is not supported\n");
2245 return false;
2248 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2252 return true;
2256 /* Analyze the access pattern of the data-reference DR.
2257 In case of non-consecutive accesses call vect_analyze_group_access() to
2258 analyze groups of accesses. */
2260 static bool
2261 vect_analyze_data_ref_access (struct data_reference *dr)
2263 tree step = DR_STEP (dr);
2264 tree scalar_type = TREE_TYPE (DR_REF (dr));
2265 gimple stmt = DR_STMT (dr);
2266 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2267 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2268 struct loop *loop = NULL;
2270 if (loop_vinfo)
2271 loop = LOOP_VINFO_LOOP (loop_vinfo);
2273 if (loop_vinfo && !step)
2275 if (dump_enabled_p ())
2276 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2277 "bad data-ref access in loop\n");
2278 return false;
2281 /* Allow invariant loads in not nested loops. */
2282 if (loop_vinfo && integer_zerop (step))
2284 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2285 if (nested_in_vect_loop_p (loop, stmt))
2287 if (dump_enabled_p ())
2288 dump_printf_loc (MSG_NOTE, vect_location,
2289 "zero step in inner loop of nest\n");
2290 return false;
2292 return DR_IS_READ (dr);
2295 if (loop && nested_in_vect_loop_p (loop, stmt))
2297 /* Interleaved accesses are not yet supported within outer-loop
2298 vectorization for references in the inner-loop. */
2299 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2301 /* For the rest of the analysis we use the outer-loop step. */
2302 step = STMT_VINFO_DR_STEP (stmt_info);
2303 if (integer_zerop (step))
2305 if (dump_enabled_p ())
2306 dump_printf_loc (MSG_NOTE, vect_location,
2307 "zero step in outer loop.\n");
2308 if (DR_IS_READ (dr))
2309 return true;
2310 else
2311 return false;
2315 /* Consecutive? */
2316 if (TREE_CODE (step) == INTEGER_CST)
2318 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2319 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2320 || (dr_step < 0
2321 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2323 /* Mark that it is not interleaving. */
2324 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2325 return true;
2329 if (loop && nested_in_vect_loop_p (loop, stmt))
2331 if (dump_enabled_p ())
2332 dump_printf_loc (MSG_NOTE, vect_location,
2333 "grouped access in outer loop.\n");
2334 return false;
2337 /* Assume this is a DR handled by non-constant strided load case. */
2338 if (TREE_CODE (step) != INTEGER_CST)
2339 return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
2341 /* Not consecutive access - check if it's a part of interleaving group. */
2342 return vect_analyze_group_access (dr);
2347 /* A helper function used in the comparator function to sort data
2348 references. T1 and T2 are two data references to be compared.
2349 The function returns -1, 0, or 1. */
2351 static int
2352 compare_tree (tree t1, tree t2)
2354 int i, cmp;
2355 enum tree_code code;
2356 char tclass;
2358 if (t1 == t2)
2359 return 0;
2360 if (t1 == NULL)
2361 return -1;
2362 if (t2 == NULL)
2363 return 1;
2366 if (TREE_CODE (t1) != TREE_CODE (t2))
2367 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2369 code = TREE_CODE (t1);
2370 switch (code)
2372 /* For const values, we can just use hash values for comparisons. */
2373 case INTEGER_CST:
2374 case REAL_CST:
2375 case FIXED_CST:
2376 case STRING_CST:
2377 case COMPLEX_CST:
2378 case VECTOR_CST:
2380 hashval_t h1 = iterative_hash_expr (t1, 0);
2381 hashval_t h2 = iterative_hash_expr (t2, 0);
2382 if (h1 != h2)
2383 return h1 < h2 ? -1 : 1;
2384 break;
2387 case SSA_NAME:
2388 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2389 if (cmp != 0)
2390 return cmp;
2392 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2393 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2394 break;
2396 default:
2397 tclass = TREE_CODE_CLASS (code);
2399 /* For var-decl, we could compare their UIDs. */
2400 if (tclass == tcc_declaration)
2402 if (DECL_UID (t1) != DECL_UID (t2))
2403 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2404 break;
2407 /* For expressions with operands, compare their operands recursively. */
2408 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2410 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2411 if (cmp != 0)
2412 return cmp;
2416 return 0;
2420 /* Compare two data-references DRA and DRB to group them into chunks
2421 suitable for grouping. */
2423 static int
2424 dr_group_sort_cmp (const void *dra_, const void *drb_)
2426 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2427 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2428 int cmp;
2430 /* Stabilize sort. */
2431 if (dra == drb)
2432 return 0;
2434 /* Ordering of DRs according to base. */
2435 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2437 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2438 if (cmp != 0)
2439 return cmp;
2442 /* And according to DR_OFFSET. */
2443 if (!dr_equal_offsets_p (dra, drb))
2445 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2446 if (cmp != 0)
2447 return cmp;
2450 /* Put reads before writes. */
2451 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2452 return DR_IS_READ (dra) ? -1 : 1;
2454 /* Then sort after access size. */
2455 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2456 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2458 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2459 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2460 if (cmp != 0)
2461 return cmp;
2464 /* And after step. */
2465 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2467 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2468 if (cmp != 0)
2469 return cmp;
2472 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2473 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2474 if (cmp == 0)
2475 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2476 return cmp;
2479 /* Function vect_analyze_data_ref_accesses.
2481 Analyze the access pattern of all the data references in the loop.
2483 FORNOW: the only access pattern that is considered vectorizable is a
2484 simple step 1 (consecutive) access.
2486 FORNOW: handle only arrays and pointer accesses. */
2488 bool
2489 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2491 unsigned int i;
2492 vec<data_reference_p> datarefs;
2493 struct data_reference *dr;
2495 if (dump_enabled_p ())
2496 dump_printf_loc (MSG_NOTE, vect_location,
2497 "=== vect_analyze_data_ref_accesses ===\n");
2499 if (loop_vinfo)
2500 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2501 else
2502 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2504 if (datarefs.is_empty ())
2505 return true;
2507 /* Sort the array of datarefs to make building the interleaving chains
2508 linear. Don't modify the original vector's order, it is needed for
2509 determining what dependencies are reversed. */
2510 vec<data_reference_p> datarefs_copy = datarefs.copy ();
2511 datarefs_copy.qsort (dr_group_sort_cmp);
2513 /* Build the interleaving chains. */
2514 for (i = 0; i < datarefs_copy.length () - 1;)
2516 data_reference_p dra = datarefs_copy[i];
2517 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2518 stmt_vec_info lastinfo = NULL;
2519 for (i = i + 1; i < datarefs_copy.length (); ++i)
2521 data_reference_p drb = datarefs_copy[i];
2522 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2524 /* ??? Imperfect sorting (non-compatible types, non-modulo
2525 accesses, same accesses) can lead to a group to be artificially
2526 split here as we don't just skip over those. If it really
2527 matters we can push those to a worklist and re-iterate
2528 over them. The we can just skip ahead to the next DR here. */
2530 /* Check that the data-refs have same first location (except init)
2531 and they are both either store or load (not load and store). */
2532 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2533 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2534 DR_BASE_ADDRESS (drb), 0)
2535 || !dr_equal_offsets_p (dra, drb))
2536 break;
2538 /* Check that the data-refs have the same constant size and step. */
2539 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2540 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2541 if (!tree_fits_uhwi_p (sza)
2542 || !tree_fits_uhwi_p (szb)
2543 || !tree_int_cst_equal (sza, szb)
2544 || !tree_fits_shwi_p (DR_STEP (dra))
2545 || !tree_fits_shwi_p (DR_STEP (drb))
2546 || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
2547 break;
2549 /* Do not place the same access in the interleaving chain twice. */
2550 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2551 break;
2553 /* Check the types are compatible.
2554 ??? We don't distinguish this during sorting. */
2555 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2556 TREE_TYPE (DR_REF (drb))))
2557 break;
2559 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2560 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2561 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2562 gcc_assert (init_a < init_b);
2564 /* If init_b == init_a + the size of the type * k, we have an
2565 interleaving, and DRA is accessed before DRB. */
2566 HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
2567 if ((init_b - init_a) % type_size_a != 0)
2568 break;
2570 /* The step (if not zero) is greater than the difference between
2571 data-refs' inits. This splits groups into suitable sizes. */
2572 HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
2573 if (step != 0 && step <= (init_b - init_a))
2574 break;
2576 if (dump_enabled_p ())
2578 dump_printf_loc (MSG_NOTE, vect_location,
2579 "Detected interleaving ");
2580 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2581 dump_printf (MSG_NOTE, " and ");
2582 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2583 dump_printf (MSG_NOTE, "\n");
2586 /* Link the found element into the group list. */
2587 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2589 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2590 lastinfo = stmtinfo_a;
2592 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2593 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2594 lastinfo = stmtinfo_b;
2598 FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
2599 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2600 && !vect_analyze_data_ref_access (dr))
2602 if (dump_enabled_p ())
2603 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2604 "not vectorized: complicated access pattern.\n");
2606 if (bb_vinfo)
2608 /* Mark the statement as not vectorizable. */
2609 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2610 continue;
2612 else
2614 datarefs_copy.release ();
2615 return false;
2619 datarefs_copy.release ();
2620 return true;
2624 /* Operator == between two dr_with_seg_len objects.
2626 This equality operator is used to make sure two data refs
2627 are the same one so that we will consider to combine the
2628 aliasing checks of those two pairs of data dependent data
2629 refs. */
2631 static bool
2632 operator == (const dr_with_seg_len& d1,
2633 const dr_with_seg_len& d2)
2635 return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
2636 DR_BASE_ADDRESS (d2.dr), 0)
2637 && compare_tree (d1.offset, d2.offset) == 0
2638 && compare_tree (d1.seg_len, d2.seg_len) == 0;
2641 /* Function comp_dr_with_seg_len_pair.
2643 Comparison function for sorting objects of dr_with_seg_len_pair_t
2644 so that we can combine aliasing checks in one scan. */
2646 static int
2647 comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
2649 const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
2650 const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
2652 const dr_with_seg_len &p11 = p1->first,
2653 &p12 = p1->second,
2654 &p21 = p2->first,
2655 &p22 = p2->second;
2657 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2658 if a and c have the same basic address snd step, and b and d have the same
2659 address and step. Therefore, if any a&c or b&d don't have the same address
2660 and step, we don't care the order of those two pairs after sorting. */
2661 int comp_res;
2663 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
2664 DR_BASE_ADDRESS (p21.dr))) != 0)
2665 return comp_res;
2666 if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
2667 DR_BASE_ADDRESS (p22.dr))) != 0)
2668 return comp_res;
2669 if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
2670 return comp_res;
2671 if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
2672 return comp_res;
2673 if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
2674 return comp_res;
2675 if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
2676 return comp_res;
2678 return 0;
2681 template <class T> static void
2682 swap (T& a, T& b)
2684 T c (a);
2685 a = b;
2686 b = c;
2689 /* Function vect_vfa_segment_size.
2691 Create an expression that computes the size of segment
2692 that will be accessed for a data reference. The functions takes into
2693 account that realignment loads may access one more vector.
2695 Input:
2696 DR: The data reference.
2697 LENGTH_FACTOR: segment length to consider.
2699 Return an expression whose value is the size of segment which will be
2700 accessed by DR. */
2702 static tree
2703 vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
2705 tree segment_length;
2707 if (integer_zerop (DR_STEP (dr)))
2708 segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
2709 else
2710 segment_length = size_binop (MULT_EXPR,
2711 fold_convert (sizetype, DR_STEP (dr)),
2712 fold_convert (sizetype, length_factor));
2714 if (vect_supportable_dr_alignment (dr, false)
2715 == dr_explicit_realign_optimized)
2717 tree vector_size = TYPE_SIZE_UNIT
2718 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
2720 segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
2722 return segment_length;
2725 /* Function vect_prune_runtime_alias_test_list.
2727 Prune a list of ddrs to be tested at run-time by versioning for alias.
2728 Merge several alias checks into one if possible.
2729 Return FALSE if resulting list of ddrs is longer then allowed by
2730 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2732 bool
2733 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2735 vec<ddr_p> may_alias_ddrs =
2736 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2737 vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
2738 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
2739 int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2740 tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
2742 ddr_p ddr;
2743 unsigned int i;
2744 tree length_factor;
2746 if (dump_enabled_p ())
2747 dump_printf_loc (MSG_NOTE, vect_location,
2748 "=== vect_prune_runtime_alias_test_list ===\n");
2750 if (may_alias_ddrs.is_empty ())
2751 return true;
2753 /* Basically, for each pair of dependent data refs store_ptr_0
2754 and load_ptr_0, we create an expression:
2756 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2757 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2759 for aliasing checks. However, in some cases we can decrease
2760 the number of checks by combining two checks into one. For
2761 example, suppose we have another pair of data refs store_ptr_0
2762 and load_ptr_1, and if the following condition is satisfied:
2764 load_ptr_0 < load_ptr_1 &&
2765 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2767 (this condition means, in each iteration of vectorized loop,
2768 the accessed memory of store_ptr_0 cannot be between the memory
2769 of load_ptr_0 and load_ptr_1.)
2771 we then can use only the following expression to finish the
2772 alising checks between store_ptr_0 & load_ptr_0 and
2773 store_ptr_0 & load_ptr_1:
2775 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2776 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2778 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2779 same basic address. */
2781 comp_alias_ddrs.create (may_alias_ddrs.length ());
2783 /* First, we collect all data ref pairs for aliasing checks. */
2784 FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
2786 struct data_reference *dr_a, *dr_b;
2787 gimple dr_group_first_a, dr_group_first_b;
2788 tree segment_length_a, segment_length_b;
2789 gimple stmt_a, stmt_b;
2791 dr_a = DDR_A (ddr);
2792 stmt_a = DR_STMT (DDR_A (ddr));
2793 dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
2794 if (dr_group_first_a)
2796 stmt_a = dr_group_first_a;
2797 dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
2800 dr_b = DDR_B (ddr);
2801 stmt_b = DR_STMT (DDR_B (ddr));
2802 dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
2803 if (dr_group_first_b)
2805 stmt_b = dr_group_first_b;
2806 dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
2809 if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
2810 length_factor = scalar_loop_iters;
2811 else
2812 length_factor = size_int (vect_factor);
2813 segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
2814 segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
2816 dr_with_seg_len_pair_t dr_with_seg_len_pair
2817 (dr_with_seg_len (dr_a, segment_length_a),
2818 dr_with_seg_len (dr_b, segment_length_b));
2820 if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
2821 swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
2823 comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
2826 /* Second, we sort the collected data ref pairs so that we can scan
2827 them once to combine all possible aliasing checks. */
2828 comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
2830 /* Third, we scan the sorted dr pairs and check if we can combine
2831 alias checks of two neighbouring dr pairs. */
2832 for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
2834 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2835 dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
2836 *dr_b1 = &comp_alias_ddrs[i-1].second,
2837 *dr_a2 = &comp_alias_ddrs[i].first,
2838 *dr_b2 = &comp_alias_ddrs[i].second;
2840 /* Remove duplicate data ref pairs. */
2841 if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
2843 if (dump_enabled_p ())
2845 dump_printf_loc (MSG_NOTE, vect_location,
2846 "found equal ranges ");
2847 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2848 DR_REF (dr_a1->dr));
2849 dump_printf (MSG_NOTE, ", ");
2850 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2851 DR_REF (dr_b1->dr));
2852 dump_printf (MSG_NOTE, " and ");
2853 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2854 DR_REF (dr_a2->dr));
2855 dump_printf (MSG_NOTE, ", ");
2856 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2857 DR_REF (dr_b2->dr));
2858 dump_printf (MSG_NOTE, "\n");
2861 comp_alias_ddrs.ordered_remove (i--);
2862 continue;
2865 if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
2867 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2868 and DR_A1 and DR_A2 are two consecutive memrefs. */
2869 if (*dr_a1 == *dr_a2)
2871 swap (dr_a1, dr_b1);
2872 swap (dr_a2, dr_b2);
2875 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
2876 DR_BASE_ADDRESS (dr_a2->dr),
2878 || !tree_fits_shwi_p (dr_a1->offset)
2879 || !tree_fits_shwi_p (dr_a2->offset))
2880 continue;
2882 HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
2883 - tree_to_shwi (dr_a1->offset));
2886 /* Now we check if the following condition is satisfied:
2888 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2890 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2891 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2892 have to make a best estimation. We can get the minimum value
2893 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2894 then either of the following two conditions can guarantee the
2895 one above:
2897 1: DIFF <= MIN_SEG_LEN_B
2898 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2902 HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
2903 ? tree_to_shwi (dr_b1->seg_len)
2904 : vect_factor);
2906 if (diff <= min_seg_len_b
2907 || (tree_fits_shwi_p (dr_a1->seg_len)
2908 && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
2910 if (dump_enabled_p ())
2912 dump_printf_loc (MSG_NOTE, vect_location,
2913 "merging ranges for ");
2914 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2915 DR_REF (dr_a1->dr));
2916 dump_printf (MSG_NOTE, ", ");
2917 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2918 DR_REF (dr_b1->dr));
2919 dump_printf (MSG_NOTE, " and ");
2920 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2921 DR_REF (dr_a2->dr));
2922 dump_printf (MSG_NOTE, ", ");
2923 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2924 DR_REF (dr_b2->dr));
2925 dump_printf (MSG_NOTE, "\n");
2928 dr_a1->seg_len = size_binop (PLUS_EXPR,
2929 dr_a2->seg_len, size_int (diff));
2930 comp_alias_ddrs.ordered_remove (i--);
2935 dump_printf_loc (MSG_NOTE, vect_location,
2936 "improved number of alias checks from %d to %d\n",
2937 may_alias_ddrs.length (), comp_alias_ddrs.length ());
2938 if ((int) comp_alias_ddrs.length () >
2939 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2940 return false;
2942 return true;
2945 /* Check whether a non-affine read in stmt is suitable for gather load
2946 and if so, return a builtin decl for that operation. */
2948 tree
2949 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2950 tree *offp, int *scalep)
2952 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2953 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2954 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2955 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2956 tree offtype = NULL_TREE;
2957 tree decl, base, off;
2958 enum machine_mode pmode;
2959 int punsignedp, pvolatilep;
2961 base = DR_REF (dr);
2962 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
2963 see if we can use the def stmt of the address. */
2964 if (is_gimple_call (stmt)
2965 && gimple_call_internal_p (stmt)
2966 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2967 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
2968 && TREE_CODE (base) == MEM_REF
2969 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2970 && integer_zerop (TREE_OPERAND (base, 1))
2971 && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
2973 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
2974 if (is_gimple_assign (def_stmt)
2975 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2976 base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
2979 /* The gather builtins need address of the form
2980 loop_invariant + vector * {1, 2, 4, 8}
2982 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
2983 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
2984 of loop invariants/SSA_NAMEs defined in the loop, with casts,
2985 multiplications and additions in it. To get a vector, we need
2986 a single SSA_NAME that will be defined in the loop and will
2987 contain everything that is not loop invariant and that can be
2988 vectorized. The following code attempts to find such a preexistng
2989 SSA_NAME OFF and put the loop invariants into a tree BASE
2990 that can be gimplified before the loop. */
2991 base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
2992 &pmode, &punsignedp, &pvolatilep, false);
2993 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
2995 if (TREE_CODE (base) == MEM_REF)
2997 if (!integer_zerop (TREE_OPERAND (base, 1)))
2999 if (off == NULL_TREE)
3001 offset_int moff = mem_ref_offset (base);
3002 off = wide_int_to_tree (sizetype, moff);
3004 else
3005 off = size_binop (PLUS_EXPR, off,
3006 fold_convert (sizetype, TREE_OPERAND (base, 1)));
3008 base = TREE_OPERAND (base, 0);
3010 else
3011 base = build_fold_addr_expr (base);
3013 if (off == NULL_TREE)
3014 off = size_zero_node;
3016 /* If base is not loop invariant, either off is 0, then we start with just
3017 the constant offset in the loop invariant BASE and continue with base
3018 as OFF, otherwise give up.
3019 We could handle that case by gimplifying the addition of base + off
3020 into some SSA_NAME and use that as off, but for now punt. */
3021 if (!expr_invariant_in_loop_p (loop, base))
3023 if (!integer_zerop (off))
3024 return NULL_TREE;
3025 off = base;
3026 base = size_int (pbitpos / BITS_PER_UNIT);
3028 /* Otherwise put base + constant offset into the loop invariant BASE
3029 and continue with OFF. */
3030 else
3032 base = fold_convert (sizetype, base);
3033 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
3036 /* OFF at this point may be either a SSA_NAME or some tree expression
3037 from get_inner_reference. Try to peel off loop invariants from it
3038 into BASE as long as possible. */
3039 STRIP_NOPS (off);
3040 while (offtype == NULL_TREE)
3042 enum tree_code code;
3043 tree op0, op1, add = NULL_TREE;
3045 if (TREE_CODE (off) == SSA_NAME)
3047 gimple def_stmt = SSA_NAME_DEF_STMT (off);
3049 if (expr_invariant_in_loop_p (loop, off))
3050 return NULL_TREE;
3052 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
3053 break;
3055 op0 = gimple_assign_rhs1 (def_stmt);
3056 code = gimple_assign_rhs_code (def_stmt);
3057 op1 = gimple_assign_rhs2 (def_stmt);
3059 else
3061 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
3062 return NULL_TREE;
3063 code = TREE_CODE (off);
3064 extract_ops_from_tree (off, &code, &op0, &op1);
3066 switch (code)
3068 case POINTER_PLUS_EXPR:
3069 case PLUS_EXPR:
3070 if (expr_invariant_in_loop_p (loop, op0))
3072 add = op0;
3073 off = op1;
3074 do_add:
3075 add = fold_convert (sizetype, add);
3076 if (scale != 1)
3077 add = size_binop (MULT_EXPR, add, size_int (scale));
3078 base = size_binop (PLUS_EXPR, base, add);
3079 continue;
3081 if (expr_invariant_in_loop_p (loop, op1))
3083 add = op1;
3084 off = op0;
3085 goto do_add;
3087 break;
3088 case MINUS_EXPR:
3089 if (expr_invariant_in_loop_p (loop, op1))
3091 add = fold_convert (sizetype, op1);
3092 add = size_binop (MINUS_EXPR, size_zero_node, add);
3093 off = op0;
3094 goto do_add;
3096 break;
3097 case MULT_EXPR:
3098 if (scale == 1 && tree_fits_shwi_p (op1))
3100 scale = tree_to_shwi (op1);
3101 off = op0;
3102 continue;
3104 break;
3105 case SSA_NAME:
3106 off = op0;
3107 continue;
3108 CASE_CONVERT:
3109 if (!POINTER_TYPE_P (TREE_TYPE (op0))
3110 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
3111 break;
3112 if (TYPE_PRECISION (TREE_TYPE (op0))
3113 == TYPE_PRECISION (TREE_TYPE (off)))
3115 off = op0;
3116 continue;
3118 if (TYPE_PRECISION (TREE_TYPE (op0))
3119 < TYPE_PRECISION (TREE_TYPE (off)))
3121 off = op0;
3122 offtype = TREE_TYPE (off);
3123 STRIP_NOPS (off);
3124 continue;
3126 break;
3127 default:
3128 break;
3130 break;
3133 /* If at the end OFF still isn't a SSA_NAME or isn't
3134 defined in the loop, punt. */
3135 if (TREE_CODE (off) != SSA_NAME
3136 || expr_invariant_in_loop_p (loop, off))
3137 return NULL_TREE;
3139 if (offtype == NULL_TREE)
3140 offtype = TREE_TYPE (off);
3142 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
3143 offtype, scale);
3144 if (decl == NULL_TREE)
3145 return NULL_TREE;
3147 if (basep)
3148 *basep = base;
3149 if (offp)
3150 *offp = off;
3151 if (scalep)
3152 *scalep = scale;
3153 return decl;
3156 /* Function vect_analyze_data_refs.
3158 Find all the data references in the loop or basic block.
3160 The general structure of the analysis of data refs in the vectorizer is as
3161 follows:
3162 1- vect_analyze_data_refs(loop/bb): call
3163 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3164 in the loop/bb and their dependences.
3165 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3166 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3167 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3171 bool
3172 vect_analyze_data_refs (loop_vec_info loop_vinfo,
3173 bb_vec_info bb_vinfo,
3174 int *min_vf, unsigned *n_stmts)
3176 struct loop *loop = NULL;
3177 basic_block bb = NULL;
3178 unsigned int i;
3179 vec<data_reference_p> datarefs;
3180 struct data_reference *dr;
3181 tree scalar_type;
3183 if (dump_enabled_p ())
3184 dump_printf_loc (MSG_NOTE, vect_location,
3185 "=== vect_analyze_data_refs ===\n");
3187 if (loop_vinfo)
3189 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
3191 loop = LOOP_VINFO_LOOP (loop_vinfo);
3192 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
3193 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
3195 if (dump_enabled_p ())
3196 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3197 "not vectorized: loop contains function calls"
3198 " or data references that cannot be analyzed\n");
3199 return false;
3202 for (i = 0; i < loop->num_nodes; i++)
3204 gimple_stmt_iterator gsi;
3206 for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
3208 gimple stmt = gsi_stmt (gsi);
3209 if (is_gimple_debug (stmt))
3210 continue;
3211 ++*n_stmts;
3212 if (!find_data_references_in_stmt (loop, stmt, &datarefs))
3214 if (is_gimple_call (stmt) && loop->safelen)
3216 tree fndecl = gimple_call_fndecl (stmt), op;
3217 if (fndecl != NULL_TREE)
3219 struct cgraph_node *node = cgraph_get_node (fndecl);
3220 if (node != NULL && node->simd_clones != NULL)
3222 unsigned int j, n = gimple_call_num_args (stmt);
3223 for (j = 0; j < n; j++)
3225 op = gimple_call_arg (stmt, j);
3226 if (DECL_P (op)
3227 || (REFERENCE_CLASS_P (op)
3228 && get_base_address (op)))
3229 break;
3231 op = gimple_call_lhs (stmt);
3232 /* Ignore #pragma omp declare simd functions
3233 if they don't have data references in the
3234 call stmt itself. */
3235 if (j == n
3236 && !(op
3237 && (DECL_P (op)
3238 || (REFERENCE_CLASS_P (op)
3239 && get_base_address (op)))))
3240 continue;
3244 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3245 if (dump_enabled_p ())
3246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3247 "not vectorized: loop contains function "
3248 "calls or data references that cannot "
3249 "be analyzed\n");
3250 return false;
3255 LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
3257 else
3259 gimple_stmt_iterator gsi;
3261 bb = BB_VINFO_BB (bb_vinfo);
3262 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3264 gimple stmt = gsi_stmt (gsi);
3265 if (is_gimple_debug (stmt))
3266 continue;
3267 ++*n_stmts;
3268 if (!find_data_references_in_stmt (NULL, stmt,
3269 &BB_VINFO_DATAREFS (bb_vinfo)))
3271 /* Mark the rest of the basic-block as unvectorizable. */
3272 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3274 stmt = gsi_stmt (gsi);
3275 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
3277 break;
3281 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
3284 /* Go through the data-refs, check that the analysis succeeded. Update
3285 pointer from stmt_vec_info struct to DR and vectype. */
3287 FOR_EACH_VEC_ELT (datarefs, i, dr)
3289 gimple stmt;
3290 stmt_vec_info stmt_info;
3291 tree base, offset, init;
3292 bool gather = false;
3293 bool simd_lane_access = false;
3294 int vf;
3296 again:
3297 if (!dr || !DR_REF (dr))
3299 if (dump_enabled_p ())
3300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3301 "not vectorized: unhandled data-ref\n");
3302 return false;
3305 stmt = DR_STMT (dr);
3306 stmt_info = vinfo_for_stmt (stmt);
3308 /* Discard clobbers from the dataref vector. We will remove
3309 clobber stmts during vectorization. */
3310 if (gimple_clobber_p (stmt))
3312 free_data_ref (dr);
3313 if (i == datarefs.length () - 1)
3315 datarefs.pop ();
3316 break;
3318 datarefs.ordered_remove (i);
3319 dr = datarefs[i];
3320 goto again;
3323 /* Check that analysis of the data-ref succeeded. */
3324 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3325 || !DR_STEP (dr))
3327 bool maybe_gather
3328 = DR_IS_READ (dr)
3329 && !TREE_THIS_VOLATILE (DR_REF (dr))
3330 && targetm.vectorize.builtin_gather != NULL;
3331 bool maybe_simd_lane_access
3332 = loop_vinfo && loop->simduid;
3334 /* If target supports vector gather loads, or if this might be
3335 a SIMD lane access, see if they can't be used. */
3336 if (loop_vinfo
3337 && (maybe_gather || maybe_simd_lane_access)
3338 && !nested_in_vect_loop_p (loop, stmt))
3340 struct data_reference *newdr
3341 = create_data_ref (NULL, loop_containing_stmt (stmt),
3342 DR_REF (dr), stmt, true);
3343 gcc_assert (newdr != NULL && DR_REF (newdr));
3344 if (DR_BASE_ADDRESS (newdr)
3345 && DR_OFFSET (newdr)
3346 && DR_INIT (newdr)
3347 && DR_STEP (newdr)
3348 && integer_zerop (DR_STEP (newdr)))
3350 if (maybe_simd_lane_access)
3352 tree off = DR_OFFSET (newdr);
3353 STRIP_NOPS (off);
3354 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3355 && TREE_CODE (off) == MULT_EXPR
3356 && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
3358 tree step = TREE_OPERAND (off, 1);
3359 off = TREE_OPERAND (off, 0);
3360 STRIP_NOPS (off);
3361 if (CONVERT_EXPR_P (off)
3362 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3363 0)))
3364 < TYPE_PRECISION (TREE_TYPE (off)))
3365 off = TREE_OPERAND (off, 0);
3366 if (TREE_CODE (off) == SSA_NAME)
3368 gimple def = SSA_NAME_DEF_STMT (off);
3369 tree reft = TREE_TYPE (DR_REF (newdr));
3370 if (is_gimple_call (def)
3371 && gimple_call_internal_p (def)
3372 && (gimple_call_internal_fn (def)
3373 == IFN_GOMP_SIMD_LANE))
3375 tree arg = gimple_call_arg (def, 0);
3376 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3377 arg = SSA_NAME_VAR (arg);
3378 if (arg == loop->simduid
3379 /* For now. */
3380 && tree_int_cst_equal
3381 (TYPE_SIZE_UNIT (reft),
3382 step))
3384 DR_OFFSET (newdr) = ssize_int (0);
3385 DR_STEP (newdr) = step;
3386 DR_ALIGNED_TO (newdr)
3387 = size_int (BIGGEST_ALIGNMENT);
3388 dr = newdr;
3389 simd_lane_access = true;
3395 if (!simd_lane_access && maybe_gather)
3397 dr = newdr;
3398 gather = true;
3401 if (!gather && !simd_lane_access)
3402 free_data_ref (newdr);
3405 if (!gather && !simd_lane_access)
3407 if (dump_enabled_p ())
3409 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3410 "not vectorized: data ref analysis "
3411 "failed ");
3412 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3413 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3416 if (bb_vinfo)
3417 break;
3419 return false;
3423 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3425 if (dump_enabled_p ())
3426 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3427 "not vectorized: base addr of dr is a "
3428 "constant\n");
3430 if (bb_vinfo)
3431 break;
3433 if (gather || simd_lane_access)
3434 free_data_ref (dr);
3435 return false;
3438 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3440 if (dump_enabled_p ())
3442 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3443 "not vectorized: volatile type ");
3444 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3445 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3448 if (bb_vinfo)
3449 break;
3451 return false;
3454 if (stmt_can_throw_internal (stmt))
3456 if (dump_enabled_p ())
3458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3459 "not vectorized: statement can throw an "
3460 "exception ");
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 if (gather || simd_lane_access)
3469 free_data_ref (dr);
3470 return false;
3473 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3474 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3476 if (dump_enabled_p ())
3478 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3479 "not vectorized: statement is bitfield "
3480 "access ");
3481 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3482 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3485 if (bb_vinfo)
3486 break;
3488 if (gather || simd_lane_access)
3489 free_data_ref (dr);
3490 return false;
3493 base = unshare_expr (DR_BASE_ADDRESS (dr));
3494 offset = unshare_expr (DR_OFFSET (dr));
3495 init = unshare_expr (DR_INIT (dr));
3497 if (is_gimple_call (stmt)
3498 && (!gimple_call_internal_p (stmt)
3499 || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
3500 && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
3502 if (dump_enabled_p ())
3504 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3505 "not vectorized: dr in a call ");
3506 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3507 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3510 if (bb_vinfo)
3511 break;
3513 if (gather || simd_lane_access)
3514 free_data_ref (dr);
3515 return false;
3518 /* Update DR field in stmt_vec_info struct. */
3520 /* If the dataref is in an inner-loop of the loop that is considered for
3521 for vectorization, we also want to analyze the access relative to
3522 the outer-loop (DR contains information only relative to the
3523 inner-most enclosing loop). We do that by building a reference to the
3524 first location accessed by the inner-loop, and analyze it relative to
3525 the outer-loop. */
3526 if (loop && nested_in_vect_loop_p (loop, stmt))
3528 tree outer_step, outer_base, outer_init;
3529 HOST_WIDE_INT pbitsize, pbitpos;
3530 tree poffset;
3531 enum machine_mode pmode;
3532 int punsignedp, pvolatilep;
3533 affine_iv base_iv, offset_iv;
3534 tree dinit;
3536 /* Build a reference to the first location accessed by the
3537 inner-loop: *(BASE+INIT). (The first location is actually
3538 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3539 tree inner_base = build_fold_indirect_ref
3540 (fold_build_pointer_plus (base, init));
3542 if (dump_enabled_p ())
3544 dump_printf_loc (MSG_NOTE, vect_location,
3545 "analyze in outer-loop: ");
3546 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3547 dump_printf (MSG_NOTE, "\n");
3550 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3551 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3552 gcc_assert (outer_base != NULL_TREE);
3554 if (pbitpos % BITS_PER_UNIT != 0)
3556 if (dump_enabled_p ())
3557 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3558 "failed: bit offset alignment.\n");
3559 return false;
3562 outer_base = build_fold_addr_expr (outer_base);
3563 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3564 &base_iv, false))
3566 if (dump_enabled_p ())
3567 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3568 "failed: evolution of base is not affine.\n");
3569 return false;
3572 if (offset)
3574 if (poffset)
3575 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3576 poffset);
3577 else
3578 poffset = offset;
3581 if (!poffset)
3583 offset_iv.base = ssize_int (0);
3584 offset_iv.step = ssize_int (0);
3586 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3587 &offset_iv, false))
3589 if (dump_enabled_p ())
3590 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3591 "evolution of offset is not affine.\n");
3592 return false;
3595 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3596 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3597 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3598 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3599 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3601 outer_step = size_binop (PLUS_EXPR,
3602 fold_convert (ssizetype, base_iv.step),
3603 fold_convert (ssizetype, offset_iv.step));
3605 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3606 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3607 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3608 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3609 STMT_VINFO_DR_OFFSET (stmt_info) =
3610 fold_convert (ssizetype, offset_iv.base);
3611 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3612 size_int (highest_pow2_factor (offset_iv.base));
3614 if (dump_enabled_p ())
3616 dump_printf_loc (MSG_NOTE, vect_location,
3617 "\touter base_address: ");
3618 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3619 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3620 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3621 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3622 STMT_VINFO_DR_OFFSET (stmt_info));
3623 dump_printf (MSG_NOTE,
3624 "\n\touter constant offset from base address: ");
3625 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3626 STMT_VINFO_DR_INIT (stmt_info));
3627 dump_printf (MSG_NOTE, "\n\touter step: ");
3628 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3629 STMT_VINFO_DR_STEP (stmt_info));
3630 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3631 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3632 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3633 dump_printf (MSG_NOTE, "\n");
3637 if (STMT_VINFO_DATA_REF (stmt_info))
3639 if (dump_enabled_p ())
3641 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3642 "not vectorized: more than one data ref "
3643 "in stmt: ");
3644 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3645 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3648 if (bb_vinfo)
3649 break;
3651 if (gather || simd_lane_access)
3652 free_data_ref (dr);
3653 return false;
3656 STMT_VINFO_DATA_REF (stmt_info) = dr;
3657 if (simd_lane_access)
3659 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3660 free_data_ref (datarefs[i]);
3661 datarefs[i] = dr;
3664 /* Set vectype for STMT. */
3665 scalar_type = TREE_TYPE (DR_REF (dr));
3666 STMT_VINFO_VECTYPE (stmt_info)
3667 = get_vectype_for_scalar_type (scalar_type);
3668 if (!STMT_VINFO_VECTYPE (stmt_info))
3670 if (dump_enabled_p ())
3672 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3673 "not vectorized: no vectype for stmt: ");
3674 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3675 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3676 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3677 scalar_type);
3678 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3681 if (bb_vinfo)
3682 break;
3684 if (gather || simd_lane_access)
3686 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3687 if (gather)
3688 free_data_ref (dr);
3690 return false;
3692 else
3694 if (dump_enabled_p ())
3696 dump_printf_loc (MSG_NOTE, vect_location,
3697 "got vectype for stmt: ");
3698 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3699 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3700 STMT_VINFO_VECTYPE (stmt_info));
3701 dump_printf (MSG_NOTE, "\n");
3705 /* Adjust the minimal vectorization factor according to the
3706 vector type. */
3707 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3708 if (vf > *min_vf)
3709 *min_vf = vf;
3711 if (gather)
3713 tree off;
3715 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3716 if (gather
3717 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3718 gather = false;
3719 if (!gather)
3721 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3722 free_data_ref (dr);
3723 if (dump_enabled_p ())
3725 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3726 "not vectorized: not suitable for gather "
3727 "load ");
3728 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3729 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3731 return false;
3734 datarefs[i] = dr;
3735 STMT_VINFO_GATHER_P (stmt_info) = true;
3737 else if (loop_vinfo
3738 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3740 if (nested_in_vect_loop_p (loop, stmt)
3741 || !DR_IS_READ (dr))
3743 if (dump_enabled_p ())
3745 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3746 "not vectorized: not suitable for strided "
3747 "load ");
3748 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3749 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3751 return false;
3753 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3757 /* If we stopped analysis at the first dataref we could not analyze
3758 when trying to vectorize a basic-block mark the rest of the datarefs
3759 as not vectorizable and truncate the vector of datarefs. That
3760 avoids spending useless time in analyzing their dependence. */
3761 if (i != datarefs.length ())
3763 gcc_assert (bb_vinfo != NULL);
3764 for (unsigned j = i; j < datarefs.length (); ++j)
3766 data_reference_p dr = datarefs[j];
3767 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3768 free_data_ref (dr);
3770 datarefs.truncate (i);
3773 return true;
3777 /* Function vect_get_new_vect_var.
3779 Returns a name for a new variable. The current naming scheme appends the
3780 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3781 the name of vectorizer generated variables, and appends that to NAME if
3782 provided. */
3784 tree
3785 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3787 const char *prefix;
3788 tree new_vect_var;
3790 switch (var_kind)
3792 case vect_simple_var:
3793 prefix = "vect";
3794 break;
3795 case vect_scalar_var:
3796 prefix = "stmp";
3797 break;
3798 case vect_pointer_var:
3799 prefix = "vectp";
3800 break;
3801 default:
3802 gcc_unreachable ();
3805 if (name)
3807 char* tmp = concat (prefix, "_", name, NULL);
3808 new_vect_var = create_tmp_reg (type, tmp);
3809 free (tmp);
3811 else
3812 new_vect_var = create_tmp_reg (type, prefix);
3814 return new_vect_var;
3818 /* Function vect_create_addr_base_for_vector_ref.
3820 Create an expression that computes the address of the first memory location
3821 that will be accessed for a data reference.
3823 Input:
3824 STMT: The statement containing the data reference.
3825 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3826 OFFSET: Optional. If supplied, it is be added to the initial address.
3827 LOOP: Specify relative to which loop-nest should the address be computed.
3828 For example, when the dataref is in an inner-loop nested in an
3829 outer-loop that is now being vectorized, LOOP can be either the
3830 outer-loop, or the inner-loop. The first memory location accessed
3831 by the following dataref ('in' points to short):
3833 for (i=0; i<N; i++)
3834 for (j=0; j<M; j++)
3835 s += in[i+j]
3837 is as follows:
3838 if LOOP=i_loop: &in (relative to i_loop)
3839 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3841 Output:
3842 1. Return an SSA_NAME whose value is the address of the memory location of
3843 the first vector of the data reference.
3844 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3845 these statement(s) which define the returned SSA_NAME.
3847 FORNOW: We are only handling array accesses with step 1. */
3849 tree
3850 vect_create_addr_base_for_vector_ref (gimple stmt,
3851 gimple_seq *new_stmt_list,
3852 tree offset,
3853 struct loop *loop)
3855 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3856 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3857 tree data_ref_base;
3858 const char *base_name;
3859 tree addr_base;
3860 tree dest;
3861 gimple_seq seq = NULL;
3862 tree base_offset;
3863 tree init;
3864 tree vect_ptr_type;
3865 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3866 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3868 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3870 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3872 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3874 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3875 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3876 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3878 else
3880 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3881 base_offset = unshare_expr (DR_OFFSET (dr));
3882 init = unshare_expr (DR_INIT (dr));
3885 if (loop_vinfo)
3886 base_name = get_name (data_ref_base);
3887 else
3889 base_offset = ssize_int (0);
3890 init = ssize_int (0);
3891 base_name = get_name (DR_REF (dr));
3894 /* Create base_offset */
3895 base_offset = size_binop (PLUS_EXPR,
3896 fold_convert (sizetype, base_offset),
3897 fold_convert (sizetype, init));
3899 if (offset)
3901 offset = fold_build2 (MULT_EXPR, sizetype,
3902 fold_convert (sizetype, offset), step);
3903 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3904 base_offset, offset);
3907 /* base + base_offset */
3908 if (loop_vinfo)
3909 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3910 else
3912 addr_base = build1 (ADDR_EXPR,
3913 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3914 unshare_expr (DR_REF (dr)));
3917 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3918 addr_base = fold_convert (vect_ptr_type, addr_base);
3919 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3920 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3921 gimple_seq_add_seq (new_stmt_list, seq);
3923 if (DR_PTR_INFO (dr)
3924 && TREE_CODE (addr_base) == SSA_NAME)
3926 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3927 if (offset)
3928 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3931 if (dump_enabled_p ())
3933 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3934 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3935 dump_printf (MSG_NOTE, "\n");
3938 return addr_base;
3942 /* Function vect_create_data_ref_ptr.
3944 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3945 location accessed in the loop by STMT, along with the def-use update
3946 chain to appropriately advance the pointer through the loop iterations.
3947 Also set aliasing information for the pointer. This pointer is used by
3948 the callers to this function to create a memory reference expression for
3949 vector load/store access.
3951 Input:
3952 1. STMT: a stmt that references memory. Expected to be of the form
3953 GIMPLE_ASSIGN <name, data-ref> or
3954 GIMPLE_ASSIGN <data-ref, name>.
3955 2. AGGR_TYPE: the type of the reference, which should be either a vector
3956 or an array.
3957 3. AT_LOOP: the loop where the vector memref is to be created.
3958 4. OFFSET (optional): an offset to be added to the initial address accessed
3959 by the data-ref in STMT.
3960 5. BSI: location where the new stmts are to be placed if there is no loop
3961 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
3962 pointing to the initial address.
3964 Output:
3965 1. Declare a new ptr to vector_type, and have it point to the base of the
3966 data reference (initial addressed accessed by the data reference).
3967 For example, for vector of type V8HI, the following code is generated:
3969 v8hi *ap;
3970 ap = (v8hi *)initial_address;
3972 if OFFSET is not supplied:
3973 initial_address = &a[init];
3974 if OFFSET is supplied:
3975 initial_address = &a[init + OFFSET];
3977 Return the initial_address in INITIAL_ADDRESS.
3979 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
3980 update the pointer in each iteration of the loop.
3982 Return the increment stmt that updates the pointer in PTR_INCR.
3984 3. Set INV_P to true if the access pattern of the data reference in the
3985 vectorized loop is invariant. Set it to false otherwise.
3987 4. Return the pointer. */
3989 tree
3990 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
3991 tree offset, tree *initial_address,
3992 gimple_stmt_iterator *gsi, gimple *ptr_incr,
3993 bool only_init, bool *inv_p)
3995 const char *base_name;
3996 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3997 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3998 struct loop *loop = NULL;
3999 bool nested_in_vect_loop = false;
4000 struct loop *containing_loop = NULL;
4001 tree aggr_ptr_type;
4002 tree aggr_ptr;
4003 tree new_temp;
4004 gimple vec_stmt;
4005 gimple_seq new_stmt_list = NULL;
4006 edge pe = NULL;
4007 basic_block new_bb;
4008 tree aggr_ptr_init;
4009 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4010 tree aptr;
4011 gimple_stmt_iterator incr_gsi;
4012 bool insert_after;
4013 tree indx_before_incr, indx_after_incr;
4014 gimple incr;
4015 tree step;
4016 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4018 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
4019 || TREE_CODE (aggr_type) == VECTOR_TYPE);
4021 if (loop_vinfo)
4023 loop = LOOP_VINFO_LOOP (loop_vinfo);
4024 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4025 containing_loop = (gimple_bb (stmt))->loop_father;
4026 pe = loop_preheader_edge (loop);
4028 else
4030 gcc_assert (bb_vinfo);
4031 only_init = true;
4032 *ptr_incr = NULL;
4035 /* Check the step (evolution) of the load in LOOP, and record
4036 whether it's invariant. */
4037 if (nested_in_vect_loop)
4038 step = STMT_VINFO_DR_STEP (stmt_info);
4039 else
4040 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
4042 if (integer_zerop (step))
4043 *inv_p = true;
4044 else
4045 *inv_p = false;
4047 /* Create an expression for the first address accessed by this load
4048 in LOOP. */
4049 base_name = get_name (DR_BASE_ADDRESS (dr));
4051 if (dump_enabled_p ())
4053 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
4054 dump_printf_loc (MSG_NOTE, vect_location,
4055 "create %s-pointer variable to type: ",
4056 get_tree_code_name (TREE_CODE (aggr_type)));
4057 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
4058 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
4059 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
4060 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
4061 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
4062 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
4063 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
4064 else
4065 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
4066 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
4067 dump_printf (MSG_NOTE, "\n");
4070 /* (1) Create the new aggregate-pointer variable.
4071 Vector and array types inherit the alias set of their component
4072 type by default so we need to use a ref-all pointer if the data
4073 reference does not conflict with the created aggregated data
4074 reference because it is not addressable. */
4075 bool need_ref_all = false;
4076 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4077 get_alias_set (DR_REF (dr))))
4078 need_ref_all = true;
4079 /* Likewise for any of the data references in the stmt group. */
4080 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
4082 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
4085 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
4086 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
4087 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
4088 get_alias_set (DR_REF (sdr))))
4090 need_ref_all = true;
4091 break;
4093 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
4095 while (orig_stmt);
4097 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
4098 need_ref_all);
4099 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
4102 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4103 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4104 def-use update cycles for the pointer: one relative to the outer-loop
4105 (LOOP), which is what steps (3) and (4) below do. The other is relative
4106 to the inner-loop (which is the inner-most loop containing the dataref),
4107 and this is done be step (5) below.
4109 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4110 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4111 redundant. Steps (3),(4) create the following:
4113 vp0 = &base_addr;
4114 LOOP: vp1 = phi(vp0,vp2)
4117 vp2 = vp1 + step
4118 goto LOOP
4120 If there is an inner-loop nested in loop, then step (5) will also be
4121 applied, and an additional update in the inner-loop will be created:
4123 vp0 = &base_addr;
4124 LOOP: vp1 = phi(vp0,vp2)
4126 inner: vp3 = phi(vp1,vp4)
4127 vp4 = vp3 + inner_step
4128 if () goto inner
4130 vp2 = vp1 + step
4131 if () goto LOOP */
4133 /* (2) Calculate the initial address of the aggregate-pointer, and set
4134 the aggregate-pointer to point to it before the loop. */
4136 /* Create: (&(base[init_val+offset]) in the loop preheader. */
4138 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
4139 offset, loop);
4140 if (new_stmt_list)
4142 if (pe)
4144 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
4145 gcc_assert (!new_bb);
4147 else
4148 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
4151 *initial_address = new_temp;
4153 /* Create: p = (aggr_type *) initial_base */
4154 if (TREE_CODE (new_temp) != SSA_NAME
4155 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
4157 vec_stmt = gimple_build_assign (aggr_ptr,
4158 fold_convert (aggr_ptr_type, new_temp));
4159 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
4160 /* Copy the points-to information if it exists. */
4161 if (DR_PTR_INFO (dr))
4162 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
4163 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
4164 if (pe)
4166 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
4167 gcc_assert (!new_bb);
4169 else
4170 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
4172 else
4173 aggr_ptr_init = new_temp;
4175 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4176 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4177 inner-loop nested in LOOP (during outer-loop vectorization). */
4179 /* No update in loop is required. */
4180 if (only_init && (!loop_vinfo || at_loop == loop))
4181 aptr = aggr_ptr_init;
4182 else
4184 /* The step of the aggregate pointer is the type size. */
4185 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
4186 /* One exception to the above is when the scalar step of the load in
4187 LOOP is zero. In this case the step here is also zero. */
4188 if (*inv_p)
4189 iv_step = size_zero_node;
4190 else if (tree_int_cst_sgn (step) == -1)
4191 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
4193 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4195 create_iv (aggr_ptr_init,
4196 fold_convert (aggr_ptr_type, iv_step),
4197 aggr_ptr, loop, &incr_gsi, insert_after,
4198 &indx_before_incr, &indx_after_incr);
4199 incr = gsi_stmt (incr_gsi);
4200 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4202 /* Copy the points-to information if it exists. */
4203 if (DR_PTR_INFO (dr))
4205 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4206 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4208 if (ptr_incr)
4209 *ptr_incr = incr;
4211 aptr = indx_before_incr;
4214 if (!nested_in_vect_loop || only_init)
4215 return aptr;
4218 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4219 nested in LOOP, if exists. */
4221 gcc_assert (nested_in_vect_loop);
4222 if (!only_init)
4224 standard_iv_increment_position (containing_loop, &incr_gsi,
4225 &insert_after);
4226 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
4227 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
4228 &indx_after_incr);
4229 incr = gsi_stmt (incr_gsi);
4230 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4232 /* Copy the points-to information if it exists. */
4233 if (DR_PTR_INFO (dr))
4235 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
4236 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
4238 if (ptr_incr)
4239 *ptr_incr = incr;
4241 return indx_before_incr;
4243 else
4244 gcc_unreachable ();
4248 /* Function bump_vector_ptr
4250 Increment a pointer (to a vector type) by vector-size. If requested,
4251 i.e. if PTR-INCR is given, then also connect the new increment stmt
4252 to the existing def-use update-chain of the pointer, by modifying
4253 the PTR_INCR as illustrated below:
4255 The pointer def-use update-chain before this function:
4256 DATAREF_PTR = phi (p_0, p_2)
4257 ....
4258 PTR_INCR: p_2 = DATAREF_PTR + step
4260 The pointer def-use update-chain after this function:
4261 DATAREF_PTR = phi (p_0, p_2)
4262 ....
4263 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4264 ....
4265 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4267 Input:
4268 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4269 in the loop.
4270 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4271 the loop. The increment amount across iterations is expected
4272 to be vector_size.
4273 BSI - location where the new update stmt is to be placed.
4274 STMT - the original scalar memory-access stmt that is being vectorized.
4275 BUMP - optional. The offset by which to bump the pointer. If not given,
4276 the offset is assumed to be vector_size.
4278 Output: Return NEW_DATAREF_PTR as illustrated above.
4282 tree
4283 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
4284 gimple stmt, tree bump)
4286 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4287 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4288 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4289 tree update = TYPE_SIZE_UNIT (vectype);
4290 gimple incr_stmt;
4291 ssa_op_iter iter;
4292 use_operand_p use_p;
4293 tree new_dataref_ptr;
4295 if (bump)
4296 update = bump;
4298 new_dataref_ptr = copy_ssa_name (dataref_ptr, NULL);
4299 incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, new_dataref_ptr,
4300 dataref_ptr, update);
4301 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4303 /* Copy the points-to information if it exists. */
4304 if (DR_PTR_INFO (dr))
4306 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4307 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4310 if (!ptr_incr)
4311 return new_dataref_ptr;
4313 /* Update the vector-pointer's cross-iteration increment. */
4314 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4316 tree use = USE_FROM_PTR (use_p);
4318 if (use == dataref_ptr)
4319 SET_USE (use_p, new_dataref_ptr);
4320 else
4321 gcc_assert (tree_int_cst_compare (use, update) == 0);
4324 return new_dataref_ptr;
4328 /* Function vect_create_destination_var.
4330 Create a new temporary of type VECTYPE. */
4332 tree
4333 vect_create_destination_var (tree scalar_dest, tree vectype)
4335 tree vec_dest;
4336 const char *name;
4337 char *new_name;
4338 tree type;
4339 enum vect_var_kind kind;
4341 kind = vectype ? vect_simple_var : vect_scalar_var;
4342 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4344 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4346 name = get_name (scalar_dest);
4347 if (name)
4348 asprintf (&new_name, "%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4349 else
4350 asprintf (&new_name, "_%u", SSA_NAME_VERSION (scalar_dest));
4351 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4352 free (new_name);
4354 return vec_dest;
4357 /* Function vect_grouped_store_supported.
4359 Returns TRUE if interleave high and interleave low permutations
4360 are supported, and FALSE otherwise. */
4362 bool
4363 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4365 enum machine_mode mode = TYPE_MODE (vectype);
4367 /* vect_permute_store_chain requires the group size to be equal to 3 or
4368 be a power of two. */
4369 if (count != 3 && exact_log2 (count) == -1)
4371 if (dump_enabled_p ())
4372 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4373 "the size of the group of accesses"
4374 " is not a power of 2 or not eqaul to 3\n");
4375 return false;
4378 /* Check that the permutation is supported. */
4379 if (VECTOR_MODE_P (mode))
4381 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4382 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4384 if (count == 3)
4386 unsigned int j0 = 0, j1 = 0, j2 = 0;
4387 unsigned int i, j;
4389 for (j = 0; j < 3; j++)
4391 int nelt0 = ((3 - j) * nelt) % 3;
4392 int nelt1 = ((3 - j) * nelt + 1) % 3;
4393 int nelt2 = ((3 - j) * nelt + 2) % 3;
4394 for (i = 0; i < nelt; i++)
4396 if (3 * i + nelt0 < nelt)
4397 sel[3 * i + nelt0] = j0++;
4398 if (3 * i + nelt1 < nelt)
4399 sel[3 * i + nelt1] = nelt + j1++;
4400 if (3 * i + nelt2 < nelt)
4401 sel[3 * i + nelt2] = 0;
4403 if (!can_vec_perm_p (mode, false, sel))
4405 if (dump_enabled_p ())
4406 dump_printf (MSG_MISSED_OPTIMIZATION,
4407 "permutaion op not supported by target.\n");
4408 return false;
4411 for (i = 0; i < nelt; i++)
4413 if (3 * i + nelt0 < nelt)
4414 sel[3 * i + nelt0] = 3 * i + nelt0;
4415 if (3 * i + nelt1 < nelt)
4416 sel[3 * i + nelt1] = 3 * i + nelt1;
4417 if (3 * i + nelt2 < nelt)
4418 sel[3 * i + nelt2] = nelt + j2++;
4420 if (!can_vec_perm_p (mode, false, sel))
4422 if (dump_enabled_p ())
4423 dump_printf (MSG_MISSED_OPTIMIZATION,
4424 "permutaion op not supported by target.\n");
4425 return false;
4428 return true;
4430 else
4432 /* If length is not equal to 3 then only power of 2 is supported. */
4433 gcc_assert (exact_log2 (count) != -1);
4435 for (i = 0; i < nelt / 2; i++)
4437 sel[i * 2] = i;
4438 sel[i * 2 + 1] = i + nelt;
4440 if (can_vec_perm_p (mode, false, sel))
4442 for (i = 0; i < nelt; i++)
4443 sel[i] += nelt / 2;
4444 if (can_vec_perm_p (mode, false, sel))
4445 return true;
4450 if (dump_enabled_p ())
4451 dump_printf (MSG_MISSED_OPTIMIZATION,
4452 "permutaion op not supported by target.\n");
4453 return false;
4457 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4458 type VECTYPE. */
4460 bool
4461 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4463 return vect_lanes_optab_supported_p ("vec_store_lanes",
4464 vec_store_lanes_optab,
4465 vectype, count);
4469 /* Function vect_permute_store_chain.
4471 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4472 a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
4473 the data correctly for the stores. Return the final references for stores
4474 in RESULT_CHAIN.
4476 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4477 The input is 4 vectors each containing 8 elements. We assign a number to
4478 each element, the input sequence is:
4480 1st vec: 0 1 2 3 4 5 6 7
4481 2nd vec: 8 9 10 11 12 13 14 15
4482 3rd vec: 16 17 18 19 20 21 22 23
4483 4th vec: 24 25 26 27 28 29 30 31
4485 The output sequence should be:
4487 1st vec: 0 8 16 24 1 9 17 25
4488 2nd vec: 2 10 18 26 3 11 19 27
4489 3rd vec: 4 12 20 28 5 13 21 30
4490 4th vec: 6 14 22 30 7 15 23 31
4492 i.e., we interleave the contents of the four vectors in their order.
4494 We use interleave_high/low instructions to create such output. The input of
4495 each interleave_high/low operation is two vectors:
4496 1st vec 2nd vec
4497 0 1 2 3 4 5 6 7
4498 the even elements of the result vector are obtained left-to-right from the
4499 high/low elements of the first vector. The odd elements of the result are
4500 obtained left-to-right from the high/low elements of the second vector.
4501 The output of interleave_high will be: 0 4 1 5
4502 and of interleave_low: 2 6 3 7
4505 The permutation is done in log LENGTH stages. In each stage interleave_high
4506 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4507 where the first argument is taken from the first half of DR_CHAIN and the
4508 second argument from it's second half.
4509 In our example,
4511 I1: interleave_high (1st vec, 3rd vec)
4512 I2: interleave_low (1st vec, 3rd vec)
4513 I3: interleave_high (2nd vec, 4th vec)
4514 I4: interleave_low (2nd vec, 4th vec)
4516 The output for the first stage is:
4518 I1: 0 16 1 17 2 18 3 19
4519 I2: 4 20 5 21 6 22 7 23
4520 I3: 8 24 9 25 10 26 11 27
4521 I4: 12 28 13 29 14 30 15 31
4523 The output of the second stage, i.e. the final result is:
4525 I1: 0 8 16 24 1 9 17 25
4526 I2: 2 10 18 26 3 11 19 27
4527 I3: 4 12 20 28 5 13 21 30
4528 I4: 6 14 22 30 7 15 23 31. */
4530 void
4531 vect_permute_store_chain (vec<tree> dr_chain,
4532 unsigned int length,
4533 gimple stmt,
4534 gimple_stmt_iterator *gsi,
4535 vec<tree> *result_chain)
4537 tree vect1, vect2, high, low;
4538 gimple perm_stmt;
4539 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4540 tree perm_mask_low, perm_mask_high;
4541 tree data_ref;
4542 tree perm3_mask_low, perm3_mask_high;
4543 unsigned int i, n, log_length = exact_log2 (length);
4544 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4545 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4547 result_chain->quick_grow (length);
4548 memcpy (result_chain->address (), dr_chain.address (),
4549 length * sizeof (tree));
4551 if (length == 3)
4553 unsigned int j0 = 0, j1 = 0, j2 = 0;
4555 for (j = 0; j < 3; j++)
4557 int nelt0 = ((3 - j) * nelt) % 3;
4558 int nelt1 = ((3 - j) * nelt + 1) % 3;
4559 int nelt2 = ((3 - j) * nelt + 2) % 3;
4561 for (i = 0; i < nelt; i++)
4563 if (3 * i + nelt0 < nelt)
4564 sel[3 * i + nelt0] = j0++;
4565 if (3 * i + nelt1 < nelt)
4566 sel[3 * i + nelt1] = nelt + j1++;
4567 if (3 * i + nelt2 < nelt)
4568 sel[3 * i + nelt2] = 0;
4570 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
4571 gcc_assert (perm3_mask_low != NULL);
4573 for (i = 0; i < nelt; i++)
4575 if (3 * i + nelt0 < nelt)
4576 sel[3 * i + nelt0] = 3 * i + nelt0;
4577 if (3 * i + nelt1 < nelt)
4578 sel[3 * i + nelt1] = 3 * i + nelt1;
4579 if (3 * i + nelt2 < nelt)
4580 sel[3 * i + nelt2] = nelt + j2++;
4582 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
4583 gcc_assert (perm3_mask_high != NULL);
4585 vect1 = dr_chain[0];
4586 vect2 = dr_chain[1];
4588 /* Create interleaving stmt:
4589 low = VEC_PERM_EXPR <vect1, vect2,
4590 {j, nelt, *, j + 1, nelt + j + 1, *,
4591 j + 2, nelt + j + 2, *, ...}> */
4592 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
4593 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4594 vect1, vect2,
4595 perm3_mask_low);
4596 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4598 vect1 = data_ref;
4599 vect2 = dr_chain[2];
4600 /* Create interleaving stmt:
4601 low = VEC_PERM_EXPR <vect1, vect2,
4602 {0, 1, nelt + j, 3, 4, nelt + j + 1,
4603 6, 7, nelt + j + 2, ...}> */
4604 data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
4605 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4606 vect1, vect2,
4607 perm3_mask_high);
4608 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4609 (*result_chain)[j] = data_ref;
4612 else
4614 /* If length is not equal to 3 then only power of 2 is supported. */
4615 gcc_assert (exact_log2 (length) != -1);
4617 for (i = 0, n = nelt / 2; i < n; i++)
4619 sel[i * 2] = i;
4620 sel[i * 2 + 1] = i + nelt;
4622 perm_mask_high = vect_gen_perm_mask (vectype, sel);
4623 gcc_assert (perm_mask_high != NULL);
4625 for (i = 0; i < nelt; i++)
4626 sel[i] += nelt / 2;
4627 perm_mask_low = vect_gen_perm_mask (vectype, sel);
4628 gcc_assert (perm_mask_low != NULL);
4630 for (i = 0, n = log_length; i < n; i++)
4632 for (j = 0; j < length/2; j++)
4634 vect1 = dr_chain[j];
4635 vect2 = dr_chain[j+length/2];
4637 /* Create interleaving stmt:
4638 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
4639 ...}> */
4640 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4641 perm_stmt
4642 = gimple_build_assign_with_ops (VEC_PERM_EXPR, high,
4643 vect1, vect2, perm_mask_high);
4644 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4645 (*result_chain)[2*j] = high;
4647 /* Create interleaving stmt:
4648 low = VEC_PERM_EXPR <vect1, vect2,
4649 {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
4650 ...}> */
4651 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4652 perm_stmt
4653 = gimple_build_assign_with_ops (VEC_PERM_EXPR, low,
4654 vect1, vect2, perm_mask_low);
4655 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4656 (*result_chain)[2*j+1] = low;
4658 memcpy (dr_chain.address (), result_chain->address (),
4659 length * sizeof (tree));
4664 /* Function vect_setup_realignment
4666 This function is called when vectorizing an unaligned load using
4667 the dr_explicit_realign[_optimized] scheme.
4668 This function generates the following code at the loop prolog:
4670 p = initial_addr;
4671 x msq_init = *(floor(p)); # prolog load
4672 realignment_token = call target_builtin;
4673 loop:
4674 x msq = phi (msq_init, ---)
4676 The stmts marked with x are generated only for the case of
4677 dr_explicit_realign_optimized.
4679 The code above sets up a new (vector) pointer, pointing to the first
4680 location accessed by STMT, and a "floor-aligned" load using that pointer.
4681 It also generates code to compute the "realignment-token" (if the relevant
4682 target hook was defined), and creates a phi-node at the loop-header bb
4683 whose arguments are the result of the prolog-load (created by this
4684 function) and the result of a load that takes place in the loop (to be
4685 created by the caller to this function).
4687 For the case of dr_explicit_realign_optimized:
4688 The caller to this function uses the phi-result (msq) to create the
4689 realignment code inside the loop, and sets up the missing phi argument,
4690 as follows:
4691 loop:
4692 msq = phi (msq_init, lsq)
4693 lsq = *(floor(p')); # load in loop
4694 result = realign_load (msq, lsq, realignment_token);
4696 For the case of dr_explicit_realign:
4697 loop:
4698 msq = *(floor(p)); # load in loop
4699 p' = p + (VS-1);
4700 lsq = *(floor(p')); # load in loop
4701 result = realign_load (msq, lsq, realignment_token);
4703 Input:
4704 STMT - (scalar) load stmt to be vectorized. This load accesses
4705 a memory location that may be unaligned.
4706 BSI - place where new code is to be inserted.
4707 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4708 is used.
4710 Output:
4711 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4712 target hook, if defined.
4713 Return value - the result of the loop-header phi node. */
4715 tree
4716 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4717 tree *realignment_token,
4718 enum dr_alignment_support alignment_support_scheme,
4719 tree init_addr,
4720 struct loop **at_loop)
4722 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4723 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4724 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4725 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4726 struct loop *loop = NULL;
4727 edge pe = NULL;
4728 tree scalar_dest = gimple_assign_lhs (stmt);
4729 tree vec_dest;
4730 gimple inc;
4731 tree ptr;
4732 tree data_ref;
4733 gimple new_stmt;
4734 basic_block new_bb;
4735 tree msq_init = NULL_TREE;
4736 tree new_temp;
4737 gimple phi_stmt;
4738 tree msq = NULL_TREE;
4739 gimple_seq stmts = NULL;
4740 bool inv_p;
4741 bool compute_in_loop = false;
4742 bool nested_in_vect_loop = false;
4743 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4744 struct loop *loop_for_initial_load = NULL;
4746 if (loop_vinfo)
4748 loop = LOOP_VINFO_LOOP (loop_vinfo);
4749 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4752 gcc_assert (alignment_support_scheme == dr_explicit_realign
4753 || alignment_support_scheme == dr_explicit_realign_optimized);
4755 /* We need to generate three things:
4756 1. the misalignment computation
4757 2. the extra vector load (for the optimized realignment scheme).
4758 3. the phi node for the two vectors from which the realignment is
4759 done (for the optimized realignment scheme). */
4761 /* 1. Determine where to generate the misalignment computation.
4763 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4764 calculation will be generated by this function, outside the loop (in the
4765 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4766 caller, inside the loop.
4768 Background: If the misalignment remains fixed throughout the iterations of
4769 the loop, then both realignment schemes are applicable, and also the
4770 misalignment computation can be done outside LOOP. This is because we are
4771 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4772 are a multiple of VS (the Vector Size), and therefore the misalignment in
4773 different vectorized LOOP iterations is always the same.
4774 The problem arises only if the memory access is in an inner-loop nested
4775 inside LOOP, which is now being vectorized using outer-loop vectorization.
4776 This is the only case when the misalignment of the memory access may not
4777 remain fixed throughout the iterations of the inner-loop (as explained in
4778 detail in vect_supportable_dr_alignment). In this case, not only is the
4779 optimized realignment scheme not applicable, but also the misalignment
4780 computation (and generation of the realignment token that is passed to
4781 REALIGN_LOAD) have to be done inside the loop.
4783 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4784 or not, which in turn determines if the misalignment is computed inside
4785 the inner-loop, or outside LOOP. */
4787 if (init_addr != NULL_TREE || !loop_vinfo)
4789 compute_in_loop = true;
4790 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4794 /* 2. Determine where to generate the extra vector load.
4796 For the optimized realignment scheme, instead of generating two vector
4797 loads in each iteration, we generate a single extra vector load in the
4798 preheader of the loop, and in each iteration reuse the result of the
4799 vector load from the previous iteration. In case the memory access is in
4800 an inner-loop nested inside LOOP, which is now being vectorized using
4801 outer-loop vectorization, we need to determine whether this initial vector
4802 load should be generated at the preheader of the inner-loop, or can be
4803 generated at the preheader of LOOP. If the memory access has no evolution
4804 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4805 to be generated inside LOOP (in the preheader of the inner-loop). */
4807 if (nested_in_vect_loop)
4809 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4810 bool invariant_in_outerloop =
4811 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4812 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4814 else
4815 loop_for_initial_load = loop;
4816 if (at_loop)
4817 *at_loop = loop_for_initial_load;
4819 if (loop_for_initial_load)
4820 pe = loop_preheader_edge (loop_for_initial_load);
4822 /* 3. For the case of the optimized realignment, create the first vector
4823 load at the loop preheader. */
4825 if (alignment_support_scheme == dr_explicit_realign_optimized)
4827 /* Create msq_init = *(floor(p1)) in the loop preheader */
4829 gcc_assert (!compute_in_loop);
4830 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4831 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4832 NULL_TREE, &init_addr, NULL, &inc,
4833 true, &inv_p);
4834 new_temp = copy_ssa_name (ptr, NULL);
4835 new_stmt = gimple_build_assign_with_ops
4836 (BIT_AND_EXPR, new_temp, ptr,
4837 build_int_cst (TREE_TYPE (ptr),
4838 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4839 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4840 gcc_assert (!new_bb);
4841 data_ref
4842 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4843 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4844 new_stmt = gimple_build_assign (vec_dest, data_ref);
4845 new_temp = make_ssa_name (vec_dest, new_stmt);
4846 gimple_assign_set_lhs (new_stmt, new_temp);
4847 if (pe)
4849 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4850 gcc_assert (!new_bb);
4852 else
4853 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4855 msq_init = gimple_assign_lhs (new_stmt);
4858 /* 4. Create realignment token using a target builtin, if available.
4859 It is done either inside the containing loop, or before LOOP (as
4860 determined above). */
4862 if (targetm.vectorize.builtin_mask_for_load)
4864 tree builtin_decl;
4866 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4867 if (!init_addr)
4869 /* Generate the INIT_ADDR computation outside LOOP. */
4870 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4871 NULL_TREE, loop);
4872 if (loop)
4874 pe = loop_preheader_edge (loop);
4875 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4876 gcc_assert (!new_bb);
4878 else
4879 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4882 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4883 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4884 vec_dest =
4885 vect_create_destination_var (scalar_dest,
4886 gimple_call_return_type (new_stmt));
4887 new_temp = make_ssa_name (vec_dest, new_stmt);
4888 gimple_call_set_lhs (new_stmt, new_temp);
4890 if (compute_in_loop)
4891 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4892 else
4894 /* Generate the misalignment computation outside LOOP. */
4895 pe = loop_preheader_edge (loop);
4896 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4897 gcc_assert (!new_bb);
4900 *realignment_token = gimple_call_lhs (new_stmt);
4902 /* The result of the CALL_EXPR to this builtin is determined from
4903 the value of the parameter and no global variables are touched
4904 which makes the builtin a "const" function. Requiring the
4905 builtin to have the "const" attribute makes it unnecessary
4906 to call mark_call_clobbered. */
4907 gcc_assert (TREE_READONLY (builtin_decl));
4910 if (alignment_support_scheme == dr_explicit_realign)
4911 return msq;
4913 gcc_assert (!compute_in_loop);
4914 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4917 /* 5. Create msq = phi <msq_init, lsq> in loop */
4919 pe = loop_preheader_edge (containing_loop);
4920 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4921 msq = make_ssa_name (vec_dest, NULL);
4922 phi_stmt = create_phi_node (msq, containing_loop->header);
4923 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4925 return msq;
4929 /* Function vect_grouped_load_supported.
4931 Returns TRUE if even and odd permutations are supported,
4932 and FALSE otherwise. */
4934 bool
4935 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4937 enum machine_mode mode = TYPE_MODE (vectype);
4939 /* vect_permute_load_chain requires the group size to be equal to 3 or
4940 be a power of two. */
4941 if (count != 3 && exact_log2 (count) == -1)
4943 if (dump_enabled_p ())
4944 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4945 "the size of the group of accesses"
4946 " is not a power of 2 or not equal to 3\n");
4947 return false;
4950 /* Check that the permutation is supported. */
4951 if (VECTOR_MODE_P (mode))
4953 unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
4954 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4956 if (count == 3)
4958 unsigned int k;
4959 for (k = 0; k < 3; k++)
4961 for (i = 0; i < nelt; i++)
4962 if (3 * i + k < 2 * nelt)
4963 sel[i] = 3 * i + k;
4964 else
4965 sel[i] = 0;
4966 if (!can_vec_perm_p (mode, false, sel))
4968 if (dump_enabled_p ())
4969 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4970 "shuffle of 3 loads is not supported by"
4971 " target\n");
4972 return false;
4974 for (i = 0, j = 0; i < nelt; i++)
4975 if (3 * i + k < 2 * nelt)
4976 sel[i] = i;
4977 else
4978 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
4979 if (!can_vec_perm_p (mode, false, sel))
4981 if (dump_enabled_p ())
4982 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4983 "shuffle of 3 loads is not supported by"
4984 " target\n");
4985 return false;
4988 return true;
4990 else
4992 /* If length is not equal to 3 then only power of 2 is supported. */
4993 gcc_assert (exact_log2 (count) != -1);
4994 for (i = 0; i < nelt; i++)
4995 sel[i] = i * 2;
4996 if (can_vec_perm_p (mode, false, sel))
4998 for (i = 0; i < nelt; i++)
4999 sel[i] = i * 2 + 1;
5000 if (can_vec_perm_p (mode, false, sel))
5001 return true;
5006 if (dump_enabled_p ())
5007 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5008 "extract even/odd not supported by target\n");
5009 return false;
5012 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
5013 type VECTYPE. */
5015 bool
5016 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
5018 return vect_lanes_optab_supported_p ("vec_load_lanes",
5019 vec_load_lanes_optab,
5020 vectype, count);
5023 /* Function vect_permute_load_chain.
5025 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
5026 a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
5027 the input data correctly. Return the final references for loads in
5028 RESULT_CHAIN.
5030 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
5031 The input is 4 vectors each containing 8 elements. We assign a number to each
5032 element, the input sequence is:
5034 1st vec: 0 1 2 3 4 5 6 7
5035 2nd vec: 8 9 10 11 12 13 14 15
5036 3rd vec: 16 17 18 19 20 21 22 23
5037 4th vec: 24 25 26 27 28 29 30 31
5039 The output sequence should be:
5041 1st vec: 0 4 8 12 16 20 24 28
5042 2nd vec: 1 5 9 13 17 21 25 29
5043 3rd vec: 2 6 10 14 18 22 26 30
5044 4th vec: 3 7 11 15 19 23 27 31
5046 i.e., the first output vector should contain the first elements of each
5047 interleaving group, etc.
5049 We use extract_even/odd instructions to create such output. The input of
5050 each extract_even/odd operation is two vectors
5051 1st vec 2nd vec
5052 0 1 2 3 4 5 6 7
5054 and the output is the vector of extracted even/odd elements. The output of
5055 extract_even will be: 0 2 4 6
5056 and of extract_odd: 1 3 5 7
5059 The permutation is done in log LENGTH stages. In each stage extract_even
5060 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
5061 their order. In our example,
5063 E1: extract_even (1st vec, 2nd vec)
5064 E2: extract_odd (1st vec, 2nd vec)
5065 E3: extract_even (3rd vec, 4th vec)
5066 E4: extract_odd (3rd vec, 4th vec)
5068 The output for the first stage will be:
5070 E1: 0 2 4 6 8 10 12 14
5071 E2: 1 3 5 7 9 11 13 15
5072 E3: 16 18 20 22 24 26 28 30
5073 E4: 17 19 21 23 25 27 29 31
5075 In order to proceed and create the correct sequence for the next stage (or
5076 for the correct output, if the second stage is the last one, as in our
5077 example), we first put the output of extract_even operation and then the
5078 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
5079 The input for the second stage is:
5081 1st vec (E1): 0 2 4 6 8 10 12 14
5082 2nd vec (E3): 16 18 20 22 24 26 28 30
5083 3rd vec (E2): 1 3 5 7 9 11 13 15
5084 4th vec (E4): 17 19 21 23 25 27 29 31
5086 The output of the second stage:
5088 E1: 0 4 8 12 16 20 24 28
5089 E2: 2 6 10 14 18 22 26 30
5090 E3: 1 5 9 13 17 21 25 29
5091 E4: 3 7 11 15 19 23 27 31
5093 And RESULT_CHAIN after reordering:
5095 1st vec (E1): 0 4 8 12 16 20 24 28
5096 2nd vec (E3): 1 5 9 13 17 21 25 29
5097 3rd vec (E2): 2 6 10 14 18 22 26 30
5098 4th vec (E4): 3 7 11 15 19 23 27 31. */
5100 static void
5101 vect_permute_load_chain (vec<tree> dr_chain,
5102 unsigned int length,
5103 gimple stmt,
5104 gimple_stmt_iterator *gsi,
5105 vec<tree> *result_chain)
5107 tree data_ref, first_vect, second_vect;
5108 tree perm_mask_even, perm_mask_odd;
5109 tree perm3_mask_low, perm3_mask_high;
5110 gimple perm_stmt;
5111 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
5112 unsigned int i, j, log_length = exact_log2 (length);
5113 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
5114 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
5116 result_chain->quick_grow (length);
5117 memcpy (result_chain->address (), dr_chain.address (),
5118 length * sizeof (tree));
5120 if (length == 3)
5122 unsigned int k;
5124 for (k = 0; k < 3; k++)
5126 for (i = 0; i < nelt; i++)
5127 if (3 * i + k < 2 * nelt)
5128 sel[i] = 3 * i + k;
5129 else
5130 sel[i] = 0;
5131 perm3_mask_low = vect_gen_perm_mask (vectype, sel);
5132 gcc_assert (perm3_mask_low != NULL);
5134 for (i = 0, j = 0; i < nelt; i++)
5135 if (3 * i + k < 2 * nelt)
5136 sel[i] = i;
5137 else
5138 sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
5140 perm3_mask_high = vect_gen_perm_mask (vectype, sel);
5141 gcc_assert (perm3_mask_high != NULL);
5143 first_vect = dr_chain[0];
5144 second_vect = dr_chain[1];
5146 /* Create interleaving stmt (low part of):
5147 low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5148 ...}> */
5149 data_ref = make_temp_ssa_name (vectype, NULL, "vect_suffle3_low");
5150 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5151 first_vect, second_vect,
5152 perm3_mask_low);
5153 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5155 /* Create interleaving stmt (high part of):
5156 high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
5157 ...}> */
5158 first_vect = data_ref;
5159 second_vect = dr_chain[2];
5160 data_ref = make_temp_ssa_name (vectype, NULL, "vect_suffle3_high");
5161 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5162 first_vect, second_vect,
5163 perm3_mask_high);
5164 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5165 (*result_chain)[k] = data_ref;
5168 else
5170 /* If length is not equal to 3 then only power of 2 is supported. */
5171 gcc_assert (exact_log2 (length) != -1);
5173 for (i = 0; i < nelt; ++i)
5174 sel[i] = i * 2;
5175 perm_mask_even = vect_gen_perm_mask (vectype, sel);
5176 gcc_assert (perm_mask_even != NULL);
5178 for (i = 0; i < nelt; ++i)
5179 sel[i] = i * 2 + 1;
5180 perm_mask_odd = vect_gen_perm_mask (vectype, sel);
5181 gcc_assert (perm_mask_odd != NULL);
5183 for (i = 0; i < log_length; i++)
5185 for (j = 0; j < length; j += 2)
5187 first_vect = dr_chain[j];
5188 second_vect = dr_chain[j+1];
5190 /* data_ref = permute_even (first_data_ref, second_data_ref); */
5191 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
5192 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5193 first_vect, second_vect,
5194 perm_mask_even);
5195 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5196 (*result_chain)[j/2] = data_ref;
5198 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5199 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
5200 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5201 first_vect, second_vect,
5202 perm_mask_odd);
5203 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5204 (*result_chain)[j/2+length/2] = data_ref;
5206 memcpy (dr_chain.address (), result_chain->address (),
5207 length * sizeof (tree));
5212 /* Function vect_transform_grouped_load.
5214 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5215 to perform their permutation and ascribe the result vectorized statements to
5216 the scalar statements.
5219 void
5220 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
5221 gimple_stmt_iterator *gsi)
5223 vec<tree> result_chain = vNULL;
5225 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5226 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5227 vectors, that are ready for vector computation. */
5228 result_chain.create (size);
5229 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
5230 vect_record_grouped_load_vectors (stmt, result_chain);
5231 result_chain.release ();
5234 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5235 generated as part of the vectorization of STMT. Assign the statement
5236 for each vector to the associated scalar statement. */
5238 void
5239 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
5241 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
5242 gimple next_stmt, new_stmt;
5243 unsigned int i, gap_count;
5244 tree tmp_data_ref;
5246 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5247 Since we scan the chain starting from it's first node, their order
5248 corresponds the order of data-refs in RESULT_CHAIN. */
5249 next_stmt = first_stmt;
5250 gap_count = 1;
5251 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
5253 if (!next_stmt)
5254 break;
5256 /* Skip the gaps. Loads created for the gaps will be removed by dead
5257 code elimination pass later. No need to check for the first stmt in
5258 the group, since it always exists.
5259 GROUP_GAP is the number of steps in elements from the previous
5260 access (if there is no gap GROUP_GAP is 1). We skip loads that
5261 correspond to the gaps. */
5262 if (next_stmt != first_stmt
5263 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
5265 gap_count++;
5266 continue;
5269 while (next_stmt)
5271 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
5272 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5273 copies, and we put the new vector statement in the first available
5274 RELATED_STMT. */
5275 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
5276 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
5277 else
5279 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5281 gimple prev_stmt =
5282 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
5283 gimple rel_stmt =
5284 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
5285 while (rel_stmt)
5287 prev_stmt = rel_stmt;
5288 rel_stmt =
5289 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
5292 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
5293 new_stmt;
5297 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5298 gap_count = 1;
5299 /* If NEXT_STMT accesses the same DR as the previous statement,
5300 put the same TMP_DATA_REF as its vectorized statement; otherwise
5301 get the next data-ref from RESULT_CHAIN. */
5302 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
5303 break;
5308 /* Function vect_force_dr_alignment_p.
5310 Returns whether the alignment of a DECL can be forced to be aligned
5311 on ALIGNMENT bit boundary. */
5313 bool
5314 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
5316 if (TREE_CODE (decl) != VAR_DECL)
5317 return false;
5319 /* With -fno-toplevel-reorder we may have already output the constant. */
5320 if (TREE_ASM_WRITTEN (decl))
5321 return false;
5323 /* Constant pool entries may be shared and not properly merged by LTO. */
5324 if (DECL_IN_CONSTANT_POOL (decl))
5325 return false;
5327 if (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
5329 symtab_node *snode;
5331 /* We cannot change alignment of symbols that may bind to symbols
5332 in other translation unit that may contain a definition with lower
5333 alignment. */
5334 if (!decl_binds_to_current_def_p (decl))
5335 return false;
5337 /* When compiling partition, be sure the symbol is not output by other
5338 partition. */
5339 snode = symtab_get_node (decl);
5340 if (flag_ltrans
5341 && (snode->in_other_partition
5342 || symtab_get_symbol_partitioning_class (snode) == SYMBOL_DUPLICATE))
5343 return false;
5346 /* Do not override the alignment as specified by the ABI when the used
5347 attribute is set. */
5348 if (DECL_PRESERVE_P (decl))
5349 return false;
5351 /* Do not override explicit alignment set by the user when an explicit
5352 section name is also used. This is a common idiom used by many
5353 software projects. */
5354 if (TREE_STATIC (decl)
5355 && DECL_SECTION_NAME (decl) != NULL
5356 && !symtab_get_node (decl)->implicit_section)
5357 return false;
5359 /* If symbol is an alias, we need to check that target is OK. */
5360 if (TREE_STATIC (decl))
5362 tree target = symtab_alias_ultimate_target (symtab_get_node (decl))->decl;
5363 if (target != decl)
5365 if (DECL_PRESERVE_P (target))
5366 return false;
5367 decl = target;
5371 if (TREE_STATIC (decl))
5372 return (alignment <= MAX_OFILE_ALIGNMENT);
5373 else
5374 return (alignment <= MAX_STACK_ALIGNMENT);
5378 /* Return whether the data reference DR is supported with respect to its
5379 alignment.
5380 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5381 it is aligned, i.e., check if it is possible to vectorize it with different
5382 alignment. */
5384 enum dr_alignment_support
5385 vect_supportable_dr_alignment (struct data_reference *dr,
5386 bool check_aligned_accesses)
5388 gimple stmt = DR_STMT (dr);
5389 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5390 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5391 enum machine_mode mode = TYPE_MODE (vectype);
5392 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5393 struct loop *vect_loop = NULL;
5394 bool nested_in_vect_loop = false;
5396 if (aligned_access_p (dr) && !check_aligned_accesses)
5397 return dr_aligned;
5399 /* For now assume all conditional loads/stores support unaligned
5400 access without any special code. */
5401 if (is_gimple_call (stmt)
5402 && gimple_call_internal_p (stmt)
5403 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
5404 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
5405 return dr_unaligned_supported;
5407 if (loop_vinfo)
5409 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
5410 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
5413 /* Possibly unaligned access. */
5415 /* We can choose between using the implicit realignment scheme (generating
5416 a misaligned_move stmt) and the explicit realignment scheme (generating
5417 aligned loads with a REALIGN_LOAD). There are two variants to the
5418 explicit realignment scheme: optimized, and unoptimized.
5419 We can optimize the realignment only if the step between consecutive
5420 vector loads is equal to the vector size. Since the vector memory
5421 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5422 is guaranteed that the misalignment amount remains the same throughout the
5423 execution of the vectorized loop. Therefore, we can create the
5424 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5425 at the loop preheader.
5427 However, in the case of outer-loop vectorization, when vectorizing a
5428 memory access in the inner-loop nested within the LOOP that is now being
5429 vectorized, while it is guaranteed that the misalignment of the
5430 vectorized memory access will remain the same in different outer-loop
5431 iterations, it is *not* guaranteed that is will remain the same throughout
5432 the execution of the inner-loop. This is because the inner-loop advances
5433 with the original scalar step (and not in steps of VS). If the inner-loop
5434 step happens to be a multiple of VS, then the misalignment remains fixed
5435 and we can use the optimized realignment scheme. For example:
5437 for (i=0; i<N; i++)
5438 for (j=0; j<M; j++)
5439 s += a[i+j];
5441 When vectorizing the i-loop in the above example, the step between
5442 consecutive vector loads is 1, and so the misalignment does not remain
5443 fixed across the execution of the inner-loop, and the realignment cannot
5444 be optimized (as illustrated in the following pseudo vectorized loop):
5446 for (i=0; i<N; i+=4)
5447 for (j=0; j<M; j++){
5448 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5449 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5450 // (assuming that we start from an aligned address).
5453 We therefore have to use the unoptimized realignment scheme:
5455 for (i=0; i<N; i+=4)
5456 for (j=k; j<M; j+=4)
5457 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5458 // that the misalignment of the initial address is
5459 // 0).
5461 The loop can then be vectorized as follows:
5463 for (k=0; k<4; k++){
5464 rt = get_realignment_token (&vp[k]);
5465 for (i=0; i<N; i+=4){
5466 v1 = vp[i+k];
5467 for (j=k; j<M; j+=4){
5468 v2 = vp[i+j+VS-1];
5469 va = REALIGN_LOAD <v1,v2,rt>;
5470 vs += va;
5471 v1 = v2;
5474 } */
5476 if (DR_IS_READ (dr))
5478 bool is_packed = false;
5479 tree type = (TREE_TYPE (DR_REF (dr)));
5481 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
5482 && (!targetm.vectorize.builtin_mask_for_load
5483 || targetm.vectorize.builtin_mask_for_load ()))
5485 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5486 if ((nested_in_vect_loop
5487 && (TREE_INT_CST_LOW (DR_STEP (dr))
5488 != GET_MODE_SIZE (TYPE_MODE (vectype))))
5489 || !loop_vinfo)
5490 return dr_explicit_realign;
5491 else
5492 return dr_explicit_realign_optimized;
5494 if (!known_alignment_for_access_p (dr))
5495 is_packed = not_size_aligned (DR_REF (dr));
5497 if ((TYPE_USER_ALIGN (type) && !is_packed)
5498 || targetm.vectorize.
5499 support_vector_misalignment (mode, type,
5500 DR_MISALIGNMENT (dr), is_packed))
5501 /* Can't software pipeline the loads, but can at least do them. */
5502 return dr_unaligned_supported;
5504 else
5506 bool is_packed = false;
5507 tree type = (TREE_TYPE (DR_REF (dr)));
5509 if (!known_alignment_for_access_p (dr))
5510 is_packed = not_size_aligned (DR_REF (dr));
5512 if ((TYPE_USER_ALIGN (type) && !is_packed)
5513 || targetm.vectorize.
5514 support_vector_misalignment (mode, type,
5515 DR_MISALIGNMENT (dr), is_packed))
5516 return dr_unaligned_supported;
5519 /* Unsupported. */
5520 return dr_unaligned_unsupported;